int uart_lthread(uart_thread_struct *uptr, int msgtype, int length, unsigned char *msgbuffer) {
#ifndef __USE18F26J50
    unsigned char address;
    if (msgtype == MSGT_OVERRUN) {
    } else if (msgtype == MSGT_UART_DATA) {
        // print the message (this assumes that the message
        // 		was a printable string)
        msgbuffer[length] = '\0'; // null-terminate the array as a string=

#if I2C_MASTER
        address = msgbuffer[0];
        i2c_master_set_target(address & 0xFE);
        if (address & 0x1) {
            //while(i2c_master_send(0, (void *) 0));
            //while(i2c_master_recv(5) != 0);
            if (i2c_master_recv(5) != 0);
                //DBG1_TOGGLE();

            //uart_send(i2c_communication()->buflen-1, i2c_communication()->buffer+1);
        } else {
            //while(i2c_master_send(length-1, msgbuffer+1) != 0);
            if (i2c_master_send(length-1, msgbuffer+1) != 0);
                //DBG2_TOGGLE();
        }
#else
        start_i2c_slave_reply(length, msgbuffer);
#endif
        
        //DBG0_TOGGLE();
    }
#endif
}
Example #2
0
void timer0_int_handler() {
    WriteTimer0(0);
//    unsigned int val;
//    int length, msgtype;

    // toggle an LED
#ifdef __USE18F2680
    LATBbits.LATB0 = !LATBbits.LATB0;
#endif
    // reset the timer
    
    // try to receive a message and, if we get one, echo it back
//    length = FromMainHigh_recvmsg(sizeof(val), (unsigned char *)&msgtype, (void *) &val);
//    if (length == sizeof (val)) {
//        ToMainHigh_sendmsg(sizeof (val), MSGT_TIMER0, (void *) &val);
//    }

    //unsigned char motorcomm[2] = {0x9F, 0x1F};
#ifdef MOTORPIC
    distMoved++;
    if(distMoved >= distDesired){

        unsigned char motormsg[5] = {0x04, 0x00, 0x00, 0x00, 0x00};
        motormsg[1] = distMoved;
        motormsg[2] = (distMoved & 0x17);
        start_i2c_slave_reply(5, motormsg);

        distMoved = 0;
        unsigned char motorcomm[2] = {0x00, 0x00};
        motorMove(0x00, 0x00, 0);
    }
#endif
    
    //ConvertADC();
    //while( BusyADC()) {
        //LATBbits.LATB1 = 1;
    //}
    //LATBbits.LATB1 = 0;

    
}
Example #3
0
void i2c_slave_int_handler() {
    unsigned char i2c_data;
    unsigned char data_read = 0;
    unsigned char data_written = 0;
    unsigned char msg_ready = 0;
    unsigned char msg_to_send = 0;
    unsigned char overrun_error = 0;
    unsigned char error_buf[3];

    // clear SSPOV
    if (SSPCON1bits.SSPOV == 1) {
        SSPCON1bits.SSPOV = 0;
        // we failed to read the buffer in time, so we know we
        // can't properly receive this message, just put us in the
        // a state where we are looking for a new message
        ic_ptr->status = I2C_IDLE;
        overrun_error = 1;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_OVERRUN;
    }
    // read something if it is there
    if (SSPSTATbits.BF == 1) {
        i2c_data = SSPBUF;
        data_read = 1;
    }

    if (!overrun_error) {
        switch (ic_ptr->status) {
        case I2C_IDLE:
        {
            // ignore anything except a start
            if (SSPSTATbits.S == 1) {
                handle_start(data_read);
                // if we see a slave read, then we need to handle it here
                if (ic_ptr->status == I2C_SLAVE_SEND) {
                    data_read = 0;
                    msg_to_send = 1;
                }
            }
            break;
        }
        case I2C_STARTED:
        {
            // in this case, we expect either an address or a stop bit
            if (SSPSTATbits.P == 1) {
                // we need to check to see if we also read an
                // address (a message of length 0)
                ic_ptr->event_count++;
                if (data_read) {
                    if (SSPSTATbits.D_A == 0) {
                        msg_ready = 1;
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                    }
                }
                ic_ptr->status = I2C_IDLE;
            } else if (data_read) {
                ic_ptr->event_count++;
                if (SSPSTATbits.D_A == 0) {
                    if (SSPSTATbits.R_W == 0) { // slave write
                        ic_ptr->status = I2C_RCV_DATA;
                    } else { // slave read
                        ic_ptr->status = I2C_SLAVE_SEND;
                        msg_to_send = 1;
                        // don't let the clock stretching bit be let go
                        data_read = 0;
                    }
                } else {
                    ic_ptr->error_count++;
                    ic_ptr->status = I2C_IDLE;
                    ic_ptr->error_code = I2C_ERR_NODATA;
                }
            }
            break;
        }
        case I2C_SLAVE_SEND:
        {
            if (ic_ptr->outbufind < ic_ptr->outbuflen) {
                SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                ic_ptr->outbufind++;
                data_written = 1;
            } else {
                // we have nothing left to send
                ic_ptr->status = I2C_IDLE;
            }
            break;
        }
        case I2C_RCV_DATA:
        {
            // we expect either data or a stop bit or a (if a restart, an addr)
            if (SSPSTATbits.P == 1) {
                // we need to check to see if we also read data
                ic_ptr->event_count++;
                if (data_read) {
                    if (SSPSTATbits.D_A == 1) {
                        ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                        ic_ptr->buflen++;
                        msg_ready = 1;
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                        ic_ptr->status = I2C_IDLE;
                    }
                } else {
                    msg_ready = 1;
                }
                ic_ptr->status = I2C_IDLE;
            } else if (data_read) {
                ic_ptr->event_count++;
                if (SSPSTATbits.D_A == 1) {
                    ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                    ic_ptr->buflen++;
                } else { /* a restart */
                    if (SSPSTATbits.R_W == 1) {
                        ic_ptr->status = I2C_SLAVE_SEND;
                        msg_ready = 1;
                        msg_to_send = 1;
                        // don't let the clock stretching bit be let go
                        data_read = 0;
                    } else { /* bad to recv an address again, we aren't ready */
                        ic_ptr->error_count++;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                        ic_ptr->status = I2C_IDLE;
                    }
                }
            }
            break;
        }
        }
    }

    // release the clock stretching bit (if we should)
    if (data_read || data_written) {
        // release the clock
        if (SSPCON1bits.CKP == 0) {
            SSPCON1bits.CKP = 1;
        }
    }

    // must check if the message is too long, if
    if ((ic_ptr->buflen > MAXI2CBUF - 2) && (!msg_ready)) {
        ic_ptr->status = I2C_IDLE;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
    }

    if (msg_ready) {
        ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
        ToMainHigh_sendmsg(ic_ptr->buflen + 1, MSGT_I2C_DATA, (void *) ic_ptr->buffer);
        ic_ptr->buflen = 0;
    } else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
        error_buf[0] = ic_ptr->error_count;
        error_buf[1] = ic_ptr->error_code;
        error_buf[2] = ic_ptr->event_count;
        ToMainHigh_sendmsg(sizeof (unsigned char) *3, MSGT_I2C_DBG, (void *) error_buf);
        ic_ptr->error_count = 0;
    }
    if (msg_to_send) {
        int length = 0;

        // send to the queue to *ask* for the data to be sent out
        //ToMainHigh_sendmsg(0, MSGT_I2C_RQST, (void *) ic_ptr->buffer);
        if(ic_ptr->buffer[0] == 0xAA) {
            length = 5;

            // Creates the message type and bitmask char values
            unsigned char messageType = 0x01, bitmask = 0x17, checksum;

            // This will hold the message that we want the slave to send
            unsigned char message[5];

            // Stores the appropriate values in the message to be sent
            message[0] = messageType;
            message[1] = adcbuffer[1];
            message[2] = adcbuffer[2];
            message[3] = adcbuffer[3];

            // Creates the checksum
            checksum = message[1] + message[2] + message[3];
            message[4] = checksum & bitmask;

            start_i2c_slave_reply(length, message);
            //adcbuffer[0] = 0; // reset count after send
        } else if(ic_ptr->buffer[0] == 0xBA) {
            // motor stuff
            length = 5;

            unsigned char motormsg[5] = {0x03, 0x04, ((0x04) & 0x17), 0x00, 0x00};
            start_i2c_slave_reply(length, motormsg);

            unsigned char motorcomm[2] = {0x9F, 0x1F};
            if(ic_ptr->buffer[1] == 0xFF) {

                uart_trans(2, motorcomm);
            } else if(ic_ptr->buffer[1] == 0x00) {
                motorcomm[0] = 0x00;
                uart_trans(1, motorcomm);
            }

        }
        msg_to_send = 0;
    }
}
Example #4
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
        }
    }
}
Example #5
0
void main(void) {
    char c;
    signed char length;
    unsigned char msgtype;
    unsigned char last_reg_recvd;
    uart_comm uc;
    i2c_comm ic;
    unsigned char msgbuffer[MSGLEN + 1];
    unsigned char i;
    uart_thread_struct uthread_data; // info for uart_lthread
    timer1_thread_struct t1thread_data; // info for timer1_lthread
    timer0_thread_struct t0thread_data; // info for timer0_lthread

#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
#ifdef __USE18F45J10
    OSCCON = 0x82; // see datasheeet
    OSCTUNEbits.PLLEN = 1; // Makes the clock exceed the PIC's rated speed if the PLL is on 48 HZ
#else
#ifdef __USE18F26J50
    OSCCON = 0xE0; // see datasheeet
    OSCTUNEbits.PLLEN = 1;
#else
#ifdef __USE18F46J50
    OSCCON = 0xE0; //see datasheet
    OSCTUNEbits.PLLEN = 1;
#else
    Something is messed up.
    The PIC selected is not supported or the preprocessor directives are wrong.
#endif
#endif
#endif
#endif

    // initialize my uart recv handling code
    init_uart_recv(&uc);

    // initialize the i2c code
    init_i2c(&ic);

    // init the timer1 lthread
    init_timer1_lthread(&t1thread_data);

    // initialize message queues before enabling any interrupts
    init_queues();

#ifndef __USE18F26J50
    // set direction for PORTB to output
    PORTB = 0xFF;
    //TRISB = 0x0;
    //LATB = 0x0;
#endif

    // how to set up PORTA for input (for the V4 board with the PIC2680)
    /*
            PORTA = 0x0;	// clear the port
            LATA = 0x0;		// clear the output latch
            ADCON1 = 0x0F;	// turn off the A2D function on these pins
            // Only for 40-pin version of this chip CMCON = 0x07;	// turn the comparator off
            TRISA = 0x0F;	// set RA3-RA0 to inputs
     */

    // initialize Timers
    OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_EDGE_RISE & T0_SOURCE_EXT & T0_PS_1_1);
    OpenTimer1(TIMER_INT_ON& T1_SYNC_EXT_OFF  & T1_8BIT_RW & T1_SOURCE_EXT & T1_OSC1EN_OFF & T1_PS_1_1);
    //OpenTimer2(TIMER_INT_ON & T2_PS_1_16)
    
