commit 0b989911121411d561b0127517f258468aa2e528
Author: Cung Sang <csang@techfak.uni-bielefeld.de>
Date:   Fri Aug 9 12:55:10 2019 +0200

    added DW1000 test and demo on top of Light Ring module

diff --git a/modules/LightRing_1-2/module.c b/modules/LightRing_1-2/module.c
index 7288abd..801e533 100644
--- a/modules/LightRing_1-2/module.c
+++ b/modules/LightRing_1-2/module.c
@@ -69,6 +69,29 @@ SPIConfig moduleHalSpiLightConfig = {
   /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
 };
 
+#if (BOARD_DW1000_CONNECTED == true)
+
+/*! SPI (high speed) configuration for DW1000 */
+SPIConfig moduleHalSpiUwbHsConfig = {
+  /* circular buffer mode        */ false,
+  /* callback function pointer   */ NULL,
+  /* chip select line port       */ PAL_PORT(LINE_SPI_SS_N),
+  /* chip select line pad number */ PAL_PAD(LINE_SPI_SS_N),
+  /* CR1                         */ 0,                                  // 36/2 Mbps
+  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,  // 0
+};
+/*! SPI (low speed) configuration for DW1000 */
+SPIConfig moduleHalSpiUwbLsConfig = {
+  /* circular buffer mode        */ false,
+  /* callback function pointer   */ NULL,
+  /* chip select line port       */ PAL_PORT(LINE_SPI_SS_N),
+  /* chip select line pad number */ PAL_PAD(LINE_SPI_SS_N),
+  /* CR1                         */ SPI_CR1_BR_1 | SPI_CR1_BR_0,        // 36/16 Mbps
+  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,  // 0
+};
+
+#endif /* BOARD_DW1000_CONNECTED == true */
+
 
 /*===========================================================================*/
 /**
@@ -213,22 +236,68 @@ ROMCONST apalControlGpio_t moduleGpioSysUartDn = {
   },
 };
 
+#if (BOARD_DW1000_CONNECTED == true)
 /**
- * @brief   IO_7 breakout signal GPIO.
+ * @brief   DW1000 interrrupt input signal GPIO.
  */
