当前位置:   article > 正文

鸿蒙开发板HI3861 利用蜂鸣器播放音乐 open Harmony1.0.0(附cmsis_os2.h源码)_鸿蒙os在蜂鸣器上播放一曲

鸿蒙os在蜂鸣器上播放一曲

        本文利用鸿蒙开发板HI3861在鸿蒙1.0.0源码下利用蜂鸣器播放音乐。

pwm_buz.c

  1. #include <stdio.h>
  2. #include "ohos_init.h"
  3. #include "cmsis_os2.h"
  4. #include "wifiiot_gpio.h"
  5. #include "wifiiot_gpio_ex.h"
  6. #include "wifiiot_pwm.h"
  7. #include "hi_pwm.h"
  8. #include "hi_time.h"
  9. static const uint16_t g_tuneFreqs[]={
  10. 0, //Default clock,40MHz
  11. //高音
  12. 38223, //1
  13. 34052, //2
  14. 30338,
  15. 28635,
  16. 25511,
  17. 22728,
  18. 20249, //7
  19. 51021,//-5 8
  20. 45454,//-6 9
  21. 40495//-7 10
  22. };
  23. static const uint8_t g_scoreNotes[]={
  24. //the music score of Two Tigers
  25. 5,3,2,1,5,1,2,3,8,5,3,2,3,5,1,9,3,2,3,5,6,5,3,5,2,3,2,1,9,3,2,2,1,9,1,8,5,3,6,5,6,2,3,5,
  26. };
  27. static const uint8_t g_scoreDurations[]={
  28. //the time for each note
  29. 3,1,2,2,8,4,2,2,8,3,1,4,2,2,4,2,2,8,4,4,6,2,4,4,3,1,2,2,4,4,2,4,2,4,4,8,6,2,4,4,4,2,2,8,
  30. };
  31. static void PwmBuzTask(void *arg)
  32. {
  33. (void) arg;
  34. GpioInit();
  35. IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9,WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
  36. PwmInit(WIFI_IOT_PWM_PORT_PWM0);
  37. /*A*/
  38. PwmStart(WIFI_IOT_PWM_PORT_PWM0,20000,40000);
  39. //this line may occur error
  40. osDelay(100);
  41. PwmStop(WIFI_IOT_PWM_PORT_PWM0);
  42. /*B*/
  43. hi_pwm_set_clock(PWM_CLK_XTAL);
  44. for (size_t i=0;i<sizeof(g_scoreNotes)/sizeof(g_scoreNotes[0]);i++){
  45. //here C< A/B,the order attention
  46. uint32_t tune = g_scoreNotes[i]; //traverse the MusicScore and read the note
  47. uint16_t freqDivisor = g_tuneFreqs[tune]; //the fequency of the note
  48. uint32_t tuneInterval = g_scoreDurations[i] * (125 * 1000); //the time of the note
  49. //para parepared
  50. printf("%d %d %d\r\n",tune,freqDivisor);
  51. PwmStart(WIFI_IOT_PWM_PORT_PWM0,freqDivisor/2, freqDivisor);
  52. //占空比 50%
  53. hi_udelay(tuneInterval);
  54. //u -> us单位微秒。还有ndelay。mdelay
  55. PwmStop(WIFI_IOT_PWM_PORT_PWM0);
  56. }
  57. }
  58. static void PwmBuzEntry(void)
  59. {
  60. osThreadAttr_t attr = {0};
  61. attr.name = "PwmBuzTask";
  62. attr.stack_size = 4096;
  63. attr.priority = osPriorityNormal;
  64. if(osThreadNew(PwmBuzTask,NULL,&attr) == NULL){
  65. printf("[PwmBuzEntry] create PwmBuzTask failed!\n");
  66. }
  67. }
  68. SYS_RUN(PwmBuzEntry);

