diff --git a/example/gap_le_advertisements.c b/example/gap_le_advertisements.c index ad15f802a..eeee0bded 100644 --- a/example/gap_le_advertisements.c +++ b/example/gap_le_advertisements.c @@ -248,11 +248,11 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe bd_addr_t address; reverse_bd_addr(&packet[pos], address); pos += 6; - uint8_t rssi = packet[pos++]; + int8_t rssi = (int8_t) packet[pos++]; uint8_t length = packet[pos++]; uint8_t * data = &packet[pos]; - printf("Advertisement event: evt-type %u, addr-type %u, addr %s, rssi %u, data[%u] ", event_type, + printf("Advertisement event: evt-type %u, addr-type %u, addr %s, rssi %d, data[%u] ", event_type, address_type, bd_addr_to_str(address), rssi, length); printf_hexdump(data, length); dump_advertisement_data(data, length); diff --git a/port/nrf5x/main.c b/port/nrf5x/main.c index b1b277a22..978b893d0 100755 --- a/port/nrf5x/main.c +++ b/port/nrf5x/main.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "app_uart.h" #include "app_error.h" @@ -57,7 +58,8 @@ static uint8_t rx_adv_buffer[2 + MAXLEN]; static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); static hci_transport_t hci_transport; static uint8_t hci_outgoing_event[258]; -static uint8_t hci_outgoing_event_ready; +static volatile uint8_t hci_outgoing_event_ready; +static volatile uint8_t hci_outgoing_event_free; static btstack_data_source_t hci_transport_data_source; // Link Layer State @@ -193,7 +195,12 @@ static void radio_init(void){ // Shorts: // - READY->START - NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos; + // - ADDRESS0>RSSISTART + NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos + | RADIO_SHORTS_ADDRESS_RSSISTART_Enabled << RADIO_SHORTS_ADDRESS_RSSISTART_Pos; + + // Disable all interrrupts + NRF_RADIO->INTENCLR = 0xffffffff; } @@ -226,6 +233,9 @@ void radio_receive_on_channel(int channel){ // ramp up receiver NRF_RADIO->TASKS_RXEN = 1; + + // enable IRQ for END event + NRF_RADIO->INTENSET = RADIO_INTENSET_END_Enabled << RADIO_INTENSET_END_Pos; } // static @@ -251,7 +261,34 @@ void radio_dump_packet(void){ } void RADIO_IRQHandler(void){ - // packet received? + + // IRQ only triggered on EVENTS_END so far + NRF_RADIO->EVENTS_END = 0; + + if (ll_state == LL_STATE_SCANNING){ + + // check if outgoing buffer available and if CRC was ok + if (hci_outgoing_event_free && + ((NRF_RADIO->CRCSTATUS & RADIO_CRCSTATUS_CRCSTATUS_Msk) == (RADIO_CRCSTATUS_CRCSTATUS_CRCOk << RADIO_CRCSTATUS_CRCSTATUS_Pos))){ + hci_outgoing_event_free = 0; + int len = rx_adv_buffer[1] & 0x3f; + hci_outgoing_event[0] = HCI_EVENT_LE_META; + hci_outgoing_event[1] = 11 + len - 6; + hci_outgoing_event[2] = HCI_SUBEVENT_LE_ADVERTISING_REPORT; + hci_outgoing_event[3] = 1; + hci_outgoing_event[4] = rx_adv_buffer[0] & 0x0f; + hci_outgoing_event[5] = (rx_adv_buffer[0] & 0x40) ? 1 : 0; + memcpy(&hci_outgoing_event[6], &rx_adv_buffer[2], 6); + hci_outgoing_event[12] = len - 6; // rest after bd addr + memcpy(&hci_outgoing_event[13], &rx_adv_buffer[8], len - 6); + hci_outgoing_event[13 + len - 6] = -NRF_RADIO->RSSISAMPLE; // RSSI is stored without sign but is negative + hci_outgoing_event_ready = 1; + } else { + // ... for now, we just throw the adv away and try to receive the next one + } + } + // restart receiving + NRF_RADIO->TASKS_START = 1; } static uint8_t random_generator_next(void){ @@ -331,33 +368,12 @@ uint8_t ll_set_scan_enable(uint8_t le_scan_enable, uint8_t filter_duplicates){ } } - static int transport_run(btstack_data_source_t * ds){ - - // ad-hoc way to trigger stuff + // deliver hci packet on main thread if (hci_outgoing_event_ready){ hci_outgoing_event_ready = 0; packet_handler(HCI_EVENT_PACKET, hci_outgoing_event, hci_outgoing_event[1]+2); - return 0; - } - - if (ll_state == LL_STATE_SCANNING && NRF_RADIO->EVENTS_END){ - // adv received - int len = rx_adv_buffer[1] & 0x3f; - hci_outgoing_event[0] = HCI_EVENT_LE_META; - hci_outgoing_event[1] = 11 + len - 6; - hci_outgoing_event[2] = HCI_SUBEVENT_LE_ADVERTISING_REPORT; - hci_outgoing_event[3] = 1; - hci_outgoing_event[4] = rx_adv_buffer[0] & 0x0f; - hci_outgoing_event[5] = (rx_adv_buffer[0] & 0x40) ? 1 : 0; - memcpy(&hci_outgoing_event[6], &rx_adv_buffer[2], 6); - hci_outgoing_event[12] = len - 6; // rest after bd addr - memcpy(&hci_outgoing_event[13], &rx_adv_buffer[8], len - 6); - hci_outgoing_event[13 + len - 6] = 0; // TODO: measure RSSI and set here - hci_outgoing_event_ready = 1; - packet_handler(HCI_EVENT_PACKET, hci_outgoing_event, hci_outgoing_event[1]+2); - // restart receiving - NRF_RADIO->TASKS_START = 1; + hci_outgoing_event_free = 1; return 0; } return 0; @@ -472,6 +488,12 @@ int main(void) init_timer(); radio_init(); + hci_outgoing_event_free = 1; + + // enable Radio IRQs + NVIC_SetPriority( RADIO_IRQn, 0 ); + NVIC_ClearPendingIRQ( RADIO_IRQn ); + NVIC_EnableIRQ( RADIO_IRQn ); // Bring up BTstack @@ -505,10 +527,6 @@ int main(void) while (1){}; #if 0 - // enable Radio IRQs - // NVIC_SetPriority( RADIO_IRQn, 0 ); - // NVIC_ClearPendingIRQ( RADIO_IRQn ); - // NVIC_EnableIRQ( RADIO_IRQn ); // start listening radio_receive_on_channel(37); diff --git a/port/nrf5x/pca10028/armgcc/Makefile b/port/nrf5x/pca10028/armgcc/Makefile index e65a797d9..64d8d81b6 100755 --- a/port/nrf5x/pca10028/armgcc/Makefile +++ b/port/nrf5x/pca10028/armgcc/Makefile @@ -71,7 +71,7 @@ INC_PATHS += -I$(abspath ../../../../../components/toolchain/gcc) # BTstack territory BTSTACK_ROOT = ../../../../../components/btstack C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/port/nrf5x/main.c) -C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/port/retarget.c) +C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/port/retarget_blocking.c) C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/platform/embedded/btstack_run_loop_embedded.c) C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/src/ble/ad_parser.c) C_SOURCE_FILES += $(abspath $(BTSTACK_ROOT)/src/btstack_linked_list.c) diff --git a/port/nrf5x/retarget.c b/port/nrf5x/retarget_blocking.c similarity index 77% rename from port/nrf5x/retarget.c rename to port/nrf5x/retarget_blocking.c index 91462b94a..c77def4d5 100755 --- a/port/nrf5x/retarget.c +++ b/port/nrf5x/retarget_blocking.c @@ -10,6 +10,8 @@ * */ +// BTstack patch: block on full outgoing buffer + #if !defined(NRF_LOG_USES_RTT) || NRF_LOG_USES_RTT != 1 #include @@ -45,7 +47,11 @@ int fputc(int ch, FILE * p_file) { UNUSED_PARAMETER(p_file); - UNUSED_VARIABLE(app_uart_put((uint8_t)ch)); + int res; + do { + res = app_uart_put((uint8_t)ch); + } while (res); + return ch; } @@ -60,7 +66,11 @@ int _write(int file, const char * p_char, int len) for (i = 0; i < len; i++) { - UNUSED_VARIABLE(app_uart_put(*p_char++)); + int res; + do { + res = app_uart_put((uint8_t)*p_char); + } while (res); + p_char++; } return len; @@ -89,7 +99,11 @@ __ATTRIBUTES size_t __write(int file, const unsigned char * p_char, size_t len) for (i = 0; i < len; i++) { - UNUSED_VARIABLE(app_uart_put(*p_char++)); + int res; + do { + res = app_uart_put((uint8_t)*p_char); + } while (res); + p_char++; } return len; diff --git a/src/ble/ad_parser.c b/src/ble/ad_parser.c index 1a3abfc71..eb74ad918 100644 --- a/src/ble/ad_parser.c +++ b/src/ble/ad_parser.c @@ -72,8 +72,13 @@ int ad_iterator_has_more(ad_context_t * context){ } void ad_iterator_next(ad_context_t * context){ - uint8_t chunk_len = context->data[context->offset]; - context->offset += 1 + chunk_len; + int chunk_len = context->data[context->offset]; + int new_offset = context->offset + 1 + chunk_len; + // avoid uint8_t overrun + if (new_offset > 0xff){ + new_offset = 0xff; + } + context->offset = new_offset; } uint8_t ad_iterator_get_data_len(ad_context_t * context){