summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Borges <paulo.borges@openbossa.org>2013-12-20 18:37:08 -0300
committerPaulo Borges <paulo.borges@openbossa.org>2013-12-23 11:29:37 -0300
commitb97119f9d6fdc145cb337cbf63f96c485df71c04 (patch)
tree6b2d138d6cc36c6c393d51422b1be3ac50e1815b
parent58004ccadc1c9cc6fa9e153092ff57af5f64d718 (diff)
nrf51822: Implement timer module
-rw-r--r--platform/nrf51822/Makefile.platform4
-rw-r--r--platform/nrf51822/timer.c97
2 files changed, 100 insertions, 1 deletions
diff --git a/platform/nrf51822/Makefile.platform b/platform/nrf51822/Makefile.platform
index e250e20..be8b3ed 100644
--- a/platform/nrf51822/Makefile.platform
+++ b/platform/nrf51822/Makefile.platform
@@ -85,9 +85,11 @@ PLATFORM_SOURCE_FILES = system_nrf51.c \
app_uart.c \
app_fifo.c \
app_gpiote.c \
+ app_timer.c \
nrf_delay.c \
nrf51822.c \
- log.c
+ log.c \
+ timer.c
PLATFORM_ASM_PATHS = $(SDK_TEMPLATE_PATH)/gcc
diff --git a/platform/nrf51822/timer.c b/platform/nrf51822/timer.c
new file mode 100644
index 0000000..0e2ddde
--- /dev/null
+++ b/platform/nrf51822/timer.c
@@ -0,0 +1,97 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013 Paulo B. de Oliveira Filho <pauloborgesfilho@gmail.com>
+ * Copyright (c) 2013 Claudio Takahasi <claudio.takahasi@gmail.com>
+ * Copyright (c) 2013 João Paulo Rechi Vita <jprvita@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <nrf51.h>
+#include <nrf51_bitfields.h>
+#include <app_timer.h>
+
+#include "timer.h"
+#include "log.h"
+
+#define PRESCALER 0
+#define MAX_TIMERS 4
+#define OP_QUEUE_SIZE 7
+
+static uint32_t APP_TIMER_BUF[CEIL_DIV(APP_TIMER_BUF_SIZE(MAX_TIMERS,
+ OP_QUEUE_SIZE + 1), sizeof(uint32_t))];
+
+int16_t timer_init(void)
+{
+ if (!NRF_CLOCK->EVENTS_LFCLKSTARTED) {
+ NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal
+ << CLOCK_LFCLKSRC_SRC_Pos;
+ NRF_CLOCK->TASKS_LFCLKSTART = 1;
+ while (!NRF_CLOCK->EVENTS_LFCLKSTARTED);
+ }
+
+ if (app_timer_init(PRESCALER, MAX_TIMERS, OP_QUEUE_SIZE + 1,
+ APP_TIMER_BUF, NULL) != NRF_SUCCESS)
+ return -1;
+
+ return 0;
+}
+
+int16_t timer_start(uint8_t type, uint32_t ms, timer_cb cb, void *user_data)
+{
+ app_timer_mode_t mode;
+ uint32_t ticks;
+ uint32_t id;
+
+ if (cb == NULL)
+ return -1;
+
+ switch (type) {
+ case TIMER_SINGLESHOT:
+ mode = APP_TIMER_MODE_SINGLE_SHOT;
+ break;
+ case TIMER_REPEATED:
+ mode = APP_TIMER_MODE_REPEATED;
+ break;
+ default:
+ return -1;
+ }
+
+ if (app_timer_create(&id, mode, cb) != NRF_SUCCESS)
+ return -1;
+
+ ticks = APP_TIMER_TICKS(ms, PRESCALER);
+
+ if (app_timer_start(id, ticks, user_data) != NRF_SUCCESS)
+ return -1;
+
+ return id;
+}
+
+int16_t timer_stop(int16_t id)
+{
+ if (id < 0)
+ return -1;
+
+ if (app_timer_stop(id) != NRF_SUCCESS)
+ return -1;
+
+ return 0;
+}