Page 1 of 1

CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 10:30 am
by duracell
Hi everybody!

I am not sure this is the right section of the forum to ask the question, however I have the following question.
I have opened the STM32F4xx-CAN project embedded in Chibistudio GCC 4.9 that contains the thread :

[spoil]static THD_FUNCTION(can_tx, p) {
CANTxFrame txmsg;

(void)p;
chRegSetThreadName("transmitter");
txmsg.IDE = CAN_IDE_EXT;
txmsg.EID = 0x01234567;
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = 8;
txmsg.data32[0] = 0x55AA55AA;
txmsg.data32[1] = 0x00FF00FF;

while (!chThdShouldTerminateX()) {
canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
chThdSleepMilliseconds(500);
}
}[/spoil]



I have tried to move the declaration and the data assignment of txmsg outside the thread (I want to use it on a callback of an interrupt), but the compiler gave me the following result:


[spoil]11:20:49 **** Incremental Build of configuration Default for project STM32F4xx-CAN ****
make -j4 all
Compiling main.c
main.c:72:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.IDE = CAN_IDE_EXT;
^
main.c:73:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.EID = 0x01234567;
^
main.c:74:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.RTR = CAN_RTR_DATA;
^
main.c:75:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.DLC = 8;
^
main.c:76:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.data32[0] = 0x55AA55AA;
^
main.c:77:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
txmsg.data32[1] = 0x00FF00FF;
^
make: *** [build/obj/main.o] Error 1

11:20:53 Build Finished (took 4s.720ms)[/spoil]



It seems that I cannot use this type outside of the thread. Why?

P.S. obviously I have commented the code in the thread that I moved outside.




I thank you in advance for your help.

Re: CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 11:20 am
by Giovanni
Where is the code you moved outside?

Giovanni

Re: CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 12:50 pm
by duracell
It is just above the thread.


[spoil]CANTxFrame txmsg;

txmsg.IDE = CAN_IDE_EXT;
txmsg.EID = 0x01234567;
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = 8;
txmsg.data32[0] = 0x55AA55AA;
txmsg.data32[1] = 0x00FF00FF;




static THD_WORKING_AREA(can_tx_wa, 256);
static THD_FUNCTION(can_tx, p) {
// CANTxFrame txmsg;

(void)p;
chRegSetThreadName("transmitter");
// txmsg.IDE = CAN_IDE_EXT;
// txmsg.EID = 0x01234567;
// txmsg.RTR = CAN_RTR_DATA;
// txmsg.DLC = 8;
// txmsg.data32[0] = 0x55AA55AA;
// txmsg.data32[1] = 0x00FF00FF;

while (!chThdShouldTerminateX()) {
canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
chThdSleepMilliseconds(500);
}
}[/spoil]

Re: CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 2:39 pm
by Giovanni
You cannot initialize the variable that way outside a function, use a C initializer:

var = {
....
};

Giovanni

Re: CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 9:20 pm
by rew
That would become:
(can't find the code tag either. Let's go with spoiler too.. :-) )
[spoil]CANTxFrame txmsg = {
.IDE = CAN_IDE_EXT;
.EID = 0x01234567;
.RTR = CAN_RTR_DATA;
.DLC = 8;
.data32 = {0x55AA55AA, 0x00FF00FF};
};[/spoil]

Re: CANTxFrame outside threads.

Posted: Thu Jul 14, 2016 9:30 pm
by Giovanni
It is the fifth button from left on the toolbar lol.

Giovanni

Re: CANTxFrame outside threads.

Posted: Fri Jul 15, 2016 2:00 pm
by duracell
Thank you very much Giovanni and rew!