News:

Let's find out together what makes a PIC Tick!

Main Menu

Falling in love with C

Started by shantanu@india, Dec 22, 2022, 07:38 AM

Previous topic - Next topic

shantanu@india

Never though I will with the Les's amazing basic compiler as my constant companion for the past 20 odd years!!!

But going through a professionally written STM32 code in Atollic Studio made me realize the sheer power of C structures...

The following snippet initializes a LoRa 1261 chip with the help of the hardware abstraction layer(HAL)  structure GPIO_InitTypeDef.
Structured programming in the professional Eclipse environment with ARM SWD(serial wire debugger) tool can be a big advantage for those accustomed in C coding.Writing portable code is easier & the complexities of the hardware are hidden from your view. But if you really want to look at the backstage you are free to do so by by opening a assembler watch window where the internal registers of the ARM can be examined.

The downside is C doesn't have the simplicity of a linearly written basic program.

void SX126xIoInit( void )
{
  GPIO_InitTypeDef initStruct={0};

  //Set DioIrqHandler port in IT_RISING mode
  initStruct.Mode = GPIO_MODE_IT_RISING;
  initStruct.Pull = GPIO_NOPULL;
  initStruct.Speed = GPIO_SPEED_HIGH;
  HW_GPIO_Init( RADIO_DIO_1_PORT, RADIO_DIO_1_PIN, &initStruct);

  initStruct.Mode = GPIO_MODE_INPUT;
  initStruct.Pull = GPIO_NOPULL;
  HW_GPIO_Init( RADIO_BUSY_PORT, RADIO_BUSY_PIN, &initStruct);

#if defined (USE_ANTENA_SWITCH_POWER)
  HW_GPIO_Init( RADIO_ANT_SWITCH_POWER_PORT, RADIO_ANT_SWITCH_POWER_PIN, &initStruct);
  HW_GPIO_Write( RADIO_ANT_SWITCH_POWER_PORT, RADIO_ANT_SWITCH_POWER_PIN, 0);
#endif
}
Regards
Shantanu

trastikata

shantanu,

I have a different personal view. The very same structure can be easily done in Positron with includes and procedures. However if you actually see the C code behind that abstraction layer, Positron wins by far IMO.

My personal opinion is that Positron can easily be compared to C structures. The big advantage of C is that more people are using it and you can find libraries for almost anything nowadays. But I never use somebody else's libraries, at most I use them as reference to develop my own -  so I have full control and understanding what is happening.

keytapper

I have to agree that the higher the level and the more complexity to realize a task will cost. Thus what looks simpler to code it may get bloated to accomplish.
I did sometimes a translation from C (for Arduino) language. Then the struct is something to be converted in a different fashion, as result to get a better performance.

There's a little shortcut to make an array looking like a structure, unfortunately the indexing should be made manually.

So I think that he C assert initStruct.Mode maybe transformed in:
' user variables are herein defined in angular brackets
$define Mode <pointer2array>
Dim initStruct[<size_used>] as Byte
initStruct[Mode] = GPIO_MODE_INPUT
Ignorance comes with a cost

Stephen Moss

But think of all the hours wasted typing those stupid semi-colons that the end of each line, If you omit one some sometimes (possibly compiler dependant) the reporteed errors line is miles from whre it acually is and it can be so difficult to find amid 100s of line of code.

shantanu@india

My intention was never to draw a comparison between Proton and a C compiler... that idea is farthest from my mind.
Both have their relative strengths and weaknesses.
For stand-alone projects where a microcontroller does some specific work , Proton has absolutely no competition. Simple, quick and effective.It will stay with me lifelong!!!
But for a collaborative code development for a large project with a sizable number of peripherals where each team member works on a portion of the code, a function-driven structured language like C with self-explanatory arguments and return values is the better choice.
Regards
Shantanu

Yasin

I'm having trouble understanding. Why do people compare Microchip's 8-bit product with the STM32? Why should compilers be criticized at the end of this comparison? From what I've seen on the forum people still hope mcu's like the 12F508 20 years ago will work wonders, and they ask for it from the compiler. It's unfair to reference the STM32 C code and criticize the Positron. STM32 and PIC32 should be compared. For example, it makes sense to compare MikroC and Positron. Of course, when making this comparison, price should be an important criterion at the performance point. I think being critical is very valuable, but fairness is more important. I used google translator while writing this article. I hope I'm telling the truth without offending anyone. I love every forum members who does not spare his knowledge, no matter how little or much.
Best regards.

HAL

Hello Yasin
You make very good points.  You are not offending anyone. The google translator is working well...
Best regards  Hal

Fanie

I'm so glad this thread started...
IMO the problem is not the semicolons at the end of each instruction/command line, but rather a lack of a proper IDE.
I'm attempting to write code for an app for the ESP32-S3, and then there is apparently 3 platforms ? sitting on each other AND you have to connect a standard development board via USB and none of the 3 platforms in it's resulting combined IDE seems tp give any proper indication of anything working (or not).

