aboutsummaryrefslogtreecommitdiffstats
path: root/docs/internals_gpio_control.md
blob: 21643d30c96187ec531355ef29ebd3ea42c0dc73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# GPIO Control

QMK has a GPIO control abstraction layer which is micro-controller agnostic. This is done to allow easy access to pin control across different platforms.

## Functions

The following functions can provide basic control of GPIOs and are found in `quantum/quantum.h`.

|Function              |Description                                                       |
|----------------------|------------------------------------------------------------------|
|`setPinInput(pin)`    |Set pin as input with high impedance (High-Z)                     |
|`setPinInputHigh(pin)`|Set pin as input with build in pull-up                            |
|`setPinInputLow(pin)` |Set pin as input with build in pull-down (Supported only on STM32)|
|`setPinOutput(pin)`   |Set pin as output                                                 |
|`writePinHige(pin)`   |Set pin level as high, assuming it is an output                   |
|`writePinLow(pin)`    |Set pin level as low, assuming it is an output                    |
|`writePin(pin, level)`|Set pin level, assuming it is an output                           |
|`readPin(pin)`        |Returns the level of the pin                                      |

## Advance settings

Each micro-controller can have multiple advance settings regarding its GPIO. This abstraction layer does not limit the use of architecture specific functions. Advance users should consult the datasheet of there desired device and include any needed libraries. For AVR the standard avr/io.h library is used and for STM32 the Chibios [PAL library](http://chibios.sourceforge.net/docs3/hal/group___p_a_l.html) is used.
copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "ch.h" #include "hal.h" #include "rt_test_root.h" #include "oslib_test_root.h" /* * Green LED blinker thread, times are in milliseconds. */ static THD_WORKING_AREA(waThread1, 128); static THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { palClearPad(GPIOA, GPIOA_LED_GREEN); chThdSleepMilliseconds(500); palSetPad(GPIOA, GPIOA_LED_GREEN); chThdSleepMilliseconds(500); } } /* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Activates the serial driver 2 using the driver default configuration. */ sdStart(&SD2, NULL); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state. */ while (true) { if (!palReadPad(GPIOC, GPIOC_BUTTON)) { test_execute((BaseSequentialStream *)&SD2, &rt_test_suite); test_execute((BaseSequentialStream *)&SD2, &oslib_test_suite); } chThdSleepMilliseconds(500); } }