Exemple #1
0
void uart_process() {
	if (uart_has_data()) {
		uint8_t command = uart_consume();
		// Process factory reset commands:
		// UTOK_RST_PROPOSE, UTOK_RST_CONFIRM, UTOK_RST_COMMIT
		if (command == UTOK_RST_PROPOSE) {
			uint8_t i;
			for (i = 0; i < LED_COUNT; i++) leds_set_led(i,LED_SLOW_1);
			wait_for_confirm();
			uart_send_byte(UTOK_RST_CONFIRM);
			for (i = 0; i < LED_COUNT; i++) leds_set_led(i,(i%2 == 0)?LED_FAST_1:LED_FAST_0);
			otp_factory_reset();
			for (i = 0; i < LED_COUNT; i++) leds_set_led(i,LED_OFF);
			while(1){} // Loop forever
		} else if (command == UTOK_BUTTON_QUERY) {
			uart_send_byte(UTOK_BUTTON_RSP);
			uart_send_byte(has_confirm()?0xff:0x00);
		} else if (command == UTOK_REQ_CHKSM) {
			uint16_t block;
			struct checksum_ret sum;
			block = uart_consume() << 8;
			block |= uart_consume();
			sum = nand_block_checksum(block);
			if (sum.ok) {
				uart_send_byte(UTOK_RSP_CHKSM);
				uart_send_byte(sum.checksum >> 8);
				uart_send_byte(sum.checksum & 0xff);
			} else {
Exemple #2
0
//Converts Pulse Width's into State Machine Changes
//0x2000 = As.wav
//0x4000 = Bs.wav
//0x7000 = Cs.wav
//0x9000 = Ds.wav
void pwm_decode(uint16_t width) {

    uart_send_byte(&udata,'-');
    uart_send_byte(&udata,'-');
    uart_send_byte(&udata,'-');
    uart_send_HEX16(&udata, width);
    uart_send_byte(&udata,'-');
    uart_send_byte(&udata, 13);

    if (between(width,0x1000,0x2000) == true) {
        //Found A code!
        pwm_state('A');
    }
    if (between(width,0x2000,0x3000) == true) {
        //Found B code!
        pwm_state('B');
    }
    if (between(width,0x4000,0x5000) == true) {
        //Found C code!
        pwm_state('C');
    }
    if (between(width,0x7000,0x8000) == true) {
        //Found D code!
        pwm_state('D');
    }
}
Exemple #3
0
static int uart_push(void *user_data, unsigned char ch)
{
    (void)user_data;

    uart_send_byte(DEBUG_UART_PORT, ch);
    if (ch == '\n')
        uart_send_byte(DEBUG_UART_PORT, '\r');
    return 1;
}
Exemple #4
0
void printf_byte(int a){		  // print a Byte in hex format
	char b;
	b=((0xF0&a)>>4);
	b+= (b<10)?48:55;
	uart_send_byte(b);
	b=(0xF&a);
	b+= (b<10)?48:55;
	uart_send_byte(b);
}
Exemple #5
0
void uart_send_string(char *sendString)
{
	int length = 0;
	while ((sendString[length] != '\0') && (length < 100))
	{
		uart_send_byte(NEW_ULTRASOUND3_BASE, sendString[length]);
		length++;
	}
	uart_send_byte(NEW_ULTRASOUND3_BASE, 0x00);
}
Exemple #6
0
void printf_byte(int a){		  // print a Byte in hex format
	#ifndef UART_ENABLED
		return ;
#else

	char b;
	b=((0xF0&a)>>4);
	b+= (b<10)?48:55;
	uart_send_byte(b);
	b=(0xF&a);
	b+= (b<10)?48:55;
	uart_send_byte(b);
		#endif
}
return_type_t uart_send_array(const uint8_t *send_array, uint8_t size)
{
	uint8_t i;
	for(i = 0; i < size; i++)
		uart_send_byte(send_array[i]);
	return RC_SUCCESS;
}
Exemple #8
0
void printf_string(char * str)
{
	while(*str!=0)
	{	      // while not reach the last string character
		uart_send_byte(*str); // send next string character
		str++;
	}
}
/*
	Send/print character/string over UART. 
	Printable data for viewing on terminal.
	Call this function:		printString("test data");
	Data must be string.
*/
void printString(char *data){
	uint16_t i;
	uint16_t str_length = strlen(data);
	for(i = 0; i < str_length; i++)
	{
		uart_send_byte(data[i]);
	}
}
Exemple #10
0
bool
ComSlip_SendMessage(uint8_t* msg, uint16_t msgLength)
{
    // send start of SLIP message
	uart_send_byte(lora, SLIP_END);

    // iterate over all message bytes
    while(msgLength--)
    {
        switch (*msg)
        {
            case SLIP_END:
            	uart_send_byte(lora, SLIP_ESC);
            	uart_send_byte(lora, SLIP_ESC_END);
                break;

            case SLIP_ESC:
            	uart_send_byte(lora, SLIP_ESC);
            	uart_send_byte(lora, SLIP_ESC_ESC);
                break;

            default:
            	uart_send_byte(lora,*msg);
                break;
        }
        // next byte
        msg++;
    }

    // send end of SLIP message
    uart_send_byte(lora, SLIP_END);

    // always ok
    return true;
}
Exemple #11
0
int sys_dev_init()
{
	char count = '0';
	init_func_t *init_call;
	extern init_func_t init_call_begin[], init_call_end[];

	uart_send_byte('B');
	uart_send_byte('\n');

	init_call = init_call_begin;
	while (init_call < init_call_end) {
		uart_send_byte(count++);
		uart_send_byte(':');

		(*init_call)();
		init_call++;

		uart_send_byte('\n');
	}

	uart_send_byte('E');
	uart_send_byte('\n');

	return 0;
}
Exemple #12
0
/****************************************************************************
  Function:
    unsigned int ultrasound_read(long Address, unsigned char settings)

  Description:
   // This function receives one Byte via the corresponding UART from the Address

  Precondition:
    Call ultrasound_init(long Address) prior to use this function

  Parameters:
    long Address 			- Address of the UART (and therefore of the Ultrasound)
	unsigned char settings 	- defines maximum distance to measure (1: 11m; 0: 5m)

  Returns:
    unsigned int - distance in mm

  Remarks:
    Distance in meter
  ***************************************************************************/
unsigned int ultrasound_read(long Address, unsigned char settings) 
{
	unsigned int i;
	unsigned char res_l, res_h;
	uart_send_byte(Address, 0xe8);
	for (i = 0; i < 300; i++);
	uart_send_byte(Address, 0x02);
	for (i = 0; i < 300; i++);
	if (settings)
		uart_send_byte(Address, 0xb8); 
	else
		uart_send_byte(Address, 0xb0); 
	for (i = 0; i < 300; i++);
	res_h = uart_receive_byte(Address);
	for (i = 0; i < 300; i++);
	res_l = uart_receive_byte(Address);
	return (((unsigned int) res_h) << 8) + res_l; 
}
Exemple #13
0
bool uart_send_buf (unsigned dev, char *buf, unsigned len)
{
    unsigned index=0;
    while (index<len)
    {
        uart_send_byte (dev, buf[index]);
        index++;
    }
    return true;
}
Exemple #14
0
int main (void)
{
	// Interrupt setup for sleep mode
	interrupt_init();
	
	// temp for debugging
	DDRB |= (1 << PINB0);
	PORTB |= (1 << PINB0); 

	// Select ADC channel
	uint8_t ch = 0b00000000;
	
	while(1)
	{
		if (asleep) 
		{
			PORTB &= ~(1 << PINB0);
			// Disable interrupt
			cli();
			// Power down
			sleep_now();
			PORTB ^= (1 << PINB0);
			// Continue here at start up
			// Disable interrupt for setting registers
			cli();
			external_clock_init();
			interrupt_init();
			timer_init();
			uart_init();
			adc_init();
			sei();
		} else if (!asleep) {
			// Temp for debugging
			_delay_ms(100);
			// PORTB ^= (1 << PINB0);
			// Read from ADC
			uint16_t result = adc_read(ch);
			// Send result
			uart_send_byte((result >> 8));
			uart_send_byte(result);
			uart_send_byte('\n');
		}
	}
Exemple #15
0
void printf_string(char * str){
	#ifndef UART_ENABLED
		return ;
#else

	while(*str!=0){	      // while not reach the last string character
		uart_send_byte(*str); // send next string character
		str++;
	}
	#endif
}
Exemple #16
0
/*
	Send raw data over UART. 
	Not "printable" data.
	Call this function:		writeData(&data, sizeof(data));
	Data can be any type.
*/
void writeData(void *data, uint8_t dataSize){
  uint8_t i, d;
  
  d = dataSize/2;
 
  for(i = 0; i < d; i++)
  {
    uart_send_byte(*( ((uint16_t *)data) + i ) );
  }
 
}
Exemple #17
0
static int my_itoa(__u32 num)
{
	int i, ret;
	char char_buff[32] = {0};

	i = 0;
	while (num) {
		char_buff[i++] = (num % 10) + '0';
		num /= 10;
	}

	ret = i--;
	do {
		uart_send_byte(char_buff[i]);
	} while (i--);

	uart_send_byte('\n');

	return ret;
}
Exemple #18
0
int main(void)
{
	uint8_t data;
	lcd_init();
	uart_init(38400);
	for (;;) {
		data = uart_recv_byte();
		lcd_put_char(data);
		uart_send_byte(data);
	}
}
Exemple #19
0
/**
 * Play one round of the contention game. If neither side received the force_master flag,
 * they each wait a random amount of time before sending the ping packet. The first to acknowledge
 * the other side's ping is the slave. If the two packets conflict, the came ends in a conflict and
 * must be run again.
 * If after a timeout passes no token has been received, the device assumes that the pad has
 * been snapped and is disconnected from its twin.
 * @param usb_present gives the usb-connected side precedence if force_master is not set.
 */
ConnectionState uart_play_round(bool force_master, bool usb_present) {
	timer_reset();
	uint16_t ms = 0;
	while (timer_msec() < 1) {} // let uarts settle
	if (!force_master) {
		hwrng_start();
		timer_reset(); while (timer_msec() < 2) {} // wait 2 ms
		while (!hwrng_done()) {} // wait for random data to become available
		if (!usb_present) {
			ms = 3+(hwrng_bits()[0] & 0x3f); // additional delay of 3-67 ms
		} else {
			ms = 0; // skip additional delay on usb-connected end
		}
	}
	timer_reset();
	while (timer_msec() < ms) {
		if (uart_has_data()) {
			uint8_t b = uart_consume();
			if (b == UTOK_GAME_PING) {
				uart_send_byte(UTOK_GAME_ACK);
				return CS_TWINNED_SLAVE;
			} else if (b == UTOK_GAME_ACK) {
				return CS_TWINNED_COLLISION;
			}
		}
	}
	uart_send_byte(UTOK_GAME_PING);
	timer_reset();
	ms = 1000 - ms;
	while (timer_msec() < ms) {
		if (uart_has_data()) {
			uint8_t b = uart_consume();
			if (b == UTOK_GAME_ACK) {
				return CS_TWINNED_MASTER;
			} else if (b == UTOK_GAME_PING) {
				return CS_TWINNED_COLLISION;
			}
		}
	}
	return CS_SINGLE;
}
Exemple #20
0
int main()
{
	init();

	print("Welcome to MSP430 Launchpad.\r\n");

	while (1) {
		char c = uart_recv_byte();
		uart_send_byte(c);
	}
	return 0;
}
Exemple #21
0
void handle_input(uint8 type) 
{
	/* check click times */	
	if (ClickNum > MAX_INPUT) {
		if (type != LETTER || type != WORD)
			reset_buff();
		beer(WARN);
		return;
	}

	if (type == DOT || type ==DASH) {
		beer(type);

		/* the ascii of '\0' is equal to number zero 
	   	   it may cause some trouble */
		buff[ClickNum++] = type + 1; 

		return;
	}

	if (type == LETTER || type == WORD) {
		buff[ClickNum] = '\0';

		/* decoding*/
		for (p = &letter_a;p != NULL;p = p->next) {
			if (strcmp(buff,p->morse_mask) == 0) {
			
				/* send */	
				uart_send_byte(p->output);
				if (type == WORD) uart_send_byte(SPACE);
				reset_buff();
				return;
			}	
		}

		/* not match */
		reset_buff();
		beer(WARN);
	}
}
Exemple #22
0
static int my_htoa(__u32 num)
{
	int i, ret;
	char char_buff[32];

	i = 0;
	while (num) {
		char_buff[i++] = ((num % 16) > 9) ? num % 16 + 'a' - 10 : num % 16 + '0';
		num = num >> 4;
	}

	uart_send_byte('0');
	uart_send_byte('x');
	ret = i--;
	do {
		uart_send_byte(char_buff[i]);
	} while (i--);

	uart_send_byte('\n');

	return ret;
}
int main(void)
{
uint8_t chan, low, hi;
uint16_t adcval;

lcd_init();
uart_init(38400);
adc_enable();

for(;;)
  {
    chan = uart_recv_byte();
    if (chan <=7)
        {
        adcval = read_adc(chan);
	lcd_clear();
        lcd_put_int(low);
        low = adcval & 255;
        hi = adcval >> 8;
        uart_send_byte(low);       // send LOW byte
        uart_send_byte(hi);        // send HI byte
        }
  }
Exemple #24
0
// TODO:  support MS hyper-term
static inline void modem_end_rx()
{
	uart_send_byte(ACK);
	// uart_send_byte('C');
	// uart_send_byte(ACK);

	uart_send_byte(CAN);
	uart_send_byte(CAN);
	uart_send_byte(CAN);
	uart_send_byte(CAN);
	uart_send_byte(CAN);
	uart_send_byte(CAN);
}
Exemple #25
0
void VCOM_bridge(void)
{
	uint32_t recv_count;
	recv_count = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
	while(recv_count--)
	{
		out_buff[0] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
		uart_send_byte(out_buff[0]);
	}
	recv_count = uart_get_data(in_buff);
	if(recv_count)
	{
		CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *)in_buff, recv_count);
		Endpoint_ClearIN();
	}
}
Exemple #26
0
void conn_main()
{
	if(conn_pipe_available != 7)
	{
		uint8_t len = NRF24L01_get_payload_len(conn_pipe_available);
		if(len == 0)
		{
			uart_write_async("RX: Length of payload is 0");
			return;
		}
		conn_frame_t* frame = conn_create_frame();
		uint8_t* data = malloc(len);
		if(frame == NULL || data == NULL)
		{
			conn_pipe_available = 7;
			NRF24L01_LOW_set_register(NRF24L01_REG_STATUS, NRF24L01_MASK_STATUS_RX_DR); //Clear data-ready flag
			return;
		}
		NRF24L01_get_received_data(data, len);
		conn_pipe_available = 7;
		NRF24L01_LOW_set_register(NRF24L01_REG_STATUS, NRF24L01_MASK_STATUS_RX_DR); //Clear data-ready flag
		conn_pack_frame(data, frame);
		free(data);
		conn_process_result_t* process_result = conn_process_frame(frame);
		conn_free_frame(frame);
		if(process_result == NULL)
		{
			uart_write_async("RX: Too few memory to save result");
		}
		else
		{
			if(process_result->error_code != CONN_ERROR_OK)
			{
				conn_free_connection(process_result->connection);
				uart_send_byte(process_result->error_code);
			}
			free(process_result);
		}
	}
}
Exemple #27
0
//Most Significant Bit first
void uart_send_BIN4(USART_t* which, uint8_t lowb) {
    switch(lowb) {
    case(0):
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        break;
    case(1):
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        break;
    case(2):
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        break;
    case(3):
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        break;
    case(4):
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        break;
    case(5):
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        break;
    case(6):
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        break;
    case(7):
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        break;
    case(8):
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        break;
    case(9):
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        break;
    case(10):
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        break;
    case(11):
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        break;
    case(12):
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'0');
        break;
    case(13):
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        uart_send_byte(which,'1');
        break;
    case(14):
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'0');
        break;
    case(15):
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        uart_send_byte(which,'1');
        break;
    }
}
Exemple #28
0
PROCESS_THREAD(zigbee_comunication, ev, data)
{
  static struct etimer et;
  unsigned char sum = 0;
  struct st_UartRcv *pdata = NULL;
  int i = 0;
  PROCESS_BEGIN();
  
  GPIO_DeInit(GPIOB);
  GPIO_Init(GPIOC, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast);  //TXD
  GPIO_Init(GPIOC, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT);      //RXD
  //GPIO_Init(GPIOB,GPIO_Pin_6,GPIO_Mode_In_FL_IT);       // ACK
  GPIO_Init(GPIOB,GPIO_Pin_5,GPIO_Mode_Out_PP_High_Fast);       // RESET
  //GPIO_Init(GPIOB,GPIO_Pin_4,GPIO_Mode_In_FL_IT);       // STATE
  GPIO_Init(GPIOB,GPIO_Pin_3,GPIO_Mode_Out_PP_High_Fast);       // WAKEUP
  GPIO_Init(GPIOB,GPIO_Pin_2,GPIO_Mode_Out_PP_High_Fast);       // SLEEP
  GPIO_Init(GPIOB,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Fast);       // DEF
  GPIO_Init(GPIOD,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Fast);       // DIR
#if 0 
  GPIO_WriteBit(GPIOB, GPIO_Pin_1, RESET);
  GPIO_WriteBit(GPIOB, GPIO_Pin_5, RESET);
  
  etimer_set(&et, CLOCK_SECOND / 10);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  
  GPIO_WriteBit(GPIOB, GPIO_Pin_5, SET);
  
  etimer_set(&et, CLOCK_SECOND / 10);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  
  GPIO_WriteBit(GPIOB, GPIO_Pin_1, SET);
#endif
  SYSCFG_REMAPPinConfig(REMAP_Pin_USART1TxRxPortC,ENABLE);
  CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
  /* USART configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - Odd parity
        - Receive and transmit enabled
        - USART Clock disabled
  */
  USART_Init(USART1, (uint32_t)115200, USART_WordLength_8b, USART_StopBits_1,
                   USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));

  /* Enable the USART Receive interrupt: this interrupt is generated when the USART
    receive data register is not empty */
  //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  /* Enable the USART Transmit complete interrupt: this interrupt is generated when the USART
    transmit Shift Register is empty */
  //USART_ITConfig(USART1, USART_IT_TC, ENABLE);
  
  /* Enable USART */
  //USART_Cmd(USART1, ENABLE);
    
  USART_Cmd(USART1, ENABLE);
  //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
#if 0 
  PUTSTRING("Now config ZM516X\r\n");

readLocalInfo:
  memcpy(usart_buf,read_local_cfg,sizeof(read_local_cfg));
  usart_buf[4] = usart_buf[0] + usart_buf[1] + usart_buf[2] + usart_buf[3];

  uart_send_byte(5,usart_buf);
   
  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_MSG);
  pdata = (struct st_UartRcv *)data;
 
  if((pdata->buf[0] == 0xAB) && (pdata->buf[1] == 0xBC) && (pdata->buf[2] == 0xCD) && (pdata->buf[3] == 0xD1))
  {
  }
  else
  {
    goto readLocalInfo;
  }
  memcpy(&stDevInfo,&pdata->buf[4],65);

  usart_buf[0] = 0xab;
  usart_buf[1] = 0xbc;
  usart_buf[2] = 0xcd;
  usart_buf[3] = enModifyCfg;
  usart_buf[4] = stDevInfo.devLoacalNetAddr[0];
  usart_buf[5] = stDevInfo.devLoacalNetAddr[1];
  stDevInfo.devLoacalNetAddr[0] = 0x00;
  stDevInfo.devLoacalNetAddr[1] = 0x03;
  //memset(stDevInfo.devLoacalNetAddr,0,2);
  stDevInfo.devDestNetAddr[0] = 0x00;
  stDevInfo.devDestNetAddr[1] = 0x00;
  stDevInfo.devChannel = 0x19;
  stDevInfo.devPanid[0] = 0x10;
  stDevInfo.devPanid[1] = 0x01;
  memcpy(&usart_buf[6],&stDevInfo,65);

  for(i = 0;i < (6 + 65);i++)
  {
      sum += usart_buf[i];
  }
  usart_buf[6 + 65] = sum;
  
  uart_send_byte(6 + 65 + 1,usart_buf);
  
  etimer_set(&et, CLOCK_SECOND);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  GPIO_WriteBit(GPIOB,GPIO_Pin_5,RESET);
  etimer_set(&et, CLOCK_SECOND / CLOCK_SECOND);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  GPIO_WriteBit(GPIOB,GPIO_Pin_5,SET);
#endif
  //uart_send_byte(5,"hello");
  process_start(&hmc5983_work, NULL);
  while(1)
  {
    //etimer_set(&et, CLOCK_SECOND);
    //PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_MSG);
    //pdata = (struct st_UartRcv *)data;
    //if(memcmp(pdata->buf,"get",3) == 0)
    if(flg)
    {
      usart_buf[0] = 0xAA;
      usart_buf[1] = 0xBB;
      usart_buf[2] = 0xCC;
      *(int*)&usart_buf[3] = x;
      *(int*)&usart_buf[5] = y;
      *(int*)&usart_buf[7] = z;
      usart_buf[9] = 0;
      for(i = 0;i < 9;i ++)
      {
        usart_buf[9] += usart_buf[i];
      }
      
      usart_buf[9] = 0xff - usart_buf[9];
      GPIO_WriteBit(GPIOD, GPIO_Pin_0, SET);
      etimer_set(&et, CLOCK_SECOND / 20);
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
      uart_send_byte(10,usart_buf);
      etimer_set(&et, CLOCK_SECOND / 20);
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
      GPIO_WriteBit(GPIOD, GPIO_Pin_0, RESET);
    }
    //etimer_set(&et, CLOCK_SECOND / 10);
    //PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    //memcpy(usart_buf,"hello!\r\n",8);
    
    //uart_send_byte(8,usart_buf);
  }
  PROCESS_EXIT();
  PROCESS_END();
}
Exemple #29
0
void uart_factory_reset_confirm() {
	uart_send_byte(UTOK_RST_PROPOSE);
	while (1) {
		if (uart_has_data() && (uart_consume() == UTOK_RST_CONFIRM)) return;
	}
}
Exemple #30
0
int main(void)
{
	// configure WDT
	WDTCTL = WDTPW | WDTHOLD;				// stop watch dog timer
	_25mhz();


#ifdef TEST
	// when compiled in test mode, use different main
	// disconnect radio when testing to avoid damage!
	test_main();
#endif

	// configure LED1 and turn it off, we'll use that for error and other stuff
	P1DIR |= LED1;
	LED1_OFF;

	P4DIR |= LED2;
	LED2_ON;

	// setup uart
	uart_init();

#ifdef DEBUG_MESSAGES
	uart_send_string("Hola mundo!\r\n");
#endif

	// setup packet handler
	ph_setup();

	// setup an configure radio
	radio_setup();
	radio_configure();

	// self-calibrate image rejection
	radio_calibrate_ir();

	// verify that radio configuration was successful
	radio_get_chip_status(0);
	if (radio_buffer.chip_status.chip_status & RADIO_CMD_ERROR) {	// check for command error
		uart_send_string("Error inicializando radio!!!\r\n");
		while (1) {
			LED1_TOGGLE;
			_delay_cycles(8000000);			// blink LED if there was an error
		}
	}


	// start packet receiving
	ph_start();

#ifdef DEBUG_MESSAGES
	uart_send_string("dAISy 0.2 started\r\n");
	LED2_OFF;
#endif

	while (1) {

		LPM0;	// deep sleep until something worthwhile happens

		__no_operation();

		ph_loop();	// packet handler house-keeping, e.g. channel hopping

#ifdef DEBUG_MESSAGES
		uint8_t channel;
		int16_t rssi;
		// debug code to monitor signal strength (RSSI)
		if (ph_get_state() == PH_STATE_PREFETCH) {											// found preamble and start flag
			// record current channel and signal strength
			channel = ph_get_radio_channel();												// read current channel
			rssi = ph_get_radio_rssi();														// read current RSSI
		}
#endif

		// retrieve last packet handler error
		uint8_t error = ph_get_last_error();
#ifdef DEBUG_MESSAGES
		// report error if packet handler failed
		if (error != PH_ERROR_NONE)	{
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
			uart_send_string("error: ");
			switch (error) {
			case PH_ERROR_NOEND:
				uart_send_string("no end flag");
				break;
			case PH_ERROR_STUFFBIT:
				uart_send_string("invalid stuff bit");
				break;
			case PH_ERROR_CRC:
				uart_send_string("CRC error");
				break;
			case PH_ERROR_RSSI_DROP:
				uart_send_string("RSSI drop");
				break;
			}
			uart_send_string("\r\n");
			ph_loop();							// house keeping, sending over UART takes time
		}
#else
		// toggle LED if packet handler failed after finding preamble and start flag
		if (error == PH_ERROR_NOEND || error == PH_ERROR_STUFFBIT || error == PH_ERROR_CRC)
			LED1_TOGGLE;
#endif

		// check if a new valid packet arrived
		uint16_t size = fifo_get_packet();
		if (size > 0) {								// if so, process packet

#ifdef DEBUG_MESSAGES
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
#endif
			LED2_ON;
			nmea_process_packet();					// process packet (NMEA message will be sent over UART)
			fifo_remove_packet();					// remove processed packet from FIFO
			LED2_OFF;
		}

		// enter low power mode LPM0 (everything off)
		// TODO: wait for UART to complete transmission

		// TODO: suspend UART
	}
}