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