Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ltc26x6.c@1:cab5535713ee, 2021-12-06 (annotated)
- Committer:
- jngarlitos
- Date:
- Mon Dec 06 05:17:03 2021 +0000
- Revision:
- 1:cab5535713ee
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jngarlitos | 1:cab5535713ee | 1 | /***************************************************************************//** |
jngarlitos | 1:cab5535713ee | 2 | * @file ltc26x6.c |
jngarlitos | 1:cab5535713ee | 3 | * @brief Implementation of LTC2606,LTC2616,LTC2626 Driver. |
jngarlitos | 1:cab5535713ee | 4 | ******************************************************************************** |
jngarlitos | 1:cab5535713ee | 5 | * Copyright 2021(c) Analog Devices, Inc. |
jngarlitos | 1:cab5535713ee | 6 | * |
jngarlitos | 1:cab5535713ee | 7 | * All rights reserved. |
jngarlitos | 1:cab5535713ee | 8 | * |
jngarlitos | 1:cab5535713ee | 9 | * Redistribution and use in source and binary forms, with or without |
jngarlitos | 1:cab5535713ee | 10 | * modification, are permitted provided that the following conditions are met: |
jngarlitos | 1:cab5535713ee | 11 | * - Redistributions of source code must retain the above copyright |
jngarlitos | 1:cab5535713ee | 12 | * notice, this list of conditions and the following disclaimer. |
jngarlitos | 1:cab5535713ee | 13 | * - Redistributions in binary form must reproduce the above copyright |
jngarlitos | 1:cab5535713ee | 14 | * notice, this list of conditions and the following disclaimer in |
jngarlitos | 1:cab5535713ee | 15 | * the documentation and/or other materials provided with the |
jngarlitos | 1:cab5535713ee | 16 | * distribution. |
jngarlitos | 1:cab5535713ee | 17 | * - Neither the name of Analog Devices, Inc. nor the names of its |
jngarlitos | 1:cab5535713ee | 18 | * contributors may be used to endorse or promote products derived |
jngarlitos | 1:cab5535713ee | 19 | * from this software without specific prior written permission. |
jngarlitos | 1:cab5535713ee | 20 | * - The use of this software may or may not infringe the patent rights |
jngarlitos | 1:cab5535713ee | 21 | * of one or more patent holders. This license does not release you |
jngarlitos | 1:cab5535713ee | 22 | * from the requirement that you obtain separate licenses from these |
jngarlitos | 1:cab5535713ee | 23 | * patent holders to use this software. |
jngarlitos | 1:cab5535713ee | 24 | * - Use of the software either in source or binary form, must be run |
jngarlitos | 1:cab5535713ee | 25 | * on or directly connected to an Analog Devices Inc. component. |
jngarlitos | 1:cab5535713ee | 26 | * |
jngarlitos | 1:cab5535713ee | 27 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
jngarlitos | 1:cab5535713ee | 28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
jngarlitos | 1:cab5535713ee | 29 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
jngarlitos | 1:cab5535713ee | 30 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
jngarlitos | 1:cab5535713ee | 31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
jngarlitos | 1:cab5535713ee | 32 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
jngarlitos | 1:cab5535713ee | 33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
jngarlitos | 1:cab5535713ee | 34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
jngarlitos | 1:cab5535713ee | 35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
jngarlitos | 1:cab5535713ee | 36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
jngarlitos | 1:cab5535713ee | 37 | *******************************************************************************/ |
jngarlitos | 1:cab5535713ee | 38 | |
jngarlitos | 1:cab5535713ee | 39 | #include <stdio.h> |
jngarlitos | 1:cab5535713ee | 40 | #include <stdint.h> |
jngarlitos | 1:cab5535713ee | 41 | #include <stdlib.h> |
jngarlitos | 1:cab5535713ee | 42 | #include <math.h> |
jngarlitos | 1:cab5535713ee | 43 | |
jngarlitos | 1:cab5535713ee | 44 | #include "ltc26x6.h" |
jngarlitos | 1:cab5535713ee | 45 | |
jngarlitos | 1:cab5535713ee | 46 | /** |
jngarlitos | 1:cab5535713ee | 47 | * Initialize the device. |
jngarlitos | 1:cab5535713ee | 48 | * @param device - The device structure. |
jngarlitos | 1:cab5535713ee | 49 | * @param init_param - The structure that contains the device initial |
jngarlitos | 1:cab5535713ee | 50 | * parameters. |
jngarlitos | 1:cab5535713ee | 51 | * @return 0 in case of success, negative error code otherwise. |
jngarlitos | 1:cab5535713ee | 52 | */ |
jngarlitos | 1:cab5535713ee | 53 | int32_t ltc26x6_init(struct ltc26x6_dev **device, |
jngarlitos | 1:cab5535713ee | 54 | struct ltc26x6_init_param init_param) |
jngarlitos | 1:cab5535713ee | 55 | { |
jngarlitos | 1:cab5535713ee | 56 | struct ltc26x6_dev *dev; |
jngarlitos | 1:cab5535713ee | 57 | int32_t ret; |
jngarlitos | 1:cab5535713ee | 58 | |
jngarlitos | 1:cab5535713ee | 59 | dev = (struct ltc26x6_dev *)malloc(sizeof(*dev)); |
jngarlitos | 1:cab5535713ee | 60 | if (!dev) |
jngarlitos | 1:cab5535713ee | 61 | return -1; |
jngarlitos | 1:cab5535713ee | 62 | |
jngarlitos | 1:cab5535713ee | 63 | ret = i2c_init(&dev->i2c_desc, &init_param.i2c_init); |
jngarlitos | 1:cab5535713ee | 64 | dev->resolution = init_param.resolution; |
jngarlitos | 1:cab5535713ee | 65 | dev->vref = init_param.vref; |
jngarlitos | 1:cab5535713ee | 66 | dev->typical_offset = init_param.typical_offset; |
jngarlitos | 1:cab5535713ee | 67 | *device = dev; |
jngarlitos | 1:cab5535713ee | 68 | return ret; |
jngarlitos | 1:cab5535713ee | 69 | } |
jngarlitos | 1:cab5535713ee | 70 | |
jngarlitos | 1:cab5535713ee | 71 | /** |
jngarlitos | 1:cab5535713ee | 72 | * Calculates an LTC26X6 DAC code for the desired output voltage. |
jngarlitos | 1:cab5535713ee | 73 | * Based on the desired output voltage, the offset, and lsb parameters, return the corresponding DAC code that should be written to the LTC26X6. |
jngarlitos | 1:cab5535713ee | 74 | * @param dac_voltage Desired output voltage |
jngarlitos | 1:cab5535713ee | 75 | * @param *code Returned DAC code |
jngarlitos | 1:cab5535713ee | 76 | * @return 0 in case of success, negative error code otherwise. |
jngarlitos | 1:cab5535713ee | 77 | */ |
jngarlitos | 1:cab5535713ee | 78 | int16_t ltc26x6_voltage_to_code(struct ltc26x6_dev *device, float dac_voltage, |
jngarlitos | 1:cab5535713ee | 79 | uint16_t *code) |
jngarlitos | 1:cab5535713ee | 80 | { |
jngarlitos | 1:cab5535713ee | 81 | uint32_t ltc26x6_full_scale = (2 << device->resolution) - 1; |
jngarlitos | 1:cab5535713ee | 82 | // The LTC26X6 least significant bit value with given voltage reference |
jngarlitos | 1:cab5535713ee | 83 | float ltc26x6_lsb = (float)(device->vref/ltc26x6_full_scale); |
jngarlitos | 1:cab5535713ee | 84 | int32_t dac_code, ret; |
jngarlitos | 1:cab5535713ee | 85 | float float_code; |
jngarlitos | 1:cab5535713ee | 86 | |
jngarlitos | 1:cab5535713ee | 87 | // 1) Calculate the DAC code: (DAC voltage/LSB)- typical offset volatge |
jngarlitos | 1:cab5535713ee | 88 | float_code = (dac_voltage / ltc26x6_lsb) - device->typical_offset; |
jngarlitos | 1:cab5535713ee | 89 | // 2) Round |
jngarlitos | 1:cab5535713ee | 90 | float_code = (float_code > (floor(float_code) + 0.5)) ? ceil(float_code) : floor(float_code); |
jngarlitos | 1:cab5535713ee | 91 | // 3) Convert to unsigned integer |
jngarlitos | 1:cab5535713ee | 92 | dac_code = (int32_t)(float_code); |
jngarlitos | 1:cab5535713ee | 93 | |
jngarlitos | 1:cab5535713ee | 94 | if(dac_code > ltc26x6_full_scale) { |
jngarlitos | 1:cab5535713ee | 95 | // Requesetd voltage is bigger than reference voltage |
jngarlitos | 1:cab5535713ee | 96 | // Return fullscale code |
jngarlitos | 1:cab5535713ee | 97 | dac_code = ltc26x6_full_scale; |
jngarlitos | 1:cab5535713ee | 98 | // Return Overflow error |
jngarlitos | 1:cab5535713ee | 99 | ret = LTC26X6_CODE_OVERFLOW; |
jngarlitos | 1:cab5535713ee | 100 | } else if(dac_code < 0) { |
jngarlitos | 1:cab5535713ee | 101 | // Requested voltage is lower than offset voltage |
jngarlitos | 1:cab5535713ee | 102 | dac_code = 0; |
jngarlitos | 1:cab5535713ee | 103 | // Return Underflow error |
jngarlitos | 1:cab5535713ee | 104 | ret = LTC26X6_CODE_UNDERFLOW; |
jngarlitos | 1:cab5535713ee | 105 | } else // Requestedd volage is in supported range |
jngarlitos | 1:cab5535713ee | 106 | ret = SUCCESS; |
jngarlitos | 1:cab5535713ee | 107 | |
jngarlitos | 1:cab5535713ee | 108 | *code = ((uint16_t)(dac_code)); |
jngarlitos | 1:cab5535713ee | 109 | return ret; |
jngarlitos | 1:cab5535713ee | 110 | } |
jngarlitos | 1:cab5535713ee | 111 | |
jngarlitos | 1:cab5535713ee | 112 | /** |
jngarlitos | 1:cab5535713ee | 113 | * Write code to LTC26X6 |
jngarlitos | 1:cab5535713ee | 114 | * @param dev The device structure. |
jngarlitos | 1:cab5535713ee | 115 | * @param dac_command Command for the DAC |
jngarlitos | 1:cab5535713ee | 116 | * Accepted values: write_command |
jngarlitos | 1:cab5535713ee | 117 | * write_update_command |
jngarlitos | 1:cab5535713ee | 118 | * |
jngarlitos | 1:cab5535713ee | 119 | * @param dac_code 16-bit DAC output voltage code |
jngarlitos | 1:cab5535713ee | 120 | * @return 0 in case of success, negative error code otherwise. |
jngarlitos | 1:cab5535713ee | 121 | */ |
jngarlitos | 1:cab5535713ee | 122 | int32_t ltc26x6_write_code(struct ltc26x6_dev *dev, |
jngarlitos | 1:cab5535713ee | 123 | enum ltc26x6_write_command write_command, uint16_t dac_code) |
jngarlitos | 1:cab5535713ee | 124 | { |
jngarlitos | 1:cab5535713ee | 125 | uint8_t data[3]; |
jngarlitos | 1:cab5535713ee | 126 | |
jngarlitos | 1:cab5535713ee | 127 | data[0] = write_command; // Write command |
jngarlitos | 1:cab5535713ee | 128 | data[1] = dac_code >>8; // MSB code |
jngarlitos | 1:cab5535713ee | 129 | data[2] = dac_code & 0x00FF; // LSB code |
jngarlitos | 1:cab5535713ee | 130 | |
jngarlitos | 1:cab5535713ee | 131 | return(i2c_write(dev->i2c_desc, data, 3, 1)); |
jngarlitos | 1:cab5535713ee | 132 | } |
jngarlitos | 1:cab5535713ee | 133 | |
jngarlitos | 1:cab5535713ee | 134 | /** |
jngarlitos | 1:cab5535713ee | 135 | * Power down the device |
jngarlitos | 1:cab5535713ee | 136 | * @param device - The device structure. |
jngarlitos | 1:cab5535713ee | 137 | * @return 0 in case of success, negative error code otherwise. |
jngarlitos | 1:cab5535713ee | 138 | */ |
jngarlitos | 1:cab5535713ee | 139 | int32_t ltc26x6_power_down(struct ltc26x6_dev *dev) |
jngarlitos | 1:cab5535713ee | 140 | { |
jngarlitos | 1:cab5535713ee | 141 | uint8_t command = LTC26X6_POWER_DOWN_COMMAND; |
jngarlitos | 1:cab5535713ee | 142 | return (i2c_write(dev->i2c_desc, &command, 1, 1)); |
jngarlitos | 1:cab5535713ee | 143 | } |
jngarlitos | 1:cab5535713ee | 144 | |
jngarlitos | 1:cab5535713ee | 145 | /** |
jngarlitos | 1:cab5535713ee | 146 | * Power up the device |
jngarlitos | 1:cab5535713ee | 147 | * @param device - The device structure. |
jngarlitos | 1:cab5535713ee | 148 | * @return 0 in case of success, negative error code otherwise. |
jngarlitos | 1:cab5535713ee | 149 | */ |
jngarlitos | 1:cab5535713ee | 150 | int32_t ltc26x6_power_up(struct ltc26x6_dev *dev) |
jngarlitos | 1:cab5535713ee | 151 | { |
jngarlitos | 1:cab5535713ee | 152 | uint8_t command = LTC26X6_UPDATE_COMMAND; |
jngarlitos | 1:cab5535713ee | 153 | return (i2c_write(dev->i2c_desc, &command, 1, 1)); |
jngarlitos | 1:cab5535713ee | 154 | } |
jngarlitos | 1:cab5535713ee | 155 |