├── .gitignore ├── CMakeLists.txt ├── FatFs_SPI ├── CMakeLists.txt ├── ff14a │ ├── LICENSE.txt │ └── source │ │ ├── 00history.txt │ │ ├── 00readme.txt │ │ ├── diskio.c │ │ ├── diskio.h │ │ ├── ff.c │ │ ├── ff.h │ │ ├── ffconf.h │ │ ├── ffsystem.c │ │ └── ffunicode.c ├── include │ ├── f_util.h │ ├── ff_stdio.h │ ├── my_debug.h │ ├── rtc.h │ └── util.h ├── sd_driver │ ├── crc.c │ ├── crc.h │ ├── demo_logging.c │ ├── hw_config.h │ ├── sd_card.c │ ├── sd_card.h │ ├── sd_spi.c │ ├── sd_spi.h │ ├── spi.c │ └── spi.h └── src │ ├── f_util.c │ ├── ff_stdio.c │ ├── glue.c │ ├── my_debug.c │ └── rtc.c ├── LICENSE ├── README.md ├── cmd.c ├── cmd.h ├── hw_config.c ├── i2s.c ├── i2s.h ├── main.c ├── main.pio ├── pico_sdk_import.cmake ├── subq.c ├── subq.h ├── utils.c ├── utils.h └── values.h /.gitignore: -------------------------------------------------------------------------------- 1 | # These are some examples of commonly ignored file patterns. 2 | # You should customize this list as applicable to your project. 3 | # Learn more about .gitignore: 4 | # https://www.atlassian.com/git/tutorials/saving-changes/gitignore 5 | 6 | build/ 7 | # Node artifact files 8 | node_modules/ 9 | dist/ 10 | 11 | # Compiled Java class files 12 | *.class 13 | 14 | # Compiled Python bytecode 15 | *.py[cod] 16 | 17 | # Log files 18 | *.log 19 | 20 | # Package files 21 | *.jar 22 | 23 | # Maven 24 | target/ 25 | dist/ 26 | 27 | # JetBrains IDE 28 | .idea/ 29 | 30 | # Unit test reports 31 | TEST*.xml 32 | 33 | # Generated by MacOS 34 | .DS_Store 35 | 36 | # Generated by Windows 37 | Thumbs.db 38 | 39 | # Applications 40 | *.app 41 | *.exe 42 | *.war 43 | 44 | # Large media files 45 | *.mp4 46 | *.tiff 47 | *.avi 48 | *.flv 49 | *.mov 50 | *.wmv 51 | 52 | 53 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.24.1) 2 | 3 | include(pico_sdk_import.cmake) 4 | 5 | project(picostation) 6 | 7 | pico_sdk_init() 8 | 9 | add_executable(picostation) 10 | 11 | target_sources(picostation PRIVATE main.c utils.c subq.c cmd.c hw_config.c i2s.c) 12 | 13 | add_subdirectory(FatFs_SPI build) 14 | 15 | pico_enable_stdio_usb(picostation 1) 16 | 17 | pico_generate_pio_header(picostation ${CMAKE_CURRENT_LIST_DIR}/main.pio) 18 | 19 | target_link_libraries(picostation PRIVATE pico_stdlib hardware_pio hardware_dma FatFs_SPI pico_multicore) 20 | 21 | pico_set_binary_type(picostation copy_to_ram) 22 | 23 | pico_add_extra_outputs(picostation) 24 | -------------------------------------------------------------------------------- /FatFs_SPI/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(FatFs_SPI INTERFACE) 2 | target_sources(FatFs_SPI INTERFACE 3 | ${CMAKE_CURRENT_LIST_DIR}/ff14a/source/ffsystem.c 4 | ${CMAKE_CURRENT_LIST_DIR}/ff14a/source/ffunicode.c 5 | ${CMAKE_CURRENT_LIST_DIR}/ff14a/source/ff.c 6 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_spi.c 7 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/demo_logging.c 8 | # ${CMAKE_CURRENT_LIST_DIR}/sd_driver/hw_config.c 9 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/spi.c 10 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_card.c 11 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/crc.c 12 | ${CMAKE_CURRENT_LIST_DIR}/src/glue.c 13 | ${CMAKE_CURRENT_LIST_DIR}/src/f_util.c 14 | ${CMAKE_CURRENT_LIST_DIR}/src/ff_stdio.c 15 | ${CMAKE_CURRENT_LIST_DIR}/src/my_debug.c 16 | ${CMAKE_CURRENT_LIST_DIR}/src/rtc.c 17 | ) 18 | target_include_directories(FatFs_SPI INTERFACE 19 | ff14a/source 20 | sd_driver 21 | include 22 | ) 23 | target_link_libraries(FatFs_SPI INTERFACE 24 | hardware_spi 25 | hardware_dma 26 | hardware_rtc 27 | pico_stdlib 28 | ) 29 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/LICENSE.txt: -------------------------------------------------------------------------------- 1 | FatFs License 2 | 3 | FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files. 4 | 5 | /*----------------------------------------------------------------------------/ 6 | / FatFs - Generic FAT Filesystem Module Rx.xx / 7 | /-----------------------------------------------------------------------------/ 8 | / 9 | / Copyright (C) 20xx, ChaN, all right reserved. 10 | / 11 | / FatFs module is an open source software. Redistribution and use of FatFs in 12 | / source and binary forms, with or without modification, are permitted provided 13 | / that the following condition is met: 14 | / 15 | / 1. Redistributions of source code must retain the above copyright notice, 16 | / this condition and the following disclaimer. 17 | / 18 | / This software is provided by the copyright holder and contributors "AS IS" 19 | / and any warranties related to this software are DISCLAIMED. 20 | / The copyright owner or contributors be NOT LIABLE for any damages caused 21 | / by use of this software. 22 | /----------------------------------------------------------------------------*/ 23 | 24 | Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license. 25 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/00history.txt: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | Revision history of FatFs module 3 | ---------------------------------------------------------------------------- 4 | 5 | R0.00 (February 26, 2006) 6 | 7 | Prototype. 8 | 9 | 10 | 11 | R0.01 (April 29, 2006) 12 | 13 | The first release. 14 | 15 | 16 | 17 | R0.02 (June 01, 2006) 18 | 19 | Added FAT12 support. 20 | Removed unbuffered mode. 21 | Fixed a problem on small (<32M) partition. 22 | 23 | 24 | 25 | R0.02a (June 10, 2006) 26 | 27 | Added a configuration option (_FS_MINIMUM). 28 | 29 | 30 | 31 | R0.03 (September 22, 2006) 32 | 33 | Added f_rename(). 34 | Changed option _FS_MINIMUM to _FS_MINIMIZE. 35 | 36 | 37 | 38 | R0.03a (December 11, 2006) 39 | 40 | Improved cluster scan algorithm to write files fast. 41 | Fixed f_mkdir() creates incorrect directory on FAT32. 42 | 43 | 44 | 45 | R0.04 (February 04, 2007) 46 | 47 | Added f_mkfs(). 48 | Supported multiple drive system. 49 | Changed some interfaces for multiple drive system. 50 | Changed f_mountdrv() to f_mount(). 51 | 52 | 53 | 54 | R0.04a (April 01, 2007) 55 | 56 | Supported multiple partitions on a physical drive. 57 | Added a capability of extending file size to f_lseek(). 58 | Added minimization level 3. 59 | Fixed an endian sensitive code in f_mkfs(). 60 | 61 | 62 | 63 | R0.04b (May 05, 2007) 64 | 65 | Added a configuration option _USE_NTFLAG. 66 | Added FSINFO support. 67 | Fixed DBCS name can result FR_INVALID_NAME. 68 | Fixed short seek (<= csize) collapses the file object. 69 | 70 | 71 | 72 | R0.05 (August 25, 2007) 73 | 74 | Changed arguments of f_read(), f_write() and f_mkfs(). 75 | Fixed f_mkfs() on FAT32 creates incorrect FSINFO. 76 | Fixed f_mkdir() on FAT32 creates incorrect directory. 77 | 78 | 79 | 80 | R0.05a (February 03, 2008) 81 | 82 | Added f_truncate() and f_utime(). 83 | Fixed off by one error at FAT sub-type determination. 84 | Fixed btr in f_read() can be mistruncated. 85 | Fixed cached sector is not flushed when create and close without write. 86 | 87 | 88 | 89 | R0.06 (April 01, 2008) 90 | 91 | Added fputc(), fputs(), fprintf() and fgets(). 92 | Improved performance of f_lseek() on moving to the same or following cluster. 93 | 94 | 95 | 96 | R0.07 (April 01, 2009) 97 | 98 | Merged Tiny-FatFs as a configuration option. (_FS_TINY) 99 | Added long file name feature. (_USE_LFN) 100 | Added multiple code page feature. (_CODE_PAGE) 101 | Added re-entrancy for multitask operation. (_FS_REENTRANT) 102 | Added auto cluster size selection to f_mkfs(). 103 | Added rewind option to f_readdir(). 104 | Changed result code of critical errors. 105 | Renamed string functions to avoid name collision. 106 | 107 | 108 | 109 | R0.07a (April 14, 2009) 110 | 111 | Septemberarated out OS dependent code on reentrant cfg. 112 | Added multiple sector size feature. 113 | 114 | 115 | 116 | R0.07c (June 21, 2009) 117 | 118 | Fixed f_unlink() can return FR_OK on error. 119 | Fixed wrong cache control in f_lseek(). 120 | Added relative path feature. 121 | Added f_chdir() and f_chdrive(). 122 | Added proper case conversion to extended character. 123 | 124 | 125 | 126 | R0.07e (November 03, 2009) 127 | 128 | Septemberarated out configuration options from ff.h to ffconf.h. 129 | Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH. 130 | Fixed name matching error on the 13 character boundary. 131 | Added a configuration option, _LFN_UNICODE. 132 | Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. 133 | 134 | 135 | 136 | R0.08 (May 15, 2010) 137 | 138 | Added a memory configuration option. (_USE_LFN = 3) 139 | Added file lock feature. (_FS_SHARE) 140 | Added fast seek feature. (_USE_FASTSEEK) 141 | Changed some types on the API, XCHAR->TCHAR. 142 | Changed .fname in the FILINFO structure on Unicode cfg. 143 | String functions support UTF-8 encoding files on Unicode cfg. 144 | 145 | 146 | 147 | R0.08a (August 16, 2010) 148 | 149 | Added f_getcwd(). (_FS_RPATH = 2) 150 | Added sector erase feature. (_USE_ERASE) 151 | Moved file lock semaphore table from fs object to the bss. 152 | Fixed f_mkfs() creates wrong FAT32 volume. 153 | 154 | 155 | 156 | R0.08b (January 15, 2011) 157 | 158 | Fast seek feature is also applied to f_read() and f_write(). 159 | f_lseek() reports required table size on creating CLMP. 160 | Extended format syntax of f_printf(). 161 | Ignores duplicated directory separators in given path name. 162 | 163 | 164 | 165 | R0.09 (September 06, 2011) 166 | 167 | f_mkfs() supports multiple partition to complete the multiple partition feature. 168 | Added f_fdisk(). 169 | 170 | 171 | 172 | R0.09a (August 27, 2012) 173 | 174 | Changed f_open() and f_opendir() reject null object pointer to avoid crash. 175 | Changed option name _FS_SHARE to _FS_LOCK. 176 | Fixed assertion failure due to OS/2 EA on FAT12/16 volume. 177 | 178 | 179 | 180 | R0.09b (January 24, 2013) 181 | 182 | Added f_setlabel() and f_getlabel(). 183 | 184 | 185 | 186 | R0.10 (October 02, 2013) 187 | 188 | Added selection of character encoding on the file. (_STRF_ENCODE) 189 | Added f_closedir(). 190 | Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO) 191 | Added forced mount feature with changes of f_mount(). 192 | Improved behavior of volume auto detection. 193 | Improved write throughput of f_puts() and f_printf(). 194 | Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write(). 195 | Fixed f_write() can be truncated when the file size is close to 4GB. 196 | Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error. 197 | 198 | 199 | 200 | R0.10a (January 15, 2014) 201 | 202 | Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID) 203 | Added a configuration option of minimum sector size. (_MIN_SS) 204 | 2nd argument of f_rename() can have a drive number and it will be ignored. 205 | Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10) 206 | Fixed f_close() invalidates the file object without volume lock. 207 | Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10) 208 | Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07) 209 | 210 | 211 | 212 | R0.10b (May 19, 2014) 213 | 214 | Fixed a hard error in the disk I/O layer can collapse the directory entry. 215 | Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07) 216 | 217 | 218 | 219 | R0.10c (November 09, 2014) 220 | 221 | Added a configuration option for the platforms without RTC. (_FS_NORTC) 222 | Changed option name _USE_ERASE to _USE_TRIM. 223 | Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b) 224 | Fixed a potential problem of FAT access that can appear on disk error. 225 | Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08) 226 | 227 | 228 | 229 | R0.11 (February 09, 2015) 230 | 231 | Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND) 232 | Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c) 233 | Fixed _FS_NORTC option does not work properly. (appeared at R0.10c) 234 | 235 | 236 | 237 | R0.11a (September 05, 2015) 238 | 239 | Fixed wrong media change can lead a deadlock at thread-safe configuration. 240 | Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE) 241 | Removed some code pages actually not exist on the standard systems. (_CODE_PAGE) 242 | Fixed errors in the case conversion teble of code page 437 and 850 (ff.c). 243 | Fixed errors in the case conversion teble of Unicode (cc*.c). 244 | 245 | 246 | 247 | R0.12 (April 12, 2016) 248 | 249 | Added support for exFAT file system. (_FS_EXFAT) 250 | Added f_expand(). (_USE_EXPAND) 251 | Changed some members in FINFO structure and behavior of f_readdir(). 252 | Added an option _USE_CHMOD. 253 | Removed an option _WORD_ACCESS. 254 | Fixed errors in the case conversion table of Unicode (cc*.c). 255 | 256 | 257 | 258 | R0.12a (July 10, 2016) 259 | 260 | Added support for creating exFAT volume with some changes of f_mkfs(). 261 | Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed. 262 | f_forward() is available regardless of _FS_TINY. 263 | Fixed f_mkfs() creates wrong volume. (appeared at R0.12) 264 | Fixed wrong memory read in create_name(). (appeared at R0.12) 265 | Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD. 266 | 267 | 268 | 269 | R0.12b (September 04, 2016) 270 | 271 | Made f_rename() be able to rename objects with the same name but case. 272 | Fixed an error in the case conversion teble of code page 866. (ff.c) 273 | Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12) 274 | Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12) 275 | Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12) 276 | Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12) 277 | Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12) 278 | Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12) 279 | 280 | 281 | 282 | R0.12c (March 04, 2017) 283 | 284 | Improved write throughput at the fragmented file on the exFAT volume. 285 | Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN. 286 | Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12) 287 | Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c) 288 | 289 | 290 | 291 | R0.13 (May 21, 2017) 292 | 293 | Changed heading character of configuration keywords "_" to "FF_". 294 | Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead. 295 | Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0) 296 | Improved cluster allocation time on stretch a deep buried cluster chain. 297 | Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3. 298 | Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous. 299 | Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12) 300 | Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c) 301 | Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c) 302 | 303 | 304 | 305 | R0.13a (October 14, 2017) 306 | 307 | Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2) 308 | Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF). 309 | Added dynamic memory allocation option for working buffer of f_mkfs() and f_fdisk(). 310 | Fixed f_fdisk() and f_mkfs() create the partition table with wrong CHS parameters. (appeared at R0.09) 311 | Fixed f_unlink() can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c) 312 | Fixed f_setlabel() rejects some valid characters for exFAT volume. (appeared at R0.12) 313 | 314 | 315 | 316 | R0.13b (April 07, 2018) 317 | 318 | Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3) 319 | Added support for Unix style volume ID. (FF_STR_VOLUME_ID = 2) 320 | Fixed accesing any object on the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c) 321 | Fixed f_setlabel() does not reject some invalid characters. (appeared at R0.09b) 322 | 323 | 324 | 325 | R0.13c (October 14, 2018) 326 | Supported stdint.h for C99 and later. (integer.h was included in ff.h) 327 | Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12) 328 | Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12) 329 | Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b) 330 | 331 | 332 | 333 | R0.14 (October 14, 2019) 334 | Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1) 335 | Changed some API functions, f_mkfs() and f_fdisk(). 336 | Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters. 337 | Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters. 338 | Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12) 339 | Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12) 340 | 341 | 342 | R0.14a (December 5, 2020) 343 | Limited number of recursive calls in f_findnext(). 344 | Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted. 345 | Fixed some compiler warnings. 346 | 347 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.14a 2 | 3 | 4 | FILES 5 | 6 | 00readme.txt This file. 7 | 00history.txt Revision history. 8 | ff.c FatFs module. 9 | ffconf.h Configuration file of FatFs module. 10 | ff.h Common include file for FatFs and application module. 11 | diskio.h Common include file for FatFs and disk I/O module. 12 | diskio.c An example of glue function to attach existing disk I/O module to FatFs. 13 | ffunicode.c Optional Unicode utility functions. 14 | ffsystem.c An example of optional O/S related functions. 15 | 16 | 17 | Low level disk I/O module is not included in this archive because the FatFs 18 | module is only a generic file system layer and it does not depend on any specific 19 | storage device. You need to provide a low level disk I/O module written to 20 | control the storage device that attached to the target system. 21 | 22 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ 3 | /*-----------------------------------------------------------------------*/ 4 | /* If a working storage control module is available, it should be */ 5 | /* attached to the FatFs via a glue function rather than modifying it. */ 6 | /* This is an example of glue functions to attach various exsisting */ 7 | /* storage control modules to the FatFs module with a defined API. */ 8 | /*-----------------------------------------------------------------------*/ 9 | 10 | #include "ff.h" /* Obtains integer types */ 11 | #include "diskio.h" /* Declarations of disk functions */ 12 | 13 | /* Definitions of physical drive number for each drive */ 14 | #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */ 15 | #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */ 16 | #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */ 17 | 18 | 19 | /*-----------------------------------------------------------------------*/ 20 | /* Get Drive Status */ 21 | /*-----------------------------------------------------------------------*/ 22 | 23 | DSTATUS disk_status ( 24 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 25 | ) 26 | { 27 | DSTATUS stat; 28 | int result; 29 | 30 | switch (pdrv) { 31 | case DEV_RAM : 32 | result = RAM_disk_status(); 33 | 34 | // translate the reslut code here 35 | 36 | return stat; 37 | 38 | case DEV_MMC : 39 | result = MMC_disk_status(); 40 | 41 | // translate the reslut code here 42 | 43 | return stat; 44 | 45 | case DEV_USB : 46 | result = USB_disk_status(); 47 | 48 | // translate the reslut code here 49 | 50 | return stat; 51 | } 52 | return STA_NOINIT; 53 | } 54 | 55 | 56 | 57 | /*-----------------------------------------------------------------------*/ 58 | /* Inidialize a Drive */ 59 | /*-----------------------------------------------------------------------*/ 60 | 61 | DSTATUS disk_initialize ( 62 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 63 | ) 64 | { 65 | DSTATUS stat; 66 | int result; 67 | 68 | switch (pdrv) { 69 | case DEV_RAM : 70 | result = RAM_disk_initialize(); 71 | 72 | // translate the reslut code here 73 | 74 | return stat; 75 | 76 | case DEV_MMC : 77 | result = MMC_disk_initialize(); 78 | 79 | // translate the reslut code here 80 | 81 | return stat; 82 | 83 | case DEV_USB : 84 | result = USB_disk_initialize(); 85 | 86 | // translate the reslut code here 87 | 88 | return stat; 89 | } 90 | return STA_NOINIT; 91 | } 92 | 93 | 94 | 95 | /*-----------------------------------------------------------------------*/ 96 | /* Read Sector(s) */ 97 | /*-----------------------------------------------------------------------*/ 98 | 99 | DRESULT disk_read ( 100 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 101 | BYTE *buff, /* Data buffer to store read data */ 102 | LBA_t sector, /* Start sector in LBA */ 103 | UINT count /* Number of sectors to read */ 104 | ) 105 | { 106 | DRESULT res; 107 | int result; 108 | 109 | switch (pdrv) { 110 | case DEV_RAM : 111 | // translate the arguments here 112 | 113 | result = RAM_disk_read(buff, sector, count); 114 | 115 | // translate the reslut code here 116 | 117 | return res; 118 | 119 | case DEV_MMC : 120 | // translate the arguments here 121 | 122 | result = MMC_disk_read(buff, sector, count); 123 | 124 | // translate the reslut code here 125 | 126 | return res; 127 | 128 | case DEV_USB : 129 | // translate the arguments here 130 | 131 | result = USB_disk_read(buff, sector, count); 132 | 133 | // translate the reslut code here 134 | 135 | return res; 136 | } 137 | 138 | return RES_PARERR; 139 | } 140 | 141 | 142 | 143 | /*-----------------------------------------------------------------------*/ 144 | /* Write Sector(s) */ 145 | /*-----------------------------------------------------------------------*/ 146 | 147 | #if FF_FS_READONLY == 0 148 | 149 | DRESULT disk_write ( 150 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 151 | const BYTE *buff, /* Data to be written */ 152 | LBA_t sector, /* Start sector in LBA */ 153 | UINT count /* Number of sectors to write */ 154 | ) 155 | { 156 | DRESULT res; 157 | int result; 158 | 159 | switch (pdrv) { 160 | case DEV_RAM : 161 | // translate the arguments here 162 | 163 | result = RAM_disk_write(buff, sector, count); 164 | 165 | // translate the reslut code here 166 | 167 | return res; 168 | 169 | case DEV_MMC : 170 | // translate the arguments here 171 | 172 | result = MMC_disk_write(buff, sector, count); 173 | 174 | // translate the reslut code here 175 | 176 | return res; 177 | 178 | case DEV_USB : 179 | // translate the arguments here 180 | 181 | result = USB_disk_write(buff, sector, count); 182 | 183 | // translate the reslut code here 184 | 185 | return res; 186 | } 187 | 188 | return RES_PARERR; 189 | } 190 | 191 | #endif 192 | 193 | 194 | /*-----------------------------------------------------------------------*/ 195 | /* Miscellaneous Functions */ 196 | /*-----------------------------------------------------------------------*/ 197 | 198 | DRESULT disk_ioctl ( 199 | BYTE pdrv, /* Physical drive nmuber (0..) */ 200 | BYTE cmd, /* Control code */ 201 | void *buff /* Buffer to send/receive control data */ 202 | ) 203 | { 204 | DRESULT res; 205 | int result; 206 | 207 | switch (pdrv) { 208 | case DEV_RAM : 209 | 210 | // Process of the command for the RAM drive 211 | 212 | return res; 213 | 214 | case DEV_MMC : 215 | 216 | // Process of the command for the MMC/SD card 217 | 218 | return res; 219 | 220 | case DEV_USB : 221 | 222 | // Process of the command the USB drive 223 | 224 | return res; 225 | } 226 | 227 | return RES_PARERR; 228 | } 229 | 230 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2019 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Status of Disk Functions */ 13 | typedef BYTE DSTATUS; 14 | 15 | /* Results of Disk Functions */ 16 | typedef enum { 17 | RES_OK = 0, /* 0: Successful */ 18 | RES_ERROR, /* 1: R/W Error */ 19 | RES_WRPRT, /* 2: Write Protected */ 20 | RES_NOTRDY, /* 3: Not Ready */ 21 | RES_PARERR /* 4: Invalid Parameter */ 22 | } DRESULT; 23 | 24 | 25 | /*---------------------------------------*/ 26 | /* Prototypes for disk control functions */ 27 | 28 | 29 | DSTATUS disk_initialize (BYTE pdrv); 30 | DSTATUS disk_status (BYTE pdrv); 31 | DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); 32 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); 33 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 34 | 35 | 36 | /* Disk Status Bits (DSTATUS) */ 37 | 38 | #define STA_NOINIT 0x01 /* Drive not initialized */ 39 | #define STA_NODISK 0x02 /* No medium in the drive */ 40 | #define STA_PROTECT 0x04 /* Write protected */ 41 | 42 | 43 | /* Command code for disk_ioctrl fucntion */ 44 | 45 | /* Generic command (Used by FatFs) */ 46 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 47 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 48 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 49 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 50 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 51 | 52 | /* Generic command (Not used by FatFs) */ 53 | #define CTRL_POWER 5 /* Get/Set power status */ 54 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 55 | #define CTRL_EJECT 7 /* Eject media */ 56 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 57 | 58 | /* MMC/SDC specific ioctl command */ 59 | #define MMC_GET_TYPE 10 /* Get card type */ 60 | #define MMC_GET_CSD 11 /* Get CSD */ 61 | #define MMC_GET_CID 12 /* Get CID */ 62 | #define MMC_GET_OCR 13 /* Get OCR */ 63 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 64 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 65 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 66 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 67 | 68 | /* ATA/CF specific ioctl command */ 69 | #define ATA_GET_REV 20 /* Get F/W revision */ 70 | #define ATA_GET_MODEL 21 /* Get model name */ 71 | #define ATA_GET_SN 22 /* Get serial number */ 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/ff.h: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------------/ 2 | / FatFs - Generic FAT Filesystem module R0.14a / 3 | /-----------------------------------------------------------------------------/ 4 | / 5 | / Copyright (C) 2020, ChaN, all right reserved. 6 | / 7 | / FatFs module is an open source software. Redistribution and use of FatFs in 8 | / source and binary forms, with or without modification, are permitted provided 9 | / that the following condition is met: 10 | 11 | / 1. Redistributions of source code must retain the above copyright notice, 12 | / this condition and the following disclaimer. 13 | / 14 | / This software is provided by the copyright holder and contributors "AS IS" 15 | / and any warranties related to this software are DISCLAIMED. 16 | / The copyright owner or contributors be NOT LIABLE for any damages caused 17 | / by use of this software. 18 | / 19 | /----------------------------------------------------------------------------*/ 20 | 21 | 22 | #ifndef FF_DEFINED 23 | #define FF_DEFINED 80196 /* Revision ID */ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #include "ffconf.h" /* FatFs configuration options */ 30 | 31 | #if FF_DEFINED != FFCONF_DEF 32 | #error Wrong configuration file (ffconf.h). 33 | #endif 34 | 35 | 36 | /* Integer types used for FatFs API */ 37 | 38 | #if defined(_WIN32) /* Main development platform */ 39 | #define FF_INTDEF 2 40 | #include 41 | typedef unsigned __int64 QWORD; 42 | #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */ 43 | #define FF_INTDEF 2 44 | #include 45 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 46 | typedef unsigned char BYTE; /* char must be 8-bit */ 47 | typedef uint16_t WORD; /* 16-bit unsigned integer */ 48 | typedef uint32_t DWORD; /* 32-bit unsigned integer */ 49 | typedef uint64_t QWORD; /* 64-bit unsigned integer */ 50 | typedef WORD WCHAR; /* UTF-16 character type */ 51 | #else /* Earlier than C99 */ 52 | #define FF_INTDEF 1 53 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 54 | typedef unsigned char BYTE; /* char must be 8-bit */ 55 | typedef unsigned short WORD; /* 16-bit unsigned integer */ 56 | typedef unsigned long DWORD; /* 32-bit unsigned integer */ 57 | typedef WORD WCHAR; /* UTF-16 character type */ 58 | #endif 59 | 60 | 61 | /* Definitions of volume management */ 62 | 63 | #if FF_MULTI_PARTITION /* Multiple partition configuration */ 64 | typedef struct { 65 | BYTE pd; /* Physical drive number */ 66 | BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ 67 | } PARTITION; 68 | extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ 69 | #endif 70 | 71 | #if FF_STR_VOLUME_ID 72 | #ifndef FF_VOLUME_STRS 73 | extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ 74 | #endif 75 | #endif 76 | 77 | 78 | 79 | /* Type of path name strings on FatFs API */ 80 | 81 | #ifndef _INC_TCHAR 82 | #define _INC_TCHAR 83 | 84 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */ 85 | typedef WCHAR TCHAR; 86 | #define _T(x) L ## x 87 | #define _TEXT(x) L ## x 88 | #elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */ 89 | typedef char TCHAR; 90 | #define _T(x) u8 ## x 91 | #define _TEXT(x) u8 ## x 92 | #elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */ 93 | typedef DWORD TCHAR; 94 | #define _T(x) U ## x 95 | #define _TEXT(x) U ## x 96 | #elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3) 97 | #error Wrong FF_LFN_UNICODE setting 98 | #else /* ANSI/OEM code in SBCS/DBCS */ 99 | typedef char TCHAR; 100 | #define _T(x) x 101 | #define _TEXT(x) x 102 | #endif 103 | 104 | #endif 105 | 106 | 107 | 108 | /* Type of file size and LBA variables */ 109 | 110 | #if FF_FS_EXFAT 111 | #if FF_INTDEF != 2 112 | #error exFAT feature wants C99 or later 113 | #endif 114 | typedef QWORD FSIZE_t; 115 | #if FF_LBA64 116 | typedef QWORD LBA_t; 117 | #else 118 | typedef DWORD LBA_t; 119 | #endif 120 | #else 121 | #if FF_LBA64 122 | #error exFAT needs to be enabled when enable 64-bit LBA 123 | #endif 124 | typedef DWORD FSIZE_t; 125 | typedef DWORD LBA_t; 126 | #endif 127 | 128 | 129 | 130 | /* Filesystem object structure (FATFS) */ 131 | 132 | typedef struct { 133 | BYTE fs_type; /* Filesystem type (0:not mounted) */ 134 | BYTE pdrv; /* Associated physical drive */ 135 | BYTE n_fats; /* Number of FATs (1 or 2) */ 136 | BYTE wflag; /* win[] flag (b0:dirty) */ 137 | BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ 138 | WORD id; /* Volume mount ID */ 139 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 140 | WORD csize; /* Cluster size [sectors] */ 141 | #if FF_MAX_SS != FF_MIN_SS 142 | WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ 143 | #endif 144 | #if FF_USE_LFN 145 | WCHAR* lfnbuf; /* LFN working buffer */ 146 | #endif 147 | #if FF_FS_EXFAT 148 | BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ 149 | #endif 150 | #if FF_FS_REENTRANT 151 | FF_SYNC_t sobj; /* Identifier of sync object */ 152 | #endif 153 | #if !FF_FS_READONLY 154 | DWORD last_clst; /* Last allocated cluster */ 155 | DWORD free_clst; /* Number of free clusters */ 156 | #endif 157 | #if FF_FS_RPATH 158 | DWORD cdir; /* Current directory start cluster (0:root) */ 159 | #if FF_FS_EXFAT 160 | DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ 161 | DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ 162 | DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ 163 | #endif 164 | #endif 165 | DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ 166 | DWORD fsize; /* Size of an FAT [sectors] */ 167 | LBA_t volbase; /* Volume base sector */ 168 | LBA_t fatbase; /* FAT base sector */ 169 | LBA_t dirbase; /* Root directory base sector/cluster */ 170 | LBA_t database; /* Data base sector */ 171 | #if FF_FS_EXFAT 172 | LBA_t bitbase; /* Allocation bitmap base sector */ 173 | #endif 174 | LBA_t winsect; /* Current sector appearing in the win[] */ 175 | BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 176 | } FATFS; 177 | 178 | 179 | 180 | /* Object ID and allocation information (FFOBJID) */ 181 | 182 | typedef struct { 183 | FATFS* fs; /* Pointer to the hosting volume of this object */ 184 | WORD id; /* Hosting volume mount ID */ 185 | BYTE attr; /* Object attribute */ 186 | BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */ 187 | DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ 188 | FSIZE_t objsize; /* Object size (valid when sclust != 0) */ 189 | #if FF_FS_EXFAT 190 | DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */ 191 | DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ 192 | DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ 193 | DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ 194 | DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ 195 | #endif 196 | #if FF_FS_LOCK 197 | UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ 198 | #endif 199 | } FFOBJID; 200 | 201 | 202 | 203 | /* File object structure (FIL) */ 204 | 205 | typedef struct { 206 | FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ 207 | BYTE flag; /* File status flags */ 208 | BYTE err; /* Abort flag (error code) */ 209 | FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ 210 | DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ 211 | LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */ 212 | #if !FF_FS_READONLY 213 | LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ 214 | BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ 215 | #endif 216 | #if FF_USE_FASTSEEK 217 | DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */ 218 | #endif 219 | #if !FF_FS_TINY 220 | BYTE buf[FF_MAX_SS]; /* File private data read/write window */ 221 | #endif 222 | } FIL; 223 | 224 | 225 | 226 | /* Directory object structure (DIR) */ 227 | 228 | typedef struct { 229 | FFOBJID obj; /* Object identifier */ 230 | DWORD dptr; /* Current read/write offset */ 231 | DWORD clust; /* Current cluster */ 232 | LBA_t sect; /* Current sector (0:Read operation has terminated) */ 233 | BYTE* dir; /* Pointer to the directory item in the win[] */ 234 | BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ 235 | #if FF_USE_LFN 236 | DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ 237 | #endif 238 | #if FF_USE_FIND 239 | const TCHAR* pat; /* Pointer to the name matching pattern */ 240 | #endif 241 | } DIR; 242 | 243 | 244 | 245 | /* File information structure (FILINFO) */ 246 | 247 | typedef struct { 248 | FSIZE_t fsize; /* File size */ 249 | WORD fdate; /* Modified date */ 250 | WORD ftime; /* Modified time */ 251 | BYTE fattrib; /* File attribute */ 252 | #if FF_USE_LFN 253 | TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */ 254 | TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */ 255 | #else 256 | TCHAR fname[12 + 1]; /* File name */ 257 | #endif 258 | } FILINFO; 259 | 260 | 261 | 262 | /* Format parameter structure (MKFS_PARM) */ 263 | 264 | typedef struct { 265 | BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */ 266 | BYTE n_fat; /* Number of FATs */ 267 | UINT align; /* Data area alignment (sector) */ 268 | UINT n_root; /* Number of root directory entries */ 269 | DWORD au_size; /* Cluster size (byte) */ 270 | } MKFS_PARM; 271 | 272 | 273 | 274 | /* File function return code (FRESULT) */ 275 | 276 | typedef enum { 277 | FR_OK = 0, /* (0) Succeeded */ 278 | FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ 279 | FR_INT_ERR, /* (2) Assertion failed */ 280 | FR_NOT_READY, /* (3) The physical drive cannot work */ 281 | FR_NO_FILE, /* (4) Could not find the file */ 282 | FR_NO_PATH, /* (5) Could not find the path */ 283 | FR_INVALID_NAME, /* (6) The path name format is invalid */ 284 | FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ 285 | FR_EXIST, /* (8) Access denied due to prohibited access */ 286 | FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ 287 | FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ 288 | FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 289 | FR_NOT_ENABLED, /* (12) The volume has no work area */ 290 | FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ 291 | FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ 292 | FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ 293 | FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ 294 | FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ 295 | FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ 296 | FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ 297 | } FRESULT; 298 | 299 | 300 | 301 | /*--------------------------------------------------------------*/ 302 | /* FatFs module application interface */ 303 | 304 | FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ 305 | FRESULT f_close (FIL* fp); /* Close an open file object */ 306 | FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ 307 | FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ 308 | FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ 309 | FRESULT f_truncate (FIL* fp); /* Truncate the file */ 310 | FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ 311 | FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ 312 | FRESULT f_closedir (DIR* dp); /* Close an open directory */ 313 | FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */ 314 | FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ 315 | FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ 316 | FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ 317 | FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ 318 | FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ 319 | FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ 320 | FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */ 321 | FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ 322 | FRESULT f_chdir (const TCHAR* path); /* Change current directory */ 323 | FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ 324 | FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ 325 | FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ 326 | FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ 327 | FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ 328 | FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ 329 | FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */ 330 | FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ 331 | FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */ 332 | FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */ 333 | FRESULT f_setcp (WORD cp); /* Set current code page */ 334 | int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ 335 | int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ 336 | int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ 337 | TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ 338 | 339 | #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) 340 | #define f_error(fp) ((fp)->err) 341 | #define f_tell(fp) ((fp)->fptr) 342 | #define f_size(fp) ((fp)->obj.objsize) 343 | #define f_rewind(fp) f_lseek((fp), 0) 344 | #define f_rewinddir(dp) f_readdir((dp), 0) 345 | #define f_rmdir(path) f_unlink(path) 346 | #define f_unmount(path) f_mount(0, path, 0) 347 | 348 | 349 | 350 | 351 | /*--------------------------------------------------------------*/ 352 | /* Additional user defined functions */ 353 | 354 | /* RTC function */ 355 | #if !FF_FS_READONLY && !FF_FS_NORTC 356 | DWORD get_fattime (void); 357 | #endif 358 | 359 | /* LFN support functions */ 360 | #if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */ 361 | WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ 362 | WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */ 363 | DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ 364 | #endif 365 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 366 | void* ff_memalloc (UINT msize); /* Allocate memory block */ 367 | void ff_memfree (void* mblock); /* Free memory block */ 368 | #endif 369 | 370 | /* Sync functions */ 371 | #if FF_FS_REENTRANT 372 | int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ 373 | int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ 374 | void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ 375 | int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ 376 | #endif 377 | 378 | 379 | 380 | 381 | /*--------------------------------------------------------------*/ 382 | /* Flags and offset address */ 383 | 384 | 385 | /* File access mode and open method flags (3rd argument of f_open) */ 386 | #define FA_READ 0x01 387 | #define FA_WRITE 0x02 388 | #define FA_OPEN_EXISTING 0x00 389 | #define FA_CREATE_NEW 0x04 390 | #define FA_CREATE_ALWAYS 0x08 391 | #define FA_OPEN_ALWAYS 0x10 392 | #define FA_OPEN_APPEND 0x30 393 | 394 | /* Fast seek controls (2nd argument of f_lseek) */ 395 | #define CREATE_LINKMAP ((FSIZE_t)0 - 1) 396 | 397 | /* Format options (2nd argument of f_mkfs) */ 398 | #define FM_FAT 0x01 399 | #define FM_FAT32 0x02 400 | #define FM_EXFAT 0x04 401 | #define FM_ANY 0x07 402 | #define FM_SFD 0x08 403 | 404 | /* Filesystem type (FATFS.fs_type) */ 405 | #define FS_FAT12 1 406 | #define FS_FAT16 2 407 | #define FS_FAT32 3 408 | #define FS_EXFAT 4 409 | 410 | /* File attribute bits for directory entry (FILINFO.fattrib) */ 411 | #define AM_RDO 0x01 /* Read only */ 412 | #define AM_HID 0x02 /* Hidden */ 413 | #define AM_SYS 0x04 /* System */ 414 | #define AM_DIR 0x10 /* Directory */ 415 | #define AM_ARC 0x20 /* Archive */ 416 | 417 | 418 | #ifdef __cplusplus 419 | } 420 | #endif 421 | 422 | #endif /* FF_DEFINED */ 423 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/ffconf.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------/ 2 | / FatFs Functional Configurations 3 | /---------------------------------------------------------------------------*/ 4 | 5 | #define FFCONF_DEF 80196 /* Revision ID */ 6 | 7 | /*---------------------------------------------------------------------------/ 8 | / Function Configurations 9 | /---------------------------------------------------------------------------*/ 10 | 11 | #define FF_FS_READONLY 0 12 | /* This option switches read-only configuration. (0:Read/Write or 1:Read-only) 13 | / Read-only configuration removes writing API functions, f_write(), f_sync(), 14 | / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() 15 | / and optional writing functions as well. */ 16 | 17 | 18 | #define FF_FS_MINIMIZE 0 19 | /* This option defines minimization level to remove some basic API functions. 20 | / 21 | / 0: Basic functions are fully enabled. 22 | / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() 23 | / are removed. 24 | / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1. 25 | / 3: f_lseek() function is removed in addition to 2. */ 26 | 27 | 28 | #define FF_USE_STRFUNC 1 29 | /* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf(). 30 | / 31 | / 0: Disable string functions. 32 | / 1: Enable without LF-CRLF conversion. 33 | / 2: Enable with LF-CRLF conversion. */ 34 | 35 | 36 | #define FF_USE_FIND 1 37 | /* This option switches filtered directory read functions, f_findfirst() and 38 | / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ 39 | 40 | 41 | #define FF_USE_MKFS 1 42 | /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ 43 | 44 | 45 | #define FF_USE_FASTSEEK 1 46 | /* This option switches fast seek function. (0:Disable or 1:Enable) */ 47 | 48 | 49 | #define FF_USE_EXPAND 0 50 | /* This option switches f_expand function. (0:Disable or 1:Enable) */ 51 | 52 | 53 | #define FF_USE_CHMOD 0 54 | /* This option switches attribute manipulation functions, f_chmod() and f_utime(). 55 | / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ 56 | 57 | 58 | #define FF_USE_LABEL 0 59 | /* This option switches volume label functions, f_getlabel() and f_setlabel(). 60 | / (0:Disable or 1:Enable) */ 61 | 62 | 63 | #define FF_USE_FORWARD 0 64 | /* This option switches f_forward() function. (0:Disable or 1:Enable) */ 65 | 66 | 67 | /*---------------------------------------------------------------------------/ 68 | / Locale and Namespace Configurations 69 | /---------------------------------------------------------------------------*/ 70 | 71 | #define FF_CODE_PAGE 437 72 | /* This option specifies the OEM code page to be used on the target system. 73 | / Incorrect code page setting can cause a file open failure. 74 | / 75 | / 437 - U.S. 76 | / 720 - Arabic 77 | / 737 - Greek 78 | / 771 - KBL 79 | / 775 - Baltic 80 | / 850 - Latin 1 81 | / 852 - Latin 2 82 | / 855 - Cyrillic 83 | / 857 - Turkish 84 | / 860 - Portuguese 85 | / 861 - Icelandic 86 | / 862 - Hebrew 87 | / 863 - Canadian French 88 | / 864 - Arabic 89 | / 865 - Nordic 90 | / 866 - Russian 91 | / 869 - Greek 2 92 | / 932 - Japanese (DBCS) 93 | / 936 - Simplified Chinese (DBCS) 94 | / 949 - Korean (DBCS) 95 | / 950 - Traditional Chinese (DBCS) 96 | / 0 - Include all code pages above and configured by f_setcp() 97 | */ 98 | 99 | 100 | #define FF_USE_LFN 3 101 | #define FF_MAX_LFN 255 102 | /* The FF_USE_LFN switches the support for LFN (long file name). 103 | / 104 | / 0: Disable LFN. FF_MAX_LFN has no effect. 105 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 106 | / 2: Enable LFN with dynamic working buffer on the STACK. 107 | / 3: Enable LFN with dynamic working buffer on the HEAP. 108 | / 109 | / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function 110 | / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 111 | / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. 112 | / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can 113 | / be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN 114 | / specification. 115 | / When use stack for the working buffer, take care on stack overflow. When use heap 116 | / memory for the working buffer, memory management functions, ff_memalloc() and 117 | / ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ 118 | 119 | 120 | #define FF_LFN_UNICODE 2 121 | /* This option switches the character encoding on the API when LFN is enabled. 122 | / 123 | / 0: ANSI/OEM in current CP (TCHAR = char) 124 | / 1: Unicode in UTF-16 (TCHAR = WCHAR) 125 | / 2: Unicode in UTF-8 (TCHAR = char) 126 | / 3: Unicode in UTF-32 (TCHAR = DWORD) 127 | / 128 | / Also behavior of string I/O functions will be affected by this option. 129 | / When LFN is not enabled, this option has no effect. */ 130 | 131 | 132 | #define FF_LFN_BUF 255 133 | #define FF_SFN_BUF 12 134 | /* This set of options defines size of file name members in the FILINFO structure 135 | / which is used to read out directory items. These values should be suffcient for 136 | / the file names to read. The maximum possible length of the read file name depends 137 | / on character encoding. When LFN is not enabled, these options have no effect. */ 138 | 139 | 140 | #define FF_STRF_ENCODE 3 141 | /* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(), 142 | / f_putc(), f_puts and f_printf() convert the character encoding in it. 143 | / This option selects assumption of character encoding ON THE FILE to be 144 | / read/written via those functions. 145 | / 146 | / 0: ANSI/OEM in current CP 147 | / 1: Unicode in UTF-16LE 148 | / 2: Unicode in UTF-16BE 149 | / 3: Unicode in UTF-8 150 | */ 151 | 152 | 153 | #define FF_FS_RPATH 2 154 | /* This option configures support for relative path. 155 | / 156 | / 0: Disable relative path and remove related functions. 157 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 158 | / 2: f_getcwd() function is available in addition to 1. 159 | */ 160 | 161 | 162 | /*---------------------------------------------------------------------------/ 163 | / Drive/Volume Configurations 164 | /---------------------------------------------------------------------------*/ 165 | 166 | # define FF_VOLUMES 2 167 | /* Number of volumes (logical drives) to be used. (1-10) */ 168 | 169 | 170 | #define FF_STR_VOLUME_ID 0 171 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 172 | /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. 173 | / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive 174 | / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each 175 | / logical drives. Number of items must not be less than FF_VOLUMES. Valid 176 | / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are 177 | / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is 178 | / not defined, a user defined volume string table needs to be defined as: 179 | / 180 | / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... 181 | */ 182 | 183 | 184 | #define FF_MULTI_PARTITION 0 185 | /* This option switches support for multiple volumes on the physical drive. 186 | / By default (0), each logical drive number is bound to the same physical drive 187 | / number and only an FAT volume found on the physical drive will be mounted. 188 | / When this function is enabled (1), each logical drive number can be bound to 189 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 190 | / funciton will be available. */ 191 | 192 | 193 | #define FF_MIN_SS 512 194 | #define FF_MAX_SS 512 195 | /* This set of options configures the range of sector size to be supported. (512, 196 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 197 | / harddisk. But a larger value may be required for on-board flash memory and some 198 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 199 | / for variable sector size mode and disk_ioctl() function needs to implement 200 | / GET_SECTOR_SIZE command. */ 201 | 202 | 203 | #define FF_LBA64 1 204 | /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) 205 | / To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ 206 | 207 | 208 | #define FF_MIN_GPT 0x10000000 209 | /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and 210 | / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ 211 | 212 | 213 | #define FF_USE_TRIM 0 214 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 215 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 216 | / disk_ioctl() function. */ 217 | 218 | 219 | 220 | /*---------------------------------------------------------------------------/ 221 | / System Configurations 222 | /---------------------------------------------------------------------------*/ 223 | 224 | #define FF_FS_TINY 0 225 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 226 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 227 | / Instead of private sector buffer eliminated from the file object, common sector 228 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 229 | 230 | 231 | #define FF_FS_EXFAT 1 232 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 233 | / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) 234 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 235 | 236 | 237 | #define FF_FS_NORTC 0 238 | #define FF_NORTC_MON 1 239 | #define FF_NORTC_MDAY 1 240 | #define FF_NORTC_YEAR 2020 241 | /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have 242 | / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable 243 | / the timestamp function. Every object modified by FatFs will have a fixed timestamp 244 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 245 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 246 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 247 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 248 | / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ 249 | 250 | 251 | #define FF_FS_NOFSINFO 0 252 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 253 | / option, and f_getfree() function at first time after volume mount will force 254 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 255 | / 256 | / bit0=0: Use free cluster count in the FSINFO if available. 257 | / bit0=1: Do not trust free cluster count in the FSINFO. 258 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 259 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 260 | */ 261 | 262 | 263 | #define FF_FS_LOCK 16 264 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 265 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 266 | / is 1. 267 | / 268 | / 0: Disable file lock function. To avoid volume corruption, application program 269 | / should avoid illegal open, remove and rename to the open objects. 270 | / >0: Enable file lock function. The value defines how many files/sub-directories 271 | / can be opened simultaneously under file lock control. Note that the file 272 | / lock control is independent of re-entrancy. */ 273 | 274 | 275 | /* #include // O/S definitions */ 276 | #define FF_FS_REENTRANT 0 277 | #define FF_FS_TIMEOUT 1000 278 | #define FF_SYNC_t HANDLE 279 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 280 | / module itself. Note that regardless of this option, file access to different 281 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 282 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 283 | / to the same volume is under control of this function. 284 | / 285 | / 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect. 286 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 287 | / ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() 288 | / function, must be added to the project. Samples are available in 289 | / option/syscall.c. 290 | / 291 | / The FF_FS_TIMEOUT defines timeout period in unit of time tick. 292 | / The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, 293 | / SemaphoreHandle_t and etc. A header file for O/S definitions needs to be 294 | / included somewhere in the scope of ff.h. */ 295 | 296 | 297 | 298 | /*--- End of configuration options ---*/ 299 | -------------------------------------------------------------------------------- /FatFs_SPI/ff14a/source/ffsystem.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* Sample Code of OS Dependent Functions for FatFs */ 3 | /* (C)ChaN, 2018 */ 4 | /*------------------------------------------------------------------------*/ 5 | 6 | #include 7 | // 8 | #include "ff.h" 9 | 10 | 11 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 12 | 13 | /*------------------------------------------------------------------------*/ 14 | /* Allocate a memory block */ 15 | /*------------------------------------------------------------------------*/ 16 | 17 | void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ 18 | UINT msize /* Number of bytes to allocate */ 19 | ) 20 | { 21 | return malloc(msize); /* Allocate a new memory block with POSIX API */ 22 | } 23 | 24 | 25 | /*------------------------------------------------------------------------*/ 26 | /* Free a memory block */ 27 | /*------------------------------------------------------------------------*/ 28 | 29 | void ff_memfree ( 30 | void* mblock /* Pointer to the memory block to free (nothing to do if null) */ 31 | ) 32 | { 33 | free(mblock); /* Free the memory block with POSIX API */ 34 | } 35 | 36 | #endif 37 | 38 | 39 | 40 | #if FF_FS_REENTRANT /* Mutal exclusion */ 41 | 42 | /*------------------------------------------------------------------------*/ 43 | /* Create a Synchronization Object */ 44 | /*------------------------------------------------------------------------*/ 45 | /* This function is called in f_mount() function to create a new 46 | / synchronization object for the volume, such as semaphore and mutex. 47 | / When a 0 is returned, the f_mount() function fails with FR_INT_ERR. 48 | */ 49 | 50 | //const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */ 51 | 52 | 53 | int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ 54 | BYTE vol, /* Corresponding volume (logical drive number) */ 55 | FF_SYNC_t* sobj /* Pointer to return the created sync object */ 56 | ) 57 | { 58 | /* Win32 */ 59 | *sobj = CreateMutex(NULL, FALSE, NULL); 60 | return (int)(*sobj != INVALID_HANDLE_VALUE); 61 | 62 | /* uITRON */ 63 | // T_CSEM csem = {TA_TPRI,1,1}; 64 | // *sobj = acre_sem(&csem); 65 | // return (int)(*sobj > 0); 66 | 67 | /* uC/OS-II */ 68 | // OS_ERR err; 69 | // *sobj = OSMutexCreate(0, &err); 70 | // return (int)(err == OS_NO_ERR); 71 | 72 | /* FreeRTOS */ 73 | // *sobj = xSemaphoreCreateMutex(); 74 | // return (int)(*sobj != NULL); 75 | 76 | /* CMSIS-RTOS */ 77 | // *sobj = osMutexCreate(&Mutex[vol]); 78 | // return (int)(*sobj != NULL); 79 | } 80 | 81 | 82 | /*------------------------------------------------------------------------*/ 83 | /* Delete a Synchronization Object */ 84 | /*------------------------------------------------------------------------*/ 85 | /* This function is called in f_mount() function to delete a synchronization 86 | / object that created with ff_cre_syncobj() function. When a 0 is returned, 87 | / the f_mount() function fails with FR_INT_ERR. 88 | */ 89 | 90 | int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */ 91 | FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ 92 | ) 93 | { 94 | /* Win32 */ 95 | return (int)CloseHandle(sobj); 96 | 97 | /* uITRON */ 98 | // return (int)(del_sem(sobj) == E_OK); 99 | 100 | /* uC/OS-II */ 101 | // OS_ERR err; 102 | // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); 103 | // return (int)(err == OS_NO_ERR); 104 | 105 | /* FreeRTOS */ 106 | // vSemaphoreDelete(sobj); 107 | // return 1; 108 | 109 | /* CMSIS-RTOS */ 110 | // return (int)(osMutexDelete(sobj) == osOK); 111 | } 112 | 113 | 114 | /*------------------------------------------------------------------------*/ 115 | /* Request Grant to Access the Volume */ 116 | /*------------------------------------------------------------------------*/ 117 | /* This function is called on entering file functions to lock the volume. 118 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 119 | */ 120 | 121 | int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ 122 | FF_SYNC_t sobj /* Sync object to wait */ 123 | ) 124 | { 125 | /* Win32 */ 126 | return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0); 127 | 128 | /* uITRON */ 129 | // return (int)(wai_sem(sobj) == E_OK); 130 | 131 | /* uC/OS-II */ 132 | // OS_ERR err; 133 | // OSMutexPend(sobj, FF_FS_TIMEOUT, &err)); 134 | // return (int)(err == OS_NO_ERR); 135 | 136 | /* FreeRTOS */ 137 | // return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); 138 | 139 | /* CMSIS-RTOS */ 140 | // return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK); 141 | } 142 | 143 | 144 | /*------------------------------------------------------------------------*/ 145 | /* Release Grant to Access the Volume */ 146 | /*------------------------------------------------------------------------*/ 147 | /* This function is called on leaving file functions to unlock the volume. 148 | */ 149 | 150 | void ff_rel_grant ( 151 | FF_SYNC_t sobj /* Sync object to be signaled */ 152 | ) 153 | { 154 | /* Win32 */ 155 | ReleaseMutex(sobj); 156 | 157 | /* uITRON */ 158 | // sig_sem(sobj); 159 | 160 | /* uC/OS-II */ 161 | // OSMutexPost(sobj); 162 | 163 | /* FreeRTOS */ 164 | // xSemaphoreGive(sobj); 165 | 166 | /* CMSIS-RTOS */ 167 | // osMutexRelease(sobj); 168 | } 169 | 170 | #endif 171 | 172 | -------------------------------------------------------------------------------- /FatFs_SPI/include/f_util.h: -------------------------------------------------------------------------------- 1 | /* f_util.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | #include "ff.h" 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | const char *FRESULT_str(FRESULT i); 22 | FRESULT delete_node ( 23 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 24 | UINT sz_buff, /* Size of path name buffer (items) */ 25 | FILINFO* fno /* Name read buffer */ 26 | ); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | -------------------------------------------------------------------------------- /FatFs_SPI/include/ff_stdio.h: -------------------------------------------------------------------------------- 1 | /* ff_stdio.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | // For compatibility with FreeRTOS+FAT API 15 | #include 16 | #include 17 | #include 18 | #include 19 | // 20 | #include "ff.h" 21 | // 22 | #include "my_debug.h" 23 | 24 | #define BaseType_t int 25 | #define FF_FILE FIL 26 | 27 | #define ff_rewind f_rewind 28 | #define pvPortMalloc malloc 29 | #define vPortFree free 30 | #define ffconfigMAX_FILENAME 250 31 | #define configASSERT myASSERT 32 | #define FF_PRINTF printf 33 | #define pdFREERTOS_ERRNO_NONE 0 34 | #define FF_EOF (-1) 35 | #define FF_SEEK_SET 0 36 | #define FF_SEEK_CUR 1 37 | #define FF_SEEK_END 2 38 | #define pdFALSE 0 39 | #define pdTRUE 1 40 | #define ff_filelength f_size 41 | #define ff_feof f_eof 42 | 43 | typedef struct FF_STAT { 44 | uint32_t st_size; /* Size of the object in number of bytes. */ 45 | // uint16_t st_mode; /* The mode (attribute bits) of this 46 | // file or directory. */ 47 | } FF_Stat_t; 48 | 49 | typedef struct { 50 | DIR dir; 51 | FILINFO fileinfo; 52 | const char *pcFileName; 53 | uint32_t ulFileSize; 54 | //uint8_t ucAttributes; 55 | } FF_FindData_t; 56 | 57 | FF_FILE *ff_fopen(const char *pcFile, const char *pcMode); 58 | int ff_fclose(FF_FILE *pxStream); 59 | int ff_stat(const char *pcFileName, FF_Stat_t *pxStatBuffer); 60 | size_t ff_fwrite(const void *pvBuffer, size_t xSize, size_t xItems, 61 | FF_FILE *pxStream); 62 | size_t ff_fread(void *pvBuffer, size_t xSize, size_t xItems, FF_FILE *pxStream); 63 | int ff_chdir(const char *pcDirectoryName); 64 | char *ff_getcwd(char *pcBuffer, size_t xBufferLength); 65 | int ff_mkdir(const char *pcPath); 66 | int ff_fputc(int iChar, FF_FILE *pxStream); 67 | int ff_fgetc(FF_FILE *pxStream); 68 | int ff_rmdir(const char *pcDirectory); 69 | int ff_remove(const char *pcPath); 70 | long ff_ftell(FF_FILE *pxStream); 71 | int ff_fseek(FF_FILE *pxStream, int iOffset, int iWhence); 72 | int ff_findfirst(const char *pcDirectory, FF_FindData_t *pxFindData); 73 | int ff_findnext( FF_FindData_t *pxFindData ); 74 | FF_FILE *ff_truncate( const char * pcFileName, long lTruncateSize ); 75 | int ff_seteof( FF_FILE *pxStream ); 76 | int ff_rename( const char *pcOldName, const char *pcNewName, int bDeleteIfExists ); 77 | char *ff_fgets(char *pcBuffer, size_t xCount, FF_FILE *pxStream); 78 | -------------------------------------------------------------------------------- /FatFs_SPI/include/my_debug.h: -------------------------------------------------------------------------------- 1 | /* my_debug.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #include 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | void my_printf(const char *pcFormat, ...) __attribute__((format(__printf__, 1, 2))); 23 | 24 | void my_assert_func(const char *file, int line, const char *func, 25 | const char *pred); 26 | 27 | #ifdef __cplusplus 28 | } 29 | #endif 30 | 31 | 32 | //#if defined(DEBUG) && !defined(NDEBUG) 33 | #define DBG_PRINTF my_printf 34 | //#else 35 | //#define DBG_PRINTF(fmt, args...) /* Don't do anything in release builds*/ 36 | //#endif 37 | 38 | #define myASSERT(__e) \ 39 | ((__e) ? (void)0 : my_assert_func(__FILE__, __LINE__, __func__, #__e)) 40 | -------------------------------------------------------------------------------- /FatFs_SPI/include/rtc.h: -------------------------------------------------------------------------------- 1 | /* rtc.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | void time_init(); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /FatFs_SPI/include/util.h: -------------------------------------------------------------------------------- 1 | /* util.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #ifndef _UTIL_H_ 15 | #define _UTIL_H_ 16 | 17 | #include 18 | #include 19 | 20 | #include "hardware/structs/scb.h" 21 | 22 | // works with negative index 23 | static inline int wrap_ix(int index, int n) 24 | { 25 | return ((index % n) + n) % n; 26 | } 27 | 28 | __attribute__((always_inline)) static inline uint32_t calculate_checksum(uint32_t const *p, size_t const size){ 29 | uint32_t checksum = 0; 30 | for (uint32_t i = 0; i < (size/sizeof(uint32_t))-1; i++){ 31 | checksum ^= *p; 32 | p++; 33 | } 34 | return checksum; 35 | } 36 | 37 | 38 | // from Google Chromium's codebase: 39 | #ifndef COUNT_OF 40 | #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) 41 | #endif 42 | 43 | __attribute__((always_inline)) static inline void __DSB(void) { 44 | __asm volatile("dsb 0xF" ::: "memory"); 45 | } 46 | 47 | // Patterned after CMSIS NVIC_SystemReset 48 | __attribute__((__noreturn__)) static inline void system_reset() { 49 | __DSB(); /* Ensure all outstanding memory accesses included 50 | buffered write are completed before reset */ 51 | scb_hw->aircr = ((0x5FAUL << 16U) | (1UL << 2U)); 52 | __DSB(); /* Ensure completion of memory access */ 53 | for (;;) { 54 | __asm volatile("nop"); 55 | } 56 | } 57 | 58 | /** 59 | \brief Disable IRQ Interrupts 60 | \details Disables IRQ interrupts by setting the I-bit in the CPSR. 61 | Can only be executed in Privileged modes. 62 | */ 63 | __attribute__((always_inline)) static inline void __disable_irq(void) { 64 | __asm volatile("cpsid i" : : : "memory"); 65 | } 66 | 67 | #endif 68 | /* [] END OF FILE */ 69 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/crc.c: -------------------------------------------------------------------------------- 1 | /* crc.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* Derived from: 15 | * SD/MMC File System Library 16 | * Copyright (c) 2016 Neil Thiessen 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | */ 30 | 31 | #include "crc.h" 32 | 33 | static const char m_Crc7Table[] = {0x00, 0x09, 0x12, 0x1B, 0x24, 0x2D, 0x36, 34 | 0x3F, 0x48, 0x41, 0x5A, 0x53, 0x6C, 0x65, 0x7E, 0x77, 0x19, 0x10, 0x0B, 35 | 0x02, 0x3D, 0x34, 0x2F, 0x26, 0x51, 0x58, 0x43, 0x4A, 0x75, 0x7C, 0x67, 36 | 0x6E, 0x32, 0x3B, 0x20, 0x29, 0x16, 0x1F, 0x04, 0x0D, 0x7A, 0x73, 0x68, 37 | 0x61, 0x5E, 0x57, 0x4C, 0x45, 0x2B, 0x22, 0x39, 0x30, 0x0F, 0x06, 0x1D, 38 | 0x14, 0x63, 0x6A, 0x71, 0x78, 0x47, 0x4E, 0x55, 0x5C, 0x64, 0x6D, 0x76, 39 | 0x7F, 0x40, 0x49, 0x52, 0x5B, 0x2C, 0x25, 0x3E, 0x37, 0x08, 0x01, 0x1A, 40 | 0x13, 0x7D, 0x74, 0x6F, 0x66, 0x59, 0x50, 0x4B, 0x42, 0x35, 0x3C, 0x27, 41 | 0x2E, 0x11, 0x18, 0x03, 0x0A, 0x56, 0x5F, 0x44, 0x4D, 0x72, 0x7B, 0x60, 42 | 0x69, 0x1E, 0x17, 0x0C, 0x05, 0x3A, 0x33, 0x28, 0x21, 0x4F, 0x46, 0x5D, 43 | 0x54, 0x6B, 0x62, 0x79, 0x70, 0x07, 0x0E, 0x15, 0x1C, 0x23, 0x2A, 0x31, 44 | 0x38, 0x41, 0x48, 0x53, 0x5A, 0x65, 0x6C, 0x77, 0x7E, 0x09, 0x00, 0x1B, 45 | 0x12, 0x2D, 0x24, 0x3F, 0x36, 0x58, 0x51, 0x4A, 0x43, 0x7C, 0x75, 0x6E, 46 | 0x67, 0x10, 0x19, 0x02, 0x0B, 0x34, 0x3D, 0x26, 0x2F, 0x73, 0x7A, 0x61, 47 | 0x68, 0x57, 0x5E, 0x45, 0x4C, 0x3B, 0x32, 0x29, 0x20, 0x1F, 0x16, 0x0D, 48 | 0x04, 0x6A, 0x63, 0x78, 0x71, 0x4E, 0x47, 0x5C, 0x55, 0x22, 0x2B, 0x30, 49 | 0x39, 0x06, 0x0F, 0x14, 0x1D, 0x25, 0x2C, 0x37, 0x3E, 0x01, 0x08, 0x13, 50 | 0x1A, 0x6D, 0x64, 0x7F, 0x76, 0x49, 0x40, 0x5B, 0x52, 0x3C, 0x35, 0x2E, 51 | 0x27, 0x18, 0x11, 0x0A, 0x03, 0x74, 0x7D, 0x66, 0x6F, 0x50, 0x59, 0x42, 52 | 0x4B, 0x17, 0x1E, 0x05, 0x0C, 0x33, 0x3A, 0x21, 0x28, 0x5F, 0x56, 0x4D, 53 | 0x44, 0x7B, 0x72, 0x69, 0x60, 0x0E, 0x07, 0x1C, 0x15, 0x2A, 0x23, 0x38, 54 | 0x31, 0x46, 0x4F, 0x54, 0x5D, 0x62, 0x6B, 0x70, 0x79}; 55 | 56 | static const unsigned short m_Crc16Table[256] = {0x0000, 0x1021, 0x2042, 57 | 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 58 | 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 59 | 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 60 | 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 61 | 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 62 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 0xB75B, 63 | 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 0x48C4, 0x58E5, 64 | 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 65 | 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 66 | 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 67 | 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 68 | 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 69 | 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 70 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, 0x9188, 71 | 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 72 | 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83B9, 0x9398, 0xA3FB, 73 | 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290, 0x22F3, 0x32D2, 74 | 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 75 | 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 76 | 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 77 | 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 78 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 79 | 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, 0xCB7D, 0xDB5C, 80 | 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 81 | 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 82 | 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 83 | 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 84 | 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 85 | 0x1EF0}; 86 | 87 | char crc7(const char* data, int length) 88 | { 89 | //Calculate the CRC7 checksum for the specified data block 90 | char crc = 0; 91 | for (int i = 0; i < length; i++) { 92 | crc = m_Crc7Table[(crc << 1) ^ data[i]]; 93 | } 94 | 95 | //Return the calculated checksum 96 | return crc; 97 | } 98 | 99 | unsigned short crc16(const char* data, int length) 100 | { 101 | //Calculate the CRC16 checksum for the specified data block 102 | unsigned short crc = 0; 103 | for (int i = 0; i < length; i++) { 104 | crc = (crc << 8) ^ m_Crc16Table[((crc >> 8) ^ data[i]) & 0x00FF]; 105 | } 106 | 107 | //Return the calculated checksum 108 | return crc; 109 | } 110 | 111 | void update_crc16(unsigned short *pCrc16, const char data[], size_t length) { 112 | for (size_t i = 0; i < length; i++) { 113 | *pCrc16 = (*pCrc16 << 8) ^ m_Crc16Table[((*pCrc16 >> 8) ^ data[i]) & 0x00FF]; 114 | } 115 | } 116 | /* [] END OF FILE */ 117 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/crc.h: -------------------------------------------------------------------------------- 1 | /* crc.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* Derived from: 15 | * SD/MMC File System Library 16 | * Copyright (c) 2016 Neil Thiessen 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | */ 30 | 31 | #ifndef SD_CRC_H 32 | #define SD_CRC_H 33 | 34 | #include 35 | 36 | char crc7(const char* data, int length); 37 | unsigned short crc16(const char* data, int length); 38 | void update_crc16(unsigned short *pCrc16, const char data[], size_t length); 39 | 40 | #endif 41 | 42 | /* [] END OF FILE */ 43 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/demo_logging.c: -------------------------------------------------------------------------------- 1 | /* demo_logging.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 16 | All rights reserved 17 | 18 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 19 | 20 | This file is part of the FreeRTOS distribution. 21 | 22 | FreeRTOS is free software; you can redistribute it and/or modify it under 23 | the terms of the GNU General Public License (version 2) as published by the 24 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 25 | 26 | *************************************************************************** 27 | >>! NOTE: The modification to the GPL is included to allow you to !<< 28 | >>! distribute a combined work that includes FreeRTOS without being !<< 29 | >>! obliged to provide the source code for proprietary components !<< 30 | >>! outside of the FreeRTOS kernel. !<< 31 | *************************************************************************** 32 | 33 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 34 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 35 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 36 | link: http://www.freertos.org/a00114.html 37 | 38 | *************************************************************************** 39 | * * 40 | * FreeRTOS provides completely free yet professionally developed, * 41 | * robust, strictly quality controlled, supported, and cross * 42 | * platform software that is more than just the market leader, it * 43 | * is the industry's de facto standard. * 44 | * * 45 | * Help yourself get started quickly while simultaneously helping * 46 | * to support the FreeRTOS project by purchasing a FreeRTOS * 47 | * tutorial book, reference manual, or both: * 48 | * http://www.FreeRTOS.org/Documentation * 49 | * * 50 | *************************************************************************** 51 | 52 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 53 | the FAQ page "My application does not run, what could be wrong?". Have you 54 | defined configASSERT()? 55 | 56 | http://www.FreeRTOS.org/support - In return for receiving this top quality 57 | embedded software for free we request you assist our global community by 58 | participating in the support forum. 59 | 60 | http://www.FreeRTOS.org/training - Investing in training allows your team to 61 | be as productive as possible as early as possible. Now you can receive 62 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 63 | Ltd, and the world's leading authority on the world's leading RTOS. 64 | 65 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 66 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 67 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 68 | 69 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 70 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 71 | 72 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 73 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 74 | licenses offer ticketed support, indemnification and commercial middleware. 75 | 76 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 77 | engineered and independently SIL3 certified version for use in safety and 78 | mission critical applications that require provable dependability. 79 | 80 | 1 tab == 4 spaces! 81 | */ 82 | 83 | #include 84 | #include 85 | 86 | void vLoggingPrintf( const char *pcFormat, ... ) 87 | { 88 | char pcBuffer[256] = {0}; 89 | va_list xArgs; 90 | va_start( xArgs, pcFormat ); 91 | vsnprintf( pcBuffer, sizeof(pcBuffer), pcFormat, xArgs ); 92 | va_end( xArgs ); 93 | printf("%s", pcBuffer); 94 | fflush(stdout); 95 | } 96 | /*-----------------------------------------------------------*/ 97 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/hw_config.h: -------------------------------------------------------------------------------- 1 | /* hw_config.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #include "ff.h" 17 | #include "sd_card.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | size_t sd_get_num(); 24 | sd_card_t *sd_get_by_num(size_t num); 25 | 26 | size_t spi_get_num(); 27 | spi_t *spi_get_by_num(size_t num); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | /* [] END OF FILE */ 34 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_card.h: -------------------------------------------------------------------------------- 1 | /* sd_card.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | // Note: The model used here is one FatFS per SD card. 16 | // Multiple partitions on a card are not supported. 17 | 18 | #ifndef _SD_CARD_H_ 19 | #define _SD_CARD_H_ 20 | 21 | #include 22 | // 23 | #include "hardware/gpio.h" 24 | #include "pico/mutex.h" 25 | // 26 | #include "ff.h" 27 | // 28 | #include "spi.h" 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | // "Class" representing SD Cards 35 | typedef struct { 36 | const char *pcName; 37 | spi_t *spi; 38 | // Slave select is here in sd_card_t because multiple SDs can share an SPI 39 | uint ss_gpio; // Slave select for this SD card 40 | bool use_card_detect; 41 | uint card_detect_gpio; // Card detect; ignored if !use_card_detect 42 | uint card_detected_true; // Varies with card socket; ignored if !use_card_detect 43 | // Drive strength levels for GPIO outputs. 44 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 45 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 46 | bool set_drive_strength; 47 | enum gpio_drive_strength ss_gpio_drive_strength; 48 | 49 | // Following fields are used to keep track of the state of the card: 50 | int m_Status; // Card status 51 | uint64_t sectors; // Assigned dynamically 52 | int card_type; // Assigned dynamically 53 | mutex_t mutex; 54 | FATFS fatfs; 55 | bool mounted; 56 | } sd_card_t; 57 | 58 | #define SD_BLOCK_DEVICE_ERROR_NONE 0 59 | #define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */ 60 | #define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */ 61 | #define SD_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */ 62 | #define SD_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */ 63 | #define SD_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */ 64 | #define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */ 65 | #define SD_BLOCK_DEVICE_ERROR_UNUSABLE -5007 /*!< unusable card */ 66 | #define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE -5008 /*!< No response from device */ 67 | #define SD_BLOCK_DEVICE_ERROR_CRC -5009 /*!< CRC error */ 68 | #define SD_BLOCK_DEVICE_ERROR_ERASE -5010 /*!< Erase error: reset/sequence */ 69 | #define SD_BLOCK_DEVICE_ERROR_WRITE -5011 /*!< SPI Write error: !SPI_DATA_ACCEPTED */ 70 | 71 | ///* Disk Status Bits (DSTATUS) */ 72 | // See diskio.h. 73 | //enum { 74 | // STA_NOINIT = 0x01, /* Drive not initialized */ 75 | // STA_NODISK = 0x02, /* No medium in the drive */ 76 | // STA_PROTECT = 0x04 /* Write protected */ 77 | //}; 78 | 79 | bool sd_init_driver(); 80 | int sd_init_card(sd_card_t *pSD); 81 | int sd_write_blocks(sd_card_t *pSD, const uint8_t *buffer, 82 | uint64_t ulSectorNumber, uint32_t blockCnt); 83 | int sd_read_blocks(sd_card_t *pSD, uint8_t *buffer, uint64_t ulSectorNumber, 84 | uint32_t ulSectorCount); 85 | bool sd_card_detect(sd_card_t *pSD); 86 | uint64_t sd_sectors(sd_card_t *pSD); 87 | 88 | #ifdef __cplusplus 89 | } 90 | #endif 91 | 92 | #endif 93 | /* [] END OF FILE */ 94 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_spi.c: -------------------------------------------------------------------------------- 1 | /* sd_spi.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | /* Standard includes. */ 16 | #include 17 | #include 18 | #include 19 | // 20 | #include "hardware/gpio.h" 21 | // 22 | #include "my_debug.h" 23 | #include "sd_card.h" 24 | #include "sd_spi.h" 25 | #include "spi.h" 26 | 27 | //#define TRACE_PRINTF(fmt, args...) 28 | #define TRACE_PRINTF printf // task_printf 29 | 30 | void sd_spi_go_high_frequency(sd_card_t *pSD) { 31 | uint actual = spi_set_baudrate(pSD->spi->hw_inst, pSD->spi->baud_rate); 32 | TRACE_PRINTF("%s: Actual frequency: %lu\n", __FUNCTION__, (long)actual); 33 | } 34 | void sd_spi_go_low_frequency(sd_card_t *pSD) { 35 | uint actual = spi_set_baudrate(pSD->spi->hw_inst, 400 * 1000); // Actual frequency: 398089 36 | TRACE_PRINTF("%s: Actual frequency: %lu\n", __FUNCTION__, (long)actual); 37 | } 38 | 39 | static void sd_spi_lock(sd_card_t *pSD) { 40 | spi_lock(pSD->spi); 41 | } 42 | static void sd_spi_unlock(sd_card_t *pSD) { 43 | spi_unlock(pSD->spi); 44 | } 45 | 46 | // Would do nothing if pSD->ss_gpio were set to GPIO_FUNC_SPI. 47 | static void sd_spi_select(sd_card_t *pSD) { 48 | gpio_put(pSD->ss_gpio, 0); 49 | // A fill byte seems to be necessary, sometimes: 50 | uint8_t fill = SPI_FILL_CHAR; 51 | spi_write_blocking(pSD->spi->hw_inst, &fill, 1); 52 | LED_ON(); 53 | } 54 | 55 | static void sd_spi_deselect(sd_card_t *pSD) { 56 | gpio_put(pSD->ss_gpio, 1); 57 | LED_OFF(); 58 | /* 59 | MMC/SDC enables/disables the DO output in synchronising to the SCLK. This 60 | means there is a posibility of bus conflict with MMC/SDC and another SPI 61 | slave that shares an SPI bus. Therefore to make MMC/SDC release the MISO 62 | line, the master device needs to send a byte after the CS signal is 63 | deasserted. 64 | */ 65 | uint8_t fill = SPI_FILL_CHAR; 66 | spi_write_blocking(pSD->spi->hw_inst, &fill, 1); 67 | } 68 | /* Some SD cards want to be deselected between every bus transaction */ 69 | void sd_spi_deselect_pulse(sd_card_t *pSD) { 70 | sd_spi_deselect(pSD); 71 | // tCSH Pulse duration, CS high 200 ns 72 | sd_spi_select(pSD); 73 | } 74 | void sd_spi_acquire(sd_card_t *pSD) { 75 | sd_spi_lock(pSD); 76 | sd_spi_select(pSD); 77 | } 78 | 79 | void sd_spi_release(sd_card_t *pSD) { 80 | sd_spi_deselect(pSD); 81 | sd_spi_unlock(pSD); 82 | } 83 | 84 | bool sd_spi_transfer(sd_card_t *pSD, const uint8_t *tx, uint8_t *rx, 85 | size_t length) { 86 | return spi_transfer(pSD->spi, tx, rx, length); 87 | } 88 | 89 | uint8_t sd_spi_write(sd_card_t *pSD, const uint8_t value) { 90 | // TRACE_PRINTF("%s\n", __FUNCTION__); 91 | uint8_t received = SPI_FILL_CHAR; 92 | #if 0 93 | int num = spi_write_read_blocking(pSD->spi->hw_inst, &value, &received, 1); 94 | myASSERT(1 == num); 95 | #else 96 | bool success = spi_transfer(pSD->spi, &value, &received, 1); 97 | myASSERT(success); 98 | #endif 99 | return received; 100 | } 101 | 102 | void sd_spi_send_initializing_sequence(sd_card_t * pSD) { 103 | bool old_ss = gpio_get(pSD->ss_gpio); 104 | // Set DI and CS high and apply 74 or more clock pulses to SCLK: 105 | gpio_put(pSD->ss_gpio, 1); 106 | uint8_t ones[10]; 107 | memset(ones, 0xFF, sizeof ones); 108 | absolute_time_t timeout_time = make_timeout_time_ms(1); 109 | do { 110 | sd_spi_transfer(pSD, ones, NULL, sizeof ones); 111 | } while (0 < absolute_time_diff_us(get_absolute_time(), timeout_time)); 112 | gpio_put(pSD->ss_gpio, old_ss); 113 | } 114 | 115 | void sd_spi_init_pl022(sd_card_t *pSD) { 116 | // Let the PL022 SPI handle it. 117 | // the CS line is brought high between each byte during transmission. 118 | gpio_set_function(pSD->ss_gpio, GPIO_FUNC_SPI); 119 | } 120 | 121 | /* [] END OF FILE */ 122 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_spi.h: -------------------------------------------------------------------------------- 1 | /* sd_spi.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #ifndef _SD_SPI_H_ 16 | #define _SD_SPI_H_ 17 | 18 | #include 19 | #include "sd_card.h" 20 | 21 | /* Transfer tx to SPI while receiving SPI to rx. 22 | tx or rx can be NULL if not important. */ 23 | bool sd_spi_transfer(sd_card_t *pSD, const uint8_t *tx, uint8_t *rx, size_t length); 24 | uint8_t sd_spi_write(sd_card_t *pSD, const uint8_t value); 25 | void sd_spi_deselect_pulse(sd_card_t *pSD); 26 | void sd_spi_acquire(sd_card_t *pSD); 27 | void sd_spi_release(sd_card_t *pSD); 28 | void sd_spi_go_low_frequency(sd_card_t *this); 29 | void sd_spi_go_high_frequency(sd_card_t *this); 30 | 31 | /* 32 | After power up, the host starts the clock and sends the initializing sequence on the CMD line. 33 | This sequence is a contiguous stream of logical ‘1’s. The sequence length is the maximum of 1msec, 34 | 74 clocks or the supply-ramp-uptime; the additional 10 clocks 35 | (over the 64 clocks after what the card should be ready for communication) is 36 | provided to eliminate power-up synchronization problems. 37 | */ 38 | void sd_spi_send_initializing_sequence(sd_card_t * pSD); 39 | 40 | #endif 41 | 42 | /* [] END OF FILE */ 43 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/spi.c: -------------------------------------------------------------------------------- 1 | /* spi.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #include 16 | // 17 | #include "pico/stdlib.h" 18 | #include "pico/mutex.h" 19 | #include "pico/sem.h" 20 | // 21 | #include "my_debug.h" 22 | // 23 | #include "spi.h" 24 | 25 | static bool irqChannel1 = false; 26 | static bool irqShared = true; 27 | 28 | void spi_irq_handler(spi_t *pSPI) { 29 | if (irqChannel1) { 30 | if (dma_hw->ints1 & 1u << pSPI->rx_dma) { // Ours? 31 | dma_hw->ints1 = 1u << pSPI->rx_dma; // clear it 32 | myASSERT(!dma_channel_is_busy(pSPI->rx_dma)); 33 | sem_release(&pSPI->sem); 34 | } 35 | } else { 36 | if (dma_hw->ints0 & 1u << pSPI->rx_dma) { // Ours? 37 | dma_hw->ints0 = 1u << pSPI->rx_dma; // clear it 38 | myASSERT(!dma_channel_is_busy(pSPI->rx_dma)); 39 | sem_release(&pSPI->sem); 40 | } 41 | } 42 | } 43 | 44 | void set_spi_dma_irq_channel(bool useChannel1, bool shared) { 45 | irqChannel1 = useChannel1; 46 | irqShared = shared; 47 | } 48 | 49 | // SPI Transfer: Read & Write (simultaneously) on SPI bus 50 | // If the data that will be received is not important, pass NULL as rx. 51 | // If the data that will be transmitted is not important, 52 | // pass NULL as tx and then the SPI_FILL_CHAR is sent out as each data 53 | // element. 54 | bool spi_transfer(spi_t *pSPI, const uint8_t *tx, uint8_t *rx, size_t length) { 55 | // myASSERT(512 == length || 1 == length); 56 | myASSERT(tx || rx); 57 | // myASSERT(!(tx && rx)); 58 | 59 | // tx write increment is already false 60 | if (tx) { 61 | channel_config_set_read_increment(&pSPI->tx_dma_cfg, true); 62 | } else { 63 | static const uint8_t dummy = SPI_FILL_CHAR; 64 | tx = &dummy; 65 | channel_config_set_read_increment(&pSPI->tx_dma_cfg, false); 66 | } 67 | 68 | // rx read increment is already false 69 | if (rx) { 70 | channel_config_set_write_increment(&pSPI->rx_dma_cfg, true); 71 | } else { 72 | static uint8_t dummy = 0xA5; 73 | rx = &dummy; 74 | channel_config_set_write_increment(&pSPI->rx_dma_cfg, false); 75 | } 76 | // Clear the interrupt request. 77 | dma_hw->ints0 = 1u << pSPI->rx_dma; 78 | 79 | dma_channel_configure(pSPI->tx_dma, &pSPI->tx_dma_cfg, 80 | &spi_get_hw(pSPI->hw_inst)->dr, // write address 81 | tx, // read address 82 | length, // element count (each element is of 83 | // size transfer_data_size) 84 | false); // start 85 | dma_channel_configure(pSPI->rx_dma, &pSPI->rx_dma_cfg, 86 | rx, // write address 87 | &spi_get_hw(pSPI->hw_inst)->dr, // read address 88 | length, // element count (each element is of 89 | // size transfer_data_size) 90 | false); // start 91 | 92 | // start them exactly simultaneously to avoid races (in extreme cases 93 | // the FIFO could overflow) 94 | dma_start_channel_mask((1u << pSPI->tx_dma) | (1u << pSPI->rx_dma)); 95 | 96 | /* Timeout 1 sec */ 97 | uint32_t timeOut = 1000; 98 | /* Wait until master completes transfer or time out has occured. */ 99 | bool rc = sem_acquire_timeout_ms( 100 | &pSPI->sem, timeOut); // Wait for notification from ISR 101 | if (!rc) { 102 | // If the timeout is reached the function will return false 103 | DBG_PRINTF("Notification wait timed out in %s\n", __FUNCTION__); 104 | return false; 105 | } 106 | // Shouldn't be necessary: 107 | dma_channel_wait_for_finish_blocking(pSPI->tx_dma); 108 | dma_channel_wait_for_finish_blocking(pSPI->rx_dma); 109 | 110 | myASSERT(!dma_channel_is_busy(pSPI->tx_dma)); 111 | myASSERT(!dma_channel_is_busy(pSPI->rx_dma)); 112 | 113 | return true; 114 | } 115 | 116 | void spi_lock(spi_t *pSPI) { 117 | myASSERT(mutex_is_initialized(&pSPI->mutex)); 118 | mutex_enter_blocking(&pSPI->mutex); 119 | } 120 | void spi_unlock(spi_t *pSPI) { 121 | myASSERT(mutex_is_initialized(&pSPI->mutex)); 122 | mutex_exit(&pSPI->mutex); 123 | } 124 | 125 | bool my_spi_init(spi_t *pSPI) { 126 | auto_init_mutex(my_spi_init_mutex); 127 | mutex_enter_blocking(&my_spi_init_mutex); 128 | if (!pSPI->initialized) { 129 | //// The SPI may be shared (using multiple SSs); protect it 130 | //pSPI->mutex = xSemaphoreCreateRecursiveMutex(); 131 | //xSemaphoreTakeRecursive(pSPI->mutex, portMAX_DELAY); 132 | if (!mutex_is_initialized(&pSPI->mutex)) mutex_init(&pSPI->mutex); 133 | spi_lock(pSPI); 134 | 135 | // For the IRQ notification: 136 | sem_init(&pSPI->sem, 0, 1); 137 | 138 | /* Configure component */ 139 | // Enable SPI at 100 kHz and connect to GPIOs 140 | spi_init(pSPI->hw_inst, 100 * 1000); 141 | spi_set_format(pSPI->hw_inst, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); 142 | 143 | gpio_set_function(pSPI->miso_gpio, GPIO_FUNC_SPI); 144 | gpio_set_function(pSPI->mosi_gpio, GPIO_FUNC_SPI); 145 | gpio_set_function(pSPI->sck_gpio, GPIO_FUNC_SPI); 146 | // ss_gpio is initialized in sd_init_driver() 147 | 148 | // Slew rate limiting levels for GPIO outputs. 149 | // enum gpio_slew_rate { GPIO_SLEW_RATE_SLOW = 0, GPIO_SLEW_RATE_FAST = 1 } 150 | // void gpio_set_slew_rate (uint gpio,enum gpio_slew_rate slew) 151 | // Default appears to be GPIO_SLEW_RATE_SLOW. 152 | 153 | // Drive strength levels for GPIO outputs. 154 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 155 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 156 | // enum gpio_drive_strength gpio_get_drive_strength (uint gpio) 157 | if (pSPI->set_drive_strength) { 158 | gpio_set_drive_strength(pSPI->mosi_gpio, pSPI->mosi_gpio_drive_strength); 159 | gpio_set_drive_strength(pSPI->sck_gpio, pSPI->sck_gpio_drive_strength); 160 | } 161 | 162 | // SD cards' DO MUST be pulled up. 163 | gpio_pull_up(pSPI->miso_gpio); 164 | 165 | // Grab some unused dma channels 166 | pSPI->tx_dma = dma_claim_unused_channel(true); 167 | pSPI->rx_dma = dma_claim_unused_channel(true); 168 | 169 | pSPI->tx_dma_cfg = dma_channel_get_default_config(pSPI->tx_dma); 170 | pSPI->rx_dma_cfg = dma_channel_get_default_config(pSPI->rx_dma); 171 | channel_config_set_transfer_data_size(&pSPI->tx_dma_cfg, DMA_SIZE_8); 172 | channel_config_set_transfer_data_size(&pSPI->rx_dma_cfg, DMA_SIZE_8); 173 | 174 | // We set the outbound DMA to transfer from a memory buffer to the SPI 175 | // transmit FIFO paced by the SPI TX FIFO DREQ The default is for the 176 | // read address to increment every element (in this case 1 byte - 177 | // DMA_SIZE_8) and for the write address to remain unchanged. 178 | channel_config_set_dreq(&pSPI->tx_dma_cfg, spi_get_index(pSPI->hw_inst) 179 | ? DREQ_SPI1_TX 180 | : DREQ_SPI0_TX); 181 | channel_config_set_write_increment(&pSPI->tx_dma_cfg, false); 182 | 183 | // We set the inbound DMA to transfer from the SPI receive FIFO to a 184 | // memory buffer paced by the SPI RX FIFO DREQ We coinfigure the read 185 | // address to remain unchanged for each element, but the write address 186 | // to increment (so data is written throughout the buffer) 187 | channel_config_set_dreq(&pSPI->rx_dma_cfg, spi_get_index(pSPI->hw_inst) 188 | ? DREQ_SPI1_RX 189 | : DREQ_SPI0_RX); 190 | channel_config_set_read_increment(&pSPI->rx_dma_cfg, false); 191 | 192 | /* Theory: we only need an interrupt on rx complete, 193 | since if rx is complete, tx must also be complete. */ 194 | 195 | // Configure the processor to run dma_handler() when DMA IRQ 0/1 is 196 | // asserted: 197 | int irq = irqChannel1 ? DMA_IRQ_1 : DMA_IRQ_0; 198 | if (irqShared) { 199 | irq_add_shared_handler( 200 | irq, pSPI->dma_isr, 201 | PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); 202 | } else { 203 | irq_set_exclusive_handler(irq, pSPI->dma_isr); 204 | } 205 | 206 | // Tell the DMA to raise IRQ line 0/1 when the channel finishes a block 207 | if (irqChannel1) { 208 | dma_channel_set_irq1_enabled(pSPI->rx_dma, true); 209 | } else { 210 | dma_channel_set_irq0_enabled(pSPI->rx_dma, true); 211 | } 212 | irq_set_enabled(irq, true); 213 | LED_INIT(); 214 | pSPI->initialized = true; 215 | spi_unlock(pSPI); 216 | } 217 | mutex_exit(&my_spi_init_mutex); 218 | return true; 219 | } 220 | 221 | /* [] END OF FILE */ 222 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/spi.h: -------------------------------------------------------------------------------- 1 | /* spi.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | // 19 | // Pico includes 20 | #include "hardware/dma.h" 21 | #include "hardware/gpio.h" 22 | #include "hardware/irq.h" 23 | #include "hardware/spi.h" 24 | #include "pico/mutex.h" 25 | #include "pico/sem.h" 26 | #include "pico/types.h" 27 | 28 | #define SPI_FILL_CHAR (0xFF) 29 | 30 | // "Class" representing SPIs 31 | typedef struct { 32 | // SPI HW 33 | spi_inst_t *hw_inst; 34 | uint miso_gpio; // SPI MISO GPIO number (not pin number) 35 | uint mosi_gpio; 36 | uint sck_gpio; 37 | uint baud_rate; 38 | 39 | // Drive strength levels for GPIO outputs. 40 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 41 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 42 | bool set_drive_strength; 43 | enum gpio_drive_strength mosi_gpio_drive_strength; 44 | enum gpio_drive_strength sck_gpio_drive_strength; 45 | 46 | // State variables: 47 | uint tx_dma; 48 | uint rx_dma; 49 | dma_channel_config tx_dma_cfg; 50 | dma_channel_config rx_dma_cfg; 51 | irq_handler_t dma_isr; 52 | bool initialized; 53 | semaphore_t sem; 54 | mutex_t mutex; 55 | } spi_t; 56 | 57 | #ifdef __cplusplus 58 | extern "C" { 59 | #endif 60 | 61 | // SPI DMA interrupts 62 | void __not_in_flash_func(spi_irq_handler)(spi_t *pSPI); 63 | 64 | bool __not_in_flash_func(spi_transfer)(spi_t *pSPI, const uint8_t *tx, uint8_t *rx, size_t length); 65 | void spi_lock(spi_t *pSPI); 66 | void spi_unlock(spi_t *pSPI); 67 | bool my_spi_init(spi_t *pSPI); 68 | void set_spi_dma_irq_channel(bool useChannel1, bool shared); 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #ifndef NO_PICO_LED 75 | # define USE_LED 1 76 | #endif 77 | 78 | #if USE_LED 79 | # define LED_PIN 25 80 | # define LED_INIT() \ 81 | { \ 82 | gpio_init(LED_PIN); \ 83 | gpio_set_dir(LED_PIN, GPIO_OUT); \ 84 | } 85 | # define LED_ON() gpio_put(LED_PIN, 1) 86 | # define LED_OFF() gpio_put(LED_PIN, 0) 87 | #else 88 | # define LED_ON() 89 | # define LED_OFF() 90 | # define LED_INIT() 91 | #endif 92 | 93 | /* [] END OF FILE */ 94 | -------------------------------------------------------------------------------- /FatFs_SPI/src/f_util.c: -------------------------------------------------------------------------------- 1 | /* f_util.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include "ff.h" 15 | 16 | const char *FRESULT_str(FRESULT i) { 17 | switch (i) { 18 | case FR_OK: 19 | return "Succeeded"; 20 | case FR_DISK_ERR: 21 | return "A hard error occurred in the low level disk I/O layer"; 22 | case FR_INT_ERR: 23 | return "Assertion failed"; 24 | case FR_NOT_READY: 25 | return "The physical drive cannot work"; 26 | case FR_NO_FILE: 27 | return "Could not find the file"; 28 | case FR_NO_PATH: 29 | return "Could not find the path"; 30 | case FR_INVALID_NAME: 31 | return "The path name format is invalid"; 32 | case FR_DENIED: 33 | return "Access denied due to prohibited access or directory full"; 34 | case FR_EXIST: 35 | return "Access denied due to prohibited access (exists)"; 36 | case FR_INVALID_OBJECT: 37 | return "The file/directory object is invalid"; 38 | case FR_WRITE_PROTECTED: 39 | return "The physical drive is write protected"; 40 | case FR_INVALID_DRIVE: 41 | return "The logical drive number is invalid"; 42 | case FR_NOT_ENABLED: 43 | return "The volume has no work area (mount)"; 44 | case FR_NO_FILESYSTEM: 45 | return "There is no valid FAT volume"; 46 | case FR_MKFS_ABORTED: 47 | return "The f_mkfs() aborted due to any problem"; 48 | case FR_TIMEOUT: 49 | return "Could not get a grant to access the volume within defined " 50 | "period"; 51 | case FR_LOCKED: 52 | return "The operation is rejected according to the file sharing " 53 | "policy"; 54 | case FR_NOT_ENOUGH_CORE: 55 | return "LFN working buffer could not be allocated"; 56 | case FR_TOO_MANY_OPEN_FILES: 57 | return "Number of open files > FF_FS_LOCK"; 58 | case FR_INVALID_PARAMETER: 59 | return "Given parameter is invalid"; 60 | default: 61 | return "Unknown"; 62 | } 63 | } 64 | 65 | FRESULT delete_node ( 66 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 67 | UINT sz_buff, /* Size of path name buffer (items) */ 68 | FILINFO* fno /* Name read buffer */ 69 | ) 70 | { 71 | UINT i, j; 72 | FRESULT fr; 73 | DIR dir; 74 | 75 | 76 | fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */ 77 | if (fr != FR_OK) return fr; 78 | 79 | for (i = 0; path[i]; i++) ; /* Get current path length */ 80 | path[i++] = '/'; 81 | 82 | for (;;) { 83 | fr = f_readdir(&dir, fno); /* Get a directory item */ 84 | if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */ 85 | j = 0; 86 | do { /* Make a path name */ 87 | if (i + j >= sz_buff) { /* Buffer over flow? */ 88 | fr = 100; break; /* Fails with 100 when buffer overflow */ 89 | } 90 | path[i + j] = fno->fname[j]; 91 | } while (fno->fname[j++]); 92 | if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */ 93 | fr = delete_node(path, sz_buff, fno); 94 | } else { /* Item is a file */ 95 | fr = f_unlink(path); 96 | } 97 | if (fr != FR_OK) break; 98 | } 99 | 100 | path[--i] = 0; /* Restore the path name */ 101 | f_closedir(&dir); 102 | 103 | if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */ 104 | return fr; 105 | } 106 | -------------------------------------------------------------------------------- /FatFs_SPI/src/ff_stdio.c: -------------------------------------------------------------------------------- 1 | /* ff_stdio.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | // For compatibility with FreeRTOS+FAT API 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "my_debug.h" 23 | // 24 | #include "f_util.h" 25 | #include "ff_stdio.h" 26 | 27 | #define TRACE_PRINTF(fmt, args...) {} 28 | //#define TRACE_PRINTF printf 29 | 30 | static BYTE posix2mode(const char *pcMode) { 31 | if (0 == strcmp("r", pcMode)) return FA_READ; 32 | if (0 == strcmp("r+", pcMode)) return FA_READ | FA_WRITE; 33 | if (0 == strcmp("w", pcMode)) return FA_CREATE_ALWAYS | FA_WRITE; 34 | if (0 == strcmp("w+", pcMode)) return FA_CREATE_ALWAYS | FA_WRITE | FA_READ; 35 | if (0 == strcmp("a", pcMode)) return FA_OPEN_APPEND | FA_WRITE; 36 | if (0 == strcmp("a+", pcMode)) return FA_OPEN_APPEND | FA_WRITE | FA_READ; 37 | if (0 == strcmp("wx", pcMode)) return FA_CREATE_NEW | FA_WRITE; 38 | if (0 == strcmp("w+x", pcMode)) return FA_CREATE_NEW | FA_WRITE | FA_READ; 39 | return 0; 40 | } 41 | 42 | int fresult2errno(FRESULT fr) { 43 | switch (fr) { 44 | case FR_OK: 45 | return 0; 46 | case FR_DISK_ERR: 47 | return EIO; 48 | case FR_INT_ERR: 49 | return EIO; 50 | case FR_NOT_READY: 51 | return EIO; 52 | case FR_NO_FILE: 53 | return ENOENT; 54 | case FR_NO_PATH: 55 | return ENOENT; 56 | case FR_INVALID_NAME: 57 | return ENAMETOOLONG; 58 | case FR_DENIED: 59 | return EACCES; 60 | case FR_EXIST: 61 | return EEXIST; 62 | case FR_INVALID_OBJECT: 63 | return EIO; 64 | case FR_WRITE_PROTECTED: 65 | return EACCES; 66 | case FR_INVALID_DRIVE: 67 | return ENOENT; 68 | case FR_NOT_ENABLED: 69 | return ENOENT; 70 | case FR_NO_FILESYSTEM: 71 | return ENOENT; 72 | case FR_MKFS_ABORTED: 73 | return EIO; 74 | case FR_TIMEOUT: 75 | return EIO; 76 | case FR_LOCKED: 77 | return EACCES; 78 | case FR_NOT_ENOUGH_CORE: 79 | return ENOMEM; 80 | case FR_TOO_MANY_OPEN_FILES: 81 | return ENFILE; 82 | case FR_INVALID_PARAMETER: 83 | return ENOSYS; 84 | default: 85 | return -1; 86 | } 87 | } 88 | 89 | FF_FILE *ff_fopen(const char *pcFile, const char *pcMode) { 90 | TRACE_PRINTF("%s\n", __func__); 91 | // FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); 92 | // /* Open or create a file */ FRESULT f_open ( 93 | // FIL* fp, /* [OUT] Pointer to the file object structure */ 94 | // const TCHAR* path, /* [IN] File name */ 95 | // BYTE mode /* [IN] Mode flags */ 96 | //); 97 | FIL *fp = malloc(sizeof(FIL)); 98 | if (!fp) { 99 | errno = ENOMEM; 100 | return NULL; 101 | } 102 | FRESULT fr = f_open(fp, pcFile, posix2mode(pcMode)); 103 | errno = fresult2errno(fr); 104 | if (FR_OK != fr) { 105 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 106 | free(fp); 107 | fp = 0; 108 | } 109 | return fp; 110 | } 111 | int ff_fclose(FF_FILE *pxStream) { 112 | TRACE_PRINTF("%s\n", __func__); 113 | // FRESULT f_close ( 114 | // FIL* fp /* [IN] Pointer to the file object */ 115 | //); 116 | FRESULT fr = f_close(pxStream); 117 | if (FR_OK != fr) 118 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 119 | errno = fresult2errno(fr); 120 | free(pxStream); 121 | if (FR_OK == fr) 122 | return 0; 123 | else 124 | return -1; 125 | } 126 | // Populates an ff_stat_struct with information about a file. 127 | int ff_stat(const char *pcFileName, FF_Stat_t *pxStatBuffer) { 128 | TRACE_PRINTF("%s\n", __func__); 129 | // FRESULT f_stat ( 130 | // const TCHAR* path, /* [IN] Object name */ 131 | // FILINFO* fno /* [OUT] FILINFO structure */ 132 | //); 133 | myASSERT(pxStatBuffer); 134 | FILINFO filinfo; 135 | FRESULT fr = f_stat(pcFileName, &filinfo); 136 | pxStatBuffer->st_size = filinfo.fsize; 137 | if (FR_OK != fr) 138 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 139 | errno = fresult2errno(fr); 140 | if (FR_OK == fr) 141 | return 0; 142 | else 143 | return -1; 144 | } 145 | size_t ff_fwrite(const void *pvBuffer, size_t xSize, size_t xItems, 146 | FF_FILE *pxStream) { 147 | TRACE_PRINTF("%s\n", __func__); 148 | // FRESULT f_write ( 149 | // FIL* fp, /* [IN] Pointer to the file object structure */ 150 | // const void* buff, /* [IN] Pointer to the data to be written */ 151 | // UINT btw, /* [IN] Number of bytes to write */ 152 | // UINT* bw /* [OUT] Pointer to the variable to return number of 153 | // bytes written */ 154 | //); 155 | UINT bw = 0; 156 | FRESULT fr = f_write(pxStream, pvBuffer, xSize * xItems, &bw); 157 | if (FR_OK != fr) 158 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 159 | errno = fresult2errno(fr); 160 | return bw / xSize; 161 | } 162 | size_t ff_fread(void *pvBuffer, size_t xSize, size_t xItems, 163 | FF_FILE *pxStream) { 164 | TRACE_PRINTF("%s\n", __func__); 165 | // FRESULT f_read ( 166 | // FIL* fp, /* [IN] File object */ 167 | // void* buff, /* [OUT] Buffer to store read data */ 168 | // UINT btr, /* [IN] Number of bytes to read */ 169 | // UINT* br /* [OUT] Number of bytes read */ 170 | //); 171 | UINT br = 0; 172 | FRESULT fr = f_read(pxStream, pvBuffer, xSize * xItems, &br); 173 | if (FR_OK != fr) 174 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 175 | errno = fresult2errno(fr); 176 | return br / xSize; 177 | } 178 | int ff_chdir(const char *pcDirectoryName) { 179 | TRACE_PRINTF("%s\n", __func__); 180 | // FRESULT f_chdir ( 181 | // const TCHAR* path /* [IN] Path name */ 182 | //); 183 | FRESULT fr = f_chdir(pcDirectoryName); 184 | if (FR_OK != fr) 185 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 186 | errno = fresult2errno(fr); 187 | if (FR_OK == fr) 188 | return 0; 189 | else 190 | return -1; 191 | } 192 | char *ff_getcwd(char *pcBuffer, size_t xBufferLength) { 193 | TRACE_PRINTF("%s\n", __func__); 194 | // FRESULT f_getcwd ( 195 | // TCHAR* buff, /* [OUT] Buffer to return path name */ 196 | // UINT len /* [IN] The length of the buffer */ 197 | //); 198 | char buf[xBufferLength]; 199 | FRESULT fr = f_getcwd(buf, xBufferLength); 200 | if (FR_OK != fr) 201 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 202 | errno = fresult2errno(fr); 203 | // If the current working directory name was successfully written to 204 | // pcBuffer then pcBuffer is returned. Otherwise NULL is returned. 205 | if (FR_OK == fr) { 206 | if ('/' != buf[0]) { 207 | // Strip off drive prefix: 208 | char *p = strchr(buf, ':'); 209 | if (p) 210 | ++p; 211 | else 212 | p = buf; 213 | strncpy(pcBuffer, p, xBufferLength); 214 | } 215 | return pcBuffer; 216 | } else { 217 | return NULL; 218 | } 219 | } 220 | int ff_mkdir(const char *pcDirectoryName) { 221 | TRACE_PRINTF("%s(pxStream=%s)\n", __func__, pcDirectoryName); 222 | FRESULT fr = f_mkdir(pcDirectoryName); 223 | if (FR_OK != fr && FR_EXIST != fr) 224 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 225 | errno = fresult2errno(fr); 226 | if (FR_OK == fr || FR_EXIST == fr) 227 | return 0; 228 | else 229 | return -1; 230 | } 231 | int ff_fputc(int iChar, FF_FILE *pxStream) { 232 | // TRACE_PRINTF("%s(iChar=%c,pxStream=%p)\n", __func__, iChar, pxStream); 233 | // FRESULT f_write ( 234 | // FIL* fp, /* [IN] Pointer to the file object structure */ 235 | // const void* buff, /* [IN] Pointer to the data to be written */ 236 | // UINT btw, /* [IN] Number of bytes to write */ 237 | // UINT* bw /* [OUT] Pointer to the variable to return number of 238 | // bytes written */ 239 | //); 240 | UINT bw = 0; 241 | uint8_t buff[1]; 242 | buff[0] = iChar; 243 | FRESULT fr = f_write(pxStream, buff, 1, &bw); 244 | if (FR_OK != fr) 245 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 246 | errno = fresult2errno(fr); 247 | // On success the byte written to the file is returned. If any other value 248 | // is returned then the byte was not written to the file and the task's 249 | // errno will be set to indicate the reason. 250 | if (1 == bw) 251 | return iChar; 252 | else { 253 | return -1; 254 | } 255 | } 256 | int ff_fgetc(FF_FILE *pxStream) { 257 | // TRACE_PRINTF("%s(pxStream=%p)\n", __func__, pxStream); 258 | // FRESULT f_read ( 259 | // FIL* fp, /* [IN] File object */ 260 | // void* buff, /* [OUT] Buffer to store read data */ 261 | // UINT btr, /* [IN] Number of bytes to read */ 262 | // UINT* br /* [OUT] Number of bytes read */ 263 | //); 264 | uint8_t buff[1] = {0}; 265 | UINT br; 266 | FRESULT fr = f_read(pxStream, buff, 1, &br); 267 | if (FR_OK != fr) 268 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 269 | errno = fresult2errno(fr); 270 | // On success the byte read from the file system is returned. If a byte 271 | // could not be read from the file because the read position is already at 272 | // the end of the file then FF_EOF is returned. 273 | if (1 == br) 274 | return buff[0]; 275 | else 276 | return FF_EOF; 277 | } 278 | int ff_rmdir(const char *pcDirectory) { 279 | TRACE_PRINTF("%s\n", __func__); 280 | // FRESULT f_unlink ( 281 | // const TCHAR* path /* [IN] Object name */ 282 | //); 283 | FRESULT fr = f_unlink(pcDirectory); 284 | // If the directory was removed successfully then zero is returned. If the 285 | // directory could not be removed then -1 is returned and the task's errno 286 | // is set to indicate the reason. 287 | errno = fresult2errno(fr); 288 | if (FR_OK == fr) 289 | return 0; 290 | else 291 | return -1; 292 | } 293 | int ff_remove(const char *pcPath) { 294 | TRACE_PRINTF("%s\n", __func__); 295 | FRESULT fr = f_unlink(pcPath); 296 | errno = fresult2errno(fr); 297 | if (FR_OK == fr) 298 | return 0; 299 | else 300 | return -1; 301 | } 302 | long ff_ftell(FF_FILE *pxStream) { 303 | TRACE_PRINTF("%s\n", __func__); 304 | // FSIZE_t f_tell ( 305 | // FIL* fp /* [IN] File object */ 306 | //); 307 | FSIZE_t pos = f_tell(pxStream); 308 | myASSERT(pos < LONG_MAX); 309 | return pos; 310 | } 311 | int ff_fseek(FF_FILE *pxStream, int iOffset, int iWhence) { 312 | TRACE_PRINTF("%s\n", __func__); 313 | FRESULT fr = -1; 314 | switch (iWhence) { 315 | case FF_SEEK_CUR: // The current file position. 316 | if ((int)f_tell(pxStream) + iOffset < 0) return -1; 317 | fr = f_lseek(pxStream, f_tell(pxStream) + iOffset); 318 | break; 319 | case FF_SEEK_END: // The end of the file. 320 | if ((int)f_size(pxStream) + iOffset < 0) return -1; 321 | fr = f_lseek(pxStream, f_size(pxStream) + iOffset); 322 | break; 323 | case FF_SEEK_SET: // The beginning of the file. 324 | if (iOffset < 0) return -1; 325 | fr = f_lseek(pxStream, iOffset); 326 | break; 327 | default: 328 | myASSERT(!"Bad iWhence"); 329 | } 330 | errno = fresult2errno(fr); 331 | if (FR_OK == fr) 332 | return 0; 333 | else 334 | return -1; 335 | } 336 | int ff_findfirst(const char *pcDirectory, FF_FindData_t *pxFindData) { 337 | TRACE_PRINTF("%s(%s)\n", __func__, pcDirectory); 338 | // FRESULT f_findfirst ( 339 | // DIR* dp, /* [OUT] Poninter to the directory object */ 340 | // FILINFO* fno, /* [OUT] Pointer to the file information structure 341 | // */ const TCHAR* path, /* [IN] Pointer to the directory name to be 342 | // opened */ const TCHAR* pattern /* [IN] Pointer to the matching pattern 343 | // string */ 344 | //); 345 | char buf1[ffconfigMAX_FILENAME] = {0}; 346 | if (pcDirectory[0]) { 347 | FRESULT fr = f_getcwd(buf1, sizeof buf1); 348 | errno = fresult2errno(fr); 349 | if (FR_OK != fr) return -1; 350 | fr = f_chdir(pcDirectory); 351 | errno = fresult2errno(fr); 352 | if (FR_OK != fr) return -1; 353 | } 354 | char buf2[ffconfigMAX_FILENAME] = {0}; 355 | FRESULT fr = f_getcwd(buf2, sizeof buf2); 356 | TRACE_PRINTF("%s: f_findfirst(path=%s)\n", __func__, buf2); 357 | fr = f_findfirst(&pxFindData->dir, &pxFindData->fileinfo, buf2, "*"); 358 | errno = fresult2errno(fr); 359 | pxFindData->pcFileName = pxFindData->fileinfo.fname; 360 | pxFindData->ulFileSize = pxFindData->fileinfo.fsize; 361 | TRACE_PRINTF("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname); 362 | if (pcDirectory[0]) { 363 | FRESULT fr2 = f_chdir(buf1); 364 | errno = fresult2errno(fr2); 365 | if (FR_OK != fr2) return -1; 366 | } 367 | if (FR_OK == fr) 368 | return 0; 369 | else 370 | return -1; 371 | } 372 | int ff_findnext(FF_FindData_t *pxFindData) { 373 | TRACE_PRINTF("%s\n", __func__); 374 | // FRESULT f_findnext ( 375 | // DIR* dp, /* [IN] Poninter to the directory object */ 376 | // FILINFO* fno /* [OUT] Pointer to the file information structure 377 | // */ 378 | //); 379 | FRESULT fr = f_findnext(&pxFindData->dir, &pxFindData->fileinfo); 380 | errno = fresult2errno(fr); 381 | pxFindData->pcFileName = pxFindData->fileinfo.fname; 382 | pxFindData->ulFileSize = pxFindData->fileinfo.fsize; 383 | TRACE_PRINTF("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname); 384 | if (FR_OK == fr && pxFindData->fileinfo.fname[0]) { 385 | return 0; 386 | } else { 387 | return -1; 388 | } 389 | } 390 | FF_FILE *ff_truncate(const char *pcFileName, long lTruncateSize) { 391 | TRACE_PRINTF("%s\n", __func__); 392 | FIL *fp = malloc(sizeof(FIL)); 393 | if (!fp) { 394 | errno = ENOMEM; 395 | return NULL; 396 | } 397 | FRESULT fr = f_open(fp, pcFileName, FA_OPEN_APPEND | FA_WRITE); 398 | if (FR_OK != fr) 399 | printf("%s: f_open error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 400 | errno = fresult2errno(fr); 401 | if (FR_OK != fr) return NULL; 402 | while (f_tell(fp) < (FSIZE_t)lTruncateSize) { 403 | UINT bw = 0; 404 | char c = 0; 405 | fr = f_write(fp, &c, 1, &bw); 406 | if (FR_OK != fr) 407 | TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 408 | errno = fresult2errno(fr); 409 | if (1 != bw) return NULL; 410 | } 411 | fr = f_lseek(fp, lTruncateSize); 412 | errno = fresult2errno(fr); 413 | if (FR_OK != fr) 414 | printf("%s: f_lseek error: %s (%d)\n", __func__, FRESULT_str(fr), fr); 415 | if (FR_OK != fr) return NULL; 416 | fr = f_truncate(fp); 417 | if (FR_OK != fr) 418 | printf("%s: f_truncate error: %s (%d)\n", __func__, FRESULT_str(fr), 419 | fr); 420 | errno = fresult2errno(fr); 421 | if (FR_OK == fr) 422 | return fp; 423 | else 424 | return NULL; 425 | } 426 | int ff_seteof(FF_FILE *pxStream) { 427 | TRACE_PRINTF("%s\n", __func__); 428 | FRESULT fr = f_truncate(pxStream); 429 | errno = fresult2errno(fr); 430 | if (FR_OK == fr) 431 | return 0; 432 | else 433 | return FF_EOF; 434 | } 435 | int ff_rename(const char *pcOldName, const char *pcNewName, 436 | int bDeleteIfExists) { 437 | TRACE_PRINTF("%s\n", __func__); 438 | // FRESULT f_rename ( 439 | // const TCHAR* old_name, /* [IN] Old object name */ 440 | // const TCHAR* new_name /* [IN] New object name */ 441 | //); 442 | // Any object with this path name except old_name must not be exist, or the 443 | // function fails with FR_EXIST. 444 | if (bDeleteIfExists) f_unlink(pcNewName); 445 | FRESULT fr = f_rename(pcOldName, pcNewName); 446 | errno = fresult2errno(fr); 447 | if (FR_OK == fr) 448 | return 0; 449 | else 450 | return -1; 451 | } 452 | char *ff_fgets(char *pcBuffer, size_t xCount, FF_FILE *pxStream) { 453 | TRACE_PRINTF("%s\n", __func__); 454 | TCHAR *p = f_gets(pcBuffer, xCount, pxStream); 455 | // On success a pointer to pcBuffer is returned. If there is a read error 456 | // then NULL is returned and the task's errno is set to indicate the reason. 457 | if (p == pcBuffer) 458 | return pcBuffer; 459 | else { 460 | errno = EIO; 461 | return NULL; 462 | } 463 | } 464 | -------------------------------------------------------------------------------- /FatFs_SPI/src/glue.c: -------------------------------------------------------------------------------- 1 | /* glue.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /*-----------------------------------------------------------------------*/ 15 | /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ 16 | /*-----------------------------------------------------------------------*/ 17 | /* If a working storage control module is available, it should be */ 18 | /* attached to the FatFs via a glue function rather than modifying it. */ 19 | /* This is an example of glue functions to attach various exsisting */ 20 | /* storage control modules to the FatFs module with a defined API. */ 21 | /*-----------------------------------------------------------------------*/ 22 | #include 23 | // 24 | #include "ff.h" /* Obtains integer types */ 25 | // 26 | #include "diskio.h" /* Declarations of disk functions */ 27 | // 28 | #include "hw_config.h" 29 | #include "my_debug.h" 30 | #include "sd_card.h" 31 | 32 | #define TRACE_PRINTF(fmt, args...) 33 | //#define TRACE_PRINTF printf // task_printf 34 | 35 | /*-----------------------------------------------------------------------*/ 36 | /* Get Drive Status */ 37 | /*-----------------------------------------------------------------------*/ 38 | 39 | DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */ 40 | ) { 41 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 42 | sd_card_t *p_sd = sd_get_by_num(pdrv); 43 | if (!p_sd) return RES_PARERR; 44 | sd_card_detect(p_sd); // Fast: just a GPIO read 45 | return p_sd->m_Status; // See http://elm-chan.org/fsw/ff/doc/dstat.html 46 | } 47 | 48 | /*-----------------------------------------------------------------------*/ 49 | /* Inidialize a Drive */ 50 | /*-----------------------------------------------------------------------*/ 51 | 52 | DSTATUS disk_initialize( 53 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 54 | ) { 55 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 56 | sd_card_t *p_sd = sd_get_by_num(pdrv); 57 | if (!p_sd) return RES_PARERR; 58 | return sd_init_card(p_sd); // See http://elm-chan.org/fsw/ff/doc/dstat.html 59 | } 60 | 61 | static int sdrc2dresult(int sd_rc) { 62 | switch (sd_rc) { 63 | case SD_BLOCK_DEVICE_ERROR_NONE: 64 | return RES_OK; 65 | case SD_BLOCK_DEVICE_ERROR_UNUSABLE: 66 | case SD_BLOCK_DEVICE_ERROR_NO_RESPONSE: 67 | case SD_BLOCK_DEVICE_ERROR_NO_INIT: 68 | case SD_BLOCK_DEVICE_ERROR_NO_DEVICE: 69 | return RES_NOTRDY; 70 | case SD_BLOCK_DEVICE_ERROR_PARAMETER: 71 | case SD_BLOCK_DEVICE_ERROR_UNSUPPORTED: 72 | return RES_PARERR; 73 | case SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED: 74 | return RES_WRPRT; 75 | case SD_BLOCK_DEVICE_ERROR_CRC: 76 | case SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK: 77 | case SD_BLOCK_DEVICE_ERROR_ERASE: 78 | case SD_BLOCK_DEVICE_ERROR_WRITE: 79 | default: 80 | return RES_ERROR; 81 | } 82 | } 83 | 84 | /*-----------------------------------------------------------------------*/ 85 | /* Read Sector(s) */ 86 | /*-----------------------------------------------------------------------*/ 87 | 88 | DRESULT disk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */ 89 | BYTE *buff, /* Data buffer to store read data */ 90 | LBA_t sector, /* Start sector in LBA */ 91 | UINT count /* Number of sectors to read */ 92 | ) { 93 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 94 | sd_card_t *p_sd = sd_get_by_num(pdrv); 95 | if (!p_sd) return RES_PARERR; 96 | int rc = sd_read_blocks(p_sd, buff, sector, count); 97 | return sdrc2dresult(rc); 98 | } 99 | 100 | /*-----------------------------------------------------------------------*/ 101 | /* Write Sector(s) */ 102 | /*-----------------------------------------------------------------------*/ 103 | 104 | #if FF_FS_READONLY == 0 105 | 106 | DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */ 107 | const BYTE *buff, /* Data to be written */ 108 | LBA_t sector, /* Start sector in LBA */ 109 | UINT count /* Number of sectors to write */ 110 | ) { 111 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 112 | sd_card_t *p_sd = sd_get_by_num(pdrv); 113 | if (!p_sd) return RES_PARERR; 114 | int rc = sd_write_blocks(p_sd, buff, sector, count); 115 | return sdrc2dresult(rc); 116 | } 117 | 118 | #endif 119 | 120 | /*-----------------------------------------------------------------------*/ 121 | /* Miscellaneous Functions */ 122 | /*-----------------------------------------------------------------------*/ 123 | 124 | DRESULT disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */ 125 | BYTE cmd, /* Control code */ 126 | void *buff /* Buffer to send/receive control data */ 127 | ) { 128 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 129 | sd_card_t *p_sd = sd_get_by_num(pdrv); 130 | if (!p_sd) return RES_PARERR; 131 | switch (cmd) { 132 | case GET_SECTOR_COUNT: { // Retrieves number of available sectors, the 133 | // largest allowable LBA + 1, on the drive 134 | // into the LBA_t variable pointed by buff. 135 | // This command is used by f_mkfs and f_fdisk 136 | // function to determine the size of 137 | // volume/partition to be created. It is 138 | // required when FF_USE_MKFS == 1. 139 | static LBA_t n; 140 | n = sd_sectors(p_sd); 141 | *(LBA_t *)buff = n; 142 | if (!n) return RES_ERROR; 143 | return RES_OK; 144 | } 145 | case GET_BLOCK_SIZE: { // Retrieves erase block size of the flash 146 | // memory media in unit of sector into the DWORD 147 | // variable pointed by buff. The allowable value 148 | // is 1 to 32768 in power of 2. Return 1 if the 149 | // erase block size is unknown or non flash 150 | // memory media. This command is used by only 151 | // f_mkfs function and it attempts to align data 152 | // area on the erase block boundary. It is 153 | // required when FF_USE_MKFS == 1. 154 | static DWORD bs = 1; 155 | *(DWORD *)buff = bs; 156 | return RES_OK; 157 | } 158 | case CTRL_SYNC: 159 | return RES_OK; 160 | default: 161 | return RES_PARERR; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /FatFs_SPI/src/my_debug.c: -------------------------------------------------------------------------------- 1 | /* my_debug.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include 15 | #include 16 | #include "my_debug.h" 17 | 18 | void my_printf(const char *pcFormat, ...) { 19 | char pcBuffer[256] = {0}; 20 | va_list xArgs; 21 | va_start(xArgs, pcFormat); 22 | vsnprintf(pcBuffer, sizeof(pcBuffer), pcFormat, xArgs); 23 | va_end(xArgs); 24 | printf("%s", pcBuffer); 25 | fflush(stdout); 26 | } 27 | 28 | 29 | void my_assert_func(const char *file, int line, const char *func, 30 | const char *pred) { 31 | printf("assertion \"%s\" failed: file \"%s\", line %d, function: %s\n", 32 | pred, file, line, func); 33 | fflush(stdout); 34 | __asm volatile("cpsid i" : : : "memory"); /* Disable global interrupts. */ 35 | while (1) { 36 | __asm("bkpt #0"); 37 | }; // Stop in GUI as if at a breakpoint (if debugging, otherwise loop 38 | // forever) 39 | } 40 | -------------------------------------------------------------------------------- /FatFs_SPI/src/rtc.c: -------------------------------------------------------------------------------- 1 | /* rtc.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include 15 | #include 16 | // 17 | #include "hardware/rtc.h" 18 | #include "pico/stdio.h" 19 | #include "pico/stdlib.h" 20 | #include "pico/util/datetime.h" 21 | // 22 | #include "ff.h" 23 | #include "util.h" // calculate_checksum 24 | // 25 | #include "rtc.h" 26 | 27 | static time_t epochtime; 28 | 29 | // Make an attempt to save a recent time stamp across reset: 30 | typedef struct rtc_save { 31 | uint32_t signature; 32 | datetime_t datetime; 33 | uint32_t checksum; // last, not included in checksum 34 | } rtc_save_t; 35 | static rtc_save_t rtc_save __attribute__((section(".uninitialized_data"))); 36 | 37 | static void update_epochtime() { 38 | bool rc = rtc_get_datetime(&rtc_save.datetime); 39 | if (rc) { 40 | rtc_save.signature = 0xBABEBABE; 41 | struct tm timeinfo = { 42 | .tm_sec = rtc_save.datetime 43 | .sec, /* Seconds. [0-60] (1 leap second) */ 44 | .tm_min = rtc_save.datetime.min, /* Minutes. [0-59] */ 45 | .tm_hour = rtc_save.datetime.hour, /* Hours. [0-23] */ 46 | .tm_mday = rtc_save.datetime.day, /* Day. [1-31] */ 47 | .tm_mon = rtc_save.datetime.month - 1, /* Month. [0-11] */ 48 | .tm_year = rtc_save.datetime.year - 1900, /* Year - 1900. */ 49 | .tm_wday = 0, /* Day of week. [0-6] */ 50 | .tm_yday = 0, /* Days in year.[0-365] */ 51 | .tm_isdst = -1 /* DST. [-1/0/1]*/ 52 | }; 53 | rtc_save.checksum = calculate_checksum((uint32_t *)&rtc_save, 54 | offsetof(rtc_save_t, checksum)); 55 | epochtime = mktime(&timeinfo); 56 | rtc_save.datetime.dotw = timeinfo.tm_wday; 57 | // configASSERT(-1 != epochtime); 58 | } 59 | } 60 | 61 | time_t time(time_t *pxTime) { 62 | update_epochtime(); 63 | if (pxTime) { 64 | *pxTime = epochtime; 65 | } 66 | return epochtime; 67 | } 68 | 69 | void time_init() { 70 | rtc_init(); 71 | datetime_t t = {0, 0, 0, 0, 0, 0, 0}; 72 | rtc_get_datetime(&t); 73 | if (!t.year && rtc_save.datetime.year) { 74 | uint32_t xor_checksum = calculate_checksum( 75 | (uint32_t *)&rtc_save, offsetof(rtc_save_t, checksum)); 76 | if (rtc_save.signature == 0xBABEBABE && 77 | rtc_save.checksum == xor_checksum) { 78 | // Set rtc 79 | rtc_set_datetime(&rtc_save.datetime); 80 | } 81 | } 82 | } 83 | 84 | // Called by FatFs: 85 | DWORD get_fattime(void) { 86 | datetime_t t = {0, 0, 0, 0, 0, 0, 0}; 87 | bool rc = rtc_get_datetime(&t); 88 | if (!rc) return 0; 89 | 90 | DWORD fattime = 0; 91 | // bit31:25 92 | // Year origin from the 1980 (0..127, e.g. 37 for 2017) 93 | uint8_t yr = t.year - 1980; 94 | fattime |= (0b01111111 & yr) << 25; 95 | // bit24:21 96 | // Month (1..12) 97 | uint8_t mo = t.month; 98 | fattime |= (0b00001111 & mo) << 21; 99 | // bit20:16 100 | // Day of the month (1..31) 101 | uint8_t da = t.day; 102 | fattime |= (0b00011111 & da) << 16; 103 | // bit15:11 104 | // Hour (0..23) 105 | uint8_t hr = t.hour; 106 | fattime |= (0b00011111 & hr) << 11; 107 | // bit10:5 108 | // Minute (0..59) 109 | uint8_t mi = t.min; 110 | fattime |= (0b00111111 & mi) << 5; 111 | // bit4:0 112 | // Second / 2 (0..29, e.g. 25 for 50) 113 | uint8_t sd = t.sec / 2; 114 | fattime |= (0b00011111 & sd); 115 | return fattime; 116 | } 117 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Picostation 2 | 3 | ## __In developement__ _Raspberry Pi Pico based ODE_ for the original Playstation 4 | original tweet 5 | 6 | ### Supported models: 7 | - PU-8 (SCPH100X) 8 | - PU-18 (SCPH55XX) 9 | 10 | ### Compatibility 11 | NOTE: rename your image to UNIROM.bin/.cue
12 | - Audio CD image 13 | - UNIROM image 14 | - Some games may load (see Game Compatibility List wiki page) 15 | 16 | ### How-to 17 | - see How-to wiki page 18 | 19 | ### Notes 20 | - For the moment, Game/app loading is not consistent, you'll need to try several times 21 | - If using an out of region multi-track bin/cue, you may need to use freepsxboot to bypass the region check 22 | 23 | ### To-do (see issues) 24 | - Resolve region unlock not always working: should be OK for single track games with commit babc072 (thanks to OldBoredEE) 25 | - Stabilize image loading 26 | - Make an interface for image choice/loading 27 | - Make it possible to update the pico via SD card 28 | 29 | ### Links 30 | - PCB: https://github.com/paulocode/picostation_pcb 31 | - FAQ: https://github.com/paulocode/picostation_faq 32 | - Slow Solder Board (SSB) solder points / checking connection: https://mmmonkey.co.uk/xstation-sony-playstation-install-notes-and-pinout/ 33 | - How to compile (Windows): https://shawnhymel.com/2096/how-to-set-up-raspberry-pi-pico-c-c-toolchain-on-windows-with-vs-code/ 34 | - PCB pinout: pinout 35 | - 3D Printable mount (550X) by @Sadsnifit : https://www.printables.com/fr/model/407224-picostation-mount-for-scph-5502 36 | -------------------------------------------------------------------------------- /cmd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "hardware/pio.h" 3 | #include "pico/stdlib.h" 4 | #include "main.pio.h" 5 | 6 | #include "utils.h" 7 | #include "values.h" 8 | #include "cmd.h" 9 | 10 | extern volatile uint latched; 11 | extern volatile uint count_track; 12 | extern volatile uint soct_offset; 13 | extern volatile uint subq_offset; 14 | extern volatile bool soct; 15 | extern volatile uint sled_move_direction; 16 | extern volatile uint track; 17 | extern volatile uint original_track; 18 | extern volatile uint sector; 19 | extern volatile uint sector_for_track_update; 20 | extern volatile int subq_start; 21 | extern volatile int mode; 22 | extern bool SENS_data[16]; 23 | 24 | volatile uint jump_track = 0; 25 | 26 | void autosequence() { 27 | int subcommand = (latched & 0x0F0000) >> 16; 28 | 29 | SENS_data[SENS_AUTOSEQ] = (subcommand != 0); 30 | 31 | switch (subcommand) { 32 | case 0xC: 33 | track = track + 2*jump_track; 34 | sector = track_to_sector(track); 35 | sector_for_track_update = sector; 36 | break; 37 | case 0xD: 38 | track = track - 2*jump_track; 39 | sector = track_to_sector(track); 40 | sector_for_track_update = sector; 41 | break; 42 | case 0x8: 43 | track = track + 1; 44 | sector = track_to_sector(track); 45 | sector_for_track_update = sector; 46 | break; 47 | case 0x9: 48 | track = track - 1; 49 | sector = track_to_sector(track); 50 | sector_for_track_update = sector; 51 | break; 52 | case 0xA: 53 | track = track + 10; 54 | sector = track_to_sector(track); 55 | sector_for_track_update = sector; 56 | break; 57 | case 0xB: 58 | track = track - 10; 59 | sector = track_to_sector(track); 60 | sector_for_track_update = sector; 61 | break; 62 | case 0xE: 63 | track = track + jump_track; 64 | sector = track_to_sector(track); 65 | sector_for_track_update = sector; 66 | break; 67 | case 0xF: 68 | track = track - jump_track; 69 | sector = track_to_sector(track); 70 | sector_for_track_update = sector; 71 | break; 72 | case 0x4: 73 | track = track + jump_track; 74 | sector = track_to_sector(track); 75 | sector_for_track_update = sector; 76 | break; 77 | case 0x5: 78 | track = track - jump_track; 79 | sector = track_to_sector(track); 80 | sector_for_track_update = sector; 81 | break; 82 | } 83 | 84 | } 85 | 86 | void sled_move() { 87 | int subcommand_move = (latched & 0x030000) >> 16; 88 | int subcommand_track = (latched & 0x0C0000) >> 16; 89 | switch (subcommand_move) { 90 | case 2: 91 | sled_move_direction = SLED_MOVE_FORWARD; 92 | original_track = track; 93 | break; 94 | case 3: 95 | sled_move_direction = SLED_MOVE_REVERSE; 96 | original_track = track; 97 | break; 98 | default: 99 | if (sled_move_direction != SLED_MOVE_STOP) { 100 | sector = track_to_sector(track); 101 | sector_for_track_update = sector; 102 | } 103 | sled_move_direction = SLED_MOVE_STOP; 104 | break; 105 | } 106 | 107 | switch (subcommand_track) { 108 | case 8: 109 | track++; 110 | sector = track_to_sector(track); 111 | sector_for_track_update = sector; 112 | break; 113 | case 0xC: 114 | track--; 115 | sector = track_to_sector(track); 116 | sector_for_track_update = sector; 117 | break; 118 | } 119 | } 120 | 121 | void spindle() { 122 | int subcommand = (latched & 0x0F0000) >> 16; 123 | 124 | SENS_data[SENS_GFS] = (subcommand == 6); 125 | } 126 | 127 | void interrupt_xlat(uint gpio, uint32_t events) { 128 | int command = (latched & 0xF00000) >> 20; 129 | 130 | switch (command) { 131 | case CMD_AUTOSEQ: 132 | autosequence(); 133 | break; 134 | case CMD_SOCT: 135 | soct = 1; 136 | pio_sm_set_enabled(pio1, SUBQ_SM, false); 137 | soct_program_init(pio1, SOCT_SM, soct_offset, SQSO, SQCK); 138 | pio_sm_set_enabled(pio1, SOCT_SM, true); 139 | pio_sm_put_blocking(pio1, SOCT_SM, 0xFFFFFFF); 140 | break; 141 | case CMD_JUMP_TRACK: 142 | jump_track = (latched & 0xFFFF0) >> 4; 143 | //printf("%d\n", jump_track); 144 | break; 145 | case CMD_COUNT_TRACK: 146 | count_track = (latched & 0xFFFF0) >> 4; 147 | //printf("%d\n", count_track); 148 | break; 149 | case CMD_SPINDLE: 150 | spindle(); 151 | break; 152 | case CMD_SLED: 153 | sled_move(); 154 | break; 155 | case CMD_SPEED: 156 | if ((latched & 0xF40000) == 0x940000 && mode == 1) { 157 | mode = 2; 158 | } else if ((latched & 0xF40000) == 0x900000 && mode == 2) { 159 | mode = 1; 160 | } 161 | break; 162 | } 163 | 164 | /* 165 | if (command == CMD_SLED || command == CMD_AUTOSEQ || 166 | command == CMD_JUMP_TRACK || command == CMD_SPEED || 167 | command == CMD_COUNT_TRACK || command == CMD_SPINDLE 168 | ) { 169 | printf("%06X\n", latched); 170 | } 171 | */ 172 | 173 | latched = 0; 174 | } -------------------------------------------------------------------------------- /cmd.h: -------------------------------------------------------------------------------- 1 | #ifndef CMD_H_INCLUDED 2 | #define CMD_H_INCLUDED 3 | 4 | void interrupt_xlat(uint gpio, uint32_t events); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | The rest of the constants will depend on the type of 24 | socket, which SPI it is driven by, and how it is wired. 25 | 26 | */ 27 | 28 | #include 29 | // 30 | #include "my_debug.h" 31 | // 32 | #include "hw_config.h" 33 | // 34 | #include "ff.h" /* Obtains integer types */ 35 | // 36 | #include "diskio.h" /* Declarations of disk functions */ 37 | 38 | void spi1_dma_isr(); 39 | 40 | // Hardware Configuration of SPI "objects" 41 | // Note: multiple SD cards can be driven by one SPI if they use different slave 42 | // selects. 43 | static spi_t spis[] = { // One for each SPI. 44 | { 45 | .hw_inst = spi1, // SPI component 46 | .miso_gpio = 8, // GPIO number (not pin number) 47 | .mosi_gpio = 11, 48 | .sck_gpio = 10, 49 | .baud_rate = 25 * 1000 * 1000, 50 | .dma_isr = spi1_dma_isr 51 | } 52 | }; 53 | 54 | // Hardware Configuration of the SD Card "objects" 55 | static sd_card_t sd_cards[] = { // One for each SD card 56 | { 57 | .pcName = "0:", // Name used to mount device 58 | .spi = &spis[0], // Pointer to the SPI driving this card 59 | .ss_gpio = 9, // The SPI slave select GPIO for this SD card 60 | .use_card_detect = false, 61 | .card_detect_gpio = -1, // Card detect 62 | .card_detected_true = -1, // What the GPIO read returns when a card is 63 | // present. Use -1 if there is no card detect. 64 | .m_Status = STA_NOINIT 65 | } 66 | }; 67 | 68 | void spi1_dma_isr() { spi_irq_handler(&spis[0]); } 69 | 70 | /* ********************************************************************** */ 71 | size_t sd_get_num() { return count_of(sd_cards); } 72 | sd_card_t *sd_get_by_num(size_t num) { 73 | if (num <= sd_get_num()) { 74 | return &sd_cards[num]; 75 | } else { 76 | return NULL; 77 | } 78 | } 79 | size_t spi_get_num() { return count_of(spis); } 80 | spi_t *spi_get_by_num(size_t num) { 81 | if (num <= sd_get_num()) { 82 | return &spis[num]; 83 | } else { 84 | return NULL; 85 | } 86 | } 87 | 88 | /* [] END OF FILE */ 89 | -------------------------------------------------------------------------------- /i2s.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hardware/dma.h" 5 | #include "hardware/pio.h" 6 | #include "pico/stdlib.h" 7 | #include "main.pio.h" 8 | 9 | #include "f_util.h" 10 | #include "ff.h" 11 | #include "rtc.h" 12 | #include "hw_config.h" 13 | #include "utils.h" 14 | #include "values.h" 15 | #include "subq.h" 16 | #include "cmd.h" 17 | #include "i2s.h" 18 | 19 | extern volatile int sector; 20 | extern volatile uint latched; 21 | extern volatile int num_logical_tracks; 22 | extern volatile int current_logical_track; 23 | extern volatile int sector_sending; 24 | extern volatile bool SENS_data[16]; 25 | extern volatile bool soct; 26 | extern volatile bool hasData; 27 | extern int *logical_track_to_sector; 28 | extern bool *is_data_track; 29 | extern mutex_t mechacon_mutex; 30 | 31 | void select_sens(uint8_t new_sens); 32 | void set_sens(uint8_t what, bool new_value); 33 | 34 | 35 | char SCExData[][44] = { 36 | {1,0,0,1,1,0,1,0,1,0,0,1,0,0,1,1,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0}, 37 | {1,0,0,1,1,0,1,0,1,0,0,1,0,0,1,1,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,1,1,1,1,0,1,0,0}, 38 | {1,0,0,1,1,0,1,0,1,0,0,1,0,0,1,1,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,0,0}, 39 | }; 40 | 41 | void i2s_data_thread() { 42 | // TODO: separate PSNEE, cue parse, and i2s functions 43 | int sector_t = -1; 44 | int bytesRead; 45 | uint32_t *pio_samples[2]; 46 | uint64_t psneeTimer = time_us_64(); 47 | uint64_t sector_change_timer = 0; 48 | int buffer_for_dma = 1; 49 | int buffer_for_sd_read = 0; 50 | int psnee_hysteresis = 0; 51 | int cachedSectors[SECTOR_CACHE] = { -1 }; 52 | int sector_loaded[2] = { -1 }; 53 | int roundRobinCacheIndex = 0; 54 | FRESULT fr; 55 | FIL fil; 56 | sd_card_t *pSD; 57 | int bytes; 58 | char buf[128]; 59 | uint16_t *cd_samples[SECTOR_CACHE]; 60 | uint16_t CD_scrambling_key[1176] = { 0 }; 61 | int key = 1; 62 | int logical_track = 0; 63 | 64 | pio_samples[0] = malloc(CD_SAMPLES_BYTES*2); 65 | pio_samples[1] = malloc(CD_SAMPLES_BYTES*2); 66 | memset(pio_samples[0], 0, CD_SAMPLES_BYTES*2); 67 | memset(pio_samples[1], 0, CD_SAMPLES_BYTES*2); 68 | 69 | for (int i=0; i>1)) << 15; 82 | key = (bit | key) >> 1; 83 | } 84 | 85 | char lower = key & 0xFF; 86 | 87 | CD_scrambling_key[i] = (lower << 8) | upper; 88 | 89 | for(int j=0; j<8; j++) { 90 | int bit = ((key & 1)^((key & 2)>>1)) << 15; 91 | key = (bit | key) >> 1; 92 | } 93 | } 94 | 95 | pSD = sd_get_by_num(0); 96 | fr = f_mount(&pSD->fatfs, pSD->pcName, 1); 97 | if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr); 98 | fr = f_open(&fil, "UNIROM.cue", FA_READ); 99 | if (FR_OK != fr && FR_EXIST != fr) 100 | panic("f_open(%s) error: (%d)\n", FRESULT_str(fr), fr); 101 | 102 | f_gets(buf, 128, &fil); 103 | 104 | while (1) { 105 | f_gets(buf, 128, &fil); 106 | char * token = strtok(buf, " "); 107 | if (strcmp("TRACK", token) == 0) { 108 | num_logical_tracks++; 109 | } 110 | if (f_eof(&fil)) { 111 | break; 112 | } 113 | } 114 | 115 | f_rewind(&fil); 116 | printf("num_logical_tracks: %d\n",num_logical_tracks); 117 | 118 | logical_track_to_sector = malloc(sizeof(int)*(num_logical_tracks+2)); 119 | is_data_track = malloc(sizeof(bool)*(num_logical_tracks+2)); 120 | logical_track_to_sector[0] = 0; 121 | logical_track_to_sector[1] = 4500; 122 | 123 | f_gets(buf, 128, &fil); 124 | while (1) { 125 | f_gets(buf, 128, &fil); 126 | 127 | if (f_eof(&fil)) { 128 | break; 129 | } 130 | char * token; 131 | token = strtok(buf, " "); 132 | if (strcmp("TRACK", token) == 0) { 133 | token = strtok(NULL, " "); 134 | logical_track = atoi(token); 135 | } 136 | token = strtok(NULL, " "); 137 | token[strcspn(token, "\r\n")] = 0; 138 | is_data_track[logical_track] = strcmp("AUDIO", token); 139 | if (is_data_track[logical_track]) { 140 | hasData = 1; 141 | } 142 | f_gets(buf, 128, &fil); 143 | token = strtok(buf, " "); 144 | token = strtok(NULL, " "); 145 | token = strtok(NULL, " "); 146 | 147 | int mm = atoi(strtok(token, ":")); 148 | int ss = atoi(strtok(NULL, ":")); 149 | int ff = atoi(strtok(NULL, ":")); 150 | if (logical_track != 1) { 151 | logical_track_to_sector[logical_track] = mm*60*75 + ss*75 + ff + 4650; 152 | } 153 | printf("cue: %d %d %d %d\n", logical_track, mm, ss, ff); 154 | } 155 | 156 | f_close(&fil); 157 | fr = f_open(&fil, "UNIROM.bin", FA_READ); 158 | if (FR_OK != fr && FR_EXIST != fr) 159 | panic("f_open(%s) error: (%d)\n", FRESULT_str(fr), fr); 160 | 161 | logical_track_to_sector[num_logical_tracks+1] = f_size(&fil)/2352 + 4650; 162 | is_data_track[num_logical_tracks+1] = 0; 163 | is_data_track[0] = 0; 164 | 165 | for(int i=0; itxf[I2S_DATA_SM], pio_samples[0], CD_SAMPLES*2, false); 177 | 178 | 179 | 180 | while (true) { 181 | sector_t = sector; 182 | if (mutex_try_enter(&mechacon_mutex,0)) { 183 | while(!pio_sm_is_rx_fifo_empty(pio1, MECHACON_SM)) { 184 | uint c = reverseBits(pio_sm_get_blocking(pio1, MECHACON_SM),8); 185 | latched >>= 8; 186 | latched |= c << 16; 187 | } 188 | select_sens(latched >> 20); 189 | gpio_put(SENS, SENS_data[latched >> 20]); 190 | mutex_exit(&mechacon_mutex); 191 | } 192 | 193 | if (sector_t > 0 && sector_t < PSNEE_SECTOR_LIMIT && 194 | SENS_data[SENS_GFS] && !soct && hasData && 195 | ((time_us_64() - psneeTimer) > 13333)) { 196 | psnee_hysteresis++; 197 | psneeTimer = time_us_64(); 198 | } 199 | 200 | if (psnee_hysteresis > 100) { 201 | psnee_hysteresis = 0; 202 | printf("+SCEX\n"); 203 | gpio_put(SCEX_DATA, 0); 204 | psneeTimer = time_us_64(); 205 | while ((time_us_64() - psneeTimer) < 90000) { 206 | if (sector >= PSNEE_SECTOR_LIMIT || soct) { 207 | goto abort_psnee; 208 | } 209 | } 210 | for (int i=0; i<6; i++) { 211 | for (int j=0; j<44; j++) { 212 | gpio_put(SCEX_DATA, SCExData[i%3][j]); 213 | psneeTimer = time_us_64(); 214 | while ((time_us_64() - psneeTimer) < 4000) { 215 | if (sector >= PSNEE_SECTOR_LIMIT || soct) { 216 | goto abort_psnee; 217 | } 218 | } 219 | } 220 | gpio_put(SCEX_DATA, 0); 221 | psneeTimer = time_us_64(); 222 | while ((time_us_64() - psneeTimer) < 90000) { 223 | if (sector >= PSNEE_SECTOR_LIMIT || soct) { 224 | goto abort_psnee; 225 | } 226 | } 227 | } 228 | 229 | abort_psnee: 230 | gpio_put(SCEX_DATA, 0); 231 | psneeTimer = time_us_64(); 232 | printf("-SCEX\n"); 233 | } 234 | 235 | if (buffer_for_dma != buffer_for_sd_read) { 236 | sector_change_timer = time_us_64(); 237 | while((time_us_64() - sector_change_timer) < 100) { 238 | if (sector_t != sector) { 239 | sector_t = sector; 240 | sector_change_timer = time_us_64(); 241 | } 242 | } 243 | int cacheHit = -1; 244 | int sector_to_search = sector_t < 4650 ? (sector_t % SECTOR_CACHE) + 4650 : sector_t; 245 | for (int i=0; i= 0) { 255 | fr = f_lseek(&fil, seek_bytes); 256 | if (FR_OK != fr) { 257 | f_rewind(&fil); 258 | } 259 | } 260 | 261 | fr = f_read(&fil, cd_samples[roundRobinCacheIndex], CD_SAMPLES_BYTES, &bytesRead); 262 | if (FR_OK != fr) 263 | panic("f_read(%s) error: (%d)\n", FRESULT_str(fr), fr); 264 | 265 | cachedSectors[roundRobinCacheIndex] = sector_to_search; 266 | cacheHit = roundRobinCacheIndex; 267 | roundRobinCacheIndex = (roundRobinCacheIndex + 1) % SECTOR_CACHE; 268 | } 269 | 270 | if (sector_t >= 4650) { 271 | for (int i=0; ich[channel].read_addr = (uint32_t)pio_samples[buffer_for_dma]; 298 | dma_channel_start(channel); 299 | } 300 | } 301 | } 302 | 303 | -------------------------------------------------------------------------------- /i2s.h: -------------------------------------------------------------------------------- 1 | #ifndef I2S_H_INCLUDED 2 | #define I2S_H_INCLUDED 3 | 4 | void i2s_data_thread(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "hardware/clocks.h" 6 | #include "hardware/dma.h" 7 | #include "hardware/irq.h" 8 | #include "hardware/pio.h" 9 | #include "pico/stdlib.h" 10 | #include "main.pio.h" 11 | #include "pico/multicore.h" 12 | #include "hardware/pll.h" 13 | #include "hardware/clocks.h" 14 | #include "hardware/structs/pll.h" 15 | #include "hardware/structs/clocks.h" 16 | 17 | #include "utils.h" 18 | #include "values.h" 19 | #include "subq.h" 20 | #include "cmd.h" 21 | #include "i2s.h" 22 | 23 | // globals 24 | mutex_t mechacon_mutex; 25 | volatile uint latched = 0; 26 | volatile uint mechachon_sm_offset; 27 | volatile uint soct_offset; 28 | volatile uint subq_offset; 29 | volatile bool soct = 0; 30 | volatile bool hasData = 0; 31 | volatile uint sled_move_direction = SLED_MOVE_STOP; 32 | volatile uint count_track = 0; 33 | volatile uint track = 0; 34 | volatile uint original_track = 0; 35 | volatile uint sector = 0; 36 | volatile uint sector_sending = -1; 37 | volatile uint sector_for_track_update = 0; 38 | volatile uint64_t subq_start_time = 0; 39 | volatile uint64_t sled_timer = 0; 40 | volatile uint8_t current_sens; 41 | volatile int num_logical_tracks = 0; 42 | int *logical_track_to_sector; 43 | bool *is_data_track; 44 | volatile int current_logical_track = 0; 45 | volatile int mode = 1; 46 | 47 | bool SENS_data[16] = { 48 | 0,0,0,0,0, 49 | 1, // FOK 50 | 0,0,0,0, 51 | 0, // GFS 52 | 0, // COMP 53 | 0, // COUT 54 | 0,0,0 55 | }; 56 | 57 | void select_sens(uint8_t new_sens) 58 | { 59 | current_sens = new_sens; 60 | } 61 | 62 | void set_sens(uint8_t what, bool new_value) 63 | { 64 | SENS_data[what] = new_value; 65 | if (what == current_sens) { 66 | gpio_put(SENS, new_value); 67 | } 68 | } 69 | 70 | void initialize() { 71 | set_sys_clock_pll(948000000, 7, 1); // 135428571 Hz, 67714286 Hz PS1 clock 72 | srand(time(NULL)); 73 | mutex_init(&mechacon_mutex); 74 | 75 | gpio_init(SCEX_DATA); 76 | gpio_init(SENS); 77 | gpio_init(LMTSW); 78 | gpio_init(XLAT); 79 | gpio_init(DOOR); 80 | gpio_init(RESET); 81 | gpio_init(SQCK); 82 | gpio_init(SQSO); 83 | gpio_init(CMD_CK); 84 | gpio_init(LRCK); 85 | 86 | gpio_set_dir(SCEX_DATA, GPIO_OUT); 87 | gpio_put(SCEX_DATA, 0); 88 | gpio_set_dir(SENS, GPIO_OUT); 89 | gpio_set_dir(LMTSW, GPIO_OUT); 90 | gpio_set_dir(XLAT, GPIO_IN); 91 | gpio_set_dir(DOOR, GPIO_IN); 92 | gpio_set_dir(RESET, GPIO_IN); 93 | gpio_set_dir(SQCK, GPIO_IN); 94 | gpio_set_dir(SQSO, GPIO_OUT); 95 | gpio_set_dir(CMD_CK, GPIO_IN); 96 | gpio_put(SQSO, 0); 97 | 98 | gpio_set_input_hysteresis_enabled(RESET,true); 99 | gpio_set_input_hysteresis_enabled(SQCK,true); 100 | gpio_set_input_hysteresis_enabled(XLAT,true); 101 | gpio_set_input_hysteresis_enabled(CMD_CK,true); 102 | gpio_set_drive_strength(LRCK, GPIO_DRIVE_STRENGTH_12MA); 103 | uint i2s_pio_offset = pio_add_program(pio0, &i2s_data_program); 104 | i2s_data_program_init(pio0, I2S_DATA_SM, i2s_pio_offset, DA15); 105 | 106 | uint offset2 = pio_add_program(pio0, &i2s_lrck_program); 107 | i2s_lrck_program_init(pio0, LRCK_DATA_SM, offset2, DA15+2); 108 | 109 | mechachon_sm_offset = pio_add_program(pio1, &mechacon_program); 110 | mechacon_program_init(pio1, MECHACON_SM, mechachon_sm_offset, CMD_DATA); 111 | 112 | uint offset3 = pio_add_program(pio0, &cpu_clk_program); 113 | cpu_clk_program_init(pio0, CPU_CLK_SM, offset3, CLK); 114 | 115 | uint offset5 = pio_add_program(pio1, &scor_program); 116 | scor_program_init(pio1, SCOR_SM, offset5, SCOR); 117 | 118 | soct_offset = pio_add_program(pio1, &soct_program); 119 | 120 | subq_offset = pio_add_program(pio1, &subq_program); 121 | 122 | uint64_t startTime = time_us_64(); 123 | 124 | pio_enable_sm_mask_in_sync(pio0, (1u << CPU_CLK_SM) | (1u << I2S_DATA_SM) | (1u << LRCK_DATA_SM)); 125 | 126 | gpio_set_dir(RESET, GPIO_OUT); 127 | gpio_put(RESET,0); 128 | sleep_ms(300); 129 | gpio_set_dir(RESET, GPIO_IN); 130 | 131 | while((time_us_64() - startTime) < 30000) { 132 | if (gpio_get(RESET) == 0) { 133 | startTime = time_us_64(); 134 | } 135 | } 136 | 137 | while((time_us_64() - startTime) < 30000) { 138 | if (gpio_get(CMD_CK) == 0) { 139 | startTime = time_us_64(); 140 | } 141 | } 142 | 143 | printf("ON!\n"); 144 | multicore_launch_core1(i2s_data_thread); 145 | gpio_set_irq_enabled_with_callback(XLAT, GPIO_IRQ_EDGE_RISE, true, &interrupt_xlat); 146 | pio_enable_sm_mask_in_sync(pio1, (1u << SCOR_SM) | (1u << MECHACON_SM)); 147 | } 148 | 149 | int main() { 150 | stdio_init_all(); 151 | 152 | initialize(); 153 | int prevMode = 1; 154 | int sectors_per_track_i = sectors_per_track(0); 155 | bool subq_delay = 0; 156 | uint64_t subq_delay_time = 0; 157 | 158 | while (true) { 159 | gpio_put(LMTSW, sector > 3000); 160 | if (mutex_try_enter(&mechacon_mutex,0)) { 161 | while(!pio_sm_is_rx_fifo_empty(pio1, MECHACON_SM)) { 162 | uint c = reverseBits(pio_sm_get_blocking(pio1, MECHACON_SM),8); 163 | latched >>= 8; 164 | latched |= c << 16; 165 | } 166 | select_sens(latched >> 20); 167 | gpio_put(SENS, SENS_data[latched >> 20]); 168 | mutex_exit(&mechacon_mutex); 169 | } 170 | 171 | if (prevMode == 1 && mode == 2) { 172 | pio_sm_set_clkdiv(pio0, I2S_DATA_SM, 1); 173 | pio_sm_set_clkdiv(pio1, SCOR_SM, 1); 174 | prevMode = 2; 175 | printf("x2\n"); 176 | } else if (prevMode == 2 && mode == 1) { 177 | pio_sm_set_clkdiv(pio0, I2S_DATA_SM, 2); 178 | pio_sm_set_clkdiv(pio1, SCOR_SM, 2); 179 | prevMode = 1; 180 | printf("x1\n"); 181 | } 182 | 183 | if (track < 0 || sector < 0) { 184 | track = 0; 185 | sector = 0; 186 | sector_for_track_update = 0; 187 | } 188 | 189 | if (track > 24000 || sector > 440000) { 190 | track = 24000; 191 | sector = track_to_sector(track); 192 | sector_for_track_update = sector; 193 | } 194 | 195 | if (gpio_get(RESET) == 0) { 196 | printf("RESET!\n"); 197 | pio_sm_set_enabled(pio1, SUBQ_SM, false); 198 | pio_sm_set_enabled(pio1, SOCT_SM, false); 199 | mechacon_program_init(pio1, MECHACON_SM, mechachon_sm_offset, CMD_DATA); 200 | subq_delay = 0; 201 | soct = 0; 202 | 203 | gpio_init(SQSO); 204 | gpio_set_dir(SQSO, GPIO_OUT); 205 | gpio_put(SQSO, 0); 206 | 207 | uint64_t startTime = time_us_64(); 208 | 209 | while ((time_us_64() - startTime) < 30000) { 210 | if (gpio_get(RESET) == 0) { 211 | startTime = time_us_64(); 212 | } 213 | } 214 | 215 | while ((time_us_64() - startTime) < 30000) { 216 | if (gpio_get(CMD_CK) == 0) { 217 | startTime = time_us_64(); 218 | } 219 | } 220 | 221 | pio_sm_set_enabled(pio1, MECHACON_SM, true); 222 | } 223 | 224 | 225 | if (soct) { 226 | uint interrupts = save_and_disable_interrupts(); 227 | // waiting for RX FIFO entry does not work. 228 | sleep_us(300); 229 | soct = 0; 230 | pio_sm_set_enabled(pio1, SOCT_SM, false); 231 | subq_start_time = time_us_64(); 232 | restore_interrupts(interrupts); 233 | } else if (sled_move_direction == SLED_MOVE_FORWARD) { 234 | if ((time_us_64() - sled_timer) > TRACK_MOVE_TIME_US) { 235 | sled_timer = time_us_64(); 236 | track++; 237 | sector = track_to_sector(track); 238 | sector_for_track_update = sector; 239 | 240 | if ((track - original_track) >= count_track) { 241 | original_track = track; 242 | set_sens(SENS_COUT, !SENS_data[SENS_COUT]); 243 | } 244 | } 245 | } else if (sled_move_direction == SLED_MOVE_REVERSE) { 246 | if ((time_us_64() - sled_timer) > TRACK_MOVE_TIME_US) { 247 | sled_timer = time_us_64(); 248 | track--; 249 | sector = track_to_sector(track); 250 | sector_for_track_update = sector; 251 | if ((original_track - track) >= count_track) { 252 | original_track = track; 253 | set_sens(SENS_COUT, !SENS_data[SENS_COUT]); 254 | } 255 | } 256 | } else if (SENS_data[SENS_GFS]) { 257 | if (sector < 4650 && (time_us_64() - subq_start_time) > 13333) { 258 | subq_start_time = time_us_64(); 259 | start_subq(); 260 | sector++; 261 | if ((sector - sector_for_track_update) >= sectors_per_track_i) { 262 | sector_for_track_update = sector; 263 | track++; 264 | sectors_per_track_i = sectors_per_track(track); 265 | } 266 | } else { 267 | if (sector_sending == sector) { 268 | if (!subq_delay) { 269 | sector++; 270 | if ((sector - sector_for_track_update) >= sectors_per_track_i) { 271 | sector_for_track_update = sector; 272 | track++; 273 | sectors_per_track_i = sectors_per_track(track); 274 | } 275 | subq_delay = 1; 276 | subq_delay_time = time_us_64(); 277 | } 278 | } 279 | 280 | if (subq_delay && (sector >= 4650 && (time_us_64() - subq_delay_time) > 3333)) { 281 | subq_delay = 0; 282 | start_subq(); 283 | } 284 | } 285 | } else { 286 | subq_delay = 0; 287 | } 288 | } 289 | 290 | } 291 | -------------------------------------------------------------------------------- /main.pio: -------------------------------------------------------------------------------- 1 | .program cpu_clk 2 | 3 | .wrap_target 4 | set pins 0 5 | set pins 1 6 | .wrap 7 | % c-sdk { 8 | 9 | static inline void cpu_clk_program_init(PIO pio, uint8_t sm, uint8_t offset, uint8_t cpu_clk_pin) { 10 | pio_gpio_init(pio, cpu_clk_pin); 11 | pio_sm_set_consecutive_pindirs(pio, sm, cpu_clk_pin, 1, true); 12 | pio_sm_config sm_config = cpu_clk_program_get_default_config(offset); 13 | sm_config_set_set_pins(&sm_config, cpu_clk_pin, 1); 14 | pio_sm_init(pio, sm, offset, &sm_config); 15 | } 16 | 17 | %} 18 | 19 | .program scor 20 | 21 | .wrap_target 22 | set pins 0 23 | pull block 24 | set y,4 25 | loopy: 26 | set x,31 27 | loopx: 28 | set pins 1 [30] 29 | set pins 1 [24] 30 | jmp x-- loopx 31 | jmp y-- loopy 32 | .wrap 33 | % c-sdk { 34 | 35 | static inline void scor_program_init(PIO pio, uint8_t sm, uint8_t offset, 36 | uint8_t scor_pin) { 37 | pio_gpio_init(pio, scor_pin); 38 | pio_sm_set_consecutive_pindirs(pio, sm, scor_pin, 1, true); 39 | 40 | pio_sm_config sm_config = scor_program_get_default_config(offset); 41 | sm_config_set_set_pins(&sm_config, scor_pin, 1); 42 | sm_config_set_clkdiv(&sm_config, 2); 43 | pio_sm_init(pio, sm, offset, &sm_config); 44 | } 45 | 46 | %} 47 | 48 | .program mechacon 49 | 50 | .wrap_target 51 | set x, 7 52 | loop: 53 | wait 0 pin 1 54 | wait 1 pin 1 55 | in pins 1 56 | jmp x-- loop 57 | push noblock 58 | .wrap 59 | % c-sdk { 60 | 61 | static inline void mechacon_program_init(PIO pio, uint8_t sm, uint8_t offset, 62 | uint8_t mechacon_pin_base) { 63 | pio_gpio_init(pio, mechacon_pin_base); 64 | pio_gpio_init(pio, mechacon_pin_base+1); 65 | pio_sm_set_consecutive_pindirs(pio, sm, mechacon_pin_base, 2, false); 66 | 67 | pio_sm_config sm_config = mechacon_program_get_default_config(offset); 68 | sm_config_set_in_pins(&sm_config, mechacon_pin_base); 69 | sm_config_set_jmp_pin(&sm_config, mechacon_pin_base+1); 70 | sm_config_set_fifo_join(&sm_config, PIO_FIFO_JOIN_RX); 71 | sm_config_set_in_shift(&sm_config, false, false, 0); 72 | pio_sm_init(pio, sm, offset, &sm_config); 73 | } 74 | 75 | %} 76 | 77 | .program soct 78 | 79 | .wrap_target 80 | set x, 16 81 | pull block 82 | loop: 83 | wait 0 pin 0 84 | out pins, 1 85 | wait 1 pin 0 86 | jmp x-- loop 87 | push block 88 | .wrap 89 | 90 | % c-sdk { 91 | 92 | static inline void soct_program_init(PIO pio, uint8_t sm, uint8_t offset, 93 | uint8_t sqso_pin, uint8_t sqck_pin) { 94 | pio_gpio_init(pio, sqso_pin); 95 | pio_gpio_init(pio, sqck_pin); 96 | pio_sm_set_consecutive_pindirs(pio, sm, sqso_pin, 1, true); 97 | pio_sm_set_consecutive_pindirs(pio, sm, sqck_pin, 1, false); 98 | 99 | pio_sm_config sm_config = soct_program_get_default_config(offset); 100 | sm_config_set_out_pins(&sm_config, sqso_pin, 1); 101 | sm_config_set_in_pins(&sm_config, sqck_pin); 102 | pio_sm_init(pio, sm, offset, &sm_config); 103 | } 104 | 105 | %} 106 | 107 | .program subq 108 | 109 | set pins 1 110 | set y, 2 111 | loop: 112 | set x, 31 113 | pull block 114 | loop_dword: 115 | wait 0 pin 0 116 | out pins, 1 117 | wait 1 pin 0 118 | jmp x-- loop_dword 119 | jmp y-- loop 120 | 121 | push block 122 | 123 | hang: 124 | jmp hang 125 | 126 | % c-sdk { 127 | 128 | static inline void subq_program_init(PIO pio, uint8_t sm, uint8_t offset, 129 | uint8_t sqso_pin, uint8_t sqck_pin) { 130 | pio_gpio_init(pio, sqso_pin); 131 | pio_gpio_init(pio, sqck_pin); 132 | pio_sm_set_consecutive_pindirs(pio, sm, sqso_pin, 1, true); 133 | pio_sm_set_consecutive_pindirs(pio, sm, sqck_pin, 1, false); 134 | 135 | pio_sm_config sm_config = subq_program_get_default_config(offset); 136 | sm_config_set_out_pins(&sm_config, sqso_pin, 1); 137 | sm_config_set_set_pins(&sm_config, sqso_pin, 1); 138 | sm_config_set_in_pins(&sm_config, sqck_pin); 139 | pio_sm_init(pio, sm, offset, &sm_config); 140 | } 141 | 142 | %} 143 | 144 | .program i2s_data 145 | 146 | .wrap_target 147 | set x, 7 148 | pull noblock 149 | loop: 150 | out pins, 1 151 | set pins, 0 [14] 152 | set pins, 1 [14] 153 | out pins, 1 154 | set pins, 0 [14] 155 | set pins, 1 [14] 156 | out pins, 1 157 | set pins, 0 [14] 158 | set pins, 1 [15] 159 | jmp x-- loop 160 | nop [4] 161 | irq 0 162 | .wrap 163 | 164 | % c-sdk { 165 | static inline void i2s_data_program_init(PIO pio, uint8_t sm, uint8_t offset, uint8_t data_clock_pin) { 166 | pio_gpio_init(pio, data_clock_pin); 167 | pio_gpio_init(pio, data_clock_pin+1); 168 | pio_sm_set_consecutive_pindirs(pio, sm, data_clock_pin, 2, true); 169 | 170 | pio_sm_config sm_config = i2s_data_program_get_default_config(offset); 171 | sm_config_set_set_pins(&sm_config, data_clock_pin, 1); 172 | sm_config_set_out_pins(&sm_config, data_clock_pin+1, 1); 173 | sm_config_set_fifo_join(&sm_config, PIO_FIFO_JOIN_TX); 174 | sm_config_set_clkdiv(&sm_config, 2); 175 | 176 | pio_sm_init(pio, sm, offset, &sm_config); 177 | } 178 | 179 | %} 180 | 181 | .program i2s_lrck 182 | 183 | start: 184 | set pins 1 185 | wait 1 irq 0 186 | set pins 0 187 | wait 1 irq 0 188 | jmp start 189 | % c-sdk { 190 | 191 | static inline void i2s_lrck_program_init(PIO pio, uint8_t sm, uint8_t offset, uint8_t lrck_pin) { 192 | pio_gpio_init(pio, lrck_pin); 193 | pio_sm_set_consecutive_pindirs(pio, sm, lrck_pin, 1, true); 194 | 195 | pio_sm_config sm_config = i2s_data_program_get_default_config(offset); 196 | sm_config_set_set_pins(&sm_config, lrck_pin, 1); 197 | 198 | pio_sm_init(pio, sm, offset, &sm_config); 199 | } 200 | %} 201 | -------------------------------------------------------------------------------- /pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /subq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "hardware/gpio.h" 3 | #include "hardware/pio.h" 4 | #include "pico/stdlib.h" 5 | #include "main.pio.h" 6 | 7 | #include "utils.h" 8 | #include "values.h" 9 | #include "subq.h" 10 | 11 | extern volatile uint subq_offset; 12 | extern volatile uint subq_offset; 13 | extern volatile int track; 14 | extern volatile int current_logical_track; 15 | extern volatile int sector; 16 | extern volatile int sector_for_track_update; 17 | extern volatile int subq_start; 18 | extern volatile int mode; 19 | extern volatile bool hasData; 20 | extern volatile int num_logical_tracks; 21 | extern bool *is_data_track; 22 | extern int *logical_track_to_sector; 23 | 24 | uint8_t tracksubq[12]; 25 | 26 | void printf_subq(uint8_t *subqdata) { 27 | for (int i=0; i<12; i++) { 28 | printf("%02X ", subqdata[i]); 29 | } 30 | } 31 | 32 | static inline void send_subq(uint8_t *subqdata) { 33 | subq_program_init(pio1, SUBQ_SM, subq_offset, SQSO, SQCK); 34 | pio_sm_set_enabled(pio1, SUBQ_SM, true); 35 | 36 | uint sub1 = (reverseBits(subqdata[0],8) << 24) | 37 | (reverseBits(subqdata[1],8) << 16) | 38 | (reverseBits(subqdata[2],8) << 8) | 39 | (reverseBits(subqdata[3],8)); 40 | uint sub2 = (reverseBits(subqdata[4],8) << 24) | 41 | (reverseBits(subqdata[5],8) << 16) | 42 | (reverseBits(subqdata[6],8) << 8) | 43 | (reverseBits(subqdata[7],8)); 44 | uint sub3 = (reverseBits(subqdata[8],8) << 24) | 45 | (reverseBits(subqdata[9],8) << 16) | 46 | (reverseBits(subqdata[10],8) << 8) | 47 | (reverseBits(subqdata[11],8)); 48 | pio_sm_put_blocking(pio1, SUBQ_SM, reverseBits(sub1,32)); 49 | pio_sm_put_blocking(pio1, SUBQ_SM, reverseBits(sub2,32)); 50 | pio_sm_put_blocking(pio1, SUBQ_SM, reverseBits(sub3,32)); 51 | 52 | pio_sm_put_blocking(pio1, SCOR_SM, 1); 53 | 54 | } 55 | 56 | void start_subq() { 57 | if (sector < 4500) { 58 | int subq_entry = sector%(3+num_logical_tracks); 59 | 60 | if (subq_entry == 0) { 61 | tracksubq[0] = hasData ? 0x61 : 0x21; 62 | tracksubq[1] = 0x00; 63 | tracksubq[2] = 0xA0; 64 | tracksubq[7] = 0x01; 65 | tracksubq[8] = 0x20; 66 | tracksubq[9] = 0x00; 67 | } else if (subq_entry == 1) { 68 | tracksubq[0] = hasData ? 0x61 : 0x21; 69 | tracksubq[1] = 0x00; 70 | tracksubq[2] = 0xA1; 71 | tracksubq[7] = tobcd(num_logical_tracks); 72 | tracksubq[8] = 0x00; 73 | tracksubq[9] = 0x00; 74 | } else if (subq_entry == 2) { 75 | int sector_lead_out = logical_track_to_sector[num_logical_tracks+1] - 4500; 76 | tracksubq[0] = hasData ? 0x61 : 0x21; 77 | tracksubq[1] = 0x00; 78 | tracksubq[2] = 0xA2; 79 | tracksubq[7] = tobcd(sector_lead_out/75/60); 80 | tracksubq[8] = tobcd((sector_lead_out/75) % 60); 81 | tracksubq[9] = tobcd(sector_lead_out % 75); 82 | } else if (subq_entry > 2) { 83 | int sector_track; 84 | int logical_track = subq_entry-2; 85 | if (logical_track == 1) { 86 | sector_track = 150; 87 | } else { 88 | sector_track = logical_track_to_sector[logical_track] - 4500; 89 | } 90 | tracksubq[0] = is_data_track[logical_track] ? 0x41 : 0x01; 91 | tracksubq[1] = 0x00; 92 | tracksubq[2] = tobcd(logical_track); 93 | tracksubq[7] = tobcd(sector_track/75/60); 94 | tracksubq[8] = tobcd((sector_track/75) % 60); 95 | tracksubq[9] = tobcd(sector_track % 75); 96 | } 97 | 98 | tracksubq[3] = tobcd(sector/75/60); 99 | tracksubq[4] = tobcd((sector/75) % 60); 100 | tracksubq[5] = tobcd(sector % 75); 101 | tracksubq[6] = 0x00; 102 | tracksubq[10] = 0x00; 103 | tracksubq[11] = 0x00; 104 | 105 | send_subq(tracksubq); 106 | } else { 107 | int logical_track = num_logical_tracks+1; // in case seek overshoots past end of disc 108 | for (int i=0; i sector) { 110 | logical_track = i; 111 | break; 112 | } 113 | } 114 | int sector_track = sector - logical_track_to_sector[logical_track]; 115 | int sector_abs = (sector - 4500); 116 | int sector_track_after_pause; 117 | 118 | if (logical_track == 1) { 119 | sector_track_after_pause = sector_track - 150; 120 | } else { 121 | sector_track_after_pause = sector_track; 122 | } 123 | 124 | current_logical_track = logical_track; 125 | 126 | tracksubq[0] = is_data_track[logical_track] ? 0x41 : 0x01; 127 | 128 | if (logical_track == num_logical_tracks+1) { 129 | tracksubq[1] = 0xAA; 130 | } else { 131 | tracksubq[1] = tobcd(logical_track); 132 | } 133 | if (sector_track < 150 && logical_track == 1) { // 2 sec pause track 134 | tracksubq[2] = 0x00; 135 | tracksubq[3] = 0x00; // min 136 | tracksubq[4] = tobcd(1-(sector_track/75)); // sec (count down) 137 | tracksubq[5] = tobcd(74 - (sector_track % 75)); // frame (count down) 138 | } else { 139 | tracksubq[2] = 0x01; 140 | tracksubq[3] = tobcd(sector_track_after_pause/75/60); // min 141 | tracksubq[4] = tobcd((sector_track_after_pause/75) % 60); // sec 142 | tracksubq[5] = tobcd(sector_track_after_pause % 75); // frame 143 | } 144 | tracksubq[6] = 0x00; 145 | tracksubq[7] = tobcd(sector_abs/75/60); // amin 146 | tracksubq[8] = tobcd((sector_abs/75) % 60); // asec 147 | tracksubq[9] = tobcd(sector_abs % 75); // aframe 148 | tracksubq[10] = 0x00; 149 | tracksubq[11] = ((sector % 2) == 0) ? 0x00 : 0x80; 150 | if (sector % (50+num_logical_tracks) == 0) { 151 | printf_subq(tracksubq); 152 | printf("%d\n",sector); 153 | } 154 | 155 | send_subq(tracksubq); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /subq.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBQ_H_INCLUDED 2 | #define SUBQ_H_INCLUDED 3 | 4 | void start_subq(); 5 | 6 | #endif -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "pico/stdlib.h" 3 | 4 | #include "utils.h" 5 | 6 | 7 | extern inline int tobcd(int in); 8 | extern inline uint32_t reverseBits(uint32_t num, int bits); 9 | extern inline int track_to_sector(int track); 10 | extern inline int sectors_per_track(int track); 11 | -------------------------------------------------------------------------------- /utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H_INCLUDED 2 | #define UTILS_H_INCLUDED 3 | #include 4 | #include "stdlib.h" 5 | 6 | inline int tobcd(int in) { 7 | if (in > 99) { 8 | return 0x99; 9 | } else { 10 | return (in / 10) << 4 | (in % 10); 11 | } 12 | } 13 | 14 | inline uint32_t reverseBits(uint32_t num, int bits) { 15 | uint32_t NO_OF_BITS = bits; 16 | uint32_t reverse_num = 0; 17 | int i; 18 | for (i = 0; i < NO_OF_BITS; i++) { 19 | if ((num & (1 << i))) 20 | reverse_num |= 1 << ((NO_OF_BITS - 1) - i); 21 | } 22 | return reverse_num; 23 | } 24 | 25 | inline int track_to_sector(int track) { 26 | return pow(track, 2) * 0.00031499 + track * 9.357516535; 27 | } 28 | 29 | inline int sectors_per_track(int track) { 30 | return round(track * 0.000616397 + 9); 31 | } 32 | #endif -------------------------------------------------------------------------------- /values.h: -------------------------------------------------------------------------------- 1 | #ifndef VALUES_H_INCLUDED 2 | #define VALUES_H_INCLUDED 3 | 4 | // GPIO pinouts 5 | #define XLAT 0 6 | #define CLK 21 7 | #define SCEX_DATA 4 8 | #define DOOR 6 9 | #define SENS 14 10 | #define DA15 15 // next pin is DA16 11 | #define LRCK 17 12 | #define SCOR 18 13 | #define SQSO 19 14 | #define SQCK 1 15 | #define LMTSW 2 16 | #define CMD_DATA 26 17 | #define CMD_CK 27 18 | #define RESET 7 19 | 20 | // C2PO, WFCK is always GND 21 | 22 | // PIO0 23 | #define I2S_DATA_SM 0 24 | #define LRCK_DATA_SM 1 25 | #define CPU_CLK_SM 3 26 | 27 | // PIO1 28 | #define SCOR_SM 0 29 | #define MECHACON_SM 1 30 | #define SOCT_SM 2 31 | #define SUBQ_SM 3 32 | 33 | 34 | // Commands 35 | #define CMD_SLED 0x2 36 | #define CMD_AUTOSEQ 0x4 37 | #define CMD_JUMP_TRACK 0x7 38 | #define CMD_SOCT 0x8 39 | #define CMD_SPEED 0x9 40 | #define CMD_COUNT_TRACK 0xB 41 | #define CMD_SPINDLE 0xE 42 | 43 | // SENS 44 | #define SENS_AUTOSEQ 0x4 45 | #define SENS_FOCUS 0x5 46 | #define SENS_GFS 0xA 47 | #define SENS_COUT 0xC 48 | 49 | // SLED 50 | #define SLED_MOVE_STOP 0 51 | #define SLED_MOVE_REVERSE 0x11 52 | #define SLED_MOVE_FORWARD 0x22 53 | 54 | // 55 | #define CD_SAMPLES 588 56 | #define CD_SAMPLES_BYTES CD_SAMPLES*2*2 57 | 58 | // 59 | #define TRACK_MOVE_TIME_US 15 60 | 61 | // 62 | #define PSNEE_SECTOR_LIMIT 4500 63 | #define SECTOR_CACHE 50 64 | 65 | #endif 66 | --------------------------------------------------------------------------------