aboutsummaryrefslogtreecommitdiff
path: root/laser-tag software/disk.c
blob: 84b1c3f1559f9136f430d2030bee84f4d4376554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/**HEADER********************************************************************
 * 
 * Copyright (c) 2008, 2013 - 2014 Freescale Semiconductor;
 * All Rights Reserved
 *
 * Copyright (c) 1989-2008 ARC International;
 * All Rights Reserved
 *
 *************************************************************************** 
 *
 * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
 * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 * THE POSSIBILITY OF SUCH DAMAGE.
 *
 **************************************************************************
 *
 * $FileName: disk.c$
 * $Version : 
 * $Date    : 
 *
 * Comments:
 *
 * @brief  RAM Disk has been emulated via this Mass Storage Demo
 *****************************************************************************/

/******************************************************************************
 * Includes
 *****************************************************************************/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device_stack_interface.h"
#include "disk.h"
#include "adapter_cfg.h"
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#include "fsl_device_registers.h"
#include "fsl_clock_manager.h"
//#include "board.h"
#include "fsl_debug_console.h"
#if (TWR_K64F120M || FRDM_K64F || TWR_K60D100M || TWR_K21F120M || TWR_K65F180M)
#include "fsl_mpu_hal.h"
#endif
#include "fsl_port_hal.h"
#include <stdio.h>
#include <stdlib.h>
#endif

#if SD_CARD_APP
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM)
#include "sd_esdhc_kinetis.h"
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#include "fsl_sdhc_card.h"
#include "fsl_gpio_driver.h"
#include "gpio_pins.h"
#include "board.h"
#else
#include "sd.h"
#endif
#endif

#if USBCFG_DEV_COMPOSITE
#error This application requires USBCFG_DEV_COMPOSITE defined zero in usb_device_config.h. Please recompile usbd with this option.
#endif

#if !USBCFG_DEV_MSC
#error This application requires USBCFG_DEV_MSC defined none zero in usb_device_config.h. Please recompile usbd with this option.
#endif

#if HIGH_SPEED

#if !USBCFG_DEV_EHCI
#error This application requires USBCFG_DEV_EHCI defined none zero in usb_device_config.h. Please recompile usbd with this option.
#endif

#endif

extern void Main_Task(uint32_t param);
#define MAIN_TASK       10

#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
    { MAIN_TASK, Main_Task, 2 * 3000L, 9L, "Main", MQX_AUTO_START_TASK, 0, 0 },
    { 0L, 0L, 0L, 0L, 0L, 0L, 0, 0 }
};

MQX_FILE_PTR g_sdcard_handle;
#endif
/*****************************************************************************
 * Constant and Macro's - None
 *****************************************************************************/

/*****************************************************************************
 * Global Functions Prototypes
 *****************************************************************************/
void TestApp_Init(void);

/****************************************************************************
 * Global Variables
 ****************************************************************************/
/* Add all the variables needed for disk.c to this structure */
extern usb_desc_request_notify_struct_t desc_callback;
usb_application_callback_struct_t msc_application_callback;
usb_vendor_req_callback_struct_t vend_req_callback;
usb_class_specific_callback_struct_t class_specific_callback;

msc_config_struct_t g_msd_config;
#if (RAM_DISK_APP)
/* Here set storage_disk value to put the 24k buffer to RW region, 
 if putting in ZI and 64k ram the ZI region cannot be put in upper/bottom 32k ram for iar */
disk_struct_t g_disk =
{
    .storage_disk = {0x01},
};
#else
disk_struct_t g_disk;
#endif

#if SD_CARD_APP
    #define USE_SDHC_PROTOCOL    (1)
    #define USE_SPI_PROTOCOL     (0)
#endif

/*****************************************************************************
 * Local Types - None
 *****************************************************************************/

/*****************************************************************************
 * Local Functions Prototypes
 *****************************************************************************/
void USB_App_Device_Callback(uint8_t event_type, void* val, void* arg);
uint8_t USB_App_Class_Callback
(uint8_t event_type,
    uint16_t value,
    uint8_t ** data,
    uint32_t* size,
    void* arg
    );
void Disk_App(void);
/*****************************************************************************
 * Local Variables 
 *****************************************************************************/
#if SD_CARD_APP
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#define SDCARD_BLOCK_SIZE_POWER   (9)
sdhc_card_t card = {0};
sdhc_host_t host = {0};
#endif
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM || OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
uint32_t g_msc_bulk_out_buff_temp[MSD_RECV_BUFFER_NUM][MSD_RECV_BUFFER_SIZE >> 2];
uint32_t g_msc_bulk_in_buff_temp[MSD_SEND_BUFFER_SIZE >> 2];
#endif
uint8_t *g_msc_bulk_out_buff;
uint8_t *g_msc_bulk_in_buff;
#endif
/*****************************************************************************
 * Local Functions
 *****************************************************************************/
#if SD_CARD_APP
os_msgq_handle write_buffer_msg_handler;

/******************************************************************************
 *  
 *   @name        get_write_buffer
 * 
 *   @brief       This function get the buffer for writing
 * 
 *   @param       None
 * 
 *   @return      buffer pointer (return NULL when there is no idle buffer )
 **                
 *****************************************************************************/
uint32_t* get_write_buffer(void)
{
    uint32_t msg;
    /* wait forever for RTOS; return immediately for bm */
    if (OS_MsgQ_recv(write_buffer_msg_handler, (void*)&msg, OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY, 0) == OS_MSGQ_OK)
    {
        return (uint32_t*)msg;
    }
    return NULL;
}

/******************************************************************************
 *  
 *   @name        get_write_buffer
 * 
 *   @brief       This function get the buffer for writing
 * 
 *   @param       buffer : The buffer pointer
 * 
 *   @return      None
 **                
 *****************************************************************************/
void free_write_buffer(uint32_t* buffer)
{
    uint32_t address = (uint32_t)buffer;
    OS_MsgQ_send(write_buffer_msg_handler, (void*)(&address), 0);
}
#endif

#if MUTILE_BUFFER && (((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) && (USE_RTOS)) || (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX))
os_msgq_handle write_msg_handler;
os_mutex_handle sdhc_mutex;

static void write_task(void *arg)
{
    uint8_t* buff_ptr;
    uint32_t offset;
    uint32_t size;
    uint32_t msg[3];
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
    sdhc_status_t error_code = kStatus_SDHC_NoError;
#else
    _mqx_uint error_code = MQX_OK;
#endif

    while (OS_MsgQ_recv(write_msg_handler, (void*)&msg, OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY, 0) == OS_MSGQ_OK)
    {
        buff_ptr = (uint8_t*)msg[0];
        offset = msg[1];
        size = msg[2];

        OS_Mutex_lock(sdhc_mutex);
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
        error_code = SDCARD_DRV_WriteBlocks(&card, buff_ptr, offset >> SDCARD_BLOCK_SIZE_POWER, size >> SDCARD_BLOCK_SIZE_POWER);
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
        error_code = sd_write_device_sectors(g_sdcard_handle, offset >> SDCARD_BLOCK_SIZE_POWER, size >> SDCARD_BLOCK_SIZE_POWER, 3, buff_ptr, NULL);
#endif
        OS_Mutex_unlock(sdhc_mutex);
        free_write_buffer((uint32_t*)buff_ptr);
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
        if (error_code != kStatus_SDHC_NoError)
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
        if (error_code != MQX_OK)
#endif
        {
            g_disk.read_write_error = 1;
            USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code);
        }
    }
}
#endif

#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK && SD_CARD_APP)
#if defined(FSL_RTOS_MQX)
#include "fsl_sdhc_driver.h"
void SDHC_MQX_IRQHandler(void)
{
#if defined BSP_FSL_SDHC_USING_IRQ
    SDHC_DRV_DoIrq(0);
#endif
}
#endif

/* don't use interrupt to detect the sdcard */
gpio_input_pin_user_config_t sdhc_detect_pin[2];

