commit 26dead12185b7721d47c45900bebd6dda6ff4ea7
Author: Cung Sang <csang@techfak.uni-bielefeld.de>
Date:   Tue Feb 12 11:20:39 2019 +0100

    DW1000 LLD updated

diff --git a/source/DW1000/v1/alld_dw1000_v1.c b/source/DW1000/v1/alld_dw1000_v1.c
index 571dd47..d7c82bc 100755
--- a/source/DW1000/v1/alld_dw1000_v1.c
+++ b/source/DW1000/v1/alld_dw1000_v1.c
@@ -32,7 +32,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #if (defined(AMIROLLD_CFG_DW1000) && (AMIROLLD_CFG_DW1000 == 1)) || defined(__DOXYGEN__)
 
 #include <v1/alld_dw1000_regs_v1.h>
-#include <aos_thread.h>
 #include <assert.h>
 #include <string.h>
 #include <stdlib.h>
@@ -241,11 +240,6 @@ const double txpwr_compensation[NUM_CH] = {
 };
 
 
-#define NUM_16M_OFFSET  (37)
-#define NUM_16M_OFFSETWB  (68)
-#define NUM_64M_OFFSET  (26)
-#define NUM_64M_OFFSETWB  (59)
-
 const uint8_t chan_idxnb[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 0, 3, 0, 0}; //only channels 1,2,3 and 5 are in the narrow band tables
 const uint8_t chan_idxwb[NUM_CH_SUPPORTED] = {0, 0, 0, 0, 0, 0, 0, 1}; //only channels 4 and 7 are in in the wide band tables
 
@@ -845,9 +839,6 @@ const uint8_t range25cm64PRFwb[2][NUM_64M_OFFSETWB] =
 }; // end range25cm64PRFwb
 
 
