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/ad5270/AD5270.cpp@3:1a8c14043a4e, 2016-04-01 (annotated)
- Committer:
- Suciu
- Date:
- Fri Apr 01 10:53:41 2016 +0300
- Revision:
- 3:1a8c14043a4e
- Parent:
- 1:c1f0670bb370
- Child:
- 4:5c1b28aff7e1
Added license, file headers and fixed missing whitespaces
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Suciu |
1:c1f0670bb370 | 1 | /** |
| Suciu |
1:c1f0670bb370 | 2 | * @file AD5270.cpp |
| Suciu |
1:c1f0670bb370 | 3 | * @brief Source file for AD5270 rheostat |
| Suciu |
3:1a8c14043a4e | 4 | * @author Analog Devices Inc. |
| Suciu |
3:1a8c14043a4e | 5 | * |
| Suciu |
3:1a8c14043a4e | 6 | * For support please go to: |
| Suciu |
3:1a8c14043a4e | 7 | * Github: https://github.com/analogdevicesinc/mbed-adi |
| Suciu |
3:1a8c14043a4e | 8 | * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers |
| Suciu |
3:1a8c14043a4e | 9 | * Product: http://www.analog.com/ad5270 |
| Suciu |
3:1a8c14043a4e | 10 | * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all |
| Suciu |
3:1a8c14043a4e | 11 | |
| Suciu |
3:1a8c14043a4e | 12 | ******************************************************************************** |
| Suciu |
3:1a8c14043a4e | 13 | * Copyright 2016(c) Analog Devices, Inc. |
| Suciu |
3:1a8c14043a4e | 14 | * |
| Suciu |
3:1a8c14043a4e | 15 | * All rights reserved. |
| Suciu |
3:1a8c14043a4e | 16 | * |
| Suciu |
3:1a8c14043a4e | 17 | * Redistribution and use in source and binary forms, with or without |
| Suciu |
3:1a8c14043a4e | 18 | * modification, are permitted provided that the following conditions are met: |
| Suciu |
3:1a8c14043a4e | 19 | * - Redistributions of source code must retain the above copyright |
| Suciu |
3:1a8c14043a4e | 20 | * notice, this list of conditions and the following disclaimer. |
| Suciu |
3:1a8c14043a4e | 21 | * - Redistributions in binary form must reproduce the above copyright |
| Suciu |
3:1a8c14043a4e | 22 | * notice, this list of conditions and the following disclaimer in |
| Suciu |
3:1a8c14043a4e | 23 | * the documentation and/or other materials provided with the |
| Suciu |
3:1a8c14043a4e | 24 | * distribution. |
| Suciu |
3:1a8c14043a4e | 25 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| Suciu |
3:1a8c14043a4e | 26 | * contributors may be used to endorse or promote products derived |
| Suciu |
3:1a8c14043a4e | 27 | * from this software without specific prior written permission. |
| Suciu |
3:1a8c14043a4e | 28 | * - The use of this software may or may not infringe the patent rights |
| Suciu |
3:1a8c14043a4e | 29 | * of one or more patent holders. This license does not release you |
| Suciu |
3:1a8c14043a4e | 30 | * from the requirement that you obtain separate licenses from these |
| Suciu |
3:1a8c14043a4e | 31 | * patent holders to use this software. |
| Suciu |
3:1a8c14043a4e | 32 | * - Use of the software either in source or binary form, must be run |
| Suciu |
3:1a8c14043a4e | 33 | * on or directly connected to an Analog Devices Inc. component. |
| Suciu |
3:1a8c14043a4e | 34 | * |
| Suciu |
3:1a8c14043a4e | 35 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| Suciu |
3:1a8c14043a4e | 36 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| Suciu |
3:1a8c14043a4e | 37 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| Suciu |
3:1a8c14043a4e | 38 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| Suciu |
3:1a8c14043a4e | 39 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| Suciu |
3:1a8c14043a4e | 40 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| Suciu |
3:1a8c14043a4e | 41 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| Suciu |
3:1a8c14043a4e | 42 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| Suciu |
3:1a8c14043a4e | 43 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| Suciu |
3:1a8c14043a4e | 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Suciu |
3:1a8c14043a4e | 45 | * |
| Suciu |
3:1a8c14043a4e | 46 | ********************************************************************************/ |
| Suciu |
3:1a8c14043a4e | 47 | |
| Suciu |
1:c1f0670bb370 | 48 | |
| Suciu |
1:c1f0670bb370 | 49 | #include "mbed.h" |
| Suciu |
1:c1f0670bb370 | 50 | #include "AD5270.h" |
| Suciu |
1:c1f0670bb370 | 51 | |
| Suciu |
1:c1f0670bb370 | 52 | /** |
| Suciu |
1:c1f0670bb370 | 53 | * @brief AD5270 constructor, sets CS pin and SPI format |
| Suciu |
1:c1f0670bb370 | 54 | * @param CS - (optional)chip select of the AD5270 |
| Suciu |
1:c1f0670bb370 | 55 | * @param max_resistance - (optional) nominal resistance of the AD5270 chip in ohms |
| Suciu |
1:c1f0670bb370 | 56 | * @param MOSI - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 57 | * @param MISO - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 58 | * @param SCK - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 59 | */ |
| Suciu |
3:1a8c14043a4e | 60 | AD5270::AD5270(PinName CS, float max_resistance, PinName MOSI, PinName MISO, PinName SCK): |
| Suciu |
3:1a8c14043a4e | 61 | ad5270(MOSI, MISO, SCK), cs(CS), _max_resistance(max_resistance) |
| Suciu |
1:c1f0670bb370 | 62 | { |
| Suciu |
3:1a8c14043a4e | 63 | ad5270.format(8, 3); |
| Suciu |
3:1a8c14043a4e | 64 | cs = true; |
| Suciu |
1:c1f0670bb370 | 65 | } |
| Suciu |
1:c1f0670bb370 | 66 | |
| Suciu |
1:c1f0670bb370 | 67 | /** |
| Suciu |
1:c1f0670bb370 | 68 | * @brief Compute for the nearest RDAC value from given resistance |
| Suciu |
1:c1f0670bb370 | 69 | * @param resistance - resistor |
| Suciu |
1:c1f0670bb370 | 70 | * @return RDAC value - closest possible to given resistance |
| Suciu |
1:c1f0670bb370 | 71 | */ |
| Suciu |
1:c1f0670bb370 | 72 | uint16_t AD5270::calc_RDAC(float resistance) |
| Suciu |
1:c1f0670bb370 | 73 | { |
| Suciu |
1:c1f0670bb370 | 74 | return static_cast<uint16_t>( (resistance / _max_resistance) * 1024.0 ); |
| Suciu |
1:c1f0670bb370 | 75 | } |
| Suciu |
1:c1f0670bb370 | 76 | |
| Suciu |
1:c1f0670bb370 | 77 | /** |
| Suciu |
1:c1f0670bb370 | 78 | * @brief Puts the AD5270 SDO line in to Hi-Z mode |
| Suciu |
1:c1f0670bb370 | 79 | * @return none |
| Suciu |
1:c1f0670bb370 | 80 | */ |
| Suciu |
1:c1f0670bb370 | 81 | void AD5270::set_SDO_HiZ(void) |
| Suciu |
1:c1f0670bb370 | 82 | { |
| Suciu |
1:c1f0670bb370 | 83 | write_reg(HI_Z_Cmd); |
| Suciu |
1:c1f0670bb370 | 84 | write_reg(NO_OP_cmd); |
| Suciu |
1:c1f0670bb370 | 85 | } |
| Suciu |
1:c1f0670bb370 | 86 | |
| Suciu |
1:c1f0670bb370 | 87 | /** |
| Suciu |
1:c1f0670bb370 | 88 | * @brief Set AD5270 SPI frequency |
| Suciu |
1:c1f0670bb370 | 89 | * @param hz - SPI bus frequency in hz |
| Suciu |
1:c1f0670bb370 | 90 | * @return none |
| Suciu |
1:c1f0670bb370 | 91 | */ |
| Suciu |
1:c1f0670bb370 | 92 | void AD5270::frequency(int hz) |
| Suciu |
1:c1f0670bb370 | 93 | { |
| Suciu |
1:c1f0670bb370 | 94 | ad5270.frequency(hz); |
| Suciu |
1:c1f0670bb370 | 95 | } |
| Suciu |
1:c1f0670bb370 | 96 | |
| Suciu |
1:c1f0670bb370 | 97 | /** |
| Suciu |
1:c1f0670bb370 | 98 | * @brief Issues AD5270 a command |
| Suciu |
1:c1f0670bb370 | 99 | * @param command - command to be sent |
| Suciu |
1:c1f0670bb370 | 100 | * @param data - (optional)value for the requested command |
| Suciu |
1:c1f0670bb370 | 101 | * @return response form the AD5270 |
| Suciu |
1:c1f0670bb370 | 102 | */ |
| Suciu |
1:c1f0670bb370 | 103 | uint16_t AD5270::write_cmd(uint8_t command, uint16_t data) |
| Suciu |
1:c1f0670bb370 | 104 | { |
| Suciu |
1:c1f0670bb370 | 105 | /* build 16 bit data to be written - Command + Value */ |
| Suciu |
1:c1f0670bb370 | 106 | uint16_t ui16Command = ((command & 0x3C) << 8) | (data & 0x3FF); |
| Suciu |
1:c1f0670bb370 | 107 | return write_reg(ui16Command); |
| Suciu |
1:c1f0670bb370 | 108 | } |
| Suciu |
1:c1f0670bb370 | 109 | |
| Suciu |
1:c1f0670bb370 | 110 | /** |
| Suciu |
1:c1f0670bb370 | 111 | * @brief Writes 16bit data to the AD5270 SPI interface |
| Suciu |
1:c1f0670bb370 | 112 | * @param data to be written |
| Suciu |
1:c1f0670bb370 | 113 | * @return data returned by the AD5270 |
| Suciu |
1:c1f0670bb370 | 114 | */ |
| Suciu |
1:c1f0670bb370 | 115 | uint16_t AD5270::write_reg(uint16_t data) |
| Suciu |
1:c1f0670bb370 | 116 | { |
| Suciu |
1:c1f0670bb370 | 117 | uint16_t result; |
| Suciu |
1:c1f0670bb370 | 118 | uint8_t upper_byte = (data >> 8) & 0xFF; |
| Suciu |
1:c1f0670bb370 | 119 | uint8_t lower_byte = data & 0xFF; |
| Suciu |
1:c1f0670bb370 | 120 | cs = false; |
| Suciu |
1:c1f0670bb370 | 121 | result = ((ad5270.write(upper_byte)) << 8); |
| Suciu |
1:c1f0670bb370 | 122 | result |= ad5270.write(lower_byte); |
| Suciu |
1:c1f0670bb370 | 123 | cs = true; |
| Suciu |
1:c1f0670bb370 | 124 | return result; |
| Suciu |
1:c1f0670bb370 | 125 | } |
| Suciu |
1:c1f0670bb370 | 126 | |
| Suciu |
1:c1f0670bb370 | 127 | /** |
| Suciu |
1:c1f0670bb370 | 128 | * @brief Gets maximum resistance of the AD5270 digital rheostat |
| Suciu |
1:c1f0670bb370 | 129 | * @return maximum resistance in ohms |
| Suciu |
1:c1f0670bb370 | 130 | */ |
| Suciu |
1:c1f0670bb370 | 131 | float AD5270::get_max_resistance() |
| Suciu |
1:c1f0670bb370 | 132 | { |
| Suciu |
1:c1f0670bb370 | 133 | return _max_resistance; |
| Suciu |
1:c1f0670bb370 | 134 | } |
CN0357 - Toxic gas measurement
CN0216 - Weight Scale