-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyFlash.c
More file actions
469 lines (422 loc) · 15.7 KB
/
CyFlash.c
File metadata and controls
469 lines (422 loc) · 15.7 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*******************************************************************************
* File Name: CyFlash.c
* Version 4.11
*
* Description:
* Provides an API for the FLASH.
*
* Note:
* Documentation of the API's in this file is located in the
* System Reference Guide provided with PSoC Creator.
*
********************************************************************************
* Copyright 2010-2014, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include "CyFlash.h"
#include "cyPm.h"
#if ((CYDEV_CHIP_MEMBER_USED == CYDEV_CHIP_MEMBER_4A) || (CYDEV_CHIP_MEMBER_USED == CYDEV_CHIP_MEMBER_4D))
#define CYFLASH_CHECKSUM_WORKAROUND 1u
#endif /* ((CYDEV_CHIP_MEMBER_USED == CYDEV_CHIP_MEMBER_4A) || (CYDEV_CHIP_MEMBER_USED == CYDEV_CHIP_MEMBER_4D)) */
#if (CY_PSOC4A)
struct CySysFlashClockState
{
uint32 imoConfigReg;
};
#else
struct CySysFlashClockState
{
uint32 clkSelectReg;
uint32 clkImoEna;
uint32 clkImoFreq;
};
#endif /* (CY_PSOC4A) */
static uint32 CySysFlashExecSysCall(uint32 cmd, volatile const void *arg);
static uint32 CySysFlashCallProgramRow(uint32 rowNum, uint32 operation);
static uint32 CySysFlashHandleLoadResult(void);
#if (CY_PSOC4A)
static void CySysFlashSetupClock(struct CySysFlashClockState *saved);
static void CySysFlashRestoreClock(const struct CySysFlashClockState *saved);
#else
static void CySysFlashSetupClock(struct CySysFlashClockState *saved);
static void CySysFlashRestoreClock(const struct CySysFlashClockState *saved);
#endif /* (CY_PSOC4A) */
#if (CY_PSOC4A)
/*******************************************************************************
* Function Name: CySysFlashSetupClock
********************************************************************************
*
* Summary:
* Configures the device clock settings for the flash write.
*
* Parameters:
* saved:
* Pointer to the structure where the data are saved.
*
* Return:
* None
*
*******************************************************************************/
static void CySysFlashSetupClock(struct CySysFlashClockState *saved)
{
/* Preserve IMO configuration that could be changed
* during syscall execution (Cypress ID #150448).
*/
saved->imoConfigReg = CY_SYS_CLK_IMO_CONFIG_REG;
}
/*******************************************************************************
* Function Name: CySysFlashRestoreClock
********************************************************************************
*
* Summary:
* Restores the device clock settings.
*
* Parameters:
* saved:
* Pointer to the structure from where the data are saved.
*
* Return:
* None
*
*******************************************************************************/
static void CySysFlashRestoreClock(const struct CySysFlashClockState *saved)
{
CY_SYS_CLK_IMO_CONFIG_REG = saved->imoConfigReg;
}
#else
/*******************************************************************************
* Function Name: CySysFlashSetupClock
********************************************************************************
*
* Summary:
* Configures the device clock settings for the flash write.
*
* Parameters:
* saved:
* Pointer to the structure where the data are saved.
*
* Return:
* None
*
*******************************************************************************/
static void CySysFlashSetupClock(struct CySysFlashClockState *saved)
{
saved->clkSelectReg = CY_SYS_CLK_SELECT_REG;
saved->clkImoEna = CY_SYS_CLK_IMO_CONFIG_REG & CY_SYS_CLK_IMO_CONFIG_ENABLE;
saved->clkImoFreq = CY_SYS_CLK_IMO_MIN_FREQ_MHZ + (CY_SYS_CLK_IMO_SELECT_REG << 2);
/* The FM-Lite IP (s8fmlt) uses the IMO at 48MHz for the pump clock and SPC
* timer clock. The PUMP_SEL and HF clock must be set to IMO before calling
* Flash write or erase operation.
*/
if ((saved->clkImoFreq != 48u) ||
((saved->clkSelectReg & CY_SYS_CLK_SELECT_DIRECT_SEL_MASK) != CY_SYS_CLK_HFCLK_IMO) ||
(((saved->clkSelectReg >> CY_SYS_CLK_SELECT_PUMP_SEL_SHIFT) & CY_SYS_CLK_SELECT_PUMP_SEL_MASK) !=
CY_SYS_CLK_SELECT_PUMP_SEL_IMO))
{
CySysClkWriteHfclkDiv(CY_SYS_CLK_HFCLK_DIV_4);
if (saved->clkImoFreq != 48u)
{
CySysClkWriteImoFreq(48u);
}
CySysClkImoStart();
CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
/* Set IMO output as clock source for charge pump clock */
CY_SYS_CLK_SELECT_REG &=
((uint32)~(uint32)(CY_SYS_CLK_SELECT_PUMP_SEL_MASK << CY_SYS_CLK_SELECT_PUMP_SEL_SHIFT));
CY_SYS_CLK_SELECT_REG |= (uint32)((uint32)1u << CY_SYS_CLK_SELECT_PUMP_SEL_SHIFT);
}
}
/*******************************************************************************
* Function Name: CySysFlashRestoreClock
********************************************************************************
*
* Summary:
* Restores the device clock settings.
*
* Parameters:
* saved:
* Pointer to the structure from where the data are saved.
*
* Return:
* None
*
*******************************************************************************/
static void CySysFlashRestoreClock(const struct CySysFlashClockState *saved)
{
if ((saved->clkImoFreq != 48u) ||
((saved->clkSelectReg & CY_SYS_CLK_SELECT_DIRECT_SEL_MASK) != CY_SYS_CLK_HFCLK_IMO) ||
(((saved->clkSelectReg >> CY_SYS_CLK_SELECT_PUMP_SEL_SHIFT) & CY_SYS_CLK_SELECT_PUMP_SEL_MASK) !=
CY_SYS_CLK_SELECT_PUMP_SEL_IMO))
{
if (saved->clkImoFreq != 48u)
{
CySysClkWriteImoFreq(saved->clkImoFreq);
}
CySysClkWriteHfclkDiv(CY_SYS_CLK_HFCLK_DIV_8);
CySysClkWriteHfclkDirect(saved->clkSelectReg & CY_SYS_CLK_SELECT_DIRECT_SEL_MASK);
CY_SYS_CLK_SELECT_REG = saved->clkSelectReg;
if (0u == saved->clkImoEna)
{
CySysClkImoStop();
}
}
}
#endif /* (CY_PSOC4A) */
/*******************************************************************************
* Function Name: CySysFlashExecSysCall
********************************************************************************
*
* Summary:
* Executes specified system call command.
*
* Parameters:
* uint32 cmd:
* CY_FLASH_SILICON_ID_OPCODE - This returns a 12 bit Family ID, 16 bit
* Silicon ID and 8 bit Revision ID.
*
* CY_FLASH_LOAD_BYTE_OPCODE - Loads data to the page latch buffer to be
* programmed into the FLASH.
*
* volatile void *arg:
* Parameters of the command specified by the cmd parameter.
*
* Return:
* Result of the system call execution.
*
*******************************************************************************/
static uint32 CySysFlashExecSysCall(uint32 cmd, volatile const void *arg)
{
CY_FLASH_CPUSS_SYSARG_REG = (uint32)arg;
CY_FLASH_CPUSS_SYSREQ_REG = cmd | CY_FLASH_CPUSS_REQ_START;
while ((CY_FLASH_CPUSS_SYSREQ_REG & CY_FLASH_CPUSS_REQ_START) != 0u)
{
/* Wait for command to be executed */
}
return CY_FLASH_CPUSS_SYSARG_REG;
}
/*******************************************************************************
* Function Name: CySysFlashCallProgramRow
********************************************************************************
*
* Summary:
* Executes the Program Row command.
*
* Parameters:
* uint32 rowNum:
* Flash row number.
*
* uint32 operation:
* Operation to be executed:
* CY_FLASH_WRITE_ROW_OPCODE - Write row: erase and program flash row.
* CY_FLASH_PROGRAM_ROW_OPCODE - Program flash row.
*
*
* Return:
* Status of the last executed command.
*
*******************************************************************************/
static uint32 CySysFlashCallProgramRow(uint32 rowNum, uint32 operation)
{
volatile struct
{
uint8 key1;
uint8 key2;
uint16 row;
} args;
args.key1 = (uint8)CY_FLASH_SROM_KEY1;
args.key2 = (uint8)(CY_FLASH_SROM_KEY2 + operation);
args.row = (uint16) rowNum;
return CySysFlashExecSysCall(operation, &args);
}
/*******************************************************************************
* Function Name: CySysFlashHandleLoadResult
********************************************************************************
*
* Summary:
* Returns the status of the last executed command.
*
* Parameters:
* None
*
* Return:
* Status of the last executed command.
*
*******************************************************************************/
static uint32 CySysFlashHandleLoadResult(void)
{
uint32 value = CY_FLASH_CPUSS_SYSARG_REG;
uint32 retValue = CYRET_SUCCESS;
/* Check if value is written to SRAM_BASE */
while (((value & CY_FLASH_SROM_CMD_RETURN_MASK) != CY_FLASH_SROM_CMD_RETURN_SUCC) && (retValue == CYRET_SUCCESS))
{
if ((value & CY_FLASH_SROM_CMD_RETURN_MASK) == CY_FLASH_SROM_CMD_RETURN_MASK)
{
retValue = value; /* Error value */
}
value = CY_FLASH_CPUSS_SYSARG_REG;
}
return retValue;
}
/*******************************************************************************
* Function Name: CySysFlashWriteRow
********************************************************************************
*
* Summary:
* Writes a row of Flash.
*
* Parameters:
* uint16 rowNum:
* Row number. Each row is 128 bytes so a 32KB flash memory will have a valid
* range of [0-255].
*
* uint8* rowData:
* Array of bytes to write.
*
* Return:
* Status
* Value Description
* CYRET_SUCCESS Successful
* CYRET_LOCKED Flash writing already in use
* CYRET_CANCELED Command not accepted
* CYRET_BAD_PARAM One or more invalid parameters
* Other non-zero Failure
*
*******************************************************************************/
cystatus CySysFlashWriteRow(uint32 rowNum, const uint8 rowData[])
{
cystatus retValue = CYRET_SUCCESS;
uint32 statusCode;
uint8 interruptState;
struct CySysFlashClockState savedClkCfg;
volatile struct
{
uint8 key1;
uint8 key2;
uint8 byteAddr;
uint8 flashMacro;
uint32 numBytesMinusOne;
uint32 data[CY_FLASH_SIZEOF_ROW / sizeof(uint32)];
} loadBuf;
#ifdef CYFLASH_CHECKSUM_WORKAROUND
uint32 needChecksumWorkaround = 0u;
uint32 savedIndex = 0u;
uint32 savedValue = 0u;
uint32 checksum = 0u;
uint32 bits = 0u;
uint32 i;
#endif /* CYFLASH_CHECKSUM_WORKAROUND */
if ((rowNum < CY_FLASH_NUMBER_ROWS) && (rowData != 0u))
{
loadBuf.key1 = (uint8)CY_FLASH_SROM_KEY1;
loadBuf.key2 = (uint8)CY_FLASH_SROM_KEY2_LOAD;
loadBuf.byteAddr = (uint8)0u;
#if(CY_FLASH_NUMBER_ROWS > 255u)
loadBuf.flashMacro = (uint8)(rowNum >> 8);
#else
loadBuf.flashMacro = (uint8)0u;
#endif /* (CY_FLASH_NUMBER_ROWS > 255u) */
loadBuf.numBytesMinusOne = (CY_FLASH_SIZEOF_ROW - 1u);
(void)memcpy((void *)loadBuf.data, rowData, CY_FLASH_SIZEOF_ROW);
#ifdef CYFLASH_CHECKSUM_WORKAROUND
for (i = 0u; i < (CY_FLASH_SIZEOF_ROW / sizeof(uint32)); i++)
{
uint32 w = loadBuf.data[i];
if (w != 0u)
{
checksum += w;
bits |= w;
savedIndex = i;
}
}
needChecksumWorkaround = ((checksum == 0u) && (bits != 0u)) ? 1u : 0u;
if (needChecksumWorkaround != 0u)
{
savedValue = loadBuf.data[savedIndex];
loadBuf.data[savedIndex] = 0u;
}
#endif /* CYFLASH_CHECKSUM_WORKAROUND */
/* First load data. Then invoke Write row command. */
(void)CySysFlashExecSysCall(CY_FLASH_LOAD_BYTE_OPCODE, &loadBuf);
retValue = CySysFlashHandleLoadResult();
if(retValue == CYRET_SUCCESS)
{
/* Mask all exceptions to guarantee that IMO configuration will be changed
* in the atomic way. It will not affect the syscall execution (flash row write)
* since it is executed in the NMI context.
*/
interruptState = CyEnterCriticalSection();
CySysFlashSetupClock(&savedClkCfg);
statusCode = CySysFlashCallProgramRow(rowNum, CY_FLASH_WRITE_ROW_OPCODE);
if (statusCode != CY_FLASH_SROM_CMD_RETURN_SUCC)
{
retValue = statusCode;
}
#ifdef CYFLASH_CHECKSUM_WORKAROUND
if ((retValue == CYRET_SUCCESS) && (needChecksumWorkaround != 0u))
{
(void)memset((void *)loadBuf.data, 0, CY_FLASH_SIZEOF_ROW);
loadBuf.data[savedIndex] = savedValue;
(void)CySysFlashExecSysCall(CY_FLASH_LOAD_BYTE_OPCODE, &loadBuf);
retValue = CySysFlashHandleLoadResult();
if (retValue == CYRET_SUCCESS)
{
statusCode = CySysFlashCallProgramRow(rowNum, CY_FLASH_PROGRAM_ROW_OPCODE);
if (statusCode != CY_FLASH_SROM_CMD_RETURN_SUCC)
{
retValue = statusCode;
}
}
}
#endif /* CYFLASH_CHECKSUM_WORKAROUND */
CySysFlashRestoreClock(&savedClkCfg);
CyExitCriticalSection(interruptState);
}
}
else
{
retValue = CYRET_BAD_PARAM;
}
return (retValue);
}
#if(CY_PSOC4A)
/*******************************************************************************
* Function Name: CySysFlashSetWaitCycles
********************************************************************************
*
* Summary:
* Sets the number of clock cycles the cache will wait before it samples data
* coming back from Flash. This function must be called before increasing the
* SYSCLK clock frequency. It can optionally be called after lowering SYSCLK
* clock frequency in order to improve the CPU performance.
*
* Parameters:
* freq: Valid range [3-48]. Frequency for operation of the IMO.
* Note: Invalid frequency will be ignored.
*
* Return:
* None
*
*******************************************************************************/
void CySysFlashSetWaitCycles(uint32 freq)
{
if ((freq >= CY_SYS_CLK_IMO_MIN_FREQ_MHZ) && (freq <= CY_SYS_CLK_IMO_MAX_FREQ_MHZ))
{
if ( freq <= CY_FLASH_SYSCLK_BOUNDARY_MHZ )
{
CY_SYS_CLK_SELECT_REG &= (uint32)(~CY_FLASH_WAIT_STATE_EN);
}
else
{
CY_SYS_CLK_SELECT_REG |= CY_FLASH_WAIT_STATE_EN;
}
}
else
{
/* Halt CPU in debug mode if frequency is invalid */
CYASSERT(0u != 0u);
}
}
#endif /* (CY_PSOC4A) */
/* [] END OF FILE */