«

»

Août 27

Robotics 101 : Transmitting data with ChibiOS Serial Driver

Now that you know how to configure the devices and what the serial driver does, let’s put it in practice.

For this tutorial you will need:

serialCable

Sample code :


#include "ch.h"
#include "hal.h"
static SerialConfig uartCfg =
{
9600, // bit rate
};
int main(void){
halInit(); // initialize Hardware Abstraction Layer
chSysInit(); // initialize ChibiOS kernel
/* sdInit(); should be used as we see in the SerialDriver state machine
, but it is already implicitely called in halInit();*/
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7)); // used function : USART3_TX
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7)); // used function : USART3_RX
sdStart(&SD3, &uartCfg); // starts the serial driver with uartCfg as a config
char data[] = "Hello World ! \n \r"; /* we create the string "Hello World!", \n means go to the next line
\r sets it to the left of the screen */
while(1){
sdWrite(&SD3, (uint8_t *) data, strlen(data)); // Writes "Hello World in the UART output
chThdSleepMilliseconds(500);
}
}

This code sends the string « Hello World ! » every 0.5 seconds on USART3 of the board, using the serial driver SD3.

Let’s learn about the new functions !

WAIIIT !!! This code is not compiling, i have an error with this SD3 of yours : it doesn’t exist !

You’re perfectly right, if you didn’t activate USART3 this code won’t work, we will see how to do it in the next paragraph.

Messing with mcuconf.h :

As you may have seen, there are other files tha main.c in the sample folder:

  • chconf.h : chooses and configures the services used by chibiOS kernel in the application
  • halconf.h : chooses and configures the peripherals used by the application
  • mcuconf.h< : controls the micro controller's configuration /li>
  • Makefile : helps to compile your code (manages dependencies and where to find all the source code)

For now we are only going to see how to mess with the mcuconf.h file. What we want to do is to use the serial driver of USART3 (we could have used any other, but i am sure that this one is not activated in the demo folder we copied).
First open mcuconf.h, then activate the serial driver for USART3 as in the picture below :
mcuconf (1)
In the general case scenario where you have to activate/deactivate a driver (call it driver x), you have to look in the mcuconf.h file for the block about this driver x, in this block you will see its instances and parameters, then simply activate the one you want by defining its use as TRUE or deactivate it by defining its use as FALSE.

That’s about it for the mcuconf.h file.

Code explanation :

palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7)); // used function : USART3_TX
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7)); // used function : USART3_RX
As you have seen we are using PB10 and PB11 for the UART with an alternate function as we did in the PWM tutorial. Just like before, we have to check in the datasheet which pin can use UART, and use the proper alternate function.

The new functions seen in this code are :

  • sdStart(&SD3, &uartCfg);
  • sdWrite(&SD3, (uint8_t *) data, strlen(data));
sdStart(SerialDriver* SD, SerialConfig* cfg)

It takes two parameters:

  • a pointer to a SerialDriver : It is the serial driver you are going to use, note that it must be activated in the mcuconf.h
  • a pointer to a SerialConfig : It is the configuration you want to use with your serial driver

This function, as the name suggests, starts the serial driver with the appropriate config (if cfg is NULL, the baud rate is set at 38400).

sdWrite(SerialDriver* SDX, uint8_t* data, size_t n)

It takes three parameters:

  • a pointer to the serial driver you want to use : SDX corresponding to USARTX
  • a uint8_t table : the bytes you want to send
  • a size_t number : the length length (in bytes) of the transmited data

This function transmits the data (as a uint8_t table) bytes through USARTX.

sdPut(SerialDriver* SDX, uint8_t b)

Simply transmits the byte b, on USARTX.

Connect it up

First connect the GND together (black wire on the cable), the connect the board USART3_TX to your cable RX (i have no idea whether it’s the yellow or orange, simply try both and note it somewhere) and the board USART3_RX to your cable TX.
It should look something like this if you took a male serial-usb cable :
uartsend

Then make sure your usb-serial cable is plugged to your computer, and on the correct ttyUSB, that you configured minicom to the proper baud rate. Eventually launch minicom in a shell, you should see your message (« Hello World ! ») received every 500 milliseconds.

And you’re done ! This program transmits data from the board to an other device (your computer here, but it could be a radio module, a xbee module, etc…).

Felix.

Feel free to use the comment section if you have a question 😉