0

我有一个 ATMega328p (MCU) 通过来自ModernDevice.com的 USB 到密封转换器 (BUB) 连接到我的计算机。MCU 通过 TX、RX 和 GND 线连接到 BUB。

为了使用 U(S)ART 发送数据,我使用了这个库,它是基于可以在 Internet 上找到的许多解决方案编写的,并且适合我的需求:

UART.h:

#ifndef __UART_H_
#define __UART_H_

#include <string.h>

#include "AVRBase.h"

#define BAUD 38400
#include <util/setbaud.h>

#define SERIAL_QUEUE_SIZE 10

struct RingBuffer {
  unsigned char achBuffer[SERIAL_BUFFER_SIZE];
  int nHead;
  int nTail;
};

class UART : public AVRBase {
  public: 
    UART();
    ~UART();

    void Init();
    void Stop();

    void StoreRxChar(uint8_t);
    void ReceiveNextByte();
    void SendNextByte();

    int  Available();
    int  Read();
    bool StringIsComplete();
    String GetInputString(); 

    void Flush();
    size_t Write(char);
    size_t SendStr(String);

  private:
    RingBuffer m_sRxBuffer;
    RingBuffer m_sTxBuffer;

    String m_strInputString;
};


#endif

UART.cpp:

#include "UART.h"

UART::UART() {
  memset(m_sRxBuffer.achBuffer, 0, SERIAL_BUFFER_SIZE);
  m_sRxBuffer.nHead = 0;
  m_sRxBuffer.nTail = 0;
  memset(m_sTxBuffer.achBuffer, 0, SERIAL_BUFFER_SIZE);
  m_sRxBuffer.nHead = 0;
  m_sRxBuffer.nTail = 0;
}

UART::~UART() {

}

void UART::Init () {
  cbi(SMCR, PRUSART0);

  // Assyncronous USART
  cbi(UCSR0C, UMSEL01);
  cbi(UCSR0C, UMSEL00);

  // No parity
  cbi(UCSR0C, UPM01);
  cbi(UCSR0C, UPM00);

  // 2 stop bits
  sbi(UCSR0C, USBS0);

  // 8 data bits
  cbi(UCSR0C, UCSZ02);
  sbi(UCSR0C, UCSZ01);
  sbi(UCSR0C, UCSZ00);

  // Clock polarity. Set to zero in assync mode
  cbi(UCSR0C, UCPOL0);

  UBRR0H = UBRRH_VALUE;
  UBRR0L = UBRRL_VALUE;

  // no 2x speed
  cbi(UCSR0A, U2X0);

  sbi(UCSR0B, RXEN0);
  sbi(UCSR0B, TXEN0);

  // Enable receive complete interrupt
  sbi(UCSR0B, RXCIE0);

  // Disable transmit complete interrupt
  cbi(UCSR0B, TXCIE0);

  // Disable date register empty interrupt
  cbi(UCSR0B, UDRIE0);
}

void UART::Stop() {
  while (m_sTxBuffer.nHead != m_sTxBuffer.nTail);

  cbi(UCSR0B, RXEN0);
  cbi(UCSR0B, TXEN0);
  cbi(UCSR0B, RXCIE0);
  cbi(UCSR0B, UDRIE0);

  m_sRxBuffer.nHead = m_sRxBuffer.nTail;
}

void UART::StoreRxChar(uint8_t nC) {
  int nI = (unsigned int) (m_sRxBuffer.nHead + 1) % SERIAL_BUFFER_SIZE;

  if (nI != m_sRxBuffer.nTail) {
    m_sRxBuffer.achBuffer[m_sRxBuffer.nHead] = nC;
    m_sRxBuffer.nHead = nI;
  }
}

void UART::ReceiveNextByte() {
  unsigned char chC  =  UDR0;
  StoreRxChar(chC);
}

