CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Sun Nov 04 18:44:51 2018 -0700
Revision:
0:aa13e1c335cd
create new repo for show2me

Who changed what in which revision?

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