Ejemplo n.º 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;
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
0
void GPIO_init() {

	/******************************************************************************
	       Open the BSP_PINB9 pin as output and drive the output level LOW.
	 ******************************************************************************/
	/* initialize lwgpio handle (CAN_stb)*/
	if (!lwgpio_init(&CAN_stb, BSP_ARDUINO_GPIO2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
	{
		printf("Initializing CAN_stb as output failed.\n");
		_task_block();
	}
	/* swich pin functionality (MUX) to GPIO mode */
	lwgpio_set_functionality(&CAN_stb, BSP_ARDUINO_GPIO2_MUX_GPIO);

	/* write logical 0 to the pin */
	lwgpio_set_value(&CAN_stb, LWGPIO_VALUE_LOW); /* set pin to 0 */

	/******************************************************************************
		       Open the BSP_LED1 pin as output
	 ******************************************************************************/

	/* initialize lwgpio handle (led1) for BSP_LED1 pin
	 * (defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file)
	 */
	if (!lwgpio_init(&led1, BSP_LED1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
	{
		printf("Initializing LED1 GPIO as output failed.\n");
		_task_block();
	}
	/* swich pin functionality (MUX) to GPIO mode */
	lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);
}
Ejemplo n.º 4
0
void Dio_Init(void)
{
	printf("Init LEDs...\n");
    if(FALSE == boLedPortsInit)
    {
    	boLedPortsInit = TRUE;
		if (!lwgpio_init(&stLedRed, BSP_RGBRED, LWGPIO_DIR_OUTPUT, PORT_OFF))
		{
			printf("...RED LED failed!!\n");
		}else
		{
			boRedInit= TRUE;
			printf("R");
			lwgpio_set_functionality(&stLedRed, BSP_RGBRED_MUX_GPIO);
		}
		/*********************************************************************************/
		if (!lwgpio_init(&stLedGreen, BSP_RGBGREEN, LWGPIO_DIR_OUTPUT, PORT_OFF))
		{
			printf("...GREEN LED failed!!\n");
		}else
		{
			boGreenInit = TRUE;
			printf("G");
			lwgpio_set_functionality(&stLedGreen, BSP_RGBGREEN_MUX_GPIO);
		}
		/*********************************************************************************/
		if (!lwgpio_init(&stLedBlue, BSP_RGBBLUE, LWGPIO_DIR_OUTPUT, PORT_OFF))
		{
			printf("...BLUE LED failed!!\n");
		}else
		{
			boBlueInit = TRUE;
			printf("B");
			lwgpio_set_functionality(&stLedBlue, BSP_RGBBLUE_MUX_GPIO);
		}
		if( (TRUE == boRedInit) && (TRUE == boGreenInit) && (TRUE == boBlueInit) )
		{
			printf("...READY!!\n");
		}
		else
		{
			printf("...ERROR on RGB!!\n");
			_time_delay(200);
		}
    }
    else
    {
    	/* do nothing */
    }
}
Ejemplo n.º 5
0
void for_loop_led_task
    (
        uint_32 initial_data
    )
{
    uint_32 i = 0;
    LWGPIO_STRUCT led2;

    /* Initialize LED pin for output */
    if (!lwgpio_init(&led2, BSP_LED2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))
    {
        printf("\nLED2 initialization failed.\n");
        _task_block();
    }
    /* Set LED pin to GPIO functionality */
    lwgpio_set_functionality(&led2, BSP_LED2_MUX_GPIO);

    while(1)
    {
        for (i = 0; i < 800000; i++) {};
        
        /* Toggle led 2 */
        lwgpio_toggle_value(&led2);
    }
}
Ejemplo n.º 6
0
void for_loop_led_task
    (
        uint32_t initial_data
    )
{
    volatile uint32_t i = 0;
    LWGPIO_STRUCT led2;

    /* Initialize LED pin for output */
    if (!lwgpio_init(&led2, BSP_LED2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))
    {
        printf("\nLED2 initialization failed.\n");
        _task_block();
    }
    /* Set LED pin to GPIO functionality */
    lwgpio_set_functionality(&led2, BSP_LED2_MUX_GPIO);

    while(1)
    {
        /* Duration of this loop depends on actual core clock */
        for (i = 0; i < 800000; i++) {};
        
        /* To let the idle task run too */
        _time_delay (50);
        
        /* Toggle led 2 */
        lwgpio_toggle_value(&led2);
    }
}
Ejemplo n.º 7
0
static _mqx_int write_cs_callback(uint32_t pre_cfg_mask, void *user_data)
{
	VDSPI_REG_STRUCT_PTR               dspi_ptr;
	LWGPIO_STRUCT  em9301_spi_mosi;
	if(pre_cfg_mask & PRE_CONFIGURE_FLAG)
	{
		dspi_ptr = _bsp_get_dspi_base_address(1);
		dspi_ptr->MCR = (dspi_ptr->MCR & ~(uint32_t)DSPI_MCR_PCSIS_MASK) | DSPI_MCR_PCSIS(0xFF);
		/*MOSI set pin*/
		lwgpio_init(&em9301_spi_mosi, BSP_EM9301_MOSI_PIN, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
		lwgpio_set_functionality(&em9301_spi_mosi, 1);
		lwgpio_set_value(&em9301_spi_mosi, 1);
		/*Assert CS pin*/
		dspi_ptr->MCR = (dspi_ptr->MCR & ~(uint32_t)DSPI_MCR_PCSIS_MASK) | DSPI_MCR_PCSIS(~0x01);
		lwgpio_set_functionality(&em9301_spi_mosi, 7);
	}
	return MQX_OK;
}
Ejemplo n.º 8
0
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);
}
Ejemplo n.º 9
0
static _mqx_int read_cs_callback(uint32_t pre_cfg_mask, void *user_data)
{		
	
	BleStatus				status;
	MQX_FILE_PTR					dev = (MQX_FILE_PTR)user_data; 
	SPI_DEV_DATA_STRUCT_PTR			dev_data = (SPI_DEV_DATA_STRUCT_PTR)(dev->DEV_DATA_PTR);

	U8 byteReceievd;
	VDSPI_REG_STRUCT_PTR               dspi_ptr;  
	LWGPIO_STRUCT  em9301_spi_mosi;
	if(pre_cfg_mask & PRE_CONFIGURE_FLAG)
	{
		dspi_ptr = _bsp_get_dspi_base_address(1);    
		dspi_ptr->MCR = (dspi_ptr->MCR & ~(uint32_t)DSPI_MCR_PCSIS_MASK) | DSPI_MCR_PCSIS(0xFF);
		lwgpio_init(&em9301_spi_mosi, BSP_EM9301_MOSI_PIN, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
		lwgpio_set_functionality(&em9301_spi_mosi, 1);
		lwgpio_set_value(&em9301_spi_mosi, 0);		
		/*Assert CS pin*/ 	
		dspi_ptr->MCR |= DSPI_MCR_MSTR_MASK;	
		dspi_ptr->MCR = (dspi_ptr->MCR & ~(uint32_t)DSPI_MCR_PCSIS_MASK) | DSPI_MCR_PCSIS(~0x01);
		lwgpio_set_functionality(&em9301_spi_mosi, 7);	
	}
	else
	{
		if(dev_data->STATS.RX_PACKETS)
		{
			if(dev_data->STATS.RX_PACKETS >= EM9301_READ_BUFFER_SIZE)
			{
				status = BLETRANSPORT_UartDataReceived(hciBuffer, dev_data->STATS.RX_PACKETS);
				if(status == BLESTATUS_FAILED)
				{
					BLEUART_Deinit();
					return BLESTATUS_FAILED;					
				}			
			}
			byteReceievd = dev_data->STATS.RX_PACKETS;
			status = BLETRANSPORT_UartDataReceived(hciBuffer, byteReceievd);			
			if(SPI_OK != ioctl(dev, IO_IOCTL_SPI_CLEAR_STATS, 0))
				status = BLESTATUS_FAILED;
		}			
        
	}	
	return status;
}
Ejemplo n.º 10
0
void InitializeIO()
{
	LWGPIO_STRUCT led;
	
	if(!lwgpio_init(&led, BSP_LED1,LWGPIO_DIR_OUTPUT,LWGPIO_VALUE_HIGH ))
	{ 
		printf("Initializing LED1 GPIO as output failed.\n"); 
	} 
	else 
	{ 
		// switch pin functionality (MUX) to GPIO mode
		lwgpio_set_functionality(&led, BSP_LED1_MUX_GPIO); 
	}
	
	if(!lwgpio_init(&led, BSP_LED2 ,LWGPIO_DIR_OUTPUT,LWGPIO_VALUE_HIGH ))
	{ 
		printf("Initializing LED2 GPIO as output failed.\n"); 
	} 
	else 
	{ 
		// switch pin functionality (MUX) to GPIO mode
		lwgpio_set_functionality(&led, BSP_LED2_MUX_GPIO); 
	}
	
	if (!lwgpio_init(&led, BSP_LED3, LWGPIO_DIR_OUTPUT,	LWGPIO_VALUE_HIGH)) 
	{ 
		printf("Initializing LED3 GPIO as output failed.\n"); 
	} 
	else 
	{ 
		// switch pin functionality (MUX) to GPIO mode
		lwgpio_set_functionality(&led, BSP_LED3_MUX_GPIO); 
	} 
	if (!lwgpio_init(&led, BSP_LED4, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH)) 
	{ 
		printf("Initializing LED4 GPIO as output failed.\n"); 
	} 
	else 
	{ 
		// switch pin functionality (MUX) to GPIO mode
		lwgpio_set_functionality(&led, BSP_LED4_MUX_GPIO); 
	} 
} // initializeIO
Ejemplo n.º 11
0
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);
    }
}
Ejemplo n.º 12
0
/*******************************************************************
*
* Function Name : init_led.
* Comments      :
*   Open the BSP_LED1 pin as output and drive the output level high.
*
********************************************************************/
void init_led(void * led)
{
    LWGPIO_STRUCT_PTR led_ptr = (LWGPIO_STRUCT_PTR)led;
    if (!lwgpio_init(led_ptr, BSP_LED1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
    {
        printf("Initializing LED1 GPIO as output failed.\n");
        _task_block();
    }
    /* swich pin functionality (MUX) to GPIO mode */
    lwgpio_set_functionality(led_ptr, BSP_LED1_MUX_GPIO);

    /* write logical 1 to the pin */
    lwgpio_set_value(led_ptr, LWGPIO_VALUE_HIGH);
}
Ejemplo n.º 13
0
static inline void init_set_vbus_low(LWGPIO_STRUCT * vbus){

    //LWGPIO_STRUCT vbus;
    _time_delay(50);

    /* initialize lwgpio handle vbus for PORTB 8 pin */
    if (!lwgpio_init(vbus, VBUSPIN, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
    {
        printf("Initializing VBUS PORTB 8 as GPIO output failed.\n");
        _task_block();
    }
    /* swich pin functionality (MUX) to GPIO mode */
    lwgpio_set_functionality(vbus, VBUS_MUX_GPIO);

    /* write logical 0 to the pin */
    lwgpio_set_value(vbus, LWGPIO_VALUE_LOW); /* set pin to 0 */
}
Ejemplo n.º 14
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 */
}
static uint32_t init_IRQ_interrupt(LWGPIO_STRUCT_PTR irq_pin, INT_ISR_FPTR isr) {
    /* Configure GPIO for nRF24L01 module */
    if (FALSE == lwgpio_init(irq_pin,  nRF24L01_IRQ_PIN, 
                                LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE)) {
        return MQX_ERROR;
    }

    /* swich pin functionality (MUX) to GPIO mode */
    lwgpio_set_functionality(irq_pin, nRF24L01_IRQ_PIN_MUX);

    if(!lwgpio_int_init(irq_pin, LWGPIO_INT_MODE_FALLING)) {
        return MQX_ERROR;
    }

    _int_install_isr(lwgpio_int_get_vector(irq_pin), isr, irq_pin);

    lwgpio_int_enable(irq_pin, TRUE);

    return _bsp_int_init(lwgpio_int_get_vector(irq_pin), 4, 0, TRUE);
}
Ejemplo n.º 16
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
		}
	
}
Ejemplo n.º 17
0
/*TASK**************************************************************************
*
* Function Name   : led_blink
*
* Input Params    : None
*
* Returned Value  : None
*
* Comments        : Perform blink BSP_LED1 led when the board is connected to a USB charger
*
*END***************************************************************************/
void led_blink()
{
   LWGPIO_STRUCT Charging_led;
   uint_32       value = 1;
   lwgpio_init(&Charging_led, CHARGING_LED, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);
   lwgpio_set_functionality(&Charging_led, CHARGING_LED_MUX_GPIO);
   lwgpio_set_value(&Charging_led, LWGPIO_VALUE_HIGH);
   while (1)
   {
      if (Vbus_flag)
      {
         lwgpio_set_value(&Charging_led, value);
         _time_delay (500);
         value ^= 1;
      }
      else
      {
         lwgpio_set_value(&Charging_led, LWGPIO_VALUE_HIGH);
      }
   }
}
Ejemplo n.º 18
0
/*FUNCTION*---------------------------------------------------------------------
*
* Function Name    : _bsp_flexbus_lcd_setup
* Returned Value   :
* Comments         :
*    Setup FlexBus for LCD operation
*
*END*-------------------------------------------------------------------------*/
void _bsp_flexbus_lcd_setup (const uint_32 base_address)
{
    FB_MemMapPtr fb_ptr = FB_BASE_PTR;
    LWGPIO_STRUCT ale_pin;

    /* Enable external LCD mapped on CS0 */
    fb_ptr->CS[0].CSAR = base_address;
    fb_ptr->CS[0].CSCR = FB_CSCR_BLS_MASK |
                         FB_CSCR_AA_MASK  |
                         FB_CSCR_PS(2)    |
                         FB_CSCR_BEM_MASK;

    /*
     * The address range is set to 128K because
     * the DC signal is connected on address wire
     */
    fb_ptr->CS[0].CSMR = FB_CSMR_BAM(1) | FB_CSMR_V_MASK;

    lwgpio_init(&ale_pin, GPIO_PORT_E | GPIO_PIN6, LWGPIO_DIR_NOCHANGE, LWGPIO_VALUE_NOCHANGE);
    lwgpio_set_direction(&ale_pin, LWGPIO_DIR_OUTPUT);
    lwgpio_set_value(&ale_pin, LWGPIO_VALUE_HIGH);
    lwgpio_set_functionality(&ale_pin, 1);
}
Ejemplo n.º 19
0
void for_loop_led_task
(
    uint32_t initial_data
)
{
    LWGPIO_STRUCT   led2;
    int             i = 0;

    /* Initialize LED pin for output */
    if (!lwgpio_init(&led2, BSP_LED2, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))
    {
        /* LED2 initialization failed.*/
        _task_block();
    }
    /* Set LED pin to GPIO functionality */
    lwgpio_set_functionality(&led2, BSP_LED2_MUX_GPIO);

    while(1)
    {
        for (i = 0; i < 700000; i++) {};
        /* toggle led 2 */
        lwgpio_toggle_value(&led2);
    }
}
Ejemplo n.º 20
0
void led_task
(
    uint32_t initial_data
)
{
    LWGPIO_STRUCT led1;

    /* Initialize LED pin for output */
    if (!lwgpio_init(&led1, BSP_LED1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))    {
        /* LED1 initialization failed. */
        _task_block();
    }

    /* Set LED pin to GPIO functionality */
    lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);

    while(1)
    {
        _time_delay (500);
        /* toggle led 1 */
        lwgpio_toggle_value(&led1);
    }

}
Ejemplo n.º 21
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, 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);
    }
}
Ejemplo n.º 22
0
int adcc_init(int adc_num)
{
    //PORT_MemMapPtr pctl = NULL;
    //uint8_t gpio_port;

	/* led pin init */
    if(!lwgpio_init(&pm2p5_ledgpio, PM2P5_LED_PWREN, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE)) {
        printf("Initializing GPIO with associated pins failed.\n");
        _time_delay (200L);
        _task_block();
    }
    lwgpio_set_functionality(&pm2p5_ledgpio, PM2P5_LED_MUX_GPIO);
    //lwgpio_set_value(&pm2p5_ledgpio, LWGPIO_VALUE_LOW);
    lwgpio_set_value(&pm2p5_ledgpio, LWGPIO_VALUE_HIGH);
    
	if(adc_num == 0) {
		/* SIM_SCGC6: ADC0=1 */
		adc_ptr = ADC0_BASE_PTR;
		SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;
	}
	else {
		adc_ptr = ADC1_BASE_PTR;
		SIM_SCGC6 |= SIM_SCGC6_ADC1_MASK;
	}

#ifdef ADC_HW_TRIGER
    SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;                 /* enable PIT */

    SIM_SOPT7 |= SIM_SOPT7_ADC1ALTTRGEN_MASK;
    SIM_SOPT7 |= (SIM_SOPT7_ADC1PRETRGSEL_MASK);     /* Pre Trigger B */
    SIM_SOPT7 &= ~(SIM_SOPT7_ADC1TRGSEL_MASK); 
    SIM_SOPT7 |= SIM_SOPT7_ADC1TRGSEL(7);            /* PIT trigger 3 */
    PIT_MCR_REG(PIT_BASE_PTR) = 0;
#endif

	// pin mux config : spec P216 ADC0_DP0/ADC1_DP3 is default
	/* set pin's multiplexer to analog
    gpio_port = ADC_SIG_PORTB | 4;
    pctl = (PORT_MemMapPtr) PORTB_BASE_PTR;
    pctl->PCR[gpio_port & 0x1F] &= ~PORT_PCR_MUX_MASK; 
	*/

    // Initialize ADC0/1 =================================================================================
    //averages (4 h/w)   // bus 60M /8/2 = 3.75M ADC clock
    ADC_CFG1_REG(adc_ptr) = ADLPC_NORMAL | ADC_CFG1_ADIV(ADIV_8) /*| ADLSMP_LONG*/ | ADC_CFG1_MODE(MODE_16)
      | ADC_CFG1_ADICLK(/*ADICLK_BUS_2*/ADICLK_BUS);

    // do calibration
    if(adc_calibrate(adc_ptr))
        printf("adc-%x calibrate failed\n",(int)adc_ptr);

    /* channel A/B selected */
#ifdef ADC_HW_TRIGER
    ADC_CFG2_REG(adc_ptr)  = MUXSEL_ADCB | ADACKEN_ENABLED | ADHSC_HISPEED | ADC_CFG2_ADLSTS(ADLSTS_20);
#else
	ADC_CFG2_REG(adc_ptr)  = MUXSEL_ADCA /*| ADACKEN_ENABLED */| ADHSC_HISPEED | ADC_CFG2_ADLSTS(ADLSTS_20);
#endif
	
    //ADC_CV1_REG(adc_ptr) = 0x0;
    //ADC_CV2_REG(adc_ptr) = 0x0;
        
    //ADC_SC1_REG(adc_ptr,0) =  AIEN_ON | DIFF_SINGLE | ADC_SC1_ADCH(ADC_INPUT_CH);   /* SC1 A */
    //ADC_SC1_REG(adc_ptr,1) =  AIEN_ON | DIFF_SINGLE | ADC_SC1_ADCH(ADC_INPUT_CH);   /* SC1 B */

#ifdef ADC_HW_TRIGER
    ADC_SC2_REG(adc_ptr) = ADTRG_HW /*ADTRG_SW*/ | ACFE_DISABLED | ACFGT_GREATER | ACREN_DISABLED
                     | DMAEN_DISABLED /* DMAEN_ENABLED*/| ADC_SC2_REFSEL(REFSEL_EXT);
#else
    ADC_SC2_REG(adc_ptr) = ADTRG_SW /*| ACFE_DISABLED | ACFGT_GREATER | ACREN_DISABLED
                     | DMAEN_DISABLED*/ | ADC_SC2_REFSEL(REFSEL_EXT);
#endif

    ADC_SC3_REG(adc_ptr) = /*ADC_SC3_CALF_MASK |*/ CAL_OFF | ADCO_SINGLE /*| AVGE_ENABLED | ADC_SC3_AVGS(AVGS_4)*/;  /* averages 4 h/w */
}
uint32_t nRF24L01_Init (nRF24L01_STRUCT_PTR nRF_PTR){
    uint32_t config_parameter;

    /* Invalid argument */
    if (nRF_PTR == NULL) {
        return MQX_INVALID_POINTER;
    }

    /* Open SPI driver module */
    nRF_PTR->spi_ptr = fopen(nRF24L01_SPI_PORT,NULL);
    if (NULL == nRF_PTR->spi_ptr) {
        return MQX_INVALID_DEVICE;
    }

    /* Configure GPIO for nRF24L01 module */
    if (FALSE == lwgpio_init(nRF_PTR->CE,  nRF24L01_CE_PIN, 
                                LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE)) {
        return MQX_ERROR;
    }

    /* swich pin functionality (MUX) to GPIO mode */
    lwgpio_set_functionality(nRF_PTR->CE, nRF24L01_CE_PIN_MUX);
    

    nRF24L01_pin_low(nRF_PTR->CE);

    if (FALSE == lwgpio_init(nRF_PTR->CSN,  nRF24L01_CSN_PIN, 
                                LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE)) {
        return MQX_ERROR;
    }
    /* swich pin functionality (MUX) to GPIO mode */
    lwgpio_set_functionality(nRF_PTR->CSN, nRF24L01_CSN_PIN_MUX);
    
    nRF24L01_pin_high(nRF_PTR->CSN);

    if (MQX_OK != _lwevent_create(&IRQ_EV_STRUCT, 0)) {
        return MQX_ERROR;
    }
    
    if(MQX_OK != init_IRQ_interrupt(nRF_PTR->IRQ, irq_isr)){
        return MQX_ERROR;
    }

    /* Configure SPI driver according nRF24L01 specifications */
    /* Set a different rate */
    config_parameter = nRF24L01_PORT_BAUD_RATE;
    if (SPI_OK != ioctl (nRF_PTR->spi_ptr, 
                            IO_IOCTL_SPI_SET_BAUD, 
                                &config_parameter)) {
       return SPI_ERROR_BAUD_RATE_INVALID;
    }
#if PRINT_SPI_CONFIG
    /* Display baud rate */
    printf ("Current baud rate ... ");
    if (SPI_OK == ioctl (nRF_PTR->spi_ptr, 
                            IO_IOCTL_SPI_GET_BAUD, 
                                &config_parameter)) {
       printf ("%d Hz\n", config_parameter);
    } else {
       printf ("ERROR\n");
    }
#endif

    /* Set clock mode */
    config_parameter = SPI_CLK_POL_PHA_MODE0;
    if (SPI_OK != ioctl (nRF_PTR->spi_ptr,
                            IO_IOCTL_SPI_SET_MODE, 
                                &config_parameter)) {
       return SPI_ERROR_MODE_INVALID;
    }

    /* Set big endian */
    config_parameter = SPI_DEVICE_BIG_ENDIAN;
    if (SPI_OK != ioctl (nRF_PTR->spi_ptr,
                            IO_IOCTL_SPI_SET_ENDIAN,
                                &config_parameter)) {
       return SPI_ERROR_ENDIAN_INVALID;
    }

#if PRINT_SPI_CONFIG
    /* Get endian */
    printf ("Getting endianness ... ");
    if (SPI_OK == ioctl (nRF_PTR->spi_ptr,
                            IO_IOCTL_SPI_GET_ENDIAN,
                                &config_parameter)) {
       printf ("%s\n", config_parameter == SPI_DEVICE_BIG_ENDIAN ? 
                       "SPI_DEVICE_BIG_ENDIAN" : "SPI_DEVICE_LITTLE_ENDIAN");
    } else {
       printf ("ERROR\n");
    }
#endif

    /* Set transfer mode */
    config_parameter = SPI_DEVICE_MASTER_MODE;
    if (SPI_OK != ioctl (nRF_PTR->spi_ptr,
                            IO_IOCTL_SPI_SET_TRANSFER_MODE,
                                &config_parameter)) {
       return SPI_ERROR_MODE_INVALID;
    }

#if PRINT_SPI_CONFIG
    /* Get transfer mode */
    printf ("Getting transfer mode ... ");
    if (SPI_OK == ioctl (nRF_PTR->spi_ptr,
                            IO_IOCTL_SPI_GET_TRANSFER_MODE,
                                &config_parameter)) {
       printf ("%s\n", config_parameter == SPI_DEVICE_MASTER_MODE ? 
               "SPI Master Mode" : "SPI Device Mode");
    } else {
       printf ("ERROR\n");
    }
#endif
    return MQX_OK;
    }
Ejemplo n.º 24
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);
}
Ejemplo n.º 25
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");
        }

    }

}
Ejemplo n.º 26
0
uint32_t OS_Gpio_set_functionality(os_gpio_handle handle, uint32_t function)
{
    LWGPIO_STRUCT_PTR    pGpio = (LWGPIO_STRUCT_PTR)handle;
    lwgpio_set_functionality(pGpio, function);
    return OS_GPIO_OK;
}
Ejemplo n.º 27
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);
}
Ejemplo n.º 28
0
/*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;
}
Ejemplo n.º 29
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   
       
}
Ejemplo n.º 30
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
        
       
    }/////////////////////////////////////////////////////////////////////////////循环
}