Сохранить текущие изменения NAND и LED перед объединением веток
This commit is contained in:
27
c/led-indicator/tests/fakes/led_indicator_stm32_hal_config.h
Normal file
27
c/led-indicator/tests/fakes/led_indicator_stm32_hal_config.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
#define LED_INDICATOR_STM32_HAL_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
GPIO_PIN_RESET = 0,
|
||||
GPIO_PIN_SET
|
||||
} GPIO_PinState;
|
||||
|
||||
typedef struct {
|
||||
uint16_t last_pin;
|
||||
GPIO_PinState last_state;
|
||||
uint32_t writes;
|
||||
} GPIO_TypeDef;
|
||||
|
||||
typedef struct {
|
||||
uint32_t counter;
|
||||
uint32_t autoreload;
|
||||
} TIM_HandleTypeDef;
|
||||
|
||||
#define __HAL_TIM_GET_COUNTER(handle) ((handle)->counter)
|
||||
#define __HAL_TIM_GET_AUTORELOAD(handle) ((handle)->autoreload)
|
||||
|
||||
void HAL_GPIO_WritePin(GPIO_TypeDef *gpio, uint16_t pin, GPIO_PinState state);
|
||||
|
||||
#endif
|
||||
84
c/led-indicator/tests/test_led_indicator.c
Normal file
84
c/led-indicator/tests/test_led_indicator.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "led_indicator.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t now;
|
||||
uint8_t outputs[3];
|
||||
uint32_t writes[3];
|
||||
} FakePort;
|
||||
|
||||
static void fake_write(void *context, uint8_t channel, uint8_t on)
|
||||
{
|
||||
FakePort *fake = (FakePort *)context;
|
||||
fake->outputs[channel] = on;
|
||||
fake->writes[channel]++;
|
||||
}
|
||||
|
||||
static uint32_t fake_now(void *context)
|
||||
{
|
||||
return ((FakePort *)context)->now;
|
||||
}
|
||||
|
||||
#define CHECK(condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
fprintf(stderr, "check failed at line %d: %s\n", __LINE__, \
|
||||
#condition); \
|
||||
return 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
LedIndicator indicator;
|
||||
LedIndicator_Channel channels[3];
|
||||
LedIndicator_Config config;
|
||||
LedIndicator_Pattern patterns[LED_INDICATOR_MODE_COUNT];
|
||||
FakePort fake = {0};
|
||||
LedIndicator_Port port = {fake_write, fake_now, &fake};
|
||||
|
||||
LedIndicator_ConfigDefault(&config);
|
||||
CHECK(LedIndicator_ValidateConfig(&config) != 0U);
|
||||
CHECK(LedIndicator_CopyDefaultPatterns(patterns, LED_INDICATOR_MODE_COUNT) ==
|
||||
LED_INDICATOR_MODE_COUNT);
|
||||
patterns[LED_INDICATOR_MODE_WORK].duration_ms[0] = 25U;
|
||||
CHECK(patterns[LED_INDICATOR_MODE_WORK].duration_ms[0] == 25U);
|
||||
CHECK(LedIndicator_Init(&indicator, channels, 3U, &port, &config) != 0U);
|
||||
CHECK(fake.writes[0] == 1U && fake.writes[1] == 1U && fake.writes[2] == 1U);
|
||||
|
||||
CHECK(LedIndicator_SetMode(&indicator, 0U, LED_INDICATOR_MODE_WORK) != 0U);
|
||||
CHECK(fake.outputs[0] == 1U);
|
||||
fake.now = 99U;
|
||||
CHECK(LedIndicator_SetMode(&indicator, 0U, LED_INDICATOR_MODE_WORK) != 0U);
|
||||
LedIndicator_Process(&indicator);
|
||||
CHECK(fake.outputs[0] == 1U);
|
||||
fake.now = 100U;
|
||||
LedIndicator_Process(&indicator);
|
||||
CHECK(fake.outputs[0] == 0U);
|
||||
fake.now = 1000U;
|
||||
LedIndicator_Process(&indicator);
|
||||
CHECK(fake.outputs[0] == 1U);
|
||||
fake.now = 1050U;
|
||||
CHECK(LedIndicator_RestartMode(&indicator, 0U, LED_INDICATOR_MODE_WORK) != 0U);
|
||||
fake.now = 1150U;
|
||||
LedIndicator_Process(&indicator);
|
||||
CHECK(fake.outputs[0] == 0U);
|
||||
|
||||
CHECK(LedIndicator_SetModeAt(&indicator, 1U, LED_INDICATOR_MODE_ERROR,
|
||||
0xFFFFFFF0UL) != 0U);
|
||||
LedIndicator_ProcessAt(&indicator, 0x00000054UL);
|
||||
CHECK(fake.outputs[1] == 0U); /* 100 ms после старта, переход во второй шаг. */
|
||||
LedIndicator_ProcessAt(&indicator, 0x000000B8UL);
|
||||
CHECK(fake.outputs[1] == 1U); /* 200 ms после старта, второй импульс. */
|
||||
|
||||
CHECK(LedIndicator_SetModeAt(&indicator, 2U, LED_INDICATOR_MODE_STARTUP, 10U) != 0U);
|
||||
LedIndicator_ProcessAt(&indicator, 610U);
|
||||
CHECK(fake.outputs[2] == 1U); /* Однократный startup завершён постоянным ON. */
|
||||
|
||||
CHECK(LedIndicator_SetMode(&indicator, 3U, LED_INDICATOR_MODE_ON) == 0U);
|
||||
CHECK(LedIndicator_GetMode(&indicator, 0U) == LED_INDICATOR_MODE_WORK);
|
||||
|
||||
puts("led indicator tests passed");
|
||||
return 0;
|
||||
}
|
||||
52
c/led-indicator/tests/test_stm32_port.c
Normal file
52
c/led-indicator/tests/test_stm32_port.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "led_indicator_stm32_hal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void HAL_GPIO_WritePin(GPIO_TypeDef *gpio, uint16_t pin, GPIO_PinState state)
|
||||
{
|
||||
gpio->last_pin = pin;
|
||||
gpio->last_state = state;
|
||||
gpio->writes++;
|
||||
}
|
||||
|
||||
#define CHECK(condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
fprintf(stderr, "check failed at line %d: %s\n", __LINE__, \
|
||||
#condition); \
|
||||
return 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
GPIO_TypeDef gpio_a = {0};
|
||||
GPIO_TypeDef gpio_b = {0};
|
||||
TIM_HandleTypeDef timer = {100U, 65535U};
|
||||
const LedIndicator_Stm32HalOutput outputs[] = {
|
||||
{&gpio_a, 0x0001U, 0U},
|
||||
{&gpio_b, 0x0080U, 1U}
|
||||
};
|
||||
LedIndicator_Stm32HalPort adapter;
|
||||
LedIndicator_Port port;
|
||||
|
||||
CHECK(LedIndicator_Stm32HalPortInit(&adapter, &timer, 10000U,
|
||||
outputs, 2U, &port) != 0U);
|
||||
port.write(port.context, 0U, 1U);
|
||||
CHECK(gpio_a.last_pin == 0x0001U && gpio_a.last_state == GPIO_PIN_SET);
|
||||
port.write(port.context, 1U, 1U);
|
||||
CHECK(gpio_b.last_pin == 0x0080U && gpio_b.last_state == GPIO_PIN_RESET);
|
||||
|
||||
timer.counter = 109U;
|
||||
CHECK(port.now_ms(port.context) == 0U);
|
||||
timer.counter = 110U;
|
||||
CHECK(port.now_ms(port.context) == 1U);
|
||||
|
||||
timer.counter = 65530U;
|
||||
(void)port.now_ms(port.context);
|
||||
timer.counter = 4U;
|
||||
CHECK(port.now_ms(port.context) == 6544U);
|
||||
|
||||
puts("led indicator STM32 port tests passed");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user