extern void timerStop(timer * pTimer) { switch(pTimer->m_TimerNumber) { case 1: CloseTimer1(); break; case 2: CloseTimer2(); break; case 3: CloseTimer3(); break; case 4: CloseTimer4(); break; case 5: CloseTimer5(); break; } }
// h/w clock ASK emulator - initialise data pointers for ISR and start timer 2 // args: clock pulsewidth (NOT period!), data as array of 0/1, repeat void write_ASK_HW_clock(unsigned int pulse, BYTE *data, unsigned int tpb, unsigned int repeat) { // convert to ticks pulse= CONVERT_TO_TICKS(pulse); // we only need to tick once per bit pulse *= (unsigned long) tpb; // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_DataBitRate= 1; EMU_SubCarrier_T0= 1; EMU_Repeat= repeat; // if we're manchester or bi-phase encoding, we want to clock twice as fast so we can toggle on half-bit if(RFIDlerConfig.Manchester || RFIDlerConfig.BiPhase) { pulse /= 2; EMU_SubCarrier_T0 *= 2; EMU_DataBitRate *= 2; } // make sure no clock is running stop_HW_clock(); // set mode EmulationMode= MOD_MODE_ASK_OOK; // make sure nobody's using our timer CloseTimer3(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // emulator mode COIL_MODE_EMULATOR(); // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, pulse - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
void closeTimer(int timer) { switch(timer) { case 1: CloseTimer1(); break; case 2: CloseTimer2(); break; case 3: CloseTimer3(); break; case 4: CloseTimer4(); break; } }
// h/w clock PSK emulator - initialise data pointers for ISR and start timer 2 // args: clock period, data as array of 0/1, fc per bit, sub-carrier ticks, repeat void write_PSK1_HW_clock(unsigned int period, BYTE *data, unsigned int fcpb, unsigned int c0, unsigned int repeat) { // convert to ticks period= CONVERT_TO_TICKS(period); // for psk we want a full clock cycle, not a tick period *= 2; // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_DataBitRate= fcpb; EMU_SubCarrier_T0= c0; EMU_Repeat= repeat; // set mode EmulationMode= MOD_MODE_PSK1; // make sure no clock is running stop_HW_clock(); // make sure nobody's using our timers CloseTimer3(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // emulator mode COIL_MODE_EMULATOR(); // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, period / 2L - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
// h/w clock FSK emulator - initialise data pointers for ISR and start timer 2 // args: clock pulsewidth (NOT period!), data as array of 0/1, T0 sub-carrier ticks, T1 sub-carrier ticks, repeat void write_FSK_HW_clock(unsigned long pulse, BYTE *data, unsigned int tpb, unsigned int t0, unsigned int t1, unsigned int repeat) { // convert to ticks pulse= CONVERT_TO_TICKS(pulse); // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_Repeat= repeat; EMU_DataBitRate= tpb; EMU_SubCarrier_T0= t0; EMU_SubCarrier_T1= t1; // set mode EmulationMode= MOD_MODE_FSK; // make sure no clock is running stop_HW_clock(); // make sure nobody's using our timer CloseTimer3(); // emulator mode COIL_MODE_EMULATOR(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, pulse - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
int main(int argc, char** argv) { /*Configuring POSC with PLL, with goal FOSC = 80 MHZ */ // Configure PLL prescaler, PLL postscaler, PLL divisor // Fin = 8 Mhz, 8 * (40/2/2) = 80 PLLFBD = 18; // M=40 // change to 38 for POSC 80 Mhz - this worked only on a single MCU for uknown reason CLKDIVbits.PLLPOST = 0; // N2=2 CLKDIVbits.PLLPRE = 0; // N1=2 // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011) //__builtin_write_OSCCONH(0x03); // tune FRC OSCTUN = 23; // 23 * 0.375 = 8.625 % -> 7.37 Mhz * 1.08625 = 8.005Mhz // Initiate Clock Switch to external oscillator NOSC=0b011 (alternative use FRC with PLL (NOSC=0b01) __builtin_write_OSCCONH(0b011); __builtin_write_OSCCONL(OSCCON | 0x01); // Wait for Clock switch to occur while (OSCCONbits.COSC!= 0b011); // Wait for PLL to lock while (OSCCONbits.LOCK!= 1); // local variables in main function int status = 0; int i = 0; int ax = 0, ay = 0, az = 0; int statusProxi[8]; int slowLoopControl = 0; UINT16 timerVal = 0; float timeElapsed = 0.0; //extern UINT8 pwmMotor; extern UINT16 speakerAmp_ref; extern UINT16 speakerFreq_ref; extern UINT8 proxyStandby; UINT16 dummy = 0x0000; setUpPorts(); delay_t1(50); PWMInit(); delay_t1(50); ctlPeltier = 0; PeltierVoltageSet(ctlPeltier); FanCooler(0); diagLED_r[0] = 100; diagLED_r[1] = 0; diagLED_r[2] = 0; LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]); // Speaker initialization - set to 0,1 spi1Init(2, 0); speakerAmp_ref = 0; speakerAmp_ref_old = 10; speakerFreq_ref = 1; speakerFreq_ref_old = 10; int count = 0; UINT16 inBuff[2] = {0}; UINT16 outBuff[2] = {0}; while (speakerAmp_ref != speakerAmp_ref_old) { if (count > 5 ) { // Error ! //LedUser(100, 0, 0); break; } inBuff[0] = (speakerAmp_ref & 0x0FFF) | 0x1000; chipSelect(slaveVib); status = spi1TransferWord(inBuff[0], outBuff); chipDeselect(slaveVib); chipSelect(slaveVib); status = spi1TransferWord(inBuff[0], &speakerAmp_ref_old); chipDeselect(slaveVib); count++; } count = 0; while (speakerFreq_ref != speakerFreq_ref_old) { if (count > 5 ) { // Error ! //LedUser(0, 100, 0); break; } inBuff[0] = (speakerFreq_ref & 0x0FFF) | 0x2000; chipSelect(slaveVib); status = spi1TransferWord(inBuff[0], outBuff); chipDeselect(slaveVib); chipSelect(slaveVib); status = spi1TransferWord(inBuff[0], &speakerFreq_ref_old); chipDeselect(slaveVib); count++; } accPin = aSlaveR; accPeriod = 1.0 / ACC_RATE * 1000000.0; // in us; for ACC_RATE = 3200 Hz it should equal 312.5 us status = adxl345Init(accPin); ax = status; delay_t1(5); /* Init FFT coefficients */ TwidFactorInit(LOG2_FFT_BUFF, &Twiddles_array[0],0); delta_freq = (float)ACC_RATE / FFT_BUFF; // read 100 values to calculate bias int m; int n = 0; for (m = 0; m < 100; m++) { status = readAccXYZ(accPin, &ax, &ay, &az); if (status <= 0) { // } else { ax_b_l += ax; ay_b_l += ay; az_b_l += az; n++; } delay_t1(1); } ax_b_l /= n; ay_b_l /= n; az_b_l /= n; _SI2C2IE = 0; _SI2C2IF = 0; // Proximity sensors initalization I2C1MasterInit(); status = VCNL4000Init(); // Cooler temperature sensors initalization status = adt7420Init(0, ADT74_I2C_ADD_mainBoard); delay_t1(1); muxCh = I2C1ChSelect(1, 6); status = adt7420Init(0, ADT74_I2C_ADD_flexPCB); // Temperature sensors initialization statusTemp[0] = adt7320Init(tSlaveF, ADT_CONT_MODE | ADT_16_BIT); delay_t1(5); statusTemp[1] = adt7320Init(tSlaveR, ADT_CONT_MODE | ADT_16_BIT); delay_t1(5); statusTemp[2] = adt7320Init(tSlaveB, ADT_CONT_MODE | ADT_16_BIT); delay_t1(5); statusTemp[3] = adt7320Init(tSlaveL, ADT_CONT_MODE | ADT_16_BIT); delay_t1(5); // Temperature estimation initialization for (i = 0; i < 50; i++) { adt7320ReadTemp(tSlaveF, &temp_f); delay_t1(1); adt7320ReadTemp(tSlaveL, &temp_l); delay_t1(1); adt7320ReadTemp(tSlaveB, &temp_b); delay_t1(1); adt7320ReadTemp(tSlaveR, &temp_r); delay_t1(1); } tempBridge[0] = temp_f; tempBridge[1] = temp_r; tempBridge[2] = temp_b; tempBridge[3] = temp_l; if (statusTemp[0] != 1) temp_f = -1; if (statusTemp[1] != 1) temp_r = -1; if (statusTemp[2] != 1) temp_b = -1; if (statusTemp[3] != 1) temp_l = -1; // CASU ring average temperature temp_casu = 0; tempNum = 0; tempSensors = 0; for (i = 0; i < 4; i++) { if (statusTemp[i] == 1 && tempBridge[i] > 20 && tempBridge[i] < 60) { tempNum++; temp_casu += tempBridge[i]; tempSensors++; } } if (tempNum > 0) temp_casu /= tempNum; else temp_casu = -1; temp_casu1 = temp_casu; temp_wax = temp_casu; temp_wax1 = temp_casu; temp_model = temp_wax; temp_old[0] = temp_f; temp_old[1] = temp_r; temp_old[2] = temp_b; temp_old[3] = temp_l; temp_old[4] = temp_flexPCB; temp_old[5] = temp_pcb; temp_old[6] = temp_casu; temp_old[7] = temp_wax; for (i = 0; i < 4; i++) { uref_m[i] = temp_wax; } // Configure i2c2 as a slave device and interrupt priority 5 I2C2SlaveInit(I2C2_CASU_ADD, BB_I2C_INT_PRIORITY); // delay for 2 sec for(i = 0; i < 4; i ++) { delay_t1(500); ClrWdt(); } while (i2cStarted == 0) { delay_t1(200); ClrWdt(); } dma0Init(); dma1Init(); CloseTimer4(); ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY); OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256)); CloseTimer5(); ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY); OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256)); diagLED_r[0] = 0; diagLED_r[1] = 0; diagLED_r[2] = 0; LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]); start_acc_acquisition(); while(1) { ConfigIntTimer2(T2_INT_OFF); // Disable timer interrupt IFS0bits.T2IF = 0; // Clear interrupt flag OpenTimer2(T2_ON | T2_PS_1_256, 65535); // Configure timer if (!proxyStandby) { statusProxi[0] = I2C1ChSelect(1, 2); // Front proxy_f = VCNL4000ReadProxi(); delay_t1(1); statusProxi[1] = I2C1ChSelect(1, 4); // Back right proxy_br = VCNL4000ReadProxi(); delay_t1(1); statusProxi[2] = I2C1ChSelect(1, 3); // Front right proxy_fr = VCNL4000ReadProxi(); delay_t1(1); statusProxi[3] = I2C1ChSelect(1, 5); // Back proxy_b = VCNL4000ReadProxi(); delay_t1(1); statusProxi[4] = I2C1ChSelect(1, 0); // Back left proxy_bl = VCNL4000ReadProxi(); delay_t1(1); statusProxi[5] = I2C1ChSelect(1, 1); // Front left proxy_fl = VCNL4000ReadProxi(); delay_t1(1); } else { proxy_f = 0; // Front proxy_br = 0; // Back right proxy_fr = 0; // Front right proxy_b = 0; // Back proxy_bl = 0; // Back left proxy_fl = 0; // Front left } if (timer4_flag == 1) { // every 2 seconds CloseTimer4(); ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY); timer4_flag = 0; if (dma_spi2_started == 0) { OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256)); skip_temp_filter++; tempLoop(); } else { OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(50, 256)); } } if (dma_spi2_done == 1) { fftLoop(); dma_spi2_done = 0; } if ((timer5_flag == 1) || (new_vibration_reference == 1)) { // every 1 seconds CloseTimer5(); ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY); OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256)); timer5_flag = 0; if (new_vibration_reference == 1) { //if(1){ CloseTimer3(); dma0Stop(); dma1Stop(); spi2Init(2, 0); dma0Init(); dma1Init(); chipDeselect(aSlaveR); IFS0bits.DMA0IF = 0; delay_t1(30); // transient response } new_vibration_reference = 0; start_acc_acquisition(); } // Cooler fan control if (fanCtlOn == 1) { if (temp_pcb >= 25 && fanCooler == FAN_COOLER_OFF) fanCooler = FAN_COOLER_ON; else if (temp_pcb <= 24 && fanCooler == FAN_COOLER_ON) fanCooler = FAN_COOLER_OFF; // In case of I2C1 fail turn on the fan if ((proxy_f == 0xFFFF) && (proxy_fr == 0xFFFF) && (proxy_br == 0xFFFF) && (proxy_b == 0xFFFF) && (proxy_bl == 0xFFFF) && (proxy_fl == 0xFFFF)) fanCooler = FAN_COOLER_ON; } else if (fanCtlOn == 2) fanCooler = FAN_COOLER_ON; else fanCooler = FAN_COOLER_OFF; //TEST // temp_f = temp_model; // if (temp_ref < 30) { // temp_r = smc_parameters[0] * 10; // } // else { // temp_r = smc_parameters[0] / 2.0 * 10.0; // } // temp_r = alpha*10; // temp_b = sigma_m * 10; // temp_l = sigma * 10; //temp_flexPCB = temp_ref_ramp; /* proxy_f = dma_spi2_started; proxy_fl = dma_spi2_done; proxy_bl = new_vibration_reference; proxy_b = timer5_flag; proxy_br = timer4_flag; */ int dummy_filt = 0; for (i = 0; i < 8; i++) { if (index_filter[i] > 0){ dummy_filt++; } } if (dummy_filt > 0) { filtered_glitch = dummy_filt; //for (i = 0; i< 8; index_filter[i++] = 0); } else { filtered_glitch = 0; } updateMeasurements(); timerVal = ReadTimer2(); CloseTimer2(); timeElapsed = ms_from_ticks(timerVal, 256); //if (timeElapsed < MAIN_LOOP_DUR) // delay_t1(MAIN_LOOP_DUR - timeElapsed); ClrWdt(); //Clear watchdog timer } // end while(1) return (EXIT_SUCCESS); }
// Emulate TAG ISR // this ISR does the actual send of a '*' terminated bitstream // mLED_Antenna is set during emulation, and can be used as a semaphore to detect emulation in progress // note that the best resolution we can achieve on a pic32 seems to be 2us // // setup routine is responsible for: // // setting clock period and arming interrupt // setting global variables: // // EMU_ThisBit - the next bit to output (as 0x00 or 0x01) // *EMU_Data - pointer to array of 0x00/0x01 values, '*' terminated // inital value should be &EMU_ThisBit + 1 // *EMU_Reset_Data - pointer to the complete data array // EMU_Repeat - number of times to repeat full data pattern (0 means send once) // EMU_Invert - Invert 0x00/0x01 values TRUE/FALSE // EMU_DataBitRate - Frame Clocks Per Bit // EMU_SubCarrier_T0 - 1st sub-carrier Frame Clocks // EMU_SubCarrier_T1 - 2nd sub-carrier Frame Clocks // EMU_Manchester - Manchester Encoding TRUE/FALSE // EMU_BiPhase - BiPhase Encoding TRUE/FALSE void __ISR(_TIMER_3_VECTOR, ipl7auto) emulation_send_bit (void) { static unsigned int count= 0; // ack interrupt mT3ClearIntFlag(); #ifdef RFIDLER_DEBUG // show bit value //DEBUG_PIN_1= EMU_ThisBit; // show tick //DEBUG_PIN_2= !DEBUG_PIN_2; #endif // are we finished? if(EMU_ThisBit == '*') { if(!EMU_Background && !(EMU_Repeat--)) { CloseTimer3(); mLED_Emulate_Off(); count= 0; COIL_OUT= 0; return; } // repeat EMU_Data= EMU_Reset_Data; EMU_ThisBit= *(EMU_Data++); count= 0; } // count reflects the FC we are about to process, starting from 1 // so we can calulate bit periods and sub-carriers. // count should be reset after each full bit. ++count; // output and set up bit for next tick switch(EmulationMode) { // ASK / OOK - we get called once per bit just toggle the line according to bit value // // MANCHESTER/BI_PHASE - we get called twice per bit, half-bit toggles // case MOD_MODE_ASK_OOK: // output half-bit? manchester always toggles, biphase only toggles if it's a 1 if(count == EMU_SubCarrier_T0 && (RFIDlerConfig.Manchester || (RFIDlerConfig.BiPhase && (EMU_ThisBit ^ RFIDlerConfig.Invert)))) COIL_OUT= !COIL_OUT; // output current bit else { // biphase always toggles on a full bit period if(RFIDlerConfig.BiPhase) { if (count % EMU_DataBitRate) COIL_OUT= !COIL_OUT; } else COIL_OUT= (EMU_ThisBit ^ RFIDlerConfig.Invert); } // is this a bit period? if (count == EMU_DataBitRate) { // set the next bit value EMU_ThisBit= *(EMU_Data++); count= 0; } break; // FSK - we get called N times per bit // create sub-carriers T0 & T1 for bit values 0 and 1 // e.g. if bit period N is 40 ticks (RF/40) and T0 is RF/8 and T1 is RF/5 // then T0 is 8 ticks and T1 is 5 ticks. // note that we must independantly check for end of bit period for each sub-carrier // to cater for cases where there is no common clock multiple (e.g. HID: RF/8 & RF/10) case MOD_MODE_FSK1: case MOD_MODE_FSK2: // output sub-carriers // if current bit is a '1' if(EMU_ThisBit ^ RFIDlerConfig.Invert) { if(!(count % EMU_SubCarrier_T1)) COIL_OUT= !COIL_OUT; // rounded bit period? if (!(count % ((EMU_DataBitRate / EMU_SubCarrier_T1) * EMU_SubCarrier_T1))) { // set the next bit value EMU_ThisBit= *(EMU_Data++); count= 0; } } // if current bit is a '0' else { if(!(count % EMU_SubCarrier_T0)) COIL_OUT= !COIL_OUT; // rounded bit period? if (!(count % ((EMU_DataBitRate / EMU_SubCarrier_T0) * EMU_SubCarrier_T0))) { // set the next bit value EMU_ThisBit= *(EMU_Data++); count= 0; } } break; // TODO: PSK2, PSK3 // PSK1 - we get called N times per bit // create sub-carrier // half-bit phase shift if bit changes case MOD_MODE_PSK1: // output sub-carrier? if(!(count % EMU_SubCarrier_T0)) COIL_OUT= !COIL_OUT; // output current bit else COIL_OUT= (EMU_ThisBit ^ RFIDlerConfig.Invert); // is this is a bit period? if (count == EMU_DataBitRate) { // set the next bit value EMU_ThisBit= *(EMU_Data++); count= 0; } break; default: break; } //#ifdef RFIDLER_DEBUG // // show modulator // DEBUG_PIN_3= COIL_OUT; //#endif }