target_flash: use new variable for buf size
in cases where blocksize is very large, the allocation may fail due to its size, allow specifying smaller buffer sizes
This commit is contained in:
parent
be4b07f5ef
commit
aec7460426
|
@ -211,6 +211,8 @@ void target_add_flash(target *t, target_flash_s *f)
|
||||||
{
|
{
|
||||||
if (f->writesize == 0)
|
if (f->writesize == 0)
|
||||||
f->writesize = f->blocksize;
|
f->writesize = f->blocksize;
|
||||||
|
if (f->writebufsize == 0)
|
||||||
|
f->writebufsize = f->writesize;
|
||||||
f->t = t;
|
f->t = t;
|
||||||
f->next = t->flash;
|
f->next = t->flash;
|
||||||
t->flash = f;
|
t->flash = f;
|
||||||
|
|
|
@ -211,7 +211,7 @@ static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const voi
|
||||||
{
|
{
|
||||||
if (f->buf == NULL) {
|
if (f->buf == NULL) {
|
||||||
/* Allocate buffer */
|
/* Allocate buffer */
|
||||||
f->buf = malloc(f->blocksize);
|
f->buf = malloc(f->writebufsize);
|
||||||
if (!f->buf) { /* malloc failed: heap exhaustion */
|
if (!f->buf) { /* malloc failed: heap exhaustion */
|
||||||
DEBUG_WARN("malloc: failed in %s\n", __func__);
|
DEBUG_WARN("malloc: failed in %s\n", __func__);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -223,7 +223,7 @@ static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const voi
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
while (len) {
|
while (len) {
|
||||||
const target_addr_t base_addr = dest & ~(f->blocksize - 1U);
|
const target_addr_t base_addr = dest & ~(f->writebufsize - 1U);
|
||||||
|
|
||||||
/* check for base address change */
|
/* check for base address change */
|
||||||
if (base_addr != f->buf_addr_base) {
|
if (base_addr != f->buf_addr_base) {
|
||||||
|
@ -231,11 +231,11 @@ static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const voi
|
||||||
|
|
||||||
/* Setup buffer */
|
/* Setup buffer */
|
||||||
f->buf_addr_base = base_addr;
|
f->buf_addr_base = base_addr;
|
||||||
memset(f->buf, f->erased, f->blocksize);
|
memset(f->buf, f->erased, f->writebufsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t offset = dest % f->blocksize;
|
const size_t offset = dest % f->writebufsize;
|
||||||
const size_t local_len = MIN(f->blocksize - offset, len);
|
const size_t local_len = MIN(f->writebufsize - offset, len);
|
||||||
|
|
||||||
/* Copy chunk into sector buffer */
|
/* Copy chunk into sector buffer */
|
||||||
memcpy(f->buf + offset, src, local_len);
|
memcpy(f->buf + offset, src, local_len);
|
||||||
|
|
|
@ -44,7 +44,8 @@ struct target_flash {
|
||||||
target_addr_t start; /* start address of flash */
|
target_addr_t start; /* start address of flash */
|
||||||
size_t length; /* flash length */
|
size_t length; /* flash length */
|
||||||
size_t blocksize; /* erase block size */
|
size_t blocksize; /* erase block size */
|
||||||
size_t writesize; /* write operation size, must be <= blocksize */
|
size_t writesize; /* write operation size, must be <= blocksize/writebufsize */
|
||||||
|
size_t writebufsize; /* size of write buffer */
|
||||||
uint8_t erased; /* byte erased state */
|
uint8_t erased; /* byte erased state */
|
||||||
bool ready; /* true if flash is in flash mode/prepared */
|
bool ready; /* true if flash is in flash mode/prepared */
|
||||||
flash_prepare_func prepare; /* prepare for flash operations */
|
flash_prepare_func prepare; /* prepare for flash operations */
|
||||||
|
|
Loading…
Reference in New Issue