generated from hulk/gd32e23x_template_cmake_vscode
detect MCU flash size and config usart
This commit is contained in:
@@ -37,6 +37,7 @@ OF SUCH DAMAGE.
|
||||
#include "uart.h"
|
||||
#include "uart_ring_buffer.h"
|
||||
#include "led.h"
|
||||
#include "board_config.h"
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
@@ -102,8 +103,19 @@ void SysTick_Handler(void) {
|
||||
}
|
||||
|
||||
void USART0_IRQHandler(void) {
|
||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART0);
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
if (get_mcu_type() == GD32E23XF4) {
|
||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART0);
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void) {
|
||||
if (get_mcu_type() == GD32E23XF6 || get_mcu_type() == GD32E23XF8) {
|
||||
if (RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART1);
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
}
|
||||
}
|
||||
}
|
32
Src/main.c
32
Src/main.c
@@ -43,6 +43,8 @@ OF SUCH DAMAGE.
|
||||
#include "ldc1612.h"
|
||||
#include "tmp112.h"
|
||||
|
||||
#define FLASH_SIZE_ADDR (*(const uint16_t *)0x1FFFF7E0)
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
\param[in] none
|
||||
@@ -51,13 +53,41 @@ OF SUCH DAMAGE.
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
systick_config();
|
||||
|
||||
/* Detect MCU type and configure USART accordingly */
|
||||
mcu_detect_and_config();
|
||||
|
||||
uint16_t flash_kb = FLASH_SIZE_ADDR;
|
||||
|
||||
rs485_init();
|
||||
printf("MCU Type detected: ");
|
||||
switch (get_mcu_type()) {
|
||||
case GD32E23XF4:
|
||||
printf("GD32E23XF4 (using USART0)\r\n");
|
||||
break;
|
||||
case GD32E23XF6:
|
||||
printf("GD32E23XF6 (using USART1)\r\n");
|
||||
break;
|
||||
case GD32E23XF8:
|
||||
printf("GD32E23XF8 (using USART1)\r\n");
|
||||
break;
|
||||
default:
|
||||
printf("Unknown (default to GD32E23XF8)\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
setbuf(stdout, NULL);
|
||||
systick_config();
|
||||
|
||||
rs485_init();
|
||||
|
||||
led_init();
|
||||
|
||||
printf("Flash Size: %d KB\r\n", flash_kb);
|
||||
printf("%x\r\n", flash_kb);
|
||||
|
||||
printf("Flash Size Bytes: %02X\r\n", flash_kb & 0xFF);
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
char hello_world[] = {"Hello World!\r\n"};
|
||||
|
||||
|
77
Src/mcu_config.c
Normal file
77
Src/mcu_config.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*!
|
||||
\file mcu_config.c
|
||||
\brief MCU type detection and dynamic configuration
|
||||
|
||||
\version 2025-08-20, V1.0.0, MCU detection for GD32E23x
|
||||
*/
|
||||
|
||||
#include "gd32e23x.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#define FLASH_SIZE_ADDR (*(const uint16_t *)0x1FFFF7E0)
|
||||
|
||||
/* Global variables for dynamic configuration */
|
||||
uint8_t g_mcu_type = 0;
|
||||
usart_config_t g_usart_config = {0};
|
||||
|
||||
/*!
|
||||
\brief detect MCU type and configure USART accordingly
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void mcu_detect_and_config(void)
|
||||
{
|
||||
uint16_t flash_kb = FLASH_SIZE_ADDR;
|
||||
uint8_t size = (flash_kb >> 8) & 0xFF;
|
||||
|
||||
/* Detect MCU type based on flash size */
|
||||
switch (size) {
|
||||
case GD32E23XF4:
|
||||
g_mcu_type = GD32E23XF4;
|
||||
/* Configure for USART0 (GD32E23XF4) */
|
||||
g_usart_config.rcu_usart = RCU_USART0;
|
||||
g_usart_config.usart_periph = USART0;
|
||||
g_usart_config.gpio_af = GPIO_AF_1;
|
||||
g_usart_config.irq_type = USART0_IRQn;
|
||||
break;
|
||||
|
||||
case GD32E23XF6:
|
||||
g_mcu_type = GD32E23XF6;
|
||||
/* Configure for USART1 (GD32E23XF6) */
|
||||
g_usart_config.rcu_usart = RCU_USART1;
|
||||
g_usart_config.usart_periph = USART1;
|
||||
g_usart_config.gpio_af = GPIO_AF_1;
|
||||
g_usart_config.irq_type = USART1_IRQn;
|
||||
break;
|
||||
|
||||
case GD32E23XF8:
|
||||
g_mcu_type = GD32E23XF8;
|
||||
/* Configure for USART1 (GD32E23XF8) */
|
||||
g_usart_config.rcu_usart = RCU_USART1;
|
||||
g_usart_config.usart_periph = USART1;
|
||||
g_usart_config.gpio_af = GPIO_AF_1;
|
||||
g_usart_config.irq_type = USART1_IRQn;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Default to GD32E23XF8 configuration */
|
||||
g_mcu_type = GD32E23XF8;
|
||||
g_usart_config.rcu_usart = RCU_USART1;
|
||||
g_usart_config.usart_periph = USART1;
|
||||
g_usart_config.gpio_af = GPIO_AF_1;
|
||||
g_usart_config.irq_type = USART1_IRQn;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief get current MCU type
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval MCU type (GD32E23XF4, GD32E23XF6, or GD32E23XF8)
|
||||
*/
|
||||
uint8_t get_mcu_type(void)
|
||||
{
|
||||
return g_mcu_type;
|
||||
}
|
@@ -16,6 +16,7 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include "gd32e23x_usart.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#undef errno
|
||||
extern int errno;
|
||||
@@ -164,7 +165,7 @@ int _execve(char *name, char **argv, char **env)
|
||||
// USART0 printf重定向实现
|
||||
int __io_putchar(int ch) {
|
||||
// 等待发送缓冲区空
|
||||
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(USART0, (uint8_t)ch);
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, (uint8_t)ch);
|
||||
return ch;
|
||||
}
|
||||
|
@@ -8,12 +8,12 @@
|
||||
void rs485_init(void) {
|
||||
|
||||
#ifndef RS485_MAX13487
|
||||
/* 使能 GPIOA 和 USART0 时钟 */
|
||||
/* 使能 GPIOA 和 USART 时钟 */
|
||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
||||
rcu_periph_clock_enable(RS485_RCU);
|
||||
|
||||
/* 配置 PA2 为 USART0_TX,PA3 为 USART0_RX */
|
||||
gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN);
|
||||
/* 配置 PA2 为 USART_TX,PA3 为 USART_RX */
|
||||
gpio_af_set(RS485_GPIO_PORT, RS485_GPIO_AF, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN);
|
||||
|
||||
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
|
||||
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_TX_PIN | RS485_RX_PIN);
|
||||
@@ -37,7 +37,7 @@ void rs485_init(void) {
|
||||
|
||||
usart_enable(RS485_PHY);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
nvic_irq_enable(RS485_IRQ, 0);
|
||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
||||
// usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
||||
|
||||
|
Reference in New Issue
Block a user