Skip to content

Commit

Permalink
serial print, close to can msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Apr 19, 2024
1 parent 5ca12ac commit 595c93a
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 44 deletions.
26 changes: 13 additions & 13 deletions .mxproject

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions Charger-FW.ioc
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#MicroXplorer Configuration settings - do not modify
CAD.formats=
CAD.pinconfig=
CAD.formats=[]
CAD.pinconfig=Dual
CAD.provider=
FDCAN1.CalculateBaudRateNominal=500000
FDCAN1.CalculateTimeBitNominal=2000
FDCAN1.CalculateTimeQuantumNominal=250.0
FDCAN1.IPParameters=CalculateTimeQuantumNominal,CalculateTimeBitNominal,CalculateBaudRateNominal,NominalPrescaler,NominalTimeSeg2,NominalTimeSeg1
FDCAN1.DataPrescaler=4
FDCAN1.DataTimeSeg1=3
FDCAN1.DataTimeSeg2=4
FDCAN1.IPParameters=CalculateTimeQuantumNominal,CalculateTimeBitNominal,CalculateBaudRateNominal,NominalPrescaler,NominalTimeSeg2,NominalTimeSeg1,DataPrescaler,DataTimeSeg1,DataTimeSeg2,Mode
FDCAN1.Mode=FDCAN_MODE_NORMAL
FDCAN1.NominalPrescaler=4
FDCAN1.NominalTimeSeg1=3
FDCAN1.NominalTimeSeg2=4
Expand Down
4 changes: 2 additions & 2 deletions Core/Inc/can.h → Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CAN_H
#define CAN_H
#ifndef CAN_HANDLER_H
#define CAN_HANDLER_H

#include <stdint.h>
#include "stm32g4xx_hal.h"
Expand Down
3 changes: 1 addition & 2 deletions Core/Inc/iroh_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ void vButtonMonitor(void* pv_params);
extern osThreadId_t button_monitor_handle;
extern const osThreadAttr_t button_monitor_attributes;

/*
void vSendChargerState(void* pv_params);
extern osThreadId_t send_charger_state_handle;
extern const osThreadAttr_t send_charger_state_attributes; */
extern const osThreadAttr_t send_charger_state_attributes;

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

#include "cmsis_os.h"

/* Function to queue a message to be sent on the UART stream */
int serial_print(const char* format, ...);

/* Defining Temperature Monitor Task */
void vSerialMonitor(void* pv_params);

extern osThreadId_t serial_monitor_handle;
extern const osThreadAttr_t serial_monitor_attributes;

#endif // SERIAL_MONITOR_H
21 changes: 15 additions & 6 deletions Core/Src/can.c → Core/Src/can_handler.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "can.h"
#include "can_handler.h"
#include "queues.h"
#include "string.h"

Expand All @@ -14,10 +14,10 @@ void receive_msg(FDCAN_HandleTypeDef *hfdcan)

uint8_t *rx_data;
if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &rx_header, rx_data) != HAL_OK) {
e.code = 2;
strcpy(e.msg, "Cannot get msg");
osMessageQueuePut(error_queue, &e, 0, 2000);
return;
// e.code = 2;
// strcpy(e.msg, "Cannot get msg");
// osMessageQueuePut(error_queue, &e, 0, 2000);
// return;
}

uint32_t message_id = rx_header.Identifier;
Expand Down Expand Up @@ -46,7 +46,16 @@ can_t* init_iroh_can(FDCAN_HandleTypeDef *hcan)
fdcan->id_list_len = 0;
fdcan->hcan = hcan;

can_init(fdcan);
uint8_t penis[8] = {1,1,1,1,1,1,1,1};

can_msg_t penis_msg = {
.data = penis,
.id = 0x69,
.len = 8};

can_send_msg(fdcan, &penis_msg);

//can_init(fdcan);

HAL_FDCAN_RxFifo0Callback(hcan, 0);

Expand Down
20 changes: 11 additions & 9 deletions Core/Src/iroh_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "queues.h"
#include "seven_segment.h"
#include "switch.h"
#include "serial_monitor.h"

static I2C_HandleTypeDef *hi2c;

Expand All @@ -20,7 +21,7 @@ int state_idx = 0;
osThreadId_t send_bms_data_handle;
const osThreadAttr_t send_bms_data_attributes = {
.name = "DisplayBmsData",
.stack_size = 128 * 8,
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityAboveNormal4};

void vDisplayBmsData(void *pv_params)
Expand All @@ -31,7 +32,8 @@ void vDisplayBmsData(void *pv_params)

