2012-12-28 13 views
5

Ich versuche, eine USART-Kommunikation zu erreichen. Also verbinde ich den RX meines STM32f1 mit seinem TX. Auch ich schreibe ein Programm für diese Kommunikation. Dieser Code besteht aus den folgenden Komponenten zusammen:USART Kommunikation mit einem STM32f1xx

  1. RCC Konfiguration
  2. GPIO Konfiguration
  3. USART Konfiguration
  4. Senden und Empfangen eines Strings
  5. Vergleich zwischen der gesendeten Zeichenfolge und der empfangenen Zeichenfolge
  6. Prüfen, ob die Kommunikation erfolgreich war => LED4 leuchtet sonst die LED3 wird eingeschaltet

Das Problem ist in allen Fällen die LED3 leuchtet auf. Dies bedeutet, dass die Datenübertragung fehlgeschlagen ist.

Mit meiner IDE (IAR Embedded Workbench) ich dieses Programm Code kompilieren:

/* Includes ------------------------------------------------------------------*/ 
#include "stm32f10x.h" 
#include "stm32_eval.h" 


/* Private typedef -----------------------------------------------------------*/ 
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus; 

/* Private define ------------------------------------------------------------*/ 
#define USARTy     USART1 
#define USARTy_GPIO    GPIOA /* PORT name*/ 
#define USARTy_CLK    RCC_APB2Periph_USART1 
#define USARTy_GPIO_CLK   RCC_APB2Periph_GPIOA 
#define USARTy_RxPin    GPIO_Pin_10/* pin Rx name*/ 
#define USARTy_TxPin    GPIO_Pin_9 /* pin Tx name*/ 

#define USARTz     USART2 
#define USARTz_GPIO    GPIOA/* PORT name*/ 
#define USARTz_CLK    RCC_APB1Periph_USART2 
#define USARTz_GPIO_CLK   RCC_APB2Periph_GPIOA 
#define USARTz_RxPin    GPIO_Pin_3/* pin Rx name*/ 
#define USARTz_TxPin    GPIO_Pin_2/* pin Tx name*/ 

#define TxBufferSize (countof(TxBuffer)) 

/* Private macro -------------------------------------------------------------*/ 
#define countof(a) (sizeof(a)/sizeof(*(a))) 

/* Private variables ---------------------------------------------------------*/ 
USART_InitTypeDef USART_InitStructure; 
uint8_t TxBuffer[] = "Bufferrr"; 
uint8_t RxBuffer[8]; 
__IO uint8_t TxConteur = 0, RxConteur = 0; 
volatile TestStatus TransferStatus = FAILED; 

/* Private function prototypes -----------------------------------------------*/ 
void RCC_Configuration(void); 
void GPIO_Configuration(void); 
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 
__IO uint8_t index = 0; 

GPIO_InitTypeDef GPIO_InitStructure; 

int main(void) 
{ 
    STM_EVAL_LEDInit(LED1); 
    STM_EVAL_LEDInit(LED2); 
    STM_EVAL_LEDInit(LED3); 
    STM_EVAL_LEDInit(LED4); 

/* System Clocks Configuration */ 
RCC_Configuration(); 
/* Configure the GPIO ports */ 
GPIO_Configuration(); 

USART_InitStructure.USART_BaudRate = 230400 /*115200*/; 
    USART_InitStructure.USART_WordLength =USART_WordLength_8b ; 
    USART_InitStructure.USART_StopBits = USART_StopBits_1; 
    USART_InitStructure.USART_Parity = USART_Parity_No; 
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 

    /* Configure USARTy */ 
    USART_Init(USART1,&USART_InitStructure); 

    /* Enable the USARTy */ 
    USART_Cmd(USART1,ENABLE); 

    while(TxConteur < TxBufferSize) 
    { 
    /* Send one byte from USARTy to USARTz */ 
    USART_SendData(USARTy, TxBuffer[TxConteur++]); 
    /* Loop until USARTy DR register is empty */ 
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) 
    { 
    } 
    /* Store the received byte in RxBuffer */ 
    RxBuffer[RxConteur++] = USART_ReceiveData(USARTy) & 0xFF; 
    } 

    /* Check the received data with the send ones */ 
    TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize); 
    /* TransferStatus = FAILED, if the data transmitted from USARTy and 
    received by USARTz are different */ 
    if (TransferStatus == FAILED) 
    { 
     STM_EVAL_LEDOn(LED3); 

    } 
    else 
    { 

    STM_EVAL_LEDOn(LED4); 
    } 

    while (1) 
    { 
    } 
} 

void RCC_Configuration(void) 
{  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); 

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); 
} 

void GPIO_Configuration(void) 
{ 
    GPIO_InitTypeDef GPIO_InitStructure1,GPIO_InitStructure2; 

    /* Configure USARTy Rx as input floating */ 
    GPIO_InitStructure1.GPIO_Pin =GPIO_Pin_10; 
    GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
    GPIO_Init(GPIOA, &GPIO_InitStructure1); 

    /* Configure USARTy Tx as alternate function push-pull */ 
    GPIO_InitStructure2.GPIO_Pin =GPIO_Pin_9; 
    GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_Init(GPIOA, &GPIO_InitStructure2); 
    /* Configure USARTz Tx as alternate function push-pull */ 


} 


TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 
{ 
    while(BufferLength--) 
    { 
    if(*pBuffer1 != *pBuffer2) 
    { 
     return FAILED; 
    } 

    pBuffer1++; 
    pBuffer2++; 
    } 

    return PASSED; 
} 
+0

Subminiatur stilistische Nebenbemerkung: Sie nennen besser nicht Ihre Typdefinitionen 'TypeDef', verwenden' Context' oder etwas ähnliches. –

+1

Testen von USART_FLAG_TC ist eine schlechte Idee, die nur testet, wenn das Byte übertragen wurde. Sie müssen testen, ob stattdessen ein Byte empfangen wurde. Verwenden Sie USART_FLAG_RXNE –

+1

@ H2CO3 Die STM "Standard-Bibliotheken" verwenden diese Benennung, es ist ... lästig, aber schwer zu wiederholen. – unwind

Antwort

3

Wie in einem Kommentar von Hans Passant erklärte, testet die OP, die die Flagge USART_FLAG_TC (Übertragung beendet) anstelle der Flagge USART_FLAG_RXNE (RX-Puffer nicht leer).

Verwandte Themen