Skip to content

Commit 3777661

Browse files
committed
add eeprom
1 parent b6c76f7 commit 3777661

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

ch55xduino/ch55x/cores/ch55xduino/Arduino.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,10 @@ char USBSerial_read();
300300
#define Serial0_println_fd(P,Q) ( Print_print_fd(Serial0_write,(P),(Q) ) + Print_println(Serial0_write) )
301301
#define Serial0_println_c(P) ( (Serial0_write(P)) + Print_println(Serial0_write) )
302302

303+
//10K lifecycle DataFlash access on CH551/CH552.
304+
#define eeprom_write_byte(ADDR,VAL) { DPL=(VAL);DPH=(ADDR);eeprom_write_byte_2_params_DPTR(); }
305+
//SDCC is not efficent to convert 2 8bit data to 1 16bit data, se we use DPTR directly. The mismatch of parameter of the H and C is intentional
306+
void eeprom_write_byte_2_params_DPTR();
307+
uint8_t eeprom_read_byte (uint8_t addr);
303308

304309
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdint.h>
2+
#include "include/ch554.h"
3+
#include "include/ch554_usb.h"
4+
5+
void eeprom_write_byte_2_params_DPTR (uint16_t addr_val){
6+
//using a single parameter of 16bit number utilize both DPL and DPH, avoid using memory to pass parameters
7+
#define ADDR_PARAM ((addr_val>>8)&0xff)
8+
#define VAL_PARAM ((addr_val>>0)&0xff)
9+
10+
if (ADDR_PARAM>=128){
11+
return;
12+
}
13+
14+
SAFE_MOD = 0x55;
15+
SAFE_MOD = 0xAA; //Enter Safe mode
16+
GLOBAL_CFG |= bDATA_WE; //Enable DataFlash write
17+
SAFE_MOD = 0; //Exit Safe mode
18+
ROM_ADDR_H = DATA_FLASH_ADDR >> 8;
19+
ROM_ADDR_L = ADDR_PARAM<<1;
20+
ROM_DATA_L = VAL_PARAM;
21+
if ( ROM_STATUS & bROM_ADDR_OK ) { // Valid access Address
22+
ROM_CTRL = ROM_CMD_WRITE; // Write
23+
}
24+
SAFE_MOD = 0x55;
25+
SAFE_MOD = 0xAA; //Enter Safe mode
26+
GLOBAL_CFG &= ~bDATA_WE; //Disable DataFlash write
27+
SAFE_MOD = 0; //Exit Safe mode
28+
}
29+
30+
uint8_t eeprom_read_byte (uint8_t addr){
31+
ROM_ADDR_H = DATA_FLASH_ADDR >> 8;
32+
ROM_ADDR_L = addr<<1; //Addr must be even
33+
ROM_CTRL = ROM_CMD_READ;
34+
return ROM_DATA_L;
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
EEPROM_cycle_write
3+
4+
A simple example write eeprom content one by one.
5+
It will continue after power cycle.
6+
7+
created 2020
8+
by Deqing Sun for use with CH55xduino
9+
10+
This example code is in the public domain.
11+
12+
*/
13+
14+
uint8_t writePtr = 0;
15+
uint8_t writeData = 0;
16+
17+
void dumpEEPROM() {
18+
USBSerial_println_s("DataFalsh Dump:");
19+
for (uint8_t i = 0; i < 128; i++) {
20+
uint8_t eepromData = eeprom_read_byte(i);
21+
if (eepromData < 0x10) USBSerial_print_c('0');
22+
USBSerial_print_ub(eepromData, HEX);
23+
USBSerial_print_c(',');
24+
if ((i & 15) == 15) USBSerial_println();
25+
}
26+
USBSerial_flush();
27+
}
28+
29+
void setup() {
30+
for (writePtr = 0; writePtr < 128; writePtr++) {
31+
uint8_t eepromData = eeprom_read_byte(writePtr);
32+
if (writePtr != eepromData) break;
33+
}
34+
writeData = writePtr;
35+
if (writePtr >= 128) writePtr = 0;
36+
}
37+
38+
void loop() {
39+
delay(5000);
40+
USBSerial_print_s("Write ");
41+
USBSerial_print_ub(writeData, HEX);
42+
USBSerial_print_s(" to addr: ");
43+
USBSerial_println_ub(writePtr, HEX);
44+
eeprom_write_byte(writePtr, writeData);
45+
writeData++;
46+
writePtr++;
47+
if (writePtr >= 128) writePtr = 0;
48+
dumpEEPROM();
49+
}

0 commit comments

Comments
 (0)