-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
438 lines (383 loc) · 18 KB
/
main.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
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
/**********************************************************************************************************************
* \file main.c
* \copyright Copyright (C) Infineon Technologies AG 2019
*
* Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
* business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
* are agreed, use of this file is subject to following:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
* accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
* and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
* Software is furnished to do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including the above license grant, this restriction
* and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are solely in the form of
* machine-executable object code generated by a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*********************************************************************************************************************/
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "cyhal.h"
#include "cybsp.h"
#include "cy_sysfault.h"
#include "cy_sysint.h"
#include "cy_retarget_io.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/* Test value */
#define CORRECT_DATA (0x0123456789abcdefull)
/* The 128-bit code word used for calculating the parity has this static 64-bit component */
#define CODEWORD_UPPER_64_BIT (0x0000000000000108ull)
/* Number of bytes in a 64-bit word */
#define BYTES_PER_64_BIT_WORD 8
/* Fault interrupt priority */
#define IRQ_PRIORITY 2
/* Shift value for CPU IRQ number ('intSrc' of cy_stc_sysint_t consists of CPU IRQ number and system IRQ number) */
#define CPU_IRQ_NUMBER_SHIFT 16
/*********************************************************************************************************************/
/*-------------------------------------------------Data Structures---------------------------------------------------*/
/*********************************************************************************************************************/
/* Data type for 128-bit variable */
typedef struct
{
uint64_t u64[2];
} Uint128Type;
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* Create a global volatile variable where the test access will store its data to prevent compiler optimization */
volatile uint64_t g_flashReadData;
/* Flag that is set in the fault IRQ handler and checked/cleared at other places */
bool g_faultIrqOccurred = false;
/*********************************************************************************************************************/
/*--------------------------------------------Private Variables/Constants--------------------------------------------*/
/*********************************************************************************************************************/
/* Variable that will end up as constant data in flash and will be used for the error injection test */
const uint64_t FLASH_TEST_VAR = CORRECT_DATA;
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/**********************************************************************************************************************
* Function Name: handleFaultIrq
* Summary:
* Handles the Fault Struct interrupt and will occur on a Code Flash correctable ECC fault.
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
static void handleFaultIrq(void)
{
uint32_t faultAddress;
uint32_t faultInfo;
printf("Fault IRQ Handler entered!\r\n");
/* Get fault specific data from the registers */
faultAddress = Cy_SysFault_GetFaultData(FAULT_STRUCT0, CY_SYSFAULT_DATA0);
faultInfo = Cy_SysFault_GetFaultData(FAULT_STRUCT0, CY_SYSFAULT_DATA1);
cy_en_SysFault_source_t errorSource = Cy_SysFault_GetErrorSource(FAULT_STRUCT0);
/* Check and display the fault information */
if (errorSource == CY_SYSFAULT_FLASHC_MAIN_C_ECC)
{
printf("Code Flash correctable ECC fault detected:\r\n");
printf("- Address: 0x%08lx\r\n", CY_FLASH_LG_SBM_BASE + faultAddress);
printf("- ECC syndromes: 0x%08lx (4x syndromes based on 256-bit aligned flash access)\r\n", faultInfo);
if (g_flashReadData == CORRECT_DATA)
{
printf("TEST OK!\r\n");
}
else
{
printf("TEST ERROR: Incorrect data read!\r\n");
}
}
else
{
printf("TEST ERROR: Unexpected fault source (0x%08lx) detected!\r\n", (uint32_t)errorSource);
}
/* Set flag so that test code can check that the IRQ has occurred */
g_faultIrqOccurred = true;
Cy_SysFault_ClearStatus(FAULT_STRUCT0);
Cy_SysFault_ClearInterrupt(FAULT_STRUCT0);
}
/**********************************************************************************************************************
* Function Name: Cy_SysLib_ProcessingFault
* Summary:
* Strong implementation of Cy_SysLib_ProcessingFault which will be called by the PDL fault handler
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
void Cy_SysLib_ProcessingFault(void)
{
printf("Hard Fault Handler entered!\r\n");
/* PDL fault handler has populated the global cy_faultFrame variable with fault information. The test access to
flash with a non-correctable ECC fault injected should result in a precise error and the Bus Fault Address
Register (BFAR) should be valid and hold the address of the test variable */
if ((cy_faultFrame.cfsr.cfsrBits.precisErr == 1) &&
(cy_faultFrame.cfsr.cfsrBits.bfarValid == 1) &&
(cy_faultFrame.bfar == (uint32_t)&FLASH_TEST_VAR))
{
printf("TEST OK!\r\n");
}
else
{
printf("TEST ERROR: Unknown Hard Fault!\r\n");
}
/* The fault struct could be used additionally to gather even more information about the fault */
/* Do not return from the fault */
for (;;)
{
}
}
/**********************************************************************************************************************
* Function Name: initFaultHandling
* Summary:
* Initialize fault handling to generate an IRQ on a Code Flash correctable ECC fault
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
static void initFaultHandling(void)
{
/* Only IRQ is needed as fault reaction in this example */
cy_stc_SysFault_t faultStructCfg =
{
.ResetEnable = false,
.OutputEnable = false,
.TriggerEnable = false,
};
/* Set up fault struct and enable interrupt for Code Flash correctable ECC fault */
Cy_SysFault_ClearStatus(FAULT_STRUCT0);
Cy_SysFault_SetMaskByIdx(FAULT_STRUCT0, CY_SYSFAULT_FLASHC_MAIN_C_ECC);
Cy_SysFault_SetInterruptMask(FAULT_STRUCT0);
if (Cy_SysFault_Init(FAULT_STRUCT0, &faultStructCfg) != CY_SYSFAULT_SUCCESS)
{
CY_ASSERT(0);
}
/* Set up the interrupt processing */
cy_stc_sysint_t irqCfg =
{
.intrSrc = ((NvicMux3_IRQn << CPU_IRQ_NUMBER_SHIFT) | cpuss_interrupts_fault_0_IRQn),
.intrPriority = IRQ_PRIORITY,
};
if (Cy_SysInt_Init(&irqCfg, &handleFaultIrq) != CY_SYSINT_SUCCESS)
{
CY_ASSERT(0);
}
NVIC_EnableIRQ((IRQn_Type) NvicMux3_IRQn);
}
/**********************************************************************************************************************
* Function Name: do128BitAnd
* Summary:
* Do bitwise AND operation on two 128-bit values
* Parameters:
* a - 1st value
* b - 2nd value
* Return:
* Uint128Type - result of AND operation
**********************************************************************************************************************
*/
static Uint128Type do128BitAnd(Uint128Type a, Uint128Type b)
{
Uint128Type result128;
result128.u64[0] = a.u64[0] & b.u64[0];
result128.u64[1] = a.u64[1] & b.u64[1];
return result128;
}
/**********************************************************************************************************************
* Function Name: do128BitXorReduction
* Summary:
* Do reduction XOR operation on 128-bit value
* Parameters:
* data128 - value
* Return:
* uint8_t - XOR reduction result
**********************************************************************************************************************
*/
static uint8_t do128BitXorReduction(Uint128Type data128)
{
/* Use Brian Kernighan algorithm to count the number of '1' bits in the provided 128-bit value */
uint8_t countOneBits = 0;
for (uint8_t word = 0; word < 2; word++)
{
for (/* No init needed */; data128.u64[word] != 0; countOneBits++)
{
/* Clear the least significant bit set */
data128.u64[word] &= (data128.u64[word] - 1);
}
}
/* If the number of bits set is even, the XOR reduction results in '0', or '1' otherwise */
return ((countOneBits % 2) == 0) ? 0 : 1;
}
/**********************************************************************************************************************
* Function Name: getParityForValue
* Summary:
* Calculate the Code Flash ECC parity for the provided 64-bit value
* Parameters:
* value64 - value for which ECC parity shall calculated
* Return:
* uint8_t - ECC parity
**********************************************************************************************************************
*/
static uint8_t getParityForValue(uint64_t value64)
{
/* Constants for ECC parity calculation as per the Architecture TRM */
static const Uint128Type ECC_P[8] =
{
{{0x44844a88952aad5bull, 0x01bfbb75be3a72dcull}},
{{0x1108931126b3366dull, 0x02df76f9dd99b971ull}},
{{0x06111c2238c3c78eull, 0x04efcf9f9ad5ce97ull}},
{{0x9821e043c0fc07f0ull, 0x08f7ecf6ed674e6cull}},
{{0xe03e007c00fff800ull, 0x10fb7baf6ba6b5a6ull}},
{{0xffc0007fff000000ull, 0x20fdb7cef36cab5bull}},
{{0xffffff8000000000ull, 0x40fedd7b74db55abull}},
{{0xd44225844ba65cb7ull, 0x807f000007ffffffull}},
};
/* The 128-bit code word which is the basis for the ECC parity calculation is constructed as follows */
Uint128Type codeWord128;
codeWord128.u64[0] = value64;
codeWord128.u64[1] = CODEWORD_UPPER_64_BIT;
/* Calculate each ECC parity bit individually according to the Architecture TRM */
uint8_t parity = 0;
for (uint32_t cnt = 0; cnt < (sizeof(ECC_P) / sizeof(ECC_P[0])); cnt++)
{
parity |= (do128BitXorReduction(do128BitAnd(codeWord128, ECC_P[cnt])) << cnt);
}
return parity;
}
/**********************************************************************************************************************
* Function Name: injectParity
* Summary:
* Enable the injection of the provided ECC parity value for the provided address
* Parameters:
* flashAddr - target flash address for which ECC parity shall be injected
* parity - parity value to be injected
* Return:
* none
**********************************************************************************************************************
*/
static void injectParity(uint32_t flashAddr, uint8_t parity)
{
uint32_t wordAddr;
/* The register expects a relative 64-bit word address/index */
wordAddr = (flashAddr - CY_FLASH_LG_SBM_BASE) / BYTES_PER_64_BIT_WORD;
/* Set the parity and target address */
FLASHC_ECC_CTL = _VAL2FLD(FLASHC_ECC_CTL_PARITY, parity) |
_VAL2FLD(FLASHC_ECC_CTL_WORD_ADDR, wordAddr);
/* Enable ECC injection */
CY_REG32_CLR_SET(FLASHC_FLASH_CTL, FLASHC_FLASH_CTL_MAIN_ECC_INJ_EN, 1);
/* Read back register to ensure that the previous register writes got effective */
FLASHC_FLASH_CTL;
}
/**********************************************************************************************************************
* Function Name: executeTestAccess
* Summary:
* Makes a test access to our target variable in flash with all required steps before and after the access
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
static void executeTestAccess(void)
{
/* Initialize the read data variable which might contain data from the previous access */
g_flashReadData = 0;
/* Clear flag that is used to detect the IRQ occurrence */
g_faultIrqOccurred = false;
/* Invalidate the respective address in the D-Cache to ensure that the test access hits the flash controller */
SCB_InvalidateDCache_by_Addr((void*)&FLASH_TEST_VAR, sizeof(FLASH_TEST_VAR));
/* Make the test access. The address of the test variable is used to prevent compiler optimization */
g_flashReadData = *((volatile const uint64_t*)&FLASH_TEST_VAR);
/* Add a data synchronization barrier to enforce the ordering of the test access with subsequent data accesses */
__DSB();
}
/**********************************************************************************************************************
* Function Name: main
* Summary:
* This is the main function.
* Parameters:
* none
* Return:
* int
**********************************************************************************************************************
*/
int main(void)
{
/* Will hold the correct parity for the configured test value */
uint8_t correctParity;
/* Initialize the device and board peripherals */
if (cybsp_init() != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
if (cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE) != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("****************** Flash ECC Error Injection Code Example ******************\r\n\r\n");
initFaultHandling();
correctParity = getParityForValue(FLASH_TEST_VAR);
printf("Info about test variable in flash\r\n");
printf("- Address: 0x%08lx\r\n", (uint32_t)&FLASH_TEST_VAR);
printf("- Correct ECC Parity: 0x%02x\r\n", correctParity);
printf("\r\n");
printf("Test step 1: Inject correct parity to prove correctness of ECC parity calculation\r\n");
injectParity((uint32_t)&FLASH_TEST_VAR, correctParity);
executeTestAccess();
if (g_faultIrqOccurred)
{
printf("TEST ERROR: Unexpected fault occurred!\r\n");
}
else if (g_flashReadData != CORRECT_DATA)
{
printf("TEST ERROR: Incorrect data read!\r\n");
}
else
{
printf("TEST OK!\r\n");
}
printf("\r\n");
printf("Test step 2: Inject parity with 1-bit error to test correctable ECC fault\r\n");
injectParity((uint32_t)&FLASH_TEST_VAR, correctParity ^ 0x01); /* Flip 1 bit */
executeTestAccess();
if (g_faultIrqOccurred == false)
{
printf("TEST ERROR: Fault IRQ has not occurred!\r\n");
}
printf("\r\n");
printf("Test step 3: Inject parity with 2-bit error to test non-correctable ECC fault\r\n");
injectParity((uint32_t)&FLASH_TEST_VAR, correctParity ^ 0x03); /* Flip 2 bits */
executeTestAccess();
/* A non-correctable ECC error in the Code Flash results in a bus error and hence a CPU HardFault exception
unless MAIN_ERR_SILENT bit in FLASHC_FLASH_CTL would be set. Optionally, the fault struct can catch a
non-correctable ECC fault as well, but this is not demonstrated in this example because the HardFault
exception has the higher priority anyway. */
printf("TEST ERROR: Execution flow should not have reached this point!\r\n");
for (;;)
{
}
}
/* [] END OF FILE */