diff options
| author | Leeto1970 <[email protected]> | 2026-07-07 10:34:04 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2026-07-14 19:55:12 +0800 |
| commit | d513da02db889d1925edd8a579e90bca382c85cc (patch) | |
| tree | bd0057e0e6a3b86524f2542823614e2edebf1ae0 /bsp/hc32/tests | |
| parent | f8a65e4092621917d53eaaca1191a7fa0f7f89bf (diff) | |
[HC32] Add HC32F4A2/HC32F467 BSP support
- Add chip macro for driver
- Modify 'IDLE_THREAD_STACK_SIZE' to 512
- Modify 'HWTIMER' to 'CLOCK_TIMER'
- Use 'clang-format' format driver and BSP code
- Fix SPI clock division configuration
Diffstat (limited to 'bsp/hc32/tests')
28 files changed, 894 insertions, 671 deletions
diff --git a/bsp/hc32/tests/SConscript b/bsp/hc32/tests/SConscript index 3733761301..74d279fda2 100644 --- a/bsp/hc32/tests/SConscript +++ b/bsp/hc32/tests/SConscript @@ -15,7 +15,9 @@ if GetDepend(['RT_USING_SERIAL']): src += ['test_uart_v1.c'] if GetDepend(['BSP_USING_SPI']): - src += ['test_spi.c'] + # don't add test file when use RT_USB_DEVICE_MSTORAGE + if not (GetDepend(['BSP_USING_USBD']) and GetDepend(['RT_USB_DEVICE_MSTORAGE'])): + src += ['test_spi.c'] if GetDepend(['BSP_USING_QSPI']): src += ['test_qspi.c'] @@ -66,6 +68,7 @@ if GetDepend(['BSP_USING_INPUT_CAPTURE']): if GetDepend(['BSP_USING_PM']): src += ['test_pm.c'] + src += ['test_wktm.c'] if GetDepend('BSP_USING_HWCRYPTO'): src += ['test_crypto.c'] diff --git a/bsp/hc32/tests/test_adc.c b/bsp/hc32/tests/test_adc.c index abe95b0c9d..5eb8ad3b57 100644 --- a/bsp/hc32/tests/test_adc.c +++ b/bsp/hc32/tests/test_adc.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 */ /* @@ -22,29 +23,33 @@ #ifdef BSP_USING_ADC -#define REFER_VOLTAGE 3300 /* 参考电压 3.3V,单位mv */ -#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */ +#define REFER_VOLTAGE 3300 /* 参考电压 3.3V,单位mv */ +#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */ /* ADC Channel Max */ -#if defined (HC32F460) - #define ADC1_CH_MAX (16U) - #define ADC2_CH_MAX (8U) -#elif defined (HC32F472) - #define ADC1_CH_MAX (21U) - #define ADC2_CH_MAX (21U) - #define ADC3_CH_MAX (22U) -#elif defined (HC32F4A0) || defined (HC32F4A8) - #define ADC1_CH_MAX (16U) - #define ADC2_CH_MAX (16U) - #define ADC3_CH_MAX (20U) -#elif defined (HC32F448) - #define ADC1_CH_MAX (16U) - #define ADC2_CH_MAX (8U) - #define ADC3_CH_MAX (12U) -#elif defined (HC32F334) - #define ADC1_CH_MAX (16U) - #define ADC2_CH_MAX (12U) - #define ADC3_CH_MAX (10U) +#if defined(HC32F460) +#define ADC1_CH_MAX (16U) +#define ADC2_CH_MAX (8U) +#elif defined(HC32F472) +#define ADC1_CH_MAX (21U) +#define ADC2_CH_MAX (21U) +#define ADC3_CH_MAX (22U) +#elif defined(HC32F467) +#define ADC1_CH_MAX (16U) +#define ADC2_CH_MAX (16U) +#define ADC3_CH_MAX (16U) +#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) +#define ADC1_CH_MAX (16U) +#define ADC2_CH_MAX (16U) +#define ADC3_CH_MAX (20U) +#elif defined(HC32F448) +#define ADC1_CH_MAX (16U) +#define ADC2_CH_MAX (8U) +#define ADC3_CH_MAX (12U) +#elif defined(HC32F334) +#define ADC1_CH_MAX (16U) +#define ADC2_CH_MAX (12U) +#define ADC3_CH_MAX (10U) #endif @@ -59,7 +64,7 @@ rt_err_t adc_dma_trig_config(void) { stc_tmr0_init_t stcTmr0Init; -#if defined(HC32F460) || defined(HC32F4A0) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || defined (HC32F334) +#if defined(HC32F460) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F334) || defined(HC32F467) FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_1, ENABLE); #endif (void)TMR0_StructInit(&stcTmr0Init); @@ -105,7 +110,7 @@ static int adc_vol_sample(int argc, char **argv) rt_strcpy(adc_device, "adc2"); adc_max_channel = ADC2_CH_MAX; } -#if defined (HC32F472) || defined (HC32F4A0) || defined (HC32F448) || defined (HC32F4A8) || defined (HC32F334) +#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F334) || defined(HC32F467) else if (0 == rt_strcmp(argv[1], "adc3")) { rt_strcpy(adc_device, "adc3"); @@ -131,8 +136,8 @@ static int adc_vol_sample(int argc, char **argv) /* DMA配置 */ adc_priv.flag = ADC_USING_EOCA_DMA_FLAG; priv_ops.dma_trig_config = &adc_dma_trig_config; - priv_ops.dma_trig_start = &adc_dma_trig_start; - priv_ops.dma_trig_stop = &adc_dma_trig_stop; + priv_ops.dma_trig_start = &adc_dma_trig_start; + priv_ops.dma_trig_stop = &adc_dma_trig_stop; adc_priv.ops = &priv_ops; adc_dev->parent.user_data = &adc_priv; #endif @@ -148,7 +153,7 @@ static int adc_vol_sample(int argc, char **argv) /* 转换为对应电压值 */ vol = value * REFER_VOLTAGE / CONVERT_BITS; rt_kprintf("Simulate voltage is :%d mv\r\n", vol); - vol = rt_adc_voltage(adc_dev, adc_channel); + vol = rt_adc_voltage(adc_dev, adc_channel); rt_kprintf("Read voltage is :%d mv\r\n", vol); rt_kprintf("*********************\r\n"); } @@ -157,5 +162,5 @@ static int adc_vol_sample(int argc, char **argv) } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(adc_vol_sample, adc convert sample: select < adc1 | adc2 | adc3 >); +MSH_CMD_EXPORT(adc_vol_sample, adc convert sample : select<adc1 | adc2 | adc3>); #endif diff --git a/bsp/hc32/tests/test_can.c b/bsp/hc32/tests/test_can.c index b96471eaca..3eeccfc255 100644 --- a/bsp/hc32/tests/test_can.c +++ b/bsp/hc32/tests/test_can.c @@ -10,16 +10,23 @@ /* * 功能 -* 展示 CAN1、CAN2、CAN3 接收消息和回发消息。 -* 代码使用方法 -* 在终端执行:can_sample 参数选择:can1 | can2 | can3 以启动CAN收发测试 +* 测试 can 和 mcan 接收消息和回发消息 +* 注:mcan 仅部分系列MCU支持(参考宏定义MCAN_DEV_CNT) +* +* 测试方法 +* 连接 can 测试设备与MCU can(mcan): +* 注:MCU can(mcan)通信引脚的配置位于 board_config.h, +* 若测试单元的通信引脚未配置,需测试人员自行添加,并于board_config.c中做初始化 +* 初始化测试: 在终端按需执行:can_sample canx 或 can_sample mcany +* 其中x和y是单元号, x = 1 ~ CAN_DEV_CNT, y = 1 ~ MCAN_DEV_CNT +* 测试:can 测试设备发送满足过滤条件的消息(见后文:接收和发送消息) +* 终端打印接收到的ID和消息,并将消息原样发回给测试设备。 * * 默认波特率 * 仲裁段:波特率500K,采样率80% * 数据段:波特率为4M,采样率80% (仅支持CAN FD的单元) * * 接收和发送消息 -* CAN1: * 仅接收满足以下过滤条件的消息,并发送接收到的消息 * 1)标准帧:match ID:0x100~0x1ff * 2)扩展帧:match ID:0x12345100~0x123451ff @@ -57,13 +64,29 @@ #include "rtdevice.h" #include "drv_can.h" -#define MSH_USAGE_CAN_SAMPLE "can_sample <can1 | can2 | mcan1 | mcan2> - open can device and test\n" -#define MSH_USAGE_CAN_SET_BAUD "can set_baud <baud> - set can baud\n" -#define MSH_USAGE_CAN_SET_BAUDFD "can set_baudfd <baudfd> - set can baudfd\n" -#define MSH_USAGE_CAN_SET_BITTIMING "can set_bittiming <count> <rt_can_bit_timing_arbitration> <rt_can_bit_timing_data> - set can bit timing,\n" -#define MSH_USAGE_CAN_SEND_MSG "can send_msg \n" +#if defined(HC32F452) || defined(HC32F460) +#define CAN_DEV_CNT (1) +#elif defined(HC32F472) +#define CAN_DEV_CNT (3) +#elif defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) +#define CAN_DEV_CNT (2) +#endif + +#if defined(HC32F334) || defined(HC32F336) || defined(HC32F448) || defined(HC32F4A8) +#define MCAN_DEV_CNT (2) +#elif defined(HC32K118) +#define MCAN_DEV_CNT (1) +#elif defined(HC32F558) +#define MCAN_DEV_CNT (3) +#endif + +#define MSH_USAGE_CAN_SAMPLE "can_sample <can1 | can2 | mcan1 | mcan2> - open can device and test\n" +#define MSH_USAGE_CAN_SET_BAUD "can set_baud <baud> - set can baud\n" +#define MSH_USAGE_CAN_SET_BAUDFD "can set_baudfd <baudfd> - set can baudfd\n" +#define MSH_USAGE_CAN_SET_BITTIMING "can set_bittiming <count> <rt_can_bit_timing_arbitration> <rt_can_bit_timing_data> - set can bit timing,\n" +#define MSH_USAGE_CAN_SEND_MSG "can send_msg \n" -#define MSH_RESULT_STR(result) ((result == RT_EOK) ? "success" : "failure") +#define MSH_RESULT_STR(result) ((result == RT_EOK) ? "success" : "failure") static rt_device_t can_dev = RT_NULL; static struct rt_semaphore can_rx_sem; @@ -71,15 +94,18 @@ static rt_mutex_t can_mutex = RT_NULL; static rt_thread_t rx_thread; #ifdef RT_CAN_USING_CANFD -static const uint8_t mcan_data_size[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64}; +static const uint8_t mcan_data_size[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64 }; #endif -#define CAN_IF_INIT() do { \ - if (can_dev == RT_NULL || can_mutex == RT_NULL) { \ - rt_kprintf("failed! please first execute can_sample cmd!\n"); \ - return; \ - } \ - } while (0) +#define CAN_IF_INIT() \ + do \ + { \ + if (can_dev == RT_NULL || can_mutex == RT_NULL) \ + { \ + rt_kprintf("failed! please first execute can_sample cmd!\n"); \ + return; \ + } \ + } while (0) static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size) { @@ -90,13 +116,12 @@ static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size) static void _set_default_filter(void) { #ifdef RT_CAN_USING_HDR - struct rt_can_filter_item can_items[3] = - { + struct rt_can_filter_item can_items[3] = { RT_CAN_FILTER_ITEM_INIT(0x100, RT_CAN_STDID, RT_CAN_DTR, 1, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,过滤表模式为1(0表示标识符列表模式,1表示标识符屏蔽位模式),hdr = -1(表示不指定过滤表号),设置默认过滤表,过滤表回调函数和参数均为NULL */ RT_CAN_FILTER_ITEM_INIT(0x12345100, RT_CAN_EXTID, RT_CAN_DTR, 1, 0xFFFFFF00, RT_NULL, RT_NULL), /* ext,match ID:0x12345100~0x123451ff,hdr = -1 */ - {0x555, RT_CAN_STDID, RT_CAN_DTR, 1, 0x7ff, 7} /* std,match ID:0x555,hdr= 7,指定设置7号过滤表 */ + { 0x555, RT_CAN_STDID, RT_CAN_DTR, 1, 0x7ff, 7 } /* std,match ID:0x555,hdr= 7,指定设置7号过滤表 */ }; - struct rt_can_filter_config cfg = {3, 1, can_items}; /* 一共有3个过滤表,1表示初始化过滤表控制块 */ + struct rt_can_filter_config cfg = { 3, 1, can_items }; /* 一共有3个过滤表,1表示初始化过滤表控制块 */ rt_err_t res; res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg); RT_ASSERT(res == RT_EOK); @@ -121,8 +146,8 @@ static uint8_t _get_can_data_bytes_len(uint32_t dlc) static void can_rx_thread(void *parameter) { - struct rt_can_msg rxmsg = {0}; - rt_size_t size; + struct rt_can_msg rxmsg = { 0 }; + rt_size_t size; uint8_t data_len; while (1) @@ -173,7 +198,7 @@ static void _msh_cmd_set_baud(int argc, char **argv) } #ifdef RT_CAN_USING_CANFD -void _msh_cmd_set_timing(int argc, char **argv) +static void _msh_cmd_set_timing(int argc, char **argv) { rt_err_t result; @@ -190,17 +215,17 @@ void _msh_cmd_set_timing(int argc, char **argv) struct rt_can_bit_timing_config cfg; uint32_t pos = 3; items[0].prescaler = atoi(argv[pos++]); - items[0].num_seg1 = atoi(argv[pos++]); - items[0].num_seg2 = atoi(argv[pos++]); - items[0].num_sjw = atoi(argv[pos++]); + items[0].num_seg1 = atoi(argv[pos++]); + items[0].num_seg2 = atoi(argv[pos++]); + items[0].num_sjw = atoi(argv[pos++]); items[0].num_sspoff = atoi(argv[pos++]); if (count > 1) { - items[1].prescaler = atoi(argv[pos++]); - items[1].num_seg1 = atoi(argv[pos++]); - items[1].num_seg2 = atoi(argv[pos++]); - items[1].num_sjw = atoi(argv[pos++]); - items[1].num_sspoff = atoi(argv[pos]); + items[1].prescaler = atoi(argv[pos++]); + items[1].num_seg1 = atoi(argv[pos++]); + items[1].num_seg2 = atoi(argv[pos++]); + items[1].num_sjw = atoi(argv[pos++]); + items[1].num_sspoff = atoi(argv[pos]); } cfg.count = count; cfg.items = items; @@ -218,7 +243,7 @@ void _msh_cmd_set_timing(int argc, char **argv) } } -void _msh_cmd_set_baudfd(int argc, char **argv) +static void _msh_cmd_set_baudfd(int argc, char **argv) { rt_err_t result; @@ -239,10 +264,10 @@ void _msh_cmd_set_baudfd(int argc, char **argv) } #endif -void _msh_cmd_send_msg(int argc, char **argv) +static void _msh_cmd_send_msg(int argc, char **argv) { - rt_size_t size; - struct rt_can_msg msg = {0}; + rt_size_t size; + struct rt_can_msg msg = { 0 }; uint8_t u8Tick; if (argc == 2) @@ -250,7 +275,7 @@ void _msh_cmd_send_msg(int argc, char **argv) CAN_IF_INIT(); rt_mutex_take(can_mutex, RT_WAITING_FOREVER); #ifdef RT_CAN_USING_CANFD - msg.id = 0x300; + msg.id = 0x300; msg.ide = RT_CAN_STDID; msg.rtr = RT_CAN_DTR; msg.len = 0xFU; @@ -261,7 +286,7 @@ void _msh_cmd_send_msg(int argc, char **argv) msg.data[u8Tick] = u8Tick + 1 + 0xA0; } #else - msg.id = 0x300; + msg.id = 0x300; msg.ide = RT_CAN_STDID; msg.rtr = RT_CAN_DTR; #ifdef BSP_USING_MCAN @@ -291,7 +316,7 @@ void _msh_cmd_send_msg(int argc, char **argv) } } -void _show_usage(void) +static void _show_usage(void) { rt_kprintf("Usage: \n"); rt_kprintf(MSH_USAGE_CAN_SET_BAUD); @@ -405,4 +430,4 @@ int can_sample(int argc, char **argv) return -RT_ERROR; } } -MSH_CMD_EXPORT(can_sample, can sample: select < can1 | can2 | mcan1 | mcan2 >); +MSH_CMD_EXPORT(can_sample, can sample : select<can1 | can2 | mcan1 | mcan2>); diff --git a/bsp/hc32/tests/test_cherryusb.c b/bsp/hc32/tests/test_cherryusb.c index d8223826e7..568b0697b5 100644 --- a/bsp/hc32/tests/test_cherryusb.c +++ b/bsp/hc32/tests/test_cherryusb.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2025-08-08 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-03 CDT Support HC32F467 */ #include <rtthread.h> #include <rtdevice.h> @@ -15,18 +17,18 @@ */ #if defined(RT_CHERRYUSB_HOST) && defined(RT_CHERRYUSB_DEVICE) - #if defined(HC32F4A0) || defined(HC32F4A8) - #define TEST_USBH_CORE_BASE (CM_USBFS_BASE) - #define TEST_USBD_CORE_BASE (CM_USBHS_BASE) - #else - #error "Only one USB role can be selected" - #endif +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) +#define TEST_USBH_CORE_BASE (CM_USBFS_BASE) +#define TEST_USBD_CORE_BASE (CM_USBHS_BASE) #else - #if defined(RT_CHERRYUSB_HOST) - #define TEST_USBH_CORE_BASE (CM_USBFS_BASE) - #elif defined(RT_CHERRYUSB_DEVICE) - #define TEST_USBD_CORE_BASE (CM_USBFS_BASE) - #endif +#error "Only one USB role can be selected" +#endif +#else +#if defined(RT_CHERRYUSB_HOST) +#define TEST_USBH_CORE_BASE (CM_USBFS_BASE) +#elif defined(RT_CHERRYUSB_DEVICE) +#define TEST_USBD_CORE_BASE (CM_USBFS_BASE) +#endif #endif #if defined(RT_CHERRYUSB_HOST) @@ -34,8 +36,7 @@ #if defined(RT_CHERRYUSB_HOST_CDC_ECM) || defined(RT_CHERRYUSB_HOST_CDC_RNDIS) || defined(RT_CHERRYUSB_HOST_MSC) /* 使用USB Host 时,应确保主机对设备供电充足 - menuconfig: ECM 关键配置 - + ************************* menuconfig: ECM 关键配置 ************************* RT-Thread Kernel --->[*] Enable soft timer with a timer thread (4096) The stack size of timer thread @@ -55,25 +56,11 @@ ... [*] Enable ping features - 备注:CherryUSB Host枚举设备时,默认选择Configuration 1,若指定设备(如CH397A模组CDC-ECM模式)需要选择Configuration 2,需在 - components/drivers/usb/cherryusb/core/usbh_core.c文件中usbh_enumerate()函数内添加如下代码: - int usbh_enumerate(struct usbh_hubport *hport) - { - ... - config_index = 0; - // Add code start - if((0x1A86 == ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idVendor) && \ - (0x5397 == ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idProduct)) { - config_index = 1; // For CH397, we need to select configuration 2 - } - // Add code end - USB_LOG_DBG("The device selects config %d\r\n", config_index); - ... - } - + 备注:CherryUSB Host枚举设备时,默认选择Configuration 1,若指定设备(如CH397A模组CDC-ECM模式)需要选择Configuration 2,重新定义 + uint8_t usbh_get_hport_active_config_index(struct usbh_hubport *hport)。 - menuconfig: MSC 关键配置 + ************************* menuconfig: MSC 关键配置 ************************* RT-Thread Kernel --->[*] Enable soft timer with a timer thread (4096) The stack size of timer thread @@ -84,7 +71,6 @@ [*] Enable usb msc driver ... (/)usb host dfs mount point - */ @@ -107,7 +93,7 @@ msh />ping www.baidu.com */ static int cherryusb_host_init(void) { - usbh_initialize(0, TEST_USBH_CORE_BASE); + usbh_initialize(0, TEST_USBH_CORE_BASE, RT_NULL); return 0; } INIT_APP_EXPORT(cherryusb_host_init); @@ -119,6 +105,18 @@ void ipconfig(void) list_if(); } MSH_CMD_EXPORT(ipconfig, list network interface information); + +uint8_t usbh_get_hport_active_config_index(struct usbh_hubport *hport) +{ + uint8_t config_index = 0U; /* Default to configuration index 0 */ + + if ((0x1A86U == hport->device_desc.idVendor) && (0x5397U == hport->device_desc.idProduct)) + { + config_index = 1U; /* For CH397, we need to select configuration 2 */ + } + + return config_index; +} #endif #endif @@ -127,18 +125,17 @@ MSH_CMD_EXPORT(ipconfig, list network interface information); #if defined(RT_CHERRYUSB_DEVICE) #if defined(RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM) /* - menuconfig:关键配置 - + ************************* menuconfig: ACM 关键配置 ************************* RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB [*] Enable usb device mode - Selectot usb host ip.... ---> + Selectot usb device ip.... ---> [*]dwc2_hc [*] Enable usb cdc acm device Select usb device template...---> [*] cdc acm */ -static int cherryusb_device_cdc_acm_init(void) +static int cherryusb_device_cdc_acm_init(void) { extern void cdc_acm_init(uint8_t busid, uint32_t reg_base); cdc_acm_init(0, TEST_USBD_CORE_BASE); diff --git a/bsp/hc32/tests/test_clock_timer.c b/bsp/hc32/tests/test_clock_timer.c index b5536b9bed..649bf35a4f 100644 --- a/bsp/hc32/tests/test_clock_timer.c +++ b/bsp/hc32/tests/test_clock_timer.c @@ -151,7 +151,8 @@ static int clock_timer_sample(int argc, char *argv[]) } /* 确保oneshot模式cb函数执行一次后才关闭定时器 */ - while (cb_run == RT_FALSE); + while (cb_run == RT_FALSE) + ; cb_run = RT_FALSE; /* close */ @@ -160,5 +161,5 @@ static int clock_timer_sample(int argc, char *argv[]) return ret; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(clock_timer_sample, clock_timer sample: devname [oneshot | period] timeout); +MSH_CMD_EXPORT(clock_timer_sample, clock_timer sample : devname[oneshot | period] timeout); #endif diff --git a/bsp/hc32/tests/test_crypto.c b/bsp/hc32/tests/test_crypto.c index 439433723a..9eff8fa527 100644 --- a/bsp/hc32/tests/test_crypto.c +++ b/bsp/hc32/tests/test_crypto.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-03 CDT Support HC32F467 */ #include <rtthread.h> @@ -15,15 +17,17 @@ #if defined(BSP_USING_HWCRYPTO) -#define ARR_SIZE(arr) (sizeof(arr)/sizeof(*arr)) -#define PRINT_DIGIT_ARR(arr) do { \ - rt_kprintf("%s: ", #arr); \ - for(int i = 0; i < ARR_SIZE(arr); i++) \ - rt_kprintf("%d ", arr[i]); \ - rt_kprintf("\n"); \ - } while(0) +#define ARR_SIZE(arr) (sizeof(arr) / sizeof(*arr)) +#define PRINT_DIGIT_ARR(arr) \ + do \ + { \ + rt_kprintf("%s: ", #arr); \ + for (int i = 0; i < ARR_SIZE(arr); i++) \ + rt_kprintf("%d ", arr[i]); \ + rt_kprintf("\n"); \ + } while (0) -#define WDT_DEVICE_NAME "crypto" +#define WDT_DEVICE_NAME "crypto" static void _crypto_cmd_print_usage(void) { @@ -44,7 +48,8 @@ static void _crypto_cmd_print_usage(void) rt_kprintf(" aes: test aes module. \n"); #if defined(HC32F460) rt_kprintf(" e.g. msh >crypto_sample aes 128 \n"); -#elif defined (HC32F4A0) || defined (HC32F448) || defined (HC32F472) || defined (HC32F4A8) +#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F472) || defined(HC32F4A8) || \ + defined(HC32F467) rt_kprintf(" e.g. msh >crypto_sample aes 128/192/256 \n"); #endif #endif @@ -58,23 +63,22 @@ static void _crypto_cmd_print_usage(void) #if defined(BSP_USING_CRC) /* menuconfig: - Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto ---> - [*]Enable Hardeware CRC + Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers ---> + [*]Using Hardeware CRC * CRC16命令调用:crypto_sample crc 16 * CRC32命令调用:crypto_sample crc 32 * 程序功能:打印CRC输入数据和计数结果,使用第三方软件计算数据,再做比较 */ -#define CRC16_WIDTH 16U -#define CRC32_WIDTH 32U +#define CRC16_WIDTH 16U +#define CRC32_WIDTH 32U static void crc_test(rt_uint32_t width) { - rt_uint8_t temp_in[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + rt_uint8_t temp_in[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; struct rt_hwcrypto_ctx *ctx; rt_uint32_t result = 0; /* CRC16_X25 */ - struct hwcrypto_crc_cfg cfg = - { + struct hwcrypto_crc_cfg cfg = { .last_val = 0xFFFFU, .poly = 0x1021U, .width = CRC16_WIDTH, @@ -110,19 +114,27 @@ static void crc_test(rt_uint32_t width) } rt_kprintf("\n"); result = rt_hwcrypto_crc_update(ctx, temp_in, sizeof(temp_in) / 2U); - rt_kprintf("crc%d result: 0x%x \n", width, result); + rt_kprintf("crc%d expect_result: 0x%X \n", width, (width == 16) ? 0x0A38 : 0x515Ad3CC); + rt_kprintf(" calc_result: 0x%X \n\n", result); /* Accumulate test */ PRINT_DIGIT_ARR(temp_in); result = rt_hwcrypto_crc_update(ctx, &temp_in[sizeof(temp_in) / 2U], sizeof(temp_in) / 2U); - rt_kprintf("crc%d result: 0x%x \n", width, result); + rt_kprintf("crc%d expect_result: 0x%X \n", width, (width == 16) ? 0x2FE2 : 0x456CD746); + rt_kprintf(" calc_result: 0x%X \n\n", result); rt_hwcrypto_crc_destroy(ctx); } #endif #if defined(BSP_USING_AES) -#define AES_DATA_LEN 32U /* data of length must be a multiple of 16(128 Bit) */ +/* menuconfig: + Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers ---> + [*]Using Hardeware AES + * AES命令调用:crypto_sample aes 128/192/256 + * 程序功能:打印明文、密文、密文解密后的数据 + */ +#define AES_DATA_LEN 32U /* data of length must be a multiple of 16(128 Bit) */ static void aes_test(rt_uint16_t key_bitlen) { rt_uint32_t result = RT_EOK; @@ -135,23 +147,21 @@ static void aes_test(rt_uint16_t key_bitlen) const char *key256 = "1234567890abcdefghijklmnopqrstuv"; const char *key; rt_uint8_t type_max = 1U; - hwcrypto_type types[] = - { + hwcrypto_type types[] = { HWCRYPTO_TYPE_AES_ECB, HWCRYPTO_TYPE_AES_CBC, HWCRYPTO_TYPE_AES_CTR, HWCRYPTO_TYPE_AES_CFB, HWCRYPTO_TYPE_AES_OFB, }; - const char *type_str[] = - { + const char *type_str[] = { "aes_ecb", "aes_cbc", "aes_ctr", "aes_cfb", "aes_ofb", }; -#if defined (HC32F4A8) +#if defined(HC32F4A8) type_max = 5U; #endif for (int i = 0; i < type_max; i++) @@ -178,7 +188,7 @@ static void aes_test(rt_uint16_t key_bitlen) break; } result = rt_hwcrypto_symmetric_setkey(ctx, (rt_uint8_t *)key, key_bitlen); -#if defined (HC32F4A8) +#if defined(HC32F4A8) const char *iv = "1234567812345678"; result = rt_hwcrypto_symmetric_setiv(ctx, (const rt_uint8_t *)iv, strlen(iv)); #endif @@ -214,9 +224,9 @@ static void aes_test(rt_uint16_t key_bitlen) { rt_kprintf("%c", dec_out[i]); } - rt_kprintf("\n"); + rt_kprintf("\n\n"); -_exit: + _exit: rt_hwcrypto_symmetric_destroy(ctx); } } @@ -224,11 +234,21 @@ _exit: #endif #if defined(BSP_USING_HASH) -#define HASH_SHA256_MSG_DIGEST_SIZE (32U) +/* menuconfig: + Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers ---> + [*]Using Hardeware Hash + * Hash命令调用:crypto_sample hash test + * 程序功能:打印hash原始消息、期望的SHA256结果和计算的SHA256结果 + */ +#define HASH_SHA256_MSG_DIGEST_SIZE (32U) static void hash_sha256_test(void) { const char *in = "0123456789abcdefghijklmnopqrstuvwxyz"; - uint8_t out[HASH_SHA256_MSG_DIGEST_SIZE]; + uint8_t calc_out[HASH_SHA256_MSG_DIGEST_SIZE]; + const uint8_t expect_out[HASH_SHA256_MSG_DIGEST_SIZE] = { 0x74, 0xE7, 0xE5, 0xBB, 0x9D, 0x22, 0xD6, 0xDB, 0x26, 0xBF, + 0x76, 0x94, 0x6D, 0x40, 0xFF, 0xF3, 0xEA, 0x9F, 0x03, 0x46, + 0xB8, 0x84, 0xFD, 0x06, 0x94, 0x92, 0x0F, 0xCC, 0xFA, 0xD1, + 0x5E, 0x33 }; struct rt_hwcrypto_ctx *ctx; ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA256); @@ -242,13 +262,20 @@ static void hash_sha256_test(void) } rt_kprintf("\n"); - rt_hwcrypto_hash_finish(ctx, out, HASH_SHA256_MSG_DIGEST_SIZE); - rt_kprintf("hash out data:"); + rt_hwcrypto_hash_finish(ctx, calc_out, HASH_SHA256_MSG_DIGEST_SIZE); + rt_kprintf("hash out expect_data:"); for (int i = 0; i < HASH_SHA256_MSG_DIGEST_SIZE; i++) { - rt_kprintf("%x ", out[i]); + rt_kprintf("%x ", expect_out[i]); } rt_kprintf("\n"); + + rt_kprintf("hash out calc_data:"); + for (int i = 0; i < HASH_SHA256_MSG_DIGEST_SIZE; i++) + { + rt_kprintf("%x ", calc_out[i]); + } + rt_kprintf("\n\n"); rt_hwcrypto_hash_destroy(ctx); } } @@ -269,7 +296,7 @@ static int crypto_sample(int argc, char *argv[]) if (!rt_strcmp("get", argv[2])) { rt_uint32_t result = rt_hwcrypto_rng_update(); - rt_kprintf("random number = %x \n", result); + rt_kprintf("random number = %x \n\n", result); } else { @@ -330,6 +357,6 @@ _exit: return -RT_ERROR; } -MSH_CMD_EXPORT(crypto_sample, crypto [option]); +MSH_CMD_EXPORT(crypto_sample, crypto[option]); #endif diff --git a/bsp/hc32/tests/test_dac.c b/bsp/hc32/tests/test_dac.c index 9821993bfa..02f218edc5 100644 --- a/bsp/hc32/tests/test_dac.c +++ b/bsp/hc32/tests/test_dac.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT support HC32F4A2 + * 2026-06-05 CDT support HC32F467 */ /* @@ -21,11 +23,11 @@ #ifdef BSP_USING_DAC -#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/ -#define DAC_MAX_OUTPUT_VALUE 4095 +#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/ +#define DAC_MAX_OUTPUT_VALUE 4095 -#if (defined (HC32F4A8) || defined (HC32F4A0)) && defined (BSP_USING_DAC2) - extern void EthPhyDisable(void); +#if (defined(HC32F4A8) || defined(HC32F4A0) || defined(HC32F4A2)) && defined(BSP_USING_DAC2) +extern void EthPhyDisable(void); #endif /* HC32F4A8 && BSP_USING_DAC2 */ static int dac_vol_sample(int argc, char *argv[]) @@ -47,18 +49,18 @@ static int dac_vol_sample(int argc, char *argv[]) rt_strcpy(dac_device_name, "dac1"); max_channel = 2; } -#if defined (HC32F4A0) || defined (HC32F472) || defined (HC32F4A8) || defined (HC32F334) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F467) || defined(HC32F472) || defined(HC32F4A8) || defined(HC32F334) else if (0 == rt_strcmp(argv[1], "dac2")) { rt_strcpy(dac_device_name, "dac2"); -#if defined (HC32F4A0) || defined (HC32F472) || defined (HC32F4A8) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F467) || defined(HC32F472) || defined(HC32F4A8) max_channel = 2; -#elif defined (HC32F334) +#elif defined(HC32F334) max_channel = 1; #endif } #endif -#if defined (HC32F472) +#if defined(HC32F472) else if (0 == rt_strcmp(argv[1], "dac3")) { rt_strcpy(dac_device_name, "dac3"); @@ -76,7 +78,7 @@ static int dac_vol_sample(int argc, char *argv[]) return -RT_ERROR; } } -#if (defined (HC32F4A8) || defined (HC32F4A0)) && defined (BSP_USING_DAC2) +#if (defined(HC32F4A8) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F467)) && defined(BSP_USING_DAC2) EthPhyDisable(); #endif /* 查找设备 */ @@ -117,5 +119,5 @@ static int dac_vol_sample(int argc, char *argv[]) return ret; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(dac_vol_sample, dac voltage convert sample < dac1 | dac2 value >); +MSH_CMD_EXPORT(dac_vol_sample, dac voltage convert sample<dac1 | dac2 value>); #endif diff --git a/bsp/hc32/tests/test_eth.c b/bsp/hc32/tests/test_eth.c index ac72665192..2fa3bde119 100644 --- a/bsp/hc32/tests/test_eth.c +++ b/bsp/hc32/tests/test_eth.c @@ -29,7 +29,7 @@ * ETH Communication USING MII * 3)拨码开关J33拨到MII端,编译下载、运行代码 * 4)等待msh> - * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.120 icmp_seq=0 ttl=128 time=1 ms) + * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.10 icmp_seq=0 ttl=128 time=1 ms) * 6)msh>窗口输入命令:eth_webserver * 7)PC打开浏览器,输入IP地址:192.168.1.30再按回车键,显示lwip的简介网页(lwIP - A Lightweight TCP/IP Stack), * 表示成功访问目标板的HTTP服务器。 @@ -41,7 +41,7 @@ * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable ETH PHY interrupt mode: (16) ETH PHY Interrupt pin number * 3)拨码开关J33拨到MII端,编译下载、运行代码 * 4)等待msh> - * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.120 icmp_seq=0 ttl=128 time=1 ms) + * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.10 icmp_seq=0 ttl=128 time=1 ms) * 6)msh>窗口输入命令:eth_webserver * 7)PC打开浏览器,输入IP地址:192.168.1.30再按回车键,显示lwip的简介网页(lwIP - A Lightweight TCP/IP Stack), * 表示成功访问目标板的HTTP服务器。 @@ -52,10 +52,12 @@ * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable Ethernet: ETH Communication USING RMII * 3)拨码开关J33拨到RMII端,编译下载、运行代码 * 4)等待msh> - * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.120 icmp_seq=0 ttl=128 time=1 ms) + * 5)msh>窗口输入命令:ping 192.168.1.10,显示连接正常(60 bytes from 192.168.1.10 icmp_seq=0 ttl=128 time=1 ms) * 6)msh>窗口输入命令:eth_webserver * 7)PC打开浏览器,输入IP地址:192.168.1.30再按回车键,显示lwip的简介网页(lwIP - A Lightweight TCP/IP Stack), * 表示成功访问目标板的HTTP服务器。 + * + * 注意:HC32F467仅支持case 3,且步骤3不需要拨码。 */ #include <rtthread.h> @@ -69,6 +71,6 @@ void eth_webserver(void) rt_kprintf("Initialize the httpd...... \r\n"); httpd_init(); } -MSH_CMD_EXPORT(eth_webserver, eth: start web server); +MSH_CMD_EXPORT(eth_webserver, eth : start web server); #endif diff --git a/bsp/hc32/tests/test_fal.c b/bsp/hc32/tests/test_fal.c index 6acb246a4a..b06e7bdc8c 100644 --- a/bsp/hc32/tests/test_fal.c +++ b/bsp/hc32/tests/test_fal.c @@ -16,7 +16,8 @@ * menuconfig: * RT-Thread Components ---> FAL: flash abstraction layer * ---> Device Drivers ---> Using SPI Bus/Device device drivers ---> Using Serial Flash Universal Driver - * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable on-chip FLASH + * Hardware Drivers Config ---> Onboard On-chip Peripheral Drivers ----> Enable on-chip FLASH + * NOTE: 忽略‘[E/SFUD] ERROR: Flash device w25q64 not found!’错误,不影响测试 */ #include <rtthread.h> #include <rtdevice.h> @@ -26,15 +27,15 @@ #include "board.h" #include <fal.h> -#define FAL_PART_NAME "app" -#define TEST_BUF_SIZE 1024UL -#define TEST_RW_CNT 32UL +#define FAL_PART_NAME "app" +#define TEST_BUF_SIZE 1024UL +#define TEST_RW_CNT 32UL -#define TEST_RW_START_ADDR HC32_FLASH_END_ADDRESS - (TEST_BUF_SIZE * TEST_RW_CNT) +#define TEST_RW_START_ADDR HC32_FLASH_END_ADDRESS - (TEST_BUF_SIZE * TEST_RW_CNT) -static uint8_t write_buffer[TEST_BUF_SIZE] = {0}; -static uint8_t read_buffer[TEST_BUF_SIZE] = {0}; +static uint8_t write_buffer[TEST_BUF_SIZE] = { 0 }; +static uint8_t read_buffer[TEST_BUF_SIZE] = { 0 }; static int fal_sample(int argc, char **argv) @@ -108,8 +109,7 @@ static int fal_sample(int argc, char **argv) for (int i = 0; i < TEST_BUF_SIZE; i++) { #if defined(HC32F460) - if ((j == (TEST_RW_CNT - 1)) && (i >= (TEST_BUF_SIZE - 32)) ? - (read_buffer[i] != 0xFF) : (read_buffer[i] != write_buffer[i])) + if ((j == (TEST_RW_CNT - 1)) && (i >= (TEST_BUF_SIZE - 32)) ? (read_buffer[i] != 0xFF) : (read_buffer[i] != write_buffer[i])) #else if (read_buffer[i] != write_buffer[i]) #endif diff --git a/bsp/hc32/tests/test_gpio.c b/bsp/hc32/tests/test_gpio.c index 9107abe83f..af6d7a59c6 100644 --- a/bsp/hc32/tests/test_gpio.c +++ b/bsp/hc32/tests/test_gpio.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 */ /* @@ -27,20 +28,24 @@ * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable TCA9539 */ #if defined(HC32F460) - #define LED1_PIN_NUM GET_PIN(D, 3) /* LED0 */ - #define KEY1_PIN_NUM GET_PIN(B, 1) /* K10 */ -#elif defined(HC32F4A0) || defined(HC32F4A8) - #define LED1_PIN_NUM GET_PIN(B, 11) /* LED10 */ - #define KEY1_PIN_NUM GET_PIN(A, 0) /* K10 */ +#define LED_PIN_NUM GET_PIN(D, 3) /* LED0 */ +#define KEY_PIN_NUM GET_PIN(B, 1) /* K10 */ +#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) +#define LED_PIN_NUM GET_PIN(B, 11) /* LED10 */ +#define KEY_PIN_NUM GET_PIN(A, 0) /* K10 */ #elif defined(HC32F448) - #define LED1_PIN_NUM GET_PIN(A, 2) /* LED3 */ - #define KEY1_PIN_NUM GET_PIN(B, 6) /* K5 */ +#define LED_PIN_NUM GET_PIN(A, 2) /* LED3 */ +#define KEY_PIN_NUM GET_PIN(B, 6) /* K5 */ #elif defined(HC32F472) - #define LED1_PIN_NUM GET_PIN(C, 9) /* LED5 */ - #define KEY1_PIN_NUM GET_PIN(B, 5) /* K10 */ +#define LED_PIN_NUM GET_PIN(C, 9) /* LED5 */ +#define KEY_PIN_NUM GET_PIN(B, 5) /* K10 */ #elif defined(HC32F334) - #define LED1_PIN_NUM GET_PIN(C, 13) /* LED1 */ - #define KEY1_PIN_NUM GET_PIN(C, 3) /* K1 */ +#define LED_PIN_NUM GET_PIN(C, 13) /* LED1 */ +#define KEY_PIN_NUM GET_PIN(C, 3) /* K1 */ +#elif defined(HC32F467) + /* NOTE: NEED short J13 to use LED11. */ +#define LED_PIN_NUM GET_PIN(C, 9) /* LED11 */ +#define KEY_PIN_NUM GET_PIN(A, 0) /* K5 */ #endif static uint8_t u8LedState = 1; @@ -50,29 +55,29 @@ void led_control(void *args) u8LedState = !u8LedState; if (0 == u8LedState) { - rt_pin_write(LED1_PIN_NUM, PIN_LOW); + rt_pin_write(LED_PIN_NUM, PIN_LOW); } else { - rt_pin_write(LED1_PIN_NUM, PIN_HIGH); + rt_pin_write(LED_PIN_NUM, PIN_HIGH); } } static void pin_sample(void) { /* LED引脚为输出模式 */ - rt_pin_mode(LED1_PIN_NUM, PIN_MODE_OUTPUT); + rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT); /* 默认高电平 */ - rt_pin_write(LED1_PIN_NUM, PIN_HIGH); + rt_pin_write(LED_PIN_NUM, PIN_HIGH); /* 按键1引脚为输入模式 */ - rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP); + rt_pin_mode(KEY_PIN_NUM, PIN_MODE_INPUT_PULLUP); /* 绑定中断,下降沿模式,回调函数名为led_control */ - // rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_RISING, led_control, RT_NULL); - // rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, led_control, RT_NULL); - rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_RISING_FALLING, led_control, RT_NULL); + // rt_pin_attach_irq(KEY_PIN_NUM, PIN_IRQ_MODE_RISING, led_control, RT_NULL); + // rt_pin_attach_irq(KEY_PIN_NUM, PIN_IRQ_MODE_FALLING, led_control, RT_NULL); + rt_pin_attach_irq(KEY_PIN_NUM, PIN_IRQ_MODE_RISING_FALLING, led_control, RT_NULL); /* 使能中断 */ - rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE); + rt_pin_irq_enable(KEY_PIN_NUM, PIN_IRQ_ENABLE); } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(pin_sample, pin sample); diff --git a/bsp/hc32/tests/test_i2c.c b/bsp/hc32/tests/test_i2c.c index ecfea2f100..c7d572f324 100644 --- a/bsp/hc32/tests/test_i2c.c +++ b/bsp/hc32/tests/test_i2c.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 */ /* @@ -30,47 +31,49 @@ #define USING_RT_I2C_TRANSFER /* defined EEPROM */ -#if defined(HC32F472) || defined(HC32F460) || defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8) || \ - defined(HC32F334) - #define EE_DEV_ADDR 0x50 - #define EE_TEST_PAGE_CNT 8 // Test 8 pages +#if defined(HC32F472) || defined(HC32F460) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || \ + defined(HC32F4A8) || defined(HC32F334) || defined(HC32F467) +#define EE_DEV_ADDR 0x50 +#define EE_TEST_PAGE_CNT 8 // Test 8 pages #endif /* define EEPROM hardware */ -#if defined(HC32F472) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F334) - #define EE24C256 -#elif defined(HC32F4A0) - #define EE24C02 +#if defined(HC32F472) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) || \ + defined(HC32F334) || defined(HC32F467) +#define EE24C256 +#elif defined(HC32F4A0) || defined(HC32F4A2) +#define EE24C02 #endif -#if defined (EE24C1024) - #define EE_PAGE_SIZE 256 // 24C1024 - #define EE_WORD_ADR_SIZE 2 // 2 word addr -#elif defined (EE24C256) - #define EE_PAGE_SIZE 64 // 24C256 - #define EE_WORD_ADR_SIZE 2 // 2 word addr -#elif defined (EE24C02) - #define EE_PAGE_SIZE 8 // 24C02 - #define EE_WORD_ADR_SIZE 1 // 1 word addr +#if defined(EE24C1024) +#define EE_PAGE_SIZE 256 // 24C1024 +#define EE_WORD_ADR_SIZE 2 // 2 word addr +#elif defined(EE24C256) +#define EE_PAGE_SIZE 64 // 24C256 +#define EE_WORD_ADR_SIZE 2 // 2 word addr +#elif defined(EE24C02) +#define EE_PAGE_SIZE 8 // 24C02 +#define EE_WORD_ADR_SIZE 1 // 1 word addr #endif /* device information */ -#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F334) - #define HW_I2C_DEV "i2c1" - #define SW_I2C_DEV "i2c1_sw" +#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || \ + defined(HC32F4A8) || defined(HC32F334) || defined(HC32F467) +#define HW_I2C_DEV "i2c1" +#define SW_I2C_DEV "i2c1_sw" #elif defined(HC32F460) - #define HW_I2C_DEV "i2c3" - #define SW_I2C_DEV "i2c1_sw" +#define HW_I2C_DEV "i2c3" +#define SW_I2C_DEV "i2c1_sw" #endif /* this API is for eeprom size is smaller than 256Bytes */ static void eeprom_page_write(uint32_t page, uint8_t *pBuf) { struct rt_i2c_bus_device *hc32_i2c = RT_NULL; - uint8_t TxBuf[EE_PAGE_SIZE + EE_WORD_ADR_SIZE] = {0}; + uint8_t TxBuf[EE_PAGE_SIZE + EE_WORD_ADR_SIZE] = { 0 }; struct rt_i2c_msg msg[1]; -#if defined (BSP_USING_I2C_HW) +#if defined(BSP_USING_I2C_HW) hc32_i2c = rt_i2c_bus_device_find(HW_I2C_DEV); //hw i2c #else hc32_i2c = rt_i2c_bus_device_find(SW_I2C_DEV); //sw i2c @@ -90,10 +93,10 @@ static void eeprom_page_write(uint32_t page, uint8_t *pBuf) { TxBuf[i + EE_WORD_ADR_SIZE] = *pBuf++; } - msg[0].addr = EE_DEV_ADDR; + msg[0].addr = EE_DEV_ADDR; msg[0].flags = RT_I2C_WR; - msg[0].len = EE_PAGE_SIZE + EE_WORD_ADR_SIZE; - msg[0].buf = TxBuf; + msg[0].len = EE_PAGE_SIZE + EE_WORD_ADR_SIZE; + msg[0].buf = TxBuf; #if defined(USING_RT_I2C_TRANSFER) rt_i2c_transfer(hc32_i2c, &msg[0], 1); @@ -113,32 +116,29 @@ static void eeprom_page_read(uint32_t page, uint8_t *pBuf) struct rt_i2c_msg msg[2]; #endif -#if defined (BSP_USING_I2C_HW) +#if defined(BSP_USING_I2C_HW) hc32_i2c = rt_i2c_bus_device_find(HW_I2C_DEV); //hw i2c #else hc32_i2c = rt_i2c_bus_device_find(SW_I2C_DEV); //sw i2c #endif - if (EE_WORD_ADR_SIZE == 2) - { - readAddr[0] = (page * EE_PAGE_SIZE) / 256; // addrH - readAddr[1] = page * EE_PAGE_SIZE; // addrL - } - else - { - readAddr[0] = page * EE_PAGE_SIZE; - } +#if (EE_WORD_ADR_SIZE == 2) + readAddr[0] = (page * EE_PAGE_SIZE) / 256; // addrH + readAddr[1] = page * EE_PAGE_SIZE; // addrL +#else + readAddr[0] = page * EE_PAGE_SIZE; +#endif #if defined(USING_RT_I2C_TRANSFER) - msg[0].addr = EE_DEV_ADDR; + msg[0].addr = EE_DEV_ADDR; msg[0].flags = RT_I2C_WR; - msg[0].len = EE_WORD_ADR_SIZE; - msg[0].buf = readAddr; + msg[0].len = EE_WORD_ADR_SIZE; + msg[0].buf = readAddr; - msg[1].addr = EE_DEV_ADDR; + msg[1].addr = EE_DEV_ADDR; msg[1].flags = RT_I2C_RD; - msg[1].len = EE_PAGE_SIZE; - msg[1].buf = pBuf; + msg[1].len = EE_PAGE_SIZE; + msg[1].buf = pBuf; rt_i2c_transfer(hc32_i2c, &msg[0], 2); #else rt_i2c_master_send(hc32_i2c, EE_DEV_ADDR, RT_I2C_NO_STOP, readAddr, EE_WORD_ADR_SIZE); @@ -191,19 +191,20 @@ void eeprom_test(void) } /* TCA9539 device */ -#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8) +#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || \ + defined(HC32F4A8) || defined(HC32F467) /* TCA9539 define */ -#define TCA9539_DEV_ADDR (0x74) // TCA9539 chip address on I2C bus +#define TCA9539_DEV_ADDR (0x74) // TCA9539 chip address on I2C bus -#define TCA9539_REG_INPUT_PORT0 (0x00U) -#define TCA9539_REG_INPUT_PORT1 (0x01U) -#define TCA9539_REG_OUTPUT_PORT0 (0x02U) -#define TCA9539_REG_OUTPUT_PORT1 (0x03U) -#define TCA9539_REG_INVERT_PORT0 (0x04U) -#define TCA9539_REG_INVERT_PORT1 (0x05U) -#define TCA9539_REG_CONFIG_PORT0 (0x06U) -#define TCA9539_REG_CONFIG_PORT1 (0x07U) +#define TCA9539_REG_INPUT_PORT0 (0x00U) +#define TCA9539_REG_INPUT_PORT1 (0x01U) +#define TCA9539_REG_OUTPUT_PORT0 (0x02U) +#define TCA9539_REG_OUTPUT_PORT1 (0x03U) +#define TCA9539_REG_INVERT_PORT0 (0x04U) +#define TCA9539_REG_INVERT_PORT1 (0x05U) +#define TCA9539_REG_CONFIG_PORT0 (0x06U) +#define TCA9539_REG_CONFIG_PORT1 (0x07U) void tca9539_test(void) { @@ -212,7 +213,7 @@ void tca9539_test(void) static rt_uint8_t send_buf1[0x10], recv_buf1[0x10]; struct rt_i2c_msg msg[2]; -#if defined (BSP_USING_I2C_HW) +#if defined(BSP_USING_I2C_HW) hc32_i2c = rt_i2c_bus_device_find(HW_I2C_DEV); //hw i2c #else hc32_i2c = rt_i2c_bus_device_find(SW_I2C_DEV); //sw i2c @@ -221,31 +222,31 @@ void tca9539_test(void) send_buf0[0] = TCA9539_REG_CONFIG_PORT1; send_buf0[1] = 0xFF; - msg[0].addr = TCA9539_DEV_ADDR; + msg[0].addr = TCA9539_DEV_ADDR; msg[0].flags = RT_I2C_WR; - msg[0].len = 2; - msg[0].buf = send_buf0; + msg[0].len = 2; + msg[0].buf = send_buf0; rt_i2c_transfer(hc32_i2c, &msg[0], 1); send_buf0[0] = TCA9539_REG_OUTPUT_PORT1; send_buf0[1] = 0xAC; - msg[1].addr = TCA9539_DEV_ADDR; + msg[1].addr = TCA9539_DEV_ADDR; msg[1].flags = RT_I2C_WR; - msg[1].len = 2; - msg[1].buf = send_buf0; + msg[1].len = 2; + msg[1].buf = send_buf0; rt_i2c_transfer(hc32_i2c, &msg[1], 1); /* read */ send_buf1[0] = TCA9539_REG_OUTPUT_PORT1; - msg[0].addr = TCA9539_DEV_ADDR; + msg[0].addr = TCA9539_DEV_ADDR; msg[0].flags = RT_I2C_WR; - msg[0].len = 1; - msg[0].buf = send_buf1; + msg[0].len = 1; + msg[0].buf = send_buf1; - msg[1].addr = TCA9539_DEV_ADDR; + msg[1].addr = TCA9539_DEV_ADDR; msg[1].flags = RT_I2C_RD; - msg[1].len = 1; - msg[1].buf = recv_buf1; + msg[1].len = 1; + msg[1].buf = recv_buf1; rt_i2c_transfer(hc32_i2c, &msg[0], 2); if (recv_buf1[0] == 0xAC) @@ -262,7 +263,8 @@ void tca9539_test(void) static void i2c_sample(int argc, char *argv[]) { eeprom_test(); -#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8) +#if defined(HC32F472) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F4A8) || \ + defined(HC32F467) tca9539_test(); #endif } diff --git a/bsp/hc32/tests/test_nand.c b/bsp/hc32/tests/test_nand.c index 5958ba9338..9b2790818c 100644 --- a/bsp/hc32/tests/test_nand.c +++ b/bsp/hc32/tests/test_nand.c @@ -14,7 +14,7 @@ * 程序功能:对整个Nand存储空间进行擦除、写和读操作,比较数据是否一致 * * 注意: - * F4A0: 修改函数SystemClock_Config,调用函数CLK_SetClockDiv参数,CLK_EXCLK_DIV2改为CLK_EXCLK_DIV4; + * F4A0/F4A2: 修改函数SystemClock_Config,调用函数CLK_SetClockDiv参数,CLK_EXCLK_DIV2改为CLK_EXCLK_DIV4; * * menuconfig: * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable EXMC ----> Using SDRAM or NAND ----> Using NAND @@ -28,7 +28,7 @@ #if defined(BSP_USING_EXMC) && defined(BSP_USING_NAND) #include "nand_port.h" -#define NAND_DEVICE_NAME "nand" +#define NAND_DEVICE_NAME "nand" static rt_err_t nand_read_id(struct rt_mtd_nand_device *mtd_nand) { diff --git a/bsp/hc32/tests/test_pm.c b/bsp/hc32/tests/test_pm.c index cb334e2b55..b76e125e9e 100644 --- a/bsp/hc32/tests/test_pm.c +++ b/bsp/hc32/tests/test_pm.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-04 CDT Support HC32F467 */ /* @@ -41,106 +43,106 @@ #if defined(BSP_USING_PM) -#if defined (HC32F4A0) || defined (HC32F4A8) - #define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) - #define BSP_KEY_PORT (GPIO_PORT_A) - #define BSP_KEY_PIN (GPIO_PIN_00) - #define BSP_KEY_EXTINT (EXTINT_CH00) - #define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ0) - #define BSP_KEY_IRQn (INT001_IRQn) - #define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH0) - #define BSP_KEY_EVT (EVT_SRC_PORT_EIRQ0) - #define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP0) - #define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP00) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) +#define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) +#define BSP_KEY_PORT (GPIO_PORT_A) +#define BSP_KEY_PIN (GPIO_PIN_00) +#define BSP_KEY_EXTINT (EXTINT_CH00) +#define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ0) +#define BSP_KEY_IRQn (INT001_IRQn) +#define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH0) +#define BSP_KEY_EVT (INTC_EVT1) +#define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP0) +#define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP00) - #define LED_GREEN_PORT (GPIO_PORT_C) - #define LED_GREEN_PIN (GPIO_PIN_09) +#define LED_GREEN_PORT (GPIO_PORT_C) +#define LED_GREEN_PIN (GPIO_PIN_09) - #define MCO_PORT (GPIO_PORT_A) - #define MCO_PIN (GPIO_PIN_08) - #define MCO_GPIO_FUNC (GPIO_FUNC_1) +#define MCO_PORT (GPIO_PORT_A) +#define MCO_PIN (GPIO_PIN_08) +#define MCO_GPIO_FUNC (GPIO_FUNC_1) -#elif defined (HC32F460) - #define PLL_SRC ((CM_CMU->PLLCFGR & CMU_PLLCFGR_PLLSRC) >> CMU_PLLCFGR_PLLSRC_POS) - #define BSP_KEY_PORT (GPIO_PORT_B) /* Key10 */ - #define BSP_KEY_PIN (GPIO_PIN_01) - #define BSP_KEY_EXTINT (EXTINT_CH01) - #define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ1) - #define BSP_KEY_IRQn (INT001_IRQn) - #define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH1) - #define BSP_KEY_EVT (EVT_SRC_PORT_EIRQ1) - #define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) - #define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP01) +#elif defined(HC32F460) +#define PLL_SRC ((CM_CMU->PLLCFGR & CMU_PLLCFGR_PLLSRC) >> CMU_PLLCFGR_PLLSRC_POS) +#define BSP_KEY_PORT (GPIO_PORT_B) /* Key10 */ +#define BSP_KEY_PIN (GPIO_PIN_01) +#define BSP_KEY_EXTINT (EXTINT_CH01) +#define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ1) +#define BSP_KEY_IRQn (INT001_IRQn) +#define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH1) +#define BSP_KEY_EVT (INTC_EVT1) +#define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) +#define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP01) - #define LED_GREEN_PORT (GPIO_PORT_D) - #define LED_GREEN_PIN (GPIO_PIN_04) +#define LED_GREEN_PORT (GPIO_PORT_D) +#define LED_GREEN_PIN (GPIO_PIN_04) - #define MCO_PORT (GPIO_PORT_A) - #define MCO_PIN (GPIO_PIN_08) - #define MCO_GPIO_FUNC (GPIO_FUNC_1) +#define MCO_PORT (GPIO_PORT_A) +#define MCO_PIN (GPIO_PIN_08) +#define MCO_GPIO_FUNC (GPIO_FUNC_1) -#elif defined (HC32F448) - #define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) - #define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ - #define BSP_KEY_PIN (GPIO_PIN_06) - #define BSP_KEY_EXTINT (EXTINT_CH06) - #define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ6) - #define BSP_KEY_IRQn (INT001_IRQn) - #define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH6) - #define BSP_KEY_EVT (EVT_SRC_PORT_EIRQ6) - #define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) - #define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP12) +#elif defined(HC32F448) +#define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) +#define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ +#define BSP_KEY_PIN (GPIO_PIN_06) +#define BSP_KEY_EXTINT (EXTINT_CH06) +#define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ6) +#define BSP_KEY_IRQn (INT001_IRQn) +#define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH6) +#define BSP_KEY_EVT (INTC_EVT1) +#define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) +#define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP12) - #define LED_GREEN_PORT (GPIO_PORT_A) - #define LED_GREEN_PIN (GPIO_PIN_02) +#define LED_GREEN_PORT (GPIO_PORT_A) +#define LED_GREEN_PIN (GPIO_PIN_02) - #define MCO_PORT (GPIO_PORT_A) - #define MCO_PIN (GPIO_PIN_08) - #define MCO_GPIO_FUNC (GPIO_FUNC_1) +#define MCO_PORT (GPIO_PORT_A) +#define MCO_PIN (GPIO_PIN_08) +#define MCO_GPIO_FUNC (GPIO_FUNC_1) -#elif defined (HC32F472) - #define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) - #define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ - #define BSP_KEY_PIN (GPIO_PIN_05) - #define BSP_KEY_EXTINT (EXTINT_CH05) - #define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ5) - #define BSP_KEY_IRQn (INT001_IRQn) - #define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH5) - #define BSP_KEY_EVT (EVT_SRC_PORT_EIRQ5) - #define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) - #define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP11) +#elif defined(HC32F472) +#define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) +#define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ +#define BSP_KEY_PIN (GPIO_PIN_05) +#define BSP_KEY_EXTINT (EXTINT_CH05) +#define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ5) +#define BSP_KEY_IRQn (INT001_IRQn) +#define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH5) +#define BSP_KEY_EVT (INTC_EVT1) +#define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP1) +#define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP11) - #define LED_GREEN_PORT (GPIO_PORT_C) - #define LED_GREEN_PIN (GPIO_PIN_09) +#define LED_GREEN_PORT (GPIO_PORT_C) +#define LED_GREEN_PIN (GPIO_PIN_09) -#elif defined (HC32F334) - #define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) - #define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ - #define BSP_KEY_PIN (GPIO_PIN_09) - #define BSP_KEY_EXTINT (EXTINT_CH09) - #define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ9) - #define BSP_KEY_IRQn (INT001_IRQn) - #define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH9) - #define BSP_KEY_EVT (EVT_SRC_PORT_EIRQ9) - #define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP2) - #define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP21) +#elif defined(HC32F334) +#define PLL_SRC ((CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLSRC) >> CMU_PLLHCFGR_PLLSRC_POS) +#define BSP_KEY_PORT (GPIO_PORT_B) /* Key5 */ +#define BSP_KEY_PIN (GPIO_PIN_09) +#define BSP_KEY_EXTINT (EXTINT_CH09) +#define BSP_KEY_INT_SRC (INT_SRC_PORT_EIRQ9) +#define BSP_KEY_IRQn (INT001_IRQn) +#define BSP_KEY_INTC_STOP_WKUP_EXTINT (INTC_STOP_WKUP_EXTINT_CH9) +#define BSP_KEY_EVT (INTC_EVT1) +#define BSP_KEY_PWC_PD_WKUP_TRIG_WKUP (PWC_PD_WKUP_TRIG_WKUP2) +#define BSP_KEY_PWC_PD_WKUP_WKUP (PWC_PD_WKUP_WKUP21) - #define LED_GREEN_PORT (GPIO_PORT_C) - #define LED_GREEN_PIN (GPIO_PIN_13) +#define LED_GREEN_PORT (GPIO_PORT_C) +#define LED_GREEN_PIN (GPIO_PIN_13) #endif -#define KEYCNT_BACKUP_ADDR (uint32_t *)(0x200F0010) -#define KEYCNT_CMD_SLEEP_NONE (0) -#define KEYCNT_CMD_SLEEP_IDLE (1) -#define KEYCNT_CMD_SLEEP_DEEP (3) -#define KEYCNT_CMD_SLEEP_STANDBY (5) -#define KEYCNT_CMD_SLEEP_SHUTDOWN (7) +#define KEYCNT_BACKUP_ADDR (uint32_t *)(0x200F0010) +#define KEYCNT_CMD_SLEEP_NONE (0) +#define KEYCNT_CMD_SLEEP_IDLE (1) +#define KEYCNT_CMD_SLEEP_DEEP (3) +#define KEYCNT_CMD_SLEEP_STANDBY (5) +#define KEYCNT_CMD_SLEEP_SHUTDOWN (7) #define PM_DBG #if defined PM_DBG - #define pm_dbg rt_kprintf +#define pm_dbg rt_kprintf #else - #define pm_dbg +#define pm_dbg #endif static volatile uint32_t g_keycnt_cmd; @@ -188,7 +190,7 @@ static void _key_int_init(void) /* IRQ sign-in */ stcIrqSignConfig.enIntSrc = BSP_KEY_INT_SRC; - stcIrqSignConfig.enIRQn = BSP_KEY_IRQn; + stcIrqSignConfig.enIRQn = BSP_KEY_IRQn; stcIrqSignConfig.pfnCallback = KEY_IrqHandler; (void)INTC_IrqSignIn(&stcIrqSignConfig); @@ -198,10 +200,10 @@ static void _key_int_init(void) NVIC_EnableIRQ(stcIrqSignConfig.enIRQn); } - static void _wkup_cfg_sleep_deep() { INTC_WakeupSrcCmd(BSP_KEY_INTC_STOP_WKUP_EXTINT, ENABLE); + INTC_EventCmd(BSP_KEY_EVT, ENABLE); } static void _wkup_cfg_sleep_standby(void) @@ -232,7 +234,7 @@ static void _sleep_enter_event_deep(void) static void _sleep_enter_event_standby(void) { _wkup_cfg_sleep_standby(); -#if defined (HC32F4A0) || defined (HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) PWC_BKR_Write(0, g_keycnt_cmd & 0xFF); #endif *KEYCNT_BACKUP_ADDR = g_keycnt_cmd; @@ -257,7 +259,7 @@ static void _sleep_exit_event_idle(void) static void _sleep_exit_event_deep(void) { -#if defined (HC32F460) +#if defined(HC32F460) PWC_STOP_ClockRecover(); #endif rt_pm_release(PM_SLEEP_MODE_DEEP); @@ -266,8 +268,7 @@ static void _sleep_exit_event_deep(void) } typedef void (*notify)(void); -static notify sleep_enter_func[PM_SLEEP_MODE_MAX] = -{ +static notify sleep_enter_func[PM_SLEEP_MODE_MAX] = { RT_NULL, _sleep_enter_event_idle, RT_NULL, @@ -276,8 +277,7 @@ static notify sleep_enter_func[PM_SLEEP_MODE_MAX] = _sleep_enter_event_shutdown, }; -static notify sleep_exit_func[PM_SLEEP_MODE_MAX] = -{ +static notify sleep_exit_func[PM_SLEEP_MODE_MAX] = { RT_NULL, _sleep_exit_event_idle, RT_NULL, @@ -286,16 +286,18 @@ static notify sleep_exit_func[PM_SLEEP_MODE_MAX] = RT_NULL, }; -static void _notify_func(uint8_t event, uint8_t mode, void *data) +static void _notify_func(uint8_t event, uint8_t mode, void *data) { if (event == RT_PM_ENTER_SLEEP) { - SysTick_Suspend(); if (sleep_enter_func[mode] == RT_NULL) { return; } - GPIO_ResetPins(LED_GREEN_PORT, LED_GREEN_PIN); + /* shutdown LED before enter sleep mode to decrease power consumption */ + rt_pin_write(LED_GREEN_PIN, PIN_LOW); + + SysTick_Suspend(); sleep_enter_func[mode](); } else @@ -319,8 +321,8 @@ static void pm_cmd_handler(void *parameter) while (1) { - if ((KEYCNT_CMD_SLEEP_IDLE == g_keycnt_cmd) || (KEYCNT_CMD_SLEEP_DEEP == g_keycnt_cmd) || \ - (KEYCNT_CMD_SLEEP_STANDBY == g_keycnt_cmd) || (KEYCNT_CMD_SLEEP_SHUTDOWN == g_keycnt_cmd)) + if ((KEYCNT_CMD_SLEEP_IDLE == g_keycnt_cmd) || (KEYCNT_CMD_SLEEP_DEEP == g_keycnt_cmd) || + (KEYCNT_CMD_SLEEP_STANDBY == g_keycnt_cmd) || (KEYCNT_CMD_SLEEP_SHUTDOWN == g_keycnt_cmd)) { switch (g_keycnt_cmd) { @@ -350,12 +352,12 @@ static void pm_cmd_handler(void *parameter) } } -#if defined(HC32F4A0) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) static void pm_run_main(void *parameter) { static rt_uint8_t run_index = 0; - char *speed[] = {"low", "high"}; - const rt_uint8_t run_mode[] = {PM_RUN_MODE_LOW_SPEED, PM_RUN_MODE_HIGH_SPEED}; + char *speed[] = { "low", "high" }; + const rt_uint8_t run_mode[] = { PM_RUN_MODE_LOW_SPEED, PM_RUN_MODE_HIGH_SPEED }; GPIO_SetFunc(MCO_PORT, MCO_PIN, MCO_GPIO_FUNC); /* Configure clock output system clock */ @@ -383,7 +385,7 @@ static void pm_run_main(void *parameter) static void _keycnt_cmd_init_after_power_on(void) { en_flag_status_t wkup_from_ptwk = PWC_PD_GetWakeupStatus(PWC_PD_WKUP_FLAG_WKUP0); -#if defined (HC32F4A0) || defined (HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) en_flag_status_t bakram_pd = PWC_BKR_GetStatus(PWC_BACKUP_RAM_FLAG_RAMPDF); uint8_t bkr0 = PWC_BKR_Read(0); @@ -421,7 +423,7 @@ static void _keycnt_cmd_init_after_power_on(void) pm_dbg("KEYCNT_BACKUP_ADDR addr =0x%p,value = %d\n", KEYCNT_BACKUP_ADDR, *KEYCNT_BACKUP_ADDR); pm_dbg("wkup_from_ptwk = %d\n", wkup_from_ptwk); -#if defined (HC32F4A0) || defined (HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) pm_dbg("bakram_pd = %d\n", bakram_pd); pm_dbg("bkr0 = %d\n", bkr0); #endif @@ -429,17 +431,17 @@ static void _keycnt_cmd_init_after_power_on(void) static void _vbat_init(void) { -#if defined (HC32F4A0) || defined (HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) while (PWC_BKR_GetStatus(PWC_BACKUP_RAM_FLAG_RAMVALID) == RESET) { rt_thread_delay(10); } FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_SRAMB, ENABLE); -#elif defined (HC32F448) || defined (HC32F334) +#elif defined(HC32F448) || defined(HC32F334) FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_SRAMB, ENABLE); -#elif defined (HC32F460) +#elif defined(HC32F460) FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_SRAMRET, ENABLE); -#elif defined (HC32F472) +#elif defined(HC32F472) FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_SRAMRET, ENABLE); #endif pm_dbg("vbat init success\n"); @@ -455,7 +457,7 @@ int pm_sample_init(void) rt_pm_notify_set(_notify_func, NULL); - rt_thread_t thread = rt_thread_create("pm_cmd_handler", pm_cmd_handler, RT_NULL, 1024, 25, 10); + rt_thread_t thread = rt_thread_create("pm_cmd_handler", pm_cmd_handler, RT_NULL, 1024, 25, 10); if (thread != RT_NULL) { rt_thread_startup(thread); @@ -465,7 +467,7 @@ int pm_sample_init(void) rt_kprintf("create pm sample thread failed!\n"); } -#if defined(HC32F4A0) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) +#if defined(HC32F467) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) thread = rt_thread_create("pm_run_main", pm_run_main, RT_NULL, 1024, 25, 10); if (thread != RT_NULL) { diff --git a/bsp/hc32/tests/test_pulse_encoder.c b/bsp/hc32/tests/test_pulse_encoder.c index 641e45d00f..16197f6847 100644 --- a/bsp/hc32/tests/test_pulse_encoder.c +++ b/bsp/hc32/tests/test_pulse_encoder.c @@ -6,11 +6,16 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-09 CDT Support HC32F467 */ /* - * 程序清单: Pulse encoder 设备使用例程, 请在图形化配置界面打开pulse encoder device, - * 并使能tmra_1和tmr6_1. + * 程序清单: Pulse encoder 设备使用例程 + * + * menuconfig: + * Hardware Drivers Config ---> On-Chip Peripheral Drivers ----> [*] Enable Pulse Encoder ----> [*] Use TIMERA As The Pulse Encoder ---> [*] Use TIMERA_1 As The Pulse Encoder + * [*] Use TIMER6 As The Pulse Encoder ---> [*] Use TIMER6_1 As The Pulse Encoder * 例程导出了 encoder_sample 命令到控制终端, 通过串口可查看当前的count数值 * 命令调用格式:pulse_encoder_sample devname [option1] [option2] * devname: [pulse_a1/pulse_61] 编码器单元名称 @@ -19,7 +24,6 @@ * eg:encoder_sample pulse_a1 2000 1000 * 编码器的分辨率是1000 * 硬件IO查看对应board/board_config.h中相关端口定义,并且需要正确连接到对应模拟脉冲生成的端口 - * 程序功能: */ #include <rtthread.h> @@ -30,19 +34,19 @@ #ifdef BSP_USING_PULSE_ENCODER -#if defined (HC32F4A0) || defined (HC32F4A8) - #define TEST_IO_A_PIN GET_PIN(A, 5) - #define TEST_IO_B_PIN GET_PIN(A, 6) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) +#define TEST_IO_A_PIN GET_PIN(A, 5) +#define TEST_IO_B_PIN GET_PIN(A, 6) #else - #define TEST_IO_A_PIN GET_PIN(B, 0) - #define TEST_IO_B_PIN GET_PIN(B, 1) +#define TEST_IO_A_PIN GET_PIN(B, 0) +#define TEST_IO_B_PIN GET_PIN(B, 1) #endif static rt_device_t pulse_encoder_dev = RT_NULL; static void printf_connect(void) { -#if defined (HC32F4A0) || defined (HC32F4A8) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) #if defined(BSP_USING_PULSE_ENCODER_TMRA_1) rt_kprintf(" [tmra]*connect PA5-->PA8 PA6-->PA9\n"); #endif @@ -50,7 +54,7 @@ static void printf_connect(void) rt_kprintf(" [tmr6]*connect PA5-->PB9 PA6-->PB8\n"); #endif #endif -#if defined (HC32F460) +#if defined(HC32F460) #if defined(BSP_USING_PULSE_ENCODER_TMRA_1) rt_kprintf(" [tmra]*connect PB0-->PA8 PB1-->PA9\n"); #endif @@ -58,7 +62,7 @@ static void printf_connect(void) rt_kprintf(" [tmr6]*connect PB0-->PE9 PB1-->PE8\n"); #endif #endif -#if defined (HC32F448) +#if defined(HC32F448) #if defined(BSP_USING_PULSE_ENCODER_TMRA_1) rt_kprintf(" [tmra]*connect PB0-->PA8 PB1-->PA9\n"); #endif @@ -66,7 +70,7 @@ static void printf_connect(void) rt_kprintf(" [tmr6]*connect PB0-->PB5 PB1-->PB13\n"); #endif #endif -#if defined (HC32F472) +#if defined(HC32F472) #if defined(BSP_USING_PULSE_ENCODER_TMRA_1) rt_kprintf(" [tmra]*connect PB0-->PA0 PB1-->PA1\n"); #endif @@ -74,7 +78,7 @@ static void printf_connect(void) rt_kprintf(" [tmr6]*connect PB0-->PA3 PB1-->PA7\n"); #endif #endif -#if defined (HC32F334) +#if defined(HC32F334) #if defined(BSP_USING_PULSE_ENCODER_TMRA_1) rt_kprintf(" [tmra]*connect PB0-->PA0 PB1-->PA1\n"); #endif @@ -98,8 +102,8 @@ static void GenClkUp(const uint16_t cnt) { uint32_t i, j; rt_int32_t count; - const uint8_t bAin[4U] = {1U, 1U, 0U, 0U}; - const uint8_t bBin[4U] = {0U, 1U, 1U, 0U}; + const uint8_t bAin[4U] = { 1U, 1U, 0U, 0U }; + const uint8_t bBin[4U] = { 0U, 1U, 1U, 0U }; for (j = 0UL; j < cnt; j++) { for (i = 0UL; i < 4UL; i++) @@ -131,8 +135,8 @@ static void GenClkDown(const uint16_t cnt) { uint32_t i, j; rt_int32_t count; - const uint8_t bAin[4U] = {0U, 1U, 1U, 0U}; - const uint8_t bBin[4U] = {1U, 1U, 0U, 0U}; + const uint8_t bAin[4U] = { 0U, 1U, 1U, 0U }; + const uint8_t bBin[4U] = { 1U, 1U, 0U, 0U }; for (j = 0UL; j < cnt; j++) { for (i = 0UL; i < 4UL; i++) @@ -222,5 +226,5 @@ static int encoder_sample(int argc, char **argv) } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(encoder_sample, encoder sample devname [option1] [option2]); +MSH_CMD_EXPORT(encoder_sample, encoder sample devname[option1][option2]); #endif diff --git a/bsp/hc32/tests/test_pwm.c b/bsp/hc32/tests/test_pwm.c index d675dd8973..3004338b6c 100644 --- a/bsp/hc32/tests/test_pwm.c +++ b/bsp/hc32/tests/test_pwm.c @@ -21,7 +21,7 @@ #ifdef BSP_USING_PWM -#define PWM_DEV_CHANNEL 1 +#define PWM_DEV_CHANNEL 1 struct rt_device_pwm *pwm_dev; @@ -61,7 +61,7 @@ static rt_int32_t pwm_sample(int argc, char *argv[]) } } } -MSH_CMD_EXPORT(pwm_sample, pwm_sample [opt]) +MSH_CMD_EXPORT(pwm_sample, pwm_sample[opt]) #endif /* EOF diff --git a/bsp/hc32/tests/test_qspi.c b/bsp/hc32/tests/test_qspi.c index 66bd0106d7..24b50b70e8 100644 --- a/bsp/hc32/tests/test_qspi.c +++ b/bsp/hc32/tests/test_qspi.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-10 CDT Support HC32F467 */ /* @@ -24,73 +26,73 @@ #if defined(BSP_USING_QSPI) #include "drv_qspi.h" -#define W25Q_QSPI_DEVICE_NAME "qspi10" +#define W25Q_QSPI_DEVICE_NAME "qspi10" -#define W25Q_FLAG_BUSY (0x01) -#define W25Q_WR_ENABLE (0x06) -#define W25Q_SECTOR_ERASE (0x20) -#define W25Q_RD_STATUS_REG1 (0x05) -#define W25Q_PAGE_PROGRAM (0x02) -#define W25Q64_QUAD_INPUT_PAGE_PROGRAM (0x32) +#define W25Q_FLAG_BUSY (0x01) +#define W25Q_WR_ENABLE (0x06) +#define W25Q_SECTOR_ERASE (0x20) +#define W25Q_RD_STATUS_REG1 (0x05) +#define W25Q_PAGE_PROGRAM (0x02) +#define W25Q64_QUAD_INPUT_PAGE_PROGRAM (0x32) -#define W25Q_STD_RD (0x03) -#define W25Q_FAST_RD (0x0B) -#define W25Q_FAST_RD_DUAL_OUTPUT (0x3B) -#define W25Q_FAST_RD_DUAL_IO (0xBB) -#define W25Q_FAST_RD_QUAD_OUTPUT (0x6B) -#define W25Q_FAST_RD_QUAD_IO (0xEB) +#define W25Q_STD_RD (0x03) +#define W25Q_FAST_RD (0x0B) +#define W25Q_FAST_RD_DUAL_OUTPUT (0x3B) +#define W25Q_FAST_RD_DUAL_IO (0xBB) +#define W25Q_FAST_RD_QUAD_OUTPUT (0x6B) +#define W25Q_FAST_RD_QUAD_IO (0xEB) -#define W25Q64_RD_STATUS_REG1 (0x05) -#define W25Q64_WR_STATUS_REG1 (0x01) -#define W25Q64_RD_STATUS_REG2 (0x35) -#define W25Q64_WR_STATUS_REG2 (0x31) -#define W25Q64_RD_STATUS_REG3 (0x15) -#define W25Q64_WR_STATUS_REG3 (0x11) +#define W25Q64_RD_STATUS_REG1 (0x05) +#define W25Q64_WR_STATUS_REG1 (0x01) +#define W25Q64_RD_STATUS_REG2 (0x35) +#define W25Q64_WR_STATUS_REG2 (0x31) +#define W25Q64_RD_STATUS_REG3 (0x15) +#define W25Q64_WR_STATUS_REG3 (0x11) -#define W25Q_PAGE_SIZE (256UL) -#define W25Q_SECTOR_SIZE (1024UL * 4UL) -#define W25Q_PAGE_PER_SECTOR (W25Q_SECTOR_SIZE / W25Q_PAGE_SIZE) -#define W25Q_MAX_ADDR (0x800000UL) +#define W25Q_PAGE_SIZE (256UL) +#define W25Q_SECTOR_SIZE (1024UL * 4UL) +#define W25Q_PAGE_PER_SECTOR (W25Q_SECTOR_SIZE / W25Q_PAGE_SIZE) +#define W25Q_MAX_ADDR (0x800000UL) -#define W25Q_QSPI_DATA_LINE_WIDTH 1 -#define W25Q_QSPI_RD_MD (W25Q_FAST_RD_QUAD_IO) +#define W25Q_QSPI_DATA_LINE_WIDTH 1 +#define W25Q_QSPI_RD_MD (W25Q_FAST_RD_QUAD_IO) -#define W25Q_QSPI_WR_RD_ADDR 0x4000 -#define W25Q_QSPI_DATA_BUF_LEN 0x2000 -#define W25Q_QSPI_WR_CMD W25Q64_QUAD_INPUT_PAGE_PROGRAM +#define W25Q_QSPI_WR_RD_ADDR 0x4000 +#define W25Q_QSPI_DATA_BUF_LEN 0x2000 +#define W25Q_QSPI_WR_CMD W25Q64_QUAD_INPUT_PAGE_PROGRAM -#if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F472) - #ifndef BSP_QSPI_USING_SOFT_CS - #if (W25Q_QSPI_WR_CMD == W25Q64_QUAD_INPUT_PAGE_PROGRAM) - #error "QUAD PAGE PROGRAM must use soft CS pin!!" - #endif - #endif +#if defined(HC32F460) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F467) +#ifndef BSP_QSPI_USING_SOFT_CS +#if (W25Q_QSPI_WR_CMD == W25Q64_QUAD_INPUT_PAGE_PROGRAM) +#error "QUAD PAGE PROGRAM must use soft CS pin!!" +#endif +#endif #endif #if W25Q_QSPI_RD_MD == W25Q_STD_RD - #define W25Q_QSPI_RD_DUMMY_CYCLE 0 +#define W25Q_QSPI_RD_DUMMY_CYCLE 0 #elif W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO - #define W25Q_QSPI_RD_DUMMY_CYCLE 4 +#define W25Q_QSPI_RD_DUMMY_CYCLE 4 #elif W25Q_QSPI_RD_MD == W25Q_FAST_RD_QUAD_IO - #define W25Q_QSPI_RD_DUMMY_CYCLE 6 +#define W25Q_QSPI_RD_DUMMY_CYCLE 6 #else - #define W25Q_QSPI_RD_DUMMY_CYCLE 8 +#define W25Q_QSPI_RD_DUMMY_CYCLE 8 #endif #if (W25Q_QSPI_RD_MD == W25Q_FAST_RD_QUAD_IO) - #define W25Q_QSPI_ADDR_LINE 4 +#define W25Q_QSPI_ADDR_LINE 4 #elif (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO) - #define W25Q_QSPI_ADDR_LINE 2 +#define W25Q_QSPI_ADDR_LINE 2 #else - #define W25Q_QSPI_ADDR_LINE 1 +#define W25Q_QSPI_ADDR_LINE 1 #endif #if (W25Q_QSPI_RD_MD == W25Q_STD_RD) || (W25Q_QSPI_RD_MD == W25Q_FAST_RD) - #define W25Q_QSPI_DATA_LINE 1 +#define W25Q_QSPI_DATA_LINE 1 #elif (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_OUTPUT) || (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO) - #define W25Q_QSPI_DATA_LINE 2 +#define W25Q_QSPI_DATA_LINE 2 #else - #define W25Q_QSPI_DATA_LINE 4 +#define W25Q_QSPI_DATA_LINE 4 #endif @@ -105,9 +107,9 @@ static int rt_hw_qspi_flash_init(void) #ifndef BSP_QSPI_USING_SOFT_CS if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL)) #else -#if defined (HC32F472) +#if defined(HC32F472) if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", GET_PIN(B, 12), W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL)) -#elif defined (HC32F4A0) || defined (HC32F460) || defined (HC32F448) || defined (HC32F4A8) +#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F467) if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", GET_PIN(C, 7), W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL)) #endif #endif @@ -125,8 +127,8 @@ INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_init); void w25q_read_uid(struct rt_qspi_device *device) { rt_uint8_t w25x_read_uid = 0x4B; /* 命令 */ - rt_uint8_t u8UID[8] = {0}; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t u8UID[8] = { 0 }; + rt_uint8_t txBuf[5] = { 0 }; rt_memset(txBuf, 0xFF, 5); txBuf[0] = w25x_read_uid; @@ -147,8 +149,8 @@ int32_t w25q_check_process_done(struct rt_qspi_device *device, uint32_t u32Timeo { __IO uint32_t u32Count = 0U; int32_t i32Ret = LL_ERR_TIMEOUT; - rt_uint8_t rxBuf[5] = {0}; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t rxBuf[5] = { 0 }; + rt_uint8_t txBuf[5] = { 0 }; txBuf[0] = W25Q_RD_STATUS_REG1; while (u32Count < u32Timeout) @@ -206,7 +208,7 @@ rt_err_t bsp_qspi_send_then_recv(struct rt_qspi_device *device, const void *send else { /* no address stage */ - message.address.content = 0 ; + message.address.content = 0; message.address.qspi_lines = 0; message.address.size = 0; } @@ -247,7 +249,7 @@ rt_err_t bsp_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_s struct rt_qspi_message message; unsigned char *ptr = (unsigned char *)send_buf; - rt_size_t count = 0; + rt_size_t count = 0; rt_err_t result = 0; message.instruction.content = ptr[0]; @@ -276,16 +278,15 @@ rt_err_t bsp_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_s else { /* no address stage */ - message.address.content = 0 ; + message.address.content = 0; message.address.qspi_lines = 0; message.address.size = 0; } - } else { /* no address stage */ - message.address.content = 0 ; + message.address.content = 0; message.address.qspi_lines = 0; message.address.size = 0; } @@ -327,10 +328,9 @@ rt_err_t bsp_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_s } - void w25q_write_sr(struct rt_qspi_device *device, uint8_t reg, uint8_t value) { - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t txBuf[5] = { 0 }; txBuf[0] = W25Q_WR_ENABLE; if (1 != rt_qspi_send(device, txBuf, 1)) @@ -353,7 +353,7 @@ void w25q_write_sr(struct rt_qspi_device *device, uint8_t reg, uint8_t value) int32_t w25q_read_data(struct rt_qspi_device *device, uint32_t u32Addr, uint8_t *pu8ReadBuf, uint32_t u32Size) { int32_t i32Ret = LL_OK; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t txBuf[5] = { 0 }; txBuf[0] = W25Q_QSPI_RD_MD; txBuf[1] = (u32Addr >> 16) & 0xFFU; @@ -419,9 +419,9 @@ int32_t w25q_write_data(struct rt_qspi_device *device, uint32_t u32Addr, uint8_t break; } - u32Addr += u32TempSize; + u32Addr += u32TempSize; u32AddrOffset += u32TempSize; - u32Size -= u32TempSize; + u32Size -= u32TempSize; } return i32Ret; @@ -481,11 +481,11 @@ void w25q_write_read_data(struct rt_qspi_device *device, uint32_t u32Addr) { rt_kprintf("qspi erase sector failed!\n"); } - if (LL_OK != w25q_write_data(device, u32Addr, u8WrBuf, W25Q_QSPI_DATA_BUF_LEN)) + if (LL_OK != w25q_write_data(device, u32Addr, u8WrBuf, W25Q_QSPI_DATA_BUF_LEN)) { rt_kprintf("qspi write data failed!\n"); } - if (LL_OK != w25q_read_data(device, u32Addr, u8RdBuf, W25Q_QSPI_DATA_BUF_LEN)) + if (LL_OK != w25q_read_data(device, u32Addr, u8RdBuf, W25Q_QSPI_DATA_BUF_LEN)) { rt_kprintf("qspi read data failed!\n"); } @@ -502,14 +502,14 @@ void w25q_write_read_data(struct rt_qspi_device *device, uint32_t u32Addr) static void qspi_thread_entry(void *parameter) { rt_err_t ret; - struct rt_qspi_configuration qcfg = {0}; + struct rt_qspi_configuration qcfg = { 0 }; uint32_t u32Addr = W25Q_QSPI_WR_RD_ADDR; - qcfg.medium_size = W25Q_MAX_ADDR; - qcfg.qspi_dl_width = W25Q_QSPI_DATA_LINE_WIDTH; - qcfg.parent.mode = RT_SPI_MODE_0; - qcfg.parent.data_width = 8; - qcfg.parent.max_hz = 10000000UL; + qcfg.medium_size = W25Q_MAX_ADDR; + qcfg.qspi_dl_width = W25Q_QSPI_DATA_LINE_WIDTH; + qcfg.parent.mode = RT_SPI_MODE_0; + qcfg.parent.data_width = 8; + qcfg.parent.max_hz = 10000000UL; ret = rt_qspi_configure(qspi_dev_w25q, &qcfg); if ((RT_EOK != ret) && (-RT_EBUSY != ret)) { diff --git a/bsp/hc32/tests/test_rtc.c b/bsp/hc32/tests/test_rtc.c index 8aa0bde8d8..6b5dc17508 100644 --- a/bsp/hc32/tests/test_rtc.c +++ b/bsp/hc32/tests/test_rtc.c @@ -11,9 +11,15 @@ /* * 程序清单:这是 RTC 设备使用例程和 Alarm 使用示例。 * 例程导出了 rtc_sample 命令到控制终端。 + * menuconfig: + * 1.Hardware Drivers Config--->On-Chip Peripheral Driver--->Enable RTC---> Select clock source(RTC USING XTAL32) + * 2.RT-Thread Components---> Device Drivers ---> [-*-] Using RTC device drivers ---> [*] Using RTC alarm + * (1024) stack size for alarm thread + * [*] Using local time for the alarm calculation * 命令调用格式:rtc_sample x * 命令解释:命令第二个参数是要使用的功能对应的编号, * RTC 基本功能对应的编号为 0~3,Alarm 功能对应的编号为 4~9 + * NOTE: 注意时间和日期格式,请严格按照提示位数输入! */ #include <rtthread.h> @@ -26,24 +32,23 @@ #if defined(BSP_USING_RTC) /* macros define */ -#define SAMPLE_RTC_NAME "rtc" +#define SAMPLE_RTC_NAME "rtc" /* variables define */ static rt_device_t rtc_dev; #if defined(RT_USING_ALARM) - extern void rt_alarm_dump(void); +extern void rt_alarm_dump(void); - static rt_uint16_t callback_counter, alarm_idx = 0; - static struct rt_alarm *ptr_alarm = RT_NULL; - static struct rt_alarm_setup alarm_setup; +static struct rt_alarm *ptr_alarm = RT_NULL; +static struct rt_alarm_setup alarm_setup; #endif /* RT_USING_ALARM */ /* command type */ enum RTC_CMD { - CMD_OPEN_RTC = 0x00, - CMD_SET_TIME = 0x01, - CMD_SET_DATE = 0x02, + CMD_OPEN_RTC = 0x00, + CMD_SET_TIME = 0x01, + CMD_SET_DATE = 0x02, CMD_GET_DATE_TIME, #if defined(RT_USING_ALARM) CMD_SET_ALARM, @@ -56,18 +61,7 @@ enum RTC_CMD void alarm_callback_fun(rt_alarm_t alarm, time_t timestamp) { - rt_kprintf("\nuser alarm %d callback function.\n", alarm_idx); - if ((0 == (--callback_counter)) && (alarm_idx)) - { - rt_kprintf("stop alarm %d \n", alarm_idx); - if (RT_EOK != rt_alarm_stop(alarm)) - { - rt_kprintf("failed to stop alarm\n"); - } - /* enter callback 2 times */ - callback_counter = 2; - --alarm_idx; - } + rt_kprintf("User alarm callback function.\nNow time is: %s", ctime(×tamp)); } #else }; @@ -85,18 +79,20 @@ static int rtc_sample(int argc, char *argv[]) if (argc < 2) { rt_kprintf("unkown rtc command, rtc [usage] as the following: \n"); - rt_kprintf("\'0\': find and open rtc \n"); - rt_kprintf("\'1 xx:xx:xx\': set time with \n"); - rt_kprintf("\'2 xxxx-xx-xx\': set date with \n"); - rt_kprintf("\'3\': get time and date \n"); + rt_kprintf("\'rtc_sample 0\': find and open rtc \n"); + rt_kprintf("\'rtc_sample 1 HH:MM:SS\': set time with \n"); + rt_kprintf("\'rtc_sample 2 yyyy-mm-dd\': set date with \n"); + rt_kprintf("\'rtc_sample 3\': get time and date \n"); #if defined(RT_USING_ALARM) - rt_kprintf("\'4\': set current time + 10s as alarm \n"); - rt_kprintf("\'5\': start alarm \n"); - rt_kprintf("\'6\': stop alarm \n"); + rt_kprintf("\'rtc_sample 4\': set current time + 1 Min as alarm \n"); + rt_kprintf("\'rtc_sample 5\': start alarm \n"); + rt_kprintf("\'rtc_sample 6\': stop alarm \n"); rt_kprintf("cmd-7 based on cmd-4\n"); - rt_kprintf("\'7\' o: oneshot,\n\'7\' s: second,\n\'7\' m: minute \n"); - rt_kprintf("\'8\': dump all alarm \n"); - rt_kprintf("\'9\': delete all alarm \n"); + rt_kprintf("\'rtc_sample 7 o\': oneshot \n"); + rt_kprintf("\'rtc_sample 7 s\': second \n"); + rt_kprintf("\'rtc_sample 7 m\': minute \n"); + rt_kprintf("\'rtc_sample 8\': dump all alarm \n"); + rt_kprintf("\'rtc_sample 9\': delete all alarm \n"); #endif /* RT_USING_ALARM */ return -RT_ERROR; } @@ -120,17 +116,17 @@ static int rtc_sample(int argc, char *argv[]) rt_kprintf("rtc opened\n"); break; case CMD_SET_TIME: - /* set time with xx:xx:xx format characters */ + /* set time with hh:mm:ss format characters */ if (argc < 3) { rt_kprintf("unsurpported command\n"); return -RT_ERROR; } - temp1 = ((argv[2][0] - '0') * 10) + \ + temp1 = ((argv[2][0] - '0') * 10) + (argv[2][1] - '0'); - temp2 = ((argv[2][3] - '0') * 10) + \ + temp2 = ((argv[2][3] - '0') * 10) + (argv[2][4] - '0'); - temp3 = ((argv[2][6] - '0') * 10) + \ + temp3 = ((argv[2][6] - '0') * 10) + (argv[2][7] - '0'); if (RT_EOK != set_time(temp1, temp2, temp3)) { @@ -140,21 +136,21 @@ static int rtc_sample(int argc, char *argv[]) rt_kprintf("\nset RTC time as %2d:%2d:%2d\n", temp1, temp2, temp3); break; case CMD_SET_DATE: - /* set data xxxx-xx-xx format characters */ - temp1 = ((argv[2][0] - '0') * 1000) + \ - ((argv[2][1] - '0') * 100) + \ - ((argv[2][2] - '0') * 10) + \ + /* set data yyyy-mm-dd format characters */ + temp1 = ((argv[2][0] - '0') * 1000) + + ((argv[2][1] - '0') * 100) + + ((argv[2][2] - '0') * 10) + (argv[2][3] - '0'); - temp2 = ((argv[2][5] - '0') * 10) + \ + temp2 = ((argv[2][5] - '0') * 10) + (argv[2][6] - '0'); - temp3 = ((argv[2][8] - '0') * 10) + \ + temp3 = ((argv[2][8] - '0') * 10) + (argv[2][9] - '0'); if (RT_EOK != set_date(temp1, temp2, temp3)) { rt_kprintf("failed to set date for %s\n", SAMPLE_RTC_NAME); return -RT_ERROR; } - rt_kprintf("\nset RTC date as %4d-%2d-%2d\n", temp1, temp2, temp3); + rt_kprintf("\nset RTC date as %4d-%02d-%02d\n", temp1, temp2, temp3); break; case CMD_GET_DATE_TIME: /* get current time and print it */ @@ -166,9 +162,12 @@ static int rtc_sample(int argc, char *argv[]) /* get current time (uint: second) from 1970-01-01 */ now = time(NULL); rt_kprintf("GMT time is: \n%s\n", ctime(&now)); - now += 60; + /* converts the local time into the calendar time. */ +#ifdef RT_ALARM_USING_LOCAL_TIME + localtime_r(&now, &p_tm); +#else gmtime_r(&now, &p_tm); - // localtime_r(&now, &p_tm); +#endif alarm_setup.flag = RT_ALARM_MINUTE; alarm_setup.wktime.tm_year = p_tm.tm_year; alarm_setup.wktime.tm_mon = p_tm.tm_mon; @@ -177,23 +176,31 @@ static int rtc_sample(int argc, char *argv[]) alarm_setup.wktime.tm_wday = p_tm.tm_wday; alarm_setup.wktime.tm_hour = p_tm.tm_hour; alarm_setup.wktime.tm_min = p_tm.tm_min; - alarm_setup.wktime.tm_sec = p_tm.tm_sec; + alarm_setup.wktime.tm_sec = 0; //p_tm.tm_sec; alarm_setup.wktime.tm_isdst = -1; + alarm_setup.wktime.tm_min += 1; + if (alarm_setup.wktime.tm_min > 59) + { + alarm_setup.wktime.tm_min = 0; + alarm_setup.wktime.tm_hour += 1; + if (alarm_setup.wktime.tm_hour > 23) + { + alarm_setup.wktime.tm_hour = 0; + } + } rt_kprintf("UTC alarm Time: \n%d-%02d-%02d %02d:%02d:%02d\n\n", - p_tm.tm_year + 1900, - p_tm.tm_mon + 1, - p_tm.tm_mday, - p_tm.tm_hour, - p_tm.tm_min, - p_tm.tm_sec); + alarm_setup.wktime.tm_year + 1900, + alarm_setup.wktime.tm_mon + 1, + alarm_setup.wktime.tm_mday, + alarm_setup.wktime.tm_hour, + alarm_setup.wktime.tm_min, + alarm_setup.wktime.tm_sec); ptr_alarm = rt_alarm_create(alarm_callback_fun, &alarm_setup); if (RT_NULL == ptr_alarm) { rt_kprintf("failed to create rtc alarm\n"); return -RT_ERROR; } - callback_counter = 2; - ++alarm_idx; rt_alarm_dump(); break; case CMD_SET_START_ALARM: @@ -245,7 +252,6 @@ static int rtc_sample(int argc, char *argv[]) { rt_kprintf("failed to delete alarm\n"); } - alarm_idx = 0; rt_kprintf("alarm deleted\n"); break; #endif /* RT_USING_ALARM */ diff --git a/bsp/hc32/tests/test_sdmmc.c b/bsp/hc32/tests/test_sdmmc.c index 9cebb78e16..cf461d1b71 100644 --- a/bsp/hc32/tests/test_sdmmc.c +++ b/bsp/hc32/tests/test_sdmmc.c @@ -18,6 +18,11 @@ * stcPLLHInit.PLLCFGR_f.PLLN = 120UL - 1UL; * 改为 * stcPLLHInit.PLLCFGR_f.PLLN = 100UL - 1UL; + * + * menuconfig: + * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable SDIO ----> Enable SDIOx(x:测试板硬件决定) + * + * RT-Thread Components ---> Device Drivers ---> Using SD/MMC device drivers ---> (1024) The stack size for mmcsd thread */ #include <stdlib.h> @@ -27,14 +32,14 @@ #if defined(BSP_USING_SDIO) -#define SDMMC_DEVICE_NAME "sd" -#define SDMMC_SECTOR_SIZE 512UL +#define SDMMC_DEVICE_NAME "sd" +#define SDMMC_SECTOR_SIZE 512UL -#define SDMMC_TEST_SECTORS_PER_TIME 100UL -#define SDMMC_TEST_TIME 10UL +#define SDMMC_TEST_SECTORS_PER_TIME 100UL +#define SDMMC_TEST_TIME 10UL -#define SDMMC_TEST_SECTORS (SDMMC_TEST_TIME * SDMMC_TEST_SECTORS_PER_TIME) -#define SDMMC_TEST_BUF_SIZE (SDMMC_SECTOR_SIZE * SDMMC_TEST_SECTORS_PER_TIME) +#define SDMMC_TEST_SECTORS (SDMMC_TEST_TIME * SDMMC_TEST_SECTORS_PER_TIME) +#define SDMMC_TEST_BUF_SIZE (SDMMC_SECTOR_SIZE * SDMMC_TEST_SECTORS_PER_TIME) static void sdmmc_thread_entry(void *parameter) { @@ -42,7 +47,8 @@ static void sdmmc_thread_entry(void *parameter) rt_uint32_t err_count = 0; rt_device_t sd_device; rt_uint32_t sector_start; - rt_uint32_t sector_end;; + rt_uint32_t sector_end; + ; rt_uint32_t sector_cur_start; rt_uint32_t sector_cur_end; rt_uint8_t *sector_rbuf; @@ -138,7 +144,6 @@ static void sdmmc_thread_entry(void *parameter) if (err_count == 0) { rt_kprintf("sector=[%d, %d]: ...... test ok...... !\r\n\r\n", sector_start, sector_end); - } else { diff --git a/bsp/hc32/tests/test_sdram.c b/bsp/hc32/tests/test_sdram.c index c02c78f0d3..ef37650e98 100644 --- a/bsp/hc32/tests/test_sdram.c +++ b/bsp/hc32/tests/test_sdram.c @@ -14,7 +14,7 @@ * 程序功能:以8/16/32bit方式分别对整个SDRAM存储空间进行写和读操作,比较数据是否一致 * * 注意: - * F4A0: 修改函数SystemClock_Config,调用函数CLK_SetClockDiv参数,CLK_EXCLK_DIV2改为CLK_EXCLK_DIV8(EXCLK: 30MHz); + * F4A0/F4A2/F467: 修改函数SystemClock_Config,调用函数CLK_SetClockDiv参数,CLK_EXCLK_DIV2改为CLK_EXCLK_DIV8(EXCLK: 30MHz); * * menuconfig: * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable EXMC ----> Using SDRAM or NAND ----> Using SDRAM diff --git a/bsp/hc32/tests/test_soft_i2c.c b/bsp/hc32/tests/test_soft_i2c.c index 4cce95dabd..b8e0b24701 100644 --- a/bsp/hc32/tests/test_soft_i2c.c +++ b/bsp/hc32/tests/test_soft_i2c.c @@ -28,26 +28,26 @@ #if defined(BSP_USING_I2C1_SW) /* using i2c1 control oled12864 */ -#define SW_I2C_NAME "i2c1_sw" -#define SSD1306_ADDR (0x78U >> 1) -#define SSD1306_MD_CMD (0x00U) -#define SSD1306_MD_DATA (0x40U) +#define SW_I2C_NAME "i2c1_sw" +#define SSD1306_ADDR (0x78U >> 1) +#define SSD1306_MD_CMD (0x00U) +#define SSD1306_MD_DATA (0x40U) /* symbol parameters: width pixles, lenght pixels */ -#define SYM_W_PIX (8U) -#define SYM_H_PIX (8U) +#define SYM_W_PIX (8U) +#define SYM_H_PIX (8U) /* ssd ohysical parameters */ -#define SSD_PAGE_SIZE (8U) -#define SSD_COL_SIZE (128U) +#define SSD_PAGE_SIZE (8U) +#define SSD_COL_SIZE (128U) /* each page 8 pix */ -#define PAGE_PIX_SIZE (8U) +#define PAGE_PIX_SIZE (8U) /* each byte set horizontal 1 pix */ -#define SYM_W_BYTE (SYM_W_PIX / 1) +#define SYM_W_BYTE (SYM_W_PIX / 1) /* each byte set vertical 8 pix */ -#define SYM_H_BYTE (SYM_H_PIX / 8) +#define SYM_H_BYTE (SYM_H_PIX / 8) /* each character occupis */ -#define SYM_BYTE_SIZE (SYM_W_BYTE * SYM_H_BYTE) +#define SYM_BYTE_SIZE (SYM_W_BYTE * SYM_H_BYTE) /* soft i2c command defines */ enum SW_I2C_CMD @@ -175,8 +175,7 @@ static int sw_i2c_sample(int argc, char *argv[]) /* ssd1306 de-init and turn off */ static void ssd1306_deinit(struct rt_i2c_bus_device *i2c_dev) { - rt_uint8_t ssd_deinit_array[] = - { + rt_uint8_t ssd_deinit_array[] = { 0X8D, /* set charge pump */ 0X10, /* turn off charge pump */ 0XAE, /* OLED sleep */ @@ -259,8 +258,7 @@ static void ssd1306_roll_display(struct rt_i2c_bus_device *i2c_dev) u16WriteTimes++; } -rt_uint8_t ssd_init_array[] = -{ +rt_uint8_t ssd_init_array[] = { 0xAE, /* display off */ 0x20, /* Set Memory Addressing Mode */ 0x10, /* Set addressing orient */ @@ -293,8 +291,7 @@ rt_uint8_t ssd_init_array[] = }; -rt_uint8_t logo_array[][SYM_BYTE_SIZE] = -{ +rt_uint8_t logo_array[][SYM_BYTE_SIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x6C, 0x74, 0x54, 0x6C, 0x44, 0x44, /*"X"*/ 0x44, 0x7C, 0x54, 0x10, 0x10, 0x54, 0x7C, 0x44, /*"H"*/ diff --git a/bsp/hc32/tests/test_spi.c b/bsp/hc32/tests/test_spi.c index 171c70da8d..4381e6f28f 100644 --- a/bsp/hc32/tests/test_spi.c +++ b/bsp/hc32/tests/test_spi.c @@ -6,6 +6,9 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-04 CDT Support HC32F467 + * 2026-07-10 CDT Modify SPI maxium clock to 30MHz */ /* @@ -24,54 +27,54 @@ #if defined(BSP_USING_SPI) #include "drv_spi.h" -#define W25Q_FLAG_BUSY (0x01) -#define W25Q_WR_ENABLE (0x06) -#define W25Q_SECTOR_ERASE (0x20) -#define W25Q_RD_STATUS_REG1 (0x05) -#define W25Q_PAGE_PROGRAM (0x02) -#define W25Q_STD_RD (0x03) +#define W25Q_FLAG_BUSY (0x01) +#define W25Q_WR_ENABLE (0x06) +#define W25Q_SECTOR_ERASE (0x20) +#define W25Q_RD_STATUS_REG1 (0x05) +#define W25Q_PAGE_PROGRAM (0x02) +#define W25Q_STD_RD (0x03) -#define W25Q_PAGE_SIZE (256UL) -#define W25Q_SECTOR_SIZE (1024UL * 4UL) -#define W25Q_PAGE_PER_SECTOR (W25Q_SECTOR_SIZE / W25Q_PAGE_SIZE) -#define W25Q_MAX_ADDR (0x800000UL) +#define W25Q_PAGE_SIZE (256UL) +#define W25Q_SECTOR_SIZE (1024UL * 4UL) +#define W25Q_PAGE_PER_SECTOR (W25Q_SECTOR_SIZE / W25Q_PAGE_SIZE) +#define W25Q_MAX_ADDR (0x800000UL) -#define W25Q_SPI_WR_RD_ADDR 0x4000 -#if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F472) || defined (HC32F448) || defined (HC32F4A8) - #define W25Q_SPI_DATA_BUF_LEN 0x2000 -#elif defined (HC32F334) - #define W25Q_SPI_DATA_BUF_LEN 0x1000 +#define W25Q_SPI_WR_RD_ADDR 0x4000 +#if defined(HC32F460) || defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F472) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F467) +#define W25Q_SPI_DATA_BUF_LEN 0x2000 +#elif defined(HC32F334) +#define W25Q_SPI_DATA_BUF_LEN 0x1000 #endif -#if defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8) - #define SPI_CS_PORT SPI1_CS_PORT - #define SPI_CS_PIN SPI1_CS_PIN - #define SPI_CS_PORT_PIN GET_PIN(C, 7) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F4A8) || defined(HC32F467) +#define SPI_CS_PORT SPI1_CS_PORT +#define SPI_CS_PIN SPI1_CS_PIN +#define SPI_CS_PORT_PIN GET_PIN(C, 7) - #define W25Q_SPI_BUS_NAME "spi1" - #define W25Q_SPI_DEVICE_NAME "spi10" +#define W25Q_SPI_BUS_NAME "spi1" +#define W25Q_SPI_DEVICE_NAME "spi10" #elif defined(HC32F472) - #define SPI_CS_PORT SPI1_CS_PORT - #define SPI_CS_PIN SPI1_CS_PIN - #define SPI_CS_PORT_PIN GET_PIN(B, 12) +#define SPI_CS_PORT SPI1_CS_PORT +#define SPI_CS_PIN SPI1_CS_PIN +#define SPI_CS_PORT_PIN GET_PIN(B, 12) - #define W25Q_SPI_BUS_NAME "spi1" - #define W25Q_SPI_DEVICE_NAME "spi10" +#define W25Q_SPI_BUS_NAME "spi1" +#define W25Q_SPI_DEVICE_NAME "spi10" #elif defined(HC32F460) - #define SPI_CS_PORT SPI3_CS_PORT - #define SPI_CS_PIN SPI3_CS_PIN - #define SPI_CS_PORT_PIN GET_PIN(C, 7) +#define SPI_CS_PORT SPI3_CS_PORT +#define SPI_CS_PIN SPI3_CS_PIN +#define SPI_CS_PORT_PIN GET_PIN(C, 7) - #define W25Q_SPI_BUS_NAME "spi3" - #define W25Q_SPI_DEVICE_NAME "spi30" +#define W25Q_SPI_BUS_NAME "spi3" +#define W25Q_SPI_DEVICE_NAME "spi30" #elif defined(HC32F334) - #define SPI_CS_PORT SPI1_CS_PORT - #define SPI_CS_PIN SPI1_CS_PIN - #define SPI_CS_PORT_PIN GET_PIN(C, 1) +#define SPI_CS_PORT SPI1_CS_PORT +#define SPI_CS_PIN SPI1_CS_PIN +#define SPI_CS_PORT_PIN GET_PIN(C, 1) - #define W25Q_SPI_BUS_NAME "spi1" - #define W25Q_SPI_DEVICE_NAME "spi10" +#define W25Q_SPI_BUS_NAME "spi1" +#define W25Q_SPI_DEVICE_NAME "spi10" #endif @@ -96,8 +99,8 @@ INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init); void w25q_read_uid(struct rt_spi_device *device) { rt_uint8_t w25x_read_uid = 0x4B; /* 命令 */ - rt_uint8_t u8UID[8] = {0}; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t u8UID[8] = { 0 }; + rt_uint8_t txBuf[5] = { 0 }; memset(txBuf, 0xFF, 5); txBuf[0] = w25x_read_uid; @@ -115,19 +118,19 @@ void w25q_read_uid(struct rt_spi_device *device) /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */ struct rt_spi_message msg1, msg2; - msg1.send_buf = txBuf; - msg1.recv_buf = RT_NULL; - msg1.length = 5; - msg1.cs_take = 1; + msg1.send_buf = txBuf; + msg1.recv_buf = RT_NULL; + msg1.length = 5; + msg1.cs_take = 1; msg1.cs_release = 0; - msg1.next = &msg2; + msg1.next = &msg2; - msg2.send_buf = RT_NULL; - msg2.recv_buf = u8UID; - msg2.length = 8; - msg2.cs_take = 0; + msg2.send_buf = RT_NULL; + msg2.recv_buf = u8UID; + msg2.length = 8; + msg2.cs_take = 0; msg2.cs_release = 1; - msg2.next = RT_NULL; + msg2.next = RT_NULL; rt_spi_transfer_message(device, &msg1); rt_kprintf("use rt_spi_transfer_message() read w25q ID is: UID is: %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n", @@ -138,8 +141,8 @@ int32_t w25q_check_process_done(struct rt_spi_device *device, uint32_t u32Timeou { __IO uint32_t u32Count = 0U; int32_t i32Ret = LL_ERR_TIMEOUT; - rt_uint8_t rxBuf[5] = {0}; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t rxBuf[5] = { 0 }; + rt_uint8_t txBuf[5] = { 0 }; txBuf[0] = W25Q_RD_STATUS_REG1; while (u32Count < u32Timeout) @@ -166,7 +169,7 @@ int32_t w25q_check_process_done(struct rt_spi_device *device, uint32_t u32Timeou int32_t w25q_read_data(struct rt_spi_device *device, uint32_t u32Addr, uint8_t *pu8ReadBuf, uint32_t u32Size) { int32_t i32Ret = LL_OK; - rt_uint8_t txBuf[5] = {0}; + rt_uint8_t txBuf[5] = { 0 }; txBuf[0] = W25Q_STD_RD; txBuf[1] = (u32Addr >> 16) & 0xFFU; @@ -221,9 +224,9 @@ int32_t w25q_write_data(struct rt_spi_device *device, uint32_t u32Addr, uint8_t break; } - u32Addr += u32TempSize; + u32Addr += u32TempSize; u32AddrOffset += u32TempSize; - u32Size -= u32TempSize; + u32Size -= u32TempSize; } return i32Ret; @@ -283,11 +286,11 @@ void w25q_write_read_data(struct rt_spi_device *device, uint32_t u32Addr) { rt_kprintf("spi erase sector failed!\n"); } - if (LL_OK != w25q_write_data(device, u32Addr, u8WrBuf, W25Q_SPI_DATA_BUF_LEN)) + if (LL_OK != w25q_write_data(device, u32Addr, u8WrBuf, W25Q_SPI_DATA_BUF_LEN)) { rt_kprintf("spi write data failed!\n"); } - if (LL_OK != w25q_read_data(device, u32Addr, u8RdBuf, W25Q_SPI_DATA_BUF_LEN)) + if (LL_OK != w25q_read_data(device, u32Addr, u8RdBuf, W25Q_SPI_DATA_BUF_LEN)) { rt_kprintf("spi read data failed!\n"); } @@ -308,7 +311,7 @@ static void spi_thread_entry(void *parameter) cfg.data_width = 8; cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; - cfg.max_hz = 80 * 1000 * 1000; /* 80M */ + cfg.max_hz = 30 * 1000 * 1000; /* 30M */ rt_spi_configure(spi_dev_w25q, &cfg); /* 读取UID */ w25q_read_uid(spi_dev_w25q); diff --git a/bsp/hc32/tests/test_tmr_capture.c b/bsp/hc32/tests/test_tmr_capture.c index 2a94e5aee7..4f723bf966 100644 --- a/bsp/hc32/tests/test_tmr_capture.c +++ b/bsp/hc32/tests/test_tmr_capture.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2025-01-10 CDT first version + * 2026-05-27 CDT Support HC32F4A2 */ /* @@ -16,7 +17,8 @@ * 默认配置 * input pin: * icx: INPUT_CAPTURE_TMR6_x_PORT, INPUT_CAPTURE_TMR6_x_PIN (x=1~IC_DEV_CNT) - 注:该引脚配置位于 board_config.h,若测试单元的input pin未配置,需测试人员自行添加 + 注:该引脚配置位于 board_config.h,若测试单元的input pin未配置,需测试人员自行添加,并于 + board_config.c中做初始化 * watermark: * 默认值为 5 * @@ -53,23 +55,23 @@ #include <board.h> #include <stdlib.h> -#define MSH_USAGE_IC_OPEN " ic open <unit> - e.g., open ic3: ic open 3 \n" -#define MSH_USAGE_IC_CLOSE " ic close <unit> - e.g., close ic3: ic close 3\n" -#define MSH_USAGE_IC_SET_WM " ic wm <unit> <wm> - e.g., set warter mark of ic3 to 11: ic wm 3 11\n" -#define MSH_USAGE_IC_CLR " ic clr <unit> - e.g., clear data buffer of ic3: ic clr 3 \n" +#define MSH_USAGE_IC_OPEN " ic open <unit> - e.g., open ic3: ic open 3 \n" +#define MSH_USAGE_IC_CLOSE " ic close <unit> - e.g., close ic3: ic close 3\n" +#define MSH_USAGE_IC_SET_WM " ic wm <unit> <wm> - e.g., set warter mark of ic3 to 11: ic wm 3 11\n" +#define MSH_USAGE_IC_CLR " ic clr <unit> - e.g., clear data buffer of ic3: ic clr 3 \n" -#if defined (HC32F4A0) || defined (HC32F4A8) - #define IC_DEV_CNT (8) -#elif defined (HC32F460) - #define IC_DEV_CNT (3) -#elif defined (HC32F334) - #define IC_DEV_CNT (4) -#elif defined (HC32F448) - #define IC_DEV_CNT (2) -#elif defined (HC32F472) - #define IC_DEV_CNT (10) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) +#define IC_DEV_CNT (8) +#elif defined(HC32F460) || defined(HC32F467) +#define IC_DEV_CNT (3) +#elif defined(HC32F334) +#define IC_DEV_CNT (4) +#elif defined(HC32F448) +#define IC_DEV_CNT (2) +#elif defined(HC32F472) +#define IC_DEV_CNT (10) #endif -#define DEFAULT_WATER_MARK (5) +#define DEFAULT_WATER_MARK (5) #ifdef BSP_USING_INPUT_CAPTURE @@ -82,7 +84,7 @@ typedef struct rt_thread_t thread; } test_ic_t; -static test_ic_t g_arr_test_ic[IC_DEV_CNT] = {0}; +static test_ic_t g_arr_test_ic[IC_DEV_CNT] = { 0 }; static int32_t _get_test_id(rt_device_t ic_dev) { @@ -109,7 +111,7 @@ static rt_err_t ic_rx_all(rt_device_t dev, rt_size_t size) static void ic_rx_thread(void *parameter) { - rt_size_t size; + rt_size_t size; rt_device_t ic_dev; test_ic_t *p_test_ic = parameter; ic_dev = p_test_ic->ic_dev; @@ -223,7 +225,7 @@ static rt_err_t _msh_cmd_parse_unit(char *n, uint32_t *u_out) return RT_EOK; } -void _show_usage(void) +static void _show_usage(void) { rt_kprintf("Usage: \n"); rt_kprintf(MSH_USAGE_IC_OPEN); @@ -297,7 +299,7 @@ static rt_int32_t ic(int argc, char *argv[]) } -MSH_CMD_EXPORT(ic, ic [opt]) +MSH_CMD_EXPORT(ic, ic[opt]) #endif /* EOF diff --git a/bsp/hc32/tests/test_uart_v1.c b/bsp/hc32/tests/test_uart_v1.c index 9d5958c093..5799e8029d 100644 --- a/bsp/hc32/tests/test_uart_v1.c +++ b/bsp/hc32/tests/test_uart_v1.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 */ /* @@ -39,17 +40,19 @@ #include <rtdevice.h> #if defined(HC32F460) && defined(BSP_USING_UART2) - #define SAMPLE_DEFAULT_UART_NAME "uart2" -#elif defined(HC32F4A0) && defined (BSP_USING_UART6) - #define SAMPLE_DEFAULT_UART_NAME "uart6" -#elif defined(HC32F448) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" -#elif defined(HC32F472) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" -#elif defined(HC32F4A8) && defined (BSP_USING_UART6) - #define SAMPLE_DEFAULT_UART_NAME "uart6" -#elif defined(HC32F334) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" +#define SAMPLE_DEFAULT_UART_NAME "uart2" +#elif (defined(HC32F4A0) || defined(HC32F4A2)) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" /* TX:PE6, RX:PH6 */ +#elif defined(HC32F448) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F472) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F4A8) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" +#elif defined(HC32F334) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F467) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" #endif #if defined(SAMPLE_DEFAULT_UART_NAME) @@ -207,7 +210,7 @@ int uart_sample_v1(int argc, char *argv[]) config.baud_rate = BAUD_RATE_115200; //baudrate 115200 config.data_bits = DATA_BITS_8; //data bit 8 config.stop_bits = STOP_BITS_1; //stop bit 1 - config.parity = PARITY_NONE; + config.parity = PARITY_NONE; rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config); if (0 == rt_strncmp(comm_mode, comm_mode_dma, 3)) diff --git a/bsp/hc32/tests/test_uart_v2.c b/bsp/hc32/tests/test_uart_v2.c index 11ab6c466d..48325d5995 100644 --- a/bsp/hc32/tests/test_uart_v2.c +++ b/bsp/hc32/tests/test_uart_v2.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-03 CDT Support HC32F467 */ /* @@ -14,7 +16,7 @@ * * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备(uart1) * 程序功能:通过串口输出字符串: - * drv_usart: drv_usart_v1 + * drv_usart: drv_usart_v2 * commnucation:using DMA/interrupt, * uart_ch: uartx (x对应测试通道) * 输出输入的字符 @@ -67,17 +69,19 @@ #include <rtdevice.h> #if defined(HC32F460) && defined(BSP_USING_UART2) - #define SAMPLE_DEFAULT_UART_NAME "uart2" -#elif defined(HC32F4A0) && defined (BSP_USING_UART6) - #define SAMPLE_DEFAULT_UART_NAME "uart6" -#elif defined(HC32F448) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" -#elif defined(HC32F472) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" -#elif defined(HC32F4A8) && defined (BSP_USING_UART6) - #define SAMPLE_DEFAULT_UART_NAME "uart6" -#elif defined(HC32F334) && defined (BSP_USING_UART1) - #define SAMPLE_DEFAULT_UART_NAME "uart1" +#define SAMPLE_DEFAULT_UART_NAME "uart2" +#elif (defined(HC32F4A0) || defined(HC32F4A2)) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" /* TX:PE6, RX:PH6 */ +#elif defined(HC32F448) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F472) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F4A8) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" +#elif defined(HC32F334) && defined(BSP_USING_UART1) +#define SAMPLE_DEFAULT_UART_NAME "uart1" +#elif defined(HC32F467) && defined(BSP_USING_UART6) +#define SAMPLE_DEFAULT_UART_NAME "uart6" #endif #if defined(SAMPLE_DEFAULT_UART_NAME) @@ -234,7 +238,7 @@ int uart_sample_v2(int argc, char *argv[]) config.baud_rate = BAUD_RATE_115200; //baudrate 115200 config.data_bits = DATA_BITS_8; //data bit 8 config.stop_bits = STOP_BITS_1; //stop bit 1 - config.parity = PARITY_NONE; + config.parity = PARITY_NONE; rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config); if (0 == rt_strncmp(comm_mode, comm_mode_dma, 3)) diff --git a/bsp/hc32/tests/test_usbd.c b/bsp/hc32/tests/test_usbd.c index 5a4a19027b..be867a3bfe 100644 --- a/bsp/hc32/tests/test_usbd.c +++ b/bsp/hc32/tests/test_usbd.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2024-12-30 CDT first version + * 2026-05-27 CDT Support HC32F4A2 + * 2026-06-03 CDT Support HC32F467 */ #include <rtthread.h> @@ -31,14 +33,19 @@ * 发送内容可在Finsh串口显示。 */ -#define USBD_DEV_NAME "vcom" /* 名称 */ -rt_uint8_t str_read[255]; +#define USBD_DEV_NAME "vcom" /* 名称 */ +static rt_uint8_t cdc_str_read[256]; static rt_err_t cdc_rx_handle(rt_device_t dev, rt_size_t size) { + if (size >= sizeof(cdc_str_read)) + { + size = sizeof(cdc_str_read) - 1; + } /* 读取虚拟串口接收内容 */ - rt_device_read(dev, 0, str_read, size); - rt_kprintf("Read message: %s\n", str_read); + rt_device_read(dev, 0, cdc_str_read, size); + cdc_str_read[size] = '\0'; + rt_kprintf("Read message: %s\n", cdc_str_read); return RT_EOK; } @@ -90,9 +97,9 @@ MSH_CMD_EXPORT(cdc_sample, usbd cdc sample); #if defined(RT_USB_DEVICE_MSTORAGE) -/* F4A0 only FS can used with spi flash */ -#if ((defined(HC32F4A0) || defined(HC32F4A8)) && defined(BSP_USING_USBFS)) || \ - defined(HC32F460) || defined(HC32F472) +/* F4A0/F4A2/HC32F467 only FS can used with spi flash */ +#if ((defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F467)) && defined(BSP_USING_USBFS)) || \ + defined(HC32F460) || defined(HC32F472) /* Enable spibus1, SFUD, usb msc */ /* menuconfig: @@ -108,30 +115,30 @@ MSH_CMD_EXPORT(cdc_sample, usbd cdc sample); (50000000)Default spi maximum speed(HZ) 4. RT-Thread Components--->Using USB legacy version [*]Using USB device---> - Device type--->...Mass Storage device - (spiflash)msc class disk name + [*]Device type---> Enable to use device as Mass Storage device + (spiflash)msc class disk name */ #include "drv_gpio.h" #include "drv_spi.h" #include "dev_spi_flash_sfud.h" -#define SPI_FLASH_CHIP RT_USB_MSTORAGE_DISK_NAME /* msc class disk name */ -#if defined(HC32F4A0) || defined(HC32F4A8) - #define SPI_FLASH_SS_PORT GPIO_PORT_C - #define SPI_FLASH_SS_PIN GPIO_PIN_07 - #define SPI_BUS_NAME "spi1" - #define SPI_FLASH_DEVICE_NAME "spi10" +#define SPI_FLASH_CHIP RT_USB_MSTORAGE_DISK_NAME /* msc class disk name */ +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) +#define SPI_FLASH_SS_PORT GPIO_PORT_C +#define SPI_FLASH_SS_PIN GPIO_PIN_07 +#define SPI_BUS_NAME "spi1" +#define SPI_FLASH_DEVICE_NAME "spi10" #elif defined(HC32F460) - #define SPI_FLASH_SS_PORT GPIO_PORT_C - #define SPI_FLASH_SS_PIN GPIO_PIN_07 - #define SPI_BUS_NAME "spi3" - #define SPI_FLASH_DEVICE_NAME "spi30" +#define SPI_FLASH_SS_PORT GPIO_PORT_C +#define SPI_FLASH_SS_PIN GPIO_PIN_07 +#define SPI_BUS_NAME "spi3" +#define SPI_FLASH_DEVICE_NAME "spi30" #elif defined(HC32F472) - #define SPI_FLASH_SS_PORT GPIO_PORT_B - #define SPI_FLASH_SS_PIN GPIO_PIN_12 - #define SPI_BUS_NAME "spi1" - #define SPI_FLASH_DEVICE_NAME "spi10" +#define SPI_FLASH_SS_PORT GPIO_PORT_B +#define SPI_FLASH_SS_PIN GPIO_PIN_12 +#define SPI_BUS_NAME "spi1" +#define SPI_FLASH_DEVICE_NAME "spi10" #endif static void rt_hw_spi_flash_reset(char *spi_dev_name) @@ -157,7 +164,8 @@ static void rt_hw_spi_flash_reset(char *spi_dev_name) static int rt_hw_spi_flash_with_sfud_init(void) { -#if defined(HC32F4A0) || defined(HC32F460) || defined(HC32F4A8) +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F4A8) || \ + defined(HC32F467) rt_hw_spi_device_attach(SPI_BUS_NAME, SPI_FLASH_DEVICE_NAME, GET_PIN(C, 7)); #elif defined(HC32F472) rt_hw_spi_device_attach(SPI_BUS_NAME, SPI_FLASH_DEVICE_NAME, GET_PIN(B, 12)); @@ -200,20 +208,20 @@ INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init); * 发送内容可在Finsh串口显示。 */ -#define USBD_DEV_NAME "hidd" /* 名称 */ -#if defined(HC32F4A0) || defined(HC32F4A8) - #define KEY_PIN_NUM GET_PIN(A,0) /* PA0 */ +#define USBD_DEV_NAME "hidd" /* 名称 */ +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) +#define KEY_PIN_NUM GET_PIN(A, 0) /* PA0 */ #elif defined(HC32F460) - #define KEY_PIN_NUM GET_PIN(B,1) /* PB1 */ +#define KEY_PIN_NUM GET_PIN(B, 1) /* PB1 */ #elif defined(HC32F472) - #define KEY_PIN_NUM GET_PIN(B,5) /* PB5 */ +#define KEY_PIN_NUM GET_PIN(B, 5) /* PB5 */ #endif static int hid_sample(void) { rt_err_t ret = RT_EOK; rt_device_t hid_dev = RT_NULL; /* usb device设备句柄 */ - char str_write[2][5] = {"test", "Key0"}; + char str_write[2][5] = { "test", "Key0" }; /* 查找设备 */ @@ -246,7 +254,8 @@ static int hid_sample(void) } } - //return ret; + /* unreachable, but keeps static analyzers happy */ + return ret; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(hid_sample, usbd hid sample); @@ -274,25 +283,29 @@ MSH_CMD_EXPORT(hid_sample, usbd hid sample); * 通过llcom.exe可发送bulk数据(100字符以内)到设备,设备收到后会回发给主机(llcom.exe),同时通过MSH终端显示收到的HEX数据。 * 注意:1、llcom.exe中的GUID与驱动程序中设定保持一致(通过设备管理器选择RTT Win USB设备的属性来查看); * 2、win_usb_read()函数中的UIO_REQUEST_READ_FULL改为UIO_REQUEST_READ_BEST,实现数据即读即取; - * 否则需要接满传入的size数量,才会回调接收函数。 + * 否则需要接满传入的sizeof(winusb_str_read)数量的数据,才会回调接收函数。 * */ -#define WINUSB_DEV_NAME "winusb" /* 名称 */ -uint8_t str_read[100]; +#define WINUSB_DEV_NAME "winusb" /* 名称 */ +static rt_uint8_t winusb_str_read[100]; static rt_err_t winusb_rx_handle(rt_device_t dev, rt_size_t size) { uint8_t i; + if (size > sizeof(winusb_str_read)) + { + size = sizeof(winusb_str_read); + } /* 读取定时器当前值 */ rt_kprintf("Rx:"); for (i = 0; i < size; i++) { - rt_kprintf("%x", str_read[i]); + rt_kprintf("%x", winusb_str_read[i]); } rt_kprintf("\r\n"); - rt_device_write(dev, 0, str_read, size); + rt_device_write(dev, 0, winusb_str_read, size); /* prepare read config */ - rt_device_read(dev, 0, str_read, sizeof(str_read)); + rt_device_read(dev, 0, winusb_str_read, sizeof(winusb_str_read)); return RT_EOK; } @@ -322,7 +335,7 @@ static int winusb_sample(void) if (ret == RT_EOK) { /* prepare read config,set once,read once, */ - rt_device_read(winusb_dev, 0, str_read, sizeof(str_read)); + rt_device_read(winusb_dev, 0, winusb_str_read, sizeof(winusb_str_read)); } return ret; } diff --git a/bsp/hc32/tests/test_usbh.c b/bsp/hc32/tests/test_usbh.c index e5cddafc61..243acb5a0e 100644 --- a/bsp/hc32/tests/test_usbh.c +++ b/bsp/hc32/tests/test_usbh.c @@ -38,7 +38,7 @@ */ #include <unistd.h> #include <fcntl.h> -#define TEST_FN "/test_usbh.c" +#define TEST_FN "/test_usbh.c" static char test_data[120], buffer[120]; void usbh_readwrite(const char *filename) @@ -53,7 +53,7 @@ void usbh_readwrite(const char *filename) return; } - for (index = 0; index < sizeof(test_data); index ++) + for (index = 0; index < sizeof(test_data); index++) { test_data[index] = index + 27; } @@ -83,7 +83,7 @@ void usbh_readwrite(const char *filename) return; } - for (index = 0; index < sizeof(test_data); index ++) + for (index = 0; index < sizeof(test_data); index++) { if (test_data[index] != buffer[index]) { diff --git a/bsp/hc32/tests/test_wdt.c b/bsp/hc32/tests/test_wdt.c index c3c1715142..476a5a4305 100644 --- a/bsp/hc32/tests/test_wdt.c +++ b/bsp/hc32/tests/test_wdt.c @@ -23,9 +23,9 @@ #ifdef BSP_USING_WDT_TMR #if defined(BSP_USING_WDT) - #define WDT_DEVICE_NAME "wdt" +#define WDT_DEVICE_NAME "wdt" #elif defined(BSP_USING_SWDT) - #define WDT_DEVICE_NAME "swdt" +#define WDT_DEVICE_NAME "swdt" #endif static rt_device_t wdg_dev; @@ -120,6 +120,6 @@ static int wdt_sample(int argc, char *argv[]) return ret; } -MSH_CMD_EXPORT(wdt_sample, wdt_sample [option]); +MSH_CMD_EXPORT(wdt_sample, wdt_sample[option]); #endif diff --git a/bsp/hc32/tests/test_wktm.c b/bsp/hc32/tests/test_wktm.c new file mode 100644 index 0000000000..6633b44863 --- /dev/null +++ b/bsp/hc32/tests/test_wktm.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2022-2026, Xiaohua Semiconductor Co., Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-06-24 CDT first version + */ + +/** + * menuconfig: + * Hardware Drivers Config ---> On-Chip Peripheral Driver---> [*] Enable PM + * RT-Thread Kernel ---> (1024) The stack size of idle thread + */ + +#include <stdlib.h> +#include "board.h" +#include "drv_wktm.h" + +#if defined(BSP_USING_PM) + +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) +#define WKTM_IRQn (INT131_IRQn) +#elif defined(HC32F460) +#define WKTM_IRQn (INT130_IRQn) +#endif + +static volatile rt_uint32_t last_tick; + +#if defined(HC32F334) || defined(HC32F448) || defined(HC32F472) +void PWC_WKTM_Handler(void) +#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) || defined(HC32F460) +void PWC_WakeupTimer_IrqHandler(void) +#endif +{ + static rt_uint32_t delta_tick; + + rt_interrupt_enter(); + + delta_tick = rt_tick_get() - last_tick; + last_tick = rt_tick_get(); + + /* 打印出的tick值由于printf原因可能有误差 */ + rt_kprintf("Wakeup-timer irq interval ticks: approximate %d.\r\n", delta_tick); + + rt_interrupt_leave(); + +#if defined(HC32F334) || defined(HC32F448) || defined(HC32F472) + __DSB(); /* Arm Errata 838869: Cortex-M4, Cortex-M4F */ +#endif +} + +void wktm_sample(int argc, char **argv) +{ + rt_base_t level; + rt_uint32_t cmp_value; + rt_uint32_t cmp_max = hc32_wktm_get_tick_max(); + + if (argc >= 2) + { + cmp_value = atol(argv[1]); + if (0UL == cmp_value) + { + /*********************** Stop wakeup-timer ************************/ + hc32_wktm_stop(); + + NVIC_DisableIRQ(WKTM_IRQn); + NVIC_ClearPendingIRQ(WKTM_IRQn); + + rt_kprintf("Stop Wakeup-timer\n\n"); + } + else if (cmp_value > cmp_max) + { + /*********************** Hold the wakeup timer configuration ******/ + rt_kprintf("compare value %d is out of %d(max), so hold the wakeup timer configuration \n\n", cmp_value, cmp_max); + } + else if (cmp_value < (cmp_max / 2)) + { + /*********************** Hold the wakeup timer configuration ******/ + rt_kprintf("compare value %d is less of %d(max/2), so hold the wakeup timer configuration: ", cmp_value, cmp_max / 2); + rt_kprintf("to avoid frequent interruptions: too much printed information affcets console commands \n\n"); + } + else + { + /*********************** Start wakeup-timer ***********************/ + /* Wakeup timer NVIC config */ +#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467) || defined(HC32F460) + (void)INTC_ShareIrqCmd(INT_SRC_WKTM_PRD, ENABLE); +#endif + NVIC_ClearPendingIRQ(WKTM_IRQn); + NVIC_SetPriority(WKTM_IRQn, DDL_IRQ_PRIO_DEFAULT); + NVIC_EnableIRQ(WKTM_IRQn); + + if (RT_EOK == hc32_wktm_start(cmp_value)) + { + level = rt_hw_interrupt_disable(); + last_tick = rt_tick_get(); + rt_hw_interrupt_enable(level); + rt_kprintf("Update wakeup-timer compare value = %d, and start timer \n\n", cmp_value); + } + else + { + rt_kprintf("Fail to set wakeup-timer compare value \n\n", cmp_value); + } + } + } + else + { + rt_kprintf("wktm_sample 0: stop wakeup-timer \n"); + rt_kprintf("wktm_sample %d~%d: set wakeup-timer compare value and start timer \n\n", cmp_max / 2, cmp_max); + } +} +MSH_CMD_EXPORT(wktm_sample, wktm_sample compare_value); +#endif |
