yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef MK_MAC_H_
#define MK_MAC_H_
#include "mk_common.h"
 
/**
 * @addtogroup MK8000_MAC
 * @{
 */
 
/** UWB TX success */
#define UWB_TX_OK (0)
/** UWB RX success */
#define UWB_RX_OK (0)
/** UWB TX error */
#define UWB_TX_ERR (1 << 0)
 
/**
 * UWB RX error code description:
 *
 * 16bxxxx_xxxx_xxxx_xx10 : PHY payload decode error
 * 16bxxxx_xxxx_xxxx_x100 : PHY header error
 * 16bxxxx_xxxx_xxxx_1000 : SFD error
 * 16bxxxx_xxxx_xxx1_0000 : BD error
 * 16bxxxx_xxxx_xx10_0000 : RX timeout
 * 16bxxxx_xxxx_x100_0000 : STS error
 *
 */
/** UWB RX PHY payload error */
#define UWB_PLD_ERR (1 << 1)
/** UWB RX PHY header error */
#define UWB_PHR_ERR (1 << 2)
/** UWB RX SFD error */
#define UWB_SFD_ERR (1 << 3)
/** UWB RX Preamble detection error */
#define UWB_BD_ERR (1 << 4)
/** UWB RX timeout error */
#define UWB_TO_ERR (1 << 5)
/** UWB RX STS error */
#define UWB_STS_ERR (1 << 6)
 
/** UWB RX MAC HW and SW conflict in use AES */
#define UWB_MAC_AES_CONFLICT_ERR (1 << 9)
/** UWB RX MAC frame auxiliary security header error */
#define UWB_MAC_AUX_SEC_HDR_ERR (1 << 10)
/** UWB RX PHY error */
#define UWB_PHY_ERR (1 << 11)
/** UWB RX MAC frame filter error */
#define UWB_MAC_FILER_ERR (1 << 12)
/** UWB RX MAC frame format error */
#define UWB_MAC_FRAME_ERR (1 << 13)
/** UWB RX MAC frame decryption error */
#define UWB_MAC_DEC_ERR (1 << 14)
/** UWB RX MAC config mismatch error */
#define UWB_MAC_CFG_ERR (1 << 15)
 
/** Key table length */
#define UNICAST_KEY_TABLE_LEN (16)
 
/// 2 Octets short MAC address
#define MAC_ADDR_SHORT (0x00)
/// 8 Octets long MAC address
#define MAC_ADDR_LONG (0x01)
 
/** MAC event id enumeration */
enum EVT_ID_T
{
    EVT_ID0 = 0, /*!< 0: TX+IFS+GT, NO ACK Req*/
    EVT_ID1 = 1, /*!< 1: TX+AIFS+RX_ACK, ACK Req*/
    EVT_ID2 = 2, /*!< 2: Beacon Rx*/
    EVT_ID3 = 3  /*!< 3: RX+XIFS+TX_ACK+IFS+GT, general Rx*/
};
 
/** MAC event start mode enumeration */
enum EVT_MODE_T
{
    EVT_MODE_MAC_ASAP_PHY_FIX = 0, /*!< PHY TRx at specified time */
    EVT_MODE_MAC_FIX_PHY_ASAP = 1, /*!< CSMA Tx only */
    EVT_MODE_MAC_PHY_ASAP = 2,     /*!< MAC&PHY   TRx ASAP */
    EVT_MODE_MAC_DLY_PHY_ASAP = 3  /*!< MAC execute the delay opertion */
};
 
/** MAC report data structure */
struct MAC_HW_REPORT_T
{
    uint32_t phy_header;
    uint32_t timestamp;
    uint8_t tadv;
    uint8_t pid;
    uint8_t ds2phase;
    uint8_t ds1phase;
    int32_t frac_drift;
    int8_t int_drift;
    int8_t rssi;
    int8_t snr;
    uint16_t err_code;
    uint16_t pkt_len;
    uint8_t *pkt_data;
    uint8_t *next_data;
    uint32_t *next_et;
};
 
/** Key */
struct KEY_T
{
    uint32_t KEY_W0;
    uint32_t KEY_W1;
    uint32_t KEY_W2;
    uint32_t KEY_W3;
};
 
/** Key table */
struct KEY_TABLE_T
{
    struct KEY_T group_key;
    struct KEY_T unicast_key[UNICAST_KEY_TABLE_LEN];
};
 
/** Key table index enumeration */
enum KEY_IDX
{
    KEY_IDX_0 = 0, /*!< group key */
    KEY_IDX_1 = 1, /*!< unicast key below */
    KEY_IDX_2 = 2,
    KEY_IDX_3 = 3,
    KEY_IDX_4 = 4,
    KEY_IDX_5 = 5,
    KEY_IDX_6 = 6,
    KEY_IDX_7 = 7,
    KEY_IDX_8 = 8,
    KEY_IDX_9 = 9,
    KEY_IDX_10 = 10,
    KEY_IDX_11 = 11,
    KEY_IDX_12 = 12,
    KEY_IDX_13 = 13,
    KEY_IDX_14 = 14,
    KEY_IDX_15 = 15,
    KEY_IDX_16 = 16,
    KEY_IDX_MAX
};
 
