Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telemetry firmware first pass #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aramachandran7
Copy link
Contributor

CAN mailbox address switching State machine not included

Copy link
Contributor

@jack-greenberg jack-greenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start Adi. Some comments for you to address. Mostly to do with memory, and some clean up. I'll take another look with an eye closer on function after you fix this stuff.

} \
}

#ifdef DOXYGEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need any of this stuff. This is all for Doxygen, which we don't use

0xc0
0x10

do i create a custom mask that fits my needs?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range of IDs goes from 0xB to 0x63, so we can just have a mask that listens to any message in that range. So 0x7F should work as a mask.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually on second thought, we should just implement a MOb for 0xB, 0xC, 0xE, and 0x10 with the mask 0x1F, and then have a MOb for 0xc0 that only listens for that, and a MOb for 0x40-0x64.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the mask part wrong.

In a mask, if a bit is 0, that bit is ignored, and if the bit is 1, the bit must patch. So if you want to accept messages from 0x40 to 0x64, your mask will be 0x7C0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it will also accept messages up to 0x7F, so you'll need to do some additional checking

#include <avr/interrupt.h>
#include <util/delay.h>
#include "can_api.h"
#include "spi.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, need UART.

#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

volatile uint16_t shutdown_error_flag = 0x00; // system failure error code flag - default 0x00.

// CAN DATA to store & send out over UART
uint8_t accumulator_voltages[192] = {0}; // 192 wholeass bytes wtf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch your language

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmao

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair

//Setup to Receive Again for future
CANSTMOB = 0x00;
CAN_wait_on_receive(BRAKE_LIGHT_MBOX, CAN_ID_BRAKE_LIGHT, CAN_LEN_BRAKE_LIGHT, CAN_MSK_SINGLE);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I said not to use malloc above, but just for the sake of completeness, you must free dynamically allocated memory at the end of usage. Call free(msg) (but actually just don't use malloc).



// we can free the data pointed to by temporary msg, as all bytes have been assigned & copied into the UART queue
free(temporary_msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to free statically allocated memory.

handle_UART_recieved();
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline.

uint8_t ID;
short data_length;
uint8_t data[8];
} CAN_MSG;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also want to have uint8_t mask as a part of this struct.

/*----- Macro Definitions -----*/

// general
#define FCLK 160000000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused. Delete

CAN_MSG * msg = malloc(sizeof(CAN_MSG));
(*msg) = { custom_ID, 8, can_recv_msg}; // TODO: To confirm, this copies over can_recv_mesg during asisgnment?

if (queue_CAN_push(&CAN_queue, msg) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then here, if msg has type CAN_MSG instead of CAN_MSG*, you would just use &msg to get the address of the msg struct.

…nts, simplified overall architecture to single queue
telemetry/firmware/queue.h Show resolved Hide resolved
\param ctx a pointer to arbitrary context that gets passed as the second argument to \c fun for each item in the queue
*/
#endif
// #ifdef DOXYGEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't comment it out, just delete it all.

#define BRAKE_LIGHT_MBOX 0
#define BMS_CORE_MBOX 1
#define AIR_CONTROL_CRITICAL_MBOX 2
#define THROTTLE_CMD_MBOX 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity's sake, let's call these MOBs instead of MBOXes.


ISR(CAN_INT_vect){

CANPAGE = (THROTTLE_BRD_MBOX<<MOBNB0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datasheet 20.11.6/7, consider using the CANIE and CANSIT registers to tell which MOb triggered the interrupt.




// super loop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment needed?

if (bit_is_set(gFlag, UART_SEND_READY)){
send_UART();
}
if (bit_is_set(gFlag, UART_RECIEVED)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missed it, you don't have an interrupt on UART_RX_vect for receiving UART messages.

handle_UART_recieved();
}
if (shutdown_error_flag){
// TODO: need a clean way to handle this HIGH PRIORITY message & send immediately, without enqueueing?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we do. If we think about the use case, it would be best to see what the CAN messages right before shutdown are, and if we bypass the queue, we lose ordering. I think we should still use the queue. Shutdown shouldn't stop this board from working, so we'll continue transmitting telemetry data after shutdown.

#define CELL_VOLT_TEMP_MOB 5

#define CAN_MSK_6 0b11000000 // custom mask for cell temps & voltages
// handle another 15 mailboxes lol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha. fine for a first pass, but given that we may want to determine which messages are sent through telemetry dynamically in the future, we probably shouldn't be hard-coding these MOBs; we should be able to re-configure them without re-flashing code.

// UART

// values within LINCR register
#define ENABLE_LIN_AND_UART LENA // can i leave this as LENA or does it have to be bit 3 (page 190)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach as opposed to hard-coding register values later in code like we usually do! You name the macros "enable XXX", but presumably only 1 bit in the register is actually doing the enabling; should we define those bits as macros as well?



QUEUE(UART, uint8_t, UART_QUEUE_SIZE);
voltatile struct queue_UART UART_queue; // creating a UART_queue of type queue_UART
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need the comment here. I can tell you're creating a UART_queue of type queue_UART. If you want to include a comment, tell me what we're gonna put in this UART queue and where the data is going.

/*----- Interrupt(s) -----*/


ISR(CAN_INT_vect){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is huge, can we break it up? Here's roughly what I have in mind:

if bit_is_set(CANSIT2, MSG_XXX) {
  read_msg_xxx();
}

void read_msg_xxx() {
  ...
}

}

ISR(USART_TX_vect){
gFlag |= _BV(UART_SEND_READY); // set flag and get back to it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/*----- Functions -----*/


void initUART(void){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you're jumping around between using underscores and not using them. I don't have a strong preference either way (I think we usually use camelCase for C, no underscores). just please don't use both in the same file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants