Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gdbinit
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ b main
b bms_entry
# b BistThread::runBist
# b MeasurementsThread::runMeasurements
# b StateMachineThread::runStateMachine
b StateMachineThread::runStateMachine
# b LEDThread::runLEDThread

52 changes: 52 additions & 0 deletions STM32F405RGTx/Core/Inc/circ_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include "slave_thread.hpp"

#define CIRC_RX_BUFF_SIZE 16

/*
Implementation of a circular CircQueue
*/

typedef enum {
CircQueue_GET_EMPTY = 0,
CircQueue_GET_OK = 1,
CircQueue_PUT_OK = 2,
CircQueue_PUT_WRAPAROUND = 3,
CircQueue_ERROR = 0
} CircQueueStatus;

typedef struct {
size_t len;

SlavePkt _arr[CIRC_RX_BUFF_SIZE];
size_t _head;
size_t _tail;
} CircQueue;

#define CIRC_INC_HEAD(self) { \
if (self->_head == (CIRC_RX_BUFF_SIZE - 1)) { \
self->_head = 0; \
} \
else { \
self->_head += 1; \
} \
}
#define CIRC_INC_TAIL(self) { \
if (self->_tail == (CIRC_RX_BUFF_SIZE - 1)) { \
self->_tail = 0; \
} \
else { \
self->_tail += 1; \
} \
}

CircQueue CircQueue_init();
uint8_t CircQueue_empty(CircQueue* self);
CircQueueStatus CircQueue_put(CircQueue* self, SlavePkt* val);
CircQueueStatus CircQueue_get(CircQueue* self, SlavePkt* val);

void CircQueue_print(CircQueue* self);

33 changes: 33 additions & 0 deletions STM32F405RGTx/Core/Inc/slave_thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <stdint.h>
#include "util.hpp"

#define SLAVE_PKT_HEADER {0x46, 0x55, 0x43, 0x4b, 0x5f, 0x43, 0x48, 0x43}
#define SLAVE_PACKET_LEN 8
#define PAYLOAD_LEN 4

#define PACKED __attribute__((__packed__))

#define ADDR_THERMISTOR1_TEMP 0x0C
#define ADDR_THERMISTOR2_TEMP 0x0D

#define MAX_ADDR 0x0D

#define FUCK_CHC {}

typedef struct {
uint8_t header[SLAVE_PACKET_LEN];
uint8_t addr;
uint8_t payload[PAYLOAD_LEN];
} PACKED SlavePkt;

class SlaveThread {
public:
static void initialize();

static osThreadId_t getThreadId();
private:
static RTOSThread thread;
static void runThread(void* args);
};
4 changes: 2 additions & 2 deletions STM32F405RGTx/Core/Inc/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

//////////////////////////////////////////////////////////////
// PACK PARAMETERS
#define PRECHARGE_VOLTAGE_THRESHOLD 40.0
#define PRECHARGE_VOLTAGE_THRESHOLD 35

#define MAX_PACK_CURRENT_SEVERE 50.0
#define MAX_PACK_CURRENT_NORMAL 40.0

#define MAX_PACK_VOLTAGE_SEVERE 50.0
#define MIN_PACK_VOLTAGE_SEVERE 40.0
#define MAX_PACK_VOLTAGE_NORMAL 47.0
#define MIN_PACK_VOLTAGE_NORMAL 44.0
#define MIN_PACK_VOLTAGE_NORMAL 40.0

#define MAX_BUCK_TEMP_SEVERE 100.0
#define MAX_BUCK_TEMP_NORMAL 80.0
Expand Down
1 change: 1 addition & 0 deletions STM32F405RGTx/Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void UsageFault_Handler(void);
void DebugMon_Handler(void);
void CAN1_RX0_IRQHandler(void);
void TIM1_BRK_TIM9_IRQHandler(void);
void USART1_IRQHandler(void);
void EXTI15_10_IRQHandler(void);
void TIM7_IRQHandler(void);
void DMA2_Stream0_IRQHandler(void);
Expand Down
4 changes: 4 additions & 0 deletions STM32F405RGTx/Core/Inc/threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ltc6813_thread.hpp"
#include "bist_thread.hpp"
#include "LED_thread.hpp"
#include "slave_thread.hpp"
#include "CAN_thread.hpp"

// see ./docs/utilization.xlsx for schedulability calculations
Expand All @@ -32,6 +33,9 @@
#define LTC6813_THREAD_PERIODICITY 500
#define LTC6813_THREAD_PRIORITY osPriorityRealtime3

#define SLAVE_THREAD_PERIODICITY 500
#define SLAVE_THREAD_PRIORITY osPriorityRealtime1


// tasks with SOFT timing deadlines
#define BIST_THREAD_PERIODICITY 50
Expand Down
3 changes: 2 additions & 1 deletion STM32F405RGTx/Core/Src/bms_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ int bms_entry() {
CoulombCountingThread::initialize();
StateMachineThread::initialize();
// Ltc6813Thread::initialize();
// SlaveThread::initialize();
CANThread::initialize();
BistThread::initialize();
// BistThread::initialize();
LEDThread::initialize();

// RUNNING A BMS test --> Don't start scheduler
Expand Down
53 changes: 53 additions & 0 deletions STM32F405RGTx/Core/Src/circ_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdint.h>
#include "circ_queue.h"

CircQueue CircQueue_init() {
CircQueue ret = {
.len = 0,
._arr = {},
._head = 0,
._tail = 0
};
return ret;
}

uint8_t CircQueue_empty(CircQueue* self) {
return self->len == 0;
}

CircQueueStatus CircQueue_put(CircQueue* self, SlavePkt* val) {
if ( !(self->len == CIRC_RX_BUFF_SIZE) ) {
// regular insert condition
if (CircQueue_empty(self)) {
self->_arr[self->_head] = *val;
}
else {
CIRC_INC_TAIL(self);
self->_arr[self->_tail] = *val;
}
self->len += 1;
return CircQueue_PUT_OK;
}
else {
// overwrite condition
self->_arr[self->_head] = *val;
CIRC_INC_HEAD(self);
CIRC_INC_TAIL(self);
return CircQueue_PUT_WRAPAROUND;
}
}
CircQueueStatus CircQueue_get(CircQueue* self, SlavePkt* val) {
if (self->len == 0) {
return CircQueue_GET_EMPTY;
}
else {
SlavePkt ret = self->_arr[self->_head];
CIRC_INC_HEAD(self);
if (!CircQueue_empty(self)) {
self->len -= 1;
}
*val = ret;
return CircQueue_GET_OK;
}
}

1 change: 1 addition & 0 deletions STM32F405RGTx/Core/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ static void MX_CAN1_Init(void)
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 5;
hcan1.Init.Mode = CAN_MODE_NORMAL;
// hcan1.Init.Mode = CAN_MODE_LOOPBACK;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void MeasurementsThread::processData() {
global_bms_data.contactor_voltage = UN_VOLTAGE_DIVIDE(ADC_TO_VOLTAGE(val));
break;
case 5:
global_bms_data.bms_current = INA180_VOLTAGE_TO_CURRENT(ADC_TO_VOLTAGE(val));
global_bms_data.bms_current = INA180_VOLTAGE_TO_CURRENT(ADC_TO_VOLTAGE(val))/2;
break;
}
}
Expand Down
56 changes: 56 additions & 0 deletions STM32F405RGTx/Core/Src/rtos_threads/slave_thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "bms_entry.hpp"
#include "threads.hpp"
#include "main.h"
#include "can.h"
#include "circ_queue.h"

RTOSThread SlaveThread::thread;
static CircQueue _rx_CircQueue;
static uint8_t _rx_bytes[sizeof(SlavePkt)];

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
SlavePkt* p_pkt = (SlavePkt*)_rx_bytes;
CircQueue_put(&_rx_CircQueue, p_pkt);

HAL_UART_Receive_IT(&huart1, _rx_bytes, sizeof(SlavePkt));
}

void SlaveThread::initialize() {
thread = RTOSThread(
"slave_thread",
1024*2,
SLAVE_THREAD_PRIORITY,
runThread
);
}


void SlaveThread::runThread(void* args) {
SlavePkt rqst_pkt = {
.header = SLAVE_PKT_HEADER,
.addr = 0,
.payload = {0, 0, 0, 0}
};
SlavePkt pkt;

HAL_UART_Receive_IT(&huart1, _rx_bytes, sizeof(SlavePkt));

while (1) {
// Send a request to the slave for data packets
HAL_UART_Transmit(&huart1, (uint8_t*)&rqst_pkt, sizeof(SlavePkt), 100);

for (uint8_t i = 0; i < 14; i++) {
HAL_UART_Receive(&huart1, (uint8_t*)&pkt, sizeof(SlavePkt), HAL_MAX_DELAY);

if (pkt.addr <= 12) {
// We are dealing with one of the 12 (2 batt * 6 cell/batt) LiPo cells
global_bms_data.battery.cells[pkt.addr].voltage = UINT_TO_FLOAT(*((uint32_t*)pkt.payload));
} else if (pkt.addr <= 14) {
// We are dealing with one of the temp sensors. We will store them in the first two cell temperatures
global_bms_data.battery.cells[pkt.addr - 13].temp = UINT_TO_FLOAT(*((uint32_t*)pkt.payload));
} else {} // Something went wrong, idk maybe do something?

@Nushaab-Syed Nushaab-Syed May 27, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure what the limitations of the perfboard slave device are but can we send an error message via uart? something like a printf statement would be sufficient

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

}

osDelay(SLAVE_THREAD_PERIODICITY);
}
}
Loading