简体   繁体   English

为什么使用中断接收时stm32f4 uart会跳过某些字符

[英]Why does stm32f4 uart skip some characters when receiving using an interrupt

Having some problems trying to receive characters from a serial Putty Session. 尝试从串行Putty会话接收字符时遇到一些问题。 When I type sentences/multiple characters into Putty to send character to the stm32f4 microcontroller, it doesn't seem to receive all of the characters using an interrupt approach. 当我在Putty中键入句子/多个字符以将字符发送到stm32f4微控制器时,似乎没有使用中断方法接收所有字符。

So two questions: 有两个问题:

  1. Not sure what is going on here. 不知道这是怎么回事。 Am I missing something? 我想念什么吗?
  2. I am a little confused when an interrupt is called for receiving characters. 当调用中断来接收字符时,我有些困惑。 Is the interrupt called for each character or do you process the whole string with one interrupt call? 是为每个字符调用了中断,还是只通过一个中断调用来处理整个字符串?

Code: 码:

void USART_Write(USART_TypeDef * USARTx, char * str)  {                         

  while(*str){                                                                  
    // First check if the Transmission Data register is empty                   
    while ( !(USARTx->SR & USART_SR_TXE)){};                                    

    // Write Data to the register which will then get shifted out               
    USARTx->DR =(*str &(uint16_t) 0x01FF);                                      

    str++;                                                                      
  }                                                                             
  // Wait until transmission is complete                                        
  while ( !(USARTx->SR & USART_SR_TC)){};                                       

}                              

void receive(USART_TypeDef * USARTx, volatile  char *buffer,
                volatile uint32_t * pCounter){

  if(USARTx->SR & USART_SR_RXNE){                                               
    char c = USARTx->DR;                                                        
    USART_Write(USARTx,&c);                                                     
    USART_Write(USARTx,"\n\r");                                                 
  }                                                                             



}                                                                               

void USART2_IRQHandler(void){                                                   
  receive(USART2, USART2_Buffer_Rx,&Rx2_Counter);                             
}  

Putty Session: 油灰会议:

(I type asdfghjkl into Putty and print out the receiving characters using the USART_WRITE(...) function) (我将asdfghjkl输入Putty,并使用USART_WRITE(...)函数打印出接收字符)

asdfghjkl
a
s
g
k
USART_Write(USARTx,&c);
...........
while(*str)...
  • NO! 没有! Single chars are not NUL-terminated char arrays! 单个字符不是NUL终止的字符数组!

You must not use a polled tx approach directly from an rx interrupt-handler. 您不得直接通过rx中断处理程序使用轮询的tx方法。 It will likely result in rx overrun as the rx buffer gets overwritten by newly received chars while the rx interrupt-handler is stuck polling the tx registers. 当RX中断处理程序卡住轮询TX寄存器时,RX缓冲区将被新接收的字符覆盖,这有可能导致RX超限。

You need a tx interrupt and some kind of buffer, eg. 您需要一个TX中断和某种缓冲区,例如。 a circular char queue. 循环字符队列。

Yes, I know it's a bit of a pain handling tx interrupts, circular buffers etc. If the tx interrupt finds no more chars to send it has to exit having sent none. 是的,我知道处理TX中断,循环缓冲区等有些麻烦。如果TX中断找不到更多要发送的字符,则必须退出而没有发送任何字符。 This means that when the rx interrupt next needs to queue up char to send, it must load it into the tx register instead of the queue in order to 'prime' the tx interrupt mechanism. 这意味着当下一个RX中断需要排队发送char时,它必须将其加载到tx寄存器而不是队列中,以便“启动” tx中断机制。

Things get even more umm 'interesting' if the chars need to traverse waiting non-interrupt thread/s and/or need to be blocked up into protocol units. 如果字符需要遍历等待的非中断线程和/或需要阻塞为协议单元,则事情变得更加“有趣”。

Anyway, whatever, you must not use this mixed interrupt/polling. 无论如何,您不得使用此混合中断/轮询。 It will not work reliably and the extra delays will adversely affect other interrupts, especially lower-priority interrupts, that will remain disabled for long periods:( 它不会可靠地工作,并且额外的延迟会严重影响其他中断,尤其是优先级较低的中断,这些中断将长时间保持禁用状态:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM