Skip to content

Commit a03e37e

Browse files
authored
Merge pull request cnlohr#838 from biemster/ch5xx-flashlib
CH5xx: add flash library and flashtest demo
2 parents f9d2d66 + 987b938 commit a03e37e

File tree

5 files changed

+412
-4
lines changed

5 files changed

+412
-4
lines changed

ch32fun/ch5xxhw.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,7 @@ typedef enum
810810
#define RB_ROM_CODE_OFS 0x10 // RWA, code offset address selection in Flash ROM: 0=start address 0x000000, 1=start address 0x040000
811811
#define RB_ROM_CTRL_EN 0x20 // RWA, enable flash ROM control interface enable: 0=disable access, 1=enable access control register
812812
#define RB_ROM_DATA_WE 0x40 // RWA, enable flash ROM data & code area being erase/write: 0=all writing protect, 1=enable data area program and erase
813-
#ifdef CH570_CH572
814813
#define RB_ROM_CODE_WE 0xC0 // RWA, enable flash ROM code area being erase/write: 0=code writing protect, 1=enable code area program and erase
815-
#else
816-
#define RB_ROM_CODE_WE 0x80 // RWA, enable flash ROM code area being erase/write: 0=code writing protect, 1=enable code area program and erase
817-
#endif
818814
#define R8_GLOB_CFG_INFO (*((vu8*)0x40001045)) // RO, global configuration information and status
819815
#define RB_CFG_ROM_READ 0x01 // RO, indicate protected status of Flash ROM code and data: 0=reading protect, 1=enable read by external programmer
820816
#define RB_CFG_RESET_EN 0x04 // RO, manual reset input enable status

examples_ch5xx/flashtest/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all : flash
2+
3+
TARGET:=flashtest
4+
TARGET_MCU:=CH570
5+
TARGET_MCU_PACKAGE:=CH570D
6+
7+
include ../../ch32fun/ch32fun.mk
8+
9+
flash : cv_flash
10+
clean : cv_clean
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _FUNCONFIG_H
2+
#define _FUNCONFIG_H
3+
4+
#define FUNCONF_USE_HSI 0 // CH592 does not have HSI
5+
#define FUNCONF_USE_HSE 1
6+
#define CLK_SOURCE_CH5XX CLK_SOURCE_PLL_60MHz // default so not really needed
7+
#define FUNCONF_SYSTEM_CORE_CLOCK 60 * 1000 * 1000 // keep in line with CLK_SOURCE_CH59X
8+
9+
#define FUNCONF_DEBUG_HARDFAULT 1
10+
#define FUNCONF_USE_CLK_SEC 0
11+
12+
#endif

0 commit comments

Comments
 (0)