Author Topic: C word  (Read 56727 times)

0 Members and 2 Guests are viewing this topic.

Online rstofer

  • Super Contributor
  • ***
  • Posts: 10093
  • Country: us
Re: C word
« Reply #25 on: September 13, 2019, 04:15:26 pm »
Excellent, thanks mate. I did OK with learning assembly so I'll do my best to learn it well.

In my view, assembly language is orders of magnitude easier to learn than C.  Sure, you have to memorize a LOT of mnemonics but, structurally, coding practices in assembly language are much simpler.  The level of detail is higher but nobody expects to write a few hundred lines of assembly code per day.

C allows you to express higher level ideas in a concise manner.  Things like structs and unions, pointers to an array of function pointers returning <whatever>.   If someone tells you they really understand pointers, it's ok to giggle.  The idea is simple, even trivial, but the application will be a challenge.  What I don't like is two levels (or more) of indirection.  A pointer to a pointer to <something>.

I hate this code but it comes up from time to time...

https://stackoverflow.com/questions/42715876/levels-of-indirection-in-c

If you REALLY understand this code, you might actually understand pointers.  I shouldn't giggle...

 

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 581
  • Country: gb
Re: C word
« Reply #26 on: September 13, 2019, 04:51:44 pm »
Cheers.
I have a "course" that is focussed on uC programming, which is my primary intent.

Going back to an earlier point, I just looked-up EEPROM writing/reading in the datasheet for the PIC16F1827 (this is the uC used in my course and the one I learned assembly on) and it gives code for writing/reading but only in assembly.

How on Earth do I know how to do this in C?  :-//

[True, my course may cover this later but as a general principle.]
You can release yourself but the only way to go is down!
RJD
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1724
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: C word
« Reply #27 on: September 13, 2019, 05:20:29 pm »
Cheers.
I have a "course" that is focussed on uC programming, which is my primary intent.

Going back to an earlier point, I just looked-up EEPROM writing/reading in the datasheet for the PIC16F1827 (this is the uC used in my course and the one I learned assembly on) and it gives code for writing/reading but only in assembly.

How on Earth do I know how to do this in C?  :-//

[True, my course may cover this later but as a general principle.]
You should start looking for C examples for the specific uC or family and start from simple ones like blink, then advance to examples of each peripheral. 
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: C word
« Reply #28 on: September 13, 2019, 07:37:22 pm »
Quote
do [the usual references] cover any peculiarities (are there any) when using C in programming microcontrollers?
No, probably not.  There aren't that many:
  • data sizes become important, and are likely to be different than the normal "desktop" systems.  You should learn about the "integer promotion rules", and why an AVR C statement like "int32_t onehour = 60*60*ticksPerSecond;"
  • You should really use the sized integer types (uint8_t, int32_t) ALL THE TIME in embedded.
  • "volatile" is unlikely to be emphasized.
  • bitwise operations are unlikely to be emphasized.  You probably won't see important and common statements like "x |= 1<<12;" or "y &= ~(1<<USART_TX_ENABLE);"  (I'm pretty sure you can get through a modern C++ class thinking that "<<" is strictly an IO operator!)
  • global variables are more important in embedded  than in desktop programming.  dynamically allocated storage is more important on the desktop.
  • Things that might result in slow or large code on a microcontroller probably won't be discussed.  A standard C text will likely treat floating point numbers as just another data type, failing to mention that it might involve a library that is larger than your device's memory, and will almost certainly not suggest that integer multiply and divide should be avoided, or that math on small data types MIGHT be faster than on bigger data types.
Quote
I just looked-up EEPROM writing/reading in the datasheet for the PIC16F1827 and it gives code for writing/reading but only in assembly.

How on Earth do I know how to do this in C?
A key thing to understand is that the vendor will usually provide a set of definitions that permits the internal registers to be manipulated just like C variables.  (if they don't, the first thing to do is to figure out how to do this yourself, which will also not be covered in typical "intro to C" books.)
For example, the code form the PIC data sheet:
Code: [Select]
   BANKSEL EEADRL       ;
    MOVLW   DATA_EE_ADDR ;
    MOVWF   EEADRL       ;Data Memory Address to read
    BCF     EECON1, CFGS ;Deselect Config space
    BCF     EECON1, EEPGD;Point to DATA memory
    BSF     EECON1, RD   ;EE Read
    MOVF    EEDATL, W    ;W = EEDATL
Would be something like:
Code: [Select]
    EEADRL = data_ee_addr; // set address to read
    EECON1 &= ~(CFGS|EEPPGD);  // deselect config, point to data.
    EECON1 |= RD;
    result = EEDATL;
(SOMETHING LIKE!  C abhors short names like "RD" that are likely to be ambiguous and collide with "something", so it'll probably actually be something like:
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: C word
« Reply #29 on: September 13, 2019, 07:39:32 pm »
oops.Something like:
Code: [Select]
    EECON1 |= _EECON1_RD_MASK;Or perhaps:
Code: [Select]
    EECON1bits.RD = 1;
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4644
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: C word
« Reply #30 on: September 13, 2019, 08:05:24 pm »
Treat C like scripted assembly. You'll be fine. It doesn't do much magic like the newest C++, Java or other languages may perform.

First determine where to get information. Since the company who made the chip might not be the company making the cpu, and the compiler might be some other entity as well. For example: ST makes a chip with an ARM Cortex processor core that you can compile code for using a compiler the GNU Project in an Eclipse IDE.

Then there are the architecture intricacies...
Those that involve the storage specifiers, volatile and static.
Also platform details, what memory you may access when and how wide.
Knowing what arithmetic you chip can do, or does slow. How to accelerate it with the FPU or crypto engine. (this often isn't automatic)
How much stack there is left, you often can't ask the OS.
How to real-time debug, since at some point you can't breakpoint the chip anymore without hardware damages.
The performance impact of the volatile keyword, or pointers. At only 20 Mhz these things count.
What functions your standard library offers, or not, or not optimized for the architecture.
Many little things....

Take a copy of the standard so you can lookup yourself if the people on the internet are correctly describing the intended behavior of the language.
Since I've seen a lot of different ways to describe what some keywords do.
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

You can make it your job!
« Last Edit: September 13, 2019, 08:08:00 pm by Jeroen3 »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: C word
« Reply #31 on: September 14, 2019, 08:12:33 am »
Quote
If you want to go professional — and especially if safety or security will depend on your code — be fully aware that you should dig into that hard to understand mess of specifications spread across 650 pages.
meh.  Nonsense.  A good programmer won't normally get very close to the edges, and even if they do, there code will still be more portable than if it were written in assembly language.  The most common pitfalls are well-known and well-described outside of the standard, and some of them are ignored because they seem to be "compiler gurus getting in the way of what needs to work."  If your work gets to be safety- or security- critical, you'll probably be at a large enough company where there is someone assigned to pay attention to such things, and the way you'll need to code will be a company standard.
That doesn't mean that it's not a good idea to be familiar with the standard, but it's not NECESASRY as an early step to learning C...

Quote
it took ages to realise that the "!" meant "not ".
Quote
Just a suggestion here - maybe actually learn C?
That said.  The original message suggests a level of unfamiliarity with high level languages in general that is worrying, and a "real class" or teaching-oriented text would probably be a good idea, rather than just looking for a "summary sheet."   (I always find it ... interesting..., the way that people with a pure assembly language and/or EE background have trouble getting started with high level languages.  And vis versa, too!)
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: C word
« Reply #32 on: September 14, 2019, 04:14:30 pm »
Quote
If you want to go professional — and especially if safety or security will depend on your code — be fully aware that you should dig into that hard to understand mess of specifications spread across 650 pages.
meh.  Nonsense.  A good programmer won't normally get very close to the edges, and even if they do, there code will still be more portable than if it were written in assembly language.  The most common pitfalls are well-known and well-described outside of the standard, and some of them are ignored because they seem to be "compiler gurus getting in the way of what needs to work."  If your work gets to be safety- or security- critical, you'll probably be at a large enough company where there is someone assigned to pay attention to such things, and the way you'll need to code will be a company standard.
That doesn't mean that it's not a good idea to be familiar with the standard, but it's not NECESASRY as an early step to learning C...

I still personally think this is a good idea to go through the C standard at least once, but I agree this is not necessary. It's just a plus to get an in-depth view of C. And as I said above, it's not for when you're just learning. It's an interesting step only when you're already proficient.

And I also agree that reading the standard(s) is absolutely NOT required to write good C (it would be a lame excuse to be claiming that).

Just a point to answer the other person's point: the C standard documents are definitely not hard to understand nor a mess of specifications. They are rather clearly layed out.
Of course as we said above, they are not meant for beginners in the least. They are meant for advanced users and compiler designers.

Quote
it took ages to realise that the "!" meant "not ".

It should have taken about 2 minutes opening a C book (even a lousy one!) and looking up the "operators" section. Dang...

That said.  The original message suggests a level of unfamiliarity with high level languages in general that is worrying, and a "real class" or teaching-oriented text would probably be a good idea, rather than just looking for a "summary sheet." 

Yes, that was exactly my point. But it's not just worrying as a programming language learning thing - it's worrying as a general learning process. It's like some people don't want to learn. They are just looking for recipes.
« Last Edit: September 14, 2019, 04:16:57 pm by SiliconWizard »
 

Offline FreddieChopin

  • Regular Contributor
  • *
  • !
  • Posts: 102
  • Country: ua
Re: C word
« Reply #33 on: September 14, 2019, 06:39:45 pm »
If you're working with PIC microcontrollers don't be fooled into buying CCS C compiler. It's a complete and utter turd that doesn't conform to any standards, breaks in most weird ways and generally is a paint in the ass to use.
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3958
  • Country: de
Re: C word
« Reply #34 on: September 14, 2019, 07:40:12 pm »
If you're working with PIC microcontrollers don't be fooled into buying CCS C compiler. It's a complete and utter turd that doesn't conform to any standards, breaks in most weird ways and generally is a paint in the ass to use.

Isn't that the one which the old PIC C compiler (pre xc8) was based on? If yes, then I concur wholeheartedly - that was an utter turd ...
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: C word
« Reply #35 on: September 15, 2019, 12:59:55 am »
Quote
If you're working with PIC microcontrollers don't be fooled into buying
CCS C compiler.
Isn't that the one which the old PIC C compiler (pre xc8) was based on?

I don't think so.  CCS is one of the few remaining independent compiler vendors.

Arguably, a certain amount of "non-conformance" is a necessary thing for an 8-bit PIC compiler, if you want code that is even close to acceptable (performance and size-wise.)
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2572
  • Country: pl
Re: C word
« Reply #36 on: September 15, 2019, 12:52:33 pm »
Since no one mentioned that yet: C-FAQ contains a nice collection of common pitfalls.

meh.  Nonsense.  A good programmer won't normally get very close to the edges,
More often than you think. Not only I have shown a very trivial case of an expression that does a different thing that people may expect it does, but bugs caused by violating the strict aliasing rule are widespread.

and even if they do, there code will still be more portable than if it were written in assembly language.
That is right, but… non sequitur. A portable bug is still a bug. :D

The most common pitfalls are well-known and well-described outside of the standard, and some of them are ignored because they seem to be "compiler gurus getting in the way of what needs to work."
They are not(1). This is exactly a type of accusation heard from people, who never actually learned C, but are only guessing what a given expression does. And then they are surprised, when compiler properly interprets their code and emits binary with behaviour different than expected.

If your work gets to be safety- or security- critical, you'll probably be at a large enough company where there is someone assigned to pay attention to such things
As we can see with seemingly infinite train of IoT vulnerabilities… ;)

Note that I do not suggest reading the standard from cover to cover! That’s a waste of time and one will learn little from that. I am speaking about having it nearby, to determine whether some opinion is right, or if your interpretation of your code is right.
____
(1) Unless you can show they do not follow what the language requires them to do. This, of course, happens all the time. But those are normal bugs, that are off-topic.
« Last Edit: September 15, 2019, 12:57:03 pm by golden_labels »
Why 📎 | We live in times when half of people have IQ below 100.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: C word
« Reply #37 on: September 15, 2019, 02:22:24 pm »
If your work gets to be safety- or security- critical, you'll probably be at a large enough company where there is someone assigned to pay attention to such things
As we can see with seemingly infinite train of IoT vulnerabilities… ;)

Oh I agree with you on the alarming number of bugs and vulnerabilities in IoT devices, but I don't quite agree with your "point" being valid within the context mentioned by westfw.

The overwhelming majority of companies designing IoT devices are nothing similar to the kind of companies westfw was referring to when saying "safety- or security- critical". So you're kind of twisting what he was on about. Most of them are just making gadgets with the mindset that comes with that.

Of course that doesn't mean that the current state of affairs with IoT stuff is right. But seriously, unless an IoT company becomes one that correctly deals with safety and security, their products should defnitely NOT be considered neither safe nor secure by their customers. So in that respect, the customers being fools by not realizing this would be almost as liable than the software developers designing the products.

Other than that, of course I agree with you on the merits of reading standards, as I was mentioning that earlier, but again that's not for beginners. So the basics need to be well known first, otherwise, the standard will just look like gibberish, like some have suggested here, which it's definitely not.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2572
  • Country: pl
Re: C word
« Reply #38 on: September 15, 2019, 04:35:16 pm »
I am continuing with the same security/safety I was talking about initially, so there is no deviation from that. Perhaps there was some communication problem and westfw has misinterpreted what I meant. Certainly I didn’t limited that topic to some specific subset of companies or products.

And yes, I do not recommend that for just learning the language. Formal specifications are never the best place to start learning things. In particular a language specifiation will not teach anyone programming: it could at most teach them the language syntax and semantics. Writing programs is a completely different beast. And no book, no teacher, not even gazing at expert’s work 24/7 will let anyone acquire experience.
Why 📎 | We live in times when half of people have IQ below 100.
 

Online PlainName

  • Super Contributor
  • ***
  • Posts: 8989
  • Country: 00
Re: C word
« Reply #39 on: September 17, 2019, 09:40:07 am »
Quote from: SiliconWizard
I still personally think this is a good idea to go through the C standard at least once

I think this is excellent advice. One of the problems in learning new stuff is that there is so much and little of it makes any sense to start with. It is easy to zero in on a subset that will do the biz most of the time and treat everything else as somewhat esoteric.

If you go through the entire thing you'll remember not the detail but the functionality, and it's that functionality that will help you to program. You might not recall the detail of how variadic functions work, for instance, or even what they are called, but you will remember what they do. Later, when you need that kind of functionality you'll hopefully remember that it can be done, and then it's just a matter of looking up the details.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: C word
« Reply #40 on: September 17, 2019, 01:55:13 pm »
Quote from: SiliconWizard
I still personally think this is a good idea to go through the C standard at least once
If you go through the entire thing you'll remember not the detail but the functionality, and it's that functionality that will help you to program. You might not recall the detail of how variadic functions work, for instance, or even what they are called, but you will remember what they do. Later, when you need that kind of functionality you'll hopefully remember that it can be done, and then it's just a matter of looking up the details.

That's actually true for any learning process IMO, and that's why, getting back to programming, I think going through a good book about the programming language you want to learn, as a first step, IS a good idea, so you get the general idea, the basic constructs and keywords. Sure you won't remember EVERYTHING by heart, but you will have a global view of the language and will know *where to look* when you don't know something.

This is what learning is all about.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: C word
« Reply #41 on: September 17, 2019, 03:39:48 pm »
Learning Erlang has been *very* difficult. For me  :o
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2572
  • Country: pl
Re: C word
« Reply #42 on: September 17, 2019, 04:13:29 pm »
The C standard, just like any similar document, is not linear. You can’t simply read it page by page. It would make as much sense, as reading a datasheet of a microcontroller. Skimming over introduction paragraphs to each of the chapters to know what you are sitting on — useful. Learning chracteristics of ADC inputs while your project is 100% digital — a waste of time. But it’s worse, as even if you would memorize the C standard in detail you are likely to miss most of the important information. That knowledge is hidden behind non-obvious interactions between many different sections.

This is why I said it is useful to have one nearby, not to make it into your bedtime/toilet read. It is just too big and complex to be useful if used that way. What is much more important, is being able to know where to search when you are in doubt.

An, of course, there is even more important thing. Knowing your toolset. If you want to read any specification cover to cover, let it be the documentation of your tool.

The C standard defines how the language should work. It doesn’t mean it will always work exactly like that or that your tool have no facilities beyond the basics. For example in the AVR Libc float and double are the same type, which is against the standard, but this is how it is. On 8-bit microcontrollers C can’t even be implemented, because the conflicting specification of int, which should be exactly 8-bit and at least 16-bit at the same time. int is also wrong on x86-64, because it is 32-bit while it should be 64-bit. The standard is also giving some freedom to implementations, so char (without further specifiers) may be either signed or unsigned — and that has much more serious implications than just differences in range, as valid code written for the unsigned version may cause UB with the signed version. wchar_t is a beast that is so ill-defined, that it is nearly useless in portable programs.
Why 📎 | We live in times when half of people have IQ below 100.
 

Online PlainName

  • Super Contributor
  • ***
  • Posts: 8989
  • Country: 00
Re: C word
« Reply #43 on: September 17, 2019, 04:49:20 pm »
Quote
Learning chracteristics of ADC inputs while your project is 100% digital — a waste of time.

That's not what I meant. As a for instance, you would take away that there is only one ADC but eight analog inputs, and you can specify your own vref (somehow - you don't even need to remember how). And you ain't doing this bit of learning just for the immediate project. The one three down the line might be branching out from your 100% digital safety.

