disk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /******************************************************************************
  2. *
  3. * Freescale Semiconductor Inc.
  4. * (c) Copyright 2004-2010 Freescale Semiconductor, Inc.
  5. * ALL RIGHTS RESERVED.
  6. *
  7. **************************************************************************//*!
  8. *
  9. * @file disk.c
  10. *
  11. * @author
  12. *
  13. * @version
  14. *
  15. * @date May-08-2009
  16. *
  17. * @brief RAM Disk has been emulated via this Mass Storage Demo
  18. *****************************************************************************/
  19. /******************************************************************************
  20. * Includes
  21. *****************************************************************************/
  22. #include "types.h" /* User Defined Data Types */
  23. #include "hidef.h" /* for EnableInterrupts macro */
  24. #include <string.h>
  25. #include <stdlib.h> /* ANSI memory controls */
  26. #include "stdio.h"
  27. #include "derivative.h" /* include peripheral declarations */
  28. #include "usb_msc.h" /* USB MSC Class Header File */
  29. #include "disk.h" /* Disk Application Header File */
  30. #include "usb_class.h"
  31. #include "FAT16.h"
  32. #include "sci.h"
  33. #if defined(__MCF52xxx_H__)
  34. #include "Wdt_cfv2.h"
  35. #include "SD_cfv2.h"
  36. #endif
  37. #ifdef MCU_mcf51jf128
  38. #include "SD_cfv1_plus.h"
  39. #endif
  40. #if (defined MCU_MK60N512VMD100) || (defined MCU_MK70F12) || (defined MCU_MK21D5)
  41. #include "SD_kinetis.h"
  42. #endif
  43. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  44. #include "exceptions.h"
  45. #endif
  46. /* skip the inclusion in dependency statge */
  47. #ifndef __NO_SETJMP
  48. #include <stdio.h>
  49. #endif
  50. #include <stdlib.h>
  51. #include <string.h>
  52. /*****************************************************************************
  53. * Constant and Macro's - None
  54. *****************************************************************************/
  55. /*****************************************************************************
  56. * Global Functions Prototypes
  57. *****************************************************************************/
  58. void TestApp_Init(void);
  59. extern void Watchdog_Reset(void);
  60. extern uint_8 filetype; /* image file type */
  61. uint_32 file_size; /* bytes */
  62. uint_8 new_file;
  63. uint_8 error=FLASH_IMAGE_SUCCESS;
  64. boolean boot_complete = FALSE;
  65. /****************************************************************************
  66. * Global Variables
  67. ****************************************************************************/
  68. /* Add all the variables needed for disk.c to this structure */
  69. DISK_GLOBAL_VARIABLE_STRUCT g_disk;
  70. unsigned char BootloaderStatus = BootloaderReady;
  71. /* Write data to SD*/
  72. static LBA_APP_STRUCT lba_data;
  73. static uint_8 lba_file_info_data[512];
  74. static uint_32 block_position = 0;
  75. static uint_16 buff_postion = 0;
  76. static boolean video_flag = FALSE;
  77. static uint_16 image_position = 0;
  78. static uint_8 data_bff[512];
  79. static uint_8 pre_data=0,cur_data=0,next_data=0;
  80. static uint_8 info_flag=FALSE;
  81. static uint_32 total_block=0,num_image=0;
  82. static uint_32 dwScale,dwRate;
  83. static uint_32 frame_rate;
  84. /*****************************************************************************
  85. * Local Types - None
  86. *****************************************************************************/
  87. /*****************************************************************************
  88. * Local Functions Prototypes
  89. *****************************************************************************/
  90. void USB_App_Callback(uint_8 controller_ID, uint_8 event_type, void* val);
  91. void MSD_Event_Callback(uint_8 controller_ID, uint_8 event_type, void* val);
  92. void Disk_App(void);
  93. /*****************************************************************************
  94. * Local Variables
  95. *****************************************************************************/
  96. /*****************************************************************************
  97. * Local Functions
  98. *****************************************************************************/
  99. static void Sd_Write (uchar_ptr data_ptr);
  100. static void Sd_Prepare_Data (uint_8_ptr data_ptr);
  101. /******************************************************************************
  102. *
  103. * @name Disk_App
  104. *
  105. * @brief
  106. *
  107. * @param None
  108. *
  109. * @return None
  110. *
  111. *****************************************************************************/
  112. void Disk_App(void)
  113. {
  114. /* Body */
  115. uint_32 i = 0;
  116. uint_32 j = 0;
  117. if(TRUE== boot_complete)
  118. {
  119. /* De-Init MSD device */
  120. for(i=0;i<1000;i++)
  121. for(j=0;j<1000;j++) ;
  122. USB_Class_MSC_DeInit(USB_CONTROLLER_ID);
  123. boot_complete = FALSE;
  124. for(i=0;i<1000;i++)
  125. for(j=0;j<1000;j++) ;
  126. /* Re-enumerate MSD device */
  127. DisableInterrupts;
  128. USB_Class_MSC_Init(g_disk.app_controller_ID,
  129. USB_App_Callback,NULL, MSD_Event_Callback);
  130. EnableInterrupts;
  131. } /* EndIf */
  132. return;
  133. } /* EndBody */
  134. /******************************************************************************
  135. *
  136. * @name USB_App_Callback
  137. *
  138. * @brief This function handles the callback
  139. *
  140. * @param controller_ID : To Identify the controller
  141. * @param event_type : value of the event
  142. * @param val : gives the configuration value
  143. *
  144. * @return None
  145. *
  146. *****************************************************************************/
  147. void USB_App_Callback
  148. (
  149. uint_8 controller_ID,
  150. uint_8 event_type,
  151. void* val
  152. )
  153. {
  154. /* Body */
  155. UNUSED (controller_ID);
  156. UNUSED (val);
  157. if(event_type == USB_APP_BUS_RESET)
  158. {
  159. g_disk.start_app=FALSE;
  160. }
  161. else
  162. {
  163. if(event_type == USB_APP_ENUM_COMPLETE)
  164. {
  165. g_disk.start_app=TRUE;
  166. }
  167. else
  168. {
  169. if(event_type == USB_APP_ERROR)
  170. {
  171. /* add user code for error handling */
  172. } /* EndIf */
  173. } /* EndIf */
  174. } /* EndIf */
  175. return;
  176. } /* EndBody */
  177. /******************************************************************************
  178. *
  179. * @name MSD_Event_Callback
  180. *
  181. * @brief This function handles the callback
  182. *
  183. * @param controller_ID : To Identify the controller
  184. * @param event_type : value of the event
  185. * @param val : gives the configuration value
  186. *
  187. * @return None
  188. *
  189. *****************************************************************************/
  190. void MSD_Event_Callback
  191. (
  192. uint_8 controller_ID,
  193. uint_8 event_type,
  194. void* val
  195. )
  196. {
  197. /* Body */
  198. PTR_LBA_APP_STRUCT lba_data_ptr;
  199. uint_32 i;
  200. uint_32 count;
  201. uint_8_ptr prevent_removal_ptr, load_eject_start_ptr;
  202. PTR_DEVICE_LBA_INFO_STRUCT device_lba_info_ptr;
  203. UNUSED (controller_ID);
  204. switch(event_type)
  205. {
  206. case USB_APP_DATA_RECEIVED :
  207. break;
  208. case USB_APP_SEND_COMPLETE :
  209. break;
  210. case USB_MSC_START_STOP_EJECT_MEDIA :
  211. load_eject_start_ptr = (uint_8_ptr)val;
  212. UNUSED(load_eject_start_ptr);
  213. /* Code to be added by user for starting, stopping or
  214. ejecting the disk drive. e.g. starting/stopping the motor in
  215. case of CD/DVD*/
  216. break;
  217. case USB_MSC_DEVICE_READ_REQUEST :
  218. /* copy data from storage device before sending it on USB Bus
  219. (Called before calling send_data on BULK IN endpoints)*/
  220. lba_data_ptr = (PTR_LBA_APP_STRUCT)val;
  221. /* read data from mass storage device to driver buffer */
  222. FATReadLBA(lba_data_ptr->offset>>9,lba_data_ptr->buff_ptr);
  223. break;
  224. case USB_MSC_DEVICE_WRITE_REQUEST :
  225. /* copy data from USb buffer to Storage device
  226. (Called before after recv_data on BULK OUT endpoints)*/
  227. lba_data_ptr = (PTR_LBA_APP_STRUCT)val;
  228. if((lba_data_ptr->offset>>9)== FATTable0Sec0)
  229. {
  230. /* write new file */
  231. new_file = TRUE;
  232. } /* EndIf */
  233. if(((lba_data_ptr->offset>>9)== FATRootDirSec0)&&(new_file == TRUE))
  234. {
  235. /* read file size of the file was received */
  236. /* search for the file entry was received */
  237. /* each file entry contain 32 bytes */
  238. for(i = lba_data_ptr->size -32; i>0; i -=32)
  239. {
  240. if(*(lba_data_ptr->buff_ptr +i) != 0)
  241. {
  242. break;
  243. } /* EndIf */
  244. } /* Endfor */
  245. /* the file size field is offet 28 of file entry */
  246. #if (defined __MK_xxx_H__) || (defined MCU_MK70F12)
  247. file_size = *(uint_32*)( lba_data_ptr->buff_ptr + i + 28);
  248. #else
  249. file_size = BYTESWAP32(*(uint_32*)( lba_data_ptr->buff_ptr + i + 28));
  250. #endif
  251. new_file = FALSE;
  252. } /* EndIf */
  253. if((lba_data_ptr->offset>>9)>= FATDataSec0)
  254. {
  255. Sd_Prepare_Data(lba_data_ptr->buff_ptr);
  256. } /* EndIf */
  257. /* rest of file */
  258. if(((lba_data_ptr->offset>>9) - FATDataSec0) == ((file_size -1)/512))
  259. {
  260. boot_complete=TRUE; /*tranfer file done */
  261. *((uint_32*)lba_file_info_data) = ieee_ntohl(total_block);
  262. *((uint_32*)(lba_file_info_data+4)) = ieee_ntohl(num_image);
  263. *((uint_32*)(lba_file_info_data+8)) = ieee_ntohl(frame_rate);
  264. for (count=12 ; count<512 ; count ++)
  265. {
  266. *(lba_file_info_data+count) = 0xFF;
  267. }
  268. lba_data.offset = 0x00010000;
  269. lba_data.size = 0x200;
  270. lba_data.buff_ptr = lba_file_info_data;
  271. SD_Write_Block(&lba_data);
  272. BootloaderStatus = BootloaderSuccess;
  273. } /* EndIf */
  274. break;
  275. case USB_MSC_DEVICE_FORMAT_COMPLETE :
  276. break;
  277. case USB_MSC_DEVICE_REMOVAL_REQUEST :
  278. prevent_removal_ptr = (uint_8_ptr) val;
  279. if(SUPPORT_DISK_LOCKING_MECHANISM)
  280. {
  281. g_disk.disk_lock = *prevent_removal_ptr;
  282. }
  283. else
  284. {
  285. if((!SUPPORT_DISK_LOCKING_MECHANISM)&&(!(*prevent_removal_ptr)))
  286. {
  287. /*there is no support for disk locking and removal of medium is enabled*/
  288. /* code to be added here for this condition, if required */
  289. } /* EndIf */
  290. } /* EndIf */
  291. break;
  292. case USB_MSC_DEVICE_GET_INFO :
  293. device_lba_info_ptr = (PTR_DEVICE_LBA_INFO_STRUCT)val;
  294. device_lba_info_ptr->total_lba_device_supports = TOTAL_LOGICAL_ADDRESS_BLOCKS;
  295. device_lba_info_ptr->length_of_each_lba_of_device = LENGTH_OF_EACH_LAB;
  296. device_lba_info_ptr->num_lun_supported = LOGICAL_UNIT_SUPPORTED;
  297. break;
  298. default :
  299. break;
  300. } /* EndSwitch */
  301. return;
  302. } /* EndBody */
  303. /******************************************************************************
  304. *
  305. * @name TestApp_Init
  306. *
  307. * @brief This function is the entry for mouse (or other usuage)
  308. *
  309. * @param None
  310. *
  311. * @return None
  312. **
  313. *****************************************************************************/
  314. void TestApp_Init(void)
  315. {
  316. /* Body */
  317. uint_8 error;
  318. #if !(defined _MC9S08JM60_H)
  319. //sci_init();
  320. #endif
  321. SD_Init();
  322. (void)SD_ReadCSD();
  323. /* initialize the Global Variable Structure */
  324. USB_memzero(&g_disk, sizeof(DISK_GLOBAL_VARIABLE_STRUCT));
  325. g_disk.app_controller_ID = USB_CONTROLLER_ID;
  326. DisableInterrupts;
  327. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  328. usb_int_dis();
  329. #endif
  330. /* Initialize the USB interface */
  331. error = USB_Class_MSC_Init(g_disk.app_controller_ID,
  332. USB_App_Callback,NULL, MSD_Event_Callback);
  333. UNUSED(error);
  334. EnableInterrupts;
  335. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  336. usb_int_en();
  337. #endif
  338. } /* EndBody */
  339. /******************************************************************************
  340. *
  341. * @name TestApp_Task
  342. *
  343. * @brief Application task function. It is called from the main loop
  344. *
  345. * @param None
  346. *
  347. * @return None
  348. *
  349. *****************************************************************************
  350. * Application task function. It is called from the main loop
  351. *****************************************************************************/
  352. void TestApp_Task(void)
  353. {
  354. /* Body */
  355. /* call the periodic task function */
  356. USB_MSC_Periodic_Task();
  357. /*check whether enumeration is complete or not */
  358. if(g_disk.start_app==TRUE)
  359. {
  360. Disk_App();
  361. } /* EndIf */
  362. } /* EndBody */
  363. /******************************************************************************
  364. *
  365. * @name Sd_Write
  366. *
  367. * @brief This function is the entry for mouse (or other usage)
  368. *
  369. * @param data_ptr
  370. *
  371. * @return None
  372. **
  373. *****************************************************************************/
  374. static void Sd_Write (uint_8_ptr data_ptr)
  375. {
  376. /* Body */
  377. lba_data.offset = 0x00011200 + 512*block_position;
  378. lba_data.size = 0x200;
  379. lba_data.buff_ptr = (uint_8_ptr)data_ptr;
  380. SD_Write_Block(&lba_data);
  381. block_position++;
  382. } /* EndBody */
  383. /******************************************************************************
  384. *
  385. * @name Sd_Prepare_Data
  386. *
  387. * @brief This function is the entry for mouse (or other usuage)
  388. *
  389. * @param data_ptr
  390. *
  391. * @return None
  392. **
  393. *****************************************************************************/
  394. static void Sd_Prepare_Data (uint_8_ptr data_ptr)
  395. {
  396. /* Body */
  397. uint_8_ptr buffer;
  398. uint_16 j = 0;
  399. buffer = data_ptr;
  400. if(TRUE==info_flag)
  401. {
  402. /*The buffer isn't contain video header*/
  403. for(j= 0;j<LENGTH_OF_EACH_LAB;j++,buff_postion++)
  404. {
  405. pre_data=cur_data;
  406. cur_data=next_data;
  407. next_data= buffer[j];
  408. if(( 0xFF==cur_data)&&(0xD8==next_data))
  409. {
  410. video_flag = TRUE;
  411. num_image++;
  412. }
  413. if(TRUE==video_flag )
  414. {
  415. if ((0xFF==pre_data)&&(0xD9==cur_data))
  416. {
  417. video_flag = FALSE;
  418. } /* EndIf */
  419. data_bff[image_position++]= cur_data;
  420. } /* EndIf */
  421. if ( 512==image_position)
  422. {
  423. Sd_Write(data_bff);
  424. image_position = 0;
  425. total_block ++;
  426. } /* EndIf */
  427. } /* EndFor */
  428. }
  429. else
  430. {
  431. /*Read video header to get video information*/
  432. dwScale = (buffer[128])|(buffer[129]<<8)|(buffer[130]<<16)|(buffer[131]<<24);
  433. dwRate = (buffer[132])|(buffer[133]<<8)|(buffer[134]<<16)|(buffer[135]<<24);
  434. frame_rate=(uint_32)(dwRate/dwScale);
  435. info_flag=TRUE;
  436. } /* EndIf */
  437. } /* EndBody */
  438. /* EOF */