#include "FslTypes.h" #include "tpm.h" #include "vksp_tx_cfg.h" #include "key.h" #include "phy.h" extern UINT8 gu8_VkspDelay; extern UINT8 keyScan_cnt; extern UINT16 keyScan_Delay; extern void vfnPhyTimerCall(void); //#define POWER_CONSUMPTION_TEST #if 0 const uint TPMCVCFG = 416; //1200bps const uint TPMMV = 10000; #define EMULATED_MANCHESTER_PIN (PTAD_PTAD4) #define EMULATED_MANCHESTER_PIN_HIGH (PTAD_PTAD4 = 1) #define EMULATED_MANCHESTER_PIN_LOW (PTAD_PTAD4 = 0) t_ManchesterBuffer ManchesterBuffer = { (byte *)(0), 0, (byte *)(0), 0, 0, { 0, } }; /* *********************************************************************** * *Syntax tpm_init() *Description: TPM module initilization *Input : None *Output : None * ************************************************************************ */ void tpm_init(void) { TPMSC_PS = 0b010; //divide by 4 TPMSC &= ~TPMSC_CPWMS_MASK; TPMMOD = TPMMV; TPMSC_TOF = 0; //clear pending interrupt } /* *********************************************************************** * *Syntax isr_tpmch0() *Description: TPM channel0 interrupt service routine *Input : None *Output : None * ************************************************************************ */ interrupt void isr_tpmch0(void) { TPMC0SC_CH0F = 0; update_channelcfg(); } /* *********************************************************************** * *Syntax update_channelcfg() *Description: update the configuration of channel *Input : void *Output : void * ************************************************************************ */ void update_channelcfg(void) { static ulong channel_value; byte level; channel_value = TPMC0V; if(ManchesterBuffer.cycleindex == 16) { ManchesterBuffer.cycleindex = 0; ManchesterBuffer.bitindex = 0; ManchesterBuffer.byteindex++; if(ManchesterBuffer.byteindex == (ManchesterBuffer.data+ManchesterBuffer.datalength)) { end_tx_manchester(); return; } /******************************** For pointer to manchester code MANCHESTER_POINTER = 1; for(i=0;i<10;i++) { __asm nop; } MANCHESTER_POINTER = 0; *********************************/ } level = *ManchesterBuffer.byteindex>>(ManchesterBuffer.cycleindex/2)&0x01; if(level) EMULATED_MANCHESTER_PIN_HIGH; else EMULATED_MANCHESTER_PIN_LOW; if(ManchesterBuffer.cycleindex == 15) { __asm nop; __asm nop; ManchesterBuffer.bitindex = 0; ManchesterBuffer.Bits.bit = *(ManchesterBuffer.byteindex+1)>>ManchesterBuffer.bitindex&0x01; } else { ManchesterBuffer.bitindex = (ManchesterBuffer.cycleindex+1)/2; ManchesterBuffer.Bits.bit = *ManchesterBuffer.byteindex>>ManchesterBuffer.bitindex&0x01; } /**************************************************/ /* update the channel mode for the next compare*/ if(ManchesterBuffer.cycleindex%2) //if odd cycle { if(ManchesterBuffer.Bits.bit) TPMC0SC_ELS0x = (0b11); //set output on compare else TPMC0SC_ELS0x = (0b10); //clear output on compare } else //if even cycle { TPMC0SC_ELS0x = (0b01); //toggle output on compare } ManchesterBuffer.cycleindex++; /*************************************************/ /* update the channel value for the next compare*/ if(channel_value>(TPMMV-TPMCVCFG)) { TPMC0V = (uint)(channel_value+TPMCVCFG-TPMMV); } else { TPMC0V += TPMCVCFG; } /**************************************************/ } /* *********************************************************************** * *Syntax start_tx_manchester() *Description: prepare manchester code transmission *Input : txbuf, pointer of data buffer to be send as manchester code * datalen, the length of data to be sent *Output : None * ************************************************************************ */ void start_tx_manchester(byte* txbuf, uint datalen) { { ManchesterBuffer.data = txbuf; ManchesterBuffer.datalength = datalen; ManchesterBuffer.byteindex = ManchesterBuffer.data; ManchesterBuffer.bitindex = 0; ManchesterBuffer.cycleindex = 0; ManchesterBuffer.Bits.bit = *ManchesterBuffer.byteindex&0x01; } TPMC0SC_MS0x = 0b01; if(ManchesterBuffer.Bits.bit) TPMC0SC_ELS0x = 0b11; //set output on compare else TPMC0SC_ELS0x = 0b10; //clear output on compare TPMC0SC &= ~TPMC0SC_CH0F_MASK; TPMC0V = TPMCVCFG;//1200bps TPMCNT = 0; //reset counter TPMC0SC |= TPMC0SC_CH0IE_MASK; TPMSC_CLKSx = 0b01; //bus clock = 4MHz } /* *********************************************************************** * *Syntax end_tx_manchester() *Description: End of transmission of a complete manchester code,put channel * into reset state. *Input : None *Output : None * ************************************************************************ */ void end_tx_manchester(void) { TPMSC_CLKSx = 0b00; //disable TPM clock source TPMC0SC = 0; TPMC0V = 0; } #endif /*******************************************************************************/ /** * \brief TIMER Interrupt - To service the physical layer and others, overflow in * \ every 200us. manchester baudrate is 2.5kbps. * \author RV * \param void * \return void * \todo */ interrupt void TMR0Interrupt(void) { #ifndef POWER_CONSUMPTION_TEST volatile UINT8 i; i = TPMSC; TPMSC_TOF = 0; vfnPhyTimerCall(); if(gu8_VkspDelay == 200) { vfnVkspTx_UpdateVariableKey(); gu8_VkspDelay = 0; } gu8_VkspDelay++; keyScan_cnt++; keyScan_Delay++; if(keyScan_cnt == 50) { keyScan(); keyScan_cnt = 0; } #endif #ifdef POWER_CONSUMPTION_TEST static uint led_time_delay=0; if(led_time_delay<182) /* 182 bits for each message */ { led_time_delay++; } else { /* LED_Toggle(); */ PTAD_PTAD4 ^= 0x01; led_time_delay = 0; } PHY_PINOUTPUT ^= 0x01; #endif }