thin printf, but buggy

calib-gui
Ondřej Hruška 2 years ago
parent f18a7733a1
commit 7e99e2cc6c
  1. 4
      Core/Inc/FreeRTOSConfig.h
  2. 6
      Core/Src/app_heater.c
  3. 10
      Core/Src/app_main.c
  4. 4
      Core/Src/app_pid.c
  5. 3
      Core/Src/app_temp.c
  6. 6
      Core/Src/freertos.c
  7. 5
      Core/Src/main.c
  8. 8
      Core/Src/stm32f1xx_it.c
  9. 25
      Core/Src/usart.c
  10. 1040
      Lib/snprintf/snprintf.c
  11. 39
      Lib/snprintf/snprintf.h
  12. 17
      Makefile
  13. 13
      Middlewares/Third_Party/FreeRTOS/Source/tasks.c

@ -137,8 +137,8 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
/* Normal assert() semantics without relying on the provision of an assert.h /* Normal assert() semantics without relying on the provision of an assert.h
header file. */ header file. */
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
#include <stdio.h> #include "snprintf.h"
#define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); printf("configASSERT "__FILE__":%d\r\n",__LINE__); for( ;; );} #define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); PRINTF("configASSERT "__FILE__":%d\r\n",__LINE__); for( ;; );}
/* USER CODE END 1 */ /* USER CODE END 1 */
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS

@ -66,14 +66,14 @@ void app_heater_set_tuning(float p, float i, float d) {
} }
void app_heater_enable(bool enable) { void app_heater_enable(bool enable) {
printf("Set heater enabled = %d\r\n", (int) enable); PRINTF("Set heater enabled = %d\r\n", (int) enable);
heaterEnterCritical(); heaterEnterCritical();
PID_SetCtlMode(&state.pid, enable ? PID_AUTOMATIC : PID_MANUAL); PID_SetCtlMode(&state.pid, enable ? PID_AUTOMATIC : PID_MANUAL);
heaterExitCritical(); heaterExitCritical();
} }
void app_heater_set_target(float target) { void app_heater_set_target(float target) {
printf("Set heater target = %d\r\n", (int) target); PRINTF("Set heater target = %d\r\n", (int) target);
heaterEnterCritical(); heaterEnterCritical();
PID_SetSetpoint(&state.pid, target); PID_SetSetpoint(&state.pid, target);
heaterExitCritical(); heaterExitCritical();
@ -99,7 +99,7 @@ void app_task_heater(void *argument)
heaterEnterCritical(); heaterEnterCritical();
PID_Compute(&state.pid, state.oven_temp); PID_Compute(&state.pid, state.oven_temp);
if (state.pid.ctlMode == PID_AUTOMATIC) { if (state.pid.ctlMode == PID_AUTOMATIC) {
printf("temp %d, output %d\r\n", (int) state.oven_temp, (int) state.pid.Output); PRINTF("temp %d, output %d\r\n", (int) state.oven_temp, (int) state.pid.Output);
heater_pwm_set_perc(state.pid.Output); heater_pwm_set_perc(state.pid.Output);
} else { } else {
// turn it off // turn it off

@ -33,13 +33,13 @@ static void redraw_display() {
char tmp[100]; char tmp[100];
sprintf(tmp, "T=%d°C", (int) s_app.oven_temp); PRINTF(tmp, "T=%d°C", (int) s_app.oven_temp);
fb_text(3, 3, tmp, FONT_5X7, 1); fb_text(3, 3, tmp, FONT_5X7, 1);
sprintf(tmp, "Cil=%d°C", s_app.set_temp); PRINTF(tmp, "Cil=%d°C", s_app.set_temp);
fb_text(3, 11, tmp, FONT_5X7, 1); fb_text(3, 11, tmp, FONT_5X7, 1);
sprintf(tmp, "Stav=%s", s_app.run ? "ZAP" : "VYP"); PRINTF(tmp, "Stav=%s", s_app.run ? "ZAP" : "VYP");
fb_text(3, 19, tmp, FONT_5X7, 1); fb_text(3, 19, tmp, FONT_5X7, 1);
if (s_app.run) { if (s_app.run) {
@ -83,7 +83,7 @@ static void redraw_display() {
void app_task_main(void *argument) void app_task_main(void *argument)
{ {
printf("Main task\r\n"); PUTS("Main task\r\n");
app_analog_init(); app_analog_init();
app_buzzer_init(); app_buzzer_init();
app_knob_init(); app_knob_init();
@ -98,7 +98,7 @@ void app_task_main(void *argument)
bool any_change = true; bool any_change = true;
uint32_t last_redraw = osKernelGetTickCount(); uint32_t last_redraw = osKernelGetTickCount();
printf("Loop\r\n"); PUTS("Loop\r\n");
for (;;) { for (;;) {
// sampling is done in the heater loop // sampling is done in the heater loop

@ -25,7 +25,7 @@ void PID_Compute(struct PID *self, float input)
uint32_t now = xTaskGetTickCount(); uint32_t now = xTaskGetTickCount();
int32_t timeChange = (now - self->lastTime); int32_t timeChange = (now - self->lastTime);
if (timeChange >= self->SampleTimeTicks) { if (timeChange >= self->SampleTimeTicks) {
printf("compute\r\n"); PUTS("compute\r\n");
/*Compute all the working error variables*/ /*Compute all the working error variables*/
float error = self->Setpoint - input; float error = self->Setpoint - input;
self->ITerm += (self->ki * error); self->ITerm += (self->ki * error);
@ -35,7 +35,7 @@ void PID_Compute(struct PID *self, float input)
float dInput = (input - self->lastInput); float dInput = (input - self->lastInput);
printf("calc x100 %d + %d - %d\r\n", PRINTF("calc x100 %d + %d - %d\r\n",
(int) (100 * (self->kp * error)), (int) (100 * (self->kp * error)),
(int) (100 * (self->ITerm)), (int) (100 * (self->ITerm)),
(int) (100 * (self->kd * dInput)) (int) (100 * (self->kd * dInput))

@ -9,6 +9,7 @@
#include <stdio.h> #include <stdio.h>
#include "app_temp.h" #include "app_temp.h"
#include "adc.h" #include "adc.h"
#include "snprintf.h"
/* DMA dest */ /* DMA dest */
static volatile uint16_t adc_values[4]; static volatile uint16_t adc_values[4];
@ -247,5 +248,5 @@ void app_temp_adc_eos()
void app_temp_show_buf() void app_temp_show_buf()
{ {
printf("%d,%d,%d,%d\r\n", adc_values[0], adc_values[1], adc_values[2], adc_values[3]); PRINTF("%d,%d,%d,%d\r\n", adc_values[0], adc_values[1], adc_values[2], adc_values[3]);
} }

@ -25,7 +25,7 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include <stdio.h> #include "snprintf.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -135,7 +135,7 @@ void vApplicationMallocFailedHook(void);
/* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName)
{ {
printf("vApplicationStackOverflowHook: %s\r\n", pcTaskName); PRINTF("vApplicationStackOverflowHook: %s\r\n", pcTaskName);
/* Run time stack overflow checking is performed if /* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
called if a stack overflow is detected. */ called if a stack overflow is detected. */
@ -145,7 +145,7 @@ void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName)
/* USER CODE BEGIN 5 */ /* USER CODE BEGIN 5 */
void vApplicationMallocFailedHook(void) void vApplicationMallocFailedHook(void)
{ {
printf("vApplicationMallocFailedHook\r\n"); PUTS("vApplicationMallocFailedHook\r\n");
/* vApplicationMallocFailedHook() will only be called if /* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails. function that will get called if a call to pvPortMalloc() fails.

@ -120,7 +120,7 @@ int main(void)
MX_SPI2_Init(); MX_SPI2_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
printf("Start.\r\n"); PUTS("Start.\r\n");
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Init scheduler */ /* Init scheduler */
@ -203,7 +203,7 @@ 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();
printf("Error_Handler\r\n"); PUTS("Error_Handler\r\n");
while (1) while (1)
{ {
} }
@ -221,6 +221,7 @@ void Error_Handler(void)
void assert_failed(uint8_t *file, uint32_t line) void assert_failed(uint8_t *file, uint32_t line)
{ {
/* USER CODE BEGIN 6 */ /* USER CODE BEGIN 6 */
PRINTF("assert_failed %s:%d", (const char *) file, (int) line);
/* User can add his own implementation to report the file name and line number, /* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */ /* USER CODE END 6 */

@ -88,7 +88,7 @@ void NMI_Handler(void)
void HardFault_Handler(void) void HardFault_Handler(void)
{ {
/* USER CODE BEGIN HardFault_IRQn 0 */ /* USER CODE BEGIN HardFault_IRQn 0 */
printf("HardFault_Handler\r\n"); PUTS("HardFault_Handler\r\n");
/* USER CODE END HardFault_IRQn 0 */ /* USER CODE END HardFault_IRQn 0 */
while (1) while (1)
@ -104,7 +104,7 @@ void HardFault_Handler(void)
void MemManage_Handler(void) void MemManage_Handler(void)
{ {
/* USER CODE BEGIN MemoryManagement_IRQn 0 */ /* USER CODE BEGIN MemoryManagement_IRQn 0 */
printf("MemManage_Handler\r\n"); PUTS("MemManage_Handler\r\n");
/* USER CODE END MemoryManagement_IRQn 0 */ /* USER CODE END MemoryManagement_IRQn 0 */
while (1) while (1)
@ -120,7 +120,7 @@ void MemManage_Handler(void)
void BusFault_Handler(void) void BusFault_Handler(void)
{ {
/* USER CODE BEGIN BusFault_IRQn 0 */ /* USER CODE BEGIN BusFault_IRQn 0 */
printf("BusFault_Handler\r\n"); PUTS("BusFault_Handler\r\n");
/* USER CODE END BusFault_IRQn 0 */ /* USER CODE END BusFault_IRQn 0 */
while (1) while (1)
@ -136,7 +136,7 @@ void BusFault_Handler(void)
void UsageFault_Handler(void) void UsageFault_Handler(void)
{ {
/* USER CODE BEGIN UsageFault_IRQn 0 */ /* USER CODE BEGIN UsageFault_IRQn 0 */
printf("UsageFault_Handler\r\n"); PUTS("UsageFault_Handler\r\n");
/* USER CODE END UsageFault_IRQn 0 */ /* USER CODE END UsageFault_IRQn 0 */
while (1) while (1)

@ -21,6 +21,8 @@
#include "usart.h" #include "usart.h"
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
#include <stddef.h>
#include <string.h>
#include "main.h" #include "main.h"
/* USER CODE END 0 */ /* USER CODE END 0 */
@ -75,17 +77,30 @@ void MX_USART1_UART_Init(void)
} }
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
void stdout_write(const char *pcBuffer, const size_t iLength) {
int cnt = (int) iLength;
while (cnt-- > 0) {
while (!LL_USART_IsActiveFlag_TXE(USART_DEBUG)) {}
LL_USART_TransmitData8(USART_DEBUG, *pcBuffer++);
}
}
void stdout_puts(const char *pcBuffer) {
stdout_write(pcBuffer, strlen(pcBuffer));
}
void stdout_putchar(char c) {
while (!LL_USART_IsActiveFlag_TXE(USART_DEBUG)) {}
LL_USART_TransmitData8(USART_DEBUG, c);
}
/** /**
* @brief Retargets the C library printf function to the USART. * @brief Retargets the C library printf function to the USART.
* @param None * @param None
* @retval None * @retval None
*/ */
int _write(int fd, const char *pcBuffer, const int iLength) { int _write(int fd, const char *pcBuffer, const int iLength) {
int cnt = iLength; stdout_write(pcBuffer, iLength);
while (cnt-- > 0) {
while (!LL_USART_IsActiveFlag_TXE(USART_DEBUG)) {}
LL_USART_TransmitData8(USART_DEBUG, *pcBuffer++);
}
return iLength; return iLength;
} }

File diff suppressed because it is too large Load Diff

@ -0,0 +1,39 @@
//
// Created by MightyPork on 2017/11/09.
//
// Small sprintf/snprintf implementation, used instead of the newlib one.
//
#ifndef GEX_SNPRINTF_H
#define GEX_SNPRINTF_H
#include <stdarg.h>
#include <limits.h>
int fixup_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
int fixup_snprintf(char *str, size_t count,const char *fmt,...);
int fixup_vasprintf(char **ptr, const char *format, va_list ap);
int fixup_asprintf(char **ptr, const char *format, ...);
int fixup_sprintf(char *ptr, const char *format, ...);
int fixup_printf(const char *format, ...);
int fixup_vprintf(const char *format, va_list ap);
#define VSNPRINTF(...) fixup_vsnprintf(__VA_ARGS__)
#define SNPRINTF(...) fixup_snprintf(__VA_ARGS__)
#define VASPRINTF(...) fixup_vasprintf(__VA_ARGS__)
#define ASPRINTF(...) fixup_asprintf(__VA_ARGS__)
#define SPRINTF(...) fixup_sprintf(__VA_ARGS__)
#define PRINTF(...) fixup_printf(__VA_ARGS__)
#define VPRINTF(...) fixup_vprintf(__VA_ARGS__)
// extern
extern void stdout_puts(const char *s);
extern void stdout_putchar(char c);
extern void stdout_write(const char *s, size_t len);
#define PUTS(s) stdout_puts((s))
#define PUTCHAR(c) stdout_putchar((c))
#define WRITE(s, n) stdout_write((s), (n))
#endif //GEX_SNPRINTF_H

@ -1,5 +1,5 @@
########################################################################################################################## ##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Sun Mar 12 14:42:27 CET 2023] # File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Sun Mar 12 14:42:27 CET 2023]
########################################################################################################################## ##########################################################################################################################
# ------------------------------------------------ # ------------------------------------------------
@ -64,7 +64,12 @@ Middlewares/Third_Party/FreeRTOS/Source/timers.c \
Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.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/MemMang/heap_4.c \
Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3/port.c \ Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3/port.c \
Lib/ufb/Src/framebuffer.c Lib/ufb/Src/utf8.c Lib/ufb/Src/font.c Lib/ufb/Src/fb_7seg.c Lib/ufb/Src/fb_text.c \ Lib/ufb/Src/framebuffer.c \
Lib/ufb/Src/utf8.c \
Lib/ufb/Src/font.c \
Lib/ufb/Src/fb_7seg.c \
Lib/ufb/Src/fb_text.c \
Lib/snprintf/snprintf.c \
Core/Src/dma.c \ Core/Src/dma.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_gpio.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_gpio.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_adc.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_adc.c \
@ -149,7 +154,8 @@ C_INCLUDES = \
-IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3 \ -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3 \
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ -IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
-IDrivers/CMSIS/Include \ -IDrivers/CMSIS/Include \
-ILib/ufb/Inc -ILib/ufb/Inc \
-ILib/snprintf
# compile gcc flags # compile gcc flags
@ -211,7 +217,7 @@ $(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BUILD_DIR): $(BUILD_DIR):
mkdir -p $@ mkdir -p $@
.PHONY: all build flash clean flash flash-stlink analyze .PHONY: all flash clean flash-stlink analyze size
####################################### #######################################
# clean up # clean up
@ -228,6 +234,9 @@ clean:
build: all build: all
size: $(BUILD_DIR)/$(TARGET).elf
$(SZ) $<
flash-stlink: $(BUILD_DIR)/$(TARGET).bin flash-stlink: $(BUILD_DIR)/$(TARGET).bin
st-flash write $< 0x8000000 st-flash write $< 0x8000000

@ -53,7 +53,8 @@ functions but without including stdio.h here. */
to generate human readable text from the raw data generated by the to generate human readable text from the raw data generated by the
uxTaskGetSystemState() function. Note the formatting functions are provided uxTaskGetSystemState() function. Note the formatting functions are provided
for convenience only, and are NOT considered part of the kernel. */ for convenience only, and are NOT considered part of the kernel. */
#include <stdio.h> //#include <stdio.h>
#include "snprintf.h"
#endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */ #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
#if( configUSE_PREEMPTION == 0 ) #if( configUSE_PREEMPTION == 0 )
@ -4229,7 +4230,7 @@ TCB_t *pxTCB;
pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName ); pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
/* Write the rest of the string. */ /* Write the rest of the string. */
sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); SPRINTF( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
pcWriteBuffer += strlen( pcWriteBuffer ); pcWriteBuffer += strlen( pcWriteBuffer );
} }
@ -4325,13 +4326,13 @@ TCB_t *pxTCB;
{ {
#ifdef portLU_PRINTF_SPECIFIER_REQUIRED #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
{ {
sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage ); SPRINTF( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
} }
#else #else
{ {
/* sizeof( int ) == sizeof( long ) so a smaller /* sizeof( int ) == sizeof( long ) so a smaller
printf() library can be used. */ printf() library can be used. */
sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); SPRINTF( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
} }
#endif #endif
} }
@ -4341,13 +4342,13 @@ TCB_t *pxTCB;
consumed less than 1% of the total run time. */ consumed less than 1% of the total run time. */
#ifdef portLU_PRINTF_SPECIFIER_REQUIRED #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
{ {
sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter ); SPRINTF( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
} }
#else #else
{ {
/* sizeof( int ) == sizeof( long ) so a smaller /* sizeof( int ) == sizeof( long ) so a smaller
printf() library can be used. */ printf() library can be used. */
sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); SPRINTF( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
} }
#endif #endif
} }

Loading…
Cancel
Save