-
-
-
 /*! ------------------------------------------------------------------------------------------------------------------
  * Function: dwt_getrangebias()
  *
@@ -873,7 +864,7 @@ double dwt_getrangebias(uint8_t chan, float range, uint8_t prf)
 
     // NB: note we may get some small negitive values e.g. up to -50 cm.
 
-    int rangeint25cm = (int) (range * 4.00) ;       // convert range to integer number of 25cm values.
+    int rangeint25cm = (int) ((double)range * 4.00) ;       // convert range to integer number of 25cm values.
 
     if (rangeint25cm > 255) rangeint25cm = 255 ;    // make sure it matches largest value in table (all tables end in 255 !!!!)
 
@@ -919,15 +910,13 @@ double dwt_getrangebias(uint8_t chan, float range, uint8_t prf)
     } // end else
 
 
-    mOffset = (float) cmoffseti ;                                       // offset result in centimmetres
+    mOffset = (double) cmoffseti ;                                       // offset result in centimmetres
 
     mOffset *= 0.01 ;                                                   // convert to metres
 
     return (mOffset) ;
 }
 
-
-
 // -------------------------------------------------------------------------------------------------------------------
 //
 // Internal functions for controlling and configuring the device
@@ -943,7 +932,7 @@ void _dwt_loaducodefromrom(void);
 // Read non-volatile memory
 uint32_t _dwt_otpread(uint32_t address);
 // Program the non-volatile memory
-uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address);
+int32_t _dwt_otpprogword32(uint32_t data, uint16_t address);
 // Upload the device configuration into always on memory
 void _dwt_aonarrayupload(void);
 // -------------------------------------------------------------------------------------------------------------------
@@ -1025,7 +1014,7 @@ int dwt_initialise(const uint16_t config, DW1000Driver* drv)
     pdw1000local->cbRxTo = NULL;
     pdw1000local->cbRxErr = NULL;
 
-    pdw1000local->driver = drv;  // TODO:
+    pdw1000local->driver = drv;
 
     // Read and validate device ID return -1 if not recognised
     if (DWT_DEVICE_ID != dwt_readdevid()) // MP IC ONLY (i.e. DW1000) FOR THIS CODE
@@ -1346,7 +1335,7 @@ void dwt_configure(dwt_config_t *config)
     pdw1000local->longFrames = config->phrMode ;
 
     pdw1000local->sysCFGreg &= ~SYS_CFG_PHR_MODE_11;
-    pdw1000local->sysCFGreg |= (SYS_CFG_PHR_MODE_11 & (config->phrMode << SYS_CFG_PHR_MODE_SHFT));
+    pdw1000local->sysCFGreg |= (SYS_CFG_PHR_MODE_11 & (uint32_t)(config->phrMode << SYS_CFG_PHR_MODE_SHFT));
 
     dwt_write32bitreg(SYS_CFG_ID,pdw1000local->sysCFGreg) ;
     // Set the lde_replicaCoeff
@@ -1413,18 +1402,18 @@ void dwt_configure(dwt_config_t *config)
         nsSfd_result = 3 ;
         useDWnsSFD = 1 ;
     }
-    regval =  (CHAN_CTRL_TX_CHAN_MASK & (chan << CHAN_CTRL_TX_CHAN_SHIFT)) | // Transmit Channel
-              (CHAN_CTRL_RX_CHAN_MASK & (chan << CHAN_CTRL_RX_CHAN_SHIFT)) | // Receive Channel
-              (CHAN_CTRL_RXFPRF_MASK & (config->prf << CHAN_CTRL_RXFPRF_SHIFT)) | // RX PRF
-              ((CHAN_CTRL_TNSSFD|CHAN_CTRL_RNSSFD) & (nsSfd_result << CHAN_CTRL_TNSSFD_SHIFT)) | // nsSFD enable RX&TX
-              (CHAN_CTRL_DWSFD & (useDWnsSFD << CHAN_CTRL_DWSFD_SHIFT)) | // Use DW nsSFD
-              (CHAN_CTRL_TX_PCOD_MASK & (config->txCode << CHAN_CTRL_TX_PCOD_SHIFT)) | // TX Preamble Code
-              (CHAN_CTRL_RX_PCOD_MASK & (config->rxCode << CHAN_CTRL_RX_PCOD_SHIFT)) ; // RX Preamble Code
+    regval =  (CHAN_CTRL_TX_CHAN_MASK & (uint32_t)(chan << CHAN_CTRL_TX_CHAN_SHIFT)) | // Transmit Channel
+              (CHAN_CTRL_RX_CHAN_MASK & (uint32_t)(chan << CHAN_CTRL_RX_CHAN_SHIFT)) | // Receive Channel
+              (CHAN_CTRL_RXFPRF_MASK & (uint32_t)(config->prf << CHAN_CTRL_RXFPRF_SHIFT)) | // RX PRF
+              ((CHAN_CTRL_TNSSFD|CHAN_CTRL_RNSSFD) & (uint32_t)(nsSfd_result << CHAN_CTRL_TNSSFD_SHIFT)) | // nsSFD enable RX&TX
+              (CHAN_CTRL_DWSFD & (uint32_t)(useDWnsSFD << CHAN_CTRL_DWSFD_SHIFT)) | // Use DW nsSFD
+              (CHAN_CTRL_TX_PCOD_MASK & (uint32_t)(config->txCode << CHAN_CTRL_TX_PCOD_SHIFT)) | // TX Preamble Code
+              (CHAN_CTRL_RX_PCOD_MASK & (uint32_t)(config->rxCode << CHAN_CTRL_RX_PCOD_SHIFT)) ; // RX Preamble Code
 
     dwt_write32bitreg(CHAN_CTRL_ID,regval) ;
 
     // Set up TX Preamble Size, PRF and Data Rate
-    pdw1000local->txFCTRL = ((config->txPreambLength | config->prf) << TX_FCTRL_TXPRF_SHFT) | (config->dataRate << TX_FCTRL_TXBR_SHFT);
+    pdw1000local->txFCTRL = (uint32_t)(((config->txPreambLength | config->prf) << TX_FCTRL_TXPRF_SHFT) | (config->dataRate << TX_FCTRL_TXBR_SHFT));
     dwt_write32bitreg(TX_FCTRL_ID, pdw1000local->txFCTRL);
 
     // The SFD transmit pattern is initialised by the DW1000 upon a user TX request, but (due to an IC issue) it is not done for an auto-ACK TX. The
@@ -1538,7 +1527,7 @@ void dwt_writetxfctrl(uint16_t txFrameLength, uint16_t txBufferOffset, int rangi
 
     // Write the frame length to the TX frame control register
     // pdw1000local->txFCTRL has kept configured bit rate information
-    uint32_t reg32 = pdw1000local->txFCTRL | txFrameLength | (txBufferOffset << TX_FCTRL_TXBOFFS_SHFT) | (ranging << TX_FCTRL_TR_SHFT);
+    uint32_t reg32 = pdw1000local->txFCTRL | txFrameLength | (uint16_t)(txBufferOffset << TX_FCTRL_TXBOFFS_SHFT) | (uint16_t)(ranging << TX_FCTRL_TR_SHFT);
     dwt_write32bitreg(TX_FCTRL_ID, reg32);
 } // end dwt_writetxfctrl()
 
@@ -1789,14 +1778,14 @@ void dwt_writetodevice
     // Write message header selecting WRITE operation and addresses as appropriate (this is one to three bytes long)
     if (index == 0) // For index of 0, no sub-index is required
     {
-        header[cnt++] = 0x80 | recordNumber ; // Bit-7 is WRITE operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
+        header[cnt++] = 0x80 | (uint8_t)recordNumber ; // Bit-7 is WRITE operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
     }
     else
     {
 #ifdef DWT_API_ERROR_CHECK
         assert((index <= 0x7FFF) && ((index + length) <= 0x7FFF)); // Index and sub-addressable area are limited to 15-bits.
 #endif
-        header[cnt++] = 0xC0 | recordNumber ; // Bit-7 is WRITE operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
+        header[cnt++] = 0xC0 | (uint8_t)recordNumber ; // Bit-7 is WRITE operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
 
         if (index <= 127) // For non-zero index < 127, just a single sub-index byte is required
         {
@@ -1810,7 +1799,7 @@ void dwt_writetodevice
     }
 
     // Write it to the SPI
-    writetospi(cnt,header,length,buffer);
+    writetospi((uint16_t)cnt,header,length,buffer);
 } // end dwt_writetodevice()
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -1874,7 +1863,7 @@ void dwt_readfromdevice
     }
 
     // Do the read from the SPI
-    readfromspi(cnt, header, length, buffer);
+    readfromspi((uint16_t)cnt, header, length, buffer);
 } // end dwt_readfromdevice()
 
 
@@ -1898,7 +1887,7 @@ uint32_t dwt_read32bitoffsetreg(int regFileID,int regOffset)
     int     j ;
     uint8_t   buffer[4] ;
 
-    dwt_readfromdevice(regFileID,regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer
+    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer
 
     for (j = 3 ; j >= 0 ; j --)
     {
@@ -1926,9 +1915,9 @@ uint16_t dwt_read16bitoffsetreg(int regFileID,int regOffset)
     uint16_t  regval = 0 ;
     uint8_t   buffer[2] ;
 
-    dwt_readfromdevice(regFileID,regOffset,2,buffer); // Read 2 bytes (16-bits) register into buffer
+    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset,2,buffer); // Read 2 bytes (16-bits) register into buffer
 
-    regval = (buffer[1] << 8) + buffer[0] ;
+    regval = (uint16_t)((buffer[1] << 8) + buffer[0] );
     return regval ;
 
 } // end dwt_read16bitoffsetreg()
@@ -1950,7 +1939,7 @@ uint8_t dwt_read8bitoffsetreg(int regFileID, int regOffset)
 {
     uint8_t regval;
 
-    dwt_readfromdevice(regFileID, regOffset, 1, &regval);
+    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset, 1, &regval);
 
     return regval ;
 }
@@ -1971,7 +1960,7 @@ uint8_t dwt_read8bitoffsetreg(int regFileID, int regOffset)
  */
 void dwt_write8bitoffsetreg(int regFileID, int regOffset, uint8_t regval)
 {
-    dwt_writetodevice(regFileID, regOffset, 1, &regval);
+    dwt_writetodevice((uint16_t)regFileID, (uint16_t)regOffset, 1, &regval);
 }
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -1995,7 +1984,7 @@ void dwt_write16bitoffsetreg(int regFileID,int regOffset,uint16_t regval)
     buffer[0] = regval & 0xFF;
     buffer[1] = regval >> 8 ;
 
