implemented printf logging

master
Ondřej Hruška 8 years ago
parent 3fd42eb41a
commit 7e539708b9
  1. 10
      Inc/utils.h
  2. 11
      Src/stm32f1xx_it.c
  3. 19
      Src/utils.c
  4. 119
      User/debug.c
  5. 95
      User/debug.h
  6. 1
      User/dotmatrix.c
  7. 63
      User/syscalls.c
  8. 4
      User/timebase.c
  9. 26
      User/user_main.c

@ -1,10 +0,0 @@
//
// Created by MightyPork on 2.9.16.
//
#ifndef mpork_utils
#define mpork_utils
void uart_print(const char *string);
#endif

@ -36,8 +36,7 @@
#include "stm32f1xx_it.h" #include "stm32f1xx_it.h"
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
#include "utils.h" #include "debug.h"
/* USER CODE END 0 */ /* USER CODE END 0 */
/* External variables --------------------------------------------------------*/ /* External variables --------------------------------------------------------*/
@ -94,6 +93,8 @@ prvGetRegistersFromStack(uint32_t *pulFaultStackAddress)
pc = pulFaultStackAddress[ 6 ]; pc = pulFaultStackAddress[ 6 ];
psr = pulFaultStackAddress[ 7 ]; psr = pulFaultStackAddress[ 7 ];
error("Hard fault.");
/* When the following line is hit, the variables contain the register values. */ /* When the following line is hit, the variables contain the register values. */
for (;;); for (;;);
} }
@ -131,7 +132,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 */
uart_print("MemManage fault.\n"); error("MemManage fault.\n");
/* USER CODE END MemoryManagement_IRQn 0 */ /* USER CODE END MemoryManagement_IRQn 0 */
while (1) while (1)
@ -148,7 +149,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 */
uart_print("Bus fault.\n"); error("Bus fault.\n");
/* USER CODE END BusFault_IRQn 0 */ /* USER CODE END BusFault_IRQn 0 */
while (1) while (1)
@ -165,7 +166,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 */
uart_print("Usage fault.\n"); error("Usage fault.\n");
/* USER CODE END UsageFault_IRQn 0 */ /* USER CODE END UsageFault_IRQn 0 */
while (1) while (1)

@ -1,19 +0,0 @@
//
// Created by MightyPork on 2.9.16.
//
#include <string.h>
#include "stm32f1xx_hal_conf.h"
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "utils.h"
void uart_print(const char *string)
{
HAL_UART_Transmit(&huart1, (char*)string, (uint16_t) strlen(string), 10);
// Stop ADC and the timer
HAL_ADC_Stop(&hadc1);
HAL_TIM_Base_Stop(&htim3);
}

@ -0,0 +1,119 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include "debug.h"
#include "timebase.h"
void dbg_printf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
va_end(va);
}
void dbg_va_base(const char *fmt, const char *tag, va_list va)
{
ms_time_t now = ms_now();
uint32_t secs = now / 1000;
uint32_t ms = now % 1000;
printf("%4"PRIu32".%03"PRIu32" ", secs, ms);
dbg_raw(tag);
vprintf(fmt, va);
dbg_raw(DEBUG_EOL);
}
/** Print a log message with a DEBUG tag and newline */
void dbg(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
dbg_va_base(fmt, DEBUG_TAG_BASE, va);
va_end(va);
}
/** Print a log message with an INFO tag and newline */
void info(const char *fmt, ...)
{
v100_attr(FMT_WHITE);
va_list va;
va_start(va, fmt);
dbg_va_base(fmt, DEBUG_TAG_INFO, va);
va_end(va);
v100_attr(FMT_RESET);
}
/** Print a log message with an INFO tag and newline */
void banner(const char *fmt, ...)
{
v100_attr(FMT_GREEN, FMT_BRIGHT);
va_list va;
va_start(va, fmt);
dbg_va_base(fmt, DEBUG_TAG_INFO, va);
va_end(va);
v100_attr(FMT_RESET);
}
/** Print a log message with a warning tag and newline */
void warn(const char *fmt, ...)
{
v100_attr(FMT_YELLOW, FMT_BRIGHT);
va_list va;
va_start(va, fmt);
dbg_va_base(fmt, DEBUG_TAG_WARN, va);
va_end(va);
v100_attr(FMT_RESET);
}
/** Print a log message with an ERROR tag and newline */
void error(const char *fmt, ...)
{
v100_attr(FMT_RED, FMT_BRIGHT);
va_list va;
va_start(va, fmt);
dbg_va_base(fmt, DEBUG_TAG_ERROR, va);
va_end(va);
v100_attr(FMT_RESET);
}
void v100_attr_(uint8_t count, ...)
{
va_list va;
va_start(va, count);
putchar(27);
putchar('[');
for (int i = 0; i < count; i++) {
int attr = va_arg(va, int);
// comma
if (i > 0) putchar(';');
// number
printf("%d", attr);
}
putchar('m');
va_end(va);
}

