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

Can #65

Merged
merged 5 commits into from
Jan 5, 2024
Merged

Can #65

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef CAN_HANDLER_H
#define CAN_HANDLER_H

#include "can.h"
#include "stm32f4xx_hal.h"
#include <stdint.h>


#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
};

static const uint16_t can2_id_list[NUM_INBOUND_CAN2_IDS] = {
//CANID_X,
0x0000
};


void can_receive_callback(CAN_HandleTypeDef *hcan);
nwdepatie marked this conversation as resolved.
Show resolved Hide resolved

/* 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
1 change: 1 addition & 0 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 Down
62 changes: 62 additions & 0 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "can_handler.h"
#include "analyzer.h"
#include "ringbuffer.h"

void can_receive_callback(CAN_HandleTypeDef* hcan)
{
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Again this should work but only if you are sending both CAN bus queues to the same FIFO buffer

Copy link
Contributor

Choose a reason for hiding this comment

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

Also do we not have a wrapper function for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Jk I guess we don't have a wrapper for this


// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This prob should be tested but looks good if it works as intended

ringbuffer_enqueue(can1_rx_queue, new_msg);
} else {
ringbuffer_enqueue(can2_rx_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;
}

int8_t get_can2_msg(can_msg_t* msg)
{
/* 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;
}
Loading
Loading