mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-02-23 23:55:15 +08:00
clarifications and minor changes in slow clock related operations:
* SpinDelay and SpinDelayUs: now rounding applies instead of truncating * Document maximum duration of SpinDelay/SpinDelayUs/BUTTON_CLICK/BUTTON_HELD * Add argument check against max duration in SpinDelay/BUTTON_CLICK/BUTTON_HELD * Document why these functions don't need to take wrapping into acount as in GetTickCountDelta * Use >= in the spinning loops * Clarify StartTickCount calibration operations and use masks AT91C_CKGR_MAINRDY and AT91C_RTTC_RTPRES
This commit is contained in:
parent
25748b0746
commit
2cbe43f269
3 changed files with 35 additions and 11 deletions
|
@ -9,12 +9,14 @@
|
|||
// Timers, Clocks functions used in LF or Legic where you would need detailed time.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "ticks.h"
|
||||
// attempt at high resolution microsecond timer
|
||||
// beware: timer counts in 21.3uS increments (1024/48Mhz)
|
||||
|
||||
// timer counts in 21.3uS increments (1024/48Mhz), rounding applies
|
||||
// WARNING: timer can't measure more than 1.39s (21.3uS * 0xffff)
|
||||
void SpinDelayUs(int us) {
|
||||
int ticks = (48 * us) >> 10;
|
||||
int ticks = (48 * us + 512) >> 10;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
// This resets PWMC_CPRDR as well
|
||||
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
|
||||
|
||||
// 48 MHz / 1024 gives 46.875 kHz
|
||||
|
@ -26,14 +28,19 @@ void SpinDelayUs(int us) {
|
|||
|
||||
for (;;) {
|
||||
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
||||
if (now == (uint16_t)(start + ticks))
|
||||
if (now >= (uint16_t)(start + ticks))
|
||||
return;
|
||||
|
||||
WDT_HIT();
|
||||
}
|
||||
}
|
||||
|
||||
// WARNING: timer can't measure more than 1.39s (21.3uS * 0xffff)
|
||||
void SpinDelay(int ms) {
|
||||
if (ms > 1390) {
|
||||
Dbprintf(_RED_("Error, SpinDelay called with %i > 1390"), ms);
|
||||
ms = 1390;
|
||||
}
|
||||
// convert to uS and call microsecond delay function
|
||||
SpinDelayUs(ms * 1000);
|
||||
}
|
||||
|
@ -49,9 +56,10 @@ void SpinDelay(int ms) {
|
|||
void StartTickCount(void) {
|
||||
// This timer is based on the slow clock. The slow clock frequency is between 22kHz and 40kHz.
|
||||
// We can determine the actual slow clock frequency by looking at the Main Clock Frequency Register.
|
||||
uint16_t mainf = AT91C_BASE_PMC->PMC_MCFR & 0xffff; // = 16 * main clock frequency (16MHz) / slow clock frequency
|
||||
// set RealTimeCounter divider to count at 1kHz:
|
||||
AT91C_BASE_RTTC->RTTC_RTMR = AT91C_RTTC_RTTRST | ((256000 + (mainf / 2)) / mainf);
|
||||
while ((AT91C_BASE_PMC->PMC_MCFR & AT91C_CKGR_MAINRDY) == 0); // Wait for MAINF value to become available...
|
||||
uint16_t mainf = AT91C_BASE_PMC->PMC_MCFR & AT91C_CKGR_MAINF; // Get # main clocks within 16 slow clocks
|
||||
// set RealTimeCounter divider to count at 1kHz, should be 32 if RC is exactly at 32kHz:
|
||||
AT91C_BASE_RTTC->RTTC_RTMR = AT91C_RTTC_RTTRST | ((((16000000 / 1000 * 16) + (mainf / 2)) / mainf) & AT91C_RTTC_RTPRES);
|
||||
// note: worst case precision is approx 2.5%
|
||||
}
|
||||
|
||||
|
|
|
@ -176,6 +176,12 @@ void SpinUp(uint32_t speed) {
|
|||
// double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
|
||||
int BUTTON_CLICKED(int ms) {
|
||||
// Up to 500ms in between clicks to mean a double click
|
||||
// timer counts in 21.3uS increments (1024/48Mhz)
|
||||
// WARNING: timer can't measure more than 1.39s (21.3uS * 0xffff)
|
||||
if (ms > 1390) {
|
||||
Dbprintf(_RED_("Error, BUTTON_CLICKED called with %i > 1390"), ms);
|
||||
ms = 1390;
|
||||
}
|
||||
int ticks = (48000 * (ms ? ms : 1000)) >> 10;
|
||||
|
||||
// If we're not even pressed, forget about it!
|
||||
|
@ -183,6 +189,7 @@ int BUTTON_CLICKED(int ms) {
|
|||
return BUTTON_NO_CLICK;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
// This resets PWMC_CPRDR as well
|
||||
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
|
||||
// 48 MHz / 1024 gives 46.875 kHz
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
|
||||
|
@ -209,7 +216,7 @@ int BUTTON_CLICKED(int ms) {
|
|||
// Still haven't let it off
|
||||
else
|
||||
// Have we held down a full second?
|
||||
if (now == (uint16_t)(start + ticks))
|
||||
if (now >= (uint16_t)(start + ticks))
|
||||
return BUTTON_HOLD;
|
||||
}
|
||||
|
||||
|
@ -220,7 +227,7 @@ int BUTTON_CLICKED(int ms) {
|
|||
return BUTTON_DOUBLE_CLICK;
|
||||
|
||||
// Have we ran out of time to double click?
|
||||
else if (now == (uint16_t)(start + ticks))
|
||||
else if (now >= (uint16_t)(start + ticks))
|
||||
// At least we did a single click
|
||||
return BUTTON_SINGLE_CLICK;
|
||||
|
||||
|
@ -233,6 +240,12 @@ int BUTTON_CLICKED(int ms) {
|
|||
|
||||
// Determine if a button is held down
|
||||
int BUTTON_HELD(int ms) {
|
||||
// timer counts in 21.3uS increments (1024/48Mhz)
|
||||
// WARNING: timer can't measure more than 1.39s (21.3uS * 0xffff)
|
||||
if (ms > 1390) {
|
||||
Dbprintf(_RED_("Error, BUTTON_HELD called with %i > 1390"), ms);
|
||||
ms = 1390;
|
||||
}
|
||||
// If button is held for one second
|
||||
int ticks = (48000 * (ms ? ms : 1000)) >> 10;
|
||||
|
||||
|
@ -241,6 +254,7 @@ int BUTTON_HELD(int ms) {
|
|||
return BUTTON_NO_CLICK;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
// This resets PWMC_CPRDR as well
|
||||
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
|
||||
// 48 MHz / 1024 gives 46.875 kHz
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
|
||||
|
@ -257,7 +271,7 @@ int BUTTON_HELD(int ms) {
|
|||
return BUTTON_SINGLE_CLICK;
|
||||
|
||||
// Have we waited the full second?
|
||||
else if (now == (uint16_t)(start + ticks))
|
||||
else if (now >= (uint16_t)(start + ticks))
|
||||
return BUTTON_HOLD;
|
||||
|
||||
WDT_HIT();
|
||||
|
|
|
@ -446,9 +446,11 @@ AT91S_CDC_LINE_CODING line = { // purely informative, actual values don't matter
|
|||
8 // 8 Data bits
|
||||
};
|
||||
|
||||
// timer counts in 21.3uS increments (1024/48Mhz), rounding applies
|
||||
// WARNING: timer can't measure more than 1.39s (21.3uS * 0xffff)
|
||||
static void SpinDelay(int ms) {
|
||||
int us = ms * 1000;
|
||||
int ticks = (48 * us) >> 10;
|
||||
int ticks = (48 * us + 512) >> 10;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
|
||||
|
|
Loading…
Reference in a new issue