re-enabling serial
[clinton/Smoothieware.git] / src / libs / ChaNFS / CHAN_FS / diskio.c
1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
3 /*-----------------------------------------------------------------------*/
4 /* This is a stub disk I/O module that acts as front end of the existing */
5 /* disk I/O modules and attach it to FatFs module with common interface. */
6 /*-----------------------------------------------------------------------*/
7
8 #include "diskio.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include "FATFileSystem.h"
12
13 #include "mbed.h"
14
15 DSTATUS disk_initialize (
16 BYTE drv /* Physical drive nmuber (0..) */
17 )
18 {
19 FFSDEBUG("disk_initialize on drv [%d]\n", drv);
20 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize();
21 }
22
23 DSTATUS disk_status (
24 BYTE drv /* Physical drive nmuber (0..) */
25 )
26 {
27 FFSDEBUG("disk_status on drv [%d]\n", drv);
28 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status();
29 }
30
31 DRESULT disk_read (
32 BYTE drv, /* Physical drive nmuber (0..) */
33 BYTE *buff, /* Data buffer to store read data */
34 DWORD sector, /* Sector address (LBA) */
35 BYTE count /* Number of sectors to read (1..255) */
36 )
37 {
38 FFSDEBUG("disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv);
39 for(int s=sector; s<sector+count; s++) {
40 FFSDEBUG(" disk_read(sector %d)\n", s);
41 int res = FATFileSystem::_ffs[drv]->disk_read((char*)buff, s);
42 if(res) {
43 return RES_PARERR;
44 }
45 buff += 512;
46 }
47 return RES_OK;
48 }
49
50 #if _READONLY == 0
51 DRESULT disk_write (
52 BYTE drv, /* Physical drive nmuber (0..) */
53 const BYTE *buff, /* Data to be written */
54 DWORD sector, /* Sector address (LBA) */
55 BYTE count /* Number of sectors to write (1..255) */
56 )
57 {
58 FFSDEBUG("disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv);
59 for(int s=sector; s<sector+count; s++) {
60 FFSDEBUG(" disk_write(sector %d)\n", s);
61 int res = FATFileSystem::_ffs[drv]->disk_write((char*)buff, sector);
62 if(res) {
63 return RES_PARERR;
64 }
65 buff += 512;
66 }
67 return RES_OK;
68 }
69 #endif /* _READONLY */
70
71 DRESULT disk_ioctl (
72 BYTE drv, /* Physical drive nmuber (0..) */
73 BYTE ctrl, /* Control code */
74 void *buff /* Buffer to send/receive control data */
75 )
76 {
77 FFSDEBUG("disk_ioctl(%d)\n", ctrl);
78 switch(ctrl) {
79 case CTRL_SYNC:
80 if(FATFileSystem::_ffs[drv] == NULL) {
81 return RES_NOTRDY;
82 } else if(FATFileSystem::_ffs[drv]->disk_sync()) {
83 return RES_ERROR;
84 }
85 return RES_OK;
86 case GET_SECTOR_COUNT:
87 if(FATFileSystem::_ffs[drv] == NULL) {
88 return RES_NOTRDY;
89 } else {
90 int res = FATFileSystem::_ffs[drv]->disk_sectors();
91 if(res > 0) {
92 *((DWORD*)buff) = res; // minimum allowed
93 return RES_OK;
94 } else {
95 return RES_ERROR;
96 }
97 }
98 case GET_BLOCK_SIZE:
99 *((DWORD*)buff) = 1; // default when not known
100 return RES_OK;
101
102 }
103 return RES_PARERR;
104 }
105