-    dwt_writetodevice(regFileID,regOffset,2,buffer);
+    dwt_writetodevice((uint16_t)regFileID, (uint16_t)regOffset,2,buffer);
 } // end dwt_write16bitoffsetreg()
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -2023,7 +2012,7 @@ void dwt_write32bitoffsetreg(int regFileID,int regOffset,uint32_t regval)
         regval >>= 8 ;
     }
 
-    dwt_writetodevice(regFileID,regOffset,4,buffer);
+    dwt_writetodevice((uint16_t)regFileID,(uint16_t)regOffset,4,buffer);
 } // end dwt_write32bitoffsetreg()
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -2157,7 +2146,7 @@ void dwt_otpread(uint32_t address, uint32_t *array, uint8_t length)
 
     for(i=0; i<length; i++)
     {
-        array[i] = _dwt_otpread(address + i) ;
+        array[i] = _dwt_otpread(address + (uint32_t)i) ;
     }
 
     _dwt_enableclocks(ENABLE_ALL_SEQ); // Restore system clock to PLL
@@ -2182,7 +2171,7 @@ uint32_t _dwt_otpread(uint32_t address)
     uint32_t ret_data;
 
     // Write the address
-    dwt_write16bitoffsetreg(OTP_IF_ID, OTP_ADDR, address);
+    dwt_write16bitoffsetreg(OTP_IF_ID, OTP_ADDR, (uint16_t)address);
 
     // Perform OTP Read - Manual read mode has to be set
     dwt_write8bitoffsetreg(OTP_IF_ID, OTP_CTRL, OTP_CTRL_OTPREAD | OTP_CTRL_OTPRDEN);
