Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

STM32F2xx Manual - Understanding GPIO register tables

pgio stm32f2 register stm32f2xx manual

  • Please log in to reply
5 replies to this topic

#1 knutolai

knutolai

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 25 April 2013 - 11:03 AM

Hi 

Im learning to program a Netduino 2 board in C++ and I'm having a hard time understanding certain parts of the STM32F2xx MCU manual:

(http://www.st.com/st.../CD00225773.pdf).

(I was unsure where to post this topic. The STMicroelectronics requires a company name, and I'm just a student. I apologize if this was posted in the wrong subforum.) 

 

Page 146 - 151 of the Manual describes the MCU GPIO register. I'm quite new to MCU's and the manuals terminology and I don't quite understand how to read the section 6.4.1 as well as the following sections. 

 

First of: In the table what does 'rw' mean and what does the syntax [1:0] (in lines such as MODER[1:0]) and the numbers 31 to 0 above the table represent?

 

Also could anyone explain the line "Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)"? (first line beneath the table). The way I read it if I for instance would want to configure PA0 of the GPIO to analog mode I would write:

 

GPIOA_MODER0 = 0b11;   // sets PA0 to analog mode

 

Have I misunderstood the concept completely? And if so, as a example, what would be the right syntax for setting the pin PA0 to analog? 

 



#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 25 April 2013 - 11:30 AM

First of: In the table what does 'rw' mean and what does the syntax [1:0] (in lines such as MODER[1:0]) and the numbers 31 to 0 above table represent? rw = read and write allowed. [1:0] = bits 0 and 1 0-31 = bit number. The table is describing the 32-bit register. So for a port, say A there are 16 possible output pins. The mode of each pin is controlled by the GPIOA_MODER register. This register is a 32 bit value. Bits 0 and 1 in the register determine the mode of PA0 and the bits can be written and read. Similarly, bits 2 and 3 determine the mode of PA1. Also could anyone explain the line "Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)"? (first line beneath the table). The way I read it if I for instance would want to configure PA0 of the GPIO to analog mode I would write: GPIOA_MODER0 = 0b11; // sets PA0 to analog mode MODERy[1:0] says that the mode of pin y is determined by the two bits 1:0 and that the bits have the following meaning... I cannot confirm if GPIOA_MODER0 exists in the header files as I am not near my development machine at the moment. Certainly for PA0 you could write:

 

GPIOA_MODER = 0x03;

For the remaining pins you would left shift by the pin number multiplied by 2. So for PA3 you would write the following:

pin = 3;mode = 0x03;GPIOA_MODER |= (mode << (pin * 2));

I've illustrated with a variable pin and mode as you can then abstract to a generic method if you wish.

Strictly speaking you should clear the bits before setting them. You can find out a great deal by looking in the standard library. Hope this help, Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 knutolai

knutolai

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 25 April 2013 - 01:10 PM

Thanks this helps a lot!  Here '|=' works like a bitwise '+=' right? 



#4 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 26 April 2013 - 07:07 AM

Thanks this helps a lot!  Here '|=' works like a bitwise '+=' right? 

 

|= is the same as the following bitwise or:

 

value = value | mask;

 

 

Remember I talked about clearing the bits before setting them, well you would use something like the following to clear them:

 

pin = 3;mode = 0x02;mask = 0x03;GPIOA_MODER &= ~(mask << (pin * 2));GPIOA_MODER |= (mode << (pin * 2));

Where mask represents all of the bits in the particular field set to 1.

 

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#5 knutolai

knutolai

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 09 May 2013 - 09:26 AM

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]Remember I talked about clearing the bits before setting them, well you would use something like the following to clear them:[/color]

Would it be a option also just to reset a given register and overwrite it afterwards?

 

like

GPIOA_MODER = 0x00; // reset

PGIOA_MODER |= <some value here>; // set register as wanted



#6 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 09 May 2013 - 11:48 AM

Would it be a option also just to reset a given register and overwrite it afterwards?

 

like

GPIOA_MODER = 0x00; // reset

PGIOA_MODER [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]|= <some value here>; // set register as wanted[/color]

Doing this would reset all of the pins on GPIOA.  If that's not what you want then you need to follow the advice on clearing just the bits relating to the pin in question (see previous posts above).

 

You are correct in thinking that you need to clear the bits before setting them using bitwise or.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter






Also tagged with one or more of these keywords: pgio, stm32f2, register, stm32f2xx, manual

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.