palWriteGroup() macro

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

palWriteGroup() macro

Post by Tectu »

Morning folks,

Maybe a little stupid, the following question, but I don't get it...
I try to replace the following working code with some ChibiOS stuff to keep it plattfrom independend:

Code: Select all

LCD_DATA_PORT_1->BSRR = ((((~d >> 12 & 0xF) << 16) | (d >> 12 & 0xF)) << LCD_DATA_PORT_1_BASE);
LCD_DATA_PORT_2->BSRR = ((((~d >> 8 & 0xF) << 16) | (d >> 8 & 0xF)) << LCD_DATA_PORT_2_BASE);
LCD_DATA_PORT_3->BSRR = ((((~d >> 4 & 0xF) << 16) | (d >> 4 & 0xF)) << LCD_DATA_PORT_3_BASE);
LCD_DATA_PORT_4->BSRR = ((((~d >> 0 & 0xF) << 16) | (d >> 0 & 0xF)) << LCD_DATA_PORT_4_BASE);

d is a uint16_t.

I thought that the best way is using the palWriteGroup() macro.
I am just very confused about the offset the macro takes as an argument. Shouldn't this work?

Code: Select all

#define MASK(a) (15 << a)

palWriteGroup(LCD_DATA_PORT_1, MASK(LCD_DATA_PORT_1_BASE), LCD_DATA_PORT_1_BASE, d >> 12 & 0xF);
palWriteGroup(LCD_DATA_PORT_2, MASK(LCD_DATA_PORT_2_BASE), LCD_DATA_PORT_2_BASE, d >> 8 & 0xF);
palWriteGroup(LCD_DATA_PORT_3, MASK(LCD_DATA_PORT_3_BASE), LCD_DATA_PORT_3_BASE, d >> 4 & 0xF);
palWriteGroup(LCD_DATA_PORT_4, MASK(LCD_DATA_PORT_4_BASE), LCD_DATA_PORT_4_BASE, d >> 0 & 0xF);


Here are the defines of the different LCD macros:

Code: Select all

#define LCD_DATA_PORT_1         GPIOE
#define LCD_DATA_PORT_2         GPIOE
#define LCD_DATA_PORT_3         GPIOE
#define LCD_DATA_PORT_4         GPIOE
#define LCD_DATA_PORT_1_BASE   12
#define LCD_DATA_PORT_2_BASE   8
#define LCD_DATA_PORT_3_BASE   4
#define LCD_DATA_PORT_4_BASE   0



~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: palWriteGroup() macro

Post by Giovanni »

Mask is the mask of the group, if the group is composed of 4 bits the mask is 15 (0b1111), you can calculate it as (((1 << (group_width)) - 1)
Offset is the bit offset within the word of the group.
Bits is the value to be written aligned to bit 0 even if the group has another offset.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: palWriteGroup() macro

Post by Tectu »

Thank you for your answer, Giovanni.

After your explanation, this is how I understand it:

Code: Select all

palWriteGroup(LCD_DATA_PORT_1, PAL_GROUP_MASK(LCD_DATA_PORT_1_BASE), LCD_DATA_PORT_1_BASE, d >> 12 & 0xF);
palWriteGroup(LCD_DATA_PORT_2, PAL_GROUP_MASK(LCD_DATA_PORT_2_BASE), LCD_DATA_PORT_2_BASE, d >> 8 & 0xF);
palWriteGroup(LCD_DATA_PORT_3, PAL_GROUP_MASK(LCD_DATA_PORT_3_BASE), LCD_DATA_PORT_3_BASE, d >> 4 & 0xF);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(LCD_DATA_PORT_4_BASE), LCD_DATA_PORT_4_BASE, d >> 0 & 0xF);


I guess this is still wrong?!
I am not sure about the offset. You said "Offset is the bit offset..." What exactly does bit offset mean here?

LCD_DATA_PORT_X_BASE are all decimal "integers", for example 12, 8, 4 and 0.


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: palWriteGroup() macro

