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