Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / io / tramp_protocol.c
blob499bd63ba6a9414ae7bcb4f86b04fdff1c99d332
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <string.h>
23 #include "platform.h"
25 #include "common/utils.h"
27 #include "tramp_protocol.h"
29 #define TRAMP_SYNC_START 0x0F
30 #define TRAMP_SYNC_STOP 0x00
31 #define TRAMP_COMMAND_SET_FREQ 'F' // 0x46
32 #define TRAMP_COMMAND_SET_POWER 'P' // 0x50
33 #define TRAMP_COMMAND_ACTIVE_STATE 'I' // 0x49
34 #define TRAMP_COMMAND_GET_CONFIG 'v' // 0x76
36 static uint8_t trampCrc(const trampFrame_t *frame)
38 uint8_t crc = 0;
39 const uint8_t *p = (const uint8_t *)frame;
40 const uint8_t *pEnd = p + (TRAMP_HEADER_LENGTH + TRAMP_PAYLOAD_LENGTH);
41 for (; p != pEnd; p++) {
42 crc += *p;
44 return crc;
47 static void trampFrameInit(uint8_t frameType, trampFrame_t *frame)
49 frame->header.syncStart = TRAMP_SYNC_START;
50 frame->header.command = frameType;
51 const uint8_t emptyPayload[TRAMP_PAYLOAD_LENGTH] = { 0 };
52 memcpy(frame->payload.buf, emptyPayload, sizeof(frame->payload.buf));
55 static void trampFrameClose(trampFrame_t *frame)
57 frame->footer.crc = trampCrc(frame);
58 frame->footer.syncStop = TRAMP_SYNC_STOP;
61 void trampFrameGetSettings(trampFrame_t *frame)
63 trampFrameInit(TRAMP_COMMAND_GET_CONFIG, frame);
64 trampFrameClose(frame);
67 void trampFrameSetFrequency(trampFrame_t *frame, const uint16_t frequency)
69 trampFrameInit(TRAMP_COMMAND_SET_FREQ, frame);
70 frame->payload.frequency = frequency;
71 trampFrameClose(frame);
74 void trampFrameSetPower(trampFrame_t *frame, const uint16_t power)
76 trampFrameInit(TRAMP_COMMAND_SET_POWER, frame);
77 frame->payload.power = power;
78 trampFrameClose(frame);
81 void trampFrameSetActiveState(trampFrame_t *frame, const bool active)
83 trampFrameInit(TRAMP_COMMAND_ACTIVE_STATE, frame);
84 frame->payload.active = (uint8_t) active;
85 trampFrameClose(frame);
88 bool trampParseResponseBuffer(trampSettings_t *settings, const uint8_t *buffer, size_t bufferLen)
90 if (bufferLen != TRAMP_FRAME_LENGTH) {
91 return false;
93 const trampFrame_t *frame = (const trampFrame_t *)buffer;
94 const uint8_t crc = trampCrc(frame);
95 if (crc != frame->footer.crc) {
96 return false;
98 memcpy(settings, &frame->payload.settings, sizeof(*settings));
99 return true;