Can we find out what is the size of micro with c program?

Go To Last Post
26 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

 

I have question,  Can we find out what is the size of microcontroller with this program? for example if we are working on 8 bit micro so Is it possible find out the size of microcontroller  by writing c program ?

 

I only heard about sizeof operator It indicate space of variable to store value but It does not tell size of micro?

 

Thanks 

SKY 
 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 3

Since the program that is running on the micro has to be compiled for a specific micro and then downloaded into the micro, I don't see how this would be helpful?

You had to know the specific micro, and hence its size, in order to compile the program for the micro to begin with.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I agree with DocJC.

 

But anyway, basically there is no way. A classic example of this that is often quoted on here is actually the Zilog Z80 CPU from the 1970's (used in a LOT of home computers during the 1980's). Every external sign would tell you that this was an 8 bit micro (for example the width of the accumulator is 8 bits and so on) but apparently, deep within the silicon it actually has a 4 bit ALU (Arithmetic/Logic Unit) which would seem to suggest a 4 bit core.

 

Right up at the C level you can't really know what code will be emitted by any particular code sequence (which is the common argument for why some parts still have to be coded in Asm if exact size/timing is required).

 

There may be clues. For example if (as on AVR) sizeof(int)==2 it could imply that the underlying architecture is 8 bit rather than something larger.

 

But at the end of the day if you are asking a question of such of this you have kind of missed the entire point of why C is used. The whole idea is to isolate you from needing to know implementation details. Well written C should have the same effect whatever it is compiled to run on.

 

Actually another case in point is AVR Mega chips versus AVR Tiny chips. If you write:

uint16_t a, b, c;

a = 123;
b = 45;
c = a * b;

