123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "diskio_blkdev.h"
- static diskio_blkdev_t * m_drives;
- static BYTE m_drives_count;
- static void block_dev_handler(struct nrf_block_dev_s const * p_blk_dev,
- nrf_block_dev_event_t const * p_event)
- {
- uint8_t drv = (uint8_t)(uint32_t) p_event->p_context;
- ASSERT(drv < m_drives_count);
- switch (p_event->ev_type)
- {
- case NRF_BLOCK_DEV_EVT_INIT:
- case NRF_BLOCK_DEV_EVT_UNINIT:
- case NRF_BLOCK_DEV_EVT_BLK_WRITE_DONE:
- case NRF_BLOCK_DEV_EVT_BLK_READ_DONE:
- m_drives[drv].last_result = p_event->result;
- m_drives[drv].busy = false;
- break;
- default:
- break;
- }
- }
- static void default_wait_func(void)
- {
- __WFE();
- }
- DSTATUS disk_initialize(BYTE drv)
- {
- ASSERT(m_drives);
- if (drv >= m_drives_count)
- {
- return (STA_NODISK | STA_NOINIT);
- }
- if (!m_drives[drv].config.p_block_device)
- {
- return (STA_NODISK | STA_NOINIT);
- }
- if (!(m_drives[drv].state & STA_NOINIT))
- {
-
- return m_drives[drv].state;
- }
- if (m_drives[drv].config.wait_func == NULL)
- {
- m_drives[drv].config.wait_func = default_wait_func;
- }
- m_drives[drv].busy = true;
- ret_code_t err_code = nrf_blk_dev_init(m_drives[drv].config.p_block_device,
- block_dev_handler,
- (void *) (uint32_t) drv);
- if (err_code == NRF_SUCCESS)
- {
- while (m_drives[drv].busy)
- {
- m_drives[drv].config.wait_func();
- }
- if (m_drives[drv].last_result == NRF_BLOCK_DEV_RESULT_SUCCESS)
- {
- m_drives[drv].state &= ~STA_NOINIT;
- }
- }
- return m_drives[drv].state;
- }
- DSTATUS disk_uninitialize(BYTE drv)
- {
- ASSERT(m_drives);
- if (drv >= m_drives_count)
- {
- return (STA_NODISK | STA_NOINIT);
- }
- if (!m_drives[drv].config.p_block_device)
- {
- return (STA_NODISK | STA_NOINIT);
- }
- if (m_drives[drv].state & STA_NOINIT)
- {
-
- return m_drives[drv].state;
- }
- (void)nrf_blk_dev_ioctl(m_drives[drv].config.p_block_device,
- NRF_BLOCK_DEV_IOCTL_REQ_CACHE_FLUSH,
- NULL);
- ret_code_t ret;
- do
- {
-
- ret = nrf_blk_dev_uninit(m_drives[drv].config.p_block_device);
- } while (ret == NRF_ERROR_BUSY);
- if (ret == NRF_SUCCESS)
- {
- while (m_drives[drv].busy)
- {
- m_drives[drv].config.wait_func();
- }
- }
- if (m_drives[drv].last_result == NRF_BLOCK_DEV_RESULT_SUCCESS)
- {
- m_drives[drv].state |= STA_NOINIT;
- }
- return m_drives[drv].state;
- }
- DSTATUS disk_status(BYTE drv)
- {
- ASSERT(m_drives);
- if (drv >= m_drives_count)
- {
- return STA_NOINIT;
- }
- return m_drives[drv].state;
- }
- DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, UINT count)
- {
- ASSERT(m_drives);
- if ((drv >= m_drives_count) || (!count))
- {
- return RES_PARERR;
- }
- if ((m_drives[drv].config.p_block_device == NULL)
- || (m_drives[drv].state & STA_NOINIT))
- {
- return RES_NOTRDY;
- }
- const nrf_block_req_t req = {
- .p_buff = buff,
- .blk_id = sector,
- .blk_count = count
- };
- m_drives[drv].busy = true;
- ret_code_t err_code = nrf_blk_dev_read_req(m_drives[drv].config.p_block_device, &req);
- if (err_code == NRF_SUCCESS)
- {
- while (m_drives[drv].busy)
- {
- m_drives[drv].config.wait_func();
- }
- if (m_drives[drv].last_result == NRF_BLOCK_DEV_RESULT_SUCCESS)
- {
- return RES_OK;
- }
- }
- return RES_ERROR;
- }
- DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, UINT count)
- {
- ASSERT(m_drives);
- if ((drv >= m_drives_count) || (!count))
- {
- return RES_PARERR;
- }
- if ((m_drives[drv].config.p_block_device == NULL)
- || (m_drives[drv].state & STA_NOINIT))
- {
- return RES_NOTRDY;
- }
- if (m_drives[drv].state & STA_PROTECT)
- {
- return RES_WRPRT;
- }
- const nrf_block_req_t req = {
- .p_buff = (void *)buff,
- .blk_id = sector,
- .blk_count = count
- };
- m_drives[drv].busy = true;
- ret_code_t err_code = nrf_blk_dev_write_req(m_drives[drv].config.p_block_device, &req);
- if (err_code == NRF_SUCCESS)
- {
- while (m_drives[drv].busy)
- {
- m_drives[drv].config.wait_func();
- }
- if (m_drives[drv].last_result == NRF_BLOCK_DEV_RESULT_SUCCESS)
- {
- return RES_OK;
- }
- }
- return RES_ERROR;
- }
- DRESULT disk_ioctl(BYTE drv, BYTE cmd, void *buff)
- {
- ASSERT(m_drives);
- if (drv >= m_drives_count)
- {
- return RES_PARERR;
- }
-
- switch (cmd)
- {
- case CTRL_SYNC:
- {
- bool flush_in_progress = true;
- do {
-
- ret_code_t ret = nrf_blk_dev_ioctl(m_drives[drv].config.p_block_device,
- NRF_BLOCK_DEV_IOCTL_REQ_CACHE_FLUSH,
- &flush_in_progress);
- if (ret != NRF_SUCCESS && ret != NRF_ERROR_BUSY)
- {
- break;
- }
- } while (flush_in_progress);
- return RES_OK;
- }
- case GET_SECTOR_COUNT:
- {
- if (m_drives[drv].config.p_block_device == NULL)
- {
- return RES_NOTRDY;
- }
- DWORD * val = buff;
- *val = nrf_blk_dev_geometry(m_drives[drv].config.p_block_device)->blk_count;
- return RES_OK;
- }
- case GET_SECTOR_SIZE:
- {
- if (m_drives[drv].config.p_block_device == NULL)
- {
- return RES_NOTRDY;
- }
- WORD * val = buff;
- *val = nrf_blk_dev_geometry(m_drives[drv].config.p_block_device)->blk_size;
- return RES_OK;
- }
- default:
- break;
- }
- return RES_ERROR;
- }
- void diskio_blockdev_register(diskio_blkdev_t * diskio_blkdevs, size_t count)
- {
- ASSERT(diskio_blkdevs);
- m_drives = diskio_blkdevs;
- m_drives_count = count;
- }
|