Make common_arm to hardware abstraction layer.
This commit is contained in:
1 parent
51d0ff260a
commit
36f1c7e44c
61 files changed
+8050
-1670
No files matched your search
@@ -0,0 +1,106 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Jonathan Westhues, Aug 2005
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Timers, Clocks functions used in LF or Legic where you would need detailed time.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef TICKS_H_
|
||||
#define TICKS_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifndef GET_TICKS
|
||||
#define GET_TICKS GetTicks()
|
||||
#endif
|
||||
|
||||
void StartTicks(void);
|
||||
uint32_t GetTicks(void);
|
||||
uint32_t RAMFUNC GetTicksDelta(uint32_t start);
|
||||
void WaitUS(uint32_t us);
|
||||
void WaitTicks(uint32_t ticks);
|
||||
void ResetTicks(void);
|
||||
void StopTicks(void);
|
||||
|
||||
void StartCountUS(void);
|
||||
uint32_t RAMFUNC GetCountUS(void);
|
||||
|
||||
void SpinDelayUs(int us);
|
||||
|
||||
#ifndef AS_BOOTROM //////////////////////////////////////////////////////////////
|
||||
// Bootrom does not require these functions.
|
||||
// Wrap in #ifndef to avoid accidental bloat of bootrom
|
||||
|
||||
void SpinDelay(int ms);
|
||||
void SpinDelayUsPrecision(int us); // precision 0.6us , running for 43ms before
|
||||
|
||||
void StartTickCount(void);
|
||||
uint32_t RAMFUNC GetTickCount(void);
|
||||
uint32_t RAMFUNC GetTickCountDelta(uint32_t start_ticks);
|
||||
void UpdateTickCountLabel(void);
|
||||
uint32_t GetTickCountLabel(void);
|
||||
|
||||
// void ResetUSClock(void); No implemented?
|
||||
// void SpinDelayCountUs(uint32_t us);
|
||||
|
||||
void StartCountSspClk(void);
|
||||
void ResetSspClk(void);
|
||||
uint32_t RAMFUNC GetCountSspClk(void);
|
||||
uint32_t RAMFUNC GetCountSspClkDelta(uint32_t start);
|
||||
|
||||
void WaitMS(uint32_t ms);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Generic precision timer counter, input capture and timestamp counter.
|
||||
// These primitives back the precise timing / edge-capture needs of the LF
|
||||
// protocols (e.g. Hitag). They are intentionally generic and platform-agnostic.
|
||||
//
|
||||
// The precision counter and timestamp counter both run at 1.5 MHz
|
||||
// (12 counts = 1 T0 = 8 us, see hitag_common.h for the T0 definition).
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Free-running precision counter @ 1.5 MHz (12 counts = 1 T0 = 8 us).
|
||||
void StartPrecisionCounter(void); // configure + start + reset
|
||||
void StopPrecisionCounter(void);
|
||||
void ResetPrecisionCounter(void); // software reset to 0
|
||||
uint16_t RAMFUNC GetPrecisionCounter(void); // current count (16-bit)
|
||||
|
||||
// Input capture(LF_EDGE_DETECT): rising/falling edges of an external signal.
|
||||
void StartLoEdgeCapture(void); // configure + start + reset
|
||||
void StopLoEdgeCapture(void); // disable capture
|
||||
void EnableLoEdgeCapture(void); // re-enable + reset (no reconfiguration)
|
||||
void ResetLoEdgeCapture(void); // software reset
|
||||
typedef enum { LO_EDGE_NO = 0, LO_EDGE_RISING = 1, LO_EDGE_FALLING = 2 } lo_edge_t;
|
||||
lo_edge_t RAMFUNC GetLoEdgeCaptureStatus(void); // edge-event flags (reading clears them)
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureCount(void); // current free-running count
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureFalling(void); // value captured on the falling edge
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureRising(void); // value captured on the rising edge
|
||||
|
||||
// Monotonic timestamp counter (free-running + overflow accumulation).
|
||||
// One 125 kHz carrier period (8 us) equals this many counter ticks at 1.5 MHz.
|
||||
#define TICKS_PER_CARRIER_PERIOD 12
|
||||
void StartTimestamp(void); // configure + start + clear (counter and overflow)
|
||||
void StopTimestamp(void);
|
||||
uint32_t RAMFUNC GetTimestamp(void); // monotonic timestamp in 125 kHz carrier periods
|
||||
|
||||
#endif // #ifndef AS_BOOTROM
|
||||
|
||||
#ifdef PM5
|
||||
#include "ticks_hw_at32.h"
|
||||
#else
|
||||
#include "ticks_hw_at91.h"
|
||||
#endif
|
||||
|
||||
#endif // TICKS_H_
|
||||
@@ -0,0 +1,101 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Jonathan Westhues, Sept 2005
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Timers, Clocks functions used in LF or Legic where you would need detailed time.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "ticks_apis.h"
|
||||
|
||||
// For OS include
|
||||
#ifndef AS_BOOTROM
|
||||
#include "dbprint.h"
|
||||
#endif
|
||||
|
||||
#ifndef AS_BOOTROM
|
||||
|
||||
// Increments whenever StartTickCount() reconfigures/resets RTTC.
|
||||
// Callers can use this to detect that previously saved tick deltas are no longer valid.
|
||||
static uint32_t g_tickcount_label = 0;
|
||||
|
||||
// WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
|
||||
void SpinDelay(int ms) {
|
||||
if (ms > 1390) {
|
||||
if (g_dbglevel >= DBG_ERROR) Dbprintf(_RED_("Error, SpinDelay called with %i > 1390"), ms);
|
||||
ms = 1390;
|
||||
}
|
||||
// convert to us and call microsecond delay function
|
||||
SpinDelayUs(ms * 1000);
|
||||
}
|
||||
|
||||
// Get tick count from start_ticks to now.
|
||||
uint32_t RAMFUNC GetTickCountDelta(uint32_t start_ticks) {
|
||||
uint32_t stop_ticks = GetTickCount();
|
||||
if (stop_ticks >= start_ticks) {
|
||||
return stop_ticks - start_ticks;
|
||||
}
|
||||
return (UINT32_MAX - start_ticks) + stop_ticks;
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this function within StartTickCount() to increment the tick count label.
|
||||
* You must do it in all platform implementations.
|
||||
*/
|
||||
void UpdateTickCountLabel(void) {
|
||||
g_tickcount_label++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get current RTTC counter label.
|
||||
* If counter config changes between calls, the value is incremented.
|
||||
*/
|
||||
uint32_t GetTickCountLabel(void) {
|
||||
return g_tickcount_label;
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetCountSspClkDelta(uint32_t start) {
|
||||
uint32_t stop = GetCountSspClk();
|
||||
if (stop >= start) {
|
||||
return stop - start;
|
||||
}
|
||||
return (UINT32_MAX - start) + stop;
|
||||
}
|
||||
|
||||
void WaitMS(uint32_t ms) {
|
||||
WaitTicks((ms & 0x1FFFFF) * 1500);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
uint32_t RAMFUNC GetTicksDelta(uint32_t start) {
|
||||
uint32_t stop = GetTicks();
|
||||
if (stop >= start) {
|
||||
return stop - start;
|
||||
}
|
||||
return (UINT32_MAX - start) + stop;
|
||||
}
|
||||
|
||||
// Wait - Spindelay in ticks.
|
||||
// if called with a high number, this will trigger the WDT...
|
||||
void WaitTicks(uint32_t ticks) {
|
||||
if (ticks == 0) return;
|
||||
ticks += GetTicks();
|
||||
while (GetTicks() < ticks);
|
||||
}
|
||||
|
||||
// Wait / Spindelay in us (microseconds)
|
||||
// 1us = 1.5ticks.
|
||||
void WaitUS(uint32_t us) {
|
||||
WaitTicks((us & 0x3FFFFFFF) * 3 / 2);
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Jonathan Westhues, Sept 2005
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Timers, Clocks functions used in LF or Legic where you would need detailed time.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "ticks_apis.h"
|
||||
#include "proxmark3_arm.h"
|
||||
#include "ticks_hw_at32.h"
|
||||
|
||||
#include "at32f435_437.h"
|
||||
#include "at32f435_437_misc.h"
|
||||
#include "at32f435_437_pwc.h"
|
||||
#include "at32f435_437_ertc.h"
|
||||
|
||||
/**
|
||||
* SysTick 频率计算,以下计算条件需要严格遵守 AHBCLK = 288mhz 且 systick的时钟输入是 AHBCLK 的8分频的条件
|
||||
*
|
||||
* - AHBCLK = 288,000, 000 = 288mhz
|
||||
* - systick-clk = 288mhz / 8 = 36,000,000 = 36mhz = 27.7ns
|
||||
* - systick-val = 24bit = 0xFFFFFF = 16777215
|
||||
* - max time = 27.7ns * 16777215 = 464,728.8555us = 464.7288555ms
|
||||
*/
|
||||
#define MAX_US_STEP (464728U)
|
||||
|
||||
// timer counts in 27.7ns increments (16777215/36MHz), rounding applies
|
||||
// WARNING: timer can't measure more than 1.39s (27.7ns * 0xFFFFFF * 3), more loop delay may to decreased accuracy.
|
||||
void SpinDelayUs(int us) {
|
||||
uint32_t fac_us = system_core_clock / 8 / 1000000;
|
||||
uint32_t temp = 0;
|
||||
SysTick->CTRL &= ~(uint32_t)SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV; // ahbclk div8 = 36mhz
|
||||
while (us) {
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
if (us > MAX_US_STEP) {
|
||||
SysTick->LOAD = MAX_US_STEP * fac_us;
|
||||
us -= MAX_US_STEP;
|
||||
} else {
|
||||
SysTick->LOAD = us * fac_us;
|
||||
us = 0;
|
||||
}
|
||||
SysTick->VAL = 0x00;
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
|
||||
do {
|
||||
temp = SysTick->CTRL;
|
||||
} while ((temp & 0x01) && !(temp & (1 << 16)));
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
SysTick->VAL = 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
// configCounter() is defined below (outside AS_BOOTROM); forward-declare it so the
|
||||
// precision/timestamp counters inside the AS_BOOTROM block can reuse it.
|
||||
static void configCounter(const uint32_t frequency);
|
||||
|
||||
#ifndef AS_BOOTROM
|
||||
|
||||
// timer counts in 27.7ns increments (16777215/36MHz), rounding applies
|
||||
// WARNING: timer can't measure more than 464.7288555ms (27.7ns * 0xFFFFFF)
|
||||
void SpinDelayUsPrecision(int us) {
|
||||
uint32_t fac_us = system_core_clock / 8 / 1000000;
|
||||
uint32_t temp = 0;
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
SysTick->CTRL &= ~(uint32_t)SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV; // ahbclk div8 = 36mhz
|
||||
SysTick->VAL = 0x00;
|
||||
SysTick->LOAD = us * fac_us;
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
|
||||
do {
|
||||
temp = SysTick->CTRL;
|
||||
} while ((temp & 0x01) && !(temp & (1 << 16)));
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
SysTick->VAL = 0x00;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer lib: 1 kHz: TickCount functions
|
||||
//
|
||||
// Precision Test Procedure:
|
||||
// ti = GetTickCount();
|
||||
// SpinDelay(1000);
|
||||
// ti = GetTickCount() - ti;
|
||||
// Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Cached tick start value when 'StartTickCount' call.
|
||||
static volatile uint64_t tick_start_val;
|
||||
|
||||
// from date to timestamp(unix format, UTC zone only)
|
||||
// we can use 'mktime()' from 'time.h', but more rom space required, so custom first.
|
||||
// tips: year is full length, such as: 2025, not 25
|
||||
static uint64_t mktime_utc_fast(int year, int month, int day, int hour, int minute, int second, uint32_t ms) {
|
||||
static const uint16_t cum_days[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
|
||||
|
||||
int years = year - 1970;
|
||||
int leap_count = (years + 2) / 4;
|
||||
if (year > 2100) leap_count--;
|
||||
if (year > 2200) leap_count--;
|
||||
if (year > 2300) leap_count--;
|
||||
|
||||
uint64_t days = years * 365ULL + leap_count;
|
||||
days += cum_days[month - 1];
|
||||
if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
|
||||
days++;
|
||||
}
|
||||
days += (day - 1);
|
||||
|
||||
return (days * 86400ULL + hour * 3600ULL + minute * 60ULL + second) * 1000ULL + ms;
|
||||
}
|
||||
|
||||
// Start tick count
|
||||
void StartTickCount(void) {
|
||||
UpdateTickCountLabel();
|
||||
crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE); // enable the pwc clock
|
||||
pwc_battery_powered_domain_access(TRUE); // allow access to ertc
|
||||
crm_battery_powered_domain_reset(TRUE); // reset ertc bpr domain
|
||||
crm_battery_powered_domain_reset(FALSE);
|
||||
// Select clock source: HEXT = 8mhz, ertc clk = 400khz
|
||||
// When using an external high-speed crystal oscillator, the clock can be very accurate,
|
||||
// so calibration does not need to be considered temporarily.
|
||||
crm_ertc_clock_select(CRM_ERTC_CLOCK_HEXT_DIV_20);
|
||||
crm_ertc_clock_enable(TRUE); // enable the ertc clock
|
||||
ertc_reset(); // deinitializes the ertc registers
|
||||
ertc_wait_update(); // wait for ertc apb registers update
|
||||
// configure the ertc divider, ertc second(1hz) = ertc_clk / (div_a + 1) * (div_b + 1)
|
||||
// the subsecond frequency is 3125(from div_b clk), so 1 clk = 0.32ms = 320us, the subsecond will -1 every 0.32ms
|
||||
ertc_divider_set(127, 3124); // 400000 / (127 + 1) * (3124 + 1) = 1hz
|
||||
ertc_hour_mode_set(ERTC_HOUR_MODE_24); // configure the ertc hour mode
|
||||
// set datetime: 2025-08-15 13:00:00, format: YEAR-MONTH-DAY HOUR:MINUTE:SECOND
|
||||
ertc_date_set(25, 8, 15, 5); // set date
|
||||
ertc_time_set(13, 0, 0, ERTC_AM); // set time
|
||||
// update tick start value when 'poweron'
|
||||
// no need calc, we can hard code cause by 'ertc_date_set' and 'ertc_time_set' is hard code
|
||||
// calc online: https://www.timestamp-converter.com/
|
||||
tick_start_val = 1755262800000ULL; // tick_start_val = mktime_utc_fast(2025, 8, 15, 13, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Get the current count.
|
||||
uint32_t RAMFUNC GetTickCount(void) {
|
||||
ertc_time_type time;
|
||||
ertc_calendar_get(&time);
|
||||
return mktime_utc_fast(
|
||||
// time.year is short length, not full, so 2025 is 25.
|
||||
time.year + 2000,
|
||||
// month & day & hour & min & sec is full length
|
||||
time.month, time.day, time.hour, time.min, time.sec,
|
||||
// this ms is from 0 -> 1000 of second, not timestamp value
|
||||
// ms = ((divb + 1) - subsecond * 1000) / (divb + 1)
|
||||
(3125 - ertc_sub_second_get()) * 1000 / 3125) - tick_start_val; // current - start = tick
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer for iso14443 commands. Uses ssp_clk from FPGA
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
void StartCountSspClk(void) {
|
||||
crm_periph_clock_enable(CRM_GPIO_PERIPH_COUNT_SSP_CLK, TRUE);
|
||||
crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_COUNT_SSP_CLK, TRUE);
|
||||
|
||||
// gpio init
|
||||
gpio_init_type gpio_init_struct = {0};
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_pins = CRM_GPIO_COUNT_SSP_CLK_PIN;
|
||||
gpio_init(CRM_GPIO_COUNT_SSP_CLK, &gpio_init_struct); // gpio setup
|
||||
gpio_pin_mux_config(CRM_GPIO_COUNT_SSP_CLK, CRM_GPIO_COUNT_SSP_CLK_SOURCE, CRM_GPIO_COUNT_SSP_CLK_MUX); // important !!! remap gpio to be timer EXT(CHx) function.
|
||||
|
||||
// timer init
|
||||
tmr_input_config_type tmr_input_config_struct;
|
||||
tmr_input_config_struct.input_channel_select = AT32_TMR_COUNT_SSP_CLK_IN_CH;
|
||||
tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
|
||||
tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE;
|
||||
tmr_input_channel_init(AT32_TMR_COUNT_SSP_CLK, &tmr_input_config_struct, TMR_CHANNEL_INPUT_DIV_1);
|
||||
tmr_trigger_input_select(AT32_TMR_COUNT_SSP_CLK, TMR_SUB_INPUT_SEL_C2DF2); // select the timer input trigger: C2IF2
|
||||
tmr_sub_mode_select(AT32_TMR_COUNT_SSP_CLK, TMR_SUB_EXTERNAL_CLOCK_MODE_A); // select the slave mode: external mode a
|
||||
tmr_32_bit_function_enable(AT32_TMR_COUNT_SSP_CLK, TRUE); // 32bit enable, reduce the complexity of cascading.
|
||||
tmr_base_init(AT32_TMR_COUNT_SSP_CLK, UINT32_MAX - 1, 0); // 288mhz, not count increment frequency.
|
||||
tmr_cnt_dir_set(AT32_TMR_COUNT_SSP_CLK, TMR_COUNT_UP);
|
||||
// tmr_external_clock_mode2_config(CRM_TMR_COUNT_SSP_CLK, TMR_ES_FREQUENCY_DIV_1, TMR_ES_POLARITY_NON_INVERTED, 0x00); ext引脚而非ch2引脚时,使用此初始化函数
|
||||
tmr_counter_enable(AT32_TMR_COUNT_SSP_CLK, TRUE);
|
||||
|
||||
// TODO DXL 可能还得像原先的逻辑那样,跳过8个clock,去同步ssp的frame和时钟,因为我们没有用级联定时器这种操作,理论上
|
||||
// 可能只需要同步一次frame的上升和下降,因为在ssp-timode的实现下,frame的上升刚好是在lsb的上升沿去执行的,
|
||||
// 同步完成之后,理论上下一次clk的上升刚好就是下一帧的msb,这个时候重置一下clk值就刚好是新的一次帧计数?不过,这还不好说,具体得看后续的实现。
|
||||
}
|
||||
|
||||
void ResetSspClk(void) {
|
||||
// tmr_counter_value_set(CRM_TMR_COUNT_SSP_CLK, 0);
|
||||
AT32_TMR_COUNT_SSP_CLK->cval = 0;
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetCountSspClk(void) {
|
||||
// return tmr_counter_value_get(CRM_TMR_COUNT_SSP_CLK);
|
||||
return AT32_TMR_COUNT_SSP_CLK->cval;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Precision counter, input capture and timestamp counter.
|
||||
// See ticks_apis.h for the generic contract. Both the precision counter and
|
||||
// the timestamp counter run at 1.5 MHz (12 counts = 1 T0 = 8 us).
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Timestamp counter overflow count, combined for ~47 min timing.
|
||||
static uint16_t timestamp_high = 0;
|
||||
|
||||
void StartPrecisionCounter(void) {
|
||||
// Reuses the 32-bit timer @ 1.5 MHz (same source as StartTicks).
|
||||
configCounter(1500000);
|
||||
}
|
||||
|
||||
void StopPrecisionCounter(void) {
|
||||
tmr_counter_enable(AT32_TMR_PRECISE_COUNTER, FALSE);
|
||||
}
|
||||
|
||||
void ResetPrecisionCounter(void) {
|
||||
tmr_counter_value_set(AT32_TMR_PRECISE_COUNTER, 0);
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetPrecisionCounter(void) {
|
||||
return (uint16_t)tmr_counter_value_get(AT32_TMR_PRECISE_COUNTER);
|
||||
}
|
||||
|
||||
void StartLoEdgeCapture(void) {
|
||||
crm_periph_clock_enable(CRM_GPIO_PERIPH_INPUT_CAPTURE, TRUE);
|
||||
crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_INPUT_CAPTURE, TRUE);
|
||||
|
||||
// GPIO: PB4 -> TMR3_CH1 (input capture on the LF SSC frame signal).
|
||||
gpio_init_type gpio_init_struct = {0};
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_pins = CRM_GPIO_INPUT_CAPTURE_PIN;
|
||||
gpio_init(CRM_GPIO_INPUT_CAPTURE, &gpio_init_struct); // gpio setup
|
||||
gpio_pin_mux_config(CRM_GPIO_INPUT_CAPTURE, CRM_GPIO_INPUT_CAPTURE_SOURCE, CRM_GPIO_INPUT_CAPTURE_MUX); // remap gpio to TMR3_CH1
|
||||
|
||||
// Time base: 16-bit counter @ 1.5 MHz (TIMER_CLK / (191 + 1) = 288MHz / 192),
|
||||
// matching the AT91 TC1 (MCK/32) so that 12 counts = 1 T0 = 8 us.
|
||||
tmr_reset(AT32_TMR_INPUT_CAPTURE);
|
||||
tmr_base_init(AT32_TMR_INPUT_CAPTURE, UINT16_MAX, 191);
|
||||
tmr_cnt_dir_set(AT32_TMR_INPUT_CAPTURE, TMR_COUNT_UP);
|
||||
|
||||
// PWM input mode (dual-edge capture) on CH1 (TI1 = PB4):
|
||||
// CH1 = direct + falling edge, CH2 = indirect (chained from TI1) + rising edge.
|
||||
tmr_input_config_type ic = {0};
|
||||
ic.input_channel_select = TMR_SELECT_CHANNEL_1;
|
||||
ic.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
|
||||
ic.input_polarity_select = TMR_INPUT_FALLING_EDGE;
|
||||
tmr_pwm_input_config(AT32_TMR_INPUT_CAPTURE, &ic, TMR_CHANNEL_INPUT_DIV_1);
|
||||
|
||||
// Slave reset mode: reset the counter on the CH1 (falling) edge, so C1DT holds
|
||||
// the period since the previous falling edge (matches AT91 ABETRG + ETRGEDG_FALLING).
|
||||
tmr_trigger_input_select(AT32_TMR_INPUT_CAPTURE, TMR_SUB_INPUT_SEL_C1DF1);
|
||||
tmr_sub_mode_select(AT32_TMR_INPUT_CAPTURE, TMR_SUB_RESET_MODE);
|
||||
tmr_sub_sync_mode_set(AT32_TMR_INPUT_CAPTURE, TRUE);
|
||||
|
||||
tmr_counter_value_set(AT32_TMR_INPUT_CAPTURE, 0);
|
||||
tmr_counter_enable(AT32_TMR_INPUT_CAPTURE, TRUE);
|
||||
}
|
||||
|
||||
void StopLoEdgeCapture(void) {
|
||||
tmr_counter_enable(AT32_TMR_INPUT_CAPTURE, FALSE);
|
||||
}
|
||||
|
||||
void EnableLoEdgeCapture(void) {
|
||||
tmr_counter_value_set(AT32_TMR_INPUT_CAPTURE, 0);
|
||||
tmr_counter_enable(AT32_TMR_INPUT_CAPTURE, TRUE);
|
||||
}
|
||||
|
||||
void ResetLoEdgeCapture(void) {
|
||||
tmr_counter_value_set(AT32_TMR_INPUT_CAPTURE, 0);
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureCount(void) {
|
||||
return (uint16_t)tmr_counter_value_get(AT32_TMR_INPUT_CAPTURE);
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureFalling(void) {
|
||||
// The falling-edge value is captured on CH1 (C1DT).
|
||||
return (uint16_t)tmr_channel_value_get(AT32_TMR_INPUT_CAPTURE, TMR_SELECT_CHANNEL_1);
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureRising(void) {
|
||||
// The rising-edge value is captured on CH2 (C2DT).
|
||||
return (uint16_t)tmr_channel_value_get(AT32_TMR_INPUT_CAPTURE, TMR_SELECT_CHANNEL_2);
|
||||
}
|
||||
|
||||
lo_edge_t RAMFUNC GetLoEdgeCaptureStatus(void) {
|
||||
// Reading the status clears the edge-event flags (matches AT91 TC_SR semantics).
|
||||
uint32_t ists = AT32_TMR_INPUT_CAPTURE->ists;
|
||||
// Only clear the overflow flag if it is set, to avoid clearing the edge-event flags.
|
||||
if (ists & TMR_OVF_FLAG) {
|
||||
// Clear the overflow flag to avoid repeated interrupts.
|
||||
AT32_TMR_INPUT_CAPTURE->ists = ~TMR_OVF_FLAG;
|
||||
}
|
||||
if (ists & INPUT_CAPTURE_EVT_RISING_EDGE) {
|
||||
AT32_TMR_INPUT_CAPTURE->ists = ~INPUT_CAPTURE_EVT_RISING_EDGE;
|
||||
return LO_EDGE_RISING;
|
||||
}
|
||||
if (ists & INPUT_CAPTURE_EVT_FALLING_EDGE) {
|
||||
AT32_TMR_INPUT_CAPTURE->ists = ~INPUT_CAPTURE_EVT_FALLING_EDGE;
|
||||
return LO_EDGE_FALLING;
|
||||
}
|
||||
return LO_EDGE_NO;
|
||||
}
|
||||
|
||||
void StartTimestamp(void) {
|
||||
// TMR6: basic 16-bit timer, free-running @ 1.5 MHz.
|
||||
crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_TIMESTAMP, TRUE);
|
||||
// APB1 = 144 MHz, divX = (144 MHz / 1.5 MHz) * 2 - 1 = 191 (see configCounter()).
|
||||
tmr_base_init(AT32_TMR_TIMESTAMP, UINT16_MAX, 191);
|
||||
tmr_cnt_dir_set(AT32_TMR_TIMESTAMP, TMR_COUNT_UP);
|
||||
tmr_counter_value_set(AT32_TMR_TIMESTAMP, 0);
|
||||
tmr_counter_enable(AT32_TMR_TIMESTAMP, TRUE);
|
||||
timestamp_high = 0;
|
||||
}
|
||||
|
||||
void StopTimestamp(void) {
|
||||
tmr_counter_enable(AT32_TMR_TIMESTAMP, FALSE);
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetTimestamp(void) {
|
||||
if (tmr_flag_get(AT32_TMR_TIMESTAMP, TMR_OVF_FLAG)) {
|
||||
tmr_flag_clear(AT32_TMR_TIMESTAMP, TMR_OVF_FLAG);
|
||||
timestamp_high++;
|
||||
}
|
||||
uint16_t cv = (uint16_t)tmr_counter_value_get(AT32_TMR_TIMESTAMP);
|
||||
return (((uint32_t)timestamp_high << 16) + cv) / TICKS_PER_CARRIER_PERIOD;
|
||||
}
|
||||
|
||||
#endif // #ifndef AS_BOOTROM
|
||||
|
||||
/**
|
||||
* Configure the timer to count up at the specified frequency.
|
||||
* @param frequency the frequency of timer running.
|
||||
*/
|
||||
static void configCounter(const uint32_t frequency) {
|
||||
crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_32B_TIMER_CLK, TRUE);
|
||||
|
||||
// AT32 has a 32-bit timer, perhaps we can achieve higher counting time without connecting the timer?
|
||||
tmr_32_bit_function_enable(AT32_TMR_32B_TIMER, TRUE);
|
||||
|
||||
// TODO DXL 注意,如果apb1的预分频系数不是1,那么TIMER5的时钟速度会是apb1的两倍,这里记录下来,后期开发可能会遇到,如果完成移植,可将此段注释删除
|
||||
// See at32f435 manual reference 4.1.3
|
||||
// The timer uses APB1/2 as the clock. In particular, when the APB pre division coefficient is 1,
|
||||
// the clock frequency of the timer is equal to the clock frequency of APB1/2;
|
||||
// When the APB prescaler coefficient is not 1, the clock frequency of the timer is equal to twice the APB1/2 clock frequency.
|
||||
// So, if we are using not apb from ahb/1, must to div2.
|
||||
#define FREQUENCY_APB1 144000000UL // apb1 = ahb/2 = 144mhz, apb1*2 = TIMER_CLK, TIMER_CLK/192(divX) = 1.5mhz
|
||||
const uint32_t divX = (FREQUENCY_APB1 / frequency) * 2 - 1;
|
||||
tmr_base_init(AT32_TMR_32B_TIMER, UINT32_MAX - 1, divX);
|
||||
|
||||
tmr_cnt_dir_set(AT32_TMR_32B_TIMER, TMR_COUNT_UP);
|
||||
tmr_counter_enable(AT32_TMR_32B_TIMER, TRUE);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// microseconds timer
|
||||
// 1us = 1tick
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
void StartCountUS(void) {
|
||||
// see: https://github.com/RfidResearchGroup/proxmark3/blob/master/doc/clocks.md#occasional-tc0tc1--countus-functions
|
||||
configCounter(1000000); // 1 MHZ
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetCountUS(void) {
|
||||
// TODO DXL maybe no function call is a good idea?
|
||||
// If it affects accuracy, you can consider directly reading the register.
|
||||
// return AT32_TMR_32B_TIMER->cval;
|
||||
return tmr_counter_value_get(AT32_TMR_32B_TIMER);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer for bitbanging, or LF stuff when you need a very precise timer
|
||||
// 1us = 1.5ticks
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
void StartTicks(void) {
|
||||
// see: https://github.com/RfidResearchGroup/proxmark3/blob/master/doc/clocks.md#occasional-tc0tc1--ticks-functions
|
||||
configCounter(1500000); // 1.5 MHz
|
||||
}
|
||||
|
||||
// Reset the count value to 0
|
||||
void ResetTicks(void) {
|
||||
tmr_counter_value_set(AT32_TMR_32B_TIMER, 0);
|
||||
}
|
||||
|
||||
void StopTicks(void) {
|
||||
tmr_counter_enable(AT32_TMR_32B_TIMER, FALSE);
|
||||
crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_32B_TIMER_CLK, FALSE);
|
||||
// TODO DXL 也许需要在这里停止 其他定时器,因为PM3原本的代码有这个设计,但是我们需要查一下用处,看看是否能这么做
|
||||
}
|
||||
|
||||
uint32_t GetTicks(void) {
|
||||
// TODO DXL maybe no function call is a good idea?
|
||||
// If it affects accuracy, you can consider directly reading the register.
|
||||
// return AT32_TMR_32B_TIMER->cval;
|
||||
return tmr_counter_value_get(AT32_TMR_32B_TIMER);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by dxl on 2026/2/7.
|
||||
//
|
||||
|
||||
#ifndef TICKS_HW_AT32_H
|
||||
#define TICKS_HW_AT32_H
|
||||
|
||||
#include "at32f435_437_crm.h"
|
||||
#include "at32f435_437_tmr.h"
|
||||
|
||||
// TODO DXL 用 TIMER2 的 ch2 来统计来自于外部ssp-clk的时钟数量
|
||||
// 用于 at32 不支持类似 at91 那种 gpio 的输入直接两个外设就能同时使用的情况,所以必须要将实际上 ssp-clk 的脚,连接到 ch2 上
|
||||
// 这样子 TIMER2 选中为外部时钟输入模式时,才能最终链接 SSP 和 TMR
|
||||
|
||||
// ssp clk counter
|
||||
#define AT32_CRM_TMR_PERIPH_COUNT_SSP_CLK CRM_TMR2_PERIPH_CLOCK
|
||||
#define AT32_TMR_COUNT_SSP_CLK TMR2
|
||||
#define AT32_TMR_COUNT_SSP_CLK_IN_CH TMR_SELECT_CHANNEL_2
|
||||
|
||||
// 32bit timer
|
||||
#define AT32_CRM_TMR_PERIPH_32B_TIMER_CLK CRM_TMR5_PERIPH_CLOCK
|
||||
#define AT32_TMR_32B_TIMER TMR5
|
||||
|
||||
// Input capture edge-event flags (single-bit masks in the TMR status register).
|
||||
// PWM input mode: CH1 = falling edge, CH2 = rising edge (see StartInputCapture).
|
||||
#define INPUT_CAPTURE_EVT_RISING_EDGE TMR_C2_FLAG // CH2 rising-edge capture event
|
||||
#define INPUT_CAPTURE_EVT_FALLING_EDGE TMR_C1_FLAG // CH1 falling-edge capture event
|
||||
|
||||
// Precision free-running counter @ 1.5MHz.
|
||||
// Reuses the 32-bit timer (same source as StartTicks / StartCountUS).
|
||||
#define AT32_CRM_TMR_PERIPH_PRECISE_COUNTER AT32_CRM_TMR_PERIPH_32B_TIMER_CLK
|
||||
#define AT32_TMR_PRECISE_COUNTER AT32_TMR_32B_TIMER
|
||||
|
||||
// Monotonic timestamp counter @ 1.5MHz (16-bit + software overflow tracking).
|
||||
// Uses TMR6 (a basic 16-bit timer, unused elsewhere in the project).
|
||||
#define AT32_CRM_TMR_PERIPH_TIMESTAMP CRM_TMR6_PERIPH_CLOCK
|
||||
#define AT32_TMR_TIMESTAMP TMR6
|
||||
|
||||
// Input capture (CH1 rising + CH2 falling on the same input pin).
|
||||
// Input pin is PB4 = TMR3_CH1 (the LF SSC frame signal).
|
||||
#define AT32_CRM_TMR_PERIPH_INPUT_CAPTURE CRM_TMR3_PERIPH_CLOCK
|
||||
#define AT32_TMR_INPUT_CAPTURE TMR3
|
||||
|
||||
#endif //TICKS_HW_AT32_H
|
||||
@@ -0,0 +1,426 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Jonathan Westhues, Sept 2005
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Timers, Clocks functions used in LF or Legic where you would need detailed time.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "ticks_apis.h"
|
||||
#include "proxmark3_arm.h"
|
||||
|
||||
|
||||
// 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 = ((MCK / 1000000) * us + 512) >> 10;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
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); // Channel Mode Register
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; // Channel Period Register
|
||||
|
||||
uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
|
||||
if (end == 0) { // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
|
||||
end++; // so we have to end++ to avoid inivity loop
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
||||
|
||||
if (now == end) {
|
||||
return;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef AS_BOOTROM
|
||||
|
||||
// timer counts in 666ns increments (32/48MHz), rounding applies
|
||||
// WARNING: timer can't measure more than 43ms (666ns * 0xFFFF)
|
||||
void SpinDelayUsPrecision(int us) {
|
||||
int ticks = ((MCK / 1000000) * us + 16) >> 5;
|
||||
|
||||
// Borrow a PWM unit for my real-time clock
|
||||
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
|
||||
|
||||
// 48 MHz / 32 gives 1.5 Mhz
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(5); // Channel Mode Register
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
|
||||
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xFFFF; // Channel Period Register
|
||||
|
||||
uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
|
||||
if (end == 0) { // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
|
||||
end++; // so we have to end++ to avoid inivity loop
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
||||
|
||||
if (now == end) {
|
||||
return;
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer lib: 1 kHz: TickCount functions
|
||||
//
|
||||
// Precision Test Procedure:
|
||||
// ti = GetTickCount();
|
||||
// SpinDelay(1000);
|
||||
// ti = GetTickCount() - ti;
|
||||
// Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
|
||||
// -------------------------------------------------------------------------
|
||||
void StartTickCount(void) {
|
||||
UpdateTickCountLabel();
|
||||
// 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.
|
||||
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 | ((((MAINCK / 1000 * 16) + (mainf / 2)) / mainf) & AT91C_RTTC_RTPRES);
|
||||
// note: worst case precision is approx 2.5%
|
||||
}
|
||||
|
||||
// Get the current count.
|
||||
uint32_t RAMFUNC GetTickCount(void) {
|
||||
return AT91C_BASE_RTTC->RTTC_RTVR;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer for iso14443 commands. Uses ssp_clk from FPGA
|
||||
// -------------------------------------------------------------------------
|
||||
void StartCountSspClk(void) {
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1) | (1 << AT91C_ID_TC2); // Enable Clock to all timers
|
||||
AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_TIOA1 // XC0 Clock = TIOA1
|
||||
| AT91C_TCB_TC1XC1S_NONE // XC1 Clock = none
|
||||
| AT91C_TCB_TC2XC2S_TIOA0; // XC2 Clock = TIOA0
|
||||
|
||||
// configure TC1 to create a short pulse on TIOA1 when a rising edge on TIOB1 (= ssp_clk from FPGA) occurs:
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // disable TC1
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK // TC1 Clock = MCK(48MHz)/2 = 24MHz
|
||||
| AT91C_TC_CPCSTOP // Stop clock on RC compare
|
||||
| AT91C_TC_EEVTEDG_RISING // Trigger on rising edge of Event
|
||||
| AT91C_TC_EEVT_TIOB // Event-Source: TIOB1 (= ssp_clk from FPGA = 13,56MHz/16)
|
||||
| AT91C_TC_ENETRG // Enable external trigger event
|
||||
| AT91C_TC_WAVESEL_UP // Upmode without automatic trigger on RC compare
|
||||
| AT91C_TC_WAVE // Waveform Mode
|
||||
| AT91C_TC_AEEVT_SET // Set TIOA1 on external event
|
||||
| AT91C_TC_ACPC_CLEAR; // Clear TIOA1 on RC Compare
|
||||
AT91C_BASE_TC1->TC_RC = 0x01; // RC Compare value = 0x01, pulse width to TC0
|
||||
|
||||
// use TC0 to count TIOA1 pulses
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // disable TC0
|
||||
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_XC0 // TC0 clock = XC0 clock = TIOA1
|
||||
| AT91C_TC_WAVE // Waveform Mode
|
||||
| AT91C_TC_WAVESEL_UP // just count
|
||||
| AT91C_TC_ACPA_CLEAR // Clear TIOA0 on RA Compare
|
||||
| AT91C_TC_ACPC_SET // Set TIOA0 on RC Compare
|
||||
| AT91C_TC_ASWTRG_SET; // Set TIOA0 on software trigger to trigger instant reset of TC2
|
||||
AT91C_BASE_TC0->TC_RA = 1; // RA Compare value = 1; pulse width to TC2
|
||||
AT91C_BASE_TC0->TC_RC = 0; // RC Compare value = 0; increment TC2 on overflow
|
||||
|
||||
// use TC2 to count TIOA0 pulses (giving us a 32bit counter (TC0/TC2) clocked by ssp_clk)
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS; // disable TC2
|
||||
AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_XC2 // TC2 clock = XC2 clock = TIOA0
|
||||
| AT91C_TC_WAVE // Waveform Mode
|
||||
| AT91C_TC_WAVESEL_UP; // just count
|
||||
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC0
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC1
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC2
|
||||
|
||||
//
|
||||
// synchronize the counter with the ssp_frame signal.
|
||||
// Note: FPGA must be in a FPGA mode with SSC transfer, otherwise SSC_FRAME and SSC_CLK signals would not be present
|
||||
//
|
||||
while (Gpio_SSC_FRAME_Read()); // wait for ssp_frame to be low
|
||||
while (!(Gpio_SSC_FRAME_Read())); // wait for ssp_frame to go high (start of frame)
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 1st ssp_clk after start of frame
|
||||
while (Gpio_SSC_CLK_Read()); // wait for ssp_clk to go low;
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 2nd ssp_clk after start of frame
|
||||
if ((AT91C_BASE_SSC->SSC_RFMR & SSC_FRAME_MODE_BITS_IN_WORD(32)) == SSC_FRAME_MODE_BITS_IN_WORD(16)) { // 16bit frame
|
||||
while (Gpio_SSC_CLK_Read()); // wait for ssp_clk to go low;
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 3rd ssp_clk after start of frame
|
||||
while (Gpio_SSC_CLK_Read()); // wait for ssp_clk to go low;
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 4th ssp_clk after start of frame
|
||||
while (Gpio_SSC_CLK_Read()); // wait for ssp_clk to go low;
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 5th ssp_clk after start of frame
|
||||
while (Gpio_SSC_CLK_Read()); // wait for ssp_clk to go low;
|
||||
while (!(Gpio_SSC_CLK_Read())); // wait for ssp_clk to go high; 6th ssp_clk after start of frame
|
||||
}
|
||||
|
||||
// note: up to now two ssp_clk rising edges have passed since the rising edge of ssp_frame
|
||||
// it is now safe to assert a sync signal. This sets all timers to 0 on next active clock edge
|
||||
AT91C_BASE_TCB->TCB_BCR = 1; // assert Sync (set all timers to 0 on next active clock edge)
|
||||
// at the next (3rd) ssp_clk rising edge, TC1 will be reset (and not generate a clock signal to TC0)
|
||||
// at the next (4th) ssp_clk rising edge, TC0 (the low word of our counter) will be reset. From now on,
|
||||
// whenever the last three bits of our counter go 0, we can be sure to be in the middle of a frame transfer.
|
||||
// (just started with the transfer of the 4th Bit).
|
||||
|
||||
// The high word of the counter (TC2) will not reset until the low word (TC0) clocks to process the external trigger.
|
||||
// Therefore may need to wait a little bit before we can use the counter.
|
||||
while (AT91C_BASE_TC2->TC_CV > 0);
|
||||
}
|
||||
|
||||
void ResetSspClk(void) {
|
||||
//enable clock of timer and software trigger
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
while (AT91C_BASE_TC2->TC_CV > 0);
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetCountSspClk(void) {
|
||||
uint32_t tmp_count = (AT91C_BASE_TC2->TC_CV << 16) | AT91C_BASE_TC0->TC_CV;
|
||||
|
||||
// small chance that we may have missed an increment in TC2
|
||||
if ((tmp_count & 0x0000ffff) == 0) {
|
||||
return (AT91C_BASE_TC2->TC_CV << 16);
|
||||
}
|
||||
return tmp_count;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Precision counter (TC0), input capture (TC1) and timestamp (TC2).
|
||||
// These are used by the LF protocols (e.g. Hitag) and are configured at
|
||||
// 1.5 MHz (MCK/32), so 12 counts = 1 T0 = 8 us.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// TC2 overflow count, combined with the TC2 counter for ~47 min timing.
|
||||
static uint16_t timestamp_high = 0;
|
||||
|
||||
void StartPrecisionCounter(void) {
|
||||
// Enable peripheral clock for TC0 (precision counter).
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0);
|
||||
|
||||
// Disable TC0 before reconfiguration.
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
// TC0: capture mode, default timer source = MCK/32 (TIMER_CLOCK3), no triggers (free-running).
|
||||
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
while (AT91C_BASE_TC0->TC_CV != 0) {}; // wait until the reset takes effect
|
||||
}
|
||||
|
||||
void StopPrecisionCounter(void) {
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
}
|
||||
|
||||
void ResetPrecisionCounter(void) {
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
|
||||
while (AT91C_BASE_TC0->TC_CV != 0) {};
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetPrecisionCounter(void) {
|
||||
return (uint16_t)AT91C_BASE_TC0->TC_CV;
|
||||
}
|
||||
|
||||
void StartLoEdgeCapture(void) {
|
||||
// Enable peripheral clock for TC1 (input capture).
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC1);
|
||||
|
||||
// Route SSC_FRAME to the timer input (TIOA) so its edges can be captured by TC1.
|
||||
AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
|
||||
|
||||
// Disable TC1 before reconfiguration.
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
// TC1: capture mode, default timer source = MCK/32 (TIMER_CLOCK3),
|
||||
// TIOA is external trigger, load RA on rising edge, load RB on falling edge.
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK // use MCK/32 (TIMER_CLOCK3)
|
||||
| AT91C_TC_ABETRG // TIOA is used as an external trigger
|
||||
| AT91C_TC_ETRGEDG_FALLING // external trigger on falling edge
|
||||
| AT91C_TC_LDRA_RISING // load RA on rising edge of TIOA
|
||||
| AT91C_TC_LDRB_FALLING; // load RB on falling edge of TIOA
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
while (AT91C_BASE_TC1->TC_CV != 0) {}; // wait until the reset takes effect
|
||||
}
|
||||
|
||||
void StopLoEdgeCapture(void) {
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
}
|
||||
|
||||
void EnableLoEdgeCapture(void) {
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
}
|
||||
|
||||
void ResetLoEdgeCapture(void) {
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG;
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureCount(void) {
|
||||
return (uint16_t)AT91C_BASE_TC1->TC_CV;
|
||||
}
|
||||
|
||||
lo_edge_t RAMFUNC GetLoEdgeCaptureStatus(void) {
|
||||
if (AT91C_BASE_TC1->TC_SR & INPUT_CAPTURE_EVT_RISING_EDGE) {
|
||||
return LO_EDGE_RISING;
|
||||
}
|
||||
if (AT91C_BASE_TC1->TC_SR & INPUT_CAPTURE_EVT_FALLING_EDGE) {
|
||||
return LO_EDGE_FALLING;
|
||||
}
|
||||
return LO_EDGE_NO;
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureFalling(void) {
|
||||
return (uint16_t)AT91C_BASE_TC1->TC_RB;
|
||||
}
|
||||
|
||||
uint16_t RAMFUNC GetLoEdgeCaptureRising(void) {
|
||||
return (uint16_t)AT91C_BASE_TC1->TC_RA;
|
||||
}
|
||||
|
||||
void StartTimestamp(void) {
|
||||
// Enable peripheral clock for TC2 (timestamp).
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC2);
|
||||
|
||||
// Disable TC2 before reconfiguration.
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
// TC2: capture mode, default timer source = MCK/32 (TIMER_CLOCK3), no triggers (free-running).
|
||||
AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
while (AT91C_BASE_TC2->TC_CV != 0) {}; // wait until the reset takes effect
|
||||
|
||||
// Reset the overflow accumulator.
|
||||
timestamp_high = 0;
|
||||
}
|
||||
|
||||
void StopTimestamp(void) {
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetTimestamp(void) {
|
||||
// Reading TC_SR clears the COVFS overflow flag.
|
||||
if (AT91C_BASE_TC2->TC_SR & AT91C_TC_COVFS) {
|
||||
timestamp_high++;
|
||||
}
|
||||
return (((uint32_t)timestamp_high << 16) + AT91C_BASE_TC2->TC_CV) / TICKS_PER_CARRIER_PERIOD;
|
||||
}
|
||||
|
||||
#endif // #ifndef AS_BOOTROM
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// microseconds timer
|
||||
// 1us = 1tick
|
||||
// -------------------------------------------------------------------------
|
||||
void StartCountUS(void) {
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
|
||||
AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
|
||||
|
||||
// fast clock
|
||||
// tick=1.5mks
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
|
||||
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz) / 32
|
||||
AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR |
|
||||
AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET;
|
||||
AT91C_BASE_TC0->TC_RA = 1;
|
||||
AT91C_BASE_TC0->TC_RC = 0xBFFF + 1; // 0xC000
|
||||
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // timer disable
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // from timer 0
|
||||
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
|
||||
// Assert a sync signal. This sets all timers to 0 on next active clock edge
|
||||
AT91C_BASE_TCB->TCB_BCR = 1;
|
||||
|
||||
while (AT91C_BASE_TC1->TC_CV > 0);
|
||||
}
|
||||
|
||||
uint32_t RAMFUNC GetCountUS(void) {
|
||||
//return (AT91C_BASE_TC1->TC_CV * 0x8000) + ((AT91C_BASE_TC0->TC_CV / 15) * 10);
|
||||
// By suggestion from PwPiwi, http://www.proxmark.org/forum/viewtopic.php?pid=17548#p17548
|
||||
return ((uint32_t)AT91C_BASE_TC1->TC_CV) * 0x8000 + (((uint32_t)AT91C_BASE_TC0->TC_CV) * 2) / 3;
|
||||
}
|
||||
|
||||
// Maybe we can make it a static inline function, but to avoid possible compiler quirks,
|
||||
// it's best not to do so, otherwise it may increase the time wasted on stack entry and exit due to not expanding the inline function,
|
||||
// leading to synchronization zeroing failure!
|
||||
#define WaitSyncTicks() \
|
||||
/* synchronized startup procedure */ \
|
||||
while (AT91C_BASE_TC0->TC_CV > 0); /* wait until TC0 returned to zero */ \
|
||||
while (AT91C_BASE_TC0->TC_CV < 2); /* and has started (TC_CV > TC_RA, now TC1 is cleared) */ \
|
||||
/* return to zero */ \
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; \
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG; \
|
||||
while (AT91C_BASE_TC0->TC_CV > 0);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timer for bitbanging, or LF stuff when you need a very precise timer
|
||||
// 1us = 1.5ticks
|
||||
// -------------------------------------------------------------------------
|
||||
void StartTicks(void) {
|
||||
// initialization of the timer
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
|
||||
AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
|
||||
|
||||
// disable TC0 and TC1 for re-configuration
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
// first configure TC1 (higher, 0xFFFF0000) 16 bit counter
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // just connect to TIOA0 from TC0
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // re-enable timer and wait for TC0
|
||||
|
||||
// second configure TC0 (lower, 0x0000FFFF) 16 bit counter
|
||||
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz) / 32
|
||||
AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO |
|
||||
AT91C_TC_ACPA_CLEAR | // RA comperator clears TIOA (carry bit)
|
||||
AT91C_TC_ACPC_SET | // RC comperator sets TIOA (carry bit)
|
||||
AT91C_TC_ASWTRG_SET; // SWTriger sets TIOA (carry bit)
|
||||
AT91C_BASE_TC0->TC_RC = 0; // set TIOA (carry bit) on overflow, return to zero
|
||||
AT91C_BASE_TC0->TC_RA = 1; // clear carry bit on next clock cycle
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // reset and re-enable timer
|
||||
|
||||
WaitSyncTicks();
|
||||
}
|
||||
|
||||
// Reset the count value to 0 for TC0 & TC1
|
||||
void ResetTicks(void) {
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
|
||||
WaitSyncTicks();
|
||||
}
|
||||
|
||||
// stop clock
|
||||
void StopTicks(void) {
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS; // TODO StartTicks() did not use TC2, is this code worthless?
|
||||
}
|
||||
|
||||
uint32_t GetTicks(void) {
|
||||
uint32_t hi, lo;
|
||||
|
||||
do {
|
||||
hi = AT91C_BASE_TC1->TC_CV;
|
||||
lo = AT91C_BASE_TC0->TC_CV;
|
||||
} while (hi != AT91C_BASE_TC1->TC_CV);
|
||||
|
||||
return (hi << 16) | lo;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Timers / Clocks HAL: AT91 (SAM7S) hardware definitions.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef TICKS_HW_AT91_H
|
||||
#define TICKS_HW_AT91_H
|
||||
|
||||
#include "at91sam7s512.h"
|
||||
|
||||
// Input capture edge-event flags (single-bit masks in the TC1 status register).
|
||||
// On AT91, reading TC_SR clears these flags automatically.
|
||||
#define INPUT_CAPTURE_EVT_RISING_EDGE AT91C_TC_LDRAS // rising-edge load (RA) event
|
||||
#define INPUT_CAPTURE_EVT_FALLING_EDGE AT91C_TC_LDRBS // falling-edge load (RB) event
|
||||
|
||||
#endif // TICKS_HW_AT91_H
|
||||
Reference in new issue
Block a user