Quote
That knowledge is hidden behind non-obvious interactions between many different sections

Much is, yes. But without the background it's very hard to put things together. It's the difference between a man with a hammer seeing every fastener as a nail, and a workman recalling that you can join things with a twisty wosname, then looking up screws et al. on the web.

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: C word
« Reply #44 on: September 17, 2019, 08:39:48 pm »
Learning Erlang has been *very* difficult. For me  :o

This is not surprising. Functional languages ARE hard, and Erlang can be a bit cryptic on top of that.
The fact that you will find little available source code doesn't help. It's kind of a niche language, and I haven't seen many open-source projects in Erlang...
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: C word
« Reply #45 on: September 17, 2019, 09:27:09 pm »
This is not surprising. Functional languages ARE hard, and Erlang can be a bit cryptic on top of that.
The fact that you will find little available source code doesn't help. It's kind of a niche language, and I haven't seen many open-source projects in Erlang...

Airports do prefer Mnesia as database because transactions are really atomic so they are sure there is no probability of collision when they sell a ticket. There are also other reasons, but this is the most important.

So I was said  :-//

Mnesia is written in Erlang, as well as a lot of things made by Ericson and used in Denmark for weird purposes.

I am using this
Code: [Select]
*  dev-lang/erlang-21.1.1
      Homepage:      [url]https://www.erlang.org[/url]
      Description:   Erlang programming language, runtime environment and libraries (OTP)
      License:       Apache-2.0

and it's possible to write callbacks in C, which opens doors to hybrid applications  :D
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: C word
« Reply #46 on: September 17, 2019, 09:32:12 pm »
I don't really like functional languages, but Erlang is an interesting beast nonetheless.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: C word
« Reply #47 on: September 17, 2019, 09:48:42 pm »
On 8-bit microcontrollers C can’t even be implemented, because the conflicting specification of int, which should be exactly 8-bit and at least 16-bit at the same time.

Yup. On Gcc-hc11 (EOL >3.4.*) there is a user flag to set the size of "int" and the size of "short".
You can also set the size of all the "soft-registers" (registers implemented in ram).

That was the best choice ever.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2572
  • Country: pl
Re: C word
« Reply #48 on: September 18, 2019, 04:25:56 am »
I don’t know about gcc-hc11, but avr-gcc has the -mint8 flag:
Quote
Assume "int" to be 8-bit integer.  This affects the sizes of all types: a "char" is 1 byte, an "int" is 1 byte, a "long" is 2 bytes, and "long long" is 4 bytes.
That way one may choose, which of the standard guarantees are broken. You either get a 16-bit int, which breaks the requirement for int to be 8-bit on AVR8, or you get an 8-bit int, which breaks the requirement for the int to be at least 16 bits wide. The flag has, however, a drawback: it also breaks other primitive types:

    Type     │   Normal   │   -mint8   
═════════════╪════════════╪════════════
    char     │      8     │      8
    short    │     16     │      8 !
    int      │     16 !   │      8 !
    long     │     32     │     16 !
  long long  │     64     │     32 !


(‘!’ marks invalid behaviour)
Why 📎 | We live in times when half of people have IQ below 100.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: C word
« Reply #49 on: September 18, 2019, 08:03:46 am »
you can use { uint8_t , uint16_t , uint32_t } instead!
 
The following users thanked this post: AndyC_772, agehall


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->