#ifdef __USE18F26J50
    // MTJ added second argument for OpenTimer1()
    OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0);
#else
#ifdef __USE18F46J50
   // OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0);
#else
    //OpenTimer1(TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
#endif
#endif

    // Decide on the priority of the enabled peripheral interrupts
    // 0 is low, 1 is high
    // Timer1 interrupt
    IPR1bits.TMR1IP = 0;
    // USART RX interrupt
    IPR1bits.RCIP = 0;
    // I2C interrupt
    IPR1bits.SSPIP = 1;

    // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D)
#if 1
    // Note that the temperature sensor Address bits (A0, A1, A2) are also the
    // least significant bits of LATB -- take care when changing them
    // They *are* changed in the timer interrupt handlers if those timers are
    //   enabled.  They are just there to make the lights blink and can be
    //   disabled.
    i2c_configure_slave(0x9E);
#else
    // If I want to test the temperature sensor from the ARM, I just make
    // sure this PIC does not have the same address and configure the
    // temperature sensor address bits and then just stay in an infinite loop
    i2c_configure_slave(0x9A);
#ifdef __USE18F2680
    LATBbits.LATB1 = 1;
    LATBbits.LATB0 = 1;
    LATBbits.LATB2 = 1;
#endif
    for (;;);
#endif

    // must specifically enable the I2C interrupts
    PIE1bits.SSPIE = 1;

    // configure the hardware USART device
