Update platform drivers
src/i2c.cpp@10:b5115cd6b916, 2020-06-17 (annotated)
- Committer:
- EndaKilgarriff
- Date:
- Wed Jun 17 14:54:14 2020 +0000
- Revision:
- 10:b5115cd6b916
- Parent:
- 9:9e247b9c9abf
Roll back delay.cpp to previous version
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 | |
EndaKilgarriff | 9:9e247b9c9abf | 55 | // Address passed in parameter shifted left by 1 to form |
EndaKilgarriff | 9:9e247b9c9abf | 56 | // 7-bit i2c slave address (7 MSBs) and the LSB acts as |
EndaKilgarriff | 9:9e247b9c9abf | 57 | // r/w bit during i2c read/write operations |
EndaKilgarriff | 9:9e247b9c9abf | 58 | new_desc->slave_address = ((param->slave_address) << 1); |
mahphalke | 8:70fc373a5f46 | 59 | |
mahphalke | 8:70fc373a5f46 | 60 | // Configure and instantiate I2C protocol |
mahphalke | 8:70fc373a5f46 | 61 | i2c = new I2C( |
mahphalke | 8:70fc373a5f46 | 62 | (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_sda_pin), |
mahphalke | 8:70fc373a5f46 | 63 | (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_scl_pin)); |
mahphalke | 8:70fc373a5f46 | 64 | |
mahphalke | 8:70fc373a5f46 | 65 | if (i2c == NULL) { |
mahphalke | 8:70fc373a5f46 | 66 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 67 | } |
mahphalke | 8:70fc373a5f46 | 68 | |
mahphalke | 8:70fc373a5f46 | 69 | // Create the i2c mbed descriptor object to store new i2c instance |
mahphalke | 8:70fc373a5f46 | 70 | mbed_desc = (mbed_i2c_desc *)malloc(sizeof(mbed_i2c_desc)); |
mahphalke | 8:70fc373a5f46 | 71 | if (mbed_desc == NULL) { |
mahphalke | 8:70fc373a5f46 | 72 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 73 | } |
mahphalke | 8:70fc373a5f46 | 74 | |
mahphalke | 8:70fc373a5f46 | 75 | mbed_desc->i2c_port = (I2C *)i2c; |
mahphalke | 8:70fc373a5f46 | 76 | new_desc->extra = (mbed_i2c_desc *)mbed_desc; |
mahphalke | 8:70fc373a5f46 | 77 | |
mahphalke | 8:70fc373a5f46 | 78 | *desc = new_desc; |
mahphalke | 8:70fc373a5f46 | 79 | |
mahphalke | 8:70fc373a5f46 | 80 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 81 | } |
mahphalke | 8:70fc373a5f46 | 82 | |
mahphalke | 8:70fc373a5f46 | 83 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 84 | } |
mahphalke | 8:70fc373a5f46 | 85 | |
mahphalke | 8:70fc373a5f46 | 86 | |
mahphalke | 8:70fc373a5f46 | 87 | /** |
mahphalke | 8:70fc373a5f46 | 88 | * @brief Free the resources allocated by i2c_init_noos(). |
mahphalke | 8:70fc373a5f46 | 89 | * @param desc - The I2C descriptor. |
mahphalke | 8:70fc373a5f46 | 90 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 91 | */ |
mahphalke | 8:70fc373a5f46 | 92 | int32_t i2c_remove(struct i2c_desc *desc) |
mahphalke | 8:70fc373a5f46 | 93 | { |
mahphalke | 8:70fc373a5f46 | 94 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 95 | // Free the I2C port object |
mahphalke | 8:70fc373a5f46 | 96 | if ((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)) { |
mahphalke | 8:70fc373a5f46 | 97 | delete((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)); |
mahphalke | 8:70fc373a5f46 | 98 | } |
mahphalke | 8:70fc373a5f46 | 99 | |
mahphalke | 8:70fc373a5f46 | 100 | // Free the I2C extra descriptor object |
mahphalke | 8:70fc373a5f46 | 101 | if ((mbed_i2c_desc *)(desc->extra)) { |
mahphalke | 8:70fc373a5f46 | 102 | free((mbed_i2c_desc *)(desc->extra)); |
mahphalke | 8:70fc373a5f46 | 103 | } |
mahphalke | 8:70fc373a5f46 | 104 | |
mahphalke | 8:70fc373a5f46 | 105 | // Free the I2C descriptor object |
mahphalke | 8:70fc373a5f46 | 106 | free(desc); |
mahphalke | 8:70fc373a5f46 | 107 | |
mahphalke | 8:70fc373a5f46 | 108 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 109 | } |
mahphalke | 8:70fc373a5f46 | 110 | |
mahphalke | 8:70fc373a5f46 | 111 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 112 | } |
mahphalke | 8:70fc373a5f46 | 113 | |
mahphalke | 8:70fc373a5f46 | 114 | |
mahphalke | 8:70fc373a5f46 | 115 | /** |
mahphalke | 8:70fc373a5f46 | 116 | * @brief Write data to a slave device. |
mahphalke | 8:70fc373a5f46 | 117 | * @param desc - The I2C descriptor. |
mahphalke | 8:70fc373a5f46 | 118 | * @param data - Buffer that stores the transmission data. |
mahphalke | 8:70fc373a5f46 | 119 | * @param bytes_number - Number of bytes to write. |
mahphalke | 8:70fc373a5f46 | 120 | * @param stop_bit - Stop condition control. |
mahphalke | 8:70fc373a5f46 | 121 | * Example: 0 - A stop condition will not be generated; |
mahphalke | 8:70fc373a5f46 | 122 | * 1 - A stop condition will be generated. |
mahphalke | 8:70fc373a5f46 | 123 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 124 | */ |
mahphalke | 8:70fc373a5f46 | 125 | |
mahphalke | 8:70fc373a5f46 | 126 | int32_t i2c_write_noos(struct i2c_desc *desc, |
mahphalke | 8:70fc373a5f46 | 127 | uint8_t *data, |
mahphalke | 8:70fc373a5f46 | 128 | uint8_t bytes_number, |
mahphalke | 8:70fc373a5f46 | 129 | uint8_t stop_bit) |
mahphalke | 8:70fc373a5f46 | 130 | { |
mahphalke | 8:70fc373a5f46 | 131 | mbed::I2C *i2c; |
mahphalke | 8:70fc373a5f46 | 132 | i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); |
mahphalke | 8:70fc373a5f46 | 133 | |
mahphalke | 8:70fc373a5f46 | 134 | /** |
mahphalke | 8:70fc373a5f46 | 135 | The MBED I2C API is reversed for parameter 4 |
mahphalke | 8:70fc373a5f46 | 136 | Instead of stop_bit - it has |
mahphalke | 8:70fc373a5f46 | 137 | @param repeated - Repeated start, true - don't send stop at end default value is false. |
mahphalke | 8:70fc373a5f46 | 138 | Inverting here to keep the no-OS/platform_drivers API |
mahphalke | 8:70fc373a5f46 | 139 | */ |
mahphalke | 8:70fc373a5f46 | 140 | if (!(i2c->write(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { |
mahphalke | 8:70fc373a5f46 | 141 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 142 | } else { |
mahphalke | 8:70fc373a5f46 | 143 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 144 | } |
mahphalke | 8:70fc373a5f46 | 145 | } |
mahphalke | 8:70fc373a5f46 | 146 | |
mahphalke | 8:70fc373a5f46 | 147 | |
mahphalke | 8:70fc373a5f46 | 148 | /** |
mahphalke | 8:70fc373a5f46 | 149 | * @brief Read data from a slave device. |
mahphalke | 8:70fc373a5f46 | 150 | * @param desc - The I2C descriptor. |
mahphalke | 8:70fc373a5f46 | 151 | * @param data - Buffer that will store the received data. |
mahphalke | 8:70fc373a5f46 | 152 | * @param bytes_number - Number of bytes to read. |
mahphalke | 8:70fc373a5f46 | 153 | * @param stop_bit - Stop condition control. |
mahphalke | 8:70fc373a5f46 | 154 | * Example: 0 - A stop condition will not be generated; |
mahphalke | 8:70fc373a5f46 | 155 | * 1 - A stop condition will be generated. |
mahphalke | 8:70fc373a5f46 | 156 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 157 | */ |
mahphalke | 8:70fc373a5f46 | 158 | int32_t i2c_read_noos(struct i2c_desc *desc, |
mahphalke | 8:70fc373a5f46 | 159 | uint8_t *data, |
mahphalke | 8:70fc373a5f46 | 160 | uint8_t bytes_number, |
mahphalke | 8:70fc373a5f46 | 161 | uint8_t stop_bit) |
mahphalke | 8:70fc373a5f46 | 162 | { |
mahphalke | 8:70fc373a5f46 | 163 | mbed::I2C *i2c; |
mahphalke | 8:70fc373a5f46 | 164 | i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); |
mahphalke | 8:70fc373a5f46 | 165 | |
mahphalke | 8:70fc373a5f46 | 166 | /** |
mahphalke | 8:70fc373a5f46 | 167 | The MBED I2C API is reversed for parameter 4 |
mahphalke | 8:70fc373a5f46 | 168 | Instead of stop_bit - it has |
mahphalke | 8:70fc373a5f46 | 169 | @param repeated - Repeated start, true - don't send stop at end default value is false. |
mahphalke | 8:70fc373a5f46 | 170 | Inverting here to keep the no-OS/platform_drivers API |
mahphalke | 8:70fc373a5f46 | 171 | */ |
mahphalke | 8:70fc373a5f46 | 172 | if (!(i2c->read(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { |
mahphalke | 8:70fc373a5f46 | 173 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 174 | } else { |
mahphalke | 8:70fc373a5f46 | 175 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 176 | } |
mahphalke | 8:70fc373a5f46 | 177 | } |