void UART::SendNextByte() {
  if (m_sTxBuffer.nHead == m_sTxBuffer.nTail) {
  //Buffer empty, so disable interrupts
    cbi(UCSR0B, UDRIE0);
  }
  else {
    unsigned char chC = m_sTxBuffer.achBuffer[m_sTxBuffer.nTail];
    m_sTxBuffer.nTail = (m_sTxBuffer.nTail + 1) % SERIAL_BUFFER_SIZE;
    UDR0 = chC;
  }
}

int UART::Available() {
  return (unsigned int) (SERIAL_BUFFER_SIZE + m_sRxBuffer.nHead - m_sRxBuffer.nTail) % SERIAL_BUFFER_SIZE;
}

int UART::Read() {
   // if the head isn't ahead of the tail, we don't have any characters
  if (m_sRxBuffer.nHead == m_sRxBuffer.nTail) {
    return -1;
  } 
  else {
    unsigned char chC = m_sRxBuffer.achBuffer[m_sRxBuffer.nTail];

    m_sRxBuffer.nTail = (unsigned int)(m_sRxBuffer.nTail + 1) % SERIAL_BUFFER_SIZE;
    return chC;
  }
}

bool UART::StringIsComplete() {

  while (Available()) {
    // get the new byte:
    char cInChar = (char)Read();
    // add it to the inputString:
    m_strInputString += cInChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (cInChar == '\n') {
      return true;
    }
  }
  return false;
}

String UART::GetInputString() { 
  String strTransfer = m_strInputString;
  m_strInputString = "";
  return strTransfer; 
}

void UART::Flush() {
  while (m_sTxBuffer.nHead != m_sTxBuffer.nTail);
}

size_t UART::Write(char chC) {
  int nI = (m_sTxBuffer.nHead + 1) % SERIAL_BUFFER_SIZE;

  // If the output buffer is full, there's nothing for it other than to 
  // wait for the interrupt handler to empty it a bit
  // ???: return 0 here instead?
  while (nI == m_sTxBuffer.nTail);

  m_sTxBuffer.achBuffer[m_sTxBuffer.nHead] = chC;
  m_sTxBuffer.nHead = nI;

  sbi(UCSR0B, UDRIE0);

  return 1;
}

size_t UART::SendStr(String strMessage) {
  size_t nSize = 0;
  for (uint8_t nI = 0; nI < strMessage.length(); nI++) {
    nSize += Write(strMessage[nI]);
  }
  return nSize;
}

以下类控制 UART:

COM.h:

#ifndef __COM_H_
#define __COM_H_

#include "AVRBase.h"
#include "UART.h"

class COM : public AVRBase {
  public:
    COM();
    ~COM();

    void Run();

    void SendNextByteWrapper();
    void ReceiveNextByteWrapper();

    void SetUserCallback( void (*)(String));
    void Echo(String);

  private:
    UART m_cUART;

    void ( * m_pRunUserCallback ) (String);

    bool m_bLed;
    bool m_bUserCallbackWasSet;
};

#endif

COM.cpp:

#include "COM.h"

COM::COM() {
  m_bUserCallbackWasSet = false;
}

COM::~COM() {
}

void COM::Run() {

  PAD3_IS_OUTPUT;
  m_bLed = true;

  m_cUART.Init();

  while(1) {

    if (m_bLed) {
      PAD3_HIGH;
      m_bLed = false;
    }
    else {
      PAD3_LOW;
      m_bLed = true;
    }

    Delay_ms(100);
  }
}

void COM::SendNextByteWrapper() {
  m_cUART.SendNextByte();
}

void COM::ReceiveNextByteWrapper() {
  m_cUART.ReceiveNextByte();
  if (m_cUART.StringIsComplete()) {
    if (m_bUserCallbackWasSet) {
      m_pRunUserCallback(m_cUART.GetInputString());
    }
  }
}

void COM::SetUserCallback( void (* pRunUserCallback) (String)) { 
  if (pRunUserCallback != NULL) {
    m_pRunUserCallback = pRunUserCallback; 
    m_bUserCallbackWasSet = true;
  }
}

