Skip to content

Commit

Permalink
add clock initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalandi committed Dec 14, 2022
1 parent 5f4ed52 commit 7288b91
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions Code/Mcal/Clock.c
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 29 additions & 0 deletions Code/Mcal/Clock.h
Original file line number Diff line number Diff line change
@@ -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__ */


4 changes: 4 additions & 0 deletions Code/Mcal/Mcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Includes
//=====================================================================================================
#include "FE310.h"
#include "Clock.h"
#include "Mcu.h"
#include "riscv-csr.h"

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 7288b91

Please sign in to comment.