@ -0,0 +1,95 @@
#pragma once
#include <stdlib.h>
#include <stdarg.h>
// helper to mark printf functions
#define PRINTF_LIKE __attribute__((format(printf, 1, 2)))
// formatting symbols
#define DEBUG_EOL "\r\n"
#define DEBUG_TAG_WARN "[W] "
#define DEBUG_TAG_ERROR "[E] "
#define DEBUG_TAG_BASE "[ ] "
#define DEBUG_TAG_INFO "[i] "
/** Print a log message with no tag and no newline */
void dbg_printf(const char *fmt, ...) PRINTF_LIKE;
/** Print via va_list */
void dbg_va_base(const char *fmt, const char *tag, va_list va);
/** Print a string to the debug interface (length not limited) */
static inline void dbg_raw(const char *str)
{
fputs(str, stdout);
}
/** Print a char to the debug interface */
static inline void dbg_raw_c(char c)
{
putchar((uint8_t) c);
}
/** Print a log message with a "debug" tag and newline */
void dbg(const char *fmt, ...) PRINTF_LIKE;
/** Print a log message with an "info" tag and newline */
void info(const char *fmt, ...) PRINTF_LIKE;
/** Print a log message with a "banner" tag and newline */
void banner(const char *fmt, ...) PRINTF_LIKE;
/** Print a log message with a "warning" tag and newline */
void warn(const char *fmt, ...) PRINTF_LIKE;
/** Print a log message with an "error" tag and newline */
void error(const char *fmt, ...) PRINTF_LIKE;
/** ANSI formatting attributes */
typedef enum {
// Non-colour Attributes
FMT_RESET = 0, // Reset all attributes
FMT_BRIGHT = 1, // Bright
FMT_DIM = 2, // Dim
FMT_UNDER = 4, // Underscore
FMT_BLINK = 5, // Blink
FMT_INVERS = 7, // Reverse
FMT_HIDDEN = 8, // Hidden
FMT_ITALIC = 16, // Italic font
FMT_FAINT = 32, // Faint color
// Foreground Colours
FMT_BLACK = 30, // Black
FMT_RED = 31, // Red
FMT_GREEN = 32, // Green
FMT_YELLOW = 33, // Yellow
FMT_BLUE = 34, // Blue
FMT_MAGENTA = 35, // Magenta
FMT_CYAN = 36, // Cyan
FMT_WHITE = 37, // White
// Background Colours
FMT_BLACK_BG = 40, // Black
FMT_RED_BG = 41, // Red
FMT_GREEN_BG = 42, // Green
FMT_YELLOW_BG = 43, // Yellow
FMT_BLUE_BG = 44, // Blue
FMT_MAGENTA_BG = 45, // Magenta
FMT_CYAN_BG = 46, // Cyan
FMT_WHITE_BG = 47, // White
} ANSI_attr_t;
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(__VA_ARGS__, 5,4,3,2,1)
#define VA_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define v100_attr(...) v100_attr_(VA_NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
/**
* Send formatting code to a com interface
*/
void v100_attr_(uint8_t count, ...);

@ -1,6 +1,5 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <utils.h>
#include "dotmatrix.h" #include "dotmatrix.h"
#include "malloc_safe.h" #include "malloc_safe.h"

