Skip to content

Commit f48fed0

Browse files
committed
Teensy: Add I2C
1 parent 2e58092 commit f48fed0

File tree

8 files changed

+343
-6
lines changed

8 files changed

+343
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import teensy
2+
3+
const ADDRESS = 0x23
4+
5+
Print "Open I2C"
6+
const I2C = teensy.OpenI2C(1) ' use I2C pins 16 and 17
7+
Print "Opened"
8+
9+
' Power down
10+
I2C.Write(ADDRESS, 0x00)
11+
' Power on
12+
I2C.Write(ADDRESS, 0x01)
13+
delay(500)
14+
15+
' Send "Continuously H-resolution mode" instruction
16+
I2C.write(ADDRESS, 0b00010000)
17+
delay(200)
18+
19+
while(1)
20+
' Read H-resolution measurement result
21+
d = I2C.read(ADDRESS, 2)
22+
ValueHighRes = ((d[0] lshift 8) BOR d[1]) / 1.2
23+
24+
ii++
25+
print ii, "High resolution: " + valueHighRes + " lx"
26+
27+
delay(500)
28+
wend
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import teensy
2+
3+
const ADDRESS = 0x77
4+
const Oversampling = 0
5+
6+
I2C = teensy.OpenI2C(1) ' use I2C pins 16 and 17
7+
I2C.setClock(100000)
8+
Init()
9+
10+
while(1)
11+
ii++
12+
m = StartMeasurement()
13+
print ii, m[0]; "°C", m[1]/100; "hPa"
14+
delay(500)
15+
wend
16+
17+
sub Init()
18+
local buffer
19+
' Get chip id
20+
I2C.write(ADDRESS, 0xD0)
21+
buffer = I2C.read(ADDRESS, 1)
22+
if(buffer != 85)
23+
print "Error Chip ID does not match"
24+
while(1)
25+
wend
26+
endif
27+
28+
' Read calibration values
29+
ac1 = short(Read_2Bytes(0xAA))
30+
ac2 = short(Read_2Bytes(0xAC))
31+
ac3 = short(Read_2Bytes(0xAE))
32+
ac4 = Read_2Bytes(0xB0)
33+
ac5 = Read_2Bytes(0xB2)
34+
ac6 = Read_2Bytes(0xB4)
35+
b1 = short(Read_2Bytes(0xB6))
36+
b2 = short(Read_2Bytes(0xB8))
37+
mb = short(Read_2Bytes(0xBA))
38+
mc = short(Read_2Bytes(0xBC))
39+
md = short(Read_2Bytes(0xBE))
40+
end
41+
42+
func short(dat)
43+
if dat > 32767 then
44+
return dat - 65536
45+
else
46+
return dat
47+
endif
48+
end
49+
50+
func StartMeasurement()
51+
52+
local buffer, UncompTemp, Temperature, UncompPres, Pressure
53+
local x1, x2, x3, b3, b5, b6, b4, b7
54+
55+
' Read uncompensated temperature
56+
buffer = [0xF4, 0x2E]
57+
I2C.write(ADDRESS, buffer)
58+
delay(5)
59+
buffer[0] = Read_Byte(0xF6)
60+
buffer[1] = Read_Byte(0xF7)
61+
UncompTemp = (buffer[0] lshift 8) BOR buffer[1]
62+
63+
' Read uncompensated pressure
64+
buffer[0] = 0xF4
65+
buffer[1] = 0x34 + (Oversampling lshift 6)
66+
I2C.write(ADDRESS, buffer)
67+
delay(2 + (3 lshift Oversampling))
68+
I2C.write(ADDRESS, 0xF6)
69+
buffer = I2C.read(ADDRESS, 3)
70+
UncompPres = ((buffer[0] lshift 16) + (buffer[1] lshift 8) + buffer[0]) rshift (8 - Oversampling)
71+
72+
' Calculate true temperature
73+
x1 = ((UncompTemp - ac6) * ac5) rshift 15
74+
x2 = (mc lshift 11) / (x1 + md)
75+
b5 = x1 + x2
76+
Temperature = ((b5 + 8) rshift 4) / 10.0
77+
78+
' Calculate true pressure
79+
b6 = b5 - 4000
80+
x1 = (b2 * (b6 * b6) rshift 12) rshift 11
81+
x2 = (ac2 * b6) rshift 11
82+
x3 = x1 + x2
83+
b3 = (((ac1 * 4 + x3) lshift Oversampling) + 2) rshift 2
84+
x1 = (ac3 * b6) rshift 13
85+
x2 = (b1 * ((b6 * b6) rshift 12)) rshift 16
86+
x3 = ((x1 + x2) + 2) rshift 2
87+
b4 = (ac4 * (x3 + 32768)) rshift 15
88+
b7 = ((UncompPres - b3) * (50000 rshift Oversampling))
89+
if (b7 < 0x80000000)
90+
Pressure = (b7 lshift 1) / b4
91+
else
92+
Pressure = (b7 / b4) lshift 1
93+
endif
94+
x1 = (Pressure rshift 8) * (Pressure rshift 8)
95+
x1 = (x1 * 3038) rshift 16
96+
x2 = (-7357 * Pressure) rshift 16
97+
Pressure = Pressure + ((x1 + x2 + 3791) rshift 4)
98+
99+
return [Temperature, Pressure]
100+
end
101+
102+
func Read_2Bytes(Reg)
103+
local buffer
104+
I2C.write(ADDRESS, Reg)
105+
buffer = I2C.read(ADDRESS, 2)
106+
temp = (buffer[0] lshift 8) BOR buffer[1]
107+
return temp
108+
end
109+
110+
func Read_Byte(Reg)
111+
local buffer
112+
I2C.write(ADDRESS, Reg)
113+
buffer = I2C.read(ADDRESS, 1)
114+
return buffer
115+
end
116+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import teensy
2+
3+
const BuiltInLED = teensy.openDigitalOutput(13)
4+
5+
while(1)
6+
ii++
7+
print ii
8+
BuiltInLED.write(value)
9+
delay(100)
10+
value = !value
11+
wend

