chen
2024-11-08 cc432b761c884a0bd8e9d83db0a4e26109fc08b1
keil/include/drivers/mk_mac.h
对比新文件
@@ -0,0 +1,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_ */