2016-11-16 15:38:25 -05:00
|
|
|
/*
|
2017-08-04 12:05:38 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
2016-11-16 15:38:25 -05:00
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
2018-07-08 15:13:05 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-07-08 15:13:05 -04:00
|
|
|
#include <stdio.h>
|
2018-07-17 05:44:55 -04:00
|
|
|
#include <string.h>
|
2016-11-16 15:38:25 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "ble.h"
|
2018-07-08 15:13:05 -04:00
|
|
|
#include "ble_drv.h"
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "nrfx_power.h"
|
|
|
|
#include "nrf_nvic.h"
|
2018-07-19 17:51:34 -04:00
|
|
|
#include "nrf_sdm.h"
|
2019-07-31 00:30:24 -04:00
|
|
|
#include "py/objstr.h"
|
2018-11-20 23:04:58 -05:00
|
|
|
#include "py/runtime.h"
|
2019-01-30 02:13:07 -05:00
|
|
|
#include "supervisor/usb.h"
|
2019-07-31 00:30:24 -04:00
|
|
|
#include "shared-bindings/bleio/Adapter.h"
|
|
|
|
#include "shared-bindings/bleio/Address.h"
|
2019-01-30 02:13:07 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
|
2018-11-20 23:04:58 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_AssertionError,
|
|
|
|
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc);
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC uint32_t ble_stack_enable(void) {
|
|
|
|
nrf_clock_lf_cfg_t clock_config = {
|
2019-08-22 01:04:00 -04:00
|
|
|
#if BOARD_HAS_32KHZ_XTAL
|
|
|
|
.source = NRF_CLOCK_LF_SRC_XTAL,
|
|
|
|
.rc_ctiv = 0,
|
|
|
|
.rc_temp_ctiv = 0,
|
|
|
|
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
|
|
|
|
#else
|
|
|
|
.source = NRF_CLOCK_LF_SRC_RC,
|
|
|
|
.rc_ctiv = 16,
|
|
|
|
.rc_temp_ctiv = 2,
|
|
|
|
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM
|
|
|
|
#endif
|
2018-07-22 10:06:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
|
|
|
|
if (err_code != NRF_SUCCESS)
|
|
|
|
return err_code;
|
|
|
|
|
|
|
|
err_code = sd_nvic_EnableIRQ(SD_EVT_IRQn);
|
|
|
|
if (err_code != NRF_SUCCESS)
|
|
|
|
return err_code;
|
|
|
|
|
2018-12-28 22:55:29 -05:00
|
|
|
// Start with no event handlers, etc.
|
|
|
|
ble_drv_reset();
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2018-12-28 22:55:29 -05:00
|
|
|
uint32_t app_ram_start;
|
2018-07-22 10:06:19 -04:00
|
|
|
app_ram_start = 0x20004000;
|
|
|
|
|
|
|
|
ble_cfg_t ble_conf;
|
|
|
|
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
|
|
|
|
ble_conf.conn_cfg.params.gap_conn_cfg.conn_count = BLE_GAP_CONN_COUNT_DEFAULT;
|
|
|
|
ble_conf.conn_cfg.params.gap_conn_cfg.event_length = BLE_GAP_EVENT_LENGTH_DEFAULT;
|
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_conf, app_ram_start);
|
|
|
|
if (err_code != NRF_SUCCESS)
|
|
|
|
return err_code;
|
|
|
|
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
|
|
|
ble_conf.gap_cfg.role_count_cfg.periph_role_count = 1;
|
|
|
|
ble_conf.gap_cfg.role_count_cfg.central_role_count = 1;
|
|
|
|
err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_conf, app_ram_start);
|
|
|
|
if (err_code != NRF_SUCCESS)
|
|
|
|
return err_code;
|
|
|
|
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
|
|
|
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
|
|
|
|
ble_conf.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = MAX_TX_IN_PROGRESS;
|
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_conf, app_ram_start);
|
|
|
|
if (err_code != NRF_SUCCESS)
|
|
|
|
return err_code;
|
|
|
|
|
|
|
|
err_code = sd_ble_enable(&app_ram_start);
|
|
|
|
|
|
|
|
return err_code;
|
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2018-07-08 15:13:05 -04:00
|
|
|
void common_hal_bleio_adapter_set_enabled(bool enabled) {
|
2018-07-22 10:06:19 -04:00
|
|
|
const bool is_enabled = common_hal_bleio_adapter_get_enabled();
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
// Don't enable or disable twice
|
|
|
|
if ((is_enabled && enabled) || (!is_enabled && !enabled)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t err_code;
|
2018-07-19 17:51:34 -04:00
|
|
|
if (enabled) {
|
2019-01-30 02:13:07 -05:00
|
|
|
// The SD takes over the POWER module and will fail if the module is already in use.
|
|
|
|
// Occurs when USB is initialized previously
|
|
|
|
nrfx_power_uninit();
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
err_code = ble_stack_enable();
|
2019-01-30 02:13:07 -05:00
|
|
|
|
|
|
|
// Re-init USB hardware
|
|
|
|
init_usb_hardware();
|
2018-07-08 15:13:05 -04:00
|
|
|
} else {
|
2018-07-19 17:51:34 -04:00
|
|
|
err_code = sd_softdevice_disable();
|
2019-01-30 02:13:07 -05:00
|
|
|
|
|
|
|
// Re-init USB hardware
|
|
|
|
init_usb_hardware();
|
2018-07-19 17:51:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-11-20 23:04:58 -05:00
|
|
|
mp_raise_OSError_msg(translate("Failed to change softdevice state"));
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2018-07-08 15:13:05 -04:00
|
|
|
bool common_hal_bleio_adapter_get_enabled(void) {
|
2018-07-22 10:06:19 -04:00
|
|
|
uint8_t is_enabled;
|
|
|
|
|
|
|
|
const uint32_t err_code = sd_softdevice_is_enabled(&is_enabled);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-11-20 23:04:58 -05:00
|
|
|
mp_raise_OSError_msg(translate("Failed to get softdevice state"));
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return is_enabled;
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 00:30:24 -04:00
|
|
|
void get_address(ble_gap_addr_t *address) {
|
2018-07-22 10:06:19 -04:00
|
|
|
uint32_t err_code;
|
|
|
|
|
|
|
|
common_hal_bleio_adapter_set_enabled(true);
|
2019-07-31 00:30:24 -04:00
|
|
|
err_code = sd_ble_gap_addr_get(address);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-11-20 23:04:58 -05:00
|
|
|
mp_raise_OSError_msg(translate("Failed to get local address"));
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2019-07-31 00:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bleio_address_obj_t *common_hal_bleio_adapter_get_address(void) {
|
|
|
|
common_hal_bleio_adapter_set_enabled(true);
|
|
|
|
|
|
|
|
ble_gap_addr_t local_address;
|
|
|
|
get_address(&local_address);
|
|
|
|
|
|
|
|
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
|
|
|
|
address->base.type = &bleio_address_type;
|
|
|
|
|
|
|
|
common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type);
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_bleio_adapter_get_default_name(void) {
|
|
|
|
common_hal_bleio_adapter_set_enabled(true);
|
|
|
|
|
|
|
|
ble_gap_addr_t local_address;
|
|
|
|
get_address(&local_address);
|
|
|
|
|
|
|
|
char name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0 };
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-07-31 00:30:24 -04:00
|
|
|
name[sizeof(name) - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf];
|
|
|
|
name[sizeof(name) - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf];
|
|
|
|
name[sizeof(name) - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf];
|
|
|
|
name[sizeof(name) - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf];
|
2019-06-13 21:55:07 -04:00
|
|
|
|
2019-07-31 00:30:24 -04:00
|
|
|
return mp_obj_new_str(name, sizeof(name));
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|