# RT-Thread GPIO [![HackMD Version](https://img.shields.io/badge/Made%20with-Markdown-1f425.svg)](https://hackmd.io/@ATKTC/S1vO3eXco) Author : Christpher Kwan (ktckwan@connect.ust.hk) ## What is GPIO ? * The link below is about some concepts related to GPIO [![HackMD Version](https://img.shields.io/badge/Made%20with-Markdown-1f425.svg)](https://hackmd.io/@ATKTC/BygTkzV0yo) ## How to configure GPIO ? ### Useful links * **Board Schematic**: * old domain : https://gitlab.hkustracing.com/chleeay/internal_board * new domain (use this): https://smartcargl.ddns.net/chleeay/internal_board * **Core Board**: https://drive.google.com/file/d/1W4W5Dwfp7e8sE6FGIGpkS1ixEExaXQ-G/view?usp=sharing ### Important steps 1. First, find an *unused* pin from the **board schematic** * ![](https://i.imgur.com/YnSDgzX.png) * Let says I choose `P2_FlexSPI_CLK_B` number 15 in this example 2. Second, check the pin name to get the corresponding GPIO port and pins from **Core Board** (start from p.16) * ![](https://i.imgur.com/YaO8pIN.png) * Since I choose `P2_FlexSPI_CLK_B` number 15, the correspnding GPIO will be `GPIO3_IO4` * Then, it will be GPIO port3 and pin4 3. Third, define a Marco the GPIO ```c /* GPIO3_IO04 */ #define IR_SENSOR_PIN1 GET_PIN(3,4) ``` 4. Next, set the `pinMode` ```c /* set IR_SENSOR_PIN1 pin mode to input */ rt_pin_mode(IR_SENSOR_PIN1, PIN_MODE_INPUT); ``` * Check out `pin.h` for the second paramter of `rt_pin_mode` ```c /* pin.h */ #define PIN_LOW 0x00 #define PIN_HIGH 0x01 #define PIN_MODE_OUTPUT 0x00 #define PIN_MODE_INPUT 0x01 #define PIN_MODE_INPUT_PULLUP 0x02 #define PIN_MODE_INPUT_PULLDOWN 0x03 #define PIN_MODE_OUTPUT_OD 0x04 ``` * Choose the right one according to the hardware design` ## How to write or set GPIO ? ### Read GPIO ```c int sensor1 = rt_pin_read(IR_SENSOR_PIN1); ``` * return values: * `PIN_LOW` or `PIN_HIGH` * [API](https://www.rt-thread.io/document/site/programming-manual/device/pin/pin/#read-pin-level) ### Set GPIO * `rt_pin_write(LED0_PIN, PIN_HIGH);` * [API](https://www.rt-thread.io/document/site/programming-manual/device/pin/pin/#set-the-pin-level) ## Examples ### Read GPIO ```c #include <rtdevice.h> #include <rtthread.h> #include "drv_gpio.h" /* GPIO3_IO04 */ #define IR_SENSOR_PIN1 GET_PIN(3,4) int main(void) { /* set IR_SENSOR_PIN1 pin mode to input */ rt_pin_mode(IR_SENSOR_PIN1, PIN_MODE_INPUT); rt_uint8_t sensor1 = rt_pin_read(IR_SENSOR_PIN1); char *message = (sensor1 == PIN_HIGH) ? "HIGH" : "LOW"; rt_kprintf("IR Sensor reads %s.\n", message); } ``` ### Set GPIO for on-board LED * default example ```c #include <rtdevice.h> #include <rtthread.h> #include "drv_gpio.h" /* GPIO1_IO09 */ #define LED0_PIN GET_PIN(1,9) int main(void) { /* set LED0 pin mode to output */ rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); while (1) { rt_pin_write(LED0_PIN, PIN_HIGH); rt_thread_mdelay(500); rt_pin_write(LED0_PIN, PIN_LOW); rt_thread_mdelay(500); } } ``` ## Further reading: * https://www.rt-thread.io/document/site/programming-manual/device/pin/pin/#read-pin-level