main.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. ChibiOS - Copyright (C) 2016 Theodore Ateba
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "chprintf.h"
  16. /**
  17. * @brief Global variables.
  18. */
  19. BaseSequentialStream* chp = (BaseSequentialStream*) &SD1;
  20. static int isrCpt; /**< Interrupt counter. */
  21. /**
  22. * @brief This is the External Interruption 4 callback.
  23. * The onboard LED is toggled every time an interruption is
  24. * detected. A varible is also incremented.
  25. */
  26. static void extcb(EXTDriver *extp, expchannel_t channel) {
  27. (void)extp;
  28. (void)channel;
  29. chSysLockFromISR();
  30. palTogglePad(IOPORT2, PORTB_LED1);
  31. isrCpt++;
  32. chSysUnlockFromISR();
  33. }
  34. /**
  35. * @brief EXT Driver configurations.
  36. */
  37. static const EXTConfig extcfg = {
  38. {
  39. {EXT_CH_MODE_DISABLED , NULL}, /* INT0 Config. */
  40. {EXT_CH_MODE_DISABLED , NULL}, /* INT1 Config. */
  41. {EXT_CH_MODE_DISABLED , NULL}, /* INT2 Config. */
  42. {EXT_CH_MODE_DISABLED , NULL}, /* INT3 Config. */
  43. {EXT_CH_MODE_RISING_EDGE , extcb}, /* INT4 Config. */
  44. {EXT_CH_MODE_DISABLED , NULL}, /* INT5 Config. */
  45. }
  46. };
  47. /**
  48. * Application entry point.
  49. */
  50. int main(void) {
  51. /*
  52. * System initializations.
  53. * - HAL initialization, this also initializes the configured device drivers
  54. * and performs the board-specific initializations.
  55. * - Kernel initialization, the main() function becomes a thread and the
  56. * RTOS is active.
  57. */
  58. halInit();
  59. chSysInit();
  60. /*
  61. * External interrupt on the Pin D2 of the Arduino Mega2560.
  62. */
  63. palSetPadMode(IOPORT5, PE4, PAL_MODE_INPUT); /* INT4 pin. */
  64. /*
  65. * Start the serial driver.
  66. */
  67. sdStart(&SD1, NULL);
  68. /*
  69. * Active the EXT driver 1 and select INT channel 4.
  70. */
  71. extStart(&EXTD1, &extcfg);
  72. extChannelEnable(&EXTD1, INT4);
  73. palClearPad(IOPORT2, PORTB_LED1);
  74. chprintf(chp, "\n\r EXT hal test started...");
  75. /*
  76. * Normal main() thread activity, in this demo it print IRQ counter every
  77. * seconde.
  78. */
  79. while (TRUE) {
  80. chThdSleepMilliseconds(1000);
  81. chprintf(chp, "\n\r External Interrupt counter: %d", isrCpt);
  82. }
  83. }