Merged changes

This commit is contained in:
Sunseeker Lab A-216
2021-05-21 13:25:08 -04:00
parent b8a5f8b8a5
commit 0956ea6ae0
7 changed files with 218 additions and 75 deletions

View File

@@ -39,9 +39,9 @@ int main(void) {
P8DIR |= BIT5; // P1.0 output P8DIR |= BIT5; // P1.0 output
P8DIR |= BIT6; // P1.0 output P8DIR |= BIT6; // P1.0 output
P8OUT &= ~BIT3; // Toggle P1.0 P8OUT &= ~BIT3; // Toggle P1.0
P8OUT &= ~BIT4; // Toggle P1.0 // P8OUT &= ~BIT4; // Toggle P1.0
P8OUT &= ~BIT5; // Toggle P1.0 P8OUT &= ~BIT5; // Toggle P1.0
P8OUT &= ~BIT6; // Toggle P1.0 // P8OUT &= ~BIT6; // Toggle P1.0
_EINT(); //enable global interrupts _EINT(); //enable global interrupts
@@ -50,6 +50,8 @@ int main(void) {
if(status_flag){ if(status_flag){
status_flag = FALSE; status_flag = FALSE;
P8OUT ^= BIT3; // Toggle P1.0 P8OUT ^= BIT3; // Toggle P1.0
P8OUT ^= BIT4; // Toggle P1.0
P8OUT ^= BIT5; // Toggle P1.0
P8OUT ^= BIT6; // Toggle P1.0 P8OUT ^= BIT6; // Toggle P1.0
} }

View File

@@ -74,14 +74,14 @@ typedef union _group_16 {
unsigned int data_u16; unsigned int data_u16;
} group_16; } group_16;
typedef struct _can_variables typedef struct _can_packet_struct
{ {
unsigned int status; unsigned int status;
unsigned int address; unsigned int address;
group_64 data; group_64 data;
} can_variables; } can_struct;
extern can_variables can; // extern can_struct can;
// Private function prototypes // Private function prototypes
void can0_reset( void ); void can0_reset( void );

View File

@@ -34,7 +34,14 @@
#include "SunseekerTelemetry2021.h" #include "SunseekerTelemetry2021.h"
// Public variables // 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 // Private variables
unsigned char buffer[16]; unsigned char buffer[16];
@@ -152,10 +159,18 @@ void can0_init( void )
*/ */
void can0_receive( void ) void can0_receive( void )
{ {
can_struct *RXPtr_can_message;
unsigned char flags; unsigned char flags;
can_msg_count++;
// Set up pointer to receive queue
RXPtr_can_message=&(*can0_queue.PutPt);
// Read out the interrupt flags register // Read out the interrupt flags register
can0_read( CANINTF, &flags, 1 ); can0_read( CANINTF, &flags, 1 );
// Check for errors // Check for errors
if(( flags & MCP_IRQ_ERR ) != 0x00 ){ if(( flags & MCP_IRQ_ERR ) != 0x00 ){
// Read error flags and counters // Read error flags and counters
@@ -164,12 +179,12 @@ void can0_receive( void )
// Clear error flags // Clear error flags
can0_mod( EFLAG, buffer[0], 0x00 ); // Modify (to '0') all bits that were set 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 // Return error code, a blank address field, and error registers in data field
can.status = CAN_ERROR; RXPtr_can_message->status = CAN_ERROR;
can.address = 0x0000; RXPtr_can_message->address = 0x0000;
can.data.data_u8[0] = flags; // CANINTF RXPtr_can_message->data.data_u8[0] = flags; // CANINTF
can.data.data_u8[1] = buffer[0]; // EFLG RXPtr_can_message->data.data_u8[1] = buffer[0]; // EFLG
can.data.data_u8[2] = buffer[1]; // TEC RXPtr_can_message->data.data_u8[2] = buffer[1]; // TEC
can.data.data_u8[3] = buffer[2]; // REC RXPtr_can_message->data.data_u8[3] = buffer[2]; // REC
// Clear the IRQ flag // Clear the IRQ flag
can0_mod( CANINTF, MCP_IRQ_ERR, 0x00 ); can0_mod( CANINTF, MCP_IRQ_ERR, 0x00 );
} }
@@ -177,65 +192,70 @@ void can0_receive( void )
else if(( flags & MCP_IRQ_RXB0 ) != 0x00 ){ else if(( flags & MCP_IRQ_RXB0 ) != 0x00 ){
// Read in the info, address & message data // Read in the info, address & message data
can0_read( RXB0CTRL, &buffer[0], 14 ); 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 // Fill out return structure
// check for Remote Frame requests and indicate the status correctly // check for Remote Frame requests and indicate the status correctly
if(( buffer[0] & MCP_RXB0_RTR ) == 0x00 ){ if(( buffer[0] & MCP_RXB0_RTR ) == 0x00 ){
// We've received a standard data packet // We've received a standard data packet
can.status = CAN_OK; RXPtr_can_message->status = CAN_OK;
// Fill in the data // Fill in the data
can.data.data_u8[0] = buffer[ 6]; RXPtr_can_message->data.data_u8[1] = buffer[ 7];
can.data.data_u8[1] = buffer[ 7]; RXPtr_can_message->data.data_u8[2] = buffer[ 8];
can.data.data_u8[2] = buffer[ 8]; RXPtr_can_message->data.data_u8[3] = buffer[ 9];
can.data.data_u8[3] = buffer[ 9]; RXPtr_can_message->data.data_u8[4] = buffer[10];
can.data.data_u8[4] = buffer[10]; RXPtr_can_message->data.data_u8[5] = buffer[11];
can.data.data_u8[5] = buffer[11]; RXPtr_can_message->data.data_u8[6] = buffer[12];
can.data.data_u8[6] = buffer[12]; RXPtr_can_message->data.data_u8[7] = buffer[13];
can.data.data_u8[7] = buffer[13];
} }
else{ else{
// We've received a remote frame request // We've received a remote frame request
// Data is irrelevant with an RTR // Data is irrelevant with an RTR
can.status = CAN_RTR; RXPtr_can_message->status = CAN_RTR;
} }
// Fill in the address // Fill in the address
can.address = buffer[1]; RXPtr_can_message->address = ((int)(buffer[1]) << 3) | ((int)(buffer[2]) >> 5);
can.address = can.address << 3; can_read_cnt++;
buffer[2] = buffer[2] >> 5;
can.address = can.address | buffer[2]; //add message to queue to be decoded
// Clear the IRQ flag can_fifo_PUT(&can0_queue, *RXPtr_can_message);
can0_mod( CANINTF, MCP_IRQ_RXB0, 0x00 );
} }
// No error, check for received messages, buffer 1 // No error, check for received messages, buffer 1
else if(( flags & MCP_IRQ_RXB1 ) != 0x00 ){ else if(( flags & MCP_IRQ_RXB1 ) != 0x00 ){
// Read in the info, address & message data // Read in the info, address & message data
can0_read( RXB1CTRL, &buffer[0], 14 ); 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 // Fill out return structure
// check for Remote Frame requests and indicate the status correctly // check for Remote Frame requests and indicate the status correctly
if(( buffer[0] & MCP_RXB1_RTR ) == 0x00 ){ if(( buffer[0] & MCP_RXB1_RTR ) == 0x00 ){
// We've received a standard data packet // We've received a standard data packet
can.status = CAN_OK; RXPtr_can_message->status = CAN_OK;
// Fill in the data // Fill in the data
can.data.data_u8[0] = buffer[ 6]; RXPtr_can_message->data.data_u8[0] = buffer[ 6];
can.data.data_u8[1] = buffer[ 7]; RXPtr_can_message->data.data_u8[1] = buffer[ 7];
can.data.data_u8[2] = buffer[ 8]; RXPtr_can_message->data.data_u8[2] = buffer[ 8];
can.data.data_u8[3] = buffer[ 9]; RXPtr_can_message->data.data_u8[3] = buffer[ 9];
can.data.data_u8[4] = buffer[10]; RXPtr_can_message->data.data_u8[4] = buffer[10];
can.data.data_u8[5] = buffer[11]; RXPtr_can_message->data.data_u8[5] = buffer[11];
can.data.data_u8[6] = buffer[12]; RXPtr_can_message->data.data_u8[6] = buffer[12];
can.data.data_u8[7] = buffer[13]; RXPtr_can_message->data.data_u8[7] = buffer[13];
} }
else{ else{
// We've received a remote frame request // We've received a remote frame request
// Data is irrelevant with an RTR // Data is irrelevant with an RTR
can.status = CAN_RTR; RXPtr_can_message->status = CAN_RTR;
} }
// Fill in the address // Fill in the address
can.address = buffer[1]; RXPtr_can_message->address = ((int)(buffer[1]) << 3) | ((int)(buffer[2]) >> 5);
can.address = can.address << 3; can_read_cnt++;
buffer[2] = buffer[2] >> 5;
can.address = can.address | buffer[2]; //add message to queue to be decoded
// Clear the IRQ flag can_fifo_PUT(&can0_queue, *RXPtr_can_message);
can0_mod( CANINTF, MCP_IRQ_RXB1, 0x00 );
} }
// If multiple receive then clear that error // If multiple receive then clear that error
else if(( flags & MCP_IRQ_MERR) != 0x00 ){ else if(( flags & MCP_IRQ_MERR) != 0x00 ){
@@ -245,21 +265,23 @@ void can0_receive( void )
// Clear error flags // Clear error flags
can0_mod( EFLAG, buffer[0], 0x00 ); // Modify (to '0') all bits that were set 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 // Return error code, a blank address field, and error registers in data field
can.status = CAN_MERROR; RXPtr_can_message->status = CAN_MERROR;
can.address = 0x0000; RXPtr_can_message->address = 0x0000;
can.data.data_u8[0] = flags; // CANINTF RXPtr_can_message->data.data_u8[0] = flags; // CANINTF
can.data.data_u8[1] = buffer[0]; // EFLG RXPtr_can_message->data.data_u8[1] = buffer[0]; // EFLG
can.data.data_u8[2] = buffer[1]; // TEC RXPtr_can_message->data.data_u8[2] = buffer[1]; // TEC
can.data.data_u8[3] = buffer[2]; // REC RXPtr_can_message->data.data_u8[3] = buffer[2]; // REC
// Clear the IRQ flag // Clear the IRQ flag
can0_mod( CANINTF, MCP_IRQ_MERR, 0x00 ); can0_mod( CANINTF, MCP_IRQ_MERR, 0x00 );
} }
//not handled by buffers or errors
else{ else{
can.status = CAN_FERROR; RXPtr_can_message->status = CAN_FERROR;
can.address = 0x0001; RXPtr_can_message->address = 0x0001;
can.data.data_u8[0] = flags; // CANINTF RXPtr_can_message->data.data_u8[0] = flags; // CANINTF
// Clear all IRQ flag // Clear all IRQ flag
can0_mod( CANINTF, 0xFF, 0x00 ); 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 //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 // Fill data into buffer, it's used by any address
// Allow room at the start of the buffer for the address info if needed // Allow room at the start of the buffer for the address info if needed
buffer[ 5] = can.data.data_u8[0]; buffer[ 5] = TX_can0_message.data.data_u8[0];
buffer[ 6] = can.data.data_u8[1]; buffer[ 6] = TX_can0_message.data.data_u8[1];
buffer[ 7] = can.data.data_u8[2]; buffer[ 7] = TX_can0_message.data.data_u8[2];
buffer[ 8] = can.data.data_u8[3]; buffer[ 8] = TX_can0_message.data.data_u8[3];
buffer[ 9] = can.data.data_u8[4]; buffer[ 9] = TX_can0_message.data.data_u8[4];
buffer[10] = can.data.data_u8[5]; buffer[10] = TX_can0_message.data.data_u8[5];
buffer[11] = can.data.data_u8[6]; buffer[11] = TX_can0_message.data.data_u8[6];
buffer[12] = can.data.data_u8[7]; buffer[12] = TX_can0_message.data.data_u8[7];
// Check if the incoming address has already been configured in a mailbox // 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 // Mailbox 0 setup matches our new message
// Write to TX Buffer 0, start at data registers, and initiate transmission // Write to TX Buffer 0, start at data registers, and initiate transmission
can0_write_tx( 0x01, &buffer[5] ); can0_write_tx( 0x01, &buffer[5] );
can0_rts( 0 ); 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 // Mailbox 1 setup matches our new message
// Write to TX Buffer 1, start at data registers, and initiate transmission // Write to TX Buffer 1, start at data registers, and initiate transmission
can0_write_tx( 0x03, &buffer[5] ); can0_write_tx( 0x03, &buffer[5] );
can0_rts( 1 ); 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 // Mailbox 2 setup matches our new message
// Write to TX Buffer 2, start at data registers, and initiate transmission // Write to TX Buffer 2, start at data registers, and initiate transmission
can0_write_tx( 0x05, &buffer[5] ); can0_write_tx( 0x05, &buffer[5] );
@@ -324,8 +346,8 @@ int can0_transmit( void )
else{ else{
// No matches in existing mailboxes // No matches in existing mailboxes
// No mailboxes already configured, so we'll need to load an address - set it up // No mailboxes already configured, so we'll need to load an address - set it up
buffer[0] = (unsigned char)(can.address >> 3); buffer[0] = (unsigned char)(TX_can0_message.address >> 3);
buffer[1] = (unsigned char)(can.address << 5); buffer[1] = (unsigned char)(TX_can0_message.address << 5);
buffer[2] = 0x00; // EID8 buffer[2] = 0x00; // EID8
buffer[3] = 0x00; // EID0 buffer[3] = 0x00; // EID0
buffer[4] = 0x08; // DLC = 8 bytes 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 // Write to TX Buffer 0, start at address registers, and initiate transmission
can0_write_tx( 0x00, &buffer[0] ); can0_write_tx( 0x00, &buffer[0] );
can0_rts( 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 else if( buf_addr[1] == 0xFFFF ){ // Mailbox 1 is free
// Write to TX Buffer 1, start at address registers, and initiate transmission // Write to TX Buffer 1, start at address registers, and initiate transmission
can0_write_tx( 0x02, &buffer[0] ); can0_write_tx( 0x02, &buffer[0] );
can0_rts( 1 ); 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 else if( buf_addr[2] == 0xFFFF ){ // Mailbox 2 is free
// Write to TX Buffer 2, start at address registers, and initiate transmission // Write to TX Buffer 2, start at address registers, and initiate transmission
can0_write_tx( 0x04, &buffer[0] ); can0_write_tx( 0x04, &buffer[0] );
can0_rts( 2 ); can0_rts( 2 );
buf_addr[2] = can.address; buf_addr[2] = TX_can0_message.address;
} }
else { else {
@@ -369,21 +391,21 @@ int can0_transmit( void )
// Setup mailbox 0 and send the message // Setup mailbox 0 and send the message
can0_write_tx( 0x00, &buffer[0] ); can0_write_tx( 0x00, &buffer[0] );
can0_rts( 0 ); can0_rts( 0 );
buf_addr[0] = can.address; buf_addr[0] = TX_can0_message.address;
} }
// Is it mailbox 1? // Is it mailbox 1?
else if(( can0_read_status() & 0x10 ) == 0x00) { else if(( can0_read_status() & 0x10 ) == 0x00) {
// Setup mailbox 1 and send the message // Setup mailbox 1 and send the message
can0_write_tx( 0x02, &buffer[0] ); can0_write_tx( 0x02, &buffer[0] );
can0_rts( 1 ); can0_rts( 1 );
buf_addr[1] = can.address; buf_addr[1] = TX_can0_message.address;
} }
// Is it mailbox 2? // Is it mailbox 2?
else if(( can0_read_status() & 0x40 ) == 0x00) { else if(( can0_read_status() & 0x40 ) == 0x00) {
// Setup mailbox 2 and send the message // Setup mailbox 2 and send the message
can0_write_tx( 0x04, &buffer[0] ); can0_write_tx( 0x04, &buffer[0] );
can0_rts( 2 ); can0_rts( 2 );
buf_addr[2] = can.address; buf_addr[2] = TX_can0_message.address;
} }
// } // }
} }

View File

@@ -34,7 +34,7 @@
#include "SunseekerTelemetry2021.h" #include "SunseekerTelemetry2021.h"
// Public variables // Public variables
can_variables can; can_struct can;
// Private variables // Private variables
unsigned char buffer[16]; unsigned char buffer[16];

View File

@@ -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);
}

View File

@@ -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

View File

@@ -23,7 +23,7 @@
#include "SunseekerTelemetry2021.h" #include "SunseekerTelemetry2021.h"
// structures // structures
//message_fifo decode_queue; can_message_fifo can0_queue;
//char_fifo USB_FIFO, MODEM_FIFO; //char_fifo USB_FIFO, MODEM_FIFO;
//hf_packet pckHF; //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 volatile char can_status_test, can_rcv_status_test;
unsigned long can_msg_count = 0, can_stall_cnt = 0; unsigned long can_msg_count = 0, can_stall_cnt = 0;
unsigned long can_err_count = 0, can_read_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; int thrs, tmin, tsec;
char ucFLAG,usbENABLE; char ucFLAG,usbENABLE;
char CAN1_INT_FLAG = FALSE;
volatile unsigned char status_flag = FALSE; volatile unsigned char status_flag = FALSE;
volatile unsigned char hs_comms_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; volatile unsigned char st_comms_flag = FALSE;
static char init_msg_data[21] = "0xHHHHHHHH,0xHHHHHHHH"; 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 // General Variables
@@ -70,15 +76,36 @@ int main(void) {
io_init(); io_init();
delay(); delay();
init_RTC();
delay();
getTime(&thrs,&tmin,&tsec);
delay();
UART_init(); UART_init();
delay(); delay();
insert_time(&time_test_msg[0]);
UART_puts(time_test_msg);
delay();
can0spi_init(); can0spi_init();
delay(); delay();
can0_init();
delay();
can1spi_init(); can1spi_init();
delay(); delay();
can1_init();
delay();
can_fifo_INIT();
@@ -94,6 +121,14 @@ int main(void) {
P8OUT ^= BIT6; // Toggle P1.0 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 // __delay_cycles(100000); // Delay
} }