cancel
Showing results for 
Search instead for 
Did you mean: 

First steps with STM bare metal, UART not working [STM32VLDiscovery]

Jesus Tomas
Associate
Posted on February 25, 2016 at 16:09

First of all, let me apologize in advance for the mistakes in my grammar. Not an english speaker here. I hope, howewer, that this can be understood.

I have recently started to move away from PIC microcontrollers (Mainly PIC18F family) to ARM microcontrollers. I chosed STM32 because of the inexpensive Discovery Boards, wich I could use before doing a board for the project I need to do. I saw that some libraries could be used, but back in the day when I was first learning about microcontrollers, teacher never allowed us to use anything complex, since it ''obscured'' what happened in the microcontroller. Those were good times, learning in ASM with small microcontrollers... Anyway, now, some ten years after that, I still like to do things ''as close to the action'' as I can. So, I found this tutorial for setting up GNU GCC in Keil [

http://m8blogspot.com.es/2012/10/using-gcc-in-keil-best-of-both-worlds.html

] and made it work with some minor changes (Only in preprocessor directives I had to change anything, I think, and there is not much difference between Keil 4 and 5 versions). I had some problems but as of now, I have GPIOs working, and SysTick happily generating ticks every ten ms. Now I am trying to set up one of the UARTs, since withthat done I find easy to develop the rest (Having the possibility to send messages to the PC makes easier to follow the program flow, specially since I am not confortable enough with the debuggin way...). Seems that I can't send anything (Not reading even garbage from the PC), and I have not a oscilloscope right now with me, so, if any person more experienced with STM32 mcus could take a look at this and enlighten me, that would be great. The code: USART configuration

void ConfigUSART2(void)
{
// Pins: UART2_TX is PA2, UART2_RX is PA3
// So PA2 ==> ''alternate function push-pull''
// PA3 ==> ''Input floating''
GPIOA->CRL &= 0xFFFF00FF; // Both nibbles to zero
GPIOA->CRL |= 0x00004200; // and now to the correct values
RCC->APB1ENR |= RCC_APB2ENR_AFIOEN; // AFIO Clock
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; //USART2 Clock
//Following the steps described in page 582 of RM0041:
USART2->CR1 |= USART_CR1_UE; // Step 1: Enable USART
//Step 2: M value, I want 8 bits so no need to touch it
//Step 3: Stop bits, again default value is ok
//Step 4: Not using DMA, so...
USART2->BRR = 0xC9; //Step 5: Baudrate --> Baudrate=Fclk/(15*USARTDIV) --> With 24MHz, 115200 USARTDIV=12,9
USART2->CR1 |= USART_CR1_TE; //Step 6: IDLE
USART2->CR1 |= USART_CR1_RE; //I enable also reception
}

Main

int main()
{
unsigned long i;
uint8_t transmit, receive, data;
Inicializar_GPIOs(); // This congigures GPIOs for leds and pushbutton, working OK
Inicializar_SysTick(); // Again, working OK, this configures SysTick only
ConfigUSART2();
transmit=0; 
receive=0;
while(1)
{
if(GPIOA->IDR & 1) // Button pressed
{
// PC9 led Toggle
GPIOC->ODR ^= GPIO_ODR_ODR9;
//Dummy loop
for(i=0;i<
0x1FFFFF
;i++); 
}
if (transmit<10)
{
data
= 
0x48
;
data+=transmit;
//while(USART2->SR&USART_SR_TXE);// Wait for idle
USART2->DR = dato;
transmitido++;
GPIOC->ODR ^= GPIO_ODR_ODR9;
for(i=0;i<
0x1FFFFF
;i++);
for(
i
=
0
;i<0x1FFFFF;i++);
}
if (USART2->SR&USART_SR_RXNE) //Data ready to be read
{
receive=USART2->DR;
if (receive==data)
{
GPIOC->ODR |= GPIO_ODR_ODR9; //I leave Led always on and wait
while(1);
}
}
}
}

I have translated variables names and comments to english,I hope there is no mistakes... Thanks in advance! And also, do you know any good tutorial/book for programming this mcus bare metal? #limited-thinking
22 REPLIES 22
AvaTar
Lead
Posted on February 29, 2016 at 10:42

> Why someone should remember all bits? Everything is in RM.   But stil for example UART have only 3 register for control and 1 with status.  WOW to many for me.

 

Rather take the timer and PWM generation as example. And imagine it had been written 6++ month ago, by some one else who already left the company. If you need to maintain such code, you spend 95% of the time reading docs and manuals. That would be easily avoidable.

>I'd attack this methd becose I see to many times, bad code created with this methos with guys who aren't familiar with architekture or how the tools work, compiler for exmaple.

 

Readability? WOW. I prefer to read 3 lines not 3 pages for config ports for example.

Bad examples exist for your preferred method, too, especially for the PIC18 (where the OP comes from).

Quite often, ''hardcore coders'' also ignore the CMSIS structs for peripheral access, and

bang ''magic number'' values at hex addresses. While there is no advantage with any decent compiler, any self-documenting feature is lost, and such code becomes almost unreadable. I've seen this method by guys trying to make themselves indispensable for the company.

> Any SPL or HAL creates lazy programist. It's not prefesional.

Imagine another context, where every change in every source file requires a re-certification, costing weeks and thousands of bucks. To outsource any hardware-specific code in a separate HAL layer (library) is economical here. And I speak of HAL in general terms here, specifically NOT of ST's Cube software.

> And and the end.  HAL SPL to have to much errors for me.

If you speak of ST's Cube software, I'm totally with you. The number of frustrated bug reports here speakes volumes ...

> At last portability, SPL, HAL -> NONE , ...

The ST libraries are admittedly not the best examples for portability, for sure.

> ... reg based -> FULL.

 

How is that supposed work for a software that is supposed to run on a Infineon C166, a Fujitsu FR16, a Fujitsu FR32, and a Cortex M4 ?

That is a situation I'm facing.

Radosław
Senior II
Posted on February 29, 2016 at 10:52

pffff. Any of my codes never use any magic number.

Portability? Why you mention other MCU with  other perpherals? this porability is implemented on higher layer. So still no argument.  And from when SPL is portable to other MCU.

Configuring PWM, ba FSM takes me 3 minutes. Maybe less.  

from when we talk about certifications?   HAL (not this from st but generaly), always is created.  So still no argument.   

If you try to attack me bocese some guy at first time do reg access in MAIN, bad choise.

Look al HAL_LL or other solution.

AvaTar
Lead
Posted on February 29, 2016 at 12:55

Posting to this thread becomes increasingly difficult - M$ SharePoint seems the worst possible option for an user forum software.

And I'm tired of answering to misquotations.

Rather let others read both argument lines, and make their choice for themselves.