Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Maple Mini, Maple Rev3, Maple Rev 5 and Maple Ret 6, iTead Maple etc
Post Reply
Morse Cold
Posts: 4
Joined: Thu Aug 15, 2019 5:32 pm
OS: Linux Mint
IDE: Arduino
Core: Roger's?
Board: Blue Pill

Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Post by Morse Cold » Fri Aug 16, 2019 4:13 am

I hope I'm in the right section and that the blue pill is considered a Maple Mini clone.
I've only just recently gotten to the point where I can use Roger's bootloader smoothly to upload sketches via USB. That is, I'm a green-as-grass STM32 newbie. I've been using my little Nokia 5110 LCD as my test device. It has a SPI interface, albeit one-way (there is no MISO). I've shunned the default SPI instance because I want to see explicitly which port I'm using. Fortunately this compiles...
SPIClass SPI_1(PA7, PA6, PA5);
SPIClass SPI_2(PB15, PB14, PB13);
after which I can indeed use SPI_1.begin() and SPI_1.transfer() to successfully control my LCD if I attach it to pins A5&A7
or I can use SPI_2.begin() and SPI_2.transfer() to control my LCD if I attach it to pins B13&B15.
So far so good, right? What's my concern? Well, this also compiles and doesn't prevent the other two from working...
SPIClass SPI_3(PA1, PA2, PA3);
I just pulled those pins out of thin air. They are not blue pill SPI pins. I haven't gone so far as to try attaching my LCD to A3&A1, though I suppose if I were to find the courage it might answer my final question below. The fact that my crazy test instantiation even compiles has left me with doubts about the other two. Am I really using SPI1 and SPI2? And if not, why does my LCD work when attached to the corresponding pins? Could I be confusing the driver and causing it to resort to simply bit banging the specified pins? Thanks in advance.

stevestrong
Posts: 60
Joined: Tue Mar 05, 2019 7:49 am
Location: Munich
OS: Win7 & 10
IDE: Arduino 1.8.8, Sloeber
Core: Libmaple
Board: Bluepill, Generic F4VET6 black & mini
Contact:

Re: Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Post by stevestrong » Fri Aug 16, 2019 7:53 am

too many questions in one thread.
Anyway, I doubt you are using Roger´s core because his SPI class does not have any method to specify the pins. How did you install the core?

Morse Cold
Posts: 4
Joined: Thu Aug 15, 2019 5:32 pm
OS: Linux Mint
IDE: Arduino
Core: Roger's?
Board: Blue Pill

Re: Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Post by Morse Cold » Fri Aug 16, 2019 11:26 am

Thank you for the prompt response. I can see that I have more homework to do. For starters I'd better figure out what a core is, which one I have, and just how I came to have it. :D

mrburnette
Posts: 90
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Post by mrburnette » Fri Aug 16, 2019 2:02 pm

Morse Cold wrote:
Fri Aug 16, 2019 11:26 am
Thank you for the prompt response. I can see that I have more homework to do. For starters I'd better figure out what a core is, which one I have, and just how I came to have it. :D
This will help, perhaps:
https://stm32duinoforum.com/forum/viewt ... _3111.html

A "core" is a set of functions and initialization routines that taken collectively create an API. In the above link,
#1 & #2 under Available cores are the recommended two STM32F103xx cores.

In considering between STM's and Roger's, understand that STM has a professional set of tools and software and to create an Arduino-ish version, essentially wrote C++ wrappers around their existing code. Roger's core was developed by Leaflabs and later abandoned; the Leaflabs' core is generally considered to be a monolithic software architecture. The Leaflabs Core morphed into Roger's Core after 4 years of forum use, abuse, and bug bashing. Roger hosts the official copy on github and has singularly been the gateway individual to effect changes and enhancements.

I have not been active (other than a forum annoyance) in a long time, but I went back to my MM code directory and this is the SPI2 test code from years back... no promises today:

Code: Select all

/* http://stm32duino.com/viewtopic.php?f=14&t=286&start=30

Arduino 1.7.8 on Linux Mint 17.3 Cinnamon tested 20160201
    Sketch uses 13,092 bytes (10%) of program storage space. Maximum is 122,880 bytes.
    Global variables use 2,768 bytes of dynamic memory.
*/
#include <Arduino.h>
#include <SPI.h>

SPIClass SPI_2(2); //Create an SPI2 object.

byte data;

void setup() {
  SPI_2.begin(); //Initiallize 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 (**) 
  SPI_2.setClockDivider(SPI_CLOCK_DIV16);      // Slow speed (72 / 16 = 4.5 MHz SPI speed)  

  pinMode(BOARD_SPI2_NSS_PIN, OUTPUT); // note: this must be after the SPI_2.begin() for gpio control of CSN
}

void loop() {
  //digitalWrite(BOARD_SPI2_NSS_PIN, LOW); // manually take CSN low for spi transmission
  gpio_write_bit(GPIOB, 12, LOW); // faster than digitalWrite()
  data = SPI_2.transfer(0x55); //Send the HEX data 0x55 over SPI-2 port and store the received byte to the <data> variable.
  //digitalWrite(BOARD_SPI2_NSS_PIN, HIGH); // manually take CSN high between spi transmissions
  gpio_write_bit(GPIOB, 12, HIGH);  // faster than digitalWrite()

  delayMicroseconds(10);    //Delay 10 micro seconds.
}
* The SPI library code (Roger) is here.


On the github site, I found this SPI reference in the WiKi:
https://github.com/rogerclarkmelbourne/ ... 2/wiki/SPI
which reads in part,
Enhancements to my original conversion of the Maple "HardwareSPI" library have been made by @timschuerewegen, who's comments are shown below.

