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 #define MAX_GPIO_COUNT 256 58 int gpio_export(
int pin)
61 ssize_t bytes_written;
65 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d", pin);
67 if (access(path, F_OK) == 0)
72 fd = open(
"/sys/class/gpio/export", O_WRONLY);
75 fprintf(stderr,
"Failed to allocate gpio pin resources[%d]: %s!\n", pin, strerror (errno));
79 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
80 if (write(fd, buffer, bytes_written) < 0)
82 fprintf(stderr,
"Failed to allocate gpio pin resources[%d]: %s!\n", pin, strerror (errno));
90 int gpio_unexport(
int pin)
93 ssize_t bytes_written;
96 fd = open(
"/sys/class/gpio/unexport", O_WRONLY);
99 fprintf(stderr,
"Failed to free gpio pin resources!\n");
103 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
104 if (write(fd, buffer, bytes_written) < 0)
106 fprintf(stderr,
"Failed to free gpio pin resources!\n");
112 int gpio_direction(
int pin,
int dir)
114 static const char s_directions_str[] =
"in\0out";
119 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/direction", pin);
120 fd = open(path, O_WRONLY);
123 fprintf(stderr,
"Failed to set gpio pin direction1[%d]: %s!\n", pin, strerror(errno));
127 if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3))
129 fprintf(stderr,
"Failed to set gpio pin direction2[%d]: %s!\n", pin, strerror(errno));
137 int gpio_read(
int pin)
143 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
144 fd = open(path, O_RDONLY);
147 fprintf(stderr,
"Failed to read gpio pin value!\n");
151 if (-1 == read(fd, value_str, 3))
153 fprintf(stderr,
"Failed to read gpio pin value!\n");
159 return(atoi(value_str));
162 int gpio_write(
int pin,
int value)
164 static const char s_values_str[] =
"01";
169 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
170 fd = open(path, O_WRONLY);
173 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s!\n", pin, strerror(errno));
177 if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1))
179 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s!\n", pin, strerror (errno));
187 #if !defined(SDL_EMULATION) 189 static uint8_t s_exported_pin[MAX_GPIO_COUNT] = {0};
190 static uint8_t s_pin_mode[MAX_GPIO_COUNT] = {0};
192 void pinMode(
int pin,
int mode)
194 if (!s_exported_pin[pin])
196 if ( gpio_export(pin)<0 )
200 s_exported_pin[pin] = 1;
204 gpio_direction(pin, OUT);
209 gpio_direction(pin, IN);
214 void digitalWrite(
int pin,
int level)
216 if (!s_exported_pin[pin])
218 if ( gpio_export(pin)<0 )
222 s_exported_pin[pin] = 1;
224 if (!s_pin_mode[pin])
226 pinMode(pin, OUTPUT);
228 gpio_write( pin, level );
231 #endif // SDL_EMULATION 236 #if defined(CONFIG_PLATFORM_I2C_AVAILABLE) && defined(CONFIG_PLATFORM_I2C_ENABLE) 240 #if !defined(SDL_EMULATION) 244 static int s_fd = -1;
245 static uint8_t s_buffer[128];
246 static uint8_t s_dataSize = 0;
248 static void platform_i2c_start(
void)
253 static void platform_i2c_stop(
void)
255 if (write(s_fd, s_buffer, s_dataSize) != s_dataSize)
257 fprintf(stderr,
"Failed to write to the i2c bus: %s.\n", strerror(errno));
262 static void platform_i2c_send(uint8_t data)
264 s_buffer[s_dataSize] = data;
266 if (s_dataSize ==
sizeof(s_buffer))
276 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
280 platform_i2c_send(*buffer);
285 static void platform_i2c_close()
294 static void empty_function()
298 static void empty_function_single_arg(uint8_t arg)
302 static void empty_function_two_args(
const uint8_t *arg1, uint16_t arg2)
313 snprintf(filename, 19,
"/dev/i2c-%d", busId);
319 if ((s_fd = open(filename, O_RDWR)) < 0)
321 fprintf(stderr,
"Failed to open the i2c bus\n");
328 if (ioctl(s_fd, I2C_SLAVE, s_sa) < 0)
330 fprintf(stderr,
"Failed to acquire bus access and/or talk to slave.\n");
342 #include "sdl_core.h" 344 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
348 sdl_send_byte(*buffer);
366 #endif // CONFIG_PLATFORM_I2C_AVAILABLE 372 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 374 #if !defined(SDL_EMULATION) 376 static int s_spi_fd = -1;
379 static void platform_spi_start(
void)
383 static void platform_spi_stop(
void)
387 static void platform_spi_send(uint8_t data)
393 struct spi_ioc_transfer mesg;
395 memset(&mesg, 0,
sizeof mesg);
396 mesg.tx_buf = (
unsigned long)&buf[0];
399 mesg.delay_usecs = 0;
401 mesg.bits_per_word = 8;
403 if (ioctl(s_spi_fd, SPI_IOC_MESSAGE(1), &mesg) < 1)
405 fprintf(stderr,
"SPI failed to send SPI message: %s\n", strerror (errno)) ;
409 static void platform_spi_close(
void)
418 static void platform_spi_send_buffer(
const uint8_t *data, uint16_t len)
422 platform_spi_send(*data);
427 static void empty_function_spi(
void)
431 static void empty_function_arg_spi(uint8_t byte)
435 static void empty_function_args_spi(
const uint8_t *buffer, uint16_t bytes)
461 snprintf(filename, 19,
"/dev/spidev%d.%d", busId, ces);
462 if ((s_spi_fd = open(filename, O_RDWR)) < 0)
464 printf(
"Failed to initialize SPI: %s!\n", strerror(errno));
468 if (ioctl(s_spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
470 printf(
"Failed to set speed on SPI line: %s!\n", strerror(errno));
472 uint8_t mode = SPI_MODE_0;
473 if (ioctl (s_spi_fd, SPI_IOC_WR_MODE, &mode) < 0)
475 printf(
"Failed to set SPI mode: %s!\n", strerror(errno));
478 if (ioctl (s_spi_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bpw) < 0)
480 printf(
"Failed to set SPI BPW: %s!\n", strerror(errno));
493 #include "sdl_core.h" 495 static void sdl_send_bytes(
const uint8_t *buffer, uint16_t size)
499 sdl_send_byte(*buffer);
515 sdl_set_dc_pin(dcPin);
526 #endif // CONFIG_PLATFORM_SPI_AVAILABLE 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.