<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Using target&amp;#39;s settings in the program code</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/22211/using-target-s-settings-in-the-program-code</link><description> 
Hello, Keil uVision world! :) 

 
Keil uVision project file contains some target&amp;#39;s settings, stored
as e.g. Device (C8051F340) or DevID (3997) . How to use
these values in the source text (e.g. for conditionally
translation)? 

 
For example: 

 
#if</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Using target's settings in the program code</title><link>https://community.arm.com/thread/100535?ContentTypeID=1</link><pubDate>Fri, 02 May 2008 04:53:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d4c08968-b545-4417-bbf8-4e490e8be274</guid><dc:creator>Paul Vertebnyy</dc:creator><description>&lt;p&gt;&lt;p&gt;
The answer from Keil:&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;currently this information cannot be directly accessed by the
Compiler/Assembler. It is indented to be used by programming
utilities and the help systems (by querying the environment
variables).&lt;/i&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using target's settings in the program code</title><link>https://community.arm.com/thread/76199?ContentTypeID=1</link><pubDate>Fri, 18 Apr 2008 10:03:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ed882da6-2a8b-4d73-b417-7ef273f1dfee</guid><dc:creator>Paul Vertebnyy</dc:creator><description>&lt;p&gt;&lt;p&gt;
Cpt. Vince, thank you for detailed info.&lt;/p&gt;

&lt;p&gt;
Cpt. Vince wrote:&lt;br /&gt;
&lt;i&gt;Since the Keil tool doesn&amp;#39;t have a predefined constant ... you can
create it yourself.&lt;br /&gt;
...&lt;br /&gt;
Each compile configuration uses a target profile...&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Here you can define Project Targets, Groups, and the files for
your project. You can have several different target groups using
different target processors...&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
I use &lt;b&gt;target setings&lt;/b&gt; in all my projects. ;)&lt;/p&gt;

&lt;p&gt;
Cpt. Vince wrote:&lt;br /&gt;
&lt;i&gt;Under the Keil&amp;#39;s IDE Options for Target &amp;quot;C51&amp;quot; Tab you can
predefine constants...&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Beacuse of developpment and support many similar projects (in
fact, they are &amp;quot;derived&amp;quot; from a few of &amp;quot;roots&amp;quot;) differing on
miscellaneous functions, I found inconvenient to introduce and check
a lot of settings (the same for C and ASM!). Especial for libraries
developpment. Therefore my projects have now only one constant
defined via IDE: CPU type. :)&lt;/p&gt;

&lt;p&gt;
Using of the Keil Configuration Wizard - like &amp;quot;GUI settings
dialog&amp;quot; - is more comfortable. IMHO.&lt;/p&gt;

&lt;p&gt;
Cpt. Vince wrote:&lt;br /&gt;
&lt;i&gt;As a general rule though, I don&amp;#39;t like IDE specific settings and
prefer to control the build process as much as I can within the
&amp;quot;controlled source [code]&amp;quot; itself. By having an include file that
specifies the processor target, you can define it there as you would
normally do anyway...&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
CPU type is already defined via Keil IDE - why don&amp;#39;t use this?&lt;/p&gt;

&lt;p&gt;
Cpt. Vince wrote:&lt;br /&gt;
&lt;i&gt;If you have source code that depends upon your specific IDE like
(Keil&amp;#39;s tool suite), then you are limiting yourself and code when/if
your company changes over to IAR&amp;#39;s tools, or BSO Tasking, etc.&lt;br /&gt;
...&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Sure. But I can define &amp;quot;manually&amp;quot; the same constants, #defines
etc. by using other tools too - without loss of compatibility. Or
nearly without...&lt;/p&gt;

&lt;p&gt;
Language extensions (e.g., using C-Preprocessor directives in ASM
source code) could cause more problems by code porting. But these
extensions are comfortable! Schould one not use they and stay in
197..? ;)&lt;/p&gt;

&lt;p&gt;
So every developper find own way, own style etc. IMHO.&lt;/p&gt;

&lt;p&gt;
P.S. Sorry for my english...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using target's settings in the program code</title><link>https://community.arm.com/thread/49744?ContentTypeID=1</link><pubDate>Fri, 18 Apr 2008 08:38:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0fa148f4-d284-4d36-8606-479e226232b3</guid><dc:creator>Cpt. Vince</dc:creator><description>&lt;p&gt;&lt;p&gt;
Since the Keil tool doesn&amp;#39;t have a predefined constant (per Andy
statement and not my own research---but Andy is a good authority)
that represents the current processor setting, you can create it
yourself.&lt;/p&gt;

