HardwareSPI

Working libraries, libraries being ported and related hardware
Post Reply
avong
Posts: 3
Joined: Sat Apr 22, 2017 7:00 am

HardwareSPI

Post by avong » Wed May 03, 2017 8:30 pm

I tried porting DMD library to Generic STM32F103C.
The attached DMD library is an edited version
DMDSTM.rar
(16.6 KiB) Downloaded 50 times
, I have attached snip of major editing done.

The major issue, within the DMD cpp file;
I included header file; #Include<HardwareSPI.h>
and then created instance; HardwareSPI spi(1);
In the DMD cpp file, whenever i call spi.begin(SPI_4_5MHZ, MSBFIRST, 0);
my timer will freeze.

in trying to figure out, i created the sketch, in the sketch i created
i added;

Code: Select all

#include <HardwareSPI.h> 
HardwareSPI spi(1);
spi.begin(SPI_4_5MHZ, MSBFIRST, 0);
alongside the HardwareTimer instance, this time there was no issue.
I now concluded, i am having lacking skills in handling how to call and manage methods in an existing library.
Please help me out, thanks in anticipation.

stevestrong
Posts: 1601
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: HardwareSPI

Post by stevestrong » Wed May 03, 2017 10:38 pm

You have to include the SPI.h header instead of HardwareSPI.h:

Code: Select all

#include <SPI.h>
And do not need to create any SPI instance, is done automatically when you include the header file.
I would recommend to use

Code: Select all

SPI.beginTransaction(SPISettings(4500000, MSBFIRST, DATA_MODE0));
...
SPI.write(0x55);

avong
Posts: 3
Joined: Sat Apr 22, 2017 7:00 am

Re: HardwareSPI

Post by avong » Thu May 04, 2017 2:06 pm

Thanks Steve,
That were i actually from,
I had tried #include <SPI.h> within the DMD.cpp file.
I then called SPI.write( .......);

Each time i tried that the timer freeze to call the function that refreshes the screen.

HardwareSPI is my last bet now.

i am asking for better method to instantiate HardwareSPI on SPI port 1, within the DMD.cpp file.

stevestrong
Posts: 1601
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: HardwareSPI

Post by stevestrong » Thu May 04, 2017 2:15 pm

Which repository are you using? The official Arduino_STM32 from Roger? Which version?

ag123
Posts: 740
Joined: Thu Jul 21, 2016 4:24 pm

Re: HardwareSPI

Post by ag123 » Thu May 04, 2017 4:17 pm

using the library
https://github.com/rogerclarkmelbourne/ ... raries/SPI
and include <SPI.h> is hardware SPI *highly optimized* nothing less,
credit goes to roger, victor, steve, serasidis etc, and possibly many more who perhaps helped to test those libraries like ILI9341 spi / sdfat etc
https://github.com/rogerclarkmelbourne/ ... raries/SPI

i'm not too sure if this would help but you may like to see if this may be related to the SPI speeds

e.g. instead of
SPI.setClockDivider(SPI_CLOCK_DIV2);
https://github.com/freetronics/DMD/blob ... MD.cpp#L48

Code: Select all

// system clock / 4 = 4MHz SPI CLK to shift registers. If using a short cable, can put SPI_CLOCK_DIV2 here for 2x faster updates
SPI.setClockDivider(SPI_CLOCK_DIV4); 
you could possibly use dividers like
SPI_CLOCK_DIV4, SPI_CLOCK_DIV8, SPI_CLOCK_DIV16 etc

if on the original processors the system clock is 16 mhz, dividing by 4 gives 4 mhz. lets just say that the system clock runs at 72mhz on stm32f103
72/4 gives 18 mhz pretty much more than 4 times the original expected speed

some one would need to correct this as i'm pretty much novice as well, lets say stm32 runs at 72mhz system clock
setting SPI_CLOCK_DIV16 gives 72/16 ~ 4.5 mhz (note, i'm not really sure if we are after all dividing 72mhz by the spi clock divider as APB buses could have prescalers etc, but i'm not too sure how to check that)

