From 8b3159c83d4b96ae0dbf3520abfe4f75d2971c8a Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 6 Aug 2019 13:40:08 +0200 Subject: [PATCH] Use more defines when using main clock or master clock --- armsrc/flashmem.h | 1 - armsrc/ticks.c | 4 ++-- armsrc/util.c | 4 ++-- common/usart.c | 4 ++-- common/usb_cdc.c | 2 +- include/proxmark3.h | 4 ++++ 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/armsrc/flashmem.h b/armsrc/flashmem.h index 26d2d974d..b332ec3f5 100644 --- a/armsrc/flashmem.h +++ b/armsrc/flashmem.h @@ -102,7 +102,6 @@ #define MAX_BLOCKS 4 #define MAX_SECTORS 16 -#define MCK 48000000 //#define FLASH_BAUD 24000000 #define FLASH_MINFAST 24000000 //33000000 #define FLASH_BAUD MCK/2 diff --git a/armsrc/ticks.c b/armsrc/ticks.c index ac76f99e3..23e9e3d1d 100644 --- a/armsrc/ticks.c +++ b/armsrc/ticks.c @@ -13,7 +13,7 @@ // 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 + 512) >> 10; + int ticks = ((MCK / 1000000) * us + 512) >> 10; // Borrow a PWM unit for my real-time clock // This resets PWMC_CPRDR as well @@ -59,7 +59,7 @@ void StartTickCount(void) { 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); + 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% } diff --git a/armsrc/util.c b/armsrc/util.c index 3deb97b6d..42d936450 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -182,7 +182,7 @@ int BUTTON_CLICKED(int ms) { Dbprintf(_RED_("Error, BUTTON_CLICKED called with %i > 1390"), ms); ms = 1390; } - int ticks = (48000 * (ms ? ms : 1000)) >> 10; + int ticks = ((MCK / 1000) * (ms ? ms : 1000)) >> 10; // If we're not even pressed, forget about it! if (!BUTTON_PRESS()) @@ -210,7 +210,7 @@ int BUTTON_CLICKED(int ms) { // reset our timer for 500ms start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; - ticks = (48000 * (500)) >> 10; + ticks = ((MCK / 1000) * (500)) >> 10; } // Still haven't let it off diff --git a/common/usart.c b/common/usart.c index ced1e5e6b..cb52d0116 100644 --- a/common/usart.c +++ b/common/usart.c @@ -229,9 +229,9 @@ void usart_init(uint32_t baudrate, uint8_t parity) { // OVER = 1, -yes we are oversampling // baudrate == selected clock/8/CD --> this is ours // - uint32_t brgr = 48000000 / (usart_baudrate << 3); + uint32_t brgr = MCK / (usart_baudrate << 3); // doing fp = round((mck / (usart_baudrate << 3) - brgr) * 8) with integers: - uint32_t fp = ((16 * 48000000 / (usart_baudrate << 3) - 16 * brgr) + 1) / 2; + uint32_t fp = ((16 * MCK / (usart_baudrate << 3) - 16 * brgr) + 1) / 2; pUS1->US_BRGR = (fp << 16) | brgr; diff --git a/common/usb_cdc.c b/common/usb_cdc.c index 073fa80f2..7f8deb62d 100644 --- a/common/usb_cdc.c +++ b/common/usb_cdc.c @@ -450,7 +450,7 @@ AT91S_CDC_LINE_CODING line = { // purely informative, actual values don't matter // 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 + 512) >> 10; + int ticks = ((MCK / 1000000) * us + 512) >> 10; // Borrow a PWM unit for my real-time clock AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0); diff --git a/include/proxmark3.h b/include/proxmark3.h index 469713b5b..ddda6826a 100644 --- a/include/proxmark3.h +++ b/include/proxmark3.h @@ -17,6 +17,10 @@ #include "pm3_cmd.h" #include "common.h" +// Check bootrom.c for actual clock settings +#define MAINCK 16000000 +#define MCK (4 * MAINCK) + #define WDT_HIT() AT91C_BASE_WDTC->WDTC_WDCR = 0xa5000001 #define PWM_CH_MODE_PRESCALER(x) ((x) << 0)