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
Diff: i2c_bit_banged.cpp
- Revision:
- 2:8e7b51353f32
- Parent:
- 1:450090bdb6f4
--- a/i2c_bit_banged.cpp Sat Apr 25 15:35:58 2020 +0000
+++ b/i2c_bit_banged.cpp Mon Jun 08 13:46:52 2020 +0000
@@ -1,6 +1,16 @@
#include "mbed.h"
#include "Alternator.h"
+
+#define I2CTEST
+
#ifdef TARGET_NUCLEO_L432KC //
+#ifdef I2CTEST
+extern Serial pc;
+extern I2C i2c;
+const int ACK = 1; // but acknowledge is 0, NAK is 1 6/6/2020 think this should be 1
+
+#else
+
extern Serial pc;
DigitalInOut SDA (D4); // Horrible bodge to get i2c working using bit banging.
DigitalInOut SCL (D5); // DigitalInOut do not work as you might expect. Fine if used only as OpenDrain opuputs though!
@@ -13,6 +23,7 @@
extern I2C i2c;
const int ACK = 1; // but acknowledge is 0, NAK is 1
#endif
+#endif
const int _24LC_rd = 0xa1; // set bit 0 for read, clear bit 0 for write
const int _24LC_wr = 0xa0; // set bit 0 for read, clear bit 0 for write
@@ -22,20 +33,35 @@
const char * t; // description
} ;*/
struct optpar option_list2[] = {
- {0, 100, 10, "max pwm% @ Eng RPM 0, 0 to 100"},
- {0, 100, 10, "max pwm% @ Eng RPM 1000, 0 to 100"},
- {0, 100, 20, "max pwm% @ Eng RPM 2000, 0 to 100"},
- {0, 100, 30, "max pwm% @ Eng RPM 3000, 0 to 100"},
- {0, 100, 40, "max pwm% @ Eng RPM 4000, 0 to 100"},
- {0, 100, 50, "max pwm% @ Eng RPM 5000, 0 to 100"},
- {0, 100, 50, "max pwm% @ Eng RPM 6000, 0 to 100"},
- {0, 100, 50, "max pwm% @ Eng RPM 7000, 0 to 100"},
- {0, 100, 50, "max pwm% @ Eng RPM 8000, 0 to 100"},
- {0, 100, 50, "Set Overall PWM Scale Factor percent"},
+ {0, 100, 1, "max pwm% @ Eng RPM 0, 0 to 100"},
+ {0, 100, 1, "max pwm% @ Eng RPM 500, 0 to 100"},
+ {0, 100, 1, "max pwm% @ Eng RPM 1000, 0 to 100"},
+ {0, 100, 1, "max pwm% @ Eng RPM 1500, 0 to 100"},
+ {0, 100, 1, "max pwm% @ Eng RPM 2000, 0 to 100"},
+ {0, 100, 10, "max pwm% @ Eng RPM 2500, 0 to 100"},
+ {0, 100, 60, "max pwm% @ Eng RPM 3000, 0 to 100"},
+ {0, 100, 70, "max pwm% @ Eng RPM 3500, 0 to 100"},
+ {0, 100, 60, "max pwm% @ Eng RPM 4000, 0 to 100"},
+ {0, 100, 50, "max pwm% @ Eng RPM 4500, 0 to 100"},
+ {0, 100, 40, "max pwm% @ Eng RPM 5000, 0 to 100"},
+ {0, 100, 33, "max pwm% @ Eng RPM 5500, 0 to 100"},
+ {0, 100, 30, "max pwm% @ Eng RPM 6000, 0 to 100"},
+ {0, 100, 30, "max pwm% @ Eng RPM 6500, 0 to 100"},
+ {0, 100, 40, "max pwm% @ Eng RPM 7000, 0 to 100"},
+ {0, 100, 50, "max pwm% @ Eng RPM 7500, 0 to 100"},
+ {0, 100, 60, "max pwm% @ Eng RPM 8000, 0 to 100"},
{0, 100, 0, "Future 2"},
{0, 100, 0, "Future 3"},
{0, 100, 0, "Future 4"},
{0, 100, 0, "Future 5"},
+ {0, 100, 0, "Future 6"},
+ {0, 100, 0, "Future 7"},
+ {0, 100, 0, "Future 8"},
+ {0, 100, 0, "Future 9"},
+ {0, 100, 0, "Future 10"},
+ {0, 100, 0, "Future 11"},
+ {0, 100, 0, "Future 12"},
+ {0, 100, 0, "Future 13"},
} ;
const int numof_eeprom_options2 = sizeof(option_list2) / sizeof (struct optpar);
@@ -79,16 +105,16 @@
return true;
}
-double eeprom_settings::get_pwm (int rpm) {
+/*double eeprom_settings::get_pwm (int rpm) {
int p = rpm * lut_size;
p /= 8000; // 8000 is upper RPM limit, p now scaled to sizeof lut
if (p < 0) p = 0; // point to first
if (p >= lut_size) p = lut_size - 1; // point to last
// pc.printf ("In get_pwm, rpm = %d, lut entry = %d, pwm = %d\r\n", rpm, p, max_pwm_lut[p]);
return max_pwm_lut[p];
-}
+}*/
-void eeprom_settings::build_lut () {
+/*void eeprom_settings::build_lut () {
int ptr = 0;
int range, i;
double acc = 0.0, incr = 0.0;
@@ -114,12 +140,12 @@
}
pc.printf ("lut_size = %d\r\n", lut_size);
-}
+}*/
bool eeprom_settings::load () { // Get 'settings' buffer from EEPROM
bool rv ;
rv = rd_24LC64 (eeprom_page * 32, settings, 32); // Can now build lookup table
- build_lut ();
+//Apr2020 build_lut ();
return rv;
}
@@ -139,6 +165,7 @@
*/
bool i2c_init(void) {
#ifdef TARGET_NUCLEO_L432KC //
+#ifndef I2CTEST
SDA.output();
SCL.output();
SDA.mode(OpenDrain);
@@ -151,12 +178,12 @@
SCL = 1;
wait_us (1);
if (SCL_IN == 0 || SDA_IN == 0) return false;
- return true;
+#endif
#endif
#ifdef TARGET_NUCLEO_F401RE //
// return i2c.init () ; // class has no member "init"
+#endif
return true;
-#endif
}
/**
@@ -171,6 +198,7 @@
*/
int i2c_start () { // Should be Both hi, start takes SDA low
#ifdef TARGET_NUCLEO_L432KC //
+#ifndef I2CTEST
int rv = 0;
if (SDA_IN == 0 ) {
rv |= 1; // Fault - SDA was lo on entry
@@ -187,6 +215,11 @@
SCL = 0;
wait_us (1);
return rv; // Returns 0 on success, 1 with SDA fault, 2 with SCL fault, 3 with SDA and SCL fault
+#else
+ i2c.start () ;
+ return 0;
+
+#endif
#endif
#ifdef TARGET_NUCLEO_F401RE //
i2c.start () ;
@@ -206,6 +239,10 @@
*/
int i2c_stop () { // Should be SDA=0, SCL=1, start takes SDA hi
#ifdef TARGET_NUCLEO_L432KC //
+#ifdef I2CTEST
+ i2c.stop () ;
+ return 0;
+#else
int rv = 0;
SDA = 0; // Pull SDA to 0
wait_us (1);
@@ -224,6 +261,7 @@
pc.printf ("SDA stuck lo in stop\r\n");
return rv; // Returns 0 on success, 1 with SDA fault, 2 with SCL fault, 3 with SDA and SCL fault
#endif
+#endif
#ifdef TARGET_NUCLEO_F401RE //
i2c.stop () ;
return 0;
@@ -231,6 +269,7 @@
}
#ifdef TARGET_NUCLEO_L432KC //
+#ifndef I2CTEST
void jclk (int bit) {
SCL = bit;
wait_us (1);
@@ -244,9 +283,13 @@
wait_us (1);
}
#endif
+#endif
int i2c_write (int d) {
#ifdef TARGET_NUCLEO_L432KC //
+#ifdef I2CTEST
+ return i2c.write (d);
+#else
int ackbit = 0;
if (SCL_IN != 0) {
pc.printf ("SCL hi on entry to write\r\n");
@@ -264,6 +307,7 @@
// pc.printf ("wr 0x%x %s\r\n", d, ackbit == 0 ? "ACK" : "nak");
return ackbit; // 0 for acknowledged ACK, 1 for NAK
#endif
+#endif
#ifdef TARGET_NUCLEO_F401RE //
return i2c.write (d);
#endif
@@ -274,6 +318,9 @@
int i2c_read (int acknak) { // acknak indicates if the byte is to be acknowledged (0 = acknowledge)
#ifdef TARGET_NUCLEO_L432KC //
+#ifdef I2CTEST
+ return i2c.read (acknak) ;
+#else
int result = 0; // SCL should be 1 on entry
SDA = 1; // Master released SDA
if (SCL_IN != 0) pc.printf ("SCL hi arriving at read\r\n");
@@ -292,6 +339,7 @@
// pc.printf ("rd 0x%x %s\r\n", result, acknak == 0 ? "ACK" : "nak");
return result; // Always ? nah
#endif
+#endif
#ifdef TARGET_NUCLEO_F401RE //
return i2c.read (acknak) ;
#endif