Пример #1
0
/**
 * \brief Test checking different registers of the USART module
 *
 * This function calls the different check functions and make sure they set the
 * correct values.
 *
 * \param test Current test case.
 */
static void run_check_registers_test(const struct test_case *test)
{
	bool success;
	uint8_t data = 'b';

	
	const usart_rs232_options_t options = {
		.baudrate   = CONF_UNIT_BAUDRATE,
		.charlength = CONF_UNIT_CHARLENGTH,
		.paritytype = CONF_UNIT_PARITY,
		.stopbits   = CONF_UNIT_STOPBITS
	};

	usart_init_rs232(&CONF_UNIT_USART, &options);
      
	/* Test empty data register */
	success = usart_data_register_is_empty(&CONF_UNIT_USART);
	test_assert_true(test, success,
			"Checking if the data register is empty failed");
	
	/* Test finished data transfers */
	usart_put(&CONF_UNIT_USART, data);
	for(volatile uint16_t delay=0;delay<20000;delay++);
    
	success = usart_rx_is_complete(&CONF_UNIT_USART);
	usart_get(&CONF_UNIT_USART);
	test_assert_true(test, success,
   	                "Checking if the receive is finished failed");		       
					
	success = usart_tx_is_complete(&CONF_UNIT_USART);
	test_assert_true(test, success,
	                "Checking if the transmit is finished failed");

}
Пример #2
0
/**
 * \brief Receive a data with the USART module
 *
 * This function returns the received data from the USART module.
 *
 * \param usart The USART module.
 *
 * \return The received data.
 */
uint8_t usart_getchar(USART_t *usart)
{
	while (usart_rx_is_complete(usart) == false) {
	}
	
	return ((uint8_t)(usart)->DATA);
}
/**
 * \internal
 * \brief Helper function to read a byte from an arbitrary interface
 *
 * This function is used to hide what interface is used by the component
 * driver, e.g.  the component driver does not need to know if USART in SPI
 * mode is used or the native SPI module.
 *
 * \retval uint8_t Byte of data read from the display controller
 */
__always_inline static uint8_t ili9341_read_byte(void)
{
	uint8_t data;

#if defined(CONF_ILI9341_USART_SPI)
#  if XMEGA
	/* Workaround for clearing the RXCIF for XMEGA */
	usart_rx_enable(CONF_ILI9341_USART_SPI);

	usart_put(CONF_ILI9341_USART_SPI, 0xFF);
	while (!usart_rx_is_complete(CONF_ILI9341_USART_SPI)) {
		/* Do nothing */
	}
	data = usart_get(CONF_ILI9341_USART_SPI);

	/* Workaround for clearing the RXCIF for XMEGA */
	usart_rx_disable(CONF_ILI9341_USART_SPI);
#  else
	usart_spi_read_single(CONF_ILI9341_USART_SPI, &data);
#  endif
#elif defined(CONF_ILI9341_SPI)
	spi_write_single(CONF_ILI9341_SPI, 0xFF);

	ili9341_wait_for_send_done();

	/* Wait for RX to complete */
	while (!spi_is_rx_full(CONF_ILI9341_SPI)) {
		/* Do nothing */
	}

	spi_read_single(CONF_ILI9341_SPI, &data);
#endif

	return data;
}
Пример #4
0
int main(void)
{
	const usart_serial_options_t usart_serial_options = {
		.baudrate   = CONF_TEST_BAUDRATE,
		.charlength = CONF_TEST_CHARLENGTH,
		.paritytype = CONF_TEST_PARITY,
		.stopbits   = CONF_TEST_STOPBITS,
	};

	/* Usual initializations */
	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

	printf("\x0C\n\r-- ADC Averaging Example --\n\r");
	printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__);

	printf("Commands:\n\r");
	printf("- key 'a' to enable averaging\n\r");
	printf("- key 'd' to disable averaging\n\r");

	/* ADC initialization */
	main_adc_init();
	main_adc_averaging_stop();

	while (1) {
		if (usart_rx_is_complete(CONF_TEST_USART)) {
			char key = getchar();
			if (key == 'a') {
				main_adc_averaging_start();
			}

			if (key == 'd') {
				main_adc_averaging_stop();
			}
		}

		/* Wait sample with or without average */
		uint16_t sample;
		adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);
		sample = adc_get_unsigned_result(&ADCA, ADC_CH0);
		printf("ADC Value: %4u\r", sample);
	}
}
int main(void)
{
	const usart_serial_options_t usart_serial_options = {
		.baudrate   = CONF_TEST_BAUDRATE,
		.charlength = CONF_TEST_CHARLENGTH,
		.paritytype = CONF_TEST_PARITY,
		.stopbits   = CONF_TEST_STOPBITS,
	};

	/* Usual initializations */
	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

	printf("\x0C\n\r-- ADC Calibration Example --\n\r");
	printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__);

	/* ADC initializations */
	main_adc_init();

	printf("Commands:\n\r");
	printf("- key 'c' to enable correction\n\r");
	printf("- key 'd' to disable correction\n\r");
	main_adc_correction_stop();

	while (1) {
		if (usart_rx_is_complete(CONF_TEST_USART)) {
			char key = getchar();
			if (key == 'c') {
				main_adc_correction_start();
			}

			if (key == 'd') {
				main_adc_correction_stop();
			}
		}

		printf(" %4d mV\r", main_adc_input());
	}
}
int main(void)
{
    const usart_serial_options_t usart_serial_options = {
        .baudrate   = CONF_TEST_BAUDRATE,
        .charlength = CONF_TEST_CHARLENGTH,
        .paritytype = CONF_TEST_PARITY,
        .stopbits   = CONF_TEST_STOPBITS,
    };
    char binary[16 + 1];
    uint8_t i;
    bool oversampling_is_enabled;

    /* Usual initializations */
    board_init();
    sysclk_init();
    sleepmgr_init();
    irq_initialize_vectors();
    cpu_irq_enable();
    stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

    printf("\x0C\n\r-- ADC Over-sampling Example --\n\r");
    printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__);

    printf("Commands:\n\r");
    printf("- key 'o' to enable over-sampling\n\r");
    printf("- key 'd' to disable over-sampling\n\r");

    /* ADC initializations */
    main_adc_init();
    main_adc_oversampling_stop();
    oversampling_is_enabled = false;

    while (1) {
        if (usart_rx_is_complete(CONF_TEST_USART)) {
            char key = getchar();
            if (key == 'o') {
                main_adc_oversampling_start();
                oversampling_is_enabled = true;
            }

            if (key == 'd') {
                main_adc_oversampling_stop();
                oversampling_is_enabled = false;
            }
        }

        /* Wait sample with or without over-sampling */
        uint16_t sample;
        adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);
        sample = adc_get_unsigned_result(&ADCA, ADC_CH0);
        if (!oversampling_is_enabled) {
            sample <<= 4;
        }

        i = 15;
        do {
            binary[i] = '0' + (sample & 1);
            sample >>= 1;
        } while (i--);
        binary[16] = 0;
        printf("ADC Value: %sb\r", binary);
    }
}
Пример #7
0
/**
 * \brief Main application routine
 *  - Configure system clock
 *  - Call QDec driver API to configure it and enable it
 *  - Call generator of quadrature encoder
 *  - Get position, direction and frequency and display its
 */
