Enda Kilgarriff / platform_drivers
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c.cpp Source File

i2c.cpp

Go to the documentation of this file.
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 SUCCESS in case of success, 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 FAILURE;
00053         }
00054 
00055         // Address passed in parameter shifted left by 1 to form
00056         // 7-bit i2c slave address (7 MSBs) and the LSB acts as
00057         // r/w bit during i2c read/write operations
00058         new_desc->slave_address = ((param->slave_address) << 1);
00059 
00060         // Configure and instantiate I2C protocol
00061         i2c = new I2C(
00062             (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_sda_pin),
00063             (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_scl_pin));
00064 
00065         if (i2c == NULL) {
00066             return FAILURE;
00067         }
00068 
00069         // Create the i2c mbed descriptor object to store new i2c instance
00070         mbed_desc = (mbed_i2c_desc *)malloc(sizeof(mbed_i2c_desc));
00071         if (mbed_desc == NULL) {
00072             return FAILURE;
00073         }
00074 
00075         mbed_desc->i2c_port = (I2C *)i2c;
00076         new_desc->extra = (mbed_i2c_desc *)mbed_desc;
00077 
00078         *desc = new_desc;
00079 
00080         return SUCCESS;
00081     }
00082 
00083     return FAILURE;
00084 }
00085 
00086 
00087 /**
00088  * @brief Free the resources allocated by i2c_init_noos().
00089  * @param desc - The I2C descriptor.
00090  * @return SUCCESS in case of success, FAILURE otherwise.
00091  */
00092 int32_t i2c_remove(struct i2c_desc *desc)
00093 {
00094     if (desc) {
00095         // Free the I2C port object
00096         if ((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)) {
00097             delete((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port));
00098         }
00099 
00100         // Free the I2C extra descriptor object
00101         if ((mbed_i2c_desc *)(desc->extra)) {
00102             free((mbed_i2c_desc *)(desc->extra));
00103         }
00104 
00105         // Free the I2C descriptor object
00106         free(desc);
00107 
00108         return SUCCESS;
00109     }
00110 
00111     return FAILURE;
00112 }
00113 
00114 
00115 /**
00116  * @brief Write data to a slave device.
00117  * @param desc - The I2C descriptor.
00118  * @param data - Buffer that stores the transmission data.
00119  * @param bytes_number - Number of bytes to write.
00120  * @param stop_bit - Stop condition control.
00121  *                   Example: 0 - A stop condition will not be generated;
00122  *                            1 - A stop condition will be generated.
00123  * @return SUCCESS in case of success, FAILURE otherwise.
00124  */
00125 
00126 int32_t i2c_write_noos(struct i2c_desc *desc,
00127                uint8_t *data,
00128                uint8_t bytes_number,
00129                uint8_t stop_bit)
00130 {
00131     mbed::I2C *i2c;
00132     i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port);
00133 
00134     /**
00135         The MBED I2C API is reversed for parameter 4
00136         Instead of stop_bit - it has
00137         @param repeated   - Repeated start, true - don't send stop at end default value is false.
00138         Inverting here to keep the no-OS/platform_drivers API
00139      */
00140     if (!(i2c->write(desc->slave_address, (char *)data, bytes_number, !stop_bit))) {
00141         return SUCCESS;
00142     } else {
00143         return FAILURE;
00144     }
00145 }
00146 
00147 
00148 /**
00149  * @brief Read data from a slave device.
00150  * @param desc - The I2C descriptor.
00151  * @param data - Buffer that will store the received data.
00152  * @param bytes_number - Number of bytes to read.
00153  * @param stop_bit - Stop condition control.
00154  *                   Example: 0 - A stop condition will not be generated;
00155  *                            1 - A stop condition will be generated.
00156  * @return SUCCESS in case of success, FAILURE otherwise.
00157  */
00158 int32_t i2c_read_noos(struct i2c_desc *desc,
00159               uint8_t *data,
00160               uint8_t bytes_number,
00161               uint8_t stop_bit)
00162 {
00163     mbed::I2C *i2c;
00164     i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port);
00165 
00166     /**
00167         The MBED I2C API is reversed for parameter 4
00168         Instead of stop_bit - it has
00169         @param repeated   - Repeated start, true - don't send stop at end default value is false.
00170         Inverting here to keep the no-OS/platform_drivers API
00171      */
00172     if (!(i2c->read(desc->slave_address, (char *)data, bytes_number, !stop_bit))) {
00173         return SUCCESS;
00174     } else {
00175         return FAILURE;
00176     }
00177 }