STM8L151 uses hardware SPI to drive W25Q16 Flash

Publisher:王大雷Latest update time:2018-09-08 Source: eefocusKeywords:STM8L151 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM8L151 Reference address:STM8L151 uses hardware SPI to drive W25Q16 Flash

Previous article:STM8L051 uses RTC and USART communication at the same time
Next article:STM8L151G6U6 to achieve low power consumption

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号