mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 09:28:13 +00:00
timer: Use Cortex-M3 SysTick timers
Removed `usbDelay(uint32_t cycles)`, added `usbSleep(uint32_t millis)` The same method signature could cause silent code breakage at runtime, as opposed to noisy code breakage at compile time which is the better kind.
This commit is contained in:
parent
3cede26fbc
commit
25b9bfd97b
1
Makefile
1
Makefile
@ -6,6 +6,7 @@ OBJS += serialno.o
|
||||
OBJS += setup.o
|
||||
OBJS += util.o
|
||||
OBJS += memory.o
|
||||
OBJS += timer.o
|
||||
OBJS += gen/bitmaps.o
|
||||
OBJS += gen/fonts.o
|
||||
|
||||
|
@ -58,7 +58,7 @@ bool protectButton(ButtonRequestType type, bool confirm_only)
|
||||
|
||||
// button acked - check buttons
|
||||
if (acked) {
|
||||
usbDelay(3300);
|
||||
usbSleep(5);
|
||||
buttonUpdate();
|
||||
if (button.YesUp) {
|
||||
result = true;
|
||||
@ -165,7 +165,7 @@ bool protectPin(bool use_cached)
|
||||
}
|
||||
layoutDialog(&bmp_icon_info, NULL, NULL, NULL, "Wrong PIN entered", NULL, "Please wait", secstr, "to continue ...", NULL);
|
||||
// wait one second
|
||||
usbDelay(800000);
|
||||
usbSleep(1000);
|
||||
if (msg_tiny_id == MessageType_MessageType_Initialize) {
|
||||
protectAbortedByInitialize = true;
|
||||
msg_tiny_id = 0xFFFF;
|
||||
|
@ -338,7 +338,7 @@ void storage_setHomescreen(const uint8_t *data, uint32_t size)
|
||||
|
||||
void get_root_node_callback(uint32_t iter, uint32_t total)
|
||||
{
|
||||
usbDelay(10); // handle up to ten usb interrupts.
|
||||
usbSleep(1);
|
||||
layoutProgress("Waking up", 1000 * iter / total);
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "layout.h"
|
||||
#include "layout2.h"
|
||||
#include "rng.h"
|
||||
#include "timer.h"
|
||||
#include "buttons.h"
|
||||
|
||||
uint32_t __stack_chk_guard;
|
||||
@ -58,13 +59,13 @@ void check_lock_screen(void)
|
||||
// wait until NoButton is released
|
||||
usbTiny(1);
|
||||
do {
|
||||
usbDelay(3300);
|
||||
usbSleep(5);
|
||||
buttonUpdate();
|
||||
} while (!button.NoUp);
|
||||
|
||||
// wait for confirmation/cancellation of the dialog
|
||||
do {
|
||||
usbDelay(3300);
|
||||
usbSleep(5);
|
||||
buttonUpdate();
|
||||
} while (!button.YesUp && !button.NoUp);
|
||||
usbTiny(0);
|
||||
@ -102,6 +103,9 @@ int main(void)
|
||||
#else
|
||||
setupApp();
|
||||
#endif
|
||||
|
||||
timer_init();
|
||||
|
||||
#if DEBUG_LINK
|
||||
oledSetDebug(1);
|
||||
storage_reset(); // wipe storage if debug link
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "u2f.h"
|
||||
#include "storage.h"
|
||||
#include "util.h"
|
||||
#include "timer.h"
|
||||
|
||||
#define USB_INTERFACE_INDEX_MAIN 0
|
||||
#if DEBUG_LINK
|
||||
@ -426,9 +427,10 @@ char usbTiny(char set)
|
||||
return old;
|
||||
}
|
||||
|
||||
void usbDelay(int cycles)
|
||||
void usbSleep(uint32_t millis)
|
||||
{
|
||||
while (cycles--) {
|
||||
uint32_t end = system_millis + millis;
|
||||
while (end > system_millis) {
|
||||
usbd_poll(usbd_dev);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,6 @@ void usbInit(void);
|
||||
void usbPoll(void);
|
||||
void usbReconnect(void);
|
||||
char usbTiny(char set);
|
||||
void usbDelay(int cycles);
|
||||
void usbSleep(uint32_t millis);
|
||||
|
||||
#endif
|
||||
|
61
timer.c
Normal file
61
timer.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* This file is part of the TREZOR project.
|
||||
*
|
||||
* Copyright (C) 2016 Saleem Rashid <trezor@saleemrashid.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
|
||||
/* 1 tick = 1 ms */
|
||||
volatile uint32_t system_millis;
|
||||
|
||||
/*
|
||||
* Initialise the Cortex-M3 SysTick timer
|
||||
*/
|
||||
void timer_init(void) {
|
||||
system_millis = 0;
|
||||
|
||||
/*
|
||||
* MCU clock (120 MHz) as source
|
||||
*
|
||||
* (120 MHz / 8) = 15 clock pulses
|
||||
*
|
||||
*/
|
||||
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
|
||||
STK_CVR = 0;
|
||||
|
||||
/*
|
||||
* 1 tick = 1 ms @ 120 MHz
|
||||
*
|
||||
* (15 clock pulses * 1000 ms) = 15000 clock pulses
|
||||
*
|
||||
* Send an interrupt every (N - 1) clock pulses
|
||||
*/
|
||||
systick_set_reload(14999);
|
||||
|
||||
/* SysTick as interrupt */
|
||||
systick_interrupt_enable();
|
||||
|
||||
systick_counter_enable();
|
||||
}
|
||||
|
||||
void sys_tick_handler(void) {
|
||||
system_millis++;
|
||||
}
|
28
timer.h
Normal file
28
timer.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* This file is part of the TREZOR project.
|
||||
*
|
||||
* Copyright (C) 2016 Saleem Rashid <trezor@saleemrashid.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* 1 tick = 1 ms */
|
||||
extern volatile uint32_t system_millis;
|
||||
|
||||
void timer_init(void);
|
||||
|
||||
void sys_tick_handler(void);
|
Loading…
Reference in New Issue
Block a user