Skip to content

Commit

Permalink
made second ringbuf, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Donahue committed Jan 2, 2024
1 parent caaad96 commit d4e0bba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 47 deletions.
11 changes: 9 additions & 2 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#define NUM_INBOUND_CAN1_IDS 1
#define NUM_INBOUND_CAN2_IDS 1

ringbuffer_t* can1_rx_queue;
ringbuffer_t* can2_rx_queue;

static const uint16_t can1_id_list[NUM_INBOUND_CAN1_IDS] = {
//CANID_X,
0x0000
Expand All @@ -19,9 +22,13 @@ static const uint16_t can2_id_list[NUM_INBOUND_CAN2_IDS] = {
0x0000
};

/* Shepherd hanbdles both can busses the same way */

void can_receive_callback(CAN_HandleTypeDef *hcan);

uint8_t get_can_msg(can_msg_t* msg);
/* for 1st CAN bus */
int8_t get_can1_msg(can_msg_t* msg);

/* for 2nd CAN bus */
int8_t get_can2_msg(can_msg_t* msg);

#endif // CAN_HANDLER_H
3 changes: 1 addition & 2 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "datastructs.h"
#include "stateMachine.h"
#include "ringbuffer.h"

#define CURRENT_SENSOR_PIN_L A1
#define CURRENT_SENSOR_PIN_H A0
Expand All @@ -14,8 +15,6 @@
#define MC_BAUD 1000000U
#define MAX_ADC_RESOLUTION 1023 // 13 bit ADC



/**
* @brief inits the compute interface
*/
Expand Down
83 changes: 51 additions & 32 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
#include "can_handler.h"
#include "ringbuffer.h"
#include "analyzer.h"
#include "ringbuffer.h"

ringbuffer_t can_receive_queue;

void can_receive_callback(CAN_HandleTypeDef *hcan)
void can_receive_callback(CAN_HandleTypeDef* hcan)
{
CAN_RxHeaderTypeDef rx_header;
can_msg_t new_msg;
/* Read in CAN message */
CAN_RxHeaderTypeDef rx_header;
can_msg_t new_msg;
/* Read in CAN message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, new_msg.data) != HAL_OK) {

// TODO add non crtical fault capability - could create one for failed can receieve
return;
}

new_msg.len = rx_header.DLC;
new_msg.id = rx_header.StdId;
// TODO add non crtical fault capability - could create one for failed can receieve
return;
}

new_msg.len = rx_header.DLC;
new_msg.id = rx_header.StdId;
if (hcan == &hcan1) {
ringbuffer_enqueue(can1_rx_queue, new_msg);
} else {
ringbuffer_enqueue(can2_rx_queue, new_msg);
}
}

ringbuffer_enqueue(can_receive_queue, new_msg);
int8_t get_can1_msg(can_msg_t* msg)
{
/* no messages to read */
if (ringbuffer_is_empty(&can1_rx_queue))
return -1;

ringbuffer_dequeue(&can1_rx_queue, msg);

// TODO list :
// 1.
switch (msg->id) {
case NULL:
break;
}
return 0;
}

uint8_t get_can_msg(can_msg_t* msg)
int8_t get_can2_msg(can_msg_t* msg)
{
/* no messages to read */
if (ringbuffer_is_empty(&can_receive_queue)) return -1;

ringbuffer_dequeue(&can_receive_queue, msg);

// TODO list :
// 1. Charger connection flag - have Charger set up with following logic, add correct CAN ID
switch (msg->id) {
case NULL:
if (msg->data[0] == 0x01) {
bmsdata->is_charger_connected = true;
} else {
bmsdata->is_charger_connected = false;
}
break;
}
return 0;
/* no messages to read */
if (ringbuffer_is_empty(&can2_rx_queue))
return -1;

ringbuffer_dequeue(&can2_rx_queue, msg);

// TODO list :
// 1. Charger connection flag - have Charger set up with following logic, add correct CAN ID
switch (msg->id) {
case NULL:
if (msg->data[0] == 0x01) {
bmsdata->is_charger_connected = true;
} else {
bmsdata->is_charger_connected = false;
}
break;
}
return 0;
}
27 changes: 16 additions & 11 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "main.h"
#include <string.h>

#define MAX_CAN1_STORAGE 10
#define MAX_CAN2_STORAGE 10

uint8_t fan_speed;
bool is_charging_enabled;
enum { CHARGE_ENABLED, CHARGE_DISABLED };
Expand All @@ -23,12 +26,14 @@ void compute_init()
can1.id_list = can1_id_list;
can1.id_list_len = sizeof(can1_id_list) / sizeof(can1_id_list[0]);
can1.callback = can_receive_callback;
can1_rx_queue = ringbuffer_create(MAX_CAN1_STORAGE, sizeof(can_msg_t));
can_init(&can1);

can2.hcan = &hcan2;
can2.id_list = can2_id_list;
can2.id_list_len = sizeof(can2_id_list) / sizeof(can2_id_list[0]);
can2.callback = can_receive_callback;
can2_rx_queue = ringbuffer_create(MAX_CAN2_STORAGE, sizeof(can_msg_t));
can_init(&can2);
}

Expand All @@ -54,7 +59,7 @@ int compute_send_charging_message(uint16_t voltage_to_set, acc_data_t* bms_data)
charger_msg_data.charger_control = 0b101;

can_msg_t charger_msg;
charger_msg.id = NULL;
charger_msg.id = 0x00; // TODO replace with correct ID;
charger_msg.len = sizeof(charger_msg_data);
memcpy(charger_msg.data, &charger_msg_data, sizeof(charger_msg_data));

Expand All @@ -73,7 +78,7 @@ int compute_send_charging_message(uint16_t voltage_to_set, acc_data_t* bms_data)
charger_msg_data.reserved2_3 = 0xFFFF;

can_msg_t charger_msg;
charger_msg.id = NULL;
charger_msg.id = 0x00; // TODO replace with correct ID;
charger_msg.len = 8;
memcpy(charger_msg.data, &charger_msg_data, sizeof(charger_msg_data));
can_send_msg(&can2, &charger_msg);
Expand Down Expand Up @@ -163,7 +168,7 @@ void compute_send_mc_message(uint16_t user_max_charge, uint16_t user_max_dischar
mc_msg_data.maxDischarge = user_max_discharge;

can_msg_t mc_msg;
mc_msg.id = NULL;
mc_msg.id = 0x00; // TODO replace with correct ID;
mc_msg.len = sizeof(mc_msg_data);
memcpy(mc_msg.data, &mc_msg_data, sizeof(mc_msg_data));

Expand All @@ -188,7 +193,7 @@ void compute_send_acc_status_message(acc_data_t* bmsdata)
acc_status_msg_data.pack_health = 0;

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(acc_status_msg_data);
memcpy(acc_msg.data, &acc_status_msg_data, sizeof(acc_status_msg_data));

Expand All @@ -212,7 +217,7 @@ void compute_send_bms_status_message(acc_data_t* bmsdata, int bms_state, bool ba
bms_status_msg_data.balance = (uint8_t)(balance);

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(bms_status_msg_data);
memcpy(acc_msg.data, &bms_status_msg_data, sizeof(bms_status_msg_data));

Expand All @@ -228,7 +233,7 @@ void compute_send_shutdown_ctrl_message(uint8_t mpe_state)
shutdown_control_msg_data.mpeState = mpe_state;

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(shutdown_control_msg_data);
memcpy(acc_msg.data, &shutdown_control_msg_data, sizeof(shutdown_control_msg_data));

Expand All @@ -252,7 +257,7 @@ void compute_send_cell_data_message(acc_data_t* bmsdata)
cell_data_msg_data.volt_avg = bmsdata->avg_voltage;

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(cell_data_msg_data);
memcpy(acc_msg.data, &cell_data_msg_data, sizeof(cell_data_msg_data));

Expand All @@ -278,7 +283,7 @@ void compute_send_cell_voltage_message(uint8_t cell_id, uint16_t instant_voltage
cell_voltage_msg_data.openVoltage = __builtin_bswap16(open_voltage);

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(cell_voltage_msg_data);
memcpy(acc_msg.data, &cell_voltage_msg_data, sizeof(cell_voltage_msg_data));

Expand All @@ -298,7 +303,7 @@ void compute_send_current_message(acc_data_t* bmsdata)
current_status_msg_data.pack_curr = bmsdata->pack_current;

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(current_status_msg_data);
memcpy(acc_msg.data, &current_status_msg_data, sizeof(current_status_msg_data));

Expand Down Expand Up @@ -328,7 +333,7 @@ void compute_send_cell_temp_message(acc_data_t* bmsdata)
cell_temp_msg_data.average_temp = bmsdata->avg_temp;

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(cell_temp_msg_data);
memcpy(acc_msg.data, &cell_temp_msg_data, sizeof(cell_temp_msg_data));

Expand All @@ -353,7 +358,7 @@ void compute_send_segment_temp_message(acc_data_t* bmsdata)
memcpy(buff, &segment_temp_msg_data, sizeof(segment_temp_msg_data));

can_msg_t acc_msg;
acc_msg.id = NULL;
acc_msg.id = 0x00; // TODO replace with correct ID;
acc_msg.len = sizeof(segment_temp_msg_data);
memcpy(acc_msg.data, &segment_temp_msg_data, sizeof(segment_temp_msg_data));

Expand Down

0 comments on commit d4e0bba

Please sign in to comment.