cantest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*!
  2. * Copyright (C) 2013 Uwe Bonnes, bon@elektron.ikp.physik.tu-darmstadt.de
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id: cantest.c 5125 2013-05-09 16:14:18Z u_bonnes $
  36. */
  37. /*!
  38. * \example cantest/cantest.c
  39. *
  40. * This sample demonstrates usage of the can_dev API
  41. * At present only available on STM32
  42. *
  43. */
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include <dev/board.h>
  48. #include <dev/canbus.h>
  49. #include <dev/uart.h>
  50. #include <sys/timer.h>
  51. #include <sys/thread.h>
  52. static char *banner = "\nNut/OS CANTEST Sample " __DATE__ " " __TIME__"\n";
  53. #define MASTER 0
  54. #define SLAVE 1
  55. #if defined(DEF_CANBUS)
  56. THREAD(service_thread, arg)
  57. {
  58. FILE *uart = arg;
  59. char inbuf[1];
  60. int res, pending;
  61. CANFRAME data;
  62. uint32_t millis;
  63. for (;;)
  64. {
  65. res = _read(_fileno(uart), inbuf, sizeof(inbuf));
  66. if (res == 0)
  67. continue;
  68. switch (inbuf[0])
  69. {
  70. case 'C':
  71. printf("MASTER CAN_RX_FRAMES %d\n",
  72. CanGetCounter(&DEF_CANBUS, CAN_RX_FRAMES));
  73. printf("MASTER CAN_TX_FRAMES %d\n",
  74. CanGetCounter(&DEF_CANBUS, CAN_TX_FRAMES));
  75. printf("MASTER CAN_INTERRUPTS %d\n",
  76. CanGetCounter(&DEF_CANBUS, CAN_INTERRUPTS));
  77. printf("MASTER CAN_RX_INTERRUPTS %d\n",
  78. CanGetCounter(&DEF_CANBUS, CAN_RX_INTERRUPTS));
  79. printf("MASTER CAN_TX_INTERRUPTS %d\n",
  80. CanGetCounter(&DEF_CANBUS, CAN_TX_INTERRUPTS));
  81. printf("MASTER CAN_SCE_INTERRUPTS %d\n",
  82. CanGetCounter(&DEF_CANBUS, CAN_SCE_INTERRUPTS));
  83. printf("MASTER CAN_OVERRUNS %d\n",
  84. CanGetCounter(&DEF_CANBUS, CAN_OVERRUNS));
  85. printf("MASTER CAN_ERRORS %d\n",
  86. CanGetCounter(&DEF_CANBUS, CAN_ERROR));
  87. #if defined(DEF_CANBUS_SLAVE)
  88. printf(" SLAVE CAN_RX_FRAMES %d\n",
  89. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_RX_FRAMES));
  90. printf(" SLAVE CAN_TX_FRAMES %d\n",
  91. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_TX_FRAMES));
  92. printf(" SLAVE CAN_INTERRUPTS %d\n",
  93. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_INTERRUPTS));
  94. printf(" SLAVE CAN_RX_INTERRUPTS %d\n",
  95. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_RX_INTERRUPTS));
  96. printf(" SLAVE CAN_TX_INTERRUPTS %d\n",
  97. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_TX_INTERRUPTS));
  98. printf(" SLAVE CAN_SCE_INTERRUPTS %d\n",
  99. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_SCE_INTERRUPTS));
  100. printf(" SLAVE CAN_OVERRUNS %d\n",
  101. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_OVERRUNS));
  102. printf(" SLAVE CAN_ERRORS %d\n",
  103. CanGetCounter(&DEF_CANBUS_SLAVE, CAN_ERROR));
  104. #endif
  105. break;
  106. case 's':
  107. memset(&data, 0, sizeof(data));
  108. data.id = 0x5a5;
  109. pending = 1;
  110. break;
  111. case 'e':
  112. memset(&data, 0, sizeof(data));
  113. data.id = 0x01234567;
  114. data.ext = 1;
  115. data.len = 4;
  116. millis = NutGetMillis();
  117. data.byte[0] = (millis>>24) & 0xff;
  118. data.byte[1] = (millis>>16) & 0xff;
  119. data.byte[2] = (millis>> 8) & 0xff;
  120. data.byte[3] = (millis>> 0) & 0xff;
  121. pending = 1;
  122. break;
  123. }
  124. if (pending && CanTxFree(&DEF_CANBUS))
  125. {
  126. CanOutput(&DEF_CANBUS, &data);
  127. pending = 0;
  128. }
  129. }
  130. }
  131. void print_frame(int type)
  132. {
  133. int res, i;
  134. CANFRAME data;
  135. res = CanInput((type)?&DEF_CANBUS_SLAVE:&DEF_CANBUS, &data);
  136. if (res)
  137. {
  138. printf("CanInput %s failed %d\n", (type)?" SLAVE":"MASTER", res);
  139. while(1) NutSleep(100);
  140. }
  141. if (data.ext)
  142. printf("ID %08lx", data.id);
  143. else
  144. printf("ID %03lx", data.id);
  145. printf("%s len %2d%4s%4s",
  146. (type)?" SLAVE ":" MASTER ",
  147. data.len,
  148. (data.ext)?" ext":" ",
  149. (data.rtr)?" rtr":" ");
  150. if(data.len)
  151. {
  152. printf(" data:");
  153. for(i=0; i<data.len; i++)
  154. printf(" %02x",data.byte[i]);
  155. }
  156. printf("\n");
  157. }
  158. #else
  159. THREAD(service_thread, arg)
  160. {
  161. while(1)
  162. NutSleep(1);
  163. }
  164. void print_frame(int type)
  165. {
  166. }
  167. #endif
  168. /*
  169. * CAN_DEV
  170. *
  171. */
  172. int main(void)
  173. {
  174. int res;
  175. uint32_t baud = 115200, read_timeout = 10;
  176. FILE *uart;
  177. CANFILTER filter;
  178. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  179. uart = fopen(DEV_CONSOLE.dev_name, "r+");
  180. _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  181. _ioctl(_fileno(uart), UART_SETREADTIMEOUT, &read_timeout);
  182. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  183. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  184. printf(banner);
  185. NutThreadCreate("input", service_thread, uart, 1024);
  186. #if !defined(DEF_CANBUS)
  187. (void) res;
  188. (void) filter;
  189. printf("No suitable CAN driver found for that device");
  190. for (;;) {
  191. NutSleep(100);
  192. }
  193. #else
  194. res = NutRegisterCanBus( &DEF_CANBUS, -1);
  195. if (res) {
  196. printf("NutRegisterCanBus failed %d\n", res);
  197. while(1) NutSleep(100);
  198. }
  199. res = CanSetBaud(&DEF_CANBUS, CAN_SPEED_1M, 0);
  200. if (res) {
  201. printf("CanSetBaud failed %d\n", res);
  202. while(1) NutSleep(100);
  203. }
  204. /* Receive extended frame with any address on Master*/
  205. memset(&filter, 0, sizeof(filter));
  206. filter.mask_ext = 1;
  207. filter.id_ext = 1;
  208. res = CanAddFilter(&DEF_CANBUS, &filter);
  209. if (res) {
  210. printf("CanAddFilter Slave failed %d\n", res);
  211. while(1) NutSleep(100);
  212. }
  213. #if defined(DEF_CANBUS_SLAVE)
  214. res = NutRegisterCanBus( &DEF_CANBUS_SLAVE, -1);
  215. if (res) {
  216. printf("NutRegisterCanBus compaignon failed %d\n", res);
  217. while(1) NutSleep(100);
  218. }
  219. /* Receive standard frame with any address on Slave*/
  220. filter.id_ext = 0;
  221. res = CanAddFilter(&DEF_CANBUS_SLAVE, &filter);
  222. if (res) {
  223. printf("CanAddFilter Master failed %d\n", res);
  224. while(1) NutSleep(100);
  225. }
  226. #endif
  227. CanEnableRx(&DEF_CANBUS);
  228. #if defined(DEF_CANBUS_SLAVE)
  229. CanEnableRx(&DEF_CANBUS_SLAVE);
  230. #endif
  231. for (;;) {
  232. if(CanRxAvail(&DEF_CANBUS))
  233. print_frame(MASTER);
  234. #if defined(DEF_CANBUS_SLAVE)
  235. if(CanRxAvail(&DEF_CANBUS_SLAVE))
  236. print_frame(SLAVE);
  237. #endif
  238. NutSleep(100);
  239. }
  240. #endif
  241. return 0;
  242. }