CH32V003: Errors in ch32v00x.h - Page 1
Forum section: electronics / general

author: rhodges
author_rank: Frequent Contributor
timestamp: October 12, 2023, 11:56:11 pm
content: As I noted in a recent thread, I am starting to work with the CH32V003 FLASH memory. The reference manual (V1.5) still has some typos, but I used the defines in ch32v00x.h as the correct usage. One notable detail: FLASH_CTLR_FLOCK is missing.And now I get to some real concerns. Here are the defines from ch32v00x.h for FLASH->CTLR:Code:#define FLASH_CTLR_PG ((uint16_t)0x0001) /* Programming */#define FLASH_CTLR_PER ((uint16_t)0x0002) /* Page Erase 1KByte*/#define FLASH_CTLR_MER ((uint16_t)0x0004) /* Mass Erase */#define FLASH_CTLR_OPTPG ((uint16_t)0x0010) /* Option Byte Programming */#define FLASH_CTLR_OPTER ((uint16_t)0x0020) /* Option Byte Erase */#define FLASH_CTLR_STRT ((uint16_t)0x0040) /* Start */#define FLASH_CTLR_LOCK ((uint16_t)0x0080) /* Lock */#define FLASH_CTLR_OPTWRE ((uint16_t)0x0200) /* Option Bytes Write Enable */#define FLASH_CTLR_ERRIE ((uint16_t)0x0400) /* Error Interrupt Enable */#define FLASH_CTLR_EOPIE ((uint16_t)0x1000) /* End of operation interrupt enable */#define FLASH_CTLR_PAGE_PG ((uint16_t)0x00010000) /* Page Programming 64Byte */#define FLASH_CTLR_PAGE_ER ((uint16_t)0x00020000) /* Page Erase 64Byte */#define FLASH_CTLR_BUF_LOAD ((uint16_t)0x00040000) /* Buffer Load */#define FLASH_CTLR_BUF_RST ((uint16_t)0x00080000) /* Buffer Reset */My first thought was, WTF?! Can this actually work? (For CTLR_PAGE_PG and higher.) I looked at the assembly. Then I added a minimal test function:Code:void asm_test(void){FLASH->CTLR = FLASH_CTLR_PAGE_ER;}Yes, the code produced was as I expected:Code:00000000 <asm_test>:0: 400227b7 lui a5,0x400224: 0007a823 sw zero,16(a5) # 40022010 <.LASF30+0x40021dd4>8: 8082 retAfter removing the offensive 16 bit cast, I get the reasonable:Code:00000000 <asm_test>:0: 400227b7 lui a5,0x400224: 00020737 lui a4,0x208: cb98 sw a4,16(a5)a: 8082 retHeads up, everyone!

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 13, 2023, 12:09:45 am
content: Yeah, good catch. Docs and support files for this chip look a bit lackluster!

author: Shonky
author_rank: Frequent Contributor
timestamp: October 13, 2023, 02:26:24 am
content: Shouldn't that beCode:4: 00002737 lui a4,0x2?

author: rhodges
author_rank: Frequent Contributor
timestamp: October 13, 2023, 12:30:07 pm
content: LUI loads the high 20 bits and clears the bottom 12 bits. So if your instruction is "LUI a5, abcde", it will load abcde000 into a5.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: October 13, 2023, 12:52:43 pm
content: There are lots of mistakes and omissions I've found in ch32v00x.h. Best to always check against the register listings in the Reference Manual.Part of the problem I think is that the WCH library code often doesn't even use the main register defs, and instead defines things locally. So they haven't noticed things are wrong or missing.I was thinking of making a pull request to WCH's GitHub repository, but I've made so many changes it would be a pain to create a proper diff. Plus they don't seem at all attentive to GitHub anyway (lots of issues going unanswered).