4242#include "nrf_sdm.h"
4343#endif
4444
45- /*------------------------------------------------------------------*/
46- /* VARIABLES
47- *------------------------------------------------------------------*/
48-
4945// defined in linker
5046extern uint32_t __fatfs_flash_start_addr [];
5147extern uint32_t __fatfs_flash_length [];
5248
53- #define NO_CACHE 0xffffffff
54- #define FL_PAGE_SZ 4096
49+ #define NO_CACHE 0xffffffff
50+ #define FL_PAGE_SZ 4096
5551
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 ;
5854
5955
6056/*------------------------------------------------------------------*/
@@ -66,14 +62,14 @@ static inline uint32_t lba2addr(uint32_t block) {
6662
6763void internal_flash_init (void ) {
6864 // 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
7773}
7874
7975uint32_t internal_flash_get_block_size (void ) {
@@ -86,16 +82,16 @@ uint32_t internal_flash_get_block_count(void) {
8682
8783// TODO support flashing with SD enabled
8884void internal_flash_flush (void ) {
89- if (_fl_pg_addr == NO_CACHE ) return ;
85+ if (_flash_page_addr == NO_CACHE ) return ;
9086
9187 // 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 ) {
9389// _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 ));
9692 }
9793
98- _fl_pg_addr = NO_CACHE ;
94+ _flash_page_addr = NO_CACHE ;
9995}
10096
10197mp_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
106102
107103mp_uint_t internal_flash_write_blocks (const uint8_t * src , uint32_t lba , uint32_t num_blocks ) {
108104
109- #ifdef MICROPY_HW_LED_MSC
105+ #ifdef MICROPY_HW_LED_MSC
110106 port_pin_set_output_level (MICROPY_HW_LED_MSC , true);
111- #endif
107+ #endif
112108
113109 while (num_blocks ) {
114110 uint32_t const addr = lba2addr (lba );
115111 uint32_t const page_addr = addr & ~(FL_PAGE_SZ - 1 );
116112
117- uint32_t count = 8 - (lba % 8 ); // up to page boundary
113+ uint32_t count = 8 - (lba % 8 ); // up to page boundary
118114 count = MIN (num_blocks , count );
119115
120- if (page_addr != _fl_pg_addr ) {
116+ if (page_addr != _flash_page_addr ) {
121117 internal_flash_flush ();
122118
123119 // writing previous cached data, skip current data until flashing is done
124120 // tinyusb stack will invoke write_block() with the same parameters later on
125121 // if ( _is_flashing ) return;
126122
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 );
129125 }
130126
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 );
132128
133129 // adjust for next run
134130 lba += count ;
135- src += count * FILESYSTEM_BLOCK_SIZE ;
131+ src += count * FILESYSTEM_BLOCK_SIZE ;
136132 num_blocks -= count ;
137133 }
138134
139- #ifdef MICROPY_HW_LED_MSC
135+ #ifdef MICROPY_HW_LED_MSC
140136 port_pin_set_output_level (MICROPY_HW_LED_MSC , false);
141- #endif
137+ #endif
142138
143139 return 0 ; // success
144140}
@@ -203,6 +199,10 @@ const mp_obj_type_t internal_flash_type = {
203199 .locals_dict = (mp_obj_t )& internal_flash_obj_locals_dict ,
204200};
205201
202+ /*------------------------------------------------------------------*/
203+ /* Flash API
204+ *------------------------------------------------------------------*/
205+
206206void flash_init_vfs (fs_user_mount_t * vfs ) {
207207 vfs -> base .type = & mp_fat_vfs_type ;
208208 vfs -> flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL ;
0 commit comments