mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
166 lines
4.3 KiB
C
166 lines
4.3 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : CH58x_flash.c
|
||
* Author : WCH
|
||
* Version : V1.2
|
||
* Date : 2021/11/17
|
||
* Description
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*******************************************************************************/
|
||
|
||
#include "CH58x_common.h"
|
||
|
||
/* RESET_EN */
|
||
#define RESET_Enable 0x00000008
|
||
#define RESET_Disable 0xFFFFFFF7
|
||
|
||
/* LOCKUP_RST_EN */
|
||
#define UART_NO_KEY_Enable 0x00000100
|
||
#define UART_NO_KEY_Disable 0xFFFFFEFF
|
||
|
||
/* BOOT_PIN */
|
||
#define BOOT_PIN_PB22 0x00000200
|
||
#define BOOT_PIN_PB11 0xFFFFFDFF
|
||
|
||
/* FLASH_WRProt */
|
||
#define FLASH_WRProt 0xFFF003FF
|
||
|
||
/*********************************************************************
|
||
* @fn FLASH_ROM_READ
|
||
*
|
||
* @brief Read Flash
|
||
*
|
||
* @param StartAddr - read address
|
||
* @param Buffer - read buffer
|
||
* @param len - read len
|
||
*
|
||
* @return none
|
||
*/
|
||
void FLASH_ROM_READ(uint32_t StartAddr, void *Buffer, uint32_t len)
|
||
{
|
||
uint32_t i, Length = (len + 3) >> 2;
|
||
uint32_t *pCode = (uint32_t *)StartAddr;
|
||
uint32_t *pBuf = (uint32_t *)Buffer;
|
||
|
||
for(i = 0; i < Length; i++)
|
||
{
|
||
*pBuf++ = *pCode++;
|
||
}
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn UserOptionByteConfig
|
||
*
|
||
* @brief Configure User Option Byte.<2E><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч,<2C><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||
* (ʹ<>øú<C3B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ùٷ<C3B9><D9B7>ṩ<EFBFBD><E1B9A9>.S<>ļ<EFBFBD><C4BC><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD>øú<C3B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5>Խӿ<D4BD>Ĭ<EFBFBD>Ϲر<CFB9>)
|
||
*
|
||
* @param RESET_EN - <20>ⲿ<EFBFBD><E2B2BF>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
||
* @param BOOT_PIN - ENABLE-ʹ<><CAB9>Ĭ<EFBFBD><C4AC>boot<6F><74>-PB22,DISABLE-ʹ<><CAB9>boot<6F><74>-PB11
|
||
* @param UART_NO_KEY_EN - <20><><EFBFBD><EFBFBD><EFBFBD>ⰴ<EFBFBD><E2B0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
||
* @param FLASHProt_Size - д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С(<28><>λ4K)
|
||
*
|
||
* @return 0-Success, 1-Err
|
||
*/
|
||
uint8_t UserOptionByteConfig(FunctionalState RESET_EN, FunctionalState BOOT_PIN, FunctionalState UART_NO_KEY_EN,
|
||
uint32_t FLASHProt_Size)
|
||
{
|
||
uint32_t s, t;
|
||
|
||
FLASH_ROM_READ(0x14, &s, 4);
|
||
|
||
if(s == 0xF5F9BDA9)
|
||
{
|
||
s = 0;
|
||
FLASH_EEPROM_CMD(CMD_GET_ROM_INFO, 0x7EFFC, &s, 4);
|
||
s &= 0xFF;
|
||
|
||
if(RESET_EN == ENABLE)
|
||
s |= RESET_Enable;
|
||
else
|
||
s &= RESET_Disable;
|
||
|
||
/* bit[7:0]-bit[31-24] */
|
||
s |= ((~(s << 24)) & 0xFF000000); //<2F><>8λ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣȡ<CFA2><C8A1><EFBFBD><EFBFBD>
|
||
|
||
if(BOOT_PIN == ENABLE)
|
||
s |= BOOT_PIN_PB22;
|
||
if(UART_NO_KEY_EN == ENABLE)
|
||
s |= UART_NO_KEY_Enable;
|
||
|
||
/* bit[23-10] */
|
||
s &= 0xFF0003FF;
|
||
s |= ((FLASHProt_Size << 10) | (5 << 20)) & 0x00FFFC00;
|
||
|
||
/*Write user option byte*/
|
||
FLASH_ROM_WRITE(0x14, &s, 4);
|
||
|
||
/* Verify user option byte */
|
||
FLASH_ROM_READ(0x14, &t, 4);
|
||
|
||
if(s == t)
|
||
return 0;
|
||
else
|
||
return 1;
|
||
}
|
||
|
||
return 1;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn UserOptionByteClose_SWD
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5>Խӿڣ<D3BF><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>.<2E><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч,<2C><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||
* (ʹ<>øú<C3B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ùٷ<C3B9><D9B7>ṩ<EFBFBD><E1B9A9>.S<>ļ<EFBFBD><C4BC><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD>øú<C3B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5>Խӿ<D4BD>Ĭ<EFBFBD>Ϲر<CFB9>)
|
||
*
|
||
* @return 0-Success, 1-Err
|
||
*/
|
||
uint8_t UserOptionByteClose_SWD(void)
|
||
{
|
||
uint32_t s, t;
|
||
|
||
FLASH_ROM_READ(0x14, &s, 4);
|
||
|
||
if(s == 0xF5F9BDA9)
|
||
{
|
||
FLASH_EEPROM_CMD(CMD_GET_ROM_INFO, 0x7EFFC, &s, 4);
|
||
|
||
s &= ~((1 << 4) | (1 << 7)); //<2F><><EFBFBD>õ<EFBFBD><C3B5>Թ<EFBFBD><D4B9>ܣ<EFBFBD> <20><><EFBFBD><EFBFBD>SPI<50><49>дFLASH
|
||
|
||
/* bit[7:0]-bit[31-24] */
|
||
s &= 0x00FFFFFF;
|
||
s |= ((~(s << 24)) & 0xFF000000); //<2F><>8λ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣȡ<CFA2><C8A1><EFBFBD><EFBFBD>
|
||
|
||
/*Write user option byte*/
|
||
FLASH_ROM_WRITE(0x14, &s, 4);
|
||
|
||
/* Verify user option byte */
|
||
FLASH_ROM_READ(0x14, &t, 4);
|
||
|
||
if(s == t)
|
||
return 0;
|
||
else
|
||
return 1;
|
||
}
|
||
|
||
return 1;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn UserOptionByte_Active
|
||
*
|
||
* @brief <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>к<EFBFBD><D0BA>Զ<EFBFBD><D4B6><EFBFBD>λ
|
||
*
|
||
* @return 0-Success, 1-Err
|
||
*/
|
||
void UserOptionByte_Active(void)
|
||
{
|
||
FLASH_ROM_SW_RESET();
|
||
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
|
||
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
|
||
SAFEOPERATE;
|
||
R16_INT32K_TUNE = 0xFFFF;
|
||
R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
|
||
R8_SAFE_ACCESS_SIG = 0;
|
||
while(1);
|
||
}
|