@@ -2215,7 +2204,7 @@ uint32_t _dwt_otpread(uint32_t address)
  *
  * returns DWT_SUCCESS for success, or DWT_ERROR for error
  */
-uint32_t _dwt_otpsetmrregs(int mode)
+int32_t _dwt_otpsetmrregs(int mode)
 {
     uint8_t rd_buf[4];
     uint8_t wr_buf[4];
@@ -2381,7 +2370,7 @@ uint32_t _dwt_otpsetmrregs(int mode)
  *
  * returns DWT_SUCCESS for success, or DWT_ERROR for error
  */
-uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address)
+int32_t _dwt_otpprogword32(uint32_t data, uint16_t address)
 {
     uint8_t rd_buf[1];
     uint8_t wr_buf[4];
@@ -3538,7 +3527,7 @@ void dwt_setsniffmode(int enable, uint8_t timeOn, uint8_t timeOff)
     if (enable)
     {
         /* Configure ON/OFF times and enable PLL2 on/off sequencing by SNIFF mode. */
-        uint16_t sniff_reg = ((timeOff << 8) | timeOn) & RX_SNIFF_MASK;
+        uint16_t sniff_reg = (uint16_t)((timeOff << 8) | timeOn) & RX_SNIFF_MASK;
         dwt_write16bitoffsetreg(RX_SNIFF_ID, RX_SNIFF_OFFSET, sniff_reg);
         pmsc_reg = dwt_read32bitoffsetreg(PMSC_ID, PMSC_CTRL0_OFFSET);
         pmsc_reg |= PMSC_CTRL0_PLL2_SEQ_EN;
@@ -4099,7 +4088,7 @@ uint16_t dwt_readtempvbat(uint8_t fastSPI)
     wr_buf[0] = 0x00; // Clear SAR enable
     dwt_writetodevice(TX_CAL_ID, TC_SARL_SAR_C,1,wr_buf);
 
-    return ((temp_raw<<8)|(vbat_raw));
+    return (uint16_t)((temp_raw<<8)|(vbat_raw));
 }
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -4198,7 +4187,7 @@ uint32_t dwt_calcbandwidthtempadj(uint16_t target_count)
         curr_bw = curr_bw | bit_field;
 
         // Write bw setting to PG_DELAY register
-        dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGDELAY_OFFSET, curr_bw);
+        dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGDELAY_OFFSET,(uint8_t)curr_bw);
 
         // Set cal direction and time
         dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGCCTRL_OFFSET, TC_PGCCTRL_DIR_CONV | TC_PGCCTRL_TMEAS_MASK);
