template libraru
ADxxxx.cpp@3:ecb47ce6f212, 2019-07-29 (annotated)
- Committer:
- malavikasaji
- Date:
- Mon Jul 29 18:35:19 2019 +0000
- Revision:
- 3:ecb47ce6f212
- Parent:
- 2:1857aa1363ef
Adding license to ADxxxx library files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
malavikasaji | 3:ecb47ce6f212 | 1 | /***************************************************************************//** |
malavikasaji | 3:ecb47ce6f212 | 2 | * @file AD1234.cpp |
malavikasaji | 3:ecb47ce6f212 | 3 | * @brief Implementation of Ad1234 Driver. |
malavikasaji | 3:ecb47ce6f212 | 4 | * @author FirstName LastName (email ID) |
malavikasaji | 3:ecb47ce6f212 | 5 | ******************************************************************************* |
malavikasaji | 3:ecb47ce6f212 | 6 | * Copyright 2013(c) Analog Devices, Inc. |
malavikasaji | 3:ecb47ce6f212 | 7 | * |
malavikasaji | 3:ecb47ce6f212 | 8 | * All rights reserved. |
malavikasaji | 3:ecb47ce6f212 | 9 | * |
malavikasaji | 3:ecb47ce6f212 | 10 | * Redistribution and use in source and binary forms, with or without |
malavikasaji | 3:ecb47ce6f212 | 11 | * modification, |
malavikasaji | 3:ecb47ce6f212 | 12 | * are permitted provided that the following conditions are met: |
malavikasaji | 3:ecb47ce6f212 | 13 | * - Redistributions of source code must retain the above copyright |
malavikasaji | 3:ecb47ce6f212 | 14 | * notice, this list of conditions and the following disclaimer. |
malavikasaji | 3:ecb47ce6f212 | 15 | * - Redistributions in binary form must reproduce the above copyright |
malavikasaji | 3:ecb47ce6f212 | 16 | * notice, this list of conditions and the following disclaimer in |
malavikasaji | 3:ecb47ce6f212 | 17 | * the documentation and/or other materials provided with the |
malavikasaji | 3:ecb47ce6f212 | 18 | * distribution. |
malavikasaji | 3:ecb47ce6f212 | 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
malavikasaji | 3:ecb47ce6f212 | 20 | * contributors may be used to endorse or promote products derived |
malavikasaji | 3:ecb47ce6f212 | 21 | * from this software without specific prior written permission. |
malavikasaji | 3:ecb47ce6f212 | 22 | * - The use of this software may or may not infringe the patent rights |
malavikasaji | 3:ecb47ce6f212 | 23 | * of one or more patent holders. This license does not release you |
malavikasaji | 3:ecb47ce6f212 | 24 | * from the requirement that you obtain separate licenses from these |
malavikasaji | 3:ecb47ce6f212 | 25 | * patent holders to use this software. |
malavikasaji | 3:ecb47ce6f212 | 26 | * - Use of the software either in source or binary form, must be run |
malavikasaji | 3:ecb47ce6f212 | 27 | * on or directly connected to an Analog Devices Inc. component. |
malavikasaji | 3:ecb47ce6f212 | 28 | * |
malavikasaji | 3:ecb47ce6f212 | 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
malavikasaji | 3:ecb47ce6f212 | 30 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
malavikasaji | 3:ecb47ce6f212 | 31 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
malavikasaji | 3:ecb47ce6f212 | 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
malavikasaji | 3:ecb47ce6f212 | 33 | * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
malavikasaji | 3:ecb47ce6f212 | 34 | * * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS |
malavikasaji | 3:ecb47ce6f212 | 35 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
malavikasaji | 3:ecb47ce6f212 | 36 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
malavikasaji | 3:ecb47ce6f212 | 37 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
malavikasaji | 3:ecb47ce6f212 | 38 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
malavikasaji | 3:ecb47ce6f212 | 39 | * DAMAGE. |
malavikasaji | 3:ecb47ce6f212 | 40 | * |
malavikasaji | 3:ecb47ce6f212 | 41 | ******************************************************************************/ |
MitchAD | 1:9524c69f480d | 42 | #include "mbed.h" /*Neccessary Include Files*/ |
MitchAD | 1:9524c69f480d | 43 | #include "ADxxxx.h" |
MitchAD | 1:9524c69f480d | 44 | |
MitchAD | 1:9524c69f480d | 45 | /*Actual Declaration of all registers |
MitchAD | 1:9524c69f480d | 46 | based on the four fields declared in |
MitchAD | 1:9524c69f480d | 47 | the header file |
MitchAD | 1:9524c69f480d | 48 | {Address, Value, Size, Read or Write}*/ |
MitchAD | 2:1857aa1363ef | 49 | struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM] = { |
MitchAD | 2:1857aa1363ef | 50 | {0x00, 0x00, 1, 1}, /*EX_ADXXXX_POWER_ON*/ |
MitchAD | 2:1857aa1363ef | 51 | {0x01, 0x00, 1, 1}, /*EX_ADXXXX_POWER_LEVEL*/ |
MitchAD | 2:1857aa1363ef | 52 | {0x02, 0x00, 1, 1}, /*EX_ADXXXX_CALIBRATION*/ |
MitchAD | 1:9524c69f480d | 53 | }; |
MitchAD | 1:9524c69f480d | 54 | |
MitchAD | 1:9524c69f480d | 55 | /*Function to malloc space for descriptor as well |
MitchAD | 1:9524c69f480d | 56 | as start the proper serial communication |
MitchAD | 1:9524c69f480d | 57 | **You should only be using either SPI or I2C at once** |
MitchAD | 1:9524c69f480d | 58 | Parameters: Pointer to a pointer to the device descriptor |
MitchAD | 1:9524c69f480d | 59 | Return Value: SUCCESS, FAILURE (You can make these more appropriate return |
MitchAD | 1:9524c69f480d | 60 | values if you would like, making it more |
MitchAD | 1:9524c69f480d | 61 | readable)*/ |
MitchAD | 2:1857aa1363ef | 62 | int adxxxx_setup(struct adxxxx_descriptor **device, struct adxxxx_init_params init_param) { |
MitchAD | 1:9524c69f480d | 63 | |
MitchAD | 1:9524c69f480d | 64 | int8_t return_val = 1; |
MitchAD | 1:9524c69f480d | 65 | struct adxxxx_descriptor * desc; |
MitchAD | 2:1857aa1363ef | 66 | extern struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM]; |
MitchAD | 1:9524c69f480d | 67 | |
MitchAD | 1:9524c69f480d | 68 | desc = (struct adxxxx_descriptor *)malloc(sizeof(*desc)); |
MitchAD | 1:9524c69f480d | 69 | if (!desc) |
MitchAD | 1:9524c69f480d | 70 | return FAILURE; |
MitchAD | 1:9524c69f480d | 71 | |
MitchAD | 1:9524c69f480d | 72 | desc->all_regs = adxxxx_all_regs; |
MitchAD | 1:9524c69f480d | 73 | |
MitchAD | 1:9524c69f480d | 74 | /*Example of turning the device on and calibrating |
MitchAD | 1:9524c69f480d | 75 | it, but clearly this is device specific*/ |
MitchAD | 2:1857aa1363ef | 76 | desc->is_calibrated = ADXXXX_NOT_CALIBRATED; |
MitchAD | 2:1857aa1363ef | 77 | desc->power_level = ADXXXX_POW_IDLE; |
MitchAD | 1:9524c69f480d | 78 | |
MitchAD | 2:1857aa1363ef | 79 | /*Uncomment the protocol your device uses*/ |
MitchAD | 1:9524c69f480d | 80 | //return_val = spi_init(&desc->spi_desc, &init_params.spi_init); |
MitchAD | 1:9524c69f480d | 81 | //return_val = i2c_init(&desc->i2c_desc, &init_params.i2c_init); |
MitchAD | 1:9524c69f480d | 82 | if (return_val < 1) |
MitchAD | 1:9524c69f480d | 83 | return FAILURE; |
MitchAD | 1:9524c69f480d | 84 | |
MitchAD | 1:9524c69f480d | 85 | *device = desc; |
MitchAD | 1:9524c69f480d | 86 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 87 | } |
MitchAD | 2:1857aa1363ef | 88 | |
MitchAD | 2:1857aa1363ef | 89 | /*Function to sweep through all of the registers and set them |
MitchAD | 2:1857aa1363ef | 90 | to their initial values |
MitchAD | 2:1857aa1363ef | 91 | Parameters: Pointer to adxxxx's descriptor |
MitchAD | 2:1857aa1363ef | 92 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 93 | int adxxxx_init_regs(struct adxxxx_descriptor *device) { |
MitchAD | 2:1857aa1363ef | 94 | /*Sweep through all of the registers that you initialized |
MitchAD | 2:1857aa1363ef | 95 | in the struct above and write all the values to the device |
MitchAD | 2:1857aa1363ef | 96 | (Could return register that failed to be written to may be |
MitchAD | 2:1857aa1363ef | 97 | helpful for debugging)*/ |
MitchAD | 2:1857aa1363ef | 98 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 99 | } |
MitchAD | 2:1857aa1363ef | 100 | |
MitchAD | 2:1857aa1363ef | 101 | /*Function to make a single or multi read from the device, based off |
MitchAD | 2:1857aa1363ef | 102 | the registers size which you can find in the devices data sheet |
MitchAD | 2:1857aa1363ef | 103 | Parameters: Pointer to adxxxx's descriptor, register struct to read from |
MitchAD | 2:1857aa1363ef | 104 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 105 | int adxxx_read(struct adxxxx_descriptor *device, struct adxxxx_reg* reg) { |
MitchAD | 2:1857aa1363ef | 106 | /*Follow your devices specific write protocol (I2C, SPI) in conjunction |
MitchAD | 2:1857aa1363ef | 107 | with the platform specific drivers to effectively communicate with your |
MitchAD | 2:1857aa1363ef | 108 | device (A good first step is perhaps reading your devices ID register |
MitchAD | 2:1857aa1363ef | 109 | or something else that is easy to get to)*/ |
MitchAD | 2:1857aa1363ef | 110 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 111 | } |
MitchAD | 2:1857aa1363ef | 112 | |
MitchAD | 2:1857aa1363ef | 113 | /*Function to make a single or multi write to the device, based off |
MitchAD | 2:1857aa1363ef | 114 | the registers size which you can find in the devices data sheet |
MitchAD | 2:1857aa1363ef | 115 | Parameters: Pointer to adxxxx's descriptor, register struct to read from |
MitchAD | 2:1857aa1363ef | 116 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 117 | int adxxx_write(struct adxxxx_descriptor *device, struct adxxxx_reg* reg, uint32_t data) { |
MitchAD | 2:1857aa1363ef | 118 | /*Follow your devices specific write protocol (I2C, SPI) in conjunction |
MitchAD | 2:1857aa1363ef | 119 | with the platform specific drivers to effectively communicate with your |
MitchAD | 2:1857aa1363ef | 120 | device (Try and write to a read-enabled register and then immediately |
MitchAD | 2:1857aa1363ef | 121 | read back the value you wrote)*/ |
MitchAD | 2:1857aa1363ef | 122 | reg->value = data; |
MitchAD | 2:1857aa1363ef | 123 | return SUCCESS; |
MitchAD | 1:9524c69f480d | 124 | } |