WIP init sequence

custom
jacqueline 3 years ago
parent a7915753c6
commit 5a8df4f2b9
  1. 22
      main/CMakeLists.txt
  2. 2
      main/dac.hpp
  3. 273
      main/display.cpp
  4. 42
      main/display.hpp
  5. 17
      main/gay-ipod-fw.cpp
  6. 2
      main/gpio-expander.hpp
  7. 2
      main/i2c.cpp
  8. 2
      main/i2c.hpp
  9. 35
      main/playback.cpp
  10. 2
      main/playback.hpp

@ -1,7 +1,15 @@
idf_component_register( idf_component_register(SRCS
SRCS "gay-ipod-fw.cpp"
"gay-ipod-fw.cpp" "dac.cpp" "gpio-expander.cpp" "battery.cpp" "dac.cpp"
"storage.cpp" "i2c.cpp" "playback.cpp" "display.cpp" "gpio-expander.cpp"
INCLUDE_DIRS "." "battery.cpp"
REQUIRES "storage.cpp"
"esp_adc_cal" "fatfs" "audio_pipeline" "audio_stream" "result" "lvgl") "i2c.cpp"
"playback.cpp"
"display.cpp" INCLUDE_DIRS "." REQUIRES
"esp_adc_cal"
"fatfs"
"audio_pipeline"
"audio_stream"
"result"
"lvgl")

@ -2,8 +2,8 @@
#include "gpio-expander.hpp" #include "gpio-expander.hpp"
#include <functional>
#include <stdint.h> #include <stdint.h>
#include <functional>
#include "esp_err.h" #include "esp_err.h"
#include "result.hpp" #include "result.hpp"

