Hello all,

I already wrote to Microchip forum but the problem can be in SDCC as well. The 
problem is that when I use "built-in SPI" read, I always get 0x00. But with 
"manual SPI" I get correct result.

I started to fight with the communication between PIC18f2580 and CC1100 in SDCC 
version 2008-Sep-10 ( I compile with sdcc -V -mpic16 -p18f2580 main.c )

I use internal 8MHz oscillator and have configured SPI following way:

CC1100ENB_TRIS = 0; // CC1100 chip select
TRISCbits.TRISC3 = 0; // SCK
TRISCbits.TRISC4 = 1; // SDI
TRISCbits.TRISC5 = 0; // SDO

SSPSTAT = 0x40;
SSPCON1 = 0x22;

Then, for CC1100 register read I use following code:

unsigned char CC1100_read( unsigned char aAdr)
{
 unsigned char tmp;

   CC1100ENB_PIN = 0;
   while ( 1 == PORTCbits.RC4) ; // wait for SO goes low again */

   SSPBUF = aAdr; // send address
   while ( 0 == SSPSTATbits.BF) ; // wait until end of address transfer
   tmp = SSPBUF;

   SSPBUF = 0x00; // send dummy data
   while ( 0 == SSPSTATbits.BF) ; // wait until end of data transfer
   tmp = SSPBUF;

   CC1100ENB_PIN = 1;

   return SSPBUF;
}


But with this routine I always get 0x00 (and as well as after I send the 
address). If I switch off the HW SPI ( SSPCON1 = 0x00) and use software SPI 
like following code, everything works OK and I get real register values. 

unsigned char CC1100_read_correct( unsigned char aAdr)
{
 unsigned char tmp, tmp1, tmp2, i;

 tmp2 = aAdr; // address
 tmp = 0; // data

   CC1100ENB_PIN = 0;
   while ( 1 == PORTCbits.RC4) ; // wait for SO goes low again */

   PORTCbits.RC3 = 0;

   // send address
   for( i = 0; i < 8;i++) {
     if ( (tmp2&0x80) == 0) {
   PORTCbits.RC5 = 0;
     } else {
   PORTCbits.RC5 = 1;
     }
     PORTCbits.RC3 = 1;
     tmp <<= 1;
     if (PORTCbits.RC4 == 1) {
   tmp |= 0x01;
     }

     PORTCbits.RC3 = 0;
   }

   // send dummy data
   for( i = 0; i < 8;i++) {
     PORTCbits.RC5 = 0;
     PORTCbits.RC3 = 1;
     tmp <<= 1;
     if (PORTCbits.RC4 == 1) {
   tmp |= 0x01;
     }

     PORTCbits.RC3 = 0;
   }

   CC1100ENB_PIN = 1;

   return SSPBUF;
}

I inspected the generated code from the "HW SPI" routine but it seems to me ok. 
But maybe more experienced people find the problem in the code.

_CC1100_read:
;       .line   472; main.c     unsigned char CC1100_read( unsigned char aAdr)
        MOVFF   FSR2L, POSTDEC1
        MOVFF   FSR1L, FSR2L
        MOVFF   r0x00, POSTDEC1
        MOVFF   r0x01, POSTDEC1
        MOVLW   0x02
        MOVFF   PLUSW2, r0x00
;       .line   479; main.c     CC1100ENB_PIN = 0;
        BCF     _PORTBbits, 5
_00357_DS_:
;       .line   480; main.c     while ( 1 == PORTCbits.RC4) ; // wait for SO 
goes low again */
        CLRF    r0x01
        BTFSC   _PORTCbits, 4
        INCF    r0x01, F
        MOVF    r0x01, W
        XORLW   0x01
        BZ      _00357_DS_
;       .line   518; main.c     SSPCON1 = 0x32;
        MOVLW   0x32
        MOVWF   _SSPCON1
;       .line   519; main.c     SSPBUF = aAdr; // send address
        MOVFF   r0x00, _SSPBUF
_00360_DS_:
;       .line   520; main.c     while ( 0 == SSPSTATbits.BF) ; // wait until 
end of address transfer
        CLRF    r0x00
        BTFSC   _SSPSTATbits, 0
        INCF    r0x00, F
        MOVF    r0x00, W
        BZ      _00360_DS_
_00376_DS_:
_00377_DS_:
        ;       VOLATILE READ - BEGIN
        MOVF    _SSPBUF, W
        ;       VOLATILE READ - END
;       .line   526; main.c     SSPBUF = 0x11; // send dummy data
        MOVLW   0x11
        MOVWF   _SSPBUF
_00363_DS_:
;       .line   527; main.c     while ( 0 == SSPSTATbits.BF) ; // wait until 
end of data transfer
        CLRF    r0x00
        BTFSC   _SSPSTATbits, 0
        INCF    r0x00, F
        MOVF    r0x00, W
        BZ      _00363_DS_
_00378_DS_:
_00379_DS_:
        ;       VOLATILE READ - BEGIN
        MOVF    _SSPBUF, W
        ;       VOLATILE READ - END
;       .line   534; main.c     CC1100ENB_PIN = 1;
        BSF     _PORTBbits, 5
;       .line   536; main.c     return SSPBUF;
        MOVF    _SSPBUF, W
        MOVFF   PREINC1, r0x01
        MOVFF   PREINC1, r0x00
        MOVFF   PREINC1, FSR2L
        RETURN  


Thank you in advance,
Vaclav

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to