Skip to content

Commit a4674b0

Browse files
committed
Accelerometer now working...
1 parent 9ee4e3d commit a4674b0

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

boards/BANGLEJS.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
};
7777

7878
devices = {
79-
8079
'BTN1' : { 'pin' : 'D24', 'pinstate' : 'IN_PULLDOWN' }, # top
8180
'BTN2' : { 'pin' : 'D22', 'pinstate' : 'IN_PULLDOWN' }, # middle
8281
'BTN3' : { 'pin' : 'D23', 'pinstate' : 'IN_PULLDOWN' }, # bottom

boards/SMAQ3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@
111111
},
112112
'ACCEL' : {
113113
'device' : 'KX023', 'addr' : 0x1e,
114-
'pin_sda' : 'D37',
115-
'pin_scl' : 'D38'
114+
'pin_sda' : 'D38',
115+
'pin_scl' : 'D37'
116116
},
117117
'MAG' : { # Magnetometer/compass
118118
'device' : 'unknown',
119119
'addr' : 0x0C,
120-
'pin_sda' : 'D45',
121-
'pin_scl' : 'D44'
120+
'pin_sda' : 'D44',
121+
'pin_scl' : 'D45'
122122
},
123123
# PRESSURE
124124
'SPIFLASH' : {

libs/banglejs/jswrap_bangle.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,21 @@ typedef struct {
313313
#define ACCEL_POLL_INTERVAL_MAX 5000 // in msec - DEFAULT_ACCEL_POLL_INTERVAL_MAX+TIMER_MAX must be <65535
314314
#define BTN_LOAD_TIMEOUT 1500 // in msec - how long does the button have to be pressed for before we restart
315315
#define TIMER_MAX 60000 // 60 sec - enough to fit in uint16_t without overflow if we add ACCEL_POLL_INTERVAL
316+
317+
#ifdef SMAQ3
318+
JshI2CInfo i2cAccel;
319+
JshI2CInfo i2cMag;
320+
// TODO: pressure, hrm...
321+
#define ACCEL_I2C &i2cAccel
322+
#define MAG_I2C &i2cMag
323+
#else
316324
/// Internal I2C used for Accelerometer/Pressure
317325
JshI2CInfo i2cInternal;
326+
#define ACCEL_I2C &i2cInternal
327+
#define MAG_I2C &i2cInternal
328+
#endif
329+
330+
318331
/// Is I2C busy? if so we'll skip one reading in our interrupt so we don't overlap
319332
bool i2cBusy;
320333
/// How often should be poll for accelerometer/compass data?
@@ -537,8 +550,8 @@ void peripheralPollHandler() {
537550
// check the magnetometer if we had it on
538551
if (compassPowerOn) {
539552
buf[0]=0x10;
540-
jsi2cWrite(&i2cInternal, MAG_ADDR, 1, buf, true);
541-
jsi2cRead(&i2cInternal, MAG_ADDR, 7, buf, true);
553+
jsi2cWrite(MAG_I2C, MAG_ADDR, 1, buf, true);
554+
jsi2cRead(MAG_I2C, MAG_ADDR, 7, buf, true);
542555
if (buf[0]&1) { // then we have data (hopefully? No datasheet)
543556
mag.y = buf[1] | (buf[2]<<8);
544557
mag.x = buf[3] | (buf[4]<<8);
@@ -555,8 +568,8 @@ void peripheralPollHandler() {
555568
// poll KX023 accelerometer (no other way as IRQ line seems disconnected!)
556569
// read interrupt source data
557570
buf[0]=0x12; // INS1
558-
jsi2cWrite(&i2cInternal, ACCEL_ADDR, 1, buf, true);
559-
jsi2cRead(&i2cInternal, ACCEL_ADDR, 2, buf, true);
571+
jsi2cWrite(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
572+
jsi2cRead(ACCEL_I2C, ACCEL_ADDR, 2, buf, true);
560573
// 0 -> 0x12 INS1 - tap event
561574
// 1 -> 0x13 INS2 - what kind of event
562575
bool hasAccelData = (buf[1]&16)!=0; // DRDY
@@ -567,13 +580,13 @@ void peripheralPollHandler() {
567580
bangleTasks |= JSBT_ACCEL_TAPPED;
568581
// clear the IRQ flags
569582
buf[0]=0x17;
570-
jsi2cWrite(&i2cInternal, ACCEL_ADDR, 1, buf, true);
571-
jsi2cRead(&i2cInternal, ACCEL_ADDR, 1, buf, true);
583+
jsi2cWrite(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
584+
jsi2cRead(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
572585
}
573586
if (hasAccelData) {
574587
buf[0]=6;
575-
jsi2cWrite(&i2cInternal, ACCEL_ADDR, 1, buf, true);
576-
jsi2cRead(&i2cInternal, ACCEL_ADDR, 6, buf, true);
588+
jsi2cWrite(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
589+
jsi2cRead(ACCEL_I2C, ACCEL_ADDR, 6, buf, true);
577590
// work out current reading in 16 bit
578591
short newx = (buf[1]<<8)|buf[0];
579592
short newy = (buf[3]<<8)|buf[2];
@@ -1435,14 +1448,24 @@ void jswrap_banglejs_init() {
14351448

14361449
// Set up I2C
14371450
i2cBusy = true;
1451+
#ifdef SMAQ3
1452+
jshI2CInitInfo(&i2cAccel);
1453+
i2cAccel.bitrate = 0x7FFFFFFF; // make it as fast as we can go
1454+
i2cAccel.pinSDA = ACCEL_PIN_SDA;
1455+
i2cAccel.pinSCL = ACCEL_PIN_SCL;
1456+
jsi2cSetup(&i2cMag);
1457+
jshI2CInitInfo(&i2cMag);
1458+
i2cMag.bitrate = 0x7FFFFFFF; // make it as fast as we can go
1459+
i2cMag.pinSDA = MAG_PIN_SDA;
1460+
i2cMag.pinSCL = MAG_PIN_SCL;
1461+
jsi2cSetup(&i2cMag);
1462+
#else
14381463
jshI2CInitInfo(&i2cInternal);
14391464
i2cInternal.bitrate = 0x7FFFFFFF; // make it as fast as we can go
14401465
i2cInternal.pinSDA = ACCEL_PIN_SDA;
14411466
i2cInternal.pinSCL = ACCEL_PIN_SCL;
1442-
jshPinSetValue(i2cInternal.pinSCL, 1);
1443-
jshPinSetState(i2cInternal.pinSCL, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
1444-
jshPinSetValue(i2cInternal.pinSDA, 1);
1445-
jshPinSetState(i2cInternal.pinSDA, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
1467+
jsi2cSetup(&i2cInternal);
1468+
#endif
14461469
#ifndef SMAQ3
14471470
// LCD pin init
14481471
jshPinOutput(LCD_PIN_CS, 1);
@@ -2102,7 +2125,7 @@ void jswrap_banglejs_accelWr(JsVarInt reg, JsVarInt data) {
21022125
buf[0] = (unsigned char)reg;
21032126
buf[1] = (unsigned char)data;
21042127
i2cBusy = true;
2105-
jsi2cWrite(&i2cInternal, ACCEL_ADDR, 2, buf, true);
2128+
jsi2cWrite(ACCEL_I2C, ACCEL_ADDR, 2, buf, true);
21062129
i2cBusy = false;
21072130
#endif
21082131
}
@@ -2125,8 +2148,8 @@ int jswrap_banglejs_accelRd(JsVarInt reg) {
21252148
unsigned char buf[1];
21262149
buf[0] = (unsigned char)reg;
21272150
i2cBusy = true;
2128-
jsi2cWrite(&i2cInternal, ACCEL_ADDR, 1, buf, true);
2129-
jsi2cRead(&i2cInternal, ACCEL_ADDR, 1, buf, true);
2151+
jsi2cWrite(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
2152+
jsi2cRead(ACCEL_I2C, ACCEL_ADDR, 1, buf, true);
21302153
i2cBusy = false;
21312154
return buf[0];
21322155
#else
@@ -2154,7 +2177,7 @@ void jswrap_banglejs_compassWr(JsVarInt reg, JsVarInt data) {
21542177
buf[0] = (unsigned char)reg;
21552178
buf[1] = (unsigned char)data;
21562179
i2cBusy = true;
2157-
jsi2cWrite(&i2cInternal, MAG_ADDR, 2, buf, true);
2180+
jsi2cWrite(MAG_I2C, MAG_ADDR, 2, buf, true);
21582181
i2cBusy = false;
21592182
#endif
21602183
}
@@ -2173,6 +2196,7 @@ void jswrap_banglejs_compassWr(JsVarInt reg, JsVarInt data) {
21732196
Changes a pin state on the IO expander
21742197
*/
21752198
void jswrap_banglejs_ioWr(JsVarInt mask, bool on) {
2199+
#ifndef SMAQ3
21762200
#ifndef EMSCRIPTEN
21772201
static unsigned char state;
21782202
if (on) state |= mask;
@@ -2181,6 +2205,7 @@ void jswrap_banglejs_ioWr(JsVarInt mask, bool on) {
21812205
jsi2cWrite(&i2cInternal, 0x20, 1, &state, true);
21822206
i2cBusy = false;
21832207
#endif
2208+
#endif
21842209
}
21852210

21862211

src/jsi2c.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ static void i2c_initstruct(i2cInfo *inf, JshI2CInfo *i) {
162162

163163
// ----------------------------------------------------------------------------
164164

165+
void jsi2cSetup(JshI2CInfo *inf) {
166+
jshPinSetValue(inf->pinSCL, 1);
167+
jshPinSetState(inf->pinSCL, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
168+
jshPinSetValue(inf->pinSDA, 1);
169+
jshPinSetState(inf->pinSDA, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
170+
}
171+
165172
void jsi2cWrite(JshI2CInfo *inf, unsigned char address, int nBytes, const unsigned char *data, bool sendStop) {
166173
if (inf->pinSCL==PIN_UNDEFINED || inf->pinSDA==PIN_UNDEFINED)
167174
return;

src/jsi2c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ bool jsi2cPopulateI2CInfo(
1919
JsVar *options
2020
);
2121

22+
void jsi2cSetup(JshI2CInfo *inf);
2223
void jsi2cWrite(JshI2CInfo *inf, unsigned char address, int nBytes, const unsigned char *data, bool sendStop);
2324
void jsi2cRead(JshI2CInfo *inf, unsigned char address, int nBytes, unsigned char *data, bool sendStop);

0 commit comments

Comments
 (0)