Skip to content

mimxrt: Provide WiFi and BLE support for MIMXRT boards. #12937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/mimxrt/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ The following boards are supported by the port:
- MIMXRT1050-EVK
- MIMXRT1060-EVK
- MIMXRT1064-EVK
- MIMXRT1170-EVK
- Teensy 4.0
- Teensy 4.1
- Olimex RT1010py
- Seeed Arch Mix
- Adafruit Metro M7

Supported MCUs
--------------

+-------------+--------------------+-------------------------+
| Product | CPU | Memory |
+=============+====================+=========================+
| i.MX RT1171 | Cortex-M7 @960 MHz | 2 MB SRAM |
+-------------+--------------------+-------------------------+
| i.MX RT1064 | Cortex-M7 @600 MHz | 1 MB SRAM, 4 MB Flash |
+-------------+--------------------+-------------------------+
| i.MX RT1061 | Cortex-M7 @600 MHz | 1 MB SRAM |
Expand Down Expand Up @@ -71,8 +77,8 @@ operating modes, internal functioning, etc.
For your convenience, a few technical specifications are provided below:

* Architecture: ARM Cortex M7
* CPU frequency: up to 600MHz
* Total RAM available: up to 1 MByte (see table)
* CPU frequency: up to 960MHz
* Total RAM available: up to 2 MByte (see table)
* BootROM: 96KB
* External FlashROM: code and data, via SPI Flash; usual size 2 - 16 MB
Some boards provide additional external RAM and SPI flash.
Expand Down
61 changes: 59 additions & 2 deletions docs/mimxrt/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ working with this board it may be useful to get an overview of the microcontroll
general.rst
tutorial/intro.rst
pinout.rst
wlan_pinout.rst


Installing MicroPython
Expand Down Expand Up @@ -535,8 +536,8 @@ DHT modules may already have one.
Ethernet driver
---------------

All MIMXRT boards except the MIMXRT1011 based boards and Teensy 4.0 support
Ethernet. Example usage::
All MIMXRT boards except the MIMXRT101x based boards and Teensy 4.0 support
Ethernet. Example usage::

import network

Expand All @@ -556,6 +557,62 @@ port and LAN(1) for the 1G port.

For details of the network interface refer to the class :ref:`network.LAN <network.LAN>`.

WLAN
----

In addition to Ethernet, some boards can be combined with an ESP32 based
WLAN module to support WLAN connectivity. WLAN is then another class
of the the :mod:`network` module::

import network

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('ssid', 'key') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses

ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(ssid='ESP-AP') # set the SSID of the access point

A useful function for connecting to your local WiFi network is::

def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('ssid', 'key')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())

Once the network is established the :mod:`socket <socket>` module can be used
to create and use TCP/UDP sockets as usual, and the ``requests`` module for
convenient HTTP requests.

Supported boards are:

- ADAFRUIT_METRO_M7_AIRLIFT: WLAN adapter built-in
- MIMXRT1010_EVK with e.g. the Adafruit Airlift Uno board
- MIMXRT1015_EVK with e.g. the Adafruit Airlift Uno board
- OLIMEX RT1010py
- MIMXRT1020_EVK with e.g. the Adafruit Airlift Uno board
- MIMXRT1050_EVK with e.g. the Adafruit Airlift Uno board
- Seeed Arch Mix
- Teensy 4.0 with e.g. Adafruit Feather Airlift + Teensy/Feather adapter
- Teensy 4.1 with e.g. Adafruit Feather Airlift + Teensy/Feather adapter

More boards may support it, but could not be tested yet. As external
WLAN module, any ESP32 based breakout can be used. It has to be loaded
with the proper firmware and wired up accordingly. For the wiring
scheme, see the :ref:`WLAN connection scheme <mimxrt_wlan_pinout>`.


Transferring files
------------------

Expand Down
190 changes: 190 additions & 0 deletions docs/mimxrt/wlan_pinout.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
.. _mimxrt_wlan_pinout:

Managing external WLAN/BLE adapters
===================================

Connection scheme for the i.MXRT WLAN adapters
----------------------------------------------

Mapping between the NINA/esp_hosted signals and board pins by name.

======== ========== ======= ======== ====== ====== ======= ========
NINAW10 esp_hosted Airlift MIMXRT Teensy OLIMEX Seeed METRO M7
Name Name Name 10xx_EVK 4.x RT1010 ArchMix Airlift
======== ========== ======= ======== ====== ====== ======= ========
MOSI/RTS MOSI MOSI D11 D11 A2 J4-14 MOSI
MISO MISO MISO D12 D12 A1 J4-13 MISO
SCK SCK SCK D13 D13 A4 J4-15 SCK
CS CS CS D10 D5 A3 J4-12 ESP_CS
ACK/CTS HANDSHAKE Busy D7 D9 SCL2 J4-11 ESP_BUSY
RESET RESET Reset D5 D6 SDA2 J4-10 ESP_RESET
GPIO0 DATAREADY GP0 D6 D10 (*) J4-09 ESP_GP0
SPI# 0 0 0 0 0
TX TX TX D0 D0 D8 J4-06 D0
RX RX RX D1 D1 D7 J4-07 D1
RTS MOSI/RTS RTS D11 D11 A2 J4-14 MOSI
CTS CTS CTS D12 D12 SCL2 J4-13 ESP_BUSY
UART# 1 1 2 3 1
======== ========== ======= ======== ====== ====== ======= ========

