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[%d]: %s%s!\n",
85 pin, strerror (errno), getuid() == 0 ?
"" :
", need to be root");
89 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
90 if (write(fd, buffer, bytes_written) < 0)
92 fprintf(stderr,
"Failed to allocate gpio pin[%d]: %s%s!\n",
93 pin, strerror (errno), getuid() == 0 ?
"" :
", need to be root");
101 int gpio_unexport(
int pin)
104 ssize_t bytes_written;
107 fd = open(
"/sys/class/gpio/unexport", O_WRONLY);
110 fprintf(stderr,
"Failed to free gpio pin resources!\n");
114 bytes_written = snprintf(buffer,
sizeof(buffer),
"%d", pin);
115 if (write(fd, buffer, bytes_written) < 0)
117 fprintf(stderr,
"Failed to free gpio pin resources!\n");
123 int gpio_direction(
int pin,
int dir)
125 static const char s_directions_str[] =
"in\0out";
130 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/direction", pin);
131 fd = open(path, O_WRONLY);
134 fprintf(stderr,
"Failed to set gpio pin direction1[%d]: %s!\n",
135 pin, strerror(errno));
139 if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3))
141 fprintf(stderr,
"Failed to set gpio pin direction2[%d]: %s!\n",
142 pin, strerror(errno));
150 int gpio_read(
int pin)
156 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
157 fd = open(path, O_RDONLY);
160 fprintf(stderr,
"Failed to read gpio pin value!\n");
164 if (-1 == read(fd, value_str, 3))
166 fprintf(stderr,
"Failed to read gpio pin value!\n");
172 return(atoi(value_str));
175 int gpio_write(
int pin,
int value)
177 static const char s_values_str[] =
"01";
182 snprintf(path,
sizeof(path),
"/sys/class/gpio/gpio%d/value", pin);
183 fd = open(path, O_WRONLY);
186 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s%s!\n",
187 pin, strerror (errno), getuid() == 0 ?
"" :
", need to be root");
191 if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1))
193 fprintf(stderr,
"Failed to set gpio pin value[%d]: %s%s!\n",
194 pin, strerror (errno), getuid() == 0 ?
"" :
", need to be root");
202 #if !defined(SDL_EMULATION) 204 static uint8_t s_exported_pin[MAX_GPIO_COUNT] = {0};
205 static uint8_t s_pin_mode[MAX_GPIO_COUNT] = {0};
207 void pinMode(
int pin,
int mode)
209 if (!s_exported_pin[pin])
211 if ( gpio_export(pin)<0 )
215 s_exported_pin[pin] = 1;
219 gpio_direction(pin, OUT);
224 gpio_direction(pin, IN);
229 void digitalWrite(
int pin,
int level)
231 #ifdef LINUX_SPI_AVAILABLE 234 platform_spi_send_cache();
238 if (!s_exported_pin[pin])
240 if ( gpio_export(pin)<0 )
244 s_exported_pin[pin] = 1;
246 if (!s_pin_mode[pin])
248 pinMode(pin, OUTPUT);
250 gpio_write( pin, level );
253 #endif // SDL_EMULATION 258 #if defined(CONFIG_PLATFORM_I2C_AVAILABLE) && defined(CONFIG_PLATFORM_I2C_ENABLE) 262 #if !defined(SDL_EMULATION) 266 static int s_fd = -1;
267 static uint8_t s_buffer[128];
268 static uint8_t s_dataSize = 0;
270 static void platform_i2c_start(
void)
275 static void platform_i2c_stop(
void)
277 if (write(s_fd, s_buffer, s_dataSize) != s_dataSize)
279 fprintf(stderr,
"Failed to write to the i2c bus: %s.\n", strerror(errno));
284 static void platform_i2c_send(uint8_t data)
286 s_buffer[s_dataSize] = data;
288 if (s_dataSize ==
sizeof(s_buffer))
298 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
302 platform_i2c_send(*buffer);
307 static void platform_i2c_close()
316 static void empty_function()
320 static void empty_function_single_arg(uint8_t arg)
324 static void empty_function_two_args(
const uint8_t *arg1, uint16_t arg2)
335 snprintf(filename, 19,
"/dev/i2c-%d", busId);
341 if ((s_fd = open(filename, O_RDWR)) < 0)
343 fprintf(stderr,
"Failed to open the i2c bus %s\n",
344 getuid() == 0 ?
"":
": need to be root");
351 if (ioctl(s_fd, I2C_SLAVE, s_sa) < 0)
353 fprintf(stderr,
"Failed to acquire bus access and/or talk to slave.\n");
365 #include "sdl_core.h" 367 static void platform_i2c_send_buffer(
const uint8_t *buffer, uint16_t size)
371 sdl_send_byte(*buffer);
389 #endif // CONFIG_PLATFORM_I2C_AVAILABLE 395 #if defined(CONFIG_PLATFORM_SPI_AVAILABLE) && defined(CONFIG_PLATFORM_SPI_ENABLE) 397 #if !defined(SDL_EMULATION) 399 static int s_spi_fd = -1;
401 static uint8_t s_spi_cache[1024];
402 static int s_spi_cached_count = 0;
404 static void platform_spi_start(
void)
406 s_spi_cached_count = 0;
409 static void platform_spi_stop(
void)
411 platform_spi_send_cache();
414 static void platform_spi_send_cache()
419 if ( s_spi_cached_count == 0 )
423 struct spi_ioc_transfer mesg;
424 memset(&mesg, 0,
sizeof mesg);
425 mesg.tx_buf = (
unsigned long)&s_spi_cache[0];
427 mesg.len = s_spi_cached_count;
428 mesg.delay_usecs = 0;
430 mesg.bits_per_word = 8;
432 if (ioctl(s_spi_fd, SPI_IOC_MESSAGE(1), &mesg) < 1)
434 fprintf(stderr,
"SPI failed to send SPI message: %s\n", strerror (errno)) ;
436 s_spi_cached_count = 0;
439 static void platform_spi_send(uint8_t data)
441 s_spi_cache[s_spi_cached_count] = data;
442 s_spi_cached_count++;
443 if ( s_spi_cached_count >=
sizeof( s_spi_cache ) )
445 platform_spi_send_cache();
449 static void platform_spi_close(
void)
458 static void platform_spi_send_buffer(
const uint8_t *data, uint16_t len)
462 platform_spi_send(*data);
467 static void empty_function_spi(
void)
471 static void empty_function_arg_spi(uint8_t byte)
475 static void empty_function_args_spi(
const uint8_t *buffer, uint16_t bytes)
501 snprintf(filename, 19,
"/dev/spidev%d.%d", busId, ces);
502 if ((s_spi_fd = open(filename, O_RDWR)) < 0)
504 printf(
"Failed to initialize SPI: %s%s!\n",
505 strerror(errno), getuid() == 0 ?
"":
", need to be root");
509 if (ioctl(s_spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
511 printf(
"Failed to set speed on SPI line: %s!\n", strerror(errno));
513 uint8_t mode = SPI_MODE_0;
514 if (ioctl (s_spi_fd, SPI_IOC_WR_MODE, &mode) < 0)
516 printf(
"Failed to set SPI mode: %s!\n", strerror(errno));
519 if (ioctl (s_spi_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bpw) < 0)
521 printf(
"Failed to set SPI BPW: %s!\n", strerror(errno));
534 #include "sdl_core.h" 536 static void sdl_send_bytes(
const uint8_t *buffer, uint16_t size)
540 sdl_send_byte(*buffer);
556 sdl_set_dc_pin(dcPin);
567 #endif // CONFIG_PLATFORM_SPI_AVAILABLE 569 #else // end of !KERNEL, KERNEL is below
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.