#ifdef __USE18F26J50
    Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x19);
#else
#ifdef __USE18F46J50
    Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x19);
#else
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x26);


    //Low  & 0x26 for 48Mhz
    //High & 0x26
#endif
#endif

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

    /* Junk to force an I2C interrupt in the simulator (if you wanted to)
    PIR1bits.SSPIF = 1;
    _asm
    goto 0x08
    _endasm;
     */
     
    // printf() is available, but is not advisable.  It goes to the UART pin
    // on the PIC and then you must hook something up to that to view it.
    // It is also slow and is blocking, so it will perturb your code's operation
    // Here is how it looks: printf("Hello\r\n");
   

    // loop forever
    // This loop is responsible for "handing off" messages to the subroutines
    // that should get them.  Although the subroutines are not threads, but
    // they can be equated with the tasks in your task diagram if you
    // structure them properly.

    TRISCbits.RC0 = 1;
    while (1) {
        // Call a routine that blocks until either on the incoming
        // messages queues has a message (this may put the processor into
        // an idle mode)
        block_on_To_msgqueues();

        // At this point, one or both of the queues has a message.  It
        // makes sense to check the high-priority messages first -- in fact,
        // you may only want to check the low-priority messages when there
        // is not a high priority message.  That is a design decision and
        // I haven't done it here.
        length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // This case be handled by your code.
            }
        } else {
            switch (msgtype) {
                case MSGT_TIMER0:
                {
                    timer0_lthread(&t0thread_data, msgtype, length, msgbuffer);
                    break;
                };
                case MSGT_I2C_DATA:
                {
                  break;
                };
                case MSGT_I2C_DBG:
                {
                    // Here is where you could handle debugging, if you wanted
                    // keep track of the first byte received for later use (if desired)
                    last_reg_recvd = msgbuffer[0];
                    break;
                };
                case MSGT_I2C_RQST:
                {
                    // Generally, this is *NOT* how I recommend you handle an I2C slave request
                    // I recommend that you handle it completely inside the i2c interrupt handler
                    // by reading the data from a queue (i.e., you would not send a message, as is done
                    // now, from the i2c interrupt handler to main to ask for data).
                    //
                    // The last byte received is the "register" that is trying to be read
                    // The response is dependent on the register.
                    start_i2c_slave_reply(length, msgbuffer);
                    break;
                };
                case MSGT_MOTOR_COMMAND:
                {
                    motor_lthread(msgtype,length,msgbuffer);
                  break;
                };
                case MSGT_MOTOR_STOP:
                {
                    motor_lthread(msgtype,length,msgbuffer);
                    break;
                };
                case MSGT_UART_SEND:
                {
                    uart_lthread(&uthread_data,msgtype,length,msgbuffer);
                     break;
                };
                default:
                {
                    // Your code should handle this error
                    break;
                };
            };
        }

        // Check the low priority queue
        length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // Your code should handle this situation
            }
        } else {
            switch (msgtype) {
                case MSGT_PARSE:
                {
                    //parser_lthread(msgtype,length,msgbuffer);
                  break;  
                };
                case MSGT_OVERRUN:
                case MSGT_UART_DATA:
                {
                    uart_lthread(&uthread_data, msgtype, length, msgbuffer);
                    break;
                };
                
                default:
                {
                    // Your code should handle this error
                    break;
                };
            };
        }
    }

}
Example #6
0
void i2c_slave_int_handler() {
    unsigned char i2c_data;
    unsigned char data_read = 0;
    unsigned char data_written = 0;
    unsigned char msg_ready = 0;
    unsigned char msg_to_send = 0;
    unsigned char overrun_error = 0;
    unsigned char error_buf[3];

    // clear SSPOV
    if (SSPCON1bits.SSPOV == 1) {
        SSPCON1bits.SSPOV = 0;
        // we failed to read the buffer in time, so we know we
        // can't properly receive this message, just put us in the
        // a state where we are looking for a new message
        ic_ptr->status = I2C_IDLE;
        overrun_error = 1;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_OVERRUN;
    }
    // read something if it is there
    if (SSPSTATbits.BF == 1) {
        i2c_data = SSPBUF;
        data_read = 1;
    }

    if (!overrun_error) {
        switch (ic_ptr->status) {
            case I2C_IDLE:
            {
                // ignore anything except a start
                if (SSPSTATbits.S == 1) {
                    handle_start(data_read);
                    // if we see a slave read, then we need to handle it here
                    if (ic_ptr->status == I2C_SLAVE_SEND) {
                        data_read = 0;
                        msg_to_send = 1;
                    }
                }
                break;
            }
            case I2C_STARTED:
            {
                // in this case, we expect either an address or a stop bit
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read an
                    // address (a message of length 0)
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 0) {
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                        }
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 0) {
                        if (SSPSTATbits.R_W == 0) { // slave write
                            ic_ptr->status = I2C_RCV_DATA;
                        } else { // slave read
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        }
                    } else {
                        ic_ptr->error_count++;
                        ic_ptr->status = I2C_IDLE;
                        ic_ptr->error_code = I2C_ERR_NODATA;
                    }
                }
                break;
            }
            case I2C_SLAVE_SEND:
            {
                if (ic_ptr->outbufind < ic_ptr->outbuflen) {
                    SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                    ic_ptr->outbufind++;
                    data_written = 1;
                } else {
                    // we have nothing left to send
                    ic_ptr->status = I2C_IDLE;
                }
                break;
            }
            case I2C_RCV_DATA:
            {
                // we expect either data or a stop bit or a (if a restart, an addr)
                if (SSPSTATbits.P == 1) {
                    // we need to check to see if we also read data
                    ic_ptr->event_count++;
                    if (data_read) {
                        if (SSPSTATbits.D_A == 1) {
                            ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                            ic_ptr->buflen++;
                            msg_ready = 1;
                        } else {
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    } else {
                        msg_ready = 1;
                    }
                    ic_ptr->status = I2C_IDLE;
                } else if (data_read) {
                    ic_ptr->event_count++;
                    if (SSPSTATbits.D_A == 1) {
                        ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
                        ic_ptr->buflen++;
                    } else /* a restart */ {
                        if (SSPSTATbits.R_W == 1) {
                            ic_ptr->status = I2C_SLAVE_SEND;
                            msg_ready = 1;
                            msg_to_send = 1;
                            // don't let the clock stretching bit be let go
                            data_read = 0;
                        } else { /* bad to recv an address again, we aren't ready */
                            ic_ptr->error_count++;
                            ic_ptr->error_code = I2C_ERR_NODATA;
                            ic_ptr->status = I2C_IDLE;
                        }
                    }
                }
                break;
            }
        }
    }

    // release the clock stretching bit (if we should)
    if (data_read || data_written) {
        // release the clock
        if (SSPCON1bits.CKP == 0) {
            SSPCON1bits.CKP = 1;
        }
    }

    // must check if the message is too long, if
    if ((ic_ptr->buflen > MAXI2CBUF - 2) && (!msg_ready)) {
        ic_ptr->status = I2C_IDLE;
        ic_ptr->error_count++;
        ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
    }

    if (msg_ready) {
        ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
        ToMainHigh_sendmsg(ic_ptr->buflen + 1, MSGT_I2C_DATA, (void *) ic_ptr->buffer);
        ic_ptr->buflen = 0;
    } else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
        error_buf[0] = ic_ptr->error_count;
        error_buf[1] = ic_ptr->error_code;
        error_buf[2] = ic_ptr->event_count;
        ToMainHigh_sendmsg(sizeof (unsigned char) *3, MSGT_I2C_DBG, (void *) error_buf);
        ic_ptr->error_count = 0;
    }
    if (msg_to_send) {
        // send to the queue to *ask* for the data to be sent out

        start_i2c_slave_reply(I2C_Buffer_Size, 0);
        msg_to_send = 0;
    }
}
// this is the interrupt handler for i2c -- it is currently built for slave mode
// -- to add master mode, you should determine (at the top of the interrupt handler)
//    which mode you are in and call the appropriate subroutine.  The existing code
//    below should be moved into its own "i2c_slave_handler()" routine and the new
//    master code should be in a subroutine called "i2c_master_handler()"
void i2c_int_handler()
{
	unsigned char	i2c_data;
	unsigned char	data_read = 0;
	unsigned char	data_written = 0;
	unsigned char	msg_ready = 0;
	unsigned char	msg_to_send = 0;
	unsigned char	overrun_error = 0;
	unsigned char	error_buf[3];

	// clear SSPOV
	if (SSPCON1bits.SSPOV == 1) {
		SSPCON1bits.SSPOV = 0;
		// we failed to read the buffer in time, so we know we
		// can't properly receive this message, just put us in the
		// a state where we are looking for a new message
		ic_ptr->status = I2C_IDLE;
		overrun_error = 1;
		ic_ptr->error_count++;
		ic_ptr->error_code = I2C_ERR_OVERRUN;
	}
	// read something if it is there
	if (SSPSTATbits.BF == 1) {
		i2c_data = SSPBUF;
		data_read = 1;
	}



	if (!overrun_error) {
		switch(ic_ptr->status) {
			case	I2C_IDLE: {
				// ignore anything except a start
				if (SSPSTATbits.S == 1) {
					handle_start(data_read);	
					// if we see a slave read, then we need to handle it here
					if (ic_ptr->status == I2C_SLAVE_SEND) {
						data_read = 0;
						msg_to_send = 1;
					}
				}	
				break;
			}
			case	I2C_STARTED: {
				// in this case, we expect either an address or a stop bit
				if (SSPSTATbits.P == 1) {
					// we need to check to see if we also read an
					// address (a message of length 0)
					ic_ptr->event_count++;
					if (data_read) {
						if (SSPSTATbits.D_A == 0) {
							msg_ready = 1;
						} else {
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
						}
					}
					ic_ptr->status = I2C_IDLE;
				} else if (data_read) {
					ic_ptr->event_count++;
					if (SSPSTATbits.D_A == 0) {
						if (SSPSTATbits.R_W == 0) { // slave write
							ic_ptr->status = I2C_RCV_DATA;
						} else { // slave read
							ic_ptr->status = I2C_SLAVE_SEND;
							msg_to_send = 1;
							// don't let the clock stretching bit be let go
							data_read = 0;
						}
					} else {
						ic_ptr->error_count++;
						ic_ptr->status = I2C_IDLE;
						ic_ptr->error_code = I2C_ERR_NODATA;
					}
				}
				break;
			}
			case I2C_SLAVE_SEND: {
				if (ic_ptr->outbufind < ic_ptr->outbuflen) {
					SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
					ic_ptr->outbufind++;
					data_written = 1;
				} else {
					// we have nothing left to send
					ic_ptr->status = I2C_IDLE;
				}
				break;
			}
			case I2C_RCV_DATA: {
				// we expect either data or a stop bit or a (if a restart, an addr)
				 if (SSPSTATbits.P == 1) {
					// we need to check to see if we also read data
					ic_ptr->event_count++;
					if (data_read) {
						if (SSPSTATbits.D_A == 1) {
							ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
							ic_ptr->buflen++;
							msg_ready = 1;
						} else {
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
							ic_ptr->status = I2C_IDLE;
						}
					} else {
						msg_ready = 1;
					}
					ic_ptr->status = I2C_IDLE;
				} else if (data_read) {
					ic_ptr->event_count++;
					if (SSPSTATbits.D_A == 1) {
						ic_ptr->buffer[ic_ptr->buflen] = i2c_data;
						ic_ptr->buflen++;
					} else /* a restart */ {
						if (SSPSTATbits.R_W == 1) {
							ic_ptr->status = I2C_SLAVE_SEND;
							msg_ready = 1;
							msg_to_send = 1;
							// don't let the clock stretching bit be let go
							data_read = 0;
						} else { /* bad to recv an address again, we aren't ready */
							ic_ptr->error_count++;
							ic_ptr->error_code = I2C_ERR_NODATA;
							ic_ptr->status = I2C_IDLE;
						}	
					}
				}
				break;
			}
		}
	}

	// release the clock stretching bit (if we should)
	if (data_read || data_written) {
		// release the clock
		if (SSPCON1bits.CKP == 0) {
			SSPCON1bits.CKP = 1;
		}
	}

	// must check if the message is too long, if 
	if ((ic_ptr->buflen > MAXI2CBUF-2) && (!msg_ready)) {
		ic_ptr->status = I2C_IDLE;
		ic_ptr->error_count++;
		ic_ptr->error_code = I2C_ERR_MSGTOOLONG;
	}

	if (msg_ready) {
		ic_ptr->buffer[ic_ptr->buflen] = ic_ptr->event_count;
		ToMainHigh_sendmsg(ic_ptr->buflen+1,MSGT_I2C_DATA,(void *) ic_ptr->buffer);
		ic_ptr->buflen = 0;
	} else if (ic_ptr->error_count >= I2C_ERR_THRESHOLD) {
		error_buf[0] = ic_ptr->error_count;
		error_buf[1] = ic_ptr->error_code;
		error_buf[2] = ic_ptr->event_count;
		ToMainHigh_sendmsg(sizeof(unsigned char)*3,MSGT_I2C_DBG,(void *) error_buf);
		ic_ptr->error_count = 0;
	}
	// This is inside of the I2C interrupt handler fxn.  
	if (msg_to_send) {
		unsigned char MSG[2];
		unsigned char type;
		int t = 0;
		
		if(ic_ptr->buffer[0] = 0xaa)	{						// Check to make sure master sent 0xaa
			int temp;
			int error;
			// Use block_on_To_I2CQueue to see if master is requesting data from an empty queue...if so,
			// send 0xFF.  If not, send the ADC data received from the I2C message queue to the master.
			if(block_on_To_I2Cqueue())	{						// Defined in messages.c
				error = toI2C_recvmsg(2, &type, (void *) MSG);	// receive ADC value from I2CQ
				//LATB = MSG[0];
				start_i2c_slave_reply(2,MSG);		// send ADC value to Master via I2C
			}
			else	{
				MSG[0] = 0xFF;
				MSG[1] = 0xFF;
				start_i2c_slave_reply(2,MSG);
			}
		}
		msg_to_send = 0;
		//ToMainHigh_sendmsg(0,MSGT_I2C_RQST,(void *)ic_ptr->buffer);
	}
}
Example #8
0
// This program 
//   (1) prints to the UART and it reads from the UART
//   (2) it "prints" what it reads from the UART to portb (where LEDs are connected)
//   (3) it uses two timers to interrupt at different rates and drive 2 LEDs (on portb)
void main (void)
{
	char c;
	int adcVal;
	signed char	length;
	unsigned char	msgtype;
	unsigned char last_reg_recvd, action;
	uart_comm uc;
	i2c_comm ic;
	unsigned char msgbuffer[MSGLEN+1];
	unsigned char i;
	uart_thread_struct	uthread_data; // info for uart_lthread
	timer1_thread_struct t1thread_data; // info for timer1_lthread
	timer0_thread_struct t0thread_data; // info for timer0_lthread
	initADC();
	// set to run really, really fast...
	OSCCON = 0x6C; // 4 MHz
	OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line

	// initialize my uart recv handling code
	init_uart_recv(&uc);

	// initialize the i2c code
	init_i2c(&ic);

	// init the timer1 lthread
	init_timer1_lthread(&t1thread_data);

	// initialize message queues before enabling any interrupts
	init_queues();

	// set direction for PORTB to output
	TRISB = 0x0;
	LATB = 0x0;

	// set up PORTA for input
/*
	PORTA = 0x0;	// clear the port
	LATA = 0x0;		// clear the output latch
	ADCON1 = 0x0F;	// turn off the A2D function on these pins
	// Only for 40-pin version of this chip CMCON = 0x07;	// turn the comparator off
	TRISA = 0x0F;	// set RA3-RA0 to inputs
*/

	// initialize Timers
	OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128);
	OpenTimer1( TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
	
	// Peripheral interrupts can have their priority set to high or low
	// enable high-priority interrupts and low-priority interrupts
	enable_interrupts();

	// Decide on the priority of the enabled peripheral interrupts
	// 0 is low, 1 is high
	// Timer1 interrupt
	IPR1bits.TMR1IP = 0;
	// USART RX interrupt
	IPR1bits.RCIP = 0;
	// I2C interrupt
	IPR1bits.SSPIP = 1;

	// configure the hardware i2c device as a slave
	i2c_configure_slave(0x8A);

	// must specifically enable the I2C interrupts
	PIE1bits.SSPIE = 1;

	// configure the hardware USART device
  	OpenUSART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT   & 
		USART_CONT_RX & USART_BRGH_LOW, 0x19);