@ -1,7 +1,9 @@
#include "display.hpp" #include "display.hpp"
#include <cstdint> #include <cstdint>
#include <cstring>
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "freertos/portable.h"
#include "hal/gpio_types.h" #include "hal/gpio_types.h"
#include "hal/spi_types.h" #include "hal/spi_types.h"
@ -12,52 +14,263 @@ static const gpio_num_t kLedPin = GPIO_NUM_22;
static const uint8_t kDisplayWidth = 128; static const uint8_t kDisplayWidth = 128;
static const uint8_t kDisplayHeight = 160; static const uint8_t kDisplayHeight = 160;
static const uint8_t kDelayBit = 0x80;
auto Display::create(GpioExpander* expander) enum StCommands {
ST77XX_NOP = 0x00,
ST77XX_SWRESET = 0x01,
ST77XX_RDDID = 0x04,
ST77XX_RDDST = 0x09,
ST77XX_SLPIN = 0x10,
ST77XX_SLPOUT = 0x11,
ST77XX_PTLON = 0x12,
ST77XX_NORON = 0x13,
ST77XX_INVOFF = 0x20,
ST77XX_INVON = 0x21,
ST77XX_DISPOFF = 0x28,
ST77XX_DISPON = 0x29,
ST77XX_CASET = 0x2A,
ST77XX_RASET = 0x2B,
ST77XX_RAMWR = 0x2C,
ST77XX_RAMRD = 0x2E,
ST77XX_PTLAR = 0x30,
ST77XX_TEOFF = 0x34,
ST77XX_TEON = 0x35,
ST77XX_MADCTL = 0x36,
ST77XX_COLMOD = 0x3A,
ST77XX_MADCTL_MY = 0x80,
ST77XX_MADCTL_MX = 0x40,
ST77XX_MADCTL_MV = 0x20,
ST77XX_MADCTL_ML = 0x10,
ST77XX_MADCTL_RGB = 0x00,
ST77XX_RDID1 = 0xDA,
ST77XX_RDID2 = 0xDB,
ST77XX_RDID3 = 0xDC,
ST77XX_RDID4 = 0xDD,
ST7735_MADCTL_BGR = 0x08,
ST7735_MADCTL_MH = 0x04,
ST7735_FRMCTR1 = 0xB1,
ST7735_FRMCTR2 = 0xB2,
ST7735_FRMCTR3 = 0xB3,
ST7735_INVCTR = 0xB4,
ST7735_DISSET5 = 0xB6,
ST7735_PWCTR1 = 0xC0,
ST7735_PWCTR2 = 0xC1,
ST7735_PWCTR3 = 0xC2,
ST7735_PWCTR4 = 0xC3,
ST7735_PWCTR5 = 0xC4,
ST7735_VMCTR1 = 0xC5,
ST7735_PWCTR6 = 0xFC,
ST7735_GMCTRP1 = 0xE0,
ST7735_GMCTRN1 = 0xE1,
};
// Based on Adafruit library, which seems to be the most complete.
// clang-format off
static uint8_t kST7735RCommonHeader[]{
15, // 15 commands in list:
ST77XX_SWRESET, kDelayBit, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST77XX_SLPOUT, kDelayBit, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg:
0x07, // No inversion
ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD
ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2,
0x2A, // opamp current small & medium low
ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
0x0E,
ST77XX_INVOFF, 0, // 13: Don't invert display, no args
ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg:
0xC8, // row/col addr, bottom-top refresh
ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
0x05
};
static uint8_t kST7735RCommonGreen[]{
2, // 2 commands in list:
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 127
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01};
static uint8_t kST7735RCommonFooter[]{
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay:
0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides
0x37, 0x32, 0x29, 0x2d, // accurate colors)
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay:
0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides
0x2E, 0x2C, 0x29, 0x2D, // accurate colors)
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST77XX_NORON, kDelayBit, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST77XX_DISPON, kDelayBit, // 4: Main screen turn on, no args w/delay
100
};
// clang-format on
InitialisationData kInitData = {
.num_sequences = 3,
.sequences = {kST7735RCommonHeader, kST7735RCommonGreen,
kST7735RCommonFooter}};
auto Display::create(GpioExpander* expander,
const InitialisationData& init_data)
-> cpp::result<std::unique_ptr<Display>, Error> { -> cpp::result<std::unique_ptr<Display>, Error> {
// First, set up our GPIOs // First, set up our GPIOs
#define SPI_QUADWP_IO (GPIO_NUM_22)
#define SPI_QUADHD_IO (GPIO_NUM_21)
gpio_config_t gpio_cfg = { gpio_config_t gpio_cfg = {
.intr_type = GPIO_INTR_DISABLE, .pin_bit_mask = GPIO_SEL_22 | GPIO_SEL_21,
.mode = GPIO_MODE_OUTPUT, .mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = (1 << GPIO_OUTPUT_22) | (1 << GPIO_OUTPUT_21), .pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = 0, .pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = 0, .intr_type = GPIO_INTR_DISABLE,
} };
gpio_config(&gpio_cfg); gpio_config(&gpio_cfg);
gpio_set_level(kLedPin, 1); gpio_set_level(kLedPin, 1);
gpio_set_level(kCommandOrDataPin, 1); gpio_set_level(kCommandOrDataPin, 0);
// Next, init the SPI device // Next, init the SPI device
auto lock = expander->AcquireSpiBus(GpioExpander::DISPLAY); auto lock = expander->AcquireSpiBus(GpioExpander::DISPLAY);
spi_device_interface_config_t spi_cfg = { spi_device_interface_config_t spi_cfg = {
.command_bits = 0, // Unused .command_bits = 0, // No command phase
.address_bits = 0, // Unused .address_bits = 0, // No address phase
.dummy_bits = 0, .dummy_bits = 0,
.mode = 0, // For ST7789, mode should be 2
.duty_cycle_pos = 0, // Unused .mode = 0,
.cs_ena_pretrans = 0, .duty_cycle_pos = 0, // Unused
.cs_ena_posttrans = 0, .cs_ena_pretrans = 0,
.clock_speed_hz = 32000000, .cs_ena_posttrans = 0,
.input_delay_ns = 0, .clock_speed_hz = SPI_MASTER_FREQ_40M,
.spics_io_num = -1, .input_delay_ns = 0, // TODO: tune?
.flags = 0, .spics_io_num = -1, // TODO: change for R2
.queue_size = 0, .flags = 0,
.pre_cb = NULL, .queue_size = 0,
.post_cb = NULL, .pre_cb = NULL,
.post_cb = NULL,
}; };
spi_device_handle_t handle; spi_device_handle_t handle;
spi_bus_add_device(VSPI_HOST, &spi_cfg, &handle); spi_bus_add_device(VSPI_HOST, &spi_cfg, &handle);
auto display = std::make_unique<Display>(expander, handle);
// Now we reset the display into a known state, then configure it // Now we reset the display into a known state, then configure it
// https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST77xx.cpp
// https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST7735.cpp
// commonInit with Rcmd1
// displayInit with Rcmd2green
// displayInit with Rcmd3
// setRotation // setRotation
for (int i = 0; i < init_data.num_sequences; i++) {
display->SendInitialisationSequence(init_data.sequences[i]);
}
return std::move(display);
}
Display::Display(GpioExpander* gpio, spi_device_handle_t handle)
: gpio_(gpio), handle_(handle) {}
Display::~Display() {
// TODO.
}
void Display::SendInitialisationSequence(uint8_t* data) {
uint8_t commands_remaining, command, num_args;
uint16_t sleep_duration_ms;
// First byte of the data is the number of commands.
commands_remaining = *data;
while (commands_remaining > 0) {
command = *(data++);
num_args = *(data++);
bool has_delay = (num_args & kDelayBit) > 0;
num_args &= ~kDelayBit;
SendCommandWithData(command, data, num_args);
data += num_args;
if (has_delay) {
sleep_duration_ms = *(data++);
if (sleep_duration_ms == 0xFF) {
sleep_duration_ms = 500;
}
vTaskDelay(pdMS_TO_TICKS(sleep_duration_ms));
}
}
}
void Display::SendCommandWithData(uint8_t command,
uint8_t* data,
size_t length) {
gpio_set_level(kCommandOrDataPin, 0);
SendTransaction(&command, 1);
gpio_set_level(kCommandOrDataPin, 1);
SendTransaction(data, length);
}
void Display::SendCmd(uint8_t* data, size_t length) {
gpio_set_level(kCommandOrDataPin, 0);
SendTransaction(data, length);
}
void Display::SendData(uint8_t* data, size_t length) {
gpio_set_level(kCommandOrDataPin, 1);
SendTransaction(data, length);
}
void Display::SendTransaction(uint8_t* data, size_t length) {
if (length == 0) {
return;
}
// TODO: Check if we should malloc this from DMA-capable memory.
spi_transaction_t transaction;
transaction.rx_buffer = NULL;
// Length is in bits, so multiply by 8.
transaction.length = length * 8;
// If the data to transmit is very short, then we can fit it directly
// inside the transaction struct.
if (length < 4) {
transaction.flags = SPI_TRANS_USE_TXDATA;
std::memcpy(&transaction.tx_data, data, length);
} else {
transaction.tx_buffer = data;
}
// TODO: acquire the bus first? Or in an outer scope?
// TODO: fail gracefully
ESP_ERROR_CHECK(spi_device_polling_transmit(handle_, &transaction));
} }
} // namespace gay_ipod } // namespace gay_ipod

