_mqx_uint _io_init ( void ) { /* Body */ KERNEL_DATA_STRUCT_PTR kernel_data; _GET_KERNEL_DATA(kernel_data); return (_lwsem_create((LWSEM_STRUCT_PTR)&kernel_data->IO_LWSEM, 1)); } /* Endbody */
void read_task ( uint32_t initial_data ) { _task_id task_id; _mqx_uint result; _mqx_uint i; /* Create the lightweight semaphores */ result = _lwsem_create(&fifo.READ_SEM, 0); if (result != MQX_OK) { printf("\nCreating read_sem failed: 0x%X", result); _task_block(); } result = _lwsem_create(&fifo.WRITE_SEM, 1); if (result != MQX_OK) { printf("\nCreating write_sem failed: 0x%X", result); _task_block(); } /* Create the write tasks */ for (i = 0; i < NUM_WRITERS; i++) { task_id = _task_create(0, WRITE_TASK, (uint32_t)('A' + i)); printf("\nwrite_task created, id 0x%lX", task_id); } while(TRUE) { result = _lwsem_wait(&fifo.READ_SEM); if (result != MQX_OK) { printf("\n_lwsem_wait failed: 0x%X", result); _task_block(); } putchar('\n'); putchar(fifo.DATA); _lwsem_post(&fifo.WRITE_SEM); } }
/*! * \brief Allocates and initializes drive context structure. * * \param drive_ptr_ptr * * \return _mfs_error */ _mfs_error MFS_Create_drive( MFS_DRIVE_STRUCT_PTR *drive_ptr_ptr) { MFS_DRIVE_STRUCT_PTR drive_ptr; _mfs_error error_code = MFS_NO_ERROR; if (drive_ptr_ptr == NULL) { return MFS_INVALID_POINTER; } drive_ptr = MFS_mem_alloc_system_zero(sizeof(MFS_DRIVE_STRUCT)); if (drive_ptr == NULL) { return MFS_INSUFFICIENT_MEMORY; } _mem_set_type(drive_ptr, MEM_TYPE_MFS_DRIVE_STRUCT); #if MFSCFG_USE_MUTEX { MUTEX_ATTR_STRUCT mutex_attr; error_code = _mutatr_init(&mutex_attr); if (error_code == MFS_NO_ERROR) { error_code = _mutatr_set_sched_protocol(&mutex_attr, MUTEX_PRIO_INHERIT); if (error_code == MFS_NO_ERROR) { error_code = _mutatr_set_wait_protocol(&mutex_attr, MUTEX_PRIORITY_QUEUEING); } if (error_code == MFS_NO_ERROR) { error_code = _mutex_init(&drive_ptr->MUTEX, &mutex_attr); } _mutatr_destroy(&mutex_attr); } } #else error_code = _lwsem_create(&drive_ptr->LWSEM, 1); #endif if (error_code) { MFS_mem_free(drive_ptr); } *drive_ptr_ptr = drive_ptr; return error_code; }
void Task1_task2(uint32_t task_init_data) { LWSEM_STRUCT lwsem; LDD_TDeviceData* btn1_ptr, *led_ptr; uint32_t button_press_count = 0; button_state_t button_state, button_last_state; _lwsem_create(&lwsem, 0); btn1_ptr = GPIO1_Init(&lwsem); /* TODO: Enable pull up because board does not have external pull up resistor */ PORTC_PCR3 |= PORT_PCR_PE_MASK | PORT_PCR_PS_MASK; led_ptr = LED_Init(NULL); printf("\n====================== GPIO Example ======================\n"); printf("The (SW1) button is configured to trigger GPIO interrupt.\n"); printf("Press the (SW1) button 3x to continue.\n\n"); button_press_count = 1; while(button_press_count < 4){ /* wait for button press, lwsem is set in button isr */ _lwsem_wait(&lwsem); printf("Button pressed %dx\r", button_press_count++); } printf("The (SW1) button state is now polled.\n"); printf("Press the (SW1) button to switch LED on or off\n\n"); while (1) { if (0 == GPIO1_GetFieldValue(btn1_ptr, BUTTON1)) { button_state = BUTTON_PRESSED; } else { button_state = BUTTON_RELEASED; } if (button_state != button_last_state) { printf("Button %s\r", button_state == BUTTON_PRESSED ? "pressed " : "released"); button_last_state = button_state; /* Set LED on or off arcodingly */ LED_PutVal(led_ptr, button_state == BUTTON_PRESSED ? FALSE : TRUE); } /* Check button state every 20 ticks*/ _time_delay_ticks(20); } }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : _io_flashx_install * Returned Value : _mqx_uint a task error code or MQX_OK * Comments : * Install a flash driver. * *END*----------------------------------------------------------------------*/ _mqx_uint _io_flashx_install ( /* [IN] A string that identifies the device for fopen */ char_ptr identifier, /* [IN] BSP specific settings for installing device */ FLASHX_INIT_STRUCT const _PTR_ init_ptr ) { /* Body */ IO_FLASHX_STRUCT_PTR dev_ptr; dev_ptr = (IO_FLASHX_STRUCT_PTR)_mem_alloc_system_zero((_mem_size)sizeof(IO_FLASHX_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (dev_ptr == NULL) { return MQX_OUT_OF_MEMORY; } /* Endif */ #endif dev_ptr->SECTOR_ERASE = init_ptr->DEVICE_IF->SECTOR_ERASE; dev_ptr->SECTOR_PROGRAM = init_ptr->DEVICE_IF->SECTOR_PROGRAM; dev_ptr->CHIP_ERASE = init_ptr->DEVICE_IF->CHIP_ERASE; dev_ptr->READ = init_ptr->DEVICE_IF->READ; dev_ptr->INIT = init_ptr->DEVICE_IF->INIT; dev_ptr->DEINIT = init_ptr->DEVICE_IF->DEINIT; dev_ptr->WRITE_PROTECT = init_ptr->DEVICE_IF->WRITE_PROTECT; dev_ptr->IOCTL = init_ptr->DEVICE_IF->IOCTL; dev_ptr->BASE_ADDR = init_ptr->BASE_ADDR; dev_ptr->HW_BLOCK = (FLASHX_BLOCK_INFO_STRUCT_PTR) init_ptr->HW_BLOCK; dev_ptr->FILE_BLOCK = (FLASHX_FILE_BLOCK_PTR) init_ptr->FILE_BLOCK; dev_ptr->WIDTH = init_ptr->WIDTH; dev_ptr->DEVICES = init_ptr->DEVICES; dev_ptr->WRITE_VERIFY = init_ptr->WRITE_VERIFY; dev_ptr->DEVICE_SPECIFIC_INIT = init_ptr->DEVICE_SPECIFIC_INIT; _lwsem_create(&dev_ptr->HW_ACCESS, 1); return (_io_dev_install_ext( identifier, _io_flashx_open, _io_flashx_close, _io_flashx_read, _io_flashx_write, _io_flashx_ioctl, _io_flashx_uninstall, (pointer)dev_ptr)); } /* Endbody */
void Lptmr_Init(int count, int clock_source) { _mqx_uint mqx_ret; SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; SIM_SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK; SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(2); // ERCLK32 is RTC OSC CLOCK PORTC_PCR1 &= ~PORT_PCR_MUX_MASK; PORTC_PCR1 |= PORT_PCR_MUX(1);//enable ptc1 alt1 functions to select RTC_CLKIN function /********************************************************************************* * On L2K tower board, we use external 32kHz clock instead of 32kHz crystal, so please * don't enable the 32kHz crystal oscillator **********************************************************************************/ /* RTC_CR |= RTC_CR_OSCE_MASK | RTC_CR_CLKO_MASK | RTC_CR_SC8P_MASK ; */ LPTMR0_PSR &= ~LPTMR_PSR_PRESCALE_MASK; LPTMR0_PSR |= LPTMR_PSR_PRESCALE(0); // 0000 is div 2 LPTMR0_PSR |= LPTMR_PSR_PBYP_MASK; // LPO feeds directly to LPT LPTMR0_PSR &= ~LPTMR_PSR_PCS_MASK; LPTMR0_PSR |= LPTMR_PSR_PCS(clock_source); // use the choice of clock if (clock_source== 0) APP_TRACE("\n LPTMR Clock source is the MCGIRCLK \n\r"); if (clock_source== 1) APP_TRACE("\n LPTMR Clock source is the LPOCLK \n\r"); if (clock_source== 2) APP_TRACE("\n LPTMR Clock source is the ERCLK32 \n\r"); if (clock_source== 3) APP_TRACE("\n LPTMR Clock source is the OSCERCLK \n\r"); LPTMR0_CMR = LPTMR_CMR_COMPARE(count); //Set compare value LPTMR0_CSR |=( LPTMR_CSR_TCF_MASK // Clear any pending interrupt | LPTMR_CSR_TIE_MASK // LPT interrupt enabled |!LPTMR_CSR_TPP_MASK //TMR Pin polarity |!LPTMR_CSR_TFC_MASK // Timer Free running counter is reset whenever TMR counter equals compare |!LPTMR_CSR_TMS_MASK //LPTMR0 as Timer ); enable_irq(28) ; _int_install_isr(LDD_ivIndex_INT_LPTimer, lptmr_isr, NULL); mqx_ret = _lwsem_create(&g_lptmr_int_sem, 0); ASSERT_PARAM(MQX_OK == mqx_ret); // // ready for this interrupt. // set_irq_priority(28, 2); }
/*! * \cond DOXYGEN_PRIVATE * \private * * \brief Initializes a name component. * * This function provides the storage and retrieval of a name associated with a * number. * \n The name component can be used by any other kernel components. Each may have * a separate set of names. * * \param[out] name_handle An address where the name data structure pointer is * to be stored. * \param[in] initial_number The initial number of names that can be stored. * \param[in] grow_number The number of names to be added when table is full. * \param[in] maximum_number The maximum number of names that can be stored. * \param[in] total_so_far The total number of name spaces in all chained pools. * * \return MQX_OK * \return MQX_OUT_OF_MEMORY (MQX cannot allocate memory for the name component.) */ _mqx_uint _name_create_handle_internal ( void **name_handle, _mqx_uint initial_number, _mqx_uint grow_number, _mqx_uint maximum_number, _mqx_uint total_so_far ) { /* Body */ register NAME_COMPONENT_STRUCT_PTR name_manager_ptr; name_manager_ptr = (NAME_COMPONENT_STRUCT_PTR) _mem_alloc_system_zero((_mem_size) (sizeof(NAME_COMPONENT_STRUCT) + ((initial_number - 1) * sizeof(NAME_STRUCT)))); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (name_manager_ptr == NULL) { return (MQX_OUT_OF_MEMORY); } /* Endif */ #endif /* MQX_CHECK_MEMORY_ALLOCATION_ERRORS */ _mem_set_type(name_manager_ptr, MEM_TYPE_NAME_COMPONENT); /* The name component has been created, so we must create the semaphore */ if (initial_number == total_so_far) { /* Initialize the semaphore for this name component */ _lwsem_create((LWSEM_STRUCT_PTR) (&name_manager_ptr->SEM), 1); } /* Endif */ name_manager_ptr->GROW_NUMBER = grow_number; if (maximum_number == 0) { name_manager_ptr->MAX_NUMBER = MAX_MQX_UINT; } else if (maximum_number < initial_number) { name_manager_ptr->MAX_NUMBER = initial_number; } else { name_manager_ptr->MAX_NUMBER = maximum_number; } /* Endif */ name_manager_ptr->NUMBER_IN_BLOCK = initial_number; name_manager_ptr->TOTAL_NUMBER = total_so_far; name_manager_ptr->VALID = NAME_VALID; *(NAME_COMPONENT_STRUCT_PTR *) name_handle = name_manager_ptr; return (MQX_OK); } /* Endbody */
os_sem_handle OS_Sem_create(uint32_t initial_number) { LWSEM_STRUCT_PTR sem = NULL; sem = (LWSEM_STRUCT_PTR)_mem_alloc_system_zero(sizeof(LWSEM_STRUCT)); if (sem == NULL) { return NULL; } if (_lwsem_create(sem, initial_number) != MQX_OK) { _mem_free(sem); return NULL; } return (os_sem_handle)sem; }
_mqx_uint _io_nandflash_install ( /* [IN] The initialization structure for the device */ NANDFLASH_INIT_STRUCT _PTR_ init_ptr ) { /* Body */ IO_NANDFLASH_STRUCT_PTR dev_ptr; dev_ptr = (IO_NANDFLASH_STRUCT_PTR)_mem_alloc_system_zero( (_mem_size)sizeof(IO_NANDFLASH_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (dev_ptr == NULL) { return MQX_OUT_OF_MEMORY; } /* Endif */ #endif dev_ptr->INIT = init_ptr->INIT; dev_ptr->DEINIT = init_ptr->DEINIT; dev_ptr->CHIP_ERASE = init_ptr->CHIP_ERASE; dev_ptr->BLOCK_ERASE = init_ptr->BLOCK_ERASE; dev_ptr->PAGE_READ = init_ptr->PAGE_READ; dev_ptr->PAGE_PROGRAM = init_ptr->PAGE_PROGRAM; dev_ptr->WRITE_PROTECT = init_ptr->WRITE_PROTECT; dev_ptr->IS_BLOCK_BAD = init_ptr->IS_BLOCK_BAD; dev_ptr->MARK_BLOCK_AS_BAD = init_ptr->MARK_BLOCK_AS_BAD; dev_ptr->IOCTL = init_ptr->IOCTL; dev_ptr->NANDFLASH_INFO_PTR = init_ptr->NANDFLASH_INFO_PTR; dev_ptr->VIRTUAL_PAGE_SIZE = BSP_VIRTUAL_PAGE_SIZE; dev_ptr->NUM_VIRTUAL_PAGES = (init_ptr->NANDFLASH_INFO_PTR->BLOCK_SIZE / BSP_VIRTUAL_PAGE_SIZE) * init_ptr->NANDFLASH_INFO_PTR->NUM_BLOCKS; dev_ptr->PHY_PAGE_SIZE_TO_VIRTUAL_PAGE_SIZE_RATIO = (init_ptr->NANDFLASH_INFO_PTR->PHY_PAGE_SIZE / BSP_VIRTUAL_PAGE_SIZE); dev_ptr->ECC_SIZE = BSP_ECC_SIZE; dev_ptr->WRITE_VERIFY = init_ptr->WRITE_VERIFY; dev_ptr->DEVICE_SPECIFIC_DATA = init_ptr->DEVICE_SPECIFIC_DATA; _lwsem_create(&dev_ptr->LWSEM, 1); return (_io_dev_install_ext( init_ptr->ID_PTR, _io_nandflash_open, _io_nandflash_close, _io_nandflash_read, _io_nandflash_write, _io_nandflash_ioctl, _io_nandflash_uninstall, (pointer)dev_ptr)); } /* Endbody */
void main_task ( uint_32 initial_data ) { _task_id test_task; _lwsem_create(&shutdown_sem, 0); test_task = _task_create(0, TEST_TASK,0); _lwsem_wait(&shutdown_sem); _task_destroy(test_task); printf("\nWatchdog expired"); _task_block(); }
void main_task ( uint32_t initial_data ) { MY_ISR_STRUCT_PTR isr_ptr; uint32_t result; /* Create the lightweight semaphore */ result = _lwsem_create(&lwsem, 0); if (result != MQX_OK) { printf("\nCreating sem failed: 0x%X", result); _task_block(); } isr_ptr = _mem_alloc_zero((_mem_size)sizeof(MY_ISR_STRUCT)); isr_ptr->TICK_COUNT = 0; isr_ptr->OLD_ISR_DATA = _int_get_isr_data(BSP_TIMER_INTERRUPT_VECTOR); isr_ptr->OLD_ISR = _int_get_isr(BSP_TIMER_INTERRUPT_VECTOR); /* Native MQX interrupt handling method, ISR is installed into the interrupt vector table in kernel */ if(! _int_install_isr(BSP_TIMER_INTERRUPT_VECTOR, new_tick_isr, isr_ptr)) { printf("Install interrupt handler to interrupt vector table of MQX kernel failed.\n"); _task_block(); } #ifndef DEMO_ENABLE_KERNEL_ISR _time_delay_ticks(200); printf("\nTick count = %d\n", isr_ptr->TICK_COUNT); _task_block(); #else printf("\n====================== ISR Example =======================\n"); printf("Press the SW1 to blink LED1, press it again to turn it off\n"); while(1) { _lwsem_wait(&lwsem); num_tick = isr_ptr->TICK_COUNT; } #endif /* DEMO_ENABLE_KERNEL_ISR */ }
/*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 } }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : btnled_init * Returned Value : client struct HMI_CLIENT_STRUCT_PTR * Comments : * This function is for HMI BTNLED client initialization. * *END*--------------------------------------------------------------------*/ HMI_CLIENT_STRUCT_PTR btnled_init(void) { HMI_CLIENT_STRUCT_PTR btnled_struct; HMI_PROVIDER_STRUCT_PTR provider_table; BTNLED_CONTEXT_STRUCT_PTR context; /* allocate btnled structure */ btnled_struct = hmi_client_init(); provider_table = (pointer)btnled_struct->PROVIDER_TABLE; /* allocate context data */ context = _mem_alloc_system_zero(sizeof(BTNLED_CONTEXT_STRUCT)); if(context == NULL) { _mem_free(provider_table); _mem_free(btnled_struct); return NULL; } /* create a semaphore for callbacks */ _lwsem_create(&context->LOCK_BTNLED_SEM, 1); btnled_struct->CONTEXT_PTR = context; btnled_struct->ON_CHANGE = (void ( _CODE_PTR_)(pointer, uint_32, uint_32))btnled_on_change; return btnled_struct; }
_mqx_int _io_dun_open ( /* [IN] the file handle for the device being opened */ MQX_FILE_PTR fd_ptr, /* [IN] the name of the device */ char_ptr open_name_ptr, /* [IN] the flags to be used during operation: ** echo, translation, xon/xoff, encoded into a pointer. */ char_ptr f_ptr ) { /* Body */ IODUN_DEV_STRUCT_PTR dev_ptr = fd_ptr->DEV_PTR->DRIVER_INIT_PTR; IODUN_STRUCT_PTR ras_ptr = _mem_alloc_system_zero(sizeof(IODUN_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (ras_ptr == NULL) { return(MQX_OUT_OF_MEMORY); } /* Endif */ #endif _mem_set_type(ras_ptr,MEM_TYPE_IO_DUN); fd_ptr->DEV_DATA_PTR = ras_ptr; _lwsem_create(&ras_ptr->LWSEM,1); ras_ptr->STATE = 0; ras_ptr->F_PTR = (MQX_FILE_PTR)f_ptr; ras_ptr->PARSE = dev_ptr->RECV; _io_write(ras_ptr->F_PTR, dev_ptr->SEND, dev_ptr->SEND_SIZE); return(MQX_OK); } /* Endbody */
/*FUNCTION*------------------------------------------------ * * Function Name: shell_ppp_start() * Comments : * This function start PPP communication and try to establish PPP connection. *END*-----------------------------------------------------*/ void shell_ppp_start(SHELL_PPP_LINK_PTR ppp_current) { uint_32 error; _ip_address local_address; _ip_address peer_address=0; IPCP_DATA_STRUCT ipcp_data; /* This is configuration for PAP. */ char* localname = ppp_current->LOCAL_NAME; char* localsecret = ppp_current->LOCAL_PASSWORD; PPP_SECRET lsecret; if(default_ppp.SERVER_FLAG==0) { /* Setup PAP for client mode.*/ lsecret.PPP_ID_LENGTH = strlen(localname); lsecret.PPP_PW_LENGTH = strlen(localsecret); lsecret.PPP_ID_PTR = localname; lsecret.PPP_PW_PTR = localsecret; _PPP_PAP_LSECRET = &lsecret; /* lsecrets */ _PPP_PAP_RSECRETS = NULL; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else if(default_ppp.SERVER_FLAG==1) { /* Setup PAP for server mode.*/ _PPP_PAP_LSECRET = NULL; /* lsecrets */ _PPP_PAP_RSECRETS = ppp_current->rsecrets; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else { ppp_current->PPP_HANDLE = NULL; return; } /* Install a route for a default gateway */ RTCS_gate_add(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); ppp_current->PPP_IO_DRIVER_HANDLE = _iopcb_ppphdlc_init(ppp_current->PPP_DEV_HANDLE); _PPP_ACCM = 0; error = PPP_initialize(ppp_current->PPP_IO_DRIVER_HANDLE, &ppp_current->PPP_HANDLE); if (error) { printf("\n PPP initialize: %lx", error); _task_block(); } _iopcb_open(ppp_current->PPP_IO_DRIVER_HANDLE, PPP_lowerup, PPP_lowerdown,ppp_current->PPP_HANDLE); error = RTCS_if_add(ppp_current->PPP_HANDLE, RTCS_IF_PPP, &ppp_current->PPP_IF_HANDLE); if (error) { printf("\n IF add failed, error = %lx", error); _task_block(); } _lwsem_create(&ppp_current->PPP_SEM, 0); _mem_zero(&ipcp_data, sizeof(ipcp_data)); ipcp_data.IP_UP = PPP_linkup; ipcp_data.IP_DOWN = PPP_linkdown; ipcp_data.IP_PARAM = &ppp_current->PPP_SEM; if(default_ppp.SERVER_FLAG==0) { ipcp_data.ACCEPT_LOCAL_ADDR = TRUE; ipcp_data.ACCEPT_REMOTE_ADDR = TRUE; } else { ipcp_data.ACCEPT_LOCAL_ADDR = FALSE; ipcp_data.ACCEPT_REMOTE_ADDR = FALSE; } ipcp_data.LOCAL_ADDR = ppp_current->PPP_LOCAL_ADDRESS; ipcp_data.REMOTE_ADDR = ppp_current->PPP_REMOTE_ADDRESS; ipcp_data.DEFAULT_NETMASK = TRUE; ipcp_data.DEFAULT_ROUTE = TRUE; error = RTCS_if_bind_IPCP(ppp_current->PPP_IF_HANDLE, &ipcp_data); if (error) { printf("\n IF bind failed, error = %lx", error); _task_block(); } /*In case "client" we need wait for connection to server*/ if(default_ppp.SERVER_FLAG==0) { printf("\n Please wait initiate PPP connection. Waiting...\n"); /*Handshake with RAS server */ /* 10 attempts to connect */ error = 0; while (error < 10) { if (PPP_link == FALSE) { _time_delay(1000); } else { printf("\n PPP_link = %d, time = %d\n",PPP_link,error); break; } error ++; } if (PPP_link == TRUE) { local_address = IPCP_get_local_addr(ppp_current->PPP_IF_HANDLE); peer_address = IPCP_get_peer_addr(ppp_current->PPP_IF_HANDLE); printf("\n PPP device on %s is bound on.\n", ppp_current->PPP_CONNECTION_DEVICE ); printf(" PPP local address is : %d.%d.%d.%d\n", IPBYTES(local_address)); printf(" PPP remote address is : %d.%d.%d.%d\n", IPBYTES(peer_address)); printf(" Now PPP connection is established on %d.%d.%d.%d\n", IPBYTES(peer_address)); } else { if(ppp_current->PPP_HANDLE) { /* clean up all PPP structure */ error = PPP_shutdown(ppp_current->PPP_HANDLE); } ppp_current->PPP_HANDLE = NULL; /* We shoul remove route to clear part block. */ RTCS_gate_remove(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); } } else { /* In case of "server" we do not need wait at all. */ printf("\n PPP server ready and waiting for connection.\n"); } }
/*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; }
uint_32 _io_usb_mfs_install ( /* [IN] A string that identifies the device for fopen */ char_ptr identifier, /* [IN] Logical unit number which driver need to install */ uint_8 logical_unit, /* [IN] Interface Handle */ CLASS_CALL_STRUCT_PTR ccs_ptr ) { /* Body */ IO_USB_MFS_STRUCT_PTR info_ptr; USB_MASS_CLASS_INTF_STRUCT_PTR intf_ptr = NULL; USB_STATUS error = USBERR_ERROR; /* Pointer validity-checking, clear memory, init header */ USB_lock(); if (usb_host_class_intf_validate(ccs_ptr)) { intf_ptr = (USB_MASS_CLASS_INTF_STRUCT_PTR) ccs_ptr->class_intf_handle; if(intf_ptr != NULL) { error = usb_hostdev_validate (intf_ptr->G.dev_handle); } } /* Endif */ if (USB_OK != error) { /* Device was already detached or intf_ptr is NULL */ USB_unlock(); return MQX_OUT_OF_MEMORY; } if (USB_OK != _usb_hostdev_get_buffer(intf_ptr->G.dev_handle, sizeof(IO_USB_MFS_STRUCT), (pointer *) &info_ptr)) { USB_unlock(); return MQX_OUT_OF_MEMORY; } _mem_zero(info_ptr, sizeof(IO_USB_MFS_STRUCT)); _mem_set_type(info_ptr, MEM_TYPE_USB_MFS_STRUCT); USB_unlock(); /* Fill in the state structure with the info we know */ info_ptr->LUN = logical_unit; info_ptr->BLENGTH = USB_MFS_DEFAULT_SECTOR_SIZE; info_ptr->ERROR_CODE = IO_OK; info_ptr->BLOCK_MODE = TRUE; info_ptr->COMMAND.CBW_PTR = (CBW_STRUCT_PTR) &info_ptr->CBW; info_ptr->COMMAND.CSW_PTR = (CSW_STRUCT_PTR) &info_ptr->CSW; info_ptr->COMMAND.CALL_PTR = ccs_ptr; info_ptr->COMMAND.LUN = logical_unit; info_ptr->COMMAND.CALLBACK = _io_usb_mfs_callback; info_ptr->COMMAND_STATUS = MQX_OK; _lwsem_create(&info_ptr->LWSEM, 1); _lwsem_create(&info_ptr->COMMAND_DONE, 0); return (_io_dev_install_ext(identifier, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, char_ptr))_io_usb_mfs_open, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR) )_io_usb_mfs_close, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, int_32) )_io_usb_mfs_read, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, int_32) )_io_usb_mfs_write, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, _mqx_uint, pointer) ) _io_usb_mfs_ioctl, _io_usb_mfs_uninstall_internal, (pointer)info_ptr )); } /* Endbody */
void main_task ( uint_32 dummy ) { MQX_FILE_PTR fd; uint_32 i, j; _mqx_int param, result; I2C_STATISTICS_STRUCT stats; uchar_ptr buffer; /* I2C transaction lock */ _lwsem_create (&lock, 1); /* Allocate receive buffer */ buffer = _mem_alloc_zero (BUFFER_SIZE); if (buffer == NULL) { printf ("ERROR getting receive buffer!\n"); _task_block (); } printf ("\n\n-------------- Polled I2C master example --------------\n\n"); /* Open the I2C driver */ fd = fopen (I2C_DEVICE_POLLED, NULL); if (fd == NULL) { printf ("ERROR opening the I2C driver!\n"); _task_block (); } /* Test ioctl commands */ param = 100000; printf ("Set current baud rate to %d ... ", param); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_BAUD, ¶m)) { printf ("OK\n"); } else { printf ("ERROR\n"); } printf ("Get current baud rate ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_BAUD, ¶m)) { printf ("%d\n", param); } else { printf ("ERROR\n"); } printf ("Set master mode ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_MASTER_MODE, NULL)) { printf ("OK\n"); } else { printf ("ERROR\n"); } printf ("Get current mode ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_MODE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } param = 0x60; printf ("Set station address to 0x%02x ... ", param); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_STATION_ADDRESS, ¶m)) { printf ("OK\n"); } else { printf ("ERROR\n"); } param = 0x00; printf ("Get station address ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATION_ADDRESS, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } printf ("Clear statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_CLEAR_STATISTICS, NULL)) { printf ("OK\n"); } else { printf ("ERROR\n"); } printf ("Get statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATISTICS, (pointer)&stats)) { printf ("OK\n Interrupts: %d\n", stats.INTERRUPTS); printf (" Rx packets: %d\n", stats.RX_PACKETS); printf (" Tx packets: %d\n", stats.TX_PACKETS); printf (" Tx lost arb: %d\n", stats.TX_LOST_ARBITRATIONS); printf (" Tx as slave: %d\n", stats.TX_ADDRESSED_AS_SLAVE); printf (" Tx naks: %d\n", stats.TX_NAKS); } else { printf ("ERROR\n"); } printf ("Get current state ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } param = I2C_EEPROM_BUS_ADDRESS; printf ("Set destination address to 0x%02x ... ", param); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, ¶m)) { printf ("OK\n"); } else { printf ("ERROR\n"); } param = 0x00; printf ("Get destination address ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_DESTINATION_ADDRESS, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } /* Test EEPROM communication */ i2c_write_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, (uchar_ptr)TEST_STRING, 0); i2c_read_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, buffer, 0); i2c_write_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, (uchar_ptr)TEST_STRING, 1); i2c_read_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, buffer, 1); printf ("Received: %c (0x%02x)\n", buffer[0], buffer[0]); if (buffer[0] != TEST_STRING[0]) { printf ("ERROR\n"); _task_block (); } _mem_zero (buffer, BUFFER_SIZE); i2c_write_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, buffer, sizeof(TEST_STRING)); i2c_read_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, buffer, sizeof(TEST_STRING)); for (result = 0; result < BUFFER_SIZE; result++) { if (0 != buffer[result]) { printf ("\nERROR during memory clearing\n"); _task_block (); } } i2c_write_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, (uchar_ptr)TEST_STRING, sizeof(TEST_STRING)); i2c_read_eeprom_polled (fd, I2C_EEPROM_MEMORY_ADDRESS1, buffer, sizeof(TEST_STRING)); printf ("Received: "); for (result = 0; result < sizeof(TEST_STRING); result++) { printf ("%c", buffer[result]); if (buffer[result] != TEST_STRING[result]) { printf ("\nERROR\n"); _task_block (); } } printf ("\n"); /* Test special cases: write 1 byte (address pointer), read 1 byte (dump memory) */ _lwsem_wait (&lock); buffer[0] = 0; buffer[1] = 0; fwrite (buffer, 1, I2C_EEPROM_MEMORY_WIDTH, fd); if (I2C_OK != ioctl (fd, IO_IOCTL_FLUSH_OUTPUT, NULL)) { printf ("\nERROR during flush\n"); } if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_STOP, NULL)) { printf ("\nERROR during stop\n"); } printf ("\nMemory dump:\n"); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { _time_delay (1); param = 1; if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_RX_REQUEST, ¶m)) { printf ("\nERROR during set rx request\n"); } fread (&(buffer[j]), 1, 1, fd); if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_STOP, NULL)) { printf ("\nERROR during stop\n"); } } printf ("0x%02x: ", i * 16); for (j = 0; j < 16; j++) { printf ("%02x ", buffer[j]); } for (j = 0; j < 16; j++) { if ((32 <= buffer[j]) && (buffer[j] <= 128)) { printf ("%c", buffer[j]); } else { printf ("."); } } printf ("\n"); } _lwsem_post (&lock); printf ("\nGet statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATISTICS, (pointer)&stats)) { printf ("OK\n Interrupts: %d\n", stats.INTERRUPTS); printf (" Rx packets: %d\n", stats.RX_PACKETS); printf (" Tx packets: %d\n", stats.TX_PACKETS); printf (" Tx lost arb: %d\n", stats.TX_LOST_ARBITRATIONS); printf (" Tx as slave: %d\n", stats.TX_ADDRESSED_AS_SLAVE); printf (" Tx naks: %d\n", stats.TX_NAKS); } else { printf ("ERROR\n"); } printf ("Get current state ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } /* Close the driver */ result = fclose (fd); if (result) { printf ("ERROR during close, returned: %d\n", result); } #if ENABLE_I2C_INTERRUPT printf ("\n\n-------------- Interrupt I2C master example --------------\n\n"); /* Open the I2C driver */ fd = fopen (I2C_DEVICE_INTERRUPT, NULL); if (fd == NULL) { printf ("Failed to open the I2C driver!\n"); _task_block (); } /* Test ioctl commands */ printf ("Get current baud rate ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_BAUD, ¶m)) { printf ("%d\n", param); } else { printf ("ERROR\n"); } printf ("Set master mode ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_MASTER_MODE, NULL)) { printf ("OK\n"); } else { printf ("ERROR\n"); } printf ("Get current mode ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_MODE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } param = 0x60; printf ("Set station address to 0x%02x ... ", param); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_STATION_ADDRESS, ¶m)) { printf ("OK\n"); } else { printf ("ERROR\n"); } param = 0x00; printf ("Get station address ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATION_ADDRESS, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } printf ("Clear statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_CLEAR_STATISTICS, NULL)) { printf ("OK\n"); } else { printf ("ERROR\n"); } printf ("Get statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATISTICS, (pointer)&stats)) { printf ("OK\n Interrupts: %d\n", stats.INTERRUPTS); printf (" Rx packets: %d\n", stats.RX_PACKETS); printf (" Tx packets: %d\n", stats.TX_PACKETS); printf (" Tx lost arb: %d\n", stats.TX_LOST_ARBITRATIONS); printf (" Tx as slave: %d\n", stats.TX_ADDRESSED_AS_SLAVE); printf (" Tx naks: %d\n", stats.TX_NAKS); } else { printf ("ERROR\n"); } printf ("Get current state ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } param = I2C_EEPROM_BUS_ADDRESS; printf ("Set destination address to 0x%02x ... ", param); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, ¶m)) { printf ("OK\n"); } else { printf ("ERROR\n"); } param = 0x00; printf ("Get destination address ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_DESTINATION_ADDRESS, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } /* Test EEPROM communication */ i2c_write_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, (uchar_ptr)TEST_STRING, 0); i2c_read_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, buffer, 0); i2c_write_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, (uchar_ptr)TEST_STRING, 1); i2c_read_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, buffer, 1); printf ("Received: %c (0x%02x)\n", buffer[0], buffer[0]); if (buffer[0] != TEST_STRING[0]) { printf ("ERROR\n"); _task_block (); } _mem_zero (buffer, BUFFER_SIZE); i2c_write_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, buffer, sizeof(TEST_STRING)); i2c_read_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, buffer, sizeof(TEST_STRING)); for (result = 0; result < BUFFER_SIZE; result++) { if (0 != buffer[result]) { printf ("\nERROR during memory clearing\n"); _task_block (); } } i2c_write_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, (uchar_ptr)TEST_STRING, sizeof(TEST_STRING)); i2c_read_eeprom_interrupt (fd, I2C_EEPROM_MEMORY_ADDRESS2, buffer, sizeof(TEST_STRING)); printf ("Received: "); for (result = 0; result < sizeof(TEST_STRING); result++) { printf ("%c", buffer[result]); if (buffer[result] != TEST_STRING[result]) { printf ("\nERROR\n"); _task_block (); } } printf ("\n"); /* Test special cases: write 1 byte (address pointer), read 1 byte (dump memory) */ _lwsem_wait (&lock); buffer[0] = 0; buffer[1] = 0; fwrite (buffer, 1, I2C_EEPROM_MEMORY_WIDTH, fd); if (I2C_OK != ioctl (fd, IO_IOCTL_FLUSH_OUTPUT, NULL)) { printf ("\nERROR during flush\n"); } if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_STOP, NULL)) { printf ("\nERROR during stop\n"); } printf ("\nMemory dump:\n"); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { _time_delay (1); param = 1; if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_SET_RX_REQUEST, ¶m)) { printf ("\nERROR during set rx request\n"); } do { result = fread (&(buffer[j]), 1, 1, fd); } while (0 == result); if (I2C_OK != ioctl (fd, IO_IOCTL_I2C_STOP, NULL)) { printf ("\nERROR during stop\n"); } } printf ("0x%02x: ", i * 16); for (j = 0; j < 16; j++) { printf ("%02x ", buffer[j]); } for (j = 0; j < 16; j++) { if ((32 <= buffer[j]) && (buffer[j] <= 128)) { printf ("%c", buffer[j]); } else { printf ("."); } } printf ("\n"); } _lwsem_post (&lock); printf ("\nGet statistics ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATISTICS, (pointer)&stats)) { printf ("OK\n Interrupts: %d\n", stats.INTERRUPTS); printf (" Rx packets: %d\n", stats.RX_PACKETS); printf (" Tx packets: %d\n", stats.TX_PACKETS); printf (" Tx lost arb: %d\n", stats.TX_LOST_ARBITRATIONS); printf (" Tx as slave: %d\n", stats.TX_ADDRESSED_AS_SLAVE); printf (" Tx naks: %d\n", stats.TX_NAKS); } else { printf ("ERROR\n"); } printf ("Get current state ... "); if (I2C_OK == ioctl (fd, IO_IOCTL_I2C_GET_STATE, ¶m)) { printf ("0x%02x\n", param); } else { printf ("ERROR\n"); } /* Close the driver */ result = fclose (fd); if (result) { printf ("ERROR during close, returned: %d\n", result); } #endif /* Free transation lock */ _lwsem_destroy (&lock); /* Free buffer */ _mem_free (buffer); printf("Example finished.\n"); _task_block(); } /* Endbody */
void USB_task(uint_32 param) { _usb_host_handle host_handle; USB_STATUS error; pointer usb_fs_handle = NULL; #if DEMO_USE_POOLS && defined(DEMO_MFS_POOL_ADDR) && defined(DEMO_MFS_POOL_SIZE) _MFS_pool_id = _mem_create_pool((pointer)DEMO_MFS_POOL_ADDR, DEMO_MFS_POOL_SIZE); #endif _lwsem_create(&USB_Stick,0); _lwevent_create(&USB_Event,0); USB_lock(); _int_install_unexpected_isr(); if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) { printf("\n Driver installation failed"); _task_block(); } error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle); if (error == USB_OK) { error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable); if (error == USB_OK) { error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL); } } USB_unlock(); if (error == USB_OK) { for ( ; ; ) { // Wait for insertion or removal event _lwevent_wait_ticks(&USB_Event,USB_EVENT,FALSE,0); if ( device.STATE== USB_DEVICE_ATTACHED) { if (device.SUPPORTED) { error = _usb_hostdev_select_interface(device.DEV_HANDLE, device.INTF_HANDLE, (pointer)&device.CLASS_INTF); if(error == USB_OK) { device.STATE = USB_DEVICE_INTERFACED; USB_handle = (pointer)&device.CLASS_INTF; // Install the file system usb_fs_handle = usb_filesystem_install( USB_handle, "USB:", "PM_C1:", "c:" ); if (usb_fs_handle) { // signal the application _lwsem_post(&USB_Stick); } } } else { device.STATE = USB_DEVICE_INTERFACED; } } else if ( device.STATE==USB_DEVICE_DETACHED) { _lwsem_wait(&USB_Stick); // remove the file system usb_filesystem_uninstall(usb_fs_handle); } // clear the event _lwevent_clear(&USB_Event,USB_EVENT); } } }
_mqx_uint _io_mem_install ( /* [IN] A string that identifies the device for fopen */ char *identifier, /* [IN] the address of the fdv_ram */ void *base_address, /* [IN] the total size of the device */ _file_size size ) { IO_MEM_STRUCT_PTR handle_ptr; #if (MAX_FILE_SIZE > MAX_MEM_SIZE) #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (size > MAX_MEM_SIZE) { return(MQX_OUT_OF_MEMORY); } #endif #endif handle_ptr = (IO_MEM_STRUCT_PTR) _mem_alloc_system_zero((_mem_size)sizeof(IO_MEM_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (handle_ptr == NULL) { return(MQX_OUT_OF_MEMORY); } #endif _mem_set_type(handle_ptr,MEM_TYPE_IO_MEM_STRUCT); if (base_address == NULL) { base_address = _mem_alloc_system((_mem_size)size); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (base_address == NULL) { _mem_free(handle_ptr); return(MQX_OUT_OF_MEMORY); } #endif _mem_set_type(base_address,MEM_TYPE_IO_MEM_DATA); /* Indicate the the RAM drive was allocated from kernel memory */ handle_ptr->TYPE = MEM_TYPE_DYNAMIC; } else { /* Indicate the the RAM drive was statically allocated (global mem) */ handle_ptr->TYPE = MEM_TYPE_STATIC; } handle_ptr->BASE_ADDR = base_address; handle_ptr->SIZE = size; _lwsem_create(&handle_ptr->LWSEM, 1); return (_io_dev_install( identifier, _io_mem_open, _io_mem_close, _io_mem_read, _io_mem_write, _io_mem_ioctl, (void *)handle_ptr )); }
/*FUNCTION**************************************************************** * * Function Name : _dspi_init * Returned Value : MQX error code * Comments : * This function initializes the SPI driver * *END*********************************************************************/ static _mqx_int _dspi_init ( /* [IN] The initialization information for the device being opened */ const void *init_data_ptr, /* [OUT] The address to store device specific information */ void **io_info_ptr_ptr ) { DSPI_INIT_STRUCT_PTR dspi_init_ptr = (DSPI_INIT_STRUCT_PTR)init_data_ptr; DSPI_INFO_STRUCT_PTR dspi_info_ptr; VDSPI_REG_STRUCT_PTR dspi_ptr; const uint32_t *vectors; uint32_t i; #if PSP_HAS_DEVICE_PROTECTION if (!_bsp_dspi_enable_access(dspi_init_ptr->CHANNEL)) { return SPI_ERROR_CHANNEL_INVALID; } #endif /* Check channel */ dspi_ptr = _bsp_get_dspi_base_address (dspi_init_ptr->CHANNEL); if (NULL == dspi_ptr) { return SPI_ERROR_CHANNEL_INVALID; } if (_bsp_dspi_io_init (dspi_init_ptr->CHANNEL) == -1) { return SPI_ERROR_CHANNEL_INVALID; } /* Initialize internal data */ dspi_info_ptr = (DSPI_INFO_STRUCT_PTR)_mem_alloc_system_zero((uint32_t)sizeof(DSPI_INFO_STRUCT)); if (dspi_info_ptr == NULL) { return MQX_OUT_OF_MEMORY; } _mem_set_type(dspi_info_ptr, MEM_TYPE_IO_SPI_INFO_STRUCT); *io_info_ptr_ptr = (void *)dspi_info_ptr; dspi_info_ptr->DSPI_PTR = dspi_ptr; dspi_info_ptr->CHANNEL = dspi_init_ptr->CHANNEL; dspi_info_ptr->CLOCK_SOURCE = dspi_init_ptr->CLOCK_SOURCE; _dspi_init_low(dspi_info_ptr->DSPI_PTR); _lwsem_create(&dspi_info_ptr->EVENT_IO_FINISHED, 0); /* Install ISRs */ dspi_info_ptr->NUM_VECTORS = _bsp_get_dspi_vectors(dspi_info_ptr->CHANNEL, &vectors); for (i=0; i<dspi_info_ptr->NUM_VECTORS; i++) { _int_install_isr(vectors[i], _dspi_isr, dspi_info_ptr); _bsp_int_init((PSP_INTERRUPT_TABLE_INDEX)vectors[i], BSP_DSPI_INT_LEVEL, 0, TRUE); } return SPI_OK; }
void ftpsrv_server_task(void* init_ptr, void* creator) { FTPSRV_STRUCT* server = (FTPSRV_STRUCT*) init_ptr; _mqx_uint res; /* Task init start */ if (server == NULL) { RTCS_task_resume_creator(creator, (uint32_t)RTCS_ERROR); } server->server_tid = _task_get_id(); res = _lwsem_create(&server->ses_cnt, server->params.max_ses); if (res != MQX_OK) { server->server_tid = 0; RTCS_task_resume_creator(creator, (uint32_t)RTCS_ERROR); } RTCS_task_resume_creator(creator, RTCS_OK); /* Task init end */ while (server->run) { uint32_t conn_sock = 0; uint32_t new_sock = 0; _lwsem_wait(&server->ses_cnt); while (!conn_sock && server->run) { conn_sock = RTCS_selectset(&(server->sock_v4), 2, 250); } if (server->run) { unsigned short length = 0; struct sockaddr remote_addr; new_sock = accept(conn_sock, (sockaddr*) &remote_addr, &length); } else { break; } if (new_sock != RTCS_SOCKET_ERROR) { FTPSRV_SESSION_PARAM ses_param; ses_param.sock = new_sock; ses_param.server = server; /* Try to create task for session */ res = RTCS_task_create("ftpsrv session", server->params.server_prio, FTPSRV_SESSION_STACK_SIZE, ftpsrv_session_task, &ses_param); if (MQX_OK != res) { shutdown(new_sock, FLAG_ABORT_CONNECTION); _lwsem_post(&server->ses_cnt); } } else { _lwsem_post(&server->ses_cnt); /* We probably run out of sockets. Wait some arbitrary * time and then try again to prevent session tasks resource starvation. */ _time_delay(150); } } _lwsem_destroy(&server->ses_cnt); server->server_tid = 0; }
_ppp_handle PPP_init ( PPP_PARAM_STRUCT* params ) { /* Body */ #if RTCSCFG_ENABLE_IP4 PPP_CFG_PTR ppp_ptr; uint32_t error; IPCP_DATA_STRUCT ipcp_data; int stage = 0; /* Allocate the state structure */ ppp_ptr = _mem_alloc_zero(sizeof(PPP_CFG)); if (!ppp_ptr) { return(NULL); } ppp_ptr->DEVICE_NAME = _mem_alloc_zero(strlen(params->device)+1); if (ppp_ptr->DEVICE_NAME == NULL) { _mem_free(ppp_ptr); return(NULL); } strcpy(ppp_ptr->DEVICE_NAME, params->device); /* Stage 0 - Open low level device */ ppp_ptr->IOPCB_DEVICE = fopen(params->device, NULL); if (ppp_ptr->IOPCB_DEVICE == NULL) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 1 - Initialize HDLC and lwsem */ ppp_ptr->DEVICE = _iopcb_ppphdlc_init(ppp_ptr->IOPCB_DEVICE); if (ppp_ptr->DEVICE == NULL) { PPP_init_fail(ppp_ptr, stage); return(NULL); } ppp_ptr->RECV_OPTIONS = &PPP_DEFAULT_OPTIONS; ppp_ptr->SEND_OPTIONS = &PPP_DEFAULT_OPTIONS; if (_lwsem_create(&ppp_ptr->MUTEX, 1)) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 2 - Initialize LCP */ error = LCP_init(ppp_ptr); if (error) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 3 - Initialize and open CCP */ /* error = CCP_init(ppp_ptr); if (error) { PPP_init_fail(ppp_ptr, stage); return error; } CCP_open(ppp_ptr); */ stage++; /* Stage 4 - Create a pool of message buffers */ ppp_ptr->MSG_POOL = RTCS_msgpool_create(sizeof(PPP_MESSAGE), PPP_MESSAGE_INITCOUNT, PPP_MESSAGE_GROWTH, PPP_MESSAGE_LIMIT); if (ppp_ptr->MSG_POOL == MSGPOOL_NULL_POOL_ID) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 5 - Create the Tx Task */ error = RTCS_task_create("PPP tx", _PPPTASK_priority, _PPPTASK_stacksize + 1000, PPP_tx_task, ppp_ptr); if (error) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 6 - Create the Rx Task */ ppp_ptr->STOP_RX = FALSE; error = RTCS_task_create("PPP rx", _PPPTASK_priority, _PPPTASK_stacksize + 1000, PPP_rx_task, ppp_ptr); if (error) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 7 - Open HDLC layer */ error = _iopcb_open(ppp_ptr->DEVICE, PPP_lowerup, PPP_lowerdown, ppp_ptr); if (error != PPPHDLC_OK) { PPP_init_fail(ppp_ptr, stage); return(NULL); } ppp_ptr->VALID = PPP_VALID; stage++; /* Stage 8 - Add PPP interface to RTCS */ error = RTCS_if_add(ppp_ptr, RTCS_IF_PPP, &ppp_ptr->IF_HANDLE); if (error != RTCS_OK) { PPP_init_fail(ppp_ptr, stage); return(NULL); } stage++; /* Stage 9 - Bind IP address to PPP interface */ _mem_zero(&ipcp_data, sizeof(ipcp_data)); ipcp_data.IP_UP = params->up; ipcp_data.IP_DOWN = params->down; ipcp_data.IP_PARAM = params->callback_param; if(params->listen_flag == 0) { ipcp_data.ACCEPT_LOCAL_ADDR = TRUE; ipcp_data.ACCEPT_REMOTE_ADDR = TRUE; } else { ipcp_data.ACCEPT_LOCAL_ADDR = FALSE; ipcp_data.ACCEPT_REMOTE_ADDR = FALSE; } ipcp_data.LOCAL_ADDR = params->local_addr; ipcp_data.REMOTE_ADDR = params->remote_addr; ipcp_data.DEFAULT_NETMASK = TRUE; ipcp_data.DEFAULT_ROUTE = TRUE; error = RTCS_if_bind_IPCP(ppp_ptr->IF_HANDLE, &ipcp_data); if (error != RTCS_OK) { PPP_init_fail(ppp_ptr, stage); return(NULL); } params->if_handle = ppp_ptr->IF_HANDLE; stage++; /* Stage 10 - Init complete, return handle */ return(ppp_ptr); #else return(NULL); #endif /* RTCSCFG_ENABLE_IP4 */ } /* Endbody */
_mqx_int _io_pipe_open ( /* [IN] the file handle for the device being opened */ FILE_DEVICE_STRUCT_PTR fd_ptr, /* [IN] the remaining portion of the name of the device */ char _PTR_ open_name_ptr, /* [IN] the flags to be used during operation: ** echo, translation, xon/xoff */ char _PTR_ flags ) { /* Body */ IO_DEVICE_STRUCT_PTR io_dev_ptr = fd_ptr->DEV_PTR; IO_PIPE_INIT_STRUCT_PTR io_pipe_init_ptr; IO_PIPE_INFO_STRUCT_PTR io_pipe_info_ptr; MUTEX_ATTR_STRUCT mutex_attr; uint_32 result = MQX_OK; /* Allocate a Pipe information structure */ io_pipe_info_ptr = _mem_alloc_system_zero( (uint_32)sizeof(IO_PIPE_INFO_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (io_pipe_info_ptr == NULL){ return(MQX_OUT_OF_MEMORY); }/* Endif */ #endif fd_ptr->DEV_DATA_PTR = io_pipe_info_ptr; io_pipe_init_ptr = (IO_PIPE_INIT_STRUCT_PTR)io_dev_ptr->DRIVER_INIT_PTR; io_pipe_info_ptr->QUEUE_SIZE = io_pipe_init_ptr->QUEUE_SIZE; /* Initialize mutexes */ result = _mutatr_init(&mutex_attr); #if MQX_CHECK_ERRORS if (result != MQX_EOK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif _mutatr_set_wait_protocol(&mutex_attr, MUTEX_PRIORITY_QUEUEING); _mutatr_set_sched_protocol(&mutex_attr, MUTEX_PRIO_INHERIT); result = _mutex_init(&io_pipe_info_ptr->READ_MUTEX, &mutex_attr); #if MQX_CHECK_ERRORS if (result != MQX_EOK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif result = _mutex_init(&io_pipe_info_ptr->WRITE_MUTEX, &mutex_attr); #if MQX_CHECK_ERRORS if (result != MQX_EOK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif result = _mutex_init(&io_pipe_info_ptr->ACCESS_MUTEX, &mutex_attr); #if MQX_CHECK_ERRORS if (result != MQX_EOK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif /* Initialize semaphores */ result = _lwsem_create(&io_pipe_info_ptr->FULL_SEM, 0); #if MQX_CHECK_ERRORS if (result != MQX_OK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif result = _lwsem_create(&io_pipe_info_ptr->EMPTY_SEM, 0); #if MQX_CHECK_ERRORS if (result != MQX_OK) { _mem_free(io_pipe_info_ptr); return (result); } /* Endif */ #endif /* Allocate queue structure for pipe char queue */ io_pipe_info_ptr->QUEUE = (pointer)_mem_alloc_system( sizeof(CHARQ_STRUCT) - (4 * sizeof(char)) + io_pipe_info_ptr->QUEUE_SIZE); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (io_pipe_info_ptr->QUEUE == NULL){ _mem_free(io_pipe_info_ptr); return(MQX_OUT_OF_MEMORY); }/* Endif */ #endif /* Initialize Pipe queue */ _CHARQ_INIT(io_pipe_info_ptr->QUEUE, io_pipe_init_ptr->QUEUE_SIZE); return(result); } /* Endbody */
/*! * \brief Initializes and starts MQX on the processor. * * The function does the following: * \li Initializes the default memory pool and memory components. * \li Initializes kernel data. * \li Performs BSP-specific initialization, which includes installing the * periodic timer. * \li Performs PSP-specific initialization. * \li Creates the interrupt stack. * \li Creates the ready queues. * \li Starts MQX tasks. * \li Starts autostart application tasks. * * \param[in] mqx_init Pointer to the MQX initialization structure for the * processor. * * \return Does not return (Success.) * \return If application called _mqx_exit(), error code that it passed to * _mqx_exit() (Success.) * \return Errors from _int_install_isr() (MQX cannot install the interrupt * subsystem.) * \return Errors from _io_init() (MQX cannot install the I/O subsystem.) * \return Errors from _mem_alloc_system() (There is not enough memory to * allocate either the interrupt stack or the interrupt table.) * \return Errors from _mem_alloc_zero() (There is not enough memory to allocate * the ready queues.) * \return MQX_KERNEL_MEMORY_TOO_SMALL (Init_struct_ptr does not specify enough * kernel memory.) * \return MQX_OUT_OF_MEMORY (There is not enough memory to allocate either the * ready queues, the interrupt stack, or the interrupt table.) * \return MQX_TIMER_ISR_INSTALL_FAIL (MQX cannot install the periodic timer ISR.) * * \warning Must be called exactly once per processor. * * \see _mqx_exit * \see _int_install_isr * \see _mem_alloc() * \see _mem_alloc_from() * \see _mem_alloc_system() * \see _mem_alloc_system_from() * \see _mem_alloc_system_zero() * \see _mem_alloc_system_zero_from() * \see _mem_alloc_zero() * \see _mem_alloc_zero_from() * \see _mem_alloc_align() * \see _mem_alloc_align_from() * \see _mem_alloc_at() * \see MQX_INITIALIZATION_STRUCT * \see TASK_TEMPLATE_STRUCT */ _mqx_uint _mqx ( register MQX_INITIALIZATION_STRUCT_PTR mqx_init ) { /* Body */ KERNEL_DATA_STRUCT_PTR kernel_data; TASK_TEMPLATE_STRUCT_PTR template_ptr; TD_STRUCT_PTR td_ptr; _mqx_uint result; pointer stack_ptr; pointer sys_td_stack_ptr; uchar_ptr sys_stack_base_ptr; #if MQX_EXIT_ENABLED || MQX_CRIPPLED_EVALUATION /* Setup a longjmp buffer using setjmp, so that if an error occurs * in mqx initialization, we can perform a longjmp to this location. * * Also _mqx_exit will use this jumpbuffer to longjmp to here in order * to cleanly exit MQX. */ if ( MQX_SETJMP( _mqx_exit_jump_buffer_internal ) ) { _GET_KERNEL_DATA(kernel_data); _int_set_vector_table(kernel_data->USERS_VBR); return kernel_data->USERS_ERROR; } /* Endif */ #endif /* * The kernel data structure starts at the start of kernel memory, * as specified in the initialization structure. Make sure address * specified is aligned */ kernel_data = (KERNEL_DATA_STRUCT_PTR) _ALIGN_ADDR_TO_HIGHER_MEM(mqx_init->START_OF_KERNEL_MEMORY); /* Set the global pointer to the kernel data structure */ _SET_KERNEL_DATA(kernel_data); /* The following assignments are done to force the linker to include * the symbols, which are required by TAD. * Note that we should use address of the variable so it is not optimized * as direct constant assignment when optimization level is high. * Note that counter will be immediately reset to zero on the subsequent * _mem_zero call. */ *(volatile pointer*) kernel_data = (pointer) & _mqx_version_number; *(volatile pointer*) kernel_data = (pointer) & _mqx_vendor; /* Initialize the kernel data to zero. */ _mem_zero((pointer) kernel_data, (_mem_size) sizeof(KERNEL_DATA_STRUCT)); #if MQX_CHECK_ERRORS && MQX_VERIFY_KERNEL_DATA /* Verify that kernel data can be read and written correcly without * errors. This is necessary during BSP development to validate the * DRAM controller is initialized properly. */ #ifndef PSP_KERNEL_DATA_VERIFY_ENABLE #define PSP_KERNEL_DATA_VERIFY_ENABLE 0 #endif /* PSP_KERNEL_DATA_VERIFY_ENABLE */ if (PSP_KERNEL_DATA_VERIFY_ENABLE) { /* This memory check is dangerous, because can destroy boot stack * stack which is used !!! -> MQX will failed ! * Set PSP_KERNEL_DATA_VERIFY_ENABLE to 1 * only if your boot stack is out of MQX memory heap */ result = _mem_verify((uchar_ptr)kernel_data + sizeof(KERNEL_DATA_STRUCT), mqx_init->END_OF_KERNEL_MEMORY); if ( result != MQX_OK ) { _mqx_exit(result); /* RETURN TO USER */ } } #endif /* MQX_CHECK_ERRORS && MQX_VERIFY_KERNEL_DATA */ /* Copy the MQX initialization structure into kernel data. */ kernel_data->INIT = *mqx_init; kernel_data->INIT.START_OF_KERNEL_MEMORY = (pointer) kernel_data; kernel_data->INIT.END_OF_KERNEL_MEMORY = (pointer) _ALIGN_ADDR_TO_LOWER_MEM(kernel_data->INIT.END_OF_KERNEL_MEMORY); /* init kernel data structures */ _mqx_init_kernel_data_internal(); /* Initialize the memory resource manager for the kernel */ result = _mem_init_internal(); #if MQX_CHECK_ERRORS if ( result != MQX_OK ) { _mqx_exit(result); /* RETURN TO USER */ } /* Endif */ #endif #if MQX_USE_INTERRUPTS /* Now obtain the interrupt stack */ if (kernel_data->INIT.INTERRUPT_STACK_LOCATION) { stack_ptr = kernel_data->INIT.INTERRUPT_STACK_LOCATION; result = kernel_data->INIT.INTERRUPT_STACK_SIZE; } else { if ( kernel_data->INIT.INTERRUPT_STACK_SIZE < PSP_MINSTACKSIZE ) { kernel_data->INIT.INTERRUPT_STACK_SIZE = PSP_MINSTACKSIZE; } /* Endif */ #if PSP_STACK_ALIGNMENT result = kernel_data->INIT.INTERRUPT_STACK_SIZE + PSP_STACK_ALIGNMENT + 1; #else result = kernel_data->INIT.INTERRUPT_STACK_SIZE; #endif stack_ptr = _mem_alloc_system((_mem_size)result); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (stack_ptr == NULL) { _mqx_exit(MQX_OUT_OF_MEMORY); /* RETURN TO USER */ } /* Endif */ #endif _mem_set_type(stack_ptr, MEM_TYPE_INTERRUPT_STACK); } /* Endif */ #if MQX_MONITOR_STACK _task_fill_stack_internal((_mqx_uint_ptr)stack_ptr, result); #endif kernel_data->INTERRUPT_STACK_PTR = _GET_STACK_BASE(stack_ptr, result); #endif /* * Set the stack for the system TD, in case the idle task gets blocked * by an exception or if idle task is not used. */ result = PSP_MINSTACKSIZE; sys_td_stack_ptr = _mem_alloc_system((_mem_size) result); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (sys_td_stack_ptr == NULL) { _mqx_exit(MQX_OUT_OF_MEMORY); /* RETURN TO USER */ } /* Endif */ #endif _mem_set_type(sys_td_stack_ptr, MEM_TYPE_SYSTEM_STACK); sys_stack_base_ptr = (uchar_ptr) _GET_STACK_BASE(sys_td_stack_ptr, result); td_ptr = SYSTEM_TD_PTR(kernel_data); td_ptr->STACK_PTR = (pointer)(sys_stack_base_ptr - sizeof(PSP_STACK_START_STRUCT)); td_ptr->STACK_BASE = sys_stack_base_ptr; #if MQX_TD_HAS_STACK_LIMIT td_ptr->STACK_LIMIT = _GET_STACK_LIMIT(sys_td_stack_ptr, result); #endif _mqx_system_stack = td_ptr->STACK_PTR; /* Build the MQX ready to run queues */ result = _psp_init_readyqs(); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if ( result != MQX_OK ) { _mqx_exit(result); /* RETURN TO USER */ } /* Endif */ #endif #if MQX_USE_COMPONENTS /* Create a light wait semaphore for component creation */ _lwsem_create((LWSEM_STRUCT_PTR)&kernel_data->COMPONENT_CREATE_LWSEM, 1); #endif /* Create a light wait semaphore for task creation/destruction creation */ _lwsem_create((LWSEM_STRUCT_PTR) & kernel_data->TASK_CREATE_LWSEM, 1); /* Call bsp to enable timers and other devices */ result = _bsp_enable_card(); #if MQX_CHECK_ERRORS if ( result != MQX_OK ) { _mqx_exit(result); /* RETURN TO USER */ } /* Endif */ #endif #if MQX_HAS_TIME_SLICE /* Set the kernel default time slice value */ PSP_ADD_TICKS_TO_TICK_STRUCT(&kernel_data->SCHED_TIME_SLICE, MQX_DEFAULT_TIME_SLICE, &kernel_data->SCHED_TIME_SLICE); #endif /* Create the idle task */ #if MQX_USE_IDLE_TASK td_ptr = _task_init_internal( (TASK_TEMPLATE_STRUCT_PTR)&kernel_data->IDLE_TASK_TEMPLATE, kernel_data->ACTIVE_PTR->TASK_ID, MQX_IDLE_TASK_PARAMETER, TRUE, NULL, 0); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (td_ptr == NULL) { _mqx_exit(MQX_OUT_OF_MEMORY); } /* Endif */ #endif _task_ready_internal(td_ptr); #endif /* Check here for auto-create tasks, and create them here */ template_ptr = kernel_data->INIT.TASK_TEMPLATE_LIST; while (template_ptr->TASK_TEMPLATE_INDEX) { if (template_ptr->TASK_ATTRIBUTES & MQX_AUTO_START_TASK) { td_ptr = _task_init_internal(template_ptr, kernel_data->ACTIVE_PTR->TASK_ID, template_ptr->CREATION_PARAMETER, FALSE, NULL, 0); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (td_ptr == NULL) { _mqx_exit(MQX_OUT_OF_MEMORY); } /* Endif */ #endif _task_ready_internal(td_ptr); } /* Endif */ ++template_ptr; } /* Endwhile */ _sched_start_internal(); /* WILL NEVER RETURN FROM HERE */ return MQX_OK; /* To satisfy lint */ } /* Endbody */
_mqx_uint _io_serial_int_install ( /* [IN] A string that identifies the device for fopen */ char_ptr identifier, /* [IN] The I/O init function */ _mqx_uint (_CODE_PTR_ init)(pointer, char _PTR_), /* [IN] The enable interrupts function */ _mqx_uint (_CODE_PTR_ enable_ints)(pointer), /* [IN] The I/O de-init function */ _mqx_uint (_CODE_PTR_ deinit)(pointer, pointer), /* [IN] The output function */ void (_CODE_PTR_ putc)(pointer, char), /* [IN] The I/O ioctl function */ _mqx_uint (_CODE_PTR_ ioctl)(pointer, _mqx_uint, pointer), /* [IN] The I/O init data pointer */ pointer init_data_ptr, /* [IN] The I/O queue size to use */ _mqx_uint queue_size ) { /* Body */ IO_SERIAL_INT_DEVICE_STRUCT_PTR int_io_dev_ptr; uint_32 result; int_io_dev_ptr = _mem_alloc_system_zero( (_mem_size)sizeof(IO_SERIAL_INT_DEVICE_STRUCT)); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if (int_io_dev_ptr == NULL) { return(MQX_OUT_OF_MEMORY); } /* Endif */ #endif _mem_set_type(int_io_dev_ptr,MEM_TYPE_IO_SERIAL_INT_DEVICE_STRUCT); int_io_dev_ptr->DEV_INIT = init; int_io_dev_ptr->DEV_ENABLE_INTS = enable_ints; int_io_dev_ptr->DEV_DEINIT = deinit; int_io_dev_ptr->DEV_PUTC = putc; int_io_dev_ptr->DEV_IOCTL = ioctl; int_io_dev_ptr->DEV_INIT_DATA_PTR = init_data_ptr; int_io_dev_ptr->QUEUE_SIZE = queue_size; result = _io_dev_install(identifier, _io_serial_int_open, _io_serial_int_close, _io_serial_int_read, _io_serial_int_write, _io_serial_int_ioctl, (pointer)int_io_dev_ptr); #if MQX_ENABLE_LOW_POWER if (MQX_OK == result) { LPM_REGISTRATION_STRUCT registration; registration.CLOCK_CONFIGURATION_CALLBACK = _io_serial_int_clock_configuration_callback; registration.OPERATION_MODE_CALLBACK = _io_serial_int_operation_mode_callback; registration.DEPENDENCY_LEVEL = BSP_LPM_DEPENDENCY_LEVEL_SERIAL_INT; result = _lpm_register_driver (®istration, int_io_dev_ptr, &(int_io_dev_ptr->LPM_INFO.REGISTRATION_HANDLE)); if (MQX_OK == result) { _lwsem_create (&(int_io_dev_ptr->LPM_INFO.LOCK), 1); int_io_dev_ptr->LPM_INFO.FLAGS = 0; } } #endif return result; } /* Endbody */
void LogInit(void) { _lwsem_create(&Logging_init_sem,0); _task_create(0, LOGGING_TASK, 0); _lwsem_wait(&Logging_init_sem); }
/*TASK*----------------------------------------------------- * * Task Name : sdcard_task * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void sdcard_task(void) { /* Body */ int dev_fd = -1, a_fd = -1; bool inserted = FALSE, last = FALSE; int32_t error_code; uint32_t isFormatted = 0; int esdhc_handle = -1; char partman_name[] = "pm:"; char partition_name[] = "pm:1"; int partition_handle; SDCARD_INIT_STRUCT sdcard_init_data; LWSEM_STRUCT lwsem; /* Create and post card detection semaphore */ if( _lwsem_create(&lwsem, 0) != MQX_OK ){ printf("\nlwsem create failed!"); _task_block(); } if( _lwsem_post(&lwsem) != MQX_OK ){ printf("\nlwsem post failed!"); _task_block(); } #ifdef BOARD_SDHC_GPIO_INSTANCE configure_gpio_pins(GPIO_EXTRACT_PORT(BOARD_SDHC_GPIO_INSTANCE)); #endif /* initialize SDHC pins */ configure_sdhc_pins(BOARD_SDHC_INSTANCE); /* install SDHC low-level driver */ _nio_dev_install("esdhc:", &nio_esdhc_dev_fn, (void*)&_bsp_esdhc_init, NULL); /* get an instance of the SDHC driver */ esdhc_handle = open("esdhc:", 0); if(esdhc_handle < 0) { printf("\nCould not open esdhc!"); _task_block(); } /* prepare init data structure */ sdcard_init_data.com_dev = esdhc_handle; sdcard_init_data.const_data = (SDCARD_CONST_INIT_STRUCT_PTR)&sdcard_esdhc_init_data; /* install device */ if (_nio_dev_install("sdcard:", &nio_sdcard_dev_fn, (void*)&sdcard_init_data, NULL) == NULL) { /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ printf("\nError installing memory device"); _task_block(); } /* Endif */ /* install isr for card detection handling and initialize gpio pin */ _int_install_isr(g_portIrqId[GPIO_EXTRACT_PORT(CDET_PIN->pinName)], card_detect_isr, &lwsem); GPIO_DRV_InputPinInit(CDET_PIN); for(;;){ /* Wait for card insertion or removal */ if( _lwsem_wait(&lwsem) != MQX_OK ){ printf("\nlwsem_wait failed!"); _task_block(); } /* Check if card is present */ if(CDET_PIN->config.pullSelect == kPortPullDown) inserted = GPIO_DRV_ReadPinInput(CDET_PIN->pinName); else inserted = !GPIO_DRV_ReadPinInput(CDET_PIN->pinName); if(last != inserted){ last = inserted; /* Card detection switch debounce delay */ _time_delay(100); if(inserted) { /* Open the device which MFS will be installed on */ dev_fd = open("sdcard:", O_RDWR); if ( dev_fd <= 0 ) { printf("\nUnable to open SDCARD device"); _task_block(); } /* Endif */ /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(dev_fd, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint32_t)error_code)); _task_block(); } /* Open partition */ partition_handle = open(partition_name, O_RDWR); if (partition_handle >= 0) { printf("Installing MFS over partition...\n"); /* Validate partition */ error_code = ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error validating partition: %s\n", MFS_Error_text((uint32_t)error_code)); printf("Not installing MFS.\n"); _task_block(); } /* Install MFS over partition */ error_code = _io_mfs_install(partition_handle, "a:", 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint32_t)error_code)); } } else { printf("Installing MFS over SD card driver...\n"); /* Install MFS over SD card driver */ error_code = _io_mfs_install(dev_fd, "a:", (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint32_t)error_code)); } } /* Open the filesystem and format detect, if format is required */ a_fd = open("a:", O_RDWR); if (0 >= a_fd) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint32_t)errno)); _task_block(); } _io_register_file_system(a_fd, "a:"); /* We check if the device is formatted with the ioctl command. */ error_code = ioctl(a_fd, IO_IOCTL_CHECK_FORMATTED, &isFormatted); if (0 > error_code) { printf("\nError while accessing a:\\ (%s)", MFS_Error_text((uint32_t)error_code)); _task_block(); } else{ if(!isFormatted){ printf("\nNOT A DOS DISK! You must format to continue."); } } } else { /* Close the filesystem */ if ((a_fd >= 0) && (MQX_OK != close(a_fd))) { printf("Error closing filesystem.\n"); } a_fd = -1; /* Force uninstall filesystem */ error_code = _nio_dev_uninstall_force("a:", NULL); if (error_code < 0) { printf("Error uninstalling filesystem.\n"); } /* Close partition */ if ((partition_handle >= 0) && (MQX_OK != close(partition_handle))) { printf("Error closing partition.\n"); } partition_handle = -1; /* Uninstall partition manager */ error_code = _nio_dev_uninstall_force(partman_name, NULL); if (error_code < 0) { printf("Error uninstalling partition manager.\n"); } /* Close the SD card device */ if ((dev_fd >= 0) && (MQX_OK != close(dev_fd))) { printf("Error closing SD card device.\n"); } dev_fd = -1; printf ("SD card uninstalled.\n"); } } } } /* Endbody */
void USB_task(uint_32 param) { _usb_host_handle host_handle; USB_STATUS error; pointer usb_fs_handle = NULL; usb_msg_t msg; /* Store mounting point used. A: is the first one, bit #0 assigned, Z: is the last one, bit #25 assigned */ uint_32 fs_mountp = 0; #if DEMOCFG_USE_POOLS && defined(DEMOCFG_MFS_POOL_ADDR) && defined(DEMOCFG_MFS_POOL_SIZE) _MFS_pool_id = _mem_create_pool((pointer)DEMOCFG_MFS_POOL_ADDR, DEMOCFG_MFS_POOL_SIZE); #endif /* This event will inform other tasks that the filesystem on USB was successfully installed */ _lwsem_create(&USB_Stick, 0); if (MQX_OK != _lwmsgq_init(usb_taskq, 20, USB_TASKQ_GRANM)) { // lwmsgq_init failed _task_block(); } USB_lock(); _int_install_unexpected_isr(); if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) { printf("\n Driver installation failed"); _task_block(); } error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle); if (error == USB_OK) { error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable); if (error == USB_OK) { error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL); } } USB_unlock(); if (error != USB_OK) { _task_block(); } for (;;) { /* Wait for event sent as a message */ _lwmsgq_receive(&usb_taskq, (_mqx_max_type *) &msg, LWMSGQ_RECEIVE_BLOCK_ON_EMPTY, 0, 0); //if (device.STATE == USB_DEVICE_ATTACHED) { if (msg.body == USB_EVENT_ATTACH) { /* This event is not so important, because it does not inform about successfull USB stack enumeration */ } else if (msg.body == USB_EVENT_INTF && fs_mountp != 0x3FFC) { /* if mountpoints c: to z: are already used */ // Install the file system, use device->ccs as a handle usb_fs_handle = usb_filesystem_install( (pointer) msg.ccs, "USB:", "PM_C1:", "c:"); if (usb_fs_handle) { DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs; dsp->mount = 'c'; // Mark file system as mounted fs_mountp |= 1 << (dsp->mount - 'a'); // Unlock the USB_Stick = signal to the application as available _lwsem_post(&USB_Stick); } } else if (msg.body == USB_EVENT_DETACH) { DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs; if (dsp->mount >= 'a' && dsp->mount <= 'z') { // Lock the USB_Stick = mark as unavailable _lwsem_wait(&USB_Stick); // Remove the file system usb_filesystem_uninstall(usb_fs_handle); // Mark file system as unmounted fs_mountp &= ~(1 << (dsp->mount - 'a')); } /* Here, the device finishes its lifetime */ _mem_free(dsp); } } }
/*! * \brief Initializes MQXLite on the processor. * * The function does the following: * \n - Initializes kernel data. * \n - Creates the interrupt stack. * \n - Creates the ready queues. * \n - Creates a lightweight semaphore for task creation/destruction. * \n - Initializes interrupts. * \n - Initializes system timer. * * \param[in] mqx_init Pointer to the MQXLITE initialization structure for the * processor. * * \return MQX_OK * \return Initialization error code * * \warning Must be called exactly once per processor. * * \see _mqxlite * \see _mqx_exit * \see MQXLITE_INITIALIZATION_STRUCT */ _mqx_uint _mqxlite_init ( MQXLITE_INITIALIZATION_STRUCT const * mqx_init ) { /* Body */ KERNEL_DATA_STRUCT_PTR kernel_data; pointer stack_ptr; _mqx_uint result = MQX_OK; /* * The kernel data structure starts at the start of kernel memory, * as specified in the initialization structure. Make sure address * specified is aligned */ kernel_data = (KERNEL_DATA_STRUCT_PTR) (mqx_init->START_OF_KERNEL_MEMORY); /* Set the global pointer to the kernel data structure */ _SET_KERNEL_DATA(kernel_data); /* The following assignments are done to force the linker to include * the symbols, which are required by TAD. * Note that we should use address of the variable so it is not optimized * as direct constant assignment when optimization level is high. * Note that counter will be immediately reset to zero on the subsequent * _mem_zero call. */ { MQX_INITIALIZATION_STRUCT * MQX_init_struct_ptr; *(volatile pointer*) kernel_data = (pointer) & _mqx_version_number; *(volatile pointer*) kernel_data = (pointer) & _mqx_vendor; *(volatile pointer*) kernel_data = (pointer) & _mqx_path; *(volatile pointer*) kernel_data = (pointer) & _mqxlite_version_number; *(volatile pointer*) kernel_data = (pointer) & MQX_init_struct_ptr; } /* Initialize the kernel data to zero. */ _mem_zero((pointer) kernel_data, (_mem_size) sizeof(KERNEL_DATA_STRUCT)); #if MQX_CHECK_ERRORS && MQX_VERIFY_KERNEL_DATA /* Verify that kernel data can be read and written correctly without * errors. This is necessary during BSP development to validate the * DRAM controller is initialized properly. */ if (PSP_KERNEL_DATA_VERIFY_ENABLE) { /* This memory check is dangerous, because can destroy boot stack * stack which is used !!! -> MQX will failed ! * Set PSP_KERNEL_DATA_VERIFY_ENABLE to 1 * only if your boot stack is out of MQX memory heap */ result = _mem_verify((uchar_ptr)kernel_data + sizeof(KERNEL_DATA_STRUCT), mqx_init->END_OF_KERNEL_MEMORY); if ( result != MQX_OK ) { return (result); /* RETURN TO USER */ } } #endif /* MQX_CHECK_ERRORS && MQX_VERIFY_KERNEL_DATA */ /* Copy the MQX initialization structure into kernel data. */ kernel_data->INIT = *mqx_init; /* init kernel data structures */ _mqx_init_kernel_data_internal(); #if MQX_USE_LWMEM == 1 /** * Initialize lightweight memory pool for dynamic memory allocation */ { /* Extern symbols defined in linker command file */ extern char __heap_addr[]; extern char __heap_size[]; KERNEL_DATA_STRUCT * kernel_data; LWMEM_POOL_STRUCT * pool_ptr; void * start_addr; _GET_KERNEL_DATA(kernel_data); pool_ptr = (LWMEM_POOL_STRUCT *) __heap_addr; kernel_data->KERNEL_LWMEM_POOL = pool_ptr; start_addr = (void *)((char *) __heap_addr + sizeof(LWMEM_POOL_STRUCT)); _lwmem_create_pool(pool_ptr, start_addr, (_mem_size)__heap_size); } #endif #if MQX_USE_INTERRUPTS /* Now obtain the interrupt stack */ if (kernel_data->INIT.INTERRUPT_STACK_LOCATION) { stack_ptr = kernel_data->INIT.INTERRUPT_STACK_LOCATION; result = kernel_data->INIT.INTERRUPT_STACK_SIZE; } else { return (MQX_INVALID_PARAMETER); } /* Endif */ #if MQX_MONITOR_STACK _task_fill_stack_internal((_mqx_uint_ptr)stack_ptr, result); #endif kernel_data->INTERRUPT_STACK_PTR = _GET_STACK_BASE(stack_ptr, result); #endif /* MQX_USE_INTERRUPTS */ #if MQX_USE_IDLE_TASK == 0 { /* * Set the stack for the system TD, in case the idle task gets blocked * by an exception or if idle task is not used. */ TD_STRUCT_PTR td_ptr; uchar_ptr stack_base_ptr; stack_base_ptr = (uchar_ptr) _GET_STACK_BASE(mqx_system_stack, PSP_MINSTACKSIZE); td_ptr = SYSTEM_TD_PTR(kernel_data); td_ptr->STACK_PTR = (pointer)(stack_base_ptr - sizeof(PSP_STACK_START_STRUCT)); td_ptr->STACK_BASE = stack_base_ptr; #if MQX_TD_HAS_STACK_LIMIT td_ptr->STACK_LIMIT = _GET_STACK_LIMIT(mqx_system_stack, PSP_MINSTACKSIZE); #endif _mqx_system_stack = td_ptr->STACK_PTR; } #endif /* MQX_USE_IDLE_TASK */ /* Build the MQX ready to run queues */ result = _psp_init_readyqs(); #if MQX_CHECK_MEMORY_ALLOCATION_ERRORS if ( result != MQX_OK ) { return (result); /* RETURN TO USER */ } /* Endif */ #endif #if MQX_USE_COMPONENTS /* Create a light wait semaphore for component creation */ _lwsem_create((LWSEM_STRUCT_PTR)&kernel_data->COMPONENT_CREATE_LWSEM, 1); #endif /* Create a light wait semaphore for task creation/destruction creation */ _lwsem_create((LWSEM_STRUCT_PTR) & kernel_data->TASK_CREATE_LWSEM, 1); /* Set the CPU type */ _mqx_set_cpu_type(MQX_CPU); result = _psp_int_init(FIRST_INTERRUPT_VECTOR_USED, LAST_INTERRUPT_VECTOR_USED); if (result != MQX_OK) { return(result); /* RETURN TO USER */ } /* set possible new interrupt vector table * if MQX_ROM_VECTORS = 0 switch to ram interrupt table which * was initialized in _psp_int_init) */ _int_set_vector_table((uint32_t)(&__vect_table)); /* * Initialize System Timer and Ticks parameters in kernel_data structure */ system_timer_init(NULL); return MQX_OK; /* To satisfy lint */ } /* Endbody */