2021-10-04

This commit is contained in:
Asryn 2021-10-04 22:26:10 +08:00
parent 2082094992
commit 310543de71
222 changed files with 23571 additions and 0 deletions

View File

@ -14,6 +14,7 @@
* 08. task008 对SW1进行上锁/解锁功能:通过判断SW1点击状态进行LED闪烁或者SW1上锁/解锁。
* 09. homework 点击SW1改变LED状态
* 10. task010 一阶段考核:通过SW1对流水灯进行操作,当按下SW1时暂停流水灯并保持当前状态,当再次按下时在上次保存状态开始继续向下执行。
* 11. Zigbee无线通信 A、B两板相互控制LED

View File

@ -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 <string.h>
//-------------------------------------------------------------------
// 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();
}

View File

@ -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);

View File

@ -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

View File

@ -0,0 +1,8 @@
//-------------------------------------------------------------------
// Filename: basic_rf_security.h
// Description: Basic RF security library header file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
void basicRfSecurityInit(basicRfCfg_t *pConfig);

View File

@ -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();
}

View File

@ -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);

View File

@ -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;
}
}
//-------------------------------------------------------------------

View File

@ -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);

View File

@ -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;
}
}

View File

@ -0,0 +1,4 @@
void Timer4_Init(void);
void Timer4_On(void);
void Timer4_Off(void);
uint8 GetSendDataFlag(void);

View File

@ -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;
}

View File

@ -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);

View File

@ -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; )

View File

@ -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;
}

View File

@ -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);

View File

@ -0,0 +1,105 @@
//-------------------------------------------------------------------
// Filename: hal_defs.h
// Description: HAL defines
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// INCLUDES
//-------------------------------------------------------------------
#include <ioCC2530.h>
//-------------------------------------------------------------------
// 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)

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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
}

View File

@ -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);

View File

@ -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);
}
//-------------------------------------------------------------------

View File

@ -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);

View File

@ -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 <string.h>
//-------------------------------------------------------------------
// 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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -0,0 +1,10 @@
//-------------------------------------------------------------------
// Filename: hal_util_rf.h
// Description:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// PUBLIC FUNCTIONS
//-------------------------------------------------------------------
int8 halSampleED(uint8 channel, uint16 sampleTime);
void halSetRxScanMode(void);

View File

@ -0,0 +1,41 @@
#include <hal_sleep.h>
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;
}

View File

@ -0,0 +1,10 @@
#include <ioCC2530.h>
#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);

View File

@ -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)();
}
}

View File

@ -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);

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}
}
//-------------------------------------------------------------------

View File

@ -0,0 +1,8 @@
//-------------------------------------------------------------------
// Filename: hal_led.h
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
void halDcStart(uint8 dc_num);
void halDcStop(uint8 dc_num);

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
void dma_tgs_Init(void);
uint16 dma_tgs_GetValue(void);

View File

@ -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;
}

View File

@ -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);

View File

@ -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 [癈]
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
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 [癈]
*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);
}
}

View File

@ -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;
}

View File

@ -0,0 +1,8 @@
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
void TC72_Init(void);
uint16 TC72_ReadTemp(void);
void TC72_Enable(void);

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
void dma_tgs_Init(void);
uint16 dma_tgs_GetValue(void);

View File

@ -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;
}

View File

@ -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);

View File

@ -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<time;cntr1++)
{
for(cntr2=0;cntr2<30000;cntr2++);
}
}

View File

@ -0,0 +1,17 @@
#ifndef _FLASH_DATA_H
#define _FLASH_DATA_H
// Function prototype for the multiple words write function
extern void writeWordM( uint8 pg, uint16 offset, uint8 *buf, uint16 cnt );
// Function prototype for the word write function
extern void writeWord( uint8 pg, uint16 offset, uint8 *buf );
// Function prototype for the buffer write function
extern void writeBuf( uint8 dstPg, uint16 dstOff, uint16 len, uint8 *buf );
// Function prototype for delay Function
void delay(int time );
#endif

View File

@ -0,0 +1,59 @@
#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
SEND_DATA_FLAG = 0;
}
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 == 1953) //定时2s,翻转一次(1/250000*256)这是进一次中断所需要的时间,进781次中断大约是0.8s
{
NUM = 0;
SEND_DATA_FLAG = 1;
//halLedToggle(1);
Timer4_Off();
}
else
{
SEND_DATA_FLAG = 0;
}
}

View File

@ -0,0 +1,4 @@
void Timer4_Init(void);
void Timer4_On(void);
void Timer4_Off(void);
uint8 GetSendDataFlag(void);

View File

@ -0,0 +1,112 @@
#include "hal_defs.h"
#include "hal_uart.h"
#include "hal_board.h"
#include "UART_PRINT.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
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;
}

View File

@ -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

View File

@ -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<len; i++)
{
dst[dststart+i] = src[srcstart+i];
}
}
/**********************************************************
参数:
1. *uRxData -- 串口0接收数据所存放的地方
2. uRxlen -- 串口0接收数据的初始长度(一般为0)
***********************************************************/
uint16 RecvUartData(uint8 *uRxData, uint16 uRxlen)
{
uint16 r_UartLen = 0;
uint8 r_UartBuf[128];
r_UartLen = halUartRxLen();
halMcuWaitMs(5);
while(r_UartLen > 0)
{
r_UartLen = halUartRead(r_UartBuf, sizeof(r_UartBuf));
MyByteCopy(uRxData, uRxlen, r_UartBuf, 0, r_UartLen);
uRxlen += r_UartLen;
halMcuWaitMs(5); //这里的延迟非常重要,因为这是串口连续读取数据时候需要有一定的时间间隔
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); //这里的延迟非常重要,因为这是串口连续读取数据时候需要有一定的时间间隔
r_Uart1Len = halUart1RxLen();
}
return uRxlen;
}

View File

@ -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);

Binary file not shown.

View File

@ -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;
}
}

View File

