Skip to content

Commit 6b72148

Browse files
committed
rx threshold and timeout updated
1 parent b4fb858 commit 6b72148

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

main/crsf.c

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "driver/uart.h"
55
#include "driver/gpio.h"
66
#include "esp_log.h"
7+
#include "hal/uart_hal.h"
78

89
uint8_t crcSingleChar(uint8_t crc, uint8_t a)
910
{
@@ -29,6 +30,7 @@ static void echo_task(void *arg)
2930
{
3031
/* Configure parameters of an UART driver,
3132
* communication pins and install the driver */
33+
//uart driver erstellt eigene interrupts
3234
uart_config_t uart_config = {
3335
.baud_rate = 420000,
3436
.data_bits = UART_DATA_8_BITS,
@@ -42,7 +44,17 @@ static void echo_task(void *arg)
4244
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, 1024 * 2, 0, 0, NULL, 0));
4345
uart_set_line_inverse(UART_NUM_2,UART_SIGNAL_RXD_INV);
4446
ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
45-
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 4, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
47+
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, UART_PIN_NO_CHANGE, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
48+
49+
//interrupt auslösen bei timeout (primär) und bei überlauf eines thresholds
50+
uart_intr_config_t uart_intr = {
51+
.intr_enable_mask = UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL,
52+
.rx_timeout_thresh = 10,
53+
.rxfifo_full_thresh = 200,
54+
};
55+
//diese interrupt schwellwellen speichern
56+
ESP_ERROR_CHECK(uart_intr_config(UART_NUM_2, &uart_intr));
57+
ESP_ERROR_CHECK(uart_enable_rx_intr(UART_NUM_2));
4658

4759
// Configure a temporary buffer for the incoming data
4860
uint8_t *data = (uint8_t *) malloc(1024);
@@ -51,75 +63,86 @@ static void echo_task(void *arg)
5163
uint16_t channelData[16] = {0};
5264

5365
while (1) {
54-
// Read data from the UART
55-
int len = uart_read_bytes(UART_NUM_2, data, 1024, 20 / portTICK_RATE_MS);
66+
// get size in uart buffer
67+
int length = 0;
68+
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_2, (size_t*)&length));
5669

5770
//mit daten arbeiten, wenn welche vorhanden sind
58-
if(len > 0){
59-
//daten durchgehen
60-
for(int i = 0; i < len; i++){
61-
//Paketaufbau: 0xEE (Addresse) + 0x18 (Länge in Byte) + 0x16 (Typenfeld: Kanaldaten)
62-
if((data[i] == 0xEE) && (i+25)<len){
63-
if(data[i+2] == 0x16){
64-
//CRC überprüfen ob kein rest rauskommst
65-
if(crcMessage(data+i+2, data[i+1]) == 0){
66-
67-
//used = wie viel Bits bereits in einen Byte verwendet wurden
68-
int used = 0;
69-
//dataIndex = Index des Datenbytes im Buffer
70-
int dataIndex = i+3;
71-
72-
//Da 16 kanäle vorhanden sind muss es 16 mal durchgegangen werden
73-
for(int j = 0; j<16; j++){
74-
//Die restlichen vorhandenen Daten eines Bytes an LSB setzen
75-
uint16_t value = data[dataIndex] >> used;
76-
77-
//ein neues Byte muss verwendet werden
78-
dataIndex++;
79-
80-
//Anzahl der noch vorhanden Bits eines Bytes im angefangenen Byte
81-
uint8_t availableBits = 8-used;
82-
83-
if(11-availableBits > 8){
84-
//3Bytes werden benötigt, wenn für ein Kanal (11Bit) noch mehr als 8 Bit benötigt werden
85-
86-
//komplettes zweites Byte verwenden
87-
value = value | (data[dataIndex] << availableBits);
71+
if(length){
72+
73+
//read uart data
74+
int len = uart_read_bytes(UART_NUM_2, data, length, 20 / portTICK_RATE_MS);
75+
76+
//RX Buffer leeren wenn Frame gelesen wurde
77+
uart_flush(UART_NUM_2);
78+
79+
//len of data read from rx buffer
80+
if(len > 0){
81+
//daten durchgehen
82+
for(int i = 0; i < len; i++){
83+
//Paketaufbau: 0xEE (Addresse) + 0x18 (Länge in Byte) + 0x16 (Typenfeld: Kanaldaten)
84+
if((data[i] == 0xEE) && (i+25)<len){
85+
if(data[i+2] == 0x16){
86+
//CRC überprüfen ob kein rest rauskommst
87+
if(crcMessage(data+i+2, data[i+1]) == 0){
88+
89+
//used = wie viel Bits bereits in einen Byte verwendet wurden
90+
int used = 0;
91+
//dataIndex = Index des Datenbytes im Buffer
92+
int dataIndex = i+3;
93+
94+
//Da 16 kanäle vorhanden sind muss es 16 mal durchgegangen werden
95+
for(int j = 0; j<16; j++){
96+
//Die restlichen vorhandenen Daten eines Bytes an LSB setzen
97+
uint16_t value = data[dataIndex] >> used;
98+
8899
//ein neues Byte muss verwendet werden
89100
dataIndex++;
90101

91-
//vom dritten Byte noch die benötigten Bits in die MSB stellen schieben
92-
value = value | ((data[dataIndex]&((1U << (11-(8+availableBits))) - 1U)) << (8+availableBits));
102+
//Anzahl der noch vorhanden Bits eines Bytes im angefangenen Byte
103+
uint8_t availableBits = 8-used;
93104

94-
//Anzahl der verwendeten Bits im akutellen Byte neu berechnen
95-
used = 11-(8+availableBits);
96-
} else {
97-
//Wenn nur ein zweites Byte für 11Bit-Daten benötigt wird
105+
if(11-availableBits > 8){
106+
//3Bytes werden benötigt, wenn für ein Kanal (11Bit) noch mehr als 8 Bit benötigt werden
98107

99-
//Aus dem zweiten Byte noch die fehlenden Bits an die MSB stellen schieben von 11 Bit
100-
value = value | ((data[dataIndex]&((1U << (11-availableBits)) - 1U)) << availableBits);
108+
//komplettes zweites Byte verwenden
109+
value = value | (data[dataIndex] << availableBits);
110+
//ein neues Byte muss verwendet werden
111+
dataIndex++;
101112

102-
//berechnen wie viele Bits im aktuellen Byte schon verwendet wurden
103-
used = 11-availableBits;
104-
}
113+
//vom dritten Byte noch die benötigten Bits in die MSB stellen schieben
114+
value = value | ((data[dataIndex]&((1U << (11-(8+availableBits))) - 1U)) << (8+availableBits));
115+
116+
//Anzahl der verwendeten Bits im akutellen Byte neu berechnen
117+
used = 11-(8+availableBits);
118+
} else {
119+
//Wenn nur ein zweites Byte für 11Bit-Daten benötigt wird
120+
121+
//Aus dem zweiten Byte noch die fehlenden Bits an die MSB stellen schieben von 11 Bit
122+
value = value | ((data[dataIndex]&((1U << (11-availableBits)) - 1U)) << availableBits);
123+
124+
//berechnen wie viele Bits im aktuellen Byte schon verwendet wurden
125+
used = 11-availableBits;
126+
}
105127

106-
//Kanaldaten abspeichern
107-
channelData[j] = value;
128+
//Kanaldaten abspeichern
129+
channelData[j] = value;
130+
}
131+
//Kanaldaten ausgeben
132+
ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData[0], channelData[1], channelData[2], channelData[3], channelData[4], channelData[5], channelData[6], channelData[7], channelData[8], channelData[9], channelData[10], channelData[11], channelData[12], channelData[13], channelData[14], channelData[15]);
133+
break;
108134
}
109-
//Kanaldaten ausgeben
110-
ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData[0], channelData[1], channelData[2], channelData[3], channelData[4], channelData[5], channelData[6], channelData[7], channelData[8], channelData[9], channelData[10], channelData[11], channelData[12], channelData[13], channelData[14], channelData[15]);
111-
break;
112135
}
113136
}
114137
}
115138
}
116-
//RX Buffer leeren wenn Frame gelesen wurde
117-
uart_flush(UART_NUM_2);
139+
} else {
140+
vTaskDelay(10 / portTICK_PERIOD_MS);
118141
}
119142
}
120143
}
121144

122145
void app_main(void)
123146
{
124147
xTaskCreate(echo_task, "uart_echo_task", 2048, NULL, 10, NULL);
125-
}
148+
}

0 commit comments

Comments
 (0)