示例#1
0
boolean SEC_GetInput(SEC_Input_t signal)
{
 
   boolean  value=FALSE;    

   if (input_port) {
      switch (signal) {
#ifdef BSP_BUTTON1
         case SEC_DOOR_INPUT:
#ifdef SECEMAIL_TWRMCF51CN_STOP_ENABLED
            lwgpio_set_functionality(&button1, BSP_BUTTON1_MUX_GPIO); /*Switch function as GPIO polling*/
            lwgpio_set_attribute(&button1, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
            value = !lwgpio_get_value(&button1);
            break;
#endif
#ifdef BSP_BUTTON2
         case SEC_WINDOW_INPUT:
#ifdef SECEMAIL_TWRMCF51CN_STOP_ENABLED
            lwgpio_set_functionality(&button2, BSP_BUTTON2_MUX_GPIO);/*Switch function as GPIO polling*/
            lwgpio_set_attribute(&button2, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
            value = !lwgpio_get_value(&button2);
            break;
#endif
      }
   }
   return value;
}
示例#2
0
文件: kbi.c 项目: gxliu/MQX_3.8.0
/******************************************************************************
*   @name        GPIO_Init
*
*   @brief       This function init GPIO
*
*   @return      None
*
*   @comment
*
*******************************************************************************/
void switch_init(void)
{
#ifdef BSP_BUTTON1
   /* opening pins/signals for input */
   if (!lwgpio_init(&btn1, BSP_BUTTON1, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))
   {
      puts("\nSW initialization failed.\n");
      _task_block();
   }

   lwgpio_set_functionality(&btn1, BSP_BUTTON1_MUX_GPIO);
   lwgpio_set_attribute(&btn1, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
#ifdef BSP_BUTTON2
   /* opening pins/signals for input */
   if (!lwgpio_init(&btn2, BSP_BUTTON2, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))
   {
      puts("\nSW initialization failed.\n");
      _task_block();
   }

   lwgpio_set_functionality(&btn2, BSP_BUTTON2_MUX_GPIO);
   lwgpio_set_attribute(&btn2, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
}
示例#3
0
/****************************************************************
*
* Function Name : init_interrupt_btn
* Comments      :
*   Open the pin BSP_BTN1 for input, initialize interrupt and set
*   interrupt handler.
*
*****************************************************************/
void init_interrupt_btn(void * button)
{

    LWGPIO_STRUCT_PTR btn_ptr = (LWGPIO_STRUCT_PTR) button;
    /* opening pins for input */
    if (!lwgpio_init(btn_ptr, BSP_BUTTON1, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))
    {
        printf("Initializing button GPIO as input failed.\n");
        _task_block();
    }
#ifdef BSP_BUTTON1_MUX_IRQ
    lwgpio_set_functionality(btn_ptr, BSP_BUTTON1_MUX_IRQ);
#if defined(BSP_BUTTONS_ACTIVE_HIGH)
    lwgpio_set_attribute(btn_ptr, LWGPIO_ATTR_PULL_DOWN, LWGPIO_AVAL_ENABLE);
#else
    lwgpio_set_attribute(btn_ptr, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
    /* enable gpio functionality for given pin, react on falling edge */
    if (! lwgpio_int_init(btn_ptr, LWGPIO_INT_MODE_FALLING))
    {
        printf("Initializing button GPIO for interrupt failed.\n");
        _task_block();
    }

    /* Install the interrupt directly to the vector table in the processor. */
    if(! _int_install_kernel_isr(lwgpio_int_get_vector(btn_ptr), btn_kernel_isr))
    {
        printf("Install interrupt handler to hardware vector table failed.\n");
        _task_block();
    }

    /* set the interrupt level, and unmask the interrupt in interrupt controller */
    if(MQX_OK != _bsp_int_init(lwgpio_int_get_vector(btn_ptr), 3, 0, TRUE))
    {
        printf("Initialize interrupt failed.\n");
        _task_block();
    }

    /* enable interrupt on GPIO peripheral module */
    lwgpio_int_enable(btn_ptr, TRUE);
#else
    printf("This platform does not support pin mux interrupt function\n");
    _task_block();
#endif /* BSP_BUTTON1_MUX_IRQ */
}
示例#4
0
文件: main.c 项目: BillyZhangZ/wifi
void button_task
(
    uint32_t initial_data
)
{

    LWGPIO_STRUCT   button;
    LWGPIO_VALUE    button_state      = LWGPIO_VALUE_HIGH;
    uint32_t        button_integrator = BUTTON_INTEGRATOR_MAXIMUM;

    /* set the pin to input */
    if (!lwgpio_init(&button, BSP_BUTTON2, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))    {
        /* Button initialization failed. */
        _task_block();
    }

    /* Set multiplexer to GPIO functionality */
    lwgpio_set_functionality(&button, BSP_SW2_MUX_GPIO);
    /* Enable pull up */
    lwgpio_set_attribute(&button, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);

    while(1)
    {

        /* Update the integrator based on the input signal. */
        if (LWGPIO_VALUE_LOW == lwgpio_get_value(&button))        {
            if (button_integrator > 0)  {
                button_integrator--;
            }
        }
        else if (button_integrator < BUTTON_INTEGRATOR_MAXIMUM)    {
            button_integrator++;
        }

        /* Defensive code if integrator got corrupted */
        if (button_integrator > BUTTON_INTEGRATOR_MAXIMUM)
            button_integrator = BUTTON_INTEGRATOR_MAXIMUM;


        /* Post event if integrator reaches limit */
        if ((button_integrator == 0) )   {
            if (button_state == LWGPIO_VALUE_LOW) {
                button_state = LWGPIO_VALUE_HIGH;
                _lwevent_set(&app_event, SW_EVENT_MASK);
            }
        }
        else {
            button_state = LWGPIO_VALUE_LOW;
        }

        _time_delay(BUTTON_SAMPLE_PERIOD_MS);
    }
}
示例#5
0
文件: main.c 项目: BillyZhangZ/wifi
static void button_led_init
    (
        void
    )
{
    static LWGPIO_STRUCT                   sw;
    /* Set the pin to input */
    if (!lwgpio_init(&sw, BSP_SW2, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))
    {
        printf("\nSW initialization failed.\n");
        _task_block();
    }

    /* Set functionality to GPIO mode */
    lwgpio_set_functionality(&sw, BSP_SW2_MUX_GPIO);
    
    /* Enable pull up */
    lwgpio_set_attribute(&sw, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);

    /* Setup the pin interrupt mode */
    if (!lwgpio_int_init(&sw, LWGPIO_INT_MODE_FALLING))
    {
        printf("Initializing SW for interrupt failed.\n");
        _task_block();
    }

    /* Install gpio interrupt service routine */
    _int_install_isr(lwgpio_int_get_vector(&sw), button_isr, (void *) &sw);
    
    /* Set interrupt priority and enable interrupt source in the interrupt controller */
    _bsp_int_init(lwgpio_int_get_vector(&sw), 3, 0, TRUE);
    
    /* Enable interrupt for pin */
    lwgpio_int_enable(&sw, TRUE);

    /* Initialize LED pin for output */
    if (!lwgpio_init(&led1, BSP_LED1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))
    {
        printf("\nLED1 initialization failed.\n");
        _task_block();
    }
    /* Set LED pin to GPIO functionality */
    lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);
}
示例#6
0
/*TASK*-------------------------------------------------------------------
*
* Task Name : spi_read_task func
* Comments  :
*
*END*----------------------------------------------------------------------*/
static void taskRead(uint32_t para)
{

	MQX_FILE_PTR dev_file = (MQX_FILE_PTR)para;
	SPI_CS_CALLBACK_STRUCT callback;
	uint8_t tmp, *buf;
	
	_lwsem_create(&IRQ_SEM, 0);
	lwgpio_init(&SPI_IRQ_PIN, BSP_EM9301_IRQ_PIN, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    lwgpio_set_functionality(&SPI_IRQ_PIN, BSP_EM9301_IRQ_MUX_IRQ);
    lwgpio_set_attribute(&SPI_IRQ_PIN, LWGPIO_ATTR_PULL_DOWN, LWGPIO_AVAL_ENABLE);
    lwgpio_int_init(&SPI_IRQ_PIN, LWGPIO_INT_MODE_RISING);
    _int_install_isr(lwgpio_int_get_vector(&SPI_IRQ_PIN), em9301_isr, &SPI_IRQ_PIN);
    _bsp_int_init(lwgpio_int_get_vector(&SPI_IRQ_PIN), BSP_DSPI_INT_LEVEL, 0, TRUE);
    lwgpio_int_enable(&SPI_IRQ_PIN, TRUE);
	
	for(;;)
	{
		_lwsem_wait(&IRQ_SEM);
		callback.CALLBACK = read_cs_callback;
		callback.USERDATA = dev_file;
		ioctl(dev_file, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback);
		buf = hciBuffer;
		while(lwgpio_get_value(&SPI_IRQ_PIN))
		{
			if(IO_ERROR != fread(&tmp, 1, 1,dev_file))
				*buf++ = tmp;
		}
		fflush(dev_file);
		#if 1
		uint8_t *start = hciBuffer;
		SYSTEM_Log("--Rx:");
		while(start < buf)
		{
			SYSTEM_Log(" %02X", *start++);
		}
		SYSTEM_Log("\n");
		#endif
		}
	
}
示例#7
0
boolean HVAC_InitializeIO(void) 
{
 
/* Init Gpio for Leds as output to drive LEDs (LED10 - LED13) */
#ifdef LED_1
   output_port = lwgpio_init(&led1, LED_1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
   if(!output_port){
      printf("Initializing LWGPIO for LED1 failed.\n");
   }
   lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);
   /*Turn off Led */
   lwgpio_set_value(&led1, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_2
   output_port = lwgpio_init(&led2, LED_2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
   if(!output_port){
      printf("Initializing LWGPIO for LED2 failed.\n");

   }
   lwgpio_set_functionality(&led2, BSP_LED2_MUX_GPIO);
   /*Turn off Led */
   lwgpio_set_value(&led2, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_3
   output_port = lwgpio_init(&led3, LED_3, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
   if(!output_port){
      printf("Initializing LWGPIO for LED3 failed.\n");
   }
   lwgpio_set_functionality(&led3, BSP_LED3_MUX_GPIO);
   /*Turn off Led */
   lwgpio_set_value(&led3, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_4
   output_port = lwgpio_init(&led4, LED_4, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
   if(!output_port){
      printf("Initializing LWGPIO for LED4 failed.\n");
   }
   lwgpio_set_functionality(&led4, BSP_LED4_MUX_GPIO);
   /*Turn off Led */
   lwgpio_set_value(&led4, LWGPIO_VALUE_LOW);
#endif

#ifdef BSP_BUTTON1
    /* Open and set port DD as input to read value from switches */
       input_port = lwgpio_init(&button1, TEMP_PLUS, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if(!input_port)
       {
           printf("Initializing LW GPIO for button1 as input failed.\n");
           _task_block();
       }    
       lwgpio_set_functionality(&button1 ,BSP_BUTTON1_MUX_GPIO);
       lwgpio_set_attribute(&button1, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
       
#ifdef BSP_BUTTON2
       input_port = lwgpio_init(&button2, TEMP_MINUS, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if(!input_port)
       {
           printf("Initializing LW GPIO for button2 as input failed.\n");
           _task_block();
       }
       lwgpio_set_functionality(&button2, BSP_BUTTON2_MUX_GPIO);
       lwgpio_set_attribute(&button2, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
 
#endif
       
#ifdef BSP_BUTTON3
       input_port = lwgpio_init(&button3, TEMP_MINUS, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if(!input_port)
       {
           printf("Initializing LW GPIO for button3 as input failed.\n");
           _task_block();
       }
       lwgpio_set_functionality(&button3, BSP_BUTTON3_MUX_GPIO);
       lwgpio_set_attribute(&button3, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
       
#if BUTTONS
    return (input_port!=0) && (output_port!=0);
#else
   return (output_port!=0);
#endif   
       
}
示例#8
0
boolean SEC_InitializeIO(void)
{

    /* Init Gpio for Leds as output to drive LEDs (LED10 - LED13) */
#ifdef LED_1
       output_port = lwgpio_init(&led1, LED_1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
       if(!output_port){
          printf("Initializing LWGPIO for LED1 failed.\n");
       }
       lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);
       /*Turn off Led */
       lwgpio_set_value(&led1, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_2
       output_port = lwgpio_init(&led2, LED_2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
       if(!output_port){
          printf("Initializing LWGPIO for LED2 failed.\n");
       }
       lwgpio_set_functionality(&led2, BSP_LED2_MUX_GPIO);
       /*Turn off Led */
       lwgpio_set_value(&led2, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_3
       output_port = lwgpio_init(&led3, LED_3, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
       if(!output_port){
          printf("Initializing LWGPIO for LED3 failed.\n");
       }
       lwgpio_set_functionality(&led3, BSP_LED3_MUX_GPIO);
       /*Turn off Led */
       lwgpio_set_value(&led3, LWGPIO_VALUE_LOW);
#endif

#ifdef LED_4
       output_port = lwgpio_init(&led4, LED_4, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
       if(!output_port){
          printf("Initializing LWGPIO for LED3 failed.\n");
       }
       lwgpio_set_functionality(&led4, BSP_LED4_MUX_GPIO);
       /*Turn off Led */
       lwgpio_set_value(&led4, LWGPIO_VALUE_LOW);
#endif

#ifdef BSP_BUTTON1
    /* Open and set port DD as input to read value from switches */
       input_port = lwgpio_init(&button1, DOOR_STATE, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if(!input_port)
       {
           printf("Initializing LW GPIO for button1 as input failed.\n");
           _task_block();
       }    
       #if (defined BSP_BUTTON1_MUX_IRQ && defined SECEMAIL_TWRMCF51CN_STOP_ENABLED)
       lwgpio_set_functionality(&button1 ,BSP_BUTTON1_MUX_IRQ);
       lwgpio_set_attribute(&button1, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
       if (!lwgpio_int_init(&button1, LWGPIO_INT_MODE_RISING))
       {
           printf("Initializing button GPIO for interrupt failed.\n");
       }
       #else 
       lwgpio_set_functionality(&button1 ,BSP_BUTTON1_MUX_GPIO);
       lwgpio_set_attribute(&button1, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
       #endif
       /* install gpio interrupt service routine */
       _int_install_isr(lwgpio_int_get_vector(&button1), kbi_callback, (void *) &button1);
       _bsp_int_init(lwgpio_int_get_vector(&button1), 3, 0, TRUE);
       lwgpio_int_enable(&button1, TRUE);
#endif
       
#ifdef BSP_BUTTON2
       input_port = lwgpio_init(&button2, WINDOW_STATE, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if(!input_port)
       {
           printf("Initializing LW GPIO for button2 as input failed.\n");
           _task_block();
       }
       #if (defined BSP_BUTTON2_MUX_IRQ && defined SECEMAIL_TWRMCF51CN_STOP_ENABLED)
       lwgpio_set_functionality(&button2 ,BSP_BUTTON2_MUX_IRQ);
       lwgpio_set_attribute(&button2, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
       if (!lwgpio_int_init(&button2, LWGPIO_INT_MODE_FALLING))
       {
           printf("Initializing button GPIO for interrupt failed.\n");
       }
       #else 
       lwgpio_set_functionality(&button2 ,BSP_BUTTON2_MUX_GPIO);
       lwgpio_set_attribute(&button2, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
       #endif
       /* install gpio interrupt service routine */
       _int_install_isr(lwgpio_int_get_vector(&button2), kbi_callback, (void *) &button2);
       _bsp_int_init(lwgpio_int_get_vector(&button2), 3, 0, TRUE);
       lwgpio_int_enable(&button2, TRUE);
#endif

    return (input_port!=0) && (output_port!=0);
}
示例#9
0
文件: spi.c 项目: BillyZhangZ/wifi
/*FUNCTION****************************************************************
*
* Function Name    : _io_spi_install
* Returned Value   : MQX error code
* Comments         :
*    Installs SPI device.
*
*END**********************************************************************/
_mqx_int _io_spi_install
    (
        /* [IN] A string that identifies the device for fopen */
        char                       *identifier,

        /* [IN] Pointer to driver initialization data */
        SPI_INIT_STRUCT_CPTR           init_data_ptr
    )
{
    SPI_DRIVER_DATA_STRUCT_PTR driver_data;

    _mqx_int error_code = MQX_OK;

    if ((init_data_ptr->DEVIF->SETPARAM == NULL) || (init_data_ptr->DEVIF->TX_RX == NULL))
    {
        /* Missing mandatory low level driver function */
        return IO_ERROR_DEVICE_INVALID;
    }

    driver_data = (SPI_DRIVER_DATA_STRUCT_PTR)_mem_alloc_system_zero((_mem_size)sizeof(SPI_DRIVER_DATA_STRUCT));
    if (driver_data == NULL)
    {
        return MQX_OUT_OF_MEMORY;
    }
    _mem_set_type(driver_data, MEM_TYPE_IO_SPI_POLLED_DEVICE_STRUCT);

    driver_data->CS_CALLBACK = init_data_ptr->CS_CALLBACK;
    driver_data->CS_USERDATA= init_data_ptr->CS_USERDATA;
    driver_data->PARAMS = init_data_ptr->PARAMS;
    driver_data->DEVIF = init_data_ptr->DEVIF;

    /* initialize low level driver */
    if (driver_data->DEVIF->INIT)
        error_code = driver_data->DEVIF->INIT(init_data_ptr->DEVIF_INIT, &(driver_data->DEVIF_DATA));

    if (error_code != MQX_OK)
    {
        _mem_free(driver_data);
        return error_code;
    }

    _lwsem_create(&driver_data->BUS_LOCK, 1);
/********************************************/
	/*special for em9301 IRQ*/
	_lwsem_create(&driver_data->IRQ_SEM, 0);
	lwgpio_init(&driver_data->SPI_IRQ_PIN, BSP_EM9301_IRQ_PIN, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    lwgpio_set_functionality(&driver_data->SPI_IRQ_PIN, BSP_EM9301_IRQ_MUX_IRQ);
    lwgpio_set_attribute(&driver_data->SPI_IRQ_PIN, LWGPIO_ATTR_PULL_DOWN, LWGPIO_AVAL_ENABLE);
    lwgpio_int_init(&driver_data->SPI_IRQ_PIN, LWGPIO_INT_MODE_RISING);
    _int_install_isr(lwgpio_int_get_vector(&driver_data->SPI_IRQ_PIN), _dspi_em9301_isr, driver_data);
    _bsp_int_init(lwgpio_int_get_vector(&driver_data->SPI_IRQ_PIN), BSP_DSPI_INT_LEVEL, 0, TRUE);
    lwgpio_int_enable(&driver_data->SPI_IRQ_PIN, TRUE);
/********************************************/
    error_code = _io_dev_install_ext(identifier,
        _io_spi_open, _io_spi_close,
        _io_spi_read, _io_spi_write,
        _io_spi_ioctl,
        _io_spi_uninstall,
        (void *)driver_data);

    if (error_code)
    {
        /* deinitialize low level driver */
        if (driver_data->DEVIF->DEINIT)
            driver_data->DEVIF->DEINIT(driver_data->DEVIF_DATA);

        _lwsem_destroy(&driver_data->BUS_LOCK);
        _mem_free(driver_data);
        return error_code;
    }

    return MQX_OK;
}
示例#10
0
/*TASK*-----------------------------------------------------------------
*
* Function Name  : sdcard_task
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void sdcard_task(uint_32 temp)
{
    boolean      inserted = TRUE, readonly = FALSE, last = FALSE;
    _mqx_int     error_code;
    _mqx_uint    param;
    MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partman_handle;
    char         filesystem_name[] = "a:";// 文件系统 名字
    char         partman_name[] = "pm:";// 分区管理 名字
#if defined BSP_SDCARD_GPIO_DETECT
    #if ! BSP_SDCARD_GPIO_DETECT_INT
    LWGPIO_STRUCT      sd_detect;
    #else 
    LWGPIO_STRUCT      sd_detect;
    /************************dx 20121210**********************************************
     */
    GPIO_PIN_STRUCT sd_detect_int[] = {
        BSP_SDCARD_GPIO_DETECT | GPIO_PIN_IRQ_FALLING | GPIO_PIN_IRQ_RISING,
        GPIO_LIST_END
    };
    
    MQX_FILE_PTR port_file_sd_detect_int;
    /***********************************************************************************/
    #endif
    
    
#endif
#if defined BSP_SDCARD_GPIO_PROTECT
    LWGPIO_STRUCT      sd_protect;
#endif
#ifdef BSP_SDCARD_GPIO_CS

    LWGPIO_STRUCT          sd_cs;
    SPI_CS_CALLBACK_STRUCT callback;

#endif
    
    
    /* Open low level communication device */
    com_handle = fopen (SDCARD_COM_CHANNEL, NULL);// 打开"esdhc:"模块底层驱动 

    if (NULL == com_handle)
    {
        printf("Error installing communication handle.\n");
        _task_block();
    }

#ifdef BSP_SDCARD_GPIO_CS

    /* Open GPIO file for SPI CS signal emulation */
    error_code = lwgpio_init(&sd_cs, BSP_SDCARD_GPIO_CS, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于spi片选
    if (!error_code)
       {
           printf("Initializing GPIO with associated pins failed.\n");
           _task_block();
       }
    lwgpio_set_functionality(&sd_cs,BSP_SDCARD_CS_MUX_GPIO);
    lwgpio_set_attribute(&sd_cs, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
    /* Set CS callback */
    callback.MASK = BSP_SDCARD_SPI_CS;
    callback.CALLBACK = set_CS;
    callback.USERDATA = &sd_cs;
    if (SPI_OK != ioctl (com_handle, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback))
    {
        printf ("Setting CS callback failed.\n");
        _task_block();
    }

#endif

#if defined BSP_SDCARD_GPIO_DETECT
    #if ! BSP_SDCARD_GPIO_DETECT_INT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_detect, BSP_SDCARD_GPIO_DETECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于插入检测
       if (!error_code)
       {
           printf("Initializing GPIO with sdcard detect pin failed.\n");
           _task_block();
       }
       /*Set detect and protect pins as GPIO Function */
       lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO);
       lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
   #else 
       /******************dx 20121210***********************************************/
        if (NULL == (port_file_sd_detect_int = fopen("gpio:read", (char_ptr) &sd_detect_int )))
        {
           printf("Opening port_file_sd_detect_int GPIO with associated button1_int failed.\n");
          _task_block();
        }
        ioctl(port_file_sd_detect_int, GPIO_IOCTL_SET_IRQ_FUNCTION, (pointer)sd_detect_int__callback);
         _lwevent_create(&SD_Event,0); // 创建轻量级事件用于检测SD插入
       /****************************************************************************/
    
   #endif
    
      
#endif

#if defined BSP_SDCARD_GPIO_PROTECT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_protect, BSP_SDCARD_GPIO_PROTECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于只读检测
       if (!error_code)
       {
           printf("Initializing GPIO with sdcard protect pin failed.\n");
           _task_block();
       }
       /*Set detect and protect pins as GPIO Function */
       lwgpio_set_functionality(&sd_protect,BSP_SDCARD_PROTECT_MUX_GPIO);
       lwgpio_set_attribute(&sd_protect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif

    /* Install SD card device */
    error_code = _io_sdcard_install("sdcard:", (pointer)&_bsp_sdcard0_init, com_handle);// 安装SD卡驱动,基于"esdhc:"模块
    if ( error_code != MQX_OK )
    {
        printf("Error installing SD card device (0x%x)\n", error_code);
        _task_block();
    }
      /*使用内部IOCTL命令测试SD卡是否已经插入 dx20121223*/
      uint_32  sd_param;
     
      sd_param = 0;
      if (ESDHC_OK != ioctl (com_handle, IO_IOCTL_ESDHC_GET_CARD, &sd_param))
      {
         printf("Error get SD card device type.\n");
          _task_block();
      }
      if (ESDHC_CARD_NONE != sd_param)
      {
         inserted = TRUE; // 已插入SD卡
         printf("inserted = 1 -> %d\n",(int)sd_param);
      }else 
      {
         inserted = FALSE; // 无SD卡
         printf("inserted = 0 -> %d\n",(int)sd_param);
         
      }

    for (;;)/////////////////////////////////////////////////////////////////////////////循环
    {


#if defined BSP_SDCARD_GPIO_PROTECT
        /* Get value of protect pin */
        readonly = lwgpio_get_value(&sd_protect);// 检测磁盘是否只读////////////////////////////////////////
#endif
#ifdef BSP_MPC8308RDB
        /* Set function as SD_CD which indicate that card is present in Present State Register */
        lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_SD_CD);
        lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
        if (last != inserted)
        {
            if (inserted)// 插入磁盘
            {
               // _time_delay (200);// 将活动的任务挂起指定的毫秒数
                /* Open the device which MFS will be installed on */
                sdcard_handle = fopen("sdcard:", 0);// 打开SD卡层驱动
                if ( sdcard_handle == NULL )
                {
                    printf("Unable to open SD card device.\n");
                    _task_block();
                }

                /* Set read only flag as needed */
                param = 0;
                if (readonly)
                {
                    param = IO_O_RDONLY;
                }
                if (IO_OK != ioctl(sdcard_handle, IO_IOCTL_SET_FLAGS, (char_ptr) &param))
                {
                    printf("Setting device read only failed.\n");
                    _task_block();
                }

                /* Install partition manager over SD card driver */
                error_code = _io_part_mgr_install(sdcard_handle, partman_name, 0);// 分区管理
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error installing partition manager: %s\n", MFS_Error_text((uint_32)error_code));
                    _task_block();
                }

                /* Open partition manager */
                partman_handle = fopen(partman_name, NULL);// 打开分区
                if (partman_handle == NULL)
                {
                    error_code = ferror(partman_handle);
                    printf("Error opening partition manager: %s\n", MFS_Error_text((uint_32)error_code));
                    _task_block();
                }

                /* Validate partition 1 */
                param = 1;
                error_code = _io_ioctl(partman_handle, IO_IOCTL_VAL_PART, &param);// 控制分区
                if (error_code == MQX_OK)
                {

                    /* Install MFS over partition 1 */
                    error_code = _io_mfs_install(partman_handle, filesystem_name, param);/////安装文件系统,基于分区
                    if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code));
                        _task_block();
                    }

                } else {

                    /* Install MFS over SD card driver */
                    error_code = _io_mfs_install(sdcard_handle, filesystem_name, (_file_size)0);////////安装文件系统,无分区
                    if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code));
                        _task_block();
                    }

                }

                /* Open file system */
                filesystem_handle = fopen(filesystem_name, NULL);// 打开文件系统
                error_code = ferror (filesystem_handle);
                if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK))
                {
                    printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code));
                    _task_block();
                }
                if ( error_code == MFS_NOT_A_DOS_DISK )
                {
                    printf("NOT A DOS DISK! You must format to continue.\n");
                }

                printf ("SD card installed to %s\n", filesystem_name);
                if (readonly)
                {
                    printf ("SD card is locked (read only).\n");
                }
            }
            else // 拔出磁盘
            {
                /* Close the filesystem */
                if (MQX_OK != fclose (filesystem_handle))
                {
                    printf("Error closing filesystem.\n");
                    _task_block();
                }
                filesystem_handle = NULL;

                /* Uninstall MFS  */
                error_code = _io_dev_uninstall(filesystem_name);
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error uninstalling filesystem.\n");
                    _task_block();
                }

                /* Close partition manager */
                if (MQX_OK != fclose (partman_handle))
                {
                    printf("Unable to close partition manager.\n");
                    _task_block();
                }
                partman_handle = NULL;

                /* Uninstall partition manager  */
                error_code = _io_dev_uninstall(partman_name);
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error uninstalling partition manager.\n");
                    _task_block();
                }

                /* Close the SD card device */
                if (MQX_OK != fclose (sdcard_handle))
                {
                    printf("Unable to close SD card device.\n");
                    _task_block();
                }
                sdcard_handle = NULL;

                printf ("SD card uninstalled.\n");
                printf ("sd unOK dx.\n");/////////////////////////dx
            }
        }

        last = inserted;
       // _time_delay (200);// 将活动的任务挂起指定的毫秒数 
        
//        #if defined BSP_SDCARD_GPIO_DETECT
//            #if BSP_SDCARD_GPIO_DETECT_INT
//            /****************dx 20121211************************************************************/
//             _lwevent_wait_ticks(&SD_Event,SD_EVENT_DETECT,FALSE,0);///////////等待事件USB_EVENT
//             _lwevent_clear(&SD_Event,SD_EVENT_DETECT);
//           /****************************************************************************************/    
//            #endif
//        #endif
      /*使用SD外部端口测试SD卡是否已经插入*/
#if defined BSP_SDCARD_GPIO_DETECT
        #ifdef BSP_MPC8308RDB
        /* Set function as GPIO to detect sdcard */
        lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO);
        lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
        #endif
        #if ! BSP_SDCARD_GPIO_DETECT_INT
        
        inserted = !lwgpio_get_value(&sd_detect);// 检测磁盘是否插入/////////////////////////////////////////
        #else
        /****************dx 20121211************************************************************/
         _lwevent_wait_ticks(&SD_Event,SD_EVENT_DETECT,FALSE,0);///////////等待事件USB_EVENT
          _lwevent_clear(&SD_Event,SD_EVENT_DETECT);
         ioctl(port_file_sd_detect_int, GPIO_IOCTL_READ, (char_ptr) &sd_detect_int);
         inserted = !( sd_detect_int[0] & GPIO_PIN_STATUS);
       /****************************************************************************************/    
        #endif
            
#endif
        
       
    }/////////////////////////////////////////////////////////////////////////////循环
}
示例#11
0
/*TASK*-----------------------------------------------------------------
*
* Function Name  : Sdcard_task
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void Sdcard_task
(
    uint_32 temp
)
{
    boolean      inserted = TRUE, last = FALSE;

    _mqx_int     error_code;

    MQX_FILE_PTR com_handle;

//#if defined BSP_SDCARD_GPIO_DETECT
//    LWGPIO_STRUCT      sd_detect;
//#endif
#if defined BSP_SDCARD_GPIO_PROTECT
    LWGPIO_STRUCT      sd_protect;
#endif

#ifdef BSP_SDCARD_GPIO_CS

    LWGPIO_STRUCT          sd_cs;
    SPI_CS_CALLBACK_STRUCT callback;

#endif
    _task_id player_task_id, sd_walker_id;

    _mqx_int        sd_event_value;
    _mqx_uint       wait_state;

#ifdef USB_ACCESSORY_PLAY
    connect_msg_t msg;
    int delaySetp = 0;
#endif

    if (MQX_OK !=_lwevent_create(&(sddetect_event), LWEVENT_AUTO_CLEAR)) {
        printf("\n_lwevent_create sddetect_event failed\n");
        _task_block();
    }

    /* Open low level communication device */
    com_handle = fopen (SDCARD_COM_CHANNEL, NULL);

    if (NULL == com_handle)
    {
        printf("Error installing communication handle.\n");
        _task_block();
    }

#ifdef BSP_SDCARD_GPIO_CS

    /* Open GPIO file for SPI CS signal emulation */
    error_code = lwgpio_init(&sd_cs, BSP_SDCARD_GPIO_CS, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
    if (!error_code)
    {
        printf("Initializing GPIO with associated pins failed.\n");
        _task_block();
    }
    lwgpio_set_functionality(&sd_cs,BSP_SDCARD_CS_MUX_GPIO);
    lwgpio_set_attribute(&sd_cs, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
    /* Set CS callback */
    callback.MASK = BSP_SDCARD_SPI_CS;
    callback.CALLBACK = set_CS;
    callback.USERDATA = &sd_cs;
    if (SPI_OK != ioctl (com_handle, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback))
    {
        printf ("Setting CS callback failed.\n");
        _task_block();
    }

#endif


#if defined BSP_SDCARD_GPIO_DETECT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_detect, BSP_SDCARD_GPIO_DETECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    if (!error_code)
    {
        printf("Initializing GPIO with sdcard detect pin failed.\n");
        _task_block();
    }
    /*Set detect and protect pins as GPIO Function */
    lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO);
    lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);

#ifndef SD_DETECT_POLLING // init sd detcet pin interrupt
    lwgpio_int_init(&sd_detect,LWGPIO_INT_MODE_RISING | LWGPIO_INT_MODE_FALLING /* LWGPIO_INT_MODE_HIGH*/);     /* falling,raising mode = 3 */

    /* install gpio interrupt service routine */
    _int_install_isr(lwgpio_int_get_vector(&sd_detect), EXT_SDDETECT_ISR, (void *) &sd_detect);
    _bsp_int_init(lwgpio_int_get_vector(&sd_detect), 5, 0, TRUE);

    lwgpio_int_enable(&sd_detect, TRUE);
#endif

#endif

#if defined BSP_SDCARD_GPIO_PROTECT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_protect, BSP_SDCARD_GPIO_PROTECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    if (!error_code)
    {
        printf("Initializing GPIO with sdcard protect pin failed.\n");
        _task_block();
    }
    /*Set detect and protect pins as GPIO Function */
    lwgpio_set_functionality(&sd_protect,BSP_SDCARD_PROTECT_MUX_GPIO);
    lwgpio_set_attribute(&sd_protect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif

    /* Install SD card device */
    error_code = _io_sdcard_install("sdcard:", (pointer)&_bsp_sdcard0_init, com_handle);
    if ( error_code != MQX_OK )
    {
        printf("Error installing SD card device (0x%x)\n", error_code);
        _task_block();
    }

    _lwevent_set(&sddetect_event,SD_ATTACHED_EVENT); // set attached event at mode manager

    _time_delay(1000);   /* wait otg main task ready */
    printf("start sd card task\n");

    // use sd detect interrupt
    for (;;) {
#ifdef SD_DETECT_POLLING
        wait_state = _lwevent_wait_ticks(&sddetect_event,SD_EVENT_MASK, FALSE, 4/* 0*/);
#else
        wait_state = _lwevent_wait_ticks(&sddetect_event,SD_EVENT_MASK, FALSE,   0);
#endif

        //if (wait_state == LWEVENT_WAIT_TIMEOUT/* MQX_OK*/) {
        if (wait_state !=  MQX_OK ) {
#ifndef SD_DETECT_POLLING
            printf("waiting sddetect_event fail\n");
            // _task_block(); // _lwevent_destroy(&sddetect_event);
            //-goto wait_timeout;
            continue;
#else
            _lwevent_set(&sddetect_event,SD_ATTACHED_EVENT);
#endif
        }
        //else

        sd_event_value = _lwevent_get_signalled();

        if (sd_event_value == SD_ATTACHED_EVENT ) {
            _time_delay (200);
            inserted = !lwgpio_get_value(&sd_detect);
            if(!inserted)   // mount sd fs ,must attached sd card !
                continue;

            // printf("mount sd card...\n");
            // mount_sdcard();

#ifndef USB_ACCESSORY_PLAY
            /* create player and sd_walker task*/
            player_task_id = _task_create(0, PLAYER_TASK, 0);
            printf("Creating sd player task................");
            if (player_task_id == MQX_NULL_TASK_ID) {
                printf("[FAIL]\n");
            }
            else {
                printf("[OK]\n");
            }

            sd_walker_id = _task_create(0, SD_WALKER_TASK, 0);
            printf("Creating sd walker task................");
            if (sd_walker_id == MQX_NULL_TASK_ID) {
                printf("[FAIL]\n");
            }
            else {
                printf("[OK]\n");
            }
#else
            msg.conct_source = mp_for_TF;
            msg.conct_action = mp_plugIn;     /* post message,  TFcard plug in*/
            if (LWMSGQ_FULL == _lwmsgq_send(connect_taskq, (uint_32 *) &msg, 0)) {
                printf("Could not inform  about TFCard device attached\n");
            }
            //_time_delay (1); // give mode manager task some times to cancel play ,if sd task high than mode task

#endif
            // _lwevent_set(&player_event, PLAYER_EVENT_MSK_SD_FS_MOUNTED);     //auto play event
            last = inserted;

        } // SD_ATTACHED_EVENT
        else if (sd_event_value == SD_DETTACHED_EVENT ) {
            // _time_delay (100);
            //inserted = !lwgpio_get_value(&sd_detect);
            //if(inserted)
            //    continue;


#ifndef USB_ACCESSORY_PLAY
            _lwevent_set(&player_event, PLAYER_EVENT_MSK_SD_FS_UNMOUNTED);
            _lwevent_wait_ticks(&player_event,
                                PLAYER_EVENT_MSK_PLAYER_TASK_KILLED,
                                TRUE, 0);
            _lwevent_clear(&player_event, PLAYER_EVENT_MSK_PLAYER_TASK_KILLED);
            /* And the destroy play_task and sd_walker task */
            _task_destroy(sd_walker_id);
            _task_destroy(player_task_id);
#else
            /* post message,  TFcard plug out*/
            msg.conct_source = mp_for_TF;
            msg.conct_action = mp_plugOut;     /* post message,  TFcard plug out*/
            if (LWMSGQ_FULL == _lwmsgq_send(connect_taskq, (uint_32 *) &msg, 0))  {
                printf("Could not inform  about TFCard device de-attached\n");
            }
            //_time_delay (1); // give mode manager task some times to cancel play ,if sd task high than mode task

#endif
            // printf("unmount sd card...\n");
            // unmount_sdcard();
            // printf ("SD card uninstalled.\n");
        }

    }

}
示例#12
0
/*TASK*-----------------------------------------------------------------
*
* Function Name  : localPlay_init_task
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void localPlay_init_task
(
    uint_32 temp
)
{
    MQX_TICK_STRUCT time;
    _mqx_int errcode = 0;

    /* Install MQX default unexpected ISR routines */
    _int_install_unexpected_isr();

    if(_lwevent_create(&player_event, 0)!= MQX_OK)
    {
        printf("\nMake event failed");
    }

    /* Setup time */
    printf("Setting up time......................");
    time.TICKS[0] = 0L;
    time.TICKS[1] = 0L;
    time.HW_TICKS = 0;
    _time_set_ticks(&time);
    printf("[OK]\n");

    /* Init GPIOs */
    lwgpio_init(&btn_next, BSP_SW1, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    lwgpio_set_functionality(&btn_next,BSP_SW1_MUX_GPIO);
    lwgpio_set_attribute(&btn_next, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
    lwgpio_int_init(&btn_next, LWGPIO_INT_MODE_RISING);
    _int_install_isr(lwgpio_int_get_vector(&btn_next), int_service_routine_btn_pause, (void *) &btn_next);
    hmi_init();
    sgtl5000_power_on();
    hmi_install(1, int_service_routine_btn_prev);

    _bsp_int_init(lwgpio_int_get_vector(&btn_next), BTN_ISR_PRIORITY, 0, TRUE);

    lwgpio_init(&btn_prev, BSP_SW2, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
    lwgpio_set_functionality(&btn_prev, BSP_SW2_MUX_GPIO);
    lwgpio_set_attribute(&btn_prev, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);

#ifdef SD_DETECT_POLLING
    lwgpio_int_init(&btn_prev, LWGPIO_INT_MODE_RISING);
    _int_install_isr(lwgpio_int_get_vector(&btn_prev), int_service_routine_btn_stop, (void *) &btn_prev);
    _bsp_int_init(lwgpio_int_get_vector(&btn_prev), BTN_ISR_PRIORITY, 0, TRUE);
#endif

    hmi_install(2, int_service_routine_btn_next);



    hmi_install(3, int_service_routine_vol_up);
    hmi_install(4, int_service_routine_vol_down);

#if 0
    /* Initialize audio codec */
    printf("Initializing audio codec.............");


    if (errcode != 0)
    {
        printf("[FAIL]\n");
        printf("  Error 0x%X\n", errcode);
    }
    else
    {
        printf("[OK]\n");
    }

//#else
    /* Initialize audio driver and codec */
    /************************************************/
    if ((errcode = msi_snd_init()) != 0)
    {
        printf("Initializing audio driver and codec........[FAIL]\n");
        printf("  Error 0x%X\n", errcode);
        return;
    }
    else
    {
        printf("Initializing audio driver and codec........[OK]\n");
    }
    /************************************************/
#endif
    /* Create tasks */
    errcode = _task_create(0, SDCARD_TASK, 0);
    printf("Creating SD card task................");
    if (errcode == MQX_NULL_TASK_ID)
    {
        printf("[FAIL]\n");
        printf("  Error 0x%X.\n");
    }
    else
    {
        printf("[OK]\n");
    }
#ifdef SD_PLAYER_SHELL_SUPPORTED
    printf("Creating shell task..................");
    errcode = _task_create(0, SHELL_TASK, 0);
    if (errcode == MQX_NULL_TASK_ID)
    {
        printf("[FAIL]\n");
        printf("  Error 0x%X.\n");
    }
    else
    {
        printf("[OK]\n");
    }
#endif

    lwgpio_int_enable(&btn_next, TRUE);

#ifdef SD_DETECT_POLLING
    lwgpio_int_enable(&btn_prev, TRUE);
#endif
    _task_abort(MQX_NULL_TASK_ID);
}
示例#13
0
文件: demo.c 项目: zhouglu/K60F120M
/*TASK*-----------------------------------------------------------------
*
* Function Name  : sdcard_task
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void sdcard_task(uint_32 temp)
{
    boolean      inserted = TRUE, readonly = FALSE, last = FALSE;
    _mqx_int     error_code;
    _mqx_uint    param;
    MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partition_handle;
    char         filesystem_name[] = "a:";
    char         partman_name[] = "pm:";
    char         partition_name[] = "pm:1";
#if defined BSP_SDCARD_GPIO_DETECT
    LWGPIO_STRUCT      sd_detect;
#endif
#if defined BSP_SDCARD_GPIO_PROTECT
    LWGPIO_STRUCT      sd_protect;
#endif
#ifdef BSP_SDCARD_GPIO_CS

    LWGPIO_STRUCT          sd_cs;
    SPI_CS_CALLBACK_STRUCT callback;

#endif

    /* Open low level communication device */
    com_handle = fopen(SDCARD_COM_CHANNEL, (void *)(SPI_FLAG_FULL_DUPLEX));

    if (NULL == com_handle)
    {
        printf("Error opening communication handle %s, check your user_config.h.\n", SDCARD_COM_CHANNEL);
        _task_block();
    }

#ifdef BSP_SDCARD_GPIO_CS

    /* Open GPIO file for SPI CS signal emulation */
    error_code = lwgpio_init(&sd_cs, BSP_SDCARD_GPIO_CS, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
    if (!error_code)
       {
           printf("Initializing GPIO with associated pins failed.\n");
           _task_block();
       }
    lwgpio_set_functionality(&sd_cs,BSP_SDCARD_CS_MUX_GPIO);
    lwgpio_set_attribute(&sd_cs, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
    /* Set CS callback */
    callback.MASK = BSP_SDCARD_SPI_CS;
    callback.CALLBACK = set_CS;
    callback.USERDATA = &sd_cs;
    if (SPI_OK != ioctl(com_handle, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback))
    {
        printf ("Setting CS callback failed.\n");
        _task_block();
    }

#endif

#if defined BSP_SDCARD_GPIO_DETECT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_detect, BSP_SDCARD_GPIO_DETECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if (!error_code)
       {
           printf("Initializing GPIO with sdcard detect pin failed.\n");
           _task_block();
       }
       /*Set detect and protect pins as GPIO Function */
       lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO);
       lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif

#if defined BSP_SDCARD_GPIO_PROTECT
    /* Init GPIO pins for other SD card signals */
    error_code = lwgpio_init(&sd_protect, BSP_SDCARD_GPIO_PROTECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);
       if (!error_code)
       {
           printf("Initializing GPIO with sdcard protect pin failed.\n");
           _task_block();
       }
       /*Set detect and protect pins as GPIO Function */
       lwgpio_set_functionality(&sd_protect,BSP_SDCARD_PROTECT_MUX_GPIO);
       lwgpio_set_attribute(&sd_protect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif

    /* Install SD card device */
    error_code = _io_sdcard_install("sdcard:", (pointer)&_bsp_sdcard0_init, com_handle);
    if ( error_code != MQX_OK )
    {
        printf("Error installing SD card device (0x%x)\n", error_code);
        _task_block();
    }

    for (;;)
    {

#if defined BSP_SDCARD_GPIO_DETECT
        #ifdef BSP_MPC8308RDB
        /* Set function as GPIO to detect sdcard */
        lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO);
        lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
        #endif
        inserted = !lwgpio_get_value(&sd_detect);
#endif

#if defined BSP_SDCARD_GPIO_PROTECT
        /* Get value of protect pin */
        readonly = lwgpio_get_value(&sd_protect);
#endif
#ifdef BSP_MPC8308RDB
        /* Set function as SD_CD which indicate that card is present in Present State Register */
        lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_SD_CD);
        lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE);
#endif
        if (last != inserted)
        {
            last = inserted;
        	        
            if (inserted)
            {
                _time_delay (200);
                /* Open the device which MFS will be installed on */
                sdcard_handle = fopen("sdcard:", 0);
                if (sdcard_handle == NULL)
                {
                    printf("Unable to open SD card device.\n");
                    continue;
                }

                /* Set read only flag as needed */
                param = 0;
                if (readonly)
                {
                    param = IO_O_RDONLY;
                }
                if (IO_OK != ioctl(sdcard_handle, IO_IOCTL_SET_FLAGS, (char_ptr) &param))
                {
                    printf("Setting device read only failed.\n");
                    continue;
                }

                /* Install partition manager over SD card driver */
                error_code = _io_part_mgr_install(sdcard_handle, partman_name, 0);
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error installing partition manager: %s\n", MFS_Error_text((uint_32)error_code));
                    continue;
                }

                /* Open partition */
                partition_handle = fopen(partition_name, NULL);
                if (partition_handle != NULL)
                {
                    printf("Installing MFS over partition...\n");
                    
                    /* Validate partition */
                    error_code = _io_ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL);
                    if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error validating partition: %s\n", MFS_Error_text((uint_32)error_code));
                        printf("Not installing MFS.\n");
                        continue;
                    }

                    /* Install MFS over partition */
                    error_code = _io_mfs_install(partition_handle, filesystem_name, 0);
                    if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code));
                    }

                } else {

                    printf("Installing MFS over SD card driver...\n");
                	
                    /* Install MFS over SD card driver */
                    error_code = _io_mfs_install(sdcard_handle, filesystem_name, (_file_size)0);
                    if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code));
                    }
                }

                /* Open file system */
                if (error_code == MFS_NO_ERROR) {
                    filesystem_handle = fopen(filesystem_name, NULL);
                    error_code = ferror(filesystem_handle);
                    if (error_code == MFS_NOT_A_DOS_DISK)
                    {
                        printf("NOT A DOS DISK! You must format to continue.\n");
                    }
                    else if (error_code != MFS_NO_ERROR)
                    {
                        printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code));
                        continue;
                    }

                    printf ("SD card installed to %s\n", filesystem_name);
                    if (readonly)
                    {
                        printf ("SD card is locked (read only).\n");
                    }
                }
            }
            else
            {
                /* Close the filesystem */
            	if ((filesystem_handle != NULL) && (MQX_OK != fclose(filesystem_handle)))
                {
                    printf("Error closing filesystem.\n");
                }
            	filesystem_handle = NULL;

                /* Uninstall MFS  */
                error_code = _io_dev_uninstall(filesystem_name);
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error uninstalling filesystem.\n");
                }
                
                /* Close partition */
                if ((partition_handle != NULL) && (MQX_OK != fclose(partition_handle)))
                {
                    printf("Error closing partition.\n");
                }
                partition_handle = NULL;

                /* Uninstall partition manager  */
                error_code = _io_dev_uninstall(partman_name);
                if (error_code != MFS_NO_ERROR)
                {
                    printf("Error uninstalling partition manager.\n");
                }

                /* Close the SD card device */
                if ((sdcard_handle != NULL) && (MQX_OK != fclose(sdcard_handle)))
                {
                    printf("Unable to close SD card device.\n");
                }
                sdcard_handle = NULL;

                printf ("SD card uninstalled.\n");
            }
        }
        
        _time_delay (200);
    }
}