cancel
Showing results for 
Search instead for 
Did you mean: 

Testing if USART works?

salahuddinash
Associate II
Posted on March 23, 2014 at 02:05

Hi everyone, 

I'm a newbie with stm32f4Discovery kit, and I've just finished writing a code for implementing sending and receiving a character using USART, I'm using Keil uV5

Here is my code which is an example found on the web with some modification to suite stm32f407 microcontroller 

<p>

#include <stm32f4xx.h>

void USARTSetup(void);

int SendChar(int ch);

int GetChar(void);

int main(void)

{

int input,output;

USARTSetup();

output=SendChar(1);

input=GetChar();

output += output;

input += input;

return 0;

}

void USARTSetup(void)

{

 //USART1 clock enable / port A clock enable

RCC->AHB1ENR |= ((1<<0)|(1<<4));       

     

    //configure GPIOA 

/*Each pin in portA has two corresponding bits in MODER, 00->input(default), 01->output, 10->alternate function

  *bit 18,19 = ''10'' to make PA9 as alternate function, bit 20,21 = ''00'' to make PA10 as input*/

    GPIOA->MODER |=(1<<19) ;          

 // step lightly , no pull up nor pull down resistors, 00->not pull nor push, 01->pull-up, 10->pull-down

    GPIOA->PUPDR = 0;    

//speed of PA9 50MHz, 11->50MHz

GPIOA->OSPEEDR |= ((1<<18)|(1<<19));

//configure USART1

//baud rate

USART1->BRR = 64000000L/115200L;

//enable Tx and Rx 

USART1->CR1 |= ((1<<2)|(1<<3));

//enable USART

USART1->CR1 |= (1<<13);

}

int SendChar(int ch)

{

//Check if DR is empty and the previous sent data is sent or not, with checking <RXNE> bit

while(!( USART1->SR & (1<<7)));

USART1->DR=(ch&0xFF);

return (ch);

}

int GetChar(void)

{

//Check if data is recceived or not with checking <RXNE> bit

while(!(USART1->SR & (1<<5))); 

return ((int)(USART1->DR & 0xFF));

}

</p>

I've compiled this code and it was compiled without any errors or warning.. 

now I want to find out if my code function properly or not

I've installed RealTerm and plugged in my Kit and burn the code on the microcontroller, but nothing happened or appeared on RealTerm

Notes: every time I open RealTerm application an error appears 

''Error in GetComDevicesList opening registry key:...''

and I've no idea how to configure RealTerm to work well with Keil, stm32f4Discovery kit, and Windows 7??

so I'll be gratitude if anyone help

thanks a lot 
24 REPLIES 24
Posted on March 23, 2014 at 02:21

You should NEVER exit main(), it goes nowhere, and will crash.

You can't use PA9 on the STM32F4-DISCO, it connects to a huge bulk capacitor related to the USB interface. Review the manual and schematic.

RealTerm needs to see a serial port, just plugging the board into a USB port does NOT achieve this.

What you need to do is connect the USART TX/RX pins to a USB-to-CMOS Serial type converter board/cable. I'd recommend using USART6 and PC6,PC7.

http://www.dlpdesign.com/usb/txrx.shtml

http://www.ebay.com/itm/New-USB-to-TTL-Serial-Cable-Adapter-Chipset-PL2303HX-Cable-Computer-Cable-/121298972592

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 23, 2014 at 02:36

Something like this...

// STM32 USART6 (Tx PC.6, Rx PC.7) STM32F4DIS-BB - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART6 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PC.6 USART6_TX, PC.7 USART6_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
}
/**************************************************************************************/
void USART6_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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(USART6, &USART_InitStructure);
USART_Cmd(USART6, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART6_Configuration();
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART6); // Collect Char
while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART6, Data); // Echo Char
}
}
/**************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while (1)
{}
}
#endif
/**************************************************************************/

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
salahuddinash
Associate II
Posted on March 23, 2014 at 04:55

Thank you very much sir for your fast reply

Here are some points I hope you kindly to indicate, I know they are too many but excuse me as I'm just a newbie trying to learn

1-

#include ''stm32f4_discovery.h''

in your code, what is the stm32f4_discovery.h file is for? I looked for it in the standard peripheral library and the code example included with keil for stm32f4-discovery but didn't find it, I also looked for it online and found this one but I don't know what its importance? and Have I to write it manually and include it?

2-

#ifdef USE_FULL_ASSERT

Is this part of code is essential? cause I've compiled and run a blinky LED code without it? and what is its importance as you didn't write a real code in it except the definition of assert_failed() funciton?

3-

how can I determine the appropriate output speed and baud rate ?

4-

 

You can't use PA9on the STM32F4-DISCO, it connects to a huge bulk capacitor related to the USB interface. Review the manual and schematic. I don't understand why? Cause when I checked the manual of stm32f407 and stm32f4Discovery I didn't find what you're talking about, I found that PA9 is connected toUSART1_TX/TIM1_CH2/I2C3_SMBA/DCMI_D0/OTG_FS_VBUS

and PC6 is connected toI2S2_MCK/TIM8_CH1/DIO_D6/USART6_TX/DCMI_D0/TIM3_CH1

so both of them is connected to many possible Alternate functions, so what is the point? would you explain?

and what is the difference between the manual and the schematic?

5-

RealTerm needs to see a serial port, just plugging the board into a USB port does NOT achieve this.

