ctr_mcu/trunk/task_sys.c
N2232 e8aebb8b95 お知らせLED3色対応、LED部分をとりあえず。
拡張レジスタ・色テーブルをもらう部分を書く前に一度コミット
一部の割り込みをレジスタバンク使用するように変更。
 効果あるか?
コンパイラのワーニング対応
バッテリ残量で割り込み(暫定3%、8% 有用か?)


git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_mcu@128 013db118-44a6-b54f-8bf7-843cb86687b1
2010-04-19 08:49:47 +00:00

451 lines
12 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.

#pragma SFR
#pragma NOP
#pragma HALT
#pragma STOP
#include "incs.h"
#include "i2c_twl.h"
#include "i2c_ctr.h"
#include "led.h"
#include "accero.h"
#include "pm.h"
#include "rtc.h"
#include "sw.h"
#include "adc.h"
#include "self_flash.h"
//=========================================================
static void chk_emergencyExit();
//=========================================================
/* ========================================================
マイコン内部で必要なもの
・省電力に入れる
 system_status.pwr_state == OFF_TRIG で、このタスクが呼ばれると、
省電力モードに入ります
======================================================== */
void tsk_sys( )
{
static u8 timeout = 0;
switch ( system_status.pwr_state )
{
case OFF: //-------------------------------------------------------
// スイッチ操作などで割り込みが発生し、スリープが解除されるとここに来ます。
#ifndef _PARRADIUM_
switch ( system_status.poweron_reason )
{
default:
// スイッチで電源on
if( SW_pow_count != 0 )
{
timeout = 0;
}
else
{
timeout += 1;
}
if( timeout > 127 )
{
system_status.pwr_state = OFF_TRIG; // スイッチはノイズだった。寝る。
renge_task_interval_run_force = 1;
return;
}
if( SW_pow_count < 3 )
{
// もう少しスイッチの様子を見る
return;
}
// 電源投入
system_status.poweron_reason = PWSW;
break;
case ( RTC_ALARM ):
break;
}
timeout = 0;
BT_DET_P = 1; // チャージに時間が掛かるので先に上げておく
BT_TEMP_P = 1;
// 電源投入 //
iic_mcu_start( );
BT_init( ); // 実機やバッテリの判定、電池残量ICの設定
if( system_status.model == MODEL_JIKKI_NOBATT )
{
renge_task_interval_run_force = 1;
system_status.pwr_state = OFF_TRIG;
return;
}
// 残量チェック
BT_get_left(); // 先に、BT_init()が実行されている必要があります。(大丈夫)
if( vreg_ctr[VREG_C_BT_REMAIN] < 5 )
{
renge_task_interval_run_force = 1;
system_status.pwr_state = OFF_TRIG;
return;
}
if( PM_sys_pow_on( ) != ERR_SUCCESS )
{ // 電源起動不可エラー
renge_task_interval_run_force = 1;
system_status.pwr_state = OFF_TRIG;
return;
}
#else
vreg_ctr[ VREG_C_STATUS_1 ] |= REG_BIT_GASGAUGE_ERR;
system_status.poweron_reason = PWSW;
#endif // _PARADDIUM_
PM_LCD_vcom_set( ); // LCDの対向電圧値など書き込み
#ifdef _PMIC_TWL_
PM_TEG_LCD_dis( 0 );
#endif
if( system_status.poweron_reason == PWSW )
{
// 電源ボタンでのonの時は、LEDを点灯させる
vreg_ctr[VREG_C_LED_POW] = LED_POW_ILM_AUTO;
}
else
{
// とりあえず、LED消灯状態で起動させる
vreg_ctr[VREG_C_LED_POW] = LED_POW_ILM_OFF;
// todo?
}
system_status.pwr_state = ON_TRIG;
// ここまで来ると、電源投入確定
break;
case ON_TRIG: //-------------------------------------------------------
LED_init( );
PU7 = 0b00011101; // 4:SW_WIFI 3:SW_PWSW 2:PM_IRQ 0:PM_EXTDC_n
IIC_ctr_Init( );
if( ( vreg_ctr[ VREG_C_MCU_STATUS ] & REG_BIT_STATUS_WDT_RESET )
/*
if( vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
*/
!= 0 )
{
set_irq( VREG_C_IRQ0, REG_BIT_IRQ_WDT_RESET );
}
{
// WDTリセット時、I2Cの初期化まで割り込み保留
// ほんとはここにべた書きしたくないが...
if( ( vreg_ctr[ VREG_C_MCU_STATUS ] & REG_BIT_STATUS_WDT_RESET )
/*
if( vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
| vreg_ctr[ VREG_C_IRQ0 ]
*/
!= 0 )
{
set_irq( VREG_C_IRQ0, REG_BIT_IRQ_WDT_RESET );
}
}
IIC_twl_Init( );
RTC_32k_on( );
KRM = 0b00000000;
system_status.poweron_reason = NONE;
renge_task_interval_run_force = 1;
MK0 = INT_MSK0_RSV;
MK1 = INT_MSK1_RSV;
iic_mcu_start();
#ifdef _MCU_BSR_
// MK2 = ~( INT_MSK2_IIC_TWL | INT_MSK2_WIFI_TX_BSR | INT_MSK2_CODEC_PMIRQ );
// PMK21 = 0; // wifi 使わない
PMK6 = 0; // pm_irq
#else
MK2L = ~INT_MSK2_WIFI_TX_KE3;
#endif
system_status.reboot = 0;
system_status.pwr_state = ON;
break;
case ON: //---------------------------------------------
// PMICによる強制電源断チェック
// デバッガがreset1をアサートすることもある。そのときは全部リセット
chk_emergencyExit();
// SLP監視
if( SLP_REQ ){
system_status.pwr_state = SLEEP_TRIG;
renge_task_interval_run_force = 1;
}
break;
case SLEEP_TRIG: //-------------------------------------
PM_VDD_ecoMode();
system_status.pwr_state = SLEEP;
break;
case SLEEP: //------------------------------------------
chk_emergencyExit();
// スリープから復帰
if( !SLP_REQ ){
PM_VDD_normMode();
wait_ms( 5 ); // tdly_sw
#ifdef _MODEL_CTR_
SLP_ACK = 1;
NOP(); // 適当ウェイト
NOP();
NOP();
NOP();
SLP_ACK = 0;
#endif
system_status.pwr_state = ON_TRIG;
renge_task_interval_run_force = 1;
}
break;
case OFF_TRIG: //---------------------------------------
// LED消灯を待つ
vreg_ctr[VREG_C_LED_POW] = LED_POW_ILM_OFF;
if( LED_duty_pow_blu != 0 )
{
return;
}
LED_stop( );
IIC_ctr_Stop( );
IIC_twl_Stop( );
RTC_32k_off();
vreg_ctr[VREG_C_IRQ0] = 0;
vreg_ctr[VREG_C_IRQ1] = 0;
vreg_ctr[VREG_C_IRQ2] = 0;
vreg_ctr[VREG_C_IRQ3] = 0;
// 電源オン条件の割り込みセット
// PWSW KR3 押すとL
// BG24 KR4
// ふた開け INTP5 閉じるとL
// ACアダプタ INTP4 アダプタありでL
// RTC
#ifdef _PMIC_TWL_
PM_TEG_LCD_dis( 1 );
#endif
// IRQ0_deactive;
// pullup_off(); ↓
{
PU5 = 0b00000011; // PM_CHG,PM_CHGERR
PU7 = 0b00011001; // SW_WiFi,PWSWI,PM_EXTTDC
}
PM_sys_pow_off( );
while( iic_mcu_busy )
{;
}
KRM = ( KR_SW_POW ); // Mask ではなく、Modeなのだそうだ。紛らわしい
// intp20系は後ほど
MK0 = ~( INT_MSK0_EXTDC );
MK1 = ~( INT_MSK1_KR | INT_MSK1_RTCALARM | INT_MSK1_RTCINTVAL );
MK2L = 0b11111111;
IF0 = 0;
IF1 = 0;
IF2 = 0;
timeout = 0;
system_status.pwr_state = BT_CHARGE;
SW_pow_count = 0;
SW_wifi_count = 0;
// no break //
case BT_CHARGE:
if( !PM_EXTDC_n )
{
// アダプタ有り:充電温度監視
BT_TEMP_P = 1;
// 電源on
if( ( SW_pow_count > 3 ) || ( SW_wifi_count > 3 )
|| ( system_status.poweron_reason == RTC_ALARM ) )
{
system_status.pwr_state = OFF; // 若干抵抗有るが...
renge_task_interval_run_force = 1;
KRMK = 1;
return;
}
return;
}
else
{
// 省電力へ移行
BT_TEMP_P = 0;
while( RWST )
{;}
iic_mcu_stop( );
// 割り込み待ちで寝る //
RTCIMK = 1;
#ifndef _PARRADIUM_
#ifdef _MCU_BSR_
CKC = 0b00001001;
OSMC = 0x00;
#endif
STOP( );
#ifdef _MCU_BSR_
OSMC = 0x01;
CKC = 0b00001000;
#endif
#endif
RTCIMK = 0;
// 起きる //
// 起きる条件は
// ・KeyReturn割り込み電源ボたん
// ・RTCアラーム
// ・アダプタ挿抜
system_status.pwr_state = OFF; //
renge_task_interval_run_force = 1;
KRMK = 1;
return;
}
default:
while( 1 )
{
NOP( );
// あり得ないステート
}
}
}
/*******************************************************//**
PMICが電源異常で止めたか確認
**********************************************************/
static void chk_emergencyExit(){
#ifndef _PARRADIUM_
static u8 shirobako_power_control;
if( shirobako_power_control == 0 ) // PM_chk_LDSW() はI2C_mを使用し、高コスト
{
if( !RESET1_n )
{
// リセットが下がってる
if( PM_chk_LDSW( ) == 0 )
{
// PMICが異常終了判断をした
system_status.pwr_state = OFF_TRIG;
renge_task_interval_run_force = 1;
}
else
{
// 白箱の仕業
shirobako_power_control = 1;
}
}
}
else
{
if( shirobako_power_control == 240 )
// デバッガが何かした。reset1を解除するまでは無視
{
if( RESET1_n )
{
shirobako_power_control = 0;
}
}else if( shirobako_power_control == 200 )
// デバッガが何かしたいらしい
{
// 白箱は電源を切りたいらしい
system_status.pwr_state = OFF_TRIG;
renge_task_interval_run_force = 1;
shirobako_power_control = 240;
}
else
{
if( !RESET1_n )
{
shirobako_power_control += 1;
}
else
{
// リセットをかけたらしい
iic_mcu_write_a_byte( IIC_SLA_PMIC, PM_REG_ADRS_BL, 0 );
vreg_ctr[VREG_C_STATUS] = ( vreg_ctr[VREG_C_STATUS] & 0b10011111 );
vreg_ctr[VREG_C_COMMAND0] |= REG_BIT_RESET1_REQ;
renge_task_immed_add( do_command0 );
shirobako_power_control = 240;
}
}
}
#endif
}
/* ========================================================
CPUからのスリープ要求
 ポーリングにしました。
======================================================== */
/*
__interrupt void intp0_slp( )
{ // SLP
if( SLP_REQ ){
system_status.pwr_state = SLEEP_TRIG;
}else{
system_status.pwr_state = ON_TRIG;
if( PM_BL_set() != ERR_SUCCESS ){
renge_task_interval_run_force = 1;
iic_mcu_stop();
system_status.pwr_state = OFF_TRIG;
}
}
renge_task_interval_run_force = 1;
}
*/
/*******************************************************//**
全く意味ないですが、気分的な物で...
**********************************************************/
task_status_immed tski_firm_update(){
firm_update();
return( ERR_SUCCESS );
}