/* Junk to force an I2C interrupt in the simulator
PIR1bits.SSPIF = 1;
_asm
goto 0x08
_endasm;
*/

	printf("Hello\r\n");
	// loop forever
	// This loop is responsible for "handing off" messages to the subroutines
	// that should get them.  Although the subroutines are not threads, but
	// they can be equated with the tasks in your task diagram if you 
	// structure them properly.
  	while (1) {
		// Call a routine that blocks until either on the incoming
		// messages queues has a message (this may put the processor into
		// an idle mode
		block_on_To_msgqueues();

		// At this point, one or both of the queues has a message.  It 
		// makes sense to check the high-priority messages first -- in fact,
		// you may only want to check the low-priority messages when there
		// is not a high priority message.  That is a design decision and
		// I haven't done it here.
		length = ToMainHigh_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
				printf("Error: Bad high priority receive, code = %x\r\n",
					length);
			}
		} else {
			switch (msgtype) {
				case MSGT_TIMER0: {
					timer0_lthread(&t0thread_data,msgtype,length,msgbuffer);
					break;
				};
				case MSGT_I2C_DATA:
				case MSGT_I2C_DBG: {
					printf("I2C Interrupt received %x: ",msgtype);
					for (i=0;i<length;i++) {
						printf(" %x",msgbuffer[i]);
					}
					//LATBbits.LATB0 = !LATBbits.LATB0;
					LATB = msgbuffer[2];
					//LATB = msgtype;
					//LATB=0x01;
					//printf("\r\n");
					// keep track of the first byte received for later use
					//last_reg_recvd = msgbuffer[0];
					//action=msgbuffer[7];
					//msgbuffer[0]=0xff;
					//msgbuffer[1]=0xff;
					//msgbuffer[2]=0xff;
					//msgbuffer[3]=0xff;
					//start_i2c_slave_reply(4,msgbuffer);
					break;
				};
				case MSGT_I2C_RQST: {
					printf("I2C Slave Req\r\n");
					
					length=2;
					msgbuffer[0]=(adcVal>>8)&0xff;
					msgbuffer[1]=adcVal&0xff;
					
					///	msgbuffer[0]=0x55;
					//			msgbuffer[1]=0x55;
								//printf("XXX: type: %x ADC: %x MsgB1: %x MsgB2: %x\r\n",msgtype,value,msgbuffer[0],msgbuffer[1]);
								//break;
					//		}
					//}
					start_i2c_slave_reply(length,msgbuffer);
					break;
				};
				default: {
					printf("Error: Unexpected msg in queue, type = %x\r\n",
						msgtype);
					break;
				};
			};
			readADC(&adcVal);
		}

		// Check the low priority queue
		length = ToMainLow_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
				printf("Error: Bad low priority receive, code = %x\r\n",
					length);
			}
		} else {
			switch (msgtype) {
				case MSGT_TIMER1: {
					timer1_lthread(&t1thread_data,msgtype,length,msgbuffer);
					break;
				};
				case MSGT_OVERRUN:
				case MSGT_UART_DATA: {
					uart_lthread(&uthread_data,msgtype,length,msgbuffer);
					break;
				};
				default: {
					printf("Error: Unexpected msg in queue, type = %x\r\n",
						msgtype);
					break;
				};
			};
		}
 	 }

}
Example #9
0
void main(void) {
    char c;
    signed char length;
    unsigned char msgtype;
    int test_var = 0;
    unsigned char SENSOR_TYPE_REQUESTED;
    uart_comm uc;
    i2c_comm ic;
    unsigned char msgbuffer[MSGLEN + 1];
    unsigned char i;
    uart_thread_struct uthread_data; // info for uart_lthread
    timer1_thread_struct t1thread_data; // info for timer1_lthread
    timer0_thread_struct t0thread_data; // info for timer0_lthread
    char count = 0;

#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

    // initialize my uart recv handling code
    init_uart_recv(&uc);

    // initialize the i2c code
    init_i2c(&ic);

    // init the timer1 lthread
   init_timer1_lthread(&t1thread_data);

    // initialize message queues before enabling any interrupts
    init_queues();

    // set direction for PORTB to output
    TRISB = 0x0;
    LATB = 0x0;

    // how to set up PORTA for input (for the V4 board with the PIC2680)
    /*
            PORTA = 0x0;	// clear the port
            LATA = 0x0;		// clear the output latch
            ADCON1 = 0x0F;	// turn off the A2D function on these pins
            // Only for 40-pin version of this chip CMCON = 0x07;	// turn the comparator off
            TRISA = 0x0F;	// set RA3-RA0 to inputs
     */

    // initialize Timers
    //OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128);
    OpenTimer1(TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
 //    WriteTimer1(65086);

    i2c_configure_slave(0x2A);


   
     

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

    // Decide on the priority of the enabled peripheral interrupts
    // 0 is low, 1 is high
    // Timer1 interrupt
    IPR1bits.TMR1IP = 0;
    // USART RX interrupt
 //   IPR1bits.RCIP = 1;
    // USART TX interrupt
 //   IPR1bits.TXIP = 0;
    // I2C interrupt
    IPR1bits.SSPIP = 1;

   //  OpenUSART( USART_TX_INT_OFF & USART_RX_INT_ON &  USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX  & USART_BRGH_HIGH & USART_ADDEN_OFF, 38);
    //38 gives us baud rate of approx 19230

    //enable rx interrupts
  //   PIE1bits.RCIE = 1;
 //   PIE1bits.TXIE = 0; //disable send interrupt until we have something in mesage queue

    //i2c interrupt enable
    PIE1bits.SSPIE = 1;



  OpenADC( ADC_FOSC_16 & ADC_RIGHT_JUST & ADC_4_TAD, ADC_CH0 & ADC_INT_ON
                & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, ADC_0ANA);

    // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D)
#if 1
    // Note that the temperature sensor Address bits (A0, A1, A2) are also the
    // least significant bits of LATB -- take care when changing them
    // They *are* changed in the timer interrupt handlers if those timers are
    //   enabled.  They are just there to make the lights blink and can be
    //   disabled.
  //  i2c_configure_slave(0x9E);
#else
    // If I want to test the temperature sensor from the ARM, I just make
    // sure this PIC does not have the same address and configure the
    // temperature sensor address bits and then just stay in an infinite loop
    i2c_configure_slave(0x9A);
    LATBbits.LATB1 = 1;
    LATBbits.LATB0 = 1;
    LATBbits.LATB2 = 1;
    for (;;);
#endif

    // must specifically enable the I2C interrupts
  //  PIE1bits.SSPIE = 1;

    // configure the hardware USART device
   // OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
     //       USART_CONT_RX & USART_BRGH_LOW, 0x19);

    /* Junk to force an I2C interrupt in the simulator (if you wanted to)
    PIR1bits.SSPIF = 1;
    _asm
    goto 0x08
    _endasm;
     */

    // printf() is available, but is not advisable.  It goes to the UART pin
    // on the PIC and then you must hook something up to that to view it.
    // It is also slow and is blocking, so it will perturb your code's operation
    // Here is how it looks: printf("Hello\r\n");


    // loop forever
    // This loop is responsible for "handing off" messages to the subroutines
    // that should get them.  Although the subroutines are not threads, but
    // they can be equated with the tasks in your task diagram if you
    // structure them properly.
    while (1) {
        // Call a routine that blocks until either on the incoming
        // messages queues has a message (this may put the processor into
        // an idle mode)
        block_on_To_msgqueues();

        // At this point, one or both of the queues has a message.  It
        // makes sense to check the high-priority messages first -- in fact,
        // you may only want to check the low-priority messages when there
        // is not a high priority message.  That is a design decision and
        // I haven't done it here.
        length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // This case be handled by your code.
            }
        } else {
            switch (msgtype) {
                case MSGT_TIMER0:
                {
                    timer0_lthread(&t0thread_data, msgtype, length, msgbuffer);
                    break;
                };
                case MSGT_I2C_DATA:
                case MSGT_I2C_DBG:
                {
                    // Here is where you could handle debugging, if you wanted
                    // keep track of the first byte received for later use (if desired)
                    SENSOR_TYPE_REQUESTED = msgbuffer[0];
                    

                    break;
                };
                case MSGT_I2C_RQST:
                {
                    // Generally, this is *NOT* how I recommend you handle an I2C slave request
                    // I recommend that you handle it completely inside the i2c interrupt handler
                    // by reading the data from a queue (i.e., you would not send a message, as is done
                    // now, from the i2c interrupt handler to main to ask for data).
                    //
                    // The last byte received is the "register" that is trying to be read
                    // The response is dependent on the register.
                   /* switch (SENSOR_TYPE_REQUESTED) {
                        case LASER_DATA_REQUEST: //laser type
                        {


                            break;
                        }
                        case INFRARED_DATA_REQUEST:
                        {
                            length = 4; //place infrared data here
                            msgbuffer[0] = 0x42;
                            msgbuffer[1] = 0x43;
                            msgbuffer[2] = 0x44;
                            msgbuffer[3] = 0x45;

                            break;
                        }
                        default:
                        {
                            length = 1;
                            msgbuffer[0] = 0xFF;
                            break;
                        }
                    };
                   /* msgbuffer[0] = 0x77;
                    msgbuffer[1] = 0x66;
                    msgbuffer[2] = 0x54*/

                    length = 6;
                    msgbuffer[0] = 30;
                    msgbuffer[1] = 33+count;
                    msgbuffer[2] = 40;
                    msgbuffer[3] = 43+count;
                    msgbuffer[4] = 44+count;
                    msgbuffer[5] = 45+count;

                    count++;

                    start_i2c_slave_reply(length, msgbuffer);
                    break;
                };
                default:
                {
                    // Your code should handle this error
                    break;
                };
            };
        }

        // Check the low priority queue
        length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // Your code should handle this situation
            }
        } else {
            switch (msgtype) {
                case MSGT_TIMER1:
                {
                    timer1_lthread(&t1thread_data, msgtype, length, msgbuffer);
                    break;
                };
                case MSGT_OVERRUN:
                case MSGT_UART_DATA:
                {
                    uart_lthread(&uthread_data, msgtype, length, msgbuffer);
                    break;
                };
                default:
                {
                    // Your code should handle this error
                    break;
                };
            };
        }
    }

}
Example #10
0
void main (void)
{

unsigned int counter=0,curval=0;
char c;
signed char	length, adlength;
unsigned char	msgtype;
unsigned char last_reg_recvd, action;
unsigned char adbuffer[2];
uart_comm uc;
i2c_comm ic;
unsigned char msgbuffer[MSGLEN+1];
unsigned char i;
uart_thread_struct	uthread_data; // info for uart_lthread
timer1_thread_struct t1thread_data; // info for timer1_lthread
timer0_thread_struct t0thread_data; // info for timer0_lthread
unsigned char data;
TRISB = 0b00000011;
TRISA = 0x0;
TRISC=0b00010000;
MIWICS=1;

glcdInit();

	OSCCON = 0x6C; // 4 MHz
	OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line

	// initialize my uart recv handling code
//	init_uart_recv(&uc);

	// initialize the i2c code
//s	init_i2c(&ic);

	// init the timer1 lthread
	init_timer1_lthread(&t1thread_data);

	// initialize message queues before enabling any interrupts
	init_queues();

	// set direction for PORTB to output
//	TRISB = 0x0;
//	LATB = 0x0;


	// set up PORTA for input
/*
	PORTA = 0x0;	// clear the port
	LATA = 0x0;		// clear the output latch
	ADCON1 = 0x0F;	// turn off the A2D function on these pins
	// Only for 40-pin version of this chip CMCON = 0x07;	// turn the comparator off
	TRISA = 0x0F;	// set RA3-RA0 to inputs
*/

	// initialize Timers
	OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4);
