历史上的今天
今天是:2025年05月14日(星期三)
2020年05月14日 | STM32CubeMX学习--(5)SPI读写W25Q128
2020-05-14 来源:eefocus
CUBE配置
SPI配置
引脚配置
参数配置
点击生成代码
代码修改
uint8_t Data1[4]={0x90,0x00,0x00,0x00};
uint8_t Data2[2]={0x00,0x00};
uint8_t RxData[2]={0x00,0x00};
/****************************/
while (1)
{
HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port,SPI1_NSS_Pin,GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1,Data1,4,100);
HAL_SPI_TransmitReceive(&hspi1,Data2,RxData,2,100);
HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port,SPI1_NSS_Pin,GPIO_PIN_SET);
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
上述程序发送读取W25Q128的ID,Debug下看到RxData为{0xEF,0x17}。即为其ID号,说明SPI读取成功,接下来根据W25Q128手册编写应用程序即可。
添加W25Q128代码
W25Qxx.c
/*********************************************************************************************************
*
* File : ws_W25Qx.c
* Hardware Environment:
* Build Environment : RealView MDK-ARM Version: 4.20
* Version : V1.0
* By :
*
* (c) Copyright 2005-2011, WaveShare
* http://www.waveshare.net
* All Rights Reserved
*
*********************************************************************************************************/
#include "W25QXX.h"
/**
* @brief Initializes the W25Q128FV interface.
* @retval None
*/
uint8_t BSP_W25Qx_Init(void)
{
/* Reset W25Qxxx */
BSP_W25Qx_Reset();
return BSP_W25Qx_GetStatus();
}
/**
* @brief This function reset the W25Qx.
* @retval None
*/
static void BSP_W25Qx_Reset(void)
{
uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};
W25Qx_Enable();
/* Send the reset command */
HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
}
/**
* @brief Reads current status of the W25Q128FV.
* @retval W25Q128FV memory status
*/
static uint8_t BSP_W25Qx_GetStatus(void)
{
__align(4) uint8_t cmd[] = {READ_STATUS_REG1_CMD};
uint8_t status;
W25Qx_Enable();
/* Send the read status command */
HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
/* Check the value of the register */
if((status & W25Q128FV_FSR_BUSY) != 0)
{
return W25Qx_BUSY;
}
return W25Qx_OK;
}
/**
* @brief This function send a Write Enable and wait it is effective.
* @retval None
*/
uint8_t BSP_W25Qx_WriteEnable(void)
{
uint8_t cmd[] = {WRITE_ENABLE_CMD};
uint32_t tickstart = HAL_GetTick();
/*Select the FLASH: Chip Select low */
W25Qx_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
{
return W25Qx_TIMEOUT;
}
}
return W25Qx_OK;
}
/**
* @brief Read Manufacture/Device ID.
* @param return value address
* @retval None
*/
void BSP_W25Qx_Read_ID(uint8_t *ID)
{
uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};
W25Qx_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
}
/**
* @brief Reads an amount of data from the QSPI memory.
* @param pData: Pointer to data to be read
* @param ReadAddr: Read start address
* @param Size: Size of data to read
* @retval QSPI memory status
*/
uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
{
uint8_t cmd[4];
/* Configure the command */
cmd[0] = READ_CMD;
cmd[1] = (uint8_t)(ReadAddr >> 16);
cmd[2] = (uint8_t)(ReadAddr >> 8);
cmd[3] = (uint8_t)(ReadAddr);
W25Qx_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
W25Qx_Disable();
return W25Qx_OK;
}
/**
* @brief Writes an amount of data to the QSPI memory.
* @param pData: Pointer to data to be written
* @param WriteAddr: Write start address
* @param Size: Size of data to write,No more than 256byte.
* @retval QSPI memory status
*/
uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
{
uint8_t cmd[4];
uint32_t end_addr, current_size, current_addr;
uint32_t tickstart = HAL_GetTick();
/* Calculation of the size between the write address and the end of the page */
current_addr = 0;
while (current_addr <= WriteAddr)
{
current_addr += W25Q128FV_PAGE_SIZE;
}
current_size = current_addr - WriteAddr;
/* Check if the size of the data is less than the remaining place in the page */
if (current_size > Size)
{
current_size = Size;
}
/* Initialize the adress variables */
current_addr = WriteAddr;
end_addr = WriteAddr + Size;
/* Perform the write page by page */
do
{
/* Configure the command */
cmd[0] = PAGE_PROG_CMD;
cmd[1] = (uint8_t)(current_addr >> 16);
cmd[2] = (uint8_t)(current_addr >> 8);
cmd[3] = (uint8_t)(current_addr);
/* Enable write operations */
BSP_W25Qx_WriteEnable();
W25Qx_Enable();
/* Send the command */
if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
/* Transmission of the data */
if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
{
return W25Qx_TIMEOUT;
}
}
/* Update the address and size variables for next page programming */
current_addr += current_size;
pData += current_size;
current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE;
} while (current_addr < end_addr);
return W25Qx_OK;
}
/**
* @brief Erases the specified block of the QSPI memory.
* @param BlockAddress: Block address to erase
* @retval QSPI memory status
*/
uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
{
uint8_t cmd[4];
uint32_t tickstart = HAL_GetTick();
cmd[0] = SECTOR_ERASE_CMD;
cmd[1] = (uint8_t)(Address >> 16);
cmd[2] = (uint8_t)(Address >> 8);
cmd[3] = (uint8_t)(Address);
/* Enable write operations */
BSP_W25Qx_WriteEnable();
/*Select the FLASH: Chip Select low */
W25Qx_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME)
{
return W25Qx_TIMEOUT;
}
}
return W25Qx_OK;
}
/**
* @brief Erases the entire QSPI memory.This function will take a very long time.
* @retval QSPI memory status
*/
uint8_t BSP_W25Qx_Erase_Chip(void)
{
__align(4) uint8_t cmd[4];
uint32_t tickstart = HAL_GetTick();
cmd[0] = SECTOR_ERASE_CMD;
/* Enable write operations */
BSP_W25Qx_WriteEnable();
/*Select the FLASH: Chip Select low */
W25Qx_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() != W25Qx_BUSY);
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME)
{
return W25Qx_TIMEOUT;
}
}
return W25Qx_OK;
}
W25Qxx.h
/*********************************************************************************************************
*
* File : W25Qx.h
* Hardware Environment:
* Build Environment : RealView MDK-ARM Version: 5.15
* Version : V1.0
* By :
*
* (c) Copyright 2005-2015, WaveShare
* http://www.waveshare.net
* All Rights Reserved
*
*********************************************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
史海拾趣
|
大功率LED驱动电路(AC—DC)设计实例(f非常实用具体) 大功率LED驱动电路(AC—DC)设计实例(f非常实用具体),主要是不需要变压器;直接接交流电源。 感兴趣的朋友可以看看… 查看全部问答> |
|
EVC或vc2005如何创建一个带有自定义类和该类方法的lib,以及如何开发使用和部署到pda上? EVC或vc2005如何创建一个带有自定义类和该类方法的lib,以及如何开发使用和部署到pda上?… 查看全部问答> |
|
刚刚作windowce的应用开发。发现完全安装EVC4.0+SP4,平台winCE5.0,s3c2410。需要选择arm4i,选择cpu平台中没有ARMv4I,只有armv4和emulator的选择。需要安装什么包吗?安装EVC的时候明明选择了其他cpu的,怪!请知道的朋友不吝赐教! 谢谢!… 查看全部问答> |
|
本人目前正在学习有关驱动设计方面的知识,觉得 还不错,尤其是配套光盘里面的例子和工具。希望哪位拥有的xdjm能发一份给我,不甚感激! 我的email是:xuefang2402@yahoo.com.cn… 查看全部问答> |




