Revision 26dead12 source/DW1000/v1/alld_dw1000_v1.c

View differences:

source/DW1000/v1/alld_dw1000_v1.c
32 32
#if (defined(AMIROLLD_CFG_DW1000) && (AMIROLLD_CFG_DW1000 == 1)) || defined(__DOXYGEN__)
33 33

  
34 34
#include <v1/alld_dw1000_regs_v1.h>
35
#include <aos_thread.h>
36 35
#include <assert.h>
37 36
#include <string.h>
38 37
#include <stdlib.h>
......
241 240
};
242 241

  
243 242

  
244
#define NUM_16M_OFFSET  (37)
245
#define NUM_16M_OFFSETWB  (68)
246
#define NUM_64M_OFFSET  (26)
247
#define NUM_64M_OFFSETWB  (59)
248

  
249 243
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
250 244
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
251 245

  
......
845 839
}; // end range25cm64PRFwb
846 840

  
847 841

  
848

  
849

  
850

  
851 842
/*! ------------------------------------------------------------------------------------------------------------------
852 843
 * Function: dwt_getrangebias()
853 844
 *
......
873 864

  
874 865
    // NB: note we may get some small negitive values e.g. up to -50 cm.
875 866

  
876
    int rangeint25cm = (int) (range * 4.00) ;       // convert range to integer number of 25cm values.
867
    int rangeint25cm = (int) ((double)range * 4.00) ;       // convert range to integer number of 25cm values.
877 868

  
878 869
    if (rangeint25cm > 255) rangeint25cm = 255 ;    // make sure it matches largest value in table (all tables end in 255 !!!!)
879 870

  
......
919 910
    } // end else
920 911

  
921 912

  
922
    mOffset = (float) cmoffseti ;                                       // offset result in centimmetres
913
    mOffset = (double) cmoffseti ;                                       // offset result in centimmetres
923 914

  
924 915
    mOffset *= 0.01 ;                                                   // convert to metres
925 916

  
926 917
    return (mOffset) ;
927 918
}
928 919

  
929

  
930

  
931 920
// -------------------------------------------------------------------------------------------------------------------
932 921
//
933 922
// Internal functions for controlling and configuring the device
......
943 932
// Read non-volatile memory
944 933
uint32_t _dwt_otpread(uint32_t address);
945 934
// Program the non-volatile memory
946
uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address);
935
int32_t _dwt_otpprogword32(uint32_t data, uint16_t address);
947 936
// Upload the device configuration into always on memory
948 937
void _dwt_aonarrayupload(void);
949 938
// -------------------------------------------------------------------------------------------------------------------
......
1025 1014
    pdw1000local->cbRxTo = NULL;
1026 1015
    pdw1000local->cbRxErr = NULL;
1027 1016

  
1028
    pdw1000local->driver = drv;  // TODO:
1017
    pdw1000local->driver = drv;
1029 1018

  
1030 1019
    // Read and validate device ID return -1 if not recognised
1031 1020
    if (DWT_DEVICE_ID != dwt_readdevid()) // MP IC ONLY (i.e. DW1000) FOR THIS CODE
......
1346 1335
    pdw1000local->longFrames = config->phrMode ;
1347 1336

  
1348 1337
    pdw1000local->sysCFGreg &= ~SYS_CFG_PHR_MODE_11;
1349
    pdw1000local->sysCFGreg |= (SYS_CFG_PHR_MODE_11 & (config->phrMode << SYS_CFG_PHR_MODE_SHFT));
1338
    pdw1000local->sysCFGreg |= (SYS_CFG_PHR_MODE_11 & (uint32_t)(config->phrMode << SYS_CFG_PHR_MODE_SHFT));
1350 1339

  
1351 1340
    dwt_write32bitreg(SYS_CFG_ID,pdw1000local->sysCFGreg) ;
1352 1341
    // Set the lde_replicaCoeff
......
1413 1402
        nsSfd_result = 3 ;
1414 1403
        useDWnsSFD = 1 ;
1415 1404
    }
1416
    regval =  (CHAN_CTRL_TX_CHAN_MASK & (chan << CHAN_CTRL_TX_CHAN_SHIFT)) | // Transmit Channel
1417
              (CHAN_CTRL_RX_CHAN_MASK & (chan << CHAN_CTRL_RX_CHAN_SHIFT)) | // Receive Channel
1418
              (CHAN_CTRL_RXFPRF_MASK & (config->prf << CHAN_CTRL_RXFPRF_SHIFT)) | // RX PRF
1419
              ((CHAN_CTRL_TNSSFD|CHAN_CTRL_RNSSFD) & (nsSfd_result << CHAN_CTRL_TNSSFD_SHIFT)) | // nsSFD enable RX&TX
1420
              (CHAN_CTRL_DWSFD & (useDWnsSFD << CHAN_CTRL_DWSFD_SHIFT)) | // Use DW nsSFD
1421
              (CHAN_CTRL_TX_PCOD_MASK & (config->txCode << CHAN_CTRL_TX_PCOD_SHIFT)) | // TX Preamble Code
1422
              (CHAN_CTRL_RX_PCOD_MASK & (config->rxCode << CHAN_CTRL_RX_PCOD_SHIFT)) ; // RX Preamble Code
1405
    regval =  (CHAN_CTRL_TX_CHAN_MASK & (uint32_t)(chan << CHAN_CTRL_TX_CHAN_SHIFT)) | // Transmit Channel
1406
              (CHAN_CTRL_RX_CHAN_MASK & (uint32_t)(chan << CHAN_CTRL_RX_CHAN_SHIFT)) | // Receive Channel
1407
              (CHAN_CTRL_RXFPRF_MASK & (uint32_t)(config->prf << CHAN_CTRL_RXFPRF_SHIFT)) | // RX PRF
1408
              ((CHAN_CTRL_TNSSFD|CHAN_CTRL_RNSSFD) & (uint32_t)(nsSfd_result << CHAN_CTRL_TNSSFD_SHIFT)) | // nsSFD enable RX&TX
1409
              (CHAN_CTRL_DWSFD & (uint32_t)(useDWnsSFD << CHAN_CTRL_DWSFD_SHIFT)) | // Use DW nsSFD
1410
              (CHAN_CTRL_TX_PCOD_MASK & (uint32_t)(config->txCode << CHAN_CTRL_TX_PCOD_SHIFT)) | // TX Preamble Code
1411
              (CHAN_CTRL_RX_PCOD_MASK & (uint32_t)(config->rxCode << CHAN_CTRL_RX_PCOD_SHIFT)) ; // RX Preamble Code
1423 1412

  
1424 1413
    dwt_write32bitreg(CHAN_CTRL_ID,regval) ;
1425 1414

  
1426 1415
    // Set up TX Preamble Size, PRF and Data Rate
1427
    pdw1000local->txFCTRL = ((config->txPreambLength | config->prf) << TX_FCTRL_TXPRF_SHFT) | (config->dataRate << TX_FCTRL_TXBR_SHFT);
1416
    pdw1000local->txFCTRL = (uint32_t)(((config->txPreambLength | config->prf) << TX_FCTRL_TXPRF_SHFT) | (config->dataRate << TX_FCTRL_TXBR_SHFT));
1428 1417
    dwt_write32bitreg(TX_FCTRL_ID, pdw1000local->txFCTRL);
1429 1418

  
1430 1419
    // 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 1527

  
1539 1528
    // Write the frame length to the TX frame control register
1540 1529
    // pdw1000local->txFCTRL has kept configured bit rate information
1541
    uint32_t reg32 = pdw1000local->txFCTRL | txFrameLength | (txBufferOffset << TX_FCTRL_TXBOFFS_SHFT) | (ranging << TX_FCTRL_TR_SHFT);
1530
    uint32_t reg32 = pdw1000local->txFCTRL | txFrameLength | (uint16_t)(txBufferOffset << TX_FCTRL_TXBOFFS_SHFT) | (uint16_t)(ranging << TX_FCTRL_TR_SHFT);
1542 1531
    dwt_write32bitreg(TX_FCTRL_ID, reg32);
1543 1532
} // end dwt_writetxfctrl()
1544 1533

  
......
1789 1778
    // Write message header selecting WRITE operation and addresses as appropriate (this is one to three bytes long)
1790 1779
    if (index == 0) // For index of 0, no sub-index is required
1791 1780
    {
1792
        header[cnt++] = 0x80 | recordNumber ; // Bit-7 is WRITE operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
1781
        header[cnt++] = 0x80 | (uint8_t)recordNumber ; // Bit-7 is WRITE operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
1793 1782
    }
1794 1783
    else
1795 1784
    {
1796 1785
#ifdef DWT_API_ERROR_CHECK
1797 1786
        assert((index <= 0x7FFF) && ((index + length) <= 0x7FFF)); // Index and sub-addressable area are limited to 15-bits.
1798 1787
#endif
1799
        header[cnt++] = 0xC0 | recordNumber ; // Bit-7 is WRITE operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
1788
        header[cnt++] = 0xC0 | (uint8_t)recordNumber ; // Bit-7 is WRITE operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
1800 1789

  
1801 1790
        if (index <= 127) // For non-zero index < 127, just a single sub-index byte is required
1802 1791
        {
......
1810 1799
    }
1811 1800

  
1812 1801
    // Write it to the SPI
1813
    writetospi(cnt,header,length,buffer);
1802
    writetospi((uint16_t)cnt,header,length,buffer);
1814 1803
} // end dwt_writetodevice()
1815 1804

  
1816 1805
/*! ------------------------------------------------------------------------------------------------------------------
......
1874 1863
    }
1875 1864

  
1876 1865
    // Do the read from the SPI
1877
    readfromspi(cnt, header, length, buffer);
1866
    readfromspi((uint16_t)cnt, header, length, buffer);
1878 1867
} // end dwt_readfromdevice()
1879 1868

  
1880 1869

  
......
1898 1887
    int     j ;
1899 1888
    uint8_t   buffer[4] ;
1900 1889

  
1901
    dwt_readfromdevice(regFileID,regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer
1890
    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer
1902 1891

  
1903 1892
    for (j = 3 ; j >= 0 ; j --)
1904 1893
    {
......
1926 1915
    uint16_t  regval = 0 ;
1927 1916
    uint8_t   buffer[2] ;
1928 1917

  
1929
    dwt_readfromdevice(regFileID,regOffset,2,buffer); // Read 2 bytes (16-bits) register into buffer
1918
    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset,2,buffer); // Read 2 bytes (16-bits) register into buffer
1930 1919

  
1931
    regval = (buffer[1] << 8) + buffer[0] ;
1920
    regval = (uint16_t)((buffer[1] << 8) + buffer[0] );
1932 1921
    return regval ;
1933 1922

  
1934 1923
} // end dwt_read16bitoffsetreg()
......
1950 1939
{
1951 1940
    uint8_t regval;
1952 1941

  
1953
    dwt_readfromdevice(regFileID, regOffset, 1, &regval);
1942
    dwt_readfromdevice((uint16_t)regFileID, (uint16_t)regOffset, 1, &regval);
1954 1943

  
1955 1944
    return regval ;
1956 1945
}
......
1971 1960
 */
1972 1961
void dwt_write8bitoffsetreg(int regFileID, int regOffset, uint8_t regval)
1973 1962
{
1974
    dwt_writetodevice(regFileID, regOffset, 1, &regval);
1963
    dwt_writetodevice((uint16_t)regFileID, (uint16_t)regOffset, 1, &regval);
1975 1964
}
1976 1965

  
1977 1966
/*! ------------------------------------------------------------------------------------------------------------------
......
1995 1984
    buffer[0] = regval & 0xFF;
1996 1985
    buffer[1] = regval >> 8 ;
1997 1986

  
1998
    dwt_writetodevice(regFileID,regOffset,2,buffer);
1987
    dwt_writetodevice((uint16_t)regFileID, (uint16_t)regOffset,2,buffer);
1999 1988
} // end dwt_write16bitoffsetreg()
2000 1989

  
2001 1990
/*! ------------------------------------------------------------------------------------------------------------------
......
2023 2012
        regval >>= 8 ;
2024 2013
    }
2025 2014

  
2026
    dwt_writetodevice(regFileID,regOffset,4,buffer);
2015
    dwt_writetodevice((uint16_t)regFileID,(uint16_t)regOffset,4,buffer);
2027 2016
} // end dwt_write32bitoffsetreg()
2028 2017

  
2029 2018
/*! ------------------------------------------------------------------------------------------------------------------
......
2157 2146

  
2158 2147
    for(i=0; i<length; i++)
2159 2148
    {
2160
        array[i] = _dwt_otpread(address + i) ;
2149
        array[i] = _dwt_otpread(address + (uint32_t)i) ;
2161 2150
    }
2162 2151

  
2163 2152
    _dwt_enableclocks(ENABLE_ALL_SEQ); // Restore system clock to PLL
......
2182 2171
    uint32_t ret_data;
2183 2172

  
2184 2173
    // Write the address
2185
    dwt_write16bitoffsetreg(OTP_IF_ID, OTP_ADDR, address);
2174
    dwt_write16bitoffsetreg(OTP_IF_ID, OTP_ADDR, (uint16_t)address);
2186 2175

  
2187 2176
    // Perform OTP Read - Manual read mode has to be set
2188 2177
    dwt_write8bitoffsetreg(OTP_IF_ID, OTP_CTRL, OTP_CTRL_OTPREAD | OTP_CTRL_OTPRDEN);
......
2215 2204
 *
2216 2205
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
2217 2206
 */
