bsp_common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __BSP_COMMON_H__
  22. #define __BSP_COMMON_H__
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <stdarg.h>
  28. #include "cmsis_gcc.h"
  29. typedef struct
  30. {
  31. uint32_t param_max_num;
  32. uint32_t param_max_len;
  33. uint32_t param_num;
  34. int8_t *param_str;
  35. }CmdParam;
  36. typedef struct
  37. {
  38. uint8_t Sec;
  39. uint8_t Min;
  40. uint8_t Hour;
  41. uint8_t Week;//表示日期0~6,sun~sat,表示预约时,bit0~bit6,sun~sat
  42. }Time_UserDataStruct;
  43. typedef struct
  44. {
  45. uint16_t Year;
  46. uint8_t Mon;
  47. uint8_t Day;
  48. }Date_UserDataStruct;
  49. typedef union
  50. {
  51. uint32_t dwTime;
  52. Time_UserDataStruct Time;
  53. }Time_Union;
  54. typedef union
  55. {
  56. uint32_t dwDate;
  57. Date_UserDataStruct Date;
  58. }Date_Union;
  59. typedef struct
  60. {
  61. uint8_t *Data;
  62. uint32_t Len;
  63. uint32_t Offset;
  64. uint32_t MaxLength;
  65. uint32_t DataSize;
  66. }Loop_Buffer;
  67. typedef struct
  68. {
  69. uint8_t *Data;
  70. uint32_t Pos;
  71. uint32_t MaxLen;
  72. }Buffer_Struct;
  73. typedef union
  74. {
  75. void *p;
  76. char *pc8;
  77. uint8_t *pu8;
  78. uint16_t *pu16;
  79. uint32_t *pu32;
  80. uint32_t u32;
  81. uint8_t u8[4];
  82. uint16_t u16[2];
  83. }PV_Union;
  84. enum
  85. {
  86. ERROR_NONE,
  87. ERROR_NO_SUCH_ID,
  88. ERROR_PERMISSION_DENIED,
  89. ERROR_PARAM_INVALID,
  90. ERROR_PARAM_OVERFLOW,
  91. ERROR_DEVICE_BUSY,
  92. ERROR_OPERATION_FAILED,
  93. ERROR_BUFFER_FULL,
  94. ERROR_NO_MEMORY,
  95. ERROR_CMD_NOT_SUPPORT,
  96. ERROR_NO_DATA,
  97. ERROR_NO_FLASH,
  98. ERROR_NO_TIMER,
  99. ERROR_TIMEOUT,
  100. ERROR_SSL_HANDSHAKE,
  101. ERROR_PROTOCL,
  102. ERROR_ID_INVALID,
  103. ERROR_MID_INVALID,
  104. ERROR_RETRY_TOO_MUCH,
  105. ERROR_CMD_BLOCK,
  106. LIST_FIND = 1,
  107. LIST_PASS = 0,
  108. LIST_DEL = -1,
  109. DMA_CB_DONE = 0,
  110. UART_CB_TX_BUFFER_DONE,
  111. UART_CB_TX_ALL_DONE,
  112. UART_CB_RX_NEW,
  113. UART_CB_RX_TIMEOUT,
  114. UART_CB_RX_BUFFER_FULL,
  115. UART_CB_ERROR,
  116. UART_CB_CONNECTED, //串口工具对方已经打开
  117. DMA_CB_ERROR = 0xffffffff,
  118. CORE_EVENT_ID_START = 0,
  119. CORE_EVENT_ID_ANY = 0,
  120. CORE_EVENT_TIMEOUT,
  121. CORE_TIMER_TIMEOUT = 0x00010000,
  122. SERVICE_EVENT_ID_START = 0x00100000,
  123. USER_EVENT_ID_START = 0x10000000,
  124. INVALID_EVENT_ID = 0xffffffff,
  125. };
  126. #define INVALID_HANDLE_VALUE ((void *)0xffffffff)
  127. #define INVALID_PARAM (0xffffffff)
  128. #define CRC32_GEN (0x04C11DB7)
  129. #define CRC32_START (0xffffffff)
  130. #define CRC16_CCITT_GEN (0x1021)
  131. #define CRC16_MODBUS_GEN (0x8005)
  132. #define CRC16_START (0xffff)
  133. #define CRC16_IBM_SEED (0xffff)
  134. #define CRC16_CCITT_SEED (0x1D0F)
  135. #define HANDLE void *
  136. #define MIN(X,Y) (((X) < (Y))?(X):(Y))
  137. typedef void (* TaskFun_t)( void * );
  138. typedef void(* CBDataFun_t)(uint8_t *Data, uint32_t Len);
  139. typedef int32_t(*CBFuncEx_t)(void *pData, void *pParam);
  140. typedef uint64_t LongInt;
  141. typedef struct
  142. {
  143. CBFuncEx_t CB;
  144. union {
  145. void *pParam; //用户回调模式
  146. uint32_t MaxCnt; //设置捕获模式时的最大tick,捕获时的tick
  147. }uParam;
  148. union {
  149. struct {
  150. uint8_t Level; //IO输入输出电平,捕获模式下中断时IO电平
  151. uint8_t PullMode; //IO上下拉控制
  152. } IOArg;
  153. struct {
  154. uint8_t ExtiMode; //中断模式
  155. uint8_t PullMode; //IO上下拉控制
  156. } ExitArg;
  157. uint16_t Time; //delay时间,us
  158. } uArg;
  159. uint8_t Operation; //操作类型
  160. uint8_t Arg1; //IO操作时为IOpin,delay操作时则为微调值,0~47,48为1us
  161. }OPQueue_CmdStruct;
  162. __attribute__((weak)) uint8_t OS_CheckInIrq(void);
  163. #ifdef __BUILD_OS__
  164. HANDLE OS_MutexCreate(void);
  165. void OS_MutexLock(HANDLE Sem);
  166. int32_t OS_MutexLockWtihTime(HANDLE Sem, uint32_t TimeoutMs);
  167. HANDLE OS_MutexRelease(HANDLE Sem);
  168. #endif
  169. uint32_t OS_EnterCritical(void);
  170. void OS_ExitCritical(uint32_t Critical);
  171. void *OS_Malloc(uint32_t Size);
  172. void *OS_Zalloc(uint32_t Size);
  173. void OS_Free(void *p);
  174. void *OS_Realloc(void *buf, uint32_t size);
  175. void OS_MemInfo(uint32_t *curalloc, uint32_t *totfree, uint32_t *maxfree);
  176. int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size);
  177. void OS_DeInitBuffer(Buffer_Struct *Buf);
  178. int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size);
  179. int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size);
  180. int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
  181. int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len);
  182. void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len);
  183. void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen);
  184. int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
  185. void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len);
  186. void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize);
  187. uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len);
  188. uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len);
  189. void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len);
  190. uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len);
  191. int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value);
  192. int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value);
  193. uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn);
  194. uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart);
  195. uint8_t SumCheck(uint8_t *Data, uint32_t Len);
  196. uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse);
  197. uint32_t AsciiToU32(uint8_t *Src, uint32_t Len);
  198. void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen);
  199. uint32_t CRC32_Cal(uint32_t * CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last);
  200. uint32_t CmdParseParam(int8_t* pStr, CmdParam *CmdParam, int8_t Cut);
  201. uint8_t IsLeapYear(uint32_t Year);
  202. LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time);
  203. uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay);
  204. /*
  205. * 转义解包
  206. * 标识Flag,即包头包尾加入Flag
  207. * 数据中遇到Code F1 -> Flag
  208. * 数据中遇到Code F2 -> Code
  209. * 数据中遇到Flag 出错返回0
  210. */
  211. uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf);
  212. /*
  213. * llist相关代码,大部分来自linux内核
  214. */
  215. /**
  216. * container_of - cast a member of a structure out to the containing structure
  217. *
  218. * @ptr: the pointer to the member.
  219. * @type: the type of the container struct this is embedded in.
  220. * @member: the name of the member within the struct.
  221. *
  222. */
  223. #define container_of(ptr, type, member) ({ \
  224. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  225. (type *)( (char *)__mptr - offsetof(type,member) );})
  226. /*
  227. * These are non-NULL pointers that will result in page faults
  228. * under normal circumstances, used to verify that nobody uses
  229. * non-initialized llist entries.
  230. */
  231. #define LLIST_POISON1 (0)
  232. #define LLIST_POISON2 (0)
  233. /*
  234. * Simple doubly linked llist implementation.
  235. *
  236. * Some of the internal functions ("__xxx") are useful when
  237. * manipulating whole llists rather than single entries, as
  238. * sometimes we already know the next/prev entries and we can
  239. * generate better code by using them directly rather than
  240. * using the generic single-entry routines.
  241. */
  242. typedef struct llist_head_t{
  243. struct llist_head_t *next, *prev;
  244. }llist_head;
  245. #define LLIST_HEAD_INIT(name) { &(name), &(name) }
  246. #define LLIST_HEAD(name) \
  247. llist_head name = LLIST_HEAD_INIT(name)
  248. #define INIT_LLIST_HEAD(ptr) do { \
  249. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  250. } while (0)
  251. /*
  252. * Insert a new entry between two known consecutive entries.
  253. *
  254. * This is only for internal llist manipulation where we know
  255. * the prev/next entries already!
  256. */
  257. void __llist_add(llist_head *p,
  258. llist_head *prev,
  259. llist_head *next);
  260. /**
  261. * llist_add - add a new entry
  262. * @new: new entry to be added
  263. * @head: llist head to add it after
  264. *
  265. * Insert a new entry after the specified head.
  266. * This is good for implementing stacks.
  267. */
  268. void llist_add(llist_head *p, llist_head *head);
  269. /**
  270. * llist_add_tail - add a new entry
  271. * @new: new entry to be added
  272. * @head: llist head to add it before
  273. *
  274. * Insert a new entry before the specified head.
  275. * This is useful for implementing queues.
  276. */
  277. void llist_add_tail(llist_head *p, llist_head *head);
  278. /*
  279. * Delete a llist entry by making the prev/next entries
  280. * point to each other.
  281. *
  282. * This is only for internal llist manipulation where we know
  283. * the prev/next entries already!
  284. */
  285. void __llist_del(llist_head * prev, llist_head * next);
  286. /**
  287. * llist_del - deletes entry from llist.
  288. * @entry: the element to delete from the llist.
  289. * Note: llist_empty on entry does not return true after this, the entry is
  290. * in an undefined state.
  291. */
  292. void llist_del(llist_head *entry);
  293. /**
  294. * llist_del_init - deletes entry from llist and reinitialize it.
  295. * @entry: the element to delete from the llist.
  296. */
  297. void llist_del_init(llist_head *entry);
  298. /**
  299. * llist_move - delete from one llist and add as another's head
  300. * @llist: the entry to move
  301. * @head: the head that will precede our entry
  302. */
  303. void llist_move(llist_head *llist, llist_head *head);
  304. /**
  305. * llist_move_tail - delete from one llist and add as another's tail
  306. * @llist: the entry to move
  307. * @head: the head that will follow our entry
  308. */
  309. void llist_move_tail(llist_head *llist,
  310. llist_head *head);
  311. /**
  312. * llist_empty - tests whether a llist is empty
  313. * @head: the llist to test.
  314. */
  315. int llist_empty(const llist_head *head);
  316. uint32_t llist_num(const llist_head *head);
  317. void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData);
  318. /**
  319. * llist_entry - get the struct for this entry
  320. * @ptr: the &llist_head pointer.
  321. * @type: the type of the struct this is embedded in.
  322. * @member: the name of the llist_struct within the struct.
  323. */
  324. #define llist_entry(ptr, type, member) \
  325. container_of(ptr, type, member)
  326. uint8_t BytesGet8(const void *ptr);
  327. void BytesPut8(void *ptr, uint8_t v);
  328. uint16_t BytesGetBe16(const void *ptr);
  329. void BytesPutBe16(void *ptr, uint16_t v);
  330. uint32_t BytesGetBe32(const void *ptr);
  331. void BytesPutBe32(void *ptr, uint32_t v);
  332. uint16_t BytesGetLe16(const void *ptr);
  333. void BytesPutLe16(void *ptr, uint16_t v);
  334. uint32_t BytesGetLe32(const void *ptr);
  335. void BytesPutLe32(void *ptr, uint32_t v);
  336. uint64_t BytesGetLe64(const void *ptr);
  337. void BytesPutLe64(void *ptr, uint64_t v);
  338. uint8_t BytesGet8FromBuf(Buffer_Struct *Buf);
  339. void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v);
  340. uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf);
  341. void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v);
  342. uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf);
  343. void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v);
  344. uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf);
  345. void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v);
  346. uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf);
  347. void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v);
  348. uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf);
  349. void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v);
  350. float BytesGetFloatFromBuf(Buffer_Struct *Buf);
  351. void BytesPutFloatToBuf(Buffer_Struct *Buf, float v);
  352. double BytesGetDoubleFromBuf(Buffer_Struct *Buf);
  353. void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v);
  354. /*************************************************************************/
  355. #define malloc OS_Malloc
  356. #define free OS_Free
  357. #define realloc OS_Realloc
  358. #define zalloc OS_Zalloc
  359. #define calloc OS_Calloc
  360. #endif