-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathix03.ts
325 lines (275 loc) · 7 KB
/
ix03.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
namespace ix03
{
/*uint8_t i2cwrite(uint16_t address, uint8_t reg, uint8_t *data, int len)
{
int i2c_error_status = 0;
#ifdef CODAL_I2C
auto sda = LOOKUP_PIN(SDA);
auto scl = LOOKUP_PIN(SCL);
codal::I2C *i2c = pxt::getI2C(sda, scl);
#endif
uint8_t val[len + 1];
val[0] = reg;
for (uint8_t i = 0; i < len; i++)
{
val[i + 1] = data[i];
}
#ifdef CODAL_I2C
return i2c_error_status = i2c->write((uint16_t)address, (uint8_t *)&val, len + 1, false);
#else
return i2c_error_status = uBit.i2c.write(address, (BUFFER_TYPE)val, len + 1, false);
#endif
}
uint8_t i2cread(uint16_t address, uint8_t reg, uint8_t *data, int len)
{
uint8_t val[1] = {reg};
#ifdef CODAL_I2C
auto sda = LOOKUP_PIN(SDA);
auto scl = LOOKUP_PIN(SCL);
codal::I2C *i2c = pxt::getI2C(sda, scl);
#endif
int i2c_error_status = 0;
#ifdef CODAL_I2C
i2c_error_status = i2c->write((uint16_t)address, (uint8_t*)®, 1, true);
#else
i2c_error_status = uBit.i2c.write(address, (BUFFER_TYPE)&val, 1, true);
#endif
#ifdef CODAL_I2C
return i2c_error_status = i2c->read((uint16_t)address, (uint8_t*)&data, len, false);
#else
return i2c_error_status = uBit.i2c.read(address, (BUFFER_TYPE)data, len, false);
#endif
}
void delay(uint16_t time_ms)
{
#ifdef CODAL_I2C
sleep_ms(time_ms);
#else
uBit.sleep(time_ms);
#endif
}*/
/*xIX03::xIX03()
{
i2cAddr = SC16IS740_I2C_ADDR << 1;
}
xIX03::xIX03(uint8_t addr)
{
i2cAddr = addr << 1;
}*/
let i2cAddr: NumberFormat.UInt8LE = 0
let SC16IS740_I2C_ADDR: NumberFormat.UInt8LE = 0x4D
let SC16IS740_RHR = 0x00
let SC16IS740_THR = 0x00
let SC16IS740_IER = 0x01
let SC16IS740_FCR = 0x02
let SC16IS740_IIR = 0x02
let SC16IS740_LCR = 0x03
let SC16IS740_MCR = 0x04
let SC16IS740_LSR = 0x05
let SC16IS740_MSR = 0x06
let SC16IS740_SPR = 0x07
let SC16IS740_TCR = 0x06
let SC16IS740_TLR = 0x07
let SC16IS740_TXLVL = 0x08
let SC16IS740_RXLVL = 0x09
let SC16IS740_IODIR = 0x0A
let SC16IS740_IOSTATE = 0x0B
let SC16IS740_IOINTENA = 0x0C
let SC16IS740_RESERVED = 0x0D
let SC16IS740_IOCONTROL = 0x0E
let SC16IS740_EFCR = 0x0F
let SC16IS740_DLL = 0x00
let SC16IS740_DLH = 0x01
let SC16IS740_EFR = 0x02
let SC16IS740_XON1 = 0x04
let SC16IS740_XON2 = 0x05
let SC16IS740_XOFF1 = 0x06
let SC16IS740_XOFF2 = 0x07
let DEC = 1
let OUTPUT = 1
let HIGH = 1
export function begin(baudRate: NumberFormat.UInt16LE): boolean
{
i2cAddr = SC16IS740_I2C_ADDR << 1
resetDevice();
FIFOEnable(1);
setBaudRate(baudRate);
config();
return true;
}
export function config()
{
writeByte(SC16IS740_LCR, 0x03); // SERIAL_8N1
}
export function setBaudRate(baudRate: NumberFormat.UInt16LE)
{
let old_data: NumberFormat.UInt8LE = 0
let new_data: NumberFormat.UInt8LE = 0
let divisor: number = ((1.8432 * 1000000) / (baudRate * 16))
//uint8_t baud_low = (uint8_t)(divisor);
let baud_low: NumberFormat.UInt8LE = divisor
//uint8_t baud_hi = (uint8_t)(divisor >> 8);
let baud_hi: NumberFormat.UInt8LE = divisor >> 8
//old_data = readByte(SC16IS740_LCR);
old_data = readByte(SC16IS740_LCR)
new_data |= 0x80;
writeByte(SC16IS740_LCR, new_data);
writeByte(SC16IS740_DLL, baud_low);
writeByte(SC16IS740_DLH, baud_hi);
writeByte(SC16IS740_LCR, 0x7F);
}
export function resetDevice()
{
let reg: NumberFormat.UInt8LE;
reg = readByte(SC16IS740_IOCONTROL);
reg |= 0x08;
writeByte(SC16IS740_IOCONTROL, reg);
}
/*bool xIX03::ping()
{
writeByte(SC16IS740_SPR, 0x99);
if (readByte(SC16IS740_SPR) != 0x99)
return false;
return true;
}*/
export function FIFOEnable(fifo_enable: NumberFormat.UInt8LE)
{
let temp_fcr: NumberFormat.UInt8LE = readByte(SC16IS740_FCR);
if (fifo_enable == 0)
temp_fcr &= ~(1 << 1);
else
temp_fcr |= 1 << 1;
writeByte(SC16IS740_FCR, temp_fcr);
}
export function readLSR(): boolean
{
let tmp_lsr:NumberFormat.UInt8LE = 0;
do
{
tmp_lsr = readByte(SC16IS740_LSR);
} while ((tmp_lsr & 0x20) == 0);
return true;
}
/*
void xIX03::write(uint8_t val)
{
if (readLSR())
writeByte(SC16IS740_THR, val);
}
void xIX03::write(const char *str)
{
if (readLSR())
writeBlock(SC16IS740_THR, (uint8_t *)str, 20);
}
*/
export function write(buffer: Buffer, size: number)
{
if (readLSR())
pins.i2cWriteBuffer(i2cAddr, buffer, false)
}
/*void xIX03::write(const char *buffer, uint8_t size)
{
if (readLSR())
writeBlock(SC16IS740_THR, (uint8_t *)buffer, size);
}
void xIX03::flush()
{
uint8_t reg = readByte(SC16IS740_FCR);
writeByte(SC16IS740_FCR, reg | 0x06);
}*/
export function available(): NumberFormat.UInt8LE
{
return readByte(SC16IS740_RXLVL);
}
/*uint8_t xIX03::availableForWrite()
{
return 64 - readByte(SC16IS740_TXLVL);
}*/
export function read(): NumberFormat.UInt8LE
{
if (available() == 0)
return 0;
return readByte(SC16IS740_RHR);
}
/*
void xIX03::end()
{
}
*/
export function pinMode(pin: NumberFormat.UInt8LE, mode: NumberFormat.UInt8LE)
{
let _reg: NumberFormat.UInt8LE = readByte(SC16IS740_IODIR);
if (mode == OUTPUT)
{
_reg |= 1 << pin;
}
else
{
_reg &= ~(1 << pin);
}
writeByte(SC16IS740_IODIR, _reg);
}
export function digitalWrite(pin: NumberFormat.UInt8LE, state: NumberFormat.UInt8LE)
{
let _reg:NumberFormat.UInt8LE = readByte(SC16IS740_IOSTATE);
if (state == HIGH)
{
_reg |= 1 << pin;
}
else
{
_reg &= ~(1 << pin);
}
writeByte(SC16IS740_IOSTATE, _reg);
}
export function digitalRead(pin: number)
{
let _reg = readByte(SC16IS740_IOSTATE);
return (_reg & pin) >> pin;
}
export function writeByte(reg: number, val: number)
{
//uint8_t k[2] = {reg << 3, val};
let k: Buffer = pins.createBuffer(pins.sizeOf(NumberFormat.UInt8LE) * 2)
k.setNumber(NumberFormat.UInt8LE, 0, reg << 3)
k.setNumber(NumberFormat.UInt8LE, 1, val)
/*#ifdef CODAL_I2C
auto sda = LOOKUP_PIN(SDA);
auto scl = LOOKUP_PIN(SCL);
codal::I2C *i2c = pxt::getI2C(sda, scl);
i2c->write(i2cAddr, (uint8_t *)k, 2, false);
#else
uBit.i2c.write(i2cAddr, (BUFFER_TYPE)k, 2, false);
#endif*/
pins.i2cWriteBuffer(i2cAddr, k, false)
}
export function readByte(reg: NumberFormat.UInt8LE): NumberFormat.UInt8LE
{
//uint8_t val[1] = {reg << 3};
let val: NumberFormat.UInt8LE = (reg << 3)
/*#ifdef CODAL_I2C
uint8_t data[1];
#else
char data[1];
#endif*/
let data: NumberFormat.UInt8LE = 0
/*#ifdef CODAL_I2C
auto sda = LOOKUP_PIN(SDA);
auto scl = LOOKUP_PIN(SCL);
codal::I2C *i2c = pxt::getI2C(sda, scl);
i2c->write(i2cAddr, (uint8_t *)val, 1, true);
i2c->read(i2cAddr, (uint8_t *)&data, 1, false);
#else
uBit.i2c.write(i2cAddr, (BUFFER_TYPE)val, 1, true);
uBit.i2c.read(i2cAddr, (BUFFER_TYPE)data, 1, false);
#endif*/
pins.i2cWriteNumber(i2cAddr, val, NumberFormat.UInt8LE, true)
data = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
return data
}
/*void xIX03::writeBlock(uint8_t reg, uint8_t *val, uint8_t len)
{
i2cwrite(i2cAddr, reg << 3, val, len);
delay(100);
}*/
}