MEAM.Design - M4 - I/O Ports (GPIO)


There are 26 GPIO pins on the M4 board, spread across ports A, B, C, E, and F. All of the pins can be configured as push-pull or open-drain with selectable internal pull-up resistors or pull-down resistors or none, and include protection diodes to Vcc and ground (thankfully!). Each port is 16 bit wide while only a subset of those pins can be used.

On port A: 0-6, 8-10
On port B: 0-9, 14
On port C: 15 (also connected to the yellow LED)
On port D: none
On port E: 8,9
On port F: 6,7
This procedure for configuring a pin for GPIO assumes that the clock initialization and prescalers have been setup. This is taken care of in mInit() for you.


The GPIO functions require you to input variables in order to specify both a port and a pin.

For ports, the variable GPIOx is used, where the x is replaced with the port letter (ranging from A to F).

For pins, the variable GPIO_Pin_n is used, where the n is replaced with the pin number (ranging from 0 to 15)


To configure and use a port pin:

1) #include mGeneral.h

Note that you don't have to include the other headers such as the GPIO header (stm32f37x_gpio.h), this is all done for you in mGeneral.h

2) Initialization:

we use the function,

GPIO_Init(GPIOx, &GPIO_InitStruct);

You must declare the second variable (which is a structure of type GPIO_InitTypeDef) as follows:

GPIO_InitTypeDef GPIO_InitStruct;

The GPIO_InitStruct Structure has 5 fields to set 5 different parameters:

i) GPIO_InitStruct.GPIO_Pin

Possible values: GPIO_Pin_All, GPIO_Pin_n, where n is defined above (can be OR'ed to set multiple pins at one time)

ii) GPIO_InitStruct.GPIO_Mode

Possible values: GPIO_Mode_OUT, GPIO_Mode_IN, GPIO_Pin_AF, GPIO_Mode_AN

[output, input, alternate function, analog]

iii) GPIO_InitStruct.GPIO_Speed

Possible values: GPIO_Speed_Level_1, GPIO_Speed_Level_2, GPIO_Speed_Level_3

[where 1 is slowest, 3 is fastest]

iv) GPIO_InitStruct.GPIO_OType

Possible values: GPIO_OType_PP, GPIO_OType_OD

[push-pull output, and open-drain output, respectively]

v) GPIO_InitStruct.GPIO_PuPd

Possible values: GPIO_PuPd_NOPULL, GPIO_PuPd_UP, GPIO_PuPd_DOWN

[neither, pull up, pull down]

For example, if say you want to configure Pins PA2 and PA6 as Output-SpeedLevel2-PushPull-NoPullup pins

	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_2 | GPIO_Pin_6;
	GPIO_InitStruct->GPIO_Mode = GPIO_Mode_OUT;
  	GPIO_InitStruct->GPIO_Speed = GPIO_Speed_Level_2;
  	GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStruct);

3) Reading:

GPIO_ReadInputData(GPIOx, GPIO_Pin_n);

This returns a 16-bit representation of input data pin n on port x.

GPIO_ReadOutputData(GPIOx);

This returns a 16-bit representation of output data pin n on port x.

4) Setting and clearing:

GPIO_SetBits(GPIOx, GPIO_Pin_n);

This sets pin n on port x.

NOTE: for port F, n can only =6

GPIO_ResetBits(GPIOx, GPIO_Pin_n);

This clears pin n on port x.

5) Writing:

GPIO_Write(GPIOx, PortVal);

This writes the value PortVal to port x, where PortVal is a 16 bit unsigned int.


Important Notes:

The maximum current output for any of the pins should NEVER exceed 4mA.