|
| 1 | +//! # ESP32-WROOM-RP Pico Wireless Example |
| 2 | +//! |
| 3 | +//! This application demonstrates how to use the ESP32-WROOM-RP crate to perform |
| 4 | +//! a DNS hostname lookup after setting what DNS server to use. |
| 5 | +//! |
| 6 | +//! See the `Cargo.toml` file for Copyright and license details. |
| 7 | +
|
| 8 | +#![no_std] |
| 9 | +#![no_main] |
| 10 | + |
| 11 | +extern crate esp32_wroom_rp; |
| 12 | + |
| 13 | +include!("secrets/secrets.rs"); |
| 14 | + |
| 15 | +// The macro for our start-up function |
| 16 | +use cortex_m_rt::entry; |
| 17 | + |
| 18 | +// Needed for debug output symbols to be linked in binary image |
| 19 | +use defmt_rtt as _; |
| 20 | + |
| 21 | +use panic_probe as _; |
| 22 | + |
| 23 | +// Alias for our HAL crate |
| 24 | +use rp2040_hal as hal; |
| 25 | + |
| 26 | +use embedded_hal::spi::MODE_0; |
| 27 | +use fugit::RateExtU32; |
| 28 | +use hal::clocks::Clock; |
| 29 | +use hal::pac; |
| 30 | + |
| 31 | +use esp32_wroom_rp::network::IpAddress; |
| 32 | + |
| 33 | +/// The linker will place this boot block at the start of our program image. We |
| 34 | +/// need this to help the ROM bootloader get our code up and running. |
| 35 | +#[link_section = ".boot2"] |
| 36 | +#[used] |
| 37 | +pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; |
| 38 | + |
| 39 | +/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust |
| 40 | +/// if your board has a different frequency |
| 41 | +const XTAL_FREQ_HZ: u32 = 12_000_000u32; |
| 42 | + |
| 43 | +/// Entry point to our bare-metal application. |
| 44 | +/// |
| 45 | +/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function |
| 46 | +/// as soon as all global variables are initialized. |
| 47 | +#[entry] |
| 48 | +fn main() -> ! { |
| 49 | + // Grab our singleton objects |
| 50 | + let mut pac = pac::Peripherals::take().unwrap(); |
| 51 | + let core = pac::CorePeripherals::take().unwrap(); |
| 52 | + |
| 53 | + // Set up the watchdog driver - needed by the clock setup code |
| 54 | + let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); |
| 55 | + |
| 56 | + // Configure the clocks |
| 57 | + let clocks = hal::clocks::init_clocks_and_plls( |
| 58 | + XTAL_FREQ_HZ, |
| 59 | + pac.XOSC, |
| 60 | + pac.CLOCKS, |
| 61 | + pac.PLL_SYS, |
| 62 | + pac.PLL_USB, |
| 63 | + &mut pac.RESETS, |
| 64 | + &mut watchdog, |
| 65 | + ) |
| 66 | + .ok() |
| 67 | + .unwrap(); |
| 68 | + |
| 69 | + let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); |
| 70 | + |
| 71 | + // The single-cycle I/O block controls our GPIO pins |
| 72 | + let sio = hal::Sio::new(pac.SIO); |
| 73 | + |
| 74 | + // Set the pins to their default state |
| 75 | + let pins = hal::gpio::Pins::new( |
| 76 | + pac.IO_BANK0, |
| 77 | + pac.PADS_BANK0, |
| 78 | + sio.gpio_bank0, |
| 79 | + &mut pac.RESETS, |
| 80 | + ); |
| 81 | + |
| 82 | + defmt::info!("ESP32-WROOM-RP DNS resolve example"); |
| 83 | + |
| 84 | + // These are implicitly used by the spi driver if they are in the correct mode |
| 85 | + let _spi_miso = pins.gpio16.into_mode::<hal::gpio::FunctionSpi>(); |
| 86 | + let _spi_sclk = pins.gpio18.into_mode::<hal::gpio::FunctionSpi>(); |
| 87 | + let _spi_mosi = pins.gpio19.into_mode::<hal::gpio::FunctionSpi>(); |
| 88 | + |
| 89 | + let spi = hal::Spi::<_, _, 8>::new(pac.SPI0); |
| 90 | + |
| 91 | + // Exchange the uninitialized SPI driver for an initialized one |
| 92 | + let mut spi = spi.init( |
| 93 | + &mut pac.RESETS, |
| 94 | + clocks.peripheral_clock.freq(), |
| 95 | + 8.MHz(), |
| 96 | + &MODE_0, |
| 97 | + ); |
| 98 | + |
| 99 | + let mut esp_pins = esp32_wroom_rp::gpio::EspControlPins { |
| 100 | + // CS on pin x (GPIO7) |
| 101 | + cs: pins.gpio7.into_mode::<hal::gpio::PushPullOutput>(), |
| 102 | + // GPIO0 on pin x (GPIO2) |
| 103 | + gpio0: pins.gpio2.into_mode::<hal::gpio::PushPullOutput>(), |
| 104 | + // RESETn on pin x (GPIO11) |
| 105 | + resetn: pins.gpio11.into_mode::<hal::gpio::PushPullOutput>(), |
| 106 | + // ACK on pin x (GPIO10) |
| 107 | + ack: pins.gpio10.into_mode::<hal::gpio::FloatingInput>(), |
| 108 | + }; |
| 109 | + |
| 110 | + let mut wifi = esp32_wroom_rp::wifi::Wifi::init(&mut spi, &mut esp_pins, &mut delay).unwrap(); |
| 111 | + |
| 112 | + let result = wifi.join(SSID, PASSPHRASE); |
| 113 | + defmt::info!("Join Result: {:?}", result); |
| 114 | + |
| 115 | + defmt::info!("Entering main loop"); |
| 116 | + |
| 117 | + loop { |
| 118 | + match wifi.get_connection_status() { |
| 119 | + Ok(byte) => { |
| 120 | + defmt::info!("Get Connection Result: {:?}", byte); |
| 121 | + let sleep: u32 = 1500; |
| 122 | + delay.delay_ms(sleep); |
| 123 | + |
| 124 | + if byte == 3 { |
| 125 | + defmt::info!("Connected to Network: {:?}", SSID); |
| 126 | + |
| 127 | + // The IPAddresses of two DNS servers to resolve hostnames with. |
| 128 | + // Note that failover from ip1 to ip2 is fully functional. |
| 129 | + let ip1: IpAddress = [9, 9, 9, 9]; |
| 130 | + let ip2: IpAddress = [8, 8, 8, 8]; |
| 131 | + let dns_result = wifi.set_dns(ip1, Some(ip2)); |
| 132 | + |
| 133 | + defmt::info!("set_dns result: {:?}", dns_result); |
| 134 | + |
| 135 | + let hostname = "github.com"; |
| 136 | + defmt::info!("Doing a DNS resolve for {}", hostname); |
| 137 | + |
| 138 | + match wifi.resolve(hostname) { |
| 139 | + Ok(ip) => { |
| 140 | + defmt::info!("Server IP: {:?}", ip); |
| 141 | + } |
| 142 | + Err(e) => { |
| 143 | + defmt::error!("Failed to resolve hostname {}", hostname); |
| 144 | + defmt::error!("Err: {}", e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + wifi.leave().ok().unwrap(); |
| 149 | + } else if byte == 6 { |
| 150 | + defmt::info!("Disconnected from Network: {:?}", SSID); |
| 151 | + } |
| 152 | + } |
| 153 | + Err(e) => { |
| 154 | + defmt::info!("Failed to Get Connection Result: {:?}", e); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments