]> git.sesse.net Git - pistorm/blob - gpio/ps_protocol.h
Update emulator.c, gpio_old.c, and gpio_old.h
[pistorm] / gpio / ps_protocol.h
1 /*
2     Code reorganized and rewritten by 
3     Niklas Ekström 2021 (https://github.com/niklasekstrom)
4 */
5
6 #ifndef _PS_PROTOCOL_H
7 #define _PS_PROTOCOL_H
8
9 #define PIN_TXN_IN_PROGRESS 0
10 #define PIN_IPL_ZERO 1
11 #define PIN_A0 2
12 #define PIN_A1 3
13 #define PIN_CLK 4
14 #define PIN_UNUSED 5
15 #define PIN_RD 6
16 #define PIN_WR 7
17 #define PIN_D(x) (8 + x)
18
19 #define REG_DATA 0
20 #define REG_ADDR_LO 1
21 #define REG_ADDR_HI 2
22 #define REG_STATUS 3
23
24 #define STATUS_BIT_INIT 1
25 #define STATUS_BIT_RESET 2
26
27 #define STATUS_MASK_IPL 0xe000
28 #define STATUS_SHIFT_IPL 13
29
30 //#define BCM2708_PERI_BASE 0x20000000  // pi0-1
31 //#define BCM2708_PERI_BASE     0xFE000000  // pi4
32 #define BCM2708_PERI_BASE 0x3F000000  // pi3
33 #define BCM2708_PERI_SIZE 0x01000000
34
35 #define GPIO_ADDR 0x200000 /* GPIO controller */
36 #define GPCLK_ADDR 0x101000
37
38 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
39 #define GPCLK_BASE (BCM2708_PERI_BASE + 0x101000)
40
41 #define CLK_PASSWD 0x5a000000
42 #define CLK_GP0_CTL 0x070
43 #define CLK_GP0_DIV 0x074
44
45 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or
46 // SET_GPIO_ALT(x,y)
47 #define INP_GPIO(g) *(gpio + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
48 #define OUT_GPIO(g) *(gpio + ((g) / 10)) |= (1 << (((g) % 10) * 3))
49 #define SET_GPIO_ALT(g, a)  \
50   *(gpio + (((g) / 10))) |= \
51       (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g) % 10) * 3))
52
53 #define GPIO_PULL *(gpio + 37)      // Pull up/pull down
54 #define GPIO_PULLCLK0 *(gpio + 38)  // Pull up/pull down clock
55
56 #define GPFSEL0_INPUT 0x0024c240
57 #define GPFSEL1_INPUT 0x00000000
58 #define GPFSEL2_INPUT 0x00000000
59
60 #define GPFSEL0_OUTPUT 0x0924c240
61 #define GPFSEL1_OUTPUT 0x09249249
62 #define GPFSEL2_OUTPUT 0x00000249
63
64 unsigned int ps_read_8(unsigned int address);
65 unsigned int ps_read_16(unsigned int address);
66 unsigned int ps_read_32(unsigned int address);
67
68 void ps_write_8(unsigned int address, unsigned int data);
69 void ps_write_16(unsigned int address, unsigned int data);
70 void ps_write_32(unsigned int address, unsigned int data);
71
72 unsigned int ps_read_status_reg();
73 void ps_write_status_reg(unsigned int value);
74
75 void ps_setup_protocol();
76 void ps_reset_state_machine();
77 void ps_pulse_reset();
78
79 unsigned int ps_get_ipl_zero();
80
81 #define read8 ps_read_8
82 #define read16 ps_read_16
83 #define read32 ps_read_32
84
85 #define write8 ps_write_8
86 #define write16 ps_write_16
87 #define write32 ps_write_32
88
89 #define write_reg ps_write_status_reg
90 #define read_reg ps_read_status_reg
91
92 #define gpio_get_irq ps_get_ipl_zero
93
94 #endif /* _PS_PROTOCOL_H */