Upload files to "/"
This commit is contained in:
parent
19c4c00ce6
commit
aa0b4d208c
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,241 @@
|
||||||
|
/*****************************************************************
|
||||||
|
CC1101.c
|
||||||
|
|
||||||
|
This file is used by all AT200 modules so send data to the CC1100
|
||||||
|
|
||||||
|
|
||||||
|
These routines will read and write the CC1100
|
||||||
|
*
|
||||||
|
* V2 - 0927-18
|
||||||
|
* Removed all watchdog resets and created a new counter
|
||||||
|
* for the SO wait function.
|
||||||
|
* This version seems to not have a lockup issue. If the radio
|
||||||
|
* stalls, the watchdog will time out
|
||||||
|
|
||||||
|
Last change: JRB 10 Jun 2008 8:54 am
|
||||||
|
*****************************************************************/
|
||||||
|
#include "SPI_ADI.h"
|
||||||
|
#include "CC1101Setup.h"
|
||||||
|
#include "Timers_ADI.h"
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include "RF_Keys.h"
|
||||||
|
#include "RF.h"
|
||||||
|
|
||||||
|
byte RFTXArray[15];
|
||||||
|
byte RFRXIndex;
|
||||||
|
byte RFLastByte;
|
||||||
|
short RFValidStart;
|
||||||
|
|
||||||
|
long LastData;
|
||||||
|
short TXToggleFlag;
|
||||||
|
|
||||||
|
#define CLK_DELAY 5
|
||||||
|
#define CC_READ_MASK 0x80
|
||||||
|
#define CC_WRITE_MASK 0x00
|
||||||
|
#define CC_BURST_MASK 0x40
|
||||||
|
|
||||||
|
// COMMAND REGISTERS
|
||||||
|
#define CC_CMD_RESET 0x30
|
||||||
|
#define CC_CMD_STX 0x31
|
||||||
|
#define CC_CMD_RX 0x34
|
||||||
|
#define CC_CMD_TX 0x35
|
||||||
|
#define CC_CMD_IDLE 0x36
|
||||||
|
#define CC_CMD_SPWD 0x39
|
||||||
|
#define CC_CMD_RX_FLUSH 0x3A
|
||||||
|
#define CC_CMD_TX_FLUSH 0x3B
|
||||||
|
#define CC_CMD_NOP 0x3D
|
||||||
|
#define CC_CMD_PATABLE 0x3E
|
||||||
|
#define CC_CMD_FIFO 0x3F
|
||||||
|
|
||||||
|
// STATUS REGISTERS
|
||||||
|
#define CC_STATUS_VERSION (0x31 | CC_BURST_MASK)
|
||||||
|
#define CC_STATUS_MSTATE (0x35 | CC_BURST_MASK)
|
||||||
|
#define CC_STATUS_TX (0x3A | CC_BURST_MASK)
|
||||||
|
#define CC_STATUS_RX (0x3B | CC_BURST_MASK)
|
||||||
|
|
||||||
|
#define CLEAR_CS (digitalWrite(RF_CS, LOW))
|
||||||
|
#define SET_CS (digitalWrite(RF_CS, HIGH))
|
||||||
|
|
||||||
|
void sendUpKey(void);
|
||||||
|
void spiWrite(byte data);
|
||||||
|
void spiTransmit(byte state);
|
||||||
|
byte spiRead(void);
|
||||||
|
void sendDnKey(byte key);
|
||||||
|
long RFReadyCounter;
|
||||||
|
|
||||||
|
uint16_t rfState = 0;
|
||||||
|
|
||||||
|
#define RF_TX 17
|
||||||
|
|
||||||
|
SoftwareSerial ccTx(0, RF_TX);
|
||||||
|
|
||||||
|
#define SOWait() (digitalRead(12) != 0)
|
||||||
|
|
||||||
|
// void SOWait(void)
|
||||||
|
// {
|
||||||
|
// RFReadyCounter = 0;
|
||||||
|
// while(SO_INPUT)
|
||||||
|
// {
|
||||||
|
// if(RFReadyCounter > 1000)
|
||||||
|
// break;
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
/*****************************************************************
|
||||||
|
byte SendCCCommand(byte Address)
|
||||||
|
|
||||||
|
This sends commands to the CC and retrieves the status of the CC part.
|
||||||
|
*****************************************************************/
|
||||||
|
byte SendCCCommand(byte command)
|
||||||
|
{
|
||||||
|
byte data;
|
||||||
|
|
||||||
|
CLEAR_CS;
|
||||||
|
|
||||||
|
SOWait();
|
||||||
|
data = spi_readwrite(command);
|
||||||
|
|
||||||
|
SET_CS;
|
||||||
|
|
||||||
|
return (data);
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
byte GetCCData(byte Address)
|
||||||
|
|
||||||
|
This retrieves the status of the CC part.
|
||||||
|
*****************************************************************/
|
||||||
|
byte GetCCData(byte address)
|
||||||
|
{
|
||||||
|
byte data;
|
||||||
|
|
||||||
|
CLEAR_CS;
|
||||||
|
|
||||||
|
address |= CC_READ_MASK;
|
||||||
|
SOWait();
|
||||||
|
spi_readwrite(address);
|
||||||
|
data = spi_readwrite(0);
|
||||||
|
|
||||||
|
SET_CS;
|
||||||
|
|
||||||
|
return (data);
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
void SendCCData(byte Address, byte Data)
|
||||||
|
|
||||||
|
This sends a single byte of data to the CC.
|
||||||
|
*****************************************************************/
|
||||||
|
void SendCCData(byte address, byte data)
|
||||||
|
{
|
||||||
|
CLEAR_CS;
|
||||||
|
|
||||||
|
address |= CC_WRITE_MASK;
|
||||||
|
|
||||||
|
SOWait();
|
||||||
|
spi_readwrite(address);
|
||||||
|
spi_readwrite(data);
|
||||||
|
|
||||||
|
SET_CS;
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
void SendCCDataBurst(byte Address, byte *Buffer, byte Count)
|
||||||
|
|
||||||
|
This sends multiple data bytes to the CC.
|
||||||
|
*****************************************************************/
|
||||||
|
void SendCCDataBurst(byte address, byte *buffer, byte count)
|
||||||
|
{
|
||||||
|
byte dataCounter, data;
|
||||||
|
|
||||||
|
CLEAR_CS;
|
||||||
|
|
||||||
|
address |= CC_WRITE_MASK | CC_BURST_MASK;
|
||||||
|
|
||||||
|
SOWait();
|
||||||
|
spi_readwrite(address);
|
||||||
|
|
||||||
|
for (dataCounter = 0; dataCounter < count; dataCounter++)
|
||||||
|
{
|
||||||
|
spi_readwrite(buffer[dataCounter]);
|
||||||
|
}
|
||||||
|
|
||||||
|
SET_CS;
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
void SendCCDataBurst(byte Address, byte *Buffer, byte Count)
|
||||||
|
|
||||||
|
This sends multiple data bytes to the CC.
|
||||||
|
*****************************************************************/
|
||||||
|
void GetCCDataBurst(byte address, byte *buffer, byte count)
|
||||||
|
{
|
||||||
|
byte dataCounter, data;
|
||||||
|
|
||||||
|
CLEAR_CS;
|
||||||
|
|
||||||
|
address |= CC_READ_MASK | CC_BURST_MASK;
|
||||||
|
|
||||||
|
SOWait();
|
||||||
|
spi_readwrite(address);
|
||||||
|
for (dataCounter = 0; dataCounter < count; dataCounter++)
|
||||||
|
{
|
||||||
|
buffer[dataCounter] = spi_readwrite(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SET_CS;
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
void SetChannel(byte Channel);
|
||||||
|
|
||||||
|
This sets up center frequency channel RF Module.
|
||||||
|
|
||||||
|
1 channel = 333kHz
|
||||||
|
*****************************************************************/
|
||||||
|
void SetChannel(byte Channel)
|
||||||
|
{
|
||||||
|
SendCCData(0x0A, Channel * 3);
|
||||||
|
}
|
||||||
|
/*****************************************************************
|
||||||
|
void InitRFModule(void)
|
||||||
|
|
||||||
|
This sets up the configuration for the RF Module.
|
||||||
|
*****************************************************************/
|
||||||
|
void initCC1101(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Init the serial lines.
|
||||||
|
SendCCCommand(CC_CMD_RESET);
|
||||||
|
delay(2);
|
||||||
|
SendCCDataBurst(0, (uint8_t *)rfSettings, sizeof(rfSettings));
|
||||||
|
|
||||||
|
SendCCData(CC_CMD_PATABLE, 0xC0); // +10dBm
|
||||||
|
SetChannel(0);
|
||||||
|
|
||||||
|
SendCCCommand(CC_CMD_IDLE);
|
||||||
|
pinMode(RF_TX, OUTPUT);
|
||||||
|
delay(1);
|
||||||
|
ccTx.begin(40000);
|
||||||
|
Serial.println("RF Init Complete");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ccTxOn(void)
|
||||||
|
{
|
||||||
|
uint32_t time;
|
||||||
|
|
||||||
|
SendCCCommand(CC_CMD_TX);
|
||||||
|
time = millis();
|
||||||
|
while (GetCCData(CC_STATUS_MSTATE) != 19)
|
||||||
|
{
|
||||||
|
if (millis() - time > 20)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ccTxOff(void)
|
||||||
|
{
|
||||||
|
uint32_t time;
|
||||||
|
|
||||||
|
SendCCCommand(CC_CMD_IDLE);
|
||||||
|
time = millis();
|
||||||
|
while (GetCCData(CC_STATUS_MSTATE) != 1)
|
||||||
|
{
|
||||||
|
if (millis() - time > 20)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
#ifndef CC1101_SETUP_H
|
||||||
|
#define CC1101_SETUP_H
|
||||||
|
// These setting were calculated in the SmartRF Studio
|
||||||
|
// trying to match the SiLabs si4460 settings used by the
|
||||||
|
// DD2702H Transmitter
|
||||||
|
// Rf settings for CC1101
|
||||||
|
const uint8_t rfSettings[] = {
|
||||||
|
0x0D, // IOCFG2 GDO2 Output Pin Configuration
|
||||||
|
0x2E, // IOCFG1 GDO1 Output Pin Configuration
|
||||||
|
0x2E, // IOCFG0 GDO0 Output Pin Configuration
|
||||||
|
0x0F, // FIFOTHR RX FIFO and TX FIFO Thresholds
|
||||||
|
0x2D, // SYNC1 Sync Word, High Byte
|
||||||
|
0xD4, // SYNC0 Sync Word, Low Byte
|
||||||
|
0x15, // PKTLEN Packet Length 21 bytes
|
||||||
|
0x00, // PKTCTRL1 Packet Automation Control // no status append
|
||||||
|
0x30, // PKTCTRL0 Packet Automation Control // async mode
|
||||||
|
0x00, // ADDR Device Address
|
||||||
|
0x00, // CHANNR Channel Number
|
||||||
|
0x06, // FSCTRL1 Frequency Synthesizer Control
|
||||||
|
0x00, // FSCTRL0 Frequency Synthesizer Control
|
||||||
|
0x10, // FREQ2 Frequency Control Word, High Byte 433.92Mhz
|
||||||
|
0x12, // FREQ1 Frequency Control Word, Middle Byte
|
||||||
|
0x34, // FREQ0 Frequency Control Word, Low Byte
|
||||||
|
0xFA, // MDMCFG4 Modem Configuration
|
||||||
|
0x84, // MDMCFG3 Modem Configuration
|
||||||
|
// 0x33, // MDMCFG2 Modem Configuration ASK no Manch 32bit sync
|
||||||
|
0x13, // MDMCFG2 Modem Configuration GFSK no Manch 32bit sync
|
||||||
|
0x73, // MDMCFG1 Modem Configuration
|
||||||
|
0x2F, // MDMCFG0 Modem Configuration
|
||||||
|
0x34, // DEVIATN Modem Deviation Setting
|
||||||
|
0x07, // MCSM2 Main Radio Control State Machine Configuration
|
||||||
|
0x30, // MCSM1 Main Radio Control State Machine Configuration
|
||||||
|
0x18, // MCSM0 Main Radio Control State Machine Configuration
|
||||||
|
0x16, // FOCCFG Frequency Offset Compensation Configuration
|
||||||
|
0x6C, // BSCFG Bit Synchronization Configuration
|
||||||
|
0x03, // AGCCTRL2 AGC Control
|
||||||
|
0x40, // AGCCTRL1 AGC Control
|
||||||
|
0x91, // AGCCTRL0 AGC Control
|
||||||
|
0x87, // WOREVT1 High Byte Event0 Timeout
|
||||||
|
0x6B, // WOREVT0 Low Byte Event0 Timeout
|
||||||
|
0xFB, // WORCTRL Wake On Radio Control
|
||||||
|
0x56, // FREND1 Front End RX Configuration
|
||||||
|
0x10, // FREND0 Front End TX Configuration
|
||||||
|
0xE9, // FSCAL3 Frequency Synthesizer Calibration
|
||||||
|
0x2A, // FSCAL2 Frequency Synthesizer Calibration
|
||||||
|
0x00, // FSCAL1 Frequency Synthesizer Calibration
|
||||||
|
0x1F, // FSCAL0 Frequency Synthesizer Calibration
|
||||||
|
0x41, // RCCTRL1 RC Oscillator Configuration
|
||||||
|
0x00, // RCCTRL0 RC Oscillator Configuration
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CC1101_SETUP_H
|
||||||
|
|
@ -0,0 +1,170 @@
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// J1939 CAN Connection
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
#include "mcp_can.h"
|
||||||
|
|
||||||
|
#define CS_PIN 4 // Use pin 10 for Seeed Studio CAN Shield up to version 1.0
|
||||||
|
// Use pin 9 for Seeed Studion CAN shiled version 1.1 and higher
|
||||||
|
// Use pin 10 for SK Pang CAN shield
|
||||||
|
MCP_CAN CAN(CS_PIN);
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// CAN message ring buffer setup
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
#define CANMSGBUFFERSIZE 10
|
||||||
|
struct CANMsg
|
||||||
|
{
|
||||||
|
long lID;
|
||||||
|
unsigned char pData[8];
|
||||||
|
int nDataLen;
|
||||||
|
};
|
||||||
|
CANMsg CANMsgBuffer[CANMSGBUFFERSIZE];
|
||||||
|
int nWritePointer;
|
||||||
|
int nReadPointer;
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Initialize the CAN controller
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
int canInitialize(int nBaudRate)
|
||||||
|
{
|
||||||
|
// Default settings
|
||||||
|
nReadPointer = 0;
|
||||||
|
nWritePointer = 0;
|
||||||
|
|
||||||
|
// Initialize the CAN controller
|
||||||
|
if (CAN.begin(nBaudRate) == 0)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
} // end canInitialize
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Transmit CAN message
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
byte canTransmit(long lID, unsigned char *pData, int nDataLen)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (CAN.sendMsgBuf(lID, CAN_EXTID, nDataLen, pData) == 0)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
} // end canTransmit
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Receive CAN message
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
byte canReceive(long *lID, unsigned char *pData, int *nDataLen)
|
||||||
|
{
|
||||||
|
// Declarations
|
||||||
|
byte nCnt;
|
||||||
|
|
||||||
|
// In case there is a message, put it into the buffer
|
||||||
|
while (CAN.checkReceive() == CAN_MSGAVAIL)
|
||||||
|
{
|
||||||
|
// Read the message buffer
|
||||||
|
CAN.readMsgBuf(&nCnt, &CANMsgBuffer[nWritePointer].pData[0]);
|
||||||
|
CANMsgBuffer[nWritePointer].nDataLen = (int)nCnt;
|
||||||
|
CANMsgBuffer[nWritePointer].lID = CAN.getCanId();
|
||||||
|
|
||||||
|
if (++nWritePointer == CANMSGBUFFERSIZE)
|
||||||
|
nWritePointer = 0;
|
||||||
|
|
||||||
|
} // end while
|
||||||
|
|
||||||
|
// Check ring buffer for a message
|
||||||
|
if (nReadPointer != nWritePointer)
|
||||||
|
{
|
||||||
|
// Read the next message buffer entry
|
||||||
|
*nDataLen = CANMsgBuffer[nReadPointer].nDataLen;
|
||||||
|
*lID = CANMsgBuffer[nReadPointer].lID;
|
||||||
|
|
||||||
|
for (int nIdx = 0; nIdx < *nDataLen; nIdx++)
|
||||||
|
pData[nIdx] = CANMsgBuffer[nReadPointer].pData[nIdx];
|
||||||
|
|
||||||
|
if (++nReadPointer == CANMSGBUFFERSIZE)
|
||||||
|
nReadPointer = 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
} // end canReceive
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// J1939 Check Peer-to-Peer
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
bool j1939PeerToPeer(long lPGN)
|
||||||
|
{
|
||||||
|
// Check the PGN
|
||||||
|
if (lPGN > 0 && lPGN <= 0xEFFF)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (lPGN > 0x10000 && lPGN <= 0x1EFFF)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} // end j1939PeerToPeer
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// J1939 Transmit
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
byte j1939Transmit(long lPGN, byte nPriority, byte nSrcAddr, byte nDestAddr, byte *nData, int nDataLen)
|
||||||
|
{
|
||||||
|
// Declarations
|
||||||
|
long lID = ((long)nPriority << 26) + (lPGN << 8) + (long)nSrcAddr;
|
||||||
|
|
||||||
|
// If PGN represents a peer-to-peer, add destination address to the ID
|
||||||
|
if (j1939PeerToPeer(lPGN) == true)
|
||||||
|
{
|
||||||
|
lID = lID & 0xFFFF00FF;
|
||||||
|
lID = lID | ((long)nDestAddr << 8);
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
|
// Transmit the message
|
||||||
|
return canTransmit(lID, nData, nDataLen);
|
||||||
|
|
||||||
|
} // end j1939Transmit
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// J1939 Receive
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
byte j1939Receive(long *lPGN, byte *nPriority, byte *nSrcAddr, byte *nDestAddr, byte *nData, int *nDataLen)
|
||||||
|
{
|
||||||
|
// Declarations
|
||||||
|
byte nRetCode = 1;
|
||||||
|
long lID;
|
||||||
|
|
||||||
|
// Default settings
|
||||||
|
*nSrcAddr = 255;
|
||||||
|
*nDestAddr = 255;
|
||||||
|
|
||||||
|
if (canReceive(&lID, nData, nDataLen) == 0)
|
||||||
|
{
|
||||||
|
long lPriority = lID & 0x1C000000;
|
||||||
|
*nPriority = (int)(lPriority >> 26);
|
||||||
|
|
||||||
|
*lPGN = lID & 0x00FFFF00;
|
||||||
|
*lPGN = *lPGN >> 8;
|
||||||
|
|
||||||
|
lID = lID & 0x000000FF;
|
||||||
|
*nSrcAddr = (int)lID;
|
||||||
|
|
||||||
|
if (j1939PeerToPeer(*lPGN) == true)
|
||||||
|
{
|
||||||
|
*nDestAddr = (int)(*lPGN & 0xFF);
|
||||||
|
*lPGN = *lPGN & 0x01FF00;
|
||||||
|
}
|
||||||
|
|
||||||
|
nRetCode = 0;
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
|
return nRetCode;
|
||||||
|
|
||||||
|
} // end j1939Receive
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef CAN_EXT_H
|
||||||
|
#define CAN_EXT_H
|
||||||
|
|
||||||
|
extern int canInitialize(int nBaudRate);
|
||||||
|
extern byte j1939Transmit(long lPGN, byte nPriority, byte nSrcAddr, byte nDestAddr, byte *nData, int nDataLen);
|
||||||
|
extern byte j1939Receive(long *lPGN, byte *nPriority, byte *nSrcAddr, byte *nDestAddr, byte *nData, int *nDataLen);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue