This is the final version of Mini Gateway for Automation and Security desgined for Renesas GR Peach Design Contest

Dependencies:   GR-PEACH_video GraphicsFramework HTTPServer R_BSP mbed-rpc mbed-rtos Socket lwip-eth lwip-sys lwip FATFileSystem

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

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 
00121  *      zero or non-zero - Number of written bytes
00122  *      negative - I2C_ERROR_XXX status
00123  */
00124 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
00125 
00126 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
00127  *
00128  *  @param obj The I2C object
00129  */
00130 void i2c_reset(i2c_t *obj);
00131 
00132 /** Read one byte
00133  *
00134  *  @param obj The I2C object
00135  *  @param last Acknoledge
00136  *  @return The read byte
00137  */
00138 int i2c_byte_read(i2c_t *obj, int last);
00139 
00140 /** Write one byte
00141  *
00142  *  @param obj The I2C object
00143  *  @param data Byte to be written
00144  *  @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
00145  */
00146 int i2c_byte_write(i2c_t *obj, int data);
00147 
00148 /**@}*/
00149 
00150 #if DEVICE_I2CSLAVE
00151 
00152 /**
00153  * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
00154  * @{
00155  */
00156 
00157 /** Configure I2C as slave or master.
00158  *  @param obj The I2C object
00159  *  @return non-zero if a value is available
00160  */
00161 void i2c_slave_mode(i2c_t *obj, int enable_slave);
00162 
00163 /** Check to see if the I2C slave has been addressed.
00164  *  @param obj The I2C object
00165  *  @return The status - 1 - read addresses, 2 - write to all slaves,
00166  *         3 write addressed, 0 - the slave has not been addressed
00167  */
00168 int  i2c_slave_receive(i2c_t *obj);
00169 
00170 /** Configure I2C as slave or master.
00171  *  @param obj The I2C object
00172  *  @return non-zero if a value is available
00173  */
00174 int  i2c_slave_read(i2c_t *obj, char *data, int length);
00175 
00176 /** Configure I2C as slave or master.
00177  *  @param obj The I2C object
00178  *  @return non-zero if a value is available
00179  */
00180 int  i2c_slave_write(i2c_t *obj, const char *data, int length);
00181 
00182 /** Configure I2C address.
00183  *  @param obj     The I2C object
00184  *  @param idx     Currently not used
00185  *  @param address The address to be set
00186  *  @param mask    Currently not used
00187  */
00188 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
00189 
00190 #endif
00191 
00192 /**@}*/
00193 
00194 #if DEVICE_I2C_ASYNCH
00195 
00196 /**
00197  * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
00198  * @{
00199  */
00200 
00201 /** Start I2C asynchronous transfer
00202  *
00203  *  @param obj       The I2C object
00204  *  @param tx        The transmit buffer
00205  *  @param tx_length The number of bytes to transmit
00206  *  @param rx        The receive buffer
00207  *  @param rx_length The number of bytes to receive
00208  *  @param address   The address to be set - 7bit or 9bit
00209  *  @param stop      If true, stop will be generated after the transfer is done
00210  *  @param handler   The I2C IRQ handler to be set
00211  *  @param hint      DMA hint usage
00212  */
00213 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);
00214 
00215 /** The asynchronous IRQ handler
00216  *
00217  *  @param obj The I2C object which holds the transfer information
00218  *  @return Event flags if a transfer termination condition was met, otherwise return 0.
00219  */
00220 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
00221 
00222 /** Attempts to determine if the I2C peripheral is already in use
00223  *
00224  *  @param obj The I2C object
00225  *  @return Non-zero if the I2C module is active or zero if it is not
00226  */
00227 uint8_t i2c_active(i2c_t *obj);
00228 
00229 /** Abort asynchronous transfer
00230  *
00231  *  This function does not perform any check - that should happen in upper layers.
00232  *  @param obj The I2C object
00233  */
00234 void i2c_abort_asynch(i2c_t *obj);
00235 
00236 #endif
00237 
00238 /**@}*/
00239 
00240 #ifdef __cplusplus
00241 }
00242 #endif
00243 
00244 #endif
00245 
00246 #endif
00247 
00248 /** @}*/