palWriteGroup() macro

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

Moderators: RoccoMarco, lbednarz, tfAteba

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 »

mabl wrote:
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_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(4) << 12, LCD_DATA_PORT_2_BASE, d);


The first one was correct, the mask does not need to be shifted. The function is a simple macro:

Code: Select all

  palWritePort(port, (palReadLatch(port) & ~((mask) << (offset))) |     \
                     (((bits) & (mask)) << (offset)))


"bits" and "mask" are shifted by "offset" internally.

Giovanni
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 »

The STM32 version is optimized for the HW, it is implemented differently but does the same thing:

Code: Select all

#define pal_lld_writegroup(port, mask, offset, bits)                        \
  ((port)->BSRR.W = ((~(bits) & (mask)) << (16 + (offset))) |               \
                     (((bits) & (mask)) << (offset)))


The advantage is that the above implementation is atomic, the default implementation in the previous post is not.

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

Re: palWriteGroup() macro

Post by mabl »

Giovanni wrote:
mabl wrote:
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_2_BASE, d);
palWriteGroup(LCD_DATA_PORT_4, PAL_GROUP_MASK(4) << 12, LCD_DATA_PORT_2_BASE, d);


The first one was correct, the mask does not need to be shifted. The function is a simple macro:

Code: Select all

  palWritePort(port, (palReadLatch(port) & ~((mask) << (offset))) |     \
                     (((bits) & (mask)) << (offset)))


"bits" and "mask" are shifted by "offset" internally.

Giovanni


Hehe, I think it get's confusing quite quickly. But you are wrong, I missed the shifts on the data. So

Code: Select all

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


Is my bet now. Hopfully it's correct now :twisted: Otherwise I just keep silent now.
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 »

OMG, I missed that part :)

It should be final.

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

Re: palWriteGroup() macro

Post by Tectu »

mabl wrote:Is my bet now. Hopfully it's correct now :twisted: Otherwise I just keep silent now.


Giovanni wrote:OMG, I missed that part :)

It should be final.

Giovanni

Still not right. This does the job:

Code: Select all

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


@mabl: I am a guy who always reads the documentation first - I know how it is when you write a shitload of documentation and people don't even read it then. I wouldn't even know about the two macros, when I wouldn't have read it. I was just confused about the playing together with the mask and the offset. I tried to understand the macro, but because I didn', I asked here. I thought this forum might be there for such questions ;)

Thank you for your help, guys!
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: palWriteGroup() macro

Post by mabl »

Hi Tectu,

this reverse shift order is only due on how you choose to label your ports. I'd propose to do it the other way around, since it's more common to start numbering from the lower bits.

What you could also think about is grouping several IOBus together and specify the shifts. That way you would be totally generic from how many ports you split. But I don't know the speed trade-off for that.
Last edited by mabl on Mon Jun 25, 2012 9:41 am, edited 1 time in total.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: palWriteGroup() macro

Post by Tectu »

Hey mabl,

Yeah, I already took a look at the IOBus stuff after Giovanni pointed to it. I am not sure about the performance either. But the thing I worry more is the configuration. I couldn't figure out how to use it a way the configuration for the user is easy and the sourcecode is still clean and readable.

Maybe Giovanni can give some more informations to this?


~ 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 »

The IOBus structure contains the same parameters of the "group" functions but bus functions have a performance impact compared to "groups". Groups are inlined and become a direct access to the GPIO register in the generated asm code.

The advantage with IOBus is that you could have those supplied from outside the library, you just do writes on the bus without knowing how it is done.

One thing I don't like is splitting the write in 4 operations, that will hurt performance a lot. You should assume from 1 to N "groups" of variable size (not always 4 bits).
The best way would be to expect an external write_lcd() function (for example) and do not try to abstract it internally, the integrator would have to provide the function.

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

Re: palWriteGroup() macro

Post by Tectu »

Giovanni wrote:The best way would be to expect an external write_lcd() function (for example) and do not try to abstract it internally, the integrator would have to provide the function.

That solutions dosen't sound that bad. That would be two functions (one writing 16-bit and one reading 16-bit).

I'll think about it. Thank you very much for your suggestion!


~ Tectu
avtop
Posts: 6
Joined: Tue Sep 17, 2013 8:33 am

Re: palWriteGroup() macro

Post by avtop »

I want drive LCD that is 16bit wide data interface but I dosn't have single port 16bit wide free so palWriteGroup,IOBus,IOBus_DATA can I declare 2 port bits as SINGLE BUS
what is deference between palWriteGroup,palWriteBus and palWritePort
Post Reply