[FL-1558] Technical Debt: console, bootloader, targets, stdglue, etc... (#590)
* ApiHal: console abstraction, change uart baud rate to 230400. Bootloader: drop F5. Furi: prevent thread local output to serial console in stdglue. * ApiHal: take control over system initialization, reorder some subsystems in a hope of fixing USB issues. * Main: cleanup dead code. ISR: take care of HSECSS by our self. USB: leave clock configuration alone. * F6 cube: switch RCC to LL. * Cli: rollback return behavior.
This commit is contained in:
@@ -1,14 +1,112 @@
|
||||
#include <api-hal-clock.h>
|
||||
|
||||
#include <main.h>
|
||||
#include <stm32wbxx_ll_pwr.h>
|
||||
#include <stm32wbxx_ll_rcc.h>
|
||||
#include <stm32wbxx_ll_utils.h>
|
||||
|
||||
void api_hal_clock_init() {
|
||||
// AHB
|
||||
LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
|
||||
while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_3);
|
||||
|
||||
/* HSE configuration and activation */
|
||||
LL_RCC_HSE_SetCapacitorTuning(0x18);
|
||||
LL_RCC_HSE_Enable();
|
||||
while(LL_RCC_HSE_IsReady() != 1) ;
|
||||
|
||||
/* HSI configuration and activation */
|
||||
LL_RCC_HSI_Enable();
|
||||
while(LL_RCC_HSI_IsReady() != 1)
|
||||
|
||||
/* LSE configuration and activation */
|
||||
LL_PWR_EnableBkUpAccess();
|
||||
LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_MEDIUMLOW);
|
||||
LL_RCC_LSE_Enable();
|
||||
while(LL_RCC_LSE_IsReady() != 1) ;
|
||||
|
||||
LL_RCC_HSE_EnableCSS();
|
||||
LL_RCC_EnableIT_LSECSS();
|
||||
LL_RCC_LSE_EnableCSS();
|
||||
|
||||
/* Main PLL configuration and activation */
|
||||
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 8, LL_RCC_PLLR_DIV_2);
|
||||
LL_RCC_PLL_Enable();
|
||||
LL_RCC_PLL_EnableDomain_SYS();
|
||||
while(LL_RCC_PLL_IsReady() != 1);
|
||||
|
||||
LL_RCC_PLLSAI1_ConfigDomain_48M(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 6, LL_RCC_PLLSAI1Q_DIV_2);
|
||||
LL_RCC_PLLSAI1_ConfigDomain_ADC(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 6, LL_RCC_PLLSAI1R_DIV_2);
|
||||
LL_RCC_PLLSAI1_Enable();
|
||||
LL_RCC_PLLSAI1_EnableDomain_48M();
|
||||
LL_RCC_PLLSAI1_EnableDomain_ADC();
|
||||
while(LL_RCC_PLLSAI1_IsReady() != 1);
|
||||
|
||||
/* Sysclk activation on the main PLL */
|
||||
/* Set CPU1 prescaler*/
|
||||
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
|
||||
|
||||
/* Set CPU2 prescaler*/
|
||||
LL_C2_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
|
||||
|
||||
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
|
||||
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL);
|
||||
|
||||
/* Set AHB SHARED prescaler*/
|
||||
LL_RCC_SetAHB4Prescaler(LL_RCC_SYSCLK_DIV_1);
|
||||
|
||||
/* Set APB1 prescaler*/
|
||||
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
|
||||
|
||||
/* Set APB2 prescaler*/
|
||||
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
|
||||
|
||||
/* Disable MSI */
|
||||
LL_RCC_MSI_Disable();
|
||||
while(LL_RCC_MSI_IsReady() != 0);
|
||||
|
||||
/* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
|
||||
LL_SetSystemCoreClock(64000000);
|
||||
|
||||
/* Update the time base */
|
||||
if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
|
||||
LL_RCC_ForceBackupDomainReset();
|
||||
LL_RCC_ReleaseBackupDomainReset();
|
||||
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
|
||||
}
|
||||
|
||||
LL_RCC_EnableRTC();
|
||||
|
||||
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
|
||||
LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSOURCE_PLLSAI1);
|
||||
LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1);
|
||||
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_CLK48);
|
||||
LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLLSAI1);
|
||||
LL_RCC_SetSMPSClockSource(LL_RCC_SMPS_CLKSOURCE_HSE);
|
||||
LL_RCC_SetSMPSPrescaler(LL_RCC_SMPS_DIV_1);
|
||||
LL_RCC_SetRFWKPClockSource(LL_RCC_RFWKP_CLKSOURCE_LSE);
|
||||
|
||||
// AHB1
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
|
||||
|
||||
// APB
|
||||
// AHB2
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOD);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOE);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOH);
|
||||
|
||||
// APB1
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
||||
|
||||
// APB2
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
|
||||
}
|
||||
|
||||
void api_hal_clock_switch_to_hsi() {
|
||||
|
54
firmware/targets/f6/api-hal/api-hal-console.c
Normal file
54
firmware/targets/f6/api-hal/api-hal-console.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <api-hal-console.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
#include <stm32wbxx_ll_usart.h>
|
||||
|
||||
volatile bool api_hal_console_alive = false;
|
||||
|
||||
void api_hal_console_init() {
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
LL_USART_InitTypeDef USART_InitStruct = {0};
|
||||
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
|
||||
USART_InitStruct.BaudRate = 230400;
|
||||
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;
|
||||
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
||||
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
||||
LL_USART_Init(USART1, &USART_InitStruct);
|
||||
LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_2);
|
||||
LL_USART_EnableFIFO(USART1);
|
||||
LL_USART_ConfigAsyncMode(USART1);
|
||||
|
||||
LL_USART_Enable(USART1);
|
||||
|
||||
while(!LL_USART_IsActiveFlag_TEACK(USART1)) ;
|
||||
api_hal_console_alive = true;
|
||||
}
|
||||
|
||||
void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
if (!api_hal_console_alive)
|
||||
return;
|
||||
|
||||
while(buffer_size > 0) {
|
||||
while (!LL_USART_IsActiveFlag_TXE(USART1));
|
||||
|
||||
LL_USART_TransmitData8(USART1, *buffer);
|
||||
|
||||
buffer++;
|
||||
buffer_size--;
|
||||
}
|
||||
|
||||
/* Wait for TC flag to be raised for last char */
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
}
|
16
firmware/targets/f6/api-hal/api-hal-console.h
Normal file
16
firmware/targets/f6/api-hal/api-hal-console.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void api_hal_console_init();
|
||||
|
||||
void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -66,6 +66,7 @@ void HAL_RCC_CSSCallback(void) {
|
||||
}
|
||||
|
||||
void api_hal_power_init() {
|
||||
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
|
||||
LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
|
||||
bq27220_init(&cedv);
|
||||
bq25896_init();
|
||||
|
@@ -52,3 +52,5 @@ const GpioPin gpio_rfid_data_in = {.port = RFID_RF_IN_GPIO_Port, .pin = RFID_RF_
|
||||
const GpioPin gpio_irda_rx = {.port = IR_RX_GPIO_Port, .pin = IR_RX_Pin};
|
||||
const GpioPin gpio_irda_tx = {.port = IR_TX_GPIO_Port, .pin = IR_TX_Pin};
|
||||
|
||||
const GpioPin gpio_usart_tx = {.port = USART1_TX_Port, .pin = USART1_TX_Pin};
|
||||
const GpioPin gpio_usart_rx = {.port = USART1_RX_Port, .pin = USART1_RX_Pin};
|
||||
|
@@ -89,6 +89,9 @@ extern const GpioPin gpio_rfid_data_in;
|
||||
extern const GpioPin gpio_irda_rx;
|
||||
extern const GpioPin gpio_irda_tx;
|
||||
|
||||
extern const GpioPin gpio_usart_tx;
|
||||
extern const GpioPin gpio_usart_rx;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -1,24 +1,78 @@
|
||||
#include <api-hal.h>
|
||||
|
||||
#include <adc.h>
|
||||
#include <aes.h>
|
||||
#include <comp.h>
|
||||
#include <crc.h>
|
||||
#include <pka.h>
|
||||
#include <rf.h>
|
||||
#include <rng.h>
|
||||
#include <rtc.h>
|
||||
#include <spi.h>
|
||||
#include <tim.h>
|
||||
#include <usb_device.h>
|
||||
#include <gpio.h>
|
||||
|
||||
void api_hal_init() {
|
||||
api_hal_boot_init();
|
||||
FURI_LOG_I("FURI_HAL", "BOOT OK");
|
||||
api_hal_clock_init();
|
||||
api_hal_console_init();
|
||||
FURI_LOG_I("FURI_HAL", "CLOCK and CONSOLE OK");
|
||||
api_hal_interrupt_init();
|
||||
FURI_LOG_I("FURI_HAL", "INTERRUPT OK");
|
||||
api_hal_clock_init();
|
||||
FURI_LOG_I("FURI_HAL", "CLOCK OK");
|
||||
api_hal_version_init();
|
||||
FURI_LOG_I("FURI_HAL", "VERSION OK");
|
||||
api_hal_delay_init();
|
||||
FURI_LOG_I("FURI_HAL", "DELAY OK");
|
||||
api_hal_os_init();
|
||||
FURI_LOG_I("FURI_HAL", "OS OK");
|
||||
api_hal_vcp_init();
|
||||
FURI_LOG_I("FURI_HAL", "VCP OK");
|
||||
|
||||
MX_GPIO_Init();
|
||||
FURI_LOG_I("HAL", "GPIO OK");
|
||||
|
||||
MX_RTC_Init();
|
||||
FURI_LOG_I("HAL", "RTC OK");
|
||||
api_hal_boot_init();
|
||||
FURI_LOG_I("FURI_HAL", "BOOT OK");
|
||||
api_hal_version_init();
|
||||
FURI_LOG_I("FURI_HAL", "VERSION OK");
|
||||
|
||||
MX_ADC1_Init();
|
||||
FURI_LOG_I("HAL", "ADC1 OK");
|
||||
|
||||
MX_SPI1_Init();
|
||||
FURI_LOG_I("HAL", "SPI1 OK");
|
||||
MX_SPI2_Init();
|
||||
FURI_LOG_I("HAL", "SPI2 OK");
|
||||
api_hal_spi_init();
|
||||
FURI_LOG_I("FURI_HAL", "SPI OK");
|
||||
|
||||
MX_TIM1_Init();
|
||||
FURI_LOG_I("HAL", "TIM1 OK");
|
||||
MX_TIM2_Init();
|
||||
FURI_LOG_I("HAL", "TIM2 OK");
|
||||
MX_TIM16_Init();
|
||||
FURI_LOG_I("HAL", "TIM16 OK");
|
||||
MX_COMP1_Init();
|
||||
FURI_LOG_I("HAL", "COMP1 OK");
|
||||
MX_RF_Init();
|
||||
FURI_LOG_I("HAL", "RF OK");
|
||||
MX_PKA_Init();
|
||||
FURI_LOG_I("HAL", "PKA OK");
|
||||
MX_RNG_Init();
|
||||
FURI_LOG_I("HAL", "RNG OK");
|
||||
MX_AES1_Init();
|
||||
FURI_LOG_I("HAL", "AES1 OK");
|
||||
MX_AES2_Init();
|
||||
FURI_LOG_I("HAL", "AES2 OK");
|
||||
MX_CRC_Init();
|
||||
FURI_LOG_I("HAL", "CRC OK");
|
||||
|
||||
// VCP + USB
|
||||
api_hal_vcp_init();
|
||||
FURI_LOG_I("FURI_HAL", "VCP OK");
|
||||
MX_USB_Device_Init();
|
||||
FURI_LOG_I("HAL", "USB OK");
|
||||
|
||||
api_hal_i2c_init();
|
||||
FURI_LOG_I("FURI_HAL", "I2C OK");
|
||||
|
||||
// High Level
|
||||
api_hal_power_init();
|
||||
FURI_LOG_I("FURI_HAL", "POWER OK");
|
||||
api_hal_light_init();
|
||||
@@ -27,4 +81,8 @@ void api_hal_init() {
|
||||
FURI_LOG_I("FURI_HAL", "VIBRO OK");
|
||||
api_hal_subghz_init();
|
||||
FURI_LOG_I("FURI_HAL", "SUBGHZ OK");
|
||||
|
||||
// FreeRTOS glue
|
||||
api_hal_os_init();
|
||||
FURI_LOG_I("FURI_HAL", "OS OK");
|
||||
}
|
||||
|
Reference in New Issue
Block a user