#ifndef STA_RTOS_THREAD_HPP #define STA_RTOS_THREAD_HPP #include #include /** * @defgroup STA_RTOS_THREAD Thread * @ingroup STA_RTOS_API * @brief RTOS Thread. */ /** * @ingroup STA_RTOS_THREAD * @{ */ /** * @brief Number of bits in task notification flags used internally. */ #define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 1U /** * @brief Number of user usable bits in task notification flags. */ #define STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY ( STA_RTOS_MAX_BITS_THREAD_NOTIFY - STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY ) /** * @brief Mask for valid system task notification flag bits. */ #define STA_RTOS_THREAD_FLAGS_VALID_SYS_BITS ( ( ( 1UL << STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY ) - 1U ) << STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY ) /** * @brief Mask for valid user task notification flag bits. */ #define STA_RTOS_THREAD_FLAGS_VALID_USER_BITS ( ( 1UL << STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY ) - 1U ) /** * @brief Indicate error code instead of flag values. */ #define STA_RTOS_THREAD_FLAG_ERROR_CODE ( 1UL << 31 ) /** * @brief Request thread start. */ #define STA_RTOS_THREAD_FLAG_START ( 1UL << 30 ) #define STA_RTOS_THREAD_FLAGS_ERROR_CODE_BITS UINT32_C( 0x0000000F ) #define STA_RTOS_THREAD_FLAGS_ERROR_CODE_EXT_BITS UINT32_C( 0x7FFFFFF0 ) /** * @brief Error code from flags. */ #define STA_RTOS_THREAD_FLAGS_GET_EXT_ERROR_CODE(flags) (~flags & STA_RTOS_THREAD_FLAGS_ERROR_CODE_EXT_BITS) /** * @brief Wait for any of the given flags to be set. */ #define STA_RTOS_THREAD_FLAGS_WAIT_ANY(timeout) osThreadFlagsWait(STA_RTOS_THREAD_FLAGS_VALID_BITS, osFlagsWaitAny, timeout) /** @} */ namespace sta { /** * @brief Wrapper for RTOS thread control. * * @ingroup STA_RTOS_THREAD */ class RtosThread { public: /** * @brief Create an RtosThread. * * @param name The thread's name. Will be used for debugging. * @param prio The thread's priority. * @param stack_size The thread's stack size. Default of 0 refers to the RTOS2 default stack size specified in the IOC. * @param cb_size The thread's control block size. Default of 0 refers to the RTOS2 default control block size specified in the IOC. */ RtosThread(const char* name, osPriority_t prio, uint32_t stack_size = 0, uint32_t cb_size = 0); /** * @brief Get the currently running instance. * * @return The currently running instance id. */ osThreadId_t getInstance(); /** * @brief Get the name of this thread. */ const char* getName() const; /** * @brief Start the execution of this thread. */ virtual void start(); /** * @brief Sets the thread to BLOCKED for a given number of ticks. * Other threads can run in the meantime. * * @param ticks The number of ticks before setting the thread to READY again. */ void sleep(uint32_t ticks); /** * @brief Send user notification flags to thread. * * @param flags User flags */ void notify(uint32_t flags); /** * @return Returns the flags that were set for this thread. */ uint32_t getFlags(); /** * @brief Wait for certain thread flags. * * @return Returns the flags triggering the activation. */ uint32_t wait(uint32_t flags); /** * @brief Clears the thread flags set for this thread. * * @return Returns the thread flags that set before clearing. */ uint32_t clear(uint32_t flags); /** * @brief Forcibly terminate thread. */ void kill(); /** * @brief Send system notification flags to thread. * * @param flags System flags */ void sysNotify(uint32_t flags); protected: /** * @brief A function that wraps this task's functionality. */ virtual void loop() = 0; private: /** * @brief Static function to pass to RTOS to run as a thread. Calls the loop function implemented here. */ static void entry_point(void* arg); private: osThreadId_t instance_; osThreadAttr_t attribs_; }; } // namespace sta #endif // STA_RTOS_THREAD_HPP