union MAC_ADDR_T
{
    uint8_t byte[8];
    uint32_t word[2];
};
 
#ifdef __cplusplus
extern "C" {
#endif
 
/** MAC callback function type definition */
typedef void (*mac_callback_t)(struct MAC_HW_REPORT_T *hw_report);
 
/**
 * @brief Initialize MAC.
 * @param[in] priority          MAC interrupt priority @ref IRQ_PRIORITY_LEVEL_T
 * @param[in] tx_evt_num        TX event number
 * @param[in] rx_evt_num        RX event number
 * @param[in] max_payload_size  Maximun payload size
 * @return 0 represent Initialize successfully
 */
int mac_init(enum IRQ_PRIORITY_LEVEL_T priority, uint8_t tx_evt_num, uint8_t rx_evt_num, uint16_t max_payload_size);
 
/**
 * @brief Configure MAC CRC mode.
 * @param[in] mode               1: CRC 32, 0: CRC 16
 */
void mac_crc_mode_configure(uint8_t mode);
 
/**
 * @brief Set IFS
 * @param[in] mode              1: Long IFS, 0: Short IFS
 */
void mac_ifs_set(uint8_t mode);
 
/**
 * @brief Enable/Disable RSSI calculation in MAC interrupt handler, the RSSI value will be reported in MAC_HW_REPORT_T
 * @param[in] en                1: Enable, 0: Disable
 */
void mac_rssi_calculation_en(uint8_t en);
 
/**
 * @brief Program the MAC to transmit UWB packet.
 * @param[in] mode              Event mode @ref EVT_MODE_T
 * @param[in] tgt_time          Target time to send packet
 * @param[in] pending           Pending to execute flag, need call mac_resume from software to trigger the hardware MAC to execute the event
 * @param[in] pkt_data          Pointer of packet data to be sent
 * @param[in] pkt_len           Packet length
 * @return programmed TX event number, the maximum TX event number depends on configuration from mac_init()
 */
int mac_tx(enum EVT_MODE_T mode, uint32_t tgt_time, uint8_t pending, uint8_t *pkt_data, uint16_t pkt_len);
 
/**
 * @brief Program the MAC to receive UWB packet.
 * @param[in] mode              Event mode @ref EVT_MODE_T
 * @param[in] tgt_time          Target time to receive packet
 * @param[in] timeout           Maximum receive window
 * @return programmed RX event number, the maximum RX event number depends on configuration from mac_init()
 */
int mac_rx(enum EVT_MODE_T mode, uint32_t tgt_time, uint32_t timeout);
 
/**
 * @brief Trigger the MAC start to process programmed events.
 */
void mac_start(void);
 
/**
 * @brief Restart the MAC.
 */
void mac_restart(void);
 
/**
 * @brief Resume the pending MAC event.
 * @param[in] next_et           Pointer of next event
 */
void mac_resume(uint32_t *next_et);
 
/**
 * @brief Clear the pending MAC TX event.
 */
void mac_pending_tx_evt_clear(void);
 
/**
 * @brief Check the MAC busy state.
 * @return MAC busy state
 */
uint8_t mac_is_busy(void);
 
/**
 * @brief Discard current RX report.
 */
void mac_current_rx_report_discard(void);
 
/**
 * @brief Update MAC key table.
 * @param[in] idx               The index of key in the key table to be updated @ref KEY_IDX
 * @param[in] key               pointer to the input key data
 */
void mac_update_key_table(enum KEY_IDX idx, uint32_t key[4]);
 
/**
 * @brief Update MAC CCM key.
 * @param[in] ccm_key           Pointer of the input CCM key data
 */
void mac_update_ccm_key(uint32_t *ccm_key);
 
/**
 * @brief Get MAC TX done and RX done process handler.
 * @param[out] tx_cb            Pointer of TX done process handler
 * @param[out] rx_cb            Pointer of RX done process handler
 */
void mac_get_process_handler(mac_callback_t *tx_cb, mac_callback_t *rx_cb);
 
/**
 * @brief Register MAC TX done and RX done process handler.
 * @param[in] tx_cb             TX done process handler
 * @param[in] rx_cb             RX done process handler
 */
void mac_register_process_handler(mac_callback_t tx_cb, mac_callback_t rx_cb);
 
/**
 * @brief Get MAC CCM key and Nonce informantion, for debug purpose, this API should be called in MAC RX done process handler.
 * @param[out] ccm_key          Pointer of the CCM key buffer
 * @param[out] nonce            Pointer of the Nonce buffer
 */
void mac_ccm_key_nonce_get(uint32_t **ccm_key, uint32_t **nonce);
 
#ifdef __cplusplus
}
#endif
 
/**
 * @}
 */
 
#endif /* MK_MAC_H_ */