|
| 1 | +/* |
| 2 | + Flash read and write demo for ch5xx |
| 3 | +
|
| 4 | + The flash on ch5xx is accessed through some funny flash controller, |
| 5 | + a library to talk to that is implemented in extralibs/ch5xx_flash.h |
| 6 | + Some functions in that library, and the user function must be run |
| 7 | + from RAM otherwise the chip locks up. |
| 8 | + By default these are automatically and always loaded into RAM which |
| 9 | + takes about 480 bytes of it, but it's possible to load them on demand |
| 10 | + into a special RAM section by using FUNCONF_CH5XXFLASHLIB_SECTION. |
| 11 | + The top comment in extralibs/ch5xx_flash.h explains how to do that. |
| 12 | +
|
| 13 | + IMPORTANT NOTE ON ERASE: |
| 14 | + Flash erase is done in Sectors of 4kB, when you want to write something |
| 15 | + to flash you first need to erase the sector it is on. |
| 16 | + The ch5xx_flash_cmd_erase(addr, len) function takes care of aligning |
| 17 | + the start and end addresses to sector boundaries, so it will erase |
| 18 | + more than just [addr, addr+len]! |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include "ch32fun.h" |
| 23 | +#include "ch5xx_flash.h" |
| 24 | + |
| 25 | +#define SECTOR_SIZE 4096 // 4kB |
| 26 | +#define SOMETHING_NICE "ch32fun" |
| 27 | + |
| 28 | +uint8_t buf_compare[] = SOMETHING_NICE; |
| 29 | +uint8_t buf[] = SOMETHING_NICE; |
| 30 | +int check_buffers() { |
| 31 | + int result = 1; |
| 32 | + for(int i = 0; i < sizeof(buf); i++) { |
| 33 | + printf("%02x ", buf[i]); |
| 34 | + if(buf[i] != buf_compare[i]) { |
| 35 | + result = 0; |
| 36 | + } |
| 37 | + } |
| 38 | + printf("\n"); |
| 39 | + return result; |
| 40 | +} |
| 41 | + |
| 42 | +__HIGH_CODE |
| 43 | +void flashtest() { |
| 44 | + // init and empty buffer |
| 45 | + for(int i = 0; i < sizeof(buf); i++) { |
| 46 | + buf[i] = 0; |
| 47 | + } |
| 48 | + |
| 49 | + // read start of third sector |
| 50 | + uint32_t addr = 2*SECTOR_SIZE; |
| 51 | + uint32_t len = sizeof(buf); |
| 52 | + ch5xx_flash_cmd_read(addr, buf, len); |
| 53 | + int check1 = check_buffers(); |
| 54 | + |
| 55 | + // erase third sector |
| 56 | + ch5xx_flash_cmd_erase(addr, len); |
| 57 | + ch5xx_flash_cmd_read(addr, buf, len); |
| 58 | + int check2 = check_buffers(); |
| 59 | + |
| 60 | + // write something nice to the start of the third sector and verify the write |
| 61 | + ch5xx_flash_cmd_write(addr, buf_compare, sizeof(buf)); |
| 62 | + int verify = ch5xx_flash_cmd_verify(addr, buf_compare, sizeof(buf)); |
| 63 | + |
| 64 | + // read it back |
| 65 | + ch5xx_flash_cmd_read(addr, buf, len); |
| 66 | + int check3 = check_buffers(); |
| 67 | + |
| 68 | + printf("flashtest: the first check will be false only on first run, second is always false and third is always true\n"); |
| 69 | + printf("checks: chk1:%d chk2:%d verify(%d):%d chk3:%d\n", check1, check2, sizeof(buf), verify, check3); |
| 70 | +} |
| 71 | + |
| 72 | +int main() |
| 73 | +{ |
| 74 | + SystemInit(); |
| 75 | + |
| 76 | + flashtest(); // must run from RAM |
| 77 | + |
| 78 | + while(1); |
| 79 | +} |
0 commit comments