diff --git a/Telem_Debug/Debug1/main.c b/Telem_Debug/Debug1/main.c index bd2db2b..6517cf1 100644 --- a/Telem_Debug/Debug1/main.c +++ b/Telem_Debug/Debug1/main.c @@ -39,9 +39,9 @@ int main(void) { P8DIR |= BIT5; // P1.0 output P8DIR |= BIT6; // P1.0 output P8OUT &= ~BIT3; // Toggle P1.0 - P8OUT &= ~BIT4; // Toggle P1.0 +// P8OUT &= ~BIT4; // Toggle P1.0 P8OUT &= ~BIT5; // Toggle P1.0 - P8OUT &= ~BIT6; // Toggle P1.0 +// P8OUT &= ~BIT6; // Toggle P1.0 _EINT(); //enable global interrupts @@ -50,6 +50,8 @@ int main(void) { if(status_flag){ status_flag = FALSE; P8OUT ^= BIT3; // Toggle P1.0 + P8OUT ^= BIT4; // Toggle P1.0 + P8OUT ^= BIT5; // Toggle P1.0 P8OUT ^= BIT6; // Toggle P1.0 } diff --git a/Telem_Debug/Debug3/can.h b/Telem_Debug/Debug3/can.h index 13f77e0..5fb4500 100644 --- a/Telem_Debug/Debug3/can.h +++ b/Telem_Debug/Debug3/can.h @@ -74,14 +74,14 @@ typedef union _group_16 { unsigned int data_u16; } group_16; -typedef struct _can_variables +typedef struct _can_packet_struct { unsigned int status; unsigned int address; group_64 data; -} can_variables; +} can_struct; -extern can_variables can; +// extern can_struct can; // Private function prototypes void can0_reset( void ); diff --git a/Telem_Debug/Debug3/can0.c b/Telem_Debug/Debug3/can0.c index b4528c7..598c889 100644 --- a/Telem_Debug/Debug3/can0.c +++ b/Telem_Debug/Debug3/can0.c @@ -34,7 +34,14 @@ #include "SunseekerTelemetry2021.h" // Public variables -can_variables can; +can_struct can; +can_struct TX_can0_message; +can_struct RX_can0_message; + +extern unsigned long can_msg_count; +extern unsigned long can_err_count; +extern unsigned long can_read_cnt; + // Private variables unsigned char buffer[16]; @@ -152,10 +159,18 @@ void can0_init( void ) */ void can0_receive( void ) { + can_struct *RXPtr_can_message; unsigned char flags; + can_msg_count++; + + // Set up pointer to receive queue + + RXPtr_can_message=&(*can0_queue.PutPt); + // Read out the interrupt flags register can0_read( CANINTF, &flags, 1 ); + // Check for errors if(( flags & MCP_IRQ_ERR ) != 0x00 ){ // Read error flags and counters @@ -164,12 +179,12 @@ void can0_receive( void ) // Clear error flags can0_mod( EFLAG, buffer[0], 0x00 ); // Modify (to '0') all bits that were set // Return error code, a blank address field, and error registers in data field - can.status = CAN_ERROR; - can.address = 0x0000; - can.data.data_u8[0] = flags; // CANINTF - can.data.data_u8[1] = buffer[0]; // EFLG - can.data.data_u8[2] = buffer[1]; // TEC - can.data.data_u8[3] = buffer[2]; // REC + RXPtr_can_message->status = CAN_ERROR; + RXPtr_can_message->address = 0x0000; + RXPtr_can_message->data.data_u8[0] = flags; // CANINTF + RXPtr_can_message->data.data_u8[1] = buffer[0]; // EFLG + RXPtr_can_message->data.data_u8[2] = buffer[1]; // TEC + RXPtr_can_message->data.data_u8[3] = buffer[2]; // REC // Clear the IRQ flag can0_mod( CANINTF, MCP_IRQ_ERR, 0x00 ); } @@ -177,65 +192,70 @@ void can0_receive( void ) else if(( flags & MCP_IRQ_RXB0 ) != 0x00 ){ // Read in the info, address & message data can0_read( RXB0CTRL, &buffer[0], 14 ); + // Clear the IRQ flag as fast as possible + can0_mod( CANINTF, MCP_IRQ_RXB0, 0x00 ); + // Fill out return structure // check for Remote Frame requests and indicate the status correctly if(( buffer[0] & MCP_RXB0_RTR ) == 0x00 ){ // We've received a standard data packet - can.status = CAN_OK; + RXPtr_can_message->status = CAN_OK; // Fill in the data - can.data.data_u8[0] = buffer[ 6]; - can.data.data_u8[1] = buffer[ 7]; - can.data.data_u8[2] = buffer[ 8]; - can.data.data_u8[3] = buffer[ 9]; - can.data.data_u8[4] = buffer[10]; - can.data.data_u8[5] = buffer[11]; - can.data.data_u8[6] = buffer[12]; - can.data.data_u8[7] = buffer[13]; + RXPtr_can_message->data.data_u8[1] = buffer[ 7]; + RXPtr_can_message->data.data_u8[2] = buffer[ 8]; + RXPtr_can_message->data.data_u8[3] = buffer[ 9]; + RXPtr_can_message->data.data_u8[4] = buffer[10]; + RXPtr_can_message->data.data_u8[5] = buffer[11]; + RXPtr_can_message->data.data_u8[6] = buffer[12]; + RXPtr_can_message->data.data_u8[7] = buffer[13]; } else{ // We've received a remote frame request // Data is irrelevant with an RTR - can.status = CAN_RTR; + RXPtr_can_message->status = CAN_RTR; } // Fill in the address - can.address = buffer[1]; - can.address = can.address << 3; - buffer[2] = buffer[2] >> 5; - can.address = can.address | buffer[2]; - // Clear the IRQ flag - can0_mod( CANINTF, MCP_IRQ_RXB0, 0x00 ); + RXPtr_can_message->address = ((int)(buffer[1]) << 3) | ((int)(buffer[2]) >> 5); + can_read_cnt++; + + //add message to queue to be decoded + can_fifo_PUT(&can0_queue, *RXPtr_can_message); + } // No error, check for received messages, buffer 1 else if(( flags & MCP_IRQ_RXB1 ) != 0x00 ){ // Read in the info, address & message data can0_read( RXB1CTRL, &buffer[0], 14 ); + // Clear the IRQ flag as fast as possible + can0_mod( CANINTF, MCP_IRQ_RXB1, 0x00 ); + // Fill out return structure // check for Remote Frame requests and indicate the status correctly if(( buffer[0] & MCP_RXB1_RTR ) == 0x00 ){ // We've received a standard data packet - can.status = CAN_OK; + RXPtr_can_message->status = CAN_OK; // Fill in the data - can.data.data_u8[0] = buffer[ 6]; - can.data.data_u8[1] = buffer[ 7]; - can.data.data_u8[2] = buffer[ 8]; - can.data.data_u8[3] = buffer[ 9]; - can.data.data_u8[4] = buffer[10]; - can.data.data_u8[5] = buffer[11]; - can.data.data_u8[6] = buffer[12]; - can.data.data_u8[7] = buffer[13]; + RXPtr_can_message->data.data_u8[0] = buffer[ 6]; + RXPtr_can_message->data.data_u8[1] = buffer[ 7]; + RXPtr_can_message->data.data_u8[2] = buffer[ 8]; + RXPtr_can_message->data.data_u8[3] = buffer[ 9]; + RXPtr_can_message->data.data_u8[4] = buffer[10]; + RXPtr_can_message->data.data_u8[5] = buffer[11]; + RXPtr_can_message->data.data_u8[6] = buffer[12]; + RXPtr_can_message->data.data_u8[7] = buffer[13]; } else{ // We've received a remote frame request // Data is irrelevant with an RTR - can.status = CAN_RTR; + RXPtr_can_message->status = CAN_RTR; } // Fill in the address - can.address = buffer[1]; - can.address = can.address << 3; - buffer[2] = buffer[2] >> 5; - can.address = can.address | buffer[2]; - // Clear the IRQ flag - can0_mod( CANINTF, MCP_IRQ_RXB1, 0x00 ); + RXPtr_can_message->address = ((int)(buffer[1]) << 3) | ((int)(buffer[2]) >> 5); + can_read_cnt++; + + //add message to queue to be decoded + can_fifo_PUT(&can0_queue, *RXPtr_can_message); + } // If multiple receive then clear that error else if(( flags & MCP_IRQ_MERR) != 0x00 ){ @@ -245,21 +265,23 @@ void can0_receive( void ) // Clear error flags can0_mod( EFLAG, buffer[0], 0x00 ); // Modify (to '0') all bits that were set // Return error code, a blank address field, and error registers in data field - can.status = CAN_MERROR; - can.address = 0x0000; - can.data.data_u8[0] = flags; // CANINTF - can.data.data_u8[1] = buffer[0]; // EFLG - can.data.data_u8[2] = buffer[1]; // TEC - can.data.data_u8[3] = buffer[2]; // REC + RXPtr_can_message->status = CAN_MERROR; + RXPtr_can_message->address = 0x0000; + RXPtr_can_message->data.data_u8[0] = flags; // CANINTF + RXPtr_can_message->data.data_u8[1] = buffer[0]; // EFLG + RXPtr_can_message->data.data_u8[2] = buffer[1]; // TEC + RXPtr_can_message->data.data_u8[3] = buffer[2]; // REC // Clear the IRQ flag can0_mod( CANINTF, MCP_IRQ_MERR, 0x00 ); } + //not handled by buffers or errors else{ - can.status = CAN_FERROR; - can.address = 0x0001; - can.data.data_u8[0] = flags; // CANINTF + RXPtr_can_message->status = CAN_FERROR; + RXPtr_can_message->address = 0x0001; + RXPtr_can_message->data.data_u8[0] = flags; // CANINTF // Clear all IRQ flag can0_mod( CANINTF, 0xFF, 0x00 ); + can_err_count++; } //added to tritum code to account for the fact that we could have more then one intrrupt pending at a given time @@ -293,29 +315,29 @@ int can0_transmit( void ) // Fill data into buffer, it's used by any address // Allow room at the start of the buffer for the address info if needed - buffer[ 5] = can.data.data_u8[0]; - buffer[ 6] = can.data.data_u8[1]; - buffer[ 7] = can.data.data_u8[2]; - buffer[ 8] = can.data.data_u8[3]; - buffer[ 9] = can.data.data_u8[4]; - buffer[10] = can.data.data_u8[5]; - buffer[11] = can.data.data_u8[6]; - buffer[12] = can.data.data_u8[7]; + buffer[ 5] = TX_can0_message.data.data_u8[0]; + buffer[ 6] = TX_can0_message.data.data_u8[1]; + buffer[ 7] = TX_can0_message.data.data_u8[2]; + buffer[ 8] = TX_can0_message.data.data_u8[3]; + buffer[ 9] = TX_can0_message.data.data_u8[4]; + buffer[10] = TX_can0_message.data.data_u8[5]; + buffer[11] = TX_can0_message.data.data_u8[6]; + buffer[12] = TX_can0_message.data.data_u8[7]; // Check if the incoming address has already been configured in a mailbox - if( can.address == buf_addr[0] ){ + if( TX_can0_message.address == buf_addr[0] ){ // Mailbox 0 setup matches our new message // Write to TX Buffer 0, start at data registers, and initiate transmission can0_write_tx( 0x01, &buffer[5] ); can0_rts( 0 ); } - else if( can.address == buf_addr[1] ){ + else if( TX_can0_message.address == buf_addr[1] ){ // Mailbox 1 setup matches our new message // Write to TX Buffer 1, start at data registers, and initiate transmission can0_write_tx( 0x03, &buffer[5] ); can0_rts( 1 ); } - else if( can.address == buf_addr[2] ){ + else if( TX_can0_message.address == buf_addr[2] ){ // Mailbox 2 setup matches our new message // Write to TX Buffer 2, start at data registers, and initiate transmission can0_write_tx( 0x05, &buffer[5] ); @@ -324,8 +346,8 @@ int can0_transmit( void ) else{ // No matches in existing mailboxes // No mailboxes already configured, so we'll need to load an address - set it up - buffer[0] = (unsigned char)(can.address >> 3); - buffer[1] = (unsigned char)(can.address << 5); + buffer[0] = (unsigned char)(TX_can0_message.address >> 3); + buffer[1] = (unsigned char)(TX_can0_message.address << 5); buffer[2] = 0x00; // EID8 buffer[3] = 0x00; // EID0 buffer[4] = 0x08; // DLC = 8 bytes @@ -336,19 +358,19 @@ int can0_transmit( void ) // Write to TX Buffer 0, start at address registers, and initiate transmission can0_write_tx( 0x00, &buffer[0] ); can0_rts( 0 ); - buf_addr[0] = can.address; + buf_addr[0] = TX_can0_message.address; } else if( buf_addr[1] == 0xFFFF ){ // Mailbox 1 is free // Write to TX Buffer 1, start at address registers, and initiate transmission can0_write_tx( 0x02, &buffer[0] ); can0_rts( 1 ); - buf_addr[1] = can.address; + buf_addr[1] = TX_can0_message.address; } else if( buf_addr[2] == 0xFFFF ){ // Mailbox 2 is free // Write to TX Buffer 2, start at address registers, and initiate transmission can0_write_tx( 0x04, &buffer[0] ); can0_rts( 2 ); - buf_addr[2] = can.address; + buf_addr[2] = TX_can0_message.address; } else { @@ -369,21 +391,21 @@ int can0_transmit( void ) // Setup mailbox 0 and send the message can0_write_tx( 0x00, &buffer[0] ); can0_rts( 0 ); - buf_addr[0] = can.address; + buf_addr[0] = TX_can0_message.address; } // Is it mailbox 1? else if(( can0_read_status() & 0x10 ) == 0x00) { // Setup mailbox 1 and send the message can0_write_tx( 0x02, &buffer[0] ); can0_rts( 1 ); - buf_addr[1] = can.address; + buf_addr[1] = TX_can0_message.address; } // Is it mailbox 2? else if(( can0_read_status() & 0x40 ) == 0x00) { // Setup mailbox 2 and send the message can0_write_tx( 0x04, &buffer[0] ); can0_rts( 2 ); - buf_addr[2] = can.address; + buf_addr[2] = TX_can0_message.address; } // } } diff --git a/Telem_Debug/Debug3/can1.c b/Telem_Debug/Debug3/can1.c index 04aba50..a009e26 100644 --- a/Telem_Debug/Debug3/can1.c +++ b/Telem_Debug/Debug3/can1.c @@ -34,7 +34,7 @@ #include "SunseekerTelemetry2021.h" // Public variables -can_variables can; +can_struct can; // Private variables unsigned char buffer[16]; diff --git a/Telem_Debug/Debug3/can_FIFO.c b/Telem_Debug/Debug3/can_FIFO.c new file mode 100644 index 0000000..ee1c5f3 --- /dev/null +++ b/Telem_Debug/Debug3/can_FIFO.c @@ -0,0 +1,57 @@ +// +// Telemetry Messgae FIFO +// +#include "Sunseeker2021.h" + +void can_fifo_INIT(void) +{ + can_struct *temptestPt; + + temptestPt = &can0_queue.msg_fifo[0]; + can0_queue.PutPt = temptestPt; + can0_queue.GetPt = temptestPt; +} + +int can_fifo_PUT(can_message_fifo *queue, can_struct toPut) +{ + can_struct *tempPt; + + tempPt = queue->PutPt; + *(tempPt++) = toPut; + if(tempPt == &queue->msg_fifo[msg_fifo_size]) + { + tempPt = &queue->msg_fifo[0]; //need to wrap around + } + if(tempPt == queue->GetPt) + { + return(0); //Failure FIFO Full + } + else + { + queue->PutPt = tempPt; + return(1); //successful + } +} + +int can_fifo_GET(can_message_fifo *queue, can_struct *toGet) +{ + if(queue->PutPt == queue->GetPt) + { + return(0); //failure FIFO empty + } + else + { + *toGet = *(queue->GetPt++); + if(queue->GetPt == &queue->msg_fifo[msg_fifo_size]) + { + queue->GetPt = &queue->msg_fifo[0]; //need to wrap around + } + return(1); + } +} + +int can_fifo_STAT(can_message_fifo *queue) +{ + return (queue->GetPt != queue->PutPt); +} + diff --git a/Telem_Debug/Debug3/can_FIFO.h b/Telem_Debug/Debug3/can_FIFO.h new file mode 100644 index 0000000..a5f1ad1 --- /dev/null +++ b/Telem_Debug/Debug3/can_FIFO.h @@ -0,0 +1,27 @@ +#ifndef message_FIFO_H +#define message_FIFO_H + +#ifndef msg_fifo_size +#define msg_fifo_size 16 +#endif + +//structure to hold incoming can messages before decoding +typedef struct _message_fifo +{ + can_struct msg_fifo[msg_fifo_size]; + can_struct *PutPt; + can_struct *GetPt; +} can_message_fifo; + + +//public structure +extern can_message_fifo can0_queue; + +//public functions +extern void can_fifo_INIT(void); +extern int can_fifo_PUT(can_message_fifo *queue, can_struct toPut); +extern int can_fifo_GET(can_message_fifo *queue, can_struct *toGet); +extern int can_fifo_STAT(can_message_fifo *queue); + +#endif + diff --git a/Telem_Debug/Debug3/main.c b/Telem_Debug/Debug3/main.c index eeedb4a..2f9f712 100644 --- a/Telem_Debug/Debug3/main.c +++ b/Telem_Debug/Debug3/main.c @@ -23,7 +23,7 @@ #include "SunseekerTelemetry2021.h" // structures -//message_fifo decode_queue; +can_message_fifo can0_queue; //char_fifo USB_FIFO, MODEM_FIFO; //hf_packet pckHF; @@ -38,9 +38,12 @@ enum MODE {INIT, CANREAD, DECODE, MODEMTX, USBTX, LOWP, LOOP} ucMODE; unsigned volatile char can_status_test, can_rcv_status_test; unsigned long can_msg_count = 0, can_stall_cnt = 0; unsigned long can_err_count = 0, can_read_cnt = 0; + +char CAN0_INT_FLAG = FALSE; +char CAN1_INT_FLAG = FALSE; + int thrs, tmin, tsec; char ucFLAG,usbENABLE; -char CAN1_INT_FLAG = FALSE; volatile unsigned char status_flag = FALSE; volatile unsigned char hs_comms_flag = FALSE; @@ -48,6 +51,9 @@ volatile unsigned char ls_comms_flag = FALSE; volatile unsigned char st_comms_flag = FALSE; static char init_msg_data[21] = "0xHHHHHHHH,0xHHHHHHHH"; +static char init_time_msg[17] = "TL_TIM,HH:MM:SS\r\n"; + +char time_test_msg[16] = "TL_TIM,HH:MM:SS\0"; // General Variables @@ -70,15 +76,36 @@ int main(void) { io_init(); delay(); + init_RTC(); + delay(); + + getTime(&thrs,&tmin,&tsec); + delay(); + UART_init(); delay(); + insert_time(&time_test_msg[0]); + UART_puts(time_test_msg); + delay(); + + can0spi_init(); delay(); + can0_init(); + delay(); + can1spi_init(); delay(); + can1_init(); + delay(); + + can_fifo_INIT(); + + + @@ -94,6 +121,14 @@ int main(void) { P8OUT ^= BIT6; // Toggle P1.0 } + CAN0_INT_FLAG = ((P2IN & CAN1_INTn)==0); + if(CAN0_INT_FLAG){ +// ucFLAG |= 0x40; + can_stall_cnt++; + can0_receive(); + } + + // __delay_cycles(100000); // Delay }