示例#1
0
void procRenard (void) {
	uint8_t rxByte;
	uint8_t channel = 0;
	
	// process bytes for each channel
	while (channel < NUM_CHANNELS) {
		usart_serial_getchar(USART_SERIAL, &rxByte);
		// map values for escaped bytes
		switch (rxByte) {
			case RENARD_PAD:
				break;
			case RENARD_ESCAPE:
				usart_serial_getchar(USART_SERIAL, &rxByte);
				switch (rxByte) {
					case RENARD_ESC_7D:
						compbuff[channel] = 0x7D;
						break;
					case RENARD_ESC_7E:
						compbuff[channel] = 0x7E;
						break;
					case RENARD_ESC_7F:
						compbuff[channel] = 0x7F;
						break;
				}
				channel++;
				break;
			default:
				compbuff[channel] = rxByte;
				channel++;
		}
	}
}
示例#2
0
/**
 * \brief Get a packet through XMODEM protocol
 *
 * \param usart  Base address of the USART instance.
 * \param p_data  Pointer to the data buffer.
 * \param uc_sno  Sequnce number.
 *
 * \return 0 for sucess and other value for XMODEM error
 */
static int32_t xmodem_get_packet(usart_if usart, int8_t *p_data,
		uint8_t uc_sno)
{
	uint8_t uc_cp_seq[2], uc_temp[2];
	uint16_t us_crc, us_xcrc;

	xmodem_get_bytes(usart, (int8_t *)uc_cp_seq, 2);

	us_xcrc = xmodem_get_bytes(usart, p_data, PKTLEN_128);

	/* An "endian independent way to combine the CRC bytes. */
	usart_serial_getchar(usart, &uc_temp[0]);
	usart_serial_getchar(usart, &uc_temp[1]);

	us_crc = uc_temp[0] << 8;
	us_crc += uc_temp[1];

	if ((us_crc != us_xcrc) || (uc_cp_seq[0] != uc_sno) ||
			(uc_cp_seq[1] != (uint8_t) ((~(uint32_t)uc_sno) & 0xff))) {
		usart_serial_putchar(usart, XMDM_CAN);
		return (-1);
	}

	return 0;
}
示例#3
0
/**
 * \brief Send the files through XMODEM protocol
 *
 * \param usart  Base address of the USART instance.
 * \param p_buffer  Pointer to send buffer
 * \param ul_length transfer file size
 */
void xmodem_send_file(usart_if usart, int8_t *p_buffer, uint32_t ul_length)
{
	uint8_t c_char, uc_sno = 1;
	int32_t l_done;
	uint32_t ul_timeout = (sysclk_get_peripheral_hz() / 10);

	if (ul_length & (PKTLEN_128-1)) {
		ul_length += PKTLEN_128;
		ul_length &= ~(PKTLEN_128-1);
	}

	/* Startup synchronization... */
	/* Wait to receive a NAK or 'C' from receiver. */
	l_done = 0;
	while(!l_done) {
		usart_serial_getchar(usart, &c_char);
		switch (c_char) {
		case XMDM_NAK:
			l_done = 1;
			break;
		case 'C':
			l_done = 1;
			break;
		case 'q':	/* ELS addition, not part of XMODEM spec. */
			return;
		default:
			break;
		}
	}

	l_done = 0;
	uc_sno = 1;
	while (!l_done) {
		c_char = xmodem_send_packet(usart, (uint8_t *)p_buffer, uc_sno);
		switch(c_char) {
		case XMDM_ACK:
			++uc_sno;
			ul_length -= PKTLEN_128;
			p_buffer += PKTLEN_128;
			break;
		case XMDM_NAK:
			break;
		case XMDM_CAN:
		case XMDM_EOT:
		default:
			l_done = -1;
			break;
		}
		if (!ul_length) {
			usart_serial_putchar(usart, XMDM_EOT);
			/* Flush the ACK */
			usart_serial_getchar(usart, &c_char);
			break;
		}
	}
}
示例#4
0
int main(void) {
	uint8_t rxByte;

	init();

	// main loop
	while (1) {
		// poll and retransmit while waiting for the sync byte
		while ((rxByte = usart_getchar(USART_SERIAL)) != RENARD_SYNC) {
			usart_serial_putchar(USART_SERIAL, rxByte);
		}

		// retransmit the sync byte
		usart_serial_putchar(USART_SERIAL, RENARD_SYNC);
			
		// evaluate command byte
		usart_serial_getchar(USART_SERIAL, &rxByte);
		if (rxByte == RENARD_ADDR) {			// process renard packet for this device
			procRenard();
		} else if (rxByte > RENARD_ADDR) {	// decrement command/address byte and transmit
			rxByte--;
			usart_serial_putchar(USART_SERIAL, rxByte);
		} else {								// unsupported / reserved.  retransmit
			usart_serial_putchar(USART_SERIAL, rxByte);
		}
	}
}
示例#5
0
/**
 * \brief Get input from user, and the biggest 4-digit decimal number is allowed.
 *
 * \param ul_lower_limit The lower limit of input
 * \param ul_upper_limit The upper limit of input
 */
