View previous topic :: View next topic |
Author |
Message |
CRH Guest
|
PIC24 SPI |
Posted: Mon Jul 27, 2009 3:29 pm |
|
|
I'm trying to setup a software SPI connection to M25PE20 external flash.
spi pin selects:
Code: | #pin_select SDI1 = PIN_D9
#pin_select SDO1 = PIN_D8
#pin_select SCK1OUT = PIN_D10
#pin_select SS1OUT = PIN_B9
#use spi(SPI1, MASTER, SAMPLE_FALL, bits=8, STREAM=SPIA)
|
In my main:
Code: | int i;
char arr[10];
char arr2[10];
init_device();
setup_spi(SPI_MASTER | SPI_CLK_DIV_64);
arr[0]=0x06; //enable write
arr[1]=0x0A; //write command
arr[2]=0x00; //address part1
arr[3]=0x00; //address part2
arr[4]=0x01; //address part3
arr[5]='H';
arr[6]='E';
arr[7]='L';
arr[8]='L';
arr[9]='O';
output_low(PIN_B9); //chip select
for(i=0;i<10;i++) spi_write(arr[i]); //write to flash
output_high(PIN_B9);
delay_ms(10);
arr[0]=0x03; //read command
arr[1]=0x00; //address part1
arr[2]=0x00; //address part2
arr[3]=0x01; //address part3
output_low(PIN_B9); //chip select
for(i=0;i<4;i++) spi_write(arr[i]); //send read command
for(i=0;i<5;i++) arr2[i]=spi_read(0); //read back first 5 characters
fputs(arr2,USER); //print string
|
I can't tell if anything is working in this code portion. When I print the string from flash it prints all nulls. |
|
|
CRH Guest
|
|
Posted: Mon Jul 27, 2009 3:33 pm |
|
|
I forgot to mention:
I'm using a PIC24FJ256GB106 with the PCD compiler from CCS |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 27, 2009 4:58 pm |
|
|
Quote: |
#use spi(SPI1, MASTER, SAMPLE_FALL, bits=8, STREAM=SPIA)
In my main:
setup_spi(SPI_MASTER | SPI_CLK_DIV_64);
|
You should not mix different methods of setting up the SPI module.
I suggest that you get rid of the #use spi() statement and just use
the setup_spi() only. Also, in your setup_spi() statement, you should
specify the SPI Mode. According to the M25PE20 data sheet, it uses
modes 0 or 3. See page 10. (Mode 0 = 0,0 and Mode 3 = 1,1).
http://www.numonyx.com/Documents/Datasheets/M25PE20_10.pdf
Here is a thread about configuring the setup_spi() function:
http://www.ccsinfo.com/forum/viewtopic.php?t=31594 |
|
|
CRH Guest
|
|
Posted: Mon Jul 27, 2009 5:46 pm |
|
|
SPI_XMIT_L_TO_H is not valid on the PIC24, what would you recommend using instead? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 27, 2009 6:15 pm |
|
|
You're right. The PCD manual only lists SPI_L_TO_H and SPI_H_TO_L
as options. That reflects that CKE bit in the SSP control register.
They don't appear to allow any control over the CKP bit, which controls
the idle state of the SCLK signal.
I don't have the PCD compiler, so I can't do any of my normal analysis
on the problem. I probably can't help too much more. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Jul 28, 2009 1:26 am |
|
|
There's a previous thread on PIC24 hardware SPI http://www.ccsinfo.com/forum/viewtopic.php?t=38491
My suggestion is to use spi_read() for read and write action, as shown in the examples. It's working perfectly in my applications, e.g. with serial SPI flash memory. |
|
|
|