1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
//! This ESP32 hal crate provides support for the ESP32 peripherals
//!
//! ## Features
//! - `external_ram` (enabled by default)
//!     - Enables support for external ram (psram). However proper initialization
//!         of external ram relies on a customized bootloader
//! - `all_in_ram`
//!     - Forces all code and data in RAM instead of flash. This allows usage with
//!         the ROM bootloader and eases debugging
//! - `alloc`
//!     - Enables support for dynamic memory allocations via a GlobalAllocator
//!         and/or AllocRef
//! - `mem`
//!     - Include customized memcpy, memset, etc. which use word (4-byte) sized and aligned
//!         instructions to support IRAM usage and as optimization

#![no_std]
#![feature(const_fn)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(feature = "alloc", feature(alloc_layout_extra))]
#![cfg_attr(feature = "alloc", feature(nonnull_slice_from_raw_parts))]

pub use embedded_hal as hal;
pub use esp32 as target;

extern crate esp32_hal_proc_macros as proc_macros;
pub use proc_macros::interrupt;
pub use proc_macros::ram;

pub mod analog;
pub mod clock_control;
pub mod delay;
pub mod dport;
pub mod efuse;
#[cfg(feature = "external_ram")]
pub mod external_ram;
pub mod gpio;
#[cfg(feature = "rt")]
pub mod interrupt;
pub mod prelude;
pub mod serial;
pub mod spi;
pub mod timer;
pub mod units;

#[cfg(feature = "alloc")]
pub mod alloc;

#[macro_use]
pub mod dprint;

#[cfg(feature = "mem")]
pub mod mem;

/// Function initializes ESP32 specific memories (RTC slow and fast) and
/// then calls original Reset function
///
/// ENTRY point is defined in memory.x
/// *Note: the pre_init function is called in the original reset handler
/// after the initializations done in this function*
#[cfg(feature = "rt")]
#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn ESP32Reset() -> ! {
    // These symbols come from `memory.x`
    extern "C" {
        static mut _rtc_fast_bss_start: u32;
        static mut _rtc_fast_bss_end: u32;

        static mut _rtc_slow_bss_start: u32;
        static mut _rtc_slow_bss_end: u32;

        static mut _stack_end_cpu0: u32;
    }

    // copying data from flash to various data segments is done by the bootloader
    // initialization to zero needs to be done by the application

    // Initialize RTC RAM
    xtensa_lx_rt::zero_bss(&mut _rtc_fast_bss_start, &mut _rtc_fast_bss_end);
    xtensa_lx_rt::zero_bss(&mut _rtc_slow_bss_start, &mut _rtc_slow_bss_end);

    #[cfg(feature = "external_ram")]
    external_ram::init();

    // set stack pointer to end of memory: no need to retain stack up to this point
    xtensa_lx::set_stack_pointer(&mut _stack_end_cpu0);

    // continue with default reset handler
    xtensa_lx_rt::Reset();
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Core {
    PRO = 0,
    APP = 1,
}

pub fn get_core() -> Core {
    match ((xtensa_lx::get_processor_id() >> 13) & 1) != 0 {
        false => Core::PRO,
        true => Core::APP,
    }
}

pub fn get_other_core() -> Core {
    match get_core() {
        Core::PRO => Core::APP,
        Core::APP => Core::PRO,
    }
}