Ditto for HAL
[reactos.git] / reactos / hal / halx86 / generic / beep.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/hal/x86/beep.c
5 * PURPOSE: Speaker function (it's only one)
6 * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de)
7 * UPDATE HISTORY:
8 * Created 31/01/99
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <hal.h>
14 #define NDEBUG
15 #include <debug.h>
16
17
18 /* CONSTANTS *****************************************************************/
19
20 #define TIMER2 0x42
21 #define TIMER3 0x43
22 #define PORT_B 0x61
23 #define CLOCKFREQ 1193167
24
25
26 /* FUNCTIONS *****************************************************************/
27 /*
28 * FUNCTION: Beeps the speaker.
29 * ARGUMENTS:
30 * Frequency = If 0, the speaker will be switched off, otherwise
31 * the speaker beeps with the specified frequency.
32 */
33
34 BOOLEAN
35 STDCALL
36 HalMakeBeep (
37 ULONG Frequency
38 )
39 {
40 UCHAR b;
41 ULONG flags;
42
43 /* save flags and disable interrupts */
44 Ki386SaveFlags(flags);
45 Ki386DisableInterrupts();
46
47 /* speaker off */
48 b = READ_PORT_UCHAR((PUCHAR)PORT_B);
49 WRITE_PORT_UCHAR((PUCHAR)PORT_B, (UCHAR)(b & 0xFC));
50
51 if (Frequency)
52 {
53 DWORD Divider = CLOCKFREQ / Frequency;
54
55 if (Divider > 0x10000)
56 {
57 /* restore flags */
58 Ki386RestoreFlags(flags);
59
60 return FALSE;
61 }
62
63 /* set timer divider */
64 WRITE_PORT_UCHAR((PUCHAR)TIMER3, 0xB6);
65 WRITE_PORT_UCHAR((PUCHAR)TIMER2, (UCHAR)(Divider & 0xFF));
66 WRITE_PORT_UCHAR((PUCHAR)TIMER2, (UCHAR)((Divider>>8) & 0xFF));
67
68 /* speaker on */
69 WRITE_PORT_UCHAR((PUCHAR)PORT_B, (UCHAR)(READ_PORT_UCHAR((PUCHAR)PORT_B) | 0x03));
70 }
71
72 /* restore flags */
73 Ki386RestoreFlags(flags);
74
75 return TRUE;
76 }
77