
#include <stddef.h>
// #include "RTE.h"
#include "BU91R65.h"
// #include "RTE_GPIO_R7F7016843.h"

BU91R65_Handler_st_t g_stBU91R65Chip0;
// uint8_t g_u8BU91R65Chip0Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip0Seg;

BU91R65_Handler_st_t g_stBU91R65Chip1;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip1Seg;

BU91R65_Handler_st_t g_stBU91R65Chip2;
// uint8_t g_u8BU91R65Chip0Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip2Seg;

BU91R65_Handler_st_t g_stBU91R65Chip3;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip3Seg;

BU91R65_Handler_st_t g_stBU91R65Chip4;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip4Seg;

BU91R65_Handler_st_t g_stBU91R65Chip5;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip5Seg;

BU91R65_Handler_st_t g_stBU91R65Chip6;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip6Seg;

BU91R65_Handler_st_t g_stBU91R65Chip7;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip7Seg;

BU91R65_Handler_st_t g_stBU91R65Chip8;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip8Seg;

BU91R65_Handler_st_t g_stBU91R65Chip9;
// uint8_t g_u8BU91R65Chip1Buffer[BU91R65_BUFFER_SIZE];
BU91R65DDRAMUnion g_stBU91R65Chip9Seg;

// BU91R65_Handler_st_t g_stBU98R10Chip2;
// uint8_t g_u8BU98R10Chip2Buffer[BU91R65_BUFFER_SIZE];
// BU91R65DDRAMUnion g_stBU98R10Chip2Seg;

void BU91R65_Register(void)
{
    BU91R65_Config_st_t stConfig;

    /* Power on */
    //BU98R10_GPIO_Request(RTE_GPIO_PORT06_PIN02);/*不一定需要*/

    BU91R65_Init(1UL);//Trim到200-400khz

    stConfig.u8DefaultVal           = 0x00U;
    stConfig.u8FrameFreq            = BU98R10_FRAME_FREQ_120_0_Hz;
    stConfig.u16PinCS               = BU91R65_Chip0_Set_nCS;
    stConfig.u16PinSCLK             = BU91R65_Chip0_Set_SCLK;
    stConfig.u16PinSD               = BU91R65_Chip0_Set_SD;
    stConfig.pu8Buffer              = &g_stBU91R65Chip0Seg;
    BU91R65_Add(&g_stBU91R65Chip0, &stConfig);
    
    stConfig.u16PinCS               = BU91R65_Chip1_Set_nCS;
    stConfig.u16PinSCLK             = BU91R65_Chip1_Set_SCLK;
    stConfig.u16PinSD               = BU91R65_Chip1_Set_SD;
    stConfig.pu8Buffer              = &g_stBU91R65Chip1Seg;
    BU91R65_Add(&g_stBU91R65Chip1, &stConfig);



    stConfig.u8DefaultVal           = 0x00U;
    stConfig.u8FrameFreq            = BU98R10_FRAME_FREQ_120_0_Hz;
    stConfig.u16PinCS               = BU91R65_Chip0_Set_nCSb;
    stConfig.u16PinSCLK             = BU91R65_Chip0_Set_SCLKb;
    stConfig.u16PinSD               = BU91R65_Chip0_Set_SDb;
    stConfig.pu8Buffer              = &g_stBU91R65Chip2Seg;
    BU91R65_Add(&g_stBU91R65Chip2, &stConfig);

    stConfig.u16PinCS               = BU91R65_Chip1_Set_nCSb;
    stConfig.u16PinSCLK             = BU91R65_Chip1_Set_SCLKb;
    stConfig.u16PinSD               = BU91R65_Chip1_Set_SDb;
    stConfig.pu8Buffer              = &g_stBU91R65Chip3Seg;
    BU91R65_Add(&g_stBU91R65Chip3, &stConfig);


    
    stConfig.u8DefaultVal           = 0x00U;
    stConfig.u8FrameFreq            = BU98R10_FRAME_FREQ_120_0_Hz;
    stConfig.u16PinCS               = BU91R65_Chip0_Set_nCSa;
    stConfig.u16PinSCLK             = BU91R65_Chip0_Set_SCLKa;
    stConfig.u16PinSD               = BU91R65_Chip0_Set_SDa;
    stConfig.pu8Buffer              = &g_stBU91R65Chip4Seg;
    BU91R65_Add(&g_stBU91R65Chip4, &stConfig);

    stConfig.u16PinCS               = BU91R65_Chip1_Set_nCSa;
    stConfig.u16PinSCLK             = BU91R65_Chip1_Set_SCLKa;
    stConfig.u16PinSD               = BU91R65_Chip1_Set_SDa;
    stConfig.pu8Buffer              = &g_stBU91R65Chip5Seg;
    BU91R65_Add(&g_stBU91R65Chip5, &stConfig);



    stConfig.u8DefaultVal           = 0x00U;
    stConfig.u8FrameFreq            = BU98R10_FRAME_FREQ_120_0_Hz;
    stConfig.u16PinCS               = BU91R65_Chip0_Set_nCSc;
    stConfig.u16PinSCLK             = BU91R65_Chip0_Set_SCLKc;
    stConfig.u16PinSD               = BU91R65_Chip0_Set_SDc;
    stConfig.pu8Buffer              = &g_stBU91R65Chip6Seg;
    BU91R65_Add(&g_stBU91R65Chip6, &stConfig);

    stConfig.u16PinCS               = BU91R65_Chip1_Set_nCSc;
    stConfig.u16PinSCLK             = BU91R65_Chip1_Set_SCLKc;
    stConfig.u16PinSD               = BU91R65_Chip1_Set_SDc;
    stConfig.pu8Buffer              = &g_stBU91R65Chip7Seg;
    BU91R65_Add(&g_stBU91R65Chip7, &stConfig);



    stConfig.u8DefaultVal           = 0x00U;
    stConfig.u8FrameFreq            = BU98R10_FRAME_FREQ_120_0_Hz;
    stConfig.u16PinCS               = BU91R65_Chip0_Set_nCSd;
    stConfig.u16PinSCLK             = BU91R65_Chip0_Set_SCLKd;
    stConfig.u16PinSD               = BU91R65_Chip0_Set_SDd;
    stConfig.pu8Buffer              = &g_stBU91R65Chip8Seg;
    BU91R65_Add(&g_stBU91R65Chip8, &stConfig);

    stConfig.u16PinCS               = BU91R65_Chip1_Set_nCSd;
    stConfig.u16PinSCLK             = BU91R65_Chip1_Set_SCLKd;
    stConfig.u16PinSD               = BU91R65_Chip1_Set_SDd;
    stConfig.pu8Buffer              = &g_stBU91R65Chip9Seg;
    BU91R65_Add(&g_stBU91R65Chip9, &stConfig);
}



// typedef struct {
//     BU91R65_Handler_st_t* pHandle;     // 芯片句柄
//     uint8_t* pBuffer;                   // 缓冲区指针
//     void (*Set_CS)(uint8_t);           // CS引脚设置函数
//     void (*Set_SCLK)(uint8_t);         // SCLK引脚设置函数  
//     void (*Set_SD)(uint8_t);           // SD引脚设置函数
// } BU91R65_ChipConfig_t;

// // 芯片配置表 - 集中管理所有芯片配置
// static const BU91R65_ChipConfig_t s_stChipConfigs[] = {
//     // 第一组：无后缀
//     {&g_stBU91R65Chip0, &g_stBU91R65Chip0Seg, BU91R65_Chip0_Set_nCS,  BU91R65_Chip0_Set_SCLK,  BU91R65_Chip0_Set_SD},
//     {&g_stBU91R65Chip1, &g_stBU91R65Chip1Seg, BU91R65_Chip1_Set_nCS,  BU91R65_Chip1_Set_SCLK,  BU91R65_Chip1_Set_SD},
    
//     // 第二组：b后缀  
//     {&g_stBU91R65Chip2, &g_stBU91R65Chip2Seg, BU91R65_Chip0_Set_nCSb, BU91R65_Chip0_Set_SCLKb, BU91R65_Chip0_Set_SDb},
//     {&g_stBU91R65Chip3, &g_stBU91R65Chip3Seg, BU91R65_Chip1_Set_nCSb, BU91R65_Chip1_Set_SCLKb, BU91R65_Chip1_Set_SDb},
    
