Electricity/FWLIB/source/ENS1_MTP.c

90 lines
2.3 KiB
C
Raw Permalink 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.

/*
*Copyright (C),2023 , NANOCHAP
*File name: ENS1_MTP.C
*Author:
*Version: V1.0
*Date: 2023-11-
*Description:
*Function List:
History:
1.V1.0
Date:
Author:
Modification: <20><><EFBFBD><EFBFBD>
*/
/*
MTP˵<EFBFBD><EFBFBD>
1<EFBFBD><EFBFBD>MTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>ܰ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>,ÿ<><C3BF><EFBFBD><EFBFBD>1024<32>ֽڴ<D6BD>С
<EFBFBD><EFBFBD>ı<EFBFBD>ţ<EFBFBD>
SECTOR 0 : 0000H - 03FFH
SECTOR 1 : 0400H - 07FFH
SECTOR 2 : 0800H - 0BFFH
SECTOR 3 : 0C00H - 0FFFH
SECTOR 4 : 1000H - 13FFH
SECTOR 5 : 1400H - 17FFH
SECTOR 6 : 1800H - 1BFFH
SECTOR 7 : 1C00H - 1FFFH
*/
#include "ENS1_MTP.h"
uint16_t write_current_data[4]={0,0,0,0};
STRUCT_MTP_TRIM MTP_FT_SET=
{
.OSCA_FT = 0x10, //Ĭ<><C4AC>ֵΪ 0X10
.OSC32K_RTRIM = 0x10, //Ĭ<><C4AC>ֵΪ 0X10
.BG_TRIM = 0x88,
};
uint8_t MTP_init(void)
{
#ifdef ENS1_HSI_16MHz
CMSDK_MTPREG->MTP_CR = 0x00000001;
#elif ENS1_HSI_32MHz
CMSDK_MTPREG->MTP_CR = 0x00000003;
#endif
return 0;
}
//MTP<54>ڱ<EFBFBD><DAB1><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ
void flash_read(uint32_t start_addr,uint16_t *test_i){
uint16_t result = 0;
result = HW16_REG(start_addr);
*test_i = result ;
}
//<2F><>MTP<54><50>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EBA3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪMTP<54>ĵ<EFBFBD>0x1BC0<43>飨MTP_BASE_ADDR + 0x6F00<30><30>
int8_t flash_buff_write(uint32_t start_addr, uint16_t *buff) {
HW32_REG(start_addr) = ((uint32_t)((*(buff+1)<<16)&0xffff0000) + ((*buff)&0x0000ffff));
// while(!(CMSDK_MTPREG->MTP_SR&0x00000002)){};
if(HW32_REG(start_addr) != ((uint32_t)(*(buff+1)<<16) + *buff ))
{
return -1;
}
return 0;
}
int8_t flash_write_ctrl(uint16_t *buff , uint32_t start_addr){
CMSDK_MTPREG->MTP_CLR = 0xffffffff; //SR<53>Ĵ<EFBFBD><C4B4><EFBFBD>
CMSDK_MTPREG->MTP_CR = 0x00000002; //2<>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
CMSDK_MTPREG->MTP_ACLR = 0x00000000; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>sector0 -sector 6 <20><>sector7 <20><>Ϊbootloader<65><72><EFBFBD><EFBFBD>
CMSDK_MTPREG->MTP_KEYR = 0x5a5a5a5a; //key
return flash_buff_write(start_addr, buff);
}
int8_t write_data(void){
uint16_t *wr_data = (uint16_t *)malloc(8*sizeof(uint8_t));
memcpy(wr_data , write_current_data, 8);
int8_t val = flash_write_ctrl(wr_data,DATA_SAVE_ADDR); //<2F><> 0x1BC0<43><30>
val = flash_write_ctrl(wr_data+2, DATA_SAVE_ADDR + 4);
return val;
}