Given the following minimal working example. I tested this initially with uint16_t and it worked with no warnings. After that, I adjusted everything to work with uint32_t.
#include <stdint.h> /* needed for the uint16_t type*/
#include <inttypes.h> /* needed for sscanf uint16_t format placeholder */
#include "printf.h"
#define SET_CMD_ID(ID) ( (ID) << 27)
#define SET_CMD_CMD(CMD) ( (CMD) << 25)
#define SET_CMD_TYPE(TYPE) ( (TYPE) << 19)
#define SET_CMD_PKT(PKT) ( (PKT) )
void setup() {
Serial.begin(115200);
printf_begin();
Serial.print(F("\n\rExample command\n\r: "));
}
void loop() {
uint32_t payload = 0;
uint8_t id = 3; // 00000 - 5
uint8_t cmd = 2; // 00 - 2
uint8_t type = 1; // 000000 - 6
uint32_t pkt = 0; // 0000000000000000000 - 19
payload =
SET_CMD_ID(id) |
SET_CMD_CMD(cmd) |
SET_CMD_TYPE(type) |
SET_CMD_PKT(pkt);
Serial.print(F("Now sending "));
Serial.println(payload);
delay(2000); // Try again soon
}
Verifying this sketch outputs:
/path/to/ino/Arduino32bitTest.ino: In function 'void loop()':
/path/to/ino/Arduino32bitTest.ino:5:37: warning: left shift count >= width of type [enabled by default]
#define SET_CMD_ID(ID) ( (ID) << 27)
^
/path/to/ino/Arduino32bitTest.ino:28:9: note: in expansion of macro 'SET_CMD_ID'
SET_CMD_ID(id) |
^
/path/to/ino/Arduino32bitTest.ino:6:39: warning: left shift count >= width of type [enabled by default]
#define SET_CMD_CMD(CMD) ( (CMD) << 25)
^
/path/to/ino/Arduino32bitTest.ino:29:9: note: in expansion of macro 'SET_CMD_CMD'
SET_CMD_CMD(cmd) |
^
/path/to/ino/Arduino32bitTest.ino:7:41: warning: left shift count >= width of type [enabled by default]
#define SET_CMD_TYPE(TYPE) ( (TYPE) << 19)
^
/path/to/ino/Arduino32bitTest.ino:30:9: note: in expansion of macro 'SET_CMD_TYPE'
SET_CMD_TYPE(type) |
^
Sketch uses 3,174 bytes (9%) of program storage space. Maximum is 32,256 bytes.
Global variables use 216 bytes (10%) of dynamic memory, leaving 1,832 bytes for local variables. Maximum is 2,048 bytes.
Executing the sketch on an Arduino Uno results in:
Example command
: Now sending 0
Now sending 0
Now sending 0
Now sending 0
I googled the message of the warning and found out that it could mean that I'm trying to shift 32bit which is undefined, but I'm not sure on that. So my question is: What am I doing wrong here? How can I get this to work?