diskio.c
Go to the documentation of this file.
1 
33 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 
41 #include "compiler.h"
42 #include "diskio.h"
43 #include "ctrl_access.h"
44 
45 #include <string.h>
46 #include <stdio.h>
47 #include <assert.h>
48 
49 #if (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4N)
50 # include <rtc.h>
51 #endif
52 
53 #if (SAM0)
54 # include <rtc_calendar.h>
55 struct rtc_module rtc_instance;
56 
57 static void configure_rtc_calendar(void)
58 {
59  /* Initialize RTC in calendar mode. */
60  struct rtc_calendar_config config_rtc_calendar;
61 
62  rtc_calendar_get_config_defaults(&config_rtc_calendar);
63 
64  struct rtc_calendar_time init_time;
65  rtc_calendar_get_time_defaults(&init_time);
66  init_time.year = 2014;
67  init_time.month = 1;
68  init_time.day = 1;
69  init_time.hour = 0;
70  init_time.minute = 0;
71  init_time.second = 4;
72 
73  config_rtc_calendar.clock_24h = true;
74  config_rtc_calendar.alarm[0].time = init_time;
75  config_rtc_calendar.alarm[0].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
76 
77  rtc_calendar_init(&rtc_instance, RTC, &config_rtc_calendar);
78 
79  rtc_calendar_enable(&rtc_instance);
80 }
81 #endif
82 
93 #define SECTOR_SIZE_DEFAULT 512
94 
97 #define SECTOR_SIZE_512 1
98 #define SECTOR_SIZE_1024 2
99 #define SECTOR_SIZE_2048 4
100 #define SECTOR_SIZE_4096 8
101 
111 {
112  int i;
113  Ctrl_status mem_status;
114 
115 #if (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S)
116  /* Default RTC configuration, 24-hour mode */
118 #endif
119 
120 #if (SAMD20 || SAMD21 || SAMR21)
121  configure_rtc_calendar();
122 #endif
123 
124 #if LUN_USB
125  /* USB disk with multiple LUNs */
126  if (drv > LUN_ID_USB + Lun_usb_get_lun()) {
127  return STA_NOINIT;
128  }
129 #else
130  if (drv > MAX_LUN) {
131  /* At least one of the LUN should be defined */
132  return STA_NOINIT;
133  }
134 #endif
135  /* Check LUN ready (USB disk report CTRL_BUSY then CTRL_GOOD) */
136  for (i = 0; i < 2; i ++) {
137  mem_status = mem_test_unit_ready(drv);
138  if (CTRL_BUSY != mem_status) {
139  break;
140  }
141  }
142  if (mem_status != CTRL_GOOD) {
143  return STA_NOINIT;
144  }
145 
146  /* Check Write Protection Status */
147  if (mem_wr_protect(drv)) {
148  return STA_PROTECT;
149  }
150 
151  /* The memory should already be initialized */
152  return 0;
153 }
154 
164 {
165  switch (mem_test_unit_ready(drv)) {
166  case CTRL_GOOD:
167  return 0;
168  case CTRL_NO_PRESENT:
169  return STA_NOINIT | STA_NODISK;
170  default:
171  return STA_NOINIT;
172  }
173 }
174 
185 DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
186 {
187 #if ACCESS_MEM_TO_RAM
188  uint8_t uc_sector_size = mem_sector_size(drv);
189  uint32_t i;
190  uint32_t ul_last_sector_num;
191 
192  if (uc_sector_size == 0) {
193  return RES_ERROR;
194  }
195 
196  /* Check valid address */
197  mem_read_capacity(drv, &ul_last_sector_num);
198  if ((sector + count * uc_sector_size) >
199  (ul_last_sector_num + 1) * uc_sector_size) {
200  return RES_PARERR;
201  }
202 
203  /* Read the data */
204  for (i = 0; i < count; i++) {
205  if (memory_2_ram(drv, sector + uc_sector_size * i,
206  buff + uc_sector_size * SECTOR_SIZE_DEFAULT * i) !=
207  CTRL_GOOD) {
208  return RES_ERROR;
209  }
210  }
211 
212  return RES_OK;
213 
214 #else
215  return RES_ERROR;
216 #endif
217 }
218 
234 #if _READONLY == 0
235 DRESULT disk_write(BYTE drv, BYTE const *buff, DWORD sector, BYTE count)
236 {
237 #if ACCESS_MEM_TO_RAM
238  uint8_t uc_sector_size = mem_sector_size(drv);
239  uint32_t i;
240  uint32_t ul_last_sector_num;
241 
242  if (uc_sector_size == 0) {
243  return RES_ERROR;
244  }
245 
246  /* Check valid address */
247  mem_read_capacity(drv, &ul_last_sector_num);
248  if ((sector + count * uc_sector_size) >
249  (ul_last_sector_num + 1) * uc_sector_size) {
250  return RES_PARERR;
251  }
252 
253  /* Write the data */
254  for (i = 0; i < count; i++) {
255  if (ram_2_memory(drv, sector + uc_sector_size * i,
256  buff + uc_sector_size * SECTOR_SIZE_DEFAULT * i) !=
257  CTRL_GOOD) {
258  return RES_ERROR;
259  }
260  }
261 
262  return RES_OK;
263 
264 #else
265  return RES_ERROR;
266 #endif
267 }
268 
269 #endif /* _READONLY */
270 
296 DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
297 {
298  DRESULT res = RES_PARERR;
299 
300  switch (ctrl) {
301  case GET_BLOCK_SIZE:
302  *(DWORD *)buff = 1;
303  res = RES_OK;
304  break;
305 
306  /* Get the number of sectors on the disk (DWORD) */
307  case GET_SECTOR_COUNT:
308  {
309  uint32_t ul_last_sector_num;
310 
311  /* Check valid address */
312  mem_read_capacity(drv, &ul_last_sector_num);
313 
314  *(DWORD *)buff = ul_last_sector_num + 1;
315 
316  res = RES_OK;
317  }
318  break;
319 
320  /* Get sectors on the disk (WORD) */
321  case GET_SECTOR_SIZE:
322  {
323  uint8_t uc_sector_size = mem_sector_size(drv);
324 
325  if ((uc_sector_size != SECTOR_SIZE_512) &&
326  (uc_sector_size != SECTOR_SIZE_1024) &&
327  (uc_sector_size != SECTOR_SIZE_2048) &&
328  (uc_sector_size != SECTOR_SIZE_4096)) {
329  /* The sector size is not supported by the FatFS */
330  return RES_ERROR;
331  }
332 
333  *(U8 *)buff = uc_sector_size * SECTOR_SIZE_DEFAULT;
334 
335  res = RES_OK;
336  }
337  break;
338 
339  /* Make sure that data has been written */
340  case CTRL_SYNC:
341  if (mem_test_unit_ready(drv) == CTRL_GOOD) {
342  res = RES_OK;
343  } else {
344  res = RES_NOTRDY;
345  }
346  break;
347 
348  default:
349  res = RES_PARERR;
350  }
351 
352  return res;
353 }
354 
356 
358 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 
DRESULT disk_write(BYTE drv, BYTE const *buff, DWORD sector, BYTE count)
Write sector(s).
Definition: diskio.c:235
#define GET_BLOCK_SIZE
Definition: diskio.h:53
#define SECTOR_SIZE_DEFAULT
Definition: diskio.c:93
#define STA_NOINIT
Definition: diskio.h:42
DSTATUS disk_status(BYTE drv)
Return disk status.
Definition: diskio.c:163
#define SECTOR_SIZE_512
Definition: diskio.c:97
unsigned char BYTE
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
Read sector(s).
Definition: diskio.c:185
#define STA_PROTECT
Definition: diskio.h:44
#define Lun_usb_get_lun()
Memory not initialized or changed.
Definition: ctrl_access.h:79
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:3206
Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram)
Copies 1 data sector from RAM to the memory.
Definition: ctrl_access.c:557
Ctrl_status mem_test_unit_ready(U8 lun)
Tests the memory state and initializes the memory if required.
Definition: ctrl_access.c:308
U8 mem_sector_size(U8 lun)
Returns the size of the physical sector.
Definition: ctrl_access.c:352
#define GET_SECTOR_COUNT
Definition: diskio.h:51
Commonly used includes, types and macros.
unsigned long DWORD
Definition: integer.h:33
Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram)
Copies 1 data sector from the memory to RAM.
Definition: ctrl_access.c:530
BYTE DSTATUS
Definition: diskio.h:14
DRESULT
Definition: diskio.h:17
Definition: diskio.h:18
#define MAX_LUN
Number of static LUNs.
Definition: ctrl_access.h:125
#define SECTOR_SIZE_4096
Definition: diskio.c:100
Ctrl_status
Status returned by CTRL_ACCESS interfaces.
Definition: ctrl_access.h:74
Success, memory ready.
Definition: ctrl_access.h:76
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
Miscellaneous functions, which support the following commands:
Definition: diskio.c:296
#define GET_SECTOR_SIZE
Definition: diskio.h:52
bool mem_wr_protect(U8 lun)
Returns the write-protection state of the memory.
Definition: ctrl_access.c:404
Memory unplugged.
Definition: ctrl_access.h:78
uint8_t U8
8-bit unsigned integer.
Definition: compiler.h:247
#define CTRL_SYNC
Definition: diskio.h:50
#define LUN_ID_USB
Definition: ctrl_access.h:126
void rtc_set_hour_mode(Rtc *p_rtc, uint32_t ul_mode)
Set the RTC hour mode.
Definition: rtc.c:77
#define STA_NODISK
Definition: diskio.h:43
#define SECTOR_SIZE_2048
Definition: diskio.c:99
#define RTC
(RTC ) Base Address
Definition: same70j19.h:536
DSTATUS disk_initialize(BYTE drv)
Initialize a disk.
Definition: diskio.c:110
#define SECTOR_SIZE_1024
Definition: diskio.c:98
Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector)
Returns the address of the last valid sector (512 bytes) in the memory.
Definition: ctrl_access.c:330


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57