Merge branch 'j1939'

This commit was merged in pull request #14.
This commit is contained in:
2026-06-15 10:30:34 +02:00
7 changed files with 708 additions and 586 deletions
+3 -1
View File
@@ -8,4 +8,6 @@
.metadata .metadata
Core/Src/Backup/ Core/Src/Backup/
Core/Inc/Backup/ Core/Inc/Backup/
Debug .bak
.gitignore
can_logger Debug.launch
+49
View File
@@ -1,7 +1,53 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : canlog.h
* @brief : Header for canlog.c file.
* This file contains the defines related to SAE J1939 CAN.
******************************************************************************
* @attention
*
* Copyright (c) 2026 Erick Ahmed.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef CANLOG_H #ifndef CANLOG_H
#define CANLOG_H #define CANLOG_H
#include "main.h" #include "main.h"
#include "cmsis_os.h"
extern osMessageQueueId_t xCAN1RxQueue;
extern osMessageQueueId_t xCAN2RxQueue;
extern osSemaphoreId_t xSemaphoreCAN1;
extern osSemaphoreId_t xSemaphoreCAN2;
/* USER CODE BEGIN PTD */
typedef struct {
GPIO_TypeDef* port;
uint16_t pin;
} LED_Config;
typedef struct {
uint32_t id;
uint8_t payload[8];
uint8_t dlc;
uint8_t isExtended;
//uint32_t timestamp; //Enable TIM2: 42 MHz / (41+1) = 1 MHz → 1 µs tick; 32bit autoreload freerunning
// this is for getting timestamps on can messages
} CanMessage_t;
typedef struct {
LED_Config *led;
osSemaphoreId_t semaphore;
osTimerId_t timer;
} LEDContext;
/* USER CODE END PTD */
/** /**
* @brief Initializes the CAN logger modules, OS threads, queues, and hardware. * @brief Initializes the CAN logger modules, OS threads, queues, and hardware.
@@ -9,5 +55,8 @@
* @param hcan2 Pointer to the CAN2 handle * @param hcan2 Pointer to the CAN2 handle
*/ */
void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2); void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2);
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
void vCANLoggerListen(void *argument);
void vLEDHeartbeat(void *argument);
#endif /* CANLOG_H */ #endif /* CANLOG_H */
+3 -4
View File
@@ -8,11 +8,9 @@
* @attention * @attention
* *
* Copyright (c) 2026 STMicroelectronics. * Copyright (c) 2026 STMicroelectronics.
* All rights reserved. * Copyright (c) 2026 Erick Ahmed.
* *
* This software is licensed under terms that can be found in the LICENSE file * SPDX-License-Identifier: GPL-3.0-or-later
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
* *
****************************************************************************** ******************************************************************************
*/ */
@@ -28,6 +26,7 @@ extern "C" {
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "canlog.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
+128 -25
View File
@@ -1,40 +1,143 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : canlog.c
* @brief : SAE J1939 CAN Real Time listening and decoding
******************************************************************************
* @attention
*
* Copyright (c) 2026 Erick Ahmed.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "canlog.h" #include "canlog.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "cmsis_os.h" #include "cmsis_os.h"
#include <string.h> #include <string.h>
/* USER CODE END Includes */
/* Definitions for canBus1Listen */ extern CAN_HandleTypeDef hcan1;
osThreadId_t canBus1ListenHandle; extern CAN_HandleTypeDef hcan2;
const osThreadAttr_t canBus1Listen_attributes = {
.name = "canBus1Listen",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityRealtime1,
};
/* Definitions for canBus2Listen */
osThreadId_t canBus2ListenHandle;
const osThreadAttr_t canBus2Listen_attributes = {
.name = "canBus2Listen",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityRealtime,
};
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2){} /* USER CODE BEGIN FunctionPrototypes */
static void CAN_Listen(void *argument){} /* BEGIN CAN_Logger_Init */
/* BEGIN Header_CAN_Listen */
/** /**
* @brief Function implementing the canBus0Listen thread. * @brief Initialize CAN bus logger.
* @param argument: Not used * @param argument: hcan1, hcan2
* @retval None * @retval None
*/ */
/* END Header_CAN_Listen */ void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2)
void CAN_Listen(void *argument) {
CAN_FilterTypeDef filter = {0};
filter.FilterMode = CAN_FILTERMODE_IDMASK;
filter.FilterScale = CAN_FILTERSCALE_32BIT;
filter.FilterIdHigh = 0x0000;
filter.FilterMaskIdHigh = 0x0000;
filter.FilterIdLow = 0x0000;
filter.FilterMaskIdLow = 0x0000;
filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
filter.FilterActivation = ENABLE;
filter.SlaveStartFilterBank = 14;
filter.FilterBank = 0;
if (HAL_CAN_ConfigFilter(hcan1, &filter) != HAL_OK) Error_Handler();
filter.FilterBank = 14;
if (HAL_CAN_ConfigFilter(hcan2, &filter) != HAL_OK) Error_Handler();
if (HAL_CAN_Start(hcan1) != HAL_OK) Error_Handler();
if (HAL_CAN_Start(hcan2) != HAL_OK) Error_Handler();
if (HAL_CAN_ActivateNotification(hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) Error_Handler();
if (HAL_CAN_ActivateNotification(hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) Error_Handler();
}
/* END CAN_Logger_Init */
/* BEGIN HAL_CAN_RxFifo0MsgPendingCallback */
/**
* @brief ISR for CAN message pending in FIFO0
* @param argument: Can handle hcan (hcan1 or hcan2)
* @retval None
*/
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{ {
/* CODE BEGIN */ /* CODE BEGIN */
/* Infinite loop */ CanMessage_t message;
for(;;) CAN_RxHeaderTypeDef rxHeader;
uint8_t data[8];
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, data) != HAL_OK) Error_Handler();
message.id = rxHeader.ExtId;
message.dlc = rxHeader.DLC;
message.isExtended = (rxHeader.IDE == CAN_ID_EXT);
memcpy(message.payload, data, rxHeader.DLC);
osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK)
{ {
osDelay(1); // TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors
// send errors like queue full via UART
} }
/* CODE END */ /* CODE END */
} }
/* END HAL_CAN_RxFifo0MsgPendingCallback */
/* BEGIN vCANLoggerListen */
/**
* @brief Log incoming CAN bus traffic in FIFO buffer
* @param argument: Not used
* @retval None
*/
void vCANLoggerListen(void *argument)
{
/* CODE BEGIN */
CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)argument;
osMessageQueueId_t queue;
CanMessage_t message;
queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
for (;;)
{
if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK)
{
osSemaphoreRelease( (hcan->Instance == CAN1) ? xSemaphoreCAN1 : xSemaphoreCAN2 );
// TODO: use this semaphore
}
}
/* CODE END */
}
/* END vCANLoggerListen */
/* BEGIN vLEDHeartbeat */
/**
* @brief Blink a LED in heartbeat mode when CAN traffic is detected
* @param argument: LED GPIO (port and pin)
* @retval None
*/
void vLEDHeartbeat(void *argument)
{
/* CODE BEGIN */
LEDContext *context = (LEDContext*)argument;
if (osSemaphoreAcquire(context->semaphore, 0U) == osOK)
{
HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET);
osTimerStart(context->timer, 25U);
}
else
{
HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_RESET);
}
/* CODE END */
}
/* END vLEDHeartbeat */
/* USER CODE END FunctionPrototypes */
-59
View File
@@ -1,59 +0,0 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */
+90 -24
View File
@@ -5,30 +5,25 @@
* @brief : Main program body * @brief : Main program body
****************************************************************************** ******************************************************************************
* @attention * @attention
*
* Copyright (c) 2026 STMicroelectronics. * Copyright (c) 2026 STMicroelectronics.
* All rights reserved. * Copyright (c) 2026 Erick Ahmed.
* *
* This software is licensed under terms that can be found in the LICENSE file * SPDX-License-Identifier: GPL-3.0-or-later
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
* *
****************************************************************************** ******************************************************************************
*/ */
/* USER CODE END Header */ /* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "main.h" #include "main.h"
#include "canlog.h"
#include "cmsis_os.h" #include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "canlog.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */ /* USER CODE BEGIN PTD */
/* USER CODE END PTD */ /* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
@@ -50,7 +45,21 @@ DMA_HandleTypeDef hdma_sdio_rx;
DMA_HandleTypeDef hdma_sdio_tx; DMA_HandleTypeDef hdma_sdio_tx;
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
LED_Config led_can1 = {GPIOB, GPIO_PIN_2};
LED_Config led_can2 = {GPIOB, GPIO_PIN_5};
LED_Config led_error = {GPIOB, GPIO_PIN_3};
LEDContext ledContextCAN1;
LEDContext ledContextCAN2;
osSemaphoreId_t xSemaphoreCAN1;
osSemaphoreId_t xSemaphoreCAN2;
osTimerId_t xHeartbeatTimerCAN1;
osTimerId_t xHeartbeatTimerCAN2;
osMessageQueueId_t xCAN1RxQueue;
osMessageQueueId_t xCAN2RxQueue;
/* USER CODE END PV */ /* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
@@ -62,7 +71,6 @@ static void MX_CAN2_Init(void);
static void MX_SDIO_SD_Init(void); static void MX_SDIO_SD_Init(void);
/* USER CODE BEGIN PFP */ /* USER CODE BEGIN PFP */
/* USER CODE END PFP */ /* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
@@ -103,40 +111,84 @@ int main(void)
MX_CAN1_Init(); MX_CAN1_Init();
MX_CAN2_Init(); MX_CAN2_Init();
MX_SDIO_SD_Init(); MX_SDIO_SD_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
// Initialize CAN1 and CAN2
CAN_Logger_Init(&hcan1, &hcan2); CAN_Logger_Init(&hcan1, &hcan2);
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Init scheduler */ /* Init scheduler */
osKernelInitialize(); osKernelInitialize();
/* USER CODE BEGIN RTOS_TASKS */
osThreadId_t xCAN1rx;
const osThreadAttr_t CAN1rxAttributes = {
.name = "CAN1rx",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityRealtime1,
};
osThreadId_t xCAN2rx;
const osThreadAttr_t CAN2rxAttributes = {
.name = "CAN2rx",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityRealtime,
};
/* USER END RTOS_TASKS */
/* USER CODE BEGIN RTOS_MUTEX */ /* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */ /* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */ /* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */ xSemaphoreCAN1 = osSemaphoreNew(10, 0, NULL);
xSemaphoreCAN2 = osSemaphoreNew(10, 0, NULL);
if (xSemaphoreCAN1 == NULL || xSemaphoreCAN2 == NULL) Error_Handler();
ledContextCAN1.led = &led_can1;
ledContextCAN1.semaphore = xSemaphoreCAN1;
ledContextCAN2.led = &led_can2;
ledContextCAN2.semaphore = xSemaphoreCAN2;
/* USER CODE END RTOS_SEMAPHORES */ /* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */ /* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */ xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN1, NULL);
xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN2, NULL);
osTimerStart(xHeartbeatTimerCAN1, 25U);
osTimerStart(xHeartbeatTimerCAN2, 25U);
if (xHeartbeatTimerCAN1 == NULL || xHeartbeatTimerCAN2 == NULL) Error_Handler();
ledContextCAN1.timer = xHeartbeatTimerCAN1;
ledContextCAN2.timer = xHeartbeatTimerCAN2;
/* USER CODE END RTOS_TIMERS */ /* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */ /* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */ xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler();
/* USER CODE END RTOS_QUEUES */ /* USER CODE END RTOS_QUEUES */
/* USER CODE BEGIN RTOS_THREADS */ /* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */ xCAN1rx = osThreadNew(vCANLoggerListen, &hcan1, &CAN1rxAttributes);
xCAN2rx = osThreadNew(vCANLoggerListen, &hcan2, &CAN2rxAttributes);
/* USER CODE END RTOS_THREADS */ /* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */ /* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */ /* add events, ... */
/* USER CODE END RTOS_EVENTS */ /* USER CODE END RTOS_EVENTS */
/* USER CODE BEGIN 3 */
ledContextCAN1.led = &led_can1;
ledContextCAN1.semaphore = xSemaphoreCAN1;
ledContextCAN1.timer = xHeartbeatTimerCAN1;
ledContextCAN2.led = &led_can2;
ledContextCAN2.semaphore = xSemaphoreCAN2;
ledContextCAN2.timer = xHeartbeatTimerCAN2;
/* USER CODE END 3 */
/* Start scheduler */ /* Start scheduler */
osKernelStart(); osKernelStart();
@@ -148,9 +200,9 @@ int main(void)
{ {
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 4 */
} }
/* USER CODE END 3 */ /* USER CODE END 4 */
} }
/** /**
@@ -222,7 +274,7 @@ static void MX_CAN1_Init(void)
hcan1.Init.TimeSeg2 = CAN_BS2_4TQ; hcan1.Init.TimeSeg2 = CAN_BS2_4TQ;
hcan1.Init.TimeTriggeredMode = DISABLE; hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE; hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE; hcan1.Init.AutoWakeUp = ENABLE;
hcan1.Init.AutoRetransmission = DISABLE; hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE; hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE; hcan1.Init.TransmitFifoPriority = DISABLE;
@@ -259,7 +311,7 @@ static void MX_CAN2_Init(void)
hcan2.Init.TimeSeg2 = CAN_BS2_4TQ; hcan2.Init.TimeSeg2 = CAN_BS2_4TQ;
hcan2.Init.TimeTriggeredMode = DISABLE; hcan2.Init.TimeTriggeredMode = DISABLE;
hcan2.Init.AutoBusOff = DISABLE; hcan2.Init.AutoBusOff = DISABLE;
hcan2.Init.AutoWakeUp = DISABLE; hcan2.Init.AutoWakeUp = ENABLE;
hcan2.Init.AutoRetransmission = DISABLE; hcan2.Init.AutoRetransmission = DISABLE;
hcan2.Init.ReceiveFifoLocked = DISABLE; hcan2.Init.ReceiveFifoLocked = DISABLE;
hcan2.Init.TransmitFifoPriority = DISABLE; hcan2.Init.TransmitFifoPriority = DISABLE;
@@ -400,11 +452,9 @@ static void MX_GPIO_Init(void)
/* USER CODE END MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */
} }
/* USER CODE BEGIN 4 */ /* USER CODE BEGIN 5 */
/* USER CODE END 4 */
/* USER CODE END 5 */
/** /**
* @brief Period elapsed callback in non blocking mode * @brief Period elapsed callback in non blocking mode
@@ -437,8 +487,24 @@ void Error_Handler(void)
/* USER CODE BEGIN Error_Handler_Debug */ /* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */ /* User can add his own implementation to report the HAL error return state */
__disable_irq(); __disable_irq();
//TODO: check if it is necessary to stop FreeRTOS
uint32_t error_code_can1;
uint32_t error_code_can2;
if (HAL_CAN_GetState(&hcan1) != HAL_CAN_STATE_READY) error_code_can1 = HAL_CAN_GetError(&hcan1);
if (HAL_CAN_GetState(&hcan2) != HAL_CAN_STATE_READY) error_code_can2 = HAL_CAN_GetError(&hcan2);
// TODO: write error to SD and/or serial
HAL_GPIO_WritePin(led_can1.port, led_can1.pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(led_can2.port, led_can2.pin, GPIO_PIN_RESET);
while (1) while (1)
{ {
HAL_GPIO_TogglePin(led_error.port, led_error.pin);
HAL_Delay(250);
} }
/* USER CODE END Error_Handler_Debug */ /* USER CODE END Error_Handler_Debug */
} }
-38
View File
@@ -1,38 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.