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