4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
iftaziz
Date:
Wed Aug 23 10:32:38 2017 +0000
Revision:
166:33361e55dd8c
Parent:
160:d5399cc887bb
r1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 *******************************************************************************
<> 144:ef7eb2e8f9f7 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
<> 144:ef7eb2e8f9f7 4 * All rights reserved.
<> 144:ef7eb2e8f9f7 5 *
<> 144:ef7eb2e8f9f7 6 * Redistribution and use in source and binary forms, with or without
<> 144:ef7eb2e8f9f7 7 * modification, are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 8 *
<> 144:ef7eb2e8f9f7 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 144:ef7eb2e8f9f7 10 * this list of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 144:ef7eb2e8f9f7 12 * this list of conditions and the following disclaimer in the documentation
<> 144:ef7eb2e8f9f7 13 * and/or other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 14 * 3. Neither the name of ARM Limited nor the names of its contributors
<> 144:ef7eb2e8f9f7 15 * may be used to endorse or promote products derived from this software
<> 144:ef7eb2e8f9f7 16 * without specific prior written permission.
<> 144:ef7eb2e8f9f7 17 *
<> 144:ef7eb2e8f9f7 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 144:ef7eb2e8f9f7 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 144:ef7eb2e8f9f7 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 144:ef7eb2e8f9f7 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 144:ef7eb2e8f9f7 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 144:ef7eb2e8f9f7 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 144:ef7eb2e8f9f7 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 144:ef7eb2e8f9f7 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 144:ef7eb2e8f9f7 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 28 *******************************************************************************
<> 144:ef7eb2e8f9f7 29 */
<> 144:ef7eb2e8f9f7 30
<> 144:ef7eb2e8f9f7 31 #include "mbed_assert.h"
<> 144:ef7eb2e8f9f7 32 #include "i2c_api.h"
<> 144:ef7eb2e8f9f7 33
<> 144:ef7eb2e8f9f7 34
<> 144:ef7eb2e8f9f7 35 #if DEVICE_I2C
<> 144:ef7eb2e8f9f7 36
<> 144:ef7eb2e8f9f7 37 #include "cmsis.h"
<> 144:ef7eb2e8f9f7 38 #include "pinmap.h"
<> 144:ef7eb2e8f9f7 39 #include "PeripheralPins.h"
<> 144:ef7eb2e8f9f7 40
<> 160:d5399cc887bb 41 #include "mbed_wait_api.h"
<> 144:ef7eb2e8f9f7 42 #include "us_ticker_api.h"
<> 144:ef7eb2e8f9f7 43 #include "W7500x_i2c.h"
<> 144:ef7eb2e8f9f7 44
<> 144:ef7eb2e8f9f7 45 /* Timeout values for flags and events waiting loops. These timeouts are
<> 144:ef7eb2e8f9f7 46 not based on accurate values, they just guarantee that the application will
<> 144:ef7eb2e8f9f7 47 not remain stuck if the I2C communication is corrupted. */
<> 144:ef7eb2e8f9f7 48 #define FLAG_TIMEOUT ((int)0x1000)
<> 144:ef7eb2e8f9f7 49 #define LONG_TIMEOUT ((int)0xFFFF)
<> 144:ef7eb2e8f9f7 50
<> 144:ef7eb2e8f9f7 51
<> 144:ef7eb2e8f9f7 52 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
<> 144:ef7eb2e8f9f7 53 {
<> 144:ef7eb2e8f9f7 54 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 55
<> 144:ef7eb2e8f9f7 56 //Determine the I2C to use
<> 144:ef7eb2e8f9f7 57 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
<> 144:ef7eb2e8f9f7 58 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
<> 144:ef7eb2e8f9f7 59 obj->I2Cx = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
<> 144:ef7eb2e8f9f7 60 MBED_ASSERT(obj->I2Cx != (I2CName)NC);
<> 144:ef7eb2e8f9f7 61
<> 144:ef7eb2e8f9f7 62 obj->sda = sda;
<> 144:ef7eb2e8f9f7 63 obj->scl = scl;
<> 144:ef7eb2e8f9f7 64 obj->ADDRESS = 0x0;
<> 144:ef7eb2e8f9f7 65 obj->is_setAddress = 0;
<> 144:ef7eb2e8f9f7 66
<> 144:ef7eb2e8f9f7 67 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 68 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 69
<> 144:ef7eb2e8f9f7 70 I2C_Init(&conf);
<> 144:ef7eb2e8f9f7 71 }
<> 144:ef7eb2e8f9f7 72
<> 144:ef7eb2e8f9f7 73 void i2c_frequency(i2c_t *obj, int hz)
<> 144:ef7eb2e8f9f7 74 {
<> 144:ef7eb2e8f9f7 75
<> 144:ef7eb2e8f9f7 76 }
<> 144:ef7eb2e8f9f7 77
<> 144:ef7eb2e8f9f7 78 inline int i2c_start(i2c_t *obj)
<> 144:ef7eb2e8f9f7 79 {
<> 144:ef7eb2e8f9f7 80 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 81 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 82 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 83
<> 144:ef7eb2e8f9f7 84 I2C_Start(&conf);
<> 144:ef7eb2e8f9f7 85
<> 144:ef7eb2e8f9f7 86 return 0;
<> 144:ef7eb2e8f9f7 87 }
<> 144:ef7eb2e8f9f7 88
<> 144:ef7eb2e8f9f7 89 inline int i2c_stop(i2c_t *obj)
<> 144:ef7eb2e8f9f7 90 {
<> 144:ef7eb2e8f9f7 91 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 92 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 93 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 94
<> 144:ef7eb2e8f9f7 95 I2C_Stop(&conf);
<> 144:ef7eb2e8f9f7 96
<> 144:ef7eb2e8f9f7 97 return 0;
<> 144:ef7eb2e8f9f7 98 }
<> 144:ef7eb2e8f9f7 99
<> 144:ef7eb2e8f9f7 100 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 101 {
<> 144:ef7eb2e8f9f7 102 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 103
<> 144:ef7eb2e8f9f7 104 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 105 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 106
<> 144:ef7eb2e8f9f7 107
<> 144:ef7eb2e8f9f7 108 if(stop)
<> 144:ef7eb2e8f9f7 109 {
<> 144:ef7eb2e8f9f7 110 if(I2C_Read(&conf, address, (uint8_t*)data, length) != 0)
<> 144:ef7eb2e8f9f7 111 return -1;
<> 144:ef7eb2e8f9f7 112 }
<> 144:ef7eb2e8f9f7 113 else
<> 144:ef7eb2e8f9f7 114 {
<> 144:ef7eb2e8f9f7 115 if(I2C_ReadRepeated(&conf, address, (uint8_t*)data, length) != 0)
<> 144:ef7eb2e8f9f7 116 return -1;
<> 144:ef7eb2e8f9f7 117 }
<> 144:ef7eb2e8f9f7 118
<> 144:ef7eb2e8f9f7 119 return length;
<> 144:ef7eb2e8f9f7 120
<> 144:ef7eb2e8f9f7 121 }
<> 144:ef7eb2e8f9f7 122
<> 144:ef7eb2e8f9f7 123 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 124 {
<> 144:ef7eb2e8f9f7 125 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 126
<> 144:ef7eb2e8f9f7 127 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 128 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 129
<> 144:ef7eb2e8f9f7 130
<> 144:ef7eb2e8f9f7 131 if(stop)
<> 144:ef7eb2e8f9f7 132 {
<> 144:ef7eb2e8f9f7 133 if(I2C_Write(&conf, address, (uint8_t*)data, length) != 0)
<> 144:ef7eb2e8f9f7 134 return -1;
<> 144:ef7eb2e8f9f7 135 }
<> 144:ef7eb2e8f9f7 136 else
<> 144:ef7eb2e8f9f7 137 {
<> 144:ef7eb2e8f9f7 138 if(I2C_WriteRepeated(&conf, address, (uint8_t*)data, length) != 0)
<> 144:ef7eb2e8f9f7 139 return -1;
<> 144:ef7eb2e8f9f7 140 }
<> 144:ef7eb2e8f9f7 141
<> 144:ef7eb2e8f9f7 142 return length;
<> 144:ef7eb2e8f9f7 143 }
<> 144:ef7eb2e8f9f7 144
<> 144:ef7eb2e8f9f7 145 int i2c_byte_read(i2c_t *obj, int last)
<> 144:ef7eb2e8f9f7 146 {
<> 144:ef7eb2e8f9f7 147 uint8_t ret;
<> 144:ef7eb2e8f9f7 148 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 149
<> 144:ef7eb2e8f9f7 150 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 151 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 152
<> 144:ef7eb2e8f9f7 153 ret = I2C_ReadByte(&conf);
<> 144:ef7eb2e8f9f7 154
<> 144:ef7eb2e8f9f7 155 if(last)
<> 144:ef7eb2e8f9f7 156 I2C_SendNACK(&conf);
<> 144:ef7eb2e8f9f7 157 else
<> 144:ef7eb2e8f9f7 158 I2C_SendACK(&conf);
<> 144:ef7eb2e8f9f7 159
<> 144:ef7eb2e8f9f7 160 return (int)ret;
<> 144:ef7eb2e8f9f7 161 }
<> 144:ef7eb2e8f9f7 162
<> 144:ef7eb2e8f9f7 163 int i2c_byte_write(i2c_t *obj, int data)
<> 144:ef7eb2e8f9f7 164 {
<> 144:ef7eb2e8f9f7 165 I2C_ConfigStruct conf;
<> 144:ef7eb2e8f9f7 166
<> 144:ef7eb2e8f9f7 167 conf.sda = (I2C_PinName)obj->sda;
<> 144:ef7eb2e8f9f7 168 conf.scl = (I2C_PinName)obj->scl;
<> 144:ef7eb2e8f9f7 169
<> 144:ef7eb2e8f9f7 170 if(I2C_WriteByte(&conf, (uint8_t)data)) // NACK
<> 144:ef7eb2e8f9f7 171 return 0;
<> 144:ef7eb2e8f9f7 172 else //ack
<> 144:ef7eb2e8f9f7 173 return 1;
<> 144:ef7eb2e8f9f7 174 }
<> 144:ef7eb2e8f9f7 175
<> 144:ef7eb2e8f9f7 176 void i2c_reset(i2c_t *obj)
<> 144:ef7eb2e8f9f7 177 {
<> 144:ef7eb2e8f9f7 178
<> 144:ef7eb2e8f9f7 179 }
<> 144:ef7eb2e8f9f7 180
<> 144:ef7eb2e8f9f7 181
<> 144:ef7eb2e8f9f7 182 #endif // DEVICE_I2C