SN32: update flash driver for 240b (#8)
* sn32 hal: update flash from latest CMSIS * sn32: build flash * sn32: fix compilation * sn32 flash: add half word flashing * sn32 flash: adaptation time
This commit is contained in:
parent
34f2c22327
commit
36d503985f
|
@ -1,90 +1,137 @@
|
|||
/******************** (C) COPYRIGHT 2013 SONiX *******************************
|
||||
* COMPANY: SONiX
|
||||
* DATE: 2013/12
|
||||
* AUTHOR: SA1
|
||||
* IC: SN32F240/230/220
|
||||
* DESCRIPTION: Flash related functions.
|
||||
*____________________________________________________________________________
|
||||
* REVISION Date User Description
|
||||
* 1.0 2013/12/17 SA1 First release
|
||||
*
|
||||
*____________________________________________________________________________
|
||||
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||||
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
|
||||
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL
|
||||
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
|
||||
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN
|
||||
* IN CONNECTION WITH THEIR PRODUCTS.
|
||||
*****************************************************************************/
|
||||
|
||||
/*_____ I N C L U D E S ____________________________________________________*/
|
||||
#include "Flash.h"
|
||||
|
||||
|
||||
/*_____ D E C L A R A T I O N S ____________________________________________*/
|
||||
uint32_t wFLASH_PGRAM[2];
|
||||
|
||||
|
||||
/*_____ D E F I N I T I O N S ______________________________________________*/
|
||||
|
||||
|
||||
/*_____ M A C R O S ________________________________________________________*/
|
||||
|
||||
|
||||
/*_____ F U N C T I O N S __________________________________________________*/
|
||||
|
||||
/*****************************************************************************
|
||||
* Function : FLASH_EraseSector
|
||||
* Description : Erase assigned sector address in Flash ROM
|
||||
* Input : adr - Sector start address
|
||||
* Output : None
|
||||
* Return : None
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
void FLASH_EraseSector (uint32_t adr)
|
||||
{
|
||||
SN_FLASH->CTRL = FLASH_PER; // Page Erase Enabled
|
||||
SN_FLASH->ADDR = adr; // Page Address
|
||||
SN_FLASH->CTRL |= FLASH_STARTE; // Start Erase
|
||||
|
||||
while ((SN_FLASH->STATUS & FLASH_BUSY) == FLASH_BUSY);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Function : Flash_ProgramPage
|
||||
* Description : Program assigned page in Flash ROM
|
||||
* Input : adr - Page start address (word-alignment) of Flash
|
||||
* sz - Content size to be programmed (Bytes)
|
||||
* pBuf - pointer to the Source data
|
||||
* Output : None
|
||||
* Return : OK or FAIL
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
uint32_t FLASH_ProgramPage (uint32_t adr, uint32_t sz, uint8_t *pBuf)
|
||||
{
|
||||
while (sz){
|
||||
|
||||
SN_FLASH->CTRL = FLASH_PG; // Programming Enabled
|
||||
SN_FLASH->ADDR = adr;
|
||||
SN_FLASH->DATA = *((uint32_t *)pBuf);
|
||||
|
||||
__nop();__nop();__nop();__nop();__nop();__nop(); //Must add to avoid Hard Fault!!!!!!
|
||||
|
||||
while ((SN_FLASH->STATUS & FLASH_BUSY) == FLASH_BUSY);
|
||||
|
||||
// Check for Errors
|
||||
if ((SN_FLASH->STATUS & FLASH_PGERR) == FLASH_PGERR) {
|
||||
SN_FLASH->STATUS &= ~FLASH_PGERR;
|
||||
return (FAIL);
|
||||
}
|
||||
|
||||
// Go to next Word
|
||||
adr += 4;
|
||||
pBuf += 4;
|
||||
sz -= 4;
|
||||
}
|
||||
|
||||
return (OK);
|
||||
}
|
||||
|
||||
/******************** (C) COPYRIGHT 2017 SONiX *******************************
|
||||
* COMPANY: SONiX
|
||||
* DATE: 2017/07
|
||||
* AUTHOR: SA1
|
||||
* IC: SN32F240B
|
||||
* DESCRIPTION: Flash related functions.
|
||||
*____________________________________________________________________________
|
||||
* REVISION Date User Description
|
||||
* 1.0 2017/07/07 SA1 First release
|
||||
*
|
||||
*____________________________________________________________________________
|
||||
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||||
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
|
||||
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL
|
||||
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
|
||||
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN
|
||||
* IN CONNECTION WITH THEIR PRODUCTS.
|
||||
*****************************************************************************/
|
||||
|
||||
/*_____ I N C L U D E S ____________________________________________________*/
|
||||
#include "Flash.h"
|
||||
|
||||
|
||||
/*_____ D E C L A R A T I O N S ____________________________________________*/
|
||||
uint32_t wFLASH_PGRAM[2];
|
||||
|
||||
|
||||
/*_____ D E F I N I T I O N S ______________________________________________*/
|
||||
|
||||
|
||||
/*_____ M A C R O S ________________________________________________________*/
|
||||
|
||||
|
||||
/*_____ F U N C T I O N S __________________________________________________*/
|
||||
/*****************************************************************************
|
||||
* Function : FLASH_EraseSector
|
||||
* Description : Erase assigned sector address in Flash ROM
|
||||
* Input : adr - Sector start address
|
||||
* Output : None
|
||||
* Return : None
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
FLASH_Status FLASH_EraseSector (uint32_t adr)
|
||||
{
|
||||
SN_FLASH->CTRL = FLASH_PER; // Page Erase Enabled
|
||||
SN_FLASH->ADDR = adr; // Page Address
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
SN_FLASH->CTRL |= FLASH_START; // Start Erase
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
return (FLASH_OKAY);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Function : Flash_ProgramPage
|
||||
* Description : Program assigned page in Flash ROM
|
||||
* Input : adr - Page start address (word-alignment) of Flash
|
||||
* sz - Content size to be programmed (Bytes)
|
||||
* Data - the Source data
|
||||
* Output : None
|
||||
* Return : FLASH_OKAY or FLASH_FAIL
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
FLASH_Status FLASH_ProgramPage (uint32_t adr, uint32_t sz, uint16_t Data)
|
||||
{
|
||||
SN_FLASH->CTRL = FLASH_PG; // Programming Enabled
|
||||
SN_FLASH->ADDR = adr;
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
*(uint32_t*)adr = Data;
|
||||
|
||||
while (sz){
|
||||
|
||||
SN_FLASH->DATA = *((uint32_t *)adr);
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
// Go to next Word
|
||||
adr += 4;
|
||||
sz -= 4;
|
||||
}
|
||||
|
||||
// Check for Errors
|
||||
if ((SN_FLASH->STATUS & FLASH_ERR) == FLASH_ERR) {
|
||||
SN_FLASH->STATUS = 0;
|
||||
return (FLASH_FAIL);
|
||||
}
|
||||
|
||||
SN_FLASH->CTRL |= FLASH_START; // Start Program
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
// Check for Errors
|
||||
if ((SN_FLASH->STATUS & FLASH_ERR) == FLASH_ERR) {
|
||||
SN_FLASH->STATUS = 0;
|
||||
return (FLASH_FAIL);
|
||||
}
|
||||
|
||||
return (FLASH_OKAY);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Function : FLASH_ProgramHalfWord
|
||||
* Description : Program a half word at a specified address
|
||||
* Input : adr - Page start address (word-alignment) of Flash
|
||||
* Data - the Source data
|
||||
* Output : None
|
||||
* Return : FLASH_OKAY or FLASH_ERR
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
FLASH_Status FLASH_ProgramHalfWord(uint32_t adr, uint16_t Data) {
|
||||
FLASH_Status status = FLASH_ProgramPage(adr, 2, Data);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Function : FLASH_Checksum
|
||||
* Description : Calculate Checksum in Flash ROM
|
||||
* Input : None
|
||||
* Output : Checksum of User ROM
|
||||
* Return : None
|
||||
* Note : None
|
||||
*****************************************************************************/
|
||||
uint16_t FLASH_Checksum (void)
|
||||
{
|
||||
SN_FLASH->CTRL = FLASH_CHK;
|
||||
|
||||
FLASH_WAIT_FOR_DONE
|
||||
|
||||
return (uint16_t) (SN_FLASH->CHKSUM);
|
||||
}
|
||||
|
|
|
@ -1,44 +1,49 @@
|
|||
#ifndef __SN32F240_FLASH_H
|
||||
#define __SN32F240_FLASH_H
|
||||
|
||||
/*_____ I N C L U D E S ____________________________________________________*/
|
||||
#include "SN32F240.h"
|
||||
#include "SN32F200_Def.h"
|
||||
|
||||
|
||||
/*_____ D E F I N I T I O N S ______________________________________________*/
|
||||
//FLASH HW
|
||||
#define FLASH_PAGE_SIZE 1024
|
||||
#define FLASH_F240_MAX_ROM_SIZE 0xFFFF
|
||||
#define FLASH_F230_MAX_ROM_SIZE 0x7FFF
|
||||
#define FLASH_F220_MAX_ROM_SIZE 0x3FFF
|
||||
|
||||
|
||||
// Flash Control Register definitions
|
||||
#define FLASH_PG 0x00000001
|
||||
#define FLASH_PER 0x00000002
|
||||
#define FLASH_STARTE 0x00000040
|
||||
|
||||
// Flash Status Register definitions
|
||||
#define FLASH_BUSY 0x00000001
|
||||
#define FLASH_PGERR 0x00000004
|
||||
|
||||
|
||||
/*_____ M A C R O S ________________________________________________________*/
|
||||
|
||||
//Flash Low Power Mode
|
||||
#define __FLASH_LPM_DISABLE SN_FLASH->LPCTRL = 0x5AFA0000
|
||||
#define __FLASH_LPM_SLOW_MODE SN_FLASH->LPCTRL = 0x5AFA0002
|
||||
|
||||
//Flash Status
|
||||
#define __FLASH_CLEAR_ERROR_STATUS SN_FLASH->STATUS &= ~FLASH_PGERR
|
||||
|
||||
|
||||
/*_____ D E C L A R A T I O N S ____________________________________________*/
|
||||
extern uint32_t wFLASH_PGRAM[2];
|
||||
|
||||
void FLASH_EraseSector (uint32_t);
|
||||
uint32_t FLASH_ProgramPage (uint32_t, uint32_t, uint8_t *);
|
||||
|
||||
|
||||
#endif /* __SN32F240_FLASH_H */
|
||||
#ifndef __SN32F240B_FLASH_H
|
||||
#define __SN32F240B_FLASH_H
|
||||
|
||||
/*_____ I N C L U D E S ____________________________________________________*/
|
||||
#include <SN32F240B.h>
|
||||
#include <SN32F200_Def.h>
|
||||
|
||||
|
||||
/*_____ D E F I N I T I O N S ______________________________________________*/
|
||||
//FLASH HW
|
||||
#define FLASH_PAGE_SIZE 64
|
||||
#define FLASH_F240B_MAX_ROM_SIZE 0xFFFF
|
||||
|
||||
|
||||
// Flash Control Register definitions
|
||||
#define FLASH_PG 0x00000001
|
||||
#define FLASH_PER 0x00000002
|
||||
#define FLASH_MER 0x00000004
|
||||
#define FLASH_START 0x00000040
|
||||
#define FLASH_CHK 0x00000080
|
||||
|
||||
// Flash Status Register definitions
|
||||
#define FLASH_BUSY 0x00000001
|
||||
#define FLASH_ERR 0x00000004
|
||||
|
||||
#define FLASH_WAIT_FOR_DONE while (SN_FLASH->STATUS & FLASH_BUSY) {__asm__("NOP");}
|
||||
|
||||
|
||||
/*_____ M A C R O S ________________________________________________________*/
|
||||
|
||||
//Flash Low Power Mode
|
||||
#define __FLASH_LPM_DISABLE SN_FLASH->LPCTRL = 0x5AFA0000; //HCLK<=12MHz
|
||||
#define __FLASH_LPM_MEDIUM_MODE SN_FLASH->LPCTRL = 0x5AFA0003; //12MHz<HCLK<=24MHz
|
||||
#define __FLASH_LPM_FAST_MODE SN_FLASH->LPCTRL = 0x5AFA0004;SN_FLASH->LPCTRL = 0x5AFA0005;SN_FLASH->LPCTRL = 0x5AFA000D; //HCLK>24MHz
|
||||
|
||||
//Flash Status
|
||||
#define __FLASH_CLEAR_ERROR_STATUS SN_FLASH->STATUS &= ~FLASH_PGERR
|
||||
|
||||
/*_____ D E C L A R A T I O N S ____________________________________________*/
|
||||
extern uint32_t wFLASH_PGRAM[2];
|
||||
typedef enum { FLASH_FAIL, FLASH_OKAY} FLASH_Status;
|
||||
|
||||
void FLASH_MassErase (void);
|
||||
FLASH_Status FLASH_EraseSector (uint32_t);
|
||||
FLASH_Status FLASH_ProgramPage (uint32_t, uint32_t, uint16_t);
|
||||
FLASH_Status FLASH_ProgramHalfWord(uint32_t, uint16_t);
|
||||
uint16_t FLASH_Checksum(void);
|
||||
|
||||
#endif /* __SN32F240B_FLASH_H */
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/SN32/LLD/SN32F24xB/FLASH/Flash.c
|
||||
|
||||
PLATFORMINC += $(CHIBIOS_CONTRIB)/os/hal/ports/SN32/LLD/SN32F24xB/FLASH
|
|
@ -27,6 +27,7 @@ endif
|
|||
include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F24xB/GPIO/driver.mk
|
||||
include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F24xB/USB/driver.mk
|
||||
include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F24xB/CT/driver.mk
|
||||
include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F24xB/FLASH/driver.mk
|
||||
|
||||
|
||||
# include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F24xB/TIM/driver.mk
|
||||
|
|
Loading…
Reference in New Issue