├── .github └── stale.yml ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── examples ├── st7789_blink │ ├── CMakeLists.txt │ └── main.c ├── st7789_random │ ├── CMakeLists.txt │ └── main.c └── st7789_random_rows │ ├── CMakeLists.txt │ └── main.c ├── pico_sdk_import.cmake └── src ├── include └── pico │ └── st7789.h └── st7789.c /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 60 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - pinned 8 | - security 9 | - help wanted 10 | # Label to use when marking an issue as stale 11 | staleLabel: stale 12 | # Comment to post when marking an issue as stale. Set to `false` to disable 13 | markComment: > 14 | This issue has been automatically marked as stale because it has not had 15 | recent activity. It will be closed if no further activity occurs. Thank you 16 | for your contributions. 17 | # Comment to post when closing a stale issue. Set to `false` to disable 18 | closeComment: false 19 | # Never stale pull requests 20 | only: issues -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | CMakeCache.txt 3 | CMakeFiles 4 | CMakeScripts 5 | Testing 6 | Makefile 7 | cmake_install.cmake 8 | install_manifest.txt 9 | compile_commands.json 10 | CTestTestfile.cmake 11 | _deps 12 | 13 | build/ 14 | 15 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | 3 | # initialize pico_sdk from GIT 4 | # (note this can come from environment, CMake cache etc) 5 | # set(PICO_SDK_FETCH_FROM_GIT on) 6 | 7 | # pico_sdk_import.cmake is a single file copied from this SDK 8 | # note: this must happen before project() 9 | include(pico_sdk_import.cmake) 10 | 11 | project(pico_st7789) 12 | 13 | # initialize the Pico SDK 14 | pico_sdk_init() 15 | 16 | add_library(pico_st7789 INTERFACE) 17 | 18 | target_sources(pico_st7789 INTERFACE 19 | ${CMAKE_CURRENT_LIST_DIR}/src/st7789.c 20 | ) 21 | 22 | target_include_directories(pico_st7789 INTERFACE 23 | ${CMAKE_CURRENT_LIST_DIR}/src/include 24 | ) 25 | 26 | target_link_libraries(pico_st7789 INTERFACE pico_stdlib hardware_spi) 27 | 28 | add_subdirectory("examples/st7789_blink") 29 | add_subdirectory("examples/st7789_random") 30 | add_subdirectory("examples/st7789_random_rows") 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ST7789 library for Pico 2 | 3 | Use an ST7789 based TFT LCD display with your [Raspberry Pi Pico](https://www.raspberrypi.org/products/raspberry-pi-pico/) or any [RP2040](https://www.raspberrypi.org/products/rp2040/) based board 📺 4 | 5 | ## Hardware 6 | 7 | * RP2040 board 8 | * [Raspberry Pi Pico](https://www.raspberrypi.org/products/raspberry-pi-pico/) 9 | * ST7789 TFT Display 10 | * [Adafruit 2.0" 320x240 Color IPS TFT Display with microSD Card Breakout](https://www.adafruit.com/product/4311) 11 | 12 | ### Default Pinout 13 | 14 | ``` 15 | +---------+-------------------+ 16 | | ST7789 | Raspberry Pi Pico | 17 | |---------+-------------------| 18 | | VIN | 3V3 | 19 | |---------+-------------------| 20 | | GND | GND | 21 | |---------+-------------------| 22 | | SCK | GPIO18 | 23 | |---------+-------------------| 24 | | MOSI | GPIO19 | 25 | |---------+-------------------| 26 | | CS | GPIO17 | 27 | |---------+-------------------| 28 | | RST | GPIO21 | 29 | |---------+-------------------| 30 | | D/C | GPIO20 | 31 | +---------+-------------------+ 32 | ``` 33 | 34 | GPIO pins are configurable in examples or API. 35 | 36 | `CS` can be set to `-1` if your display has the CS pin grounded. 37 | 38 | ## Examples 39 | 40 | See [examples](examples/) folder. 41 | 42 | 43 | ## Cloning 44 | 45 | ```sh 46 | git clone https://github.com/ArmDeveloperEcosystem/st7789-library-for-pico.git 47 | ``` 48 | 49 | ## Building 50 | 51 | 1. [Set up the Pico C/C++ SDK](https://datasheets.raspberrypi.org/pico/getting-started-with-pico.pdf) 52 | 2. Set `PICO_SDK_PATH` 53 | ```sh 54 | export PICO_SDK_PATH=/path/to/pico-sdk 55 | ``` 56 | 3. Create `build` dir, run `cmake` and `make`: 57 | ``` 58 | mkdir build 59 | cd build 60 | cmake .. -DPICO_BOARD=pico 61 | make 62 | ``` 63 | 4. Copy example `.uf2` to Pico when in BOOT mode. 64 | 65 | ## License 66 | 67 | [Apache-2.0 License](LICENSE) 68 | 69 | --- 70 | 71 | Disclaimer: This is not an official Arm product. 72 | -------------------------------------------------------------------------------- /examples/st7789_blink/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | 3 | # rest of your project 4 | add_executable(st7789_blink 5 | main.c 6 | ) 7 | 8 | target_link_libraries(st7789_blink PRIVATE pico_st7789) 9 | 10 | # create map/bin/hex/uf2 file in addition to ELF. 11 | pico_add_extra_outputs(st7789_blink) 12 | -------------------------------------------------------------------------------- /examples/st7789_blink/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | */ 7 | 8 | #include "pico/stdlib.h" 9 | #include "pico/st7789.h" 10 | 11 | // lcd configuration 12 | const struct st7789_config lcd_config = { 13 | .spi = PICO_DEFAULT_SPI_INSTANCE, 14 | .gpio_din = PICO_DEFAULT_SPI_TX_PIN, 15 | .gpio_clk = PICO_DEFAULT_SPI_SCK_PIN, 16 | .gpio_cs = PICO_DEFAULT_SPI_CSN_PIN, 17 | .gpio_dc = 20, 18 | .gpio_rst = 21, 19 | .gpio_bl = 22, 20 | }; 21 | 22 | const int lcd_width = 240; 23 | const int lcd_height = 320; 24 | 25 | int main() 26 | { 27 | // initialize the lcd 28 | st7789_init(&lcd_config, lcd_width, lcd_height); 29 | 30 | while (1) { 31 | // make screen black 32 | st7789_fill(0x0000); 33 | 34 | // wait 1 second 35 | sleep_ms(1000); 36 | 37 | // make screen white 38 | st7789_fill(0xffff); 39 | 40 | // wait 1 second 41 | sleep_ms(1000); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/st7789_random/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | 3 | # rest of your project 4 | add_executable(st7789_random 5 | main.c 6 | ) 7 | 8 | target_link_libraries(st7789_random PRIVATE pico_st7789) 9 | 10 | # create map/bin/hex/uf2 file in addition to ELF. 11 | pico_add_extra_outputs(st7789_random) 12 | -------------------------------------------------------------------------------- /examples/st7789_random/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | */ 7 | 8 | #include 9 | 10 | #include "pico/stdlib.h" 11 | #include "pico/st7789.h" 12 | 13 | // lcd configuration 14 | const struct st7789_config lcd_config = { 15 | .spi = PICO_DEFAULT_SPI_INSTANCE, 16 | .gpio_din = PICO_DEFAULT_SPI_TX_PIN, 17 | .gpio_clk = PICO_DEFAULT_SPI_SCK_PIN, 18 | .gpio_cs = PICO_DEFAULT_SPI_CSN_PIN, 19 | .gpio_dc = 20, 20 | .gpio_rst = 21, 21 | .gpio_bl = 22, 22 | }; 23 | 24 | const int LCD_WIDTH = 240; 25 | const int LCD_HEIGHT = 320; 26 | 27 | int main() 28 | { 29 | // initialize the lcd 30 | st7789_init(&lcd_config, LCD_WIDTH, LCD_HEIGHT); 31 | 32 | // make screen black 33 | st7789_fill(0x0000); 34 | 35 | while (1) { 36 | // create a random x, y, and color value 37 | int rand_x = rand() % LCD_WIDTH; 38 | int rand_y = rand() % LCD_HEIGHT; 39 | uint16_t rand_color = rand() % 0xffff; 40 | 41 | // move the cursor to the random x and y position 42 | st7789_set_cursor(rand_x, rand_y); 43 | 44 | // put the random color as the pixel value 45 | st7789_put(rand_color); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /examples/st7789_random_rows/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | 3 | # rest of your project 4 | add_executable(st7789_random_rows 5 | main.c 6 | ) 7 | 8 | target_link_libraries(st7789_random_rows PRIVATE pico_st7789) 9 | 10 | # create map/bin/hex/uf2 file in addition to ELF. 11 | pico_add_extra_outputs(st7789_random_rows) 12 | -------------------------------------------------------------------------------- /examples/st7789_random_rows/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | */ 7 | 8 | #include 9 | 10 | #include "pico/stdlib.h" 11 | #include "pico/st7789.h" 12 | 13 | // lcd configuration 14 | const struct st7789_config lcd_config = { 15 | .spi = PICO_DEFAULT_SPI_INSTANCE, 16 | .gpio_din = PICO_DEFAULT_SPI_TX_PIN, 17 | .gpio_clk = PICO_DEFAULT_SPI_SCK_PIN, 18 | .gpio_cs = PICO_DEFAULT_SPI_CSN_PIN, 19 | .gpio_dc = 20, 20 | .gpio_rst = 21, 21 | .gpio_bl = 22, 22 | }; 23 | 24 | #define LCD_WIDTH 240 25 | #define LCD_HEIGHT 320 26 | 27 | uint16_t row_pixels[LCD_WIDTH]; 28 | 29 | int main() 30 | { 31 | // initialize the lcd 32 | st7789_init(&lcd_config, LCD_WIDTH, LCD_HEIGHT); 33 | 34 | // make screen black 35 | st7789_fill(0x0000); 36 | 37 | while (1) { 38 | // create a random y and color value 39 | int rand_y = rand() % LCD_HEIGHT; 40 | uint16_t rand_color = rand() % 0xffff; 41 | 42 | // move the cursor to the y position 43 | st7789_set_cursor(0, rand_y); 44 | 45 | // set the row pixels buffer to the color value 46 | for (int i = 0; i < LCD_WIDTH; i++) { 47 | row_pixels[i] = rand_color; 48 | } 49 | 50 | // write the row pixel values 51 | st7789_write(row_pixels, sizeof(row_pixels)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/include/pico/st7789.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | */ 7 | 8 | #ifndef _PICO_ST7789_H_ 9 | #define _PICO_ST7789_H_ 10 | 11 | #include "hardware/spi.h" 12 | 13 | struct st7789_config { 14 | spi_inst_t* spi; 15 | uint gpio_din; 16 | uint gpio_clk; 17 | int gpio_cs; 18 | uint gpio_dc; 19 | uint gpio_rst; 20 | uint gpio_bl; 21 | }; 22 | 23 | void st7789_init(const struct st7789_config* config, uint16_t width, uint16_t height); 24 | void st7789_write(const void* data, size_t len); 25 | void st7789_put(uint16_t pixel); 26 | void st7789_fill(uint16_t pixel); 27 | void st7789_set_cursor(uint16_t x, uint16_t y); 28 | void st7789_vertical_scroll(uint16_t row); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/st7789.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | */ 7 | 8 | #include 9 | 10 | #include "hardware/gpio.h" 11 | 12 | #include "pico/st7789.h" 13 | 14 | static struct st7789_config st7789_cfg; 15 | static uint16_t st7789_width; 16 | static uint16_t st7789_height; 17 | static bool st7789_data_mode = false; 18 | 19 | static void st7789_cmd(uint8_t cmd, const uint8_t* data, size_t len) 20 | { 21 | if (st7789_cfg.gpio_cs > -1) { 22 | spi_set_format(st7789_cfg.spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); 23 | } else { 24 | spi_set_format(st7789_cfg.spi, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); 25 | } 26 | st7789_data_mode = false; 27 | 28 | sleep_us(1); 29 | if (st7789_cfg.gpio_cs > -1) { 30 | gpio_put(st7789_cfg.gpio_cs, 0); 31 | } 32 | gpio_put(st7789_cfg.gpio_dc, 0); 33 | sleep_us(1); 34 | 35 | spi_write_blocking(st7789_cfg.spi, &cmd, sizeof(cmd)); 36 | 37 | if (len) { 38 | sleep_us(1); 39 | gpio_put(st7789_cfg.gpio_dc, 1); 40 | sleep_us(1); 41 | 42 | spi_write_blocking(st7789_cfg.spi, data, len); 43 | } 44 | 45 | sleep_us(1); 46 | if (st7789_cfg.gpio_cs > -1) { 47 | gpio_put(st7789_cfg.gpio_cs, 1); 48 | } 49 | gpio_put(st7789_cfg.gpio_dc, 1); 50 | sleep_us(1); 51 | } 52 | 53 | void st7789_caset(uint16_t xs, uint16_t xe) 54 | { 55 | uint8_t data[] = { 56 | xs >> 8, 57 | xs & 0xff, 58 | xe >> 8, 59 | xe & 0xff, 60 | }; 61 | 62 | // CASET (2Ah): Column Address Set 63 | st7789_cmd(0x2a, data, sizeof(data)); 64 | } 65 | 66 | void st7789_raset(uint16_t ys, uint16_t ye) 67 | { 68 | uint8_t data[] = { 69 | ys >> 8, 70 | ys & 0xff, 71 | ye >> 8, 72 | ye & 0xff, 73 | }; 74 | 75 | // RASET (2Bh): Row Address Set 76 | st7789_cmd(0x2b, data, sizeof(data)); 77 | } 78 | 79 | void st7789_init(const struct st7789_config* config, uint16_t width, uint16_t height) 80 | { 81 | memcpy(&st7789_cfg, config, sizeof(st7789_cfg)); 82 | st7789_width = width; 83 | st7789_height = height; 84 | 85 | spi_init(st7789_cfg.spi, 125 * 1000 * 1000); 86 | if (st7789_cfg.gpio_cs > -1) { 87 | spi_set_format(st7789_cfg.spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); 88 | } else { 89 | spi_set_format(st7789_cfg.spi, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); 90 | } 91 | 92 | gpio_set_function(st7789_cfg.gpio_din, GPIO_FUNC_SPI); 93 | gpio_set_function(st7789_cfg.gpio_clk, GPIO_FUNC_SPI); 94 | 95 | if (st7789_cfg.gpio_cs > -1) { 96 | gpio_init(st7789_cfg.gpio_cs); 97 | } 98 | gpio_init(st7789_cfg.gpio_dc); 99 | gpio_init(st7789_cfg.gpio_rst); 100 | gpio_init(st7789_cfg.gpio_bl); 101 | 102 | if (st7789_cfg.gpio_cs > -1) { 103 | gpio_set_dir(st7789_cfg.gpio_cs, GPIO_OUT); 104 | } 105 | gpio_set_dir(st7789_cfg.gpio_dc, GPIO_OUT); 106 | gpio_set_dir(st7789_cfg.gpio_rst, GPIO_OUT); 107 | gpio_set_dir(st7789_cfg.gpio_bl, GPIO_OUT); 108 | 109 | if (st7789_cfg.gpio_cs > -1) { 110 | gpio_put(st7789_cfg.gpio_cs, 1); 111 | } 112 | gpio_put(st7789_cfg.gpio_dc, 1); 113 | gpio_put(st7789_cfg.gpio_rst, 1); 114 | sleep_ms(100); 115 | 116 | // SWRESET (01h): Software Reset 117 | st7789_cmd(0x01, NULL, 0); 118 | sleep_ms(150); 119 | 120 | // SLPOUT (11h): Sleep Out 121 | st7789_cmd(0x11, NULL, 0); 122 | sleep_ms(50); 123 | 124 | // COLMOD (3Ah): Interface Pixel Format 125 | // - RGB interface color format = 65K of RGB interface 126 | // - Control interface color format = 16bit/pixel 127 | st7789_cmd(0x3a, (uint8_t[]){ 0x55 }, 1); 128 | sleep_ms(10); 129 | 130 | // MADCTL (36h): Memory Data Access Control 131 | // - Page Address Order = Top to Bottom 132 | // - Column Address Order = Left to Right 133 | // - Page/Column Order = Normal Mode 134 | // - Line Address Order = LCD Refresh Top to Bottom 135 | // - RGB/BGR Order = RGB 136 | // - Display Data Latch Data Order = LCD Refresh Left to Right 137 | st7789_cmd(0x36, (uint8_t[]){ 0x00 }, 1); 138 | 139 | st7789_caset(0, width); 140 | st7789_raset(0, height); 141 | 142 | // INVON (21h): Display Inversion On 143 | st7789_cmd(0x21, NULL, 0); 144 | sleep_ms(10); 145 | 146 | // NORON (13h): Normal Display Mode On 147 | st7789_cmd(0x13, NULL, 0); 148 | sleep_ms(10); 149 | 150 | // DISPON (29h): Display On 151 | st7789_cmd(0x29, NULL, 0); 152 | sleep_ms(10); 153 | 154 | gpio_put(st7789_cfg.gpio_bl, 1); 155 | } 156 | 157 | void st7789_ramwr() 158 | { 159 | sleep_us(1); 160 | if (st7789_cfg.gpio_cs > -1) { 161 | gpio_put(st7789_cfg.gpio_cs, 0); 162 | } 163 | gpio_put(st7789_cfg.gpio_dc, 0); 164 | sleep_us(1); 165 | 166 | // RAMWR (2Ch): Memory Write 167 | uint8_t cmd = 0x2c; 168 | spi_write_blocking(st7789_cfg.spi, &cmd, sizeof(cmd)); 169 | 170 | sleep_us(1); 171 | if (st7789_cfg.gpio_cs > -1) { 172 | gpio_put(st7789_cfg.gpio_cs, 0); 173 | } 174 | gpio_put(st7789_cfg.gpio_dc, 1); 175 | sleep_us(1); 176 | } 177 | 178 | void st7789_write(const void* data, size_t len) 179 | { 180 | if (!st7789_data_mode) { 181 | st7789_ramwr(); 182 | 183 | if (st7789_cfg.gpio_cs > -1) { 184 | spi_set_format(st7789_cfg.spi, 16, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); 185 | } else { 186 | spi_set_format(st7789_cfg.spi, 16, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); 187 | } 188 | 189 | st7789_data_mode = true; 190 | } 191 | 192 | spi_write16_blocking(st7789_cfg.spi, data, len / 2); 193 | } 194 | 195 | void st7789_put(uint16_t pixel) 196 | { 197 | st7789_write(&pixel, sizeof(pixel)); 198 | } 199 | 200 | void st7789_fill(uint16_t pixel) 201 | { 202 | int num_pixels = st7789_width * st7789_height; 203 | 204 | st7789_set_cursor(0, 0); 205 | 206 | for (int i = 0; i < num_pixels; i++) { 207 | st7789_put(pixel); 208 | } 209 | } 210 | 211 | void st7789_set_cursor(uint16_t x, uint16_t y) 212 | { 213 | st7789_caset(x, st7789_width); 214 | st7789_raset(y, st7789_height); 215 | } 216 | 217 | void st7789_vertical_scroll(uint16_t row) 218 | { 219 | uint8_t data[] = { 220 | (row >> 8) & 0xff, 221 | row & 0x00ff 222 | }; 223 | 224 | // VSCSAD (37h): Vertical Scroll Start Address of RAM 225 | st7789_cmd(0x37, data, sizeof(data)); 226 | } 227 | --------------------------------------------------------------------------------