commit 884499878182d0763bfd441d1c447cd8af1e4d8a
Author: Cung Sang <csang@techfak.uni-bielefeld.de>
Date:   Mon Apr 1 18:17:22 2019 +0200

    introduced UWB apps and UT on Light Ring breakout

diff --git a/unittests/periphery-lld/src/ut_alld_DW1000_v1.c b/unittests/periphery-lld/src/ut_alld_DW1000_v1.c
index 2600a7a..17150a6 100755
--- a/unittests/periphery-lld/src/ut_alld_DW1000_v1.c
+++ b/unittests/periphery-lld/src/ut_alld_DW1000_v1.c
@@ -27,6 +27,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <module.h>
 #include <alld_DW1000.h>
 #include <v1/deca_instance_v1.h>
+#include <v1/alld_dw1000_regs_v1.h>
+#include <string.h>
 
 
 /******************************************************************************/
@@ -69,9 +71,7 @@ int instance_mode = ANCHOR;
 
 /*! @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
   }
@@ -80,9 +80,7 @@ void setHighSpeed_SPI(bool speedValue, DW1000Driver* drv){
   }
 }
 
-/*! @brief entry point to the IRQn event in DW1000 module
- *
- * */
+/*! @brief entry point to the IRQn event in DW1000 module */
 void process_deca_irq(void){
   do{
     dwt_isr();
@@ -90,13 +88,6 @@ void process_deca_irq(void){
   }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);
@@ -109,25 +100,31 @@ void clear_SPI_chip_select(void){
 
 /*! @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(7);
+}
 
-  aosThdMSleep(2);
+/*! @brief waking up the DW1000 module using Chip Select pin */
+void wakeup_DW1000(void){
+  clear_SPI_chip_select();
+  aosThdMSleep(1);
+  set_SPI_chip_select();
+  aosThdMSleep(7);  // wait for XTAL to stabilize
+
+  // set wakeup pin directly high
+//   apalGpioWrite(moduleGpioDw1000WakeUp.gpio, APAL_GPIO_HIGH);
+   aosThdMSleep(10);
 }
 
 /*! @brief Configure instance tag/anchor/etc... addresses */
 void addressconfigure(uint8_t s1switch, uint8_t mode){
   uint16_t instAddress ;
-
   instance_anchaddr = (((s1switch & SWS1_A1A_MODE) << 2) + (s1switch & SWS1_A2A_MODE) + ((s1switch & SWS1_A3A_MODE) >> 2)) >> 4;
-
   if(mode == ANCHOR) {
     if(instance_anchaddr > 3) {
       instAddress = GATEWAY_ANCHOR_ADDR | 0x4 ; //listener
@@ -139,22 +136,18 @@ void addressconfigure(uint8_t s1switch, uint8_t mode){
   else{
     instAddress = (uint16_t)instance_anchaddr;
   }
-
   instancesetaddresses(instAddress);
 }
 
 /*! @brief returns the use case / operational mode */
 int decarangingmode(uint8_t s1switch){
   int mode = 0;
-
   if(s1switch & SWS1_SHF_MODE)  {
     mode = 1;
   }
-
   if(s1switch & SWS1_CH5_MODE) {
     mode = mode + 2;
   }
-
   return mode;
 }
 
@@ -177,35 +170,26 @@ int32_t inittestapplication(uint8_t s1switch, DW1000Driver* drv){
     }    
     dwt_softreset();
   }
-
     reset_DW1000();  //reset the DW1000 by driving the RSTn line low
-
   if((s1switch & SWS1_ANC_MODE) == 0){
     instance_mode = TAG;
   }
   else{
     instance_mode = ANCHOR;
   }
-
-  result = instance_init(drv) ;
-
+  result = instance_init(drv);
   if (0 > result){
     return(-1) ;
   }
-
   setHighSpeed_SPI(TRUE, drv);       // high speed spi max. ~ 20M
   devID = instancereaddeviceid() ;
-
   if (DWT_DEVICE_ID != devID){
     return(-1) ;
   }
-
   addressconfigure(s1switch, (uint8_t)instance_mode) ;
-
   if((instance_mode == ANCHOR) && (instance_anchaddr > 0x3)){
     instance_mode = LISTENER;
   }
-
   instancesetrole(instance_mode) ;       // Set this instance role
   dr_mode = decarangingmode(s1switch);
   chan = chConfig[dr_mode].channelNumber ;
@@ -227,15 +211,11 @@ int UWB_Init(DW1000Driver* drv){
            | S1_SWITCH_OFF << 6  // (configure Tag or anchor ID no.)
            | S1_SWITCH_OFF << 7; // Not use in this demo
 
-
   port_DisableEXT_IRQ();           //disable ScenSor IRQ until we configure the device
-
   if(inittestapplication(s1switch, drv) == -1) {
     return (-1); //error
   }
-
   aosThdMSleep(5);
-
   port_EnableEXT_IRQ();  //enable DW1000 IRQ before starting
 
   return 0;
@@ -245,154 +225,217 @@ int UWB_Init(DW1000Driver* drv){
 /******************************************************************************/
 /* EXPORTED FUNCTIONS                                                         */
 /******************************************************************************/
-
-
 aos_utresult_t utAlldDw1000Func(BaseSequentialStream* stream, aos_unittest_t* ut) {
 
   aosDbgCheck(ut->data != NULL);
-
   aos_utresult_t result = {0, 0};
 
+#if defined (AMIROLLD_CFG_MIC9404x)
+  // Enable 3.3 and 1.8 supply voltages for powering up the DW1000 module in AMiRo Light Ring
+  if ((ut->data != NULL) && (((ut_dw1000data_t*)(ut->data))->mic9404xd != NULL)){
+    mic9404x_lld_state_t state;
+    uint32_t status = APAL_STATUS_OK;
+
+    chprintf(stream, "reading current status of the Power..\n");
+    status = mic9404x_lld_get(((ut_dw1000data_t*)(ut->data))->mic9404xd, &state);
+    if (status == APAL_STATUS_OK) {
+      aosUtPassedMsg(stream, &result, "power %s\n", (state == MIC9404x_LLD_STATE_ON) ? "enabled" : "disabled");
+    } else {
+      aosUtFailed(stream, &result);
+    }
+    if (state == MIC9404x_LLD_STATE_OFF) {
+      chprintf(stream, "enabling the power ...\n");
+      status = mic9404x_lld_set(((ut_dw1000data_t*)(ut->data))->mic9404xd, MIC9404x_LLD_STATE_ON);
+      status |= mic9404x_lld_get(((ut_dw1000data_t*)(ut->data))->mic9404xd, &state);
+      if (state == MIC9404x_LLD_STATE_ON) {
+        aosThdSSleep(2);
+        status |= mic9404x_lld_get(((ut_dw1000data_t*)(ut->data))->mic9404xd, &state);
+      }
+      if ((status == APAL_STATUS_OK) && (state == MIC9404x_LLD_STATE_ON)) {
+        aosUtPassed(stream, &result);
+      } else {
+        aosUtFailed(stream, &result);
+      }
+    }
+    aosThdSleep(1);
+    return result;
+  }
+#endif  /* defined (AMIROLLD_CFG_MIC9404x) */
+
+
+  // Start the DW1000 module UT after powering up
   chprintf(stream, "init DW1000...\n");
-  dwt_initialise(DWT_LOADUCODE, (DW1000Driver*) ut->data);
-  aosThdMSleep(5);
+  aosThdSleep(1);
+  ut_dw1000data_t* h_dw1000data = NULL;
+  if((ut->data != NULL) && (((ut_dw1000data_t*)(ut->data))->dw1000d != NULL)){
+    h_dw1000data = ut->data;
+    chprintf(stream, "assign handle for DW1000Driver struct \n");
+  }
+  else {
+    chprintf(stream, "unsupported data type \n");
+    return result;
+  }
+  aosThdSleep(1);
 
+  reset_DW1000();     // hard reset
+//  wakeup_DW1000();
+  aosThdMSleep(5);
+  int init = dwt_initialise(DWT_LOADUCODE, h_dw1000data->dw1000d);
+  if (init == 0){
+    chprintf(stream, "DW1000 is initialized \n");
+  }
+  else {
+    chprintf(stream, "init error with return value: %d \n", init);
+  }
+  aosThdMSleep(5);
 
 /*! Unit Test snippets for DW1000.
  * @Note: Event IRQ for DW1000 should be tested separately
  */
 #if defined(UNIT_TEST_SNIPPETS_DW1000)
-
-  uint32_t actual_deviceId;
-
+  uint32_t actual_devID;
   port_DisableEXT_IRQ();
 
-  setHighSpeed_SPI(false, (DW1000Driver*) ut->data);
-  chprintf(stream, "expected device ID (LS SPI): 0xDECA0130 \n");
+  /*! UT1: Low speed SPI result */
+  setHighSpeed_SPI(false, h_dw1000data->dw1000d);
+  chprintf(stream, "expected ID (LS SPI): 0xDECA0130 \n");
   aosThdMSleep(5);
-  actual_deviceId = instancereaddeviceid();
-  chprintf(stream, "actual read ID: 0x%x\n", actual_deviceId);
+  actual_devID = instancereaddeviceid();
+  chprintf(stream, "read ID (LS SPI): 0x%x\n", actual_devID);
   aosThdMSleep(5);
 
   //if the read of device ID fails, the DW1000 could be asleep
-  if(DWT_DEVICE_ID != actual_deviceId){
-
+  if(DWT_DEVICE_ID != actual_devID){
     clear_SPI_chip_select();
     aosThdMSleep(1);
     set_SPI_chip_select();
     aosThdMSleep(7);
-    actual_deviceId = instancereaddeviceid() ;
-
-    if(DWT_DEVICE_ID != actual_deviceId){
+    actual_devID = instancereaddeviceid() ;
+    if(DWT_DEVICE_ID != actual_devID){
       chprintf(stream, "SPI is not working or Unsupported Device ID\n");
-      chprintf(stream, "actual device ID is: 0x%x\n", actual_deviceId);
+      chprintf(stream, "actual device ID is: 0x%x\n", actual_devID);
       chprintf(stream, "expected device ID: 0xDECA0130 \n");
       aosThdMSleep(5);
-    }
-
-    //clear the sleep bit - so that after the hard reset below the DW does not go into sleep
+    }   
     dwt_softreset();
   }
 
-  /*! UT1: Low speed SPI result */
-  if (actual_deviceId == DWT_DEVICE_ID){
+  if (actual_devID == DWT_DEVICE_ID){
     aosUtPassed(stream, &result);
   } else {
     aosUtFailed(stream, &result);
   }
 
-  reset_DW1000();
-
-  chprintf(stream, "initialise instance for DW1000 \n");
-  aosThdSleep(1);
-
-  int x_init = instance_init((DW1000Driver*) ut->data) ;
+  /*! Blinking TX and RX LED simultenously for 5 times */
+  chprintf(stream, "blinking TX and RX LEDs simultenously for 5 times \n");
+  for(int i =0; i< 5; i++){
+    uint8_t mode = 3;
+    uint32_t reg ;
+
+    // Set up for LED output.
+    reg = dwt_read32bitoffsetreg(GPIO_CTRL_ID, GPIO_MODE_OFFSET); // Hardcoded = 0xDE001400
+    reg &= ~(GPIO_MSGP2_MASK | GPIO_MSGP3_MASK);
+    reg |= (GPIO_PIN2_RXLED | GPIO_PIN3_TXLED);
+    dwt_write32bitoffsetreg(GPIO_CTRL_ID, GPIO_MODE_OFFSET, reg);
+
+    // Enable LP Oscillator to run from counter and turn on de-bounce clock.
+    reg = dwt_read32bitoffsetreg(PMSC_ID, PMSC_CTRL0_OFFSET);      // hard-coded = 0xF0B40200
+    reg |= (PMSC_CTRL0_GPDCE | PMSC_CTRL0_KHZCLEN);
+    dwt_write32bitoffsetreg(PMSC_ID, PMSC_CTRL0_OFFSET, reg);
+
+    // Enable LEDs to blink and set default blink time.
+    reg = PMSC_LEDC_BLNKEN | PMSC_LEDC_BLINK_TIME_DEF;
+    // Make LEDs blink once if requested.
+    if (mode & DWT_LEDS_INIT_BLINK)
+    {
+      reg |= PMSC_LEDC_BLINK_NOW_ALL;
+    }
+    dwt_write32bitoffsetreg(PMSC_ID, PMSC_LEDC_OFFSET, reg);
+    // Clear force blink bits if needed.
+    if(mode & DWT_LEDS_INIT_BLINK)
+    {
+      reg &= ~PMSC_LEDC_BLINK_NOW_ALL;
+      dwt_write32bitoffsetreg(PMSC_ID, PMSC_LEDC_OFFSET, reg);
+    }
+    aosThdMSleep(500);
+  }
 
-  if (0 != x_init){
+  /*! UT2: Initialization  of the DW1000 module */
+  reset_DW1000();
+  chprintf(stream, "initialise the instance for DW1000 \n");
+  aosThdMSleep(100);
+  int x_init = instance_init(h_dw1000data->dw1000d) ;
+  if (x_init != 0){
     chprintf(stream, "init error with return value: %d \n", x_init);
-    aosThdSleep(1);
   }
   else {
-    chprintf(stream, "init success with return value: %d \n", x_init);
-    aosThdSleep(1);
+    chprintf(stream, "succeed init! \n");
   }
-
-  /*! UT2: Initialization  result*/
+  aosThdMSleep(100);
   if (x_init == 0){
     aosUtPassed(stream, &result);
   } else {
     aosUtFailed(stream, &result);
   }
 
-  setHighSpeed_SPI(true, (DW1000Driver*) ut->data);
-
-  chprintf(stream, "expected device ID (HS SPI): 0xDECA0130\n");
-  actual_deviceId = instancereaddeviceid();
-  chprintf(stream, "actual read ID: 0x%x\n", actual_deviceId);
-  aosThdMSleep(1);
-
-  /*! UT3: High speed SPI result*/
-  if (actual_deviceId == DWT_DEVICE_ID){
+  /*! UT3: High speed SPI Testing */
+  setHighSpeed_SPI(true, h_dw1000data->dw1000d);
+  chprintf(stream, "expected ID (HS SPI): 0xDECA0130\n");
+  actual_devID = instancereaddeviceid();
+  chprintf(stream, "Read ID (HS SPI): 0x%x\n", actual_devID);
+  aosThdMSleep(100);
+  if (actual_devID == DWT_DEVICE_ID){
     aosUtPassed(stream, &result);
   } else {
     aosUtFailed(stream, &result);
   }
 
+  /*! UT4: Configuration of UWB module
+   * If all the five unit tests are passed, the module is ready to run.
+   * Note that the interrupt IRQn should be tested separately.
+   */
   port_EnableEXT_IRQ();
   reset_DW1000();
-
   chprintf(stream, "initialise the configuration for UWB application \n");
   aosThdSleep(1);
-
-  int uwb_init = UWB_Init((DW1000Driver*) ut->data);
-
-  if (0 != uwb_init){
+  int uwb_init = UWB_Init(h_dw1000data->dw1000d);
+  if (uwb_init != 0){
     chprintf(stream, "UWB config error with return value: %d \n", uwb_init);
-    aosThdSleep(1);
   }
   else {
-    chprintf(stream, "UWB config success with return value: %d \n", uwb_init);
-    aosThdSleep(1);
+    chprintf(stream, "succeed UWB config process \n", uwb_init);
   }
-
-  /*! UT4: UWB configuration result
-   * If all the four unit tests are passed, the module is ready to run.
-   * Note that the interrupt IRQn should be tested separately.
-   */
   if (uwb_init == 0){
     aosUtPassed(stream, &result);
   } else {
     aosUtFailed(stream, &result);
   }
 
-  /************** End of UNIT_TEST_SNIPPETS_DW1000*****************/
+  /************** End of UNIT_TEST_SNIPPETS_DW1000 *****************/
 
 #else /* defined(UNIT_TEST_SNIPPETS_DW1000) */
 
-
   /*! RUN THE STATE MACHINE DEMO APP (RTLS) */
-
   chprintf(stream, "initialise the State Machine \n");
   aosThdSleep(2);
 
   /* Initialize UWB system with user defined configuration  */
-  int uwb_init = UWB_Init((DW1000Driver*) ut->data);
+  int uwb_init = UWB_Init(h_dw1000data->dw1000d);
 
-  if (0 != uwb_init){
+  if (uwb_init != 0){
     chprintf(stream, "error in UWB config with return value: %d \n", uwb_init);
   }
   else {
     chprintf(stream, "succeed the init of UWB config \n");
   }
   aosThdSleep(1);
-
   chprintf(stream, "running the RTLS demo application ... \n");
-  aosThdSleep(1);
 
   /*! Run the localization system demo app as a thread */
   while(1){
     instance_run();
 //    aosThdUSleep(10);
+//    aosThdMSleep(1);
   }
 
 #endif  /* defined(UNIT_TEST_SNIPPETS_DW1000) */
