SPI partly working when using hardware, library or code CS management

Post here first, or if you can't find a relevant section!
Post Reply
mrguen
Posts: 12
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 1

SPI partly working when using hardware, library or code CS management

Post by mrguen »

Hello,

Maybe some of you know well the SPI?

I am trying to communicate between STM32F401RCT6 and an ADC module MCP3561.

The expected com on SPI is attached at the end of the message.

The SPI used is SPI2 with alternate pins

Code: Select all

const byte SPI_MOSI = PC3;
const byte SPI_MISO = PC2;
const byte SPI_SCK = PB13;
const byte SPI_CS = PB12;
const uint32_t spiClk = 5000000; // or 10 MHz
byte passedValue = (MCP3561DeviceAddress << 6) & REGISTER_CONFIG1 << 2 & COMMAND_STATIC_READ_REGISTER;
1) It is partly working when using the hardware CS pin. The device answers correctly. But since the CS pin probably goes up after each transfer I can't read the data after sending a command.

Code: Select all

SPIClass mySPI2(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
SPISettings mySPIsettings = SPISettings(spiClk, MSBFIRST, SPI_MODE0); 

mySPI2.begin();
mySPI2.beginTransaction(mySPIsettings);

returnedValue =  mySPI2.transfer(passedValue);
// The returned value is as expected (returning the MCP3561DeviceAddress): 00011111

returnedData =  mySPI2.transfer(0);
// Not correct all "1" . The device is supposed to send the data immediatly after receiving and answering to the command, never mind what is on MOSI.  But indeed if CS goes high MISO is supposed to return to HIGH. Or maybe I didn't catch the data ? (I did not find a shiftIn method).

mySPI2.endTransaction();


2) Using the CS management in the library: not working

Code: Select all

SPIClass mySPI2(SPI_MOSI, SPI_MISO, SPI_SCK);
SPISettings mySPIsettings = SPISettings(spiClk, MSBFIRST, SPI_MODE0); 

mySPI2.begin(SPI_CS);
mySPI2.beginTransaction(mySPIsettings);

returnedValue =  mySPI2.transfer(passedValue, SPI_CONTINUE);
// The returned value is NOT as expected - all "1"

returnedData =  mySPI2.transfer(0, SPI_LAST);
// The returned value is NOT as expected 

mySPI2.endTransaction();


3) Managing CS pin in the code: working as with hardware management

Code: Select all

pinMode(SPI_CS, OUTPUT);
digitalWrite(SPI_CS, HIGH);
   
SPIClass mySPI2(SPI_MOSI, SPI_MISO, SPI_SCK);
SPISettings mySPIsettings = SPISettings(spiClk, MSBFIRST, SPI_MODE0); 

mySPI2.begin();
mySPI2.beginTransaction(mySPIsettings);

digitalWrite(SPI_CS, LOW);
  
returnedValue =  mySPI2.transfer(passedValue);
// The returned value is as expected (returning the MCP3561DeviceAddress): 00011111

returnedData =  mySPI2.transfer(0);
// The returned value is  NOT as expected 

digitalWrite(SPI_CS, HIGH);

mySPI2.endTransaction();

What might be wrong? Also I could not find a shiftIn() method.
Attachments
SP2.gif
SP2.gif (38.99 KiB) Viewed 595 times
by mrguen » Wed Sep 02, 2020 12:28 pm
The command was not correct. I can have it work when managing the CS pin in the code. I did not tested more the other two methods.
Go to full post

User avatar
fpiSTM
Posts: 731
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 27
Location: Le Mans
Contact:

Re: SPI partly working when using hardware, library or code CS management

Post by fpiSTM »

Hi @mrguen
did you read the wiki:
https://github.com/stm32duino/wiki/wiki/API#spi

Maybe you need the SPI_CONTINUE ?

mrguen
Posts: 12
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 1

Re: SPI partly working when using hardware, library or code CS management

Post by mrguen »

@fpiSTM Yes I did read the WIKI and use SPI_CONTINUE in example 2, but maybe not correctly?

User avatar
fpiSTM
Posts: 731
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 27
Location: Le Mans
Contact:

Re: SPI partly working when using hardware, library or code CS management

Post by fpiSTM »

Well, like this I see nothing wrong. Anyway, I don't know this component (MCP3561),
is your value correct

Code: Select all

byte passedValue = (MCP3561DeviceAddress << 6) & REGISTER_CONFIG1 << 2 & COMMAND_STATIC_READ_REGISTER;

mrguen
Posts: 12
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 1

Re: SPI partly working when using hardware, library or code CS management

Post by mrguen »

@fpiSTM Yes this is a recent component (2019). So there is hardly any info available. I guess I have to analyze the signals. I will do some more test and let you know.

mrguen
Posts: 12
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 1

Re: SPI partly working when using hardware, library or code CS management

Post by mrguen »

The command was not correct. I can have it work when managing the CS pin in the code. I did not tested more the other two methods.

Post Reply

Return to “General discussion”