Exemple #1
0
void main(void) {

    TRISA = 0x0;
    TRISB = 0x0;
    TRISC = 0x0;
    PORTA = 0x0;
    PORTB = 0x0;
    PORTC = 0x0;
    LATA = 0x0;
    LATB = 0x0;
    LATC = 0x0;

    signed char length;
    unsigned char msgtype;
    uart_comm uc;
    i2c_comm ic;
    unsigned char msgbuffer[MSGLEN + 1];
    uart_thread_struct uthread_data; // info for uart_lthread
    timer0_thread_struct t0thread_data;
    timer1_thread_struct t1thread_data; // info for timer1_lthread
    encoder_struct encoder_data;
    sensor_data sensors;

#ifdef __USE18F2680
    OSCCON = 0xFC; // see datasheet
    // We have enough room below the Max Freq to enable the PLL for this chip
    OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line
#else
    OSCCON = 0x82; // see datasheeet
    OSCTUNEbits.PLLEN = 0; // Makes the clock exceed the PIC's rated speed if the PLL is on
#endif

#ifdef SENSORPIC
    i2c2_comm ic2;
    init_i2c2(&ic2);
#endif

    // initialize everything
    init_uart_comm(&uc);
    init_i2c(&ic);
    init_encoder(&encoder_data);
    init_timer0_lthread(&t0thread_data);
    init_timer1_lthread(&t1thread_data);
    init_uart_lthread(&uthread_data);
    init_queues();

#ifdef MASTERPIC

    // Enable and set I2C interrupt to high
    i2c_configure_master();
    IPR1bits.SSPIP = 1;
    PIE1bits.SSPIE = 1;

    // initialize Timer0 to go off approximately every 10 ms
    //OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_1);
    CloseTimer0();
    INTCONbits.TMR0IE 	= 0;	//Enable Timer0 Interrupt

    // Configure UART
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
            USART_CONT_RX & USART_BRGH_LOW, 9); // 19.2kHz (197910/t - 1 = target)
    IPR1bits.RCIP = 0;
    IPR1bits.TXIP = 0;
    uc.expected = 1;

    PORTB   = 0x0;
    LATB    = 0x0;
    TRISB   = 0x0;
#endif //MASTERPIC

#ifdef SENSORPIC

    // Enable and set I2C interrupt to high
    i2c_configure_slave(SENSORPICADDR);
    IPR1bits.SSPIP = 1;
    PIE1bits.SSPIE = 1;

    i2c2_configure_master();
    IPR3bits.SSP2IP = 1;
    PIE3bits.SSP2IE = 1;

    // Open ADC on channel 1
    ADCON0= 0x01;
    ADCON1=0x30;
    ADCON2=0xa1;
    TRISA=0x0F;
    PIE1bits.ADIE = 1; //Enabling ADC interrupts
    IPR1bits.ADIP = 0; //Setting A/D priority

    // initialize Timer0 to go off approximately every 10 ms
    OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_128);
    INTCONbits.TMR0IE 	= 1;	//Enable Timer0 Interrupt
    INTCON2bits.TMR0IP	= 0;	//TMR0 set to Low Priority Interrupt
#endif //SENSORPIC

#ifdef MOTORPIC
    i2c_configure_slave(MOTORPICADDR);
    // Enable and set I2C interrupt to high
    IPR1bits.SSPIP = 1;
    PIE1bits.SSPIE = 1;

    // Configure UART
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
            USART_CONT_RX & USART_BRGH_LOW, 0x9);
    IPR1bits.RCIP = 0;
    IPR1bits.TXIP = 0;

    INTCONbits.TMR0IE = 0;	// Disable Timer0 Interrupt
    
    PORTB   = 0x0;
    LATB    = 0x0;
    TRISB   = 0x0;

    // Set up RB5 as interrupt
    TRISBbits.RB5 = 1;
    INTCON2bits.RBIP = 0;
#endif //MOTORPIC

#ifdef MASTERPIC
    //init_communication(MOTORPICADDR);
    //init_communication(SENSORPICADDR);
#endif

    // Peripheral interrupts can have their priority set to high or low
    // enable high-priority interrupts and low-priority interrupts
    enable_interrupts();

    while (1) {

        // Spins until a message appears
        block_on_To_msgqueues();

        // Continuously check high priority messages until it is empty
        while((length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer)) >= 0) {
            switch (msgtype) {
                case MSGT_I2C_RQST: {
#ifdef SENSORPIC
                    char command = msgbuffer[0];

                    char length = 0;
                    char buffer[8];

                    switch(command) {
                        case SHORT_IR1_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x11;
                        }   break;
                        case SHORT_IR2_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x22;
                        }   break;
                        case MID_IR1_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x33;
                        }   break;
                        case MID_IR2_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x44;
                        }   break;
                        case COMPASS_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x55;
                        }   break;
                        case ULTRASONIC_REQ: {
                            length = 2;
                            buffer[0] = command;
                            buffer[1] = 0x66;
                        }   break;
                        default: {
                            length = 2;
                            buffer[0] = 0x0;
                            buffer[1] = 0x0;
                        }   break;
                    }
                    if(length > 0) {
                        start_i2c_slave_reply(length,buffer);
                    }
#endif
#ifdef MOTORPIC
                    unsigned char buffer[8];
                    unsigned char length = 0;
                    
                    char command = msgbuffer[0];
                    buffer[0] = command;

                    switch(command) {
                        case MOVE_FORWARD_CMD: {
                            length = 1;
                            motor_move_forward(10);
                            INTCONbits.RBIE = 1; // Temporary
                        }   break;
                        case MOVE_BACKWARD_CMD: {
                            length = 1;
                            motor_move_backward(10);
                        }   break;
                        case TURN_RIGHT_CMD: {
                            length = 1;
                            motor_turn_right(10);
                        }   break;
                        case TURN_LEFT_CMD: {
                            length = 1;
                            motor_turn_left(10);
                        }   break;
                        case STOP_CMD: {
                            length = 1;
                            motor_stop();
                        }   break;
                        case DISTANCE_REQ: {
                            INTCONbits.RBIE = 0; // Temporary
                            buffer[1] = encoder_to_distance();
                            length = 2;
                        }   break;
                        default: {
                            buffer[0] = 0x0;
                            length = 1;
                        }   break;
                    }
                    if(length > 0) {
                        start_i2c_slave_reply(length,buffer);
                    }