src/platform/teensy/src/device.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
#include "lib/maapi.h"
1818
#include "serial.h"
1919

20-
#define SERIAL_BAUD_RATE 1000000
21-
2220
//
2321
// setup the Serial device
22+
// Teensy 4.x uses USB serial. Setup is done at startup. Communication
23+
// is always at full USB speed. Either 12 or 480 Mbit/s.
2424
//
2525
void serial_init() {
26-
Serial.begin(SERIAL_BAUD_RATE);
27-
while (!Serial) {
28-
// wait
29-
}
3026
if (CrashReport) {
27+
while(!Serial) {
28+
delay(10);
29+
}
3130
Serial.print(CrashReport);
3231
}
3332
}

src/platform/teensy/src/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ void serial_read() {
9797
bool eof = false;
9898
int lastRead = -1;
9999

100+
while(!Serial){
101+
delay(10);
102+
}
103+
100104
dev_print("Waiting for data... ");
105+
101106
buffer.clear();
102107

103108
while (!eof) {

src/platform/teensy/src/module.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,30 @@ const char *get_param_str(int argc, slib_par_t *params, int n, const char *def)
7373
}
7474
return result;
7575
}
76+
77+
int get_int(var_t *v) {
78+
int result;
79+
switch (v ? v->type : -1) {
80+
case V_INT:
81+
result = v->v.i;
82+
break;
83+
case V_NUM:
84+
result = v->v.n;
85+
break;
86+
default:
87+
result = 0;
88+
break;
89+
}
90+
return result;
91+
}
92+
93+
int get_array_elem_int(var_p_t array, int index) {
94+
int result;
95+
int size = v_asize(array);
96+
if (index >= 0 && index < size) {
97+
result = get_int(v_elem(array, index));
98+
} else {
99+
result = 0;
100+
}
101+
return result;
102+
}

src/platform/teensy/src/module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ void error(var_p_t var, const char *field, int n);
1414
void error(var_p_t var, const char *text);
1515
int get_param_int(int argc, slib_par_t *params, int n, int def);
1616
const char *get_param_str(int argc, slib_par_t *params, int n, const char *def);
17+
int get_int(var_t *v);
18+
int get_array_elem_int(var_p_t array, int index);
1719

1820
typedef struct {
1921
const char *name;

0 commit comments

Comments
 (0)