SD card data log

Dependents:   riset_Datalogger

Committer:
kotachi
Date:
Sun Nov 03 07:28:09 2019 +0000
Revision:
0:18d80d7403d3
SD card logging system

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kotachi 0:18d80d7403d3 1 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
kotachi 0:18d80d7403d3 3 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 4 /* If a working storage control module is available, it should be */
kotachi 0:18d80d7403d3 5 /* attached to the FatFs via a glue function rather than modifying it. */
kotachi 0:18d80d7403d3 6 /* This is an example of glue functions to attach various exsisting */
kotachi 0:18d80d7403d3 7 /* storage control modules to the FatFs module with a defined API. */
kotachi 0:18d80d7403d3 8 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 9
kotachi 0:18d80d7403d3 10 #include "diskio.h"
kotachi 0:18d80d7403d3 11 #include "mbed_debug.h"
kotachi 0:18d80d7403d3 12 #include "FATFileSystem.h"
kotachi 0:18d80d7403d3 13
kotachi 0:18d80d7403d3 14 using namespace mbed;
kotachi 0:18d80d7403d3 15
kotachi 0:18d80d7403d3 16 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 17 /* Get Drive Status */
kotachi 0:18d80d7403d3 18 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 19
kotachi 0:18d80d7403d3 20 DSTATUS disk_status (
kotachi 0:18d80d7403d3 21 BYTE pdrv /* Physical drive nmuber to identify the drive */
kotachi 0:18d80d7403d3 22 )
kotachi 0:18d80d7403d3 23 {
kotachi 0:18d80d7403d3 24 debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
kotachi 0:18d80d7403d3 25 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status();
kotachi 0:18d80d7403d3 26 }
kotachi 0:18d80d7403d3 27
kotachi 0:18d80d7403d3 28 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 29 /* Inidialize a Drive */
kotachi 0:18d80d7403d3 30 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 31
kotachi 0:18d80d7403d3 32 DSTATUS disk_initialize (
kotachi 0:18d80d7403d3 33 BYTE pdrv /* Physical drive nmuber to identify the drive */
kotachi 0:18d80d7403d3 34 )
kotachi 0:18d80d7403d3 35 {
kotachi 0:18d80d7403d3 36 debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv);
kotachi 0:18d80d7403d3 37 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize();
kotachi 0:18d80d7403d3 38 }
kotachi 0:18d80d7403d3 39
kotachi 0:18d80d7403d3 40 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 41 /* Read Sector(s) */
kotachi 0:18d80d7403d3 42 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 43
kotachi 0:18d80d7403d3 44 DRESULT disk_read (
kotachi 0:18d80d7403d3 45 BYTE pdrv, /* Physical drive nmuber to identify the drive */
kotachi 0:18d80d7403d3 46 BYTE* buff, /* Data buffer to store read data */
kotachi 0:18d80d7403d3 47 DWORD sector, /* Sector address in LBA */
kotachi 0:18d80d7403d3 48 UINT count /* Number of sectors to read */
kotachi 0:18d80d7403d3 49 )
kotachi 0:18d80d7403d3 50 {
kotachi 0:18d80d7403d3 51 debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
kotachi 0:18d80d7403d3 52 if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count))
kotachi 0:18d80d7403d3 53 return RES_PARERR;
kotachi 0:18d80d7403d3 54 else
kotachi 0:18d80d7403d3 55 return RES_OK;
kotachi 0:18d80d7403d3 56 }
kotachi 0:18d80d7403d3 57
kotachi 0:18d80d7403d3 58 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 59 /* Write Sector(s) */
kotachi 0:18d80d7403d3 60 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 61
kotachi 0:18d80d7403d3 62 #if _USE_WRITE
kotachi 0:18d80d7403d3 63 DRESULT disk_write (
kotachi 0:18d80d7403d3 64 BYTE pdrv, /* Physical drive nmuber to identify the drive */
kotachi 0:18d80d7403d3 65 const BYTE* buff, /* Data to be written */
kotachi 0:18d80d7403d3 66 DWORD sector, /* Sector address in LBA */
kotachi 0:18d80d7403d3 67 UINT count /* Number of sectors to write */
kotachi 0:18d80d7403d3 68 )
kotachi 0:18d80d7403d3 69 {
kotachi 0:18d80d7403d3 70 debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
kotachi 0:18d80d7403d3 71 if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count))
kotachi 0:18d80d7403d3 72 return RES_PARERR;
kotachi 0:18d80d7403d3 73 else
kotachi 0:18d80d7403d3 74 return RES_OK;
kotachi 0:18d80d7403d3 75 }
kotachi 0:18d80d7403d3 76 #endif
kotachi 0:18d80d7403d3 77
kotachi 0:18d80d7403d3 78 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 79 /* Miscellaneous Functions */
kotachi 0:18d80d7403d3 80 /*-----------------------------------------------------------------------*/
kotachi 0:18d80d7403d3 81
kotachi 0:18d80d7403d3 82 #if _USE_IOCTL
kotachi 0:18d80d7403d3 83 DRESULT disk_ioctl (
kotachi 0:18d80d7403d3 84 BYTE pdrv, /* Physical drive nmuber (0..) */
kotachi 0:18d80d7403d3 85 BYTE cmd, /* Control code */
kotachi 0:18d80d7403d3 86 void* buff /* Buffer to send/receive control data */
kotachi 0:18d80d7403d3 87 )
kotachi 0:18d80d7403d3 88 {
kotachi 0:18d80d7403d3 89 debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd);
kotachi 0:18d80d7403d3 90 switch(cmd) {
kotachi 0:18d80d7403d3 91 case CTRL_SYNC:
kotachi 0:18d80d7403d3 92 if(FATFileSystem::_ffs[pdrv] == NULL) {
kotachi 0:18d80d7403d3 93 return RES_NOTRDY;
kotachi 0:18d80d7403d3 94 } else if(FATFileSystem::_ffs[pdrv]->disk_sync()) {
kotachi 0:18d80d7403d3 95 return RES_ERROR;
kotachi 0:18d80d7403d3 96 }
kotachi 0:18d80d7403d3 97 return RES_OK;
kotachi 0:18d80d7403d3 98 case GET_SECTOR_COUNT:
kotachi 0:18d80d7403d3 99 if(FATFileSystem::_ffs[pdrv] == NULL) {
kotachi 0:18d80d7403d3 100 return RES_NOTRDY;
kotachi 0:18d80d7403d3 101 } else {
kotachi 0:18d80d7403d3 102 DWORD res = FATFileSystem::_ffs[pdrv]->disk_sectors();
kotachi 0:18d80d7403d3 103 if(res > 0) {
kotachi 0:18d80d7403d3 104 *((DWORD*)buff) = res; // minimum allowed
kotachi 0:18d80d7403d3 105 return RES_OK;
kotachi 0:18d80d7403d3 106 } else {
kotachi 0:18d80d7403d3 107 return RES_ERROR;
kotachi 0:18d80d7403d3 108 }
kotachi 0:18d80d7403d3 109 }
kotachi 0:18d80d7403d3 110 case GET_BLOCK_SIZE:
kotachi 0:18d80d7403d3 111 *((DWORD*)buff) = 1; // default when not known
kotachi 0:18d80d7403d3 112 return RES_OK;
kotachi 0:18d80d7403d3 113
kotachi 0:18d80d7403d3 114 }
kotachi 0:18d80d7403d3 115 return RES_PARERR;
kotachi 0:18d80d7403d3 116 }
kotachi 0:18d80d7403d3 117 #endif