@@ -4284,8 +4273,8 @@ uint32_t _dwt_computetxpowersetting(uint32_t ref_powerreg, int32_t power_adj)
             }
         }
 
-        new_da_attn = current_da_attn + da_attn_change;
-        new_mixer_gain = current_mixer_gain + mixer_gain_change;
+        new_da_attn = current_da_attn + (uint8_t)da_attn_change;
+        new_mixer_gain = current_mixer_gain + (uint8_t)mixer_gain_change;
 
         new_regval |= ((uint32_t) ((new_da_attn << 5) | new_mixer_gain)) << (i * 8);
     }
@@ -4394,7 +4383,7 @@ uint16_t dwt_calcpgcount(uint8_t pgdly)
     dwt_write16bitoffsetreg(PMSC_ID, PMSC_CTRL1_OFFSET, old_pmsc_ctrl1);
     dwt_write32bitreg(RF_CONF_ID, old_rf_conf_txpow_mask);
 
-    average_count = (int)(sum_count / NUM_SAMPLES);
+    average_count = (uint16_t)(sum_count / NUM_SAMPLES);
     return average_count;
 }
 
@@ -4405,8 +4394,7 @@ uint16_t dwt_calcpgcount(uint8_t pgdly)
 
     0xDECA0130                               // DW1000 - MP
 
-   ===============================================================================================
-*/
+   =============================================================================================== */
 
 /****************************************************************************************************************************************************
  *
@@ -4429,20 +4417,19 @@ uint16_t dwt_calcpgcount(uint8_t pgdly)
  */
 #pragma GCC optimize ("O3")
 static int writetospi(uint16_t headerLength,
-               const	uint8_t *headerBuffer,
-               uint32_t bodyLength,
-               const	uint8_t *bodyBuffer)
-{
-
-    uint8_t buffer[bodyLength + 3];
-    memcpy(buffer, headerBuffer, headerLength); //copy data to buffer
-    memcpy(&buffer[headerLength], bodyBuffer, bodyLength); //copy data to buffer
-
-    apalSPITransmit(pdw1000local->driver->spid,
-                    buffer,
-                    bodyLength + headerLength); // send header and data
-
-    return 0;
+                      const	uint8_t *headerBuffer,
+                      uint32_t bodyLength,
+                      const	uint8_t *bodyBuffer)
+{   
+  uint8_t buffer[SPIBUFFLEN];
+  memcpy(buffer, headerBuffer, headerLength); //copy header (register id no.) to the buffer
+  memcpy(&buffer[headerLength], bodyBuffer, bodyLength); //copy data to the buffer
+
+  apalSPITransmit(pdw1000local->driver->spid,
+                  buffer,
+                  bodyLength + headerLength); // send header (register id) and data
+
+  return 0;
 } // end writetospi()
 
 
@@ -4533,13 +4520,13 @@ void decamutexoff(decaIrqStatus_t s)
 /*! @brief sleep or idle the thread in millisecond */
 void deca_sleep(unsigned int time_ms)
 {
-  aosThdMSleep(time_ms);
+  usleep(time_ms * 1000);
 }
 
 /*! @brief sleep or idle the thread in millisecond */
 void Sleep(unsigned int time_ms)
 {
-  aosThdMSleep(time_ms);
+  usleep(time_ms * 1000);
 }
 
 
@@ -4552,15 +4539,9 @@ uint32_t portGetTickCnt(){
   return chVTGetSystemTimeX();
 }
 
-//inline uint32_t portGetTickCnt(){
-//    return (uint32_t) chVTGetSystemTimeX();
-//}
-
-
 /*! @brief Disable the interrupt handler */
 void port_DisableEXT_IRQ(void){   
   nvicDisableVector(DW1000_EXTI_IRQn);
-
 }
 
 /*! @brief Enable the interrupt handler */
