Community-PIO-CH32V / platform-ch32v

PlatformIO platform for CH32V RISC-V chips (CH32V003, CH32V103, CH32V20x, CH32V30x, CH32X035) and CH56x, CH57x, CH58x, CH59x
Apache License 2.0
234 stars 37 forks source link

Improve setting of configurable Flash + RAM sizes #20

Open maxgerhardt opened 1 year ago

maxgerhardt commented 1 year ago

Very sneakily, some CH32V20x and CH32V30x chips have the capability for different Flash+SRAM configurations.

These seem to be programmed into the option bytes (through the flash peripheral).

Original linker scripts.

/* CH32V30x_D8C - CH32V307VC-CH32V307WC-CH32V307RC
   CH32V30x_D8 - CH32V303VC-CH32V303RC
   FLASH + RAM supports the following configuration
   FLASH-192K + RAM-128K
   FLASH-224K + RAM-96K
   FLASH-256K + RAM-64K  
   FLASH-288K + RAM-32K  
*/
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 288K
    RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
}
/* CH32V20x_D8 - CH32V203RB
   CH32V20x_D8W - CH32V208x
   FLASH + RAM supports the following configuration
   FLASH-128K + RAM-64K
   FLASH-144K + RAM-48K
   FLASH-160K + RAM-32K
*/
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 160K
    RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K

}

Currently we only hint at this in the platformio.ini for one chip

https://github.com/Community-PIO-CH32V/platform-ch32v/blob/f1eb50cee3b0a9a068fadc87f54f8c89e3cab310/examples/blinky-none-os/platformio.ini#L28-L43

If the chip was configured to have one configuration (e.g., "FLASH-288K + RAM-32K") but the platformio.ini configuration builds a firmware for the "FLASH-256K + RAM-64K" configuration, the chip will fail too boot properly, because the linker script and startup file will place the initial stack pointer (SP) at the end of RAM (so e.g. 0x200000 + 64K) which is not RAM in the current configuration, so at the first usage of the stack (like a jal instruction), the chip HardFault's.

We need a way for these chips to readout the current Flash+SRAM configuration and check them against the used build config, then either throw a really big warning or straightup fail the build or upload process.

It would also be good if we had a way to set a desired configuration and have that available as a PlatformIO task (together with removing read/write protection locks).

Currently I've been doing this via the official W.CH ISP Programmer tools.