Skip to content

Commit 6b82299

Browse files
diogoviannaaraujomkellner
authored andcommitted
Preliminary version of hardware Serial communication module for esp32 #73
1 parent 2e24a69 commit 6b82299

File tree

7 files changed

+368
-0
lines changed

7 files changed

+368
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2016-2018 Moddable Tech, Inc.
3+
*
4+
* This file is part of the Moddable SDK Runtime.
5+
*
6+
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#include "mc.defines.h"
22+
#include "modSerial.h"
23+
#include "xsesp.h"
24+
25+
#include "driver/uart.h"
26+
27+
static uint8_t modSerialActivate(modSerialConfiguration config);
28+
29+
void modSerialInit(modSerialConfiguration config)
30+
{
31+
modSerialActivate(config);
32+
}
33+
34+
void modSerialUninit(modSerialConfiguration config)
35+
{
36+
uart_driver_delete(UART_NUM_1);
37+
}
38+
39+
uint8_t modSerialRead(modSerialConfiguration config, uint8_t *buffer, uint16_t length)
40+
{
41+
int ret;
42+
43+
ret = uart_read_bytes(UART_NUM_1, buffer, length, 100);
44+
45+
return (ret > 0) ? 0 : 1;
46+
}
47+
48+
uint8_t modSerialWrite(modSerialConfiguration config, const uint8_t *buffer)
49+
{
50+
int ret;
51+
52+
ret = uart_write_bytes(UART_NUM_1, buffer, strlen(buffer));
53+
54+
return (ret > 0) ? 0 : 1;
55+
}
56+
57+
uint8_t modSerialActivate(modSerialConfiguration config)
58+
{
59+
modLog("Activating uart");
60+
uart_config_t conf;
61+
62+
conf.baud_rate = config->baudrate;
63+
conf.data_bits = UART_DATA_8_BITS;
64+
conf.parity = UART_PARITY_DISABLE;
65+
conf.stop_bits = UART_STOP_BITS_1;
66+
conf.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
67+
conf.rx_flow_ctrl_thresh = 122;
68+
69+
if (ESP_OK != uart_param_config(UART_NUM_1, &conf)) {
70+
modLog("uart_param_config fail");
71+
return 1;
72+
}
73+
74+
if (ESP_OK != uart_set_pin(UART_NUM_1, config->tx, config->rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)) {
75+
modLog("uart_set_pin fail");
76+
return 1;
77+
}
78+
79+
if (ESP_OK != uart_driver_install(UART_NUM_1, 1024, 1024, 10, NULL, 0)) {
80+
modLog("uart_driver_install fail");
81+
return 1;
82+
}
83+
84+
return 0;
85+
}
86+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2016-2018 Moddable Tech, Inc.
3+
*
4+
* This file is part of the Moddable SDK Runtime.
5+
*
6+
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#ifndef __MODSERIAL_H__
22+
#define __MODSERIAL_H__
23+
24+
#include "stdint.h"
25+
26+
typedef struct modSerialConfigurationRecord modSerialConfigurationRecord;
27+
typedef struct modSerialConfigurationRecord *modSerialConfiguration;
28+
29+
struct modSerialConfigurationRecord {
30+
uint32_t baudrate;
31+
int16_t rx;
32+
int16_t tx;
33+
};
34+
35+
typedef struct modSerialConfigurationRecord modSerialConfigurationRecord;
36+
typedef struct modSerialConfigurationRecord *modSerialConfiguration;
37+
38+
extern void modSerialInit(modSerialConfiguration config); // required
39+
extern void modSerialUninit(modSerialConfiguration config); // optional
40+
41+
extern uint8_t modSerialRead(modSerialConfiguration config, uint8_t *buffer, uint16_t length);
42+
extern uint8_t modSerialWrite(modSerialConfiguration config, const uint8_t *buffer);
43+
44+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2016-2017 Moddable Tech, Inc.
3+
*
4+
* This file is part of the Moddable SDK.
5+
*
6+
* This work is licensed under the
7+
* Creative Commons Attribution 4.0 International License.
8+
* To view a copy of this license, visit
9+
* <http://creativecommons.org/licenses/by/4.0>.
10+
* or send a letter to Creative Commons, PO Box 1866,
11+
* Mountain View, CA 94042, USA.
12+
*
13+
*/
14+
15+
import Timer from "timer";
16+
import Serial from "pins/serial";
17+
18+
let loopback = new Serial({baudrate: 9600, rx: 33, tx: 32});
19+
20+
const timer = Timer.repeat(id => {
21+
loopback.write("Echo\n");
22+
let ret = loopback.read(40);
23+
trace(String.fromCharCode.apply(String, ret));
24+
}, 1000);
25+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"include": [
3+
"$(MODDABLE)/examples/manifest_base.json",
4+
"$(MODDABLE)/modules/pins/serial/manifest.json"
5+
],
6+
"modules": {
7+
"*": [
8+
"./main",
9+
"$(MODULES)/data/hex/*"
10+
]
11+
}
12+
}
13+

contributed/serial/manifest.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"platforms": {
3+
"esp32": {
4+
"modules": {
5+
"*": "$(MODULES)/pins/serial/esp32/*",
6+
"pins/serial": "$(MODULES)/pins/serial/serial"
7+
},
8+
"preload": "pins/serial"
9+
},
10+
"...": {
11+
"error": "Hardware serial is unsupported"
12+
}
13+
}
14+
}
15+

