Example program for EVAL-ADMX2001
Dependencies: ADMX2001
platform_drivers/src/i2c.cpp@15:fca7551aaf0a, 2021-11-17 (annotated)
- Committer:
- nsheth
- Date:
- Wed Nov 17 18:15:37 2021 +0000
- Revision:
- 15:fca7551aaf0a
- Parent:
- 9:29db35656fcb
Updating default .lib file for eval platform
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nsheth | 9:29db35656fcb | 1 | /***************************************************************************//** |
nsheth | 9:29db35656fcb | 2 | * @file i2c.cpp |
nsheth | 9:29db35656fcb | 3 | * @brief Implementation of I2C No-OS platform driver interfaces |
nsheth | 9:29db35656fcb | 4 | ******************************************************************************** |
nsheth | 9:29db35656fcb | 5 | * Copyright (c) 2019, 2020 Analog Devices, Inc. |
nsheth | 9:29db35656fcb | 6 | * |
nsheth | 9:29db35656fcb | 7 | * All rights reserved. |
nsheth | 9:29db35656fcb | 8 | * |
nsheth | 9:29db35656fcb | 9 | * This software is proprietary to Analog Devices, Inc. and its licensors. |
nsheth | 9:29db35656fcb | 10 | * By using this software you agree to the terms of the associated |
nsheth | 9:29db35656fcb | 11 | * Analog Devices Software License Agreement. |
nsheth | 9:29db35656fcb | 12 | *******************************************************************************/ |
nsheth | 9:29db35656fcb | 13 | |
nsheth | 9:29db35656fcb | 14 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 15 | /***************************** Include Files **********************************/ |
nsheth | 9:29db35656fcb | 16 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 17 | |
nsheth | 9:29db35656fcb | 18 | #include <stdio.h> |
nsheth | 9:29db35656fcb | 19 | #include <mbed.h> |
nsheth | 9:29db35656fcb | 20 | |
nsheth | 9:29db35656fcb | 21 | #include "platform_drivers.h" |
nsheth | 9:29db35656fcb | 22 | #include "i2c_extra.h" |
nsheth | 9:29db35656fcb | 23 | |
nsheth | 9:29db35656fcb | 24 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 25 | /********************** Macros and Constants Definitions **********************/ |
nsheth | 9:29db35656fcb | 26 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 27 | |
nsheth | 9:29db35656fcb | 28 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 29 | /************************ Functions Declarations ******************************/ |
nsheth | 9:29db35656fcb | 30 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 31 | |
nsheth | 9:29db35656fcb | 32 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 33 | /************************ Functions Definitions *******************************/ |
nsheth | 9:29db35656fcb | 34 | /******************************************************************************/ |
nsheth | 9:29db35656fcb | 35 | |
nsheth | 9:29db35656fcb | 36 | /** |
nsheth | 9:29db35656fcb | 37 | * @brief Initialize the I2C communication peripheral. |
nsheth | 9:29db35656fcb | 38 | * @param desc - The I2C descriptor. |
nsheth | 9:29db35656fcb | 39 | * @param param - The structure that contains the I2C parameters. |
nsheth | 9:29db35656fcb | 40 | * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. |
nsheth | 9:29db35656fcb | 41 | */ |
nsheth | 9:29db35656fcb | 42 | int32_t i2c_init_noos(struct i2c_desc **desc, |
nsheth | 9:29db35656fcb | 43 | const struct i2c_init_param *param) |
nsheth | 9:29db35656fcb | 44 | { |
nsheth | 9:29db35656fcb | 45 | mbed::I2C *i2c; // pointer to new I2C instance |
nsheth | 9:29db35656fcb | 46 | mbed_i2c_desc *mbed_desc; // pointer to mbed i2c desc |
nsheth | 9:29db35656fcb | 47 | |
nsheth | 9:29db35656fcb | 48 | if (desc) { |
nsheth | 9:29db35656fcb | 49 | // Create an i2c descriptor object for the device |
nsheth | 9:29db35656fcb | 50 | i2c_desc *new_desc = (i2c_desc *)malloc(sizeof(i2c_desc)); |
nsheth | 9:29db35656fcb | 51 | if (new_desc == NULL) { |
nsheth | 9:29db35656fcb | 52 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 53 | } |
nsheth | 9:29db35656fcb | 54 | |
nsheth | 9:29db35656fcb | 55 | new_desc->slave_address = param->slave_address; |
nsheth | 9:29db35656fcb | 56 | |
nsheth | 9:29db35656fcb | 57 | // Configure and instantiate I2C protocol |
nsheth | 9:29db35656fcb | 58 | i2c = new I2C( |
nsheth | 9:29db35656fcb | 59 | (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_sda_pin), |
nsheth | 9:29db35656fcb | 60 | (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_scl_pin)); |
nsheth | 9:29db35656fcb | 61 | |
nsheth | 9:29db35656fcb | 62 | if (i2c == NULL) { |
nsheth | 9:29db35656fcb | 63 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 64 | } |
nsheth | 9:29db35656fcb | 65 | |
nsheth | 9:29db35656fcb | 66 | // Create the i2c mbed descriptor object to store new i2c instance |
nsheth | 9:29db35656fcb | 67 | mbed_desc = (mbed_i2c_desc *)malloc(sizeof(mbed_i2c_desc)); |
nsheth | 9:29db35656fcb | 68 | if (mbed_desc == NULL) { |
nsheth | 9:29db35656fcb | 69 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 70 | } |
nsheth | 9:29db35656fcb | 71 | |
nsheth | 9:29db35656fcb | 72 | mbed_desc->i2c_port = (I2C *)i2c; |
nsheth | 9:29db35656fcb | 73 | new_desc->extra = (mbed_i2c_desc *)mbed_desc; |
nsheth | 9:29db35656fcb | 74 | |
nsheth | 9:29db35656fcb | 75 | *desc = new_desc; |
nsheth | 9:29db35656fcb | 76 | |
nsheth | 9:29db35656fcb | 77 | return ADI_SUCCESS; |
nsheth | 9:29db35656fcb | 78 | } |
nsheth | 9:29db35656fcb | 79 | |
nsheth | 9:29db35656fcb | 80 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 81 | } |
nsheth | 9:29db35656fcb | 82 | |
nsheth | 9:29db35656fcb | 83 | |
nsheth | 9:29db35656fcb | 84 | /** |
nsheth | 9:29db35656fcb | 85 | * @brief Free the resources allocated by i2c_init_noos(). |
nsheth | 9:29db35656fcb | 86 | * @param desc - The I2C descriptor. |
nsheth | 9:29db35656fcb | 87 | * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. |
nsheth | 9:29db35656fcb | 88 | */ |
nsheth | 9:29db35656fcb | 89 | int32_t i2c_remove(struct i2c_desc *desc) |
nsheth | 9:29db35656fcb | 90 | { |
nsheth | 9:29db35656fcb | 91 | if (desc) { |
nsheth | 9:29db35656fcb | 92 | // Free the I2C port object |
nsheth | 9:29db35656fcb | 93 | if ((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)) { |
nsheth | 9:29db35656fcb | 94 | delete((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)); |
nsheth | 9:29db35656fcb | 95 | } |
nsheth | 9:29db35656fcb | 96 | |
nsheth | 9:29db35656fcb | 97 | // Free the I2C extra descriptor object |
nsheth | 9:29db35656fcb | 98 | if ((mbed_i2c_desc *)(desc->extra)) { |
nsheth | 9:29db35656fcb | 99 | free((mbed_i2c_desc *)(desc->extra)); |
nsheth | 9:29db35656fcb | 100 | } |
nsheth | 9:29db35656fcb | 101 | |
nsheth | 9:29db35656fcb | 102 | // Free the I2C descriptor object |
nsheth | 9:29db35656fcb | 103 | free(desc); |
nsheth | 9:29db35656fcb | 104 | |
nsheth | 9:29db35656fcb | 105 | return ADI_SUCCESS; |
nsheth | 9:29db35656fcb | 106 | } |
nsheth | 9:29db35656fcb | 107 | |
nsheth | 9:29db35656fcb | 108 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 109 | } |
nsheth | 9:29db35656fcb | 110 | |
nsheth | 9:29db35656fcb | 111 | |
nsheth | 9:29db35656fcb | 112 | /** |
nsheth | 9:29db35656fcb | 113 | * @brief Write data to a slave device. |
nsheth | 9:29db35656fcb | 114 | * @param desc - The I2C descriptor. |
nsheth | 9:29db35656fcb | 115 | * @param data - Buffer that stores the transmission data. |
nsheth | 9:29db35656fcb | 116 | * @param bytes_number - Number of bytes to write. |
nsheth | 9:29db35656fcb | 117 | * @param stop_bit - Stop condition control. |
nsheth | 9:29db35656fcb | 118 | * Example: 0 - A stop condition will not be generated; |
nsheth | 9:29db35656fcb | 119 | * 1 - A stop condition will be generated. |
nsheth | 9:29db35656fcb | 120 | * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. |
nsheth | 9:29db35656fcb | 121 | */ |
nsheth | 9:29db35656fcb | 122 | |
nsheth | 9:29db35656fcb | 123 | int32_t i2c_write_noos(struct i2c_desc *desc, |
nsheth | 9:29db35656fcb | 124 | uint8_t *data, |
nsheth | 9:29db35656fcb | 125 | uint8_t bytes_number, |
nsheth | 9:29db35656fcb | 126 | uint8_t stop_bit) |
nsheth | 9:29db35656fcb | 127 | { |
nsheth | 9:29db35656fcb | 128 | mbed::I2C *i2c; |
nsheth | 9:29db35656fcb | 129 | i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); |
nsheth | 9:29db35656fcb | 130 | |
nsheth | 9:29db35656fcb | 131 | /** |
nsheth | 9:29db35656fcb | 132 | The MBED I2C API is reversed for parameter 4 |
nsheth | 9:29db35656fcb | 133 | Instead of stop_bit - it has |
nsheth | 9:29db35656fcb | 134 | @param repeated - Repeated start, true - don't send stop at end default value is false. |
nsheth | 9:29db35656fcb | 135 | Inverting here to keep the no-OS/platform_drivers API |
nsheth | 9:29db35656fcb | 136 | */ |
nsheth | 9:29db35656fcb | 137 | if (!(i2c->write(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { |
nsheth | 9:29db35656fcb | 138 | return ADI_SUCCESS; |
nsheth | 9:29db35656fcb | 139 | } else { |
nsheth | 9:29db35656fcb | 140 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 141 | } |
nsheth | 9:29db35656fcb | 142 | } |
nsheth | 9:29db35656fcb | 143 | |
nsheth | 9:29db35656fcb | 144 | |
nsheth | 9:29db35656fcb | 145 | /** |
nsheth | 9:29db35656fcb | 146 | * @brief Read data from a slave device. |
nsheth | 9:29db35656fcb | 147 | * @param desc - The I2C descriptor. |
nsheth | 9:29db35656fcb | 148 | * @param data - Buffer that will store the received data. |
nsheth | 9:29db35656fcb | 149 | * @param bytes_number - Number of bytes to read. |
nsheth | 9:29db35656fcb | 150 | * @param stop_bit - Stop condition control. |
nsheth | 9:29db35656fcb | 151 | * Example: 0 - A stop condition will not be generated; |
nsheth | 9:29db35656fcb | 152 | * 1 - A stop condition will be generated. |
nsheth | 9:29db35656fcb | 153 | * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. |
nsheth | 9:29db35656fcb | 154 | */ |
nsheth | 9:29db35656fcb | 155 | int32_t i2c_read_noos(struct i2c_desc *desc, |
nsheth | 9:29db35656fcb | 156 | uint8_t *data, |
nsheth | 9:29db35656fcb | 157 | uint8_t bytes_number, |
nsheth | 9:29db35656fcb | 158 | uint8_t stop_bit) |
nsheth | 9:29db35656fcb | 159 | { |
nsheth | 9:29db35656fcb | 160 | mbed::I2C *i2c; |
nsheth | 9:29db35656fcb | 161 | i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); |
nsheth | 9:29db35656fcb | 162 | |
nsheth | 9:29db35656fcb | 163 | /** |
nsheth | 9:29db35656fcb | 164 | The MBED I2C API is reversed for parameter 4 |
nsheth | 9:29db35656fcb | 165 | Instead of stop_bit - it has |
nsheth | 9:29db35656fcb | 166 | @param repeated - Repeated start, true - don't send stop at end default value is false. |
nsheth | 9:29db35656fcb | 167 | Inverting here to keep the no-OS/platform_drivers API |
nsheth | 9:29db35656fcb | 168 | */ |
nsheth | 9:29db35656fcb | 169 | if (!(i2c->read(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { |
nsheth | 9:29db35656fcb | 170 | return ADI_SUCCESS; |
nsheth | 9:29db35656fcb | 171 | } else { |
nsheth | 9:29db35656fcb | 172 | return ADI_FAILURE; |
nsheth | 9:29db35656fcb | 173 | } |
nsheth | 9:29db35656fcb | 174 | } |