当前位置:网站首页>stm32F407------SPI
stm32F407------SPI
2022-07-25 17:12:00 【Can't go on the ground】
Catalog
One 、 The physical layer 、 Protocol layer
① The punctual atoms SPI Interface :(SPI1 identical )
② Wildfire SPI Interface :(SPI1 identical )
3、 ... and 、stm32 Of SPI Firmware library
Four 、 Code ——SPI initialization
5、 ... and 、 Code —— Read FLASH Of ID
6、 ... and 、 Code —— Erase operation
7、 ... and 、 Code —— Read and write operations
8、 ... and 、 Code —— Routines and stored decimals
One 、 The physical layer 、 Protocol layer


CS = 0; Select film selection


full duplex





CPHA = 0; Odd edge sampling

CPHA = 1; Even edge sampling

Two 、stm32 Of SPI peripherals


① The punctual atoms SPI Interface :(SPI1 inequality )

② Wildfire SPI Interface :(SPI1 inequality )







3、 ... and 、stm32 Of SPI Firmware library

① SPI_Direction
This member setup SPI The direction of communication , Can be set to
Two wire full duplex (SPI_Direction_2Lines_FullDuplex),
Two lines only receive (SPI_Direction_2Lines_RxOnly),
One line only receives (SPI_Direction_1Line_Rx)、
Single line send only mode (SPI_Direction_1Line_Tx).
②SPI_Mode
This member setup SPI Working in host mode (SPI_Mode_Master) Or slave mode (SPI_Mode_Slave ),
The biggest difference between the two modes is SPI Of SCK The timing of the signal line ,SCK The time sequence of is generated by the host in the communication . If configured as slave mode ,STM32 Of SPI Peripherals will accept external SCK The signal .
③SPI_DataSize
This member may choose SPI The data frame size of communication is 8 position (SPI_DataSize_8b) still 16(SPI_DataSize_16b).
④SPI_CPOL and SPI_CPHA
These two member configurations SPI Of Clock polarity CPOL and Clock phase CPHA, These two configurations affect SPI Communication mode ,
Clock polarity CPOL member , Can be set to High level (SPI_CPOL_High) or Low level (SPI_CPOL_Low ).
Clock phase CPHA Can be set to SPI_CPHA_1Edge( stay SCK The odd edge of the data collection ) or SPI_CPHA_2Edge ( stay SCK Even edge of the data collection ) .
⑤SPI_NSS
This member configures NSS The mode of use of pins , You can choose Hardware mode (SPI_NSS_Hard ) And Software mode (SPI_NSS_Soft ), In hardware mode SPI The selection signal is from SPI The hardware automatically generates , The software model needs to put the corresponding GPIO When the port is set high or low, non chip select and chip select signals are generated . In practice, the application of software mode is more .
⑥SPI_BaudRatePrescaler
This member sets baud rate frequency division factor , The clock after frequency division is SPI Of SCK Clock frequency of signal line . This member parameter can be set to fpclk Of 2、4、6、8、16、32、64、128、256 frequency division .
⑦SPI_FirstBit
All serial communication protocols have MSB Go ahead ( The high data is at the top ) still LSB Go ahead ( The low data is in the front ) The problem of , and STM32 Of SPI Modules can be organized through this structure member , Program and control this feature .
⑧SPI_CRCPolynomial
This is a SPI Of CRC Polynomials in validation , If we use CRC When checking , Just use the parameters of this member ( polynomial ), To calculate CRC Value .
After configuring these structure members , To be called SPI_Init Function writes these parameters to the register , Realization SPI The initialization , And then call SPI_Cmd Enable SPI peripherals .
Four 、 Code ——SPI initialization
/* Private typedef -----------------------------------------------------------*/
//#define sFLASH_ID 0xEF3015 //W25X16
//#define sFLASH_ID 0xEF4015 //W25Q16
//#define sFLASH_ID 0XEF4017 //W25Q64
#define sFLASH_ID 0XEF4018 //W25Q128
//#define SPI_FLASH_PageSize 4096
#define SPI_FLASH_PageSize 256
#define SPI_FLASH_PerWritePageSize 256
/* Private define ------------------------------------------------------------*/
/* Command definition - start *******************************/
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg 0x05
#define W25X_WriteStatusReg 0x01
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
#define Dummy_Byte 0xFF
/* Command definition - ending *******************************/
/*SPI Interface definition - start ****************************/
#define FLASH_SPI SPI1
#define FLASH_SPI_CLK RCC_APB2Periph_SPI1
#define FLASH_SPI_CLK_INIT RCC_APB2PeriphClockCmd
#define FLASH_SPI_SCK_PIN GPIO_Pin_3
#define FLASH_SPI_SCK_GPIO_PORT GPIOB
#define FLASH_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define FLASH_SPI_SCK_PINSOURCE GPIO_PinSource3
#define FLASH_SPI_SCK_AF GPIO_AF_SPI1
#define FLASH_SPI_MISO_PIN GPIO_Pin_4
#define FLASH_SPI_MISO_GPIO_PORT GPIOB
#define FLASH_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define FLASH_SPI_MISO_PINSOURCE GPIO_PinSource4
#define FLASH_SPI_MISO_AF GPIO_AF_SPI1
#define FLASH_SPI_MOSI_PIN GPIO_Pin_5
#define FLASH_SPI_MOSI_GPIO_PORT GPIOB
#define FLASH_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define FLASH_SPI_MOSI_PINSOURCE GPIO_PinSource5
#define FLASH_SPI_MOSI_AF GPIO_AF_SPI1
#define FLASH_CS_PIN GPIO_Pin_6
#define FLASH_CS_GPIO_PORT GPIOG
#define FLASH_CS_GPIO_CLK RCC_AHB1Periph_GPIOG// Software simulation mode
#define SPI_FLASH_CS_LOW() {FLASH_CS_GPIO_PORT->BSRRH=FLASH_CS_PIN;}
#define SPI_FLASH_CS_HIGH() {FLASH_CS_GPIO_PORT->BSRRL=FLASH_CS_PIN;}
/*SPI Interface definition - ending ****************************/
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Can make FLASH_SPI And GPIO The clock */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO,SPI_FLASH_SPI_SCK_GPIO Clock enable */
RCC_AHB1PeriphClockCmd (FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK|FLASH_SPI_MOSI_GPIO_CLK|FLASH_CS_GPIO_CLK, ENABLE);
/*!< SPI_FLASH_SPI Clock enable */
FLASH_SPI_CLK_INIT(FLASH_SPI_CLK, ENABLE);
// Set pin multiplexing
GPIO_PinAFConfig(FLASH_SPI_SCK_GPIO_PORT,FLASH_SPI_SCK_PINSOURCE,FLASH_SPI_SCK_AF);
GPIO_PinAFConfig(FLASH_SPI_MISO_GPIO_PORT,FLASH_SPI_MISO_PINSOURCE,FLASH_SPI_MISO_AF);
GPIO_PinAFConfig(FLASH_SPI_MOSI_GPIO_PORT,FLASH_SPI_MOSI_PINSOURCE,FLASH_SPI_MOSI_AF);
/*!< To configure SPI_FLASH_SPI Pin : SCK */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< To configure SPI_FLASH_SPI Pin : MISO */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< To configure SPI_FLASH_SPI Pin : MOSI */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/*!< To configure SPI_FLASH_SPI Pin : CS */
GPIO_InitStructure.GPIO_Pin = FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(FLASH_CS_GPIO_PORT, &GPIO_InitStructure);
/* Stop signal FLASH: CS Pin high level */
SPI_FLASH_CS_HIGH();
/* FLASH_SPI Mode configuration */
// FLASH chip Support SPI Pattern 0 And mode 3, Set it accordingly CPOL CPHA
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(FLASH_SPI, &SPI_InitStructure);
/* Can make FLASH_SPI */
SPI_Cmd(FLASH_SPI, ENABLE);
}
5、 ... and 、 Code —— Read FLASH Of ID
spi_flash.c:
/**
* @brief Read FLASH ID
* @param nothing
* @retval FLASH ID
*/
u32 SPI_FLASH_ReadID(void)
{
u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
/* Start communicating :CS Low level */
SPI_FLASH_CS_LOW();
/* send out JEDEC Instructions , Read ID */
SPI_FLASH_SendByte(W25X_JedecDeviceID);
/* Read a byte of data */
Temp0 = SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte of data */
Temp1 = SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte of data */
Temp2 = SPI_FLASH_SendByte(Dummy_Byte);
/* Stop communicating :CS High level */
SPI_FLASH_CS_HIGH();
/* Put the data together , As the return value of the function */
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return Temp;
}
/**
* @brief Read FLASH Device ID
* @param nothing
* @retval FLASH Device ID
*/
u32 SPI_FLASH_ReadDeviceID(void)
{
u32 Temp = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "RDID " instruction */
SPI_FLASH_SendByte(W25X_DeviceID);
SPI_FLASH_SendByte(Dummy_Byte);
SPI_FLASH_SendByte(Dummy_Byte);
SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
Temp = SPI_FLASH_SendByte(Dummy_Byte);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
return Temp;
}main.c:
/* 16M Serial flash W25Q128 initialization */
SPI_FLASH_Init();
/* obtain Flash Device ID */
DeviceID = SPI_FLASH_ReadDeviceID();
Delay( 200 );
/* obtain SPI Flash ID */
FlashID = SPI_FLASH_ReadID();
printf("\r\nFlashID is 0x%X, Manufacturer Device ID is 0x%X\r\n", FlashID, DeviceID);6、 ... and 、 Code —— Erase operation
/**
* @brief erase FLASH A sector
* @param SectorAddr: Address of the sector to be erased
* @retval nothing
*/
void SPI_FLASH_SectorErase(u32 SectorAddr)
{
/* send out FLASH Write enabling commands */
SPI_FLASH_WriteEnable();
SPI_FLASH_WaitForWriteEnd();
/* Erase sectors */
/* choice FLASH: CS Low level */
SPI_FLASH_CS_LOW();
/* Send sector erase command */
SPI_FLASH_SendByte(W25X_SectorErase);
/* Send the high bit of the erase sector address */
SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
/* Send the median of the erase sector address */
SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
/* Send the low order of the erase sector address */
SPI_FLASH_SendByte(SectorAddr & 0xFF);
/* Stop signal FLASH: CS High level */
SPI_FLASH_CS_HIGH();
/* Wait until the erase is complete */
SPI_FLASH_WaitForWriteEnd();
}
/**
* @brief erase FLASH A sector , Erase the whole piece
* @param nothing
* @retval nothing
*/
void SPI_FLASH_BulkErase(void)
{
/* send out FLASH Write enabling commands */
SPI_FLASH_WriteEnable();
/* Whole block Erase */
/* choice FLASH: CS Low level */
SPI_FLASH_CS_LOW();
/* Send a block erase instruction */
SPI_FLASH_SendByte(W25X_ChipErase);
/* Stop signal FLASH: CS High level */
SPI_FLASH_CS_HIGH();
/* Wait until the erase is complete */
SPI_FLASH_WaitForWriteEnd();
}
7、 ... and 、 Code —— Read and write operations
8、 ... and 、 Code —— Routines and stored decimals
边栏推荐
- 为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
- HCIP笔记十二天
- 基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
- How to install govendor and open a project
- What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
- [knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification
- ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
- 枚举类和魔术值
- EasyUI drop-down box, add and put on and off shelves of products
- 大型仿人机器人的技术难点和应用情况
猜你喜欢

EasyUI drop-down box, add and put on and off shelves of products

【目标检测】YOLOv5跑通VOC2007数据集(修复版)
![Sogou batch push software - Sogou batch push tool [2022 latest]](/img/87/d89c8d301743d1087d001a4f97de02.jpg)
Sogou batch push software - Sogou batch push tool [2022 latest]

Hcip notes 11 days

Don't believe these "rumors" in the process of preparing for the exam!

Mindoc makes mind map

Rainbow plug-in extension: monitor MySQL based on MySQL exporter

Outlook 教程,如何在 Outlook 中搜索日历项?

Jenkins' role based authorization strategy installation configuration

Chapter III data types and variables
随机推荐
HCIP笔记十二天
为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
方正期货网上开户靠谱吗,开户安全吗?
Solution for win10 device management not recognizing gtx1080ti display device
Roson的Qt之旅#99 QML表格控件-TableView
简述redis集群的实现原理
7. Dependency injection
7.依赖注入
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
第六章 继承
[cloud co creation] explore how gaussdb helps ICBC create core financial data
Is the online account opening of Founder futures reliable and safe?
[book club issue 13] +ffmpeg open source project
Jenkins' file parameters can be used to upload files
[MySQL] takes you to the database
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
2D semantic segmentation -- deeplabv3plus reproduction
华泰vip账户证券开户安全吗
枚举类和魔术值
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification