main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. struct can_instance {
  16. CANDriver *canp;
  17. ioline_t led;
  18. };
  19. static const struct can_instance can1 = {&CAND1, LINE_LED_GREEN};
  20. /*
  21. * Internal loopback mode, 500KBaud, automatic wakeup, automatic recover
  22. * from abort mode.
  23. * See section 42.7.7 on the STM32 reference manual.
  24. */
  25. static const CANConfig cancfg = {
  26. CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
  27. CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
  28. CAN_BTR_TS1(12) | CAN_BTR_BRP(9)
  29. };
  30. /*
  31. * Receiver thread.
  32. */
  33. static THD_WORKING_AREA(can_rx1_wa, 256);
  34. static THD_FUNCTION(can_rx, p) {
  35. struct can_instance *cip = p;
  36. event_listener_t el;
  37. CANRxFrame rxmsg;
  38. (void)p;
  39. chRegSetThreadName("receiver");
  40. chEvtRegister(&cip->canp->rxfull_event, &el, 0);
  41. while(!chThdShouldTerminateX()) {
  42. if (chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(100)) == 0)
  43. continue;
  44. while (canReceive(cip->canp, CAN_ANY_MAILBOX,
  45. &rxmsg, TIME_IMMEDIATE) == MSG_OK) {
  46. /* Process message.*/
  47. palToggleLine(cip->led);
  48. }
  49. }
  50. chEvtUnregister(&CAND1.rxfull_event, &el);
  51. }
  52. /*
  53. * Transmitter thread.
  54. */
  55. static THD_WORKING_AREA(can_tx_wa, 256);
  56. static THD_FUNCTION(can_tx, p) {
  57. CANTxFrame txmsg;
  58. (void)p;
  59. chRegSetThreadName("transmitter");
  60. txmsg.IDE = CAN_IDE_EXT;
  61. txmsg.EID = 0x01234567;
  62. txmsg.RTR = CAN_RTR_DATA;
  63. txmsg.DLC = 8;
  64. txmsg.data32[0] = 0x55AA55AA;
  65. txmsg.data32[1] = 0x00FF00FF;
  66. while (!chThdShouldTerminateX()) {
  67. canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
  68. chThdSleepMilliseconds(500);
  69. }
  70. }
  71. /*
  72. * Application entry point.
  73. */
  74. int main(void) {
  75. /*
  76. * System initializations.
  77. * - HAL initialization, this also initializes the configured device drivers
  78. * and performs the board-specific initializations.
  79. * - Kernel initialization, the main() function becomes a thread and the
  80. * RTOS is active.
  81. */
  82. halInit();
  83. chSysInit();
  84. /*
  85. * LED line as output.
  86. */
  87. palSetLineMode(LINE_LED_GREEN, PAL_MODE_OUTPUT_PUSHPULL);
  88. /*
  89. * Activates the CAN drivers 1 and 2.
  90. */
  91. canStart(&CAND1, &cancfg);
  92. /*
  93. * Starting the transmitter and receiver threads.
  94. */
  95. chThdCreateStatic(can_rx1_wa, sizeof(can_rx1_wa), NORMALPRIO + 7,
  96. can_rx, (void *)&can1);
  97. chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7,
  98. can_tx, NULL);
  99. /*
  100. * Normal main() thread activity, in this demo it does nothing.
  101. */
  102. while (true) {
  103. chThdSleepMilliseconds(500);
  104. }
  105. return 0;
  106. }