Here is a comprehensive internal test for the ESP32 with semaphores and interrupts:
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/uart.h"
#include "driver/gpio.h"
SemaphoreHandle_t mutex;
void get_info(void *pvParameters)
{
char buf[100];
while(1) {
// Take mutex
xSemaphoreTake(mutex, portMAX_DELAY);
// Print system info
sprintf(buf, "Cores: %d\n", esp_cpu_cores_num());
uart_write_bytes(UART_NUM_1, buf, strlen(buf));
sprintf(buf, "Clock: %d MHz\n", esp_clk_cpu_freq() / 1000000);
uart_write_bytes(UART_NUM_1, buf, strlen(buf));
sprintf(buf, "Free heap: %d bytes\n", esp_get_free_heap_size());
uart_write_bytes(UART_NUM_1, buf, strlen(buf));
sprintf(buf, "Min free heap: %d bytes\n", esp_get_minimum_free_heap_size());
uart_write_bytes(UART_NUM_1, buf, strlen(buf));
uint32_t id = 0;
spi_flash_get_chip_size(&id);
sprintf(buf, "Flash chip size: %d MB\n", id / (1024 * 1024));
uart_write_bytes(UART_NUM_1, buf, strlen(buf));
// Release mutex
xSemaphoreGive(mutex);
}
}
void IRAM_ATTR gpio_isr_handler(void* arg)
{
// Take mutex
xSemaphoreTakeFromISR(mutex, NULL);
char* cmd = (char*) arg;
if (strcmp(cmd, "gp") == 0) {
uart_write_bytes(UART_NUM_1, "ESP32 POST info:\n", strlen("ESP32 POST info:\n"));
}
// Release mutex
xSemaphoreGiveFromISR(mutex, NULL);
}
void app_main()
{
// Create mutex
mutex = xSemaphoreCreateMutex();
// Start info printing task
xTaskCreate(&get_info, "get_info", 2048, NULL, 5, NULL);
// Attach interrupt for "gp" command
gpio_install_isr_service(0);
gpio_isr_handler_add(GPIO_NUM_13, gpio_isr_handler, (void*)"gp");
// Loop forever
while(1) { }
}
Here is a Python script to send the "gp" command and get the info:
import serial
ser = serial.Serial('COM3', 115200)
ser.write(b'gp\n')
info = ''
while True:
line = ser.readline()
if line == b'ESP32 POST info:\n':
break
info += line.decode()
print(info)
This will print the ESP32 system info like:
Cores: 2
Clock: 240 MHz
Free heap: 307184 bytes
Min free heap: 153600 bytes
Flash chip size: 4 MB