twl_mcu/flash.c
2024-12-17 04:24:29 -05:00

183 lines
4.5 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

#include "incs.h"
#include "eeelib.h"
#include "eepe.h"
extern u8 vregs[];
extern u8 adc_comp_val;
bit EEPemu;
/*****************************************************************************
 
****************************************************************************/
void flash_init(){
if( ( iic2m_read_byte( IIC_SLV_ADDR_DCP, 1 ) & 0x80 ) == 0 ){ // == isl95810
// if( ( iic2m_read_byte( IIC_SLV_ADDR_DCP, 1 ) & 0x80 ) != 0 ){ // == isl95811
EEPemu = 1;
eepe_init();
}else{
EEPemu = 0;
}
}
/*****************************************************************************
 
****************************************************************************/
void restore_params(){
u8 temp,temp2;
u8 eep_status;
// ADC補正パラメータ復帰 //
eep_status = flash_read( DAT_ADC_COMP, &temp );
if( EEPemu == 0 ){
// ISL95811で、書き込みに失敗してたら確実に 0xFF なのだそうだ
flash_read( DAT_ADC_COMP + 1 , &temp2 );
if( temp2 < temp ){ temp = temp2; }
flash_read( DAT_ADC_COMP + 2 , &temp2 );
if( temp2 < temp ){ temp = temp2; }
flash_read( DAT_ADC_COMP + 3 , &temp2 );
if( temp2 < temp ){ temp = temp2; }
}
if( eep_status == EEE_NORMAL ){
adc_comp_val = temp;
vregs[ REG_INT_ADRS_ADC_CALIB_STATUS ] = 1;
}else{ // 何かエラー
adc_comp_val = 0x50; // 512にこれを足す。余裕を持って 3.40V とする
vregs[ REG_INT_ADRS_ADC_CALIB_STATUS ] = 0xE0;
}
vregs[ REG_INT_ADRS_ADC_CALIB_VALUE ] = adc_comp_val;
DBG_IIC_REG_TMP1( adc_comp_val );
// バックライト輝度復帰 //
eep_status = flash_read( DAT_BL_BRIGHT, &temp );
if( ( eep_status == EEE_NORMAL ) &&
( temp <= BL_IND_MAX ) ){
vregs[ REG_INT_ADRS_BL ] = temp;
}else{
vregs[ REG_INT_ADRS_BL ] = 3; // TWL初期値
}
// rsv_set_BL = 1; // 不要。/resetの解除を待って、pmic_init()が書く。
}
/*****************************************************************************
 内部で使用
****************************************************************************/
// 内部で一度だけリトライを行います。
// だめだったらエラーを返します。
// 返り値:エラーコード
u8 flash_write( u8 adrs, u8* dat ){
static u8 EEPe_counter = 0;
u8 status;
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
// 前処理
DI();
IIC0_Stop();
TM51_Stop();
ADC_Stop();
flash_we = 1;
status = ucEEPROMWriteEx_A( adrs, dat );
if( status != 0x00 ){ // 一回だけリトライ
flash_erase();
flash_block_change();
status = ucEEPROMWriteEx_A( adrs, dat );
}
// 後処理
flash_we = 0;
IIC0_SlaveReceiveStart();
TM51_Start();
ADC_Start();
EI();
return( status ); // 書き込み失敗とか
}else{
// 95811向け。ーチェックなのですが。
if( adrs == DAT_ADC_COMP ){
EEPe_counter &= 0x03;
return( iic2m_write_data( IIC_SLV_ADDR_DCP, adrs + 2 + EEPe_counter++, *dat ) ); // nakとか。
}else{
return( iic2m_write_data( IIC_SLV_ADDR_DCP, adrs + 2, *dat ) );
}
}
}
// 返り値:エラーコード
u8 flash_read( u8 adrs, u8* dat ){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
return( ucEEPROMReadEx_A( adrs, dat ) );
}else{
// 95811向け。ーチェック
*dat = iic2m_read_byte( IIC_SLV_ADDR_DCP, adrs + 2 );
return 0;
}
}
// 返り値:エラーコード
u8 flash_erase(){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
return( ucEEPROMEraseEx_A() );
}else{
return( 0 );
// nothing to do
}
}
// 返り値:エラーコード
u8 flash_block_change(){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
return( ucEEPROMChangeEx_A() );
}else{
// nothing to do
return 0;
}
}
void flash_enable(){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
DI();
IIC0_Stop();
TM51_Stop();
ADC_Stop();
flash_we = 1;
}else{
// nothing to do
}
}
void flash_disable(){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
flash_we = 0;
IIC0_SlaveReceiveStart();
TM51_Start();
ADC_Start();
EI();
}else{
// nothing to do
}
}
// 未使用
u8 self_free_space(){
if( EEPemu == 1 ){ // 内部フラッシュでEEPROMエミュレーション
return( ucEEPROMGetRemainEx_A() );
}else{
return( 0xFF );
}
}