Skip to content

Commit

Permalink
✨ (ext-flash): Add write method
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Sep 24, 2021
1 parent 1cde146 commit b870e94
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CoreFlashMemoryIS25LP016D : public interface::FlashMemory
auto getSize() -> size_t final;

auto read(uint32_t address, lstd::span<char> rx_buffer) -> size_t final;
auto write(uint32_t address, lstd::span<char> tx_buffer) -> size_t final;

private:
void configureDataTransmissionFormat();
Expand Down
11 changes: 11 additions & 0 deletions drivers/CoreFlashMemory/source/CoreFlashMemoryDriverIS25LP016D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ auto CoreFlashMemoryIS25LP016D::read(uint32_t address, lstd::span<char> rx_buffe
return bytes_read;
}

auto CoreFlashMemoryIS25LP016D::write(uint32_t address, lstd::span<char> tx_buffer) -> size_t
{
if (address + tx_buffer.size() > getSize()) {
return 0;
}

auto bytes_write = _qspi.write(flash_memory::is25lp016d::command::write, -1, address, tx_buffer);

return bytes_write;
}

} // namespace leka
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,31 @@ TEST_F(CoreFlashMemoryIS25LP016DTest, readOverSize)

ASSERT_EQ(expected_bytes_read, actual_bytes_read);
}

TEST_F(CoreFlashMemoryIS25LP016DTest, write)
{
const size_t bytes_to_write = 0x10;
char data[bytes_to_write];
uint32_t address = 0x2A;
size_t expected_bytes_write = 0x10;

EXPECT_CALL(qspimock, write(flash_memory::is25lp016d::command::write, _, address, data, _)).Times(1);

auto actual_bytes_write = flash_memory_is25lp.write(address, data, bytes_to_write);

ASSERT_EQ(expected_bytes_write, actual_bytes_write);
}

TEST_F(CoreFlashMemoryIS25LP016DTest, writeOverSize)
{
const size_t bytes_to_write = 0x10;
char buffer[bytes_to_write];
uint32_t address = flash_memory::is25lp016d::size;
size_t expected_bytes_write = 0x0;

EXPECT_CALL(qspimock, write(flash_memory::is25lp016d::command::write, _, address, buffer, _)).Times(0);

auto actual_bytes_write = flash_memory_is25lp.write(address, buffer, bytes_to_write);

ASSERT_EQ(expected_bytes_write, actual_bytes_write);
}
3 changes: 2 additions & 1 deletion include/interface/drivers/FlashMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class FlashMemory
virtual void setFrequency(int hz) = 0;
virtual auto getSize() -> size_t = 0;

virtual auto read(uint32_t address, lstd::span<char> rx_buffer) -> size_t = 0;
virtual auto read(uint32_t address, lstd::span<char> rx_buffer) -> size_t = 0;
virtual auto write(uint32_t address, lstd::span<char> tx_buffer) -> size_t = 0;
};
} // namespace leka::interface

Expand Down
6 changes: 5 additions & 1 deletion spikes/lk_flash_memory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ auto main() -> int

const size_t bytes_to_read = 0x20;
char buffer[bytes_to_read];
uint32_t address = 0x00;
const size_t bytes_to_write = 0x0B;
char data[bytes_to_write] = "4815162342";
uint32_t address = 0x00;

while (true) {
auto t = rtos::Kernel::Clock::now() - start;
Expand All @@ -44,6 +46,8 @@ auto main() -> int

rtos::ThisThread::sleep_for(1s);

coreis25lp.write(address, data);

auto bytes_read = coreis25lp.read(address, buffer);
log_info("Content at 0x%x (%dB): %s", address, bytes_read, buffer);

Expand Down
1 change: 1 addition & 0 deletions tests/unit/mocks/mocks/leka/CoreFlashMemoryDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CoreFlashMemoryDriver : public interface::FlashMemory
MOCK_METHOD(size_t, getSize, (), (override));

MOCK_METHOD(size_t, read, (uint32_t, char *, const size_t), (override));
MOCK_METHOD(size_t, write, (uint32_t, char *, const size_t), (override));
};

} // namespace leka::mock
Expand Down

0 comments on commit b870e94

Please sign in to comment.