Author Topic: CH32V003: Errors in ch32v00x.h  (Read 1102 times)

0 Members and 1 Guest are viewing this topic.

Offline rhodgesTopic starter

  • Frequent Contributor
  • **
  • Posts: 306
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
CH32V003: Errors in ch32v00x.h
« on: October 12, 2023, 11:56:11 pm »
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: [Select]
#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: [Select]
void asm_test(void)
{
    FLASH->CTLR = FLASH_CTLR_PAGE_ER;
}
Yes, the code produced was as I expected:
Code: [Select]
00000000 <asm_test>:
   0:   400227b7                lui     a5,0x40022
   4:   0007a823                sw      zero,16(a5) # 40022010 <.LASF30+0x40021dd4>
   8:   8082                    ret
After removing the offensive 16 bit cast, I get the reasonable:
Code: [Select]
00000000 <asm_test>:
   0:   400227b7                lui     a5,0x40022
   4:   00020737                lui     a4,0x20
   8:   cb98                    sw      a4,16(a5)
   a:   8082                    ret
Heads up, everyone!
Currently developing STM8 and STM32. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 
The following users thanked this post: paf

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14521
  • Country: fr
Re: CH32V003: Errors in ch32v00x.h
« Reply #1 on: October 13, 2023, 12:09:45 am »
Yeah, good catch. Docs and support files for this chip look a bit lackluster! ;D
 

Offline Shonky

  • Frequent Contributor
  • **
  • Posts: 290
  • Country: au
Re: CH32V003: Errors in ch32v00x.h
« Reply #2 on: October 13, 2023, 02:26:24 am »
Shouldn't that be
Code: [Select]
4:   00002737                lui     a4,0x2
?
 

Offline rhodgesTopic starter

  • Frequent Contributor
  • **
  • Posts: 306
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
Re: CH32V003: Errors in ch32v00x.h
« Reply #3 on: October 13, 2023, 12:30:07 pm »
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.
Currently developing STM8 and STM32. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 
The following users thanked this post: Shonky

Online HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1494
  • Country: gb
Re: CH32V003: Errors in ch32v00x.h
« Reply #4 on: October 13, 2023, 12:52:43 pm »
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).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf