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