1

It is known that in AVR microcontrollers the stack pointer decreases for each element added to the stack. I want to reserve memory for a new task stack, and I use

unsigned char *pS = (unsigned char*)malloc(64*sizeof(unsigned char));

This I think returns a pointer to the beginning of an area in SRAM followed by 64 bytes, therefore for setting the stack pointer at the new stack I use

SP = (unsigned int)pS + 64

Is this correct?

C Marius
  • 189
  • 9
  • 1
    [don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Aug 22 '17 at 13:31
  • and `sizeof(unsigned char)` is completely redundant, it's defined to be `1`. –  Aug 22 '17 at 13:33
  • apart from that, looks correct, but there's no way to set a stack pointer in C .. C doesn't know what a *stack pointer* **is**. You'll have to write (maybe embed) some assembler code. While at it, it's probably unnecessary to use `malloc()`, just use a linker script and reserve some space for a second stack. –  Aug 22 '17 at 13:34
  • I definitely would not use malloc and write own memory manager. Remember that you work with tiny amount of RAM – 0___________ Aug 22 '17 at 13:38
  • Thank you for your answer ... SP = *(the adress of the stack pointer) it is defined some where – C Marius Aug 22 '17 at 13:39
  • But I am more interested here if it is correct to add the the address returned by malloc the number of bytes allocated to be a valid stack pointer ... – C Marius Aug 22 '17 at 13:41
  • @Lưu Vĩnh Phúc it is silly. This advice comes from the ancient era when compilers were not issuing warnings about implicit declarations. Nowadays if someone is ignoring this warning - will ignore other as well :). The rest is only style of coding which is 100% opinion based. – 0___________ Aug 22 '17 at 13:59
  • @PeterJ I agree that this particular "pet peeve" of the SO community is mostly nonsense. But there are several answers posted in that thread, including [mine](https://stackoverflow.com/a/22538350/584518) which states that apart from adding clutter, the cast isn't even an issue since the year 1999. Overall it is better to keep the whole malloc debate inside that thread. – Lundin Aug 22 '17 at 14:25
  • Yes but some users put this link and remark in almost every topic with the malloc involved. It become some kind of religion. – 0___________ Aug 22 '17 at 14:31
  • 1
    @PeterJ Indeed. Perhaps we should start flagging these as "not constructive" or alternatively raise some meta discussion. – Lundin Aug 22 '17 at 14:42
  • Nothing will stop the myth to be thoughtlessly repeated all over again. – 0___________ Aug 22 '17 at 14:46
  • One does not simply change the SP manually through code, it just doesn't make sense. Configure the AVR to use a specific amount of stack, it will reserve the right amount of bytes. And what is a "task stack"? How will this be used in your program? – vgru Aug 22 '17 at 15:58
  • @PeterJ it's not silly, but for different reasons. Pointer casts in C (not C++) are a *code smell*. Unfortunately, the accepted answer in the linked question gives a very outdated reason instead. –  Aug 22 '17 at 16:01
  • @Groo I wanted to have some space in RAM, to be considered as a stack. That is, after I make the Stack Pointer to point at the end of that memory location, I expect "push" and "pop" to operate on that memory ... – C Marius Aug 22 '17 at 16:05
  • @Lundin I [added an answer](https://stackoverflow.com/a/45713482/2371524) as well because I felt the need to explain a not outdated reason I consider to be very strong. –  Aug 22 '17 at 16:05
  • @Felix Palmen your answer is unfortunately about something else. – 0___________ Aug 22 '17 at 16:31
  • @PeterJ no it isn't. But to understand this requires you to understand the abstractions the C standard makes from real machines. Btw, you should comment over there... –  Aug 22 '17 at 16:40
  • @Felix Palmen so lets leave it . I am too dumb to understand this level of abstraction. – 0___________ Aug 22 '17 at 16:43

1 Answers1

2

No it is not correct.

  • Using malloc on an AVR for any purpose is definitely incorrect, because it doesn't make any sense. See this. In addition, how does it even make sense to have the stack residing on the heap?

  • If you need RTOS-like behavior, it means you already picked the wrong MCU for the task. 8-bitters are just far too limited. At a minimum, use a Cortex-M0 for such programs.

That being said, if you want to make your own simple task-switching OS just for the sake of learning, then you would reserve stack space at compile/link-time. This is done through the linker files of your cross-compiler environment. Check how and where the ordinary stack was allocated and then create a custom memory segment of similar size. You'll note that there is not a whole lot of RAM to play with at all.

It is correct that your stack pointer would be at an offset of the stack size. But if you are truly writing an OS, then you wouldn't be allocating the SP as some sort of C pointer, but rather you would let the CPU use the actual SP register.

Also note that setting the stack pointer is one of those things that simply can't be done in C. You must do so through assembler.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @tilz0R Just read the linked post. Atmel AVR is a 8 bit MCU with very little RAM memory. Anyone using malloc on such a system is confused, simple as that. – Lundin Aug 22 '17 at 14:39
  • @tilz0R The question just asks "is this correct?" and I find nothing in the question that _is_ correct. – Lundin Aug 22 '17 at 14:42
  • @tilz0R If the OP was merely asking if their syntax was correct, then any compiler ought could tell them. – Lundin Aug 22 '17 at 14:44
  • in another OP question he was already told that the avr is not the very best choice. But he has decided to go ahead with it – 0___________ Aug 22 '17 at 15:20
  • Good answer as always, but out of interest, do you even have the need to use memory pools ("object pools") when programming embedded microcontrollers without RTOS? – vgru Aug 22 '17 at 15:55
  • 1
    @Groo Sometimes you do, when designing for OO by using opaque pointers. Since malloc isn't an option and that technique relies on the "class" doing the object allocation internally, each such class ends up with its own memory pool. Typically just 10 objects or so, at most. – Lundin Aug 23 '17 at 06:25