AnsweredAssumed Answered

USART2 TX Problem stm32f4

Question asked by pina.javier on May 26, 2014
Latest reply on May 26, 2014 by Clive One
Hello, I am having a problem with my STM32F4 USART2. the problem is throwing me this ******** and can not find where the error may be. 

#include "stm32f4xx.h"                  // Device header
#include "stm32f4xx_gpio.h"
#include <misc.h>                // I recommend you have a look at these in the ST firmware folder
#include "stm32f4xx_usart.h"
#include "stm32f4xx_rcc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int flag = 0;


void init(USART_TypeDef* USARTx, uint32_t baudrate) {
       
          GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
          NVIC_InitTypeDef NVIC_InitStructure;       


          //General settings of pins TX/RX
          GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
          GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
          GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;


     if(USARTx == USART1){            
          
               RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins B6 (TX) and B7 (RX)
               GPIO_Init(GPIOB, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
               GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);


               NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
     }
     else if(USARTx == USART2){
          flag =1;
          
               RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // Pins A2 (TX) and A3 (RX)
               GPIO_Init(GPIOA, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
               GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);


               NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
     }
     else if(USARTx == USART3){     
          
               RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // Pins D8 (TX) and D9 (RX)
               GPIO_Init(GPIOD, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
               GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);


               NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
     }
     else if(USARTx == UART4){          
          
               RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // Pins C10 (TX) and C11 (RX)
               GPIO_Init(GPIOC, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
               GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);


               NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
     }
     else if(USARTx == UART5){
          
               RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; // Pin C12 (TX)
               GPIO_Init(GPIOC, &GPIO_InitStruct);
               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; // Pin D2 (RX)
               GPIO_Init(GPIOD, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
               GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);


               NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
     }
     else if(USARTx == USART6){
          
               RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);


               GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins C6 (TX) and C7 (RX)
               GPIO_Init(GPIOC, &GPIO_InitStruct);


               GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
               GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);


               NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
          }


          //UART setting
          USART_InitStruct.USART_BaudRate = baudrate;
          USART_InitStruct.USART_WordLength = USART_WordLength_8b; // octet comme taille élémentaore (standard)
          USART_InitStruct.USART_StopBits = USART_StopBits_1; // bit de stop = 1 (standard)
          USART_InitStruct.USART_Parity = USART_Parity_No; // pas de bit de parité (standard)
          USART_InitStruct.USART_HardwareFlowControl =     USART_HardwareFlowControl_None; // pas de controle de flux (standard)
          USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
          USART_Init(USARTx, &USART_InitStruct);


          USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);


          //Setting of interrupt
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
          NVIC_Init(&NVIC_InitStructure);


          //Enable UART
          USART_Cmd(USARTx, ENABLE);
     }


void send_char(USART_TypeDef* USARTx, unsigned char c) {
          USART_SendData(USARTx, c);
          while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET) {
          }
     }


void Delay(__IO uint32_t nCount)
{
  while(nCount--) {}
}


void LED()
     {          
    GPIO_InitTypeDef     GPIO_InitStructure; 
       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
          GPIO_Init(GPIOD, &GPIO_InitStructure);
}
int main()
{  
      init(USART2,9600);
      Delay(0x5FFFFF);
   LED();     
      send_char(USART2, 'A');
      Delay(0x5FFFFF);
      send_char(USART2, 'B');
      Delay(0x5FFFFF);
      send_char(USART2, 'C');
   
  while(1)
     {
          if (flag ==1){
          GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
       Delay(0x5FFFFF);
    GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
          Delay(0x5FFFFF);
          }
                     
     }
}
regards

Outcomes