&lt;p&gt;
Each compile configuration uses a target profile, so you can use
this as a mechanism to define the processor ID and then use it within
the code as needed. Since you would change this profile anyway to use
a different micro-controller, this does make sense.&lt;/p&gt;

&lt;p&gt;
Each target profile is managed in the &amp;quot;Components, Environments
and books&amp;quot; using the &amp;quot;Project Components&amp;quot; tab. Here you can define
Project Targets, Groups, and the files for your project. You can have
several different target groups using different target
processors.&lt;/p&gt;

&lt;p&gt;
Under the Keil&amp;#39;s IDE Options for Target &amp;quot;C51&amp;quot; Tab you can
predefine constants.&lt;/p&gt;

&lt;p&gt;
You can, under your target configuration setting, define the
&amp;quot;DEVICE_ID 3997&amp;quot; (no quotes) and then test for it within the source
code. (or &amp;quot;C8051F340 1&amp;quot; then #ifdef C8051F340)&lt;/p&gt;

&lt;p&gt;
Watch the lower part of the tab area under &amp;quot;Compiler control
string&amp;quot; and see that what you are typing in, is auto inserted using
the compiler directive DEFINE( , ).&lt;/p&gt;

&lt;p&gt;
As a general rule though, I don&amp;#39;t like IDE specific settings and
prefer to control the build process as much as I can within the
&amp;quot;controlled source [code]&amp;quot; itself. By having an include file that
specifies the processor target, you can define it there as you would
normally do anyway.&lt;/p&gt;

&lt;p&gt;
If you have source code that depends upon your specific IDE like
(Keil&amp;#39;s tool suite), then you are limiting yourself and code when/if
your company changes over to IAR&amp;#39;s tools, or BSO Tasking, etc.&lt;/p&gt;

&lt;p&gt;
I &lt;b&gt;do&lt;/b&gt; like the fact that Keil has features like:&lt;/p&gt;

&lt;pre&gt;
/*

The XL51 has some built-in features that are handy to use.
[Only] the LX51 allocates them &lt;b&gt;if they are used in the final code&lt;/b&gt;
(BL51 doesn&amp;#39;t have this feature)

A list class names

bit
code
const
data
ecode
hconst
hdata
idata
srom
xdata

_class-name_S_  The starting address of the class.
_class-name_B_  The code bank in which the class is stored.
_class-name_L_  The length of the class in bytes.
_class-name_E_  The ending address of the class.
_class-name_T_  The target (execution) address of the class.

*/
// examples of using these pre-defined Keil IDE data allocation information
// LX51 pre-defines these data-stores (only upon use of it)
extern unsigned char _code_B_;  //
extern unsigned char _code_S_;  //
extern unsigned char _code_E_;  // Ending address of code space
extern unsigned char _code_T_;  //
extern unsigned char _code_L_;  //

extern unsigned char _bit_L_;   //
extern unsigned char _const_L_; //
extern unsigned char _data_L_;  //
extern unsigned char _idata_L_; //
extern unsigned char _xdata_L_; // Length of xdata space
extern unsigned char _ecode_L_; //
extern unsigned char _srom_L_;  //
extern unsigned char _hconst_L_;
extern unsigned char _hdata_L_; //

extern unsigned char _idata_E_; //

u16 code_length = (u16) &amp;amp;(_code_L_);
u16 code_bank   = (u16) &amp;amp;(_code_B_);
u16 code_start  = (u16) &amp;amp;(_code_S_);
u16 code_end    = (u16) &amp;amp;(_code_E_);
u16 code_target = (u16) &amp;amp;(_code_T_);

u8 idata_end    = (u8) &amp;amp;(_idata_E_);

u16 bit_length   = (u16) &amp;amp;(_bit_L_  );
u16 const_length = (u16) &amp;amp;(_const_L_);
u16 data_length  = (u16) &amp;amp;(_data_L_ );
u16 idata_length = (u16) &amp;amp;(_idata_L_);
u16 xdata_target = (u16) &amp;amp;(_xdata_L_);
u16 ecode_length = (u16) &amp;amp;(_ecode_L_);
u16 srom_target  = (u16) &amp;amp;(_srom_L_);
u16 hconst_target= (u16) &amp;amp;(_hconst_L_);
u16 hdata_length = (u16) &amp;amp;(_hdata_L_);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Other handy Keil specific features are&lt;/p&gt;

&lt;pre&gt;
&lt;br /&gt;
// There are two (2) leading and trailing underscore characters (&amp;#39;_&amp;#39;)&lt;br /&gt;
// used for the predefined constants.

__C51__     // Version number of the compiler (for example, 701 for version 7.01)&lt;br /&gt;
__DATE__    // Date when the compilation was started in ANSI format (month dd yyyy).&lt;br /&gt;
__DATE2__   // Date when the compilation was started in short form (mm/dd/yy).&lt;br /&gt;
__TIME__    // Time when the compilation was started. (Format: hh:mm:ss)&lt;br /&gt;
__FILE__    // Name of the file being compiled.&lt;br /&gt;
__LINE__    // Current line number in the file being compiled.&lt;br /&gt;
__MODEL__   // Memory model selected:
            //    * 1 for SMALL,
            //    * 2 for COMPACT,
            //    * 3 for LARGE.&lt;br /&gt;
__STDC__    // Defined to 1 to indicate full conformance with the ANSI C Standard.&lt;br /&gt;
__KEIL__    // Defined if Keil is used

// example from the &amp;#39;book&amp;#39;&lt;br /&gt;
char code Compile_date[] = &amp;quot;Compile Date = &amp;quot; __DATE__;&lt;br /&gt;
char code Compile_time[] = &amp;quot;Compile Time = &amp;quot; __TIME__;&lt;br /&gt;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Example of their use:&lt;/p&gt;

&lt;pre&gt;
#ifndef __KEIL__
    #error    &amp;quot;This source code is highly dependant upon the Keil IDE toolset&amp;quot;
    #message  &amp;quot;NOTE: &amp;quot;
    #message  &amp;quot;Please refer to the Compiler.H file to see how this source code&amp;quot;
    #message  &amp;quot;depends way too heavily on a specific tool suite.&amp;quot;
#endif

#ifdef __KEIL__ // So if the compiler IS Keil, then make sure its the right one

#if (__C51__ &amp;lt; 700) || (__C51__ &amp;gt; 799)      // example right out of the &amp;#39;book&amp;#39;
    #error &amp;quot;Requires C51 Compiler V7.xx&amp;quot;    // example right out of the &amp;#39;book&amp;#39;
#endif

#if (__MODEL__ &amp;lt; 3)
    #warning &amp;quot;This code will not work on any environment that is not set for LARGE&amp;quot;
    #message &amp;quot; NOTE: &amp;quot;
    #message &amp;quot;The code contains a look up table that that is 16K x 8.  Without that&amp;quot;
    #message &amp;quot;table, the code can fit in SMALL: We lost the arguement for run-time&amp;quot;
    #message &amp;quot;calculation versus code size using the LUT... yell at Joe if the&amp;quot;
    #message &amp;quot;CPU oscillator changed from 11.0592MHz to 24MHz as that is the&amp;quot;
    #message &amp;quot;threashold speed needed to convert this code to run-time calcs,&amp;quot;
    #message &amp;quot;and allow the removal of the external memory stores.&amp;quot;
#endif

#endif // end of the &amp;quot;If it was KEIL&amp;quot; conditional
&lt;/pre&gt;

&lt;p&gt;
--Cpt. Vince Foster&lt;br /&gt;
2nd Cannon Place&lt;br /&gt;
Fort Marcy Park, VA&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using target's settings in the program code</title><link>https://community.arm.com/thread/76198?ContentTypeID=1</link><pubDate>Thu, 17 Apr 2008 13:06:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d6c3ae0c-92ac-461a-bfa0-86b7c89efa49</guid><dc:creator>Paul Vertebnyy</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;... but Keil don&amp;#39;t do it.&lt;br /&gt;
:-(&lt;/i&gt;&lt;br /&gt;
Pity! Keil don&amp;#39;t do some other thins too, e.g.: &lt;a href="http://www.keil.com/forum/docs/thread11228.asp#msg54097"&gt;http://www.keil.com/forum/docs/thread11228.asp#msg54097&lt;/a&gt;
;)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using target's settings in the program code</title><link>https://community.arm.com/thread/49736?ContentTypeID=1</link><pubDate>Thu, 17 Apr 2008 12:31:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ca03efa2-c2e2-4c46-ae52-964c8d51b51b</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
... but Keil don&amp;#39;t do it.&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;:-(&lt;/b&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>