void sdhc_detect_io_init(void)
{
    for (int i = 0; i < 2; ++i)
    {
        sdhc_detect_pin[i].pinName = sdhcCdPin[i].pinName;
        sdhc_detect_pin[i].config.isPullEnable = sdhcCdPin[i].config.isPullEnable;
        sdhc_detect_pin[i].config.pullSelect = sdhcCdPin[i].config.pullSelect;
        sdhc_detect_pin[i].config.isPassiveFilterEnabled = sdhcCdPin[i].config.isPassiveFilterEnabled;
        sdhc_detect_pin[i].config.interrupt = kPortIntDisabled; /* don't use interrupt */
    }

    GPIO_DRV_Init(sdhc_detect_pin, NULL);
}

uint8_t sdhc_detect(void)
{
    uint32_t value = 0;
    if (sdhc_detect_pin[0].config.pullSelect == kPortPullUp) /* pull up */
    {
        value = GPIO_DRV_ReadPinInput(sdhc_detect_pin[0].pinName);
        return (!value);
    }
    else /* pull down */
    {
        return (GPIO_DRV_ReadPinInput(sdhc_detect_pin[0].pinName));
    }
}

uint8_t SD_Init(void)
{
    sdhc_user_config_t config = {0};
    /* enable SDHC module instance */
    CLOCK_SYS_EnableSdhcClock(0);
    /* disable mpu for kSdhcTransModeAdma2 */
#if (TWR_K64F120M || FRDM_K64F || TWR_K60D100M || TWR_K21F120M || TWR_K65F180M)
    MPU_HAL_Disable(MPU);
#endif
    /* initialize user sdhc configuration structure */
    config.transMode = kSdhcTransModeAdma2;  //kSdhcTransModePio;
    config.clock = SDMMC_CLK_100KHZ;
    config.cdType = kSdhcCardDetectGpio;
    /* initialize the SDHC driver with the user configuration */
    if (SDHC_DRV_Init(BOARD_SDHC_INSTANCE, &host, &config) != kStatus_SDHC_NoError)
    {
        USB_PRINTF("SDHC_DRV_Init failed\r\n");
        return 0;
    }

#if defined (FSL_RTOS_MQX)
    OS_install_isr(SDHC_IRQn, SDHC_MQX_IRQHandler, NULL);
#endif
#if (USE_RTOS)
    OS_intr_init(SDHC_IRQn, 5, 0, TRUE);
#endif

    /* A card is detected, now initialize the card driver */
    if (kStatus_SDHC_NoError != SDCARD_DRV_Init(&host, &card))
    {
        USB_PRINTF("SDCARD_DRV_Init failed\r\n");
        SDHC_DRV_Shutdown(card.hostInstance);
        return 0;
    }
    return 1;
}

#endif

/******************************************************************************
 * 
 *    @name       Disk_App
 *    
 *    @brief      
 *                  
 *    @param      None
 * 
 *    @return     None
 *    
 *****************************************************************************/
void Disk_App(void)
{
    /* User Code */
    return;
}

/******************************************************************************
 * 
 *    @name        USB_App_Device_Callback
 *    
 *    @brief       This function handles the callback  
 *                  
 *    @param       pointer : 
 *    @param       event_type : value of the event
 *    @param       val : gives the configuration value 
 * 
 *    @return      None
 *
 *****************************************************************************/
