forked from portapack-mayhem/mayhem-firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathportapack_io.cpp
More file actions
153 lines (130 loc) · 4.01 KB
/
Copy pathportapack_io.cpp
File metadata and controls
153 lines (130 loc) · 4.01 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* copyleft 2024 zxkmm
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "portapack_io.hpp"
#include "portapack_persistent_memory.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
#include "utility.hpp"
#include <ch.h>
#include <cstdint>
namespace portapack {
DeviceType device_type = DEV_PORTAPACK;
void IO::lcd_read_bytes(uint8_t* byte, size_t byte_count) {
if (portapack::device_type == portapack::DeviceType::DEV_PORTAPACK) {
size_t word_count = byte_count / 2;
while (word_count) {
const auto word = lcd_read_data();
*(byte++) = word >> 8;
*(byte++) = word >> 0;
word_count--;
}
if (byte_count & 1) {
const auto word = lcd_read_data();
*(byte++) = word >> 8;
}
return;
}
// prf
//--dummy read:
dir_read();
lcd_rd_assert();
halPolledDelay(71);
data_read();
lcd_rd_deassert();
size_t word_count = byte_count / 3;
for (size_t i = 0; i < word_count; i++) {
uint32_t word = lcd_read_data(); // reads 3 byte of data
uint8_t r = ((word >> 16) & 0xff);
uint8_t g = ((word >> 8) & 0xff);
uint8_t b = ((word >> 0) & 0xff);
*(byte++) = r;
*(byte++) = g << 2;
*(byte++) = b;
}
}
void IO::init() {
data_mask_set();
data_write_high(0);
dir_read();
lcd_rd_deassert();
lcd_wr_deassert();
io_stb_deassert();
addr(0);
gpio_dir.output();
gpio_lcd_rdx.output();
gpio_io_stbx.output();
gpio_addr.output();
gpio_rot_a.input();
}
void IO::lcd_backlight(const bool value) {
io_reg = (io_reg & 0x7f) | ((value ? 1 : 0) << 7);
io_write(1, io_reg);
}
void IO::lcd_reset_state(const bool active) {
io_reg = (io_reg & 0xfe) | ((active ? 1 : 0) << 0);
io_write(1, io_reg);
}
void IO::audio_reset_state(const bool active) {
/* NOTE: This overwrites the contents of the IO register, which for now
* have no significance. But someday...?
*/
io_reg = (io_reg & 0xfd) | ((active ? 1 : 0) << 1);
io_write(1, io_reg);
}
void IO::reference_oscillator(const bool enable) {
const uint8_t mask = 1 << 6;
io_reg = (io_reg & ~mask) | (enable ? mask : 0);
io_write(1, io_reg);
}
uint32_t IO::io_update(const TouchPinsConfig write_value) {
/* Very touchy code to save context of PortaPack data bus while the
* resistive touch pin drive is changed. Order of operations is
* important to prevent latching spurious data into the LCD or IO
* registers.
*/
const auto save_data = data_read();
const auto addr = gpio_addr.read();
const auto dir = gpio_dir.read();
io_stb_assert();
/* Switch to read */
dir_read();
addr_0();
__asm__("nop");
__asm__("nop");
__asm__("nop");
const auto switches_raw = data_read();
/* Switch to write */
data_write_low(toUType(write_value));
dir_write();
__asm__("nop");
__asm__("nop");
__asm__("nop");
io_stb_deassert();
data_write_low(save_data);
if (dir) { /* 0 (write) -> 1 (read) */
dir_read();
}
gpio_addr.write(addr);
uint32_t dfu_btn = gpio_control::dfu_button.read();
return (switches_raw & 0x7f) | (dfu_btn << 7);
}
} // namespace portapack