OSStatus MicoSpiTransfer( const mico_spi_device_t* spi, const mico_spi_message_segment_t* segments, uint16_t number_of_segments ) { platform_spi_config_t config; OSStatus err = kNoErr; if ( spi->port >= MICO_SPI_NONE ) return kUnsupportedErr; #ifdef MICO_WIFI_SHARE_SPI_BUS if( platform_spi_peripherals[spi->port].port == wifi_spi.port ) { return platform_wlan_spi_transfer( &platform_gpio_pins[spi->chip_select], segments, number_of_segments ); } #endif if( platform_spi_drivers[spi->port].spi_mutex == NULL) mico_rtos_init_mutex( &platform_spi_drivers[spi->port].spi_mutex ); config.chip_select = &platform_gpio_pins[spi->chip_select]; config.speed = spi->speed; config.mode = spi->mode; config.bits = spi->bits; mico_rtos_lock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); err = platform_spi_init( &platform_spi_drivers[spi->port], &platform_spi_peripherals[spi->port], &config ); err = platform_spi_transfer( &platform_spi_drivers[spi->port], &config, segments, number_of_segments ); mico_rtos_unlock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); return err; }
OSStatus platform_flash_init( platform_flash_driver_t *driver, const platform_flash_t *peripheral ) { OSStatus err = kNoErr; require_action_quiet( driver != NULL && peripheral != NULL, exit, err = kParamErr); require_action_quiet( driver->initialized == false, exit, err = kNoErr); driver->peripheral = (platform_flash_t *)peripheral; if( driver->peripheral->flash_type == FLASH_TYPE_INTERNAL ){ err = internalFlashInitialize(); require_noerr(err, exit); } #ifdef USE_MICO_SPI_FLASH else if( driver->peripheral->flash_type == FLASH_TYPE_SPI ){ err = init_sflash( &sflash_handle, 0, SFLASH_WRITE_ALLOWED ); require_noerr(err, exit); } #endif else{ err = kTypeErr; goto exit; } #ifndef NO_MICO_RTOS err = mico_rtos_init_mutex( &driver->flash_mutex ); #endif require_noerr(err, exit); driver->initialized = true; exit: return err; }
int application_start( void ) { OSStatus err = kNoErr; /* Create a mutex*/ err = mico_rtos_init_mutex( &mutex); require_noerr(err, exit); /* Create threads */ err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY+1, "t1", run, 0x800, p_name1 ); require_noerr(err, exit); err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY+1, "t2", run, 0x800, p_name2 ); require_noerr(err, exit); err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY+1, "t3", run, 0x800, p_name3 ); require_noerr(err, exit); err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY+1, "t4", run, 0x800, p_name4 ); require_noerr(err, exit); exit: if( err != kNoErr ) os_mutex_log( "Thread exit with err: %d", err ); mico_rtos_delete_thread(NULL); return err; }
OSStatus sppProtocolInit(app_context_t * const inContext) { int i; spp_log_trace(); (void)inContext; for(i=0; i < MAX_QUEUE_NUM; i++) { inContext->appStatus.socket_out_queue[i] = NULL; } mico_rtos_init_mutex(&inContext->appStatus.queue_mtx); return kNoErr; }
// Object initialization void ObjectInit(void) { OSStatus err; memset(object, 0, MAX_OBJECT_NUM * sizeof(SObjectMng)); memset(objectBackup, 0, MAX_OBJECT_NUM * sizeof(SObjectMng)); err = mico_rtos_init_mutex(&ObjectMutex); if(err) { Object_ERR("ObjectInit: ObjectMutex initialized failed"); } Object_INF("ObjectInit Finished"); }
void init_architecture( void){ /*wakeup by watchdog in standby mode, re-enter standby mode in this situation*/ /**/ if(platform_inited == 1) return; #ifndef MICO_DISABLE_STDIO #ifndef NO_MICO_RTOS mico_rtos_init_mutex( &stdio_tx_mutex ); mico_rtos_unlock_mutex ( &stdio_tx_mutex ); mico_rtos_init_mutex( &stdio_rx_mutex ); mico_rtos_unlock_mutex ( &stdio_rx_mutex ); #endif ring_buffer_init ( (ring_buffer_t*)&stdio_rx_buffer, (uint8_t*)stdio_rx_data, STDIO_BUFFER_SIZE ); MicoStdioUartInitialize( &stdio_uart_config, (ring_buffer_t*)&stdio_rx_buffer ); #endif SystemCoreClockUpdate();//cp from set() platform_inited = 1; }
OSStatus MicoSpiFinalize( const mico_spi_device_t* spi ) { OSStatus err = kNoErr; if ( spi->port >= MICO_SPI_NONE ) return kUnsupportedErr; if( platform_spi_drivers[spi->port].spi_mutex == NULL) mico_rtos_init_mutex( &platform_spi_drivers[spi->port].spi_mutex ); mico_rtos_lock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); err = platform_spi_deinit( &platform_spi_drivers[spi->port] ); mico_rtos_unlock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); return err; }
OSStatus MicoFlashInitialize( mico_flash_t flash ) { OSStatus err = kNoErr; if( platform_flash_drivers[flash].flash_mutex == NULL){ err = mico_rtos_init_mutex( &platform_flash_drivers[flash].flash_mutex ); require_noerr(err, exit); } mico_rtos_lock_mutex( &platform_flash_drivers[flash].flash_mutex ); err = platform_flash_init( &platform_flash_drivers[flash], &platform_flash_peripherals[flash] ); mico_rtos_unlock_mutex( &platform_flash_drivers[flash].flash_mutex ); exit: return err; }
void Platform_Init(void) { /*STM32 wakeup by watchdog in standby mode, re-enter standby mode in this situation*/ PlatformWDGReload(); if ( (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) && RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { RCC_ClearFlag(); Platform_Enter_STANDBY(); } PWR_ClearFlag(PWR_FLAG_SB); mico_rtos_init_mutex(&printf_mutex); Platform_Button_EL_Init(); Platform_Button_STANDBY_Init(); Platform_LED_SYS_Init(); Platform_LED_RF_Init(); Platform_Debug_UART_Init(); }
void SysComInit() { u8 i; char queue_name[ESM_SYSCOM_QUEUE_NAME_MAX_LENGTH]; OSStatus err = kUnknownErr; err = mico_rtos_init_mutex(&SysComMutex); if(err != kNoErr) { SysCom_ERR("SysComInit: mutex initialized failed"); } for(i=0; i<MAX_THREAD_NUM; i++) { sprintf(queue_name, "SysCom Queue %d", i); err = mico_rtos_init_queue(&SysComQueue[i], queue_name, sizeof(addP_t), MAX_SYSCOM_NUM); if(err != kNoErr) { SysCom_ERR("SysComInit: SysComQueue[%d] initialized failed", i); } } SysCom_DBG("SysComInit() finished"); }
void Platform_Debug_UART_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; mico_rtos_init_mutex( &printf_mutex ); DEBUG_GPIO_CLK_INIT(DEBUG_USARTx_RX_GPIO_CLK, ENABLE); DEBUG_USARTx_CLK_INIT(DEBUG_USARTx_CLK, ENABLE); /* Configure USART pin*/ GPIO_PinAFConfig(DEBUG_USARTx_TX_GPIO_PORT, DEBUG_USARTx_TX_SOURCE, DEBUG_USARTx_TX_AF); GPIO_PinAFConfig(DEBUG_USARTx_RX_GPIO_PORT, DEBUG_USARTx_RX_SOURCE, DEBUG_USARTx_RX_AF); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = DEBUG_USARTx_TX_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(DEBUG_USARTx_TX_GPIO_PORT, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = DEBUG_USARTx_RX_PIN; GPIO_Init(DEBUG_USARTx_RX_GPIO_PORT, &GPIO_InitStructure); USART_DeInit(DEBUG_USARTx); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(DEBUG_USARTx, &USART_InitStructure); USART_Cmd(DEBUG_USARTx, ENABLE); }
int application_start( void ) { OSStatus err = kNoErr; err = mico_rtos_init_mutex(&os_mutex); require_noerr( err, exit ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mutex1", mutex_thread, 0x800, (void *)os_mutex1); require_noerr_action( err, exit, os_mutex_log("ERROR: Unable to start the mutex1 thread.") ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mutex2", mutex_thread, 0x800, (void *)os_mutex2); require_noerr_action( err, exit, os_mutex_log("ERROR: Unable to start the mutex2 thread.") ); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mutex2", mutex_thread, 0x800, (void *)os_mutex3); require_noerr_action( err, exit, os_mutex_log("ERROR: Unable to start the mutex2 thread.") ); return err; exit: os_mutex_log("ERROR, err: %d", err); return err; }
OSStatus MicoSpiInitialize( const mico_spi_device_t* spi ) { platform_spi_config_t config; OSStatus err = kNoErr; if ( spi->port >= MICO_SPI_NONE ) return kUnsupportedErr; if( platform_spi_drivers[spi->port].spi_mutex == NULL) mico_rtos_init_mutex( &platform_spi_drivers[spi->port].spi_mutex ); config.chip_select = &platform_gpio_pins[spi->chip_select]; config.speed = spi->speed; config.mode = spi->mode; config.bits = spi->bits; mico_rtos_lock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); err = platform_spi_init( &platform_spi_drivers[spi->port], &platform_spi_peripherals[spi->port], &config ); mico_rtos_unlock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); return err; }
OSStatus MicoI2cInitialize( mico_i2c_device_t* device ) { platform_i2c_config_t config; OSStatus result; if ( device->port >= MICO_I2C_NONE ) return kUnsupportedErr; config.address = device->address; config.address_width = device->address_width; config.flags &= ~I2C_DEVICE_USE_DMA ; config.speed_mode = device->speed_mode; if( platform_i2c_drivers[device->port].i2c_mutex == NULL) mico_rtos_init_mutex( &platform_i2c_drivers[device->port].i2c_mutex ); mico_rtos_lock_mutex( &platform_i2c_drivers[device->port].i2c_mutex ); result = (OSStatus) platform_i2c_init( &platform_i2c_peripherals[device->port], &config ); mico_rtos_unlock_mutex( &platform_i2c_drivers[device->port].i2c_mutex ); return result; }
OSStatus startEasyLink( mico_Context_t * const inContext) { easylink_log_trace(); OSStatus err = kUnknownErr; require_action(inContext->micoStatus.easylink_thread_handler==NULL, exit, err = kParamErr); require_action(inContext->micoStatus.easylink_sem==NULL, exit, err = kParamErr); inContext->micoStatus.easylinkClient_fd = -1; //ps_enable(); mico_mcu_powersave_config(true); mico_rtos_init_mutex(&_mutex); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)EasyLinkNotify_DHCPCompleteHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); /*Led trigger*/ mico_init_timer(&_Led_EL_timer, LED_EL_TRIGGER_INTERVAL, _led_EL_Timeout_handler, NULL); mico_start_timer(&_Led_EL_timer); // Start the EasyLink thread ConfigWillStart(inContext); mico_rtos_init_semaphore(&inContext->micoStatus.easylink_sem, 1); err = mico_rtos_create_thread(&inContext->micoStatus.easylink_thread_handler, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x1000, (void*)inContext ); require_noerr_action( err, exit, easylink_log("ERROR: Unable to start the EasyLink thread.") ); exit: return err; }
static OSStatus MicoFlashInitialize( mico_partition_t partition ) { OSStatus err = kNoErr; require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr ); require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr ); require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr ); if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex == NULL){ err = mico_rtos_init_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); require_noerr( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); err = platform_flash_init( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ] ); platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].peripheral = (platform_flash_t *)&platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ]; platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized = true; mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); exit: return err; }
OSStatus MicoSpiFinalize( const mico_spi_device_t* spi ) { OSStatus err = kNoErr; if ( spi->port >= MICO_SPI_NONE ) return kUnsupportedErr; #ifdef MICO_WIFI_SHARE_SPI_BUS if( platform_spi_peripherals[spi->port].port == wifi_spi.port ) { return platform_wlan_spi_deinit( &platform_gpio_pins[spi->chip_select] ); } #endif if( platform_spi_drivers[spi->port].spi_mutex == NULL) mico_rtos_init_mutex( &platform_spi_drivers[spi->port].spi_mutex ); mico_rtos_lock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); err = platform_spi_deinit( &platform_spi_drivers[spi->port] ); mico_rtos_unlock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); return err; }
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer ) { OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); driver->rx_size = 0; driver->tx_size = 0; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; driver->peripheral = (platform_uart_t*)peripheral; driver->buart_fifo_head = 0; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore( &driver->tx_complete, 1 ); mico_rtos_init_semaphore( &driver->rx_complete, 1 ); mico_rtos_init_mutex ( &driver->tx_mutex ); #else driver->tx_complete = false; driver->rx_complete = false; #endif if ( peripheral->uart == FUART ){ if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 1 ) GpioFuartTxIoConfig(0); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 7 ) GpioFuartTxIoConfig(1); else if( peripheral->pin_tx->port == GPIOC && peripheral->pin_tx->pin == 3 ) GpioFuartTxIoConfig(2); else return kUnsupportedErr; if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 1 ) GpioFuartRxIoConfig(0); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 6 ) GpioFuartRxIoConfig(1); else if( peripheral->pin_rx->port == GPIOC && peripheral->pin_rx->pin == 4 ) GpioFuartRxIoConfig(2); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = FuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); FuartIOctl(UART_IOCTL_RXINT_SET, 1); if (optional_ring_buffer != NULL){ /* Note that the ring_buffer should've been initialised first */ driver->rx_buffer = optional_ring_buffer; driver->rx_size = 0; //platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } }else if( peripheral->uart == BUART ){ if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 16 ) GpioBuartTxIoConfig(0); else if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 25 ) GpioBuartTxIoConfig(1); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 9 ) GpioBuartTxIoConfig(2); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 28 ) GpioBuartTxIoConfig(3); else return kUnsupportedErr; if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 13 ) GpioBuartRxIoConfig(0); else if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 24 ) GpioBuartRxIoConfig(1); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 8 ) GpioBuartRxIoConfig(2); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 29 ) GpioBuartRxIoConfig(3); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = BuartExFifoInit( BUART_FIFO_START, BUART_RX_FIFO_SIZE, BUART_TX_FIFO_SIZE, 1 ); require_noerr(err, exit); err = BuartInit( config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1 ); require_noerr(err, exit); BuartIOctl( UART_IOCTL_TXINT_SET, 1 ); }else return kUnsupportedErr; exit: return err; }
/**************************************************************************** * Function : eZCB_AddNode * Description : Add Node * Input Para : * Output Para : * Return Value: ****************************************************************************/ teZcbStatus eZCB_AddNode(uint16_t u16ShortAddress, uint64_t u64IEEEAddress, uint16_t u16DeviceID, uint8_t u8MacCapability, tsZCB_Node **ppsZCBNode) { teZcbStatus eStatus = E_ZCB_OK; tsZCB_Node *psZCBNode = &sZCB_Network.sNodes; user_ZBNetwork_log("add node"); mico_rtos_lock_mutex(&sZCB_Network.sLock); while (psZCBNode->psNext) { if (u64IEEEAddress) { if (psZCBNode->psNext->u64IEEEAddress == u64IEEEAddress) { user_ZBNetwork_log("IEEE address already in network - update short address"); mico_rtos_lock_mutex(&psZCBNode->psNext->sLock); psZCBNode->psNext->u16ShortAddress = u16ShortAddress; if (ppsZCBNode) { *ppsZCBNode = psZCBNode->psNext; } else { mico_rtos_unlock_mutex(&psZCBNode->psNext->sLock); } goto done; } if (psZCBNode->psNext->u16ShortAddress == u16ShortAddress) { user_ZBNetwork_log("Short address already in network - update IEEE address"); mico_rtos_lock_mutex(&psZCBNode->psNext->sLock); psZCBNode->psNext->u64IEEEAddress = u64IEEEAddress; if (ppsZCBNode) { *ppsZCBNode = psZCBNode->psNext; } else { mico_rtos_unlock_mutex(&psZCBNode->psNext->sLock); } goto done; } } else { if (psZCBNode->psNext->u16ShortAddress == u16ShortAddress) { user_ZBNetwork_log("Short address already in network"); mico_rtos_lock_mutex(&psZCBNode->psNext->sLock); if (ppsZCBNode) { *ppsZCBNode = psZCBNode->psNext; } else { mico_rtos_unlock_mutex(&psZCBNode->psNext->sLock); } goto done; } } psZCBNode = psZCBNode->psNext; } psZCBNode->psNext = malloc(sizeof(tsZCB_Node)); if (!psZCBNode->psNext) { user_ZBNetwork_log("Memory allocation failure allocating node"); eStatus = E_ZCB_ERROR_NO_MEM; goto done; } memset(psZCBNode->psNext, 0, sizeof(tsZCB_Node)); /* Got to end of list without finding existing node - add it at the end of the list */ mico_rtos_init_mutex(&psZCBNode->psNext->sLock); psZCBNode->psNext->u16ShortAddress = u16ShortAddress; psZCBNode->psNext->u64IEEEAddress = u64IEEEAddress; psZCBNode->psNext->u8MacCapability = u8MacCapability; psZCBNode->psNext->u16DeviceID = u16DeviceID; user_ZBNetwork_log("Created new Node"); DBG_PrintNode(psZCBNode->psNext); if (ppsZCBNode) { mico_rtos_lock_mutex(&psZCBNode->psNext->sLock); *ppsZCBNode = psZCBNode->psNext; } done: mico_rtos_unlock_mutex(&sZCB_Network.sLock); return eStatus; }
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer ) { pdc_packet_t pdc_uart_packet, pdc_uart_tx_packet; OSStatus err = kNoErr; sam_usart_opt_t settings; bool hardware_shaking = false; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); require_action_quiet( (optional_ring_buffer->buffer != NULL ) && (optional_ring_buffer->size != 0), exit, err = kUnsupportedErr); driver->rx_size = 0; driver->tx_size = 0; driver->rx_ring_buffer = optional_ring_buffer; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; driver->peripheral = (platform_uart_t*)peripheral; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore( &driver->tx_complete, 1 ); mico_rtos_init_semaphore( &driver->rx_complete, 1 ); mico_rtos_init_semaphore( &driver->sem_wakeup, 1 ); mico_rtos_init_mutex ( &driver->tx_mutex ); #else driver->tx_complete = false; driver->rx_complete = false; #endif /* Set Tx and Rx pin mode to UART peripheral */ platform_gpio_peripheral_pin_init( peripheral->tx_pin, ( peripheral->tx_pin_mux_mode | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( peripheral->rx_pin, ( peripheral->rx_pin_mux_mode | IOPORT_MODE_PULLUP ) ); /* Init CTS and RTS pins (if available) */ if ( peripheral->cts_pin != NULL && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { hardware_shaking = true; platform_gpio_peripheral_pin_init( peripheral->cts_pin, ( peripheral->cts_pin_mux_mode | IOPORT_MODE_PULLUP ) ); } if ( peripheral->rts_pin != NULL && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { hardware_shaking = true; platform_gpio_peripheral_pin_init( peripheral->rts_pin, ( peripheral->rts_pin_mux_mode | IOPORT_MODE_PULLUP ) ); } /* Enable the clock. */ if( pmc_is_periph_clk_enabled( peripheral->peripheral_id ) == 0 ) { flexcom_enable( peripheral->flexcom_base ); } flexcom_set_opmode( peripheral->flexcom_base, FLEXCOM_USART ); /* Enable the receiver and transmitter. */ usart_reset_tx( peripheral->port ); usart_reset_rx( peripheral->port ); /* Disable all the interrupts. */ usart_disable_interrupt( peripheral->port, 0xffffffff ); switch ( config->parity ) { case NO_PARITY: settings.parity_type = US_MR_PAR_NO; break; case EVEN_PARITY: settings.parity_type = US_MR_PAR_EVEN; break; case ODD_PARITY: settings.parity_type = US_MR_PAR_ODD; break; default: err = kParamErr; goto exit; } switch ( config->data_width) { case DATA_WIDTH_5BIT: settings.char_length = US_MR_CHRL_5_BIT; break; case DATA_WIDTH_6BIT: settings.char_length = US_MR_CHRL_6_BIT; break; case DATA_WIDTH_7BIT: settings.char_length = US_MR_CHRL_7_BIT; break; case DATA_WIDTH_8BIT: settings.char_length = US_MR_CHRL_8_BIT; break; case DATA_WIDTH_9BIT: settings.char_length = US_MR_MODE9; break; default: err = kParamErr; goto exit; } settings.baudrate = config->baud_rate; settings.stop_bits = ( config->stop_bits == STOP_BITS_1 ) ? US_MR_NBSTOP_1_BIT : US_MR_NBSTOP_2_BIT; settings.channel_mode= US_MR_CHMODE_NORMAL; /* Configure USART in serial mode. */ if (!hardware_shaking) { usart_init_rs232( peripheral->port, &settings, sysclk_get_peripheral_hz()); } else { usart_init_hw_handshaking( peripheral->port, &settings, sysclk_get_peripheral_hz()); } /* Enable uart interrupt */ NVIC_SetPriority( platform_flexcom_irq_numbers[peripheral->uart_id], 0x06 ); NVIC_EnableIRQ( platform_flexcom_irq_numbers[peripheral->uart_id] ); /* Enable PDC transmit */ pdc_enable_transfer( usart_get_pdc_base( peripheral->port ), PERIPH_PTCR_TXTEN | PERIPH_PTCR_RXTEN ); pdc_disable_transfer( usart_get_pdc_base( driver->peripheral->port ), PERIPH_PTCR_TXTDIS ); pdc_uart_packet.ul_addr = (uint32_t)driver->rx_ring_buffer->buffer; pdc_uart_packet.ul_size = (uint32_t)driver->rx_ring_buffer->size; pdc_rx_init( usart_get_pdc_base( peripheral->port ), &pdc_uart_packet, &pdc_uart_packet ); pdc_uart_tx_packet.ul_addr = (uint32_t)0; pdc_uart_tx_packet.ul_size = (uint32_t)1; pdc_tx_init( usart_get_pdc_base( driver->peripheral->port ), &pdc_uart_tx_packet, NULL ); usart_enable_interrupt( peripheral->port, US_IER_ENDRX | US_IER_RXBUFF | US_IER_RXRDY | US_IER_ENDTX ); /* Enable the receiver and transmitter. */ usart_enable_tx( peripheral->port ); usart_enable_rx( peripheral->port ); exit: platform_mcu_powersave_enable( ); return err; }
OSStatus internal_uart_init( mico_uart_t uart, const mico_uart_config_t* config, ring_buffer_t* optional_rx_buffer ) { OSStatus err = kNoErr; require_action(optional_rx_buffer!=NULL, exit, err = kParamErr); #ifndef NO_MICO_RTOS mico_rtos_init_semaphore(&uart_interfaces[uart].tx_complete, 1); mico_rtos_init_semaphore(&uart_interfaces[uart].rx_complete, 1); mico_rtos_init_mutex(&uart_interfaces[uart].tx_mutex); #else uart_interfaces[uart].tx_complete = false; uart_interfaces[uart].rx_complete = false; #endif if(uart_mapping[uart].uart == FUART){ if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 1 ) GpioFuartTxIoConfig(0); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 7 ) GpioFuartTxIoConfig(1); else if( uart_mapping[uart].pin_tx->port == GPIOC && uart_mapping[uart].pin_tx->pin == 3 ) GpioFuartTxIoConfig(2); else return kUnsupportedErr; if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 1 ) GpioFuartRxIoConfig(0); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 6 ) GpioFuartRxIoConfig(1); else if( uart_mapping[uart].pin_rx->port == GPIOC && uart_mapping[uart].pin_rx->pin == 4 ) GpioFuartRxIoConfig(2); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = FuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); FuartIOctl(UART_IOCTL_RXINT_SET, 1); if (optional_rx_buffer != NULL){ /* Note that the ring_buffer should've been initialised first */ uart_interfaces[uart].rx_buffer = optional_rx_buffer; uart_interfaces[uart].rx_size = 0; //platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } }else if(uart_mapping[uart].uart == BUART){ if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 16 ) GpioBuartTxIoConfig(0); else if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 25 ) GpioBuartTxIoConfig(1); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 9 ) GpioBuartTxIoConfig(2); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 28 ) GpioBuartTxIoConfig(3); else return kUnsupportedErr; if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 13 ) GpioBuartRxIoConfig(0); else if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 24 ) GpioBuartRxIoConfig(1); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 8 ) GpioBuartRxIoConfig(2); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 29 ) GpioBuartRxIoConfig(3); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = BuartExFifoInit( (32-2-1)*1024, 1024*2, 1024, 1); require_noerr(err, exit); err = BuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); BuartIOctl(UART_IOCTL_RXINT_SET, 1); BuartIOctl(UART_IOCTL_TXINT_SET, 1); }else return kUnsupportedErr; exit: return err; }
void mico_clib_thread_safe_init(void) { mico_rtos_init_mutex( &systemMutex ); mico_rtos_init_mutex( &fileMutex ); }
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer ) { DMA_InitTypeDef dma_init_structure; USART_InitTypeDef uart_init_structure; uint32_t uart_number; OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); require_action_quiet( (optional_ring_buffer == NULL) || ((optional_ring_buffer->buffer != NULL ) && (optional_ring_buffer->size != 0)), exit, err = kParamErr); uart_number = platform_uart_get_port_number( peripheral->port ); driver->rx_size = 0; driver->tx_size = 0; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; driver->peripheral = (platform_uart_t*)peripheral; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore( &driver->tx_complete, 1 ); mico_rtos_init_semaphore( &driver->rx_complete, 1 ); mico_rtos_init_semaphore( &driver->sem_wakeup, 1 ); mico_rtos_init_mutex ( &driver->tx_mutex ); #else driver->tx_complete = false; driver->rx_complete = false; #endif /* Configure TX and RX pin_mapping */ platform_gpio_set_alternate_function( peripheral->pin_tx->port, peripheral->pin_tx->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] ); platform_gpio_set_alternate_function( peripheral->pin_rx->port, peripheral->pin_rx->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] ); if ( ( peripheral->pin_cts != NULL ) && ( config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS ) ) { platform_gpio_set_alternate_function( peripheral->pin_cts->port, peripheral->pin_cts->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] ); } if ( ( peripheral->pin_rts != NULL ) && ( config->flow_control == FLOW_CONTROL_RTS || config->flow_control == FLOW_CONTROL_CTS_RTS ) ) { platform_gpio_set_alternate_function( peripheral->pin_rts->port, peripheral->pin_rts->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] ); } #ifndef NO_MICO_RTOS if(config->flags & UART_WAKEUP_ENABLE) { mico_rtos_init_semaphore( driver->sem_wakeup, 1 ); mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "UART_WAKEUP", thread_wakeup, 0x100, driver); } #endif /* Enable UART peripheral clock */ uart_peripheral_clock_functions[ uart_number ]( uart_peripheral_clocks[ uart_number ], ENABLE ); uart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; uart_init_structure.USART_BaudRate = config->baud_rate; uart_init_structure.USART_WordLength = ( ( config->data_width == DATA_WIDTH_9BIT ) || ( ( config->data_width == DATA_WIDTH_8BIT ) && ( config->parity != NO_PARITY ) ) ) ? USART_WordLength_9b : USART_WordLength_8b; uart_init_structure.USART_StopBits = ( config->stop_bits == STOP_BITS_1 ) ? USART_StopBits_1 : USART_StopBits_2; switch ( config->parity ) { case NO_PARITY: uart_init_structure.USART_Parity = USART_Parity_No; break; case EVEN_PARITY: uart_init_structure.USART_Parity = USART_Parity_Even; break; case ODD_PARITY: uart_init_structure.USART_Parity = USART_Parity_Odd; break; default: err = kParamErr; goto exit; } switch ( config->flow_control ) { case FLOW_CONTROL_DISABLED: uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; break; case FLOW_CONTROL_CTS: uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; break; case FLOW_CONTROL_RTS: uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS; break; case FLOW_CONTROL_CTS_RTS: uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; break; default: err = kParamErr; goto exit; } /* Initialise USART peripheral */ USART_DeInit( peripheral->port ); USART_Init( peripheral->port, &uart_init_structure ); /************************************************************************** * Initialise STM32 DMA registers * Note: If DMA is used, USART interrupt isn't enabled. **************************************************************************/ /* Enable DMA peripheral clock */ if ( peripheral->tx_dma_config.controller == DMA1 ) { RCC->AHB1ENR |= RCC_AHB1Periph_DMA1; } else { RCC->AHB1ENR |= RCC_AHB1Periph_DMA2; } /* Fill init structure with common DMA settings */ dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single; dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; if ( config->data_width == DATA_WIDTH_9BIT ) { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } /* Initialise TX DMA */ DMA_DeInit( peripheral->tx_dma_config.stream ); dma_init_structure.DMA_Channel = peripheral->tx_dma_config.channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &peripheral->port->DR; dma_init_structure.DMA_Memory0BaseAddr = (uint32_t) 0; dma_init_structure.DMA_DIR = DMA_DIR_MemoryToPeripheral; dma_init_structure.DMA_BufferSize = 0xFFFF; // This parameter will be configured during communication dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( peripheral->tx_dma_config.stream, &dma_init_structure ); /* Initialise RX DMA */ DMA_DeInit( peripheral->rx_dma_config.stream ); dma_init_structure.DMA_Channel = peripheral->rx_dma_config.channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &peripheral->port->DR; dma_init_structure.DMA_Memory0BaseAddr = (uint32_t) 0; dma_init_structure.DMA_DIR = DMA_DIR_PeripheralToMemory; dma_init_structure.DMA_BufferSize = 0xFFFF; // This parameter will be configured during communication dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( peripheral->rx_dma_config.stream, &dma_init_structure ); /************************************************************************** * Initialise STM32 DMA interrupts **************************************************************************/ /* Configure TX DMA interrupt on Cortex-M3 */ NVIC_EnableIRQ( peripheral->tx_dma_config.irq_vector ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ clear_dma_interrupts( peripheral->tx_dma_config.stream, peripheral->tx_dma_config.complete_flags | peripheral->tx_dma_config.error_flags ); DMA_ITConfig( peripheral->tx_dma_config.stream, DMA_INTERRUPT_FLAGS, ENABLE ); /* Enable USART interrupt vector in Cortex-M3 */ NVIC_EnableIRQ( uart_irq_vectors[uart_number] ); USART_DMACmd( driver->peripheral->port, USART_DMAReq_Tx, DISABLE ); /* Enable USART */ USART_Cmd( peripheral->port, ENABLE ); /* Enable both transmit and receive */ peripheral->port->CR1 |= USART_CR1_TE; peripheral->port->CR1 |= USART_CR1_RE; /* Setup ring buffer */ if ( optional_ring_buffer != NULL ) { /* Note that the ring_buffer should've been initialised first */ driver->rx_buffer = optional_ring_buffer; driver->rx_size = 0; receive_bytes( driver, optional_ring_buffer->buffer, optional_ring_buffer->size, 0 ); } else { /* Not using ring buffer. Configure RX DMA interrupt on Cortex-M3 */ NVIC_EnableIRQ( peripheral->rx_dma_config.irq_vector ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ clear_dma_interrupts( peripheral->rx_dma_config.stream, peripheral->rx_dma_config.complete_flags | peripheral->rx_dma_config.error_flags ); DMA_ITConfig( peripheral->rx_dma_config.stream, DMA_INTERRUPT_FLAGS, ENABLE ); } exit: MicoMcuPowerSaveConfig(true); return err; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC err = startMfiWac( context ); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ err = MICOStartBonjourService( Station, context ); require_noerr( err, exit ); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(100); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
teZcbStatus ePDM_Init(mico_Context_t* mico_context) { OSStatus err; user_zigbeePDM_log("Create PDM lock"); mico_rtos_init_mutex(&sLock); mico_logic_partition_t *zigbeePDM_partition_info; //mico_logic_partition_t *p1_info; uint8_t read_test[100]= {0}; uint8_t i = 0; uint32_t dest_offset = 0; uint8_t data_write[6]= {0x06,0x05,0x04,0x03,0x02,0x01}; mico_rtos_lock_mutex(&sLock); #if 0 //init MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashInitialize(MICO_PARTITION_ZIGBEEPDM_TEMP); require_noerr(err, exit); // Get Info MICO_PARTITION_ZIGBEEPDM_TEMP zigbeePDM_partition_info = MicoFlashGetInfo(MICO_PARTITION_ZIGBEEPDM_TEMP); user_zigbeePDM_log("ZigBee PDM Partition info:start_addr:%x ,length:%x",zigbeePDM_partition_info->partition_start_addr,zigbeePDM_partition_info->partition_length); //Erase MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashErase( MICO_PARTITION_ZIGBEEPDM_TEMP, 0x0, zigbeePDM_partition_info->partition_length); require_noerr(err, exit); mico_thread_msleep(100); //sleep //MicoFlashWrite(mico_partition_t partition, volatile uint32_t * off_set, uint8_t * inBuffer, uint32_t inBufferLength); //Write MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashWrite(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, (uint8_t *)data_write, sizeof(data_write)); require_noerr(err, exit); #endif #if 0 mico_context -> user_config_data = (void*)data_write; mico_context -> user_config_data_size = 10; err = mico_system_context_update(mico_context); require_noerr(err, exit); mico_thread_msleep(1000); #endif #if 0 //Read dest_offset = 0; err = MicoFlashRead(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, read_test, 5); require_noerr(err, exit); #endif #if 0 err = MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, 60); require_noerr(err, exit); mico_thread_msleep(10); err = MicoFlashWrite( MICO_PARTITION_PARAMETER_1, &dest_offset, "aaaaa", 5); require_noerr(err, exit); p1_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_1); err = MicoFlashRead(MICO_PARTITION_PARAMETER_1, &dest_offset, read_test, 60); require_noerr(err, exit); #endif #if 0 //Output for(i = 0; i<5; i++) { printf("0x%x ",read_test[i]); } printf("\r\n"); #endif //MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &context->offset, (uint8_t *)inData, inLen); //MicoFlashRead(MICO_PARTITION_OTA_TEMP, &flashaddr, (uint8_t *)md5_recv, 16); //err = MicoFlashDisableSecurity( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition_info->partition_length ); //if (sqlite3_open_v2(pcPDMFile, &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) != SQLITE_OK) //{ // daemon_log(LOG_ERR, "Error initialising PDM database (%s)", sqlite3_errmsg(pDb)); // return E_ZCB_ERROR; //} //user_zigbeePDM_log("PDM Database opened\n"); { //const char *pcTableDef = "CREATE TABLE IF NOT EXISTS pdm (id INTEGER, size INTEGER, numblocks INTEGER, block INTEGER, blocksize INTEGER, data BLOB, PRIMARY KEY (id,block))"; //char *pcErr; //user_zigbeePDM_log("Execute SQL: '%s'\n", pcTableDef); //if (sqlite3_exec(pDb, pcTableDef, NULL, NULL, &pcErr) != SQLITE_OK) //{ // mico_log("Error creating table (%s)", pcErr); //sqlite3_free(pcErr); //mico_rtos_unlock_mutex(&sLock); //return E_ZCB_ERROR; //} } //user_zigbeePDM_log("PDM Database initialised\n"); //eSL_AddListener(E_SL_MSG_PDM_AVAILABLE_REQUEST, PDM_HandleAvailableRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_LOAD_RECORD_REQUEST, PDM_HandleLoadRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_SAVE_RECORD_REQUEST, PDM_HandleSaveRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_DELETE_ALL_RECORDS_REQUEST,PDM_HandleDeleteAllRequest, NULL); mico_rtos_unlock_mutex(&sLock); return E_ZCB_OK; exit: mico_rtos_unlock_mutex(&sLock); return err; }
OSStatus bt_smartbridge_att_cache_enable( uint32_t cache_count, mico_bt_uuid_t cache_services[], uint32_t service_count ) { uint32_t a; OSStatus result; bt_smartbridge_att_cache_manager_t* manager; mico_bt_uuid_t* services = NULL; if ( att_cache_manager != NULL ) { return MICO_BT_SUCCESS; } manager = (bt_smartbridge_att_cache_manager_t*)malloc_named( "att_cache", CALCULATE_ATT_CACHE_MANAGER_SIZE( cache_count ) ); if ( manager == NULL ) { return MICO_BT_OUT_OF_HEAP_SPACE; } if( service_count != 0 ) { services = (mico_bt_uuid_t *)malloc_named( "cache_services", service_count * sizeof(mico_bt_uuid_t) ); if ( services == NULL ) { return MICO_BT_OUT_OF_HEAP_SPACE; } } memset( manager, 0, CALCULATE_ATT_CACHE_MANAGER_SIZE( cache_count ) ); att_cache_manager = manager; manager->count = cache_count; manager->att_cache_services_count = service_count; if( service_count != 0 ) { manager->att_cache_services = services; memcpy( manager->att_cache_services, cache_services, service_count * sizeof(mico_bt_uuid_t) ); } result = linked_list_init( &manager->free_list ); if ( result != MICO_BT_SUCCESS ) { bt_smartbridge_log( "Error creating linked list\n" ); goto error; } result = linked_list_init( &manager->used_list ); if ( result != MICO_BT_SUCCESS ) { bt_smartbridge_log( "Error creating linked list\n" ); goto error; } result = mico_rtos_init_mutex( &manager->mutex ); if ( result != MICO_BT_SUCCESS ) { bt_smartbridge_log( "Error creating mutex\n" ); goto error; } /* Initialise mutexes for protecting access to cached attributes */ for ( a = 0; a < manager->count; a++ ) { result = mico_rtos_init_mutex( &manager->pool[a].mutex ); if ( result != MICO_BT_SUCCESS ) { goto error; } /* Point node data to cached attribute instance */ manager->pool[a].node.data = (void*)&manager->pool[a]; /* Insert cached attribute instance into free list */ result = linked_list_insert_node_at_rear( &manager->free_list, &manager->pool[a].node ); if ( result != MICO_BT_SUCCESS ) { goto error; } } return MICO_BT_SUCCESS; error: bt_smartbridge_att_cache_disable(); return result; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols;; err = startMFiWAC( context, WAC_Params, 1200); free(WAC_Params); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ // if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ // err = MICOStartBonjourService( Station, context ); // require_noerr( err, exit ); // } err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mDNSResponder", mDNSResponder_thread, 0x1000, (void*)context ); require_noerr_action( err, exit, mico_log("ERROR: Unable to start mDNSResponder thread.") ); /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start(void) { OSStatus err = kNoErr; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64]; mico_log_trace(); #if 1 /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); #endif /*wlan driver and tcpip init*/ MicoInit(); MicoSysLed(true); /**********************add ethernet**********************/ add_ethernet(); //sleep(5000); /*********************************************************/ mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /* Enter test mode, call a build-in test function amd output on STDIO */ if(MicoShouldEnterMFGMode()==true) mico_mfg_test(); /*Read current time from RTC.*/ MicoRtcGetTime(&time); currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000 - 100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); /*************add ethernet********Why add twice?***********/ //add_ethernet(); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == CONFIG_MODE_WAC) WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols; err = startMFiWAC( context, WAC_Params, 1200); free(WAC_Params); require_noerr( err, exit ); #else #error "Wi-Fi configuration mode is not defined"? #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); MicoSystemReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); micoWlanPowerOff(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); micoWlanPowerOff(); MicoSystemStandBy(MICO_WAIT_FOREVER); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start(void) { OSStatus err = kNoErr; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64] = {0}; mico_log_trace(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex);//ram互斥初始化 mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1);//系统状态信号量 mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "sys", _sys_state_thread, 800, NULL ); MICOReadConfiguration( context );//读flash数据 err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mico_log( "MiCO starting..." ); MicoInit(); #ifdef MICO_CLI_ENABLE MicoCliInit(); #endif MicoSysLed(true); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)para.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("ip = %s,mac=%s",para.ip,para.mac); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*Start system monotor thread*/ //err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000/2, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); /* Enter test mode, call a build-in test function amd output on MFG UART */ if(MicoShouldEnterMFGMode()==true){ mico_log( "Enter MFG mode by MFG button" ); mico_mfg_test(context); } /*Read current time from RTC.*/ if( MicoRtcGetTime(&time) == kNoErr ){ currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); }else mico_log("RTC function unsupported"); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); if( context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured || context->flashContentInRam.micoSystemConfig.configured == unConfigured){ mico_log("Empty configuration. Starting configuration mode..."); //HERE TO config network #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == CONFIG_MODE_WAC) WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols; err = startMFiWAC( context, WAC_Params, MICO_I2C_CP, 1200 ); free(WAC_Params); require_noerr( err, exit ); #else #error "Wi-Fi configuration mode is not defined" #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); _ConnectToAP( context ); } #ifdef MFG_MODE_AUTO if( context->flashContentInRam.micoSystemConfig.configured == mfgConfigured ){ mico_log( "Enter MFG mode automatically" ); mico_mfg_test(context); mico_thread_sleep(MICO_NEVER_TIMEOUT); } #endif err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
void fogcloud_main_thread(void *arg) { OSStatus err = kUnknownErr; mico_Context_t *inContext = (mico_Context_t *)arg; MVDResetRequestData_t devResetRequestData; #ifdef ENABLE_FOGCLOUD_AUTO_ACTIVATE MVDActivateRequestData_t devDefaultActivateData; #endif /* wait for station on */ while(!inContext->appStatus.isWifiConnected){ mico_thread_msleep(500); } //--- create msg recv queue, NOTE: just push msg pionter into queue, so msg memory must be freed after used. if(NULL == msg_recv_queue_mutex){ err = mico_rtos_init_mutex(&msg_recv_queue_mutex); require_noerr_action(err, exit, fogcloud_log("ERROR: mico_rtos_init_mutex (msg_recv_queue_mutex) failed, err=%d.", err)); } err = mico_rtos_init_queue(&msg_recv_queue, "fog_recv_queue", sizeof(int), FOGCLOUD_MAX_RECV_QUEUE_LENGTH); require_noerr_action(err, exit, fogcloud_log("ERROR: mico_rtos_init_queue (msg_recv_queue) failed, err=%d", err)); /* start FogCloud service */ err = fogCloudStart(inContext); require_noerr_action(err, exit, fogcloud_log("ERROR: MicoFogCloudCloudInterfaceStart failed!") ); /* start configServer for fogcloud (server for activate/authorize/reset/ota cmd from user APP) */ if(false == inContext->flashContentInRam.appConfig.fogcloudConfig.owner_binding){ err = MicoStartFogCloudConfigServer( inContext); require_noerr_action(err, exit, fogcloud_log("ERROR: start FogCloud configServer failed!") ); } #ifdef ENABLE_FOGCLOUD_AUTO_ACTIVATE /* activate when wifi on */ while(false == inContext->flashContentInRam.appConfig.fogcloudConfig.isActivated){ // auto activate, using default login_id/dev_pass/user_token fogcloud_log("device activate start..."); memset((void*)&devDefaultActivateData, 0, sizeof(devDefaultActivateData)); strncpy(devDefaultActivateData.loginId, inContext->flashContentInRam.appConfig.fogcloudConfig.loginId, MAX_SIZE_LOGIN_ID); strncpy(devDefaultActivateData.devPasswd, inContext->flashContentInRam.appConfig.fogcloudConfig.devPasswd, MAX_SIZE_DEV_PASSWD); strncpy(devDefaultActivateData.user_token, inContext->micoStatus.mac, // use MAC as default user_token MAX_SIZE_USER_TOKEN); err = fogCloudDevActivate(inContext, devDefaultActivateData); if(kNoErr == err){ fogcloud_log("device activate success!"); break; } else{ fogcloud_log("device auto activate failed, err = %d, will retry in 3s ...", err); } mico_thread_sleep(3); } #endif // ENABLE_FOGCLOUD_AUTO_ACTIVATE #ifndef DISABLE_FOGCLOUD_OTA_CHECK /* OTA check just device activated */ if( (!inContext->appStatus.noOTACheckOnSystemStart) && (inContext->flashContentInRam.appConfig.fogcloudConfig.isActivated) ){ // start ota thread err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "fogcloud_ota", fogcloud_ota_thread, STACK_SIZE_FOGCLOUD_OTA_THREAD, inContext); if(kNoErr != err){ fogcloud_log("ERROR: start FogCloud OTA thread failed, err=%d.", err); } } inContext->appStatus.noOTACheckOnSystemStart = false; #endif // DISABLE_FOGCLOUD_OTA_CHECK while(1){ // device info reset if(device_need_delete){ fogcloud_log("delete device from cloud ..."); memset((void*)&devResetRequestData, 0, sizeof(devResetRequestData)); strncpy(devResetRequestData.loginId, inContext->flashContentInRam.appConfig.fogcloudConfig.loginId, MAX_SIZE_LOGIN_ID); strncpy(devResetRequestData.devPasswd, inContext->flashContentInRam.appConfig.fogcloudConfig.devPasswd, MAX_SIZE_DEV_PASSWD); strncpy(devResetRequestData.user_token, inContext->flashContentInRam.appConfig.fogcloudConfig.userToken, MAX_SIZE_USER_TOKEN); err = fogCloudResetCloudDevInfo(inContext, devResetRequestData); if(kNoErr == err){ device_need_delete = false; fogcloud_log("delete device success, system need reboot..."); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); MicoFogCloudRestoreDefault(inContext); MICOUpdateConfiguration(inContext); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); // system restart inContext->micoStatus.sys_state = eState_Software_Reset; if(inContext->micoStatus.sys_state_change_sem){ mico_rtos_set_semaphore(&inContext->micoStatus.sys_state_change_sem); } } else{ fogcloud_log("delete device failed, err = %d.", err); } } mico_thread_sleep(1); if(inContext->appStatus.fogcloudStatus.isOTAInProgress){ continue; // ota is in progress, the oled && system led will be holding } if(inContext->appStatus.fogcloudStatus.isCloudConnected){ set_RF_LED_cloud_connected(inContext); // toggle LED } else{ set_RF_LED_cloud_disconnected(inContext); // stop LED blink } } exit: fogcloud_log("fogcloud_main_thread exit err=%d.", err); if(NULL != msg_recv_queue_mutex){ mico_rtos_deinit_mutex(&msg_recv_queue_mutex); } if(NULL != msg_recv_queue){ mico_rtos_deinit_queue(&msg_recv_queue); } mico_rtos_delete_thread(NULL); return; }