What does system_init() do?

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

After reading some of the colossal D10 datasheet, I managed to make a blink program for the SAMD10 Xplained Mini board:

#include "sam.h"

#define LED_PIN 1<<9
int main(void)
{
    /* Initialize the SAM system */
    //SystemInit();

    /* configure generic clock gen. 1 for 1Hz output to pin */
    GCLK->GENDIV.reg =
        GCLK_GENDIV_ID(1)    |
        GCLK_GENDIV_DIV(14)    ;        //division factor = 2^15

    while (GCLK->STATUS.bit.SYNCBUSY);
    GCLK->GENCTRL.reg =
        GCLK_GENCTRL_DIVSEL            |
        GCLK_GENCTRL_GENEN            |
        GCLK_GENCTRL_SRC_OSCULP32K    |
        GCLK_GENCTRL_OE                |
        GCLK_GENCTRL_ID(1)            ;
    
    //configure pin 9 as output and connect to generic clock generator 1
    PORT->Group[0].DIRSET.reg = LED_PIN;
    PORT->Group[0].WRCONFIG.reg =
        /* select pin 9 */
        PORT_WRCONFIG_PINMASK(LED_PIN)    |
        /* write both pin configuration and pin multiplexing */
        PORT_WRCONFIG_WRPINCFG            |
        PORT_WRCONFIG_WRPMUX            |
        /* multiplexer: enable and connect to option H, which is the generic clock generator */
        PORT_WRCONFIG_PMUXEN            |
        PORT_WRCONFIG_PMUX(PORT_PMUX_PMUXE_H_Val)|
        /* pin configuration: high drive strength */
        PORT_WRCONFIG_DRVSTR            ;

    /* wait forever */
    PM->SLEEP.reg = PM_SLEEP_IDLE_APB;
    while (1) __asm("wfi");
}

This program connects a generic clock generator, running at 1 Hz, to the LED port, then the MCU goes to sleep (I hope). So what does system_init() does, exactly? I just commented it out. I found this in some pdf:

System Initialization
system_init() is an ASF function which is used to configure the generic clocks and clock sources as per the
settings in the conf_clocks.h file. The main clock will be configured as stated in Section 4.1. It also initializes
the board hardware of SAM D21 Xplained Pro and the event system.

 So I'm supposed to use this function to initialize stuff?

Last Edited: Sat. Apr 22, 2017 - 12:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are not using ASF (and that is probably best with a SAMD10) why do you think you should use ASF functions?

/Lars
 

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

I'm just asking, because it's the first time I'm programming the SAMD (I'm using AS 6), and when I created a new project, a call to system init was automatically placed in the template. So I didn't know if it was really needed. I don't want to use ASF.

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

Why don't you jut use Atmel Studio's 'Go To Definition' feature - then you can see exactly what it does!!

 

Edit: any particular reason for sticking with AS6 ?

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Edit: any particular reason for sticking with AS6 ?

 

Because: https://www.avrfreaks.net/forum/need-help-as7-only-works-admin-privileges

Regarding the function, it just sets a variable?

/**
 * Initial system clock frequency. The System RC Oscillator (RCSYS) provides
 *  the source for the main clock at chip startup.
 */
#define __SYSTEM_CLOCK    (1000000)

uint32_t SystemCoreClock = __SYSTEM_CLOCK;/*!< System Clock Frequency (Core Clock)*/

/**
 * Initialize the system
 *
 * @brief  Setup the microcontroller system.
 *         Initialize the System and update the SystemCoreClock variable.
 */
void SystemInit(void)
{
    // Keep the default device state after reset
    SystemCoreClock = __SYSTEM_CLOCK;
    return;
}

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

As others have said, it's an ASF thing.

The ASF "wizard" would add other stuff in there ...

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Confusion here, system_init() is not the same as SystemInit().  SystemInit() is not from ASF, it's from CMSIS.

https://www.arm.com/products/pro...

Not much needs init in a D21.

/Lars

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

Lajon wrote:
system_init() is not the same as SystemInit().  

Good catch!

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah, ok, they are different functions. Guess I'll have to read that CMSIS documentation.