cancel
Showing results for 
Search instead for 
Did you mean: 

USART not getting enabled

malehakim
Associate III
Posted on September 24, 2016 at 17:57

Am trying to run USART by getting to write in the E-Terminal of CodeLite to interract with my STM32F4 However, i get no response when i the main method is called....here is my main method....


#include ''inc/stm32/stm32f4xx.h''

#include ''inc/stm32/stm32f4xx_gpio.h''

#include ''inc/stm32/stm32f4xx_usart.h''

#include ''inc/stm32/stm32f4xx_rcc.h''

#include ''inc/stm32/misc.h''

#include <
inc
/uart.h>


int main(int argc, char **argv)

{

SystemCoreClockUpdate();


UART2_Init(1382400);


UART_SendStr(''SD init: '');


return 0;

}

and here is my header uart.h file


// Define to prevent recursive inclusion -------------------------------------

#ifndef __UART_H

#define __UART_H


#include ''inc/stm32/stm32f4xx_rcc.h''


#define UART_PORT USART2

#define UART_TX_PIN GPIO_Pin_5 // PD2 (USART2_TX) 

#define UART_RX_PIN GPIO_Pin_6 // PD3 (USART2_RX)

#define UART_GPIO_PORT GPIOD

#define UART_PORT_PERIPH RCC_AHB1Periph_GPIOD


#define HEX_CHARS ''0123456789ABCDEF''



// Function prototypes

void UART2_Init(uint32_t baudrate);


void UART_SendChar(char ch);


void UART_SendInt(int32_t num);

void UART_SendInt0(int32_t num);

void UART_SendHex8(uint16_t num);

void UART_SendHex16(uint16_t num);

void UART_SendHex32(uint32_t num);


void UART_SendStr(char *str);


#endif // __UART_H

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


#include ''inc/stm32/stm32f4xx_rcc.h''

#include ''inc/stm32/stm32f4xx_gpio.h''

#include ''inc/stm32/stm32f4xx_spi.h''

#include ''inc/stm32/stm32f4xx_usart.h''


#include <
inc
/uart.h>

#include <
string.h
>



// Initialize and configure UART peripheral with specified baudrate

// input:

// baudrate - UART speed (bits/s)

void UART2_Init(uint32_t baudrate) {

GPIO_InitTypeDef PORT;

USART_InitTypeDef UART;


// UART peripheral clock enable

RCC_AHB1PeriphClockCmd(UART_PORT_PERIPH,ENABLE);

RCC_APB1PeriphClockCmd((RCC_APB1Periph_USART2),ENABLE); 


// Alternative functions of GPIO pins

GPIO_PinAFConfig(GPIOD, GPIO_PinSource5 | GPIO_PinSource6, GPIO_AF_USART2); 


// TX pin

PORT.GPIO_Mode = GPIO_Mode_AF;

PORT.GPIO_Speed = GPIO_Speed_50MHz;

PORT.GPIO_OType = GPIO_OType_PP;

PORT.GPIO_PuPd = GPIO_PuPd_UP;

PORT.GPIO_Pin = UART_TX_PIN;

GPIO_Init(UART_GPIO_PORT,&PORT);

// RX pin

PORT.GPIO_Pin = UART_RX_PIN;

GPIO_Init(UART_GPIO_PORT,&PORT);


// UART port

UART.USART_BaudRate = baudrate;

UART.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // No flow control

UART.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // RX+TX mode

UART.USART_WordLength = USART_WordLength_8b; // 8-bit frame

UART.USART_Parity = USART_Parity_No; // No parity check

UART.USART_StopBits = USART_StopBits_1; // 1 stop bit

USART_Init(UART_PORT,&UART);

USART_Cmd(UART_PORT,ENABLE);

}


void UART_SendChar(char ch) {

while (!(UART_PORT->SR & USART_SR_TC)); // wait for ''Transmission Complete'' flag cleared

UART_PORT->DR = ch;

}


void UART_SendInt(int32_t num) {

char str[10]; // 10 chars max for INT32_MAX

int i = 0;

if (num < 
0
) {

UART_SendChar('-');

num *= -1;

}

do str[i++] = num % 10 + '0'; while ((num /= 10) > 0);

for (i--; i >= 0; i--) UART_SendChar(str[i]);

}


void UART_SendInt0(int32_t num) {

char str[10]; // 10 chars max for INT32_MAX

int i = 0;

if (num < 
0
) {

UART_SendChar('-');

num *= -1;

}

if ((num < 10) && (num >= 0)) UART_SendChar('0');

do str[i++] = num % 10 + '0'; while ((num /= 10) > 0);

for (i--; i >= 0; i--) UART_SendChar(str[i]);

}


void UART_SendHex8(uint16_t num) {

UART_SendChar(HEX_CHARS[(num >> 4) % 0x10]);

UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);

}


void UART_SendHex16(uint16_t num) {

uint8_t i;

for (i = 12; i > 0; i -= 4) UART_SendChar(HEX_CHARS[(num >> i) % 0x10]);

UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);

}


void UART_SendHex32(uint32_t num) {

uint8_t i;

for (i = 28; i > 0; i -= 4) UART_SendChar(HEX_CHARS[(num >> i) % 0x10]);

UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);

}


void UART_SendStr(char *str) {

while (*str) UART_SendChar(*str++);

}


}

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? #usart #stm32 #stm32f4 #stm32f407
3 REPLIES 3
Posted on September 24, 2016 at 18:16

GPIO_PinAFConfig(GPIOD, GPIO_PinSource5 | GPIO_PinSource6, GPIO_AF_USART2); 

PinSource is an Index, you can't OR it together, decompose this into two individual lines. Also use TXE to see if the buffer in empty, the TC bit doesn't do that, and is potentially the slowest method.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
malehakim
Associate III
Posted on September 24, 2016 at 19:00

I have effected the changes...but one thing. I realised my processor too can do USART1 on PORTA peripheral. I changed them out as in guided in the attached JPG. However, i get no response yet...

________________

Attachments :

plus.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0bs&d=%2Fa%2F0X0000000bb9%2FUQi.liYrQnkfjVQMZ7kWGIUwVBGy8OvH6oFt9MjzN7A&asPdf=false
Posted on September 24, 2016 at 19:59

The usability of ports/pins will depend on the design of the board you are using, you haven't provided any details to make that determination.

The STM32F4-DISCO board can't use PA9 due to a large capacitor on that pin used by the USB interface.

USART1 is on APB2 you need to make sure you enable the clock on the right bus. You haven't provided code which can be reviewed wrt USART1

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..