fatfs_port.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2017 Damien P. George
  7. * Parts of this file are (C)ChaN, 2014, from FatFs option/syscall.c
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/runtime.h"
  28. #include "lib/oofatfs/ff.h"
  29. #include "lib/timeutils/timeutils.h"
  30. #include "mods/pybrtc.h"
  31. #if _FS_REENTRANT
  32. // Create a Synchronization Object
  33. // This function is called in f_mount() function to create a new
  34. // synchronization object, such as semaphore and mutex.
  35. // A return of 0 indicates failure, and then f_mount() fails with FR_INT_ERR.
  36. int ff_cre_syncobj(FATFS *fatfs, _SYNC_t *sobj) {
  37. vSemaphoreCreateBinary((*sobj));
  38. return (int)(*sobj != NULL);
  39. }
  40. // Delete a Synchronization Object
  41. // This function is called in f_mount() function to delete a synchronization
  42. // object that created with ff_cre_syncobj function.
  43. // A return of 0 indicates failure, and then f_mount() fails with FR_INT_ERR.
  44. int ff_del_syncobj(_SYNC_t sobj) {
  45. vSemaphoreDelete(sobj);
  46. return 1;
  47. }
  48. // Request Grant to Access the Volume
  49. // This function is called on entering file functions to lock the volume.
  50. // When a 0 is returned, the file function fails with FR_TIMEOUT.
  51. int ff_req_grant(_SYNC_t sobj) {
  52. return (int)(xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE);
  53. }
  54. // Release Grant to Access the Volume
  55. // This function is called on leaving file functions to unlock the volume.
  56. void ff_rel_grant(_SYNC_t sobj) {
  57. xSemaphoreGive(sobj);
  58. }
  59. #endif
  60. DWORD get_fattime(void) {
  61. timeutils_struct_time_t tm;
  62. timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), &tm);
  63. return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) |
  64. ((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) |
  65. ((tm.tm_min) << 5) | (tm.tm_sec >> 1);
  66. }