for (;;)
{
if (osMessageQueueGet(bms_data_queue, &bms_data, NULL, 0U) == osOK)
//serial_print("BIG DICK\r\n");
if (osMessageQueueGet(bms_data_queue, &bms_data, NULL, 10U) == osOK)
{
if (states[state_idx] == CURRENT_AND_SOC)
{
Expand All @@ -48,7 +50,7 @@ void vDisplayBmsData(void *pv_params)
osThreadId_t button_monitor_handle;
const osThreadAttr_t button_monitor_attributes = {
.name = "DisplayBmsData",
.stack_size = 128 * 8,
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityAboveNormal4};

void vButtonMonitor(void *pv_params)
Expand All @@ -69,25 +71,25 @@ void vButtonMonitor(void *pv_params)
}
}

/* osThreadId_t send_charger_state_handle;
osThreadId_t send_charger_state_handle;
const osThreadAttr_t send_charger_state_attributes = {
.name = "SendChargerState",
.stack_size = 128 * 8,
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityAboveNormal4};

void vSendChargerState(void *pv_params)
{

can_t *can = (can_t *)pv_params;
uint8_t penis[8] = {'P', 'E', 'N', 'I', 'S'};
can_t* can = (can_t *)pv_params;
uint8_t penis[8] = {1,1,1,1,1,1,1,1};

can_msg_t penis_msg = {
.data = penis,
.id = 0x69,
.len = 5};
.len = 8};

for (;;)
{
can_send_msg(can, &penis_msg);
}
} */
}
33 changes: 27 additions & 6 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
/* USER CODE BEGIN Includes */
#include "queues.h"
#include "iroh_tasks.h"
#include "can.h"
#include "can_handler.h"
#include "serial_monitor.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -75,7 +76,26 @@ void StartDefaultTask(void *argument);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}

int _write(int file, char* ptr, int len) {
int DataIdx;

for (DataIdx = 0; DataIdx < len; DataIdx++) {
__io_putchar( *ptr++ );
}
return len;
}
/* USER CODE END 0 */

/**
Expand Down Expand Up @@ -110,7 +130,7 @@ int main(void)
MX_USART2_UART_Init();
MX_FDCAN1_Init();
/* USER CODE BEGIN 2 */
init_iroh_can(&hfdcan1);
can_t* iroh_can = init_iroh_can(&hfdcan1);
/* USER CODE END 2 */

/* Init scheduler */
Expand Down Expand Up @@ -140,6 +160,8 @@ int main(void)
/* USER CODE BEGIN RTOS_THREADS */
send_bms_data_handle = osThreadNew(vDisplayBmsData, &hi2c2, &send_bms_data_attributes);
button_monitor_handle = osThreadNew(vButtonMonitor, GPIOB, &button_monitor_attributes);
send_charger_state_handle = osThreadNew(vSendChargerState, iroh_can, &send_charger_state_attributes);
serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);
/* USER CODE END RTOS_THREADS */

/* USER CODE BEGIN RTOS_EVENTS */
Expand Down Expand Up @@ -227,10 +249,10 @@ static void MX_FDCAN1_Init(void)
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 3;
hfdcan1.Init.NominalTimeSeg2 = 4;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataPrescaler = 4;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 1;
hfdcan1.Init.DataTimeSeg2 = 1;
hfdcan1.Init.DataTimeSeg1 = 3;
hfdcan1.Init.DataTimeSeg2 = 4;
hfdcan1.Init.StdFiltersNbr = 0;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
Expand Down Expand Up @@ -376,7 +398,6 @@ static void MX_GPIO_Init(void)
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/* USER CODE BEGIN Header_StartDefaultTask */
Expand Down
63 changes: 63 additions & 0 deletions Core/Src/serial_monitor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "serial_monitor.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PRINTF_QUEUE_SIZE 16 /* Strings */
#define PRINTF_BUFFER_LEN 128 /* Characters */

osMessageQueueId_t printf_queue;
osThreadId_t serial_monitor_handle;
const osThreadAttr_t serial_monitor_attributes;

/*
* Referenced https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Print.cpp
* Preformat string then put into a buffer
*/
int serial_print(const char* format, ...)
{
va_list arg;
char* buffer = malloc(sizeof(char) * PRINTF_BUFFER_LEN);
if (buffer == NULL)
return -1;

/* Format Variadic Args into string */
va_start(arg, format);
size_t len = vsnprintf(buffer, PRINTF_BUFFER_LEN, format, arg);
va_end(arg);

/* Check to make sure we don't overflow buffer */
if (len > PRINTF_BUFFER_LEN - 1) {
free(buffer);
return -2;
}

/* If string can't be queued */
osStatus_t stat = osMessageQueuePut(printf_queue, &buffer, 0U, 0U);
if (stat) {
free(buffer);
return -3;
}

return 0;
}

void vSerialMonitor(void* pv_params)
{
char* message;
osStatus_t status;

printf_queue = osMessageQueueNew(PRINTF_QUEUE_SIZE, sizeof(char*), NULL);

for (;;) {
/* Wait until new printf message comes into queue */
status = osMessageQueueGet(printf_queue, &message, NULL, osWaitForever);
if (status != osOK) {
// TODO: Trigger fault ?
} else {
printf(message);
free(message);
}
}
}
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Mon Apr 15 21:40:21 EDT 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Thu Apr 18 18:14:29 EDT 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down Expand Up @@ -43,7 +43,8 @@ Core/Src/stm32g4xx_hal_msp.c \
Core/Src/seven_segment.c \
Core/Src/switch.c \
Core/Src/iroh_tasks.c \
Core/Src/can.c \
Core/Src/can_handler.c \
Core/Src/serial_monitor.c \
Drivers/Embedded-Base/platforms/stm32g431/src/fdcan.c \
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c \
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_rcc.c \
Expand Down Expand Up @@ -75,7 +76,7 @@ Middlewares/Third_Party/FreeRTOS/Source/tasks.c \
Middlewares/Third_Party/FreeRTOS/Source/timers.c \
Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c \
Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c \
Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c
Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c \

# ASM sources
ASM_SOURCES = \
Expand Down

0 comments on commit 595c93a

Please sign in to comment.