From 3ec66893e377b088c1284d2d23adb2aeea6d7965 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 27 Feb 2021 22:08:58 +0000 Subject: New port: FiiO M3K on bare metal Change-Id: I7517e7d5459e129dcfc9465c6fbd708619888fbe --- firmware/target/mips/ingenic_x1000/timer-x1000.c | 85 ++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 firmware/target/mips/ingenic_x1000/timer-x1000.c (limited to 'firmware/target/mips/ingenic_x1000/timer-x1000.c') diff --git a/firmware/target/mips/ingenic_x1000/timer-x1000.c b/firmware/target/mips/ingenic_x1000/timer-x1000.c new file mode 100644 index 0000000000..de97cbb3a3 --- /dev/null +++ b/firmware/target/mips/ingenic_x1000/timer-x1000.c @@ -0,0 +1,85 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2021 Aidan MacDonald + * + * 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 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "timer.h" +#include "x1000/tcu.h" + +#define TIMER_CHN 5 + +bool timer_set(long cycles, bool start) +{ + if(cycles <= 0) + return false; + + /* Calculate timer interval */ + unsigned long counter = cycles; + unsigned prescale = 0; + while(counter > 0xffff && prescale < 5) { + counter /= 4; + prescale += 1; + } + + /* Duration too long */ + if(counter > 0xffff) + return false; + + /* Unregister old function */ + if(start && pfn_unregister) { + pfn_unregister(); + pfn_unregister = 0; + } + + /* Configure the timer */ + jz_clr(TCU_STOP, 1 << TIMER_CHN); + jz_clr(TCU_ENABLE, 1 << TIMER_CHN); + jz_overwritef(TCU_CTRL(TIMER_CHN), SOURCE_V(EXT), PRESCALE(prescale)); + jz_write(TCU_CMP_FULL(TIMER_CHN), counter); + jz_write(TCU_CMP_HALF(TIMER_CHN), 0); + jz_clr(TCU_FLAG, 1 << TIMER_CHN); + jz_clr(TCU_MASK, 1 << TIMER_CHN); + + if(start) + return timer_start(); + else + return true; +} + +bool timer_start(void) +{ + jz_set(TCU_ENABLE, 1 << TIMER_CHN); + return true; +} + +void timer_stop(void) +{ + jz_clr(TCU_ENABLE, 1 << TIMER_CHN); + jz_set(TCU_MASK, 1 << TIMER_CHN); + jz_clr(TCU_FLAG, 1 << TIMER_CHN); + jz_set(TCU_STOP, 1 << TIMER_CHN); +} + +void TCU1(void) +{ + jz_clr(TCU_FLAG, 1 << TIMER_CHN); + + if(pfn_timer) + pfn_timer(); +} -- cgit v1.2.3