I have made some improvements to the SPIClass class. The "setClockDivider" method needed weird and >limiting values (i.e. no way to set a 36 Mhz clock for SPI 1) and the "transfer" and "write" methods >were not waiting for the SPI busy flag to be cleared. I also added a "setFrequency" method.

SPI.setClockDivider(SPI_CLOCK_DIV2) gives you the fastest clock, 36 Mhz (SPI 1) or 18 Mhz (SPI 2) ... SPI.setClockDivider(SPI_CLOCK_DIV256) gives you the slowest clock, 281.250 Khz (SPI 1) or 140.625 Khz (SPI 2)

SPI.setFrequency(SPI_36MHZ) gives you a 36 Mhz clock (only supported by SPI 1) SPI.setFrequency(SPI_18MHZ) gives you a 18 Mhz clock ... SPI.setFrequency(SPI_281_250KHZ) gives you a 281.250 Khz clock SPI.setFrequency(SPI_140_625KHZ) gives you a 140.625 Khz clock (only supported by SPI 2)
As you are using a Nokia 5110 LCD, I attached two of my 4 year old test sketches from the old days of the original forum.
Nokia5100_test sketches.zip
(8.5 KiB) Downloaded 15 times
Here are my notes on the subject:

Code: Select all

/*
Nokia5510 library by Matthias Diro
settings tested on maple mini
  HW SPI and soft SPI tested
  do not use as slave (SCE) pin 7

Tested by R. Burnette 20180829
Arduino 1.8.6
  Sketch uses 17,764 bytes (16%) of program storage space. Maximum is 108,000 bytes.
  Global variables use 4,592 bytes of dynamic memory.
*/

#include "SPI.h"
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

// #define MAPLE 1// Some Aruino/Maple specific stuff
#define ADMAX   4095
#define ADPIN     15
#define PROGMEM


/*    Nokia 5110          Maple Mini STM32F103
  RED board      Signal      Signal (Pin#)
  ---------      -------     ----------
  Pin 1            Rst         Reset  ( 9)
  Pin 2            CE          SCE    (10)
  Pin 3            DC          D/C    ( 8)
  Pin 4            Din         MOSI   ( 4)
  Pin 5            Clk         SCLK   ( 6)
  Pin 6            Vcc
  Pin 7            LED
  Pin 8            GND
*/

byte dc_pin     = 8;    // Data/Command
byte reset_pin  = 9;
byte cs_pin     = 10;    // Chip Select

// Create a pcd8544 object using Hardware SPI
// (SPI1 on the Maple, add 2 as last argument for SPI2 (untested)).

Adafruit_PCD8544 display = Adafruit_PCD8544(dc_pin, reset_pin, cs_pin);

// For software SPI, any 5 pins.
// pcd8544 lcd(dc_pin, reset_pin, cs_pin, sdin_pin, sclk_pin);


// 2 rows by 16 cols smiley. First 16 bytes is first row,
// next 16 is second.
byte smile[] PROGMEM = {
	0xE0,0x18,0x04,0x04,0x02,0x32,0x32,0x02,0x02,0x22,0x22,
	0x02,0x04,0x04,0x18,0xE0,0x07,0x18,0x20,0x22,0x44,0x48,
	0x48,0x48,0x48,0x48,0x48,0x44,0x22,0x20,0x18,0x07};


void setup(void)
{
	pinMode(11, INPUT);

	display.begin();  // Always call display.begin() first.
	display.clearDisplay();

	// Fill the screen with characters
	for (char ch = '!'; ch <= 't'; ch++)
		display.print(ch);
	delay(2000);

	display.clearDisplay();
	// Set cursor to (character) column zero and row 0
	display.setCursor(0,0);
	display.println("Level: ");  // First line
	display.println("Temp: ");   // println skips to second line

	// Draw a double line
	//for (byte i = 0; i < display.width(16/2-2); i++)
		display.data(0b00010100);
	display.data(0); // A blink pixel column

	// Some small numbers
	//for (byte i = 0; i < 11; i++)
	//	display.smallNum(i);

	display.setCursor(0, 3);
	display.println(" PCD8544");
	display.println("    on");
#ifdef BOARD_maple
	display.print("the Maple");
#else
	display.print(" Arduino");
#endif
	// Draw an image.
	display.gotoRc(4, 57);
	display.drawBitmap(2, 16, smile);
}


void loop(void)
{
	byte i, val;

	// Draw bar on row 0, pixel column 8*5
	display.gotoRc(0, 8*5);
	val = map(analogRead(11), 0, 4096, 8*5, 84);

        // the bar
	for (i = 5*8; i < val; i++)
		display.data(0b01111100); // Bar
	display.clearRestOfLine();

	// Write a fake temperature
	// display.gotoRc(1, 8*5);
	display.print("22.8");
	display.data(0);

	// A degree sign
	display.data(0b00000100); // Degree
	display.data(0b00001010);
	display.data(0b00000100);
	display.data(0);
	display.print('C');
	display.clearRestOfLine();
	delay(100);
}

Ray

Added: For Roger's core, more examples specific to the Core are found here:
https://github.com/rogerclarkmelbourne/ ... s/examples

Also, a STM32F102 + Nokia barometer:
https://www.hackster.io/rayburne/bmp180 ... 6-0-45ad34
GLCD5110_BMP085.zip
(199.81 KiB) Downloaded 13 times
As Steve says, pin usage for SPI is fixed on the board: https://alselectro.wordpress.com/2018/1 ... uino-core/
Image

Andy2No
Posts: 27
Joined: Wed Aug 21, 2019 1:59 pm
IDE: Arduino 1.8.9 & 1.8.3
Core: STM official, MightyCore

Re: Using SPI1 and SPI2 on Blue Pill from Arduino IDE

Post by Andy2No » Sun Aug 25, 2019 11:18 am


Post Reply