#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
// Define the SPI bus and device configurations
#define SPI_BUS VSPI_HOST
#define MAX7219_CS_PIN GPIO_NUM_5
// Define the SPI transaction configuration
spi_device_handle_t spi;
void initialize_spi() {
// SPI bus configuration
spi_bus_config_t bus_config = {
.mosi_io_num = GPIO_NUM_27, // MOSI pin
.sclk_io_num = GPIO_NUM_12, // CLK pin
};
// Initialize the SPI bus
spi_bus_initialize(SPI_BUS, &bus_config, 0);
// SPI device configuration
spi_device_interface_config_t dev_config = {
.mode = 0, // SPI mode 0
.clock_speed_hz = 10000000, // Clock speed (1 MHz)
.spics_io_num = GPIO_NUM_14, // CS pin
.queue_size = 1, // Queue size
};
// Add the SPI device to the bus
spi_bus_add_device(SPI_BUS, &dev_config, &spi);
}
void set_first_dot() {
uint8_t data[2] = {0x08, 0xFF}; // Address of the first digit (DIG0) and value to display (binary 1)
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 16; // Total data length, in bits
t.tx_buffer = data; // Data buffer to transmit
// Transmit the data
spi_device_transmit(spi, &t);
}
extern "C" void app_main() {
initialize_spi();
set_first_dot();
vTaskDelay(1000 / portTICK_RATE_MS); // Delay for 1 second to see the dot
// Clean up and free SPI device
spi_bus_remove_device(spi);
}