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:
#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:
void asm_test(void)
{
FLASH->CTLR = FLASH_CTLR_PAGE_ER;
}
Yes, the code produced was as I expected:
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:
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!