Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #23515

Re: Use RAM to modify a field

Newsgroups comp.arch.embedded
Date 2016-04-05 14:45 -0700
References (4 earlier) <mJudnS_jsJLVKJ_KnZ2dnUU7-R-dnZ2d@giganews.com> <z4ednfU1hsU5SZ_KnZ2dnUU7-Y3NnZ2d@giganews.com> <S7mdneaKWZMacp_KnZ2dnUU7-e2dnZ2d@giganews.com> <kc-dnScTONX5ZZ7KnZ2dnUU7-dHNnZ2d@giganews.com> <z6OdnQHGec2xu5nKnZ2dnUU7-dlg4p2d@giganews.com>
Message-ID <67b4f666-2a5a-4227-84bb-af42104efb79@googlegroups.com> (permalink)
Subject Re: Use RAM to modify a field
From lasselangwadtchristensen@gmail.com

Show all headers | View raw


Den tirsdag den 5. april 2016 kl. 22.55.12 UTC+2 skrev Tim Wescott:
> On Tue, 05 Apr 2016 12:39:48 -0500, srao wrote:
> 
> >>On Mon, 04 Apr 2016 15:55:00 -0500, srao wrote:
> >>
> >>>>On Mon, 04 Apr 2016 12:34:28 -0500, Tim Wescott wrote:
> >>>>
> >>>>> On Mon, 04 Apr 2016 12:09:53 -0500, srao wrote:
> >>>>> 
> >>>>>>>On Fri, 01 Apr 2016 19:37:22 -0500, srao wrote:
> >>>>>>>
> >>>>>>>> I need to use Copy and 8K block of data (from a parameter block
> >>>>>> address
> >>>>>>>> in flash) to RAM, modify data at a particular offset and copy the
> >>>>>>>> contents back to flash. The size of the RAM is 256K.  I believe
> >>>>>> dynamic
> >>>>>>>> memory allocation with heap (using a pointer)should work for
> > this?
> >>>>>> Would
> >>>>>>>> like to know if this would be the right way togo?
> >>>>>>>> Note: Please notify if any detail seems missing.
> >>>>>>>
> >>>>>>>A general description of what you're doing would be useful.
> >>>>>>>
> >>>>>>>If it's an embedded system and it has to run forever, using the
> > heap
> >>> is
> >>>>>>>probably a bad idea (Google "heap fragmentation").  Truly paranoid*
> >>>>>>>embedded engineers would allocate the block statically unless that
> >>>>>>>makes
> >>>>>> 
> >>>>>>>the system run out of RAM.  If they did NOT allocate it statically,
> >>>>>>>then
> >>>>>> 
> >>>>>>>they would ask themselves if they have enough memory in the first
> >>>>>>>place.
> >>>>>>>
> >>>>>>>* In embedded, "paranoid" often means "successful"
> >>>>>>>
> >>>>>>>--
> >>>>>>>
> >>>>>>>Tim Wescott Wescott Design Services http://www.wescottdesign.com
> >>>>>> 
> >>>>>> Also,
> >>>>>> 
> >>>>>> Would static allocation utilize RAM space? I am using this for an
> >>>>>> embedded application which has to read data out from hardware,
> > store
> >>> it
> >>>>>> in RAM and modify the field.
> >>>>>> 
> >>>>>> The RAM here is not part of processor.A separate flashchip  has
> >>>>>> boot+flash+ RAM region  which the application uses. C++ borland
> >>>>>> compiler in use.  Does static memory allocation use space out of
> > RAM?
> >>>>>> Or does it depend on the compiler?
> >>>>> 
> >>>>> I believe that you are asking if using the "static" keyword in some
> > C
> >>> or
> >>>>> C ++ code would cause the tools to put the space in RAM.
> >>>>> 
> >>>>> The answer is that if the tools are working correctly, and if the
> >>> space
> >>>>> is not declared as "const", then yes.  The tools working correctly
> >>>>> depends on both the tools (Borland was still good the last time I
> > used
> >>>>> it) and how they're set up, though, so I can't just say that it WILL
> >>>>> work for you based on the fact that it's Borland.
> >>>>> 
> >>>>> HOWEVER -- if you're declaring a variable outside of any struct,
> >>>>> function, or whatever, then "static" is telling the compiler about
> > who
> >>>>> can see the variable, not whether the variable is writable (and thus
> >>> in
> >>>>> RAM) or not.
> >>>>> 
> >>>>> Here's what SHOULD happen in most embedded systems, in C or C++, if
> >>> you
> >>>>> declare a variable outside of any struct, function, or whatever:
> >>>>> 
> >>>>> // "whatever" gets put in read-only memory, and is visible outside
> > of
> >>> //
> >>>>> the resulting object file const sometype whatever = <inital value>;
> >>>>> 
> >>>>> // "whatever" gets put in read-only memory, and is only visible to
> > //
> >>>>> things within the C file:
> >>>>> const static sometype whatever = <initial value>;
> >>>>> 
> >>>>> // "whatever" gets put in RAM, is visible outside of // the
> > resulting
> >>>>> object file, and (if the tools are set // up correctly) filled with
> > 0
> >>>>> bytes:
> >>>>> sometype whatever;
> >>>>> 
> >>>>> // "whatever" gets put in RAM, is only visible to // things within
> > the
> >>> C
> >>>>> file, and (if the tools are set // up correctly) filled with 0
> > bytes:
> >>>>> static sometype whatever;
> >>>>> 
> >>>>> HTH
> >>>>> 
> >>>>> If David Brown says that I'm wrong, just fill in a resigned sigh
> > from
> >>> me
> >>>>> and believe what he says.
> >>>>
> >>>>I failed to mention:
> >>>>
> >>>>If you want to permanently allocate some space at compile time, from
> >>>>within a function, then you need to use "static" -- otherwise the
> >>>>compiler will try to create it on the heap or on the stack.  For an
> > 8kB
> >>>>hunk of memory, this would probably cause Bad Things to happen.
> >>>>
> >>>>void my_function(void)
> >>>>{
> >>>>  char bob[8192];  // Allocates on stack or heap -- ick static char
> > sue
> >>>>  [8192];  // allocates in RAM, only visible
> >>>>                           // within this function
> >>>>}
> >>>>
> >>>>Note that doing this, with this large a chunk of memory, is probably
> > an
> >>>>indication that you're doing something wrong.  I could easily see your
> >>>>buffer as being static within a C file (or a C++ structure) that's
> >>>>dedicated to talking to flash -- but I can't easily see it within a
> >>>>function.
> >>>>
> >>>>--
> >>>>
> >>>>Tim Wescott Wescott Design Services http://www.wescottdesign.com
> >>> 
> >>> Thanks! Completely understand. So do you feel there is a better way to
> >>> handle this?
> >>
> >>Better than what?
> >>
> >>Keep in mind that with Embedded system programming there is no "best".
> >>Using the heap is almost always not at all a good idea, but there are a
> >>very few applications where it'll work OK.  Ditto for just about any
> >>other idea that works.  Driving factors are how long the thing has to
> >>work between reboots, how bad it is if the processor cocks up its toes
> >>unexpectedly, how much RAM you're actually using, how much time you have
> > 
> >>to spend on coding, and, to some extent, how much time you need to
> >>complete the operation.
> >>
> >>If, for instance, you told us that the product has to run for years and
> >>years between reboots that would point away from using the heap, ever.
> >>But if you then said that the _only_ time that this code is ever used is
> > 
> >>in the factory or by service personnel who then immediately cycle power,
> > 
> >>then I'd say "sure, go ahead" (in fact, this is one of the two places
> >>where I'll routinely use the heap).
> >>
> >>In nearly all cases that I can think of, statically allocating a chunk
> >>that's dedicated to this is best, because it's safe (unless you're
> >>running out of RAM) and easy (so, fewer lines of code and hence fewer
> >>opportunities for bugs).  But it's not best if you're almost out of
> >>memory.
> >>
> >>Rob's idea of allocating on the stack would work well in a lot of cases,
> > 
> >>but you're using an old 16-bit x86 processor with segments, and that
> >>makes it a not-quite-so-good idea because you're not talking about up to
> > 
> >>1/32 of the available RAM, you're talking about up to 1/8 of the
> >>available stack space -- that seems a lot more cramped to me.
> >>
> >>Rob's idea of a "monoheap" could also be best, if you're really having
> >>to
> > 
> >>shake the box to try to make room for a bit more cereal.
> >>
> >>--
> >>
> >>Tim Wescott Wescott Design Services http://www.wescottdesign.com
> > 
> > Well, this is used by service personnel once in a while during a image
> > update only. Once the image is in, the application running does not need
> > to use this functionality, is a part of boot only.
> 
> In that case then I'd say go ahead and use the heap.  Most of the "OMG" 
> connected with heap usage in an embedded system is that heaps don't 
> usually stand up well to having everything in the world using one heap 
> for a very long time.
> 
> You are proposing a very limited use of the heap -- it certainly works 
> for me.
> 

and the heap and stack will usually be competing for the same memory


-Lasse

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-01 19:37 -0500
  Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-02 19:35 -0500
    Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-04 08:55 +0200
      Re: Use RAM to modify a field Jack <jack4747@gmail.com> - 2016-04-04 00:52 -0700
      Re: Use RAM to modify a field Grant Edwards <invalid@invalid.invalid> - 2016-04-04 14:24 +0000
      Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:25 -0500
        Re: Use RAM to modify a field upsidedown@downunder.com - 2016-04-04 21:42 +0300
          Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:41 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:02 +0200
    Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 11:42 -0500
    Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 12:09 -0500
      Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:34 -0500
        Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 13:41 -0500
          Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 18:49 +0000
            Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:46 -0500
              Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 16:32 -0500
                Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 21:48 +0000
          Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:55 -0500
            Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 17:49 -0500
              Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 12:39 -0500
                Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-05 15:55 -0500
                Re: Use RAM to modify a field lasselangwadtchristensen@gmail.com - 2016-04-05 14:45 -0700
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 19:22 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:29 +0200
        Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:50 -0500
          Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:38 +0200
            Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:11 -0500
            Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:46 -0500
        Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:17 +0200
          Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 09:58 +0300
            Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:54 -0500
              Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 22:46 +0300
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 18:21 -0500
                Re: Use RAM to modify a field Reinhardt Behm <rbehm@hushmail.com> - 2016-04-06 12:58 +0800
                Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-06 11:57 -0500
                Re: Use RAM to modify a field Dave Nadler <drn@nadler.com> - 2016-04-06 10:20 -0700
          Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:04 -0500

csiph-web