![](/media/cache/profiles/eb1feb5ab28dec83652dd1e5c673f256.jpg.50x50_q85.jpg)
LTC2945 ported
LT_I2C.cpp@3:2ab78e7e8518, 2017-08-25 (annotated)
- Committer:
- odtulumbedder
- Date:
- Fri Aug 25 14:42:14 2017 +0000
- Revision:
- 3:2ab78e7e8518
- Parent:
- 2:7e390bcce297
- Child:
- 4:d32eaca53504
I added a few comments, and cleaned up the code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
odtulumbedder | 0:6be57f391716 | 1 | #include "LT_I2C.h" |
odtulumbedder | 0:6be57f391716 | 2 | |
odtulumbedder | 0:6be57f391716 | 3 | /* mbed headers */ |
odtulumbedder | 0:6be57f391716 | 4 | #include <mbed.h> |
odtulumbedder | 0:6be57f391716 | 5 | #include <I2C.h> |
odtulumbedder | 0:6be57f391716 | 6 | |
odtulumbedder | 2:7e390bcce297 | 7 | I2C *i2c_object = NULL; |
odtulumbedder | 1:6d4265aa4184 | 8 | |
odtulumbedder | 0:6be57f391716 | 9 | extern "C" { |
odtulumbedder | 0:6be57f391716 | 10 | |
odtulumbedder | 0:6be57f391716 | 11 | int lt_i2c_init(int sda, int scl) { |
odtulumbedder | 0:6be57f391716 | 12 | if (i2c_object == NULL) { |
odtulumbedder | 0:6be57f391716 | 13 | i2c_object = new I2C((PinName) sda, (PinName) scl); |
odtulumbedder | 0:6be57f391716 | 14 | return LT_I2C_INIT_FINE; |
odtulumbedder | 0:6be57f391716 | 15 | } |
odtulumbedder | 0:6be57f391716 | 16 | else { |
odtulumbedder | 0:6be57f391716 | 17 | return LT_I2C_INIT_QUIT_FIRST; |
odtulumbedder | 0:6be57f391716 | 18 | } |
odtulumbedder | 0:6be57f391716 | 19 | } |
odtulumbedder | 0:6be57f391716 | 20 | |
odtulumbedder | 0:6be57f391716 | 21 | int lt_i2c_quit() { |
odtulumbedder | 0:6be57f391716 | 22 | delete i2c_object; |
odtulumbedder | 0:6be57f391716 | 23 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 24 | } |
odtulumbedder | 0:6be57f391716 | 25 | |
odtulumbedder | 0:6be57f391716 | 26 | int lt_i2c_init_attach(void *p_i2c_object) { |
odtulumbedder | 0:6be57f391716 | 27 | if (i2c_object == NULL) { |
odtulumbedder | 0:6be57f391716 | 28 | i2c_object = (I2C *) p_i2c_object; |
odtulumbedder | 0:6be57f391716 | 29 | return LT_I2C_INIT_FINE; |
odtulumbedder | 0:6be57f391716 | 30 | } |
odtulumbedder | 0:6be57f391716 | 31 | else { |
odtulumbedder | 0:6be57f391716 | 32 | return LT_I2C_INIT_QUIT_FIRST; |
odtulumbedder | 0:6be57f391716 | 33 | } |
odtulumbedder | 0:6be57f391716 | 34 | } |
odtulumbedder | 0:6be57f391716 | 35 | |
odtulumbedder | 0:6be57f391716 | 36 | void *lt_i2c_get_i2c_object() { |
odtulumbedder | 0:6be57f391716 | 37 | return i2c_object; |
odtulumbedder | 0:6be57f391716 | 38 | } |
odtulumbedder | 0:6be57f391716 | 39 | |
odtulumbedder | 0:6be57f391716 | 40 | int lt_i2c_start() { |
odtulumbedder | 0:6be57f391716 | 41 | i2c_object->start(); |
odtulumbedder | 0:6be57f391716 | 42 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 43 | } |
odtulumbedder | 0:6be57f391716 | 44 | |
odtulumbedder | 0:6be57f391716 | 45 | int lt_i2c_stop() { |
odtulumbedder | 0:6be57f391716 | 46 | i2c_object->stop(); |
odtulumbedder | 0:6be57f391716 | 47 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 48 | } |
odtulumbedder | 0:6be57f391716 | 49 | |
odtulumbedder | 0:6be57f391716 | 50 | uint8_t lt_i2c_write(uint8_t data) { |
odtulumbedder | 2:7e390bcce297 | 51 | return 1-i2c_object->write(data); |
odtulumbedder | 0:6be57f391716 | 52 | } |
odtulumbedder | 0:6be57f391716 | 53 | |
odtulumbedder | 0:6be57f391716 | 54 | uint8_t lt_i2c_read(int8_t ack) { |
odtulumbedder | 0:6be57f391716 | 55 | return (uint8_t) (i2c_object->read(1-ack)); // act is different in mbed |
odtulumbedder | 0:6be57f391716 | 56 | } |
odtulumbedder | 0:6be57f391716 | 57 | |
odtulumbedder | 0:6be57f391716 | 58 | int8_t lt_i2c_write_byte_data( |
odtulumbedder | 0:6be57f391716 | 59 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 60 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 61 | uint8_t value) { |
odtulumbedder | 0:6be57f391716 | 62 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 63 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 64 | { |
odtulumbedder | 0:6be57f391716 | 65 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 66 | } |
odtulumbedder | 0:6be57f391716 | 67 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 68 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 69 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 70 | ret|= lt_i2c_write(value); |
odtulumbedder | 0:6be57f391716 | 71 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 72 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 73 | { |
odtulumbedder | 0:6be57f391716 | 74 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 75 | } |
odtulumbedder | 0:6be57f391716 | 76 | return(0); // Return success |
odtulumbedder | 0:6be57f391716 | 77 | } |
odtulumbedder | 0:6be57f391716 | 78 | |
odtulumbedder | 0:6be57f391716 | 79 | int8_t lt_i2c_write_word_data( |
odtulumbedder | 0:6be57f391716 | 80 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 81 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 82 | uint16_t value) { |
odtulumbedder | 0:6be57f391716 | 83 | int8_t ret=0; |
odtulumbedder | 0:6be57f391716 | 84 | union |
odtulumbedder | 0:6be57f391716 | 85 | { |
odtulumbedder | 0:6be57f391716 | 86 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 87 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 88 | } data; |
odtulumbedder | 0:6be57f391716 | 89 | data.w = value; |
odtulumbedder | 0:6be57f391716 | 90 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 91 | { |
odtulumbedder | 0:6be57f391716 | 92 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 93 | } |
odtulumbedder | 0:6be57f391716 | 94 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 95 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 96 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 97 | ret|= lt_i2c_write(data.b[1]); //Write MSB |
odtulumbedder | 0:6be57f391716 | 98 | ret|= lt_i2c_write(data.b[0]); //Write LSB; |
odtulumbedder | 0:6be57f391716 | 99 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 100 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 101 | { |
odtulumbedder | 2:7e390bcce297 | 102 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 103 | } |
odtulumbedder | 0:6be57f391716 | 104 | return(0); |
odtulumbedder | 0:6be57f391716 | 105 | } |
odtulumbedder | 0:6be57f391716 | 106 | |
odtulumbedder | 0:6be57f391716 | 107 | uint8_t lt_i2c_write_block_data( |
odtulumbedder | 0:6be57f391716 | 108 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 109 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 110 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 111 | uint8_t *values) { |
odtulumbedder | 0:6be57f391716 | 112 | int8_t i = length-1; |
odtulumbedder | 0:6be57f391716 | 113 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 114 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 0:6be57f391716 | 115 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 116 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 117 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 118 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 119 | while (i>=0) |
odtulumbedder | 0:6be57f391716 | 120 | { |
odtulumbedder | 0:6be57f391716 | 121 | ret|= lt_i2c_write(values[i]); //Write Value |
odtulumbedder | 0:6be57f391716 | 122 | i--; |
odtulumbedder | 0:6be57f391716 | 123 | } |
odtulumbedder | 0:6be57f391716 | 124 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 125 | if (ret!=0) |
odtulumbedder | 1:6d4265aa4184 | 126 | { |
odtulumbedder | 0:6be57f391716 | 127 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 128 | } |
odtulumbedder | 0:6be57f391716 | 129 | else |
odtulumbedder | 1:6d4265aa4184 | 130 | { |
odtulumbedder | 0:6be57f391716 | 131 | return(0); // Success! |
odtulumbedder | 1:6d4265aa4184 | 132 | } |
odtulumbedder | 0:6be57f391716 | 133 | } |
odtulumbedder | 0:6be57f391716 | 134 | |
odtulumbedder | 0:6be57f391716 | 135 | uint8_t lt_i2c_read_byte_data( |
odtulumbedder | 0:6be57f391716 | 136 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 137 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 138 | uint8_t *value) { |
odtulumbedder | 0:6be57f391716 | 139 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 140 | if (lt_i2c_start() != 0) // I2C START |
odtulumbedder | 0:6be57f391716 | 141 | { |
odtulumbedder | 0:6be57f391716 | 142 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 143 | } |
odtulumbedder | 0:6be57f391716 | 144 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 145 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 146 | { |
odtulumbedder | 0:6be57f391716 | 147 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 148 | return (1); |
odtulumbedder | 0:6be57f391716 | 149 | } |
odtulumbedder | 0:6be57f391716 | 150 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 151 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 152 | { |
odtulumbedder | 0:6be57f391716 | 153 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 154 | return (1); |
odtulumbedder | 0:6be57f391716 | 155 | } |
odtulumbedder | 0:6be57f391716 | 156 | if (lt_i2c_start() != 0) // I2C repeated START |
odtulumbedder | 0:6be57f391716 | 157 | { |
odtulumbedder | 0:6be57f391716 | 158 | lt_i2c_stop(); // Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 159 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 160 | } |
odtulumbedder | 0:6be57f391716 | 161 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 162 | *value = lt_i2c_read(LT_I2C_WITH_NACK); // Read byte from buffer with NAK |
odtulumbedder | 0:6be57f391716 | 163 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 164 | if (ret != 0) // If there was a NACK return 1 |
odtulumbedder | 0:6be57f391716 | 165 | { |
odtulumbedder | 0:6be57f391716 | 166 | return (1); |
odtulumbedder | 0:6be57f391716 | 167 | } |
odtulumbedder | 0:6be57f391716 | 168 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 169 | } |
odtulumbedder | 0:6be57f391716 | 170 | |
odtulumbedder | 0:6be57f391716 | 171 | uint8_t lt_i2c_read_word_data( |
odtulumbedder | 0:6be57f391716 | 172 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 173 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 174 | uint16_t *value) { |
odtulumbedder | 0:6be57f391716 | 175 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 176 | union { |
odtulumbedder | 0:6be57f391716 | 177 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 178 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 179 | } data; |
odtulumbedder | 0:6be57f391716 | 180 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 181 | { |
odtulumbedder | 0:6be57f391716 | 182 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 183 | } |
odtulumbedder | 0:6be57f391716 | 184 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 185 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 186 | { |
odtulumbedder | 0:6be57f391716 | 187 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 188 | return (1); |
odtulumbedder | 0:6be57f391716 | 189 | } |
odtulumbedder | 0:6be57f391716 | 190 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 191 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 192 | { |
odtulumbedder | 0:6be57f391716 | 193 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 194 | return (1); |
odtulumbedder | 0:6be57f391716 | 195 | } |
odtulumbedder | 0:6be57f391716 | 196 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 0:6be57f391716 | 197 | { |
odtulumbedder | 0:6be57f391716 | 198 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 199 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 200 | } |
odtulumbedder | 0:6be57f391716 | 201 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 202 | data.b[1] = lt_i2c_read(LT_I2C_WITH_ACK); // Read MSB from buffer |
odtulumbedder | 0:6be57f391716 | 203 | data.b[0] = lt_i2c_read(LT_I2C_WITH_NACK); // Read LSB from buffer |
odtulumbedder | 0:6be57f391716 | 204 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 205 | *value = data.w; |
odtulumbedder | 0:6be57f391716 | 206 | if (ret != 0) //If NAK |
odtulumbedder | 1:6d4265aa4184 | 207 | { |
odtulumbedder | 0:6be57f391716 | 208 | return (1); //return 1 |
odtulumbedder | 1:6d4265aa4184 | 209 | } |
odtulumbedder | 0:6be57f391716 | 210 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 211 | } |
odtulumbedder | 0:6be57f391716 | 212 | |
odtulumbedder | 0:6be57f391716 | 213 | uint8_t lt_i2c_read_block_data( |
odtulumbedder | 0:6be57f391716 | 214 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 215 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 216 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 217 | uint8_t *values) { |
odtulumbedder | 0:6be57f391716 | 218 | uint8_t i = (length - 1); |
odtulumbedder | 0:6be57f391716 | 219 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 220 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 221 | { |
odtulumbedder | 0:6be57f391716 | 222 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 223 | } |
odtulumbedder | 0:6be57f391716 | 224 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); //Write 7-bit address with W bit |
odtulumbedder | 0:6be57f391716 | 225 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 226 | { |
odtulumbedder | 0:6be57f391716 | 227 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 228 | return (1); |
odtulumbedder | 0:6be57f391716 | 229 | } |
odtulumbedder | 0:6be57f391716 | 230 | ret |= lt_i2c_write(command); //Write 8 bit command word |
odtulumbedder | 0:6be57f391716 | 231 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 232 | { |
odtulumbedder | 0:6be57f391716 | 233 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 234 | return (1); |
odtulumbedder | 0:6be57f391716 | 235 | } |
odtulumbedder | 0:6be57f391716 | 236 | if (lt_i2c_start() != 0) //I2C repeated START |
odtulumbedder | 0:6be57f391716 | 237 | { |
odtulumbedder | 0:6be57f391716 | 238 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 239 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 240 | } |
odtulumbedder | 0:6be57f391716 | 241 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); //Write 7-bit address with R bit |
odtulumbedder | 0:6be57f391716 | 242 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 243 | { |
odtulumbedder | 0:6be57f391716 | 244 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 245 | return (1); |
odtulumbedder | 0:6be57f391716 | 246 | } |
odtulumbedder | 0:6be57f391716 | 247 | while (i > 0) //Begin read loop |
odtulumbedder | 0:6be57f391716 | 248 | { |
odtulumbedder | 0:6be57f391716 | 249 | values[i] = lt_i2c_read(LT_I2C_WITH_ACK); //Read from bus with ACK |
odtulumbedder | 0:6be57f391716 | 250 | i--; |
odtulumbedder | 0:6be57f391716 | 251 | } |
odtulumbedder | 0:6be57f391716 | 252 | values[0] = lt_i2c_read(LT_I2C_WITH_NACK); //Read from bus with NACK for the last one; |
odtulumbedder | 0:6be57f391716 | 253 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 254 | return (0); // Success! |
odtulumbedder | 0:6be57f391716 | 255 | } |
odtulumbedder | 0:6be57f391716 | 256 | |
odtulumbedder | 0:6be57f391716 | 257 | } |