2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal_console.h>
|
|
|
|
#include <furi_hal_uart.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stm32wbxx_ll_gpio.h>
|
|
|
|
#include <stm32wbxx_ll_usart.h>
|
|
|
|
#include <m-string.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#include <utilities_conf.h>
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
#include <furi.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalConsole"
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
#ifdef HEAP_PRINT_DEBUG
|
|
|
|
#define CONSOLE_BAUDRATE 1843200
|
|
|
|
#else
|
2021-10-21 21:12:20 +03:00
|
|
|
#define CONSOLE_BAUDRATE 230400
|
2022-02-18 22:53:46 +03:00
|
|
|
#endif
|
2021-10-21 21:12:20 +03:00
|
|
|
|
2022-04-14 19:41:15 +03:00
|
|
|
typedef struct {
|
|
|
|
bool alive;
|
|
|
|
FuriHalConsoleTxCallback tx_callback;
|
|
|
|
void* tx_callback_context;
|
|
|
|
} FuriHalConsole;
|
|
|
|
|
|
|
|
FuriHalConsole furi_hal_console = {
|
|
|
|
.alive = false,
|
|
|
|
.tx_callback = NULL,
|
|
|
|
.tx_callback_context = NULL,
|
|
|
|
};
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
void furi_hal_console_init() {
|
2021-10-26 21:41:56 +03:00
|
|
|
furi_hal_uart_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
|
2022-04-14 19:41:15 +03:00
|
|
|
furi_hal_console.alive = true;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
void furi_hal_console_enable() {
|
2021-11-20 01:19:31 +03:00
|
|
|
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
|
2022-01-05 19:10:18 +03:00
|
|
|
while(!LL_USART_IsActiveFlag_TC(USART1))
|
|
|
|
;
|
2021-10-26 21:41:56 +03:00
|
|
|
furi_hal_uart_set_br(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
|
2022-04-14 19:41:15 +03:00
|
|
|
furi_hal_console.alive = true;
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
void furi_hal_console_disable() {
|
2022-01-05 19:10:18 +03:00
|
|
|
while(!LL_USART_IsActiveFlag_TC(USART1))
|
|
|
|
;
|
2022-04-14 19:41:15 +03:00
|
|
|
furi_hal_console.alive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_console_set_tx_callback(FuriHalConsoleTxCallback callback, void* context) {
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
furi_hal_console.tx_callback = callback;
|
|
|
|
furi_hal_console.tx_callback_context = context;
|
|
|
|
FURI_CRITICAL_EXIT();
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 12:48:08 +03:00
|
|
|
void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
|
2022-04-14 19:41:15 +03:00
|
|
|
if(!furi_hal_console.alive) return;
|
2021-09-21 12:48:08 +03:00
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2021-09-21 12:48:08 +03:00
|
|
|
// Transmit data
|
2022-04-14 19:41:15 +03:00
|
|
|
|
|
|
|
if(furi_hal_console.tx_callback) {
|
|
|
|
furi_hal_console.tx_callback(buffer, buffer_size, furi_hal_console.tx_callback_context);
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)buffer, buffer_size);
|
2021-09-21 12:48:08 +03:00
|
|
|
// Wait for TC flag to be raised for last char
|
2022-01-05 19:10:18 +03:00
|
|
|
while(!LL_USART_IsActiveFlag_TC(USART1))
|
|
|
|
;
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-09-21 12:48:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_console_tx_with_new_line(const uint8_t* buffer, size_t buffer_size) {
|
2022-04-14 19:41:15 +03:00
|
|
|
if(!furi_hal_console.alive) return;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2021-09-21 12:48:08 +03:00
|
|
|
// Transmit data
|
2021-10-26 21:41:56 +03:00
|
|
|
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)buffer, buffer_size);
|
2021-09-21 12:48:08 +03:00
|
|
|
// Transmit new line symbols
|
2021-10-26 21:41:56 +03:00
|
|
|
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)"\r\n", 2);
|
2021-09-21 12:48:08 +03:00
|
|
|
// Wait for TC flag to be raised for last char
|
2022-01-05 19:10:18 +03:00
|
|
|
while(!LL_USART_IsActiveFlag_TC(USART1))
|
|
|
|
;
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_console_printf(const char format[], ...) {
|
|
|
|
string_t string;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
string_init_vprintf(string, format, args);
|
|
|
|
va_end(args);
|
|
|
|
furi_hal_console_tx((const uint8_t*)string_get_cstr(string), string_size(string));
|
|
|
|
string_clear(string);
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
void furi_hal_console_puts(const char* data) {
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_console_tx((const uint8_t*)data, strlen(data));
|
2021-10-26 21:41:56 +03:00
|
|
|
}
|