void USB_App_Device_Callback(uint8_t event_type, void* val, void* arg)
{
    if (event_type == USB_DEV_EVENT_BUS_RESET)
    {
        g_disk.start_app = FALSE;
        if (USB_OK == USB_Class_MSC_Get_Speed(g_disk.app_handle, &g_disk.speed))
        {
            USB_Desc_Set_Speed(g_disk.app_handle, g_disk.speed);
        }
    }
    else if (event_type == USB_DEV_EVENT_ENUM_COMPLETE)
    {
        g_disk.start_app = TRUE;
    }
    else if (event_type == USB_DEV_EVENT_ERROR)
    {
        /* add user code for error handling */
    }
    else if (event_type == USB_MSC_DEVICE_GET_SEND_BUFF_INFO)
    {
        if (NULL != val)
        {
#if SD_CARD_APP
            *((uint32_t *)val) = (uint32_t)MSD_SEND_BUFFER_SIZE;
#elif RAM_DISK_APP
            *((uint32_t *)val) = (uint32_t)DISK_SIZE_NORMAL;
#endif
        }
    }
    else if (event_type == USB_MSC_DEVICE_GET_RECV_BUFF_INFO)
    {
        if (NULL != val)
        {
#if SD_CARD_APP
            *((uint32_t *)val) = (uint32_t)MSD_RECV_BUFFER_SIZE;
#elif RAM_DISK_APP
            *((uint32_t *)val) = (uint32_t)DISK_SIZE_NORMAL;
#endif
        }
    }

    return;
}

/******************************************************************************
 * 
 *    @name        USB_App_Class_Callback
 *    
 *    @brief       This function handles the callback  
 *                  
 *    @param       pointer : 
 *    @param       event_type : value of the event
 *    @param       val : gives the configuration value 
 * 
 *    @return      None
 *
 *****************************************************************************/
uint8_t USB_App_Class_Callback
(uint8_t event_type,
    uint16_t value,
    uint8_t ** data,
    uint32_t* size,
    void* arg
) 
{
    lba_app_struct_t* lba_data_ptr;
    uint8_t * prevent_removal_ptr;
    device_lba_info_struct_t* device_lba_info_ptr;
    uint8_t error = USB_OK;
#if SD_CARD_APP && (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
    _mqx_uint error_code = MQX_OK;
#endif
#if SD_CARD_APP && (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
    sdhc_status_t error_code = kStatus_SDHC_NoError;
#endif

    if (g_disk.read_write_error)
    {
        return USBERR_ERROR;
    }

    switch(event_type)
    {
    case USB_DEV_EVENT_DATA_RECEIVED:
        /* Add User defined code -- if required*/
        lba_data_ptr = (lba_app_struct_t*) size;
#if RAM_DISK_APP
#elif SD_CARD_APP
        if (lba_data_ptr->size == 0)
        free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr);
        else
        {
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM)
            SD_Write_Block(lba_data_ptr);
            free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr);
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#if MUTILE_BUFFER && (USE_RTOS)
                    uint32_t msg[3] = {(uint32_t)lba_data_ptr->buff_ptr, lba_data_ptr->offset, lba_data_ptr->size};
            OS_MsgQ_send(write_msg_handler, (void*)(msg), 0);
#else
            error_code = SDCARD_DRV_WriteBlocks(&card, lba_data_ptr->buff_ptr, lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER,
            lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER);
            free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr);
            if (error_code != kStatus_SDHC_NoError)
            {
                error = USBERR_ERROR;
                g_disk.read_write_error=1;
                USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code);
            }
#endif

#else
#if MUTILE_BUFFER
                    uint32_t msg[3] = {(uint32_t)lba_data_ptr->buff_ptr, lba_data_ptr->offset, lba_data_ptr->size};
            OS_MsgQ_send(write_msg_handler, (void*)(msg), 0);
#else
            error_code = sd_write_device_sectors(g_sdcard_handle, (lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER),
            (lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER) , 3, lba_data_ptr->buff_ptr,NULL);
            free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr);
            if (error_code != MQX_OK)
            {
                error = USBERR_ERROR;
                g_disk.read_write_error=1;
                USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code);
            }
#endif
#endif
        }
#endif
        break;
    case USB_DEV_EVENT_SEND_COMPLETE:
        /* Add User defined code -- if required*/
        lba_data_ptr = (lba_app_struct_t*) size;
        if (lba_data_ptr->size != 0)
        {
            /* read data from mass storage device to driver buffer */
#if RAM_DISK_APP
            if(data != NULL)
            {
                *data = g_disk.storage_disk + lba_data_ptr->offset;
            }
#elif SD_CARD_APP
#endif
        }
        break;
    case USB_MSC_START_STOP_EJECT_MEDIA:
        /*  Code to be added by user for starting, stopping or 
         ejecting the disk drive. e.g. starting/stopping the motor in 
         case of CD/DVD*/
        break;
    case USB_MSC_DEVICE_READ_REQUEST:
        lba_data_ptr = (lba_app_struct_t*) size;

