2 Commits

Author SHA1 Message Date
eeeck 9ea83fcbcd Make Gitea recognize license file correctly 2026-07-01 16:02:42 +02:00
eeeck 3e0ac47930 Fix links 2026-07-01 14:27:34 +02:00
7 changed files with 92 additions and 133 deletions
-1
View File
@@ -22,7 +22,6 @@
#include "main.h"
void uart_printf(const char *fmt, ...);
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void vUARTLogger(void *argument);
+9 -9
View File
@@ -18,7 +18,6 @@
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "cansend.h"
#include "cmsis_os.h"
#include <string.h>
/* USER CODE END Includes */
@@ -73,15 +72,11 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
osMessageQueueId_t queue;
osThreadId_t led_task;
if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV0) != RESET) __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
// TODO: manage the case of FIFO overflow
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, data) != HAL_OK) return;
if (rxHeader.DLC > 8) return;
if (rxHeader.IDE == CAN_ID_EXT) message.id = rxHeader.ExtId;
else message.id = rxHeader.StdId;
message.dlc = rxHeader.DLC;
message.isExtended = (rxHeader.IDE == CAN_ID_EXT);
message.source = (hcan->Instance == CAN1) ? 1 : 2;
@@ -118,19 +113,24 @@ void vCANListener(void *argument)
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
uint32_t irqn = (hcan->Instance == CAN1) ? CAN1_RX0_IRQn : CAN2_RX0_IRQn;
HAL_NVIC_SetPriority(irqn, 5, 0);
HAL_NVIC_SetPriority(irqn, 6, 0);
HAL_NVIC_EnableIRQ(irqn);
for (;;)
{
if (osMessageQueueGet(queue, &message, NULL, 1000) == osOK) osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
if (osMessageQueueGet(queue, &message, NULL, 1000) == osOK)
{
// J1939 decoding
osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
}
}
}
/* END vCANListener */
/* BEGIN vLEDHeartbeat */
/**
* @brief Turn on LED when CAN traffic is detected
* @brief Blink a LED in heartbeat mode when CAN traffic is detected
* @param argument: LED GPIO (port and pin)
* @retval None
*/
+55 -56
View File
@@ -40,28 +40,26 @@ extern osSemaphoreId_t xUARTDMASemaphore;
*/
void uart_printf(const char *fmt, ...)
{
if (__get_IPSR() != 0) return;
char buffer[128];
static char buf[128];
va_list args;
va_start(args, fmt);
int len = vsnprintf(buffer, sizeof(buffer), fmt, args);
int len = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (len > 0)
{
if (len >= sizeof(buffer)) len = sizeof(buffer) - 1;
if (osKernelGetState() == osKernelRunning)
if (osKernelGetState() == osKernelRunning && __get_IPSR() == 0)
{
if (osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever) == osOK)
{
HAL_UART_Transmit(&huart1, (uint8_t*)buffer, len, HAL_MAX_DELAY);
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, HAL_MAX_DELAY);
osSemaphoreRelease(xUARTDMASemaphore);
}
}
else HAL_UART_Transmit(&huart1, (uint8_t*)buffer, len, HAL_MAX_DELAY);
else
{
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, HAL_MAX_DELAY);
}
}
}
/* END uart_printf */
@@ -72,49 +70,49 @@ void uart_printf(const char *fmt, ...)
* @param arguments: buffer, source, message
* @retval None
*/
static int format_can_message(char *buffer, uint8_t source, const CanMessage_t *message)
static int format_can_message(char *buf, uint8_t source, const CanMessage_t *message)
{
const char hex[] = "0123456789ABCDEF";
buffer[0] = 'C';
buffer[1] = (source == 1) ? '1' : '2';
buffer[2] = ':';
buf[0] = 'C';
buf[1] = (source == 1) ? '1' : '2';
buf[2] = ':';
buffer[3] = hex[(message->id >> 28) & 0x0F];
buffer[4] = hex[(message->id >> 24) & 0x0F];
buffer[5] = hex[(message->id >> 20) & 0x0F];
buffer[6] = hex[(message->id >> 16) & 0x0F];
buffer[7] = hex[(message->id >> 12) & 0x0F];
buffer[8] = hex[(message->id >> 8) & 0x0F];
buffer[9] = hex[(message->id >> 4) & 0x0F];
buffer[10] = hex[message->id & 0x0F];
buf[3] = hex[(message->id >> 28) & 0x0F];
buf[4] = hex[(message->id >> 24) & 0x0F];
buf[5] = hex[(message->id >> 20) & 0x0F];
buf[6] = hex[(message->id >> 16) & 0x0F];
buf[7] = hex[(message->id >> 12) & 0x0F];
buf[8] = hex[(message->id >> 8) & 0x0F];
buf[9] = hex[(message->id >> 4) & 0x0F];
buf[10] = hex[message->id & 0x0F];
buffer[11] = ' ';
buffer[12] = hex[(message->dlc >> 4) & 0x0F];
buffer[13] = hex[message->dlc & 0x0F];
buffer[14] = ' ';
buf[11] = ' ';
buf[12] = hex[(message->dlc >> 4) & 0x0F];
buf[13] = hex[message->dlc & 0x0F];
buf[14] = ' ';
int index = 15;
int idx = 15;
for (int i = 0; i < 8; i++)
{
if (i < message->dlc)
{
buffer[index++] = hex[(message->payload[i] >> 4) & 0x0F];
buffer[index++] = hex[message->payload[i] & 0x0F];
buf[idx++] = hex[(message->payload[i] >> 4) & 0x0F];
buf[idx++] = hex[message->payload[i] & 0x0F];
}
else
{
buffer[index++] = ' ';
buffer[index++] = ' ';
buf[idx++] = ' ';
buf[idx++] = ' ';
}
if (i < 7) buffer[index++] = ' ';
if (i < 7) buf[idx++] = ' ';
}
buffer[index++] = '\r';
buffer[index++] = '\n';
buffer[index] = '\0';
buf[idx++] = '\r';
buf[idx++] = '\n';
buf[idx] = '\0';
return index;
return idx;
}
/* END format_can_message */
@@ -126,31 +124,22 @@ static int format_can_message(char *buffer, uint8_t source, const CanMessage_t *
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
if (huart->Instance == USART1)
{
osSemaphoreRelease(xUARTDMASemaphore);
}
}
/* END HAL_UART_TxCpltCallback */
/* BEGIN HAL_UART_ErrorCallback */
/**``MEN
* @brief Handle UART errors
/**
* @brief Safely release UART semaphore inside ISR and yield task if possible.
* @param argument: UART handle
* @retval None
*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
__HAL_UART_CLEAR_OREFLAG(huart);
__HAL_UART_CLEAR_FEFLAG(huart);
__HAL_UART_CLEAR_NEFLAG(huart);
__HAL_UART_CLEAR_PEFLAG(huart);
HAL_UART_DMAStop(huart);
huart->gState = HAL_UART_STATE_READY;
osSemaphoreRelease(xUARTDMASemaphore);
}
if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
}
/* END HAL_UART_ErrorCallback */
@@ -163,7 +152,7 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
void vUARTLogger(void *argument)
{
CanMessage_t message;
static char tx_buffer[45];
char tx_buffer[45];
#ifdef DEBUG_DUMMY_FRAME
CanMessage_t dummy_frame = {
@@ -177,9 +166,19 @@ void vUARTLogger(void *argument)
for (;;)
{
if (osMessageQueueGet(xUARTQueue, &message, NULL, osWaitForever) == osOK)
{
if (osSemaphoreAcquire(xUARTDMASemaphore, 100U) == osOK)
uint32_t queue_timeout = 2500U;
//#ifdef DEBUG_DUMMY_FRAME
//uint32_t queue_timeout = 100U;
//osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U);
//osDelay(500);
//#else
//uint32_t queue_timeout = osWaitForever;
// #endif
if (osMessageQueueGet(xUARTQueue, &message, NULL, queue_timeout) == osOK)
{
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK)
{
int len = format_can_message(tx_buffer, message.source, &message);
+20 -42
View File
@@ -127,9 +127,9 @@ int main(void)
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_DMA_Init();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_GPIO_Init();
MX_DMA_Init();
MX_CAN1_Init();
MX_CAN2_Init();
MX_SDIO_SD_Init();
@@ -183,9 +183,9 @@ int main(void)
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
xCAN1RxQueue = osMessageQueueNew(2048, sizeof(CanMessage_t), NULL);
xCAN2RxQueue = osMessageQueueNew(2048, sizeof(CanMessage_t), NULL);
xUARTQueue = osMessageQueueNew(4096, sizeof(CanMessage_t), NULL);
xCAN1RxQueue = osMessageQueueNew(64, sizeof(CanMessage_t), NULL);
xCAN2RxQueue = osMessageQueueNew(64, sizeof(CanMessage_t), NULL);
xUARTQueue = osMessageQueueNew(128, sizeof(CanMessage_t), NULL);
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL || xUARTQueue == NULL) Error_Handler();
/* USER CODE END RTOS_QUEUES */
@@ -196,9 +196,6 @@ int main(void)
xUartTask = osThreadNew(vUARTLogger, NULL, &UartLoggerAttributes);
xCAN1LedTask = osThreadNew(vLEDHeartbeat, &led_can1, &LEDHeartbeatCAN1Attributes);
xCAN2LedTask = osThreadNew(vLEDHeartbeat, &led_can2, &LEDHeartbeatCAN2Attributes);
if (xCAN1rxTask == NULL || xCAN2rxTask == NULL || xUartTask == NULL ||
xCAN1LedTask == NULL || xCAN2LedTask == NULL) Error_Handler();
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */
@@ -285,7 +282,7 @@ static void MX_CAN1_Init(void)
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 4;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.Mode = CAN_MODE_SILENT;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_16TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_4TQ;
@@ -322,7 +319,7 @@ static void MX_CAN2_Init(void)
/* USER CODE END CAN2_Init 1 */
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 8;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.Mode = CAN_MODE_SILENT;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_16TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_4TQ;
@@ -394,7 +391,7 @@ static void MX_USART1_UART_Init(void)
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 1000000;
huart1.Init.BaudRate = 1152000;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
@@ -501,22 +498,6 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//*Configure CAN1 pins : PB8, PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//*Configure CAN2 pins : PB12, PB13 */
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
DEBUG_PRINT("[L] GPIO initialized\r\n");
/* USER CODE END MX_GPIO_Init_2 */
@@ -557,28 +538,25 @@ void Error_Handler(void)
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
if (HAL_CAN_GetState(&hcan1) != HAL_CAN_STATE_READY)
{
DEBUG_PRINT("[E] CAN1 error code: 0x%08lX\r\n", (unsigned long)HAL_CAN_GetError(&hcan1));
}
if (HAL_CAN_GetState(&hcan2) != HAL_CAN_STATE_READY)
{
DEBUG_PRINT("[E] CAN2 error code: 0x%08lX\r\n", (unsigned long)HAL_CAN_GetError(&hcan2));
}
// TODO: write error to SD
/* Disabling interrupts here (after reporting above) freezes the FreeRTOS
scheduler for good, since it relies on SysTick/PendSV. */
__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)
{
HAL_GPIO_TogglePin(led_error.port, led_error.pin);
for(volatile uint32_t i = 0; i < 4000000; i++);
HAL_GPIO_TogglePin(led_error.port, led_error.pin);
HAL_Delay(250);
}
/* USER CODE END Error_Handler_Debug */
}
+1 -18
View File
@@ -41,8 +41,7 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -251,20 +250,4 @@ void DMA2_Stream7_IRQHandler(void)
/* USER CODE BEGIN 1 */
/**
* @brief This function handles CAN1 RX0 interrupt.
*/
void CAN1_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
/**
* @brief This function handles CAN2 RX0 interrupt.
*/
void CAN2_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan2);
}
/* USER CODE END 1 */
+4 -4
View File
@@ -1,15 +1,15 @@
# Copyright Notices
# Third-Party Notices
This project is built with open source components and includes third-party software:
## 1. Original Application Code
- **Copyright (c) 2026 Erick Ahmed**
- **License:** [GNU General Public License v3.0 or later](https://git.erickahmed.com/erickahmed/CANdigger/src/branch/main/LICENSE)
- **License:** [GNU General Public License v3.0](https://git.erickahmed.com/erickahmed/bxcan-logger/src/branch/main/LICENSE.txt)
## 2. STM32F4 HAL Drivers
- **Copyright (c) 2016 STMicroelectronics**
- **License:** [BSD 3-Clause License](https://git.erickahmed.com/erickahmed/CANdigger/src/branch/main/Drivers/STM32F4xx_HAL_Driver/LICENSE.txt)
- **License:** [BSD 3-Clause License](https://git.erickahmed.com/erickahmed/bxcan-logger/src/branch/main/Drivers/STM32F4xx_HAL_Driver/LICENSE.txt)
## 3. STM32 CMSIS
- **Copyright (c) 2017 STMicroelectronics**
- **License:** [Apache License 2.0](https://git.erickahmed.com/erickahmed/CANdigger/src/branch/main/Drivers/CMSIS/LICENSE.txt)
- **License:** [Apache License 2.0](https://git.erickahmed.com/erickahmed/bxcan-logger/src/branch/main/Drivers/CMSIS/LICENSE.txt)
+3 -3
View File
@@ -15,7 +15,7 @@
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.ip_address_local" value="localhost"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.limit_swo_clock.enabled" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.limit_swo_clock.value" value=""/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.loadList" value="{&quot;fItems&quot;:[{&quot;fIsFromMainTab&quot;:true,&quot;fPath&quot;:&quot;/Users/erickahmed/TESI/j1939-can-logger_NEW/can-logger/can-logger/Debug/can-logger.elf&quot;,&quot;fProjectName&quot;:&quot;can-logger&quot;,&quot;fPerformBuild&quot;:true,&quot;fDownload&quot;:true,&quot;fLoadSymbols&quot;:true}]}"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.loadList" value="{&quot;fItems&quot;:[{&quot;fIsFromMainTab&quot;:true,&quot;fPath&quot;:&quot;Debug/can-logger.elf&quot;,&quot;fProjectName&quot;:&quot;can-logger&quot;,&quot;fPerformBuild&quot;:true,&quot;fDownload&quot;:true,&quot;fLoadSymbols&quot;:true}]}"/>
<intAttribute key="com.st.stm32cube.ide.mcu.debug.launch.mode" value="0"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.multi_drop_enabled" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.override_start_address_mode" value="default"/>
@@ -74,9 +74,9 @@
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_START_MODE" value="remote"/>
<booleanAttribute key="org.eclipse.cdt.launch.DEBUGGER_STOP_AT_MAIN" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_STOP_AT_MAIN_SYMBOL" value="main"/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="/Users/erickahmed/TESI/j1939-can-logger_NEW/can-logger/can-logger/Debug/can-logger.elf"/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="Debug/can-logger.elf"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="can-logger"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="false"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_ID_ATTR" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.642969664"/>
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">