25 #if defined(__linux__) && !defined(ARDUINO) 36 #include <sys/types.h> 42 #include <sys/ioctl.h> 43 #include <linux/i2c-dev.h> 44 #include <linux/spi/spidev.h> 46 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) \ 47 && !defined(SDL_EMULATION) 48 #define LINUX_SPI_AVAILABLE 51 #define MAX_GPIO_COUNT 256 63 #ifdef LINUX_SPI_AVAILABLE 64 static void platform_spi_send_cache();
67 int gpio_export(
int pin)
70 ssize_t bytes_written;
74 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d", pin);
76 if (access(path, F_OK) == 0)
81 fd = open(
"/sys/class/gpio/export", O_WRONLY);
84 fprintf(stderr,
"Failed to allocate gpio pin resources[%d]: %s!\n", pin, strerror (errno));
88 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
89 if (write(fd, buffer, bytes_written) < 0)
91 fprintf(stderr,
"Failed to allocate gpio pin resources[%d]: %s!\n", pin, strerror (errno));
99 int gpio_unexport(
int pin)
102 ssize_t bytes_written;
105 fd = open(
"/sys/class/gpio/unexport", O_WRONLY);
108 fprintf(stderr,
"Failed to free gpio pin resources!\n");
112 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
113 if (write(fd, buffer, bytes_written) < 0)
115 fprintf(stderr,
"Failed to free gpio pin resources!\n");
121 int gpio_direction(
int pin,
int dir)
123 static const char s_directions_str[] =
"in\0out";
128 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/direction", pin);
129 fd = open(path, O_WRONLY);
132 fprintf(stderr,
"Failed to set gpio pin direction1[%d]: %s!\n", pin, strerror(errno));
136 if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3))
138 fprintf(stderr,
"Failed to set gpio pin direction2[%d]: %s!\n", pin, strerror(errno));
146 int gpio_read(
int pin)
152 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
153 fd = open(path, O_RDONLY);
156 fprintf(stderr,
"Failed to read gpio pin value!\n");
160 if (-1 == read(fd, value_str, 3))
162 fprintf(stderr,
"Failed to read gpio pin value!\n");
168 return(atoi(value_str));
171 int gpio_write(
int pin,
int value)
173 static const char s_values_str[] =
"01";
178 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
179 fd = open(path, O_WRONLY);
182 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s!\n", pin, strerror(errno));
186 if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1))
188 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s!\n", pin, strerror (errno));
196 #if !defined(SDL_EMULATION) 198 static uint8_t s_exported_pin[MAX_GPIO_COUNT] = {0};
199 static uint8_t s_pin_mode[MAX_GPIO_COUNT] = {0};
201 void pinMode(
int pin,
int mode)
203 if (!s_exported_pin[pin])
205 if ( gpio_export(pin)<0 )
209 s_exported_pin[pin] = 1;
213 gpio_direction(pin, OUT);
218 gpio_direction(pin, IN);
223 void digitalWrite(
int pin,
int level)
225 #ifdef LINUX_SPI_AVAILABLE 228 platform_spi_send_cache();
232 if (!s_exported_pin[pin])
234 if ( gpio_export(pin)<0 )
238 s_exported_pin[pin] = 1;
240 if (!s_pin_mode[pin])
242 pinMode(pin, OUTPUT);
244 gpio_write( pin, level );
247 #endif // SDL_EMULATION 252 #if defined(CONFIG_PLATFORM_I2C_AVAILABLE) && defined(CONFIG_PLATFORM_I2C_ENABLE) 256 #if !defined(SDL_EMULATION) 260 static int s_fd = -1;
261 static uint8_t s_buffer[128];
262 static uint8_t s_dataSize = 0;
264 static void platform_i2c_start(
void)
269 static void platform_i2c_stop(
void)
271 if (write(s_fd, s_buffer, s_dataSize) != s_dataSize)
273 fprintf(stderr,
"Failed to write to the i2c bus: %s.\n", strerror(errno));
278 static void platform_i2c_send(uint8_t data)
280 s_buffer[s_dataSize] = data;
282 if (s_dataSize ==
sizeof(s_buffer))
292 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
296 platform_i2c_send(*buffer);
301 static void platform_i2c_close()
310 static void empty_function()
314 static void empty_function_single_arg(uint8_t arg)
318 static void empty_function_two_args(
const uint8_t *arg1, uint16_t arg2)
329 snprintf(filename, 19,
"/dev/i2c-%d", busId);
335 if ((s_fd = open(filename, O_RDWR)) < 0)
337 fprintf(stderr,
"Failed to open the i2c bus\n");
344 if (ioctl(s_fd, I2C_SLAVE, s_sa) < 0)
346 fprintf(stderr,
"Failed to acquire bus access and/or talk to slave.\n");
358 #include "sdl_core.h" 360 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
364 sdl_send_byte(*buffer);
382 #endif // CONFIG_PLATFORM_I2C_AVAILABLE 388 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 390 #if !defined(SDL_EMULATION) 392 static int s_spi_fd = -1;
394 static uint8_t s_spi_cache[1024];
395 static int s_spi_cached_count = 0;
397 static void platform_spi_start(
void)
399 s_spi_cached_count = 0;
402 static void platform_spi_stop(
void)
404 platform_spi_send_cache();
407 static void platform_spi_send_cache()
412 if ( s_spi_cached_count == 0 )
416 struct spi_ioc_transfer mesg;
417 memset(&mesg, 0,
sizeof mesg);
418 mesg.tx_buf = (
unsigned long)&s_spi_cache[0];
420 mesg.len = s_spi_cached_count;
421 mesg.delay_usecs = 0;
423 mesg.bits_per_word = 8;
425 if (ioctl(s_spi_fd, SPI_IOC_MESSAGE(1), &mesg) < 1)
427 fprintf(stderr,
"SPI failed to send SPI message: %s\n", strerror (errno)) ;
429 s_spi_cached_count = 0;
432 static void platform_spi_send(uint8_t data)
434 s_spi_cache[s_spi_cached_count] = data;
435 s_spi_cached_count++;
436 if ( s_spi_cached_count >=
sizeof( s_spi_cache ) )
438 platform_spi_send_cache();
442 static void platform_spi_close(
void)
451 static void platform_spi_send_buffer(
const uint8_t *data, uint16_t len)
455 platform_spi_send(*data);
460 static void empty_function_spi(
void)
464 static void empty_function_arg_spi(uint8_t byte)
468 static void empty_function_args_spi(
const uint8_t *buffer, uint16_t bytes)
494 snprintf(filename, 19,
"/dev/spidev%d.%d", busId, ces);
495 if ((s_spi_fd = open(filename, O_RDWR)) < 0)
497 printf(
"Failed to initialize SPI: %s!\n", strerror(errno));
501 if (ioctl(s_spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
503 printf(
"Failed to set speed on SPI line: %s!\n", strerror(errno));
505 uint8_t mode = SPI_MODE_0;
506 if (ioctl (s_spi_fd, SPI_IOC_WR_MODE, &mode) < 0)
508 printf(
"Failed to set SPI mode: %s!\n", strerror(errno));
511 if (ioctl (s_spi_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bpw) < 0)
513 printf(
"Failed to set SPI BPW: %s!\n", strerror(errno));
526 #include "sdl_core.h" 528 static void sdl_send_bytes(
const uint8_t *buffer, uint16_t size)
532 sdl_send_byte(*buffer);
548 sdl_set_dc_pin(dcPin);
559 #endif // CONFIG_PLATFORM_SPI_AVAILABLE 561 #else // end of !KERNEL, KERNEL is below void ssd1306_platform_i2cInit(int8_t busId, uint8_t addr, int8_t arg)
Initializes i2c interface for platform being used.
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(* 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.