mch2022-talktopics/main/main.c

240 lines
7.3 KiB
C
Raw Normal View History

#include <stdio.h>
2022-01-01 04:51:12 +00:00
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2022-04-08 21:15:57 +00:00
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include <nvs_flash.h>
#include <nvs.h>
2022-05-03 18:44:45 +00:00
#include "driver/uart.h"
#include "hardware.h"
#include "managed_i2c.h"
#include "pax_gfx.h"
#include "sdcard.h"
#include "appfs.h"
2022-01-24 21:32:33 +00:00
#include "rp2040.h"
#include "rp2040bl.h"
2022-01-24 21:32:33 +00:00
2022-04-08 21:15:57 +00:00
#include "fpga_test.h"
#include "menu.h"
2022-04-15 23:16:36 +00:00
#include "system_wrapper.h"
#include "graphics_wrapper.h"
#include "appfs_wrapper.h"
2022-04-16 00:14:28 +00:00
#include "settings.h"
2022-04-18 00:10:44 +00:00
#include "wifi_connection.h"
#include "rp2040_updater.h"
2022-02-04 00:04:51 +00:00
2022-04-23 02:00:55 +00:00
#include "ws2812.h"
#include "esp32/rom/crc.h"
2022-05-12 20:41:31 +00:00
#include "efuse.h"
2022-05-13 22:19:15 +00:00
#include "wifi_ota.h"
#include "esp_vfs.h"
2022-05-14 22:15:45 +00:00
#include "esp_vfs_fat.h"
2022-05-30 21:56:23 +00:00
#include <pax_codecs.h>
#include "audio.h"
#include "bootscreen.h"
#include "menus/start.h"
extern const uint8_t wallpaper_png_start[] asm("_binary_wallpaper_png_start");
extern const uint8_t wallpaper_png_end[] asm("_binary_wallpaper_png_end");
extern const uint8_t logo_screen_png_start[] asm("_binary_logo_screen_png_start");
extern const uint8_t logo_screen_png_end[] asm("_binary_logo_screen_png_end");
2022-05-16 21:07:30 +00:00
static const char *TAG = "main";
void display_fatal_error(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* line0, const char* line1, const char* line2, const char* line3) {
pax_noclip(pax_buffer);
pax_background(pax_buffer, 0xa85a32);
if (line0 != NULL) pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, line0);
if (line1 != NULL) pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, line1);
if (line2 != NULL) pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*2, line2);
if (line3 != NULL) pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*3, line3);
ili9341_write(ili9341, pax_buffer->buf);
}
void app_main(void) {
esp_err_t res;
2022-05-24 12:35:05 +00:00
2022-04-15 23:16:36 +00:00
/* Initialize memory */
uint8_t* framebuffer = heap_caps_malloc(ILI9341_BUFFER_SIZE, MALLOC_CAP_8BIT);
if (framebuffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate framebuffer");
esp_restart();
}
memset(framebuffer, 0, ILI9341_BUFFER_SIZE);
2022-05-24 12:35:05 +00:00
2022-04-15 23:16:36 +00:00
pax_buf_t* pax_buffer = malloc(sizeof(pax_buf_t));
2022-02-04 00:04:51 +00:00
if (framebuffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate buffer for PAX graphics library");
esp_restart();
2022-02-04 00:04:51 +00:00
}
memset(pax_buffer, 0, sizeof(pax_buf_t));
2022-05-24 12:35:05 +00:00
2022-02-04 00:04:51 +00:00
pax_buf_init(pax_buffer, framebuffer, ILI9341_WIDTH, ILI9341_HEIGHT, PAX_BUF_16_565RGB);
2022-05-24 12:35:05 +00:00
2022-04-15 23:16:36 +00:00
/* Initialize hardware */
2022-05-24 12:35:05 +00:00
2022-05-12 20:41:31 +00:00
efuse_protect();
2022-05-24 12:35:05 +00:00
if (bsp_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize basic board support functions");
esp_restart();
2022-01-23 00:27:22 +00:00
}
2022-05-24 12:35:05 +00:00
2022-04-15 23:16:36 +00:00
ILI9341* ili9341 = get_ili9341();
if (ili9341 == NULL) {
ESP_LOGE(TAG, "ili9341 is NULL");
esp_restart();
}
display_boot_screen(pax_buffer, ili9341, "Starting...");
audio_init();
2022-05-24 12:35:05 +00:00
if (bsp_rp2040_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the RP2040 co-processor");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "RP2040 co-processor error", NULL, NULL);
esp_restart();
}
2022-05-24 12:35:05 +00:00
RP2040* rp2040 = get_rp2040();
if (rp2040 == NULL) {
ESP_LOGE(TAG, "rp2040 is NULL");
esp_restart();
}
rp2040_updater(rp2040, pax_buffer, ili9341); // Handle RP2040 firmware update & bootloader mode
2022-05-15 20:01:40 +00:00
/*uint8_t rp2040_uid[8];
2022-05-15 20:01:40 +00:00
if (rp2040_get_uid(rp2040, rp2040_uid) != ESP_OK) {
ESP_LOGE(TAG, "Failed to get RP2040 UID");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "Failed to read UID", NULL, NULL);
esp_restart();
}
printf("RP2040 UID: %02X%02X%02X%02X%02X%02X%02X%02X\n", rp2040_uid[0], rp2040_uid[1], rp2040_uid[2], rp2040_uid[3], rp2040_uid[4], rp2040_uid[5], rp2040_uid[6], rp2040_uid[7]);*/
2022-05-30 21:56:23 +00:00
if (bsp_ice40_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the ICE40 FPGA");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "ICE40 FPGA error", NULL, NULL);
esp_restart();
}
2022-05-24 12:35:05 +00:00
2022-04-15 23:16:36 +00:00
ICE40* ice40 = get_ice40();
if (ice40 == NULL) {
ESP_LOGE(TAG, "ice40 is NULL");
esp_restart();
}
2022-05-30 21:56:23 +00:00
/*display_boot_screen(pax_buffer, ili9341, "Initializing BNO055...");
2022-05-24 12:35:05 +00:00
if (bsp_bno055_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the BNO055 position sensor");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BNO055 sensor error", "Check I2C bus", "Remove SAO and try again");
esp_restart();
}
2022-04-15 23:16:36 +00:00
BNO055* bno055 = get_bno055();
if (bno055 == NULL) {
ESP_LOGE(TAG, "bno055 is NULL");
esp_restart();
}*/
2022-04-08 21:15:57 +00:00
/*display_boot_screen(pax_buffer, ili9341, "Initializing BME680...");
2022-05-30 21:56:23 +00:00
2022-05-18 21:28:00 +00:00
if (bsp_bme680_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the BME680 position sensor");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BME680 sensor error", "Check I2C bus", "Remove SAO and try again");
esp_restart();
}
BME680* bme680 = get_bme680();
if (bme680 == NULL) {
ESP_LOGE(TAG, "bme680 is NULL");
esp_restart();
}*/
2022-05-30 21:56:23 +00:00
//display_boot_screen(pax_buffer, ili9341, "Initializing AppFS...");
2022-05-18 21:28:00 +00:00
2022-05-12 20:41:31 +00:00
/* Start AppFS */
2022-01-06 00:12:28 +00:00
res = appfs_init();
if (res != ESP_OK) {
ESP_LOGE(TAG, "AppFS init failed: %d", res);
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "AppFS failed to initialize", "Flash may be corrupted", NULL);
esp_restart();
2022-01-06 00:12:28 +00:00
}
2022-05-30 21:56:23 +00:00
//display_boot_screen(pax_buffer, ili9341, "Initializing NVS...");
2022-05-24 12:35:05 +00:00
2022-05-12 20:41:31 +00:00
/* Start NVS */
2022-04-16 00:14:28 +00:00
res = nvs_init();
if (res != ESP_OK) {
ESP_LOGE(TAG, "NVS init failed: %d", res);
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "NVS failed to initialize", "Flash may be corrupted", NULL);
esp_restart();
2022-04-16 00:14:28 +00:00
}
2022-05-24 12:35:05 +00:00
//display_boot_screen(pax_buffer, ili9341, "Initializing filesystem...");
2022-05-30 21:56:23 +00:00
2022-05-14 22:15:45 +00:00
/* Start internal filesystem */
const esp_partition_t* fs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "locfd");
2022-05-24 12:35:05 +00:00
2022-05-14 22:15:45 +00:00
wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
2022-05-24 12:35:05 +00:00
2022-05-14 22:15:45 +00:00
if (fs_partition != NULL) {
const esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 0,
};
esp_err_t res = esp_vfs_fat_spiflash_mount("/internal", "locfd", &mount_config, &s_wl_handle);
if (res != ESP_OK) {
ESP_LOGE(TAG, "failed to mount locfd (%d)", res);
} else {
ESP_LOGI(TAG, "Internal filesystem mounted");
}
} else {
ESP_LOGE(TAG, "locfd partition not found");
}
2022-05-24 12:35:05 +00:00
2022-05-14 22:15:45 +00:00
/* Start SD card filesystem */
2022-05-15 01:02:49 +00:00
res = mount_sd(GPIO_SD_CMD, GPIO_SD_CLK, GPIO_SD_D0, GPIO_SD_PWR, "/sd", false, 5);
2022-01-06 00:12:28 +00:00
bool sdcard_ready = (res == ESP_OK);
if (sdcard_ready) {
2022-05-14 22:15:45 +00:00
ESP_LOGI(TAG, "SD card filesystem mounted");
}
2022-05-12 20:41:31 +00:00
/* Start LEDs */
2022-04-23 02:00:55 +00:00
ws2812_init(GPIO_LED_DATA);
2022-04-29 03:24:17 +00:00
uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0};
2022-04-23 02:00:55 +00:00
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
2022-05-24 12:35:05 +00:00
/* Start WiFi */
wifi_init();
/* Rick that roll */
play_bootsound();
2022-05-12 20:41:31 +00:00
/* Launcher menu */
2022-04-15 23:16:36 +00:00
while (true) {
2022-05-30 21:56:23 +00:00
menu_start(rp2040->queue, pax_buffer, ili9341);
}
2022-04-08 21:15:57 +00:00
free(framebuffer);
}