在网上看到per_counter这个开源项目,里边有一个宏这么用的。
- int32_t stop_cycle_counter(void)
- {
- int32_t nTemp = 0;
- safe_atom_code(){
- nTemp = check_systick() + s_nCycleCounts;
- }
- return nTemp - s_nOffset;
- }
复制代码 这个safe_atom_code(){} 应该是防止被打断的,但是我看了.h中的宏,没看懂。求教大神给发发。
- /*============================ MACROS ========================================*/
- #define __PLOOC_VA_NUM_ARGS_IMPL( _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12, \
- _13,_14,_15,_16,__N,...) __N
- #define __PLOOC_VA_NUM_ARGS(...) \
- __PLOOC_VA_NUM_ARGS_IMPL( 0,##__VA_ARGS__,16,15,14,13,12,11,10,9, \
- 8,7,6,5,4,3,2,1,0)
-
- #define __CONNECT2(__A, __B) __A##__B
- #define __CONNECT3(__A, __B, __C) __A##__B##__C
- #define __CONNECT4(__A, __B, __C, __D) __A##__B##__C##__D
- #define __CONNECT5(__A, __B, __C, __D, __E) __A##__B##__C##__D##__E
- #define __CONNECT6(__A, __B, __C, __D, __E, __F) __A##__B##__C##__D##__E##__F
- #define __CONNECT7(__A, __B, __C, __D, __E, __F, __G) \
- __A##__B##__C##__D##__E##__F##__G
- #define __CONNECT8(__A, __B, __C, __D, __E, __F, __G, __H) \
- __A##__B##__C##__D##__E##__F##__G##__H
- #define __CONNECT9(__A, __B, __C, __D, __E, __F, __G, __H, __I) \
- __A##__B##__C##__D##__E##__F##__G##__H##__I
-
- #define CONNECT2(__A, __B) __CONNECT2(__A, __B)
- #define CONNECT3(__A, __B, __C) __CONNECT3(__A, __B, __C)
- #define CONNECT4(__A, __B, __C, __D) __CONNECT4(__A, __B, __C, __D)
- #define CONNECT5(__A, __B, __C, __D, __E) __CONNECT5(__A, __B, __C, __D, __E)
- #define CONNECT6(__A, __B, __C, __D, __E, __F) \
- __CONNECT6(__A, __B, __C, __D, __E, __F)
- #define CONNECT7(__A, __B, __C, __D, __E, __F, __G) \
- __CONNECT7(__A, __B, __C, __D, __E, __F, __G)
- #define CONNECT8(__A, __B, __C, __D, __E, __F, __G, __H) \
- __CONNECT8(__A, __B, __C, __D, __E, __F, __G, __H)
- #define CONNECT9(__A, __B, __C, __D, __E, __F, __G, __H, __I) \
- __CONNECT9(__A, __B, __C, __D, __E, __F, __G, __H, __I)
-
- #define CONNECT(...) \
- CONNECT2(CONNECT, __PLOOC_VA_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
-
-
- #define __using1(__declare) \
- for (__declare, *CONNECT3(__using_, __LINE__,_ptr) = NULL; \
- CONNECT3(__using_, __LINE__,_ptr)++ == NULL; \
- )
- #define __using2(__declare, __on_leave_expr) \
- for (__declare, *CONNECT3(__using_, __LINE__,_ptr) = NULL; \
- CONNECT3(__using_, __LINE__,_ptr)++ == NULL; \
- __on_leave_expr \
- )
- #define __using3(__declare, __on_enter_expr, __on_leave_expr) \
- for (__declare, *CONNECT3(__using_, __LINE__,_ptr) = NULL; \
- CONNECT3(__using_, __LINE__,_ptr)++ == NULL ? \
- ((__on_enter_expr),1) : 0; \
- __on_leave_expr \
- )
-
- #define __using4(__dcl1, __dcl2, __on_enter_expr, __on_leave_expr) \
- for (__dcl1, __dcl2, *CONNECT3(__using_, __LINE__,_ptr) = NULL; \
- CONNECT3(__using_, __LINE__,_ptr)++ == NULL ? \
- ((__on_enter_expr),1) : 0; \
- __on_leave_expr \
- )
-
- #define using(...) \
- CONNECT2(__using, __PLOOC_VA_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
- #define safe_atom_code() \
- using( uint32_t CONNECT2(temp,__LINE__) = __disable_irq(), \
- __set_PRIMASK(CONNECT2(temp,__LINE__)))
- #define __with2(__type, __addr) \
- using(__type *_p=(__addr))
- #define __with3(__type, __addr, __item) \
- using(__type *_p=(__addr), *__item = _p, _p=_p, )
- #define with(...) \
- CONNECT2(__with, __PLOOC_VA_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
- #undef _
- #define _ (*_p)
- #ifndef dimof
- # define dimof(__array) (sizeof(__array)/sizeof(__array[0]))
- #endif
- #define foreach2(__type, __array) \
- using(__type *_p = __array) \
- for ( uint_fast32_t CONNECT2(count,__LINE__) = dimof(__array); \
- CONNECT2(count,__LINE__) > 0; \
- _p++, CONNECT2(count,__LINE__)-- \
- )
- #define foreach3(__type, __array, __item) \
- using(__type *_p = __array, *__item = _p, _p = _p, ) \
- for ( uint_fast32_t CONNECT2(count,__LINE__) = dimof(__array); \
- CONNECT2(count,__LINE__) > 0; \
- _p++, __item = _p, CONNECT2(count,__LINE__)-- \
- )
- #define foreach(...) \
- CONNECT2(foreach, __PLOOC_VA_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
- #ifndef safe_atom_code
- # define safe_atom_code() \
- using( uint32_t CONNECT2(temp,__LINE__) = __disable_irq(), \
- __set_PRIMASK(CONNECT2(temp,__LINE__)))
- #endif
复制代码
|