#if RAM_DISK_APP
        if(data != NULL)
        {
            *data = g_disk.storage_disk + lba_data_ptr->offset;
        }
#elif SD_CARD_APP
        if(data != NULL)
        {
            *data = g_msc_bulk_in_buff;
        }
        lba_data_ptr->buff_ptr = g_msc_bulk_in_buff;
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM)
        SD_Read_Block(lba_data_ptr);
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#if MUTILE_BUFFER && (USE_RTOS)
        OS_Mutex_lock(sdhc_mutex);
#endif
        error_code = SDCARD_DRV_ReadBlocks(&card, lba_data_ptr->buff_ptr, lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER,
        lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER);
#if MUTILE_BUFFER && (USE_RTOS)
        OS_Mutex_unlock(sdhc_mutex);
#endif
        if (error_code != kStatus_SDHC_NoError)
        {
            error = USBERR_ERROR;
            g_disk.read_write_error=1;
            USB_PRINTF("Read SD card error, error = 0x%x.\tPlease check send buff size(must be less than 128 sectors).\r\n", error_code);
        }
#else
#if MUTILE_BUFFER
        OS_Mutex_lock(sdhc_mutex);
#endif
        error_code = sd_read_device_sectors(g_sdcard_handle, (lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER),
        (lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER) , 3, lba_data_ptr->buff_ptr,NULL);
#if MUTILE_BUFFER
        OS_Mutex_unlock(sdhc_mutex);
#endif
        if (error_code != MQX_OK)
        {
            error = USBERR_ERROR;
            g_disk.read_write_error=1;
            USB_PRINTF("Read SD card error, error = 0x%x.\tPlease check send buff size(must be less than 128 sectors).\r\n", error_code);
        }
#endif
#endif 
        break;
    case USB_MSC_DEVICE_WRITE_REQUEST:
        lba_data_ptr = (lba_app_struct_t*) size;
#if RAM_DISK_APP
        if(data != NULL)
        {
            *data = g_disk.storage_disk + lba_data_ptr->offset;
        }
#elif SD_CARD_APP
        if(data != NULL)
        {
            /* get_write_buffer should return NULL when there are no buffers for writing */
            *data = (uint8_t*)get_write_buffer();
        }
#endif
        break;
    case USB_MSC_DEVICE_FORMAT_COMPLETE:
        break;
    case USB_MSC_DEVICE_REMOVAL_REQUEST:
        prevent_removal_ptr = (uint8_t *) size;
        if (SUPPORT_DISK_LOCKING_MECHANISM)
        {
            g_disk.disk_lock = *prevent_removal_ptr;
        }
        else if ((!SUPPORT_DISK_LOCKING_MECHANISM) && (!(*prevent_removal_ptr)))
        {
            /*there is no support for disk locking and removal of medium is enabled*/
            /* code to be added here for this condition, if required */
        }
        break;
    case USB_MSC_DEVICE_GET_INFO:
        device_lba_info_ptr = (device_lba_info_struct_t*) size;
#if RAM_DISK_APP
        device_lba_info_ptr->total_lba_device_supports = TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL;
        device_lba_info_ptr->length_of_each_lab_of_device = LENGTH_OF_EACH_LAB;
#elif SD_CARD_APP
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM)
        SD_Card_Info(&device_lba_info_ptr->total_lba_device_supports,
        &device_lba_info_ptr->length_of_each_lab_of_device);
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
        device_lba_info_ptr->total_lba_device_supports = card.blockCount;
        device_lba_info_ptr->length_of_each_lab_of_device = card.blockSize;
#else
        sd_get_block_size(g_sdcard_handle, &device_lba_info_ptr->length_of_each_lab_of_device);
        sd_get_blocks_num(g_sdcard_handle, &device_lba_info_ptr->total_lba_device_supports);
