├── hello.zig ├── README.md ├── main.cpp ├── lxd-arduino.sh ├── qemu.Dockerfile ├── lxd-platformio.sh ├── nuttx.Dockerfile ├── lxd-nuttx.sh └── LICENSE /hello.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub extern fn printf( 4 | _format: [*:0]const u8 5 | ) c_int; 6 | 7 | pub export fn hello_zig_main( 8 | _argc: c_int, 9 | _argv: [*]const [*]const u8 10 | ) c_int { 11 | _ = _argc; 12 | _ = _argv; 13 | _ = printf("Hello Zig\x0A"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp32 2 | 3 | Learning esp32 embedded code 4 | 5 | 6 | ## Credits 7 | 8 | Zig with NuttX 9 | by [Lup Yuen Lee](https://zig.news/lupyuen/zig-on-risc-v-bl602-quick-peek-with-apache-nuttx-rtos-3apd) [LICENSE](https://github.com/lupyuen/zig-bl602-nuttx/blob/main/LICENSE) 10 | 11 | 12 | [esp32 with NuttX](https://blog.espressif.com/getting-started-with-esp32-and-nuttx-fd3e1a3d182c?gi=523f4b1d78f9) 13 | 14 | [esp32 emulator](https://github.com/espressif/qemu) 15 | 16 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Blink 3 | 4 | Turn LED on for one second, 5 | then off for one second, 6 | repeat. 7 | */ 8 | 9 | #include "Arduino.h" 10 | 11 | #ifndef LED_BUILTIN 12 | #define LED_BUILTIN 13 13 | #endif 14 | 15 | void setup() { 16 | // initialize LED digital pin for output 17 | pinMode(LED_BUILTIN, OUTPUT); 18 | } 19 | 20 | void loop() { 21 | // turn LED on 22 | digitalWrite(LED_BUILTIN, HIGH); 23 | 24 | // wait one second 25 | delay(1000); 26 | 27 | // turn LED off 28 | digitalWrite(LED_BUILTIN, LOW); 29 | 30 | // wait one second 31 | delay(1000); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /lxd-arduino.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | # start at https://documentation.ubuntu.com/lxd/en/latest/tutorial/first_steps 5 | 6 | sudo snap refresh lxd 7 | lxc launch images:debian/12 bkw 8 | lxc exec bkw -- apt-get update 9 | lxc exec bkw -- apt-get install -y --no-install-recommends ca-certificates curl 10 | # note: don't use the install.sh script because it runs outside the container 11 | lxc exec bkw -- curl -sL -o /tmp/arduinocli.tgz https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Linux_ARM64.tar.gz 12 | lxc exec bkw -- tar xzf /tmp/arduinocli.tgz -C /usr/local/bin 13 | lxc exec bkw -- arduino-cli config init 14 | lxc exec bkw -- arduino-cli sketch new MyFirstSketch 15 | #lxc file push MyFirstSketch.ino bkw/root/MyFirstSketch/MyFirstSketch.ino 16 | lxc exec bkw -- arduino-cli core update-index 17 | # manual step: plug in esp32-s3 (TODO ttyACM0 is not available to container) 18 | lxc exec bkw -- arduino-cli board list 19 | 20 | 21 | -------------------------------------------------------------------------------- /qemu.Dockerfile: -------------------------------------------------------------------------------- 1 | 2 | FROM ubuntu:jammy 3 | 4 | RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \ 5 | git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build \ 6 | libgcrypt-dev git-email build-essential \ 7 | libaio-dev libbluetooth-dev libcapstone-dev libbrlapi-dev libbz2-dev \ 8 | libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev \ 9 | libibverbs-dev libjpeg8-dev libncurses5-dev libnuma-dev \ 10 | librbd-dev librdmacm-dev \ 11 | libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh-dev \ 12 | libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev liblzo2-dev \ 13 | valgrind xfslibs-dev 14 | 15 | RUN git clone --depth=1 https://github.com/espressif/qemu 16 | RUN mkdir -p qemu/build && cd qemu/build &&\ 17 | ../configure --target-list=xtensa-softmmu \ 18 | --enable-gcrypt \ 19 | --enable-debug --enable-sanitizers \ 20 | --disable-strip --disable-user \ 21 | --disable-capstone --disable-vnc \ 22 | --disable-sdl --disable-gtk 23 | 24 | RUN cd qemu && ninja -C build 25 | 26 | 27 | -------------------------------------------------------------------------------- /lxd-platformio.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | # start at https://documentation.ubuntu.com/lxd/en/latest/tutorial/first_steps 5 | 6 | sudo snap refresh lxd 7 | lxc launch images:debian/12 bkw 8 | lxc exec bkw -- apt-get update 9 | lxc exec bkw -- apt-get install -y --no-install-recommends ca-certificates curl 10 | lxc exec bkw -- apt-get install -y pipx 11 | lxc exec bkw -- adduser --system --uid 999 --ingroup dialout --shell /bin/bash --home /home/bee bee 12 | lxc exec bkw --user 999 -- pipx install platformio 13 | lxc exec bkw --user 999 -- pipx ensurepath 14 | lxc exec bkw --user 999 --cwd /home/bee -- mkdir blinky 15 | lxc exec bkw --user 999 --cwd /home/bee --env PATH=/home/bee/.local/bin:$PATH -- pio project init -d /home/bee/blinky --board bee_s3 16 | lxc file push main.cpp bkw/home/bee/blinky/src/main.cpp 17 | lxc exec bkw --user 999 --cwd /home/bee --env PATH=/home/bee/.local/bin:$PATH -- pio run -d /home/bee/blinky 18 | 19 | # manual step: plug in esp32-s3 (TODO ttyACM0 is not available to container) 20 | ##lxc exec bkw --user 999 --cwd /home/bee --env PATH=/home/bee/.local/bin:$PATH -- pio run -d /home/bee/blinky -t upload 21 | 22 | 23 | -------------------------------------------------------------------------------- /nuttx.Dockerfile: -------------------------------------------------------------------------------- 1 | 2 | ##FROM ubuntu:jammy 3 | FROM python:3.10.7-bullseye 4 | RUN apt-get update && apt-get install -y --no-install-recommends git ca-certificates curl \ 5 | bison flex gettext texinfo libncurses5-dev libncursesw5-dev \ 6 | gperf automake libtool pkg-config build-essential gperf \ 7 | libgmp-dev libmpc-dev libmpfr-dev libisl-dev binutils-dev libelf-dev \ 8 | libexpat-dev picocom u-boot-tools util-linux \ 9 | kconfig-frontends 10 | 11 | ARG USERNAME=user-name-goes-here 12 | ARG USER_UID=1000 13 | ARG USER_GID=$USER_UID 14 | 15 | # Create the user 16 | RUN groupadd --gid $USER_GID $USERNAME \ 17 | && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ 18 | # 19 | # [Optional] Add sudo support. Omit if you don't need to install software after connecting. 20 | && apt-get update \ 21 | && apt-get install -y sudo \ 22 | && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ 23 | && chmod 0440 /etc/sudoers.d/$USERNAME 24 | 25 | RUN adduser $USERNAME dialout 26 | USER $USERNAME 27 | 28 | ENV PATH "$PATH:/opt/xtensa/bin:/home/${USERNAME}/.local/bin" 29 | ENV WDIR "/home/${USERNAME}/nuttxspace" 30 | #WORKDIR /home/${USERNAME}/nuttxspace 31 | RUN mkdir -p ${WDIR} && cd ${WDIR} 32 | RUN git clone --depth=1 https://github.com/apache/incubator-nuttx.git ${WDIR}/nuttx 33 | RUN git clone --depth=1 https://github.com/apache/incubator-nuttx-apps.git ${WDIR}/apps 34 | RUN curl -sL https://github.com/espressif/crosstool-NG/releases/download/esp-2022r1/xtensa-esp32s3-elf-gcc11_2_0-esp-2022r1-linux-arm64.tar.xz | tar x -Jv -C ${WDIR} -f - 35 | 36 | RUN sudo mv ${WDIR}/xtensa-esp32s3-elf /opt/xtensa ;\ 37 | pip3 install esptool && pip3 install pyserial 38 | RUN mkdir ${WDIR}/esp-bins ;\ 39 | curl -sL https://github.com/espressif/esp-nuttx-bootloader/releases/download/latest/bootloader-esp32.bin -o ${WDIR}/esp-bins/bootloader-esp32.bin ;\ 40 | curl -sL https://github.com/espressif/esp-nuttx-bootloader/releases/download/latest/partition-table-esp32.bin -o ${WDIR}/esp-bins/partition-table-esp32.bin 41 | 42 | # cd ${WDIR}/nuttx && ./tools/configure.sh -L |more 43 | RUN cd ${WDIR}/nuttx && ./tools/configure.sh -l esp32s3-devkit:nsh 44 | # && make 45 | 46 | 47 | -------------------------------------------------------------------------------- /lxd-nuttx.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | # start at https://documentation.ubuntu.com/lxd/en/latest/tutorial/first_steps 5 | 6 | sudo snap refresh lxd 7 | lxc launch images:debian/12 bookworm 8 | lxc exec bookworm -- apt-get update 9 | lxc exec bookworm -- apt-get install -y --no-install-recommends git ca-certificates curl \ 10 | bison flex gettext texinfo libncurses5-dev libncursesw5-dev \ 11 | gperf automake libtool pkg-config build-essential \ 12 | libgmp-dev libmpc-dev libmpfr-dev libisl-dev binutils-dev libelf-dev \ 13 | libexpat-dev picocom u-boot-tools util-linux \ 14 | kconfig-frontends genromfs 15 | 16 | lxc exec bookworm -- mkdir /tmp/nuttxspace 17 | lxc exec bookworm -- curl -sL https://github.com/apache/nuttx/tarball/master \ 18 | -o /tmp/nuttx.tgz 19 | lxc exec bookworm -- curl -sL https://github.com/apache/nuttx-apps/tarball/master \ 20 | -o /tmp/apps.tgz 21 | lxc exec bookworm -- tar xzf /tmp/nuttx.tgz -C /tmp/nuttxspace --one-top-level=nuttx \ 22 | --strip-components 1 23 | lxc exec bookworm -- tar xzf /tmp/apps.tgz -C /tmp/nuttxspace --one-top-level=apps \ 24 | --strip-components 1 25 | lxc exec bookworm -- curl -sL https://github.com/espressif/crosstool-NG/releases/download/esp-12.2.0_20230208/xtensa-esp32s3-elf-12.2.0_20230208-aarch64-linux-gnu.tar.xz \ 26 | | tar x -Jv -C /tmp -f - 27 | lxc exec bookworm -- mv /tmp/xtensa-esp32s3-elf /opt/xtensa 28 | lxc exec bookworm -- mkdir /tmp/nuttxspace/esp-bins 29 | lxc exec bookworm -- curl -sL https://github.com/espressif/esp-nuttx-bootloader/releases/download/latest/partition-table-esp32s3.bin \ 30 | -o /tmp/nuttxspace/esp-bins/partition-table-esp32s3.bin 31 | lxc exec bookworm -- curl -sL https://github.com/espressif/esp-nuttx-bootloader/releases/download/latest/bootloader-esp32s3.bin \ 32 | -o /tmp/nuttxspace/esp-bins/bootloader-esp32s3.bin 33 | 34 | 35 | lxc exec bookworm -- apt-get install pipx 36 | lxc exec bookworm -- pipx install esptool 37 | lxc exec bookworm -- pipx install pyserial 38 | lxc exec bookworm -- pipx ensurepath 39 | ##lxc exec bookworm -- export PATH=$PATH:/opt/xtensa/bin:/$HOME/.local/bin 40 | lxc exec bookworm -- cd /tmp/nuttxspace/nuttx && \ 41 | ./tools/configure.sh -l esp32s3-devkit:nsh && make 42 | 43 | 44 | 45 | lxc exec bookworm -- curl -sL https://ziglang.org/builds/zig-linux-aarch64-0.12.0-dev.244+f4c9e19bc.tar.xz \ 46 | | tar x -Jv -C $HOME/.local -f - 47 | 48 | lxc exec bookworm -- ln -s $HOME/.local/zig-linux-aarch64-0.12.0-dev.244+f4c9e19bc/zig $HOME/.local/bin/zig 49 | lxc exec bookworm -- zig build-obj -target=riscv32-freestanding-none \ 50 | -mcpu=baseline_rv32-d hello.zig 51 | 52 | #TODO esp32s3 datasheet says RISCV and single precision FPU, 53 | # what are its other attributes? 54 | # https://zig.news/lupyuen/zig-on-risc-v-bl602-quick-peek-with-apache-nuttx-rtos-3apd 55 | 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | [SOFTWARE NAME] Copyright (YEAR) (COPYRIGHT HOLDER(S)/AUTHOR(S))(“Licensor”) 2 | 3 | Hippocratic License Version Number: 2.1. 4 | 5 | Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability. 6 | 7 | Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions: 8 | 9 | * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor. 10 | 11 | * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately. 12 | 13 | * Compliance with Human Rights Principles and Human Rights Laws. 14 | 15 | 1. Human Rights Principles. 16 | 17 | (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles. 18 | 19 | (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise. 20 | 21 | Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply. 22 | 23 | 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply. 24 | 25 | 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles. 26 | 27 | * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee. 28 | 29 | * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party. 30 | 31 | * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM. 32 | 33 | This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws. 34 | 35 | --------------------------------------------------------------------------------