rt-thread-official/bsp/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_norflash.c

188 lines
7.2 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : SWM320_norflash.c
2021-09-13 21:04:42 +08:00
* : SWM320单片机的NOR Flash驱动程序
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
2021-05-06 10:42:41 +08:00
* :
2021-09-13 21:04:42 +08:00
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
2021-09-13 21:04:42 +08:00
* COPYRIGHT 2012 Synwit Technology
2021-02-18 13:29:12 +08:00
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_norflash.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_Init()
* : NOR Flash控制器初始化
2021-05-06 10:42:41 +08:00
* : NORFL_InitStructure * initStruct NOR Flash控制器相关设定值的结构体
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void NORFL_Init(NORFL_InitStructure *initStruct)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t i;
// 配置SRAM前需要刷新下SDRAM控制器
do
{
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
while (SDRAMC->REFDONE == 0)
;
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
for (i = 0; i < 1000; i++)
__NOP();
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
} while (0);
SYS->CLKEN |= (1 << SYS_CLKEN_NORFL_Pos);
NORFLC->CR = ((initStruct->DataWidth == 8 ? 1 : 0) << NORFLC_CR_BYTEIF_Pos) |
(initStruct->WELowPulseTime << NORFLC_CR_WRTIME_Pos) |
(initStruct->OEPreValidTime << NORFLC_CR_RDTIME_Pos);
NORFLC->IE = 3;
NORFLC->IF = 3; // 清除中断标志
if (initStruct->OperFinishIEn)
NORFLC->IM &= ~(1 << NORFLC_IM_FINISH_Pos);
else
NORFLC->IM |= (1 << NORFLC_IM_FINISH_Pos);
if (initStruct->OperTimeoutIEn)
NORFLC->IM &= ~(1 << NORFLC_IM_TIMEOUT_Pos);
else
NORFLC->IM |= (1 << NORFLC_IM_TIMEOUT_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_ChipErase()
* : NOR Flash整片擦除
2021-05-06 10:42:41 +08:00
* :
2021-09-13 21:04:42 +08:00
* : uint32_t 0 1
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_ChipErase(void)
{
2021-05-06 10:42:41 +08:00
uint32_t res;
NORFLC->CMD = (NORFL_CMD_CHIP_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_SectorErase()
* : NOR Flash扇区擦除
* : uint32_t addr
* : uint32_t 0 1
2021-05-06 10:42:41 +08:00
* : MX29LV128DB 88K25564K MX29LV128DT 25564K88K
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_SectorErase(uint32_t addr)
{
2021-05-06 10:42:41 +08:00
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_SECTOR_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_Write()
* : NOR Flash写
* : uint32_t addr
* uint32_t data
* : uint32_t 0 1
2021-05-06 10:42:41 +08:00
* : 线16线8
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_Write(uint32_t addr, uint32_t data)
{
2021-05-06 10:42:41 +08:00
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_PROGRAM << NORFLC_CMD_CMD_Pos) | (data << NORFLC_CMD_DATA_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_Read()
* : NOR Flash读
* : uint32_t addr
* : uint32_t
2021-05-06 10:42:41 +08:00
* : 线16线8
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_Read(uint32_t addr)
{
2021-05-06 10:42:41 +08:00
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
return (NORFLC->CMD & NORFLC_CMD_DATA_Msk);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : NORFL_ReadID()
* : NOR Flash读ID
* : uint32_t id_addr ID地址
* : uint16_t ID
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint16_t NORFL_ReadID(uint32_t id_addr)
{
2021-05-06 10:42:41 +08:00
uint16_t id;
NORFLC->CMD = (NORFL_CMD_AUTO_SELECT << NORFLC_CMD_CMD_Pos);
NORFLC->ADDR = id_addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
id = NORFLC->CMD & NORFLC_CMD_DATA_Msk;
NORFLC->CMD = (NORFL_CMD_RESET << NORFLC_CMD_CMD_Pos); // 退出ID读取模式
return id;
2021-02-18 13:29:12 +08:00
}