[gp712] align with the new otPlatFlash API (#4774)

This commit is contained in:
Thomas
2020-04-01 10:24:24 -07:00
committed by GitHub
parent 3579a68b93
commit 7fd9902c38
2 changed files with 63 additions and 62 deletions
+52 -62
View File
@@ -30,6 +30,7 @@
#include "platform_qorvo.h"
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -38,20 +39,18 @@
#include <unistd.h>
#include <openthread/config.h>
#include <openthread/platform/flash.h>
#include "utils/code_utils.h"
#include "utils/flash.h"
static int sFlashFd = -1;
static int sFlashFd;
uint32_t sEraseAddress;
enum
{
SWAP_SIZE = 2048,
SWAP_NUM = 2,
};
#define FLASH_SIZE 0x40000
#define FLASH_PAGE_SIZE 0x800
#define FLASH_PAGE_NUM 128
otError utilsFlashInit(void)
void otPlatFlashInit(otInstance *aInstance)
{
otError error = OT_ERROR_NONE;
char fileName[20];
struct stat st;
bool create = false;
@@ -70,89 +69,80 @@ otError utilsFlashInit(void)
create = true;
}
sFlashFd = open(fileName, O_RDWR | O_CREAT, 0666);
sFlashFd = open(fileName, O_RDWR | O_CREAT, 0600);
lseek(sFlashFd, 0, SEEK_SET);
otEXPECT_ACTION(sFlashFd >= 0, error = OT_ERROR_FAILED);
assert(sFlashFd >= 0);
if (create)
{
for (uint16_t index = 0; index < FLASH_PAGE_NUM; index++)
for (uint8_t index = 0; index < SWAP_NUM; index++)
{
error = utilsFlashErasePage(index * FLASH_PAGE_SIZE);
otEXPECT(error == OT_ERROR_NONE);
otPlatFlashErase(aInstance, index);
}
}
exit:
return error;
}
uint32_t utilsFlashGetSize(void)
uint32_t otPlatFlashGetSwapSize(otInstance *aInstance)
{
return FLASH_SIZE;
OT_UNUSED_VARIABLE(aInstance);
return SWAP_SIZE;
}
otError utilsFlashErasePage(uint32_t aAddress)
void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex)
{
otError error = OT_ERROR_NONE;
OT_UNUSED_VARIABLE(aInstance);
uint32_t address;
uint8_t dummyPage[FLASH_SIZE];
uint8_t buffer[SWAP_SIZE];
ssize_t r;
otEXPECT_ACTION(sFlashFd >= 0, error = OT_ERROR_FAILED);
otEXPECT_ACTION(aAddress < FLASH_SIZE, error = OT_ERROR_INVALID_ARGS);
assert((sFlashFd) >= 0 && (aSwapIndex < SWAP_NUM));
// Get start address of the flash page that includes aAddress
address = aAddress & (~(uint32_t)(FLASH_PAGE_SIZE - 1));
// set the page to the erased state.
memset((void *)(&dummyPage[0]), 0xff, FLASH_PAGE_SIZE);
address = aSwapIndex ? SWAP_SIZE : 0;
memset(buffer, 0xff, sizeof(buffer));
// Write the page
r = pwrite(sFlashFd, &(dummyPage[0]), FLASH_PAGE_SIZE, (off_t)address);
otEXPECT_ACTION((r) == ((FLASH_PAGE_SIZE)), error = OT_ERROR_FAILED);
exit:
return error;
r = pwrite(sFlashFd, buffer, sizeof(buffer), (off_t)address);
assert(r == SWAP_SIZE);
}
otError utilsFlashStatusWait(uint32_t aTimeout)
void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize)
{
OT_UNUSED_VARIABLE(aTimeout);
OT_UNUSED_VARIABLE(aInstance);
return OT_ERROR_NONE;
uint32_t address;
ssize_t rval;
assert((sFlashFd >= 0) && (aSwapIndex < SWAP_NUM) && (aSize <= SWAP_SIZE) && (aOffset <= (SWAP_SIZE - aSize)));
address = aSwapIndex ? SWAP_SIZE : 0;
rval = pread(sFlashFd, aData, aSize, (off_t)(address + aOffset));
assert((uint32_t)rval == aSize);
}
uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, const void *aData, uint32_t aSize)
{
uint32_t ret = 0;
uint32_t index = 0;
OT_UNUSED_VARIABLE(aInstance);
uint32_t address;
uint8_t byte;
ssize_t rval;
otEXPECT(sFlashFd >= 0 && aAddress < FLASH_SIZE);
assert((sFlashFd >= 0) && (aSwapIndex < SWAP_NUM) && (aSize <= SWAP_SIZE) && (aOffset <= (SWAP_SIZE - aSize)));
for (index = 0; index < aSize; index++)
address = aSwapIndex ? SWAP_SIZE : 0;
address += aOffset;
for (uint32_t offset = 0; offset < aSize; offset++)
{
ret = utilsFlashRead(aAddress + index, &byte, 1);
otEXPECT(ret == 1);
rval = pread(sFlashFd, &byte, sizeof(byte), (off_t)(address + offset));
assert(rval == sizeof(byte));
// Use bitwise AND to emulate the behavior of flash memory
byte &= aData[index];
ret = (uint32_t)pwrite(sFlashFd, &byte, 1, (off_t)(aAddress + index));
otEXPECT(ret == 1);
byte &= ((uint8_t *)aData)[offset];
rval = pwrite(sFlashFd, &byte, sizeof(byte), (off_t)(address + offset));
assert(rval == sizeof(byte));
}
exit:
return index;
}
uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
uint32_t ret = 0;
otEXPECT(sFlashFd >= 0 && aAddress < FLASH_SIZE);
ret = (uint32_t)pread(sFlashFd, aData, aSize, (off_t)aAddress);
exit:
return ret;
}
@@ -42,4 +42,15 @@
*/
#define OPENTHREAD_CONFIG_PLATFORM_INFO "GP712"
/**
* @def OPENTHREAD_CONFIG_PLATFORM_INFO
*
* Define to 1 to enable otPlatFlash* APIs to support non-volatile storage.
*
* When defined to 1, the platform MUST implement the otPlatFlash* APIs
* instead of the otPlatSettings*
*
*/
#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 1
#endif // OPENTHREAD_CORE_GP712_CONFIG_H_