#include "gd32e23x.h"
#include "board_config.h"
#include "systick.h"

/******************************************************************************/

#define FLASH_SIZE_ADDR      (*(const uint8_t *)0x1FFFF7E0) // Flash base address

/******************************************************************************/

/* 前向声明中断处理函数 */
void usart0_irq_handler(void);
void usart1_irq_handler(void);

usart_config_t g_usart_config = {
    .rcu_usart = RCU_USART1,
    .usart_periph = USART1,
    .irq_type = USART1_IRQn,
    .irq_handler = usart1_irq_handler  // 初始化函数指针
};

uint8_t g_mcu_flash_size = 0;

void mcu_detect_and_config(void) {
    g_mcu_flash_size = FLASH_SIZE_ADDR;

    switch (g_mcu_flash_size) {
        case GD32E23XF4:
            g_usart_config.rcu_usart = RCU_USART0;
            g_usart_config.usart_periph = USART0;
            g_usart_config.irq_type = USART0_IRQn;
            g_usart_config.irq_handler = usart0_irq_handler;  // 指向USART0处理函数
            break;
        case GD32E23XF6:
            g_usart_config.rcu_usart = RCU_USART1;
            g_usart_config.usart_periph = USART1;
            g_usart_config.irq_type = USART1_IRQn;
            g_usart_config.irq_handler = usart1_irq_handler;  // 指向USART1处理函数
            break;
        default: // Default to GD32E23XF8
            g_usart_config.rcu_usart = RCU_USART1;
            g_usart_config.usart_periph = USART1;
            g_usart_config.irq_type = USART1_IRQn;
            g_usart_config.irq_handler = usart1_irq_handler;  // 指向USART1处理函数
            break;
    }

}

uint8_t get_flash_size(void) {
    return g_mcu_flash_size;
}
