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