bool KMutexCreate( KMutex* pMutex, const char* pMutexName ) { bool retval = false; if( pMutex ) { pMutex->hSem = xSemaphoreCreateMutexStatic( &pMutex->sem ); if( pMutexName ){ strncpy_s( pMutex->mutexName, sizeof( pMutex->mutexName ), pMutexName, sizeof( pMutex->mutexName ) - 1 ); } retval = ( pMutex->hSem ) ? true : false; } return retval; }
status_t I2C_RTOS_Init(i2c_rtos_handle_t *handle, I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz) { if (handle == NULL) { return kStatus_InvalidArgument; } if (base == NULL) { return kStatus_InvalidArgument; } memset(handle, 0, sizeof(i2c_rtos_handle_t)); #if (configSUPPORT_STATIC_ALLOCATION == 1) handle->mutex = xSemaphoreCreateMutexStatic(&handle->mutexBuffer); #else handle->mutex = xSemaphoreCreateMutex(); #endif if (handle->mutex == NULL) { return kStatus_Fail; } #if (configSUPPORT_STATIC_ALLOCATION == 1) handle->semaphore = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer); #else handle->semaphore = xSemaphoreCreateBinary(); #endif if (handle->semaphore == NULL) { vSemaphoreDelete(handle->mutex); return kStatus_Fail; } handle->base = base; I2C_MasterInit(handle->base, masterConfig, srcClock_Hz); I2C_MasterTransferCreateHandle(base, &handle->drv_handle, I2C_RTOS_Callback, (void *)handle); return kStatus_Success; }
static void prvCreateAndDeleteStaticallyAllocatedMutexes( void ) { SemaphoreHandle_t xSemaphore; BaseType_t xReturned; /* StaticSemaphore_t is a publicly accessible structure that has the same size and alignment requirements as the real semaphore structure. It is provided as a mechanism for applications to know the size of the semaphore (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real semaphore internals. This StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic() function calls within this function. */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more parameter than the usual xSemaphoreCreateMutex() function. The parameter is a pointer to the pre-allocated StaticSemaphore_t structure, which will hold information on the semaphore in an anonymous way. If the pointer is passed as NULL then the structure will be allocated dynamically, just as when xSemaphoreCreateMutex() is called. */ xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Now do the same using a dynamically allocated mutex to ensure the delete function is working correctly in both the static and dynamic allocation cases. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { xSemaphore = xSemaphoreCreateMutex(); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore != NULL ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); } #endif }
static void prvCreateAndDeleteStaticallyAllocatedMutexes( void ) { SemaphoreHandle_t xSemaphore; BaseType_t xReturned; /* StaticSemaphore_t is a publicly accessible structure that has the same size and alignment requirements as the real semaphore structure. It is provided as a mechanism for applications to know the size of the semaphore (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real semaphore internals. This StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic() function calls within this function. */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more parameter than the usual xSemaphoreCreateMutex() function. The paraemter is a pointer to the pre-allocated StaticSemaphore_t structure, which will hold information on the semaphore in an anonymous way. If the pointer is passed as NULL then the structure will be allocated dynamically, just as when xSemaphoreCreateMutex() is called. */ xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* The semaphore created above had a statically allocated semaphore structure. Repeat the above using NULL as the xSemaphoreCreateMutexStatic() parameter so the semaphore structure is instead allocated dynamically. */ xSemaphore = xSemaphoreCreateMutexStatic( NULL ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Ensure lower priority tasks get CPU time. */ vTaskDelay( prvGetNextDelayTime() ); /* Just to show the check task that this task is still executing. */ uxCycleCounter++; }
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer); }
/** * Only task configured to start at boot; starts the rest of RTOS. * We use this task to do most of the startup because it allows * use of various RTOS functionality (which can cause issues when used * before the scheduler is running). * WARNING: The vApplicationDaemonTaskStartupHook did not work for us. */ void startup_task(void* pvParameters) { print("EQUiSatOS starting... "); #ifdef WRITE_DEFAULT_MRAM_VALS // utility function to write initial state to MRAM (ONCE before launch) write_custom_state(); #endif /************************************************************************/ /* DATA INITIALIZATION */ /************************************************************************/ // Initialize misc. state mutexes hardware_state_mutex = xSemaphoreCreateMutexStatic(&_hardware_state_mutex_d); critical_action_mutex = xSemaphoreCreateMutexStatic(&_critical_action_mutex_d); // Initialize EQUiStack mutexes _idle_equistack_mutex = xSemaphoreCreateMutexStatic(&_idle_equistack_mutex_d); _attitude_equistack_mutex = xSemaphoreCreateMutexStatic(&_attitude_equistack_mutex_d); _flash_equistack_mutex = xSemaphoreCreateMutexStatic(&_flash_equistack_mutex_d); _flash_cmp_equistack_mutex = xSemaphoreCreateMutexStatic(&_flash_cmp_equistack_mutex_d); _low_power_equistack_mutex = xSemaphoreCreateMutexStatic(&_low_power_equistack_mutex_d); // Initialize EQUiStacks equistack_Init(&idle_readings_equistack, &_idle_equistack_arr, sizeof(idle_data_t), IDLE_STACK_MAX, _idle_equistack_mutex); equistack_Init(&attitude_readings_equistack, &_attitude_equistack_arr, sizeof(attitude_data_t), ATTITUDE_STACK_MAX, _attitude_equistack_mutex); equistack_Init(&flash_readings_equistack, &_flash_equistack_arr, sizeof(flash_data_t), FLASH_STACK_MAX, _flash_equistack_mutex); equistack_Init(&flash_cmp_readings_equistack, &_flash_cmp_equistack_arr, sizeof(flash_cmp_data_t), FLASH_CMP_STACK_MAX, _flash_cmp_equistack_mutex); equistack_Init(&low_power_readings_equistack, &_low_power_equistack_arr, sizeof(low_power_data_t), LOW_POWER_STACK_MAX, _low_power_equistack_mutex); /************************************************************************/ /* ESSENTIAL INITIALIZATION */ /************************************************************************/ // read state from MRAM for first time // this initializes the error equistacks, so make sure no errors // are logged before this (they shouldn't be before RTOS is started...) configure_state_from_reboot(); // function in global to init things that use RTOS // (MUST be done after config state from reboot) global_init_post_rtos(); // populate task_handles array and setup constants pre_init_rtos_tasks(); #if configUSE_TRACE_FACILITY == 1 // name mutexes vTraceSetMutexName(critical_action_mutex, "CA"); vTraceSetMutexName(i2c_irpow_mutex, "I2C"); vTraceSetMutexName(irpow_semaphore, "IRPOW"); vTraceSetMutexName(processor_adc_mutex, "ADC"); vTraceSetMutexName(hardware_state_mutex, "HWST"); vTraceSetMutexName(watchdog_mutex, "WD"); vTraceSetMutexName(mram_spi_cache_mutex, "SPI"); vTraceSetMutexName(_idle_equistack_mutex, "eqIDLE"); vTraceSetMutexName(_attitude_equistack_mutex, "eqATT"); vTraceSetMutexName(_flash_equistack_mutex , "eqF_BST"); vTraceSetMutexName(_flash_cmp_equistack_mutex, "eqF_CMP"); vTraceSetMutexName(_low_power_equistack_mutex, "eqLP"); vTraceSetMutexName(_error_equistack_mutex, "eqERR"); #endif // note RTOS is ready for watchdog callback, and log error if it already tripped rtos_ready = true; if (got_early_warning_callback_in_boot) { log_error(ELOC_WATCHDOG, ECODE_WATCHDOG_EARLY_WARNING, true); } /************************************************************************/ /* TASK CREATION */ /************************************************************************/ // NOTE: We can't actually set task state before they are started below, // and tasks start as active (resumed), so we have the tasks themselves // call the function below which will either suspend them or leave them // running as defined by the configuration of initial state. // suspend the scheduler while adding tasks so their task handles // are non-null when they start executing (i.e. they can be controlled) pet_watchdog(); // in case this takes a bit and we're close vTaskSuspendAll(); /************************************************************************/ /* TESTING/MISC */ /************************************************************************/ create_testing_tasks(); /************************************************************************/ /* END TESTING */ /************************************************************************/ #ifndef ONLY_RUN_TESTING_TASKS battery_charging_task_handle = xTaskCreateStatic(battery_charging_task, "battery charging action task", TASK_BATTERY_CHARGING_STACK_SIZE, NULL, TASK_BATTERY_CHARGING_PRIORITY, battery_charging_task_stack, &battery_charging_task_buffer); antenna_deploy_task_handle = xTaskCreateStatic(antenna_deploy_task, "antenna deploy action task", TASK_ANTENNA_DEPLOY_STACK_SIZE, NULL, TASK_ANTENNA_DEPLOY_PRIORITY, antenna_deploy_task_stack, &antenna_deploy_task_buffer); state_handling_task_handle = xTaskCreateStatic(state_handling_task, "state handling action task", TASK_STATE_HANDLING_STACK_SIZE, NULL, TASK_STATE_HANDLING_PRIORITY, state_handling_task_stack, &state_handling_task_buffer); watchdog_task_handle = xTaskCreateStatic(watchdog_task, "watchdog task", TASK_WATCHDOG_STACK_SIZE, NULL, TASK_WATCHDOG_PRIORITY, watchdog_task_stack, &watchdog_task_buffer); persistent_data_backup_task_handle = xTaskCreateStatic(persistent_data_backup_task, "persistent data backup task", TASK_PERSISTENT_DATA_BACKUP_STACK_SIZE, NULL, TASK_PERSISTENT_DATA_BACKUP_PRIORITY, persistent_data_backup_task_stack, &persistent_data_backup_task_buffer); flash_activate_task_handle = xTaskCreateStatic(flash_activate_task, "flash action task", TASK_FLASH_ACTIVATE_STACK_SIZE, NULL, TASK_FLASH_ACTIVATE_PRIORITY, flash_activate_task_stack, &flash_activate_task_buffer); transmit_task_handle = xTaskCreateStatic(transmit_task, "transmit action task", TASK_TRANSMIT_STACK_SIZE, NULL, TASK_TRANSMIT_PRIORITY, transmit_task_stack, &transmit_task_buffer); /* Data tasks */ idle_data_task_handle = xTaskCreateStatic(idle_data_task, "idle data reader task", TASK_IDLE_DATA_RD_STACK_SIZE, NULL, TASK_IDLE_DATA_RD_PRIORITY, idle_data_task_stack, &idle_data_task_buffer); attitude_data_task_handle = xTaskCreateStatic(attitude_data_task, "attitude data reader task", TASK_ATTITUDE_DATA_RD_STACK_SIZE, NULL, TASK_ATTITUDE_DATA_DATA_RD_PRIORITY, attitude_data_task_stack, &attitude_data_task_buffer); low_power_data_task_handle = xTaskCreateStatic(low_power_data_task, "low power data reader task", TASK_LOW_POWER_DATA_RD_STACK_SIZE, NULL, TASK_LOW_POWER_DATA_RD_PRIORITY, low_power_data_task_stack, &low_power_data_task_buffer); #endif xTaskResumeAll(); setTXEnable(false); usart_send_string((uint8_t*) "EQUiSatOS started"); setTXEnable(true); print("initialization complete.\n"); // delete this task after it's done booting vTaskDelete(NULL); }