helos1/driver/irq/pic/ps2/internal.h

65 lines
1.8 KiB
C
Raw Normal View History

2021-10-10 14:39:17 +08:00
#pragma once
#include "../../../../main.h"
#include "ps2.h"
#include "../internal.h"
#ifdef __cplusplus
extern "C" {
#endif
2021-11-04 22:31:28 +08:00
SYSV_ABI void pic_ps2_IRQHandlerK(); // keyboard IRQ1
SYSV_ABI void pic_ps2_IRQHandlerM(); // mouse IRQ12
2021-10-10 14:39:17 +08:00
// waits until the Output Buffer Status (bit 0) of the Status Register is set
// and port 0x60 is ready for data read
static inline void __ps2_PollWait_ReadReady() {
2021-11-04 22:31:28 +08:00
while ((inb(PIC_PS2_STATUSPORT) & PIC_PS2_STATUS_OUTPUT_BUFFER) == 0)
2021-10-10 14:39:17 +08:00
asm("pause");
}
// waits until the Input Buffer Status (bit 1) of the Status Register is clear
// and ports 0x60/0x64 are ready for data write
static inline void __ps2_PollWait_WriteReady() {
2021-11-04 22:31:28 +08:00
while ((inb(PIC_PS2_STATUSPORT) & PIC_PS2_STATUS_INPUT_BUFFER) != 0)
2021-10-10 14:39:17 +08:00
asm("pause");
}
// waits until port 0x60 is ready and reads the byte in it
static inline uint8_t __ps2_ReadData() {
__ps2_PollWait_ReadReady();
2021-11-04 22:31:28 +08:00
return inb(PIC_PS2_IOPORT);
2021-10-10 14:39:17 +08:00
}
// waits until port 0x64 is ready for write, OUTB to it.
static inline void __ps2_WriteCommand(uint8_t cmd) {
__ps2_PollWait_WriteReady();
2021-11-04 22:31:28 +08:00
outb(PIC_PS2_CMDPORT, cmd);
2021-10-10 14:39:17 +08:00
}
// waits until port 0x64 is ready, OUTB to it, OUTB to 0x60 for data byte.
static inline void __ps2_WriteCommandData(uint8_t cmd, uint8_t data) {
__ps2_PollWait_WriteReady();
2021-11-04 22:31:28 +08:00
outb(PIC_PS2_CMDPORT, cmd);
2021-10-10 14:39:17 +08:00
__ps2_PollWait_WriteReady();
2021-11-04 22:31:28 +08:00
outb(PIC_PS2_IOPORT, data);
2021-10-10 14:39:17 +08:00
}
static inline void __ps2_ReadACK() {
2021-11-04 22:31:28 +08:00
while (__ps2_ReadData() != PIC_PS2_ACK) {}
2021-10-10 14:39:17 +08:00
}
// sets the sample rate of the mouse
static inline void __ps2_SetMouseRate(uint8_t rate) {
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_SEND_MOUSE, 0xf3); // command to the mouse
2021-10-10 14:39:17 +08:00
__ps2_ReadACK(); // read the ACK
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_SEND_MOUSE, rate); // send the rate
2021-10-10 14:39:17 +08:00
__ps2_ReadACK(); // read the ACK
}
#ifdef __cplusplus
}
#endif