-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathsketch.ino.cpp
204 lines (155 loc) · 4.95 KB
/
sketch.ino.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
This file is part of the Arduino NINA firmware.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <rom/uart.h>
extern "C" {
#include <driver/periph_ctrl.h>
#include <driver/uart.h>
#include <esp_bt.h>
#include "esp_spiffs.h"
#include "esp_log.h"
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include "esp_partition.h"
}
#include <Arduino.h>
#include <SPIS.h>
#include <WiFi.h>
#include "CommandHandler.h"
#define SPI_BUFFER_LEN SPI_MAX_DMA_LEN
int debug = 0;
uint8_t* commandBuffer;
uint8_t* responseBuffer;
void dumpBuffer(const char* label, uint8_t data[], int length) {
ets_printf("%s: ", label);
for (int i = 0; i < length; i++) {
ets_printf("%02x", data[i]);
}
ets_printf("\r\n");
}
void setDebug(int d) {
debug = d;
if (debug) {
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[1], 0);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[3], 0);
const char* default_uart_dev = "/dev/uart/0";
_GLOBAL_REENT->_stdin = fopen(default_uart_dev, "r");
_GLOBAL_REENT->_stdout = fopen(default_uart_dev, "w");
_GLOBAL_REENT->_stderr = fopen(default_uart_dev, "w");
uart_div_modify(CONFIG_CONSOLE_UART_NUM, (APB_CLK_FREQ << 4) / 115200);
// uartAttach();
ets_install_uart_printf();
uart_tx_switch(CONFIG_CONSOLE_UART_NUM);
} else {
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[1], PIN_FUNC_GPIO);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[3], PIN_FUNC_GPIO);
_GLOBAL_REENT->_stdin = (FILE*) &__sf_fake_stdin;
_GLOBAL_REENT->_stdout = (FILE*) &__sf_fake_stdout;
_GLOBAL_REENT->_stderr = (FILE*) &__sf_fake_stderr;
ets_install_putc1(NULL);
ets_install_putc2(NULL);
}
}
void setupWiFi();
void setupBluetooth();
void setup() {
setDebug(debug);
// put SWD and SWCLK pins connected to SAMD as inputs
pinMode(15, INPUT);
pinMode(21, INPUT);
#if defined(NANO_RP2040_CONNECT)
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
#endif
pinMode(5, INPUT);
if (digitalRead(5) == LOW) {
setupBluetooth();
} else {
setupWiFi();
}
}
// #define UNO_WIFI_REV2
void setupBluetooth() {
periph_module_enable(PERIPH_UART1_MODULE);
periph_module_enable(PERIPH_UHCI0_MODULE);
#if defined(UNO_WIFI_REV2)
uart_set_pin(UART_NUM_1, 1, 3, 33, 0); // TX, RX, RTS, CTS
#elif defined(NANO_RP2040_CONNECT)
uart_set_pin(UART_NUM_1, 1, 3, 33, 12); // TX, RX, RTS, CTS
#else
uart_set_pin(UART_NUM_1, 23, 12, 18, 5);
#endif
uart_set_hw_flow_ctrl(UART_NUM_1, UART_HW_FLOWCTRL_CTS_RTS, 5);
esp_bt_controller_config_t btControllerConfig = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
btControllerConfig.hci_uart_no = UART_NUM_1;
#if defined(UNO_WIFI_REV2) || defined(NANO_RP2040_CONNECT)
btControllerConfig.hci_uart_baudrate = 115200;
#else
btControllerConfig.hci_uart_baudrate = 912600;
#endif
esp_bt_controller_init(&btControllerConfig);
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE);
esp_bt_controller_enable(ESP_BT_MODE_BLE);
esp_bt_sleep_enable();
vTaskSuspend(NULL);
while (1) {
vTaskDelay(portMAX_DELAY);
}
}
unsigned long getTime() {
int ret = 0;
do {
ret = WiFi.getTime();
} while (ret == 0);
return ret;
}
void setupWiFi() {
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
SPIS.begin();
esp_vfs_spiffs_conf_t conf = {
.base_path = "/fs",
.partition_label = "storage",
.max_files = 20,
.format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (WiFi.status() == WL_NO_SHIELD) {
while (1); // no shield
}
commandBuffer = (uint8_t*)heap_caps_malloc(SPI_BUFFER_LEN, MALLOC_CAP_DMA);
responseBuffer = (uint8_t*)heap_caps_malloc(SPI_BUFFER_LEN, MALLOC_CAP_DMA);
CommandHandler.begin();
}
void loop() {
// wait for a command
memset(commandBuffer, 0x00, SPI_BUFFER_LEN);
int commandLength = SPIS.transfer(NULL, commandBuffer, SPI_BUFFER_LEN);
if (commandLength == 0) {
return;
}
if (debug) {
dumpBuffer("COMMAND", commandBuffer, commandLength);
}
// process
memset(responseBuffer, 0x00, SPI_BUFFER_LEN);
int responseLength = CommandHandler.handle(commandBuffer, responseBuffer);
SPIS.transfer(responseBuffer, NULL, responseLength);
if (debug) {
dumpBuffer("RESPONSE", responseBuffer, responseLength);
}
}