Skip to content

Commit 76c9fa9

Browse files
authored
Merge pull request #44 from maxiwoj/tm/stm32l4
Tm/stm32l4
2 parents 5f2ed1b + d9a2c0d commit 76c9fa9

File tree

7 files changed

+437
-1
lines changed

7 files changed

+437
-1
lines changed

Inc/cortexm/stm32/stm32l4.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef __STM32L4_H
2+
#define __STM32L4_H
3+
4+
#define STM32L4_PAGE_SIZE 0x800
5+
/* Flash Program ad Erase Controller Register Map */
6+
#define STM32L4_FPEC_BASE 0x40022000
7+
#define STM32L4_FLASH_ACR (STM32L4_FPEC_BASE+0x00)
8+
#define STM32L4_FLASH_KEYR (STM32L4_FPEC_BASE+0x08)
9+
#define STM32L4_FLASH_OPTKEYR (STM32L4_FPEC_BASE+0x0c)
10+
#define STM32L4_FLASH_SR (STM32L4_FPEC_BASE+0x10)
11+
#define STM32L4_FLASH_CR (STM32L4_FPEC_BASE+0x14)
12+
#define STM32L4_FLASH_OPTR (STM32L4_FPEC_BASE+0x20)
13+
14+
#define STM32L4_FLASH_CR_PG (1 << 0)
15+
#define STM32L4_FLASH_CR_PER (1 << 1)
16+
#define STM32L4_FLASH_CR_MER1 (1 << 2)
17+
#define STM32L4_FLASH_CR_PAGE_SHIFT 3
18+
#define STM32L4_FLASH_CR_BKER (1 << 11)
19+
#define STM32L4_FLASH_CR_MER2 (1 << 15)
20+
#define STM32L4_FLASH_CR_STRT (1 << 16)
21+
#define STM32L4_FLASH_CR_OPTSTRT (1 << 17)
22+
#define STM32L4_FLASH_CR_FSTPG (1 << 18)
23+
#define STM32L4_FLASH_CR_EOPIE (1 << 24)
24+
#define STM32L4_FLASH_CR_ERRIE (1 << 25)
25+
#define STM32L4_FLASH_CR_OBL_LAUNCH (1 << 27)
26+
#define STM32L4_FLASH_CR_OPTLOCK (1 << 30)
27+
#define STM32L4_FLASH_CR_LOCK (1 << 31)
28+
29+
#define STM32L4_FLASH_SR_EOP (1 << 0)
30+
#define STM32L4_FLASH_SR_OPERR (1 << 1)
31+
#define STM32L4_FLASH_SR_PROGERR (1 << 3)
32+
#define STM32L4_FLASH_SR_WRPERR (1 << 4)
33+
#define STM32L4_FLASH_SR_PGAERR (1 << 5)
34+
#define STM32L4_FLASH_SR_SIZERR (1 << 6)
35+
#define STM32L4_FLASH_SR_PGSERR (1 << 7)
36+
#define STM32L4_FLASH_SR_MSERR (1 << 8)
37+
#define STM32L4_FLASH_SR_FASTERR (1 << 9)
38+
#define STM32L4_FLASH_SR_RDERR (1 << 14)
39+
#define STM32L4_FLASH_SR_OPTVERR (1 << 15)
40+
#define STM32L4_FLASH_SR_ERROR_MASK 0xC3FA
41+
#define STM32L4_FLASH_SR_BSY (1 << 16)
42+
43+
#define STM32L4_KEY1 0x45670123
44+
#define STM32L4_KEY2 0xCDEF89AB
45+
46+
#define STM32L4_OPTKEY1 0x08192A3B
47+
#define STM32L4_OPTKEY2 0x4C5D6E7F
48+
49+
#define STM32L4_SR_ERROR_MASK 0xF2
50+
#define STM32L4_SR_EOP 0x01
51+
52+
#define STM32L4_OR_DUALBANK (1 << 21)
53+
54+
#define STM32L4_DBGMCU_IDCODE 0xE0042000
55+
#define STM32L4_FLASH_SIZE_REG 0x1FFF75E0
56+
57+
#define STM32L4_SIZE_OF_ONE_WRITE 0x1000
58+
#define STM32L4_ERASE_TIME_IN_WRITES 10
59+
60+
/*
61+
* flash errors returned to flash_target_task
62+
* 0x0 - 0x1000 - reserved for target (we can use this pool here)
63+
* bit 9 (0x200) - error while flash erasing. [8..0] represent FLASH_SR[8..0]
64+
* bit 10 (0x400) - error while flash writing. [8..0] represent FLASH_SR[8..0]
65+
*/
66+
#define STM32L4_ERASE_ERROR_BIT 0x200
67+
#define STM32L4_FLASH_ERROR_BIT 0x400
68+
#define STM32L4_ERASE_NEVER_END 0x800
69+
#define STM32L4_ERROR_ON_FLASH_WRITE_SETUP 0x801
70+
71+
typedef struct CORTEXM_s CORTEXM_t;
72+
73+
typedef struct STM32L4_PRIV_s {
74+
CORTEXM_t *cortex;
75+
} STM32L4_PRIV_t;
76+
77+
int stm32l4_probe(CORTEXM_t *cortexm);
78+
79+
#endif //__STM32L4_H

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Src/adiv5/adiv5_jtag.c \
170170
Src/adiv5/adiv5.c \
171171
Src/cortexm/cortexm.c \
172172
Src/cortexm/stm32/stm32f4.c \
173+
Src/cortexm/stm32/stm32l4.c \
173174
Src/target.c
174175

