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.
Dependencies: ADMX2001
i2c.cpp
00001 /***************************************************************************//** 00002 * @file i2c.cpp 00003 * @brief Implementation of I2C No-OS platform driver interfaces 00004 ******************************************************************************** 00005 * Copyright (c) 2019, 2020 Analog Devices, Inc. 00006 * 00007 * All rights reserved. 00008 * 00009 * This software is proprietary to Analog Devices, Inc. and its licensors. 00010 * By using this software you agree to the terms of the associated 00011 * Analog Devices Software License Agreement. 00012 *******************************************************************************/ 00013 00014 /******************************************************************************/ 00015 /***************************** Include Files **********************************/ 00016 /******************************************************************************/ 00017 00018 #include <stdio.h> 00019 #include <mbed.h> 00020 00021 #include "platform_drivers.h" 00022 #include "i2c_extra.h" 00023 00024 /******************************************************************************/ 00025 /********************** Macros and Constants Definitions **********************/ 00026 /******************************************************************************/ 00027 00028 /******************************************************************************/ 00029 /************************ Functions Declarations ******************************/ 00030 /******************************************************************************/ 00031 00032 /******************************************************************************/ 00033 /************************ Functions Definitions *******************************/ 00034 /******************************************************************************/ 00035 00036 /** 00037 * @brief Initialize the I2C communication peripheral. 00038 * @param desc - The I2C descriptor. 00039 * @param param - The structure that contains the I2C parameters. 00040 * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. 00041 */ 00042 int32_t i2c_init_noos(struct i2c_desc **desc, 00043 const struct i2c_init_param *param) 00044 { 00045 mbed::I2C *i2c; // pointer to new I2C instance 00046 mbed_i2c_desc *mbed_desc; // pointer to mbed i2c desc 00047 00048 if (desc) { 00049 // Create an i2c descriptor object for the device 00050 i2c_desc *new_desc = (i2c_desc *)malloc(sizeof(i2c_desc)); 00051 if (new_desc == NULL) { 00052 return ADI_FAILURE; 00053 } 00054 00055 new_desc->slave_address = param->slave_address; 00056 00057 // Configure and instantiate I2C protocol 00058 i2c = new I2C( 00059 (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_sda_pin), 00060 (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_scl_pin)); 00061 00062 if (i2c == NULL) { 00063 return ADI_FAILURE; 00064 } 00065 00066 // Create the i2c mbed descriptor object to store new i2c instance 00067 mbed_desc = (mbed_i2c_desc *)malloc(sizeof(mbed_i2c_desc)); 00068 if (mbed_desc == NULL) { 00069 return ADI_FAILURE; 00070 } 00071 00072 mbed_desc->i2c_port = (I2C *)i2c; 00073 new_desc->extra = (mbed_i2c_desc *)mbed_desc; 00074 00075 *desc = new_desc; 00076 00077 return ADI_SUCCESS; 00078 } 00079 00080 return ADI_FAILURE; 00081 } 00082 00083 00084 /** 00085 * @brief Free the resources allocated by i2c_init_noos(). 00086 * @param desc - The I2C descriptor. 00087 * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. 00088 */ 00089 int32_t i2c_remove(struct i2c_desc *desc) 00090 { 00091 if (desc) { 00092 // Free the I2C port object 00093 if ((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)) { 00094 delete((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)); 00095 } 00096 00097 // Free the I2C extra descriptor object 00098 if ((mbed_i2c_desc *)(desc->extra)) { 00099 free((mbed_i2c_desc *)(desc->extra)); 00100 } 00101 00102 // Free the I2C descriptor object 00103 free(desc); 00104 00105 return ADI_SUCCESS; 00106 } 00107 00108 return ADI_FAILURE; 00109 } 00110 00111 00112 /** 00113 * @brief Write data to a slave device. 00114 * @param desc - The I2C descriptor. 00115 * @param data - Buffer that stores the transmission data. 00116 * @param bytes_number - Number of bytes to write. 00117 * @param stop_bit - Stop condition control. 00118 * Example: 0 - A stop condition will not be generated; 00119 * 1 - A stop condition will be generated. 00120 * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. 00121 */ 00122 00123 int32_t i2c_write_noos(struct i2c_desc *desc, 00124 uint8_t *data, 00125 uint8_t bytes_number, 00126 uint8_t stop_bit) 00127 { 00128 mbed::I2C *i2c; 00129 i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); 00130 00131 /** 00132 The MBED I2C API is reversed for parameter 4 00133 Instead of stop_bit - it has 00134 @param repeated - Repeated start, true - don't send stop at end default value is false. 00135 Inverting here to keep the no-OS/platform_drivers API 00136 */ 00137 if (!(i2c->write(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { 00138 return ADI_SUCCESS; 00139 } else { 00140 return ADI_FAILURE; 00141 } 00142 } 00143 00144 00145 /** 00146 * @brief Read data from a slave device. 00147 * @param desc - The I2C descriptor. 00148 * @param data - Buffer that will store the received data. 00149 * @param bytes_number - Number of bytes to read. 00150 * @param stop_bit - Stop condition control. 00151 * Example: 0 - A stop condition will not be generated; 00152 * 1 - A stop condition will be generated. 00153 * @return ADI_SUCCESS in case of ADI_SUCCESS, ADI_FAILURE otherwise. 00154 */ 00155 int32_t i2c_read_noos(struct i2c_desc *desc, 00156 uint8_t *data, 00157 uint8_t bytes_number, 00158 uint8_t stop_bit) 00159 { 00160 mbed::I2C *i2c; 00161 i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); 00162 00163 /** 00164 The MBED I2C API is reversed for parameter 4 00165 Instead of stop_bit - it has 00166 @param repeated - Repeated start, true - don't send stop at end default value is false. 00167 Inverting here to keep the no-OS/platform_drivers API 00168 */ 00169 if (!(i2c->read(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { 00170 return ADI_SUCCESS; 00171 } else { 00172 return ADI_FAILURE; 00173 } 00174 }
Generated on Tue Jul 12 2022 17:25:36 by
1.7.2