Code for controlling mbed hardware (LED's, motors), as well as code for the Raspberry Pi to run a Support Vector Machine that identifies objects using the Pi camera

Dependencies:   mbed Motordriver mbed-rtos PololuLedStrip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c_api.h Source File

i2c_api.h

00001 
00002 /** \addtogroup hal */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2006-2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #ifndef MBED_I2C_API_H
00020 #define MBED_I2C_API_H
00021 
00022 #include "device.h"
00023 #include "hal/buffer.h"
00024 
00025 #if DEVICE_I2C_ASYNCH
00026 #include "hal/dma_api.h"
00027 #endif
00028 
00029 #if DEVICE_I2C
00030 
00031 /**
00032  * @defgroup hal_I2CEvents I2C Events Macros
00033  *
00034  * @{
00035  */
00036 #define I2C_EVENT_ERROR               (1 << 1)
00037 #define I2C_EVENT_ERROR_NO_SLAVE      (1 << 2)
00038 #define I2C_EVENT_TRANSFER_COMPLETE   (1 << 3)
00039 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
00040 #define I2C_EVENT_ALL                 (I2C_EVENT_ERROR |  I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
00041 
00042 /**@}*/
00043 
00044 #if DEVICE_I2C_ASYNCH
00045 /** Asynch I2C HAL structure
00046  */
00047 typedef struct {
00048     struct i2c_s    i2c;     /**< Target specific I2C structure */
00049     struct buffer_s tx_buff; /**< Tx buffer */
00050     struct buffer_s rx_buff; /**< Rx buffer */
00051 } i2c_t;
00052 
00053 #else
00054 /** Non-asynch I2C HAL structure
00055  */
00056 typedef struct i2c_s i2c_t;
00057 
00058 #endif
00059 
00060 enum {
00061   I2C_ERROR_NO_SLAVE = -1,
00062   I2C_ERROR_BUS_BUSY = -2
00063 };
00064 
00065 #ifdef __cplusplus
00066 extern "C" {
00067 #endif
00068 
00069 /**
00070  * \defgroup hal_GeneralI2C I2C Configuration Functions
00071  * @{
00072  */
00073 
00074 /** Initialize the I2C peripheral. It sets the default parameters for I2C
00075  *  peripheral, and configures its specifieds pins.
00076  *  
00077  *  @param obj  The I2C object
00078  *  @param sda  The sda pin
00079  *  @param scl  The scl pin
00080  */
00081 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
00082 
00083 /** Configure the I2C frequency
00084  *
00085  *  @param obj The I2C object
00086  *  @param hz  Frequency in Hz
00087  */
00088 void i2c_frequency(i2c_t *obj, int hz);
00089 
00090 /** Send START command
00091  *
00092  *  @param obj The I2C object
00093  */
00094 int  i2c_start(i2c_t *obj);
00095 
00096 /** Send STOP command
00097  *
00098  *  @param obj The I2C object
00099  */
00100 int  i2c_stop(i2c_t *obj);
00101 
00102 /** Blocking reading data
00103  *
00104  *  @param obj     The I2C object
00105  *  @param address 7-bit address (last bit is 1)
00106  *  @param data    The buffer for receiving
00107  *  @param length  Number of bytes to read
00108  *  @param stop    Stop to be generated after the transfer is done
00109  *  @return Number of read bytes
00110  */
00111 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
00112 
00113 /** Blocking sending data
00114  *
00115  *  @param obj     The I2C object
00116  *  @param address 7-bit address (last bit is 0)
00117  *  @param data    The buffer for sending
00118  *  @param length  Number of bytes to write
00119  *  @param stop    Stop to be generated after the transfer is done
00120  *  @return Number of written bytes
00121  */
00122 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
00123 
00124 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
00125  *
00126  *  @param obj The I2C object
00127  */
00128 void i2c_reset(i2c_t *obj);
00129 
00130 /** Read one byte
00131  *
00132  *  @param obj The I2C object
00133  *  @param last Acknoledge
00134  *  @return The read byte
00135  */
00136 int i2c_byte_read(i2c_t *obj, int last);
00137 
00138 /** Write one byte
00139  *
00140  *  @param obj The I2C object
00141  *  @param data Byte to be written
00142  *  @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
00143  */
00144 int i2c_byte_write(i2c_t *obj, int data);
00145 
00146 /**@}*/
00147 
00148 #if DEVICE_I2CSLAVE
00149 
00150 /**
00151  * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
00152  * @{
00153  */
00154 
00155 /** Configure I2C as slave or master.
00156  *  @param obj The I2C object
00157  *  @return non-zero if a value is available
00158  */
00159 void i2c_slave_mode(i2c_t *obj, int enable_slave);
00160 
00161 /** Check to see if the I2C slave has been addressed.
00162  *  @param obj The I2C object
00163  *  @return The status - 1 - read addresses, 2 - write to all slaves,
00164  *         3 write addressed, 0 - the slave has not been addressed
00165  */
00166 int  i2c_slave_receive(i2c_t *obj);
00167 
00168 /** Configure I2C as slave or master.
00169  *  @param obj The I2C object
00170  *  @return non-zero if a value is available
00171  */
00172 int  i2c_slave_read(i2c_t *obj, char *data, int length);
00173 
00174 /** Configure I2C as slave or master.
00175  *  @param obj The I2C object
00176  *  @return non-zero if a value is available
00177  */
00178 int  i2c_slave_write(i2c_t *obj, const char *data, int length);
00179 
00180 /** Configure I2C address.
00181  *  @param obj     The I2C object
00182  *  @param idx     Currently not used
00183  *  @param address The address to be set
00184  *  @param mask    Currently not used
00185  */
00186 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
00187 
00188 #endif
00189 
00190 /**@}*/
00191 
00192 #if DEVICE_I2C_ASYNCH
00193 
00194 /**
00195  * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
00196  * @{
00197  */
00198 
00199 /** Start I2C asynchronous transfer
00200  *
00201  *  @param obj       The I2C object
00202  *  @param tx        The transmit buffer
00203  *  @param tx_length The number of bytes to transmit
00204  *  @param rx        The receive buffer
00205  *  @param rx_length The number of bytes to receive
00206  *  @param address   The address to be set - 7bit or 9bit
00207  *  @param stop      If true, stop will be generated after the transfer is done
00208  *  @param handler   The I2C IRQ handler to be set
00209  *  @param hint      DMA hint usage
00210  */
00211 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);
00212 
00213 /** The asynchronous IRQ handler
00214  *
00215  *  @param obj The I2C object which holds the transfer information
00216  *  @return Event flags if a transfer termination condition was met, otherwise return 0.
00217  */
00218 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
00219 
00220 /** Attempts to determine if the I2C peripheral is already in use
00221  *
00222  *  @param obj The I2C object
00223  *  @return Non-zero if the I2C module is active or zero if it is not
00224  */
00225 uint8_t i2c_active(i2c_t *obj);
00226 
00227 /** Abort asynchronous transfer
00228  *
00229  *  This function does not perform any check - that should happen in upper layers.
00230  *  @param obj The I2C object
00231  */
00232 void i2c_abort_asynch(i2c_t *obj);
00233 
00234 #endif
00235 
00236 /**@}*/
00237 
00238 #ifdef __cplusplus
00239 }
00240 #endif
00241 
00242 #endif
00243 
00244 #endif
00245 
00246 /** @}*/