mxpopstar
2022-05-04 fc9a39919da8db88c68ee1c41c01f6c26b88f12c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*!
    \file    usbh_msc_fatfs.c
    \brief   USB MSC host FATFS related functions
 
    \version 2020-08-01, V3.0.0, firmware for GD32F30x
*/
 
/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.
 
    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/
 
#include "usb_conf.h"
#include "diskio.h"
#include "usbh_msc_core.h"
 
static volatile DSTATUS state = STA_NOINIT; /* disk status */
 
extern usbh_host usb_host;
 
/*!
    \brief      initialize the disk drive
    \param[in]  drv: physical drive number (0)
    \param[out] none
    \retval     operation status
*/
DSTATUS disk_initialize (BYTE drv)
{
    usb_core_driver *pudev = (usb_core_driver *)usb_host.data;
 
    if (pudev->host.connect_status) {
        state &= ~STA_NOINIT;
    }
 
    return state;
}
 
/*!
    \brief      get disk status
    \param[in]  drv: physical drive number (0)
    \param[out] none
    \retval     operation status
*/
DSTATUS disk_status (BYTE drv)
{
    if (drv) {
        return STA_NOINIT; /* supports only single drive */
    }
 
    return state;
}
 
/*!
    \brief      read sectors
    \param[in]  drv: physical drive number (0)
    \param[in]  buff: pointer to the data buffer to store read data
    \param[in]  sector: start sector number (LBA)
    \param[in]  count: sector count (1..255)
    \param[out] none
    \retval     operation status
*/
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, UINT count)
{
    BYTE status = USBH_OK;
    usb_core_driver *pudev = (usb_core_driver *)usb_host.data;
 
    if (drv || (!count)) {
        return RES_PARERR;
    }
 
    if (state & STA_NOINIT) {
        return RES_NOTRDY;
    }
 
    if (pudev->host.connect_status) {
        do {
            status = usbh_msc_read (&usb_host, drv, sector, buff, count);
 
            if (!pudev->host.connect_status) {
                return RES_ERROR;
            }
        } while(status == USBH_BUSY);
    }
 
    if (status == USBH_OK) {
        return RES_OK;
    }
 
    return RES_ERROR;
}
 
#if _READONLY == 0U
 
/*!
    \brief      write sectors
    \param[in]  drv: physical drive number (0)
    \param[in]  buff: pointer to the data buffer to store read data
    \param[in]  sector: start sector number (LBA)
    \param[in]  count: sector count (1..255)
    \param[out] none
    \retval     operation status
*/
DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, UINT count)
{
    BYTE status = USBH_OK;
    usb_core_driver *pudev = (usb_core_driver *)usb_host.data;
 
    if ((!count) || drv) {
        return RES_PARERR;
    }
 
    if (state & STA_NOINIT) {
        return RES_NOTRDY;
    }
 
    if (state & STA_PROTECT) {
        return RES_WRPRT;
    }
 
    if (pudev->host.connect_status) {
        do {
            status = usbh_msc_write (&usb_host, drv, sector, (BYTE*)buff, count);
 
            if (!pudev->host.connect_status) {
                return RES_ERROR;
            }
        } while(status == USBH_BUSY);
    }
 
    if (status == USBH_OK) {
        return RES_OK;
    }
 
    return RES_ERROR;
}
 
#endif /* _READONLY == 0 */
 
/*!
    \brief      I/O control function
    \param[in]  drv: physical drive number (0)
    \param[in]  ctrl: control code
    \param[in]  buff: pointer to the data buffer to store read data
    \param[out] none
    \retval     operation status
*/
DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
{
    DRESULT res = RES_OK;
    msc_lun info;
 
    if (drv) {
        return RES_PARERR;
    }
 
    res = RES_ERROR;
 
    if (state & STA_NOINIT) {
        return RES_NOTRDY;
    }
 
    switch (ctrl) {
    /* make sure that no pending write process */
    case CTRL_SYNC:
        res = RES_OK;
        break;
 
    /* get number of sectors on the disk (dword) */
    case GET_SECTOR_COUNT:
        if (USBH_OK == usbh_msc_lun_info_get(&usb_host, drv, &info)) {
            *(DWORD*)buff = (DWORD)info.capacity.block_nbr;
            res = RES_OK;
        }
        break;
 
    /* get r/w sector size (word) */
    case GET_SECTOR_SIZE:
        if (USBH_OK == usbh_msc_lun_info_get(&usb_host, drv, &info)) {
            *(WORD*)buff = (DWORD)info.capacity.block_size;
            res = RES_OK;
        }
        break;
 
    /* get erase block size in unit of sector (dword) */
    case GET_BLOCK_SIZE:
        *(DWORD*)buff = 512;
        break;
 
    default:
        res = RES_PARERR;
        break;
    }
 
    return res;
}
 
/*!
    \brief      get fat time
    \param[in]  none
    \param[out] none
    \retval     time value
*/
DWORD get_fattime(void) {
 
    return    ((DWORD)(2019U - 1980U) << 25U)   /* Year 2019 */
            | ((DWORD)1U << 21U)                /* Month 1 */
            | ((DWORD)1U << 16U)                /* Mday 1 */
            | ((DWORD)0U << 11U)                /* Hour 0 */
            | ((DWORD)0U << 5U)                 /* Min 0 */
            | ((DWORD)0U >> 1U);                /* Sec 0 */
}