2014-05-20 1 views
2

ARM 마이크로 컨트롤러에서 초보자이므로 Arduino와 통신하기 위해 STM32F3 Discovery Kit을 사용하고 있습니다. 필자는 EWARM을 코딩 용으로 사용하고 퍼티는 직렬 터미널 용으로 사용합니다.STM32F3 UART 수신 인터럽트 데이터가 Arduino의 첫 번째 데이터 수신 후 응답하지 않는다

stm32f3 UART2 에코 테스트를 수행했습니다. 데이터가 직렬 터미널에 올바르게 표시되므로 uart 통신이 예상대로 작동하는지 확인했습니다.

그런 다음 arduino에서 stm32f3으로 데이터를 전송하려고 시도하지만 stm32f3의 uart가 첫 번째 데이터 집합 (데이터의 첫 번째 집합은 일부 가비지 문자가 포함 된 상태로 올바르게 표시됨) 이후에 중단됩니다. 나는 꽤 오랫동안 붙어 있었다.

다음은 UART 용으로 사용한 코드입니다. 전송 속도는 9600입니다. 누구든지 도울 수 있습니까?

#define LINEMAX 15 // Maximal allowed/expected line length 

volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL 
volatile int line_valid = 0; 

/** 
    * @brief Initialize USART2 for PD6 (USART2_RX) and PD5 (USART2_TX) 
    *   used for receiving mouse data from arduino 
    * @param baudrate; by default is 9600 
    * @retval None 
    * link: http://eliaselectronics.com/stm32f4-usart-example-with-interrupt/ 
    */ 
void init_USART2(int baudrate) 
{ 
     USART_InitTypeDef USART_InitStructure; // this is for the GPIO pins used as TX and R 
     GPIO_InitTypeDef GPIO_InitStructure;  // this is for the USART1 initilization 
     NVIC_InitTypeDef NVIC_InitStructure;  // this is used to configure the NVIC (nested vector interrupt controller) 

     /* Configure USART1 pins: Rx and Tx ----------------------------*/ 
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); 
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; 
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
     GPIO_Init(GPIOD, &GPIO_InitStructure); 

     GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7); 
     GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7); 

     /* Configure USART1 pins: --------------------------------------*/ 
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 

     USART_DeInit(USART2); 
     USART_InitStructure.USART_BaudRate = baudrate; 
     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; 
     USART_Init(USART2,&USART_InitStructure); 

     USART_Cmd(USART2, ENABLE); 

     /* Here the USART2 receive interrupt is enabled 
     * and the interrupt controller is configured 
     * to jump to the USART2_IRQHandler() function 
     * if the USART2 receive interrupt occurs 
     */ 
     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt 

     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;    // we want to configure the USART1 interrupts 
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   // this sets the priority group of the USART1 interrupts 
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  // this sets the subpriority inside the group 
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   // the USART2 interrupts are globally enabled 
     NVIC_Init(&NVIC_InitStructure);      // the properties are passed to the NVIC_Init function which takes care of the low level stuff 

     // finally this enables the complete USART2 peripheral 
     USART_Cmd(USART2, ENABLE); 
} 

void serial_prints (USART_TypeDef* USARTx, volatile char *buffer) 
{ 
    /* transmit till NULL character is encountered */ 
    while(*buffer) 
    { 
     USART_SendData(USARTx, *buffer++); 
     while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); 
     delay_us(5); 
    } 
} 

void USART2_IRQHandler(void) 
{ 
    static char rx_buffer[LINEMAX]; // Local holding buffer to build line 
    static int rx_index = 0; 

    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received character? 
    { 
    char rx = USART_ReceiveData(USART2); 

    if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice? 
    { 
     if (rx_index != 0) // Line has some content? 
     { 
      memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer 
      line_buffer[rx_index] = 0; // Add terminating NUL 
      line_valid = 1; // flag new line valid for processing 
      serial_prints(USART2, rx_buffer);  
      rx_index = 0; // Reset content pointer 
     } 
    } 
    else 
    { 
     if (rx_index == LINEMAX) // If overflows pull back to start 
      rx_index = 0; 

      rx_buffer[rx_index++] = rx; // Copy to buffer and increment 
    } 
    } 
} 

는 아두 이노를 들어, 다음 코드를 사용하여 데이터를 시리얼 터미널에 올바르게 표시됩니다

printf("%d, %d \n", X, Y); 

답변

2

당신은 line_buffer에 널 종료 문자를 추가 할 수 있지만 당신은 함수에 rx_buffer 전달 serial_prints는 :

line_buffer[rx_index] = 0; // Add terminating NUL 
line_valid = 1; // flag new line valid for processing 
serial_prints(USART2, rx_buffer);  
rx_index = 0; // Reset content pointer 

이것은 당신이의 길이 과거를 반복 할 결과를 가지고 기능에서210 serial_printsbuffer 때문에 널 (null) 종료되지 않습니다

사이드 노드에서
while(*buffer) 
{ 
    USART_SendData(USARTx, *buffer++); 
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); 
    delay_us(5); 
} 

, 당신은 또한 당신의 문자 (TX 빈 인터럽트)를 보내 인터럽트를 사용해야합니다. UART 문자가 전송되도록 인터럽트 서비스 루틴에서 적극적으로 대기하면 프로그램 속도가 상당히 느려집니다.