//	OpenTimer1( TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW  & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0);
	
	// Peripheral interrupts can have their priority set to high or low
	// enable high-priority interrupts and low-priority interrupts
	enable_interrupts();

	// Decide on the priority of the enabled peripheral interrupts
	// 0 is low, 1 is high
	// Timer1 interrupt
	IPR1bits.TMR1IP = 0;
	// USART RX interrupt
	IPR1bits.RCIP = 0;
	// I2C interrupt
	IPR1bits.SSPIP = 1;

	// configure the hardware i2c device as a slave
//	i2c_configure_slave(0x8A);

	// must specifically enable the I2C interrupts
	PIE1bits.SSPIE = 1;

	// configure the hardware USART device
  	/*OpenUSART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT   & 
		USART_CONT_RX & USART_BRGH_LOW, 0x19);*/
while(1)
{

	block_on_To_msgqueues();

		// At this point, one or both of the queues has a message.  It 
		// makes sense to check the high-priority messages first -- in fact,
		// you may only want to check the low-priority messages when there
		// is not a high priority message.  That is a design decision and
		// I haven't done it here.
		length = ToMainHigh_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
				printf("Error: Bad high priority receive, code = %x\r\n",
					length);
			}
		} else {
			switch (msgtype) {
				case MSGT_TIMER0: {
					timer0_lthread(&t0thread_data,msgtype,length,msgbuffer);
					break;
				};
				case MSGT_I2C_DATA:
				case MSGT_I2C_DBG: {
					printf("I2C Interrupt received %x: ",msgtype);
					for (i=0;i<length;i++) {
						printf(" %x",msgbuffer[i]);
					}
					//LATBbits.LATB0 = !LATBbits.LATB0;
					LATB = msgbuffer[2];
					//LATB = msgtype;
					//LATB=0x01;
					//printf("\r\n");
					// keep track of the first byte received for later use
					//last_reg_recvd = msgbuffer[0];
					//action=msgbuffer[7];
					//msgbuffer[0]=0xff;
					//msgbuffer[1]=0xff;
					//msgbuffer[2]=0xff;
					//msgbuffer[3]=0xff;
					//start_i2c_slave_reply(4,msgbuffer);
					break;
				};
				case MSGT_I2C_RQST: {
					printf("I2C Slave Req\r\n");
					length=2;
				/*	if(counter==0)
					{
						msgbuffer[0]=0x55;
						msgbuffer[1]=0x55;
						counter++;
					}else
					{
						msgbuffer[0]=0x22;
						msgbuffer[1]=0x22;
						counter=0;
					}*/
				/*	adlength = 	ADQueue_recvmsg(2,0x55,(void *)&adbuffer);
					if((adlength==MSGQUEUE_EMPTY) || (adlength==MSGBUFFER_TOOSMALL))
					{
						msgbuffer[0]=0xff;
						msgbuffer[1]=0xff;
						//msgbuffer[2]=0xff;
					}
					else
					{					
						msgbuffer[0]=adbuffer[0];
						msgbuffer[1]=adbuffer[1];
					//	msgbuffer[0]=0x22;
					//	msgbuffer[1]=0x22;
					}*/
				//	LATB=msgbuffer[0];
								//printf("XXX: type: %x ADC: %x MsgB1: %x MsgB2: %x\r\n",msgtype,value,msgbuffer[0],msgbuffer[1]);
								//break;
					//		}
					//}
					start_i2c_slave_reply(length,msgbuffer);
					break;
				};
				case MSGT_LCD_AREA1:{
				DEBUG_LED1=1;
				DEBUG_LED2=0;
				DEBUG_LED3=0;
				};
				break;
				case MSGT_LCD_AREA2:{
				DEBUG_LED1=0;
				DEBUG_LED2=1;
				DEBUG_LED3=0;
				};
				break;
				case MSGT_LCD_AREA3:{
				DEBUG_LED1=1;
				DEBUG_LED2=1;
				DEBUG_LED3=0;
				};
				break;
				case MSGT_LCD_AREA4:{
				DEBUG_LED1=0;
				DEBUG_LED2=0;
				DEBUG_LED3=1;
				};
				break;
				case MSGT_LCD_TOUCH:{
				DEBUG_LED1=0;
				DEBUG_LED2=0;
				DEBUG_LED3=0;
				};
				break;
				case MSGT_LCD_NOTOUCH:{	
					DEBUG_LED1=0;
					DEBUG_LED2=0;
					DEBUG_LED3=0;
				};
				break;
				default: {
					printf("Error: Unexpected msg in queue, type = %x\r\n",
						msgtype);
					break;
				};
			};
		}

		// Check the low priority queue
		length = ToMainLow_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
				printf("Error: Bad low priority receive, code = %x\r\n",
					length);
			}
		} else {
			switch (msgtype) {
				case MSGT_TIMER1: {
					timer1_lthread(&t1thread_data,msgtype,length,msgbuffer);
					break;
				};
				case MSGT_OVERRUN:
				case MSGT_UART_DATA: {
					uart_lthread(&uthread_data,msgtype,length,msgbuffer);
					break;
				};
				default: {
					printf("Error: Unexpected msg in queue, type = %x\r\n",
						msgtype);
					break;
				};
			};
		}