BUILD.c文件

  1. static_library("pwm_demo"){
  2. sources = ["pwm_buz.c"]
  3. include_dirs = [
  4. "//third_party/cmsis/CMSIS/RTOS2/Include",
  5. "//utils/native/lite/include",
  6. "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  7. ]
  8. }

注意:

1.注意BUILD.gn文件名,BUILD五个字母均为大写

2.编译时可能出现"cmsis_os2.h"文件不存在的情况。此时将"cmsis_os2.h"文件源码复制到本级目录即可。"cmsis_os2.h"源码在文末给出

3.本代码只适用于1.0.0版本

4.自己想要的音阶在g_tuneFreqs中的值可以用40000000/频率估算,因为主频是40MHZ。

cmsis_os2.h源码

cmsis_os2.h · 傲然_手握日月摘星辰/third_party_cmsis - Gitee.comhttps://gitee.com/proud_321/third_party_cmsis/blob/master/cmsis_os2.h

  1. /*
  2. * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * ----------------------------------------------------------------------
  19. *
  20. * $Date: 18. June 2018
  21. * $Revision: V2.1.3
  22. *
  23. * Project: CMSIS-RTOS2 API
  24. * Title: cmsis_os2.h header file
  25. *
  26. * Version 2.1.3
  27. * Additional functions allowed to be called from Interrupt Service Routines:
  28. * - osThreadGetId
  29. * Version 2.1.2
  30. * Additional functions allowed to be called from Interrupt Service Routines:
  31. * - osKernelGetInfo, osKernelGetState
  32. * Version 2.1.1
  33. * Additional functions allowed to be called from Interrupt Service Routines:
  34. * - osKernelGetTickCount, osKernelGetTickFreq
  35. * Changed Kernel Tick type to uint32_t:
  36. * - updated: osKernelGetTickCount, osDelayUntil
  37. * Version 2.1.0
  38. * Support for critical and uncritical sections (nesting safe):
  39. * - updated: osKernelLock, osKernelUnlock
  40. * - added: osKernelRestoreLock
  41. * Updated Thread and Event Flags:
  42. * - changed flags parameter and return type from int32_t to uint32_t
  43. * Version 2.0.0
  44. * Initial Release
  45. *---------------------------------------------------------------------------*/
  46. /**
  47. * @addtogroup CMSIS
  48. * @{
  49. *
  50. * @brief Provides standard, universal real-time operating system (RTOS) APIs.
  51. *
  52. * CMSIS Module may contain portions from ARM Cortex Microcontroller Software Interface Standard (CMSIS) licensed under Apache License v2.0.
  53. *
  54. * @since 1.0
  55. * @version 1.0
  56. */
  57. #ifndef CMSIS_OS2_H_
  58. #define CMSIS_OS2_H_
  59. #ifndef __NO_RETURN
  60. #if defined(__CC_ARM)
  61. #define __NO_RETURN __declspec(noreturn)
  62. #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  63. #define __NO_RETURN __attribute__((__noreturn__))
  64. #elif defined(__GNUC__)
  65. #define __NO_RETURN __attribute__((__noreturn__))
  66. #elif defined(__ICCARM__)
  67. #define __NO_RETURN __noreturn
  68. #else
  69. #define __NO_RETURN
  70. #endif
  71. #endif
  72. #include <stdint.h>
  73. #include <stddef.h>
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. // ==== Enumerations, structures, defines ====
  78. /**
  79. * @brief Describes the system version.
  80. *
  81. * @since 1.0
  82. * @version 1.0
  83. */
  84. typedef struct {
  85. /** API version */
  86. uint32_t api;
  87. /** Kernel version */
  88. uint32_t kernel;
  89. } osVersion_t;
  90. /**
  91. * @brief Enumerates kernel states.
  92. *
  93. */
  94. typedef enum {
  95. /** The kernel is inactive. */
  96. osKernelInactive = 0,
  97. /** The kernel is ready. */
  98. osKernelReady = 1,
  99. /** The kernel is running. */
  100. osKernelRunning = 2,
  101. /** The kernel is locked. */
  102. osKernelLocked = 3,
  103. /** The kernel is suspended. */
  104. osKernelSuspended = 4,
  105. /** The kernel is abnormal. */
  106. osKernelError = -1,
  107. /** Reserved */
  108. osKernelReserved = 0x7FFFFFFFU
  109. } osKernelState_t;
  110. /**
  111. * @brief Enumerates thread states.
  112. *
  113. */
  114. typedef enum {
  115. /** The thread is inactive. */
  116. osThreadInactive = 0,
  117. /** The thread is ready. */
  118. osThreadReady = 1,
  119. /** The thread is running. */
  120. osThreadRunning = 2,
  121. /** The thread is blocked. */
  122. osThreadBlocked = 3,
  123. /** The thread is terminated. */
  124. osThreadTerminated = 4,
  125. /** The thread is abnormal. */
  126. osThreadError = -1,
  127. /** Reserved */
  128. osThreadReserved = 0x7FFFFFFF
  129. } osThreadState_t;
  130. /**
  131. * @brief Enumerates thread priorities.
  132. *
  133. */
  134. typedef enum {
  135. /** Undefined */
  136. osPriorityNone = 0,
  137. /** Reserved for idle threads */
  138. osPriorityIdle = 1,
  139. /** Low (unsupported) */
  140. osPriorityLow = 8,
  141. /** Low + 1 */
  142. osPriorityLow1 = 8+1,
  143. /** Low + 2 */
  144. osPriorityLow2 = 8+2,
  145. /** Low + 3 */
  146. osPriorityLow3 = 8+3,
  147. /** Low + 4 */
  148. osPriorityLow4 = 8+4,
  149. /** Low + 5 */
  150. osPriorityLow5 = 8+5,
  151. /** Low + 6 */
  152. osPriorityLow6 = 8+6,
  153. /** Low + 7 */
  154. osPriorityLow7 = 8+7,
  155. /** Below normal */
  156. osPriorityBelowNormal = 16,
  157. /** Below normal + 1 */
  158. osPriorityBelowNormal1 = 16+1,
  159. /** Below normal + 2 */
  160. osPriorityBelowNormal2 = 16+2,
  161. /** Below normal + 3 */
  162. osPriorityBelowNormal3 = 16+3,
  163. /** Below normal + 4 */
  164. osPriorityBelowNormal4 = 16+4,
  165. /** Below normal + 5 */
  166. osPriorityBelowNormal5 = 16+5,
  167. /** Below normal + 6 */
  168. osPriorityBelowNormal6 = 16+6,
  169. /** Below normal + 7 */
  170. osPriorityBelowNormal7 = 16+7,
  171. /** Normal */
  172. osPriorityNormal = 24,
  173. /** Normal + 1 */
  174. osPriorityNormal1 = 24+1,
  175. /** Normal + 2 */
  176. osPriorityNormal2 = 24+2,
  177. /** Normal + 3 */
  178. osPriorityNormal3 = 24+3,
  179. /** Normal + 4 */
  180. osPriorityNormal4 = 24+4,
  181. /** Normal + 5 */
  182. osPriorityNormal5 = 24+5,
  183. /** Normal + 6 */
  184. osPriorityNormal6 = 24+6,
  185. /** Normal + 7 */
  186. osPriorityNormal7 = 24+7,
  187. /** Above normal */
  188. osPriorityAboveNormal = 32,
  189. /** Above normal + 1 */
  190. osPriorityAboveNormal1 = 32+1,
  191. /** Above normal + 2 */
  192. osPriorityAboveNormal2 = 32+2,
  193. /** Above normal + 3 */
  194. osPriorityAboveNormal3 = 32+3,
  195. /** Above normal + 4 */
  196. osPriorityAboveNormal4 = 32+4,
  197. /** Above normal + 5 */
  198. osPriorityAboveNormal5 = 32+5,
  199. /** Above normal + 6 */
  200. osPriorityAboveNormal6 = 32+6,
  201. /** Above normal + 7 (unsupported) */
  202. osPriorityAboveNormal7 = 32+7,
  203. /** High (unsupported) */
  204. osPriorityHigh = 40,
  205. /** High + 1 (unsupported) */
  206. osPriorityHigh1 = 40+1,
  207. /** High + 2 (unsupported) */
  208. osPriorityHigh2 = 40+2,
  209. /** High + 3 (unsupported) */
  210. osPriorityHigh3 = 40+3,
  211. /** High + 4 (unsupported) */
  212. osPriorityHigh4 = 40+4,
  213. /** High + 5 (unsupported) */
  214. osPriorityHigh5 = 40+5,
  215. /** High + 6 (unsupported) */
  216. osPriorityHigh6 = 40+6,
  217. /** High + 7 (unsupported) */
  218. osPriorityHigh7 = 40+7,
  219. /** Real-time (unsupported) */
  220. osPriorityRealtime = 48,
  221. /** Real-time + 1 (unsupported) */
  222. osPriorityRealtime1 = 48+1,
  223. /** Real-time + 2 (unsupported) */
  224. osPriorityRealtime2 = 48+2,
  225. /** Real-time + 3 (unsupported) */
  226. osPriorityRealtime3 = 48+3,
  227. /** Real-time + 4 (unsupported) */
  228. osPriorityRealtime4 = 48+4,
  229. /** Real-time + 5 (unsupported) */
  230. osPriorityRealtime5 = 48+5,
  231. /** Real-time + 6 (unsupported) */
  232. osPriorityRealtime6 = 48+6,
  233. /** Real-time + 7 (unsupported) */
  234. osPriorityRealtime7 = 48+7,
  235. /** Reserved for ISR deferred threads (unsupported) */
  236. osPriorityISR = 56,
  237. /** Invalid */
  238. osPriorityError = -1,
  239. /** Reserved. It enables the compiler to identify enumeration variables as 32-bit numbers and prevents the enumeration variables from being optimized. */
  240. osPriorityReserved = 0x7FFFFFFF
  241. } osPriority_t;
  242. /**
  243. * @brief Callback for thread scheduling
  244. *
  245. */
  246. typedef void (*osThreadFunc_t) (void *argument);
  247. /**
  248. * @brief Callback for timer triggering
  249. *
  250. */
  251. typedef void (*osTimerFunc_t) (void *argument);
  252. /**
  253. * @brief Enumerates timer types.
  254. *
  255. */
  256. typedef enum {
  257. /** One-shot timer */
  258. osTimerOnce = 0,
  259. /** Repeating timer */
  260. osTimerPeriodic = 1
  261. } osTimerType_t;
  262. /**
  263. * @brief Indicates that the RTOS waits forever unless an event flag is received.
  264. *
  265. */
  266. #define osWaitForever 0xFFFFFFFFU
  267. /**
  268. * @brief Indicates that the RTOS does not wait.
  269. *
  270. */
  271. #define osNoWait 0x0U
  272. /**
  273. * @brief Indicates that the RTOS waits until any event flag is triggered.
  274. *
  275. */
  276. #define osFlagsWaitAny 0x00000000U
  277. /**
  278. * @brief Indicates that the system waits until all event flags are triggered.
  279. *
  280. */
  281. #define osFlagsWaitAll 0x00000001U
  282. /**
  283. * @brief Indicates that defined flags are not cleared.
  284. *
  285. */
  286. #define osFlagsNoClear 0x00000002U
  287. /**
  288. * @brief Indicates a flag error.
  289. *
  290. */
  291. #define osFlagsError 0x80000000U
  292. /**
  293. * @brief Indicates an unknown error.
  294. *
  295. */
  296. #define osFlagsErrorUnknown 0xFFFFFFFFU
  297. /**
  298. * @brief Indicates a timeout.
  299. *
  300. */
  301. #define osFlagsErrorTimeout 0xFFFFFFFEU
  302. /**
  303. * @brief Indicates a resource error.
  304. *
  305. */
  306. #define osFlagsErrorResource 0xFFFFFFFDU
  307. /**
  308. * @brief Indicates an incorrect parameter.
  309. *
  310. */
  311. #define osFlagsErrorParameter 0xFFFFFFFCU
  312. #define osFlagsErrorISR 0xFFFFFFFAU
  313. // Thread attributes (attr_bits in \ref osThreadAttr_t).
  314. #define osThreadDetached 0x00000000U
  315. #define osThreadJoinable 0x00000001U
  316. // Mutex attributes (attr_bits in \ref osMutexAttr_t).
  317. #define osMutexRecursive 0x00000001U
  318. #define osMutexPrioInherit 0x00000002U
  319. #define osMutexRobust 0x00000008U
  320. /**
  321. * @brief Enumerates return values of CMSIS-RTOS.
  322. *
  323. */
  324. typedef enum {
  325. /** Operation completed successfully */
  326. osOK = 0,
  327. /** Unspecified error */
  328. osError = -1,
  329. /** Timeout */
  330. osErrorTimeout = -2,
  331. /** Resource error */
  332. osErrorResource = -3,
  333. /** Incorrect parameter */
  334. osErrorParameter = -4,
  335. /** Insufficient memory */
  336. osErrorNoMemory = -5,
  337. /** Service interruption */
  338. osErrorISR = -6,
  339. /** Reserved. It is used to prevent the compiler from optimizing enumerations. */
  340. osStatusReserved = 0x7FFFFFFF
  341. } osStatus_t;
  342. /**
  343. * @brief Identifies a thread.
  344. *
  345. */
  346. typedef void *osThreadId_t;
  347. /**
  348. * @brief Identifies a timer.
  349. *
  350. */
  351. typedef void *osTimerId_t;
  352. /**
  353. * @brief Identifies an event flag.
  354. *
  355. */
  356. typedef void *osEventFlagsId_t;
  357. /**
  358. * @brief Identifies a mutex.
  359. *
  360. */
  361. typedef void *osMutexId_t;
  362. /**
  363. * @brief Identifies a semaphore object.
  364. *
  365. */
  366. typedef void *osSemaphoreId_t;
  367. typedef void *osMemoryPoolId_t;
  368. /**
  369. * @brief Identifies a message queue.
  370. *
  371. */
  372. typedef void *osMessageQueueId_t;
  373. #ifndef TZ_MODULEID_T
  374. #define TZ_MODULEID_T
  375. /**
  376. * @brief Identifies a TrustZone module call process.
  377. *
  378. */
  379. typedef uint32_t TZ_ModuleId_t;
  380. #endif
  381. /**
  382. * @brief Describes thread attributes.
  383. *
  384. * @since 1.0
  385. * @version 1.0
  386. */
  387. typedef struct {
  388. /** Thread name */
  389. const char *name;
  390. /** Thread attribute bits */
  391. uint32_t attr_bits;
  392. /** Memory for the thread control block */
  393. void *cb_mem;
  394. /** Size of the memory for the thread control block */
  395. uint32_t cb_size;
  396. /** Memory for the thread stack */
  397. void *stack_mem;
  398. /** Size of the thread stack */
  399. uint32_t stack_size;
  400. /** Thread priority */
  401. osPriority_t priority;
  402. /** TrustZone module of the thread */
  403. TZ_ModuleId_t tz_module;
  404. /** Reserved */
  405. uint32_t reserved;
  406. } osThreadAttr_t;
  407. /**
  408. * @brief Describes timer attributes.
  409. *
  410. * @since 1.0
  411. * @version 1.0
  412. */
  413. typedef struct {
  414. /** Timer name */
  415. const char *name;
  416. /** Reserved attribute bits */
  417. uint32_t attr_bits;
  418. /** Memory for the timer control block */
  419. void *cb_mem;
  420. /** Size of the memory for the timer control block */
  421. uint32_t cb_size;
  422. } osTimerAttr_t;
  423. /**
  424. * @brief Describes event attributes.
  425. *
  426. * @since 1.0
  427. * @version 1.0
  428. */
  429. typedef struct {
  430. /** Event name */
  431. const char *name;
  432. /** Reserved attribute bits */
  433. uint32_t attr_bits;
  434. /** Memory for the event control block */
  435. void *cb_mem;
  436. /** Size of the memory for the event control block */
  437. uint32_t cb_size;
  438. } osEventFlagsAttr_t;
  439. /**
  440. * @brief Describes mutex attributes.
  441. *
  442. * @since 1.0
  443. * @version 1.0
  444. */
  445. typedef struct {
  446. /** Mutex name */
  447. const char *name;
  448. /** Reserved attribute bits */
  449. uint32_t attr_bits;
  450. /** Memory for the mutex control block */
  451. void *cb_mem;
  452. /** Size of the memory for the mutex control block */
  453. uint32_t cb_size;
  454. } osMutexAttr_t;
  455. /**
  456. * @brief Describes semaphore attributes.
  457. *
  458. * @since 1.0
  459. * @version 1.0
  460. */
  461. typedef struct {
  462. /** Semaphore name */
  463. const char *name;
  464. /** Reserved attribute bits */
  465. uint32_t attr_bits;
  466. /** Memory for the semaphore control block */
  467. void *cb_mem;
  468. /** Size of the memory for the semaphore control block */
  469. uint32_t cb_size;
  470. } osSemaphoreAttr_t;
  471. typedef struct {
  472. const char *name;
  473. uint32_t attr_bits;
  474. void *cb_mem;
  475. uint32_t cb_size;
  476. void *mp_mem;
  477. uint32_t mp_size;
  478. } osMemoryPoolAttr_t;
  479. /**
  480. * @brief Describes message queue attributes.
  481. *
  482. * @since 1.0
  483. * @version 1.0
  484. */
  485. typedef struct {
  486. /** Message queue name */
  487. const char *name;
  488. /** Reserved attribute bits */
  489. uint32_t attr_bits;
  490. /** Memory for the message queue control block */
  491. void *cb_mem;
  492. /** Size of the memory for the message queue control block */
  493. uint32_t cb_size;
  494. /** Memory for storing data in the message queue */
  495. void *mq_mem;
  496. /** Size of the memory for storing data in the message queue */
  497. uint32_t mq_size;
  498. } osMessageQueueAttr_t;
  499. // ==== Kernel Management Functions ====
  500. /**
  501. * @brief Initializes the RTOS kernel.
  502. *
  503. * @return Returns the CMSIS-RTOS running result.
  504. * @since 1.0
  505. * @version 1.0
  506. */
  507. osStatus_t osKernelInitialize (void);
  508. /**
  509. * @brief Obtains the system version and name.
  510. *
  511. * @param version Indicates the pointer to the buffer for storing the version.
  512. * @param id_buf Indicates the pointer to the buffer for storing the kernel ID.
  513. * @param id_size Indicates the size of the buffer for storing the kernel ID.
  514. * @return Returns the CMSIS-RTOS running result.
  515. * @since 1.0
  516. * @version 1.0
  517. */
  518. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size);
  519. /**
  520. * @brief Obtains the kernel state.
  521. *
  522. * @return Returns the kernel state.
  523. * @since 1.0
  524. * @version 1.0
  525. */
  526. osKernelState_t osKernelGetState (void);
  527. /**
  528. * @brief Starts the kernel.
  529. *
  530. * @return Returns the CMSIS-RTOS running result.
  531. * @since 1.0
  532. * @version 1.0
  533. */
  534. osStatus_t osKernelStart (void);
  535. /**
  536. * @brief Locks the kernel.
  537. *
  538. * @return Returns 1 if the kernel is locked successfully; returns 0 if the lock starts; returns a negative value in the case of an error.
  539. * @since 1.0
  540. * @version 1.0
  541. */
  542. int32_t osKernelLock (void);
  543. /**
  544. * @brief Unlocks the kernel.
  545. *
  546. * @return Returns 1 if the kernel is unlocked successfully; returns 0 if the kernel is not locked; returns a negative value in the case of an error.
  547. * @since 1.0
  548. * @version 1.0
  549. */
  550. int32_t osKernelUnlock (void);
  551. /**
  552. * @brief Restores the previous lock state of the kernel.
  553. *
  554. * @param lock Indicates the lock state to restore to. The value 1 indicates the locked state, and 0 indicates the unlocked state.
  555. * @return Returns 1 if the kernel is locked; returns 0 if the kernel is not locked; returns a negative value in the case of an error.
  556. * @since 1.0
  557. * @version 1.0
  558. */
  559. int32_t osKernelRestoreLock (int32_t lock);
  560. uint32_t osKernelSuspend (void);
  561. void osKernelResume (uint32_t sleep_ticks);
  562. /// Get the RTOS kernel tick count.
  563. /// \return RTOS kernel current tick count.
  564. uint32_t osKernelGetTickCount (void);
  565. /**
  566. * @brief Obtains the number of kernel ticks per second.
  567. *
  568. * @return Returns the number of kernel ticks.
  569. * @since 1.0
  570. * @version 1.0
  571. */
  572. uint32_t osKernelGetTickFreq (void);
  573. /**
  574. * @brief Obtains the kernel system timer.
  575. *
  576. * @return Returns the kernel system timer.
  577. * @since 1.0
  578. * @version 1.0
  579. */
  580. uint32_t osKernelGetSysTimerCount (void);
  581. /**
  582. * @brief Obtains the frequency of the system timer.
  583. *
  584. * @return Returns the system timer frequency.
  585. * @since 1.0
  586. * @version 1.0
  587. */
  588. uint32_t osKernelGetSysTimerFreq (void);
  589. // ==== Thread Management Functions ====
  590. /**
  591. * @brief Creates an active thread.
  592. *
  593. * The task priority ranges from 9 (highest priority) to 38 (lowest priority). {@code LOSCFG_BASE_CORE_TSK_LIMIT} declared in target_config.h specifies the
  594. maximum number of tasks running in this system.
  595. * @param func Indicates the entry of the thread callback function.
  596. * @param argument Indicates the pointer to the argument passed to the thread.
  597. * @param attr Indicates the thread attributes.
  598. * @return Returns the thread ID; returns NULL in the case of an error.
  599. * @since 1.0
  600. * @version 1.0
  601. */
  602. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);
  603. /**
  604. * @brief Obtains the name of a thread.
  605. *
  606. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  607. * @return Returns the thread name; returns NULL in the case of an error.
  608. * @since 1.0
  609. * @version 1.0
  610. */
  611. const char *osThreadGetName (osThreadId_t thread_id);
  612. /**
  613. * @brief Obtains the ID of the currently running thread.
  614. *
  615. * @return Returns the thread ID; returns NULL in the case of an error.
  616. * @since 1.0
  617. * @version 1.0
  618. */
  619. osThreadId_t osThreadGetId (void);
  620. /**
  621. * @brief Obtains the state of a thread.
  622. *
  623. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  624. * @return Returns the thread state.
  625. * @since 1.0
  626. * @version 1.0
  627. */
  628. osThreadState_t osThreadGetState (osThreadId_t thread_id);
  629. /**
  630. * @brief Obtains the stack size of a thread.
  631. *
  632. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  633. * @return Returns the stack size, in bytes; returns 0 in the case of an error.
  634. * @since 1.0
  635. * @version 1.0
  636. */
  637. uint32_t osThreadGetStackSize (osThreadId_t thread_id);
  638. /**
  639. * @brief Obtains the size of the available stack space for a thread based on the stack watermark.
  640. *
  641. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  642. * @return Returns the available stack size, in bytes; returns 0 in the case of an error.
  643. * @since 1.0
  644. * @version 1.0
  645. */
  646. uint32_t osThreadGetStackSpace (osThreadId_t thread_id);
  647. /**
  648. * @brief Changes the priority of a thread.
  649. *
  650. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  651. * @param priority Indicates the new priority.
  652. * @return Returns the CMSIS-RTOS running result.
  653. * @since 1.0
  654. * @version 1.0
  655. */
  656. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority);
  657. /**
  658. * @brief Gets the prority of an active thread.
  659. *
  660. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  661. * @return Returns the prority of the thread.
  662. * @since 1.0
  663. * @version 1.0
  664. */
  665. osPriority_t osThreadGetPriority (osThreadId_t thread_id);
  666. /**
  667. * @brief Sets the currently running thread to the ready state.
  668. *
  669. * @return Returns the CMSIS-RTOS running result.
  670. * @since 1.0
  671. * @version 1.0
  672. */
  673. osStatus_t osThreadYield (void);
  674. /**
  675. * @brief Suspends a thread.
  676. *
  677. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  678. * @return Returns the CMSIS-RTOS running result.
  679. * @since 1.0
  680. * @version 1.0
  681. */
  682. osStatus_t osThreadSuspend (osThreadId_t thread_id);
  683. /**
  684. * @brief Resumes a thread from the suspended state.
  685. *
  686. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  687. * @return Returns the CMSIS-RTOS running result.
  688. * @since 1.0
  689. * @version 1.0
  690. */
  691. osStatus_t osThreadResume (osThreadId_t thread_id);
  692. osStatus_t osThreadDetach (osThreadId_t thread_id);
  693. osStatus_t osThreadJoin (osThreadId_t thread_id);
  694. void osThreadExit (void);
  695. /**
  696. * @brief Terminates a thread.
  697. *
  698. * @param thread_id Indicates the thread ID, which is obtained using osThreadNew or osThreadGetId.
  699. * @return Returns the CMSIS-RTOS running result.
  700. * @since 1.0
  701. * @version 1.0
  702. */
  703. osStatus_t osThreadTerminate (osThreadId_t thread_id);
  704. /**
  705. * @brief Obtains the number of active threads.
  706. *
  707. * @return Returns the number; returns 0 in the case of an error.
  708. * @since 1.0
  709. * @version 1.0
  710. */
  711. uint32_t osThreadGetCount (void);
  712. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items);
  713. // ==== Thread Flags Functions ====
  714. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags);
  715. uint32_t osThreadFlagsClear (uint32_t flags);
  716. uint32_t osThreadFlagsGet (void);
  717. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout);
  718. // ==== Generic Wait Functions ====
  719. /**
  720. * @brief Waits for a period of time.
  721. *
  722. * @param ticks Indicates the number of ticks to wait for.
  723. * @return Returns the CMSIS-RTOS running result.
  724. * @since 1.0
  725. * @version 1.0
  726. */
  727. osStatus_t osDelay (uint32_t ticks);
  728. /**
  729. * @brief Waits until a specified time arrives.
  730. *
  731. * This function handles the overflow of the system timer. Note that the maximum value of this parameter is (2^31 - 1) ticks.
  732. * @param ticks Indicates the number of ticks converted from the absolute time.
  733. * @return Returns the CMSIS-RTOS running result.
  734. * @since 1.0
  735. * @version 1.0
  736. */
  737. osStatus_t osDelayUntil (uint32_t ticks);
  738. // ==== Timer Management Functions ====
  739. /**
  740. * @brief Creates and initializes a timer.
  741. *
  742. * 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.
  743. * 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).
  744. * @param func Indicates the entry of the timer callback function.
  745. * @param type Indicates the timer type.
  746. * @param argument Indicates the pointer to the argument used in timer callback.
  747. * @param attr Indicates the pointer to the timer attributes. This parameter is not used.
  748. * @return Returns the timer ID; returns NULL in the case of an error.
  749. * @since 1.0
  750. * @version 1.0
  751. */
  752. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);
  753. /**
  754. * @brief Obtains the timer name.
  755. *
  756. * @param timer_id Indicates the timer ID, which is obtained using osTimerNew.
  757. * @return Returns the timer name; returns NULL in the case of an error.
  758. * @since 1.0
  759. * @version 1.0
  760. */
  761. const char *osTimerGetName (osTimerId_t timer_id);
  762. /**
  763. * @brief Starts or restarts a timer.
  764. *
  765. * @param timer_id Indicates the timer ID, which is obtained using osTimerNew.
  766. * @param ticks Indicates the number of ticks since the timer starts running.
  767. * @return Returns the CMSIS-RTOS running result.
  768. * @since 1.0
  769. * @version 1.0
  770. */
  771. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks);
  772. /**
  773. * @brief Stops a timer.
  774. *
  775. * @param timer_id Indicates the timer ID, which is obtained using osTimerNew.
  776. * @return Returns the CMSIS-RTOS running result.
  777. * @since 1.0
  778. * @version 1.0
  779. */
  780. osStatus_t osTimerStop (osTimerId_t timer_id);
  781. /**
  782. * @brief Checks whether a timer is running.
  783. *
  784. * @param timer_id Indicates the timer ID, which is obtained using osTimerNew.
  785. * @return Returns 1 if the timer is running; returns 0 otherwise.
  786. * @since 1.0
  787. * @version 1.0
  788. */
  789. uint32_t osTimerIsRunning (osTimerId_t timer_id);
  790. /**
  791. * @brief Deletes a timer.
  792. *
  793. * @param timer_id Indicates the timer ID, which is obtained using osTimerNew.
  794. * @return Returns the CMSIS-RTOS running result.
  795. * @since 1.0
  796. * @version 1.0
  797. */
  798. osStatus_t osTimerDelete (osTimerId_t timer_id);
  799. // ==== Event Flags Management Functions ====
  800. /**
  801. * @brief Creates and initializes an event flags object.
  802. *
  803. * @param attr Indicates the pointer to the event flags attributes. This parameter is not used.
  804. * @return Returns the event flags ID; returns NULL in the case of an error.
  805. * @since 1.0
  806. * @version 1.0
  807. */
  808. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr);
  809. /**
  810. * @brief Obtains the name of an event flags object.
  811. *
  812. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  813. * @return Returns the event flags name; returns NULL in the case of an error.
  814. * @since 1.0
  815. * @version 1.0
  816. */
  817. const char *osEventFlagsGetName (osEventFlagsId_t ef_id);
  818. /**
  819. * @brief Sets event flags.
  820. *
  821. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  822. * @param flags Indicates the event flags to set.
  823. * @return Returns the event flags; returns osFlagsErrorParameter in the case of an error.
  824. * @since 1.0
  825. * @version 1.0
  826. */
  827. uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags);
  828. /**
  829. * @brief Clears event flags.
  830. *
  831. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  832. * @param flags Indicates the event flags to clear.
  833. * @return Returns the event flags; returns osFlagsErrorParameter in the case of an error.
  834. * @since 1.0
  835. * @version 1.0
  836. */
  837. uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags);
  838. /**
  839. * @brief Obtains event flags.
  840. *
  841. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  842. * @return Returns the event flags triggered.
  843. * @since 1.0
  844. * @version 1.0
  845. */
  846. uint32_t osEventFlagsGet (osEventFlagsId_t ef_id);
  847. /**
  848. * @brief Waits for event flags to trigger.
  849. *
  850. * This function is blocked if the specified event flags are not set via {@code flags}.
  851. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  852. * @param flags Indicates the event flags to trigger.
  853. * @param options Indicates the configuration of the event flags to trigger.
  854. * @param timeout Indicates the timeout duration.
  855. * @return Returns the triggered event flags; returns an error value in the case of an error.
  856. * @since 1.0
  857. * @version 1.0
  858. */
  859. uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);
  860. /**
  861. * @brief Deletes an event flags object.
  862. *
  863. * @param ef_id Indicates the event flags ID, which is obtained using osEventFlagsNew.
  864. * @return Returns the CMSIS-RTOS running result.
  865. * @since 1.0
  866. * @version 1.0
  867. */
  868. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id);
  869. // ==== Mutex Management Functions ====
  870. /**
  871. * @brief Creates and initializes a mutex.
  872. *
  873. * @param attr Indicates the pointer to the mutex attributes. This parameter is not used.
  874. * @return Returns the mutex ID; returns NULL in the case of an error.
  875. * @since 1.0
  876. * @version 1.0
  877. */
  878. osMutexId_t osMutexNew (const osMutexAttr_t *attr);
  879. const char *osMutexGetName (osMutexId_t mutex_id);
  880. /**
  881. * @brief Obtains a mutex.
  882. *
  883. * @param mutex_id Indicates the mutex ID, which is obtained using osMutexNew.
  884. * @param timeout Indicates the timeout duration.
  885. * @return Returns the CMSIS-RTOS running result.
  886. * @since 1.0
  887. * @version 1.0
  888. */
  889. osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout);
  890. /**
  891. * @brief Releases a mutex.
  892. *
  893. * @param mutex_id Indicates the mutex ID, which is obtained using osMutexNew.
  894. * @return Returns the CMSIS-RTOS running result.
  895. * @since 1.0
  896. * @version 1.0
  897. */
  898. osStatus_t osMutexRelease (osMutexId_t mutex_id);
  899. /**
  900. * @brief Obtains the thread ID of the currently acquired mutex.
  901. *
  902. * @param mutex_id Indicates the mutex ID, which is obtained using osMutexNew.
  903. * @return Returns the thread ID.
  904. * @since 1.0
  905. * @version 1.0
  906. */
  907. osThreadId_t osMutexGetOwner (osMutexId_t mutex_id);
  908. /**
  909. * @brief Deletes a mutex.
  910. *
  911. * @param mutex_id Indicates the mutex ID, which is obtained using osMutexNew.
  912. * @return Returns the CMSIS-RTOS running result.
  913. * @since 1.0
  914. * @version 1.0
  915. */
  916. osStatus_t osMutexDelete (osMutexId_t mutex_id);
  917. // ==== Semaphore Management Functions ====
  918. /**
  919. * @brief Creates and initializes a semaphore object.
  920. *
  921. * @param max_count Indicates the maximum number of available tokens that can be applied for.
  922. * @param initial_count Indicates the initial number of available tokens.
  923. * @param attr Indicates the pointer to the semaphore attributes. This parameter is not used.
  924. * @return Returns the semaphore ID; returns NULL in the case of an error.
  925. * @since 1.0
  926. * @version 1.0
  927. */
  928. osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);
  929. const char *osSemaphoreGetName (osSemaphoreId_t semaphore_id);
  930. /**
  931. * @brief Acquires a token of a semaphore object.
  932. *
  933. * @param semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew.
  934. * @param timeout Indicates the timeout duration, in ticks.
  935. * @return Returns the CMSIS-RTOS running result.
  936. * @since 1.0
  937. * @version 1.0
  938. */
  939. osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout);
  940. /**
  941. * @brief Releases a token of a semaphore object.
  942. *
  943. * @param semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew.
  944. * @return Returns the CMSIS-RTOS running result.
  945. * @since 1.0
  946. * @version 1.0
  947. */
  948. osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id);
  949. /**
  950. * @brief Obtains the number of available tokens of a semaphore object.
  951. *
  952. * @param semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew.
  953. * @return Returns the number of available tokens.
  954. * @since 1.0
  955. * @version 1.0
  956. */
  957. uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id);
  958. /**
  959. * @brief Deletes a semaphore object.
  960. *
  961. * @param semaphore_id Indicates the semaphore ID, which is obtained using osSemaphoreNew.
  962. * @return Returns the CMSIS-RTOS running result.
  963. * @since 1.0
  964. * @version 1.0
  965. */
  966. osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id);
  967. // ==== Memory Pool Management Functions ====
  968. osMemoryPoolId_t osMemoryPoolNew (uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr);
  969. const char *osMemoryPoolGetName (osMemoryPoolId_t mp_id);
  970. void *osMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout);
  971. osStatus_t osMemoryPoolFree (osMemoryPoolId_t mp_id, void *block);
  972. uint32_t osMemoryPoolGetCapacity (osMemoryPoolId_t mp_id);
  973. uint32_t osMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id);
  974. uint32_t osMemoryPoolGetCount (osMemoryPoolId_t mp_id);
  975. uint32_t osMemoryPoolGetSpace (osMemoryPoolId_t mp_id);
  976. osStatus_t osMemoryPoolDelete (osMemoryPoolId_t mp_id);
  977. // ==== Message Queue Management Functions ====
  978. /**
  979. * @brief Creates and initializes a message queue.
  980. *
  981. * @param msg_count Indicates the number of messages in the message queue.
  982. * @param msg_size Indicates the size of messages in the message queue.
  983. * @param attr Indicates the pointer to the message queue attributes. This parameter is not used.
  984. * @return Returns the message queue ID; returns NULL in the case of an error.
  985. * @since 1.0
  986. * @version 1.0
  987. */
  988. osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
  989. const char *osMessageQueueGetName (osMessageQueueId_t mq_id);
  990. /**
  991. * @brief Places a message in a message queue.
  992. *
  993. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  994. * @param msg_ptr Indicates the pointer to the buffer for storing the message to be placed in the message queue.
  995. * @param msg_prio Indicates the priority of the message to be placed in the message queue. This parameter is not used.
  996. * @param timeout Indicates the timeout duration.
  997. * @return Returns the CMSIS-RTOS running result.
  998. * @since 1.0
  999. * @version 1.0
  1000. */
  1001. osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);
  1002. /**
  1003. * @brief Obtains a message in a message queue.
  1004. *
  1005. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1006. * @param msg_ptr Indicates the pointer to the buffer for storing the message to be retrieved from the message queue.
  1007. * @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.
  1008. * @param timeout Indicates the timeout duration.
  1009. * @return Returns the CMSIS-RTOS running result.
  1010. * @since 1.0
  1011. * @version 1.0
  1012. */
  1013. osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
  1014. /**
  1015. * @brief Obtains the maximum number of messages that can be placed in a message queue.
  1016. *
  1017. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1018. * @return Returns the maximum number.
  1019. * @since 1.0
  1020. * @version 1.0
  1021. */
  1022. uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id);
  1023. /**
  1024. * @brief Obtains the maximum size of messages that can be placed in a message queue.
  1025. *
  1026. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1027. * @return Returns the maximum message size.
  1028. * @since 1.0
  1029. * @version 1.0
  1030. */
  1031. uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id);
  1032. /**
  1033. * @brief Obtains the number of queued messages in a message queue.
  1034. *
  1035. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1036. * @return Returns the number of queued messages.
  1037. * @since 1.0
  1038. * @version 1.0
  1039. */
  1040. uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id);
  1041. /**
  1042. * @brief Obtains the number of available slots for messages in a message queue.
  1043. *
  1044. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1045. * @return Returns the number of available slots for messages.
  1046. * @since 1.0
  1047. * @version 1.0
  1048. */
  1049. uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id);
  1050. osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id);
  1051. /**
  1052. * @brief Deletes a message queue.
  1053. *
  1054. * @param mq_id Indicates the message queue ID, which is obtained using osMessageQueueNew.
  1055. * @return Returns the CMSIS-RTOS running result.
  1056. * @since 1.0
  1057. * @version 1.0
  1058. */
  1059. osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id);
  1060. #ifdef __cplusplus
  1061. }
  1062. #endif
  1063. #endif // CMSIS_OS2_H_

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/288184
推荐阅读
相关标签
  

闽ICP备14008679号