forked from trezor/trezor-mcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervise.h
82 lines (70 loc) · 2.58 KB
/
supervise.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (C) 2018 Jochen Hoenicke <[email protected]>
*
* 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/>.
*/
#ifndef __SUPERVISE_H__
#define __SUPERVISE_H__
#if !EMULATOR
#define SVC_FLASH_UNLOCK 0
#define SVC_FLASH_ERASE 1
#define SVC_FLASH_PROGRAM 2
#define SVC_FLASH_LOCK 3
#define SVC_TIMER_MS 4
/* Unlocks flash. This function needs to be called before programming
* or erasing. Multiple calls of flash_program and flash_erase can
* follow and should be completed with flash_lock().
*/
inline void svc_flash_unlock(void) {
__asm__ __volatile__ ("svc %0" :: "i" (SVC_FLASH_UNLOCK) : "memory");
}
/* Enable flash write operations.
* @param program_size (8-bit, 16-bit, 32-bit or 64-bit)
* should be one of the FLASH_CR_PROGRAM_X.. constants
*/
inline void svc_flash_program(uint32_t program_size) {
register uint32_t r0 __asm__("r0") = program_size;
__asm__ __volatile__ ("svc %0" :: "i" (SVC_FLASH_PROGRAM), "r" (r0) : "memory");
}
/* Erase a flash sector.
* @param sector sector number 0..11
* (this only allows erasing meta sectors 2 and 3 though).
*/
inline void svc_flash_erase_sector(uint8_t sector) {
register uint32_t r0 __asm__("r0") = sector;
__asm__ __volatile__ ("svc %0" :: "i" (SVC_FLASH_ERASE), "r" (r0) : "memory");
}
/* Lock flash after programming or erasing.
* @return flash status register (FLASH_SR)
*/
inline uint32_t svc_flash_lock(void) {
register uint32_t r0 __asm__("r0");
__asm__ __volatile__ ("svc %1" : "=r" (r0) : "i" (SVC_FLASH_LOCK) : "memory");
return r0;
}
inline uint32_t svc_timer_ms(void) {
register uint32_t r0 __asm__("r0");
__asm__ __volatile__ ("svc %1" : "=r" (r0) : "i" (SVC_TIMER_MS) : "memory");
return r0;
}
#else
extern void svc_flash_unlock(void);
extern void svc_flash_program(uint32_t program_size);
extern void svc_flash_erase_sector(uint16_t sector);
extern uint32_t svc_flash_lock(void);
extern uint32_t svc_timer_ms(void);
#endif
#endif