main.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-14 Jonas first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_gpio.h"
  14. /* defined the LED pin: PB4 */
  15. #define LED_PIN GET_PIN(B, 4)
  16. /* defined the KEY1 pin: PA1 */
  17. #define KEY1_PIN GET_PIN(A, 1)
  18. /* defined the KEY WakeUp pin: PA0 */
  19. #define WKUP_PIN GET_PIN(A, 0)
  20. void key_pressed_cb(void *arg)
  21. {
  22. if (*(rt_base_t *)arg == KEY1_PIN)
  23. {
  24. rt_kprintf("key1 pressed\n");
  25. }
  26. if (*(rt_base_t *)arg == WKUP_PIN)
  27. {
  28. rt_kprintf("key wake up pressed\n");
  29. }
  30. }
  31. int main(void)
  32. {
  33. static rt_base_t key1 = KEY1_PIN;
  34. static rt_base_t key_wkup = WKUP_PIN;
  35. uint32_t speed = 500;
  36. /* set LED5 pin mode to output */
  37. rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  38. /* set KEY pin mode to input */
  39. rt_pin_mode(KEY1_PIN, PIN_MODE_INPUT_PULLUP);
  40. rt_pin_attach_irq(KEY1_PIN, PIN_IRQ_MODE_FALLING, key_pressed_cb, &key1);
  41. rt_pin_irq_enable(KEY1_PIN, PIN_IRQ_ENABLE);
  42. rt_pin_mode(WKUP_PIN, PIN_MODE_INPUT_PULLDOWN);
  43. rt_pin_attach_irq(WKUP_PIN, PIN_IRQ_MODE_RISING, key_pressed_cb, &key_wkup);
  44. rt_pin_irq_enable(WKUP_PIN, PIN_IRQ_ENABLE);
  45. while (1)
  46. {
  47. rt_pin_write(LED_PIN, PIN_LOW);
  48. rt_thread_mdelay(speed);
  49. rt_pin_write(LED_PIN, PIN_HIGH);
  50. rt_thread_mdelay(speed);
  51. }
  52. }