(*) GPIO0 of the WiFi module can be shared e.g. with SCK if they are
decoupled with a resistor from SCK (Host) to the GPIO input module.

Mapping between firmware signal names and ESP32 pins for the NINA firmware
and esp_hosted firmware

======== ========== ======== ======= =======
NINAW10 esp_hosted NINA Airlift Airlift
Name Name W102 pin Name pin
======== ========== ======== ======= =======
MOSI MOSI 12 MOSI 14
MISO MISO 23 MISO 23
SCK SCK 18 SCK 18
GPIO1/CS CS 5 CS 5
ACK HANDSHAKE 33 Busy 33
RESET RESET EN Reset EN
GPIO0 DATAREADY 0 GP0 0
TX TX 1 TX 1
RX TX 3 RX 3
RTS MOSI/RTS 12 - 14
CTS CTS 33 - 33
======== ========== ======== ======= =======

Mapping between the NINA/esp_hosted signals and adapter pins by name.

======== ========== ======= ========= ======== ========= ==========
NINAW10 esp_hosted Airlift ItsyBitsy Feather METRO M7 Airlift
Name Name Name Add-on Add-on Airlift Uno Shield
======== ========== ======= ========= ======== ========= ==========
MOSI MOSI MOSI MOSI MOSI MOSI MOSI
MISO MISO MISO MISO MISO MISO MISO
SCK SCK SCK SCK SCK SCK SCK
GPIO1/CS CS CS ECS ESPCS ESP_CS CS
ACK HANDSHAKE Busy EBSY ESPBUSY ESP_BUSY BUSY
RESET RESET Reset RST ESPRESET ESP_RESET RST
GPIO0 DATAREADY GP0 GPIO0 ESPGPIO0 ESP_GP0 G0
TX TX TX ETX ESPTX ESP_TX TX
RX RX RX ERX ESPRX ESP_RX RX
======== ========== ======= ========= ======== ========= ==========

Signals required for WiFi:

- NinaW10: MISO/MOSI/SCK (SPI), CS, Busy (ACK), Reset
- esp_hosted: MISO/MOSI/SCK (SPI), CS, Handshake, Dataready, Reset

Signals required for Bluetooth:

- NinaW10: RX/TX (UART), RTS (MOSI), CS, RESET
- esp_hosted: RX/TX (UART), RTS (MOSI), CS, RESET

CTS seems not to be used, but specified.

Signals required for NINA/esp_hosted firmware upload:

- RX/TX (UART), GPIO0, Reset.

If the hardware supports it, pull RESET low (!). That keeps the
ESP32 with the NINA/esp_hosted firmware silent and at low power
when not being used.


.. _mimxrt_wlan_firmware_upload:

Instructions for WLAN/BLE firmware upload ESP32 module
------------------------------------------------------

The NINA firmware in the NINA module has to be updated for use with MicroPython. That can be done
using MicroPython and two small Python scripts.

The firmware binaries are available at
https://github.com/micropython/micropython-lib/tree/master/micropython/espflash
There are the firmware files available for different esp32 modules.

1. Adafruit Airlift modules with NINA firmware: NINA_FW_v1.5.0_Airlift.bin.
2. Adafruit Airlift modules with esp_hosted firmware: esp_hosted_airlift.bin
3. Arduino Nano RP2040 connect: NINA_FW_v1.5.0_W102.bin

For the mimxrt boards with i.MX RT 101x MCU, you need the file NINA_FW_v1.5.0_Airlift.bin, for
all other boards esp_hosted_airlift.bin.

For firmware upload, the following connections to the WiFi module are required:

- Pin Reset (as above)
- Pin GPIO0
- UART RX
- UART TX

The GPIO pins and UART device id varies between boards. See the tables above.
At the Adafruit Metro M7 board, the UART is UART(1), and the Pin names
for reset and GPIO0 are ESP_RESET and ESP_GPIO0.
The firmware can be uploaded, using the espflash.py module, a short script
using espflash.py and mpremote. espflash.py is available at
https://github.com/micropython/micropython-lib/tree/master/micropython/espflash.
This place also holds the example script.::

import espflash
from machine import Pin
from machine import UART
import sys
sys.path.append("/flash")

