1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #ifndef ZB_BUFPOOL_H 19 #define ZB_BUFPOOL_H 1 20 21 #include "tl_common.h" 22 23 /*! \addtogroup buf */ 24 /*! @{ */ 25 26 /** 27 Packet buffer header. 28 */ 29 30 typedef struct { 31 u8 id; // primitive id 32 u8 handle; 33 s8 rssi; 34 u8 used : 1; 35 u8 macTxFifo : 1; 36 u8 mgmtLeave : 1; 37 u8 active : 1; // only for mac command buffer 38 u8 pending : 1; // only for endDev, if parent has pending data for me 39 u8 resvHdr : 3; 40 } zb_buf_hdr_t; // 4 41 42 /** 43 Packet buffer 44 */ 45 #define ZB_BUFFER_DEBUG 0 46 47 #define TL_RXPRIMITIVEHDR 32 // To use the in buffer as RX buffer, needs to preallocate primitive fields 48 49 #define ZB_BUF_SIZE (160 + TL_RXPRIMITIVEHDR) 50 51 #ifndef ZB_BUF_POOL_NUM 52 #if defined(MCU_CORE_8258) || defined(MCU_CORE_8278) 53 #define ZB_BUF_POOL_NUM 36 54 #else 55 #ifdef CHIP_8269 56 #define ZB_BUF_POOL_NUM 36 57 #else 58 #define ZB_BUF_POOL_NUM 12 59 #endif 60 #endif 61 #endif 62 63 typedef struct { 64 u16 allocLine; 65 u16 freeLine; 66 u8 handler; 67 u8 nlmeStatus; 68 u8 id; 69 u8 resv; 70 } zb_buf_allocInfo_t; 71 72 #if ZB_BUFFER_DEBUG 73 #define ZB_BUFF_DBG_NUM 16 74 #endif 75 76 typedef struct zb_buf_s { 77 u8 buf[ZB_BUF_SIZE]; 78 zb_buf_hdr_t hdr; 79 struct zb_buf_s *next; 80 u32 allocCnt; 81 u32 freeCnt; 82 #if ZB_BUFFER_DEBUG 83 zb_buf_allocInfo_t allocInfo[ZB_BUFF_DBG_NUM]; 84 #endif 85 } zb_buf_t; 86 87 typedef struct { 88 zb_buf_t *head; 89 u32 usedNum; 90 #if ZB_BUFFER_DEBUG 91 u32 rsv_fill[2]; 92 #endif 93 zb_buf_t pool[ZB_BUF_POOL_NUM]; // shall be allocated at the last field in the structure of the zb_buf_pool_t 94 } zb_buf_pool_t; 95 96 extern u8 ZB_BUF_POOL_SIZE; 97 extern zb_buf_pool_t g_mPool; 98 99 void *tl_bufInitalloc(zb_buf_t *p, u8 size); 100 #define TL_BUF_INITIAL_ALLOC(p, size, ptr, type) (ptr) = (type)tl_bufInitalloc((p), (size)) 101 102 #define TL_COPY_BUF(dst, src) \ 103 do { \ 104 memcpy((dst), (src), ZB_BUF_SIZE + sizeof(zb_buf_hdr_t) - 1); \ 105 } while (0) 106 107 void zb_buf_clear(zb_buf_t *p); 108 109 bool is_zb_buf(void *p); 110 111 /* 112 * buffer(only used in zigbee stack) initialization 113 * 114 * */ 115 extern void tl_zbBufferInit(void); 116 117 extern u8 *tl_phyRxBufTozbBuf(u8 *p); 118 119 extern u8 *tl_zbBufToPhyRxBuf(u8 *p); 120 121 #define TL_RXBUF_TO_INBUF(p) tl_phyRxBufTozbBuf(p) 122 #define TL_INBUF_TO_RXBUF(p) tl_zbBufToPhyRxBuf(p) 123 124 u8 *tl_getRxBuf(void); 125 126 #if ZB_BUFFER_DEBUG 127 #define zb_buf_allocate(void) my_zb_buf_allocate(__LINE__) 128 #else 129 zb_buf_t *zb_buf_allocate(void); 130 #endif 131 132 #if ZB_BUFFER_DEBUG 133 #define zb_buf_free(x) my_zb_buf_free(x, __LINE__) 134 #else 135 u8 zb_buf_free(zb_buf_t *buf); 136 #endif 137 138 #define ZB_BUF_FROM_REF(ref) (&g_mPool.pool[ref]) 139 140 #define ZB_REF_FROM_BUF(p) ((p) - &g_mPool.pool[0]) 141 142 extern u8 RX_ZBBUF_OFFSET; 143 /*! @} */ 144 145 #endif /* ZB_BUFPOOL_H */ 146