Beginners - Serial Echo (UART)
Posted: Wed Jun 11, 2014 11:17 am
Code sample for Serial (UART)
ChibiOS has two UART implementations. Serial is simpler for using in terminal application.
Programm echoes byte to terminal.
Here is example for
Changes in halconf.h
Choose USART1 in mcuconf.h
main.c
ChibiOS has two UART implementations. Serial is simpler for using in terminal application.
Programm echoes byte to terminal.
Here is example for
- thread creation
- waiting for event
- receiving and transmiting to UART
Changes in halconf.h
Code: Select all
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL TRUE
#endif
#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
#define SERIAL_DEFAULT_BITRATE 115200 // set baudrate here
#endif
Choose USART1 in mcuconf.h
Code: Select all
#define STM32_SERIAL_USE_USART1 TRUEmain.c
Code: Select all
#include <ch.h>
#include <hal.h>
#include <stm32f10x.h>
static WORKING_AREA(waSerEcho, 128);
static msg_t thSerEcho(void *arg)
{
(void)arg;
chRegSetThreadName("SerEcho");
EventListener elSerData;
flagsmask_t flags;
chEvtRegisterMask((EventSource *)chnGetEventSource(&SD1), &elSerData, EVENT_MASK(1));
while (TRUE)
{
chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
chSysLock();
flags = chEvtGetAndClearFlags(&elSerData);
chSysUnlock();
if (flags & CHN_INPUT_AVAILABLE)
{
msg_t charbuf;
do
{
charbuf = chnGetTimeout(&SD1, TIME_IMMEDIATE);
if ( charbuf != Q_TIMEOUT )
{
chSequentialStreamPut(&SD1, charbuf);
}
}
while (charbuf != Q_TIMEOUT);
}
}
return 0;
}
int main(void) {
halInit();
chSysInit();
/*
* Activates the serial driver 1 using the driver default configuration.
*/
sdStart(&SD1, NULL);
chThdCreateStatic(waSerEcho, sizeof(waSerEcho), NORMALPRIO, thSerEcho, NULL);
while (!chThdShouldTerminate()) {
chThdSleepMilliseconds(500);
}
return 0;
}