static uint32_t get_input_value(uint32_t ul_lower_limit,
	uint32_t ul_upper_limit)
{
	uint32_t i = 0, length = 0, value = 0;
	uint8_t uc_key, str_temp[5] = { 0 };

	while (1) {
        usart_serial_getchar((Usart *)CONSOLE_UART, &uc_key);

		if (uc_key == '\n' || uc_key == '\r') {
			puts("\r");
			break;
		}

		if ('0' <= uc_key && '9' >= uc_key) {
			printf("%c", uc_key);
			str_temp[i++] = uc_key;

			if (i >= 4) {
				break;
			}
		}
	}

	str_temp[i] = '\0';
	/* Input string length */
	length = i;
	value = 0;

	/* Convert string to integer */
	for (i = 0; i < 4; i++) {
		if (str_temp[i] != '0') {
			switch (length - i - 1) {
			case 0:
				value += (str_temp[i] - '0');
				break;

			case 1:
				value += (str_temp[i] - '0') * 10;
				break;

			case 2:
				value += (str_temp[i] - '0') * 100;
				break;

			case 3:
				value += (str_temp[i] - '0') * 1000;
				break;
			}
		}
	}

	if (value > ul_upper_limit || value < ul_lower_limit) {
		puts("\n\r-F- Input value is invalid!");
		return VAL_INVALID;
	}

	return value;
}
示例#6
0
/**
 * \brief Receive a sequence of bytes from USART device
 *
 * \param usart  Base address of the USART instance.
 * \param data   Data buffer to write
 * \param len    Length of data
 *
 */
status_code_t usart_serial_read_packet(usart_if usart, uint8_t *data,
                                       size_t len)
{
    while (len) {
        usart_serial_getchar(usart, data);
        len--;
        data++;
    }
    return STATUS_OK;
}
示例#7
0
/**
 * \brief Receive the files through XMODEM protocol
 *
 * \param usart  Base address of the USART instance.
 * \param p_buffer  Pointer to receive buffer
 *
 * \return received file size
 */
