-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: main
Are you sure you want to change the base?
telemetry firmware first pass #80
Conversation
There was a problem hiding this 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.
telemetry/firmware/queue.h
Outdated
} \ | ||
} | ||
|
||
#ifdef DOXYGEN |
There was a problem hiding this comment.
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
telemetry/firmware/telemetry.c
Outdated
0xc0 | ||
0x10 | ||
|
||
do i create a custom mask that fits my needs? |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
telemetry/firmware/telemetry.c
Outdated
#include <avr/interrupt.h> | ||
#include <util/delay.h> | ||
#include "can_api.h" | ||
#include "spi.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed, need UART.
telemetry/firmware/telemetry.c
Outdated
#include <string.h> | ||
#include <avr/io.h> | ||
#include <avr/interrupt.h> | ||
#include <util/delay.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed.
telemetry/firmware/telemetry.c
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Watch your language
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lmao
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair
telemetry/firmware/telemetry.c
Outdated
//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); | ||
} |
There was a problem hiding this comment.
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).
telemetry/firmware/telemetry.c
Outdated
|
||
|
||
// we can free the data pointed to by temporary msg, as all bytes have been assigned & copied into the UART queue | ||
free(temporary_msg); |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
handle_UART_recieved(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newline.
telemetry/firmware/telemetry.c
Outdated
uint8_t ID; | ||
short data_length; | ||
uint8_t data[8]; | ||
} CAN_MSG; |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
/*----- Macro Definitions -----*/ | ||
|
||
// general | ||
#define FCLK 160000000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused. Delete
telemetry/firmware/telemetry.c
Outdated
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) { |
There was a problem hiding this comment.
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
Outdated
\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 |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
#define BRAKE_LIGHT_MBOX 0 | ||
#define BMS_CORE_MBOX 1 | ||
#define AIR_CONTROL_CRITICAL_MBOX 2 | ||
#define THROTTLE_CMD_MBOX 0 |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
|
||
ISR(CAN_INT_vect){ | ||
|
||
CANPAGE = (THROTTLE_BRD_MBOX<<MOBNB0); |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
|
||
|
||
|
||
// super loop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment needed?
telemetry/firmware/telemetry.c
Outdated
if (bit_is_set(gFlag, UART_SEND_READY)){ | ||
send_UART(); | ||
} | ||
if (bit_is_set(gFlag, UART_RECIEVED)){ |
There was a problem hiding this comment.
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.
telemetry/firmware/telemetry.c
Outdated
handle_UART_recieved(); | ||
} | ||
if (shutdown_error_flag){ | ||
// TODO: need a clean way to handle this HIGH PRIORITY message & send immediately, without enqueueing? |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
/*----- Functions -----*/ | ||
|
||
|
||
void initUART(void){ |
There was a problem hiding this comment.
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.
CAN mailbox address switching State machine not included