AnsweredAssumed Answered

USART not getting enabled

Question asked by male.hakim on Sep 24, 2016
Latest reply on Sep 24, 2016 by Clive One
Am trying to run USART by getting to write in the E-Terminal of CodeLite to interract with my STM32F407. However, i get no response when i the main method is called....here is my main method....

01.#include "inc/stm32/stm32f4xx.h"
02.#include "inc/stm32/stm32f4xx_gpio.h"
03.#include "inc/stm32/stm32f4xx_usart.h"
04.#include "inc/stm32/stm32f4xx_rcc.h"
05.#include "inc/stm32/misc.h"
06.#include <inc/uart.h>
07. 
08.int main(int argc, char **argv)
09.{
10.    SystemCoreClockUpdate();
11.     
12.    UART2_Init(1382400);
13.     
14.    UART_SendStr("SD init: ");
15.     
16.    return 0;
17.}

and here is my header uart.h file

01.// Define to prevent recursive inclusion -------------------------------------
02.#ifndef __UART_H
03.#define __UART_H
04. 
05.#include "inc/stm32/stm32f4xx_rcc.h"
06. 
07.#define UART_PORT         USART2
08.#define UART_TX_PIN       GPIO_Pin_5     // PD2 (USART2_TX)
09.#define UART_RX_PIN       GPIO_Pin_6     // PD3 (USART2_RX)
10.#define UART_GPIO_PORT    GPIOD
11.#define UART_PORT_PERIPH  RCC_AHB1Periph_GPIOD
12. 
13.#define HEX_CHARS         "0123456789ABCDEF"
14. 
15. 
16.// Function prototypes
17.void UART2_Init(uint32_t baudrate);
18. 
19.void UART_SendChar(char ch);
20. 
21.void UART_SendInt(int32_t num);
22.void UART_SendInt0(int32_t num);
23.void UART_SendHex8(uint16_t num);
24.void UART_SendHex16(uint16_t num);
25.void UART_SendHex32(uint32_t num);
26. 
27.void UART_SendStr(char *str);
28. 
29.#endif // __UART_H

and here is my uart file where i do the method description

01.#include "inc/stm32/stm32f4xx_rcc.h"
02.#include "inc/stm32/stm32f4xx_gpio.h"
03.#include "inc/stm32/stm32f4xx_spi.h"
04.#include "inc/stm32/stm32f4xx_usart.h"
05. 
06.#include <inc/uart.h>
07.#include <string.h>
08. 
09. 
10.// Initialize and configure UART peripheral with specified baudrate
11.// input:
12.//   baudrate - UART speed (bits/s)
13.void UART2_Init(uint32_t baudrate) {
14.    GPIO_InitTypeDef PORT;
15.    USART_InitTypeDef UART;
16. 
17.    // UART peripheral clock enable
18.    RCC_AHB1PeriphClockCmd(UART_PORT_PERIPH,ENABLE);
19.    RCC_APB1PeriphClockCmd((RCC_APB1Periph_USART2),ENABLE);
20. 
21.    // Alternative functions of GPIO pins
22.    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5 | GPIO_PinSource6, GPIO_AF_USART2);
23. 
24.    // TX pin
25.    PORT.GPIO_Mode  = GPIO_Mode_AF;
26.    PORT.GPIO_Speed = GPIO_Speed_50MHz;
27.    PORT.GPIO_OType = GPIO_OType_PP;
28.    PORT.GPIO_PuPd  = GPIO_PuPd_UP;
29.    PORT.GPIO_Pin   = UART_TX_PIN;
30.    GPIO_Init(UART_GPIO_PORT,&PORT);
31.    // RX pin
32.    PORT.GPIO_Pin   = UART_RX_PIN;
33.    GPIO_Init(UART_GPIO_PORT,&PORT);
34. 
35.    // UART port
36.    UART.USART_BaudRate             = baudrate;
37.    UART.USART_HardwareFlowControl  = USART_HardwareFlowControl_None;   // No flow control
38.    UART.USART_Mode                 = USART_Mode_Rx | USART_Mode_Tx;    // RX+TX mode
39.    UART.USART_WordLength           = USART_WordLength_8b;              // 8-bit frame
40.    UART.USART_Parity               = USART_Parity_No;                  // No parity check
41.    UART.USART_StopBits             = USART_StopBits_1;                 // 1 stop bit
42.    USART_Init(UART_PORT,&UART);
43.    USART_Cmd(UART_PORT,ENABLE);
44.}
45. 
46.void UART_SendChar(char ch) {
47.    while (!(UART_PORT->SR & USART_SR_TC)); // wait for "Transmission Complete" flag cleared
48.    UART_PORT->DR = ch;
49.}
50. 
51.void UART_SendInt(int32_t num) {
52.    char str[10]; // 10 chars max for INT32_MAX
53.    int i = 0;
54.    if (num < 0) {
55.        UART_SendChar('-');
56.        num *= -1;
57.    }
58.    do str[i++] = num % 10 + '0'; while ((num /= 10) > 0);
59.    for (i--; i >= 0; i--) UART_SendChar(str[i]);
60.}
61. 
62.void UART_SendInt0(int32_t num) {
63.    char str[10]; // 10 chars max for INT32_MAX
64.    int i = 0;
65.    if (num < 0) {
66.        UART_SendChar('-');
67.        num *= -1;
68.    }
69.    if ((num < 10) && (num >= 0)) UART_SendChar('0');
70.    do str[i++] = num % 10 + '0'; while ((num /= 10) > 0);
71.    for (i--; i >= 0; i--) UART_SendChar(str[i]);
72.}
73. 
74.void UART_SendHex8(uint16_t num) {
75.    UART_SendChar(HEX_CHARS[(num >> 4)   % 0x10]);
76.    UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);
77.}
78. 
79.void UART_SendHex16(uint16_t num) {
80.    uint8_t i;
81.    for (i = 12; i > 0; i -= 4) UART_SendChar(HEX_CHARS[(num >> i) % 0x10]);
82.    UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);
83.}
84. 
85.void UART_SendHex32(uint32_t num) {
86.    uint8_t i;
87.    for (i = 28; i > 0; i -= 4) UART_SendChar(HEX_CHARS[(num >> i) % 0x10]);
88.    UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);
89.}
90. 
91.void UART_SendStr(char *str) {
92.    while (*str) UART_SendChar(*str++);
93.}
94. 
95.}

am pretty much sure am activating the right GPIO peripheral. What am not sure is my baudrate. I don't know where to get that figure from. But i borrowed the figure from another STM32FXX family...

I still can't activate and interract in the terminal with USART? Any work around?

Outcomes