mayhem-firmware/firmware/common/portapack_io.hpp

383 lines
8.6 KiB
C++
Raw Normal View History

2015-07-08 15:39:24 +00:00
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* 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.
*/
#ifndef __PORTAPACK_IO_H__
#define __PORTAPACK_IO_H__
#include <cstdint>
#include <cstddef>
#include <array>
#include "gpio.hpp"
#include "ui.hpp"
namespace portapack {
class IO {
public:
enum class TouchPinsConfig : uint8_t {
XN_BIT = (1 << 0),
XP_BIT = (1 << 1),
YN_BIT = (1 << 2),
YP_BIT = (1 << 3),
XN_OE = (1 << 4),
XP_OE = (1 << 5),
YN_OE = (1 << 6),
YP_OE = (1 << 7),
XN_IN = XN_BIT,
XN_OUT_1 = XN_OE | XN_BIT,
XN_OUT_0 = XN_OE,
XP_IN = XP_BIT,
XP_OUT_1 = XP_OE | XP_BIT,
XP_OUT_0 = XP_OE,
YN_IN = YN_BIT,
YN_OUT_1 = YN_OE | YN_BIT,
YN_OUT_0 = YN_OE,
YP_IN = YP_BIT,
YP_OUT_1 = YP_OE | YP_BIT,
YP_OUT_0 = YP_OE,
/* Allow pins to be pulled up by CPLD pull-ups. */
Float = XP_IN | XN_IN | YP_IN | YN_IN,
/* Drive one plane to 0V, other plane is pulled up. Watch for when pulled-up
* plane falls to ~0V.
*/
WaitTouch = XP_OUT_0 | XN_OUT_0 | YP_IN | YN_IN,
/* Create a voltage divider between X plane, touch resistance, Y plane. */
SensePressure = XP_IN | XN_OUT_0 | YP_OUT_1 | YN_IN,
/* Create a voltage divider across X plane, read voltage from Y plane. */
SenseX = XP_OUT_1 | XN_OUT_0 | YP_IN | YN_IN,
/* Create a voltage divider across Y plane, read voltage from X plane. */
SenseY = XP_IN | XN_IN | YP_OUT_1 | YN_OUT_0,
};
constexpr IO(
GPIO gpio_dir,
GPIO gpio_lcd_rdx,
GPIO gpio_lcd_wrx,
2015-07-08 15:39:24 +00:00
GPIO gpio_io_stbx,
GPIO gpio_addr,
GPIO gpio_rot_a,
GPIO gpio_rot_b
) : gpio_dir { gpio_dir },
gpio_lcd_rdx { gpio_lcd_rdx },
gpio_lcd_wrx { gpio_lcd_wrx },
2015-07-08 15:39:24 +00:00
gpio_io_stbx { gpio_io_stbx },
gpio_addr { gpio_addr },
gpio_rot_a { gpio_rot_a },
gpio_rot_b { gpio_rot_b }
{
};
void init();
void lcd_backlight(const bool value);
void lcd_reset_state(const bool active);
void audio_reset_state(const bool active);
Upstream merge to make new revision of PortaPack work (#206) * Power: Turn off additional peripheral clock branches. * Update schematic with new symbol table and KiCad standard symbols. Fix up wires. * Schematic: Update power net labels. * Schematic: Update footprint names to match library changes. * Schematic: Update header vendor and part numbers. * Schematic: Specify (arbitrary) value for PDN# net. * Schematic: Remove fourth fiducial. Not standard practice, and was taking up valuable board space. * Schematic: Add reference oscillator -- options for clipped sine or HCMOS output. * Schematic: Update copyright year. * Schematic: Remove CLKOUT to CPLD. It was a half-baked idea. * Schematic: Add (experimental) GPS circuit. Add note about charging circuit. Update date and revision to match PCB. * PCB: Update from schematic change: now revision 20180819. Diff was extensive due to net renumbering... * PCB: Fix GPS courtyard to accommodate crazy solder paste recommendation in integration manual. PCB: Address DRC clearance violation between via and oscillator pad. * PCB: Update copyright on drawing. * Update schematic and PCB date and revision. * gitignore: Sublime Text editor project/workspace files * Power: Power up or power down peripheral clock at appropriate times, so firmware doesn't freeze... * Clocking: Fix incorrect shift for CGU IDIVx_CTRL.PD field. * LPC43xx: Add CGU IDIVx struct/union type. * Power: Switch off unused IDIV dividers. Make note of active IDIVs and their use. * HackRF Mode: Upgrade firmware to 2018.01.1 (API 1.02) * MAX V CPLD: Refactor class to look more like Xilinx CoolRunner II CPLD class. * MAX V CPLD: Add BYPASS, SAMPLE support. Rename enter_isp -> enable, exit_isp -> disable. Use SAMPLE at start of flash process, which somehow addresses the problem where CFM wouldn't load into SRAM (and become the active bitstream) after flashing. * MAX V CPLD: Reverse verify data checking logic to make it a little faster. * CPLD: After reprogramming flash, immediately clamp I/O signals, load to SRAM, and "execute" the new bitstream. * Si5351: Refactor code, make one of the registers more type-safe. Clock Manager: Track selected reference clock source for later use in user interface. * Clock Manager: Add note about PPM only affecting Si5351C PLLA, which always runs from the HackRF 25MHz crystal. It is assumed an external clock does not need adjustment, though I am open to being convinced otherwise... * PPM UI: Show "EXT" when showing PPM adjustment and reference clock is external. * CPLD: Add pins and logic for new PortaPack hardware feature(s). * CPLD: Bitstream to support new hardware features. * Clock Generator: Add a couple more setter methods for ClockControl registers. * Clock Manager: Use shared MCU CLKIN clock control configuration constant. * Clock Manager: Reduce MCU CLKIN driver current. 2mA should be plenty. * Clock Manager: Remove redundant clock generator output enable. * Bootstrap: Remove unnecessary ldscript hack to locate SPIFI mode change code in RAM. * Bootstrap: Get CPU operating at max frequency as soon as possible. Update SPIFI speed comment. Make some more LPC43xx types into unions with uint32_t. * Bootstrap: Explicitly configure IDIVB for SPIFI, despite LPC43xx bootloader setting it. * Clock Manager: Init peripherals before CPLD reconfig. Do the clock generator setup after, so we can check presence of PortaPack reference clock with the help of the latest CPLD bitstream. * Clock Manager: Reverse sense of conditional that determines crystal or non-crystal reference source. This is for an expected upcoming change where multiple external options can be differentiated. * Bootstrap: Consolidate clock configuration, update SPIFI rate comment. * Clock Manager: Use IDIVA for clock source for all peripherals, instead of PLL1. Should make switching easier going forward. Don't use IRC as clock during initial clock manager configuration. Until we switch to GP_CLKIN, we should go flat out... * ChibiOS M0: Change default clock speed to 204MHz, since bootstrap now maxes out clock speed before starting M0 execution. * PortaPack IO: Expose method to set reference oscillator enable pin. * Pin configuration: Do SPIFI pin config with other pins, in preparation for eliminating separate bootloader. * Pin configuration: Disable input buffers on pins that are never read. * Revert "ChibiOS M0: Change default clock speed to 204MHz, since bootstrap now maxes out clock speed before starting M0 execution." This reverts commit c0e2bb6cc4cc656769323bdbb8ee5a16d2d5bb03. * Remove unused board files. * Add LPC43xx functions. * chibios: Replace code with per-peripheral structs defining clocks, interrupts, and reset bits. * LPC43xx: Add MCPWM peripheral struct. * clock generator: Use recommended PLL reset register value. Datasheet recommends a value. AN619 is quiet on the topic, claims the low nibble is default 0b0000. * GPIO: Tweak masking of SCU function. I don't remember why I thought this was necessary... * HAL: Explicitly turn on timer peripheral clocks used as systicks, during init. * SCU: Add struct to hold pin configuration. * PAL: Add functions to address The Glitch. https://greatscottgadgets.com/2018/02-28-we-fixed-the-glitch/ * PAL/board: New IO initialization code Declare initial state for SCU pin config, GPIOs. Apply initial state during PAL init. Perform VAA slow turn-on to address The Glitch. * Merge M0 and M4 to eliminate need for bootstrap firmware During _early_init, detect if we're running on the M4 or M0. If M4: do M4-specific core initialization, reset peripherals, speed up SPIFI clock, start M0, go to sleep. If M0: do all the other things. * Pins: Miscellaneous SCU configuration tweaks. * Little code clarity improvement. * bootstrap: Remove, not necessary. * Clock Manager: Large re-working to support external references. * Fix merge conflicts
2019-01-11 06:56:21 +00:00
void reference_oscillator(const bool enable);
2015-07-08 15:39:24 +00:00
void lcd_data_write_command_and_data(
const uint_fast8_t command,
const uint8_t* data,
const size_t data_count
) {
lcd_command(command);
for(size_t i=0; i<data_count; i++) {
lcd_write_data(data[i]);
}
}
2015-07-08 15:39:24 +00:00
void lcd_data_write_command_and_data(
const uint_fast8_t command,
const std::initializer_list<uint8_t>& data
) {
lcd_command(command);
for(const auto d : data) {
lcd_write_data(d);
2015-07-08 15:39:24 +00:00
}
}
void lcd_data_read_command_and_data(
const uint_fast8_t command,
uint16_t* const data,
const size_t data_count
) {
lcd_command(command);
for(size_t i=0; i<data_count; i++) {
data[i] = lcd_read_data();
2015-07-08 15:39:24 +00:00
}
}
void lcd_write_word(const uint32_t w) {
lcd_write_data(w);
2015-07-08 15:39:24 +00:00
}
void lcd_write_words(const uint16_t* const w, size_t n) {
for(size_t i=0; i<n; i++) {
lcd_write_data(w[i]);
2015-07-08 15:39:24 +00:00
}
}
void lcd_write_pixel(const ui::Color pixel) {
lcd_write_data(pixel.v);
2015-07-08 15:39:24 +00:00
}
uint32_t lcd_read_word() {
return lcd_read_data();
2015-07-08 15:39:24 +00:00
}
2015-07-08 15:39:24 +00:00
void lcd_write_pixels(const ui::Color pixel, size_t n) {
while(n--) {
lcd_write_data(pixel.v);
2015-07-08 15:39:24 +00:00
}
}
void lcd_write_pixels_unrolled8(const ui::Color pixel, size_t n) {
auto v = pixel.v;
n >>= 3;
while(n--) {
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
lcd_write_data(v);
}
}
2015-07-08 15:39:24 +00:00
void lcd_write_pixels(const ui::Color* const pixels, size_t n) {
for(size_t i=0; i<n; i++) {
lcd_write_pixel(pixels[i]);
}
}
void lcd_read_bytes(uint8_t* byte, size_t byte_count) {
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;
}
}
2015-07-08 15:39:24 +00:00
uint32_t io_read() {
io_stb_assert();
dir_read();
addr_0();
__asm__("nop");
__asm__("nop");
__asm__("nop");
const auto switches_raw = data_read();
io_stb_deassert();
return switches_raw;
}
uint32_t io_update(const TouchPinsConfig write_value);
uint32_t lcd_te() {
return gpio_rot_a.read();
}
private:
const GPIO gpio_dir;
const GPIO gpio_lcd_rdx;
const GPIO gpio_lcd_wrx;
2015-07-08 15:39:24 +00:00
const GPIO gpio_io_stbx;
const GPIO gpio_addr;
const GPIO gpio_rot_a;
const GPIO gpio_rot_b;
static constexpr ioportid_t gpio_data_port_id = 3;
static constexpr size_t gpio_data_shift = 8;
static constexpr ioportmask_t gpio_data_mask = 0xffU << gpio_data_shift;
uint8_t io_reg { 0x03 };
2015-07-08 15:39:24 +00:00
void lcd_rd_assert() {
gpio_lcd_rdx.clear();
2015-07-08 15:39:24 +00:00
}
void lcd_rd_deassert() {
gpio_lcd_rdx.set();
2015-07-08 15:39:24 +00:00
}
void lcd_wr_assert() {
gpio_lcd_wrx.clear();
2015-07-08 15:39:24 +00:00
}
void lcd_wr_deassert() {
gpio_lcd_wrx.set();
2015-07-08 15:39:24 +00:00
}
void io_stb_assert() {
gpio_io_stbx.clear();
}
void io_stb_deassert() {
gpio_io_stbx.set();
}
void addr(const bool value) {
gpio_addr.write(value);
}
void addr_1() {
gpio_addr.set();
}
void addr_0() {
gpio_addr.clear();
}
void data_mask_set() {
LPC_GPIO->MASK[gpio_data_port_id] = ~gpio_data_mask;
}
void dir_write() {
gpio_dir.clear();
LPC_GPIO->DIR[gpio_data_port_id] |= gpio_data_mask;
/* TODO: Manipulating DIR[3] makes me queasy. The RFFC5072 DATA pin
* is also on port 3, and switches direction periodically...
* Time to resort to bit-banding to enforce atomicity? But then, how
* to change direction on eight bits efficiently? Or do I care, since
* the PortaPack data bus shouldn't change direction too frequently?
*/
}
void dir_read() {
LPC_GPIO->DIR[gpio_data_port_id] &= ~gpio_data_mask;
gpio_dir.set();
}
void data_write_low(const uint32_t value) {
LPC_GPIO->MPIN[gpio_data_port_id] = (value << gpio_data_shift);
}
void data_write_high(const uint32_t value) {
LPC_GPIO->MPIN[gpio_data_port_id] = value;
}
uint32_t data_read() {
return (LPC_GPIO->MPIN[gpio_data_port_id] >> gpio_data_shift) & 0xffU;
}
void lcd_command(const uint32_t value) {
data_write_high(0); /* Drive high byte (with zero -- don't care) */
dir_write(); /* Turn around data bus, MCU->CPLD */
addr(0); /* Indicate command */
__asm__("nop");
__asm__("nop");
__asm__("nop");
lcd_wr_assert(); /* Latch high byte */
data_write_low(value); /* Drive low byte (pass-through) */
__asm__("nop");
__asm__("nop");
__asm__("nop");
lcd_wr_deassert(); /* Complete write operation */
addr(1); /* Set up for data phase (most likely after a command) */
}
void lcd_write_data(const uint32_t value) __attribute__((always_inline)) {
2015-07-08 15:39:24 +00:00
// NOTE: Assumes and DIR=0 and ADDR=1 from command phase.
data_write_high(value); /* Drive high byte */
__asm__("nop");
lcd_wr_assert(); /* Latch high byte */
data_write_low(value); /* Drive low byte (pass-through) */
__asm__("nop");
__asm__("nop");
__asm__("nop");
lcd_wr_deassert(); /* Complete write operation */
}
uint32_t lcd_read_data() {
2015-07-08 15:39:24 +00:00
// NOTE: Assumes ADDR=1 from command phase.
dir_read();
/* Start read operation */
lcd_rd_assert();
/* Wait for passthrough data(15:8) to settle -- ~16ns (3 cycles) typical */
/* Wait for read control L duration (355ns) */
halPolledDelay(71); // 355ns
const auto value_high = data_read();
/* Latch data[7:0] */
lcd_rd_deassert();
/* Wait for latched data[7:0] to settle -- ~26ns (5 cycles) typical */
/* Wait for read control H duration (90ns) */
halPolledDelay(18); // 90ns
const auto value_low = data_read();
return (value_high << 8) | value_low;
}
void io_write(const bool address, const uint_fast16_t value) {
data_write_low(value);
dir_write();
addr(address);
__asm__("nop");
__asm__("nop");
__asm__("nop");
io_stb_assert();
__asm__("nop");
__asm__("nop");
__asm__("nop");
io_stb_deassert();
}
/*
void lcd_data_write_command_and_data(
const uint_fast16_t command,
const uint8_t* const data,
const size_t count
) {
lcd_data_write_command(command);
for(size_t i=0; i<count; i++) {
lcd_data_write_data(data[i]);
}
}
*/
};
extern IO io;
} /* namespace portapack */
#endif/*__PORTAPACK_IO_H__*/