uint32_t xmodem_receive_file(usart_if usart, int8_t *p_buffer)
{
	uint32_t ul_timeout;
	uint8_t c_char;
	int32_t l_done;
	uint8_t uc_sno = 0x01;
	uint32_t ul_size = 0;

	/* Wait and put 'C' till start XMODEM transfer */
	while (1) {
		usart_serial_putchar(usart, 'C');
		ul_timeout = (sysclk_get_peripheral_hz() / 10);
		while (!(usart_serial_is_rx_ready(usart)) && ul_timeout) {
			ul_timeout--;
		}
		if (usart_serial_is_rx_ready(usart)) {
			break;
		}
	}

	/* Begin to receive the data */
	l_done = 0;
	while (l_done == 0) {
		usart_serial_getchar(usart, &c_char);

		switch (c_char) {
		/* Start of transfer */
		case XMDM_SOH:
			l_done = xmodem_get_packet(usart, p_buffer+ul_size, uc_sno);
			if (l_done == 0) {
				uc_sno++;
				ul_size += PKTLEN_128;
			}
			usart_serial_putchar(usart, XMDM_ACK);
			break;

		/* End of transfer */
		case XMDM_EOT:
			usart_serial_putchar(usart, XMDM_ACK);
			l_done = ul_size;
			break;

		case XMDM_CAN:
		case XMDM_ESC:
		default:
			l_done = -1;
			break;
		}
	}
	return ul_size;
}
示例#8
0
/**
 * \brief Get bytes through XMODEM protocol.
 *
 * \param usart  Base address of the USART instance.
 * \param p_data  Pointer to the data buffer.
 * \param ul_length Length of data expected.
 *
 * \return Calculated CRC value
 */
static uint16_t xmodem_get_bytes(usart_if usart, int8_t *p_data,
		uint32_t ul_length)
{
	uint16_t us_crc = 0;
	uint32_t i, j;
	uint8_t c_char;

	for (i = 0; i < ul_length; ++i) {
		usart_serial_getchar(usart, &c_char);
		us_crc = us_crc ^ (int32_t) c_char << 8;
		for (j = 0; j < 8; j++) {
			if (us_crc & 0x8000) {
				us_crc = us_crc << 1 ^ CRC16POLY;
			} else {
				us_crc = us_crc << 1;
			}
		}
		us_crc = us_crc & 0xFFFF;
		*p_data++ = c_char;
	}
	return us_crc;
}
示例#9
0
/**
 * \brief Send a packet through XMODEM protocol
 *
 * \param usart  Base address of the USART instance.
 * \param p_data  Pointer to the data buffer.
 * \param uc_sno  Sequnce number.
 *
 * \return 0 for sucess and other value for XMODEM error
 */
static uint8_t xmodem_send_packet(usart_if usart, uint8_t *p_data,
		uint8_t uc_sno)
{
	uint32_t  i, j;
	uint16_t us_check_sum;
	int8_t	 c_data;
	uint8_t	 uc_ack;

	us_check_sum = 0;

	usart_serial_putchar(usart, XMDM_SOH);
	usart_serial_putchar(usart, uc_sno);
	usart_serial_putchar(usart, ((uint8_t)(~(uc_sno))));

	for(i = 0; i < PKTLEN_128; i++) {
		c_data = *p_data++;
		usart_serial_putchar(usart, c_data);

		us_check_sum = us_check_sum ^ (int32_t) c_data << 8;
		for (j = 0; j < 8; j++) {
			if (us_check_sum & 0x8000) {
				us_check_sum = us_check_sum << 1 ^ CRC16POLY;
			} else {
				us_check_sum = us_check_sum << 1;
			}
		}
		us_check_sum = us_check_sum & 0xFFFF;
	}

	/* An "endian independent way to extract the CRC bytes. */
	usart_serial_putchar(usart, (uint8_t)(us_check_sum >> 8));
	usart_serial_putchar(usart, (uint8_t)us_check_sum);

	usart_serial_getchar(usart, &uc_ack);

	return uc_ack;	/* Wait for ack */
}
示例#10
0
signed int    Get_PoorSignal_HeartRate(void)
{
	if(Packet_Header == usart_serial_getchar(&ext3_uart_module,tmp_data))  //0xAA
	{
		if(Packet_Header == usart_serial_getchar(&ext3_uart_module,tmp_data))//0xAA
		{
			vBMD101.Rpayload_Length = usart_serial_getchar(&ext3_uart_module,tmp_data);
			if(vBMD101.Rpayload_Length == Payload_Length )
			{
				generatedCheckSum=0;
				for( kk= 0; kk< vBMD101.Rpayload_Length; kk++)
				{
					vBMD101.PayloadData[kk] = usart_serial_getchar(&ext3_uart_module,tmp_data);
					generatedCheckSum += vBMD101.PayloadData[kk];
				}
				vBMD101.PacketCheckSum = usart_serial_getchar(&ext3_uart_module,tmp_data);
				generatedCheckSum &= 0xFF;
				generatedCheckSum = ~generatedCheckSum & 0xFF;
				if(vBMD101.PacketCheckSum == generatedCheckSum)
				{
					//printf("No touch the sensor and the value is short\n");
					vBMD101.PacketCheckSum=0;
					generatedCheckSum=0;
					vBMD101.HighData= vBMD101.PayloadData[2] & 0xFFFF;
					vBMD101.LowData= vBMD101.PayloadData[3] & 0xFFFF;
					temp = (vBMD101.HighData)<<8;
					vBMD101.RawData = (temp|vBMD101.LowData);
					if(vBMD101.RawData > 32768)
					{
						vBMD101.RawData -= 65536;
					}
					if(vBMD101.PoorSignal_Value == 0xC8)
					{
						/*nsk_ecg_update(vBMD101.RawData); // keep updating for every incoming raw ecg sample.
						
						if (nsk_ecg_is_new_beat()) { // usually we only query algorithm outputs when a new beat is detected.
							ssq = nsk_ecg_get_signal_quality(); // Query the short term Signal Quality.
							rhr1 = nsk_ecg_get_hr_robust(); // Query the Robust Heart Rate.
							rhr = nsk_ecg_compute_hr();
							sprintf((char*)Hr,"HR: %2d bmp ",rhr);
							sprintf((char*)Hr1,"RHR: %2d bmp ",rhr1);
							sprintf((char*)ssqBuffer,"SQ: %2d ",ssq);
							lsq = nsk_ecg_get_overall_signal_quality(); // Query the long term Signal Quality.
							rr_Interval = nsk_ecg_get_rri_count(); 	//Interval  count>30,not to zero????
							if (rr_Interval >= NEL_EXN) { // only query ext algorithms when enough RRI is ready.
								hrv = nsk_ecg_compute_hrv(); // only if HRV is needed.
								mood = nsk_ecg_compute_mood(); // only if Mood is needed.
								resp_rate = nsk_ecg_compute_respiratory_rate(); // only if Respiratory Rate is needed.
								rri = nsk_ecg_get_rri();
								sprintf((char*)hrvBuffer,"HRV: %2d",hrv);
								sprintf((char*)moodBuffer,"Mood: %2d",mood);
								sprintf((char*)resp_rateBuffer,"Resp: %2d ",resp_rate);
								sprintf((char*)rriBuffer,"RRI: %2d ms ",rri);
								sprintf((char*)lsqBuffer,"LSQ: %2d ms ",lsq);
							}
						}
						*/
						//printf("\nRawData is %d\n",vBMD101.RawData);
						return vBMD101.RawData;
					}
					else
					{
						return 0;
					}
				}
			}
			else if (vBMD101.Rpayload_Length == 0x12 )
			{
				generatedCheckSum=0;
				for( jj= 0; jj< vBMD101.Rpayload_Length; jj++)
				{
					vBMD101.PayloadData[jj] = usart_serial_getchar(&ext3_uart_module,tmp_data);
					generatedCheckSum += vBMD101.PayloadData[jj];
				}
				vBMD101.PacketCheckSum = usart_serial_getchar(&ext3_uart_module,tmp_data);
				generatedCheckSum &= 0xFF;
				generatedCheckSum = ~generatedCheckSum & 0xFF;
				if(vBMD101.PacketCheckSum == generatedCheckSum)
				{
					vBMD101.PacketCheckSum=0;
					generatedCheckSum=0;
					for(ii=0; ii<vBMD101.Rpayload_Length;ii++)
					{
						switch(ii)
						{
							case 0:
							vBMD101.PoorSignal_Tag = vBMD101.PayloadData[0];
							break;
							case 1:
							vBMD101.PoorSignal_Value = vBMD101.PayloadData[1];
							//printf("PoorSignal_Value is %d",vBMD101.PoorSignal_Value);
							break;
							case 2:
							vBMD101.HeartRate_Tag = vBMD101.PayloadData[2];
							break;
							case 3:
							vBMD101.HeartRate_Value = vBMD101.PayloadData[3];
							break;
							default:
							break;
						}
					}
				}
			}
		}
	}
	return 0;
}
示例#11
0
/**
 *  \brief DAC Sinewave application entry point.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_key;
	uint32_t ul_freq, ul_amp;

	/* Initialize the system */
	sysclk_init();
	board_init();

	/* Initialize debug console */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Enable clock for DACC */
#if SAM4L
	sysclk_enable_peripheral_clock(DACC_BASE);
#else
	sysclk_enable_peripheral_clock(DACC_ID);
#endif

	/* Reset DACC registers */
	dacc_reset(DACC_BASE);

	/* Half word transfer mode */
	dacc_set_transfer_mode(DACC_BASE, 0);

	/* Initialize timing, amplitude and frequency */
#if (SAM3N) || (SAM4L)
	/* Timing:
	 * startup                - 0x10 (17 clocks)
	 * internal trigger clock - 0x60 (96 clocks)
	 */
	dacc_set_timing(DACC_BASE, 0x10, 0x60);

	/* Enable DAC */
	dacc_enable(DACC_BASE);
#else
	/* Power save:
	 * sleep mode  - 0 (disabled)
	 * fast wakeup - 0 (disabled)
	 */
	dacc_set_power_save(DACC_BASE, 0, 0);
	/* Timing:
	 * refresh        - 0x08 (1024*8 dacc clocks)
	 * max speed mode -    0 (disabled)
	 * startup time   - 0x10 (1024 dacc clocks)
	 */
	dacc_set_timing(DACC_BASE, 0x08, 0, 0x10);

	/* Disable TAG and select output channel DACC_CHANNEL */
	dacc_set_channel_selection(DACC_BASE, DACC_CHANNEL);

	/* Enable output channel DACC_CHANNEL */
	dacc_enable_channel(DACC_BASE, DACC_CHANNEL);

	/* Set up analog current */
	dacc_set_analog_control(DACC_BASE, DACC_ANALOG_CONTROL);