int main( void )
{
	const usart_serial_options_t usart_serial_options = {
		.baudrate   = CONF_TEST_BAUDRATE,
		.charlength = CONF_TEST_CHARLENGTH,
		.paritytype = CONF_TEST_PARITY,
		.stopbits   = CONF_TEST_STOPBITS,
	};
	qdec_config_t config;
	uint16_t qdec_position, qdec_position_prev = 0;
	uint32_t generator_qenc_freq = 2000 / QUADRATURE_RESOLUTION;
	bool generator_qenc_dir = true;

	/* Usual initializations */
	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

	printf("\x0C\n\r-- QDec Example --\n\r");
	printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__);

	/* QDec configuration
	 * Here, we use the default hardware ressources
	 * proposed by default configuration.
	 */
	qdec_get_config_defaults(&config);
	qdec_config_phase_pins(&config, CONF_QDEC_PORT, CONF_QDEC_PINS_BASE,
			false, 1);
	qdec_config_tc(&config, CONF_QDEC_TC);
	qdec_config_enable_freq(&config, 1);
	qdec_config_freq_tc(&config, CONF_QDEC_FREQ_TC);
	qdec_config_revolution(&config, QUADRATURE_RESOLUTION);
	/* QDec enable */
	qdec_enabled(&config);

	/* Generate quadrature encoder signals */
	generator_qenc_enable(CONF_QDEC_PORT, CONF_QDEC_PINS_BASE,
			CONF_GENERATOR_QENC_TC, QUADRATURE_RESOLUTION,
			generator_qenc_freq, generator_qenc_dir);

	while (1) {
		/* read position */
		qdec_position = qdec_get_position(&config);

		if (qdec_position_prev != qdec_position) {
			/* New position then display it */
			qdec_position_prev = qdec_position;
			printf("%02u", qdec_position);
			/* display direction */
			if (qdec_get_direction(&config)) {
				printf(" ++");
			} else {
				printf(" --");
			}

			/* Display frequency */
			printf(" %5umHz\r\n", qdec_get_frequency(&config));
		}

		/* Manage Quadrature encoder generator through UART */
		if (usart_rx_is_complete(CONF_TEST_USART)) {
			char key = getchar();
			if (key == 'd') {
				generator_qenc_dir = !generator_qenc_dir;
				generator_qenc_set_direction(generator_qenc_dir);
			}

			if (key == '-') {
				if (generator_qenc_freq > QUADRATURE_GENERATOR_FREQ_STEP) {
					generator_qenc_freq -= QUADRATURE_GENERATOR_FREQ_STEP;
					generator_qenc_set_freq(generator_qenc_freq);
				}
			}

			if (key == '+') {
				generator_qenc_freq += QUADRATURE_GENERATOR_FREQ_STEP;
				generator_qenc_set_freq(generator_qenc_freq);
			}
		}
	}
}
Пример #8
0
int bluetooth_is_rx_complete(void)
{
	return usart_rx_is_complete(BLUETOOTH);
}
Пример #9
0
static uint8_t sim900_wait_data_on_usart(uint8_t seconds)
{

	for(uint8_t d1=seconds;d1>0;--d1)
	{
		for(uint16_t d2=10000;d2>0;--d2)
		{
#ifdef SIM900_USART_POLLED
			if(usart_rx_is_complete(USART_GPRS)) {
#else
			if(USART_RX_CBuffer_Data_Available(&sim900_usart_data)) {
#endif				
				return 0xFF;
			}
			delay_us(100);
		}
	}
	
	debug_string(VERY_VERBOSE,PSTR("(sim900_wait_data_on_usart) timed-out\r\n"),PGM_STRING);
	return 0;
}


static uint8_t sim900_read_string(char * const szBuf,uint8_t * const lenBuf)
{
	const uint8_t ec = *lenBuf;

	uint8_t l = 0;
	uint8_t r = 0;
	char c;

#ifndef SIM900_USART_POLLED
	usart_buffer_flush(&sim900_usart_data);
#endif

	while(1) {
		
		if(!sim900_wait_data_on_usart(10)) {
			debug_string(NORMAL,PSTR("(sim900_read_string) got timeout waiting for a character\r\n"),true);
			r=1;
			break;
		}
		
#ifdef SIM900_USART_POLLED
		c = usart_get(USART_GPRS);
#else
		c = USART_RX_CBuffer_GetByte(&sim900_usart_data);
#endif

		if(c=='\r') {
			if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'@');
		} else	if(c=='\n') {
			if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'#');
		} else break;
	}

	if(r==0) while(l<ec) {
		
		szBuf[l++] = c;

		if(!sim900_wait_data_on_usart(10)) {
			debug_string(NORMAL,PSTR("(sim900_read_string) got timeout waiting for a character\r\n"),true);
			r=1;
			break;
		}
		
#ifdef SIM900_USART_POLLED
		c = usart_get(USART_GPRS);
#else
		c = USART_RX_CBuffer_GetByte(&sim900_usart_data);
#endif

		if(c=='\r') {
			//if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'@');
			break;
		} else	if(c=='\n') {
			//if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'#');
			break;
		} 
		//if(g_log_verbosity > NORMAL) usart_putchar(USART_DEBUG,'.');

	}  

	if(ec==l) {
		r = 2;
		debug_string(NORMAL,PSTR("(sim900_read_string) Provided buffer is not big enough. Discarding chars\r\n"),true);
		l -= 1;
	}

	szBuf[l] = 0;

	//debug_string(VERBOSE,szBuf,RAM_STRING);
	//debug_string(VERBOSE,szCRLF,PGM_STRING);

	//debug_string(VERBOSE,PSTR("(sim900_read_string) out\r\n"),true);

	*lenBuf = l;
	return r;
}


