[FL-1009, FL-1118] Better initialization sequence and V8 hardware support (#406)

* Interrupt manager: add memory barriers.
* ISRs: remove TIM17 dead code.
* API HAL Delay: rename initialization routine and move to API-HAL
* Main: move FURI initialization to the start.
* API HAL GPIO: drop CC1101 shenanigans, COMP inversion for new boards.
* IButton: migrate Cyfral and Metakom to RFID comp routine, make it compatible with new boards.
* RFID: Better keyboard handling and shutdown routines
This commit is contained in:
あく 2021-04-11 16:47:36 +03:00 committed by GitHub
parent 2fe44e1b11
commit 5d08b35b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 75 additions and 80 deletions

View File

@ -137,17 +137,12 @@ void KeyReader::stop_comaparator(void) {
} }
void KeyReader::comparator_trigger_callback(void* hcomp, void* comp_ctx) { void KeyReader::comparator_trigger_callback(void* hcomp, void* comp_ctx) {
COMP_HandleTypeDef* _hcomp = static_cast<COMP_HandleTypeDef*>(hcomp);
KeyReader* _this = static_cast<KeyReader*>(comp_ctx); KeyReader* _this = static_cast<KeyReader*>(comp_ctx);
if(hcomp == &hcomp1) { if(hcomp == &hcomp1) {
_this->cyfral_decoder.process_front( _this->cyfral_decoder.process_front(get_rfid_in_level(), DWT->CYCCNT - last_dwt_value);
(HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_HIGH),
DWT->CYCCNT - last_dwt_value);
_this->metakom_decoder.process_front( _this->metakom_decoder.process_front(get_rfid_in_level(), DWT->CYCCNT - last_dwt_value);
(HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_HIGH),
DWT->CYCCNT - last_dwt_value);
last_dwt_value = DWT->CYCCNT; last_dwt_value = DWT->CYCCNT;
} }

View File

@ -51,10 +51,12 @@ static void render_callback(Canvas* canvas, void* ctx) {
static void input_callback(InputEvent* input_event, void* ctx) { static void input_callback(InputEvent* input_event, void* ctx) {
osMessageQueueId_t event_queue = ctx; osMessageQueueId_t event_queue = ctx;
if(input_event->type != InputTypeShort) return;
AppEvent event; AppEvent event;
event.type = EventTypeKey; event.type = EventTypeKey;
event.value.input = *input_event; event.value.input = *input_event;
osMessageQueuePut(event_queue, &event, 0, 0); osMessageQueuePut(event_queue, &event, 1, osWaitForever);
} }
extern TIM_HandleTypeDef TIM_C; extern TIM_HandleTypeDef TIM_C;
@ -244,7 +246,7 @@ static void extract_data(uint8_t* buf, uint8_t* customer, uint32_t* em_data) {
} }
int32_t lf_rfid_workaround(void* p) { int32_t lf_rfid_workaround(void* p) {
osMessageQueueId_t event_queue = osMessageQueueNew(2, sizeof(AppEvent), NULL); osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(AppEvent), NULL);
// create pin // create pin
GpioPin pull_pin = {.pin = RFID_PULL_Pin, .port = RFID_PULL_GPIO_Port}; GpioPin pull_pin = {.pin = RFID_PULL_Pin, .port = RFID_PULL_GPIO_Port};
@ -337,41 +339,27 @@ int32_t lf_rfid_workaround(void* p) {
if(event_status == osOK) { if(event_status == osOK) {
if(event.type == EventTypeKey) { if(event.type == EventTypeKey) {
// press events // press events
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyBack) {
event.value.input.key == InputKeyBack) { break;
hal_pwmn_stop(&TIM_C, TIM_CHANNEL_1); // TODO: move to furiac_onexit
api_interrupt_remove(
comparator_trigger_callback, InterruptTypeComparatorTrigger);
gpio_init(pull_pin_record, GpioModeInput);
gpio_init((GpioPin*)&ibutton_gpio, GpioModeInput);
// TODO remove all view_ports create by app
view_port_enabled_set(view_port, false);
return 255;
} }
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyUp) {
event.value.input.key == InputKeyUp) {
state->dirty_freq = true; state->dirty_freq = true;
state->freq_khz += 10; state->freq_khz += 10;
} }
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyDown) {
event.value.input.key == InputKeyDown) {
state->dirty_freq = true; state->dirty_freq = true;
state->freq_khz -= 10; state->freq_khz -= 10;
} }
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyLeft) {
event.value.input.key == InputKeyLeft) {
} }
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyRight) {
event.value.input.key == InputKeyRight) {
} }
if(event.value.input.type == InputTypePress && if(event.value.input.key == InputKeyOk) {
event.value.input.key == InputKeyOk) {
state->dirty = true; state->dirty = true;
state->on = !state->on; state->on = !state->on;
} }
@ -412,5 +400,22 @@ int32_t lf_rfid_workaround(void* p) {
} }
} }
hal_pwmn_stop(&TIM_C, TIM_CHANNEL_1); // TODO: move to furiac_onexit
api_interrupt_remove(comparator_trigger_callback, InterruptTypeComparatorTrigger);
gpio_init(pull_pin_record, GpioModeInput);
gpio_init((GpioPin*)&ibutton_gpio, GpioModeInput);
// TODO remove all view_ports create by app
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
HAL_COMP_Stop(&hcomp1);
vStreamBufferDelete(comp_ctx.stream_buffer);
osMessageQueueDelete(event_queue);
return 0; return 0;
} }

