Blame view

RIOT/cpu/saml21/periph/rtt.c 3.28 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  /*
   * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
   *               2015 FreshTemp, LLC.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     cpu_saml21
   * @ingroup     drivers_periph_rtt
   * @{
   *
   * @file        rtt.c
   * @brief       Low-level RTT driver implementation
   *
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   *
   * @}
   */
  
  #include <stdint.h>
  #include "periph/rtt.h"
  #include "board.h"
  
  #define ENABLE_DEBUG 0
  #include "debug.h"
  
  static rtt_cb_t _overflow_cb;
  static void* _overflow_arg;
  
  static rtt_cb_t _cmp0_cb;
  static void* _cmp0_arg;
  
  void rtt_init(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      rtt_poweron();
  
      /* reset */
      RTC->MODE0.CTRLA.bit.SWRST = 1;
      while(RTC->MODE0.CTRLA.bit.SWRST) {}
  
      /* set 32bit counting mode */
      RTC->MODE0.CTRLA.bit.MODE = 0;
  
      /* set clock source */
      OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_ULP32K;
  
      /* enable */
      RTC->MODE0.CTRLA.bit.ENABLE = 1;
      while(RTC->MODE0.SYNCBUSY.bit.ENABLE) {}
  
      /* initially clear flag */
      RTC->MODE0.INTFLAG.reg |= RTC_MODE1_INTFLAG_CMP(1 << 0);
  
      /* enable RTT IRQ */
      NVIC_EnableIRQ(RTC_IRQn);
  
      DEBUG("%s:%d %u\n", __func__, __LINE__, (unsigned)rtt_get_counter());
  }
  
  void rtt_set_overflow_cb(rtt_cb_t cb, void *arg)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      /* clear overflow cb to avoid race while assigning */
      rtt_clear_overflow_cb();
  
      /* set callback variables */
      _overflow_cb = cb;
      _overflow_arg = arg;
  
      /* enable overflow interrupt */
      RTC->MODE0.INTENSET.bit.OVF = 1;
  }
  void rtt_clear_overflow_cb(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      /* disable overflow interrupt */
      RTC->MODE0.INTENCLR.bit.OVF = 1;
  }
  
  uint32_t rtt_get_counter(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      while (RTC->MODE0.SYNCBUSY.bit.COUNT) {}
      return RTC->MODE0.COUNT.reg;
  }
  
  void rtt_set_alarm(uint32_t alarm, rtt_cb_t cb, void *arg)
  {
      DEBUG("%s:%d alarm=%u\n", __func__, __LINE__, (unsigned)alarm);
  
      /* disable interrupt to avoid race */
      rtt_clear_alarm();
  
      /* set COM register */
      while (RTC->MODE0.SYNCBUSY.bit.COMP0) {}
      RTC->MODE0.COMP[0].reg = alarm;
  
      /* setup callback */
      _cmp0_cb = cb;
      _cmp0_arg = arg;
  
      /* enable compare interrupt */
      RTC->MODE0.INTENSET.bit.CMP0 = 1;
  }
  
  void rtt_clear_alarm(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      /* clear compare interrupt */
      RTC->MODE0.INTENCLR.bit.CMP0 = 1;
  }
  
  void rtt_poweron(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      MCLK->APBAMASK.reg |= MCLK_APBAMASK_RTC;
  }
  
  void rtt_poweroff(void)
  {
      DEBUG("%s:%d\n", __func__, __LINE__);
      MCLK->APBAMASK.reg &= ~MCLK_APBAMASK_RTC;
  }
  
  void isr_rtc(void)
  {
      if (RTC->MODE0.INTFLAG.bit.OVF) {
          RTC->MODE0.INTFLAG.reg |= RTC_MODE0_INTFLAG_OVF;
          if (_overflow_cb) {
              _overflow_cb(_overflow_arg);
          }
      }
      if (RTC->MODE0.INTFLAG.bit.CMP0) {
          /* clear flag */
          RTC->MODE0.INTFLAG.reg |= RTC_MODE1_INTFLAG_CMP(1 << 0);
          /* disable interrupt */
          RTC->MODE0.INTENCLR.bit.CMP0 = 1;
          if (_cmp0_cb) {
              _cmp0_cb(_cmp0_arg);
          }
      }
      cortexm_isr_end();
  }