Skip to content

Commit

Permalink
#25 - add serial monitor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RChandler234 authored and RChandler234 committed Mar 25, 2024
1 parent c537e5e commit 7d680d6
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 74 deletions.
15 changes: 15 additions & 0 deletions CM4/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
145 changes: 71 additions & 74 deletions CM4/Core/Src/main.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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.
*
******************************************************************************
*/
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "main.h"
#include "cmsis_os.h"
#include <serial_monitor.h>

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
Expand Down Expand Up @@ -59,9 +60,9 @@ PCD_HandleTypeDef hpcd_USB_OTG_FS;
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityNormal,
};
/* USER CODE BEGIN PV */

Expand All @@ -86,16 +87,16 @@ void StartDefaultTask(void *argument);
/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* USER CODE BEGIN Boot_Mode_Sequence_1 */
/* USER CODE BEGIN Boot_Mode_Sequence_1 */
/*HW semaphore Clock enable*/
__HAL_RCC_HSEM_CLK_ENABLE();
/* Activate HSEM notification for Cortex-M4*/
Expand All @@ -109,7 +110,7 @@ int main(void)
/* Clear HSEM flag */
__HAL_HSEM_CLEAR_FLAG(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID_0));

/* USER CODE END Boot_Mode_Sequence_1 */
/* USER CODE END Boot_Mode_Sequence_1 */
/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
Expand Down Expand Up @@ -157,6 +158,8 @@ int main(void)
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);

/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
Expand All @@ -181,10 +184,10 @@ int main(void)
}

/**
* @brief CRC Initialization Function
* @param None
* @retval None
*/
* @brief CRC Initialization Function
* @param None
* @retval None
*/
void MX_CRC_Init(void)
{

Expand All @@ -208,14 +211,13 @@ void MX_CRC_Init(void)
/* USER CODE BEGIN CRC_Init 2 */

/* USER CODE END CRC_Init 2 */

}

/**
* @brief FDCAN1 Initialization Function
* @param None
* @retval None
*/
* @brief FDCAN1 Initialization Function
* @param None
* @retval None
*/
static void MX_FDCAN1_Init(void)
{

Expand Down Expand Up @@ -261,14 +263,13 @@ static void MX_FDCAN1_Init(void)
/* USER CODE BEGIN FDCAN1_Init 2 */

/* USER CODE END FDCAN1_Init 2 */

}

/**
* @brief QUADSPI Initialization Function
* @param None
* @retval None
*/
* @brief QUADSPI Initialization Function
* @param None
* @retval None
*/
static void MX_QUADSPI_Init(void)
{

Expand Down Expand Up @@ -296,14 +297,13 @@ static void MX_QUADSPI_Init(void)
/* USER CODE BEGIN QUADSPI_Init 2 */

/* USER CODE END QUADSPI_Init 2 */

}

/**
* @brief UART4 Initialization Function
* @param None
* @retval None
*/
* @brief UART4 Initialization Function
* @param None
* @retval None
*/
static void MX_UART4_Init(void)
{

Expand Down Expand Up @@ -344,14 +344,13 @@ static void MX_UART4_Init(void)
/* USER CODE BEGIN UART4_Init 2 */

/* USER CODE END UART4_Init 2 */

}

/**
* @brief USB_OTG_FS Initialization Function
* @param None
* @retval None
*/
* @brief USB_OTG_FS Initialization Function
* @param None
* @retval None
*/
static void MX_USB_OTG_FS_PCD_Init(void)
{

Expand Down Expand Up @@ -380,29 +379,27 @@ static void MX_USB_OTG_FS_PCD_Init(void)
/* USER CODE BEGIN USB_OTG_FS_Init 2 */

/* USER CODE END USB_OTG_FS_Init 2 */

}

/**
* Enable DMA controller clock
*/
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{

/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();

}

/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
Expand All @@ -413,8 +410,8 @@ static void MX_GPIO_Init(void)
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
Expand All @@ -423,26 +420,26 @@ static void MX_GPIO_Init(void)

/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
for (;;)
{
osDelay(1);
}
/* USER CODE END 5 */
}

/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
Expand All @@ -454,14 +451,14 @@ void Error_Handler(void)
/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
Expand Down
69 changes: 69 additions & 0 deletions CM4/Core/Src/serial_monitor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#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);
}
}
}
1 change: 1 addition & 0 deletions Makefile/CM4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ C_SOURCES = \
../../CM4/Core/Src/freertos.c \
../../CM4/Core/Src/stm32h7xx_it.c \
../../CM4/Core/Src/stm32h7xx_hal_msp.c \
../../CM4/Core/Src/serial_monitor.c \
../../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cortex.c \
../../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c \
../../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c \
Expand Down

0 comments on commit 7d680d6

Please sign in to comment.