Skip to content

Commit b531629

Browse files
hs0225yichoi
authored andcommitted
Rework I2C module (#951)
- remove similar API - remove global variable - test coverage IoT.js-DCO-1.0-Signed-off-by: Hosung Kim [email protected]
1 parent 75829d8 commit b531629

File tree

10 files changed

+68
-524
lines changed

10 files changed

+68
-524
lines changed

docs/api/IoT.js-API-I2C.md

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ The following shows I2C module APIs available for each platform.
66
| :---: | :---: | :---: | :---: |
77
| i2c.open | O | O | O |
88
| i2cbus.read | O | O | O |
9-
| i2cbus.readByte | O | O | O |
10-
| i2cbus.readBytes | O | O | O |
119
| i2cbus.write | O | O | O |
12-
| i2cbus.writeByte | O | O | O |
13-
| i2cbus.writeBytes | O | O | O |
1410
| i2cbus.close | O | O | O |
1511

1612

@@ -82,55 +78,6 @@ i2c_bus.read(2, function(err, res) {
8278
});
8379
```
8480

85-
86-
### i2cbus.readByte([callback])
87-
* `callback` {Function}
88-
* `err` {Error|null}
89-
* `res` {number} Result byte.
90-
91-
Read one byte from I2C device.
92-
93-
**Example**
94-
95-
```js
96-
var I2C = require('i2c');
97-
98-
var i2c = new I2C();
99-
var i2c_bus = i2c.open({device: '/dev/i2c-1', address: 0x23});
100-
101-
i2c_bus.readByte(function(err, res) {
102-
if(!err) {
103-
console.log('readByte result: ' + res);
104-
}
105-
});
106-
```
107-
108-
109-
### i2cbus.readBytes(cmd, length[, callback])
110-
* `cmd` {number} Command to the device.
111-
* `length` {number} Number of bytes to read.
112-
* `callback` {Function}
113-
* `err` {Error|null}
114-
* `res` {Array} Array of bytes.
115-
116-
Read bytes from I2C device with command.
117-
118-
**Example**
119-
120-
```js
121-
var I2C = require('i2c');
122-
123-
var i2c = new I2C();
124-
var i2c_bus = i2c.open({device: '/dev/i2c-1', address: 0x23});
125-
126-
i2c_bus.readBytes(0x20, 2, function(err, res) {
127-
if(!err) {
128-
console.log('readBytes result: ' + res);
129-
}
130-
});
131-
```
132-
133-
13481
### i2cbus.write(bytes[, callback])
13582
* `bytes` {Array} Array of bytes to write.
13683
* `callback` {Function}
@@ -154,53 +101,6 @@ i2c_bus.write([0x10], function(err) {
154101
```
155102

156103

157-
### i2cbus.writeByte(byte[, callback])
158-
* `byte` {number} Byte to write.
159-
* `callback` {Function}
160-
* `err` {Error|null}
161-
162-
Write one byte to I2C device.
163-
164-
**Example**
165-
166-
```js
167-
var I2C = require('i2c');
168-
169-
var i2c = new I2C();
170-
var i2c_bus = i2c.open({device: '/dev/i2c-1', address: 0x23});
171-
172-
i2c_bus.writeByte(0x10, function(err) {
173-
if(!err) {
174-
console.log('writeByte done');
175-
}
176-
});
177-
```
178-
179-
180-
### i2cbus.writeBytes(cmd, bytes[, callback])
181-
* `cmd` {number} Command to the device.
182-
* `bytes` {Array} Array of bytes to write.
183-
* `callback` {Function}
184-
* `err` {Error|null}
185-
186-
Write bytes to I2C device with command.
187-
188-
**Example**
189-
190-
```js
191-
var I2C = require('i2c');
192-
193-
var i2c = new I2C();
194-
var i2c_bus = i2c.open({device: '/dev/i2c-1', address: 0x23});
195-
196-
i2c_bus.writeBytes(0x10, [0x10], function(err) {
197-
if(!err) {
198-
console.log('writeBytes done');
199-
}
200-
});
201-
```
202-
203-
204104
### i2cbus.close()
205105

206106
Close I2C device.

docs/targets/nuttx/stm32f4dis/IoT.js-API-Stm32f4dis.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,13 @@ Note: You can add more ports according to the patch file.
165165

166166
```
167167

168+
## I2C Port Information
169+
170+
In order to use the I2C on stm32f4-discovery board, you must use proper pins.
171+
Currently only I2C1 is supported.
172+
173+
The following table shows the I2C pin map:
174+
| I2C Pin Name | GPIO Name |
175+
| :--- | :---: |
176+
| I2C1_SCL | PB8 |
177+
| I2C1_SDA | PB7 |

docs/targets/nuttx/stm32f4dis/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Followings are the options to set:
128128
* For `uart` module
129129
* Enable `System Type -> STM32 Peripheral Support -> U[S]ART(N)`
130130

131+
* For `i2c` module
132+
* Enable `System Type -> STM32 Peripheral Support -> I2C1`
133+
131134
#### 4. Build IoT.js for NuttX
132135

133136
##### Follow the instruction

src/iotjs_magic_strings.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@
130130
#define IOTJS_MAGIC_STRING_PULLDOWN "PULLDOWN"
131131
#define IOTJS_MAGIC_STRING_PULLUP "PULLUP"
132132
#define IOTJS_MAGIC_STRING_PUSHPULL "PUSHPULL"
133-
#define IOTJS_MAGIC_STRING_READBLOCK "readBlock"
134-
#define IOTJS_MAGIC_STRING_READBYTE "readByte"
135133
#define IOTJS_MAGIC_STRING_READDIR "readdir"
136134
#define IOTJS_MAGIC_STRING_READ "read"
137135
#define IOTJS_MAGIC_STRING_READSOURCE "readSource"
@@ -181,8 +179,6 @@
181179
#define IOTJS_MAGIC_STRING_UNREF "unref"
182180
#define IOTJS_MAGIC_STRING_UPGRADE "upgrade"
183181
#define IOTJS_MAGIC_STRING_URL "url"
184-
#define IOTJS_MAGIC_STRING_WRITEBLOCK "writeBlock"
185-
#define IOTJS_MAGIC_STRING_WRITEBYTE "writeByte"
186182
#define IOTJS_MAGIC_STRING_WRITESYNC "writeSync"
187183
#define IOTJS_MAGIC_STRING_WRITEUINT8 "writeUInt8"
188184
#define IOTJS_MAGIC_STRING_WRITE "write"

src/js/i2c.js

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,6 @@ function i2cBusOpen(configurable, callback) {
115115
});
116116
};
117117

118-
I2CBus.prototype.writeByte = function(byte, callback) {
119-
if (!util.isNumber(byte)) {
120-
throw new TypeError('Bad argument - byte: Number');
121-
}
122-
123-
this.setAddress(this.address);
124-
_binding.writeByte(byte, function(err) {
125-
util.isFunction(callback) && callback(err);
126-
});
127-
};
128-
129-
I2CBus.prototype.writeBytes = function(cmd, array, callback) {
130-
if (!util.isNumber(cmd)) {
131-
throw new TypeError('Bad argument - cmd: Number');
132-
}
133-
if (!util.isArray(array)) {
134-
throw new TypeError('Bad argument - array: Array');
135-
}
136-
137-
this.setAddress(this.address);
138-
_binding.writeBlock(cmd, array, function(err) {
139-
util.isFunction(callback) && callback(err);
140-
});
141-
};
142-
143118
I2CBus.prototype.read = function(length, callback) {
144119
if (!util.isNumber(length)) {
145120
throw new TypeError('Bad argument - length: Number');
@@ -151,27 +126,6 @@ function i2cBusOpen(configurable, callback) {
151126
});
152127
};
153128

154-
I2CBus.prototype.readByte = function(callback) {
155-
this.setAddress(this.address);
156-
_binding.readByte(function(err, data) {
157-
util.isFunction(callback) && callback(err, data);
158-
});
159-
};
160-
161-
I2CBus.prototype.readBytes = function(cmd, length, callback) {
162-
if (!util.isNumber(cmd)) {
163-
throw new TypeError('Bad argument - cmd: Number');
164-
}
165-
if (!util.isNumber(length)) {
166-
throw new TypeError('Bad argument - length: Number');
167-
}
168-
169-
this.setAddress(this.address);
170-
_binding.readBlock(cmd, length, 0, function(err, resArray) {
171-
util.isFunction(callback) && callback(err, resArray);
172-
});
173-
};
174-
175129
return new I2CBus(configurable, callback);
176130
}
177131

src/modules/iotjs_module_i2c.c

Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ iotjs_i2c_t* iotjs_i2c_create(const iotjs_jval_t* ji2c) {
9494
iotjs_i2c_t* i2c = IOTJS_ALLOC(iotjs_i2c_t);
9595
IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_i2c_t, i2c);
9696

97-
#if defined(__NUTTX__)
97+
#if defined(__linux__)
98+
_this->device_fd = -1;
99+
#elif defined(__NUTTX__)
98100
_this->i2c_master = NULL;
99101
#endif
100102

@@ -139,9 +141,7 @@ void AfterI2CWork(uv_work_t* work_req, int status) {
139141
}
140142
break;
141143
}
142-
case kI2cOpWrite:
143-
case kI2cOpWriteByte:
144-
case kI2cOpWriteBlock: {
144+
case kI2cOpWrite: {
145145
if (req_data->error == kI2cErrWrite) {
146146
iotjs_jval_t error =
147147
iotjs_jval_create_error("Cannot write to device");
@@ -152,20 +152,13 @@ void AfterI2CWork(uv_work_t* work_req, int status) {
152152
}
153153
break;
154154
}
155-
case kI2cOpRead:
156-
case kI2cOpReadBlock: {
155+
case kI2cOpRead: {
157156
if (req_data->error == kI2cErrRead) {
158157
iotjs_jval_t error =
159158
iotjs_jval_create_error("Cannot read from device");
160159
iotjs_jargs_append_jval(&jargs, &error);
161160
iotjs_jargs_append_null(&jargs);
162161
iotjs_jval_destroy(&error);
163-
} else if (req_data->error == kI2cErrReadBlock) {
164-
iotjs_jval_t error =
165-
iotjs_jval_create_error("Error reading length of bytes");
166-
iotjs_jargs_append_jval(&jargs, &error);
167-
iotjs_jargs_append_null(&jargs);
168-
iotjs_jval_destroy(&error);
169162
} else {
170163
iotjs_jargs_append_null(&jargs);
171164
iotjs_jval_t result =
@@ -184,19 +177,6 @@ void AfterI2CWork(uv_work_t* work_req, int status) {
184177
}
185178
break;
186179
}
187-
case kI2cOpReadByte: {
188-
if (req_data->error == kI2cErrRead) {
189-
iotjs_jval_t error =
190-
iotjs_jval_create_error("Cannot read from device");
191-
iotjs_jargs_append_jval(&jargs, &error);
192-
iotjs_jargs_append_null(&jargs);
193-
iotjs_jval_destroy(&error);
194-
} else {
195-
iotjs_jargs_append_null(&jargs);
196-
iotjs_jargs_append_number(&jargs, req_data->byte);
197-
}
198-
break;
199-
}
200180
default: {
201181
IOTJS_ASSERT(!"Unreachable");
202182
break;
@@ -315,46 +295,6 @@ JHANDLER_FUNCTION(Write) {
315295
}
316296

317297

318-
JHANDLER_FUNCTION(WriteByte) {
319-
DJHANDLER_CHECK_THIS(object);
320-
DJHANDLER_CHECK_ARGS(2, number, function);
321-
322-
uint8_t byte = JHANDLER_GET_ARG(0, number);
323-
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG(1, function);
324-
const iotjs_jval_t* ji2c = JHANDLER_GET_THIS(object);
325-
326-
iotjs_i2c_reqwrap_t* req_wrap =
327-
iotjs_i2c_reqwrap_create(jcallback, ji2c, kI2cOpWriteByte);
328-
329-
iotjs_i2c_reqdata_t* req_data = iotjs_i2c_reqwrap_data(req_wrap);
330-
req_data->byte = byte;
331-
332-
I2C_ASYNC(WriteByte);
333-
334-
iotjs_jhandler_return_null(jhandler);
335-
}
336-
337-
338-
JHANDLER_FUNCTION(WriteBlock) {
339-
DJHANDLER_CHECK_THIS(object);
340-
DJHANDLER_CHECK_ARGS(3, number, array, function);
341-
342-
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG(2, function);
343-
const iotjs_jval_t* ji2c = JHANDLER_GET_THIS(object);
344-
345-
iotjs_i2c_reqwrap_t* req_wrap =
346-
iotjs_i2c_reqwrap_create(jcallback, ji2c, kI2cOpWriteBlock);
347-
348-
iotjs_i2c_reqdata_t* req_data = iotjs_i2c_reqwrap_data(req_wrap);
349-
req_data->cmd = JHANDLER_GET_ARG(0, number);
350-
GetI2cArray(JHANDLER_GET_ARG(1, array), req_data);
351-
352-
I2C_ASYNC(WriteBlock);
353-
354-
iotjs_jhandler_return_null(jhandler);
355-
}
356-
357-
358298
JHANDLER_FUNCTION(Read) {
359299
DJHANDLER_CHECK_THIS(object);
360300
DJHANDLER_CHECK_ARGS(2, number, function);
@@ -375,43 +315,6 @@ JHANDLER_FUNCTION(Read) {
375315
}
376316

377317

378-
JHANDLER_FUNCTION(ReadByte) {
379-
DJHANDLER_CHECK_THIS(object);
380-
DJHANDLER_CHECK_ARGS(1, function);
381-
382-
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG(0, function);
383-
const iotjs_jval_t* ji2c = JHANDLER_GET_THIS(object);
384-
385-
iotjs_i2c_reqwrap_t* req_wrap =
386-
iotjs_i2c_reqwrap_create(jcallback, ji2c, kI2cOpReadByte);
387-
388-
I2C_ASYNC(ReadByte);
389-
390-
iotjs_jhandler_return_null(jhandler);
391-
}
392-
393-
394-
JHANDLER_FUNCTION(ReadBlock) {
395-
DJHANDLER_CHECK_THIS(object);
396-
DJHANDLER_CHECK_ARGS(4, number, number, number, function);
397-
398-
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG(3, function);
399-
const iotjs_jval_t* ji2c = JHANDLER_GET_THIS(object);
400-
401-
iotjs_i2c_reqwrap_t* req_wrap =
402-
iotjs_i2c_reqwrap_create(jcallback, ji2c, kI2cOpReadBlock);
403-
404-
iotjs_i2c_reqdata_t* req_data = iotjs_i2c_reqwrap_data(req_wrap);
405-
req_data->cmd = JHANDLER_GET_ARG(0, number);
406-
req_data->buf_len = JHANDLER_GET_ARG(1, number);
407-
req_data->delay = JHANDLER_GET_ARG(2, number);
408-
409-
I2C_ASYNC(ReadBlock);
410-
411-
iotjs_jhandler_return_null(jhandler);
412-
}
413-
414-
415318
iotjs_jval_t InitI2c() {
416319
iotjs_jval_t jI2cCons = iotjs_jval_create_function_with_dispatch(I2cCons);
417320

@@ -420,11 +323,7 @@ iotjs_jval_t InitI2c() {
420323
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_SETADDRESS, SetAddress);
421324
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_CLOSE, Close);
422325
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_WRITE, Write);
423-
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_WRITEBYTE, WriteByte);
424-
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_WRITEBLOCK, WriteBlock);
425326
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_READ, Read);
426-
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_READBYTE, ReadByte);
427-
iotjs_jval_set_method(&prototype, IOTJS_MAGIC_STRING_READBLOCK, ReadBlock);
428327

429328
iotjs_jval_set_property_jval(&jI2cCons, IOTJS_MAGIC_STRING_PROTOTYPE,
430329
&prototype);

0 commit comments

Comments
 (0)