#include <stdint.h>

#include "clock.h"


static struct prci_mem_map *const prci_mm = (struct prci_mem_map *const)PRCI_MEMORY_BLOCK;


// Sets the hfpclkpll register to the appropriate value for the freqency, returns
// -1 on error
// TODO: for higher frequencies use the internal PLLs
int set_hfp_frequency(uint32_t f)
{
	// test if the correct register is present
	if (!prci_mm->prci_plls.hfpclkpll)
		return -1;

	// make sure that the clock source is set to external
	prci_mm->hfpclkpllsel = 1;

	// the external clock source is 26MHz, the formula for the baud rate is:
	// f_baud = f_hfclk / (1 + hfpclk_div_reg)
	prci_mm->hfpclk_div_reg = (HFCLK_FREQ_HZ/f + 1);

	return 0;
}
