Skip to content

Commit 8944fed

Browse files
[fix][services] optimize STSAFE-A frame reception (to fix emerging from hibernate state due polling)
1 parent bc6798d commit 8944fed

1 file changed

Lines changed: 145 additions & 111 deletions

File tree

services/stsafea/stsafea_frame_transfer.c

Lines changed: 145 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handler_t *pSTSE, stse_frame_t *pFr
133133
return (STSE_SERVICE_INVALID_PARAMETER);
134134
}
135135

136-
/* ======================================================= */
137-
/* ============== Get the total frame length ============= */
136+
/* ================================================================================= */
137+
/* ============== Get the total frame length + 2 bytes (potential CRC) ============= */
138138
while ((retry_count != 0) && (ret == STSE_PLATFORM_BUS_ACK_ERROR)) {
139139
/* - Receive frame length from target STSAFE */
140140
ret = pSTSE->io.BusRecvStart(
141141
pSTSE->io.busID,
142142
pSTSE->io.Devaddr,
143143
pSTSE->io.BusSpeed,
144-
STSE_FRAME_LENGTH_SIZE + STSE_RSP_FRAME_HEADER_SIZE);
144+
STSE_FRAME_LENGTH_SIZE + STSE_RSP_FRAME_HEADER_SIZE + STSE_FRAME_CRC_SIZE);
145145

146146
if (ret != STSE_OK) {
147147
retry_count--;
@@ -163,7 +163,7 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handler_t *pSTSE, stse_frame_t *pFr
163163
STSE_RSP_FRAME_HEADER_SIZE);
164164

165165
/* - Get STSAFE Response Length */
166-
ret = pSTSE->io.BusRecvStop(
166+
ret = pSTSE->io.BusRecvContinue(
167167
pSTSE->io.busID,
168168
pSTSE->io.Devaddr,
169169
pSTSE->io.BusSpeed,
@@ -173,6 +173,17 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handler_t *pSTSE, stse_frame_t *pFr
173173
return ret;
174174
}
175175

176+
/* - Get STSAFE Response Potential CRC */
177+
ret = pSTSE->io.BusRecvStop(
178+
pSTSE->io.busID,
179+
pSTSE->io.Devaddr,
180+
pSTSE->io.BusSpeed,
181+
received_crc,
182+
STSE_FRAME_CRC_SIZE);
183+
if (ret != STSE_OK) {
184+
return ret;
185+
}
186+
176187
/* - Store response Length */
177188
received_length = ((length_value[0] << 8) + length_value[1]) - STSE_FRAME_CRC_SIZE + STSE_RSP_FRAME_HEADER_SIZE;
178189

@@ -187,101 +198,136 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handler_t *pSTSE, stse_frame_t *pFr
187198
}
188199
}
189200