-static apalGpio_t _gpioBreakoutIo7 = {
+static apalGpio_t _gpioDw1000Irqn = {
+  /* line */  LINE_IO_8,            // (GPIOB, GPIOB_IO_8),
+};
+ROMCONST apalControlGpio_t moduleGpioDw1000Irqn = {
+  /* GPIO */ &_gpioDw1000Irqn,
+  /* meta */ {
+    /* direction      */ APAL_GPIO_DIRECTION_INPUT,
+    /* active state   */ APAL_GPIO_ACTIVE_LOW,
+    /* interrupt edge */ APAL_GPIO_EDGE_RISING,
+  },
+};
+
+/**
+ * @brief   DW1000 reset output signal GPIO.
+ */
+static apalGpio_t _gpioDw1000Reset = {
   /* line */ LINE_IO_7,
 };
-apalControlGpio_t moduleGpioBreakoutIo7 = {
-  /* GPIO */ &_gpioBreakoutIo7,
+ROMCONST apalControlGpio_t moduleGpioDw1000Reset = {
+  /* GPIO */ &_gpioDw1000Reset,
   /* meta */ {
-    /* direction      */ APAL_GPIO_DIRECTION_UNDEFINED,
+    /* direction      */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
     /* active state   */ APAL_GPIO_ACTIVE_LOW,
     /* interrupt edge */ APAL_GPIO_EDGE_NONE,
   },
 };
 
 /**
+ * @brief   IO_2 breakout signal GPIO.
+ */
+static apalGpio_t _gpioDw1000WakeUp = {
+  /* line */ LINE_IO_2,
+};
+ROMCONST apalControlGpio_t moduleGpioDw1000WakeUp = {
+  /* GPIO */ &_gpioDw1000WakeUp,
+  /* meta */ {
+    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
+    /* active state   */ APAL_GPIO_ACTIVE_HIGH,
+    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
+  },
+};
+
+/**
+ * @brief   DW1000 SPI chip select output signal GPIO.
+ */
+static apalGpio_t _gpioSpiChipSelect = {
+  /* line */ LINE_SPI_SS_N,
+};
+ROMCONST apalControlGpio_t moduleGpioSpiChipSelect = {
+  /* GPIO */ &_gpioSpiChipSelect,
+  /* meta */ {
+    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
+    /* active state   */ APAL_GPIO_ACTIVE_LOW,
+    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
+  },
+};
+#else
+/**
  * @brief   IO_8 breakout signal GPIO.
  */
 static apalGpio_t _gpioBreakoutIo8 = {
@@ -244,6 +313,38 @@ apalControlGpio_t moduleGpioBreakoutIo8 = {
 };
 
 /**
+ * @brief   IO_7 breakout signal GPIO.
+ */
+static apalGpio_t _gpioBreakoutIo7 = {
+  /* line */ LINE_IO_7,
+};
+apalControlGpio_t moduleGpioBreakoutIo7 = {
+  /* GPIO */ &_gpioBreakoutIo7,
+  /* meta */ {
+    /* direction      */ APAL_GPIO_DIRECTION_UNDEFINED,
+    /* active state   */ APAL_GPIO_ACTIVE_LOW,
+    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
+  },
+};
+
+/**
+ * @brief   IO_2 breakout signal GPIO.
+ */
+static apalGpio_t _gpioBreakoutIo2 = {
+  /* line */ LINE_IO_2,
+};
+apalControlGpio_t moduleGpioBreakoutIo2 = {
+  /* GPIO */ &_gpioBreakoutIo2,
+  /* meta */ {
+    /* direction      */ APAL_GPIO_DIRECTION_UNDEFINED,
+    /* active state   */ APAL_GPIO_ACTIVE_LOW,
+    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
+  },
+};
+#endif  /* BOARD_DW1000_CONNECTED == true */
+
+
+/**
  * @brief   IO_4 breakout signal GPIO.
  */
 static apalGpio_t _gpioBreakoutIo4 = {
@@ -273,20 +374,6 @@ apalControlGpio_t moduleGpioBreakoutIo1 = {
   },
 };
 
-/**
- * @brief   IO_2 breakout signal GPIO.
- */
-static apalGpio_t _gpioBreakoutIo2 = {
-  /* line */ LINE_IO_2,
-};
-apalControlGpio_t moduleGpioBreakoutIo2 = {
-  /* GPIO */ &_gpioBreakoutIo2,
-  /* meta */ {
-    /* direction      */ APAL_GPIO_DIRECTION_UNDEFINED,
-    /* active state   */ APAL_GPIO_ACTIVE_LOW,
-    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
-  },
-};
 
 /**
  * @brief   LED output signal GPIO.
@@ -419,6 +506,72 @@ ROMCONST char* moduleShellPrompt = "LightRing";
 
 /*===========================================================================*/
 /**
+ * @name Hardware specific wrappers Functions
+ * @{
+ */
+/*===========================================================================*/
+
+#if (BOARD_DW1000_CONNECTED == true)
+/*! @brief Manually reset the DW1000 module  */
+void reset_DW1000(void){
+
+  // Set the pin as output
+  palSetLineMode(moduleGpioDw1000Reset.gpio->line, APAL_GPIO_DIRECTION_OUTPUT);
+
+  //drive the RSTn pin low
+  apalGpioWrite(moduleGpioDw1000Reset.gpio, APAL_GPIO_LOW);
+
+  //put the pin back to tri-state ... as input
+//  palSetLineMode(moduleGpioDw1000Reset.gpio->line, APAL_GPIO_DIRECTION_INPUT); // TODO:
+
+  aosThdMSleep(2);
+}
+
+/*! @brief entry point to the IRQn event in DW1000 module
+ *
+ * */
+void process_deca_irq(void){
+  do{
+    dwt_isr();
+   //while IRS line active (ARM can only do edge sensitive interrupts)
+  }while(port_CheckEXT_IRQ() == 1);
+}
+
+/*! @brief Check the current value of GPIO pin and return the value */
+apalGpioState_t port_CheckEXT_IRQ(void) {
+  apalGpioState_t  val;
+  apalGpioRead(moduleGpioDw1000Irqn.gpio, &val);
+  return val;
+}
+
+/*! @brief Manually set the chip select pin of the SPI */
+void set_SPI_chip_select(void){
+  apalGpioWrite(moduleGpioSpiChipSelect.gpio, APAL_GPIO_HIGH);
+}
+
+/*! @brief Manually reset the chip select pin of the SPI */
+void clear_SPI_chip_select(void){
+  apalGpioWrite(moduleGpioSpiChipSelect.gpio, APAL_GPIO_LOW);
+}
+
+/*! @brief Change the SPI speed configuration on the fly */
+void setHighSpeed_SPI(bool speedValue, DW1000Driver* drv){
+
+  spiStop(drv->spid);
+
+  if (speedValue == FALSE){
+    spiStart(drv->spid, &moduleHalSpiUwbLsConfig);  // low speed spi configuration
+  }
+  else{
+    spiStart(drv->spid, &moduleHalSpiUwbHsConfig); // high speed spi configuration
+  }
+}
+#endif /* (BOARD_DW1000_CONNECTED == true) */
+/** @} */
+
+
+/*===========================================================================*/
+/**
  * @name Low-level drivers
  * @{
  */
@@ -469,6 +622,18 @@ TLC5947Driver moduleLldLedPwm = {
   /* XLAT signal GPIO   */ &moduleGpioLightXlat,
 };
 
+#if (BOARD_DW1000_CONNECTED == true)
+DW1000Driver moduleLldDw1000 = {
+  /* SPI driver         */ &MODULE_HAL_SPI_UWB,
+  /* ext interrupt      */ &moduleGpioDw1000Irqn,
+  /* RESET DW1000       */ &moduleGpioDw1000Reset,
+};
+
+MIC9404xDriver moduleLldPowerSwitchDw1000 = {
+  /* power enable GPIO  */ &moduleGpioSwVsysEn,
+};
+#endif /* (BOARD_DW1000_CONNECTED == true) */
+
 /** @} */
 
 /*===========================================================================*/
@@ -531,6 +696,18 @@ static int _testTlc5947ShellCmdCb(BaseSequentialStream* stream, int argc, char*
 }
 AOS_SHELL_COMMAND(moduleTestTlc5947ShellCmd, "test:Lights", _testTlc5947ShellCmdCb);
 
+#if (BOARD_DW1000_CONNECTED == true) || defined(__DOXYGEN__)
+/*
+ * UwB Driver (DW1000)
+ */
+#include <module_test_DW1000.h>
+static int _testDw1000ShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
+{
+  return moduleTestDw1000ShellCb(stream, argc, argv, NULL);
+}
+AOS_SHELL_COMMAND(moduleTestDw1000ShellCmd, "test:DW1000", _testDw1000ShellCmdCb);
+#endif /* (BOARD_DW1000_CONNECTED == true) */
+
 /*
  * entire module
  */
