13
13
#include <ms_rtos.h>
14
14
#include "u8x8.h"
15
15
16
+ /*********************************************************************************************************
17
+ Hardware I2C Communication
18
+ *********************************************************************************************************/
19
+
16
20
static const char * ms_u8g2_i2c_dev = "/dev/i2c0" ;
17
21
static ms_uint16_t ms_u8g2_i2c_addr = 0 ;
18
22
@@ -26,7 +30,7 @@ void ms_u8x8_i2c_address_set(ms_uint16_t i2c_addr)
26
30
ms_u8g2_i2c_addr = i2c_addr ;
27
31
}
28
32
29
- uint8_t ms_u8x8_byte_hw_i2c (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr )
33
+ ms_uint8_t ms_u8x8_byte_hw_i2c (u8x8_t * u8x8 , ms_uint8_t msg , ms_uint8_t arg_int , ms_ptr_t arg_ptr )
30
34
{
31
35
static int fd = -1 ;
32
36
static ms_uint8_t fifo_buf [32 ];
@@ -70,7 +74,7 @@ uint8_t ms_u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar
70
74
return 1 ;
71
75
}
72
76
73
- uint8_t ms_u8x8_gpio_and_delay_hw_i2c (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr )
77
+ ms_uint8_t ms_u8x8_gpio_and_delay_hw_i2c (u8x8_t * u8x8 , ms_uint8_t msg , ms_uint8_t arg_int , ms_ptr_t arg_ptr )
74
78
{
75
79
switch (msg ) {
76
80
case U8X8_MSG_GPIO_AND_DELAY_INIT :
@@ -107,3 +111,186 @@ uint8_t ms_u8x8_gpio_and_delay_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int
107
111
108
112
return 1 ;
109
113
}
114
+
115
+ /*********************************************************************************************************
116
+ Hardware SPI Communication
117
+ *********************************************************************************************************/
118
+
119
+ typedef struct {
120
+ int fd_spi ;
121
+ const char * dev_name ;
122
+ int dev_fd ;
123
+ int fd_cs ;
124
+ int fd_dc ;
125
+ int inited ;
126
+ } ms_u8x8_spi_ctx_t ;
127
+
128
+ static ms_u8x8_spi_ctx_t __u8x8_spi_ctx ;
129
+
130
+ void ms_u8x8_spi_dev_set (const char * spi_dev , const char * gpio_cs , const char * gpio_dc )
131
+ {
132
+ int fd_spi ;
133
+ int fd_cs ;
134
+ int fd_dc ;
135
+ ms_gpio_param_t param ;
136
+
137
+ fd_spi = ms_io_open (spi_dev , O_RDWR , 0666 );
138
+ fd_cs = ms_io_open (gpio_cs , O_RDWR , 0666 );
139
+ fd_dc = ms_io_open (gpio_dc , O_RDWR , 0666 );
140
+
141
+ param .mode = MS_GPIO_MODE_OUTPUT_PP ;
142
+ param .pull = MS_GPIO_PULL_UP ;
143
+ param .speed = MS_GPIO_SPEED_HIGH ;
144
+ ms_io_ioctl (fd_cs , MS_GPIO_CMD_SET_PARAM , & param );
145
+ ms_io_ioctl (fd_dc , MS_GPIO_CMD_SET_PARAM , & param );
146
+
147
+ __u8x8_spi_ctx .inited = MS_FALSE ;
148
+ __u8x8_spi_ctx .fd_spi = fd_spi ;
149
+ __u8x8_spi_ctx .fd_cs = fd_cs ;
150
+ __u8x8_spi_ctx .fd_dc = fd_dc ;
151
+ }
152
+
153
+ void ms_u8x8_spi_chip_select (const char * device_name )
154
+ {
155
+ int ret ;
156
+ int dev_fd ;
157
+ int fd_spi = __u8x8_spi_ctx .fd_spi ;
158
+ const char * node_name = MS_NULL ;
159
+
160
+ dev_fd = ms_io_open (device_name , O_RDWR , 0666 );
161
+ if (dev_fd > 0 ) {
162
+ node_name = device_name + strlen ("/dev/" );
163
+ ret = ms_io_ioctl (fd_spi , MS_SPI_CMD_SEL_DEV , (ms_ptr_t )node_name );
164
+ } else {
165
+ ret = -1 ;
166
+ }
167
+
168
+ if (ret == 0 ) {
169
+ __u8x8_spi_ctx .inited = MS_TRUE ;
170
+ } else {
171
+ __u8x8_spi_ctx .inited = MS_FALSE ;
172
+ }
173
+
174
+ __u8x8_spi_ctx .dev_name = device_name ;
175
+ }
176
+
177
+ ms_uint8_t ms_u8x8_byte_4wire_hw_spi (u8x8_t * u8x8 , ms_uint8_t msg , ms_uint8_t arg_int , ms_ptr_t arg_ptr )
178
+ {
179
+ static int fd_spi = -1 ;
180
+ static int fd_cs = -1 ;
181
+ static int fd_dc = -1 ;
182
+
183
+ if (__u8x8_spi_ctx .inited == MS_FALSE ) {
184
+ return 0 ;
185
+ }
186
+
187
+ if (fd_spi < 0 ) {
188
+ fd_spi = __u8x8_spi_ctx .fd_spi ;
189
+ fd_cs = __u8x8_spi_ctx .fd_cs ;
190
+ fd_dc = __u8x8_spi_ctx .fd_dc ;
191
+ }
192
+
193
+ switch (msg ) {
194
+ case U8X8_MSG_BYTE_SEND : {
195
+ ms_spi_msg_t msg ;
196
+ ms_uint8_t dump ;
197
+
198
+ msg .len = arg_int ;
199
+ msg .tx_buf = arg_ptr ;
200
+ msg .rx_buf = & dump ;
201
+ msg .flags = MS_SPI_M_ONCE | MS_SPI_M_RX_FIX | MS_SPI_M_WRITE ;
202
+
203
+ (void )ms_io_write (fd_spi , & msg , sizeof (msg ));
204
+ }
205
+ break ;
206
+
207
+ case U8X8_MSG_BYTE_INIT : {
208
+ ms_uint8_t status ;
209
+ status = u8x8 -> display_info -> chip_disable_level ;
210
+ ms_io_write (fd_cs , & status , sizeof (status ));
211
+ }
212
+ break ;
213
+
214
+ case U8X8_MSG_BYTE_SET_DC : {
215
+ ms_uint8_t status = arg_int ;
216
+ ms_io_write (fd_dc , & status , sizeof (status ));
217
+ }
218
+ break ;
219
+
220
+ case U8X8_MSG_BYTE_START_TRANSFER : {
221
+ ms_uint8_t status ;
222
+ ms_spi_param_t param ;
223
+
224
+ ms_io_ioctl (fd_spi , MS_SPI_CMD_GET_PARAM , & param );
225
+ param .frame_mode &= ~MS_SPI_CLK_POLARITY_HIGH ;
226
+ param .frame_mode &= ~MS_SPI_CLK_PHASE_2EDGE ;
227
+
228
+ switch (u8x8 -> display_info -> spi_mode ) {
229
+ case 0 : param .frame_mode |= MS_SPI_CLK_POLARITY_LOW | MS_SPI_CLK_PHASE_1EDGE ; break ;
230
+ case 1 : param .frame_mode |= MS_SPI_CLK_POLARITY_LOW | MS_SPI_CLK_PHASE_2EDGE ; break ;
231
+ case 2 : param .frame_mode |= MS_SPI_CLK_POLARITY_HIGH | MS_SPI_CLK_PHASE_1EDGE ; break ;
232
+ case 3 : param .frame_mode |= MS_SPI_CLK_POLARITY_HIGH | MS_SPI_CLK_PHASE_2EDGE ; break ;
233
+ }
234
+ ms_io_ioctl (fd_spi , MS_SPI_CMD_SET_PARAM , & param );
235
+
236
+ status = u8x8 -> display_info -> chip_enable_level ;
237
+ ms_io_write (fd_cs , & status , sizeof (status ));
238
+
239
+ u8x8 -> gpio_and_delay_cb (u8x8 , U8X8_MSG_DELAY_NANO , u8x8 -> display_info -> post_chip_enable_wait_ns , NULL );
240
+ }
241
+ break ;
242
+
243
+ case U8X8_MSG_BYTE_END_TRANSFER : {
244
+ ms_uint8_t status ;
245
+
246
+ u8x8 -> gpio_and_delay_cb (u8x8 , U8X8_MSG_DELAY_NANO , u8x8 -> display_info -> pre_chip_disable_wait_ns , NULL );
247
+
248
+ status = u8x8 -> display_info -> chip_disable_level ;
249
+ ms_io_write (fd_cs , & status , sizeof (status ));
250
+ }
251
+ break ;
252
+
253
+ default :
254
+ return 0 ;
255
+ }
256
+
257
+ return 1 ;
258
+ }
259
+
260
+ ms_uint8_t ms_u8x8_gpio_and_delay_hw_spi (u8x8_t * u8x8 , ms_uint8_t msg , ms_uint8_t arg_int , ms_ptr_t arg_ptr )
261
+ {
262
+ switch (msg ) {
263
+ case U8X8_MSG_GPIO_AND_DELAY_INIT :
264
+ /* init gpio and delay timer */
265
+ break ;
266
+
267
+ case U8X8_MSG_DELAY_MILLI :
268
+ /* delay_ms(arg_int); */
269
+ break ;
270
+
271
+ case U8X8_MSG_GPIO_CS :
272
+ /* chip select */
273
+ break ;
274
+
275
+ case U8X8_MSG_GPIO_DC :
276
+ /* indicate whether data or command */
277
+ break ;
278
+
279
+ case U8X8_MSG_GPIO_RESET :
280
+ /* reset gpio */
281
+ break ;
282
+
283
+ case U8X8_MSG_GPIO_SPI_CLOCK :
284
+ /* configure spi baud rate */
285
+ break ;
286
+
287
+ case U8X8_MSG_GPIO_SPI_DATA :
288
+ /* send data by using gpio-spi */
289
+ break ;
290
+
291
+ default :
292
+ break ;
293
+ }
294
+
295
+ return 1 ;
296
+ }
0 commit comments