Skip to content

Set HSPI ports by default when HSPI is selected #874

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

Merged
merged 1 commit into from
Nov 28, 2017

Conversation

zipiju
Copy link
Contributor

@zipiju zipiju commented Nov 25, 2017

When user selected HSPI with SPIClass name(HSPI) ESP was, by default,
still using VSPI ports (the ones defined in pins_arduino.h).
With this change when user selects HSPI then HSPI default ports will be
used.
If user won't specify HSPI then VSPI default ports will be used.
If user will specify SCLK, MOSI, MISO and SS with SPI.begin() then user
defined ports will be used no matter if VSPI or HSPI is selected.
With this change fe. SD library can use default HSPI ports. It was
possible to
pass HSPI SPI instance to SD lib, however even then it was using VSPI
ports which were (probably) GPIO matrixed to HSPI.

When user selected HSPI with SPIClass name(HSPI) ESP was, by default,
still using VSPI ports (the ones defined in pins_arduino.h).
With this change when user selects HSPI then HSPI default ports will be
used.
If user won't specify HSPI then VSPI default ports will be used.
If user will specify SCLK, MOSI, MISO and SS with SPI.begin() then user
defined ports will be used no matter if VSPI or HSPI is selected.
With this change fe. SD library can use default HSPI ports. It was
possible to
pass HSPI SPI instance to SD lib, however even then it was using VSPI
ports which were (probably) GPIO matrixed to HSPI.
@me-no-dev
Copy link
Member

Cool! Keep in mind that the default SPI port might change in near future ;) PSRAM is using VSPI

@me-no-dev me-no-dev merged commit 3198f25 into espressif:master Nov 28, 2017
@rmetzner49
Copy link

Hello, I'm sort of a newbie at this, so please bear with what may be an obvious question to someone that eats and sleeps code. I have an application where I'm at wit's end sharing VSPI with an RA8875 Display. There is a documented Silicon bug where this chip does NOT tri-state MISO. I have tried every which way to isolate the hardware but even just putting a 74LVC125 with OE grounded makes it not work.

So my answer is to use both HSPI and VSPI. Would the correct way to go about this be to create TWO separate Classes, one called HSPIClass and one called VSPIClass? From what I could tell the Library creates an object using either port, but not both.

Thank you in advance for your help.

@stickbreaker
Copy link
Contributor

@rmetzner49 have you tried connecting the OE to the RA8875's CS pin? if the RA8875 does not tristate MISO then adding a tri-state buffer(74LVC125) between the RA8875's SO and the MISO input of the ESP32 should work?

Chuck.

@rmetzner49
Copy link

You would think so, but even with just that one Buffer delay, the display stops working. I initially did connect it to the CS pin but that broke it, so my thought was just connecting it to GND should work provided it's the only device on the bus. It should just look like a buffer, but no dice. I suppose I could try running every pin through a buffer just to see if that changes but I'm not sure that's the correct road. There is a LOT of activity on the SPI bus running the display because I also used the Touch feature. The other device I want to run is a TDC1000 Ultrasonic Active Front End. Without having run THAT, I don't know what overhead it has beyond just initializing. My plan was to use the PulseIn function to measure the actual start/stop events on REAL pins. Do you think making two classes will allow both ports to work?

@dpharris
Copy link

dpharris commented Jan 14, 2018 via email

@stickbreaker
Copy link
Contributor

stickbreaker commented Jan 14, 2018

@rmetzner49 that buffer is spec'd to only delay the signal 4.3ns at 3.3V. from OE to output is 2.4ns.

What is your bus speed? 160mhz? I'm being factious. :) if you are using 4MHz or 8MHz I cannot see how the delay from a single buffer would affect your communication. More likely you have a SPI MODE mismatch. Looking at the RA8875 datasheet from AdaFruit, the Device is using MODE3.

Chuck.

@rmetzner49
Copy link

Good question. I had tried to up the speed months ago without much luck. I came across the following in RA8875.cpp which tells me Adafruit is mucking with the settings. It was initially at 2Mhz but I bumped it to 4. REM are my initials and comments. I'll try to peek at which MODE is being sent, though I never tweaked that. I remember trying 4, 8, 10, & 12 and 4 is about the fastest reliable setting. I'm using Adafruit's RA8875 drivers.

// REM CHANGED FROM 2000000
SPI.begin();
spi_speed = 4000000; // 4MHZ IS ALL SHE WROTE!! WON'T DO 10MHZ EITHER

RA8875 start
SPI_speed: 4000000
FuelGauge 0

@stickbreaker
Copy link
Contributor

@rmetzner49 look through their code for SPI.beginTransaction();, SPISettings(); or setDataMode()

hopefully you will find SPI.beginTransaction(SPISetting(2000000, MSBFIRST, SPI_MODE3));

Chuck.

@rmetzner49
Copy link

    // max speed!
    SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));

WELL THEN! they are calling it with the wrong MODE.

@rmetzner49
Copy link

Though if I read the RA8875 Data Sheet correctly, it says "read mode <SysClock/6" which matches my empirical tests; that it dies at any setting > 4MHz because 20M/6 = 3.333.

@rmetzner49
Copy link

dpharris: I doubt a simple diode; even a Schottky is the answer, since this thing seems really fussy on timing.

@stickbreaker
Copy link
Contributor

Here is a screen grab of the pertinent SPI timing:
ra8875 spi

clock is inactive Positive (CPOL==1), Data is sampled on the Rising Edge of clock (CPHA==1) there for
0b(CPOL)(CPHA) = 0b11 = 3

Try just changing to Mode3 and see what happens.

I can see where Mode0 (clock inactive Low, Sampled on Falling edge) would be very timing sensitive.

Chuck.

@rmetzner49
Copy link

I'll do that right after I replace the LVC125 which seems to have gone to lunch. Gotta haul out the microscope. ;-) The other thing I was thinking is I could maybe pass both the SCLK and MOSI through a buffer and see if that fixes it. Thank you for at least making me examine the issue. I still may try to make TWO classes and see what happens. It's going to be a bunch of work though. I wish I could run it past 3.33 MHz because it's dorky to switch between one screen and another. Takes about 3-4 seconds. Both the TDC1000 and the Display CAN work on the same bus only because there isn't any traffic on the TDC1000 once getting it initialized. Thanks, Bob M.

@rmetzner49
Copy link

Now I'm really confused. I haven't tried MODE3 yet but I seem to be getting MISO activity while CS is high, which I don't think is supposed to happen. I have a brand new OWON scope and I think one of the features I stumbled on in the menus is it has a BUS menu for I2C and SPI. Time to get the manual out and understand that feature. Thanks, and stay tuned. I did BTW get the LVC125 replaced and I can now ground OE and still have the display work. As soon as I determine what's going on with CS, I'll know more. Bob M.

@stickbreaker
Copy link
Contributor

@rmetzner49
Are you doing individual SPI.transfer() calls, each call en curse a MUTEX call, there is a transaction feature set that is much more efficient for bigger transfers.

Chuck.

@rmetzner49
Copy link

I'm doing what EVER the Adafruit Library does. I haven't messed with anything else, and I would expect therefore the display should work, and it does sans the problem with the RA8875 we discussed above. Unless I'm confused and SPI works differently than say, any other addressed device, there shouldn't be MISO (or MOSI) activity in the absence of CS being LOW. Else how would a device know it's being selected for communication? Once I understand how to properly set up my new 'scope using its SPI trigger feature, I might see sensible activity. Right now though I can see why when I connect the LVC125's OE pin to CS it quits working. In my head, there shouldn't be activity on MISO unless CS is LOW. Yet there is. -- Bob M.

@rmetzner49
Copy link

rmetzner49 commented Jan 15, 2018

Will hit this again tomorrow with a clear head. Thanks for all your help.

@stickbreaker
Copy link
Contributor

@rmetzner49 Yep, the MISO line should be tri-stated when it is inactive.

Good luck.!
Chuck.

@rmetzner49
Copy link

Well this has been interesting: Whilst poking around all of my "hal" files to understand what's going on, I had included esp32-hal-adc files even though I decided against using the onboard A/D. I had already thrown in an I2C, AD7994 to take care of my battery monitoring functions and I was also going to use it for Ultrasonic Flow Detection also. So I ripped those files out that deal with the onboard ADC and low and behold, MOSI now works when tri-stated through CS on the SPI bus. The picture attached shows this. The top trace is MOS, the botom is CS and you can see about 1V when CS is inactive. Please excuse the shabby triggering, but the display now works.

@stickbreaker
Copy link
Contributor

