Commit a1c9e002 authored by Daniel Kampert's avatar Daniel Kampert 👷

Add clock driver for ATtiny0 family

parent 0a09503d
......@@ -3,7 +3,7 @@
*
* Copyright (C) Daniel Kampert, 2018
* Website: www.kampis-elektroecke.de
* File info: Driver for Atmel AVR tinyAVR system clock.
* File info: Driver for Atmel AVR tiny0 system clock.
GNU GENERAL PUBLIC LICENSE:
This program is free software: you can redistribute it and/or modify
......@@ -22,13 +22,13 @@
Errors and commissions should be reported to [email protected]
*/
/** @file Arch/AVR8/tinyAVR/ClockManagement/SysClock.h
* @brief Driver for Atmel AVR tinyAVR clock system.
/** @file Arch/AVR8/tinyAVR/tiny0/ClockManagement/SysClock.h
* @brief Driver for Atmel AVR tiny0 clock system.
*
* This file contains the prototypes and definitions for the Atmel AVR tinyAVR clock system.
* This file contains the prototypes and definitions for the Atmel AVR tiny0 clock system.
*
* @author Daniel Kampert
* @bug No known bugs.
* @bug - Check if NVM can read fuses to detect the right clock
*/
#ifndef SYSCLOCK_H_
......@@ -68,7 +68,7 @@
static inline void SysClock_SetClockSource(const ClockSource_t Source) __attribute__((always_inline));
static inline void SysClock_SetClockSource(const ClockSource_t Source)
{
/*
/*
* Use inline assembler, because otherwise the code will only run with at least
* optimization level -O1. This level is bad for debugging purposes. You can avoid
* this issue by using inline assembler.
......@@ -120,9 +120,6 @@
static inline void SysClock_SetClockPrescaler(const ClockPrescaler_t Prescaler) __attribute__((always_inline));
static inline void SysClock_SetClockPrescaler(const ClockPrescaler_t Prescaler)
{
// Enable the clock prescaler
SysClock_SwitchPrescaler(TRUE);
asm volatile( "movw r30, %0" "\n\t"
"ldi r16, %2" "\n\t"
"out %3, r16" "\n\t"
......@@ -135,6 +132,24 @@
);
}
/** @brief Get the current main system clock.
* @return Clock frequency
*/
static inline const uint32_t SysClock_GetClock(void) __attribute__ ((always_inline));
static inline const uint32_t SysClock_GetClock(void)
{
if(CLKCTRL.MCLKCTRLA & CLKCTRL_CLKSEL_OSCULP32K_gc)
{
return 32000;
}
else if(CLKCTRL.MCLKCTRLA & CLKCTRL_CLKSEL_EXTCLK_gc)
{
return F_CPU;
}
return F_CPU;
}
/** @brief Initialize the CPU clock.
*/
static inline void SysClock_Init(void) __attribute__ ((always_inline));
......@@ -142,4 +157,9 @@
{
}
/** @brief Get the current clock from the clk_Per domain.
* @return Clock frequency
*/
uint32_t SysClock_GetClockPer(void);
#endif /* SYSCLOCK_H_ */
\ No newline at end of file
/*
* SysClock.c
*
* Copyright (C) Daniel Kampert, 2018
* Website: www.kampis-elektroecke.de
* File info: Driver for Atmel AVR tiny0 system clock.
GNU GENERAL PUBLIC LICENSE:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Errors and commissions should be reported to [email protected]
*/
/** @file Arch/AVR8/tinyAVR/tiny0/ClockManagement/SysClock.h
* @brief Driver for Atmel AVR tiny0 clock system.
*
* This contains the implementation of the tiny0 clock system.
*
* @author Daniel Kampert
* @bug No known bugs
*/
#include "Arch/AVR8/tinyAVR/tiny0/ClockManagement/SysClock.h"
uint32_t SysClock_GetClockPer(void)
{
uint32_t SysClock = SysClock_GetClock();
if(CLKCTRL.MCLKCTRLB & CLKCTRL_PEN_bm)
{
uint8_t Prescaler = ((CLKCTRL.MCLKCTRLB >> 0x01) & 0x0F) + 0x01;
if(Prescaler > 0x06)
{
switch(Prescaler)
{
case(CLKCTRL_PDIV_6X_gc):
{
return SysClock / 0x06;
break;
}
case(CLKCTRL_PDIV_10X_gc):
{
return SysClock / 0xA;
break;
}
case(CLKCTRL_PDIV_12X_gc):
{
return SysClock / 0x0C;
break;
}
case(CLKCTRL_PDIV_24X_gc):
{
return SysClock / 0x18;
break;
}
case(CLKCTRL_PDIV_48X_gc):
{
return SysClock / 0x30;
break;
}
}
}
else
{
return SysClock >> Prescaler;
}
}
return SysClock;
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment