SSD1306 OLED display driver  1.6.99
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
ssd1306_spi_linux.c
1 /*
2  MIT License
3 
4  Copyright (c) 2018, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
24 
25 #include "ssd1306_spi_linux.h"
26 #include "intf/ssd1306_interface.h"
27 #include "ssd1306_spi_conf.h"
28 #include "ssd1306_spi.h"
29 
30 #if defined(SSD1306_LINUX_SUPPORTED) && !defined(__KERNEL__)
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 #if !defined(SDL_EMULATION)
36 
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <sys/ioctl.h>
41 #include <linux/spi/spidev.h>
42 
43 static int s_fd = -1;
44 extern uint32_t s_ssd1306_spi_clock;
45 
46 static void ssd1306_spiStart_Linux(void)
47 {
48 }
49 
50 static void ssd1306_spiStop_Linux(void)
51 {
52 }
53 
54 static void ssd1306_spiSendByte_Linux(uint8_t data)
55 {
56  /* TODO: Yeah, sending single bytes is too slow, but *
57  * need to figure out how to detect data/command bytes *
58  * to send bytes as one block */
59  uint8_t buf[1];
60  struct spi_ioc_transfer mesg;
61  buf[0] = data;
62  memset(&mesg, 0, sizeof mesg);
63  mesg.tx_buf = (unsigned long)&buf[0];
64  mesg.rx_buf = 0;
65  mesg.len = 1;
66  mesg.delay_usecs = 0;
67  mesg.speed_hz = 0;
68  mesg.bits_per_word = 8;
69  mesg.cs_change = 0;
70  if (ioctl(s_fd, SPI_IOC_MESSAGE(1), &mesg) < 1)
71  {
72  fprintf(stderr, "SPI failed to send SPI message: %s\n", strerror (errno)) ;
73  }
74 }
75 
76 static void ssd1306_spiSendBytes_Linux(const uint8_t *buffer, uint16_t size)
77 {
78  while (size--)
79  {
80  ssd1306_spiSendByte_Linux(*buffer);
81  buffer++;
82  }
83 }
84 
85 static void ssd1306_spiClose_Linux()
86 {
87  if (s_fd >= 0)
88  {
89  close(s_fd);
90  s_fd = -1;
91  }
92 }
93 
94 static void empty_function(void)
95 {
96 }
97 
98 static void empty_function_arg(uint8_t byte)
99 {
100 }
101 
102 static void empty_function_args(const uint8_t *buffer, uint16_t bytes)
103 {
104 }
105 
106 void ssd1306_spiInit_Linux(int8_t busId, int8_t ces, int8_t dcPin)
107 {
108  char filename[20];
109  if (busId < 0)
110  {
111  busId = 0;
112  }
113  if (ces < 0)
114  {
115  ces = 0;
116  }
117  s_ssd1306_cs = -1; // SPI interface does't need separate ces pin
118  s_ssd1306_dc = dcPin;
119  ssd1306_intf.spi = 1;
120  ssd1306_intf.start = empty_function;
121  ssd1306_intf.stop = empty_function;
122  ssd1306_intf.send = empty_function_arg;
123  ssd1306_intf.send_buffer = empty_function_args;
124  ssd1306_intf.close = empty_function;
125 
126  snprintf(filename, 19, "/dev/spidev%d.%d", busId, ces);
127  if ((s_fd = open(filename, O_RDWR)) < 0)
128  {
129  printf("Failed to initialize SPI: %s!\n", strerror(errno));
130  return;
131  }
132  unsigned int speed = s_ssd1306_spi_clock;
133  if (ioctl(s_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
134  {
135  printf("Failed to set speed on SPI line: %s!\n", strerror(errno));
136  }
137  uint8_t mode = SPI_MODE_0;
138  if (ioctl (s_fd, SPI_IOC_WR_MODE, &mode) < 0)
139  {
140  printf("Failed to set SPI mode: %s!\n", strerror(errno));
141  }
142  uint8_t spi_bpw = 8;
143  if (ioctl (s_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bpw) < 0)
144  {
145  printf("Failed to set SPI BPW: %s!\n", strerror(errno));
146  }
147 
148  ssd1306_intf.spi = 1;
149  ssd1306_intf.start = ssd1306_spiStart_Linux;
150  ssd1306_intf.stop = ssd1306_spiStop_Linux;
151  ssd1306_intf.send = ssd1306_spiSendByte_Linux;
152  ssd1306_intf.send_buffer = ssd1306_spiSendBytes_Linux;
153  ssd1306_intf.close = ssd1306_spiClose_Linux;
154 }
155 
156 #else /* SDL_EMULATION */
157 
158 #include "sdl_core.h"
159 
160 static void sdl_send_bytes(const uint8_t *buffer, uint16_t size)
161 {
162  while (size--)
163  {
164  sdl_send_byte(*buffer);
165  buffer++;
166  };
167 }
168 
169 void ssd1306_spiInit_Linux(int8_t busId, int8_t ces, int8_t dcPin)
170 {
171  sdl_core_init();
172  if (ces >= 0)
173  {
174  s_ssd1306_cs = ces;
175  }
176  if (dcPin >= 0)
177  {
178  s_ssd1306_dc = dcPin;
179  }
180  sdl_set_dc_pin(dcPin);
181  ssd1306_intf.spi = 1;
182  ssd1306_intf.start = sdl_send_init;
183  ssd1306_intf.stop = sdl_send_stop;
184  ssd1306_intf.send = sdl_send_byte;
185  ssd1306_intf.send_buffer = sdl_send_bytes;
186  ssd1306_intf.close = sdl_core_close;
187 }
188 
189 #endif /* SDL_EMULATION */
190 
191 #endif
192 
193 #if defined(__KERNEL__) && defined(SSD1306_LINUX_SUPPORTED)
194 void ssd1306_spiInit_Linux(int8_t busId, int8_t ces, int8_t dcPin)
195 {
196 }
197 #endif
int8_t s_ssd1306_dc
Definition: ssd1306_spi.c:36
void(* send)(uint8_t data)
uint32_t s_ssd1306_spi_clock
Definition: ssd1306_spi.c:37
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.
int8_t s_ssd1306_cs
Definition: ssd1306_spi.c:35