Skip to content

Commit 7e07843

Browse files
committed
Added i2cRead/WriteReg for simplified internal I2C code
Bangle.js2: Support for new Bangles with MMC36X0 magnetometer (Bangle.dbg() now shows info)
1 parent 711379b commit 7e07843

File tree

7 files changed

+145
-119
lines changed

7 files changed

+145
-119
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
nRF52 Central: Lower min connection interval from 20->7.5ms, max 200->1000ms (allows some extra devices to be connected to)
1313
Bangle.js2: Add optional long press button actions to `showPrompt` (#2656)
1414
nRF52: Fix gatt.setRSSIHandler on the first active connection
15+
Added i2cRead/WriteReg for simplified internal I2C code
16+
Bangle.js2: Support for new Bangles with MMC36X0 magnetometer (Bangle.dbg() now shows info)
1517

1618
2v27 : nRF5x: Ensure Bluetooth notifications work correctly when two separate connections use the same handle for their characteristics
1719
nRF5x: Remove handlers from our handlers array when a device is disconnected

libs/banglejs/jswrap_bangle.c

Lines changed: 115 additions & 88 deletions
Large diffs are not rendered by default.

libs/misc/hrm_vc31.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ VC31AInfo vcaInfo;
215215
VC31BInfo vcbInfo;
216216

217217
static void vc31_w(uint8_t reg, uint8_t data) {
218-
uint8_t buf[2] = {reg, data};
219-
jsi2cWrite(&i2cHRM, HEARTRATE_ADDR, 2, buf, true);
218+
jsi2cWriteReg(&i2cHRM, HEARTRATE_ADDR, reg, data);
220219
}
221220
static void vc31_wx(uint8_t reg, uint8_t *data, int cnt) {
222221
uint8_t buf[32];
@@ -226,10 +225,9 @@ static void vc31_wx(uint8_t reg, uint8_t *data, int cnt) {
226225
}
227226

228227
static uint8_t vc31_r(uint8_t reg) {
229-
uint8_t buf[1] = {reg};
230-
jsi2cWrite(&i2cHRM, HEARTRATE_ADDR, 1, buf, false);
231-
jsi2cRead(&i2cHRM, HEARTRATE_ADDR, 1, buf, true);
232-
return buf[0];
228+
uint8_t v;
229+
jsi2cReadReg(&i2cHRM, HEARTRATE_ADDR, reg, 1, &v);
230+
return v;
233231
}
234232
static void vc31_rx(uint8_t reg, uint8_t *data, int cnt) {
235233
uint8_t buf[1] = {reg};

libs/nordic_thingy/jswrap_thingy.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ JshI2CInfo i2cInfo = {
4747

4848
// Write to IO expander
4949
void sxWriteReg(unsigned reg, unsigned d) {
50-
unsigned char data[2] = {reg,d};
51-
jsi2cWrite(&i2cInfo, SX_I2C_ADDR, sizeof(data), data, true);
50+
jsi2cWriteReg(&i2cInfo, SX_I2C_ADDR, reg, d);
5251
}
5352
// Read from IO expander
5453
unsigned char sxReadReg(unsigned reg) {
55-
unsigned char data = reg;
56-
jsi2cWrite(&i2cInfo, SX_I2C_ADDR, 1, &data, true);
57-
jsi2cRead(&i2cInfo, SX_I2C_ADDR, 1, &data, true);
54+
unsigned char data;
55+
jsi2cReadReg(&i2cInfo, SX_I2C_ADDR, reg, 1, &data);
5856
return data;
5957
}
6058

libs/puckjs/jswrap_puck.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,7 @@ void mag_rd(int addr, unsigned char *data, int cnt) {
254254
wr(MAG_PIN_SDA, 1);
255255
wr(MAG_PIN_SCL, 1);
256256
} else {
257-
unsigned char buf[1];
258-
buf[0] = addr;
259-
jsi2cWrite(&i2cMag, iaddr, 1, buf, false);
260-
jsi2cRead(&i2cMag, iaddr, cnt, data, true);
257+
jsi2cReadReg(&i2cMag, iaddr, addr, cnt, data);
261258
}
262259
}
263260

@@ -559,9 +556,7 @@ bool accel_on(int milliHz) {
559556
void accel_read() {
560557
unsigned char buf[12];
561558
// FIXME: If just the accelerometer is on, we could avoid reading the gyro and read only 6 bytes
562-
buf[0] = 0x22; // OUTX_L_G
563-
jsi2cWrite(&i2cAccel, ACCEL_ADDR, 1, buf, false);
564-
jsi2cRead(&i2cAccel, ACCEL_ADDR, 12, buf, true);
559+
jsi2cReadReg(&i2cAccel, ACCEL_ADDR, 0x22, 12, buf); // OUTX_L_G
565560
gyro_reading[0] = (buf[1]<<8) | buf[0];
566561
gyro_reading[1] = (buf[3]<<8) | buf[2];
567562
gyro_reading[2] = (buf[5]<<8) | buf[4];
@@ -575,9 +570,7 @@ void accel_wait() {
575570
unsigned char buf[1];
576571
timeout = 400;
577572
do {
578-
buf[0] = 0x1E; // STATUS_REG
579-
jsi2cWrite(&i2cAccel, ACCEL_ADDR, 1, buf, false);
580-
jsi2cRead(&i2cAccel, ACCEL_ADDR, 1, buf, true);
573+
jsi2cReadReg(&i2cAccel, ACCEL_ADDR, 0x1E, 1, buf); // STATUS_REG
581574
//jsiConsolePrintf("M %d\n", buf[0]);
582575
} while (!(buf[0]&3) && --timeout); // ZYXDA
583576
if (!timeout) jsExceptionHere(JSET_INTERNALERROR, "Timeout (Accelerometer)");
@@ -950,9 +943,7 @@ JsVarFloat jswrap_puck_getTemperature() {
950943
//buf[1] = 1; // CONF
951944
//buf[0] = 0; // on
952945
//jsi2cWrite(&i2cTemp,TEMP_ADDR, 1, buf, false);
953-
buf[0] = 0; // TEMP
954-
jsi2cWrite(&i2cTemp,TEMP_ADDR, 1, buf, false);
955-
jsi2cRead(&i2cTemp, TEMP_ADDR, 2, buf, true);
946+
jsi2cReadReg(&i2cTemp, TEMP_ADDR, 0, 2, buf); // TEMP
956947
int t = (buf[0]<<3) | (buf[1]>>5);
957948
if (t&1024) t-=2048; // negative
958949
temp_off();
@@ -1153,9 +1144,7 @@ int jswrap_puck_accelRd(JsVarInt reg) {
11531144
return -1;
11541145
}
11551146
unsigned char buf[1];
1156-
buf[0] = (unsigned char)reg;
1157-
jsi2cWrite(&i2cAccel, ACCEL_ADDR, 1, buf, false);
1158-
jsi2cRead(&i2cAccel, ACCEL_ADDR, 1, buf, true);
1147+
jsi2cReadReg(&i2cAccel, ACCEL_ADDR, reg, 1, buf);
11591148
return buf[0];
11601149
}
11611150

@@ -1555,9 +1544,7 @@ bool _jswrap_puck_selfTest(bool advertisePassOrFail) {
15551544
if (PUCKJS_HAS_ACCEL) {
15561545
accel_on(1660000);
15571546
unsigned char buf[1];
1558-
buf[0] = 0x0F; // WHOAMI
1559-
jsi2cWrite(&i2cAccel, ACCEL_ADDR, 1, buf, false);
1560-
jsi2cRead(&i2cAccel, ACCEL_ADDR, 1, buf, true);
1547+
jsi2cReadReg(&i2cAccel, ACCEL_ADDR, 0x0F, 1, buf); // WHOAMI
15611548
accel_off();
15621549
if (buf[0]!=106) {
15631550
if (!err[0]) strcpy(err,"ACC");

src/jsi2c.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void i2c_wr_bit(i2cInfo *inf, bool b) {
120120
if (inf->timeout && !timeout) err("Timeout (wr)");
121121
i2c_pin_wr0(inf->pinSCL);
122122
i2c_pin_wr1(inf->pinSDA); // stop forcing SDA (needed?)
123-
dly(inf);
123+
dly(inf);
124124
}
125125

126126
static bool i2c_rd_bit(i2cInfo *inf) {
@@ -218,4 +218,16 @@ bool jsi2cRead(JshI2CInfo *inf, unsigned char address, int nBytes, unsigned char
218218
return true;
219219
}
220220

221+
bool jsi2cWriteReg(JshI2CInfo *inf, unsigned char address, unsigned char reg, unsigned char value) {
222+
unsigned char buf[2];
223+
buf[0] = reg;
224+
buf[1] = value;
225+
return jsi2cWrite(inf, address, 2, buf, true);
226+
}
227+
228+
bool jsi2cReadReg(JshI2CInfo *inf, unsigned char address, unsigned char reg, int nBytes, unsigned char *data) {
229+
jsi2cWrite(inf, address, 1, &reg, false);
230+
return jsi2cRead(inf, address, nBytes, data, true);
231+
}
232+
221233
#endif // ESPR_NO_SOFT_I2C

src/jsi2c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void jsi2cSetup(JshI2CInfo *inf);
2727
void jsi2cUnsetup(JshI2CInfo *inf); ///< turn off I2C (remove pullups/sense)
2828
bool jsi2cWrite(JshI2CInfo *inf, unsigned char address, int nBytes, const unsigned char *data, bool sendStop);
2929
bool jsi2cRead(JshI2CInfo *inf, unsigned char address, int nBytes, unsigned char *data, bool sendStop);
30+
bool jsi2cWriteReg(JshI2CInfo *inf, unsigned char address, unsigned char reg, unsigned char value);
31+
bool jsi2cReadReg(JshI2CInfo *inf, unsigned char address, unsigned char reg, int nBytes, unsigned char *data);
3032
#endif // ESPR_NO_SOFT_I2C
3133

3234
#endif // JSI2C_H_

0 commit comments

Comments
 (0)