A collection of Analog Devices drivers for the mbed platform
For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
libraries/AD7124/AD7124.cpp@30:990ce210e8c2, 2016-10-24 (annotated)
- Committer:
- Adrian Suciu
- Date:
- Mon Oct 24 18:15:23 2016 +0300
- Revision:
- 30:990ce210e8c2
Added example and library for the CN0398
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Adrian Suciu |
30:990ce210e8c2 | 1 | /** |
Adrian Suciu |
30:990ce210e8c2 | 2 | * @file AD7124.cpp |
Adrian Suciu |
30:990ce210e8c2 | 3 | * @brief Source file for AD7124 ADC |
Adrian Suciu |
30:990ce210e8c2 | 4 | * @author Analog Devices Inc. |
Adrian Suciu |
30:990ce210e8c2 | 5 | * |
Adrian Suciu |
30:990ce210e8c2 | 6 | * For support please go to: |
Adrian Suciu |
30:990ce210e8c2 | 7 | * Github: https://github.com/analogdevicesinc/mbed-adi |
Adrian Suciu |
30:990ce210e8c2 | 8 | * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers |
Adrian Suciu |
30:990ce210e8c2 | 9 | * Product: http://www.analog.com/ad7124 |
Adrian Suciu |
30:990ce210e8c2 | 10 | * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all |
Adrian Suciu |
30:990ce210e8c2 | 11 | |
Adrian Suciu |
30:990ce210e8c2 | 12 | ******************************************************************************** |
Adrian Suciu |
30:990ce210e8c2 | 13 | * Copyright 2016(c) Analog Devices, Inc. |
Adrian Suciu |
30:990ce210e8c2 | 14 | * |
Adrian Suciu |
30:990ce210e8c2 | 15 | * All rights reserved. |
Adrian Suciu |
30:990ce210e8c2 | 16 | * |
Adrian Suciu |
30:990ce210e8c2 | 17 | * Redistribution and use in source and binary forms, with or without |
Adrian Suciu |
30:990ce210e8c2 | 18 | * modification, are permitted provided that the following conditions are met: |
Adrian Suciu |
30:990ce210e8c2 | 19 | * - Redistributions of source code must retain the above copyright |
Adrian Suciu |
30:990ce210e8c2 | 20 | * notice, this list of conditions and the following disclaimer. |
Adrian Suciu |
30:990ce210e8c2 | 21 | * - Redistributions in binary form must reproduce the above copyright |
Adrian Suciu |
30:990ce210e8c2 | 22 | * notice, this list of conditions and the following disclaimer in |
Adrian Suciu |
30:990ce210e8c2 | 23 | * the documentation and/or other materials provided with the |
Adrian Suciu |
30:990ce210e8c2 | 24 | * distribution. |
Adrian Suciu |
30:990ce210e8c2 | 25 | * - Neither the name of Analog Devices, Inc. nor the names of its |
Adrian Suciu |
30:990ce210e8c2 | 26 | * contributors may be used to endorse or promote products derived |
Adrian Suciu |
30:990ce210e8c2 | 27 | * from this software without specific prior written permission. |
Adrian Suciu |
30:990ce210e8c2 | 28 | * - The use of this software may or may not infringe the patent rights |
Adrian Suciu |
30:990ce210e8c2 | 29 | * of one or more patent holders. This license does not release you |
Adrian Suciu |
30:990ce210e8c2 | 30 | * from the requirement that you obtain separate licenses from these |
Adrian Suciu |
30:990ce210e8c2 | 31 | * patent holders to use this software. |
Adrian Suciu |
30:990ce210e8c2 | 32 | * - Use of the software either in source or binary form, must be run |
Adrian Suciu |
30:990ce210e8c2 | 33 | * on or directly connected to an Analog Devices Inc. component. |
Adrian Suciu |
30:990ce210e8c2 | 34 | * |
Adrian Suciu |
30:990ce210e8c2 | 35 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
Adrian Suciu |
30:990ce210e8c2 | 36 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
Adrian Suciu |
30:990ce210e8c2 | 37 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
Adrian Suciu |
30:990ce210e8c2 | 38 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
Adrian Suciu |
30:990ce210e8c2 | 39 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
Adrian Suciu |
30:990ce210e8c2 | 40 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
Adrian Suciu |
30:990ce210e8c2 | 41 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Adrian Suciu |
30:990ce210e8c2 | 42 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Adrian Suciu |
30:990ce210e8c2 | 43 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Adrian Suciu |
30:990ce210e8c2 | 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Adrian Suciu |
30:990ce210e8c2 | 45 | * |
Adrian Suciu |
30:990ce210e8c2 | 46 | ********************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 47 | |
Adrian Suciu |
30:990ce210e8c2 | 48 | #include <stdint.h> |
Adrian Suciu |
30:990ce210e8c2 | 49 | #include "mbed.h" |
Adrian Suciu |
30:990ce210e8c2 | 50 | #include "AD7124.h" |
Adrian Suciu |
30:990ce210e8c2 | 51 | |
Adrian Suciu |
30:990ce210e8c2 | 52 | /** |
Adrian Suciu |
30:990ce210e8c2 | 53 | * @brief AD7790 constructor, sets CS pin and SPI format |
Adrian Suciu |
30:990ce210e8c2 | 54 | * @param CS - (optional)chip select of the AD7790 |
Adrian Suciu |
30:990ce210e8c2 | 55 | * @param MOSI - (optional)pin of the SPI interface |
Adrian Suciu |
30:990ce210e8c2 | 56 | * @param MISO - (optional)pin of the SPI interface |
Adrian Suciu |
30:990ce210e8c2 | 57 | * @param SCK - (optional)pin of the SPI interface |
Adrian Suciu |
30:990ce210e8c2 | 58 | */ |
Adrian Suciu |
30:990ce210e8c2 | 59 | AD7124::AD7124(PinName CS, |
Adrian Suciu |
30:990ce210e8c2 | 60 | PinName MOSI, |
Adrian Suciu |
30:990ce210e8c2 | 61 | PinName MISO, |
Adrian Suciu |
30:990ce210e8c2 | 62 | PinName SCK) : |
Adrian Suciu |
30:990ce210e8c2 | 63 | miso(MISO), ad7124(MOSI, MISO, SCK), cs(CS) |
Adrian Suciu |
30:990ce210e8c2 | 64 | { |
Adrian Suciu |
30:990ce210e8c2 | 65 | cs = true; // cs is active low |
Adrian Suciu |
30:990ce210e8c2 | 66 | ad7124.format(8, _SPI_MODE); |
Adrian Suciu |
30:990ce210e8c2 | 67 | this->regs = ad7124_regs; |
Adrian Suciu |
30:990ce210e8c2 | 68 | this->useCRC = false; |
Adrian Suciu |
30:990ce210e8c2 | 69 | } |
Adrian Suciu |
30:990ce210e8c2 | 70 | |
Adrian Suciu |
30:990ce210e8c2 | 71 | /** |
Adrian Suciu |
30:990ce210e8c2 | 72 | * @brief Set AD7790 SPI frequency |
Adrian Suciu |
30:990ce210e8c2 | 73 | * @param hz - SPI bus frequency in hz |
Adrian Suciu |
30:990ce210e8c2 | 74 | * @return none |
Adrian Suciu |
30:990ce210e8c2 | 75 | */ |
Adrian Suciu |
30:990ce210e8c2 | 76 | void AD7124::frequency(int hz) |
Adrian Suciu |
30:990ce210e8c2 | 77 | { |
Adrian Suciu |
30:990ce210e8c2 | 78 | ad7124.frequency(hz); |
Adrian Suciu |
30:990ce210e8c2 | 79 | } |
Adrian Suciu |
30:990ce210e8c2 | 80 | |
Adrian Suciu |
30:990ce210e8c2 | 81 | /** |
Adrian Suciu |
30:990ce210e8c2 | 82 | * @brief Resets the AD7790 |
Adrian Suciu |
30:990ce210e8c2 | 83 | * @return none |
Adrian Suciu |
30:990ce210e8c2 | 84 | */ |
Adrian Suciu |
30:990ce210e8c2 | 85 | /*void AD7124::reset() |
Adrian Suciu |
30:990ce210e8c2 | 86 | { |
Adrian Suciu |
30:990ce210e8c2 | 87 | ad7124.format(8, _SPI_MODE); |
Adrian Suciu |
30:990ce210e8c2 | 88 | cs = false; |
Adrian Suciu |
30:990ce210e8c2 | 89 | wait_us(_DELAY_TIMING); |
Adrian Suciu |
30:990ce210e8c2 | 90 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 91 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 92 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 93 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 94 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 95 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 96 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 97 | ad7124.write(_RESET); |
Adrian Suciu |
30:990ce210e8c2 | 98 | wait_us(_DELAY_TIMING); |
Adrian Suciu |
30:990ce210e8c2 | 99 | cs = true; |
Adrian Suciu |
30:990ce210e8c2 | 100 | //_continous_conversion = true; |
Adrian Suciu |
30:990ce210e8c2 | 101 | }*/ |
Adrian Suciu |
30:990ce210e8c2 | 102 | /** |
Adrian Suciu |
30:990ce210e8c2 | 103 | * @brief Reads a register of the AD7790 |
Adrian Suciu |
30:990ce210e8c2 | 104 | * @param address - address of the register |
Adrian Suciu |
30:990ce210e8c2 | 105 | * @return value of the register |
Adrian Suciu |
30:990ce210e8c2 | 106 | */ |
Adrian Suciu |
30:990ce210e8c2 | 107 | uint16_t AD7124::read_reg(uint8_t address) |
Adrian Suciu |
30:990ce210e8c2 | 108 | { |
Adrian Suciu |
30:990ce210e8c2 | 109 | uint16_t data = address << 8; |
Adrian Suciu |
30:990ce210e8c2 | 110 | data |= _DUMMY_BYTE; |
Adrian Suciu |
30:990ce210e8c2 | 111 | data |= _READ_FLAG; |
Adrian Suciu |
30:990ce210e8c2 | 112 | return write_spi(data); |
Adrian Suciu |
30:990ce210e8c2 | 113 | } |
Adrian Suciu |
30:990ce210e8c2 | 114 | |
Adrian Suciu |
30:990ce210e8c2 | 115 | /** |
Adrian Suciu |
30:990ce210e8c2 | 116 | * @brief Writes a register of the AD7790 |
Adrian Suciu |
30:990ce210e8c2 | 117 | * @param address - address of the register |
Adrian Suciu |
30:990ce210e8c2 | 118 | * @param reg_val - value to be written |
Adrian Suciu |
30:990ce210e8c2 | 119 | * @return none |
Adrian Suciu |
30:990ce210e8c2 | 120 | * |
Adrian Suciu |
30:990ce210e8c2 | 121 | */ |
Adrian Suciu |
30:990ce210e8c2 | 122 | void AD7124::write_reg(uint8_t address, uint8_t reg_val) |
Adrian Suciu |
30:990ce210e8c2 | 123 | { |
Adrian Suciu |
30:990ce210e8c2 | 124 | uint16_t spi_data = address << 8; |
Adrian Suciu |
30:990ce210e8c2 | 125 | spi_data |= reg_val; |
Adrian Suciu |
30:990ce210e8c2 | 126 | write_spi(spi_data); |
Adrian Suciu |
30:990ce210e8c2 | 127 | } |
Adrian Suciu |
30:990ce210e8c2 | 128 | |
Adrian Suciu |
30:990ce210e8c2 | 129 | /** |
Adrian Suciu |
30:990ce210e8c2 | 130 | * @brief Writes 16bit data to the AD7790 SPI interface |
Adrian Suciu |
30:990ce210e8c2 | 131 | * @param reg_val to be written |
Adrian Suciu |
30:990ce210e8c2 | 132 | * @return data returned by the AD7790 |
Adrian Suciu |
30:990ce210e8c2 | 133 | */ |
Adrian Suciu |
30:990ce210e8c2 | 134 | uint16_t AD7124::write_spi(uint16_t reg_val) |
Adrian Suciu |
30:990ce210e8c2 | 135 | { |
Adrian Suciu |
30:990ce210e8c2 | 136 | uint16_t data_result; |
Adrian Suciu |
30:990ce210e8c2 | 137 | uint8_t upper_byte = (reg_val >> 8) & 0xFF; |
Adrian Suciu |
30:990ce210e8c2 | 138 | uint8_t lower_byte = reg_val & 0xFF; |
Adrian Suciu |
30:990ce210e8c2 | 139 | ad7124.format(8, _SPI_MODE); |
Adrian Suciu |
30:990ce210e8c2 | 140 | cs = false; |
Adrian Suciu |
30:990ce210e8c2 | 141 | data_result = (ad7124.write(upper_byte) << 8); |
Adrian Suciu |
30:990ce210e8c2 | 142 | data_result |= ad7124.write(lower_byte); |
Adrian Suciu |
30:990ce210e8c2 | 143 | cs = true; |
Adrian Suciu |
30:990ce210e8c2 | 144 | return data_result; |
Adrian Suciu |
30:990ce210e8c2 | 145 | } |
Adrian Suciu |
30:990ce210e8c2 | 146 | |
Adrian Suciu |
30:990ce210e8c2 | 147 | |
Adrian Suciu |
30:990ce210e8c2 | 148 | |
Adrian Suciu |
30:990ce210e8c2 | 149 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 150 | * @brief Reads the value of the specified register without checking if the |
Adrian Suciu |
30:990ce210e8c2 | 151 | * device is ready to accept user requests. |
Adrian Suciu |
30:990ce210e8c2 | 152 | * |
Adrian Suciu |
30:990ce210e8c2 | 153 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 154 | * @param pReg - Pointer to the register structure holding info about the |
Adrian Suciu |
30:990ce210e8c2 | 155 | * register to be read. The read value is stored inside the |
Adrian Suciu |
30:990ce210e8c2 | 156 | * register structure. |
Adrian Suciu |
30:990ce210e8c2 | 157 | * |
Adrian Suciu |
30:990ce210e8c2 | 158 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 159 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 160 | int32_t AD7124::NoCheckReadRegister(ad7124_st_reg* pReg) |
Adrian Suciu |
30:990ce210e8c2 | 161 | { |
Adrian Suciu |
30:990ce210e8c2 | 162 | int32_t ret = 0; |
Adrian Suciu |
30:990ce210e8c2 | 163 | uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adrian Suciu |
30:990ce210e8c2 | 164 | uint8_t i = 0; |
Adrian Suciu |
30:990ce210e8c2 | 165 | uint8_t check8 = 0; |
Adrian Suciu |
30:990ce210e8c2 | 166 | uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adrian Suciu |
30:990ce210e8c2 | 167 | |
Adrian Suciu |
30:990ce210e8c2 | 168 | |
Adrian Suciu |
30:990ce210e8c2 | 169 | check8 = useCRC; |
Adrian Suciu |
30:990ce210e8c2 | 170 | |
Adrian Suciu |
30:990ce210e8c2 | 171 | /* Build the Command word */ |
Adrian Suciu |
30:990ce210e8c2 | 172 | buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | |
Adrian Suciu |
30:990ce210e8c2 | 173 | AD7124_COMM_REG_RA(pReg->addr); |
Adrian Suciu |
30:990ce210e8c2 | 174 | |
Adrian Suciu |
30:990ce210e8c2 | 175 | /* Read data from the device */ |
Adrian Suciu |
30:990ce210e8c2 | 176 | ret = SPI_Read(buffer, |
Adrian Suciu |
30:990ce210e8c2 | 177 | ((useCRC != AD7124_DISABLE_CRC) ? pReg->size + 1 |
Adrian Suciu |
30:990ce210e8c2 | 178 | : pReg->size) + 1); |
Adrian Suciu |
30:990ce210e8c2 | 179 | if(ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 180 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 181 | |
Adrian Suciu |
30:990ce210e8c2 | 182 | /* Check the CRC */ |
Adrian Suciu |
30:990ce210e8c2 | 183 | if(check8 == AD7124_USE_CRC) { |
Adrian Suciu |
30:990ce210e8c2 | 184 | msgBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | |
Adrian Suciu |
30:990ce210e8c2 | 185 | AD7124_COMM_REG_RA(pReg->addr); |
Adrian Suciu |
30:990ce210e8c2 | 186 | for(i = 1; i < pReg->size + 2; ++i) { |
Adrian Suciu |
30:990ce210e8c2 | 187 | msgBuf[i] = buffer[i]; |
Adrian Suciu |
30:990ce210e8c2 | 188 | } |
Adrian Suciu |
30:990ce210e8c2 | 189 | check8 = ComputeCRC8(msgBuf, pReg->size + 2); |
Adrian Suciu |
30:990ce210e8c2 | 190 | } |
Adrian Suciu |
30:990ce210e8c2 | 191 | |
Adrian Suciu |
30:990ce210e8c2 | 192 | if(check8 != 0) { |
Adrian Suciu |
30:990ce210e8c2 | 193 | /* ReadRegister checksum failed. */ |
Adrian Suciu |
30:990ce210e8c2 | 194 | return COMM_ERR; |
Adrian Suciu |
30:990ce210e8c2 | 195 | } |
Adrian Suciu |
30:990ce210e8c2 | 196 | |
Adrian Suciu |
30:990ce210e8c2 | 197 | /* Build the result */ |
Adrian Suciu |
30:990ce210e8c2 | 198 | pReg->value = 0; |
Adrian Suciu |
30:990ce210e8c2 | 199 | for(i = 1; i < pReg->size + 1; i++) { |
Adrian Suciu |
30:990ce210e8c2 | 200 | pReg->value <<= 8; |
Adrian Suciu |
30:990ce210e8c2 | 201 | pReg->value += buffer[i]; |
Adrian Suciu |
30:990ce210e8c2 | 202 | } |
Adrian Suciu |
30:990ce210e8c2 | 203 | |
Adrian Suciu |
30:990ce210e8c2 | 204 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 205 | } |
Adrian Suciu |
30:990ce210e8c2 | 206 | |
Adrian Suciu |
30:990ce210e8c2 | 207 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 208 | * @brief Writes the value of the specified register without checking if the |
Adrian Suciu |
30:990ce210e8c2 | 209 | * device is ready to accept user requests. |
Adrian Suciu |
30:990ce210e8c2 | 210 | * |
Adrian Suciu |
30:990ce210e8c2 | 211 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 212 | * @param reg - Register structure holding info about the register to be written |
Adrian Suciu |
30:990ce210e8c2 | 213 | * |
Adrian Suciu |
30:990ce210e8c2 | 214 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 215 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 216 | int32_t AD7124::NoCheckWriteRegister(ad7124_st_reg reg) |
Adrian Suciu |
30:990ce210e8c2 | 217 | { |
Adrian Suciu |
30:990ce210e8c2 | 218 | int32_t ret = 0; |
Adrian Suciu |
30:990ce210e8c2 | 219 | int32_t regValue = 0; |
Adrian Suciu |
30:990ce210e8c2 | 220 | uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adrian Suciu |
30:990ce210e8c2 | 221 | uint8_t i = 0; |
Adrian Suciu |
30:990ce210e8c2 | 222 | uint8_t crc8 = 0; |
Adrian Suciu |
30:990ce210e8c2 | 223 | |
Adrian Suciu |
30:990ce210e8c2 | 224 | |
Adrian Suciu |
30:990ce210e8c2 | 225 | /* Build the Command word */ |
Adrian Suciu |
30:990ce210e8c2 | 226 | wrBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_WR | |
Adrian Suciu |
30:990ce210e8c2 | 227 | AD7124_COMM_REG_RA(reg.addr); |
Adrian Suciu |
30:990ce210e8c2 | 228 | |
Adrian Suciu |
30:990ce210e8c2 | 229 | /* Fill the write buffer */ |
Adrian Suciu |
30:990ce210e8c2 | 230 | regValue = reg.value; |
Adrian Suciu |
30:990ce210e8c2 | 231 | for(i = 0; i < reg.size; i++) { |
Adrian Suciu |
30:990ce210e8c2 | 232 | wrBuf[reg.size - i] = regValue & 0xFF; |
Adrian Suciu |
30:990ce210e8c2 | 233 | regValue >>= 8; |
Adrian Suciu |
30:990ce210e8c2 | 234 | } |
Adrian Suciu |
30:990ce210e8c2 | 235 | |
Adrian Suciu |
30:990ce210e8c2 | 236 | /* Compute the CRC */ |
Adrian Suciu |
30:990ce210e8c2 | 237 | if(useCRC != AD7124_DISABLE_CRC) { |
Adrian Suciu |
30:990ce210e8c2 | 238 | crc8 = ComputeCRC8(wrBuf, reg.size + 1); |
Adrian Suciu |
30:990ce210e8c2 | 239 | wrBuf[reg.size + 1] = crc8; |
Adrian Suciu |
30:990ce210e8c2 | 240 | } |
Adrian Suciu |
30:990ce210e8c2 | 241 | |
Adrian Suciu |
30:990ce210e8c2 | 242 | /* Write data to the device */ |
Adrian Suciu |
30:990ce210e8c2 | 243 | ret = SPI_Write(wrBuf, |
Adrian Suciu |
30:990ce210e8c2 | 244 | (useCRC != AD7124_DISABLE_CRC) ? reg.size + 2 |
Adrian Suciu |
30:990ce210e8c2 | 245 | : reg.size + 1); |
Adrian Suciu |
30:990ce210e8c2 | 246 | |
Adrian Suciu |
30:990ce210e8c2 | 247 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 248 | } |
Adrian Suciu |
30:990ce210e8c2 | 249 | |
Adrian Suciu |
30:990ce210e8c2 | 250 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 251 | * @brief Reads the value of the specified register only when the device is ready |
Adrian Suciu |
30:990ce210e8c2 | 252 | * to accept user requests. If the device ready flag is deactivated the |
Adrian Suciu |
30:990ce210e8c2 | 253 | * read operation will be executed without checking the device state. |
Adrian Suciu |
30:990ce210e8c2 | 254 | * |
Adrian Suciu |
30:990ce210e8c2 | 255 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 256 | * @param pReg - Pointer to the register structure holding info about the |
Adrian Suciu |
30:990ce210e8c2 | 257 | * register to be read. The read value is stored inside the |
Adrian Suciu |
30:990ce210e8c2 | 258 | * register structure. |
Adrian Suciu |
30:990ce210e8c2 | 259 | * |
Adrian Suciu |
30:990ce210e8c2 | 260 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 261 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 262 | int32_t AD7124::ReadRegister(ad7124_st_reg* pReg) |
Adrian Suciu |
30:990ce210e8c2 | 263 | { |
Adrian Suciu |
30:990ce210e8c2 | 264 | int32_t ret; |
Adrian Suciu |
30:990ce210e8c2 | 265 | |
Adrian Suciu |
30:990ce210e8c2 | 266 | if (pReg->addr != ERR_REG && check_ready) { |
Adrian Suciu |
30:990ce210e8c2 | 267 | ret = WaitForSpiReady(spi_rdy_poll_cnt); |
Adrian Suciu |
30:990ce210e8c2 | 268 | if (ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 269 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 270 | } |
Adrian Suciu |
30:990ce210e8c2 | 271 | ret = NoCheckReadRegister(pReg); |
Adrian Suciu |
30:990ce210e8c2 | 272 | |
Adrian Suciu |
30:990ce210e8c2 | 273 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 274 | } |
Adrian Suciu |
30:990ce210e8c2 | 275 | |
Adrian Suciu |
30:990ce210e8c2 | 276 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 277 | * @brief Writes the value of the specified register only when the device is |
Adrian Suciu |
30:990ce210e8c2 | 278 | * ready to accept user requests. If the device ready flag is deactivated |
Adrian Suciu |
30:990ce210e8c2 | 279 | * the write operation will be executed without checking the device state. |
Adrian Suciu |
30:990ce210e8c2 | 280 | * |
Adrian Suciu |
30:990ce210e8c2 | 281 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 282 | * @param reg - Register structure holding info about the register to be written |
Adrian Suciu |
30:990ce210e8c2 | 283 | * |
Adrian Suciu |
30:990ce210e8c2 | 284 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 285 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 286 | int32_t AD7124::WriteRegister(ad7124_st_reg pReg) |
Adrian Suciu |
30:990ce210e8c2 | 287 | { |
Adrian Suciu |
30:990ce210e8c2 | 288 | int32_t ret; |
Adrian Suciu |
30:990ce210e8c2 | 289 | |
Adrian Suciu |
30:990ce210e8c2 | 290 | if (check_ready) { |
Adrian Suciu |
30:990ce210e8c2 | 291 | ret = WaitForSpiReady(spi_rdy_poll_cnt); |
Adrian Suciu |
30:990ce210e8c2 | 292 | if (ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 293 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 294 | } |
Adrian Suciu |
30:990ce210e8c2 | 295 | ret = NoCheckWriteRegister(pReg); |
Adrian Suciu |
30:990ce210e8c2 | 296 | |
Adrian Suciu |
30:990ce210e8c2 | 297 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 298 | } |
Adrian Suciu |
30:990ce210e8c2 | 299 | |
Adrian Suciu |
30:990ce210e8c2 | 300 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 301 | * @brief Reads and returns the value of a device register. The read value is |
Adrian Suciu |
30:990ce210e8c2 | 302 | * also stored in software register list of the device. |
Adrian Suciu |
30:990ce210e8c2 | 303 | * |
Adrian Suciu |
30:990ce210e8c2 | 304 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 305 | * @param reg - Which register to read from. |
Adrian Suciu |
30:990ce210e8c2 | 306 | * @param pError - Pointer to the location where to store the error code if an |
Adrian Suciu |
30:990ce210e8c2 | 307 | * error occurs. Stores 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 308 | * Does not store anything if pErorr = NULL; |
Adrian Suciu |
30:990ce210e8c2 | 309 | * |
Adrian Suciu |
30:990ce210e8c2 | 310 | * @return Returns the value read from the specified register. |
Adrian Suciu |
30:990ce210e8c2 | 311 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 312 | uint32_t AD7124::ReadDeviceRegister(enum ad7124_registers reg) |
Adrian Suciu |
30:990ce210e8c2 | 313 | { |
Adrian Suciu |
30:990ce210e8c2 | 314 | ReadRegister(®s[reg]); |
Adrian Suciu |
30:990ce210e8c2 | 315 | return (regs[reg].value); |
Adrian Suciu |
30:990ce210e8c2 | 316 | } |
Adrian Suciu |
30:990ce210e8c2 | 317 | |
Adrian Suciu |
30:990ce210e8c2 | 318 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 319 | * @brief Writes the specified value to a device register. The value to be |
Adrian Suciu |
30:990ce210e8c2 | 320 | * written is also stored in the software register list of the device. |
Adrian Suciu |
30:990ce210e8c2 | 321 | * |
Adrian Suciu |
30:990ce210e8c2 | 322 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 323 | * @param reg - Which register to write to. |
Adrian Suciu |
30:990ce210e8c2 | 324 | * @param value - The value to be written to the reigster of the device. |
Adrian Suciu |
30:990ce210e8c2 | 325 | * |
Adrian Suciu |
30:990ce210e8c2 | 326 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 327 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 328 | int32_t AD7124::WriteDeviceRegister(enum ad7124_registers reg, uint32_t value) |
Adrian Suciu |
30:990ce210e8c2 | 329 | { |
Adrian Suciu |
30:990ce210e8c2 | 330 | regs[reg].value = value; |
Adrian Suciu |
30:990ce210e8c2 | 331 | return(WriteRegister(regs[reg])); |
Adrian Suciu |
30:990ce210e8c2 | 332 | } |
Adrian Suciu |
30:990ce210e8c2 | 333 | |
Adrian Suciu |
30:990ce210e8c2 | 334 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 335 | * @brief Resets the device. |
Adrian Suciu |
30:990ce210e8c2 | 336 | * |
Adrian Suciu |
30:990ce210e8c2 | 337 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 338 | * |
Adrian Suciu |
30:990ce210e8c2 | 339 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 340 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 341 | int32_t AD7124::Reset() |
Adrian Suciu |
30:990ce210e8c2 | 342 | { |
Adrian Suciu |
30:990ce210e8c2 | 343 | int32_t ret = 0; |
Adrian Suciu |
30:990ce210e8c2 | 344 | uint8_t wrBuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
Adrian Suciu |
30:990ce210e8c2 | 345 | |
Adrian Suciu |
30:990ce210e8c2 | 346 | ret = SPI_Write( wrBuf, 8); |
Adrian Suciu |
30:990ce210e8c2 | 347 | |
Adrian Suciu |
30:990ce210e8c2 | 348 | |
Adrian Suciu |
30:990ce210e8c2 | 349 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 350 | } |
Adrian Suciu |
30:990ce210e8c2 | 351 | |
Adrian Suciu |
30:990ce210e8c2 | 352 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 353 | * @brief Waits until the device can accept read and write user actions. |
Adrian Suciu |
30:990ce210e8c2 | 354 | * |
Adrian Suciu |
30:990ce210e8c2 | 355 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 356 | * @param timeout - Count representing the number of polls to be done until the |
Adrian Suciu |
30:990ce210e8c2 | 357 | * function returns. |
Adrian Suciu |
30:990ce210e8c2 | 358 | * |
Adrian Suciu |
30:990ce210e8c2 | 359 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 360 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 361 | int32_t AD7124::WaitForSpiReady(uint32_t timeout) |
Adrian Suciu |
30:990ce210e8c2 | 362 | { |
Adrian Suciu |
30:990ce210e8c2 | 363 | ad7124_st_reg *regs; |
Adrian Suciu |
30:990ce210e8c2 | 364 | int32_t ret; |
Adrian Suciu |
30:990ce210e8c2 | 365 | int8_t ready = 0; |
Adrian Suciu |
30:990ce210e8c2 | 366 | |
Adrian Suciu |
30:990ce210e8c2 | 367 | regs = this->regs; |
Adrian Suciu |
30:990ce210e8c2 | 368 | |
Adrian Suciu |
30:990ce210e8c2 | 369 | while(!ready && --timeout) { |
Adrian Suciu |
30:990ce210e8c2 | 370 | /* Read the value of the Error Register */ |
Adrian Suciu |
30:990ce210e8c2 | 371 | ret = ReadRegister(®s[AD7124_Error]); |
Adrian Suciu |
30:990ce210e8c2 | 372 | if(ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 373 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 374 | |
Adrian Suciu |
30:990ce210e8c2 | 375 | /* Check the SPI IGNORE Error bit in the Error Register */ |
Adrian Suciu |
30:990ce210e8c2 | 376 | ready = (regs[AD7124_Error].value & |
Adrian Suciu |
30:990ce210e8c2 | 377 | AD7124_ERR_REG_SPI_IGNORE_ERR) == 0; |
Adrian Suciu |
30:990ce210e8c2 | 378 | } |
Adrian Suciu |
30:990ce210e8c2 | 379 | |
Adrian Suciu |
30:990ce210e8c2 | 380 | return timeout ? 0 : TIMEOUT; |
Adrian Suciu |
30:990ce210e8c2 | 381 | } |
Adrian Suciu |
30:990ce210e8c2 | 382 | |
Adrian Suciu |
30:990ce210e8c2 | 383 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 384 | * @brief Waits until a new conversion result is available. |
Adrian Suciu |
30:990ce210e8c2 | 385 | * |
Adrian Suciu |
30:990ce210e8c2 | 386 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 387 | * @param timeout - Count representing the number of polls to be done until the |
Adrian Suciu |
30:990ce210e8c2 | 388 | * function returns if no new data is available. |
Adrian Suciu |
30:990ce210e8c2 | 389 | * |
Adrian Suciu |
30:990ce210e8c2 | 390 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 391 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 392 | int32_t AD7124::WaitForConvReady(uint32_t timeout) |
Adrian Suciu |
30:990ce210e8c2 | 393 | { |
Adrian Suciu |
30:990ce210e8c2 | 394 | ad7124_st_reg *regs; |
Adrian Suciu |
30:990ce210e8c2 | 395 | int32_t ret; |
Adrian Suciu |
30:990ce210e8c2 | 396 | int8_t ready = 0; |
Adrian Suciu |
30:990ce210e8c2 | 397 | |
Adrian Suciu |
30:990ce210e8c2 | 398 | regs = this->regs; |
Adrian Suciu |
30:990ce210e8c2 | 399 | |
Adrian Suciu |
30:990ce210e8c2 | 400 | while(!ready && --timeout) { |
Adrian Suciu |
30:990ce210e8c2 | 401 | /* Read the value of the Status Register */ |
Adrian Suciu |
30:990ce210e8c2 | 402 | ret = ReadRegister(®s[AD7124_Status]); |
Adrian Suciu |
30:990ce210e8c2 | 403 | if(ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 404 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 405 | |
Adrian Suciu |
30:990ce210e8c2 | 406 | /* Check the RDY bit in the Status Register */ |
Adrian Suciu |
30:990ce210e8c2 | 407 | ready = (regs[AD7124_Status].value & |
Adrian Suciu |
30:990ce210e8c2 | 408 | AD7124_STATUS_REG_RDY) == 0; |
Adrian Suciu |
30:990ce210e8c2 | 409 | wait_ms(1); |
Adrian Suciu |
30:990ce210e8c2 | 410 | } |
Adrian Suciu |
30:990ce210e8c2 | 411 | |
Adrian Suciu |
30:990ce210e8c2 | 412 | return timeout ? 0 : TIMEOUT; |
Adrian Suciu |
30:990ce210e8c2 | 413 | } |
Adrian Suciu |
30:990ce210e8c2 | 414 | |
Adrian Suciu |
30:990ce210e8c2 | 415 | bool AD7124::get_miso() |
Adrian Suciu |
30:990ce210e8c2 | 416 | { |
Adrian Suciu |
30:990ce210e8c2 | 417 | return miso.read(); |
Adrian Suciu |
30:990ce210e8c2 | 418 | } |
Adrian Suciu |
30:990ce210e8c2 | 419 | |
Adrian Suciu |
30:990ce210e8c2 | 420 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 421 | * @brief Reads the conversion result from the device. |
Adrian Suciu |
30:990ce210e8c2 | 422 | * |
Adrian Suciu |
30:990ce210e8c2 | 423 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 424 | * @param pData - Pointer to store the read data. |
Adrian Suciu |
30:990ce210e8c2 | 425 | * |
Adrian Suciu |
30:990ce210e8c2 | 426 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 427 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 428 | int32_t AD7124::ReadData( int32_t* pData) |
Adrian Suciu |
30:990ce210e8c2 | 429 | { |
Adrian Suciu |
30:990ce210e8c2 | 430 | int32_t ret = 0; |
Adrian Suciu |
30:990ce210e8c2 | 431 | uint8_t check8 = 0; |
Adrian Suciu |
30:990ce210e8c2 | 432 | uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adrian Suciu |
30:990ce210e8c2 | 433 | uint8_t i = 0; |
Adrian Suciu |
30:990ce210e8c2 | 434 | uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Adrian Suciu |
30:990ce210e8c2 | 435 | ad7124_st_reg *pReg; |
Adrian Suciu |
30:990ce210e8c2 | 436 | |
Adrian Suciu |
30:990ce210e8c2 | 437 | if( !pData) |
Adrian Suciu |
30:990ce210e8c2 | 438 | return INVALID_VAL; |
Adrian Suciu |
30:990ce210e8c2 | 439 | |
Adrian Suciu |
30:990ce210e8c2 | 440 | pReg = ®s[AD7124_Data]; |
Adrian Suciu |
30:990ce210e8c2 | 441 | |
Adrian Suciu |
30:990ce210e8c2 | 442 | /* Build the Command word */ |
Adrian Suciu |
30:990ce210e8c2 | 443 | buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | |
Adrian Suciu |
30:990ce210e8c2 | 444 | AD7124_COMM_REG_RA(pReg->addr); |
Adrian Suciu |
30:990ce210e8c2 | 445 | |
Adrian Suciu |
30:990ce210e8c2 | 446 | |
Adrian Suciu |
30:990ce210e8c2 | 447 | /* Read data from the device */ |
Adrian Suciu |
30:990ce210e8c2 | 448 | ret = SPI_Read(buffer, |
Adrian Suciu |
30:990ce210e8c2 | 449 | ((useCRC != AD7124_DISABLE_CRC) ? pReg->size + 1 |
Adrian Suciu |
30:990ce210e8c2 | 450 | : pReg->size) + 2); |
Adrian Suciu |
30:990ce210e8c2 | 451 | |
Adrian Suciu |
30:990ce210e8c2 | 452 | if(ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 453 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 454 | |
Adrian Suciu |
30:990ce210e8c2 | 455 | /* Check the CRC */ |
Adrian Suciu |
30:990ce210e8c2 | 456 | if(check8 == AD7124_USE_CRC) { |
Adrian Suciu |
30:990ce210e8c2 | 457 | msgBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | |
Adrian Suciu |
30:990ce210e8c2 | 458 | AD7124_COMM_REG_RA(pReg->addr); |
Adrian Suciu |
30:990ce210e8c2 | 459 | for(i = 1; i < pReg->size + 2; ++i) { |
Adrian Suciu |
30:990ce210e8c2 | 460 | msgBuf[i] = buffer[i]; |
Adrian Suciu |
30:990ce210e8c2 | 461 | } |
Adrian Suciu |
30:990ce210e8c2 | 462 | check8 = ComputeCRC8(msgBuf, pReg->size + 3); |
Adrian Suciu |
30:990ce210e8c2 | 463 | } |
Adrian Suciu |
30:990ce210e8c2 | 464 | |
Adrian Suciu |
30:990ce210e8c2 | 465 | if(check8 != 0) { |
Adrian Suciu |
30:990ce210e8c2 | 466 | /* ReadRegister checksum failed. */ |
Adrian Suciu |
30:990ce210e8c2 | 467 | return COMM_ERR; |
Adrian Suciu |
30:990ce210e8c2 | 468 | } |
Adrian Suciu |
30:990ce210e8c2 | 469 | |
Adrian Suciu |
30:990ce210e8c2 | 470 | /* Build the result */ |
Adrian Suciu |
30:990ce210e8c2 | 471 | *pData = 0; |
Adrian Suciu |
30:990ce210e8c2 | 472 | for(i = 1; i < pReg->size + 2; i++) { |
Adrian Suciu |
30:990ce210e8c2 | 473 | *pData <<= 8; |
Adrian Suciu |
30:990ce210e8c2 | 474 | *pData += buffer[i]; |
Adrian Suciu |
30:990ce210e8c2 | 475 | } |
Adrian Suciu |
30:990ce210e8c2 | 476 | wait_ms(500); |
Adrian Suciu |
30:990ce210e8c2 | 477 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 478 | } |
Adrian Suciu |
30:990ce210e8c2 | 479 | |
Adrian Suciu |
30:990ce210e8c2 | 480 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 481 | * @brief Computes the CRC checksum for a data buffer. |
Adrian Suciu |
30:990ce210e8c2 | 482 | * |
Adrian Suciu |
30:990ce210e8c2 | 483 | * @param pBuf - Data buffer |
Adrian Suciu |
30:990ce210e8c2 | 484 | * @param bufSize - Data buffer size in bytes |
Adrian Suciu |
30:990ce210e8c2 | 485 | * |
Adrian Suciu |
30:990ce210e8c2 | 486 | * @return Returns the computed CRC checksum. |
Adrian Suciu |
30:990ce210e8c2 | 487 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 488 | uint8_t AD7124::ComputeCRC8(uint8_t * pBuf, uint8_t bufSize) |
Adrian Suciu |
30:990ce210e8c2 | 489 | { |
Adrian Suciu |
30:990ce210e8c2 | 490 | uint8_t i = 0; |
Adrian Suciu |
30:990ce210e8c2 | 491 | uint8_t crc = 0; |
Adrian Suciu |
30:990ce210e8c2 | 492 | |
Adrian Suciu |
30:990ce210e8c2 | 493 | while(bufSize) { |
Adrian Suciu |
30:990ce210e8c2 | 494 | for(i = 0x80; i != 0; i >>= 1) { |
Adrian Suciu |
30:990ce210e8c2 | 495 | if(((crc & 0x80) != 0) != ((*pBuf & i) != 0)) { /* MSB of CRC register XOR input Bit from Data */ |
Adrian Suciu |
30:990ce210e8c2 | 496 | crc <<= 1; |
Adrian Suciu |
30:990ce210e8c2 | 497 | crc ^= AD7124_CRC8_POLYNOMIAL_REPRESENTATION; |
Adrian Suciu |
30:990ce210e8c2 | 498 | } else { |
Adrian Suciu |
30:990ce210e8c2 | 499 | crc <<= 1; |
Adrian Suciu |
30:990ce210e8c2 | 500 | } |
Adrian Suciu |
30:990ce210e8c2 | 501 | } |
Adrian Suciu |
30:990ce210e8c2 | 502 | pBuf++; |
Adrian Suciu |
30:990ce210e8c2 | 503 | bufSize--; |
Adrian Suciu |
30:990ce210e8c2 | 504 | } |
Adrian Suciu |
30:990ce210e8c2 | 505 | return crc; |
Adrian Suciu |
30:990ce210e8c2 | 506 | } |
Adrian Suciu |
30:990ce210e8c2 | 507 | |
Adrian Suciu |
30:990ce210e8c2 | 508 | |
Adrian Suciu |
30:990ce210e8c2 | 509 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 510 | * @brief Updates the device SPI interface settings. |
Adrian Suciu |
30:990ce210e8c2 | 511 | * |
Adrian Suciu |
30:990ce210e8c2 | 512 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 513 | * |
Adrian Suciu |
30:990ce210e8c2 | 514 | * @return None. |
Adrian Suciu |
30:990ce210e8c2 | 515 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 516 | void AD7124::UpdateDevSpiSettings() |
Adrian Suciu |
30:990ce210e8c2 | 517 | { |
Adrian Suciu |
30:990ce210e8c2 | 518 | ad7124_st_reg *regs; |
Adrian Suciu |
30:990ce210e8c2 | 519 | |
Adrian Suciu |
30:990ce210e8c2 | 520 | regs = this->regs; |
Adrian Suciu |
30:990ce210e8c2 | 521 | |
Adrian Suciu |
30:990ce210e8c2 | 522 | if (regs[AD7124_Error_En].value & AD7124_ERREN_REG_SPI_IGNORE_ERR_EN) { |
Adrian Suciu |
30:990ce210e8c2 | 523 | check_ready = 1; |
Adrian Suciu |
30:990ce210e8c2 | 524 | } else { |
Adrian Suciu |
30:990ce210e8c2 | 525 | check_ready = 0; |
Adrian Suciu |
30:990ce210e8c2 | 526 | } |
Adrian Suciu |
30:990ce210e8c2 | 527 | } |
Adrian Suciu |
30:990ce210e8c2 | 528 | |
Adrian Suciu |
30:990ce210e8c2 | 529 | /***************************************************************************//** |
Adrian Suciu |
30:990ce210e8c2 | 530 | * @brief Initializes the AD7124. |
Adrian Suciu |
30:990ce210e8c2 | 531 | * |
Adrian Suciu |
30:990ce210e8c2 | 532 | * @param device - The handler of the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 533 | * @param slave_select - The Slave Chip Select Id to be passed to the SPI calls. |
Adrian Suciu |
30:990ce210e8c2 | 534 | * @param regs - The list of registers of the device (initialized or not) to be |
Adrian Suciu |
30:990ce210e8c2 | 535 | * added to the instance of the driver. |
Adrian Suciu |
30:990ce210e8c2 | 536 | * |
Adrian Suciu |
30:990ce210e8c2 | 537 | * @return Returns 0 for success or negative error code. |
Adrian Suciu |
30:990ce210e8c2 | 538 | *******************************************************************************/ |
Adrian Suciu |
30:990ce210e8c2 | 539 | int32_t AD7124::Setup() |
Adrian Suciu |
30:990ce210e8c2 | 540 | { |
Adrian Suciu |
30:990ce210e8c2 | 541 | int32_t ret; |
Adrian Suciu |
30:990ce210e8c2 | 542 | uint8_t regNr; |
Adrian Suciu |
30:990ce210e8c2 | 543 | |
Adrian Suciu |
30:990ce210e8c2 | 544 | |
Adrian Suciu |
30:990ce210e8c2 | 545 | spi_rdy_poll_cnt = 25000; |
Adrian Suciu |
30:990ce210e8c2 | 546 | |
Adrian Suciu |
30:990ce210e8c2 | 547 | /* Initialize the SPI communication. */ |
Adrian Suciu |
30:990ce210e8c2 | 548 | /*ret = SPI_Init(0, 2500000, 1, 0); |
Adrian Suciu |
30:990ce210e8c2 | 549 | if (ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 550 | return ret;*/ |
Adrian Suciu |
30:990ce210e8c2 | 551 | |
Adrian Suciu |
30:990ce210e8c2 | 552 | /* Reset the device interface.*/ |
Adrian Suciu |
30:990ce210e8c2 | 553 | ret = Reset(); |
Adrian Suciu |
30:990ce210e8c2 | 554 | if (ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 555 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 556 | |
Adrian Suciu |
30:990ce210e8c2 | 557 | check_ready = 1; |
Adrian Suciu |
30:990ce210e8c2 | 558 | |
Adrian Suciu |
30:990ce210e8c2 | 559 | /* Initialize registers AD7124_ADC_Control through AD7124_Filter_7. */ |
Adrian Suciu |
30:990ce210e8c2 | 560 | for(regNr = static_cast<uint8_t>(AD7124_Status); (regNr < static_cast<uint8_t>(AD7124_Offset_0)) && !(ret < 0); |
Adrian Suciu |
30:990ce210e8c2 | 561 | regNr++) { |
Adrian Suciu |
30:990ce210e8c2 | 562 | if (regs[regNr].rw == AD7124_RW) { |
Adrian Suciu |
30:990ce210e8c2 | 563 | ret = WriteRegister(regs[regNr]); |
Adrian Suciu |
30:990ce210e8c2 | 564 | if (ret < 0) |
Adrian Suciu |
30:990ce210e8c2 | 565 | break; |
Adrian Suciu |
30:990ce210e8c2 | 566 | } |
Adrian Suciu |
30:990ce210e8c2 | 567 | |
Adrian Suciu |
30:990ce210e8c2 | 568 | /* Get CRC State and device SPI interface settings */ |
Adrian Suciu |
30:990ce210e8c2 | 569 | if (regNr == AD7124_Error_En) { |
Adrian Suciu |
30:990ce210e8c2 | 570 | UpdateDevSpiSettings(); |
Adrian Suciu |
30:990ce210e8c2 | 571 | } |
Adrian Suciu |
30:990ce210e8c2 | 572 | } |
Adrian Suciu |
30:990ce210e8c2 | 573 | |
Adrian Suciu |
30:990ce210e8c2 | 574 | return ret; |
Adrian Suciu |
30:990ce210e8c2 | 575 | } |
Adrian Suciu |
30:990ce210e8c2 | 576 | |
Adrian Suciu |
30:990ce210e8c2 | 577 | uint8_t AD7124::SPI_Read(uint8_t *data, uint8_t bytes_number) |
Adrian Suciu |
30:990ce210e8c2 | 578 | { |
Adrian Suciu |
30:990ce210e8c2 | 579 | cs = false; |
Adrian Suciu |
30:990ce210e8c2 | 580 | for(uint8_t byte = 0; byte < bytes_number; byte++) { |
Adrian Suciu |
30:990ce210e8c2 | 581 | data[byte] = ad7124.write(data[byte]); |
Adrian Suciu |
30:990ce210e8c2 | 582 | } |
Adrian Suciu |
30:990ce210e8c2 | 583 | cs = true; |
Adrian Suciu |
30:990ce210e8c2 | 584 | return bytes_number; |
Adrian Suciu |
30:990ce210e8c2 | 585 | } |
Adrian Suciu |
30:990ce210e8c2 | 586 | |
Adrian Suciu |
30:990ce210e8c2 | 587 | uint8_t AD7124::SPI_Write(uint8_t *data, uint8_t bytes_number) |
Adrian Suciu |
30:990ce210e8c2 | 588 | { |
Adrian Suciu |
30:990ce210e8c2 | 589 | cs = false; |
Adrian Suciu |
30:990ce210e8c2 | 590 | for(uint8_t byte = 0; byte < bytes_number; byte++) { |
Adrian Suciu |
30:990ce210e8c2 | 591 | ad7124.write(data[byte]); |
Adrian Suciu |
30:990ce210e8c2 | 592 | } |
Adrian Suciu |
30:990ce210e8c2 | 593 | |
Adrian Suciu |
30:990ce210e8c2 | 594 | cs = true; |
Adrian Suciu |
30:990ce210e8c2 | 595 | return bytes_number; |
Adrian Suciu |
30:990ce210e8c2 | 596 | |
Adrian Suciu |
30:990ce210e8c2 | 597 | } |