Move flash related flags to the flash driver; refactor accordingly.
[bertos.git] / bertos / cpu / arm / drv / flash_lpc2.c
index 1f42b92163dc1082613bd3d0861f8993f13e2fe1..e1a3391bf7bbc39a1deec9a8728228f5af300854 100644 (file)
@@ -90,11 +90,12 @@ typedef enum IapCommands
 struct FlashHardware
 {
        uint8_t status;
+       int flags;
 };
 
 #define FLASH_PAGE_CNT  FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES
 
-ALLOC_BITARRAY(page_dirty, FLASH_PAGE_CNT);
+BITARRAY_ALLOC(page_dirty, FLASH_PAGE_CNT);
 static BitArray lpc2_bitx;
 
 uint8_t erase_group[] = {
@@ -179,10 +180,7 @@ static uint32_t addr_to_sector(size_t addr)
 
 static size_t lpc2_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
 {
-       ASSERT(offset == 0);
-       ASSERT(size == blk->blk_size);
-
-       memcpy(buf, (void *)(idx * blk->blk_size), size);
+       memcpy(buf, (void *)(idx * blk->blk_size + offset), size);
        return size;
 }
 
@@ -192,7 +190,7 @@ static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const
        ASSERT(FLASH_PAGE_SIZE_BYTES == size);
 
        Flash *fls = FLASH_CAST(blk);
-       if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
+       if (!(fls->hw->flags & FLASH_WRITE_ONCE))
                ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES);
 
        const uint8_t *buf = (const uint8_t *)_buf;
@@ -216,8 +214,8 @@ static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const
        if (res.status != CMD_SUCCESS)
                goto flash_error;
 
-       if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
-                       bitarray_blockFull(&lpc2_bitx, idx_sector, erase_group[sector]))
+       if ((fls->hw->flags & FLASH_WRITE_ONCE) &&
+                       bitarray_isRangeFull(&lpc2_bitx, idx_sector, erase_group[sector]))
        {
                kputs("blocchi pieni\n");
                ASSERT(0);
@@ -225,11 +223,11 @@ static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const
        }
 
        bool erase = false;
-       if ((fls->blk.priv.flags & KB_WRITE_ONCE) &&
-                       bitarray_blockEmpty(&lpc2_bitx, idx_sector, erase_group[sector]))
+       if ((fls->hw->flags & FLASH_WRITE_ONCE) &&
+                       bitarray_isRangeEmpty(&lpc2_bitx, idx_sector, erase_group[sector]))
                erase = true;
 
-       if (!(fls->blk.priv.flags & KB_WRITE_ONCE))
+       if (!(fls->hw->flags & FLASH_WRITE_ONCE))
                erase = true;
 
        if (erase)
@@ -251,9 +249,9 @@ static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const
        if (res.status != CMD_SUCCESS)
                goto flash_error;
 
-       if (fls->blk.priv.flags & KB_WRITE_ONCE)
+       if (fls->hw->flags & FLASH_WRITE_ONCE)
        {
-               if (bitarray_check(&lpc2_bitx, idx))
+               if (bitarray_test(&lpc2_bitx, idx))
                {
                        ASSERT(0);
                        goto flash_error;
@@ -278,6 +276,7 @@ static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const
        return blk->blk_size;
 
 flash_error:
+       IRQ_RESTORE(flags);
        LOG_ERR("%ld\n", res.status);
        fls->hw->status |= FLASH_WR_ERR;
        return 0;
@@ -332,26 +331,28 @@ static const KBlockVTable flash_lpc2_unbuffered_vt =
 static struct FlashHardware flash_lpc2_hw;
 static uint8_t flash_buf[FLASH_PAGE_SIZE_BYTES];
 
-static void common_init(Flash *fls)
+static void common_init(Flash *fls, int flags)
 {
        memset(fls, 0, sizeof(*fls));
        DB(fls->blk.priv.type = KBT_FLASH);
 
        fls->hw = &flash_lpc2_hw;
+       fls->hw->flags = flags;
 
        fls->blk.blk_size = FLASH_PAGE_SIZE_BYTES;
        fls->blk.blk_cnt = FLASH_MEM_SIZE / FLASH_PAGE_SIZE_BYTES;
 
-       init_bitarray(&lpc2_bitx, FLASH_PAGE_CNT, page_dirty, sizeof(page_dirty));
+       bitarray_init(&lpc2_bitx, FLASH_PAGE_CNT, page_dirty, sizeof(page_dirty));
 }
 
 void flash_hw_init(Flash *fls, int flags)
 {
-       common_init(fls);
+       common_init(fls, flags);
        fls->blk.priv.vt = &flash_lpc2_buffered_vt;
-       fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE | flags;
+       fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
        fls->blk.priv.buf = flash_buf;
 
+
        /* Load the first block in the cache */
        void *flash_start = 0x0;
        memcpy(fls->blk.priv.buf, flash_start, fls->blk.blk_size);
@@ -359,7 +360,6 @@ void flash_hw_init(Flash *fls, int flags)
 
 void flash_hw_initUnbuffered(Flash *fls, int flags)
 {
-       common_init(fls);
+       common_init(fls, flags);
        fls->blk.priv.vt = &flash_lpc2_unbuffered_vt;
-       fls->blk.priv.flags |= flags;
 }