cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery Demo code question

baev_al
Associate II
Posted on August 12, 2014 at 12:14

Hello.

I have medium knowledge of C/C++ (it was enough to make certain devices using Atmel AVR microcontorllers). Now I try to understand the Demo example of the STM32F4 Discovery. Is there some description of it?

I watch the program in IAR EWARM.

For example what does __IO mean:

__IO uint32_t DR;

and this (exactly if USART_TypeDef is a pointer why * is placed after the type):

#define USART3 ((USART_TypeDef *) USART3_BASE)

Thanks!

5 REPLIES 5
Posted on August 12, 2014 at 13:37

__IO is a typedef of volatile, it's used to describe peripheral registers which can change internal outside of program control. This causes the compiler to re-read the value every time it's used/referenced.

The value is an Address, values passed as Addresses use Pointers. In this case a pointer to a structure, permitting access to registers inside by name. ie USART3->DR = 0x55;
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
baev_al
Associate II
Posted on August 12, 2014 at 15:34

Thank you for the answer, Clive1.

Did I understand correctly:

__IO uint32_t DR;

is the same as:

volatile uint32_t DR;

?

As for the Addresses and Pointers it is not clear for me.

How the following expression can be explained:

((USART_TypeDef *) USART3_BASE)

?

USART3_BASE = 0x40004800, so

((USART_TypeDef *) 0x40004800) .

How can be the following explained:

USART_TypeDef *

Thanks

Posted on August 12, 2014 at 16:23

Probably want to re-read the chapters on

http://en.cppreference.com/w/c/language/typedef

, pointers and casting.

When you create a variable, the compiler allocates some space, and the linker assigns it an address.

uint32_t foo;

uint32_t *p;

p = &foo; // Address of foo in memory

p = (uint32_t *)0x20002000; // Explicit address of memory

*p = 0xDEADBEEF; // write value to memory address described by pointer

p[0] = 0x12345678; // write to address described by pointer

p[1] = 0x876534321; // write to the next address, incremented by size of the element (4 bytes)

Also works for structures and unions. The USART structure describes the registers within the peripheral, the casting sets it to an address within the memory space.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Chief II
Posted on August 12, 2014 at 16:32

Did I understandcorrectly:

__IO uint32_t DR;

is the same as:

volatile uint32_t DR;

Yes - you should be able to find that easily using the 'Go To Definition' feature of the IDE.

''As for the Addresses andPointers it is not clear for me''

It's pretty basic 'C' stuff:

USART_TypeDef *

is a pointer to  something of the 

USART_TypeDef

type.

Just like

(int*)

0x40004800

 

 casts the value

0x40004800

 to be a pointer to  something of type int

''

I have medium knowledge of C/C++''

Hmm... I think you may be over-estimating

''(it was enough to make certain devices using Atmel AVR microcontorllers)''

All this applies equally to AVRs.

http://blog.antronics.co.uk/2010/12/12/things-you-shouldve-learned-in-c-class-0-introduction/

baev_al
Associate II
Posted on August 12, 2014 at 17:07

Clive1, Neil Anderw thank you for the answers and for the reference. This part is clear now. Will continue to scramble through the mess of demo program...