C Structure Code of the SPI Register Map of an STM32F407xx Microcontroller Board

STM32F407G discovery board



In this article, we show the C structure code of the SPI register map of an STM32F407xx microcontroller board, so that we can reference each of the SPI registers.

We then will be able to do things such set up SPI communication with another device.

This code can go into the device specific header file.

Instead of looking up the address of each of the SPI registers in order to reference them, you can create a C structure that contains all of the SPI registers.

This is the best practice way of referencing elements in an STM32 board.

Below is the SPI register map of the STM32F407xx board.


SPI register map of an STM32F407xx microcontroller board


So you can see it has quite a number of registers, many of which we may have to reference in order to initialize values for any of the various programs that we write.

Instead of looking up all of the addresses for each of these registers and manually assigning these addresses to variables we create, we can instead create a C structure that contains all of the SPI registers.

We do this in the C code below.

What you want to do is you want to create a separate header file and it's good practice to name this file by the STM32 board you are working with. For example, if I am working with the STM32F407xx board, I can name this file, stm32f407xx.h

Inside of this file, I will put the C structure that contains all of the RCC registers.

Below is the contents of the stm32f407xx.h file.





So let's now go over the code.

So first we create a C macro, which contain the base address of the SPI registers.

As best practice, you want all of these registers to be volatile, as they may be subject to frequent change within a program. All of these registers are 32-bit registers, so we initialize them with uint32_t.

As you can see, we follow the SPI register map. You must put everything in order, as is displayed in the memory map.

The first register of the SPI port registers is the CR1 register, or the SPI control register 1. This has an address offset of 0x00. This register must come first or else the structure will be wrong.

The second register is the CR2 register, or the SPI control register 2. This register has an address offset of 0x04. This register must come next, as order is vital for a C structure representing registers.

The third register is the SR register, or the SPI status register. This register has an address offset of 0x08.

Notice how each subsequent register increases by an address offset of 4. This is because each register is composed of 4 bytes (32-bit registers).

So this continues in our program all the way down to the register, I2SPR.

We refer to this C structure as, SPI_RegDef_t

This is a fitting name, because it forms the register definition for each of the SPI registers.

(SPI_RegDef_t*) represents the pointer to this general structure we created. We then have to initialize this definition to the base address of the SPI registers.

This is done through the following line, SPI_RegDef_t *pSPI1= ((SPI_RegDef_t*)SPI1_BASEADDR);

We then define all the other SPI registers from SPI2 to SPI6.

Now we can use this pSPI pointer variable in our C program to be able to reference any of the SPI registers.

Next we have the line, #define SPI1 ((SPI_RegDef_t*)SPI1_BASEADDR)

And then we create all the other subsequent SPI macro definitions.

This just allows us to have a macro version of the SPI pointer variable.

We are now finished creating our C structure.

Now we can go to our main C program and now use this structure that we have created.

The code below is an example of this.





So one of the most important things is you must include the header file at the top of the C program. Otherwise, the C structure you created will not be tied in with your program.

If it states that the file is not found, then you must include the path for it, so that it can be found.

We then have our main function.

We now can reference any of the SPI registers and set any register equal to any value as we need to for our program to work.

In this code in this example, we set bit 11 of the SPI1 CR1 register to 1, setting the data frame format to 16 bits. We then set the baud rate equal to fPCLK/16

So this is the C structure code of the SPI register map of an STM32F407xx microcontroller board.



Related Resources





HTML Comment Box is loading comments...