Skip to content

Commit

Permalink
Add feature Flash Memory (#252)
Browse files Browse the repository at this point in the history
* Add feature Flash Memory

Feature:
- Add feature Flash Memory

API Updates:
- Add flash memory related API

Misc:
- Update keywords.txt files
- Update library.properties files
  • Loading branch information
M-ichae-l authored Jul 9, 2024
1 parent fdaa9f1 commit d9022ef
Show file tree
Hide file tree
Showing 30 changed files with 454 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Arduino_package/hardware/libraries/Analog/library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name=AmebaAnalog
version=1.0.0
author=Realtek SG
maintainer=Realtek SG
sentence=With this library you can find useful analog application.
sentence=Analog application usage
paragraph=
category=Signal Input/Output
category=Signal IO
url=
architectures=AmebaPro2
4 changes: 0 additions & 4 deletions Arduino_package/hardware/libraries/BLE/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,3 @@ begin KEYWORD2
end KEYWORD2
addService KEYWORD2
advData KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
6 changes: 3 additions & 3 deletions Arduino_package/hardware/libraries/BLE/library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=AmebaBLE
version=1.0.0
author=Realtek
maintainer=Realtek
sentence=Enables communication with devices that use Bluetooth.
author=Realtek SG
maintainer=Realtek SG
sentence=Enables communication with devices that use Bluetooth
paragraph=
category=Communication
url=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=AmebaDebugExample
name=AmebaDebug
version=1.0.0
author=Realtek
maintainer=Realtek
sentence=Debugging example for RTL8735B.
author=Realtek SG
maintainer=Realtek SG
sentence=Debugging mode
paragraph=
category=Debugging
url=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=AmebaFileSystem
version=1.0.0
author=Realtek
maintainer=Realtek <[email protected]>
sentence=Access FAT file systems located on SD cards connected to the SDIO bus.
author=Realtek SG
maintainer=Realtek SG
sentence=Access FAT file systems located on SD cards connected to the SDIO bus
paragraph=
category=
category=External Storage
url=
architectures=AmebaPro2
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This sketch shows how to request flash memory read/write stream word.
Example guide: TBD
*/

#include <FlashMemory.h>

#define TEST_SIZE 0x1000
#define RESET_THRESHOLD 5

void setup()
{
Serial.begin(115200);

FlashMemory.begin(FLASH_MEMORY_APP_BASE, TEST_SIZE);
FlashMemory.read();

int i;
if (FlashMemory.buf[0] >= RESET_THRESHOLD) {
for (i = 0; i < TEST_SIZE; i++) {
FlashMemory.buf[i] = 0x00;
}
FlashMemory.write();
Serial.print("Reset count to 0");
} else {
for (i = 0; i < TEST_SIZE; i++) {
FlashMemory.buf[i]++;
}
FlashMemory.write();
Serial.print("Boot count: ");

for (i = 0; i < (TEST_SIZE - 1); i++) {
if (FlashMemory.buf[i] != FlashMemory.buf[i + 1]) {
Serial.print("error at ");
Serial.print(i);
Serial.print(" value ");
break;
}
}
Serial.println(FlashMemory.buf[i]);
}
}

void loop()
{
delay(1000);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This sketch shows how to request flash memory read/write one specific word.
Example guide: TBD
*/

#include <FlashMemory.h>

void setup()
{
Serial.begin(115200);

unsigned int value;
unsigned int check_value;

/* request flash size 0x1000 from FLASH_MEMORY_APP_BASE */
FlashMemory.begin(FLASH_MEMORY_APP_BASE, 0x1000);

/* read one word (32-bit) from FLASH_MEMORY_APP_BASE plus offset 0x1E00 */
value = FlashMemory.readWord(0x1E00);
Serial.println();
Serial.print("Read value is ");
Serial.println(value, HEX);

if (value == 0xFFFFFFFF) {
value = 0;
} else {
value++;
}

/* write one word (32-bit) to FLASH_MEMORY_APP_BASE plus offset 0x1F00 */
Serial.print("Write value is ");
Serial.println(value, HEX);
FlashMemory.writeWord(0x1E00, value);

check_value = FlashMemory.readWord(0x1E00);
if (check_value == value) {
Serial.println("Success! Read and Write flash memory by word.");
} else {
Serial.println("Error! Read and Write flash memory by word.");
}
}

void loop()
{
delay(1000);
}
29 changes: 29 additions & 0 deletions Arduino_package/hardware/libraries/FlashMemory/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#######################################
# Syntax Coloring Map for FlashMemory
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

FlashMemory KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2
end KEYWORD2
read KEYWORD2
write KEYWORD2
readWord KEYWORD2
writeWord KEYWORD2
eraseSector KEYWORD2
eraseWord KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

buf_size LITERAL1
buf LITERAL1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=AmebaFlashMemory
version=1.0.0
author=Realtek SG
maintainer=Realtek SG
sentence=Flash memory lib that allows to eidt flash data
paragraph=
category=Storage
url=
architectures=AmebaPro2
162 changes: 162 additions & 0 deletions Arduino_package/hardware/libraries/FlashMemory/src/FlashMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "FlashMemory.h"

flash_t flash_obj;

FlashMemoryClass::FlashMemoryClass()
{
_flash_base_address = FLASH_MEMORY_APP_BASE;
buf_size = MAX_FLASH_MEMORY_APP_SIZE;

buf = (unsigned char *)(malloc(MAX_FLASH_MEMORY_APP_SIZE));
_pFlash = &flash_obj;
}

FlashMemoryClass::~FlashMemoryClass()
{
if (buf != NULL) {
free(buf);
buf = NULL;
}
}

void FlashMemoryClass::begin(unsigned int flash_base_address, unsigned int flash_buf_size)
{
if (buf != NULL) {
free(buf);
buf = NULL;
}

if ((FLASH_MEMORY_APP_BASE <= flash_base_address) && (flash_base_address <= FLASH_MEMORY_SIZE)) {
_flash_base_address = flash_base_address;
} else {
_flash_base_address = FLASH_MEMORY_APP_BASE;
}

if (flash_buf_size > MAX_FLASH_MEMORY_APP_SIZE) {
buf_size = MAX_FLASH_MEMORY_APP_SIZE;
} else {
buf_size = flash_buf_size;
}
buf = (unsigned char *)(malloc(buf_size));
}

void FlashMemoryClass::end()
{
if (buf != NULL) {
free(buf);
buf = NULL;
}
}

void FlashMemoryClass::read(unsigned int offset)
{
if ((_flash_base_address + offset) < FLASH_MEMORY_APP_BASE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
} else if ((_flash_base_address + offset + buf_size) > FLASH_MEMORY_SIZE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
}

flash_stream_read(_pFlash, (_flash_base_address + offset), buf_size, (uint8_t *)buf);
}

void FlashMemoryClass::write(unsigned int offset)
{
if ((_flash_base_address + offset) < FLASH_MEMORY_APP_BASE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
} else if ((_flash_base_address + offset + buf_size) > FLASH_MEMORY_SIZE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
}

for (int i = 0; i < (MAX_FLASH_MEMORY_APP_SIZE / FLASH_SECTOR_SIZE); i++) {
flash_erase_sector(_pFlash, (_flash_base_address + (i * FLASH_SECTOR_SIZE)));
}

flash_stream_write(_pFlash, (_flash_base_address + offset), buf_size, (uint8_t *)buf);
}

unsigned int FlashMemoryClass::readWord(unsigned int offset)
{
if ((_flash_base_address + offset) < FLASH_MEMORY_APP_BASE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return 0;
} else if ((_flash_base_address + offset) > (FLASH_MEMORY_SIZE - 4)) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return 0;
}

unsigned int value;

flash_read_word(_pFlash, _flash_base_address + offset, (uint32_t *)&value);

return value;
}

void FlashMemoryClass::writeWord(unsigned int offset, unsigned int data)
{
if ((_flash_base_address + offset) < FLASH_MEMORY_APP_BASE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
} else if ((_flash_base_address + offset) > (FLASH_MEMORY_SIZE - 4)) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
}

unsigned int check_val;
unsigned char *tmp_buf;
unsigned int *tmp_val;
unsigned int tmp_sector_adr;

flash_write_word(_pFlash, _flash_base_address + offset, data);
flash_read_word(_pFlash, _flash_base_address + offset, (uint32_t *)&check_val);

if (check_val != data) {
tmp_sector_adr = ((_flash_base_address + offset) / FLASH_SECTOR_SIZE) * FLASH_SECTOR_SIZE;
tmp_buf = (unsigned char *)(malloc(FLASH_SECTOR_SIZE));
flash_stream_read(_pFlash, tmp_sector_adr, FLASH_SECTOR_SIZE, tmp_buf);
flash_erase_sector(_pFlash, tmp_sector_adr);
tmp_val = (unsigned int *)(tmp_buf + _flash_base_address + offset - tmp_sector_adr);
*tmp_val = data;
flash_stream_write(_pFlash, tmp_sector_adr, FLASH_SECTOR_SIZE, tmp_buf);
free(tmp_buf);
}
}

