-
Notifications
You must be signed in to change notification settings - Fork 1k
How to use an SPI2? (Incompatibility between cores is very bad) #669
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
Comments
I have modified the code, based on the link below, and apparently there is a clock signal on the SPI2 port, but it is still not possible to access W25Q64 memory, while on the other core it works normally.
|
Unfortunately the code below worked, but the next code, doesn't work, only in the other core. Only direct memory access works. File system access does not work. Works:
Did not work:
|
@rtek1000 For ref: Anyway if you want add compatibility this probably could be done by adding some API. But as said this core support 13 series and more soon. So, my feeling is this is not a good option. |
Thank you, I'm trying to adapt to this core, but it's hard, the wiki example can't even be compiled.
Now I'm trying to remap SPI1 to pins PB5, PB4, PB3 and A15. I found someone else with the same question, so it may mean that the wiki is not helping users. I know that for anyone writing library code it's easy to find anything, but for outsiders, things need to be less generic, and examples need to work without being edited. I saw that the alternate pins of SPI1 are in the file below, but how can I remap SPI1? |
The wiki example is generic. It is up to user to set the correct pins. |
Thanks, This code did not work below, when I inform the particular SS, the clock signal is not generated in the SPI, but pin PB0 is set at high level:
If I use the hardware SS pin, then the SPI port generates the clock signal:
How can I make the SPI port work with SS that is not a SS hardware pin? |
In case you want the SPI class drive the software CS you have to pass it as an argument else it assume you drive manually the CS. void setup()
{
SPI.setMOSI(PB5);
SPI.setMISO(PB4);
SPI.setSCLK(PB3);
SPI.begin(PB0);
while(1){
SPI.transfer(PB0, 0x01);
}
} or void setup()
{
SPI.setMOSI(PB5);
SPI.setMISO(PB4);
SPI.setSCLK(PB3);
SPI.begin(PB0);
digitalWrite(PB0, LOW);
while(1){
SPI.transfer(0x01);
}
} |
The sample (CardInfo*) SD card access code worked with custom SS.
I changed the flash memory wiring to work with SS hardware, but it didn't work when trying to format memory with FatFS, can only access basic information. Therefore, the flash memory access library (Adafruit_SPIFlash) does not accept to operate with this core (for now), even on SPI1. Anyway thank you. |
I found something interesting, with the "Arduino_STM32" core the flash_info (modified*) sketch reads 0xFF after chip erase. But with "Arduino_Core_STM32" core, the first byte is 0x7F for all pages, this may explain why it doesn't work on this core. Note: using SPI1 remaped
// The MIT License (MIT)
// Copyright (c) 2019 Ha Thach for Adafruit Industries
#include "SdFat.h"
#include "Adafruit_SPIFlash.h"
#define SS1 PA15
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI);
Adafruit_SPIFlash flash(&flashTransport);
#define MAXPAGESIZE 256
uint8_t buffer[MAXPAGESIZE];
//Serial = &Serial1;
#if defined(__STM32F1__)
#define mySerial Serial1
#else
#define mySerial Serial
#endif
// the setup function runs once when you press reset or power the board
void setup()
{
#if defined(__STM32F1__)
// Remap USART 1
// afio_remap(AFIO_REMAP_USART1);
// Remap SPI 1
// Source: https://community.platformio.org/t/pio-arduino-stm32f103cbt6-remap/6786/5
afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); // PB3 free
afio_remap(AFIO_REMAP_SPI1);
gpio_set_mode (GPIOB, 3, GPIO_AF_OUTPUT_PP);
gpio_set_mode (GPIOB, 4, GPIO_INPUT_FLOATING);
gpio_set_mode (GPIOB, 5, GPIO_AF_OUTPUT_PP);
#else
SPI.setMOSI(PB5); // using pin number PYn
SPI.setMISO(PB4); // using pin name PY_n
SPI.setSCLK(PB3); // using pin number PYn
SPI.setSSEL(PA15); // using pin number PYn
#endif
delay(1000);
mySerial.begin(115200);
// mySerial.begin(2000000);
//while ( !Serial ) delay(10); // wait for native usb
pinMode(PB0, OUTPUT); // Memory power supply, MOSFET
digitalWrite(PB0, LOW);
mySerial.println("Adafruit Serial Flash Info example");
delay(1000);
flash.begin();
// mySerial.println("Adafruit Serial Flash Info example");
mySerial.print("JEDEC ID: "); mySerial.println(flash.getJEDECID(), HEX);
mySerial.print("Flash size: "); mySerial.println(flash.size());
mySerial.print("Flash status register: "); mySerial.println(flash.readStatus());
mySerial.print("Flash status register 2: "); mySerial.println(flash.readStatus2());
//digitalWrite(PB0, LOW);
pinMode(PB0, INPUT);
}
void loop()
{
// nothing to do
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
if (inChar == 'e') {
mySerial.println("Erasing chip");
pinMode(PB0, OUTPUT);
digitalWrite(PB0, LOW);
delay(10);
if (flash.eraseChip()) {
mySerial.println("Ok");
} else {
mySerial.println("Fail");
}
mySerial.println("Wait...");
flash.waitUntilReady();
mySerial.println("Ready");
//digitalWrite(PB0, LOW);
pinMode(PB0, INPUT);
}
if (inChar == 'd') {
delay(10);
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
}
dumpChip();
}
if (inChar == 'b') {
delay(10);
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
}
blankingChip();
}
}
}
void dumpChip() {
pinMode(PB0, OUTPUT);
digitalWrite(PB0, LOW);
uint16_t pagesize = flash.pageSize();
mySerial.println("Dumping FLASH");
for (int32_t page = 0; page < flash.numPages() ; page++) {
if (mySerial.available()) {
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
}
break;
}
memset(buffer, 0, pagesize);
mySerial.print("Reading page: ");
mySerial.println(page);
buffer[0] = 0xFF;
uint16_t r = flash.readBuffer (page * pagesize, buffer, pagesize);
if (r != pagesize) {
mySerial.println("Flash read failure");
} else {
int j = 0;
for (int16_t i = 0; i < 256 ; i++) {
if (buffer[i] < 16) {
mySerial.print(0, DEC);
}
mySerial.print(buffer[i], HEX);
if (j < 15) {
j++;
mySerial.print(" ");
} else {
j = 0;
mySerial.println();
}
}
mySerial.println();
}
}
//digitalWrite(PB0, LOW);
pinMode(PB0, INPUT);
}
void blankingChip() {
pinMode(PB0, OUTPUT);
digitalWrite(PB0, LOW);
uint16_t pagesize = flash.pageSize();
mySerial.println("Blanking check");
for (int32_t page = 0; page < flash.numPages() ; page++) {
if (mySerial.available()) {
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
}
break;
}
memset(buffer, 0, pagesize);
mySerial.print("Reading page: ");
mySerial.println(page);
uint16_t r = flash.readBuffer (page * pagesize, buffer, pagesize);
if (r != pagesize) {
mySerial.println("Flash read failure");
} else {
bool j = 0;
for (int16_t i = 0; i < 256 ; i++) {
if (buffer[i] != 0xFF) {
mySerial.print("Flash read failure at: ");
mySerial.print(i, DEC);
mySerial.print(", found: 0x");
mySerial.print(buffer[i], HEX);
mySerial.println(" <> 0xFF");
j = true;
break;
}
}
if (j) {
break;
}
}
}
//digitalWrite(PB0, LOW);
pinMode(PB0, INPUT);
}
|
@rtek1000 |
Moreover please avoid to reopen an issue for an other subject. This is very confusing. |
Ok |
Seems to be just an SPI port speed issue, for some reason after slowing down the 0x7F is no longer found: Added to main code after flash.begin(); flashTransport.setClockSpeed(200*1000000UL); // 200MHz: fail (1st: 0x7F) flashTransport.setClockSpeed(1*1000000UL); // 1MHz: ok (all: 0xFF) Main code readBuffer: uint16_t r = flash.readBuffer (page * pagesize, buffer, pagesize); Library Subroutines: uint32_t Adafruit_SPIFlash::readBuffer(uint32_t address, uint8_t *buffer, uint32_t len)
{
if (!_flash_dev) return 0;
waitUntilReady();
SPIFLASH_LOG(address/512, len/512);
return _trans->readMemory(address, buffer, len) ? len : 0;
} bool Adafruit_FlashTransport_SPI::readMemory(uint32_t addr, uint8_t *data, uint32_t len)
{
digitalWrite(_ss, LOW);
_spi->beginTransaction(_setting);
_spi->transfer(SFLASH_CMD_READ);
// 24-bit address MSB first
_spi->transfer((addr >> 16) & 0xFF);
_spi->transfer((addr >> 8) & 0xFF);
_spi->transfer(addr & 0xFF);
while(len--)
{
*data++ = _spi->transfer(0xFF);
}
_spi->endTransaction();
digitalWrite(_ss, HIGH);
return true;
}
void Adafruit_FlashTransport_SPI::setClockSpeed(uint32_t clock_hz)
{
_setting = SPISettings(clock_hz, MSBFIRST, SPI_MODE0);
} I will try to use the library with FAT to test better. |
That's it, the FAT part is working now, just modify the speed. Added to main code after flash.begin(); flashTransport.setClockSpeed(1*1000000UL); // 1MHz: ok (all: 0xFF) Scketch SdFat_format results on serial monitor:
Scketch SdFat_full_usage results on serial monitor:
|
@fpistm Why does the other core work with SPI set to 133MHz and this core here only works at 35MHz or below? |
Roger's core use SPI with DMA. |
Nice |
Is your feature request/improvement related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
I can't use SPI2
Describe the solution you'd like
A clear and concise description of what you want to happen.
For the other core you can normally use SPI2 with the code below
https://github.com/rogerclarkmelbourne/Arduino_STM32/blob/master/STM32F1/libraries/SPI/examples/using_SPI_ports/using_SPI_ports.ino
SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
I tried unsuccessfully to follow the library descriptions:
https://github.com/stm32duino/Arduino_Core_STM32/tree/master/libraries/SPI
Additional context
Add any other context, example or screenshots about the feature request here.
I don't know which core was started first, but you 2 would have to follow a single pattern for these basic pieces of hardware, every time someone tries to build code using libraries available on the internet, one works on one core, another works on another core. , but you can't put the libraries together in the same sketch, resulting in project abandonment
Any contribution is welcome, so do not hesitate to submit a PR.
The text was updated successfully, but these errors were encountered: