diff --git a/README.md b/README.md index 3b28cb2..b944bd3 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ * 08. task008 对SW1进行上é”�/è§£é”�功能:通过判断SW1点击状æ€�进行LEDé—ªçƒ�或者SW1上é”�/è§£é”�。 * 09. homework 点击SW1改å�˜LED状æ€� * 10. task010 一阶段考核:通过SW1对æµ�æ°´ç�¯è¿›è¡Œæ“�作,当按下SW1æ—¶æš‚å�œæµ�æ°´ç�¯å¹¶ä¿�æŒ�当å‰�状æ€�,当å†�次按下时在上次ä¿�存状æ€�开始继续å�‘下执行。 +* 11. Zigbee无线通信 Aã€�B两æ�¿ç›¸äº’控制LED diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.c new file mode 100644 index 0000000..b41d6f7 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.c @@ -0,0 +1,523 @@ +//------------------------------------------------------------------- +// Filename: basic_rf.c +// Description: Basic RF library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_mcu.h" // Using halMcuWaitUs() +#include "hal_rf.h" +#include "basic_rf.h" +#ifdef SECURITY_CCM + #include "hal_rf_security.h" + #include "basic_rf_security.h" +#endif +#include + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Packet and packet part lengths +#define PKT_LEN_MIC 8 +#define PKT_LEN_SEC PKT_LEN_UNSEC + PKT_LEN_MIC +#define PKT_LEN_AUTH 8 +#define PKT_LEN_ENCR 24 + +// Packet overhead ((frame control field, sequence number, PAN ID, +// destination and source) + (footer)) +// Note that the length byte itself is not included included in the packet length +#define BASIC_RF_PACKET_OVERHEAD_SIZE ((2 + 1 + 2 + 2 + 2) + (2)) +#define BASIC_RF_MAX_PAYLOAD_SIZE (127 - BASIC_RF_PACKET_OVERHEAD_SIZE - BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_LEN_MIC) +#define BASIC_RF_ACK_PACKET_SIZE 5 +#define BASIC_RF_FOOTER_SIZE 2 +#define BASIC_RF_HDR_SIZE 10 + +// The time it takes for the acknowledgment packet to be received after the +// data packet has been transmitted. +#define BASIC_RF_ACK_DURATION (0.5 * 32 * 2 * ((4 + 1) + (1) + (2 + 1) + (2))) +#define BASIC_RF_SYMBOL_DURATION (32 * 0.5) + +// The length byte +#define BASIC_RF_PLD_LEN_MASK 0x7F + +// Frame control field +#define BASIC_RF_FCF_NOACK 0x8841 +#define BASIC_RF_FCF_ACK 0x8861 +#define BASIC_RF_FCF_ACK_BM 0x0020 +#define BASIC_RF_FCF_BM (~BASIC_RF_FCF_ACK_BM) +#define BASIC_RF_SEC_ENABLED_FCF_BM 0x0008 + +// Frame control field LSB +#define BASIC_RF_FCF_NOACK_L LO_UINT16(BASIC_RF_FCF_NOACK) +#define BASIC_RF_FCF_ACK_L LO_UINT16(BASIC_RF_FCF_ACK) +#define BASIC_RF_FCF_ACK_BM_L LO_UINT16(BASIC_RF_FCF_ACK_BM) +#define BASIC_RF_FCF_BM_L LO_UINT16(BASIC_RF_FCF_BM) +#define BASIC_RF_SEC_ENABLED_FCF_BM_L LO_UINT16(BASIC_RF_SEC_ENABLED_FCF_BM) + +// Auxiliary Security header +#define BASIC_RF_AUX_HDR_LENGTH 5 +#define BASIC_RF_LEN_AUTH BASIC_RF_PACKET_OVERHEAD_SIZE + BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_FOOTER_SIZE +#define BASIC_RF_SECURITY_M 2 +#define BASIC_RF_LEN_MIC 8 +#ifdef SECURITY_CCM + #undef BASIC_RF_HDR_SIZE + #define BASIC_RF_HDR_SIZE 15 +#endif + +// Footer +#define BASIC_RF_CRC_OK_BM 0x80 + + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// The receive struct +typedef struct +{ + uint8 seqNumber; + uint16 srcAddr; + uint16 srcPanId; + int8 length; + uint8 *pPayload; + uint8 ackRequest; + int8 rssi; + volatile uint8 isReady; + uint8 status; +} basicRfRxInfo_t; + +// Tx state +typedef struct +{ + uint8 txSeqNumber; + volatile uint8 ackReceived; + uint8 receiveOn; + uint32 frameCounter; +} basicRfTxState_t; + + +// Basic RF packet header (IEEE 802.15.4) +typedef struct +{ + uint8 packetLength; + uint8 fcf0; // Frame control field LSB + uint8 fcf1; // Frame control field MSB + uint8 seqNumber; + uint16 panId; + uint16 destAddr; + uint16 srcAddr; + #ifdef SECURITY_CCM + uint8 securityControl; + uint8 frameCounter[4]; + #endif +} basicRfPktHdr_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +static basicRfRxInfo_t rxi = +{ + 0xFF +}; // Make sure sequence numbers are +static basicRfTxState_t txState = +{ + 0x00 +}; // initialised and distinct. + +static basicRfCfg_t *pConfig; +static uint8 txMpdu[BASIC_RF_MAX_PAYLOAD_SIZE + BASIC_RF_PACKET_OVERHEAD_SIZE + 1]; +static uint8 rxMpdu[128]; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfBuildHeader +// @brief Builds packet header according to IEEE 802.15.4 frame format +// @param buffer - Pointer to buffer to write the header +// destAddr - destination short address +// payloadLength - length of higher layer payload +// @return uint8 - length of header +//------------------------------------------------------------------- +static uint8 basicRfBuildHeader(uint8 *buffer, uint16 destAddr, uint8 payloadLength) +{ + basicRfPktHdr_t *pHdr; + uint16 fcf; + + pHdr = (basicRfPktHdr_t*)buffer; + + // Populate packet header + pHdr->packetLength = payloadLength + BASIC_RF_PACKET_OVERHEAD_SIZE; + //pHdr->frameControlField = pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK; + fcf = pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK; + pHdr->fcf0 = LO_UINT16(fcf); + pHdr->fcf1 = HI_UINT16(fcf); + pHdr->seqNumber = txState.txSeqNumber; + pHdr->panId = pConfig->panId; + pHdr->destAddr = destAddr; + pHdr->srcAddr = pConfig->myAddr; + + #ifdef SECURITY_CCM + + // Add security to FCF, length and security header + pHdr->fcf0 |= BASIC_RF_SEC_ENABLED_FCF_BM_L; + pHdr->packetLength += PKT_LEN_MIC; + pHdr->packetLength += BASIC_RF_AUX_HDR_LENGTH; + + pHdr->securityControl = SECURITY_CONTROL; + pHdr->frameCounter[0] = LO_UINT16(LO_UINT32(txState.frameCounter)); + pHdr->frameCounter[1] = HI_UINT16(LO_UINT32(txState.frameCounter)); + pHdr->frameCounter[2] = LO_UINT16(HI_UINT32(txState.frameCounter)); + pHdr->frameCounter[3] = HI_UINT16(HI_UINT32(txState.frameCounter)); + + #endif + + return BASIC_RF_HDR_SIZE; +} + +//------------------------------------------------------------------- +// @fn basicRfBuildMpdu +// @brief Builds mpdu (MAC header + payload) according to IEEE 802.15.4 +// frame format +// @param destAddr - Destination short address +// pPayload - pointer to buffer with payload +// payloadLength - length of payload buffer +// @return uint8 - length of mpdu +//------------------------------------------------------------------- +static uint8 basicRfBuildMpdu(uint16 destAddr, uint8 *pPayload, uint8 payloadLength) +{ + uint8 hdrLength, n; + + hdrLength = basicRfBuildHeader(txMpdu, destAddr, payloadLength); + + for (n = 0; n < payloadLength; n++) + { + txMpdu[hdrLength + n] = pPayload[n]; + } + return hdrLength + payloadLength; // total mpdu length +} + +//------------------------------------------------------------------- +// @fn basicRfRxFrmDoneIsr +// @brief Interrupt service routine for received frame from radio +// (either data or acknowlegdement) +// @param rxi - file scope variable info extracted from the last incoming +// frame +// txState - file scope variable that keeps tx state info +// @return none +//------------------------------------------------------------------- +static void basicRfRxFrmDoneIsr(void) +{ + basicRfPktHdr_t *pHdr; + uint8 *pStatusWord; + #ifdef SECURITY_CCM + uint8 authStatus = 0; + #endif + + // Map header to packet buffer + pHdr = (basicRfPktHdr_t*)rxMpdu; + + // Clear interrupt and disable new RX frame done interrupt + halRfDisableRxInterrupt(); + + // Enable all other interrupt sources (enables interrupt nesting) + halIntOn(); + + // Read payload length. + halRfReadRxBuf(&pHdr->packetLength, 1); + pHdr->packetLength &= BASIC_RF_PLD_LEN_MASK; // Ignore MSB + + // Is this an acknowledgment packet? + // Only ack packets may be 5 bytes in total. + if (pHdr->packetLength == BASIC_RF_ACK_PACKET_SIZE) + { + // Read the packet + halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength); + + rxi.ackRequest = !!(pHdr->fcf0 &BASIC_RF_FCF_ACK_BM_L); + + // Read the status word and check for CRC OK + pStatusWord = rxMpdu + 4; + + // Indicate the successful ACK reception if CRC and sequence number OK + if ((pStatusWord[1] &BASIC_RF_CRC_OK_BM) && (pHdr->seqNumber == txState.txSeqNumber)) + { + txState.ackReceived = TRUE; + } + // No, it is data + } + else + { + // It is assumed that the radio rejects packets with invalid length. + // Subtract the number of bytes in the frame overhead to get actual payload. + rxi.length = pHdr->packetLength - BASIC_RF_PACKET_OVERHEAD_SIZE; + + #ifdef SECURITY_CCM + rxi.length -= (BASIC_RF_AUX_HDR_LENGTH + BASIC_RF_LEN_MIC); + authStatus = halRfReadRxBufSecure(&rxMpdu[1], pHdr->packetLength, rxi.length, BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M); + #else + halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength); + #endif + + rxi.ackRequest = !!(pHdr->fcf0 &BASIC_RF_FCF_ACK_BM_L); + + // Read the source address + rxi.srcAddr = pHdr->srcAddr; + + // Read the packet payload + rxi.pPayload = rxMpdu + BASIC_RF_HDR_SIZE; + + // Read the FCS to get the RSSI and CRC + pStatusWord = rxi.pPayload + rxi.length; + #ifdef SECURITY_CCM + pStatusWord += BASIC_RF_LEN_MIC; + #endif + rxi.rssi = pStatusWord[0]; + + // Notify the application about the received data packet if the CRC is OK + // Throw packet if the previous packet had the same sequence number + if ((pStatusWord[1] &BASIC_RF_CRC_OK_BM) )//&& (rxi.seqNumber != pHdr->seqNumber)) + { + // If security is used check also that authentication passed + #ifdef SECURITY_CCM + if (authStatus == SUCCESS) + { + if ((pHdr->fcf0 &BASIC_RF_FCF_BM_L) == (BASIC_RF_FCF_NOACK_L | BASIC_RF_SEC_ENABLED_FCF_BM_L)) + { + rxi.isReady = TRUE; + } + } + #else + if (((pHdr->fcf0 &(BASIC_RF_FCF_BM_L)) == BASIC_RF_FCF_NOACK_L)) + { + rxi.isReady = TRUE; + } + #endif + } + rxi.seqNumber = pHdr->seqNumber; + } + // Enable RX frame done interrupt again + halIntOff(); + halRfEnableRxInterrupt(); +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfInit +// @brief Initialise basic RF datastructures. Sets channel, short address and +// PAN id in the chip and configures interrupt on packet reception +// @param pRfConfig - pointer to BASIC_RF_CONFIG struct. +// This struct must be allocated by higher layer +// txState - file scope variable that keeps tx state info +// rxi - file scope variable info extracted from the last incoming frame +// @return none +//------------------------------------------------------------------- +uint8 basicRfInit(basicRfCfg_t *pRfConfig) +{ + if (halRfInit() == FAILED) + { + return FAILED; + } + + halIntOff(); + + // Set the protocol configuration + pConfig = pRfConfig; + rxi.pPayload = NULL; + + txState.receiveOn = TRUE; + txState.frameCounter = 0; + + // Set channel + halRfSetChannel(pConfig->channel); + + // Write the short address and the PAN ID to the CC2520 RAM + halRfSetShortAddr(pConfig->myAddr); + halRfSetPanId(pConfig->panId); + + // if security is enabled, write key and nonce + #ifdef SECURITY_CCM + basicRfSecurityInit(pConfig); + #endif + + // Set up receive interrupt (received data or acknowlegment) + halRfRxInterruptConfig(basicRfRxFrmDoneIsr); + + halIntOn(); + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn basicRfSendPacket +// @brief Send packet +// @param destAddr - destination short address +// pPayload - pointer to payload buffer. This buffer must be +// allocated by higher layer. +// length - length of payload +// txState - file scope variable that keeps tx state info +// mpdu - file scope variable. Buffer for the frame to send +// @return basicRFStatus_t - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 basicRfSendPacket(uint16 destAddr, uint8 *pPayload, uint8 length) +{ + uint8 mpduLength; + uint8 status; + + // Turn on receiver if its not on + if (!txState.receiveOn) + { + halRfReceiveOn(); + } + + // Check packet length + length = MIN(length, BASIC_RF_MAX_PAYLOAD_SIZE); + + // Wait until the transceiver is idle + halRfWaitTransceiverReady(); + + // Turn off RX frame done interrupt to avoid interference on the SPI interface + halRfDisableRxInterrupt(); + + mpduLength = basicRfBuildMpdu(destAddr, pPayload, length); + + #ifdef SECURITY_CCM + halRfWriteTxBufSecure(txMpdu, mpduLength, length, BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M); + txState.frameCounter++; // Increment frame counter field + #else + halRfWriteTxBuf(txMpdu, mpduLength); + #endif + + // Turn on RX frame done interrupt for ACK reception + halRfEnableRxInterrupt(); + + // Send frame with CCA. return FAILED if not successful + if (halRfTransmit() != SUCCESS) + { + status = FAILED; + } + + // Wait for the acknowledge to be received, if any + if (pConfig->ackRequest) + { + txState.ackReceived = FALSE; + + // We'll enter RX automatically, so just wait until we can be sure that the ack reception should have finished + // The timeout consists of a 12-symbol turnaround time, the ack packet duration, and a small margin + halMcuWaitUs((12 *BASIC_RF_SYMBOL_DURATION) + (BASIC_RF_ACK_DURATION) + (2 *BASIC_RF_SYMBOL_DURATION) + 10); + + // If an acknowledgment has been received (by RxFrmDoneIsr), the ackReceived flag should be set + status = txState.ackReceived ? SUCCESS : FAILED; + } + else + { + status = SUCCESS; + } + + // Turn off the receiver if it should not continue to be enabled + if (!txState.receiveOn) + { + halRfReceiveOff(); + } + + if (status == SUCCESS) + { + txState.txSeqNumber++; + } + + #ifdef SECURITY_CCM + halRfIncNonceTx(); // Increment nonce value + #endif + + return status; +} + +//------------------------------------------------------------------- +// @fn basicRfPacketIsReady +// @brief Check if a new packet is ready to be read by next higher layer +// @param none +// @return uint8 - TRUE if a packet is ready to be read by higher layer +//------------------------------------------------------------------- +uint8 basicRfPacketIsReady(void) +{ + return rxi.isReady; +} + +//------------------------------------------------------------------- +// @fn basicRfReceive +// @brief Copies the payload of the last incoming packet into a buffer +// @param pRxData - pointer to data buffer to fill. This buffer must be +// allocated by higher layer. +// len - Number of bytes to read in to buffer +// rxi - file scope variable holding the information of the last +// incoming packet +// @return uint8 - number of bytes actually copied into buffer +//------------------------------------------------------------------- +uint8 basicRfReceive(uint8 *pRxData, uint16 len, int16 *pRssi) +{ + // Accessing shared variables -> this is a critical region + // Critical region start + halIntOff(); + memcpy(pRxData, rxi.pPayload, MIN(rxi.length, len)); + if (pRssi != NULL) + { + if (rxi.rssi < 128) + { + *pRssi = rxi.rssi - halRfGetRssiOffset(); + } + else + { + *pRssi = (rxi.rssi - 256) - halRfGetRssiOffset(); + } + } + rxi.isReady = FALSE; + halIntOn(); + + // Critical region end + return MIN(rxi.length, len); +} + +//------------------------------------------------------------------- +// @fn basicRfGetRssi +// @brief Copies the payload of the last incoming packet into a buffer +// @param none +// @return int8 - RSSI value +//------------------------------------------------------------------- +int8 basicRfGetRssi(void) +{ + if (rxi.rssi < 128) + { + return rxi.rssi - halRfGetRssiOffset(); + } + else + { + return (rxi.rssi - 256) - halRfGetRssiOffset(); + } +} + +//------------------------------------------------------------------- +// @fn basicRfReceiveOn +// @brief Turns on receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOn(void) +{ + txState.receiveOn = TRUE; + halRfReceiveOn(); +} + +//------------------------------------------------------------------- +// @fn basicRfReceiveOff +// @brief Turns off receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOff(void) +{ + txState.receiveOn = FALSE; + halRfReceiveOff(); +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.h new file mode 100644 index 0000000..655960e --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf.h @@ -0,0 +1,123 @@ +//------------------------------------------------------------------- +// Filename: basic_rf.h +// Description: Basic RF library header file +//------------------------------------------------------------------- +// The "Basic RF" library contains simple functions for packet transmission and +// reception with the IEEE 802.15.4 compliant radio devices. The intention of this +// library is to demonstrate how the radio devices are operated, and not to provide +// a complete and fully-functional packet protocol. The protocol uses 802.15.4 MAC compliant data +// and acknowledgment packets, however it contains only a small subset of the +// 802.15.4 standard: +// - Association, scanning nor beacons are not implemented +// - No defined coordinator/device roles (peer-to-peer, all nodes are equal) +// - Waits for the channel to become ready, but does not check CCA twice +// (802.15.4 CSMA-CA) +// - Does not retransmit packets +// +// INSTRUCTIONS: +// Startup: +// 1. Create a basicRfCfg_t structure, and initialize the members: +// 2. Call basicRfInit() to initialize the packet protocol. +// +// Transmission: +// 1. Create a buffer with the payload to send +// 2. Call basicRfSendPacket() +// +// Reception: +// 1. Check if a packet is ready to be received by highger layer with +// basicRfPacketIsReady() +// 2. Call basicRfReceive() to receive the packet by higher layer +// +// FRAME FORMATS: +// Data packets (without security): +// [Preambles (4)][SFD (1)][Length (1)][Frame control field (2)] +// [Sequence number (1)][PAN ID (2)][Dest. address (2)][Source address (2)] +// [Payload (Length - 2+1+2+2+2)][Frame check sequence (2)] +// +// Acknowledgment packets: +// [Preambles (4)][SFD (1)][Length = 5 (1)][Frame control field (2)] +// [Sequence number (1)][Frame check sequence (2)] +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + uint16 myAddr; // ±¾»úµØÖ· + uint16 panId; // ÍøÂçID + uint8 channel; // ͨÐÅÐŵÀ£¬ 11~26 + uint8 ackRequest; //Ó¦´ðÐźŠ+ #ifdef SECURITY_CCM + uint8 *securityKey; + uint8 *securityNonce; + #endif +} basicRfCfg_t; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS + + +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfInit +// @brief Initialise basic RF datastructures. Sets channel, short address and +// PAN id in the chip and configures interrupt on packet reception +// @param pRfConfig - pointer to BASIC_RF_CONFIG struct. +// This struct must be allocated by higher layer +// txState - file scope variable that keeps tx state info +// rxi - file scope variable info extracted from the last incoming frame +// @return none +//------------------------------------------------------------------- +uint8 basicRfInit(basicRfCfg_t *pRfConfig); + +//------------------------------------------------------------------- +// @fn basicRfSendPacket +// @brief Send packet +// @param destAddr - destination short address +// pPayload - pointer to payload buffer. This buffer must be +// allocated by higher layer. +// length - length of payload +// txState - file scope variable that keeps tx state info +// mpdu - file scope variable. Buffer for the frame to send +// @return basicRFStatus_t - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 basicRfSendPacket(uint16 destAddr, uint8 *pPayload, uint8 length); + +//------------------------------------------------------------------- +// @fn basicRfPacketIsReady +// @brief Check if a new packet is ready to be read by next higher layer +// @param none +// @return uint8 - TRUE if a packet is ready to be read by higher layer +//------------------------------------------------------------------- +uint8 basicRfPacketIsReady(void); + +//------------------------------------------------------------------- +// @fn basicRfReceive +// @brief Copies the payload of the last incoming packet into a buffer +// @param pRxData - pointer to data buffer to fill. This buffer must be +// allocated by higher layer. +// len - Number of bytes to read in to buffer +// rxi - file scope variable holding the information of the last +// incoming packet +// @return uint8 - number of bytes actually copied into buffer +//------------------------------------------------------------------- +uint8 basicRfReceive(uint8 *pRxData, uint16 len, int16 *pRssi); + +//------------------------------------------------------------------- +// @fn basicRfReceiveOn +// @brief Turns on receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOn(void); + +//------------------------------------------------------------------- +// @fn basicRfReceiveOff +// @brief Turns off receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOff(void); + + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.c new file mode 100644 index 0000000..b73faf0 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: basic_rf_security.c +// Description: Basic RF security library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#ifdef SECURITY_CCM + #include "hal_defs.h" + #include "hal_rf_security.h" + #include "basic_rf.h" + #include "basic_rf_security.h" + + //------------------------------------------------------------------- + // CONSTANTS AND DEFINES + //------------------------------------------------------------------- + #define FLAG_FIELD 0x09 + #define NONCE_SIZE 16 + + //------------------------------------------------------------------- + // LOCAL VARIABLES + //------------------------------------------------------------------- + static uint8 nonceTx[NONCE_SIZE]; + static uint8 nonceRx[NONCE_SIZE]; + + //------------------------------------------------------------------- + // GLOBAL FUNCTIONS + //------------------------------------------------------------------- + //------------------------------------------------------------------- + // @fn basicRfSecurityInit + // @brief Initialise key and nonces and write to radio + // @param pConfig - file scope variable holding configuration data for basic RF + // @return none + //------------------------------------------------------------------- + void basicRfSecurityInit(basicRfCfg_t *pConfig) + { + uint8 i; + + // Initialise nonce bytes to 0 + for (i = 0; i < NONCE_SIZE; i++) + { + nonceRx[i] = 0; + nonceTx[i] = 0; + } + + // Set nonce flag field (Byte 0) + nonceRx[0] = FLAG_FIELD; + nonceTx[0] = FLAG_FIELD; + + // Set byte 7 and 8 of nonce to myAddr + nonceTx[8] = (uint8)pConfig->myAddr; + nonceTx[7] = (uint8)(pConfig->myAddr >> 8); + + // Set Security mode field of nonces (Byte 13) + nonceRx[13] = SECURITY_CONTROL; + nonceTx[13] = SECURITY_CONTROL; + + halRfSecurityInit(pConfig->securityKey, nonceRx, nonceTx); + } +#endif diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.h new file mode 100644 index 0000000..1af1cdf --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/basicrf/basic_rf_security.h @@ -0,0 +1,8 @@ +//------------------------------------------------------------------- +// Filename: basic_rf_security.h +// Description: Basic RF security library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void basicRfSecurityInit(basicRfCfg_t *pConfig); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.c new file mode 100644 index 0000000..1a1eb90 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.c @@ -0,0 +1,17 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_digio.h" +#include "hal_int.h" +#include "hal_board.h" +#include "hal_uart.h" + +void halBoardInit(void) +{ + halMcuInit(); + halUartInit(115200); + halIntOn(); +} + + + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.h new file mode 100644 index 0000000..51cae44 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_board.h @@ -0,0 +1,307 @@ +//------------------------------------------------------------------- +// Filename: hal_board.h +// Description: MCU-KIT1 board with CC2530 +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS +//------------------------------------------------------------------- + +// Board properties +#define BOARD_NAME "ZIGBEE-DMA2530" +#define NUM_LEDS 4 + +// Clock +#define BSP_CONFIG_CLOCK_MHZ 32 + + +//RFID_FLAG +#define HAL_BOARD_IO_FLAG_PORT 1 +#define HAL_BOARD_IO_FLAG_PIN 1 + +//CD2530_Test_Pin +#define HAL_BOARD_IO_TEST1_PORT 1 +#define HAL_BOARD_IO_TEST1_PIN 3 +#define HAL_BOARD_IO_TEST2_PORT 1 +#define HAL_BOARD_IO_TEST2_PIN 4 +#define HAL_BOARD_IO_TEST3_PORT 1 +#define HAL_BOARD_IO_TEST3_PIN 5 +#define HAL_BOARD_IO_TEST4_PORT 0 +#define HAL_BOARD_IO_TEST4_PIN 0 + + +// LEDs +#define HAL_BOARD_IO_LED_PORT 1 + +//#define HAL_BOARD_IO_LED_1_PORT 1 +//#define HAL_BOARD_IO_LED_1_PIN 2 +#define HAL_BOARD_IO_LED_1_PORT 1 +#define HAL_BOARD_IO_LED_1_PIN 0 +#define HAL_BOARD_IO_LED_2_PORT 1 +#define HAL_BOARD_IO_LED_2_PIN 1 +#define HAL_BOARD_IO_LED_3_PORT 1 +#define HAL_BOARD_IO_LED_3_PIN 3 +#define HAL_BOARD_IO_LED_4_PORT 1 +#define HAL_BOARD_IO_LED_4_PIN 4 + +//DC +#define HAL_BOARD_IO_DC_1_PORT 2 +#define HAL_BOARD_IO_DC_1_PIN 0 +#define HAL_BOARD_IO_DC_2_PORT 1 +#define HAL_BOARD_IO_DC_2_PIN 7 + +//TC72 +#define HAL_BOARD_IO_TC72_SDI_PORT 0 +#define HAL_BOARD_IO_TC72_SDI_PIN 4 +#define HAL_BOARD_IO_TC72_SCL_PORT 0 +#define HAL_BOARD_IO_TC72_SCL_PIN 5 +#define HAL_BOARD_IO_TC72_SDO_PORT 0 +#define HAL_BOARD_IO_TC72_SDO_PIN 6 +#define HAL_BOARD_IO_TC72_SSS_PORT 0 +#define HAL_BOARD_IO_TC72_SSS_PIN 7 + +//AT24C02(E2PROM) +#define HAL_BOARD_IO_E2PROM_SCL_PORT 0 +#define HAL_BOARD_IO_E2PROM_SCL_PIN 6 +#define HAL_BOARD_IO_E2PROM_SDA_PORT 0 +#define HAL_BOARD_IO_E2PROM_SDA_PIN 7 + +//ITG +#define HAL_BOARD_IO_E2PROM_SCK_PORT 0 +#define HAL_BOARD_IO_E2PROM_SCK_PIN 6 +#define HAL_BOARD_IO_E2PROM_SDA_PORT 0 +#define HAL_BOARD_IO_E2PROM_SDA_PIN 7 + + +// SPI +#define HAL_BOARD_IO_SPI_MISO_PORT 0 +#define HAL_BOARD_IO_SPI_MISO_PIN 6 +#define HAL_BOARD_IO_SPI_MOSI_PORT 0 +#define HAL_BOARD_IO_SPI_MOSI_PIN 4 +#define HAL_BOARD_IO_SPI_CLK_PORT 0 +#define HAL_BOARD_IO_SPI_CLK_PIN 5 +#define HAL_BOARD_IO_SPI_CS_PORT 0 +#define HAL_BOARD_IO_SPI_CS_PIN 7 + +//SHT +#define HAL_BOARD_IO_SHT_SCK_PORT 2 +#define HAL_BOARD_IO_SHT_SCK_PIN 4 +#define HAL_BOARD_IO_SHT_SDA_PORT 2 +#define HAL_BOARD_IO_SHT_SDA_PIN 3 + +//SR04£¨³¬Éù²¨£© +#define HAL_BOARD_IO_SR04_TX_PORT 2 +#define HAL_BOARD_IO_SR04_TX_PIN 3 +#define HAL_BOARD_IO_SR04_RX_PORT 2 +#define HAL_BOARD_IO_SR04_RX_PIN 4 + +//TCS (ÑÕÉ«) +#define HAL_BOARD_IO_TCS_S2_PORT 2 +#define HAL_BOARD_IO_TCS_S2_PIN 3 +#define HAL_BOARD_IO_TCS_S3_PORT 2 +#define HAL_BOARD_IO_TCS_S3_PIN 4 + + +// I2C +#define HAL_BOARD_IO_I2C_SCL_PORT 0 +#define HAL_BOARD_IO_I2C_SCL_PIN 5 +#define HAL_BOARD_IO_I2C_SDA_PORT 0 +#define HAL_BOARD_IO_I2C_SDA_PIN 4 + +// LED +#define HAL_BOARD_IO_LED_DATA_PORT 1 + +// A/D +#define HAL_BOARD_IO_ADC_PORT 0 +#define HAL_BOARD_IO_ADC_CH 0 + +// INT +#define HAL_BOARD_IO_INT_PORT 0 +#define HAL_BOARD_IO_INT_PIN 1 + +// DO +#define HAL_BOARD_IO_DO_PORT 0 +#define HAL_BOARD_IO_DO_PIN 0 + +//OUT +#define HAL_BOARD_IO_OUT_PORT 1 +#define HAL_BOARD_IO_OUT_PIN 7 + +// UART +#define HAL_BOARD_IO_UART_TXD_PORT 0 +#define HAL_BOARD_IO_UART_TXD_PIN 3 +#define HAL_BOARD_IO_UART_RXD_PORT 0 +#define HAL_BOARD_IO_UART_RXD_PIN 2 + +// UART1 +#define HAL_BOARD_IO_UART1_TXD_PORT 1 +#define HAL_BOARD_IO_UART1_TXD_PIN 6 +#define HAL_BOARD_IO_UART1_RXD_PORT 1 +#define HAL_BOARD_IO_UART1_RXD_PIN 7 + +// Debounce +#define HAL_DEBOUNCE(expr) { char i; for (i=0; i<50; i++) { if (!(expr)) i = 0; } } + +// Assert +#define HAL_ASSERT(szErrMsg) { halAssertHandler(szErrMsg); } + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- + +//CD2530_Test_Pin + +#define HAL_BOARD_TEST1_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST1_PORT, HAL_BOARD_IO_TEST1_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST2_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST2_PORT, HAL_BOARD_IO_TEST2_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST3_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST3_PORT, HAL_BOARD_IO_TEST3_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST4_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST4_PORT, HAL_BOARD_IO_TEST4_PIN, MCU_IO_PULLUP) + +#define HAL_BOARD_TEST1_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST1_PORT, HAL_BOARD_IO_TEST1_PIN) +#define HAL_BOARD_TEST2_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST2_PORT, HAL_BOARD_IO_TEST2_PIN) +#define HAL_BOARD_TEST3_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST3_PORT, HAL_BOARD_IO_TEST3_PIN) +#define HAL_BOARD_TEST4_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST4_PORT, HAL_BOARD_IO_TEST4_PIN) + +//RFID_FLAG +#define HAL_FLAG_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_FLAG_PORT, HAL_BOARD_IO_FLAG_PIN) +#define HAL_FLAG_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_FLAG_PORT, HAL_BOARD_IO_FLAG_PIN) + +// LED +#define HAL_LED_SET_1() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_SET_2() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_SET_3() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_SET_4() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + +#define HAL_LED_CLR_1() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_CLR_2() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_CLR_3() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_CLR_4() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + + +#define HAL_LED_TGL_1() MCU_IO_TGL(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_TGL_2() MCU_IO_TGL(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_TGL_3() MCU_IO_TGL(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_TGL_4() MCU_IO_TGL(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + +//DC +#define HAL_DC_START_1() MCU_IO_SET_HIGH(HAL_BOARD_IO_DC_1_PORT, HAL_BOARD_IO_DC_1_PIN) +#define HAL_DC_START_2() MCU_IO_SET_HIGH(HAL_BOARD_IO_DC_2_PORT, HAL_BOARD_IO_DC_2_PIN) + +#define HAL_DC_STOP_1() MCU_IO_SET_LOW(HAL_BOARD_IO_DC_1_PORT, HAL_BOARD_IO_DC_1_PIN) +#define HAL_DC_STOP_2() MCU_IO_SET_LOW(HAL_BOARD_IO_DC_2_PORT, HAL_BOARD_IO_DC_2_PIN) + +//TC72 +#define HAL_TC72_SDI_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN) +#define HAL_TC72_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN) +#define HAL_TC72_SSS_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN) + +#define HAL_TC72_SDI_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN) +#define HAL_TC72_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN) +#define HAL_TC72_SSS_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN) + +#define HAL_TC72_SDO_VAL() MCU_IO_GET(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN) + +#define HAL_TC72_SDO_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN) + +//AT24C02(E2PROM) +#define HAL_E2PROM_SCL_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,0) +//#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +//#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,MCU_IO_PULLUP) + +#define HAL_E2PROM_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN) +#define HAL_E2PROM_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN) + +//#define HAL_E2PROM_SDA_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) +//#define HAL_E2PROM_SDA_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +//#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +//ITG +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,MCU_IO_PULLUP) + +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,0) + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,0) + +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +// LED_ALL +#define HAL_LED_PORT(val) MCU_PORT_OUTPUT(HAL_BOARD_IO_LED_DATA_PORT, val) + +// SPI +#define HAL_SPI_INIT() {MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN, 1);MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN, 1);MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN, 1);MCU_IO_INPUT(HAL_BOARD_IO_SPI_MISO_PORT, HAL_BOARD_IO_SPI_MISO_PIN,MCU_IO_PULLUP);} + +#define HAL_SPI_MOSI_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN) +#define HAL_SPI_MOSI_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN) + +#define HAL_SPI_CLK_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN) +#define HAL_SPI_CLK_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN) + +#define HAL_SPI_CS_EN() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN) +#define HAL_SPI_CS_DIS() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN) + +#define HAL_SPI_MISO_VAL() MCU_IO_GET(HAL_BOARD_IO_SPI_MISO_PORT, HAL_BOARD_IO_SPI_MISO_PIN) + +//SHT +#define HAL_SHT_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_SHT_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1)//MCU_IO_SET_HIGH(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN) +#define HAL_SHT_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0)//MCU_IO_SET_LOW(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN) + +#define HAL_SHT_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1)//MCU_IO_SET_HIGH(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define HAL_SHT_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0)//MCU_IO_SET_LOW(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define HAL_SHT_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,MCU_IO_PULLUP) +#define HAL_SHT_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) + +//SR04£¨³¬Éù²¨£© + +#define HAL_SHT_TX_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,0) +#define HAL_SHT_TX_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,1) +#define HAL_SHT_TX_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,0) + +#define HAL_SHT_RX_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SR04_RX_PORT, HAL_BOARD_IO_SR04_RX_PIN,MCU_IO_PULLUP) +#define HAL_SHT_RX_VAL() MCU_IO_GET(HAL_BOARD_IO_SR04_RX_PORT, HAL_BOARD_IO_SR04_RX_PIN) + +//flame(»ðÑæ) +#define HAL_SHT_FLAME_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH,MCU_IO_PULLUP) +#define HAL_SHT_FLAME_VAL() MCU_IO_GET(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH) + +//TCS(ÑÕÉ«) +#define HAL_SHT_S2_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,0) +#define HAL_SHT_S2_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,1) +#define HAL_SHT_S2_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,0) + +#define HAL_SHT_S3_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,0) +#define HAL_SHT_S3_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,1) +#define HAL_SHT_S3_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,0) + +// I2C +#define HAL_I2C_SCL_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN,0) +#define HAL_I2C_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN) +#define HAL_I2C_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN) + +#define HAL_I2C_SDA_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) +#define HAL_I2C_SDA_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) +#define HAL_I2C_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN,1) +#define HAL_I2C_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN,MCU_IO_PULLUP) +#define HAL_I2C_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) + +// DO +#define HAL_DO_OFF() MCU_IO_SET_HIGH(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN) +#define HAL_DO_ON() MCU_IO_SET_LOW(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN) +#define HAL_DO_OUTPUT() MCU_IO_OUTPUT(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN, 1) + +//INT +#define HAL_INT_VAL() MCU_IO_GET(HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN) + +//OUT +#define HAL_OUT_VAL() MCU_IO_GET(HAL_BOARD_IO_OUT_PORT, HAL_BOARD_IO_OUT_PIN) + + +//------------------------------------------------------------------- +// FUNCTION PROTOTYPES +//------------------------------------------------------------------- +void halBoardInit(void); + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.c new file mode 100644 index 0000000..1980f63 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.c @@ -0,0 +1,98 @@ +//------------------------------------------------------------------- +// Filename: hal_led.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_led.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halLedSet +// @brief Turn LED on. +// @param uint8 id - led to set +// @return none +//------------------------------------------------------------------- +void halLedSet(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_SET_1(); + break; + case 2: + HAL_LED_SET_2(); + break; + case 3: + HAL_LED_SET_3(); + break; + case 4: + HAL_LED_SET_4(); + break; + + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halLedClear +// @brief Turn LED off. +// @param uint8 id - led to clear +// @return none +//------------------------------------------------------------------- +void halLedClear(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_CLR_1(); + break; + case 2: + HAL_LED_CLR_2(); + break; + case 3: + HAL_LED_CLR_3(); + break; + case 4: + HAL_LED_CLR_4(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halLedToggle +// @brief Change state of LED. If on, turn it off. Else turn on. +// @param uint8 id - led to toggle +// @return none +//------------------------------------------------------------------- +void halLedToggle(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_TGL_1(); + break; + case 2: + HAL_LED_TGL_2(); + break; + case 3: + HAL_LED_TGL_3(); + break; + case 4: + HAL_LED_TGL_4(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.h new file mode 100644 index 0000000..1591503 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/board/hal_led.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: hal_led.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halLedSet(uint8 led_id); +void halLedClear(uint8 led_id); +void halLedToggle(uint8 led_id); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.c new file mode 100644 index 0000000..1ec78ce --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.c @@ -0,0 +1,57 @@ +#include "hal_defs.h" +#include "hal_led.h" +#include "TIMER.h" + + +uint8 SEND_DATA_FLAG = 0; +uint16 NUM = 0; + +void Timer4_Init(void) +{ + // Set prescaler divider value to 128 (32M/128 = 250KHZ) + T4CTL |= 0xE0; + T4CTL &= ~(0x10); // Stop timer + T4CTL &= ~(0x08); // ½ûÖ¹Òç³öÖÐ¶Ï + T4CTL |= 0x04; //¼ÆÊýÆ÷ÇåÁã + T4IE = 0; // Disable interrupt +} + +void Timer4_On(void) +{ + T4CTL |= 0x08; //ʹÄÜÒç³öÖÐ¶Ï + T4CTL &= ~(0x03);//0x00-0xFF + T4CTL |= 0x10; // Start timer + T4IE = 1; // Enable interrupt +} + +void Timer4_Off(void) +{ + T4CTL &= ~(0x08); // ½ûÖ¹Òç³öÖÐ¶Ï + T4CTL &= ~(0x10); // Stop timer + T4IE = 0; // Disable interrupt +} + +uint8 GetSendDataFlag(void) +{ + return SEND_DATA_FLAG; +} + + + +HAL_ISR_FUNCTION(T4_ISR, T4_VECTOR) +{ + T4OVFIF = 0; + T4IF = 0; + + NUM ++; + if(NUM == 500)//1953) //¶¨Ê±2s£¬·­×ªÒ»´Î£¨1/250000*256£©ÕâÊǽøÒ»´ÎÖжÏËùÐèÒªµÄʱ¼ä£¬½ø781´ÎÖжϴóÔ¼ÊÇ0.8s + { + NUM = 0; + SEND_DATA_FLAG = 1; + //halLedToggle(1); + } + else + { + SEND_DATA_FLAG = 0; + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.h new file mode 100644 index 0000000..16a4653 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/TIMER.h @@ -0,0 +1,4 @@ +void Timer4_Init(void); +void Timer4_On(void); +void Timer4_Off(void); +uint8 GetSendDataFlag(void); \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.c new file mode 100644 index 0000000..04d7258 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.c @@ -0,0 +1,56 @@ +//------------------------------------------------------------------- +// Filename: hal_adc.c +// Description: These functions/macros simplifies usage of the ADC. +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_adc.h" + +//------------------------------------------------------------------- +// @fn adcSampleSingle +// @brief +// This function makes the adc sample the given channel at the given +// resolution with the given reference. +// @param uint8 reference +// The reference to compare the channel to be sampled. +// uint8 resolution +// The resolution to use during the sample (8, 10, 12 or 14 bit) +// uint8 input +// The channel to be sampled. +// @return int16 +// The conversion result +//------------------------------------------------------------------- +unsigned int adcSampleSingle(uint8 reference, uint8 resolution, uint8 channel) +{ + unsigned int value; + + ADC_ENABLE_CHANNEL(channel); + + ADCIF = 0; + ADC_SINGLE_CONVERSION(reference | resolution | channel); + while (!ADCIF) + ; + + value = ((unsigned int) ADCH) << 8; + value |= (unsigned int) ADCL; + + if(value & 0x8000) + { + value = 0; + } + + value = value & 0x7FF8; + + ADC_DISABLE_CHANNEL(channel); + + // The variable 'value' contains 16 bits where + // bit 15 is a sign bit + // bit [14 .. 0] contain the absolute sample value + // Only the r upper bits are significant, where r is the resolution + // Resolution: + // 12 -> [14 .. 3] (bitmask 0x7FF8) + // 10 -> [14 .. 5] (bitmask 0x7FE0) + // 9 -> [14 .. 6] (bitmask 0x7FC0) + // 7 -> [14 .. 8] (bitmask 0x7F00) + + return value; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.h new file mode 100644 index 0000000..309c674 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_adc.h @@ -0,0 +1,86 @@ +//------------------------------------------------------------------- +// Filename: hal_adc.h +// Description: These functions/macros simplifies usage of the ADC. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND MACROS +//------------------------------------------------------------------- +// Macro for setting up a single conversion. If ADCCON1.STSEL = 11, using this +// macro will also start the conversion. +#define ADC_SINGLE_CONVERSION(settings) st( ADCCON3 = (settings); ) + +// Macro for setting up a single conversion +#define ADC_SEQUENCE_SETUP(settings) st( ADCCON2 = (settings); ) + +// Where _settings_ are the following: +// Reference voltage: +#define ADC_REF_1_25_V 0x00 // Internal 1.25V reference +#define ADC_REF_P0_7 0x40 // External reference on AIN7 pin +#define ADC_REF_AVDD 0x80 // AVDD_SOC pin +#define ADC_REF_P0_6_P0_7 0xC0 // External reference on AIN6-AIN7 differential input + +// Resolution (decimation rate): +#define ADC_7_BIT 0x00 // 64 decimation rate +#define ADC_9_BIT 0x10 // 128 decimation rate +#define ADC_10_BIT 0x20 // 256 decimation rate +#define ADC_12_BIT 0x30 // 512 decimation rate + +// Input channel: +#define ADC_AIN0 0x00 // single ended P0_0 +#define ADC_AIN1 0x01 // single ended P0_1 +#define ADC_AIN2 0x02 // single ended P0_2 +#define ADC_AIN3 0x03 // single ended P0_3 +#define ADC_AIN4 0x04 // single ended P0_4 +#define ADC_AIN5 0x05 // single ended P0_5 +#define ADC_AIN6 0x06 // single ended P0_6 +#define ADC_AIN7 0x07 // single ended P0_7 +#define ADC_GND 0x0C // Ground +#define ADC_TEMP_SENS 0x0E // on-chip temperature sensor +#define ADC_VDD_3 0x0F // (vdd/3) + +// Macro for starting the ADC in continuous conversion mode +#define ADC_SAMPLE_CONTINUOUS() \ +st( ADCCON1 &= ~0x30; \ +ADCCON1 |= 0x10; ) + +// Macro for stopping the ADC in continuous mode +#define ADC_STOP() st( ADCCON1 |= 0x30; ) + +// Macro for initiating a single sample in single-conversion mode (ADCCON1.STSEL = 11). +#define ADC_SAMPLE_SINGLE() \ +st( ADC_STOP(); \ +ADCCON1 |= 0x40; ) + +// Macro for configuring the ADC to be started from T1 channel 0. (T1 ch 0 must be in compare mode!!) +#define ADC_TRIGGER_FROM_TIMER1() \ +st( ADC_STOP(); \ +ADCCON1 &= ~0x10; ) + +// Expression indicating whether a conversion is finished or not. +#define ADC_SAMPLE_READY() st( ADCCON1 & 0x80; ) + +// Macro for setting/clearing a channel as input of the ADC +#define ADC_ENABLE_CHANNEL(ch) ADCCFG |= (0x01 << ch) +#define ADC_DISABLE_CHANNEL(ch) ADCCFG &= ~(0x01 << ch) + + +// Macros for configuring the ADC input: +// Example usage: +// IO_ADC_PORT0_PIN(0, IO_ADC_EN); +// IO_ADC_PORT0_PIN(4, IO_ADC_DIS); +// IO_ADC_PORT0_PIN(6, IO_ADC_EN); + +#define IO_ADC_PORT0_PIN(pin, adcEn) \ +st( if (adcEn) \ +ADCCFG |= BM( pin ); \ +else \ +ADCCFG &= ~BM( pin ) ) + +// where adcEn is one of: +#define IO_ADC_EN 1 // ADC input enabled +#define IO_ADC_DIS 0 // ADC input disab + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +unsigned int adcSampleSingle(uint8 reference, uint8 resolution, uint8 channel); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_cc8051.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_cc8051.h new file mode 100644 index 0000000..d59f7f6 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_cc8051.h @@ -0,0 +1,53 @@ +//------------------------------------------------------------------- +// Filename: hal_cc8051.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +//CONSTANTS +//------------------------------------------------------------------- +#define BIT0 0x01 +#define BIT1 0x02 +#define BIT2 0x04 +#define BIT3 0x08 +#define BIT4 0x10 +#define BIT5 0x20 +#define BIT6 0x40 +#define BIT7 0x80 + +//------------------------------------------------------------------- +//MACROS +//------------------------------------------------------------------- +#define MCU_IO_TRISTATE 1 // Used as "func" for the macros below +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +//----------------------------------------------------------------------------- +// Macros for simple configuration of IO pins on TI LPW SoCs +//----------------------------------------------------------------------------- +#define MCU_IO_PERIPHERAL(port, pin) MCU_IO_PERIPHERAL_PREP(port, pin) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_SET(port, pin, val) MCU_IO_SET_PREP(port, pin, val) +#define MCU_IO_SET_HIGH(port, pin) MCU_IO_SET_HIGH_PREP(port, pin) +#define MCU_IO_SET_LOW(port, pin) MCU_IO_SET_LOW_PREP(port, pin) +#define MCU_IO_TGL(port, pin) MCU_IO_TGL_PREP(port, pin) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_DIR_INPUT(port, pin) MCU_IO_DIR_INPUT_PREP(port, pin) +#define MCU_IO_DIR_OUTPUT(port, pin) MCU_IO_DIR_OUTPUT_PREP(port, pin) +#define MCU_PORT_SET_OUTPUT(port, val) MCU_PORT_SET_OUTPUT_PREP(port, val) +#define MCU_PORT_OUTPUT(port, val) MCU_PORT_OUTPUT_PREP(port, val) + +//---------------------------------------------------------------------------------- +// Macros for internal use (the macros above need a new round in the preprocessor) +//---------------------------------------------------------------------------------- +#define MCU_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BM(pin); ) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_SET_HIGH_PREP(port, pin) st( P##port##_##pin## = 1; ) +#define MCU_IO_SET_LOW_PREP(port, pin) st( P##port##_##pin## = 0; ) +#define MCU_IO_SET_PREP(port, pin, val) st( P##port##_##pin## = val; ) +#define MCU_IO_TGL_PREP(port, pin) st( P##port##_##pin## ^= 1; ) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) +#define MCU_IO_DIR_INPUT_PREP(port, pin) st( P##port##DIR &= ~BM(pin); ) +#define MCU_IO_DIR_OUTPUT_PREP(port, pin) st( P##port##DIR |= BM(pin); ) +#define MCU_PORT_SET_OUTPUT_PREP(port, val) st( P##port##SEL = 0x00; P##port## = val; P##port##DIR = 0xff; ) +#define MCU_PORT_OUTPUT_PREP(port, val) st( P##port## = val; ) diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.c new file mode 100644 index 0000000..fa4351a --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.c @@ -0,0 +1,75 @@ +//------------------------------------------------------------------- +// Filename: hal_clock.c +// Description: This file defines clock related functions for the CC253x family +// of RF system-on-chips from Texas Instruments. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_clock.h" + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn clockSetMainSrc +// @brief Function for setting the main system clock source. +// The function turns off the clock source that is not being used. +// TICKSPD is set to the same frequency as the source. +// @param uint8 source (one of CLOCK_SRC_HFRC or CLOCK_SRC_XOSC) +// @return void +//------------------------------------------------------------------- +void clockSetMainSrc(uint8 source) +{ + register uint8 osc32k_bm = CLKCONCMD &CLKCON_OSC32K_BM; + + // Source can have the following values: + // CLOCK_SRC_XOSC 0x00 High speed Crystal Oscillator (XOSC) + // CLOCK_SRC_HFRC 0x01 Low power RC Oscillator (HFRC) + + SLEEPCMD &= ~SLEEP_OSC_PD_BM; // power up both oscillators + while (!CC2530_IS_HFRC_STABLE() || ((SLEEPSTA &SLEEP_OSC_PD_BM) != 0)) + ; + // wait until the oscillator is stable + NOP(); + + if (source == CLOCK_SRC_HFRC) + { + CLKCONCMD = (osc32k_bm | CLKCON_OSC_BM | TICKSPD_DIV_2 | CLKCON_CLKSPD_BM); + } + else if (source == CLOCK_SRC_XOSC) + { + CLKCONCMD = (osc32k_bm | TICKSPD_DIV_1); + } + CC2530_WAIT_CLK_UPDATE(); + SLEEPCMD |= SLEEP_OSC_PD_BM; // power down the unused oscillator +} + +//------------------------------------------------------------------- +// @fn clockSelect32k +// @brief Function for selecting source for the 32kHz oscillator +// @param uint8 source (one of CLOCK_32K_XTAL or CLOCK_32K_RCOSC) +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 clockSelect32k(uint8 source) +{ + // System clock source must be high frequency RC oscillator before + // changing 32K source. + if (!(CLKCONSTA &CLKCON_OSC_BM)) + { + return FAILED; + } + + if (source == CLOCK_32K_XTAL) + { + CLKCONCMD &= ~CLKCON_OSC32K_BM; + } + else if (source == CLOCK_32K_RCOSC) + { + CLKCONCMD |= CLKCON_OSC32K_BM; + } + CC2530_WAIT_CLK_UPDATE(); + + return SUCCESS; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.h new file mode 100644 index 0000000..c664467 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_clock.h @@ -0,0 +1,66 @@ +//------------------------------------------------------------------- +// Filename: hal_clock.h +// Description: This file defines interface for clock related functions +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS +//------------------------------------------------------------------- +// SEE DATA SHEET FOR DETAILS ABOUT THE FOLLOWING BIT MASKS + +// Parameters for cc2530ClockSetMainSrc +#define CLOCK_SRC_XOSC 0 // High speed Crystal Oscillator Control +#define CLOCK_SRC_HFRC 1 // Low power RC Oscillator + +// Parameters for cc2530ClockSelect32k +#define CLOCK_32K_XTAL 0 // 32.768 Hz crystal oscillator +#define CLOCK_32K_RCOSC 1 // 32 kHz RC oscillator + +// Bit masks to check CLKCON register +#define CLKCON_OSC32K_BM 0x80 // bit mask, for the slow 32k clock oscillator +#define CLKCON_OSC_BM 0x40 // bit mask, for the system clock oscillator +#define CLKCON_TICKSPD_BM 0x38 // bit mask, for timer ticks output setting +#define CLKCON_CLKSPD_BM 0x01 // bit maks, for the clock speed + +#define TICKSPD_DIV_1 (0x00 << 3) +#define TICKSPD_DIV_2 (0x01 << 3) +#define TICKSPD_DIV_4 (0x02 << 3) +#define TICKSPD_DIV_8 (0x03 << 3) +#define TICKSPD_DIV_16 (0x04 << 3) +#define TICKSPD_DIV_32 (0x05 << 3) +#define TICKSPD_DIV_64 (0x06 << 3) +#define TICKSPD_DIV_128 (0x07 << 3) + +// Bit masks to check SLEEPSTA register +#define SLEEP_XOSC_STB_BM 0x40 // bit mask, check the stability of XOSC +#define SLEEP_HFRC_STB_BM 0x20 // bit maks, check the stability of the High-frequency RC oscillator +#define SLEEP_OSC_PD_BM 0x04 // bit mask, power down system clock oscillator(s) + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +// Macro for checking status of the high frequency RC oscillator. +#define CC2530_IS_HFRC_STABLE(x) (SLEEPSTA & SLEEP_HFRC_STB_BM) + +// Macro for checking status of the crystal oscillator +#define CC2530_IS_XOSC_STABLE(x) (SLEEPSTA & SLEEP_XOSC_STB_BM) + +// Macro for getting the clock division factor +#define CC2530_GET_CLKSPD(x) (CLKCONSTA & CLKCON_CLKSPD_BM) + +// Macro for getting the timer tick division factor. +#define CC2530_GET_TICKSPD(x) ((CLKCONSTA & CLKCON_TICKSPD_BM) >> 3) + +// Macro for setting the clock division factor, x value from 0b000 to 0b111 +#define CC2530_SET_TICKSPD(x) st( CLKCONCMD = ((((x) << 3) & 0x38) | (CLKCONCMD & 0xC7)); ) + +// Macro for setting the timer tick division factor, x value from 0b000 to 0b111 +#define CC2530_SET_CLKSPD(x) st( CLKCONCMD = (((x) & 0x07)| (CLKCONCMD & 0xF8)); ) + +// Macro for waiting for clock updates +#define CC2530_WAIT_CLK_UPDATE() st( uint8 ____clkcon; uint8 ____clkconsta; ____clkcon = CLKCONCMD; do { ____clkconsta = CLKCONSTA; } while (____clkconsta != ____clkcon); ) + +//------------------------------------------------------------------- +// FUNCTIONS +//------------------------------------------------------------------- +void clockSetMainSrc(uint8 source); +uint8 clockSelect32k(uint8 source); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_defs.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_defs.h new file mode 100644 index 0000000..84c14d7 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_defs.h @@ -0,0 +1,105 @@ +//------------------------------------------------------------------- +// Filename: hal_defs.h +// Description: HAL defines +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef signed char int8; +typedef unsigned char uint8; + +typedef signed short int16; +typedef unsigned short uint16; + +typedef signed long int32; +typedef unsigned long uint32; + +typedef void(*ISR_FUNC_PTR)(void); +typedef void(*VFPTR)(void); + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +#define TRUE 1 +#define FALSE 0 +#define NULL (void *)0 + +#define SUCCESS 0 +#define FAILED 1 + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define BV(n) (1 << (n)) + +#define BM(n) (1 << (n)) + +#define BF(x,b,s) (((x) & (b)) >> (s)) + +#define MIN(n,m) (((n) < (m)) ? (n) : (m)) + +#define MAX(n,m) (((n) < (m)) ? (m) : (n)) + +#define ABS(n) (((n) < 0) ? -(n) : (n)) + +// uint32 processing +#define BREAK_UINT32( var, ByteNum ) (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF)) +#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) ((uint32)((uint32)((Byte0) & 0x00FF) + ((uint32)((Byte1) & 0x00FF) << 8) + ((uint32)((Byte2) & 0x00FF) << 16) + ((uint32)((Byte3) & 0x00FF) << 24))) + +#define HI_UINT32(a) ((uint16) (((uint32)(a)) >> 16)) +#define LO_UINT32(a) ((uint16) ((uint32)(a))) + +// uint16 processing +#define BUILD_UINT16(loByte, hiByte) ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8))) + +#define HI_UINT16(a) (((uint16)(a) >> 8) & 0xFF) +#define LO_UINT16(a) ((uint16)(a) & 0xFF) + +// uint16 processing +#define BUILD_UINT8(hiByte, loByte) ((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4))) + +#define HI_UINT8(a) (((uint8)(a) >> 4) & 0x0F) +#define LO_UINT8(a) ((uint8)(a) & 0x0F) + +//------------------------------------------------------------------- +// This macro is for use by other macros to form a fully valid C statement. +// Without this, the if/else conditionals could show unexpected behavior. +// +// For example, use... +// #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; ) +// instead of ... +// #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; } +// or +// #define SET_REGS() ioreg1 = 0; ioreg2 = 0; +// The last macro would not behave as expected in the if/else construct. +// The second to last macro will cause a compiler error in certain uses +// of if/else construct +// +// It is not necessary, or recommended, to use this macro where there is +// already a valid C statement. For example, the following is redundant... +// #define CALL_FUNC() st( func(); ) +// This should simply be... +// #define CALL_FUNC() func() +// +// (The while condition below evaluates false without generating a +// constant-controlling-loop type of warning on most compilers.) +//------------------------------------------------------------------- +#define st(x) do { x } while (__LINE__ == -1) + +//------------------------------------------------------------------- +// Compiler abstraction +//------------------------------------------------------------------- +#define _PRAGMA(x) _Pragma(#x) + +#define FAR +#define NOP() asm("NOP") + +#define HAL_MCU_LITTLE_ENDIAN() __LITTLE_ENDIAN__ +#define HAL_ISR_FUNC_DECLARATION(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNC_PROTOTYPE(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v) diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.c new file mode 100644 index 0000000..4c5fd79 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.c @@ -0,0 +1,492 @@ +//------------------------------------------------------------------- +// Filename: hal_digio.c +// Description: HAL digital IO functionality +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_digio.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- + +static ISR_FUNC_PTR port0_isr_tbl[8] = +{ + 0 +}; +static ISR_FUNC_PTR port1_isr_tbl[8] = +{ + 0 +}; +static ISR_FUNC_PTR port2_isr_tbl[5] = +{ + 0 +}; + + +// Find a better place for this! +#define PICTL_PADSC_BM 0x80 +#define PICTL_P2IEN_BM 0x40 +#define PICTL_P0IENH_BM 0x20 +#define PICTL_P0IENL_BM 0x10 +#define PICTL_P2ICON_BM 0x08 +#define PICTL_P1ICONH_BM 0x04 +#define PICTL_P1ICONL_BM 0x02 +#define PICTL_P1ICON_BM 0x06 // Combined - for compatibility +#define PICTL_P0ICON_BM 0x01 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halDigioConfig +// @brief Configure the pin as specified by p. +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- + +//comment: find better way of doing this. SFR registers can not be accessed by pointers, therefore code is different from MSP430 code +// comment: currently support only P0 and P1 +uint8 halDigioConfig(const digioConfig *p) +{ + // register volatile uint8* dir; + register const uint8 bitmask = p->pin_bm; + + // Sanity check + if ((bitmask == 0) || (bitmask != (uint8)BV(p->pin))) + { + return (HAL_DIGIO_ERROR); + } + + switch (p->port) + { + case 0: + P0SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P0 |= bitmask; + } + else + { + P0 &= ~bitmask; + } + P0DIR |= bitmask; + } + else + // input + { + P0DIR &= ~bitmask; + } + break; + case 1: + P1SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P1 |= bitmask; + } + else + { + P1 &= ~bitmask; + } + P1DIR |= bitmask; + } + else + // input + { + P1DIR &= ~bitmask; + } + break; + case 2: + P2SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P2 |= bitmask; + } + else + { + P2 &= ~bitmask; + } + P2DIR |= bitmask; + } + else + // input + { + P2DIR &= ~bitmask; + } + break; + //case 1: P1SEL &= ~bitmask; out = &P1OUT; dir = &P1DIR; break; + //case 2: P2SEL &= ~bitmask; out = &P2OUT; dir = &P2DIR; break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + + +//------------------------------------------------------------------- +// @fn halDigioSet +// @brief Set output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioSet(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 |= p->pin_bm; + break; + case 1: + P1 |= p->pin_bm; + break; + case 2: + P2 |= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + + +//------------------------------------------------------------------- +// @fn halDigioClear +// @brief Clear output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioClear(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 &= ~p->pin_bm; + break; + case 1: + P1 &= ~p->pin_bm; + break; + case 2: + P2 &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioToggle +// @brief Toggle output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioToggle(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 ^= p->pin_bm; + break; + case 1: + P1 ^= p->pin_bm; + break; + case 2: + P2 ^= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioGet +// @brief Get value on input pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioGet(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_INPUT) + { + switch (p->port) + { + case 0: + return (P0 &p->pin_bm ? 1 : 0); + case 1: + return (P1 &p->pin_bm ? 1 : 0); + case 2: + return (P2 &p->pin_bm ? 1 : 0); + default: + break; + } + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioIntConnect +// @brief Connect function to IO interrupt +// @param digioConfig* p - pointer to configuration structure for IO pin +// ISR_FUNC_PTR func - pointer to function +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntConnect(const digioConfig *p, ISR_FUNC_PTR func) +{ + istate_t key; + HAL_INT_LOCK(key); + switch (p->port) + { + case 0: + port0_isr_tbl[p->pin] = func; + break; + case 1: + port1_isr_tbl[p->pin] = func; + break; + case 2: + port2_isr_tbl[p->pin] = func; + break; + default: + HAL_INT_UNLOCK(key); + return (HAL_DIGIO_ERROR); + } + halDigioIntClear(p); + HAL_INT_UNLOCK(key); + return (HAL_DIGIO_OK); +} + + +//------------------------------------------------------------------- +// @fn halDigioIntEnable +// @brief Enable interrupt on IO pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntEnable(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IE = 1; // set P0IE bit + P0IEN |= p->pin_bm; + break; + case 1: + IEN2 |= 0x10; // set P1IE bit + P1IEN |= p->pin_bm; + break; + case 2: + IEN2 |= 0x02; // set P2IE bit + P2IEN |= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntDisable +// @brief Disable interrupt on IO pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntDisable(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IEN &= ~p->pin_bm; + break; + case 1: + P1IEN &= ~p->pin_bm; + break; + case 2: + P2IEN &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntClear +// @brief Clear interrupt flag +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntClear(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IFG &= ~p->pin_bm; + break; + case 1: + P1IFG &= ~p->pin_bm; + break; + case 2: + P2IFG &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntSetEdge +// @brief Set edge for IO interrupt +// @param digioConfig* p - pointer to configuration structure for IO pin +// edge - HAL_DIGIO_INT_FALLING_EDGE or HAL_DIGIO_INT_RISING_EDGE +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +// Comment: all pins on port are configured at same time +uint8 halDigioIntSetEdge(const digioConfig *p, uint8 edge) +{ + switch (edge) + { + case HAL_DIGIO_INT_FALLING_EDGE: + switch (p->port) + { + case 0: + PICTL |= PICTL_P0ICON_BM; // set P0ICON high + break; + case 1: + PICTL |= PICTL_P1ICON_BM; // set P1ICON high + break; + case 2: + PICTL |= PICTL_P2ICON_BM; // set P2ICON high + break; + default: + return (HAL_DIGIO_ERROR); + } + break; + + case HAL_DIGIO_INT_RISING_EDGE: + switch (p->port) + { + case 0: + PICTL &= ~PICTL_P0ICON_BM; // set P0ICON low + break; + case 1: + PICTL &= ~PICTL_P1ICON_BM; // set P0ICON low + break; + case 2: + PICTL &= ~PICTL_P2ICON_BM; // set P0ICON low + break; + default: + return (HAL_DIGIO_ERROR); + } + break; + + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn port0_ISR +// @brief ISR framework for P0 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port0_ISR, P0INT_VECTOR) +{ + register uint8 i; + P0IF = 0; + if (P0IFG) + { + for (i = 0; i < 8; i++) + { + register const uint8 pinmask = 1 << i; + if (P0IFG &pinmask) + { + if (port0_isr_tbl[i] != 0) + { + (*port0_isr_tbl[i])(); + } + P0IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} + +//------------------------------------------------------------------- +// @fn port1_ISR +// @brief ISR framework for P0 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port1_ISR, P1INT_VECTOR) +{ + register uint8 i; + P1IF = 0; + if (P1IFG) + { + for (i = 0; i < 8; i++) + { + register const uint8 pinmask = 1 << i; + if (P1IFG &pinmask) + { + if (port1_isr_tbl[i] != 0) + { + (*port1_isr_tbl[i])(); + } + P1IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} + +//------------------------------------------------------------------- +// @fn port2_ISR +// @brief ISR framework for P2 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port2_ISR, P2INT_VECTOR) +{ + register uint8 i; + P2IF = 0; + if (P2IFG) + { + for (i = 0; i < 5; i++) + { + register const uint8 pinmask = 1 << i; + if (P2IFG &pinmask) + { + if (port2_isr_tbl[i] != 0) + { + (*port2_isr_tbl[i])(); + } + P2IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.h new file mode 100644 index 0000000..e851f23 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_digio.h @@ -0,0 +1,46 @@ +//------------------------------------------------------------------- +// Filename: hal_digio.h +// Description: HAL digital IO functionality +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +enum +{ + HAL_DIGIO_INPUT, HAL_DIGIO_OUTPUT +}; + +enum +{ + HAL_DIGIO_INT_FALLING_EDGE, HAL_DIGIO_INT_RISING_EDGE +}; + +#define HAL_DIGIO_OK 0 +#define HAL_DIGIO_ERROR (~0) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + uint8 port; // port number + uint8 pin; // pin number + uint8 pin_bm; // pin bitmask + uint8 dir; // direction (input or output) + uint8 initval; // initial value +} digioConfig; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +uint8 halDigioConfig(const digioConfig *p); +uint8 halDigioClear(const digioConfig *p); +uint8 halDigioToggle(const digioConfig *p); +uint8 halDigioSet(const digioConfig *p); +uint8 halDigioGet(const digioConfig *p); + +uint8 halDigioIntConnect(const digioConfig *p, ISR_FUNC_PTR func); +uint8 halDigioIntEnable(const digioConfig *p); +uint8 halDigioIntDisable(const digioConfig *p); +uint8 halDigioIntClear(const digioConfig *p); +uint8 halDigioIntSetEdge(const digioConfig *p, uint8 edge); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.c new file mode 100644 index 0000000..29e52ce --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: hal_int.c +// Description: HAL interrupt control +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halIntOn +// @brief Enable global interrupts. +// @param none +// @return none +//------------------------------------------------------------------- +void halIntOn(void) +{ + HAL_INT_ON(); +} + +//------------------------------------------------------------------- +// @fn halIntOff +// @brief Turns global interrupts off. +// @param none +// @return none +//------------------------------------------------------------------- +void halIntOff(void) +{ + HAL_INT_OFF(); +} + +//------------------------------------------------------------------- +// @fn halIntLock +// @brief Turns global interrupts off and returns current interrupt state. +// Should always be used together with halIntUnlock(). +// @param none +// @return uint16 - current interrupt state +//------------------------------------------------------------------- +uint16 halIntLock(void) +{ + istate_t key; + HAL_INT_LOCK(key); + return (key); +} + +//------------------------------------------------------------------- +// @fn halIntUnlock +// @brief Set interrupt state back to the state it had before calling halIntLock(). +// Should always be used together with halIntLock(). +// @param key +// @return none +//------------------------------------------------------------------- +void halIntUnlock(uint16 key) +{ + HAL_INT_UNLOCK(key); +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.h new file mode 100644 index 0000000..648e6d3 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_int.h @@ -0,0 +1,21 @@ +//------------------------------------------------------------------- +// Filename: hal_int.h +// Description: HAL interrupt control header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_INT_ON(x) st( EA = 1; ) +#define HAL_INT_OFF(x) st( EA = 0; ) +#define HAL_INT_LOCK(x) st( (x) = EA; EA = 0; ) +#define HAL_INT_UNLOCK(x) st( EA = (x); ) + +typedef unsigned short istate_t; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halIntOn(void); +void halIntOff(void); +uint16 halIntLock(void); +void halIntUnlock(uint16 key); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.c new file mode 100644 index 0000000..5054eb1 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.c @@ -0,0 +1,116 @@ +//------------------------------------------------------------------- +// Filename: hal_mcu.c +// Description: +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_clock.h" +#include "hal_mcu.h" +#include "hal_board.h" + +//------------------------------------------------------------------- +// @fn halMcuInit +// @brief Set Main Clock source to XOSC +// @param none +// @return none +//------------------------------------------------------------------- +void halMcuInit(void) +{ + clockSetMainSrc(CLOCK_SRC_XOSC); +} + +//------------------------------------------------------------------- +// @fn halMcuWaitUs +// @brief Busy wait function. Waits the specified number of microseconds. Use +// assumptions about number of clock cycles needed for the various +// instructions. This function assumes a 32 MHz clock. +// NB! This function is highly dependent on architecture and compiler! +// @param uint16 usec - number of microseconds delays +// @return none +//------------------------------------------------------------------- +#pragma optimize=none +void halMcuWaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} + +//------------------------------------------------------------------- +// @fn halMcuWaitMs +// @brief Busy wait function. Waits the specified number of milliseconds. Use +// assumptions about number of clock cycles needed for the various +// instructions. +// NB! This function is highly dependent on architecture and compiler! +// @param uint16 millisec - number of milliseconds delay +// @return none +//------------------------------------------------------------------- +#pragma optimize=none +void halMcuWaitMs(uint16 msec) +{ + while (msec--) + { + halMcuWaitUs(1000); + } +} + +//------------------------------------------------------------------- +// @fn halMcuSetLowPowerMode +// @brief Sets the MCU in a low power mode. Will turn global interrupts on at +// the same time as entering the LPM mode. The MCU must be waken from +// an interrupt (status register on stack must be modified). +// NB! This function is highly dependent on architecture and compiler! +// @param uint8 mode - power mode +// @return none +//------------------------------------------------------------------- +void halMcuSetLowPowerMode(uint8 mode) +{ + char buf[] = "LowPowerMode( )"; + buf[13] = mode + '0'; + // comment: not yet implemented + HAL_ASSERT(buf); +} + + +//------------------------------------------------------------------- +// @fn halMcuReset +// @brief +// Resets the MCU. This utilize the watchdog timer as there is no other way +// for a software reset. The reset will not occur until ~2 ms. +// NB: The function will not return! (hangs until reset) +// Parameters: +// @param void +// @return void +//------------------------------------------------------------------- +void halMcuReset(void) +{ + const uint8 WDT_INTERVAL_MSEC_2 = 0x03; // after ~2 ms + + WDCTL = ((WDCTL &0xFC) | (WDT_INTERVAL_MSEC_2 &0x03)); + // Start watchdog + WDCTL &= ~0x04; // Select watchdog mode + WDCTL |= 0x08; // Enable timer + while (1) + ; + // Halt here until reset +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.h new file mode 100644 index 0000000..6ecb965 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_mcu.h @@ -0,0 +1,17 @@ +//------------------------------------------------------------------- +// Filename: hal_mcu.h +// Description: hal mcu library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halMcuInit(void); +void halMcuWaitUs(uint16 usec); +void halMcuWaitMs(uint16 msec); +void halMcuSetLowPowerMode(uint8 mode); +void halMcuReset(void); +uint8 halMcuGetResetCause(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.c new file mode 100644 index 0000000..d46019d --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.c @@ -0,0 +1,418 @@ +//------------------------------------------------------------------- +// Filename: hal_rf.c +// Description: CC2530 radio interface. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_int.h" +#include "util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// CC2530 RSSI Offset +#define RSSI_OFFSET 73 + +// Various radio settings +#define AUTO_ACK 0x20 +#define AUTO_CRC 0x40 + +// TXPOWER values +#define CC2530_TXPOWER_MIN_3_DBM 0xB5 +#define CC2530_TXPOWER_0_DBM 0xD5 +#define CC2530_TXPOWER_4_DBM 0xF5 + +#define HAL_RF_TXPOWER_MIN_3_DBM 0 +#define HAL_RF_TXPOWER_0_DBM 1 +#define HAL_RF_TXPOWER_4_DBM 2 + +// RF interrupt flags +#define IRQ_TXDONE 0x02 +#define IRQ_RXPKTDONE 0x40 + +// Selected strobes +#define ISRXON() st(RFST = 0xE3;) +#define ISTXON() st(RFST = 0xE9;) +#define ISTXONCCA() st(RFST = 0xEA;) +#define ISRFOFF() st(RFST = 0xEF;) +#define ISFLUSHRX() st(RFST = 0xEC;) +#define ISFLUSHTX() st(RFST = 0xEE;) + + + +//------------------------------------------------------------------- +// GLOBAL DATA +//------------------------------------------------------------------- + +static const menuItem_t pPowerSettings[] = +{ + "-3dBm", HAL_RF_TXPOWER_MIN_3_DBM, "0dBm", HAL_RF_TXPOWER_0_DBM, "4dBm", HAL_RF_TXPOWER_4_DBM +}; + +const menu_t powerMenu = +{ + pPowerSettings, N_ITEMS(pPowerSettings) +}; + +//------------------------------------------------------------------- +// LOCAL DATA +//------------------------------------------------------------------- + +static ISR_FUNC_PTR pfISR = NULL; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halRfInit +// @brief Power up, sets default tuning settings, enables autoack, enables random generator. +// @param none +// @return SUCCESS always (for interface compatibility) +//------------------------------------------------------------------- +uint8 halRfInit(void) +{ + // Enable auto ack and auto crc + FRMCTRL0 |= (AUTO_ACK | AUTO_CRC); + + // Write non-default register values + // Tuning settings + TXPOWER = CC2530_TXPOWER_0_DBM; // TX output power ~0 dBm + + // Recommended RX settings + TXFILTCFG = 0x09; + AGCCTRL1 = 0x15; + FSCAL1 = 0x00; + + // Enable random generator -> Not implemented yet + + // Enable CC2591 with High Gain Mode -> Not implemented yet + + // Enable RX interrupt + halRfEnableRxInterrupt(); + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn halRfGetChipId +// @brief Get chip id +// @param none +// @return uint8 - result +//------------------------------------------------------------------- +uint8 halRfGetChipId(void) +{ + return CHIPID; +} + +//------------------------------------------------------------------- +// @fn halRfGetChipVer +// @brief Get chip version +// @param none +// @return uint8 - result +//------------------------------------------------------------------- +uint8 halRfGetChipVer(void) +{ + // return major revision (4 upper bits) + return CHVER >> 4; +} + +//------------------------------------------------------------------- +// @fn halRfGetRssiOffset +// @brief Return RSSI Offset +// @param none +// @return uint8 - RSSI offset +//------------------------------------------------------------------- +uint8 halRfGetRssiOffset(void) +{ + return RSSI_OFFSET; +} + +//------------------------------------------------------------------- +// @fn halRfSetChannel +// @brief Set RF channel in the 2.4GHz band. The Channel must be in the range 11-26, +// 11= 2005 MHz, channel spacing 5 MHz. +// @param channel - logical channel number +// @return none +//------------------------------------------------------------------- +void halRfSetChannel(uint8 channel) +{ + FREQCTRL = (MIN_CHANNEL + (channel - MIN_CHANNEL) *CHANNEL_SPACING); +} + +//------------------------------------------------------------------- +// @fn halRfSetShortAddr +// @brief Write short address to chip +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSetShortAddr(uint16 shortAddr) +{ + SHORT_ADDR0 = LO_UINT16(shortAddr); + SHORT_ADDR1 = HI_UINT16(shortAddr); +} + +//------------------------------------------------------------------- +// @fn halRfSetPanId +// @brief Write PAN Id to chip +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSetPanId(uint16 panId) +{ + PAN_ID0 = LO_UINT16(panId); + PAN_ID1 = HI_UINT16(panId); +} + + +//------------------------------------------------------------------- +// @fn halRfSetPower +// @brief Set TX output power +// @param uint8 power - power level: TXPOWER_MIN_4_DBM, TXPOWER_0_DBM, TXPOWER_4_DBM +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfSetTxPower(uint8 power) +{ + uint8 n; + + switch (power) + { + case HAL_RF_TXPOWER_MIN_3_DBM: + n = CC2530_TXPOWER_MIN_3_DBM; + break; + case HAL_RF_TXPOWER_0_DBM: + n = CC2530_TXPOWER_0_DBM; + break; + case HAL_RF_TXPOWER_4_DBM: + n = CC2530_TXPOWER_4_DBM; + break; + + default: + return FAILED; + } + + // Set TX power + TXPOWER = n; + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn halRfWriteTxBuf +// @brief Write to TX buffer +// @param uint8* pData - buffer to write +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfWriteTxBuf(uint8 *pData, uint8 length) +{ + uint8 i; + + ISFLUSHTX(); // Making sure that the TX FIFO is empty. + + RFIRQF1 = ~IRQ_TXDONE; // Clear TX done interrupt + + // Insert data + for (i = 0; i < length; i++) + { + RFD = pData[i]; + } +} + + +//------------------------------------------------------------------- +// @fn halRfAppendTxBuf +// @brief Write to TX buffer +// @param uint8* pData - buffer to write +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfAppendTxBuf(uint8 *pData, uint8 length) +{ + uint8 i; + + // Insert data + for (i = 0; i < length; i++) + { + RFD = pData[i]; + } +} + +//------------------------------------------------------------------- +// @fn halRfReadRxBuf +// @brief Read RX buffer +// @param uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfReadRxBuf(uint8 *pData, uint8 length) +{ + // Read data + while (length > 0) + { + *pData++ = RFD; + length--; + } +} + + +//------------------------------------------------------------------- +// @fn halRfReadMemory +// @brief Read RF device memory +// @param uint16 addr - memory address +// uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return Number of bytes read +//------------------------------------------------------------------- +uint8 halRfReadMemory(uint16 addr, uint8 *pData, uint8 length) +{ + return 0; +} + + +//------------------------------------------------------------------- +// @fn halRfWriteMemory +// @brief Write RF device memory +// @param uint16 addr - memory address +// uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return Number of bytes written +//------------------------------------------------------------------- +uint8 halRfWriteMemory(uint16 addr, uint8 *pData, uint8 length) +{ + return 0; +} + +//------------------------------------------------------------------- +// @fn halRfTransmit +// @brief Transmit frame with Clear Channel Assessment. +// @param none +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfTransmit(void) +{ + uint8 status; + + ISTXON(); // Sending + + // Waiting for transmission to finish + while (!(RFIRQF1 &IRQ_TXDONE)) + ; + + RFIRQF1 = ~IRQ_TXDONE; + status = SUCCESS; + + return status; +} + +//------------------------------------------------------------------- +// @fn halRfReceiveOn +// @brief Turn receiver on +// @param none +// @return none +//------------------------------------------------------------------- +void halRfReceiveOn(void) +{ + ISFLUSHRX(); // Making sure that the TX FIFO is empty. + ISRXON(); +} + +//------------------------------------------------------------------- +// @fn halRfReceiveOff +// @brief Turn receiver off +// @param none +// @return none +//------------------------------------------------------------------- +void halRfReceiveOff(void) +{ + ISRFOFF(); + ISFLUSHRX(); // Making sure that the TX FIFO is empty. +} + + +//------------------------------------------------------------------- +// @fn halRfDisableRxInterrupt +// @brief Clear and disable RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfDisableRxInterrupt(void) +{ + // disable RXPKTDONE interrupt + RFIRQM0 &= ~BV(6); + // disable general RF interrupts + IEN2 &= ~BV(0); +} + + +//------------------------------------------------------------------- +// @fn halRfEnableRxInterrupt +// @brief Enable RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfEnableRxInterrupt(void) +{ + // enable RXPKTDONE interrupt + RFIRQM0 |= BV(6); + // enable general RF interrupts + IEN2 |= BV(0); +} + +//------------------------------------------------------------------- +// @fn halRfRxInterruptConfig +// @brief Configure RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfRxInterruptConfig(ISR_FUNC_PTR pf) +{ + uint8 x; + HAL_INT_LOCK(x); + pfISR = pf; + HAL_INT_UNLOCK(x); +} + + +//------------------------------------------------------------------- +// @fn halRfWaitTransceiverReady +// @brief Wait until the transciever is ready (SFD inactive). +// @param none +// @return none +//------------------------------------------------------------------- +void halRfWaitTransceiverReady(void) +{ + // Wait for SFD not active and TX_Active not active + while (FSMSTAT1 &(BV(1) | BV(5))) + ; +} + + +//------------------------------------------------------------------- +// @fn rfIsr +// @brief Interrupt service routine that handles RFPKTDONE interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(rfIsr, RF_VECTOR) +{ + uint8 x; + + HAL_INT_LOCK(x); + + if (RFIRQF0 &IRQ_RXPKTDONE) + { + if (pfISR) + { + (*pfISR)(); // Execute the custom ISR + } + S1CON = 0; // Clear general RF interrupt flag + RFIRQF0 &= ~IRQ_RXPKTDONE; // Clear RXPKTDONE interrupt + } + HAL_INT_UNLOCK(x); +} + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.h new file mode 100644 index 0000000..845b908 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------- +// Filename: hal_rf.h +// Description: HAL radio interface header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Chip ID's +#define HAL_RF_CHIP_ID_CC1100 0x00 +#define HAL_RF_CHIP_ID_CC1110 0x01 +#define HAL_RF_CHIP_ID_CC1111 0x11 +#define HAL_RF_CHIP_ID_CC2420 0x02 +#define HAL_RF_CHIP_ID_CC2500 0x80 +#define HAL_RF_CHIP_ID_CC2510 0x81 +#define HAL_RF_CHIP_ID_CC2511 0x91 +#define HAL_RF_CHIP_ID_CC2550 0x82 +#define HAL_RF_CHIP_ID_CC2520 0x84 +#define HAL_RF_CHIP_ID_CC2430 0x85 +#define HAL_RF_CHIP_ID_CC2431 0x89 +#define HAL_RF_CHIP_ID_CC2530 0xA5 +#define HAL_RF_CHIP_ID_CC2531 0xB5 +#define HAL_RF_CHIP_ID_CC2540 0x8D + +// IEEE 802.15.4 defined constants (2.4 GHz logical channels) +#define MIN_CHANNEL 11 // 2405 MHz +#define MAX_CHANNEL 26 // 2480 MHz +#define CHANNEL_SPACING 5 // MHz + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +// Generic RF interface +uint8 halRfInit(void); +uint8 halRfSetTxPower(uint8 power); +uint8 halRfTransmit(void); + +uint8 halRfGetChipId(void); +uint8 halRfGetChipVer(void); +uint8 halRfGetRssiOffset(void); + +void halRfWriteTxBuf(uint8 *pData, uint8 length); +void halRfReadRxBuf(uint8 *pData, uint8 length); +void halRfWaitTransceiverReady(void); +uint8 halRfReadMemory(uint16 addr, uint8 *pData, uint8 length); +uint8 halRfWriteMemory(uint16 addr, uint8 *pData, uint8 length); + +void halRfReceiveOn(void); +void halRfReceiveOff(void); +void halRfDisableRxInterrupt(void); +void halRfEnableRxInterrupt(void); +void halRfRxInterruptConfig(ISR_FUNC_PTR pfISR); + +// IEEE 802.15.4 specific interface +void halRfSetChannel(uint8 channel); +void halRfSetShortAddr(uint16 shortAddr); +void halRfSetPanId(uint16 PanId); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.c new file mode 100644 index 0000000..480368e --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.c @@ -0,0 +1,393 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_security.c +// Description: CC2530 CCM security +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_rf.h" +#include "hal_rf_security.h" +#include "util.h" +#include + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Staring AES operation +#define AES_START() st (ENCCS |= 0x01;) + +// Setting the mode of the AES operation +#define AES_SET_MODE(mode) st( ENCCS &= ~0x70; ENCCS |= mode; ) + +// Starting or stopping encryption or decryption +#define AES_SET_OPERATION(op) st( ENCCS = (ENCCS & ~0x07) | op; ) + +// Poll the AES ready bit +#define AES_RDY() ( ENCCS & 8) + +// Encryption mode +#define AES_MODE_CBC 0x00 +#define AES_MODE_CFB 0x10 +#define AES_MODE_OFB 0x20 +#define AES_MODE_CTR 0x30 +#define AES_MODE_ECB 0x40 +#define AES_MODE_CBCMAC 0x50 + +// Operations +#define AES_ENCRYPT 0x00 +#define AES_DECRYPT 0x02 +#define AES_LOAD_KEY 0x04 +#define AES_LOAD_IV 0x06 + +// Convert MIC to MIC length +#define MIC_2_MICLEN(m) ( BV((m&3)+1) & ~3 ) + +//------------------------------------------------------------------- +// LOCAL DATA +//------------------------------------------------------------------- +static uint8 nonceTx[16]; +static uint8 nonceRx[16]; +static uint8 IV[16]; +static uint8 cipherText[128]; +static uint8 buf[128]; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector); +static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector); +static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm); + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +extern void halRfAppendTxBuf(uint8 *pData, uint8 length); + +//------------------------------------------------------------------- +// @fn halRfSecurityInit +// @brief Security init. Write nonces and key to chip. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSecurityInit(uint8 *pKey, uint8 *pNonceRx, uint8 *pNonceTx) +{ + uint8 i; + + AES_SET_OPERATION(AES_LOAD_KEY); + + // Starting loading of key or vector. + AES_START(); + + // Store key and nonces + for (i = 0; i < NONCE_LENGTH; i++) + { + nonceRx[i] = *pNonceRx; + nonceTx[i] = *pNonceTx; + pNonceTx++; + pNonceRx++; + ENCDI = *pKey++; + } +} + +//------------------------------------------------------------------- +// @fn halRfWriteTxBufSecure +// @brief Encrypt and authenticate plaintext then fill TX buffer +// @param uint8* data - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// uint8 c - number of bytes to encrypt +// uint8 f - number of bytes to authenticate +// uint8 m - integrity code (m=1,2,3 gives lenght of integrity +// field l(m)= 4,8,16) +// @return none +//------------------------------------------------------------------- +void halRfWriteTxBufSecure(uint8 *pPkt, uint8 length, uint8 c, uint8 f, uint8 m) +{ + uint8 i, lm; + + // Generate B(i) vector + memset(buf, 0, sizeof(buf)); + lm = MIC_2_MICLEN(m); + i = generateAuthData(c, nonceTx, pPkt + 1, f, lm); + + // Calculate CBC-MAC value + memset(IV, 0, sizeof(IV)); + halAesEncrypt(AES_MODE_CBCMAC, buf, i, cipherText, IV); + + // Encrypt authentication tag + memcpy(IV, nonceTx, 16); // Use nonce as IV + IV[0] = 0x01; // Flag for CTR + IV[15] = 0; // Counter= 0 + + memcpy(buf, cipherText, 16); + halAesEncrypt(AES_MODE_CTR, buf, 16, cipherText, IV); + + // Encrypt plaintext + memset(buf + i, 0, 16-(i &0xf)); // Zero padding + IV[15] = 1; // Counter= 1 + halAesEncrypt(AES_MODE_CTR, &buf[i - c], c, &cipherText[i - c], IV); + + // Fill TX buffer + halRfWriteTxBuf(pPkt, 1); // Length byte + halRfAppendTxBuf(pPkt + 1, f); // MPDU + halRfAppendTxBuf(&cipherText[i - c], c); // Payload + halRfAppendTxBuf(cipherText, lm); // MIC +} + +//------------------------------------------------------------------- +// @fn halRfReadRxBufSecure +// @brief Decrypts and reverse authenticates with CCM then reads out received frame +// @param uint8* pPkt - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// uint8 encrLength - number of bytes to decrypt +// uint8 authLength - number of bytes to reverse authenticate +// uuint8 m - ets length of integrity code (m=1,2,3 gives lenght of integrity field 4,8,16) +// @return SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfReadRxBufSecure(uint8 *data, uint8 length, uint8 c, uint8 f, uint8 m) +{ + uint8 lm, l, i; + + // Get received packet + halRfReadRxBuf(data, length); + + // Update RX nonce; frame counter + nonceRx[12] = data[10]; + nonceRx[11] = data[11]; + nonceRx[10] = data[12]; + nonceRx[9] = data[13]; + // Update RX nonce; source address + nonceRx[7] = data[8]; + nonceRx[8] = data[7]; + + // Decrypt authentication tag + memcpy(IV, nonceRx, 16); + IV[15] = 0; // Counter= 0 + IV[0] = 0x01; // Flag field for decryption + lm = MIC_2_MICLEN(m); // Length of authentication tag + + halAesDecrypt(AES_MODE_CTR, data + c + f, lm, buf, IV); + memcpy(data + c + f, buf, lm); + + // Prepare payload for decryption + memcpy(cipherText, data + f, c); + + // Zero padding + l = (c &0x0f) == 0 ? c : (c &0xf0) + 0x10; + for (i = c; i < l; i++) + { + cipherText[i] = 0x00; + } + + // Decrypt payload + IV[15] = 1; // Counter= 1 + halAesDecrypt(AES_MODE_CTR, cipherText, c, buf, IV); + memcpy(data + f, buf, c); + + // Generate B(i) vector + memset(buf, 0, sizeof(buf)); + i = generateAuthData(c, nonceRx, data, f, lm); + + // Calculate CBC-MAC value + memset(IV, 0, sizeof(IV)); + halAesEncrypt(AES_MODE_CBCMAC, buf, i, cipherText, IV); + + // Check authentication data + return memcmp(cipherText, data + c + f, lm) == 0 ? SUCCESS : FAILED; +} + +//------------------------------------------------------------------- +// @fn halRfIncNonceTx +// @brief Increments frame counter field of stored nonce TX +// @param none +// @return none +//------------------------------------------------------------------- +void halRfIncNonceTx(void) +{ + uint32 *pCount; + + pCount = (uint32*) &nonceTx[9]; + utilReverseBuf((uint8*)pCount, 4); + (*pCount)++; + utilReverseBuf((uint8*)pCount, 4); +} + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS + +//------------------------------------------------------------------- +static void halAesLoadBlock(uint8 *pData, uint8 op) +{ + uint8 i; + + // Set operation + AES_SET_OPERATION(op); + // Starting loading of key or vector. + AES_START(); + + // loading the data (key or vector) + for (i = 0; i < 16; i++) + { + ENCDI = pData[i]; + } + + return ; +} + +//------------------------------------------------------------------- +static void halAesOperation(uint8 oper, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + uint16 i; + uint8 j, k; + uint8 mode; + uint16 nbrOfBlocks; + uint16 convertedBlock; + + nbrOfBlocks = length / 0x10; + + if ((length % 0x10) != 0) + { + // length not multiplum of 16, convert one block extra with zeropadding + nbrOfBlocks++; + } + + // Loading the IV. + halAesLoadBlock(pInitVector, AES_LOAD_IV); + + // Start the operation + AES_SET_OPERATION(oper); + + // Getting the operation mode. + mode = ENCCS &0x70; + + for (convertedBlock = 0; convertedBlock < nbrOfBlocks; convertedBlock++) + { + // Starting the conversion. + AES_START(); + + i = convertedBlock * 16; + // Counter, Output Feedback and Cipher Feedback operates on 4 bytes and not 16 bytes. + if ((mode == AES_MODE_CFB) || (mode == AES_MODE_OFB) || (mode == AES_MODE_CTR)) + { + + for (j = 0; j < 4; j++) + { + // Writing the input data with zero-padding + for (k = 0; k < 4; k++) + { + ENCDI = ((i + 4 * j + k < length) ? pDataIn[i + 4 * j + k]: 0x00); + } + + // Read out data for every 4th byte + for (k = 0; k < 4; k++) + { + pDataOut[i + 4 * j + k] = ENCDO; + } + + } + } + else if (mode == AES_MODE_CBCMAC) + { + // Writing the input data with zero-padding + for (j = 0; j < 16; j++) + { + ENCDI = ((i + j < length) ? pDataIn[i + j]: 0x00); + } + // The last block of the CBC-MAC is computed by using CBC mode. + if (convertedBlock == nbrOfBlocks - 2) + { + AES_SET_MODE(AES_MODE_CBC); + // wait for data ready + halMcuWaitUs(1); + } + // The CBC-MAC does not produce an output on the n-1 first blocks + // only the last block is read out + else if (convertedBlock == nbrOfBlocks - 1) + { + + // wait for data ready + halMcuWaitUs(1); + for (j = 0; j < 16; j++) + { + pDataOut[j] = ENCDO; + } + } + } // ECB or CBC + else + { + // Writing the input data with zero-padding + for (j = 0; j < 16; j++) + { + ENCDI = ((i + j < length) ? pDataIn[i + j]: 0x00); + } + + // wait for data ready + halMcuWaitUs(1); + + // Read out data + for (j = 0; j < 16; j++) + { + pDataOut[i + j] = ENCDO; + } + } + } +} + +//------------------------------------------------------------------- +static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + AES_SET_MODE(mode); + halAesOperation(AES_ENCRYPT, pDataIn, length, pDataOut, pInitVector); +} + +//------------------------------------------------------------------- +static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + AES_SET_MODE(mode); + halAesOperation(AES_DECRYPT, pDataIn, length, pDataOut, pInitVector); +} + +//------------------------------------------------------------------- +static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm) +{ + uint8 i, j, l; + + // Prepare input for CBC-MAC, B0, Bi.... Bn + memcpy(buf, pIv, 16); // B0 + + // Create flag + buf[0] = 0x01; // L'= L - 1 (L=2) + if (f > 0) + { + buf[0] |= 0x40; + } + // Adata= f > 0 + buf[0] |= ((lm - 2) / 2) << 3; // M'= (lm-2) / 2; + + buf[14] = 0x00; + buf[15] = c; + + // Add authentication data + buf[16] = 0; + buf[17] = f; + memcpy(&buf[18], pData, f); + + // .... zero padding + i = 18+f; + l = (i &0x0f) == 0 ? i : (i &0xf0) + 0x10; + while (i < l) + { + buf[i++] = 0x00; + } + + // Add command payload + j = 0; + while (i < l + c) + { + buf[i++] = pData[f + j]; + j++; + } + + return i; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.h new file mode 100644 index 0000000..e05c6ca --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_security.h @@ -0,0 +1,21 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_security.h +// Description: HAL radio interface header file for security operations. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// IEEE 802.15.4 security (AES-128) +#define KEY_LENGTH 16 +#define NONCE_LENGTH 16 + +#define SECURITY_CONTROL 0x06 // MIC-64 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +// CCM security interface, IEEE 802.15.4 +void halRfSecurityInit(uint8 *key, uint8 *nonceRx, uint8 *nonceTx); +uint8 halRfReadRxBufSecure(uint8 *pData, uint8 length, uint8 encrLength, uint8 authLength, uint8 m); +void halRfWriteTxBufSecure(uint8 *pData, uint8 length, uint8 encrLength, uint8 authLength, uint8 m); +void halRfIncNonceTx(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.c new file mode 100644 index 0000000..0487a75 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_util.c +// Description: Support for anergy detection applications. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_rf_util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halSampleED +// @brief Sample Energy Detect +// @param uint8 channel - channel between 11 and 26 +// uint16 sampleTime - sample time in us +// @return int8 - sampled RSSI value +//------------------------------------------------------------------- +int8 halSampleED(uint8 channel, uint16 sampleTime) +{ + int8 rssi = 0; + + // Set channel + halRfSetChannel(channel); + + // Set RX on + halRfReceiveOn(); + while (!RSSISTAT) + ; + + // Enable energy scan mode, using peak signal strength + FRMCTRL0 |= 0x10; + + // Spend sampleTime us accumulating the peak RSSI value + halMcuWaitUs(sampleTime); + rssi = RSSI; + + // Exit the current channel + halRfReceiveOff(); + // Disable ED scan mode + FRMCTRL0 &= ~0x10; + + return rssi; +} + +//------------------------------------------------------------------- +// @fn halSetRxScanMode +// @brief Set chip in RX scanning mode +// @param none +// @return none +//------------------------------------------------------------------- +void halSetRxScanMode(void) +{ + // Infinite RX mode (disables symbol search) + FRMCTRL0 = 0x0C; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.h new file mode 100644 index 0000000..1a496cd --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_rf_util.h @@ -0,0 +1,10 @@ +//------------------------------------------------------------------- +// Filename: hal_util_rf.h +// Description: +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +int8 halSampleED(uint8 channel, uint16 sampleTime); +void halSetRxScanMode(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.c new file mode 100644 index 0000000..840aefc --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.c @@ -0,0 +1,41 @@ +#include + +void SET_POWER_MODE(unsigned char mode) +{ + if(mode == 0) + SLEEPCMD &= ~0x03; + else + if(mode == 3) + SLEEPCMD |= 0x03; + else + { + SLEEPCMD &= ~0x03; + SLEEPCMD |= mode; + } + + PCON |= 0x01; + asm("NOP"); +} + +void Set_ST_Period(unsigned char sec) +{ + unsigned long sleepTimer = 0; + + sleepTimer |= ST0; + sleepTimer |= (unsigned long)ST1 << 8; + sleepTimer |= (unsigned long)ST2 << 16; + + sleepTimer += ((unsigned long)sec * (unsigned long)32768); + + ST2 = (unsigned char)(sleepTimer >> 16); + ST1 = (unsigned char)(sleepTimer >> 8); + ST0 = (unsigned char) sleepTimer; +} + +void Init_SLEEP_TIMER(void) +{ + EA = 1; + STIE = 1; + STIF = 0; +} + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.h new file mode 100644 index 0000000..83b2601 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_sleep.h @@ -0,0 +1,10 @@ +#include + +#define CRYSTAL 0 +#define RC 1 + +void SET_MAIN_CLOCK_SOURCE(unsigned char source); +void SET_LOW_CLOCK(unsigned char source); +void SET_POWER_MODE(unsigned char mode); +void Set_ST_Period(unsigned char sec); +void Init_SLEEP_TIMER(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.c new file mode 100644 index 0000000..8b8ab4f --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.c @@ -0,0 +1,130 @@ +//------------------------------------------------------------------- +// Filename: hal_timer_32k.c +// Description: hal 32KHz timer +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_timer_32k.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +static ISR_FUNC_PTR fptr; + +//------------------------------------------------------------------- +// @fn halTimer32kInit +// @brief Set up timer 1 to generate an interrupt every "cycles" 32768 Hz +// clock cycles. +// Use halTimerIntConnect() to connect an ISR to the interrupt. +// @param uint16 cycles - Number of cycles between interrupt +// (does not support larger cycles value than 0xFFFF/8) +// @return none +//------------------------------------------------------------------- +void halTimer32kInit(uint16 cycles) +{ + uint16 compareValue; + + //uint16 clock_divider = ID_0; // Default - don't divide input clock + + // Set prescaler divider value to 128 + T1CTL |= 0x0C; + + // Multiply cycles by 8 to get 32KHz cycles + // 32MHz/128/8 gives approximately 32KHz + if (cycles > 0xFFFF / 8) + { + compareValue = 0xFFFF; + } + else + { + compareValue = cycles * 8; + } + + // Set compare value + T1CC0H = HI_UINT16(compareValue); + T1CC0L = LO_UINT16(compareValue); + #ifndef CC2530_PG1 + // Set conmpare mode with interrupt enabled + T1CCTL0 = 0x44; + #endif + + // clear interrupt pending flag, disable interrupt + #ifdef CC2530_PG1 + T1CTL &= ~0x10; // T1CTL.OVFIF = 0 + #else + T1STAT &= ~0x01; // T1STAT.CH1IF = 0 + #endif + IEN1 &= ~0x02; // IEN1.T1EN = 0 +} + +//------------------------------------------------------------------- +// @fn halTimer32kRestart +// @brief Restart timer 1. The timer is first stopped, then restarted, +// counting up from 0 +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kRestart(void) +{ + T1CTL &= ~0x02; // Stop timer + IEN1 &= ~0x02; // Disable interrupt + + T1CNTL = 0x00; // Reset counter value + + T1CTL |= 0x02; // Start timer + IEN1 |= 0x02; // Enable interrupt +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntConnect +// @brief Connect function to timer interrupt +// @param ISR_FUNC_PTR isr - pointer to function +// @return none +//------------------------------------------------------------------- +void halTimer32kIntConnect(ISR_FUNC_PTR isr) +{ + istate_t key; + HAL_INT_LOCK(key); + fptr = isr; + HAL_INT_UNLOCK(key); +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntEnable +// @brief Enable 32KHz timer interrupt +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kIntEnable(void) +{ + IEN1 |= 0x02; // IEN1.T1EN = 1 + + // Start timer in modulo mode + T1CTL |= 0x02; // T1CTL.MODE = 10 + +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntDisable +// @brief Disable 32KHz timer interrupt +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kIntDisable(void) +{ + IEN1 &= ~0x02; // IEN1.T1EN = 0 +} + +//------------------------------------------------------------------- +// @fn timer32k0_ISR +// @brief ISR framework for the 32KHz timer component +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(T1_ISR, T1_VECTOR) +{ + if (fptr != NULL) + { + (*fptr)(); + } +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.h new file mode 100644 index 0000000..e9d639e --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_timer_32k.h @@ -0,0 +1,17 @@ +//------------------------------------------------------------------- +// Filename: hal_timer_32k.h +// Description: hal 32KHz timer library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +#define TIMER_32K_CLK_FREQ 32768 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halTimer32kInit(uint16 cycles); +void halTimer32kRestart(void); +void halTimer32kIntConnect(ISR_FUNC_PTR isr); +void halTimer32kIntEnable(void); +void halTimer32kIntDisable(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.c new file mode 100644 index 0000000..88d9d68 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.c @@ -0,0 +1,286 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.c +// Description: This file contains the interface to the H/W UART driver. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_board.h" +#include "hal_uart.h" + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_UART_ISR_RX_AVAIL() \ +(uartCfg.rxTail >= uartCfg.rxHead) ? \ +(uartCfg.rxTail - uartCfg.rxHead) : \ +(HAL_UART_ISR_RX_MAX - uartCfg.rxHead + uartCfg.rxTail) + +#define HAL_UART_ISR_TX_AVAIL() \ +(uartCfg.txHead > uartCfg.txTail) ? \ +(uartCfg.txHead - uartCfg.txTail - 1) : \ +(HAL_UART_ISR_TX_MAX - uartCfg.txTail + uartCfg.txHead - 1) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// U0CSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// U0UCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX0IE 0x04 +#define U0RX_TX 0x0C + +#define HAL_UART_PERCFG_BIT 0x01 + +#define HAL_UART_ISR_RX_MAX 128 +#define HAL_UART_ISR_TX_MAX HAL_UART_ISR_RX_MAX + +typedef struct +{ + uint8 rxBuf[HAL_UART_ISR_RX_MAX]; + uint8 rxHead; + volatile uint8 rxTail; + uint8 rxShdw; + + uint8 txBuf[HAL_UART_ISR_TX_MAX]; + volatile uint8 txHead; + uint8 txTail; + uint8 txMT; +} uartCfg_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uartCfg_t uartCfg; + +//------------------------------------------------------------------- +// @fn halUartInit +// @brief Initialize the UART +// @param none +// @return none +//------------------------------------------------------------------- +void halUartInit(uint32 baud) +{ + // UART Configuration + PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O location to P0. + + P0SEL |= U0RX_TX; // Enable Tx and Rx peripheral functions on pins. + ADCCFG &= ~U0RX_TX; // Make sure ADC doesnt use this. + U0CSR = CSR_MODE; // Mode is UART Mode. + U0UCR = UCR_FLUSH; // Flush it. + + // Only supporting subset of baudrate for code size - other is possible. + // 38400 + //U0BAUD = 59; + //U0GCR = 10; + // 9600 + //U0BAUD = 59; + //U0GCR = 8; + //U0BAUD = value_u0baud; + //U0GCR = value_u0gcr; + + switch(baud) + { + case 1200 : + U0BAUD = 59; U0GCR = 5; break; + case 2400 : + U0BAUD = 59; U0GCR = 6; break; + case 4800 : + U0BAUD = 59; U0GCR = 7; break; + case 9600 : + U0BAUD = 59; U0GCR = 8; break; + case 14400 : + U0BAUD = 216; U0GCR = 8; break; + case 19200 : + U0BAUD = 59; U0GCR = 9; break; + case 28800 : + U0BAUD = 216; U0GCR = 9; break; + case 38400 : + U0BAUD = 59; U0GCR = 10; break; + case 57600 : + U0BAUD = 216; U0GCR = 10; break; + case 76800 : + U0BAUD = 59; U0GCR = 11; break; + case 115200 : + U0BAUD = 216; U0GCR = 11; break; + case 230400 : + U0BAUD = 216; U0GCR = 12; break; + default : + U0BAUD = 59; U0GCR = 10; break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + U0UCR = UCR_STOP; + U0CSR |= CSR_RE; + URX0IE = 1; + U0DBUF = 0; // Prime the ISR pump. + + uartCfg.rxHead = 0; + uartCfg.rxTail = 0; + uartCfg.txHead = 0; + uartCfg.txTail = 0; + uartCfg.rxShdw = 0; + uartCfg.txMT = 0; + +} + +//------------------------------------------------------------------- +// @fn halUartRead +// @brief Read a buffer from the UART +// @param buf - valid data buffer at least 'len' bytes in size +// len - max length number of bytes to copy to 'buf' +// @return length of buffer that was read +//------------------------------------------------------------------- +uint16 halUartRead(uint8 *buf, uint16 len) +{ + uint16 cnt = 0; + while ((uartCfg.rxHead != uartCfg.rxTail) && (cnt < len)) + { + *buf++ = uartCfg.rxBuf[uartCfg.rxHead++]; + if (uartCfg.rxHead >= HAL_UART_ISR_RX_MAX) + { + uartCfg.rxHead = 0; + } + cnt++; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartWrite +// @brief Write a buffer to the UART. +// @param buf - pointer to the buffer that will be written, not freed +// len - length of +// @return length of the buffer that was sent +//------------------------------------------------------------------- +uint16 halUartWrite(uint8 *buf, uint16 len) +{ + uint16 cnt; + + // Accept "all-or-none" on write request. + if (HAL_UART_ISR_TX_AVAIL() < len) + { + return 0; + } + + for (cnt = 0; cnt < len; cnt++) + { + uartCfg.txBuf[uartCfg.txTail] = *buf++; + uartCfg.txMT = 0; + + if (uartCfg.txTail >= HAL_UART_ISR_TX_MAX - 1) + { + uartCfg.txTail = 0; + } + else + { + uartCfg.txTail++; + } + + // Keep re-enabling ISR as it might be keeping up with this loop due to other ints. + IEN2 |= UTX0IE; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartRxLen() +// @brief Calculate Rx Buffer length - the number of bytes in the buffer. +// @param none +// @return length of current Rx Buffer +//------------------------------------------------------------------- +uint16 halUartRxLen(void) +{ + return HAL_UART_ISR_RX_AVAIL(); +} + +//------------------------------------------------------------------- +// @fn halUartSuspend +// @brief Suspend UART hardware before entering PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUartSuspend(void) +{ + U0CSR &= ~CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartResume +// @brief Resume UART hardware after exiting PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUartResume(void) +{ + U0UCR |= UCR_FLUSH; + U0CSR |= CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartRxIsr +// @brief UART Receive Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart0RxIsr, URX0_VECTOR) +{ + uint8 tmp = U0DBUF; + uartCfg.rxBuf[uartCfg.rxTail] = tmp; + + // Re-sync the shadow on any 1st byte received. + if (uartCfg.rxHead == uartCfg.rxTail) + { + uartCfg.rxShdw = ST0; + } + + if (++uartCfg.rxTail >= HAL_UART_ISR_RX_MAX) + { + uartCfg.rxTail = 0; + } +} + +//------------------------------------------------------------------- +// @fn halUartTxIsr +// @brief UART Transmit Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart0TxIsr, UTX0_VECTOR) +{ + if (uartCfg.txHead == uartCfg.txTail) + { + IEN2 &= ~UTX0IE; + uartCfg.txMT = 1; + } + else + { + UTX0IF = 0; + U0DBUF = uartCfg.txBuf[uartCfg.txHead++]; + + if (uartCfg.txHead >= HAL_UART_ISR_TX_MAX) + { + uartCfg.txHead = 0; + } + } +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.h new file mode 100644 index 0000000..1dfea8c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.h +// Description: hal UART library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------ + +//------------------------------------------------------------------- +// Initialize UART at the startup +//------------------------------------------------------------------- +void halUartInit(uint32 baud); + +//------------------------------------------------------------------- +// Read a buffer from the UART +//------------------------------------------------------------------- +extern uint16 halUartRead(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Write a buff to the uart +//------------------------------------------------------------------- +extern uint16 halUartWrite(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Return the number of bytes in the Rx buffer +//------------------------------------------------------------------- +extern uint16 halUartRxLen(void); + +//------------------------------------------------------------------- +// Abort UART when entering sleep mode +//------------------------------------------------------------------- +extern void halUartSuspend(void); + +//------------------------------------------------------------------- +// Resume UART after wakeup from sleep +//------------------------------------------------------------------- +extern void halUartResume(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.c new file mode 100644 index 0000000..b281d45 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.c @@ -0,0 +1,282 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.c +// Description: This file contains the interface to the H/W UART driver. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_board.h" +#include "hal_uart1.h" + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_UART1_ISR_RX_AVAIL() \ +(uart1Cfg.rxTail >= uart1Cfg.rxHead) ? \ +(uart1Cfg.rxTail - uart1Cfg.rxHead) : \ +(HAL_UART1_ISR_RX_MAX - uart1Cfg.rxHead + uart1Cfg.rxTail) + +#define HAL_UART1_ISR_TX_AVAIL() \ +(uart1Cfg.txHead > uart1Cfg.txTail) ? \ +(uart1Cfg.txHead - uart1Cfg.txTail - 1) : \ +(HAL_UART1_ISR_TX_MAX - uart1Cfg.txTail + uart1Cfg.txHead - 1) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// U0CSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// U0UCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX1IE 0x08 +#define U1RX_TX 0xC0 + +#define HAL_UART1_PERCFG_BIT 0x02 + +#define HAL_UART1_ISR_RX_MAX 128 +#define HAL_UART1_ISR_TX_MAX HAL_UART1_ISR_RX_MAX + +typedef struct +{ + uint8 rxBuf[HAL_UART1_ISR_RX_MAX]; + uint8 rxHead; + volatile uint8 rxTail; + uint8 rxShdw; + + uint8 txBuf[HAL_UART1_ISR_TX_MAX]; + volatile uint8 txHead; + uint8 txTail; + uint8 txMT; +} uartCfg_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uartCfg_t uart1Cfg; + +//------------------------------------------------------------------- +// @fn halUartInit +// @brief Initialize the UART +// @param none +// @return none +//------------------------------------------------------------------- +void halUart1Init(uint32 baud) +{ + // UART Configuration + PERCFG |= HAL_UART1_PERCFG_BIT; // ÉèÖà UART1 I/O λÖÃÔÚP1¿ÚÉÏ. + + P1SEL |= U1RX_TX; // ʹÄÜ UART1 µÄTx£¨P1.6£©ºÍRx £¨P1.7£©ÍⲿÉ豸¹¦ÄÜÒý½Å. + // ADCCFG &= ~U0RX_TX; // Make sure ADC doesnt use this. + U1CSR = CSR_MODE; // UART ģʽ. + U1UCR = UCR_FLUSH; // Çå³ýµ¥Ôª. + + // Only supporting subset of baudrate for code size - other is possible. + // 38400 + //U1BAUD = 59; + //U1GCR = 10; + //9600 + //U1BAUD = 59; + //U1GCR = 8; + switch(baud) + { + case 2400 : + U1BAUD = 59; U1GCR = 6; break; + case 4800 : + U1BAUD = 59; U1GCR = 7; break; + case 9600 : + U1BAUD = 59; U1GCR = 8; break; + case 14400 : + U1BAUD = 216; U1GCR = 8; break; + case 19200 : + U1BAUD = 59; U1GCR = 9; break; + case 28800 : + U1BAUD = 216; U1GCR = 9; break; + case 38400 : + U1BAUD = 59; U1GCR = 10; break; + case 57600 : + U1BAUD = 216; U1GCR = 10; break; + case 76800 : + U1BAUD = 59; U1GCR = 11; break; + case 115200 : + U1BAUD = 216; U1GCR = 11; break; + case 230400 : + U1BAUD = 216; U1GCR = 12; break; + default : + U1BAUD = 59; U1GCR = 10; break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + U1UCR = UCR_STOP; //ֹͣλΪ¸ßµçƽ£¨1£© + U1CSR |= CSR_RE; //UART1 ½ÓÊÕÆ÷ʹÄÜ + URX1IE = 1; //UART1 ½ÓÊÕÖжÏʹÄÜ + U1DBUF = 0; //Êý¾Ý»º³åÆ÷Çå0 + + uart1Cfg.rxHead = 0; + uart1Cfg.rxTail = 0; + uart1Cfg.txHead = 0; + uart1Cfg.txTail = 0; + uart1Cfg.rxShdw = 0; + uart1Cfg.txMT = 0; + +} + +//------------------------------------------------------------------- +// @fn halUartRead +// @brief Read a buffer from the UART +// @param buf - valid data buffer at least 'len' bytes in size +// len - max length number of bytes to copy to 'buf' +// @return length of buffer that was read +//------------------------------------------------------------------- +uint16 halUart1Read(uint8 *buf, uint16 len) +{ + uint16 cnt = 0; + + while ((uart1Cfg.rxHead != uart1Cfg.rxTail) && (cnt < len)) + { + *buf++ = uart1Cfg.rxBuf[uart1Cfg.rxHead++]; + if (uart1Cfg.rxHead >= HAL_UART1_ISR_RX_MAX) + { + uart1Cfg.rxHead = 0; + } + cnt++; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartWrite +// @brief Write a buffer to the UART. +// @param buf - pointer to the buffer that will be written, not freed +// len - length of +// @return length of the buffer that was sent +//------------------------------------------------------------------- +uint16 halUart1Write(uint8 *buf, uint16 len) +{ + uint16 cnt; + + // Accept "all-or-none" on write request. + if (HAL_UART1_ISR_TX_AVAIL() < len) + { + return 0; + } + + for (cnt = 0; cnt < len; cnt++) + { + uart1Cfg.txBuf[uart1Cfg.txTail] = *buf++; + uart1Cfg.txMT = 0; + + if (uart1Cfg.txTail >= HAL_UART1_ISR_TX_MAX - 1) + { + uart1Cfg.txTail = 0; + } + else + { + uart1Cfg.txTail++; + } + + // Keep re-enabling ISR as it might be keeping up with this loop due to other ints. + IEN2 |= UTX1IE; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartRxLen() +// @brief Calculate Rx Buffer length - the number of bytes in the buffer. +// @param none +// @return length of current Rx Buffer +//------------------------------------------------------------------- +uint16 halUart1RxLen(void) +{ + return HAL_UART1_ISR_RX_AVAIL(); +} + +//------------------------------------------------------------------- +// @fn halUartSuspend +// @brief Suspend UART hardware before entering PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUart1Suspend(void) +{ + U1CSR &= ~CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartResume +// @brief Resume UART hardware after exiting PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUart1Resume(void) +{ + U1UCR |= UCR_FLUSH; + U1CSR |= CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartRxIsr +// @brief UART Receive Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart1RxIsr, URX1_VECTOR) +{ + uint8 tmp = U1DBUF; + uart1Cfg.rxBuf[uart1Cfg.rxTail] = tmp; + + // Re-sync the shadow on any 1st byte received. + if (uart1Cfg.rxHead == uart1Cfg.rxTail) + { + uart1Cfg.rxShdw = ST0; + } + + if (++uart1Cfg.rxTail >= HAL_UART1_ISR_RX_MAX) + { + uart1Cfg.rxTail = 0; + } +} + +//------------------------------------------------------------------- +// @fn halUartTxIsr +// @brief UART Transmit Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart1TxIsr, UTX1_VECTOR) +{ + if (uart1Cfg.txHead == uart1Cfg.txTail) + { + IEN2 &= ~UTX1IE; + uart1Cfg.txMT = 1; + } + else + { + UTX1IF = 0; + U1DBUF = uart1Cfg.txBuf[uart1Cfg.txHead++]; + + if (uart1Cfg.txHead >= HAL_UART1_ISR_TX_MAX) + { + uart1Cfg.txHead = 0; + } + } +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.h new file mode 100644 index 0000000..a528757 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/common/hal_uart1.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.h +// Description: hal UART library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// FUNCTIONS - API +//------------------------------------------------------------------- +// Initialize UART at the startup +//------------------------------------------------------------------- +extern void halUart1Init(uint32 baud); + +//------------------------------------------------------------------- +// Read a buffer from the UART +//------------------------------------------------------------------- +extern uint16 halUart1Read(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Write a buff to the uart +//------------------------------------------------------------------- +extern uint16 halUart1Write(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Return the number of bytes in the Rx buffer +//------------------------------------------------------------------- +extern uint16 halUart1RxLen(void); + +//------------------------------------------------------------------- +// Abort UART when entering sleep mode +//------------------------------------------------------------------- +extern void halUart1Suspend(void); + +//------------------------------------------------------------------- +// Resume UART after wakeup from sleep +//------------------------------------------------------------------- +extern void halUart1Resume(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.c new file mode 100644 index 0000000..de7bc79 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.c @@ -0,0 +1,38 @@ +//------------------------------------------------------------------- +// Filename: dma_ad590.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_ad590.h" + +//------------------------------------------------------------------- +// @fn dma_ad590_Init +// @brief Set up port ad590 ad +// @return none +//------------------------------------------------------------------- +void dma_ad590_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_ad590_GetValue +// @brief Get this ad590 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_ad590_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.h new file mode 100644 index 0000000..e27099c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_ad590.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: dma_ad590.h +// Description: ad590 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_ad590_Init(void); +uint16 dma_ad590_GetValue(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.c new file mode 100644 index 0000000..00a11df --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.c @@ -0,0 +1,193 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_bma.h" + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } +} + +//------------------------------------------------------------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0x70);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); + halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + + M3_I2cWrite(0x70); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + + M3_I2cStart(); + + M3_I2cWrite(0x71); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + + M3_I2cNack(); + M3_I2cStop(); + + return res; +} + +uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; + +void BMA_Pooling(void) +{ + uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + + BMA_XL_Value = M3_ReadEEPROM(0x02); + BMA_XM_Value = M3_ReadEEPROM(0x03); + BMA_YL_Value = M3_ReadEEPROM(0x04); + BMA_YM_Value = M3_ReadEEPROM(0x05); + BMA_ZL_Value = M3_ReadEEPROM(0x06); + BMA_ZM_Value = M3_ReadEEPROM(0x07); + + BMA_X_Value = (BMA_XM_Value << 2) + ((BMA_XL_Value&0xc0)>>6); + BMA_Y_Value = (BMA_YM_Value << 2) + ((BMA_YL_Value&0xc0)>>6); + BMA_Z_Value = (BMA_ZM_Value << 2) + ((BMA_ZL_Value&0xc0)>>6); +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.h new file mode 100644 index 0000000..f5096dc --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_bma.h @@ -0,0 +1,16 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.h +// Description: dma_bma module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +extern uint16 BMA_X_Value; +extern uint16 BMA_Y_Value; +extern uint16 BMA_Z_Value; + +void M3_Init(void); +//void M3_WriteEEPROM(uint8 addr, uint8 ch); +uint8 M3_ReadEEPROM(uint8 addr); + +void BMA_Pooling(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.c new file mode 100644 index 0000000..446351d --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.c @@ -0,0 +1,59 @@ +//------------------------------------------------------------------- +// Filename: hal_dc.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_dc.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halDcStart +// @brief Turn DC on. +// @param uint8 id - led to set +// @return none +//------------------------------------------------------------------- +void halDcStart(uint8 dc_num) +{ + switch (dc_num) + { + case 1: + HAL_DC_START_1(); + break; + case 2: + HAL_DC_START_2(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halDcStop +// @brief Turn LED off. +// @param uint8 id - led to clear +// @return none +//------------------------------------------------------------------- +void halDcStop(uint8 dc_num) +{ + switch (dc_num) + { + case 1: + HAL_DC_STOP_1(); + break; + case 2: + HAL_DC_STOP_2(); + break; + default: + break; + } +} + + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.h new file mode 100644 index 0000000..063d3de --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_dc.h @@ -0,0 +1,8 @@ +//------------------------------------------------------------------- +// Filename: hal_led.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halDcStart(uint8 dc_num); +void halDcStop(uint8 dc_num); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.c new file mode 100644 index 0000000..0e581dd --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.c @@ -0,0 +1,175 @@ +//------------------------------------------------------------------- +// Filename: dma_eeprom.c +// Description: dma_eeprom module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_eeprom.h" + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } +} + +//------------------------------------------------------------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xa0);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); + halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + + M3_I2cWrite(0xa0); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + + M3_I2cStart(); + + M3_I2cWrite(0xa1); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + + M3_I2cNack(); + M3_I2cStop(); + + return res; +} + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.h new file mode 100644 index 0000000..6875a54 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_eeprom.h @@ -0,0 +1,11 @@ +//------------------------------------------------------------------- +// Filename: dma_eeprom.h +// Description: dma_eeprom module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- + +void M3_Init(void); +void M3_WriteEEPROM(uint8 addr, uint8 ch); +uint8 M3_ReadEEPROM(uint8 addr); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.c new file mode 100644 index 0000000..fd2b78c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.c @@ -0,0 +1,64 @@ +//------------------------------------------------------------------- +// Filename: dma_imc.c +// Description: dma_imc library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "dma_imc.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uint16 M1_ImcValue; + +static const digioConfig pinM1_Imc = +{ + HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN, BV(HAL_BOARD_IO_INT_PIN), HAL_DIGIO_INPUT, 0 +}; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +/*static void M1_ImcISR(void) +{ + M1_ImcValue++; +}*/ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M1_Init(void) +{ + M1_ImcValue = 0; + halDigioConfig(&pinM1_Imc); + MCU_IO_INPUT(HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN, MCU_IO_TRISTATE); //MCU_IO_TRISTATE MCU_IO_PULLUP + halDigioIntSetEdge(&pinM1_Imc, HAL_DIGIO_INT_RISING_EDGE); + // halDigioIntConnect(&pinM1_Imc, &M1_ImcISR); +} + +//------------------------------------------------------------------- +uint16 M1_GetValue(void) +{ + M1_ImcValue = HAL_INT_VAL(); + return M1_ImcValue; +} + +//------------------------------------------------------------------- +void M1_Enable(void) +{ + M1_ImcValue = 0; + halDigioIntEnable(&pinM1_Imc); +} + +//------------------------------------------------------------------- +void M1_Disable(void) +{ + halDigioIntDisable(&pinM1_Imc); +} + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.h new file mode 100644 index 0000000..78c234c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_imc.h @@ -0,0 +1,11 @@ +//------------------------------------------------------------------- +// Filename: dma_imc.h +// Description: dma_imc library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M1_Init(void); +uint16 M1_GetValue(void); +void M1_Enable(void); +void M1_Disable(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.c new file mode 100644 index 0000000..80accd1 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.c @@ -0,0 +1,204 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_uart.h" +#include "hal_led.h" +#include "dma_itg.h" + + + +//--------- write a byte and checks the ack signal----------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCK_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } + +} + +//--------- read a byte and checks the ack signal----------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + HAL_E2PROM_SCK_SET(); + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR(); + halMcuWaitUs(2); + } + // MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + // HAL_E2PROM_SDA_SET(); + + HAL_E2PROM_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_E2PROM_SCK_CLR(); + HAL_E2PROM_SDA_SET(); + + return res; +} + +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + + HAL_E2PROM_SCK_SET(); //SCL=1 + + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +void M3_I2cAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_SET(); + HAL_E2PROM_SCK_CLR(); +} + +//------------------------------------------------------------------- +void M3_I2cNAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_CLR(); + HAL_E2PROM_SCK_CLR(); +} + +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1); +} + +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xD2);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); +} + +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + M3_I2cWrite(0xD2); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cStart(); + M3_I2cWrite(0xD3); //Æ÷¼þÉèÖÃΪ¶Á״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + M3_I2cNAck(); + M3_I2cStop(); + + return res; +} + +uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; +uint8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; +uint8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + +void BMA_Pooling(void) +{ + // uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + // uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + uint16 num_x1=0,num_y1=0,num_z1=0; + uint16 num_x=0,num_y=0,num_z=0; + uint8 i = 0; + + for(i=0;i<10;i++) + { + while (!(M3_ReadEEPROM(0x1A) & 0x01)); + + BMA_XM_Value = M3_ReadEEPROM(0x1D); //1d 1b + halMcuWaitUs(5); + BMA_XL_Value = M3_ReadEEPROM(0x1E); //1e 1c + halMcuWaitUs(5); + BMA_YM_Value = M3_ReadEEPROM(0x1F); + halMcuWaitUs(5); + BMA_YL_Value = M3_ReadEEPROM(0x20); + halMcuWaitUs(5); + BMA_ZM_Value = M3_ReadEEPROM(0x21); + halMcuWaitUs(5); + BMA_ZL_Value = M3_ReadEEPROM(0x22); + halMcuWaitUs(5); + + + num_x1 = (BMA_XM_Value << 8) + BMA_XL_Value; + num_y1 = (BMA_YM_Value << 8) + BMA_YL_Value; + num_z1 = (BMA_ZM_Value << 8) + BMA_ZL_Value; + + num_x = num_x + num_x1; + num_y = num_y + num_y1; + num_z = num_z + num_z1; + } + BMA_X_Value = num_x/10; + BMA_Y_Value = num_y/10; + BMA_Z_Value = num_z/10; + + halMcuWaitMs(1); +} + + + + + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.h new file mode 100644 index 0000000..937107c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_itg.h @@ -0,0 +1,25 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +extern uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; +extern uint8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; +extern uint8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + + + +void M3_Init(void); +void BMA_Pooling(void); +void M3_WriteEEPROM(uint8 addr, uint8 ch); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.c new file mode 100644 index 0000000..14d09aa --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_tgs.c +// Description: dma_tgs library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_kr.h" + +//------------------------------------------------------------------- +// @fn dma_tgs_Init +// @brief Set up port dma_tgs ad +// @return none +//------------------------------------------------------------------- +void dma_tgs_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_tgs_GetValue +// @brief Get this dma_tgs module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.h new file mode 100644 index 0000000..4adb40d --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_kr.h @@ -0,0 +1,6 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs_Init(void); +uint16 dma_tgs_GetValue(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.c new file mode 100644 index 0000000..2cf60c1 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_m4.c +// Description: dma_m4 library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_m4.h" + +//------------------------------------------------------------------- +// @fn dma_m4_Init +// @brief Set up port dma_m4 ad +// @return none +//------------------------------------------------------------------- +void dma_m4_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_m4_GetValue +// @brief Get this dma_m4 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_m4_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.h new file mode 100644 index 0000000..915079b --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_m4.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: dma_m4.h +// Description: dma_m4 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_m4_Init(void); +uint16 dma_m4_GetValue(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_sht.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_sht.h new file mode 100644 index 0000000..c817fb3 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_sht.h @@ -0,0 +1,219 @@ + + +#define ACK 1 +#define noACK 0 +#define measure_temp 0x03 //²âÁ¿Î¶ÈÃüÁî +#define measure_humi 0x05 //²âÁ¿Êª¶ÈÃüÁî +#define RESET 0x1e //ÈíÆô¶¯ + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +//------------------------Êý¾Ý½á¹¹Ì嶨Òå--------------------------- +typedef union //±£´æËù²âµÃµÄζȣ¦Êª¶ÈÖµ +{ uint i; + float f; +} value; + +typedef struct __WENSHIDU__ +{ + uchar gewei; + uchar shiwei; + uchar DateString1[4]; + // uchar DateString2[8]; +}WENSHIDU; + +WENSHIDU S; + + +/***************************º¯ÊýÉùÃ÷*******************************/ +char write_byte(unsigned char value); +char read_byte(unsigned char ack); +void transstart(void); +void connectionreset(void); +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); +void calc_sth11(float *p_humidity ,float *p_temperature); +void DateToStr(WENSHIDU *Time,float datax,float datax1); +void call_sht11(void); + + + + +//--------- write a byte and checks the ack signal----------------- +char write_byte(uchar value) +{ + uchar i,error=0; + + for (i=0x80;i>0;i/=2) /*Á¬ÐøÏòÓÒÒÆ¶¯8λ*/ + { + if (i & value) + HAL_SHT_SDA_SET(); /*°ÑÏàÓ¦µÄλËÍÊý¾ÝÏß*/ + else + HAL_SHT_SDA_CLR(); + + HAL_SHT_SCK_SET(); /*ʱÐòÂö³å£¬Ó¦Ñϸñ°´×Ŵ˱ê×¼*/ + halMcuWaitUs(1); /*´óÔ¼ 1 us*/ + HAL_SHT_SCK_CLR(); + } + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + HAL_SHT_SDA_DIR_IN(); + HAL_SHT_SCK_SET(); /*µÚ9λ×÷ΪÏìӦλ*/ + error=HAL_SHT_SDA_VAL(); /*¼ì²âÏìÓ¦Çé¿ö£¬Èç¹ûÓÐÏìÓ¦Êý¾ÝÏ߾ͻᱻSHT10À­µÍ*/ + HAL_SHT_SCK_CLR(); + + return error; /*·µ»Ø1±íʾûÏìÓ¦*/ +} + +//--------- read a byte and checks the ack signal----------------- +char read_byte(uchar ack) +{ + uchar i,val=0; + + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + HAL_SHT_SDA_DIR_IN(); + for (i=0x80;i>0;i/=2) /*Á¬ÐøÏòÓÒÒÆ¶¯8λ*/ + { + HAL_SHT_SCK_SET(); /*clk for SENSI-BUS*/ + if (HAL_SHT_SDA_VAL()) + val=(val | i); /*read bit */ + HAL_SHT_SCK_CLR(); + } + MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + HAL_SHT_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_SHT_SCK_CLR(); + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + + return val; +} + + +void transstart(void) +{ + HAL_SHT_SCK_SET(); + halMcuWaitUs(1); + HAL_SHT_SDA_CLR(); + halMcuWaitUs(1); + HAL_SHT_SCK_CLR(); + halMcuWaitUs(2); + HAL_SHT_SCK_SET(); + halMcuWaitUs(1); + + HAL_SHT_SDA_SET(); + halMcuWaitUs(1); + HAL_SHT_SCK_CLR(); + halMcuWaitUs(2); + HAL_SHT_SDA_CLR(); +} + +void connectionreset(void) +{ + unsigned char i; + + HAL_SHT_SDA_SET(); + HAL_SHT_SCK_CLR(); //Initial state + + for(i=0;i<9;i++) //9 SCK cycles + { + HAL_SHT_SCK_SET(); + HAL_SHT_SCK_CLR(); + } + + transstart(); //transmission start +} + + + +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) +{ + unsigned error=0; + uint i; + + connectionreset(); + switch(mode) + { //·¢ËͼĴæÆ÷µØÖ·¸ø´«¸ÐÆ÷ + case 0 : error+=write_byte(measure_temp); break; + case 1 : error+=write_byte(measure_humi); break; + default : break; + } + HAL_SHT_SDA_DIR_IN(); + for (i=0;i<35000;i++) + { + halMcuWaitUs(1); + if(HAL_SHT_SDA_VAL()== 0) break; + } //µÈ´ýÀ­µÍÊý¾ÝÏß + if(HAL_SHT_SDA_VAL()) error+=1; + *(p_value+1) =read_byte(ACK); //¶Á¸ß×Ö½Ú + *(p_value)=read_byte(ACK); //¶ÁµÍ×Ö½Ú + *p_checksum =read_byte(noACK); + + return error; +} + + +void calc_sth11(float *p_humidity ,float *p_temperature) +{ + const float C1=-4.0; //for 12 Bit + const float C2=+0.0405; //for 12 Bit + const float C3=-0.0000028; //for 12 Bit + const float T1=+0.01; //for 14 Bit @ 5V + const float T2=+0.00008; //for 14 Bit @ 5V + + float rh=*p_humidity; //rh: Humidity [Ticks] 12 Bit + float t=*p_temperature; //*t: Temperature [Ticks] 14 Bit + float rh_lin; //rh_lin: Humidity linear + float rh_true; //rh_true: Temperature compensated humidity + float t_C; //t_C : Temperature [°C] + + t_C=t*0.01 - 40; //calc. temperature from ticks to [°C] + rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH] + rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH] + if(rh_true>100)rh_true=100; //cut if the value is outside of + if(rh_true<0.1)rh_true=0.1; //the physical possible range + + *p_temperature=t_C; //return temperature [°C] + *p_humidity=rh_true; //return humidity[%RH] +} + + +void DateToStr(WENSHIDU *Time,float datax,float datax1) +{ + uint i; + + i=(int)datax; + Time->DateString1[0] = i; + i=(int)((datax-i)*10); + Time->DateString1[1] = i; + + i=(int)datax1; + Time->DateString1[2] = i; + i=(int)((datax1-i)*10); + Time->DateString1[3] = i; + +} + + + +void call_sht11(void) +{ + + value humi_val,temp_val; + unsigned char error = 0,checksum; + + + error+=s_measure((unsigned char*) &humi_val.i,&checksum,1); //measure humidity + error+=s_measure((unsigned char*) &temp_val.i,&checksum,0); //measure temperature + if(error!=0) + connectionreset(); //Èç¹û·¢Éú´íÎó£¬ÏµÍ³¸´Î» + else + { + humi_val.f=(float)humi_val.i; //converts integer to float + temp_val.f=(float)temp_val.i; //converts integer to float + calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity,temperature + DateToStr(&S,temp_val.f,humi_val.f); + } +} + + + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.c new file mode 100644 index 0000000..731a8dd --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.c @@ -0,0 +1,101 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tc72.h" + +uint16 TC72_Value = 0; + +void Write_Tc72(uint8 ch) +{ + uint8 i, cmd; + + cmd = ch; + for (i = 0; i < 8; i++) + { + HAL_TC72_SCL_CLR(); + if (cmd &0x80) + { + HAL_TC72_SDI_SET(); + } + else + { + HAL_TC72_SDI_CLR(); + } + cmd <<= 1; + HAL_TC72_SCL_SET(); + } + HAL_TC72_SDI_SET(); +} + +uint16 Read_Tc72(void) +{ + uint8 i, ret; + + HAL_TC72_SDI_CLR(); + for (i = 0; i < 8; i++) + { + HAL_TC72_SCL_CLR(); + ret <<= 1; + HAL_TC72_SCL_SET(); + if (HAL_TC72_SDO_VAL()) + ret |= 0x01; + } + HAL_TC72_SDI_SET(); + return ret; +} + + + +//------------------------------------------------------------------- +void TC72_Start(void) +{ + HAL_TC72_SSS_SET(); + Write_Tc72(0x80); + Write_Tc72(0x15); + HAL_TC72_SSS_CLR(); +} + +//------------------------------------------------------------------- +void TC72_Read(void) +{ + uint16 m,n; + HAL_TC72_SSS_SET(); + + Write_Tc72(0x02); + halMcuWaitUs(200); + m = Read_Tc72(); + halMcuWaitUs(200); + + Write_Tc72(0x01); + halMcuWaitUs(200); + n = Read_Tc72(); + halMcuWaitUs(200); + + TC72_Value = (m<<8)+n; + HAL_TC72_SSS_CLR(); +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void TC72_Init(void) +{ + MCU_IO_INPUT(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN, MCU_IO_PULLUP); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN,1); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN,1); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN,1); +} + +//------------------------------------------------------------------- +uint16 TC72_ReadTemp(void) +{ + TC72_Start(); + halMcuWaitMs(2); + TC72_Read(); + return TC72_Value; +} + + diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.h new file mode 100644 index 0000000..672b851 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tc72.h @@ -0,0 +1,8 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- + +void TC72_Init(void); +uint16 TC72_ReadTemp(void); +void TC72_Enable(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.c new file mode 100644 index 0000000..034a3a8 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_tgs.c +// Description: dma_tgs library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tgs.h" + +//------------------------------------------------------------------- +// @fn dma_tgs_Init +// @brief Set up port dma_tgs ad +// @return none +//------------------------------------------------------------------- +void dma_tgs_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_tgs_GetValue +// @brief Get this dma_tgs module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.h new file mode 100644 index 0000000..4adb40d --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs.h @@ -0,0 +1,6 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs_Init(void); +uint16 dma_tgs_GetValue(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.c new file mode 100644 index 0000000..9e743a2 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: M190.c +// Description: hal M190 library å�¯è®Šé›»é˜»(A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tgs2602.h" + +//------------------------------------------------------------------- +// @fn M190_Init +// @brief Set up port M190 ad +// @return none +//------------------------------------------------------------------- +void dma_tgs2602_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn M190_GetValue +// @brief Get this M190 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs2602_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.h new file mode 100644 index 0000000..fb8f887 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/moudle/dma_tgs2602.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: M190.h +// Description: M190 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs2602_Init(void); +uint16 dma_tgs2602_GetValue(void); diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/Flash_data.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/Flash_data.c new file mode 100644 index 0000000..c0c5f3a --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/Flash_data.c @@ -0,0 +1,73 @@ +#include "hal_defs.h" +#include "hal_flash.h" +#include "Flash_data.h" + +#define OSAL_NV_WORD_SIZE 4 + + +void writeWordM( uint8 pg, uint16 offset, uint8 *buf, uint16 cnt ) +{ + offset = (offset >> 2) + ((uint16)pg << 9); + HalFlashWrite(offset, buf, cnt); + +} + + +void writeWord( uint8 pg, uint16 offset, uint8 *buf ) +{ + offset = (offset / HAL_FLASH_WORD_SIZE) + ((uint16)pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)); + HalFlashWrite(offset, buf, 1); +} + + +extern void writeBuf( uint8 dstPg, uint16 dstOff, uint16 len, uint8 *buf ) +{ + uint8 rem = dstOff % OSAL_NV_WORD_SIZE; + uint8 tmp[OSAL_NV_WORD_SIZE]; + + if ( rem ) + { + dstOff = (dstOff / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE; + HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE); + + while ( (rem < OSAL_NV_WORD_SIZE) && len ) + { + tmp[rem++] = *buf++; + len--; + } + + writeWord( dstPg, dstOff, tmp ); + dstOff += OSAL_NV_WORD_SIZE; + } + + rem = len % OSAL_NV_WORD_SIZE; + len /= OSAL_NV_WORD_SIZE; + + if ( len ) + { + writeWordM( dstPg, dstOff, buf, len ); + dstOff += OSAL_NV_WORD_SIZE * len; + buf += OSAL_NV_WORD_SIZE * len; + } + + if ( rem ) + { + uint8 idx = 0; + HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE); + while ( rem-- ) + { + tmp[idx++] = *buf++; + } + writeWord( dstPg, dstOff, tmp ); + } + } + + +void delay(int time ) +{ + uint16 cntr1,cntr2; + for (cntr1=0;cntr1 +#include +#include + + +void WaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +/********************************************************** +func: ½ÓÊÕ´®¿ÚÊý¾Ý°ü +²ÎÊý£º + 1. *buf -- ´®¿Ú½ÓÊÕÊý¾ÝËù´æ·ÅµÄµØ·½ + 2. maxlen -- ´®¿Ú½ÓÊÕÊý¾ÝµÄ×î´ó³¤¶È +retuen: uint16 ½ÓÊÕµ½µÄÊý¾Ý³¤¶È +***********************************************************/ +uint16 uart_read(uint8 *buf, uint16 maxlen) +{ + uint16 slen=0; + uint8 dtime=0; + while(dtime<20) + { + while(HalUARTRead(buf+slen, 1)) + { + slen++; + if(slen>=maxlen) + return slen; + dtime=0; + } + WaitUs(30); // Wait 30us; + dtime++; + } + return slen; +} +/***** +°´printf¸ñʽ´òÓ¡Êä³ö×Ö·û´® + +***/ +void printf_str( char *buf, char *fmt,... ) +{ + va_list ap; + va_start(ap,fmt); + vsprintf(buf,fmt,ap); + va_end(ap); +} +/********/ +void uart_printf(char *fmt,...) +{ + va_list ap; + char str[256]; + + va_start(ap,fmt); + vsprintf(str,fmt,ap); + UartWrite(str,strlen(str)); + va_end(ap); +} + +void uart_enter(void) +{ + + uart_printf("\r\n"); + +} + +void uart_datas(void * fmt,uint16 len) +{ + char * ptr; + ptr=fmt; + while(len--) + { + uart_printf("%02x ",*ptr ); + ptr++; + } + uart_enter(); +} + +uint8 lrc_checksum(uint8* buf, uint8 len) +{ + uint8 lrc=0; + while(len--) + { + lrc+=*buf; + buf++; + } + lrc =~lrc; + lrc++; + return lrc; +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_PRINT.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_PRINT.h new file mode 100644 index 0000000..771f60a --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_PRINT.h @@ -0,0 +1,17 @@ + + +#ifndef uart_PRINT_H +#define uart_PRINT_H +//#define UartWrite(buf,len) (HalUARTWrite(HAL_UART_PORT_0,(buf),(len))) +#define UartWrite(buf,len) (halUartWrite((buf),(len))) +#define HalUARTRead(buf,len) (halUartRead((buf),(len))) + +uint16 RecvUartData(uint8 *uRxData, uint16 uRxlen); +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen); +extern void uart_datas(void *fmt,uint16 len); +extern void uart_enter(void); +extern void uart_printf(char *fmt,...); +extern void printf_str(char *buf, char *fmt,...); +extern uint16 uart_read(uint8 *buf, uint16 maxlen); +extern uint8 lrc_checksum(uint8* buf, uint8 len); +#endif \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c new file mode 100644 index 0000000..93fd870 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c @@ -0,0 +1,74 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_uart.h" +#include "hal_uart1.h" + + +/************************************************************************************** +²ÎÊý£º + 1. *dst -- Ä¿µÄÊý×éµÄÊ×µØÖ·£¬Ò²¾ÍÊÇ×îºó´æ·ÅÊý¾ÝµÄµØ·½ + 2. dststart -- ´ÓÄ¿µÄÊý×éµÄµÚ dststart λ¿ªÊ¼´æ·ÅÊý¾Ý + 3. *src -- Ô´Êý¾ÝµÄÊ×µØÖ·£¬Ò²¾ÍÊÇÊý¾ÝµÄÀ´Ô´ + 4. srcstart -- ´ÓÔ´Êý×éµÄµÚ srcstart λ¿ªÊ¼È¡Êý¾Ý + 5. len -- ÐèÒª´ÓÔ´Êý×éÈ¡ len ×Ö½ÚµÄÊý¾Ý´æ·Åµ½Ä¿µÄÊý×éÖÐ +Àý×Ó£º + MyByteCopy(buf1, 5, buf2, 3, 4)£» + ´ÓÊý×é buf2 µÄµÚ3¸ö×Ö½Ú¿ªÊ¼ÍùºóµÄ4¸ö×Ö½Ú£¨buf2[3]¡¢buf2[4]¡¢buf2[5]¡¢buf2[6]£© + ¸´ÖƵ½ buf1[5]¡¢buf1[6]¡¢buf1[7]¡¢buf1[8]ÖС£ +***************************************************************************************/ +void MyByteCopy(uint8 *dst, int dststart, uint8 *src, int srcstart, int len) +{ + int i; + for(i=0; i 0) + { + r_UartLen = halUartRead(r_UartBuf, sizeof(r_UartBuf)); + MyByteCopy(uRxData, uRxlen, r_UartBuf, 0, r_UartLen); + uRxlen += r_UartLen; + halMcuWaitMs(5); //ÕâÀïµÄÑӳٷdz£ÖØÒª£¬ÒòΪÕâÊÇ´®¿ÚÁ¬Ðø¶ÁÈ¡Êý¾ÝʱºòÐèÒªÓÐÒ»¶¨µÄʱ¼ä¼ä¸ô + r_UartLen = halUartRxLen(); + } + return uRxlen; +} + +/********************************************************** +²ÎÊý£º + 1. *uRxData -- ´®¿Ú1½ÓÊÕÊý¾ÝËù´æ·ÅµÄµØ·½ + 2. uRxlen -- ´®¿Ú1½ÓÊÕÊý¾ÝµÄ³õʼ³¤¶È£¨Ò»°ãΪ0£© +***********************************************************/ +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen) +{ + uint16 r_Uart1Len = 0; + uint8 r_Uart1Buf[128]; + + r_Uart1Len = halUart1RxLen(); + halMcuWaitMs(5); + while(r_Uart1Len > 0) + { + r_Uart1Len = halUart1Read(r_Uart1Buf, sizeof(r_Uart1Buf)); + MyByteCopy(uRxData, uRxlen, r_Uart1Buf, 0, r_Uart1Len); + uRxlen += r_Uart1Len; + halMcuWaitMs(5); //ÕâÀïµÄÑӳٷdz£ÖØÒª£¬ÒòΪÕâÊÇ´®¿ÚÁ¬Ðø¶ÁÈ¡Êý¾ÝʱºòÐèÒªÓÐÒ»¶¨µÄʱ¼ä¼ä¸ô + r_Uart1Len = halUart1RxLen(); + } + return uRxlen; +} \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h new file mode 100644 index 0000000..d08350f --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h @@ -0,0 +1,4 @@ + +void MyByteCopy(uint8 *dst, int dststart, uint8 *src, int srcstart, int len); +uint16 RecvUartData(uint8 *uRxData, uint16 uRxlen); +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen); \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/readme.doc b/Zigbee无线通信/Aæ�¿/CC2530_lib/readme.doc new file mode 100644 index 0000000..f808f56 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/CC2530_lib/readme.doc differ diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.c b/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.c new file mode 100644 index 0000000..cd5405a --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.c @@ -0,0 +1,129 @@ +//------------------------------------------------------------------- +// Filename: util.c +// Description: Utility library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn utilChipIdToStr +// @brief Converts a chip ID to a text string. +// @param uint8 chipID +// @return none +//------------------------------------------------------------------- +const char *utilChipIdToStr(uint8 chipID) +{ + const char *szId; + + switch (chipID) + { + case HAL_RF_CHIP_ID_CC2420: + szId = "2420"; + break; + case HAL_RF_CHIP_ID_CC2430: + szId = "2430"; + break; + case HAL_RF_CHIP_ID_CC2431: + szId = "2431"; + break; + case HAL_RF_CHIP_ID_CC2520: + szId = "2520"; + break; + case HAL_RF_CHIP_ID_CC2530: + szId = "2530"; + break; + case HAL_RF_CHIP_ID_CC2531: + szId = "2531"; + break; + case HAL_RF_CHIP_ID_CC2510: + szId = "2510"; + break; + case HAL_RF_CHIP_ID_CC2511: + szId = "2511"; + break; + case HAL_RF_CHIP_ID_CC1110: + szId = "1110"; + break; + case HAL_RF_CHIP_ID_CC1111: + szId = "1111"; + break; + default: + szId = "----"; + }; + + return szId; +} + +//------------------------------------------------------------------- +// @fn convInt32ToText +// @brief Converts 32 bit int to text +// @param int32 value +// @return char* - pointer to text buffer which is a file scope allocated array +//------------------------------------------------------------------- +char *convInt32ToText(int32 value) +{ + static char pValueToTextBuffer[12]; + char *pLast; + char *pFirst; + char last; + uint8 negative; + + pLast = pValueToTextBuffer; + + // Record the sign of the value + negative = (value < 0); + value = ABS(value); + + // Print the value in the reverse order + do + { + *(pLast++) = '0' + (uint8)(value % 10); + value /= 10; + } + while (value); + + // Add the '-' when the number is negative, and terminate the string + if (negative) + { + *(pLast++) = '-'; + } + *(pLast--) = 0x00; + + // Now reverse the string + pFirst = pValueToTextBuffer; + while (pLast > pFirst) + { + last = *pLast; + *(pLast--) = *pFirst; + *(pFirst++) = last; + } + + return pValueToTextBuffer; +} + +//------------------------------------------------------------------- +// @fn utilReverseBuf +// @brief reverse buffer +// @param uint8 pBuf - pointer to buffer +// uint8 length - length of buffer +// @return void +//------------------------------------------------------------------- +void utilReverseBuf(uint8 *pBuf, uint8 length) +{ + uint8 temp; + uint8 *pBufLast = (pBuf + length - 1); + + while (pBufLast > pBuf) + { + temp = *pBuf; + *pBuf++ = *pBufLast; + *pBufLast-- = temp; + } +} diff --git a/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.h b/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.h new file mode 100644 index 0000000..dcf47d1 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/CC2530_lib/utils/util.h @@ -0,0 +1,31 @@ +//------------------------------------------------------------------- +// Filename: util.h +// Description: Utility library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + const char *szDescr; // Textual description + const uint8 value; // Value +} menuItem_t; + +typedef struct +{ + const menuItem_t *pMenuItems; + const uint8 nItems; +} menu_t; + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Number of elements in an array +#define N_ITEMS(arr) sizeof(arr)/sizeof(arr[0]) + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +char *convInt32ToText(int32 value); +const char *utilChipIdToStr(uint8 chipID); +void utilReverseBuf(uint8 *pBuf, uint8 length); diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Exe/rf_set.d51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Exe/rf_set.d51 new file mode 100644 index 0000000..d20d772 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Exe/rf_set.d51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/BMA020.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/BMA020.r51 new file mode 100644 index 0000000..411f625 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/BMA020.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/basic_rf.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/basic_rf.r51 new file mode 100644 index 0000000..4226422 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/basic_rf.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/dma_itg.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/dma_itg.r51 new file mode 100644 index 0000000..6f91dc6 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/dma_itg.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_adc.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_adc.r51 new file mode 100644 index 0000000..16765dd Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_adc.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_swsensor.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_swsensor.r51 new file mode 100644 index 0000000..ee264c4 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/get_swsensor.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_board.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_board.r51 new file mode 100644 index 0000000..f75c7a8 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_board.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_clock.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_clock.r51 new file mode 100644 index 0000000..bfe7cb9 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_clock.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_digio.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_digio.r51 new file mode 100644 index 0000000..bad3a29 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_digio.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_int.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_int.r51 new file mode 100644 index 0000000..56da32d Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_int.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_led.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_led.r51 new file mode 100644 index 0000000..5ddfc26 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_led.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_mcu.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_mcu.r51 new file mode 100644 index 0000000..ce5df80 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_mcu.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf.r51 new file mode 100644 index 0000000..1b84e1e Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf_security.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf_security.r51 new file mode 100644 index 0000000..309cede Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_rf_security.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_uart.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_uart.r51 new file mode 100644 index 0000000..1d89472 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/hal_uart.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.pbd b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.pbd new file mode 100644 index 0000000..c239902 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.pbd @@ -0,0 +1,19 @@ +This is an internal working file generated by the Source Browser. +22:13 07s +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\BMA020.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\basic_rf.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\dma_itg.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\get_adc.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\get_swsensor.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_board.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_clock.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_digio.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_int.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_led.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_mcu.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_rf.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_rf_security.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\hal_uart.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\rf_set.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\sensor.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\A°å\Project\Debug\Obj\util.pbi diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.r51 new file mode 100644 index 0000000..ad90e51 Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/rf_set.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/sensor.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/sensor.r51 new file mode 100644 index 0000000..64366ac Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/sensor.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/util.r51 b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/util.r51 new file mode 100644 index 0000000..2de530d Binary files /dev/null and b/Zigbee无线通信/Aæ�¿/Project/Debug/Obj/util.r51 differ diff --git a/Zigbee无线通信/Aæ�¿/Project/rf_set.c b/Zigbee无线通信/Aæ�¿/Project/rf_set.c new file mode 100644 index 0000000..09846cc --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/rf_set.c @@ -0,0 +1,144 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_led.h" +#include "hal_rf.h" +#include "basic_rf.h" +#include "hal_uart.h" +#include "sensor_drv/sensor.h" +#include +#include +#include +/*****µã¶ÔµãͨѶµØÖ·ÉèÖÃ****A**/ +#define RF_CHANNEL 25 // ƵµÀ 11~26 +#define PAN_ID 0xFF //ÍøÂçid +#define MY_ADDR 0xA1 //±¾»úÄ£¿éµØÖ· +#define SEND_ADDR 0xA2 //·¢Ë͵ØÖ· + +/** +* ÎÞÏßͨÐÅʵÑé-A°å +* +*/ +//LED & SW +#define LED1 P1_0 +#define LED2 P1_1 +#define LED3 P1_3 +#define LED4 P1_4 +#define SW P1_2 + +//³¤°´±êÖ¾ +uint8 Long_click = 0; +//·µ»Ø°´¼ü״̬ + uint8 key_down_status = 0; +//·¢ËÍÊý¾Ý + uint8 send_data = 0x00; +//½ÓÊÕÊý¾Ý + uint8 recv_data = 0x00; +//³¤°´¼ÆÊý +unsigned int time = 0; +//ÅÜÂí±ê־λ +unsigned char sign = 0; + + +static basicRfCfg_t basicRfConfig; +// ÎÞÏßRF³õʼ»¯ +void ConfigRf_Init(void) +{ + basicRfConfig.panId = PAN_ID; + basicRfConfig.channel = RF_CHANNEL; + basicRfConfig.myAddr = MY_ADDR; + basicRfConfig.ackRequest = TRUE; + while(basicRfInit(&basicRfConfig) == FAILED); + basicRfReceiveOn(); +} +void initial_gpio(){ + + P1SEL &=~0X1F; + P1DIR |=0X1B; + P1DIR &=~0X04; + P1INP &=~0X04; + P2INP &=~0X40; + P1 &=~0X1B; +} + +uint8 key_down(){ + //ÅжÏÊÇÊÇ·ñ³¤°´ + if(Long_click == 1){ + //ËÉ¿ª·µ»Ø + if(SW){ + Long_click = 0; + return 11; + } + } + if(SW == 0){ + halMcuWaitUs(20); + if(SW == 0){ + time = 0; + while(!SW){ + halMcuWaitMs(1); + time++; + if(time>1000){ + Long_click = 1; + return 22; + } + } + return 33; + } + } + return 0; +} +//ÅÜÂíµÆ +void light(){ + LED2 = 1; + halMcuWaitMs(500); + LED1 = 1; + halMcuWaitMs(500); + LED4 = 1; + halMcuWaitMs(500); + LED3 = 1; + halMcuWaitMs(500); + LED2 = 0; + halMcuWaitMs(500); + LED1 = 0; + halMcuWaitMs(500); + LED4 = 0; + halMcuWaitMs(500); + LED3 = 0; + halMcuWaitMs(500); +} +/********************MAIN************************/ +void main(void) +{ + halBoardInit();//Ñ¡ÊÖ²»µÃÔڴ˺¯ÊýÄÚÌí¼Ó´úÂë + ConfigRf_Init();//Ñ¡ÊÖ²»µÃÔڴ˺¯ÊýÄÚÌí¼Ó´úÂë + initial_gpio(); + while(1) + { + key_down_status = key_down(); + if(key_down_status == 22){ + + //·¢ËͺôÎü¿ªÊ¼Êý¾Ý + send_data = 0x1F; + basicRfSendPacket(SEND_ADDR,&send_data,1); + } + if(key_down_status == 11){ + + //·¢ËͺôÎü½áÊøÊý¾Ý + send_data = 0x2F; + basicRfSendPacket(SEND_ADDR,&send_data,1); + } + //½ÓÊÕÅÐ¶Ï + if(basicRfPacketIsReady()){ + basicRfReceive(&recv_data,1,NULL); + //½ÓÊÕÅÜÂíÊý¾Ý + if(recv_data == 0x1D){ + sign = !sign; + } + } + if(sign){ + light(); + } + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/Project/rf_set.dep b/Zigbee无线通信/Aæ�¿/Project/rf_set.dep new file mode 100644 index 0000000..be4bd15 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/rf_set.dep @@ -0,0 +1,524 @@ + + + + 2 + 37149274 + + Debug + + $PROJ_DIR$\Debug\Obj\sensor.pbi + $PROJ_DIR$\Debug\Obj\rf_set.pbi + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.h + $PROJ_DIR$\Debug\Obj\hal_board.r51 + $PROJ_DIR$\..\CC2530_lib\common\hal_cc8051.h + $PROJ_DIR$\Debug\Obj\hal_rf_security.pbi + $PROJ_DIR$\Debug\Obj\hal_led.pbi + $PROJ_DIR$\Debug\Obj\get_adc.pbi + $PROJ_DIR$\Debug\Obj\basic_rf.pbi + $PROJ_DIR$\Debug\Obj\hal_rf_security.r51 + $TOOLKIT_DIR$\inc\clib\stdio.h + $PROJ_DIR$\Debug\Obj\hal_clock.r51 + $PROJ_DIR$\Debug\Obj\hal_mcu.r51 + $PROJ_DIR$\..\CC2530_lib\utils\util.h + $TOOLKIT_DIR$\inc\clib\sysmac.h + $PROJ_DIR$\Debug\Obj\basic_rf.r51 + $PROJ_DIR$\Debug\Obj\get_adc.r51 + $PROJ_DIR$\Debug\Obj\hal_digio.pbi + $TOOLKIT_DIR$\inc\ioCC2530.h + $PROJ_DIR$\Debug\Obj\get_swsensor.r51 + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.h + $PROJ_DIR$\..\CC2530_lib\common\hal_int.h + $PROJ_DIR$\Debug\Obj\hal_led.r51 + $PROJ_DIR$\Debug\Obj\hal_mcu.pbi + $PROJ_DIR$\Debug\Obj\dma_itg.r51 + $PROJ_DIR$\Debug\Obj\BMA020.r51 + $PROJ_DIR$\..\CC2530_lib\board\hal_led.h + $PROJ_DIR$\Debug\Obj\get_swsensor.pbi + $PROJ_DIR$\Debug\Obj\rf_set.r51 + $PROJ_DIR$\Debug\Obj\util.pbi + $PROJ_DIR$\..\CC2530_lib\board\hal_board.h + $PROJ_DIR$\Debug\Obj\util.r51 + $PROJ_DIR$\Debug\Obj\hal_clock.pbi + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.h + $TOOLKIT_DIR$\config\devices\Texas Instruments\lnk51ew_CC2530F256_banked.xcl + $PROJ_DIR$\Debug\Obj\hal_digio.r51 + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.h + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.h + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.h + $TOOLKIT_DIR$\inc\clib\stdarg.h + $PROJ_DIR$\Debug\Obj\sensor.r51 + $PROJ_DIR$\Debug\Obj\hal_board.pbi + $PROJ_DIR$\Debug\Obj\hal_rf.r51 + $PROJ_DIR$\Debug\Obj\hal_int.r51 + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.h + $PROJ_DIR$\Debug\Obj\dma_itg.pbi + $PROJ_DIR$\Debug\Obj\hal_uart.r51 + $TOOLKIT_DIR$\inc\clib\string.h + $PROJ_DIR$\Debug\Obj\hal_rf.pbi + $PROJ_DIR$\Debug\Exe\send.hex + $PROJ_DIR$\Debug\Obj\hal_uart.pbi + $PROJ_DIR$\Debug\Exe\rf_set.d51 + $TOOLKIT_DIR$\lib\clib\cl-pli-blxd-1e16x01.r51 + $PROJ_DIR$\..\CC2530_lib\utils\util.c + $PROJ_DIR$\Debug\Obj\BMA020.pbi + $PROJ_DIR$\Debug\Obj\rf_set.pbd + $PROJ_DIR$\Debug\Obj\hal_int.pbi + $PROJ_DIR$\..\CC2530_lib\common\hal_defs.h + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + $PROJ_DIR$\rf_set.c + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + $PROJ_DIR$\SENSOR_DRV\sensor.c + $PROJ_DIR$\SENSOR_DRV\BMA020.c + $PROJ_DIR$\SENSOR_DRV\get_adc.h + $PROJ_DIR$\SENSOR_DRV\get_swsensor.h + $PROJ_DIR$\SENSOR_DRV\BMA020.h + $PROJ_DIR$\SENSOR_DRV\dma_itg.h + $PROJ_DIR$\SENSOR_DRV\sensor.h + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + $PROJ_DIR$\SENSOR_DRV\get_adc.c + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + + + [ROOT_NODE] + + + XLINK + 49 51 + + + + + $PROJ_DIR$\Debug\Exe\rf_set.d51 + + + XLINK + 49 + + + + + XLINK + 34 25 15 24 16 19 3 11 35 43 22 12 42 9 46 28 40 31 52 + + + + + $PROJ_DIR$\..\CC2530_lib\utils\util.c + + + BICOMP + 29 + + + ICC8051 + 31 + + + + + BICOMP + 57 18 13 38 + + + ICC8051 + 57 18 13 38 + + + + + $PROJ_DIR$\Debug\Obj\rf_set.pbd + + + BILINK + 54 8 45 7 27 41 32 17 56 6 23 48 5 50 1 0 29 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + + + BICOMP + 17 + + + ICC8051 + 35 + + + + + BICOMP + 57 18 21 2 + + + ICC8051 + 57 18 21 2 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + + + BICOMP + 48 + + + ICC8051 + 42 + + + + + BICOMP + 57 18 33 21 13 38 + + + ICC8051 + 57 18 33 21 13 38 + + + + + $PROJ_DIR$\rf_set.c + + + BICOMP + 1 + + + ICC8051 + 28 + + + + + BICOMP + 57 18 4 21 33 30 26 38 20 36 74 10 39 14 47 + + + ICC8051 + 57 18 4 21 33 30 26 38 20 36 74 10 39 14 47 + + + + + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + + + BICOMP + 8 + + + ICC8051 + 15 + + + + + BICOMP + 57 18 21 33 38 20 47 14 + + + ICC8051 + 57 18 21 33 38 20 47 14 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + + + BICOMP + 5 + + + ICC8051 + 9 + + + + + BICOMP + 57 18 33 38 37 13 47 14 + + + ICC8051 + 57 18 33 38 37 13 47 14 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + + + BICOMP + 56 + + + ICC8051 + 43 + + + + + BICOMP + 57 18 21 + + + ICC8051 + 57 18 21 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + + + BICOMP + 23 + + + ICC8051 + 12 + + + + + BICOMP + 57 18 44 33 30 + + + ICC8051 + 57 18 44 33 30 + + + + + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + + + BICOMP + 6 + + + ICC8051 + 22 + + + + + BICOMP + 57 18 4 33 30 26 + + + ICC8051 + 57 18 4 33 30 26 + + + + + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + + + BICOMP + 41 + + + ICC8051 + 3 + + + + + BICOMP + 57 18 4 33 2 21 30 36 + + + ICC8051 + 57 18 4 33 2 21 30 36 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + + + BICOMP + 32 + + + ICC8051 + 11 + + + + + BICOMP + 57 18 44 + + + ICC8051 + 57 18 44 + + + + + $PROJ_DIR$\SENSOR_DRV\sensor.c + + + BICOMP + 0 + + + ICC8051 + 40 + + + + + BICOMP + 57 18 33 70 74 73 72 + + + ICC8051 + 57 18 33 70 74 73 72 + + + + + $PROJ_DIR$\SENSOR_DRV\BMA020.c + + + BICOMP + 54 + + + ICC8051 + 25 + + + + + BICOMP + 18 57 72 + + + ICC8051 + 18 57 72 + + + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + + + BICOMP + 45 + + + ICC8051 + 24 + + + + + BICOMP + 18 57 73 + + + ICC8051 + 18 57 73 + + + + + $PROJ_DIR$\SENSOR_DRV\get_adc.c + + + BICOMP + 7 + + + ICC8051 + 16 + + + + + BICOMP + 18 57 70 + + + ICC8051 + 18 57 70 + + + + + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + + + BICOMP + 50 + + + ICC8051 + 46 + + + + + BICOMP + 57 18 30 36 + + + ICC8051 + 57 18 30 36 + + + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + + + BICOMP + 27 + + + ICC8051 + 19 + + + + + BICOMP + 18 57 71 + + + ICC8051 + 18 57 71 + + + + + + Release + + + [MULTI_TOOL] + XLINK + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/rf_set.ewd b/Zigbee无线通信/Aæ�¿/Project/rf_set.ewd new file mode 100644 index 0000000..99972a8 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/rf_set.ewd @@ -0,0 +1,1281 @@ + + + + 2 + + Debug + + 8051 + + 1 + + C-SPY + 2 + + 8 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _3RD_ID + 1 + + 0 + 1 + 1 + + + + + + + + CHIPCON_ID + 2 + + 3 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + FS2_ID + 1 + + 0 + 1 + 1 + + + + + + + + + + + + + INFINEON_ID + 1 + + 1 + 1 + 1 + + + + + + + + + + + + + + + + NS_ID + 1 + + 0 + 1 + 1 + + + + + + + ROM_ID + 1 + + 2 + 1 + 1 + + + + + + + + + + + + + + + + + + AD2_ID + 2 + + 6 + 1 + 1 + + + + + + + + + + + + + + CYGNAL_ID + 1 + + 1 + 1 + 1 + + + + + + + + + + + + + + + + + + + SIM_ID + 1 + + 2 + 1 + 1 + + + + + + + + + $EW_DIR$\common\plugins\CodeCoverage\CodeCoverage.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\Orti\Orti.ENU.ewplugin + 0 + + + $EW_DIR$\common\plugins\Stack\Stack.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\SymList\SymList.ENU.ewplugin + 1 + + + + + Release + + 8051 + + 0 + + C-SPY + 2 + + 8 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _3RD_ID + 1 + + 0 + 1 + 0 + + + + + + + + CHIPCON_ID + 2 + + 3 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + FS2_ID + 1 + + 0 + 1 + 0 + + + + + + + + + + + + + INFINEON_ID + 1 + + 1 + 1 + 0 + + + + + + + + + + + + + + + + NS_ID + 1 + + 0 + 1 + 0 + + + + + + + ROM_ID + 1 + + 2 + 1 + 0 + + + + + + + + + + + + + + + + + + AD2_ID + 2 + + 6 + 1 + 0 + + + + + + + + + + + + + + CYGNAL_ID + 1 + + 1 + 1 + 0 + + + + + + + + + + + + + + + + + + + SIM_ID + 1 + + 2 + 1 + 0 + + + + + + + + + $EW_DIR$\common\plugins\CodeCoverage\CodeCoverage.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\Orti\Orti.ENU.ewplugin + 0 + + + $EW_DIR$\common\plugins\Stack\Stack.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\SymList\SymList.ENU.ewplugin + 1 + + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/rf_set.ewp b/Zigbee无线通信/Aæ�¿/Project/rf_set.ewp new file mode 100644 index 0000000..856ed0b --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/rf_set.ewp @@ -0,0 +1,2157 @@ + + + + 2 + + Debug + + 8051 + + 1 + + General + 1 + + 5 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICC8051 + 4 + + 9 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A8051 + 2 + + 5 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + XLINK + 3 + + 17 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + XAR + 1 + + 0 + 1 + 1 + + + + + + + + BILINK + 0 + + + + + Release + + 8051 + + 0 + + General + 1 + + 5 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICC8051 + 4 + + 9 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A8051 + 2 + + 5 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + XLINK + 3 + + 17 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + XAR + 1 + + 0 + 1 + 0 + + + + + + + + BILINK + 0 + + + + + app + + $PROJ_DIR$\rf_set.c + + + + basicrf + + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + + + + board + + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + + + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + + + + common + + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + + + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + + + + sensor_drv + + $PROJ_DIR$\SENSOR_DRV\BMA020.c + + + $PROJ_DIR$\SENSOR_DRV\BMA020.h + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.h + + + $PROJ_DIR$\SENSOR_DRV\get_adc.c + + + $PROJ_DIR$\SENSOR_DRV\get_adc.h + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.h + + + $PROJ_DIR$\SENSOR_DRV\sensor.c + + + $PROJ_DIR$\sensor_drv\sensor.h + + + + utils + + $PROJ_DIR$\..\CC2530_lib\utils\util.c + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/rf_set.eww b/Zigbee无线通信/Aæ�¿/Project/rf_set.eww new file mode 100644 index 0000000..5706dae --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/rf_set.eww @@ -0,0 +1,10 @@ + + + + + $WS_DIR$\rf_set.ewp + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.c new file mode 100644 index 0000000..6f60b91 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.c @@ -0,0 +1,285 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +//#include "hal_types.h" +#include "hal_defs.h" +#include "BMA020.h" + + + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + + +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 + +#define HAL_BOARD_IO_E2PROM_SCL_PORT HAL_BOARD_IO_SHT_SCK_PORT +#define HAL_BOARD_IO_E2PROM_SCL_PIN HAL_BOARD_IO_SHT_SCK_PIN + + +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCL_DIR_OUT HAL_E2PROM_SCK_DIR_OUT +#define HAL_E2PROM_SCL_SET HAL_E2PROM_SCK_SET +#define HAL_E2PROM_SCL_CLR HAL_E2PROM_SCK_CLR + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define halMcuWaitUs McuWaitUs +#define NOP() asm("NOP") +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void McuWaitUs2(uint16 usec); +void McuWaitUs2(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void M2_I2cStart(void); +void M2_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + McuWaitUs2(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cStop(void); +void M2_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + McuWaitUs2(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + McuWaitUs2(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cAck(void); +void M2_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SDA_CLR(); + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cNack(void); +void M2_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SDA_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cWrite(uint8 dat); +void M2_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + McuWaitUs2(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + McuWaitUs2(2); + } +} + +//------------------------------------------------------------------- +static uint8 M2_I2cRead(void); +uint8 M2_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + McuWaitUs2(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + McuWaitUs2(2); + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +static void M2_Init(void); +void M2_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +static void M2_WriteEEPROM(uint8 addr, uint8 ch); +void M2_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M2_I2cStart(); + + M2_I2cWrite(0x70);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M2_I2cAck(); + + M2_I2cWrite(addr); + M2_I2cAck(); + + M2_I2cWrite(ch); //data + M2_I2cAck(); + + M2_I2cStop(); + // halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +static uint8 M3_ReadEEPROM(uint8 addr); +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M2_I2cStart(); + + M2_I2cWrite(0x70); //Æ÷¼þÉèÖÃΪд״̬ + M2_I2cAck(); + + M2_I2cWrite(addr); + M2_I2cAck(); + + + M2_I2cStart(); + + M2_I2cWrite(0x71); //Æ÷¼þÉèÖÃΪд״̬ + M2_I2cAck(); + + res = M2_I2cRead(); + + M2_I2cNack(); + M2_I2cStop(); + + return res; +} + + +/****************************************************************************** + * Ãû³Æ BMA_Pooling + * ¹¦ÄÜ ¶ÁÈ¡bma020µÄX,Y ZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ + * Èë¿Ú²ÎÊý (int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) + *BMA_X_Value£ºXµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Y_Value£ºYµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Z_Value£ºZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + ¼ÓËÙ¶ÈֵΪ16λÓзûºÅÊý£¬·Ö±æÂÊΪ 2/512 (g), g=9.8m/s + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void BMA_Pooling(int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) +{ + uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + int16 value; + M2_Init(); + BMA_XL_Value = M3_ReadEEPROM(0x02); + BMA_XM_Value = M3_ReadEEPROM(0x03); + BMA_YL_Value = M3_ReadEEPROM(0x04); + BMA_YM_Value = M3_ReadEEPROM(0x05); + BMA_ZL_Value = M3_ReadEEPROM(0x06); + BMA_ZM_Value = M3_ReadEEPROM(0x07); + value = (BMA_XM_Value << 8) | ((BMA_XL_Value&0xc0)); + *BMA_X_Value= value/64; + value = (BMA_YM_Value << 8) | ((BMA_YL_Value&0xc0)); + *BMA_Y_Value= value/64; + value = (BMA_ZM_Value << 8) | ((BMA_ZL_Value&0xc0)); + *BMA_Z_Value= value/64; +} + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.h new file mode 100644 index 0000000..2ac8afc --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/BMA020.h @@ -0,0 +1,29 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned short +#define ulong unsigned long + + +/****************************************************************************** + * Ãû³Æ BMA_Pooling + * ¹¦ÄÜ ¶ÁÈ¡bma020µÄX,Y ZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ + * Èë¿Ú²ÎÊý (int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) + *BMA_X_Value£ºXµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Y_Value£ºYµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Z_Value£ºZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + ¼ÓËÙ¶ÈֵΪ16λÓзûºÅÊý£¬·Ö±æÂÊΪ 2/512 (g), g=9.8m/s + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +extern void BMA_Pooling(int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value); + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.c new file mode 100644 index 0000000..2c40357 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.c @@ -0,0 +1,292 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "dma_itg.h" + + + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + + +/***********аå°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 + +#define HAL_BOARD_IO_E2PROM_SCL_PORT HAL_BOARD_IO_SHT_SCK_PORT +#define HAL_BOARD_IO_E2PROM_SCL_PIN HAL_BOARD_IO_SHT_SCK_PIN + + +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCL_DIR_OUT HAL_E2PROM_SCK_DIR_OUT +#define HAL_E2PROM_SCL_SET HAL_E2PROM_SCK_SET +#define HAL_E2PROM_SCL_CLR HAL_E2PROM_SCK_CLR + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define halMcuWaitUs ITGMcuWaitUs +#define NOP() asm("NOP") +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void ITGMcuWaitUs(uint16 usec); +void ITGMcuWaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +//--------- write a byte and checks the ack signal----------------- +static void M3_I2cWrite(uint8 dat); +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCK_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } + +} + +//--------- read a byte and checks the ack signal----------------- +static uint8 M3_I2cRead(void); +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + HAL_E2PROM_SCK_SET(); + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR(); + halMcuWaitUs(2); + } + // MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + // HAL_E2PROM_SDA_SET(); + + HAL_E2PROM_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_E2PROM_SCK_CLR(); + HAL_E2PROM_SDA_SET(); + + return res; +} +static void M3_I2cStart(void); +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +static void M3_I2cStop(void); +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +static void M3_I2cAck(void); +void M3_I2cAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_SET(); + HAL_E2PROM_SCK_CLR(); +} + +//------------------------------------------------------------------- +static void M3_I2cNAck(void); +void M3_I2cNAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_CLR(); + HAL_E2PROM_SCK_CLR(); +} + + +static void M3_WriteEEPROM(uint8 addr, uint8 ch); +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xD2);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); +} +static uint8 M3_ReadEEPROM(uint8 addr); +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + M3_I2cWrite(0xD2); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cStart(); + M3_I2cWrite(0xD3); //Æ÷¼þÉèÖÃΪ¶Á״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + M3_I2cNAck(); + M3_I2cStop(); + + return res; +} +static void M3_Init(void); +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + static char initflg=0; + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); + if(initflg==0) + { + M3_WriteEEPROM(0x16,0x18); + initflg=1 ; + } +} + +/******************************************************************* + * Ãû³Æ ITG_Pooling + * ¹¦ÄÜ ¶ÁÈ¡itg_3200µÄX,Y ZµÄ½ÇËÙ¶È + * Èë¿Ú²ÎÊý (int16* X_Value, int16* Y_Value, int16* Z_Value ) + *X_Value£ºXÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + *Y_Value£ºYÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + *Z_Value£ºZÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + ½ÇËÙ¶ÈֵΪ16λÓзûºÅÊý£¬µ¥Î»Îª ¶È/S + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************/ +void ITG_Pooling(int16* X_Value, int16* Y_Value, int16* Z_Value ) +{ + int8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + int8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + int16 num_x1=0,num_y1=0,num_z1=0; + int16 num_x=0,num_y=0,num_z=0; + uint8 i = 0; + M3_Init(); + for(i=0;i<10;i++) + { + while (!(M3_ReadEEPROM(0x1A) & 0x01)); + BMA_XM_Value = M3_ReadEEPROM(0x1D); + halMcuWaitUs(5); + BMA_XL_Value = M3_ReadEEPROM(0x1E); + halMcuWaitUs(5); + BMA_YM_Value = M3_ReadEEPROM(0x1F); + halMcuWaitUs(5); + BMA_YL_Value = M3_ReadEEPROM(0x20); + halMcuWaitUs(5); + BMA_ZM_Value = M3_ReadEEPROM(0x21); + halMcuWaitUs(5); + BMA_ZL_Value = M3_ReadEEPROM(0x22); + halMcuWaitUs(5); + num_x1 = (BMA_XM_Value << 8) | BMA_XL_Value; + num_y1 = (BMA_YM_Value << 8) | BMA_YL_Value; + num_z1 = (BMA_ZM_Value << 8) | BMA_ZL_Value; + num_x = num_x + num_x1; + num_y = num_y + num_y1; + num_z = num_z + num_z1; + } + // »»Ëã³É ¶È/S + *X_Value = (int16)(num_x/10/14.375); + *Y_Value = (int16)(num_y/10/14.375); + *Z_Value = (int16)(num_z/10/14.375); +} + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.h new file mode 100644 index 0000000..8c5c2aa --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/dma_itg.h @@ -0,0 +1,20 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned short +#define ulong unsigned long + + + +extern void ITG_Pooling(int16* X_Value, int16* Y_Value, int16* Z_Value ); + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.c new file mode 100644 index 0000000..f9dde86 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.c @@ -0,0 +1,160 @@ +/************************************************************************************************** + Filename: sh10.c + + Description: sh10 driver + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "get_adc.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ +void hal_adc_Init(void); + uint16 readVoltage(void); +//static volatile uint32 value; + +void hal_adc_Init(void) +{ + APCFG |=1; + P0SEL |= (1 << (0)); + P0DIR &= ~(1 << (0)); + +} +/****************************************************************************** + * Ãû³Æ get_adc + * ¹¦ÄÜ ¶ÁÈ¡A/DÖµ + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý 16λµçѹֵ£¬·Ö±æÂÊΪ10mV, Èç0x0102±íʾ2.58V + *****************************************************************************/ +uint16 get_adc(void) +{ + int32 value; + hal_adc_Init(); // ADC³õʼ»¯ + ADCIF = 0; //ÇåADC ÖжϱêÖ¾ + //²ÉÓûù×¼µçѹavdd5:3.3V£¬Í¨µÀ0£¬Æô¶¯ADת»¯ + ADCCON3 = (0x80 | 0x10 | 0x00); + while ( !ADCIF ) + { + ; //µÈ´ýADת»¯½áÊø + } + value = ADCH; + value = value<< 8; + value |= ADCL; + if(value&0x8000) + value=0; + // ADֵת»¯³Éµçѹֵ + // 0 ±íʾ 0V £¬32768 ±íʾ 3.3V + // µçѹֵ = (value*3.3)/32768 £¨V) + value = (value * 330); + value = value >> 15; // ³ýÒÔ32768 + // ·µ»Ø·Ö±æÂÊΪ0.01VµÄµçѹֵ + return (uint16)value; +} +#define ACH1 0 +#define ACH2 4 +#define ACH3 5 +#define ACH4 6 + +void hal_adc4_Init(void); +void hal_adc4_Init(void) +{ + APCFG |= (1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4); + P0SEL |= (1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4); + P0DIR &= ~((1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4)); + +} +#define AD4MA 32768*150*4/3300 +#define AD20MA 32768*150*20/3300 +//return 10ADÖµ£¬0---0v, 3ff---3.3v +uint16 adC_ch(uint8 ch); +uint16 adC_ch(uint8 ch) +{ + int32 value; + ADCIF = 0; //ÇåADC ÖжϱêÖ¾ + //²ÉÓûù×¼µçѹavdd5:3.3V£¬Í¨µÀch£¬Æô¶¯ADת»¯ + ADCCON3 = (0x80 | 0x10 | (ch&0x0f) ); + while ( !ADCIF ) + { + ; //µÈ´ýADת»¯½áÊø + } + value = ADCH; + value = value<< 8; + value |= ADCL; + if(value&0x8000) + value=0; + value= value>>5; + return (uint16)value; + /* + // ADֵת»¯³Éµçѹֵ + // 0 ±íʾ 0V £¬32768 ±íʾ 3.3V + // µçÁ÷ 0--4ma, 255--20ma, µç×è150r + value = (value - AD4MA)*255/(AD20MA - AD4MA); + if(value<0) + return 0; + if(value>255) + return 255; + return (uint8)value&0xff;*/ +} +/****************************************************************************** + * Ãû³Æ get_adc4 + * ¹¦ÄÜ ¶ÁÈ¡ËÄͨµÀA/DÖµ ch0 ch4 ch5 ch6 + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý + *****************************************************************************/ +void get_adc4ch(uint8 *buf) +{ + uint16 value; + hal_adc4_Init(); // ADC³õʼ»¯ + value=adC_ch(ACH1); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH2); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH3); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH4); + *buf=value; + buf++; + *buf=value>>8; +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.h new file mode 100644 index 0000000..17fc182 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_adc.h @@ -0,0 +1,16 @@ +/************************************************************************************************* + Filename: adc.h + + *****/ + + +#ifndef SIMPLE_adc_H +#define SIMPLE_adc_H + + +extern void hal_adc_Init(void); +extern uint16 get_adc(void); +extern void get_adc4ch(uint8 *buf); +#endif + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.c new file mode 100644 index 0000000..7a8a559 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.c @@ -0,0 +1,57 @@ +/************************************************************************************************** + Filename: + + Description: + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "get_swsensor.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ + +/****************************************************************************** + * Ãû³Æ get_swsensor + * ¹¦ÄÜ ¶ÁÈ¡¿ª¹ØÁ¿Öµ + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý 0»ò1µçƽ + *****************************************************************************/ +uint8 get_swsensor(void) +{ + P0SEL &= ~( 1 <<1); //ÉèÖÃp0.1ΪÆÕͨi/o¿Ú + P0DIR &= ~( 1 <<1); //ÉèÖÃp0.1ΪÊäÈë¿Ú + return P0_1; //·µ»Øp0.1µçƽ +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.h new file mode 100644 index 0000000..41ef756 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/get_swsensor.h @@ -0,0 +1,13 @@ +/************************************************************************************************* + Filename: + + *****/ + + +#ifndef SIMPLE_swsensor_H +#define SIMPLE_swsensor_H + +extern uint8 get_swsensor(void); +#endif + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.c new file mode 100644 index 0000000..fa93954 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.c @@ -0,0 +1,166 @@ +/************************************************************************************************** + Filename: sensor.c */ + + + +#include "hal_defs.h" +//#include "hal_types.h" +#include "hal_mcu.h" +#include "ioCC2530.h" +//#include "sh10.h" +#include "get_adc.h" +#include "sensor.h" +#include "dma_itg.h" +#include "bma020.h" +//#include "UART_PRINT.h" +//#include "OnBoard.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ +void hal_LightSensor_Init(void); +static uint16 readVoltage(void); + +uint16 get_guangdian_ad(void); +void get_4channel_ad(uint8 *buf); +uint8 get_rentihongwai(void); +void get_wendu_shidu(int16 *temp, int16 *hum); +//static volatile uint32 value; + +void hal_LightSensor_Init(void) +{ + APCFG |=1; + P0SEL |= (1 << (0)); + P0DIR &= ~(1 << (0)); + P0INP |=1; +} +/****************************************************************************** + * @fn readVoltage + * + * @brief read voltage from ADC + * + * @param none + * + * @return voltage + *******/ +static uint16 readVoltage(void) +{ + + uint32 value; + int16 value1; + hal_LightSensor_Init(); + // Clear ADC interrupt flag + ADCIF = 0; + + ADCCON3 = (0x80 | 0x10 | 0x00); + //ADCCON3 = (0x80 | 0x10 | 0x0c); + //²ÉÓûù×¼µçѹavdd5:3.3V + // Wait for the conversion to finish + while ( !ADCIF ); + + // Get the result + value1 = ADCH; + value1 = value1<< 8; + value1 |= ADCL; + if(value1<0) + value1=0; + value=value1; + + // value now contains measurement of 3.3V + // 0 indicates 0V and 32767 indicates 3.3V + // voltage = (value*3.3)/32767 volts + // we will multiply by this by 10 to allow units of 0.01 volts + value = value >> 6; // divide first by 2^6 + value = (uint32)(value * 330); + value = value >> 9; // ...and later by 2^9...to prevent overflow during multiplication + + return (uint16)value; + +} + +/****************************************************************************** + * @fn readsensor + * + * @brief read readsensor ´«¸ÐÆ÷²ÉÑù + * + * @param sensorid + * + * @return sensor value, len + *******/ +uint8 readsensor(uint8 sensorid, char *val) +{ + if(sensorid == 0x01) + { //ÎÂʪ¶È + //call_sht11(tmp1,tmp2); + + return 4; + } + if(sensorid == 0x02) + { // G_SENSOR + BMA_Pooling((int16*)val, (int16*)(val+2),(int16*)(val+4)); + return 6; + } + if(sensorid == 0x03) + { //ÍÓÂÝÒÇ + ITG_Pooling((int16*)val, (int16*)(val+2),(int16*)(val+4)); + return 6; + } + if(sensorid == 0x4) + { //ÈËÌå p0.1 + APCFG &= ~(1<<1); + P0SEL &= ~(1 << (1)); + P0DIR &= ~(1 << (1)); + *val=0; + if( P0_1==0 ) + *val=0x01; + return 2; + } + if(sensorid == 0x5) + { //ËÄͨµÀ ad + get_adc4ch(val); + return 8; + } + if(sensorid == 0x6) + { //¹âÕÕ adc »òÆäËû + *(int16*)val=get_adc(); + return 2; + } +} +uint16 get_guangdian_ad(void) +{ + return get_adc(); +} +void get_4channel_ad(uint8 *buf) +{ + get_adc4ch(buf); +} +//1 youren +uint8 get_rentihongwai(void) +{ + uint8 val; + APCFG &= ~(1<<1); + P0SEL &= ~(1 << (1)); + P0DIR &= ~(1 << (1)); + val=0; + if( P0_1==0 ) + val=0x01; + return val; +} + +void get_wendu_shidu(int16 *temp, int16 *hum) +{ + int16 tmp1,tmp2; + call_sht11 (&tmp1,&tmp2); + *temp = tmp1; + *hum = tmp2; +} +//1 dongzuo +void set_relay(uint8 status) +{ + if(status == 1){ + HAL_TURN_ON_DC1(); + }else{ + HAL_TURN_OFF_DC1(); + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.h new file mode 100644 index 0000000..ac08b19 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sensor.h @@ -0,0 +1,30 @@ +/************************************************************************************************** + Filename: + + +**************************************************************************************************/ + + +#ifndef SENSOR_H +#define SENSOR_H + +/****************************************************************************** + * CONSTANTS + */ + + +/****************************************************************************** + * PUBLIC FUNCTIONS + */ +extern uint8 readsensor(uint8 sensorid, char *val); + +extern uint16 get_guangdian_ad(void); +extern void get_4channel_ad(uint8 *buf); +extern uint8 get_rentihongwai(void); +extern void get_wendu_shidu(int16 *temp, int16 *hum); +extern void set_relay(uint8 status); +#endif // + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.c b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.c new file mode 100644 index 0000000..3dfd53c --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.c @@ -0,0 +1,325 @@ +/************************************************************************************************** + Filename: sh10.c + + Description: sh10 driver + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +#include "sh10.h" + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + +#if defined (BOARD_OLD) +/***********¾É°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 2 +#define HAL_BOARD_IO_SHT_SCK_PIN 4 +#define HAL_BOARD_IO_SHT_SDA_PORT 2 +#define HAL_BOARD_IO_SHT_SDA_PIN 3 +#else +/***********аå°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 +#endif + + +#define HAL_SHT_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_SHT_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_SHT_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) + +#define HAL_SHT_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_SHT_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_SHT_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) + + + + +#define ACK 1 +#define noACK 0 +#define measure_temp 0x03 //²âÁ¿Î¶ÈÃüÁî +#define measure_humi 0x05 //²âÁ¿Êª¶ÈÃüÁî +#define RESET 0x1e //ÈíÆô¶¯ + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +//------------------------Êý¾Ý½á¹¹Ì嶨Òå--------------------------- +typedef union //±£´æËù²âµÃµÄζȣ¦Êª¶ÈÖµ +{ + uint i; + float f; +} value; +//WENSHIDU S; + + + + + + +/****************************************************************************** + * LOCAL FUNCTIONS + */ + +void SHT11_DELAY(uint16 usec); +char write_byte(unsigned char value); +char read_byte(unsigned char ack); +void transstart(void); +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); +void calc_sth11(float *p_humidity ,float *p_temperature); + + +#define SENSOR_SHT10 + + + + + +#if defined SENSOR_SHT10 + +void SHT11_DELAY(uint16 usec) +{ + usec >>= 1; + + while (usec--) + { + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + } +} + + + + +char write_byte(unsigned char value) +{ + unsigned char i,error=0; + + for (i=0x80;i>0;i/=2) + { + if (i & value) + HAL_SHT_SDA_SET(); + else + HAL_SHT_SDA_CLR(); + + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + } + + HAL_SHT_SDA_SET(); + HAL_SHT_SDA_DIR_IN(); + HAL_SHT_SCK_SET(); + error=HAL_SHT_SDA_VAL(); + HAL_SHT_SCK_CLR(); + + return error; +} + +char read_byte(unsigned char ack) +{ + unsigned char i,val=0; + + HAL_SHT_SDA_SET(); + HAL_SHT_SDA_DIR_IN(); + for (i=0x80;i>0;i/=2) + { + HAL_SHT_SCK_SET(); + if (HAL_SHT_SDA_VAL()) + val=(val | i); + HAL_SHT_SCK_CLR(); + } + MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,!ack); + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + HAL_SHT_SDA_SET(); + + return val; +} + +void transstart(void) +{ + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SDA_CLR(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + SHT11_DELAY(2); + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + + HAL_SHT_SDA_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + SHT11_DELAY(2); + HAL_SHT_SDA_CLR(); +} + +void connectionreset(void) +{ + unsigned char i; + HAL_SHT_SDA_SET(); + HAL_SHT_SCK_CLR(); + for(i=0;i<9;i++) + { + HAL_SHT_SCK_SET(); + HAL_SHT_SCK_CLR(); + } + transstart(); +} + +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) +{ + unsigned char error=0; + unsigned int i; + + connectionreset(); + switch(mode) + { + case 0 : error+=write_byte(measure_temp); break; + case 1 : error+=write_byte(measure_humi); break; + default : break; + } + + HAL_SHT_SDA_DIR_IN(); + for (i=0;i<65535;i++) + { + SHT11_DELAY(10); + if(HAL_SHT_SDA_VAL()== 0) break; + } + if(HAL_SHT_SDA_VAL()) + { + error+=1; + } + + *(p_value+1) =read_byte(ACK); + *(p_value)=read_byte(ACK); + *p_checksum =read_byte(noACK); + return error; +} +/****************************************************************************** + * Ãû³Æ calc_sth11 + * ¹¦ÄÜ ¼ÆËãÎÂʪ¶ÈÖµ + * Èë¿Ú²ÎÊý float *p_humidity ,float *p_temperature + ʪ¶È ÎÂ¶È + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void calc_sth11(float *p_humidity ,float *p_temperature) +{ + const float C1=-4.0; + const float C2=+0.0405; + const float C3=-0.0000028; + const float T1=+0.01; + const float T2=+0.00008; + float rh=*p_humidity; + float t=*p_temperature; + float rh_lin; + float rh_true; + float t_C; + t_C=t*0.01 - 40; + rh_lin=C3*rh*rh + C2*rh + C1; + rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; + if(rh_true>100)rh_true=100; + if(rh_true<0.1)rh_true=0.1; + *p_temperature=t_C; + *p_humidity=rh_true; +} + +/****************************************************************************** + * Ãû³Æ call_sht11 + * ¹¦ÄÜ ¶ÁÈ¡SH1xÎÂʪ¶ÈÖµ + * Èë¿Ú²ÎÊý void call_sht11(unsigned int *tem_val,unsigned int *hum_val) + *tem_val:ζȴæ·ÅµØÖ·£¬0.1¶È·Ö±æÂÊ + *hum_val:ʪ¶È´æ·ÅµØÖ·£¬0.1·Ö±æÂÊ + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void call_sht11(unsigned int *tem_val,unsigned int *hum_val) +{ + value humi_val,temp_val; + unsigned char error = 0,checksum; + *tem_val=0; + *hum_val=0; + //Æô¶¯SH1xµÄʪ¶È²âÁ¿ + error+=s_measure((unsigned char*) &humi_val.i,&checksum,1); + //Æô¶¯SH1xµÄζȲâÁ¿ + error+=s_measure((unsigned char*) &temp_val.i,&checksum,0); + if(error!=0) + { //²âÊÔ´íÎ󣬽øÐÐÈí¸´Î» + connectionreset(); + } + else + { //²âÊÔÊý¾Ý´¦Àí + humi_val.f=(float)humi_val.i; + temp_val.f=(float)temp_val.i; + //¼ÆËãÎÂʪ¶Èʵ¼ÊÖµ + calc_sth11(&humi_val.f,&temp_val.f); + //ת»¯³É0.1µÄ·Ö±æÂÊ + *tem_val=temp_val.f*10; + *hum_val=humi_val.f*10; + } +} + +#endif + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.h b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.h new file mode 100644 index 0000000..c7f6f99 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/sensor_drv/sh10.h @@ -0,0 +1,14 @@ +/************************************************************************************************* + Filename: sh10.h + + *****/ + + +#ifndef SIMPLE_SH10_H +#define SIMPLE_SH10_H + +extern void call_sht11(unsigned int *tem, unsigned int *hum); +extern void connectionreset(void); +#endif + + diff --git a/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.cspy.bat b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.cspy.bat new file mode 100644 index 0000000..4b0b9ff --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.cspy.bat @@ -0,0 +1,15 @@ +@REM This batch file has been generated by the IAR Embedded Workbench +@REM C-SPY Debugger, as an aid to preparing a command line for running +@REM the cspybat command line utility using the appropriate settings. +@REM +@REM You can launch cspybat by typing the name of this batch file followed +@REM by the name of the debug file (usually an ELF/DWARF or UBROF file). +@REM Note that this file is generated every time a new debug session +@REM is initialized, so you may want to move or rename the file before +@REM making changes. +@REM + + +"D:\IAR Systems\Embedded Workbench 6.0\common\bin\cspybat" "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051proc.dll" "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051emu_cc.dll" %1 --plugin "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051bat.dll" --backend -B "--proc_core" "plain" "--proc_code_model" "banked" "--proc_nr_virtual_regs" "8" "--proc_pdata_bank_reg_addr" "0x93" "--proc_dptr_nr_of" "1" "--proc_codebank_reg" "0x9F" "--proc_codebank_start" "0x8000" "--proc_codebank_end" "0xFFFF" "--proc_codebank_mask" "0xFF" "--proc_data_model" "large" "-p" "D:\IAR Systems\Embedded Workbench 6.0\8051\config\devices\Texas Instruments\ioCC2530F256.ddf" "--proc_exclude_exit_breakpoint" "--proc_driver" "chipcon" "--retain_memory" "--verify_download" "use_crc16" "--stack_overflow" "--number_of_banks" "4" + + diff --git a/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dbgdt b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dbgdt new file mode 100644 index 0000000..e068f91 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dbgdt @@ -0,0 +1,5 @@ + + + + + diff --git a/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dni b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dni new file mode 100644 index 0000000..aef7a55 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.dni @@ -0,0 +1,24 @@ +[DebugChecksum] +Checksum=521045402 +[Log file] +LoggingEnabled=_ 0 +LogFile=_ "" +Category=_ 0 +[StackPlugin] +Enabled=1 +OverflowWarningsEnabled=1 +WarningThreshold=90 +SpWarningsEnabled=1 +WarnHow=0 +UseTrigger=1 +TriggerName=main +LimitSize=0 +ByteLimit=50 +[Breakpoints] +Count=0 +[ChipconEmu] +Stop timers on halt=1 +Leave target running=1 +[Aliases] +Count=0 +SuppressDialog=0 diff --git a/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.wsdt b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.wsdt new file mode 100644 index 0000000..926a6d0 --- /dev/null +++ b/Zigbee无线通信/Aæ�¿/Project/settings/rf_set.wsdt @@ -0,0 +1,49 @@ + + + + + + rf_set/Debug + + + + + + + + + 140272727 + + 2098026165201861 + + + + + + TabID-1552-11148 + Workspace + Workspace + + + rf_setrf_set/Outputrf_set/apprf_set/basicrfrf_set/boardrf_set/common + + + + 0TabID-5639-8760BuildBuildTabID-28319-24938Debug LogDebug-Log0 + + + + + + TextEditor$WS_DIR$\rf_set.c096163416340TextEditor$WS_DIR$\..\CC2530_lib\basicrf\basic_rf.c03961304013057TextEditor$WS_DIR$\..\CC2530_lib\common\hal_mcu.c093133413460100000010000001 + + + + + + + iaridepm.enu1-2-2545214-2-213115085286194049140625707633-2-21831538-2-215401851002604239327111979239327 + + + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.c new file mode 100644 index 0000000..b41d6f7 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.c @@ -0,0 +1,523 @@ +//------------------------------------------------------------------- +// Filename: basic_rf.c +// Description: Basic RF library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_mcu.h" // Using halMcuWaitUs() +#include "hal_rf.h" +#include "basic_rf.h" +#ifdef SECURITY_CCM + #include "hal_rf_security.h" + #include "basic_rf_security.h" +#endif +#include + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Packet and packet part lengths +#define PKT_LEN_MIC 8 +#define PKT_LEN_SEC PKT_LEN_UNSEC + PKT_LEN_MIC +#define PKT_LEN_AUTH 8 +#define PKT_LEN_ENCR 24 + +// Packet overhead ((frame control field, sequence number, PAN ID, +// destination and source) + (footer)) +// Note that the length byte itself is not included included in the packet length +#define BASIC_RF_PACKET_OVERHEAD_SIZE ((2 + 1 + 2 + 2 + 2) + (2)) +#define BASIC_RF_MAX_PAYLOAD_SIZE (127 - BASIC_RF_PACKET_OVERHEAD_SIZE - BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_LEN_MIC) +#define BASIC_RF_ACK_PACKET_SIZE 5 +#define BASIC_RF_FOOTER_SIZE 2 +#define BASIC_RF_HDR_SIZE 10 + +// The time it takes for the acknowledgment packet to be received after the +// data packet has been transmitted. +#define BASIC_RF_ACK_DURATION (0.5 * 32 * 2 * ((4 + 1) + (1) + (2 + 1) + (2))) +#define BASIC_RF_SYMBOL_DURATION (32 * 0.5) + +// The length byte +#define BASIC_RF_PLD_LEN_MASK 0x7F + +// Frame control field +#define BASIC_RF_FCF_NOACK 0x8841 +#define BASIC_RF_FCF_ACK 0x8861 +#define BASIC_RF_FCF_ACK_BM 0x0020 +#define BASIC_RF_FCF_BM (~BASIC_RF_FCF_ACK_BM) +#define BASIC_RF_SEC_ENABLED_FCF_BM 0x0008 + +// Frame control field LSB +#define BASIC_RF_FCF_NOACK_L LO_UINT16(BASIC_RF_FCF_NOACK) +#define BASIC_RF_FCF_ACK_L LO_UINT16(BASIC_RF_FCF_ACK) +#define BASIC_RF_FCF_ACK_BM_L LO_UINT16(BASIC_RF_FCF_ACK_BM) +#define BASIC_RF_FCF_BM_L LO_UINT16(BASIC_RF_FCF_BM) +#define BASIC_RF_SEC_ENABLED_FCF_BM_L LO_UINT16(BASIC_RF_SEC_ENABLED_FCF_BM) + +// Auxiliary Security header +#define BASIC_RF_AUX_HDR_LENGTH 5 +#define BASIC_RF_LEN_AUTH BASIC_RF_PACKET_OVERHEAD_SIZE + BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_FOOTER_SIZE +#define BASIC_RF_SECURITY_M 2 +#define BASIC_RF_LEN_MIC 8 +#ifdef SECURITY_CCM + #undef BASIC_RF_HDR_SIZE + #define BASIC_RF_HDR_SIZE 15 +#endif + +// Footer +#define BASIC_RF_CRC_OK_BM 0x80 + + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// The receive struct +typedef struct +{ + uint8 seqNumber; + uint16 srcAddr; + uint16 srcPanId; + int8 length; + uint8 *pPayload; + uint8 ackRequest; + int8 rssi; + volatile uint8 isReady; + uint8 status; +} basicRfRxInfo_t; + +// Tx state +typedef struct +{ + uint8 txSeqNumber; + volatile uint8 ackReceived; + uint8 receiveOn; + uint32 frameCounter; +} basicRfTxState_t; + + +// Basic RF packet header (IEEE 802.15.4) +typedef struct +{ + uint8 packetLength; + uint8 fcf0; // Frame control field LSB + uint8 fcf1; // Frame control field MSB + uint8 seqNumber; + uint16 panId; + uint16 destAddr; + uint16 srcAddr; + #ifdef SECURITY_CCM + uint8 securityControl; + uint8 frameCounter[4]; + #endif +} basicRfPktHdr_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +static basicRfRxInfo_t rxi = +{ + 0xFF +}; // Make sure sequence numbers are +static basicRfTxState_t txState = +{ + 0x00 +}; // initialised and distinct. + +static basicRfCfg_t *pConfig; +static uint8 txMpdu[BASIC_RF_MAX_PAYLOAD_SIZE + BASIC_RF_PACKET_OVERHEAD_SIZE + 1]; +static uint8 rxMpdu[128]; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfBuildHeader +// @brief Builds packet header according to IEEE 802.15.4 frame format +// @param buffer - Pointer to buffer to write the header +// destAddr - destination short address +// payloadLength - length of higher layer payload +// @return uint8 - length of header +//------------------------------------------------------------------- +static uint8 basicRfBuildHeader(uint8 *buffer, uint16 destAddr, uint8 payloadLength) +{ + basicRfPktHdr_t *pHdr; + uint16 fcf; + + pHdr = (basicRfPktHdr_t*)buffer; + + // Populate packet header + pHdr->packetLength = payloadLength + BASIC_RF_PACKET_OVERHEAD_SIZE; + //pHdr->frameControlField = pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK; + fcf = pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK; + pHdr->fcf0 = LO_UINT16(fcf); + pHdr->fcf1 = HI_UINT16(fcf); + pHdr->seqNumber = txState.txSeqNumber; + pHdr->panId = pConfig->panId; + pHdr->destAddr = destAddr; + pHdr->srcAddr = pConfig->myAddr; + + #ifdef SECURITY_CCM + + // Add security to FCF, length and security header + pHdr->fcf0 |= BASIC_RF_SEC_ENABLED_FCF_BM_L; + pHdr->packetLength += PKT_LEN_MIC; + pHdr->packetLength += BASIC_RF_AUX_HDR_LENGTH; + + pHdr->securityControl = SECURITY_CONTROL; + pHdr->frameCounter[0] = LO_UINT16(LO_UINT32(txState.frameCounter)); + pHdr->frameCounter[1] = HI_UINT16(LO_UINT32(txState.frameCounter)); + pHdr->frameCounter[2] = LO_UINT16(HI_UINT32(txState.frameCounter)); + pHdr->frameCounter[3] = HI_UINT16(HI_UINT32(txState.frameCounter)); + + #endif + + return BASIC_RF_HDR_SIZE; +} + +//------------------------------------------------------------------- +// @fn basicRfBuildMpdu +// @brief Builds mpdu (MAC header + payload) according to IEEE 802.15.4 +// frame format +// @param destAddr - Destination short address +// pPayload - pointer to buffer with payload +// payloadLength - length of payload buffer +// @return uint8 - length of mpdu +//------------------------------------------------------------------- +static uint8 basicRfBuildMpdu(uint16 destAddr, uint8 *pPayload, uint8 payloadLength) +{ + uint8 hdrLength, n; + + hdrLength = basicRfBuildHeader(txMpdu, destAddr, payloadLength); + + for (n = 0; n < payloadLength; n++) + { + txMpdu[hdrLength + n] = pPayload[n]; + } + return hdrLength + payloadLength; // total mpdu length +} + +//------------------------------------------------------------------- +// @fn basicRfRxFrmDoneIsr +// @brief Interrupt service routine for received frame from radio +// (either data or acknowlegdement) +// @param rxi - file scope variable info extracted from the last incoming +// frame +// txState - file scope variable that keeps tx state info +// @return none +//------------------------------------------------------------------- +static void basicRfRxFrmDoneIsr(void) +{ + basicRfPktHdr_t *pHdr; + uint8 *pStatusWord; + #ifdef SECURITY_CCM + uint8 authStatus = 0; + #endif + + // Map header to packet buffer + pHdr = (basicRfPktHdr_t*)rxMpdu; + + // Clear interrupt and disable new RX frame done interrupt + halRfDisableRxInterrupt(); + + // Enable all other interrupt sources (enables interrupt nesting) + halIntOn(); + + // Read payload length. + halRfReadRxBuf(&pHdr->packetLength, 1); + pHdr->packetLength &= BASIC_RF_PLD_LEN_MASK; // Ignore MSB + + // Is this an acknowledgment packet? + // Only ack packets may be 5 bytes in total. + if (pHdr->packetLength == BASIC_RF_ACK_PACKET_SIZE) + { + // Read the packet + halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength); + + rxi.ackRequest = !!(pHdr->fcf0 &BASIC_RF_FCF_ACK_BM_L); + + // Read the status word and check for CRC OK + pStatusWord = rxMpdu + 4; + + // Indicate the successful ACK reception if CRC and sequence number OK + if ((pStatusWord[1] &BASIC_RF_CRC_OK_BM) && (pHdr->seqNumber == txState.txSeqNumber)) + { + txState.ackReceived = TRUE; + } + // No, it is data + } + else + { + // It is assumed that the radio rejects packets with invalid length. + // Subtract the number of bytes in the frame overhead to get actual payload. + rxi.length = pHdr->packetLength - BASIC_RF_PACKET_OVERHEAD_SIZE; + + #ifdef SECURITY_CCM + rxi.length -= (BASIC_RF_AUX_HDR_LENGTH + BASIC_RF_LEN_MIC); + authStatus = halRfReadRxBufSecure(&rxMpdu[1], pHdr->packetLength, rxi.length, BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M); + #else + halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength); + #endif + + rxi.ackRequest = !!(pHdr->fcf0 &BASIC_RF_FCF_ACK_BM_L); + + // Read the source address + rxi.srcAddr = pHdr->srcAddr; + + // Read the packet payload + rxi.pPayload = rxMpdu + BASIC_RF_HDR_SIZE; + + // Read the FCS to get the RSSI and CRC + pStatusWord = rxi.pPayload + rxi.length; + #ifdef SECURITY_CCM + pStatusWord += BASIC_RF_LEN_MIC; + #endif + rxi.rssi = pStatusWord[0]; + + // Notify the application about the received data packet if the CRC is OK + // Throw packet if the previous packet had the same sequence number + if ((pStatusWord[1] &BASIC_RF_CRC_OK_BM) )//&& (rxi.seqNumber != pHdr->seqNumber)) + { + // If security is used check also that authentication passed + #ifdef SECURITY_CCM + if (authStatus == SUCCESS) + { + if ((pHdr->fcf0 &BASIC_RF_FCF_BM_L) == (BASIC_RF_FCF_NOACK_L | BASIC_RF_SEC_ENABLED_FCF_BM_L)) + { + rxi.isReady = TRUE; + } + } + #else + if (((pHdr->fcf0 &(BASIC_RF_FCF_BM_L)) == BASIC_RF_FCF_NOACK_L)) + { + rxi.isReady = TRUE; + } + #endif + } + rxi.seqNumber = pHdr->seqNumber; + } + // Enable RX frame done interrupt again + halIntOff(); + halRfEnableRxInterrupt(); +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfInit +// @brief Initialise basic RF datastructures. Sets channel, short address and +// PAN id in the chip and configures interrupt on packet reception +// @param pRfConfig - pointer to BASIC_RF_CONFIG struct. +// This struct must be allocated by higher layer +// txState - file scope variable that keeps tx state info +// rxi - file scope variable info extracted from the last incoming frame +// @return none +//------------------------------------------------------------------- +uint8 basicRfInit(basicRfCfg_t *pRfConfig) +{ + if (halRfInit() == FAILED) + { + return FAILED; + } + + halIntOff(); + + // Set the protocol configuration + pConfig = pRfConfig; + rxi.pPayload = NULL; + + txState.receiveOn = TRUE; + txState.frameCounter = 0; + + // Set channel + halRfSetChannel(pConfig->channel); + + // Write the short address and the PAN ID to the CC2520 RAM + halRfSetShortAddr(pConfig->myAddr); + halRfSetPanId(pConfig->panId); + + // if security is enabled, write key and nonce + #ifdef SECURITY_CCM + basicRfSecurityInit(pConfig); + #endif + + // Set up receive interrupt (received data or acknowlegment) + halRfRxInterruptConfig(basicRfRxFrmDoneIsr); + + halIntOn(); + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn basicRfSendPacket +// @brief Send packet +// @param destAddr - destination short address +// pPayload - pointer to payload buffer. This buffer must be +// allocated by higher layer. +// length - length of payload +// txState - file scope variable that keeps tx state info +// mpdu - file scope variable. Buffer for the frame to send +// @return basicRFStatus_t - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 basicRfSendPacket(uint16 destAddr, uint8 *pPayload, uint8 length) +{ + uint8 mpduLength; + uint8 status; + + // Turn on receiver if its not on + if (!txState.receiveOn) + { + halRfReceiveOn(); + } + + // Check packet length + length = MIN(length, BASIC_RF_MAX_PAYLOAD_SIZE); + + // Wait until the transceiver is idle + halRfWaitTransceiverReady(); + + // Turn off RX frame done interrupt to avoid interference on the SPI interface + halRfDisableRxInterrupt(); + + mpduLength = basicRfBuildMpdu(destAddr, pPayload, length); + + #ifdef SECURITY_CCM + halRfWriteTxBufSecure(txMpdu, mpduLength, length, BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M); + txState.frameCounter++; // Increment frame counter field + #else + halRfWriteTxBuf(txMpdu, mpduLength); + #endif + + // Turn on RX frame done interrupt for ACK reception + halRfEnableRxInterrupt(); + + // Send frame with CCA. return FAILED if not successful + if (halRfTransmit() != SUCCESS) + { + status = FAILED; + } + + // Wait for the acknowledge to be received, if any + if (pConfig->ackRequest) + { + txState.ackReceived = FALSE; + + // We'll enter RX automatically, so just wait until we can be sure that the ack reception should have finished + // The timeout consists of a 12-symbol turnaround time, the ack packet duration, and a small margin + halMcuWaitUs((12 *BASIC_RF_SYMBOL_DURATION) + (BASIC_RF_ACK_DURATION) + (2 *BASIC_RF_SYMBOL_DURATION) + 10); + + // If an acknowledgment has been received (by RxFrmDoneIsr), the ackReceived flag should be set + status = txState.ackReceived ? SUCCESS : FAILED; + } + else + { + status = SUCCESS; + } + + // Turn off the receiver if it should not continue to be enabled + if (!txState.receiveOn) + { + halRfReceiveOff(); + } + + if (status == SUCCESS) + { + txState.txSeqNumber++; + } + + #ifdef SECURITY_CCM + halRfIncNonceTx(); // Increment nonce value + #endif + + return status; +} + +//------------------------------------------------------------------- +// @fn basicRfPacketIsReady +// @brief Check if a new packet is ready to be read by next higher layer +// @param none +// @return uint8 - TRUE if a packet is ready to be read by higher layer +//------------------------------------------------------------------- +uint8 basicRfPacketIsReady(void) +{ + return rxi.isReady; +} + +//------------------------------------------------------------------- +// @fn basicRfReceive +// @brief Copies the payload of the last incoming packet into a buffer +// @param pRxData - pointer to data buffer to fill. This buffer must be +// allocated by higher layer. +// len - Number of bytes to read in to buffer +// rxi - file scope variable holding the information of the last +// incoming packet +// @return uint8 - number of bytes actually copied into buffer +//------------------------------------------------------------------- +uint8 basicRfReceive(uint8 *pRxData, uint16 len, int16 *pRssi) +{ + // Accessing shared variables -> this is a critical region + // Critical region start + halIntOff(); + memcpy(pRxData, rxi.pPayload, MIN(rxi.length, len)); + if (pRssi != NULL) + { + if (rxi.rssi < 128) + { + *pRssi = rxi.rssi - halRfGetRssiOffset(); + } + else + { + *pRssi = (rxi.rssi - 256) - halRfGetRssiOffset(); + } + } + rxi.isReady = FALSE; + halIntOn(); + + // Critical region end + return MIN(rxi.length, len); +} + +//------------------------------------------------------------------- +// @fn basicRfGetRssi +// @brief Copies the payload of the last incoming packet into a buffer +// @param none +// @return int8 - RSSI value +//------------------------------------------------------------------- +int8 basicRfGetRssi(void) +{ + if (rxi.rssi < 128) + { + return rxi.rssi - halRfGetRssiOffset(); + } + else + { + return (rxi.rssi - 256) - halRfGetRssiOffset(); + } +} + +//------------------------------------------------------------------- +// @fn basicRfReceiveOn +// @brief Turns on receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOn(void) +{ + txState.receiveOn = TRUE; + halRfReceiveOn(); +} + +//------------------------------------------------------------------- +// @fn basicRfReceiveOff +// @brief Turns off receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOff(void) +{ + txState.receiveOn = FALSE; + halRfReceiveOff(); +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.h new file mode 100644 index 0000000..655960e --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf.h @@ -0,0 +1,123 @@ +//------------------------------------------------------------------- +// Filename: basic_rf.h +// Description: Basic RF library header file +//------------------------------------------------------------------- +// The "Basic RF" library contains simple functions for packet transmission and +// reception with the IEEE 802.15.4 compliant radio devices. The intention of this +// library is to demonstrate how the radio devices are operated, and not to provide +// a complete and fully-functional packet protocol. The protocol uses 802.15.4 MAC compliant data +// and acknowledgment packets, however it contains only a small subset of the +// 802.15.4 standard: +// - Association, scanning nor beacons are not implemented +// - No defined coordinator/device roles (peer-to-peer, all nodes are equal) +// - Waits for the channel to become ready, but does not check CCA twice +// (802.15.4 CSMA-CA) +// - Does not retransmit packets +// +// INSTRUCTIONS: +// Startup: +// 1. Create a basicRfCfg_t structure, and initialize the members: +// 2. Call basicRfInit() to initialize the packet protocol. +// +// Transmission: +// 1. Create a buffer with the payload to send +// 2. Call basicRfSendPacket() +// +// Reception: +// 1. Check if a packet is ready to be received by highger layer with +// basicRfPacketIsReady() +// 2. Call basicRfReceive() to receive the packet by higher layer +// +// FRAME FORMATS: +// Data packets (without security): +// [Preambles (4)][SFD (1)][Length (1)][Frame control field (2)] +// [Sequence number (1)][PAN ID (2)][Dest. address (2)][Source address (2)] +// [Payload (Length - 2+1+2+2+2)][Frame check sequence (2)] +// +// Acknowledgment packets: +// [Preambles (4)][SFD (1)][Length = 5 (1)][Frame control field (2)] +// [Sequence number (1)][Frame check sequence (2)] +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + uint16 myAddr; // ±¾»úµØÖ· + uint16 panId; // ÍøÂçID + uint8 channel; // ͨÐÅÐŵÀ£¬ 11~26 + uint8 ackRequest; //Ó¦´ðÐźŠ+ #ifdef SECURITY_CCM + uint8 *securityKey; + uint8 *securityNonce; + #endif +} basicRfCfg_t; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS + + +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn basicRfInit +// @brief Initialise basic RF datastructures. Sets channel, short address and +// PAN id in the chip and configures interrupt on packet reception +// @param pRfConfig - pointer to BASIC_RF_CONFIG struct. +// This struct must be allocated by higher layer +// txState - file scope variable that keeps tx state info +// rxi - file scope variable info extracted from the last incoming frame +// @return none +//------------------------------------------------------------------- +uint8 basicRfInit(basicRfCfg_t *pRfConfig); + +//------------------------------------------------------------------- +// @fn basicRfSendPacket +// @brief Send packet +// @param destAddr - destination short address +// pPayload - pointer to payload buffer. This buffer must be +// allocated by higher layer. +// length - length of payload +// txState - file scope variable that keeps tx state info +// mpdu - file scope variable. Buffer for the frame to send +// @return basicRFStatus_t - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 basicRfSendPacket(uint16 destAddr, uint8 *pPayload, uint8 length); + +//------------------------------------------------------------------- +// @fn basicRfPacketIsReady +// @brief Check if a new packet is ready to be read by next higher layer +// @param none +// @return uint8 - TRUE if a packet is ready to be read by higher layer +//------------------------------------------------------------------- +uint8 basicRfPacketIsReady(void); + +//------------------------------------------------------------------- +// @fn basicRfReceive +// @brief Copies the payload of the last incoming packet into a buffer +// @param pRxData - pointer to data buffer to fill. This buffer must be +// allocated by higher layer. +// len - Number of bytes to read in to buffer +// rxi - file scope variable holding the information of the last +// incoming packet +// @return uint8 - number of bytes actually copied into buffer +//------------------------------------------------------------------- +uint8 basicRfReceive(uint8 *pRxData, uint16 len, int16 *pRssi); + +//------------------------------------------------------------------- +// @fn basicRfReceiveOn +// @brief Turns on receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOn(void); + +//------------------------------------------------------------------- +// @fn basicRfReceiveOff +// @brief Turns off receiver on radio +// @param txState - file scope variable +// @return none +//------------------------------------------------------------------- +void basicRfReceiveOff(void); + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.c new file mode 100644 index 0000000..b73faf0 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: basic_rf_security.c +// Description: Basic RF security library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#ifdef SECURITY_CCM + #include "hal_defs.h" + #include "hal_rf_security.h" + #include "basic_rf.h" + #include "basic_rf_security.h" + + //------------------------------------------------------------------- + // CONSTANTS AND DEFINES + //------------------------------------------------------------------- + #define FLAG_FIELD 0x09 + #define NONCE_SIZE 16 + + //------------------------------------------------------------------- + // LOCAL VARIABLES + //------------------------------------------------------------------- + static uint8 nonceTx[NONCE_SIZE]; + static uint8 nonceRx[NONCE_SIZE]; + + //------------------------------------------------------------------- + // GLOBAL FUNCTIONS + //------------------------------------------------------------------- + //------------------------------------------------------------------- + // @fn basicRfSecurityInit + // @brief Initialise key and nonces and write to radio + // @param pConfig - file scope variable holding configuration data for basic RF + // @return none + //------------------------------------------------------------------- + void basicRfSecurityInit(basicRfCfg_t *pConfig) + { + uint8 i; + + // Initialise nonce bytes to 0 + for (i = 0; i < NONCE_SIZE; i++) + { + nonceRx[i] = 0; + nonceTx[i] = 0; + } + + // Set nonce flag field (Byte 0) + nonceRx[0] = FLAG_FIELD; + nonceTx[0] = FLAG_FIELD; + + // Set byte 7 and 8 of nonce to myAddr + nonceTx[8] = (uint8)pConfig->myAddr; + nonceTx[7] = (uint8)(pConfig->myAddr >> 8); + + // Set Security mode field of nonces (Byte 13) + nonceRx[13] = SECURITY_CONTROL; + nonceTx[13] = SECURITY_CONTROL; + + halRfSecurityInit(pConfig->securityKey, nonceRx, nonceTx); + } +#endif diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.h new file mode 100644 index 0000000..1af1cdf --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/basicrf/basic_rf_security.h @@ -0,0 +1,8 @@ +//------------------------------------------------------------------- +// Filename: basic_rf_security.h +// Description: Basic RF security library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void basicRfSecurityInit(basicRfCfg_t *pConfig); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.c new file mode 100644 index 0000000..1a1eb90 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.c @@ -0,0 +1,17 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_digio.h" +#include "hal_int.h" +#include "hal_board.h" +#include "hal_uart.h" + +void halBoardInit(void) +{ + halMcuInit(); + halUartInit(115200); + halIntOn(); +} + + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.h new file mode 100644 index 0000000..51cae44 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_board.h @@ -0,0 +1,307 @@ +//------------------------------------------------------------------- +// Filename: hal_board.h +// Description: MCU-KIT1 board with CC2530 +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS +//------------------------------------------------------------------- + +// Board properties +#define BOARD_NAME "ZIGBEE-DMA2530" +#define NUM_LEDS 4 + +// Clock +#define BSP_CONFIG_CLOCK_MHZ 32 + + +//RFID_FLAG +#define HAL_BOARD_IO_FLAG_PORT 1 +#define HAL_BOARD_IO_FLAG_PIN 1 + +//CD2530_Test_Pin +#define HAL_BOARD_IO_TEST1_PORT 1 +#define HAL_BOARD_IO_TEST1_PIN 3 +#define HAL_BOARD_IO_TEST2_PORT 1 +#define HAL_BOARD_IO_TEST2_PIN 4 +#define HAL_BOARD_IO_TEST3_PORT 1 +#define HAL_BOARD_IO_TEST3_PIN 5 +#define HAL_BOARD_IO_TEST4_PORT 0 +#define HAL_BOARD_IO_TEST4_PIN 0 + + +// LEDs +#define HAL_BOARD_IO_LED_PORT 1 + +//#define HAL_BOARD_IO_LED_1_PORT 1 +//#define HAL_BOARD_IO_LED_1_PIN 2 +#define HAL_BOARD_IO_LED_1_PORT 1 +#define HAL_BOARD_IO_LED_1_PIN 0 +#define HAL_BOARD_IO_LED_2_PORT 1 +#define HAL_BOARD_IO_LED_2_PIN 1 +#define HAL_BOARD_IO_LED_3_PORT 1 +#define HAL_BOARD_IO_LED_3_PIN 3 +#define HAL_BOARD_IO_LED_4_PORT 1 +#define HAL_BOARD_IO_LED_4_PIN 4 + +//DC +#define HAL_BOARD_IO_DC_1_PORT 2 +#define HAL_BOARD_IO_DC_1_PIN 0 +#define HAL_BOARD_IO_DC_2_PORT 1 +#define HAL_BOARD_IO_DC_2_PIN 7 + +//TC72 +#define HAL_BOARD_IO_TC72_SDI_PORT 0 +#define HAL_BOARD_IO_TC72_SDI_PIN 4 +#define HAL_BOARD_IO_TC72_SCL_PORT 0 +#define HAL_BOARD_IO_TC72_SCL_PIN 5 +#define HAL_BOARD_IO_TC72_SDO_PORT 0 +#define HAL_BOARD_IO_TC72_SDO_PIN 6 +#define HAL_BOARD_IO_TC72_SSS_PORT 0 +#define HAL_BOARD_IO_TC72_SSS_PIN 7 + +//AT24C02(E2PROM) +#define HAL_BOARD_IO_E2PROM_SCL_PORT 0 +#define HAL_BOARD_IO_E2PROM_SCL_PIN 6 +#define HAL_BOARD_IO_E2PROM_SDA_PORT 0 +#define HAL_BOARD_IO_E2PROM_SDA_PIN 7 + +//ITG +#define HAL_BOARD_IO_E2PROM_SCK_PORT 0 +#define HAL_BOARD_IO_E2PROM_SCK_PIN 6 +#define HAL_BOARD_IO_E2PROM_SDA_PORT 0 +#define HAL_BOARD_IO_E2PROM_SDA_PIN 7 + + +// SPI +#define HAL_BOARD_IO_SPI_MISO_PORT 0 +#define HAL_BOARD_IO_SPI_MISO_PIN 6 +#define HAL_BOARD_IO_SPI_MOSI_PORT 0 +#define HAL_BOARD_IO_SPI_MOSI_PIN 4 +#define HAL_BOARD_IO_SPI_CLK_PORT 0 +#define HAL_BOARD_IO_SPI_CLK_PIN 5 +#define HAL_BOARD_IO_SPI_CS_PORT 0 +#define HAL_BOARD_IO_SPI_CS_PIN 7 + +//SHT +#define HAL_BOARD_IO_SHT_SCK_PORT 2 +#define HAL_BOARD_IO_SHT_SCK_PIN 4 +#define HAL_BOARD_IO_SHT_SDA_PORT 2 +#define HAL_BOARD_IO_SHT_SDA_PIN 3 + +//SR04£¨³¬Éù²¨£© +#define HAL_BOARD_IO_SR04_TX_PORT 2 +#define HAL_BOARD_IO_SR04_TX_PIN 3 +#define HAL_BOARD_IO_SR04_RX_PORT 2 +#define HAL_BOARD_IO_SR04_RX_PIN 4 + +//TCS (ÑÕÉ«) +#define HAL_BOARD_IO_TCS_S2_PORT 2 +#define HAL_BOARD_IO_TCS_S2_PIN 3 +#define HAL_BOARD_IO_TCS_S3_PORT 2 +#define HAL_BOARD_IO_TCS_S3_PIN 4 + + +// I2C +#define HAL_BOARD_IO_I2C_SCL_PORT 0 +#define HAL_BOARD_IO_I2C_SCL_PIN 5 +#define HAL_BOARD_IO_I2C_SDA_PORT 0 +#define HAL_BOARD_IO_I2C_SDA_PIN 4 + +// LED +#define HAL_BOARD_IO_LED_DATA_PORT 1 + +// A/D +#define HAL_BOARD_IO_ADC_PORT 0 +#define HAL_BOARD_IO_ADC_CH 0 + +// INT +#define HAL_BOARD_IO_INT_PORT 0 +#define HAL_BOARD_IO_INT_PIN 1 + +// DO +#define HAL_BOARD_IO_DO_PORT 0 +#define HAL_BOARD_IO_DO_PIN 0 + +//OUT +#define HAL_BOARD_IO_OUT_PORT 1 +#define HAL_BOARD_IO_OUT_PIN 7 + +// UART +#define HAL_BOARD_IO_UART_TXD_PORT 0 +#define HAL_BOARD_IO_UART_TXD_PIN 3 +#define HAL_BOARD_IO_UART_RXD_PORT 0 +#define HAL_BOARD_IO_UART_RXD_PIN 2 + +// UART1 +#define HAL_BOARD_IO_UART1_TXD_PORT 1 +#define HAL_BOARD_IO_UART1_TXD_PIN 6 +#define HAL_BOARD_IO_UART1_RXD_PORT 1 +#define HAL_BOARD_IO_UART1_RXD_PIN 7 + +// Debounce +#define HAL_DEBOUNCE(expr) { char i; for (i=0; i<50; i++) { if (!(expr)) i = 0; } } + +// Assert +#define HAL_ASSERT(szErrMsg) { halAssertHandler(szErrMsg); } + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- + +//CD2530_Test_Pin + +#define HAL_BOARD_TEST1_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST1_PORT, HAL_BOARD_IO_TEST1_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST2_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST2_PORT, HAL_BOARD_IO_TEST2_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST3_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST3_PORT, HAL_BOARD_IO_TEST3_PIN, MCU_IO_PULLUP) +#define HAL_BOARD_TEST4_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_TEST4_PORT, HAL_BOARD_IO_TEST4_PIN, MCU_IO_PULLUP) + +#define HAL_BOARD_TEST1_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST1_PORT, HAL_BOARD_IO_TEST1_PIN) +#define HAL_BOARD_TEST2_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST2_PORT, HAL_BOARD_IO_TEST2_PIN) +#define HAL_BOARD_TEST3_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST3_PORT, HAL_BOARD_IO_TEST3_PIN) +#define HAL_BOARD_TEST4_VAL() MCU_IO_GET(HAL_BOARD_IO_TEST4_PORT, HAL_BOARD_IO_TEST4_PIN) + +//RFID_FLAG +#define HAL_FLAG_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_FLAG_PORT, HAL_BOARD_IO_FLAG_PIN) +#define HAL_FLAG_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_FLAG_PORT, HAL_BOARD_IO_FLAG_PIN) + +// LED +#define HAL_LED_SET_1() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_SET_2() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_SET_3() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_SET_4() MCU_IO_SET_HIGH(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + +#define HAL_LED_CLR_1() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_CLR_2() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_CLR_3() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_CLR_4() MCU_IO_SET_LOW(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + + +#define HAL_LED_TGL_1() MCU_IO_TGL(HAL_BOARD_IO_LED_1_PORT, HAL_BOARD_IO_LED_1_PIN) +#define HAL_LED_TGL_2() MCU_IO_TGL(HAL_BOARD_IO_LED_2_PORT, HAL_BOARD_IO_LED_2_PIN) +#define HAL_LED_TGL_3() MCU_IO_TGL(HAL_BOARD_IO_LED_3_PORT, HAL_BOARD_IO_LED_3_PIN) +#define HAL_LED_TGL_4() MCU_IO_TGL(HAL_BOARD_IO_LED_4_PORT, HAL_BOARD_IO_LED_4_PIN) + +//DC +#define HAL_DC_START_1() MCU_IO_SET_HIGH(HAL_BOARD_IO_DC_1_PORT, HAL_BOARD_IO_DC_1_PIN) +#define HAL_DC_START_2() MCU_IO_SET_HIGH(HAL_BOARD_IO_DC_2_PORT, HAL_BOARD_IO_DC_2_PIN) + +#define HAL_DC_STOP_1() MCU_IO_SET_LOW(HAL_BOARD_IO_DC_1_PORT, HAL_BOARD_IO_DC_1_PIN) +#define HAL_DC_STOP_2() MCU_IO_SET_LOW(HAL_BOARD_IO_DC_2_PORT, HAL_BOARD_IO_DC_2_PIN) + +//TC72 +#define HAL_TC72_SDI_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN) +#define HAL_TC72_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN) +#define HAL_TC72_SSS_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN) + +#define HAL_TC72_SDI_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN) +#define HAL_TC72_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN) +#define HAL_TC72_SSS_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN) + +#define HAL_TC72_SDO_VAL() MCU_IO_GET(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN) + +#define HAL_TC72_SDO_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN) + +//AT24C02(E2PROM) +#define HAL_E2PROM_SCL_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,0) +//#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +//#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,MCU_IO_PULLUP) + +#define HAL_E2PROM_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN) +#define HAL_E2PROM_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN) + +//#define HAL_E2PROM_SDA_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) +//#define HAL_E2PROM_SDA_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +//#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +//ITG +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,MCU_IO_PULLUP) + +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,0) + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,0) + +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN) + +// LED_ALL +#define HAL_LED_PORT(val) MCU_PORT_OUTPUT(HAL_BOARD_IO_LED_DATA_PORT, val) + +// SPI +#define HAL_SPI_INIT() {MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN, 1);MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN, 1);MCU_IO_OUTPUT(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN, 1);MCU_IO_INPUT(HAL_BOARD_IO_SPI_MISO_PORT, HAL_BOARD_IO_SPI_MISO_PIN,MCU_IO_PULLUP);} + +#define HAL_SPI_MOSI_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN) +#define HAL_SPI_MOSI_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_MOSI_PORT, HAL_BOARD_IO_SPI_MOSI_PIN) + +#define HAL_SPI_CLK_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN) +#define HAL_SPI_CLK_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_CLK_PORT, HAL_BOARD_IO_SPI_CLK_PIN) + +#define HAL_SPI_CS_EN() MCU_IO_SET_LOW(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN) +#define HAL_SPI_CS_DIS() MCU_IO_SET_HIGH(HAL_BOARD_IO_SPI_CS_PORT, HAL_BOARD_IO_SPI_CS_PIN) + +#define HAL_SPI_MISO_VAL() MCU_IO_GET(HAL_BOARD_IO_SPI_MISO_PORT, HAL_BOARD_IO_SPI_MISO_PIN) + +//SHT +#define HAL_SHT_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_SHT_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1)//MCU_IO_SET_HIGH(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN) +#define HAL_SHT_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0)//MCU_IO_SET_LOW(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN) + +#define HAL_SHT_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1)//MCU_IO_SET_HIGH(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define HAL_SHT_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0)//MCU_IO_SET_LOW(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define HAL_SHT_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,MCU_IO_PULLUP) +#define HAL_SHT_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) + +//SR04£¨³¬Éù²¨£© + +#define HAL_SHT_TX_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,0) +#define HAL_SHT_TX_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,1) +#define HAL_SHT_TX_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SR04_TX_PORT, HAL_BOARD_IO_SR04_TX_PIN,0) + +#define HAL_SHT_RX_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SR04_RX_PORT, HAL_BOARD_IO_SR04_RX_PIN,MCU_IO_PULLUP) +#define HAL_SHT_RX_VAL() MCU_IO_GET(HAL_BOARD_IO_SR04_RX_PORT, HAL_BOARD_IO_SR04_RX_PIN) + +//flame(»ðÑæ) +#define HAL_SHT_FLAME_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH,MCU_IO_PULLUP) +#define HAL_SHT_FLAME_VAL() MCU_IO_GET(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH) + +//TCS(ÑÕÉ«) +#define HAL_SHT_S2_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,0) +#define HAL_SHT_S2_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,1) +#define HAL_SHT_S2_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S2_PORT, HAL_BOARD_IO_TCS_S2_PIN,0) + +#define HAL_SHT_S3_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,0) +#define HAL_SHT_S3_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,1) +#define HAL_SHT_S3_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_TCS_S3_PORT, HAL_BOARD_IO_TCS_S3_PIN,0) + +// I2C +#define HAL_I2C_SCL_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN,0) +#define HAL_I2C_SCL_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN) +#define HAL_I2C_SCL_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_I2C_SCL_PORT, HAL_BOARD_IO_I2C_SCL_PIN) + +#define HAL_I2C_SDA_SET() MCU_IO_SET_HIGH(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) +#define HAL_I2C_SDA_CLR() MCU_IO_SET_LOW(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) +#define HAL_I2C_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN,1) +#define HAL_I2C_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN,MCU_IO_PULLUP) +#define HAL_I2C_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_I2C_SDA_PORT, HAL_BOARD_IO_I2C_SDA_PIN) + +// DO +#define HAL_DO_OFF() MCU_IO_SET_HIGH(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN) +#define HAL_DO_ON() MCU_IO_SET_LOW(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN) +#define HAL_DO_OUTPUT() MCU_IO_OUTPUT(HAL_BOARD_IO_DO_PORT, HAL_BOARD_IO_DO_PIN, 1) + +//INT +#define HAL_INT_VAL() MCU_IO_GET(HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN) + +//OUT +#define HAL_OUT_VAL() MCU_IO_GET(HAL_BOARD_IO_OUT_PORT, HAL_BOARD_IO_OUT_PIN) + + +//------------------------------------------------------------------- +// FUNCTION PROTOTYPES +//------------------------------------------------------------------- +void halBoardInit(void); + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.c new file mode 100644 index 0000000..1980f63 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.c @@ -0,0 +1,98 @@ +//------------------------------------------------------------------- +// Filename: hal_led.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_led.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halLedSet +// @brief Turn LED on. +// @param uint8 id - led to set +// @return none +//------------------------------------------------------------------- +void halLedSet(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_SET_1(); + break; + case 2: + HAL_LED_SET_2(); + break; + case 3: + HAL_LED_SET_3(); + break; + case 4: + HAL_LED_SET_4(); + break; + + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halLedClear +// @brief Turn LED off. +// @param uint8 id - led to clear +// @return none +//------------------------------------------------------------------- +void halLedClear(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_CLR_1(); + break; + case 2: + HAL_LED_CLR_2(); + break; + case 3: + HAL_LED_CLR_3(); + break; + case 4: + HAL_LED_CLR_4(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halLedToggle +// @brief Change state of LED. If on, turn it off. Else turn on. +// @param uint8 id - led to toggle +// @return none +//------------------------------------------------------------------- +void halLedToggle(uint8 id) +{ + switch (id) + { + case 1: + HAL_LED_TGL_1(); + break; + case 2: + HAL_LED_TGL_2(); + break; + case 3: + HAL_LED_TGL_3(); + break; + case 4: + HAL_LED_TGL_4(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.h new file mode 100644 index 0000000..1591503 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/board/hal_led.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: hal_led.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halLedSet(uint8 led_id); +void halLedClear(uint8 led_id); +void halLedToggle(uint8 led_id); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.c new file mode 100644 index 0000000..1ec78ce --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.c @@ -0,0 +1,57 @@ +#include "hal_defs.h" +#include "hal_led.h" +#include "TIMER.h" + + +uint8 SEND_DATA_FLAG = 0; +uint16 NUM = 0; + +void Timer4_Init(void) +{ + // Set prescaler divider value to 128 (32M/128 = 250KHZ) + T4CTL |= 0xE0; + T4CTL &= ~(0x10); // Stop timer + T4CTL &= ~(0x08); // ½ûÖ¹Òç³öÖÐ¶Ï + T4CTL |= 0x04; //¼ÆÊýÆ÷ÇåÁã + T4IE = 0; // Disable interrupt +} + +void Timer4_On(void) +{ + T4CTL |= 0x08; //ʹÄÜÒç³öÖÐ¶Ï + T4CTL &= ~(0x03);//0x00-0xFF + T4CTL |= 0x10; // Start timer + T4IE = 1; // Enable interrupt +} + +void Timer4_Off(void) +{ + T4CTL &= ~(0x08); // ½ûÖ¹Òç³öÖÐ¶Ï + T4CTL &= ~(0x10); // Stop timer + T4IE = 0; // Disable interrupt +} + +uint8 GetSendDataFlag(void) +{ + return SEND_DATA_FLAG; +} + + + +HAL_ISR_FUNCTION(T4_ISR, T4_VECTOR) +{ + T4OVFIF = 0; + T4IF = 0; + + NUM ++; + if(NUM == 500)//1953) //¶¨Ê±2s£¬·­×ªÒ»´Î£¨1/250000*256£©ÕâÊǽøÒ»´ÎÖжÏËùÐèÒªµÄʱ¼ä£¬½ø781´ÎÖжϴóÔ¼ÊÇ0.8s + { + NUM = 0; + SEND_DATA_FLAG = 1; + //halLedToggle(1); + } + else + { + SEND_DATA_FLAG = 0; + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.h new file mode 100644 index 0000000..16a4653 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/TIMER.h @@ -0,0 +1,4 @@ +void Timer4_Init(void); +void Timer4_On(void); +void Timer4_Off(void); +uint8 GetSendDataFlag(void); \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.c new file mode 100644 index 0000000..04d7258 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.c @@ -0,0 +1,56 @@ +//------------------------------------------------------------------- +// Filename: hal_adc.c +// Description: These functions/macros simplifies usage of the ADC. +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_adc.h" + +//------------------------------------------------------------------- +// @fn adcSampleSingle +// @brief +// This function makes the adc sample the given channel at the given +// resolution with the given reference. +// @param uint8 reference +// The reference to compare the channel to be sampled. +// uint8 resolution +// The resolution to use during the sample (8, 10, 12 or 14 bit) +// uint8 input +// The channel to be sampled. +// @return int16 +// The conversion result +//------------------------------------------------------------------- +unsigned int adcSampleSingle(uint8 reference, uint8 resolution, uint8 channel) +{ + unsigned int value; + + ADC_ENABLE_CHANNEL(channel); + + ADCIF = 0; + ADC_SINGLE_CONVERSION(reference | resolution | channel); + while (!ADCIF) + ; + + value = ((unsigned int) ADCH) << 8; + value |= (unsigned int) ADCL; + + if(value & 0x8000) + { + value = 0; + } + + value = value & 0x7FF8; + + ADC_DISABLE_CHANNEL(channel); + + // The variable 'value' contains 16 bits where + // bit 15 is a sign bit + // bit [14 .. 0] contain the absolute sample value + // Only the r upper bits are significant, where r is the resolution + // Resolution: + // 12 -> [14 .. 3] (bitmask 0x7FF8) + // 10 -> [14 .. 5] (bitmask 0x7FE0) + // 9 -> [14 .. 6] (bitmask 0x7FC0) + // 7 -> [14 .. 8] (bitmask 0x7F00) + + return value; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.h new file mode 100644 index 0000000..309c674 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_adc.h @@ -0,0 +1,86 @@ +//------------------------------------------------------------------- +// Filename: hal_adc.h +// Description: These functions/macros simplifies usage of the ADC. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND MACROS +//------------------------------------------------------------------- +// Macro for setting up a single conversion. If ADCCON1.STSEL = 11, using this +// macro will also start the conversion. +#define ADC_SINGLE_CONVERSION(settings) st( ADCCON3 = (settings); ) + +// Macro for setting up a single conversion +#define ADC_SEQUENCE_SETUP(settings) st( ADCCON2 = (settings); ) + +// Where _settings_ are the following: +// Reference voltage: +#define ADC_REF_1_25_V 0x00 // Internal 1.25V reference +#define ADC_REF_P0_7 0x40 // External reference on AIN7 pin +#define ADC_REF_AVDD 0x80 // AVDD_SOC pin +#define ADC_REF_P0_6_P0_7 0xC0 // External reference on AIN6-AIN7 differential input + +// Resolution (decimation rate): +#define ADC_7_BIT 0x00 // 64 decimation rate +#define ADC_9_BIT 0x10 // 128 decimation rate +#define ADC_10_BIT 0x20 // 256 decimation rate +#define ADC_12_BIT 0x30 // 512 decimation rate + +// Input channel: +#define ADC_AIN0 0x00 // single ended P0_0 +#define ADC_AIN1 0x01 // single ended P0_1 +#define ADC_AIN2 0x02 // single ended P0_2 +#define ADC_AIN3 0x03 // single ended P0_3 +#define ADC_AIN4 0x04 // single ended P0_4 +#define ADC_AIN5 0x05 // single ended P0_5 +#define ADC_AIN6 0x06 // single ended P0_6 +#define ADC_AIN7 0x07 // single ended P0_7 +#define ADC_GND 0x0C // Ground +#define ADC_TEMP_SENS 0x0E // on-chip temperature sensor +#define ADC_VDD_3 0x0F // (vdd/3) + +// Macro for starting the ADC in continuous conversion mode +#define ADC_SAMPLE_CONTINUOUS() \ +st( ADCCON1 &= ~0x30; \ +ADCCON1 |= 0x10; ) + +// Macro for stopping the ADC in continuous mode +#define ADC_STOP() st( ADCCON1 |= 0x30; ) + +// Macro for initiating a single sample in single-conversion mode (ADCCON1.STSEL = 11). +#define ADC_SAMPLE_SINGLE() \ +st( ADC_STOP(); \ +ADCCON1 |= 0x40; ) + +// Macro for configuring the ADC to be started from T1 channel 0. (T1 ch 0 must be in compare mode!!) +#define ADC_TRIGGER_FROM_TIMER1() \ +st( ADC_STOP(); \ +ADCCON1 &= ~0x10; ) + +// Expression indicating whether a conversion is finished or not. +#define ADC_SAMPLE_READY() st( ADCCON1 & 0x80; ) + +// Macro for setting/clearing a channel as input of the ADC +#define ADC_ENABLE_CHANNEL(ch) ADCCFG |= (0x01 << ch) +#define ADC_DISABLE_CHANNEL(ch) ADCCFG &= ~(0x01 << ch) + + +// Macros for configuring the ADC input: +// Example usage: +// IO_ADC_PORT0_PIN(0, IO_ADC_EN); +// IO_ADC_PORT0_PIN(4, IO_ADC_DIS); +// IO_ADC_PORT0_PIN(6, IO_ADC_EN); + +#define IO_ADC_PORT0_PIN(pin, adcEn) \ +st( if (adcEn) \ +ADCCFG |= BM( pin ); \ +else \ +ADCCFG &= ~BM( pin ) ) + +// where adcEn is one of: +#define IO_ADC_EN 1 // ADC input enabled +#define IO_ADC_DIS 0 // ADC input disab + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +unsigned int adcSampleSingle(uint8 reference, uint8 resolution, uint8 channel); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_cc8051.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_cc8051.h new file mode 100644 index 0000000..d59f7f6 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_cc8051.h @@ -0,0 +1,53 @@ +//------------------------------------------------------------------- +// Filename: hal_cc8051.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +//CONSTANTS +//------------------------------------------------------------------- +#define BIT0 0x01 +#define BIT1 0x02 +#define BIT2 0x04 +#define BIT3 0x08 +#define BIT4 0x10 +#define BIT5 0x20 +#define BIT6 0x40 +#define BIT7 0x80 + +//------------------------------------------------------------------- +//MACROS +//------------------------------------------------------------------- +#define MCU_IO_TRISTATE 1 // Used as "func" for the macros below +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +//----------------------------------------------------------------------------- +// Macros for simple configuration of IO pins on TI LPW SoCs +//----------------------------------------------------------------------------- +#define MCU_IO_PERIPHERAL(port, pin) MCU_IO_PERIPHERAL_PREP(port, pin) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_SET(port, pin, val) MCU_IO_SET_PREP(port, pin, val) +#define MCU_IO_SET_HIGH(port, pin) MCU_IO_SET_HIGH_PREP(port, pin) +#define MCU_IO_SET_LOW(port, pin) MCU_IO_SET_LOW_PREP(port, pin) +#define MCU_IO_TGL(port, pin) MCU_IO_TGL_PREP(port, pin) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_DIR_INPUT(port, pin) MCU_IO_DIR_INPUT_PREP(port, pin) +#define MCU_IO_DIR_OUTPUT(port, pin) MCU_IO_DIR_OUTPUT_PREP(port, pin) +#define MCU_PORT_SET_OUTPUT(port, val) MCU_PORT_SET_OUTPUT_PREP(port, val) +#define MCU_PORT_OUTPUT(port, val) MCU_PORT_OUTPUT_PREP(port, val) + +//---------------------------------------------------------------------------------- +// Macros for internal use (the macros above need a new round in the preprocessor) +//---------------------------------------------------------------------------------- +#define MCU_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BM(pin); ) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_SET_HIGH_PREP(port, pin) st( P##port##_##pin## = 1; ) +#define MCU_IO_SET_LOW_PREP(port, pin) st( P##port##_##pin## = 0; ) +#define MCU_IO_SET_PREP(port, pin, val) st( P##port##_##pin## = val; ) +#define MCU_IO_TGL_PREP(port, pin) st( P##port##_##pin## ^= 1; ) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) +#define MCU_IO_DIR_INPUT_PREP(port, pin) st( P##port##DIR &= ~BM(pin); ) +#define MCU_IO_DIR_OUTPUT_PREP(port, pin) st( P##port##DIR |= BM(pin); ) +#define MCU_PORT_SET_OUTPUT_PREP(port, val) st( P##port##SEL = 0x00; P##port## = val; P##port##DIR = 0xff; ) +#define MCU_PORT_OUTPUT_PREP(port, val) st( P##port## = val; ) diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.c new file mode 100644 index 0000000..fa4351a --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.c @@ -0,0 +1,75 @@ +//------------------------------------------------------------------- +// Filename: hal_clock.c +// Description: This file defines clock related functions for the CC253x family +// of RF system-on-chips from Texas Instruments. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_clock.h" + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn clockSetMainSrc +// @brief Function for setting the main system clock source. +// The function turns off the clock source that is not being used. +// TICKSPD is set to the same frequency as the source. +// @param uint8 source (one of CLOCK_SRC_HFRC or CLOCK_SRC_XOSC) +// @return void +//------------------------------------------------------------------- +void clockSetMainSrc(uint8 source) +{ + register uint8 osc32k_bm = CLKCONCMD &CLKCON_OSC32K_BM; + + // Source can have the following values: + // CLOCK_SRC_XOSC 0x00 High speed Crystal Oscillator (XOSC) + // CLOCK_SRC_HFRC 0x01 Low power RC Oscillator (HFRC) + + SLEEPCMD &= ~SLEEP_OSC_PD_BM; // power up both oscillators + while (!CC2530_IS_HFRC_STABLE() || ((SLEEPSTA &SLEEP_OSC_PD_BM) != 0)) + ; + // wait until the oscillator is stable + NOP(); + + if (source == CLOCK_SRC_HFRC) + { + CLKCONCMD = (osc32k_bm | CLKCON_OSC_BM | TICKSPD_DIV_2 | CLKCON_CLKSPD_BM); + } + else if (source == CLOCK_SRC_XOSC) + { + CLKCONCMD = (osc32k_bm | TICKSPD_DIV_1); + } + CC2530_WAIT_CLK_UPDATE(); + SLEEPCMD |= SLEEP_OSC_PD_BM; // power down the unused oscillator +} + +//------------------------------------------------------------------- +// @fn clockSelect32k +// @brief Function for selecting source for the 32kHz oscillator +// @param uint8 source (one of CLOCK_32K_XTAL or CLOCK_32K_RCOSC) +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 clockSelect32k(uint8 source) +{ + // System clock source must be high frequency RC oscillator before + // changing 32K source. + if (!(CLKCONSTA &CLKCON_OSC_BM)) + { + return FAILED; + } + + if (source == CLOCK_32K_XTAL) + { + CLKCONCMD &= ~CLKCON_OSC32K_BM; + } + else if (source == CLOCK_32K_RCOSC) + { + CLKCONCMD |= CLKCON_OSC32K_BM; + } + CC2530_WAIT_CLK_UPDATE(); + + return SUCCESS; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.h new file mode 100644 index 0000000..c664467 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_clock.h @@ -0,0 +1,66 @@ +//------------------------------------------------------------------- +// Filename: hal_clock.h +// Description: This file defines interface for clock related functions +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS +//------------------------------------------------------------------- +// SEE DATA SHEET FOR DETAILS ABOUT THE FOLLOWING BIT MASKS + +// Parameters for cc2530ClockSetMainSrc +#define CLOCK_SRC_XOSC 0 // High speed Crystal Oscillator Control +#define CLOCK_SRC_HFRC 1 // Low power RC Oscillator + +// Parameters for cc2530ClockSelect32k +#define CLOCK_32K_XTAL 0 // 32.768 Hz crystal oscillator +#define CLOCK_32K_RCOSC 1 // 32 kHz RC oscillator + +// Bit masks to check CLKCON register +#define CLKCON_OSC32K_BM 0x80 // bit mask, for the slow 32k clock oscillator +#define CLKCON_OSC_BM 0x40 // bit mask, for the system clock oscillator +#define CLKCON_TICKSPD_BM 0x38 // bit mask, for timer ticks output setting +#define CLKCON_CLKSPD_BM 0x01 // bit maks, for the clock speed + +#define TICKSPD_DIV_1 (0x00 << 3) +#define TICKSPD_DIV_2 (0x01 << 3) +#define TICKSPD_DIV_4 (0x02 << 3) +#define TICKSPD_DIV_8 (0x03 << 3) +#define TICKSPD_DIV_16 (0x04 << 3) +#define TICKSPD_DIV_32 (0x05 << 3) +#define TICKSPD_DIV_64 (0x06 << 3) +#define TICKSPD_DIV_128 (0x07 << 3) + +// Bit masks to check SLEEPSTA register +#define SLEEP_XOSC_STB_BM 0x40 // bit mask, check the stability of XOSC +#define SLEEP_HFRC_STB_BM 0x20 // bit maks, check the stability of the High-frequency RC oscillator +#define SLEEP_OSC_PD_BM 0x04 // bit mask, power down system clock oscillator(s) + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +// Macro for checking status of the high frequency RC oscillator. +#define CC2530_IS_HFRC_STABLE(x) (SLEEPSTA & SLEEP_HFRC_STB_BM) + +// Macro for checking status of the crystal oscillator +#define CC2530_IS_XOSC_STABLE(x) (SLEEPSTA & SLEEP_XOSC_STB_BM) + +// Macro for getting the clock division factor +#define CC2530_GET_CLKSPD(x) (CLKCONSTA & CLKCON_CLKSPD_BM) + +// Macro for getting the timer tick division factor. +#define CC2530_GET_TICKSPD(x) ((CLKCONSTA & CLKCON_TICKSPD_BM) >> 3) + +// Macro for setting the clock division factor, x value from 0b000 to 0b111 +#define CC2530_SET_TICKSPD(x) st( CLKCONCMD = ((((x) << 3) & 0x38) | (CLKCONCMD & 0xC7)); ) + +// Macro for setting the timer tick division factor, x value from 0b000 to 0b111 +#define CC2530_SET_CLKSPD(x) st( CLKCONCMD = (((x) & 0x07)| (CLKCONCMD & 0xF8)); ) + +// Macro for waiting for clock updates +#define CC2530_WAIT_CLK_UPDATE() st( uint8 ____clkcon; uint8 ____clkconsta; ____clkcon = CLKCONCMD; do { ____clkconsta = CLKCONSTA; } while (____clkconsta != ____clkcon); ) + +//------------------------------------------------------------------- +// FUNCTIONS +//------------------------------------------------------------------- +void clockSetMainSrc(uint8 source); +uint8 clockSelect32k(uint8 source); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_defs.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_defs.h new file mode 100644 index 0000000..84c14d7 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_defs.h @@ -0,0 +1,105 @@ +//------------------------------------------------------------------- +// Filename: hal_defs.h +// Description: HAL defines +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef signed char int8; +typedef unsigned char uint8; + +typedef signed short int16; +typedef unsigned short uint16; + +typedef signed long int32; +typedef unsigned long uint32; + +typedef void(*ISR_FUNC_PTR)(void); +typedef void(*VFPTR)(void); + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +#define TRUE 1 +#define FALSE 0 +#define NULL (void *)0 + +#define SUCCESS 0 +#define FAILED 1 + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define BV(n) (1 << (n)) + +#define BM(n) (1 << (n)) + +#define BF(x,b,s) (((x) & (b)) >> (s)) + +#define MIN(n,m) (((n) < (m)) ? (n) : (m)) + +#define MAX(n,m) (((n) < (m)) ? (m) : (n)) + +#define ABS(n) (((n) < 0) ? -(n) : (n)) + +// uint32 processing +#define BREAK_UINT32( var, ByteNum ) (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF)) +#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) ((uint32)((uint32)((Byte0) & 0x00FF) + ((uint32)((Byte1) & 0x00FF) << 8) + ((uint32)((Byte2) & 0x00FF) << 16) + ((uint32)((Byte3) & 0x00FF) << 24))) + +#define HI_UINT32(a) ((uint16) (((uint32)(a)) >> 16)) +#define LO_UINT32(a) ((uint16) ((uint32)(a))) + +// uint16 processing +#define BUILD_UINT16(loByte, hiByte) ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8))) + +#define HI_UINT16(a) (((uint16)(a) >> 8) & 0xFF) +#define LO_UINT16(a) ((uint16)(a) & 0xFF) + +// uint16 processing +#define BUILD_UINT8(hiByte, loByte) ((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4))) + +#define HI_UINT8(a) (((uint8)(a) >> 4) & 0x0F) +#define LO_UINT8(a) ((uint8)(a) & 0x0F) + +//------------------------------------------------------------------- +// This macro is for use by other macros to form a fully valid C statement. +// Without this, the if/else conditionals could show unexpected behavior. +// +// For example, use... +// #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; ) +// instead of ... +// #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; } +// or +// #define SET_REGS() ioreg1 = 0; ioreg2 = 0; +// The last macro would not behave as expected in the if/else construct. +// The second to last macro will cause a compiler error in certain uses +// of if/else construct +// +// It is not necessary, or recommended, to use this macro where there is +// already a valid C statement. For example, the following is redundant... +// #define CALL_FUNC() st( func(); ) +// This should simply be... +// #define CALL_FUNC() func() +// +// (The while condition below evaluates false without generating a +// constant-controlling-loop type of warning on most compilers.) +//------------------------------------------------------------------- +#define st(x) do { x } while (__LINE__ == -1) + +//------------------------------------------------------------------- +// Compiler abstraction +//------------------------------------------------------------------- +#define _PRAGMA(x) _Pragma(#x) + +#define FAR +#define NOP() asm("NOP") + +#define HAL_MCU_LITTLE_ENDIAN() __LITTLE_ENDIAN__ +#define HAL_ISR_FUNC_DECLARATION(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNC_PROTOTYPE(f,v) _PRAGMA(vector=v) __near_func __interrupt void f(void) +#define HAL_ISR_FUNCTION(f,v) HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v) diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.c new file mode 100644 index 0000000..4c5fd79 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.c @@ -0,0 +1,492 @@ +//------------------------------------------------------------------- +// Filename: hal_digio.c +// Description: HAL digital IO functionality +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_digio.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- + +static ISR_FUNC_PTR port0_isr_tbl[8] = +{ + 0 +}; +static ISR_FUNC_PTR port1_isr_tbl[8] = +{ + 0 +}; +static ISR_FUNC_PTR port2_isr_tbl[5] = +{ + 0 +}; + + +// Find a better place for this! +#define PICTL_PADSC_BM 0x80 +#define PICTL_P2IEN_BM 0x40 +#define PICTL_P0IENH_BM 0x20 +#define PICTL_P0IENL_BM 0x10 +#define PICTL_P2ICON_BM 0x08 +#define PICTL_P1ICONH_BM 0x04 +#define PICTL_P1ICONL_BM 0x02 +#define PICTL_P1ICON_BM 0x06 // Combined - for compatibility +#define PICTL_P0ICON_BM 0x01 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halDigioConfig +// @brief Configure the pin as specified by p. +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- + +//comment: find better way of doing this. SFR registers can not be accessed by pointers, therefore code is different from MSP430 code +// comment: currently support only P0 and P1 +uint8 halDigioConfig(const digioConfig *p) +{ + // register volatile uint8* dir; + register const uint8 bitmask = p->pin_bm; + + // Sanity check + if ((bitmask == 0) || (bitmask != (uint8)BV(p->pin))) + { + return (HAL_DIGIO_ERROR); + } + + switch (p->port) + { + case 0: + P0SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P0 |= bitmask; + } + else + { + P0 &= ~bitmask; + } + P0DIR |= bitmask; + } + else + // input + { + P0DIR &= ~bitmask; + } + break; + case 1: + P1SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P1 |= bitmask; + } + else + { + P1 &= ~bitmask; + } + P1DIR |= bitmask; + } + else + // input + { + P1DIR &= ~bitmask; + } + break; + case 2: + P2SEL &= ~bitmask; + if (p->dir == HAL_DIGIO_OUTPUT) + { + if (p->initval == 1) + { + P2 |= bitmask; + } + else + { + P2 &= ~bitmask; + } + P2DIR |= bitmask; + } + else + // input + { + P2DIR &= ~bitmask; + } + break; + //case 1: P1SEL &= ~bitmask; out = &P1OUT; dir = &P1DIR; break; + //case 2: P2SEL &= ~bitmask; out = &P2OUT; dir = &P2DIR; break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + + +//------------------------------------------------------------------- +// @fn halDigioSet +// @brief Set output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioSet(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 |= p->pin_bm; + break; + case 1: + P1 |= p->pin_bm; + break; + case 2: + P2 |= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + + +//------------------------------------------------------------------- +// @fn halDigioClear +// @brief Clear output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioClear(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 &= ~p->pin_bm; + break; + case 1: + P1 &= ~p->pin_bm; + break; + case 2: + P2 &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioToggle +// @brief Toggle output pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioToggle(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_OUTPUT) + { + switch (p->port) + { + case 0: + P0 ^= p->pin_bm; + break; + case 1: + P1 ^= p->pin_bm; + break; + case 2: + P2 ^= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioGet +// @brief Get value on input pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioGet(const digioConfig *p) +{ + if (p->dir == HAL_DIGIO_INPUT) + { + switch (p->port) + { + case 0: + return (P0 &p->pin_bm ? 1 : 0); + case 1: + return (P1 &p->pin_bm ? 1 : 0); + case 2: + return (P2 &p->pin_bm ? 1 : 0); + default: + break; + } + } + return (HAL_DIGIO_ERROR); +} + +//------------------------------------------------------------------- +// @fn halDigioIntConnect +// @brief Connect function to IO interrupt +// @param digioConfig* p - pointer to configuration structure for IO pin +// ISR_FUNC_PTR func - pointer to function +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntConnect(const digioConfig *p, ISR_FUNC_PTR func) +{ + istate_t key; + HAL_INT_LOCK(key); + switch (p->port) + { + case 0: + port0_isr_tbl[p->pin] = func; + break; + case 1: + port1_isr_tbl[p->pin] = func; + break; + case 2: + port2_isr_tbl[p->pin] = func; + break; + default: + HAL_INT_UNLOCK(key); + return (HAL_DIGIO_ERROR); + } + halDigioIntClear(p); + HAL_INT_UNLOCK(key); + return (HAL_DIGIO_OK); +} + + +//------------------------------------------------------------------- +// @fn halDigioIntEnable +// @brief Enable interrupt on IO pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntEnable(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IE = 1; // set P0IE bit + P0IEN |= p->pin_bm; + break; + case 1: + IEN2 |= 0x10; // set P1IE bit + P1IEN |= p->pin_bm; + break; + case 2: + IEN2 |= 0x02; // set P2IE bit + P2IEN |= p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntDisable +// @brief Disable interrupt on IO pin +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntDisable(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IEN &= ~p->pin_bm; + break; + case 1: + P1IEN &= ~p->pin_bm; + break; + case 2: + P2IEN &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntClear +// @brief Clear interrupt flag +// @param digioConfig* p - pointer to configuration structure for IO pin +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +uint8 halDigioIntClear(const digioConfig *p) +{ + switch (p->port) + { + case 0: + P0IFG &= ~p->pin_bm; + break; + case 1: + P1IFG &= ~p->pin_bm; + break; + case 2: + P2IFG &= ~p->pin_bm; + break; + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn halDigioIntSetEdge +// @brief Set edge for IO interrupt +// @param digioConfig* p - pointer to configuration structure for IO pin +// edge - HAL_DIGIO_INT_FALLING_EDGE or HAL_DIGIO_INT_RISING_EDGE +// @return uint8 - HAL_DIGIO_ERROR or HAL_DIGIO_OK +//------------------------------------------------------------------- +// Comment: all pins on port are configured at same time +uint8 halDigioIntSetEdge(const digioConfig *p, uint8 edge) +{ + switch (edge) + { + case HAL_DIGIO_INT_FALLING_EDGE: + switch (p->port) + { + case 0: + PICTL |= PICTL_P0ICON_BM; // set P0ICON high + break; + case 1: + PICTL |= PICTL_P1ICON_BM; // set P1ICON high + break; + case 2: + PICTL |= PICTL_P2ICON_BM; // set P2ICON high + break; + default: + return (HAL_DIGIO_ERROR); + } + break; + + case HAL_DIGIO_INT_RISING_EDGE: + switch (p->port) + { + case 0: + PICTL &= ~PICTL_P0ICON_BM; // set P0ICON low + break; + case 1: + PICTL &= ~PICTL_P1ICON_BM; // set P0ICON low + break; + case 2: + PICTL &= ~PICTL_P2ICON_BM; // set P0ICON low + break; + default: + return (HAL_DIGIO_ERROR); + } + break; + + default: + return (HAL_DIGIO_ERROR); + } + return (HAL_DIGIO_OK); +} + +//------------------------------------------------------------------- +// @fn port0_ISR +// @brief ISR framework for P0 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port0_ISR, P0INT_VECTOR) +{ + register uint8 i; + P0IF = 0; + if (P0IFG) + { + for (i = 0; i < 8; i++) + { + register const uint8 pinmask = 1 << i; + if (P0IFG &pinmask) + { + if (port0_isr_tbl[i] != 0) + { + (*port0_isr_tbl[i])(); + } + P0IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} + +//------------------------------------------------------------------- +// @fn port1_ISR +// @brief ISR framework for P0 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port1_ISR, P1INT_VECTOR) +{ + register uint8 i; + P1IF = 0; + if (P1IFG) + { + for (i = 0; i < 8; i++) + { + register const uint8 pinmask = 1 << i; + if (P1IFG &pinmask) + { + if (port1_isr_tbl[i] != 0) + { + (*port1_isr_tbl[i])(); + } + P1IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} + +//------------------------------------------------------------------- +// @fn port2_ISR +// @brief ISR framework for P2 digio interrupt +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(port2_ISR, P2INT_VECTOR) +{ + register uint8 i; + P2IF = 0; + if (P2IFG) + { + for (i = 0; i < 5; i++) + { + register const uint8 pinmask = 1 << i; + if (P2IFG &pinmask) + { + if (port2_isr_tbl[i] != 0) + { + (*port2_isr_tbl[i])(); + } + P2IFG &= ~pinmask; + } + } + //__low_power_mode_off_on_exit(); + } +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.h new file mode 100644 index 0000000..e851f23 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_digio.h @@ -0,0 +1,46 @@ +//------------------------------------------------------------------- +// Filename: hal_digio.h +// Description: HAL digital IO functionality +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +enum +{ + HAL_DIGIO_INPUT, HAL_DIGIO_OUTPUT +}; + +enum +{ + HAL_DIGIO_INT_FALLING_EDGE, HAL_DIGIO_INT_RISING_EDGE +}; + +#define HAL_DIGIO_OK 0 +#define HAL_DIGIO_ERROR (~0) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + uint8 port; // port number + uint8 pin; // pin number + uint8 pin_bm; // pin bitmask + uint8 dir; // direction (input or output) + uint8 initval; // initial value +} digioConfig; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +uint8 halDigioConfig(const digioConfig *p); +uint8 halDigioClear(const digioConfig *p); +uint8 halDigioToggle(const digioConfig *p); +uint8 halDigioSet(const digioConfig *p); +uint8 halDigioGet(const digioConfig *p); + +uint8 halDigioIntConnect(const digioConfig *p, ISR_FUNC_PTR func); +uint8 halDigioIntEnable(const digioConfig *p); +uint8 halDigioIntDisable(const digioConfig *p); +uint8 halDigioIntClear(const digioConfig *p); +uint8 halDigioIntSetEdge(const digioConfig *p, uint8 edge); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.c new file mode 100644 index 0000000..29e52ce --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: hal_int.c +// Description: HAL interrupt control +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halIntOn +// @brief Enable global interrupts. +// @param none +// @return none +//------------------------------------------------------------------- +void halIntOn(void) +{ + HAL_INT_ON(); +} + +//------------------------------------------------------------------- +// @fn halIntOff +// @brief Turns global interrupts off. +// @param none +// @return none +//------------------------------------------------------------------- +void halIntOff(void) +{ + HAL_INT_OFF(); +} + +//------------------------------------------------------------------- +// @fn halIntLock +// @brief Turns global interrupts off and returns current interrupt state. +// Should always be used together with halIntUnlock(). +// @param none +// @return uint16 - current interrupt state +//------------------------------------------------------------------- +uint16 halIntLock(void) +{ + istate_t key; + HAL_INT_LOCK(key); + return (key); +} + +//------------------------------------------------------------------- +// @fn halIntUnlock +// @brief Set interrupt state back to the state it had before calling halIntLock(). +// Should always be used together with halIntLock(). +// @param key +// @return none +//------------------------------------------------------------------- +void halIntUnlock(uint16 key) +{ + HAL_INT_UNLOCK(key); +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.h new file mode 100644 index 0000000..648e6d3 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_int.h @@ -0,0 +1,21 @@ +//------------------------------------------------------------------- +// Filename: hal_int.h +// Description: HAL interrupt control header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_INT_ON(x) st( EA = 1; ) +#define HAL_INT_OFF(x) st( EA = 0; ) +#define HAL_INT_LOCK(x) st( (x) = EA; EA = 0; ) +#define HAL_INT_UNLOCK(x) st( EA = (x); ) + +typedef unsigned short istate_t; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halIntOn(void); +void halIntOff(void); +uint16 halIntLock(void); +void halIntUnlock(uint16 key); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.c new file mode 100644 index 0000000..5054eb1 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.c @@ -0,0 +1,116 @@ +//------------------------------------------------------------------- +// Filename: hal_mcu.c +// Description: +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_clock.h" +#include "hal_mcu.h" +#include "hal_board.h" + +//------------------------------------------------------------------- +// @fn halMcuInit +// @brief Set Main Clock source to XOSC +// @param none +// @return none +//------------------------------------------------------------------- +void halMcuInit(void) +{ + clockSetMainSrc(CLOCK_SRC_XOSC); +} + +//------------------------------------------------------------------- +// @fn halMcuWaitUs +// @brief Busy wait function. Waits the specified number of microseconds. Use +// assumptions about number of clock cycles needed for the various +// instructions. This function assumes a 32 MHz clock. +// NB! This function is highly dependent on architecture and compiler! +// @param uint16 usec - number of microseconds delays +// @return none +//------------------------------------------------------------------- +#pragma optimize=none +void halMcuWaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} + +//------------------------------------------------------------------- +// @fn halMcuWaitMs +// @brief Busy wait function. Waits the specified number of milliseconds. Use +// assumptions about number of clock cycles needed for the various +// instructions. +// NB! This function is highly dependent on architecture and compiler! +// @param uint16 millisec - number of milliseconds delay +// @return none +//------------------------------------------------------------------- +#pragma optimize=none +void halMcuWaitMs(uint16 msec) +{ + while (msec--) + { + halMcuWaitUs(1000); + } +} + +//------------------------------------------------------------------- +// @fn halMcuSetLowPowerMode +// @brief Sets the MCU in a low power mode. Will turn global interrupts on at +// the same time as entering the LPM mode. The MCU must be waken from +// an interrupt (status register on stack must be modified). +// NB! This function is highly dependent on architecture and compiler! +// @param uint8 mode - power mode +// @return none +//------------------------------------------------------------------- +void halMcuSetLowPowerMode(uint8 mode) +{ + char buf[] = "LowPowerMode( )"; + buf[13] = mode + '0'; + // comment: not yet implemented + HAL_ASSERT(buf); +} + + +//------------------------------------------------------------------- +// @fn halMcuReset +// @brief +// Resets the MCU. This utilize the watchdog timer as there is no other way +// for a software reset. The reset will not occur until ~2 ms. +// NB: The function will not return! (hangs until reset) +// Parameters: +// @param void +// @return void +//------------------------------------------------------------------- +void halMcuReset(void) +{ + const uint8 WDT_INTERVAL_MSEC_2 = 0x03; // after ~2 ms + + WDCTL = ((WDCTL &0xFC) | (WDT_INTERVAL_MSEC_2 &0x03)); + // Start watchdog + WDCTL &= ~0x04; // Select watchdog mode + WDCTL |= 0x08; // Enable timer + while (1) + ; + // Halt here until reset +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.h new file mode 100644 index 0000000..6ecb965 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_mcu.h @@ -0,0 +1,17 @@ +//------------------------------------------------------------------- +// Filename: hal_mcu.h +// Description: hal mcu library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halMcuInit(void); +void halMcuWaitUs(uint16 usec); +void halMcuWaitMs(uint16 msec); +void halMcuSetLowPowerMode(uint8 mode); +void halMcuReset(void); +uint8 halMcuGetResetCause(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.c new file mode 100644 index 0000000..d46019d --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.c @@ -0,0 +1,418 @@ +//------------------------------------------------------------------- +// Filename: hal_rf.c +// Description: CC2530 radio interface. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_int.h" +#include "util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// CC2530 RSSI Offset +#define RSSI_OFFSET 73 + +// Various radio settings +#define AUTO_ACK 0x20 +#define AUTO_CRC 0x40 + +// TXPOWER values +#define CC2530_TXPOWER_MIN_3_DBM 0xB5 +#define CC2530_TXPOWER_0_DBM 0xD5 +#define CC2530_TXPOWER_4_DBM 0xF5 + +#define HAL_RF_TXPOWER_MIN_3_DBM 0 +#define HAL_RF_TXPOWER_0_DBM 1 +#define HAL_RF_TXPOWER_4_DBM 2 + +// RF interrupt flags +#define IRQ_TXDONE 0x02 +#define IRQ_RXPKTDONE 0x40 + +// Selected strobes +#define ISRXON() st(RFST = 0xE3;) +#define ISTXON() st(RFST = 0xE9;) +#define ISTXONCCA() st(RFST = 0xEA;) +#define ISRFOFF() st(RFST = 0xEF;) +#define ISFLUSHRX() st(RFST = 0xEC;) +#define ISFLUSHTX() st(RFST = 0xEE;) + + + +//------------------------------------------------------------------- +// GLOBAL DATA +//------------------------------------------------------------------- + +static const menuItem_t pPowerSettings[] = +{ + "-3dBm", HAL_RF_TXPOWER_MIN_3_DBM, "0dBm", HAL_RF_TXPOWER_0_DBM, "4dBm", HAL_RF_TXPOWER_4_DBM +}; + +const menu_t powerMenu = +{ + pPowerSettings, N_ITEMS(pPowerSettings) +}; + +//------------------------------------------------------------------- +// LOCAL DATA +//------------------------------------------------------------------- + +static ISR_FUNC_PTR pfISR = NULL; + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halRfInit +// @brief Power up, sets default tuning settings, enables autoack, enables random generator. +// @param none +// @return SUCCESS always (for interface compatibility) +//------------------------------------------------------------------- +uint8 halRfInit(void) +{ + // Enable auto ack and auto crc + FRMCTRL0 |= (AUTO_ACK | AUTO_CRC); + + // Write non-default register values + // Tuning settings + TXPOWER = CC2530_TXPOWER_0_DBM; // TX output power ~0 dBm + + // Recommended RX settings + TXFILTCFG = 0x09; + AGCCTRL1 = 0x15; + FSCAL1 = 0x00; + + // Enable random generator -> Not implemented yet + + // Enable CC2591 with High Gain Mode -> Not implemented yet + + // Enable RX interrupt + halRfEnableRxInterrupt(); + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn halRfGetChipId +// @brief Get chip id +// @param none +// @return uint8 - result +//------------------------------------------------------------------- +uint8 halRfGetChipId(void) +{ + return CHIPID; +} + +//------------------------------------------------------------------- +// @fn halRfGetChipVer +// @brief Get chip version +// @param none +// @return uint8 - result +//------------------------------------------------------------------- +uint8 halRfGetChipVer(void) +{ + // return major revision (4 upper bits) + return CHVER >> 4; +} + +//------------------------------------------------------------------- +// @fn halRfGetRssiOffset +// @brief Return RSSI Offset +// @param none +// @return uint8 - RSSI offset +//------------------------------------------------------------------- +uint8 halRfGetRssiOffset(void) +{ + return RSSI_OFFSET; +} + +//------------------------------------------------------------------- +// @fn halRfSetChannel +// @brief Set RF channel in the 2.4GHz band. The Channel must be in the range 11-26, +// 11= 2005 MHz, channel spacing 5 MHz. +// @param channel - logical channel number +// @return none +//------------------------------------------------------------------- +void halRfSetChannel(uint8 channel) +{ + FREQCTRL = (MIN_CHANNEL + (channel - MIN_CHANNEL) *CHANNEL_SPACING); +} + +//------------------------------------------------------------------- +// @fn halRfSetShortAddr +// @brief Write short address to chip +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSetShortAddr(uint16 shortAddr) +{ + SHORT_ADDR0 = LO_UINT16(shortAddr); + SHORT_ADDR1 = HI_UINT16(shortAddr); +} + +//------------------------------------------------------------------- +// @fn halRfSetPanId +// @brief Write PAN Id to chip +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSetPanId(uint16 panId) +{ + PAN_ID0 = LO_UINT16(panId); + PAN_ID1 = HI_UINT16(panId); +} + + +//------------------------------------------------------------------- +// @fn halRfSetPower +// @brief Set TX output power +// @param uint8 power - power level: TXPOWER_MIN_4_DBM, TXPOWER_0_DBM, TXPOWER_4_DBM +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfSetTxPower(uint8 power) +{ + uint8 n; + + switch (power) + { + case HAL_RF_TXPOWER_MIN_3_DBM: + n = CC2530_TXPOWER_MIN_3_DBM; + break; + case HAL_RF_TXPOWER_0_DBM: + n = CC2530_TXPOWER_0_DBM; + break; + case HAL_RF_TXPOWER_4_DBM: + n = CC2530_TXPOWER_4_DBM; + break; + + default: + return FAILED; + } + + // Set TX power + TXPOWER = n; + + return SUCCESS; +} + +//------------------------------------------------------------------- +// @fn halRfWriteTxBuf +// @brief Write to TX buffer +// @param uint8* pData - buffer to write +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfWriteTxBuf(uint8 *pData, uint8 length) +{ + uint8 i; + + ISFLUSHTX(); // Making sure that the TX FIFO is empty. + + RFIRQF1 = ~IRQ_TXDONE; // Clear TX done interrupt + + // Insert data + for (i = 0; i < length; i++) + { + RFD = pData[i]; + } +} + + +//------------------------------------------------------------------- +// @fn halRfAppendTxBuf +// @brief Write to TX buffer +// @param uint8* pData - buffer to write +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfAppendTxBuf(uint8 *pData, uint8 length) +{ + uint8 i; + + // Insert data + for (i = 0; i < length; i++) + { + RFD = pData[i]; + } +} + +//------------------------------------------------------------------- +// @fn halRfReadRxBuf +// @brief Read RX buffer +// @param uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return none +//------------------------------------------------------------------- +void halRfReadRxBuf(uint8 *pData, uint8 length) +{ + // Read data + while (length > 0) + { + *pData++ = RFD; + length--; + } +} + + +//------------------------------------------------------------------- +// @fn halRfReadMemory +// @brief Read RF device memory +// @param uint16 addr - memory address +// uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return Number of bytes read +//------------------------------------------------------------------- +uint8 halRfReadMemory(uint16 addr, uint8 *pData, uint8 length) +{ + return 0; +} + + +//------------------------------------------------------------------- +// @fn halRfWriteMemory +// @brief Write RF device memory +// @param uint16 addr - memory address +// uint8* pData - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// @return Number of bytes written +//------------------------------------------------------------------- +uint8 halRfWriteMemory(uint16 addr, uint8 *pData, uint8 length) +{ + return 0; +} + +//------------------------------------------------------------------- +// @fn halRfTransmit +// @brief Transmit frame with Clear Channel Assessment. +// @param none +// @return uint8 - SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfTransmit(void) +{ + uint8 status; + + ISTXON(); // Sending + + // Waiting for transmission to finish + while (!(RFIRQF1 &IRQ_TXDONE)) + ; + + RFIRQF1 = ~IRQ_TXDONE; + status = SUCCESS; + + return status; +} + +//------------------------------------------------------------------- +// @fn halRfReceiveOn +// @brief Turn receiver on +// @param none +// @return none +//------------------------------------------------------------------- +void halRfReceiveOn(void) +{ + ISFLUSHRX(); // Making sure that the TX FIFO is empty. + ISRXON(); +} + +//------------------------------------------------------------------- +// @fn halRfReceiveOff +// @brief Turn receiver off +// @param none +// @return none +//------------------------------------------------------------------- +void halRfReceiveOff(void) +{ + ISRFOFF(); + ISFLUSHRX(); // Making sure that the TX FIFO is empty. +} + + +//------------------------------------------------------------------- +// @fn halRfDisableRxInterrupt +// @brief Clear and disable RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfDisableRxInterrupt(void) +{ + // disable RXPKTDONE interrupt + RFIRQM0 &= ~BV(6); + // disable general RF interrupts + IEN2 &= ~BV(0); +} + + +//------------------------------------------------------------------- +// @fn halRfEnableRxInterrupt +// @brief Enable RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfEnableRxInterrupt(void) +{ + // enable RXPKTDONE interrupt + RFIRQM0 |= BV(6); + // enable general RF interrupts + IEN2 |= BV(0); +} + +//------------------------------------------------------------------- +// @fn halRfRxInterruptConfig +// @brief Configure RX interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfRxInterruptConfig(ISR_FUNC_PTR pf) +{ + uint8 x; + HAL_INT_LOCK(x); + pfISR = pf; + HAL_INT_UNLOCK(x); +} + + +//------------------------------------------------------------------- +// @fn halRfWaitTransceiverReady +// @brief Wait until the transciever is ready (SFD inactive). +// @param none +// @return none +//------------------------------------------------------------------- +void halRfWaitTransceiverReady(void) +{ + // Wait for SFD not active and TX_Active not active + while (FSMSTAT1 &(BV(1) | BV(5))) + ; +} + + +//------------------------------------------------------------------- +// @fn rfIsr +// @brief Interrupt service routine that handles RFPKTDONE interrupt. +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(rfIsr, RF_VECTOR) +{ + uint8 x; + + HAL_INT_LOCK(x); + + if (RFIRQF0 &IRQ_RXPKTDONE) + { + if (pfISR) + { + (*pfISR)(); // Execute the custom ISR + } + S1CON = 0; // Clear general RF interrupt flag + RFIRQF0 &= ~IRQ_RXPKTDONE; // Clear RXPKTDONE interrupt + } + HAL_INT_UNLOCK(x); +} + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.h new file mode 100644 index 0000000..845b908 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------- +// Filename: hal_rf.h +// Description: HAL radio interface header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Chip ID's +#define HAL_RF_CHIP_ID_CC1100 0x00 +#define HAL_RF_CHIP_ID_CC1110 0x01 +#define HAL_RF_CHIP_ID_CC1111 0x11 +#define HAL_RF_CHIP_ID_CC2420 0x02 +#define HAL_RF_CHIP_ID_CC2500 0x80 +#define HAL_RF_CHIP_ID_CC2510 0x81 +#define HAL_RF_CHIP_ID_CC2511 0x91 +#define HAL_RF_CHIP_ID_CC2550 0x82 +#define HAL_RF_CHIP_ID_CC2520 0x84 +#define HAL_RF_CHIP_ID_CC2430 0x85 +#define HAL_RF_CHIP_ID_CC2431 0x89 +#define HAL_RF_CHIP_ID_CC2530 0xA5 +#define HAL_RF_CHIP_ID_CC2531 0xB5 +#define HAL_RF_CHIP_ID_CC2540 0x8D + +// IEEE 802.15.4 defined constants (2.4 GHz logical channels) +#define MIN_CHANNEL 11 // 2405 MHz +#define MAX_CHANNEL 26 // 2480 MHz +#define CHANNEL_SPACING 5 // MHz + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +// Generic RF interface +uint8 halRfInit(void); +uint8 halRfSetTxPower(uint8 power); +uint8 halRfTransmit(void); + +uint8 halRfGetChipId(void); +uint8 halRfGetChipVer(void); +uint8 halRfGetRssiOffset(void); + +void halRfWriteTxBuf(uint8 *pData, uint8 length); +void halRfReadRxBuf(uint8 *pData, uint8 length); +void halRfWaitTransceiverReady(void); +uint8 halRfReadMemory(uint16 addr, uint8 *pData, uint8 length); +uint8 halRfWriteMemory(uint16 addr, uint8 *pData, uint8 length); + +void halRfReceiveOn(void); +void halRfReceiveOff(void); +void halRfDisableRxInterrupt(void); +void halRfEnableRxInterrupt(void); +void halRfRxInterruptConfig(ISR_FUNC_PTR pfISR); + +// IEEE 802.15.4 specific interface +void halRfSetChannel(uint8 channel); +void halRfSetShortAddr(uint16 shortAddr); +void halRfSetPanId(uint16 PanId); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.c new file mode 100644 index 0000000..480368e --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.c @@ -0,0 +1,393 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_security.c +// Description: CC2530 CCM security +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_rf.h" +#include "hal_rf_security.h" +#include "util.h" +#include + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Staring AES operation +#define AES_START() st (ENCCS |= 0x01;) + +// Setting the mode of the AES operation +#define AES_SET_MODE(mode) st( ENCCS &= ~0x70; ENCCS |= mode; ) + +// Starting or stopping encryption or decryption +#define AES_SET_OPERATION(op) st( ENCCS = (ENCCS & ~0x07) | op; ) + +// Poll the AES ready bit +#define AES_RDY() ( ENCCS & 8) + +// Encryption mode +#define AES_MODE_CBC 0x00 +#define AES_MODE_CFB 0x10 +#define AES_MODE_OFB 0x20 +#define AES_MODE_CTR 0x30 +#define AES_MODE_ECB 0x40 +#define AES_MODE_CBCMAC 0x50 + +// Operations +#define AES_ENCRYPT 0x00 +#define AES_DECRYPT 0x02 +#define AES_LOAD_KEY 0x04 +#define AES_LOAD_IV 0x06 + +// Convert MIC to MIC length +#define MIC_2_MICLEN(m) ( BV((m&3)+1) & ~3 ) + +//------------------------------------------------------------------- +// LOCAL DATA +//------------------------------------------------------------------- +static uint8 nonceTx[16]; +static uint8 nonceRx[16]; +static uint8 IV[16]; +static uint8 cipherText[128]; +static uint8 buf[128]; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector); +static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector); +static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm); + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +extern void halRfAppendTxBuf(uint8 *pData, uint8 length); + +//------------------------------------------------------------------- +// @fn halRfSecurityInit +// @brief Security init. Write nonces and key to chip. +// @param none +// @return none +//------------------------------------------------------------------- +void halRfSecurityInit(uint8 *pKey, uint8 *pNonceRx, uint8 *pNonceTx) +{ + uint8 i; + + AES_SET_OPERATION(AES_LOAD_KEY); + + // Starting loading of key or vector. + AES_START(); + + // Store key and nonces + for (i = 0; i < NONCE_LENGTH; i++) + { + nonceRx[i] = *pNonceRx; + nonceTx[i] = *pNonceTx; + pNonceTx++; + pNonceRx++; + ENCDI = *pKey++; + } +} + +//------------------------------------------------------------------- +// @fn halRfWriteTxBufSecure +// @brief Encrypt and authenticate plaintext then fill TX buffer +// @param uint8* data - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// uint8 c - number of bytes to encrypt +// uint8 f - number of bytes to authenticate +// uint8 m - integrity code (m=1,2,3 gives lenght of integrity +// field l(m)= 4,8,16) +// @return none +//------------------------------------------------------------------- +void halRfWriteTxBufSecure(uint8 *pPkt, uint8 length, uint8 c, uint8 f, uint8 m) +{ + uint8 i, lm; + + // Generate B(i) vector + memset(buf, 0, sizeof(buf)); + lm = MIC_2_MICLEN(m); + i = generateAuthData(c, nonceTx, pPkt + 1, f, lm); + + // Calculate CBC-MAC value + memset(IV, 0, sizeof(IV)); + halAesEncrypt(AES_MODE_CBCMAC, buf, i, cipherText, IV); + + // Encrypt authentication tag + memcpy(IV, nonceTx, 16); // Use nonce as IV + IV[0] = 0x01; // Flag for CTR + IV[15] = 0; // Counter= 0 + + memcpy(buf, cipherText, 16); + halAesEncrypt(AES_MODE_CTR, buf, 16, cipherText, IV); + + // Encrypt plaintext + memset(buf + i, 0, 16-(i &0xf)); // Zero padding + IV[15] = 1; // Counter= 1 + halAesEncrypt(AES_MODE_CTR, &buf[i - c], c, &cipherText[i - c], IV); + + // Fill TX buffer + halRfWriteTxBuf(pPkt, 1); // Length byte + halRfAppendTxBuf(pPkt + 1, f); // MPDU + halRfAppendTxBuf(&cipherText[i - c], c); // Payload + halRfAppendTxBuf(cipherText, lm); // MIC +} + +//------------------------------------------------------------------- +// @fn halRfReadRxBufSecure +// @brief Decrypts and reverse authenticates with CCM then reads out received frame +// @param uint8* pPkt - data buffer. This must be allocated by caller. +// uint8 length - number of bytes +// uint8 encrLength - number of bytes to decrypt +// uint8 authLength - number of bytes to reverse authenticate +// uuint8 m - ets length of integrity code (m=1,2,3 gives lenght of integrity field 4,8,16) +// @return SUCCESS or FAILED +//------------------------------------------------------------------- +uint8 halRfReadRxBufSecure(uint8 *data, uint8 length, uint8 c, uint8 f, uint8 m) +{ + uint8 lm, l, i; + + // Get received packet + halRfReadRxBuf(data, length); + + // Update RX nonce; frame counter + nonceRx[12] = data[10]; + nonceRx[11] = data[11]; + nonceRx[10] = data[12]; + nonceRx[9] = data[13]; + // Update RX nonce; source address + nonceRx[7] = data[8]; + nonceRx[8] = data[7]; + + // Decrypt authentication tag + memcpy(IV, nonceRx, 16); + IV[15] = 0; // Counter= 0 + IV[0] = 0x01; // Flag field for decryption + lm = MIC_2_MICLEN(m); // Length of authentication tag + + halAesDecrypt(AES_MODE_CTR, data + c + f, lm, buf, IV); + memcpy(data + c + f, buf, lm); + + // Prepare payload for decryption + memcpy(cipherText, data + f, c); + + // Zero padding + l = (c &0x0f) == 0 ? c : (c &0xf0) + 0x10; + for (i = c; i < l; i++) + { + cipherText[i] = 0x00; + } + + // Decrypt payload + IV[15] = 1; // Counter= 1 + halAesDecrypt(AES_MODE_CTR, cipherText, c, buf, IV); + memcpy(data + f, buf, c); + + // Generate B(i) vector + memset(buf, 0, sizeof(buf)); + i = generateAuthData(c, nonceRx, data, f, lm); + + // Calculate CBC-MAC value + memset(IV, 0, sizeof(IV)); + halAesEncrypt(AES_MODE_CBCMAC, buf, i, cipherText, IV); + + // Check authentication data + return memcmp(cipherText, data + c + f, lm) == 0 ? SUCCESS : FAILED; +} + +//------------------------------------------------------------------- +// @fn halRfIncNonceTx +// @brief Increments frame counter field of stored nonce TX +// @param none +// @return none +//------------------------------------------------------------------- +void halRfIncNonceTx(void) +{ + uint32 *pCount; + + pCount = (uint32*) &nonceTx[9]; + utilReverseBuf((uint8*)pCount, 4); + (*pCount)++; + utilReverseBuf((uint8*)pCount, 4); +} + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS + +//------------------------------------------------------------------- +static void halAesLoadBlock(uint8 *pData, uint8 op) +{ + uint8 i; + + // Set operation + AES_SET_OPERATION(op); + // Starting loading of key or vector. + AES_START(); + + // loading the data (key or vector) + for (i = 0; i < 16; i++) + { + ENCDI = pData[i]; + } + + return ; +} + +//------------------------------------------------------------------- +static void halAesOperation(uint8 oper, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + uint16 i; + uint8 j, k; + uint8 mode; + uint16 nbrOfBlocks; + uint16 convertedBlock; + + nbrOfBlocks = length / 0x10; + + if ((length % 0x10) != 0) + { + // length not multiplum of 16, convert one block extra with zeropadding + nbrOfBlocks++; + } + + // Loading the IV. + halAesLoadBlock(pInitVector, AES_LOAD_IV); + + // Start the operation + AES_SET_OPERATION(oper); + + // Getting the operation mode. + mode = ENCCS &0x70; + + for (convertedBlock = 0; convertedBlock < nbrOfBlocks; convertedBlock++) + { + // Starting the conversion. + AES_START(); + + i = convertedBlock * 16; + // Counter, Output Feedback and Cipher Feedback operates on 4 bytes and not 16 bytes. + if ((mode == AES_MODE_CFB) || (mode == AES_MODE_OFB) || (mode == AES_MODE_CTR)) + { + + for (j = 0; j < 4; j++) + { + // Writing the input data with zero-padding + for (k = 0; k < 4; k++) + { + ENCDI = ((i + 4 * j + k < length) ? pDataIn[i + 4 * j + k]: 0x00); + } + + // Read out data for every 4th byte + for (k = 0; k < 4; k++) + { + pDataOut[i + 4 * j + k] = ENCDO; + } + + } + } + else if (mode == AES_MODE_CBCMAC) + { + // Writing the input data with zero-padding + for (j = 0; j < 16; j++) + { + ENCDI = ((i + j < length) ? pDataIn[i + j]: 0x00); + } + // The last block of the CBC-MAC is computed by using CBC mode. + if (convertedBlock == nbrOfBlocks - 2) + { + AES_SET_MODE(AES_MODE_CBC); + // wait for data ready + halMcuWaitUs(1); + } + // The CBC-MAC does not produce an output on the n-1 first blocks + // only the last block is read out + else if (convertedBlock == nbrOfBlocks - 1) + { + + // wait for data ready + halMcuWaitUs(1); + for (j = 0; j < 16; j++) + { + pDataOut[j] = ENCDO; + } + } + } // ECB or CBC + else + { + // Writing the input data with zero-padding + for (j = 0; j < 16; j++) + { + ENCDI = ((i + j < length) ? pDataIn[i + j]: 0x00); + } + + // wait for data ready + halMcuWaitUs(1); + + // Read out data + for (j = 0; j < 16; j++) + { + pDataOut[i + j] = ENCDO; + } + } + } +} + +//------------------------------------------------------------------- +static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + AES_SET_MODE(mode); + halAesOperation(AES_ENCRYPT, pDataIn, length, pDataOut, pInitVector); +} + +//------------------------------------------------------------------- +static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector) +{ + AES_SET_MODE(mode); + halAesOperation(AES_DECRYPT, pDataIn, length, pDataOut, pInitVector); +} + +//------------------------------------------------------------------- +static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm) +{ + uint8 i, j, l; + + // Prepare input for CBC-MAC, B0, Bi.... Bn + memcpy(buf, pIv, 16); // B0 + + // Create flag + buf[0] = 0x01; // L'= L - 1 (L=2) + if (f > 0) + { + buf[0] |= 0x40; + } + // Adata= f > 0 + buf[0] |= ((lm - 2) / 2) << 3; // M'= (lm-2) / 2; + + buf[14] = 0x00; + buf[15] = c; + + // Add authentication data + buf[16] = 0; + buf[17] = f; + memcpy(&buf[18], pData, f); + + // .... zero padding + i = 18+f; + l = (i &0x0f) == 0 ? i : (i &0xf0) + 0x10; + while (i < l) + { + buf[i++] = 0x00; + } + + // Add command payload + j = 0; + while (i < l + c) + { + buf[i++] = pData[f + j]; + j++; + } + + return i; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.h new file mode 100644 index 0000000..e05c6ca --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_security.h @@ -0,0 +1,21 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_security.h +// Description: HAL radio interface header file for security operations. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// IEEE 802.15.4 security (AES-128) +#define KEY_LENGTH 16 +#define NONCE_LENGTH 16 + +#define SECURITY_CONTROL 0x06 // MIC-64 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +// CCM security interface, IEEE 802.15.4 +void halRfSecurityInit(uint8 *key, uint8 *nonceRx, uint8 *nonceTx); +uint8 halRfReadRxBufSecure(uint8 *pData, uint8 length, uint8 encrLength, uint8 authLength, uint8 m); +void halRfWriteTxBufSecure(uint8 *pData, uint8 length, uint8 encrLength, uint8 authLength, uint8 m); +void halRfIncNonceTx(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.c new file mode 100644 index 0000000..0487a75 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.c @@ -0,0 +1,60 @@ +//------------------------------------------------------------------- +// Filename: hal_rf_util.c +// Description: Support for anergy detection applications. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_mcu.h" +#include "hal_rf_util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halSampleED +// @brief Sample Energy Detect +// @param uint8 channel - channel between 11 and 26 +// uint16 sampleTime - sample time in us +// @return int8 - sampled RSSI value +//------------------------------------------------------------------- +int8 halSampleED(uint8 channel, uint16 sampleTime) +{ + int8 rssi = 0; + + // Set channel + halRfSetChannel(channel); + + // Set RX on + halRfReceiveOn(); + while (!RSSISTAT) + ; + + // Enable energy scan mode, using peak signal strength + FRMCTRL0 |= 0x10; + + // Spend sampleTime us accumulating the peak RSSI value + halMcuWaitUs(sampleTime); + rssi = RSSI; + + // Exit the current channel + halRfReceiveOff(); + // Disable ED scan mode + FRMCTRL0 &= ~0x10; + + return rssi; +} + +//------------------------------------------------------------------- +// @fn halSetRxScanMode +// @brief Set chip in RX scanning mode +// @param none +// @return none +//------------------------------------------------------------------- +void halSetRxScanMode(void) +{ + // Infinite RX mode (disables symbol search) + FRMCTRL0 = 0x0C; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.h new file mode 100644 index 0000000..1a496cd --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_rf_util.h @@ -0,0 +1,10 @@ +//------------------------------------------------------------------- +// Filename: hal_util_rf.h +// Description: +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +// PUBLIC FUNCTIONS +//------------------------------------------------------------------- +int8 halSampleED(uint8 channel, uint16 sampleTime); +void halSetRxScanMode(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.c new file mode 100644 index 0000000..840aefc --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.c @@ -0,0 +1,41 @@ +#include + +void SET_POWER_MODE(unsigned char mode) +{ + if(mode == 0) + SLEEPCMD &= ~0x03; + else + if(mode == 3) + SLEEPCMD |= 0x03; + else + { + SLEEPCMD &= ~0x03; + SLEEPCMD |= mode; + } + + PCON |= 0x01; + asm("NOP"); +} + +void Set_ST_Period(unsigned char sec) +{ + unsigned long sleepTimer = 0; + + sleepTimer |= ST0; + sleepTimer |= (unsigned long)ST1 << 8; + sleepTimer |= (unsigned long)ST2 << 16; + + sleepTimer += ((unsigned long)sec * (unsigned long)32768); + + ST2 = (unsigned char)(sleepTimer >> 16); + ST1 = (unsigned char)(sleepTimer >> 8); + ST0 = (unsigned char) sleepTimer; +} + +void Init_SLEEP_TIMER(void) +{ + EA = 1; + STIE = 1; + STIF = 0; +} + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.h new file mode 100644 index 0000000..83b2601 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_sleep.h @@ -0,0 +1,10 @@ +#include + +#define CRYSTAL 0 +#define RC 1 + +void SET_MAIN_CLOCK_SOURCE(unsigned char source); +void SET_LOW_CLOCK(unsigned char source); +void SET_POWER_MODE(unsigned char mode); +void Set_ST_Period(unsigned char sec); +void Init_SLEEP_TIMER(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.c new file mode 100644 index 0000000..8b8ab4f --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.c @@ -0,0 +1,130 @@ +//------------------------------------------------------------------- +// Filename: hal_timer_32k.c +// Description: hal 32KHz timer +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_int.h" +#include "hal_timer_32k.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +static ISR_FUNC_PTR fptr; + +//------------------------------------------------------------------- +// @fn halTimer32kInit +// @brief Set up timer 1 to generate an interrupt every "cycles" 32768 Hz +// clock cycles. +// Use halTimerIntConnect() to connect an ISR to the interrupt. +// @param uint16 cycles - Number of cycles between interrupt +// (does not support larger cycles value than 0xFFFF/8) +// @return none +//------------------------------------------------------------------- +void halTimer32kInit(uint16 cycles) +{ + uint16 compareValue; + + //uint16 clock_divider = ID_0; // Default - don't divide input clock + + // Set prescaler divider value to 128 + T1CTL |= 0x0C; + + // Multiply cycles by 8 to get 32KHz cycles + // 32MHz/128/8 gives approximately 32KHz + if (cycles > 0xFFFF / 8) + { + compareValue = 0xFFFF; + } + else + { + compareValue = cycles * 8; + } + + // Set compare value + T1CC0H = HI_UINT16(compareValue); + T1CC0L = LO_UINT16(compareValue); + #ifndef CC2530_PG1 + // Set conmpare mode with interrupt enabled + T1CCTL0 = 0x44; + #endif + + // clear interrupt pending flag, disable interrupt + #ifdef CC2530_PG1 + T1CTL &= ~0x10; // T1CTL.OVFIF = 0 + #else + T1STAT &= ~0x01; // T1STAT.CH1IF = 0 + #endif + IEN1 &= ~0x02; // IEN1.T1EN = 0 +} + +//------------------------------------------------------------------- +// @fn halTimer32kRestart +// @brief Restart timer 1. The timer is first stopped, then restarted, +// counting up from 0 +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kRestart(void) +{ + T1CTL &= ~0x02; // Stop timer + IEN1 &= ~0x02; // Disable interrupt + + T1CNTL = 0x00; // Reset counter value + + T1CTL |= 0x02; // Start timer + IEN1 |= 0x02; // Enable interrupt +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntConnect +// @brief Connect function to timer interrupt +// @param ISR_FUNC_PTR isr - pointer to function +// @return none +//------------------------------------------------------------------- +void halTimer32kIntConnect(ISR_FUNC_PTR isr) +{ + istate_t key; + HAL_INT_LOCK(key); + fptr = isr; + HAL_INT_UNLOCK(key); +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntEnable +// @brief Enable 32KHz timer interrupt +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kIntEnable(void) +{ + IEN1 |= 0x02; // IEN1.T1EN = 1 + + // Start timer in modulo mode + T1CTL |= 0x02; // T1CTL.MODE = 10 + +} + +//------------------------------------------------------------------- +// @fn halTimer32kIntDisable +// @brief Disable 32KHz timer interrupt +// @param none +// @return none +//------------------------------------------------------------------- +void halTimer32kIntDisable(void) +{ + IEN1 &= ~0x02; // IEN1.T1EN = 0 +} + +//------------------------------------------------------------------- +// @fn timer32k0_ISR +// @brief ISR framework for the 32KHz timer component +// @param none +// @return none +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(T1_ISR, T1_VECTOR) +{ + if (fptr != NULL) + { + (*fptr)(); + } +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.h new file mode 100644 index 0000000..e9d639e --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_timer_32k.h @@ -0,0 +1,17 @@ +//------------------------------------------------------------------- +// Filename: hal_timer_32k.h +// Description: hal 32KHz timer library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +#define TIMER_32K_CLK_FREQ 32768 + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halTimer32kInit(uint16 cycles); +void halTimer32kRestart(void); +void halTimer32kIntConnect(ISR_FUNC_PTR isr); +void halTimer32kIntEnable(void); +void halTimer32kIntDisable(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.c new file mode 100644 index 0000000..88d9d68 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.c @@ -0,0 +1,286 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.c +// Description: This file contains the interface to the H/W UART driver. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_board.h" +#include "hal_uart.h" + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_UART_ISR_RX_AVAIL() \ +(uartCfg.rxTail >= uartCfg.rxHead) ? \ +(uartCfg.rxTail - uartCfg.rxHead) : \ +(HAL_UART_ISR_RX_MAX - uartCfg.rxHead + uartCfg.rxTail) + +#define HAL_UART_ISR_TX_AVAIL() \ +(uartCfg.txHead > uartCfg.txTail) ? \ +(uartCfg.txHead - uartCfg.txTail - 1) : \ +(HAL_UART_ISR_TX_MAX - uartCfg.txTail + uartCfg.txHead - 1) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// U0CSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// U0UCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX0IE 0x04 +#define U0RX_TX 0x0C + +#define HAL_UART_PERCFG_BIT 0x01 + +#define HAL_UART_ISR_RX_MAX 128 +#define HAL_UART_ISR_TX_MAX HAL_UART_ISR_RX_MAX + +typedef struct +{ + uint8 rxBuf[HAL_UART_ISR_RX_MAX]; + uint8 rxHead; + volatile uint8 rxTail; + uint8 rxShdw; + + uint8 txBuf[HAL_UART_ISR_TX_MAX]; + volatile uint8 txHead; + uint8 txTail; + uint8 txMT; +} uartCfg_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uartCfg_t uartCfg; + +//------------------------------------------------------------------- +// @fn halUartInit +// @brief Initialize the UART +// @param none +// @return none +//------------------------------------------------------------------- +void halUartInit(uint32 baud) +{ + // UART Configuration + PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O location to P0. + + P0SEL |= U0RX_TX; // Enable Tx and Rx peripheral functions on pins. + ADCCFG &= ~U0RX_TX; // Make sure ADC doesnt use this. + U0CSR = CSR_MODE; // Mode is UART Mode. + U0UCR = UCR_FLUSH; // Flush it. + + // Only supporting subset of baudrate for code size - other is possible. + // 38400 + //U0BAUD = 59; + //U0GCR = 10; + // 9600 + //U0BAUD = 59; + //U0GCR = 8; + //U0BAUD = value_u0baud; + //U0GCR = value_u0gcr; + + switch(baud) + { + case 1200 : + U0BAUD = 59; U0GCR = 5; break; + case 2400 : + U0BAUD = 59; U0GCR = 6; break; + case 4800 : + U0BAUD = 59; U0GCR = 7; break; + case 9600 : + U0BAUD = 59; U0GCR = 8; break; + case 14400 : + U0BAUD = 216; U0GCR = 8; break; + case 19200 : + U0BAUD = 59; U0GCR = 9; break; + case 28800 : + U0BAUD = 216; U0GCR = 9; break; + case 38400 : + U0BAUD = 59; U0GCR = 10; break; + case 57600 : + U0BAUD = 216; U0GCR = 10; break; + case 76800 : + U0BAUD = 59; U0GCR = 11; break; + case 115200 : + U0BAUD = 216; U0GCR = 11; break; + case 230400 : + U0BAUD = 216; U0GCR = 12; break; + default : + U0BAUD = 59; U0GCR = 10; break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + U0UCR = UCR_STOP; + U0CSR |= CSR_RE; + URX0IE = 1; + U0DBUF = 0; // Prime the ISR pump. + + uartCfg.rxHead = 0; + uartCfg.rxTail = 0; + uartCfg.txHead = 0; + uartCfg.txTail = 0; + uartCfg.rxShdw = 0; + uartCfg.txMT = 0; + +} + +//------------------------------------------------------------------- +// @fn halUartRead +// @brief Read a buffer from the UART +// @param buf - valid data buffer at least 'len' bytes in size +// len - max length number of bytes to copy to 'buf' +// @return length of buffer that was read +//------------------------------------------------------------------- +uint16 halUartRead(uint8 *buf, uint16 len) +{ + uint16 cnt = 0; + while ((uartCfg.rxHead != uartCfg.rxTail) && (cnt < len)) + { + *buf++ = uartCfg.rxBuf[uartCfg.rxHead++]; + if (uartCfg.rxHead >= HAL_UART_ISR_RX_MAX) + { + uartCfg.rxHead = 0; + } + cnt++; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartWrite +// @brief Write a buffer to the UART. +// @param buf - pointer to the buffer that will be written, not freed +// len - length of +// @return length of the buffer that was sent +//------------------------------------------------------------------- +uint16 halUartWrite(uint8 *buf, uint16 len) +{ + uint16 cnt; + + // Accept "all-or-none" on write request. + if (HAL_UART_ISR_TX_AVAIL() < len) + { + return 0; + } + + for (cnt = 0; cnt < len; cnt++) + { + uartCfg.txBuf[uartCfg.txTail] = *buf++; + uartCfg.txMT = 0; + + if (uartCfg.txTail >= HAL_UART_ISR_TX_MAX - 1) + { + uartCfg.txTail = 0; + } + else + { + uartCfg.txTail++; + } + + // Keep re-enabling ISR as it might be keeping up with this loop due to other ints. + IEN2 |= UTX0IE; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartRxLen() +// @brief Calculate Rx Buffer length - the number of bytes in the buffer. +// @param none +// @return length of current Rx Buffer +//------------------------------------------------------------------- +uint16 halUartRxLen(void) +{ + return HAL_UART_ISR_RX_AVAIL(); +} + +//------------------------------------------------------------------- +// @fn halUartSuspend +// @brief Suspend UART hardware before entering PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUartSuspend(void) +{ + U0CSR &= ~CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartResume +// @brief Resume UART hardware after exiting PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUartResume(void) +{ + U0UCR |= UCR_FLUSH; + U0CSR |= CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartRxIsr +// @brief UART Receive Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart0RxIsr, URX0_VECTOR) +{ + uint8 tmp = U0DBUF; + uartCfg.rxBuf[uartCfg.rxTail] = tmp; + + // Re-sync the shadow on any 1st byte received. + if (uartCfg.rxHead == uartCfg.rxTail) + { + uartCfg.rxShdw = ST0; + } + + if (++uartCfg.rxTail >= HAL_UART_ISR_RX_MAX) + { + uartCfg.rxTail = 0; + } +} + +//------------------------------------------------------------------- +// @fn halUartTxIsr +// @brief UART Transmit Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart0TxIsr, UTX0_VECTOR) +{ + if (uartCfg.txHead == uartCfg.txTail) + { + IEN2 &= ~UTX0IE; + uartCfg.txMT = 1; + } + else + { + UTX0IF = 0; + U0DBUF = uartCfg.txBuf[uartCfg.txHead++]; + + if (uartCfg.txHead >= HAL_UART_ISR_TX_MAX) + { + uartCfg.txHead = 0; + } + } +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.h new file mode 100644 index 0000000..1dfea8c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.h +// Description: hal UART library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------ + +//------------------------------------------------------------------- +// Initialize UART at the startup +//------------------------------------------------------------------- +void halUartInit(uint32 baud); + +//------------------------------------------------------------------- +// Read a buffer from the UART +//------------------------------------------------------------------- +extern uint16 halUartRead(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Write a buff to the uart +//------------------------------------------------------------------- +extern uint16 halUartWrite(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Return the number of bytes in the Rx buffer +//------------------------------------------------------------------- +extern uint16 halUartRxLen(void); + +//------------------------------------------------------------------- +// Abort UART when entering sleep mode +//------------------------------------------------------------------- +extern void halUartSuspend(void); + +//------------------------------------------------------------------- +// Resume UART after wakeup from sleep +//------------------------------------------------------------------- +extern void halUartResume(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.c new file mode 100644 index 0000000..b281d45 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.c @@ -0,0 +1,282 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.c +// Description: This file contains the interface to the H/W UART driver. +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_board.h" +#include "hal_uart1.h" + +//------------------------------------------------------------------- +// MACROS +//------------------------------------------------------------------- +#define HAL_UART1_ISR_RX_AVAIL() \ +(uart1Cfg.rxTail >= uart1Cfg.rxHead) ? \ +(uart1Cfg.rxTail - uart1Cfg.rxHead) : \ +(HAL_UART1_ISR_RX_MAX - uart1Cfg.rxHead + uart1Cfg.rxTail) + +#define HAL_UART1_ISR_TX_AVAIL() \ +(uart1Cfg.txHead > uart1Cfg.txTail) ? \ +(uart1Cfg.txHead - uart1Cfg.txTail - 1) : \ +(HAL_UART1_ISR_TX_MAX - uart1Cfg.txTail + uart1Cfg.txHead - 1) + +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +// U0CSR - USART Control and Status Register. +#define CSR_MODE 0x80 +#define CSR_RE 0x40 +#define CSR_SLAVE 0x20 +#define CSR_FE 0x10 +#define CSR_ERR 0x08 +#define CSR_RX_BYTE 0x04 +#define CSR_TX_BYTE 0x02 +#define CSR_ACTIVE 0x01 + +// U0UCR - USART UART Control Register. +#define UCR_FLUSH 0x80 +#define UCR_FLOW 0x40 +#define UCR_D9 0x20 +#define UCR_BIT9 0x10 +#define UCR_PARITY 0x08 +#define UCR_SPB 0x04 +#define UCR_STOP 0x02 +#define UCR_START 0x01 + +#define UTX1IE 0x08 +#define U1RX_TX 0xC0 + +#define HAL_UART1_PERCFG_BIT 0x02 + +#define HAL_UART1_ISR_RX_MAX 128 +#define HAL_UART1_ISR_TX_MAX HAL_UART1_ISR_RX_MAX + +typedef struct +{ + uint8 rxBuf[HAL_UART1_ISR_RX_MAX]; + uint8 rxHead; + volatile uint8 rxTail; + uint8 rxShdw; + + uint8 txBuf[HAL_UART1_ISR_TX_MAX]; + volatile uint8 txHead; + uint8 txTail; + uint8 txMT; +} uartCfg_t; + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uartCfg_t uart1Cfg; + +//------------------------------------------------------------------- +// @fn halUartInit +// @brief Initialize the UART +// @param none +// @return none +//------------------------------------------------------------------- +void halUart1Init(uint32 baud) +{ + // UART Configuration + PERCFG |= HAL_UART1_PERCFG_BIT; // ÉèÖà UART1 I/O λÖÃÔÚP1¿ÚÉÏ. + + P1SEL |= U1RX_TX; // ʹÄÜ UART1 µÄTx£¨P1.6£©ºÍRx £¨P1.7£©ÍⲿÉ豸¹¦ÄÜÒý½Å. + // ADCCFG &= ~U0RX_TX; // Make sure ADC doesnt use this. + U1CSR = CSR_MODE; // UART ģʽ. + U1UCR = UCR_FLUSH; // Çå³ýµ¥Ôª. + + // Only supporting subset of baudrate for code size - other is possible. + // 38400 + //U1BAUD = 59; + //U1GCR = 10; + //9600 + //U1BAUD = 59; + //U1GCR = 8; + switch(baud) + { + case 2400 : + U1BAUD = 59; U1GCR = 6; break; + case 4800 : + U1BAUD = 59; U1GCR = 7; break; + case 9600 : + U1BAUD = 59; U1GCR = 8; break; + case 14400 : + U1BAUD = 216; U1GCR = 8; break; + case 19200 : + U1BAUD = 59; U1GCR = 9; break; + case 28800 : + U1BAUD = 216; U1GCR = 9; break; + case 38400 : + U1BAUD = 59; U1GCR = 10; break; + case 57600 : + U1BAUD = 216; U1GCR = 10; break; + case 76800 : + U1BAUD = 59; U1GCR = 11; break; + case 115200 : + U1BAUD = 216; U1GCR = 11; break; + case 230400 : + U1BAUD = 216; U1GCR = 12; break; + default : + U1BAUD = 59; U1GCR = 10; break; + } + + // 8 bits/char; no parity; 1 stop bit; stop bit hi. + U1UCR = UCR_STOP; //ֹͣλΪ¸ßµçƽ£¨1£© + U1CSR |= CSR_RE; //UART1 ½ÓÊÕÆ÷ʹÄÜ + URX1IE = 1; //UART1 ½ÓÊÕÖжÏʹÄÜ + U1DBUF = 0; //Êý¾Ý»º³åÆ÷Çå0 + + uart1Cfg.rxHead = 0; + uart1Cfg.rxTail = 0; + uart1Cfg.txHead = 0; + uart1Cfg.txTail = 0; + uart1Cfg.rxShdw = 0; + uart1Cfg.txMT = 0; + +} + +//------------------------------------------------------------------- +// @fn halUartRead +// @brief Read a buffer from the UART +// @param buf - valid data buffer at least 'len' bytes in size +// len - max length number of bytes to copy to 'buf' +// @return length of buffer that was read +//------------------------------------------------------------------- +uint16 halUart1Read(uint8 *buf, uint16 len) +{ + uint16 cnt = 0; + + while ((uart1Cfg.rxHead != uart1Cfg.rxTail) && (cnt < len)) + { + *buf++ = uart1Cfg.rxBuf[uart1Cfg.rxHead++]; + if (uart1Cfg.rxHead >= HAL_UART1_ISR_RX_MAX) + { + uart1Cfg.rxHead = 0; + } + cnt++; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartWrite +// @brief Write a buffer to the UART. +// @param buf - pointer to the buffer that will be written, not freed +// len - length of +// @return length of the buffer that was sent +//------------------------------------------------------------------- +uint16 halUart1Write(uint8 *buf, uint16 len) +{ + uint16 cnt; + + // Accept "all-or-none" on write request. + if (HAL_UART1_ISR_TX_AVAIL() < len) + { + return 0; + } + + for (cnt = 0; cnt < len; cnt++) + { + uart1Cfg.txBuf[uart1Cfg.txTail] = *buf++; + uart1Cfg.txMT = 0; + + if (uart1Cfg.txTail >= HAL_UART1_ISR_TX_MAX - 1) + { + uart1Cfg.txTail = 0; + } + else + { + uart1Cfg.txTail++; + } + + // Keep re-enabling ISR as it might be keeping up with this loop due to other ints. + IEN2 |= UTX1IE; + } + + return cnt; +} + +//------------------------------------------------------------------- +// @fn halUartRxLen() +// @brief Calculate Rx Buffer length - the number of bytes in the buffer. +// @param none +// @return length of current Rx Buffer +//------------------------------------------------------------------- +uint16 halUart1RxLen(void) +{ + return HAL_UART1_ISR_RX_AVAIL(); +} + +//------------------------------------------------------------------- +// @fn halUartSuspend +// @brief Suspend UART hardware before entering PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUart1Suspend(void) +{ + U1CSR &= ~CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartResume +// @brief Resume UART hardware after exiting PM mode 1, 2 or 3. +// @param None +// @return None +//------------------------------------------------------------------- +void halUart1Resume(void) +{ + U1UCR |= UCR_FLUSH; + U1CSR |= CSR_RE; +} + +//------------------------------------------------------------------- +// @fn halUartRxIsr +// @brief UART Receive Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart1RxIsr, URX1_VECTOR) +{ + uint8 tmp = U1DBUF; + uart1Cfg.rxBuf[uart1Cfg.rxTail] = tmp; + + // Re-sync the shadow on any 1st byte received. + if (uart1Cfg.rxHead == uart1Cfg.rxTail) + { + uart1Cfg.rxShdw = ST0; + } + + if (++uart1Cfg.rxTail >= HAL_UART1_ISR_RX_MAX) + { + uart1Cfg.rxTail = 0; + } +} + +//------------------------------------------------------------------- +// @fn halUartTxIsr +// @brief UART Transmit Interrupt +// @param None +// @return None +//------------------------------------------------------------------- +HAL_ISR_FUNCTION(halUart1TxIsr, UTX1_VECTOR) +{ + if (uart1Cfg.txHead == uart1Cfg.txTail) + { + IEN2 &= ~UTX1IE; + uart1Cfg.txMT = 1; + } + else + { + UTX1IF = 0; + U1DBUF = uart1Cfg.txBuf[uart1Cfg.txHead++]; + + if (uart1Cfg.txHead >= HAL_UART1_ISR_TX_MAX) + { + uart1Cfg.txHead = 0; + } + } +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.h new file mode 100644 index 0000000..a528757 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/common/hal_uart1.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------- +// Filename: hal_uart.h +// Description: hal UART library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// FUNCTIONS - API +//------------------------------------------------------------------- +// Initialize UART at the startup +//------------------------------------------------------------------- +extern void halUart1Init(uint32 baud); + +//------------------------------------------------------------------- +// Read a buffer from the UART +//------------------------------------------------------------------- +extern uint16 halUart1Read(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Write a buff to the uart +//------------------------------------------------------------------- +extern uint16 halUart1Write(uint8 *pBuffer, uint16 length); + +//------------------------------------------------------------------- +// Return the number of bytes in the Rx buffer +//------------------------------------------------------------------- +extern uint16 halUart1RxLen(void); + +//------------------------------------------------------------------- +// Abort UART when entering sleep mode +//------------------------------------------------------------------- +extern void halUart1Suspend(void); + +//------------------------------------------------------------------- +// Resume UART after wakeup from sleep +//------------------------------------------------------------------- +extern void halUart1Resume(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.c new file mode 100644 index 0000000..de7bc79 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.c @@ -0,0 +1,38 @@ +//------------------------------------------------------------------- +// Filename: dma_ad590.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_ad590.h" + +//------------------------------------------------------------------- +// @fn dma_ad590_Init +// @brief Set up port ad590 ad +// @return none +//------------------------------------------------------------------- +void dma_ad590_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_ad590_GetValue +// @brief Get this ad590 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_ad590_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.h new file mode 100644 index 0000000..e27099c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_ad590.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: dma_ad590.h +// Description: ad590 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_ad590_Init(void); +uint16 dma_ad590_GetValue(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.c new file mode 100644 index 0000000..00a11df --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.c @@ -0,0 +1,193 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_bma.h" + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } +} + +//------------------------------------------------------------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0x70);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); + halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + + M3_I2cWrite(0x70); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + + M3_I2cStart(); + + M3_I2cWrite(0x71); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + + M3_I2cNack(); + M3_I2cStop(); + + return res; +} + +uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; + +void BMA_Pooling(void) +{ + uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + + BMA_XL_Value = M3_ReadEEPROM(0x02); + BMA_XM_Value = M3_ReadEEPROM(0x03); + BMA_YL_Value = M3_ReadEEPROM(0x04); + BMA_YM_Value = M3_ReadEEPROM(0x05); + BMA_ZL_Value = M3_ReadEEPROM(0x06); + BMA_ZM_Value = M3_ReadEEPROM(0x07); + + BMA_X_Value = (BMA_XM_Value << 2) + ((BMA_XL_Value&0xc0)>>6); + BMA_Y_Value = (BMA_YM_Value << 2) + ((BMA_YL_Value&0xc0)>>6); + BMA_Z_Value = (BMA_ZM_Value << 2) + ((BMA_ZL_Value&0xc0)>>6); +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.h new file mode 100644 index 0000000..f5096dc --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_bma.h @@ -0,0 +1,16 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.h +// Description: dma_bma module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +extern uint16 BMA_X_Value; +extern uint16 BMA_Y_Value; +extern uint16 BMA_Z_Value; + +void M3_Init(void); +//void M3_WriteEEPROM(uint8 addr, uint8 ch); +uint8 M3_ReadEEPROM(uint8 addr); + +void BMA_Pooling(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.c new file mode 100644 index 0000000..446351d --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.c @@ -0,0 +1,59 @@ +//------------------------------------------------------------------- +// Filename: hal_dc.c +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_dc.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn halDcStart +// @brief Turn DC on. +// @param uint8 id - led to set +// @return none +//------------------------------------------------------------------- +void halDcStart(uint8 dc_num) +{ + switch (dc_num) + { + case 1: + HAL_DC_START_1(); + break; + case 2: + HAL_DC_START_2(); + break; + default: + break; + } +} + +//------------------------------------------------------------------- +// @fn halDcStop +// @brief Turn LED off. +// @param uint8 id - led to clear +// @return none +//------------------------------------------------------------------- +void halDcStop(uint8 dc_num) +{ + switch (dc_num) + { + case 1: + HAL_DC_STOP_1(); + break; + case 2: + HAL_DC_STOP_2(); + break; + default: + break; + } +} + + +//------------------------------------------------------------------- diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.h new file mode 100644 index 0000000..063d3de --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_dc.h @@ -0,0 +1,8 @@ +//------------------------------------------------------------------- +// Filename: hal_led.h +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void halDcStart(uint8 dc_num); +void halDcStop(uint8 dc_num); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.c new file mode 100644 index 0000000..0e581dd --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.c @@ -0,0 +1,175 @@ +//------------------------------------------------------------------- +// Filename: dma_eeprom.c +// Description: dma_eeprom module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "dma_eeprom.h" + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_CLR(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); + halMcuWaitUs(2); + + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); +} + +//------------------------------------------------------------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } +} + +//------------------------------------------------------------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + halMcuWaitUs(2); + HAL_E2PROM_SCL_CLR(); + halMcuWaitUs(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xa0);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); + halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + + M3_I2cWrite(0xa0); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + + M3_I2cStart(); + + M3_I2cWrite(0xa1); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + + M3_I2cNack(); + M3_I2cStop(); + + return res; +} + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.h new file mode 100644 index 0000000..6875a54 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_eeprom.h @@ -0,0 +1,11 @@ +//------------------------------------------------------------------- +// Filename: dma_eeprom.h +// Description: dma_eeprom module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- + +void M3_Init(void); +void M3_WriteEEPROM(uint8 addr, uint8 ch); +uint8 M3_ReadEEPROM(uint8 addr); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.c new file mode 100644 index 0000000..fd2b78c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.c @@ -0,0 +1,64 @@ +//------------------------------------------------------------------- +// Filename: dma_imc.c +// Description: dma_imc library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "dma_imc.h" + +//------------------------------------------------------------------- +// LOCAL VARIABLES +//------------------------------------------------------------------- +uint16 M1_ImcValue; + +static const digioConfig pinM1_Imc = +{ + HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN, BV(HAL_BOARD_IO_INT_PIN), HAL_DIGIO_INPUT, 0 +}; + +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +/*static void M1_ImcISR(void) +{ + M1_ImcValue++; +}*/ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M1_Init(void) +{ + M1_ImcValue = 0; + halDigioConfig(&pinM1_Imc); + MCU_IO_INPUT(HAL_BOARD_IO_INT_PORT, HAL_BOARD_IO_INT_PIN, MCU_IO_TRISTATE); //MCU_IO_TRISTATE MCU_IO_PULLUP + halDigioIntSetEdge(&pinM1_Imc, HAL_DIGIO_INT_RISING_EDGE); + // halDigioIntConnect(&pinM1_Imc, &M1_ImcISR); +} + +//------------------------------------------------------------------- +uint16 M1_GetValue(void) +{ + M1_ImcValue = HAL_INT_VAL(); + return M1_ImcValue; +} + +//------------------------------------------------------------------- +void M1_Enable(void) +{ + M1_ImcValue = 0; + halDigioIntEnable(&pinM1_Imc); +} + +//------------------------------------------------------------------- +void M1_Disable(void) +{ + halDigioIntDisable(&pinM1_Imc); +} + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.h new file mode 100644 index 0000000..78c234c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_imc.h @@ -0,0 +1,11 @@ +//------------------------------------------------------------------- +// Filename: dma_imc.h +// Description: dma_imc library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void M1_Init(void); +uint16 M1_GetValue(void); +void M1_Enable(void); +void M1_Disable(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.c new file mode 100644 index 0000000..80accd1 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.c @@ -0,0 +1,204 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_uart.h" +#include "hal_led.h" +#include "dma_itg.h" + + + +//--------- write a byte and checks the ack signal----------------- +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCK_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } + +} + +//--------- read a byte and checks the ack signal----------------- +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + HAL_E2PROM_SCK_SET(); + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR(); + halMcuWaitUs(2); + } + // MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + // HAL_E2PROM_SDA_SET(); + + HAL_E2PROM_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_E2PROM_SCK_CLR(); + HAL_E2PROM_SDA_SET(); + + return res; +} + +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + + HAL_E2PROM_SCK_SET(); //SCL=1 + + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +void M3_I2cAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_SET(); + HAL_E2PROM_SCK_CLR(); +} + +//------------------------------------------------------------------- +void M3_I2cNAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_CLR(); + HAL_E2PROM_SCK_CLR(); +} + +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCK_PORT, HAL_BOARD_IO_E2PROM_SCK_PIN,1); +} + +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xD2);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); +} + +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + M3_I2cWrite(0xD2); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cStart(); + M3_I2cWrite(0xD3); //Æ÷¼þÉèÖÃΪ¶Á״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + M3_I2cNAck(); + M3_I2cStop(); + + return res; +} + +uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; +uint8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; +uint8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + +void BMA_Pooling(void) +{ + // uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + // uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + uint16 num_x1=0,num_y1=0,num_z1=0; + uint16 num_x=0,num_y=0,num_z=0; + uint8 i = 0; + + for(i=0;i<10;i++) + { + while (!(M3_ReadEEPROM(0x1A) & 0x01)); + + BMA_XM_Value = M3_ReadEEPROM(0x1D); //1d 1b + halMcuWaitUs(5); + BMA_XL_Value = M3_ReadEEPROM(0x1E); //1e 1c + halMcuWaitUs(5); + BMA_YM_Value = M3_ReadEEPROM(0x1F); + halMcuWaitUs(5); + BMA_YL_Value = M3_ReadEEPROM(0x20); + halMcuWaitUs(5); + BMA_ZM_Value = M3_ReadEEPROM(0x21); + halMcuWaitUs(5); + BMA_ZL_Value = M3_ReadEEPROM(0x22); + halMcuWaitUs(5); + + + num_x1 = (BMA_XM_Value << 8) + BMA_XL_Value; + num_y1 = (BMA_YM_Value << 8) + BMA_YL_Value; + num_z1 = (BMA_ZM_Value << 8) + BMA_ZL_Value; + + num_x = num_x + num_x1; + num_y = num_y + num_y1; + num_z = num_z + num_z1; + } + BMA_X_Value = num_x/10; + BMA_Y_Value = num_y/10; + BMA_Z_Value = num_z/10; + + halMcuWaitMs(1); +} + + + + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.h new file mode 100644 index 0000000..937107c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_itg.h @@ -0,0 +1,25 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +extern uint16 BMA_X_Value,BMA_Y_Value,BMA_Z_Value; +extern uint8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; +extern uint8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + + + +void M3_Init(void); +void BMA_Pooling(void); +void M3_WriteEEPROM(uint8 addr, uint8 ch); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.c new file mode 100644 index 0000000..14d09aa --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_tgs.c +// Description: dma_tgs library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_kr.h" + +//------------------------------------------------------------------- +// @fn dma_tgs_Init +// @brief Set up port dma_tgs ad +// @return none +//------------------------------------------------------------------- +void dma_tgs_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_tgs_GetValue +// @brief Get this dma_tgs module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.h new file mode 100644 index 0000000..4adb40d --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_kr.h @@ -0,0 +1,6 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs_Init(void); +uint16 dma_tgs_GetValue(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.c new file mode 100644 index 0000000..2cf60c1 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_m4.c +// Description: dma_m4 library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_m4.h" + +//------------------------------------------------------------------- +// @fn dma_m4_Init +// @brief Set up port dma_m4 ad +// @return none +//------------------------------------------------------------------- +void dma_m4_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_m4_GetValue +// @brief Get this dma_m4 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_m4_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.h new file mode 100644 index 0000000..915079b --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_m4.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: dma_m4.h +// Description: dma_m4 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_m4_Init(void); +uint16 dma_m4_GetValue(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_sht.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_sht.h new file mode 100644 index 0000000..c817fb3 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_sht.h @@ -0,0 +1,219 @@ + + +#define ACK 1 +#define noACK 0 +#define measure_temp 0x03 //²âÁ¿Î¶ÈÃüÁî +#define measure_humi 0x05 //²âÁ¿Êª¶ÈÃüÁî +#define RESET 0x1e //ÈíÆô¶¯ + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +//------------------------Êý¾Ý½á¹¹Ì嶨Òå--------------------------- +typedef union //±£´æËù²âµÃµÄζȣ¦Êª¶ÈÖµ +{ uint i; + float f; +} value; + +typedef struct __WENSHIDU__ +{ + uchar gewei; + uchar shiwei; + uchar DateString1[4]; + // uchar DateString2[8]; +}WENSHIDU; + +WENSHIDU S; + + +/***************************º¯ÊýÉùÃ÷*******************************/ +char write_byte(unsigned char value); +char read_byte(unsigned char ack); +void transstart(void); +void connectionreset(void); +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); +void calc_sth11(float *p_humidity ,float *p_temperature); +void DateToStr(WENSHIDU *Time,float datax,float datax1); +void call_sht11(void); + + + + +//--------- write a byte and checks the ack signal----------------- +char write_byte(uchar value) +{ + uchar i,error=0; + + for (i=0x80;i>0;i/=2) /*Á¬ÐøÏòÓÒÒÆ¶¯8λ*/ + { + if (i & value) + HAL_SHT_SDA_SET(); /*°ÑÏàÓ¦µÄλËÍÊý¾ÝÏß*/ + else + HAL_SHT_SDA_CLR(); + + HAL_SHT_SCK_SET(); /*ʱÐòÂö³å£¬Ó¦Ñϸñ°´×Ŵ˱ê×¼*/ + halMcuWaitUs(1); /*´óÔ¼ 1 us*/ + HAL_SHT_SCK_CLR(); + } + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + HAL_SHT_SDA_DIR_IN(); + HAL_SHT_SCK_SET(); /*µÚ9λ×÷ΪÏìӦλ*/ + error=HAL_SHT_SDA_VAL(); /*¼ì²âÏìÓ¦Çé¿ö£¬Èç¹ûÓÐÏìÓ¦Êý¾ÝÏ߾ͻᱻSHT10À­µÍ*/ + HAL_SHT_SCK_CLR(); + + return error; /*·µ»Ø1±íʾûÏìÓ¦*/ +} + +//--------- read a byte and checks the ack signal----------------- +char read_byte(uchar ack) +{ + uchar i,val=0; + + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + HAL_SHT_SDA_DIR_IN(); + for (i=0x80;i>0;i/=2) /*Á¬ÐøÏòÓÒÒÆ¶¯8λ*/ + { + HAL_SHT_SCK_SET(); /*clk for SENSI-BUS*/ + if (HAL_SHT_SDA_VAL()) + val=(val | i); /*read bit */ + HAL_SHT_SCK_CLR(); + } + MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + HAL_SHT_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_SHT_SCK_CLR(); + HAL_SHT_SDA_SET(); /*ÊÍ·ÅÊý¾ÝÏß*/ + + return val; +} + + +void transstart(void) +{ + HAL_SHT_SCK_SET(); + halMcuWaitUs(1); + HAL_SHT_SDA_CLR(); + halMcuWaitUs(1); + HAL_SHT_SCK_CLR(); + halMcuWaitUs(2); + HAL_SHT_SCK_SET(); + halMcuWaitUs(1); + + HAL_SHT_SDA_SET(); + halMcuWaitUs(1); + HAL_SHT_SCK_CLR(); + halMcuWaitUs(2); + HAL_SHT_SDA_CLR(); +} + +void connectionreset(void) +{ + unsigned char i; + + HAL_SHT_SDA_SET(); + HAL_SHT_SCK_CLR(); //Initial state + + for(i=0;i<9;i++) //9 SCK cycles + { + HAL_SHT_SCK_SET(); + HAL_SHT_SCK_CLR(); + } + + transstart(); //transmission start +} + + + +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) +{ + unsigned error=0; + uint i; + + connectionreset(); + switch(mode) + { //·¢ËͼĴæÆ÷µØÖ·¸ø´«¸ÐÆ÷ + case 0 : error+=write_byte(measure_temp); break; + case 1 : error+=write_byte(measure_humi); break; + default : break; + } + HAL_SHT_SDA_DIR_IN(); + for (i=0;i<35000;i++) + { + halMcuWaitUs(1); + if(HAL_SHT_SDA_VAL()== 0) break; + } //µÈ´ýÀ­µÍÊý¾ÝÏß + if(HAL_SHT_SDA_VAL()) error+=1; + *(p_value+1) =read_byte(ACK); //¶Á¸ß×Ö½Ú + *(p_value)=read_byte(ACK); //¶ÁµÍ×Ö½Ú + *p_checksum =read_byte(noACK); + + return error; +} + + +void calc_sth11(float *p_humidity ,float *p_temperature) +{ + const float C1=-4.0; //for 12 Bit + const float C2=+0.0405; //for 12 Bit + const float C3=-0.0000028; //for 12 Bit + const float T1=+0.01; //for 14 Bit @ 5V + const float T2=+0.00008; //for 14 Bit @ 5V + + float rh=*p_humidity; //rh: Humidity [Ticks] 12 Bit + float t=*p_temperature; //*t: Temperature [Ticks] 14 Bit + float rh_lin; //rh_lin: Humidity linear + float rh_true; //rh_true: Temperature compensated humidity + float t_C; //t_C : Temperature [°C] + + t_C=t*0.01 - 40; //calc. temperature from ticks to [°C] + rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH] + rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH] + if(rh_true>100)rh_true=100; //cut if the value is outside of + if(rh_true<0.1)rh_true=0.1; //the physical possible range + + *p_temperature=t_C; //return temperature [°C] + *p_humidity=rh_true; //return humidity[%RH] +} + + +void DateToStr(WENSHIDU *Time,float datax,float datax1) +{ + uint i; + + i=(int)datax; + Time->DateString1[0] = i; + i=(int)((datax-i)*10); + Time->DateString1[1] = i; + + i=(int)datax1; + Time->DateString1[2] = i; + i=(int)((datax1-i)*10); + Time->DateString1[3] = i; + +} + + + +void call_sht11(void) +{ + + value humi_val,temp_val; + unsigned char error = 0,checksum; + + + error+=s_measure((unsigned char*) &humi_val.i,&checksum,1); //measure humidity + error+=s_measure((unsigned char*) &temp_val.i,&checksum,0); //measure temperature + if(error!=0) + connectionreset(); //Èç¹û·¢Éú´íÎó£¬ÏµÍ³¸´Î» + else + { + humi_val.f=(float)humi_val.i; //converts integer to float + temp_val.f=(float)temp_val.i; //converts integer to float + calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity,temperature + DateToStr(&S,temp_val.f,humi_val.f); + } +} + + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.c new file mode 100644 index 0000000..731a8dd --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.c @@ -0,0 +1,101 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tc72.h" + +uint16 TC72_Value = 0; + +void Write_Tc72(uint8 ch) +{ + uint8 i, cmd; + + cmd = ch; + for (i = 0; i < 8; i++) + { + HAL_TC72_SCL_CLR(); + if (cmd &0x80) + { + HAL_TC72_SDI_SET(); + } + else + { + HAL_TC72_SDI_CLR(); + } + cmd <<= 1; + HAL_TC72_SCL_SET(); + } + HAL_TC72_SDI_SET(); +} + +uint16 Read_Tc72(void) +{ + uint8 i, ret; + + HAL_TC72_SDI_CLR(); + for (i = 0; i < 8; i++) + { + HAL_TC72_SCL_CLR(); + ret <<= 1; + HAL_TC72_SCL_SET(); + if (HAL_TC72_SDO_VAL()) + ret |= 0x01; + } + HAL_TC72_SDI_SET(); + return ret; +} + + + +//------------------------------------------------------------------- +void TC72_Start(void) +{ + HAL_TC72_SSS_SET(); + Write_Tc72(0x80); + Write_Tc72(0x15); + HAL_TC72_SSS_CLR(); +} + +//------------------------------------------------------------------- +void TC72_Read(void) +{ + uint16 m,n; + HAL_TC72_SSS_SET(); + + Write_Tc72(0x02); + halMcuWaitUs(200); + m = Read_Tc72(); + halMcuWaitUs(200); + + Write_Tc72(0x01); + halMcuWaitUs(200); + n = Read_Tc72(); + halMcuWaitUs(200); + + TC72_Value = (m<<8)+n; + HAL_TC72_SSS_CLR(); +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void TC72_Init(void) +{ + MCU_IO_INPUT(HAL_BOARD_IO_TC72_SDO_PORT, HAL_BOARD_IO_TC72_SDO_PIN, MCU_IO_PULLUP); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SDI_PORT, HAL_BOARD_IO_TC72_SDI_PIN,1); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SCL_PORT, HAL_BOARD_IO_TC72_SCL_PIN,1); + MCU_IO_OUTPUT(HAL_BOARD_IO_TC72_SSS_PORT, HAL_BOARD_IO_TC72_SSS_PIN,1); +} + +//------------------------------------------------------------------- +uint16 TC72_ReadTemp(void) +{ + TC72_Start(); + halMcuWaitMs(2); + TC72_Read(); + return TC72_Value; +} + + diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.h new file mode 100644 index 0000000..672b851 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tc72.h @@ -0,0 +1,8 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- + +void TC72_Init(void); +uint16 TC72_ReadTemp(void); +void TC72_Enable(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.c new file mode 100644 index 0000000..034a3a8 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: dma_tgs.c +// Description: dma_tgs library (A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tgs.h" + +//------------------------------------------------------------------- +// @fn dma_tgs_Init +// @brief Set up port dma_tgs ad +// @return none +//------------------------------------------------------------------- +void dma_tgs_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn dma_tgs_GetValue +// @brief Get this dma_tgs module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.h new file mode 100644 index 0000000..4adb40d --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs.h @@ -0,0 +1,6 @@ + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs_Init(void); +uint16 dma_tgs_GetValue(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.c new file mode 100644 index 0000000..9e743a2 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.c @@ -0,0 +1,39 @@ +//------------------------------------------------------------------- +// Filename: M190.c +// Description: hal M190 library å�¯è®Šé›»é˜»(A/D) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_digio.h" +#include "hal_adc.h" +#include "dma_tgs2602.h" + +//------------------------------------------------------------------- +// @fn M190_Init +// @brief Set up port M190 ad +// @return none +//------------------------------------------------------------------- +void dma_tgs2602_Init(void) +{ + // Analog input + MCU_IO_PERIPHERAL(HAL_BOARD_IO_ADC_PORT, HAL_BOARD_IO_ADC_CH); + +} + +//------------------------------------------------------------------- +// @fn M190_GetValue +// @brief Get this M190 module value +// @param none +// @return none +//------------------------------------------------------------------- +uint16 dma_tgs2602_GetValue(void) +{ + uint16 adcValue; + adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_12_BIT, HAL_BOARD_IO_ADC_CH); + return adcValue; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.h new file mode 100644 index 0000000..fb8f887 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/moudle/dma_tgs2602.h @@ -0,0 +1,9 @@ +//------------------------------------------------------------------- +// Filename: M190.h +// Description: M190 module library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +void dma_tgs2602_Init(void); +uint16 dma_tgs2602_GetValue(void); diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/Flash_data.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/Flash_data.c new file mode 100644 index 0000000..c0c5f3a --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/Flash_data.c @@ -0,0 +1,73 @@ +#include "hal_defs.h" +#include "hal_flash.h" +#include "Flash_data.h" + +#define OSAL_NV_WORD_SIZE 4 + + +void writeWordM( uint8 pg, uint16 offset, uint8 *buf, uint16 cnt ) +{ + offset = (offset >> 2) + ((uint16)pg << 9); + HalFlashWrite(offset, buf, cnt); + +} + + +void writeWord( uint8 pg, uint16 offset, uint8 *buf ) +{ + offset = (offset / HAL_FLASH_WORD_SIZE) + ((uint16)pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)); + HalFlashWrite(offset, buf, 1); +} + + +extern void writeBuf( uint8 dstPg, uint16 dstOff, uint16 len, uint8 *buf ) +{ + uint8 rem = dstOff % OSAL_NV_WORD_SIZE; + uint8 tmp[OSAL_NV_WORD_SIZE]; + + if ( rem ) + { + dstOff = (dstOff / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE; + HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE); + + while ( (rem < OSAL_NV_WORD_SIZE) && len ) + { + tmp[rem++] = *buf++; + len--; + } + + writeWord( dstPg, dstOff, tmp ); + dstOff += OSAL_NV_WORD_SIZE; + } + + rem = len % OSAL_NV_WORD_SIZE; + len /= OSAL_NV_WORD_SIZE; + + if ( len ) + { + writeWordM( dstPg, dstOff, buf, len ); + dstOff += OSAL_NV_WORD_SIZE * len; + buf += OSAL_NV_WORD_SIZE * len; + } + + if ( rem ) + { + uint8 idx = 0; + HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE); + while ( rem-- ) + { + tmp[idx++] = *buf++; + } + writeWord( dstPg, dstOff, tmp ); + } + } + + +void delay(int time ) +{ + uint16 cntr1,cntr2; + for (cntr1=0;cntr1 +#include +#include + + +void WaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +/********************************************************** +func: ½ÓÊÕ´®¿ÚÊý¾Ý°ü +²ÎÊý£º + 1. *buf -- ´®¿Ú½ÓÊÕÊý¾ÝËù´æ·ÅµÄµØ·½ + 2. maxlen -- ´®¿Ú½ÓÊÕÊý¾ÝµÄ×î´ó³¤¶È +retuen: uint16 ½ÓÊÕµ½µÄÊý¾Ý³¤¶È +***********************************************************/ +uint16 uart_read(uint8 *buf, uint16 maxlen) +{ + uint16 slen=0; + uint8 dtime=0; + while(dtime<20) + { + while(HalUARTRead(buf+slen, 1)) + { + slen++; + if(slen>=maxlen) + return slen; + dtime=0; + } + WaitUs(30); // Wait 30us; + dtime++; + } + return slen; +} +/***** +°´printf¸ñʽ´òÓ¡Êä³ö×Ö·û´® + +***/ +void printf_str( char *buf, char *fmt,... ) +{ + va_list ap; + va_start(ap,fmt); + vsprintf(buf,fmt,ap); + va_end(ap); +} +/********/ +void uart_printf(char *fmt,...) +{ + va_list ap; + char str[256]; + + va_start(ap,fmt); + vsprintf(str,fmt,ap); + UartWrite(str,strlen(str)); + va_end(ap); +} + +void uart_enter(void) +{ + + uart_printf("\r\n"); + +} + +void uart_datas(void * fmt,uint16 len) +{ + char * ptr; + ptr=fmt; + while(len--) + { + uart_printf("%02x ",*ptr ); + ptr++; + } + uart_enter(); +} + +uint8 lrc_checksum(uint8* buf, uint8 len) +{ + uint8 lrc=0; + while(len--) + { + lrc+=*buf; + buf++; + } + lrc =~lrc; + lrc++; + return lrc; +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_PRINT.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_PRINT.h new file mode 100644 index 0000000..771f60a --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_PRINT.h @@ -0,0 +1,17 @@ + + +#ifndef uart_PRINT_H +#define uart_PRINT_H +//#define UartWrite(buf,len) (HalUARTWrite(HAL_UART_PORT_0,(buf),(len))) +#define UartWrite(buf,len) (halUartWrite((buf),(len))) +#define HalUARTRead(buf,len) (halUartRead((buf),(len))) + +uint16 RecvUartData(uint8 *uRxData, uint16 uRxlen); +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen); +extern void uart_datas(void *fmt,uint16 len); +extern void uart_enter(void); +extern void uart_printf(char *fmt,...); +extern void printf_str(char *buf, char *fmt,...); +extern uint16 uart_read(uint8 *buf, uint16 maxlen); +extern uint8 lrc_checksum(uint8* buf, uint8 len); +#endif \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c new file mode 100644 index 0000000..93fd870 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.c @@ -0,0 +1,74 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_uart.h" +#include "hal_uart1.h" + + +/************************************************************************************** +²ÎÊý£º + 1. *dst -- Ä¿µÄÊý×éµÄÊ×µØÖ·£¬Ò²¾ÍÊÇ×îºó´æ·ÅÊý¾ÝµÄµØ·½ + 2. dststart -- ´ÓÄ¿µÄÊý×éµÄµÚ dststart λ¿ªÊ¼´æ·ÅÊý¾Ý + 3. *src -- Ô´Êý¾ÝµÄÊ×µØÖ·£¬Ò²¾ÍÊÇÊý¾ÝµÄÀ´Ô´ + 4. srcstart -- ´ÓÔ´Êý×éµÄµÚ srcstart λ¿ªÊ¼È¡Êý¾Ý + 5. len -- ÐèÒª´ÓÔ´Êý×éÈ¡ len ×Ö½ÚµÄÊý¾Ý´æ·Åµ½Ä¿µÄÊý×éÖÐ +Àý×Ó£º + MyByteCopy(buf1, 5, buf2, 3, 4)£» + ´ÓÊý×é buf2 µÄµÚ3¸ö×Ö½Ú¿ªÊ¼ÍùºóµÄ4¸ö×Ö½Ú£¨buf2[3]¡¢buf2[4]¡¢buf2[5]¡¢buf2[6]£© + ¸´ÖƵ½ buf1[5]¡¢buf1[6]¡¢buf1[7]¡¢buf1[8]ÖС£ +***************************************************************************************/ +void MyByteCopy(uint8 *dst, int dststart, uint8 *src, int srcstart, int len) +{ + int i; + for(i=0; i 0) + { + r_UartLen = halUartRead(r_UartBuf, sizeof(r_UartBuf)); + MyByteCopy(uRxData, uRxlen, r_UartBuf, 0, r_UartLen); + uRxlen += r_UartLen; + halMcuWaitMs(5); //ÕâÀïµÄÑӳٷdz£ÖØÒª£¬ÒòΪÕâÊÇ´®¿ÚÁ¬Ðø¶ÁÈ¡Êý¾ÝʱºòÐèÒªÓÐÒ»¶¨µÄʱ¼ä¼ä¸ô + r_UartLen = halUartRxLen(); + } + return uRxlen; +} + +/********************************************************** +²ÎÊý£º + 1. *uRxData -- ´®¿Ú1½ÓÊÕÊý¾ÝËù´æ·ÅµÄµØ·½ + 2. uRxlen -- ´®¿Ú1½ÓÊÕÊý¾ÝµÄ³õʼ³¤¶È£¨Ò»°ãΪ0£© +***********************************************************/ +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen) +{ + uint16 r_Uart1Len = 0; + uint8 r_Uart1Buf[128]; + + r_Uart1Len = halUart1RxLen(); + halMcuWaitMs(5); + while(r_Uart1Len > 0) + { + r_Uart1Len = halUart1Read(r_Uart1Buf, sizeof(r_Uart1Buf)); + MyByteCopy(uRxData, uRxlen, r_Uart1Buf, 0, r_Uart1Len); + uRxlen += r_Uart1Len; + halMcuWaitMs(5); //ÕâÀïµÄÑӳٷdz£ÖØÒª£¬ÒòΪÕâÊÇ´®¿ÚÁ¬Ðø¶ÁÈ¡Êý¾ÝʱºòÐèÒªÓÐÒ»¶¨µÄʱ¼ä¼ä¸ô + r_Uart1Len = halUart1RxLen(); + } + return uRxlen; +} \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h new file mode 100644 index 0000000..d08350f --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/mylib/UART_SEND_RECV.h @@ -0,0 +1,4 @@ + +void MyByteCopy(uint8 *dst, int dststart, uint8 *src, int srcstart, int len); +uint16 RecvUartData(uint8 *uRxData, uint16 uRxlen); +uint16 RecvUart1Data(uint8 *uRxData, uint16 uRxlen); \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/readme.doc b/Zigbee无线通信/Bæ�¿/CC2530_lib/readme.doc new file mode 100644 index 0000000..f808f56 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/CC2530_lib/readme.doc differ diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.c b/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.c new file mode 100644 index 0000000..cd5405a --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.c @@ -0,0 +1,129 @@ +//------------------------------------------------------------------- +// Filename: util.c +// Description: Utility library +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +#include "hal_defs.h" +#include "util.h" +#include "hal_rf.h" + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// @fn utilChipIdToStr +// @brief Converts a chip ID to a text string. +// @param uint8 chipID +// @return none +//------------------------------------------------------------------- +const char *utilChipIdToStr(uint8 chipID) +{ + const char *szId; + + switch (chipID) + { + case HAL_RF_CHIP_ID_CC2420: + szId = "2420"; + break; + case HAL_RF_CHIP_ID_CC2430: + szId = "2430"; + break; + case HAL_RF_CHIP_ID_CC2431: + szId = "2431"; + break; + case HAL_RF_CHIP_ID_CC2520: + szId = "2520"; + break; + case HAL_RF_CHIP_ID_CC2530: + szId = "2530"; + break; + case HAL_RF_CHIP_ID_CC2531: + szId = "2531"; + break; + case HAL_RF_CHIP_ID_CC2510: + szId = "2510"; + break; + case HAL_RF_CHIP_ID_CC2511: + szId = "2511"; + break; + case HAL_RF_CHIP_ID_CC1110: + szId = "1110"; + break; + case HAL_RF_CHIP_ID_CC1111: + szId = "1111"; + break; + default: + szId = "----"; + }; + + return szId; +} + +//------------------------------------------------------------------- +// @fn convInt32ToText +// @brief Converts 32 bit int to text +// @param int32 value +// @return char* - pointer to text buffer which is a file scope allocated array +//------------------------------------------------------------------- +char *convInt32ToText(int32 value) +{ + static char pValueToTextBuffer[12]; + char *pLast; + char *pFirst; + char last; + uint8 negative; + + pLast = pValueToTextBuffer; + + // Record the sign of the value + negative = (value < 0); + value = ABS(value); + + // Print the value in the reverse order + do + { + *(pLast++) = '0' + (uint8)(value % 10); + value /= 10; + } + while (value); + + // Add the '-' when the number is negative, and terminate the string + if (negative) + { + *(pLast++) = '-'; + } + *(pLast--) = 0x00; + + // Now reverse the string + pFirst = pValueToTextBuffer; + while (pLast > pFirst) + { + last = *pLast; + *(pLast--) = *pFirst; + *(pFirst++) = last; + } + + return pValueToTextBuffer; +} + +//------------------------------------------------------------------- +// @fn utilReverseBuf +// @brief reverse buffer +// @param uint8 pBuf - pointer to buffer +// uint8 length - length of buffer +// @return void +//------------------------------------------------------------------- +void utilReverseBuf(uint8 *pBuf, uint8 length) +{ + uint8 temp; + uint8 *pBufLast = (pBuf + length - 1); + + while (pBufLast > pBuf) + { + temp = *pBuf; + *pBuf++ = *pBufLast; + *pBufLast-- = temp; + } +} diff --git a/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.h b/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.h new file mode 100644 index 0000000..dcf47d1 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/CC2530_lib/utils/util.h @@ -0,0 +1,31 @@ +//------------------------------------------------------------------- +// Filename: util.h +// Description: Utility library header file +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// TYPEDEFS +//------------------------------------------------------------------- +typedef struct +{ + const char *szDescr; // Textual description + const uint8 value; // Value +} menuItem_t; + +typedef struct +{ + const menuItem_t *pMenuItems; + const uint8 nItems; +} menu_t; + +//------------------------------------------------------------------- +// CONSTANTS AND DEFINES +//------------------------------------------------------------------- +// Number of elements in an array +#define N_ITEMS(arr) sizeof(arr)/sizeof(arr[0]) + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +char *convInt32ToText(int32 value); +const char *utilChipIdToStr(uint8 chipID); +void utilReverseBuf(uint8 *pBuf, uint8 length); diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Exe/rf_set.d51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Exe/rf_set.d51 new file mode 100644 index 0000000..dcb09b6 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Exe/rf_set.d51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/BMA020.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/BMA020.r51 new file mode 100644 index 0000000..56218f2 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/BMA020.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/basic_rf.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/basic_rf.r51 new file mode 100644 index 0000000..fa11446 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/basic_rf.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/dma_itg.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/dma_itg.r51 new file mode 100644 index 0000000..a0a38fb Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/dma_itg.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_adc.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_adc.r51 new file mode 100644 index 0000000..96620f1 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_adc.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_swsensor.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_swsensor.r51 new file mode 100644 index 0000000..2661be1 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/get_swsensor.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_board.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_board.r51 new file mode 100644 index 0000000..04bc1bb Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_board.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_clock.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_clock.r51 new file mode 100644 index 0000000..dc543a7 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_clock.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_digio.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_digio.r51 new file mode 100644 index 0000000..da3bcdc Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_digio.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_int.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_int.r51 new file mode 100644 index 0000000..99ba405 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_int.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_led.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_led.r51 new file mode 100644 index 0000000..17860ad Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_led.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_mcu.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_mcu.r51 new file mode 100644 index 0000000..0959543 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_mcu.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf.r51 new file mode 100644 index 0000000..d22652d Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf_security.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf_security.r51 new file mode 100644 index 0000000..d16008f Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_rf_security.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_uart.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_uart.r51 new file mode 100644 index 0000000..4e53776 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/hal_uart.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.pbd b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.pbd new file mode 100644 index 0000000..3c329d0 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.pbd @@ -0,0 +1,19 @@ +This is an internal working file generated by the Source Browser. +22:14 49s +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\BMA020.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\basic_rf.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\dma_itg.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\get_adc.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\get_swsensor.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_board.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_clock.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_digio.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_int.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_led.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_mcu.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_rf.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_rf_security.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\hal_uart.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\rf_set.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\sensor.pbi +E:\cc2530-projects\ZigbeeÎÞÏßͨÐÅ\B°å\Project\Debug\Obj\util.pbi diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.r51 new file mode 100644 index 0000000..222c0fa Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/rf_set.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/sensor.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/sensor.r51 new file mode 100644 index 0000000..54fb7aa Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/sensor.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/util.r51 b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/util.r51 new file mode 100644 index 0000000..e7faf07 Binary files /dev/null and b/Zigbee无线通信/Bæ�¿/Project/Debug/Obj/util.r51 differ diff --git a/Zigbee无线通信/Bæ�¿/Project/rf_set.c b/Zigbee无线通信/Bæ�¿/Project/rf_set.c new file mode 100644 index 0000000..cd192a1 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/rf_set.c @@ -0,0 +1,154 @@ +#include "hal_defs.h" +#include "hal_cc8051.h" +#include "hal_int.h" +#include "hal_mcu.h" +#include "hal_board.h" +#include "hal_led.h" +#include "hal_rf.h" +#include "basic_rf.h" +#include "hal_uart.h" +#include "sensor_drv/sensor.h" +#include +#include +#include +/*****µã¶ÔµãͨѶµØÖ·ÉèÖÃ***B***/ +#define RF_CHANNEL 25 // ƵµÀ 11~26 +#define PAN_ID 0xFF //ÍøÂçid +#define MY_ADDR 0xA2 //±¾»úÄ£¿éµØÖ· +#define SEND_ADDR 0xA1 //·¢Ë͵ØÖ· +/** +* ÎÞÏßͨÐÅʵÑé-B°å +* +*/ + +//LED & SW +#define LED1 P1_0 +#define SW P1_2 + +//°´¼ü״̬ + uint8 key_down_status = 0x00; +//·¢ËÍÊý¾Ý + uint8 send_data = 0x00; +//½ÓÊÕÊý¾Ý + uint8 recv_data = 0x00; +//Ë«»÷ʱ¼ä¼ä¸ô +unsigned int time = 0; +//µ±Ç°ºôÎüÁÁ¶È +unsigned char hx = 0 ; +//ÁÁ¶È±¸·Ý +unsigned char backup_h = 0; +//ºôÎü±ê־λ +unsigned char breath_sign = 0; + + +/**************************************************/ +static basicRfCfg_t basicRfConfig; +// ÎÞÏßRF³õʼ»¯ +void ConfigRf_Init(void) +{ + basicRfConfig.panId = PAN_ID; + basicRfConfig.channel = RF_CHANNEL; + basicRfConfig.myAddr = MY_ADDR; + basicRfConfig.ackRequest = TRUE; + while(basicRfInit(&basicRfConfig) == FAILED); + basicRfReceiveOn(); +} +//³õʼ»¯GPIO +void initial_gpio(){ + + P1SEL &=~0X05; + P1DIR |=0X01; + P1DIR &=~0X04; + P1INP &=~0X04; + P2INP &=~0X40; + LED1 = 0; +} +//³õʼ»¯T1 +void initial_t1(){ + T1CTL |=0X01; + PERCFG = 0X40; + P1SEL |=0X01; + T1CCTL2 = 0X64; + T1CC2L = 0XFF; + T1CC2H = hx; +} +//ºôÎüЧ¹û +void brearh(){ + static unsigned char js = 0; + static unsigned char a = 1; + if((T1STAT & 0X04)>0){ + T1STAT &= 0XFB; + js++; + if(js>=1){ + js = 0; + if(a ==1){ + hx=hx+1; + halMcuWaitMs(10); + }else if(a == 2){ + hx=hx-1; + halMcuWaitMs(10); + } + if(hx>=254){ + a = 2; + } + if(hx == 0){ + a = 1; + } + T1CC2L = 0XFF; + T1CC2H = hx; + backup_h = hx; + + } + } +} +//Åжϰ´¼ü״̬ +uint8 key_down(){ + if(SW == 0){ + halMcuWaitMs(50); + if(SW == 0){ + halMcuWaitMs(100); + time = 500; + while(time--){ + if(SW == 0){ + while(!SW); + return 11; + } + } + } + } + return 0; +} +/********************MAIN************************/ +void main(void) +{ + halBoardInit();//Ñ¡ÊÖ²»µÃÔڴ˺¯ÊýÄÚÌí¼Ó´úÂë + ConfigRf_Init();//Ñ¡ÊÖ²»µÃÔڴ˺¯ÊýÄÚÌí¼Ó´úÂë + initial_gpio(); + initial_t1(); + while(1) + { + key_down_status = key_down(); + if(key_down_status == 11){ + send_data = 0x1D; + //·¢ËÍÊý¾Ý + basicRfSendPacket(SEND_ADDR,&send_data,1); + } + //½ÓÊÕÊý¾Ý + if(basicRfPacketIsReady()){ + basicRfReceive(&recv_data,1,NULL); + if(recv_data == 0x1F){ + brearh(); + breath_sign = 1; + }else if(recv_data == 0x2F){ + breath_sign = 0; + } + } + //ÅжÏÊÇ·ñ¿ªÆôºôÎüµÆ + if(breath_sign){ + brearh(); + hx = backup_h; + }else{ + T1CC2H = backup_h; + } + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/Project/rf_set.dep b/Zigbee无线通信/Bæ�¿/Project/rf_set.dep new file mode 100644 index 0000000..3c04fb4 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/rf_set.dep @@ -0,0 +1,524 @@ + + + + 2 + 2330247038 + + Debug + + $PROJ_DIR$\Debug\Obj\BMA020.r51 + $PROJ_DIR$\..\CC2530_lib\utils\util.h + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.h + $PROJ_DIR$\Debug\Obj\hal_uart.pbi + $PROJ_DIR$\Debug\Obj\hal_mcu.pbi + $PROJ_DIR$\Debug\Obj\hal_rf_security.r51 + $TOOLKIT_DIR$\inc\clib\stdarg.h + $PROJ_DIR$\Debug\Obj\hal_clock.pbi + $PROJ_DIR$\Debug\Obj\basic_rf.pbi + $PROJ_DIR$\Debug\Obj\BMA020.pbi + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.h + $PROJ_DIR$\Debug\Obj\hal_digio.r51 + $PROJ_DIR$\SENSOR_DRV\sensor.h + $PROJ_DIR$\..\CC2530_lib\utils\util.c + $PROJ_DIR$\Debug\Obj\get_adc.r51 + $PROJ_DIR$\Debug\Obj\hal_digio.pbi + $PROJ_DIR$\Debug\Obj\hal_uart.r51 + $PROJ_DIR$\Debug\Obj\hal_int.pbi + $TOOLKIT_DIR$\inc\clib\sysmac.h + $PROJ_DIR$\..\CC2530_lib\common\hal_cc8051.h + $PROJ_DIR$\Debug\Exe\rec.hex + $PROJ_DIR$\Debug\Obj\hal_rf.pbi + $TOOLKIT_DIR$\inc\ioCC2530.h + $PROJ_DIR$\..\CC2530_lib\common\hal_defs.h + $PROJ_DIR$\Debug\Obj\get_swsensor.r51 + $PROJ_DIR$\..\CC2530_lib\common\hal_int.h + $PROJ_DIR$\Debug\Obj\rf_set.r51 + $PROJ_DIR$\Debug\Obj\rf_set.pbi + $PROJ_DIR$\Debug\Obj\get_swsensor.pbi + $PROJ_DIR$\Debug\Obj\hal_rf_security.pbi + $PROJ_DIR$\Debug\Obj\hal_led.pbi + $PROJ_DIR$\Debug\Obj\hal_clock.r51 + $PROJ_DIR$\Debug\Obj\sensor.pbi + $PROJ_DIR$\Debug\Obj\util.pbi + $PROJ_DIR$\Debug\Obj\rf_set.pbd + $PROJ_DIR$\Debug\Obj\dma_itg.pbi + $PROJ_DIR$\Debug\Exe\rf_set.d51 + $TOOLKIT_DIR$\lib\clib\cl-pli-blxd-1e16x01.r51 + $TOOLKIT_DIR$\config\devices\Texas Instruments\lnk51ew_CC2530F256_banked.xcl + $PROJ_DIR$\Debug\Obj\hal_int.r51 + $PROJ_DIR$\Debug\Obj\hal_led.r51 + $PROJ_DIR$\Debug\Obj\hal_mcu.r51 + $TOOLKIT_DIR$\inc\clib\stdio.h + $PROJ_DIR$\Debug\Obj\hal_board.pbi + $PROJ_DIR$\Debug\Obj\hal_rf.r51 + $PROJ_DIR$\Debug\Obj\hal_board.r51 + $PROJ_DIR$\Debug\Obj\dma_itg.r51 + $PROJ_DIR$\Debug\Obj\get_adc.pbi + $TOOLKIT_DIR$\inc\clib\string.h + $PROJ_DIR$\Debug\Obj\sensor.r51 + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.h + $PROJ_DIR$\Debug\Obj\basic_rf.r51 + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.h + $PROJ_DIR$\Debug\Obj\util.r51 + $PROJ_DIR$\..\CC2530_lib\board\hal_board.h + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.h + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.h + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.h + $PROJ_DIR$\..\CC2530_lib\board\hal_led.h + $PROJ_DIR$\SENSOR_DRV\get_adc.c + $PROJ_DIR$\SENSOR_DRV\get_swsensor.h + $PROJ_DIR$\SENSOR_DRV\sensor.c + $PROJ_DIR$\SENSOR_DRV\dma_itg.h + $PROJ_DIR$\SENSOR_DRV\get_adc.h + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + $PROJ_DIR$\SENSOR_DRV\BMA020.c + $PROJ_DIR$\SENSOR_DRV\BMA020.h + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + $PROJ_DIR$\rf_set.c + + + $PROJ_DIR$\..\CC2530_lib\utils\util.c + + + ICC8051 + 53 + + + BICOMP + 33 + + + + + ICC8051 + 23 22 1 55 + + + BICOMP + 23 22 1 55 + + + + + [ROOT_NODE] + + + XLINK + 36 20 + + + + + $PROJ_DIR$\Debug\Obj\rf_set.pbd + + + BILINK + 9 8 35 47 28 43 7 15 17 30 4 21 29 3 27 32 33 + + + + + $PROJ_DIR$\Debug\Exe\rf_set.d51 + + + XLINK + 20 + + + + + XLINK + 38 0 51 46 14 24 45 31 11 39 40 41 44 5 16 26 49 53 37 + + + + + $PROJ_DIR$\SENSOR_DRV\get_adc.c + + + ICC8051 + 14 + + + BICOMP + 47 + + + + + ICC8051 + 22 23 63 + + + BICOMP + 22 23 63 + + + + + $PROJ_DIR$\SENSOR_DRV\sensor.c + + + ICC8051 + 49 + + + BICOMP + 32 + + + + + ICC8051 + 23 22 50 63 12 62 67 + + + BICOMP + 23 22 50 63 12 62 67 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + + + ICC8051 + 44 + + + BICOMP + 21 + + + + + ICC8051 + 23 22 50 25 1 55 + + + BICOMP + 23 22 50 25 1 55 + + + + + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + + + ICC8051 + 16 + + + BICOMP + 3 + + + + + ICC8051 + 23 22 54 52 + + + BICOMP + 23 22 54 52 + + + + + $PROJ_DIR$\SENSOR_DRV\BMA020.c + + + ICC8051 + 0 + + + BICOMP + 9 + + + + + ICC8051 + 22 23 67 + + + BICOMP + 22 23 67 + + + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + + + ICC8051 + 46 + + + BICOMP + 35 + + + + + ICC8051 + 22 23 62 + + + BICOMP + 22 23 62 + + + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + + + ICC8051 + 24 + + + BICOMP + 28 + + + + + ICC8051 + 22 23 60 + + + BICOMP + 22 23 60 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + + + ICC8051 + 31 + + + BICOMP + 7 + + + + + ICC8051 + 23 22 56 + + + BICOMP + 23 22 56 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + + + ICC8051 + 5 + + + BICOMP + 29 + + + + + ICC8051 + 23 22 50 55 2 1 48 18 + + + BICOMP + 23 22 50 55 2 1 48 18 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + + + ICC8051 + 41 + + + BICOMP + 4 + + + + + ICC8051 + 23 22 56 50 54 + + + BICOMP + 23 22 56 50 54 + + + + + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + + + ICC8051 + 40 + + + BICOMP + 30 + + + + + ICC8051 + 23 22 19 50 54 58 + + + BICOMP + 23 22 19 50 54 58 + + + + + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + + + ICC8051 + 51 + + + BICOMP + 8 + + + + + ICC8051 + 23 22 25 50 55 10 48 18 + + + BICOMP + 23 22 25 50 55 10 48 18 + + + + + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + + + ICC8051 + 45 + + + BICOMP + 43 + + + + + ICC8051 + 23 22 19 50 57 25 54 52 + + + BICOMP + 23 22 19 50 57 25 54 52 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + + + ICC8051 + 11 + + + BICOMP + 15 + + + + + ICC8051 + 23 22 25 57 + + + BICOMP + 23 22 25 57 + + + + + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + + + ICC8051 + 39 + + + BICOMP + 17 + + + + + ICC8051 + 23 22 25 + + + BICOMP + 23 22 25 + + + + + $PROJ_DIR$\rf_set.c + + + ICC8051 + 26 + + + BICOMP + 27 + + + + + ICC8051 + 23 22 19 25 50 54 58 55 10 52 12 42 6 18 48 + + + BICOMP + 23 22 19 25 50 54 58 55 10 52 12 42 6 18 48 + + + + + + Release + + + [MULTI_TOOL] + XLINK + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/rf_set.ewd b/Zigbee无线通信/Bæ�¿/Project/rf_set.ewd new file mode 100644 index 0000000..99972a8 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/rf_set.ewd @@ -0,0 +1,1281 @@ + + + + 2 + + Debug + + 8051 + + 1 + + C-SPY + 2 + + 8 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _3RD_ID + 1 + + 0 + 1 + 1 + + + + + + + + CHIPCON_ID + 2 + + 3 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + FS2_ID + 1 + + 0 + 1 + 1 + + + + + + + + + + + + + INFINEON_ID + 1 + + 1 + 1 + 1 + + + + + + + + + + + + + + + + NS_ID + 1 + + 0 + 1 + 1 + + + + + + + ROM_ID + 1 + + 2 + 1 + 1 + + + + + + + + + + + + + + + + + + AD2_ID + 2 + + 6 + 1 + 1 + + + + + + + + + + + + + + CYGNAL_ID + 1 + + 1 + 1 + 1 + + + + + + + + + + + + + + + + + + + SIM_ID + 1 + + 2 + 1 + 1 + + + + + + + + + $EW_DIR$\common\plugins\CodeCoverage\CodeCoverage.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\Orti\Orti.ENU.ewplugin + 0 + + + $EW_DIR$\common\plugins\Stack\Stack.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\SymList\SymList.ENU.ewplugin + 1 + + + + + Release + + 8051 + + 0 + + C-SPY + 2 + + 8 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _3RD_ID + 1 + + 0 + 1 + 0 + + + + + + + + CHIPCON_ID + 2 + + 3 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + FS2_ID + 1 + + 0 + 1 + 0 + + + + + + + + + + + + + INFINEON_ID + 1 + + 1 + 1 + 0 + + + + + + + + + + + + + + + + NS_ID + 1 + + 0 + 1 + 0 + + + + + + + ROM_ID + 1 + + 2 + 1 + 0 + + + + + + + + + + + + + + + + + + AD2_ID + 2 + + 6 + 1 + 0 + + + + + + + + + + + + + + CYGNAL_ID + 1 + + 1 + 1 + 0 + + + + + + + + + + + + + + + + + + + SIM_ID + 1 + + 2 + 1 + 0 + + + + + + + + + $EW_DIR$\common\plugins\CodeCoverage\CodeCoverage.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\Orti\Orti.ENU.ewplugin + 0 + + + $EW_DIR$\common\plugins\Stack\Stack.ENU.ewplugin + 1 + + + $EW_DIR$\common\plugins\SymList\SymList.ENU.ewplugin + 1 + + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/rf_set.ewp b/Zigbee无线通信/Bæ�¿/Project/rf_set.ewp new file mode 100644 index 0000000..0d03d96 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/rf_set.ewp @@ -0,0 +1,2157 @@ + + + + 2 + + Debug + + 8051 + + 1 + + General + 1 + + 5 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICC8051 + 4 + + 9 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A8051 + 2 + + 5 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + XLINK + 3 + + 17 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + XAR + 1 + + 0 + 1 + 1 + + + + + + + + BILINK + 0 + + + + + Release + + 8051 + + 0 + + General + 1 + + 5 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICC8051 + 4 + + 9 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A8051 + 2 + + 5 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + XLINK + 3 + + 17 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + XAR + 1 + + 0 + 1 + 0 + + + + + + + + BILINK + 0 + + + + + app + + $PROJ_DIR$\rf_set.c + + + + basicrf + + $PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c + + + + board + + $PROJ_DIR$\..\CC2530_LIB\board\hal_board.c + + + $PROJ_DIR$\..\CC2530_lib\board\hal_led.c + + + + common + + $PROJ_DIR$\..\CC2530_lib\common\hal_clock.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_digio.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_int.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf.c + + + $PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c + + + $PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c + + + + sensor_drv + + $PROJ_DIR$\SENSOR_DRV\BMA020.c + + + $PROJ_DIR$\SENSOR_DRV\BMA020.h + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.c + + + $PROJ_DIR$\SENSOR_DRV\dma_itg.h + + + $PROJ_DIR$\SENSOR_DRV\get_adc.c + + + $PROJ_DIR$\SENSOR_DRV\get_adc.h + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.c + + + $PROJ_DIR$\SENSOR_DRV\get_swsensor.h + + + $PROJ_DIR$\SENSOR_DRV\sensor.c + + + $PROJ_DIR$\SENSOR_DRV\sensor.h + + + + utils + + $PROJ_DIR$\..\CC2530_lib\utils\util.c + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/rf_set.eww b/Zigbee无线通信/Bæ�¿/Project/rf_set.eww new file mode 100644 index 0000000..5706dae --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/rf_set.eww @@ -0,0 +1,10 @@ + + + + + $WS_DIR$\rf_set.ewp + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.c new file mode 100644 index 0000000..6f60b91 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.c @@ -0,0 +1,285 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +//#include "hal_types.h" +#include "hal_defs.h" +#include "BMA020.h" + + + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + + +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 + +#define HAL_BOARD_IO_E2PROM_SCL_PORT HAL_BOARD_IO_SHT_SCK_PORT +#define HAL_BOARD_IO_E2PROM_SCL_PIN HAL_BOARD_IO_SHT_SCK_PIN + + +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCL_DIR_OUT HAL_E2PROM_SCK_DIR_OUT +#define HAL_E2PROM_SCL_SET HAL_E2PROM_SCK_SET +#define HAL_E2PROM_SCL_CLR HAL_E2PROM_SCK_CLR + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define halMcuWaitUs McuWaitUs +#define NOP() asm("NOP") +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void McuWaitUs2(uint16 usec); +void McuWaitUs2(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void M2_I2cStart(void); +void M2_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + McuWaitUs2(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cStop(void); +void M2_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + HAL_E2PROM_SCL_SET(); //SCL=1 + McuWaitUs2(2); + + HAL_E2PROM_SDA_CLR(); //SDA=0 + McuWaitUs2(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); //SCL=0 + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cAck(void); +void M2_I2cAck(void)//µ±SDA=0ʱ²úÉúÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SDA_CLR(); + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cNack(void); +void M2_I2cNack(void)//µ±SDA=1ʱ²úÉú·ÇÓ¦´ðÐźŠ+{ + HAL_E2PROM_SCL_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SDA_SET(); + McuWaitUs2(2); + + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); +} + +//------------------------------------------------------------------- +static void M2_I2cWrite(uint8 dat); +void M2_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCL_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + McuWaitUs2(2); + HAL_E2PROM_SCL_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + McuWaitUs2(2); + } +} + +//------------------------------------------------------------------- +static uint8 M2_I2cRead(void); +uint8 M2_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + McuWaitUs2(2); + + for (cnt = 0; cnt < 8; cnt++) + { + HAL_E2PROM_SCL_SET(); + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + McuWaitUs2(2); + HAL_E2PROM_SCL_CLR(); + McuWaitUs2(2); + } + HAL_E2PROM_SDA_DIR_OUT();//cpu»ñÈ¡ÍêÒ»¸ö×Ö½Úºó£¬ÔÙ½«SDA¶Ë¿ÚÉèÖóÉÊä³ö£¬ÇÒÖÃ¸ß´Ë¶Ë¿Ú + return res; +} + +//------------------------------------------------------------------- +// GLOBAL FUNCTIONS +//------------------------------------------------------------------- +static void M2_Init(void); +void M2_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); +} + + +//------------------------------------------------------------------- +static void M2_WriteEEPROM(uint8 addr, uint8 ch); +void M2_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M2_I2cStart(); + + M2_I2cWrite(0x70);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M2_I2cAck(); + + M2_I2cWrite(addr); + M2_I2cAck(); + + M2_I2cWrite(ch); //data + M2_I2cAck(); + + M2_I2cStop(); + // halMcuWaitMs(5); +} + +//------------------------------------------------------------------- +static uint8 M3_ReadEEPROM(uint8 addr); +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M2_I2cStart(); + + M2_I2cWrite(0x70); //Æ÷¼þÉèÖÃΪд״̬ + M2_I2cAck(); + + M2_I2cWrite(addr); + M2_I2cAck(); + + + M2_I2cStart(); + + M2_I2cWrite(0x71); //Æ÷¼þÉèÖÃΪд״̬ + M2_I2cAck(); + + res = M2_I2cRead(); + + M2_I2cNack(); + M2_I2cStop(); + + return res; +} + + +/****************************************************************************** + * Ãû³Æ BMA_Pooling + * ¹¦ÄÜ ¶ÁÈ¡bma020µÄX,Y ZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ + * Èë¿Ú²ÎÊý (int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) + *BMA_X_Value£ºXµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Y_Value£ºYµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Z_Value£ºZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + ¼ÓËÙ¶ÈֵΪ16λÓзûºÅÊý£¬·Ö±æÂÊΪ 2/512 (g), g=9.8m/s + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void BMA_Pooling(int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) +{ + uint16 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + uint16 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + int16 value; + M2_Init(); + BMA_XL_Value = M3_ReadEEPROM(0x02); + BMA_XM_Value = M3_ReadEEPROM(0x03); + BMA_YL_Value = M3_ReadEEPROM(0x04); + BMA_YM_Value = M3_ReadEEPROM(0x05); + BMA_ZL_Value = M3_ReadEEPROM(0x06); + BMA_ZM_Value = M3_ReadEEPROM(0x07); + value = (BMA_XM_Value << 8) | ((BMA_XL_Value&0xc0)); + *BMA_X_Value= value/64; + value = (BMA_YM_Value << 8) | ((BMA_YL_Value&0xc0)); + *BMA_Y_Value= value/64; + value = (BMA_ZM_Value << 8) | ((BMA_ZL_Value&0xc0)); + *BMA_Z_Value= value/64; +} + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.h new file mode 100644 index 0000000..2ac8afc --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/BMA020.h @@ -0,0 +1,29 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned short +#define ulong unsigned long + + +/****************************************************************************** + * Ãû³Æ BMA_Pooling + * ¹¦ÄÜ ¶ÁÈ¡bma020µÄX,Y ZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ + * Èë¿Ú²ÎÊý (int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value) + *BMA_X_Value£ºXµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Y_Value£ºYµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + *BMA_Z_Value£ºZµÄÖØÁ¦¼ÓËÙ¶ÈÖµ´æ·ÅµØÖ· + ¼ÓËÙ¶ÈֵΪ16λÓзûºÅÊý£¬·Ö±æÂÊΪ 2/512 (g), g=9.8m/s + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +extern void BMA_Pooling(int16 *BMA_X_Value,int16 *BMA_Y_Value,int16 *BMA_Z_Value); + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.c new file mode 100644 index 0000000..2c40357 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.c @@ -0,0 +1,292 @@ +//------------------------------------------------------------------- +// Filename: dma_bma.c +// Description: dma_bma module library (I2C) +//------------------------------------------------------------------- +//------------------------------------------------------------------- +// INCLUDES +//------------------------------------------------------------------- +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "dma_itg.h" + + + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + + +/***********аå°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 + +#define HAL_BOARD_IO_E2PROM_SCL_PORT HAL_BOARD_IO_SHT_SCK_PORT +#define HAL_BOARD_IO_E2PROM_SCL_PIN HAL_BOARD_IO_SHT_SCK_PIN + + +#define HAL_E2PROM_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_E2PROM_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_E2PROM_SCL_DIR_OUT HAL_E2PROM_SCK_DIR_OUT +#define HAL_E2PROM_SCL_SET HAL_E2PROM_SCK_SET +#define HAL_E2PROM_SCL_CLR HAL_E2PROM_SCK_CLR + +#define HAL_E2PROM_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_E2PROM_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_E2PROM_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_E2PROM_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) +#define halMcuWaitUs ITGMcuWaitUs +#define NOP() asm("NOP") +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +static void ITGMcuWaitUs(uint16 usec); +void ITGMcuWaitUs(uint16 usec) +{ + usec >>= 1; + while (usec--) + { + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + NOP(); + //NOP(); + } +} +//------------------------------------------------------------------- +// LOCAL FUNCTIONS +//------------------------------------------------------------------- +//--------- write a byte and checks the ack signal----------------- +static void M3_I2cWrite(uint8 dat); +void M3_I2cWrite(uint8 dat)//ÔÚ´«ËÍÊý¾Ýʱ£¬Êý¾ÝÏß±£³ÖÎȶ¨²»ÄÜÓÐÌø±ä +{ + uint8 i; + + for (i = 0; i < 8; i++) + { + if (dat &0x80) + { + HAL_E2PROM_SDA_SET(); + } + else + { + HAL_E2PROM_SDA_CLR(); + } + HAL_E2PROM_SCK_SET();//µ±Ê±ÖÓÏßΪ¸ßµçƽ(SCL=1)ʱ¿ªÊ¼´«ËÍÊý¾Ý + dat = dat << 1; + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR();//´«ËÍÍêºóÊÍ·Å×ÜÏߣ¨SCL=0£© + halMcuWaitUs(2); + } + +} + +//--------- read a byte and checks the ack signal----------------- +static uint8 M3_I2cRead(void); +uint8 M3_I2cRead(void) +{ + uint8 res = 0; + uint8 cnt; + + HAL_E2PROM_SDA_SET();//ÔÚ¶ÁÊý¾Ý֮ǰ£¬°ÑÊý¾ÝÏßÀ­¸ß£¨datesheetÉÏÓнéÉÜ£©,È»ºó°ÑÊý¾ÝÏ߶˿ڶÔCPUÉèÖóÉÊäÈë¶Ë¿Ú + HAL_E2PROM_SDA_DIR_IN(); + halMcuWaitUs(2); + + for (cnt = 0; cnt < 8; cnt++) + { + res <<= 1; + if (HAL_E2PROM_SDA_VAL())//cpu»ñÈ¡SDA¶Ë¿ÚÉϵÄÖµ + { + res |= 0x01; + } + HAL_E2PROM_SCK_SET(); + halMcuWaitUs(2); + HAL_E2PROM_SCK_CLR(); + halMcuWaitUs(2); + } + // MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SDA_PORT, HAL_BOARD_IO_E2PROM_SDA_PIN,!ack); /*µ±ack£½1ʱÀ­µÍÊý¾ÝÏß*/ + // HAL_E2PROM_SDA_SET(); + + HAL_E2PROM_SCK_SET(); /*clk #9 for ack*/ + halMcuWaitUs(1); /*pulswith approx 1 us */ + HAL_E2PROM_SCK_CLR(); + HAL_E2PROM_SDA_SET(); + + return res; +} +static void M3_I2cStart(void); +void M3_I2cStart(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɸ߱äµÍ£¬ÎªÆðʼÐźŠ+{ + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +static void M3_I2cStop(void); +void M3_I2cStop(void)//ÔÚSCL±£³Ö¸ßµçƽÆÚ¼ä£¬SDAÓɵͱä¸ß£¬ÎªÍ£Ö¹ÐźŠ+{ + + HAL_E2PROM_SCK_SET(); //SCL=1 + + HAL_E2PROM_SDA_CLR(); //SDA=0 + halMcuWaitUs(2); + + HAL_E2PROM_SDA_SET(); //SDA=1 + halMcuWaitUs(2); + + HAL_E2PROM_SCK_CLR(); //SCL=0 +} + +//------------------------------------------------------------------- +static void M3_I2cAck(void); +void M3_I2cAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_SET(); + HAL_E2PROM_SCK_CLR(); +} + +//------------------------------------------------------------------- +static void M3_I2cNAck(void); +void M3_I2cNAck(void) +{ + HAL_E2PROM_SCK_SET(); + HAL_E2PROM_SDA_CLR(); + HAL_E2PROM_SCK_CLR(); +} + + +static void M3_WriteEEPROM(uint8 addr, uint8 ch); +void M3_WriteEEPROM(uint8 addr, uint8 ch)//ÍùEEPROMÀïдÊý¾Ý£¨ÏÈдµØÖ·£¬ÔÚдÊý¾Ý£© +{ + M3_I2cStart(); + + M3_I2cWrite(0xD2);//дÆ÷¼þµØÖ·£¬¶ÔÓÚAT24C02À´Ëµ£¬¸ßËÄλ¹Ì¶¨Îª1010,ºóÈýλûÓÐÒâÒ壬×îºóһλΪ0ʱд£¬Îª1ʱ¶Á + M3_I2cAck(); + + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cWrite(ch); //data + M3_I2cAck(); + + M3_I2cStop(); +} +static uint8 M3_ReadEEPROM(uint8 addr); +uint8 M3_ReadEEPROM(uint8 addr) +{ + uint8 res; + + M3_I2cStart(); + M3_I2cWrite(0xD2); //Æ÷¼þÉèÖÃΪд״̬ + M3_I2cAck(); + M3_I2cWrite(addr); + M3_I2cAck(); + + M3_I2cStart(); + M3_I2cWrite(0xD3); //Æ÷¼þÉèÖÃΪ¶Á״̬ + M3_I2cAck(); + + res = M3_I2cRead(); + M3_I2cNAck(); + M3_I2cStop(); + + return res; +} +static void M3_Init(void); +void M3_Init(void)//³õʼ»¯ SDA=1¡¢SCL=1 +{ + static char initflg=0; + HAL_E2PROM_SDA_DIR_OUT(); + MCU_IO_OUTPUT(HAL_BOARD_IO_E2PROM_SCL_PORT, HAL_BOARD_IO_E2PROM_SCL_PIN,1); + if(initflg==0) + { + M3_WriteEEPROM(0x16,0x18); + initflg=1 ; + } +} + +/******************************************************************* + * Ãû³Æ ITG_Pooling + * ¹¦ÄÜ ¶ÁÈ¡itg_3200µÄX,Y ZµÄ½ÇËÙ¶È + * Èë¿Ú²ÎÊý (int16* X_Value, int16* Y_Value, int16* Z_Value ) + *X_Value£ºXÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + *Y_Value£ºYÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + *Z_Value£ºZÖáµÄ16λ½ÇËÙ¶È´æ·ÅµØÖ· + ½ÇËÙ¶ÈֵΪ16λÓзûºÅÊý£¬µ¥Î»Îª ¶È/S + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************/ +void ITG_Pooling(int16* X_Value, int16* Y_Value, int16* Z_Value ) +{ + int8 BMA_XM_Value,BMA_YM_Value,BMA_ZM_Value; + int8 BMA_XL_Value,BMA_YL_Value,BMA_ZL_Value; + int16 num_x1=0,num_y1=0,num_z1=0; + int16 num_x=0,num_y=0,num_z=0; + uint8 i = 0; + M3_Init(); + for(i=0;i<10;i++) + { + while (!(M3_ReadEEPROM(0x1A) & 0x01)); + BMA_XM_Value = M3_ReadEEPROM(0x1D); + halMcuWaitUs(5); + BMA_XL_Value = M3_ReadEEPROM(0x1E); + halMcuWaitUs(5); + BMA_YM_Value = M3_ReadEEPROM(0x1F); + halMcuWaitUs(5); + BMA_YL_Value = M3_ReadEEPROM(0x20); + halMcuWaitUs(5); + BMA_ZM_Value = M3_ReadEEPROM(0x21); + halMcuWaitUs(5); + BMA_ZL_Value = M3_ReadEEPROM(0x22); + halMcuWaitUs(5); + num_x1 = (BMA_XM_Value << 8) | BMA_XL_Value; + num_y1 = (BMA_YM_Value << 8) | BMA_YL_Value; + num_z1 = (BMA_ZM_Value << 8) | BMA_ZL_Value; + num_x = num_x + num_x1; + num_y = num_y + num_y1; + num_z = num_z + num_z1; + } + // »»Ëã³É ¶È/S + *X_Value = (int16)(num_x/10/14.375); + *Y_Value = (int16)(num_y/10/14.375); + *Z_Value = (int16)(num_z/10/14.375); +} + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.h new file mode 100644 index 0000000..8c5c2aa --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/dma_itg.h @@ -0,0 +1,20 @@ +#define ACK 1 +#define noACK 0 +#define measure_temp_H 0x1B +#define measure_temp_L 0x1C +#define measure_X_H 0x1D +#define measure_X_L 0x1E +#define measure_Y_H 0x1F +#define measure_Y_L 0x20 +#define measure_Z_H 0x21 +#define measure_Z_L 0x22 + + +#define uchar unsigned char +#define uint unsigned short +#define ulong unsigned long + + + +extern void ITG_Pooling(int16* X_Value, int16* Y_Value, int16* Z_Value ); + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.c new file mode 100644 index 0000000..f9dde86 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.c @@ -0,0 +1,160 @@ +/************************************************************************************************** + Filename: sh10.c + + Description: sh10 driver + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "get_adc.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ +void hal_adc_Init(void); + uint16 readVoltage(void); +//static volatile uint32 value; + +void hal_adc_Init(void) +{ + APCFG |=1; + P0SEL |= (1 << (0)); + P0DIR &= ~(1 << (0)); + +} +/****************************************************************************** + * Ãû³Æ get_adc + * ¹¦ÄÜ ¶ÁÈ¡A/DÖµ + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý 16λµçѹֵ£¬·Ö±æÂÊΪ10mV, Èç0x0102±íʾ2.58V + *****************************************************************************/ +uint16 get_adc(void) +{ + int32 value; + hal_adc_Init(); // ADC³õʼ»¯ + ADCIF = 0; //ÇåADC ÖжϱêÖ¾ + //²ÉÓûù×¼µçѹavdd5:3.3V£¬Í¨µÀ0£¬Æô¶¯ADת»¯ + ADCCON3 = (0x80 | 0x10 | 0x00); + while ( !ADCIF ) + { + ; //µÈ´ýADת»¯½áÊø + } + value = ADCH; + value = value<< 8; + value |= ADCL; + if(value&0x8000) + value=0; + // ADֵת»¯³Éµçѹֵ + // 0 ±íʾ 0V £¬32768 ±íʾ 3.3V + // µçѹֵ = (value*3.3)/32768 £¨V) + value = (value * 330); + value = value >> 15; // ³ýÒÔ32768 + // ·µ»Ø·Ö±æÂÊΪ0.01VµÄµçѹֵ + return (uint16)value; +} +#define ACH1 0 +#define ACH2 4 +#define ACH3 5 +#define ACH4 6 + +void hal_adc4_Init(void); +void hal_adc4_Init(void) +{ + APCFG |= (1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4); + P0SEL |= (1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4); + P0DIR &= ~((1 << ACH1) | (1 << ACH2)| (1 << ACH3)| (1 << ACH4)); + +} +#define AD4MA 32768*150*4/3300 +#define AD20MA 32768*150*20/3300 +//return 10ADÖµ£¬0---0v, 3ff---3.3v +uint16 adC_ch(uint8 ch); +uint16 adC_ch(uint8 ch) +{ + int32 value; + ADCIF = 0; //ÇåADC ÖжϱêÖ¾ + //²ÉÓûù×¼µçѹavdd5:3.3V£¬Í¨µÀch£¬Æô¶¯ADת»¯ + ADCCON3 = (0x80 | 0x10 | (ch&0x0f) ); + while ( !ADCIF ) + { + ; //µÈ´ýADת»¯½áÊø + } + value = ADCH; + value = value<< 8; + value |= ADCL; + if(value&0x8000) + value=0; + value= value>>5; + return (uint16)value; + /* + // ADֵת»¯³Éµçѹֵ + // 0 ±íʾ 0V £¬32768 ±íʾ 3.3V + // µçÁ÷ 0--4ma, 255--20ma, µç×è150r + value = (value - AD4MA)*255/(AD20MA - AD4MA); + if(value<0) + return 0; + if(value>255) + return 255; + return (uint8)value&0xff;*/ +} +/****************************************************************************** + * Ãû³Æ get_adc4 + * ¹¦ÄÜ ¶ÁÈ¡ËÄͨµÀA/DÖµ ch0 ch4 ch5 ch6 + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý + *****************************************************************************/ +void get_adc4ch(uint8 *buf) +{ + uint16 value; + hal_adc4_Init(); // ADC³õʼ»¯ + value=adC_ch(ACH1); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH2); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH3); + *buf=value; + buf++; + *buf=value>>8; + buf++; + value=adC_ch(ACH4); + *buf=value; + buf++; + *buf=value>>8; +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.h new file mode 100644 index 0000000..17fc182 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_adc.h @@ -0,0 +1,16 @@ +/************************************************************************************************* + Filename: adc.h + + *****/ + + +#ifndef SIMPLE_adc_H +#define SIMPLE_adc_H + + +extern void hal_adc_Init(void); +extern uint16 get_adc(void); +extern void get_adc4ch(uint8 *buf); +#endif + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.c new file mode 100644 index 0000000..7a8a559 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.c @@ -0,0 +1,57 @@ +/************************************************************************************************** + Filename: + + Description: + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +//#include "hal_types.h" +#include "get_swsensor.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ + +/****************************************************************************** + * Ãû³Æ get_swsensor + * ¹¦ÄÜ ¶ÁÈ¡¿ª¹ØÁ¿Öµ + * Èë¿Ú²ÎÊý ÎÞ + * ³ö¿Ú²ÎÊý 0»ò1µçƽ + *****************************************************************************/ +uint8 get_swsensor(void) +{ + P0SEL &= ~( 1 <<1); //ÉèÖÃp0.1ΪÆÕͨi/o¿Ú + P0DIR &= ~( 1 <<1); //ÉèÖÃp0.1ΪÊäÈë¿Ú + return P0_1; //·µ»Øp0.1µçƽ +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.h new file mode 100644 index 0000000..41ef756 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/get_swsensor.h @@ -0,0 +1,13 @@ +/************************************************************************************************* + Filename: + + *****/ + + +#ifndef SIMPLE_swsensor_H +#define SIMPLE_swsensor_H + +extern uint8 get_swsensor(void); +#endif + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.c new file mode 100644 index 0000000..fa93954 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.c @@ -0,0 +1,166 @@ +/************************************************************************************************** + Filename: sensor.c */ + + + +#include "hal_defs.h" +//#include "hal_types.h" +#include "hal_mcu.h" +#include "ioCC2530.h" +//#include "sh10.h" +#include "get_adc.h" +#include "sensor.h" +#include "dma_itg.h" +#include "bma020.h" +//#include "UART_PRINT.h" +//#include "OnBoard.h" + + +/****************************************************************************** + * GLOBAL VARIABLES + */ +void hal_LightSensor_Init(void); +static uint16 readVoltage(void); + +uint16 get_guangdian_ad(void); +void get_4channel_ad(uint8 *buf); +uint8 get_rentihongwai(void); +void get_wendu_shidu(int16 *temp, int16 *hum); +//static volatile uint32 value; + +void hal_LightSensor_Init(void) +{ + APCFG |=1; + P0SEL |= (1 << (0)); + P0DIR &= ~(1 << (0)); + P0INP |=1; +} +/****************************************************************************** + * @fn readVoltage + * + * @brief read voltage from ADC + * + * @param none + * + * @return voltage + *******/ +static uint16 readVoltage(void) +{ + + uint32 value; + int16 value1; + hal_LightSensor_Init(); + // Clear ADC interrupt flag + ADCIF = 0; + + ADCCON3 = (0x80 | 0x10 | 0x00); + //ADCCON3 = (0x80 | 0x10 | 0x0c); + //²ÉÓûù×¼µçѹavdd5:3.3V + // Wait for the conversion to finish + while ( !ADCIF ); + + // Get the result + value1 = ADCH; + value1 = value1<< 8; + value1 |= ADCL; + if(value1<0) + value1=0; + value=value1; + + // value now contains measurement of 3.3V + // 0 indicates 0V and 32767 indicates 3.3V + // voltage = (value*3.3)/32767 volts + // we will multiply by this by 10 to allow units of 0.01 volts + value = value >> 6; // divide first by 2^6 + value = (uint32)(value * 330); + value = value >> 9; // ...and later by 2^9...to prevent overflow during multiplication + + return (uint16)value; + +} + +/****************************************************************************** + * @fn readsensor + * + * @brief read readsensor ´«¸ÐÆ÷²ÉÑù + * + * @param sensorid + * + * @return sensor value, len + *******/ +uint8 readsensor(uint8 sensorid, char *val) +{ + if(sensorid == 0x01) + { //ÎÂʪ¶È + //call_sht11(tmp1,tmp2); + + return 4; + } + if(sensorid == 0x02) + { // G_SENSOR + BMA_Pooling((int16*)val, (int16*)(val+2),(int16*)(val+4)); + return 6; + } + if(sensorid == 0x03) + { //ÍÓÂÝÒÇ + ITG_Pooling((int16*)val, (int16*)(val+2),(int16*)(val+4)); + return 6; + } + if(sensorid == 0x4) + { //ÈËÌå p0.1 + APCFG &= ~(1<<1); + P0SEL &= ~(1 << (1)); + P0DIR &= ~(1 << (1)); + *val=0; + if( P0_1==0 ) + *val=0x01; + return 2; + } + if(sensorid == 0x5) + { //ËÄͨµÀ ad + get_adc4ch(val); + return 8; + } + if(sensorid == 0x6) + { //¹âÕÕ adc »òÆäËû + *(int16*)val=get_adc(); + return 2; + } +} +uint16 get_guangdian_ad(void) +{ + return get_adc(); +} +void get_4channel_ad(uint8 *buf) +{ + get_adc4ch(buf); +} +//1 youren +uint8 get_rentihongwai(void) +{ + uint8 val; + APCFG &= ~(1<<1); + P0SEL &= ~(1 << (1)); + P0DIR &= ~(1 << (1)); + val=0; + if( P0_1==0 ) + val=0x01; + return val; +} + +void get_wendu_shidu(int16 *temp, int16 *hum) +{ + int16 tmp1,tmp2; + call_sht11 (&tmp1,&tmp2); + *temp = tmp1; + *hum = tmp2; +} +//1 dongzuo +void set_relay(uint8 status) +{ + if(status == 1){ + HAL_TURN_ON_DC1(); + }else{ + HAL_TURN_OFF_DC1(); + } +} \ No newline at end of file diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.h new file mode 100644 index 0000000..ac08b19 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sensor.h @@ -0,0 +1,30 @@ +/************************************************************************************************** + Filename: + + +**************************************************************************************************/ + + +#ifndef SENSOR_H +#define SENSOR_H + +/****************************************************************************** + * CONSTANTS + */ + + +/****************************************************************************** + * PUBLIC FUNCTIONS + */ +extern uint8 readsensor(uint8 sensorid, char *val); + +extern uint16 get_guangdian_ad(void); +extern void get_4channel_ad(uint8 *buf); +extern uint8 get_rentihongwai(void); +extern void get_wendu_shidu(int16 *temp, int16 *hum); +extern void set_relay(uint8 status); +#endif // + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.c b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.c new file mode 100644 index 0000000..3dfd53c --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.c @@ -0,0 +1,325 @@ +/************************************************************************************************** + Filename: sh10.c + + Description: sh10 driver + +**************************************************************************************************/ + +/****************************************************************************** + * INCLUDES + */ +#include "ioCC2530.h" +#include "hal_defs.h" +#include "sh10.h" + +#define BM(n) (1 << (n)) + +#define MCU_IO_TRISTATE 1 +#define MCU_IO_PULLUP 2 +#define MCU_IO_PULLDOWN 3 + +#define MCU_IO_OUTPUT(port, pin, val) MCU_IO_OUTPUT_PREP(port, pin, val) +#define MCU_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BM(pin); P##port##_##pin## = val; P##port##DIR |= BM(pin); ) +#define MCU_IO_INPUT(port, pin, func) MCU_IO_INPUT_PREP(port, pin, func) +#define MCU_IO_INPUT_PREP(port, pin, func) st( P##port##SEL &= ~BM(pin); P##port##DIR &= ~BM(pin); switch (func) { case MCU_IO_PULLUP: P##port##INP &= ~BM(pin); P2INP &= ~BM(port + 5); break; case MCU_IO_PULLDOWN: P##port##INP &= ~BM(pin); P2INP |= BM(port + 5); break; default: P##port##INP |= BM(pin); break; } ) +#define MCU_IO_GET(port, pin) MCU_IO_GET_PREP(port, pin) +#define MCU_IO_GET_PREP(port, pin) (P##port## & BM(pin)) + + +#if defined (BOARD_OLD) +/***********¾É°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 2 +#define HAL_BOARD_IO_SHT_SCK_PIN 4 +#define HAL_BOARD_IO_SHT_SDA_PORT 2 +#define HAL_BOARD_IO_SHT_SDA_PIN 3 +#else +/***********аå°åIOÉèÖÃ********************/ +#define HAL_BOARD_IO_SHT_SCK_PORT 1 +#define HAL_BOARD_IO_SHT_SCK_PIN 6 +#define HAL_BOARD_IO_SHT_SDA_PORT 1 +#define HAL_BOARD_IO_SHT_SDA_PIN 5 +#endif + + +#define HAL_SHT_SCK_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) +#define HAL_SHT_SCK_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,1) +#define HAL_SHT_SCK_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SCK_PORT, HAL_BOARD_IO_SHT_SCK_PIN,0) + +#define HAL_SHT_SDA_SET() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_CLR() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,0) +#define HAL_SHT_SDA_DIR_OUT() MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,1) +#define HAL_SHT_SDA_DIR_IN() MCU_IO_INPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,2) +#define HAL_SHT_SDA_VAL() MCU_IO_GET(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN) + + + + +#define ACK 1 +#define noACK 0 +#define measure_temp 0x03 //²âÁ¿Î¶ÈÃüÁî +#define measure_humi 0x05 //²âÁ¿Êª¶ÈÃüÁî +#define RESET 0x1e //ÈíÆô¶¯ + +#define uchar unsigned char +#define uint unsigned int +#define ulong unsigned long + +//------------------------Êý¾Ý½á¹¹Ì嶨Òå--------------------------- +typedef union //±£´æËù²âµÃµÄζȣ¦Êª¶ÈÖµ +{ + uint i; + float f; +} value; +//WENSHIDU S; + + + + + + +/****************************************************************************** + * LOCAL FUNCTIONS + */ + +void SHT11_DELAY(uint16 usec); +char write_byte(unsigned char value); +char read_byte(unsigned char ack); +void transstart(void); +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); +void calc_sth11(float *p_humidity ,float *p_temperature); + + +#define SENSOR_SHT10 + + + + + +#if defined SENSOR_SHT10 + +void SHT11_DELAY(uint16 usec) +{ + usec >>= 1; + + while (usec--) + { + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + } +} + + + + +char write_byte(unsigned char value) +{ + unsigned char i,error=0; + + for (i=0x80;i>0;i/=2) + { + if (i & value) + HAL_SHT_SDA_SET(); + else + HAL_SHT_SDA_CLR(); + + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + } + + HAL_SHT_SDA_SET(); + HAL_SHT_SDA_DIR_IN(); + HAL_SHT_SCK_SET(); + error=HAL_SHT_SDA_VAL(); + HAL_SHT_SCK_CLR(); + + return error; +} + +char read_byte(unsigned char ack) +{ + unsigned char i,val=0; + + HAL_SHT_SDA_SET(); + HAL_SHT_SDA_DIR_IN(); + for (i=0x80;i>0;i/=2) + { + HAL_SHT_SCK_SET(); + if (HAL_SHT_SDA_VAL()) + val=(val | i); + HAL_SHT_SCK_CLR(); + } + MCU_IO_OUTPUT(HAL_BOARD_IO_SHT_SDA_PORT, HAL_BOARD_IO_SHT_SDA_PIN,!ack); + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + HAL_SHT_SDA_SET(); + + return val; +} + +void transstart(void) +{ + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + HAL_SHT_SDA_CLR(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + SHT11_DELAY(2); + HAL_SHT_SCK_SET(); + SHT11_DELAY(1); + + HAL_SHT_SDA_SET(); + SHT11_DELAY(1); + HAL_SHT_SCK_CLR(); + SHT11_DELAY(2); + HAL_SHT_SDA_CLR(); +} + +void connectionreset(void) +{ + unsigned char i; + HAL_SHT_SDA_SET(); + HAL_SHT_SCK_CLR(); + for(i=0;i<9;i++) + { + HAL_SHT_SCK_SET(); + HAL_SHT_SCK_CLR(); + } + transstart(); +} + +char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) +{ + unsigned char error=0; + unsigned int i; + + connectionreset(); + switch(mode) + { + case 0 : error+=write_byte(measure_temp); break; + case 1 : error+=write_byte(measure_humi); break; + default : break; + } + + HAL_SHT_SDA_DIR_IN(); + for (i=0;i<65535;i++) + { + SHT11_DELAY(10); + if(HAL_SHT_SDA_VAL()== 0) break; + } + if(HAL_SHT_SDA_VAL()) + { + error+=1; + } + + *(p_value+1) =read_byte(ACK); + *(p_value)=read_byte(ACK); + *p_checksum =read_byte(noACK); + return error; +} +/****************************************************************************** + * Ãû³Æ calc_sth11 + * ¹¦ÄÜ ¼ÆËãÎÂʪ¶ÈÖµ + * Èë¿Ú²ÎÊý float *p_humidity ,float *p_temperature + ʪ¶È ÎÂ¶È + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void calc_sth11(float *p_humidity ,float *p_temperature) +{ + const float C1=-4.0; + const float C2=+0.0405; + const float C3=-0.0000028; + const float T1=+0.01; + const float T2=+0.00008; + float rh=*p_humidity; + float t=*p_temperature; + float rh_lin; + float rh_true; + float t_C; + t_C=t*0.01 - 40; + rh_lin=C3*rh*rh + C2*rh + C1; + rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; + if(rh_true>100)rh_true=100; + if(rh_true<0.1)rh_true=0.1; + *p_temperature=t_C; + *p_humidity=rh_true; +} + +/****************************************************************************** + * Ãû³Æ call_sht11 + * ¹¦ÄÜ ¶ÁÈ¡SH1xÎÂʪ¶ÈÖµ + * Èë¿Ú²ÎÊý void call_sht11(unsigned int *tem_val,unsigned int *hum_val) + *tem_val:ζȴæ·ÅµØÖ·£¬0.1¶È·Ö±æÂÊ + *hum_val:ʪ¶È´æ·ÅµØÖ·£¬0.1·Ö±æÂÊ + * ³ö¿Ú²ÎÊý ÎÞ + *****************************************************************************/ +void call_sht11(unsigned int *tem_val,unsigned int *hum_val) +{ + value humi_val,temp_val; + unsigned char error = 0,checksum; + *tem_val=0; + *hum_val=0; + //Æô¶¯SH1xµÄʪ¶È²âÁ¿ + error+=s_measure((unsigned char*) &humi_val.i,&checksum,1); + //Æô¶¯SH1xµÄζȲâÁ¿ + error+=s_measure((unsigned char*) &temp_val.i,&checksum,0); + if(error!=0) + { //²âÊÔ´íÎ󣬽øÐÐÈí¸´Î» + connectionreset(); + } + else + { //²âÊÔÊý¾Ý´¦Àí + humi_val.f=(float)humi_val.i; + temp_val.f=(float)temp_val.i; + //¼ÆËãÎÂʪ¶Èʵ¼ÊÖµ + calc_sth11(&humi_val.f,&temp_val.f); + //ת»¯³É0.1µÄ·Ö±æÂÊ + *tem_val=temp_val.f*10; + *hum_val=humi_val.f*10; + } +} + +#endif + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.h b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.h new file mode 100644 index 0000000..c7f6f99 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/sensor_drv/sh10.h @@ -0,0 +1,14 @@ +/************************************************************************************************* + Filename: sh10.h + + *****/ + + +#ifndef SIMPLE_SH10_H +#define SIMPLE_SH10_H + +extern void call_sht11(unsigned int *tem, unsigned int *hum); +extern void connectionreset(void); +#endif + + diff --git a/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.cspy.bat b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.cspy.bat new file mode 100644 index 0000000..4b0b9ff --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.cspy.bat @@ -0,0 +1,15 @@ +@REM This batch file has been generated by the IAR Embedded Workbench +@REM C-SPY Debugger, as an aid to preparing a command line for running +@REM the cspybat command line utility using the appropriate settings. +@REM +@REM You can launch cspybat by typing the name of this batch file followed +@REM by the name of the debug file (usually an ELF/DWARF or UBROF file). +@REM Note that this file is generated every time a new debug session +@REM is initialized, so you may want to move or rename the file before +@REM making changes. +@REM + + +"D:\IAR Systems\Embedded Workbench 6.0\common\bin\cspybat" "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051proc.dll" "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051emu_cc.dll" %1 --plugin "D:\IAR Systems\Embedded Workbench 6.0\8051\bin\8051bat.dll" --backend -B "--proc_core" "plain" "--proc_code_model" "banked" "--proc_nr_virtual_regs" "8" "--proc_pdata_bank_reg_addr" "0x93" "--proc_dptr_nr_of" "1" "--proc_codebank_reg" "0x9F" "--proc_codebank_start" "0x8000" "--proc_codebank_end" "0xFFFF" "--proc_codebank_mask" "0xFF" "--proc_data_model" "large" "-p" "D:\IAR Systems\Embedded Workbench 6.0\8051\config\devices\Texas Instruments\ioCC2530F256.ddf" "--proc_exclude_exit_breakpoint" "--proc_driver" "chipcon" "--retain_memory" "--verify_download" "use_crc16" "--stack_overflow" "--number_of_banks" "4" + + diff --git a/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dbgdt b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dbgdt new file mode 100644 index 0000000..6da1fa6 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dbgdt @@ -0,0 +1,205 @@ + + + + + + + 20 + 1861 + + + 20 + 1395 + 372 + 93 + + + + 124 + 27 + 27 + 27 + + + + 1 + 1 + 1 + + + + + + + TabID-30582-27334 + Debug Log + Debug-Log + + + + TabID-30060-27344 + Build + Build + + + + 0 + + + + + TabID-8563-27337 + Workspace + Workspace + + + rf_set + + + + + 0 + + + + + TabID-19311-27341 + Disassembly + Disassembly + + + + 0 + + + + + + TextEditor + $WS_DIR$\rf_set.c + 0 + 26 + 707 + 707 + + 0 + + TextEditor + $WS_DIR$\..\CC2530_lib\basicrf\basic_rf.c + 0 + 435 + 16120 + 16134 + + + TextEditor + $WS_DIR$\..\CC2530_lib\common\hal_mcu.c + 0 + 36 + 2227 + 2239 + + + 0 + + + 1000000 + 1000000 + + + 1 + + + + + + + iaridepm.enu1 + + + + + + + debuggergui.enu1 + + + + + + + + + + + + + -2 + -2 + 730 + 198 + -2 + -2 + 200 + 200 + 104167 + 200602 + 104167 + 734203 + + + + + + + + + + + -2 + -2 + 730 + 649 + -2 + -2 + 200 + 200 + 104167 + 200602 + 339063 + 734203 + + + + + + + + + + + -2 + -2 + 198 + 1922 + -2 + -2 + 1924 + 200 + 1002083 + 200602 + 104167 + 200602 + + + + + + + + + + + + + diff --git a/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dni b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dni new file mode 100644 index 0000000..44cb120 --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.dni @@ -0,0 +1,34 @@ +[DebugChecksum] +Checksum=466055467 +[DisAssemblyWindow] +NumStates=_ 1 +State 1=_ 1 +[InstructionProfiling] +Enabled=_ 0 +[CodeCoverage] +Enabled=_ 0 +[StackPlugin] +Enabled=1 +OverflowWarningsEnabled=1 +WarningThreshold=90 +SpWarningsEnabled=1 +WarnHow=0 +UseTrigger=1 +TriggerName=main +LimitSize=0 +ByteLimit=50 +[Log file] +LoggingEnabled=_ 0 +LogFile=_ "" +Category=_ 0 +[TermIOLog] +LoggingEnabled=_ 0 +LogFile=_ "" +[Breakpoints] +Count=0 +[ChipconEmu] +Stop timers on halt=0 +Leave target running=1 +[Aliases] +Count=0 +SuppressDialog=0 diff --git a/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.wsdt b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.wsdt new file mode 100644 index 0000000..c192c2e --- /dev/null +++ b/Zigbee无线通信/Bæ�¿/Project/settings/rf_set.wsdt @@ -0,0 +1,49 @@ + + + + + + rf_set/Debug + + + + + + + + + 149272727 + + 2098026165 + + + + + + TabID-1552-11148 + Workspace + Workspace + + + rf_setrf_set/Outputrf_set/apprf_set/basicrfrf_set/boardrf_set/commonrf_set/sensor_drv + + + + 0TabID-5639-8760BuildBuild0 + + + + + + TextEditor$WS_DIR$\rf_set.c01535715710TextEditor$WS_DIR$\..\CC2530_lib\basicrf\basic_rf.c04351612016134TextEditor$WS_DIR$\..\CC2530_lib\common\hal_mcu.c036222722390100000010000001 + + + + + + + iaridepm.enu1-2-2545240-2-213115085286194049157552707633-2-21831538-2-215401851002604239327111979239327 + + + + diff --git a/Zigbee无线通信/任务è¦�求.png b/Zigbee无线通信/任务è¦�求.png new file mode 100644 index 0000000..7728284 Binary files /dev/null and b/Zigbee无线通信/任务è¦�求.png differ