application.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <rtthread.h>
  2. #include <rthw.h>
  3. #include "board.h"
  4. #include "port.h"
  5. #include "extint.h"
  6. #include "sleep_timer.h"
  7. void LED_Init(void)
  8. {
  9. struct port_config config_port_pin;
  10. port_get_config_defaults(&config_port_pin);
  11. config_port_pin.direction = PORT_PIN_DIR_INPUT;
  12. config_port_pin.input_pull = PORT_PIN_PULL_UP;
  13. port_pin_set_config(PIN_PA15, &config_port_pin);
  14. config_port_pin.direction = PORT_PIN_DIR_OUTPUT;
  15. port_pin_set_config(PIN_PB30, &config_port_pin);
  16. }
  17. void LED_ON(void)
  18. {
  19. port_pin_set_output_level(PIN_PB30, false);
  20. }
  21. void LED_OFF(void)
  22. {
  23. port_pin_set_output_level(PIN_PB30, true);
  24. }
  25. void extint_detection_callback(void);
  26. void configure_extint_channel(void)
  27. {
  28. //! [setup_1]
  29. struct extint_chan_conf config_extint_chan;
  30. //! [setup_1]
  31. //! [setup_2]
  32. extint_chan_get_config_defaults(&config_extint_chan);
  33. //! [setup_2]
  34. //! [setup_3]
  35. config_extint_chan.gpio_pin = PIN_PA15A_EIC_EXTINT15;
  36. config_extint_chan.gpio_pin_mux = MUX_PA15A_EIC_EXTINT15;
  37. config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
  38. config_extint_chan.detection_criteria = EXTINT_DETECT_BOTH;
  39. //! [setup_3]
  40. //! [setup_4]
  41. extint_chan_set_config(15, &config_extint_chan);
  42. //! [setup_4]
  43. }
  44. void configure_extint_callbacks(void)
  45. {
  46. //! [setup_5]
  47. extint_register_callback(extint_detection_callback, 15, EXTINT_CALLBACK_TYPE_DETECT);
  48. //! [setup_5]
  49. //! [setup_6]
  50. extint_chan_enable_callback(15, EXTINT_CALLBACK_TYPE_DETECT);
  51. //! [setup_6]
  52. }
  53. //! [setup_7]
  54. void extint_detection_callback(void)
  55. {
  56. bool pin_state = port_pin_get_input_level(PIN_PA15);
  57. port_pin_set_output_level(PIN_PB30, pin_state);
  58. }
  59. static struct rt_semaphore _rx_sem;
  60. static rt_err_t _rx_ind(rt_device_t dev, rt_size_t size)
  61. {
  62. return rt_sem_release(&_rx_sem);
  63. }
  64. void rt_init_thread_entry(void* parameter)
  65. {
  66. rt_thread_t thread;
  67. rt_device_t dev;
  68. rt_kprintf("SYSTEM running at %uhz\n", SystemCoreClock);
  69. #ifdef RT_USING_FINSH
  70. /* init finsh */
  71. finsh_system_init();
  72. #if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  73. finsh_set_device("uart1");
  74. #endif
  75. #endif
  76. LED_Init();
  77. configure_extint_channel();
  78. configure_extint_callbacks();
  79. sleep_timer_init();
  80. // sleep_timer_start(1500);
  81. while (1)
  82. {
  83. rt_kprintf("init thread running tick:%u\n", rt_tick_get());
  84. rt_thread_delay(2*RT_TICK_PER_SECOND);
  85. }
  86. }
  87. int rt_application_init(void)
  88. {
  89. rt_thread_t tid;
  90. tid = rt_thread_create("init", rt_init_thread_entry, RT_NULL,
  91. 512, RT_THREAD_PRIORITY_MAX / 3, 20);
  92. if (tid != RT_NULL)
  93. rt_thread_startup(tid);
  94. return 0;
  95. }