[FL-872] Furi, API-HAL, App-Loader cleanup and improvements (#334)

* Furi: replace obsolete furiac_exit with osThreadExit, drop obsolete apis and test. Rename systemd to flipper and move to separate file, cleanup. ApiHal: Rename timebase to os and move freertos hooks there, move insomnia api to power module.
* Furi: new thread helper
* Furi: cleanup thread documentation
* Flipper, AppLoader: update to use FuriThread. Update tasks signatures to match FuriThreadCallback signature.
* F4: rename API_HAL_TIMEBASE_DEBUG to API_HAL_OS_DEBUG
* Applications: rename FuriApplication to FlipperApplication, use FuriThreadCallback signature for apps.
* C++ app template sample, new exit method
This commit is contained in:
あく
2021-02-12 20:24:34 +03:00
committed by GitHub
parent 7c5de59f53
commit b835d7a451
67 changed files with 906 additions and 1494 deletions

View File

@@ -1,25 +0,0 @@
#include <cmsis_os2.h>
#include <FreeRTOS.h>
#include <task.h>
#include <main.h>
void systemd(void *argument);
osThreadId_t systemdHandle;
const osThreadAttr_t systemd_attributes = {
.name = "systemd",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 1024
};
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) {
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
called if a stack overflow is detected. */
asm("bkpt 1");
while(1);
}
void MX_FREERTOS_Init(void) {
systemdHandle = osThreadNew(systemd, NULL, &systemd_attributes);
}

View File

