📄 hal.h
字号:
// multi-conversion, stopping mode) or reset (in multi-conversion,
// reset-generating mode).
//
// Return value:
// void
//----------------------------------------------------------------------------
void halConfigADC(byte options, word clkFreq, byte threshold);
// Option specifies operational mode and voltage reference source and must be
// one of the ADC_MODE_x and one of the ADC_REFERENCE_x constants below:
#define ADC_MODE_SINGLE 0x00 // Take single sample
#define ADC_MODE_MULTI_CONTINUOUS 0x10 // Continuosly take samples
#define ADC_MODE_MULTI_STOPPING 0x20 // Continuosly take samples and
// stop when value > threshold
#define ADC_MODE_MULTI_RESETTING 0x30 // Continuosly take samples and
// reset when value > threshold
#define ADC_INTERRUPT_ENABLE 0x80 // Enable ADC interrupts
#define ADC_REFERENCE_VDD 0x00 // Use VDD as reference voltage
#define ADC_REFERENCE_INTERNAL_1_25 0x08 // Internal 1.25 V reference
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Macro for selecting ADC input source
#define ADC_SELECT_INPUT(input) (ADCON=(ADCON&~0x03)|(input))
// Where _input_ is one of:
#define ADC_INPUT_AD0 0
#define ADC_INPUT_AD1 1
#define ADC_INPUT_AD2 2
#define ADC_INPUT_AD0_WITH_AD1_REF 3 // Input is AD0, AD1 is used as a
// reference, the ADC_REFERENCE_x
// supplied to halConfigADC(...)
// is ignored
//----------------------------------------------------------------------------
// Macro for powering up/down the ADC
#define ADC_POWER(bool) (ADCON=((bool) ? ADCON&~0x80 : ADCON|0x80))
// Macro for starting/stopping the ADC when in a multi-conversion mode
#define ADC_RUN(bool) (ADCON=((bool) ? ADCON|0x04 : ADCON&~0x04))
// Macro for taking a single sample in single-conversion mode
#define ADC_SAMPLE_SINGLE() do { ADCON|=0x04; while (ADCON&0x04); } while (0)
// Macro for obtaining the latest sample value (all 10 bits)
#define ADC_GET_SAMPLE_10BIT() ( (ADDATH<<8) | (ADDATL<<0) )
// Macro for obtaining the latest sample value (8 MSB)
#define ADC_GET_SAMPLE_8BIT() ( (ADDATH*64) | (ADDATL/4) )
//----------------------------------------------------------------------------
// word halReadPotSensor(...)
//
// Description:
// This function reads the CC1010EB potentiometer sensor from AD0 pin.
// Arguments:
// void
// Return value:
// word
// 10 bit sample from AD0
//----------------------------------------------------------------------------
word halReadPotSensor(void);
//----------------------------------------------------------------------------
// word halReadTempSensor(...)
//
// Description:
// This function reads the CC1010EM temperature sensor from AD1 pin.
// Arguments:
// void
// Return value:
// word
// 10 bit sample from AD1
//----------------------------------------------------------------------------
word halReadTempSensor(void);
/*****************************************************************************
*****************************************************************************
************* DES macros/functions *************
*****************************************************************************
****************************************************************************/
//----------------------------------------------------------------------------
// byte* halDES(...)
//
// Description:
// This function performs DES encryption/decryption on a block of data.
// The encryption/decryption operations are performed in place (i.e.
// the plaintext is overwritten by the ciphertext or vice versa) on
// suitably aligned data [ address(_buffer_) mod 8 = 0 ]. _key_ should
// point to the key used for single-DES operations or keys in the case
// of triple-DES.
// Two modes of the DES standard are supported: 8-bit Cipher Feedback
// (CFB) and Output Feedback (OFB.) CFB is self-synchronizing (the end of
// a ciphertext can be decrypted even though the beginning is unavailable)
// and can be used to calculate error check codes. OFB is not self-
// synchronizing and is not suitable for error check code calculation,
// but does have the favourable property that a single bit error in the
// received ciphertext produces only a single bit error in the decrypted
// plaintext.
// In CFB/OFB mode an initialization vector of 8 bytes is part of the
// algorithm. This vector must be identical for the encryption and
// decryption process. The choice of initialization data does not affect
// the security of the encryption in any way, and this function thus uses
// the value 0.
// _option_ is used to chose between these modes of operation, between
// single-DES and triple-DES, and between decryption and decryption.
// This function does not return until all data has been encrypted/
// decrypted, since the DES hardware is so fast. The DES hardware can
// run at the same time as the 8051 and generate an intterrupt when
// finished. If this is desired the hardware must be programmed directly.
//
// Arguments:
// byte options
// One or more of the below defined constants define the desired
// operational mode.
// byte xdata* key
// A pointer to a key (or three keys for triple-DES) stored in
// XDATA memory space. This address must be divisible by eight, i.e.
// address(_key_) mod 8 = 0. The 56 active bits of a DES-key are
// expected to be in a compressed 7-byte format, in which all parity
// bits are removed. In the case of a single key, the 56 bits of the key
// must lie on _key_[0] - _key_[6], in big-endian order. In the case of
// three keys (triple-des), the three keys must lie on _key_[0] -
// _key_[6], _key_[8] - _key_[14] and _key_[16] - _key_[22], all in big-
// endian order. The macro DES_NORMAL_2_COMPACT_KEY(...) can be used
// to convert a regular DES key to the compact form. A key can be
// generated by simply using 7 random bytes.
// byte xdata* buffer
// Pointer to the data to encrypt/decrypt in XDATA memory space.
// Encryption/decryption is performed in-place, i.e. the original
// plaintext/ciphertext is overwritten. The address of _buffer_ must
// be divisible by eight, i.e. address(_buffer_) mod 8 = 0.
// word length
// The number of bytes to perform the encryption/decryption on.
//
// Return value:
// byte*
// A pointer to the start of _buffer_ is returned.
//----------------------------------------------------------------------------
byte* halDES(byte options, byte xdata* buffer, byte xdata* key, word length);
// option is one of each of:
#define DES_SINGLE_DES 0
#define DES_TRIPLE_DES 2
#define DES_ENCRYPT 0
#define DES_DECRYPT 4
#define DES_OFB_MODE 0
#define DES_CFB_MODE 8
//----------------------------------------------------------------------------
// Macro used to convert between a normal 64-bit DES-key and the
// 7-byte compressed format used by CC1010. (only useful for
// constant declarations.) Use example:
// byte my_triple_keys[]= {
// DES_NORMAL_2_COMPACT_KEY(0xCD,0x64,0x74,0x03,0xbc,0x90,0xc4,0xc4), 0,
// DES_NORMAL_2_COMPACT_KEY(0xDA,0xCA,0x26,0x83,0x30,0x98,0x8A,0x7D), 0,
// DES_NORMAL_2_COMPACT_KEY(0x37,0x1E,0xC6,0x0F,0x26,0xc3,0xB2,0x9A)
// };
#define DES_NORMAL_2_COMPACT_KEY(k7, k6, k5, k4, k3, k2, k1, k0) \
(((k7)&0xFE)<<0)|(((k6)&0x80)>>7), \
(((k6)&0x7E)<<1)|(((k5)&0xC0)>>6), \
(((k5)&0x3E)<<2)|(((k4)&0xE0)>>5), \
(((k4)&0x1E)<<3)|(((k3)&0xF0)>>4), \
(((k3)&0x0E)<<4)|(((k2)&0xF8)>>3), \
(((k2)&0x06)<<5)|(((k1)&0xFC)>>2), \
(((k1)&0x02)<<6)|(((k0)&0xFE)>>1)
/*****************************************************************************
*****************************************************************************
************ Random number generator macros/functions *************
*****************************************************************************
****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
// The CC1010 has a REAL random number generator (RNG) which uses the 140 dB
// or so of gain in the receiver path to amplify thermal noise on the shorted
// differential input of the mixer. The resulting bitstream can be used
// directly as random numbers or used to seed a pseudo-random number
// generator (PRNG), for instance the srand()/rand() functions in stdlib,
// using the function below.
//////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
// halRandomNumberGen(...);
//
// Description:
// This function activates the true RNG in the CC1010, waits long enough
// for the output to be truly random and then samples individual random
// bits with a period which ensures that the output is sufficiently
// random. This function must never be used at the same time as RF is
// in use.
// A total of _length_ bytes of random data is stored at the location
// pointed to by _rnd_data_.
//
// Arguments:
// byte* rnd_data
// A pointer to a buffer to receive the random bytes.
// word length
// The number of random bytes to generate.
//
// Return value:
// void
//----------------------------------------------------------------------------
void halRandomNumberGen(byte* rnd_data, word length);
/*****************************************************************************
*****************************************************************************
************* RF communication functions/macros *************
*****************************************************************************
****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
// The CC1010's on-chip radio transceiver requires that a lot of parameters
// are set and that a calibration procedure is performed before RF is used.
// The way this is abstracted in the HAL is through the notion of
// RF RX/TX Settings Pairs -- a data structure called RF_RXTXPAIR_SETTINGS
// stores all the required settings for one RX channel and one TX channel as
// a pair. This data structure can be exported from the application Chipcon
// SmartRF Studio which allows easy configuration of such a pair.
// Each such pair to be used (normally just one, unless in a multichannel
// application) must be passed in a call to the function halRFCalib(...)
// which performs the abovementioned calibration and writes the results into
// a data structure called RF_RXTXPAIR_CALDATA.
//
// When an RX/TX-pair is to be used, the function halRFSetRxTxOff(...) should
// be called passing the corresponding RF_RXTXPAIR_SETTINGS and
// RF_RXTXPAIR_CALDATA data structures and the desired mode (RF_RX, RF_TX,
// RF_OFF.) This functions configures the RF parameters, powers up different
// RF modules and then finally enables the desired mode (RX/TX.)
//
// Two functions which allow for easy reception/transmission of radio packets
// are halRFReceivePacket(...)/halRFSendPacket(...). Alternatively user-defined
// receive/send routines can be made in either a polled loop or in an
// interrupt service routine. Several macros are defined that makes this
// easier.
//
// The dataformat (NRZ/Manchester), baudrate (600 - 76.8k) and TX output power
// can be configured to override whatever the current RX/TX-pair is using by
// calling the functions halRFOverrideDataFormat(...),
// halRFOverrideBaudRate(...) and halRFOverrideOutputPower(...) either before
// or after the call to halRFSetRxTxOff(...).
//
// After having received/transmitted the required data RF can be turned off
// using halRFSetRxTxOff(...) passing the parameter RF_OFF.
//
// Below is an example of how use two RX/TX-pairs in a program and how to
// transmit and send with both.
//
//
/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -