{"id":20586729,"url":"https://github.com/sparkfun/sparkfun-pico","last_synced_at":"2025-04-14T21:26:46.338Z","repository":{"id":253475308,"uuid":"838503117","full_name":"sparkfun/sparkfun-pico","owner":"sparkfun","description":"Libraries, examples and links in support of the Raspberry Pi rp2 silicon boards and associated software.","archived":false,"fork":false,"pushed_at":"2024-09-06T18:10:22.000Z","size":69,"stargazers_count":13,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-28T09:36:23.716Z","etag":null,"topics":["psram","rp2350"],"latest_commit_sha":null,"homepage":"https://www.sparkfun.com/products/24870","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sparkfun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-05T19:17:04.000Z","updated_at":"2025-03-22T18:23:02.000Z","dependencies_parsed_at":"2024-08-17T01:25:01.540Z","dependency_job_id":"6a159315-d35c-4744-a29c-b852b370f21c","html_url":"https://github.com/sparkfun/sparkfun-pico","commit_stats":null,"previous_names":["sparkfun/sparkfun-pico"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparkfun%2Fsparkfun-pico","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparkfun%2Fsparkfun-pico/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparkfun%2Fsparkfun-pico/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparkfun%2Fsparkfun-pico/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sparkfun","download_url":"https://codeload.github.com/sparkfun/sparkfun-pico/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248962238,"owners_count":21190160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["psram","rp2350"],"created_at":"2024-11-16T07:12:43.403Z","updated_at":"2025-04-14T21:26:46.317Z","avatar_url":"https://github.com/sparkfun.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SparkFun Examples/Utilities for the Raspberry Pi rp2* Processors\n\nThe repository contains a set of utilities, structured for use with the Raspberry Pico SDK. This are target a new or unique functionality not fully supported by the standard ```pico-sdk``` yet, or that are unique to SparkFun Boards.\n\n## Using The sparkfun_pico CMake library\n\nA majority of the functionality outlined in this repository is contained in the folder ```sparkfun_pico```. This folder is structured as a ```cmake``` library, ready for use by the ```pico-sdk``` build environment.\n\nThe general pattern when using this library is:\n\n* Copy the ```sparkfun_pico``` folder and all it's contents to your ```pico-sdk``` project directory.\n* Update the projects ```CMakeList.txt``` file to add the ```sparkfun_pico``` sub-directory, and potentially *malloc/free wrap directives* outlined below.\n* included the header file ```sparkfun_pico/sfe_pico.h``` in your projects source as needed.\n\n### Add sparkfun_pico Library to a pico-sdk Project\n\nTo use the library as part of the cmake build process of the ```pico-sdk```, perform the following:\n\n* Clone this repository to some directory ($SRC_DIR)\n* CD into the cloned repository\n* Checkout the submodules in the repository\n\n```sh\ncd $SRC_DIR\ngit clone https://github.com/sparkfun/sparkfun-pico\ncd sparkfun-pico\ngit submodule update --init --recursive\n```\n\n* Copy the ```sparkfun_pico``` folder in this repository to the project folder.\n\n```sh\ncd myproject\ncp -R $SRC_DIR/sparkfun-pico/sparkfun_pico .\n```\n\n* Add the sparkfun_pico library to your projects ```CMakeLists.txt``` file using the following line:\n\n```cmake\nadd_subdirectory(sparkfun_pico)\n```\n\n### Replacing the Integrated Memory Allocator Routines\n\nOne option the sparkfun_pico library provides is replacing the default allocator in the pico-sdk and it's associated members (```malloc, free, ...```) with a version that access both the build heap SRAM and PSRAM. This leverages the ***wrap*** functionality provided by the ```pico-sdk```.\n\nTo enable this functionality, the following lines are added to your projects CMakeLists.txt file.\n\n```cmake\n# use our own allocator\n\n# the following enables the system malloc/free to be wrapped\nset(SKIP_PICO_MALLOC 1)\n\n# the following enables wrapping in sparkfun_pico builds\nset(SFE_PICO_ALLOC_WRAP 1)\n\n# the following enables the system malloc/free to be wrapped during compilation\nadd_definitions(-DSFE_PICO_ALLOC_WRAP)\n```\n\n\u003e [!NOTE]\n\u003e These lines must be added before the include of the pico-sdk file ```pico_sdk_import.cmake```\n\nOnce setup in the CMakeFiles.txt file, the resulting firmware will use all available PSRAM and SRAM via the standard allocator API (malloc, free, calloc, realloc ).\n\nThe example [all_allocator](examples/all_allocator) shows how to use this functionality.\n\n### Board File\n\nThe sparkfun_pico library also includes a board file that includes board specific defines that are not part of the standard pico-sdk board files.\n\nCurrently this is just the PIN used as CS for the PSRAM on sparkfun RP2350 boards.\n\nExample:\n\n```c\n// For the pro micro rp2350\n#define SFE_RP2350_XIP_CSI_PIN 19\n```\n\nThe file settings key off the board #defined set during the build process.\n\nMostly the use of this board file is automatic, not requiring direct access, but when using the raw PSRAM detection functionality, the required value of the PSRAM CS pin is obtained by including the file ```sparkfun_pico/sfe_pico_boards.h```, and defining a supported board in your cmake command.\n\n```sh\ncmake .. -DPICO_BOARD=sparkfun_promicro_rp2350\n```\n\n## The sparkfun_pico API\n\nThe sparkfun_pico library supports the following functions, with function signatures declared in [sparkfun_pico/sfe_pico.h](sparkfun_pico/sfe_pico.h):\n\n### PSRAM detection\n\n\u003e [!NOTE]\n\u003e The SparkFun RP2350 boards us the PSRAM IC which is detailed [here](https://cdn.sparkfun.com/assets/0/a/3/d/e/APS6404L_3SQR_Datasheet.pdf)\n\nThese functions are exported from the file [sfe_psram.h](sparkfun_pico/sfe_psram.h)\n\n#### Setup\n\n```C\nsize_t sfe_setup_psram(uint32_t psram_cs_pin);\n```\n\nThis function is used to detect the presence of PSRAM on the board and return the size of the PSRAM available. If no PSRAM is detected, a value of ```0``` is returned.\n\n|Parameter|Description|\n|---|---|\n|psram_cs_pin| The CS pin used by the PSRAM IC on the board|\n|return | The size of the PSRAM detected in bytes. If no PSRAM detected the value of 0 is returned|\n\nAdditionally, if PSRAM is detected, it is setup correctly for use by the RP2350.\n\n#### Update Timing\n\n```C\nvoid sfe_psram_update_timing(void);\n```\n\nThis function is used to update the timing settings used by the pico-sdk to communicate with the PSRAM IC. These values are set during the PSRAM setup process based on the system clock. If the system clock value is changed (overclocking!) calling this method will adjust the PSRAM settings for use with the new clock frequency values.\n\n### Memory Allocation Functions\n\nThe following functions are available when using the sparkfun_pico memory allocator with PSRAM. They mimic the standard C memory allocation functions and are defined i n the include file [sfe_pico_alloc.h](sparkfun_pico/sfe_pico_alloc.h)\n\n#### Initialization\n\n```C\nbool sfe_pico_alloc_init();\n```\n\nThe function detect available PSRAM and initializes the allocator. If the system is setup for also using SRAM as part of the wrapping of the built in allocation functions, the available heap is added to the allocator also.\n\n\u003e [!NOTE]\n\u003e Calling this function is optional - it is called by default on the first call to a memory allocation function.\n\n#### Memory Allocation\n\n```C\nvoid *sfe_mem_malloc(size_t size);\n```\n\nAllocate a block of memory of the requested size.\n\n|Parameter|Description|\n|---|---|\n|size| The number of bytes to allocate|\n|return | void pointer to the allocated memory. NULL if allocation failed|\n\n#### Memory De-allocation\n\n```C\nvoid sfe_mem_free(void *ptr);\n```\n\nFree a block of memory.\n\n|Parameter|Description|\n|---|---|\n|ptr| The pointer to the memory to free|\n\n#### Memory Re-allocation\n\n```C\nvoid *sfe_mem_realloc(void *ptr, size_t size);\n```\n\nRe-allocate a block of memory to the given size using the following methodology:\n\n* Expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined.\n* Allocating a new memory block of size size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.\n\n|Parameter|Description|\n|---|---|\n| ptr | A previously allocated block of memory. If NULL, this methods behaves like standard malloc|\n|size| The number of bytes to re-allocate to|\n|return | void pointer to the allocated memory. NULL if allocation failed|\n\n#### Memory Allocation and Clear\n\n```C\nvoid *sfe_mem_calloc(size_t num, size_t size);\n```\n\nAllocate a block of memory of the requested size and zero/clear it out\n\n|Parameter|Description|\n|---|---|\n|size| The number of bytes to allocate|\n|return | void pointer to the allocated memory. NULL if allocation failed|\n\n#### Maximum Free Block Size\n\n```C\nsize_t sfe_mem_max_free_size(void);\n```\n\nThis function returns the maximum free block size available for allocation.\n\n#### Total Memory Pool Size\n\n```C\nsize_t sfe_mem_size(void);\n```\n\nThis function returns the total size of the available memory across all memory pools available to the allocator.\n\n#### Total Memory Used\n\n```C\nsize_t sfe_mem_used(void);\n```\n\nThis function returns the total size of memory used.\n\n## The Examples\n\n### Examples - General Use\n\nFor a majority of the examples provided in this repository - especially those related to the use of PSRAM on the RP2350 - the following steps are used to build the examples:\n\n* Clone and setup this repo as noted above\n* Copy in the ```sparkfun_pico``` library as noted above\n\nAt this point, the examples are built following the standard ```pico-sdk``` build process.\n\n\u003e[!NOTE]\n\u003e Specify the target SparkFun Board when calling cmake.\n\n```sh\nexport PICO_SDK_PATH=\u003cthe path to the pico-sdk\u003e\ncd examples/has_psram\nmkdir build\ncmake .. -DPICO_BOARD=sparkfun_promicro_rp2350\nmake\n```\n\nThis repository contains the following examples:\n\n### [has_psram](examples/has_psram)\n\nOne of the more simple examples, this example shows how to detect if PSRAM is on the rp2350 board, and the size of the PSRAM available. It also walks through the available PSRAM, manually setting and verifying values.\n\n### [psram_allocator](examples/psram_allocator)\n\nThis example detects the PSRAM available on the board, and adds it to an allocator, which manages the *allocation* of the PSRAM. PSRAM is accessed (allocated) using a provided API, which mimics the standard malloc/free functionality.  \n\nA \"Two-Level Segregated Fit\" (flsf) allocator is used from [here](https://github.com/espressif/tlsf).\n\n### [all_allocator](examples/all_allocator)\n\nThis example detects the PSRAM available on the board, and adds it as well as the built in SRAM based heap to an allocator to provide an unified access to available memory. The allocator manages the *allocation* of the PSRAM and heap SRAM via a single API. The example also ```wraps``` the built in ```malloc``` and ```free``` suite of commands to integrate with existing examples and uses.\n\nA \"Two-Level Segregated Fit\" (flsf) allocator is used from [here](https://github.com/espressif/tlsf).\n\n### [set_qflash](examples/set_qflash)\n\nThis is a simple example that is used to verify the Quad SPI bit is set in the flash IC used on the attached board. It doesn't require the use of the ```sparkfun_pico``` library, but is helpful during board development and fits with the goals of this repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsparkfun%2Fsparkfun-pico","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsparkfun%2Fsparkfun-pico","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsparkfun%2Fsparkfun-pico/lists"}