Blame view

RIOT/cpu/x86/x86_pit.c 2.06 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * 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 2.1 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, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup     x86-irq
   * @{
   *
   * @file
   * @brief       Configuration and interrupt handling for the Programmable Interval Timer (PIT).
   *
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include "x86_interrupts.h"
  #include "x86_pit.h"
  #include "x86_ports.h"
  #include "cpu.h"
  #include "irq.h"
  
  #include <stdio.h>
  
  void x86_init_pit(void)
  {
      puts("PIT initialized");
  }
  
  uint16_t x86_pit_read(unsigned channel)
  {
      unsigned old_flags = irq_disable();
      outb(PIT_COMMAND_PORT, (channel - 1) << 6 | PIT_ACCESS_MODE_LATCH_COUNT);
      uint16_t lohi = inb(PIT_CHANNEL_0_PORT + channel - 1);
      lohi += inb(PIT_CHANNEL_0_PORT + channel - 1) << 8;
      irq_restore(old_flags);
      return lohi;
  }
  
  void x86_pit_set2(unsigned channel, unsigned mode, uint16_t max)
  {
      unsigned old_flags = irq_disable();
      outb(PIT_COMMAND_PORT, ((channel - 1) << 6) | mode | PIT_ACCESS_MODE_LO_HI);
      outb(PIT_CHANNEL_0_PORT + channel - 1, max && 0xff);
      outb(PIT_CHANNEL_0_PORT + channel - 1, max >> 8);
      irq_restore(old_flags);
  }
  
  bool x86_pit_set(unsigned channel, unsigned mode, unsigned hz)
  {
      if (PIT_MIN_HZ > hz || hz > PIT_MAX_HZ) {
          return false;
      }
  
      uint16_t max = PIT_MAX_HZ / hz;
      x86_pit_set2(channel, mode, max);
      return true;
  }