Skip to content

Need help in SPI. #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Vicky-S opened this issue Nov 10, 2018 · 8 comments
Closed

Need help in SPI. #370

Vicky-S opened this issue Nov 10, 2018 · 8 comments
Labels
question ❓ Usually converted as a discussion

Comments

@Vicky-S
Copy link

Vicky-S commented Nov 10, 2018

I got successfully programmed STM32F103 and I can able to do basic tasks.

Now I need help in using 2 SPI in STM32F103 at the same time. The STM32F103 has 2 SPI. Which is the default one uses Arduino SPI library and how can I initialize another SPI?

I need to access 2 SPI at the same program. I have tried to parallel the SPI devices in single library but its not working well. That's why I try to use 2 SPI.

@fpistm
Copy link
Member

fpistm commented Nov 10, 2018

By default SPI instance are mapped on SPI1 using:

#define PIN_SPI_SS              PA4
#define PIN_SPI_MOSI            PA7
#define PIN_SPI_MISO            PA6
#define PIN_SPI_SCK PA5

See

SPI2 is available on pin PB15/PB14/PB13.
See here

const PinMap PinMap_SPI_MOSI[] = {

To use the second one:
SPIClass SPITwo(PB15, PB14, PB13);
Then use it as you used the default SPI istance.
Note that you can't use the name SPI2 as it is defined by CMSIS.
More info are available here:
https://github.com/stm32duino/wiki/wiki/API#spi

@fpistm fpistm added the question ❓ Usually converted as a discussion label Nov 10, 2018
@fpistm
Copy link
Member

fpistm commented Nov 13, 2018

Hi @Vicky-S
Is this ok now?

@Vicky-S
Copy link
Author

Vicky-S commented Nov 17, 2018

Default SPI works well. But getting error in using 2nd SPI.

error: 'SPIClass' does not name a type

SPIClass SPITwo(PB15, PB14, PB13);

^~~~~~~~

exit status 1
'SPIClass' does not name a type

@fpistm
Copy link
Member

fpistm commented Nov 17, 2018

Looks strange....
No issue with this sketch:


#include <SPI.h>

SPIClass SPITwo(PB15, PB14, PB13);

void setup() {
  // put your setup code here, to run once:
  // initialize SPI:
  SPI.begin();
  SPITwo.begin();
}

void loop() {
  // put your main code here, to run repeatedly:

}

I assume you well include SPI.h.

@Vicky-S
Copy link
Author

Vicky-S commented Nov 17, 2018

Ok. Sorry my mistake. I didn't extern the SPI Library. It works fine.

@Vicky-S
Copy link
Author

Vicky-S commented Nov 17, 2018

Likewise how can I use several I2C and UART in STM32F103. ?

@Vicky-S Vicky-S closed this as completed Nov 17, 2018
@fpistm
Copy link
Member

fpistm commented Nov 17, 2018

HardwareSerial Serial2(USART2);
or
HardwareSerial Serial2(PA3, PA2);

Default I2C is on PB7/PB6 (D2/D3)

#define PIN_WIRE_SDA PB7

So using Wire instance will use those pin.
else you can use the second I2C like that:
TwoWire i2cTwo(PB11, PB10);
or if you want change the default pin for Wire instance call this before begin():

Wire.setSCL(PB10);
Wire.setSDA(PB11);
Wire.begin();

For uart/i2c/SPI, this is the same basic usage as Arduino except some API added which has been added for better flexibility. Example the setSDA/SCL. There is some other added for SPI (setMISO,...), serial (setRx,..)

Here I use stm32 pin name PXy but you can also use the pin number Dx or x.

@Vicky-S
Copy link
Author

Vicky-S commented Nov 17, 2018

Ok. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question ❓ Usually converted as a discussion
Projects
None yet
Development

No branches or pull requests

2 participants