Add thread-safe (-ish) console output
This commit is contained in:
parent
7f5ef3de8a
commit
01807f47ab
6 changed files with 129 additions and 11 deletions
|
@ -5,11 +5,15 @@
|
|||
* Author: abody
|
||||
*/
|
||||
#include <main.h>
|
||||
#include <projdefs.h>
|
||||
|
||||
#include <cmsis_os.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
|
||||
#include <f4ll/console_handler.h>
|
||||
#include <f4ll/str_util.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -18,24 +22,64 @@
|
|||
|
||||
void app_main(void const *param)
|
||||
{
|
||||
(void)param;
|
||||
app::init().main();
|
||||
|
||||
app::init(param).main();
|
||||
}
|
||||
|
||||
app::app()
|
||||
app::app(void const *param)
|
||||
: m_con(
|
||||
f4ll::console_handler::init(
|
||||
USART2, DMA1, LL_DMA_STREAM_5, LL_DMA_STREAM_6, m_rx_buf_mem, sizeof(m_rx_buf_mem), m_tx_buf_mem, sizeof(m_tx_buf_mem),
|
||||
nullptr))
|
||||
nullptr)),
|
||||
m_safe_conout(m_con)
|
||||
|
||||
{
|
||||
(void)param;
|
||||
}
|
||||
|
||||
void app::main()
|
||||
{
|
||||
m_con.print("-------------------------------------------\n");
|
||||
m_cli_task_handle = xTaskCreateStatic(
|
||||
cli_task_dispatch, "CLI_task", CLI_TASK_STACK_LEN, this, osPriorityAboveNormal, m_cli_task_stack, &m_cli_task_buffer);
|
||||
|
||||
queue_ts();
|
||||
m_safe_conout.print("-------------------------------------------\n");
|
||||
while (true) {
|
||||
m_con.print("Hello woooooooooooooooooooooooooooooooorld!\n");
|
||||
queue_ts();
|
||||
m_safe_conout.print("Hello woooooooooooooooooooooooooooooooorld!\n");
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
|
||||
}
|
||||
}
|
||||
|
||||
void app::input_available(f4ll::console_handler::size_type len)
|
||||
{
|
||||
// TODO: trigger a semaphore here and do the processing from a task. This is running in interrupt context
|
||||
f4ll::iringbuffer &rb = f4ll::console_handler::instance().get_rx_buffer();
|
||||
f4ll::console_handler::size_type chunk_len = 0;
|
||||
uint8_t const *chunk = nullptr;
|
||||
bool more = false;
|
||||
do {
|
||||
more = rb.get_chunk(chunk, chunk_len);
|
||||
if (chunk_len) {
|
||||
rb.consumed(chunk_len);
|
||||
}
|
||||
} while (more);
|
||||
}
|
||||
|
||||
void app::cli_task()
|
||||
{
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||
queue_ts();
|
||||
m_safe_conout.print(">>> TICK <<<\n");
|
||||
}
|
||||
}
|
||||
|
||||
void app::queue_ts()
|
||||
{
|
||||
char ts_buf[12];
|
||||
uitodec(ts_buf, xTaskGetTickCount());
|
||||
m_safe_conout.append(ts_buf);
|
||||
m_safe_conout.append(" ");
|
||||
}
|
34
components/app/src/thread_safe_console_output.cpp
Normal file
34
components/app/src/thread_safe_console_output.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <f4ll/console_handler.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <portmacro.h>
|
||||
|
||||
#include <thread_safe_console_output.h>
|
||||
|
||||
thread_safe_console_output::thread_safe_console_output(f4ll::console_handler &con)
|
||||
: m_con(con),
|
||||
m_sem(xSemaphoreCreateMutexStatic(&m_sem_buf))
|
||||
{
|
||||
}
|
||||
|
||||
thread_safe_console_output::size_type thread_safe_console_output::append(char const *s)
|
||||
{
|
||||
xSemaphoreTake(m_sem, portMAX_DELAY);
|
||||
auto ret = m_con.append(s);
|
||||
xSemaphoreGive(m_sem);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void thread_safe_console_output::flush()
|
||||
{
|
||||
xSemaphoreTake(m_sem, portMAX_DELAY);
|
||||
m_con.flush();
|
||||
xSemaphoreGive(m_sem);
|
||||
}
|
||||
|
||||
void thread_safe_console_output::print(const char *s)
|
||||
{
|
||||
xSemaphoreTake(m_sem, portMAX_DELAY);
|
||||
m_con.print(s);
|
||||
xSemaphoreGive(m_sem);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue