-
Notifications
You must be signed in to change notification settings - Fork 0
/
remora_rock_hal.c
333 lines (286 loc) · 7.04 KB
/
remora_rock_hal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdbool.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa/gpio.h"
#include "mraa/pwm.h"
#include "mraa/spi.h"
#include "ssd1362_interface.h"
#define BUFFER_WIDTH 256U
#define BUFFER_HEIGHT 64U
#define BUFFER_SIZE 8192U
#define ALIGN_RIGHT 0U
#define ALIGN_LEFT 1U
#define CHAR_SPACING 2U
#define BPP 4
#define NUM_ROWS 8
#define PWM_PIN 18
#define PWM_FREQ 200
#define SPI_BUS 3
#define SPI_FREQ 400000
#define RES 11 // pin 11
#define DC 13 // pin 13
#define CS 24 // pin 24
#define RES_G 116
#define DC_G 117
#define CS_G 150
#define MOSI 19 // pin 19
#define CLK 23 // pin 23
#define _delay_us(x) usleep(x)
#define _delay_ms(x) usleep(x*1000)
#define _delay_s(x) usleep(x*1000000)
char *fb;
volatile sig_atomic_t flag = 1;
int err;
mraa_spi_context spi;
mraa_gpio_context res, dc, cs;
void sig_handler(int signum)
{
if (signum == SIGINT) {
fprintf(stdout, "Exiting...\n");
flag = 0;
}
}
void sysfs_pin_init(int gpio)
{
char buf[100];
FILE* fd = fopen("/sys/class/gpio/export", "w");
fprintf(fd, "%d", gpio);
fclose(fd);
sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
fd = fopen(buf, "w");
fprintf(fd, "out");
fclose(fd);
sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
fd = fopen(buf, "w");
fd = fopen(buf, "w");
fprintf(fd, "1");
fclose(fd);
}
void sysfs_pin_val(int val, int gpio)
{
char buf[100];
FILE* fd = fopen("/sys/class/gpio/export", "w");
int length = snprintf( NULL, 0, "%d", val);
char* str = malloc( length + 1 );
sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
fd = fopen(buf, "w");
fd = fopen(buf, "w");
fprintf(fd, str);
fclose(fd);
}
int main(int argc, char** argv)
{
char *fb = (char *) calloc(8192, sizeof(char)); // Initialise framebuffer
mraa_result_t status = MRAA_SUCCESS;
/* GPIO INITIALISATION */
signal(SIGINT, sig_handler);
const char* board_name = mraa_get_platform_name();
fprintf(stdout, "Version: %s\nRunning on %s\n", mraa_get_version(), board_name);
mraa_init(); // apparently not always necessary, included in example
/*
sysfs_pin_init(RES_G);
sysfs_pin_init(DC_G);
sysfs_pin_init(CS_G);
*/
// INIT PINS
res = mraa_gpio_init(RES);
if (res == NULL)
{
fprintf(stderr, "Failed to initialize GPIO %d\n", RES);
mraa_deinit();
return EXIT_FAILURE;
}
dc = mraa_gpio_init(DC);
if (dc == NULL)
{
fprintf(stderr, "Failed to initialize GPIO %d\n", DC);
mraa_deinit();
return EXIT_FAILURE;
}
cs = mraa_gpio_init(CS);
if (res == NULL)
{
fprintf(stderr, "Failed to initialize GPIO %d\n", CS);
mraa_deinit();
return EXIT_FAILURE;
}
fprintf(stdout, "TP0\n");
// SET OUTPUT
status = mraa_gpio_dir(res, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
status = mraa_gpio_dir(dc, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
status = mraa_gpio_dir(cs, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
fprintf(stdout, "TP1\n");
// SET MODE PULLUP (DC, RES)
status = mraa_gpio_mode(dc, MRAA_GPIO_PULLUP);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
status = mraa_gpio_mode(res, MRAA_GPIO_PULLUP);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
fprintf(stdout, "TP2\n");
/* SPI INITIALISATION */
spi = mraa_spi_init(SPI_BUS);
if (spi == NULL)
{
fprintf(stderr, "Failed to initialize SPI\n");
mraa_deinit();
return EXIT_FAILURE;
}
fprintf(stdout, "Successfully initialised SPI bus\n");
// SPI FREQ
status = mraa_spi_frequency(spi, SPI_FREQ);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
/*
// SPI MODE (BIG ENDIAN)
status = mraa_spi_lsbmode(spi, 0);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
*/
// USE BYTE FUNCTION
fprintf(stdout, "Opened SPI on Channel 3 [SSD1362]\n");
fflush(stdout);
/* REMOVE FOR NOW, CAUSING SEG FAULT
SSD1362_fb_checker();
err = SSD1362_fb_write();
if (err != 0){
fprintf(stdout, "Error in writing fb to mraa_spi_write_buf()!");
goto err_exit;
}
*/
_delay_ms(1000000); // 10s delay
// END
status = mraa_gpio_close(res);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
status = mraa_gpio_close(dc);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
status = mraa_gpio_close(cs);
if (status != MRAA_SUCCESS)
{
goto err_exit;
}
mraa_spi_stop(spi);
mraa_deinit();
fflush(stdout);
return MRAA_SUCCESS;
err_exit:
mraa_result_print(status);
status = mraa_gpio_close(res);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_gpio_close(dc);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_gpio_close(cs);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
mraa_result_print(status);
mraa_spi_stop(spi);
mraa_deinit();
return EXIT_FAILURE;
}
void SSD1362_cmd(char cmd)
{
mraa_gpio_write(dc, 0);
mraa_spi_write(spi, &cmd);
mraa_gpio_write(dc, 1);
}
void SSD1362_data(char data)
{
mraa_spi_write(spi, &data);
}
void SSD1362_init()
{
mraa_gpio_write(res, 0);
_delay_ms(150);
mraa_gpio_write(res, 1); //Reset pin high
_delay_ms(150);
_delay_ms(150);
}
int SSD1362_fb_write()
{
SSD1362_set_columns(SSD1362_COLS_MIN, SSD1362_COLS_MAX);
SSD1362_set_rows(SSD1362_ROWS_MIN, SSD1362_ROWS_MAX);
if (fb == NULL) {
return -1;
}
char buffer1[4096];
char buffer2[4096];
memcpy(buffer1, fb, 4096);
memcpy(buffer2, fb + 4096, 4096);
mraa_spi_write_buf(spi, buffer1, 4096); // Example function call to transmit data
mraa_spi_write_buf(spi, buffer2, 4096); // Transmit the second part
free(fb);
return 0;
}
void SSD1362_fb_checker()
{
// NSTRIPE
unsigned int i, j;
unsigned int n = 0;
unsigned char b[128], w[128];
for (n=0; n<128; n++){
b[n] = 0x00;
w[n] = 0xFF;
}
for (i=0; i<64; i++){
for (int j = 0; j < 128; j++){
if (i & 0x01U){
fb[(i * BUFFER_HEIGHT) + j] = b[j];
}
else {
fb[(i * BUFFER_HEIGHT) + j] = w[j];
}
}
}
}
void SSD1362_stream()
{
SSD1362_cmd(SSD1362_CMD_SET_WRITE);
mraa_gpio_write(dc, 1);
}
void SSD1362_set_columns(uint8_t start, uint8_t end)
{
SSD1362_cmd(SSD1362_CMD_SET_COLS);
SSD1362_data(start);
SSD1362_data(end);
}
void SSD1362_set_rows(uint8_t start, uint8_t end)
{
SSD1362_cmd(SSD1362_CMD_SET_ROWS);
SSD1362_data(start);
SSD1362_data(end);
}