2

I have this code:

void GPIO_InitPortPin(uint8* PortControl, uint8 Pin, uint8 PinDir){
    *PortControl &= (~(1U << Pin));
    *PortControl |= (PinDir << Pin);
}

If the PortControl register is 11111111, the first line should clear the bit of whatever the corresponding Pin is, but unexpectedly it clears all the register.

My Client Function is: GPIO_InitPortPin(&TRISB, GPIO_PIN_0, GPIO_IN);

GPIO_PIN_0 and GPIO_IN are macros defined as follows:

#define GPIO_PIN_0    (0)
#define GPIO_PIN_1    (1)
...
#define GPIO_PIN_7    (7)

#define GPIO_OUT      (0)
#define GPIO_IN       (1)

So, I tried to cast Pin to sint8: *PortControl &= (~(1U << (sint8) Pin));

and it worked. Also, I tried to hard-code the Pin as 1 and it worked fine. I know the problem is that Pin is uint8 or specifically to be unsigned since

*PortControl &= (~(1U << 1)); also worked fine. My Question is: why the right operand type affects the shifting operation, although I've red otherwise before(That RHS doesn't affect operation)?

Iam using SDCC compiler.

Edit : I've tested the function without casting on gcc and it worked as expected.

Mosaaleb
  • 1,028
  • 1
  • 9
  • 23
  • Sounds like an SDCC bug to me. – Barmar Jan 18 '18 at 03:37
  • 1
    You migth try searching at https://sourceforge.net/p/sdcc/bugs/ – Barmar Jan 18 '18 at 03:39
  • 1
    This is questionable code as it may change the pin direction twice in a short space of time. I recommend against using it. – chux - Reinstate Monica Jan 18 '18 at 03:40
  • What is the problem with that? @chux – Mosaaleb Jan 18 '18 at 03:43
  • 1
    `*PortControl` is a hardware register. By flipping the pin direction in a short time, strange output may be seen on the port - even if only for a short time. A better approach would either set the bit of clear the bit, not toggle the bit. – chux - Reinstate Monica Jan 18 '18 at 03:46
  • 1
    Consider `if (PinDir) *PortControl |= (1u<< Pin); else *PortControl &= ~(1u << Pin);`. BTW: make certain `Pin` is 0-7. – chux - Reinstate Monica Jan 18 '18 at 03:52
  • 1
    Have you looked at the assembly generated by the sdcc? – Turn Jan 18 '18 at 03:57
  • 2
    If `*PortControl` is a hardware register, then *where's the `volatile`?!* – Antti Haapala -- Слава Україні Jan 18 '18 at 03:59
  • @AnttiHaapala As a port control, `volatile` is not the issue. The port control does not change unless with CPU action. `volatile` is very appropriate to the port IO register though. – chux - Reinstate Monica Jan 18 '18 at 04:01
  • 2
    @chux of course it can be an issue, all writes can be optimized out in a larger program if the compiler sees so – Antti Haapala -- Слава Україні Jan 18 '18 at 04:03
  • @AnttiHaapala Fair enough. A subtle and correct point. – chux - Reinstate Monica Jan 18 '18 at 04:04
  • 2
    what is the value of `Pin` in the call? why not show a complete and verifiable example, instead of trying to make 100 people to guess about what could happen in your code without looking at it? – Luis Colorado Jan 18 '18 at 06:40
  • @LuisColorado Edited, and just `what is the value of Pin in the call? Why not show a complete and verifiable example?` would also be great. – Mosaaleb Jan 18 '18 at 15:11
  • @Mosaaleb, show yours first, as you are showing correct code. There's no problem in your code, at least without showing more context. This is a forum to deal with programming mistakes and errors, not to do the homework of students. Please, show a full, complete and verifiable example, and we'll be able to show you where your mistakes are. – Luis Colorado Jan 22 '18 at 07:08
  • @Mosaaleb, telling things like _I've tested something_ and telling absolutely nothing at what tests your have made, and what results you received from them, is saying nothing about your testing. – Luis Colorado Jan 22 '18 at 07:40
  • @Mosaaleb, by the way, have you tried to print the values of parameters before the call and inside the function? just to see if they pass untouched... Have I to ask you whatever I imagine to test to see if you have done that test? read https://stackoverflow.com/help/mcve or probably you'll get the question closed because of not following the SO rules. – Luis Colorado Jan 22 '18 at 07:45

0 Answers0