A board support package for the LPC4088 Display Module.

Dependencies:   DM_HttpServer DM_USBHost

Dependents:   lpc4088_displaymodule_emwin lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI ... more

Fork of DMSupport by EmbeddedArtists AB

Revision:
42:bbfe299d4a0c
Parent:
41:e06e764ff4fd
--- a/FileSystems/MCIFileSystem.cpp	Wed Oct 23 06:59:29 2019 +0000
+++ b/FileSystems/MCIFileSystem.cpp	Mon Nov 04 14:32:50 2019 +0000
@@ -581,12 +581,11 @@
  * Public Functions
  *****************************************************************************/
 
-MCIFileSystem::MCIFileSystem(const char* name, PinName cd) :
-    FATFileSystem(name)
+MCIFileSystem::MCIFileSystem(PinName cd) : 
+    _init_ref_count(0), _is_initialized(false)
 {
     pUglyForIRQ = this;
 
-    _Stat = STA_NOINIT;
     memset(&_sdCardInfo, 0, sizeof(SDMMC_CARD_T));
     _eventReceived = false;
     _eventSuccess = false;
@@ -670,90 +669,131 @@
     _eventDmaChannel = GPDMA::instance().acquireChannel(mydmairq);
 }
 
-int MCIFileSystem::disk_initialize() {
+int MCIFileSystem::init() {
+    uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
+        
+    if (val != 1) {
+        return BD_ERROR_OK;
+    }
 
-    debug_if(MCI_DBG, "mcifs:disk_initialize(), _Stat = %#x\n", _Stat);
+    debug_if(MCI_DBG, "mcifs:disk_initialize()\n");
 
     if (!cardInserted()) {
-      /* No card in the socket */
-      _Stat = STA_NODISK | STA_NOINIT;
+        debug("No card detected\r\n");
+        return BD_ERROR_DEVICE_ERROR;
     }
 
-    if (_Stat != STA_NOINIT) {
-        return _Stat;          /* card is already enumerated */
-    }
-
-    //rtc_init();
-
     /* Initialize the Card Data Strucutre */
     memset(&_sdCardInfo, 0, sizeof(SDMMC_CARD_T));
 
-    /* Reset */
-    _Stat = STA_NOINIT;
-
     /* Enumerate the card once detected. Note this function may block for a little while. */
     int ret = mci_Acquire();
     if (ret != 1) {
       debug("Card Acquire failed... got %d, but expected 1\r\n", ret);
-      return 1;//Stat;
+      return BD_ERROR_DEVICE_ERROR;
     }
 
-    _Stat &= ~STA_NOINIT;
-    return _Stat;
+    _is_initialized = true;
+    return BD_ERROR_OK;
 }
 
-int MCIFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number, uint8_t count) {
-    debug_if(MCI_DBG, "mcifs:disk_write(%#x, %llu, %u), _Stat = %#x\n", (uint32_t)buffer, block_number, count, _Stat);
-    if (_Stat & STA_NOINIT) {
-        // not ready
-        return 1;
-    }
-    if (mci_WriteBlocks((void*)buffer, block_number, count) == SDC_RET_OK) {
-        return 0;
+int MCIFileSystem::deinit() {
+
+    if (!_is_initialized) {
+        return BD_ERROR_OK;
+    }    
+
+    uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
+
+    if (val) {
+        return BD_ERROR_OK;
     }
 
-    return 1;
+    _is_initialized = false;
+    return BD_ERROR_OK;
 }
 
-int MCIFileSystem::disk_read(uint8_t *buffer, uint64_t block_number, uint8_t count) {
-    debug_if(MCI_DBG, "mcifs:disk_read(%#x, %llu, %u), _Stat = %#x\n", (uint32_t)buffer, block_number, count, _Stat);
-    if (_Stat & STA_NOINIT) {
-        // not ready
-        return _Stat;
+int MCIFileSystem::program(const void *buffer, bd_addr_t addr, bd_size_t size) {
+    ReturnCode status;
+    
+    debug_if(MCI_DBG, "mcifs:disk_write(%#x, %llu, %llu)\n", (uint32_t)buffer, addr, size);
+    
+    if (!_is_initialized) {
+        return BD_ERROR_DEVICE_ERROR;
     }
-    if (mci_ReadBlocks(buffer, block_number, count) == SDC_RET_OK) {
-        return 0;
+    
+    /* need to convert from number of bytes to blocks */
+    addr = addr / MMC_SECTOR_SIZE;
+    size = size / MMC_SECTOR_SIZE;
+    
+    status = mci_WriteBlocks((void*)buffer, addr, size);
+    if (status != SDC_RET_OK) {
+        return status;
     }
 
-    return 1;
+    return BD_ERROR_OK;
 }
 
-int MCIFileSystem::disk_status()
-{
-  debug_if(MCI_DBG, "mcifs:disk_status(), _Stat = %#x\n", _Stat);
-  return _Stat;
+int MCIFileSystem::read(void *buffer, bd_addr_t addr, bd_size_t size) {
+    ReturnCode status;
+    debug_if(MCI_DBG, "mcifs:disk_read(%#x, %llu, %llu)\n", (uint32_t)buffer, addr, size);
+    
+    if (!_is_initialized) {
+        return BD_ERROR_DEVICE_ERROR;
+    }
+    
+    /* need to convert from number of bytes to blocks */
+    addr = addr / MMC_SECTOR_SIZE;
+    size = size / MMC_SECTOR_SIZE;    
+    
+    status = mci_ReadBlocks(buffer, addr, size);
+    if (status != SDC_RET_OK) {
+        return status;
+    }
+
+    return BD_ERROR_OK;
 }
 
-int MCIFileSystem::disk_sync()
+int MCIFileSystem::sync()
 {
-  debug_if(MCI_DBG, "mcifs:disk_sync(), _Stat = %#x\n", _Stat);
-  uint32_t end = us_ticker_read() + 50*1000; // 50ms
-  while (us_ticker_read() < end)
-  {
-    if (mci_GetCardStatus() & R1_READY_FOR_DATA)
+    debug_if(MCI_DBG, "mcifs:disk_sync()\n");
+  
+    if (!_is_initialized) {
+        return BD_ERROR_OK;
+    }  
+  
+    uint32_t end = us_ticker_read() + 50*1000; // 50ms
+    while (us_ticker_read() < end)
     {
-      // card is ready
-      return 0;
+        if (mci_GetCardStatus() & R1_READY_FOR_DATA)
+        {
+            // card is ready
+            return BD_ERROR_OK;
+        }
     }
-  }
-  // timeout while waiting for card to get ready
-  return 1;
+    
+    // timeout while waiting for card to get ready
+    return SDC_RET_TIMEOUT;
 }
 
-uint64_t MCIFileSystem::disk_sectors()
+bd_size_t MCIFileSystem::get_read_size() const
+{
+    return MMC_SECTOR_SIZE;
+}
+
+bd_size_t MCIFileSystem::get_program_size() const
 {
-    debug_if(MCI_DBG, "mcifs:disk_sectors(), _Stat = %#x, returning %u\n", _Stat, _sdCardInfo.blocknr);
-    return _sdCardInfo.blocknr;
+    return MMC_SECTOR_SIZE;
+}
+
+bd_size_t MCIFileSystem::size() const
+{
+    return _sdCardInfo.block_len * MMC_SECTOR_SIZE;
+}
+
+const char *MCIFileSystem::get_type() const
+{
+    return "MCI";
 }
 
 void MCIFileSystem::mci_MCIIRQHandler()
@@ -1386,7 +1426,6 @@
 
   /* compute block length based on CSD response */
   _sdCardInfo.block_len = 1 << mci_GetBits(80, 83, _sdCardInfo.csd);
-
   if ((_sdCardInfo.card_type & CARD_TYPE_HC) && (_sdCardInfo.card_type & CARD_TYPE_SD)) {
     /* See section 5.3.3 CSD Register (CSD Version 2.0) of SD2.0 spec  an explanation for the calculation of these values */
     CSize = mci_GetBits(48, 63, (uint32_t *) _sdCardInfo.csd) + 1;
@@ -1778,6 +1817,7 @@
     // If the driver is having problems reading the card, adding a delay here
     // might help.
     //wait(0.01);
+    ThisThread::sleep_for(10);
   }
   
   if (_eventReceived && _eventSuccess) {