Post by Giovanni »

Those decimal integers are your bit offsets.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: palWriteGroup() macro

Post by Tectu »

Okay, they I have to ask, if you can tell me why this does not work.

I replaced:

Code: Select all

LCD_DATA_PORT_1->BSRR = ((((~d >> 12 & 0xF) << 16) | (d >> 12 & 0xF)) << LCD_DATA_PORT_1_BASE);
LCD_DATA_PORT_2->BSRR = ((((~d >> 8 & 0xF) << 16) | (d >> 8 & 0xF)) << LCD_DATA_PORT_2_BASE);
LCD_DATA_PORT_3->BSRR = ((((~d >> 4 & 0xF) << 16) | (d >> 4 & 0xF)) << LCD_DATA_PORT_3_BASE);
LCD_DATA_PORT_4->BSRR = ((((~d >> 0 & 0xF) << 16) | (d >> 0 & 0xF)) << LCD_DATA_PORT_4_BASE);


with:

Code: Select all

palWriteGroup(LCD_DATA_PORT_1, PAL_GROUP_MASK(LCD_DATA_PORT_1_BASE), LCD_DATA_PORT_1_BASE, d >> 12 & 0xF);
palWriteGroup(LCD_DATA_PORT_2, PAL_GROUP_MASK(LCD_DATA_PORT_2_BASE), LCD_DATA_PORT_2_BASE, d >> 8 & 0xF);
palWriteGroup(LCD_DATA_PORT_3, PAL_GROUP_MASK(LCD_DATA_PORT_3_BASE), LCD_DATA_PORT_3_BASE, d >> 4 & 0xF);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(LCD_DATA_PORT_4_BASE), LCD_DATA_PORT_4_BASE, d >> 0 & 0xF);



Thank you for your help

~ Tectu
Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm

Re: palWriteGroup() macro

Post by Abhishek »

Hi,

Could you try removing & 0xF after the bit shift and operation and test?

Abhishek
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: palWriteGroup() macro

Post by mabl »

Tectu,

I guess Giovanni wants you to learn the hard way by looking at the documentation 8-) :lol: But to help you, why don't you do:

Code: Select all

palWriteGroup(LCD_DATA_PORT_1, PAL_GROUP_MASK(4), LCD_DATA_PORT_1_BASE, d);
palWriteGroup(LCD_DATA_PORT_2, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_3, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: palWriteGroup() macro

Post by Tectu »

Abhishek wrote:Could you try removing & 0xF after the bit shift and operation and test?

I've already tried that. That didn't work too. But thanks for your help, of course :)


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: palWriteGroup() macro

Post by Giovanni »

Your problem is the mask, you can't possibly derived that from the offset, it is calculated from the group width using the expression I posted before or the macro PAL_GROUP_MASK() like mabl suggested.

Giovanni
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: palWriteGroup() macro

Post by mabl »

mabl wrote:Tectu,

I guess Giovanni wants you to learn the hard way by looking at the documentation 8-) :lol: But to help you, why don't you do:

Code: Select all

palWriteGroup(LCD_DATA_PORT_1, PAL_GROUP_MASK(4), LCD_DATA_PORT_1_BASE, d);
palWriteGroup(LCD_DATA_PORT_2, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_3, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(4), LCD_DATA_PORT_2_BASE, d);


My bad, I think it should read.

Code: Select all

palWriteGroup(LCD_DATA_PORT_1, PAL_GROUP_MASK(4) <<  0, LCD_DATA_PORT_1_BASE, d);
palWriteGroup(LCD_DATA_PORT_2, PAL_GROUP_MASK(4) <<  4, LCD_DATA_PORT_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_3, PAL_GROUP_MASK(4) <<  8, LCD_DATA_PORT_3_BASE, d);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(4) << 12, LCD_DATA_PORT_4_BASE, d);
Last edited by mabl on Mon Jun 25, 2012 9:11 am, edited 1 time in total.
Post Reply