/
decode_dshot.h
136 lines (104 loc) · 2.7 KB
/
decode_dshot.h
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
#pragma once
#include <ch.h>
#include <hal.h>
#include "hal_stm32_dma.h"
#define DSHOT_MIN_THROTTLE 48U
#define DSHOT_MAX_THROTTLE 2047U
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Type of a structure representing an DecodeDSHOT driver.
*/
typedef struct DecodeDSHOTDriver DecodeDSHOTDriver;
/**
* @brief DecodeDSHOT Driver configuration structure.
*/
typedef struct {
/**
* @brief : dma stream associated with icu timer used to generate decode_dshot output
*/
uint32_t dma_stream;
/**
* @brief : dma channel associated with icu timer used to generate decode_dshot output
*/
uint8_t dma_channel;
/**
* @brief ICU driver that can listen on channel 1 ou channel 2
*/
ICUDriver *icup;
icuchannel_t icu_channel;
} DecodeDSHOTConfig;
typedef enum {ESC_NONE, ESC_PWM50, ESC_PWM400,
ESC_DSHOT150, ESC_DSHOT300, ESC_DSHOT600} EscCmdMode;
typedef struct {
uint32_t w;
uint32_t p;
} WidthPeriod ;
typedef union {
struct {
uint16_t crc:4;
uint16_t telemetryRequest:1;
uint16_t throttle:11;
};
uint16_t rawFrame;
} DshotPacket;
/* typedef struct { */
/* uint16_t dshotFrequency; */
/* DshotPacket packet; */
/* } DecodeDSHOTFrame; */
typedef struct {
EscCmdMode mode;
union{
uint8_t percent;
DshotPacket dshotp;
};
} DecodeESCFrame;
void decodeDshotStart(DecodeDSHOTDriver *driver, const DecodeDSHOTConfig *config);
DecodeESCFrame decodeDshotCapture(DecodeDSHOTDriver *driver);
void decodeDshotStop(DecodeDSHOTDriver *driver);
/*
# _ __ _ _
# | '_ \ (_) | |
# | |_) | _ __ _ __ __ __ _ | |_ ___
# | .__/ | '__| | | \ \ / / / _` | | __| / _ \
# | | | | | | \ V / | (_| | \ |_ | __/
# |_| |_| |_| \_/ \__,_| \__| \___|
*/
#define DMA_DATA_LEN 64U
typedef union {
WidthPeriod wp[DMA_DATA_LEN / 2];
uint32_t raw[DMA_DATA_LEN];
} WidthPeriodBuffer;
/**
* @brief DecodeDSHOT driver structure.
*/
struct DecodeDSHOTDriver {
/**
* @brief DMA config associated with icu timer
*/
const DecodeDSHOTConfig *config;
/**
* @brief DMA config associated with icu timer
*/
DMAConfig dma_conf;
/**
* @brief ICU config associated with icu timer
*/
ICUConfig icu_conf;
/**
* @brief DMA driver associated with icu timer
*/
DMADriver dmap;
/**
* @brief DMA buffer to store widths and periods
*/
WidthPeriodBuffer wp_buffer;
/**
* @brief timer width in bytes : 2 or 4
*/
uint8_t timer_width;
};
#ifdef __cplusplus
}
#endif