SPI: A four-wire serial communications protocol that allows half- or full-duplex, synchronous, serial communications with other devices.
MISO: Master mode input, slave mode output line
MOSI: Master mode output, slave mode input line
CLK: Clock line
NSS: Slave device selection pin, master device standard IO driver, and used to distinguish slave devices
Take STM8L driving SPI Flash W25Q16 as an example to explain what to pay attention to when using STM8L's SPI and how to simply drive W25Q16.
Winbond's W25Q16 SPI Flash chip uses the SPI interface. I won't talk about the advantages and disadvantages of this chip. There is a hardware SPI on the STM8L, which can easily drive the W25Q16. Let's take a look at how to configure the STM8L's SPI peripheral.
void SPI_FLASH_Init(void)
{
//SPI_CLOCK:PB5, SPI_MOSI: PB6, SPI_MISO: PB7
GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast);
GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast);
//Host mode, configured to enter this setting is critical
GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT);
/* Initialize SPI */
SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_4, SPI_Mode_Master,\
SPI_CPOL_High, SPI_CPHA_2Edge, \
SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft, 0x07);
SPI_Cmd(SPI1, ENABLE); /* Enable SPI */
/* Configure CS pin */
GPIO_Init(SPI_CS, SPI_Pin_CS, GPIO_Mode_Out_PP_High_Fast);
GPIO_WriteBit(SPI_CS, SPI_Pin_CS, SET); /* Pull high to disable external SPI device*/
}
In the above, we should pay attention to the settings of the pins PB5, PB6, and PB7 used by SPI. In the SPI master mode, PB5 and PB6 can be set to external pull-up outputs or internal pull-up outputs according to the peripheral circuit, while PB7 should be set to internal pull-up input or floating input (with external pull-up resistor). After these settings, the clock of the SPI peripheral should also be turned on.
The SPI bus mode of W25Q16 is Mode0 or Mode3, which can be seen in the manual or the timing diagram below in this article. So the initialization of SPI should be considered.
The parameters in SPI_Init are SPI_CPOL_High, SPI_CPHA_2Edge for Mode0, SPI_CPOL_Low, SPI_CPHA_1Edge for Mode3. If it is any other combination, the communication data will be misaligned and incorrect.
The following is the function to read the Flash manufacturer ID and device ID.
uint8_t SPI_FLASH_SendByte(u8 byte)
{
/* Loop while DR register in not empty */
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_SendData(SPI1, byte);
/* Wait to receive a byte */
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_ReceiveData(SPI1);
}
uint16_t SPI_FLASH_ReadID(void)
{
u16 Device_ID = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW(); //Pull down the chip select pin level to select the peripheral
/* Send "RDID " instruction */
SPI_FLASH_SendByte(0x90); //Read device ID instruction
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
/* Read a byte from the FLASH */
Device_ID = (SPI_FLASH_SendByte(Dummy_Byte)<<8);
Device_ID |= SPI_FLASH_SendByte(Dummy_Byte);
SPI_FLASH_CS_HIGH(); //Pull up the chip select pin level,
return Device_ID;
}
In the manual of W25Q16,
The data read back by the function should be the value above. Look at the ID read instruction 0x90 in the manual.
The command sending sequence for obtaining the device ID is as shown above. The first byte is 0x90, followed by two dummy bytes (any value is acceptable) and 0x00. Then the manufacturer ID and device ID are read. The above function basically implements this timing sequence.
The actual test results are as follows:
The result is consistent with the ID given in the manual.
Previous article:STM8L051 uses RTC and USART communication at the same time
Next article:STM8L151G6U6 to achieve low power consumption
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- DSP28335 interrupt configuration
- Quickly get a fuel gauge working from scratch
- Experiment on the use of voltage sensor and current sensor
- Meaning of the tail vertebra
- Why can't I open the TI reference design?
- The module function address of driverlib.a solidified in ROM
- Show off the PSSD mobile hard drive
- Program-controlled voltage source and its electromagnetic compatibility design
- Add horizontal lines to letters in Altium designer 19
- [Evaluation of Motor Kit P-NUCLEO-IHM]-3.1. Task check-in (basic tasks and self-assessment tasks)