Contiki 3.x
rtc.c
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "drivers/legacy_pc/rtc.h"
32 #include "drivers/legacy_pc/pic.h"
33 #include "drivers/legacy_pc/nmi.h"
34 #include "helpers.h"
35 #include "interrupt.h"
36 
37 #define RTC_INDEX_REGISTER 0x70
38 #define RTC_TARGET_REGISTER 0x71
39 #define RTC_IRQ 8
40 #define RTC_INT PIC_INT(RTC_IRQ)
41 
42 static void (*user_callback)(void);
43 
44 static void
46 {
47  user_callback();
48 
49  /* Clear Register C otherwise interrupts will not happen again.
50  * Register C is automatically cleared when it is read so we do
51  * a dummy read to clear it.
52  */
53  outb(RTC_INDEX_REGISTER, 0x0C);
54  inb(RTC_TARGET_REGISTER);
55 
56  /* Issue the End of Interrupt to PIC */
57  pic_eoi(RTC_IRQ);
58 }
59 /*---------------------------------------------------------------------------*/
60 /* Initialize the Real Time Clock.
61  * @frequency: RTC has very specific values for frequency. They are: 2, 4, 8,
62  * 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, and 8192 Hz.
63  * value otherwise it will not work properly.
64  * @callback: This callback is called every time the RTC IRQ is raised.
65  * It is executed in interrupt context.
66  */
67 void
68 rtc_init(rtc_frequency_t frequency, void (*callback)(void))
69 {
70  uint8_t reg_a, reg_b;
71 
72  user_callback = callback;
73 
74  SET_INTERRUPT_HANDLER(RTC_INT, 0, rtc_handler);
75 
76  nmi_disable();
77 
78  /* Select interrupt period to 7.8125 ms */
79  outb(RTC_INDEX_REGISTER, 0x8A);
80  reg_a = inb(RTC_TARGET_REGISTER);
81  reg_a &= 0xF0;
82  reg_a |= frequency;
83  outb(RTC_INDEX_REGISTER, 0x8A);
84  outb(RTC_TARGET_REGISTER, reg_a);
85 
86  /* Enable periodic interrupt */
87  outb(RTC_INDEX_REGISTER, 0x8B);
88  reg_b = inb(RTC_TARGET_REGISTER);
89  outb(RTC_INDEX_REGISTER, 0x8B);
90  outb(RTC_TARGET_REGISTER, reg_b | BIT(6));
91 
92  nmi_enable();
93 
94  pic_unmask_irq(RTC_IRQ);
95 }
#define BIT(x)
Useful to reference a single bit of a byte.
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock.c:71