@ -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);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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 <stdio.h>
#include <string.h>
#include <stdarg.h>
/*****点对点通讯地址设置****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();
}
}
}

View File

@ -0,0 +1,524 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<project>
<fileVersion>2</fileVersion>
<fileChecksum>37149274</fileChecksum>
<configuration>
<name>Debug</name>
<outputs>
<file>$PROJ_DIR$\Debug\Obj\sensor.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\rf_set.pbi</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_digio.h</file>
<file>$PROJ_DIR$\Debug\Obj\hal_board.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_cc8051.h</file>
<file>$PROJ_DIR$\Debug\Obj\hal_rf_security.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\hal_led.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\get_adc.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\basic_rf.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\hal_rf_security.r51</file>
<file>$TOOLKIT_DIR$\inc\clib\stdio.h</file>
<file>$PROJ_DIR$\Debug\Obj\hal_clock.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_mcu.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\utils\util.h</file>
<file>$TOOLKIT_DIR$\inc\clib\sysmac.h</file>
<file>$PROJ_DIR$\Debug\Obj\basic_rf.r51</file>
<file>$PROJ_DIR$\Debug\Obj\get_adc.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_digio.pbi</file>
<file>$TOOLKIT_DIR$\inc\ioCC2530.h</file>
<file>$PROJ_DIR$\Debug\Obj\get_swsensor.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.h</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_int.h</file>
<file>$PROJ_DIR$\Debug\Obj\hal_led.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_mcu.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\dma_itg.r51</file>
<file>$PROJ_DIR$\Debug\Obj\BMA020.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\board\hal_led.h</file>
<file>$PROJ_DIR$\Debug\Obj\get_swsensor.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\rf_set.r51</file>
<file>$PROJ_DIR$\Debug\Obj\util.pbi</file>
<file>$PROJ_DIR$\..\CC2530_lib\board\hal_board.h</file>
<file>$PROJ_DIR$\Debug\Obj\util.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_clock.pbi</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_mcu.h</file>
<file>$TOOLKIT_DIR$\config\devices\Texas Instruments\lnk51ew_CC2530F256_banked.xcl</file>
<file>$PROJ_DIR$\Debug\Obj\hal_digio.r51</file>
<file>$PROJ_DIR$\..\CC2530_LIB\common\hal_uart.h</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.h</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_rf.h</file>
<file>$TOOLKIT_DIR$\inc\clib\stdarg.h</file>
<file>$PROJ_DIR$\Debug\Obj\sensor.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_board.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\hal_rf.r51</file>
<file>$PROJ_DIR$\Debug\Obj\hal_int.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_clock.h</file>
<file>$PROJ_DIR$\Debug\Obj\dma_itg.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\hal_uart.r51</file>
<file>$TOOLKIT_DIR$\inc\clib\string.h</file>
<file>$PROJ_DIR$\Debug\Obj\hal_rf.pbi</file>
<file>$PROJ_DIR$\Debug\Exe\send.hex</file>
<file>$PROJ_DIR$\Debug\Obj\hal_uart.pbi</file>
<file>$PROJ_DIR$\Debug\Exe\rf_set.d51</file>
<file>$TOOLKIT_DIR$\lib\clib\cl-pli-blxd-1e16x01.r51</file>
<file>$PROJ_DIR$\..\CC2530_lib\utils\util.c</file>
<file>$PROJ_DIR$\Debug\Obj\BMA020.pbi</file>
<file>$PROJ_DIR$\Debug\Obj\rf_set.pbd</file>
<file>$PROJ_DIR$\Debug\Obj\hal_int.pbi</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_defs.h</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_digio.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_rf.c</file>
<file>$PROJ_DIR$\rf_set.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_int.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\board\hal_led.c</file>
<file>$PROJ_DIR$\..\CC2530_LIB\board\hal_board.c</file>
<file>$PROJ_DIR$\..\CC2530_lib\common\hal_clock.c</file>
<file>$PROJ_DIR$\SENSOR_DRV\sensor.c</file>
<file>$PROJ_DIR$\SENSOR_DRV\BMA020.c</file>
<file>$PROJ_DIR$\SENSOR_DRV\get_adc.h</file>
<file>$PROJ_DIR$\SENSOR_DRV\get_swsensor.h</file>
<file>$PROJ_DIR$\SENSOR_DRV\BMA020.h</file>
<file>$PROJ_DIR$\SENSOR_DRV\dma_itg.h</file>
<file>$PROJ_DIR$\SENSOR_DRV\sensor.h</file>
<file>$PROJ_DIR$\SENSOR_DRV\dma_itg.c</file>
<file>$PROJ_DIR$\SENSOR_DRV\get_adc.c</file>
<file>$PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c</file>
<file>$PROJ_DIR$\SENSOR_DRV\get_swsensor.c</file>
</outputs>
<file>
<name>[ROOT_NODE]</name>
<outputs>
<tool>
<name>XLINK</name>
<file> 49 51</file>
</tool>
</outputs>
</file>
<file>
<name>$PROJ_DIR$\Debug\Exe\rf_set.d51</name>
<outputs>
<tool>
<name>XLINK</name>
<file> 49</file>
</tool>
</outputs>
<inputs>
<tool>
<name>XLINK</name>
<file> 34 25 15 24 16 19 3 11 35 43 22 12 42 9 46 28 40 31 52</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\utils\util.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 29</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 31</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 13 38</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 13 38</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\Debug\Obj\rf_set.pbd</name>
<inputs>
<tool>
<name>BILINK</name>
<file> 54 8 45 7 27 41 32 17 56 6 23 48 5 50 1 0 29</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_digio.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 17</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 35</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 21 2</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 21 2</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_rf.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 48</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 42</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 33 21 13 38</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 33 21 13 38</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\rf_set.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 1</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 28</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 4 21 33 30 26 38 20 36 74 10 39 14 47</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 4 21 33 30 26 38 20 36 74 10 39 14 47</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\basicrf\basic_rf.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 8</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 15</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 21 33 38 20 47 14</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 21 33 38 20 47 14</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_rf_security.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 5</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 9</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 33 38 37 13 47 14</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 33 38 37 13 47 14</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_int.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 56</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 43</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 21</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 21</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_mcu.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 23</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 12</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 44 33 30</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 44 33 30</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\board\hal_led.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 6</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 22</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 4 33 30 26</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 4 33 30 26</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_LIB\board\hal_board.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 41</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 3</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 4 33 2 21 30 36</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 4 33 2 21 30 36</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_lib\common\hal_clock.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 32</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 11</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 44</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 44</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\SENSOR_DRV\sensor.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 0</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 40</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 33 70 74 73 72</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 33 70 74 73 72</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\SENSOR_DRV\BMA020.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 54</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 25</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 18 57 72</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 18 57 72</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\SENSOR_DRV\dma_itg.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 45</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 24</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 18 57 73</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 18 57 73</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\SENSOR_DRV\get_adc.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 7</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 16</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 18 57 70</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 18 57 70</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\..\CC2530_LIB\common\hal_uart.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 50</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 46</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 57 18 30 36</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 57 18 30 36</file>
</tool>
</inputs>
</file>
<file>
<name>$PROJ_DIR$\SENSOR_DRV\get_swsensor.c</name>
<outputs>
<tool>
<name>BICOMP</name>
<file> 27</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 19</file>
</tool>
</outputs>
<inputs>
<tool>
<name>BICOMP</name>
<file> 18 57 71</file>
</tool>
<tool>
<name>ICC8051</name>
<file> 18 57 71</file>
</tool>
</inputs>
</file>
</configuration>
<configuration>
<name>Release</name>
<outputs/>
<forcedrebuild>
<name>[MULTI_TOOL]</name>
<tool>XLINK</tool>
</forcedrebuild>
</configuration>
</project>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<workspace>
<project>
<path>$WS_DIR$\rf_set.ewp</path>
</project>
<batchBuild/>
</workspace>

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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 );

View File

@ -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;
}

Some files were not shown because too many files have changed in this diff Show More