00001 #include "devices/pit.h" 00002 //106675408 00003 #include <debug.h> 00004 #include <stdint.h> 00005 #include "threads/interrupt.h" 00006 #include "threads/io.h" 00007 00008 /* Interface to 8254 Programmable Interrupt Timer (PIT). 00009 Refer to [8254] for details. */ 00010 00011 /* 8254 registers. */ 00012 #define PIT_PORT_CONTROL 0x43 /* Control port. */ 00013 #define PIT_PORT_COUNTER(CHANNEL) (0x40 + (CHANNEL)) /* Counter port. */ 00014 00015 /* PIT cycles per second. */ 00016 #define PIT_HZ 1193180 00017 00018 /* Configure the given CHANNEL in the PIT. In a PC, the PIT's 00019 three output channels are hooked up like this: 00020 00021 - Channel 0 is connected to interrupt line 0, so that it can 00022 be used as a periodic timer interrupt, as implemented in 00023 Pintos in devices/timer.c. 00024 00025 - Channel 1 is used for dynamic RAM refresh (in older PCs). 00026 No good can come of messing with this. 00027 00028 - Channel 2 is connected to the PC speaker, so that it can 00029 be used to play a tone, as implemented in Pintos in 00030 devices/speaker.c. 00031 00032 MODE specifies the form of output: 00033 00034 - Mode 2 is a periodic pulse: the channel's output is 1 for 00035 most of the period, but drops to 0 briefly toward the end 00036 of the period. This is useful for hooking up to an 00037 interrupt controller to generate a periodic interrupt. 00038 00039 - Mode 3 is a square wave: for the first half of the period 00040 it is 1, for the second half it is 0. This is useful for 00041 generating a tone on a speaker. 00042 00043 - Other modes are less useful. 00044 00045 FREQUENCY is the number of periods per second, in Hz. */ 00046 void 00047 pit_configure_channel (int channel, int mode, int frequency) 00048 { 00049 uint16_t count; 00050 enum intr_level old_level; 00051 00052 ASSERT (channel == 0 || channel == 2); 00053 ASSERT (mode == 2 || mode == 3); 00054 00055 /* Convert FREQUENCY to a PIT counter value. The PIT has a 00056 clock that runs at PIT_HZ cycles per second. We must 00057 translate FREQUENCY into a number of these cycles. */ 00058 if (frequency < 19) 00059 { 00060 /* Frequency is too low: the quotient would overflow the 00061 16-bit counter. Force it to 0, which the PIT treats as 00062 65536, the highest possible count. This yields a 18.2 00063 Hz timer, approximately. */ 00064 count = 0; 00065 } 00066 else if (frequency > PIT_HZ) 00067 { 00068 /* Frequency is too high: the quotient would underflow to 00069 0, which the PIT would interpret as 65536. A count of 1 00070 is illegal in mode 2, so we force it to 2, which yields 00071 a 596.590 kHz timer, approximately. (This timer rate is 00072 probably too fast to be useful anyhow.) */ 00073 count = 2; 00074 } 00075 else 00076 count = (PIT_HZ + frequency / 2) / frequency; 00077 00078 /* Configure the PIT mode and load its counters. */ 00079 old_level = intr_disable (); 00080 outb (PIT_PORT_CONTROL, (channel << 6) | 0x30 | (mode << 1)); 00081 outb (PIT_PORT_COUNTER (channel), count); 00082 outb (PIT_PORT_COUNTER (channel), count >> 8); 00083 intr_set_level (old_level); 00084 }