![](/media/cache/profiles/eb1feb5ab28dec83652dd1e5c673f256.jpg.50x50_q85.jpg)
LTC2945 ported
LT_I2C.cpp@2:7e390bcce297, 2017-08-25 (annotated)
- Committer:
- odtulumbedder
- Date:
- Fri Aug 25 07:46:48 2017 +0000
- Revision:
- 2:7e390bcce297
- Parent:
- 1:6d4265aa4184
- Child:
- 3:2ab78e7e8518
Now, it's working.; The problems were:; 1- I forgot ack = 0 in the main loop.; 2- Adding other calls between I2C calls causes latency, which interrupts I2C communication.
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 | #define CHECK_OBJECT if (i2c_object == NULL) return LT_I2C_FAILURE; |
odtulumbedder | 0:6be57f391716 | 41 | |
odtulumbedder | 0:6be57f391716 | 42 | int lt_i2c_start() { |
odtulumbedder | 0:6be57f391716 | 43 | i2c_object->start(); |
odtulumbedder | 0:6be57f391716 | 44 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 45 | } |
odtulumbedder | 0:6be57f391716 | 46 | |
odtulumbedder | 0:6be57f391716 | 47 | int lt_i2c_stop() { |
odtulumbedder | 0:6be57f391716 | 48 | i2c_object->stop(); |
odtulumbedder | 0:6be57f391716 | 49 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 50 | } |
odtulumbedder | 0:6be57f391716 | 51 | |
odtulumbedder | 0:6be57f391716 | 52 | uint8_t lt_i2c_write(uint8_t data) { |
odtulumbedder | 2:7e390bcce297 | 53 | return 1-i2c_object->write(data); |
odtulumbedder | 0:6be57f391716 | 54 | } |
odtulumbedder | 0:6be57f391716 | 55 | |
odtulumbedder | 0:6be57f391716 | 56 | uint8_t lt_i2c_read(int8_t ack) { |
odtulumbedder | 0:6be57f391716 | 57 | return (uint8_t) (i2c_object->read(1-ack)); // act is different in mbed |
odtulumbedder | 0:6be57f391716 | 58 | } |
odtulumbedder | 0:6be57f391716 | 59 | |
odtulumbedder | 0:6be57f391716 | 60 | int8_t lt_i2c_write_byte_data( |
odtulumbedder | 0:6be57f391716 | 61 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 62 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 63 | uint8_t value) { |
odtulumbedder | 0:6be57f391716 | 64 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 65 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 66 | { |
odtulumbedder | 0:6be57f391716 | 67 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 68 | } |
odtulumbedder | 0:6be57f391716 | 69 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 70 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 71 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 72 | ret|= lt_i2c_write(value); |
odtulumbedder | 0:6be57f391716 | 73 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 74 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 75 | { |
odtulumbedder | 0:6be57f391716 | 76 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 77 | } |
odtulumbedder | 0:6be57f391716 | 78 | return(0); // Return success |
odtulumbedder | 0:6be57f391716 | 79 | } |
odtulumbedder | 0:6be57f391716 | 80 | |
odtulumbedder | 0:6be57f391716 | 81 | int8_t lt_i2c_write_word_data( |
odtulumbedder | 0:6be57f391716 | 82 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 83 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 84 | uint16_t value) { |
odtulumbedder | 0:6be57f391716 | 85 | int8_t ret=0; |
odtulumbedder | 0:6be57f391716 | 86 | union |
odtulumbedder | 0:6be57f391716 | 87 | { |
odtulumbedder | 0:6be57f391716 | 88 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 89 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 90 | } data; |
odtulumbedder | 0:6be57f391716 | 91 | data.w = value; |
odtulumbedder | 0:6be57f391716 | 92 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 93 | { |
odtulumbedder | 0:6be57f391716 | 94 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 95 | } |
odtulumbedder | 0:6be57f391716 | 96 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 97 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 98 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 99 | ret|= lt_i2c_write(data.b[1]); //Write MSB |
odtulumbedder | 0:6be57f391716 | 100 | ret|= lt_i2c_write(data.b[0]); //Write LSB; |
odtulumbedder | 0:6be57f391716 | 101 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 102 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 103 | { |
odtulumbedder | 2:7e390bcce297 | 104 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 105 | } |
odtulumbedder | 0:6be57f391716 | 106 | return(0); |
odtulumbedder | 0:6be57f391716 | 107 | } |
odtulumbedder | 0:6be57f391716 | 108 | |
odtulumbedder | 0:6be57f391716 | 109 | uint8_t lt_i2c_write_block_data( |
odtulumbedder | 0:6be57f391716 | 110 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 111 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 112 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 113 | uint8_t *values) { |
odtulumbedder | 0:6be57f391716 | 114 | int8_t i = length-1; |
odtulumbedder | 0:6be57f391716 | 115 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 116 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 0:6be57f391716 | 117 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 118 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 119 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 120 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 121 | while (i>=0) |
odtulumbedder | 0:6be57f391716 | 122 | { |
odtulumbedder | 0:6be57f391716 | 123 | ret|= lt_i2c_write(values[i]); //Write Value |
odtulumbedder | 0:6be57f391716 | 124 | i--; |
odtulumbedder | 0:6be57f391716 | 125 | } |
odtulumbedder | 0:6be57f391716 | 126 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 127 | if (ret!=0) |
odtulumbedder | 1:6d4265aa4184 | 128 | { |
odtulumbedder | 0:6be57f391716 | 129 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 130 | } |
odtulumbedder | 0:6be57f391716 | 131 | else |
odtulumbedder | 1:6d4265aa4184 | 132 | { |
odtulumbedder | 0:6be57f391716 | 133 | return(0); // Success! |
odtulumbedder | 1:6d4265aa4184 | 134 | } |
odtulumbedder | 0:6be57f391716 | 135 | } |
odtulumbedder | 0:6be57f391716 | 136 | |
odtulumbedder | 0:6be57f391716 | 137 | uint8_t lt_i2c_read_byte_data( |
odtulumbedder | 0:6be57f391716 | 138 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 139 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 140 | uint8_t *value) { |
odtulumbedder | 0:6be57f391716 | 141 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 142 | if (lt_i2c_start() != 0) // I2C START |
odtulumbedder | 0:6be57f391716 | 143 | { |
odtulumbedder | 0:6be57f391716 | 144 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 145 | } |
odtulumbedder | 0:6be57f391716 | 146 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 147 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 148 | { |
odtulumbedder | 0:6be57f391716 | 149 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 150 | return (1); |
odtulumbedder | 0:6be57f391716 | 151 | } |
odtulumbedder | 0:6be57f391716 | 152 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 153 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 154 | { |
odtulumbedder | 0:6be57f391716 | 155 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 156 | return (1); |
odtulumbedder | 0:6be57f391716 | 157 | } |
odtulumbedder | 0:6be57f391716 | 158 | if (lt_i2c_start() != 0) // I2C repeated START |
odtulumbedder | 0:6be57f391716 | 159 | { |
odtulumbedder | 0:6be57f391716 | 160 | lt_i2c_stop(); // Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 161 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 162 | } |
odtulumbedder | 0:6be57f391716 | 163 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 164 | *value = lt_i2c_read(LT_I2C_WITH_NACK); // Read byte from buffer with NAK |
odtulumbedder | 0:6be57f391716 | 165 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 166 | if (ret != 0) // If there was a NACK return 1 |
odtulumbedder | 0:6be57f391716 | 167 | { |
odtulumbedder | 0:6be57f391716 | 168 | return (1); |
odtulumbedder | 0:6be57f391716 | 169 | } |
odtulumbedder | 0:6be57f391716 | 170 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 171 | } |
odtulumbedder | 0:6be57f391716 | 172 | |
odtulumbedder | 0:6be57f391716 | 173 | uint8_t lt_i2c_read_word_data( |
odtulumbedder | 0:6be57f391716 | 174 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 175 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 176 | uint16_t *value) { |
odtulumbedder | 0:6be57f391716 | 177 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 178 | union { |
odtulumbedder | 0:6be57f391716 | 179 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 180 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 181 | } data; |
odtulumbedder | 0:6be57f391716 | 182 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 183 | { |
odtulumbedder | 0:6be57f391716 | 184 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 185 | } |
odtulumbedder | 0:6be57f391716 | 186 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 187 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 188 | { |
odtulumbedder | 0:6be57f391716 | 189 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 190 | return (1); |
odtulumbedder | 0:6be57f391716 | 191 | } |
odtulumbedder | 0:6be57f391716 | 192 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 193 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 194 | { |
odtulumbedder | 0:6be57f391716 | 195 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 196 | return (1); |
odtulumbedder | 0:6be57f391716 | 197 | } |
odtulumbedder | 0:6be57f391716 | 198 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 0:6be57f391716 | 199 | { |
odtulumbedder | 0:6be57f391716 | 200 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 201 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 202 | } |
odtulumbedder | 0:6be57f391716 | 203 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 204 | data.b[1] = lt_i2c_read(LT_I2C_WITH_ACK); // Read MSB from buffer |
odtulumbedder | 0:6be57f391716 | 205 | data.b[0] = lt_i2c_read(LT_I2C_WITH_NACK); // Read LSB from buffer |
odtulumbedder | 0:6be57f391716 | 206 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 207 | *value = data.w; |
odtulumbedder | 0:6be57f391716 | 208 | if (ret != 0) //If NAK |
odtulumbedder | 1:6d4265aa4184 | 209 | { |
odtulumbedder | 0:6be57f391716 | 210 | return (1); //return 1 |
odtulumbedder | 1:6d4265aa4184 | 211 | } |
odtulumbedder | 0:6be57f391716 | 212 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 213 | } |
odtulumbedder | 0:6be57f391716 | 214 | |
odtulumbedder | 0:6be57f391716 | 215 | uint8_t lt_i2c_read_block_data( |
odtulumbedder | 0:6be57f391716 | 216 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 217 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 218 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 219 | uint8_t *values) { |
odtulumbedder | 0:6be57f391716 | 220 | uint8_t i = (length - 1); |
odtulumbedder | 0:6be57f391716 | 221 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 222 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 223 | { |
odtulumbedder | 0:6be57f391716 | 224 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 225 | } |
odtulumbedder | 0:6be57f391716 | 226 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); //Write 7-bit address with W bit |
odtulumbedder | 0:6be57f391716 | 227 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 228 | { |
odtulumbedder | 0:6be57f391716 | 229 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 230 | return (1); |
odtulumbedder | 0:6be57f391716 | 231 | } |
odtulumbedder | 0:6be57f391716 | 232 | ret |= lt_i2c_write(command); //Write 8 bit command word |
odtulumbedder | 0:6be57f391716 | 233 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 234 | { |
odtulumbedder | 0:6be57f391716 | 235 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 236 | return (1); |
odtulumbedder | 0:6be57f391716 | 237 | } |
odtulumbedder | 0:6be57f391716 | 238 | if (lt_i2c_start() != 0) //I2C repeated START |
odtulumbedder | 0:6be57f391716 | 239 | { |
odtulumbedder | 0:6be57f391716 | 240 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 241 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 242 | } |
odtulumbedder | 0:6be57f391716 | 243 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); //Write 7-bit address with R bit |
odtulumbedder | 0:6be57f391716 | 244 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 245 | { |
odtulumbedder | 0:6be57f391716 | 246 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 247 | return (1); |
odtulumbedder | 0:6be57f391716 | 248 | } |
odtulumbedder | 0:6be57f391716 | 249 | while (i > 0) //Begin read loop |
odtulumbedder | 0:6be57f391716 | 250 | { |
odtulumbedder | 0:6be57f391716 | 251 | values[i] = lt_i2c_read(LT_I2C_WITH_ACK); //Read from bus with ACK |
odtulumbedder | 0:6be57f391716 | 252 | i--; |
odtulumbedder | 0:6be57f391716 | 253 | } |
odtulumbedder | 0:6be57f391716 | 254 | values[0] = lt_i2c_read(LT_I2C_WITH_NACK); //Read from bus with NACK for the last one; |
odtulumbedder | 0:6be57f391716 | 255 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 256 | return (0); // Success! |
odtulumbedder | 0:6be57f391716 | 257 | } |
odtulumbedder | 0:6be57f391716 | 258 | |
odtulumbedder | 0:6be57f391716 | 259 | } |