#endif
                }   break;
                case MSGT_I2C_DATA: {
                }   break;
                case MSGT_I2C_MASTER_SEND_COMPLETE: {
#ifdef MASTERPIC
/*                    char command = msgbuffer[0];
                    switch(command) {
                        case MOVE_FORWARD_CMD:
                        case MOVE_BACKWARD_CMD:
                        case TURN_RIGHT_CMD:
                        case TURN_LEFT_CMD:
                        case STOP_CMD: {
                            i2c_master_recv(1,MOTORPICADDR);
                        }   break;
                        case DISTANCE_REQ: {
                            i2c_master_recv(2,MOTORPICADDR);
                        }   break;
                        case SHORT_IR1_REQ:
                        case SHORT_IR2_REQ:
                        case MID_IR1_REQ:
                        case MID_IR2_REQ:
                        case COMPASS_REQ:
                        case ULTRASONIC_REQ: {
                            i2c_master_recv(2,SENSORPICADDR);
                        }   break;
                        default: {
                        }   break;
                    }*/
#endif
#ifdef SENSORPIC
                    // Start receiving data from sensor
                    if(t0thread_data.currentState == readCompassState) {
                        i2c2_master_recv(6,COMPASSADDR);
                    }
                    else if(t0thread_data.currentState == readUltrasonicState) {
                        i2c2_master_recv(2,ULTRASONICADDR);
                    }
#endif
                }   break;
                case MSGT_I2C_MASTER_SEND_FAILED: {
                }   break;
                case MSGT_I2C_MASTER_RECV_COMPLETE: {
#ifdef MASTERPIC
                    char buffer[2];
                    char length = 0;
                    
                    char command = msgbuffer[0];                    
                    switch(command) {
                        case MOVE_FORWARD_CMD:
                        case MOVE_BACKWARD_CMD:
                        case TURN_RIGHT_CMD:
                        case TURN_LEFT_CMD:
                        case STOP_CMD: {
                            buffer[0] = command;
                            length = 1;
                        }   break;
                        case DISTANCE_REQ: {
                            buffer[0] = command;
                            buffer[1] = msgbuffer[1];
                            length = 2;
                        }   break;
                        case SHORT_IR1_REQ:
                        case SHORT_IR2_REQ:
                        case MID_IR1_REQ:
                        case MID_IR2_REQ:
                        case COMPASS_REQ:
                        case ULTRASONIC_REQ: {
                            buffer[0] = command;
                            buffer[1] = msgbuffer[1];
                            length = 2;
                        }   break;
                        default: {
                            length = 0;
                        }   break;
                    }
                    if(length > 0) {
                        start_uart_send(length,buffer);
                    }
#endif
#ifdef SENSORPIC
                    // Received data from sensor
                    if(t0thread_data.currentState == readCompassState) {
                        sensors.compassData[0] = msgbuffer[0];
                        sensors.compassData[1] = msgbuffer[1];
                        sensors.compassData[2] = msgbuffer[2];
                        sensors.compassData[3] = msgbuffer[3];
                        sensors.compassData[4] = msgbuffer[4];
                        sensors.compassData[5] = msgbuffer[5];
                    }
                    else if(t0thread_data.currentState == readUltrasonicState) {
                        
                    }
#endif
                }   break;
                case MSGT_I2C_MASTER_RECV_FAILED: {
                }   break;
                case MSGT_I2C_DBG: {
                }   break;
                default: {
                }   break;
            }
        }

        // Error check
        if (length != MSGQUEUE_EMPTY) {
            // Handle Error
        }

        // Check the low priority queue
        if ((length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer)) >= 0) {
            switch (msgtype) {
                case MSGT_TIMER0: {
                    timer0_lthread(&t0thread_data, msgtype, length, msgbuffer);
                }   break;
                case MSGT_TIMER1:
                    timer1_lthread(&t1thread_data, msgtype, length, msgbuffer);
                    break;
                case ADCSC1_ADCH:
                    ADC_lthread(&ADCthread_data, msgtype, length, msgbuffer,&t0thread_data);
                    break;
                case MSGT_OVERRUN:
                case MSGT_UART_DATA: {
                    uart_lthread(&uthread_data, msgtype, length, msgbuffer);
                }   break;
                case MSGT_UART_SEND_COMPLETE: {
                }   break;
                default:
                    break;
            }
        }

        // Error check
        else if (length != MSGQUEUE_EMPTY) {
            // Handle error
        }
    }
}
Exemple #2
0
//Initialize and enables all the peripherals
void init_peripherals(void)
{
	//Motor control variables & peripherals:
	init_motor();
	
	//Init Control:
	init_ctrl_data_structure();
	
	//Timebases:
	init_tb_timers();
	
	//UART 2 - RS-485
	init_rs485();
	
	//Analog, expansion port:
	init_analog();
	
	//Clutch:
	init_clutch();	
	
	//Enable Global Interrupts
    CyGlobalIntEnable; 
	
	//I2C1 (internal, potentiometers, Safety-CoP & IMU)
	init_i2c1();
	
	//Peripherals that depend on I2C:
	#ifdef USE_I2C_INT	
		
		//MPU-6500 IMU:
		#ifdef USE_IMU
		init_imu();
		CyDelay(25);
		init_imu();
		CyDelay(25);
		init_imu();
		CyDelay(25);
		#endif	//USE_IMU
		
		//Strain amplifier:
		#ifdef USE_STRAIN
		init_strain();
		#endif	//USE_STRAIN
		
	#endif	//USE_I2C_INT	
	
	//I2C2 (external)	
	#ifdef USE_I2C_EXT
	
	//Enable pull-ups:
	I2C_OPT_PU_Write(1);
		
	//I2C2 peripheral:
	init_i2c2();
	
	//Set RGB LED - Starts Green
	i2c_write_minm_rgb(SET_RGB, 0, 255, 0);
	
	#endif //USE_I2C_EXT
	
	//Magnetic encoder:
	init_as5047();
	
	// First DieTemp reading is always inaccurate -- throw out the first one
	#ifdef USE_DIETEMP	
	DieTemp_1_GetTemp(&temp);
	#endif
	
	//USB CDC
	#ifdef USE_USB	
	init_usb();
	#endif	//USE_USB
}