/
pal_internalFlash.c
327 lines (273 loc) · 9.6 KB
/
pal_internalFlash.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
* Copyright 2016, 2017 ARM Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include "pal.h"
#include "pal_plat_internalFlash.h"
#include <stdlib.h>
#include <string.h>
#define TRACE_GROUP "PAL"
#if (PAL_USE_INTERNAL_FLASH)
#define BITS_ALIGNED_TO_32 0x3
//////////////////////////GLOBALS SECTION ////////////////////////////
#if PAL_THREAD_SAFETY
// Use semaphore and not mutex, as mutexes don't behave well when trying to delete them while taken (which may happen in our tests).
static palSemaphoreID_t flashSem = 0;
#endif
//////////////////////////END GLOBALS SECTION ////////////////////////////
//////////////////////////START PRIVATE SECTION////////////////////////////
// Verify that the alignment to sector size
// Parameters :
// @param[in] address - Address to verify.
// @param[in] size - Size to write
// Return : None.
PAL_PRIVATE bool pal_isAlignedToSector(uint32_t address, size_t size)
{
uint32_t currentSectorSize = pal_internalFlashGetSectorSize(address);
if ((size % currentSectorSize) || (address % currentSectorSize))
{
return false;
}
else
{
return true;
}
}
// Program to Flash with alignments to page size
// Parameters :
// @param[in] buffer - pointer to the buffer to be written
// @param[in] size - the size of the buffer in bytes.
// @param[in] address - the address of the internal flash, must be aligned to minimum writing unit (page size).
// Return : None.
PAL_PRIVATE palStatus_t pal_programToFlashAligned(const size_t size, const uint32_t address, const uint32_t * buffer)
{
palStatus_t ret = PAL_SUCCESS;
uint32_t pageSize = 0, alignmentLeft = 0;
pageSize = pal_internalFlashGetPageSize();
alignmentLeft = size % pageSize; //Keep the leftover to be copied separately
if (size >= pageSize)
{
ret = pal_plat_internalFlashWrite(size - alignmentLeft, address, buffer);
}
if ((ret == PAL_SUCCESS) && (alignmentLeft != 0))
{
uint32_t * pageBuffer = (uint32_t *)malloc(pageSize);
if (pageBuffer == NULL)
{
ret = PAL_ERR_NO_MEMORY;
}
else
{
memset(pageBuffer, 0xFF, pageSize);
memcpy(pageBuffer, (uint8_t*)buffer + (size - alignmentLeft), alignmentLeft);
ret = pal_plat_internalFlashWrite(pageSize, address + (size - alignmentLeft), pageBuffer);
free(pageBuffer);
}
}
return ret;
}
//////////////////////////END PRIVATE SECTION////////////////////////////
size_t pal_internalFlashGetPageSize(void)
{
size_t ret = pal_plat_internalFlashGetPageSize();
return ret;
}
size_t pal_internalFlashGetSectorSize(uint32_t address)
{
size_t ret = pal_plat_internalFlashGetSectorSize(address);
return ret;
}
palStatus_t pal_internalFlashInit(void)
{
palStatus_t ret = PAL_SUCCESS;
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreCreate(1, &flashSem);
if (PAL_SUCCESS != ret)
{
PAL_LOG_ERR("Semaphore Create Error %" PRId32 ".", ret);
}
else
#endif
{
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreWait(flashSem, PAL_RTOS_WAIT_FOREVER, NULL);
if (PAL_SUCCESS == ret)
#endif
{
ret = pal_plat_internalFlashInit();
#if PAL_THREAD_SAFETY
palStatus_t error = pal_osSemaphoreRelease(flashSem);
if (PAL_SUCCESS != error)
{
PAL_LOG_ERR("SemaphoreRelease Error %" PRId32 ".", error);
}
#endif
}
if (PAL_SUCCESS != ret)
{//Clean resources, including the flash semaphore
pal_internalFlashDeInit();
}
}
return ret;
}
palStatus_t pal_internalFlashDeInit(void)
{
palStatus_t ret = PAL_SUCCESS;
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreWait(flashSem, PAL_RTOS_WAIT_FOREVER, NULL);
if (PAL_SUCCESS != ret)
{
return ret;
}
#endif
if (PAL_SUCCESS == ret)
{
ret = pal_plat_internalFlashDeInit();
if (PAL_SUCCESS != ret) {
PAL_LOG_ERR("pal_plat_internalFlashDeInit Error %" PRId32 ".", ret);
}
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreRelease(flashSem);
if (PAL_SUCCESS != ret) {
PAL_LOG_ERR("SemaphoreRelease Error %" PRId32 ".", ret);
}
ret = pal_osSemaphoreDelete(&flashSem);
if (PAL_SUCCESS != ret) {
PAL_LOG_ERR("pal_osSemaphoreDelete Error %" PRId32 ".", ret);
}
#endif
}
return ret;
}
palStatus_t pal_internalFlashRead(const size_t size, const uint32_t address, uint32_t * buffer)
{
palStatus_t ret = PAL_SUCCESS;
PAL_VALIDATE_CONDITION_WITH_ERROR ((buffer == NULL), PAL_ERR_INTERNAL_FLASH_NULL_PTR_RECEIVED)
PAL_VALIDATE_CONDITION_WITH_ERROR ((size == 0),PAL_ERR_INTERNAL_FLASH_WRONG_SIZE)
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreWait(flashSem, PAL_RTOS_WAIT_FOREVER, NULL);
if (PAL_SUCCESS != ret)
{
return ret;
}
#endif
ret = pal_plat_internalFlashRead(size, address, buffer);
#if PAL_THREAD_SAFETY
palStatus_t error = pal_osSemaphoreRelease(flashSem);
if (PAL_SUCCESS != error)
{
PAL_LOG_ERR("SemaphoreRelease Error %" PRId32 ".", error);
}
#endif
return ret;
}
palStatus_t pal_internalFlashErase(uint32_t address, size_t size)
{
palStatus_t ret = PAL_SUCCESS;
PAL_VALIDATE_CONDITION_WITH_ERROR ((size == 0),PAL_ERR_INTERNAL_FLASH_WRONG_SIZE)
PAL_VALIDATE_ARG_RLZ ((address & BITS_ALIGNED_TO_32),PAL_ERR_INTERNAL_FLASH_BUFFER_ADDRESS_NOT_ALIGNED)//Address not aligned to 32 bit
PAL_VALIDATE_ARG_RLZ ((!pal_isAlignedToSector(address,size)),PAL_ERR_INTERNAL_FLASH_SECTOR_NOT_ALIGNED)//not aligned to sector
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreWait(flashSem, PAL_RTOS_WAIT_FOREVER, NULL);
if (PAL_SUCCESS != ret)
{
return ret;
}
#endif
size_t sectorSize = 0;
sectorSize = pal_internalFlashGetSectorSize(address);
while (size)
{
ret = pal_plat_internalFlashErase(address, sectorSize);
size -= sectorSize;
address += pal_internalFlashGetSectorSize(address + sectorSize);
sectorSize = pal_internalFlashGetSectorSize(address);
}
#if PAL_THREAD_SAFETY
palStatus_t error = pal_osSemaphoreRelease(flashSem);
if (PAL_SUCCESS != error)
{
PAL_LOG_ERR("SemaphoreRelease Error %" PRId32 ".", error);
}
#endif
return ret;
}
palStatus_t pal_internalFlashWrite(const size_t size, const uint32_t address, const uint32_t * buffer)
{
palStatus_t ret = PAL_SUCCESS;
uint32_t pageSize = 0;
PAL_VALIDATE_CONDITION_WITH_ERROR ((buffer == NULL), PAL_ERR_INTERNAL_FLASH_NULL_PTR_RECEIVED)
PAL_VALIDATE_ARG_RLZ ((address & BITS_ALIGNED_TO_32),PAL_ERR_INTERNAL_FLASH_BUFFER_ADDRESS_NOT_ALIGNED)//Address not aligned to 32 bit
PAL_VALIDATE_ARG_RLZ ((size == 0),PAL_ERR_INTERNAL_FLASH_WRONG_SIZE)
pageSize = pal_internalFlashGetPageSize();
if (address % pageSize)
{
ret = PAL_ERR_INTERNAL_FLASH_ADDRESS_NOT_ALIGNED;
}
else
{
#if PAL_THREAD_SAFETY
ret = pal_osSemaphoreWait(flashSem, PAL_RTOS_WAIT_FOREVER, NULL);
if (PAL_SUCCESS != ret)
{
return ret;
}
#endif
size_t sizeLeft = size;
uint32_t tempAddress = address;
uint32_t sectorSize = pal_internalFlashGetSectorSize(address);
//This section handles writing on cross sectors
while (((tempAddress % sectorSize) + sizeLeft) > sectorSize)
{
size_t tmpSize = sectorSize - (tempAddress % sectorSize);
ret = pal_programToFlashAligned(tmpSize, tempAddress, buffer); //Fill the sector to the end
if (PAL_SUCCESS != ret)
{
break;
}
sizeLeft -= tmpSize;
tempAddress += tmpSize;
buffer += tmpSize / sizeof(uint32_t);
//Read sector size again because Sector size can change when crossing sectors.
sectorSize = pal_internalFlashGetSectorSize(address);
}
//Write part of a sector (remainder of the buffer)
if ((PAL_SUCCESS == ret) && (sizeLeft > 0))
{
ret = pal_programToFlashAligned(sizeLeft, tempAddress, buffer);
}
#if PAL_THREAD_SAFETY
palStatus_t error = pal_osSemaphoreRelease(flashSem);
if (PAL_SUCCESS != error)
{
PAL_LOG_ERR("SemaphoreRelease Error %" PRId32 ".", error);
}
#endif
}
return ret;
}
palStatus_t pal_internalFlashGetAreaInfo(uint8_t section, palSotpAreaData_t *data)
{
palStatus_t ret = PAL_SUCCESS;
const palSotpAreaData_t internalFlashArea[] =
{
{PAL_INTERNAL_FLASH_SECTION_1_ADDRESS, PAL_INTERNAL_FLASH_SECTION_1_SIZE},
{PAL_INTERNAL_FLASH_SECTION_2_ADDRESS, PAL_INTERNAL_FLASH_SECTION_2_SIZE}
};
PAL_VALIDATE_CONDITION_WITH_ERROR ((data == NULL), PAL_ERR_INTERNAL_FLASH_NULL_PTR_RECEIVED)
data->address = internalFlashArea[section].address;
data->size = internalFlashArea[section].size;
return ret;
}
#endif //(PAL_USE_INTERNAL_FLASH)