candemo.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2005 proconX Pty Ltd <www.proconx.com>
  3. *
  4. * $Id: candemo.c 4640 2012-09-24 12:05:56Z u_bonnes $
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \example canbus/candemo.c
  38. *
  39. * Example program to demonstrate the use of the CAN bus and the ATCAN driver
  40. * on the <A href="http://www.proconx.com/xnut105"> XNUT-105 DIN rail single
  41. * board computer with CAN and embedded Ethernet</A>. The XNUT-105 module
  42. * features the AT90CAN128 AVR CPU with built-in CAN controller and is running
  43. * the Nut/OS. This demo also compiles for AVR ATmega128 designs with SJA1000
  44. * CAN controller.
  45. *
  46. * This program receives CAN messages and logs them on the serial port via
  47. * devDebug0. It also continuously broadcasts a CAN frame.
  48. */
  49. // Nut/OS header
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <io.h>
  54. #include <fcntl.h>
  55. #include <sys/timer.h>
  56. #include <sys/thread.h>
  57. #include <dev/debug.h>
  58. #include <dev/uartavr.h>
  59. #include <dev/can_dev.h>
  60. #include <dev/board.h>
  61. #if defined(MCU_AT90CAN128) // Internal AVR MCU CAN controller
  62. # include <dev/atcan.h>
  63. # define DEV_CAN devAtCan
  64. #else
  65. # include <dev/sja1000.h> // External SJA1000 CAN controller
  66. # define DEV_CAN devSJA1000
  67. #endif
  68. /*****************************************************************************
  69. * Main
  70. *****************************************************************************/
  71. CANFRAME canFrame;
  72. CANINFO *canInfoPtr;
  73. /**
  74. * Main entry point of application
  75. */
  76. int main(void)
  77. {
  78. #if defined(__AVR__) && defined(__GNUC__)
  79. unsigned long i;
  80. int result;
  81. #endif
  82. uint32_t baud = 115200;
  83. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  84. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  85. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  86. printf("CAN driver test program\n");
  87. #if defined(__AVR__) && defined(__GNUC__)
  88. // Init CAN controller
  89. result = NutRegisterDevice(&DEV_CAN, 0, 0);
  90. canInfoPtr = (CANINFO *) DEV_CAN.dev_dcb;
  91. #if defined(MCU_AT90CAN128)
  92. // Re-configure receive message objects
  93. AtCanEnableRx(8, // 8 CAN objects as RX buffer, 7 remaining as TX buffer
  94. 0, // Acceptance code (0 = accept all IDs)
  95. 1, // Flag if acceptance code is extended (0 = standard, 1 = extended)
  96. 0, // Acceptance code's remote tag (0 or 1)
  97. 0, // Acceptance mask
  98. 0, // 0 to receive extended and standard frames, 1 if message ID type must match acceptance code flag
  99. 0 // 0 to receive remote and standard frames, 1 if remote tag must match acceptance code's remote tag
  100. );
  101. #endif
  102. // Set CAN bit rate
  103. CAN_SetSpeed(&DEV_CAN, CAN_SPEED_125K);
  104. printf("Starting CAN RX/TX loop...\n");
  105. for (i = 0;;i++)
  106. {
  107. // Prepare a sample frame for sending
  108. memset(&canFrame, 0, sizeof(canFrame));
  109. canFrame.id = 0x123;
  110. canFrame.len = 8;
  111. canFrame.ext = 0; // Set to 1 to send an extended frame
  112. canFrame.byte[0] = 0x11;
  113. canFrame.byte[1] = 0x22;
  114. canFrame.byte[2] = 0x33;
  115. canFrame.byte[3] = 0x44;
  116. canFrame.byte[4] = 0x55;
  117. canFrame.byte[5] = 0x66;
  118. canFrame.byte[6] = 0x77;
  119. canFrame.byte[7] = 0x88;
  120. CAN_TxFrame(&DEV_CAN, &canFrame);
  121. // Check if we did receive a frame
  122. if (CAN_TryRxFrame(&DEV_CAN, &canFrame) == 0)
  123. {
  124. uint8_t j;
  125. printf("%ld ", canFrame.id);
  126. for (j = 0; j < canFrame.len; j++)
  127. printf("%02X ", canFrame.byte[j]);
  128. printf(" Stats: %lu %lu %lu %lu\n", canInfoPtr->can_interrupts,
  129. canInfoPtr->can_rx_frames,
  130. canInfoPtr->can_tx_frames,
  131. canInfoPtr->can_overruns);
  132. }
  133. NutSleep(10); // Don't overflow the bus and give time to other threads
  134. }
  135. #else /* __AVR__ && __GNUC__ */
  136. puts("No CAN device available");
  137. #endif /* __AVR__ && __GNUC__ */
  138. return 0;
  139. }