@rmetzner49 Sounds like you found something, but alas, I can't see your 'attached' picture 😦.

It was MISO right? not MOSI? Did you change the SPI Mode?

Chuck.

@rmetzner49
Copy link

Yes, it was MISO and I tried MODE3 but NO GO. MODE0 is all that works. I'll try attaching the picture again, if not, I'll attach it to gmail.
wp_20180115_14_53_45_pro

@rmetzner49
Copy link

rmetzner49 commented Jan 15, 2018

Worked that time. You can see the tiny little floating state on the top MISO trace (top)

@rmetzner49
Copy link

rmetzner49 commented Jan 15, 2018

I love my new Christmas Present to myself. (Scope). Seems every bit as capable as my Tek-2465 and it was $438 including shipping! Next chapter is getting the TDI1000 to talk SPI

@stickbreaker
Copy link
Contributor

@rmetzner49 your are dating your self. I have a Tek-2236, couldn't afford the 2465 in 1984! , I'm looking at a Rigol MSO1074z for $699. I can't believe I spent $2,400 on a scope in 1984 and now, I am questioning if I want to spend $699.00. I just ran a inflation calculator. $2,400 in 1984 is equivalent to $5,806 in today's dollars! What a ripoff!
Chuck.

@rmetzner49
Copy link

I bought the 2465 six years ago on Ebay when I retired for $150. It served me well, besides I used a 465 at work for 25 or so years, so it was familiar. That Rigol you are looking at looks HOT. I'm not sure I would ever use the 16 channels though. I bought the Owon because of the 12-bit resolution and DMM plus Sig Gen features. Though retired, I take on these project to keep away the Parkinson's. (Hint- the "49" in rmetzner49 is the year I was born). I'm building an Enteral Feed Pump for my Son using the ESP32.
wp_20170717_16_22_22_pro

@stickbreaker
Copy link
Contributor

@rmetzner49 That display look great! I'm a little younger than you but, hardly enough that matters.
The primary reason I'll looking at it is the 16 channel logic analyzer. Though it is only 8bit resolution with 4 inputs. If you enable the logic analyzer you loose channel 3 and 4. It just stuffs those 16bits into the trace buffer where channel 3 and 4's data would go.

Do you think you will trace down the interaction with the 'hal-adc'?

Chuck.

@rmetzner49
Copy link

TBH, I don't know that was the only factor. I did also grab both new "hal-spi" files from this article. I also remember Espressif changed a bunch of HAL code between 10/14/17 and 1/1/18 because about the time I added esp32-hal-i2c to the project, it no longer compiled until I updated esp32-hal. This is why I ended up on this thread trying to run both SPI ports. I originally had a 32U4 with its timers setting up the Ultrasonic and I had it about half working, but every wire I moved gave different results. My plan was to use i2c to talk between processors.
LINDHOLM.zip

So although I might have eventually gotten it to work, using the TDC1000 seemed like the way to go. Of course it's going to be a chore to port the setup of that device to this project, but I have a good starting example from Lindholm using it with a TDC7200 stopwatch chip. Now that I can have it on VSPI, I can at least see light at the end of the tunnel.

@stickbreaker
Copy link
Contributor

@rmetzner49 You might run into some issues with the I2C code. The I2C HAL currently in the main branch espressif/arduino-esp32 are very unreliable, they use a polling interface with the I2C hardware and are very timing sensitive.

I rewrote the I2C 'HALs' in my fork stickbreaker/ardunio-esp32. My version uses an ISR model instead, It has proven to be robust. The maintainer for the main branch @me-no-dev was working on incorporating my fork into main branch. But, I have seen no activity for ~30days.

I'm working on SlaveMode now. I am confusing myself with spinlock's and Mutex's between the ISR and foreground apps. Currently I'm almost up to the point to stimulate the hardware and see how it twitches. Hopefully my prognostication will match reality.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Jan 15, 2018

Not to worry, the only thing the i2C will be doing is the AD7994 and the DS3232 RTC and I've already had both working. On the other hand, my need for a good A/D will be gone, so I may revive the ESP32's on board. In theory, I could also use the ESP32's RTC but I didn't see much info on how it runs on battery or any examples of an RTC using the internal. I successfully used i2c talking to the 32U4 using Nick Gannon's messaging method as explained on his site. Most of my pre-retirement work was Hardware, signal conditioning and motor control. We had a whole group that wrote the main apps in Windows VB, so I'm really green at coding "C". On the flip side, no one in the VB group knew anything about embedded coding so I eventually ended up writing some drivers.