The C or C++ code is not the problem... its the lack of a proper IDE.

I guess we got spoiled with the ease if using proton, what Les did was awesome.
In my case I'm replacing the 18F45k20 with the ESP32-S3 simply because the pic's aren't available for a year, and I refuse to be held hostage by the Wests political attrocities.

If anyone can refer me to a proper IDE for the ESP32-S3 I will be gratefull forever.

Oh !  And you cannot find a simple programmer for the ESP's anywhere either...

ken_k

Hi Fanie

I will ask a friend that uses large numbers of ESP32 devices and find out what he uses.

I did a find chips search for 18F45k20 devices.

https://www.findchips.com/search/18F45k20

There are not a lot around, I see your problem.

BTW I seem to leave out the semi-colons occasionally!

shantanu@india

#9
Quote from: Fanie on Dec 23, 2022, 08:49 AMI'm so glad this thread started...
IMO the problem is not the semicolons at the end of each instruction/command line, but rather a lack of a proper IDE.
I'm attempting to write code for an app for the ESP32-S3, and then there is apparently 3 platforms ? sitting on each other AND you have to connect a standard development board via USB and none of the 3 platforms in it's resulting combined IDE seems tp give any proper indication of anything working (or not).

The C or C++ code is not the problem... its the lack of a proper IDE.

I guess we got spoiled with the ease if using proton, what Les did was awesome.
In my case I'm replacing the 18F45k20 with the ESP32-S3 simply because the pic's aren't available for a year, and I refuse to be held hostage by the Wests political attrocities.

If anyone can refer me to a proper IDE for the ESP32-S3 I will be gratefull forever.

Oh !  And you cannot find a simple programmer for the ESP's anywhere either...
Fanie... use Micropython. It's dead easy and no hassles of semicolons and braces.
I've never used Arduino C with ESP.
I'm sure you'll love it... you don't need any programmer... just a USB cable if you are using the boards with USB sockets and built-in 5.0 to 3.3 converters
Regards
Shantanu

Gary Scott

You did not define proper IDE but Visual Code should support the ESP with a plugin. Personally I hate Visual Code

Gary Scott

I dream of a Positron and Raspberry Pi Pico 2040 merge. Brit hardware and software together is sure to be a hit!

Fanie

All Les's fault for writing such an easy IDE for the pics  ;D

Ken - IF there are pics available they are ridiculously priced.

Shantanu - will defenately look into it ! They say guys must have everything the biggest... except their phones, so to use MICROpython might take convincing  ;)

Gary, I share your feeling on the visual code thing.

I prefer a "programmer" with the USB sockets and built-in 5.0 to 3.3 converters which plugs into the app instead of having to put it all on the app board.

I have hundreds of boards for pics - will look at a way to put another micro on the boards...

GaryC

  As a person who spells poorly and has messed up hands, Positrons error catching is the best, as far as Microchip, are they sending everything to China and giving us almost no chips.
Digikey is a wasteland and Microchip direct is almost as bad, prices are going up, Should have bought 12F1501 DIPs when I had a chance.
And a Merry Christmas to Les and all the great Positron users.

shantanu@india

Regards
Shantanu

Dompie

There is an interpreted Basic Annex and Annex32 for the ESP8266 and ESP32.
The site link: Annex site
The latest Annex32 Help: Annex32 Help

It is no Positron but it has his charme.

Happy Christmas
Johan

shantanu@india

This is indeed interesting Johan!!
Regards
Shantanu

Mapo

great website, thanks Johan


top204

#18
The ANSI C language, when written clearly, is a nice language. However, for some reason, some people create C code that is so stupid to follow, it makes the language look complex, and the code bloated and slow. Especially when pointers of pointers of pointers are used, or pointers of structures that contain pointer are used etc...

These is no need for this type of code writing, but it seems to be, the more complex the C code that a user can create, the smarter the writer may look. But to me, it is the exact opposite. :-)

For example, take a look at the loop below and see what you think it represents in "correctly" written code. It is from a simple program I was converting, but it was made to look more complex that it actually is for some reason:

for(j = 0; (j < i) && (((grain[i].x / 256) != (grain[j].x / 256))
                    || ((grain[i].y / 256) != (grain[j].y / 256))); j++);

Interpreted BASIC is a simple concept, and what it normally is, is an interpreter sitting over a C compiler core, or a translator from BASIC to C. Both, to me, are a cheat and rely fully on the C compiler they are using, even if the C is dreadful at creating code itself. So the BASIC language will be dreadfully bloated and a lot, lot slower than it should be.


RGV250

Come on Les, put us out of our misery and show us how simple it would be in Positron.

Bob