Spark Logic

SPI-3 wires

miki Wed May 18, 2016 10:30 pm

Hi,
I need some help.I want to use 3 wire spi comm with a rf module.
I need to use MOSI pin, bidirectional for write and read .How can I do it ?I want to use HW spi .bitbaging I know to do it.
I have a mini maple board.

Replies

racemaniac Thu May 19, 2016 1:34 pm

To be honest, i think that if he gave it a try, and came for us for help when trying but having a hard time, it’ll be doable and he’ll get some good help :).
I’ve very recently been working with the SPI port, and its current implementations (and have made my own implementation building further on libmaple code), so if he has problems, i can for sure help him out.
If i spend a weekend on it, i can probably also get it working myself (but i just don’t have the time to do that atm -_-).

Forums follow a certain karma: you’ll get as much respons as you’re showing effort. If your question is: “i want this, and you should make it for me”, then the answer is “no”. If your question is “i’m trying to get this to work, but i’m stuck here, or don’t understand how that works”, you’ll get help from people who’ve been doing the same thing and know it better than you :).
Atm he’s pretty much asking the first thing, and i’ve got my own projects & code to care about, so i don’t have the time to implement that. If he’s stuck in implementing it himself, i’ve got a lot of experience with those things i’d love to share :).

miki Thu May 19, 2016 1:41 pm

Wow,…. I managed to get out 3-rd page out of you guys.That means there is some interest. Lol!
I read briefly the reference.It seems easy to implement at a lower level.I will post some code soon.
Thanks!

mrburnette Thu May 19, 2016 1:52 pm

miki wrote:
<…>
I read briefly the reference.It seems easy to implement at a lower level.I will post some code soon.
Thanks!

racemaniac Thu May 19, 2016 1:54 pm

miki wrote:Wow,…. I managed to get out 3-rd page out of you guys.That means there is some interest. Lol!
I read briefly the reference.It seems easy to implement at a lower level.I will post some code soon.
Thanks!

martinayotte Thu May 19, 2016 2:01 pm

BTW, @miki, I don’t think you’ve really mentioned which RF module you are using …
Which one is it ?

miki Thu May 19, 2016 2:11 pm

I pull up some general code.I”m afraid going in maple.I’m used to work with avr registers.But it seems not much different.
Maybe I can combine arduino for init function and in the rest use manipulate registers.