another thing though, it seemed DMD are specified for 5V operation, stm32 are 3.3v devices, be careful or it may 'fry' the stm32, some kind of 'level shifters' plus interfacing resistors (e.g. pull up to 5v etc) may be needed
https://www.freetronics.com.au/collecti ... -32x16-red

just 2 cents, hope it helps

btw stm32 'runs too fast' especially if u consider an overclocked f4 @ 240 mhz some of the slower processors & peripherals are panting and sweating trying to chase up :lol:
Last edited by ag123 on Thu May 04, 2017 5:06 pm, edited 3 times in total.

avong
Posts: 3
Joined: Sat Apr 22, 2017 7:00 am

Re: HardwareSPI

Post by avong » Thu May 04, 2017 4:31 pm

Yes, i am using Rep from Roger "Arduino_STM32",

I just implemented just the way you advised but still having issues; this is brief of the important areas of the code.
in my DMD.cpp, this is what i did

Code: Select all

#include<SPI.h>
SPIClass spi(1);
i added this line in the initialization of DMD;

Code: Select all

spi.beginTransaction(SPISettings(4500000, MSBFIRST, SPI_MODE_0));
Still in the DMD library, there is a function called refresh, this function is tied to timer 1 at 5000Hz
SPI1 initialization.

Code: Select all

spi.beginTransaction(SPISettings(4500000, MSBFIRST, SPI_MODE_0));

void DMD::refresh(){
    ......
    ....... 
    spi.write(......);
    ...................
    ..................
}
/////////////////////////////////////////////////////////////////////////////////////////////////
Now in my IDE, I created a sketch, within the setup i did something like;

Code: Select all

HardwareTimer Timer(1);  // create instance of Timer 1

DMD display;                    // Instance of DMD

void setup(){
    Timer.pause();
    Timer.setPeriod(5000);               // in microseconds
    Timer.setMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);
    Timer.setCompare(TIMER_CH1, 1);           // Interrupt 1 count after each update
    Timer.attachInterrupt(1, dmdUpdate);
    Timer.refresh();
    Timer.resume();
}
 
void dmdUpdate(){
  display.refresh();
}
If i disable the line, //spi.beginTransaction(SPISettings(4500000, MSBFIRST, SPI_MODE_0));
my timer will be working, if i enable the line spi.beginTransaction(SPISettings(4500000, MSBFIRST, SPI_MODE_0));
the timer stops.
Capture.PNG
Capture.PNG (26.36 KiB) Viewed 791 times

stevestrong
Posts: 1601
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: HardwareSPI

Post by stevestrong » Thu May 04, 2017 9:56 pm

Your timer is generating an interrupt each 5 milliseconds, right?
It can maybe happen, that the ISR gets nested, because it cannot finish the job within that short time.
Will spi.write work on main levet, for example, in setup(), or loop()?
As an alterantive you could use spi.transfer() function instead spi.write().

Another alternative is to try the spi lib from my repo: https://github.com/stevstrong/Arduino_STM32
I remember there was an issue with not readed value and other stuff which is not yet updated in Rogers repository (PR ongoing).

haris
Posts: 2
Joined: Sat Aug 12, 2017 10:40 pm

Re: HardwareSPI

Post by haris » Sat Aug 12, 2017 10:44 pm

avong wrote:
Wed May 03, 2017 8:30 pm
I tried porting DMD library to Generic STM32F103C.
The attached DMD library is an edited versionDMDSTM.rar, I have attached snip of major editing done.

The major issue, within the DMD cpp file;
I included header file; #Include<HardwareSPI.h>
and then created instance; HardwareSPI spi(1);
In the DMD cpp file, whenever i call spi.begin(SPI_4_5MHZ, MSBFIRST, 0);
my timer will freeze.

in trying to figure out, i created the sketch, in the sketch i created
i added;
#Include<HardwareSPI.h>
HardwareSPI spi(1);
spi.begin(SPI_4_5MHZ, MSBFIRST, 0);
alongside the HardwareTimer instance, this time there was no issue.
I now concluded, i am having lacking skills in handling how to call and manage methods in an existing library.
Please help me out, thanks in anticipation.
hello thanks for your code, can you tell me how its works ..? i mean how cable connected toP10
for example

STM32 P10

1 A
2 B

many hanks

Post Reply