Gaining control of the CS on SPI bus

Hi all
I’m building a system consisting of an Arduino Node MCU ESP-12E, a 1.3’’ OLED display and an SD card module for saving and reading data.
Both are based on SPI and both shall be on the same bus-pins of the ESP to save pins.

Setup:
MOSI: D7 GPIO13
MISO: D6 GPIO12 (NC on the OLED)
CLK: D5 GPIO14
CS: D8 GPIO15
DC: D2 GPIO4 (NC on the SD CARD)
RES: D1 GPIO5 (NC on the SD CARD)
D0, D3 and D4 are used for other purposes.
I cannot afford to have separate pins for CSs.
However, I have no constraints on what function needs to be on what pin.

I found an example (ReadWrite - from the examples under SD in the IDE) where I can RW to the SD-card using the ESP.
Works fine! Even if I set the CS constantly LOW (CS for the other device is disabled while testing this code (set HI in hardware))

I found an example for the OLED (SH1106Demo.ino)
Works fine! Even if I set the CS constantly LOW (CS for the other device is disabled while testing this code (set HI in hardware))

Nice libraries taking care of everything – unfortunately also the CS…

My problem is that I need to use only one single pin for the CS of both devices. By inserting an inverter to one of the CS inputs I can select only one of them at a time provided that I get control of the CS pin D8 GPIO15.
So I need to control what happens with the CS based on the spi.h and/or SH1106.h ( and underlying files?)
The following (including the CS) is defined in the start of the source code of the OLED:
SH1106 display(true, OLED_RESET, OLED_DC, OLED_CS)
Spoiling my possibility of controlling the CS…?
In the OLED code I tried to set the CS LOW in my .ino code to force it Low during the session. It (of cause) failed completely as the pulse train on the pin is exactly the same. (The OLED continued to work fine as before)
I guess the same will be the case with the SD card, however not tested.

How can I gain control of the CS?
Should I use other libraries? Which ones?
Any help on this is very welcome?

Many thanks in advance.

So I need to control what happens with the CS based on the spi.h and/or SH1106.h ( and underlying files?)

No you need to modify one of the two libraries to set CS HIGH instead of LOW while accessing the chips functionality.
BTW, the header file is called SPI.h, not spil.h.

How can I gain control of the CS?

You always have it. You are using the libraries and all are available with complete source code. You just have to alter one of them.

Should I use other libraries? Which ones?

You won't find a library that drives CS HIGH while accessing the chip, so you have to make that modification yourself.

I cannot afford to have separate pins for CSs.

You made the wrong hardware choices to begin with. Rethink the project.

Try putting an inverter in only one of the CS lines. A 74HC04 will do fine. You can also build a discrete inverter with a transistor or MOSFET and some resistors. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

Search 'spi mux'.

Hi all and thank you for your thoughts and your valuable time.

To pylon
Thanks for your reply and proposal.
I guess I need to modify both .h (and .cpp files?) as I intend to have the CS constantly ON or OFF to avoid any pulsing CS-pulses on the device I am not accessing. Otherwise I fear to get garbage into the device I am not handling.
I am not really sure which SPI.h I should use as basis for my alterations. I found a huge amount of SPI.h files (50+) with sizes from 2k to 50k or more. (I have learned it is with capitals...)
I don't really want to modify an existing "well known" .h file as the SPI.h
Don't you think I should invent a mySPI.h file?
Thanks

To jremington
I know the hardware is a challenge but that is my choice and problem 

To gilshultz
Yes, this is exactly what I have planned and already installed 74HC14 for this purpose.
Thanks

To dougp
Oh yes. That could be a solution.
You triggered me!
If I use a 74HC240 – connect the Arduino CS output to the 3-state control input of the 240 – all inputs of the 240 are grounded. One output is used as CS of device #1. Another output is connected to input of the next input/channel of the 240. This output is routed to CS of device #2. Both outputs (actually 3) need a pullup resistor. This should work.
Now my problem is still how I “disconnect” the control of the CS of the .h files.
I still don’t know how to do that…

To dougp
Smoke out of the chip is much worse.

Sorry, That's all too complicated. My problem is still the disconnection in software of the CS signal.
The 74HC14 is enough.
Sorry!!!

I guess I need to modify both .h (and .cpp files?) as I intend to have the CS constantly ON or OFF to avoid any pulsing CS-pulses on the device I am not accessing. Otherwise I fear to get garbage into the device I am not handling.

You only need to alter the .cpp file as there the CS is handled.

I am not really sure which SPI.h I should use as basis for my alterations. I found a huge amount of SPI.h files (50+) with sizes from 2k to 50k or more. (I have learned it is with capitals...)

You don't have to alter the SPI library! You have to alter either your display or SD card library to have the CS pin inverted.

Sorry for the late reply.
I will do so and I will see how for my abilities will reach - if you understand.
Thanks for taking your time.