uint8_t spi_read(){
uint8_t rx;
while(!(SPI_SR & (1<<TXE));
while(!(SPI_SR & (1<<BSY));
SPI_DISABLE();
SPI_SET_BIDIRECTIONAL();
SPI_ENABLE();
while(!(SPI_SR&(1<<RXNE));
rx=SPI_DR;
SPI_DISABLE();
SPI_SET_UNIDIRECTIONAL();
SPI_ENABLE();
return rx;
}

void SPI_ENABLE(){
SPI_CR1 |= (1<<SPE0);//enable SPI
}

void SPI_DISABLE(){
SPI_CR1 &= ~(1<<SPE0);//disable SPI
}

void SPI_SET_BIDIRECTIONAL()
{
SPI_CR1 |= (1<<BIDIMODE);
SPI_CR1 &= ~(1<<BIDIOE);//receive only
}

void SPI_SET_UNIDIRECTIONAL()
{
SPI_CR1 &= ~(1<<BIDIMODE);
}

GrumpyOldPizza Thu May 19, 2016 3:26 pm

miki wrote:I pull up some general code.I”m afraid going in maple.I’m used to work with avr registers.But it seems not much different.
Maybe I can combine arduino for init function and in the rest use manipulate registers.

GrumpyOldPizza Thu May 19, 2016 3:39 pm

GrumpyOldPizza wrote:miki wrote:I pull up some general code.I”m afraid going in maple.I’m used to work with avr registers.But it seems not much different.
Maybe I can combine arduino for init function and in the rest use manipulate registers.

miki Thu May 19, 2016 4:01 pm

I don’t need to going faster 4.5Mhz on SPI2 div8 is enough.

GrumpyOldPizza Thu May 19, 2016 4:08 pm

miki wrote:I don’t need to going faster 4.5Mhz on SPI2 div8 is enough.

miki Thu May 19, 2016 4:43 pm

I have to test it when I’m getting back home.Yes start with lower speed.
Thanks.

GrumpyOldPizza Mon May 23, 2016 6:03 pm

miki wrote:I have to test it when I’m getting back home.Yes start with lower speed.
Thanks.

miki Tue May 31, 2016 4:50 pm

Sorry for delay ,
Yes I was able to test today and it is working ok Compile very well in arduino IDE 1.6.5
See below my code for SPI2

#include <SPI.h>
SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port

void initSPI2() {
//SPI_DISABLE();
SPI_2.end();
SPI2_BASE->CR1 &=~SPI_CR1_DFF_8_BIT;//8 bits format This bit should be written only when SPI is disabled (SPE = �0�) for correct operation.
SPI_2.begin(); //Initialize the SPI_2 port.
SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0
SPI_2.setClockDivider(SPI_CLOCK_DIV32); //// Slow speed (36 / 32 = 1.125 MHz SPI_2 speed) It is not worlking witvDIV16 and higher
}

uint16_t _spi_write(uint16_t command){
SPI2_BASE->DR = command;
while (!(SPI2_BASE->SR & SPI_SR_RXNE));
//SPI_2.transfer(command);//working also with this one only
return SPI2_BASE->DR;
}

uint8_t _spi_read(){

uint8_t rx=0;
SPI_DISABLE();
SPI_SET_BIDIRECTIONAL();
SPI_ENABLE();
while(!(SPI2_BASE->SR& SPI_SR_RXNE));
rx=SPI2_BASE->DR;
SPI_DISABLE();
SPI_SET_UNIDIRECTIONAL();
SPI_ENABLE();
return rx;
}

void SPI_ENABLE(){
SPI2_BASE->CR1 |= SPI_CR1_SPE;//enable SPI
}

void SPI_DISABLE(){
SPI2_BASE->CR1 &= ~SPI_CR1_SPE;//disable SPI
}

void SPI_SET_BIDIRECTIONAL()
{
SPI2_BASE->CR1 |= SPI_CR1_BIDIMODE;
SPI2_BASE->CR1 &= ~ SPI_CR1_BIDIOE;//receive only
}

void SPI_SET_UNIDIRECTIONAL()
{
SPI2_BASE->CR1 &= ~SPI_CR1_BIDIMODE;
}

The spi speed cannot be increased more than 1.125 MHz clock DIV32.The A7105 gives error byte readings.
Another thing I observed there is a pain in the ass to find a tutorial explaining how SPI port other than SPI1 to be set.The original maple HardwareSPI class is not usable anymore.

mrburnette Tue May 31, 2016 7:22 pm

Another thing I observed there is a pain in the ass to find a tutorial explaining how SPI port other than SPI1 to be set.The original maple HardwareSPI class is not usable anymore.

http://www.stm32duino.com/viewtopic.php?t=278

miki Tue May 31, 2016 7:31 pm

Thanks.

Yes I found how to implement it in your examples.

miki Tue May 31, 2016 9:24 pm

The SPi it is working but it is too slow.
1.125 Mhz is too slow for the speed I need.I need faster at least double.

Vassilis Tue May 31, 2016 11:37 pm

miki wrote:The SPi it is working but it is too slow.
1.125 Mhz is too slow for the speed I need.I need faster at least double.

miki Wed Jun 01, 2016 12:02 am

I know how to set the the divisor and it is 36/4 -9Mhz
SPI2 afaik it is on 36Mhz bus on F10x.
The problem is not that ,I received garbage , if I increase the speed more than DIV32

RogerClark Wed Jun 01, 2016 1:22 am

miki wrote:
The problem is not that ,I received garbage , if I increase the speed more than DIV32

miki Wed Jun 01, 2016 1:31 am

The STM32 produced garbage at reading A7105 reg
However I played a little with the same code and I fix it .
Now I can go div4 and received normal.

I started liking the arduino with STM32 even if i had dig into maple library to play with regs.Anyway I did the same with avr’s.
It is very simple to make faster code ,compile and upload sketches.

Sign in to reply