/
external_flash.c
70 lines (54 loc) · 1.52 KB
/
external_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdint.h>
#include <xc.h>
#include "string.h"
#include "os.h"
#include "external_flash.h"
static FILEIO_MEDIA_INFORMATION mediaInformation;
FILEIO_MEDIA_INFORMATION * ExternalFlash_MediaInitialize(void* config)
{
mediaInformation.validityFlags.bits.sectorSize = true;
mediaInformation.sectorSize = FLASH_PAGE_SIZE;
mediaInformation.errorCode = MEDIA_NO_ERROR;
return &mediaInformation;
}
uint16_t ExternalFlash_SectorSizeRead(void* config)
{
return FLASH_PAGE_SIZE;
}
uint32_t ExternalFlash_CapacityRead(void* config)
{
//This is the truthful answer
return ((uint32_t) FLASH_NUMBER_OF_PAGES - 1);
}
uint8_t ExternalFlash_MediaDetect(void* config)
{
return true;
}
uint8_t ExternalFlash_WriteProtectStateGet(void* config)
{
return false;
}
uint8_t ExternalFlash_SectorRead(void* config, uint32_t sector_addr, uint8_t* buffer)
{
uint16_t page = (uint16_t) sector_addr;
//Error check. Make sure the host is trying to read from a legitimate address
if(sector_addr >= FLASH_NUMBER_OF_PAGES)
{
return false;
}
//Read the data
flash_sector_read(page, buffer);
return true;
}
uint8_t ExternalFlash_SectorWrite(void* config, uint32_t sector_addr, uint8_t* buffer, uint8_t allowWriteToZero)
{
uint16_t page = (uint16_t) sector_addr;
//First, error check the resulting address
if(sector_addr >= FLASH_NUMBER_OF_PAGES)
{
return false;
}
//Write new data to flash
flash_sector_write(page, buffer);
return true;
}