View File

@ -36,11 +36,9 @@ void api_interrupt_add(InterruptCallback callback, InterruptType type, void* con
item->callback = callback; item->callback = callback;
item->type = type; item->type = type;
item->context = context; item->context = context;
asm volatile("dmb" : : : "memory");
item->ready = true; item->ready = true;
// TODO remove on app exit
//flapp_on_exit(api_interrupt_remove, callback);
osMutexRelease(interrupt_mutex); osMutexRelease(interrupt_mutex);
} }
} }
@ -77,6 +75,7 @@ void api_interrupt_enable(InterruptCallback callback, InterruptType type) {
// if the iterator is equal to our element // if the iterator is equal to our element
if(it->current->data.callback == callback) { if(it->current->data.callback == callback) {
it->current->data.ready = true; it->current->data.ready = true;
asm volatile("dmb" : : : "memory");
} }
} }
} }
@ -96,6 +95,7 @@ void api_interrupt_disable(InterruptCallback callback, InterruptType type) {
// if the iterator is equal to our element // if the iterator is equal to our element
if(it->current->data.callback == callback) { if(it->current->data.callback == callback) {
it->current->data.ready = false; it->current->data.ready = false;
asm volatile("dmb" : : : "memory");
} }
} }
} }

View File

@ -5,6 +5,9 @@
extern "C" { extern "C" {
#endif #endif
/** Init DWT */
void api_hal_delay_init(void);
/** /**
* Delay in milliseconds * Delay in milliseconds
* @warning Cannot be used from ISR * @warning Cannot be used from ISR
@ -14,9 +17,6 @@ void delay(float milliseconds);
/** Delay in microseconds */ /** Delay in microseconds */
void delay_us(float microseconds); void delay_us(float microseconds);
/** Init DWT */
void delay_us_init_DWT(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -23,11 +23,13 @@
void SystemClock_Config(void); void SystemClock_Config(void);
void MX_FREERTOS_Init(void); void MX_FREERTOS_Init(void);
int main(void) int main(void) {
{ // Initialize FURI layer
furi_init();
// Initialize ST HAL hardware
HAL_Init(); HAL_Init();
SystemClock_Config(); SystemClock_Config();
MX_GPIO_Init(); MX_GPIO_Init();
MX_ADC1_Init(); MX_ADC1_Init();
MX_RTC_Init(); MX_RTC_Init();
@ -46,11 +48,12 @@ int main(void)
MX_AES2_Init(); MX_AES2_Init();
MX_CRC_Init(); MX_CRC_Init();
delay_us_init_DWT(); // Flipper API HAL
api_hal_init(); api_hal_init();
// 3rd party
MX_FATFS_Init(); MX_FATFS_Init();
furi_init();
// CMSIS initialization // CMSIS initialization
osKernelInitialize(); osKernelInitialize();
// Init flipper // Init flipper

View File

@ -5,7 +5,7 @@
static uint32_t clk_per_microsecond; static uint32_t clk_per_microsecond;
void delay_us_init_DWT(void) { void api_hal_delay_init(void) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
DWT->CYCCNT = 0U; DWT->CYCCNT = 0U;

View File

@ -1,4 +1,5 @@
#include <api-hal-gpio.h> #include <api-hal-gpio.h>
#include <api-hal-version.h>
// init GPIO // init GPIO
void hal_gpio_init( void hal_gpio_init(
@ -17,17 +18,12 @@ void hal_gpio_init(
HAL_GPIO_Init(gpio->port, &GPIO_InitStruct); HAL_GPIO_Init(gpio->port, &GPIO_InitStruct);
} }
void enable_cc1101_irq() {
HAL_NVIC_SetPriority(EXTI4_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}
extern COMP_HandleTypeDef hcomp1; extern COMP_HandleTypeDef hcomp1;
bool get_rfid_in_level() { bool get_rfid_in_level() {
#ifdef INVERT_RFID_IN #ifdef INVERT_RFID_IN
return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_LOW); return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_LOW);
#else #else
return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_HIGH); return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_HIGH);
#endif #endif
} }

View File

@ -68,8 +68,6 @@ static inline bool hal_gpio_read(const GpioPin* gpio) {
} }
} }
void enable_cc1101_irq();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

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

View File

@ -23,11 +23,6 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
/* Timer update event */ /* Timer update event */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
api_interrupt_call(InterruptTypeTimerUpdate, htim); api_interrupt_call(InterruptTypeTimerUpdate, htim);
// handle HAL ticks
if(htim->Instance == TIM17) {
HAL_IncTick();
}
} }
/* External interrupt event */ /* External interrupt event */

