View previous topic :: View next topic |
Author |
Message |
micsch
Joined: 06 Dec 2011 Posts: 4
|
error 51 in setup_adc_ports(... |
Posted: Tue Dec 06, 2011 4:01 am |
|
|
Hi,
why does the following instruction generate the error 51 (A numeric expression must appear here):
Code: |
#include <16F913.h>
typedef unsigned int uint8_type;
uint8_type tmpmask = 0U;
void main (void)
{
tmpmask = sAN2;
tmpmask |= sAN4;
tmpmask |= VSS_VREF;
setup_adc_ports( tmpmask ); //get error
...
setup_adc_ports(sAN2|sAN4|VSS_VREF); //it is ok, but I can not use it because a misra rule
...
}
|
Thank you for your help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Dec 06, 2011 4:13 am |
|
|
It will.
The setup, generates code at _compile time_ to implement the settings required. It _won't_ accept a variable.
From the manual:
"value - a constant defined in the devices .h file".
_Constant_...
If you need different setups in different situations, you need to have multiple setup lines, and so your own tests to determine which ones to use.
However, MISRA, does not prevent you from using the standard line:
"It is recognized that it may be necessary to raise deviations (as described in section 5.3.2) to permit certain language extensions, for example to support hardware specific features."
Configuring the ADC, is such a feature.
What you should do though, is do all the setup in a single block, 'init_hardware' for example, load this first, and document what each operation does. Better anyway, and makes it plain what you are doing, and why.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Tue Dec 06, 2011 6:19 am |
|
|
I have to ask...
WHY be concerned about MISRA ? MISRA C appears to only be relative to ISO C NOT other forms,like CCS C.
It seems highly unlikely that you're creating a device that needs MISRA 'certification' and since the 'guidlines' are not free why pay to follow what's NOT applicable in the source code you're writing in
Bottom line, what gets blasted into the PIC is machine code, not C source,so unless the MISRA 'guidelines' KNOW what code is produced for a given line of code, how the devil can it say 'yes' or 'no' to your code?
Maybe I'm missing something in the 'picture', but trying to conform to a 'standard' for another language seems to be a rediculous waste of time. |
|
|
micsch
Joined: 06 Dec 2011 Posts: 4
|
|
Posted: Tue Dec 06, 2011 7:42 am |
|
|
It is amazing, when I try something like that:
Code: |
tmpmask = RTCC_INTERNAL;
tmpmask |= RTCC_DIV_16;
setup_timer_0( tmpmask );
//or
tmpmask = T1_INTERNAL;
tmpmask |= T1_DIV_BY_8;
setup_timer_1( tmpmask );
|
no errors have been occurred by the compiler, but why? Considered the code above, it is similar.
@temtronic
In fact, it seems strange. We have a device with a uC and the code written by someone extern. My task ist to verify the software written in C and certify if everything is all right. So in my opinion the Misra-rules are a good tool to test how is the code written and get a standard. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Dec 06, 2011 8:09 am |
|
|
The manual states that setup_adc_ports requires a constant as input. Maybe you are lucky for the setup_timer_0 function not to be that restrictive, or, maybe it compiles but with the wrong output....???
I think you have to consider carefully what you want to achieve. MISRA is just one of the many initiatives to write better C code. Great, but MISRA was never developed with the CCS compiler in mind so you will run into compatibility problems.
In my opinion you have the following options:
1) You can put a lot of effort to make your CCS program pass the MISRA test.
2) You can accept MISRA will give some false positives.
3) Forget about MISRA and use one of the many other static analysis tools.
4) Forget about automated tools and do a manual code review.
I think no. 4, a manual code review, should always be performed on a new project. Using a tool is nice as additional test and/or in automated build environments.
People using a tool as the first test method are in my opinion using the tool to hide their own lack of programming knowledge. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Tue Dec 06, 2011 8:27 am |
|
|
Well I still say it's 'silly' to use a 3rd party's idea of what ISO C is supposed to look like to verify a nonISO version of C.
I'd like to know how MISRA handles the 'standard IO' default since 'flips' I/O pins DDR dynamically depending on the programs code.
Also how the C source gets compiled into machine instructions is important. We all could write a chunk of code that does the same function , yet one is fast,one takes up less memory,one is 'universal', one takes up a lot of memory and is slow....
what's 'right' as far as MISRA dictates ?? |
|
|
|