Page 15 of 16
Page 379
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
void app_main(void)
{
uint32_t *GPIObase = (uint32_t *)0x60004000; // EP32 S3
// uint32_t* GPIObase = (uint32_t*)0x3FF44000; //ESP32
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
uint32_t *GPIOSet = GPIObase + 8 / 4;
uint32_t *GPIOClear = GPIObase + 0xC / 4;
uint32_t mask = 1 << 2;
while (true)
{
*GPIOSet = mask;
vTaskDelay(1000 / portTICK_PERIOD_MS);
*GPIOClear = mask;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Page 380
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "soc/gpio_reg.h"
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
uint32_t mask = 1 << 2;
while (true)
{
*(int32_t *)GPIO_OUT_W1TS_REG = mask;
vTaskDelay(1000 / portTICK_PERIOD_MS);
*(int32_t *)GPIO_OUT_W1TC_REG = mask;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Page 382
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "soc/gpio_reg.h"
void gpio_value_mask(int32_t value, int32_t mask)
{
*(int32_t *)GPIO_OUT_REG = (*(int32_t *)GPIO_OUT_REG & ~mask) |
(value & mask);
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_OUTPUT);
uint32_t mask = (1 << 2) | (1 << 4);
uint32_t value1 = (1 << 2);
uint32_t value2 = (1 << 4);
while (true)
{
gpio_value_mask(value1, mask);
gpio_value_mask(value2, mask);
}
}
Page 385 Complete program
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include <sys/time.h>
#include "driver/ledc.h"
#include "math.h"
#include <unistd.h>
void delay_us(int t)
{
usleep(t);
}
bool isrollover(int timer)
{
int32_t *base = (int32_t *)0x600190C0; // ESP32 S3
// int32_t *base=(int32_t*)0x3FF59180; //ESP32
bool value = *((int32_t *)base) & 1 << timer;
*((int32_t *)base + 3) = 1 << timer;
return value;
}
void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.clk_cfg = LEDC_APB_CLK};
ledc_timer.timer_num = timer;
ledc_timer.duty_resolution = res;
ledc_timer.freq_hz = freq;
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.intr_type = LEDC_INTR_DISABLE,
};
ledc_channel.channel = chan;
ledc_channel.timer_sel = timer;
ledc_channel.gpio_num = gpio;
ledc_channel.duty = ((float)(2 << (res - 1))) * duty;
ledc_timer_config(&ledc_timer);
ledc_channel_config(&ledc_channel);
}
uint8_t wave[256];
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_OUTPUT);
gpio_set_level(4, 0);
for (int i = 0; i < 256; i++)
{
wave[i] = (uint8_t)((128.0 + sinf((float)i * 2.0 *
3.14159 / 255.0) *
128.0));
}
int f = 60000;
PWMconfigLow(2, 0, 0, 8, f, 0.25);
while (true)
{
for (int i = 0; i < 256; i++)
{
ledc_set_duty(LEDC_LOW_SPEED_MODE, 0, wave[i]);
ledc_update_duty(LEDC_LOW_SPEED_MODE, 0);
while (!isrollover(0))
{
};
}
}
}
Page 385 full program from fragment
#include "esp_wifi.h"
#include "string.h"
#include "nvs_flash.h"
#include "esp_netif_sntp.h"
#include "sys/time.h"
#include "wificonnect.h"
void app_main(void)
{
wifiConnect("co", "ssid", "password");
while (wifiStatus != 1010)
{
vTaskDelay(3 / portTICK_PERIOD_MS);
};
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
esp_netif_sntp_init(&config);
if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000)) != ESP_OK)
{
printf("Failed to update system time within 10s timeout");
}
struct timeval t;
gettimeofday(&t, NULL);
setenv("TZ", "UTC-1", 1);
tzset();
struct tm *tm = localtime(&(t.tv_sec));
printf("hour = %d\n", tm->tm_hour);
printf("min = %d\n", tm->tm_min);
char date[100];
strftime(date, 100, "%a, %d %b %Y %T", tm);
printf("date = %s\n", date);
}
Page 390
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_sleep.h"
void app_main(void)
{
esp_sleep_enable_timer_wakeup(1000000);
for (int i = 0; i < 10; i++)
{
printf("starting sleep %d \n", i);
fflush(stdout);
esp_light_sleep_start();
printf("back from sleep %d \n", i);
fflush(stdout);
}
}
Page 390
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_sleep.h"
void app_main(void)
{
esp_sleep_enable_timer_wakeup(1000000);
for (int i = 0; i < 10; i++)
{
printf("starting sleep %d \n", i);
fflush(stdout);
esp_deep_sleep_start();
printf("back from sleep %d \n", i);
fflush(stdout);
}
}
Page 399
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_partition.h"
void app_main(void)
{
esp_partition_iterator_t partit = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (partit != NULL)
{
const esp_partition_t *part = esp_partition_get(partit);
printf("DATA label= %s Address= %lX size = %ld \n", part->label, part->address, part->size);
partit = esp_partition_next(partit);
};
esp_partition_iterator_release(partit);
printf("\n");
partit = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (partit != NULL)
{
const esp_partition_t *part = esp_partition_get(partit);
printf("APP label= %s Address= %lX size = %ld \n", part->label, part->address, part->size);
partit = esp_partition_next(partit);
};
esp_partition_iterator_release(partit);
return;
}
Page 403
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "nvs_flash.h"
void app_main(void)
{
nvs_flash_init();
nvs_handle_t my_handle;
nvs_open("localstorage", NVS_READWRITE, &my_handle);
char key[] = "mykey";
int32_t myvalue = 42;
int32_t myretrievedvalue;
nvs_set_i32(my_handle, key, myvalue);
nvs_commit(my_handle);
nvs_get_i32(my_handle, key, &myretrievedvalue);
printf("%ld\n", myretrievedvalue);
return;
}
Page 404 You have to create a data partition subtype fat named FAT for this to work
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_vfs_fat.h"
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
void app_main(void)
{
const esp_vfs_fat_mount_config_t mount_config = {
.max_files = 4,
.format_if_mount_failed = true,
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
};
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", "FAT", &mount_config, &s_wl_handle);
if (err != ESP_OK) {
printf("%X\n",err);
return;
}
FILE *f = fopen("/spiflash/hello.txt", "wb");
fprintf(f, "Hello world");
fclose(f);
char buf[25];
f = fopen("/spiflash/hello.txt", "rb");
fgets(buf,sizeof(buf),f);
fclose(f);
printf("%s\n",buf);
return;
}
Page 408
#include <string.h>
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
void app_main(void)
{
spi_bus_config_t bus_cfg = {
.mosi_io_num = 15,
.miso_io_num = 4,
.sclk_io_num = 18,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000,
};
sdmmc_host_t host_config = SDSPI_HOST_DEFAULT();
host_config.max_freq_khz = 400;
sdspi_device_config_t slot_config =
SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = 5;
slot_config.host_id = host_config.slot;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
spi_bus_initialize(host_config.slot, &bus_cfg,
SDSPI_DEFAULT_DMA);
sdmmc_card_t* card;
esp_err_t err = esp_vfs_fat_sdspi_mount("/sdcard",
&host_config, &slot_config, &mount_config, &card);
if (err != ESP_OK) {
printf("%d", err);
}
sdmmc_card_print_info(stdout, card);
FILE *f = fopen("/sdcard/hello.txt", "w");
fprintf(f, "Hello World");
fclose(f);
char buf[25];
f = fopen("/sdcard/hello.txt", "rb");
fgets(buf,sizeof(buf),f);
fclose(f);
printf("%s\n",buf);
return;
}