@stickbreaker
Copy link
Contributor

@rmetzner49 sounds good, EXCEPT the ESP32 RTC stands for Real Time CoProcessor. It is lowpower lowspeed cpu. It could be programmed to act as a Real Time Clock, but you will have to add a 32KHz accurate clock, and code.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Jan 16, 2018 via email

@stickbreaker
Copy link
Contributor

@rmetzner49 I just use the Arduino environment 1.8.5 with NotePad++ as an external editor. So I just edit my code in NotePad++, save it and touch Arduino checkmark or Right Arrow to compile and download. Currently I'm using a couple of clone WeMos Bluetooth + battery boards.

I have a messy I2C bus (18" of 0.050" ribbon connecting 6 24LCxx EEproms, a DS1307, 2 MCP23008 (one as a 4x4 keypad driver, one driving a 20x4 LCD using a modified LiquidCrystal library), And of course a UNO. I'm using the UNO as an I2C Master to poke the ESP32 as a slave.

A couple of DS18b20 as temp sensors.(I had to modify the OneWire.h library to make it work on the ESP32);

A Micro SD card directly connected as the SD.h readme.md shows. (but it is unstable right now).

Nothing Special. After I get I2C stable I'm planning on embedding ESP-VRoom-32 modules on some custom boards.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Jan 16, 2018 via email

@rmetzner49
Copy link

rmetzner49 commented Jan 16, 2018 via email

@stickbreaker
Copy link
Contributor

@rmetzner49 Yea I use Eagle 7.4 Not gonna upgrade to monthly payments. When AutoCad bought them out they changed licensing styles. I have been looking at KiCAD.. I have used IteadStudios (china) to manufacture PCB, a 10cm x 10cm 2 layer Qty10 costs about $35 with shipping(3 to 4 weeks).
Chuck.

@dpharris
Copy link

dpharris commented Jan 16, 2018 via email

@stickbreaker
Copy link
Contributor

@dpharris No, Currently it only support Master Mode communications. I am working on Slave functions. Multiprocessor, multi thread, interrupt servicing, callbacks, spinlocks, mutex's Have me running in circles with my hair on fire!

I'm having design issues with deadlocking between the ISR and foreground tasks modifying buffers.
Here is my current dilemma:

  • SLAVE ISR triggered on Core0, which will add characters to a buffer thru a pointer and offset + count perhaps dispatch the completed transaction via Callback()
  • Core1 in routine to add Master transaction to work queue, this addition causing a reallocate() expansion of the buffer control array. This reallocate() ends up moving the buffer control block to a new position in memory.
  • Can I assume xPortEnter_CriticalfromISR() will freeze the other Core?
  • Can I assume xPortEnter_Critical() will freeze the already entered ISR on the Other Core?

My problem is that I can see no way to test these edge condition because they are so timing critical. The ISR has to accessing the buffer control block while the other Core is reallocating the control block! I am tending towards a two level control structure, the Active structure is only modified when the ISR Is pinned down, or maybe the ISR manages the Staged to Active merging. The Slave Mode I2C on the Arduino co-exists with Master Mode I2C transparently. That is what I am trying to emulate.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Feb 3, 2018 via email

@rmetzner49
Copy link

rmetzner49 commented Sep 22, 2018 via email

@stickbreaker
Copy link
Contributor

@rmetzner49 I haven't done much with SPI, your code looks valid, I just reread this thread.
Back at the beginning, the first issue I brought up was that MISO might not be tri-stating.
But, My recommendation of a '125 buffer caused failures.

How did you get it to work?

I think a '125 with a weak 10k .. 100k pullup on MISO should work. the pullup will keep a float pin oscillation from happening.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Sep 23, 2018 via email

@stickbreaker
Copy link
Contributor

@rmetzner49 Ya, Stopping and Starting should work, as long as your CS pins stay disabled during the switch. The devices won't know any difference.

Chuck.

@rmetzner49
Copy link

rmetzner49 commented Oct 31, 2018 via email

@stickbreaker
Copy link
Contributor

@rmetzner49 Nice to see you got it to work. Isn't it nice 😬 when "interface standards" are adhered to?

Chuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants