1affe4deeSMatthias Ringwald /*
2affe4deeSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
3affe4deeSMatthias Ringwald *
4affe4deeSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5affe4deeSMatthias Ringwald * modification, are permitted provided that the following conditions
6affe4deeSMatthias Ringwald * are met:
7affe4deeSMatthias Ringwald *
8affe4deeSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9affe4deeSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10affe4deeSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11affe4deeSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12affe4deeSMatthias Ringwald * documentation and/or other materials provided with the distribution.
13affe4deeSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14affe4deeSMatthias Ringwald * contributors may be used to endorse or promote products derived
15affe4deeSMatthias Ringwald * from this software without specific prior written permission.
16affe4deeSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17affe4deeSMatthias Ringwald * personal benefit and not for any commercial purpose or for
18affe4deeSMatthias Ringwald * monetary gain.
19affe4deeSMatthias Ringwald *
20affe4deeSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21affe4deeSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22affe4deeSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25affe4deeSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26affe4deeSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27affe4deeSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28affe4deeSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29affe4deeSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30affe4deeSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31affe4deeSMatthias Ringwald * SUCH DAMAGE.
32affe4deeSMatthias Ringwald *
33affe4deeSMatthias Ringwald * Please inquire about commercial licensing options at
34affe4deeSMatthias Ringwald * [email protected]
35affe4deeSMatthias Ringwald *
36affe4deeSMatthias Ringwald */
37affe4deeSMatthias Ringwald
38affe4deeSMatthias Ringwald #define BTSTACK_FILE__ "sx1280_ble_demo.c"
39affe4deeSMatthias Ringwald
40affe4deeSMatthias Ringwald // *****************************************************************************
41affe4deeSMatthias Ringwald /* EXAMPLE_START(sx1280_ble_demo): LE Peripheral - Control LED over GATT */
42affe4deeSMatthias Ringwald // *****************************************************************************
43affe4deeSMatthias Ringwald
44affe4deeSMatthias Ringwald #include <stdint.h>
45affe4deeSMatthias Ringwald #include <stdio.h>
46affe4deeSMatthias Ringwald #include <stdlib.h>
47affe4deeSMatthias Ringwald #include <string.h>
48affe4deeSMatthias Ringwald
49affe4deeSMatthias Ringwald #include "stm32l4xx_hal.h"
50affe4deeSMatthias Ringwald
51affe4deeSMatthias Ringwald #include "btstack.h"
52affe4deeSMatthias Ringwald #include "gatt_rgb.h"
53affe4deeSMatthias Ringwald
54affe4deeSMatthias Ringwald #define LED_SMALL_PORT GPIOB
55affe4deeSMatthias Ringwald #define LED_SMALL_PIN GPIO_PIN_8
56affe4deeSMatthias Ringwald
57affe4deeSMatthias Ringwald #define LED_R_PORT GPIOH
58affe4deeSMatthias Ringwald #define LED_R_PIN GPIO_PIN_0
59affe4deeSMatthias Ringwald #define LED_G_PORT GPIOH
60affe4deeSMatthias Ringwald #define LED_G_PIN GPIO_PIN_1
61affe4deeSMatthias Ringwald #define LED_B_PORT GPIOC
62affe4deeSMatthias Ringwald #define LED_B_PIN GPIO_PIN_1
63affe4deeSMatthias Ringwald
64affe4deeSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 2000
65affe4deeSMatthias Ringwald #define HEARTBEAT_BLINK_MS 200
66affe4deeSMatthias Ringwald
67affe4deeSMatthias Ringwald static btstack_timer_source_t heartbeat;
68affe4deeSMatthias Ringwald
69affe4deeSMatthias Ringwald static void heartbeat_handler_led_on(struct btstack_timer_source *ts);
70affe4deeSMatthias Ringwald static void heartbeat_handler_led_off(struct btstack_timer_source *ts);
71affe4deeSMatthias Ringwald
72affe4deeSMatthias Ringwald const uint8_t adv_data[] = {
73affe4deeSMatthias Ringwald // Flags general discoverable, BR/EDR not supported
74affe4deeSMatthias Ringwald 0x02, 0x01, 0x06,
75affe4deeSMatthias Ringwald // Service 16-bit: FFF0
76affe4deeSMatthias Ringwald 0x03, 0x02, 0xF0, 0xFF,
77affe4deeSMatthias Ringwald // Manufacturer specific data (aa bb - address 78:A5:04:82:1B:A4)
78affe4deeSMatthias Ringwald 0x09, 0xFF, 0x04, 0x1E, 0x78, 0xA5, 0x04, 0x82, 0x1A, 0xAA,
79affe4deeSMatthias Ringwald // Name
80affe4deeSMatthias Ringwald 0x08, 0x09, 'D', 'E', 'L', 'I', 'G', 'H', 'T',
81affe4deeSMatthias Ringwald };
82affe4deeSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
83affe4deeSMatthias Ringwald
heartbeat_handler_led_off(struct btstack_timer_source * ts)84affe4deeSMatthias Ringwald static void heartbeat_handler_led_off(struct btstack_timer_source *ts){
85affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_SET);
86affe4deeSMatthias Ringwald heartbeat.process = &heartbeat_handler_led_on;
87affe4deeSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS - HEARTBEAT_BLINK_MS);
88affe4deeSMatthias Ringwald btstack_run_loop_add_timer(ts);
89affe4deeSMatthias Ringwald }
90affe4deeSMatthias Ringwald
heartbeat_handler_led_on(struct btstack_timer_source * ts)91affe4deeSMatthias Ringwald static void heartbeat_handler_led_on(struct btstack_timer_source *ts){
92affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
93affe4deeSMatthias Ringwald printf("BLINK\n");
94affe4deeSMatthias Ringwald heartbeat.process = &heartbeat_handler_led_off;
95affe4deeSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_BLINK_MS);
96affe4deeSMatthias Ringwald btstack_run_loop_add_timer(ts);
97affe4deeSMatthias Ringwald }
98affe4deeSMatthias Ringwald
set_rgb(uint8_t r,uint8_t g,uint8_t b)99b4c22689SMatthias Ringwald static void set_rgb(uint8_t r, uint8_t g, uint8_t b){
100b4c22689SMatthias Ringwald printf("R: %u, G: %u, B: %u\n", r, g, b);
101b4c22689SMatthias Ringwald HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, r > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
102b4c22689SMatthias Ringwald HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, g > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
103b4c22689SMatthias Ringwald HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, b > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
104b4c22689SMatthias Ringwald }
105b4c22689SMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)106affe4deeSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
107affe4deeSMatthias Ringwald UNUSED(channel);
108affe4deeSMatthias Ringwald UNUSED(size);
109affe4deeSMatthias Ringwald
110affe4deeSMatthias Ringwald switch (packet_type) {
111affe4deeSMatthias Ringwald case HCI_EVENT_PACKET:
112affe4deeSMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
113affe4deeSMatthias Ringwald case HCI_EVENT_LE_META:
114affe4deeSMatthias Ringwald switch (hci_event_le_meta_get_subevent_code(packet)){
115affe4deeSMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
116affe4deeSMatthias Ringwald printf("Connected\n");
117affe4deeSMatthias Ringwald btstack_run_loop_remove_timer(&heartbeat);
118affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
119affe4deeSMatthias Ringwald break;
120affe4deeSMatthias Ringwald default:
121affe4deeSMatthias Ringwald break;
122affe4deeSMatthias Ringwald }
123affe4deeSMatthias Ringwald break;
124affe4deeSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE:
125affe4deeSMatthias Ringwald printf("Disconnected\n");
126affe4deeSMatthias Ringwald // restart heartbeat
127b4c22689SMatthias Ringwald set_rgb(0,0,0);
128affe4deeSMatthias Ringwald heartbeat_handler_led_on(&heartbeat);
129affe4deeSMatthias Ringwald break;
130affe4deeSMatthias Ringwald }
131affe4deeSMatthias Ringwald break;
132affe4deeSMatthias Ringwald }
133affe4deeSMatthias Ringwald }
134affe4deeSMatthias Ringwald
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)135affe4deeSMatthias Ringwald static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
136affe4deeSMatthias Ringwald UNUSED(connection_handle);
137affe4deeSMatthias Ringwald return 0;
138affe4deeSMatthias Ringwald }
139affe4deeSMatthias Ringwald
att_write_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)140affe4deeSMatthias Ringwald static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
141affe4deeSMatthias Ringwald UNUSED(transaction_mode);
142affe4deeSMatthias Ringwald UNUSED(offset);
143affe4deeSMatthias Ringwald UNUSED(buffer_size);
144affe4deeSMatthias Ringwald
145affe4deeSMatthias Ringwald if (att_handle == ATT_CHARACTERISTIC_FFF3_01_VALUE_HANDLE) {
146affe4deeSMatthias Ringwald set_rgb(buffer[4], buffer[5], buffer[6]);
147affe4deeSMatthias Ringwald }
148affe4deeSMatthias Ringwald return 0;
149affe4deeSMatthias Ringwald }
150affe4deeSMatthias Ringwald
151affe4deeSMatthias Ringwald int btstack_main(void);
btstack_main(void)152affe4deeSMatthias Ringwald int btstack_main(void)
153affe4deeSMatthias Ringwald {
154affe4deeSMatthias Ringwald l2cap_init();
155affe4deeSMatthias Ringwald
156affe4deeSMatthias Ringwald // setup le device db
157affe4deeSMatthias Ringwald le_device_db_init();
158affe4deeSMatthias Ringwald
159affe4deeSMatthias Ringwald // setup SM: Display only
160affe4deeSMatthias Ringwald sm_init();
161affe4deeSMatthias Ringwald
162affe4deeSMatthias Ringwald // setup ATT server
163affe4deeSMatthias Ringwald att_server_init(profile_data, att_read_callback, att_write_callback);
164affe4deeSMatthias Ringwald
165affe4deeSMatthias Ringwald // setup advertisements
166affe4deeSMatthias Ringwald uint16_t adv_int_min = 0x0030;
167affe4deeSMatthias Ringwald uint16_t adv_int_max = 0x0030;
168affe4deeSMatthias Ringwald uint8_t adv_type = 0;
169affe4deeSMatthias Ringwald bd_addr_t null_addr;
170affe4deeSMatthias Ringwald memset(null_addr, 0, 6);
171affe4deeSMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
172affe4deeSMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
173affe4deeSMatthias Ringwald gap_advertisements_enable(1);
174affe4deeSMatthias Ringwald
175affe4deeSMatthias Ringwald // register for ATT event
176affe4deeSMatthias Ringwald att_server_register_packet_handler(packet_handler);
177affe4deeSMatthias Ringwald
178affe4deeSMatthias Ringwald // set one-shot timer
179affe4deeSMatthias Ringwald heartbeat.process = &heartbeat_handler_led_on;
180affe4deeSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
181affe4deeSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
182affe4deeSMatthias Ringwald
183affe4deeSMatthias Ringwald // LEDs init
184affe4deeSMatthias Ringwald GPIO_InitTypeDef GPIO_InitStruct = {0};
185affe4deeSMatthias Ringwald
186affe4deeSMatthias Ringwald // enable GPIO for Port H
187affe4deeSMatthias Ringwald __HAL_RCC_GPIOH_CLK_ENABLE();
188affe4deeSMatthias Ringwald
189affe4deeSMatthias Ringwald GPIO_InitStruct.Pin = LED_SMALL_PIN;
190affe4deeSMatthias Ringwald GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
191affe4deeSMatthias Ringwald GPIO_InitStruct.Pull = GPIO_NOPULL;
192affe4deeSMatthias Ringwald GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
193affe4deeSMatthias Ringwald HAL_GPIO_Init(LED_SMALL_PORT, &GPIO_InitStruct);
194affe4deeSMatthias Ringwald
195affe4deeSMatthias Ringwald GPIO_InitStruct.Pin = LED_R_PIN;
196affe4deeSMatthias Ringwald GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
197affe4deeSMatthias Ringwald GPIO_InitStruct.Pull = GPIO_NOPULL;
198affe4deeSMatthias Ringwald GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
199affe4deeSMatthias Ringwald HAL_GPIO_Init(LED_R_PORT, &GPIO_InitStruct);
200affe4deeSMatthias Ringwald
201affe4deeSMatthias Ringwald GPIO_InitStruct.Pin = LED_G_PIN;
202affe4deeSMatthias Ringwald GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
203affe4deeSMatthias Ringwald GPIO_InitStruct.Pull = GPIO_NOPULL;
204affe4deeSMatthias Ringwald GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
205affe4deeSMatthias Ringwald HAL_GPIO_Init(LED_G_PORT, &GPIO_InitStruct);
206affe4deeSMatthias Ringwald
207affe4deeSMatthias Ringwald GPIO_InitStruct.Pin = LED_B_PIN;
208affe4deeSMatthias Ringwald GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
209affe4deeSMatthias Ringwald GPIO_InitStruct.Pull = GPIO_NOPULL;
210affe4deeSMatthias Ringwald GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
211affe4deeSMatthias Ringwald HAL_GPIO_Init(LED_B_PORT, &GPIO_InitStruct);
212affe4deeSMatthias Ringwald
213affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
214affe4deeSMatthias Ringwald
215affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, GPIO_PIN_SET);
216affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, GPIO_PIN_SET);
217affe4deeSMatthias Ringwald HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, GPIO_PIN_SET);
218affe4deeSMatthias Ringwald
219affe4deeSMatthias Ringwald
220affe4deeSMatthias Ringwald // turn on!
221affe4deeSMatthias Ringwald hci_power_control(HCI_POWER_ON);
222affe4deeSMatthias Ringwald
223affe4deeSMatthias Ringwald return 0;
224affe4deeSMatthias Ringwald }
225affe4deeSMatthias Ringwald /* EXAMPLE_END */
226