Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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