SDMP_IOT / Mbed OS AdiSense1000_SmartBabySeat

Fork of Babyseat_NewFirmware_copy_sean by Ross O'Halloran

Committer:
kevin1990
Date:
Fri Aug 25 12:52:52 2017 +0000
Revision:
3:3796776e2c27
Parent:
2:625a45555a85
adding function  test code to demonstrate printing calibration table

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kevin1990 3:3796776e2c27 1 /*!
kevin1990 3:3796776e2c27 2 ******************************************************************************
kevin1990 3:3796776e2c27 3 * @file: spi_nucleo.cpp
kevin1990 3:3796776e2c27 4 * @brief: ADISense1000 OS Dependant wrapper layer for spi
kevin1990 3:3796776e2c27 5 *-----------------------------------------------------------------------------
kevin1990 3:3796776e2c27 6 *
kevin1990 3:3796776e2c27 7 Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc.
kevin1990 3:3796776e2c27 8
kevin1990 3:3796776e2c27 9 All rights reserved.
kevin1990 3:3796776e2c27 10
kevin1990 3:3796776e2c27 11 Redistribution and use in source and binary forms, with or without modification,
kevin1990 3:3796776e2c27 12 are permitted provided that the following conditions are met:
kevin1990 3:3796776e2c27 13 - Redistributions of source code must retain the above copyright notice,
kevin1990 3:3796776e2c27 14 this list of conditions and the following disclaimer.
kevin1990 3:3796776e2c27 15 - Redistributions in binary form must reproduce the above copyright notice,
kevin1990 3:3796776e2c27 16 this list of conditions and the following disclaimer in the documentation
kevin1990 3:3796776e2c27 17 and/or other materials provided with the distribution.
kevin1990 3:3796776e2c27 18 - Modified versions of the software must be conspicuously marked as such.
kevin1990 3:3796776e2c27 19 - This software is licensed solely and exclusively for use with processors
kevin1990 3:3796776e2c27 20 manufactured by or for Analog Devices, Inc.
kevin1990 3:3796776e2c27 21 - This software may not be combined or merged with other code in any manner
kevin1990 3:3796776e2c27 22 that would cause the software to become subject to terms and conditions
kevin1990 3:3796776e2c27 23 which differ from those listed here.
kevin1990 3:3796776e2c27 24 - Neither the name of Analog Devices, Inc. nor the names of its
kevin1990 3:3796776e2c27 25 contributors may be used to endorse or promote products derived
kevin1990 3:3796776e2c27 26 from this software without specific prior written permission.
kevin1990 3:3796776e2c27 27 - The use of this software may or may not infringe the patent rights of one
kevin1990 3:3796776e2c27 28 or more patent holders. This license does not release you from the
kevin1990 3:3796776e2c27 29 requirement that you obtain separate licenses from these patent holders
kevin1990 3:3796776e2c27 30 to use this software.
kevin1990 3:3796776e2c27 31
kevin1990 3:3796776e2c27 32 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY
kevin1990 3:3796776e2c27 33 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
kevin1990 3:3796776e2c27 34 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
kevin1990 3:3796776e2c27 35 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
kevin1990 3:3796776e2c27 36 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
kevin1990 3:3796776e2c27 37 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
kevin1990 3:3796776e2c27 38 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kevin1990 3:3796776e2c27 39 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
kevin1990 3:3796776e2c27 40 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
kevin1990 3:3796776e2c27 41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
kevin1990 3:3796776e2c27 42 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kevin1990 3:3796776e2c27 43 *
kevin1990 3:3796776e2c27 44 *****************************************************************************/
kevin1990 3:3796776e2c27 45 /******************************************************************************/
kevin1990 3:3796776e2c27 46 /* Include Files */
kevin1990 3:3796776e2c27 47 /******************************************************************************/
kevin1990 3:3796776e2c27 48 #include "inc/spi_nucleo.h"
kevin1990 3:3796776e2c27 49
kevin1990 3:3796776e2c27 50 #define BUFFER_SIZE 256
kevin1990 3:3796776e2c27 51 #define CMD_BIT 7
kevin1990 3:3796776e2c27 52
kevin1990 3:3796776e2c27 53 #define SPI_MODE_0 0
kevin1990 3:3796776e2c27 54 #define SPI_FRAME_SIZE_8 8
kevin1990 3:3796776e2c27 55 #define SPI_CLOCK_FREQUENCY_700KHZ 700000
kevin1990 3:3796776e2c27 56 #define DEVICE_SPI_ASYNCH 1
kevin1990 3:3796776e2c27 57 #define TIMEOUT_3_SEC 3.0f
kevin1990 3:3796776e2c27 58
kevin1990 3:3796776e2c27 59 // DummyByte config defines
kevin1990 3:3796776e2c27 60 #define SPI_CHIP_TYPE_ADDR 0x03
kevin1990 3:3796776e2c27 61 #define SPI_CHIP_TYPE_VALUE 0x07
kevin1990 3:3796776e2c27 62 #define READ_CMD 0x00
kevin1990 3:3796776e2c27 63 #define MAX_SYNC_ATTEMPTS 50
kevin1990 3:3796776e2c27 64 #define DUMMY_BYTE_COUNT 1
kevin1990 3:3796776e2c27 65
kevin1990 3:3796776e2c27 66 #define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
kevin1990 3:3796776e2c27 67
kevin1990 3:3796776e2c27 68 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
kevin1990 3:3796776e2c27 69 DigitalOut chipSel(D10, 1);
kevin1990 3:3796776e2c27 70
kevin1990 3:3796776e2c27 71 event_callback_t callbck;
kevin1990 3:3796776e2c27 72 Timer timeout;
kevin1990 3:3796776e2c27 73
kevin1990 3:3796776e2c27 74 volatile int intType;
kevin1990 3:3796776e2c27 75 volatile bool cbkFired;
kevin1990 3:3796776e2c27 76
kevin1990 3:3796776e2c27 77 static uint8_t rxBuff[BUFFER_SIZE];
kevin1990 3:3796776e2c27 78
kevin1990 3:3796776e2c27 79 void callbackFired(int event)
kevin1990 3:3796776e2c27 80 {
kevin1990 3:3796776e2c27 81 cbkFired = true;
kevin1990 3:3796776e2c27 82 intType = event;
kevin1990 3:3796776e2c27 83 }
kevin1990 3:3796776e2c27 84
kevin1990 3:3796776e2c27 85 static int transferComplete()
kevin1990 3:3796776e2c27 86 {
kevin1990 3:3796776e2c27 87 int rc = 0;
kevin1990 3:3796776e2c27 88
kevin1990 3:3796776e2c27 89 timeout.start();
kevin1990 3:3796776e2c27 90 while ((cbkFired != true) && (intType != SPI_EVENT_COMPLETE))
kevin1990 3:3796776e2c27 91 {
kevin1990 3:3796776e2c27 92 if (timeout.read() > TIMEOUT_3_SEC)
kevin1990 3:3796776e2c27 93 {
kevin1990 3:3796776e2c27 94 rc = -1;
kevin1990 3:3796776e2c27 95 break;
kevin1990 3:3796776e2c27 96 }
kevin1990 3:3796776e2c27 97 }
kevin1990 3:3796776e2c27 98 timeout.stop();
kevin1990 3:3796776e2c27 99
kevin1990 3:3796776e2c27 100 cbkFired = false;
kevin1990 3:3796776e2c27 101 intType = 0;
kevin1990 3:3796776e2c27 102
kevin1990 3:3796776e2c27 103 return rc;
kevin1990 3:3796776e2c27 104 }
kevin1990 3:3796776e2c27 105
kevin1990 3:3796776e2c27 106 static int spiWrite(uint8_t *data, uint8_t txSize, bool bCsHold)
kevin1990 3:3796776e2c27 107 {
kevin1990 3:3796776e2c27 108 int rc;
kevin1990 3:3796776e2c27 109
kevin1990 3:3796776e2c27 110 // Assert chip select
kevin1990 3:3796776e2c27 111 if (!bCsHold)
kevin1990 3:3796776e2c27 112 {
kevin1990 3:3796776e2c27 113 chipSel = 0;
kevin1990 3:3796776e2c27 114 }
kevin1990 3:3796776e2c27 115
kevin1990 3:3796776e2c27 116 rc = spi.transfer(data, txSize, (uint8_t *)NULL, 0,
kevin1990 3:3796776e2c27 117 callbck, SPI_EVENT_COMPLETE);
kevin1990 3:3796776e2c27 118
kevin1990 3:3796776e2c27 119 rc = transferComplete();
kevin1990 3:3796776e2c27 120
kevin1990 3:3796776e2c27 121 // De assert chip select
kevin1990 3:3796776e2c27 122 if (!bCsHold)
kevin1990 3:3796776e2c27 123 {
kevin1990 3:3796776e2c27 124 chipSel = 1;
kevin1990 3:3796776e2c27 125 }
kevin1990 3:3796776e2c27 126
kevin1990 3:3796776e2c27 127 return rc;
kevin1990 3:3796776e2c27 128 }
kevin1990 3:3796776e2c27 129
kevin1990 3:3796776e2c27 130 static int spiRead(uint8_t *data, uint16_t rxSize, bool bCsHold)
kevin1990 3:3796776e2c27 131 {
kevin1990 3:3796776e2c27 132 int rc;
kevin1990 3:3796776e2c27 133
kevin1990 3:3796776e2c27 134 // Assert chip select
kevin1990 3:3796776e2c27 135 chipSel = 0;
kevin1990 3:3796776e2c27 136
kevin1990 3:3796776e2c27 137 // Send read command address
kevin1990 3:3796776e2c27 138 rc = spi.transfer(data, REG_CMD_SIZE, (uint8_t *)NULL, 0,
kevin1990 3:3796776e2c27 139 callbck, SPI_EVENT_COMPLETE);
kevin1990 3:3796776e2c27 140
kevin1990 3:3796776e2c27 141 rc = transferComplete();
kevin1990 3:3796776e2c27 142
kevin1990 3:3796776e2c27 143 if (rc < 0)
kevin1990 3:3796776e2c27 144 {
kevin1990 3:3796776e2c27 145 chipSel = 1;
kevin1990 3:3796776e2c27 146 return rc;
kevin1990 3:3796776e2c27 147 }
kevin1990 2:625a45555a85 148
kevin1990 3:3796776e2c27 149 for (int i = 0; i < DUMMY_BYTE_COUNT; i++) {
kevin1990 3:3796776e2c27 150 rc = spi.write(0);
kevin1990 3:3796776e2c27 151 }
kevin1990 3:3796776e2c27 152
kevin1990 3:3796776e2c27 153 // Retrieve data from slave
kevin1990 3:3796776e2c27 154 rc = spi.transfer((uint8_t *)NULL, rxSize, rxBuff, rxSize,
kevin1990 3:3796776e2c27 155 callbck, SPI_EVENT_COMPLETE);
kevin1990 3:3796776e2c27 156
kevin1990 3:3796776e2c27 157 rc = transferComplete();
kevin1990 3:3796776e2c27 158
kevin1990 3:3796776e2c27 159 if (rc < 0)
kevin1990 3:3796776e2c27 160 {
kevin1990 3:3796776e2c27 161 chipSel = 1;
kevin1990 3:3796776e2c27 162 return rc;
kevin1990 3:3796776e2c27 163 }
kevin1990 3:3796776e2c27 164
kevin1990 3:3796776e2c27 165 // De assert chip select
kevin1990 3:3796776e2c27 166 if (!bCsHold)
kevin1990 3:3796776e2c27 167 {
kevin1990 3:3796776e2c27 168 chipSel = 1;
kevin1990 3:3796776e2c27 169 }
kevin1990 3:3796776e2c27 170
kevin1990 3:3796776e2c27 171 return rc;
kevin1990 3:3796776e2c27 172 }
kevin1990 3:3796776e2c27 173
kevin1990 3:3796776e2c27 174 static int syncWithAdisense1000()
kevin1990 3:3796776e2c27 175 {
kevin1990 3:3796776e2c27 176 uint8_t txBuff[20];
kevin1990 3:3796776e2c27 177 uint8_t nRxLen = 1;
kevin1990 3:3796776e2c27 178 bool bCsHoldOff = false;
kevin1990 3:3796776e2c27 179 uint16_t nAttempts = 0;
kevin1990 3:3796776e2c27 180
kevin1990 3:3796776e2c27 181 memset(txBuff, 0, sizeof(txBuff));
kevin1990 3:3796776e2c27 182
kevin1990 3:3796776e2c27 183 txBuff[0] = READ_CMD;
kevin1990 3:3796776e2c27 184 txBuff[1] = SPI_CHIP_TYPE_ADDR;
kevin1990 3:3796776e2c27 185
kevin1990 3:3796776e2c27 186 while (rxBuff[0] != SPI_CHIP_TYPE_VALUE)
kevin1990 3:3796776e2c27 187 {
kevin1990 3:3796776e2c27 188 if(spiRead(txBuff, nRxLen, bCsHoldOff) < 0)
kevin1990 3:3796776e2c27 189 {
kevin1990 3:3796776e2c27 190 return -1;
kevin1990 3:3796776e2c27 191 }
kevin1990 3:3796776e2c27 192
kevin1990 3:3796776e2c27 193 if (nAttempts >= MAX_SYNC_ATTEMPTS)
kevin1990 3:3796776e2c27 194 {
kevin1990 3:3796776e2c27 195 return -1;
kevin1990 3:3796776e2c27 196 }
kevin1990 3:3796776e2c27 197
kevin1990 3:3796776e2c27 198 nAttempts++;
kevin1990 3:3796776e2c27 199 wait_ms(100);
kevin1990 3:3796776e2c27 200 }
kevin1990 3:3796776e2c27 201 /* TODO: Reminder not to forget about the following routine which was previously
kevin1990 3:3796776e2c27 202 used: Not valid currently as we are hardcoding the dummy byte count
kevin1990 3:3796776e2c27 203 This is a temporary fix until we have a defined spi protocall.
kevin1990 3:3796776e2c27 204 This function reads the spi chip type (fixed value) and determines how many
kevin1990 3:3796776e2c27 205 dummy bytes need to be sent in a spi read transaction.
kevin1990 3:3796776e2c27 206 The dummy bytes varies depending on clock speed, debug/release build on
kevin1990 3:3796776e2c27 207 adisense, and will keep varying as the code grows. So this elimantes the need
kevin1990 3:3796776e2c27 208 to manually modify the dummy bytes.
kevin1990 3:3796776e2c27 209 */
kevin1990 3:3796776e2c27 210
kevin1990 3:3796776e2c27 211 return 0;
kevin1990 3:3796776e2c27 212 }
kevin1990 3:3796776e2c27 213
kevin1990 3:3796776e2c27 214 /*!
kevin1990 3:3796776e2c27 215 * @brief Register a new spi connection
kevin1990 3:3796776e2c27 216 *
kevin1990 3:3796776e2c27 217 * @param[in] spiConfig spi settings for connection
kevin1990 3:3796776e2c27 218 *
kevin1990 3:3796776e2c27 219 * @return Status
kevin1990 3:3796776e2c27 220 * - #ADI_SENSE_SUCCESS Call completed successfully.
kevin1990 3:3796776e2c27 221 * - todo
kevin1990 3:3796776e2c27 222 *
kevin1990 3:3796776e2c27 223 */
kevin1990 3:3796776e2c27 224 ADI_SENSE_RESULT ADISense1000_HostSpiOpen(spiSettings spiConfig)
kevin1990 3:3796776e2c27 225 {
kevin1990 3:3796776e2c27 226 /*
kevin1990 3:3796776e2c27 227 For v0.1 we are using the mbed api.
kevin1990 3:3796776e2c27 228 The digital classes are declared globally.
kevin1990 3:3796776e2c27 229 Parameters are placeholders for future revisions.
kevin1990 3:3796776e2c27 230 */
kevin1990 3:3796776e2c27 231
kevin1990 3:3796776e2c27 232 cbkFired = false;
kevin1990 3:3796776e2c27 233 intType = 0;
kevin1990 3:3796776e2c27 234
kevin1990 3:3796776e2c27 235 callbck.attach(callbackFired);
kevin1990 3:3796776e2c27 236
kevin1990 3:3796776e2c27 237 spi.format(SPI_FRAME_SIZE_8, SPI_MODE_0);
kevin1990 3:3796776e2c27 238 spi.frequency(SPI_CLOCK_FREQUENCY_700KHZ);
kevin1990 3:3796776e2c27 239
kevin1990 3:3796776e2c27 240 if (syncWithAdisense1000() != ADI_SENSE_SUCCESS)
kevin1990 3:3796776e2c27 241 return ADI_SENSE_FAILURE;
kevin1990 3:3796776e2c27 242
kevin1990 3:3796776e2c27 243 return ADI_SENSE_SUCCESS;
kevin1990 3:3796776e2c27 244 }
kevin1990 3:3796776e2c27 245
kevin1990 3:3796776e2c27 246 /*!
kevin1990 3:3796776e2c27 247 * @brief Transfer data to slave device
kevin1990 3:3796776e2c27 248 *
kevin1990 3:3796776e2c27 249 * @param[in] pData array with transfer data
kevin1990 3:3796776e2c27 250 * @param[in] nTxLen number of bytes to transfer
kevin1990 3:3796776e2c27 251 * @param[in] bCsHold keep cs high/low after transfer
kevin1990 3:3796776e2c27 252 *
kevin1990 3:3796776e2c27 253 * @return Status
kevin1990 3:3796776e2c27 254 * - #ADI_SENSE_SUCCESS Call completed successfully.
kevin1990 3:3796776e2c27 255 * - todo
kevin1990 3:3796776e2c27 256 *
kevin1990 3:3796776e2c27 257 */
kevin1990 3:3796776e2c27 258 ADI_SENSE_RESULT
kevin1990 3:3796776e2c27 259 ADISense1000_HostSpiTransfer(uint8_t *pData, uint16_t nTxLen, bool bCsHold)
kevin1990 3:3796776e2c27 260 {
kevin1990 3:3796776e2c27 261 int txWrite = CHECK_BIT(pData[0], CMD_BIT);
kevin1990 3:3796776e2c27 262
kevin1990 3:3796776e2c27 263 if (txWrite)
kevin1990 3:3796776e2c27 264 {
kevin1990 3:3796776e2c27 265 if (spiWrite(pData, nTxLen, bCsHold) < 0)
kevin1990 3:3796776e2c27 266 return ADI_SENSE_FAILURE;
kevin1990 3:3796776e2c27 267 else
kevin1990 3:3796776e2c27 268 return ADI_SENSE_SUCCESS;
kevin1990 3:3796776e2c27 269 }
kevin1990 3:3796776e2c27 270
kevin1990 3:3796776e2c27 271 if(spiRead(pData, nTxLen, bCsHold) < 0)
kevin1990 3:3796776e2c27 272 return ADI_SENSE_FAILURE;
kevin1990 3:3796776e2c27 273
kevin1990 3:3796776e2c27 274 memcpy(pData, rxBuff, nTxLen);
kevin1990 3:3796776e2c27 275
kevin1990 3:3796776e2c27 276 return ADI_SENSE_SUCCESS;
kevin1990 3:3796776e2c27 277 }
kevin1990 3:3796776e2c27 278
kevin1990 3:3796776e2c27 279
kevin1990 3:3796776e2c27 280 /*!
kevin1990 3:3796776e2c27 281 * @brief Close spi connection
kevin1990 3:3796776e2c27 282 *
kevin1990 3:3796776e2c27 283 * @param[in]
kevin1990 3:3796776e2c27 284 *
kevin1990 3:3796776e2c27 285 * @return Status
kevin1990 3:3796776e2c27 286 * - #ADI_SENSE_SUCCESS Call completed successfully.
kevin1990 3:3796776e2c27 287 * - todo
kevin1990 3:3796776e2c27 288 *
kevin1990 3:3796776e2c27 289 */
kevin1990 3:3796776e2c27 290 ADI_SENSE_RESULT ADISense1000_HostSpiClose()
kevin1990 3:3796776e2c27 291 {
kevin1990 3:3796776e2c27 292 return ADI_SENSE_SUCCESS;
kevin1990 3:3796776e2c27 293 }
kevin1990 3:3796776e2c27 294 /**
kevin1990 3:3796776e2c27 295 * @}
kevin1990 3:3796776e2c27 296 */
kevin1990 3:3796776e2c27 297