void COM::Echo(String strReceived) {

  // TEST 1
  m_cUART.SendStr(strReceived); // Sends only trash!

  // TEST 2
  m_cUART.Write('S');
  m_cUART.Write('U');
  m_cUART.Write('C');
  m_cUART.Write('C');
  m_cUART.Write('E');
  m_cUART.Write('S');
  m_cUART.Write('S');
  m_cUART.Write('\n');   // Sends SUCCESS

  // TEST 3
  char Test[] = "BLERG\n";

  for (uint8_t nI = 0; nI <= 5; nI++) {
    m_cUART.Write(Test[nI]);   
  }
  // Sends only trash!


  // TEST 4
  for (uint8_t nI = 0; nI <= 5; nI++) {
    m_cUART.Write(Test[0]);   
  }
  // Sends BBBBB
}

在主代码中,以下代码实现了中断处理程序:

COMModule.h:

#include "COM.h"

COM cCOM;

void ManageUART(String);

SIGNAL(USART_RX_vect) {
  cCOM.ReceiveNextByteWrapper();
}

ISR(USART_UDRE_vect) {
  cCOM.SendNextByteWrapper();
}

int main(void) {
  sei();
  cCOM.SetUserCallback(&ManageUART);
  cCOM.Run();
}

void ManageUART(String strInput) {
  cCOM.Echo(strInput);
}

所以,在所有这些介绍之后,问题是当我连接到 MCU 并发送一个字节时,当执行 TEST 2 和 TEST 4 时(方法 Echo,在 COM.cpp 上),我只能在终端上得到一些 ASCII 答案。每次我在循环内访问 char 数组或字符串数​​组以发送数组字符时,我都会在桌面终端中得到垃圾。

你有什么可能是错的猜测?


编辑:这是另一个线索:

void COM::Echo(String strReceived) {

  char Test[] = "BLERG\n";

  m_cUART.Write(Test[0]);
  Delay_ms(100);
  m_cUART.Write(Test[1]);
  m_cUART.Write(Test[2]);
  m_cUART.Write(Test[3]);
  m_cUART.Write(Test[4]);
  m_cUART.Write(Test[5]);

  // The above code works. The terminal receives BLERG.

  uint8_t nI=0;
  uint8_t nA=3;
  while (nI <= 5) {
    m_cUART.Write(Test[nA]);
    nI++;
  }
  // The above code works too... The terminal receives RRRRRR.

  for (uint8_t nJ = 0; nJ <= 5; nJ++) {
    m_cUART.Write(Test[nJ]);   
    //Delay_ms(1);
  }
  // The above code sends only trash: ÿÿÿÿÿÿ
}

编辑2:还有一个事实。我真的不明白为什么会这样:

void COM::Echo(String strReceived) {

  char Test[] = "BLERG\n";

  m_cUART.Write(Test[0]);
  Delay_ms(100);
  m_cUART.Write(Test[1]);
  m_cUART.Write(Test[2]);
  m_cUART.Write(Test[3]);
  m_cUART.Write(Test[4]);
  m_cUART.Write(Test[5]);

  // The above code works. The terminal receives BLERG.

  uint8_t nI=0;
  uint8_t nA=3;
  while (nI <= 5) {
    m_cUART.Write(Test[nA]);
    nI++;
  }
  // The above code works too... The terminal receives RRRRRR.

 nA=0;
 m_cUART.Write(Test[nA]);  // MCU LOCKS HERE!

  for (uint8_t nJ = 0; nJ <= 5; nJ++) {
    m_cUART.Write(Test[nJ]);   
    //Delay_ms(1);
  }
  // The above code sends only trash: ÿÿÿÿÿÿ
}
4

1 回答 1

1

您的代码中似乎有竞争条件。m_sTxBuffer.achBuffer 和 m_sTxBuffer.nHead 字段可从主线程和 USART_UDRE_vect 中断处理程序访问。

有几种方法可以处理这个问题。您可以推出自己的解决方案:全局禁用所有中断(sei/cli),将 nHead 和 achBuffer 字段标记为 volatile 并使用编译器内存屏障来防止重新排序

或者,您可以使用avr-libc 中的 atomic.h

于 2016-04-03T19:49:13.673 回答