Skip to content
Snippets Groups Projects
Commit ceb88d6b authored by Bengt's avatar Bengt
Browse files

Cleaning up

parent f09c215f
Branches
Tags
No related merge requests found
/*
* ArduinoZeroTemplate.c
*
* Created: 2020-12-09 09:56:19
* Author: ilosz01
*/
#include <asf.h>
#include "ArduinoZeroTemplate.h"
// Globala variabler
// Instances for peripherals. One for each used.
struct rtc_module rtc_instance_struct;
//struct spi_module spi1_instance_struct;
uint32_t t1ms_timer = 0;
void ArduinoZeroTemplate(void)
{
while (true)
{
}// end while (true) infinite loop
}// end ArduinoZeroTemplate
void ArduinoZeroTemplateInit(void)
{
// Temporary Configuration structures
struct rtc_count_config rtc_count_config_struct;
// If used: struct spi_config spi_config_struct;
// I/O
// Set pin input/output directions.
// For outputs, only the direction bit needs to be set.
// For inputs direction bit is already set but the input buffer must be enabled.
// Most pin functions is set through the PINCFG registers, one for each pin.
// Before writing to any port registers, enable constant input sampling on pins that shall be able to use the faster IOBUS.
// Note: Only Data Output Value, Data Input Value and Pin Direction registers can be accessed through IOBUS operation.
PORT_SET_CTRLSAMPLING(LED0_PIN_PORTNR,LED_0_PIN);
PORT_SET_CTRLSAMPLING(MY_OUTPUT_PIN_PORTNR,MY_OUTPUT_PIN);
PORT_SET_CTRLSAMPLING(MY_INPUT_PIN_PORTNR,MY_INPUT_PIN);
// Configure output pins
// To in all circumstances avoid glitches, set the default value of the pin before setting the direction.
PORT_CLR_PIN(LED0_PIN_PORTNR,LED_0_PIN); // LED
PORT_OUTPUT_PIN(LED0_PIN_PORTNR,LED_0_PIN);
// PORT_SET_PIN(MY_OUTPUT_PIN_PORTNR,MY_OUTPUT_PIN) // MY_OUTPUT
// PORT_OUTPUT_PIN(MY_OUTPUT_PIN_PORTNR,MY_OUTPUT_PIN);
// Configure input pins
// All IN-buffers for pins used as inputs must be enabled. This is controlled through the PINCFG.INEN bit
PORT_INPUT_PIN(MY_INPUT_PIN_PORTNR,MY_INPUT_PIN); // MY_INPUT
PORT_INPUT_PIN_EN(MY_INPUT_PIN_PORTNR,MY_INPUT_PIN);
// End I/O configuration
// RTC
// Time is measured by saving start time and then compare desired time with current time.
rtc_count_get_config_defaults(&rtc_count_config_struct);
// Clock source is hardcoded to GCLK_2 in rtc_count_init and set to prescaler of 1 in Channel8ForceClocks.h
rtc_count_config_struct.mode = RTC_COUNT_MODE_32BIT;
rtc_count_config_struct.prescaler = RTC_COUNT_PRESCALER_DIV_4; // 32kHz/1/4 ~ 12,2 s
rtc_count_config_struct.clear_on_match = false;
//rtc_count_config_struct.continuously_update = true;
//rtc_count_config_struct.enable_read_sync = true;
rtc_count_init(&rtc_instance_struct, RTC, &rtc_count_config_struct);
rtc_count_enable(&rtc_instance_struct);
// USART 1
/* If used
// I/O does not need top be defined separately for USARTS. Is done automatically when defining the use (SPI) below.
port_get_config_defaults(&port_config_struct);
port_config_struct.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(SCK, &port_config_struct); // SCK, Mode 1. Normally low, trigg negative edge. CPOL=0, CPHA=1
port_pin_set_config(MOSI, &port_config_struct);
*/
// SPI
/* If used
spi_get_config_defaults(&spi_config_struct);
spi_config_struct.generator_source = GCLK_GENERATOR_3;
spi_config_struct.mode = SPI_MODE_MASTER;
spi_config_struct.data_order = SPI_DATA_ORDER_MSB;
spi_config_struct.transfer_mode = SPI_TRANSFER_MODE_1;
spi_config_struct.mux_setting = SPI_SIGNAL_MUX_SETTING_D;
spi_config_struct.character_size = SPI_CHARACTER_SIZE_8BIT;
spi_config_struct.pinmux_pad0 = PINMUX_PA16C_SERCOM1_PAD0;
spi_config_struct.pinmux_pad1 = PINMUX_PA17C_SERCOM1_PAD1;
spi_config_struct.pinmux_pad2 = PINMUX_PA18C_SERCOM1_PAD2; // Not used. Can it be ignored to leave open for other use?
spi_config_struct.pinmux_pad3 = PINMUX_PA19C_SERCOM1_PAD3;
spi_config_struct.mode_specific.master.baudrate = 2000000;
spi_init(&spi1_instance_struct, SERCOM1, &spi_config_struct);
spi_enable(&spi1_instance_struct);
*/
} // end init
void Update1msClock(void)
{
// Create a 1 ms clock (0.9765625 ms)
// Note that the program will hang here a maximum of 400 s to synchronize the system clock to the slower RTC clock.
static uint32_t current_time; // Counts in ticks of the RTC clock
static uint32_t previous_time_1ms=0; // Also counts in ticks of the RTC clock
current_time = rtc_count_get_count(&rtc_instance_struct);
if (current_time >= (previous_time_1ms + 8)) // That is 1 ms!
{
t1ms_timer++; // Increase our 1 ms clock
previous_time_1ms = current_time; // Remember the RTC time of the 1 ms tick
}
} // end Create1msClock
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment