Hello all,

after some elaboration I found the proper solution. Or, at least, it can 
currently work for me.

I apologize for my stupid questions and as a reward I am sending current 
working SPI_test.c to all people interested.

There are some couple of things which can be done better. For example, I found 
that SDCC does not know how to optimize "if( 1 == SOMEREGbits.SOMEBIT) {}" - 
instead one can use "if (SOMEREGbits.SOMEBIT != 0) {}". But this is minor 
problem...

Best Regards,
Vaclav
// CC1100 SPI test

#define LED_TRIS  TRISCbits.TRISC0
#define LED_PIN   PORTCbits.RC0

#define CC1100ENB_TRIS TRISBbits.TRISB5
#define CC1100ENB_PIN PORTBbits.RB5


#define CC1100_CONFIG_LEN (70)

unsigned char CC1100_REG_CONFIG[] = {
0x0B, 0x06, /* Frequency synthesizer control. */
0x0C, 0x00, /* Frequency synthesizer control. */
0x0D, 0x10, /* Frequency control word, high byte. */
0x0E, 0x1C, /* Frequency control word, middle byte. */
0x0F, 0x71, /* Frequency control word, low byte. */
0x10, 0xC7, /* Modem configuration. */
0x11, 0x75, /* Modem configuration. */
0x12, 0x03, /* Modem configuration. */
0x13, 0x22, /* Modem configuration. */
0x14, 0xE5, /* Modem configuration. */
0x0A, 0x00, /* Channel number. */
0x15, 0x37, /* Modem deviation setting (when FSK modulation is enabled). */
0x21, 0x56, /* Front end RX configuration. */
0x22, 0x10, /* Front end TX configuration. */
0x18, 0x18, /* Main Radio Control State Machine configuration. */
0x19, 0x16, /* Frequency Offset Compensation Configuration. */
0x1A, 0x6C, /* Bit synchronization Configuration. */
0x1B, 0x43, /* AGC control. */
0x1C, 0x40, /* AGC control. */
0x1D, 0x91, /* AGC control. */
0x23, 0xE9, /* Frequency synthesizer calibration. */
0x24, 0x2A, /* Frequency synthesizer calibration. */
0x25, 0x00, /* Frequency synthesizer calibration. */
0x26, 0x1F, /* Frequency synthesizer calibration. */
0x29, 0x59, /* Frequency synthesizer calibration. */
0x2C, 0x81, /* Various test settings. */
0x2D, 0x35, /* Various test settings. */
0x2E, 0x09, /* Various test settings. */
0x03, 0x07, /* RXFIFO and TXFIFO thresholds. */
0x00, 0x29, /* GDO2 output pin configuration. */
0x02, 0x06, /* GDO0 output pin configuration. Refer to SmartRF® Studio User Manual for detailed pseudo register explanation. */
0x07, 0x04, /* Packet automation control. */
0x08, 0x05, /* Packet automation control. */
0x09, 0x00, /* Device address. */
0x06, 0xFF  /* Packet length. */
};

#include "pic18fregs.h"

#pragma stack 0x300 64

__code char __at(__CONFIG1H) conf1h = 0x08; // Select HS OSC
__code char __at(__CONFIG2L) conf2l = 0x00; // BOREN disabled, PWRTEN enabled
__code char __at(__CONFIG2H) conf2h = 0x00; // WDT disabled
__code char __at(__CONFIG3H) conf3h = 0x00; // digi RB pins, TMR1 normal
__code char __at(__CONFIG4L) conf4l = 0x81; // Disable LVP, disable ext. mode

// ------------------------------------------------
// a simple delay function

void delay_ms( long ms);
void CC1100_init( void);
unsigned char CC1100_read( unsigned char aAdr);
void CC1100_write( unsigned char aAdr, unsigned char aDat);
void putchar(unsigned char c);
void CharToAsc( unsigned int inp, unsigned char * out1, unsigned char * out2);

// --------------------------------------------------
// and our main entry point