@ -1,25 +1,47 @@
#pragma once #pragma once
#include <cstdint>
#include "driver/spi_master.h"
#include "gpio-expander.hpp" #include "gpio-expander.hpp"
#include "result.hpp"
// https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST77xx.cpp
// https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST7735.cpp
namespace gay_ipod { namespace gay_ipod {
struct InitialisationData {
uint8_t num_sequences;
uint8_t* sequences[4];
};
extern InitialisationData kInitData;
/* /*
* Display driver for LVGL. * Display driver for LVGL.
*/ */
class Display { class Display {
public: public:
enum Error {}; enum Error {};
static auto create(GpioExpander* expander) static auto create(GpioExpander* expander,
-> cpp::result<std::unique_ptr<Display>, Error>; const InitialisationData& init_data)
-> cpp::result<std::unique_ptr<Display>, Error>;
Display(GpioExpander* gpio, spi_device_handle_t handle);
~Display();
void WriteData();
private:
GpioExpander* gpio_;
spi_device_handle_t handle_;
Display(GpioExpander *gpio); void SendInitialisationSequence(uint8_t* data);
~Display();
void WriteData(); void SendCommandWithData(uint8_t command, uint8_t* data, size_t length);
private: void SendCmd(uint8_t* data, size_t length);
GpioExpander *gpio_; void SendData(uint8_t* data, size_t length);
void SendTransaction(uint8_t* data, size_t length);
}; };
} // namespace gay_ipod } // namespace gay_ipod

@ -1,13 +1,14 @@
#include "battery.hpp" #include "battery.hpp"
#include "dac.hpp" #include "dac.hpp"
#include "display.hpp"
#include "gpio-expander.hpp" #include "gpio-expander.hpp"
#include "playback.hpp" #include "playback.hpp"
#include "storage.hpp" #include "storage.hpp"
#include <cstdint>
#include <dirent.h> #include <dirent.h>
#include <memory>
#include <stdio.h> #include <stdio.h>
#include <cstdint>
#include <memory>
#include "audio_common.h" #include "audio_common.h"
#include "audio_element.h" #include "audio_element.h"
@ -64,8 +65,8 @@ esp_err_t init_spi(void) {
.mosi_io_num = SPI_SDO_IO, .mosi_io_num = SPI_SDO_IO,
.miso_io_num = SPI_SDI_IO, .miso_io_num = SPI_SDI_IO,
.sclk_io_num = SPI_SCLK_IO, .sclk_io_num = SPI_SCLK_IO,
.quadwp_io_num = -1, //SPI_QUADWP_IO, .quadwp_io_num = -1, // SPI_QUADWP_IO,
.quadhd_io_num = -1, //SPI_QUADHD_IO, .quadhd_io_num = -1, // SPI_QUADHD_IO,
// Unused // Unused
.data4_io_num = -1, .data4_io_num = -1,
@ -126,6 +127,14 @@ extern "C" void app_main(void) {
ESP_LOGI(TAG, "Everything looks good! Waiting a mo for debugger."); ESP_LOGI(TAG, "Everything looks good! Waiting a mo for debugger.");
vTaskDelay(pdMS_TO_TICKS(1500)); vTaskDelay(pdMS_TO_TICKS(1500));
ESP_LOGI(TAG, "Init Display");
auto display_res = gay_ipod::Display::create(&expander, gay_ipod::kInitData);
if (display_res.has_error()) {
ESP_LOGE(TAG, "Failed: %d", display_res.error());
return;
}
std::unique_ptr<gay_ipod::Display> display = std::move(display_res.value());
ESP_LOGI(TAG, "Time to deinit."); ESP_LOGI(TAG, "Time to deinit.");
ESP_LOGI(TAG, "Hooray!"); ESP_LOGI(TAG, "Hooray!");

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <stdint.h>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <mutex> #include <mutex>
#include <stdint.h>
#include <tuple> #include <tuple>
#include <utility> #include <utility>

@ -10,7 +10,7 @@ static constexpr int kCmdLinkSize = I2C_LINK_RECOMMENDED_SIZE(12);
I2CTransaction::I2CTransaction() { I2CTransaction::I2CTransaction() {
// Use a fixed size buffer to avoid many many tiny allocations. // Use a fixed size buffer to avoid many many tiny allocations.
buffer_ = (uint8_t*) calloc(sizeof(uint8_t), kCmdLinkSize); buffer_ = (uint8_t*)calloc(sizeof(uint8_t), kCmdLinkSize);
handle_ = i2c_cmd_link_create_static(buffer_, kCmdLinkSize); handle_ = i2c_cmd_link_create_static(buffer_, kCmdLinkSize);
assert(handle_ != NULL && "failed to create command link"); assert(handle_ != NULL && "failed to create command link");
} }

@ -78,7 +78,7 @@ class I2CTransaction {
private: private:
i2c_cmd_handle_t handle_; i2c_cmd_handle_t handle_;
uint8_t *buffer_; uint8_t* buffer_;
}; };
} // namespace gay_ipod } // namespace gay_ipod

@ -17,7 +17,7 @@ static const i2s_port_t kI2SPort = I2S_NUM_0;
namespace gay_ipod { namespace gay_ipod {
static audio_element_status_t status_from_the_void(void *status) { static audio_element_status_t status_from_the_void(void* status) {
uintptr_t as_pointer_int = reinterpret_cast<uintptr_t>(status); uintptr_t as_pointer_int = reinterpret_cast<uintptr_t>(status);
return static_cast<audio_element_status_t>(as_pointer_int); return static_cast<audio_element_status_t>(as_pointer_int);
} }
@ -182,39 +182,42 @@ void DacAudioPlayback::Pause() {
void DacAudioPlayback::ProcessEvents() { void DacAudioPlayback::ProcessEvents() {
while (1) { while (1) {
audio_event_iface_msg_t event; audio_event_iface_msg_t event;
esp_err_t err = audio_event_iface_listen(event_interface_, &event, portMAX_DELAY); esp_err_t err =
audio_event_iface_listen(event_interface_, &event, portMAX_DELAY);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGI(kTag, "error listening for event:%x", err); ESP_LOGI(kTag, "error listening for event:%x", err);
continue; continue;
} }
ESP_LOGI(kTag, "received event, cmd %i", event.cmd); ESP_LOGI(kTag, "received event, cmd %i", event.cmd);
if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT &&
&& event.source == (void *) mp3_decoder_ event.source == (void*)mp3_decoder_ &&
&& event.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) { event.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
audio_element_info_t music_info = {0}; audio_element_info_t music_info = {0};
audio_element_getinfo(mp3_decoder_, &music_info); audio_element_getinfo(mp3_decoder_, &music_info);
ESP_LOGI(kTag, "sample_rate=%d, bits=%d, ch=%d", music_info.sample_rates, music_info.bits, music_info.channels); ESP_LOGI(kTag, "sample_rate=%d, bits=%d, ch=%d", music_info.sample_rates,
music_info.bits, music_info.channels);
audio_element_setinfo(i2s_stream_writer_, &music_info); audio_element_setinfo(i2s_stream_writer_, &music_info);
i2s_stream_set_clk(i2s_stream_writer_, music_info.sample_rates, music_info.bits, music_info.channels); i2s_stream_set_clk(i2s_stream_writer_, music_info.sample_rates,
music_info.bits, music_info.channels);
} }
if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT &&
&& event.source == (void *) fatfs_stream_reader_ event.source == (void*)fatfs_stream_reader_ &&
&& event.cmd == AEL_MSG_CMD_REPORT_STATUS) { event.cmd == AEL_MSG_CMD_REPORT_STATUS) {
audio_element_status_t status = status_from_the_void(event.data); audio_element_status_t status = status_from_the_void(event.data);
if (status == AEL_STATUS_STATE_FINISHED) { if (status == AEL_STATUS_STATE_FINISHED) {
// TODO: enqueue next track? // TODO: enqueue next track?
} }
} }
if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT if (event.source_type == AUDIO_ELEMENT_TYPE_ELEMENT &&
&& event.source == (void *) i2s_stream_writer_ event.source == (void*)i2s_stream_writer_ &&
&& event.cmd == AEL_MSG_CMD_REPORT_STATUS) { event.cmd == AEL_MSG_CMD_REPORT_STATUS) {
audio_element_status_t status = status_from_the_void(event.data); audio_element_status_t status = status_from_the_void(event.data);
if (status == AEL_STATUS_STATE_FINISHED) { if (status == AEL_STATUS_STATE_FINISHED) {
// TODO. // TODO.
return; return;
} }
} }

@ -13,9 +13,9 @@
#include "audio_pipeline.h" #include "audio_pipeline.h"
#include "esp_err.h" #include "esp_err.h"
#include "fatfs_stream.h" #include "fatfs_stream.h"
#include "result.hpp"
#include "i2s_stream.h" #include "i2s_stream.h"
#include "mp3_decoder.h" #include "mp3_decoder.h"
#include "result.hpp"
namespace gay_ipod { namespace gay_ipod {

Loading…
Cancel
Save