#include "stk3332.h"
#include "iic.h"
#include "uart.h"
//___________________________________________________________________
// Copyright :LINCHENYANG
// File Name :
// Description :
// author : LINCHENYANG
// data : 2020
// Customer :
//___________________________________________________________________
/*****************************************************************************************************/
volatile bit IS_Hava_Data;
volatile unsigned char STK_Read_Data;
volatile unsigned short STK_Distance_Data;
/*****************************************************************************************************/
#define ALS_INTERRUPT_THD_DIFF 50 //20
//#define PS_INTERRUPT_MODE 0
//#define ALS_INTERRUPT_MODE 0
void report_ps_value(x_data ps_data,unsigned char nf_data)
{
UartSend(ps_data.byte.byte1);
UartSend(ps_data.byte.byte0);
UartSend(nf_data);
UartSend('\0');
}
/**********************************************************************
int STK_Delay()
**********************************************************************
Description: Delay for some ms, to be customized according to user
application platform
Parameters:
ms: ms counts
Return Value:
None
**********************************************************************/
void STK_Delay(unsigned short ms)
{
volatile unsigned short i;
while(ms--)
{
//this constant 15000 is just for MTK6253 running on 104M.
for(i=0; i<15000; i++)
{
}
}
}
void STK_Show_Allreg(void)
{
char reg[9] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x3E, 0x3F};
int i = 0;
unsigned char val = 0;
for(i=0;i<9;i++)
{
val = IIC_read_add(reg[i]);
}
}
void STK_sw_reset(void)
{
IIC_write_add(0x80,0x99);//W any data to reset the chip
}
unsigned short STK_Read_ALS(void)
{
unsigned short als_data;
als_data = ((IIC_read_add(STK_DATA1_ALS_REG)<<8)|IIC_read_add(STK_DATA2_ALS_REG));//ALS_DATA
return als_data;
}
unsigned short STK_Read_PS(void)
{
unsigned short ps_data = 0;
unsigned char temp;
IIC_Start();
IIC_SendData(DEVICE_ADD);
IIC_RecvACK();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_SendData(0x11);
IIC_RecvACK();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_Stop();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_Start();
IIC_SendData(DEVICE_ADD+0x01);
IIC_RecvACK();
ps_data=IIC_RecvData();
IIC_Stop();
ps_data=ps_data<<8;
IIC_Start();
IIC_SendData(DEVICE_ADD);
IIC_RecvACK();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_SendData(0x12);
IIC_RecvACK();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_Stop();
// GCC_DELAY(BLE_DELAY_TIME_COUNT);
IIC_Start();
IIC_SendData(DEVICE_ADD+0x01);
IIC_RecvACK();
temp=IIC_RecvData();
IIC_Stop();
ps_data=ps_data+temp;
return ps_data;
}
/**********************************************************************
int STK_enable_PS()
int STK_enable_ALS()
**********************************************************************
Description: enable ps/als
Parameters:
None
Return Value:
PS_en/ALS_en
**********************************************************************/
void STK_enable_PS(void)
{
unsigned char state = 0;
state = IIC_read_add(0x00);
IIC_write_add(0x00,0x05 | state);//enable ps and wait time
}
void STK_DisEnable_PS(void)
{
unsigned char state = 0;
state = IIC_read_add(0x00);
IIC_write_add(0x00,(~0x05) & state);//disenable ps and wait time
}
void STK_enable_ALS(void)
{
unsigned char state = 0;
state = IIC_read_add(0x00);
IIC_write_add(0x00,0x02 | state);//only enable als,no wait time
}
void STK_DisEnable_ALS(void)
{
unsigned char state = 0;
state = IIC_read_add(0x00);
IIC_write_add(0x00,(~0x02) & state);//only disenable als,no wait time
}
void STK_Init(void)
{
volatile unsigned short ps, ps_thd_h, ps_thd_l;
unsigned char ID;
STK_sw_reset();
STK_Delay(50);
ID = IIC_read_add(0x3E);
STK_Show_Allreg();
IIC_write_add(0x00, 0x00);/*0x05 only enable ps;0x02 only enable als;
0x00 disable all &go into a low power standby mode//STATE */
IIC_write_add(0x01, 0x31);
IIC_write_add(0x02, 0x3a);
IIC_write_add(0x03, 0x3F);//12ma 0x4c
#ifdef PS_INTERRUPT_MODE
IIC_write_add(0x04, 0x03);//0x01看标志位,0x03标志在/int脚,0x05用远近都产生中断
#else
IIC_write_add(0x04, 0x03);
#endif
//WAIT TIME
IIC_write_add(0x05, 0x07);
IIC_write_add(0x0A, 0xFF);//THDH1_ALS
IIC_write_add(0x0B, 0xFF);//THDH2_ALS
IIC_write_add(0x0C, 0x00);//THDL1_ALS
IIC_write_add(0x0D, 0x00);//0xf0//THDL2_ALS
//Cali
IIC_write_add(0x05, 0x00);
IIC_write_add(0x00, 0x05);
STK_Delay(10);
ps = STK_Read_PS();
STK_Delay(10);
ps += STK_Read_PS();
ps = ps>>1;//两次取平均
IIC_write_add(0x00, 0x00);
IIC_write_add(0x05, 0x07);//WAIT 50ms
//STK_ps_thd_h = 450;
//STK_ps_thd_l = 200;
//ps_thd_h = STK_ps_thd_h; //300
//ps_thd_l = STK_ps_thd_l; //250
ps_thd_h = ps+75;//120; //300 1580
ps_thd_l = ps+70;//110; //250 1560
IIC_write_add(0x06, (ps_thd_h & 0xFF00) >> 8);//THDH1_PS
IIC_write_add(0x07, (ps_thd_h & 0x00FF));//THDH2_PS
IIC_write_add(0x08, (ps_thd_l & 0xFF00) >> 8);//THDL1_PS
IIC_write_add(0x09, (ps_thd_l & 0x00FF));//THDL2_PS
STK_enable_PS();
STK_enable_ALS();
}
void stk_als_set_new_thd(unsigned short alscode)
{
unsigned int high_thd,low_thd;
unsigned short high_thd_u16, low_thd_u16;
high_thd = alscode + ALS_INTERRUPT_THD_DIFF;
low_thd = alscode - ALS_INTERRUPT_THD_DIFF;
if (high_thd >= (1<<16))
high_thd = (1<<16) -1;
if (low_thd <0)
low_thd = 0;
high_thd_u16 = (unsigned short)high_thd;
low_thd_u16 = (unsigned short)low_thd;
IIC_write_add(0x0A, (unsigned char)((high_thd_u16&0xFF00) >> 8));//THDH1_ALS
IIC_write_add(0x0B, (unsigned char)(high_thd_u16&0x00FF));//THDH2_ALS
IIC_write_add(0x0C, (unsigned char)((low_thd_u16&0xFF00) >> 8));//THDL1_ALS
IIC_write_add(0x0D, (unsigned char)(low_thd_u16&0x00FF));//0xf0//THDL2_ALS
return;
}
#if defined(PS_INTERRUPT_MODE) || defined(ALS_INTERRUPT_MODE)
void eint_work_func(void)
{
unsigned char flag = 0;
unsigned char flag1 = 0;
unsigned char nf_data = 0;
unsigned short ps_data = 0, als_data = 0;
unsigned char flag_psint = 0, flag_alsint = 0;
// unsigned long savedMask;
// savedMask = SaveAndSetIRQMask(); //disable interrupt
flag1 = IIC_read_add(STK_FLAG_REG);
flag_psint = flag & STK_FLG_PSINT_MASK;
flag_alsint = flag & STK_FLG_ALSINT_MASK;
if(flag_psint) //IF PS Interrupt
{
ps_data = STK_Read_PS();
if(flag & STK_FLG_NF_MASK) // Far
{
nf_data = 0;
//to do something
}
else // Near
{
nf_data = 1;
//to do something
}
report_ps_value(ps_data, nf_data); //Report ps_data and ps event(Far or Near) to system
IIC_write_add(STK_FLAG_REG,flag & (~STK_FLG_PSINT_MASK)); //clear FLG_PSINT ( Bit[4] )
}
if(flag_alsint) //IF ALS Interrupt
{
als_data = STK_Read_ALS();
stk_als_set_new_thd(als_data);
//report_als_value(als_data); //Report als_data to system
IIC_write_add(0x10,flag & (~0x20));//clear FLG_ALSINT ( Bit[5] )
}
//STK_Show_Allreg(); //for debug
}
#endif
//#ifndef PS_INTERRUPT_MODE
void get_ps_data(void)
{
unsigned char flag = 0;
unsigned char nf_data = 0;
x_data ps_data ;
flag = IIC_read_add(STK_FLAG_REG);
ps_data.byte.byte0 = IIC_read_add(STK_DATA1_PS_REG);
ps_data.byte.byte1 = IIC_read_add(STK_DATA2_PS_REG);
STK_Distance_Data =ps_data.get_data;
if(flag & 0x01) // Far
{
nf_data = 0;
// GCC_NOP();
//to do something
}
else // Near
{
nf_data = 1;
// GCC_DELAY(20);
//to do so