CANTxFrame outside threads.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
duracell
Posts: 24
Joined: Tue Jul 05, 2016 8:55 am
Has thanked: 4 times
Been thanked: 1 time

CANTxFrame outside threads.

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: CANTxFrame outside threads.

Post by Giovanni »

Where is the code you moved outside?

Giovanni
duracell
Posts: 24
Joined: Tue Jul 05, 2016 8:55 am
Has thanked: 4 times
Been thanked: 1 time

Re: CANTxFrame outside threads.

Post 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]
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: CANTxFrame outside threads.

Post by Giovanni »

You cannot initialize the variable that way outside a function, use a C initializer:

var = {
....
};

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: CANTxFrame outside threads.

Post 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]
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: CANTxFrame outside threads.

Post by Giovanni »

It is the fifth button from left on the toolbar lol.

Giovanni
duracell
Posts: 24
Joined: Tue Jul 05, 2016 8:55 am
Has thanked: 4 times
Been thanked: 1 time

Re: CANTxFrame outside threads.

Post by duracell »

Thank you very much Giovanni and rew!
Post Reply