/** * Add timer to run_loop (keep list sorted) */ static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ #ifdef TIMER_SUPPORT #ifdef HAVE_EMBEDDED_TICK uint32_t now = system_ticks; #endif #ifdef HAVE_EMBEDDED_TIME_MS uint32_t now = hal_time_ms(); #endif uint32_t new_low = ts->timeout; int new_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, new_low); btstack_linked_item_t *it; for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ // don't add timer that's already in there if ((btstack_timer_source_t *) it->next == ts){ log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); return; } uint32_t next_low = ((btstack_timer_source_t *) it->next)->timeout; int next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low); if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){ break; } } ts->item.next = it->next; it->next = (btstack_linked_item_t *) ts; #endif }
/** * Execute run_loop once */ void embedded_execute_once(void) { data_source_t *ds; // process data sources data_source_t *next; for (ds = (data_source_t *) data_sources; ds != NULL ; ds = next){ next = (data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself ds->process(ds); } #ifdef HAVE_TICK uint32_t now = system_ticks; #endif #ifdef HAVE_TIME_MS uint32_t now = hal_time_ms(); #endif #ifdef TIMER_SUPPORT // process timers while (timers) { timer_source_t *ts = (timer_source_t *) timers; if (ts->timeout > now) break; run_loop_remove_timer(ts); ts->process(ts); } #endif // disable IRQs and check if run loop iteration has been requested. if not, go to sleep hal_cpu_disable_irqs(); if (trigger_event_received){ trigger_event_received = 0; hal_cpu_enable_irqs(); } else { hal_cpu_enable_irqs_and_sleep(); } }
static uint32_t btstack_run_loop_embedded_get_time_ms(void){ #ifdef HAVE_EMBEDDED_TIME_MS return hal_time_ms(); #endif #ifdef HAVE_EMBEDDED_TICK return system_ticks * hal_tick_get_tick_period_in_ms(); #endif return 0; }
static uint32_t embedded_get_time_ms(void){ #ifdef HAVE_TIME_MS return hal_time_ms(); #endif #ifdef HAVE_TICK return system_ticks * hal_tick_get_tick_period_in_ms(); #endif return 0; }
// set timer static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ #ifdef HAVE_EMBEDDED_TICK uint32_t ticks = btstack_run_loop_embedded_ticks_for_ms(timeout_in_ms); if (ticks == 0) ticks++; // time until next tick is < hal_tick_get_tick_period_in_ms() and we don't know, so we add one ts->timeout = system_ticks + 1 + ticks; #endif #ifdef HAVE_EMBEDDED_TIME_MS ts->timeout = hal_time_ms() + timeout_in_ms + 1; #endif }
/** * Execute run_loop once */ void btstack_run_loop_embedded_execute_once(void) { btstack_data_source_t *ds; // process data sources btstack_data_source_t *next; for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ ds->process(ds, DATA_SOURCE_CALLBACK_POLL); } } #ifdef TIMER_SUPPORT #ifdef HAVE_EMBEDDED_TICK uint32_t now = system_ticks; #endif #ifdef HAVE_EMBEDDED_TIME_MS uint32_t now = hal_time_ms(); #endif // process timers while (timers) { btstack_timer_source_t *ts = (btstack_timer_source_t *) timers; uint32_t timeout_low = ts->timeout; int timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low); if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break; btstack_run_loop_remove_timer(ts); ts->process(ts); } #endif // disable IRQs and check if run loop iteration has been requested. if not, go to sleep hal_cpu_disable_irqs(); if (trigger_event_received){ trigger_event_received = 0; hal_cpu_enable_irqs(); } else { hal_cpu_enable_irqs_and_sleep(); } }
int main(void) { int sock_srv, sock_raw, nbytes, nread, msg_count, inc, err; size_t size, count; err = hal_comm_init("NRF0"); if (err < 0) printf("erro init radio\n"); sock_srv = hal_comm_socket(HAL_COMM_PF_NRF24, HAL_COMM_PROTO_RAW); if (sock_srv < 0) printf("erro open sockt mgmt"); printf("NRF24L01 loaded\n"); for (size = 0, inc = MESSAGE_SIZE; size < sizeof(msg);) { if ((inc+size) > sizeof(msg)) inc = sizeof(msg) - size; memcpy(msg+size, MESSAGE, inc); size += inc; } for (;;) { if (!connected) { hal_comm_listen(sock_srv); while (sock_raw <= 0) sock_raw = hal_comm_accept(sock_srv, &mac); printf("connected sock_raw=%d\n", sock_raw); count = 1; msg_count = 0; start = hal_time_ms() + 1000; connected = true; } if (hal_timeout(hal_time_ms(), start, 1000) > 0) { memcpy(buffer, msg, count); nbytes = hal_comm_write(sock_raw, buffer, count); if (nbytes > 0) { inc = (count == size) ? -1 : (count == 1 ? 1 : inc); count += inc; buffer[nbytes] = '\0'; printf("TX:[%03d]: %d-'%s'\n", nbytes, ++msg_count, buffer); } start = hal_time_ms(); } nread = hal_comm_read(sock_raw, buffer, sizeof(buffer)); if (nread > 0) { buffer[nread] = '\0'; printf("RX:[%03d]: '%s'\n", nread, buffer); } nread = hal_comm_read(sock_srv, buffer, sizeof(buffer)); if (nread > 0) { struct mgmt_nrf24_header *evt = (struct mgmt_nrf24_header *) buffer; struct mgmt_evt_nrf24_disconnected *evt_discon = (struct mgmt_evt_nrf24_disconnected *) evt->payload; switch (evt->opcode) { case MGMT_EVT_NRF24_DISCONNECTED: printf("evt->disconnect %llX\n", (long long) evt_discon->mac.address.uint64); hal_comm_close(sock_raw); sock_raw = -1; connected = false; break; default: break; } } } }