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; }
boolean HVAC_GetInput(HVAC_Input_t signal) { boolean value=FALSE; if (input_port){ switch (signal) { #ifdef BSP_BUTTON1 case HVAC_TEMP_UP_INPUT: value = !lwgpio_get_value(&button1); break; #endif #ifdef BSP_BUTTON2 case HVAC_TEMP_DOWN_INPUT: value = !lwgpio_get_value(&button2); break; #endif #if defined(FAN_ON_OFF) case HVAC_FAN_ON_INPUT: value = !lwgpio_get_value(&button3); break; #endif } } return value; }
/****************************************************************************** * @name polling_switch * * @brief This function poll the button * * @return None * * @comment * *******************************************************************************/ void polling_switch (void) { static uint_32 switch_state = 1; #ifdef BSP_BUTTON1 /* Test if SW1 was pressed */ if (LWGPIO_VALUE_LOW == lwgpio_get_value(&btn1)) { if ((switch_state & SW1_EVENT_MASK) == 0) { switch_state |= SW1_EVENT_MASK; mute_flag = !mute_flag; } } else { switch_state &= ~SW1_EVENT_MASK; } #endif #ifdef BSP_BUTTON2 /* Test if SW2 was pressed */ if (LWGPIO_VALUE_LOW == lwgpio_get_value(&btn2)) { if ((switch_state & SW2_EVENT_MASK) == 0) { switch_state |= SW2_EVENT_MASK; process_play_button(); } } else { switch_state &= ~SW2_EVENT_MASK; } #endif }
/*FUNCTION***************************************************************** * * Function Name : LWGPIO_GET_PIN_INPUT * Returned Value : LWGPIO_VALUE, LWGPIO_VALUE_NOCHANGE if error * Comments : * Gets input pin state * *END*********************************************************************/ LWGPIO_VALUE lwgpio_get_pin_input(LWGPIO_PIN_ID id) { LWGPIO_STRUCT tmp; if (lwgpio_init(&tmp, id, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE)) { return lwgpio_get_value(&tmp); } return LWGPIO_VALUE_NOCHANGE; }
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); } }
boolean SEC_GetInput(SEC_Input_t signal) { boolean value=FALSE; #if BUTTONS if (input_port) { switch (signal) { #ifdef BSP_BUTTON1 case SEC_DOOR_INPUT: value = !lwgpio_get_value(&button1); break; #endif #ifdef BSP_BUTTON2 case SEC_WINDOW_INPUT: value = !lwgpio_get_value(&button2); break; #endif } } #endif /* BUTTONS */ return value; }
void EXT_SDDETECT_ISR(void *pin) { int sd_pin; boolean inserted; // if(sd_pin = lwgpio_int_get_flag((LWGPIO_STRUCT_PTR) pin)) { inserted = !lwgpio_get_value((LWGPIO_STRUCT_PTR) pin); // printf("sd detect pin changed %d,insert state %d,cur_musicPlayMS %d\n",sd_pin,inserted,cur_musicPlayMS); //if(cur_musicPlayMS != mp_sBT) { if(inserted) _lwevent_set(&sddetect_event,SD_ATTACHED_EVENT); else _lwevent_set(&sddetect_event,SD_DETTACHED_EVENT); //} lwgpio_int_clear_flag((LWGPIO_STRUCT_PTR) pin); // } }
/*FUNCTION**************************************************************** * * Function Name : _io_spi_read * Returned Value : Number of bytes read * Comments : * POSIX wrapper to perform SPI transfer storing received data into the buffer. * Returns number of bytes received. * *END*********************************************************************/ static _mqx_int _io_spi_read ( /* [IN] The handle returned from _fopen */ MQX_FILE_PTR fd_ptr, /* [OUT] Where the characters are to be stored */ char *data_ptr, /* [IN] The number of bytes to read */ _mqx_int n ) { #if 0 //SPI_DEV_DATA_STRUCT_PTR dev_data; SPI_DRIVER_DATA_STRUCT_PTR driver_data; _mqx_int num = n; //dev_data = (SPI_DEV_DATA_STRUCT_PTR)(fd_ptr->DEV_DATA_PTR); char tmp; driver_data = (SPI_DRIVER_DATA_STRUCT_PTR)(fd_ptr->DEV_PTR->DRIVER_INIT_PTR); _lwsem_wait(&driver_data->IRQ_SEM); /*add callback function here before real read*/ SPI_CS_CALLBACK_STRUCT callback; callback.CALLBACK = read_cs_callback; callback.USERDATA = fd_ptr; _io_spi_ioctl(fd_ptr, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback); while(lwgpio_get_value(&driver_data->SPI_IRQ_PIN) && num) { if(IO_ERROR !=_io_spi_read_write(fd_ptr, NULL, &tmp, 1)) { *data_ptr++ = tmp; num--; } else return IO_ERROR; } //return _io_spi_read_write(fd_ptr, NULL, data_ptr, n); _io_spi_flush(fd_ptr, 0); return (n-num); #endif return _io_spi_read_write(fd_ptr, NULL, data_ptr, n); }
/*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 } }
void user_key_polling(void) { #ifndef SD_DETECT_POLLING static LWGPIO_VALUE gpio_cur = LWGPIO_VALUE_NOCHANGE, gpio_last = LWGPIO_VALUE_NOCHANGE; #endif #ifdef USB_ACCESSORY_PLAY hmi_setLed( 1, 1); hmi_setLed( 2, 1); hmi_setLed( 3, 1); hmi_setLed( 4, 1); #endif #ifndef SD_DETECT_POLLING gpio_cur = lwgpio_get_value(&btn_prev); if((gpio_cur == LWGPIO_VALUE_LOW)&&(gpio_last == LWGPIO_VALUE_HIGH)) { _int_disable(); mode_switch_action_check(); _int_enable(); } gpio_last = gpio_cur; #endif if(0 != AccessroyHidEvent) { #ifdef USE_ACC_HID Accessory_action_check(); #endif } if(0 != BtAVRCPEvent ) { bt_action_check(); } hmi_polling(); /* at this pointer ,there must be high priority task is ready ,otherwise polling can't exist !!! */ }
/*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) ¶m)) { 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, ¶m);// 控制分区 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 }/////////////////////////////////////////////////////////////////////////////循环 }
/*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"); } } }
/*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) ¶m)) { 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); } }
static LWGPIO_VALUE DIO__vGetLwgpio(LWGPIO_STRUCT * pstPortReg) { return lwgpio_get_value(pstPortReg); }
/*TASK************************************************************************** * * Function Name : dcd_task * * Input Params : initial_data * * Returned Value : None * * Comments : Detect connect to USB, test IOCTL for USB DCD driver * *END***************************************************************************/ void dcd_task ( uint_32 initial_data ) { uint_32 param; char ch; #if defined BSP_TWR_K60N512 || BSP_TWR_K70F120M || BSP_TWR_K60F120M ADC_RESULT_STRUCT data; #else LWGPIO_STRUCT vbus_file; #endif printf("\nUSB DCD testing... %d \n "); fd = fopen ("usbdcd:", NULL); if (fd == NULL) { printf ("ERROR opening the USB DCD driver!\n"); _task_block(); } #if defined BSP_TWR_K60N512 || BSP_TWR_K70F120M || BSP_TWR_K60F120M Adc_Vbus(); #else lwgpio_init(&vbus_file, USB_DCD_PIN_VBUS, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE); lwgpio_set_functionality(&vbus_file, USB_DCD_PIN_VBUS_MUX_GPIO); if (!(lwgpio_get_value(&vbus_file))) { printf("Vbus is disable...\n"); Vbus_flag = 0x00; } #endif printf("\n---------------------------------------------------"); printf("\n Press 1 to select testing IOCTL for USB DCD driver"); printf("\n Press 2 to start detecting USB charger"); printf("\n---------------------------------------------------"); printf("\n Your choice: "); do{ ch = fgetc(stdin); }while ((ch != '1') && (ch != '2')); if (ch == '1') { IO_IOCTL_TEST(); } printf("\nStart detecting USB charger"); for (;;) { #if defined BSP_TWR_K60N512 || BSP_TWR_K70F120M || BSP_TWR_K60F120M read(f_ch1, &data, sizeof(data)); if (( data.result > VBUS_DETECT)&& (!Vbus_flag) ) { /* Starting DCD - IO_IOCTL_USB_DCD_START */ printf("\nVbus is enable ...\n"); if (USB_DCD_OK == ioctl (fd, IO_IOCTL_USB_DCD_START, ¶m)) { printf ("Starting detecting...\n"); } else { printf ("ERROR\n"); } Vbus_flag = 0x01; } if ((data.result < VBUS_DETECT)&& (Vbus_flag)) { printf("Vbus is disable...\n"); Vbus_flag = 0x00; } #else if ( (lwgpio_get_value(&vbus_file)) && (!Vbus_flag) ) { /* Starting DCD - IO_IOCTL_USB_DCD_START */ printf("\nVbus is enable...\n"); if (USB_DCD_OK == ioctl (fd, IO_IOCTL_USB_DCD_START, ¶m)) { printf ("Starting detecting...\n"); } else { printf ("ERROR\n"); } Vbus_flag = 0x01; } if ((!(lwgpio_get_value(&vbus_file))) && (Vbus_flag)) { printf("Vbus is disable ...\n"); Vbus_flag = 0x00; } #endif /* BSP_TWR_K60N512 */ if (USB_DCD_OK == ioctl (fd, IO_IOCTL_USB_DCD_GET_STATE, ¶m)) { if (param == USB_DCD_SEQ_COMPLETE ) { if (USB_DCD_OK == ioctl (fd, IO_IOCTL_USB_DCD_GET_CHARGER_TYPE, ¶m)) { if ((param & 0x0F) == STANDARD_HOST ) { printf("Connected to a Standard Host\n"); } else if ((param & 0x0F) == CHARGING_HOST) { printf("Connected to a Charging Host\n"); } else if ((param & 0x0F) == DEDICATED_CHARGER) { printf("Connected to a dedicated Charger\n"); } else // if (param & 0xF0) printf("------------- DCD Sequence occurs error (param = 0x%X).... \r\n",param); } else { printf("IO_IOCTL_USB_DCD_GET_CHARGER_TYPE - ERROR...\n"); } if (USB_DCD_OK == ioctl (fd, IO_IOCTL_USB_DCD_RESET, ¶m)) { printf("USB-DCD module : Reset\n"); } else { printf("Cannot reset USB-DCD module\n"); } } /* ioctl command test*/ _time_delay(20); } else { printf("Can not get state...\n"); /* ioctl command test*/ _time_delay(20); } } printf("Closing channels..."); fclose(f_ch1); _task_block(); }