Page 1 of 3

palWriteGroup() macro

Posted: Sun Jun 24, 2012 9:18 pm
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 7:59 am
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:26 am
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:32 am
by Giovanni
Those decimal integers are your bit offsets.

Giovanni

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:53 am
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:55 am
by Abhishek
Hi,

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

Abhishek

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:59 am
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);

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 8:59 am
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 9:05 am
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

Re: palWriteGroup() macro

Posted: Mon Jun 25, 2012 9:07 am
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);