-
Notifications
You must be signed in to change notification settings - Fork 6
/
STM32.cpp
64 lines (52 loc) · 1.29 KB
/
STM32.cpp
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
#include "STM32.hpp"
#include "MemoryAccessPort.hpp"
#include <unistd.h>
static const uint32_t FLASH_BASE = 0x40022000;
static const uint32_t FLASH_KEYR = FLASH_BASE + 0x04;
static const uint32_t FLASH_SR = FLASH_BASE + 0x0c;
static const uint32_t FLASH_CR = FLASH_BASE + 0x10;
static const uint32_t FLASH_AR = FLASH_BASE + 0x14;
STM32::STM32(DebugPort *debugPort) {
ahb = new MemoryAccessPort(debugPort, 0);
}
STM32::~STM32() {
delete ahb;
}
void STM32::halt() {
ahb->writeWord(0xE000EDF0, 0xA05F0003);
}
void STM32::unhalt() {
ahb->writeWord(0xE000EDF0, 0xA05F0000);
}
void STM32::reset() {
ahb->writeWord(0xE000ED0C, 0x05FA0004);
}
void STM32::unlockFlash() {
ahb->writeWord(FLASH_KEYR, 0x45670123);
ahb->writeWord(FLASH_KEYR, 0xcdef89ab);
}
void STM32::lockFlash() {
ahb->writeWord(FLASH_CR, ahb->readWord(FLASH_CR) | (1 << 7));
}
void STM32::startProgramming() {
ahb->writeWord(FLASH_CR, 1);
}
void STM32::endProgramming() {
ahb->writeWord(FLASH_CR, 0);
}
void STM32::eraseFlash(uint32_t page) {
ahb->writeWord(FLASH_CR, 2);
ahb->writeWord(FLASH_AR, page);
ahb->writeWord(FLASH_CR, 0x42);
while ((ahb->readWord(FLASH_SR) & 0x1) != 0) {
usleep(10000);
}
}
void STM32::eraseFlash() {
// TODO
}
void STM32::waitFlash() {
while ((ahb->readWord(FLASH_SR) & 0x1) != 0) {
usleep(10000);
}
}