/*curval=counter%4;
counter++;
if(curval==0)
{

	writePixelByte(0xf5,CS1);
}
else
{
	writePixelByte(0x0,CS2);
}*/

/*if(curval==0)
{
	data=0b01010101;
	printSPIHeader(OLATA, data);
}
else if(curval==1)
{
	data=0b10101010;
	printSPIHeader(OLATA, data);
}
else if(curval==2)
{
	data=0b01010101;
	printSPIHeader(OLATB, data);
}
else
{
	data=0b10101010;
	printSPIHeader(OLATB, data);
	counter=0;
}

X_PLUS=1;//touchscreen voltage
X_MINUS=1;
Y_PLUS=0;
//Y_MINUS=1;
//LATB=LATB|0xf;
readADC2(&adcVal); //detect a touch

if(adcVal>0x300) //touch threshold
	{

		X_PLUS=1;//touchscreen voltage
		X_MINUS=0;
		readADC2(&adcVal); //read a touch Y location
		if(adcVal <0x0f0)
		{
			LATCbits.LATC0=1;
			LATCbits.LATC1=0;
			LATCbits.LATC2=0;
		}
		else if(adcVal<0x1f0)
		{
			LATCbits.LATC0=0;
			LATCbits.LATC1=1;
			LATCbits.LATC2=0;
		}
		else if(adcVal<0x2f0)
		{
			LATCbits.LATC0=1;
			LATCbits.LATC1=1;
			LATCbits.LATC2=0;
		}
		else 
		{
			LATCbits.LATC0=0;
			LATCbits.LATC1=0;
			LATCbits.LATC2=1;
		}
	}else
	{
		LATCbits.LATC0=0;
		LATCbits.LATC1=0;
		LATCbits.LATC2=0;
	}*/

}

}