|
| 1 | +/** |
| 2 | + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "pico/stdlib.h" |
| 8 | +#include "pico/binary_info.h" |
| 9 | + |
| 10 | +#ifdef CYW43_WL_GPIO_LED_PIN |
| 11 | +#include "pico/cyw43_arch.h" |
| 12 | +#endif |
| 13 | + |
| 14 | +// Set an LED_TYPE variable - 0 is default, 1 is connected to WIFI chip |
| 15 | +// Note that LED_TYPE == 1 is only supported when initially compiled for |
| 16 | +// a board with PICO_CYW43_SUPPORTED (eg pico_w), else the required |
| 17 | +// libraries won't be present |
| 18 | +bi_decl(bi_program_feature_group(0x1111, 0, "LED Configuration")); |
| 19 | +#if defined(PICO_DEFAULT_LED_PIN) |
| 20 | + // the tag and id are not important as picotool filters based on the |
| 21 | + // variable name, so just set them to 0 |
| 22 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 0)); |
| 23 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, PICO_DEFAULT_LED_PIN)); |
| 24 | +#elif defined(CYW43_WL_GPIO_LED_PIN) |
| 25 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 1)); |
| 26 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, CYW43_WL_GPIO_LED_PIN)); |
| 27 | +#else |
| 28 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 0)); |
| 29 | + bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, 25)); |
| 30 | +#endif |
| 31 | + |
| 32 | +#ifndef LED_DELAY_MS |
| 33 | +#define LED_DELAY_MS 250 |
| 34 | +#endif |
| 35 | + |
| 36 | +// Perform initialisation |
| 37 | +int pico_led_init(void) { |
| 38 | + if (LED_TYPE == 0) { |
| 39 | + // A device like Pico that uses a GPIO for the LED so we can |
| 40 | + // use normal GPIO functionality to turn the led on and off |
| 41 | + gpio_init(LED_PIN); |
| 42 | + gpio_set_dir(LED_PIN, GPIO_OUT); |
| 43 | + return PICO_OK; |
| 44 | +#ifdef CYW43_WL_GPIO_LED_PIN |
| 45 | + } else if (LED_TYPE == 1) { |
| 46 | + // For Pico W devices we need to initialise the driver etc |
| 47 | + return cyw43_arch_init(); |
| 48 | +#endif |
| 49 | + } else { |
| 50 | + return PICO_ERROR_INVALID_DATA; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Turn the led on or off |
| 55 | +void pico_set_led(bool led_on) { |
| 56 | + if (LED_TYPE == 0) { |
| 57 | + // Just set the GPIO on or off |
| 58 | + gpio_put(LED_PIN, led_on); |
| 59 | +#ifdef CYW43_WL_GPIO_LED_PIN |
| 60 | + } else if (LED_TYPE == 1) { |
| 61 | + // Ask the wifi "driver" to set the GPIO on or off |
| 62 | + cyw43_arch_gpio_put(LED_PIN, led_on); |
| 63 | +#endif |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +int main() { |
| 68 | + int rc = pico_led_init(); |
| 69 | + hard_assert(rc == PICO_OK); |
| 70 | + while (true) { |
| 71 | + pico_set_led(true); |
| 72 | + sleep_ms(LED_DELAY_MS); |
| 73 | + pico_set_led(false); |
| 74 | + sleep_ms(LED_DELAY_MS); |
| 75 | + } |
| 76 | +} |
0 commit comments