Page 1 sur 1

Using GPIO22

Message non luPosté: 13 Jan 2019, 00:48
de wodz94
Hello all!

(Scroll down for the answer! :) )

I'm trying to understand the logic behind the mapping of the GPIO sections and bits, so I can do some fancy shizzle with them.
In this post, the master vogtinator wrote a short snippet of code controlling the GPIO4 on pin 6 of the dock connector.

Here's the code:

Code: Tout sélectionner
*(volatile uint32_t*)(0x90000010) &= ~(1<<4);
while(!any_key_pressed()) {
    *(volatile uint32_t*)(0x90000014) &= ~(1<<4);
    sleep(2000);
    *(volatile uint32_t*)(0x90000014) |= 1<<4;
    sleep(2000);
}


Now that works a dream with me in the c++ app that I'm writing, but as soon as I leave the application, the pin voltage reverts to 2.75v.

That's mildly inconvenient, because I'm looking to have a pin at 0v until I use the app to set it to 3.3v
Through measuring, I realised that on pin 18 of the dock connector, where GPIO22 sits, there isn't a voltage by system-default.
Exactly what I'm looking for.

Now I've been trying to understand how to set GPIO22 to 1 but maybe I've blown one braincell too many over christmas...I just can't figure it out.

Any suggestions would be greatly appreciated.
Warm regards from Switzerland!

____________________________________________________________________________
EDIT:

So after another long day of staring at hex numbers, I've just figured it out.
I'll try and explain it, so even I would understand it... and please correct me if I'm wrong.

So the GPIO registers are split into a few sections (Hackspire):

Section 0: 0x90000000-0x9000003F
Section 1: 0x90000040-0x9000007F
Section 2: 0x90000080-0x900000BF
Section 3: 0x900000C0-0x900000FF
Section 5: 0x90000140-0x9000017F


Each GPIO has a corresponding bit in one of those sections.
Here's the allocation table for all the GPIOs attached to this post.(Hackspire).

Say you want to write to GPIO22 (which happens to be pin 18 on the dock connector), this is what needs doing:

1) Find your GPIO in the table. For me it's Section 2, bit 6.

2) Set your GPIO as an output.
To do this, you find the start of the section. - for me it's 0x90000080
Now the i/o bits are allocated at +0x10 from the start of the section. - for me: 0x90000090
In there we want to set the 6th bit to our desired state (false).

Code: Tout sélectionner
*(volatile uint32_t*)(0x90000090) &= ~(1<<6);


3) Now let's set the Output to true.
These bits lie at the start address +0x14 - for me: 0x90000094
Here we also want to set the 6th bit.

Code: Tout sélectionner
*(volatile uint32_t*)(0x90000094) |= 1<<6;



There's probably a sleaker way to implement the code but here's a small method:

Code: Tout sélectionner
void setGPIO22(bool value){
  *(volatile uint32_t*)(0x90000090) &= ~(1<<6);
  if(value == TRUE){
    *(volatile uint32_t*)(0x90000094) |= 1<<6;
  }

  if(value == FALSE){
    *(volatile uint32_t*)(0x90000094) &= ~(1<<6);
  }
}



Many thanks to all who do the good work and write the documentation allowing ndless to happen so well!

Best,
wodz

Re: Using GPIO22

Message non luPosté: 14 Jan 2019, 05:23
de Adriweb
Well, congrats for figuring it out :)