//     // 第三组：a后缀
//     {&g_stBU91R65Chip4, &g_stBU91R65Chip4Seg, BU91R65_Chip0_Set_nCSa, BU91R65_Chip0_Set_SCLKa, BU91R65_Chip0_Set_SDa},
//     {&g_stBU91R65Chip5, &g_stBU91R65Chip5Seg, BU91R65_Chip1_Set_nCSa, BU91R65_Chip1_Set_SCLKa, BU91R65_Chip1_Set_SDa},
    
//     // 第四组：c后缀
//     {&g_stBU91R65Chip6, &g_stBU91R65Chip6Seg, BU91R65_Chip0_Set_nCSc, BU91R65_Chip0_Set_SCLKc, BU91R65_Chip0_Set_SDc},
//     {&g_stBU91R65Chip7, &g_stBU91R65Chip7Seg, BU91R65_Chip1_Set_nCSc, BU91R65_Chip1_Set_SCLKc, BU91R65_Chip1_Set_SDc},
    
//     // 第五组：d后缀
//     {&g_stBU91R65Chip8, &g_stBU91R65Chip8Seg, BU91R65_Chip0_Set_nCSd, BU91R65_Chip0_Set_SCLKd, BU91R65_Chip0_Set_SDd},
//     {&g_stBU91R65Chip9, &g_stBU91R65Chip9Seg, BU91R65_Chip1_Set_nCSd, BU91R65_Chip1_Set_SCLKd, BU91R65_Chip1_Set_SDd},
// };

// #define CHIP_CONFIG_COUNT (sizeof(s_stChipConfigs) / sizeof(s_stChipConfigs[0]))

// void BU91R65_InitAllChips(void)
// {
//     BU91R65_Config_st_t stConfig;
    
//     // 公共配置
//     stConfig.u8DefaultVal = 0x00U;
//     stConfig.u8FrameFreq  = BU98R10_FRAME_FREQ_120_0_Hz;
    
//     // 循环初始化所有芯片
//     for (uint32_t i = 0; i < CHIP_CONFIG_COUNT; i++) {
//         const BU91R65_ChipConfig_t* pChipConfig = &s_stChipConfigs[i];
        
//         // 设置芯片特定配置
//         stConfig.u16PinCS   = pChipConfig->Set_CS;
//         stConfig.u16PinSCLK = pChipConfig->Set_SCLK;  
//         stConfig.u16PinSD   = pChipConfig->Set_SD;
//         stConfig.pu8Buffer  = pChipConfig->pBuffer;
        
//         // 添加芯片
//         BU91R65_Add(pChipConfig->pHandle, &stConfig);
//     }
// }

// // 统一的引脚控制函数
// void BU91R65_Set_CS(uint8_t u8ChipId, uint8_t u8Level)
// {
//     switch(u8ChipId) {
//         case 0: BU91R65_CHIP0_CSB = u8Level; break;
//         case 1: BU91R65_CHIP1_CSB = u8Level; break;
//         case 2: BU91R65_CHIP0_CSBb = u8Level; break;
//         // ... 其他芯片
//         default: break;
//     }
// }

// // 类似的统一SCLK和SD函数
// void BU91R65_Set_SCLK(uint8_t u8ChipId, uint8_t u8Level) { /* ... */ }
// void BU91R65_Set_SD(uint8_t u8ChipId, uint8_t u8Level) { /* ... */ }

// // 初始化所有芯片（一行搞定！）
// BU91R65_InitAllChips();

// // 如果需要单独初始化某个芯片
// void BU91R65_InitSingleChip(uint32_t chipIndex)
// {
//     if (chipIndex < CHIP_CONFIG_COUNT) {
//         BU91R65_ConfigTypeDef stConfig = {
//             .u8DefaultVal = 0x00U,
//             .u8FrameFreq  = BU98R10_FRAME_FREQ_120_0_Hz,
//             .u16PinCS     = s_stChipConfigs[chipIndex].Set_CS,
//             .u16PinSCLK   = s_stChipConfigs[chipIndex].Set_SCLK,
//             .u16PinSD     = s_stChipConfigs[chipIndex].Set_SD,
//             .pu8Buffer    = s_stChipConfigs[chipIndex].pBuffer
//         };
//         BU91R65_Add(s_stChipConfigs[chipIndex].pHandle, &stConfig);
//     }
// }