@@ -1,25 +1,6 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "cmsis_os2.h"
#include "adc.h"
#include "aes.h"
#include "comp.h"
@@ -34,234 +15,141 @@
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "fatfs/fatfs.h"
#include "api-hal.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#include <furi.h>
#include <api-hal.h>
#include <flipper.h>
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
HAL_Init();
SystemClock_Config();
/* USER CODE END 1 */
MX_GPIO_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_RTC_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_USART1_UART_Init();
MX_USB_Device_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM16_Init();
MX_COMP1_Init();
MX_RF_Init();
MX_PKA_Init();
MX_RNG_Init();
MX_AES1_Init();
MX_AES2_Init();
MX_CRC_Init();
/* MCU Configuration--------------------------------------------------------*/
api_hal_init();
MX_FATFS_Init();
delay_us_init_DWT();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
furi_init();
// CMSIS initialization
osKernelInitialize();
// Init flipper
flipper_init();
// Start kernel
osKernelStart();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_RTC_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_USART1_UART_Init();
MX_USB_Device_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM16_Init();
MX_COMP1_Init();
MX_RF_Init();
MX_PKA_Init();
MX_RNG_Init();
MX_AES1_Init();
MX_AES2_Init();
MX_CRC_Init();
/* USER CODE BEGIN 2 */
api_hal_init();
MX_FATFS_Init();
delay_us_init_DWT();
// Errata 2.2.9, Flash OPTVERR flag is always set after system reset
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
/* USER CODE END 2 */
/* Init scheduler */
osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
MX_FREERTOS_Init();
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
while (1) {}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMLOW);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 8;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
|RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
HAL_PWR_EnableBkUpAccess();
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RFWAKEUP
|RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
|RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_CLK48SEL
|RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG
|RCC_PERIPHCLK_ADC;
PeriphClkInitStruct.PLLSAI1.PLLN = 6;
PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK|RCC_PLLSAI1_ADCCLK;
PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_CLK48;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Smps */
/* USER CODE END Smps */
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
/** Enables the Clock Security System
*/
HAL_RCCEx_EnableLSECSS();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMLOW);
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 8;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
|RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RFWAKEUP
|RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
|RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_CLK48SEL
|RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG
|RCC_PERIPHCLK_ADC;
PeriphClkInitStruct.PLLSAI1.PLLN = 6;
PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK|RCC_PLLSAI1_ADCCLK;
PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_CLK48;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
Error_Handler();
}
// Enable CSS for both clocks
HAL_RCC_EnableCSS();
HAL_RCCEx_EnableLSECSS();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
asm("bkpt 1");
/* USER CODE END Error_Handler_Debug */
void Error_Handler(void) {
asm("bkpt 1");
while(1) {}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line) {
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@@ -0,0 +1,95 @@
#pragma once
#include <stm32wbxx_ll_lptim.h>
#include <stdbool.h>
static inline void assert(bool value) {
if (!value) asm("bkpt 1");
}
// Timer used for system ticks
#define API_HAL_OS_TIMER_MAX 0xFFFF
#define API_HAL_OS_TIMER_REG_LOAD_DLY 0x1
#define API_HAL_OS_TIMER LPTIM2
#define API_HAL_OS_TIMER_IRQ LPTIM2_IRQn
#define API_HAL_OS_TIMER_CLOCK_INIT() \
{ \
LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_LSE); \
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPTIM2); \
} \
static inline void api_hal_os_timer_init() {
API_HAL_OS_TIMER_CLOCK_INIT();
LL_LPTIM_Enable(API_HAL_OS_TIMER);
while(!LL_LPTIM_IsEnabled(API_HAL_OS_TIMER)) {}
LL_LPTIM_SetClockSource(API_HAL_OS_TIMER, LL_LPTIM_CLK_SOURCE_INTERNAL);
LL_LPTIM_SetPrescaler(API_HAL_OS_TIMER, LL_LPTIM_PRESCALER_DIV1);
LL_LPTIM_SetPolarity(API_HAL_OS_TIMER, LL_LPTIM_OUTPUT_POLARITY_REGULAR);
LL_LPTIM_SetUpdateMode(API_HAL_OS_TIMER, LL_LPTIM_UPDATE_MODE_IMMEDIATE);
LL_LPTIM_SetCounterMode(API_HAL_OS_TIMER, LL_LPTIM_COUNTER_MODE_INTERNAL);
LL_LPTIM_TrigSw(API_HAL_OS_TIMER);
LL_LPTIM_SetInput1Src(API_HAL_OS_TIMER, LL_LPTIM_INPUT1_SRC_GPIO);
LL_LPTIM_SetInput2Src(API_HAL_OS_TIMER, LL_LPTIM_INPUT2_SRC_GPIO);
NVIC_SetPriority(API_HAL_OS_TIMER_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
NVIC_EnableIRQ(API_HAL_OS_TIMER_IRQ);
}
static inline uint32_t api_hal_os_timer_get_cnt() {
uint32_t counter = LL_LPTIM_GetCounter(API_HAL_OS_TIMER);
uint32_t counter_shadow = LL_LPTIM_GetCounter(API_HAL_OS_TIMER);
while(counter != counter_shadow) {
counter = counter_shadow;
counter_shadow = LL_LPTIM_GetCounter(API_HAL_OS_TIMER);
}
return counter;
}
static inline bool api_hal_os_timer_arr_is_ok() {
return LL_LPTIM_IsActiveFlag_ARROK(API_HAL_OS_TIMER);
}
static inline uint32_t api_hal_os_timer_get_arr() {
return LL_LPTIM_GetAutoReload(API_HAL_OS_TIMER);;
}
static inline void api_hal_os_timer_set_arr(uint32_t value) {
value &= API_HAL_OS_TIMER_MAX;
if (value != api_hal_os_timer_get_arr()) {
assert(api_hal_os_timer_arr_is_ok());
LL_LPTIM_ClearFlag_ARROK(API_HAL_OS_TIMER);
LL_LPTIM_SetAutoReload(API_HAL_OS_TIMER, value);
}
}
static inline bool api_hal_os_timer_cmp_is_ok() {
return LL_LPTIM_IsActiveFlag_CMPOK(API_HAL_OS_TIMER);
}
static inline uint32_t api_hal_os_timer_get_cmp() {
return LL_LPTIM_GetCompare(API_HAL_OS_TIMER);
}
static inline void api_hal_os_timer_set_cmp(uint32_t value) {
value &= API_HAL_OS_TIMER_MAX;
if (value != api_hal_os_timer_get_cmp()) {
assert(api_hal_os_timer_cmp_is_ok());
LL_LPTIM_ClearFlag_CMPOK(API_HAL_OS_TIMER);
LL_LPTIM_SetCompare(API_HAL_OS_TIMER, value);
}
}
static inline bool api_hal_os_timer_is_safe() {
uint16_t cmp = api_hal_os_timer_get_cmp();
uint16_t cnt = api_hal_os_timer_get_cnt();
uint16_t margin = (cmp > cnt) ? cmp - cnt : cnt - cmp;
if (margin < 8) {
return false;
}
if (!api_hal_os_timer_cmp_is_ok()) {
return false;
}
return true;
}

View File

@@ -0,0 +1,169 @@
#include <api-hal-os.h>
#include <api-hal-os-timer.h>
#include <api-hal-power.h>
#include <FreeRTOS.h>
#include <cmsis_os.h>
#define API_HAL_OS_CLK_FREQUENCY 32768
#define API_HAL_OS_TICK_PER_SECOND 1024
#define API_HAL_OS_CLK_PER_TICK (API_HAL_OS_CLK_FREQUENCY / API_HAL_OS_TICK_PER_SECOND)
#define API_HAL_OS_TICK_PER_EPOCH (API_HAL_OS_TIMER_MAX / API_HAL_OS_CLK_PER_TICK)
#define API_HAL_OS_MAX_SLEEP (API_HAL_OS_TICK_PER_EPOCH - 1)
#ifdef API_HAL_OS_DEBUG
#include <stm32wbxx_ll_gpio.h>
#define LED_GREEN_PORT GPIOA
#define LED_GREEN_PIN LL_GPIO_PIN_2
#endif
typedef struct {
// Tick counters
volatile uint32_t in_sleep;
volatile uint32_t in_awake;
// Error counters
volatile uint32_t sleep_error;
volatile uint32_t awake_error;
} ApiHalOs;
ApiHalOs api_hal_os = {
.in_sleep = 0,
.in_awake = 0,
.sleep_error = 0,
.awake_error = 0,
};
void api_hal_os_init() {
api_hal_os_timer_init();
LL_DBGMCU_APB1_GRP2_FreezePeriph(LL_DBGMCU_APB1_GRP2_LPTIM2_STOP);
LL_LPTIM_EnableIT_CMPM(API_HAL_OS_TIMER);
LL_LPTIM_EnableIT_ARRM(API_HAL_OS_TIMER);
LL_LPTIM_SetAutoReload(API_HAL_OS_TIMER, API_HAL_OS_TIMER_MAX);
LL_LPTIM_SetCompare(API_HAL_OS_TIMER, API_HAL_OS_CLK_PER_TICK);
LL_LPTIM_StartCounter(API_HAL_OS_TIMER, LL_LPTIM_OPERATING_MODE_CONTINUOUS);
}
void LPTIM2_IRQHandler(void) {
// Autoreload
const bool arrm_flag = LL_LPTIM_IsActiveFlag_ARRM(API_HAL_OS_TIMER);
if(arrm_flag) {
LL_LPTIM_ClearFLAG_ARRM(API_HAL_OS_TIMER);
}
if(LL_LPTIM_IsActiveFlag_CMPM(API_HAL_OS_TIMER)) {
LL_LPTIM_ClearFLAG_CMPM(API_HAL_OS_TIMER);
// Store important value
uint16_t cnt = api_hal_os_timer_get_cnt();
uint16_t cmp = api_hal_os_timer_get_cmp();
uint16_t current_tick = cnt / API_HAL_OS_CLK_PER_TICK;
uint16_t compare_tick = cmp / API_HAL_OS_CLK_PER_TICK;
// Calculate error
// happens when HAL or other high priority IRQ takes our time
int32_t error = (int32_t)compare_tick - current_tick;
api_hal_os.awake_error += ((error>0) ? error : -error);
// Calculate and set next tick
uint16_t next_tick = current_tick + 1;
api_hal_os_timer_set_cmp(next_tick * API_HAL_OS_CLK_PER_TICK);
// Notify OS
api_hal_os.in_awake ++;
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
xPortSysTickHandler();
}
}
}
static inline uint32_t api_hal_os_sleep(TickType_t expected_idle_ticks) {
// Store important value before going to sleep
const uint16_t before_cnt = api_hal_os_timer_get_cnt();
const uint16_t before_tick = before_cnt / API_HAL_OS_CLK_PER_TICK;
// Calculate and set next wakeup compare value
const uint16_t expected_cnt = (before_tick + expected_idle_ticks - 2) * API_HAL_OS_CLK_PER_TICK;
api_hal_os_timer_set_cmp(expected_cnt);
HAL_SuspendTick();
// Go to stop2 mode
#ifdef API_HAL_OS_DEBUG
LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
#endif
api_hal_power_deep_sleep();
#ifdef API_HAL_OS_DEBUG
LL_GPIO_ResetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
#endif
HAL_ResumeTick();
// Spin till we are in timer safe zone
while(!api_hal_os_timer_is_safe()) {}
// Store current counter value, calculate current tick
const uint16_t after_cnt = api_hal_os_timer_get_cnt();
const uint16_t after_tick = after_cnt / API_HAL_OS_CLK_PER_TICK;
// Store and clear interrupt flags
// we don't want handler to be called after renabling IRQ
bool arrm_flag = LL_LPTIM_IsActiveFlag_ARRM(API_HAL_OS_TIMER);
// Calculate and set next wakeup compare value
const uint16_t next_cmp = (after_tick + 1) * API_HAL_OS_CLK_PER_TICK;
api_hal_os_timer_set_cmp(next_cmp);
// Calculate ticks count spent in sleep and perform sanity checks
int32_t completed_ticks = arrm_flag ? (int32_t)before_tick - after_tick : (int32_t)after_tick - before_tick;
return completed_ticks;
}
void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
if (!api_hal_power_deep_available()) {
return;
}
// Limit mount of ticks to maximum that timer can count
if (expected_idle_ticks > API_HAL_OS_MAX_SLEEP) {
expected_idle_ticks = API_HAL_OS_MAX_SLEEP;
}
// Stop IRQ handling, no one should disturb us till we finish
__disable_irq();
// Confirm OS that sleep is still possible
// And check if timer is in safe zone
// (8 clocks till any IRQ event or ongoing synchronization)
if (eTaskConfirmSleepModeStatus() == eAbortSleep
|| !api_hal_os_timer_is_safe()) {
__enable_irq();
return;
}
uint32_t completed_ticks = api_hal_os_sleep(expected_idle_ticks);
assert(completed_ticks >= 0);
// Reenable IRQ
__enable_irq();
// Notify system about time spent in sleep
if (completed_ticks > 0) {
api_hal_os.in_sleep += completed_ticks;
if (completed_ticks > expected_idle_ticks) {
// We are late, count error
api_hal_os.sleep_error += (completed_ticks - expected_idle_ticks);
// Freertos is not happy when we overleep
// But we are not going to tell her
vTaskStepTick(expected_idle_ticks);
} else {
vTaskStepTick(completed_ticks);
}
}
}
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) {
asm("bkpt 1");
while(1) {};
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Initialize OS helpers
* Configure and start tick timer
*/
void api_hal_os_init();
#ifdef __cplusplus
}
#endif

