Flash
In principle, flash memory is used as an area for storing programs. Unused sectors can also be used by programs.
There is no denying the possibility of destroying the entire program area due to power failure during the writing process. For applications that require frequent writing, please perform sufficient verification.

About Sectors

TWELITE BLUE provides 5 blocks (160KB) of 32KB sectors, while TWELITE RED provides 16 blocks (512KB).

Read out

This is a method to read from a user-defined tsFlash structure. In the example, CRC check and data check by MAGIC NUMBER are also performed.
1
#define FLASH_TYPE E_FL_CHIP_INTERNAL
2
#define FLASH_SECTOR_SIZE (32L* 1024L) // 32KB
3
#define FLASH_SECTOR_NUMBER 5 // 0..4 (for TWELITE BLUE)
4
5
/** @ingroup FLASH
6
* Read out from Flash
7
* @param psFlash data buffer for storing.
8
* @param sector readout sector
9
* @param offset Offset from the beginning of the sector
10
* @return TRUE: Read successfully FALSE: Failure
11
*/
12
bool_t bFlash_Read(tsFlash *psFlash, uint8 sector, uint32 offset) {
13
bool_t bRet = FALSE;
14
offset += (uint32)sector * FLASH_SECTOR_SIZE; // calculate the absolute address
15
16
if (bAHI_FlashInit(FLASH_TYPE, NULL) == TRUE) {
17
if (bAHI_FullFlashRead(offset, sizeof(tsFlash), (uint8 *)psFlash)) {
18
bRet = TRUE;
19
}
20
}
21
22
// validate content
23
if (bRet && psFlash->u32Magic != FLASH_MAGIC_NUMBER) {
24
bRet = FALSE;
25
}
26
if (bRet && psFlash->u8CRC != u8CCITT8((uint8*)&(psFlash->sData), sizeof(tsFlashApp))) {
27
bRet = FALSE;
28
}
29
30
return bRet;
31
}
32
Copied!

Writing

Writing is performed in sectors. Sector erase is performed beforehand.
1
#define FLASH_TYPE E_FL_CHIP_INTERNAL
2
#define FLASH_SECTOR_SIZE (32L* 1024L) // 32KB
3
#define FLASH_SECTOR_NUMBER 5 // 0..4 (for TWELITE BLUE)
4
5
/** @ingroup FLASH
6
* Flash Write
7
* @param psFlash Data to be written
8
* @param sector write sector
9
* @param offset Offset from the beginning of the write sector
10
* @return TRUE:Write success FALSE: Failure
11
*/
12
bool_t bFlash_Write(tsFlash *psFlash, uint8 sector, uint32 offset)
13
{
14
bool_t bRet = FALSE;
15
offset += (uint32)sector * FLASH_SECTOR_SIZE; // calculate the absolute address
16
17
if (bAHI_FlashInit(FLASH_TYPE, NULL) == TRUE) {
18
if (bAHI_FlashEraseSector(sector) == TRUE) { // erase a corresponding sector.
19
psFlash->u32Magic = FLASH_MAGIC_NUMBER;
20
psFlash->u8CRC = u8CCITT8((uint8*)&(psFlash->sData), sizeof(tsFlashApp));
21
if (bAHI_FullFlashProgram(offset, sizeof(tsFlash), (uint8 *)psFlash)) {
22
bRet = TRUE;
23
}
24
}
25
}
26
27
return bRet;
28
}
Copied!

Erase

This is an example of performing sector erasure.
1
/** @ingroup FLASH
2
* Flash Sector Erase
3
* @return TRUE: Write success FALSE: Failure
4
*/
5
bool_t bFlash_Erase(uint8 sector)
6
{
7
int i;
8
bool_t bRet = FALSE;
9
10
if (bAHI_FlashInit(FLASH_TYPE, NULL) == TRUE) {
11
if (bAHI_FlashEraseSector(sector) == TRUE) { // erase a corresponding sector.
12
bRet = TRUE;
13
}
14
}
15
16
return bRet;
17
}
Copied!
リンクのコピー