2024-04-23 01:01 PM
Hi, I bought me some mini mp3/wav players frome china. Serial communication is executed at a fixed baudrate of 9600baud. Messages to and from the unit do both have a size of 10 bytes. Transmitting serial messages works perfect. Receiving messages do not work. I use the UART1 RX interrupt vector 18 which is executed if the receive data byte buffer (UART_DR) is not-empty. I put a PC3 pin-toggle in the receive interrupt handler after the data (UART_DR) register is read. On my logic analyser I see that the pin toggle takes more time per received byte. I measure the time between the end of the first received STOP bit and the PC3 toggle being 354us which to my opinion is awfully long... With each received byte the time between de end of the STOP bit and a PC3 toggle increases. At byte 6 it is 870us and then PC3 starts oscillating (9us) as if the data (UART_DR) register is not read anymore and contains an unread value. I disabled all other interrupts. What could be going on?
2024-04-23 06:56 PM - edited 2024-04-24 12:36 PM
Show code, and plots.
Might oscillate if other errors are flagging and not cleared. Perhaps OR, NF, FE, PE ?
2024-04-24 12:23 PM
Hi, you are right at the OR/NF/FE/PE oscillation case as with byte 7 an OR is reported but the SR is not read to clear the OR. But still it does not explain why it takes that long before the interrupt handler is called. In the interrupt handler I disable all interrupts and re-enable the RIEN. I finally found the cause. I need 2 UARTs so I implemented a soft-uart which uses for-loop delays during reception and this soft-uart was running during uart1 reception. All works fine now and latency (byte received versus interrupt handler called) is about 9us! Thanks for your comment.
2024-04-24 12:50 PM
Not sure. You have any other higher priority interrupts firing? Something with a multi KHz rate, like a TIM?
What does the assembler code look like for the handler, buffering, processing, etc.
Code optimized? MCU running at expected frequency?
2024-04-24 01:25 PM
Hi, as I posted my response I was aware that I miss something. As I disable all interrupts in the UART1 interrupt handler and only enable RIEN (just for debugging) no other interrupt should intervene. Although my timer1 is clocked at 10KHz (prescaler=1600 with internal 16MHz oscillator) with a reload value of 100 generating a 100Hz signal because I use it both for PWM generation and for software timers update. A prescaler 0f 16.000 would be nice for software timers update but then there is no resolution for PWM valus. But as all interrupts are disabled this should have no effect. Otherwise I studied interrupt priority but I'm still confused how to interpred it. I did many experiments: connecting a square wavte to 2 inputs and configure their interrupt priorities and see what interrupt handler is handled first. As I understand: level0 = main() = lowest priority (I1/I0 = 1/0), then comes level 1 (0/1), level 2 (0/0) and level 3 (1/1). A higher number means a higher priority and a higher priority interupt will interrupt a lower priority interrupt) Level 2 (0/0) can't be set and a previous level set will stay active. I set uart-rx priority to 3 so I would expect it not to be influenced bij some main loop. A long story but maybe you can help me out!
2024-04-24 01:44 PM
Not clear to me how the latency to arrival in the interrupt handler would be affected by you disabling other interrupts once you've already got there.
Look at the instructions and count cycles. Perhaps someone with a gate-level understanding of the STM8 can shed some light on how the UART moves received data into the holding registers and then interrupts?
https://blog.mark-stevens.co.uk/2012/09/interrupts-on-the-stm8s/
Sorry, I mostly moved beyond 8-bit MCU's as a teenager..
I'd strongly advise moving beyond STM8, 8051, et al and toward the Cortex-M or RISC-V parts of today.
2024-04-25 11:39 AM
Hi, I have been programming different types of 8051 uC's and Pic's and Atmels for tens of years and last 15 years daily on stm32 cortex M0 so I know that stuff, I just discovered this cute tiny little STM8S103 and was wondering what I could do with it, amazing! My soft-uart disables all interrupts during transmit and receive otherwise it could be disturbed, therefore my hard-uart will not respond in any way when the soft-uart is active!!! So I now am busy to create some control mechanism where only one uart at a time can be active. To me my problem is solved. Thanks for your support.