Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.embedded > #85
| Date | 2012-02-09 10:50 +0100 |
|---|---|
| From | David Brown <david@westcontrol.removethisbit.com> |
| Newsgroups | comp.os.linux.advocacy, comp.os.linux.embedded |
| Subject | Re: Reading the Riot Act To ARM's developers |
| References | (1 earlier) <02c9e013-9c6b-4270-8b82-c7d16b51269c@sw7g2000pbc.googlegroups.com> <i3AYq.10307$as3.6991@newsfe27.ams2> <x_KdnVX1OrdqQa_SnZ2dnUVZ8oCdnZ2d@lyse.net> <%8CYq.29115$R87.2889@newsfe19.ams2> <4f337165$0$6982$e4fe514c@news2.news.xs4all.nl> |
| Message-ID | <g_adna_cJf5mCq7SnZ2dnUVZ8kqdnZ2d@lyse.net> (permalink) |
Cross-posted to 2 groups.
On 09/02/2012 08:09, Arie de Muynck wrote:
>
> "7" wrote in message news:%8CYq.29115$R87.2889@newsfe19.ams2...
>
>> The CMSIS libraries have to be written properly with structs
>> and unions. An example is this:
>>
>>
>> typedef struct tagLED_Register{
>> union
>> {
>> unsigned long All_LEDs; // this accesses all flags
>> struct
>> {
>> unsigned LED_Power:1;
>> unsigned LED_Fault:1;
>> unsigned :3; // unused - ignore
>> unsigned LED_Amber_Alert:1;
>> };
>> };
>> } struct_LED_Register;
>>
>>
>> struct_LED_Register LED_Register;
>> #define LED_ON 1
>> #define LED_OFF 0
>>
>> main()
>> {
>> LED_Register.All_LEDs = 0; // All LEDs off - directly write to all LEDs
>> LED_Register.LED_Amber_Alert = LED_ON; // write to individual LED
>> LED_Register.LED_Fault = LED_ON; // write to individual LED
>> }
>>
>>
>> From above, it is possible to write directly to registers
>> for speed, or choose to write to individual flags for clarity.
>> The idea of unions was invented to take of issues like this.
Unions were invented to allow flexible data structures that save space.
They were /not/ invented for type punning like this - type punning is
in fact undefined behaviour according to the C standards. That means if
you write data to one field in a union, reading a different field is
undefined behaviour. And a compiler could legally implement the union
as though it were a struct - it does not have to overlap the memory
between the fields.
Having said that, the validity of type punning is an unwritten rule that
is implemented by all C compilers, even though it is not in the standards.
>> So ARM's developers will now learn to use it! THANKS!!!!!!!!
>>
>
> As answered before, it will compile to non-functioning code using some ARM
> compilers (like GCC). They will create byte access operations to access the
> 32-bit peripheral registers, thrashing the other bytes. I actually had to
> rewrite a complete library that used this bit field cra^H^H^H stuff.
> Just compile it and then look at the resulting assembly code.
>
> Regards,
> Arie de Muynck
>
Newer versions of gcc have a "-fstrict-volatile-bitfield" flag to avoid
this problem (I'm assuming that in a real program, LED_Register would be
"volatile").
But it is /exactly/ this sort of thing that makes bitfields risky.
There are no standards defining how they are to be accessed - with the
above example, the compiler could choose to use 32-bit accesses, since
the fields are declared "unsigned" (though no one writing real code
would use "unsigned" or "unsigned long" in such circumstances - they
would always use size-specific types). It could also choose 8-bit
accesses, to avoid affecting neighbouring fields. Or it could choose
32-bit regardless of the field type, because those instructions are
faster on the ARM. Or it could pick 16-bit accesses just for fun.
Back to comp.os.linux.embedded | Previous | Next — Previous in thread | Next in thread | Find similar
Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-07 23:43 +0000
Re: Reading the Riot Act To ARM's developers cassiope <fpm@u.washington.edu> - 2012-02-08 09:51 -0800
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-08 19:29 +0000
Re: Reading the Riot Act To ARM's developers David Brown <david.brown@removethis.hesbynett.no> - 2012-02-08 21:37 +0100
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-08 21:52 +0000
Re: Reading the Riot Act To ARM's developers "Arie de Muynck" <nospam@nospam.com> - 2012-02-09 08:09 +0100
Re: Reading the Riot Act To ARM's developers David Brown <david@westcontrol.removethisbit.com> - 2012-02-09 10:50 +0100
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-09 21:51 +0000
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-02-10 15:41 -0500
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-02-09 07:44 -0500
Re: Reading the Riot Act To ARM's developers Bob Hauck <postmaster@avalanche.org> - 2012-02-09 08:28 -0500
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-09 23:38 +0000
Re: Reading the Riot Act To ARM's developers David Brown <david@westcontrol.removethisbit.com> - 2012-02-10 09:26 +0100
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-10 19:51 +0000
Re: Reading the Riot Act To ARM's developers David Brown <david.brown@removethis.hesbynett.no> - 2012-02-10 21:51 +0100
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-10 23:37 +0000
Re: Reading the Riot Act To ARM's developers Grant Edwards <invalid@invalid.invalid> - 2012-02-13 15:11 +0000
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-13 21:30 +0000
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-08 19:29 +0000
Re: Reading the Riot Act To ARM's developers Kelsey Bjarnason <kbjarnason@gmail.com> - 2012-02-10 21:25 -0800
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-11 09:27 +0000
Re: Reading the Riot Act To ARM's developers Kelsey Bjarnason <kbjarnason@gmail.com> - 2012-02-10 21:22 -0800
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-11 09:58 +0000
Re: Reading the Riot Act To ARM's developers David Brown <david.brown@removethis.hesbynett.no> - 2012-02-11 15:16 +0100
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-12 00:56 +0000
Re: Reading the Riot Act To ARM's developers David Brown <david.brown@removethis.hesbynett.no> - 2012-02-12 10:24 +0100
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-02-12 08:21 -0500
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-12 19:12 +0000
Re: Reading the Riot Act To ARM's developers Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2012-02-13 08:54 +0200
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-13 21:37 +0000
Re: Reading the Riot Act To ARM's developers Grant Edwards <invalid@invalid.invalid> - 2012-02-13 22:07 +0000
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-13 23:31 +0000
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-13 23:31 +0000
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-13 23:31 +0000
Re: Reading the Riot Act To ARM's developers Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-14 10:35 +0100
more troll spew from eternal-september.org 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-14 11:27 +0000
Re: more troll spew from eternal-september.org Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-14 13:17 +0100
Re: more troll spew from eternal-september.org Big Steel <SteelOne@SteelOne.com> - 2012-02-14 07:55 -0500
Re: more troll spew from eternal-september.org GreyCloud <mist@cumulus.com> - 2012-02-14 11:18 -0700
Re: more troll spew from eternal-september.org GreyCloud <mist@cumulus.com> - 2012-02-14 11:18 -0700
Re: more troll spew from eternal-september.org Foster <frankfoster50@yahoo.com> - 2012-02-14 09:09 -0500
Re: Reading the Riot Act To ARM's developers Foster <frankfoster50@yahoo.com> - 2012-02-14 08:38 -0500
Re: Reading the Riot Act To ARM's developers GreyCloud <mist@cumulus.com> - 2012-02-14 11:19 -0700
Re: Reading the Riot Act To ARM's developers GreyCloud <mist@cumulus.com> - 2012-02-14 11:17 -0700
Re: Reading the Riot Act To ARM's developers Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-14 21:50 +0100
Re: Reading the Riot Act To ARM's developers GreyCloud <mist@cumulus.com> - 2012-02-14 15:42 -0700
Re: Reading the Riot Act To ARM's developers Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-14 23:56 +0100
more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-14 22:34 +0000
Re: more troll spew GreyCloud <mist@cumulus.com> - 2012-02-15 00:12 -0700
Re: more troll spew Foster <frankfoster50@yahoo.com> - 2012-02-15 08:29 -0500
Re: more troll spew GreyCloud <mist@cumulus.com> - 2012-02-15 11:22 -0700
Re: more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-15 23:03 +0000
Re: more troll spew Foster <frankfoster50@yahoo.com> - 2012-02-15 18:07 -0500
Re: more troll spew GreyCloud <mist@cumulus.com> - 2012-02-15 16:36 -0700
Re: more troll spew GreyCloud <mist@cumulus.com> - 2012-02-15 16:34 -0700
Re: Reading the Riot Act To ARM's developers Kelsey Bjarnason <kbjarnason@gmail.com> - 2012-02-14 20:06 -0800
Re: Reading the Riot Act To ARM's developers chrisv <chrisv@nospam.invalid> - 2012-02-15 07:20 -0600
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-02-15 11:20 -0500
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-15 20:15 +0000
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-02-15 15:26 -0500
Re: Reading the Riot Act To ARM's developers Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-15 21:38 +0100
more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-15 22:56 +0000
Re: more troll spew Foster <frankfoster50@yahoo.com> - 2012-02-15 18:00 -0500
Re: more troll spew Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-16 00:06 +0100
Re: more troll spew Snit <usenet@gallopinginsanity.com> - 2012-02-15 16:28 -0700
Re: more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-16 19:48 +0000
Re: more troll spew Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-16 20:57 +0100
Re: more troll spew cc <scatnubbs@hotmail.com> - 2012-02-16 12:10 -0800
Re: more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-16 20:43 +0000
Re: more troll spew Peter Köhlmann <peter-koehlmann@t-online.de> - 2012-02-16 21:45 +0100
Re: more troll spew 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-16 21:20 +0000
Re: more troll spew Big Steel <SteelOne@SteelOne.com> - 2012-02-15 19:37 -0500
Re: Reading the Riot Act To ARM's developers David Brown <david@westcontrol.removethisbit.com> - 2012-02-13 09:12 +0100
Re: Reading the Riot Act To ARM's developers Kelsey Bjarnason <kbjarnason@gmail.com> - 2012-02-11 10:34 -0800
Re: Reading the Riot Act To ARM's developers Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-25 10:18 +0200
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-25 19:34 +0000
Re: Reading the Riot Act To ARM's developers Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-27 15:52 +0200
Re: Reading the Riot Act To ARM's developers David Brown <david.brown@removethis.hesbynett.no> - 2012-02-27 22:04 +0100
Re: Reading the Riot Act To ARM's developers Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-03 02:04 +0200
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-03-03 00:20 +0000
Re: Reading the Riot Act To ARM's developers GreyCloud <cumulus@mist.com> - 2012-03-03 00:06 -0700
Re: Reading the Riot Act To ARM's developers Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-03 15:22 +0200
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-03-04 12:28 +0000
Re: Reading the Riot Act To ARM's developers "Ezekiel" <zeke@nosuchemail.com> - 2012-03-04 09:15 -0500
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-03-04 21:29 +0000
While( Ch ) if ( Ch‑1 <= 32 ) *P -= 32 ; Jeff-Relf.Me <x@x.Invalid> - 2012-02-28 05:52 -0800
Re: While( Ch ) if ( Châ1 <= 32 ) *P -= 32 ;  Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-03 02:09 +0200
Re: Reading the Riot Act To ARM's developers 7 <email_at_www_at_enemygadgets_dot_com@enemygadgets.com> - 2012-02-25 19:34 +0000
csiph-web