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