示例#1
0
void uart_recv_int_handler() {
#ifdef __USE18F26J50
    if (DataRdy1USART()) {
        uc_ptr->buffer[uc_ptr->buflen] = Read1USART();
#else
#ifdef __USE18F46J50
    if (DataRdy1USART()) {
        uc_ptr->buffer[uc_ptr->buflen] = Read1USART();
#else
    if (DataRdyUSART()) {
        uc_ptr->buffer[uc_ptr->buflen] = ReadUSART();
#endif
#endif

        uc_ptr->buflen++;
        // check if a message should be sent
        if (uc_ptr->buflen == MAXUARTBUF) {
            ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer);
            uc_ptr->buflen = 0;
        }

        ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer);
    }
#ifdef __USE18F26J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
#ifdef __USE18F46J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
    if (USART_Status.OVERRUN_ERROR == 1) {
#endif
#endif
        // we've overrun the USART and must reset
        // send an error message for this
        RCSTAbits.CREN = 0;
        RCSTAbits.CREN = 1;
        ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0);
    }
}

void uart_trans_int_handler() {
    unsigned char msgbuffer[MSGLEN+1];
    unsigned char msgtype;
    signed char length;
    unsigned char start;
    length = SensorData_recvmsg(start, MSGLEN, &msgtype, (void *) msgbuffer);
    
    //unsigned char msg[6];
    //msg[0] = 0x0;
//    i = i << 1;
    if (msgtest[0] == 0xFF) {
        msgtest[0] = 0x0;
    }

    msgtest[0]++;
    msgtest[1] = 0xA;
    msgtest[2] = 0x00;
    msgtest[3] = msgtest[0];
    msgtest[4] = 0xFF;
    msgtest[5] = 0x0;

//    if (length == 0) {
//        PIE1bits.TX1IE = 0x0;
//    }
    TXREG1 = msgtest[index];

    //i++;
    index++;
    //FromMainHigh_recvmsg()
    if (index == 6) {
        index = 0;
        msgtest[0] = msgtest[0] - 0x5;
        PIE1bits.TX1IE = 0x0;
    }
}

void init_uart_recv(uart_comm *uc) {
    uc_ptr = uc;
    uc_ptr->buflen = 0;
}

