/** * @brief Creates an active thread. * * The priority ranges from 9 to 38. Select a proper priority as required. * The maximum of tasks is LOSCFG_BASE_CORE_TSK_LIMIT(LOSCFG_BASE_CORE_TSK_LIMIT is defined in the traget_config.h). * @param func Indicates the entry of the thread callback function.线程函数 * @param argument Indicates the pointer to the argument passed to the thread.开始参数 * @param attr Indicates the thread attributes.线程属性 * @return Returns the thread ID; returns NULL in the case of an error. * @since 1.0 * @version 1.0 */ osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);
常用API
接口名称
函数说明
osThreadGetName
获取指定线程的名字
osThreadGetId
获取当前运行线程的线程ID
osThreadGetState
获取当前线程的状态
osThreadSetPriority
设置指定线程的优先级
osThreadGetPriority
获取当前线程的优先级
osThreadYield
将运行控制转交给下一个处于READY状态的线程
osThreadSuspend
挂起指定线程的运行
osThreadResume
恢复指定线程的运行
osThreadDetach
分离指定的线程(当线程终止运行时,线程存储可以被回收)
osThreadJoin
等待指定线程终止运行
osThreadExit
终止当前线程的运行
osThreadTerminate
终止指定线程的运行
osThreadGetStackSize
获取指定线程的栈空间大小
osThreadGetStackSpace
获取指定线程的未使用的栈空间大小
osThreadGetCount
获取活跃线程数
osThreadEnumerate
获取线程组中的活跃线程数
Timer
创建函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * @brief Creates and initializes a timer. * * This function creates a timer associated with the arguments callback function. The timer stays in the stopped state until OSTimerStart is used to start the timer. * The timer precision is 1000 / LOSCFG_BASE_CORE_TICK_PER_SECOND ms(LOSCFG_BASE_CORE_TICK_PER_SECOND is defined in the traget_config.h). * @param func Indicates the entry of the timer callback function. * @param type Indicates the timer type. * @param argument Indicates the pointer to the argument used in timer callback. * @param attr Indicates the pointer to the timer attributes. This parameter is not used. * @return Returns the timer ID; returns NULL in the case of an error. * @since 1.0 * @version 1.0 */ osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);
**/** * @brief Creates and initializes a mutex. * * @param attr Indicates the pointer to the mutex attributes. This parameter is not used. * @return Returns the mutex ID; returns NULL in the case of an error. * @since 1.0 * @version 1.0 */ osMutexId_t osMutexNew(const osMutexAttr_t *attr);**
/** * @brief Creates and initializes a semaphore object. * * @param max_count 最大令牌数,Indicates the maximum number of available tokens that can be applied for. * @param initial_count 初始令牌数,Indicates the initial number of available tokens. * @param attr Indicates the pointer to the semaphore attributes. This parameter is not used. * @return Returns the semaphore ID; returns NULL in the case of an error. * @since 1.0 * @version 1.0 */ osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);
voidconsumer_thread(int *arg) { (int)arg; while (NUM) { osSemaphoreAcquire(filled_id, osWaitForever); product_number--; printf("[Semp Test]%s consumes a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number); osDelay(OS_DELAY_S); osSemaphoreRelease(empty_id); } }
总结起来就是生产者productor根据空闲位置empty_id获取继续生产的资格
1
osSemaphoreAcquire(empty_id, osWaitForever);
只能使空闲位置empty_id减少,使产品数量filled_id增加
1
osSemaphoreRelease(filled_id);
消费者consumer根据产品数量filled_id获取消费资格
1
osSemaphoreAcquire(filled_id, osWaitForever);
只能使产品数量filled_id减少,使空闲位置empty_id增加
1
osSemaphoreRelease(empty_id);
消息队列
API名称
说明
osMessageQueueNew
创建和初始化一个消息队列
osMessageQueueGetName
返回指定的消息队列的名字
osMessageQueuePut
向指定的消息队列存放1条消息,如果消息队列满了,那么返回超时
osMessageQueueGet
从指定的消息队列中取得1条消息,如果消息队列为空,那么返回超时
osMessageQueueGetCapacity
获得指定的消息队列的消息容量
osMessageQueueGetMsgSize
获得指定的消息队列中可以存放的最大消息的大小
osMessageQueueGetCount
获得指定的消息队列中当前的消息数
osMessageQueueGetSpace
获得指定的消息队列中还可以存放的消息数
osMessageQueueReset
将指定的消息队列重置为初始状态
osMessageQueueDelete
删除指定的消息队列
创建函数
1 2 3 4 5 6 7 8 9
/** * @brief Creates and initializes a message queue. * * @param msg_count 队列长度:Indicates the number of messages in the message queue. * @param msg_size 队列消息大小:Indicates the size of messages in the message queue. * @param attr Indicates the pointer to the message queue attributes. This parameter is not used. * @return Returns the message queue ID; returns NULL in the case of an error. */ osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
发送消息
1 2 3 4 5 6 7 8 9 10 11
/** * @brief Places a message in a message queue. * * @param mq_id 消息队列ID:Indicates the message queue ID, which is obtained using osMessageQueueNew. * @param msg_ptr 消息指针:Indicates the pointer to the buffer for storing the message to be placed in the message queue. * @param msg_prio 消息优先级(此参数没用)Indicates the priority of the message to be placed in the message queue. This parameter is not used. * @param timeout Indicates the timeout duration. 表示超时时间,如果消息队列已满,函数会等待一段时间,直到有空间可以放入消息或者超时返回。 * @return Returns the CMSIS-RTOS running result. */ osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, constvoid *msg_ptr, uint8_t msg_prio, uint32_t timeout);
获取消息
1 2 3 4 5 6 7 8 9 10
/** * @brief Obtains a message in a message queue. * * @param mq_id 队列ID:Indicates the message queue ID, which is obtained using osMessageQueueNew. * @param msg_ptr 存放取得消息的指针:Indicates the pointer to the buffer for storing the message to be retrieved from the message queue. * @param msg_prio (无用)Indicates the pointer to the buffer for storing the priority of the message to be retrieved from the message queue. This parameter is not used. * @param timeout Indicates the timeout duration. * @return Returns the CMSIS-RTOS running result. */ osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
/** * @brief Obtains a message in a message queue. * * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew. * @param msg_ptr Indicates the pointer to the buffer for storing the message to be retrieved from the message queue. * @param msg_prio Indicates the pointer to the buffer for storing the priority of the message to be retrieved from the message queue. This parameter is not used. * @param timeout Indicates the timeout duration. * @return Returns the CMSIS-RTOS running result. * @since 1.0 * @version 1.0 */ osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
/** * When you want to send some messages to the iot server(including the response message), * please call this api * @param qos: the mqtt qos,:0,1,2 * @param topic: the iot mqtt topic * @param payload: the mqtt payload * * @return 0 success while others failed * * @instruction: if success means we write the message to the queue susccess, * not means communicate with the server success */ intIotSendMsg(int qos, constchar *topic, constchar *payload);
// <use this function to deal all the comming message staticintProcessQueueMsg(MQTTClient client) { unsignedint ret; unsignedint msgSize; IoTMsg_t *msg; unsignedint timeout; MQTTClient_message pubmsg = MQTTClient_message_initializer;
timeout = CN_QUEUE_WAITTIMEOUT; do { msg = NULL; msgSize = sizeof(hi_pvoid); ret = osMessageQueueGet(gIoTAppCb.queueID, &msg, &msgSize, timeout); if (msg != NULL) { IOT_LOG_DEBUG("QUEUEMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", msg->qos, msg->topic, msg->payload); MqttProcessQueueMsg(client, msg, pubmsg); hi_free(0, msg); } timeout = 0; // < continous to deal the message without wait here } while (ret == IOT_SUCCESS);