190-
/* ======================================================= */
191-
/* ====== Format the frame to handle CRC and filler ====== */
201+
if (received_length == 1) {
202+
/* ====================================================== */
203+
/* ====== compute CRC for response without payload ====== */
192204

193-
/* - Compare STSAFE Received frame length with local RSP Frame length */
194-
if (received_length > pFrame->length) {
195-
/* Calculate needed filler to match both length */
196-
filler_size = received_length - pFrame->length;
197-
}
198-
if (received_length < pFrame->length) {
199-
pFrame->length = received_length;
200-
}
205+
computed_crc = stse_platform_Crc16_Calculate(&received_header, STSE_RSP_FRAME_HEADER_SIZE);
201206

202-
/* Append filler frame element even if its length equal 0 */
203-
PLAT_UI8 filler[filler_size];
204-
stse_frame_element_allocate(eFiller,
205-
filler_size,
206-
filler);
207-
if (filler_size > 0) {
208-
stse_frame_push_element(pFrame,
209-
&eFiller);
210-
}
207+
#ifdef STSE_FRAME_DEBUG_LOG
208+
printf("\n\r STSAFE Frame < (%d-byte) : { 0x%02X } { 0x%02X 0x%02X }\n\r",
209+
received_length + STSE_FRAME_CRC_SIZE,
210+
received_header,
211+
received_crc[0],
212+
received_crc[1]);
213+
#endif /* STSE_FRAME_DEBUG_LOG */
211214

212-
/* ======================================================= */
213-
/* ========= Receive the frame in frame elements ========= */
215+
/* - Verify CRC */
216+
if (computed_crc != ((received_crc[0] << 8) + received_crc[1])) {
217+
return (STSE_SERVICE_FRAME_CRC_ERROR);
218+
}
214219

215-
ret = STSE_PLATFORM_BUS_ACK_ERROR;
216-
while ((retry_count != 0) && (ret == STSE_PLATFORM_BUS_ACK_ERROR)) {
217-
/* - Receive frame length from target STSAFE */
218-
ret = pSTSE->io.BusRecvStart(
219-
pSTSE->io.busID,
220-
pSTSE->io.Devaddr,
221-
pSTSE->io.BusSpeed,
222-
STSE_FRAME_LENGTH_SIZE + received_length + STSE_FRAME_CRC_SIZE);
220+
ret = (stse_ReturnCode_t)(received_header & STSE_STSAFEA_RSP_STATUS_MASK);
221+
} else {
222+
/* ======================================================= */
223+
/* ====== Format the frame to handle CRC and filler ====== */
223224

224-
if (ret != STSE_OK) {
225-
retry_count--;
226-
stse_platform_Delay_ms(STSE_POLLING_RETRY_INTERVAL);
225+
/* - Compare STSAFE Received frame length with local RSP Frame length */
226+
if (received_length > pFrame->length) {
227+
/* Calculate needed filler to match both length */
228+
filler_size = received_length - pFrame->length;
229+
}
230+
if (received_length < pFrame->length) {
231+
pFrame->length = received_length;
227232
}
228-
}
229233

230-
/* - Verify correct reception*/
231-
if (ret != STSE_OK) {
232-
return ret;
233-
}
234+
/* Append filler frame element even if its length equal 0 */
235+
PLAT_UI8 filler[filler_size];
236+
stse_frame_element_allocate(eFiller,
237+
filler_size,
238+
filler);
239+
if (filler_size > 0) {
240+
stse_frame_push_element(pFrame,
241+
&eFiller);
242+
}
234243

235-
/* Receive response header */
236-
ret = pSTSE->io.BusRecvContinue(
237-
pSTSE->io.busID,
238-
pSTSE->io.Devaddr,
239-
pSTSE->io.BusSpeed,
240-
pFrame->first_element->pData,
241-
STSE_RSP_FRAME_HEADER_SIZE);
244+
/* ======================================================= */
245+
/* ========= Receive the frame in frame elements ========= */
246+
247+
ret = STSE_PLATFORM_BUS_ACK_ERROR;
248+
while ((retry_count != 0) && (ret == STSE_PLATFORM_BUS_ACK_ERROR)) {
249+
/* - Receive frame length from target STSAFE */
250+
ret = pSTSE->io.BusRecvStart(
251+
pSTSE->io.busID,
252+
pSTSE->io.Devaddr,
253+
pSTSE->io.BusSpeed,
254+
STSE_FRAME_LENGTH_SIZE + received_length + STSE_FRAME_CRC_SIZE);
255+
256+
if (ret != STSE_OK) {
257+
retry_count--;
258+
stse_platform_Delay_ms(STSE_POLLING_RETRY_INTERVAL);
259+
}
260+
}
242261

243-
if (ret != STSE_OK) {
244-
return ret;
245-
}
262+
/* - Verify correct reception*/
263+
if (ret != STSE_OK) {
264+
return ret;
265+
}
246266

247-
/* Substract response header already read in STSAFE-A */
248-
received_length -= STSE_RSP_FRAME_HEADER_SIZE;
267+
/* Receive response header */
268+
ret = pSTSE->io.BusRecvContinue(
269+
pSTSE->io.busID,
270+
pSTSE->io.Devaddr,
271+
pSTSE->io.BusSpeed,
272+
pFrame->first_element->pData,
273+
STSE_RSP_FRAME_HEADER_SIZE);
249274

250-
/* Receive and discard length (already stored) */
251-
ret = pSTSE->io.BusRecvContinue(
252-
pSTSE->io.busID,
253-
pSTSE->io.Devaddr,
254-
pSTSE->io.BusSpeed,
255-
NULL,
256-
STSE_FRAME_LENGTH_SIZE);
257-
if (ret != STSE_OK) {
258-
return ret;
259-
}
275+
if (ret != STSE_OK) {
276+
return ret;
277+
}
260278

261-
/* - Append CRC element to the RSP Frame (valid only in Receive Scope) */
262-
stse_frame_element_allocate_push(pFrame, eCRC, STSE_FRAME_CRC_SIZE, received_crc);
279+
/* Substract response header already read in STSAFE-A */
280+
received_length -= STSE_RSP_FRAME_HEADER_SIZE;
263281

264-
/* If first element is longer than just the header */
265-
if (pFrame->first_element->length > STSE_RSP_FRAME_HEADER_SIZE) {
266-
/* Receive missing bytes after discarding the 2 bytes length */
282+
/* Receive and discard length (already stored) */
267283
ret = pSTSE->io.BusRecvContinue(
268284
pSTSE->io.busID,
269285
pSTSE->io.Devaddr,
270286
pSTSE->io.BusSpeed,
271-
pFrame->first_element->pData + STSE_RSP_FRAME_HEADER_SIZE,
272-
pFrame->first_element->length - STSE_RSP_FRAME_HEADER_SIZE);
287+
NULL,
288+
STSE_FRAME_LENGTH_SIZE);
273289
if (ret != STSE_OK) {
274290
return ret;
275291
}
276-
}
277292

278-
/* - Perform frame element reception and populate local RSP Frame */
279-
pCurrent_element = pFrame->first_element->next;
280-
while (pCurrent_element != pFrame->last_element) {
281-
if (received_length < pCurrent_element->length) {
282-
pCurrent_element->length = received_length;
293+
/* - Append CRC element to the RSP Frame (valid only in Receive Scope) */
294+
stse_frame_element_allocate_push(pFrame, eCRC, STSE_FRAME_CRC_SIZE, received_crc);
295+
296+
/* If first element is longer than just the header */
297+
if (pFrame->first_element->length > STSE_RSP_FRAME_HEADER_SIZE) {
298+
/* Receive missing bytes after discarding the 2 bytes length */
299+
ret = pSTSE->io.BusRecvContinue(
300+
pSTSE->io.busID,
301+
pSTSE->io.Devaddr,
302+
pSTSE->io.BusSpeed,
303+
pFrame->first_element->pData + STSE_RSP_FRAME_HEADER_SIZE,
304+
pFrame->first_element->length - STSE_RSP_FRAME_HEADER_SIZE);
305+
if (ret != STSE_OK) {
306+
return ret;
307+
}
283308
}
284-
ret = pSTSE->io.BusRecvContinue(
309+
310+
/* - Perform frame element reception and populate local RSP Frame */
311+
pCurrent_element = pFrame->first_element->next;
312+
while (pCurrent_element != pFrame->last_element) {
313+
if (received_length < pCurrent_element->length) {
314+
pCurrent_element->length = received_length;
315+
}
316+
ret = pSTSE->io.BusRecvContinue(
317+
pSTSE->io.busID,
318+
pSTSE->io.Devaddr,
319+
pSTSE->io.BusSpeed,
320+
pCurrent_element->pData,
321+
pCurrent_element->length);
322+
if (ret != STSE_OK) {
323+
return ret;
324+
}
325+
326+
received_length -= pCurrent_element->length;
327+
pCurrent_element = pCurrent_element->next;
328+
}
329+
330+
ret = pSTSE->io.BusRecvStop(
285331
pSTSE->io.busID,
286332
pSTSE->io.Devaddr,
287333
pSTSE->io.BusSpeed,
@@ -291,48 +337,36 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handler_t *pSTSE, stse_frame_t *pFr
291337
return ret;
292338
}
293339

294-
received_length -= pCurrent_element->length;
295-
pCurrent_element = pCurrent_element->next;
296-
}
297-
ret = pSTSE->io.BusRecvStop(
298-
pSTSE->io.busID,
299-
pSTSE->io.Devaddr,
300-
pSTSE->io.BusSpeed,
301-
pCurrent_element->pData,
302-
pCurrent_element->length);
303-
if (ret != STSE_OK) {
304-
return ret;
305-
}
306-
307340
#ifdef STSE_FRAME_DEBUG_LOG
308-
printf("\n\r STSAFE Frame < ");
309-
stse_frame_debug_print(pFrame);
310-
printf("\n\r");
341+
printf("\n\r STSAFE Frame < ");
342+
stse_frame_debug_print(pFrame);
343+
printf("\n\r");
311344
#endif /* STSE_FRAME_DEBUG_LOG */
312345

313-
/* - Swap CRC */
314-
stse_frame_element_swap_byte_order(&eCRC);
346+
/* - Swap CRC */
347+
stse_frame_element_swap_byte_order(&eCRC);
315348

316-
/* - Pop CRC element from Frame*/
317-
stse_frame_pop_element(pFrame);
349+
/* - Pop CRC element from Frame*/
350+
stse_frame_pop_element(pFrame);
318351

319-
/* - Compute CRC */
320-
ret = stse_frame_crc16_compute(pFrame, &computed_crc);
321-
if (ret != STSE_OK) {
322-
return ret;
323-
}
352+
/* - Compute CRC */
353+
ret = stse_frame_crc16_compute(pFrame, &computed_crc);
354+
if (ret != STSE_OK) {
355+
return ret;
356+
}
324357

325-
/* - Pop Filler element from Frame*/
326-
if (filler_size > 0) {
327-
stse_frame_pop_element(pFrame);
328-
}
358+
/* - Pop Filler element from Frame*/
359+
if (filler_size > 0) {
360+
stse_frame_pop_element(pFrame);
361+
}
329362

330-
/* - Verify CRC */
331-
if (computed_crc != *(PLAT_UI16 *)received_crc) {
332-
return (STSE_SERVICE_FRAME_CRC_ERROR);
333-
}
363+
/* - Verify CRC */
364+
if (computed_crc != *(PLAT_UI16 *)received_crc) {
365+
return (STSE_SERVICE_FRAME_CRC_ERROR);
366+
}
334367

335-
ret = (stse_ReturnCode_t)(pFrame->first_element->pData[0] & STSE_STSAFEA_RSP_STATUS_MASK);
368+
ret = (stse_ReturnCode_t)(pFrame->first_element->pData[0] & STSE_STSAFEA_RSP_STATUS_MASK);
369+
}
336370

337371
return ret;
338372
}

0 commit comments

Comments
 (0)