Skip to content

Commit 6aa9e99

Browse files
committed
Merge tag '0.55.0' into merge-0.55.0
Conflicts: .github/workflows/build.yml .github/workflows/checkpatch.yml .github/workflows/format_check.yml .github/workflows/license_check.yml .gitignore CMakeLists.txt Kconfig README.md cores/arduino/Arduino.h cores/arduino/main.cpp cores/arduino/zephyrPrint.cpp cores/arduino/zephyrPrint.h cores/arduino/zephyrSerial.cpp cores/arduino/zephyrSerial.h documentation/variants.md libraries/SPI/SPI.cpp libraries/SPI/SPI.h libraries/Wire/Wire.cpp libraries/Wire/Wire.h samples/analog_input/CMakeLists.txt samples/attach_interrupt/CMakeLists.txt samples/blinky_arduino/CMakeLists.txt samples/button_press_led/CMakeLists.txt samples/fade/CMakeLists.txt samples/hello_arduino/CMakeLists.txt samples/i2cdemo/CMakeLists.txt samples/serial_event/CMakeLists.txt samples/spi_controller/CMakeLists.txt samples/threads_arduino/CMakeLists.txt variants/default/variant.h west.yml
2 parents 23a3d96 + 6544208 commit 6aa9e99

166 files changed

Lines changed: 16458 additions & 19 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
rust/Cargo.lock
1+
/build/
2+
/distrib/
3+
/firmwares/*
4+
/rust/Cargo.lock
5+
/venv/
6+
llext-edk/
7+
cflags.txt
8+
cxxflags.txt
9+
includes.txt
10+
syms-dynamic.ld
11+
syms-static.ld

README.gsoc.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# GSoC 2022 Project: Arduino Core API module for Zephyr
2+
3+
![](https://dhruvag2000.github.io/Blog-GSoC22/assets/images/website_header.png)
4+
5+
The **Arduino Core API** module for zephyr leverages the power of Zephyr under an Arduino-C++ style abtraction layer thus helping zephyr new-comers to start using it without worrying about learning about new APIs and libraries. See the project documentation folder for detailed documentation on these topics:
6+
7+
* [Using external Arduino Libraries](/documentation/arduino_libs.md)
8+
* [Adding custom boards/ variants](/documentation/variants.md)
9+
10+
## Adding Arduino Core API to Zephyr
11+
12+
* **Pre requisites:** It is assumed that you have zephyrproject configured and installed on your system as per the official [Get Started Guide](https://docs.zephyrproject.org/latest/develop/getting_started/index.html). The recommended path to install is `~/zephyrproject` as specified in the guide. If you have zephyr installed in a custom path you may need to make changes to the CMakeLists.txt file in the sample code directory when building these samples.
13+
14+
* Add following entry to `west.yml` file in `manifest/projects` subtree of Zephyr:
15+
```
16+
# Arduino API repository.
17+
- name: Arduino-Core-Zephyr
18+
path: modules/lib/Arduino-Zephyr-API
19+
revision: main
20+
url: https://github.com/zephyrproject-rtos/gsoc-2022-arduino-core
21+
```
22+
23+
* Then, clone the repository by running
24+
25+
```sh
26+
west update
27+
```
28+
29+
* **Note:** For ***Linux users only*** there exists an ``install.sh`` script in this project that can be run to quickly link the ArduinoCore-API to this module.
30+
If you are able to run that script successfully then you can skip the next steps.
31+
32+
* To "complete" the core you need to copy or symlink the api folder from the [ArduinoCore-API](https://github.com/arduino/ArduinoCore-API.git) repo to the target's ``cores/arduino`` folder:
33+
```sh
34+
$ git clone git@github.com:arduino/ArduinoCore-API # Any location
35+
$ ln -s /<your>/<location>/ArduinoCore-API/api cores/arduino/.
36+
```
37+
The `cores` folder can be found at `~/zephyrproject/modules/lib/Arduino-Zephyr-API/cores`.
38+
39+
**Known Bug(s):**
40+
41+
__NOTE:__ You can skip this step as well if you ran ``install.sh``.
42+
43+
**Maintainers**:
44+
- [DhruvaG2000](https://github.com/DhruvaG2000)
45+
- [soburi](https://github.com/soburi)
46+
- [szczys](https://github.com/szczys)
47+
- [beriberikix](https://github.com/beriberikix)
48+
- [alvarowolfx](https://github.com/alvarowolfx)
49+
50+
## License
51+
Please note that the current license is Apache 2. Previously it was LGPL 2.1 but after careful review it was determined that no LGPL code or derivates was used and the more permissive license was chosen.
52+
53+
**Additional Links**
54+
* [Official Project Blog](https://dhruvag2000.github.io/Blog-GSoC22/)
55+
* Golioth's Article: [Zephyr + Arduino: a Google Summer of Code story](https://blog.golioth.io/zephyr-arduino-a-google-summer-of-code-story/)

cores/arduino/DebugLog.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
/*
10+
* This header is used to forcibly disable logging and assertions in the
11+
* MsgPack library. It is here to shadow the real DebugLog library available in
12+
* the index, which is currently not compatible with the latest boards.
13+
*/
14+
15+
#define LOG_ERROR(...) ((void)0)
16+
#define LOG_WARN(...) ((void)0)
17+
#define LOG_INFO(...) ((void)0)
18+
#define LOG_DEBUG(...) ((void)0)
19+
#define LOG_TRACE(...) ((void)0)
20+
#define ASSERT(...) ((void)0)
21+
#define ASSERTM(...) ((void)0)
22+
#define PRINT(...) ((void)0)
23+
#define PRINTLN(...) ((void)0)

