AnsweredAssumed Answered

stm32f4 USART2 problem

Question asked by ali.med on Jun 4, 2015
Latest reply on Jun 5, 2015 by ali.med
I am trying to use pb8 and pb9 with stm32f4 's usart 2 peripheral.I first used usart 2 with pb6 and pb7.It worked great but then i was obliged to change them. to pb8 and pb9.
Here a quick look at my code.















#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "misc.h"
#include <string.h>
#include <stdlib.h>
/* Private variables ---------------------------------------------------------*/
//UART connection
void UARTSend(const unsigned char * pucBuffer, unsigned long ulCount);
volatile char received_string[20 + 1]; // this will hold the recieved string
 
int main(void) {
 
    unsigned char welcome_str[] = "xxyyzz";
    u8 loop = 1;
    char X[8] = "aaaaaa";
    char Y[8] = "aaaaaa";
    int length = 0;
 
    int i = 2;
    int findY = 0;
    int y = 0;
 
    init_USART2(9600);
 
 
    while (loop) {
                if (Full == 1) {
                    ExtracTData(received_string);
        }
 
    }
    //UARTSend(welcome_str, sizeof(welcome_str));
    USART_Cmd(USART2, DISABLE);
}
 
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount) {
    //
    // Loop while there are more characters to send.
    //
    while (ulCount--) {
        while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) {
        }
 
        USART_SendData(USART2, (uint8_t) * pucBuffer++);
        /* Loop until the end of transmission */
 
    }
}
void init_USART2(uint32_t baudrate) {
 
    GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
    USART_InitTypeDef USART_InitStruct; // this is for the USART2 initilization
    NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
    RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // Pins 6 (TX) and 7 (RX) are used
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate!
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain)
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this activates the pullup resistors on the IO pins
 
    GPIO_Init(GPIOA, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers
 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
 
    USART_InitStruct.USART_BaudRate = 9600;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART2, &USART_InitStruct);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
 
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // we want to configure the USART2 interrupts
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the priority group of the USART2 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 USART2_IRQHandler(void) {
 
    // check if the USART2 receive interrupt flag was set
    if (USART_GetITStatus(USART2, USART_IT_RXNE)) {
 
        static uint8_t cnt = 0; // this counter is used to determine the string length
        char t = USART2->DR; // the character from the USART2 data register is saved in t
 
        /* check if the received character is not the LF character (used to determine end of string)
         * or the if the maximum string length has been been reached
         */
        if ((t != '\n') && (cnt < 20)) {
            received_string[cnt] = t;
            cnt++;
 
        } else { // otherwise reset the character counter
            cnt = 0;
            Full = 1;
        }
    }
}






Outcomes