2021-09-10 02:19:02 +00:00
|
|
|
#include <furi-hal-console.h>
|
2021-10-26 18:41:56 +00:00
|
|
|
#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"
|
|
|
|
|
2021-10-21 18:12:20 +00:00
|
|
|
#define CONSOLE_BAUDRATE 230400
|
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
volatile bool furi_hal_console_alive = false;
|
|
|
|
|
|
|
|
void furi_hal_console_init() {
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_hal_uart_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
|
2021-09-10 02:19:02 +00:00
|
|
|
furi_hal_console_alive = true;
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
void furi_hal_console_enable() {
|
|
|
|
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL);
|
2021-10-21 18:12:20 +00:00
|
|
|
while (!LL_USART_IsActiveFlag_TC(USART1));
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_hal_uart_set_br(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
|
2021-10-21 18:12:20 +00:00
|
|
|
furi_hal_console_alive = true;
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
void furi_hal_console_disable() {
|
|
|
|
while (!LL_USART_IsActiveFlag_TC(USART1));
|
|
|
|
furi_hal_console_alive = false;
|
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) {
|
|
|
|
if (!furi_hal_console_alive)
|
|
|
|
return;
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
UTILS_ENTER_CRITICAL_SECTION();
|
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
|
|
|
// Wait for TC flag to be raised for last char
|
|
|
|
while (!LL_USART_IsActiveFlag_TC(USART1));
|
2021-11-12 13:04:35 +00:00
|
|
|
UTILS_EXIT_CRITICAL_SECTION();
|
2021-09-21 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_console_tx_with_new_line(const uint8_t* buffer, size_t buffer_size) {
|
|
|
|
if (!furi_hal_console_alive)
|
|
|
|
return;
|
2021-09-10 02:19:02 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
UTILS_ENTER_CRITICAL_SECTION();
|
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
|
2021-09-10 02:19:02 +00:00
|
|
|
while (!LL_USART_IsActiveFlag_TC(USART1));
|
2021-11-12 13:04:35 +00:00
|
|
|
UTILS_EXIT_CRITICAL_SECTION();
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_console_puts(const char *data) {
|
|
|
|
furi_hal_console_tx((const uint8_t*)data, strlen(data));
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|