|
Embedded /
ESP Libraries
ESP Arduino standard SW stackPlatformIO - Library managementExample :
lib_deps =
bodmer/TFT_eSPI@^1.2.3
https://github.com/stickbreaker/OneWire.git#v2.3.3-ESP32
Where
ISR - interrupt service routineshw_timer_t* my_timer; uint32_t FREQ = 1000; //Hz - desired frequency const uint8_t TMR0_3 = 3; // 0..3 const uint16_t DIVIDER = (80000000/80); // 1us tick base const uint64_t alarm_value = DIVIDER / 2 / FREQ /*[Hz]*/; // ISR frequency = 1000 Hz --> ISR change pin state twice per period --> thus divider 2 void IRAM_ATTR timer_isr_callback() { digitalWrite( PWM_PIN, !digitalRead(PWM_PIN) ); } void setup() { pinMode( PWM_PIN, OUTPUT ); my_timer = timerBegin( TMR0_3, DIVIDER, true ); // timerBegin( uint8_t num, uint16_t divider, bool countUp ) timerAttachInterrupt( TMR0_3, timer_isr_callback, true ); // timerAttachInterrupt( hw_timer_t *timer, void (*fn)(void), bool edge ) timerAlarmWrite( TMR0_3, alarm_value, true ); // timerAlarmWrite( hw_timer_t *timer, uint64_t alarm_value, bool autoreload ) timerAlarmEnable( TMR0_3 ); } void loop() { unsigned long currMillis = millis(); static unsigned long lastMillis = 0; if( (currMillis - lastMillis) >= 1000) { lastMillis = currMillis; FREQ += 10; // icrease by 10 Hz if( FREQ >= 2000 ) FREQ = 1000; // set new frequency uint64_t alarm_value = DIVIDER / 2 / FREQ; timerAlarmWrite( TMR0_3, alarm_value, true ); // timerAlarmWrite( hw_timer_t *timer, uint64_t alarm_value, bool autoreload ) } } MUTEXIRAM_ATTR portMUX_TYPE my_mutex = portMUX_INITIALIZER_UNLOCKED; IRAM_ATTR volatile uint8_t event = 0; void IRAM_ATTR isr_callback() { // critical section portENTER_CRITICAL( &my_mutex ); //LOCK event++; // set event portEXIT_CRITICAL( &my_mutex ); //UNLOCK } void setup() { pinMode( INTERRUPT_PIN, INPUT_PULLUP); attachInterrupt( digitalPinToInterrupt(INTERRUPT_PIN), isr_callback, FALLING); } void loop { // critical section - synchro portENTER_CRITICAL( &my_mutex ); //LOCK uint8_t local = event; // read event if(local) event--; portEXIT_CRITICAL( &my_mutex ); //UNLOCK if(local) { // event processing ... } } PWM and TimersLinks
PWM Channels Now let us turn our attention to the concept of a channel. A channel represents a unique PWM output signal. Within each group, there are 4 timers shared among 8 channels, which means that every two channels share the same timer. Since the timer determines the frequency, it’s important to understand that we cannot adjust the frequency of each channel independently within a pair. However, we can control the PWM duty cycle of each channel independently. The ESP32 can generate a PWM signal with a frequency of up to 40 MHz, and the PWM resolution can be adjusted from 1 to 16 bits. But this doesn’t mean you can set a frequency of 40 MHz and a resolution of 16 bits at the same time. This is due to the fact that the maximum PWM frequency and resolution are both bound by the clock source. #include <stdio.h> #include "driver/ledc.h" #include "esp_err.h" #define TIMER_USED LEDC_TIMER_0 // LEDC_TIMER_0 .. LEDC_TIMER_3 #define RESOLUTION_USED LEDC_TIMER_4_BIT // LEDC_TIMER_1_BIT .. LEDC_TIMER_20_BIT #define PWM_CHANNEL_USED LEDC_CHANNEL_0 // LEDC_CHANNEL_0 .. LEDC_CHANNEL_7 #define PWM_GPIO_PIN_USED 17 static void IDF_ledc_set_frequency( uint32_t IN_freq_hz ) { //After that, MAX_DUTY_CYCLE is calculated using the formula 2^PWM_RESOLUTION−1. //This value determines the maximum achievable duty cycle based on the chosen resolution. const int MAX_DUTY_CYCLE = (int)(pow(2, RESOLUTION_USED) - 1); // Prepare and then apply the LEDC PWM timer configuration ledc_timer_config_t ledc_timer; memset( &ledc_timer, 0, sizeof(ledc_timer_config_t)); ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; ledc_timer.timer_num = (ledc_timer_t) TIMER_USED; // LEDC_TIMER_0, LEDC_TIMER_1, LEDC_TIMER_2, LEDC_TIMER_3 ledc_timer.duty_resolution = (ledc_timer_bit_t) RESOLUTION_USED; // for (LEDC_TIMER_4_BIT) duty values are 0..15 (8=50/50%) ledc_timer.freq_hz = IN_freq_hz; // set output frequency //ledc_timer.clk_cfg = LEDC_APB_CLK; // somehow missing in Arduino framework ledc_timer_config( &ledc_timer ); // Prepare and then apply the LEDC PWM channel configuration ledc_channel_config_t ledc_channel; memset( &ledc_channel, 0, sizeof(ledc_channel_config_t)); ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE; ledc_channel.channel = (ledc_channel_t) PWM_CHANNEL_USED; // LEDC_CHANNEL_0 .. LEDC_CHANNEL_7 ledc_channel.timer_sel = (ledc_timer_t) TIMER_USED ; // LEDC_TIMER_0 .. LEDC_TIMER_3 ledc_channel.intr_type = LEDC_INTR_DISABLE; ledc_channel.gpio_num = PWM_GPIO_PIN_USED; ledc_channel.duty = MAX_DUTY_CYCLE/2+1; // for (LEDC_TIMER_4_BIT) duty values are 0..15 (8=50/50%) ledc_channel.hpoint = 0; ledc_channel_config( &ledc_channel ); }
PSRAMWhen PSRAM not detected
Library : esp32-hal-psram.c
bool psramFound();
void *ps_malloc(size_t size);
void *ps_calloc(size_t n, size_t size);
void *ps_realloc(void *ptr, size_t size);
ESP-NOW
DS18B20Libraries :
Does not work with ESP32
OTA - Over The Air (firmware update)YouTube guides
GxEPD - Libraries Comparison
GxEPD2 - Libraries ComparisonULP - Ultra Low PowerYouTube guides
Sleep modesBootModerst cause:2, boot mode:(X,Y)
DEBUG LEVELbuild_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG esp32-hal-log.h :
#define ARDUHAL_LOG_LEVEL_NONE (0)
#define ARDUHAL_LOG_LEVEL_ERROR (1)
#define ARDUHAL_LOG_LEVEL_WARN (2)
#define ARDUHAL_LOG_LEVEL_INFO (3)
#define ARDUHAL_LOG_LEVEL_DEBUG (4)
#define ARDUHAL_LOG_LEVEL_VERBOSE (5)
esptool
Info
Manufacturer: 20
Device: 4016
Detected flash size: 4MB
MAC: 84:cc:a8:5e:59:c0
Warning: ESP32 has no Chip ID. Reading MAC instead.
MAC: 84:cc:a8:5e:59:c0
Enabling default SPI flash mode... (--no-stub)
Status value: 0x0000
Flashing
PATH of : esptool.py :
usage: esptool [-h]
[--chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2,esp8684}]
[--port PORT] [-p PORT]
[--baud BAUD] [-b BAUD]
[--before {default_reset,usb_reset,no_reset,no_reset_no_sync}]
[--after {hard_reset,soft_reset,no_reset,no_reset_stub}]
[--no-stub] [--trace] [--override-vddsdio [{1.8V,1.9V,OFF}]]
[--connect-attempts CONNECT_ATTEMPTS]
{load_ram, dump_mem, read_mem, write_mem,
write_flash, run, image_info, make_image, elf2image,
read_mac, chip_id, flash_id, read_flash_status, write_flash_status,
read_flash, verify_flash,
erase_flash, erase_region, merge_bin, version, get_security_info}
...
|