Debian Codegraph @

ports/cc3200/fatfs/src/drivers/sflash_diskio.c

raw
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4
5 #include "py/mpconfig.h"
6 #include "py/obj.h"
7 #include "lib/oofatfs/ff.h"
8 #include "lib/oofatfs/diskio.h"
9 #include "simplelink.h"
10 #include "sflash_diskio.h"
11 #include "debug.h"
12 #include "modnetwork.h"
13 #include "modwlan.h"
14
15 #define SFLASH_TIMEOUT_MAX_MS 5500
16 #define SFLASH_WAIT_TIME_MS 5
17
18 static _u8 sflash_block_name[] = "__NNN__.fsb";
19 static _u8 *sflash_block_cache;
20 static bool sflash_init_done = false;
21 static bool sflash_cache_is_dirty;
22 static uint32_t sflash_ublock;
23 static uint32_t sflash_prblock;
24
25
26 static void print_block_name (_u32 ublock) {
27 char _sblock[4];
28 snprintf (_sblock, sizeof(_sblock), "%03u", ublock);
29 memcpy (&sflash_block_name[2], _sblock, 3);
30 }
31
32 static bool sflash_access (_u32 mode, _i32 (* sl_FsFunction)(_i32 FileHdl, _u32 Offset, _u8* pData, _u32 Len)) {
33 _i32 fileHandle;
34 bool retval = false;
35
36 // wlan must be enabled in order to access the serial flash
37 sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
38
39 if (0 == sl_FsOpen(sflash_block_name, mode, NULL, &fileHandle)) {
40 if (SFLASH_BLOCK_SIZE == sl_FsFunction (fileHandle, 0, sflash_block_cache, SFLASH_BLOCK_SIZE)) {
41 retval = true;
42 }
43 sl_FsClose (fileHandle, NULL, NULL, 0);
44 }
45 sl_LockObjUnlock (&wlan_LockObj);
46 return retval;
47 }
48
49 DRESULT sflash_disk_init (void) {
50 _i32 fileHandle;
51 SlFsFileInfo_t FsFileInfo;
52
53 if (!sflash_init_done) {
54 // Allocate space for the block cache
55 ASSERT ((sflash_block_cache = mem_Malloc(SFLASH_BLOCK_SIZE)) != NULL);
56 sflash_init_done = true;
57 sflash_prblock = UINT32_MAX;
58 sflash_cache_is_dirty = false;
59
60 // In order too speed up booting, check the last block, if exists, then
61 // it means that the file system has been already created
62 print_block_name (SFLASH_BLOCK_COUNT - 1);
63 sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
64 if (!sl_FsGetInfo(sflash_block_name, 0, &FsFileInfo)) {
65 sl_LockObjUnlock (&wlan_LockObj);
66 return RES_OK;
67 }
68 sl_LockObjUnlock (&wlan_LockObj);
69
70 // Proceed to format the memory
71 for (int i = 0; i < SFLASH_BLOCK_COUNT; i++) {
72 print_block_name (i);
73 sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
74 // Create the block file if it doesn't exist
75 if (sl_FsGetInfo(sflash_block_name, 0, &FsFileInfo) != 0) {
76 if (!sl_FsOpen(sflash_block_name, FS_MODE_OPEN_CREATE(SFLASH_BLOCK_SIZE, 0), NULL, &fileHandle)) {
77 sl_FsClose(fileHandle, NULL, NULL, 0);
78 sl_LockObjUnlock (&wlan_LockObj);
79 memset(sflash_block_cache, 0xFF, SFLASH_BLOCK_SIZE);
80 if (!sflash_access(FS_MODE_OPEN_WRITE, sl_FsWrite)) {
81 return RES_ERROR;
82 }
83 }
84 else {
85 // Unexpected failure while creating the file
86 sl_LockObjUnlock (&wlan_LockObj);
87 return RES_ERROR;
88 }
89 }
90 sl_LockObjUnlock (&wlan_LockObj);
91 }
92 }
93 return RES_OK;
94 }
95
96 DRESULT sflash_disk_status(void) {
97 if (!sflash_init_done) {
98 return STA_NOINIT;
99 }
100 return RES_OK;
101 }
102
103 DRESULT sflash_disk_read(BYTE *buff, DWORD sector, UINT count) {
104 uint32_t secindex;
105
106 if (!sflash_init_done) {
107 return STA_NOINIT;
108 }
109
110 if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
111 return RES_PARERR;
112 }
113
114 for (int i = 0; i < count; i++) {
115 secindex = (sector + i) % SFLASH_SECTORS_PER_BLOCK;
116 sflash_ublock = (sector + i) / SFLASH_SECTORS_PER_BLOCK;
117 // See if it's time to read a new block
118 if (sflash_prblock != sflash_ublock) {
119 if (sflash_disk_flush() != RES_OK) {
120 return RES_ERROR;
121 }
122 sflash_prblock = sflash_ublock;
123 print_block_name (sflash_ublock);
124 if (!sflash_access(FS_MODE_OPEN_READ, sl_FsRead)) {
125 return RES_ERROR;
126 }
127 }
128 // Copy the requested sector from the block cache
129 memcpy (buff, &sflash_block_cache[(secindex * SFLASH_SECTOR_SIZE)], SFLASH_SECTOR_SIZE);
130 buff += SFLASH_SECTOR_SIZE;
131 }
132 return RES_OK;
133 }
134
135 DRESULT sflash_disk_write(const BYTE *buff, DWORD sector, UINT count) {
136 uint32_t secindex;
137 int32_t index = 0;
138
139 if (!sflash_init_done) {
140 return STA_NOINIT;
141 }
142
143 if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
144 sflash_disk_flush();
145 return RES_PARERR;
146 }
147
148 do {
149 secindex = (sector + index) % SFLASH_SECTORS_PER_BLOCK;
150 sflash_ublock = (sector + index) / SFLASH_SECTORS_PER_BLOCK;
151 // Check if it's a different block than last time
152 if (sflash_prblock != sflash_ublock) {
153 if (sflash_disk_flush() != RES_OK) {
154 return RES_ERROR;
155 }
156 sflash_prblock = sflash_ublock;
157 print_block_name (sflash_ublock);
158 // Read the block into the cache
159 if (!sflash_access(FS_MODE_OPEN_READ, sl_FsRead)) {
160 return RES_ERROR;
161 }
162 }
163 // Copy the input sector to the block cache
164 memcpy (&sflash_block_cache[(secindex * SFLASH_SECTOR_SIZE)], buff, SFLASH_SECTOR_SIZE);
165 buff += SFLASH_SECTOR_SIZE;
166 sflash_cache_is_dirty = true;
167 } while (++index < count);
168
169 return RES_OK;
170 }
171
172 DRESULT sflash_disk_flush (void) {
173 // Write back the cache if it's dirty
174 if (sflash_cache_is_dirty) {
175 if (!sflash_access(FS_MODE_OPEN_WRITE, sl_FsWrite)) {
176 return RES_ERROR;
177 }
178 sflash_cache_is_dirty = false;
179 }
180 return RES_OK;
181 }
182