void mp_hal_delay_ms(mp_uint_t ms) { if (ms <= 0) { return; } // Wraparound of tick is taken care of by 2's complement arithmetic uint64_t start = system_timer_current_time(); while (system_timer_current_time() - start < (uint64_t)ms) { // Check for any pending events, like a KeyboardInterrupt mp_handle_pending(); // Enter sleep mode, waiting for (at least) the SysTick interrupt __WFI(); } }
/** * Updates the temperature sample of this instance of MicroBitThermometer * only if isSampleNeeded() indicates that an update is required. * * This call also will add the thermometer to fiber components to receive * periodic callbacks. * * @return MICROBIT_OK on success. */ int MicroBitThermometer::updateSample() { if(!(status & MICROBIT_THERMOMETER_ADDED_TO_IDLE)) { // If we're running under a fiber scheduer, register ourselves for a periodic callback to keep our data up to date. // Otherwise, we do just do this on demand, when polled through our read() interface. fiber_add_idle_component(this); status |= MICROBIT_THERMOMETER_ADDED_TO_IDLE; } // check if we need to update our sample... if(isSampleNeeded()) { int32_t processorTemperature; uint8_t sd_enabled; // For now, we just rely on the nrf senesor to be the most accurate. // The compass module also has a temperature sensor, and has the lowest power consumption, so will run the cooler... // ...however it isn't trimmed for accuracy during manufacture, so requires calibration. sd_softdevice_is_enabled(&sd_enabled); if (sd_enabled) { // If Bluetooth is enabled, we need to go through the Nordic software to safely do this sd_temp_get(&processorTemperature); } else { // Othwerwise, we access the information directly... uint32_t *TEMP = (uint32_t *)0x4000C508; NRF_TEMP->TASKS_START = 1; while (NRF_TEMP->EVENTS_DATARDY == 0); NRF_TEMP->EVENTS_DATARDY = 0; processorTemperature = *TEMP; NRF_TEMP->TASKS_STOP = 1; } // Record our reading... temperature = processorTemperature / 4; // Schedule our next sample. sampleTime = system_timer_current_time() + samplePeriod; // Send an event to indicate that we'e updated our temperature. MicroBitEvent e(id, MICROBIT_THERMOMETER_EVT_UPDATE); } return MICROBIT_OK; };
//% help=input/running-time weight=50 blockGap=8 //% blockId=device_get_running_time block="running time (ms)" //% advanced=true int runningTime() { return system_timer_current_time(); }
/*---------------------------------------------------------------------------*/ clock_time_t clock_time(void) { return system_timer_current_time(); }
/** * Determines if we're due to take another temperature reading * * @return 1 if we're due to take a temperature reading, 0 otherwise. */ int MicroBitThermometer::isSampleNeeded() { return system_timer_current_time() >= sampleTime; }
mp_uint_t mp_hal_ticks_ms(void) { return system_timer_current_time(); }