Skip to content

Commit d76b76c

Browse files
committed
Add non-volatile memory support to STM
1 parent fa95c1d commit d76b76c

File tree

8 files changed

+286
-6
lines changed

8 files changed

+286
-6
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
GNU linker script for STM32F411 via Micropython
3+
*/
4+
5+
/* Specify the memory areas */
6+
MEMORY
7+
{
8+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* entire flash */
9+
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */
10+
FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 32K /* sectors 1,2 are 16K */
11+
FLASH_NVM (rwx) : ORIGIN = 0x0800C000, LENGTH = 16K /* sector 3 is 16K */
12+
FLASH_TEXT (rx) : ORIGIN = 0x08010000, LENGTH = 448K /* sector 4 is 64K, sectors 5,6,7 are 128K */
13+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
14+
}
15+
16+
/* produce a link error if there is not this amount of RAM for these sections */
17+
_minimum_stack_size = 2K;
18+
_minimum_heap_size = 16K;
19+
20+
/* Define the top end of the stack. The stack is full descending so begins just
21+
above last byte of RAM. Note that EABI requires the stack to be 8-byte
22+
aligned for a call. */
23+
_estack = ORIGIN(RAM) + LENGTH(RAM);
24+
25+
/* RAM extents for the garbage collector */
26+
_ram_start = ORIGIN(RAM);
27+
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
28+
_heap_start = _ebss; /* heap starts just after statically allocated memory */
29+
_heap_end = 0x2001c000; /* tunable */
30+
31+
ENTRY(Reset_Handler)
32+
33+
/* define output sections */
34+
SECTIONS
35+
{
36+
/* The startup code goes first into FLASH */
37+
.isr_vector :
38+
{
39+
. = ALIGN(4);
40+
KEEP(*(.isr_vector)) /* Startup code */
41+
42+
/* This first flash block is 16K annd the isr vectors only take up
43+
about 400 bytes. Micropython pads this with files, but this didn't
44+
work with the size of Circuitpython's ff object. */
45+
46+
. = ALIGN(4);
47+
} >FLASH_ISR
48+
49+
/* Non-volitile memory */
50+
.nvm_data :
51+
{
52+
. = ALIGN(4);
53+
KEEP(*(.nvm_data))
54+
. = ALIGN(4);
55+
} >FLASH_NVM
56+
57+
/* The program code and other data goes into FLASH */
58+
.text :
59+
{
60+
. = ALIGN(4);
61+
*(.text*) /* .text* sections (code) */
62+
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
63+
/* *(.glue_7) */ /* glue arm to thumb code */
64+
/* *(.glue_7t) */ /* glue thumb to arm code */
65+
66+
. = ALIGN(4);
67+
_etext = .; /* define a global symbol at end of code */
68+
} >FLASH_TEXT
69+
70+
/* used by the startup to initialize data */
71+
_sidata = LOADADDR(.data);
72+
73+
/* This is the initialized data section
74+
The program executes knowing that the data is in the RAM
75+
but the loader puts the initial values in the FLASH (inidata).
76+
It is one task of the startup to copy the initial values from FLASH to RAM. */
77+
.data :
78+
{
79+
. = ALIGN(4);
80+
_sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */
81+
*(.data*) /* .data* sections */
82+
83+
. = ALIGN(4);
84+
_edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */
85+
} >RAM AT> FLASH_TEXT
86+
87+
/* Uninitialized data section */
88+
.bss :
89+
{
90+
. = ALIGN(4);
91+
_sbss = .; /* define a global symbol at bss start; used by startup code */
92+
*(.bss*)
93+
*(COMMON)
94+
95+
. = ALIGN(4);
96+
_ebss = .; /* define a global symbol at bss end; used by startup code and GC */
97+
} >RAM
98+
99+
/* this is to define the start of the heap, and make sure we have a minimum size */
100+
.heap :
101+
{
102+
. = ALIGN(4);
103+
. = . + _minimum_heap_size;
104+
. = ALIGN(4);
105+
} >RAM
106+
107+
/* this just checks there is enough RAM for the stack */
108+
.stack :
109+
{
110+
. = ALIGN(4);
111+
. = . + _minimum_stack_size;
112+
. = ALIGN(4);
113+
} >RAM
114+
115+
.ARM.attributes 0 : { *(.ARM.attributes) }
116+
}
117+
118+

ports/stm/boards/thunderpack/mpconfigboard.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@
2626
#define MICROPY_HW_BOARD_NAME "THUNDERPACK"
2727
#define MICROPY_HW_MCU_NAME "STM32F411CE"
2828

29-
#define FLASH_SIZE (0x80000)
30-
#define FLASH_PAGE_SIZE (0x4000)
31-
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x2000 - 0xC000)
29+
#define FLASH_SIZE (0x80000)
30+
#define FLASH_PAGE_SIZE (0x4000)
31+
#define CIRCUITPY_INTERNAL_NVM_SIZE (0x4000)
32+
#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x0800C000)
33+
#define CIRCUITPY_INTERNAL_NVM_SECTOR FLASH_SECTOR_3
34+
#define BOARD_FLASH_SIZE (FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE- 0x2000 - 0xC000)
3235

3336
#define BOARD_OSC_DIV (24)
3437
#define BOARD_OVERWRITE_SWD (1)
3538
#define BOARD_NO_VBUS_SENSE (1)
3639

