当前位置:网站首页>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
边栏推荐
- [target detection] yolov5 Runtong voc2007 dataset (repair version)
- win10如何删除微软拼音输入法
- 【redis】redis安装
- 为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
- Add batch delete
- 简述redis集群的实现原理
- GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
- Go language series: where does go come from and where will go?
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- 【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测
猜你喜欢

Automatic reply of wechat official account development message

第三章、数据类型和变量

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

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

文字翻译软件-文字批量翻译转换器免费

7.依赖注入

Redis cluster deployment based on redis6.2.4

ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性

EasyUI modification and DataGrid dialog form control use

一百个用户眼中,就有一百个QQ
随机推荐
文字翻译软件-文字批量翻译转换器免费
Unity is better to use the hot scheme Wolong
EasyUI DataGrid control uses
Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
Enterprise live broadcast: witness focused products, praise and embrace ecology
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
博云容器云、DevOps平台斩获可信云“技术最佳实践奖”
mindoc制作思维导图
[PHP pseudo protocol] source code reading, file reading and writing, and arbitrary PHP command execution
postgreSQL 密码区分大小写 ,有参数控制吗?
Dynamic planning topic record
win10自带的框选截图快捷键
Outlook tutorial, how to search for calendar items in outlook?
Homepage portal classification query
如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序
搜狗批量推送软件-搜狗批量推送工具【2022最新】
EasyUI modification and DataGrid dialog form control use
We were tossed all night by a Kong performance bug
Outlook 教程,如何在 Outlook 中搜索日历项?