]> mj.ucw.cz Git - home-hw.git/commitdiff
SSR: USB communitation including test client
authorMartin Mares <mj@ucw.cz>
Tue, 7 Aug 2018 21:23:08 +0000 (23:23 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 7 Aug 2018 21:23:08 +0000 (23:23 +0200)
ssr/Inc/app.h
ssr/Inc/util.h
ssr/Src/main.c
ssr/Src/stm32f1xx_it.c
ssr/host/test.c

index ba8b412069d1e9529fb163d590b37cfe00a04d8b..980422d55c2bf2c9c98554046c9d3878c94c16f6 100644 (file)
@@ -1,5 +1,7 @@
 // main.c
 
 // main.c
 
+extern volatile byte led_trigger;
+
 // usbdev.c
 
 extern byte rx_packet[64];
 // usbdev.c
 
 extern byte rx_packet[64];
index 405098c869505bb5ae6780c5c1c1da12edba0ad2..3a29fd63c476627d721b7db94d0e22d4bcb274f3 100644 (file)
@@ -16,6 +16,21 @@ static inline uint get_u16_le(byte *p)
   return (p[1] << 8) | p[0];
 }
 
   return (p[1] << 8) | p[0];
 }
 
+static inline uint get_u16_be(byte *p)
+{
+  return (p[0] << 8) | p[1];
+}
+
+static inline uint get_u32_le(byte *p)
+{
+  return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
+}
+
+static inline uint get_u32_be(byte *p)
+{
+  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
+}
+
 static inline void put_u16_le(byte *p, u16 x)
 {
   p[0] = x;
 static inline void put_u16_le(byte *p, u16 x)
 {
   p[0] = x;
@@ -36,10 +51,19 @@ static inline void put_u32_be(byte *p, u32 x)
   p[3] = x & 0xff;
 }
 
   p[3] = x & 0xff;
 }
 
+static inline void put_u32_le(byte *p, u32 x)
+{
+  p[3] = x >> 24;
+  p[2] = (x >> 16) & 0xff;
+  p[1] = (x >> 8) & 0xff;
+  p[0] = x & 0xff;
+}
+
 // debug.c
 
 #undef DEBUG_SEMIHOSTING
 // debug.c
 
 #undef DEBUG_SEMIHOSTING
-#define DEBUG_USART USART1
+// #define DEBUG_USART USART1
+#undef DEBUG_USART
 
 void debug_printf(const char *fmt, ...);
 void debug_puts(const char *s);
 
 void debug_printf(const char *fmt, ...);
 void debug_puts(const char *s);
index cc6e3636777405c8354cda4ae8cee2e8922e0f86..da07b57dce35e694fb97dc40c7df7de1f1ccca6e 100644 (file)
@@ -52,6 +52,7 @@
 /* Private variables ---------------------------------------------------------*/
 
 PCD_HandleTypeDef hpcd_USB_FS;
 /* Private variables ---------------------------------------------------------*/
 
 PCD_HandleTypeDef hpcd_USB_FS;
+volatile byte led_trigger;
 
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
 
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
@@ -73,6 +74,44 @@ static void MX_USART1_UART_Init(void);
 
 /* USER CODE BEGIN 0 */
 
 
 /* USER CODE BEGIN 0 */
 