#endif /* (SAM3N) */

	g_l_amplitude = MAX_AMPLITUDE / 2;
	g_ul_frequency = DEFAULT_FREQUENCY;

	SysTick_Config(sysclk_get_cpu_hz() / (g_ul_frequency * SAMPLES));

	/* Main menu */
	display_menu();

	while (1) {
		usart_serial_getchar((Usart *)CONSOLE_UART, &uc_key);

		switch (uc_key) {
		case '0':
			puts("Frequency:");
			ul_freq = get_input_value(MIN_FREQUENCY, MAX_FREQUENCY);
			puts("\r");

			if (ul_freq != VAL_INVALID) {
				printf("Set frequency to : %luHz\n\r", (unsigned long)ul_freq);
				SysTick_Config(sysclk_get_cpu_hz() / (ul_freq * SAMPLES));
				g_ul_frequency = ul_freq;
			}
			break;

		case '1':
			puts("Amplitude:");
			ul_amp = get_input_value(MIN_AMPLITUDE, MAX_AMPLITUDE);
			puts("\r");
			if (ul_amp != VAL_INVALID) {
				printf("Set amplitude to : %lu\n\r", (unsigned long)ul_amp);
				g_l_amplitude = ul_amp;
			}
			break;

		case 'i':
		case 'I':
			printf("-I- Frequency : %lu Hz Amplitude : %ld\n\r",
				(unsigned long)g_ul_frequency, (long)g_l_amplitude);
			break;

		case 'w':
		case 'W':
			printf("-I- Switch wave to : %s\n\r", g_uc_wave_sel ?
				"SINE" : "Full Amplitude SQUARE");
			g_uc_wave_sel = (g_uc_wave_sel + 1) & 1;
			break;

		case 'm':
		case 'M':
			display_menu();
			break;
		}
		puts("Press \'m\' or \'M\' to display the main menu again!\r");
	}
}
示例#12
0
int main(void)
{
	uint8_t uc_key;

	/* Initialize the system */
	sysclk_init();
	board_init();

	/* Configure LED 1 */
	pmc_enable_periph_clk(PIN_LED_BLUE_ID);
	pio_set_output(PIN_LED_BLUE_PIO , PIN_LED_BLUE_MASK, 1, 0, 0);

	/* Initialize debug console */
	config_uart();
	
	/* frase de boas vindas */
	puts(" ---------------------------- \n\r"
	 	 " Bem vindo terraquio !		\n\r"
		 " ---------------------------- \n\r");
	
	/* Enable peripheral clock */
	pmc_enable_periph_clk(ID_SMC);

	/** Configura o LEDs */
	configure_leds();

	/** Configura o timer */
	configure_tc();


	/* Configuração LCD */
	configure_lcd();
	
	desenhaCabecalho();
	
	/* display main menu */
	display_menu();

	while (1) {
		
		
		usart_serial_getchar((Usart *)CONSOLE_UART, &uc_key);	
		switch (uc_key) {
			case '1':
				display_menu();
				break;
			case '2':
				flagLED = 0;
				pio_clear(PIN_LED_BLUE_PIO, PIN_LED_BLUE_MASK);
				puts("Led ON \n\r");
				break;
			case '3' :
				flagLED = 1;
				pio_set(PIN_LED_BLUE_PIO, PIN_LED_BLUE_MASK);
				puts("Led OFF \n\r");
				break;
			case '4' :
				flagLED = 2;
				pio_set(PIN_LED_BLUE_PIO, PIN_LED_BLUE_MASK);
				puts("Led OFF \n\r");
				break;
			case '5' :
				flagLED = 3;
				pio_set(PIN_LED_BLUE_PIO, PIN_LED_BLUE_MASK);
				puts("Defina a o valor da Frequência (0-65356) \n\r");
				usart_serial_getchar((Usart *)CONSOLE_UART, &uc_key);
				//tc_write_rc(uc_key);
				break;
				
			default:
				printf("Opcao nao definida: %d \n\r", uc_key);
		}	
	}
}
示例#13
0
int main(void)
{
	uint8_t uc_key;

	/* Initialize the system */
	sysclk_init();
	board_init();

	/* Configure LED 1 */
	pmc_enable_periph_clk(ID_LED_BLUE);
	pio_set_output(PORT_LED_BLUE  , MASK_LED_BLUE	,1,0,0);

	/* Initialize debug console */
	config_uart();
	
	/* frase de boas vindas */
	puts(" ---------------------------- \n\r"
	 	 " Bem vindo Corsi  !		\n\r"
		 " ---------------------------- \n\r");
		 
	/* display main menu */
	display_menu();
	configure_tc();
	TC0_Handler();
	
	pio_clear(PORT_LED_RED, MASK_LED_RED);
	pio_set(PORT_LED_GREEN, MASK_LED_GREEN);
	
	PIOA->PIO_PER = (1 << PIN_LED_BLUE );
	PIOA->PIO_OER |=  (1 << PIN_LED_BLUE );
	
	
	PMC->PMC_PCER0 |= ID_PIOC;
	PIOC->PIO_PER |= (1 << PIN_LED_RED );
	PIOC->PIO_OER |=  (1 << PIN_LED_RED );
	
	
	tc_stop(TC0, 0);
	PIOA->PIO_SODR = (1 << PIN_LED_BLUE );
	PIOC->PIO_CODR = (1 << PIN_LED_RED );
	
	while (1) {
		usart_serial_getchar((Usart *)CONSOLE_UART, &uc_key);	
		switch (uc_key) {
			case '1':
				display_menu();
				break;
			case '2':
				tc_start(TC0,0);
				PIOA->PIO_CODR = (1 << PIN_LED_BLUE );
				
				puts("Led BLUE ON \n\r");
				break;
			case '3' :
				tc_stop(TC0, 0);
				PIOA->PIO_SODR = (1 << PIN_LED_BLUE );
				puts("Led BLUE OFF \n\r");
			
				break;
			case '4':
				tc_start(TC0,0);
				pio_clear(PORT_LED_GREEN, MASK_LED_GREEN);
				puts("Led GREEN ON \n\r");
				break;
			case '5' :
				tc_stop(TC0, 0);

				pio_set(PORT_LED_GREEN, MASK_LED_GREEN);
				puts("Led GREEN OFF \n\r");
				break;
			case '6':
					tc_start(TC0,0);
					PIOC->PIO_SODR = (1 << PIN_LED_RED );
				
				puts("Led RED ON \n\r");
			break;
			case '7' :
				tc_stop(TC0, 0);
				PIOC->PIO_CODR = (1 << PIN_LED_RED );
				puts("Led RED OFF \n\r");
			break;
			case '8' :
				
				tc_write_rc(TC0, 0, tc_read_rc(TC0,0) * 0.5);
				puts("aumentando \n\r");
				break;
			case '9' :
				
				tc_write_rc(TC0, 0, tc_read_rc(TC0,0) * 1.4);
				puts("diminuindo \n\r");
				break;				
			default:
				printf("Opcao nao definida: %d \n\r", uc_key);
		}	
	}
}
示例#14
0
文件: wifi.c 项目: jdrunner/smartee
int wifi_init(void){
  uint32_t BUF_SIZE = MD_BUF_SIZE;
  uint8_t tmp;  //dummy var for flushing UART
  int r; //return value from wifi_send_cmd (length of resp)
  char *buf;
  char *tx_buf;

  //allocate static buffers if they are not NULL
  if(resp_buf==NULL){
    resp_buf=core_malloc(RESP_BUF_SIZE);
  }
  if(resp_complete_buf==NULL){
    resp_complete_buf=core_malloc(RESP_COMPLETE_BUF_SIZE);
  }
  if(wifi_rx_buf==NULL){
    wifi_rx_buf = core_malloc(WIFI_RX_BUF_SIZE);
  }
  //if we are standalone, don't do anything
  if(wemo_config.standalone){
    printf("warning: wifi_init called in standalone mode\n");
    return 0; 
  }
  //initialize the memory
  buf = core_malloc(BUF_SIZE);
  tx_buf = core_malloc(BUF_SIZE);

  //set up the UART
  static usart_serial_options_t usart_options = {
    .baudrate = WIFI_UART_BAUDRATE,
    .charlength = WIFI_UART_CHAR_LENGTH,
    .paritytype = WIFI_UART_PARITY,
    .stopbits = WIFI_UART_STOP_BITS
  };
  gpio_configure_pin(PIO_PA9_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  gpio_configure_pin(PIO_PA10_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  pmc_enable_periph_clk(ID_WIFI_UART);
  sysclk_enable_peripheral_clock(ID_WIFI_UART);
  usart_serial_init(WIFI_UART,&usart_options);
  //flush any existing data
  while(usart_serial_is_rx_ready(WIFI_UART)){
    usart_serial_getchar(WIFI_UART,&tmp);
  }
  // Trigger from timer 0
  pmc_enable_periph_clk(ID_TC0);
  tc_init(TC0, 0,                        // channel 0
	  TC_CMR_TCCLKS_TIMER_CLOCK5     // source clock (CLOCK5 = Slow Clock)
	  | TC_CMR_CPCTRG                // up mode with automatic reset on RC match
	  | TC_CMR_WAVE                  // waveform mode
	  | TC_CMR_ACPA_CLEAR            // RA compare effect: clear
	  | TC_CMR_ACPC_SET              // RC compare effect: set
	  );
  TC0->TC_CHANNEL[0].TC_RA = 0;  // doesn't matter
  TC0->TC_CHANNEL[0].TC_RC = 64000;  // sets frequency: 32kHz/32000 = 1 Hz
  NVIC_ClearPendingIRQ(TC0_IRQn);
  NVIC_SetPriority(TC0_IRQn,1);//high priority
  NVIC_EnableIRQ(TC0_IRQn);
  tc_enable_interrupt(TC0, 0, TC_IER_CPCS);
  //reset the module
  if(wifi_send_cmd("AT+RST","ready",buf,BUF_SIZE,8)==0){
    printf("Error reseting ESP8266\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //set to mode STA
  if(wifi_send_cmd("AT+CWMODE=1","OK",buf,BUF_SIZE,8)==0){
    printf("Error setting ESP8266 mode\n");
    core_free(buf);
    core_free(tx_buf);
    return 0;
  }
  //try to join the specified network  
  snprintf(tx_buf,BUF_SIZE,"AT+CWJAP=\"%s\",\"%s\"",
	   wemo_config.wifi_ssid,wemo_config.wifi_pwd);
  if((r=wifi_send_cmd(tx_buf,"OK",buf,BUF_SIZE,10))==0){
    printf("no response to CWJAP\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //check for errors
  if( (r<1) || (strcmp(&buf[r-1],"OK")!=0)){
    snprintf(tx_buf,BUF_SIZE,"failed to join network [%s]: [%s]\n",wemo_config.wifi_ssid, buf);
    printf(tx_buf);
    core_log(tx_buf);
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //see if we have an IP address
  wifi_send_cmd("AT+CIFSR","OK",buf,BUF_SIZE,5);
  if(strstr(buf,"ERROR")==buf){
    printf("error getting IP address\n");
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //try to parse the response into an IP address
  //expect 4 octets but *not* 0.0.0.0
  int a1,a2,a3,a4;
  if(!(sscanf(buf,"+CIFSR:STAIP,\"%d.%d.%d.%d\"",&a1,&a2,&a3,&a4)==4 && a1!=0)){
    printf("error, bad address: %s\n",buf);
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //save the IP to our config
  snprintf(buf,BUF_SIZE,"%d.%d.%d.%d",a1,a2,a3,a4);
  memset(wemo_config.ip_addr,0x0,MAX_CONFIG_LEN);
  strcpy(wemo_config.ip_addr,buf);
  //set the mode to multiple connection
  wifi_send_cmd("AT+CIPMUX=1","OK",buf,BUF_SIZE,2);
  //start a server on port 1336
  wifi_send_cmd("AT+CIPSERVER=1,1336","OK",buf,BUF_SIZE,2);

  //if we know the NILM IP address, send it our IP
  if(strlen(wemo_config.nilm_ip_addr)!=0){
    if(wifi_send_ip()==TX_ERR_MODULE_RESET){
      return TX_ERR_MODULE_RESET;
    }
  } 
  else {
     //get the NILM IP address from the manager
     //once we know the NILM address we send it ours
     core_get_nilm_ip_addr();
  }

  //log the event
  snprintf(buf,BUF_SIZE,"Joined [%s] with IP [%s]",
	   wemo_config.wifi_ssid,wemo_config.ip_addr);
  printf("\n%s\n",buf);
  core_log(buf);
  //free the memory
  core_free(tx_buf);
  core_free(buf);
  return 0;
}
示例#15
0
文件: debug_usart.c 项目: marekr/asf
/**
 * Get a byte from console
 * \return byte read
 */
uint8_t dbg_usart_getchar(void)
{
	uint8_t byte;
	usart_serial_getchar(DBG_USART_BASE, &byte);
	return byte;
}