CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18LF4620 hardware SPI bus problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Zloi



Joined: 26 Oct 2012
Posts: 11
Location: Croatia

View user's profile Send private message

18LF4620 hardware SPI bus problem
PostPosted: Wed May 22, 2013 1:01 pm     Reply with quote

Hello,
I have a weird spi problem when i try to communicate width cc2530znp chip. My mcu is pic18f4620, hardware spi (tried software SPI, it works but not on SPI pin RC4/SDI, tried pull up, poll down, put the serial resistor in line MI, change mcu to 18f45k22, the problem remains).
I'm trying to initialize cc2530 by hardware reseting it an then reading the poll msg from it, chip responds with chaotic data, like it is tree stating MI data line.
Sometimes initialization process goes well, I configure the cc2530znp, and can send and receive over the air msg, but this is in a very rare case..

Attached are scope traces.

First spi byte ok



First spi byte error


any suggestions what to do?
Is this problem related to, Rev. B5 Silicon Errata
http://ww1.microchip.com/downloads/en/DeviceDoc/80364a.pdf

or I'm incorrectly Initializing SPI port.

C compiler version 4.134


Code:

#include <18LF4620.h>
#fuses INTRC,NOWDT,NOPROTECT,NOLVP,PUT
//#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
//#use delay(crystal=10Mhz)
#use delay (clock=40Mhz,crystal=10Mhz )                              //x4 PLL, CPU runs at 40Mhz,
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, ERRORS)

#define DEVID1_ADDR 0x3FFFFE
#define DEVID2_ADDR 0x3FFFFF

#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_0_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_1_0  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3


#define     RESET_ZNP      PIN_C2   //Reset CC2530
#define      MRDY_ZNP      PIN_C0   
#define      SRDY_ZNP      PIN_B0
//#define      SS_ZNP      PIN_C0   //ZNP SS, Slave select
//#define      MI             PIN_C4   
//#define      MO            PIN_C5
//#define      CS            PIN_C2         //MAX7219
//#define      CLK         PIN_C3



void SystemInit(){
   printf("System Power up\n\r");
   disable_interrupts(GLOBAL); 
   output_high(MRDY_ZNP);
   setup_adc(ADC_OFF);
//   setup_vref(FALSE);
    setup_psp(PSP_DISABLED);
//  setup_wdt(WDT_OFF);
   setup_comparator(NC_NC_NC_NC);
//  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8|T0_8_BIT);   //     
//  setup_ccp1(CCP_PWM);                            // PWM period = [(PR2)+ 1] • 4 • 1/TOSC • TMR2 prescale value
//  setup_timer_2(T2_DIV_BY_4,0xFF,2);                 // PWM period = [(99)+ 1] • 4 • 1/4E6 • 1 = 0,0001
                                                      // PWM frequency is = 1/0,0001 = 10000Hz = 10Khz
//   setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);            //                             
//   setup_timer_3(T3_EXTERNAL_SYNC|T3_DIV_BY_1);      // TIMER3 korišten kao brojač, RC0 Ulaz za TIMER/COUNTER3,     

//   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);              //Timer1 generira signal za CCP2 Compare modul

//   setup_spi(SPI_DISABLED  );   
setup_spi(SPI_MASTER,SPI_CLK_DIV_16,SPI_SS_DISABLED,SPI_MODE_0_0)

//#use spi(MASTER,DI=PIN_B1,DO=PIN_C5,CLK=PIN_C3,BITS=8,MODE=0)
//  soft works ok, but on DI PIN_B1
}

// cc2530 poll spi poll command
void ZNP_Poll(void){
   //   output_toggle(PIN_A1);
      while(input(SRDY_ZNP)){};     
         output_low(MRDY_ZNP);      //Select ZNP
      //   delay_cycles (6);
         spi_xfer(0);
         spi_xfer(0);
         spi_xfer(0);   
         while(!input(SRDY_ZNP)){   
         }
      //   delay_cycles(5);
         Data[0]=spi_xfer(0);
         Data[1]=spi_xfer(0);
         Data[2]=spi_xfer(0);
         
         for(i=0;i<=Data[0];i++){
            Data[i+3]=spi_xfer(0);
         }
         
         ZNP_SetDataReady();
         output_high(MRDY_ZNP);   //DeSelect ZNP   
}

