Could anybody explain me the working principle of the SPI communication? And if its possible show me a link with example codes for communicating SPI? I am going to use “STM32f1xx” Microprocessor with a SPI communicated peripheral device which is ADE7878 (Energy Analyzer). I am using “STM32CubeMx” and “stm32f1xx_hal.h” library. But I couldn’t find much code examples with “stm32f1xx_hal.h” Library. Thanks in advance for your help
-
Does this help?: https://github.com/eleciawhite/STM32Cube/tree/master/STM32Cube_FW_F3_V1.3.0/Projects/STM32F303RE-Nucleo/Examples/SPI/SPI_FullDuplex_ComIT/Src – SoreDakeNoKoto May 17 '16 at 01:55
1 Answers
Basically SPI is a synchronous serial communication interface, which use master-slave relation. Master (your STM32 micro) selects a slave on the bus, pulls down the chip select pin. In your case before all communication actions you need select SPI interface in the ADE7878. Toggle three times the SS/HSA
pin after power-up unless you need communicate with I2C protokoll (see details in page 68 in the datasheet). After selection master starts clock signal in SCLK line. Master sends the first byte to select read/write with bit 0, second byte with a register address on MOSI line. Finally the master reads date of the register of the slave on the MISO line or writes a register value on MOSI.
Before communication you need sets the parameters (clock phase, polarity, clock frequency etc.). You can read about it in the literature and see STM32F1 HAL documentation. There are many good generic materials about SPI. First of all I recommend you the wiki page of SPI. If you search here you can find also several useful hints related to SPI and stm32 (eg. some shorter general intro). I warmly suggest you to read Geoffrey Brown's book, which is an intro to STM32F1 programming. It based on Standard Peripheral Library (ancestor of HAL), but the principles are very well explained.
If you use CubeMX and STM32CubeF1 firmware package is installed you can find some working SPI examples for different tools in the repository. If you want more you can see any examples for any STM32 because HAL code is portable across families. I recommend a search with eg. "__HAL_SPI_ENABLE"
.