-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrf_comm_master.c
226 lines (188 loc) · 4.99 KB
/
nrf_comm_master.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
#pragma GCC diagnostic ignored "-Wwrite-strings"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <stdio.h>
#include <string.h>
#include "Mirf.h"
#include "Mirf_nRF24L01.h"
#define DEV_ADDR 1
mirfPacket volatile inPacket;
mirfPacket volatile outPacket;
uint8_t volatile citac;
uint8_t volatile uartPos = 0;
//uint8_t volatile uartBufEmpty = 1;
uint8_t volatile uartIncoming = 0;
uint8_t volatile awaitingResult;
char buff[35];
typedef union {
uint16_t uint;
struct {
uint8_t lsb;
uint8_t msb;
};
} IntUnion;
//IntUnion volatile adcVal;
//======================================================
ISR(TIMER0_COMPA_vect) {
Mirf.handleRxLoop();
Mirf.handleTxLoop();
citac++;
}
ISR(BADISR_vect) { //just for case
__asm__("nop\n\t");
}
ISR(USART_RX_vect)
{
uint8_t inp = UDR0;
if (uartIncoming == 0)
{
if (inp == 254)
{
uartIncoming = 1;
//uartBufEmpty = 1;
uartPos = 0;
citac = 0;
}
}
else
{
if (uartPos == sizeof(mirfPacket)) //incoming packet is longer than allowed
{
uartIncoming = 0;
}
else
{
((char*)&outPacket)[uartPos] = inp;
uartPos++;
}
}
}
//==========================================================
void USART_Transmit( char *data, uint8_t len )
{
for (uint8_t i=0; i < len; i++)
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data[i];
}
}
#ifdef _DEBUG_
void debug(void)
{
uint8_t i;
uint8_t volatile rr;
uint8_t adr[5];
cli();
for (i=0; i< 10; i++)
{
Mirf.readRegister(i, (uint8_t*)&rr, 1);
sprintf(buff, "%d:%d,", i, rr);
USART_Transmit(buff, strlen(buff) );
}
sprintf(buff, "\n");
USART_Transmit(buff, strlen(buff) );
Mirf.readRegister(TX_ADDR, (uint8_t*)&adr, 5);
sprintf(buff, "TX: 0x%2x%2x%2x%2x%2x\n", adr[0], adr[1], adr[2], adr[3], adr[4]);
USART_Transmit(buff, strlen(buff) );
Mirf.readRegister(RX_ADDR_P0, (uint8_t*)&adr, 5);
sprintf(buff, "RX: 0x%2x%2x%2x%2x%2x\n", adr[0], adr[1], adr[2], adr[3], adr[4]);
USART_Transmit(buff, strlen(buff) );
sei();
}
#endif
void setup()
{
//configure uart0 (57600, 8bits, no parity, 1 stop bit)
UBRR0H = 0;
UBRR0L = 16;
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
//start radio
Mirf.init();
Mirf.setDevAddr(DEV_ADDR);
Mirf.config();
#ifdef _DEBUG_
Mirf.csnLow();
uint8_t a = Mirf.spi->transfer(R_REGISTER | ( REGISTER_MASK & 5 ));
uint8_t b = Mirf.spi->transfer(255);
uint8_t c = Mirf.spi->transfer(255);
Mirf.csnHi();
sprintf(buff, "po: %d, %d, %d\n", a, b, c); //DEBUG
USART_Transmit(buff, strlen(buff) );
#endif
//timer0 10ms period, interrupt enable
//prescaler 1024, count to 156
OCR0A = 156;
OCR0B = 170;
TCCR0A = 2;
TCCR0B = 5;
TIMSK0 = 2;
//disable unused peripherials
PRR = ( _BV(PRTWI) | _BV(PRTIM1) | _BV(PRTIM2) ) ;
sei();
}
int main(void)
{
wdt_disable();
setup();
memset((void*)&outPacket, 0, sizeof(mirfPacket) );
outPacket.txAddr = DEV_ADDR; //but this should be filled during sending packet
outPacket.rxAddr = 2;
outPacket.type = (PACKET_TYPE)REQUEST;
((payloadRequestStruct *)&outPacket.payload)->cmd = READ;
((payloadRequestStruct *)&outPacket.payload)->len = 0;
((payloadRequestStruct *)&outPacket.payload)->for_sensor = 0;
sprintf(buff, "COMM MASTER\n");
USART_Transmit(buff, strlen(buff) );
//------------------------------------
while(1) {
//zpracovat prichozi packet
if (Mirf.inPacketReady)
{
if (!uartIncoming)
{
Mirf.readPacket((mirfPacket*)&inPacket);
//send whole packet through usart
USART_Transmit((char*)&inPacket, sizeof(mirfPacket));
//sprintf(buff, "RX:%d,TX:%d,T:%d,C:%d,P:%d\n", inPacket.rxAddr, inPacket.txAddr, inPacket.type, inPacket.counter, inPacket.payload[0]);
//USART_Transmit(buff, strlen(buff) );
}
}
if ( (uartIncoming) && (uartPos == (sizeof(mirfPacket)-1)) ) //whole packet is received
{
uint8_t res = Mirf.sendPacket((mirfPacket*)&outPacket);
uartIncoming = 0;
if (res == 0) //packet was not transmitted
{
USART_Transmit("ER", 2);
awaitingResult = 0;
}
else //packet was sent, we will wait for sendResult and send it to usart
{
awaitingResult = 1;
}
}
if (awaitingResult && (Mirf.sendResult != 1) ) //we are waiting for send result and it changed from in queue to some other value
{
if (Mirf.sendResult == 0) {
USART_Transmit("OK", 2);
}
else if (Mirf.sendResult == TIMEOUT) {
USART_Transmit("TO", 2);
}
else if (Mirf.sendResult == MAX_ATTEMPTS) {
USART_Transmit("MA", 2);
}
awaitingResult = 0;
}
if ((uartIncoming) && (citac > 1) ) //timeout receiving whole packet
{
uartIncoming = 0; //reset receiving
}
}
return 0;
}