contributed/serial/serial.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2016-2018 Moddable Tech, Inc.
3+
*
4+
* This file is part of the Moddable SDK Runtime.
5+
*
6+
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#include "xsmc.h"
22+
#ifdef gecko
23+
#include "xsPlatform.h"
24+
#include "xsgecko.h"
25+
#else
26+
#include "xsesp.h"
27+
#endif
28+
#include "mc.xs.h" // for xsID_ values
29+
30+
#include "modSerial.h"
31+
32+
/*
33+
Serial
34+
*/
35+
36+
void xs_serial(xsMachine *the)
37+
{
38+
modSerialConfiguration serial;
39+
int baudrate, rx, tx = 0;
40+
41+
xsmcVars(1);
42+
xsmcGet(xsVar(0), xsArg(0), xsID_baudrate);
43+
baudrate = (xsUndefinedType == xsmcTypeOf(xsVar(0))) ? -1 : xsmcToInteger(xsVar(0));
44+
xsmcGet(xsVar(0), xsArg(0), xsID_rx);
45+
rx = (xsUndefinedType == xsmcTypeOf(xsVar(0))) ? -1 : xsmcToInteger(xsVar(0));
46+
xsmcGet(xsVar(0), xsArg(0), xsID_tx);
47+
tx = xsmcToInteger(xsVar(0));
48+
49+
serial = xsmcSetHostChunk(xsThis, NULL, sizeof(modSerialConfigurationRecord));
50+
serial->baudrate = baudrate;
51+
serial->rx = rx;
52+
serial->tx = tx;
53+
modSerialInit(serial);
54+
}
55+
56+
void xs_serial_destructor(void *data)
57+
{
58+
if (data)
59+
modSerialUninit((modSerialConfiguration)data);
60+
}
61+
62+
void xs_serial_close(xsMachine *the)
63+
{
64+
modSerialConfiguration serial = xsmcGetHostChunk(xsThis);
65+
if (serial) {
66+
modSerialUninit(serial);
67+
xsmcSetHostData(xsThis, NULL); // this clears the host chunk allocated
68+
}
69+
}
70+
71+
void xs_serial_read(xsMachine *the)
72+
{
73+
modSerialConfiguration serial;
74+
int argc = xsmcArgc;
75+
unsigned int len = xsmcToInteger(xsArg(0)), i;
76+
unsigned char err;
77+
unsigned char buffer[40];
78+
79+
if (len > sizeof(buffer))
80+
xsUnknownError("40 byte read limit");
81+
82+
serial = xsmcGetHostChunk(xsThis);
83+
err = modSerialRead(serial, buffer, len);
84+
if (err)
85+
return; // undefined returned on read failure
86+
87+
if (argc >= 2) {
88+
int bufferByteLength;
89+
xsResult = xsArg(1);
90+
bufferByteLength = xsGetArrayBufferLength(xsResult);
91+
if (bufferByteLength < len)
92+
xsUnknownError("buffer too small");
93+
c_memmove(xsmcToArrayBuffer(xsResult), buffer, len);
94+
95+
}
96+
else {
97+
xsResult = xsArrayBuffer(buffer, len);
98+
xsResult = xsNew1(xsGlobal, xsID_Uint8Array, xsResult);
99+
}
100+
}
101+
102+
void xs_serial_write(xsMachine *the)
103+
{
104+
modSerialConfiguration serial;
105+
uint8_t argc = xsmcArgc, i;
106+
unsigned char err;
107+
uint8_t stop = true;
108+
unsigned int len = 0;
109+
unsigned char buffer[40];
110+
111+
xsmcVars(1);
112+
113+
for (i = 0; i < argc; i++) {
114+
xsType t = xsmcTypeOf(xsArg(i));
115+
if ((xsBooleanType == t) && ((argc - 1) == i)) {
116+
stop = xsmcToBoolean(xsArg(i));
117+
continue;
118+
}
119+
if ((xsNumberType == t) || (xsIntegerType == t)) {
120+
if ((len + 1) > sizeof(buffer))
121+
xsUnknownError("40 byte write limit");
122+
buffer[len++] = (unsigned char)xsmcToInteger(xsArg(i));
123+
continue;
124+
}
125+
if (xsStringType == t) {
126+
char *s = xsmcToString(xsArg(i));
127+
int l = c_strlen(s);
128+
if ((len + l) > sizeof(buffer))
129+
xsUnknownError("40 byte write limit");
130+
c_memcpy(buffer + len, s, l);
131+
len += l;
132+
continue;
133+
}
134+
135+
{ // assume some kind of array (Array, Uint8Array, etc) (@@ use .buffer if present)
136+
int l;
137+
uint8_t j;
138+
139+
xsmcGet(xsVar(0), xsArg(i), xsID_length);
140+
l = xsmcToInteger(xsVar(0));
141+
if ((len + l) > sizeof(buffer))
142+
xsUnknownError("40 byte write limit");
143+
for (j = 0; j < l; j++) {
144+
xsmcGet(xsVar(0), xsArg(i), j);
145+
buffer[len++] = xsmcToInteger(xsVar(0));
146+
}
147+
}
148+
}
149+
150+
serial = xsmcGetHostChunk(xsThis);
151+
err = modSerialWrite(serial, buffer);
152+
if (err)
153+
xsUnknownError("write failed");
154+
}
155+

contributed/serial/serial.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016-2017 Moddable Tech, Inc.
3+
*
4+
* This file is part of the Moddable SDK Runtime.
5+
*
6+
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
export class Serial @ "xs_serial_destructor" {
22+
constructor(dictionary) @ "xs_serial";
23+
close() @ "xs_serial_close";
24+
read(count) @ "xs_serial_read";
25+
write() @ "xs_serial_write";
26+
}
27+
Object.freeze(Serial.prototype);
28+
29+
export default Serial;
30+

0 commit comments

Comments
 (0)