]> mj.ucw.cz Git - home-hw.git/blobdiff - Src/main.c
Request/reply protocol
[home-hw.git] / Src / main.c
index 9d3dbb8905792a7ba4634cfea9b5e7177b88bb3f..e77264bd3e58ad62ed54c3917417cb5fc73155f1 100644 (file)
 /* Includes ------------------------------------------------------------------*/
 #include "main.h"
 #include "stm32f1xx_hal.h"
-#include "usb.h"
 
 /* USER CODE BEGIN Includes */
+#include "util.h"
+#include "usb.h"
+#include "app.h"
+
+#include <string.h>
 
 /* USER CODE END Includes */
 
@@ -61,6 +65,7 @@ static void MX_GPIO_Init(void);
 static void MX_I2C1_Init(void);
 static void MX_I2C2_Init(void);
 static void MX_USB_PCD_Init(void);
+static void MX_TIM4_Init(void);
 
 /* USER CODE BEGIN PFP */
 /* Private function prototypes -----------------------------------------------*/
@@ -69,168 +74,6 @@ static void MX_USB_PCD_Init(void);
 
 /* USER CODE BEGIN 0 */
 
-#include <stdarg.h>
-#include <stdint.h>
-#include <string.h>
-
-void semi_put_char(char c)
-{
-  // This is tricky, we need to work around GCC bugs
-  volatile char cc = c;
-  asm volatile (
-    "mov r0, #0x03\n"   /* SYS_WRITEC */
-    "mov r1, %[msg]\n"
-    "bkpt #0xAB\n"
-    :
-    : [msg] "r" (&cc)
-    : "r0", "r1"
-  );
-}
-
-void semi_write_string(char *c)
-{
-  asm volatile (
-    "mov r0, #0x04\n"   /* SYS_WRITE0 */
-    "mov r1, %[msg]\n"
-    "bkpt #0xAB\n"
-    :
-    : [msg] "r" (c)
-    : "r0", "r1"
-  );
-}
-
-void debug_putc(int c)
-{
-  static char debug_buf[16];
-  static int debug_i;
-  debug_buf[debug_i++] = c;
-  if (c == '\n' || debug_i >= sizeof(debug_buf) - 1)
-    {
-      debug_buf[debug_i] = 0;
-      semi_write_string(debug_buf);
-      debug_i = 0;
-    }
-}
-
-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);
-}
-
 /* USER CODE END 0 */
 
 /**
@@ -257,29 +100,67 @@ int main(void)
   SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
+  usb_init(&usb, &hpcd_USB_FS);
 
   /* USER CODE END SysInit */
 
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
+
+  // A hack to let USB host reset us
+  LL_GPIO_InitTypeDef gpio;
+  gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
+  gpio.Mode = LL_GPIO_MODE_OUTPUT;
+  gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
+  gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
+  LL_GPIO_Init(GPIOA, &gpio);
+  LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
+  LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
+  LL_mDelay(1000);
+
   MX_I2C1_Init();
   MX_I2C2_Init();
   MX_USB_PCD_Init();
+  MX_TIM4_Init();
   /* USER CODE BEGIN 2 */
-  usb_init(&usb, &hpcd_USB_FS);
+  display_init();
+  usb_start(&usb);
+
+  LL_TIM_EnableCounter(TIM4);
+  LL_TIM_EnableIT_UPDATE(TIM4);
+  LL_TIM_GenerateEvent_UPDATE(TIM4);
+
+  {
+    byte buf[5] = { 0xff, 0xff, 10, 0xff, 0xff };
+    display_buffer(buf);
+  }
 
   /* USER CODE END 2 */
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
-  int cnt = 0;
   while (1)
   {
-    LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
-    LL_mDelay(500);
-    LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
-    LL_mDelay(500);
-    debug_printf("Counter = %d\n", cnt++);
+    if (rx_packet_state == 1 && !tx_packet_state)
+      {
+       display_buffer(rx_packet);
+       tx_packet_state = 1;
+       usb_ep_send(&usb, 0x82, tx_packet, 8);
+       rx_packet_state = 0;
+       usb_ep_receive(&usb, 0x01, rx_packet, 64);
+      }
+
+    // debug_printf("Counter = %d\n", cnt);
+    // display_counter(cnt);
+
+    static byte led_state;
+    if (led_state)
+      LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
+    else
+      LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
+    led_state ^= 1;
+
+    __WFI();
 
   /* USER CODE END WHILE */
 
@@ -426,9 +307,49 @@ static void MX_I2C2_Init(void)
 
 }
 
+/* TIM4 init function */
+static void MX_TIM4_Init(void)
+{
+
+  LL_TIM_InitTypeDef TIM_InitStruct;
+  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
+
+  /* Peripheral clock enable */
+  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
+
+  /* TIM4 interrupt Init */
+  NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
+  NVIC_EnableIRQ(TIM4_IRQn);
+
+  TIM_InitStruct.Prescaler = 7200;
+  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
+  TIM_InitStruct.Autoreload = 1000;
+  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
+  LL_TIM_Init(TIM4, &TIM_InitStruct);
+
+  LL_TIM_DisableARRPreload(TIM4);
+
+  LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
+
+  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
+  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
+  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
+  TIM_OC_InitStruct.CompareValue = 0;
+  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
+  LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
+
+  LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
+
+  LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
+
+  LL_TIM_DisableMasterSlaveMode(TIM4);
+
+}
+
 /* USB init function */
 static void MX_USB_PCD_Init(void)
 {
+
   hpcd_USB_FS.Instance = USB;
   hpcd_USB_FS.Init.dev_endpoints = 8;
   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
@@ -440,6 +361,7 @@ static void MX_USB_PCD_Init(void)
   {
     _Error_Handler(__FILE__, __LINE__);
   }
+
 }
 
 /** Configure pins as