Jon Freeman / Mbed 2 deprecated Alternator2020_06

Dependencies:   mbed BufferedSerial Servo2 PCT2075 I2CEeprom FastPWM

Committer:
JonFreeman
Date:
Sat Apr 25 15:35:58 2020 +0000
Revision:
1:450090bdb6f4
Parent:
0:77803b3ee157
Child:
2:8e7b51353f32
About to pick this up again late Apr 2020.;

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