3740
// Status LEDs
38-
#define MICROPY_HW_LED_TX (&pin_PA00)
39-
#define MICROPY_HW_LED_RX (&pin_PB01)
4041
#define MICROPY_HW_LED_STATUS (&pin_PA02)
4142

4243
#define DEFAULT_I2C_BUS_SCL (&pin_PB06)

ports/stm/boards/thunderpack/mpconfigboard.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ USB_DEVICES = "CDC,MSC"
77
INTERNAL_FLASH_FILESYSTEM = 1
88
LONGINT_IMPL = NONE
99

10+
CIRCUITPY_NVM = 1
11+
1012
MCU_SERIES = m4
1113
MCU_VARIANT = stm32f4
1214
MCU_SUB_VARIANT = stm32f411xe
1315
MCU_PACKAGE = 48
1416
CMSIS_MCU = STM32F411xE
15-
LD_FILE = boards/STM32F411VETx_FLASH.ld
17+
LD_FILE = boards/STM32F411VETx_nvm_FLASH.ld
1618
STARTUP_FILE = startup_stm32f411xe.s
1719

1820

ports/stm/common-hal/microcontroller/__init__.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "common-hal/microcontroller/Pin.h"
3333
#include "common-hal/microcontroller/Processor.h"
3434

35+
#include "shared-bindings/nvm/ByteArray.h"
3536
#include "shared-bindings/microcontroller/__init__.h"
3637
#include "shared-bindings/microcontroller/Pin.h"
3738
#include "shared-bindings/microcontroller/Processor.h"
@@ -109,6 +110,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
109110
},
110111
};
111112

113+
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
114+
// The singleton nvm.ByteArray object.
115+
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
116+
.base = {
117+
.type = &nvm_bytearray_type,
118+
},
119+
.len = NVM_BYTEARRAY_BUFFER,
120+
.start_address = (uint8_t*) (CIRCUITPY_INTERNAL_NVM_START_ADDR)
121+
};
122+
#endif
123+
112124
STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
113125
#if MCU_PACKAGE >= 100
114126
{ MP_ROM_QSTR(MP_QSTR_PE02), MP_ROM_PTR(&pin_PE02) },

ports/stm/common-hal/nvm/ByteArray.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "common-hal/nvm/ByteArray.h"
28+
29+
#include "stm32f4xx_hal.h"
30+
31+
#include "supervisor/shared/stack.h"
32+
33+
#include <stdint.h>
34+
#include <string.h>
35+
36+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
37+
return self->len;
38+
}
39+
40+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
41+
uint32_t start_index, uint8_t* values, uint32_t len) {
42+
// Copy flash to buffer
43+
uint8_t buffer[self->len];
44+
memcpy(buffer, self->start_address, self->len);
45+
46+
// Set bytes in buffer
47+
memmove(buffer + start_index, values, len);
48+
49+
// Erase flash sector
50+
HAL_FLASH_Unlock();
51+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
52+
FLASH_Erase_Sector(CIRCUITPY_INTERNAL_NVM_SECTOR, VOLTAGE_RANGE_3);
53+
54+
// Write bytes to flash
55+
uint32_t i;
56+
uint32_t flash_addr = (uint32_t)self->start_address;
57+
for (i = 0; i < self->len; i++, flash_addr++) {
58+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, flash_addr, buffer[i]) != HAL_OK) {
59+
HAL_FLASH_Lock();
60+
return false;
61+
}
62+
}
63+
64+
// Finish up
65+
HAL_FLASH_Lock();
66+
return true;
67+
}
68+
69+
// NVM memory is memory mapped so reading it is easy.
70+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
71+
uint32_t start_index, uint32_t len, uint8_t* values) {
72+
memcpy(values, self->start_address + start_index, len);
73+
}

ports/stm/common-hal/nvm/ByteArray.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_NVM_BYTEARRAY_H
28+
#define MICROPY_INCLUDED_STM32_COMMON_HAL_NVM_BYTEARRAY_H
29+
30+
#include "py/obj.h"
31+
32+
// STM flash is saved in sectors (not pages), at a minimum size of 16k.
33+
// To limit the RAM usage during writing, we want to set a smaller
34+
// maximum value.
35+
#define NVM_BYTEARRAY_BUFFER 512
36+
37+
typedef struct {
38+
mp_obj_base_t base;
39+
uint8_t* start_address;
40+
uint32_t len;
41+
} nvm_bytearray_obj_t;
42+
43+
#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_NVM_BYTEARRAY_H

ports/stm/common-hal/nvm/__init__.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// No nvm module functions.

ports/stm/supervisor/internal_flash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@
3939

4040
#ifdef STM32F411xE
4141
#define STM32_FLASH_SIZE 0x80000 //512KiB
42+
#ifdef CIRCUITPY_NVM
43+
#define INTERNAL_FLASH_FILESYSTEM_SIZE 0x8000 //32KiB
44+
#else
4245
#define INTERNAL_FLASH_FILESYSTEM_SIZE 0xC000 //48KiB
4346
#endif
47+
#endif
4448

4549
#ifdef STM32F412Zx
4650
#define STM32_FLASH_SIZE 0x100000 //1MB

0 commit comments

Comments
 (0)