From 29afbb9631991adfe6ef05e6500b72f9f1f608bc Mon Sep 17 00:00:00 2001 From: Renze Nicolai Date: Thu, 30 Dec 2021 00:32:55 +0100 Subject: [PATCH] Add initial steps for hardware init, add PCA9555 IO expander driver --- .gitmodules | 6 ++ factory_test/components/bus-i2c | 1 + factory_test/components/i2c-pca9555 | 1 + factory_test/main/CMakeLists.txt | 10 ++- factory_test/main/hardware.c | 125 ++++++++++++++++++++++++++++ factory_test/main/hardware.h | 68 +++++++++++++++ factory_test/main/main.c | 23 +++-- 7 files changed, 224 insertions(+), 10 deletions(-) create mode 160000 factory_test/components/bus-i2c create mode 160000 factory_test/components/i2c-pca9555 create mode 100644 factory_test/main/hardware.c create mode 100644 factory_test/main/hardware.h diff --git a/.gitmodules b/.gitmodules index 2d47e0f..add00f7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,9 @@ path = esp-idf url = https://github.com/espressif/esp-idf.git branch = master +[submodule "factory_test/components/bus-i2c"] + path = factory_test/components/bus-i2c + url = git@github.com:Nicolai-Electronics/esp32-component-bus-i2c.git +[submodule "factory_test/components/i2c-pca9555"] + path = factory_test/components/i2c-pca9555 + url = git@github.com:Nicolai-Electronics/esp32-component-i2c-pca9555.git diff --git a/factory_test/components/bus-i2c b/factory_test/components/bus-i2c new file mode 160000 index 0000000..3eacf81 --- /dev/null +++ b/factory_test/components/bus-i2c @@ -0,0 +1 @@ +Subproject commit 3eacf810ace52d0b3e11e6d5706adcadf5708626 diff --git a/factory_test/components/i2c-pca9555 b/factory_test/components/i2c-pca9555 new file mode 160000 index 0000000..0350a5b --- /dev/null +++ b/factory_test/components/i2c-pca9555 @@ -0,0 +1 @@ +Subproject commit 0350a5bd92d7848e6f3db2e80992677c98934f58 diff --git a/factory_test/main/CMakeLists.txt b/factory_test/main/CMakeLists.txt index cf2c455..b5c6eaf 100644 --- a/factory_test/main/CMakeLists.txt +++ b/factory_test/main/CMakeLists.txt @@ -1,2 +1,8 @@ -idf_component_register(SRCS "main.c" - INCLUDE_DIRS ".") +idf_component_register( + SRCS + "main.c" + "hardware.c" + + INCLUDE_DIRS + "." +) diff --git a/factory_test/main/hardware.c b/factory_test/main/hardware.c new file mode 100644 index 0000000..fea0edc --- /dev/null +++ b/factory_test/main/hardware.c @@ -0,0 +1,125 @@ +#include "hardware.h" +#include +#include +#include +#include "managed_i2c.h" + +static const char *TAG = "hardware"; + +PCA9555 pca9555; + +void button_handler(uint8_t pin, bool value) { + switch(pin) { + case PCA9555_PIN_BTN_START: + printf("Start button %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_SELECT: + printf("Select button %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_MENU: + printf("Menu button %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_HOME: + printf("Home button %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_JOY_LEFT: + printf("Joystick horizontal %s\n", value ? "left" : "center"); + break; + case PCA9555_PIN_BTN_JOY_PRESS: + printf("Joystick %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_JOY_DOWN: + printf("Joystick vertical %s\n", value ? "down" : "center"); + break; + case PCA9555_PIN_BTN_JOY_UP: + printf("Joy vertical %s\n", value ? "up" : "center"); + break; + case PCA9555_PIN_BTN_JOY_RIGHT: + printf("Joy horizontal %s\n", value ? "right" : "center"); + break; + case PCA9555_PIN_BTN_BACK: + printf("Back button %s\n", value ? "pressed" : "released"); + break; + case PCA9555_PIN_BTN_ACCEPT: + printf("Accept button %s\n", value ? "pressed" : "released"); + break; + default: + printf("Unknown button %d %s\n", pin, value ? "pressed" : "released"); + } +} + +esp_err_t hardware_init() { + esp_err_t res; + + // Interrupts + res = gpio_install_isr_service(0); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Initializing ISR service failed"); + return res; + } + + // SPI bus + spi_bus_config_t busConfiguration = {0}; + busConfiguration.mosi_io_num = GPIO_SPI_MOSI; + busConfiguration.miso_io_num = GPIO_SPI_MISO; + busConfiguration.sclk_io_num = GPIO_SPI_CLK; + busConfiguration.quadwp_io_num = -1; + busConfiguration.quadhd_io_num = -1; + busConfiguration.max_transfer_sz = SPI_MAX_TRANSFER_SIZE; + res = spi_bus_initialize(SPI_BUS, &busConfiguration, SPI_DMA_CHANNEL); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Initializing SPI bus failed"); + return res; + } + + // System I2C bus + res = i2c_init(I2C_BUS_SYS, GPIO_I2C_SYS_SDA, GPIO_I2C_SYS_SCL, I2C_SPEED_SYS, false, false); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Initializing system I2C bus failed"); + return res; + } + + // PCA9555 IO expander on system I2C bus + res = pca9555_init(&pca9555, I2C_BUS_SYS, PCA9555_ADDR, GPIO_INT_PCA9555); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Initializing PCA9555 failed"); + return res; + } + + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_START, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_SELECT, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_MENU, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_HOME, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_JOY_LEFT, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_JOY_PRESS, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_JOY_DOWN, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_JOY_UP, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_JOY_RIGHT, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_BACK, true); + pca9555_set_gpio_polarity(&pca9555, PCA9555_PIN_BTN_ACCEPT, true); + + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_START, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_SELECT, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_MENU, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_HOME, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_JOY_LEFT, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_JOY_PRESS, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_JOY_DOWN, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_JOY_UP, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_JOY_RIGHT, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_BACK, button_handler); + pca9555_set_interrupt_handler(&pca9555, PCA9555_PIN_BTN_ACCEPT, button_handler); + + // User I2C bus + res = i2c_init(I2C_BUS_EXT, GPIO_I2C_EXT_SDA, GPIO_I2C_EXT_SCL, I2C_SPEED_EXT, false, false); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Initializing user I2C bus failed"); + return res; + } + + return res; +} + +PCA9555* get_pca9555() { + return &pca9555; +} diff --git a/factory_test/main/hardware.h b/factory_test/main/hardware.h new file mode 100644 index 0000000..4c23af4 --- /dev/null +++ b/factory_test/main/hardware.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include "pca9555.h" + +esp_err_t hardware_init(); +PCA9555* get_pca9555(); + +// Interrupts +#define GPIO_INT_STM32 0 +#define GPIO_INT_PCA9555 34 +#define GPIO_INT_ACCEL 36 +#define GPIO_INT_FPGA 39 + +// SD card +#define SD_PWR 5 +#define SD_D0 2 +#define SD_CLK 14 +#define SD_CMD 15 + +// I2S audio +#define GPIO_I2S_CLK 14 +#define GPIO_I2S_DATA 13 +#define GPIO_I2S_LR 4 + +// System I2C bus +#define GPIO_I2C_SYS_SCL 21 +#define GPIO_I2C_SYS_SDA 22 +#define I2C_BUS_SYS 0 +#define I2C_SPEED_SYS 40000 + +// PCA9555 IO expander +#define PCA9555_ADDR 0x26 +#define PCA9555_PIN_STM32_RESET 0 +#define PCA9555_PIN_STM32_BOOT0 1 +#define PCA9555_PIN_FPGA_RESET 2 +#define PCA9555_PIN_FPGA_CDONE 3 +#define PCA9555_PIN_BTN_START 5 +#define PCA9555_PIN_BTN_SELECT 6 +#define PCA9555_PIN_BTN_MENU 7 +#define PCA9555_PIN_BTN_HOME 8 +#define PCA9555_PIN_BTN_JOY_LEFT 9 +#define PCA9555_PIN_BTN_JOY_PRESS 10 +#define PCA9555_PIN_BTN_JOY_DOWN 11 +#define PCA9555_PIN_BTN_JOY_UP 12 +#define PCA9555_PIN_BTN_JOY_RIGHT 13 +#define PCA9555_PIN_BTN_BACK 14 +#define PCA9555_PIN_BTN_ACCEPT 15 + +// User I2C bus +#define GPIO_I2C_EXT_SCL 25 +#define GPIO_I2C_EXT_SDA 26 +#define I2C_BUS_EXT 1 +#define I2C_SPEED_EXT 40000 + +// SPI bus +#define GPIO_SPI_CLK 18 +#define GPIO_SPI_MOSI 23 +#define GPIO_SPI_MISO 35 +#define GPIO_SPI_CS_STM32 19 +#define GPIO_SPI_CS_FPGA 27 +#define GPIO_SPI_CS_LCD 32 +#define GPIO_SPI_DC_LCD 33 +#define SPI_BUS VSPI_HOST +#define SPI_MAX_TRANSFER_SIZE 4094 +#define SPI_DMA_CHANNEL 2 diff --git a/factory_test/main/main.c b/factory_test/main/main.c index 08b1191..1f0b79d 100644 --- a/factory_test/main/main.c +++ b/factory_test/main/main.c @@ -1,9 +1,12 @@ #include -#include "sdkconfig.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_system.h" -#include "esp_spi_flash.h" +#include +#include +#include +#include +#include +#include +#include "hardware.h" +#include "pca9555.h" void restart() { for (int i = 3; i >= 0; i--) { @@ -16,7 +19,13 @@ void restart() { } void app_main(void) { - printf("Hello world!\n"); + esp_err_t res; + + res = hardware_init(); + if (res != ESP_OK) { + printf("Failed to initialize hardware!\n"); + restart(); + } /* Print chip information */ esp_chip_info_t chip_info; @@ -33,6 +42,4 @@ void app_main(void) { (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size()); - - restart(); }