175176
# ASM sources

Src/cortexm/cortexm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "adiv5/adiv5.h"
44
#include "cortexm/cortexm.h"
55
#include "cortexm/stm32/stm32f4.h"
6+
#include "cortexm/stm32/stm32l4.h"
67

78
inline static uint32_t cortexm_read_word(CORTEXM_PRIV_t *priv, uint32_t addr)
89
{
@@ -135,6 +136,10 @@ int probe_cortexm(ADIv5_AP_t *ap)
135136
return 1;
136137
}
137138

139+
if(stm32l4_probe(cortexm)){
140+
return 1;
141+
}
142+
138143
// Non of the targets successful probed. Cleanup
139144
vPortFree(cortexm->priv);
140145
vPortFree(cortexm);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.text
2+
.syntax unified
3+
.cpu cortex-m4
4+
.thumb
5+
6+
7+
#define STM32_FLASH_CR_OFFSET 0x14 /* offset of CR register in FLASH struct */
8+
#define STM32_FLASH_SR_OFFSET 0x10 /* offset of SR register in FLASH struct */
9+
10+
_start:
11+
ldr r0, _flashbase
12+
ldr r1, _addr
13+
adr r2, _data
14+
ldr r3, _size
15+
ldr r5, _cr
16+
_next:
17+
cbz r3, _done
18+
str r5, [r0, #STM32_FLASH_CR_OFFSET]
19+
ldr r4, [r2]
20+
str r4, [r1]
21+
_wait:
22+
ldr r4, [r0, #STM32_FLASH_SR_OFFSET]
23+
tst r6, #0x10000 /* BSY (bit16) == 1 => operation in progress */
24+
bne _wait
25+
tst r6, #0xfa /* PGSERR | PGPERR | PGAERR | WRPERR | PROGERR*/
26+
bne _done /* fail... */
27+
28+
subs r3, #4
29+
adds r1, #4
30+
adds r2, #4
31+
b _next
32+
_done:
33+
bkpt
34+
35+
_cr:
36+
.word 0x00000001 /*(Value to write to FLASH_CR) PG*/
37+
_flashbase:
38+
.word 0x40022000 /* (FPEC_BASE) */
39+
_addr:
40+
.word 0x0
41+
_size:
42+
.word 0x0
43+
_data:
44+
.word 0x0
856 Bytes
Binary file not shown.

Src/cortexm/stm32/stm32f4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static int stm32f4_program(void *priv_void, FIL *file, int *progress)
218218
UINT br;
219219
uint8_t unaligned;
220220
uint32_t addr = 0x8000000; // start of flash memory
221-
uint32_t *data = pvPortMalloc(STM32F4_SIZE_OF_ONE_WRITE/sizeof(uint32_t));
221+
uint32_t *data = pvPortMalloc(STM32F4_SIZE_OF_ONE_WRITE);
222222
STM32F4_PRIV_t *priv = priv_void;
223223
uint16_t result;
224224
uint32_t file_len = f_size(file);

0 commit comments

Comments
 (0)