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 ) 2019 Dan Halbert for Adafruit Industries
2018-07-19 14:33:24 -04:00
* Copyright ( c ) 2018 Artur Pacholec
2019-06-19 10:42:36 -04:00
* Copyright ( c ) 2016 Glenn Ruben Bakke
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 .
*/
2017-02-12 08:24:15 -05:00
# include <stdbool.h>
2018-07-22 10:06:19 -04:00
# include <stdio.h>
2017-02-09 17:56:32 -05:00
2018-07-22 10:06:19 -04:00
# include "ble.h"
2017-02-17 13:10:24 -05:00
# include "ble_drv.h"
2018-07-19 17:51:34 -04:00
# include "nrf_nvic.h"
2016-11-16 15:38:25 -05:00
# include "nrf_sdm.h"
2019-01-29 08:47:01 -05:00
# include "nrf_soc.h"
# include "nrfx_power.h"
2018-07-22 10:06:19 -04:00
# include "py/misc.h"
2019-02-21 00:19:31 -05:00
# include "py/mpstate.h"
2017-05-14 11:14:57 -04:00
2021-07-08 22:11:05 -04:00
# if CIRCUITPY_SERIAL_BLE && CIRCUITPY_VERBOSE_BLE
# include "supervisor/shared/bluetooth/serial.h"
# endif
2018-07-19 17:51:34 -04:00
nrf_nvic_state_t nrf_nvic_state = { 0 } ;
2017-02-12 08:24:15 -05:00
2019-03-18 09:11:40 -04:00
// Flag indicating progress of internal flash operation.
2019-12-12 00:19:03 -05:00
volatile sd_flash_operation_status_t sd_flash_operation_status ;
2019-03-18 09:11:40 -04:00
2018-07-22 10:06:19 -04:00
__attribute__ ( ( aligned ( 4 ) ) )
2019-12-06 15:27:46 -05:00
static uint8_t m_ble_evt_buf [ sizeof ( ble_evt_t ) + ( BLE_GATTS_VAR_ATTR_LEN_MAX ) ] ;
2017-02-17 15:47:38 -05:00
2018-12-28 22:55:29 -05:00
void ble_drv_reset ( ) {
2018-12-31 09:02:43 -05:00
// Linked list items will be gc'd.
2019-02-21 00:19:31 -05:00
MP_STATE_VM ( ble_drv_evt_handler_entries ) = NULL ;
2019-03-18 09:11:40 -04:00
sd_flash_operation_status = SD_FLASH_OPERATION_DONE ;
2018-12-28 22:55:29 -05:00
}
2017-02-17 15:47:38 -05:00
2021-03-15 09:57:36 -04:00
void ble_drv_add_event_handler_entry ( ble_drv_evt_handler_entry_t * entry , ble_drv_evt_handler_t func , void * param ) {
2021-05-19 19:22:07 -04:00
ble_drv_evt_handler_entry_t * it = MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
while ( it ! = NULL ) {
// If event handler and its corresponding param are already on the list, don't add again.
if ( ( it - > func = = func ) & & ( it - > param = = param ) ) {
return ;
}
it = it - > next ;
}
2019-09-14 15:40:24 -04:00
entry - > next = MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
entry - > param = param ;
entry - > func = func ;
MP_STATE_VM ( ble_drv_evt_handler_entries ) = entry ;
}
2018-07-22 10:06:19 -04:00
void ble_drv_add_event_handler ( ble_drv_evt_handler_t func , void * param ) {
2019-02-21 00:19:31 -05:00
ble_drv_evt_handler_entry_t * it = MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
2019-01-03 21:42:42 -05:00
while ( it ! = NULL ) {
// If event handler and its corresponding param are already on the list, don't add again.
2018-07-22 10:06:19 -04:00
if ( ( it - > func = = func ) & & ( it - > param = = param ) ) {
return ;
2018-07-19 14:33:24 -04:00
}
2018-07-22 10:06:19 -04:00
it = it - > next ;
2017-03-30 16:41:19 -04:00
}
2017-03-28 17:37:19 -04:00
2019-01-03 21:42:42 -05:00
// Add a new handler to the front of the list
2019-02-21 00:19:31 -05:00
ble_drv_evt_handler_entry_t * handler = m_new_ll ( ble_drv_evt_handler_entry_t , 1 ) ;
2019-09-14 15:40:24 -04:00
ble_drv_add_event_handler_entry ( handler , func , param ) ;
2017-03-26 16:59:30 -04:00
}
2019-01-07 22:46:20 -05:00
void ble_drv_remove_event_handler ( ble_drv_evt_handler_t func , void * param ) {
2019-02-21 00:19:31 -05:00
ble_drv_evt_handler_entry_t * it = MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
ble_drv_evt_handler_entry_t * * prev = & MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
2019-01-07 22:46:20 -05:00
while ( it ! = NULL ) {
if ( ( it - > func = = func ) & & ( it - > param = = param ) ) {
// Splice out the matching handler.
* prev = it - > next ;
2021-05-19 19:22:07 -04:00
// Clear next of the removed node so it's clearly not in a list.
it - > next = NULL ;
2019-01-07 22:46:20 -05:00
return ;
}
prev = & ( it - > next ) ;
it = it - > next ;
}
}
2021-03-15 09:57:36 -04:00
extern void tusb_hal_nrf_power_event ( uint32_t event ) ;
2019-02-15 18:55:10 -05:00
2018-07-22 10:06:19 -04:00
void SD_EVT_IRQHandler ( void ) {
uint32_t evt_id ;
while ( sd_evt_get ( & evt_id ) ! = NRF_ERROR_NOT_FOUND ) {
2019-01-29 08:47:01 -05:00
switch ( evt_id ) {
2021-04-16 10:59:11 -04:00
# if CIRCUITPY_USB
2019-01-29 08:47:01 -05:00
// usb power event
2021-03-15 09:57:36 -04:00
case NRF_EVT_POWER_USB_DETECTED :
case NRF_EVT_POWER_USB_POWER_READY :
case NRF_EVT_POWER_USB_REMOVED : {
int32_t usbevt = ( evt_id = = NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED :
( evt_id = = NRF_EVT_POWER_USB_POWER_READY ) ? NRFX_POWER_USB_EVT_READY :
( evt_id = = NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : - 1 ;
tusb_hal_nrf_power_event ( usbevt ) ;
}
2019-01-29 08:47:01 -05:00
break ;
2021-04-16 10:59:11 -04:00
# endif
2019-01-29 08:47:01 -05:00
2019-03-18 09:11:40 -04:00
// Set flag indicating that a flash operation has finished.
2021-03-15 09:57:36 -04:00
case NRF_EVT_FLASH_OPERATION_SUCCESS :
sd_flash_operation_status = SD_FLASH_OPERATION_DONE ;
break ;
case NRF_EVT_FLASH_OPERATION_ERROR :
sd_flash_operation_status = SD_FLASH_OPERATION_ERROR ;
break ;
default :
break ;
2019-01-29 08:47:01 -05:00
}
2018-07-19 16:59:18 -04:00
}
2021-07-08 22:11:05 -04:00
# if CIRCUITPY_SERIAL_BLE && CIRCUITPY_VERBOSE_BLE
ble_serial_disable ( ) ;
# endif
2018-07-22 10:06:19 -04:00
while ( 1 ) {
uint16_t evt_len = sizeof ( m_ble_evt_buf ) ;
const uint32_t err_code = sd_ble_evt_get ( m_ble_evt_buf , & evt_len ) ;
if ( err_code ! = NRF_SUCCESS ) {
if ( err_code = = NRF_ERROR_DATA_SIZE ) {
printf ( " NRF_ERROR_DATA_SIZE \n " ) ;
}
2017-03-30 17:18:49 -04:00
break ;
2018-07-22 10:06:19 -04:00
}
2017-03-12 15:18:37 -04:00
2021-03-15 09:57:36 -04:00
ble_evt_t * event = ( ble_evt_t * ) m_ble_evt_buf ;
2019-11-22 19:33:48 -05:00
# if CIRCUITPY_VERBOSE_BLE
2021-05-19 19:22:07 -04:00
size_t eid = event - > header . evt_id ;
if ( eid ! = 0x1d ) {
if ( BLE_GAP_EVT_BASE < = eid & & eid < = BLE_GAP_EVT_LAST ) {
mp_printf ( & mp_plat_print , " BLE GAP event: %d \n " , eid - BLE_GAP_EVT_BASE ) ;
} else {
mp_printf ( & mp_plat_print , " BLE event: 0x%04x \n " , event - > header . evt_id ) ;
}
2019-09-14 15:40:24 -04:00
}
2021-05-19 19:22:07 -04:00
# endif
2019-09-14 15:40:24 -04:00
2019-02-21 00:19:31 -05:00
ble_drv_evt_handler_entry_t * it = MP_STATE_VM ( ble_drv_evt_handler_entries ) ;
2019-09-14 15:40:24 -04:00
bool done = false ;
2018-07-22 10:06:19 -04:00
while ( it ! = NULL ) {
2020-01-30 09:52:06 -05:00
# if CIRCUITPY_VERBOSE_BLE
2021-05-19 19:22:07 -04:00
// mp_printf(&mp_plat_print, " calling handler: 0x%08lx, param: 0x%08lx\n", it->func - 1, it->param);
2020-01-30 09:52:06 -05:00
# endif
2021-05-19 19:22:07 -04:00
// Capture next before calling the function in case it removes itself from the list.
ble_drv_evt_handler_entry_t * next = it - > next ;
2019-09-14 15:40:24 -04:00
done = it - > func ( event , it - > param ) | | done ;
2021-05-19 19:22:07 -04:00
it = next ;
2018-07-22 10:06:19 -04:00
}
2019-11-22 19:33:48 -05:00
# if CIRCUITPY_VERBOSE_BLE
if ( event - > header . evt_id = = BLE_GATTS_EVT_WRITE ) {
2021-03-15 09:57:36 -04:00
ble_gatts_evt_write_t * write_evt = & event - > evt . gatts_evt . params . write ;
2019-11-22 19:33:48 -05:00
mp_printf ( & mp_plat_print , " Write to: UUID(0x%04x) handle %x of length %d auth %x \n " , write_evt - > uuid . uuid , write_evt - > handle , write_evt - > len , write_evt - > auth_required ) ;
}
# endif
2017-02-12 18:18:47 -05:00
}
2021-07-08 22:11:05 -04:00
# if CIRCUITPY_SERIAL_BLE && CIRCUITPY_VERBOSE_BLE
ble_serial_enable ( ) ;
# endif
2017-02-12 18:18:47 -05:00
}