Simple example of FreeRTOS on Arduino

2013-01-06
2013-01-07
  • Nicolas JAN
    Nicolas JAN
    2013-01-06

    Hi,

    I work on a project which need some tasks carried out in the same time on
    my Arduino Uno. And after few web researches, I found your project.

    I try to make a simple example with a task which writes on the serial port.
    But when I load the program, the arduino card sends me texts with strange
    characters (example : GCÿƒ#ÿØ#ü¿cÿ€¤äƒ›ç¼ƒã£Dà [Ã'§'ä['¼¸C#$Dà)

    Have you an idea where comes from this bug ?

    This is my code :

    main.c :

    include

    include

    include

    #include io.h>

    / Scheduler include files. /

    include

    include

    include

    include

    / serial interface include file. /

    include

    / Create a handle for the serial port. /

    xComPortHandle xSerialPort;

    static void vHelloWorld(void*);

    static void vHelloWorld(void*)

    {

    for( ;; )

    {

    xSerialPrintf("Hello World !\n");

    vTaskDelay(1000);

    }

    }

    / Main program loop /

    int16_t main(void) attribute((OS_main));

    int16_t main(void)

    {

    xSerialPort = xSerialPortInitMinimal( 115200, portSERIAL_BUFFER,
    portSERIAL_BUFFER);

    avrSerialPrintf("Start of Main()\n");

    xTaskCreate( vHelloWorld, ( signed portCHAR * ) "vHelloWorld", 50,
    NULL,tskIDLE_PRIORITY+2, NULL );

    vTaskStartScheduler();

    }

    --
    Nicolas JAN

     
    • Nicolas,

      thanks for writing.

      Garbage on the serial port is almost always caused by a mismatch of speeds. The Arduino frequency of 16MHz is one of the worst cases, because it doesn't divide closely to any serial bit rate and there's always a small rate error.

      I'm guessing that this case is caused because the CPU_CLOCK_HZ is not set to 16000000. This is done in the freeRTOS/include/FreeRTOSConfig.h file. Just adjust the comment appropriately, from my default below.

          #define configCPU_CLOCK_HZ      ( ( uint32_t ) 22118400 )       // Modded Arduino (usually 16000000) with 328p device
      //  #define configCPU_CLOCK_HZ      ( ( uint32_t ) 16000000 )       // Arduino with 328p device
      

      For a bit more background, see here.
      http://feilipu.posterous.com/freetronics-2010-arduino-overclocking-and-rev

      Good luck.