Skip to content

Commit

Permalink
Rtos develop (#22)
Browse files Browse the repository at this point in the history
* big branch of dumb shit

* changed ioc and makefile and fixed dumb stuff

* shame

* minor change

* Created can.c, added receive_msg function, initialed CAN

* idk what changes i made

* Added functionality to receiving CAN IDs (#21)

---------

Co-authored-by: Scott A <[email protected]>
Co-authored-by: caiodasilva2005 <[email protected]>
Co-authored-by: caiodasilva2005 <[email protected]>
  • Loading branch information
4 people authored Feb 18, 2024
1 parent 3bb73d7 commit 3d9fed2
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 94 deletions.
Binary file modified .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ ctrlp.root
*.html

#DS_Store
.DS_Store
.DS_Store

#misc
scott.txt
36 changes: 18 additions & 18 deletions .mxproject

Large diffs are not rendered by default.

48 changes: 30 additions & 18 deletions Charger-FW.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,47 @@
CAD.formats=
CAD.pinconfig=
CAD.provider=
FDCAN1.CalculateBaudRateNominal=200000
FDCAN1.CalculateTimeBitNominal=5000
FDCAN1.CalculateTimeQuantumNominal=1000.0
FDCAN1.IPParameters=CalculateTimeQuantumNominal,CalculateTimeBitNominal,CalculateBaudRateNominal
FREERTOS.IPParameters=Tasks01
FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL
File.Version=6
GPIO.groupedBy=
KeepUserPlacement=false
Mcu.CPN=STM32G431C8T6
Mcu.Family=STM32G4
Mcu.IP0=FREERTOS
Mcu.IP1=I2C2
Mcu.IP2=NVIC
Mcu.IP3=RCC
Mcu.IP4=SYS
Mcu.IP5=USART2
Mcu.IPNb=6
Mcu.IP0=FDCAN1
Mcu.IP1=FREERTOS
Mcu.IP2=I2C2
Mcu.IP3=NVIC
Mcu.IP4=RCC
Mcu.IP5=SYS
Mcu.IP6=USART2
Mcu.IPNb=7
Mcu.Name=STM32G431C(6-8-B)Tx
Mcu.Package=LQFP48
Mcu.Pin0=PA2
Mcu.Pin1=PA3
Mcu.Pin10=PB4
Mcu.Pin11=PB5
Mcu.Pin12=PB6
Mcu.Pin13=PB7
Mcu.Pin14=VP_FREERTOS_VS_CMSIS_V2
Mcu.Pin15=VP_SYS_VS_Systick
Mcu.Pin16=VP_SYS_VS_DBSignals
Mcu.Pin10=PA14
Mcu.Pin11=PB3
Mcu.Pin12=PB4
Mcu.Pin13=PB5
Mcu.Pin14=PB6
Mcu.Pin15=PB7
Mcu.Pin16=VP_FREERTOS_VS_CMSIS_V2
Mcu.Pin17=VP_SYS_VS_Systick
Mcu.Pin18=VP_SYS_VS_DBSignals
Mcu.Pin2=PB0
Mcu.Pin3=PB11
Mcu.Pin4=PB12
Mcu.Pin5=PA8
Mcu.Pin6=PA9
Mcu.Pin7=PA13
Mcu.Pin8=PA14
Mcu.Pin9=PB3
Mcu.PinsNb=17
Mcu.Pin7=PA11
Mcu.Pin8=PA12
Mcu.Pin9=PA13
Mcu.PinsNb=19
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32G431C8Tx
Expand All @@ -55,6 +62,10 @@ NVIC.SavedSvcallIrqHandlerGenerated=true
NVIC.SavedSystickIrqHandlerGenerated=true
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:true\:false\:true\:false
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
PA11.Mode=FDCAN_Activate
PA11.Signal=FDCAN1_RX
PA12.Mode=FDCAN_Activate
PA12.Signal=FDCAN1_TX
PA13.Locked=true
PA13.Signal=SYS_JTMS-SWDIO
PA14.Locked=true
Expand Down Expand Up @@ -167,3 +178,4 @@ VP_SYS_VS_DBSignals.Signal=SYS_VS_DBSignals
VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
board=custom
rtos.0.ip=FREERTOS
5 changes: 5 additions & 0 deletions Core/Inc/can.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdint.h>
#include "stm32g4xx_hal.h"
#include "fdcan.h"

HAL_StatusTypeDef receive_msg(can_t *hcan);
12 changes: 12 additions & 0 deletions Core/Inc/iroh_tasks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef IROH_TASKS_H
#define IROH_TASKS_H

#include "cmsis_os.h"
#include "seven_segment.h"
#include "fdcan.h"

void vSendBmsData(void* pv_params);
extern osThreadId_t send_bms_data_handle;
extern const osThreadAttr_t send_bms_data_attributes;

#endif
15 changes: 15 additions & 0 deletions Core/Inc/queues.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef QUEUES_H
#define QUEUES_H

#include "cmsis_os.h"

#define BMS_DATA_QUEUE_SIZE 8

typedef struct {
uint8_t SoC;
uint8_t current;
} bms_data_t;

extern osMessageQueueId_t bms_data_queue;

#endif
4 changes: 4 additions & 0 deletions Core/Inc/seven_segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_i2c.h"
#include "cmsis_os.h"
#include "seven_segment.h"
#include "fdcan.h"
#include "queues.h"

typedef struct {
uint16_t mem_address; //internal memory address
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#define SWITCH_3_PIN GPIO_PIN_6
#define SWITCH_4_PIN GPIO_PIN_7

bool get_switch(GPIO_InitTypeDef, uint8_t);
bool get_switch(GPIO_InitTypeDef *init, uint8_t pin);

#endif
43 changes: 43 additions & 0 deletions Core/Src/can.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "can.h"
#include "queues.h"

//Arbitrary CAN IDs
#define CAN_ID 0x69

bms_data_t bms_data;


void init_iroh_can(FDCAN_HandleTypeDef *hcan) {
can_t fdcan;
fdcan.callback = receive_msg;
fdcan.id_list = 0;
fdcan.id_list_len = 0;
fdcan.hcan = hcan;

can_init(&fdcan);
}

HAL_StatusTypeDef receive_msg(can_t *can)
{

FDCAN_RxHeaderTypeDef rx_header;
uint8_t rx_data[8]; //array of 8 bytes

if (HAL_FDCAN_GetRxFifoFillLevel(can->hcan, FDCAN_RX_FIFO0) > 0) { //Check if messages are available
if (HAL_FDCAN_GetRxMessage(can->hcan,FDCAN_RX_FIFO0,&rx_header,rx_data) == HAL_OK) {
uint32_t message_id = rx_header.Identifier;
if (message_id == CAN_ID) {
bms_data.SoC = (uint8_t) rx_data[0];
bms_data.current = (uint8_t) rx_data[1];
if (osStatus_t osMessageQueuePut(bms_data_queue *bms_data, 0, 2000) == osOK) //timeout is 2000 ms
return HAL_OK;
else
return HAL_ERROR
}
}
return HAL_ERROR;
}
return HAL_BUSY;
}


27 changes: 27 additions & 0 deletions Core/Src/iroh_tasks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* All RTOS tasks are defined here */

#include "iroh_tasks.h"
#include "queues.h"
#include "seven_segment.h"

static I2C_HandleTypeDef* hi2c;

osThreadId_t send_bms_data_handle;
const osThreadAttr_t send_bms_data_attributes = {
.name = "SendBmsData",
.stack_size = 128 * 8,
.priority = (osPriority_t)osPriorityAboveNormal4
};

void vSendBmsData(void* pv_params) {

hi2c = (I2C_HandleTypeDef*)pv_params;
bms_data_t bms_data;

for (;;) {
if (osMessageQueueGet(bms_data_queue, &bms_data, NULL, 0U)) {
write_current(hi2c, bms_data.current);
write_charge(hi2c, bms_data.SoC);
}
}
}
Loading

0 comments on commit 3d9fed2

Please sign in to comment.