-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_spi_gamepad.v
74 lines (55 loc) · 1.68 KB
/
esp32_spi_gamepad.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// esp32_spi_gamepad.v
//
// Copyright (C) 2020 Dan Rodrigues <[email protected]>
//
// SPDX-License-Identifier: MIT
`default_nettype none
module esp32_spi_gamepad #(
parameter integer PAD_BUTTONS = 12
) (
input clk,
input reset,
// ESP32 reset support
input user_reset,
output esp32_en,
output esp32_gpio0,
output esp32_gpio12,
// SPI gamepad input
input spi_csn,
input spi_clk,
input spi_mosi,
output reg [PAD_BUTTONS - 1:0] pad_btn
);
localparam PAD_WIDTH = PAD_BUTTONS - 1;
// --- ESP32 resetting ---
assign esp32_en = !user_reset;
// ESP32 "strapping pins" for boot:
// Serial console shows ESP32 stuck in reset loop
// MTDI (GPIO12) apparently has pull down in ESP32 but still need this for whatever reason
// GPIO0 = 1 (SPI flash boot, instead of serial bootloader)
assign esp32_gpio0 = 1;
// MTDI = 0 (VDD_SDIO = 3.3v, instead of 1.8v)
assign esp32_gpio12 = 0;
// --- SPI gamepad ---
reg spi_clk_r;
reg spi_csn_r;
reg [PAD_WIDTH:0] receive_buffer;
wire spi_csn_rose = spi_csn && !spi_csn_r;
wire spi_csn_fell = !spi_csn && spi_csn_r;
wire spi_clk_rose = spi_clk && !spi_clk_r;
always @(posedge clk) begin
if (reset) begin
pad_btn <= 0;
spi_clk_r <= 0;
spi_csn_r <= 1;
end else begin
spi_clk_r <= spi_clk;
spi_csn_r <= spi_csn;
if (!spi_csn && spi_clk_rose) begin
receive_buffer <= {spi_mosi, receive_buffer[PAD_WIDTH:1]};
end else if (spi_csn_rose) begin
pad_btn <= receive_buffer;
end
end
end
endmodule