mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 09 15:30:08 2015 +0100
Revision:
563:9f26fcd0c9ce
Parent:
558:0880f51c4036
Synchronized with git revision a140fc60a6f74003a3d46c4ce8bf8c742148b9fd

Full URL: https://github.com/mbedmicro/mbed/commit/a140fc60a6f74003a3d46c4ce8bf8c742148b9fd/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 558:0880f51c4036 1 /* mbed Microcontroller Library
mbed_official 558:0880f51c4036 2 *******************************************************************************
mbed_official 558:0880f51c4036 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
mbed_official 558:0880f51c4036 4 * All rights reserved.
mbed_official 558:0880f51c4036 5 *
mbed_official 558:0880f51c4036 6 * Redistribution and use in source and binary forms, with or without
mbed_official 558:0880f51c4036 7 * modification, are permitted provided that the following conditions are met:
mbed_official 558:0880f51c4036 8 *
mbed_official 558:0880f51c4036 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 558:0880f51c4036 10 * this list of conditions and the following disclaimer.
mbed_official 558:0880f51c4036 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 558:0880f51c4036 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 558:0880f51c4036 13 * and/or other materials provided with the distribution.
mbed_official 558:0880f51c4036 14 * 3. Neither the name of ARM Limited nor the names of its contributors
mbed_official 558:0880f51c4036 15 * may be used to endorse or promote products derived from this software
mbed_official 558:0880f51c4036 16 * without specific prior written permission.
mbed_official 558:0880f51c4036 17 *
mbed_official 558:0880f51c4036 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 558:0880f51c4036 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 558:0880f51c4036 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 558:0880f51c4036 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 558:0880f51c4036 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 558:0880f51c4036 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 558:0880f51c4036 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 558:0880f51c4036 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 558:0880f51c4036 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 558:0880f51c4036 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 558:0880f51c4036 28 *******************************************************************************
mbed_official 558:0880f51c4036 29 */
mbed_official 558:0880f51c4036 30
mbed_official 558:0880f51c4036 31 #include "mbed_assert.h"
mbed_official 558:0880f51c4036 32 #include "i2c_api.h"
mbed_official 558:0880f51c4036 33
mbed_official 558:0880f51c4036 34 #if DEVICE_I2C
mbed_official 558:0880f51c4036 35
mbed_official 558:0880f51c4036 36 #include "cmsis.h"
mbed_official 558:0880f51c4036 37 #include "pinmap.h"
mbed_official 558:0880f51c4036 38 #include "PeripheralPins.h"
mbed_official 558:0880f51c4036 39
mbed_official 558:0880f51c4036 40 /* Timeout values for flags and events waiting loops. These timeouts are
mbed_official 558:0880f51c4036 41 not based on accurate values, they just guarantee that the application will
mbed_official 558:0880f51c4036 42 not remain stuck if the I2C communication is corrupted. */
mbed_official 558:0880f51c4036 43 #define FLAG_TIMEOUT ((int)0x1000)
mbed_official 558:0880f51c4036 44 #define LONG_TIMEOUT ((int)0xFFFF)
mbed_official 558:0880f51c4036 45
mbed_official 558:0880f51c4036 46 I2C_TypeDef * I2cHandle;
mbed_official 558:0880f51c4036 47
mbed_official 558:0880f51c4036 48 int i2c0_inited = 0;
mbed_official 558:0880f51c4036 49 int i2c1_inited = 0;
mbed_official 558:0880f51c4036 50
mbed_official 558:0880f51c4036 51 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
mbed_official 558:0880f51c4036 52 {
mbed_official 558:0880f51c4036 53 // Determine the I2C to use
mbed_official 558:0880f51c4036 54 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 558:0880f51c4036 55 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 558:0880f51c4036 56
mbed_official 558:0880f51c4036 57 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 558:0880f51c4036 58 MBED_ASSERT(obj->i2c != (I2CName)NC);
mbed_official 558:0880f51c4036 59
mbed_official 558:0880f51c4036 60 // Enable I2C1 clock and pinout if not done
mbed_official 558:0880f51c4036 61 if ((obj->i2c == I2C_0) && !i2c0_inited) {
mbed_official 558:0880f51c4036 62 i2c0_inited = 1;
mbed_official 558:0880f51c4036 63 // Configure I2C pins
mbed_official 558:0880f51c4036 64 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 558:0880f51c4036 65 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 558:0880f51c4036 66 pin_mode(sda, OpenDrain);
mbed_official 558:0880f51c4036 67 pin_mode(scl, OpenDrain);
mbed_official 558:0880f51c4036 68 }
mbed_official 558:0880f51c4036 69
mbed_official 558:0880f51c4036 70 // Enable I2C2 clock and pinout if not done
mbed_official 558:0880f51c4036 71 if ((obj->i2c == I2C_1) && !i2c1_inited) {
mbed_official 558:0880f51c4036 72 i2c1_inited = 1;
mbed_official 558:0880f51c4036 73 // Configure I2C pins
mbed_official 558:0880f51c4036 74 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 558:0880f51c4036 75 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 558:0880f51c4036 76 pin_mode(sda, OpenDrain);
mbed_official 558:0880f51c4036 77 pin_mode(scl, OpenDrain);
mbed_official 558:0880f51c4036 78 }
mbed_official 558:0880f51c4036 79
mbed_official 558:0880f51c4036 80 // Reset to clear pending flags if any
mbed_official 558:0880f51c4036 81 i2c_reset(obj);
mbed_official 558:0880f51c4036 82
mbed_official 558:0880f51c4036 83 // I2C configuration
mbed_official 558:0880f51c4036 84 i2c_frequency(obj, 100000); // 100 kHz per default
mbed_official 558:0880f51c4036 85 obj->is_setAddress = 0;
mbed_official 558:0880f51c4036 86 }
mbed_official 558:0880f51c4036 87
mbed_official 558:0880f51c4036 88 void i2c_frequency(i2c_t *obj, int hz)
mbed_official 558:0880f51c4036 89 {
mbed_official 558:0880f51c4036 90 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
mbed_official 558:0880f51c4036 91 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 92
mbed_official 558:0880f51c4036 93 // wait before init
mbed_official 558:0880f51c4036 94 I2C_ConfigStruct conf;
mbed_official 558:0880f51c4036 95
mbed_official 558:0880f51c4036 96 conf.mode = I2C_Master;
mbed_official 558:0880f51c4036 97 conf.master.timeout = LONG_TIMEOUT;
mbed_official 558:0880f51c4036 98
mbed_official 558:0880f51c4036 99 // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
mbed_official 558:0880f51c4036 100 switch (hz) {
mbed_official 558:0880f51c4036 101 case 100000:
mbed_official 558:0880f51c4036 102 conf.master.prescale = 0x61; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
mbed_official 558:0880f51c4036 103 break;
mbed_official 558:0880f51c4036 104 case 400000:
mbed_official 558:0880f51c4036 105 break;
mbed_official 558:0880f51c4036 106 case 1000000:
mbed_official 558:0880f51c4036 107 break;
mbed_official 558:0880f51c4036 108 default:
mbed_official 558:0880f51c4036 109 break;
mbed_official 558:0880f51c4036 110 }
mbed_official 558:0880f51c4036 111
mbed_official 558:0880f51c4036 112 // I2C configuration
mbed_official 558:0880f51c4036 113 I2C_Init(I2cHandle, conf);
mbed_official 558:0880f51c4036 114 }
mbed_official 558:0880f51c4036 115
mbed_official 558:0880f51c4036 116 inline int i2c_start(i2c_t *obj)
mbed_official 558:0880f51c4036 117 {
mbed_official 558:0880f51c4036 118 obj->is_setAddress = 0;
mbed_official 558:0880f51c4036 119
mbed_official 558:0880f51c4036 120 return 0;
mbed_official 558:0880f51c4036 121 }
mbed_official 558:0880f51c4036 122
mbed_official 558:0880f51c4036 123 inline int i2c_stop(i2c_t *obj)
mbed_official 558:0880f51c4036 124 {
mbed_official 558:0880f51c4036 125 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 126
mbed_official 558:0880f51c4036 127 // Generate the STOP condition
mbed_official 558:0880f51c4036 128 I2C_Stop(I2cHandle);
mbed_official 558:0880f51c4036 129 I2C_Reset(I2cHandle);
mbed_official 558:0880f51c4036 130 obj->is_setAddress = 0;
mbed_official 558:0880f51c4036 131
mbed_official 558:0880f51c4036 132 return 0;
mbed_official 558:0880f51c4036 133 }
mbed_official 558:0880f51c4036 134
mbed_official 558:0880f51c4036 135 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
mbed_official 558:0880f51c4036 136 {
mbed_official 558:0880f51c4036 137 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 138 int count;
mbed_official 558:0880f51c4036 139 int value;
mbed_official 558:0880f51c4036 140
mbed_official 558:0880f51c4036 141 if(!obj->is_setAddress)
mbed_official 558:0880f51c4036 142 {
mbed_official 558:0880f51c4036 143 if( I2C_Start(I2cHandle, address, I2C_READ_SA7) == ERROR )
mbed_official 558:0880f51c4036 144 {
mbed_official 558:0880f51c4036 145 return -1;
mbed_official 558:0880f51c4036 146 }
mbed_official 558:0880f51c4036 147 obj->is_setAddress = 1;
mbed_official 558:0880f51c4036 148 obj->ADDRESS = address;
mbed_official 558:0880f51c4036 149 }
mbed_official 558:0880f51c4036 150 else
mbed_official 558:0880f51c4036 151 {
mbed_official 558:0880f51c4036 152 I2C_Restart_Structure(I2cHandle, address, I2C_READ_SA7);
mbed_official 558:0880f51c4036 153 obj->ADDRESS = address;
mbed_official 558:0880f51c4036 154 }
mbed_official 558:0880f51c4036 155
mbed_official 558:0880f51c4036 156 // Read all bytes
mbed_official 558:0880f51c4036 157 for (count = 0; count < (length-1); count++) {
mbed_official 558:0880f51c4036 158 if( (value = i2c_byte_read(obj, 0)) == -1) return value;
mbed_official 558:0880f51c4036 159 data[count] = (char)value;
mbed_official 558:0880f51c4036 160 }
mbed_official 558:0880f51c4036 161
mbed_official 558:0880f51c4036 162 if(stop){
mbed_official 558:0880f51c4036 163 if( (value = i2c_byte_read(obj, 1)) == -1) return value;
mbed_official 558:0880f51c4036 164 data[count] = (char)value;
mbed_official 558:0880f51c4036 165 }
mbed_official 558:0880f51c4036 166 else{
mbed_official 558:0880f51c4036 167 if( (value = i2c_byte_read(obj, 0)) == -1) return value;
mbed_official 558:0880f51c4036 168 data[count] = (char)value;
mbed_official 558:0880f51c4036 169 }
mbed_official 558:0880f51c4036 170
mbed_official 558:0880f51c4036 171 return count;
mbed_official 558:0880f51c4036 172 }
mbed_official 558:0880f51c4036 173
mbed_official 558:0880f51c4036 174 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
mbed_official 558:0880f51c4036 175 {
mbed_official 558:0880f51c4036 176 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 177 int count;
mbed_official 558:0880f51c4036 178
mbed_official 558:0880f51c4036 179 if(!obj->is_setAddress)
mbed_official 558:0880f51c4036 180 {
mbed_official 558:0880f51c4036 181 if( I2C_Start(I2cHandle, address, I2C_WRITE_SA7) == ERROR )
mbed_official 558:0880f51c4036 182 {
mbed_official 558:0880f51c4036 183 return -1;
mbed_official 558:0880f51c4036 184 }
mbed_official 558:0880f51c4036 185 obj->is_setAddress = 1;
mbed_official 558:0880f51c4036 186 obj->ADDRESS = address;
mbed_official 558:0880f51c4036 187 }
mbed_official 558:0880f51c4036 188 else
mbed_official 558:0880f51c4036 189 {
mbed_official 558:0880f51c4036 190 I2C_Restart_Structure(I2cHandle, address, I2C_WRITE_SA7);
mbed_official 558:0880f51c4036 191 obj->ADDRESS = address;
mbed_official 558:0880f51c4036 192 }
mbed_official 558:0880f51c4036 193
mbed_official 558:0880f51c4036 194 for (count = 0; count < length; count++) {
mbed_official 558:0880f51c4036 195 i2c_byte_write(obj, data[count]);
mbed_official 558:0880f51c4036 196 }
mbed_official 558:0880f51c4036 197
mbed_official 558:0880f51c4036 198 // If not repeated start, send stop
mbed_official 558:0880f51c4036 199 if (stop) {
mbed_official 558:0880f51c4036 200 i2c_stop(obj);
mbed_official 558:0880f51c4036 201 }
mbed_official 558:0880f51c4036 202
mbed_official 558:0880f51c4036 203 return count;
mbed_official 558:0880f51c4036 204 }
mbed_official 558:0880f51c4036 205
mbed_official 558:0880f51c4036 206 int i2c_byte_read(i2c_t *obj, int last)
mbed_official 558:0880f51c4036 207 {
mbed_official 558:0880f51c4036 208 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 209
mbed_official 558:0880f51c4036 210 return I2C_ReceiveData(I2cHandle, last);
mbed_official 558:0880f51c4036 211 }
mbed_official 558:0880f51c4036 212
mbed_official 558:0880f51c4036 213 int i2c_byte_write(i2c_t *obj, int data)
mbed_official 558:0880f51c4036 214 {
mbed_official 558:0880f51c4036 215 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 216 return I2C_SendDataAck(I2cHandle,(uint8_t)data);
mbed_official 558:0880f51c4036 217 }
mbed_official 558:0880f51c4036 218
mbed_official 558:0880f51c4036 219 void i2c_reset(i2c_t *obj)
mbed_official 558:0880f51c4036 220 {
mbed_official 558:0880f51c4036 221 I2cHandle = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 222
mbed_official 558:0880f51c4036 223 I2C_Reset(I2cHandle);
mbed_official 558:0880f51c4036 224 }
mbed_official 558:0880f51c4036 225
mbed_official 558:0880f51c4036 226 //#if DEVICE_I2CSLAVE
mbed_official 558:0880f51c4036 227 //
mbed_official 558:0880f51c4036 228 //void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
mbed_official 558:0880f51c4036 229 //{
mbed_official 558:0880f51c4036 230 // I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 231 // uint16_t tmpreg = 0;
mbed_official 558:0880f51c4036 232 //
mbed_official 558:0880f51c4036 233 // // disable
mbed_official 558:0880f51c4036 234 // i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
mbed_official 558:0880f51c4036 235 // // Get the old register value
mbed_official 558:0880f51c4036 236 // tmpreg = i2c->OAR1;
mbed_official 558:0880f51c4036 237 // // Reset address bits
mbed_official 558:0880f51c4036 238 // tmpreg &= 0xFC00;
mbed_official 558:0880f51c4036 239 // // Set new address
mbed_official 558:0880f51c4036 240 // tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
mbed_official 558:0880f51c4036 241 // // Store the new register value
mbed_official 558:0880f51c4036 242 // i2c->OAR1 = tmpreg;
mbed_official 558:0880f51c4036 243 // // enable
mbed_official 558:0880f51c4036 244 // i2c->OAR1 |= I2C_OAR1_OA1EN;
mbed_official 558:0880f51c4036 245 //}
mbed_official 558:0880f51c4036 246 //
mbed_official 558:0880f51c4036 247 //void i2c_slave_mode(i2c_t *obj, int enable_slave)
mbed_official 558:0880f51c4036 248 //{
mbed_official 558:0880f51c4036 249 // I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 250 // uint16_t tmpreg;
mbed_official 558:0880f51c4036 251 //
mbed_official 558:0880f51c4036 252 // // Get the old register value
mbed_official 558:0880f51c4036 253 // tmpreg = i2c->OAR1;
mbed_official 558:0880f51c4036 254 //
mbed_official 558:0880f51c4036 255 // // Enable / disable slave
mbed_official 558:0880f51c4036 256 // if (enable_slave == 1) {
mbed_official 558:0880f51c4036 257 // tmpreg |= I2C_OAR1_OA1EN;
mbed_official 558:0880f51c4036 258 // } else {
mbed_official 558:0880f51c4036 259 // tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
mbed_official 558:0880f51c4036 260 // }
mbed_official 558:0880f51c4036 261 //
mbed_official 558:0880f51c4036 262 // // Set new mode
mbed_official 558:0880f51c4036 263 // i2c->OAR1 = tmpreg;
mbed_official 558:0880f51c4036 264 //}
mbed_official 558:0880f51c4036 265 //
mbed_official 558:0880f51c4036 266 //// See I2CSlave.h
mbed_official 558:0880f51c4036 267 //#define NoData 0 // the slave has not been addressed
mbed_official 558:0880f51c4036 268 //#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
mbed_official 558:0880f51c4036 269 //#define WriteGeneral 2 // the master is writing to all slave
mbed_official 558:0880f51c4036 270 //#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
mbed_official 558:0880f51c4036 271 //
mbed_official 558:0880f51c4036 272 //int i2c_slave_receive(i2c_t *obj)
mbed_official 558:0880f51c4036 273 //{
mbed_official 558:0880f51c4036 274 // I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 275 // int retValue = NoData;
mbed_official 558:0880f51c4036 276 //
mbed_official 558:0880f51c4036 277 // if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
mbed_official 558:0880f51c4036 278 // if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
mbed_official 558:0880f51c4036 279 // if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
mbed_official 558:0880f51c4036 280 // retValue = ReadAddressed;
mbed_official 558:0880f51c4036 281 // else
mbed_official 558:0880f51c4036 282 // retValue = WriteAddressed;
mbed_official 558:0880f51c4036 283 //
mbed_official 558:0880f51c4036 284 // __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
mbed_official 558:0880f51c4036 285 // }
mbed_official 558:0880f51c4036 286 // }
mbed_official 558:0880f51c4036 287 //
mbed_official 558:0880f51c4036 288 // return (retValue);
mbed_official 558:0880f51c4036 289 //}
mbed_official 558:0880f51c4036 290 //
mbed_official 558:0880f51c4036 291 //int i2c_slave_read(i2c_t *obj, char *data, int length)
mbed_official 558:0880f51c4036 292 //{
mbed_official 558:0880f51c4036 293 // char size = 0;
mbed_official 558:0880f51c4036 294 //
mbed_official 558:0880f51c4036 295 // while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
mbed_official 558:0880f51c4036 296 //
mbed_official 558:0880f51c4036 297 // return size;
mbed_official 558:0880f51c4036 298 //}
mbed_official 558:0880f51c4036 299 //
mbed_official 558:0880f51c4036 300 //int i2c_slave_write(i2c_t *obj, const char *data, int length)
mbed_official 558:0880f51c4036 301 //{
mbed_official 558:0880f51c4036 302 // char size = 0;
mbed_official 558:0880f51c4036 303 // I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 558:0880f51c4036 304 //
mbed_official 558:0880f51c4036 305 // do {
mbed_official 558:0880f51c4036 306 // i2c_byte_write(obj, data[size]);
mbed_official 558:0880f51c4036 307 // size++;
mbed_official 558:0880f51c4036 308 // } while (size < length);
mbed_official 558:0880f51c4036 309 //
mbed_official 558:0880f51c4036 310 // return size;
mbed_official 558:0880f51c4036 311 //}
mbed_official 558:0880f51c4036 312 //
mbed_official 558:0880f51c4036 313 //
mbed_official 558:0880f51c4036 314 //#endif // DEVICE_I2CSLAVE
mbed_official 558:0880f51c4036 315
mbed_official 558:0880f51c4036 316 #endif // DEVICE_I2C