]> mj.ucw.cz Git - home-hw.git/blobdiff - nucleo-test/Src/test.c
First bits of DS18B20 communication
[home-hw.git] / nucleo-test / Src / test.c
index 4216c10f0caa4af0bd7835ebb0d3738a1de28a3e..0939aeb8be82d2d210a205a9aea6e73370c173af 100644 (file)
@@ -253,7 +253,7 @@ void run_test(void)
     }
 }
 
-#else
+#elif 0
 
 static const byte Gentium23x32[] = {
         0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x1F, 0x00, 0x80, 0xFF, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0x03, 0xF0, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x3F, 0xFE, 0x01, 0xE0, 0x3F, 0x3E, 0x00, 0x00, 0x7F, 0x1F, 0x00, 0x00, 0x7C, 0x0F, 0x00, 0x00, 0x78, 0x0F, 0x00, 0x00, 0x78, 0x1F, 0x00, 0x00, 0x78, 0x3F, 0x00, 0x00, 0x78, 0x7F, 0x00, 0x00, 0x3E, 0xFE, 0x07, 0xC0, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x07, 0xE0, 0xFF, 0xFF, 0x03, 0x80, 0xFF, 0xFF, 0x00, 0x00, 0xFC, 0x0F, 0x00,  // Code for char 0
@@ -419,4 +419,79 @@ void run_test(void)
     }
 }
 
+#else
+
+static void ds18b20_reset(void)
+{
+  debug_puts("DS18B20: Reset\r\n");
+  //LL_TIM_DisableCounter(TIM3);
+  LL_TIM_SetOnePulseMode(TIM3, LL_TIM_ONEPULSEMODE_SINGLE);
+
+  LL_TIM_OC_InitTypeDef oc;
+  oc.OCMode = LL_TIM_OCMODE_FORCED_ACTIVE;
+  oc.OCState = LL_TIM_OCSTATE_ENABLE;
+  oc.OCNState = LL_TIM_OCSTATE_DISABLE;
+  oc.CompareValue = 480;
+  oc.OCPolarity = LL_TIM_OCPOLARITY_LOW;
+  LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &oc);
+
+  LL_TIM_SetAutoReload(TIM3, 999);
+  LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
+
+  LL_TIM_EnableCounter(TIM3);
+  while (!LL_TIM_IsEnabledCounter(TIM3))
+    ;
+  // Counter is automatically disabled at the end of cycle
+}
+
+static void ds18b20_send_byte(byte b)
+{
+  LL_TIM_SetAutoReload(TIM3, 99);
+  for (uint m=0x80; m; m >>= 1)
+    {
+      LL_TIM_OC_SetCompareCH2(TIM3, ((b & m) ? 1 : 89));
+      LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_FORCED_ACTIVE);
+      LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
+      LL_TIM_EnableCounter(TIM3);
+      while (LL_TIM_IsEnabledCounter(TIM3))
+       ;
+      // Counter is automatically disabled at the end of cycle
+    }
+}
+
+void run_test(void)
+{
+  uint cnt = 0;
+
+  debug_puts("Init\r\n");
+  NVIC_DisableIRQ(TIM3_IRQn);
+
+#if 0
+  LL_GPIO_InitTypeDef GPIO_InitStruct;
+  GPIO_InitStruct.Pin = THERMO_Pin;
+  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
+  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
+  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
+  LL_GPIO_Init(THERMO_GPIO_Port, &GPIO_InitStruct);
+  LL_GPIO_SetOutputPin(THERMO_GPIO_Port, THERMO_Pin);
+#endif
+
+  ds18b20_reset();
+
+  for (;;)
+    {
+      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
+
+      debug_printf("Tick tock: %d\r\n", cnt);
+      // ds18b20_send_byte(0xcc);
+      ds18b20_reset();
+
+      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
+
+      LL_mDelay(100);
+      cnt++;
+    }
+}
+
 #endif