View File

@@ -12,6 +12,8 @@
#include <bq27220.h>
#include <bq25896.h>
volatile uint32_t api_hal_power_insomnia = 0;
void HAL_RCC_CSSCallback(void) {
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
@@ -24,8 +26,20 @@ void api_hal_power_init() {
bq25896_init();
}
uint16_t api_hal_power_insomnia_level() {
return api_hal_power_insomnia;
}
void api_hal_power_insomnia_enter() {
api_hal_power_insomnia++;
}
void api_hal_power_insomnia_exit() {
api_hal_power_insomnia--;
}
bool api_hal_power_deep_available() {
return api_hal_bt_is_alive();
return api_hal_bt_is_alive() && api_hal_power_insomnia == 0;
}
void api_hal_power_deep_sleep() {

View File

@@ -1,95 +0,0 @@
#pragma once
#include <stm32wbxx_ll_lptim.h>
#include <stdbool.h>
static inline void assert(bool value) {
if (!value) asm("bkpt 1");
}
// Timer used for system ticks
#define API_HAL_TIMEBASE_TIMER_MAX 0xFFFF
#define API_HAL_TIMEBASE_TIMER_REG_LOAD_DLY 0x1
#define API_HAL_TIMEBASE_TIMER LPTIM2
#define API_HAL_TIMEBASE_TIMER_IRQ LPTIM2_IRQn
#define API_HAL_TIMEBASE_TIMER_CLOCK_INIT() \
{ \
LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_LSE); \
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPTIM2); \
} \
static inline void api_hal_timebase_timer_init() {
API_HAL_TIMEBASE_TIMER_CLOCK_INIT();
LL_LPTIM_Enable(API_HAL_TIMEBASE_TIMER);
while(!LL_LPTIM_IsEnabled(API_HAL_TIMEBASE_TIMER)) {}
LL_LPTIM_SetClockSource(API_HAL_TIMEBASE_TIMER, LL_LPTIM_CLK_SOURCE_INTERNAL);
LL_LPTIM_SetPrescaler(API_HAL_TIMEBASE_TIMER, LL_LPTIM_PRESCALER_DIV1);
LL_LPTIM_SetPolarity(API_HAL_TIMEBASE_TIMER, LL_LPTIM_OUTPUT_POLARITY_REGULAR);
LL_LPTIM_SetUpdateMode(API_HAL_TIMEBASE_TIMER, LL_LPTIM_UPDATE_MODE_IMMEDIATE);
LL_LPTIM_SetCounterMode(API_HAL_TIMEBASE_TIMER, LL_LPTIM_COUNTER_MODE_INTERNAL);
LL_LPTIM_TrigSw(API_HAL_TIMEBASE_TIMER);
LL_LPTIM_SetInput1Src(API_HAL_TIMEBASE_TIMER, LL_LPTIM_INPUT1_SRC_GPIO);
LL_LPTIM_SetInput2Src(API_HAL_TIMEBASE_TIMER, LL_LPTIM_INPUT2_SRC_GPIO);
NVIC_SetPriority(API_HAL_TIMEBASE_TIMER_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
NVIC_EnableIRQ(API_HAL_TIMEBASE_TIMER_IRQ);
}
static inline uint32_t api_hal_timebase_timer_get_cnt() {
uint32_t counter = LL_LPTIM_GetCounter(API_HAL_TIMEBASE_TIMER);
uint32_t counter_shadow = LL_LPTIM_GetCounter(API_HAL_TIMEBASE_TIMER);
while(counter != counter_shadow) {
counter = counter_shadow;
counter_shadow = LL_LPTIM_GetCounter(API_HAL_TIMEBASE_TIMER);
}
return counter;
}
static inline bool api_hal_timebase_timer_arr_is_ok() {
return LL_LPTIM_IsActiveFlag_ARROK(API_HAL_TIMEBASE_TIMER);
}
static inline uint32_t api_hal_timebase_timer_get_arr() {
return LL_LPTIM_GetAutoReload(API_HAL_TIMEBASE_TIMER);;
}
static inline void api_hal_timebase_timer_set_arr(uint32_t value) {
value &= API_HAL_TIMEBASE_TIMER_MAX;
if (value != api_hal_timebase_timer_get_arr()) {
assert(api_hal_timebase_timer_arr_is_ok());
LL_LPTIM_ClearFlag_ARROK(API_HAL_TIMEBASE_TIMER);
LL_LPTIM_SetAutoReload(API_HAL_TIMEBASE_TIMER, value);
}
}
static inline bool api_hal_timebase_timer_cmp_is_ok() {
return LL_LPTIM_IsActiveFlag_CMPOK(API_HAL_TIMEBASE_TIMER);
}
static inline uint32_t api_hal_timebase_timer_get_cmp() {
return LL_LPTIM_GetCompare(API_HAL_TIMEBASE_TIMER);
}
static inline void api_hal_timebase_timer_set_cmp(uint32_t value) {
value &= API_HAL_TIMEBASE_TIMER_MAX;
if (value != api_hal_timebase_timer_get_cmp()) {
assert(api_hal_timebase_timer_cmp_is_ok());
LL_LPTIM_ClearFlag_CMPOK(API_HAL_TIMEBASE_TIMER);
LL_LPTIM_SetCompare(API_HAL_TIMEBASE_TIMER, value);
}
}
static inline bool api_hal_timebase_timer_is_safe() {
uint16_t cmp = api_hal_timebase_timer_get_cmp();
uint16_t cnt = api_hal_timebase_timer_get_cnt();
uint16_t margin = (cmp > cnt) ? cmp - cnt : cnt - cmp;
if (margin < 8) {
return false;
}
if (!api_hal_timebase_timer_cmp_is_ok()) {
return false;
}
return true;
}

View File

@@ -1,179 +0,0 @@
#include <api-hal-timebase.h>
#include <api-hal-timebase-timer.h>
#include <api-hal-power.h>
#include <FreeRTOS.h>
#include <cmsis_os.h>
#define API_HAL_TIMEBASE_CLK_FREQUENCY 32768
#define API_HAL_TIMEBASE_TICK_PER_SECOND 1024
#define API_HAL_TIMEBASE_CLK_PER_TICK (API_HAL_TIMEBASE_CLK_FREQUENCY / API_HAL_TIMEBASE_TICK_PER_SECOND)
#define API_HAL_TIMEBASE_TICK_PER_EPOCH (API_HAL_TIMEBASE_TIMER_MAX / API_HAL_TIMEBASE_CLK_PER_TICK)
#define API_HAL_TIMEBASE_MAX_SLEEP (API_HAL_TIMEBASE_TICK_PER_EPOCH - 1)
#ifdef API_HAL_TIMEBASE_DEBUG
#include <stm32wbxx_ll_gpio.h>
#define LED_GREEN_PORT GPIOA
#define LED_GREEN_PIN LL_GPIO_PIN_2
#endif
typedef struct {
// Sleep control
volatile uint16_t insomnia;
// Tick counters
volatile uint32_t in_sleep;
volatile uint32_t in_awake;
// Error counters
volatile uint32_t sleep_error;
volatile uint32_t awake_error;
} ApiHalTimbase;
ApiHalTimbase api_hal_timebase = {
.insomnia = 0,
.in_sleep = 0,
.in_awake = 0,
.sleep_error = 0,
.awake_error = 0,
};
void api_hal_timebase_init() {
api_hal_timebase_timer_init();
LL_DBGMCU_APB1_GRP2_FreezePeriph(LL_DBGMCU_APB1_GRP2_LPTIM2_STOP);
LL_LPTIM_EnableIT_CMPM(API_HAL_TIMEBASE_TIMER);
LL_LPTIM_EnableIT_ARRM(API_HAL_TIMEBASE_TIMER);
LL_LPTIM_SetAutoReload(API_HAL_TIMEBASE_TIMER, API_HAL_TIMEBASE_TIMER_MAX);
LL_LPTIM_SetCompare(API_HAL_TIMEBASE_TIMER, API_HAL_TIMEBASE_CLK_PER_TICK);
LL_LPTIM_StartCounter(API_HAL_TIMEBASE_TIMER, LL_LPTIM_OPERATING_MODE_CONTINUOUS);
}
uint16_t api_hal_timebase_insomnia_level() {
return api_hal_timebase.insomnia;
}
void api_hal_timebase_insomnia_enter() {
api_hal_timebase.insomnia++;
}
void api_hal_timebase_insomnia_exit() {
api_hal_timebase.insomnia--;
}
void LPTIM2_IRQHandler(void) {
// Autoreload
const bool arrm_flag = LL_LPTIM_IsActiveFlag_ARRM(API_HAL_TIMEBASE_TIMER);
if(arrm_flag) {
LL_LPTIM_ClearFLAG_ARRM(API_HAL_TIMEBASE_TIMER);
}
if(LL_LPTIM_IsActiveFlag_CMPM(API_HAL_TIMEBASE_TIMER)) {
LL_LPTIM_ClearFLAG_CMPM(API_HAL_TIMEBASE_TIMER);
// Store important value
uint16_t cnt = api_hal_timebase_timer_get_cnt();
uint16_t cmp = api_hal_timebase_timer_get_cmp();
uint16_t current_tick = cnt / API_HAL_TIMEBASE_CLK_PER_TICK;
uint16_t compare_tick = cmp / API_HAL_TIMEBASE_CLK_PER_TICK;
// Calculate error
// happens when HAL or other high priority IRQ takes our time
int32_t error = (int32_t)compare_tick - current_tick;
api_hal_timebase.awake_error += ((error>0) ? error : -error);
// Calculate and set next tick
uint16_t next_tick = current_tick + 1;
api_hal_timebase_timer_set_cmp(next_tick * API_HAL_TIMEBASE_CLK_PER_TICK);
// Notify OS
api_hal_timebase.in_awake ++;
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
xPortSysTickHandler();
}
}
}
static inline uint32_t api_hal_timebase_sleep(TickType_t expected_idle_ticks) {
// Store important value before going to sleep
const uint16_t before_cnt = api_hal_timebase_timer_get_cnt();
const uint16_t before_tick = before_cnt / API_HAL_TIMEBASE_CLK_PER_TICK;
// Calculate and set next wakeup compare value
const uint16_t expected_cnt = (before_tick + expected_idle_ticks - 2) * API_HAL_TIMEBASE_CLK_PER_TICK;
api_hal_timebase_timer_set_cmp(expected_cnt);
HAL_SuspendTick();
// Go to stop2 mode
#ifdef API_HAL_TIMEBASE_DEBUG
LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
#endif
api_hal_power_deep_sleep();
#ifdef API_HAL_TIMEBASE_DEBUG
LL_GPIO_ResetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
#endif
HAL_ResumeTick();
// Spin till we are in timer safe zone
while(!api_hal_timebase_timer_is_safe()) {}
// Store current counter value, calculate current tick
const uint16_t after_cnt = api_hal_timebase_timer_get_cnt();
const uint16_t after_tick = after_cnt / API_HAL_TIMEBASE_CLK_PER_TICK;
// Store and clear interrupt flags
// we don't want handler to be called after renabling IRQ
bool arrm_flag = LL_LPTIM_IsActiveFlag_ARRM(API_HAL_TIMEBASE_TIMER);
// Calculate and set next wakeup compare value
const uint16_t next_cmp = (after_tick + 1) * API_HAL_TIMEBASE_CLK_PER_TICK;
api_hal_timebase_timer_set_cmp(next_cmp);
// Calculate ticks count spent in sleep and perform sanity checks
int32_t completed_ticks = arrm_flag ? (int32_t)before_tick - after_tick : (int32_t)after_tick - before_tick;
return completed_ticks;
}
void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
if (!api_hal_power_deep_available() || api_hal_timebase.insomnia) {
return;
}
// Limit mount of ticks to maximum that timer can count
if (expected_idle_ticks > API_HAL_TIMEBASE_MAX_SLEEP) {
expected_idle_ticks = API_HAL_TIMEBASE_MAX_SLEEP;
}
// Stop IRQ handling, no one should disturb us till we finish
__disable_irq();
// Confirm OS that sleep is still possible
// And check if timer is in safe zone
// (8 clocks till any IRQ event or ongoing synchronization)
if (eTaskConfirmSleepModeStatus() == eAbortSleep
|| !api_hal_timebase_timer_is_safe()) {
__enable_irq();
return;
}
uint32_t completed_ticks = api_hal_timebase_sleep(expected_idle_ticks);
assert(completed_ticks >= 0);
// Reenable IRQ
__enable_irq();
// Notify system about time spent in sleep
if (completed_ticks > 0) {
api_hal_timebase.in_sleep += completed_ticks;
if (completed_ticks > expected_idle_ticks) {
// We are late, count error
api_hal_timebase.sleep_error += (completed_ticks - expected_idle_ticks);
// Freertos is not happy when we overleep
// But we are not going to tell her
vTaskStepTick(expected_idle_ticks);
} else {
vTaskStepTick(completed_ticks);
}
}
}

