Thursday 27 September 2012

How to use UART to send and receive data in Contiki




1. By default, Contiki allows us to send data on UART0 but not receiving any data. So, we should enable it. 

2. For doing this, let's check out the contiki-conf.h placed on the /contiki-sensinode/platform/cc2530dk/. The code below is the part we need from that file.




#ifndef UART0_CONF_WITH_INPUT                                                                               
#define UART0_CONF_WITH_INPUT 0
#endif



3. As we can see the UART0_CONF_WITH_INPUT is set 0 but this causes we receive nothing from the port, so we set its value to 1.

4. That's it. Now we can use the following code to send and receive data from the port. We receive data by registering an interrupt callback function.





  • Take note that enabling the UART0 costs you around 200 byte.
  • Do not forget to connect your SMARTRF to PC using RS232.




/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/leds.h"
#include "sys/clock.h"
#include "dev/uart0.h"
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
static struct etimer et;
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
PROCESS(test_send_process, "Testing send data");
PROCESS(test_receive_process, "Testing receive data");
AUTOSTART_PROCESSES(&test_send_process,&test_receive_process);
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int uart_rx_callback(unsigned char c)
{
uart0_writeb(c);
return 1;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_send_process, ev, data)
{
  PROCESS_BEGIN();
  while(1) 
  {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    uart0_writeb(42);
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/

PROCESS_THREAD(test_receive_process, ev, data)
{

  PROCESS_BEGIN();
  
uart0_set_input(uart_rx_callback);

  while(1) 
  {
  PROCESS_YIELD();
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/










1 comment: