2022-10-19 22:44:15 +08:00

173 lines
6.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************************************************************************
* <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>: SWM320_norflash.c
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: SWM320<32><30>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>NOR Flash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>: http://www.synwit.com.cn/e/tool/gbook/?bid=1
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
* <20><EFBFBD><E6B1BE><EFBFBD><EFBFBD>: V1.1.0 2017<31><37>10<31><30>25<32><35>
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼:
*
*
*******************************************************************************************************************************************
* @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"
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_Init()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
* <20><> <20><>: NORFL_InitStructure * initStruct <20><><EFBFBD><EFBFBD>NOR Flash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨ֵ<E8B6A8>Ľṹ<C4BD><E1B9B9>
* <20><> <20><>: <20><>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
void NORFL_Init(NORFL_InitStructure * initStruct)
{
uint32_t i;
// <20><><EFBFBD><EFBFBD>SRAMǰ<4D><C7B0>Ҫˢ<D2AA><CBA2><EFBFBD><EFBFBD>SDRAM<41><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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; // <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
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);
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_ChipErase()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flash<73><68>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>
* <20><> <20><>: <20><>
* <20><> <20><>: uint32_t 0 <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
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) &&
((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;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_SectorErase()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><> <20><>: uint32_t addr Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ
* <20><> <20><>: uint32_t 0 <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: MX29LV128DB ǰ8<C7B0><38><EFBFBD><EFBFBD>Ϊ8K<38><4B><EFBFBD><EFBFBD>255<35><35><EFBFBD><EFBFBD>Ϊ64K MX29LV128DT ǰ255<35><35><EFBFBD><EFBFBD>Ϊ64K<34><4B><EFBFBD><EFBFBD>8<EFBFBD><38><EFBFBD><EFBFBD>Ϊ8K
******************************************************************************************************************************************/
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) &&
((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;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_Write()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flashд
* <20><> <20><>: uint32_t addr <09><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD>ĵ<EFBFBD>ַ
* uint32_t data Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><> <20><>: uint32_t 0 д<><D0B4><EFBFBD>ɹ<EFBFBD> 1 д<>볬ʱ
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ16λʱ<CEBB><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EBA3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ8λʱ<CEBB><CAB1><EFBFBD>ֽ<EFBFBD>д<EFBFBD><D0B4>
******************************************************************************************************************************************/
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) &&
((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;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_Read()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flash<73><68>
* <20><> <20><>: uint32_t addr <09><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* <20><> <20><>: uint32_t <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ16λʱ<CEBB><CAB1><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ8λʱ<CEBB><CAB1><EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>
******************************************************************************************************************************************/
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);
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: NORFL_ReadID()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: NOR Flash<73><68>ID
* <20><> <20><>: uint32_t id_addr ID<49><44>ַ<EFBFBD><D6B7><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>оƬ<D0BE><C6AC><EFBFBD>صģ<D8B5>ÿ<EFBFBD><C3BF>оƬ<D0BE><C6AC><EFBFBD><EFBFBD>ͬ
* <20><> <20><>: uint16_t <09><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ID
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
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;
NORFLC->CMD = (NORFL_CMD_RESET << NORFLC_CMD_CMD_Pos); // <20>˳<EFBFBD>ID<49><44>ȡģʽ
return id;
}