View File

@@ -1,37 +0,0 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Initialize timebase
* Configure and start tick timer
*/
void api_hal_timebase_init();
/* Get current insomnia level
* @return insomnia level: 0 - no insomnia, >0 - insomnia, bearer count.
*/
uint16_t api_hal_timebase_insomnia_level();
/* Enter insomnia mode
* Prevents device from going to sleep
* @warning Internally increases insomnia level
* Must be paired with api_hal_timebase_insomnia_exit
*/
void api_hal_timebase_insomnia_enter();
/* Exit insomnia mode
* Allow device to go to sleep
* @warning Internally decreases insomnia level.
* Must be paired with api_hal_timebase_insomnia_enter
*/
void api_hal_timebase_insomnia_exit();
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,7 @@
#include <api-hal.h>
void api_hal_init() {
api_hal_timebase_init();
api_hal_os_init();
api_hal_vcp_init();
api_hal_spi_init();
}

View File

@@ -219,7 +219,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
}
/* restart advertising */
Adv_Request(APP_BLE_FAST_ADV);
api_hal_timebase_insomnia_exit();
api_hal_power_insomnia_exit();
}
break; /* EVT_DISCONN_COMPLETE */
@@ -268,7 +268,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
break;
case EVT_LE_CONN_COMPLETE:
{
api_hal_timebase_insomnia_enter();
api_hal_power_insomnia_enter();
hci_le_connection_complete_event_rp0 *connection_complete_event;
/**

View File

@@ -52,7 +52,7 @@ void APPE_Init() {
HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
// APPD_Init();
api_hal_timebase_insomnia_enter();
api_hal_power_insomnia_enter();
appe_Tl_Init(); /* Initialize all transport layers */
@@ -144,7 +144,7 @@ static void APPE_SysUserEvtRx( void * pPayload ) {
} else {
ble_glue_status = BleGlueStatusBroken;
}
api_hal_timebase_insomnia_exit();
api_hal_power_insomnia_exit();
}
/*************************************************************

View File

@@ -14,9 +14,9 @@ FLASH_ADDRESS = 0x08000000
CFLAGS += -DNO_BOOTLOADER
endif
API_HAL_TIMEBASE_DEBUG ?= 0
ifeq ($(API_HAL_TIMEBASE_DEBUG), 1)
CFLAGS += -DAPI_HAL_TIMEBASE_DEBUG
API_HAL_OS_DEBUG ?= 0
ifeq ($(API_HAL_OS_DEBUG), 1)
CFLAGS += -DAPI_HAL_OS_DEBUG
endif
OPENOCD_OPTS = -f interface/stlink.cfg -c "transport select hla_swd" -f ../debug/stm32wbx.cfg -c "stm32wbx.cpu configure -rtos auto" -c "init"