42
42
#include "nrf_sdm.h"
43
43
#endif
44
44
45
- /*------------------------------------------------------------------*/
46
- /* VARIABLES
47
- *------------------------------------------------------------------*/
48
-
49
45
// defined in linker
50
46
extern uint32_t __fatfs_flash_start_addr [];
51
47
extern uint32_t __fatfs_flash_length [];
52
48
53
- #define NO_CACHE 0xffffffff
54
- #define FL_PAGE_SZ 4096
49
+ #define NO_CACHE 0xffffffff
50
+ #define FL_PAGE_SZ 4096
55
51
56
- uint8_t _fl_cache [FL_PAGE_SZ ] __attribute__((aligned (4 )));
57
- uint32_t _fl_pg_addr = NO_CACHE ;
52
+ uint8_t _flash_cache [FL_PAGE_SZ ] __attribute__((aligned (4 )));
53
+ uint32_t _flash_page_addr = NO_CACHE ;
58
54
59
55
60
56
/*------------------------------------------------------------------*/
@@ -66,14 +62,14 @@ static inline uint32_t lba2addr(uint32_t block) {
66
62
67
63
void internal_flash_init (void ) {
68
64
// Activity LED for flash writes.
69
- #ifdef MICROPY_HW_LED_MSC
70
- struct port_config pin_conf ;
71
- port_get_config_defaults (& pin_conf );
72
-
73
- pin_conf .direction = PORT_PIN_DIR_OUTPUT ;
74
- port_pin_set_config (MICROPY_HW_LED_MSC , & pin_conf );
75
- port_pin_set_output_level (MICROPY_HW_LED_MSC , false);
76
- #endif
65
+ #ifdef MICROPY_HW_LED_MSC
66
+ struct port_config pin_conf ;
67
+ port_get_config_defaults (& pin_conf );
68
+
69
+ pin_conf .direction = PORT_PIN_DIR_OUTPUT ;
70
+ port_pin_set_config (MICROPY_HW_LED_MSC , & pin_conf );
71
+ port_pin_set_output_level (MICROPY_HW_LED_MSC , false);
72
+ #endif
77
73
}
78
74
79
75
uint32_t internal_flash_get_block_size (void ) {
@@ -86,16 +82,16 @@ uint32_t internal_flash_get_block_count(void) {
86
82
87
83
// TODO support flashing with SD enabled
88
84
void internal_flash_flush (void ) {
89
- if (_fl_pg_addr == NO_CACHE ) return ;
85
+ if (_flash_page_addr == NO_CACHE ) return ;
90
86
91
87
// Skip if data is the same
92
- if (memcmp (_fl_cache , (void * )_fl_pg_addr , FL_PAGE_SZ ) != 0 ) {
88
+ if (memcmp (_flash_cache , (void * )_flash_page_addr , FL_PAGE_SZ ) != 0 ) {
93
89
// _is_flashing = true;
94
- nrf_nvmc_page_erase (_fl_pg_addr );
95
- nrf_nvmc_write_words (_fl_pg_addr , (uint32_t * )_fl_cache , FL_PAGE_SZ / sizeof (uint32_t ));
90
+ nrf_nvmc_page_erase (_flash_page_addr );
91
+ nrf_nvmc_write_words (_flash_page_addr , (uint32_t * )_flash_cache , FL_PAGE_SZ / sizeof (uint32_t ));
96
92
}
97
93
98
- _fl_pg_addr = NO_CACHE ;
94
+ _flash_page_addr = NO_CACHE ;
99
95
}
100
96
101
97
mp_uint_t internal_flash_read_blocks (uint8_t * dest , uint32_t block , uint32_t num_blocks ) {
@@ -106,39 +102,39 @@ mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num
106
102
107
103
mp_uint_t internal_flash_write_blocks (const uint8_t * src , uint32_t lba , uint32_t num_blocks ) {
108
104
109
- #ifdef MICROPY_HW_LED_MSC
105
+ #ifdef MICROPY_HW_LED_MSC
110
106
port_pin_set_output_level (MICROPY_HW_LED_MSC , true);
111
- #endif
107
+ #endif
112
108
113
109
while (num_blocks ) {
114
110
uint32_t const addr = lba2addr (lba );
115
111
uint32_t const page_addr = addr & ~(FL_PAGE_SZ - 1 );
116
112
117
- uint32_t count = 8 - (lba % 8 ); // up to page boundary
113
+ uint32_t count = 8 - (lba % 8 ); // up to page boundary
118
114
count = MIN (num_blocks , count );
119
115
120
- if (page_addr != _fl_pg_addr ) {
116
+ if (page_addr != _flash_page_addr ) {
121
117
internal_flash_flush ();
122
118
123
119
// writing previous cached data, skip current data until flashing is done
124
120
// tinyusb stack will invoke write_block() with the same parameters later on
125
121
// if ( _is_flashing ) return;
126
122
127
- _fl_pg_addr = page_addr ;
128
- memcpy (_fl_cache , (void * )page_addr , FL_PAGE_SZ );
123
+ _flash_page_addr = page_addr ;
124
+ memcpy (_flash_cache , (void * )page_addr , FL_PAGE_SZ );
129
125
}
130
126
131
- memcpy (_fl_cache + (addr & (FL_PAGE_SZ - 1 )), src , count * FILESYSTEM_BLOCK_SIZE );
127
+ memcpy (_flash_cache + (addr & (FL_PAGE_SZ - 1 )), src , count * FILESYSTEM_BLOCK_SIZE );
132
128
133
129
// adjust for next run
134
130
lba += count ;
135
- src += count * FILESYSTEM_BLOCK_SIZE ;
131
+ src += count * FILESYSTEM_BLOCK_SIZE ;
136
132
num_blocks -= count ;
137
133
}
138
134
139
- #ifdef MICROPY_HW_LED_MSC
135
+ #ifdef MICROPY_HW_LED_MSC
140
136
port_pin_set_output_level (MICROPY_HW_LED_MSC , false);
141
- #endif
137
+ #endif
142
138
143
139
return 0 ; // success
144
140
}
@@ -203,6 +199,10 @@ const mp_obj_type_t internal_flash_type = {
203
199
.locals_dict = (mp_obj_t )& internal_flash_obj_locals_dict ,
204
200
};
205
201
202
+ /*------------------------------------------------------------------*/
203
+ /* Flash API
204
+ *------------------------------------------------------------------*/
205
+
206
206
void flash_init_vfs (fs_user_mount_t * vfs ) {
207
207
vfs -> base .type = & mp_fat_vfs_type ;
208
208
vfs -> flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL ;
0 commit comments