static const char * sim900_wait4dictionary(const char * const dictionary[],uint8_t len)
{
	
	uint8_t num = len;
	const char * p[len];

#ifndef SIM900_USART_POLLED
	usart_buffer_flush(&sim900_usart_data);
#endif

	//debug_string(VERBOSE,PSTR("(sim900_wait4dictionary) IN\r\n"),true);


	while(num--) {
		p[num] = nvm_flash_read_word(dictionary+num);
	}

	while(1) {
		
		
		if(!sim900_wait_data_on_usart(20)) {
			debug_string(NORMAL,PSTR("(sim900_wait4dictionary) timeout waiting for sim900 response\r\n(sim900_wait4dictionary) OUT\r\n"),true);
			return NULL;
		}
		
#ifdef SIM900_USART_POLLED
		const char c1 = usart_get(USART_GPRS);
#else
		const char c1=USART_RX_CBuffer_GetByte(&sim900_usart_data);
#endif

		for(uint8_t i=0;i<len;++i) {

			const char c2 = nvm_flash_read_byte(p[i]);
			if(c1!=c2)
			{
				p[i]=(char *) nvm_flash_read_word(dictionary+i);
				continue;
			}

			p[i]++;

			if(nvm_flash_read_byte(p[i])==0)
			{
				const char * const r = nvm_flash_read_word(dictionary+i);
				//debug_string(VERY_VERBOSE,PSTR("(sim900_wait4dictionary) got: "),true);
				//debug_string(VERY_VERBOSE,r,true);
				//debug_string(VERY_VERBOSE,szCRLF,true);
				//debug_string(NORMAL,PSTR("(sim900_wait4dictionary) OUT\r\n"),true);
				return r;
			}
		}
	}
}