void main()
{
  unsigned char i,j;
  unsigned char lnib, hnib;

  OSCCON = 0x72;
  LED_TRIS = 0;
  LED_PIN = 1;

    // USART
    PIE1bits.TXIE = 0; // (1 = enabled)
    PIE1bits.RCIE = 0; // (1 = enabled)
    TRISCbits.TRISC6 = 0; // (0 = pin set as output)
    TRISCbits.TRISC7 = 1; // (1 = pin set as input)
    RCSTA = 0x90;
    TXSTA = 0x22;
    BAUDCON = 0;
    //SPBRG = 0x40;
    SPBRG = 0x0C; // internal OSC 8MHz

    delay_ms(4000);

    CC1100_init();

    // read CONFIG bytes
    for( i = 0x0; i < 0x2f; i++) {
      // send 0xAA = 0xDD;_
      putchar('0');      putchar('x');
      CharToAsc( i, &hnib, &lnib);
      putchar( hnib);      putchar( lnib);      putchar('=');      putchar('0');      putchar('x');

      j = CC1100_read( i);
      CharToAsc( j, &hnib, &lnib);
      putchar( hnib);      putchar( lnib);      putchar(';');      putchar(' ');

    }
    putchar('\r');    putchar('\n');

    // read STATUS bytes
    for( i = 0xf0; i < 0xff; i++) {
      // send 0xAA = 0xDD;_
      putchar('0');      putchar('x');
      CharToAsc( i, &hnib, &lnib);
      putchar( hnib);      putchar( lnib);      putchar('=');      putchar('0');      putchar('x');

      j = CC1100_read( i);
      CharToAsc( j, &hnib, &lnib);
      putchar( hnib);      putchar( lnib);      putchar(';');      putchar(' ');

    }
    putchar('\r');    putchar('\n');

    // end of the test
    for(;;) {
        LED_PIN = 0;
        delay_ms(700);
        LED_PIN = 1;
        delay_ms(50);
    }
}

void CharToAsc( unsigned int inp, unsigned char * out1, unsigned char * out2)
{

  *out1 = ((inp&0xf0) >> 4) + 0x30;
  if (*out1 > 0x39) *out1 += 7;
  *out2 = (inp&0x0f) + 0x30;
  if (*out2 > 0x39) *out2 += 7;
}

void delay_ms( long ms)
{
    long i;

    while (ms--)
        for (i=0; i < 170; i++)
            ;
}

void putchar(unsigned char c)
{
	// Wait for TXREG to be ready
	while(!PIR1bits.TXIF);
	
	// Place char in TXREG - this starts transmition
	TXREG = c;
	
}

/* CC1100 init
 *
 */
void CC1100_init( void)
{
  unsigned char i, j;

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

  SSPSTAT = 0x40;
  SSPCON1 = 0x02;

  // RESET the CC1100 chip
  PORTCbits.RC3 = 0;
  PORTCbits.RC5 = 0;

  CC1100ENB_PIN = 0;
  __asm
    nop
  __endasm;

  CC1100ENB_PIN = 1;
  delay_ms(1);


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


  // send SRES
  SSPBUF = 0x30; // SRES strobe
  while ( 0 == SSPSTATbits.BF) ; // wait until end of transfer
  i = SSPBUF;
  while ( 1 == PORTCbits.RC4) ; // wait for SO goes low again
  CC1100ENB_PIN = 1;

  // send configuration
  i = 0;
  while (i < CC1100_CONFIG_LEN) {

    j = CC1100_REG_CONFIG[i]; // send address
    i++;

    CC1100_write(j, CC1100_REG_CONFIG[i]); // send data
putchar(';');
putchar(' ');
    i++; // increment is here to have some more delay
  }
putchar('\r');
putchar('\n');

}

void CC1100_write( unsigned char aAdr, unsigned char aDat)
{
  unsigned char i, tmp, tmp1, tmp2;

  tmp= 0;

    SSPCON1 = 0x00;

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

CharToAsc( aAdr, &tmp1, &tmp2);   putchar( tmp1);    putchar( tmp2);
putchar('=');
CharToAsc( aDat, &tmp1, &tmp2);    putchar( tmp1);    putchar( tmp2);

    SSPCON1 = 0x22;

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

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

    CC1100ENB_PIN = 1;
}

unsigned char CC1100_read( unsigned char aAdr)
{
  unsigned char tmp, tmp1, tmp2;

    SSPCON1 = 0x00;
    CC1100ENB_PIN = 0;
    while ( PORTCbits.RC4 != 0) ; // wait for SO goes low again */
    SSPCON1 = 0x22;


    SSPBUF = aAdr | 0x80; // send address
    while ( SSPSTATbits.BF == 0) ; // wait until end of address transfer
    tmp = SSPBUF;
    //CharToAsc( tmp, &tmp1, &tmp2);    putchar( tmp1);    putchar( tmp2);    putchar( '_');
    SSPBUF = 0x11; // send dummy data
    while ( SSPSTATbits.BF == 0) ; // wait until end of data transfer
    tmp = SSPBUF;
    //CharToAsc( tmp, &tmp1, &tmp2);    putchar( tmp1);    putchar( tmp2);    putchar( '_');

    CC1100ENB_PIN = 1;

    return tmp;
}
-------------------------------------------------------------------------
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