STM32F4Discovery USART problems - Page 1
Forum section: electronics / general

author: skerdzius
author_rank: Contributor
timestamp: February 08, 2013, 07:24:00 pm
content: i am using STM32F4Discovery board with CP2102 based USB-TTL converter to send data between PC and this board. so i connect RX, TX wires i use STM32F4xx_DSP_StdPeriph_Lib_V1.0.1 libraries and uVision Keil IDE. i send symbols, but in terminal i recieve a bunch of irrelevant symbolsi am using example of USART code found on internet.

author: ve7xen
author_rank: Super Contributor
timestamp: February 08, 2013, 07:31:39 pm
content: Looks like a baudrate mismatch. We can't do much to help without your code...

author: enz
author_rank: Regular Contributor
timestamp: February 08, 2013, 07:33:31 pm
content: Wrong Baudrate?

author: Bored@Work
author_rank: Super Contributor
timestamp: February 08, 2013, 08:07:45 pm
content: Wrong baudrate. Send 'U's in an endless loop from the board. Use an oscilloscope to check the baud rate you are really sending.Also check number of bits, stop bit length and parity bit (if any).

author: skerdzius
author_rank: Contributor
timestamp: February 08, 2013, 08:10:11 pm
content: i am aware of baudrate and i set it right.Code:#include <stm32f4xx.h>#include <misc.h>// I recommend you have a look at these in the ST firmware folder#include <stm32f4xx_usart.h> // under Libraries/STM32F4xx_StdPeriph_Driver/inc and src#define MAX_STRLEN 12 // this is the maximum string length of our string in charactersvolatile char received_string[MAX_STRLEN+1]; // this will hold the recieved stringvoid Delay(__IO uint32_t nCount) {while(nCount--) {}}/* This funcion initializes the USART1 peripheral** Arguments: baudrate --> the baudrate at which the USART is*supposed to operate*/void init_USART1(uint32_t baudrate){/* This is a concept that has to do with the libraries provided by ST* to make development easier the have made up something similar to* classes, called TypeDefs, which actually just define the common* parameters that every peripheral needs to work correctly** They make our life easier because we don't have to mess around with* the low level stuff of setting bits in the correct registers*/GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RXUSART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization/* enable APB2 peripheral clock for USART1* note that only USART1 and USART6 are connected to APB2* the other USARTs are connected to APB1*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);/* enable the peripheral clock for the pins used by* USART1, PB6 for TX and PB7 for RX*/RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);/* This sequence sets up the TX and RX pins* so they work correctly with the USART1 peripheral*/GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are usedGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;// the pins are configured as alternate function so the USART peripheral has access to themGPIO_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_UP;// this activates the pullup resistors on the IO pinsGPIO_Init(GPIOB, &GPIO_InitStruct);// now all the values are passed to the GPIO_Init() function which sets the GPIO registers/* The RX and TX pins are now connected to their AF* so that the USART1 can take over control of the* pins*/GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);/* Now the USART_InitStruct is used to define the* properties of USART1*/USART_InitStruct.USART_BaudRate = baudrate;// the baudrate is set to the value we passed into this init functionUSART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)USART_InitStruct.USART_StopBits = USART_StopBits_1;// we want 1 stop bit (standard)USART_InitStruct.USART_Parity = USART_Parity_No;// we don't want a parity bit (standard)USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiverUSART_Init(USART1, &USART_InitStruct);// again all the properties are passed to the USART_Init function which takes care of all the bit setting/* Here the USART1 receive interrupt is enabled* and the interrupt controller is configured* to jump to the USART1_IRQHandler() function* if the USART1 receive interrupt occurs*/USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt// finally this enables the complete USART1 peripheralUSART_Cmd(USART1, ENABLE);}/* This function is used to transmit a string of characters via* the USART specified in USARTx.** It takes two arguments: USARTx --> can be any of the USARTs e.g. USART1, USART2 etc.*(volatile) char *s is the string you want to send** Note: The string has to be passed to the function as a pointer because*the compiler doesn't know the 'string' data type. In standard*C a string is just an array of characters** Note 2: At the moment it takes a volatile char because the received_string variable*declared as volatile char --> otherwise the compiler will spit out warnings* */void USART_puts(USART_TypeDef* USARTx, volatile char *s){while(*s){// wait until data register is emptywhile( !(USARTx->SR & 0x00000040) );USART_SendData(USARTx, *s);*s++;}}int main(void) {init_USART1(9600); // initialize USART1 @ 9600 baudUSART_puts(USART1, "hello world"); // just send a message to indicate that it workswhile (1){}}comments are not mine, they are author's who posted this on internet

author: Bored@Work
author_rank: Super Contributor
timestamp: February 08, 2013, 08:15:46 pm
content: Quote from: skerdzius on February 08, 2013, 08:10:11 pmThen it would work. But it is your choice. You can start to measure or you can continue sitting there claiming everything is right

author: skerdzius
author_rank: Contributor
timestamp: February 08, 2013, 08:28:10 pm
content: baudrate in code is set at 9600. i set the same value in terminal. so what could possibly go wrong?and sorry, but i don't have osciloscope

