forked

Committer:
Kojto
Date:
Thu Aug 03 13:13:39 2017 +0100
Revision:
170:19eb464bc2be
Parent:
160:d5399cc887bb
This updates the lib to the mbed lib v 148

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /**
<> 149:156823d33999 2 *******************************************************************************
<> 149:156823d33999 3 * @file i2c.c
<> 149:156823d33999 4 * @brief Implementation of an i2c api
<> 149:156823d33999 5 * @internal
<> 149:156823d33999 6 * @author ON Semiconductor
<> 149:156823d33999 7 * $Rev: 3525 $
<> 149:156823d33999 8 * $Date: 2015-07-20 15:24:25 +0530 (Mon, 20 Jul 2015) $
<> 149:156823d33999 9 ******************************************************************************
<> 149:156823d33999 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
<> 149:156823d33999 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
<> 149:156823d33999 12 * under limited terms and conditions. The terms and conditions pertaining to the software
<> 149:156823d33999 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
<> 149:156823d33999 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
<> 149:156823d33999 15 * if applicable the software license agreement. Do not use this software and/or
<> 149:156823d33999 16 * documentation unless you have carefully read and you agree to the limited terms and
<> 149:156823d33999 17 * conditions. By using this software and/or documentation, you agree to the limited
<> 149:156823d33999 18 * terms and conditions.
<> 149:156823d33999 19 *
<> 149:156823d33999 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
<> 149:156823d33999 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
<> 149:156823d33999 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
<> 149:156823d33999 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
<> 149:156823d33999 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
<> 149:156823d33999 25 * @endinternal
<> 149:156823d33999 26 *
<> 149:156823d33999 27 * @ingroup i2c
<> 149:156823d33999 28 *
<> 149:156823d33999 29 */
<> 149:156823d33999 30 #if DEVICE_I2C
<> 149:156823d33999 31
<> 149:156823d33999 32 #include "i2c.h"
<> 149:156823d33999 33 #include "i2c_api.h"
<> 160:d5399cc887bb 34 #include "mbed_wait_api.h"
<> 149:156823d33999 35
<> 149:156823d33999 36 #define I2C_READ_WRITE_BIT_MASK 0xFE
<> 149:156823d33999 37
<> 149:156823d33999 38 /* See i2c_api.h for details */
<> 149:156823d33999 39 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
<> 149:156823d33999 40 {
<> 149:156823d33999 41 fI2cInit(obj, sda, scl);
<> 149:156823d33999 42 }
<> 149:156823d33999 43
<> 149:156823d33999 44 /* See i2c_api.h for details */
<> 149:156823d33999 45 void i2c_frequency(i2c_t *obj, int hz)
<> 149:156823d33999 46 {
<> 149:156823d33999 47 fI2cFrequency(obj, hz);
<> 149:156823d33999 48 }
<> 149:156823d33999 49
<> 149:156823d33999 50 /* See i2c_api.h for details */
<> 149:156823d33999 51 int i2c_start(i2c_t *obj)
<> 149:156823d33999 52 {
<> 149:156823d33999 53 return(fI2cStart(obj));
<> 149:156823d33999 54 }
<> 149:156823d33999 55
<> 149:156823d33999 56 /* See i2c_api.h for details */
<> 149:156823d33999 57 int i2c_stop(i2c_t *obj)
<> 149:156823d33999 58 {
<> 149:156823d33999 59 return(fI2cStop(obj));
<> 149:156823d33999 60 }
<> 149:156823d33999 61
<> 149:156823d33999 62 /* See i2c_api.h for details */
<> 149:156823d33999 63 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 149:156823d33999 64 {
<> 149:156823d33999 65 /* TODO address parameter not usable */
<> 149:156823d33999 66 int Count, status;
<> 149:156823d33999 67 const char WriteData = (address | (~I2C_READ_WRITE_BIT_MASK)) & 0xFF;
<> 149:156823d33999 68
<> 149:156823d33999 69 /* Send start bit */
<> 149:156823d33999 70 status = fI2cStart(obj);
<> 149:156823d33999 71 if(status) {
<> 149:156823d33999 72 /* Error sending start bit */
<> 149:156823d33999 73 return status;
<> 149:156823d33999 74 }
<> 149:156823d33999 75
<> 149:156823d33999 76 /* Send address | read */
<> 149:156823d33999 77 Count = fI2cWriteB(obj, &WriteData, 1);
<> 149:156823d33999 78 if(Count != 1) {
<> 149:156823d33999 79 /* Error sending address */
<> 149:156823d33999 80 return Count;
<> 149:156823d33999 81 }
<> 149:156823d33999 82
<> 149:156823d33999 83 /* Send command/s */
<> 149:156823d33999 84 Count = fI2cReadB(obj, data, length);
<> 149:156823d33999 85 if(Count != length) {
<> 149:156823d33999 86 /* Error sending coomand/s */
<> 149:156823d33999 87 return Count;
<> 149:156823d33999 88 }
<> 149:156823d33999 89 if(stop) { /* Send stop bit if requested */
<> 149:156823d33999 90 status = fI2cStop(obj);
<> 149:156823d33999 91 if(status) {
<> 149:156823d33999 92 /* Error sending stop bit */
<> 149:156823d33999 93 return status;
<> 149:156823d33999 94 }
<> 149:156823d33999 95 }
<> 149:156823d33999 96 return Count;
<> 149:156823d33999 97 }
<> 149:156823d33999 98
<> 149:156823d33999 99 /* See i2c_api.h for details */
<> 149:156823d33999 100 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 149:156823d33999 101 {
<> 149:156823d33999 102 int Count, status;
<> 149:156823d33999 103 const char WriteData = (address & I2C_READ_WRITE_BIT_MASK) & 0xFF;
<> 149:156823d33999 104
<> 149:156823d33999 105 /* Send start bit */
<> 149:156823d33999 106 status = fI2cStart(obj);
<> 149:156823d33999 107 if(status) {
<> 149:156823d33999 108 /* Error sending start bit */
<> 149:156823d33999 109 return status;
<> 149:156823d33999 110 }
<> 149:156823d33999 111
<> 149:156823d33999 112 /* Send address | write */
<> 149:156823d33999 113 Count = fI2cWriteB(obj, &WriteData, 1);
<> 149:156823d33999 114 if(Count != 1) {
<> 149:156823d33999 115 /* Error sending address */
<> 149:156823d33999 116 return Count;
<> 149:156823d33999 117 }
<> 149:156823d33999 118
<> 149:156823d33999 119 /* Sens command, [data] */
<> 149:156823d33999 120 Count = fI2cWriteB(obj, data, length);
<> 149:156823d33999 121 if(Count != length) {
<> 149:156823d33999 122 /* Error sending address */
<> 149:156823d33999 123 return Count;
<> 149:156823d33999 124 }
<> 149:156823d33999 125
<> 149:156823d33999 126 if(stop) { /* If stop requested */
<> 149:156823d33999 127 /* Send stop bit */
<> 149:156823d33999 128 status = fI2cStop(obj);
<> 149:156823d33999 129 if(status) {
<> 149:156823d33999 130 /* Error sending stop bit */
<> 149:156823d33999 131 return status;
<> 149:156823d33999 132 }
<> 149:156823d33999 133 }
<> 149:156823d33999 134 return Count;
<> 149:156823d33999 135 }
<> 149:156823d33999 136
<> 149:156823d33999 137 /* See i2c_api.h for details */
<> 149:156823d33999 138 void i2c_reset(i2c_t *obj)
<> 149:156823d33999 139 {
<> 149:156823d33999 140 (void)fI2cStop(obj);
<> 149:156823d33999 141 }
<> 149:156823d33999 142
<> 149:156823d33999 143 /* See i2c_api.h for details */
<> 149:156823d33999 144 int i2c_byte_read(i2c_t *obj, int last) /* TODO return size can be uint8_t */
<> 149:156823d33999 145 {
<> 149:156823d33999 146 int Count;
<> 149:156823d33999 147 char data;
<> 149:156823d33999 148 Count = fI2cReadB(obj, &data, 1);
<> 149:156823d33999 149 if(Count != 1) {
<> 149:156823d33999 150 /* Error */
<> 149:156823d33999 151 return Count;
<> 149:156823d33999 152 }
<> 149:156823d33999 153 if(last) {
<> 149:156823d33999 154 /* ACK */
<> 159:612c381a210f 155 SEND_COMMAND(I2C_CMD_WDAT0);
<> 149:156823d33999 156 } else {
<> 149:156823d33999 157 /* No ACK */
<> 159:612c381a210f 158 SEND_COMMAND(I2C_CMD_WDAT1);
<> 149:156823d33999 159 }
<> 149:156823d33999 160 return data;
<> 149:156823d33999 161 }
<> 149:156823d33999 162
<> 149:156823d33999 163 /* See i2c_api.h for details */
<> 149:156823d33999 164 int i2c_byte_write(i2c_t *obj, int data)
<> 149:156823d33999 165 {
<> 149:156823d33999 166 int Count;
<> 149:156823d33999 167 Count = fI2cWriteB(obj, (const char *)&data, 1);
<> 149:156823d33999 168 if(Count != 1) {
<> 149:156823d33999 169 return Count;
<> 149:156823d33999 170 }
<> 149:156823d33999 171
<> 149:156823d33999 172 while(obj->membase->STATUS.WORD & I2C_STATUS_CMD_FIFO_OFL_BIT); /* Wait till command overflow ends */
<> 149:156823d33999 173
<> 149:156823d33999 174 if(obj->membase->STATUS.WORD & I2C_STATUS_BUS_ERR_BIT) {
<> 149:156823d33999 175 /* Bus error means NAK received */
<> 149:156823d33999 176 return 0;
<> 149:156823d33999 177 } else {
<> 149:156823d33999 178 /* ACK received */
<> 149:156823d33999 179 return 1;
<> 149:156823d33999 180 }
<> 149:156823d33999 181 }
<> 149:156823d33999 182
<> 159:612c381a210f 183 #endif /* DEVICE_I2C */