void FlashMemoryClass::eraseSector(unsigned int sector_offset)
{
if ((sector_offset % FLASH_SECTOR_SIZE) != 0) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
}

flash_erase_sector(_pFlash, (_flash_base_address + sector_offset));
}

void FlashMemoryClass::eraseWord(unsigned int offset)
{
if ((_flash_base_address + offset) < FLASH_MEMORY_APP_BASE) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
} else if ((_flash_base_address + offset) > (FLASH_MEMORY_SIZE - 4)) {
printf("\r\n[ERROR] %s. Invalid offset \n", __FUNCTION__);
return;
}

unsigned char *tmp_buf;
unsigned int *tmp_val;
unsigned int tmp_sector_adr;

tmp_sector_adr = ((_flash_base_address + offset) / FLASH_SECTOR_SIZE) * FLASH_SECTOR_SIZE;
tmp_buf = (unsigned char *)(malloc(FLASH_SECTOR_SIZE));
flash_stream_read(_pFlash, tmp_sector_adr, FLASH_SECTOR_SIZE, tmp_buf);
flash_erase_sector(_pFlash, tmp_sector_adr);
tmp_val = (unsigned int *)(tmp_buf + _flash_base_address + offset - tmp_sector_adr);
*tmp_val = 0;
flash_stream_write(_pFlash, tmp_sector_adr, FLASH_SECTOR_SIZE, tmp_buf);
free(tmp_buf);
}

FlashMemoryClass FlashMemory = FlashMemoryClass();
Loading

0 comments on commit d9022ef

Please sign in to comment.