Сохранить текущие изменения NAND и LED перед объединением веток
This commit is contained in:
65
c/led-indicator/ports/stm32-hal/led_indicator_stm32_hal.c
Normal file
65
c/led-indicator/ports/stm32-hal/led_indicator_stm32_hal.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "led_indicator_stm32_hal.h"
|
||||
|
||||
static void stm32_write(void *context, uint8_t channel, uint8_t on)
|
||||
{
|
||||
LedIndicator_Stm32HalPort *adapter = (LedIndicator_Stm32HalPort *)context;
|
||||
const LedIndicator_Stm32HalOutput *output;
|
||||
GPIO_PinState state;
|
||||
|
||||
if ((adapter == NULL) || ((size_t)channel >= adapter->output_count)) {
|
||||
return;
|
||||
}
|
||||
output = &adapter->outputs[channel];
|
||||
state = ((on != 0U) ^ (output->active_low != 0U)) ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
||||
HAL_GPIO_WritePin(output->gpio, output->pin, state);
|
||||
}
|
||||
|
||||
static uint32_t stm32_now_ms(void *context)
|
||||
{
|
||||
LedIndicator_Stm32HalPort *adapter = (LedIndicator_Stm32HalPort *)context;
|
||||
const uint32_t current = __HAL_TIM_GET_COUNTER(adapter->timer);
|
||||
const uint32_t reload = __HAL_TIM_GET_AUTORELOAD(adapter->timer);
|
||||
uint32_t delta;
|
||||
uint64_t scaled;
|
||||
|
||||
if (current >= adapter->last_counter) {
|
||||
delta = current - adapter->last_counter;
|
||||
} else if (reload == UINT32_MAX) {
|
||||
delta = current - adapter->last_counter;
|
||||
} else {
|
||||
delta = (reload - adapter->last_counter) + 1U + current;
|
||||
}
|
||||
adapter->last_counter = current;
|
||||
|
||||
scaled = (uint64_t)adapter->remainder + ((uint64_t)delta * 1000ULL);
|
||||
adapter->accumulated_ms += (uint32_t)(scaled / adapter->timer_tick_hz);
|
||||
adapter->remainder = (uint32_t)(scaled % adapter->timer_tick_hz);
|
||||
return adapter->accumulated_ms;
|
||||
}
|
||||
|
||||
uint8_t LedIndicator_Stm32HalPortInit(LedIndicator_Stm32HalPort *adapter,
|
||||
TIM_HandleTypeDef *timer,
|
||||
uint32_t timer_tick_hz,
|
||||
const LedIndicator_Stm32HalOutput *outputs,
|
||||
size_t output_count,
|
||||
LedIndicator_Port *port)
|
||||
{
|
||||
if ((adapter == NULL) || (timer == NULL) || (timer_tick_hz == 0U) ||
|
||||
(outputs == NULL) || (output_count == 0U) || (output_count > 256U) ||
|
||||
(port == NULL)) {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
adapter->timer = timer;
|
||||
adapter->outputs = outputs;
|
||||
adapter->output_count = output_count;
|
||||
adapter->timer_tick_hz = timer_tick_hz;
|
||||
adapter->last_counter = __HAL_TIM_GET_COUNTER(timer);
|
||||
adapter->accumulated_ms = 0U;
|
||||
adapter->remainder = 0U;
|
||||
|
||||
port->write = stm32_write;
|
||||
port->now_ms = stm32_now_ms;
|
||||
port->context = adapter;
|
||||
return 1U;
|
||||
}
|
||||
39
c/led-indicator/ports/stm32-hal/led_indicator_stm32_hal.h
Normal file
39
c/led-indicator/ports/stm32-hal/led_indicator_stm32_hal.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file led_indicator_stm32_hal.h
|
||||
* @brief Порт LED Indicator для STM32F1/F4/G4 HAL и выбранного TIM.
|
||||
*/
|
||||
|
||||
#ifndef LED_INDICATOR_STM32_HAL_H
|
||||
#define LED_INDICATOR_STM32_HAL_H
|
||||
|
||||
#include "led_indicator.h"
|
||||
#include "led_indicator_stm32_hal_config.h"
|
||||
|
||||
typedef struct {
|
||||
GPIO_TypeDef *gpio;
|
||||
uint16_t pin;
|
||||
uint8_t active_low;
|
||||
} LedIndicator_Stm32HalOutput;
|
||||
|
||||
typedef struct {
|
||||
TIM_HandleTypeDef *timer;
|
||||
const LedIndicator_Stm32HalOutput *outputs;
|
||||
size_t output_count;
|
||||
uint32_t timer_tick_hz;
|
||||
uint32_t last_counter;
|
||||
uint32_t accumulated_ms;
|
||||
uint32_t remainder;
|
||||
} LedIndicator_Stm32HalPort;
|
||||
|
||||
/**
|
||||
* Создаёт порт на уже настроенном и запущенном таймере.
|
||||
* timer_tick_hz — частота изменения CNT после prescaler, например 1000 Гц.
|
||||
*/
|
||||
uint8_t LedIndicator_Stm32HalPortInit(LedIndicator_Stm32HalPort *adapter,
|
||||
TIM_HandleTypeDef *timer,
|
||||
uint32_t timer_tick_hz,
|
||||
const LedIndicator_Stm32HalOutput *outputs,
|
||||
size_t output_count,
|
||||
LedIndicator_Port *port);
|
||||
|
||||
#endif /* LED_INDICATOR_STM32_HAL_H */
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
#define LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
#define LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
#define LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
|
||||
#include "stm32g4xx_hal.h"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user