Skip to content

Commit

Permalink
Console: Limit the problem when printing from interrupt
Browse files Browse the repository at this point in the history
This commits adds a consolePutcharFromISR frunction that only prints in
the local buffer. This limits the amount of data that can be printed from an 
interrupt but it also prevents the firmware to crash as described in #89
  • Loading branch information
ataffanel committed Feb 8, 2016
1 parent 7f42bbb commit 3bab819
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
20 changes: 16 additions & 4 deletions modules/interface/console.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
Expand Down Expand Up @@ -45,11 +45,23 @@ bool consoleTest(void);
*/
int consolePutchar(int ch);

/**
* Put a character to the console buffer
*
* @param ch character that shall be printed
* @return The character casted to unsigned int or EOF in case of error
*
* @note This version can be called by interrup. In such case the internal
* buffer is going to be used. If a task currently is printing or if the
* interrupts prints too much the data will be ignored.
*/
int consolePutcharFromISR(int ch);

/**
* Put a null-terminated string on the console buffer
*
* @param str Null terminated string
* @return a nonnegative number on success, or EOF on error.
* @return a nonnegative number on success, or EOF on error.
*/
int consolePuts(char *str);

Expand All @@ -60,7 +72,7 @@ void consoleFlush(void);

/**
* Macro implementing consolePrintf with eprintf
*
*
* @param FMT String format
* @patam ... Parameters to print
*/
Expand Down
37 changes: 30 additions & 7 deletions modules/src/console.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
Expand Down Expand Up @@ -32,6 +32,8 @@

#include "crtp.h"

#include "stm32fxxx.h"

CRTPPacket messageToPrint;
xSemaphoreHandle synch = NULL;

Expand Down Expand Up @@ -65,7 +67,7 @@ void consoleInit()
messageToPrint.size = 0;
messageToPrint.header = CRTP_HEADER(CRTP_PORT_CONSOLE, 0);
vSemaphoreCreateBinary(synch);

isInit = true;
}

Expand All @@ -77,9 +79,15 @@ bool consoleTest(void)
int consolePutchar(int ch)
{
int i;
bool isInInterrupt = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;

if (!isInit)
if (!isInit) {
return 0;
}

if (isInInterrupt) {
return consolePutcharFromISR(ch);
}

if (xSemaphoreTake(synch, portMAX_DELAY) == pdTRUE)
{
Expand All @@ -102,17 +110,32 @@ int consolePutchar(int ch)
}
xSemaphoreGive(synch);
}

return (unsigned char)ch;
}

int consolePutcharFromISR(int ch) {
BaseType_t higherPriorityTaskWoken;

if (xSemaphoreTakeFromISR(synch, &higherPriorityTaskWoken) == pdTRUE) {
if (messageToPrint.size < CRTP_MAX_DATA_SIZE)
{
messageToPrint.data[messageToPrint.size] = (unsigned char)ch;
messageToPrint.size++;
}
xSemaphoreGiveFromISR(synch, &higherPriorityTaskWoken);
}

return ch;
}

int consolePuts(char *str)
{
int ret = 0;

while(*str)
ret |= consolePutchar(*str++);

return ret;
}

Expand Down

0 comments on commit 3bab819

Please sign in to comment.