Attached is a project with console server that sends strings to UART in ChibiOS synchronous mode.
Since I'm newbie in ChibiOS it is definitely not perfect, but it works. Might be useful for beginners to start.
Project made for ChibiOS 2.6.6 in EM::Blocks 2.30 with Nucleo 152RE board. It based on code shown early in this thread by its author and Giovanni
Hopefully the emblocks author will create ChibiOS 3.0 template (I could not). After that this project might be adapted.
Here are the main changes I made:
main.cCode: Select all
Thread *DebugConsole; // somehow this thread should be in the main. When it is located in other file of the project, the console server stops working in an hour.
// When it is located in main.c the program works long time. At least overnight.
....
static msg_t Blinky(void *arg)
{
(void)arg;
uint16_t cnt = 0;
chRegSetThreadName("Blinky");
while(1)
{
M3_DEBUG(2,0, "%s Number %d", "LED ON", cnt); // parameters: debug messages level = 2, output timestamp = 0(no timestamp), message format, data
palSetPad(GPIOA, GPIOA_LED_GREEN);
chThdSleepMilliseconds(500);
M3_DEBUG(3, 1, "LED OFF\r\n");
palClearPad(GPIOA, GPIOA_LED_GREEN);
chThdSleepMilliseconds(500);
cnt++;
}
return 0;
}
console.c - adapted Giovanni code for ChibiOS 2.6.6
Code: Select all
char DebugMessage[DEBUGLINE_LENGTH]; // Definitely not ChibiOS style solution, but I don't know the right way yet.
....
static msg_t ConsoleServer(void *arg)
{
(void)arg;
Thread *tp;
chRegSetThreadName("ConsoleServer");
BaseSequentialStream *stream = (BaseSequentialStream *)arg;
while (true)
{
/* Waiting for a queued message then retrieving it.*/
tp = chMsgWait();
const char *msg = (const char *)chMsgGet(tp);
/* Printing message the message prefixing it with a time stamp.*/
if(msg[0] != '0')
chprintf(stream, "%010d: %s\r\n", chTimeNow(), msg);
else
chprintf(stream, "%s\r\n", msg+1); // without time stamp
/* Sending back an acknowledge.*/
while(chMsgIsPendingI(tp)) // without this check the task stopped after about an hour
chThdSleepMilliseconds(50);
chMsgRelease(tp, MSG_OK);
}
}
console.hCode: Select all
#define M3_DEBUG( level, timeon, ... ) \
{ int16_t i; \
chSysLock(); \
if(console_debug && level <= console_debug) \
{ \
chsnprintf(DebugMessage, DEBUGLINE_LENGTH - 1, ## __VA_ARGS__ ); \
if(!timeon) \
{ \
for ( i = strlen(DebugMessage); i>=0; i--) \ //memcpy/strncpy implementation in this version of GCC are not correct
DebugMessage[i+1] = DebugMessage[i]; \ // so, had to copy it by old fashioned way
DebugMessage[0] = '0'; \ // '0' in the string beginning shows that time stamp should not be shown
} \
chSysUnlock(); \
(void)chMsgSend(DebugConsole, (msg_t)DebugMessage); \
} \
else \
chSysUnlock(); \
}
Used macro because there is no a simple way to transfer format and data to printf like function.
Will be glad to see any advises how to make this code more effective and correct style.