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

18F67J50 SPI2

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



Joined: 28 Oct 2009
Posts: 4

View user's profile Send private message

18F67J50 SPI2
PostPosted: Wed Oct 28, 2009 11:58 am     Reply with quote

Hello, I'm working with a 18F67J50. I want to interface with an SD card over the SPI2 port (port D pins). I have successfully targeted the 18F6722 and am porting to the J50 because of the need for USB.

The problem is I never see any activity on the clock pin (D6) at all.

The SPI1 port works.

Before posting code and such, has anyone ever encountered problems with the 18F67J50 and SPI2?

BTW, I'm using the #use spi version not setup_spi2

#use spi(MASTER,CLK=MMCSD_PIN_SCL, DO=MMCSD_PIN_SDO, DI=MMCSD_PIN_SDI, BITS=8, MODE=3, stream=mmcsd_spi, FORCE_HW)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 28, 2009 4:02 pm     Reply with quote

Post your compiler version. It's given at the top of the .LST file.
It's a 4-digit number in this format: x.xxx
chrisa



Joined: 28 Oct 2009
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 29, 2009 7:11 am     Reply with quote

The compiler version is 4.099
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 29, 2009 11:12 am     Reply with quote

Quote:
#use spi(MASTER,CLK=MMCSD_PIN_SCL, DO=MMCSD_PIN_SDO, DI=MMCSD_PIN_SDI, BITS=8, MODE=3, stream=mmcsd_spi, FORCE_HW)

Post the #define statements for these 3 pins.
chrisa



Joined: 28 Oct 2009
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 29, 2009 11:41 am     Reply with quote

#define MMCSD_PIN_SCL PIN_D6 //o
#define MMCSD_PIN_SDI PIN_D5 //i
#define MMCSD_PIN_SDO PIN_D4 //o
#define MMCSD_PIN_SELECT PIN_D7 //o

The program uses mmcsd.c written by CCS as the driver to an mmc card
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 29, 2009 3:07 pm     Reply with quote

It's buggy. It's accessing the wrong registers for SPI2. I compiled it
with vs. 4.099 and looked at the .LST file. Here's the code that is
supposed to write to SSPBUF2 to do the transfer:
Code:

.... #use spi(MASTER, CLK=MMCSD_PIN_SCL, DO=MMCSD_PIN_SDO, DI=MMCSD_PIN_SDI, BITS=8, MODE=3, stream=mmcsd_spi, FORCE_HW)

00004:  MOVF   F66,W  // PMDIN1L (*** Bug - should be SSP2BUF)
00006:  MOVFF  07,F66 // PMDIN1L (*** Bug - should be SSP2BUF)
0000A:  BTFSS  F64.0  // USTAT (*** Bug - should be SSP2STAT)
0000C:  BRA    000A
0000E:  MOVFF  F66,01 // PMDIN1L (*** Bug - should be SSP2BUF)
00012:  GOTO   004E (RETURN)


The start-up code is also partially incorrect:
Code:

00020:  BCF    F95.4
00022:  BSF    F95.5
00024:  BCF    F95.6
00026:  MOVLW  30
00028:  MOVWF  F63  // UEIR (*** Bug - should be SSP2CON1)
0002A:  MOVLW  00
0002C:  MOVWF  F64  // USTAT (*** Bug - should be SSP2STAT)


spi_write2() does access the correct registers. However, the mmcsd.c
file is written for spi_xfer(), so this doesn't help directly.
Code:

... spi_write2(0x55);
00064:  MOVF   F6F,W  // SSP2BUF
00066:  MOVLW  55
00068:  MOVWF  F6F   // SSP2BUF
0006A:  RRCF   F6D,W  // SSP2STAT
0006C:  BNC   006A



However, I think you can do a work-around by doing this:

1. Comment out the #use spi() statement in the mmcsd.c file.

2. Setup the SPI mode, etc., with a setup_spi2() statement near the
start of main() in your application program. I looked at the .LST file,
and setup_spi2() to work OK.

3. Write your own spi_xfer() routine (that works). Put it above the
#include line for the mmcsd.c file.

Here's a framework for the work-around:
Code:

#include <18F67J50.h>
#fuses HS,NOWDT
#use delay(clock=20000000)

// SPI mode definition.
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

// We need a dummy stream for our own spi_xfer() function.
#define mmcsd_spi 1

// This spi_xfer() replaces the built-in function,
// which is buggy in vs. 4.099 for the 18F67J50
// when using #use spi() with SPI2.
int8 spi_xfer(int8 stream, int8 data)
{
int8 retval;

retval = spi_read2(data);

return(retval);
}

#define MMCSD_PIN_SCL    PIN_D6    // o
#define MMCSD_PIN_SDI    PIN_D5    // i
#define MMCSD_PIN_SDO    PIN_D4    // o
#define MMCSD_PIN_SELECT PIN_D7    // o

// Remember to comment out both #use spi() statements in mmcsd.c
#include <mmcsd.c>


//======================================
void main(void)
{
int8 result;

setup_spi2(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_4);

mmcsd_init();

// Put other mmcsd function calls here.

while(1);
}
chrisa



Joined: 28 Oct 2009
Posts: 4

View user's profile Send private message

PostPosted: Fri Oct 30, 2009 1:11 pm     Reply with quote

Thank you for the help. Although this instance still isn't working, we are seeing a clock and data on the pins. That was the first obstacle.
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