Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any C standard for microcontrollers?

Is there any special C standard for microcontrollers?

I ask because so far when I programmed something under Windows OS, it doesn't matter which compiler I used. If I had a compiler for C99, I knew what I could do with it.

But recently I started to program in C for microcontrollers, and I was shocked, that even it's still C in its basics, like loops, variables creation and so, there is some syntax type I have never seen in C for desktop computers. And furthermore, the syntax is changing from version to version. I use AVR-GCC compiler, and in previous versions, you used a function for port I/O, now you can handle a port like a variable in the new version.

What defines what functions and how to have them to be implemented into the compiler and still have it be called C?

like image 922
B.Gen.Jack.O.Neill Avatar asked Jul 23 '10 14:07

B.Gen.Jack.O.Neill


2 Answers

Is there any special C standard for microcontrollers?

No, there is the ISO C standard. Because many small devices have special architecture features that need to be supported, many compilers support language extensions. For example because an 8051 has bit addressable RAM, a _bit data type may be provided. It also has a Harvard architecture, so keywords are provided for specifying different memory address spaces which an address alone does not resolve since different instructions are required to address these spaces. Such extensions will be clearly indicated in the compiler documentation. Moreover, extensions in a conforming compiler should be prefixed with an underscore. However, many provide unadorned aliases for backward compatibility, and their use should be deprecated.

... when I programmed something under Windows OS, it doesn't matter which compiler I used.

Because the Windows API is standardized (by Microsoft), and it only runs on x86, so there is no architectural variation to consider. That said, you may still see FAR, and NEAR macros in APIs, and that is a throwback to 16-bit x86 with its segmented addressing, which also required compiler extensions to handle.

... that even it's still C in its basics, like loops, variables creation and so,

I am not sure what that means. A typical microcontroller application has no OS or a simple kernel, you should expect to see a lot more 'bare metal' or 'system-level' code, because there are no extensive OS APIs and device driver interfaces to do lots of work under the hood for you. All those library calls are just that; they are not part of the language; it is the same C language; jut put to different work.

... there is some syntax type I have never seen in C for desktop computers.

For example...?

And furthermore, the syntax is changing from version to version.

I doubt it. Again; for example...?

I use AVR-GCC compiler, and in previous versions, you used a function for port I/O, now you can handle a port like a variable in the new version.

That is not down to changes in the language or compiler, but more likely simple 'preprocessor magic'. On AVR, all I/O is memory mapped, so if for example you include the device support header, it may have a declaration such as:

#define PORTA (*((volatile char*)0x0100))

You can then write:

PORTA = 0xFF;

to write 0xFF to memory mapped the register at address 0x100. You could just take a look at the header file and see exactly how it does it.

The GCC documentation describes target specific variations; AVR is specifically dealt with here in section 6.36.8, and in 3.17.3. If you compare that with other targets supported by GCC, it has very few extensions, perhaps because the AVR architecture and instruction set were specifically designed for clean and efficient implementation of a C compiler without extensions.

What defines what functions and how to have them to be implemented into the compiler and still have it be called C?

It is important to realise that the C programming language is a distinct entity from its libraries, and that functions provided by libraries are no different from the ones you might write yourself - they are not part of the language - so it can be C with no library whatsoever. Ultimately, library functions are written using the same basic language elements. You cannot expect the level of abstraction present in, say, the Win32 API to exist in a library intended for a microcontroller. You can in most cases expect at least a subset of the C Standard Library to be implemented since it was designed as a systems level library with few target hardware dependencies.

I have been writing C and C++ for embedded and desktop systems for years and do not recognise the huge differences you seem to perceive, so can only assume that they are the result of a misunderstanding of what constitutes the C language. The following books may help.

  • C Programming Language (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie
  • Embedded C by Michael J. Pont
like image 114
Clifford Avatar answered Sep 18 '22 12:09

Clifford


Embedded systems are weird and sometimes have exceptions to "standard" C.

From system to system you will have different ways to do things like declare interrupts, or define what variables live in different segments of memory, or run "intrinsics" (pseudo-functions that map directly to assembly code), or execute inline assembly code.

But the basics of control flow (for/if/while/switch/case) and variable and function declarations should be the same across the board.

and in previous versions, you used function for Port I/O, now you can handle Port like variable in new version.

That's not part of the C language; that's part of a device support library. That's something each manufacturer will have to document.

like image 30
Jason S Avatar answered Sep 18 '22 12:09

Jason S