27 #if defined(SSD1306_ESP_PLATFORM) 31 #include "freertos/FreeRTOS.h" 32 #include "freertos/task.h" 33 #include "driver/gpio.h" 37 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 38 static void platform_spi_send_cache();
42 int digitalRead(
int pin)
44 return gpio_get_level(pin);
47 void digitalWrite(
int pin,
int level)
49 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 52 platform_spi_send_cache();
55 gpio_set_level(pin, level);
58 void pinMode(
int pin,
int mode)
61 gpio_set_direction(pin, GPIO_MODE_INPUT);
62 else if (mode == OUTPUT)
63 gpio_set_direction(pin, GPIO_MODE_OUTPUT);
68 return xTaskGetTickCount() * portTICK_PERIOD_MS;
71 void delay(uint32_t ms)
73 vTaskDelay(ms / portTICK_PERIOD_MS);
80 #if defined(CONFIG_PLATFORM_I2C_AVAILABLE) && defined(CONFIG_PLATFORM_I2C_ENABLE) 83 #include "driver/i2c.h" 85 static uint8_t s_i2c_addr = 0x3C;
86 static int8_t s_bus_id;
88 static i2c_cmd_handle_t s_cmd_handle;
90 static void platform_i2c_start(
void)
93 s_cmd_handle = i2c_cmd_link_create();
94 i2c_master_start(s_cmd_handle);
95 i2c_master_write_byte(s_cmd_handle, ( s_i2c_addr << 1 ) | I2C_MASTER_WRITE, 0x1);
98 static void platform_i2c_stop(
void)
101 i2c_master_stop(s_cmd_handle);
102 i2c_master_cmd_begin(s_bus_id, s_cmd_handle, 1000 / portTICK_RATE_MS);
103 i2c_cmd_link_delete(s_cmd_handle);
106 static void platform_i2c_send(uint8_t data)
109 i2c_master_write_byte(s_cmd_handle, data, 0x1);
112 static void platform_i2c_close(
void)
115 i2c_driver_delete(s_bus_id);
118 static void platform_i2c_send_buffer(
const uint8_t *data, uint16_t len)
123 platform_i2c_send(*data);
131 if (addr) s_i2c_addr = addr;
139 if ( busId < 0) busId = I2C_NUM_1;
142 conf.mode = I2C_MODE_MASTER;
143 conf.sda_io_num = cfg->
sda >= 0 ? cfg->
sda : 21;
144 conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
145 conf.scl_io_num = cfg->
scl >= 0 ? cfg->
scl : 22;
146 conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
147 conf.master.clk_speed = 400000;
148 i2c_param_config(s_bus_id, &conf);
149 i2c_driver_install(s_bus_id, conf.mode, 0, 0, 0);
157 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 160 #include "driver/spi_master.h" 163 static spi_device_handle_t s_spi;
164 static int8_t s_spi_bus_id;
168 static uint8_t s_first_spi_session = 0;
169 static uint8_t s_spi_cache[1024];
170 static int s_spi_cached_count = 0;
172 static void platform_spi_send_cache()
177 uint8_t *data = s_spi_cache;
178 while (s_spi_cached_count)
180 size_t sz = s_spi_cached_count > 32 ? 32: s_spi_cached_count;
182 memset(&t, 0,
sizeof(t));
188 spi_device_transmit(s_spi, &t);
190 s_spi_cached_count-=sz;
192 s_spi_cached_count = 0;
195 static void platform_spi_start(
void)
198 if (s_first_spi_session)
200 spi_device_interface_config_t devcfg=
207 spi_bus_add_device(s_spi_bus_id ? VSPI_HOST : HSPI_HOST, &devcfg, &s_spi);
208 s_first_spi_session = 0;
210 s_spi_cached_count = 0;
213 static void platform_spi_stop(
void)
217 platform_spi_send_cache();
220 static void platform_spi_send(uint8_t data)
222 s_spi_cache[s_spi_cached_count] = data;
223 s_spi_cached_count++;
224 if ( s_spi_cached_count >=
sizeof( s_spi_cache ) )
226 platform_spi_send_cache();
230 static void platform_spi_close(
void)
233 if (!s_first_spi_session)
235 spi_bus_remove_device( s_spi );
237 spi_bus_free( s_spi_bus_id ? VSPI_HOST : HSPI_HOST );
240 static void platform_spi_send_buffer(
const uint8_t *data, uint16_t len)
244 platform_i2c_send(*data);
254 if (busId < 0) busId = 1;
255 s_spi_bus_id = busId;
260 cesPin = s_spi_bus_id ? 5 : 15;
265 if (cesPin >=0) pinMode(cesPin, OUTPUT);
266 if (dcPin >= 0) pinMode(dcPin, OUTPUT);
276 spi_bus_config_t buscfg=
278 .miso_io_num= s_spi_bus_id ? 19 : 12,
279 .mosi_io_num= s_spi_bus_id ? 23 : 13,
280 .sclk_io_num= s_spi_bus_id ? 18 : 14,
285 spi_bus_initialize(s_spi_bus_id ? VSPI_HOST : HSPI_HOST, &buscfg, 0);
286 s_first_spi_session = 1;
290 #endif // SSD1306_ESP_PLATFORM
void ssd1306_platform_spiInit(int8_t busId, int8_t cesPin, int8_t dcPin)
Initializes spi interface for platform being used.
void(* send)(uint8_t data)
uint32_t s_ssd1306_spi_clock
void ssd1306_platform_i2cInit(int8_t busId, uint8_t addr, ssd1306_platform_i2cConfig_t *cfg)
Initializes i2c interface for platform being used.
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_interface_t ssd1306_intf
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.