2017-09-29 16:58:13 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-10-28 23:16:39 -04:00
|
|
|
|
2017-09-29 16:58:13 -04:00
|
|
|
#include "tick.h"
|
2016-10-28 23:16:39 -04:00
|
|
|
|
2017-09-29 16:58:13 -04:00
|
|
|
#include "peripheral_clk_config.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
|
|
|
|
#include "supervisor/shared/autoreload.h"
|
2017-10-12 20:10:31 -04:00
|
|
|
#include "shared-module/gamepad/__init__.h"
|
2017-09-29 16:58:13 -04:00
|
|
|
#include "shared-bindings/microcontroller/Processor.h"
|
2016-10-28 23:16:39 -04:00
|
|
|
|
|
|
|
// Global millisecond tick count
|
2016-11-03 18:50:59 -04:00
|
|
|
volatile uint64_t ticks_ms = 0;
|
2016-10-28 23:16:39 -04:00
|
|
|
|
2017-09-29 16:58:13 -04:00
|
|
|
void SysTick_Handler(void) {
|
2016-10-28 23:16:39 -04:00
|
|
|
// SysTick interrupt handler called when the SysTick timer reaches zero
|
|
|
|
// (every millisecond).
|
|
|
|
ticks_ms += 1;
|
|
|
|
|
2017-05-12 19:45:38 -04:00
|
|
|
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
|
|
|
|
autoreload_tick();
|
2016-10-28 23:16:39 -04:00
|
|
|
#endif
|
2017-10-03 16:35:57 -04:00
|
|
|
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
|
|
|
if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
|
|
|
|
gamepad_tick();
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-28 23:16:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tick_init() {
|
2017-09-29 16:58:13 -04:00
|
|
|
uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000;
|
2018-04-17 21:46:15 -04:00
|
|
|
SysTick_Config(ticks_per_ms-1);
|
2017-09-29 16:58:13 -04:00
|
|
|
NVIC_EnableIRQ(SysTick_IRQn);
|
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2017-09-29 16:58:13 -04:00
|
|
|
void tick_delay(uint32_t us) {
|
|
|
|
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
|
2018-04-19 22:25:48 -04:00
|
|
|
uint32_t us_until_next_tick = SysTick->VAL / ticks_per_us;
|
2018-04-16 08:11:14 -04:00
|
|
|
uint32_t start_tick;
|
2017-09-29 16:58:13 -04:00
|
|
|
while (us > 1000) {
|
2018-04-19 22:25:48 -04:00
|
|
|
// check if interrupts are disabled
|
|
|
|
if(__get_PRIMASK())
|
|
|
|
return; // if not just return
|
2018-04-16 08:11:14 -04:00
|
|
|
start_tick=SysTick->VAL; // wait for SysTick->VAL to RESET
|
|
|
|
while (SysTick->VAL < start_tick) {}
|
2018-04-19 22:25:48 -04:00
|
|
|
us -= us_until_next_tick;
|
|
|
|
us_until_next_tick = 1000;
|
2017-09-29 16:58:13 -04:00
|
|
|
}
|
2018-04-19 22:25:48 -04:00
|
|
|
if(us&&(us < us_until_next_tick)){
|
|
|
|
while (SysTick->VAL > ((us_until_next_tick - us) * ticks_per_us)) {}
|
2018-04-16 08:11:14 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
start_tick=SysTick->VAL; // wait for SysTick->VAL to RESET
|
|
|
|
while (SysTick->VAL < start_tick) {}
|
2018-04-19 22:25:48 -04:00
|
|
|
us -= us_until_next_tick;
|
2018-04-16 08:11:14 -04:00
|
|
|
if(us){
|
|
|
|
while (SysTick->VAL > ((1000 - us) * ticks_per_us)) {}
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 01:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// us counts down!
|
|
|
|
void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
|
|
|
|
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
|
|
|
|
*ms = ticks_ms;
|
|
|
|
*us_until_ms = SysTick->VAL / ticks_per_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_until(uint64_t ms, uint32_t us_until_ms) {
|
|
|
|
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
|
|
|
|
while(ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
|
2016-10-28 23:16:39 -04:00
|
|
|
}
|