From dec5b97275433cc0965681e2838b35a8ff9abf02 Mon Sep 17 00:00:00 2001 From: Michel Pastor Date: Thu, 3 Oct 2019 16:58:13 +0200 Subject: [PATCH] AP_Logger: fix logging on 256Mbit flash chips --- libraries/AP_Logger/AP_Logger_Block.cpp | 28 ++++++++++----------- libraries/AP_Logger/AP_Logger_Block.h | 4 +-- libraries/AP_Logger/AP_Logger_DataFlash.cpp | 11 ++++++++ libraries/AP_Logger/AP_Logger_DataFlash.h | 3 ++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/libraries/AP_Logger/AP_Logger_Block.cpp b/libraries/AP_Logger/AP_Logger_Block.cpp index e17ca13c53..7b385fc003 100644 --- a/libraries/AP_Logger/AP_Logger_Block.cpp +++ b/libraries/AP_Logger/AP_Logger_Block.cpp @@ -11,7 +11,7 @@ extern AP_HAL::HAL& hal; // the last page holds the log format in first 4 bytes. Please change // this if (and only if!) the low level format changes -#define DF_LOGGING_FORMAT 0x19012019 +#define DF_LOGGING_FORMAT 0x1901201A AP_Logger_Block::AP_Logger_Block(AP_Logger &front, LoggerMessageWriter_DFLogStart *writer) : writebuf(0), @@ -464,20 +464,20 @@ uint32_t AP_Logger_Block::find_last_page(void) uint32_t look; uint32_t bottom = 1; uint32_t top = df_NumPages; - uint32_t look_hash; - uint32_t bottom_hash; - uint32_t top_hash; + uint64_t look_hash; + uint64_t bottom_hash; + uint64_t top_hash; WITH_SEMAPHORE(sem); StartRead(bottom); - bottom_hash = ((int32_t)GetFileNumber()<<16) | df_FilePage; + bottom_hash = ((int64_t)GetFileNumber()<<32) | df_FilePage; while (top-bottom > 1) { look = (top+bottom)/2; StartRead(look); - look_hash = (int32_t)GetFileNumber()<<16 | df_FilePage; - if (look_hash >= 0xFFFF0000) { + look_hash = (int64_t)GetFileNumber()<<32 | df_FilePage; + if (look_hash >= 0xFFFF00000000) { look_hash = 0; } @@ -492,8 +492,8 @@ uint32_t AP_Logger_Block::find_last_page(void) } StartRead(top); - top_hash = ((int32_t)GetFileNumber()<<16) | df_FilePage; - if (top_hash >= 0xFFFF0000) { + top_hash = ((int64_t)GetFileNumber()<<32) | df_FilePage; + if (top_hash >= 0xFFFF00000000) { top_hash = 0; } if (top_hash > bottom_hash) { @@ -509,8 +509,8 @@ uint32_t AP_Logger_Block::find_last_page_of_log(uint16_t log_number) uint32_t look; uint32_t bottom; uint32_t top; - uint32_t look_hash; - uint32_t check_hash; + uint64_t look_hash; + uint64_t check_hash; WITH_SEMAPHORE(sem); @@ -529,13 +529,13 @@ uint32_t AP_Logger_Block::find_last_page_of_log(uint16_t log_number) top = find_last_page(); } - check_hash = (int32_t)log_number<<16 | 0xFFFF; + check_hash = (int64_t)log_number<<32 | 0xFFFFFFFF; while (top-bottom > 1) { look = (top+bottom)/2; StartRead(look); - look_hash = (int32_t)GetFileNumber()<<16 | df_FilePage; - if (look_hash >= 0xFFFF0000) { + look_hash = (int64_t)GetFileNumber()<<32 | df_FilePage; + if (look_hash >= 0xFFFF00000000) { look_hash = 0; } diff --git a/libraries/AP_Logger/AP_Logger_Block.h b/libraries/AP_Logger/AP_Logger_Block.h index 5028e21d2b..fe384ec08e 100644 --- a/libraries/AP_Logger/AP_Logger_Block.h +++ b/libraries/AP_Logger/AP_Logger_Block.h @@ -46,9 +46,9 @@ private: virtual void StartErase() = 0; virtual bool InErase() = 0; - struct PageHeader { + struct PACKED PageHeader { + uint32_t FilePage; uint16_t FileNumber; - uint16_t FilePage; }; HAL_Semaphore_Recursive sem; diff --git a/libraries/AP_Logger/AP_Logger_DataFlash.cpp b/libraries/AP_Logger/AP_Logger_DataFlash.cpp index 020282e179..88bdf035cb 100644 --- a/libraries/AP_Logger/AP_Logger_DataFlash.cpp +++ b/libraries/AP_Logger/AP_Logger_DataFlash.cpp @@ -68,6 +68,10 @@ void AP_Logger_DataFlash::Init() return; } + if (use_32bit_address) { + Enter4ByteAddressMode(); + } + flash_died = false; AP_Logger_Block::Init(); @@ -174,6 +178,13 @@ bool AP_Logger_DataFlash::Busy() return (status & JEDEC_STATUS_BUSY) != 0; } +void AP_Logger_DataFlash::Enter4ByteAddressMode(void) +{ + WITH_SEMAPHORE(dev_sem); + + const uint8_t cmd = 0xB7; + dev->transfer(&cmd, 1, NULL, 0); +} /* send a command with an address diff --git a/libraries/AP_Logger/AP_Logger_DataFlash.h b/libraries/AP_Logger/AP_Logger_DataFlash.h index f7939e5d93..0c09e7a748 100644 --- a/libraries/AP_Logger/AP_Logger_DataFlash.h +++ b/libraries/AP_Logger/AP_Logger_DataFlash.h @@ -27,7 +27,8 @@ private: void WaitReady(); bool Busy(); uint8_t ReadStatusReg(); - + void Enter4ByteAddressMode(void); + void WriteEnable(); bool getSectorCount(void); void flash_test(void);