MOD5282 GPIO for inputs on Port J (J1-5/6/7 Pins)

Discussion to talk about software related topics only.
Post Reply
microwavejoe
Posts: 2
Joined: Sat Jan 21, 2012 12:34 pm

MOD5282 GPIO for inputs on Port J (J1-5/6/7 Pins)

Post by microwavejoe »

Using the MOD5282 on my own board, initializing for GPIO on Port J per MOD5282 GPIO Configuration App Note, with push buttons with pullups on J1-5/6/7 pins. Pushing button definitely brings voltage at pins J1-5/6/7 low, but reading Port J results in a fixed value of 14.

What is missing? Code snippet:

//J1-5,6,7 as input
// Port J Pin Assignment Register. Set bit to 0 for GPIO, or 1
// for primary function (5282UM table 26-12). The following
// code example sets the CS pins to GPIO without changing the
// configuration of the BSx pins (i.e upper 4 bits and bit
// 0 are unchanged):
sim.gpio.pjpar &= 0xf1;
// Port J Data Direction Register. Set bit to 1 for output, or
// 0 for input.
sim.gpio.ddrj &= 0xf1; // Zero bits 1,2,3 for input set, leave other alone


while(1)
{
BYTE value_j = sim.gpio.portj;
iprintf("Portj is %d\r\n",value_j);
}

This forever just prints 14, regardless of pushing the buttons to bring low J1-5/6/7

Any ideas?
User avatar
lgitlitz
Posts: 331
Joined: Wed Apr 23, 2008 11:43 am
Location: San Diego, CA
Contact:

Re: MOD5282 GPIO for inputs on Port J (J1-5/6/7 Pins)

Post by lgitlitz »

This looks like compiler optimization. Since sim.gpio.portj is being modified by external hardware, you need to tell the compiler that is should read its value from memory every-time it is accessed.
change:
BYTE value_j = sim.gpio.portj;
to:
volatile BYTE value_j = sim.gpio.portj;

There are other cases where variables should be volatile even though they are not being modified by externally... here is an example:
If you want to make a quick delay loop:
for( int count=0; count<100; count++); // Loop will be optimized to "int count=100;" or possibly removed completely
for( volatile int count=0; count<100; count++); // Loop will run 100 cycles since variable count is volatile

For more complex applications where I think optimization may be a problem, I will test by running a debug build or turn optimization to level 0. This essentially makes all variables into volatiles since there is no compiler optimization. If that succeeds then I go on the hunt for which variables need to be volatile. Tagging non-volatile variables to be volatile will impact performance though it should not affect the intended functionality of your code.

-Larry
roland.ames

Re: MOD5282 GPIO for inputs on Port J (J1-5/6/7 Pins)

Post by roland.ames »

you should be reading portjp not portj.
Check the MCF5282 user manual for the difference between these two registers
microwavejoe
Posts: 2
Joined: Sat Jan 21, 2012 12:34 pm

Re: MOD5282 GPIO for inputs on Port J (J1-5/6/7 Pins) (SOLVE

Post by microwavejoe »

Thanks roland.ames - changing to portjp did the trick. Time for me to do some more reading!
Post Reply