N32WB031_MysteryLight/board/board.c
Embedded_Projects eb5a3c2a8d Initial commit
2023-10-26 13:18:36 +00:00

57 lines
1.4 KiB
C

/* Device */
#include "n32wb03x.h"
/* Board */
#include "board.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
typedef struct {
GPIO_Module *port;
uint32_t port_rcc;
uint8_t pin;
bool active_high;
} board_led_table_t;
static const board_led_table_t s_led_table[] = {
{.port = GPIOB, .port_rcc = RCC_APB2_PERIPH_GPIOB, .pin = 0, .active_high = true},
{.port = GPIOA, .port_rcc = RCC_APB2_PERIPH_GPIOA, .pin = 6, .active_high = true},
};
void board_led_init(uint8_t led) {
GPIO_InitType led_init;
GPIO_InitStruct(&led_init);
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
led_init.Pin = (1U << s_led_table[led].pin);
led_init.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
RCC_EnableAPB2PeriphClk(s_led_table[led].port_rcc, ENABLE);
GPIO_InitPeripheral(s_led_table[led].port, &led_init);
board_led_set(led, false);
}
void board_led_set(uint8_t led, bool on) {
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
Bit_OperateType val = on ? Bit_SET : Bit_RESET;
if (!s_led_table[led].active_high) {
val = on ? Bit_RESET : Bit_SET;
}
GPIO_WriteBit(s_led_table[led].port, (1U << s_led_table[led].pin), val);
}
void board_led_toggle(uint8_t led) {
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
GPIO_TogglePin(s_led_table[led].port, (1U << s_led_table[led].pin));
}