├── .gitignore ├── ext ├── FatFs_SPI │ ├── include │ │ ├── rtc.h │ │ ├── f_util.h │ │ ├── my_debug.h │ │ ├── util.h │ │ └── ff_stdio.h │ ├── ff15 │ │ ├── source │ │ │ ├── 00readme.txt │ │ │ ├── diskio.h │ │ │ ├── diskio.c │ │ │ ├── ffsystem.c │ │ │ ├── ffconf.h │ │ │ └── 00history.txt │ │ ├── documents │ │ │ ├── res │ │ │ │ ├── app1.c │ │ │ │ ├── app5.c │ │ │ │ ├── app6.c │ │ │ │ ├── app2.c │ │ │ │ ├── app3.c │ │ │ │ └── app4.c │ │ │ └── css_e.css │ │ └── LICENSE.txt │ ├── sd_driver │ │ ├── hw_config.h │ │ ├── crc.h │ │ ├── sd_spi.h │ │ ├── spi.h │ │ ├── sd_card.h │ │ ├── sd_spi.c │ │ ├── demo_logging.c │ │ ├── crc.c │ │ └── spi.c │ ├── CMakeLists.txt │ ├── src │ │ ├── my_debug.c │ │ ├── f_util.c │ │ ├── rtc.c │ │ ├── glue.c │ │ └── ff_stdio.c │ └── LICENSE ├── minigb_apu │ ├── LICENSE │ ├── minigb_apu.h │ └── minigb_apu.c └── i2s │ ├── i2s.h │ ├── audio_i2s.pio │ └── i2s.c ├── inc ├── sdcard.h └── mk_ili9225.h ├── CMakeLists.txt ├── pico_sdk_import.cmake └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | uf2 3 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/source/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.15 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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(FatFs_SPI INTERFACE) 2 | target_sources(FatFs_SPI INTERFACE 3 | ${CMAKE_CURRENT_LIST_DIR}/ff15/source/ffsystem.c 4 | ${CMAKE_CURRENT_LIST_DIR}/ff15/source/ffunicode.c 5 | ${CMAKE_CURRENT_LIST_DIR}/ff15/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 | ff15/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 | -------------------------------------------------------------------------------- /ext/minigb_apu/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Alex Baines 2 | Copyright (c) 2019 Mahyar Koshkouei 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | -------------------------------------------------------------------------------- /ext/minigb_apu/minigb_apu.h: -------------------------------------------------------------------------------- 1 | /** 2 | * minigb_apu is released under the terms listed within the LICENSE file. 3 | * 4 | * minigb_apu emulates the audio processing unit (APU) of the Game Boy. This 5 | * project is based on MiniGBS by Alex Baines: https://github.com/baines/MiniGBS 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #define AUDIO_SAMPLE_RATE 44100 13 | 14 | #define DMG_CLOCK_FREQ 4194304.0 15 | #define SCREEN_REFRESH_CYCLES 70224.0 16 | #define VERTICAL_SYNC (DMG_CLOCK_FREQ/SCREEN_REFRESH_CYCLES) 17 | 18 | #define AUDIO_SAMPLES ((unsigned)(AUDIO_SAMPLE_RATE / VERTICAL_SYNC)) 19 | #define AUDIO_BUFFER_SIZE_BYTES (AUDIO_SAMPLES*4) 20 | 21 | /** 22 | * Fill allocated buffer "data" with "len" number of 32-bit floating point 23 | * samples (native endian order) in stereo interleaved format. 24 | */ 25 | void audio_callback(void *ptr, int16_t *data, size_t len); 26 | 27 | /** 28 | * Read audio register at given address "addr". 29 | */ 30 | uint8_t audio_read(const uint16_t addr); 31 | 32 | /** 33 | * Write "val" to audio register at given address "addr". 34 | */ 35 | void audio_write(const uint16_t addr, const uint8_t val); 36 | 37 | /** 38 | * Initialise audio driver. 39 | */ 40 | void audio_init(void); 41 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app1.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Open or create a file in append mode 3 | / (This function was sperseded by FA_OPEN_APPEND flag at FatFs R0.12a) 4 | /------------------------------------------------------------*/ 5 | 6 | FRESULT open_append ( 7 | FIL* fp, /* [OUT] File object to create */ 8 | const char* path /* [IN] File name to be opened */ 9 | ) 10 | { 11 | FRESULT fr; 12 | 13 | /* Opens an existing file. If not exist, creates a new file. */ 14 | fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS); 15 | if (fr == FR_OK) { 16 | /* Seek to end of the file to append data */ 17 | fr = f_lseek(fp, f_size(fp)); 18 | if (fr != FR_OK) 19 | f_close(fp); 20 | } 21 | return fr; 22 | } 23 | 24 | 25 | int main (void) 26 | { 27 | FRESULT fr; 28 | FATFS fs; 29 | FIL fil; 30 | 31 | /* Open or create a log file and ready to append */ 32 | f_mount(&fs, "", 0); 33 | fr = open_append(&fil, "logfile.txt"); 34 | if (fr != FR_OK) return 1; 35 | 36 | /* Append a line */ 37 | f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min); 38 | 39 | /* Close the file */ 40 | f_close(&fil); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app5.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Test if the file is contiguous / 3 | /----------------------------------------------------------------------*/ 4 | 5 | FRESULT test_contiguous_file ( 6 | FIL* fp, /* [IN] Open file object to be checked */ 7 | int* cont /* [OUT] 1:Contiguous, 0:Fragmented or zero-length */ 8 | ) 9 | { 10 | DWORD clst, clsz, step; 11 | FSIZE_t fsz; 12 | FRESULT fr; 13 | 14 | 15 | *cont = 0; 16 | fr = f_rewind(fp); /* Validates and prepares the file */ 17 | if (fr != FR_OK) return fr; 18 | 19 | #if FF_MAX_SS == FF_MIN_SS 20 | clsz = (DWORD)fp->obj.fs->csize * FF_MAX_SS; /* Cluster size */ 21 | #else 22 | clsz = (DWORD)fp->obj.fs->csize * fp->obj.fs->ssize; 23 | #endif 24 | fsz = f_size(fp); 25 | if (fsz > 0) { 26 | clst = fp->obj.sclust - 1; /* A cluster leading the first cluster for first test */ 27 | while (fsz) { 28 | step = (fsz >= clsz) ? clsz : (DWORD)fsz; 29 | fr = f_lseek(fp, f_tell(fp) + step); /* Advances file pointer a cluster */ 30 | if (fr != FR_OK) return fr; 31 | if (clst + 1 != fp->clust) break; /* Is not the cluster next to previous one? */ 32 | clst = fp->clust; fsz -= step; /* Get current cluster for next test */ 33 | } 34 | if (fsz == 0) *cont = 1; /* All done without fail? */ 35 | } 36 | 37 | return FR_OK; 38 | } 39 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/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 | -------------------------------------------------------------------------------- /inc/sdcard.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Original source code from https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico 3 | * Copyright 2021 Carl John Kugler III 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the License); you may not use 6 | * this file except in compliance with the License. You may obtain a copy of the 7 | * License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * Unless required by applicable law or agreed to in writing, software distributed 11 | * under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 12 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | * 15 | */ 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include 22 | #include "my_debug.h" 23 | #include "f_util.h" 24 | #include "ff.h" 25 | #include "sd_card.h" 26 | #include "diskio.h" 27 | #include "rtc.h" 28 | 29 | void spi_dma_isr(); 30 | 31 | static spi_t spis[]={ 32 | { 33 | .hw_inst=spi1, 34 | .miso_gpio=12, 35 | .mosi_gpio=15, 36 | .sck_gpio=14, 37 | .baud_rate=12500*1000, 38 | .dma_isr=spi_dma_isr 39 | } 40 | }; 41 | 42 | static sd_card_t sd_cards[]={ 43 | { 44 | .pcName="0:", 45 | .spi=&spis[0], 46 | .ss_gpio=13, 47 | .use_card_detect=false, 48 | .m_Status=STA_NOINIT 49 | } 50 | }; 51 | 52 | void spi_dma_isr() { 53 | spi_irq_handler(&spis[0]); 54 | } 55 | 56 | size_t sd_get_num() { 57 | return count_of(sd_cards); 58 | } 59 | 60 | sd_card_t *sd_get_by_num(size_t num) { 61 | if (num<=sd_get_num()) { 62 | return &sd_cards[num]; 63 | } else { 64 | return NULL; 65 | } 66 | } 67 | 68 | size_t spi_get_num() { 69 | return count_of(spis); 70 | } 71 | 72 | spi_t *spi_get_by_num(size_t num) { 73 | if (num <= sd_get_num()) { 74 | return &spis[num]; 75 | } else { 76 | return NULL; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13...3.23) 2 | 3 | include(pico_sdk_import.cmake) 4 | 5 | project(RP2040_GB C CXX ASM) 6 | set(CMAKE_C_STANDARD 11) 7 | set(CMAKE_CXX_STANDARD 17) 8 | 9 | pico_sdk_init() 10 | 11 | add_subdirectory(ext/FatFs_SPI build) 12 | 13 | add_executable(RP2040_GB 14 | src/main.c 15 | src/mk_ili9225.c 16 | ext/minigb_apu/minigb_apu.c 17 | ext/i2s/i2s.c 18 | ) 19 | 20 | pico_generate_pio_header(RP2040_GB ${CMAKE_CURRENT_LIST_DIR}/ext/i2s/audio_i2s.pio) 21 | 22 | target_include_directories(RP2040_GB PRIVATE inc ext/minigb_apu ext/i2s) 23 | 24 | target_link_libraries(RP2040_GB 25 | FatFs_SPI 26 | pico_stdlib pico_stdio pico_bootrom pico_multicore pico_stdio pico_multicore 27 | hardware_clocks hardware_pio hardware_vreg hardware_pio 28 | hardware_sync hardware_pll hardware_spi hardware_irq hardware_dma 29 | pico_binary_info) 30 | target_compile_definitions(RP2040_GB PRIVATE 31 | PARAM_ASSERTIONS_DISABLE_ALL=1 32 | PICO_ENTER_USB_BOOT_ON_EXIT=1 33 | PICO_STDIO_ENABLE_CRLF_SUPPORT=0 34 | PICO_STDIO_DEFAULT_CRLF=0 35 | PICO_PRINTF_SUPPORT_FLOAT=0 36 | PICO_PRINTF_SUPPORT_EXPONENTIAL=0 37 | PICO_PRINTF_SUPPORT_LONG_LONG=1 38 | PICO_PRINTF_SUPPORT_PTRDIFF_T=0) 39 | 40 | function(pico_add_verbose_dis_output TARGET) 41 | add_custom_command(TARGET ${TARGET} POST_BUILD 42 | COMMAND ${CMAKE_OBJDUMP} -h $ >$>,$,$>.dis 43 | COMMAND ${CMAKE_OBJDUMP} -drwCSl $ >>$>,$,$>.dis 44 | ) 45 | endfunction() 46 | 47 | pico_set_binary_type(RP2040_GB copy_to_ram) 48 | #pico_set_binary_type(RP2040_GB no_flash) 49 | pico_enable_stdio_usb(RP2040_GB 1) 50 | pico_enable_stdio_uart(RP2040_GB 0) 51 | pico_add_verbose_dis_output(RP2040_GB) 52 | pico_add_bin_output(RP2040_GB) 53 | pico_add_uf2_output(RP2040_GB) 54 | -------------------------------------------------------------------------------- /ext/i2s/i2s.h: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | * 4 | * Copyright (c) 2022 Vincent Mistler 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to 8 | * deal in the Software without restriction, including without limitation the 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 | * sell copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | * IN THE SOFTWARE. 23 | */ 24 | 25 | #pragma once 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "audio_i2s.pio.h" 33 | 34 | typedef struct i2s_config 35 | { 36 | uint32_t sample_freq; 37 | uint16_t channel_count; 38 | uint8_t data_pin; 39 | uint8_t clock_pin_base; 40 | PIO pio; 41 | uint8_t sm; 42 | uint8_t dma_channel; 43 | uint16_t dma_trans_count; 44 | uint16_t *dma_buf; 45 | uint8_t volume; 46 | } i2s_config_t; 47 | 48 | 49 | i2s_config_t i2s_get_default_config(void); 50 | void i2s_init(i2s_config_t *i2s_config); 51 | void i2s_write(const i2s_config_t *i2s_config,const int16_t *samples,const size_t len); 52 | void i2s_dma_write(i2s_config_t *i2s_config,const int16_t *samples); 53 | void i2s_volume(i2s_config_t *i2s_config,uint8_t volume); 54 | void i2s_increase_volume(i2s_config_t *i2s_config); 55 | void i2s_decrease_volume(i2s_config_t *i2s_config); 56 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app6.c: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------*/ 2 | /* Raw Read/Write Throughput Checker */ 3 | /*---------------------------------------------------------------------*/ 4 | 5 | #include 6 | #include 7 | #include "diskio.h" 8 | #include "ff.h" 9 | 10 | 11 | int test_raw_speed ( 12 | BYTE pdrv, /* Physical drive number */ 13 | DWORD lba, /* Start LBA for read/write test */ 14 | DWORD len, /* Number of bytes to read/write (must be multiple of sz_buff) */ 15 | void* buff, /* Read/write buffer */ 16 | UINT sz_buff /* Size of read/write buffer (must be multiple of FF_MAX_SS) */ 17 | ) 18 | { 19 | WORD ss; 20 | DWORD ofs, tmr; 21 | 22 | 23 | #if FF_MIN_SS != FF_MAX_SS 24 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) { 25 | printf("\ndisk_ioctl() failed.\n"); 26 | return 0; 27 | } 28 | #else 29 | ss = FF_MAX_SS; 30 | #endif 31 | 32 | printf("Starting raw write test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 33 | tmr = systimer(); 34 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 35 | if (disk_write(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 36 | printf("\ndisk_write() failed.\n"); 37 | return 0; 38 | } 39 | } 40 | if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) { 41 | printf("\ndisk_ioctl() failed.\n"); 42 | return 0; 43 | } 44 | tmr = systimer() - tmr; 45 | printf("\n%lu bytes written and it took %lu timer ticks.\n", len, tmr); 46 | 47 | printf("Starting raw read test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 48 | tmr = systimer(); 49 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 50 | if (disk_read(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 51 | printf("\ndisk_read() failed.\n"); 52 | return 0; 53 | } 54 | } 55 | tmr = systimer() - tmr; 56 | printf("\n%lu bytes read and it took %lu timer ticks.\n", len, tmr); 57 | 58 | printf("Test completed.\n"); 59 | return 1; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /ext/i2s/audio_i2s.pio: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | ; 4 | ; SPDX-License-Identifier: BSD-3-Clause 5 | ; 6 | 7 | ; Transmit a mono or stereo I2S audio stream as stereo 8 | ; This is 16 bits per sample; can be altered by modifying the "set" params, 9 | ; or made programmable by replacing "set x" with "mov x, y" and using Y as a config register. 10 | ; 11 | ; Autopull must be enabled, with threshold set to 32. 12 | ; Since I2S is MSB-first, shift direction should be to left. 13 | ; Hence the format of the FIFO word is: 14 | ; 15 | ; | 31 : 16 | 15 : 0 | 16 | ; | sample ws=0 | sample ws=1 | 17 | ; 18 | ; Data is output at 1 bit per clock. Use clock divider to adjust frequency. 19 | ; Fractional divider will probably be needed to get correct bit clock period, 20 | ; but for common syslck freqs this should still give a constant word select period. 21 | ; 22 | ; One output pin is used for the data output. 23 | ; Two side-set pins are used. Bit 0 is clock, bit 1 is word select. 24 | 25 | ; Send 16 bit words to the PIO for mono, 32 bit words for stereo 26 | 27 | .program audio_i2s 28 | .side_set 2 29 | 30 | ; /--- LRCLK 31 | ; |/-- BCLK 32 | bitloop1: ; || 33 | out pins, 1 side 0b10 34 | jmp x-- bitloop1 side 0b11 35 | out pins, 1 side 0b00 36 | set x, 14 side 0b01 37 | 38 | bitloop0: 39 | out pins, 1 side 0b00 40 | jmp x-- bitloop0 side 0b01 41 | out pins, 1 side 0b10 42 | public entry_point: 43 | set x, 14 side 0b11 44 | 45 | % c-sdk { 46 | 47 | static inline void audio_i2s_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint clock_pin_base) { 48 | pio_sm_config sm_config = audio_i2s_program_get_default_config(offset); 49 | 50 | sm_config_set_out_pins(&sm_config, data_pin, 1); 51 | sm_config_set_sideset_pins(&sm_config, clock_pin_base); 52 | sm_config_set_out_shift(&sm_config, false, true, 32); 53 | 54 | pio_sm_init(pio, sm, offset, &sm_config); 55 | 56 | uint pin_mask = (1u << data_pin) | (3u << clock_pin_base); 57 | pio_sm_set_pindirs_with_mask(pio, sm, pin_mask, pin_mask); 58 | pio_sm_set_pins(pio, sm, 0); // clear pins 59 | 60 | pio_sm_exec(pio, sm, pio_encode_jmp(offset + audio_i2s_offset_entry_point)); 61 | } 62 | 63 | %} -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app2.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Delete a sub-directory even if it contains any file 3 | /-------------------------------------------------------------/ 4 | / The delete_node() function is for R0.12+. 5 | / It works regardless of FF_FS_RPATH. 6 | */ 7 | 8 | 9 | FRESULT delete_node ( 10 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 11 | UINT sz_buff, /* Size of path name buffer (items) */ 12 | FILINFO* fno /* Name read buffer */ 13 | ) 14 | { 15 | UINT i, j; 16 | FRESULT fr; 17 | DIR dir; 18 | 19 | 20 | fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */ 21 | if (fr != FR_OK) return fr; 22 | 23 | for (i = 0; path[i]; i++) ; /* Get current path length */ 24 | path[i++] = _T('/'); 25 | 26 | for (;;) { 27 | fr = f_readdir(&dir, fno); /* Get a directory item */ 28 | if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */ 29 | j = 0; 30 | do { /* Make a path name */ 31 | if (i + j >= sz_buff) { /* Buffer over flow? */ 32 | fr = 100; break; /* Fails with 100 when buffer overflow */ 33 | } 34 | path[i + j] = fno->fname[j]; 35 | } while (fno->fname[j++]); 36 | if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */ 37 | fr = delete_node(path, sz_buff, fno); 38 | } else { /* Item is a file */ 39 | fr = f_unlink(path); 40 | } 41 | if (fr != FR_OK) break; 42 | } 43 | 44 | path[--i] = 0; /* Restore the path name */ 45 | f_closedir(&dir); 46 | 47 | if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */ 48 | return fr; 49 | } 50 | 51 | 52 | 53 | 54 | int main (void) /* How to use */ 55 | { 56 | FRESULT fr; 57 | FATFS fs; 58 | TCHAR buff[256]; 59 | FILINFO fno; 60 | 61 | 62 | f_mount(&fs, _T("5:"), 0); 63 | 64 | /* Directory to be deleted */ 65 | _tcscpy(buff, _T("5:dir")); 66 | 67 | /* Delete the directory */ 68 | fr = delete_node(buff, sizeof buff / sizeof buff[0], &fno); 69 | 70 | /* Check the result */ 71 | if (fr) { 72 | _tprintf(_T("Failed to delete the directory. (%u)\n"), fr); 73 | return fr; 74 | } else { 75 | _tprintf(_T("The directory and the contents have successfully been deleted.\n"), buff); 76 | return 0; 77 | } 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/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 | -------------------------------------------------------------------------------- /ext/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(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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/css_e.css: -------------------------------------------------------------------------------- 1 | * {margin: 0; padding: 0; border-width: 0;} 2 | body {margin: 8px; background-color: #e0ffff; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;} 3 | a:link {color: blue;} 4 | a:visited {color: darkmagenta;} 5 | a:hover {background-color: #a0ffff;} 6 | a:active {color: darkmagenta; overflow: hidden; outline:none; position: relative; top: 1px; left: 1px;} 7 | abbr {border-width: 1px;} 8 | 9 | p {margin: 0 0 0.3em 1em;} 10 | i {margin: 0 0.3em 0 0;} 11 | b {margin: 0 0.1em;} 12 | em {font-style: normal; font-weight: bold; margin: 0 0.1em;} 13 | strong {} 14 | pre {border: 1px dashed gray; margin: 0.5em 1em; padding: 0.5em; line-height: 1.2em; font-size: 85%; font-family: "Consolas", "Courier New", monospace; background-color: white;} 15 | pre span.c {color: green;} 16 | pre span.k {color: blue;} 17 | pre span.e {color: red;} 18 | pre span.b {font-weight: bold;} 19 | pre span.arg {font-style: italic;} 20 | tt {margin: 0 0.2em; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 21 | tt.arg {font-style: italic;} 22 | ol {margin: 0.5em 2.5em;} 23 | ul {margin: 0.5em 2em;} 24 | ul ul {margin: 0 2em 0.5em 1em;} 25 | dl {margin: 0.5em 1em;} 26 | dd {margin: 0 2em;} 27 | dt {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} 28 | dl.par dt {margin: 0.5em 0 0 0 ; font-style: italic; } 29 | dl.ret dt {margin: 0.5em 0 0 0 ; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; font-weight: bold; } 30 | hr {border-width: 1px; margin: 1em;} 31 | div.abst {font-family: sans-serif;} 32 | div.para {clear: both; font-family: serif;} 33 | div.ret a {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 34 | .equ {text-indent: 0; margin: 1em 2em 1em;} 35 | .indent {margin-left: 2em;} 36 | .rset {float: right; margin: 0.3em 0 0.5em 0.5em;} 37 | .lset {float: left; margin: 0.3em 0.5em 0.5em 0.5em;} 38 | ul.flat li {list-style-type: none; margin: 0;} 39 | a.imglnk img {border: 1px solid;} 40 | .iequ {white-space: nowrap; font-weight: bold;} 41 | .clr {clear: both;} 42 | .it {font-style: italic;} 43 | .mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} 44 | .ral {text-align: right; } 45 | .lal {text-align: left; } 46 | .cal {text-align: center; } 47 | 48 | h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} 49 | h2 {font-size: 2em; font-family: sans-serif; background-color: #d8d8FF; padding: 0.5em 0.5em; margin: 0 0 0.5em;} 50 | h3 {font-size: 1.5em; font-family: sans-serif; margin: 1.5em 0 0.5em;} 51 | div.doc h3 {border-color: #b0d8d8; border-style: solid; border-width: 0px 0px 4px 12px; padding: 4px; margin-top: 3em;} 52 | h4 {font-size: 1.2em; font-family: sans-serif; margin: 2em 0 0.2em;} 53 | h5 {font-size: 1em; font-family: sans-serif; margin: 1em 0 0em;} 54 | p.hdd {float: right; text-align: right; margin-top: 0.5em;} 55 | hr.hds {clear: both; margin-bottom: 1em;} 56 | kbd {letter-spacing: 0;} 57 | small {font-size: 80%;} 58 | .indent {margin-left: 2em;} 59 | 60 | /* Tables */ 61 | table {margin: 0.5em 1em; border-collapse: collapse; border: 2px solid gray; } 62 | table caption {font-family: sans-serif; font-weight: bold;} 63 | table th {background-color: white; border-style: solid; border-width: 1px 1px 2px; border-color: gray; padding: 0 3px; vertical-align: top; white-space: nowrap;} 64 | table td {background-color: white; border: 1px solid gray; padding: 0 3px; vertical-align: top; line-height: 1.3em;} 65 | table.lst td:first-child {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 66 | table.lst2 td {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 67 | table.lst3 td {font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 68 | tr.lst3 td {border-width: 2px 1px 1px; } 69 | table.lst4 td {padding: 0.3em;} 70 | table.lst4 td:nth-child(2) {width: 45%;} 71 | table.lst4 td:nth-child(3) {width: 45%;} 72 | 73 | p.foot {clear: both; text-indent: 0; margin: 1em 0.5em 1em;} 74 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app3.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Allocate a contiguous area to the file 3 | /-----------------------------------------------------------------------/ 4 | / This function checks if the file is contiguous with desired size. 5 | / If not, a block of contiguous sectors is allocated to the file. 6 | / If the file has been opened without FA_WRITE flag, it only checks if 7 | / the file is contiguous and returns the resulut. 8 | /-----------------------------------------------------------------------/ 9 | / This function can work with FatFs R0.09 - R0.11a. 10 | / It is incompatible with R0.12+. Use f_expand function instead. 11 | /----------------------------------------------------------------------*/ 12 | 13 | /* Declarations of FatFs internal functions accessible from applications. 14 | / This is intended to be used for disk checking/fixing or dirty hacks :-) */ 15 | DWORD clust2sect (FATFS* fs, DWORD clst); 16 | DWORD get_fat (FATFS* fs, DWORD clst); 17 | FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val); 18 | 19 | 20 | DWORD allocate_contiguous_clusters ( /* Returns the first sector in LBA (0:error or not contiguous) */ 21 | FIL* fp, /* Pointer to the open file object */ 22 | DWORD len /* Number of bytes to allocate */ 23 | ) 24 | { 25 | DWORD csz, tcl, ncl, ccl, cl; 26 | 27 | 28 | if (f_lseek(fp, 0) || !len) /* Check if the given parameters are valid */ 29 | return 0; 30 | csz = 512UL * fp->fs->csize; /* Cluster size in unit of byte (assuming 512 bytes/sector) */ 31 | tcl = (len + csz - 1) / csz; /* Total number of clusters required */ 32 | len = tcl * csz; /* Round-up file size to the cluster boundary */ 33 | 34 | /* Check if the existing cluster chain is contiguous */ 35 | if (len == fp->fsize) { 36 | ncl = 0; ccl = fp->sclust; 37 | do { 38 | cl = get_fat(fp->fs, ccl); /* Get the cluster status */ 39 | if (cl + 1 < 3) return 0; /* Hard error? */ 40 | if (cl != ccl + 1 && cl < fp->fs->n_fatent) break; /* Not contiguous? */ 41 | ccl = cl; 42 | } while (++ncl < tcl); 43 | if (ncl == tcl) /* Is the file contiguous? */ 44 | return clust2sect(fp->fs, fp->sclust); /* File is contiguous. Return the start sector */ 45 | } 46 | 47 | /* File is not contiguous */ 48 | #if _FS_READONLY 49 | return 0; /* Exit if in read-only cfg. */ 50 | #else 51 | if (!(fp->flag & FA_WRITE)) return 0; /* Exit if the file object is for read-only */ 52 | 53 | if (f_truncate(fp)) return 0; /* Remove the non-contiguous chain */ 54 | 55 | /* Find a free contiguous area */ 56 | ccl = cl = 2; ncl = 0; 57 | do { 58 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 59 | if (get_fat(fp->fs, cl)) { /* Encounterd a cluster in use */ 60 | do { /* Skip the block of used clusters */ 61 | cl++; 62 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 63 | } while (get_fat(fp->fs, cl)); 64 | ccl = cl; ncl = 0; 65 | } 66 | cl++; ncl++; 67 | } while (ncl < tcl); 68 | 69 | /* Create a contiguous cluster chain */ 70 | fp->fs->last_clust = ccl - 1; 71 | if (f_lseek(fp, len)) return 0; 72 | 73 | return clust2sect(fp->fs, fp->sclust); /* Return file start sector */ 74 | #endif 75 | } 76 | 77 | 78 | int main (void) 79 | { 80 | FRESULT fr; 81 | DRESULT dr; 82 | FATFS fs; 83 | FIL fil; 84 | DWORD org; 85 | 86 | 87 | /* Open or create a file to write */ 88 | f_mount(&fs, "", 0); 89 | fr = f_open(&fil, "fastrec.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS); 90 | if (fr) return 1; 91 | 92 | /* Check if the file is 256MB in size and occupies a contiguous area. 93 | / If not, a contiguous area will be re-allocated to the file. */ 94 | org = allocate_contiguous_clusters(&fil, 0x10000000); 95 | if (!org) { 96 | printf("Function failed due to any error or insufficient contiguous area.\n"); 97 | f_close(&fil); 98 | return 1; 99 | } 100 | 101 | /* Now you can read/write the file without filesystem layer. */ 102 | ... 103 | dr = disk_write(fil.fs->drv, Buff, org, 1024); /* Write 512KiB from top of the file */ 104 | ... 105 | 106 | f_close(&fil); 107 | return 0; 108 | } 109 | 110 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RP2040-GB for Pico-GB 2 | 3 | This is a fork of the [RP2040-GB Game Boy (DMG) emulator from deltabeard](https://github.com/deltabeard/RP2040-GB). RP2040-GB is a Game Boy (DMG) emulator Peanut-GB on the Raspberry Pi RP2040 microcontroller, using an ILI9225 screen. Runs at more than 70 fps without audio emulation. With frame skip and interlacing, can run at up to 120 fps. 4 | 5 | This fork includes changes done by me for [Pico-GB](https://www.youmaketech.com/pico-gb-gameboy-emulator-handheld-for-raspberry-pi-pico/): 6 | * push buttons support 7 | * overclocking to 266MHz for more accurate framerate (~60 FPS) 8 | * I2S sound support (44.1kHz 16 bits stereo audio) 9 | * SD card support (store roms and save games) + game selection menu 10 | * automatic color palette selection for some games (emulation of Game Boy Color Bootstrap ROM) + manual color palette selection 11 | 12 | Pico-GB is a [3d-printed Game Boy emulator handheld gaming console for Raspberry Pi Pico](https://www.youmaketech.com/pico-gb-gameboy-emulator-handheld-for-raspberry-pi-pico/) that ressembles to the original Nintendo Game Boy released in 1989. 13 | 14 | # Videos 15 | * [Let's build a Game Boy Emulator on a Breadboard!](https://youtu.be/ThmwXpIsGWs) 16 | * [Build the ULTIMATE GameBoy Emulator for Raspberry Pi Pico](https://youtu.be/yauNQSS6nC4) 17 | 18 | # Hardware 19 | ## What you need 20 | * (1x) [Raspberry Pi Pico](https://amzn.to/3rAcmDy) 21 | * (1x) [2.2inch ILI9225 176×220 LCD Display Module](https://amzn.to/3aNAMD7) 22 | * (1x) [FAT 32 formatted Micro SD card + adapter](https://amzn.to/3ICKzcm) with roms you legally own. Roms must have the .gb extension and must be copied to the root folder. 23 | * (1x) [MAX98357A amplifier](https://www.youmaketech.com/max98357) 24 | * (1x) [2W 8ohms speaker](https://amzn.to/3ikDy6S) 25 | * (8x) [Micro Push Button Switch, Momentary Tactile Tact Touch, 6x6x6 mm, 4 pins](https://amzn.to/3dyXBsx) 26 | * (1x) [Solderable Breadboard](https://amzn.to/3lwvfDi) 27 | * [Dupont Wires Assorted Kit (Male to Female + Male to Male + Female to Female)](https://amzn.to/3HtbvdO) 28 | * [Preformed Breadboard Jumper Wires](https://amzn.to/3rxwVjM) 29 | 30 | DISCLAIMER: Some links are affiliate links. As an Amazon Associate I receive a small commission (at no extra cost to you) if you make a purchase after clicking one of the affiliate links. Thanks for your support! 31 | 32 | ## Setting up the hardware 33 | [Pico-GB assembly instructions, circuit diagrams, 3d printed files etc.](https://www.youmaketech.com/pico-gb-gameboy-emulator-handheld-for-raspberry-pi-pico/) 34 | 35 | # Pinout 36 | * UP = GP2 37 | * DOWN = GP3 38 | * LEFT = GP4 39 | * RIGHT = GP5 40 | * BUTTON A = GP6 41 | * BUTTON B = GP7 42 | * SELECT = GP8 43 | * START = GP9 44 | * SD MISO = GP12 45 | * SD CS = GP13 46 | * SD CSK = GP14 47 | * SD MOSI = GP15 48 | * LCD CS = GP17 49 | * LCD CLK = GP18 50 | * LCD SDI = GP19 51 | * LCD RS = GP20 52 | * LCD RST = GP21 53 | * LCD LED = GP22 54 | * MAX98357A DIN = GP26 55 | * MAX98357A BCLK = GP27 56 | * MAX98357A LRC = GP28 57 | 58 | # Flashing the firmware 59 | * Download RP2040_GB.uf2 from the [releases page](https://github.com/YouMakeTech/Pico-GB/releases) 60 | * Push and hold the BOOTSEL button on the Pico, then connect to your computer using a micro USB cable. Release BOOTSEL once the drive RPI-RP2 appears on your computer. 61 | * Drag and drop the UF2 file on to the RPI-RP2 drive. The Raspberry Pi Pico will reboot and will now run the emulator. 62 | 63 | # Preparing the SD card 64 | The SD card is used to store game roms and save game progress. For this project, you will need a FAT 32 formatted Micro SD card with roms you legally own. Roms must have the .gb extension. 65 | 66 | * Insert your SD card in a Windows computer and format it as FAT 32 67 | * Copy your .gb files to the SD card root folder (subfolders are not supported at this time) 68 | * Insert the SD card into the ILI9225 SD card slot using a Micro SD adapter 69 | 70 | # Building from source 71 | The [Raspberry Pi Pico SDK](https://github.com/raspberrypi/pico-sdk) is required to build this project. Make sure you are able to compile an [example project](https://github.com/raspberrypi/pico-examples#first--examples) before continuing. 72 | 73 | # Known issues and limitations 74 | * No copyrighted games are included with Pico-GB / RP2040-GB. For this project, you will need a FAT 32 formatted Micro SD card with roms you legally own. Roms must have the .gb extension. 75 | * The RP2040-GB emulator is able to run at full speed on the Pico, at the expense of emulation accuracy. Some games may not work as expected or may not work at all. RP2040-GB is still experimental and not all features are guaranteed to work. 76 | * RP2040-GB is only compatible with [original Game Boy DMG games](https://en.wikipedia.org/wiki/List_of_Game_Boy_games) (not compatible with Game Boy Color or Game Boy Advance games) 77 | * Repeatedly flashing your Pico will eventually wear out the flash memory (Pico is qualified for min. 100K flash/erase cycles) 78 | * The emulator overclocks the Pico in order to get the emulator working fast enough. Overclocking can reduce the Pico’s lifespan. 79 | * Use this software and instructions at your own risk! I will not be responsible in any way for any damage to your Pico and/or connected peripherals caused by using this software. I also do not take responsibility in any way when damage is caused to the Pico or display due to incorrect wiring or voltages. 80 | 81 | # License 82 | MIT 83 | -------------------------------------------------------------------------------- /inc/mk_ili9225.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2019-2022 by Mahyar Koshkouei 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 8 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 9 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 10 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 11 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 12 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 13 | * PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #pragma once 17 | 18 | //#define NDEBUG 19 | #ifndef MK_ILI9225_READ_AVAILABLE 20 | # define MK_ILI9225_READ_AVAILABLE 0 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define SCREEN_SIZE_X 176u 29 | #define SCREEN_SIZE_Y 220u 30 | 31 | typedef enum { 32 | ILI9225_COLOR_MODE_FULL = 0, 33 | ILI9225_COLOR_MODE_8COLOR = 1 34 | } ili9225_color_mode_e; 35 | 36 | /** 37 | * Controls the reset pin of the ILI9225. 38 | * \param state Set to 0 on low output, else high. 39 | */ 40 | extern void mk_ili9225_set_rst(bool state); 41 | 42 | /** 43 | * Controls state of RS pin. 44 | * \param state Set to 0 on low output, else high. 45 | */ 46 | extern void mk_ili9225_set_rs(bool state); 47 | 48 | /** 49 | * Controls state of CS pin. 50 | * \param state Set to 0 on low output, else high. 51 | */ 52 | extern void mk_ili9225_set_cs(bool state); 53 | 54 | /** 55 | * Controls state of LED pin. 56 | * \param state Set to 0 on low output, else high. 57 | */ 58 | extern void mk_ili9225_set_led(bool state); 59 | 60 | /** 61 | * Sends data to the ILI9225 using SPI. Return only after sending data. 62 | * \param halfword Data to send. 63 | */ 64 | void mk_ili9225_spi_write16(const uint16_t *halfwords, size_t len); 65 | 66 | #if MK_ILI9225_READ_AVAILABLE 67 | /** 68 | * Reads 16-bit data from the ILI9225 using SPI. 69 | * \return 16-bit data. 70 | */ 71 | extern uint16_t mk_ili9225_spi_read16(void); 72 | #endif 73 | 74 | /** 75 | * Delays execution in milliseconds. 76 | * \param ms Duration to sleep for. 77 | */ 78 | extern void mk_ili9225_delay_ms(unsigned ms); 79 | 80 | /** 81 | * Initialise ILI9225 LCD with default settings. 82 | * \return 0 on success, else error due to invalid LCD identification. 83 | */ 84 | unsigned mk_ili9225_init(void); 85 | 86 | #if MK_ILI9225_READ_AVAILABLE 87 | /** 88 | * Read the current line being driven by the LCD. Can help with tearing 89 | * mitigation. 90 | * \return Line driven by LCD. 91 | */ 92 | unsigned mk_ili9225_read_driving_line(void); 93 | #endif 94 | 95 | /** 96 | * Set the window that pixel will be written to. Address will loop within the 97 | * window. 98 | * 99 | * \param hor_start 100 | * \param hor_end 101 | * \param vert_start 102 | * \param vert_end 103 | */ 104 | void mk_ili9225_set_window(uint16_t hor_start, uint16_t hor_end, 105 | uint16_t vert_start, uint16_t vert_end); 106 | 107 | /** 108 | * Set address pointer in GRAM. Must be within window. 109 | * \param x 110 | * \param y 111 | */ 112 | void mk_ili9225_set_address(uint8_t x, uint8_t y); 113 | 114 | /** 115 | * Write pixels to ILI9225 GRAM. These pixels will be displayed on screen at 116 | * next vsync. 117 | * \param pixels Pixel data in RGB565 format to write to LCD. 118 | * \param nmemb Number of pixels. 119 | */ 120 | void mk_ili9225_write_pixels(const uint16_t *pixels, uint_fast16_t nmemb); 121 | 122 | /** 123 | * Inverts the display. 124 | * @param invert 125 | */ 126 | void mk_ili9225_invert_display(bool invert); 127 | 128 | void mk_ili9225_write_pixels_start(void); 129 | void mk_ili9225_write_pixels_end(void); 130 | 131 | void mk_ili9225_set_gate_scan(uint16_t hor_start, uint16_t hor_end); 132 | 133 | void mk_ili9225_display_control(bool invert, ili9225_color_mode_e colour_mode); 134 | 135 | void mk_ili9225_power_control(uint8_t drive_power, bool sleep); 136 | 137 | void mk_ili9225_set_drive_freq(uint16_t f); 138 | 139 | void mk_ili9225_set_x(uint8_t x); 140 | 141 | /** 142 | * Exit and stop using LCD. Currently does nothing. 143 | */ 144 | void mk_ili9225_exit(void); 145 | 146 | /** 147 | * Fill a rectangle at the given location, size and color. 148 | */ 149 | void mk_ili9225_fill_rect(uint8_t x,uint8_t y,uint8_t w,uint8_t h,uint16_t color); 150 | 151 | /** 152 | * Fill the entire screen with the specified RGB565 color 153 | */ 154 | void mk_ili9225_fill(uint16_t color); 155 | 156 | /** 157 | * Set a specified pixel to the given color 158 | */ 159 | void mk_ili9225_pixel(uint8_t x,uint8_t y,uint16_t color); 160 | 161 | /** 162 | * Copy the specified framebuffer at the given coordinates 163 | */ 164 | void mk_ili9225_blit(uint16_t *fbuf,uint8_t x,uint8_t y,uint8_t w,uint8_t h); 165 | 166 | /** 167 | * Return an 8x8 framebuffer for the given letter and color / background color 168 | */ 169 | void mk_ili9225_get_letter(uint16_t *fbuf,char letter,uint16_t color,uint16_t bgcolor); 170 | 171 | /** 172 | * Write text to the screen using the the coordinates as the upper-left corner of the text. 173 | * All characters have dimensions of 8x8 pixels. 174 | */ 175 | void mk_ili9225_text(char *s,uint8_t x,uint8_t y,uint16_t color,uint16_t bgcolor); 176 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/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 | -------------------------------------------------------------------------------- /ext/i2s/i2s.c: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | * 4 | * Copyright (c) 2022 Vincent Mistler 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to 8 | * deal in the Software without restriction, including without limitation the 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 | * sell copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | * IN THE SOFTWARE. 23 | */ 24 | 25 | #include "i2s.h" 26 | 27 | /** 28 | * return the default i2s context used to store information about the setup 29 | */ 30 | i2s_config_t i2s_get_default_config(void) { 31 | i2s_config_t i2s_config = { 32 | .sample_freq = 44100, 33 | .channel_count = 2, 34 | .data_pin = 26, 35 | .clock_pin_base = 27, 36 | .pio = pio0, 37 | .sm = 0, 38 | .dma_channel = 0, 39 | .dma_buf = NULL, 40 | .dma_trans_count = 0, 41 | .volume = 0, 42 | }; 43 | 44 | return i2s_config; 45 | } 46 | 47 | /** 48 | * Initialize the I2S driver. Must be called before calling i2s_write or i2s_dma_write 49 | * i2s_config: I2S context obtained by i2s_get_default_config() 50 | */ 51 | void i2s_init(i2s_config_t *i2s_config) { 52 | uint8_t func=GPIO_FUNC_PIO0; // TODO: GPIO_FUNC_PIO0 for pio0 or GPIO_FUNC_PIO1 for pio1 53 | gpio_set_function(i2s_config->data_pin, GPIO_FUNC_PIO0); 54 | gpio_set_function(i2s_config->clock_pin_base, GPIO_FUNC_PIO0); 55 | gpio_set_function(i2s_config->clock_pin_base+1, GPIO_FUNC_PIO0); 56 | 57 | i2s_config->sm = pio_claim_unused_sm(i2s_config->pio, true); 58 | 59 | uint offset = pio_add_program(i2s_config->pio, &audio_i2s_program); 60 | 61 | audio_i2s_program_init(i2s_config->pio, i2s_config->sm , offset, i2s_config->data_pin , i2s_config->clock_pin_base); 62 | 63 | /* Set PIO clock */ 64 | uint32_t system_clock_frequency = clock_get_hz(clk_sys); 65 | uint32_t divider = system_clock_frequency * 4 / i2s_config->sample_freq; // avoid arithmetic overflow 66 | pio_sm_set_clkdiv_int_frac(i2s_config->pio, i2s_config->sm , divider >> 8u, divider & 0xffu); 67 | 68 | pio_sm_set_enabled(i2s_config->pio, i2s_config->sm, false); 69 | 70 | /* Allocate memory for the DMA buffer */ 71 | i2s_config->dma_buf=malloc(i2s_config->dma_trans_count*sizeof(uint32_t)); 72 | 73 | /* Direct Memory Access setup */ 74 | i2s_config->dma_channel = dma_claim_unused_channel(true); 75 | 76 | dma_channel_config dma_config = dma_channel_get_default_config(i2s_config->dma_channel); 77 | channel_config_set_read_increment(&dma_config, true); 78 | channel_config_set_write_increment(&dma_config, false); 79 | channel_config_set_dreq(&dma_config, pio_get_dreq(i2s_config->pio, i2s_config->sm, true)); 80 | channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_32); 81 | dma_channel_configure(i2s_config->dma_channel, 82 | &dma_config, 83 | &(i2s_config->pio->txf[i2s_config->sm]), // Destination pointer 84 | i2s_config->dma_buf, // Source pointer 85 | i2s_config->dma_trans_count, // Number of 32 bits words to transfer 86 | false // Start immediately 87 | ); 88 | 89 | pio_sm_set_enabled(i2s_config->pio, i2s_config->sm , true); 90 | } 91 | 92 | /** 93 | * Write samples to I2S directly and wait for completion (blocking) 94 | * i2s_config: I2S context obtained by i2s_get_default_config() 95 | * sample: pointer to an array of len x 32 bits samples 96 | * Each 32 bits sample contains 2x16 bits samples, 97 | * one for the left channel and one for the right channel 98 | * len: length of sample in 32 bits words 99 | */ 100 | void i2s_write(const i2s_config_t *i2s_config,const int16_t *samples,const size_t len) { 101 | for(size_t i=0;ipio, i2s_config->sm, (uint32_t)samples[i]); 103 | } 104 | } 105 | 106 | /** 107 | * Write samples to DMA buffer and initiate DMA transfer (non blocking) 108 | * i2s_config: I2S context obtained by i2s_get_default_config() 109 | * sample: pointer to an array of dma_trans_count x 32 bits samples 110 | */ 111 | void i2s_dma_write(i2s_config_t *i2s_config,const int16_t *samples) { 112 | /* Wait the completion of the previous DMA transfer */ 113 | dma_channel_wait_for_finish_blocking(i2s_config->dma_channel); 114 | /* Copy samples into the DMA buffer */ 115 | if(i2s_config->volume==0) { 116 | memcpy(i2s_config->dma_buf,samples,i2s_config->dma_trans_count*sizeof(int32_t)); 117 | } else { 118 | for(uint16_t i=0;idma_trans_count*2;i++) { 119 | i2s_config->dma_buf[i] = samples[i]>>i2s_config->volume; 120 | } 121 | } 122 | 123 | /* Initiate the DMA transfer */ 124 | dma_channel_transfer_from_buffer_now(i2s_config->dma_channel, 125 | i2s_config->dma_buf, 126 | i2s_config->dma_trans_count); 127 | } 128 | 129 | /** 130 | * Adjust the output volume 131 | * i2s_config: I2S context obtained by i2s_get_default_config() 132 | * volume: desired volume between 0 (highest. volume) and 16 (lowest volume) 133 | */ 134 | void i2s_volume(i2s_config_t *i2s_config,uint8_t volume) { 135 | if(volume>16) volume=16; 136 | i2s_config->volume=volume; 137 | } 138 | 139 | /** 140 | * Increases the output volume 141 | */ 142 | void i2s_increase_volume(i2s_config_t *i2s_config) { 143 | if(i2s_config->volume>0) { 144 | i2s_config->volume--; 145 | } 146 | } 147 | 148 | /** 149 | * Decreases the output volume 150 | */ 151 | void i2s_decrease_volume(i2s_config_t *i2s_config) { 152 | if(i2s_config->volume<16) { 153 | i2s_config->volume++; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/source/ffsystem.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* A Sample Code of User Provided OS Dependent Functions for FatFs */ 3 | /*------------------------------------------------------------------------*/ 4 | 5 | #include "ff.h" 6 | 7 | 8 | #if FF_USE_LFN == 3 /* Use dynamic memory allocation */ 9 | 10 | /*------------------------------------------------------------------------*/ 11 | /* Allocate/Free a Memory Block */ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | #include /* with POSIX API */ 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((size_t)msize); /* Allocate a new memory block */ 22 | } 23 | 24 | 25 | void ff_memfree ( 26 | void* mblock /* Pointer to the memory block to free (no effect if null) */ 27 | ) 28 | { 29 | free(mblock); /* Free the memory block */ 30 | } 31 | 32 | #endif 33 | 34 | 35 | 36 | 37 | #if FF_FS_REENTRANT /* Mutal exclusion */ 38 | /*------------------------------------------------------------------------*/ 39 | /* Definitions of Mutex */ 40 | /*------------------------------------------------------------------------*/ 41 | 42 | #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */ 43 | 44 | 45 | #if OS_TYPE == 0 /* Win32 */ 46 | #include 47 | static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 48 | 49 | #elif OS_TYPE == 1 /* uITRON */ 50 | #include "itron.h" 51 | #include "kernel.h" 52 | static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 53 | 54 | #elif OS_TYPE == 2 /* uc/OS-II */ 55 | #include "includes.h" 56 | static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */ 57 | 58 | #elif OS_TYPE == 3 /* FreeRTOS */ 59 | #include "FreeRTOS.h" 60 | #include "semphr.h" 61 | static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 62 | 63 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 64 | #include "cmsis_os.h" 65 | static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 66 | 67 | #endif 68 | 69 | 70 | 71 | /*------------------------------------------------------------------------*/ 72 | /* Create a Mutex */ 73 | /*------------------------------------------------------------------------*/ 74 | /* This function is called in f_mount function to create a new mutex 75 | / or semaphore for the volume. When a 0 is returned, the f_mount function 76 | / fails with FR_INT_ERR. 77 | */ 78 | 79 | int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ 80 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 81 | ) 82 | { 83 | #if OS_TYPE == 0 /* Win32 */ 84 | Mutex[vol] = CreateMutex(NULL, FALSE, NULL); 85 | return (int)(Mutex[vol] != INVALID_HANDLE_VALUE); 86 | 87 | #elif OS_TYPE == 1 /* uITRON */ 88 | T_CMTX cmtx = {TA_TPRI,1}; 89 | 90 | Mutex[vol] = acre_mtx(&cmtx); 91 | return (int)(Mutex[vol] > 0); 92 | 93 | #elif OS_TYPE == 2 /* uC/OS-II */ 94 | OS_ERR err; 95 | 96 | Mutex[vol] = OSMutexCreate(0, &err); 97 | return (int)(err == OS_NO_ERR); 98 | 99 | #elif OS_TYPE == 3 /* FreeRTOS */ 100 | Mutex[vol] = xSemaphoreCreateMutex(); 101 | return (int)(Mutex[vol] != NULL); 102 | 103 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 104 | osMutexDef(cmsis_os_mutex); 105 | 106 | Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex)); 107 | return (int)(Mutex[vol] != NULL); 108 | 109 | #endif 110 | } 111 | 112 | 113 | /*------------------------------------------------------------------------*/ 114 | /* Delete a Mutex */ 115 | /*------------------------------------------------------------------------*/ 116 | /* This function is called in f_mount function to delete a mutex or 117 | / semaphore of the volume created with ff_mutex_create function. 118 | */ 119 | 120 | void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ 121 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 122 | ) 123 | { 124 | #if OS_TYPE == 0 /* Win32 */ 125 | CloseHandle(Mutex[vol]); 126 | 127 | #elif OS_TYPE == 1 /* uITRON */ 128 | del_mtx(Mutex[vol]); 129 | 130 | #elif OS_TYPE == 2 /* uC/OS-II */ 131 | OS_ERR err; 132 | 133 | OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err); 134 | 135 | #elif OS_TYPE == 3 /* FreeRTOS */ 136 | vSemaphoreDelete(Mutex[vol]); 137 | 138 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 139 | osMutexDelete(Mutex[vol]); 140 | 141 | #endif 142 | } 143 | 144 | 145 | /*------------------------------------------------------------------------*/ 146 | /* Request a Grant to Access the Volume */ 147 | /*------------------------------------------------------------------------*/ 148 | /* This function is called on enter file functions to lock the volume. 149 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 150 | */ 151 | 152 | int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ 153 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 154 | ) 155 | { 156 | #if OS_TYPE == 0 /* Win32 */ 157 | return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0); 158 | 159 | #elif OS_TYPE == 1 /* uITRON */ 160 | return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK); 161 | 162 | #elif OS_TYPE == 2 /* uC/OS-II */ 163 | OS_ERR err; 164 | 165 | OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err)); 166 | return (int)(err == OS_NO_ERR); 167 | 168 | #elif OS_TYPE == 3 /* FreeRTOS */ 169 | return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE); 170 | 171 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 172 | return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK); 173 | 174 | #endif 175 | } 176 | 177 | 178 | 179 | /*------------------------------------------------------------------------*/ 180 | /* Release a Grant to Access the Volume */ 181 | /*------------------------------------------------------------------------*/ 182 | /* This function is called on leave file functions to unlock the volume. 183 | */ 184 | 185 | void ff_mutex_give ( 186 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 187 | ) 188 | { 189 | #if OS_TYPE == 0 /* Win32 */ 190 | ReleaseMutex(Mutex[vol]); 191 | 192 | #elif OS_TYPE == 1 /* uITRON */ 193 | unl_mtx(Mutex[vol]); 194 | 195 | #elif OS_TYPE == 2 /* uC/OS-II */ 196 | OSMutexPost(Mutex[vol]); 197 | 198 | #elif OS_TYPE == 3 /* FreeRTOS */ 199 | xSemaphoreGive(Mutex[vol]); 200 | 201 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 202 | osMutexRelease(Mutex[vol]); 203 | 204 | #endif 205 | } 206 | 207 | #endif /* FF_FS_REENTRANT */ 208 | 209 | -------------------------------------------------------------------------------- /ext/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(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 | -------------------------------------------------------------------------------- /ext/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/documents/res/app4.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Low level disk I/O module function checker / 3 | /-----------------------------------------------------------------------/ 4 | / WARNING: The data on the target drive will be lost! 5 | */ 6 | 7 | #include 8 | #include 9 | #include "ff.h" /* Declarations of sector size */ 10 | #include "diskio.h" /* Declarations of disk functions */ 11 | 12 | 13 | 14 | static DWORD pn ( /* Pseudo random number generator */ 15 | DWORD pns /* 0:Initialize, !0:Read */ 16 | ) 17 | { 18 | static DWORD lfsr; 19 | UINT n; 20 | 21 | 22 | if (pns) { 23 | lfsr = pns; 24 | for (n = 0; n < 32; n++) pn(0); 25 | } 26 | if (lfsr & 1) { 27 | lfsr >>= 1; 28 | lfsr ^= 0x80200003; 29 | } else { 30 | lfsr >>= 1; 31 | } 32 | return lfsr; 33 | } 34 | 35 | 36 | int test_diskio ( 37 | BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */ 38 | UINT ncyc, /* Number of test cycles */ 39 | DWORD* buff, /* Pointer to the working buffer */ 40 | UINT sz_buff /* Size of the working buffer in unit of byte */ 41 | ) 42 | { 43 | UINT n, cc, ns; 44 | DWORD sz_drv, lba, lba2, sz_eblk, pns = 1; 45 | WORD sz_sect; 46 | BYTE *pbuff = (BYTE*)buff; 47 | DSTATUS ds; 48 | DRESULT dr; 49 | 50 | 51 | printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff); 52 | 53 | if (sz_buff < FF_MAX_SS + 8) { 54 | printf("Insufficient work area to run the program.\n"); 55 | return 1; 56 | } 57 | 58 | for (cc = 1; cc <= ncyc; cc++) { 59 | printf("**** Test cycle %u of %u start ****\n", cc, ncyc); 60 | 61 | printf(" disk_initalize(%u)", pdrv); 62 | ds = disk_initialize(pdrv); 63 | if (ds & STA_NOINIT) { 64 | printf(" - failed.\n"); 65 | return 2; 66 | } else { 67 | printf(" - ok.\n"); 68 | } 69 | 70 | printf("**** Get drive size ****\n"); 71 | printf(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (UINT)&sz_drv); 72 | sz_drv = 0; 73 | dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv); 74 | if (dr == RES_OK) { 75 | printf(" - ok.\n"); 76 | } else { 77 | printf(" - failed.\n"); 78 | return 3; 79 | } 80 | if (sz_drv < 128) { 81 | printf("Failed: Insufficient drive size to test.\n"); 82 | return 4; 83 | } 84 | printf(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv); 85 | 86 | #if FF_MAX_SS != FF_MIN_SS 87 | printf("**** Get sector size ****\n"); 88 | printf(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (UINT)&sz_sect); 89 | sz_sect = 0; 90 | dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect); 91 | if (dr == RES_OK) { 92 | printf(" - ok.\n"); 93 | } else { 94 | printf(" - failed.\n"); 95 | return 5; 96 | } 97 | printf(" Size of sector is %u bytes.\n", sz_sect); 98 | #else 99 | sz_sect = FF_MAX_SS; 100 | #endif 101 | 102 | printf("**** Get block size ****\n"); 103 | printf(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (UINT)&sz_eblk); 104 | sz_eblk = 0; 105 | dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk); 106 | if (dr == RES_OK) { 107 | printf(" - ok.\n"); 108 | } else { 109 | printf(" - failed.\n"); 110 | } 111 | if (dr == RES_OK || sz_eblk >= 2) { 112 | printf(" Size of the erase block is %lu sectors.\n", sz_eblk); 113 | } else { 114 | printf(" Size of the erase block is unknown.\n"); 115 | } 116 | 117 | /* Single sector write test */ 118 | printf("**** Single sector write test ****\n"); 119 | lba = 0; 120 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0); 121 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 122 | dr = disk_write(pdrv, pbuff, lba, 1); 123 | if (dr == RES_OK) { 124 | printf(" - ok.\n"); 125 | } else { 126 | printf(" - failed.\n"); 127 | return 6; 128 | } 129 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 130 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 131 | if (dr == RES_OK) { 132 | printf(" - ok.\n"); 133 | } else { 134 | printf(" - failed.\n"); 135 | return 7; 136 | } 137 | memset(pbuff, 0, sz_sect); 138 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 139 | dr = disk_read(pdrv, pbuff, lba, 1); 140 | if (dr == RES_OK) { 141 | printf(" - ok.\n"); 142 | } else { 143 | printf(" - failed.\n"); 144 | return 8; 145 | } 146 | for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ; 147 | if (n == sz_sect) { 148 | printf(" Read data matched.\n"); 149 | } else { 150 | printf(" Read data differs from the data written.\n"); 151 | return 10; 152 | } 153 | pns++; 154 | 155 | printf("**** Multiple sector write test ****\n"); 156 | lba = 5; ns = sz_buff / sz_sect; 157 | if (ns > 4) ns = 4; 158 | if (ns > 1) { 159 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0); 160 | printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 161 | dr = disk_write(pdrv, pbuff, lba, ns); 162 | if (dr == RES_OK) { 163 | printf(" - ok.\n"); 164 | } else { 165 | printf(" - failed.\n"); 166 | return 11; 167 | } 168 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 169 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 170 | if (dr == RES_OK) { 171 | printf(" - ok.\n"); 172 | } else { 173 | printf(" - failed.\n"); 174 | return 12; 175 | } 176 | memset(pbuff, 0, sz_sect * ns); 177 | printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 178 | dr = disk_read(pdrv, pbuff, lba, ns); 179 | if (dr == RES_OK) { 180 | printf(" - ok.\n"); 181 | } else { 182 | printf(" - failed.\n"); 183 | return 13; 184 | } 185 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ; 186 | if (n == (UINT)(sz_sect * ns)) { 187 | printf(" Read data matched.\n"); 188 | } else { 189 | printf(" Read data differs from the data written.\n"); 190 | return 14; 191 | } 192 | } else { 193 | printf(" Test skipped.\n"); 194 | } 195 | pns++; 196 | 197 | printf("**** Single sector write test (unaligned buffer address) ****\n"); 198 | lba = 5; 199 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0); 200 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba); 201 | dr = disk_write(pdrv, pbuff+3, lba, 1); 202 | if (dr == RES_OK) { 203 | printf(" - ok.\n"); 204 | } else { 205 | printf(" - failed.\n"); 206 | return 15; 207 | } 208 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 209 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 210 | if (dr == RES_OK) { 211 | printf(" - ok.\n"); 212 | } else { 213 | printf(" - failed.\n"); 214 | return 16; 215 | } 216 | memset(pbuff+5, 0, sz_sect); 217 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+5), lba); 218 | dr = disk_read(pdrv, pbuff+5, lba, 1); 219 | if (dr == RES_OK) { 220 | printf(" - ok.\n"); 221 | } else { 222 | printf(" - failed.\n"); 223 | return 17; 224 | } 225 | for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ; 226 | if (n == sz_sect) { 227 | printf(" Read data matched.\n"); 228 | } else { 229 | printf(" Read data differs from the data written.\n"); 230 | return 18; 231 | } 232 | pns++; 233 | 234 | printf("**** 4GB barrier test ****\n"); 235 | if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) { 236 | lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2); 237 | for (n = 0, pn(pns); n < (UINT)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0); 238 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 239 | dr = disk_write(pdrv, pbuff, lba, 1); 240 | if (dr == RES_OK) { 241 | printf(" - ok.\n"); 242 | } else { 243 | printf(" - failed.\n"); 244 | return 19; 245 | } 246 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 247 | dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1); 248 | if (dr == RES_OK) { 249 | printf(" - ok.\n"); 250 | } else { 251 | printf(" - failed.\n"); 252 | return 20; 253 | } 254 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 255 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 256 | if (dr == RES_OK) { 257 | printf(" - ok.\n"); 258 | } else { 259 | printf(" - failed.\n"); 260 | return 21; 261 | } 262 | memset(pbuff, 0, sz_sect * 2); 263 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 264 | dr = disk_read(pdrv, pbuff, lba, 1); 265 | if (dr == RES_OK) { 266 | printf(" - ok.\n"); 267 | } else { 268 | printf(" - failed.\n"); 269 | return 22; 270 | } 271 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 272 | dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1); 273 | if (dr == RES_OK) { 274 | printf(" - ok.\n"); 275 | } else { 276 | printf(" - failed.\n"); 277 | return 23; 278 | } 279 | for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ; 280 | if (n == (UINT)(sz_sect * 2)) { 281 | printf(" Read data matched.\n"); 282 | } else { 283 | printf(" Read data differs from the data written.\n"); 284 | return 24; 285 | } 286 | } else { 287 | printf(" Test skipped.\n"); 288 | } 289 | pns++; 290 | 291 | printf("**** Test cycle %u of %u completed ****\n\n", cc, ncyc); 292 | } 293 | 294 | return 0; 295 | } 296 | 297 | 298 | 299 | int main (int argc, char* argv[]) 300 | { 301 | int rc; 302 | DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */ 303 | 304 | /* Check function/compatibility of the physical drive #0 */ 305 | rc = test_diskio(0, 3, buff, sizeof buff); 306 | 307 | if (rc) { 308 | printf("Sorry the function/compatibility test failed. (rc=%d)\nFatFs will not work with this disk driver.\n", rc); 309 | } else { 310 | printf("Congratulations! The disk driver works well.\n"); 311 | } 312 | 313 | return rc; 314 | } 315 | 316 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/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 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/source/ffconf.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------/ 2 | / Configurations of FatFs Module 3 | /---------------------------------------------------------------------------*/ 4 | 5 | #define FFCONF_DEF 80286 /* 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_FIND 1 29 | /* This option switches filtered directory read functions, f_findfirst() and 30 | / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ 31 | 32 | 33 | #define FF_USE_MKFS 1 34 | /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ 35 | 36 | 37 | #define FF_USE_FASTSEEK 1 38 | /* This option switches fast seek function. (0:Disable or 1:Enable) */ 39 | 40 | 41 | #define FF_USE_EXPAND 0 42 | /* This option switches f_expand function. (0:Disable or 1:Enable) */ 43 | 44 | 45 | #define FF_USE_CHMOD 0 46 | /* This option switches attribute manipulation functions, f_chmod() and f_utime(). 47 | / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ 48 | 49 | 50 | #define FF_USE_LABEL 0 51 | /* This option switches volume label functions, f_getlabel() and f_setlabel(). 52 | / (0:Disable or 1:Enable) */ 53 | 54 | 55 | #define FF_USE_FORWARD 0 56 | /* This option switches f_forward() function. (0:Disable or 1:Enable) */ 57 | 58 | 59 | #define FF_USE_STRFUNC 1 60 | #define FF_PRINT_LLI 1 61 | #define FF_PRINT_FLOAT 1 62 | #define FF_STRF_ENCODE 3 63 | /* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and 64 | / f_printf(). 65 | / 66 | / 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect. 67 | / 1: Enable without LF-CRLF conversion. 68 | / 2: Enable with LF-CRLF conversion. 69 | / 70 | / FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2 71 | / makes f_printf() support floating point argument. These features want C99 or later. 72 | / When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character 73 | / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE 74 | / to be read/written via those functions. 75 | / 76 | / 0: ANSI/OEM in current CP 77 | / 1: Unicode in UTF-16LE 78 | / 2: Unicode in UTF-16BE 79 | / 3: Unicode in UTF-8 80 | */ 81 | 82 | 83 | /*---------------------------------------------------------------------------/ 84 | / Locale and Namespace Configurations 85 | /---------------------------------------------------------------------------*/ 86 | 87 | #define FF_CODE_PAGE 437 88 | /* This option specifies the OEM code page to be used on the target system. 89 | / Incorrect code page setting can cause a file open failure. 90 | / 91 | / 437 - U.S. 92 | / 720 - Arabic 93 | / 737 - Greek 94 | / 771 - KBL 95 | / 775 - Baltic 96 | / 850 - Latin 1 97 | / 852 - Latin 2 98 | / 855 - Cyrillic 99 | / 857 - Turkish 100 | / 860 - Portuguese 101 | / 861 - Icelandic 102 | / 862 - Hebrew 103 | / 863 - Canadian French 104 | / 864 - Arabic 105 | / 865 - Nordic 106 | / 866 - Russian 107 | / 869 - Greek 2 108 | / 932 - Japanese (DBCS) 109 | / 936 - Simplified Chinese (DBCS) 110 | / 949 - Korean (DBCS) 111 | / 950 - Traditional Chinese (DBCS) 112 | / 0 - Include all code pages above and configured by f_setcp() 113 | */ 114 | 115 | 116 | #define FF_USE_LFN 3 117 | #define FF_MAX_LFN 255 118 | /* The FF_USE_LFN switches the support for LFN (long file name). 119 | / 120 | / 0: Disable LFN. FF_MAX_LFN has no effect. 121 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 122 | / 2: Enable LFN with dynamic working buffer on the STACK. 123 | / 3: Enable LFN with dynamic working buffer on the HEAP. 124 | / 125 | / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function 126 | / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 127 | / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. 128 | / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can 129 | / be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN 130 | / specification. 131 | / When use stack for the working buffer, take care on stack overflow. When use heap 132 | / memory for the working buffer, memory management functions, ff_memalloc() and 133 | / ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ 134 | 135 | 136 | #define FF_LFN_UNICODE 2 137 | /* This option switches the character encoding on the API when LFN is enabled. 138 | / 139 | / 0: ANSI/OEM in current CP (TCHAR = char) 140 | / 1: Unicode in UTF-16 (TCHAR = WCHAR) 141 | / 2: Unicode in UTF-8 (TCHAR = char) 142 | / 3: Unicode in UTF-32 (TCHAR = DWORD) 143 | / 144 | / Also behavior of string I/O functions will be affected by this option. 145 | / When LFN is not enabled, this option has no effect. */ 146 | 147 | 148 | #define FF_LFN_BUF 255 149 | #define FF_SFN_BUF 12 150 | /* This set of options defines size of file name members in the FILINFO structure 151 | / which is used to read out directory items. These values should be suffcient for 152 | / the file names to read. The maximum possible length of the read file name depends 153 | / on character encoding. When LFN is not enabled, these options have no effect. */ 154 | 155 | 156 | #define FF_FS_RPATH 2 157 | /* This option configures support for relative path. 158 | / 159 | / 0: Disable relative path and remove related functions. 160 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 161 | / 2: f_getcwd() function is available in addition to 1. 162 | */ 163 | 164 | 165 | /*---------------------------------------------------------------------------/ 166 | / Drive/Volume Configurations 167 | /---------------------------------------------------------------------------*/ 168 | 169 | # define FF_VOLUMES 2 170 | /* Number of volumes (logical drives) to be used. (1-10) */ 171 | 172 | 173 | #define FF_STR_VOLUME_ID 0 174 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 175 | /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. 176 | / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive 177 | / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each 178 | / logical drives. Number of items must not be less than FF_VOLUMES. Valid 179 | / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are 180 | / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is 181 | / not defined, a user defined volume string table is needed as: 182 | / 183 | / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... 184 | */ 185 | 186 | 187 | #define FF_MULTI_PARTITION 0 188 | /* This option switches support for multiple volumes on the physical drive. 189 | / By default (0), each logical drive number is bound to the same physical drive 190 | / number and only an FAT volume found on the physical drive will be mounted. 191 | / When this function is enabled (1), each logical drive number can be bound to 192 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 193 | / function will be available. */ 194 | 195 | 196 | #define FF_MIN_SS 512 197 | #define FF_MAX_SS 512 198 | /* This set of options configures the range of sector size to be supported. (512, 199 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 200 | / harddisk, but a larger value may be required for on-board flash memory and some 201 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 202 | / for variable sector size mode and disk_ioctl() function needs to implement 203 | / GET_SECTOR_SIZE command. */ 204 | 205 | 206 | #define FF_LBA64 1 207 | /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) 208 | / To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ 209 | 210 | 211 | #define FF_MIN_GPT 0x10000000 212 | /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and 213 | / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ 214 | 215 | 216 | #define FF_USE_TRIM 0 217 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 218 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 219 | / disk_ioctl() function. */ 220 | 221 | 222 | 223 | /*---------------------------------------------------------------------------/ 224 | / System Configurations 225 | /---------------------------------------------------------------------------*/ 226 | 227 | #define FF_FS_TINY 0 228 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 229 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 230 | / Instead of private sector buffer eliminated from the file object, common sector 231 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 232 | 233 | 234 | #define FF_FS_EXFAT 1 235 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 236 | / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) 237 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 238 | 239 | 240 | #define FF_FS_NORTC 0 241 | #define FF_NORTC_MON 1 242 | #define FF_NORTC_MDAY 1 243 | #define FF_NORTC_YEAR 2022 244 | /* The option FF_FS_NORTC switches timestamp feature. If the system does not have 245 | / an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the 246 | / timestamp feature. Every object modified by FatFs will have a fixed timestamp 247 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 248 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 249 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 250 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 251 | / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ 252 | 253 | 254 | #define FF_FS_NOFSINFO 0 255 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 256 | / option, and f_getfree() function at the first time after volume mount will force 257 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 258 | / 259 | / bit0=0: Use free cluster count in the FSINFO if available. 260 | / bit0=1: Do not trust free cluster count in the FSINFO. 261 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 262 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 263 | */ 264 | 265 | 266 | #define FF_FS_LOCK 16 267 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 268 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 269 | / is 1. 270 | / 271 | / 0: Disable file lock function. To avoid volume corruption, application program 272 | / should avoid illegal open, remove and rename to the open objects. 273 | / >0: Enable file lock function. The value defines how many files/sub-directories 274 | / can be opened simultaneously under file lock control. Note that the file 275 | / lock control is independent of re-entrancy. */ 276 | 277 | 278 | #define FF_FS_REENTRANT 0 279 | #define FF_FS_TIMEOUT 1000 280 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 281 | / module itself. Note that regardless of this option, file access to different 282 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 283 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 284 | / to the same volume is under control of this featuer. 285 | / 286 | / 0: Disable re-entrancy. FF_FS_TIMEOUT have no effect. 287 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 288 | / ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give() 289 | / function, must be added to the project. Samples are available in ffsystem.c. 290 | / 291 | / The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick. 292 | */ 293 | 294 | 295 | 296 | /*--- End of configuration options ---*/ 297 | -------------------------------------------------------------------------------- /ext/FatFs_SPI/ff15/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 | 348 | 349 | R0.14b (April 17, 2021) 350 | Made FatFs uses standard library for copy, compare and search instead of built-in string functions. 351 | Added support for long long integer and floating point to f_printf(). (FF_STRF_LLI and FF_STRF_FP) 352 | Made path name parser ignore the terminating separator to allow "dir/". 353 | Improved the compatibility in Unix style path name feature. 354 | Fixed the file gets dead-locked when f_open() failed with some conditions. (appeared at R0.12a) 355 | Fixed f_mkfs() can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12) 356 | Fixed code page 855 cannot be set by f_setcp(). 357 | Fixed some compiler warnings. 358 | 359 | 360 | 361 | R0.15 (November 6, 2022) 362 | Changed user provided synchronization functions in order to completely eliminate the platform dependency from FatFs code. 363 | FF_SYNC_t is removed from the configuration options. 364 | Fixed a potential error in f_mount when FF_FS_REENTRANT. 365 | Fixed file lock control FF_FS_LOCK is not mutal excluded when FF_FS_REENTRANT && FF_VOLUMES > 1 is true. 366 | Fixed f_mkfs() creates broken exFAT volume when the size of volume is >= 2^32 sectors. 367 | Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8). 368 | Fixed a compatibility issue in identification of GPT header. 369 | 370 | -------------------------------------------------------------------------------- /ext/minigb_apu/minigb_apu.c: -------------------------------------------------------------------------------- 1 | /** 2 | * minigb_apu is released under the terms listed within the LICENSE file. 3 | * 4 | * minigb_apu emulates the audio processing unit (APU) of the Game Boy. This 5 | * project is based on MiniGBS by Alex Baines: https://github.com/baines/MiniGBS 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "minigb_apu.h" 13 | 14 | #define DMG_CLOCK_FREQ_U ((unsigned)DMG_CLOCK_FREQ) 15 | #define AUDIO_NSAMPLES (AUDIO_SAMPLES * 2u) 16 | 17 | #define AUDIO_MEM_SIZE (0xFF3F - 0xFF10 + 1) 18 | #define AUDIO_ADDR_COMPENSATION 0xFF10 19 | 20 | #define MAX(a, b) ( a > b ? a : b ) 21 | #define MIN(a, b) ( a <= b ? a : b ) 22 | 23 | #define VOL_INIT_MAX (INT16_MAX/8) 24 | #define VOL_INIT_MIN (INT16_MIN/8) 25 | 26 | /* Handles time keeping for sound generation. 27 | * FREQ_INC_REF must be equal to, or larger than AUDIO_SAMPLE_RATE in order 28 | * to avoid a division by zero error. 29 | * Using a square of 2 simplifies calculations. */ 30 | #define FREQ_INC_REF (AUDIO_SAMPLE_RATE * 16) 31 | 32 | #define MAX_CHAN_VOLUME 15 33 | 34 | /** 35 | * Memory holding audio registers between 0xFF10 and 0xFF3F inclusive. 36 | */ 37 | static uint8_t audio_mem[AUDIO_MEM_SIZE]; 38 | 39 | struct chan_len_ctr { 40 | uint8_t load; 41 | unsigned enabled : 1; 42 | uint32_t counter; 43 | uint32_t inc; 44 | }; 45 | 46 | struct chan_vol_env { 47 | uint8_t step; 48 | unsigned up : 1; 49 | uint32_t counter; 50 | uint32_t inc; 51 | }; 52 | 53 | struct chan_freq_sweep { 54 | uint16_t freq; 55 | uint8_t rate; 56 | uint8_t shift; 57 | unsigned up : 1; 58 | uint32_t counter; 59 | uint32_t inc; 60 | }; 61 | 62 | static struct chan { 63 | unsigned enabled : 1; 64 | unsigned powered : 1; 65 | unsigned on_left : 1; 66 | unsigned on_right : 1; 67 | unsigned muted : 1; 68 | 69 | uint8_t volume; 70 | uint8_t volume_init; 71 | 72 | uint16_t freq; 73 | uint32_t freq_counter; 74 | uint32_t freq_inc; 75 | 76 | int_fast16_t val; 77 | 78 | struct chan_len_ctr len; 79 | struct chan_vol_env env; 80 | struct chan_freq_sweep sweep; 81 | 82 | union { 83 | struct { 84 | uint8_t duty; 85 | uint8_t duty_counter; 86 | } square; 87 | struct { 88 | uint16_t lfsr_reg; 89 | uint8_t lfsr_wide; 90 | uint8_t lfsr_div; 91 | } noise; 92 | struct { 93 | uint8_t sample; 94 | } wave; 95 | }; 96 | } chans[4]; 97 | 98 | static int32_t vol_l, vol_r; 99 | 100 | static void set_note_freq(struct chan *c, const uint32_t freq) 101 | { 102 | /* Lowest expected value of freq is 64. */ 103 | c->freq_inc = freq * (uint32_t)(FREQ_INC_REF / AUDIO_SAMPLE_RATE); 104 | } 105 | 106 | static void chan_enable(const uint_fast8_t i, const bool enable) 107 | { 108 | uint8_t val; 109 | 110 | chans[i].enabled = enable; 111 | val = (audio_mem[0xFF26 - AUDIO_ADDR_COMPENSATION] & 0x80) | 112 | (chans[3].enabled << 3) | (chans[2].enabled << 2) | 113 | (chans[1].enabled << 1) | (chans[0].enabled << 0); 114 | 115 | audio_mem[0xFF26 - AUDIO_ADDR_COMPENSATION] = val; 116 | //audio_mem[0xFF26 - AUDIO_ADDR_COMPENSATION] |= 0x80 | ((uint8_t)enable) << i; 117 | } 118 | 119 | static void update_env(struct chan *c) 120 | { 121 | c->env.counter += c->env.inc; 122 | 123 | while (c->env.counter > FREQ_INC_REF) { 124 | if (c->env.step) { 125 | c->volume += c->env.up ? 1 : -1; 126 | if (c->volume == 0 || c->volume == MAX_CHAN_VOLUME) { 127 | c->env.inc = 0; 128 | } 129 | c->volume = MAX(0, MIN(MAX_CHAN_VOLUME, c->volume)); 130 | } 131 | c->env.counter -= FREQ_INC_REF; 132 | } 133 | } 134 | 135 | static void update_len(struct chan *c) 136 | { 137 | if (!c->len.enabled) 138 | return; 139 | 140 | c->len.counter += c->len.inc; 141 | if (c->len.counter > FREQ_INC_REF) { 142 | chan_enable(c - chans, 0); 143 | c->len.counter = 0; 144 | } 145 | } 146 | 147 | static bool update_freq(struct chan *c, uint32_t *pos) 148 | { 149 | uint32_t inc = c->freq_inc - *pos; 150 | c->freq_counter += inc; 151 | 152 | if (c->freq_counter > FREQ_INC_REF) { 153 | *pos = c->freq_inc - (c->freq_counter - FREQ_INC_REF); 154 | c->freq_counter = 0; 155 | return true; 156 | } else { 157 | *pos = c->freq_inc; 158 | return false; 159 | } 160 | } 161 | 162 | static void update_sweep(struct chan *c) 163 | { 164 | c->sweep.counter += c->sweep.inc; 165 | 166 | while (c->sweep.counter > FREQ_INC_REF) { 167 | if (c->sweep.shift) { 168 | uint16_t inc = (c->sweep.freq >> c->sweep.shift); 169 | if (!c->sweep.up) 170 | inc *= -1; 171 | 172 | c->freq += inc; 173 | if (c->freq > 2047) { 174 | c->enabled = 0; 175 | } else { 176 | set_note_freq(c, 177 | DMG_CLOCK_FREQ_U / ((2048 - c->freq)<< 5)); 178 | c->freq_inc *= 8; 179 | } 180 | } else if (c->sweep.rate) { 181 | c->enabled = 0; 182 | } 183 | c->sweep.counter -= FREQ_INC_REF; 184 | } 185 | } 186 | 187 | static void update_square(int16_t* samples, const bool ch2) 188 | { 189 | uint32_t freq; 190 | struct chan* c = chans + ch2; 191 | 192 | if (!c->powered || !c->enabled) 193 | return; 194 | 195 | freq = DMG_CLOCK_FREQ_U / ((2048 - c->freq) << 5); 196 | set_note_freq(c, freq); 197 | c->freq_inc *= 8; 198 | 199 | for (uint_fast16_t i = 0; i < AUDIO_NSAMPLES; i += 2) { 200 | update_len(c); 201 | 202 | if (!c->enabled) 203 | continue; 204 | 205 | update_env(c); 206 | if (!ch2) 207 | update_sweep(c); 208 | 209 | uint32_t pos = 0; 210 | uint32_t prev_pos = 0; 211 | int32_t sample = 0; 212 | 213 | while (update_freq(c, &pos)) { 214 | c->square.duty_counter = (c->square.duty_counter + 1) & 7; 215 | sample += ((pos - prev_pos) / c->freq_inc) * c->val; 216 | c->val = (c->square.duty & (1 << c->square.duty_counter)) ? 217 | VOL_INIT_MAX / MAX_CHAN_VOLUME : 218 | VOL_INIT_MIN / MAX_CHAN_VOLUME; 219 | prev_pos = pos; 220 | } 221 | 222 | if (c->muted) 223 | continue; 224 | 225 | sample += c->val; 226 | sample *= c->volume; 227 | sample /= 4; 228 | 229 | samples[i + 0] += sample * c->on_left * vol_l; 230 | samples[i + 1] += sample * c->on_right * vol_r; 231 | } 232 | } 233 | 234 | static uint8_t wave_sample(const unsigned int pos, const unsigned int volume) 235 | { 236 | uint8_t sample; 237 | 238 | sample = audio_mem[(0xFF30 + pos / 2) - AUDIO_ADDR_COMPENSATION]; 239 | if (pos & 1) { 240 | sample &= 0xF; 241 | } else { 242 | sample >>= 4; 243 | } 244 | return volume ? (sample >> (volume - 1)) : 0; 245 | } 246 | 247 | static void update_wave(int16_t *samples) 248 | { 249 | uint32_t freq; 250 | struct chan *c = chans + 2; 251 | 252 | if (!c->powered || !c->enabled) 253 | return; 254 | 255 | freq = (DMG_CLOCK_FREQ_U / 64) / (2048 - c->freq); 256 | set_note_freq(c, freq); 257 | 258 | c->freq_inc *= 32; 259 | 260 | for (uint_fast16_t i = 0; i < AUDIO_NSAMPLES; i += 2) { 261 | update_len(c); 262 | 263 | if (!c->enabled) 264 | continue; 265 | 266 | uint32_t pos = 0; 267 | uint32_t prev_pos = 0; 268 | int32_t sample = 0; 269 | 270 | c->wave.sample = wave_sample(c->val, c->volume); 271 | 272 | while (update_freq(c, &pos)) { 273 | c->val = (c->val + 1) & 31; 274 | sample += ((pos - prev_pos) / c->freq_inc) * 275 | ((int)c->wave.sample - 8) * (INT16_MAX/64); 276 | c->wave.sample = wave_sample(c->val, c->volume); 277 | prev_pos = pos; 278 | } 279 | 280 | sample += ((int)c->wave.sample - 8) * (int)(INT16_MAX/64); 281 | 282 | if (c->volume == 0) 283 | continue; 284 | 285 | { 286 | /* First element is unused. */ 287 | int16_t div[] = { INT16_MAX, 1, 2, 4 }; 288 | sample = sample / (div[c->volume]); 289 | } 290 | 291 | if (c->muted) 292 | continue; 293 | 294 | sample /= 4; 295 | 296 | samples[i + 0] += sample * c->on_left * vol_l; 297 | samples[i + 1] += sample * c->on_right * vol_r; 298 | } 299 | } 300 | 301 | static void update_noise(int16_t *samples) 302 | { 303 | struct chan *c = chans + 3; 304 | 305 | if (!c->powered) 306 | return; 307 | 308 | { 309 | const uint32_t lfsr_div_lut[] = { 310 | 8, 16, 32, 48, 64, 80, 96, 112 311 | }; 312 | uint32_t freq; 313 | 314 | freq = DMG_CLOCK_FREQ_U / (lfsr_div_lut[c->noise.lfsr_div] << c->freq); 315 | set_note_freq(c, freq); 316 | } 317 | 318 | if (c->freq >= 14) 319 | c->enabled = 0; 320 | 321 | for (uint_fast16_t i = 0; i < AUDIO_NSAMPLES; i += 2) { 322 | update_len(c); 323 | 324 | if (!c->enabled) 325 | continue; 326 | 327 | update_env(c); 328 | 329 | uint32_t pos = 0; 330 | uint32_t prev_pos = 0; 331 | int32_t sample = 0; 332 | 333 | while (update_freq(c, &pos)) { 334 | c->noise.lfsr_reg = (c->noise.lfsr_reg << 1) | 335 | (c->val >= VOL_INIT_MAX/MAX_CHAN_VOLUME); 336 | 337 | if (c->noise.lfsr_wide) { 338 | c->val = !(((c->noise.lfsr_reg >> 14) & 1) ^ 339 | ((c->noise.lfsr_reg >> 13) & 1)) ? 340 | VOL_INIT_MAX / MAX_CHAN_VOLUME : 341 | VOL_INIT_MIN / MAX_CHAN_VOLUME; 342 | } else { 343 | c->val = !(((c->noise.lfsr_reg >> 6) & 1) ^ 344 | ((c->noise.lfsr_reg >> 5) & 1)) ? 345 | VOL_INIT_MAX / MAX_CHAN_VOLUME : 346 | VOL_INIT_MIN / MAX_CHAN_VOLUME; 347 | } 348 | 349 | sample += ((pos - prev_pos) / c->freq_inc) * c->val; 350 | prev_pos = pos; 351 | } 352 | 353 | if (c->muted) 354 | continue; 355 | 356 | sample += c->val; 357 | sample *= c->volume; 358 | sample /= 4; 359 | 360 | samples[i + 0] += sample * c->on_left * vol_l; 361 | samples[i + 1] += sample * c->on_right * vol_r; 362 | } 363 | } 364 | 365 | /** 366 | * SDL2 style audio callback function. 367 | */ 368 | void audio_callback(void *userdata, int16_t *stream, size_t len) 369 | { 370 | /* Appease unused variable warning. */ 371 | (void)userdata; 372 | 373 | memset(stream, 0, len); 374 | 375 | update_square(stream, 0); 376 | update_square(stream, 1); 377 | update_wave(stream); 378 | update_noise(stream); 379 | } 380 | 381 | static void chan_trigger(uint_fast8_t i) 382 | { 383 | struct chan *c = chans + i; 384 | 385 | chan_enable(i, 1); 386 | c->volume = c->volume_init; 387 | 388 | // volume envelope 389 | { 390 | uint8_t val = 391 | audio_mem[(0xFF12 + (i * 5)) - AUDIO_ADDR_COMPENSATION]; 392 | 393 | c->env.step = val & 0x07; 394 | c->env.up = val & 0x08 ? 1 : 0; 395 | c->env.inc = c->env.step ? 396 | (FREQ_INC_REF * 64ul) / ((uint32_t)c->env.step * AUDIO_SAMPLE_RATE) : 397 | (8ul * FREQ_INC_REF) / AUDIO_SAMPLE_RATE ; 398 | c->env.counter = 0; 399 | } 400 | 401 | // freq sweep 402 | if (i == 0) { 403 | uint8_t val = audio_mem[0xFF10 - AUDIO_ADDR_COMPENSATION]; 404 | 405 | c->sweep.freq = c->freq; 406 | c->sweep.rate = (val >> 4) & 0x07; 407 | c->sweep.up = !(val & 0x08); 408 | c->sweep.shift = (val & 0x07); 409 | c->sweep.inc = c->sweep.rate ? 410 | ((128 * FREQ_INC_REF) / (c->sweep.rate * AUDIO_SAMPLE_RATE)) : 0; 411 | c->sweep.counter = FREQ_INC_REF; 412 | } 413 | 414 | int len_max = 64; 415 | 416 | if (i == 2) { // wave 417 | len_max = 256; 418 | c->val = 0; 419 | } else if (i == 3) { // noise 420 | c->noise.lfsr_reg = 0xFFFF; 421 | c->val = VOL_INIT_MIN / MAX_CHAN_VOLUME; 422 | } 423 | 424 | c->len.inc = (256 * FREQ_INC_REF) / (AUDIO_SAMPLE_RATE * (len_max - c->len.load)); 425 | c->len.counter = 0; 426 | } 427 | 428 | /** 429 | * Read audio register. 430 | * \param addr Address of audio register. Must be 0xFF10 <= addr <= 0xFF3F. 431 | * This is not checked in this function. 432 | * \return Byte at address. 433 | */ 434 | uint8_t audio_read(const uint16_t addr) 435 | { 436 | static const uint8_t ortab[] = { 437 | 0x80, 0x3f, 0x00, 0xff, 0xbf, 438 | 0xff, 0x3f, 0x00, 0xff, 0xbf, 439 | 0x7f, 0xff, 0x9f, 0xff, 0xbf, 440 | 0xff, 0xff, 0x00, 0x00, 0xbf, 441 | 0x00, 0x00, 0x70, 442 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 443 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 444 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 445 | }; 446 | 447 | return audio_mem[addr - AUDIO_ADDR_COMPENSATION] | 448 | ortab[addr - AUDIO_ADDR_COMPENSATION]; 449 | } 450 | 451 | /** 452 | * Write audio register. 453 | * \param addr Address of audio register. Must be 0xFF10 <= addr <= 0xFF3F. 454 | * This is not checked in this function. 455 | * \param val Byte to write at address. 456 | */ 457 | void audio_write(const uint16_t addr, const uint8_t val) 458 | { 459 | /* Find sound channel corresponding to register address. */ 460 | uint_fast8_t i; 461 | 462 | if(addr == 0xFF26) 463 | { 464 | audio_mem[addr - AUDIO_ADDR_COMPENSATION] = val & 0x80; 465 | /* On APU power off, clear all registers apart from wave 466 | * RAM. */ 467 | if((val & 0x80) == 0) 468 | { 469 | memset(audio_mem, 0x00, 0xFF26 - AUDIO_ADDR_COMPENSATION); 470 | chans[0].enabled = false; 471 | chans[1].enabled = false; 472 | chans[2].enabled = false; 473 | chans[3].enabled = false; 474 | } 475 | 476 | return; 477 | } 478 | 479 | /* Ignore register writes if APU powered off. */ 480 | if(audio_mem[0xFF26 - AUDIO_ADDR_COMPENSATION] == 0x00) 481 | return; 482 | 483 | audio_mem[addr - AUDIO_ADDR_COMPENSATION] = val; 484 | i = (addr - AUDIO_ADDR_COMPENSATION) / 5; 485 | 486 | switch (addr) { 487 | case 0xFF12: 488 | case 0xFF17: 489 | case 0xFF21: { 490 | chans[i].volume_init = val >> 4; 491 | chans[i].powered = (val >> 3) != 0; 492 | 493 | // "zombie mode" stuff, needed for Prehistorik Man and probably 494 | // others 495 | if (chans[i].powered && chans[i].enabled) { 496 | if ((chans[i].env.step == 0 && chans[i].env.inc != 0)) { 497 | if (val & 0x08) { 498 | chans[i].volume++; 499 | } else { 500 | chans[i].volume += 2; 501 | } 502 | } else { 503 | chans[i].volume = 16 - chans[i].volume; 504 | } 505 | 506 | chans[i].volume &= 0x0F; 507 | chans[i].env.step = val & 0x07; 508 | } 509 | } break; 510 | 511 | case 0xFF1C: 512 | chans[i].volume = chans[i].volume_init = (val >> 5) & 0x03; 513 | break; 514 | 515 | case 0xFF11: 516 | case 0xFF16: 517 | case 0xFF20: { 518 | const uint8_t duty_lookup[] = { 0x10, 0x30, 0x3C, 0xCF }; 519 | chans[i].len.load = val & 0x3f; 520 | chans[i].square.duty = duty_lookup[val >> 6]; 521 | break; 522 | } 523 | 524 | case 0xFF1B: 525 | chans[i].len.load = val; 526 | break; 527 | 528 | case 0xFF13: 529 | case 0xFF18: 530 | case 0xFF1D: 531 | chans[i].freq &= 0xFF00; 532 | chans[i].freq |= val; 533 | break; 534 | 535 | case 0xFF1A: 536 | chans[i].powered = (val & 0x80) != 0; 537 | chan_enable(i, val & 0x80); 538 | break; 539 | 540 | case 0xFF14: 541 | case 0xFF19: 542 | case 0xFF1E: 543 | chans[i].freq &= 0x00FF; 544 | chans[i].freq |= ((val & 0x07) << 8); 545 | /* Intentional fall-through. */ 546 | case 0xFF23: 547 | chans[i].len.enabled = val & 0x40 ? 1 : 0; 548 | if (val & 0x80) 549 | chan_trigger(i); 550 | 551 | break; 552 | 553 | case 0xFF22: 554 | chans[3].freq = val >> 4; 555 | chans[3].noise.lfsr_wide = !(val & 0x08); 556 | chans[3].noise.lfsr_div = val & 0x07; 557 | break; 558 | 559 | case 0xFF24: 560 | { 561 | vol_l = ((val >> 4) & 0x07); 562 | vol_r = (val & 0x07); 563 | break; 564 | } 565 | 566 | case 0xFF25: 567 | for (uint_fast8_t j = 0; j < 4; j++) { 568 | chans[j].on_left = (val >> (4 + j)) & 1; 569 | chans[j].on_right = (val >> j) & 1; 570 | } 571 | break; 572 | } 573 | } 574 | 575 | void audio_init(void) 576 | { 577 | /* Initialise channels and samples. */ 578 | memset(chans, 0, sizeof(chans)); 579 | chans[0].val = chans[1].val = -1; 580 | 581 | /* Initialise IO registers. */ 582 | { 583 | const uint8_t regs_init[] = { 0x80, 0xBF, 0xF3, 0xFF, 0x3F, 584 | 0xFF, 0x3F, 0x00, 0xFF, 0x3F, 585 | 0x7F, 0xFF, 0x9F, 0xFF, 0x3F, 586 | 0xFF, 0xFF, 0x00, 0x00, 0x3F, 587 | 0x77, 0xF3, 0xF1 }; 588 | 589 | for(uint_fast8_t i = 0; i < sizeof(regs_init); ++i) 590 | audio_write(0xFF10 + i, regs_init[i]); 591 | } 592 | 593 | /* Initialise Wave Pattern RAM. */ 594 | { 595 | const uint8_t wave_init[] = { 0xac, 0xdd, 0xda, 0x48, 596 | 0x36, 0x02, 0xcf, 0x16, 597 | 0x2c, 0x04, 0xe5, 0x2c, 598 | 0xac, 0xdd, 0xda, 0x48 }; 599 | 600 | for(uint_fast8_t i = 0; i < sizeof(wave_init); ++i) 601 | audio_write(0xFF30 + i, wave_init[i]); 602 | } 603 | } 604 | -------------------------------------------------------------------------------- /ext/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 | --------------------------------------------------------------------------------