My Project
Loading...
Searching...
No Matches
pit.c
Go to the documentation of this file.
1#include <stdint.h>
3#include "../../includes/mh.h"
4#include "../../includes/me.h"
5
6/* PIT constants */
7#define PIT_FREQ_HZ 1193182U
8#define PIT_CMD_PORT 0x43
9#define PIT_CH0_PORT 0x40
10
11/* Command bytes:
12 - 0x34 = channel 0, lobyte/hibyte, mode 2 (rate generator), binary
13 - 0x00 = latch command for channel 0 (bits 7..6 = 00, rest 0 = latch)
14*/
15#define PIT_CMD_MODE2_LBHB 0x34
16#define PIT_CMD_LATCH_CH0 0x00
17
18/* Sleep ms implementation (blocking). Uses chunks <= 0xFFFF PIT ticks. */
19void pit_sleep_ms(uint32_t ms) {
20 if (ms == 0) return;
21
22 uint64_t total_ticks = ((uint64_t)PIT_FREQ_HZ * ms + 999) / 1000;
23
24 while (total_ticks > 0) {
25 uint32_t chunk = (total_ticks > 0xFFFF) ? 0xFFFF : (uint32_t)total_ticks;
26 if (chunk == 0) chunk = 0xFFFF;
27
29 __outbyte(PIT_CH0_PORT, (uint8_t)(chunk & 0xFF));
30 __outbyte(PIT_CH0_PORT, (uint8_t)((chunk >> 8) & 0xFF));
31
32 bool Enabled = MeDisableInterrupts();
33
35 // Ensure proper sequencing of port reads
36 uint8_t start_lo = __inbyte(PIT_CH0_PORT);
37 asm volatile("" ::: "memory"); // compiler barrier
38 uint8_t start_hi = __inbyte(PIT_CH0_PORT);
39 uint16_t start = start_lo | ((uint16_t)start_hi << 8);
40
41 while (1) {
43 uint8_t curr_lo = __inbyte(PIT_CH0_PORT);
44 asm volatile("" ::: "memory"); // compiler barrier
45 uint8_t curr_hi = __inbyte(PIT_CH0_PORT);
46 uint16_t curr = curr_lo | ((uint16_t)curr_hi << 8);
47
48 uint16_t elapsed = (uint16_t)(start - curr);
49 if ((uint32_t)elapsed >= chunk) break;
50
51 asm volatile("pause");
52 }
53
54 MeEnableInterrupts(Enabled);
55 total_ticks -= chunk;
56 }
57}
FORCEINLINE void __outbyte(unsigned short port, unsigned char val)
Definition intrin.h:179
FORCEINLINE unsigned char __inbyte(unsigned short port)
Definition intrin.h:172
bool MeDisableInterrupts(void)
Definition irql.c:186
void MeEnableInterrupts(IN bool EnabledBefore)
Definition irql.c:199
#define PIT_CMD_LATCH_CH0
Definition pit.c:16
#define PIT_CMD_MODE2_LBHB
Definition pit.c:15
#define PIT_CH0_PORT
Definition pit.c:9
#define PIT_FREQ_HZ
Definition pit.c:7
void pit_sleep_ms(uint32_t ms)
Definition pit.c:19
#define PIT_CMD_PORT
Definition pit.c:8