#include "includes.h"
#include "bsp_Extflash.h"
extern SPI_HandleTypeDef hspi3;
static void MX25L64_Enable(void)
{
GPIOB->BSRR = (uint32_t)GPIO_PIN_5 << 16;
}
static void MX25L64_Disable(void)
{
GPIOB->BSRR = GPIO_PIN_5;
}
/**
* @brief Read Manufacture/Device ID.
* @param return value address
* @retval None
*/
void BSP_MX25L64_Read_ID(uint8_t *ID)
{
uint8_t cmd[4] = {0x9F,0x00,0x00,0x00};
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 1, MX25L64_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi3,ID, 3, MX25L64_TIMEOUT_VALUE);
MX25L64_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_MX25L64_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
{
uint8_t cmd[4];
/* Configure the command */
cmd[0] = 0x03;
cmd[1] = (uint8_t)(ReadAddr >> 16);
cmd[2] = (uint8_t)(ReadAddr >> 8);
cmd[3] = (uint8_t)(ReadAddr);
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 4, MX25L64_TIMEOUT_VALUE);
/* Reception of the data */
if (HAL_SPI_Receive(&hspi3, pData,Size,MX25L64_TIMEOUT_VALUE) != HAL_OK)
{
return MX25L64_ERROR;
}
MX25L64_Disable();
return MX25L64_OK;
}
/**
* @brief Reads current status of the W25Q128FV.
* @retval W25Q128FV memory status
*/
static uint8_t BSP_MX25L64_GetStatus(void)
{
__align(4) uint8_t cmd[] = {0x05};
uint8_t status;
MX25L64_Enable();
/* Send the read status command */
HAL_SPI_Transmit(&hspi3, cmd, 1, MX25L64_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi3,&status, 1, MX25L64_TIMEOUT_VALUE);
MX25L64_Disable();
/* Check the value of the register */
if((status & MX25L64_FSR_BUSY) != 0)
{
return MX25L64_BUSY;
}
return MX25L64_OK;
}
/**
* @brief This function send a Write Enable and wait it is effective.
* @retval None
*/
static uint8_t BSP_MX25L64_WriteEnable(void)
{
uint8_t cmd[] = {0x06};
uint32_t tickstart = HAL_GetTick();
/*Select the FLASH: Chip Select low */
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 1, MX25L64_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
MX25L64_Disable();
/* Wait the end of Flash writing */
while(BSP_MX25L64_GetStatus() == MX25L64_BUSY)
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > MX25L64_TIMEOUT_VALUE)
{
return MX25L64_TIMEOUT;
}
}
return MX25L64_OK;
}
/**
* @brief Erases the specified sector of the QSPI memory. (4k bytes)
* @param BlockAddress: Sector address to erase
* @retval QSPI memory status
*/
uint8_t BSP_MX25L64_Erase_Sector(uint32_t Address)
{
uint8_t cmd[4];
uint32_t tickstart = HAL_GetTick();
cmd[0] = 0x20;
cmd[1] = (uint8_t)(Address >> 16);
cmd[2] = (uint8_t)(Address >> 8);
cmd[3] = (uint8_t)(Address);
/* Enable write operations */
BSP_MX25L64_WriteEnable();
/*Select the FLASH: Chip Select low */
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 4, MX25L64_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
MX25L64_Disable();
/* Wait the end of Flash writing */
while(BSP_MX25L64_GetStatus() == MX25L64_BUSY)
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > MX25L64_SECTOR_ERASE_MAX_TIME)
{
return MX25L64_TIMEOUT;
}
}
return MX25L64_OK;
}
/**
* @brief Erases the specified block of the QSPI memory. (64k bytes)
* @param BlockAddress: Block address to erase
* @retval QSPI memory status
*/
uint8_t BSP_MX25L64_Erase_Block(uint32_t Address)
{
uint8_t cmd[4];
uint32_t tickstart = HAL_GetTick();
cmd[0] = 0xD8;
cmd[1] = (uint8_t)(Address >> 16);
cmd[2] = (uint8_t)(Address >> 8);
cmd[3] = (uint8_t)(Address);
/* Enable write operations */
BSP_MX25L64_WriteEnable();
/*Select the FLASH: Chip Select low */
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 4, MX25L64_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
MX25L64_Disable();
/* Wait the end of Flash writing */
while(BSP_MX25L64_GetStatus() == MX25L64_BUSY)
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > MX25L64_BULK_ERASE_MAX_TIME)
{
return MX25L64_TIMEOUT;
}
}
return MX25L64_OK;
}
uint8_t BSP_MX25L64_Erase_Chip(void)
{
uint8_t cmd[1];
uint32_t tickstart = HAL_GetTick();
cmd[0] = 0x60;
/* Enable write operations */
BSP_MX25L64_WriteEnable();
/*Select the FLASH: Chip Select low */
MX25L64_Enable();
/* Send the read ID command */
HAL_SPI_Transmit(&hspi3, cmd, 1, MX25L64_TIMEOUT_VALUE);
/*Deselect the FLASH: Chip Select high */
MX25L64_Disable();
/* Wait the end of Flash writing */
while(BSP_MX25L64_GetStatus() == MX25L64_BUSY)
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > MX25L64_BULK_ERASE_MAX_TIME)
{
return MX25L64_TIMEOUT;
}
}
return MX25L64_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_MX25L64_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 += MX25L64_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] = 0x02;
cmd[1] = (uint8_t)(current_addr >> 16);
cmd[2] = (uint8_t)(current_addr >> 8);
cmd[3] = (uint8_t)(current_addr);
/* Enable write operations */
BSP_MX25L64_WriteEnable();
MX25L64_Enable();
/* Send the command */
if (HAL_SPI_Transmit(&hspi3,cmd, 4, MX25L64_TIMEOUT_VALUE) != HAL_OK)
{
return MX25L64_ERROR;
}
/* Transmission of the data */
if (HAL_SPI_Transmit(&hspi3, pData,current_size, MX25L64_TIMEOUT_VALUE) != HAL_OK)
{
return MX25L64_ERROR;
}
MX25L64_Disable();
/* Wait the end of Flash writing */
while(BSP_MX25L64_GetStatus() == MX25L64_BUSY)
{
/* Check for the Timeout */
if((HAL_GetTick() - tickstart) > MX25L64_TIMEOUT_VALUE)
{
return MX25L64_TIMEOUT;
}
}
/* Update the address and size variables for next page programming */
current_addr += current_size;
pData += current_size;
current_size = ((current_addr + MX25L64_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : MX25L64_PAGE_SIZE;
} while (current_addr < end_addr);
return MX25L64_OK;
}
#if 1
void SPI_Flash_Demo(void)
{
uint8_t id[3];
uint8_t wData[0x100];
uint8_t rData[0x100];
int i;
#if 1
BSP_MX25L64_Read_ID(id);
printf(" MX25L64 ID is : 0
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
bsp_Extflash.rar (2个子文件)
bsp_Extflash.c 9KB
bsp_Extflash.h 1KB
共 2 条
- 1
资源评论
empshan
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 案例分析:研发人员绩效和薪酬管理的困境.doc
- 企业中薪酬管理存在的问题分析及对策.doc
- 员工年度薪酬收入结构分析报告.doc
- 薪酬分析报告.docx
- 西门子S7-1200控制四轴伺服程序案例: 1.内容涵盖伺服,步进点动,回原,相对定位,绝对定位,速度模式控制 特别适合学习伺服和步进的朋友们 PTO伺服轴脉冲定位控制+速度模式控制+扭矩模式; 2
- 企业公司薪酬保密协议.doc
- 薪酬保密制度 (1).docx
- 薪酬保密管理规定制度.doc
- 薪酬保密制度.docx
- 薪酬保密协议书.docx
- 薪酬保密承诺书.docx
- 薪酬管理制度.doc
- 员工工资薪酬保密协议.docx
- 员工工资保密暂行管理条例.docx
- 员工薪酬保密协议.doc
- 1Redis基础认识与安装.html
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功