10 Commits

3 changed files with 42 additions and 60 deletions
+1 -1
View File
@@ -129,7 +129,7 @@ void vCANListener(void *argument)
osMessageQueuePut(xUARTQueue, &message, 0U, 0U); osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
} }
else DEBUG_PRINT("No CAN yet!\r\n"); else DEBUG_PRINT("LALALLALA CIAOOOOOO c:\r\n");
} }
} }
/* END vCANListener */ /* END vCANListener */
+10 -38
View File
@@ -92,7 +92,6 @@ static int format_can_message(char *buf, uint8_t source, const CanMessage_t *mes
*/ */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{ {
if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
} }
/* END HAL_UART_TxCpltCallback */ /* END HAL_UART_TxCpltCallback */
@@ -138,46 +137,19 @@ void vUARTLogger(void *argument)
HAL_UART_Init(&huart1); HAL_UART_Init(&huart1);
} }
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK) osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever);
{
int len = format_can_message(tx_buffer, message.source, &message); int len = format_can_message(tx_buffer, message.source, &message);
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, len) != HAL_OK) HAL_UART_Transmit(&huart1, (uint8_t*)tx_buffer, len, 100);
{
osSemaphoreRelease(xUARTDMASemaphore); osSemaphoreRelease(xUARTDMASemaphore);
DEBUG_PRINT("HAL error, CAN frame NOT sent!\r\n");
}
}
else
{
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n");
HAL_UART_Abort(&huart1);
HAL_UART_Init(&huart1);
osSemaphoreRelease(xUARTDMASemaphore);
}
} }
else else
{ {
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK) DEBUG_PRINT("[D] ISR1:%lu ISR2:%lu | ST1:%lu ST2:%lu | ER1:0x%lX ER2:0x%lX\r\n",
{ (unsigned long)can1_rx_isr_count, (unsigned long)can2_rx_isr_count,
int len = snprintf(diag_buf, sizeof(diag_buf), (unsigned long)HAL_CAN_GetState(&hcan1), (unsigned long)HAL_CAN_GetState(&hcan2),
"[D] ISR1:%lu ISR2:%lu | ST1:%lu ST2:%lu | ER1:0x%lX ER2:0x%lX\r\n", (unsigned long)HAL_CAN_GetError(&hcan1), (unsigned long)HAL_CAN_GetError(&hcan2));
(unsigned long)can1_rx_isr_count, (unsigned long)can2_rx_isr_count,
(unsigned long)HAL_CAN_GetState(&hcan1), (unsigned long)HAL_CAN_GetState(&hcan2),
(unsigned long)HAL_CAN_GetError(&hcan1), (unsigned long)HAL_CAN_GetError(&hcan2));
if (len > 0)
{
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)diag_buf, len) != HAL_OK) osSemaphoreRelease(xUARTDMASemaphore);
}
else osSemaphoreRelease(xUARTDMASemaphore);
}
else
{
HAL_UART_Abort(&huart1);
HAL_UART_Init(&huart1);
osSemaphoreRelease(xUARTDMASemaphore);
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n");
}
} }
} }
} }
+31 -21
View File
@@ -55,6 +55,8 @@ LED_Config led_can1 = {GPIOB, GPIO_PIN_5};
LED_Config led_can2 = {GPIOB, GPIO_PIN_6}; LED_Config led_can2 = {GPIOB, GPIO_PIN_6};
LED_Config led_error = {GPIOB, GPIO_PIN_7}; LED_Config led_error = {GPIOB, GPIO_PIN_7};
osMutexId_t debugPrintMutex;
osThreadId_t xCAN1rxTask; osThreadId_t xCAN1rxTask;
osThreadId_t xCAN2rxTask; osThreadId_t xCAN2rxTask;
osThreadId_t xCAN1LedTask; osThreadId_t xCAN1LedTask;
@@ -81,16 +83,25 @@ static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PF */ /* USER CODE BEGIN PF */
int _write(int file, char *ptr, int len) int _write(int file, char *ptr, int len)
{ {
for (int i = 0; i < len; i++) ITM_SendChar((*ptr++)); if (osKernelGetState() == osKernelRunning) osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever);
HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, 100);
if (osKernelGetState() == osKernelRunning) osSemaphoreRelease(xUARTDMASemaphore);
return len; return len;
} }
#ifndef DEBUG_ITM #ifndef DEBUG_ITW
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
extern osSemaphoreId_t xUARTDMASemaphore;
void uart_debug_print(const char *fmt, ...) void uart_debug_print(const char *fmt, ...)
{ {
if (huart1.gState == HAL_UART_STATE_RESET) return;
if (osKernelGetState() == osKernelRunning) osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever);
char buf[128]; char buf[128];
va_list args; va_list args;
@@ -98,11 +109,9 @@ void uart_debug_print(const char *fmt, ...)
int len = vsnprintf(buf, sizeof(buf), fmt, args); int len = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args); va_end(args);
if (len > 0) if (len > 0) HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, 100);
{
while (HAL_UART_GetState(&huart1) & HAL_UART_STATE_BUSY_TX); if (osKernelGetState() == osKernelRunning) osSemaphoreRelease(xUARTDMASemaphore);
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, 100);
}
} }
#endif #endif
/* USER CODE END PF */ /* USER CODE END PF */
@@ -127,16 +136,15 @@ int main(void)
ITM->TCR = ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk | ITM_TCR_SWOENA_Msk; ITM->TCR = ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk | ITM_TCR_SWOENA_Msk;
} }
DEBUG_PRINT("Booting system\r\n"); DEBUG_PRINT("[L] Booting system\r\n");
/* USER CODE END 1 */ /* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/ /* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init(); HAL_Init();
/* USER CODE BEGIN Init */ /* USER CODE BEGIN Init */
DEBUG_PRINT("Configuring system clock\r\n"); DEBUG_PRINT("[L] Configuring system clock\r\n");
/* USER CODE END Init */ /* USER CODE END Init */
@@ -144,7 +152,7 @@ int main(void)
SystemClock_Config(); SystemClock_Config();
/* USER CODE BEGIN SysInit */ /* USER CODE BEGIN SysInit */
DEBUG_PRINT("Initializing configured peripherals...\r\n"); DEBUG_PRINT("[L] Initializing configured peripherals...\r\n");
/* USER CODE END SysInit */ /* USER CODE END SysInit */
/* Initialize all configured peripherals */ /* Initialize all configured peripherals */
@@ -155,14 +163,14 @@ int main(void)
MX_SDIO_SD_Init(); MX_SDIO_SD_Init();
MX_USART1_UART_Init(); MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
DEBUG_PRINT("Initializing CAN bus logger\r\n"); DEBUG_PRINT("[L] Initializing CAN bus logger\r\n");
CAN_Logger_Init(&hcan1, &hcan2); CAN_Logger_Init(&hcan1, &hcan2);
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Init scheduler */ /* Init scheduler */
osKernelInitialize(); osKernelInitialize();
DEBUG_PRINT("Creating RTOS entities\r\n"); DEBUG_PRINT("[L] Creating RTOS entities\r\n");
/* USER CODE BEGIN RTOS_TASKS */ /* USER CODE BEGIN RTOS_TASKS */
const osThreadAttr_t CAN1rxAttributes = { const osThreadAttr_t CAN1rxAttributes = {
@@ -193,6 +201,8 @@ int main(void)
/* USER CODE END RTOS_TASKS */ /* USER CODE END RTOS_TASKS */
/* USER CODE BEGIN RTOS_MUTEX */ /* USER CODE BEGIN RTOS_MUTEX */
debugPrintMutex = osMutexNew(NULL);
if (debugPrintMutex == NULL) Error_Handler();
/* USER CODE END RTOS_MUTEX */ /* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_SEMAPHORES */
@@ -223,7 +233,7 @@ int main(void)
/* USER CODE END RTOS_EVENTS */ /* USER CODE END RTOS_EVENTS */
/* Start scheduler */ /* Start scheduler */
DEBUG_PRINT("Starting RTOS init scheduler\r\n"); DEBUG_PRINT("[L] Starting RTOS init scheduler\r\n");
osKernelStart(); osKernelStart();
/* We should never get here as control is now taken by the scheduler */ /* We should never get here as control is now taken by the scheduler */
@@ -235,7 +245,7 @@ int main(void)
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
DEBUG_PRINT("ERROR: RTOS scheduler crashed!\r\n"); DEBUG_PRINT("[E] RTOS scheduler crashed!\r\n");
/* USER CODE END 3 */ /* USER CODE END 3 */
} }
} }
@@ -318,7 +328,7 @@ static void MX_CAN1_Init(void)
Error_Handler(); Error_Handler();
} }
/* USER CODE BEGIN CAN1_Init 2 */ /* USER CODE BEGIN CAN1_Init 2 */
DEBUG_PRINT("CAN1 initialized!\r\n"); DEBUG_PRINT("[L] CAN1 initialized\r\n");
/* USER CODE END CAN1_Init 2 */ /* USER CODE END CAN1_Init 2 */
} }
@@ -355,7 +365,7 @@ static void MX_CAN2_Init(void)
Error_Handler(); Error_Handler();
} }
/* USER CODE BEGIN CAN2_Init 2 */ /* USER CODE BEGIN CAN2_Init 2 */
DEBUG_PRINT("CAN2 initialized!\r\n"); DEBUG_PRINT("[L] CAN2 initialized\r\n");
/* USER CODE END CAN2_Init 2 */ /* USER CODE END CAN2_Init 2 */
} }
@@ -424,7 +434,7 @@ static void MX_USART1_UART_Init(void)
Error_Handler(); Error_Handler();
} }
/* USER CODE BEGIN USART1_Init 2 */ /* USER CODE BEGIN USART1_Init 2 */
DEBUG_PRINT("USART1 initialized!\r\n"); DEBUG_PRINT("[L] USART1 initialized\r\n");
/* USER CODE END USART1_Init 2 */ /* USER CODE END USART1_Init 2 */
} }
@@ -450,7 +460,7 @@ static void MX_DMA_Init(void)
HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn); HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
/* USER CODE BEGIN MX_DMA_Init 1 */ /* USER CODE BEGIN MX_DMA_Init 1 */
DEBUG_PRINT("DMA initialized!\r\n"); DEBUG_PRINT("[L] DMA initialized\r\n");
/* USER CODE END MX_DMA_Init 1 */ /* USER CODE END MX_DMA_Init 1 */
} }
@@ -520,7 +530,7 @@ static void MX_GPIO_Init(void)
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE BEGIN MX_GPIO_Init_2 */
DEBUG_PRINT("GPIO initialized!\r\n"); DEBUG_PRINT("[L] GPIO initialized\r\n");
/* USER CODE END MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */
} }
@@ -555,7 +565,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
*/ */
void Error_Handler(void) void Error_Handler(void)
{ {
DEBUG_PRINT("ERROR: entering error handler\r\n"); DEBUG_PRINT("[E] Eentering error handler\r\n");
/* 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 */