diff --git a/EVT/CH32V20x_List.txt b/EVT/CH32V20x_List.txt index 1f36e65..6dfe479 100644 --- a/EVT/CH32V20x_List.txt +++ b/EVT/CH32V20x_List.txt @@ -1,5 +1,5 @@ -南京沁恒微电子股份有限公司 2023.09 +南京沁恒微电子股份有限公司 2023.10 最新资料请到我们的网站上查询 https://wch.cn @@ -36,6 +36,11 @@ https://wch.cn | | | | |-- DualADC_SlowInterleaved:双ADC慢速交叉采样例程 ——仅适用于CH32V20x_D6 | | | | |-- ExtLines_Trigger:外部触发ADC转换例程 | | | | |-- Internal_Temperature:内部温度传感器例程 + | | | |-- APPLICATION + | | | | |-- USBPD + | | | | | |-- I2C_OP_CH223:通过I2C操作CH223例程 ——仅适用于CH32V20x_D8 + | | | | |-- WS2812_LED + | | | | | |-- WS2812_LED:通过SPI\PWM操作WS2812例程 | | | |-- BLE ——仅适用于CH32V20x_D8W | | | | |-- BackupUpgrade_IAP:备份无线升级IAP例程,检测当前代码标志,判断是否搬运备份区代码到用户区并运行用户区代码 | | | | |-- BackupUpgrade_OTA:备份无线升级用户例程,外设从机例程基础上添加OTA功能,可将升级固件保存到备份区后跳转IAP程序进行升级 @@ -92,12 +97,14 @@ https://wch.cn | | | | | |-- MESH_LIB:MESH协议栈库文件及其头文件 | | | | | |-- 沁恒MESH APP管理配网应用手册.pdf | | | | | |-- 沁恒低功耗蓝牙MESH软件开发参考手册.pdf - | | | |-- BKP:BKP例程 + | | | |-- BKP + | | | | |-- BKP:BKP例程 | | | |-- CAN ——仅适用于CH32V20x_D6-CH32V20x_D8W | | | | |-- Networking:CAN例程:正常模式,标准帧和扩展帧数据收发 | | | | |-- TestMode:测试模式,包括静默模式、回环模式、回环静默模式 | | | | |-- Time-triggered:时间触发通信模式 - | | | |-- CRC:CRC例程 + | | | |-- CRC + | | | | |-- CRC_Calculation:CRC例程 | | | |-- DMA | | | | |-- DMA_MEM2MEM:储存器到储存器模式例程 | | | | |-- DMA_MEM2PERIP:储存器到外设模式、外设到储存器模式例程见各外设子例程 @@ -117,24 +124,31 @@ https://wch.cn | | | | |-- UdpClient:演示Udp Client接收数据并回传 | | | | |-- UdpServer:演示Udp Server接收数据并回传 | | | | |-- WebServer: 演示通过Web浏览器配置WCHNET芯片的功能,WCHNET芯片内置web服务器 ,通过网页可以实现WCHNET的网络参数配置,以及密码管理 - | | | |-- EXTI:外部中断线例程 - | | | |-- FLASH:FLASH的擦/读/写,以及快速编程 + | | | |-- EXTI + | | | | |-- EXTI0:外部中断线例程 + | | | |-- FLASH + | | | | |-- FLASH_Program:FLASH的擦/读/写,以及快速编程 | | | |-- FreeRTOS | | | | |-- FreeRTOS_Core:FreeRTOS移植例程 - | | | |-- GPIO:GPIO例程 - | | | |-- HarmonyOS:HarmonyOS移植例程 + | | | |-- GPIO + | | | | |-- GPIO_Toggle:GPIO例程 + | | | |-- HarmonyOS + | | | | |-- LiteOS_m:HarmonyOS移植例程 | | | |-- I2C | | | | |-- I2C_7bit_Mode:7位地址模式,Master/Slave 模式收发例程 | | | | |-- I2C_10bit_Mode:10位地址模式,Master/Slave 模式收发例程 | | | | |-- I2C_DMA:I2C使用DMA,Master/Slave 模式收发例程 | | | | |-- I2C_EEPROM:I2C接口操作EEPROM外设例程 - | | | | |-- I2C_PEC:使用PEC错误校验,Master/Slave 模式收发例程 + | | | | |-- I2C_PEC:使用PEC错误校验,Master/Slave 模式收发例程 + | | | | |-- I2C_7bit_Interrupt_Mode:使用中断进行收发例程 | | | |-- INT | | | | |-- Interrupt_VTF:使用VTF IRQ中断例程 | | | |-- IAP:IAP升级例程-内置Hex转Bin工具和IAP升级工具 | | | | |-- USB_UART:USB+UART IAP例程 - | | | |-- IWDG:独立看门狗例程 - | | | |-- OPA:OPA4作电压跟随器输出例程 + | | | |-- IWDG + | | | | |-- IWDG:独立看门狗例程 + | | | |-- OPA + | | | | |-- OPA_Voltage_Follower:OPA4作电压跟随器输出例程 | | | |-- PWR | | | | |-- Sleep_Mode:低功耗,睡眠模式例程 | | | | |-- Standby_Mode:低功耗,待机模式例程 @@ -145,8 +159,10 @@ https://wch.cn | | | | |-- Get_CLK:获取system-HCLK-AHB1-AHB2时钟例程 | | | | |-- MCO:MCO引脚时钟输出例程 | | | | |-- HSI_PLL_Source:HSI或HSI/2作为PLL输入时钟例程 - | | | |-- RT-Thread:RT-Thread移植例程 - | | | |-- RTC:日历例程 + | | | |-- RT-Thread + | | | | |-- RT_Thread_Core:RT-Thread移植例程 + | | | |-- RTC + | | | | |-- RTC_Calendar:日历例程 | | | |-- SPI | | | | |-- 1Lines_half-duplex:单线半双工模式,Master/Slave 模式数据收发 | | | | |-- 2Lines_FullDuplex:双线全双工模式,Master/Slave 模式数据收发 @@ -154,9 +170,13 @@ https://wch.cn | | | | |-- SPI_CRC:使用CRC错误校验,Master/Slave 模式收发例程 | | | | |-- SPI_DMA:SPI使用DMA,Master/Slave 模式收发例程 | | | | |-- SPI_FLASH:SPI接口操作FLASH外设例程 - | | | |-- TencentOS:TencentOS移植例程 - | | | |-- SDI_Printf:调试口打印例程 - | | | |-- SYSTICK:滴答定时器例程 + | | | | |-- SPI_LCD:SPI接口操作FLASH和LCD例程 + | | | |-- TencentOS + | | | | |-- TencentOS:TencentOS移植例程 + | | | |-- SDI_Printf + | | | | |-- SDI_Printf:调试口打印例程 + | | | |-- SYSTICK + | | | | |-- SYSTICK_Interrupt:滴答定时器例程 | | | |-- TIM | | | | |-- Clock_Select:时钟源选择例程 | | | | |-- ComplementaryOutput_DeadTime:互补输出和死区插入模式例程 @@ -168,7 +188,8 @@ https://wch.cn | | | | |-- Synchro_ExtTrigger:从模式例程,包括复位模式、门控模式、触发模式 | | | | |-- Synchro_Timer:定时器同步模式 | | | | |-- TIM_DMA:定时器使用DMA例程 - | | | |-- TOUCHKEY:TouchKey检测例程 + | | | |-- TOUCHKEY + | | | | |-- TKey:TouchKey检测例程 | | | |-- USART | | | | |-- USART_DMA:USART使用DMA,Master/Slave 模式收发例程 | | | | |-- USART_HalfDuplex:单线半双工模式,Master/Slave 模式收发例程 @@ -178,6 +199,7 @@ https://wch.cn | | | | |-- USART_Polling:轮询收发模式,Master/Slave 模式收发例程 | | | | |-- USART_Printf:串口打印调试例程 | | | | |-- USART_SynchronousMode:同步模式,Master/Slave 模式收发例程 + | | | | |-- USART_Idle_Recv:串口使用DMA进行不定长接收例程 | | | |-- USB | | | | |-- USBD | | | | | |-- CH372:模拟自定义USB设备(CH372设备),端点1,3下传,2,4上传,端点1下传的数据从端点3上传,不取反,端点2下传的数据从端点4上传,取反。 @@ -204,3 +226,4 @@ https://wch.cn | | | | | |-- Udisk_Lib:U盘文件系统库文件 | | | | | |-- Host_Udisk:USB主机操作U盘例程 | | | |-- WWDG:窗口看门狗例程 + | | | | |-- WWDG:窗口看门狗例程 diff --git a/EVT/CH32V20x_List_EN.txt b/EVT/CH32V20x_List_EN.txt index 1cdb2e4..67f124f 100644 --- a/EVT/CH32V20x_List_EN.txt +++ b/EVT/CH32V20x_List_EN.txt @@ -35,6 +35,11 @@ Directory | | | | |-- DualADC_SlowInterleaved: dual ADC slow interleaved sampling routine, only applied to CH32V20x_D6 | | | | |-- ExtLines_Trigger: external lines trigger ADC conversion routine | | | | |-- Internal_Temperature: internal temperature sensor routine + | | | |-- APPLICATION + | | | | |-- USBPD + | | | | | |-- I2C_OP_CH223锛欼2C Operates CH223 routine, only applied to CH32V20x_D8 + | | | | |-- WS2812_LED + | | | | | |-- WS2812_LED锛歋PI\PWM Operates WS2812_LED routine | | | |-- BLE ----only for CH32V20x_D8W | | | | |-- BackupUpgrade_IAP: backup wireless upgrade IAP routine. Detect the current code flag, determine whether to move the code from the backup area to the user area, and run the code in the user area | | | | |-- BackupUpgrade_OTA: backup wireless upgrade user routine. Add OTA function to peripheral slave routines, save the upgrade firmware to the backup area and jump to the IAP program to upgrade @@ -91,12 +96,14 @@ Directory | | | | | |-- MESH_LIB: MESH protocol stack library file and header file | | | | | |-- Qinheng MESH APP Management Distribution Network Application Manual.pdf | | | | | |-- Qinheng Low Energy Bluetooth MESH Software Development Reference Manual.pdf - | | | |-- BKP: BKP routine + | | | |-- BKP + | | | | |-- BKP:BKP routines | | | |-- CAN ----only for CH32V20x_D6-CH32V20x_D8W | | | | |-- Networking: CAN routine: normal mode, standard frame and expanded frame data transceiver | | | | |-- TestMode: test mode, including silent mode, loopback mode and loopback silent mode | | | | |-- Time-triggered: time triggered communication mode - | | | |-- CRC: CRC routine + | | | |-- CRC + | | | | |-- CRC_Calculation:CRC routines | | | |-- DMA | | | | |-- DMA_MEM2MEM: Memory to memory mode routine | | | | |-- DMA_MEM2PERIP: Memory to peripheral mode, and peripheral to memory mode routine, see peripheral sub-routines @@ -116,24 +123,31 @@ Directory | | | | |-- UdpClient: demonstration routine that Udp Client receives data and returns data | | | | |-- UdpServer: demonstration routine that Udp Server receives data and returns data | | | | |-- WebServer: Web Server routine, to demonstrate the configuration of WCHNET device functions through Web browser. As the WCHNET devices have built-in web server, the network parameter configuration and password management of WCHNET can be implemented on the web page. - | | | |-- EXTI: external interrupt line routine - | | | |-- FLASH: FLASH erase/read/write, and fast programming + | | | |-- EXTI + | | | | |-- EXTI0:External Interrupt Routine + | | | |-- FLASH + | | | | |-- FLASH_Program:FLASH erase/read/write, and fast programming | | | |-- FreeRTOS | | | | |-- FreeRTOS_Core:FreeRTOS migration routines - | | | |-- GPIO: GPIO routine - | | | |-- HarmonyOS:HarmonyOS migration routine + | | | |-- GPIO + | | | | |-- GPIO_Toggle:GPIO routines + | | | |-- HarmonyOS + | | | | |-- LiteOS_m锛欻armonyOS migration routine | | | |-- I2C | | | | |-- I2C_7bit_Mode: 7-bit addressing mode, master/slave mode, transceiver routine | | | | |-- I2C_10bit_Mode: 10-bit addressing mode, master/slave mode transceiver routine | | | | |-- I2C_DMA: I2C DMA, master/slave mode transceiver routine | | | | |-- I2C_EEPROM: I2C interface routine to operate EEPROM peripheral | | | | |-- I2C_PEC: PEC error check, master/slave mode transceiver routine + | | | | |-- I2C_7bit_Interrupt_Mode锛歎se interrupt to receive and send routine | | | |-- INT | | | | |-- Interrupt_VTF: VTF IRQ interrupt routine | | | |-- IAP: IAP upgrade routine, including the Hex to Bin tool and IAP upgrade tool | | | | |-- USB_UART: USB+UART IAP routine - | | | |-- IWDG: independent watchdog routine - | | | |-- OPA: OPA4 as voltage follower output routine + | | | |-- IWDG + | | | | |-- IWDG:Independent Watchdog routine + | | | |-- OPA + | | | | |-- OPA_Voltage_Follower锛歄PA4 as voltage follower output routine | | | |-- PWR | | | | |-- Sleep_Mode: low power, sleep mode routine | | | | |-- Standby_Mode: low power, standby mode routine @@ -144,8 +158,10 @@ Directory | | | | |-- Get_CLK:Get system-HCLK-AHB1-AHB2 clock routine | | | | |-- MCO: MCO pin clock output routine | | | | |-- HSI_PLL_Source:HSI or HSI/2 as PLL input clock routine - | | | |-- RT-Thread:RT thread migration routine - | | | |-- RTC: calendar routine + | | | |-- RT-Thread + | | | | |-- RT_Thread_Core:RT-Thread migration routine + | | | |-- RTC + | | | | |-- RTC_Calendar:Calendar Routines | | | |-- SPI | | | | |-- 1Lines_half-duplex: single wire half duplex mode, master/slave mode, data transceiver | | | | |-- 2Lines_FullDuplex: two-wire full duplex mode, master/slave mode, data transceiver @@ -153,10 +169,13 @@ Directory | | | | |-- SPI_CRC: CRC error check and master/slave mode transceiver routine | | | | |-- SPI_DMA: SPI DMA, master/slave mode transceiver routine | | | | |-- SPI_FLASH: SPI interface operation flash peripheral routine - | | | |-- TencentOS: TencentOS migration routine + | | | | |-- SPI_LCD:SPI interface operation FLASH and LCD peripheral routines + | | | |-- TencentOS + | | | | |-- TencentOS:TencentOS migration routine | | | |-- SDI_Printf | | | | |-- SDI_Printf锛歞ebug interface SDI, virtual serial port. - | | | |-- SYSTICK: Systick timer routine + | | | |-- SYSTICK + | | | | |-- SYSTICK_Interrupt锛歋ystick timer routine | | | |-- TIM | | | | |-- Clock_Select: clock source selection routine | | | | |-- ComplementaryOutput_DeadTime: complementary output and deadband insertion mode routines @@ -168,7 +187,8 @@ Directory | | | | |-- Synchro_ExtTrigger:slave mode routine, including reset mode, gating mode and trigger mode | | | | |-- Synchro_Timer:timer synchronization mode | | | | |-- TIM_DMA: timer DMA routines - | | | |-- TOUCHKEY:touchkey detection routine + | | | |-- TOUCHKEY + | | | | |-- TKey:TouchKey Detection Routine | | | |-- USART | | | | |-- USART_DMA: USART DMA, master/slave mode transceiver routine | | | | |-- USART_HalfDuplex: single wire half duplex mode, master/slave mode transceiver routine @@ -178,6 +198,8 @@ Directory | | | | |-- USART_Polling: polling transceiver mode, master/slave transceiver routine | | | | |-- USART_Printf: USART Print debugging routine | | | | |-- USART_SynchronousMode: synchronous mode, master/slave mode, transceiver routine + | | | | |-- USART_Printf:Serial port printing debugging routines + | | | | |-- USART_Idle_Recv锛歎SART use DMA receiving indeterminate length data routines | | | |-- USB | | | | |-- USBD | | | | | |-- CH372:Simulates a custom USB device (CH372 device) with endpoints 1, 3 down and 2, 4 up. Data down from endpoint 1 is uploaded from endpoint 3 and not reversed, and data down from endpoint 2 is uploaded from endpoint 4 and reversed. @@ -203,5 +225,6 @@ Directory | | | | | |-- HOST_MTP_FileSystem:Enumeration process of a USB host to a device that supports MTP and PTP protocols, supports MTP and PTP protocols, and reads its files | | | | | |-- Host_Udisk:USB host operation USB disk routine | | | | | |-- Udisk_Lib:U disk file system library file - | | | |-- WWDG: window watchdog routine + | | | |-- WWDG + | | | | |-- WWDG:Window Watchdog Routine diff --git a/EVT/EXAM/ADC/ADC_DMA/User/main.c b/EVT/EXAM/ADC/ADC_DMA/User/main.c index b42e490..429f9ed 100644 --- a/EVT/EXAM/ADC/ADC_DMA/User/main.c +++ b/EVT/EXAM/ADC/ADC_DMA/User/main.c @@ -149,7 +149,7 @@ void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsiz */ u16 Get_ConversionVal(s16 val) { - if((val + Calibrattion_Val) < 0) + if((val + Calibrattion_Val) < 0 || val==0) return 0; if((Calibrattion_Val + val) > 4095||val==4095) return 4095; diff --git a/EVT/EXAM/ADC/ADC_DMA/User/system_ch32v20x.c b/EVT/EXAM/ADC/ADC_DMA/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/ADC_DMA/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/ADC_DMA/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/AnalogWatchdog/User/system_ch32v20x.c b/EVT/EXAM/ADC/AnalogWatchdog/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/AnalogWatchdog/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/AnalogWatchdog/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/Auto_Injection/User/main.c b/EVT/EXAM/ADC/Auto_Injection/User/main.c index a8898b9..a226c06 100644 --- a/EVT/EXAM/ADC/Auto_Injection/User/main.c +++ b/EVT/EXAM/ADC/Auto_Injection/User/main.c @@ -54,6 +54,7 @@ void ADC_Function_Init(void) ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); + ADC_InjectedSequencerLengthConfig(ADC1, 1); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5); ADC_InjectedChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_239Cycles5); ADC_AutoInjectedConvCmd(ADC1, ENABLE); @@ -119,7 +120,7 @@ u16 Get_ADC_Val(u8 ch) */ u16 Get_ConversionVal(s16 val) { - if((val + Calibrattion_Val) < 0) + if((val + Calibrattion_Val) < 0||val==0) return 0; if((Calibrattion_Val + val) > 4095||val==4095) return 4095; diff --git a/EVT/EXAM/ADC/Auto_Injection/User/system_ch32v20x.c b/EVT/EXAM/ADC/Auto_Injection/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/Auto_Injection/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/Auto_Injection/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/Discontinuous_mode/User/main.c b/EVT/EXAM/ADC/Discontinuous_mode/User/main.c index 97551f5..a98808d 100644 --- a/EVT/EXAM/ADC/Discontinuous_mode/User/main.c +++ b/EVT/EXAM/ADC/Discontinuous_mode/User/main.c @@ -57,7 +57,7 @@ void ADC_Function_Init(void) ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigInjecConv_T1_CC4; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; - ADC_InitStructure.ADC_NbrOfChannel = 3; + ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_InjectedSequencerLengthConfig(ADC1, 3); @@ -133,7 +133,7 @@ void TIM1_PWM_In(u16 arr, u16 psc, u16 ccp) */ u16 Get_ConversionVal(s16 val) { - if((val + Calibrattion_Val) < 0) + if((val + Calibrattion_Val) < 0||val==0) return 0; if((Calibrattion_Val + val) > 4095||val==4095) return 4095; diff --git a/EVT/EXAM/ADC/Discontinuous_mode/User/system_ch32v20x.c b/EVT/EXAM/ADC/Discontinuous_mode/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/Discontinuous_mode/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/Discontinuous_mode/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/main.c b/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/main.c index a74d758..e40f32f 100644 --- a/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/main.c +++ b/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/main.c @@ -151,7 +151,7 @@ void TIM2_PWM_In(u16 arr, u16 psc, u16 ccp) */ u16 Get_ConversionVal1(s16 val) { - if((val + Calibrattion_Val1) < 0) + if((val + Calibrattion_Val1) < 0|| val==0) return 0; if((Calibrattion_Val1 + val) > 4095) return 4095; diff --git a/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_AlternateTrigger/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/main.c b/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/main.c index 4e824bc..321c46e 100644 --- a/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/main.c +++ b/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/main.c @@ -166,7 +166,7 @@ void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsiz */ u16 Get_ConversionVal1(s16 val) { - if((val + Calibrattion_Val1) < 0) + if((val + Calibrattion_Val1) < 0|| val==0) return 0; if((Calibrattion_Val1 + val) > 4095||val==4095) return 4095; diff --git a/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_Combined_RegInjectionSimul/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_FastInterleaved/User/main.c b/EVT/EXAM/ADC/DualADC_FastInterleaved/User/main.c index 61d5d7d..46a5ccd 100644 --- a/EVT/EXAM/ADC/DualADC_FastInterleaved/User/main.c +++ b/EVT/EXAM/ADC/DualADC_FastInterleaved/User/main.c @@ -110,7 +110,7 @@ void ADC_Function_Init(void) */ u16 Get_ConversionVal1(s16 val) { - if((val + Calibrattion_Val1) < 0) + if((val + Calibrattion_Val1) < 0|| val==0) return 0; if((Calibrattion_Val1 + val) > 4095||val==4095) return 4095; diff --git a/EVT/EXAM/ADC/DualADC_FastInterleaved/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_FastInterleaved/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_FastInterleaved/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_FastInterleaved/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_InjectionSimul/User/main.c b/EVT/EXAM/ADC/DualADC_InjectionSimul/User/main.c index 2de8e1f..697147d 100644 --- a/EVT/EXAM/ADC/DualADC_InjectionSimul/User/main.c +++ b/EVT/EXAM/ADC/DualADC_InjectionSimul/User/main.c @@ -101,9 +101,9 @@ void ADC_Function_Init(void) */ u16 Get_ConversionVal1(s16 val) { - if((val + Calibrattion_Val1) < 0) + if((val + Calibrattion_Val1) < 0|| val==0) return 0; - if((Calibrattion_Val1 + val) > 4095) + if((Calibrattion_Val1 + val) > 4095|| val==4095) return 4095; return (val + Calibrattion_Val1); } diff --git a/EVT/EXAM/ADC/DualADC_InjectionSimul/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_InjectionSimul/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_InjectionSimul/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_InjectionSimul/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_RegSimul/User/main.c b/EVT/EXAM/ADC/DualADC_RegSimul/User/main.c index 3274a6e..d3853a6 100644 --- a/EVT/EXAM/ADC/DualADC_RegSimul/User/main.c +++ b/EVT/EXAM/ADC/DualADC_RegSimul/User/main.c @@ -16,7 +16,7 @@ *ADC1 channel 1 (PA1), ADC2 channel 3 (PA3)), and the rule group channel obtains *dual ADC conversion data through DMA interrupt. * - *Note锟斤拷only applied toCH32V203 + *Note:only applied toCH32V203 */ #include "debug.h" @@ -162,9 +162,9 @@ u16 Get_ConversionVal1(s16 val) */ u16 Get_ConversionVal2(s16 val) { - if((val + Calibrattion_Val2) < 0) + if((val + Calibrattion_Val2) < 0||val==0) return 0; - if((Calibrattion_Val2 + val) > 4095) + if((Calibrattion_Val2 + val) > 4095||val==4095) return 4095; return (val + Calibrattion_Val2); } diff --git a/EVT/EXAM/ADC/DualADC_RegSimul/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_RegSimul/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_RegSimul/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_RegSimul/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/main.c b/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/main.c index a9d71e0..eef2acf 100644 --- a/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/main.c +++ b/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/main.c @@ -111,7 +111,7 @@ void ADC_Function_Init(void) */ u16 Get_ConversionVal1(s16 val) { - if((val + Calibrattion_Val1) < 0) + if((val + Calibrattion_Val1) < 0|| val==0) return 0; if((Calibrattion_Val1 + val) > 4095 || val == 4095) return 4095; diff --git a/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/system_ch32v20x.c b/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/DualADC_SlowInterleaved/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/ExtLines_Trigger/User/main.c b/EVT/EXAM/ADC/ExtLines_Trigger/User/main.c index 9e785dc..4102332 100644 --- a/EVT/EXAM/ADC/ExtLines_Trigger/User/main.c +++ b/EVT/EXAM/ADC/ExtLines_Trigger/User/main.c @@ -116,7 +116,7 @@ void EXTI_Event_Init(void) */ u16 Get_ConversionVal(s16 val) { - if((val + Calibrattion_Val) < 0) + if((val + Calibrattion_Val) < 0|| val==0) return 0; if((Calibrattion_Val + val) > 4095 || val==4095) return 4095; diff --git a/EVT/EXAM/ADC/ExtLines_Trigger/User/system_ch32v20x.c b/EVT/EXAM/ADC/ExtLines_Trigger/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/ExtLines_Trigger/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/ExtLines_Trigger/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/ADC/Internal_Temperature/User/main.c b/EVT/EXAM/ADC/Internal_Temperature/User/main.c index dd2cceb..dd27c5d 100644 --- a/EVT/EXAM/ADC/Internal_Temperature/User/main.c +++ b/EVT/EXAM/ADC/Internal_Temperature/User/main.c @@ -161,9 +161,9 @@ u16 Get_ADC_Average(u8 ch, u8 times) */ u16 Get_ConversionVal(s16 val) { - if((val + Calibrattion_Val) < 0) + if((val + Calibrattion_Val) < 0|| val==0) return 0; - if((Calibrattion_Val + val) > 4095) + if((Calibrattion_Val + val) > 4095|| val==4095) return 4095; return (val + Calibrattion_Val); } diff --git a/EVT/EXAM/ADC/Internal_Temperature/User/system_ch32v20x.c b/EVT/EXAM/ADC/Internal_Temperature/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/ADC/Internal_Temperature/User/system_ch32v20x.c +++ b/EVT/EXAM/ADC/Internal_Temperature/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/APPLICATION/USBPD/CH223 Demo鏉夸娇鐢ㄨ鏄�.pdf b/EVT/EXAM/APPLICATION/USBPD/CH223 Demo鏉夸娇鐢ㄨ鏄�.pdf new file mode 100644 index 0000000..0ae9a38 Binary files /dev/null and b/EVT/EXAM/APPLICATION/USBPD/CH223 Demo鏉夸娇鐢ㄨ鏄�.pdf differ diff --git a/EVT/EXAM/APPLICATION/USBPD/CH233 Demo Board Reference-EN.pdf b/EVT/EXAM/APPLICATION/USBPD/CH233 Demo Board Reference-EN.pdf new file mode 100644 index 0000000..fd33a50 Binary files /dev/null and b/EVT/EXAM/APPLICATION/USBPD/CH233 Demo Board Reference-EN.pdf differ diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.cproject b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.cproject new file mode 100644 index 0000000..adbd3f0 --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.cproject @@ -0,0 +1,143 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.project b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.project new file mode 100644 index 0000000..13aa2f8 --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.project @@ -0,0 +1,64 @@ + + + I2C_OP_CH223 + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + Core + 2 + PARENT-3-PROJECT_LOC/SRC/Core + + + Debug + 2 + PARENT-3-PROJECT_LOC/SRC/Debug + + + Peripheral + 2 + PARENT-3-PROJECT_LOC/SRC/Peripheral + + + Startup + 2 + PARENT-3-PROJECT_LOC/SRC/Startup + + + ld + 2 + PARENT-3-PROJECT_LOC/SRC/Ld + + + + + 1595986042669 + + 22 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.wvproj + + + + diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.template b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.template new file mode 100644 index 0000000..f9de1ff --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/.template @@ -0,0 +1,17 @@ +Mcu Type=CH32V20x +Address=0x08000000 +Target Path=obj\I2C_OP_CH223.hex +Erase All=true +Program=true +Verify=true +Reset=true + +Vendor=WCH +Link=WCH-Link +Toolchain=RISC-V +Series=CH32V203 +Description=ROM(byte): 64K, SRAM(byte): 20K, CHIP PINS: 32, GPIO PORTS: 27.\nWCH CH32V2 series of mainstream MCUs covers the needs of a large variety of applications in the industrial,medical and consumer markets. High performance with first-class peripherals and low-power,low-voltage operation is paired with a high level of integration at accessible prices with a simple architecture and easy-to-use tools. + +PeripheralVersion=1.0 +MCU=CH32V203RBT6 +CLKSpeed=1 diff --git a/EVT/EXAM/BKP/BKP.wvproj b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/I2C_OP_CH223.wvproj similarity index 100% rename from EVT/EXAM/BKP/BKP.wvproj rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/I2C_OP_CH223.wvproj diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.c b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.c new file mode 100644 index 0000000..825189e --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.c @@ -0,0 +1,300 @@ +/********************************** (C) COPYRIGHT ******************************* + * File Name : CH223.c + * Author : WCH + * Version : V1.0.0 + * Date : 2023/08/15 + * Description : CH223芯片操作相关文件 +********************************************************************************* +* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. +* Attention: This software (modified or not) and binary are used for +* microcontroller manufactured by Nanjing Qinheng Microelectronics. +*******************************************************************************/ + +/******************************************************************************/ +#include "debug.h" +#include "CH223.h" +#include "string.h" + +u32 Buffer[7] = {0};//NDO raw data in SRCCAP +u16 BufferV[SIZE] = {0};//Analyzing voltage data in SRCCAP +u16 BufferI[SIZE] = {0};//Analyzing Current Data in SRCCAP +u16 BufferVPPS[SIZE] = {0};//Parsing PPS data in SRCCAP + +/********************************************************************* + * @fn IIC_Init + * + * @brief Initializes the IIC peripheral. + * + * PB11------SDA + * PB10------SCL + * + * @return none + */ +void IIC_Init(u32 bound, u16 address) +{ + GPIO_InitTypeDef GPIO_InitStructure={0}; + I2C_InitTypeDef I2C_InitTSturcture={0}; + + RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE ); +// GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE); + RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C2, ENABLE ); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init( GPIOB, &GPIO_InitStructure ); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init( GPIOB, &GPIO_InitStructure ); + + I2C_InitTSturcture.I2C_ClockSpeed = bound; + I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C; + I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9; + I2C_InitTSturcture.I2C_OwnAddress1 = address; + I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable; + I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; + I2C_Init( I2C2, &I2C_InitTSturcture ); + + I2C_Cmd( I2C2, ENABLE ); +} +/********************************************************************* + * @fn CH223_Init + * + * @brief Initializes CH223. + * + * @return none + */ +void CH223_Init() +{ + IIC_Init(100000, 0XA0); +} + +/********************************************************************* + * @fn CH223_ReadOneByte + * + * @brief Read one data from CH223. + * + * @param ReadAddr - Read frist address. + * + * @return temp - Read data. + */ +u8 CH223_ReadOneByte(u16 ReadAddr) +{ + u8 temp = 0; + while( I2C_GetFlagStatus( I2C2, I2C_FLAG_BUSY ) != RESET ) + { + + } + I2C_GenerateSTART( I2C2, ENABLE ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_MODE_SELECT ) ); + + I2C_Send7bitAddress( I2C2, 0X44, I2C_Direction_Transmitter ); + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ); + + #if (Address_Lenth == Address_8bit) + I2C_SendData( I2C2, (u8)(ReadAddr&0x00FF) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + #elif (Address_Lenth == Address_16bit) + I2C_SendData( I2C2, (u8)(ReadAddr>>8) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + I2C_SendData( I2C2, (u8)(ReadAddr&0x00FF) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + #endif + + I2C_GenerateSTART( I2C2, ENABLE ); + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_MODE_SELECT ) ); + I2C_Send7bitAddress( I2C2, 0X45, I2C_Direction_Receiver ); + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) ); + while( I2C_GetFlagStatus( I2C2, I2C_FLAG_RXNE ) == RESET ) + I2C_AcknowledgeConfig( I2C2, DISABLE ); + + temp = I2C_ReceiveData( I2C2 ); + I2C_GenerateSTOP( I2C2, ENABLE ); + + return temp; +} + +/********************************************************************* + * @fn CH223_WriteOneByte + * + * @brief Write one data to CH223. + * + * @param WriteAddr - Write frist address. + * + * @return DataToWrite - Write data. + */ +void CH223_WriteOneByte(u16 WriteAddr, u8 DataToWrite) +{ + while( I2C_GetFlagStatus( I2C2, I2C_FLAG_BUSY ) != RESET ); + I2C_GenerateSTART( I2C2, ENABLE ); + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_MODE_SELECT ) ); + I2C_Send7bitAddress( I2C2, 0X45, I2C_Direction_Transmitter ); + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ); + + #if (Address_Lenth == Address_8bit) + I2C_SendData( I2C2, (u8)(WriteAddr&0x00FF) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + #elif (Address_Lenth == Address_16bit) + I2C_SendData( I2C2, (u8)(WriteAddr>>8) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + I2C_SendData( I2C2, (u8)(WriteAddr&0x00FF) ); + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + + #endif + + if( I2C_GetFlagStatus( I2C2, I2C_FLAG_TXE ) != RESET ) + { + I2C_SendData( I2C2, DataToWrite ); + } + + while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); + I2C_GenerateSTOP( I2C2, ENABLE ); +} + +/********************************************************************* + * @fn CH223_Read + * + * @brief Read multiple data from CH223. + * + * @param ReadAddr - Read frist address. + * pBuffer - Read data. + * NumToRead - Data number. + * + * @return none + */ +void CH223_Read(u16 ReadAddr, u32 *pBuffer, u16 NumToRead) +{ + u8 t = 0; + u32 temp[4] = {0}; + u32 temp1 = 0; + + while(NumToRead) + { + temp[4] = 0; + for (t = 0; t < 4; t++) + { + temp[t] = CH223_ReadOneByte(ReadAddr++); + } + temp1 = (temp[0] + (temp[1] << 8) + (temp[2] << 16) + (temp[3] << 24)); + *pBuffer++=temp1; + NumToRead--; + } +} +/********************************************************************* + * @fn CH223_Write + * + * @brief Write multiple data to CH223. + * + * @param WriteAddr - Write frist address. + * pBuffer - Write data. + * NumToWrite - Data number. + * + * @return none + */ +void CH223_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite) +{ + while(NumToWrite--) + { + CH223_WriteOneByte(WriteAddr, *pBuffer); + WriteAddr++; + pBuffer++; + Delay_Ms(2); + } +} + + +/********************************************************************* + * @fn Buffer_Change + * + * @brief Analysis of SRCCAP message + * + * @param buffer[] - NDO data. + * n - NDO_Number. + * + * @return none +*/ +void Buffer_Change(u32 buffer[], int n) +{ + int i = 0; + u32 tempV = 0, tempI = 0, tempVmax = 0; + for(i = 0; i < n; i++) + { + tempV = buffer[i]; + tempI = buffer[i]; + if((tempV & 0xC0000000) == 0) + { + tempV >>= 10; + tempV &= 0x03FF; + BufferV[i] = tempV * 0.5; + + tempI &= 0x03FF; + BufferI[i] = tempI * 0.1; + } + else { + tempV >>= 8; + tempVmax = tempV >> 9; + tempV &= 0x00FF; + BufferV[i] = tempV * 1; + tempVmax &= 0x00FF; + BufferVPPS[i] = tempVmax * 1; + + tempI &= 0x00FF; + BufferI[i] = tempI * 0.5; + } + } +} +/********************************************************************* + * @fn Current_VI + * + * @brief Read and display the current output voltage value + * + * @param ndo_select - NDO_Select. + * + * + * @return none +*/ +void Current_VI(u8 ndo_select) +{ + u8 tempV = 0, tempI = 0; + u8 current[2]; + tempV = BufferV[ndo_select] % 10; + current[0] = BufferV[ndo_select] / 10; + + tempI = BufferI[ndo_select] % 10; + current[1] = BufferI[ndo_select] / 10; + + printf("The current regulating voltage is:%d.%dV,%d.%dA\t\r\n", current[0], tempV, current[1], tempI); +} +/********************************************************************* + * @fn User_Set_VI + * + * @brief Set VBUS voltage + * + * @param TargetData - Target voltage value. + * VBUS voltage=TargetData*200mv + * + * @return none +*/ + +void User_Set_VI(u8 TargetData) +{ + u8 ndo_select = 0, state_flag = 0; + CH223_WriteOneByte(0xB6, TargetData); + Delay_Ms(5000); + state_flag = CH223_ReadOneByte(0xB5); + ndo_select = (state_flag >> 3) & 0x07; + printf("nod_select_user-->%d\r\n",ndo_select); + Current_VI(ndo_select - 1); +} diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.h b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.h new file mode 100644 index 0000000..c6ba34e --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/CH223.h @@ -0,0 +1,33 @@ +/********************************** (C) COPYRIGHT ******************************* + * File Name : CH223.h + * Author : WCH + * Version : V1.0.0 + * Date : 2023/08/15 + * Description : header file for CH223.c +********************************************************************************* +* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. +* Attention: This software (modified or not) and binary are used for +* microcontroller manufactured by Nanjing Qinheng Microelectronics. +*******************************************************************************/ + +#include"debug.h" + +#define Address_8bit 0 +#define Address_16bit 1 +#define Address_Lenth Address_8bit +#define SIZE sizeof(Buffer) + +extern u32 Buffer[7] ;//NDO raw data in SRCCAP +extern u16 BufferV[SIZE];//Analyzing voltage data in SRCCAP +extern u16 BufferI[SIZE];//Analyzing Current Data in SRCCAP +extern u16 BufferVPPS[SIZE];//Parsing PPS data in SRCCAP + +void IIC_Init(u32 bound, u16 address); +void CH223_Init(); +u8 CH223_ReadOneByte(u16 ReadAddr); +void CH223_WriteOneByte(u16 WriteAddr, u8 DataToWrite); +void CH223_Read(u16 ReadAddr, u32 *pBuffer, u16 NumToRead); +void CH223_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite); +void Buffer_Change(u32 buffer[], int n); +void Current_VI(u8 ndo_select); +void User_Set_VI(u8 TargetData); diff --git a/EVT/EXAM/BKP/User/ch32v20x_conf.h b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_conf.h similarity index 100% rename from EVT/EXAM/BKP/User/ch32v20x_conf.h rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_conf.h diff --git a/EVT/EXAM/IWDG/User/ch32v20x_it.c b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_it.c similarity index 100% rename from EVT/EXAM/IWDG/User/ch32v20x_it.c rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_it.c diff --git a/EVT/EXAM/BKP/User/ch32v20x_it.h b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_it.h similarity index 100% rename from EVT/EXAM/BKP/User/ch32v20x_it.h rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/ch32v20x_it.h diff --git a/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/main.c b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/main.c new file mode 100644 index 0000000..429a433 --- /dev/null +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/main.c @@ -0,0 +1,392 @@ +/********************************** (C) COPYRIGHT ******************************* + * File Name : main.c + * Author : WCH + * Version : V1.0.0 + * Date : 2023/06/06 + * Description : Main program body. +********************************************************************************* +* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd. +* Attention: This software (modified or not) and binary are used for +* microcontroller manufactured by Nanjing Qinheng Microelectronics. +*******************************************************************************/ + +/* + *@Note +CH223 chip application routine + +*/ + +#include "debug.h" +#include"CH223.h" + +u8 ndo = 0;//Number of NDOs in SrcCap +u8 IT_flag = 0; +u8 rst_IT = 0, cc_IT = 0, srccap_IT = 0, ps_IT = 0;//Interrupt flag +s16 Calibrattion_Val = 0; +u16 TxBuf[1024]; + +/********************************************************************* + * @fn ADC_Function_Init + * + * @brief Initializes ADC collection. + * PB0-->ADC_IN + * + * @return none + */ +void ADC_Function_Init(void) +{ + ADC_InitTypeDef ADC_InitStructure = {0}; + GPIO_InitTypeDef GPIO_InitStructure = {0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); + RCC_ADCCLKConfig(RCC_PCLK2_Div8); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + ADC_DeInit(ADC1); + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + ADC_InitStructure.ADC_ScanConvMode = DISABLE; + ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_NbrOfChannel = 1; + ADC_Init(ADC1, &ADC_InitStructure); + + ADC_DMACmd(ADC1, ENABLE); + ADC_Cmd(ADC1, ENABLE); + + ADC_BufferCmd(ADC1, DISABLE); //disable buffer + ADC_ResetCalibration(ADC1); + while(ADC_GetResetCalibrationStatus(ADC1)); + ADC_StartCalibration(ADC1); + while(ADC_GetCalibrationStatus(ADC1)); + Calibrattion_Val = Get_CalibrationValue(ADC1); + +} + +/********************************************************************* + * @fn Get_ADC_Val + * + * @brief Returns ADCx conversion result data. + * + * @param ch - ADC channel. + * ADC_Channel_0 - ADC Channel0 selected. + * ADC_Channel_1 - ADC Channel1 selected. + * ADC_Channel_2 - ADC Channel2 selected. + * ADC_Channel_3 - ADC Channel3 selected. + * ADC_Channel_4 - ADC Channel4 selected. + * ADC_Channel_5 - ADC Channel5 selected. + * ADC_Channel_6 - ADC Channel6 selected. + * ADC_Channel_7 - ADC Channel7 selected. + * ADC_Channel_8 - ADC Channel8 selected. + * ADC_Channel_9 - ADC Channel9 selected. + * ADC_Channel_10 - ADC Channel10 selected. + * ADC_Channel_11 - ADC Channel11 selected. + * ADC_Channel_12 - ADC Channel12 selected. + * ADC_Channel_13 - ADC Channel13 selected. + * ADC_Channel_14 - ADC Channel14 selected. + * ADC_Channel_15 - ADC Channel15 selected. + * ADC_Channel_16 - ADC Channel16 selected. + * ADC_Channel_17 - ADC Channel17 selected. + * + * @return none + */ +u16 Get_ADC_Val(u8 ch) +{ + u16 val; + + ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5); + ADC_SoftwareStartConvCmd(ADC1, ENABLE); + + while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); + val = ADC_GetConversionValue(ADC1); + + return val; +} + +/********************************************************************* + * @fn DMA_Tx_Init + * + * @brief Initializes the DMAy Channelx configuration. + * + * @param DMA_CHx - x can be 1 to 7. + * ppadr - Peripheral base address. + * memadr - Memory base address. + * bufsize - DMA channel buffer size. + * + * @return none + */ +void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize) +{ + DMA_InitTypeDef DMA_InitStructure = {0}; + + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + + DMA_DeInit(DMA_CHx); + DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr; + DMA_InitStructure.DMA_MemoryBaseAddr = memadr; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + DMA_InitStructure.DMA_BufferSize = bufsize; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_Init(DMA_CHx, &DMA_InitStructure); +} +/********************************************************************* + * @fn Get_ConversionVal + * + * @brief Get Conversion Value. + * + * @param val - Sampling value + * + * @return val+Calibrattion_Val - Conversion Value. + */ +u16 Get_ConversionVal(s16 val) +{ + if((val + Calibrattion_Val) < 0) + return 0; + if((Calibrattion_Val + val) > 4095||val==4095) + return 4095; + return (val + Calibrattion_Val); +} + +/********************************************************************* + * @fn EXTI0_INT_INIT + * + * @brief Initialize external trigger interrupt. + * + * PB13-->INT + * + * @return none + */ +/********************************************************************* + * @fn EXTI0_INT_INIT + * + * @brief Initializes EXTI0 collection. + * + * @return none + */ +void EXTI0_INT_INIT(void) +{ + GPIO_InitTypeDef GPIO_InitStructure = {0}; + EXTI_InitTypeDef EXTI_InitStructure = {0}; + NVIC_InitTypeDef NVIC_InitStructure = {0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + /* GPIOB ----> EXTI_Line0 */ + GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13); + EXTI_InitStructure.EXTI_Line = EXTI_Line13; + EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; + EXTI_InitStructure.EXTI_LineCmd = ENABLE; + EXTI_Init(&EXTI_InitStructure); + + NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); +} + +/********************************************************************* + * @fn Adc_test + * + * @brief ADC measure VBUS output voltage. + * + * + * @return none + */ +void Adc_test(){ + u16 i = 0; + float sum=0; + DMA_Tx_Init(DMA1_Channel1, (u32)&ADC1->RDATAR, (u32)TxBuf, 1024); + DMA_Cmd(DMA1_Channel1, ENABLE); + ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5); + ADC_SoftwareStartConvCmd(ADC1, ENABLE); + Delay_Ms(50); + ADC_SoftwareStartConvCmd(ADC1, DISABLE); + for(i = 0; i < 1024; i++){ + sum+=Get_ConversionVal(TxBuf[i]); + } + printf("ADC_Value=%0.1fV\r\n",sum/1024/4095*3.3*6.1); +} + + +/********************************************************************* + * @fn main + * + * @brief Main program. + * + * @return none + */ +int main(void) +{ + u16 n = 0; + u8 tempV = 0, tempI = 0, tempV0 = 0, tempI0 = 0, tempVmax = 0; + u8 res = 0, ndo_select = 0, STATE_flag = 0; + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); + Delay_Init(); + USART_Printf_Init( 115200 ); + CH223_Init(); + ADC_Function_Init(); + EXTI0_INT_INIT(); + printf("PD_CH223 Demo!\r\n"); + printf("\r\n"); + IT_flag = CH223_ReadOneByte(0xB4); + if (IT_flag==0x0c) + { + printf("power-on reset!!!\r\n"); + printf("\r\n"); + Delay_Ms(10); + printf("CC Connecting...\r\n"); + printf("\r\n"); + } + while(1) + { + Delay_Ms(1); + while(ps_IT) //PS_RDY -->The power supply reaches the required working state + { + CH223_WriteOneByte(0xB6, 0x19); //The initial voltage is set to 9V:0x2D + res = CH223_ReadOneByte(0xB6); + printf("The initial voltage is set to:%dV\r\n",res*200/1000); + printf("\r\n"); + Delay_Ms(300); + if (rst_IT == 1) + { + printf("power-on reset!!!\r\n"); + printf("\r\n"); + } + if (cc_IT == 1) + { + printf("CC Connecting...\r\n"); + printf("\r\n"); + } + STATE_flag = CH223_ReadOneByte(0xB5); //Read IIC status register + ndo = STATE_flag & 0x07; //NDO_Number + printf("Number of NDOs in SRCCAP:%d\r\n", ndo); + printf("\r\n"); + CH223_Read(0x84, Buffer, ndo); //SRCCAP message + Buffer_Change(Buffer, ndo); + if (srccap_IT == 1) //Display SRCCAP message + { + printf("Received SrcCap,NDO data is:\n"); + printf("\r\n"); + for(n = 0; n < ndo; n++) + { + if((Buffer[n] & 0xC0000000) == 0) + { + tempV = BufferV[n] % 10; + tempV0 = BufferV[n] / 10; + tempI = BufferI[n] % 10; + tempI0 = BufferI[n] / 10; + printf("%d.%dV,%d.%dA\t", tempV0, tempV, tempI0, tempI); + } + else + { + tempV = BufferV[n] % 10; + tempV0 = BufferV[n] / 10; + tempVmax = BufferVPPS[n] % 10; + BufferVPPS[n] /= 10; + tempI = BufferI[n] % 10; + tempI0 = BufferI[n] / 10; + printf("%d.%dV/%d.%dV,%d.%dA\t", tempV0, tempV, BufferVPPS[n], tempVmax, tempI0, tempI); + } + } + printf("\r\n"); + } + ndo_select = (STATE_flag >> 3) & 0x07; + printf("ndo_select:%d\r\n", ndo_select); + printf("Waiting PS_RDY...\r\n"); + Delay_Ms(500); + if (ps_IT == 1) + { + printf("Received PS_RDY\r\n"); + Current_VI(ndo_select - 1); + } + //Set VBUS voltage + printf("\r\n"); + Delay_Ms(10); + printf("Set voltage output 5V-->0x19\r\n"); + User_Set_VI(0x19); //0x19---5V + Adc_test(); + Delay_Ms(50); + printf("\r\n"); + Delay_Ms(10); + printf("Set voltage output 9V-->0x2d\r\n"); + User_Set_VI(0x2d); //0x2d---9V + Adc_test(); + Delay_Ms(50); + printf("\r\n"); + Delay_Ms(10); + printf("Set voltage output 12V-->0x3c\r\n"); + User_Set_VI(0x3c); //0x3c---12V + Adc_test(); + Delay_Ms(50); + printf("\r\n"); + Delay_Ms(10); + printf("Set voltage output 15V-->0x4b\r\n"); + User_Set_VI(0x4b); //0x4b---15V + Adc_test(); + Delay_Ms(50); + printf("\r\n"); + Delay_Ms(10); + printf("Set voltage output 20V-->0x64\r\n"); + User_Set_VI(0x64); //0x64---20V + Adc_test(); + Delay_Ms(50); + ps_IT = 0; + } + } + +} + +void EXTI15_10_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); + +void EXTI15_10_IRQHandler(void) +{ + if(EXTI_GetITStatus(EXTI_Line13) != RESET) + { + Delay_Us(500); + if(EXTI_GetITStatus(EXTI_Line13) != RESET) + { + IT_flag = CH223_ReadOneByte(0xB4); + if ((IT_flag&0xF)) + { + if (IT_flag&0x7) + { + if(IT_flag&0x3) + { + if (IT_flag&0x1) + { + ps_IT = 1; + } + else + { + srccap_IT = 1; + } + } + else + { + rst_IT = 1; + } + }else + { + cc_IT = 1; + } + } + EXTI_ClearITPendingBit(EXTI_Line13); /* Clear Flag */ + } + } +} diff --git a/EVT/EXAM/RTC/User/system_ch32v20x.c b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/system_ch32v20x.c similarity index 99% rename from EVT/EXAM/RTC/User/system_ch32v20x.c rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/RTC/User/system_ch32v20x.c +++ b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/BKP/User/system_ch32v20x.h b/EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/system_ch32v20x.h similarity index 100% rename from EVT/EXAM/BKP/User/system_ch32v20x.h rename to EVT/EXAM/APPLICATION/USBPD/I2C_OP_CH223/User/system_ch32v20x.h diff --git a/EVT/EXAM/APPLICATION/WS2812_LED/.cproject b/EVT/EXAM/APPLICATION/WS2812_LED/.cproject new file mode 100644 index 0000000..35043f3 --- /dev/null +++ b/EVT/EXAM/APPLICATION/WS2812_LED/.cproject @@ -0,0 +1,143 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EVT/EXAM/OPA/.project b/EVT/EXAM/APPLICATION/WS2812_LED/.project similarity index 81% rename from EVT/EXAM/OPA/.project rename to EVT/EXAM/APPLICATION/WS2812_LED/.project index 41c6ac7..0974069 100644 --- a/EVT/EXAM/OPA/.project +++ b/EVT/EXAM/APPLICATION/WS2812_LED/.project @@ -1,6 +1,6 @@ - OPA + WS2812_LED @@ -35,27 +35,27 @@ Core 2 - PARENT-1-PROJECT_LOC/SRC/Core + PARENT-2-PROJECT_LOC/SRC/Core Debug 2 - PARENT-1-PROJECT_LOC/SRC/Debug + PARENT-2-PROJECT_LOC/SRC/Debug Ld 2 - PARENT-1-PROJECT_LOC/SRC/Ld + PARENT-2-PROJECT_LOC/SRC/Ld Peripheral 2 - PARENT-1-PROJECT_LOC/SRC/Peripheral + PARENT-2-PROJECT_LOC/SRC/Peripheral Startup 2 - PARENT-1-PROJECT_LOC/SRC/Startup + PARENT-2-PROJECT_LOC/SRC/Startup diff --git a/EVT/EXAM/APPLICATION/WS2812_LED/.template b/EVT/EXAM/APPLICATION/WS2812_LED/.template new file mode 100644 index 0000000..3c7f718 --- /dev/null +++ b/EVT/EXAM/APPLICATION/WS2812_LED/.template @@ -0,0 +1,17 @@ +Mcu Type=CH32V20x +Address=0x08000000 +Target Path=obj\WS2812_LED.hex +Erase All=true +Program=true +Verify=true +Reset=true + +Vendor=WCH +Link=WCH-Link +Toolchain=RISC-V +Series=CH32V203 +Description=ROM(byte): 64K, SRAM(byte): 20K, CHIP PINS: 32, GPIO PORTS: 27.\nWCH CH32V2 series of mainstream MCUs covers the needs of a large variety of applications in the industrial,medical and consumer markets. High performance with first-class peripherals and low-power,low-voltage operation is paired with a high level of integration at accessible prices with a simple architecture and easy-to-use tools. + +PeripheralVersion=1.0 +MCU=CH32V203RBT6 +CLKSpeed=1 diff --git a/EVT/EXAM/IWDG/User/ch32v20x_conf.h b/EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_conf.h similarity index 100% rename from EVT/EXAM/IWDG/User/ch32v20x_conf.h rename to EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_conf.h diff --git a/EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_it.c b/EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_it.c new file mode 100644 index 0000000..ea6d338 --- /dev/null +++ b/EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_it.c @@ -0,0 +1,45 @@ +/********************************** (C) COPYRIGHT ******************************* + * File Name : ch32v20x_it.c + * Author : WCH + * Version : V1.0.0 + * Date : 2021/06/06 + * Description : Main Interrupt Service Routines. +********************************************************************************* +* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. +* Attention: This software (modified or not) and binary are used for +* microcontroller manufactured by Nanjing Qinheng Microelectronics. +*******************************************************************************/ +#include "ch32v20x_it.h" + +void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); + +/********************************************************************* + * @fn NMI_Handler + * + * @brief This function handles NMI exception. + * + * @return none + */ +void NMI_Handler(void) +{ +} + +/********************************************************************* + * @fn HardFault_Handler + * + * @brief This function handles Hard Fault exception. + * + * @return none + */ +void HardFault_Handler(void) +{ + printf("\nHardFault \n"); + printf("mepc %x \n",__get_MEPC()); + printf("mcause %x \n",__get_MCAUSE()); + while (1) + { + } +} + + diff --git a/EVT/EXAM/IWDG/User/ch32v20x_it.h b/EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_it.h similarity index 100% rename from EVT/EXAM/IWDG/User/ch32v20x_it.h rename to EVT/EXAM/APPLICATION/WS2812_LED/User/ch32v20x_it.h diff --git a/EVT/EXAM/APPLICATION/WS2812_LED/User/main.c b/EVT/EXAM/APPLICATION/WS2812_LED/User/main.c new file mode 100644 index 0000000..6bcefe9 --- /dev/null +++ b/EVT/EXAM/APPLICATION/WS2812_LED/User/main.c @@ -0,0 +1,527 @@ +/********************************** (C) COPYRIGHT ******************************* + * File Name : main.c + * Author : WCH + * Version : V1.0.0 + * Date : 2021/06/06 + * Description : Main program body. + ********************************************************************************* + * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. + * Attention: This software (modified or not) and binary are used for + * microcontroller manufactured by Nanjing Qinheng Microelectronics. + *******************************************************************************/ + +/* + *@Note + * This example demonstrates control w2812 by using SPI or PWM. + * + * In spi mode + * PA7 - w2812 + * In PWM mode + * PA8 - w2812 + */ + +#include "debug.h" + +/* Global define */ + +#define LED_SPI_MODE 1 +#define LED_PWM_MODE 2 + +#define LED_MODE LED_SPI_MODE +//#define LED_MODE LED_PWM_MODE + +#define Pixel_NUM (8) + +#define LIST_SIZE(list) (sizeof(list)/sizeof(list[0])) +#define hex2rgb(c) (((c)>>16)&0xff),(((c)>>8)&0xff),((c)&0xff) + +/* Global Variable */ + +#if LED_MODE == LED_SPI_MODE + +/** spi mode + * When you using spi mode, you should make sure your spi work at 6M clock + */ + +#define Pixel_PRE_LEN (12u) +#define Pixel_RESET_LEN (25u) +#define COLOR_BUFFER_LEN (((Pixel_NUM)*Pixel_PRE_LEN)+Pixel_RESET_LEN) +#define SPI1_DMA_TX_CH DMA1_Channel3 + + +uint8_t color_buf[COLOR_BUFFER_LEN] = {0}; + + +/********************************************************************* + * @fn convToBit + * + * @brief Convert hex to spi bit + * + * @param res - the result + * input - input data + * + * @return none + */ +void convToBit(uint8_t *res, uint8_t input) +{ + uint8_t mask = 0x80; + for (int i = 0; i < 4; i++) { + uint8_t result = (input & mask) ? 0xE : 0x8; + result <<= 4; + mask >>= 1; + result |= (input & mask) ? 0xE : 0x8; + mask >>= 1; + res[i] = result; + } +} + + +/********************************************************************* + * @fn colorToBit + * + * @brief Convert color to spi bit + * + * @param buf - the result + * r - red channel + * g - green channel + * b - blue channel + * + * @return none + */ +void colorToBit(uint8_t *buf, uint8_t r, uint8_t g, uint8_t b) +{ + uint8_t *res = buf; + convToBit(res, g); + convToBit(&(res[4]), r); + convToBit(&(res[8]), b); +} + + +/********************************************************************* + * @fn setPixelColor + * + * @brief Set the pixel color of an LED + * + * @param index - index of LED + * r - red channel + * g - green channel + * b - blue channel + * + * + * @return none + */ +void setPixelColor(uint16_t index, uint8_t r, uint8_t g, uint8_t b) +{ + uint8_t *buf = &(color_buf[index * Pixel_PRE_LEN]); + colorToBit(buf,r,g,b); +} + +/********************************************************************* + * @fn SPI_1Lines_HalfDuplex_Init + * + * @brief Configuring the SPI for half-duplex communication. + * + * @return none + */ +void SPI_1Lines_HalfDuplex_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure= {0}; + SPI_InitTypeDef SPI_InitStructure= {0}; + + RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE ); + + + // the clock output +// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; +// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; +// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; +// GPIO_Init( GPIOA, &GPIO_InitStructure ); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init( GPIOA, &GPIO_InitStructure ); + + SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_InitStructure.SPI_CRCPolynomial = 7; + SPI_Init( SPI1, &SPI_InitStructure ); + + SPI_Cmd( SPI1, ENABLE ); +} + +/********************************************************************* + * @fn SPI1_DMA_Init + * + * @brief Initialize DMA for SPI2 + * + * @return none + */ +void SPI1_DMA_Init() +{ + DMA_InitTypeDef DMA_InitStructure = {0}; + + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + + DMA_DeInit(SPI1_DMA_TX_CH); + + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DATAR; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)color_buf; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; + DMA_InitStructure.DMA_BufferSize = COLOR_BUFFER_LEN; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + + DMA_Init(SPI1_DMA_TX_CH, &DMA_InitStructure); + + SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE); +} +#elif LED_MODE == LED_PWM_MODE + +void DMA1_Channel5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); + +#define CODE_0 (3) +#define CODE_1 (7) +#define RESET_LEN (60) +#define TIM_DMA_CH1_CH DMA1_Channel5 +#define COLOR_BUFFER_LEN (((Pixel_NUM)*(3*8))+RESET_LEN) + +uint16_t color_buf[COLOR_BUFFER_LEN] = {0}; + +/********************************************************************* + * @fn TIM1_Init + * + * @brief Initialize TIM1 + * + * @return none + */ +void TIM1_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure = {0}; + TIM_OCInitTypeDef TIM_OCInitStructure = {0}; + TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0}; + + RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init( GPIOA, &GPIO_InitStructure); + + TIM_TimeBaseInitStructure.TIM_Period = 10 - 1; + TIM_TimeBaseInitStructure.TIM_Prescaler = SystemCoreClock / 8000000 - 1; + TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; + TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure); + + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_Pulse = 0; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + TIM_OC1Init( TIM1, &TIM_OCInitStructure); + + TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Enable); + TIM_ARRPreloadConfig( TIM1, ENABLE); + TIM_CtrlPWMOutputs(TIM1, ENABLE); + TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); + TIM_Cmd( TIM1, ENABLE); +} + +/********************************************************************* + * @fn DMA1_Init + * + * @brief Initialize DMA for TIM1 ch1 + * + * @return none + */ +void DMA1_Init(void) +{ + DMA_InitTypeDef DMA_InitStructure = {0}; + NVIC_InitTypeDef NVIC_InitStructure = {0}; + + RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE); + + DMA_DeInit( TIM_DMA_CH1_CH); + DMA_Cmd( TIM_DMA_CH1_CH, DISABLE); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &TIM1->CH1CVR; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) color_buf; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; + DMA_InitStructure.DMA_BufferSize = COLOR_BUFFER_LEN; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_Init( TIM_DMA_CH1_CH, &DMA_InitStructure); + + DMA_Cmd( TIM_DMA_CH1_CH, DISABLE); + + DMA_ITConfig( DMA1_Channel5, DMA_IT_TC, ENABLE); + + NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + +} + +/********************************************************************* + * @fn setPixelColor + * + * @brief Set the pixel color of an LED + * + * @param id - index of LED + * r - red channel + * g - green channel + * b - blue channel + * + * + * @return none + */ +void setPixelColor(uint16_t id, uint8_t r, uint8_t g, uint8_t b) +{ + int i = 0, j = id * 24u; + if (id >= Pixel_NUM) { + return; + } + + for (i = 0; i < 8; i++) { + if (g & (0x80 >> i)) { + color_buf[j] = CODE_1; + } else { + color_buf[j] = CODE_0; + } + j++; + } + for (i = 0; i < 8; i++) { + if (r & (0x80 >> i)) { + color_buf[j] = CODE_1; + } else { + color_buf[j] = CODE_0; + } + j++; + } + for (i = 0; i < 8; i++) { + if (b & (0x80 >> i)) { + color_buf[j] = CODE_1; + } else { + color_buf[j] = CODE_0; + } + j++; + } +} +#endif + +/********************************************************************* + * @fn w2812_sync + * + * @brief Write data to LEDs + * + * @return none + */ +void w2812_sync() +{ +#if LED_MODE == LED_SPI_MODE + while(SPI1_DMA_TX_CH->CNTR!=0); + DMA_ClearFlag(DMA1_FLAG_TC3); + DMA_Cmd(SPI1_DMA_TX_CH, DISABLE); + SPI1_DMA_TX_CH->CNTR = COLOR_BUFFER_LEN; + DMA_Cmd(SPI1_DMA_TX_CH, ENABLE); +#elif LED_MODE == LED_PWM_MODE + while(DMA_GetCurrDataCounter(TIM_DMA_CH1_CH)!=0); + DMA_SetCurrDataCounter( TIM_DMA_CH1_CH, COLOR_BUFFER_LEN); + DMA_Cmd( TIM_DMA_CH1_CH, ENABLE); + TIM_Cmd( TIM1, ENABLE); +#endif +} + +#define MAX_STEP (200) +/********************************************************************* + * @fn interpolateColors + * + * @brief Set the pixel color of an LED + * + * @param color1 - The color at the beginning + * color2 - The color at the ending + * step - + * + * + * @return color + */ +uint32_t interpolateColors(uint32_t color1, uint32_t color2, uint8_t step) +{ + uint8_t r1 = (color1 >> 16) & 0xFF; + uint8_t g1 = (color1 >> 8) & 0xFF; + uint8_t b1 = color1 & 0xFF; + + uint8_t r2 = (color2 >> 16) & 0xFF; + uint8_t g2 = (color2 >> 8) & 0xFF; + uint8_t b2 = color2 & 0xFF; + + float tmp1 = (r2 - r1) * (step / (float) MAX_STEP); + float tmp2 = (g2 - g1) * (step / (float) MAX_STEP); + float tmp3 = (b2 - b1) * (step / (float) MAX_STEP); + + uint8_t r = (uint8_t) (r1 + tmp1); + uint8_t g = (uint8_t) (g1 + tmp2); + uint8_t b = (uint8_t) (b1 + tmp3); + + return ((uint32_t) r << 16) | ((uint32_t) g << 8) | b; +} + +uint32_t color_list[] = { + 0xff0000, + 0x00ff00, + 0x0000ff, + 0xffff00, + 0xff00ff, + 0x00ffff, + 0xffffff, +// 0x000000, + }; + +/********************************************************************* + * @fn led_example_0 + * + * @brief In this example, all leds will gradually change from one color to another + * + * @return none + */ +void led_example_0(void) +{ + uint32_t i = 0, j = 0; + uint32_t c = 0x0f0f00; + while(1) { + c = color_list[i]; + uint32_t next_color = color_list[i+1]; + i++; + if((i+1)>=LIST_SIZE(color_list)) { + i=0; + } + for(j = 0; j=LIST_SIZE(color_list)) { + i=0; + } + for (int var = 0; var < Pixel_NUM; ++var) { + setPixelColor(var,hex2rgb(c)); + } + w2812_sync(); + Delay_Ms(100); + + } +} + +/********************************************************************* + * @fn led_example_2 + * + * @brief In this example, the leds will blink one by one + * + * @return none + */ +void led_example_2(void) +{ + uint32_t i = 0; + uint32_t c = 0x0f0f00; + while(1) { + c = color_list[i]; + printf("color[%d] %08x %08x \n",i,c,color_list[i]); + i++; + if(i>=LIST_SIZE(color_list)) { + i=0; + } + + for (int var = 0; var < Pixel_NUM; var+=1) { + setPixelColor(var, hex2rgb(c)); + w2812_sync(); + Delay_Ms(100); + setPixelColor(var,0,0,0); + w2812_sync(); + } + } +} +/********************************************************************* + * @fn main + * + * @brief Main program. + * + * @return none + */ +int main(void) +{ + uint16_t i = 0; + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); + Delay_Init(); + USART_Printf_Init(115200); + printf("SystemClk:%d\r\n", SystemCoreClock); + + // Turn off all LEDs + for (i = 0; i < Pixel_NUM; i++) { + setPixelColor(i, 0, 0, 0); + } +#if LED_MODE == LED_SPI_MODE + SPI_1Lines_HalfDuplex_Init(); + SPI1_DMA_Init(); + DMA_Cmd(SPI1_DMA_TX_CH, ENABLE); +#elif LED_MODE == LED_PWM_MODE + TIM1_Init(); + DMA1_Init(); + DMA_Cmd( TIM_DMA_CH1_CH, ENABLE); +#endif + + led_example_0(); +// led_example_2(); + +} + +#if LED_MODE == LED_PWM_MODE +void DMA1_Channel5_IRQHandler(void) +{ + if (DMA_GetFlagStatus( DMA1_FLAG_TC5)) { + TIM_Cmd( TIM1, DISABLE); + DMA_Cmd( TIM_DMA_CH1_CH, DISABLE); + DMA_ClearFlag( DMA1_FLAG_TC5); + } +} +#endif diff --git a/EVT/EXAM/IWDG/User/system_ch32v20x.c b/EVT/EXAM/APPLICATION/WS2812_LED/User/system_ch32v20x.c similarity index 99% rename from EVT/EXAM/IWDG/User/system_ch32v20x.c rename to EVT/EXAM/APPLICATION/WS2812_LED/User/system_ch32v20x.c index 37730a5..8d3a568 100644 --- a/EVT/EXAM/IWDG/User/system_ch32v20x.c +++ b/EVT/EXAM/APPLICATION/WS2812_LED/User/system_ch32v20x.c @@ -205,6 +205,7 @@ void SystemCoreClockUpdate (void) */ static void SetSysClock(void) { + //GPIO_IPD_Unused(); #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_48MHz_HSE diff --git a/EVT/EXAM/IWDG/User/system_ch32v20x.h b/EVT/EXAM/APPLICATION/WS2812_LED/User/system_ch32v20x.h similarity index 100% rename from EVT/EXAM/IWDG/User/system_ch32v20x.h rename to EVT/EXAM/APPLICATION/WS2812_LED/User/system_ch32v20x.h diff --git a/EVT/EXAM/IWDG/IWDG.wvproj b/EVT/EXAM/APPLICATION/WS2812_LED/WS2812_LED.wvproj similarity index 100% rename from EVT/EXAM/IWDG/IWDG.wvproj rename to EVT/EXAM/APPLICATION/WS2812_LED/WS2812_LED.wvproj diff --git a/EVT/EXAM/OPA/.cproject b/EVT/EXAM/BKP/BKP/.cproject similarity index 97% rename from EVT/EXAM/OPA/.cproject rename to EVT/EXAM/BKP/BKP/.cproject index 7e5eb99..93c33ac 100644 --- a/EVT/EXAM/OPA/.cproject +++ b/EVT/EXAM/BKP/BKP/.cproject @@ -45,11 +45,11 @@