2010-02-21 05:57:20 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-01-06 09:19:46 +08:00
|
|
|
// Copyright (C) Jonathan Westhues, Sept 2005
|
|
|
|
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
2010-02-21 08:12:52 +08:00
|
|
|
//
|
2022-01-06 09:19:46 +08:00
|
|
|
// 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.
|
2010-02-21 08:12:52 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Utility functions used in many places, not specific to any piece of code.
|
2010-02-21 05:57:20 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-02-21 06:51:00 +08:00
|
|
|
#include "util.h"
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "proxmark3_arm.h"
|
|
|
|
#include "ticks.h"
|
|
|
|
#include "commonutil.h"
|
|
|
|
#include "dbprint.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "usb_cdc.h"
|
|
|
|
#include "usart.h"
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
size_t nbytes(size_t nbits) {
|
2019-03-10 07:00:59 +08:00
|
|
|
return (nbits >> 3) + ((nbits % 8) > 0);
|
2013-04-03 16:45:04 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 04:09:36 +08:00
|
|
|
//convert hex digit to integer
|
2019-03-10 18:20:22 +08:00
|
|
|
uint8_t hex2int(char hexchar) {
|
2019-03-10 07:00:59 +08:00
|
|
|
switch (hexchar) {
|
|
|
|
case '0':
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
return 2;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
return 3;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
return 4;
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
return 5;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
return 6;
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
return 7;
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
return 8;
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
return 9;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'a':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'A':
|
|
|
|
return 10;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'b':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'B':
|
|
|
|
return 11;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'c':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'C':
|
|
|
|
return 12;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'd':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'D':
|
|
|
|
return 13;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'e':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'E':
|
|
|
|
return 14;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
case 'f':
|
2019-03-10 07:00:59 +08:00
|
|
|
case 'F':
|
|
|
|
return 15;
|
|
|
|
break;
|
2017-02-06 04:09:36 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
void LEDsoff(void) {
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_A_OFF();
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_C_OFF();
|
|
|
|
LED_D_OFF();
|
2010-02-21 05:57:20 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 22:27:25 +08:00
|
|
|
//ICEMAN: LED went from 1,2,3,4 -> 1,2,4,8
|
2019-03-10 18:20:22 +08:00
|
|
|
void LED(int led, int ms) {
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_A) // Proxmark3 historical mapping: LED_ORANGE
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_A_ON();
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_B) // Proxmark3 historical mapping: LED_GREEN
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_B_ON();
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_C) // Proxmark3 historical mapping: LED_RED
|
|
|
|
LED_C_ON();
|
|
|
|
if (led & LED_D) // Proxmark3 historical mapping: LED_RED2
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_D_ON();
|
|
|
|
|
|
|
|
if (!ms)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SpinDelay(ms);
|
|
|
|
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_A)
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_A_OFF();
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_B)
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_B_OFF();
|
2019-06-02 17:47:10 +08:00
|
|
|
if (led & LED_C)
|
|
|
|
LED_C_OFF();
|
|
|
|
if (led & LED_D)
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_D_OFF();
|
2010-02-21 05:57:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SpinOff(uint32_t pause) {
|
2018-11-07 05:20:55 +08:00
|
|
|
LED_A_OFF();
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_C_OFF();
|
|
|
|
LED_D_OFF();
|
|
|
|
SpinDelay(pause);
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:58:05 +08:00
|
|
|
// Blinks..
|
|
|
|
// A = 1, B = 2, C = 4, D = 8
|
2019-03-10 18:20:22 +08:00
|
|
|
void SpinErr(uint8_t led, uint32_t speed, uint8_t times) {
|
2018-11-07 05:20:55 +08:00
|
|
|
SpinOff(speed);
|
2019-03-10 07:00:59 +08:00
|
|
|
NTIME(times) {
|
2019-09-12 15:58:05 +08:00
|
|
|
|
|
|
|
if (led & LED_A) // Proxmark3 historical mapping: LED_ORANGE
|
|
|
|
LED_A_INV();
|
|
|
|
if (led & LED_B) // Proxmark3 historical mapping: LED_GREEN
|
|
|
|
LED_B_INV();
|
|
|
|
if (led & LED_C) // Proxmark3 historical mapping: LED_RED
|
|
|
|
LED_C_INV();
|
|
|
|
if (led & LED_D) // Proxmark3 historical mapping: LED_RED2
|
|
|
|
LED_D_INV();
|
|
|
|
|
2018-11-07 05:20:55 +08:00
|
|
|
SpinDelay(speed);
|
|
|
|
}
|
2019-09-12 15:58:05 +08:00
|
|
|
LED_A_OFF();
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_C_OFF();
|
|
|
|
LED_D_OFF();
|
2018-11-07 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SpinDown(uint32_t speed) {
|
2018-11-07 05:20:55 +08:00
|
|
|
SpinOff(speed);
|
|
|
|
LED_D_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_D_OFF();
|
|
|
|
LED_C_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_C_OFF();
|
|
|
|
LED_B_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_A_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_A_OFF();
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SpinUp(uint32_t speed) {
|
2018-11-07 05:20:55 +08:00
|
|
|
SpinOff(speed);
|
|
|
|
LED_A_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_A_OFF();
|
|
|
|
LED_B_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_C_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_C_OFF();
|
|
|
|
LED_D_ON();
|
|
|
|
SpinDelay(speed);
|
|
|
|
LED_D_OFF();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-21 05:57:20 +08:00
|
|
|
// Determine if a button is double clicked, single clicked,
|
|
|
|
// not clicked, or held down (for ms || 1sec)
|
|
|
|
// In general, don't use this function unless you expect a
|
|
|
|
// double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
|
2019-03-10 18:20:22 +08:00
|
|
|
int BUTTON_CLICKED(int ms) {
|
2019-03-10 03:34:41 +08:00
|
|
|
// Up to 500ms in between clicks to mean a double click
|
2019-08-06 19:51:10 +08:00
|
|
|
// timer counts in 21.3us increments (1024/48MHz)
|
|
|
|
// WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
|
2019-08-06 19:09:48 +08:00
|
|
|
if (ms > 1390) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel >= DBG_ERROR) Dbprintf(_RED_("Error, BUTTON_CLICKED called with %i > 1390"), ms);
|
2019-08-06 19:09:48 +08:00
|
|
|
ms = 1390;
|
|
|
|
}
|
2019-08-06 19:40:08 +08:00
|
|
|
int ticks = ((MCK / 1000) * (ms ? ms : 1000)) >> 10;
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
// If we're not even pressed, forget about it!
|
2022-01-07 02:41:45 +08:00
|
|
|
if (BUTTON_PRESS() == false)
|
2019-03-10 03:34:41 +08:00
|
|
|
return BUTTON_NO_CLICK;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
|
|
|
|
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
|
|
|
|
|
|
|
|
uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
|
|
|
|
|
|
|
int letoff = 0;
|
2019-03-10 07:00:59 +08:00
|
|
|
for (;;) {
|
2019-03-10 03:34:41 +08:00
|
|
|
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
|
|
|
|
|
|
|
// We haven't let off the button yet
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!letoff) {
|
2019-03-10 03:34:41 +08:00
|
|
|
// We just let it off!
|
2022-01-07 02:41:45 +08:00
|
|
|
if (BUTTON_PRESS() == false) {
|
2019-03-10 03:34:41 +08:00
|
|
|
letoff = 1;
|
|
|
|
|
|
|
|
// reset our timer for 500ms
|
|
|
|
start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
2019-08-06 19:40:08 +08:00
|
|
|
ticks = ((MCK / 1000) * (500)) >> 10;
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Still haven't let it off
|
|
|
|
else
|
|
|
|
// Have we held down a full second?
|
2019-08-07 05:10:02 +08:00
|
|
|
if (now == (uint16_t)(start + ticks))
|
2019-03-10 03:34:41 +08:00
|
|
|
return BUTTON_HOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already let off, did we click again?
|
|
|
|
else
|
|
|
|
// Sweet, double click!
|
|
|
|
if (BUTTON_PRESS())
|
|
|
|
return BUTTON_DOUBLE_CLICK;
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
// Have we ran out of time to double click?
|
2019-08-07 05:10:02 +08:00
|
|
|
else if (now == (uint16_t)(start + ticks))
|
2019-03-10 07:00:59 +08:00
|
|
|
// At least we did a single click
|
|
|
|
return BUTTON_SINGLE_CLICK;
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
WDT_HIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should never get here
|
|
|
|
return BUTTON_ERROR;
|
2010-02-21 05:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if a button is held down
|
2019-03-10 18:20:22 +08:00
|
|
|
int BUTTON_HELD(int ms) {
|
2019-08-06 19:51:10 +08:00
|
|
|
// timer counts in 21.3us increments (1024/48MHz)
|
|
|
|
// WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
|
2019-08-06 19:09:48 +08:00
|
|
|
if (ms > 1390) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel >= DBG_ERROR) Dbprintf(_RED_("Error, BUTTON_HELD called with %i > 1390"), ms);
|
2019-08-06 19:09:48 +08:00
|
|
|
ms = 1390;
|
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
// If button is held for one second
|
|
|
|
int ticks = (48000 * (ms ? ms : 1000)) >> 10;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// If we're not even pressed, forget about it!
|
2022-01-07 02:41:45 +08:00
|
|
|
if (BUTTON_PRESS() == false)
|
2019-03-10 03:34:41 +08:00
|
|
|
return BUTTON_NO_CLICK;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// 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);
|
|
|
|
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
|
|
|
|
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
for (;;) {
|
2019-03-10 03:34:41 +08:00
|
|
|
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// As soon as our button let go, we didn't hold long enough
|
2022-01-07 02:41:45 +08:00
|
|
|
if (BUTTON_PRESS() == false)
|
2019-03-10 03:34:41 +08:00
|
|
|
return BUTTON_SINGLE_CLICK;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// Have we waited the full second?
|
2019-08-07 05:10:02 +08:00
|
|
|
else if (now == (uint16_t)(start + ticks))
|
2019-03-10 07:00:59 +08:00
|
|
|
return BUTTON_HOLD;
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
WDT_HIT();
|
|
|
|
}
|
2010-02-21 05:57:20 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// We should never get here
|
|
|
|
return BUTTON_ERROR;
|
2010-02-21 05:57:20 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 18:04:52 +08:00
|
|
|
// This function returns false if no data is available or
|
|
|
|
// the USB connection is invalid.
|
2019-06-03 06:01:08 +08:00
|
|
|
bool data_available(void) {
|
|
|
|
#ifdef WITH_FPC_USART_HOST
|
|
|
|
return usb_poll_validate_length() || (usart_rxdata_available() > 0);
|
|
|
|
#else
|
|
|
|
return usb_poll_validate_length();
|
|
|
|
#endif
|
|
|
|
}
|
2023-11-14 01:40:31 +08:00
|
|
|
|
2023-11-15 18:04:52 +08:00
|
|
|
// This function doesn't check if the USB connection is valid.
|
|
|
|
// In most of the cases, you should use data_available() unless
|
|
|
|
// the timing is critical.
|
2023-11-14 01:40:31 +08:00
|
|
|
bool data_available_fast(void) {
|
|
|
|
#ifdef WITH_FPC_USART_HOST
|
|
|
|
return usb_available_length() || (usart_rxdata_available() > 0);
|
|
|
|
#else
|
|
|
|
return usb_available_length();
|
|
|
|
#endif
|
|
|
|
}
|