void main( void ){
   // setup_oscillator(OSC_32MHZ);


   SystemInit();
   VariableInit();
   
   int i;
   for(i=0;i<6;i++){   
      output_toggle(PIN_A0);
      delay_ms(200);
   }


char devid1, devid2;

devid1 = read_program_eeprom(DEVID1_ADDR);
devid2 = read_program_eeprom(DEVID2_ADDR);

printf("dev ID 1 = %x\n\r", devid1);
printf("dev ID 2 = %x\n\r", devid2);

   while (TRUE ){
ZNP_Poll();
//znp methods
ZNP_Decode_Data()...
.
.
}



any suggestions what to do?
or am I stuck with using soft spi ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 22, 2013 1:25 pm     Reply with quote

Code:
setup_spi(SPI_MASTER,SPI_CLK_DIV_16,SPI_SS_DISABLED,SPI_MODE_0_0)

You're not really testing this, because this line doesn't compile. The
syntax for setup_spi() is to use the bitwise OR operator '|' to combine the
parameters, not commas. Also, SPI_SS_DISABLED is not used with
an SPI master. It's only used with a PIC SPI slave.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed May 22, 2013 1:39 pm     Reply with quote

Code:
#fuses INTRC,NOWDT,NOPROTECT,NOLVP,PUT
//#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
//#use delay(crystal=10Mhz)
#use delay (clock=40Mhz,crystal=10Mhz )                              //x4 PLL, CPU runs at 40Mhz,
First, get your clock setup fixed!
With INTRC you can never be running at 40MHz, most likely you are running at 32MHz.
I don't know where your printf output is going to but if it is any kind of UART output I'm surprised you get readable text.
Also note that the internal oscillator is not well suited for RS232 communications. RS232 requires a clock with 3% maximum tolerance and that is best achieved with a crystal clock.

Code:
setup_spi(SPI_MASTER,SPI_CLK_DIV_16,SPI_SS_DISABLED,SPI_MODE_0_0)
setup_spi accepts only 1 parameter. All options have to be or'ed together, just like you do in the setup_timer functions.
Also note that the SPI_SS_DISABLED parameter is for the slave only, on the master it generates an invalid configuration.
The CC2530 accepts a maximum SPI clock frequency of 4MHz, so with your PIC running at 32MHz / 40MHz you can try with the following setup:
Code:
setup_spi(SPI_MASTER | SPI_CLK_DIV_16 | SPI_MODE_0_0)

I checked the datasheet and SPI mode 0 is correct.
Zloi



Joined: 26 Oct 2012
Posts: 11
Location: Croatia

View user's profile Send private message

PostPosted: Wed May 22, 2013 2:02 pm     Reply with quote

ckielstra wrote:
Code:
#fuses INTRC,NOWDT,NOPROTECT,NOLVP,PUT
//#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
//#use delay(crystal=10Mhz)
#use delay (clock=40Mhz,crystal=10Mhz )                              //x4 PLL, CPU runs at 40Mhz,
First, get your clock setup fixed!
With INTRC you can never be running at 40MHz, most likely you are running at 32MHz.
I don't know where your printf output is going to but if it is any kind of UART output I'm surprised you get readable text.
Also note that the internal oscillator is not well suited for RS232 communications. RS232 requires a clock with 3% maximum tolerance and that is best achieved with a crystal clock.


I'm experimenting with various clock settings, because of that the settings
are stated/noted.
I'm using printf just for debugging purpose, it's directed to rs232 PC serial port.
here is sample of returned data...

System Power up
dev ID 1 = 07
dev ID 2 = 0c
Resetting ZNP1
Decode Znp Data
ZNP -> 060000010200020501 //junk data
Unknown State
Resetting ZNP1
Decode Znp Data
ZNP -> 0201800102 //junk data
Unknown State
Resetting ZNP1
Decode Znp Data
ZNP -> 064180000200020001 //Data OK
PIC -> ZNP Clear Device on Reset
Decode Znp Data
ZNP -> 000000 //junk data
.
.
.
.


Quote:

Code:
setup_spi(SPI_MASTER,SPI_CLK_DIV_16,SPI_SS_DISABLED,SPI_MODE_0_0)
setup_spi accepts only 1 parameter. All options have to be or'ed together, just like you do in the setup_timer functions.
Also note that the SPI_SS_DISABLED parameter is for the slave only, on the master it generates an invalid configuration.
The CC2530 accepts a maximum SPI clock frequency of 4MHz, so with your PIC running at 32MHz / 40MHz you can try with the following setup:
Code:
setup_spi(SPI_MASTER | SPI_CLK_DIV_16 | SPI_MODE_0_0)

I checked the datasheet and SPI mode 0 is correct.


more parameters separated by a comma is my fault when I was preparing the code for the forum post, in the source code, all parameters are or'ed together...

Did you perhaps view the attached images of scope traces ?
Zloi



Joined: 26 Oct 2012
Posts: 11
Location: Croatia

View user's profile Send private message

PostPosted: Wed May 22, 2013 2:05 pm     Reply with quote

PCM programmer wrote:
Code:
setup_spi(SPI_MASTER,SPI_CLK_DIV_16,SPI_SS_DISABLED,SPI_MODE_0_0)

You're not really testing this, because this line doesn't compile. The
syntax for setup_spi() is to use the bitwise OR operator '|' to combine the
parameters, not commas. Also, SPI_SS_DISABLED is not used with
an SPI master. It's only used with a PIC SPI slave.


more parameters separated by a comma is my fault when I was preparing the code for the forum post, in the source code, all parameters are or'ed together...

I tried setup_spi() with and without this parameter, SPI_SS_DISABLED,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group