//
// Created by anonymous on 2026/5/24.
//

#ifndef _VOILET_PORT_H
#define _VOILET_PORT_H

#include "stm32f10x.h"

#define INTERRUPT_ENABLE    __set_PRIMASK(0);       //开启中断
#define INTERRUPT_DISABLE   __set_PRIMASK(1);       //关闭中断

#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))

/* BOOL类型定义 */
typedef enum {
    false = 0,
    true  = 1,
} BOOL_MAP_T;

/* DI数字信号采集枚举 */
enum {
    DI_OFF = 0,
    DI_ON
};
/* DO数字信号输出枚举 */
enum {
    DO_OFF = 0,
    DO_ON
};
/* LED灯输出枚举 */
enum {
    LED_OFF = 0,
    LED_ON
};

/* Interrupt Irq */
typedef struct {
    unsigned char Irqn;
    void *Periph;
    void (*IrqCallback)(void *Param);
}IRQ_MAP_T;

#define IRQ_NUM 67    //系统共有多少个中断
#define NVIC_GROUP_LEVEL NVIC_PriorityGroup_2   //中断组

/* GPIO */
typedef struct {
    GPIO_TypeDef *GpioPort;
    unsigned short GpioPin;
    // unsigned char Irqn;
} GPIO_PERIPH_T;

typedef struct {
    void (*ClockCmd)(uint32_t RccPeriph, FunctionalState State);
    unsigned int ClockTree;
    GPIO_PERIPH_T Periph;
} GPIO_MAP_T;

typedef struct {
    GPIO_MAP_T GpioX;
    GPIOMode_TypeDef Mode;
    GPIOSpeed_TypeDef Speed;
}GPIO_CONFIG;

// ==================== GPIOA 组 ====================
#ifdef GPIOA
#define GPIO0   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_0}}
#define GPIO1   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_1}}
#define GPIO2   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_2}}
#define GPIO3   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_3}}
#define GPIO4   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_4}}
#define GPIO5   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_5}}
#define GPIO6   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_6}}
#define GPIO7   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_7}}
#define GPIO8   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_8}}
#define GPIO9   {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_9}}
#define GPIO10  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_10}}
#define GPIO11  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_11}}
#define GPIO12  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_12}}
#define GPIO13  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_13}}
#define GPIO14  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_14}}
#define GPIO15  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOA, {GPIOA, GPIO_Pin_15}}
#endif
#ifdef GPIOB
// ==================== GPIOB 组 ====================
#define GPIO16  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_0}}
#define GPIO17  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_1}}
#define GPIO18  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_2}}
#define GPIO19  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_3}}
#define GPIO20  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_4}}
#define GPIO21  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_5}}
#define GPIO22  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_6}}
#define GPIO23  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_7}}
#define GPIO24  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_8}}
#define GPIO25  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_9}}
#define GPIO26  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_10}}
#define GPIO27  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_11}}
#define GPIO28  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_12}}
#define GPIO29  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_13}}
#define GPIO30  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_14}}
#define GPIO31  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOB, {GPIOB, GPIO_Pin_15}}
#endif
#ifdef GPIOC
// ==================== GPIOC 组 ====================
#define GPIO32  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_0}}
#define GPIO33  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_1}}
#define GPIO34  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_2}}
#define GPIO35  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_3}}
#define GPIO36  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_4}}
#define GPIO37  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_5}}
#define GPIO38  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_6}}
#define GPIO39  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_7}}
#define GPIO40  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_8}}
#define GPIO41  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_9}}
#define GPIO42  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_10}}
#define GPIO43  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_11}}
#define GPIO44  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_12}}
#define GPIO45  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_13}}
#define GPIO46  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_14}}
#define GPIO47  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOC, {GPIOC, GPIO_Pin_15}}
#endif
#ifdef GPIOD
// ==================== GPIOD 组 ====================
#define GPIO48  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_0}}
#define GPIO49  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_1}}
#define GPIO50  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_2}}
#define GPIO51  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_3}}
#define GPIO52  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_4}}
#define GPIO53  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_5}}
#define GPIO54  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_6}}
#define GPIO55  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_7}}
#define GPIO56  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_8}}
#define GPIO57  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_9}}
#define GPIO58  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_10}}
#define GPIO59  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_11}}
#define GPIO60  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_12}}
#define GPIO61  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_13}}
#define GPIO62  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_14}}
#define GPIO63  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOD, {GPIOD, GPIO_Pin_15}}
#endif
#ifdef GPIOE
// ==================== GPIOE 组 ====================
#define GPIO64  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_0}}
#define GPIO65  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_1}}
#define GPIO66  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_2}}
#define GPIO67  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_3}}
#define GPIO68  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_4}}
#define GPIO69  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_5}}
#define GPIO70  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_6}}
#define GPIO71  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_7}}
#define GPIO72  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_8}}
#define GPIO73  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_9}}
#define GPIO74  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_10}}
#define GPIO75  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_11}}
#define GPIO76  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_12}}
#define GPIO77  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_13}}
#define GPIO78  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_14}}
#define GPIO79  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOE, {GPIOE, GPIO_Pin_15}}
#endif
#ifdef GPIOF
// ==================== GPIOF 组 ====================
#define GPIO80  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_0}}
#define GPIO81  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_1}}
#define GPIO82  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_2}}
#define GPIO83  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_3}}
#define GPIO84  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_4}}
#define GPIO85  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_5}}
#define GPIO86  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_6}}
#define GPIO87  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_7}}
#define GPIO88  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_8}}
#define GPIO89  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_9}}
#define GPIO90  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_10}}
#define GPIO91  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_11}}
#define GPIO92  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_12}}
#define GPIO93  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_13}}
#define GPIO94  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_14}}
#define GPIO95  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOF, {GPIOF, GPIO_Pin_15}}
#endif
#ifdef GPIOG
// ==================== GPIOG 组 ====================
#define GPIO96  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_0}}
#define GPIO97  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_1}}
#define GPIO98  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_2}}
#define GPIO99  {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_3}}
#define GPIO100 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_4}}
#define GPIO101 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_5}}
#define GPIO102 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_6}}
#define GPIO103 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_7}}
#define GPIO104 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_8}}
#define GPIO105 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_9}}
#define GPIO106 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_10}}
#define GPIO107 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_11}}
#define GPIO108 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_12}}
#define GPIO109 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_13}}
#define GPIO110 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_14}}
#define GPIO111 {RCC_APB2PeriphClockCmd, RCC_APB2Periph_GPIOG, {GPIOG, GPIO_Pin_15}}
#endif

#define GPIO_NULL    {0, 0, {0, 0}}

/* COM */
typedef struct {
    USART_TypeDef *UartId;
    unsigned char Irqn;
} COM_PERIPH_T;

typedef struct {
    void (*ClockCmd)(uint32_t RccPeriph, FunctionalState State);
    unsigned int ClockTree;
    COM_PERIPH_T Periph;
} COM_MAP_T;

typedef struct {
    COM_MAP_T *ComX;                //串口外设
    GPIO_MAP_T TxPort;              //发送端口
    GPIO_MAP_T RxPort;              //接收端口
    GPIO_MAP_T TxRxEn;              //收发使能
}COM_CONFIG;

#define COM_NUM 5
#ifdef USART1
#define COM0   (&(COM_MAP_T){RCC_APB2PeriphClockCmd, RCC_APB2Periph_USART1, {USART1,USART1_IRQn}})
#endif
#ifdef USART2
#define COM1   (&(COM_MAP_T){RCC_APB1PeriphClockCmd, RCC_APB1Periph_USART2, {USART2,USART2_IRQn}})
#endif
#ifdef USART3
#define COM2   (&(COM_MAP_T){RCC_APB1PeriphClockCmd, RCC_APB1Periph_USART3,{USART3,USART3_IRQn}})
#endif
#ifdef UART4
#define COM3   (&(COM_MAP_T){RCC_APB1PeriphClockCmd, RCC_APB1Periph_UART4,{UART4,UART4_IRQn}})
#endif
#define COM4   (&(COM_MAP_T){RCC_APB1PeriphClockCmd, RCC_APB1Periph_UART5,{UART5,UART5_IRQn}})

#endif //_VOILET_PORT_H
