MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgabino 0:c76361bd82e8 1 /* mbed Microcontroller Library
dgabino 0:c76361bd82e8 2 * Copyright (c) 2006-2015 ARM Limited
dgabino 0:c76361bd82e8 3 *
dgabino 0:c76361bd82e8 4 * Licensed under the Apache License, Version 2.0 (the "License");
dgabino 0:c76361bd82e8 5 * you may not use this file except in compliance with the License.
dgabino 0:c76361bd82e8 6 * You may obtain a copy of the License at
dgabino 0:c76361bd82e8 7 *
dgabino 0:c76361bd82e8 8 * http://www.apache.org/licenses/LICENSE-2.0
dgabino 0:c76361bd82e8 9 *
dgabino 0:c76361bd82e8 10 * Unless required by applicable law or agreed to in writing, software
dgabino 0:c76361bd82e8 11 * distributed under the License is distributed on an "AS IS" BASIS,
dgabino 0:c76361bd82e8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dgabino 0:c76361bd82e8 13 * See the License for the specific language governing permissions and
dgabino 0:c76361bd82e8 14 * limitations under the License.
dgabino 0:c76361bd82e8 15 */
dgabino 0:c76361bd82e8 16 #ifndef MBED_I2C_API_H
dgabino 0:c76361bd82e8 17 #define MBED_I2C_API_H
dgabino 0:c76361bd82e8 18
dgabino 0:c76361bd82e8 19 #include "device.h"
dgabino 0:c76361bd82e8 20 #include "buffer.h"
dgabino 0:c76361bd82e8 21
dgabino 0:c76361bd82e8 22 #if DEVICE_I2C_ASYNCH
dgabino 0:c76361bd82e8 23 #include "dma_api.h"
dgabino 0:c76361bd82e8 24 #endif
dgabino 0:c76361bd82e8 25
dgabino 0:c76361bd82e8 26 #if DEVICE_I2C
dgabino 0:c76361bd82e8 27
dgabino 0:c76361bd82e8 28 /**
dgabino 0:c76361bd82e8 29 * @defgroup hal_I2CEvents I2C Events Macros
dgabino 0:c76361bd82e8 30 *
dgabino 0:c76361bd82e8 31 * @{
dgabino 0:c76361bd82e8 32 */
dgabino 0:c76361bd82e8 33 #define I2C_EVENT_ERROR (1 << 1)
dgabino 0:c76361bd82e8 34 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
dgabino 0:c76361bd82e8 35 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
dgabino 0:c76361bd82e8 36 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
dgabino 0:c76361bd82e8 37 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
dgabino 0:c76361bd82e8 38
dgabino 0:c76361bd82e8 39 /**@}*/
dgabino 0:c76361bd82e8 40
dgabino 0:c76361bd82e8 41 #if DEVICE_I2C_ASYNCH
dgabino 0:c76361bd82e8 42 /** Asynch I2C HAL structure
dgabino 0:c76361bd82e8 43 */
dgabino 0:c76361bd82e8 44 typedef struct {
dgabino 0:c76361bd82e8 45 struct i2c_s i2c; /**< Target specific I2C structure */
dgabino 0:c76361bd82e8 46 struct buffer_s tx_buff; /**< Tx buffer */
dgabino 0:c76361bd82e8 47 struct buffer_s rx_buff; /**< Rx buffer */
dgabino 0:c76361bd82e8 48 } i2c_t;
dgabino 0:c76361bd82e8 49
dgabino 0:c76361bd82e8 50 #else
dgabino 0:c76361bd82e8 51 /** Non-asynch I2C HAL structure
dgabino 0:c76361bd82e8 52 */
dgabino 0:c76361bd82e8 53 typedef struct i2c_s i2c_t;
dgabino 0:c76361bd82e8 54
dgabino 0:c76361bd82e8 55 #endif
dgabino 0:c76361bd82e8 56
dgabino 0:c76361bd82e8 57 enum {
dgabino 0:c76361bd82e8 58 I2C_ERROR_NO_SLAVE = -1,
dgabino 0:c76361bd82e8 59 I2C_ERROR_BUS_BUSY = -2
dgabino 0:c76361bd82e8 60 };
dgabino 0:c76361bd82e8 61
dgabino 0:c76361bd82e8 62 #ifdef __cplusplus
dgabino 0:c76361bd82e8 63 extern "C" {
dgabino 0:c76361bd82e8 64 #endif
dgabino 0:c76361bd82e8 65
dgabino 0:c76361bd82e8 66 /**
dgabino 0:c76361bd82e8 67 * \defgroup hal_GeneralI2C I2C Configuration Functions
dgabino 0:c76361bd82e8 68 * @{
dgabino 0:c76361bd82e8 69 */
dgabino 0:c76361bd82e8 70
dgabino 0:c76361bd82e8 71 /** Initialize the I2C peripheral. It sets the default parameters for I2C
dgabino 0:c76361bd82e8 72 * peripheral, and configures its specifieds pins.
dgabino 0:c76361bd82e8 73 *
dgabino 0:c76361bd82e8 74 * @param obj The I2C object
dgabino 0:c76361bd82e8 75 * @param sda The sda pin
dgabino 0:c76361bd82e8 76 * @param scl The scl pin
dgabino 0:c76361bd82e8 77 */
dgabino 0:c76361bd82e8 78 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
dgabino 0:c76361bd82e8 79
dgabino 0:c76361bd82e8 80 /** Configure the I2C frequency
dgabino 0:c76361bd82e8 81 *
dgabino 0:c76361bd82e8 82 * @param obj The I2C object
dgabino 0:c76361bd82e8 83 * @param hz Frequency in Hz
dgabino 0:c76361bd82e8 84 */
dgabino 0:c76361bd82e8 85 void i2c_frequency(i2c_t *obj, int hz);
dgabino 0:c76361bd82e8 86
dgabino 0:c76361bd82e8 87 /** Send START command
dgabino 0:c76361bd82e8 88 *
dgabino 0:c76361bd82e8 89 * @param obj The I2C object
dgabino 0:c76361bd82e8 90 */
dgabino 0:c76361bd82e8 91 int i2c_start(i2c_t *obj);
dgabino 0:c76361bd82e8 92
dgabino 0:c76361bd82e8 93 /** Send STOP command
dgabino 0:c76361bd82e8 94 *
dgabino 0:c76361bd82e8 95 * @param obj The I2C object
dgabino 0:c76361bd82e8 96 */
dgabino 0:c76361bd82e8 97 int i2c_stop(i2c_t *obj);
dgabino 0:c76361bd82e8 98
dgabino 0:c76361bd82e8 99 /** Blocking reading data
dgabino 0:c76361bd82e8 100 *
dgabino 0:c76361bd82e8 101 * @param obj The I2C object
dgabino 0:c76361bd82e8 102 * @param address 7-bit address (last bit is 1)
dgabino 0:c76361bd82e8 103 * @param data The buffer for receiving
dgabino 0:c76361bd82e8 104 * @param length Number of bytes to read
dgabino 0:c76361bd82e8 105 * @param stop Stop to be generated after the transfer is done
dgabino 0:c76361bd82e8 106 * @return Number of read bytes
dgabino 0:c76361bd82e8 107 */
dgabino 0:c76361bd82e8 108 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
dgabino 0:c76361bd82e8 109
dgabino 0:c76361bd82e8 110 /** Blocking sending data
dgabino 0:c76361bd82e8 111 *
dgabino 0:c76361bd82e8 112 * @param obj The I2C object
dgabino 0:c76361bd82e8 113 * @param address 7-bit address (last bit is 0)
dgabino 0:c76361bd82e8 114 * @param data The buffer for sending
dgabino 0:c76361bd82e8 115 * @param length Number of bytes to write
dgabino 0:c76361bd82e8 116 * @param stop Stop to be generated after the transfer is done
dgabino 0:c76361bd82e8 117 * @return Number of written bytes
dgabino 0:c76361bd82e8 118 */
dgabino 0:c76361bd82e8 119 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
dgabino 0:c76361bd82e8 120
dgabino 0:c76361bd82e8 121 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
dgabino 0:c76361bd82e8 122 *
dgabino 0:c76361bd82e8 123 * @param obj The I2C object
dgabino 0:c76361bd82e8 124 */
dgabino 0:c76361bd82e8 125 void i2c_reset(i2c_t *obj);
dgabino 0:c76361bd82e8 126
dgabino 0:c76361bd82e8 127 /** Read one byte
dgabino 0:c76361bd82e8 128 *
dgabino 0:c76361bd82e8 129 * @param obj The I2C object
dgabino 0:c76361bd82e8 130 * @param last Acknoledge
dgabino 0:c76361bd82e8 131 * @return The read byte
dgabino 0:c76361bd82e8 132 */
dgabino 0:c76361bd82e8 133 int i2c_byte_read(i2c_t *obj, int last);
dgabino 0:c76361bd82e8 134
dgabino 0:c76361bd82e8 135 /** Write one byte
dgabino 0:c76361bd82e8 136 *
dgabino 0:c76361bd82e8 137 * @param obj The I2C object
dgabino 0:c76361bd82e8 138 * @param data Byte to be written
dgabino 0:c76361bd82e8 139 * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
dgabino 0:c76361bd82e8 140 */
dgabino 0:c76361bd82e8 141 int i2c_byte_write(i2c_t *obj, int data);
dgabino 0:c76361bd82e8 142
dgabino 0:c76361bd82e8 143 /**@}*/
dgabino 0:c76361bd82e8 144
dgabino 0:c76361bd82e8 145 #if DEVICE_I2CSLAVE
dgabino 0:c76361bd82e8 146
dgabino 0:c76361bd82e8 147 /**
dgabino 0:c76361bd82e8 148 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
dgabino 0:c76361bd82e8 149 * @{
dgabino 0:c76361bd82e8 150 */
dgabino 0:c76361bd82e8 151
dgabino 0:c76361bd82e8 152 /** Configure I2C as slave or master.
dgabino 0:c76361bd82e8 153 * @param obj The I2C object
dgabino 0:c76361bd82e8 154 * @return non-zero if a value is available
dgabino 0:c76361bd82e8 155 */
dgabino 0:c76361bd82e8 156 void i2c_slave_mode(i2c_t *obj, int enable_slave);
dgabino 0:c76361bd82e8 157
dgabino 0:c76361bd82e8 158 /** Check to see if the I2C slave has been addressed.
dgabino 0:c76361bd82e8 159 * @param obj The I2C object
dgabino 0:c76361bd82e8 160 * @return The status - 1 - read addresses, 2 - write to all slaves,
dgabino 0:c76361bd82e8 161 * 3 write addressed, 0 - the slave has not been addressed
dgabino 0:c76361bd82e8 162 */
dgabino 0:c76361bd82e8 163 int i2c_slave_receive(i2c_t *obj);
dgabino 0:c76361bd82e8 164
dgabino 0:c76361bd82e8 165 /** Configure I2C as slave or master.
dgabino 0:c76361bd82e8 166 * @param obj The I2C object
dgabino 0:c76361bd82e8 167 * @return non-zero if a value is available
dgabino 0:c76361bd82e8 168 */
dgabino 0:c76361bd82e8 169 int i2c_slave_read(i2c_t *obj, char *data, int length);
dgabino 0:c76361bd82e8 170
dgabino 0:c76361bd82e8 171 /** Configure I2C as slave or master.
dgabino 0:c76361bd82e8 172 * @param obj The I2C object
dgabino 0:c76361bd82e8 173 * @return non-zero if a value is available
dgabino 0:c76361bd82e8 174 */
dgabino 0:c76361bd82e8 175 int i2c_slave_write(i2c_t *obj, const char *data, int length);
dgabino 0:c76361bd82e8 176
dgabino 0:c76361bd82e8 177 /** Configure I2C address.
dgabino 0:c76361bd82e8 178 * @param obj The I2C object
dgabino 0:c76361bd82e8 179 * @param idx Currently not used
dgabino 0:c76361bd82e8 180 * @param address The address to be set
dgabino 0:c76361bd82e8 181 * @param mask Currently not used
dgabino 0:c76361bd82e8 182 */
dgabino 0:c76361bd82e8 183 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
dgabino 0:c76361bd82e8 184
dgabino 0:c76361bd82e8 185 #endif
dgabino 0:c76361bd82e8 186
dgabino 0:c76361bd82e8 187 /**@}*/
dgabino 0:c76361bd82e8 188
dgabino 0:c76361bd82e8 189 #if DEVICE_I2C_ASYNCH
dgabino 0:c76361bd82e8 190
dgabino 0:c76361bd82e8 191 /**
dgabino 0:c76361bd82e8 192 * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
dgabino 0:c76361bd82e8 193 * @{
dgabino 0:c76361bd82e8 194 */
dgabino 0:c76361bd82e8 195
dgabino 0:c76361bd82e8 196 /** Start I2C asynchronous transfer
dgabino 0:c76361bd82e8 197 *
dgabino 0:c76361bd82e8 198 * @param obj The I2C object
dgabino 0:c76361bd82e8 199 * @param tx The transmit buffer
dgabino 0:c76361bd82e8 200 * @param tx_length The number of bytes to transmit
dgabino 0:c76361bd82e8 201 * @param rx The receive buffer
dgabino 0:c76361bd82e8 202 * @param rx_length The number of bytes to receive
dgabino 0:c76361bd82e8 203 * @param address The address to be set - 7bit or 9bit
dgabino 0:c76361bd82e8 204 * @param stop If true, stop will be generated after the transfer is done
dgabino 0:c76361bd82e8 205 * @param handler The I2C IRQ handler to be set
dgabino 0:c76361bd82e8 206 * @param hint DMA hint usage
dgabino 0:c76361bd82e8 207 */
dgabino 0:c76361bd82e8 208 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
dgabino 0:c76361bd82e8 209
dgabino 0:c76361bd82e8 210 /** The asynchronous IRQ handler
dgabino 0:c76361bd82e8 211 *
dgabino 0:c76361bd82e8 212 * @param obj The I2C object which holds the transfer information
dgabino 0:c76361bd82e8 213 * @return Event flags if a transfer termination condition was met, otherwise return 0.
dgabino 0:c76361bd82e8 214 */
dgabino 0:c76361bd82e8 215 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
dgabino 0:c76361bd82e8 216
dgabino 0:c76361bd82e8 217 /** Attempts to determine if the I2C peripheral is already in use
dgabino 0:c76361bd82e8 218 *
dgabino 0:c76361bd82e8 219 * @param obj The I2C object
dgabino 0:c76361bd82e8 220 * @return Non-zero if the I2C module is active or zero if it is not
dgabino 0:c76361bd82e8 221 */
dgabino 0:c76361bd82e8 222 uint8_t i2c_active(i2c_t *obj);
dgabino 0:c76361bd82e8 223
dgabino 0:c76361bd82e8 224 /** Abort asynchronous transfer
dgabino 0:c76361bd82e8 225 *
dgabino 0:c76361bd82e8 226 * This function does not perform any check - that should happen in upper layers.
dgabino 0:c76361bd82e8 227 * @param obj The I2C object
dgabino 0:c76361bd82e8 228 */
dgabino 0:c76361bd82e8 229 void i2c_abort_asynch(i2c_t *obj);
dgabino 0:c76361bd82e8 230
dgabino 0:c76361bd82e8 231 #endif
dgabino 0:c76361bd82e8 232
dgabino 0:c76361bd82e8 233 /**@}*/
dgabino 0:c76361bd82e8 234
dgabino 0:c76361bd82e8 235 #ifdef __cplusplus
dgabino 0:c76361bd82e8 236 }
dgabino 0:c76361bd82e8 237 #endif
dgabino 0:c76361bd82e8 238
dgabino 0:c76361bd82e8 239 #endif
dgabino 0:c76361bd82e8 240
dgabino 0:c76361bd82e8 241 #endif