akkera 102
/
apuplay
SPC music playback tools for real snes apu
Diff: gpio.cpp
- Revision:
- 2:62e6e22f8be2
- Parent:
- 0:5bd52e196edb
--- a/gpio.cpp Mon Jan 09 13:58:58 2017 +0000 +++ b/gpio.cpp Wed Jan 11 16:00:29 2017 +0000 @@ -1,13 +1,7 @@ -/** - * @file gpio.cpp - * @brief GPIO クラスの動作の定義を行います - */ - -#include "stdafx.h" #include "gpio.h" -// #include <wiringPi.h> #include "mbed.h" +// mbed LPC1768 DigitalInOut d[] = { p5, // A7 p6, // A6 @@ -28,26 +22,99 @@ p19, // ICL }; +static void gpio_set_pin(int pin, bool high); +static void gpio_set_dir(int pin, bool write); +static unsigned char gpio_swap_bits(unsigned char c); -/*! 唯一のインスタンスです */ -CGpio CGpio::sm_instance; +void gpio_init(void) +{ + gpio_set_pin(GPIO_A0, false); + gpio_set_pin(GPIO_A1, false); + gpio_set_pin(GPIO_A6, true); + gpio_set_pin(GPIO_A7, false); + gpio_set_pin(GPIO_WR, true); + gpio_set_pin(GPIO_RD, true); + gpio_set_pin(GPIO_ICL, true); + + gpio_set_dir(GPIO_A0, true); + gpio_set_dir(GPIO_A1, true); + gpio_set_dir(GPIO_A6, true); + gpio_set_dir(GPIO_A7, true); + gpio_set_dir(GPIO_WR, true); + gpio_set_dir(GPIO_RD, true); + gpio_set_dir(GPIO_ICL, true); -/** - * 初期化 - */ -void CGpio::Initialize() + gpio_set_pin(GPIO_A0, false); + gpio_set_pin(GPIO_A1, false); + gpio_set_pin(GPIO_A6, true); + gpio_set_pin(GPIO_A7, false); + gpio_set_pin(GPIO_WR, true); + gpio_set_pin(GPIO_RD, true); + gpio_set_pin(GPIO_ICL, true); +} + +void gpio_reset(void) { -// ::wiringPiSetup(); + gpio_set_pin(GPIO_A0, false); + gpio_set_pin(GPIO_A1, false); + gpio_set_pin(GPIO_A6, true); + gpio_set_pin(GPIO_A7, false); + gpio_set_pin(GPIO_WR, true); + gpio_set_pin(GPIO_RD, true); + gpio_set_pin(GPIO_ICL, false); + wait_us(50 * 1000); + + gpio_set_pin(GPIO_ICL, true); + wait_us(50 * 1000); } -/** - * I/Oの方向を設定する - * @param[in] pin ピン番号 - * @param[in] write 方向 - */ -void CGpio::PinDir(int pin, bool write) +void gpio_write(int address, unsigned char data) +{ + gpio_set_pin(GPIO_A0, ((address & 1) != 0)); + gpio_set_pin(GPIO_A1, ((address & 2) != 0)); + gpio_set_pin(GPIO_RD, true); + gpio_set_pin(GPIO_WR, false); + + for(int i=6; i<6+8; i++) + { + d[i].output(); + d[i] = data & 0x1; + data >>= 1; + } + wait_us(1); + + gpio_set_pin(GPIO_WR, true); +} + +unsigned char gpio_read(int address) { -// ::pinMode(pin, (write ? OUTPUT : INPUT)); + gpio_set_pin(GPIO_A0, ((address & 1) != 0)); + gpio_set_pin(GPIO_A1, ((address & 2) != 0)); + gpio_set_pin(GPIO_WR, true); + gpio_set_pin(GPIO_RD, false); + + unsigned char ret = 0; + + wait_us(1); + for(int i=6; i<6+8; i++) + { + d[i].input(); + ret <<= 1; + ret |= d[i]; + } + + gpio_set_pin(GPIO_RD, true); + + return gpio_swap_bits(ret); +} + +static void gpio_set_pin(int pin, bool high) +{ + d[pin] = high ? 1 : 0; +} + +static void gpio_set_dir(int pin, bool write) +{ if(write) { d[pin].output(); @@ -55,138 +122,21 @@ else { d[pin].input(); - } -} - -/** - * Output - * @param[in] pin ピン番号 - * @param[in] high ON/OFF - */ -void CGpio::SetPin(int pin, bool high) -{ -// ::digitalWrite(pin, (high ? HIGH : LOW)); - d[pin] = high ? 1 : 0; -} - -/** - * Input - * @param[in] pin ピン番号 - * @return ON/OFF - */ -bool CGpio::GetPin(int pin) -{ -// return (::digitalRead(pin) != LOW); - d[pin].input(); - return d[pin] != 0; -} - -/** - * コンストラクタ - */ -CGpio::CGpio() - : m_dir(kUninitialize) -{ -} - -/** - * バイト書き込み - * @param[in] c データ - */ -void CGpio::WriteByte(unsigned char c) -{ -// ByteMode(kWrite); -// ::digitalWriteByte(c); - int i; - - for(i=6; i<6+8; i++) - { - d[i].output(); - d[i] = c & 0x1; - c >>= 1; } } -/** - * バイト読み込み - * @return データ - */ -unsigned char CGpio::ReadByte() +static unsigned char gpio_swap_bits(unsigned char c) { -// ByteMode(kRead); -// return SwapBits(::digitalReadByte()); - - unsigned char ret = 0; - int i; - - for(i=6; i<6+8; i++) - { - d[i].input(); - ret <<= 1; - ret |= d[i]; - } - - return SwapBits(ret); -} - -/** - * バイト モード - * @param[in] dir I/O - */ - /* -void CGpio::ByteMode(ByteDir dir) -{ - if (m_dir != dir) - { - m_dir = dir; - const int mode = (dir != kRead) ? OUTPUT : INPUT; - for (int i = 0; i < 8; i++) - { - ::pinMode(i, mode); - } - } -} -*/ + unsigned char r = 0; -/** - * ビット スワップ - * @param[in] c データ - * @return データ - */ -unsigned char CGpio::SwapBits(unsigned char c) -{ - unsigned char s = 0; - if (c & 0x01) - { - s |= 0x80; - } - if (c & 0x02) - { - s |= 0x40; - } - if (c & 0x04) - { - s |= 0x20; - } - if (c & 0x08) - { - s |= 0x10; - } - if (c & 0x10) - { - s |= 0x08; - } - if (c & 0x20) - { - s |= 0x04; - } - if (c & 0x40) - { - s |= 0x02; - } - if (c & 0x80) - { - s |= 0x01; - } - return s; + if(c & 0x01) r |= 0x80; + if(c & 0x02) r |= 0x40; + if(c & 0x04) r |= 0x20; + if(c & 0x08) r |= 0x10; + if(c & 0x10) r |= 0x08; + if(c & 0x20) r |= 0x04; + if(c & 0x40) r |= 0x02; + if(c & 0x80) r |= 0x01; + + return r; }