Skip to content

Commit 54f3277

Browse files
committed
Create I2C.ino
1 parent 0ce3c96 commit 54f3277

File tree

1 file changed

+122
-0
lines changed
  • libraries/Apollo3/examples/I2C

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
/*
7+
8+
I2C functionality is accessed via TwoWire objects or their children.
9+
On the Apollo3 the MbedI2C class inherits from the TwoWire class and
10+
provides the actual implemenation based on mbed-os I2C
11+
https://os.mbed.com/docs/mbed-os/v6.2/apis/i2c.html
12+
13+
Apollo3 features 6 IOM peripherals that can each be configured as either
14+
SPI or I2C. Each IOM has only one set of pins that can be used. Check the
15+
board schematic or Apollo3 Blue datasheet to determine which IOM
16+
peripherals are available on your board. There are a few different ways
17+
to access these ports:
18+
- via pre-defined MbedI2C objects like Wire, Wire1 -> Wire5
19+
- declaring your own MbedI2C object using pins that correspond to the correct IOM
20+
21+
Once you have an MbedI2C object to work with you can use all the standard
22+
Arduino SPI API methods on it
23+
https://www.arduino.cc/en/reference/wire
24+
25+
This example will use threads to organize I2C operations based on board
26+
capabilities. The test function will list I2C devices detected on each I2C port
27+
28+
Wiring:
29+
For each applicable I2C port:
30+
SCL <--> sensor SCL
31+
SDA <--> sensor SDA
32+
3V3 <--> sensor 3V3
33+
GND <--> sensor GND
34+
35+
*/
36+
37+
#include "Wire.h"
38+
39+
// This thread will use the pre-defined SPI object if it exists
40+
#if VARIANT_WIRE_INTFCS > 0
41+
rtos::Thread wire_thread;
42+
void wire_thread_fn( void ){
43+
Wire.begin();
44+
while(1){
45+
testPortI2C(Wire);
46+
delay(1000);
47+
}
48+
}
49+
#endif
50+
51+
// This thread will use the pre-defined SPI1 object if it exists
52+
#if VARIANT_WIRE_INTFCS > 1
53+
rtos::Thread wire1_thread;
54+
void wire1_thread_fn( void ){
55+
delay(100);
56+
Wire1.begin();
57+
while(1){
58+
testPortWire(Wire1);
59+
delay(1000);
60+
}
61+
}
62+
#endif
63+
64+
// This thread will create its own MbedI2C object using IOM pins
65+
#define mySDA D25
66+
#define mySCL D27
67+
MbedI2C myWire(mySDA, mySCL);
68+
rtos::Thread mywire_thread;
69+
void mywire_thread_fn( void ){
70+
delay(200);
71+
myWire.begin();
72+
while(1){
73+
testPortI2C(myWire);
74+
delay(1000);
75+
}
76+
}
77+
78+
void testPortI2C(MbedI2C &i2c){
79+
Serial.printf("Scanning... (port: 0x%08X), time (ms): %d\n", (uint32_t)&i2c, millis());
80+
81+
uint8_t detected = 0;
82+
for(uint8_t addr = 1; addr < 127; addr++ ){
83+
// use endTransmission to determine if a device is present at address
84+
i2c.beginTransmission(addr);
85+
uint8_t retval = i2c.endTransmission();
86+
87+
if(retval == 0){
88+
Serial.printf("\t0x%02X detected\n", addr);
89+
detected++;
90+
}
91+
}
92+
93+
if(!detected){
94+
Serial.printf("\tNo device detected!\n");
95+
}
96+
97+
Serial.println();
98+
}
99+
100+
void setup() {
101+
Serial.begin(115200);
102+
Serial.println("Apollo3 - I2C");
103+
104+
pinMode(LED_BUILTIN, OUTPUT);
105+
106+
#if VARIANT_WIRE_INTFCS > 0
107+
wire_thread.start(wire_thread_fn);
108+
#endif
109+
110+
#if VARIANT_WIRE_INTFCS > 1
111+
wire1_thread.start(wire1_thread_fn);
112+
#endif
113+
114+
mywire_thread.start(mywire_thread_fn);
115+
}
116+
117+
void loop() {
118+
digitalWrite(LED_BUILTIN, LOW);
119+
delay(1000);
120+
digitalWrite(LED_BUILTIN, HIGH);
121+
delay(1000);
122+
}

0 commit comments

Comments
 (0)