On any CPU where this code is built this should end up with c = 5535 but in a "Mega" it is achieved with just a few MUL opcodes whereas on a Tiny (they don't have MUL) it's far, far more complex, larger and slower because it will have to do all kinds of shifts and adds because it cannot multiply (directly) but at the end of the day c still ends up holding 5535 however it might have been achieved in the implementation. On a 32 bit ARM the whole thing is probably one opcode !

 

The point is that C hides/isolates you from needing to know anything about how c got 5535. It's simply the case that 123 * 45 is 5535.

 

It also isolates you from knowing whether the CPU is 8, 16, 32, 64 or 128 bit too !

 

(if you write it in Asm (especially on an AVR Tiny) you would be VERY aware of the architecture!!)

 

Last Edited: Tue. Mar 3, 2020 - 05:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
But at the end of the day if you are asking a question of such of this you have kind of missed the entire point of why C is used. The whole idea is to isolate you from needing to know implementation details. Well written C should have the same effect whatever it is compiled to run on.


 

This question was asked, write c program to find bus architecture  of 32 bit microcontroller ? I do not understand how to answer this.Is this a tricky question 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I say, you basically can't. But as Doc said to even run such a program to do the test you'd have to compile it for the right CPU first and to do that you must already know the CPU and hence the resources anyway.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
This question was asked, write c program to find bus architecture  of 32 bit microcontroller ? I do not understand how to answer this.Is this a tricky question 

 

As mentioned, you can get some hints. Using sizeof(int) was already mentioned, another important hint is sizeof(void*).

You can also try to measure how many cycles it takes to load/store data of different well defined sizes, like uint8_t, uint16_t and uint32_t, and how long it takes to do a simple arithmetic operation on these sizes.

 

For example, an ARM will probably take about the same time for uint8_t, uint16_t and uint32_t operations because it's a 32 bit CPU, while an AVR will take t time for uint8_t, 2t for uint16_t and 4t for uint32_t.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:
As mentioned, you can get some hints. Using sizeof(int) was already mentioned, another important hint is sizeof(void*).

 

Yabbut:

DocJC wrote:
Since the program that is running on the micro has to be compiled for a specific micro and then downloaded into the micro,

Exactly. 

 

You cannot simply write a c program and generically compile it then shove it into any micro/ARM can you?

 

Jim

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
You cannot simply write a c program and generically compile it then shove it into any micro/ARM can you?

 

I think this is just a theoretical question from the OP.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To find the resource sizes of any micro, all you have to do is read its data sheet!

just google its part number and add the word data sheet, then download it.

 

jim

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

If you want something you've never had...

...you must be willing to do something you've never done!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:
As mentioned, you can get some hints. Using sizeof(int) was already mentioned, another important hint is sizeof(void*).

You might also get some additional hints by trying different sized memory accesses with various alignments and see in which cases the processor throws an exception (if any).

 

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I think this is a trick question to make the students think a little.

 

When IBM made the 360 series computers, the actual implementation varied widely. The concept was to have the one architecture so that little or no software changes were needed when moving between different models.

 

On a Linux system, you can look at the /proc data to find out various system parameters.

 

The idea of writing a simple C program to determine the system that is running it is not valid. Even determining the sizeof(int) is fraught with implementation details. A different compiler on the same system may give different results.

 

I think the teacher is fishing for the brighter students to explain why it is not a simple solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
I think the teacher is fishing for the brighter students to explain why it is not a simple solution.

 

Or we are being trolled....trolling?  I can't remember.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

Kartman wrote:
I think the teacher is fishing for the brighter students to explain why it is not a simple solution.

 

Or we are being trolled....trolling?  I can't remember.

 

JIm

My guess is that we are doing his homework. Earlier questions already pointed that way, but with this as an 'assignment'  it has to be school stuff.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:

...for example if we are working on 8 bit micro so Is it possible find out the size of microcontroller  by writing c program ?

 

 

Portable ANSI C - No.

Targeted C - Yes. But then you already know the answer in advance as pointed out back in #2.

 

Basically your question makes no sense.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

Last Edited: Wed. Mar 4, 2020 - 08:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:

Since the program that is running on the micro has to be compiled for a specific micro and then downloaded into the micro, I don't see how this would be helpful?

You had to know the specific micro, and hence its size, in order to compile the program for the micro to begin with.

 

I don't see how this remark makes any sense. It is the compiler that has to know the size, not to the user. That's actually why we have compilers.

 

For the user (i.e. for the writer of the source code) it is extremely useful to be able to have a useful layer of hardware isolation, i.e. to be able to determine some universal parameters of the platform in a cross-platform or in a portable way. This enables writing platform-agnostic source code. And writing platform-agnostic code is an absolutely necessary skill in C/C++ programming for any platform. The OPs question is about that exactly.

 

Yet, it is true that there's no way to reliably answer that question by using standard features of the language. 

 

Dessine-moi un mouton

Last Edited: Wed. Mar 4, 2020 - 09:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreyT wrote:

It is the compiler that has to know the size, not to the user. That's actually why we have compilers.

 

But that's the point though isn't it? If we want to test an 'unknown' microcontroller we have to use a compiler that it totally specific to that make of microcontroller. And once we know which commands to type to invoke the right compiler then we automatically know what microcontroller we are trying to test.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A program to figure out the size of RAM should not be too difficult,

especially on processors that initialize SP to RAMEND.

Do all processors have SP at the same locations?

Flash would be trickier at best.

One might have to settle for reading the signature.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:

One might have to settle for reading the signature.

 

With a C program that runs on the any micro?

 

What's not clear here is what the OP's question is.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 Is it possible find out the size of microcontroller  by writing c program

Even if it is possible, so what?  Determining total/free/used memory size is certainly more critical & seems more useful.

What will you do after you find out the size of microcontroller?  Sound an alarm?

 

Edit:

Oh, I see this is a school "thinking cap" question--ok
but you say yourself it it 32 bit:   question was asked, write c program to find bus architecture  of 32 bit microcontroller

Since there are obvious inconsistencies here, I've sounded the troll alert:

    PORTB |= (1<<TROLL_ALARM) | (1<<TRAP_DOOR_CHUTE)

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Mar 4, 2020 - 09:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

 

switch (part) {

  case ATMEGA328P-PU:

    printf("size is about 34x8mm\n");

    break;

  case ATMEGA328P-AU:

    printf("size is about 7x7mm\n");

    break;

  ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Since there are obvious inconsistencies here, I've sounded the troll alert:

    PORTB |= (1<<TROLL_ALARM) | (1<<TRAP_DOOR_CHUTE) | (1<<HOME_WORK_ALARM)

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

AndreyT wrote:

 

It is the compiler that has to know the size, not to the user. That's actually why we have compilers.

 

But that's the point though isn't it? If we want to test an 'unknown' microcontroller we have to use a compiler that it totally specific to that make of microcontroller. And once we know which commands to type to invoke the right compiler then we automatically know what microcontroller we are trying to test.

 

I assume, again, that the value is in being able to write platform-agnostic source code. 

 

When I write my C programs, I might know that the number of bits in 'char' on my platform is, say, 8. Nevertheless I will never continue to insist on using `CHAR_BIT`constant in my code even though I know that it is 8. I will insist on using `uintptr_t` type for integer representation of pointers, even though I know that my pointers are, say, 16 bits wide. When working with a specific AVR microcontroller, I use manifest constants like `A0` to access analog pins, even though I know what specific microcontroller I'm working with and what physical pin `A0` actually maps to.

 

The same applies to the "size" of the micro, as stated by the OP.

Dessine-moi un mouton

Last Edited: Fri. Mar 6, 2020 - 03:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can we find out what is the size of microcontroller

 write c program to find bus architecture  of 32 bit microcontroller ?

Were those the same question?   Because they're different enough that I have lost confidence that we know exactly what question we're supposed to be looking at...

 

 

Hmm.  So what can we learn about a CPU/system from the C compiler?

sizeof (int)

sizeof (void*)

assorted compiler/linker symbols.

addresses of global, stack, and dynamically allocated storage.

Amount of dynamically allocated storage that is available.

Whether stack and DAS goes up or down.

Limits for stack growth. (maybe)
 

Anything else?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Methinks this is being taken too seriously.

In pretty much any case of embedded code,

the code writer should know the target device

and provide the information to the toolchain.

 

If one is writing code to be run on various size servers of the same ilk,

getting platform information at runtime might be useful.

The platform would no doubt provide a mechanism to provide such information.

If all else fails, there is always user input.

 

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think we're talking to ourselves here. The OP seems to have lost interest and found another subject to have doubts about.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
This question was asked

Where, exactly, was it asked ?

 

The context may be important here!

 

As Kartman said, on system like Linux or Android it might have some meaning ... ?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...