@ -1,7 +1,9 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <usart.h>
#include <sys/stat.h>
register char * stack_ptr asm("sp"); register char *stack_ptr asm("sp");
caddr_t _sbrk(int incr) caddr_t _sbrk(int incr)
{ {
@ -13,8 +15,7 @@ caddr_t _sbrk(int incr)
heap_end = &end; heap_end = &end;
prev_heap_end = heap_end; prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr) if (heap_end + incr > stack_ptr) {
{
// write(1, "Heap and stack collision\n", 25); // write(1, "Heap and stack collision\n", 25);
// abort(); // abort();
errno = ENOMEM; errno = ENOMEM;
@ -27,6 +28,56 @@ caddr_t _sbrk(int incr)
} }
// Other systcalls are defined in /**
// - com/com_fileio.c * @brief Write to a file by file descriptor.
// - hw_utils/reset.h *
* @param fd : open file descriptor
* @param buf : data to write
* @param len : buffer size
* @return number of written bytes
*/
int _write(int fd, const char *buf, int len)
{
switch (fd) {
case 1: // stdout
case 2: // stderr
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, 10);
return len;
default:
return 0;
}
}
// region stubs
int _read(int fd, char *buf, int len)
{
return 0;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _lseek(int file, int ptr, int dir)
{
return 0;
}
int _close(int file)
{
return -1;
}
int _isatty(int fd)
{
if (fd == 0 || fd == 1 || fd == 2)
return 1;
else
return 0;
}
// endregion

@ -1,5 +1,6 @@
#include "timebase.h" #include "timebase.h"
#include "malloc_safe.h" #include "malloc_safe.h"
#include "debug.h"
// Time base // Time base
static volatile ms_time_t SystemTime_ms = 0; static volatile ms_time_t SystemTime_ms = 0;
@ -87,8 +88,7 @@ static periodic_task_t* claim_periodic_task_slot(ms_time_t interval, bool enqueu
return task; return task;
} }
// TODO logging error("Periodic task table full.");
//error("Periodic task table full.");
return NULL; return NULL;
} }

@ -9,11 +9,11 @@
#include "dotmatrix.h" #include "dotmatrix.h"
#include "mxconstants.h" #include "mxconstants.h"
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "utils.h"
#include "adc.h" #include "adc.h"
#include "tim.h" #include "tim.h"
#include "user_main.h" #include "user_main.h"
#include "debounce.h" #include "debounce.h"
#include "debug.h"
#define SAMPLE_COUNT 256 #define SAMPLE_COUNT 256
#define BIN_COUNT (SAMPLE_COUNT/2) #define BIN_COUNT (SAMPLE_COUNT/2)
@ -160,12 +160,7 @@ void HAL_SYSTICK_Callback(void)
static void gamepad_button_press(uint32_t btn) static void gamepad_button_press(uint32_t btn)
{ {
uart_print("Button press "); dbg("Button press %d", btn);
char x[2];
x[0] = '0' + btn;
x[1] = 0;
uart_print(x);
uart_print("\n");
} }
void user_init() { void user_init() {
@ -225,7 +220,9 @@ void user_init() {
void user_main() void user_main()
{ {
uart_print("== USER CODE STARTING ==\n"); banner("== USER CODE STARTING ==");
info("Hello world");
user_init(); user_init();
@ -247,7 +244,7 @@ void user_main()
void user_Error_Handler() void user_Error_Handler()
{ {
uart_print("HAL error occurred.\n"); error("HAL error occurred.\n");
while (1); while (1);
} }
@ -265,16 +262,7 @@ void user_assert_failed(uint8_t *file, uint32_t line)
void user_error_file_line(const char *message, const char *file, uint32_t line) void user_error_file_line(const char *message, const char *file, uint32_t line)
{ {
uart_print(message); error("%s in file %s on line %d", message, file, line);
uart_print(" in file ");
uart_print((char *) file);
uart_print(" on line ");
char x[10];
sprintf(x, "%"PRIu32, line);
uart_print(x);
uart_print("\n");
while (1); while (1);
} }

Loading…
Cancel
Save