#endif
#endif
        device_lba_info_ptr->num_lun_supported = LOGICAL_UNIT_SUPPORTED;
        break;
    default:
        break;
    }

    return error;
}

/******************************************************************************
 *  
 *   @name        TestApp_Init
 * 
 *   @brief       This function is the entry for mouse (or other usage)
 * 
 *   @param       None
 * 
 *   @return      None
 **                
 *****************************************************************************/

void APP_init(void)
{
#if SD_CARD_APP

#if ((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) || (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK))
    g_msc_bulk_in_buff = (uint8_t*)g_msc_bulk_in_buff_temp;
    g_msc_bulk_out_buff = (uint8_t*)g_msc_bulk_out_buff_temp;
#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
    g_msc_bulk_in_buff = (uint8_t*)OS_Mem_alloc_uncached_align(MSD_SEND_BUFFER_SIZE, 32);
    g_msc_bulk_out_buff = (uint8_t*)OS_Mem_alloc_uncached_align(MSD_RECV_BUFFER_SIZE * MSD_RECV_BUFFER_NUM, 32);
#endif

#endif

#if SD_CARD_APP
    USB_PRINTF("Please insert SD card\n");
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM || OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)
#if USE_SDHC_PROTOCOL
    sdhc_detect_io_init();
#endif
    while (!sdhc_detect())
    {
        ;
    } /* SD Card inserted */
    if(!SD_Init()) return; /* Initialize SD_CARD and SPI Interface */
#else
    g_sdcard_handle = sd_init();

#endif
    USB_PRINTF("\nSD card inserted!\n");

#endif
    OS_Mem_zero(&g_disk, sizeof(disk_struct_t));

    OS_Mem_zero(&g_msd_config, sizeof(msc_config_struct_t));

    msc_application_callback.callback = USB_App_Device_Callback;
    msc_application_callback.arg = &g_disk.app_handle;

    /* Register the callbacks to lower layers */
    g_msd_config.msc_application_callback = msc_application_callback;
    g_msd_config.vendor_req_callback = vend_req_callback;
    g_msd_config.class_specific_callback.callback = USB_App_Class_Callback;
    g_msd_config.class_specific_callback.arg = &g_disk.app_handle;
    g_msd_config.desc_callback_ptr = &desc_callback;

    g_disk.speed = USB_SPEED_FULL;
    /* Finally, Initialize the device and USB Stack layers*/
    USB_Class_MSC_Init(CONTROLLER_ID, &g_msd_config, &g_disk.app_handle);

#if SD_CARD_APP
    write_buffer_msg_handler = OS_MsgQ_create((MSD_RECV_BUFFER_NUM + 1), 1);  //msg_size = 4Bytes
    for (int i = 0; i < MSD_RECV_BUFFER_NUM; ++i)
    {
        uint32_t address = (uint32_t)(&g_msc_bulk_out_buff[i * MSD_RECV_BUFFER_SIZE]);
        OS_MsgQ_send(write_buffer_msg_handler, (void*)(&address), 0);
    }
#endif
}

void APP_task()
{
    if (g_disk.read_write_error)
    {
        return;
    }
    Disk_App();
}

#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
/*FUNCTION*----------------------------------------------------------------
 * 
 * Function Name  : Main_Task
 * Returned Value : None
 * Comments       :
 *     First function called.  Calls Test_App
 *     callback functions.
 * 
 *END*--------------------------------------------------------------------*/
void Main_Task
(
    uint32_t param
    )
{
    UNUSED_ARGUMENT (param)
    APP_init();
#if MUTILE_BUFFER
    /* write sdhc task */
    sdhc_mutex = OS_Mutex_create();
    write_msg_handler = OS_MsgQ_create((MSD_RECV_BUFFER_NUM), 3);/* msg_size = 12Bytes */
    OS_Task_create(write_task, NULL, 8L, 2000L, "write_task", NULL);
#endif
}
#endif

/* EOF */