reset = Pin("ESP_RESET", Pin.OUT)
gpio0 = Pin("ESP_GPIO0", Pin.OUT)
uart = UART(0, 115200, timeout=350)

md5sum = b"b0b9ab23da820a469e597c41364acb3a"
path = "/remote/NINA_FW_v1.5.0_Airlift.bin"

esp = espflash.ESPFlash(reset, gpio0, uart)
# Enter bootloader download mode, at 115200
esp.bootloader()
# Can now change to higher/lower baud rate
esp.set_baudrate(921600)
# Must call this first before any flash functions.
esp.flash_attach()
# Read flash size
size = esp.flash_read_size()
# Configure flash parameters.
esp.flash_config(size)
# Write firmware image from internal storage.
esp.flash_write_file(path)
# Compares file and flash MD5 checksum.
esp.flash_verify_file(path, md5sum)
# Resets the ESP32 chip.
esp.reboot()

The script shows the set-up for the Metro M7 board.
The md5sum is the one of the WiFi firmware. It may change and
can be recalculated using e.g. the Linux *md5sum* command. It is used to
verify the firmware upload. To upload the firmware, place the firmware
and the above script (let's call it ninaflash.py) into the same directory
on your PC, and run the command::

mpremote connect <port> mount . run ninaflash.py

After a while, the upload will start. A typical start sequence looks like::

Local directory . is mounted at /remote
Failed to read response to command 8.
Failed to read response to command 8.
Changing baudrate => 921600
Flash attached
Flash size 2.0 MBytes
Flash write size: 1310720 total_blocks: 320 block size: 4096
Writing sequence number 0/320...
Writing sequence number 1/320...
Writing sequence number 2/320...
Writing sequence number 3/320...
Writing sequence number 4/320...
....
....
Writing sequence number 317/320...
Writing sequence number 318/320...
Writing sequence number 319/320...
Flash write finished
Flash verify: File MD5 b'b0b9ab23da820a469e597c41364acb3a'
Flash verify: Flash MD5 b'b0b9ab23da820a469e597c41364acb3a'
Firmware verified.

The initial messages *Failed to read response to command 8.*
can be ignored.
3 changes: 2 additions & 1 deletion drivers/esp-hosted/esp_hosted_bthci_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ int mp_bluetooth_hci_controller_init(void) {

mp_uint_t start = mp_hal_ticks_ms();
// Skip bootloader messages.
while ((mp_hal_ticks_ms() - start) < 2500) {
while ((mp_hal_ticks_ms() - start) < 500) {
if (mp_bluetooth_hci_uart_any()) {
mp_bluetooth_hci_uart_readchar();
start = mp_hal_ticks_ms();
}
MICROPY_EVENT_POLL_HOOK
}
Expand Down
25 changes: 23 additions & 2 deletions drivers/ninaw10/nina_bthci_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
* Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io>
* Copyright (c) 2023 Robert Hammelrath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -53,12 +54,23 @@
#define error_printf(...) // mp_printf(&mp_plat_print, "nina_bthci_uart.c: " __VA_ARGS__)
#define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bthci_uart.c: " __VA_ARGS__)

// Use the name CS instead of GPIO1
#ifndef MICROPY_HW_NINA_CS
#define MICROPY_HW_NINA_CS MICROPY_HW_NINA_GPIO1
#endif

// Provided by the port, and also possibly shared with the stack.
extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];

static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param_buf) {
uint8_t *buf = mp_bluetooth_hci_cmd_buf;

// Clear the UART input buffer before sending the reset command.
// The ESP32 reset message may stick in it.
while (mp_bluetooth_hci_uart_any()) {
mp_bluetooth_hci_uart_readchar();
}

buf[0] = HCI_COMMAND_PACKET;
buf[1] = ocf;
buf[2] = ogf << 2 | ocf >> 8;
Expand Down Expand Up @@ -125,8 +137,17 @@ int mp_bluetooth_hci_controller_init(void) {
mp_hal_pin_output(MICROPY_HW_NINA_RESET);
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0);

mp_hal_pin_output(MICROPY_HW_NINA_GPIO1);
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 0);
// care for dedicated setting of RTS/CTS
#ifdef MICROPY_HW_NINA_RTS
mp_hal_pin_output(MICROPY_HW_NINA_RTS);
mp_hal_pin_write(MICROPY_HW_NINA_RTS, 0);
#endif
#ifdef MICROPY_HW_NINA_CTS
mp_hal_pin_input(MICROPY_HW_NINA_CTS);
#endif

mp_hal_pin_output(MICROPY_HW_NINA_CS);
mp_hal_pin_write(MICROPY_HW_NINA_CS, 0);
mp_hal_delay_ms(150);

mp_hal_pin_write(MICROPY_HW_NINA_RESET, 1);
Expand Down
Loading
Loading