33#include " FreeRTOS.h"
44#include " task.h"
55#include < cstring>
6+ #include " pico/time.h"
67
78#include < stdio.h>
89
9- #define LOG_DEBUG (s, ...) // printf(s, __VA_ARGS__)
10- #define LOG_INFO (s... ) // printf(s)
11- #define LOG_ERR (s... ) // printf(s)
12- // #define LOG_ERR(s, ...) printf(s, __VA_ARGS__)
13- // #define LOG_ERR(s, ...) printf(s, __VA_ARGS__)
14- // #define LOG_ERR(s, ...) printf(s, __VA_ARGS__)
10+ #define LOG_LEVEL LOG_LEVEL_INFO
11+ #define LOG_MODULE " Flash"
12+ #include " Log.h"
1513
1614bool Flash::isPresent () {
1715 xSemaphoreTakeRecursive (mutex, portMAX_DELAY);
@@ -50,16 +48,16 @@ void Flash::read(uint32_t address, uint16_t length, void *dest) {
5048 xSemaphoreGiveRecursive (mutex);
5149}
5250
53- bool Flash::write (uint32_t address, uint16_t length, const void *src) {
51+ bool Flash::write (uint32_t address, uint16_t length, const uint8_t *src) {
5452 xSemaphoreTakeRecursive (mutex, portMAX_DELAY);
5553 if (address % PageSize != 0 || length%PageSize != 0 ) {
5654 // only writes to complete pages allowed
57- LOG_ERR (" Invalid write address/size: %lu /%u" , address, length);
55+ LOG_ERR (" Invalid write address/size: 0x%08x /%u" , address, length);
5856 xSemaphoreGiveRecursive (mutex);
5957 return false ;
6058 }
6159 address &= 0x00FFFFFF ;
62- LOG_DEBUG (" Writing %u bytes to address %lu " , length, address);
60+ LOG_DEBUG (" Writing %u bytes to address 0x%08x " , length, address);
6361 while (length > 0 ) {
6462 EnableWrite ();
6563 CS (false );
@@ -101,6 +99,7 @@ void Flash::EnableWrite() {
10199 uint8_t wel = 0x06 ;
102100 spi_write_blocking (spi, &wel, 1 );
103101 CS (true );
102+ sleep_us (1 );
104103}
105104
106105bool Flash::eraseChip () {
@@ -111,14 +110,15 @@ bool Flash::eraseChip() {
111110 uint8_t chip_erase = 0x60 ;
112111 spi_write_blocking (spi, &chip_erase, 1 );
113112 CS (true );
113+ sleep_us (1 );
114114 return WaitBusy (25000 );
115115}
116116
117117bool Flash::eraseSector (uint32_t address) {
118118 xSemaphoreTakeRecursive (mutex, portMAX_DELAY);
119119 // align address with sector address
120120 address -= address % SectorSize;
121- LOG_INFO (" Erasing sector at %lu " , address);
121+ LOG_INFO (" Erasing sector at 0x%08x " , address);
122122 EnableWrite ();
123123 CS (false );
124124 uint8_t cmd[4 ] = {
@@ -129,6 +129,7 @@ bool Flash::eraseSector(uint32_t address) {
129129 };
130130 spi_write_blocking (spi, cmd, 4 );
131131 CS (true );
132+ sleep_us (1 );
132133 bool ret = WaitBusy (25000 );
133134 xSemaphoreGiveRecursive (mutex);
134135 return ret;
@@ -138,7 +139,7 @@ bool Flash::erase32Block(uint32_t address) {
138139 xSemaphoreTakeRecursive (mutex, portMAX_DELAY);
139140 // align address with block address
140141 address -= address % Block32Size;
141- LOG_INFO (" Erasing 32kB block at %lu " , address);
142+ LOG_INFO (" Erasing 32kB block at 0x%08x " , address);
142143 EnableWrite ();
143144 CS (false );
144145 uint8_t cmd[4 ] = {
@@ -149,6 +150,7 @@ bool Flash::erase32Block(uint32_t address) {
149150 };
150151 spi_write_blocking (spi, cmd, 4 );
151152 CS (true );
153+ sleep_us (1 );
152154 bool ret = WaitBusy (25000 );
153155 xSemaphoreGiveRecursive (mutex);
154156 return ret;
@@ -158,7 +160,7 @@ bool Flash::erase64Block(uint32_t address) {
158160 xSemaphoreTakeRecursive (mutex, portMAX_DELAY);
159161 // align address with block address
160162 address -= address % Block64Size;
161- LOG_INFO (" Erasing 64kB block at %lu " , address);
163+ LOG_INFO (" Erasing 64kB block at 0x%08x " , address);
162164 EnableWrite ();
163165 CS (false );
164166 uint8_t cmd[4 ] = {
0 commit comments