#include "SIM_JEDEC.h" #include #include #include using namespace SITL; extern HAL_SITL& hal; void JEDEC::open_storage_fd() { if (storage_fd != -1) { AP_HAL::panic("Should not have been called"); } const char *filepath = filename(); if (hal.get_wipe_storage()) { unlink(filepath); } storage_fd = open(filepath, O_RDWR|O_CREAT, 0644); if (storage_fd == -1) { AP_HAL::panic("open(%s): %s", filepath, strerror(errno)); } if (ftruncate(storage_fd, get_storage_size()) != 0) { AP_HAL::panic("truncate(%s): %s", filepath, strerror(errno)); } } void JEDEC::sector4k_erase (uint32_t addr) { for (uint8_t i=0; i get_storage_size()) { AP_HAL::panic("trying to write outside memory"); } memset(fill, 0xFF, sizeof(fill)); const size_t write_ret = pwrite(storage_fd, fill, sizeof(fill), addr); if (write_ret != sizeof(fill)) { printf("Failed page erase"); } } void JEDEC::bulk_erase() { for (uint16_t i=0; i get_storage_size()) { AP_HAL::panic("trying to read outside memory"); } if (lseek(storage_fd, xfr_addr, SEEK_SET) == -1) { AP_HAL::panic("lseek(): %s", strerror(errno)); } const size_t read_ret = read(storage_fd, rx_buf, tfr.len); if (read_ret != tfr.len) { AP_HAL::panic("read(): %s (%d/%u)", strerror(errno), (signed)read_ret, (unsigned)tfr.len); } state = State::WAITING; break; } case State::WRITING: { assert_writes_enabled(); if (xfr_addr + tfr.len > get_storage_size()) { AP_HAL::panic("trying to write outside memory"); } if (lseek(storage_fd, xfr_addr, SEEK_SET) == -1) { AP_HAL::panic("lseek(): %s", strerror(errno)); } const size_t write_ret = write(storage_fd, tx_buf, tfr.len); if (write_ret != tfr.len) { AP_HAL::panic("write(): %s (%d/%u)", strerror(errno), (signed)write_ret, (unsigned)tfr.len); } state = State::WAITING; write_enabled = false; break; } } } return 0; }