Repository navigation
Expand file tree
/
Copy pathfirmware.c
More file actions
140 lines (121 loc) · 4.09 KB
/
Copy pathfirmware.c
File metadata and controls
140 lines (121 loc) · 4.09 KB
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
// Firmware for PicoRV32 on Arty A7-100T
// Uses LEDs, switches, buttons, and performs array operations
#define LED_REG (*(volatile unsigned int*)0x02000000)
#define GPIO_REG (*(volatile unsigned int*)0x02000004)
#define RGB_REG (*(volatile unsigned int*)0x02000008)
#define UART_REG (*(volatile unsigned int*)0x0200000C)
// Helper macros
#define GET_SWITCHES() (GPIO_REG & 0x0F)
#define GET_BUTTONS() ((GPIO_REG >> 4) & 0x0F)
// Simple delay function
void delay(int count) {
for (volatile int i = 0; i < count; i++) {
asm volatile ("nop");
}
}
// Array operations - similar to NTT butterfly but simpler
void simple_array_ops(int *arr, int size) {
// Simple transformation similar to butterfly operations
for (int i = 0; i < size/2; i++) {
int temp1 = arr[i];
int temp2 = arr[i + size/2];
// Simple addition/subtraction (like NTT butterfly)
arr[i] = temp1 + temp2;
arr[i + size/2] = temp1 - temp2;
}
}
// Calculate sum of array
int array_sum(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
// RGB color patterns
void set_rgb_pattern(int pattern) {
switch(pattern % 8) {
case 0: RGB_REG = 0x111; break; // All dim white
case 1: RGB_REG = 0x700; break; // Red on LED0
case 2: RGB_REG = 0x070; break; // Green on LED0
case 3: RGB_REG = 0x007; break; // Blue on LED0
case 4: RGB_REG = 0x777; break; // All bright white
case 5: RGB_REG = 0xF00; break; // Bright red
case 6: RGB_REG = 0x0F0; break; // Bright green
case 7: RGB_REG = 0x00F; break; // Bright blue
}
}
// Display number on 4 LEDs (binary representation)
void display_on_leds(int value) {
LED_REG = value & 0x0F;
}
void main() {
// Initialize array in RAM (8 elements)
int data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int result;
unsigned int counter = 0;
unsigned int mode = 0;
// Startup animation on RGB LEDs
for (int i = 0; i < 8; i++) {
set_rgb_pattern(i);
delay(500000);
}
// Main loop
while(1) {
// Read switches to determine mode
mode = GET_SWITCHES();
// Mode 0: Normal operation - perform array ops and display
if (mode == 0) {
// Perform array operations (like simplified NTT)
simple_array_ops(data, 8);
// Calculate result
result = array_sum(data, 8);
// Display result on LEDs (lower 4 bits)
display_on_leds(result);
// Show pattern on RGB LEDs
set_rgb_pattern(counter);
delay(1000000);
counter++;
// Reset array periodically
if (counter % 10 == 0) {
for (int i = 0; i < 8; i++) {
data[i] = i + 1;
}
}
}
// Mode 1: Show counter on LEDs
else if (mode == 1) {
display_on_leds(counter);
RGB_REG = 0x001; // Blue indicator
delay(500000);
counter++;
}
// Mode 2: Chase pattern on LEDs
else if (mode == 2) {
display_on_leds(1 << (counter & 0x3));
RGB_REG = 0x010; // Green indicator
delay(200000);
counter++;
}
// Mode 3: Button-controlled LEDs
else if (mode == 3) {
unsigned int buttons = GET_BUTTONS();
display_on_leds(buttons);
RGB_REG = 0x100; // Red indicator
delay(100000);
}
// Other modes: Show array values on LEDs
else {
for (int i = 0; i < 8; i++) {
display_on_leds(data[i] & 0xF);
set_rgb_pattern(i);
delay(500000);
}
}
}
}
// Boot code
void _start() {
main();
while(1); // Hang if main returns
}