2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_uart.h>
|
2021-10-26 18:41:56 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stm32wbxx_ll_lpuart.h>
|
|
|
|
#include <stm32wbxx_ll_usart.h>
|
2022-03-30 15:23:40 +00:00
|
|
|
#include <stm32wbxx_ll_rcc.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_resources.h>
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
static bool furi_hal_usart_prev_enabled[2];
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
static void (*irq_cb[2])(uint8_t ev, uint8_t data, void* context);
|
|
|
|
static void* irq_ctx[2];
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
static void furi_hal_usart_init(uint32_t baud) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2021-10-26 18:41:56 +00:00
|
|
|
&gpio_usart_tx,
|
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullUp,
|
|
|
|
GpioSpeedVeryHigh,
|
|
|
|
GpioAltFn7USART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2021-10-26 18:41:56 +00:00
|
|
|
&gpio_usart_rx,
|
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullUp,
|
|
|
|
GpioSpeedVeryHigh,
|
|
|
|
GpioAltFn7USART1);
|
|
|
|
|
|
|
|
LL_USART_InitTypeDef USART_InitStruct = {0};
|
|
|
|
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
|
|
|
|
USART_InitStruct.BaudRate = baud;
|
|
|
|
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
|
|
|
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
|
|
|
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
|
|
|
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
|
|
|
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
|
|
|
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
|
|
|
LL_USART_Init(USART1, &USART_InitStruct);
|
|
|
|
LL_USART_EnableFIFO(USART1);
|
|
|
|
LL_USART_ConfigAsyncMode(USART1);
|
|
|
|
|
|
|
|
LL_USART_Enable(USART1);
|
|
|
|
|
2022-02-10 11:20:50 +00:00
|
|
|
while(!LL_USART_IsActiveFlag_TEACK(USART1) || !LL_USART_IsActiveFlag_REACK(USART1))
|
2021-11-19 22:19:31 +00:00
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
LL_USART_EnableIT_RXNE_RXFNE(USART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void furi_hal_lpuart_init(uint32_t baud) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2021-10-26 18:41:56 +00:00
|
|
|
&gpio_ext_pc0,
|
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullUp,
|
|
|
|
GpioSpeedVeryHigh,
|
|
|
|
GpioAltFn8LPUART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2021-10-26 18:41:56 +00:00
|
|
|
&gpio_ext_pc1,
|
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullUp,
|
|
|
|
GpioSpeedVeryHigh,
|
|
|
|
GpioAltFn8LPUART1);
|
|
|
|
|
|
|
|
LL_LPUART_InitTypeDef LPUART_InitStruct = {0};
|
|
|
|
LPUART_InitStruct.PrescalerValue = LL_LPUART_PRESCALER_DIV1;
|
|
|
|
LPUART_InitStruct.BaudRate = 115200;
|
|
|
|
LPUART_InitStruct.DataWidth = LL_LPUART_DATAWIDTH_8B;
|
|
|
|
LPUART_InitStruct.StopBits = LL_LPUART_STOPBITS_1;
|
|
|
|
LPUART_InitStruct.Parity = LL_LPUART_PARITY_NONE;
|
|
|
|
LPUART_InitStruct.TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
|
|
|
LPUART_InitStruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
|
|
|
LL_LPUART_Init(LPUART1, &LPUART_InitStruct);
|
|
|
|
LL_LPUART_EnableFIFO(LPUART1);
|
|
|
|
|
|
|
|
LL_LPUART_Enable(LPUART1);
|
|
|
|
|
2022-02-10 11:20:50 +00:00
|
|
|
while(!LL_LPUART_IsActiveFlag_TEACK(LPUART1) || !LL_LPUART_IsActiveFlag_REACK(LPUART1))
|
2021-11-19 22:19:31 +00:00
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
furi_hal_uart_set_br(FuriHalUartIdLPUART1, baud);
|
|
|
|
|
|
|
|
LL_LPUART_EnableIT_RXNE_RXFNE(LPUART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
NVIC_SetPriority(LPUART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_uart_init(FuriHalUartId ch, uint32_t baud) {
|
2021-11-19 22:19:31 +00:00
|
|
|
if(ch == FuriHalUartIdLPUART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_hal_lpuart_init(baud);
|
2021-11-19 22:19:31 +00:00
|
|
|
else if(ch == FuriHalUartIdUSART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_hal_usart_init(baud);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_uart_set_br(FuriHalUartId ch, uint32_t baud) {
|
2021-11-19 22:19:31 +00:00
|
|
|
if(ch == FuriHalUartIdUSART1) {
|
|
|
|
if(LL_USART_IsEnabled(USART1)) {
|
2021-10-26 18:41:56 +00:00
|
|
|
// Wait for transfer complete flag
|
2021-11-19 22:19:31 +00:00
|
|
|
while(!LL_USART_IsActiveFlag_TC(USART1))
|
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_USART_Disable(USART1);
|
|
|
|
uint32_t uartclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
|
2021-11-19 22:19:31 +00:00
|
|
|
LL_USART_SetBaudRate(
|
|
|
|
USART1, uartclk, LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16, baud);
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_USART_Enable(USART1);
|
|
|
|
}
|
2021-11-19 22:19:31 +00:00
|
|
|
} else if(ch == FuriHalUartIdLPUART1) {
|
|
|
|
if(LL_LPUART_IsEnabled(LPUART1)) {
|
2021-10-26 18:41:56 +00:00
|
|
|
// Wait for transfer complete flag
|
2021-11-19 22:19:31 +00:00
|
|
|
while(!LL_LPUART_IsActiveFlag_TC(LPUART1))
|
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_LPUART_Disable(LPUART1);
|
2021-11-15 20:36:57 +00:00
|
|
|
uint32_t uartclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
|
2021-11-19 22:19:31 +00:00
|
|
|
if(uartclk / baud > 4095) {
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_LPUART_SetPrescaler(LPUART1, LL_LPUART_PRESCALER_DIV32);
|
|
|
|
LL_LPUART_SetBaudRate(LPUART1, uartclk, LL_LPUART_PRESCALER_DIV32, baud);
|
|
|
|
} else {
|
|
|
|
LL_LPUART_SetPrescaler(LPUART1, LL_LPUART_PRESCALER_DIV1);
|
|
|
|
LL_LPUART_SetBaudRate(LPUART1, uartclk, LL_LPUART_PRESCALER_DIV1, baud);
|
|
|
|
}
|
|
|
|
LL_LPUART_Enable(LPUART1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_uart_deinit(FuriHalUartId ch) {
|
2021-11-19 22:19:31 +00:00
|
|
|
furi_hal_uart_set_irq_cb(ch, NULL, NULL);
|
|
|
|
if(ch == FuriHalUartIdUSART1) {
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_USART_Disable(USART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&gpio_usart_tx, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
furi_hal_gpio_init(&gpio_usart_rx, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-11-19 22:19:31 +00:00
|
|
|
} else if(ch == FuriHalUartIdLPUART1) {
|
2021-10-26 18:41:56 +00:00
|
|
|
LL_LPUART_Disable(LPUART1);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&gpio_ext_pc0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
furi_hal_gpio_init(&gpio_ext_pc1, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
void furi_hal_uart_suspend(FuriHalUartId channel) {
|
|
|
|
if(channel == FuriHalUartIdLPUART1 && LL_LPUART_IsEnabled(LPUART1)) {
|
|
|
|
LL_LPUART_Disable(LPUART1);
|
|
|
|
furi_hal_usart_prev_enabled[channel] = true;
|
|
|
|
} else if(channel == FuriHalUartIdUSART1 && LL_USART_IsEnabled(USART1)) {
|
|
|
|
LL_USART_Disable(USART1);
|
|
|
|
furi_hal_usart_prev_enabled[channel] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_uart_resume(FuriHalUartId channel) {
|
|
|
|
if(!furi_hal_usart_prev_enabled[channel]) {
|
|
|
|
return;
|
|
|
|
} else if(channel == FuriHalUartIdLPUART1) {
|
|
|
|
LL_LPUART_Enable(LPUART1);
|
|
|
|
} else if(channel == FuriHalUartIdUSART1) {
|
|
|
|
LL_USART_Enable(USART1);
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_usart_prev_enabled[channel] = false;
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
void furi_hal_uart_tx(FuriHalUartId ch, uint8_t* buffer, size_t buffer_size) {
|
2021-11-19 22:19:31 +00:00
|
|
|
if(ch == FuriHalUartIdUSART1) {
|
|
|
|
if(LL_USART_IsEnabled(USART1) == 0) return;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
while(buffer_size > 0) {
|
2021-11-19 22:19:31 +00:00
|
|
|
while(!LL_USART_IsActiveFlag_TXE(USART1))
|
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
LL_USART_TransmitData8(USART1, *buffer);
|
|
|
|
buffer++;
|
|
|
|
buffer_size--;
|
|
|
|
}
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
} else if(ch == FuriHalUartIdLPUART1) {
|
|
|
|
if(LL_LPUART_IsEnabled(LPUART1) == 0) return;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
while(buffer_size > 0) {
|
2021-11-19 22:19:31 +00:00
|
|
|
while(!LL_LPUART_IsActiveFlag_TXE(LPUART1))
|
|
|
|
;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
LL_LPUART_TransmitData8(LPUART1, *buffer);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
buffer++;
|
|
|
|
buffer_size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
void furi_hal_uart_set_irq_cb(
|
|
|
|
FuriHalUartId ch,
|
|
|
|
void (*cb)(UartIrqEvent ev, uint8_t data, void* ctx),
|
|
|
|
void* ctx) {
|
|
|
|
if(cb == NULL) {
|
|
|
|
if(ch == FuriHalUartIdUSART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
NVIC_DisableIRQ(USART1_IRQn);
|
2021-11-19 22:19:31 +00:00
|
|
|
else if(ch == FuriHalUartIdLPUART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
NVIC_DisableIRQ(LPUART1_IRQn);
|
|
|
|
irq_cb[ch] = cb;
|
2021-11-19 22:19:31 +00:00
|
|
|
irq_ctx[ch] = ctx;
|
2021-10-26 18:41:56 +00:00
|
|
|
} else {
|
2021-11-19 22:19:31 +00:00
|
|
|
irq_ctx[ch] = ctx;
|
2021-10-26 18:41:56 +00:00
|
|
|
irq_cb[ch] = cb;
|
2021-11-19 22:19:31 +00:00
|
|
|
if(ch == FuriHalUartIdUSART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
NVIC_EnableIRQ(USART1_IRQn);
|
2021-11-19 22:19:31 +00:00
|
|
|
else if(ch == FuriHalUartIdLPUART1)
|
2021-10-26 18:41:56 +00:00
|
|
|
NVIC_EnableIRQ(LPUART1_IRQn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LPUART1_IRQHandler(void) {
|
2021-11-19 22:19:31 +00:00
|
|
|
if(LL_LPUART_IsActiveFlag_RXNE_RXFNE(LPUART1)) {
|
2021-10-26 18:41:56 +00:00
|
|
|
uint8_t data = LL_LPUART_ReceiveData8(LPUART1);
|
2021-11-19 22:19:31 +00:00
|
|
|
irq_cb[FuriHalUartIdLPUART1](UartIrqEventRXNE, data, irq_ctx[FuriHalUartIdLPUART1]);
|
|
|
|
} else if(LL_LPUART_IsActiveFlag_ORE(LPUART1)) {
|
2021-11-04 19:33:28 +00:00
|
|
|
LL_LPUART_ClearFlag_ORE(LPUART1);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void USART1_IRQHandler(void) {
|
2021-11-19 22:19:31 +00:00
|
|
|
if(LL_USART_IsActiveFlag_RXNE_RXFNE(USART1)) {
|
2021-10-26 18:41:56 +00:00
|
|
|
uint8_t data = LL_USART_ReceiveData8(USART1);
|
2021-11-19 22:19:31 +00:00
|
|
|
irq_cb[FuriHalUartIdUSART1](UartIrqEventRXNE, data, irq_ctx[FuriHalUartIdUSART1]);
|
|
|
|
} else if(LL_USART_IsActiveFlag_ORE(USART1)) {
|
2021-11-04 19:33:28 +00:00
|
|
|
LL_USART_ClearFlag_ORE(USART1);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
}
|