From 0002b64533fe91ce66fab7275f1bb1a986e28c40 Mon Sep 17 00:00:00 2001 From: nopnop2002 Date: Mon, 25 Mar 2024 22:28:55 +0900 Subject: [PATCH] Added spi_clock_speed function --- README.md | 21 +++++++++++++++++++++ components/ssd1306/ssd1306.h | 3 +++ components/ssd1306/ssd1306_spi.c | 12 ++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a0e62f..e570526 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,11 @@ Right:0.96 inch SSD1306 --- +# I2C Clock speed +According to the SSD1306 datasheet, the minimum i2c clock cycle time is 2.5us. +Therefore, the maximum i2c clock frequency is 400KHz. +The i2c clock frequency used by this project is 400KHz. + # I2C Port selection ![config-i2c-port](https://github.com/nopnop2002/esp-idf-ssd1306/assets/6020549/7a7de5ec-ef20-42ac-ba70-73ba74a762a7) @@ -119,6 +124,22 @@ If you use this module at the same time as another I2C device using I2C port 0, --- +# SPI Clock speed +According to the SSD1306 datasheet, the minimum SPI clock cycle time is 100ns. +Therefore, the maximum SPI clock frequency is 10MHz. +The SPI clock frequency used by this project is 1MHz. +Higher SPI clock frequencies can be specified using ```spi_clock_speed()```. +``` + //int speed = 1000000; // 1MHz + //int speed = 2000000; // 2MHz + //int speed = 4000000; // 4MHz + //int speed = 6000000; // 6MHz + //int speed = 8000000; // 8MHz + int speed = 10000000; // 10MHz + spi_clock_speed(speed); + spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO); +``` + # SPI BUS selection ![config-spi-bus](https://user-images.githubusercontent.com/6020549/202815807-6c2df14f-f38e-4032-94fb-da1723607279.jpg) diff --git a/components/ssd1306/ssd1306.h b/components/ssd1306/ssd1306.h index 8b4f3dc..45010ec 100644 --- a/components/ssd1306/ssd1306.h +++ b/components/ssd1306/ssd1306.h @@ -134,6 +134,9 @@ void ssd1306_fadeout(SSD1306_t * dev); void ssd1306_dump(SSD1306_t dev); void ssd1306_dump_page(SSD1306_t * dev, int page, int seg); +#if CONFIG_SPI_INTERFACE +void spi_clock_speed(int speed); +#endif void i2c_master_init(SSD1306_t * dev, int16_t sda, int16_t scl, int16_t reset); void i2c_init(SSD1306_t * dev, int width, int height); void i2c_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width); diff --git a/components/ssd1306/ssd1306_spi.c b/components/ssd1306/ssd1306_spi.c index 220de18..8207e0e 100644 --- a/components/ssd1306/ssd1306_spi.c +++ b/components/ssd1306/ssd1306_spi.c @@ -20,7 +20,14 @@ #define SPI_COMMAND_MODE 0 #define SPI_DATA_MODE 1 -#define SPI_FREQUENCY 1000000; // 1MHz +#define SPI_DEFAULT_FREQUENCY 1000000; // 1MHz + +int clock_speed_hz = SPI_DEFAULT_FREQUENCY; + +void spi_clock_speed(int speed) { + ESP_LOGI(TAG, "SPI clock speed=%d MHz", speed/1000000); + clock_speed_hz = speed; +} void spi_master_init(SSD1306_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t GPIO_CS, int16_t GPIO_DC, int16_t GPIO_RESET) { @@ -59,7 +66,8 @@ void spi_master_init(SSD1306_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int1 spi_device_interface_config_t devcfg; memset( &devcfg, 0, sizeof( spi_device_interface_config_t ) ); - devcfg.clock_speed_hz = SPI_FREQUENCY; + //devcfg.clock_speed_hz = SPI_DEFAULT_FREQUENCY; + devcfg.clock_speed_hz = clock_speed_hz; devcfg.spics_io_num = GPIO_CS; devcfg.queue_size = 1;