/
syslink.c
203 lines (168 loc) · 4.84 KB
/
syslink.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
/*
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2012 BitCraze AB
*
* 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, in version 3.
*
* 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/>.
*
* syslink.c: Communication between NRF51 and STM32
*/
#define DEBUG_MODULE "SL"
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "timers.h"
#include "config.h"
#include "autoconf.h"
#include "debug.h"
#include "syslink.h"
#include "radiolink.h"
#include "uart_syslink.h"
#include "configblock.h"
#include "pm.h"
#include "ow.h"
#include "static_mem.h"
#include "system.h"
#include "param.h"
static bool isInit = false;
static uint8_t sendBuffer[SYSLINK_MTU + 6];
static void syslinkRouteIncommingPacket(SyslinkPacket *slp);
static xTimerHandle debugTimer;
static uint8_t triggerDebugProbe;
static void debugHandler(xTimerHandle timer);
static void debugSyslinkReceive(SyslinkPacket *slp);
static xSemaphoreHandle syslinkAccess;
STATIC_MEM_TASK_ALLOC_STACK_NO_DMA_CCM_SAFE(syslinkTask, SYSLINK_TASK_STACKSIZE);
/* Syslink task, handles communication between nrf and stm and dispatch messages
*/
static void syslinkTask(void *param)
{
SyslinkPacket slp;
while(1)
{
uartslkGetPacketBlocking(&slp);
syslinkRouteIncommingPacket(&slp);
}
}
static void syslinkRouteIncommingPacket(SyslinkPacket *slp)
{
uint8_t groupType;
groupType = slp->type & SYSLINK_GROUP_MASK;
switch (groupType)
{
case SYSLINK_RADIO_GROUP:
radiolinkSyslinkDispatch(slp);
break;
case SYSLINK_PM_GROUP:
pmSyslinkUpdate(slp);
break;
case SYSLINK_OW_GROUP:
owSyslinkReceive(slp);
break;
case SYSLINK_SYS_GROUP:
systemSyslinkReceive(slp);
break;
case SYSLINK_DEBUG_GROUP:
debugSyslinkReceive(slp);
break;
default:
DEBUG_PRINT("Unknown packet:%X.\n", slp->type);
break;
}
}
/*
* Public functions
*/
void syslinkInit()
{
if(isInit) {
return;
}
vSemaphoreCreateBinary(syslinkAccess);
STATIC_MEM_TASK_CREATE(syslinkTask, syslinkTask, SYSLINK_TASK_NAME, NULL, SYSLINK_TASK_PRI);
debugTimer = xTimerCreate( "syslinkTimer", M2T(1000), pdTRUE, NULL, debugHandler );
xTimerStart(debugTimer, M2T(1000));
isInit = true;
}
bool syslinkTest()
{
return isInit;
}
bool isSyslinkUp()
{
return isInit;
}
int syslinkSendPacket(SyslinkPacket *slp)
{
int i = 0;
int dataSize;
uint8_t cksum[2] = {0};
xSemaphoreTake(syslinkAccess, portMAX_DELAY);
ASSERT(slp->length <= SYSLINK_MTU);
sendBuffer[0] = SYSLINK_START_BYTE1;
sendBuffer[1] = SYSLINK_START_BYTE2;
sendBuffer[2] = slp->type;
sendBuffer[3] = slp->length;
memcpy(&sendBuffer[4], slp->data, slp->length);
dataSize = slp->length + 6;
// Calculate checksum delux
for (i = 2; i < dataSize - 2; i++)
{
cksum[0] += sendBuffer[i];
cksum[1] += cksum[0];
}
sendBuffer[dataSize-2] = cksum[0];
sendBuffer[dataSize-1] = cksum[1];
uartslkSendDataDmaBlocking(dataSize, sendBuffer);
xSemaphoreGive(syslinkAccess);
return 0;
}
static void debugHandler(xTimerHandle timer) {
static SyslinkPacket txPacket;
if (triggerDebugProbe) {
triggerDebugProbe = 0;
uartSyslinkDumpDebugProbe();
DEBUG_PRINT("Syslink NRF debug probe initialized\n");
txPacket.type = SYSLINK_DEBUG_PROBE;
txPacket.length = 0;
syslinkSendPacket(&txPacket);
}
}
static void debugSyslinkReceive(SyslinkPacket *slp) {
if (slp->type == SYSLINK_DEBUG_PROBE) {
DEBUG_PRINT("NRF Address received: %d\n", slp->data[0]);
DEBUG_PRINT("NRF Chan received: %d\n", slp->data[1]);
DEBUG_PRINT("NRF Rate received: %d\n", slp->data[2]);
DEBUG_PRINT("NRF Dropped: %d\n", slp->data[3]);
DEBUG_PRINT("NRF uart error code: %d\n", slp->data[4]);
DEBUG_PRINT("NRF uart error count: %d\n", slp->data[5]);
DEBUG_PRINT("NRF uart checksum 1 fail count: %d\n", slp->data[6]);
DEBUG_PRINT("NRF uart checksum 2 fail count: %d\n", slp->data[7]);
}
}
PARAM_GROUP_START(syslink)
/**
* @brief Trigger syslink debug probe in the NRF by setting to 1
*/
PARAM_ADD(PARAM_UINT8, probe, &triggerDebugProbe)
PARAM_GROUP_STOP(syslink)