Writing to inputs...

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
Y@@J
Posts: 57
Joined: Mon Sep 28, 2020 8:51 pm

Writing to inputs...

Post by Y@@J »

Hi,
this is a ususual question on Arduino forums. AFAIR, the answer is : no !
But what about the STM32 (BluePill).
Is writing from output pins to input pins allowed ?

Not sure it is understandable ! Some explanations about the context...

The STM32F103 has two SPI ports. In my application, they are in use. I'd like to be able to programmatically, at compile time, switch between two SPI "inputs". Ok. SPI1 has an alternate pinout (PA4-PA7/PB5-PB4-PB3-PA15) ; the solution could be compiling for the main one or for the alternate one... Unfortunately, I can't use SPI1 as "input", as the input has to be 5V tolerant (comes from a MEGA). SPI2 is 5V tolerant, and already in use as input.
I've been thinking of using some free 5V tolerant pins and attaching a couple inteerrupt vectors in order to programmatically write to SPI2 ports. Like a internal port duplication.
The other SPI port (SPI1) is the "output", and cannot be used as input (no room on the PCB for MOSFETs as voltage shifters).

The actual schematics uses pin headers and jumpers. I'd like to get rid of them...

I don't have access to my MCUs for now, so I can't test. I'm currently rebuilding my small lab (woodworking, wiring, etc.).
It is a working project (protoboards) I'm enhancing. I can live with jumpers, but don't want to use SMDs ; want to use THT's as I've no SMDs but lots of THTs ! And also lots of BluePills.
I'm out of space. Maybe should I bite the bullet, and order populated PCBs... (costs are not the same...)

Thanks by advance.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Writing to inputs...

Post by ozcar »

I'll take your word for the difference in 5v tolerance between SPI1 and SPI2, as I did not check.

So, you are proposing to duplicate the incoming signal on an available/unused 5V tolerant pin, on another pin which is available/unused? If you externally connected the output pin of that to the SPI input pin, then I suppose it could work, but with maximum clock speed implications.

Which end is master, the MEGA or the STM32? If the MEGA is master, can you change things there, and from a performance point of view, what sort of clock speed do you need?

Another possibility would be to use a software-defined or bit-banged SPI implementation that can use any arbitrary pins. This would also limit the maximum clock speed though.
Y@@J
Posts: 57
Joined: Mon Sep 28, 2020 8:51 pm

Re: Writing to inputs...

Post by Y@@J »

The STM32 is the slave, and any MCU can be the master ; MISO is not in use. Low frequency clock : 1MHz.

clock PB13
MOSI : PB15

I'd like to select one from two 3.3V or 5V SPI buses, at runtime, with no additional components (currently, it's done externally using pin headers and jumpers).

I was thinking of that :

clock -> PB13 (5V tolerant) -> interrupt on change -> digitalWrite(CLK2, ...)
MOSI -> PB15 (5V tolerant) -> interrupt on change -> digitalWrite(MOSI2, ...)

of course writing to register, not using digitalWrite() !!!

Maybe it can be done if the STM32 code can write on input pins (internally changing the SPI input pins status). 1MHz is not that much... I experimented with the STM32 SPI+DMA, and it was failing somewhere between 12 and 16 MHz. 1MHz shouldn't be a problem.

The STM32 wiki, and the IO schematics :
https://wiki.st.com/stm32mpu/wiki/GPIO_ ... peripheral

This schematics makes me think that *maybe* we could write to an input pin, assuming it does not receive any signal and is hooked to a high impedance output ! The SPI source is a 3D printer motherboard that can be any model, and no firmware hack is allowed. Only legit configuration.

It's an emulator for 3D printer display. I succeded with the SPI OLED display, and would like to experiment with the Reprap Discount Full Graphic Smart Controller (ST7920 based LCD, SPI). It uses different pins on the two 10pin display connectors. A project similar to the BigTreeTech Marlin Mode, but intended for the RaspBerry Pi (BigTreeTech emulates the Reprap display, not a OLED, and displays on a cheap TFT). You can see an earlier version on this video : https://www.youtube.com/watch?v=vd2Wgo4UeBw

A picture of the last prototype :
Attachments
MarlinEmu.JPG
MarlinEmu.JPG (61.98 KiB) Viewed 3058 times
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Writing to inputs...

Post by ozcar »

Now I see what you mean (maybe).

I would take an "input pin" as being one that has MODEy[1:0] = 00 in CRL/CRH, which results in the ODR content not being driven to the pin. So in that sense you cannot usefully "write to an input pin".

However if MODEy[1:0] = non-zero (which you could call an "output pin") the pin is still connected to the Schmitt trigger input, and hence to IDR and alternate function input. So in that sense you can "read from an output pin", which is maybe just a different way of looking at it. That is not something I have ever tried to do, at least, not when alternate function is involved. So, maybe it is possible, but don't ask me exactly how to achieve it.

Even if you can get that to work, I doubt you would be able to handle it for a 1MHz SPI clock speed.
Y@@J
Posts: 57
Joined: Mon Sep 28, 2020 8:51 pm

Re: Writing to inputs...

Post by Y@@J »

"office/lab" in 90% completed, back to business !

You are right.

I tested some very simple code.

Can internally read an output pin : it blinks :

Code: Select all

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PB12, OUTPUT);
}

void loop() {
	digitalWrite(PB12, HIGH);
	digitalWrite(LED_BUILTIN, digitalRead(PB12));
	delay(1000);
	digitalWrite(PB12, LOW);
	digitalWrite(LED_BUILTIN, digitalRead(PB12));
	delay(1000);
}
But cannot internally write to input pin : does not blink :

Code: Select all

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PB12, INPUT);
}

void loop() {
	digitalWrite(PB12, HIGH);
	digitalWrite(LED_BUILTIN, digitalRead(PB12));
	delay(1000);
	digitalWrite(PB12, LOW);
	digitalWrite(LED_BUILTIN, digitalRead(PB12));
	delay(1000);
}
I didn't test using direct port access and interrupts, as I see no reason why it could be different.

No room for additionnal gates or trannies on the RasPi shield (still a THT guy...), so will have to live with 4 uggly jumpers instead of one... Not smart.
Capture.JPG
Capture.JPG (59.46 KiB) Viewed 2952 times
End of story...
Post Reply

Return to “General discussion”