-
Notifications
You must be signed in to change notification settings - Fork 7k
/
bg96_usb.hpp
84 lines (71 loc) · 2.43 KB
/
bg96_usb.hpp
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
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#pragma once
#include "usb/cdc_acm_host.h"
#include "esp_log.h"
#define BG96_VID (0x2C7C)
#define BG96_PID (0x0296)
#define BG96_AT_INTERFACE (2)
#define BG96_NMEA_INTERFACE (1)
class Bg96Usb {
public:
explicit Bg96Usb() : at_opened(false) {
};
esp_err_t at_start(cdc_acm_data_callback_t data_cb, void *user_arg)
{
// This driver doesn't support CDC notifications. This can lead to silent failures
const cdc_acm_host_device_config_t dev_config = {
.connection_timeout_ms = 10000,
.out_buffer_size = 64,
.event_cb = NULL,
.data_cb = data_cb,
.user_arg = user_arg,
};
ESP_ERROR_CHECK(this->at_port.open_vendor_specific(BG96_VID, BG96_PID, BG96_AT_INTERFACE, &dev_config));
this->at_opened = true;
// Some FW versions have Echo enabled by default. Disable it with ATE0 command
ESP_LOGD("BG96_USB", "Turning off echo with ATE0");
ESP_ERROR_CHECK(this->at_port.tx_blocking((uint8_t *)"ATE0\r", 5, 1000));
vTaskDelay(100);
return ESP_OK;
}
void at_stop()
{
this->at_port.close();
this->at_opened = false;
}
esp_err_t at_write(uint8_t *data, size_t len)
{
ESP_LOG_BUFFER_HEXDUMP("BG96_USB", data, len, ESP_LOG_DEBUG);
return this->at_port.tx_blocking(data, len, 1000);
}
esp_err_t gnss_start(cdc_acm_data_callback_t data_cb)
{
if (!this->at_opened) {
return ESP_ERR_INVALID_STATE;
}
const cdc_acm_host_device_config_t dev_config = {
.connection_timeout_ms = 1000,
.out_buffer_size = 0, // Read-only
.event_cb = NULL,
.data_cb = data_cb,
.user_arg = this,
};
ESP_ERROR_CHECK(this->nmea_port.open_vendor_specific(BG96_VID, BG96_PID, BG96_NMEA_INTERFACE, &dev_config));
return this->at_port.tx_blocking((uint8_t*)"AT+QGPS=1\r", 10, 1000);
}
esp_err_t gnss_stop()
{
esp_err_t ret = this->at_port.tx_blocking((uint8_t*)"AT+QGPSEND\r", 11, 1000);
this->nmea_port.close();
return ret;
}
protected:
CdcAcmDevice at_port; // Main control port for AT commands
CdcAcmDevice nmea_port; // Read only port for NMEA messages
private:
bool at_opened;
};