Skip to content

Commit 7e43fd9

Browse files
committed
New ESP IDF examples: BLE, Coap Server, Peripherals UART, Storage SDCard
1 parent 0911455 commit 7e43fd9

File tree

28 files changed

+1773
-0
lines changed

28 files changed

+1773
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.pioenvs
2+
.clang_complete
3+
.gcc-flags.json
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < http://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < http://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choice one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
#
39+
# script:
40+
# - platformio run
41+
42+
43+
#
44+
# Template #2: The project is intended to by used as a library with examples
45+
#
46+
47+
# language: python
48+
# python:
49+
# - "2.7"
50+
#
51+
# sudo: false
52+
# cache:
53+
# directories:
54+
# - "~/.platformio"
55+
#
56+
# env:
57+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
58+
# - PLATFORMIO_CI_SRC=examples/file.ino
59+
# - PLATFORMIO_CI_SRC=path/to/test/directory
60+
#
61+
# install:
62+
# - pip install -U platformio
63+
#
64+
# script:
65+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. Copyright 2014-present PlatformIO <[email protected]>
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
12+
How to build PlatformIO based project
13+
=====================================
14+
15+
1. `Install PlatformIO Core <http://docs.platformio.org/page/core.html>`_
16+
2. Download `examples source code <https://github.com/platformio/platformio-examples/archive/develop.zip>`_
17+
3. Extract ZIP archive
18+
4. Run these commands:
19+
20+
.. code-block:: bash
21+
22+
# Change directory to example
23+
> cd platformio-examples/espressif/esp32-espidf-ble-adv
24+
25+
# Build project
26+
> platformio run
27+
28+
# Upload firmware
29+
> platformio run --target upload
30+
31+
# Build specific environment
32+
> platformio run -e esp32dev
33+
34+
# Upload firmware for the specific environment
35+
> platformio run -e esp32dev --target upload
36+
37+
# Clean build files
38+
> platformio run --target clean
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
This directory is intended for the project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link to executable file.
4+
5+
The source code of each library should be placed in separate directory, like
6+
"lib/private_lib/[here are source files]".
7+
8+
For example, see how can be organised `Foo` and `Bar` libraries:
9+
10+
|--lib
11+
| |--Bar
12+
| | |--docs
13+
| | |--examples
14+
| | |--src
15+
| | |- Bar.c
16+
| | |- Bar.h
17+
| |--Foo
18+
| | |- Foo.c
19+
| | |- Foo.h
20+
| |- readme.txt --> THIS FILE
21+
|- platformio.ini
22+
|--src
23+
|- main.c
24+
25+
Then in `src/main.c` you should use:
26+
27+
#include <Foo.h>
28+
#include <Bar.h>
29+
30+
// rest H/C/CPP code
31+
32+
PlatformIO will find your libraries automatically, configure preprocessor's
33+
include paths and build them.
34+
35+
See additional options for PlatformIO Library Dependency Finder `lib_*`:
36+
37+
http://docs.platformio.org/page/projectconf.html#lib-install
38+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; http://docs.platformio.org/page/projectconf.html
9+
10+
[env:nano32]
11+
platform = espressif32
12+
framework = espidf
13+
board = nano32
14+
build_flags = -DCONFIG_WIFI_SSID=\"ESP_AP\" -DCONFIG_WIFI_PASSWORD=\"MYPASS\"
15+
16+
[env:espea32]
17+
platform = espressif32
18+
framework = espidf
19+
board = espea32
20+
build_flags = -DCONFIG_WIFI_SSID=\"ESP_AP\" -DCONFIG_WIFI_PASSWORD=\"MYPASS\"
21+
22+
[env:esp32dev]
23+
platform = espressif32
24+
framework = espidf
25+
board = esp32dev
26+
build_flags = -DCONFIG_WIFI_SSID=\"ESP_AP\" -DCONFIG_WIFI_PASSWORD=\"MYPASS\"
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stdio.h>
16+
#include "freertos/FreeRTOS.h"
17+
#include "freertos/task.h"
18+
#include "bt.h"
19+
#include <string.h>
20+
21+
#define HCI_H4_CMD_PREAMBLE_SIZE (4)
22+
23+
/* HCI Command opcode group field(OGF) */
24+
#define HCI_GRP_HOST_CONT_BASEBAND_CMDS (0x03 << 10) /* 0x0C00 */
25+
#define HCI_GRP_BLE_CMDS (0x08 << 10)
26+
27+
#define HCI_RESET (0x0003 | HCI_GRP_HOST_CONT_BASEBAND_CMDS)
28+
#define HCI_BLE_WRITE_ADV_ENABLE (0x000A | HCI_GRP_BLE_CMDS)
29+
#define HCI_BLE_WRITE_ADV_PARAMS (0x0006 | HCI_GRP_BLE_CMDS)
30+
#define HCI_BLE_WRITE_ADV_DATA (0x0008 | HCI_GRP_BLE_CMDS)
31+
32+
#define HCIC_PARAM_SIZE_WRITE_ADV_ENABLE (1)
33+
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS (15)
34+
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA (31)
35+
36+
#define BD_ADDR_LEN (6) /* Device address length */
37+
typedef uint8_t bd_addr_t[BD_ADDR_LEN]; /* Device address */
38+
39+
#define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);}
40+
#define UINT8_TO_STREAM(p, u8) {*(p)++ = (uint8_t)(u8);}
41+
#define BDADDR_TO_STREAM(p, a) {int ijk; for (ijk = 0; ijk < BD_ADDR_LEN; ijk++) *(p)++ = (uint8_t) a[BD_ADDR_LEN - 1 - ijk];}
42+
#define ARRAY_TO_STREAM(p, a, len) {int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[ijk];}
43+
44+
enum {
45+
H4_TYPE_COMMAND = 1,
46+
H4_TYPE_ACL = 2,
47+
H4_TYPE_SCO = 3,
48+
H4_TYPE_EVENT = 4
49+
};
50+
51+
static uint8_t hci_cmd_buf[128];
52+
53+
/*
54+
* @brief: BT controller callback function, used to notify the upper layer that
55+
* controller is ready to receive command
56+
*/
57+
static void controller_rcv_pkt_ready(void)
58+
{
59+
printf("controller rcv pkt ready\n");
60+
}
61+
62+
/*
63+
* @brief: BT controller callback function, to transfer data packet to upper
64+
* controller is ready to receive command
65+
*/
66+
static int host_rcv_pkt(uint8_t *data, uint16_t len)
67+
{
68+
printf("host rcv pkt: ");
69+
for (uint16_t i = 0; i < len; i++) {
70+
printf("%02x", data[i]);
71+
}
72+
printf("\n");
73+
return 0;
74+
}
75+
76+
static esp_vhci_host_callback_t vhci_host_cb = {
77+
controller_rcv_pkt_ready,
78+
host_rcv_pkt
79+
};
80+
81+
static uint16_t make_cmd_reset(uint8_t *buf)
82+
{
83+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
84+
UINT16_TO_STREAM (buf, HCI_RESET);
85+
UINT8_TO_STREAM (buf, 0);
86+
return HCI_H4_CMD_PREAMBLE_SIZE;
87+
}
88+
89+
static uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable)
90+
{
91+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
92+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_ENABLE);
93+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE);
94+
UINT8_TO_STREAM (buf, adv_enable);
95+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE;
96+
}
97+
98+
static uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max,
99+
uint8_t adv_type, uint8_t addr_type_own,
100+
uint8_t addr_type_dir, bd_addr_t direct_bda,
101+
uint8_t channel_map, uint8_t adv_filter_policy)
102+
{
103+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
104+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_PARAMS);
105+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS );
106+
107+
UINT16_TO_STREAM (buf, adv_int_min);
108+
UINT16_TO_STREAM (buf, adv_int_max);
109+
UINT8_TO_STREAM (buf, adv_type);
110+
UINT8_TO_STREAM (buf, addr_type_own);
111+
UINT8_TO_STREAM (buf, addr_type_dir);
112+
BDADDR_TO_STREAM (buf, direct_bda);
113+
UINT8_TO_STREAM (buf, channel_map);
114+
UINT8_TO_STREAM (buf, adv_filter_policy);
115+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS;
116+
}
117+
118+
119+
static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data)
120+
{
121+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
122+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_DATA);
123+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
124+
125+
memset(buf, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA);
126+
127+
if (p_data != NULL && data_len > 0) {
128+
if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
129+
data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
130+
}
131+
132+
UINT8_TO_STREAM (buf, data_len);
133+
134+
ARRAY_TO_STREAM (buf, p_data, data_len);
135+
}
136+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1;
137+
}
138+
139+
static void hci_cmd_send_reset(void)
140+
{
141+
uint16_t sz = make_cmd_reset (hci_cmd_buf);
142+
esp_vhci_host_send_packet(hci_cmd_buf, sz);
143+
}
144+
145+
static void hci_cmd_send_ble_adv_start(void)
146+
{
147+
uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1);
148+
esp_vhci_host_send_packet(hci_cmd_buf, sz);
149+
}
150+
151+
static void hci_cmd_send_ble_set_adv_param(void)
152+
{
153+
uint16_t adv_intv_min = 256; // 160ms
154+
uint16_t adv_intv_max = 256; // 160ms
155+
uint8_t adv_type = 0; // connectable undirected advertising (ADV_IND)
156+
uint8_t own_addr_type = 0; // Public Device Address
157+
uint8_t peer_addr_type = 0; // Public Device Address
158+
uint8_t peer_addr[6] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85};
159+
uint8_t adv_chn_map = 0x07; // 37, 38, 39
160+
uint8_t adv_filter_policy = 0; // Process All Conn and Scan
161+
162+
uint16_t sz = make_cmd_ble_set_adv_param(hci_cmd_buf,
163+
adv_intv_min,
164+
adv_intv_max,
165+
adv_type,
166+
own_addr_type,
167+
peer_addr_type,
168+
peer_addr,
169+
adv_chn_map,
170+
adv_filter_policy);
171+
esp_vhci_host_send_packet(hci_cmd_buf, sz);
172+
}
173+
174+
static void hci_cmd_send_ble_set_adv_data(void)
175+
{
176+
char *adv_name = "ESP-BLE-HELLO";
177+
uint8_t name_len = (uint8_t)strlen(adv_name);
178+
uint8_t adv_data[31] = {0x02, 0x01, 0x06, 0x0, 0x09};
179+
uint8_t adv_data_len;
180+
181+
adv_data[3] = name_len + 1;
182+
for (int i = 0; i < name_len; i++) {
183+
adv_data[5 + i] = (uint8_t)adv_name[i];
184+
}
185+
adv_data_len = 5 + name_len;
186+
187+
uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data);
188+
esp_vhci_host_send_packet(hci_cmd_buf, sz);
189+
}
190+
191+
/*
192+
* @brief: send HCI commands to perform BLE advertising;
193+
*/
194+
void bleAdvtTask(void *pvParameters)
195+
{
196+
int cmd_cnt = 0;
197+
bool send_avail = false;
198+
esp_vhci_host_register_callback(&vhci_host_cb);
199+
printf("BLE advt task start\n");
200+
while (1) {
201+
vTaskDelay(1000 / portTICK_PERIOD_MS);
202+
send_avail = esp_vhci_host_check_send_available();
203+
if (send_avail) {
204+
switch (cmd_cnt) {
205+
case 0: hci_cmd_send_reset(); ++cmd_cnt; break;
206+
case 1: hci_cmd_send_ble_set_adv_param(); ++cmd_cnt; break;
207+
case 2: hci_cmd_send_ble_set_adv_data(); ++cmd_cnt; break;
208+
case 3: hci_cmd_send_ble_adv_start(); ++cmd_cnt; break;
209+
}
210+
}
211+
printf("BLE Advertise, flag_send_avail: %d, cmd_sent: %d\n", send_avail, cmd_cnt);
212+
}
213+
}
214+
215+
void app_main()
216+
{
217+
esp_bt_controller_init();
218+
xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
219+
}

0 commit comments

Comments
 (0)