virtual_com.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /******************************************************************************
  2. *
  3. * Freescale Semiconductor Inc.
  4. * (c) Copyright 2004-2010 Freescale Semiconductor, Inc.
  5. * ALL RIGHTS RESERVED.
  6. *
  7. **************************************************************************//*!
  8. *
  9. * @file virtual_com.c
  10. *
  11. * @author
  12. *
  13. * @version
  14. *
  15. * @date May-28-2009
  16. *
  17. * @brief The file emulates a USB PORT as Loopback Serial Port.
  18. *****************************************************************************/
  19. /******************************************************************************
  20. * Includes
  21. *****************************************************************************/
  22. #include "hidef.h" /* for EnableInterrupts macro */
  23. #include "derivative.h" /* include peripheral declarations */
  24. #include "types.h" /* Contains User Defined Data Types */
  25. #include "usb_cdc.h" /* USB CDC Class Header File */
  26. #include "virtual_com.h" /* Virtual COM Application Header File */
  27. #include <stdio.h>
  28. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  29. #include "exceptions.h"
  30. #endif
  31. /* skip the inclusion in dependency state */
  32. #ifndef __NO_SETJMP
  33. #include <stdio.h>
  34. #endif
  35. #include <stdlib.h>
  36. #include <string.h>
  37. /*****************************************************************************
  38. * Constant and Macro's - None
  39. *****************************************************************************/
  40. /*****************************************************************************
  41. * Global Functions Prototypes
  42. *****************************************************************************/
  43. void TestApp_Init(void);
  44. /****************************************************************************
  45. * Global Variables
  46. ****************************************************************************/
  47. #if HIGH_SPEED_DEVICE
  48. uint_32 g_cdcBuffer[DIC_BULK_OUT_ENDP_PACKET_SIZE>>1];
  49. #endif
  50. /*****************************************************************************
  51. * Local Types - None
  52. *****************************************************************************/
  53. /*****************************************************************************
  54. * Local Functions Prototypes
  55. *****************************************************************************/
  56. static void USB_App_Callback(uint_8 controller_ID,
  57. uint_8 event_type, void* val);
  58. static void USB_Notify_Callback(uint_8 controller_ID,
  59. uint_8 event_type, void* val);
  60. static void Virtual_Com_App(void);
  61. /*****************************************************************************
  62. * Local Variables
  63. *****************************************************************************/
  64. #ifdef _MC9S08JS16_H
  65. #pragma DATA_SEG APP_DATA
  66. #endif
  67. /* Virtual COM Application start Init Flag */
  68. static volatile boolean start_app = FALSE;
  69. /* Virtual COM Application Carrier Activate Flag */
  70. static volatile boolean start_transactions = FALSE;
  71. /* Receive Buffer */
  72. static uint_8 g_curr_recv_buf[DATA_BUFF_SIZE];
  73. /* Send Buffer */
  74. static uint_8 g_curr_send_buf[DATA_BUFF_SIZE];
  75. /* Receive Data Size */
  76. static uint_8 g_recv_size;
  77. /* Send Data Size */
  78. static uint_8 g_send_size;
  79. /*****************************************************************************
  80. * Local Functions
  81. *****************************************************************************/
  82. /******************************************************************************
  83. *
  84. * @name TestApp_Init
  85. *
  86. * @brief This function is the entry for the Virtual COM Loopback app
  87. *
  88. * @param None
  89. *
  90. * @return None
  91. *****************************************************************************
  92. * This function starts the Virtual COM Loopback application
  93. *****************************************************************************/
  94. void TestApp_Init(void)
  95. {
  96. uint_8 error;
  97. g_recv_size = 0;
  98. g_send_size= 0;
  99. DisableInterrupts;
  100. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  101. usb_int_dis();
  102. #endif
  103. /* Initialize the USB interface */
  104. error = USB_Class_CDC_Init(CONTROLLER_ID,USB_App_Callback,
  105. NULL,USB_Notify_Callback, TRUE);
  106. if(error != USB_OK)
  107. {
  108. /* Error initializing USB-CDC Class */
  109. return;
  110. }
  111. EnableInterrupts;
  112. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  113. usb_int_en();
  114. #endif
  115. }
  116. /******************************************************************************
  117. *
  118. * @name TestApp_Task
  119. *
  120. * @brief Application task function. It is called from the main loop
  121. *
  122. * @param None
  123. *
  124. * @return None
  125. *
  126. *****************************************************************************
  127. * Application task function. It is called from the main loop
  128. *****************************************************************************/
  129. void TestApp_Task(void)
  130. {
  131. #if (defined _MC9S08JE128_H) || (defined _MC9S08JM16_H) || (defined _MC9S08JM60_H) || (defined _MC9S08JS16_H) || (defined _MC9S08MM128_H)
  132. if(USB_PROCESS_PENDING())
  133. {
  134. USB_Engine();
  135. }
  136. #endif
  137. /* call the periodic task function */
  138. USB_Class_CDC_Periodic_Task();
  139. /*check whether enumeration is complete or not */
  140. if((start_app==TRUE) && (start_transactions==TRUE))
  141. {
  142. Virtual_Com_App();
  143. }
  144. }
  145. /******************************************************************************
  146. *
  147. * @name Virtual_Com_App
  148. *
  149. * @brief Implements Loopback COM Port
  150. *
  151. * @param None
  152. *
  153. * @return None
  154. *
  155. *****************************************************************************
  156. * Receives data from USB Host and transmits back to the Host
  157. *****************************************************************************/
  158. static void Virtual_Com_App(void)
  159. {
  160. static uint_8 status = 0;
  161. /* Loopback Application Code */
  162. if(g_recv_size)
  163. {
  164. /* Copy Received Buffer to Send Buffer */
  165. for (status = 0; status < g_recv_size; status++)
  166. {
  167. g_curr_send_buf[status] = g_curr_recv_buf[status];
  168. }
  169. g_send_size = g_recv_size;
  170. g_recv_size = 0;
  171. }
  172. if(g_send_size)
  173. {
  174. /* Send Data to USB Host*/
  175. uint_8 size = g_send_size;
  176. g_send_size = 0;
  177. status = USB_Class_CDC_Interface_DIC_Send_Data(CONTROLLER_ID,
  178. g_curr_send_buf,size);
  179. if(status != USB_OK)
  180. {
  181. /* Send Data Error Handling Code goes here */
  182. }
  183. }
  184. return;
  185. }
  186. /******************************************************************************
  187. *
  188. * @name USB_App_Callback
  189. *
  190. * @brief This function handles Class callback
  191. *
  192. * @param controller_ID : Controller ID
  193. * @param event_type : Value of the event
  194. * @param val : gives the configuration value
  195. *
  196. * @return None
  197. *
  198. *****************************************************************************
  199. * This function is called from the class layer whenever reset occurs or enum
  200. * is complete. After the enum is complete this function sets a variable so
  201. * that the application can start.
  202. * This function also receives DATA Send and RECEIVED Events
  203. *****************************************************************************/
  204. static void USB_App_Callback (
  205. uint_8 controller_ID, /* [IN] Controller ID */
  206. uint_8 event_type, /* [IN] value of the event */
  207. void* val /* [IN] gives the configuration value */
  208. )
  209. {
  210. UNUSED (controller_ID)
  211. UNUSED (val)
  212. if(event_type == USB_APP_BUS_RESET)
  213. {
  214. start_app=FALSE;
  215. }
  216. else if(event_type == USB_APP_ENUM_COMPLETE)
  217. {
  218. #if HIGH_SPEED_DEVICE
  219. // prepare for the next receive event
  220. USB_Class_CDC_Interface_DIC_Recv_Data(&controller_ID,
  221. (uint_8_ptr)g_cdcBuffer,
  222. DIC_BULK_OUT_ENDP_PACKET_SIZE);
  223. #endif
  224. start_app=TRUE;
  225. }
  226. else if((event_type == USB_APP_DATA_RECEIVED)&&
  227. (start_transactions == TRUE))
  228. {
  229. /* Copy Received Data buffer to Application Buffer */
  230. USB_PACKET_SIZE BytesToBeCopied;
  231. APP_DATA_STRUCT* dp_rcv = (APP_DATA_STRUCT*)val;
  232. uint_8 index;
  233. BytesToBeCopied = (USB_PACKET_SIZE)((dp_rcv->data_size > DATA_BUFF_SIZE) ?
  234. DATA_BUFF_SIZE:dp_rcv->data_size);
  235. for(index = 0; index<BytesToBeCopied ; index++)
  236. {
  237. g_curr_recv_buf[index]= dp_rcv->data_ptr[index];
  238. }
  239. g_recv_size = index;
  240. }
  241. else if((event_type == USB_APP_SEND_COMPLETE) && (start_transactions == TRUE))
  242. {
  243. /* Previous Send is complete. Queue next receive */
  244. #if HIGH_SPEED_DEVICE
  245. (void)USB_Class_CDC_Interface_DIC_Recv_Data(CONTROLLER_ID, g_cdcBuffer, DIC_BULK_OUT_ENDP_PACKET_SIZE);
  246. #else
  247. (void)USB_Class_CDC_Interface_DIC_Recv_Data(CONTROLLER_ID, NULL, 0);
  248. #endif
  249. }
  250. return;
  251. }
  252. /******************************************************************************
  253. *
  254. * @name USB_Notify_Callback
  255. *
  256. * @brief This function handles PSTN Sub Class callbacks
  257. *
  258. * @param controller_ID : Controller ID
  259. * @param event_type : PSTN Event Type
  260. * @param val : gives the configuration value
  261. *
  262. * @return None
  263. *
  264. *****************************************************************************
  265. * This function handles USB_APP_CDC_CARRIER_ACTIVATED and
  266. * USB_APP_CDC_CARRIER_DEACTIVATED PSTN Events
  267. *****************************************************************************/
  268. static void USB_Notify_Callback (
  269. uint_8 controller_ID, /* [IN] Controller ID */
  270. uint_8 event_type, /* [IN] PSTN Event Type */
  271. void* val /* [IN] gives the configuration value */
  272. )
  273. {
  274. UNUSED (controller_ID)
  275. UNUSED (val)
  276. if(start_app == TRUE)
  277. {
  278. if(event_type == USB_APP_CDC_CARRIER_ACTIVATED)
  279. {
  280. start_transactions = TRUE;
  281. }
  282. else if(event_type == USB_APP_CDC_CARRIER_DEACTIVATED)
  283. {
  284. start_transactions = FALSE;
  285. }
  286. }
  287. return;
  288. }
  289. /* EOF */