author: AndreasF
author_rank: Frequent Contributor
timestamp: February 08, 2013, 08:31:43 pm
content: Code looks fine, but pin PB6 (your USART1_TX) is used as the I2C clock signal to the audio codec chip on the STM32F4Discovery, and maybe this causes some sort of interference.I would try some of the other pins that aren't connected to anything else, eg. PA2 and PA3 (of USART2).

author: enz
author_rank: Regular Contributor
timestamp: February 08, 2013, 08:42:59 pm
content: Maybe you should change this:Code:void USART_puts(USART_TypeDef* USARTx, volatile char *s){while(*s){// wait until data register is emptywhile( !(USARTx->SR & 0x00000040) );USART_SendData(USARTx, *s);*s++;}}to thisCode:void USART_puts(USART_TypeDef* USARTx, volatile char *s){while(*s){// wait until data register is emptywhile( !(USARTx->SR & 0x00000040) );USART_SendData(USARTx, *s);s++;}}Hint: Note the removed asterisk before s++

author: Skimask
author_rank: Super Contributor
timestamp: February 08, 2013, 08:51:49 pm
content: Quote from: skerdzius on February 08, 2013, 08:28:10 pmSaid nobody...ever...

author: skerdzius
author_rank: Contributor
timestamp: February 08, 2013, 09:15:19 pm
content: i tried deleting asterix as told. everything compiled good but nothing changed. when i changed to USART2 nothing changed as well :S

author: enz
author_rank: Regular Contributor
timestamp: February 08, 2013, 09:22:35 pm
content: Weird, it seems that there are a couple of errors coming together.With the asterisk in you were incrementing the data you want to send, not the pointer. So this was an endless loop.Without the asterisk you are now incrementing the pointer, there's no longer an endlees loop and the intended data will be send. Two errors less.Next point:Is the system clock set to the value the baudrate will be calculated from?

author: CarlG
author_rank: Regular Contributor
timestamp: February 08, 2013, 09:25:49 pm
content: Have you verified that you're using the same PCLK and prescaler setting(s) as the USART code you're using requires?

author: skerdzius
author_rank: Contributor
timestamp: February 09, 2013, 12:45:03 pm
content: sorry, but i am not aware of these PCL prescaler or System clock settings. i don't know where to change them using standart peripheral librariesexamples of usart code found on internet don't have any of these settings involved in them as i discovered

author: gxti
author_rank: Frequent Contributor
timestamp: February 09, 2013, 02:43:16 pm
content: Quote from: enz on February 08, 2013, 09:22:35 pmActually, no. In C, "++" binds more tightly than "*". So "*x++" is really "*(x++)" which is why you can do e.g ."*x++ = 0;". The before and after statements are equivalent.

author: skerdzius
author_rank: Contributor
timestamp: February 09, 2013, 03:44:34 pm
content: i found the formula in stm32f4xx_usart.h file, however i don't understand what are these parameters and how to change them in codeCode:The baud rate is computed using the following formula:- IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (USART_InitStruct->USART_BaudRate)))- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 8 * (OVR8+1)) + 0.5Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register.

author: mrflibble
author_rank: Super Contributor
timestamp: February 09, 2013, 04:42:30 pm
content: Here's a quick snippet from working code I normally use on that board. I think you can figure out how to change it from the current setting of 115200 baud to 9600 baud. ;-)Code:#include <stm32f4xx_usart.h>/*** @brief Configure USART2 at 115200 baud, 8N1. Configure the GPIO pins (PA.2, PA.3) as push-pull.* @param None*/void USART2_Config(void){GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);// PA.2 = TX// PA.3 = RXGPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // PA.2 = TXGPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // PA.3 = RX// USART_InitStructure.USART_BaudRate = 9600; // confirmed working. Remember to do: stty -F /dev/ttyUSB0 9600USART_InitStructure.USART_BaudRate = 115200; // confirmed working. Remember to do: stty -F /dev/ttyUSB0 115200USART_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_Tx | USART_Mode_Rx;USART_Init(USART2, &USART_InitStructure);USART_Cmd(USART2, ENABLE);}

author: mrflibble
author_rank: Super Contributor
timestamp: February 09, 2013, 04:50:47 pm
content: Quote from: skerdzius on February 08, 2013, 07:24:00 pmI just noticed this... I also have a few of those. Andsomeof those have the RX/TX labeled precisely the wrong way around. So better check and double check that CP2102 first.

author: skerdzius
author_rank: Contributor
timestamp: February 09, 2013, 06:09:02 pm
content: i can't explain this but i fix my connection like this: i am using 115200 baudrate on my discovery boardCode:init_USART1(115200); // initialize USART1 @ 115200 baudand on terminal i am listening at 38400 baudrate. then it works greatthanks for help. maybe later i will find out a better solution, but it's enough for now

author: AndreasF
author_rank: Frequent Contributor
timestamp: February 09, 2013, 08:49:01 pm
content: Ahhha, I'm going to take a wild guess here then, that your HSE_VALUE define is wrong. As you may have noticed, 115200 is exactly 1/3 of 38400, so it seems that your STM32F4 thinks it's running three times faster than it actually is. This HSE_VALUE must be the frequency of the crystal on your board (on mine it's 8 Mhz). If it's not defined elsewhere the startup code will use the value stored in "stm32f4xx.h", which is 25000000 (or 25Mhz). As you notice, 25Mhz is approx. 3 times 8 Mhz.I'm not familiar with the Keil IDE, but I'm guessing there is a way to set "predefined symbols" and that's where you should include a line like this "HSE_VALUE = 8000000" (or whatever crystal frequency you have on your board).from "system_stm32f4xx.c":Code:* (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value* 25 MHz), user has to ensure that HSE_VALUE is same as the real* frequency of the crystal used. Otherwise, this function may* have wrong result.

author: mrflibble
author_rank: Super Contributor
timestamp: February 09, 2013, 11:25:58 pm
content: Yeah, that 25 MHz vs 8 MHz got me too when I just started. I grabbed some example code ... which of course justhad to havethe wrong PLL settings. You probably have it set to 25 MHz while you should have it set to 8 MHz.

author: enz
author_rank: Regular Contributor
timestamp: February 10, 2013, 10:08:19 am
content: Quote from: gxti on February 09, 2013, 02:43:16 pmYou are right, i missed the binding precedence.But then the asterisk does not do anything and is needless.

author: mrflibble
author_rank: Super Contributor
timestamp: February 10, 2013, 11:58:56 am
content: Quote from: enz on February 10, 2013, 10:08:19 amO_oYou don't use pointers very often, do you?

author: Bored@Work
author_rank: Super Contributor
timestamp: February 10, 2013, 12:05:24 pm
content: Quote from: skerdzius on February 09, 2013, 06:09:02 pmIn other words, the baud rate is still wrong and was always wrong. And you are still in denial and call that a fix.

author: mrflibble
author_rank: Super Contributor
timestamp: February 10, 2013, 12:31:49 pm
content: Yeah, but look on the bright side! Now the baud rate error is only about 4%. XD

author: HackedFridgeMagnet
author_rank: Super Contributor
timestamp: February 10, 2013, 02:40:04 pm
content: Sorry to be pedantic, but because I didn't know this I looked it up.Apparently ++ and * have the same binding precedence but their group bind RL instead of LR.Yeah yeah, which effectively means ++ has the higher binding precedence.

author: enz
author_rank: Regular Contributor
timestamp: February 10, 2013, 02:49:31 pm
content: Quote from: mrflibble on February 10, 2013, 11:58:56 amOf course i do.Maybe you can explain what the asterisk would do exactly (and why they would be neccessary) in the following two statements:1. *x++;2. *(x++);Regards,Martin

author: mrflibble
author_rank: Super Contributor
timestamp: February 10, 2013, 03:46:13 pm
content: Quote from: enz on February 10, 2013, 10:08:19 amI was replying to this particular quote. As in the combination of "*x++ = 0;" and "But then the asterisk does not do anything and is needless.". I was not replying to what you may or may not have been thinking while you were writing that. ;-)But from your question I see what you meant to say.QuoteAs for the answer, that would depend on how naive the compiler is, and what optimizations and such. In general I would expect those to just increment the x pointer. In fully pedantic mode, and on a platform where a memory access also performs a particular operation (memory mapped control register or whatnot) that cannot be optimized away, you could also expect it to do: 1) access memory at location x and then 2) increment x pointer.