2218
uint32_t _dwt_otpsetmrregs(int mode)
2207
int32_t _dwt_otpsetmrregs(int mode)
2219 2208
{
2220 2209
    uint8_t rd_buf[4];
2221 2210
    uint8_t wr_buf[4];
......
2381 2370
 *
2382 2371
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
2383 2372
 */
2384
uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address)
2373
int32_t _dwt_otpprogword32(uint32_t data, uint16_t address)
2385 2374
{
2386 2375
    uint8_t rd_buf[1];
2387 2376
    uint8_t wr_buf[4];
......
3538 3527
    if (enable)
3539 3528
    {
3540 3529
        /* Configure ON/OFF times and enable PLL2 on/off sequencing by SNIFF mode. */
3541
        uint16_t sniff_reg = ((timeOff << 8) | timeOn) & RX_SNIFF_MASK;
3530
        uint16_t sniff_reg = (uint16_t)((timeOff << 8) | timeOn) & RX_SNIFF_MASK;
3542 3531
        dwt_write16bitoffsetreg(RX_SNIFF_ID, RX_SNIFF_OFFSET, sniff_reg);
3543 3532
        pmsc_reg = dwt_read32bitoffsetreg(PMSC_ID, PMSC_CTRL0_OFFSET);
3544 3533
        pmsc_reg |= PMSC_CTRL0_PLL2_SEQ_EN;
......
4099 4088
    wr_buf[0] = 0x00; // Clear SAR enable
4100 4089
    dwt_writetodevice(TX_CAL_ID, TC_SARL_SAR_C,1,wr_buf);
4101 4090

  
4102
    return ((temp_raw<<8)|(vbat_raw));
4091
    return (uint16_t)((temp_raw<<8)|(vbat_raw));
4103 4092
}
4104 4093

  
4105 4094
/*! ------------------------------------------------------------------------------------------------------------------
......
4198 4187
        curr_bw = curr_bw | bit_field;
4199 4188

  
4200 4189
        // Write bw setting to PG_DELAY register
4201
        dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGDELAY_OFFSET, curr_bw);
4190
        dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGDELAY_OFFSET,(uint8_t)curr_bw);
4202 4191

  
4203 4192
        // Set cal direction and time
4204 4193
        dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGCCTRL_OFFSET, TC_PGCCTRL_DIR_CONV | TC_PGCCTRL_TMEAS_MASK);
......
4284 4273
            }
4285 4274
        }
4286 4275

  
4287
        new_da_attn = current_da_attn + da_attn_change;
4288
        new_mixer_gain = current_mixer_gain + mixer_gain_change;
4276
        new_da_attn = current_da_attn + (uint8_t)da_attn_change;
4277
        new_mixer_gain = current_mixer_gain + (uint8_t)mixer_gain_change;
4289 4278

  
4290 4279
        new_regval |= ((uint32_t) ((new_da_attn << 5) | new_mixer_gain)) << (i * 8);
4291 4280
    }
......
4394 4383
    dwt_write16bitoffsetreg(PMSC_ID, PMSC_CTRL1_OFFSET, old_pmsc_ctrl1);
4395 4384
    dwt_write32bitreg(RF_CONF_ID, old_rf_conf_txpow_mask);
4396 4385

  
4397
    average_count = (int)(sum_count / NUM_SAMPLES);
4386
    average_count = (uint16_t)(sum_count / NUM_SAMPLES);
4398 4387
    return average_count;
4399 4388
}
4400 4389

  
......
4405 4394

  
4406 4395
    0xDECA0130                               // DW1000 - MP
4407 4396

  
4408
   ===============================================================================================
4409
*/
4397
   =============================================================================================== */
4410 4398

  
4411 4399
/****************************************************************************************************************************************************
4412 4400
 *
......
4429 4417
 */
4430 4418
#pragma GCC optimize ("O3")
4431 4419
static int writetospi(uint16_t headerLength,
4432
               const	uint8_t *headerBuffer,
4433
               uint32_t bodyLength,
4434
               const	uint8_t *bodyBuffer)
4435
{
4436

  
4437
    uint8_t buffer[bodyLength + 3];
4438
    memcpy(buffer, headerBuffer, headerLength); //copy data to buffer
4439
    memcpy(&buffer[headerLength], bodyBuffer, bodyLength); //copy data to buffer
4440

  
4441
    apalSPITransmit(pdw1000local->driver->spid,
4442
                    buffer,
4443
                    bodyLength + headerLength); // send header and data
4444

  
4445
    return 0;
4420
                      const	uint8_t *headerBuffer,
4421
                      uint32_t bodyLength,
4422
                      const	uint8_t *bodyBuffer)
4423
{   
4424
  uint8_t buffer[SPIBUFFLEN];
4425
  memcpy(buffer, headerBuffer, headerLength); //copy header (register id no.) to the buffer
4426
  memcpy(&buffer[headerLength], bodyBuffer, bodyLength); //copy data to the buffer
4427

  
4428
  apalSPITransmit(pdw1000local->driver->spid,
4429
                  buffer,
4430
                  bodyLength + headerLength); // send header (register id) and data
4431

  
4432
  return 0;
4446 4433
} // end writetospi()
4447 4434

  
4448 4435

  
......
4533 4520
/*! @brief sleep or idle the thread in millisecond */
4534 4521
void deca_sleep(unsigned int time_ms)
4535 4522
{
4536
  aosThdMSleep(time_ms);
4523
  usleep(time_ms * 1000);
4537 4524
}
4538 4525

  
4539 4526
/*! @brief sleep or idle the thread in millisecond */
4540 4527
void Sleep(unsigned int time_ms)
4541 4528
{
4542
  aosThdMSleep(time_ms);
4529
  usleep(time_ms * 1000);
4543 4530
}
4544 4531

  
4545 4532

  
......
4552 4539
  return chVTGetSystemTimeX();
4553 4540
}
4554 4541

  
4555
//inline uint32_t portGetTickCnt(){
4556
//    return (uint32_t) chVTGetSystemTimeX();
4557
//}
4558

  
4559

  
4560 4542
/*! @brief Disable the interrupt handler */
4561 4543
void port_DisableEXT_IRQ(void){   
4562 4544
  nvicDisableVector(DW1000_EXTI_IRQn);
4563

  
4564 4545
}
4565 4546

  
4566 4547
/*! @brief Enable the interrupt handler */

Also available in: Unified diff