-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp_functions.c
209 lines (179 loc) · 4.2 KB
/
bsp_functions.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
#include <stdint.h>
#include <stdbool.h>
#include <config.h>
#include "bsp_functions.h"
#include "pico/stdlib.h"
#include "pico/time.h"
#include <stdio.h>
#include "mb.h"
static uint8_t s_input_state = 0;
static bool s_output_state[NUM_OUTPUTS] = { false };
static volatile bool s_changed = false;
static bool light_enabled = false;
static uint8_t s_debounce[2] = { 0, 0 };
static uint32_t s_bsp_counters[2] = { 0 };
static int light_timer = 0;
static uint64_t light_timer_next = 0;
#define FLASHING_INTERVAL_US 250000
enum BSP_COUNTERS
{
OUTPUT_1,
OUTPUT_2
};
void light_update()
{
if (!light_enabled) { return; }
if (time_us_64() > light_timer_next)
{
bool state = gpio_get(OUTPUT_2_PIN);
gpio_put(OUTPUT_2_PIN, !state);
light_timer++;
light_timer_next = time_us_64() + FLASHING_INTERVAL_US;
if (light_timer > 40 && state) { light_enabled = false; }
}
}
void bsp_setup_pins()
{
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, true);
gpio_init(INPUT_1_PIN);
gpio_set_dir(INPUT_1_PIN, GPIO_IN);
gpio_set_input_enabled(INPUT_1_PIN, true);
gpio_init(INPUT_2_PIN);
gpio_set_dir(INPUT_2_PIN, GPIO_IN);
gpio_set_input_enabled(INPUT_2_PIN, true);
irq_set_enabled(IO_IRQ_BANK0, true);
s_input_state |= gpio_get(INPUT_1_PIN) ? 0x01 : 0;
s_input_state |= gpio_get(INPUT_2_PIN) ? 0x02 : 0;
gpio_init(OUTPUT_1_PIN);
gpio_set_dir(OUTPUT_1_PIN, GPIO_OUT);
gpio_init(OUTPUT_2_PIN);
gpio_set_dir(OUTPUT_2_PIN, GPIO_OUT);
gpio_init(ADDRESS_B0_PIN);
gpio_init(ADDRESS_B1_PIN);
gpio_init(ADDRESS_B2_PIN);
gpio_init(ADDRESS_B3_PIN);
gpio_init(ADDRESS_B4_PIN);
gpio_init(ADDRESS_B5_PIN);
gpio_init(ADDRESS_B6_PIN);
gpio_init(ADDRESS_B7_PIN);
gpio_set_dir(ADDRESS_B0_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B1_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B2_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B3_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B4_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B5_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B6_PIN, GPIO_IN);
gpio_set_dir(ADDRESS_B7_PIN, GPIO_IN);
}
bool get_LED_state()
{
return !gpio_get(LED_PIN);
}
void set_LED_state(bool state)
{
gpio_put(LED_PIN, !state);
}
bool get_output(uint8_t channel)
{
if (channel == 1)
{
return gpio_get_out_level(OUTPUT_1_PIN);
}
else if (channel == 2)
{
return gpio_get_out_level(OUTPUT_2_PIN);
}
return false;
}
void pulse_output(uint8_t channel)
{
#define PULSE_TIME_MS 100
if (channel == 0)
{
gpio_put(OUTPUT_1_PIN, true);
gpio_put(OUTPUT_2_PIN, true);
sleep_ms(PULSE_TIME_MS);
gpio_put(OUTPUT_1_PIN, false);
s_bsp_counters[OUTPUT_1]++;
light_timer = 0;
light_enabled = true;
light_timer_next = time_us_64() + FLASHING_INTERVAL_US;
//add_repeating_timer_ms(500, light_callback, NULL, &timer);
}
else if (channel == 1)
{
gpio_put(OUTPUT_2_PIN, true);
sleep_ms(PULSE_TIME_MS);
gpio_put(OUTPUT_2_PIN, false);
s_bsp_counters[OUTPUT_2]++;
}
}
bool get_input(uint8_t channel)
{
return s_input_state & (1 << channel);
}
uint8_t get_address_byte()
{
return (uint8_t)((gpio_get_all() >> 16) & 0xFF);
}
float get_bus_voltage();
bool input_changed()
{
if (s_changed)
{
s_changed = false;
return true;
}
return false;
}
void update_inputs()
{
s_debounce[0] <<= 1;
s_debounce[0] |= gpio_get(INPUT_1_PIN);
s_debounce[1] <<= 1;
s_debounce[1] |= gpio_get(INPUT_2_PIN);
if (s_debounce[0] == 0xFF && !(s_input_state & 0x01))
{
s_input_state |= 0x01;
mb_set_discrete_input(0, true);
s_changed = true;
}
else if (s_debounce[0] == 0x00 && s_input_state & 0x01)
{
s_input_state &= ~(0x01);
mb_set_discrete_input(0, false);
s_changed = true;
}
if (s_debounce[1] == 0xFF && !(s_input_state & 0x02))
{
s_input_state |= 0x02;
mb_set_discrete_input(1, true);
s_changed = true;
}
else if (s_debounce[1] == 0x00 && s_input_state & 0x02)
{
s_input_state &= ~(0x02);
mb_set_discrete_input(1, false);
s_changed = true;
}
}
void update_outputs()
{
for (int i = 0; i < NUM_OUTPUTS; i++)
{
bool coil_value = mb_get_coil(i);
if (coil_value)
{
pulse_output(i);
mb_set_coil(i, false);
}
}
}
void print_bsp_stats()
{
printf("** BSP STATISTICS **\r\n");
printf("OUTPUT 1 CYCLES\t= %lu\r\n", s_bsp_counters[OUTPUT_1]);
printf("OUTPUT 2 CYCLES\t= %lu\r\n", s_bsp_counters[OUTPUT_2]);
}