... | @@ -12,12 +12,13 @@ In this tutorial you will write a basic driver that can be used to control the c |
... | @@ -12,12 +12,13 @@ In this tutorial you will write a basic driver that can be used to control the c |
|
First of all we have to define the GPIOs that are needed in order for the SPI bus to work.
|
|
First of all we have to define the GPIOs that are needed in order for the SPI bus to work.
|
|
Those GPIOs are:
|
|
Those GPIOs are:
|
|
|
|
|
|
- **SCK**: bus clock signal
|
|
- **SCK**: SPI bus clock signal.
|
|
- **MISO**: communication from slave to master (Master Input Slave Output)
|
|
- **MISO**: communication from slave to master (_Master Input Slave Output_).
|
|
- **MOSI**: communication master to slave (Master Output Slave Input)
|
|
- **MOSI**: communication master to slave (_Master Output Slave Input_).
|
|
|
|
|
|
> :warning: **The SPI is a _synchronous bus_: while the master is writing to `MOSI`, the slave is writing to `MISO`.**
|
|
> :warning: **The SPI is a _synchronous bus_: while the master is writing to `MOSI`, the slave is writing to `MISO`.**
|
|
> 
|
|
|
|
|
|
<div align="center"><img src="images/spi/spi.png" height="400"/><br/><i>SPI transmission scheme.</i></div><br>
|
|
|
|
|
|
Consider that the GPIOs associated to the SPI bus (`SPI1` in this example) can vary according to the microcontroller you are using. For the STM32F407VG, the needed GPIOs are
|
|
Consider that the GPIOs associated to the SPI bus (`SPI1` in this example) can vary according to the microcontroller you are using. For the STM32F407VG, the needed GPIOs are
|
|
`PA5`, `PA6` and `PA7`.
|
|
`PA5`, `PA6` and `PA7`.
|
... | @@ -30,7 +31,7 @@ typedef Gpio<GPIOA_BASE, 6> miso; |
... | @@ -30,7 +31,7 @@ typedef Gpio<GPIOA_BASE, 6> miso; |
|
typedef Gpio<GPIOA_BASE, 7> mosi;
|
|
typedef Gpio<GPIOA_BASE, 7> mosi;
|
|
```
|
|
```
|
|
|
|
|
|
Moreover we need to specify a **chip-select** pin: when set to the low logic level it indicates the beginning of the communication. If you have multiple slaves you will need one chip-select pin for each of them.
|
|
Moreover we need to specify a **chip-select** (or _slave-select_) pin: when set to the low logic level it indicates the beginning of the communication. If you have multiple slaves you will need one chip-select pin for each of them.
|
|
In this example we consider the scenario in which we have a single slave, so a single chip-select pin is required:
|
|
In this example we consider the scenario in which we have a single slave, so a single chip-select pin is required:
|
|
|
|
|
|
```cpp
|
|
```cpp
|
... | | ... | |