Writing to GPIOs on two different ports at the same time?

Post here first, or if you can't find a relevant section!
arpruss
Posts: 83
Joined: Sat Dec 21, 2019 10:06 pm

Writing to GPIOs on two different ports at the same time?

Post by arpruss »

Is there an easy way to synchronize writes to GPIOs on two different ports so they happen at the same time?

I am using a blue pill with Roger's core if it matters.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Writing to GPIOs on two different ports at the same time?

Post by ag123 »

among the things take a look in RM0008
https://www.st.com/resource/en/referenc ... ronics.pdf
e.g. the GPIO chapter covers quite a lot

oh and there is this thing about the BSRR register
https://gist.github.com/iwalpola/6c36c9 ... 0a118571ca
not quite different ports but it is possible to toggle different pins on a same port e.g. PAxx together.

I'm not sure if it helps to write to 2 different GPIO registers say in sequence, not quite 'same time' but close
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by dannyf »

Is there an easy way to synchronize writes to GPIOs on two different ports so they happen at the same time?
unless in some highly-specialized cases, the answer is no.

two ways that I can think of:
1. you can use timer output compares to output up to 4 pin states. those OC pins can be on different ports.
2. you can use analog comparators to output to different pins / on different ports.

in both cases, the number of pins you can output to is highly limited.
arpruss
Posts: 83
Joined: Sat Dec 21, 2019 10:06 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by arpruss »

I'd like to do 13 pins at once. I have 12 on one port and 1 on another, and I can't get them all on one, because the only port I have where there are 13 also has a bunch of 5V tolerant pins that I need for another purpose.

For my use-case (writing addresses to an Atari 2600 cartridge), writing to the BSRR registers of the two ports in immediate sequence may not be fast enough. At least, I've noticed differences in results depending on the order in which I write them.

Can I maybe set up two DMA transfers to the BSRR or ODR registers? (I feel that I remember that DMA doesn't work with special addresses, though.)
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by dannyf »

For my use-case (writing addresses to an Atari 2600 cartridge), writing to the BSRR registers of the two ports in immediate sequence may not be fast enough.
maybe you should first explore if that assumption is correct - i find it hard to believe.
arpruss
Posts: 83
Joined: Sat Dec 21, 2019 10:06 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by arpruss »

dannyf wrote: Wed Sep 20, 2023 11:02 pm
For my use-case (writing addresses to an Atari 2600 cartridge), writing to the BSRR registers of the two ports in immediate sequence may not be fast enough.
maybe you should first explore if that assumption is correct - i find it hard to believe.
Yeah. The problem is that I currently only own two cartridges for testing. I know that I can dump them correctly if I write A0-11 first and then immediately A12, but if I write A12 first and then A0-11, then at least one of the two cartridges has some dump errors. If I'm lucky, then all cartridges will be fine with the A0-11-then-A12 order, but I would be happier if I could just write both simultaneously, as I can't test all cartridges.

Some cartridges do weird things. Normally A12 is chip select, and the cartridge ignores all data with A12=0, so if I write A0-11 and then A12, things work. However, some cartridges have mappers that switch banks on certain accesses with A12=0. So if I write A0-11 and then A12, if A12 is zero before I write A0-11, and the A0-11 part of the address matches the bank switching address, something may go wrong. I currently do some kludgy stuff to avoid this, but I would just feel that things would be neater if I made all the pins switch at the same time.

One thing I noticed is that you can lower the speed of an individual GPIO pin. I haven't tried to put that on an oscilloscope, but I wonder if one could use that to induce a delay on the pins written earlier? But that sounds super kludgy.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Writing to GPIOs on two different ports at the same time?

Post by ag123 »

i'm thinking maybe it could help to use an external chip, e.g. shift register
https://assets.nexperia.com/documents/d ... HCT595.pdf
but I'm not sure if toggling /OE could be affected by timing glitches

my guess is to control A12 (chip select) separately so that the sequence would after all be correct.
e.g. switch A12 /CS high 1st
then set A0-11
then switch A12 /CS low - enable
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: Writing to GPIOs on two different ports at the same time?

Post by Bakisha »

Have you consider to use that one pin on separate port for address A0? So you can keep A12 on port with 12 pins. In worst case, you are reading data from address $xxx0 when A0 is 0, and from address $xxx1 when A0 is 1.

74HCT595 is good for sync timing, but if it's too slow, you could use 74HCT273.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by dannyf »

I know that I can dump them correctly if I write A0-11 first and then immediately A12
I would say that you found your solution already.

1. you do NOT need to write to two different ports at the same time;
2. you do need to write to numerous pins *sufficiently* fast.
arpruss
Posts: 83
Joined: Sat Dec 21, 2019 10:06 pm

Re: Writing to GPIOs on two different ports at the same time?

Post by arpruss »

I think I have the best solution now. I can use PA13 and PA14, which are by default assigned to the debug interface, as long as I don't use debugging (which I don't).
Post Reply

Return to “General discussion”