main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  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. static virtual_timer_t vt1, vt2;
  16. static void restart(void *p) {
  17. (void)p;
  18. chSysLockFromISR();
  19. uartStartSendI(&UARTD2, 14, "Hello World!\r\n");
  20. chSysUnlockFromISR();
  21. }
  22. static void ledoff(void *p) {
  23. (void)p;
  24. palSetPad(GPIOC, GPIOC_LED1);
  25. }
  26. /*
  27. * This callback is invoked when a transmission buffer has been completely
  28. * read by the driver.
  29. */
  30. static void txend1(UARTDriver *uartp) {
  31. (void)uartp;
  32. palSetPad(GPIOC, GPIOC_LED1);
  33. }
  34. /*
  35. * This callback is invoked when a transmission has physically completed.
  36. */
  37. static void txend2(UARTDriver *uartp) {
  38. (void)uartp;
  39. palSetPad(GPIOC, GPIOC_LED1);
  40. chSysLockFromISR();
  41. chVTResetI(&vt1);
  42. chVTDoSetI(&vt1, TIME_MS2I(5000), restart, NULL);
  43. chSysUnlockFromISR();
  44. }
  45. /*
  46. * This callback is invoked on a receive error, the errors mask is passed
  47. * as parameter.
  48. */
  49. static void rxerr(UARTDriver *uartp, uartflags_t e) {
  50. (void)uartp;
  51. (void)e;
  52. }
  53. /*
  54. * This callback is invoked when a character is received but the application
  55. * was not ready to receive it, the character is passed as parameter.
  56. */
  57. static void rxchar(UARTDriver *uartp, uint16_t c) {
  58. (void)uartp;
  59. (void)c;
  60. /* Flashing the LED each time a character is received.*/
  61. palClearPad(GPIOC, GPIOC_LED1);
  62. chSysLockFromISR();
  63. chVTResetI(&vt2);
  64. chVTDoSetI(&vt2, TIME_MS2I(200), ledoff, NULL);
  65. chSysUnlockFromISR();
  66. }
  67. /*
  68. * This callback is invoked when a receive buffer has been completely written.
  69. */
  70. static void rxend(UARTDriver *uartp) {
  71. (void)uartp;
  72. }
  73. /*
  74. * This callback is invoked when configured timeout reached.
  75. */
  76. static void rxtimeout(UARTDriver *uartp) {
  77. (void)uartp;
  78. }
  79. /*
  80. * UART driver configuration structure.
  81. */
  82. static UARTConfig uart_cfg_1 = {
  83. txend1,
  84. txend2,
  85. rxend,
  86. rxchar,
  87. rxerr,
  88. rxtimeout,
  89. 0,
  90. 38400,
  91. 0,
  92. USART_CR2_LINEN,
  93. 0
  94. };
  95. /*
  96. * Application entry point.
  97. */
  98. int main(void) {
  99. /*
  100. * System initializations.
  101. * - HAL initialization, this also initializes the configured device drivers
  102. * and performs the board-specific initializations.
  103. * - Kernel initialization, the main() function becomes a thread and the
  104. * RTOS is active.
  105. */
  106. halInit();
  107. chSysInit();
  108. /*
  109. * Activates the serial driver 1, PA9 and PA10 are routed to USART2.
  110. */
  111. uartStart(&UARTD2, &uart_cfg_1);
  112. /*
  113. * Starts the transmission, it will be handled entirely in background.
  114. */
  115. uartStartSend(&UARTD2, 13, "Starting...\r\n");
  116. /*
  117. * Normal main() thread activity, in this demo it does nothing.
  118. */
  119. while (true) {
  120. chThdSleepMilliseconds(500);
  121. }
  122. }