196
bdk/utils/util.c
196
bdk/utils/util.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2018 naehrwert
|
||||
* Copyright (c) 2018-2020 CTCaer
|
||||
* Copyright (c) 2018-2022 CTCaer
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
@@ -15,7 +15,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <utils/util.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mem/heap.h>
|
||||
#include <power/max77620.h>
|
||||
#include <rtc/max77620-rtc.h>
|
||||
@@ -23,13 +24,13 @@
|
||||
#include <soc/hw_init.h>
|
||||
#include <soc/i2c.h>
|
||||
#include <soc/pmc.h>
|
||||
#include <soc/timer.h>
|
||||
#include <soc/t210.h>
|
||||
#include <storage/nx_sd.h>
|
||||
#include <storage/sd.h>
|
||||
#include <utils/util.h>
|
||||
|
||||
#define USE_RTC_TIMER
|
||||
|
||||
extern volatile nyx_storage_t *nyx_str;
|
||||
|
||||
u8 bit_count(u32 val)
|
||||
{
|
||||
u8 cnt = 0;
|
||||
@@ -51,54 +52,153 @@ u32 bit_count_mask(u8 bits)
|
||||
return val;
|
||||
}
|
||||
|
||||
u32 get_tmr_s()
|
||||
char *strcpy_ns(char *dst, char *src)
|
||||
{
|
||||
return RTC(APBDEV_RTC_SECONDS);
|
||||
if (!src || !dst)
|
||||
return NULL;
|
||||
|
||||
// Remove starting space.
|
||||
u32 len = strlen(src);
|
||||
if (len && src[0] == ' ')
|
||||
{
|
||||
len--;
|
||||
src++;
|
||||
}
|
||||
|
||||
strcpy(dst, src);
|
||||
|
||||
// Remove trailing space.
|
||||
if (len && dst[len - 1] == ' ')
|
||||
dst[len - 1] = 0;
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
u32 get_tmr_ms()
|
||||
// Approximate square root finder for a 64-bit number.
|
||||
u64 sqrt64(u64 num)
|
||||
{
|
||||
// The registers must be read with the following order:
|
||||
// RTC_MILLI_SECONDS (0x10) -> RTC_SHADOW_SECONDS (0xC)
|
||||
return (RTC(APBDEV_RTC_MILLI_SECONDS) + (RTC(APBDEV_RTC_SHADOW_SECONDS) * 1000));
|
||||
u64 base = 0;
|
||||
u64 limit = num;
|
||||
u64 square_root = 0;
|
||||
|
||||
while (base <= limit)
|
||||
{
|
||||
u64 tmp_sqrt = (base + limit) / 2;
|
||||
|
||||
if (tmp_sqrt * tmp_sqrt == num) {
|
||||
square_root = tmp_sqrt;
|
||||
break;
|
||||
}
|
||||
|
||||
if (tmp_sqrt * tmp_sqrt < num)
|
||||
{
|
||||
square_root = base;
|
||||
base = tmp_sqrt + 1;
|
||||
}
|
||||
else
|
||||
limit = tmp_sqrt - 1;
|
||||
}
|
||||
|
||||
return square_root;
|
||||
}
|
||||
|
||||
u32 get_tmr_us()
|
||||
#define TLONG_MAX ((long)(((unsigned long)(~0L)) >> 1))
|
||||
#define TLONG_MIN ((long)(~TLONG_MAX))
|
||||
#define ISSPACE(ch) ((ch >= '\t' && ch <= '\r') || (ch == ' '))
|
||||
#define ISDIGIT(ch) ( ch >= '0' && ch <= '9' )
|
||||
#define ISALPHA(ch) ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
|
||||
#define ISUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
|
||||
|
||||
/*
|
||||
* Avoid using reentrant newlib version of strol. It's only used for errno.
|
||||
*
|
||||
* strol/atoi:
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
*/
|
||||
long strtol(const char *nptr, char **endptr, register int base)
|
||||
{
|
||||
return TMR(TIMERUS_CNTR_1US);
|
||||
register const char *s = nptr;
|
||||
register unsigned long acc;
|
||||
register int c;
|
||||
register unsigned long cutoff;
|
||||
register int neg = 0, any, cutlim;
|
||||
|
||||
/*
|
||||
* Skip white space and pick up leading +/- sign if any.
|
||||
* If base is 0, allow 0x for hex and 0 for octal, else
|
||||
* assume decimal; if base is already 16, allow 0x.
|
||||
*/
|
||||
do {
|
||||
c = *s++;
|
||||
} while (ISSPACE(c));
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else if (c == '+')
|
||||
c = *s++;
|
||||
if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
}
|
||||
if (base == 0)
|
||||
base = c == '0' ? 8 : 10;
|
||||
|
||||
/*
|
||||
* Compute the cutoff value between legal numbers and illegal
|
||||
* numbers. That is the largest legal value, divided by the
|
||||
* base. An input number that is greater than this value, if
|
||||
* followed by a legal input character, is too big. One that
|
||||
* is equal to this value may be valid or not; the limit
|
||||
* between valid and invalid numbers is then based on the last
|
||||
* digit. For instance, if the range for longs is
|
||||
* [-2147483648..2147483647] and the input base is 10,
|
||||
* cutoff will be set to 214748364 and cutlim to either
|
||||
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
||||
* a value > 214748364, or equal but the next digit is > 7 (or 8),
|
||||
* the number is too big, and we will return a range error.
|
||||
*
|
||||
* Set any if any `digits' consumed; make it negative to indicate
|
||||
* overflow.
|
||||
*/
|
||||
cutoff = neg ? -(unsigned long)TLONG_MIN : TLONG_MAX;
|
||||
cutlim = cutoff % (unsigned long)base;
|
||||
cutoff /= (unsigned long)base;
|
||||
for (acc = 0, any = 0;; c = *s++) {
|
||||
if (ISDIGIT(c))
|
||||
c -= '0';
|
||||
else if (ISALPHA(c))
|
||||
c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
|
||||
else
|
||||
break;
|
||||
if (c >= base)
|
||||
break;
|
||||
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
|
||||
any = -1;
|
||||
else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
if (any < 0) {
|
||||
acc = neg ? TLONG_MIN : TLONG_MAX;
|
||||
} else if (neg)
|
||||
acc = -acc;
|
||||
if (endptr != 0)
|
||||
*endptr = (char *) (any ? s - 1 : nptr);
|
||||
return (acc);
|
||||
}
|
||||
|
||||
void msleep(u32 ms)
|
||||
int atoi(const char *nptr)
|
||||
{
|
||||
#ifdef USE_RTC_TIMER
|
||||
u32 start = RTC(APBDEV_RTC_MILLI_SECONDS) + (RTC(APBDEV_RTC_SHADOW_SECONDS) * 1000);
|
||||
// Casting to u32 is important!
|
||||
while (((u32)(RTC(APBDEV_RTC_MILLI_SECONDS) + (RTC(APBDEV_RTC_SHADOW_SECONDS) * 1000)) - start) <= ms)
|
||||
;
|
||||
#else
|
||||
bpmp_msleep(ms);
|
||||
#endif
|
||||
}
|
||||
|
||||
void usleep(u32 us)
|
||||
{
|
||||
#ifdef USE_RTC_TIMER
|
||||
u32 start = TMR(TIMERUS_CNTR_1US);
|
||||
|
||||
// Check if timer is at upper limits and use BPMP sleep so it doesn't wake up immediately.
|
||||
if ((start + us) < start)
|
||||
bpmp_usleep(us);
|
||||
else
|
||||
while ((u32)(TMR(TIMERUS_CNTR_1US) - start) <= us) // Casting to u32 is important!
|
||||
;
|
||||
#else
|
||||
bpmp_usleep(us);
|
||||
#endif
|
||||
return (int)strtol(nptr, (char **)NULL, 10);
|
||||
}
|
||||
|
||||
void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops)
|
||||
{
|
||||
for(u32 i = 0; i < num_ops; i++)
|
||||
for (u32 i = 0; i < num_ops; i++)
|
||||
base[ops[i].off] = ops[i].val;
|
||||
}
|
||||
|
||||
@@ -143,14 +243,14 @@ void panic(u32 val)
|
||||
{
|
||||
// Set panic code.
|
||||
PMC(APBDEV_PMC_SCRATCH200) = val;
|
||||
//PMC(APBDEV_PMC_CRYPTO_OP) = PMC_CRYPTO_OP_SE_DISABLE;
|
||||
TMR(TIMER_WDT4_UNLOCK_PATTERN) = TIMER_MAGIC_PTRN;
|
||||
TMR(TIMER_TMR9_TMR_PTV) = TIMER_EN | TIMER_PER_EN;
|
||||
TMR(TIMER_WDT4_CONFIG) = TIMER_SRC(9) | TIMER_PER(1) | TIMER_PMCRESET_EN;
|
||||
TMR(TIMER_WDT4_COMMAND) = TIMER_START_CNT;
|
||||
|
||||
while (true)
|
||||
usleep(1);
|
||||
// Disable SE.
|
||||
//PMC(APBDEV_PMC_CRYPTO_OP) = PMC_CRYPTO_OP_SE_DISABLE;
|
||||
|
||||
// Immediately cause a full system reset.
|
||||
watchdog_start(0, TIMER_PMCRESET_EN);
|
||||
|
||||
while (true);
|
||||
}
|
||||
|
||||
void power_set_state(power_state_t state)
|
||||
@@ -179,7 +279,7 @@ void power_set_state(power_state_t state)
|
||||
break;
|
||||
|
||||
case POWER_OFF:
|
||||
// Initiate power down sequence and do not generate a reset (regulators retain state).
|
||||
// Initiate power down sequence and do not generate a reset (regulators retain state after POR).
|
||||
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_PWR_OFF);
|
||||
break;
|
||||
|
||||
@@ -194,7 +294,7 @@ void power_set_state(power_state_t state)
|
||||
reg |= MAX77620_ONOFFCNFG2_SFT_RST_WK;
|
||||
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG2, reg);
|
||||
|
||||
// Initiate power down sequence and generate a reset (regulators' state resets).
|
||||
// Initiate power down sequence and generate a reset (regulators' state resets after POR).
|
||||
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_SFT_RST);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user