What you need to do is connect the USART TX/RX pins to a USB-to-CMOS Serial type converter board/cable.

so there is no other way to check if USART is working without this USB-To-CMOS cable?

and If you connected this cable to USART Tx/Rx, will the realterm display the output automatically?

Thanks a ton for your patient and kindness

Posted on March 23, 2014 at 05:47

STM32F4-Discovery_FW_V1.1.0\Utilities\STM32F4-Discovery\stm32f4_discovery.h

It includes stm32f4xx.h, the code itself doesn't need anything in the discovery file, but if you want to go beyond the basic demo, and say control LEDs or buttons, or port something from the other EVAL boards this is the point of abstraction. ie you mix-and-match code between the discovery specific firmware library, and the F4 DSP library which targets the EVAL series boards.

The stm32f4xx.h in turn loads your localized stm32f4xx_conf.h, which should contain board specific details. The most problematic thing to remember is that the Discovery boards use an 8 MHz HSE crystal, whereas the EVAL boards typically have 25 MHz crystals to support Ethernet options.

The assert infrastructure is optional, but code throughout the library does parameter checking, this can be useful for ''Debug'' builds which while slower can do sanity checking, and catch logic/functional errors in your surrounding code. You do have to either do it, or not do it, so if some portions of the code think they should do it, and you fail to provide the support code, you get these linker errors.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 23, 2014 at 06:01

Pins have multiple functions, it is a puzzle to get all the peripherals you want out of the chip. It's generally impossible to get all the peripherals in the chip to function together, and out side. You can get higher pin count devices, which makes life easier, but you're still be able to use a sub-set of the functionality. The design of the STM32F4-DISCO further limits you by applying specific functions, and connections to the chip. With your own design you have more flexibility, but it is still a task to ''fit'' the functionality. ST has an Micro Xplorer tool (and CubeMX), that can here work through the pin assignment.

0690X000006056NQAQ.png

The STM32 does not output RS232 compatible levels, so you cannot connect it directly to a PC serial port, so a) you need a converter chip to go to/from CMOS and RS232 levels, or b) use a USB-to-CMOS/TLL Serial adapter/cable.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
salahuddinash
Associate II
Posted on March 23, 2014 at 06:40

Thanks again for your continuous support and help

ST has an Micro Xplorer tool (and CubeMX), that can here work through the pin assignment.

I understand what you're talking about except this line, I really don't know what does it mean?? 

Sir you didn't explain how can I define the output speed, In your code you're using 2MHz, why? and how can I define the suitable baud rate? in your code you're using 115200, why?

and I read the manual and found there are too many different stuff related to clock like HSE, HSE, PLL but I can't fit them together...

Thanks for your time and sorry for bothering
Posted on March 23, 2014 at 07:00

ST has an Micro Xplorer tool (and CubeMX), that can here work through the pin assignment.

I understand what you're talking about except this line, I really don't know what does it mean??

That should have read ''help work through'', ie permits you to move pin assignments around, and provide immediate feedback whether they can work together. These tools do what others do with Excel spreadsheets, or by manually solving the problem.

The 2 MHz setting relates to the slew-rate of the pin (driving a hard/rapid edge into a capacitive load), there is no point jamming the pin up and down with ''50 MHz edges'', when the highest bandwidth is well below 1 MHz (1 Mbps). Saves power, and reduces EMI. Google slew-rate and rise-time

I choose 115200 because I like that rate, most of the SoC parts I work with use it as their default, and I start up my terminals at that speed. If you like 9600, then use that, it matters not.

and I read the manual and found there are too many different stuff related to clock like HSI, HSE, PLL but I can't fit them together...

Review the Clock Tree diagram, if you've never worked on this stuff before it might take a while to get your head around all the complexity, I have a significant head start.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
salahuddinash
Associate II
Posted on March 23, 2014 at 09:02

I've taken your advice and started studying clock tree from the manual, it looks complicated with a lot of mysterious small block, but I'll understand it, I hope.

Sir, I've copied your code, pasted it instead the code in a main.c of blinky previous woking project, and included ''stm32f4_discovery.h'' and copied and pasted all the required header files in the folder which contains the header in my project, and uncommented #includes related to gpio, usart, rcc in stm32f4xx_conf.h, OK

when compiled, 90 errors appear and all of the same type

C:\Keil_v5\ARM\PACK\Keil\STM32F4xx_DFP\1.0.6\RTE_Driver\GPIO_STM32F4xx.h(88): error: #40: expected an identifier GPIO_AF_SDIO = 12,

honestly, I've no idea about this ''GPIO_STM32F4xx.h'' file and never use it myself, but I think it's something related to Keil because it's found in a group called board support.

any suggestions?

or Do I have to do it manually using registers and give up trying to use StdPeriphLib ?

________________

Attachments :

Capture.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0be&d=%2Fa%2F0X0000000bbu%2FoQIGq7WEfVTc8oePfV4n5TlC619OittUHdbnfB9zpks&asPdf=false
Posted on March 23, 2014 at 12:37

We seem to be going in circles, what you need to do is STOP using the uV5 libraries, and packs, and other random crap Keil is generating/adding, because clearly they are incompatible.

Use the TEMPLATE Project from the STM32F4-Discovery Firmware, if you don't have that already download it now.

http://www.st.com/web/en/catalog/tools/PF257904

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