+static void process_packet(void)
+{
+  uint cmd = get_u32_be(rx_packet);
+  uint arg = get_u32_be(rx_packet+4);
+  debug_printf("<< cmd %08x %08x\n", cmd, arg);
+
+  memset(tx_packet, 0, sizeof(tx_packet));
+  put_u32_be(tx_packet, 1);
+
+  switch (cmd)
+    {
+    case 1:
+      if (arg < 16)
+       {
+         if (arg & 1)
+           LL_GPIO_ResetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
+         else
+           LL_GPIO_SetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
+         if (arg & 2)
+           LL_GPIO_ResetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
+         else
+           LL_GPIO_SetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
+         if (arg & 4)
+           LL_GPIO_ResetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
+         else
+           LL_GPIO_SetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
+         if (arg & 8)
+           LL_GPIO_ResetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
+         else
+           LL_GPIO_SetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
+         put_u32_be(tx_packet, 0);
+       }
+      break;
+    }
+
+  debug_printf(">> status %08x\n", get_u32_be(tx_packet));
+}
+
 /* USER CODE END 0 */
 
 /**
 /* USER CODE END 0 */
 
 /**
@@ -133,53 +172,34 @@ int main(void)
   /* USER CODE BEGIN WHILE */
   while (1)
   {
   /* USER CODE BEGIN WHILE */
   while (1)
   {
-    static byte led_state;
-    if (led_state)
+    if (led_trigger)
       {
       {
-       LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
-       LL_GPIO_SetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
-       LL_GPIO_SetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
-       LL_GPIO_SetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
-       LL_GPIO_SetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
-      }
-    else
-      {
-       LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
-       static byte xxx;
-       switch (xxx)
-         {
-         case 0:
-           LL_GPIO_ResetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
-           break;
-         case 1:
-           LL_GPIO_ResetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
-           break;
-         case 2:
-           LL_GPIO_ResetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
-           break;
-         case 3:
-           LL_GPIO_ResetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
-           break;
-         }
-       xxx = (xxx+1) % 4;
+       led_trigger = 0;
+       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;
       }
       }
-    led_state ^= 1;
 
     if (rx_packet_state == 1 && !tx_packet_state)
       {
        tx_packet_state = 1;
 
     if (rx_packet_state == 1 && !tx_packet_state)
       {
        tx_packet_state = 1;
-       put_u32_be(tx_packet, 42);
+       process_packet();
        usb_ep_send(&usb, 0x82, tx_packet, 12);
        rx_packet_state = 0;
        usb_ep_receive(&usb, 0x01, rx_packet, 64);
       }
 
        usb_ep_send(&usb, 0x82, tx_packet, 12);
        rx_packet_state = 0;
        usb_ep_receive(&usb, 0x01, rx_packet, 64);
       }
 
+#if 0
     static int cnt;
     debug_printf("Counter = %d\n", cnt);
     cnt++;
     static int cnt;
     debug_printf("Counter = %d\n", cnt);
     cnt++;
+#endif
 
 
-    //__WFI();
-    LL_mDelay(1000);
+    __WFI();
+    // LL_mDelay(1000);
 
   /* USER CODE END WHILE */
 
 
   /* USER CODE END WHILE */
 
index 1754251c6d162f6b33ff88692dd65c2ff7ebdd83..745832c0416ba58b9aecb59cff217651e9a6a3d1 100644 (file)
@@ -230,7 +230,7 @@ void TIM4_IRQHandler(void)
   /* USER CODE BEGIN TIM4_IRQn 0 */
   if (LL_TIM_IsActiveFlag_UPDATE(TIM4))
     {
   /* USER CODE BEGIN TIM4_IRQn 0 */
   if (LL_TIM_IsActiveFlag_UPDATE(TIM4))
     {
-      // FIXME
+      led_trigger = 1;
       LL_TIM_ClearFlag_UPDATE(TIM4);
     }
 
       LL_TIM_ClearFlag_UPDATE(TIM4);
     }
 
index 2d94a98ec8c9e31cba8341c7a8149a7b8fcf5415..402db87d37b4a07f1f1dbe107251a1a4ef5ec5ae 100644 (file)
@@ -27,7 +27,7 @@ static libusb_device *find_device(void)
       libusb_device *dev = devlist[i];
       if (!libusb_get_device_descriptor(dev, &desc))
        {
       libusb_device *dev = devlist[i];
       if (!libusb_get_device_descriptor(dev, &desc))
        {
-         if (desc.idVendor == 0x4242 && desc.idProduct == 0x0001)
+         if (desc.idVendor == 0x4242 && desc.idProduct == 0x0002)
            {
              printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
              // FIXME: Free device list
            {
              printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
              // FIXME: Free device list
@@ -65,18 +65,15 @@ int main(void)
       exit(1);
     }
 
       exit(1);
     }
 
+  uint tst = 0;
+
   for (;;)
     {
   for (;;)
     {
-      time_t t = time(NULL);
-      struct tm *tm = localtime(&t);
+      byte req[8];
+      put_u32_be(req, 1);
+      put_u32_be(req+4, (1 << tst));
+      tst = (tst+1) % 4;
 
 
-      unsigned char req[8] = {
-       tm->tm_hour / 10,
-       tm->tm_hour % 10,
-       (tm->tm_sec % 2 ? 10 : 0xff),
-       tm->tm_min / 10,
-       tm->tm_min % 10,
-      };
       int transferred;
       if (err = libusb_bulk_transfer(devh, 0x01, req, 8, &transferred, 2000))
        die("Transfer failed: error %d\n", err);
       int transferred;
       if (err = libusb_bulk_transfer(devh, 0x01, req, 8, &transferred, 2000))
        die("Transfer failed: error %d\n", err);
@@ -87,13 +84,15 @@ int main(void)
       if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
        die("Receive failed: error %d\n", err);
       // printf("Received %d bytes\n", received);
       if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
        die("Receive failed: error %d\n", err);
       // printf("Received %d bytes\n", received);
-      if (received >= 12)
+
+      if (received >= 4)
        {
        {
-         int t = get_u32_be(resp);
-         int p = get_u32_be(resp + 4);
-         uint cnt = get_u32_be(resp + 8);
-         msg(L_INFO, "Temperature %d ddegC, pressure %d Pa, cnt %u", t, p, cnt);
+         int status = get_u32_be(resp);
+         if (status)
+           msg(L_ERROR, "Received error status %08x", status);
        }
        }
+      else
+       msg(L_ERROR, "Received short packet");
 
       sleep(1);
     }
 
       sleep(1);
     }