Jon Freeman / Mbed 2 deprecated Alternator2020_06

Dependencies:   mbed BufferedSerial Servo2 PCT2075 I2CEeprom FastPWM

Committer:
JonFreeman
Date:
Mon Jun 08 13:46:52 2020 +0000
Revision:
2:8e7b51353f32
Parent:
1:450090bdb6f4
About to revamp i2c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 0:77803b3ee157 1 #include "mbed.h"
JonFreeman 0:77803b3ee157 2 #include "Alternator.h"
JonFreeman 2:8e7b51353f32 3
JonFreeman 2:8e7b51353f32 4 #define I2CTEST
JonFreeman 2:8e7b51353f32 5
JonFreeman 1:450090bdb6f4 6 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 7 #ifdef I2CTEST
JonFreeman 2:8e7b51353f32 8 extern Serial pc;
JonFreeman 2:8e7b51353f32 9 extern I2C i2c;
JonFreeman 2:8e7b51353f32 10 const int ACK = 1; // but acknowledge is 0, NAK is 1 6/6/2020 think this should be 1
JonFreeman 2:8e7b51353f32 11
JonFreeman 2:8e7b51353f32 12 #else
JonFreeman 2:8e7b51353f32 13
JonFreeman 0:77803b3ee157 14 extern Serial pc;
JonFreeman 0:77803b3ee157 15 DigitalInOut SDA (D4); // Horrible bodge to get i2c working using bit banging.
JonFreeman 0:77803b3ee157 16 DigitalInOut SCL (D5); // DigitalInOut do not work as you might expect. Fine if used only as OpenDrain opuputs though!
JonFreeman 0:77803b3ee157 17 DigitalIn SDA_IN (A4); // That means paralleling up with two other pins as inputs
JonFreeman 0:77803b3ee157 18 DigitalIn SCL_IN (A5); // This works but is a pain. Inbuilt I2C should have worked but never does on small boards with 32 pin cpu.
JonFreeman 1:450090bdb6f4 19 const int ACK = 0; // but acknowledge is 0, NAK is 1
JonFreeman 1:450090bdb6f4 20 #endif
JonFreeman 1:450090bdb6f4 21 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 22 extern BufferedSerial pc;
JonFreeman 1:450090bdb6f4 23 extern I2C i2c;
JonFreeman 1:450090bdb6f4 24 const int ACK = 1; // but acknowledge is 0, NAK is 1
JonFreeman 1:450090bdb6f4 25 #endif
JonFreeman 2:8e7b51353f32 26 #endif
JonFreeman 0:77803b3ee157 27 const int _24LC_rd = 0xa1; // set bit 0 for read, clear bit 0 for write
JonFreeman 0:77803b3ee157 28 const int _24LC_wr = 0xa0; // set bit 0 for read, clear bit 0 for write
JonFreeman 0:77803b3ee157 29
JonFreeman 0:77803b3ee157 30
JonFreeman 0:77803b3ee157 31 /*struct optpar {
JonFreeman 0:77803b3ee157 32 int min, max, def; // min, max, default
JonFreeman 0:77803b3ee157 33 const char * t; // description
JonFreeman 0:77803b3ee157 34 } ;*/
JonFreeman 0:77803b3ee157 35 struct optpar option_list2[] = {
JonFreeman 2:8e7b51353f32 36 {0, 100, 1, "max pwm% @ Eng RPM 0, 0 to 100"},
JonFreeman 2:8e7b51353f32 37 {0, 100, 1, "max pwm% @ Eng RPM 500, 0 to 100"},
JonFreeman 2:8e7b51353f32 38 {0, 100, 1, "max pwm% @ Eng RPM 1000, 0 to 100"},
JonFreeman 2:8e7b51353f32 39 {0, 100, 1, "max pwm% @ Eng RPM 1500, 0 to 100"},
JonFreeman 2:8e7b51353f32 40 {0, 100, 1, "max pwm% @ Eng RPM 2000, 0 to 100"},
JonFreeman 2:8e7b51353f32 41 {0, 100, 10, "max pwm% @ Eng RPM 2500, 0 to 100"},
JonFreeman 2:8e7b51353f32 42 {0, 100, 60, "max pwm% @ Eng RPM 3000, 0 to 100"},
JonFreeman 2:8e7b51353f32 43 {0, 100, 70, "max pwm% @ Eng RPM 3500, 0 to 100"},
JonFreeman 2:8e7b51353f32 44 {0, 100, 60, "max pwm% @ Eng RPM 4000, 0 to 100"},
JonFreeman 2:8e7b51353f32 45 {0, 100, 50, "max pwm% @ Eng RPM 4500, 0 to 100"},
JonFreeman 2:8e7b51353f32 46 {0, 100, 40, "max pwm% @ Eng RPM 5000, 0 to 100"},
JonFreeman 2:8e7b51353f32 47 {0, 100, 33, "max pwm% @ Eng RPM 5500, 0 to 100"},
JonFreeman 2:8e7b51353f32 48 {0, 100, 30, "max pwm% @ Eng RPM 6000, 0 to 100"},
JonFreeman 2:8e7b51353f32 49 {0, 100, 30, "max pwm% @ Eng RPM 6500, 0 to 100"},
JonFreeman 2:8e7b51353f32 50 {0, 100, 40, "max pwm% @ Eng RPM 7000, 0 to 100"},
JonFreeman 2:8e7b51353f32 51 {0, 100, 50, "max pwm% @ Eng RPM 7500, 0 to 100"},
JonFreeman 2:8e7b51353f32 52 {0, 100, 60, "max pwm% @ Eng RPM 8000, 0 to 100"},
JonFreeman 0:77803b3ee157 53 {0, 100, 0, "Future 2"},
JonFreeman 0:77803b3ee157 54 {0, 100, 0, "Future 3"},
JonFreeman 0:77803b3ee157 55 {0, 100, 0, "Future 4"},
JonFreeman 0:77803b3ee157 56 {0, 100, 0, "Future 5"},
JonFreeman 2:8e7b51353f32 57 {0, 100, 0, "Future 6"},
JonFreeman 2:8e7b51353f32 58 {0, 100, 0, "Future 7"},
JonFreeman 2:8e7b51353f32 59 {0, 100, 0, "Future 8"},
JonFreeman 2:8e7b51353f32 60 {0, 100, 0, "Future 9"},
JonFreeman 2:8e7b51353f32 61 {0, 100, 0, "Future 10"},
JonFreeman 2:8e7b51353f32 62 {0, 100, 0, "Future 11"},
JonFreeman 2:8e7b51353f32 63 {0, 100, 0, "Future 12"},
JonFreeman 2:8e7b51353f32 64 {0, 100, 0, "Future 13"},
JonFreeman 0:77803b3ee157 65 } ;
JonFreeman 0:77803b3ee157 66
JonFreeman 0:77803b3ee157 67 const int numof_eeprom_options2 = sizeof(option_list2) / sizeof (struct optpar);
JonFreeman 0:77803b3ee157 68
JonFreeman 0:77803b3ee157 69 bool wr_24LC64 (int start_addr, char * source, int length) ; // think this works
JonFreeman 0:77803b3ee157 70 bool rd_24LC64 (int start_addr, char * source, int length) ; // think this works
JonFreeman 0:77803b3ee157 71
JonFreeman 0:77803b3ee157 72
JonFreeman 0:77803b3ee157 73
JonFreeman 1:450090bdb6f4 74 eeprom_settings user_settings ;
JonFreeman 0:77803b3ee157 75
JonFreeman 0:77803b3ee157 76 eeprom_settings::eeprom_settings () {}
JonFreeman 0:77803b3ee157 77
JonFreeman 0:77803b3ee157 78 bool eeprom_settings::set_defaults () {
JonFreeman 0:77803b3ee157 79 for (int i = 0; i < numof_eeprom_options2; i++)
JonFreeman 0:77803b3ee157 80 settings[i] = option_list2[i].def; // Load defaults and 'Save Settings'
JonFreeman 0:77803b3ee157 81 return save ();
JonFreeman 0:77803b3ee157 82 }
JonFreeman 0:77803b3ee157 83
JonFreeman 0:77803b3ee157 84 char eeprom_settings::rd (uint32_t i) { // Read one setup char value from private buffer 'settings'
JonFreeman 0:77803b3ee157 85 if (i > 31) {
JonFreeman 0:77803b3ee157 86 pc.printf ("ERROR Attempt to read setting %d\r\n", i);
JonFreeman 0:77803b3ee157 87 return 0;
JonFreeman 0:77803b3ee157 88 }
JonFreeman 0:77803b3ee157 89 return settings[i];
JonFreeman 0:77803b3ee157 90 }
JonFreeman 0:77803b3ee157 91
JonFreeman 1:450090bdb6f4 92 bool eeprom_settings::rd (char * c, uint32_t i) { // Read one setup char value from private buffer 'settings'
JonFreeman 1:450090bdb6f4 93 if (i > 31) {
JonFreeman 1:450090bdb6f4 94 pc.printf ("ERROR Attempt to read setting %d\r\n", i);
JonFreeman 1:450090bdb6f4 95 return false;
JonFreeman 1:450090bdb6f4 96 }
JonFreeman 1:450090bdb6f4 97 *c = settings[i];
JonFreeman 1:450090bdb6f4 98 return true;
JonFreeman 1:450090bdb6f4 99 }
JonFreeman 1:450090bdb6f4 100
JonFreeman 1:450090bdb6f4 101 bool eeprom_settings::wr (char c, uint32_t i) { // Write one setup char value to private buffer 'settings'
JonFreeman 0:77803b3ee157 102 if (i > 31)
JonFreeman 0:77803b3ee157 103 return false;
JonFreeman 0:77803b3ee157 104 settings[i] = c;
JonFreeman 0:77803b3ee157 105 return true;
JonFreeman 0:77803b3ee157 106 }
JonFreeman 0:77803b3ee157 107
JonFreeman 2:8e7b51353f32 108 /*double eeprom_settings::get_pwm (int rpm) {
JonFreeman 0:77803b3ee157 109 int p = rpm * lut_size;
JonFreeman 1:450090bdb6f4 110 p /= 8000; // 8000 is upper RPM limit, p now scaled to sizeof lut
JonFreeman 0:77803b3ee157 111 if (p < 0) p = 0; // point to first
JonFreeman 0:77803b3ee157 112 if (p >= lut_size) p = lut_size - 1; // point to last
JonFreeman 0:77803b3ee157 113 // pc.printf ("In get_pwm, rpm = %d, lut entry = %d, pwm = %d\r\n", rpm, p, max_pwm_lut[p]);
JonFreeman 0:77803b3ee157 114 return max_pwm_lut[p];
JonFreeman 2:8e7b51353f32 115 }*/
JonFreeman 0:77803b3ee157 116
JonFreeman 2:8e7b51353f32 117 /*void eeprom_settings::build_lut () {
JonFreeman 0:77803b3ee157 118 int ptr = 0;
JonFreeman 0:77803b3ee157 119 int range, i;
JonFreeman 1:450090bdb6f4 120 double acc = 0.0, incr = 0.0;
JonFreeman 0:77803b3ee157 121 for (i = 0; i < 8; i++) {
JonFreeman 1:450090bdb6f4 122 range = user_settings.rd(i+1) - user_settings.rd(i); // range now change in percent between two 'n'000 RPMs
JonFreeman 0:77803b3ee157 123 incr = (double)range;
JonFreeman 1:450090bdb6f4 124 incr /= 100.0; // percent
JonFreeman 0:77803b3ee157 125 incr /= lut_seg_size;
JonFreeman 0:77803b3ee157 126 for(int j = 0; j < lut_seg_size; j++) {
JonFreeman 1:450090bdb6f4 127 max_pwm_lut[ptr++] = acc;
JonFreeman 0:77803b3ee157 128 acc += incr;
JonFreeman 0:77803b3ee157 129 }
JonFreeman 0:77803b3ee157 130 }
JonFreeman 0:77803b3ee157 131 max_pwm_lut[ptr] = (int)acc;
JonFreeman 0:77803b3ee157 132 pc.printf ("At end of build_lut ptr=%d\r\n", ptr);
JonFreeman 0:77803b3ee157 133 range = 0;
JonFreeman 1:450090bdb6f4 134
JonFreeman 1:450090bdb6f4 135 while (range < ptr) {
JonFreeman 1:450090bdb6f4 136 for (i = 0; i < 8; i++) {
JonFreeman 1:450090bdb6f4 137 pc.printf ("%.3f\t", max_pwm_lut[range++]);
JonFreeman 1:450090bdb6f4 138 }
JonFreeman 1:450090bdb6f4 139 pc.printf ("\r\n");
JonFreeman 1:450090bdb6f4 140 }
JonFreeman 0:77803b3ee157 141 pc.printf ("lut_size = %d\r\n", lut_size);
JonFreeman 1:450090bdb6f4 142
JonFreeman 2:8e7b51353f32 143 }*/
JonFreeman 0:77803b3ee157 144
JonFreeman 0:77803b3ee157 145 bool eeprom_settings::load () { // Get 'settings' buffer from EEPROM
JonFreeman 0:77803b3ee157 146 bool rv ;
JonFreeman 0:77803b3ee157 147 rv = rd_24LC64 (eeprom_page * 32, settings, 32); // Can now build lookup table
JonFreeman 2:8e7b51353f32 148 //Apr2020 build_lut ();
JonFreeman 0:77803b3ee157 149 return rv;
JonFreeman 0:77803b3ee157 150 }
JonFreeman 0:77803b3ee157 151
JonFreeman 0:77803b3ee157 152 bool eeprom_settings::save () { // Write 'settings' buffer to EEPROM
JonFreeman 0:77803b3ee157 153 return wr_24LC64 (eeprom_page * 32, settings, 32);
JonFreeman 0:77803b3ee157 154 }
JonFreeman 0:77803b3ee157 155
JonFreeman 0:77803b3ee157 156
JonFreeman 1:450090bdb6f4 157 //#ifdef TARGET_NUCLEO_L432KC //
JonFreeman 0:77803b3ee157 158
JonFreeman 0:77803b3ee157 159 /**
JonFreeman 0:77803b3ee157 160 * bool i2c_init(void) {
JonFreeman 0:77803b3ee157 161 *
JonFreeman 0:77803b3ee157 162 * Init function. Needs to be called once in the beginning.
JonFreeman 0:77803b3ee157 163 * Returns false if SDA or SCL are low, which probably means
JonFreeman 0:77803b3ee157 164 * a I2C bus lockup or that the lines are not pulled up.
JonFreeman 0:77803b3ee157 165 */
JonFreeman 0:77803b3ee157 166 bool i2c_init(void) {
JonFreeman 1:450090bdb6f4 167 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 168 #ifndef I2CTEST
JonFreeman 0:77803b3ee157 169 SDA.output();
JonFreeman 0:77803b3ee157 170 SCL.output();
JonFreeman 0:77803b3ee157 171 SDA.mode(OpenDrain);
JonFreeman 0:77803b3ee157 172 SCL.mode(OpenDrain); // Device may pull clock lo to indicate to master
JonFreeman 0:77803b3ee157 173 SDA = 0;
JonFreeman 0:77803b3ee157 174 SCL = 0;
JonFreeman 0:77803b3ee157 175 wait_us (1);
JonFreeman 0:77803b3ee157 176 SDA = 1;
JonFreeman 0:77803b3ee157 177 wait_us (1);
JonFreeman 0:77803b3ee157 178 SCL = 1;
JonFreeman 0:77803b3ee157 179 wait_us (1);
JonFreeman 0:77803b3ee157 180 if (SCL_IN == 0 || SDA_IN == 0) return false;
JonFreeman 2:8e7b51353f32 181 #endif
JonFreeman 1:450090bdb6f4 182 #endif
JonFreeman 1:450090bdb6f4 183 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 184 // return i2c.init () ; // class has no member "init"
JonFreeman 2:8e7b51353f32 185 #endif
JonFreeman 1:450090bdb6f4 186 return true;
JonFreeman 0:77803b3ee157 187 }
JonFreeman 0:77803b3ee157 188
JonFreeman 0:77803b3ee157 189 /**
JonFreeman 0:77803b3ee157 190 * During data transfer, the data line must remain
JonFreeman 0:77803b3ee157 191 * stable whenever the clock line is high. Changes in
JonFreeman 0:77803b3ee157 192 * the data line while the clock line is high will be
JonFreeman 0:77803b3ee157 193 * interpreted as a Start or Stop condition
JonFreeman 0:77803b3ee157 194 *
JonFreeman 0:77803b3ee157 195 * A high-to-low transition of the SDA line while the clock
JonFreeman 0:77803b3ee157 196 * (SCL) is high determines a Start condition. All
JonFreeman 0:77803b3ee157 197 * commands must be preceded by a Start condition.
JonFreeman 0:77803b3ee157 198 */
JonFreeman 0:77803b3ee157 199 int i2c_start () { // Should be Both hi, start takes SDA low
JonFreeman 1:450090bdb6f4 200 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 201 #ifndef I2CTEST
JonFreeman 0:77803b3ee157 202 int rv = 0;
JonFreeman 0:77803b3ee157 203 if (SDA_IN == 0 ) {
JonFreeman 0:77803b3ee157 204 rv |= 1; // Fault - SDA was lo on entry
JonFreeman 0:77803b3ee157 205 SDA = 1;
JonFreeman 0:77803b3ee157 206 wait_us (1);
JonFreeman 0:77803b3ee157 207 }
JonFreeman 0:77803b3ee157 208 if (SCL == 0 ) {
JonFreeman 0:77803b3ee157 209 rv |= 2; // Fault - SCL was lo on entry
JonFreeman 0:77803b3ee157 210 SCL = 1;
JonFreeman 0:77803b3ee157 211 wait_us (1);
JonFreeman 0:77803b3ee157 212 }
JonFreeman 0:77803b3ee157 213 SDA = 0; // Take SDA lo
JonFreeman 0:77803b3ee157 214 wait_us (1);
JonFreeman 0:77803b3ee157 215 SCL = 0;
JonFreeman 0:77803b3ee157 216 wait_us (1);
JonFreeman 0:77803b3ee157 217 return rv; // Returns 0 on success, 1 with SDA fault, 2 with SCL fault, 3 with SDA and SCL fault
JonFreeman 2:8e7b51353f32 218 #else
JonFreeman 2:8e7b51353f32 219 i2c.start () ;
JonFreeman 2:8e7b51353f32 220 return 0;
JonFreeman 2:8e7b51353f32 221
JonFreeman 2:8e7b51353f32 222 #endif
JonFreeman 1:450090bdb6f4 223 #endif
JonFreeman 1:450090bdb6f4 224 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 225 i2c.start () ;
JonFreeman 1:450090bdb6f4 226 return 0;
JonFreeman 1:450090bdb6f4 227 #endif
JonFreeman 0:77803b3ee157 228 }
JonFreeman 0:77803b3ee157 229
JonFreeman 0:77803b3ee157 230 /**
JonFreeman 0:77803b3ee157 231 * During data transfer, the data line must remain
JonFreeman 0:77803b3ee157 232 * stable whenever the clock line is high. Changes in
JonFreeman 0:77803b3ee157 233 * the data line while the clock line is high will be
JonFreeman 0:77803b3ee157 234 * interpreted as a Start or Stop condition
JonFreeman 0:77803b3ee157 235 *
JonFreeman 0:77803b3ee157 236 * A low-to-high transition of the SDA line while the clock
JonFreeman 0:77803b3ee157 237 * (SCL) is high determines a Stop condition. All
JonFreeman 0:77803b3ee157 238 * operations must be ended with a Stop condition.
JonFreeman 0:77803b3ee157 239 */
JonFreeman 0:77803b3ee157 240 int i2c_stop () { // Should be SDA=0, SCL=1, start takes SDA hi
JonFreeman 1:450090bdb6f4 241 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 242 #ifdef I2CTEST
JonFreeman 2:8e7b51353f32 243 i2c.stop () ;
JonFreeman 2:8e7b51353f32 244 return 0;
JonFreeman 2:8e7b51353f32 245 #else
JonFreeman 0:77803b3ee157 246 int rv = 0;
JonFreeman 0:77803b3ee157 247 SDA = 0; // Pull SDA to 0
JonFreeman 0:77803b3ee157 248 wait_us (1);
JonFreeman 0:77803b3ee157 249 if (SCL_IN != 0) {
JonFreeman 0:77803b3ee157 250 pc.printf ("SCL 1 on entry to stop\r\n");
JonFreeman 0:77803b3ee157 251 SCL = 0; // pull SCL to 0 if not there already
JonFreeman 0:77803b3ee157 252 wait_us (1);
JonFreeman 0:77803b3ee157 253 }
JonFreeman 0:77803b3ee157 254 SCL = 1;
JonFreeman 0:77803b3ee157 255 wait_us (1);
JonFreeman 0:77803b3ee157 256 if (SCL_IN == 0)
JonFreeman 0:77803b3ee157 257 pc.printf ("SCL stuck lo in stop\r\n");
JonFreeman 0:77803b3ee157 258 SDA = 1;
JonFreeman 0:77803b3ee157 259 wait_us (1);
JonFreeman 0:77803b3ee157 260 if (SDA_IN == 0)
JonFreeman 0:77803b3ee157 261 pc.printf ("SDA stuck lo in stop\r\n");
JonFreeman 0:77803b3ee157 262 return rv; // Returns 0 on success, 1 with SDA fault, 2 with SCL fault, 3 with SDA and SCL fault
JonFreeman 1:450090bdb6f4 263 #endif
JonFreeman 2:8e7b51353f32 264 #endif
JonFreeman 1:450090bdb6f4 265 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 266 i2c.stop () ;
JonFreeman 1:450090bdb6f4 267 return 0;
JonFreeman 1:450090bdb6f4 268 #endif
JonFreeman 0:77803b3ee157 269 }
JonFreeman 0:77803b3ee157 270
JonFreeman 1:450090bdb6f4 271 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 272 #ifndef I2CTEST
JonFreeman 0:77803b3ee157 273 void jclk (int bit) {
JonFreeman 0:77803b3ee157 274 SCL = bit;
JonFreeman 0:77803b3ee157 275 wait_us (1);
JonFreeman 0:77803b3ee157 276 }
JonFreeman 0:77803b3ee157 277
JonFreeman 0:77803b3ee157 278 void jclkout () {
JonFreeman 0:77803b3ee157 279 wait_us (1);
JonFreeman 0:77803b3ee157 280 SCL = 1;
JonFreeman 0:77803b3ee157 281 wait_us (1);
JonFreeman 0:77803b3ee157 282 SCL = 0;
JonFreeman 0:77803b3ee157 283 wait_us (1);
JonFreeman 0:77803b3ee157 284 }
JonFreeman 1:450090bdb6f4 285 #endif
JonFreeman 2:8e7b51353f32 286 #endif
JonFreeman 0:77803b3ee157 287
JonFreeman 0:77803b3ee157 288 int i2c_write (int d) {
JonFreeman 1:450090bdb6f4 289 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 290 #ifdef I2CTEST
JonFreeman 2:8e7b51353f32 291 return i2c.write (d);
JonFreeman 2:8e7b51353f32 292 #else
JonFreeman 0:77803b3ee157 293 int ackbit = 0;
JonFreeman 0:77803b3ee157 294 if (SCL_IN != 0) {
JonFreeman 0:77803b3ee157 295 pc.printf ("SCL hi on entry to write\r\n");
JonFreeman 0:77803b3ee157 296 jclk (0);
JonFreeman 0:77803b3ee157 297 }
JonFreeman 0:77803b3ee157 298 for (int i = 0x80; i != 0; i >>= 1) { // bit out msb first
JonFreeman 0:77803b3ee157 299 if ((d & i) == 0) SDA = 0;
JonFreeman 0:77803b3ee157 300 else SDA = 1;
JonFreeman 0:77803b3ee157 301 jclkout (); // SCL ____---____
JonFreeman 0:77803b3ee157 302 }
JonFreeman 0:77803b3ee157 303 SDA = 1; // Release data to allow remote device to pull lo for ACK or not
JonFreeman 0:77803b3ee157 304 jclk (1); // SCL = 1
JonFreeman 0:77803b3ee157 305 ackbit = SDA_IN; // read in ack bit
JonFreeman 0:77803b3ee157 306 jclk (0); // SCL = 0
JonFreeman 0:77803b3ee157 307 // pc.printf ("wr 0x%x %s\r\n", d, ackbit == 0 ? "ACK" : "nak");
JonFreeman 0:77803b3ee157 308 return ackbit; // 0 for acknowledged ACK, 1 for NAK
JonFreeman 1:450090bdb6f4 309 #endif
JonFreeman 2:8e7b51353f32 310 #endif
JonFreeman 1:450090bdb6f4 311 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 312 return i2c.write (d);
JonFreeman 1:450090bdb6f4 313 #endif
JonFreeman 0:77803b3ee157 314 }
JonFreeman 0:77803b3ee157 315
JonFreeman 0:77803b3ee157 316
JonFreeman 0:77803b3ee157 317
JonFreeman 0:77803b3ee157 318
JonFreeman 0:77803b3ee157 319 int i2c_read (int acknak) { // acknak indicates if the byte is to be acknowledged (0 = acknowledge)
JonFreeman 1:450090bdb6f4 320 #ifdef TARGET_NUCLEO_L432KC //
JonFreeman 2:8e7b51353f32 321 #ifdef I2CTEST
JonFreeman 2:8e7b51353f32 322 return i2c.read (acknak) ;
JonFreeman 2:8e7b51353f32 323 #else
JonFreeman 0:77803b3ee157 324 int result = 0; // SCL should be 1 on entry
JonFreeman 0:77803b3ee157 325 SDA = 1; // Master released SDA
JonFreeman 0:77803b3ee157 326 if (SCL_IN != 0) pc.printf ("SCL hi arriving at read\r\n");
JonFreeman 0:77803b3ee157 327 wait_us (2);
JonFreeman 0:77803b3ee157 328 for (int i = 0; i < 8; i++) {
JonFreeman 0:77803b3ee157 329 result <<= 1;
JonFreeman 0:77803b3ee157 330 jclk (1);
JonFreeman 0:77803b3ee157 331 if (SDA_IN != 0) result |= 1;
JonFreeman 0:77803b3ee157 332 jclk (0);
JonFreeman 0:77803b3ee157 333 }
JonFreeman 0:77803b3ee157 334 if (acknak != 0 && acknak != 1)
JonFreeman 0:77803b3ee157 335 pc.printf ("Bad acknak in 12c_read %d\r\n", acknak);
JonFreeman 0:77803b3ee157 336 if (acknak == 0) SDA = 0;
JonFreeman 0:77803b3ee157 337 else SDA = 1;
JonFreeman 0:77803b3ee157 338 jclkout (); // clock out the ACK bit __--__
JonFreeman 0:77803b3ee157 339 // pc.printf ("rd 0x%x %s\r\n", result, acknak == 0 ? "ACK" : "nak");
JonFreeman 0:77803b3ee157 340 return result; // Always ? nah
JonFreeman 1:450090bdb6f4 341 #endif
JonFreeman 2:8e7b51353f32 342 #endif
JonFreeman 1:450090bdb6f4 343 #ifdef TARGET_NUCLEO_F401RE //
JonFreeman 1:450090bdb6f4 344 return i2c.read (acknak) ;
JonFreeman 1:450090bdb6f4 345 #endif
JonFreeman 0:77803b3ee157 346 }
JonFreeman 0:77803b3ee157 347
JonFreeman 0:77803b3ee157 348 int check_24LC64 () { // Call from near top of main() to init i2c bus
JonFreeman 0:77803b3ee157 349 int last_found = 0, q, e; // Note address bits 3-1 to match addr pins on device
JonFreeman 0:77803b3ee157 350 for (int i = 0; i < 255; i += 2) { // Search for devices at all possible i2c addresses
JonFreeman 0:77803b3ee157 351 e = i2c_start();
JonFreeman 0:77803b3ee157 352 if (e) pc.putc(',');
JonFreeman 0:77803b3ee157 353 q = i2c_write(i); // may return error code 2 when no start issued
JonFreeman 0:77803b3ee157 354 if (q == ACK) {
JonFreeman 0:77803b3ee157 355 pc.printf ("I2C device found at 0x%x\r\n", i);
JonFreeman 0:77803b3ee157 356 last_found = i;
JonFreeman 0:77803b3ee157 357 wait_ms (5);
JonFreeman 0:77803b3ee157 358 }
JonFreeman 0:77803b3ee157 359 i2c_stop();
JonFreeman 0:77803b3ee157 360 }
JonFreeman 0:77803b3ee157 361 return last_found;
JonFreeman 0:77803b3ee157 362 }
JonFreeman 0:77803b3ee157 363
JonFreeman 0:77803b3ee157 364
JonFreeman 0:77803b3ee157 365
JonFreeman 0:77803b3ee157 366
JonFreeman 0:77803b3ee157 367
JonFreeman 0:77803b3ee157 368
JonFreeman 0:77803b3ee157 369
JonFreeman 0:77803b3ee157 370
JonFreeman 0:77803b3ee157 371 bool ack_poll () { // wait short while for any previous memory operation to complete
JonFreeman 0:77803b3ee157 372 const int poll_tries = 40;
JonFreeman 0:77803b3ee157 373 int poll_count = 0;
JonFreeman 0:77803b3ee157 374 bool i2cfree = false;
JonFreeman 0:77803b3ee157 375 while (poll_count++ < poll_tries && !i2cfree) {
JonFreeman 0:77803b3ee157 376 i2c_start ();
JonFreeman 0:77803b3ee157 377 if (i2c_write(_24LC_wr) == ACK)
JonFreeman 0:77803b3ee157 378 i2cfree = true;
JonFreeman 0:77803b3ee157 379 else
JonFreeman 0:77803b3ee157 380 wait_ms (1);
JonFreeman 0:77803b3ee157 381 }
JonFreeman 0:77803b3ee157 382 // pc.printf ("ack_poll, count = %d, i2cfree = %s\r\n", poll_count, i2cfree ? "true" : "false");
JonFreeman 0:77803b3ee157 383 return i2cfree;
JonFreeman 0:77803b3ee157 384 }
JonFreeman 0:77803b3ee157 385
JonFreeman 0:77803b3ee157 386 /**bool set_24LC64_internal_address (int start_addr) {
JonFreeman 0:77803b3ee157 387 *
JonFreeman 0:77803b3ee157 388 *
JonFreeman 0:77803b3ee157 389 *
JonFreeman 0:77803b3ee157 390 */
JonFreeman 0:77803b3ee157 391 bool set_24LC64_internal_address (int start_addr) {
JonFreeman 0:77803b3ee157 392 if (!ack_poll())
JonFreeman 0:77803b3ee157 393 {
JonFreeman 0:77803b3ee157 394 pc.printf ("Err in set_24LC64_internal_address, no ACK writing device address byte\r\n");
JonFreeman 0:77803b3ee157 395 i2c_stop();
JonFreeman 0:77803b3ee157 396 return false;
JonFreeman 0:77803b3ee157 397 }
JonFreeman 0:77803b3ee157 398 int err = 0;
JonFreeman 0:77803b3ee157 399 if (i2c_write(start_addr >> 8) != ACK) err++;
JonFreeman 0:77803b3ee157 400 if (i2c_write(start_addr & 0xff) != ACK) err++;
JonFreeman 0:77803b3ee157 401 if (err) {
JonFreeman 0:77803b3ee157 402 pc.printf ("In set_24LC64_internal_address, Believe Device present, failed in writing 2 mem addr bytes %d\r\n", err);
JonFreeman 0:77803b3ee157 403 i2c_stop();
JonFreeman 0:77803b3ee157 404 return false;
JonFreeman 0:77803b3ee157 405 }
JonFreeman 0:77803b3ee157 406 // pc.printf ("GOOD set_24LC64_internal_address %d\r\n", start_addr);
JonFreeman 0:77803b3ee157 407 return true;
JonFreeman 0:77803b3ee157 408 }
JonFreeman 0:77803b3ee157 409
JonFreeman 0:77803b3ee157 410 bool wr_24LC64 (int start_addr, char * source, int length) { // think this works
JonFreeman 0:77803b3ee157 411 int err = 0;
JonFreeman 0:77803b3ee157 412 if(length < 1 || length > 32) {
JonFreeman 0:77803b3ee157 413 pc.printf ("Length out of range %d in wr_24LC64\r\n", length);
JonFreeman 0:77803b3ee157 414 return false;
JonFreeman 0:77803b3ee157 415 }
JonFreeman 1:450090bdb6f4 416 ack_poll ();
JonFreeman 0:77803b3ee157 417 if (!set_24LC64_internal_address (start_addr)) {
JonFreeman 0:77803b3ee157 418 pc.printf ("In wr_24LC64, Believe Device present, failed in writing 2 mem addr bytes %d\r\n", err);
JonFreeman 0:77803b3ee157 419 return false;
JonFreeman 0:77803b3ee157 420 }
JonFreeman 1:450090bdb6f4 421 while(length--)
JonFreeman 1:450090bdb6f4 422 if (i2c_write(*source++) != ACK)
JonFreeman 1:450090bdb6f4 423 err++;
JonFreeman 1:450090bdb6f4 424 // err += i2c_write(*source++);
JonFreeman 0:77803b3ee157 425 i2c_stop();
JonFreeman 0:77803b3ee157 426 if (err) {
JonFreeman 0:77803b3ee157 427 pc.printf ("in wr_24LC64, device thought good, mem addr write worked, failed writing string\r\n");
JonFreeman 0:77803b3ee157 428 return false;
JonFreeman 0:77803b3ee157 429 }
JonFreeman 0:77803b3ee157 430 // pc.printf ("In wr_24LC64 No Errors Found!\r\n");
JonFreeman 0:77803b3ee157 431 return true;
JonFreeman 0:77803b3ee157 432 }
JonFreeman 0:77803b3ee157 433
JonFreeman 0:77803b3ee157 434 bool rd_24LC64 (int start_addr, char * dest, int length) {
JonFreeman 0:77803b3ee157 435 int acknak = ACK;
JonFreeman 0:77803b3ee157 436 if(length < 1)
JonFreeman 0:77803b3ee157 437 return false;
JonFreeman 0:77803b3ee157 438 if (!set_24LC64_internal_address (start_addr)) {
JonFreeman 0:77803b3ee157 439 pc.printf ("In rd_24LC64, failed to set_ramaddr\r\n");
JonFreeman 0:77803b3ee157 440 return false;
JonFreeman 0:77803b3ee157 441 }
JonFreeman 0:77803b3ee157 442 i2c_start();
JonFreeman 0:77803b3ee157 443 if (i2c_write(_24LC_rd) != ACK) {
JonFreeman 0:77803b3ee157 444 pc.printf ("Errors in rd_24LC64 sending 24LC_rd\r\n");
JonFreeman 0:77803b3ee157 445 return false;
JonFreeman 0:77803b3ee157 446 }
JonFreeman 0:77803b3ee157 447 while(length--) {
JonFreeman 0:77803b3ee157 448 if(length == 0)
JonFreeman 0:77803b3ee157 449 acknak = 1;
JonFreeman 0:77803b3ee157 450 *dest++ = i2c_read(acknak);
JonFreeman 0:77803b3ee157 451 }
JonFreeman 0:77803b3ee157 452 i2c_stop();
JonFreeman 0:77803b3ee157 453 return true;
JonFreeman 0:77803b3ee157 454 }
JonFreeman 0:77803b3ee157 455
JonFreeman 0:77803b3ee157 456
JonFreeman 0:77803b3ee157 457
JonFreeman 0:77803b3ee157 458
JonFreeman 0:77803b3ee157 459
JonFreeman 0:77803b3ee157 460
JonFreeman 0:77803b3ee157 461
JonFreeman 0:77803b3ee157 462
JonFreeman 0:77803b3ee157 463
JonFreeman 0:77803b3ee157 464
JonFreeman 0:77803b3ee157 465
JonFreeman 0:77803b3ee157 466