-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigbee.c
304 lines (285 loc) · 9.7 KB
/
zigbee.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* zigbee.c
*
* Created on: Oct 4, 2017
* Author: sdev
*/
#include "zigbee.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/systick.h"
#include "utils/uartstdio.h"
#include "driverlib/pwm.h"
#include "modbus-rtu.h"
#include "system.h"
#include "config.h"
/*
*
*/
uint8_t g_UART3RX1[UARTRX_BUFFER_SIZE];
uint8_t g_UART3RX2[UARTRX_BUFFER_SIZE];
uint8_t g_UART3TX[UARTTX_BUFFER_SIZE];
bool timer_processing=false;
uint8_t bufferid=0;
/*
*
*
*/
#define ZIGBEE_FUNC16 16
#define ZIGBEE_FUNC03 3
#define ZIGBEE_BUFFER_SIZE 50
#define SLAVER_REG 10
#define MATCH_ADDR 0x00000001
#define MATCH_FUNC 0x00000010
uint32_t ZIGBEE_MASK;
/*
*
*/
uint32_t g_ui32SysClock;
volatile uint8_t g_bFeedWatchdog;
//int tram0;
void zigbeeInit(uint8_t addr, uint32_t uart)
{
g_bFeedWatchdog = false;
cfg_uartext(uart, 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
int i;
for(i=0;i<ROBOT_RXBUFFER_SIZE;i++)
{
ROBOTRX_Buffer[i] = 0;
ROBOTTX_Buffer[i] = 0;
}
// Timer 4
TimerClockSourceSet(TIMER4_BASE, TIMER_CLOCK_PIOSC);
ROM_TimerConfigure(TIMER4_BASE, TIMER_CFG_PERIODIC);
ROM_TimerLoadSet(TIMER4_BASE, TIMER_A, g_ui32SysClock/100);
ROM_IntEnable(INT_TIMER4A);
g_bFeedWatchdog = true;
ROM_TimerIntEnable(TIMER4_BASE, TIMER_TIMA_TIMEOUT);
ROM_TimerEnable(TIMER4_BASE, TIMER_A);
}
//*****************************************************************************
//
// The interrupt handler for the second timer interrupt.
//
//*****************************************************************************
uint32_t dma_previos_size = 0;
void Timer4IntHandler(void)
{
//
ROM_TimerIntClear(TIMER4_BASE, TIMER_TIMA_TIMEOUT);
uint32_t dma_current_size = uDMAChannelSizeGet(UDMA_CH16_UART3RX);
#ifdef ZIGBEE_DEBUG
UARTprintf("DMA in processing %d %d\n", dma_current_size, dma_previos_size);
#endif
if (dma_previos_size != 0 && dma_previos_size != dma_current_size || dma_current_size < UARTRX_BUFFER_SIZE)
{
modbusarrayProcessing(g_UART3RX1, sizeof(g_UART3RX1), 1);
ROM_uDMAChannelTransferSet(UDMA_CH16_UART3RX | UDMA_PRI_SELECT,
UDMA_MODE_BASIC,
(void *)(UART3_BASE + UART_O_DR),
g_UART3RX1, sizeof(g_UART3RX1));
MAP_uDMAChannelEnable(UDMA_CH16_UART3RX);
dma_previos_size = 0;
}
if (timer_processing == true)
{
timer_processing = false;
switch (bufferid)
{
case 1:
modbusarrayProcessing(g_UART3RX1, sizeof(g_UART3RX1), 1);
break;
case 2:
modbusarrayProcessing(g_UART3RX2, sizeof(g_UART3RX2), 1);
break;
default:
break;
}
}
dma_previos_size = dma_current_size;
}
// Vector Rx/Tx UART2 from RFID
//void UART3IntHandler(void)
//{
// UARTprintf("UART3 Interrupt !!! \n");
//// uint32_t status;
//// uint32_t mode;
//// status = MAP_UARTIntStatus(UART3_BASE, true);
//// MAP_UARTIntClear(UART3_BASE, status);
//// modbusarrayProcessing(g_UART3RX1, sizeof(g_UART3RX1), 1);
//
//
//// uint32_t channel;
////
// uint32_t status;
// uint32_t mode;
//
// status = MAP_UARTIntStatus(UART3_BASE, true);
// MAP_UARTIntClear(UART3_BASE, status);
//
// mode = MAP_uDMAChannelModeGet(UDMA_CH16_UART3RX | UDMA_PRI_SELECT);
//
// //channel = MAP_uDMAIntStatus();
//
// if (mode == UDMA_MODE_STOP && MAP_uDMAChannelIsEnabled(UDMA_CH16_UART3RX))
// {
// //MAP_UARTIntDisable(UART3_BASE, UART_INT_DMARX);
// modbusarrayProcessing(g_UART3RX1, sizeof(g_UART3RX1), 1);
// ROM_uDMAChannelTransferSet(UDMA_CH16_UART3RX | UDMA_PRI_SELECT,
// UDMA_MODE_PINGPONG,
// (void *) (UART3_BASE + UART_O_DR),
// g_UART3RX1, sizeof(g_UART3RX1));
// // MAP_uDMAChannelEnable(UDMA_CH16_UART3RX);
//
//// MAP_UARTDMADisable(UART3_BASE, UART_DMA_RX);
// }
//
//}
void
UART3IntHandler(void)
{
uint32_t ui32Status;
uint32_t ui32Mode;
//
// Read the interrupt status of the UART.
//
ui32Status = ROM_UARTIntStatus(UART3_BASE, 1);
//
// Clear any pending status, even though there should be none since no UART
// interrupts were enabled. If UART error interrupts were enabled, then
// those interrupts could occur here and should be handled. Since uDMA is
// used for both the RX and TX, then neither of those interrupts should be
// enabled.
//
ROM_UARTIntClear(UART3_BASE, ui32Status);
//
// Check the DMA control table to see if the ping-pong "A" transfer is
// complete. The "A" transfer uses receive buffer "A", and the primary
// control structure.
//
ui32Mode = ROM_uDMAChannelModeGet(UDMA_CH16_UART3RX | UDMA_PRI_SELECT);
//
// If the primary control structure indicates stop, that means the "A"
// receive buffer is done. The uDMA controller should still be receiving
// data into the "B" buffer.
//
bufferid=1;
if(ui32Mode == UDMA_MODE_STOP)
{
#ifdef ZIGBEE_DEBUG_LV1
UARTprintf("DMA UDMA_PRI_SELECT STOP\n");
int i;
UARTprintf("UDMA_PRI_SELECT DATA: ");
for (i = 0; i < UARTRX_BUFFER_SIZE; i++)
{
UARTprintf("%x ", g_UART3RX1[i]);
}
UARTprintf("\n");
#endif
//modbusarrayProcessing(g_UART3RX1, sizeof(g_UART3RX1), 1);
//
// Increment a counter to indicate data was received into buffer A. In
// a real application this would be used to signal the main thread that
// data was received so the main thread can process the data.
//
//g_ui32RxBufACount++;
//
// Set up the next transfer for the "A" buffer, using the primary
// control structure. When the ongoing receive into the "B" buffer is
// done, the uDMA controller will switch back to this one. This
// example re-uses buffer A, but a more sophisticated application could
// use a rotating set of buffers to increase the amount of time that
// the main thread has to process the data in the buffer before it is
// reused.
//
timer_processing = true;
ROM_uDMAChannelTransferSet(UDMA_CH16_UART3RX | UDMA_PRI_SELECT,
UDMA_MODE_PINGPONG,
(void *)(UART3_BASE + UART_O_DR),
g_UART3RX1, sizeof(g_UART3RX1));
MAP_uDMAChannelEnable(UDMA_CH16_UART3RX);
}
//
// Check the DMA control table to see if the ping-pong "B" transfer is
// complete. The "B" transfer uses receive buffer "B", and the alternate
// control structure.
//
ui32Mode = ROM_uDMAChannelModeGet(UDMA_CH16_UART3RX | UDMA_ALT_SELECT);
bufferid = 2;
//
// If the alternate control structure indicates stop, that means the "B"
// receive buffer is done. The uDMA controller should still be receiving
// data into the "A" buffer.
//
if(ui32Mode == UDMA_MODE_STOP)
{
#ifdef ZIGBEE_DEBUG_LV1
UARTprintf("DMA UDMA_ALT_SELECT STOP\n");
int i;
UARTprintf("UDMA_ALT_SELECT DATA: ");
for (i = 0; i < UARTRX_BUFFER_SIZE; i++)
{
UARTprintf("%x ", g_UART3RX2[i]);
}
UARTprintf("\n");
#endif
//modbusarrayProcessing(g_UART3RX2, sizeof(g_UART3RX2), 1);
//
// Increment a counter to indicate data was received into buffer A. In
// a real application this would be used to signal the main thread that
// data was received so the main thread can process the data.
//
// g_ui32RxBufBCount++;
//
// Set up the next transfer for the "B" buffer, using the alternate
// control structure. When the ongoing receive into the "A" buffer is
// done, the uDMA controller will switch back to this one. This
// example re-uses buffer B, but a more sophisticated application could
// use a rotating set of buffers to increase the amount of time that
// the main thread has to process the data in the buffer before it is
// reused.
//
ROM_uDMAChannelTransferSet(UDMA_CH16_UART3RX | UDMA_ALT_SELECT,
UDMA_MODE_PINGPONG,
(void *)(UART3_BASE + UART_O_DR),
g_UART3RX2, sizeof(g_UART3RX2));
MAP_uDMAChannelEnable(UDMA_CH16_UART3RX);
}
//
// If the UART1 DMA TX channel is disabled, that means the TX DMA transfer
// is done.
//
// if(!ROM_uDMAChannelIsEnabled(UDMA_CH17_UART3TX))
// {
// UARTprintf("DMA UDMA_CH17_UART3TX STOP\n");
//
// //
// // Start another DMA transfer to UART1 TX.
// //
// ROM_uDMAChannelTransferSet(UDMA_CH17_UART3TX | UDMA_PRI_SELECT,
// UDMA_MODE_BASIC, g_UART3TX,
// (void *)(UART3_BASE + UART_O_DR),
// sizeof(g_UART3TX));
//
// //
// // The uDMA TX channel must be re-enabled.
// //
// ROM_uDMAChannelEnable(UDMA_CH17_UART3TX);
// }
}