From 5d4f759c9207e19b459ada7ca56f50aab95ba9f4 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Tue, 2 Jul 2013 12:46:33 -0400 Subject: [PATCH] lpc43xx/timer: Add timer utilities --- include/libopencm3/lpc43xx/timer.h | 16 +++++++ lib/lpc43xx/m0/Makefile | 2 +- lib/lpc43xx/m4/Makefile | 2 +- lib/lpc43xx/timer.c | 70 ++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 lib/lpc43xx/timer.c diff --git a/include/libopencm3/lpc43xx/timer.h b/include/libopencm3/lpc43xx/timer.h index 6b6ad22f..5a649ccc 100644 --- a/include/libopencm3/lpc43xx/timer.h +++ b/include/libopencm3/lpc43xx/timer.h @@ -249,6 +249,22 @@ LGPL License Terms @ref lgpl_license #define TIMER_CTCR_CINSEL_CAPN_3 (0x3 << 2) #define TIMER_CTCR_CINSEL_MASK (0x3 << 2) +/* --- TIMER function prototypes --------------------------------------------- */ + +BEGIN_DECLS + +void timer_reset(uint32_t timer_peripheral); +void timer_enable_counter(uint32_t timer_peripheral); +void timer_disable_counter(uint32_t timer_peripheral); +uint32_t timer_get_counter(uint32_t timer_peripheral); +void timer_set_counter(uint32_t timer_peripheral, uint32_t count); +uint32_t timer_get_prescaler(uint32_t timer_peripheral); +void timer_set_prescaler(uint32_t timer_peripheral, uint32_t prescaler); +void timer_set_mode(uint32_t timer_peripheral, uint32_t mode); +void timer_set_count_input(uint32_t timer_peripheral, uint32_t input); + +END_DECLS + /**@}*/ #endif diff --git a/lib/lpc43xx/m0/Makefile b/lib/lpc43xx/m0/Makefile index 0aa5bea9..4dc6359b 100644 --- a/lib/lpc43xx/m0/Makefile +++ b/lib/lpc43xx/m0/Makefile @@ -32,7 +32,7 @@ CFLAGS = -O2 -g3 -Wall -Wextra -I../../../include -fno-common \ ARFLAGS = rcs # LPC43xx common files for M4 / M0 -OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o +OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o #LPC43xx M0 specific file + Generic LPC43xx M4/M0 files OBJS = $(OBJ_LPC43XX) diff --git a/lib/lpc43xx/m4/Makefile b/lib/lpc43xx/m4/Makefile index 75dbea34..d06cd916 100644 --- a/lib/lpc43xx/m4/Makefile +++ b/lib/lpc43xx/m4/Makefile @@ -37,7 +37,7 @@ CFLAGS = -O2 -g3 \ ARFLAGS = rcs # LPC43xx common files for M4 / M0 -OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o +OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o #LPC43xx M4 specific file + Generic LPC43xx M4/M0 files OBJS = $(OBJ_LPC43XX) ipc.o diff --git a/lib/lpc43xx/timer.c b/lib/lpc43xx/timer.c new file mode 100644 index 00000000..3fe7393f --- /dev/null +++ b/lib/lpc43xx/timer.c @@ -0,0 +1,70 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2013 Ben Gamari + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + * This provides the code for the "next gen" EXTI block provided in F2/F4/L1 + * devices. (differences only in the source selection) + */ + +#include + +void timer_reset(uint32_t timer_peripheral) +{ + TIMER_TCR(timer_peripheral) |= TIMER_TCR_CRST; + TIMER_TCR(timer_peripheral) &= ~TIMER_TCR_CRST; +} + +void timer_enable_counter(uint32_t timer_peripheral) +{ + TIMER_TCR(timer_peripheral) |= TIMER_TCR_CEN; +} + +void timer_disable_counter(uint32_t timer_peripheral) +{ + TIMER_TCR(timer_peripheral) &= ~TIMER_TCR_CEN; +} + +void timer_set_counter(uint32_t timer_peripheral, uint32_t count) +{ + TIMER_TC(timer_peripheral) = count; +} + +uint32_t timer_get_counter(uint32_t timer_peripheral) +{ + return TIMER_TC(timer_peripheral); +} + +uint32_t timer_get_prescaler(uint32_t timer_peripheral) +{ + return TIMER_PR(timer_peripheral); +} + +void timer_set_prescaler(uint32_t timer_peripheral, uint32_t prescaler) +{ + TIMER_PR(timer_peripheral) = prescaler; +} + +void timer_set_mode(uint32_t timer_peripheral, uint32_t mode) +{ + TIMER_CTCR(timer_peripheral) = (TIMER_CTCR(timer_peripheral) & TIMER_CTCR_MODE_MASK) | mode; +} + +void timer_set_count_input(uint32_t timer_peripheral, uint32_t input) +{ + TIMER_CTCR(timer_peripheral) = (TIMER_CTCR(timer_peripheral) & TIMER_CTCR_CINSEL_MASK) | input; +} +