void uart_send(int length, unsigned char *msg_buffer) {
    int i = 0;
    for (i; i < length; i++) {
        //TXREG1 = msg_buffer[i];
        uc_ptr->buffer[i] = msg_buffer[i];
    }
    PIE1bits.TX1IE = 0x0;
}
示例#2
0
void i2c_master_int_handler(){
    switch (ic_ptr->status){
        // Page 183 - I2C Master Mode Waveform (Transfer)
        case (I2C_MASTER_SEND_STARTED): {
            ic_ptr->status = I2C_MASTER_SEND_START;
            SSPBUF = ((ic_ptr->slave_addr << 1) & 0xFE);
            break;
        }
        case (I2C_MASTER_SEND_START): {
            if (SSPCON2bits.ACKSTAT == 0) {
                if (ic_ptr->outbufind < ic_ptr->outbuflen) {
                    SSPBUF = ic_ptr->outbuffer[ic_ptr->outbufind];
                    ic_ptr->outbufind++;
                    ic_ptr->status = I2C_MASTER_SEND_START;
                }
                else {
                    ToMainHigh_sendmsg(0, MSGT_I2C_MASTER_SEND_COMPLETE, 0);
                    ic_ptr->outbuflen = 0;
                    ic_ptr->status = I2C_IDLE;
                    SSPCON2bits.PEN = 1;
                }
            }
            else {
                ToMainHigh_sendmsg(0, MSGT_I2C_MASTER_SEND_FAILED, 0);
                ic_ptr->status = I2C_IDLE;
                SSPCON2bits.PEN = 1;
            }
            break;
        }
        case (I2C_MASTER_SEND_DONE): {
            ToMainHigh_sendmsg(0, MSGT_I2C_MASTER_SEND_COMPLETE, 0);
            ic_ptr->status = I2C_IDLE;
            break;
        }

        // Master receive

        case I2C_MASTER_RECV_STARTED: {
            //flipDBG(1);
                ic_ptr->status = I2C_MASTER_RECV_SET_RCEN;
                SSPBUF = ((ic_ptr->slave_addr << 1) | 0x01) & 0xFF;
                break;
        }
        case I2C_MASTER_RECV_SET_RCEN: {
            if (SSPCON2bits.ACKSTAT == 0) {
                //flipDBG(1);
                ic_ptr->status = I2C_MASTER_RECV_DATA;
                SSPCON2bits.RCEN = 1;
            }
            else {
                ToMainHigh_sendmsg(0, MSGT_I2C_MASTER_RECV_FAILED, 0);
                ic_ptr->status = I2C_IDLE;
                SSPCON2bits.PEN = 1;
            }
            break;
        }
        case I2C_MASTER_RECV_DATA: {
            //flipDBG(1);
            //    if (SSPSTATbits.BF == 1) {
                    ic_ptr->buffer[ic_ptr->bufind] = SSPBUF;
                    ic_ptr->bufind++;
                    ic_ptr->status = I2C_MASTER_RECV_ACK;
           //     }
                if (ic_ptr->bufind < ic_ptr->buflen) {
                    SSPCON2bits.ACKDT = 0;
                    SSPCON2bits.ACKEN = 1;
                }
                else {
                    SSPCON2bits.ACKDT = 1;
                    SSPCON2bits.ACKEN = 1;
                }
            break;
        }
        case I2C_MASTER_RECV_ACK: {
            if (ic_ptr->bufind < ic_ptr->buflen) {
                ic_ptr->status = I2C_MASTER_RECV_DATA;
                SSPCON2bits.RCEN = 1;
            }
            else {
                if(ic_ptr->slave_addr == 0x4F)
                {ToMainLow_sendmsg(ic_ptr->buflen, MSGT_MOTOR_BACK, (void *)(ic_ptr->buffer));}
                else
                {ToMainLow_sendmsg(ic_ptr->buflen, MSGT_SEND_BACK, (void *)(ic_ptr->buffer));}
                
                ic_ptr->status = I2C_IDLE;
                SSPCON2bits.PEN = 1;
                SSPCON2bits.ACKDT = 0;
                SSPCON2bits.ACKEN = 1;
            }
            break;
        }
    }
}
示例#3
0
void uart_recv_state(unsigned char byte) {
    
    switch (uc_ptr->state) {

        case UART_STATE_HEADER1:
            if ( byte == UART_HEADER1) {
//                WriteUSART(0x01);
                uc_ptr->state = UART_STATE_HEADER2;
            }
            break;

        case UART_STATE_HEADER2:
            if ( byte == UART_HEADER2 ) {
                //WriteUSART(0x02);
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_COUNT;
#elif defined (MAIN_PIC)
                uc_ptr->state = UART_STATE_MSGTYPE;
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            break;

        case UART_STATE_MSGTYPE:
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
//            WriteUSART(0x03);
            uc_ptr->msgtype = byte;
            uc_ptr->buffer[0] = byte;
            uc_ptr->state = UART_STATE_COUNT;
            break;

        case UART_STATE_COUNT:
//            WriteUSART(0x04);
            uc_ptr->count = byte;
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_LENGTH;
                uc_ptr->buffer[0] = byte;
#elif defined (MAIN_PIC)
                uc_ptr->buffer[1] = byte;
                uc_ptr->state = UART_STATE_FOOTER;
#endif    
             break;

        case UART_STATE_LENGTH:
//            WriteUSART(0x05);
            uc_ptr->buffer[1] = byte;
            uc_ptr->data_length = byte;
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            uc_ptr->state = UART_STATE_DATA;
            break;
            
        case UART_STATE_DATA:
//            WriteUSART(0x06);
            
            // Store the byte into buffer
            if ( uc_ptr->data_read + 2 < MAXUARTBUF ) {             
                uc_ptr->buffer[uc_ptr->data_read+2] = byte;
            }
            
            // Increment because we read 1 more byte
            uc_ptr->data_read += 1;
            
            // Keep reading data until, 9 BYTES for now
            if(UARTDATALEN == uc_ptr->data_read) {
                uc_ptr->state = UART_STATE_FOOTER;
            }
            
            break;
        case UART_STATE_FOOTER:
            // WriteUSART(0x07);
            if ( byte == UART_FOOTER ) {
            // WriteUSART(0x08);
                uc_ptr->state = UART_STATE_HEADER1;
                //LATBbits.LATB7 = !LATBbits.LATB7;

#if defined (ARM_PIC)
                // Copy over into a buffer before passing
                unsigned char temp[I2CMSGLEN];
                int i;
                for (i = 0; i < I2CMSGLEN; i++) {
                    temp[i] = uc_ptr->buffer[i];
                }
                LATB = 7; // Sequence 7
                LATAbits.LA0 = 0;
                // Put it in the roverDataBuf
                ToMainLow_sendmsg(I2CMSGLEN, MSGT_BUF_PUT_DATA, (void *) temp);
#elif defined (MAIN_PIC)
                // Wait for first to finish
                
                LATBbits.LATB0 = 1;
                LATBbits.LATB1 = 0;
                LATBbits.LATB2 = 0; // Sequence 1
                LATAbits.LA0 = 0;

                i2c_master_cmd message;

                message.msgType = uc_ptr->buffer[0];
                message.msgCount = uc_ptr->buffer[1];


                //if ( i2c_master_busy() == 0 )
                    // Put the message in the queue
                while(i2c_master_busy());
                putQueue(&i2c_q,message);
                //ToMainLow_sendmsg(2, MSGT_BUF_PUT_DATA, (void *) uc_ptr->buffer);
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            break;
        default:
            break;
    }
    
}
// A function called by the interrupt handler
// This one does the action I wanted for this program on a timer0 interrupt
void ADC_int_handler() {

    // reset the timer

    ToMainLow_sendmsg(0, ADCSC1_ADCH, (void *) 0);
}
示例#5
0
void uart_recv_int_handler() {

    // Write a byte (one character) to the usart transmit buffer.
    // If 9-bit mode is enabled, the 9th bit is written from the field TX_NINE,
    // found in a union of type USART

#ifdef __USE18F26J50
    if (DataRdy1USART()) {
        uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = Read1USART();
#else
#ifdef __USE18F46J50
    if (DataRdy1USART()) {
        char data = Read1USART();
#else
    if (DataRdyUSART()) {
        char data = ReadUSART();
#endif
#endif

        switch (msgtype_uart) {
            case MSGTYPE:
                uc_ptr->Rx_buffer[0] = data;
                uc_ptr->Rx_buflen++;
                msgtype_uart = LENGTH;
                break;

            case LENGTH:
                if (uc_ptr->Rx_buflen == 1) {
                    uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data;
                    uc_ptr->Rx_buflen++;
                    msgtype_uart = MESSAGE;
                } else {
                    uc_ptr->Rx_buflen = 0;
                    msgtype_uart = MSGTYPE;
                }
                break;

            case MESSAGE:
                if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) {
                    uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data;
                    uc_ptr->Rx_buflen++;
                    msgtype_uart = CHECKSUM;
                } else {
                    uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data;
                    uc_ptr->Rx_buflen++;
                    msgtype_uart = MESSAGE;
                }
                break;

            case CHECKSUM:
                if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) {
                    uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data;
                    uc_ptr->Rx_buflen++;
                    unsigned char checkSum = 0;
                    unsigned char bufLength = uc_ptr->Rx_buffer[1];

                    // Check for correct checksum.
                    int i = 0;
                    for (; i < uc_ptr->Rx_buffer[1]; i++) {
                        checkSum ^= uc_ptr->Rx_buffer[i + 2];
                    }

                    if (uc_ptr->Rx_buflen != uc_ptr->Rx_buffer[1] + 3) {
                        // Message has been scrambled.
                        uc_ptr->Rx_buffer[0] == COMMAND_NACK;
                    }

                    if (checkSum != uc_ptr->Rx_buffer[uc_ptr->Rx_buflen - 1] || uc_ptr->Rx_buffer[0] == COMMAND_NACK) {
                        // Send previous message again.
                        ToMainHigh_sendmsg(uc_ptr->Tx_buflen, MSGT_UART_SEND, (void *) uc_ptr->Tx_buffer);
                    } else if (uc_ptr->Rx_buffer[0] != COMMAND_ACK) {
                        // Send sensor or motor encoder data to the ARM.
                        ToMainHigh_sendmsg(uc_ptr->Rx_buflen, MSGT_UART_RCV, (void *) uc_ptr->Rx_buffer);
                        // Return an ACK to the Master.
                        ToMainLow_sendmsg(CMD_ACKLEN, MSGT_UART_SEND, (void *) uc_ptr->cmd_ack_buf);
                    } else if (uc_ptr->Rx_buffer[0] == COMMAND_ACK) {
                        // Allow ARM to send a new motor command.
                        motorCommandSent = 1;
                    }
                }
                msgtype_uart = MSGTYPE;
                uc_ptr->Rx_buflen = 0;
                break;

            default:
                break;
        }
    }

#ifdef __USE18F26J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
#ifdef __USE18F46J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
    if (USART_Status.OVERRUN_ERROR == 1) {
#endif
#endif
        // we've overrun the USART and must reset
        // send an error message for this
        RCSTAbits.CREN = 0;
        RCSTAbits.CREN = 1;
        ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0);
    }
}

void uart_send_int_handler() {

    if (uc_ptr->Tx_buflen == uc_ptr->msg_length) {
        PIE1bits.TX1IE = 0; // Clear TXIE to end write.
        uc_ptr->Tx_buflen = 0;
    } else {
        Write1USART(uc_ptr->Tx_buffer[uc_ptr->Tx_buflen++]);
    }
}

void init_uart_recv(uart_comm * uc) {
    uc_ptr = uc;
    uc_ptr->Tx_buflen = 0;
    uc_ptr->Rx_buflen = 0;
    uc_ptr->msg_length = 0;

    // Intialize CMD ACK response.
    uc_ptr->cmd_ack_buf[0] = 0x10;
    uc_ptr->cmd_ack_buf[1] = 0x01;
    uc_ptr->cmd_ack_buf[2] = 0x02;
    uc_ptr->cmd_ack_buf[3] = 0x02;
}

void uart_retrieve_buffer(int length, unsigned char* msgbuffer) {

    int i = 0;
    uc_ptr->Tx_buflen = 0;
    uc_ptr->msg_length = length;

    for (; i < length; i++) {
        uc_ptr->Tx_buffer[i] = msgbuffer[i];
    }
    // Set UART TXF interrupt flag
    PIE1bits.TX1IE = 0x1;
}
示例#6
0
void uart_recv_int_handler() {
#ifdef __USE18F26J50
    if (DataRdy1USART()) {
        uc_ptr->buffer[uc_ptr->buflen] = Read1USART();
#else
#ifdef __USE18F46J50
    if (DataRdy1USART()) {
        uc_ptr->buffer[uc_ptr->buflen] = Read1USART();
#else
    if (DataRdyUSART()) {
        buffer_temp[buf_len] = ReadUSART();
#endif
#endif
        uc_ptr->buflen++;
        buf_len++;
        parseUART();

        // check if a message should be sent
    }
#ifdef __USE18F26J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
#ifdef __USE18F46J50
    if (USART1_Status.OVERRUN_ERROR == 1) {
#else
    if (USART_Status.OVERRUN_ERROR == 1) {
#endif
#endif
        // we've overrun the USART and must reset
        // send an error message for this
        RCSTAbits.CREN = 0;
        RCSTAbits.CREN = 1;
        ToMainHigh_sendmsg(0, MSGT_OVERRUN, (void *) 0);

    }
}

void init_uart_recv(uart_comm *uc) {
    uc_ptr = uc;
    uc_ptr->buflen = 0;
    buf_len = 0;
    command_length = 0;
    command_count = 0;
    State = GET_MSGID;
}

void parseUART()
{
    unsigned char x = uc_ptr->buflen;
   // uart_write(1, &x);
    switch(State)
    {
        case(GET_MSGID):
        {
            command_length = buffer_temp[buf_len -1] & 0xF;
            command_length = command_length;
            if(command_length != 0)
            {
                State = GET_COMMAND;
            }
            else
            {
                State = CHECKSUM;
            }
            break;
        }
        case(GET_COMMAND):
        {
            if(command_count+1 < command_length)
            {
                command_count++;
            }
            else
            {
                State = CHECKSUM;
            }
            break;
        }
        case(CHECKSUM):
        {
            ToMainLow_sendmsg(buf_len ,MSGT_PARSE, &buffer_temp[0]);
            uc_ptr->buflen = 0;
            buf_len = 0;
            State = GET_MSGID;
            command_count = 0;
            command_length = 0;
            break;
        }
    }
}


void uart_write(unsigned char length, unsigned char *msg){

    unsigned char i = 0;
    for(i = 0; i < length; i++){

    #ifdef __USE18F26J50
        while(Busy1USART());
        Write1USART(msg[i]);
    #else
    #ifdef __USE18F46J50
        while(Busy1USART());
        Write1USART(msg[i]);
    #else
        while(BusyUSART());
        WriteUSART(msg[i]);
    #endif
    #endif
    }
}