基于HAL库的SPI学习总结
背景
主芯片使用的是STM32F429IGTX,从机是W25Q128(flash)。
实验现象
开发板复位后显示“flash_id ok”,按下key0按键后,向W25Q128(flash)的0地址中写入一串字符转“flash test”,再从芯片的0地址中读入该字符串,用printf在串口助手显示。
CubeMX配置
- 时钟配置,使能RCC异步通信
- 使能HSE,将SYSCLK和HCLK配置为180MHz,其他默认
- key0按键对应的引脚为PA4,所以将PA4设置为GPIO_Input
- 配置USART1
- 配置SPI2
- 片选信号使用代码控制,所以需要将PB12设置为GPIO_Output,并且默认设置为高电平
- 勾选如下配置
- ## 封装SPI2的发送和接收函数
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 RxData = 0;
HAL_SPI_TransmitReceive(&hspi2, &TxData, &RxData, 1, 2000);
return RxData;
}
添加FLASH文件
- flash.c
#include "flash.h"
// 读状态寄存器
u8 FLASH_ReadSR(void)
{
// 接收状态寄存器返回的状态值的变量
u8 temp = 0;
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 写读状态寄存器的指令0x05
SPI2_ReadWriteByte(0x05);
// 接收状态寄存器返回的状态值
temp = SPI2_ReadWriteByte(0xff);
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
return temp;
}
u16 FLASH_ReadID(void)
{
u16 FlashID = 0;
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 发送读ID指令0x90
SPI2_ReadWriteByte(0x90);
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
// 读ID
FlashID |= (SPI2_ReadWriteByte(0xff) << 8);
FlashID |= SPI2_ReadWriteByte(0xff);
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
return FlashID;
}
void FLASH_Read(u8 *pBuffer, u32 ReadAddr, u16 len)
{
// 后面的for循环用得着
int i=0;
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 发送读数据指令0x03
SPI2_ReadWriteByte(0x03);
SPI2_ReadWriteByte((ReadAddr>>16) & 0xff);
SPI2_ReadWriteByte((ReadAddr>>8) & 0xff);
SPI2_ReadWriteByte((ReadAddr>>0) & 0xff);
// 读取SPI数据
for(i=0; i<len; i++)
{
pBuffer[i] = SPI2_ReadWriteByte(0xff);
}
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
}
void FLASH_Wait_Buzy(void)
{
//判断第一个bit,如果为1 表示繁忙,否则表示不忙,不忙就可以擦除和写操作
while((FLASH_ReadSR() & 0x01) == 1);
}
// FLASH写使能
void FLASH_Write_ENBLE(void)
{
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 发送写使能指令0x03
SPI2_ReadWriteByte(0x03);
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
}
/* 擦除要写“页”所在的扇区*/
void FLASH_EraseSector(u32 Dsr_Addr)
{
//按照扇区擦除时,将地址按“扇区”区对齐
//每个扇区4kB(4096B)
u8 n = Dsr_Addr / 4096;
Dsr_Addr = n * 4096;
// 等待FLASH是否空闲
FLASH_Wait_Buzy();
// 写使能
FLASH_Write_ENBLE();
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 发送按扇区擦除的指令0x20
SPI2_ReadWriteByte(0x20);
SPI2_ReadWriteByte((Dsr_Addr >> 16) & 0xff);
SPI2_ReadWriteByte((Dsr_Addr >> 8) & 0xff);
SPI2_ReadWriteByte((Dsr_Addr >> 0) & 0xff);
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
}
void FLASH_Write_Page(u8 *pBuffer, u32 WriteAddr, u16 NumberByteToWrite)
{
u8 i = 0;
u8 n = WriteAddr / 4096;
WriteAddr = n * 4096;
// 等待FLASH是否空闲
FLASH_Wait_Buzy();
// 写使能
FLASH_Write_ENBLE();
// 将SCK拉低
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
// 发送按扇区擦除的指令0x02
SPI2_ReadWriteByte(0x02);
SPI2_ReadWriteByte((WriteAddr >> 16) & 0xff);
SPI2_ReadWriteByte((WriteAddr >> 8) & 0xff);
SPI2_ReadWriteByte((WriteAddr >> 0) & 0xff);
for(i=0; i<NumberByteToWrite; i++)
{
SPI2_ReadWriteByte(pBuffer[i]); //发送一个字节的数据
}
// 将SCK拉高
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
}
void FLASH_Write(u8 *pBuffer, u32 WriteAddr, u16 len)
{
FLASH_EraseSector(WriteAddr); //调用擦除函数
FLASH_Write_Page(pBuffer, WriteAddr, len); //调用按页写函数
}
- flash.h
#ifndef __FLASH_H__
#define __FLASH_H__
#include "spi.h"
#define FLASH_ID 0xEF16
u8 FLASH_ReadSR(void);
u16 FLASH_ReadID(void);
void FLASH_Read(u8 *pBuffer, u32 ReadAddr, u16 len);
void FLASH_Wait_Buzy(void);
void FLASH_Write_ENBLE(void);
void FLASH_EraseSector(u32 Dsr_Addr);
void FLASH_Write_Page(u8 *pBuffer, u32 WriteAddr, u16 NumberByteToWrite);
void FLASH_Write(u8 *pBuffer, u32 WriteAddr, u16 len);
#endif
主函数 main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "flash.h"
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
u8 TextBuffer[] = "flash test";
u8 Databuffer[sizeof(TextBuffer)];
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI2_Init();
MX_USART1_UART_Init();
u16 flash_id;
flash_id = FLASH_ReadID();
if(FLASH_ID != flash_id)
{
printf("%x", flash_id);
printf("flash_id error");
}
printf("flash_id ok");
GPIO_PinState KeyValue;
while (1)
{
KeyValue = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
if(KeyValue == GPIO_PIN_RESET)
{
FLASH_Write(TextBuffer, 0, sizeof(TextBuffer));
FLASH_Read(Databuffer, 0, sizeof(TextBuffer));
printf("%s\r\n", Databuffer);
}
}
}
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 15;
RCC_OscInitStruct.PLL.PLLN = 216;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
注意
下图需要进行勾选,不然不能printf打印。