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 support@ccsinfo.com

Two SPI streams, same mode

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



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

Two SPI streams, same mode
PostPosted: Fri Jul 08, 2022 12:55 pm     Reply with quote

PIC18F26K22
CCS version 5.094

Tilt sensor SCL3300
EEPROM 93LC66 in 8bit mode

I have the above two devices on SPI2 of a 18F26K22.

my SPI set up is as follows:

Code:
#use spi(master, SPI2, mode=0, baud=2000000, stream=SPI2_TILT, bits=32, MSB_FIRST) // SPI Call for Tilt Sensor
#use spi(master, SPI2, mode=0, baud=2000000, stream=SPI2_EEPROM, bits=8, FORCE_SW) // SPI Call for EEPROM


both devices are mode 0 just different speeds and different select lines.

The select line for the Tilt sensor is C1 and the select for the EEPROM is A2.

separately, each chip works fine. The tilt sensor will work fine with the EEPROM hardware present and the EEPROM stream defined.

Problem is the EEPROM will not work properly with the Tilt stream defined, comment it out and the EEPROM works fine even with the Tilt chip connected to the SPI. Problem is, I am only reading 0xFF from the EEPROM with the Tilt stream defined.

There seems to be a lot of discussion about two streams using use_spi calls. Is this still a problem on the 5.094V and the PIC18F26K22?

Is the work around to use the SETUP_SPI method?

I can post more code if needed, just starting with the use_spi questionable behavior with two streams.
I do not think there is a way to just use one stream for the two devices as the tilt sensor wants 32b data and is quite particular about data transfer timing.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 1:05 pm     Reply with quote

Specifying SPI2, says 'FORCE_HW'. Get rid of this for the second channel,
and specify the pins.

An SPIx selection specifies to use the physical hardware,
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 1:22 pm     Reply with quote

Probably doing this wrong, but here is how I interpreted your comment:

Code:
#use spi(master, SPI2, mode=0, baud=2000000, stream=SPI2_TILT, bits=32, MSB_FIRST) // SPI Call for Tilt Sensor
#use spi(master, DO=Pin_B3, CLK=PIN_B1, DI=PIN_B2, mode=0, baud=2000000, stream=SPI2_EEPROM, bits=8, FORCE_SW) // SPI Call for EEPROM


Still reading 0XFF from the EEPROM. The EEPROM setup does work with the TILT setup disabled as before.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 2:27 pm     Reply with quote

OK
Here is what worked and everyone (Tilt Sensor and EEPROM) are being nice neighbors and working correctly now.

Code:
#use spi(master, DO=Pin_B3, CLK=PIN_B1, DI=PIN_B2 ,mode=0, baud=2000000, stream=SPI2_TILT, bits=32, MSB_FIRST, FORCE_SW) // SPI Call for Tilt Sensor
#use spi(master, DO=Pin_B3, CLK=PIN_B1, DI=PIN_B2 ,ENABLE_ACTIVE=1, ENABLE_DELAY=20,mode=0, baud=100000, stream=SPI2_EEPROM, bits=8, FORCE_SW) // SPI Call for EEPROM


The thing that made it all start working was taking the SPI2 setup out of the TILT stream setup. I also added a line to my main that allowed PIN_A2 to float.

Still open for discussion on why this works. Otherwise I will take it as a coding mystery that just is.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 2:45 pm     Reply with quote

CS on the tilt sensor is low true. CS on the eeprom is high true.
Does your code do this ? You never showed us your code for that.

Also, you did more than just remove 'SPI2' from the tilt sensor stream.
You made it into software SPI so both streams are now software SPI.

I'm wondering if the CS active levels are the cause of the whole problem.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 3:24 pm     Reply with quote

True, I did only change one thing at a time, just posting where I was at when it all started working, I will start taking other stuff out.
I bit bang the CS pins.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2022 3:31 pm     Reply with quote

I understand that you bitbang CS. But do you set CS active high for the eeprom ? ie:
Code:

output_high(EEPROM_CS);
spi_xfer(etc);
output_low(EEPROM_CS)
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Jul 11, 2022 2:03 am     Reply with quote

There is a key bit of understanding here.

If a _peripheral_ has control of a set of pins, the output ones cannot
be used for normal I/O.
So a software SPI cannot be run on the same pins as a hardware SPI,
while the latter is enabled.

This setup:
Code:

#use spi(master, SPI2, mode=0, baud=2000000, stream=SPI2_TILT, bits=32, MSB_FIRST) // SPI Call for Tilt Sensor
#use spi(master, DO=Pin_B3, CLK=PIN_B1, DI=PIN_B2, mode=0, baud=2000000, stream=SPI2_EEPROM, bits=8, FORCE_SW) // SPI Call for EEPROM


Will merrily work, _but_ you have to disable the hardware stream
before you use the software stream. Use the spi_init command to disable
the hardware module before you talk to the software SPI.
Code:

    spi_init(SPI2_TILT, FALSE);
    //now you can use the EEPROM stream.

    //Then when you have finished with the EEPROM use
    spi_init(SPI2_TILT, TRUE); 
    // to re-enable the hardware SPI


It does all seem silly though. Why not just use the same stream for both
devices?. Set up just the single stream with 32bit selected, and use this
for both devices. Just specify to only transfer 8 bits in the spi_xfer
commands for the EEPROM device. Optional last value for this is the
number of bits to actually send.
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