diff --git a/CMakeLists.txt b/CMakeLists.txt index 945ade9..91dbe05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,6 +117,7 @@ set(CMAKE_VERBOSE_MAKEFILE OFF) # Set it to ON to have verbose build output # Construct executable add_executable(${EXECUTABLE} ${SRC_DIR}/Mcal/mtimer.c + ${SRC_DIR}/Mcal/Clock.c ${SRC_DIR}/Mcal/Mcu.c ${SRC_DIR}/Startup/boot.s ${SRC_DIR}/Startup/intvect.c diff --git a/Code/Mcal/Clock.c b/Code/Mcal/Clock.c new file mode 100644 index 0000000..dd6a0b0 --- /dev/null +++ b/Code/Mcal/Clock.c @@ -0,0 +1,73 @@ +/****************************************************************************************** + Filename : Clock.c + + Core : RV32IMAC + + MCU : FE310-G002 (SiFive) + + Author : Chalandi Amine + + Owner : Chalandi Amine + + Date : 11.12.2022 + + Description : Clock driver implementation + +******************************************************************************************/ + +//===================================================================================================== +// Includes +//===================================================================================================== +#include "FE310.h" +#include "Clock.h" +#include "riscv-csr.h" + +//===================================================================================================== +// Functions prototype +//===================================================================================================== + +//===================================================================================================== +// Macros +//===================================================================================================== + +//===================================================================================================== +// Globals +//===================================================================================================== + + +//----------------------------------------------------------------------------------------- +/// \brief +/// +/// \param +/// +/// \return +//----------------------------------------------------------------------------------------- +void FE310_ClockInitialization(void) +{ + /* wait for HFXOSC to be become ready */ + while(!PRCI->hfxosccfg.bit.ready); + + /* select pllref clock (HFXOSC) */ + PRCI->pllcfg.bit.refsel = 1; + + /* divide pllref (HFXOSC) by 2 ==> refr = 8 MHz */ + PRCI->pllcfg.bit.pllr = 1; + + /* multiply refr by 96 ==> vco = 768 MHz */ + PRCI->pllcfg.bit.pllf = 47; + + /* divide vco by 4 ==> pllout = 192 MHz */ + PRCI->pllcfg.bit.pllq = 2; + + /* bypass final pllout divider */ + PRCI->plloutdiv.bit.divby1 = 1; + + /* drive the final hfclk with the PLL output */ + PRCI->pllcfg.bit.sel = 1; + + /* disable pll bypass */ + PRCI->pllcfg.bit.bypass = 0; + + /* wait for pll to lock */ + while(!PRCI->pllcfg.bit.lock); +} diff --git a/Code/Mcal/Clock.h b/Code/Mcal/Clock.h new file mode 100644 index 0000000..b79e5f8 --- /dev/null +++ b/Code/Mcal/Clock.h @@ -0,0 +1,29 @@ +/****************************************************************************************** + Filename : Clock.h + + Core : RV32IMAC + + MCU : FE310-G002 (SiFive) + + Author : Chalandi Amine + + Owner : Chalandi Amine + + Date : 11.12.2022 + + Description : Clock driver header file + +******************************************************************************************/ + +#ifndef __CLOCK_H__ +#define __CLOCK_H__ + +//===================================================================================================== +// Functions prototype +//===================================================================================================== +void FE310_ClockInitialization(void); + + +#endif /* __MCU_H__ */ + + diff --git a/Code/Mcal/Mcu.c b/Code/Mcal/Mcu.c index 4aa71fa..ebfc379 100644 --- a/Code/Mcal/Mcu.c +++ b/Code/Mcal/Mcu.c @@ -19,6 +19,7 @@ // Includes //===================================================================================================== #include "FE310.h" +#include "Clock.h" #include "Mcu.h" #include "riscv-csr.h" @@ -44,6 +45,9 @@ //----------------------------------------------------------------------------------------- void FE310_HwInitialization(void) { + /* Configure the cpu and the peripheral clocks */ + FE310_ClockInitialization(); + /* Set output high (and set value before switching to output). */ GPIO0->output_val.bit.pin5 = 1; GPIO0->output_en.bit.pin5 = 1; diff --git a/Makefile b/Makefile index 4c1ac25..43e9237 100644 --- a/Makefile +++ b/Makefile @@ -183,6 +183,7 @@ endif ############################################################################################ SRC_FILES := $(SRC_DIR)/Mcal/mtimer.c \ + $(SRC_DIR)/Mcal/Clock.c \ $(SRC_DIR)/Mcal/Mcu.c \ $(SRC_DIR)/Startup/boot.s \ $(SRC_DIR)/Startup/intvect.c \