Skip to content

Commit

Permalink
Added flow control
Browse files Browse the repository at this point in the history
flow control can be enabled by setting RTS and CTS pins to different GPIOs. Otherwise flow control is disabled (as by default)
  • Loading branch information
seeul8er committed Feb 26, 2024
1 parent d4bfd36 commit 71422b1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
18 changes: 17 additions & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
.toast-close,.toastify{color:#fff;cursor:pointer}.toastify{padding:12px 20px;display:inline-block;box-shadow:0 3px 6px -1px rgba(0,0,0,.12),0 10px 36px -4px rgba(77,96,232,.3);background:-webkit-linear-gradient(315deg,#73a5ff,#5477f5);background:linear-gradient(135deg,#73a5ff,#5477f5);position:fixed;opacity:0;transition:.4s cubic-bezier(.215, .61, .355, 1);border-radius:2px;text-decoration:none;max-width:calc(50% - 20px);z-index:2147483647}.toastify.on{opacity:1}.toast-close{background:0 0;border:0;font-family:inherit;font-size:1em;opacity:.4;padding:0 5px}.toastify-right{right:15px}.toastify-left{left:15px}.toastify-top{top:-150px}.toastify-bottom{bottom:-150px}.toastify-rounded{border-radius:25px}.toastify-avatar{width:1.5em;height:1.5em;margin:-7px 5px;border-radius:2px}.toastify-center{margin-left:auto;margin-right:auto;left:0;right:0;max-width:fit-content;}@media only screen and (max-width:360px){.toastify-left,.toastify-right{margin-left:auto;margin-right:auto;left:0;right:0;max-width:fit-content}}
</style>
<style>
form,h3{margin-bottom:.5rem}.dot_green,.dot_red{height:.8rem;width:.8rem;border-radius:50%;display:inline-block}html{min-height:100%}body{height:100%;margin:0;color:#fff;background:#003166;background:linear-gradient(180deg,#003166 0,#135ba8 100%)}h2{font-size:x-large;padding-top:2rem;margin-bottom:.8rem}h3{font-family:Arial,sans-serif;font-size:large}button.button-primary{background-color:#ff881a;border-color:grey}button.button-primary:hover{background-color:#003166;color:#ff881a;border-color:#ff881a}button.button-primary:focus{background-color:#003166;color:#9a4e0b;border-color:#9a4e0b}input,select{color:#000}.dot_green{background-color:#68b838}.dot_red{background-color:#f63e3e}
label{font-weight:normal;font-size:small}form,h3{margin-bottom:.5rem}.dot_green,.dot_red{height:.8rem;width:.8rem;border-radius:50%;display:inline-block}html{min-height:100%}body{height:100%;margin:0;color:#fff;background:#003166;background:linear-gradient(180deg,#003166 0,#135ba8 100%)}h2{font-size:x-large;padding-top:2rem;margin-bottom:.8rem}h3{font-family:Arial,sans-serif;font-size:large}button.button-primary{background-color:#ff881a;border-color:grey}button.button-primary:hover{background-color:#003166;color:#ff881a;border-color:#ff881a}button.button-primary:focus{background-color:#003166;color:#9a4e0b;border-color:#9a4e0b}input,select{color:#000}.dot_green{background-color:#68b838}.dot_red{background-color:#f63e3e}
</style>
<script type="text/javascript">
/*!
Expand Down Expand Up @@ -124,6 +124,22 @@ <h3>Serial</h3>
<input type="text" name="rx_pin" value="" id="rx_pin">
</div>
</div>
<div class="row">
<div class="six columns">
<label for="rts_pin">UART RTS Pin</label>
<input type="text" name="rts_pin" value="" id="rts_pin">
</div>
<div class="six columns">
<label for="cts_pin">UART CTS Pin</label>
<input type="text" name="cts_pin" value="" id="cts_pin">
</div>
</div>
<div class="row">
<div class="six columns">
<label for="rts_thresh">UART RTS threshold</label>
<input type="number" id="rts_thresh" name="rts_thresh" min="1" max="128">
</div>
</div>
<div class="row">
<div class="six columns">
<label for="telem_proto">UART serial protocol</label>
Expand Down
17 changes: 14 additions & 3 deletions main/db_esp32_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,28 @@ uint ltm_frames_in_buffer_pnt = 0;
uint32_t uart_byte_count = 0;
int8_t num_connected_tcp_clients = 0;

/**
* Opens UART socket.
* Enables UART flow control if RTS and CTS pins do NOT match.
*
* 8 data bits, no parity, 1 stop bit
* @return ESP_ERROR of uart_driver_install
*/
esp_err_t open_serial_socket() {
bool flow_control = DB_UART_PIN_CTS != DB_UART_PIN_RTS;
uart_config_t uart_config = {
.baud_rate = DB_UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
.flow_ctrl = flow_control ? UART_HW_FLOWCTRL_CTS_RTS : UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = DB_UART_RTS_THRESH,
};
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, DB_UART_PIN_TX, DB_UART_PIN_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
return uart_driver_install(UART_NUM, 1024, 0, 0, NULL, 0);
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, DB_UART_PIN_TX, DB_UART_PIN_RX,
flow_control ? DB_UART_PIN_RTS : UART_PIN_NO_CHANGE,
flow_control ? DB_UART_PIN_CTS : UART_PIN_NO_CHANGE));
return uart_driver_install(UART_NUM, 1024, 0, 10, NULL, 0);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions main/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ extern uint8_t DEFAULT_CHANNEL;
extern uint8_t SERIAL_PROTOCOL; // 1=MSP, 3=MAVLink/transparent
extern uint8_t DB_UART_PIN_TX; // set TX & RX pin to the same number to indicate vanilla system
extern uint8_t DB_UART_PIN_RX;
extern uint8_t DB_UART_PIN_RTS;
extern uint8_t DB_UART_PIN_CTS;
extern uint8_t DB_UART_RTS_THRESH;
extern int DB_UART_BAUD_RATE;
extern uint16_t TRANSPARENT_BUF_SIZE;
extern uint8_t LTM_FRAME_NUM_BUFFER; // Number of LTM frames per UDP packet (min = 1; max = 5)
Expand Down
7 changes: 7 additions & 0 deletions main/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ static esp_err_t settings_change_post_handler(httpd_req_t *req) {
if (json) DB_UART_PIN_TX = json->valueint;
json = cJSON_GetObjectItem(root, "rx_pin");
if (json) DB_UART_PIN_RX = json->valueint;
json = cJSON_GetObjectItem(root, "rts_pin");
if (json) DB_UART_PIN_RTS = json->valueint;
json = cJSON_GetObjectItem(root, "cts_pin");
if (json) DB_UART_PIN_CTS = json->valueint;
json = cJSON_GetObjectItem(root, "rts_thresh");
if (json) DB_UART_RTS_THRESH = json->valueint;

json = cJSON_GetObjectItem(root, "baud");
if (json) DB_UART_BAUD_RATE = json->valueint;

Expand Down
14 changes: 12 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ char CURRENT_CLIENT_IP[32] = "192.168.2.1";
uint8_t DEFAULT_CHANNEL = 6;
uint8_t SERIAL_PROTOCOL = 4; // 1=MSP, 4=MAVLink/transparent

// initially set both pins to 0 to allow the start of the system on all boards. User has to set the correct pins
// initially set pins to 0 to allow the start of the system on all boards. User has to set the correct pins
uint8_t DB_UART_PIN_TX = GPIO_NUM_0;
uint8_t DB_UART_PIN_RX = GPIO_NUM_0;
uint8_t DB_UART_PIN_RTS = GPIO_NUM_0;
uint8_t DB_UART_PIN_CTS = GPIO_NUM_0;
uint8_t DB_UART_RTS_THRESH = 100;

int32_t DB_UART_BAUD_RATE = 115200;
uint16_t TRANSPARENT_BUF_SIZE = 64;
Expand Down Expand Up @@ -327,9 +330,10 @@ int init_wifi_clientmode() {

void write_settings_to_nvs() {
ESP_LOGI(TAG,
"Trying to save: ssid %s\nwifi_pass %s\nwifi_chan %i\nbaud %liu\ngpio_tx %i\ngpio_rx %i\nproto %i\n"
"Trying to save: ssid %s\nwifi_pass %s\nwifi_chan %i\nbaud %liu\ngpio_tx %i\ngpio_rx %i\ngpio_cts %i\ngpio_rts %i\nrts_thresh %i\nproto %i\n"
"trans_pack_size %i\nltm_per_packet %i\nmsp_ltm %i\nap_ip %s",
DEFAULT_SSID, DEFAULT_PWD, DEFAULT_CHANNEL, DB_UART_BAUD_RATE, DB_UART_PIN_TX, DB_UART_PIN_RX,
DB_UART_PIN_CTS, DB_UART_PIN_RTS, DB_UART_RTS_THRESH,
SERIAL_PROTOCOL, TRANSPARENT_BUF_SIZE, LTM_FRAME_NUM_BUFFER, MSP_LTM_SAMEPORT, DEFAULT_AP_IP);
ESP_LOGI(TAG, "Saving to NVS %s", NVS_NAMESPACE);
nvs_handle my_handle;
Expand All @@ -341,6 +345,9 @@ void write_settings_to_nvs() {
ESP_ERROR_CHECK(nvs_set_i32(my_handle, "baud", DB_UART_BAUD_RATE));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "gpio_tx", DB_UART_PIN_TX));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "gpio_rx", DB_UART_PIN_RX));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "gpio_cts", DB_UART_PIN_CTS));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "gpio_rts", DB_UART_PIN_RTS));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "rts_thresh", DB_UART_RTS_THRESH));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "proto", SERIAL_PROTOCOL));
ESP_ERROR_CHECK(nvs_set_u16(my_handle, "trans_pack_size", TRANSPARENT_BUF_SIZE));
ESP_ERROR_CHECK(nvs_set_u8(my_handle, "ltm_per_packet", LTM_FRAME_NUM_BUFFER));
Expand Down Expand Up @@ -383,6 +390,9 @@ void read_settings_nvs() {
ESP_ERROR_CHECK(nvs_get_i32(my_handle, "baud", &DB_UART_BAUD_RATE));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "gpio_tx", &DB_UART_PIN_TX));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "gpio_rx", &DB_UART_PIN_RX));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "gpio_cts", &DB_UART_PIN_CTS));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "gpio_rts", &DB_UART_PIN_RTS));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "rts_thresh", &DB_UART_RTS_THRESH));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "proto", &SERIAL_PROTOCOL));
ESP_ERROR_CHECK(nvs_get_u16(my_handle, "trans_pack_size", &TRANSPARENT_BUF_SIZE));
ESP_ERROR_CHECK(nvs_get_u8(my_handle, "ltm_per_packet", &LTM_FRAME_NUM_BUFFER));
Expand Down

0 comments on commit 71422b1

Please sign in to comment.