Contiki 3.x
pit.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 <math.h>
32 
33 #include "drivers/legacy_pc/pic.h"
34 #include "drivers/legacy_pc/pit.h"
35 #include "helpers.h"
36 #include "interrupt.h"
37 
38 /* PCs usually provide an 8254 PIT chip with maximum clock of 1.193182 MHz. */
39 #define PIT_CONTROL_PORT 0x43
40 #define PIT_COUNTER0_PORT 0x40
41 #define PIT_CLOCK_FREQUENCY 1193182
42 #define PIT_IRQ 0
43 #define PIT_INT PIC_INT(PIT_IRQ)
44 
45 static pit_int_callback interrupt_cb;
46 
47 static void
48 pit_int_handler(void)
49 {
50  interrupt_cb();
51 
52  pic_eoi(PIT_IRQ);
53 }
54 /*---------------------------------------------------------------------------*/
55 void
56 pit_init(uint32_t ticks_rate, pit_int_callback cb)
57 {
58  SET_INTERRUPT_HANDLER(PIT_INT, 0, pit_int_handler);
59 
60  interrupt_cb = cb;
61 
62  /* Calculate the 16bit divisor that can provide the chosen clock tick rate
63  * (CLOCK_CONF_SECOND in contiki-conf.h). For reference --> tick rate = clock frequency / divisor.
64  * If we provide an odd divisor to the Square Wave generator (Mode 3) of
65  * the Counter0, the duty cycle won't be exactly 50%, so we always round
66  * it to nearest even integer.
67  */
68  uint16_t divisor = rint(PIT_CLOCK_FREQUENCY / ticks_rate);
69 
70  /* Setup Control register flags in a didactic way. */
71  uint8_t flags = 0x30; /* Set bits 7:6 to select Counter0 and 5:4 to select "write 7:0 bits first". */
72  flags |= 0x6; /* Set bits 3:1 to Mode 3 and bit 0 to BCD off. */
73 
74  outb(PIT_CONTROL_PORT, flags);
75 
76  outb(PIT_COUNTER0_PORT, divisor & 0xFF); /* Write least significant bytes first. */
77  outb(PIT_COUNTER0_PORT, (divisor >> 8) & 0xFF);
78 
79  pic_unmask_irq(PIT_IRQ);
80 }