-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
301 lines (261 loc) · 11.1 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
/*******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the PSoC6 MCU Transition To RMA
* example for ModusToolbox.
*
* Related Document: See README.md
*
*
********************************************************************************
* Copyright 2021-2023, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Macros
*******************************************************************************/
#define CY_TRANSIT_TO_RMA_OPCODE (0x3B000000UL) /* The SROM API opcode for RMA lifcycle stage conversion .
Refer 'System Call APIs' section in device Technical Reference Manual*/
#define CY_OPCODE_SUCCESS (0xA0000000UL) /* The command completed with no errors */
#define CY_OPCODE_STS_Msk (0xF0000000UL) /* The status mask of the SROM API return value */
#define GPIO_INTERRUPT_PRIORITY (7u)
#define CY_IPC_STRUCT (Cy_IPC_Drv_GetIpcBaseAddress(CY_IPC_CHAN_SYSCALL))
#define CY_IPC_NOTIFY_STRUCT0 (0x1UL << CY_IPC_INTR_SYSCALL1)
#define SUCCESS (0)
#define FAILED (!SUCCESS)
#define JWT_LENGTH (367) /* Length of the JWT */
#define LIFECYCLE_EFUSE_OFFSET (0x02B)
/* Refer to Appendix B of the 'PSoC 6 MCU Programming Specifications' (002-15554) for efuse mapping*/
#define NORMAL_STAGE_EFUSE_BIT 0
#define SECURE_STAGE_EFUSE_BIT 2
#define SECURE_DEB_STAGE_EFUSE_BIT 1
#define RMA_STAGE_EFUSE_BIT 3
#define DELAY_BUTTON_DEBOUNCE_MS 500u /*delay in millisecond*/
#define IPC_STATUS_WAIT_TIME_S 60u /*maximum wait time for IPC Lock*/
#define DELAY_IPC_STATUS_CHECK_MS 1000u /*delay for checking the IPC status*/
/*******************************************************************************
* Structures
*******************************************************************************/
typedef struct
{
uint32_t opCode;
uint32_t api_param_addr;
}transit_to_RMA_param_t;
typedef struct
{
uint32_t length;
unsigned char jwt[JWT_LENGTH];
}jwt_param_t;
/*******************************************************************************
* Function Definitions
*******************************************************************************/
/*******************************************************************************
* Function Name: TransitionToRMA
********************************************************************************
* Summary:
* This function implements the 'TransitionToRMA' syscall
*
* Parameters:
* transit_to_RMA_param_t
*
* Return:
* uint32_t
*
*******************************************************************************/
uint32_t TransitionToRMA(transit_to_RMA_param_t *paramRMA)
{
uint32_t result = FAILED;
uint32_t elapsedTime =0; //count number of seconds elapsed
/* Send the IPC message */
if (Cy_IPC_Drv_SendMsgPtr(CY_IPC_STRUCT, CY_IPC_NOTIFY_STRUCT0, (void*)paramRMA) == CY_IPC_DRV_SUCCESS)
{
/* Wait for the IPC structure to be freed */
while (elapsedTime < IPC_STATUS_WAIT_TIME_S)
{
if (Cy_IPC_Drv_IsLockAcquired(CY_IPC_STRUCT) == false)
{
break;
}
//Delay by 1s before checking again
Cy_SysLib_Delay(DELAY_IPC_STATUS_CHECK_MS);
elapsedTime++;
}
/* The result of the SROM API call is returned to the opcode variable */
if ((paramRMA->opCode & CY_OPCODE_STS_Msk) == CY_OPCODE_SUCCESS)
{
result = SUCCESS;
}
else
{
result = FAILED;
}
}
else
{
result = FAILED;
}
return result;
}
/*******************************************************************************
* Function Name: ReadDeviceLifecycleStage
********************************************************************************
* Summary:
* This function reads and prints the efuse lifecycle stage
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void ReadDeviceLifecycleStage()
{
uint32_t bit_secure_stage = offsetof(cy_stc_efuse_data_t, LIFECYCLE_STAGE.SECURE);
uint32_t bit_normal_stage = offsetof(cy_stc_efuse_data_t, LIFECYCLE_STAGE.NORMAL);
uint32_t bit_secure_debug_stage = offsetof(cy_stc_efuse_data_t, LIFECYCLE_STAGE.SECURE_WITH_DEBUG);
uint32_t bit_secure_rma = offsetof(cy_stc_efuse_data_t, LIFECYCLE_STAGE.RMA);
bool bitVal_secure, bitVal_normal, bitVal_secDbg, bitVal_RMA;
Cy_EFUSE_GetEfuseBit(bit_normal_stage, &bitVal_normal);
Cy_EFUSE_GetEfuseBit(bit_secure_debug_stage, &bitVal_secDbg);
Cy_EFUSE_GetEfuseBit(bit_secure_stage, &bitVal_secure);
Cy_EFUSE_GetEfuseBit(bit_secure_rma, &bitVal_RMA);
if(bitVal_RMA)
{
printf("\r\nLife Cycle Stage : RMA\r\n");
}
else if((bitVal_secDbg) || (bitVal_secure))
{
if(bitVal_secDbg)
{
printf("\r\nLife Cycle Stage : SECURE_DEBUG\r\n");
}
else
{
printf("\r\nLife Cycle Stage : SECURE\r\n");
}
}
else
{
printf("\r\nLife Cycle Stage : NORMAL\r\n");
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for CPU. It transitions the PSoC 64 MCU into RMA mode
* when the user button is pressed
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
uint32_t rmaApiResult= FAILED;
uint32_t length;
transit_to_RMA_param_t transitToRmaParam;
jwt_param_t jwt_param;
//NOTE: See the readme file for instructions to generate this JWT
char jwt[] = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRoIjp7ImRpZV9pZCI"
"6eyJtYXgiOnsiZGF5IjoyNTUsImxvdCI6MTY3NzcyMTUsIm1vbnRoIjoyNTUsIndh"
"ZmVyIjoyNTUsInhwb3MiOjI1NSwieWVhciI6MjU1LCJ5cG9zIjoyNTV9LCJtaW4iO"
"nsiZGF5IjowLCJsb3QiOjAsIm1vbnRoIjowLCJ3YWZlciI6MCwieHBvcyI6MCwieW"
"VhciI6MCwieXBvcyI6MH19fX0.z6ePvuJTcY0z3azJFGpzcq0-4bxxpgfL7H-E4V-"
"Dg6UGpwpLqf8pFXdMIXNXbQKCYW1Pq5HM7npZXNTUDtgEEw";
#if defined (CY_DEVICE_SECURE)
cyhal_wdt_t wdt_obj;
/* Clear watchdog timer so that it doesn't trigger a reset */
result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
CY_ASSERT(CY_RSLT_SUCCESS == result);
cyhal_wdt_free(&wdt_obj);
#endif
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize the user button */
result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT,
CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
printf("\r\n***** PSoC 6 MCU : TransitionToRMA ***** \r\n\n");
/* Read the Device Lifecycle */
ReadDeviceLifecycleStage();
/* Prepare the sycall params */
transitToRmaParam.opCode = CY_TRANSIT_TO_RMA_OPCODE;
transitToRmaParam.api_param_addr = (uint32_t) &jwt_param;
length = (uint32_t)strlen(jwt);
jwt_param.length = length;
for(uint32_t jwtIdx = 0; jwtIdx < length; jwtIdx++)
{
jwt_param.jwt[jwtIdx] = jwt[jwtIdx];
}
printf("\r\n**Warning : After Device is in RMA, it cannot be moved to other LCS** \r\n");
printf("\r\nWaiting for User button (SW2) press to transition device to RMA \r\n");
for (;;)
{
/* Read current button state from the user button */
if (cyhal_gpio_read(CYBSP_USER_BTN) == false)
{
/*500ms delay for button debounce on button press*/
Cy_SysLib_Delay(DELAY_BUTTON_DEBOUNCE_MS);
/*Execute TransitionToRMA syscall*/
rmaApiResult = TransitionToRMA(&transitToRmaParam);
if (rmaApiResult == SUCCESS)
{
printf("\r\nTransition to RMA successful\r\n");
}
else
{
printf("\r\nTransition to RMA failed\r\n");
}
}
}
}
/* [] END OF FILE */