To map an LED from the BBC Micro:bit's LED matrix to led0 in Zephyr, you need to define a device tree overlay that specifies the GPIO pins for the row and column. However, the Micro:bit's LED matrix is a multiplexed grid, so standard GPIO-based LED definitions won't work directly. Here's how to approach it:
1. Understand the Micro:bit LED Matrix Pinout
The Micro:bit's 5x5 LED matrix uses the following GPIO pins:
- Rows: Connected to pins
4, 7, 2, 1, 17 (P0.04, P0.07, P0.02, P0.01, P0.17).
- Columns: Connected to pins
3, 10, 13, 9, 12 (P0.03, P1.00, P0.13, P0.09, P0.12).
2. Define the LED in the Overlay
If you're trying to control a specific LED (e.g., the LED at row=4 (P0.04) and column=13 (P0.13)), you need to create a custom driver or use the matrix driver. The standard led0 API assumes a single GPIO, but matrix LEDs require row/column control.
Example Overlay (for reference, not direct use):
/ {
leds {
compatible = "gpio-leds";
led0: led_0 {
// This won't work directly for matrix LEDs!
gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>; // Row pin (P0.04)
};
};
};
Note: This won't work for matrix LEDs. Use the matrix driver instead.
3. Use the LED Matrix Driver
The proper way to control the Micro:bit's LED matrix is to use Zephyr's led-matrix driver. Here's how to set it up:
a. Update prj.conf
Enable the matrix driver:
CONFIG_LEDS=y
CONFIG_LEDS_MATRIX=y
b. Define the Matrix in the Overlay
Create an overlay file (e.g., bbc_microbit.overlay) to describe the matrix:
/ {
leds {
compatible = "led-matrix";
rows = <5>;
cols = <5>;
row-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>, // P0.04 (Row 1)
<&gpio0 7 GPIO_ACTIVE_HIGH>, // P0.07 (Row 2)
<&gpio0 2 GPIO_ACTIVE_HIGH>, // P0.02 (Row 3)
<&gpio0 1 GPIO_ACTIVE_HIGH>, // P0.01 (Row 4)
<&gpio0 17 GPIO_ACTIVE_HIGH>; // P0.17 (Row 5)
col-gpios = <&gpio0 3 GPIO_ACTIVE_LOW>, // P0.03 (Col 1)
<&gpio1 0 GPIO_ACTIVE_LOW>, // P1.00 (Col 2)
<&gpio0 13 GPIO_ACTIVE_LOW>, // P0.13 (Col 3)
<&gpio0 9 GPIO_ACTIVE_LOW>, // P0.09 (Col 4)
<&gpio0 12 GPIO_ACTIVE_LOW>; // P0.12 (Col 5)
};
};
c. Control the LED in Code
Use the matrix API to toggle LEDs. Example code:
#include <drivers/led.h>
void main(void) {
const struct device *led_matrix = DEVICE_DT_GET(DT_NODELABEL(leds));
if (!device_is_ready(led_matrix)) {
printk("LED matrix not ready!\n");
return;
}
// Turn on the LED at row=0, col=2 (assuming 0-based indexing)
led_matrix_set(led_matrix, 0, 2, 1);
}
4. If You Insist on Using led0 (Not Recommended)
If you want to repurpose a GPIO from the matrix as a regular LED (ignoring the matrix), define it in the overlay. For example, use P0.04 (row pin) as a standalone LED:
/ {
leds {
compatible = "gpio-leds";
led0: led_0 {
gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
};
};
};
Warning: This will conflict with the matrix driver and may not work as expected.
Final Recommendation
Use the LED matrix driver (led-matrix) to control individual LEDs in the grid. The led0 API is designed for single-GPIO LEDs and isn't suitable for multiplexed matrices.