{"id":19999162,"url":"https://github.com/mr-addict/stm32-hal-ps2-library","last_synced_at":"2025-09-11T02:35:19.647Z","repository":{"id":107625317,"uuid":"484648196","full_name":"MR-Addict/STM32-HAL-PS2-Library","owner":"MR-Addict","description":null,"archived":false,"fork":false,"pushed_at":"2022-05-02T04:11:17.000Z","size":470,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-13T05:11:57.569Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MR-Addict.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-04-23T04:43:01.000Z","updated_at":"2024-11-11T17:50:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"7e71593d-1076-4d4b-91e6-557ab7f88872","html_url":"https://github.com/MR-Addict/STM32-HAL-PS2-Library","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MR-Addict%2FSTM32-HAL-PS2-Library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MR-Addict%2FSTM32-HAL-PS2-Library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MR-Addict%2FSTM32-HAL-PS2-Library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MR-Addict%2FSTM32-HAL-PS2-Library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MR-Addict","download_url":"https://codeload.github.com/MR-Addict/STM32-HAL-PS2-Library/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233606414,"owners_count":18701614,"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":[],"created_at":"2024-11-13T05:10:52.134Z","updated_at":"2025-01-12T12:46:32.100Z","avatar_url":"https://github.com/MR-Addict.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# STM32 HAL PS2 Library\n\nThis library is written for STM32 chips based on HAL libary.\n\n## 1. Prepartions\n\n### 1.1 CubeMX Configuration\n\nYou need to use CubeMX or CubeIDE to configurate some Hardware preparations.\n\n#### 1.1.1 PS2 Pin Configuration\n\nActually, PS2 use SPI protocol, but here I decided to write my custom SPI protocol to comunicate with PS2. The biggest reason is that PS2 SPI communication speed is too slow, less than **250KHz**.\n\nSo, you need to configuration four GPIOs for SPI communication, which are `MOSI,MISO,SCK,SS`. You can use any of STM32 GPIOs for these pins, their init status and name should be the same as below:\n\n- MOSI -\u003e GPIO_OUTPUT, HIGH\n- MISO -\u003e GPIO_INPUT\n- SCK -\u003e GPIO_OUTPUT, HIGH\n- SS -\u003e GPIO_OUTPUT, HIGH\n\n![SPI-Port](Images/SPI.png)\n\n#### 1.1.2 Timer Configuraion\n\nSecond, I use a timer to create a **delay_us** function. When you configurate timer, make sure that your timer is 1us per clock tick. For example, my system clock is **72MHz**, so my timer prescable should be **71**.\n\n![Timer](Images/Timer.png)\n\n### 1.1.3 LED Configuration\n\nI use PC13 on-board LED to test my PS2 library, you can also configurate one for testing.\n\n![LED](Images/LED.png)\n\n### 1.2 Library Configuration\n\nCopy my PS2 Libary to your projects source folder `Core/Inc` like below\n\n![Include](Images/Inlcude.png)\n\n## 2. Use PS2 Library\n\nAfter you finish configurating hardware and copying PS2 library in your project, now you can use it in your project.\n\nFirst, you need to include PS2 library in your main.c file\n\n```c\n#include \"PS2/PS2.h\"\n```\n\nThen you need to create a PS2 Object for easy usage, you can read any button status through PS2.\\\u003cBUTTONNAME\\\u003e, like PS2.UP\n\n```c\nPS2Buttons PS2;\n```\n\nAfter this, you need to pass these argumemts to PS2_Init function\n\n```c\nPS2_Init(\u0026htim1, \u0026PS2);\n```\n\nIn your loop, you need to call PS2_Update every time you want to update PS2 Buttons, and you can use these buttons to finish your jobs.\n\n```c\nwhile (1) {\n  PS2_Update();\n  if (PS2.UP) {\n    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);\n  } else if (PS2.DOWN) {\n    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);\n  }\n  HAL_Delay(2);\n}\n```\n\nBelow is 20 buttons you can use in PS2Buttons object, note that last four buttons are analog joystick value which varies from **0~255**, and other 16 buttons are just **0** and **1**.\n\n```c\ntypedef struct {\n  uint8_t SELECT;\n  uint8_t L3;\n  uint8_t R3;\n  uint8_t START;\n  uint8_t UP;\n  uint8_t RIGHT;\n  uint8_t DOWN;\n  uint8_t LEFT;\n  uint8_t L2;\n  uint8_t R2;\n  uint8_t L1;\n  uint8_t R1;\n  uint8_t TRIANGLE;\n  uint8_t CIRCLE;\n  uint8_t CROSS;\n  uint8_t SQUARE;\n  uint8_t RX;\n  uint8_t RY;\n  uint8_t LX;\n  uint8_t LY;\n} PS2Buttons;\n```\n\n## 3. Reference\n\nWhen I write these PS2 Library, I searched a lot online. Below two articals are most usful I use for write this. You can read these two articals if you want to write yourself.\n\n[Interfacing a PS2 (PlayStation 2) Controller.](https://store.curiousinventor.com/guides/PS2)\n\n[Playstation 2 (Dual Shock) controller protocol notes.](https://gist.github.com/scanlime/5042071)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmr-addict%2Fstm32-hal-ps2-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmr-addict%2Fstm32-hal-ps2-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmr-addict%2Fstm32-hal-ps2-library/lists"}