cores/arduino/SerialUSB.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <zephyrSerial.h>
10+
11+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
12+
#include <zephyr/usb/usbd.h>
13+
extern "C" struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
14+
#endif
15+
16+
namespace arduino {
17+
18+
class SerialUSB_ : public ZephyrSerial {
19+
20+
public:
21+
SerialUSB_(const struct device *dev) : ZephyrSerial(dev) {
22+
}
23+
24+
void begin(unsigned long baudrate, uint16_t config);
25+
26+
void begin(unsigned long baudrate) {
27+
begin(baudrate, SERIAL_8N1);
28+
}
29+
30+
operator bool() override;
31+
size_t write(const uint8_t *buffer, size_t size) override;
32+
33+
size_t write(const uint8_t data) override {
34+
return write(&data, 1);
35+
}
36+
37+
void flush() override;
38+
39+
protected:
40+
uint32_t dtr = 0;
41+
uint32_t baudrate;
42+
static void baudChangeHandler(const struct device *dev, uint32_t rate);
43+
44+
void _reinit_if_needed() override {
45+
/* prevent reinit: USB device is always available */
46+
}
47+
48+
private:
49+
bool started = false;
50+
51+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
52+
struct usbd_context *_usbd;
53+
int enable_usb_device_next();
54+
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
55+
static int usb_disable();
56+
#endif
57+
};
58+
} // namespace arduino
59+
60+
#if ZARD_FIRST_SERIAL_IS_SERIALUSB
61+
extern arduino::SerialUSB_ Serial;
62+
#endif

cores/arduino/USB.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/devicetree.h>
8+
#include <zephyr/drivers/uart.h>
9+
#include <zephyr/drivers/uart/cdc_acm.h>
10+
#include <zephyr/usb/usb_device.h>
11+
#include <SerialUSB.h>
12+
13+
#if ZARD_FIRST_SERIAL_IS_SERIALUSB
14+
const struct device *const usb_dev =
15+
DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm_serial, 0));
16+
17+
void __attribute__((weak)) _on_1200_bps() {
18+
NVIC_SystemReset();
19+
}
20+
21+
void arduino::SerialUSB_::baudChangeHandler(const struct device *dev, uint32_t rate) {
22+
(void)dev; // unused
23+
if (rate == 1200) {
24+
usb_disable();
25+
_on_1200_bps();
26+
}
27+
}
28+
29+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
30+
int arduino::SerialUSB_::usb_disable() {
31+
// To avoid Cannot perform port reset: 1200-bps touch: setting DTR to OFF: protocol error
32+
k_sleep(K_MSEC(100));
33+
return usbd_disable(Serial._usbd);
34+
}
35+
36+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
37+
if (usbd_can_detect_vbus(ctx)) {
38+
if (msg->type == USBD_MSG_VBUS_READY) {
39+
usbd_enable(ctx);
40+
}
41+
42+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
43+
usbd_disable(ctx);
44+
}
45+
}
46+
47+
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
48+
uint32_t baudrate;
49+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
50+
Serial.baudChangeHandler(nullptr, baudrate);
51+
}
52+
}
53+
54+
int arduino::SerialUSB_::enable_usb_device_next(void) {
55+
int err;
56+
57+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
58+
if (_usbd == NULL) {
59+
return -ENODEV;
60+
}
61+
62+
if (!usbd_can_detect_vbus(_usbd)) {
63+
err = usbd_enable(_usbd);
64+
if (err) {
65+
return err;
66+
}
67+
}
68+
return 0;
69+
}
70+
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
71+
72+
void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
73+
if (!started) {
74+
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
75+
usb_enable(NULL);
76+
#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT
77+
#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
78+
#else
79+
cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::baudChangeHandler);
80+
#endif
81+
#else
82+
enable_usb_device_next();
83+
#endif
84+
ZephyrSerial::begin(baudrate, config);
85+
started = true;
86+
}
87+
}
88+
89+
arduino::SerialUSB_::operator bool() {
90+
uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
91+
return dtr;
92+
}
93+
94+
size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) {
95+
if (!Serial) {
96+
return 0;
97+
}
98+
return arduino::ZephyrSerial::write(buffer, size);
99+
}
100+
101+
void arduino::SerialUSB_::flush() {
102+
if (!Serial) {
103+
return;
104+
}
105+
arduino::ZephyrSerial::flush();
106+
}
107+
108+
arduino::SerialUSB_ Serial(usb_dev);
109+
#endif

cores/arduino/abi.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdlib.h>
8+
9+
extern "C" {
10+
11+
void *__dso_handle = (void *)&__dso_handle;
12+
13+
void __cxa_pure_virtual(void) {
14+
}
15+
16+
void __cxa_deleted_virtual(void) {
17+
}
18+
19+
int __cxa_atexit(void (*func)(void *), void *arg, void *dso_handle) {
20+
(void)func;
21+
(void)arg;
22+
(void)dso_handle; // unused
23+
return 0;
24+
}
25+
26+
} /* extern "C" */

0 commit comments

Comments
 (0)