Hi,
I have a problem with my design: a device can receive signal from BLE and send it out by Sub 1 Ghz RF signal. like get commend from smart phone then send it to the device just have RF receiver. BLE part use cc2541 and RF part use cc1101.
Here I have a problem with the the communication between two chip:
IAR didn't give me any warning or error in program, but when I try to write the Characteristic 6, the cc2541 will lost connection, I have to restart it again to find it. In this programe write Characteristic 6 will active call back function to communicate with CC1101 by SPI.
"Characteristic 6" is a Characteristic I just add at simpleGATTprofile with read and write, callback function is locate at simpleBLEPeripheral.c "simpleProfileChangeCB" like that:
/********************************************************************* * @fn simpleProfileChangeCB * @brief Callback from SimpleBLEProfile indicating a value change * @param paramID - parameter ID of the value that was changed. */
static void simpleProfileChangeCB( uint8 paramID ) { uint8 newValue; switch( paramID ) { #if G5_Jin case SIMPLEPROFILE_CHAR6: //send commend through spi to RF SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, &newValue ); RFsend(newValue); break; #endif };
And the defination of "RFsend" is:
#include "SPI_driver_DJ.h"
#include <iocc2540.h>
#define  INT8U  unsigned char
#define  uint8  unsigned char
#define  INT16U  unsigned int
#define  WRITE_BURST      0x40 
#define  CRC_OK           0x80
//*****************************************************************************************
#define SCK             P0_4 
#define MISO            P0_2 
#define MOSI            P0_3
#define CSN             P0_5
#define GDO0            P1_2
//*****************************************************************************************
INT8U num=0;
//*****************************************************************************************
INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};
//*****************************************************************************************
INT8U seg[10]={0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; 
typedef struct {
    uint8 iocfg0;     // GDO0 Output Pin Configuration
    uint8 fifothr;    // RX FIFO and TX FIFO Thresholds
    uint8 pktctrl0;   // Packet Automation Control
    uint8 fsctrl1;    // Frequency Synthesizer Control
    uint8 freq2;      // Frequency Control Word, High Byte
    uint8 freq1;      // Frequency Control Word, Middle Byte
    uint8 freq0;      // Frequency Control Word, Low Byte
    uint8 mdmcfg4;    // Modem Configuration
    uint8 mdmcfg3;    // Modem Configuration
    uint8 mdmcfg2;    // Modem Configuration
    uint8 deviatn;    // Modem Deviation Setting
    uint8 mcsm0;      // Main Radio Control State Machine Configuration
    uint8 foccfg;     // Frequency Offset Compensation Configuration
    uint8 worctrl;    // Wake On Radio Control
    uint8 fscal3;     // Frequency Synthesizer Calibration
    uint8 fscal2;     // Frequency Synthesizer Calibration
    uint8 fscal1;     // Frequency Synthesizer Calibration
    uint8 fscal0;     // Frequency Synthesizer Calibration
    uint8 test2;      // Various Test Settings
    uint8 test1;      // Various Test Settings
} RF_SETTINGS;
const RF_SETTINGS rfSettings =
{
    0x06,  // IOCFG0        GDO0 Output Pin Configuration
    0x47,  // FIFOTHR       RX FIFO and TX FIFO Thresholds
    0x05,  // PKTCTRL0      Packet Automation Control
    0x06,  // FSCTRL1       Frequency Synthesizer Control
    0x0B,  // FREQ2         Frequency Control Word, High Byte
    0xB0,  // FREQ1         Frequency Control Word, Middle Byte
    0x00,  // FREQ0         Frequency Control Word, Low Byte
    0xF5,  // MDMCFG4       Modem Configuration
    0x83,  // MDMCFG3       Modem Configuration
    0x13,  // MDMCFG2       Modem Configuration
    0x15,  // DEVIATN       Modem Deviation Setting
    0x18,  // MCSM0         Main Radio Control State Machine Configuration
    0x16,  // FOCCFG        Frequency Offset Compensation Configuration
    0xFB,  // WORCTRL       Wake On Radio Control
    0xE9,  // FSCAL3        Frequency Synthesizer Calibration
    0x2A,  // FSCAL2        Frequency Synthesizer Calibration
    0x00,  // FSCAL1        Frequency Synthesizer Calibration
    0x1F,  // FSCAL0        Frequency Synthesizer Calibration
    0x81,  // TEST2         Various Test Settings
    0x35,  // TEST1         Various Test Settings
}; 
/********************************************************************************/
static void delay(unsigned int s)
{
unsigned int i;
for(i=0; i<s; i++);
for(i=0; i<s; i++);
}
void halWait(INT16U timeout) {
do {
  asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
  asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
    } while (--timeout);
}
void SpiInit(void)
{
CSN=0;
SCK=0;
CSN=1;
}
/*****************************************************************************************/
void CpuInit(void)
{
SpiInit();
delay(5000);
}
//*****************************************************************************************
//SpisendByte(INT8U dat)
INT8U SpiTxRxByte(INT8U dat)
{
INT8U i,temp;
temp = 0;
SCK = 0;
for(i=0; i<8; i++)
{
  if(dat & 0x80)
  {
   MOSI = 1;
  }
  else MOSI = 0;
  dat <<= 1;
  SCK = 1;
  asm("NOP");
  asm("NOP");
  temp <<= 1;
  if(MISO)temp++;
  SCK = 0;
  asm("NOP");
  asm("NOP");
}
return temp;
}
//*****************************************************************************************
void RESET_CC1100(void)
{
CSN = 0;
while (MISO);
    SpiTxRxByte(CCxxx0_SRES); 
while (MISO);
    CSN = 1;
}
//*****************************************************************************************
void POWER_UP_RESET_CC1100(void)
{
CSN = 1;
halWait(1);
CSN = 0;
halWait(1);
CSN = 1;
halWait(41);
RESET_CC1100();     
}
//*****************************************************************************************
//void halSpiWriteReg(INT8U addr, INT8U value)
void halSpiWriteReg(INT8U addr, INT8U value)
{
    CSN = 0;
    while (MISO);
    SpiTxRxByte(addr);  
    SpiTxRxByte(value);  
    CSN = 1;
}
//*****************************************************************************************
void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
{
    INT8U i, temp;
temp = addr | WRITE_BURST;
    CSN = 0;
    while (MISO);
    SpiTxRxByte(temp);
    for (i = 0; i < count; i++)
  {
        SpiTxRxByte(buffer[i]);
    }
    CSN = 1;
}
/*********************************************************************************************/
void halSpiStrobe(INT8U strobe)
{
    CSN = 0;
    while (MISO);
    SpiTxRxByte(strobe); 
    CSN = 1;
}
//*****************************************************************************************
//CC1101 register
//*****************************************************************************************
void halRfWriteRfSettings(void)
{
halSpiWriteReg(CCxxx0_IOCFG0,rfSettings.iocfg0);    //GDO0 Output Pin Configuration
halSpiWriteReg(CCxxx0_FIFOTHR,rfSettings.fifothr);  //RX FIFO and TX FIFO Thresholds
halSpiWriteReg(CCxxx0_PKTCTRL0,rfSettings.pktctrl0);//Packet Automation Control
halSpiWriteReg(CCxxx0_FSCTRL1,rfSettings.fsctrl1);  //Frequency Synthesizer Control
halSpiWriteReg(CCxxx0_FREQ2,rfSettings.freq2);      //Frequency Control Word, High Byte
halSpiWriteReg(CCxxx0_FREQ1,rfSettings.freq1);      //Frequency Control Word, Middle Byte
halSpiWriteReg(CCxxx0_FREQ0,rfSettings.freq0);      //Frequency Control Word, Low Byte
halSpiWriteReg(CCxxx0_MDMCFG4,rfSettings.mdmcfg4);  //Modem Configuration
halSpiWriteReg(CCxxx0_MDMCFG3,rfSettings.mdmcfg3);  //Modem Configuration
halSpiWriteReg(CCxxx0_MDMCFG2,rfSettings.mdmcfg2);  //Modem Configuration
halSpiWriteReg(CCxxx0_DEVIATN,rfSettings.deviatn);  //Modem Deviation Setting
halSpiWriteReg(CCxxx0_MCSM0,rfSettings.mcsm0);      //Main Radio Control State Machine Configuration
halSpiWriteReg(CCxxx0_FOCCFG,rfSettings.foccfg);    //Frequency Offset Compensation Configuration
halSpiWriteReg(CCxxx0_WORCTRL,rfSettings.worctrl);  //Wake On Radio Control
halSpiWriteReg(CCxxx0_FSCAL3,rfSettings.fscal3);    //Frequency Synthesizer Calibration
halSpiWriteReg(CCxxx0_FSCAL2,rfSettings.fscal2);    //Frequency Synthesizer Calibration
halSpiWriteReg(CCxxx0_FSCAL1,rfSettings.fscal1);    //Frequency Synthesizer Calibration
halSpiWriteReg(CCxxx0_FSCAL0,rfSettings.fscal0);    //Frequency Synthesizer Calibration
halSpiWriteReg(CCxxx0_TEST2,rfSettings.test2);      //Various Test Settings
halSpiWriteReg(CCxxx0_TEST1,rfSettings.test1);      //Various Test Settings
}
//*****************************************************************************************
void halRfSendPacket(INT8U *txBuffer, INT8U size)
{
    halSpiWriteReg(CCxxx0_TXFIFO, size);
    halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size); 
    halSpiStrobe(CCxxx0_STX); 
    // Wait for GDO0 to be set -> sync transmitted
    while (!GDO0);
    // Wait for GDO0 to be cleared -> end of packet
    while (GDO0);
halSpiStrobe(CCxxx0_SFTX);
}
void RFsend(uint8 outputdat)
{
  INT8U leng=3;
  //INT8U tf =0;
  INT8U TxBuf[3]={0xAA, 0xAA, 0xE0}; 
     switch(outputdat)
      {
      case fan_6:
          TxBuf[2] = 0x80;
          break;
        
      case fan_off:
          TxBuf[2] = 0x20;
          break;
        
      case fan_learn:
          TxBuf[2] = 0xE0;
          break;
      };       
  CpuInit();
  POWER_UP_RESET_CC1100();
  halRfWriteRfSettings();
  halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
  delay(6000);
  halRfSendPacket(TxBuf, leng);
}
Thanks
D.J