-
Notifications
You must be signed in to change notification settings - Fork 3
/
ibus.c
93 lines (72 loc) · 1.66 KB
/
ibus.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
/*
* flysky_ibus.h
*
* Created on: Feb 4, 2021
* Author: mokhwasomssi
*/
#include "ibus.h"
/* Static variable */
static uint8_t uart_rx_buffer[IBUS_LENGTH] = {0};
static uint8_t fail_safe_flag = 0;
/* Main Functions */
void ibus_init()
{
HAL_UART_Receive_DMA(IBUS_UART, uart_rx_buffer, 32);
}
bool ibus_read(uint16_t* ibus_data)
{
if(!ibus_is_valid())
return false;
if(!ibus_checksum())
return false;
ibus_update(ibus_data);
return true;
}
/* Sub Functions */
bool ibus_is_valid()
{
// is it ibus?
return (uart_rx_buffer[0] == IBUS_LENGTH && uart_rx_buffer[1] == IBUS_COMMAND40);
}
bool ibus_checksum()
{
uint16_t checksum_cal = 0xffff;
uint16_t checksum_ibus;
for(int i = 0; i < 30; i++)
{
checksum_cal -= uart_rx_buffer[i];
}
checksum_ibus = uart_rx_buffer[31] << 8 | uart_rx_buffer[30]; // checksum value from ibus
return (checksum_ibus == checksum_cal);
}
void ibus_update(uint16_t* ibus_data)
{
for(int ch_index = 0, bf_index = 2; ch_index < IBUS_USER_CHANNELS; ch_index++, bf_index += 2)
{
ibus_data[ch_index] = uart_rx_buffer[bf_index + 1] << 8 | uart_rx_buffer[bf_index];
}
}
/**
* @note FS-A8S don't have fail safe feature, So make software fail-safe.
*/
void ibus_soft_failsafe(uint16_t* ibus_data, uint8_t fail_safe_max)
{
fail_safe_flag++;
if(fail_safe_max > fail_safe_flag)
return;
// Clear ibus data
for(int i = 0; i < IBUS_USER_CHANNELS; i++)
ibus_data[i] = 0;
// Clear ibus buffer
for(int j = 0; j < IBUS_LENGTH; j++)
uart_rx_buffer[j] = 0;
fail_safe_flag = 0;
return;
}
/**
* @note This function is located in HAL_UART_RxCpltCallback.
*/
void ibus_reset_failsafe()
{
fail_safe_flag = 0; // flag reset
}