Avoid compiler issue when copying port input  to output

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Avoid compiler issue when copying port input  to output

跳至解决方案
2,307 次查看
Kopone
Contributor IV

Hi all,

 

it's probably covered somewhere, but I just cant seem to find the right search phrases.

 

I need to switch a port from input to output (repeatedly) without generating any spikes. So my plan is to read the current value fromt the input and write it to the output latch while the port is still in input mode, then switch to output.

The code for this obviously looks like this:

 

// Switching to output mode

PTAD_PTAD0=PTAD_PTAD0; // Write current value to output latch

PTADD_PTADD0 = 1; // Switch port direction to output

 

The compiler will generate a warning "Result not used", which is ok, the first expression does of course seem pretty useless.

 

I did not check the generated assembly instructions yet, so I do not know if the compiler really wipes out the whole instruction (I guess it should not, since the registers should be declared volatile?)

 

So, my question is, what is best practice here? 

Am I simply supposed to ignore the warning?

Is CW supposed to not generate a warning since it's a volatile declared thing?

Should I read the input value to a temporary variable before writing it back to the port?

Is there any suggested way to do it "right"?

 

Thank you very much, 

Sven

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,336 次查看
BlackNight
NXP Employee
NXP Employee

Hello,

indeed, volatile is a big and complicated area.  http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf is a good paper on possible traps and pitfalls.

 

To your problem: You did not indicate which compiler you are using, but I assume that the compiler will do a read from the port, and does not write the value back. At least this is what the warning is indicating, and that's why the warning makes sense here. In any case you should look at the generated code in your case.

 

It may depend on your hardware if writing to an input port will latch the value (you need to check this). Anyway, if you want to enforce a read-write cycle you could use

if (PTAD_PTAD0) {
  PTAD_PTAD0=1;
} else {
  PTAD_PTAD0=0;
}

 

BK

 

 

在原帖中查看解决方案

0 项奖励
回复
3 回复数
1,337 次查看
BlackNight
NXP Employee
NXP Employee

Hello,

indeed, volatile is a big and complicated area.  http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf is a good paper on possible traps and pitfalls.

 

To your problem: You did not indicate which compiler you are using, but I assume that the compiler will do a read from the port, and does not write the value back. At least this is what the warning is indicating, and that's why the warning makes sense here. In any case you should look at the generated code in your case.

 

It may depend on your hardware if writing to an input port will latch the value (you need to check this). Anyway, if you want to enforce a read-write cycle you could use

if (PTAD_PTAD0) {
  PTAD_PTAD0=1;
} else {
  PTAD_PTAD0=0;
}

 

BK

 

 

0 项奖励
回复
1,336 次查看
kef
Specialist I

Yes, CW6.3 doesn't make CPU writing anything back

 

   13:     PTAD_PTAD0=PTAD_PTAD0; // Write current value to output latch
  0000 b600     [3]             LDA   _PTAD

 

 

Since CPU overwrites all PTAD bits when one tries to update just one bit (PTAD_PTADn=xx), I would try to update whole PTAD register.

 

   23:     PTAD = PTAD;
  000a 4e0000   [5]             MOV   _PTAD,_PTAD

 

 

But since the task is to avoid glitch on dedicated output pin, I think you don't need to sample input and make pin outputing sampled value. I think you should just latch to PTAD what you need to be outputed, then set data direction to output.

 

 

Message Edited by kef on 2010-01-26 09:28 AM
0 项奖励
回复
1,336 次查看
Kopone
Contributor IV

Ooh, stupid me, totally forgot about the tech details, SORRY:

I am using CW 6.2 on a 9S08QE8 here.

 

 So meanwhile, I did check the assembly, and yes, it only reads the port data, so the warning is totally valid.

 

BlackKnight, I changed the code to your suggested  if/else pattern and this produces correct code. I'll check out that paper you ref'd to, thanks a bunch.

 

Kef, thank you very much for your suggestions. Absolutely valid points and the PTAD=PTAD approach seems even more elegant (though it might have a tad of a smell of asking for sideeffects, but that might be just me :smileywink: ).

It's just in my certain case here that the present code and type of encapsulation would mean quite some reprogramming to have the code know what port and what direction register belongs to the used bit. But I dont want to bore you with further details :smileywink:

 

Thank you guys for your time and help,

 Sven

0 项奖励
回复