View File

@ -23,11 +23,13 @@
void SystemClock_Config(void); void SystemClock_Config(void);
void MX_FREERTOS_Init(void); void MX_FREERTOS_Init(void);
int main(void) int main(void) {
{ // Initialize FURI layer
furi_init();
// Initialize ST HAL hardware
HAL_Init(); HAL_Init();
SystemClock_Config(); SystemClock_Config();
MX_GPIO_Init(); MX_GPIO_Init();
MX_ADC1_Init(); MX_ADC1_Init();
MX_RTC_Init(); MX_RTC_Init();
@ -46,11 +48,12 @@ int main(void)
MX_AES2_Init(); MX_AES2_Init();
MX_CRC_Init(); MX_CRC_Init();
delay_us_init_DWT(); // Flipper API HAL
api_hal_init(); api_hal_init();
// 3rd party
MX_FATFS_Init(); MX_FATFS_Init();
furi_init();
// CMSIS initialization // CMSIS initialization
osKernelInitialize(); osKernelInitialize();
// Init flipper // Init flipper

View File

@ -5,7 +5,7 @@
static uint32_t clk_per_microsecond; static uint32_t clk_per_microsecond;
void delay_us_init_DWT(void) { void api_hal_delay_init(void) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
DWT->CYCCNT = 0U; DWT->CYCCNT = 0U;

View File

@ -1,7 +1,5 @@
#include <api-hal-gpio.h> #include <api-hal-gpio.h>
#include <api-hal-spi.h> #include <api-hal-version.h>
#include <api-hal-resources.h>
#include <api-hal-delay.h>
// init GPIO // init GPIO
void hal_gpio_init( void hal_gpio_init(
@ -23,9 +21,16 @@ void hal_gpio_init(
extern COMP_HandleTypeDef hcomp1; extern COMP_HandleTypeDef hcomp1;
bool get_rfid_in_level() { bool get_rfid_in_level() {
#ifdef INVERT_RFID_IN bool value = false;
return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_LOW); if (api_hal_version_get_hw_version() > 7) {
#else value = (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_LOW);
return (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_HIGH); } else {
#endif value = (HAL_COMP_GetOutputLevel(&hcomp1) == COMP_OUTPUT_LEVEL_HIGH);
}
#ifdef INVERT_RFID_IN
return !value;
#else
return value;
#endif
} }

View File

@ -68,8 +68,6 @@ static inline bool hal_gpio_read(const GpioPin* gpio) {
} }
} }
void enable_cc1101_irq();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

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

View File

@ -23,11 +23,6 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
/* Timer update event */ /* Timer update event */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
api_interrupt_call(InterruptTypeTimerUpdate, htim); api_interrupt_call(InterruptTypeTimerUpdate, htim);
// handle HAL ticks
if(htim->Instance == TIM17) {
HAL_IncTick();
}
} }
/* External interrupt event */ /* External interrupt event */