Library to use Arduino USB host shield on mbed
ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。
Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です
シールドについて
3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください
接続例
使い方
Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。
仕様
- Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。
main.cpp
#include "mbed.h" #include <PS3BT.h> #include <usbhub.h> Serial pc(USBTX, USBRX, 115200); //Nucleo f303k8用 USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr BTD Btd(&Usb); PS3BT PS3(&Btd); int main() { bool printAngle = false; if (Usb.Init() == -1) { pc.printf("\r\nOSC did not start"); while (1); // Halt } pc.printf("\r\nPS3 USB Library Started"); while (1) { Usb.Task(); if (PS3.PS3Connected || PS3.PS3NavigationConnected) { if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX)); pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY)); if (PS3.PS3Connected) { // The Navigation controller only have one joystick pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX)); pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY)); } } // Analog button values can be read from almost all buttons if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2)); if (!PS3.PS3NavigationConnected) { pc.printf("\tR2: %d", PS3.getAnalogButton(R2)); } } if (PS3.getButtonClick(PS)) { PS3.disconnect(); pc.printf("\r\nPS"); } if (PS3.getButtonClick(TRIANGLE)) pc.printf("\r\nTriangle"); if (PS3.getButtonClick(CIRCLE)) pc.printf("\r\nCircle"); if (PS3.getButtonClick(CROSS)) pc.printf("\r\nCross"); if (PS3.getButtonClick(SQUARE)) pc.printf("\r\nSquare"); if (PS3.getButtonClick(UP)) { pc.printf("\r\nUp"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED4); } if (PS3.getButtonClick(RIGHT)) { pc.printf("\r\nRight"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED1); } if (PS3.getButtonClick(DOWN)) { pc.printf("\r\nDown"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED2); } if (PS3.getButtonClick(LEFT)) { pc.printf("\r\nLeft"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED3); } if (PS3.getButtonClick(L1)) pc.printf("\r\nL1"); if (PS3.getButtonClick(L3)) pc.printf("\r\nL3"); if (PS3.getButtonClick(R1)) pc.printf("\r\nR1"); if (PS3.getButtonClick(R3)) pc.printf("\r\nR3"); if (PS3.getButtonClick(SELECT)) { pc.printf("\r\nSelect - "); PS3.printStatusString(); } if (PS3.getButtonClick(START)) { pc.printf("\r\nStart"); printAngle = !printAngle; } if (printAngle) { pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch)); pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll)); } } else { pc.printf("not connect\n"); } } }
USB_Host/usbhost.cpp@1:da31140f2a1c, 2020-05-02 (annotated)
- Committer:
- robo_ichinoseki_a
- Date:
- Sat May 02 05:56:48 2020 +0000
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | #include "Usb.h" |
kotakku | 0:b1ce54272580 | 2 | #include "usbhost.h" |
kotakku | 0:b1ce54272580 | 3 | |
kotakku | 0:b1ce54272580 | 4 | //#define DEBUGMODE |
kotakku | 0:b1ce54272580 | 5 | #ifdef DEBUGMODE |
kotakku | 0:b1ce54272580 | 6 | #define DEBUG(x, ...) printf("[%s:%d]" x "\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); |
kotakku | 0:b1ce54272580 | 7 | #else |
kotakku | 0:b1ce54272580 | 8 | #define DEBUG(...) while (0); |
kotakku | 0:b1ce54272580 | 9 | #endif |
kotakku | 0:b1ce54272580 | 10 | |
kotakku | 0:b1ce54272580 | 11 | |
kotakku | 0:b1ce54272580 | 12 | |
kotakku | 0:b1ce54272580 | 13 | /* constructor */ |
kotakku | 0:b1ce54272580 | 14 | MAX3421E::MAX3421E(PinName mosi, PinName miso, PinName sclk, PinName ssel, PinName intr) : SPI(mosi, miso, sclk), _ss(ssel), _intr(intr) |
kotakku | 0:b1ce54272580 | 15 | { |
kotakku | 0:b1ce54272580 | 16 | _ss = 1; |
kotakku | 0:b1ce54272580 | 17 | |
kotakku | 0:b1ce54272580 | 18 | //_spi.mode = SPI_MODE_MASTER; |
kotakku | 0:b1ce54272580 | 19 | format(8, 0); |
kotakku | 0:b1ce54272580 | 20 | frequency(26000000); |
kotakku | 0:b1ce54272580 | 21 | } |
kotakku | 0:b1ce54272580 | 22 | |
kotakku | 0:b1ce54272580 | 23 | /* write single byte into MAX3421 register */ |
kotakku | 0:b1ce54272580 | 24 | void MAX3421E::regWr(uint8_t reg, uint8_t data) |
kotakku | 0:b1ce54272580 | 25 | { |
kotakku | 0:b1ce54272580 | 26 | uint8_t c[2]; |
kotakku | 0:b1ce54272580 | 27 | |
kotakku | 0:b1ce54272580 | 28 | _ss = 0; |
kotakku | 0:b1ce54272580 | 29 | |
kotakku | 0:b1ce54272580 | 30 | c[0] = reg | 0x02; |
kotakku | 0:b1ce54272580 | 31 | c[1] = data; |
kotakku | 0:b1ce54272580 | 32 | |
kotakku | 0:b1ce54272580 | 33 | SPI::write(c[0]); |
kotakku | 0:b1ce54272580 | 34 | SPI::write(c[1]); |
kotakku | 0:b1ce54272580 | 35 | |
kotakku | 0:b1ce54272580 | 36 | //HAL_SPI_Transmit(&SPI_Handle, c, 2, HAL_MAX_DELAY); |
kotakku | 0:b1ce54272580 | 37 | DEBUG("- wirte %2x, %2x\n", c[0], c[1]); |
kotakku | 0:b1ce54272580 | 38 | |
kotakku | 0:b1ce54272580 | 39 | //DEBUG("w %d, %d\n", c[0], c[1]); |
kotakku | 0:b1ce54272580 | 40 | |
kotakku | 0:b1ce54272580 | 41 | _ss = 1; |
kotakku | 0:b1ce54272580 | 42 | } |
kotakku | 0:b1ce54272580 | 43 | |
kotakku | 0:b1ce54272580 | 44 | /* multiple-byte write */ |
kotakku | 0:b1ce54272580 | 45 | |
kotakku | 0:b1ce54272580 | 46 | /* returns a pointer to memory position after last written */ |
kotakku | 0:b1ce54272580 | 47 | uint8_t *MAX3421E::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t *data_p) |
kotakku | 0:b1ce54272580 | 48 | { |
kotakku | 0:b1ce54272580 | 49 | //uint8_t data = reg | 0x02; |
kotakku | 0:b1ce54272580 | 50 | |
kotakku | 0:b1ce54272580 | 51 | _ss = 0; |
kotakku | 0:b1ce54272580 | 52 | |
kotakku | 0:b1ce54272580 | 53 | uint8_t data = reg | 0x02; |
kotakku | 0:b1ce54272580 | 54 | |
kotakku | 0:b1ce54272580 | 55 | SPI::write(data); |
kotakku | 0:b1ce54272580 | 56 | while (nbytes) |
kotakku | 0:b1ce54272580 | 57 | { |
kotakku | 0:b1ce54272580 | 58 | SPI::write(*data_p); |
kotakku | 0:b1ce54272580 | 59 | nbytes--; |
kotakku | 0:b1ce54272580 | 60 | data_p++; // advance data pointer |
kotakku | 0:b1ce54272580 | 61 | } |
kotakku | 0:b1ce54272580 | 62 | |
kotakku | 0:b1ce54272580 | 63 | _ss = 1; |
kotakku | 0:b1ce54272580 | 64 | return data_p; |
kotakku | 0:b1ce54272580 | 65 | } |
kotakku | 0:b1ce54272580 | 66 | /* GPIO write */ |
kotakku | 0:b1ce54272580 | 67 | /*GPIO byte is split between 2 registers, so two writes are needed to write one byte */ |
kotakku | 0:b1ce54272580 | 68 | /* GPOUT bits are in the low nibble. 0-3 in IOPINS1, 4-7 in IOPINS2 */ |
kotakku | 0:b1ce54272580 | 69 | |
kotakku | 0:b1ce54272580 | 70 | void MAX3421E::gpioWr(uint8_t data) |
kotakku | 0:b1ce54272580 | 71 | { |
kotakku | 0:b1ce54272580 | 72 | regWr(rIOPINS1, data); |
kotakku | 0:b1ce54272580 | 73 | data >>= 4; |
kotakku | 0:b1ce54272580 | 74 | regWr(rIOPINS2, data); |
kotakku | 0:b1ce54272580 | 75 | return; |
kotakku | 0:b1ce54272580 | 76 | } |
kotakku | 0:b1ce54272580 | 77 | |
kotakku | 0:b1ce54272580 | 78 | /* single host register read */ |
kotakku | 0:b1ce54272580 | 79 | |
kotakku | 0:b1ce54272580 | 80 | uint8_t MAX3421E::regRd(uint8_t reg) |
kotakku | 0:b1ce54272580 | 81 | { |
kotakku | 0:b1ce54272580 | 82 | _ss = 0; |
kotakku | 0:b1ce54272580 | 83 | |
kotakku | 0:b1ce54272580 | 84 | SPI::write(reg); |
kotakku | 0:b1ce54272580 | 85 | uint8_t rv = SPI::write(0); |
kotakku | 0:b1ce54272580 | 86 | |
kotakku | 0:b1ce54272580 | 87 | DEBUG("- read %2x\n", rv); |
kotakku | 0:b1ce54272580 | 88 | _ss = 1; |
kotakku | 0:b1ce54272580 | 89 | |
kotakku | 0:b1ce54272580 | 90 | return rv; |
kotakku | 0:b1ce54272580 | 91 | } |
kotakku | 0:b1ce54272580 | 92 | /* multiple-byte register read */ |
kotakku | 0:b1ce54272580 | 93 | |
kotakku | 0:b1ce54272580 | 94 | /* returns a pointer to a memory position after last read */ |
kotakku | 0:b1ce54272580 | 95 | uint8_t *MAX3421E::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t *data_p) |
kotakku | 0:b1ce54272580 | 96 | { |
kotakku | 0:b1ce54272580 | 97 | _ss = 0; |
kotakku | 0:b1ce54272580 | 98 | |
kotakku | 0:b1ce54272580 | 99 | |
kotakku | 0:b1ce54272580 | 100 | SPI::write(reg); |
kotakku | 0:b1ce54272580 | 101 | memset(data_p, 0, nbytes); |
kotakku | 0:b1ce54272580 | 102 | while (nbytes) |
kotakku | 0:b1ce54272580 | 103 | { |
kotakku | 0:b1ce54272580 | 104 | *data_p = SPI::write(0); |
kotakku | 0:b1ce54272580 | 105 | data_p++; |
kotakku | 0:b1ce54272580 | 106 | nbytes--; |
kotakku | 0:b1ce54272580 | 107 | } |
kotakku | 0:b1ce54272580 | 108 | |
kotakku | 0:b1ce54272580 | 109 | _ss = 1; |
kotakku | 0:b1ce54272580 | 110 | |
kotakku | 0:b1ce54272580 | 111 | return data_p; |
kotakku | 0:b1ce54272580 | 112 | } |
kotakku | 0:b1ce54272580 | 113 | |
kotakku | 0:b1ce54272580 | 114 | /* GPIO read. See gpioWr for explanation */ |
kotakku | 0:b1ce54272580 | 115 | |
kotakku | 0:b1ce54272580 | 116 | /** @brief Reads the current GPI input values |
kotakku | 0:b1ce54272580 | 117 | * @retval uint8_t Bitwise value of all 8 GPI inputs |
kotakku | 0:b1ce54272580 | 118 | */ |
kotakku | 0:b1ce54272580 | 119 | /* GPIN pins are in high nibbles of IOPINS1, IOPINS2 */ |
kotakku | 0:b1ce54272580 | 120 | |
kotakku | 0:b1ce54272580 | 121 | uint8_t MAX3421E::gpioRd() |
kotakku | 0:b1ce54272580 | 122 | { |
kotakku | 0:b1ce54272580 | 123 | uint8_t gpin = 0; |
kotakku | 0:b1ce54272580 | 124 | gpin = regRd(rIOPINS2); //pins 4-7 |
kotakku | 0:b1ce54272580 | 125 | gpin &= 0xf0; //clean lower nibble |
kotakku | 0:b1ce54272580 | 126 | gpin |= (regRd(rIOPINS1) >> 4); //shift low bits and OR with upper from previous operation. |
kotakku | 0:b1ce54272580 | 127 | return (gpin); |
kotakku | 0:b1ce54272580 | 128 | } |
kotakku | 0:b1ce54272580 | 129 | |
kotakku | 0:b1ce54272580 | 130 | /** @brief Reads the current GPI output values |
kotakku | 0:b1ce54272580 | 131 | * @retval uint8_t Bitwise value of all 8 GPI outputs |
kotakku | 0:b1ce54272580 | 132 | */ |
kotakku | 0:b1ce54272580 | 133 | /* GPOUT pins are in low nibbles of IOPINS1, IOPINS2 */ |
kotakku | 0:b1ce54272580 | 134 | |
kotakku | 0:b1ce54272580 | 135 | uint8_t MAX3421E::gpioRdOutput() |
kotakku | 0:b1ce54272580 | 136 | { |
kotakku | 0:b1ce54272580 | 137 | uint8_t gpout = 0; |
kotakku | 0:b1ce54272580 | 138 | gpout = regRd(rIOPINS1); //pins 0-3 |
kotakku | 0:b1ce54272580 | 139 | gpout &= 0x0f; //clean upper nibble |
kotakku | 0:b1ce54272580 | 140 | gpout |= (regRd(rIOPINS2) << 4); //shift high bits and OR with lower from previous operation. |
kotakku | 0:b1ce54272580 | 141 | return (gpout); |
kotakku | 0:b1ce54272580 | 142 | } |
kotakku | 0:b1ce54272580 | 143 | |
kotakku | 0:b1ce54272580 | 144 | /* reset MAX3421E. Returns number of cycles it took for PLL to stabilize after reset |
kotakku | 0:b1ce54272580 | 145 | or zero if PLL haven't stabilized in 65535 cycles */ |
kotakku | 0:b1ce54272580 | 146 | |
kotakku | 0:b1ce54272580 | 147 | uint16_t MAX3421E::reset() |
kotakku | 0:b1ce54272580 | 148 | { |
kotakku | 0:b1ce54272580 | 149 | uint16_t i = 0; |
kotakku | 0:b1ce54272580 | 150 | regWr(rUSBCTL, bmCHIPRES); |
kotakku | 0:b1ce54272580 | 151 | regWr(rUSBCTL, 0x00); |
kotakku | 0:b1ce54272580 | 152 | while (++i) |
kotakku | 0:b1ce54272580 | 153 | { |
kotakku | 0:b1ce54272580 | 154 | if ((regRd(rUSBIRQ) & bmOSCOKIRQ)) |
kotakku | 0:b1ce54272580 | 155 | { |
kotakku | 0:b1ce54272580 | 156 | break; |
kotakku | 0:b1ce54272580 | 157 | } |
kotakku | 0:b1ce54272580 | 158 | } |
kotakku | 0:b1ce54272580 | 159 | return (i); |
kotakku | 0:b1ce54272580 | 160 | } |
kotakku | 0:b1ce54272580 | 161 | |
kotakku | 0:b1ce54272580 | 162 | /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ |
kotakku | 0:b1ce54272580 | 163 | |
kotakku | 0:b1ce54272580 | 164 | int8_t MAX3421E::Init() |
kotakku | 0:b1ce54272580 | 165 | { |
kotakku | 0:b1ce54272580 | 166 | XMEM_ACQUIRE_SPI(); |
kotakku | 0:b1ce54272580 | 167 | // Moved here. |
kotakku | 0:b1ce54272580 | 168 | // you really should not init hardware in the constructor when it involves locks. |
kotakku | 0:b1ce54272580 | 169 | // Also avoids the vbus flicker issue confusing some devices. |
kotakku | 0:b1ce54272580 | 170 | /* pin and peripheral setup */ |
kotakku | 0:b1ce54272580 | 171 | |
kotakku | 0:b1ce54272580 | 172 | _ss = 1; |
kotakku | 0:b1ce54272580 | 173 | |
kotakku | 0:b1ce54272580 | 174 | XMEM_RELEASE_SPI(); |
kotakku | 0:b1ce54272580 | 175 | /* MAX3421E - full-duplex SPI, level interrupt */ |
kotakku | 0:b1ce54272580 | 176 | // GPX pin on. Moved here, otherwise we flicker the vbus. |
kotakku | 0:b1ce54272580 | 177 | regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL)); |
kotakku | 0:b1ce54272580 | 178 | |
kotakku | 0:b1ce54272580 | 179 | if (reset() == 0) |
kotakku | 0:b1ce54272580 | 180 | { //OSCOKIRQ hasn't asserted in time |
kotakku | 0:b1ce54272580 | 181 | return (-1); |
kotakku | 0:b1ce54272580 | 182 | } |
kotakku | 0:b1ce54272580 | 183 | |
kotakku | 0:b1ce54272580 | 184 | regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection |
kotakku | 0:b1ce54272580 | 187 | |
kotakku | 0:b1ce54272580 | 188 | /* check if device is connected */ |
kotakku | 0:b1ce54272580 | 189 | regWr(rHCTL, bmSAMPLEBUS); // sample USB bus |
kotakku | 0:b1ce54272580 | 190 | while (!(regRd(rHCTL) & bmSAMPLEBUS)) |
kotakku | 0:b1ce54272580 | 191 | ; //wait for sample operation to finish |
kotakku | 0:b1ce54272580 | 192 | |
kotakku | 0:b1ce54272580 | 193 | busprobe(); //check if anything is connected |
kotakku | 0:b1ce54272580 | 194 | |
kotakku | 0:b1ce54272580 | 195 | regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt |
kotakku | 0:b1ce54272580 | 196 | regWr(rCPUCTL, 0x01); //enable interrupt pin |
kotakku | 0:b1ce54272580 | 197 | |
kotakku | 0:b1ce54272580 | 198 | return (0); |
kotakku | 0:b1ce54272580 | 199 | } |
kotakku | 0:b1ce54272580 | 200 | |
kotakku | 0:b1ce54272580 | 201 | /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ |
kotakku | 0:b1ce54272580 | 202 | |
kotakku | 0:b1ce54272580 | 203 | int8_t MAX3421E::Init(int mseconds) |
kotakku | 0:b1ce54272580 | 204 | { |
kotakku | 0:b1ce54272580 | 205 | XMEM_ACQUIRE_SPI(); |
kotakku | 0:b1ce54272580 | 206 | // Moved here. |
kotakku | 0:b1ce54272580 | 207 | // you really should not init hardware in the constructor when it involves locks. |
kotakku | 0:b1ce54272580 | 208 | // Also avoids the vbus flicker issue confusing some devices. |
kotakku | 0:b1ce54272580 | 209 | /* pin and peripheral setup */ |
kotakku | 0:b1ce54272580 | 210 | |
kotakku | 0:b1ce54272580 | 211 | _ss = 1; |
kotakku | 0:b1ce54272580 | 212 | |
kotakku | 0:b1ce54272580 | 213 | XMEM_RELEASE_SPI(); |
kotakku | 0:b1ce54272580 | 214 | /* MAX3421E - full-duplex SPI, level interrupt, vbus off */ |
kotakku | 0:b1ce54272580 | 215 | regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | GPX_VBDET)); |
kotakku | 0:b1ce54272580 | 216 | |
kotakku | 0:b1ce54272580 | 217 | if (reset() == 0) |
kotakku | 0:b1ce54272580 | 218 | { //OSCOKIRQ hasn't asserted in time |
kotakku | 0:b1ce54272580 | 219 | return (-1); |
kotakku | 0:b1ce54272580 | 220 | } |
kotakku | 0:b1ce54272580 | 221 | |
kotakku | 0:b1ce54272580 | 222 | // Delay a minimum of 1 second to ensure any capacitors are drained. |
kotakku | 0:b1ce54272580 | 223 | // 1 second is required to make sure we do not smoke a Microdrive! |
kotakku | 0:b1ce54272580 | 224 | if (mseconds < 1000) |
kotakku | 0:b1ce54272580 | 225 | mseconds = 1000; |
kotakku | 0:b1ce54272580 | 226 | delay(mseconds); |
kotakku | 0:b1ce54272580 | 227 | |
kotakku | 0:b1ce54272580 | 228 | regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host |
kotakku | 0:b1ce54272580 | 229 | |
kotakku | 0:b1ce54272580 | 230 | regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection |
kotakku | 0:b1ce54272580 | 231 | |
kotakku | 0:b1ce54272580 | 232 | /* check if device is connected */ |
kotakku | 0:b1ce54272580 | 233 | regWr(rHCTL, bmSAMPLEBUS); // sample USB bus |
kotakku | 0:b1ce54272580 | 234 | while (!(regRd(rHCTL) & bmSAMPLEBUS)) |
kotakku | 0:b1ce54272580 | 235 | ; //wait for sample operation to finish |
kotakku | 0:b1ce54272580 | 236 | |
kotakku | 0:b1ce54272580 | 237 | busprobe(); //check if anything is connected |
kotakku | 0:b1ce54272580 | 238 | |
kotakku | 0:b1ce54272580 | 239 | regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt |
kotakku | 0:b1ce54272580 | 240 | regWr(rCPUCTL, 0x01); //enable interrupt pin |
kotakku | 0:b1ce54272580 | 241 | |
kotakku | 0:b1ce54272580 | 242 | // GPX pin on. This is done here so that busprobe will fail if we have a switch connected. |
kotakku | 0:b1ce54272580 | 243 | regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL)); |
kotakku | 0:b1ce54272580 | 244 | |
kotakku | 0:b1ce54272580 | 245 | return (0); |
kotakku | 0:b1ce54272580 | 246 | } |
kotakku | 0:b1ce54272580 | 247 | |
kotakku | 0:b1ce54272580 | 248 | /* probe bus to determine device presence and speed and switch host to this speed */ |
kotakku | 0:b1ce54272580 | 249 | |
kotakku | 0:b1ce54272580 | 250 | void MAX3421E::busprobe() |
kotakku | 0:b1ce54272580 | 251 | { |
kotakku | 0:b1ce54272580 | 252 | uint8_t bus_sample; |
kotakku | 0:b1ce54272580 | 253 | DEBUG("busprobe()\n"); |
kotakku | 0:b1ce54272580 | 254 | bus_sample = regRd(rHRSL); //Get J,K status |
kotakku | 0:b1ce54272580 | 255 | bus_sample &= (bmJSTATUS | bmKSTATUS); //zero the rest of the byte |
kotakku | 0:b1ce54272580 | 256 | switch (bus_sample) |
kotakku | 0:b1ce54272580 | 257 | { //start full-speed or low-speed host |
kotakku | 0:b1ce54272580 | 258 | case (bmJSTATUS): |
kotakku | 0:b1ce54272580 | 259 | if ((regRd(rMODE) & bmLOWSPEED) == 0) |
kotakku | 0:b1ce54272580 | 260 | { |
kotakku | 0:b1ce54272580 | 261 | regWr(rMODE, MODE_FS_HOST); //start full-speed host |
kotakku | 0:b1ce54272580 | 262 | vbusState = FSHOST; |
kotakku | 0:b1ce54272580 | 263 | } |
kotakku | 0:b1ce54272580 | 264 | else |
kotakku | 0:b1ce54272580 | 265 | { |
kotakku | 0:b1ce54272580 | 266 | regWr(rMODE, MODE_LS_HOST); //start low-speed host |
kotakku | 0:b1ce54272580 | 267 | vbusState = LSHOST; |
kotakku | 0:b1ce54272580 | 268 | } |
kotakku | 0:b1ce54272580 | 269 | break; |
kotakku | 0:b1ce54272580 | 270 | case (bmKSTATUS): |
kotakku | 0:b1ce54272580 | 271 | if ((regRd(rMODE) & bmLOWSPEED) == 0) |
kotakku | 0:b1ce54272580 | 272 | { |
kotakku | 0:b1ce54272580 | 273 | regWr(rMODE, MODE_LS_HOST); //start low-speed host |
kotakku | 0:b1ce54272580 | 274 | vbusState = LSHOST; |
kotakku | 0:b1ce54272580 | 275 | } |
kotakku | 0:b1ce54272580 | 276 | else |
kotakku | 0:b1ce54272580 | 277 | { |
kotakku | 0:b1ce54272580 | 278 | regWr(rMODE, MODE_FS_HOST); //start full-speed host |
kotakku | 0:b1ce54272580 | 279 | vbusState = FSHOST; |
kotakku | 0:b1ce54272580 | 280 | } |
kotakku | 0:b1ce54272580 | 281 | break; |
kotakku | 0:b1ce54272580 | 282 | case (bmSE1): //illegal state |
kotakku | 0:b1ce54272580 | 283 | vbusState = SE1; |
kotakku | 0:b1ce54272580 | 284 | break; |
kotakku | 0:b1ce54272580 | 285 | case (bmSE0): //disconnected state |
kotakku | 0:b1ce54272580 | 286 | regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST | bmSEPIRQ); |
kotakku | 0:b1ce54272580 | 287 | vbusState = SE0; |
kotakku | 0:b1ce54272580 | 288 | break; |
kotakku | 0:b1ce54272580 | 289 | } //end switch( bus_sample ) |
kotakku | 0:b1ce54272580 | 290 | } |
kotakku | 0:b1ce54272580 | 291 | |
kotakku | 0:b1ce54272580 | 292 | /* MAX3421 state change task and interrupt handler */ |
kotakku | 0:b1ce54272580 | 293 | |
kotakku | 0:b1ce54272580 | 294 | uint8_t MAX3421E::Task(void) |
kotakku | 0:b1ce54272580 | 295 | { |
kotakku | 0:b1ce54272580 | 296 | uint8_t rcode = 0; |
kotakku | 0:b1ce54272580 | 297 | uint8_t pinvalue; |
kotakku | 0:b1ce54272580 | 298 | //USB_HOST_SERIAL.print("Vbus state: "); |
kotakku | 0:b1ce54272580 | 299 | //USB_HOST_SERIAL.println( vbusState, HEX ); |
kotakku | 0:b1ce54272580 | 300 | |
kotakku | 0:b1ce54272580 | 301 | pinvalue = _intr.read(); |
kotakku | 0:b1ce54272580 | 302 | |
kotakku | 0:b1ce54272580 | 303 | //pinvalue = digitalRead( MAX_INT ); |
kotakku | 0:b1ce54272580 | 304 | if (pinvalue == 0) |
kotakku | 0:b1ce54272580 | 305 | { |
kotakku | 0:b1ce54272580 | 306 | rcode = IntHandler(); |
kotakku | 0:b1ce54272580 | 307 | } |
kotakku | 0:b1ce54272580 | 308 | // pinvalue = digitalRead( MAX_GPX ); |
kotakku | 0:b1ce54272580 | 309 | // if( pinvalue == LOW ) { |
kotakku | 0:b1ce54272580 | 310 | // GpxHandler(); |
kotakku | 0:b1ce54272580 | 311 | // } |
kotakku | 0:b1ce54272580 | 312 | // usbSM(); //USB state machine |
kotakku | 0:b1ce54272580 | 313 | return (rcode); |
kotakku | 0:b1ce54272580 | 314 | } |
kotakku | 0:b1ce54272580 | 315 | |
kotakku | 0:b1ce54272580 | 316 | uint8_t MAX3421E::IntHandler() |
kotakku | 0:b1ce54272580 | 317 | { |
kotakku | 0:b1ce54272580 | 318 | uint8_t HIRQ; |
kotakku | 0:b1ce54272580 | 319 | uint8_t HIRQ_sendback = 0x00; |
kotakku | 0:b1ce54272580 | 320 | |
kotakku | 0:b1ce54272580 | 321 | DEBUG("IntHandler\n"); |
kotakku | 0:b1ce54272580 | 322 | HIRQ = regRd(rHIRQ); //determine interrupt source |
kotakku | 0:b1ce54272580 | 323 | //if( HIRQ & bmFRAMEIRQ ) { //->1ms SOF interrupt handler |
kotakku | 0:b1ce54272580 | 324 | // HIRQ_sendback |= bmFRAMEIRQ; |
kotakku | 0:b1ce54272580 | 325 | //}//end FRAMEIRQ handling |
kotakku | 0:b1ce54272580 | 326 | if (HIRQ & bmCONDETIRQ) |
kotakku | 0:b1ce54272580 | 327 | { |
kotakku | 0:b1ce54272580 | 328 | busprobe(); |
kotakku | 0:b1ce54272580 | 329 | HIRQ_sendback |= bmCONDETIRQ; |
kotakku | 0:b1ce54272580 | 330 | } |
kotakku | 0:b1ce54272580 | 331 | // End HIRQ interrupts handling, clear serviced IRQs |
kotakku | 0:b1ce54272580 | 332 | regWr(rHIRQ, HIRQ_sendback); |
kotakku | 0:b1ce54272580 | 333 | return (HIRQ_sendback); |
kotakku | 0:b1ce54272580 | 334 | } |