]> mj.ucw.cz Git - home-hw.git/commitdiff
Our minimalistic test file with printf
authorMartin Mares <mj@ucw.cz>
Sat, 26 May 2018 11:00:50 +0000 (13:00 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 26 May 2018 11:00:50 +0000 (13:00 +0200)
Inc/main.h
Makefile
Src/main.c
Src/test.c [new file with mode: 0644]

index 6683e3504ffff65a021cd1a0f1f69a15d1826e0d..815a807b2e3ca003d67c345d3c090f4a8125e854 100644 (file)
@@ -94,6 +94,8 @@
 
 /* USER CODE BEGIN Private defines */
 
 
 /* USER CODE BEGIN Private defines */
 
+void run_test(void);
+
 /* USER CODE END Private defines */
 
 #ifdef __cplusplus
 /* USER CODE END Private defines */
 
 #ifdef __cplusplus
index 048f2c7fa61f13a7b2d6b8be5b6101908eb7293d..897b98ecf71c2eb39e0f5e2c4570616c6b024647 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ TARGET = test
 # debug build?
 DEBUG = 1
 # optimization
 # debug build?
 DEBUG = 1
 # optimization
-OPT = -Og
+OPT = -Os
 
 
 #######################################
 
 
 #######################################
@@ -51,9 +51,8 @@ BUILD_DIR = build
 # C sources
 C_SOURCES =  \
 Src/main.c \
 # C sources
 C_SOURCES =  \
 Src/main.c \
-Src/main.c \
-/Src/system_stm32f0xx.c \
-Src/stm32f0xx_it.c \
+Src/test.c \
+Src/system_stm32f0xx.c \
 Src/stm32f0xx_it.c \
 /aux/misc/stm/F0-package/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_ll_exti.c \
 /aux/misc/stm/F0-package/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_ll_usart.c \
 Src/stm32f0xx_it.c \
 /aux/misc/stm/F0-package/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_ll_exti.c \
 /aux/misc/stm/F0-package/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_ll_usart.c \
@@ -77,7 +76,7 @@ PERIFLIB_SOURCES =
 #######################################
 # binaries
 #######################################
 #######################################
 # binaries
 #######################################
-BINPATH = 
+BINPATH = /usr/bin
 PREFIX = arm-none-eabi-
 CC = $(BINPATH)/$(PREFIX)gcc
 AS = $(BINPATH)/$(PREFIX)gcc -x assembler-with-cpp
 PREFIX = arm-none-eabi-
 CC = $(BINPATH)/$(PREFIX)gcc
 AS = $(BINPATH)/$(PREFIX)gcc -x assembler-with-cpp
@@ -126,7 +125,7 @@ C_INCLUDES =  \
 # compile gcc flags
 ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
 # compile gcc flags
 ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
-CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
+CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -Wextra -Wno-parentheses -Wno-unused -fdata-sections -ffunction-sections
 
 ifeq ($(DEBUG), 1)
 CFLAGS += -g -gdwarf-2
 
 ifeq ($(DEBUG), 1)
 CFLAGS += -g -gdwarf-2
@@ -151,6 +150,9 @@ LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BU
 # default action: build all
 all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
 # default action: build all
 all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
+flash: all
+       LD_LIBRARY_PATH=../stlink ../stlink/st-flash write $(BUILD_DIR)/$(TARGET).bin 0x8000000
+
 
 #######################################
 # build the application
 
 #######################################
 # build the application
index 02220adda978003e7bc1dd0548c9177cb37f073a..ceeb92b085b1097be98e9e5b9ee4d45be71a7c99 100644 (file)
@@ -97,17 +97,17 @@ int main(void)
   MX_USART2_UART_Init();
   /* USER CODE BEGIN 2 */
 
   MX_USART2_UART_Init();
   /* USER CODE BEGIN 2 */
 
+  run_test();
+
   /* USER CODE END 2 */
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
   while (1)
   {
   /* USER CODE END 2 */
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
   while (1)
   {
-
   /* USER CODE END WHILE */
 
   /* USER CODE BEGIN 3 */
   /* USER CODE END WHILE */
 
   /* USER CODE BEGIN 3 */
-
   }
   /* USER CODE END 3 */
 
   }
   /* USER CODE END 3 */
 
@@ -212,7 +212,7 @@ static void MX_USART2_UART_Init(void)
   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
   LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
 
   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
   LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
 
-  USART_InitStruct.BaudRate = 38400;
+  USART_InitStruct.BaudRate = 115200;
   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
diff --git a/Src/test.c b/Src/test.c
new file mode 100644 (file)
index 0000000..ec85b6c
--- /dev/null
@@ -0,0 +1,142 @@
+#include "main.h"
+
+#include <stdarg.h>
+#include <string.h>
+
+typedef unsigned int uint;
+
+void debug_putc(int c)
+{
+  while (!LL_USART_IsActiveFlag_TXE(USART2))
+    ;
+  LL_USART_TransmitData8(USART2, c);
+}
+
+void debug_puts(const char *s)
+{
+  while (*s)
+    debug_putc(*s++);
+}
+
+enum printf_flags {
+  PF_ZERO_PAD = 1,
+  PF_SIGNED = 2,
+  PF_NEGATIVE = 4,
+  PF_UPPERCASE = 8,
+  PF_LEFT = 16,
+};
+
+static void printf_string(const char *s, uint width, uint flags)
+{
+  uint len = strlen(s);
+  uint pad = (len < width) ? width - len : 0;
+  char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
+
+  if (flags & PF_LEFT)
+    debug_puts(s);
+  while (pad--)
+    debug_putc(pad_char);
+  if (!(flags & PF_LEFT))
+    debug_puts(s);
+}
+
+static void printf_number(uint i, uint width, uint flags, uint base)
+{
+  char buf[16];
+  char *w = buf + sizeof(buf);
+
+  if (flags & PF_SIGNED)
+    {
+      if ((int) i < 0)
+       {
+         i = - (int) i;
+         flags |= PF_NEGATIVE;
+       }
+    }
+
+  *--w = 0;
+  do
+    {
+      uint digit = i % base;
+      if (digit < 10)
+       *--w = '0' + digit;
+      else
+       *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
+      i /= base;
+    }
+  while (i);
+
+  if (flags & PF_NEGATIVE)
+    *--w = '-';
+
+  printf_string(w, width, flags);
+}
+
+void debug_printf(const char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+
+  while (*fmt)
+    {
+      int c = *fmt++;
+      if (c != '%')
+       {
+         debug_putc(c);
+         continue;
+       }
+
+      uint width = 0;
+      uint flags = 0;
+
+      if (*fmt == '-')
+       {
+         fmt++;
+         flags |= PF_LEFT;
+       }
+
+      if (*fmt == '0')
+       {
+         fmt++;
+         flags |= PF_ZERO_PAD;
+       }
+
+      while (*fmt >= '0' && *fmt <= '9')
+       width = 10*width + *fmt++ - '0';
+
+      c = *fmt++;
+      switch (c)
+       {
+       case 'd':
+         printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
+         break;
+       case 'u':
+         printf_number(va_arg(args, int), width, flags, 10);
+         break;
+       case 'X':
+         flags |= PF_UPPERCASE;
+         // fall-thru
+       case 'x':
+         printf_number(va_arg(args, int), width, flags, 16);
+         break;
+       case 's':
+         printf_string(va_arg(args, char *), width, flags);
+         break;
+       default:
+         debug_putc(c);
+         continue;
+       }
+    }
+
+  va_end(args);
+}
+
+void run_test(void)
+{
+  for (;;)
+    {
+      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
+      debug_printf("Testing printf: %08X %s\r\n", -42, "mnaeiou");
+      LL_mDelay(200);
+    }
+}