ctr_mcu/branches/nup0/i2c_twl.c
n2232 5d39dbe347 [branch] 0nup 1.33
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_mcu@339 013db118-44a6-b54f-8bf7-843cb86687b1
2011-06-09 08:44:21 +00:00

225 lines
5.6 KiB
C
Raw Permalink 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 /* 特殊機能レジスタ使用 */
/*============================================================================*/
#include "config.h"
#ifdef _MCU_KE3_
#else
#pragma interrupt INTIICA0 int_iic_twl RB2
#endif
#include "incs.h"
#include "i2c_twl_defs.h"
#include "i2c_twl.h"
extern u8 vreg_twl[];
#ifdef _MCU_BSR_
//#ifdef _MODEL_TS0_ || _MODEL_WM0_
// ワーキングモデルはI2Cが逆
#define ACKD ACKD0
#define ACKE ACKE0
#define COI COI0
#define IICAEN IICA0EN
#define IICRSV IICRSV0
#define IICA IICA0
#define IICAIF IICAIF0
#define IICAMK IICAMK0
#define IICAPR0 IICAPR00
#define IICAPR1 IICAPR10
#define IICCTL0 IICCTL00
#define IICE IICE0
#define IICF IICF0
#define IICS IICS0
#define IICWH IICWH0
#define IICWL IICWL0
#define LREL LREL0
#define SPD SPD0
#define SPIE SPIE0
#define STCEN STCEN0
#define STD STD0
#define SVA SVA0
#define WREL WREL0
#define WTIM WTIM0
#define SMC SMC0
#define TRC TRC0
#endif
#ifndef _MCU_BSR_
// ke3の時はダミー関数
void IIC_twl_Stop( void )
{
}
void IIC_twl_Init( void )
{
}
#else
enum IIC_TWL_STATE{
WAIT_ADRS = 0,
WAIT_DATA,
DATA_READED
};
/*============================================================================*/
// 注 ↓はマクロなので、returnはメインループに戻ります。
#define wait_next { \
tot = 0; \
while( IICAIF != 1 ){ \
tot++; \
if( tot == 0 ){ \
LREL = 1; \
return; \
} \
} \
}
__interrupt void int_iic_twl( )
{
u8 vreg_adrs;
u8 pre_dat;
u8 rcvd;
u16 tot;
u8 state;
// ①
WREL = 1; // ウェイト解除して次のバイトを待つ
WDT_Restart();
state = WAIT_ADRS;
while( 1 )
{
u8 my_iics;
wait_next; // 1バイト受信完了を待つ
my_iics = IICS;
IICAIF = 0;
if( my_iics & 0x02 ) // ( STD && !SPD )
{
// スタートコンディションがきた
if( ( my_iics & 0x08 ) == 0 ) // ( TRC ) 送信方向フラグ 0:マイコンが受信
{
// ①マイコンが応答できず、TWLがリトライしたときとか
// ここに来るのはスレーブ呼び出しの時 //
WREL = 1;
state = WAIT_ADRS;
/// レジスタアドレスが書かれるのを待つ
}
else
{
// ③R Rでのスレーブ呼び出し(リスタートコンディション) //
IICA = vreg_twl_read( vreg_adrs ); // データ送信
// ストップ待ち後、LREL = 1 にしないと、送信バッファが破壊されることがある
state = DATA_READED;
}
}
else if( ( my_iics & 0x03 ) == 0 ) // ( !STD && !SPD )
{
// 何らか受信
rcvd = IICA;
WREL = 1;
if( state == WAIT_ADRS )
{
// ②
vreg_adrs = adrs_table_twl_ext2int( rcvd );
/// データが書かれるか、リスタートで読み出されるのを待つ
state = WAIT_DATA;
}
else if( state == DATA_READED )
{
// データ1バイト送信後発生する
LREL = 1;
return;
}
else
{
// ③W
// データ書き込まれ
SVA = 0x5A; // ダミー
LREL = 1; // スタートコンディション待ちへ(連続書き込み未対応のため)
vreg_twl_write( vreg_adrs, rcvd );
SVA = IIC_T_SLAVEADDRESS;
return; // 受信おしまい //
}
}
else if( my_iics & 0x01 ) // SPD
{
// 終了
LREL = 1;
return;
}
else
{
// NOP();
}
}
}
/*****************************************************/
void IIC_twl_Init( void )
{
IICAEN = 1;
IICE = 0; /* IICA disable */
IICAMK = 1; /* INTIICA disable */
IICAIF = 0; /* clear INTIICA interrupt flag */
IICAPR0 = 0; /* set INTIICA high priority */
IICAPR1 = 0; /* set INTIICA high priority */
P20 &= ~0x3;
SVA = IIC_T_SLAVEADDRESS;
IICF = 0x01;
STCEN = 1; // リスタートの許可
IICRSV = 1; // 通信予約をさせない:スレーブに徹する
SPIE = 0; // ストップコンディションでの割り込みを禁止
WTIM = 1; // 自動でACKを返した後clkをLに固定する
ACKE = 1; // ダメCPUは無視して次の通信をはじめるかもしれないんで早くclkを開放しないといけない
IICWH = 5;
IICWL = 10; // L期間の長さ
SMC = 1;
IICAMK = 0; // 割り込みを許可
IICE = 1;
PM20 &= ~0x3; /* set clock pin for IICA */
LREL = 1;
}
//****************************************************************************
void IIC_twl_Stop( void )
{
IICE = 0; /* IICA disable */
IICAEN = 0;
}
#endif