PageRenderTime 127ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/testhal/STM32F4xx/CAN/main.c

https://bitbucket.org/doudoudou/incas
C | 127 lines | 67 code | 14 blank | 46 comment | 6 complexity | 2443f6124c0007c398ac01a1b44d6319 MD5 | raw file
  1. /*
  2. ChibiOS/RT - Copyright (C) 2006-2013 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. uint32_t led;
  18. };
  19. static const struct can_instance can1 = {&CAND1, GPIOD_LED5};
  20. static const struct can_instance can2 = {&CAND2, GPIOD_LED3};
  21. /*
  22. * Internal loopback mode, 500KBaud, automatic wakeup, automatic recover
  23. * from abort mode.
  24. * See section 22.7.7 on the STM32 reference manual.
  25. */
  26. static const CANConfig cancfg = {
  27. CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
  28. CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
  29. CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
  30. };
  31. /*
  32. * Receiver thread.
  33. */
  34. static WORKING_AREA(can_rx1_wa, 256);
  35. static WORKING_AREA(can_rx2_wa, 256);
  36. static msg_t can_rx(void *p) {
  37. struct can_instance *cip = p;
  38. EventListener el;
  39. CANRxFrame rxmsg;
  40. (void)p;
  41. chRegSetThreadName("receiver");
  42. chEvtRegister(&cip->canp->rxfull_event, &el, 0);
  43. while(!chThdShouldTerminate()) {
  44. if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100)) == 0)
  45. continue;
  46. while (canReceive(cip->canp, CAN_ANY_MAILBOX,
  47. &rxmsg, TIME_IMMEDIATE) == RDY_OK) {
  48. /* Process message.*/
  49. palTogglePad(GPIOD, cip->led);
  50. }
  51. }
  52. chEvtUnregister(&CAND1.rxfull_event, &el);
  53. return 0;
  54. }
  55. /*
  56. * Transmitter thread.
  57. */
  58. static WORKING_AREA(can_tx_wa, 256);
  59. static msg_t can_tx(void * p) {
  60. CANTxFrame txmsg;
  61. (void)p;
  62. chRegSetThreadName("transmitter");
  63. txmsg.IDE = CAN_IDE_EXT;
  64. txmsg.EID = 0x01234567;
  65. txmsg.RTR = CAN_RTR_DATA;
  66. txmsg.DLC = 8;
  67. txmsg.data32[0] = 0x55AA55AA;
  68. txmsg.data32[1] = 0x00FF00FF;
  69. while (!chThdShouldTerminate()) {
  70. canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
  71. canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
  72. chThdSleepMilliseconds(500);
  73. }
  74. return 0;
  75. }
  76. /*
  77. * Application entry point.
  78. */
  79. int main(void) {
  80. /*
  81. * System initializations.
  82. * - HAL initialization, this also initializes the configured device drivers
  83. * and performs the board-specific initializations.
  84. * - Kernel initialization, the main() function becomes a thread and the
  85. * RTOS is active.
  86. */
  87. halInit();
  88. chSysInit();
  89. /*
  90. * Activates the CAN drivers 1 and 2.
  91. */
  92. canStart(&CAND1, &cancfg);
  93. canStart(&CAND2, &cancfg);
  94. /*
  95. * Starting the transmitter and receiver threads.
  96. */
  97. chThdCreateStatic(can_rx1_wa, sizeof(can_rx1_wa), NORMALPRIO + 7,
  98. can_rx, (void *)&can1);
  99. chThdCreateStatic(can_rx2_wa, sizeof(can_rx2_wa), NORMALPRIO + 7,
  100. can_rx, (void *)&can2);
  101. chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7,
  102. can_tx, NULL);
  103. /*
  104. * Normal main() thread activity, in this demo it does nothing.
  105. */
  106. while (TRUE) {
  107. chThdSleepMilliseconds(500);
  108. }
  109. return 0;
  110. }