author: enz
author_rank: Regular Contributor
timestamp: February 10, 2013, 03:56:47 pm
content: QuoteAh, now that was the misunderstanding. I was only referring to the original code from the OP (where there is no assignement), not to the "*x++ = 0;" example.In the original code (*x++;) there is no need for the asterisk. In fact, it leads to false understanding what should be achieved with this statement and should be removed.

author: mrflibble
author_rank: Super Contributor
timestamp: February 10, 2013, 10:34:03 pm
content: For extra confuzlement you could do"++*x++";.

author: enz
author_rank: Regular Contributor
timestamp: February 11, 2013, 07:48:10 am
content: Quote from: mrflibble on February 10, 2013, 10:34:03 pmNice try. But i think it may not be enough for getting acknowledged by the obfuscated c aficionados.

author: skerdzius
author_rank: Contributor
timestamp: February 15, 2013, 08:21:39 pm
content: Quote from: Bored@Work on February 10, 2013, 12:05:24 pmso i found out what was my problem. and it's rather shame for mei was using STM32F4xx_DSP_StdPeriph_Lib_V1.0.1 for all my projects, but all clocks, and settings were for STM32_eval board, and i am usint STM32F4Discovery. so i downloaded STM32F4-Discovery_FW_V1.1.0 edited project a bit, and now program is running as it's supposed to do. so happy now

author: ericksantosjaquez
author_rank: Newbie
timestamp: December 01, 2013, 01:41:42 am
content: Where you find the correct template?

author: mrflibble
author_rank: Super Contributor
timestamp: December 02, 2013, 11:07:37 am
content: Quote from: ericksantosjaquez on December 01, 2013, 01:41:42 amIf you read the previous posts you'll find code snippets by various posters.