rt-thread-official/bsp/swm320-lq100/Libraries/SWM320_StdPeriph_Driver/SWM320_norflash.c

188 lines
7.2 KiB
C
Raw Normal View History

2018-12-24 17:17:27 +08:00
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : SWM320_norflash.c
* : SWM320单片机的NOR Flash驱动程序
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
2018-12-24 17:17:27 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
* 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
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_norflash.h"
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_Init()
* : NOR Flash控制器初始化
* : NORFL_InitStructure * initStruct NOR Flash控制器相关设定值的结构体
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void NORFL_Init(NORFL_InitStructure *initStruct)
{
uint32_t i;
2021-09-30 17:55:16 +08:00
// 配置SRAM前需要刷新下SDRAM控制器
2018-12-24 17:17:27 +08:00
do
{
SYS->CLKEN |= (1 << SYS_CLKEN_SDRAM_Pos);
2021-09-30 17:55:16 +08:00
while (SDRAMC->REFDONE == 0)
;
2018-12-24 17:17:27 +08:00
SDRAMC->REFRESH &= ~(1 << SDRAMC_REFRESH_EN_Pos);
2021-09-30 17:55:16 +08:00
for (i = 0; i < 1000; i++)
__NOP();
2018-12-24 17:17:27 +08:00
SYS->CLKEN &= ~(1 << SYS_CLKEN_SDRAM_Pos);
2021-09-30 17:55:16 +08:00
} while (0);
2018-12-24 17:17:27 +08:00
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;
2021-09-30 17:55:16 +08:00
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);
2018-12-24 17:17:27 +08:00
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_ChipErase()
* : NOR Flash整片擦除
* :
* : uint32_t 0 1
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_ChipErase(void)
{
uint32_t res;
NORFLC->CMD = (NORFL_CMD_CHIP_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
2021-09-30 17:55:16 +08:00
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
2018-12-24 17:17:27 +08:00
2021-09-30 17:55:16 +08:00
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
2018-12-24 17:17:27 +08:00
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_SectorErase()
* : NOR Flash扇区擦除
* : uint32_t addr
* : uint32_t 0 1
* : MX29LV128DB 88K25564K MX29LV128DT 25564K88K
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_SectorErase(uint32_t addr)
{
uint32_t res;
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_SECTOR_ERASE << NORFLC_CMD_CMD_Pos);
while (((NORFLC->IF & NORFLC_IF_FINISH_Msk) == 0) &&
2021-09-30 17:55:16 +08:00
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
2018-12-24 17:17:27 +08:00
2021-09-30 17:55:16 +08:00
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
2018-12-24 17:17:27 +08:00
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_Write()
* : NOR Flash写
* : uint32_t addr
* uint32_t data
* : uint32_t 0 1
* : 线16线8
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_Write(uint32_t addr, uint32_t data)
{
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) &&
2021-09-30 17:55:16 +08:00
((NORFLC->IF & NORFLC_IF_TIMEOUT_Msk) == 0))
__NOP();
2018-12-24 17:17:27 +08:00
2021-09-30 17:55:16 +08:00
if (NORFLC->IF & NORFLC_IF_FINISH_Msk)
res = 0;
else
res = 1;
2018-12-24 17:17:27 +08:00
NORFLC->IF = NORFLC_IF_FINISH_Msk | NORFLC_IF_TIMEOUT_Msk;
return res;
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_Read()
* : NOR Flash读
* : uint32_t addr
* : uint32_t
* : 线16线8
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t NORFL_Read(uint32_t addr)
{
NORFLC->ADDR = addr;
NORFLC->CMD = (NORFL_CMD_READ << NORFLC_CMD_CMD_Pos);
return (NORFLC->CMD & NORFLC_CMD_DATA_Msk);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : NORFL_ReadID()
* : NOR Flash读ID
* : uint32_t id_addr ID地址
* : uint16_t ID
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint16_t NORFL_ReadID(uint32_t id_addr)
{
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;
2021-09-30 17:55:16 +08:00
NORFLC->CMD = (NORFL_CMD_RESET << NORFLC_CMD_CMD_Pos); // 退出ID读取模式
2018-12-24 17:17:27 +08:00
return id;
}