Skip to content

Commit 472bfba

Browse files
committed
i2c support: Add sample
* Adds a very basic examples to demonstrate that i2c works using Wire library. * This sample is a demo over ADXL345 sensor and prints x,y,z values to the console over TX pin of the arduino. * Sample has been built and tested and we should get an output similar to below: ``` *** Booting Zephyr OS build zephyr-v3.1.0-3162-gbe1380cc51bd *** Setup begins Setup COMPLETE X = -0.012000 Y = -0.056000 Z = 0.876000 X = -0.008000 Y = -0.060000 Z = 0.876000 ``` * Changing the orientation of the sensor by tilting it right, left up and down we get corresponding changes in the X, Y and Z values. * Errors are printed incase there are improper connections as below: ``` ERR: i2c burst read fails ``` * Currently this sample is a very very basic demo which yet doesn't demonstrate how this project can leverage another arduino IDE - specific library, but it's a start, a promising one! * In future, this sample maybe replaced with a proper arduino library based example. Signed-off-by: Dhruva Gole <[email protected]>
1 parent 300921a commit 472bfba

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

samples/i2cdemo/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
set(DTC_OVERLAY_FILE $ENV{HOME}/zephyrproject/modules/lib/Arduino-Zephyr-API/variants/ARDUINO_NANO33BLE/arduino_nano_33_ble.overlay)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(blinky)
8+
9+
target_sources(app PRIVATE src/main.cpp)
10+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/i2cdemo/README.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _blinky-sample:
2+
3+
Blinky
4+
######
5+
6+
Overview
7+
********
8+
9+
This Arduino Blinky sample blinks an LED forever using the `ArduinoAPI`.
10+
11+
Requirements
12+
************
13+
14+
Your board must:
15+
16+
#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
17+
Zephyr's :ref:`boards`).
18+
#. Have the LED configured using the ``led0`` devicetree alias.
19+
20+
Building and Running
21+
********************
22+
23+
Build and flash Blinky as follows,
24+
25+
```sh
26+
$> west build -p -b arduino_nano_33_ble samples/basic/arduino-blinky/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
27+
28+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
29+
```
30+
31+
After flashing, the LED starts to blink. If a runtime error occurs, the sample
32+
exits without printing to the console.
33+
34+
Adding board support
35+
********************
36+
37+
To add support for your board, add something like this to your devicetree:
38+
39+
.. code-block:: DTS
40+
41+
/ {
42+
aliases {
43+
led0 = &myled0;
44+
};
45+
46+
leds {
47+
compatible = "gpio-leds";
48+
myled0: led_0 {
49+
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
50+
};
51+
};
52+
};
53+
54+
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
55+
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
56+
the pin is set to its high state, and off when the pin is in its low state.
57+
58+
Tips:
59+
60+
- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
61+
in devicetree.
62+
63+
- If you're not sure what to do, check the devicetrees for supported boards which
64+
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
65+
66+
- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
67+
in devicetree.
68+
69+
- If the LED is built in to your board hardware, the alias should be defined in
70+
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
71+
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.

samples/i2cdemo/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y
4+
CONFIG_I2C=y
5+
CONFIG_NEWLIB_LIBC=y
6+
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
7+
CONFIG_RING_BUFFER=y

samples/i2cdemo/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sample:
2+
name: Blinky Sample
3+
tests:
4+
sample.basic.blinky:
5+
tags: LED gpio
6+
filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
7+
depends_on: gpio
8+
harness: led
9+
integration_platforms:
10+
- frdm_k64f

samples/i2cdemo/src/main.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* Blink inbuilt LED example */
2+
3+
#include <Arduino.h>
4+
#include "Wire.h"
5+
6+
void setup()
7+
{
8+
printf("\n\nSetup begins\n");
9+
Wire.begin();
10+
// initialize the LED pin as an output:
11+
12+
// initialize the pushbutton pin as an input:
13+
// pinMode(buttonPin, INPUT);
14+
// pinMode(ledPin, OUTPUT);
15+
Wire.beginTransmission(0x53);
16+
Wire.write(0x2C);
17+
Wire.write(0x08);
18+
Wire.endTransmission();
19+
20+
Wire.beginTransmission(0x53);
21+
Wire.write(0x31);
22+
Wire.write(0x08);
23+
Wire.endTransmission();
24+
25+
Wire.beginTransmission(0x53);
26+
Wire.write(0x2D);
27+
Wire.write(0x08);
28+
Wire.endTransmission();
29+
printf("\n\nSetup COMPLETE\n\n\n");
30+
}
31+
32+
void loop()
33+
{
34+
Wire.beginTransmission(0x53);
35+
Wire.write(0x32);
36+
Wire.endTransmission();
37+
// printf("\n\nrequesting from 53\n\n\n");
38+
Wire.requestFrom(0x53, 1);
39+
byte x0 = Wire.read();
40+
41+
Wire.beginTransmission(0x53);
42+
Wire.write(0x33);
43+
Wire.endTransmission();
44+
Wire.requestFrom(0x53, 1);
45+
byte x1 = Wire.read();
46+
x1 = x1 & 0x03;
47+
48+
uint16_t x = (x1 << 8) + x0;
49+
int16_t xf = x;
50+
if(xf > 511)
51+
{
52+
xf = xf - 1024;
53+
}
54+
float xa = xf * 0.004;
55+
printf("\n\nX = %f\n",xa);
56+
// Serial.print("X = ");
57+
// Serial.print(xa);
58+
// Serial.print(" g");
59+
// Serial.println();
60+
61+
62+
Wire.beginTransmission(0x53);
63+
Wire.write(0x34);
64+
Wire.endTransmission();
65+
Wire.requestFrom(0x53, 1);
66+
byte y0 = Wire.read();
67+
68+
Wire.beginTransmission(0x53);
69+
Wire.write(0x35);
70+
Wire.endTransmission();
71+
Wire.requestFrom(0x53, 1);
72+
byte y1 = Wire.read();
73+
y1 = y1 & 0x03;
74+
75+
uint16_t y = (y1 << 8) + y0;
76+
int16_t yf = y;
77+
if(yf > 511)
78+
{
79+
yf = yf - 1024;
80+
}
81+
float ya = yf * 0.004;
82+
// printk("Y = %f\n",ya);
83+
printf("Y = %f\n",ya);
84+
// printf("\n\nYa = %f\n\n\n",ya);
85+
// Serial.print("Y = ");
86+
// Serial.print(ya);
87+
// Serial.print(" g");
88+
// Serial.println();
89+
90+
Wire.beginTransmission(0x53);
91+
Wire.write(0x36);
92+
Wire.endTransmission();
93+
Wire.requestFrom(0x53, 1);
94+
byte z0 = Wire.read();
95+
96+
Wire.beginTransmission(0x53);
97+
Wire.write(0x37);
98+
Wire.endTransmission();
99+
Wire.requestFrom(0x53, 1);
100+
byte z1 = Wire.read();
101+
z1 = z1 & 0x03;
102+
103+
uint16_t z = (z1 << 8) + z0;
104+
int16_t zf = z;
105+
if(zf > 511)
106+
{
107+
zf = zf - 1024;
108+
}
109+
float za = zf * 0.004;
110+
printf("Z = %f\n\n",za);
111+
// Serial.print("Z = ");
112+
// Serial.print(za);
113+
// Serial.print(" g");
114+
// Serial.println();
115+
// Serial.println();
116+
delay(500);
117+
118+
}

0 commit comments

Comments
 (0)