forked from vedderb/nrf24l01plus_stm32f100_chibios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
180 lines (145 loc) · 3.99 KB
/
main.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
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "spi_sw.h"
#include "rf.h"
#include "rfhelp.h"
static Mutex print_mutex;
static WORKING_AREA(rx_thread_wa, 256);
static WORKING_AREA(tx_thread_wa, 256);
const SerialConfig sc1_config =
{
115200, // baud rate
0, // CR1 register
0, // CR2 register
0 // CR3 register
};
void printf_thd(const char* format, ...) {
va_list arg;
va_start(arg, format);
chMtxLock(&print_mutex);
chvprintf((BaseSequentialStream*)&SD1, format, arg);
chMtxUnlock();
}
void print_rf_status(void) {
int s = rfhelp_rf_status();
printf_thd("RF Status Register\r\n");
printf_thd("RX_DR TX_DS MAX_RT RX_P_NO TX_FULL\r\n");
printf_thd("%i %i %i %i %i\r\n",
NRF_STATUS_GET_RX_DR(s), NRF_STATUS_GET_TX_DS(s), NRF_STATUS_GET_MAX_RT(s),
NRF_STATUS_GET_RX_P_NO(s), NRF_STATUS_GET_TX_FULL(s));
}
static msg_t tx_thread(void *arg) {
(void)arg;
chRegSetThreadName("TX");
int ok_sends = 0;
int timeout_sends = 0;
int maxrt_sends = 0;
int total_sends = 0;
char pl[3] = {1, 4, 6};
for(;;) {
switch (rfhelp_send_data(pl, 3)) {
case 0:
printf_thd("Send OK\r\n");
ok_sends++;
total_sends++;
break;
case -1:
printf_thd("Max RT\r\n");
maxrt_sends++;
total_sends++;
break;
case -2:
printf_thd("Timeout\r\n");
timeout_sends++;
total_sends++;
break;
default:
break;
}
printf_thd("OK sends: %i\r\n"
"MaxRT sends: %i\r\n"
"Timeout sends: %i\r\n"
"Total sends: %i\r\n",
ok_sends, maxrt_sends,
timeout_sends, total_sends);
printf_thd("\r\n");
chThdSleepMilliseconds(500);
if (pl[0] == 1) {
pl[0] = 119;
} else {
pl[0] = 1;
}
}
return 0;
}
static msg_t rx_thread(void *arg) {
(void)arg;
chRegSetThreadName("RX");
for(;;) {
char buf[32];
int len;
int pipe;
for(;;) {
int res = rfhelp_read_rx_data(buf, &len, &pipe);
// If something was read
if (res >= 0) {
printf_thd("Pipe: %i\r\n", pipe);
printf_thd("PL Length: %i\r\n", len);
for(int i = 0;i < len;i++) {
printf_thd("PL B%i: %i\r\n", i, buf[i]);
}
printf_thd("\r\n");
}
// Stop when there is no more data to read.
if (res <= 0) {
break;
}
}
chThdSleepMilliseconds(1);
}
return 0;
}
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
// initialize serial port driver
sdStart(&SD1, &sc1_config);
// Setup pins
palSetPadMode(GPIOA, 9, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
palSetPadMode(GPIOA, 10, PAL_MODE_INPUT);
// Initialize mutexes
chMtxInit(&print_mutex);
// NRF
rf_init();
rfhelp_init();
print_rf_status();
// Start threads
chThdCreateStatic(rx_thread_wa, sizeof(rx_thread_wa), NORMALPRIO, rx_thread, NULL);
chThdCreateStatic(tx_thread_wa, sizeof(tx_thread_wa), NORMALPRIO, tx_thread, NULL);
for(;;) {
chThdSleepMilliseconds(1000);
// printf_thd("Hello World!\r\n");
}
return 0;
}