-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
245 lines (185 loc) · 7.17 KB
/
main.cpp
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
#include <stdio.h>
#include <math.h>
#include <pico/stdlib.h>
#include <hardware/gpio.h>
#include <hardware/spi.h>
#include <hardware/dma.h>
#include <pico/cyw43_arch.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <semphr.h>
#include "animation.h"
#include "hub75.pio.h"
#if SPI_TO_FPGA
#define FPGA_RESET_PIN 26
#else
#define DATA_BASE_PIN 2
#define DATA_N_PINS 6
#define ROWSEL_BASE_PIN 8
#define ROWSEL_N_PINS 4
#define CLK_PIN 13
#define STROBE_PIN 14
#define OEN_PIN 15
#endif
void animate_task(void *dummy);
void matrix_task(void *dummy);
void i2c_task(void *dummy);
void buzzer_task(void *dummy);
void vApplicationTickHook(void);
extern void mqtt_task(void *dummy);
QueueHandle_t animate_queue;
QueueHandle_t matrix_queue;
QueueHandle_t mqtt_queue;
QueueHandle_t i2c_queue;
QueueHandle_t buzzer_queue;
int main(void)
{
stdio_init_all();
printf("Hello, matrix here\n");
srand(0);
animate_queue = xQueueCreate(3, sizeof(message_anim_t));
mqtt_queue = xQueueCreate(3, sizeof(message_mqtt_t));
i2c_queue = xQueueCreate(3, sizeof(message_i2c_t));
buzzer_queue = xQueueCreate(3, sizeof(message_buzzer_t));
matrix_queue = xQueueCreate(1, sizeof(fb_t));
xTaskCreate(&animate_task, "Animate Task", 4096, NULL, 0, NULL);
xTaskCreate(&mqtt_task, "MQTT Task", 4096, NULL, 0, NULL);
xTaskCreate(&i2c_task, "I2C Task", 4096, NULL, 0, NULL);
xTaskCreate(&buzzer_task, "Buzzer Task", 4096, NULL, 0, NULL);
xTaskCreate(&matrix_task, "Matrix Task", 1024, NULL, 10, NULL);
vTaskStartScheduler();
while(1);
return 0;
}
framebuffer fb;
void animate_task(void *dummy)
{
#if FREE_RTOS_KERNEL_SMP
vTaskCoreAffinitySet(NULL, 1 << 0);
printf("%s: core%u\n", pcTaskGetName(NULL), get_core_num());
#endif
message_anim_t message;
memset(&message, 0, sizeof(message_anim_t));
animation anim(fb);
while (1)
{
while (xQueueReceive(animate_queue, &message, 0) == pdTRUE) {
printf("New message, type: %d\n", message.message_type);
switch (message.message_type) {
case MESSAGE_ANIM_WEATHER:
anim.new_weather_data(&message.weather_data);
break;
case MESSAGE_ANIM_MEDIA_PLAYER:
anim.new_media_player_data(&message.media_player_data);
break;
case MESSAGE_ANIM_CALENDAR:
anim.new_calendar_data(&message.calendar_data);
break;
case MESSAGE_ANIM_TRANSPORT:
anim.new_transport_data(&message.transport_data);
break;
case MESSAGE_ANIM_NOTIFICATION:
anim.new_notification(&message.notification);
break;
case MESSAGE_ANIM_PORCH:
anim.new_porch(&message.porch);
break;
case MESSAGE_ANIM_DS3231:
anim.new_ds3231(&message.ds3231);
break;
case MESSAGE_ANIM_BRIGHTNESS:
fb.set_brightness(message.brightness);
break;
case MESSAGE_ANIM_GRAYSCALE:
fb.set_grayscale(message.grayscale);
break;
case MESSAGE_ANIM_CONFIGURATION:
anim.update_configuration(&message.configuration);
break;
default:
printf("Unknown message type: %d\n", message.message_type);
break;
}
}
anim.prepare_screen();
anim.render_page();
anim.render_notification();
fb.atomic_back_to_fore_copy();
vTaskDelay(10);
}
}
void matrix_task(void *dummy)
{
#if FREE_RTOS_KERNEL_SMP
vTaskCoreAffinitySet(NULL, 1 << 1);
printf("%s: core%u\n", pcTaskGetName(NULL), get_core_num());
#endif
#if SPI_TO_FPGA
gpio_init(FPGA_RESET_PIN);
gpio_set_dir(FPGA_RESET_PIN, true);
gpio_put(FPGA_RESET_PIN, false);
static fb_t output_fb;
spi_init(spi_default, 10 * 1000 * 1000);
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, true);
gpio_put(FPGA_RESET_PIN, true);
const uint dma_tx = dma_claim_unused_channel(true);
printf("Configure TX DMA\n");
dma_channel_config dma_config_c = dma_channel_get_default_config(dma_tx);
// TODO: investigate DMA_SIZE_32 transfers
channel_config_set_transfer_data_size(&dma_config_c, DMA_SIZE_8);
channel_config_set_dreq(&dma_config_c, spi_get_dreq(spi_default, true));
dma_channel_configure(dma_tx, &dma_config_c,
&spi_get_hw(spi_default)->dr, // write address
NULL, // read address (set later)
FB_WIDTH * FB_HEIGHT * sizeof(uint32_t), // element count
false); // don't start it now
sleep_ms(1000);
while (1) {
fb.atomic_fore_copy_out(&output_fb);
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, false);
// Set the read address to the top of frame and trigger
dma_channel_set_read_addr(dma_tx, &output_fb.uint32, true);
dma_channel_wait_for_finish_blocking(dma_tx);
fb.atomic_fore_copy_out(&output_fb);
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, true);
// Set the read address to the top of frame and trigger
dma_channel_set_read_addr(dma_tx, &output_fb.uint32, true);
dma_channel_wait_for_finish_blocking(dma_tx);
}
#else
PIO pio = pio0;
uint sm_data = 0;
uint sm_row = 1;
uint data_prog_offs = pio_add_program(pio, &hub75_data_rgb888_program);
uint row_prog_offs = pio_add_program(pio, &hub75_row_program);
hub75_data_rgb888_program_init(pio, sm_data, data_prog_offs, DATA_BASE_PIN, CLK_PIN);
hub75_row_program_init(pio, sm_row, row_prog_offs, ROWSEL_BASE_PIN, ROWSEL_N_PINS, STROBE_PIN);
static fb_t output_fb;
while (1) {
fb.atomic_fore_copy_out(&output_fb);
for (int rowsel = 0; rowsel < 16; ++rowsel) {
for (int bit = 0; bit < 8; ++bit) {
hub75_data_rgb888_set_shift(pio, sm_data, data_prog_offs, bit);
for (int x = 0; x < FB_WIDTH; x++) {
pio_sm_put_blocking(pio, sm_data, output_fb.uint32[rowsel][x]);
pio_sm_put_blocking(pio, sm_data, output_fb.uint32[rowsel + 16][x]);
}
// Dummy pixel per lane
pio_sm_put_blocking(pio, sm_data, 0);
pio_sm_put_blocking(pio, sm_data, 0);
// SM is finished when it stalls on empty TX FIFO
hub75_wait_tx_stall(pio, sm_data);
// Also check that previous OEn pulse is finished, else things can get out of sequence
hub75_wait_tx_stall(pio, sm_row);
// Latch row data, pulse output enable for new row.
pio_sm_put_blocking(pio, sm_row, rowsel | (100u * (1u << bit) << 5));
}
}
}
#endif
}