source: S-port/trunk/FATFS/Target/user_diskio.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 6.3 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file user_diskio.c
5 * @brief This file includes a diskio driver skeleton to be completed by the user.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under Ultimate Liberty license
13 * SLA0044, the "License"; You may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at:
15 * www.st.com/SLA0044
16 *
17 ******************************************************************************
18 */
19 /* USER CODE END Header */
20
21#ifdef USE_OBSOLETE_USER_CODE_SECTION_0
22/*
23 * Warning: the user section 0 is no more in use (starting from CubeMx version 4.16.0)
24 * To be suppressed in the future.
25 * Kept to ensure backward compatibility with previous CubeMx versions when
26 * migrating projects.
27 * User code previously added there should be copied in the new user sections before
28 * the section contents can be deleted.
29 */
30/* USER CODE BEGIN 0 */
31/* USER CODE END 0 */
32#endif
33
34/* USER CODE BEGIN DECL */
35
36/* Includes ------------------------------------------------------------------*/
37#include <string.h>
38#include "ff_gen_drv.h"
39#include "AT45DB.h"
40
41/* Private typedef -----------------------------------------------------------*/
42/* Private define ------------------------------------------------------------*/
43
44/* Private variables ---------------------------------------------------------*/
45/* Disk status */
46static volatile DSTATUS Stat = STA_NOINIT;
47
48/* USER CODE END DECL */
49
50/* Private function prototypes -----------------------------------------------*/
51DSTATUS USER_initialize (BYTE pdrv);
52DSTATUS USER_status (BYTE pdrv);
53DRESULT USER_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
54#if _USE_WRITE == 1
55 DRESULT USER_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
56#endif /* _USE_WRITE == 1 */
57#if _USE_IOCTL == 1
58 DRESULT USER_ioctl (BYTE pdrv, BYTE cmd, void *buff);
59#endif /* _USE_IOCTL == 1 */
60
61Diskio_drvTypeDef USER_Driver =
62{
63 USER_initialize,
64 USER_status,
65 USER_read,
66#if _USE_WRITE
67 USER_write,
68#endif /* _USE_WRITE == 1 */
69#if _USE_IOCTL == 1
70 USER_ioctl,
71#endif /* _USE_IOCTL == 1 */
72};
73
74/* Private functions ---------------------------------------------------------*/
75
76/**
77 * @brief Initializes a Drive
78 * @param pdrv: Physical drive number (0..)
79 * @retval DSTATUS: Operation status
80 */
81DSTATUS USER_initialize (
82 BYTE pdrv /* Physical drive nmuber to identify the drive */
83)
84{
85 /* USER CODE BEGIN INIT */
86 //CDC_Transmit_FS((uint8_t*)"init\r\n", strlen("init\r\n"));
87 Stat &= ~STA_NOINIT;
88 return Stat;
89 /* USER CODE END INIT */
90}
91
92/**
93 * @brief Gets Disk Status
94 * @param pdrv: Physical drive number (0..)
95 * @retval DSTATUS: Operation status
96 */
97DSTATUS USER_status (
98 BYTE pdrv /* Physical drive number to identify the drive */
99)
100{
101 /* USER CODE BEGIN STATUS */
102 if (pdrv) return STA_NOINIT;
103 return Stat;
104 /* USER CODE END STATUS */
105}
106
107/**
108 * @brief Reads Sector(s)
109 * @param pdrv: Physical drive number (0..)
110 * @param *buff: Data buffer to store read data
111 * @param sector: Sector address (LBA)
112 * @param count: Number of sectors to read (1..128)
113 * @retval DRESULT: Operation result
114 */
115DRESULT USER_read (
116 BYTE pdrv, /* Physical drive nmuber to identify the drive */
117 BYTE *buff, /* Data buffer to store read data */
118 DWORD sector, /* Sector address in LBA */
119 UINT count /* Number of sectors to read */
120)
121{
122 /* USER CODE BEGIN READ */
123 if (pdrv || !count) return RES_PARERR;
124 if (Stat & STA_NOINIT) return RES_NOTRDY;
125
126 uint32_t cr = 0;
127 for(uint16_t i = 0; i < count; i ++){
128 Flash_Read(((sector + i) << 9), 512, &buff[cr]);
129 cr += 512;
130 }
131 count = 0;
132 return count ? RES_ERROR : RES_OK;
133 /* USER CODE END READ */
134}
135
136/**
137 * @brief Writes Sector(s)
138 * @param pdrv: Physical drive number (0..)
139 * @param *buff: Data to be written
140 * @param sector: Sector address (LBA)
141 * @param count: Number of sectors to write (1..128)
142 * @retval DRESULT: Operation result
143 */
144#if _USE_WRITE == 1
145DRESULT USER_write (
146 BYTE pdrv, /* Physical drive nmuber to identify the drive */
147 const BYTE *buff, /* Data to be written */
148 DWORD sector, /* Sector address in LBA */
149 UINT count /* Number of sectors to write */
150)
151{
152 /* USER CODE BEGIN WRITE */
153 /* USER CODE HERE */
154 if (pdrv || !count) return RES_PARERR;
155 if (Stat & STA_NOINIT) return RES_NOTRDY;
156 if (Stat & STA_PROTECT) return RES_WRPRT;
157
158 uint32_t cr = 0;
159
160 for(uint16_t i = 0; i < count; i ++){
161 Flash_ByteWrite(((sector + i) << 9), 512, (BYTE*)&buff[cr]); //—÷èòàåì áëîê â áóôåð
162 cr += 512;
163 }
164 count = 0;
165 return count ? RES_ERROR : RES_OK;
166 /* USER CODE END WRITE */
167}
168#endif /* _USE_WRITE == 1 */
169
170/**
171 * @brief I/O control operation
172 * @param pdrv: Physical drive number (0..)
173 * @param cmd: Control code
174 * @param *buff: Buffer to send/receive control data
175 * @retval DRESULT: Operation result
176 */
177#if _USE_IOCTL == 1
178DRESULT USER_ioctl (
179 BYTE pdrv, /* Physical drive nmuber (0..) */
180 BYTE cmd, /* Control code */
181 void *buff /* Buffer to send/receive control data */
182)
183{
184 /* USER CODE BEGIN IOCTL */
185 DRESULT res = RES_ERROR;
186 if (pdrv) return RES_PARERR;
187 if (Stat & STA_NOINIT) return RES_NOTRDY;
188 res = RES_ERROR;
189 switch (cmd)
190 {
191 case GET_SECTOR_COUNT:
192 *(DWORD*)buff = FLASH_SECTOR_COUNT;
193 res = RES_OK;
194 break;
195 case GET_BLOCK_SIZE :
196 *(DWORD*)buff = FLASH_BLOCK_SIZE;
197 res = RES_OK;
198 break;
199 case CTRL_SYNC : /* Flush dirty buffer if present */
200 Flash_StatusRDY();
201 res = RES_OK;
202 break;
203 case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
204 *(WORD*)buff = FLASH_SECTOR_SIZE;
205 res = RES_OK;
206 break;
207 default:
208 res = RES_PARERR;
209 }
210 return res;
211 /* USER CODE END IOCTL */
212}
213#endif /* _USE_IOCTL == 1 */
214
215/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.