Ejemplo n.º 1
0
extern void timerStart(timer * pTimer, const timerCalc * pTimerCalc)
{
  pTimer->m_TimerCalc = *pTimerCalc;

  const uint16_t        PrescalerBits = pTimer->m_TimerCalc.PrescalerBits;
  const uint16_t        PriorityBits  = pTimer->m_TimerCalc.PriorityBits;
  const uint16_t        Ticks         = pTimer->m_TimerCalc.Ticks;
  const timer_tCallback pCallback     = pTimer->m_pCallback;

  pTimer->m_OverflowCount = pTimer->m_TimerCalc.OverflowCount;

  switch (pTimer->m_TimerNumber) {
    case 1: 
      OpenTimer1(T1_ON | T1_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer1((pCallback == NULL ? T1_INT_OFF : T1_INT_ON) | PriorityBits);
      break;
    case 2: 
      OpenTimer2(T2_ON | T2_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer2((pCallback == NULL ? T2_INT_OFF : T2_INT_ON) | PriorityBits);
      break;
    case 3: 
      OpenTimer3(T3_ON | T3_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer3((pCallback == NULL ? T3_INT_OFF : T3_INT_ON) | PriorityBits);
      break;
    case 4: 
      OpenTimer4(T4_ON | T4_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer4((pCallback == NULL ? T4_INT_OFF : T4_INT_ON) | PriorityBits);
      break;
    case 5: 
      OpenTimer5(T5_ON | T5_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer5((pCallback == NULL ? T5_INT_OFF : T5_INT_ON) | PriorityBits);
      break;
  }
}
Ejemplo n.º 2
0
/*
 * Setup a timer for a regular tick.
 */
void prvSetupTimerInterrupt( void )
{
const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;

	OpenTimer5( ( T5_ON | T5_PS_1_8 | T5_SOURCE_INT ), ulCompareMatch );
	ConfigIntTimer5( T5_INT_ON | configKERNEL_INTERRUPT_PRIORITY );
}
Ejemplo n.º 3
0
void vMBPortTimersEnable(void){
    /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
	OpenTimer5();
	xMBPortTimersInit(usTimerT35_50us);		//Adjust timing for real timeouts
	//ConfigIntTimer3(T2_INT_ON | T2_INT_PRIOR_2);
	_T5IE = 1;
	T5CONbits.TON = 1; // Start Timer
//	INTCONbits.GIE = 1;
//INTEnableSystemMultiVectoredInt();
        
}
Ejemplo n.º 4
0
//******************************************************************************
//Public Function Definitions
//******************************************************************************
void Orientation_start()
{
    //Setup Timer5
    INTClearFlag(INT_T5);
    INTSetVectorPriority(INT_TIMER_5_VECTOR, INT_PRIORITY_LEVEL_3);
    INTSetVectorSubPriority(INT_TIMER_5_VECTOR, INT_SUB_PRIORITY_LEVEL_1);
    INTEnable(INT_T5, INT_ENABLED);

    //Turn on clock
    OpenTimer5(T5_ON | T5_SOURCE_INT | T1_PS_1_64, 12500);//50hz @ 40MHz
    //OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_32, 3333); //375hz @ 40MHz  (0.0026664 sec)
}
Ejemplo n.º 5
0
void fis_Timer5_config(unsigned int period){
    //                      7654321076543210
    unsigned int config = 0b1000000000110000; //T4_ON & T4_GATE_ON & T4_IDLE_CON & T4_PS_1_1 & T4_SOURCE_INT;
    //                      7654321076543210
    //unsigned int period = 0b0000000000000111;
    WriteTimer5(0x0000);
    OpenTimer5( config, period );

    //ConfigIntTimer5(T5_INT_ON & T5_INT_PRIOR_1);
    EnableIntT5;

    //printf("t5_config\n");
}
Ejemplo n.º 6
0
//Timer 5 setup function
int sysServiceConfigT5(unsigned int T5conval, unsigned int T5perval,
                        unsigned int T5intconval){
    //Todo: is there any way to have a compile time semaphore here?
    if(T5_already_confgured){
        return -1;
    }
    else{
        T5_already_confgured = 1;
        OpenTimer5(T5conval, T5perval);
        ConfigIntTimer5(T5intconval);
        return 0;
    }
}
Ejemplo n.º 7
0
inline void Timer5_Setup(void)
{
    OpenTimer5(
            T5_OFF &
            T5_IDLE_CON &
            T5_GATE_OFF &
            T5_PS_1_256 &
            T5_SOURCE_INT,
            39063); //for 250 ms.

    ConfigIntTimer5(
            T5_INT_PRIOR_2 &
            T5_INT_ON
                    );

    return;
}
//********************************
//********************************
//********** INITIALISE **********
//********************************
//********************************
void initialise (void)
{
	BYTE data;
	
	//##### GENERAL NOTE ABOUT PIC32'S #####
	//Try and use the peripheral libraries instead of special function registers for everything (literally everything!) to avoid
	//bugs that can be caused by the pipeline and interrupts.
	

	//---------------------------------
	//----- CONFIGURE PERFORMANCE -----
	//---------------------------------
	
	//----- SETUP EVERYTHING FOR OPTIMUM PERFORMANCE -----
	SYSTEMConfigPerformance(80000000ul);		//Note this sets peripheral bus to '1' max speed (regardless of configuration bit setting)
												//Use PBCLK divider of 1:1 to calculate UART baud, timer tick etc


	//----- SET PERIPHERAL BUS DIVISOR -----
	//To minimize dynamic power the PB divisor should be chosen to run the peripherals at the lowest frequency that provides acceptable system performance
	mOSCSetPBDIV(OSC_PB_DIV_2);			//OSC_PB_DIV_1, OSC_PB_DIV_2, OSC_PB_DIV_4, OSC_PB_DIV_8, 

	//----- SETUP INTERRUPTS -----
	INTEnableSystemMultiVectoredInt();

	//-------------------------
	//----- SETUP IO PINS -----
	//-------------------------
	//(Device will powerup with all IO pins as inputs)

	//----- TURN OFF THE JTAG PORT -----
	//(JTAG is on by default)
	//mJTAGPortEnable(0);		//Must be on for Microchip Multimedia Development board

	#define	PORTA_IO	0xc2ff				//Setup the IO pin type (0 = output, 1 = input)
	mPORTAWrite(0xc033);					//Set initial ouput pin states
	mPORTASetPinsDigitalIn(PORTA_IO);		//(Sets high bits as input)
	mPORTASetPinsDigitalOut(~PORTA_IO);		//(Sets high bits as output)
	
	#define	PORTB_IO	0xfbff				//Setup the IO pin type (0 = output, 1 = input)
	mPORTBWrite(0x6d13);					//Set initial ouput pin states
	mPORTBSetPinsDigitalIn(PORTB_IO);		//(Sets high bits as input)
	mPORTBSetPinsDigitalOut(~PORTB_IO);		//(Sets high bits as output)

	mPORTBSetPinsDigitalIn(BIT_0 | BIT_1 | BIT_3 | BIT_4 | BIT_15);			//Joystick inputs

	#define	PORTC_IO	0xf01e				//Setup the IO pin type (0 = output, 1 = input)
	mPORTCWrite(0x3018);					//Set initial ouput pin states
	mPORTCSetPinsDigitalIn(PORTC_IO);		//(Sets high bits as input)
	mPORTCSetPinsDigitalOut(~PORTC_IO);		//(Sets high bits as output)

	#define	PORTD_IO	0x7bfe				//Setup the IO pin type (0 = output, 1 = input)
	mPORTDWrite(0xbdaf);					//Set initial ouput pin states
	mPORTDSetPinsDigitalIn(PORTD_IO);		//(Sets high bits as input)
	mPORTDSetPinsDigitalOut(~PORTD_IO);		//(Sets high bits as output)

	mPORTDSetPinsDigitalOut(BIT_2 | BIT_1);		//LED's 2 and 3
	mPORTDSetPinsDigitalIn(BIT_9);

	#define	PORTE_IO	0x03ff				//Setup the IO pin type (0 = output, 1 = input)
	mPORTEWrite(0x02a2);					//Set initial ouput pin states
	mPORTESetPinsDigitalIn(PORTE_IO);		//(Sets high bits as input)
	mPORTESetPinsDigitalOut(~PORTE_IO);		//(Sets high bits as output)

	#define	PORTF_IO	0x111f				//Setup the IO pin type (0 = output, 1 = input)
	mPORTFWrite(0x0039);					//Set initial ouput pin states
	mPORTFSetPinsDigitalIn(PORTF_IO);		//(Sets high bits as input)
	mPORTFSetPinsDigitalOut(~PORTF_IO);		//(Sets high bits as output)

	#define	PORTG_IO	0xd3cf				//Setup the IO pin type (0 = output, 1 = input)
	mPORTGWrite(0xf203);					//Set initial ouput pin states
	mPORTGSetPinsDigitalIn(PORTG_IO);		//(Sets high bits as input)
	mPORTGSetPinsDigitalOut(~PORTG_IO);		//(Sets high bits as output)



	//Read pins using:
	// mPORTAReadBits(BIT_0);
	//Write pins using:
	// mPORTAClearBits(BIT_0);
	// mPORTASetBits(BIT_0);
	// mPORTAToggleBits(BIT_0);



	//----- INPUT CHANGE NOTIFICATION CONFIGURATION -----
	//EnableCN0();
	ConfigCNPullups(CN2_PULLUP_ENABLE | CN3_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE | CN12_PULLUP_ENABLE);		//Joystick pins



	//----- SETUP THE A TO D PINS -----
	ENABLE_ALL_DIG;


	//---------------------
	//----- SETUP USB -----
	//---------------------
	//The USB specifications require that USB peripheral devices must never source current onto the Vbus pin.  Additionally, USB peripherals should not source
	//current on D+ or D- when the host/hub is not actively powering the Vbus line.  When designing a self powered (as opposed to bus powered) USB peripheral
	//device, the firmware should make sure not to turn on the USB module and D+ or D- pull up resistor unless Vbus is actively powered.  Therefore, the
	//firmware needs some means to detect when Vbus is being powered by the host. A 5V tolerant I/O pin can be connected to Vbus (through a resistor), and
	//can be used to detect when Vbus is high (host actively powering), or low (host is shut down or otherwise not supplying power).  The USB firmware
	//can then periodically poll this I/O pin to know when it is okay to turn on the USB module/D+/D- pull up resistor.  When designing a purely bus powered
	//peripheral device, it is not possible to source current on D+ or D- when the host is not actively providing power on Vbus. Therefore, implementing this
	//bus sense feature is optional.  This firmware can be made to use this bus sense feature by making sure "USE_USB_BUS_SENSE_IO" has been defined in the
	//HardwareProfile.h file.    
    //	#if defined(USE_USB_BUS_SENSE_IO)
    //	tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h
    //	#endif
    
	//If the host PC sends a GetStatus (device) request, the firmware must respond	and let the host know if the USB peripheral device is currently bus powered
	//or self powered.  See chapter 9 in the official USB specifications for details regarding this request.  If the peripheral device is capable of being both
	//self and bus powered, it should not return a hard coded value for this request.  Instead, firmware should check if it is currently self or bus powered, and
	//respond accordingly.  If the hardware has been configured like demonstrated on the PICDEM FS USB Demo Board, an I/O pin can be polled to determine the
	//currently selected power source.  On the PICDEM FS USB Demo Board, "RA2" is used for	this purpose.  If using this feature, make sure "USE_SELF_POWER_SENSE_IO"
	//has been defined in HardwareProfile.h, and that an appropriate I/O pin has been mapped to it in HardwareProfile.h.
    //	#if defined(USE_SELF_POWER_SENSE_IO)
    //	tris_self_power = INPUT_PIN;	// See HardwareProfile.h
    //	#endif
    
    
    //Enable the USB port now - we will check to see if Vbus is  powered at the end of init and disable it if not.
    //USBDeviceInit();	//usb_device.c.  Initializes USB module SFRs and firmware variables to known states.

	

	//------------------------
	//----- SETUP TIMERS -----
	//------------------------
	//(INCLUDE THE USAGE OF ALL TIMERS HERE EVEN IF NOT SETUP HERE SO THIS IS THE ONE POINT OF
	//REFERENCE TO KNOW WHICH TIMERS ARE IN USE AND FOR WHAT).

	//----- SETUP TIMER 1 -----
	//Used for: Available
	//OpenTimer1((T1_ON | T1_IDLE_CON | T1_GATE_OFF | T1_PS_1_4 | T1_SOURCE_INT), 20000);

	//----- SETUP TIMER 2 -----
	//Used for: 
	//OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_SOURCE_INT), 0xffff);		//0xffff = 305Hz

	//----- SETUP TIMER 3 -----
	//Used for: 
	//OpenTimer3((T3_ON | T3_IDLE_CON | T3_GATE_OFF | T3_PS_1_1 | T3_SOURCE_INT), PIEZO_TIMER_PERIOD);

	//----- SETUP TIMER 4 -----
	//Used for: 
	//OpenTimer4((T4_ON | T4_IDLE_CON | T4_GATE_OFF | T4_PS_1_1 | T4_SOURCE_INT), 20000);

	//----- SETUP TIMER 5 -----
	//Used for: Heartbeat
	OpenTimer5((T5_ON | T5_IDLE_CON | T5_GATE_OFF | T5_PS_1_1 | T5_SOURCE_INT), 40000);		//1mS with 80MHz osc and PB_DIV_2
	ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_7);											//1=lowest priority to 7=highest priority.  ISR function must specify same value



	//---------------------------------
	//----- SETUP EVAL BOARD CPLD -----
	//---------------------------------
	//Graphics bus width = 16
	mPORTGSetPinsDigitalOut(BIT_14);
	mPORTGSetBits(BIT_14);

	//SPI source select = SPI3 (not used)
	mPORTGSetPinsDigitalOut(BIT_12);
	mPORTGClearBits(BIT_12);
	
	//SPI peripheral destination select = Expansion Slot (not used)
	mPORTASetPinsDigitalOut(BIT_7 | BIT_6);
	mPORTASetBits(BIT_7);
	mPORTAClearBits(BIT_6);


	//--------------------------------------
    //----- PARALLEL MASTER PORT SETUP -----
	//--------------------------------------
    PMMODE = 0;
    PMAEN = 0;
    PMCON = 0;
    PMMODE = 0x0610;
    PMCONbits.PTRDEN = 1;                   //Enable RD line
    PMCONbits.PTWREN = 1;                   //Enable WR line
    PMCONbits.PMPEN = 1;                    //Enable PMP


	//------------------------------
	//----- INITIALISE DISPLAY -----
	//------------------------------
	display_initialise();


	display_test();

	//LOAD OUR GLOBAL HTML STYLES FILE READY FOR DISPLAY HTML PAGES
	BYTE dummy_styles_count;
	DWORD file_size;
	if (display_html_setup_read_file(global_css, 0, &file_size))
	{
		dummy_styles_count = 0;
		display_html_read_styles(&file_size, &dummy_styles_count, 1);			//1 = this is global styles file
	}

}
Ejemplo n.º 9
0
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);
}
Ejemplo n.º 10
0
void InitApp()
{
    _TRISA0 = 0; //led en sortie
    _TRISA1 = 0;
    led = 0;
    led2 = 0;

    //Sortie enable
    _TRISB9 = 0;
    _TRISB11 = 0;
    _ODCB9 = 1; //Open drain RB9 (dir 1)
    _ODCB11 = 1; //Open drain RB11 (dir 2)
    _ODCB10 = 1; //open drain RB10 PWM1H3

    //Le microswicth sur la pin RC5 (par exemple), on la met en entrée
    _TRISC5 = 1; //bumper bas de pince
    //Et on active la pullup qui va bien (registres CNPU1 et CNPU2)
    _CN26PUE = 1;

    _ODCC9 = 1; // Open drain sur la pin RC9 (pour les AX12)

    _TRISA4 = 1;
    _TRISA8 = 1;
    _TRISA9 = 1;
    _TRISB2 = 1; //RTS ?
    _TRISB3 = 1; //FLush ?
    _TRISA2 = 1; //CLK12 ?

    _TRISB4 = 1; // Arret d'urgence
    _CN1PUE = 1; // avec pullup
    _TRISB12 = 1; // switch 1
    _CN14PUE = 1; // avec pullup
    _TRISB13 = 1; // switch 2
    _CN13PUE = 1; // avec pullup
    _TRISB14 = 1; // switch 3
    _CN12PUE = 1; // avec pullup
    _TRISB15 = 1; // Laisse
    _CN11PUE = 1; // avec pullup

    OpenUART2(UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_FLOW
              & UART_UEN_00 & UART_DIS_WAKE & UART_DIS_LOOPBACK
              & UART_DIS_ABAUD & UART_UXRX_IDLE_ONE & UART_BRGH_SIXTEEN
              & UART_NO_PAR_8BIT & UART_1STOPBIT,
              UART_INT_TX_BUF_EMPTY & UART_IrDA_POL_INV_ZERO
              & UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_TX_BUF_NOT_FUL & UART_INT_RX_CHAR
              & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR,
              BRGVALAX12);

    ConfigIntUART2(UART_RX_INT_PR4 & UART_RX_INT_EN
                   & UART_TX_INT_PR4 & UART_TX_INT_DIS);

    OpenTimer2(T2_ON & T2_GATE_OFF & T2_PS_1_256 & T2_32BIT_MODE_OFF & T2_SOURCE_INT, 1500);
    ConfigIntTimer2(T2_INT_PRIOR_3 & T2_INT_ON); //Interruption ON et priorite 3

    OpenTimer5(T5_OFF & T5_GATE_OFF & T5_PS_1_1 & T5_SOURCE_INT, 40000);
    ConfigIntTimer5(T5_INT_PRIOR_2 & T5_INT_ON);

    OpenQEI1(QEI_DIR_SEL_QEB & QEI_INT_CLK & QEI_INDEX_RESET_DISABLE & QEI_CLK_PRESCALE_1
             & QEI_NORMAL_IO & QEI_MODE_x4_MATCH & QEI_UP_COUNT,0);
//    ConfigIntQEI1(QEI_INT_DISABLE);
//    WriteQEI1(65535);        //Valeur pour declencher l'interruption du module QEI

    _QEA1R = 5;     //Module QEI 1 phase A sur RB5
    _QEB1R = 6;     //Module QEI 1 phase B sur RB6
    POS1CNT = 0; //valeur QEI

    IFS2bits.SPI2IF = 0; // Flag SPI2 Event Interrupt Priority
    IPC8bits.SPI2IP = 2; // Priority SPI2 Event Interrupt Priority
    IEC2bits.SPI2IE = 1; //Enable SPI2 Event Interrupt Priority

    // activation de la priorité des interruptions
    _NSTDIS = 0;
}
Ejemplo n.º 11
0
/**
 * @function TmrSetFrequency
 * @brief configure a given timer with a desired frequency
 * @param tmr_t tmr_id: timer id
 * @param uint32_t desiredFrequency: desired frequency, in Hz
 * @return int8_t: 0 sucess, otherwise error
 */
int8_t TmrSetFrequency(tmr_t tmr_id, uint32_t desiredFrequency) {

  uint16_t tmrValue = 0xFFFF;
  uint16_t prescale;
  uint32_t cfg;
  int8_t res = -1;

  
    switch(tmr_id) {
    
      case TMR_1:
        if(SetTimerFrequency(TMR_TYPE_A, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_1);
          mT1ClearIntFlag();
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_1 | T1_INT_SUB_PRIOR_1);
          cfg = T1_SOURCE_INT | T1_IDLE_CON;
          if(prescale == 1) cfg |= T1_PS_1_1;
          else if(prescale == 8) cfg |= T1_PS_1_8;
          else if(prescale == 64) cfg |= T1_PS_1_64;
          else cfg |= T1_PS_1_256;
          OpenTimer1(cfg, tmrValue);
          timer1Configured = 1;
          res = 0;
        }
        break;
        
      case TMR_4:
        if(SetTimerFrequency(TMR_TYPE_B, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_4);
          mT4ClearIntFlag();
          ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_4 | T4_INT_SUB_PRIOR_1);
          cfg = T4_SOURCE_INT | T4_IDLE_CON;
          if(prescale == 1) cfg |= T4_PS_1_1;
          else if(prescale == 2) cfg |= T4_PS_1_2;
          else if(prescale == 4) cfg |= T4_PS_1_4;
          else if(prescale == 8) cfg |= T4_PS_1_8;
          else if(prescale == 16) cfg |= T4_PS_1_16;
          else if(prescale == 32) cfg |= T4_PS_1_32;
          else if(prescale == 64) cfg |= T4_PS_1_64;
          else cfg |= T4_PS_1_256;
          OpenTimer4(cfg, tmrValue);
          timer4Configured = 1;
          res = 0;
        }
        break;
        
      case TMR_5:
        if(SetTimerFrequency(TMR_TYPE_B, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_5);
          mT5ClearIntFlag();
          ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_5 | T5_INT_SUB_PRIOR_1);
          cfg = T5_SOURCE_INT | T5_IDLE_CON;
          if(prescale == 1) cfg |= T5_PS_1_1;
          else if(prescale == 2) cfg |= T5_PS_1_2;
          else if(prescale == 4) cfg |= T5_PS_1_4;
          else if(prescale == 8) cfg |= T5_PS_1_8;
          else if(prescale == 16) cfg |= T5_PS_1_16;
          else if(prescale == 32) cfg |= T5_PS_1_32;
          else if(prescale == 64) cfg |= T5_PS_1_64;
          else cfg |= T5_PS_1_256;
          OpenTimer5(cfg, tmrValue);
          timer5Configured = 1;
          res = 0;
        }
        break;
        
      default:
        break;
    }
  return res;
}