Why the program go to UART_WaitOnFlagUntilTimeout after the first simpleserial_get() is finished?

Hi,
I built an experimental program based the “simpleserial-base”, and now I meet a problem that I can get the right return value after every reset, and later, the target won’t reponse after I send an instruction to it.
Here is my code,

uint8_t get_pt(uint8_t* pt, uint8_t len)
#endif

{
	trigger_high();

	static uint8_t sk[KYBER_INDCPA_SECRETKEYBYTES], pk[KYBER_INDCPA_PUBLICKEYBYTES];
	PQCLEAN_KYBER512_CLEAN_indcpa_keypair(sk, pk, pt);

	trigger_low();

	simpleserial_put('r', 16, pk);
	return 0x00;
}

int main(void)
{
    platform_init();
	init_uart();
	trigger_setup();

 	/* Uncomment this to get a HELLO message for debug  */
	 
	putch('h');
	putch('e');
	putch('l');
	putch('l');
	putch('o');
	putch('\n');

	simpleserial_init();
	simpleserial_addcmd('p', 32, get_pt);
#if SS_VER != SS_VER_2_0
	simpleserial_addcmd('k', 16, get_key);
	simpleserial_addcmd('x', 0, reset);
#endif
	while(1)
		simpleserial_get();
}

I used a ST-link to debug it just now, and I found, the program will run correctly during the first “simpleserial_get()”, and when it exit from this method, it won’t run next “simpleserial_get()” as the “while(1)” loop, it went to

hardware/victims/firmware/hal/stm32f3/stm32f3_hal_lowlevel.c:1044

and stuck at the method “UART_WaitOnFlagUntilTimeout” like

I don’t understand how this happens, can you help me?

Thanks a lot!

Hi,

We’ve seen a similar issue on the CWNano, where the devices gets stuck on reads. You can try implementing the fix for the STM32F3 as well:

char getch(void)
{
	uint8_t d;
	while(HAL_UART_Receive(&UartHandle, &d, 1, 50) != HAL_OK)
		USART1->ICR |= (1 << 3); // make sure overrun error is cleared, otherwise can stall here
	return d;
}

Alex

Hi Alex,

Sorry to bother you again!

I don’t understand that well, do you mean I need to fix it by myself or I can use this new “getch()” to fix it?

For the latter, I found that it had been

// stm32f3_hal.c
char getch(void)
{
  uint8_t d;
  while (HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK)
    USART1->ICR |= (1 << 3);
  //putch(d);
  return d;
}

For the former,
I have searched the key word “overrun error” on Google, and I know what causes a USART overrun error. But what puzzle me is I just send 16 bytes data as usual, it shouldn’t act like here. So, maybe there are another error ?

Thanks!
Luffy