0

Suppose that IO-part is memory mapped to address 0x32 and the directive defines

#define portx 0x32

How to construct C language macro which writes the port by storing a value to corresponding register?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Sara
  • 47
  • 2
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Dec 15 '16 at 12:17
  • See [this](http://stackoverflow.com/a/31266754/584518) for another duplicate. – Lundin Dec 15 '16 at 12:52

1 Answers1

0

If you must use a macro then typically it would be something like this:

#define WRITE_PORT(port, val) *((volatile uint8_t *)(port)) = (val)

and you might then invoke this as, e.g.

WRITE_PORT(portx, 0xff);  // write 0xff to portx

Note that this assumes an 8 bit port.

Note also the use of volatile, to prevent I/O reads/writes from being optimised away by the compiler.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • what is val? is *((volatile uint8_t *)(port)) the address of port mapped IO? – Sara Dec 15 '16 at 13:24
  • `val` is the second macro parameter, i.e. the value that will be written to the port. `*((volatile uint8_t *)(port))` simply casts the numeric port address to a pointer and then de-references that pointer in order to write to the address it points at. – Paul R Dec 15 '16 at 13:49
  • ok, thanks! so if IO-part is memory mapped to address 0x32 and the directive defines #define portx 0x32 the macro would be #define WRITE_PORT(port, val) – Sara Dec 17 '16 at 08:30
  • ok, thanks! so if IO-part is memory mapped to address 0x32 and the directive defines #define portx 0x32 the macro would be #define WRITE_PORT(port, val) 0x32 = (val) ? – Sara Dec 17 '16 at 08:33
  • Um, no, the whole point of making `port` and `val` parameters of the macro is that you can use the same macro with any port and any val. In your case you'd just invoke it as e.g. `WRITE_PORT(portx, 0xff);`, which would write `0xff` to the 8 bit port at memory address `0x32`. – Paul R Dec 17 '16 at 08:44
  • ok, this is now clear for me! thank you very much Paul :) – Sara Dec 18 '16 at 18:56
  • just to make sure: is the macro in my case #define WRITE_PORT(portx, val) *((volatile uint8_t *)(0x32)) = (val) – Sara Dec 19 '16 at 22:04
  • No, you've changed it from that is written above in the answer: `port` and `val` are *parameters*. The macro is meant to be *generic*, so you can pass any port address and any value. In your specific case you pass `portx` and whatever value you want to write to that port, as in the example I gave in the answer. – Paul R Dec 19 '16 at 22:11
  • Ok, so I don't need to mention to 0x32 in the macro? – Sara Dec 19 '16 at 22:19
  • No, you don't need to mention 0x32, or portx, or anything specific in the macro itself - good programming practice is is write things in a generic way, so that they can be re-used without modification. – Paul R Dec 19 '16 at 22:21
  • thank you for your explanations and help! – Sara Dec 19 '16 at 22:29
  • No problem - just take exactly what's written in the answer and make sure you fully understand every detail. – Paul R Dec 19 '16 at 22:33