did i breack my SAMC21 xplained pro? - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 08:44:35 am
content: I tried setting all the pins of my SAMC21 Xplained pro board as outputs and toggling all pins. On port A I got a 1.2V signal that appeared as though perhaps capacitively coupled. Have I damaged it? or is it just that something else on the board is in conflict?

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 11:57:33 am
content: What specific pin on PA? Or any pin behaves like that?It is basically impossible to brick the device board or the device with incorrect program.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 12:13:16 pm
content: OK, I will go round one pin at a time. PortB was fine but i think most things are on portA

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 12:24:11 pm
content: Are the pins used by onboard stuff like CANbus not in the headers at the side? That would simplify life.Interestingly I have written a program to toggle a pin on a simple AS project and it is using nearly 1 kB of RAM?

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 12:27:27 pm
content: Not all pins are available on the headers, some just go to dedicated peripherals.Default ASF linker scripts allocate a fixed size for the stack, so what you see is that stack size. My scripts allocate stack at the end of RAM. This is more convenient for memory usage measurement, but it is on you to make sure that there is enough RAM left for the stack.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 01:09:45 pm
content: It works, I guess i should just not try to take over the whole port. Also getting more comfortable with the Atmel register definitionsCode:#include "sam.h"#define SL_pin (1<< 21)int main(void){/* Initialize the SAM system */SystemInit();REG_PORT_DIR0 = SL_pin;/* Replace with your application code */while (1){REG_PORT_OUTTGL0 = SL_pin;}}

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 01:13:28 pm
content: Do I actually need that initialize thing? what is it initializing? I am not using ASF, Harmony or any other non described bollocks just doing what I always do on an MCU, write code to manipulate whatever is in the datasheet.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 01:17:31 pm
content: SystemInit() does all the clock initialization. If you plan to do that your self later, then you don't need it. If you just want to run on the default clocks, then you also don't need it. Just have a look at its code and you will see what it does. The function is a part of a frameworks, so if you really want to not use a frameworks, then you should definitely remove it and provide the same functionality manually.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 01:32:02 pm
content: Well disabling it does not change the amount of RAM used so presumably that is a hardware thing?I guess i will change the clocks later. I do remember having a quick look and seeing it to be only about clock stuff. I could use it if it's convenient.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 01:45:39 pm
content: As I said, the RAM is specified in the linker script. ASF linker scripts reserve that memory for the stack.On 8-bit devices you typically did not have to think about linker scripts unless you were doing something special (like a bootloader or an application that works with a bootloader). Compiler just came with some predefined linker scripts. With ARM there are no predefined scripts. The scripts is the part of the program and parts of the complete system are specified through the linker scripts. You need to have at least a basic understanding of how they work.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 03:38:28 pm
content: I'm confused, I am not using ASF. why is it boing built into my application without my knowledge? it can only be in some setting of AS or is it some code in the army of definition files that the innocent looking samc.h file brings in. Now you know why i wanted to work without the vendors crap, this is starting to become like trying to work with windows10!

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 03:41:18 pm
content: How did you create the original project?

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 04:53:53 pm
content: It's just a blank AS project.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 05:11:08 pm
content: Ok, this is my bad. In this case SystemInit() simply assigns one variable. You are not using ASF.The stuff about linker scripts is still correct. Even a simple project has to have a linker script. And the scripts used in the default projects are the same as used in the ASF.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 05:13:50 pm
content: I just created an empty project and the linker file is located here GccApplication1\Device_Startup\samc21j18a_flash.ld. The line that defines the stack size is this:QuoteSo the default stack size for thew device I selected (samc21j18a) is 0x2000 or 8192 bytes. And this would be the minimal reported RAM consumption.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 05:17:41 pm
content: Yea, 10% of the RAM. The project uses 28% I think so I'm not entirely sure where it goes. Not a major issue now as I don't tend to do stuff that needs too much RAM.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 05:21:16 pm
content: You can adjust it to suit your needs. You will need the stack no matter what, but the size depends on the application and the way you code. If you don't keep big structures on the stack and don't call a lot of functions (or do deep recursion), then you can reduce it quite a bit.Or you can rewrite linker scripts the way I do. This way stack will be fixed at the end of the RAM. But in this case you will have to remember to leave enough RAM for the stack. This is the way things work on AVRs by default.

author: Simon
author_rank: Unknown
timestamp: September 16, 2019, 05:24:40 pm
content: So reserving some of the RAM like this is what the MCU does in order to run the program? A bit like a PC OS reserving memory? If it's actually used for a purpose then fair enough.

author: ataradov
author_rank: Super Contributor
timestamp: September 16, 2019, 05:32:19 pm
content: Yes, stack memory is always present and used on all MCUs. There are some exceptions for very-very-very low end MCUs. And even there there is usually at least a limited hardware call stack.Stack is used to store variables that are local to the function and to pass function arguments during the call.Keep in mind that even with this fixed size, it is still on you to not exceed it in the application. So this codeCode:void foo(void){uint8_t big_array;big_array = 123;}with the above linker script will fail in run-time and it will overwrite some other variables. The compiler will not complain here.My script is better in that respect, since you essentially get all the remaining memory allocated for the stack. So if you don't use a lot of static (global) variables, then you have a better chance to not run into a stack overflow situation.

author: cv007
author_rank: Frequent Contributor
timestamp: September 16, 2019, 07:28:03 pm
content: > But in this case you will have to remember to leave enough RAM for the stackYou can probably just modify the .stack section in the linker script, with something like this at the beginning-. = ORIGIN(ram) + LENGTH(ram) - STACK_SIZE;that way you get the warning if STACK_SIZE is not available, but still get _estack set to the end of ram. Probably not a big deal, but if you are not paying attention (and you are a hungry ram user), I imagine it can creep up and a little warning from the linker before 'something' happens may be nice.'arm-none-whatever-size -A' may produce a better rundown of memory usage (.stack will be displayed separately from bss and relocate/data).I'm not sure where a malloc/new starts looking when no heap symbols specified, so there could be some reason the above may not be a good idea.