Bugfixes and improvements for new hardware

This commit is contained in:
Jan Käberich 2020-09-14 23:13:32 +02:00
parent 7d9d5e27eb
commit 4cbd60e62d
33 changed files with 747 additions and 193 deletions

View file

@ -1,5 +1,13 @@
#include "Flash.hpp"
#include "FreeRTOS.h"
#include "task.h"
#include <cstring>
#define LOG_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE "Flash"
#include "Log.h"
bool Flash::isPresent() {
CS(false);
// read JEDEC ID
@ -24,16 +32,18 @@ void Flash::read(uint32_t address, uint16_t length, void *dest) {
bool Flash::write(uint32_t address, uint16_t length, uint8_t *src) {
if((address & 0xFF) != 0 || length%256 != 0) {
// only writes to complete pages allowed
LOG_ERR("Invalid write address/size: %lu/%u", address, length);
return false;
}
address &= 0x00FFFFFF;
LOG_DEBUG("Writing %u bytes to address %lu", length, address);
while(length > 0) {
EnableWrite();
CS(false);
uint8_t cmd[4] = {
0x02,
((uint8_t) address >> 16) & 0xFF,
((uint8_t) address >> 8) & 0xFF,
(uint8_t) (address >> 16) & 0xFF,
(uint8_t) (address >> 8) & 0xFF,
(uint8_t) (address & 0xFF),
};
// issue read command
@ -41,9 +51,18 @@ bool Flash::write(uint32_t address, uint16_t length, uint8_t *src) {
// write data
HAL_SPI_Transmit(spi, src, 256, 1000);
CS(true);
if(!WaitBusy(5)) {
if(!WaitBusy(20)) {
LOG_ERR("Write timed out");
return false;
}
// Verify
uint8_t buf[256];
read(address, 256, buf);
if(memcmp(src, buf, 256)) {
LOG_ERR("Verification error");
return false;
}
address += 256;
length -= 256;
src += 256;
}
@ -59,6 +78,7 @@ void Flash::EnableWrite() {
}
bool Flash::eraseChip() {
LOG_INFO("Erasing...");
EnableWrite();
CS(false);
// enable write latch
@ -73,8 +93,8 @@ void Flash::initiateRead(uint32_t address) {
CS(false);
uint8_t cmd[4] = {
0x03,
((uint8_t) address >> 16) & 0xFF,
((uint8_t) address >> 8) & 0xFF,
(uint8_t) (address >> 16) & 0xFF,
(uint8_t) (address >> 8) & 0xFF,
(uint8_t) (address & 0xFF),
};
// issue read command
@ -86,15 +106,17 @@ bool Flash::WaitBusy(uint32_t timeout) {
CS(false);
uint8_t readStatus1 = 0x05;
HAL_SPI_Transmit(spi, &readStatus1, 1, 100);
while(HAL_GetTick() - starttime > timeout) {
do {
vTaskDelay(1);
uint8_t status1;
HAL_SPI_Receive(spi, &status1, 1, 100);
if(!(status1 & 0x01)) {
if (!(status1 & 0x01)) {
CS(true);
return true;
}
}
} while (HAL_GetTick() - starttime < timeout);
// timed out
CS(true);
LOG_ERR("Timeout occured");
return false;
}