works!

Dependencies:   mbed

Committer:
mjoun
Date:
Fri Feb 21 20:40:56 2020 +0000
Revision:
1:6e512faaa17c
works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjoun 1:6e512faaa17c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
mjoun 1:6e512faaa17c 2 *
mjoun 1:6e512faaa17c 3 * Permission is hereby granted, free of charge, to any person
mjoun 1:6e512faaa17c 4 * obtaining a copy of this software and associated documentation
mjoun 1:6e512faaa17c 5 * files (the "Software"), to deal in the Software without
mjoun 1:6e512faaa17c 6 * restriction, including without limitation the rights to use,
mjoun 1:6e512faaa17c 7 * copy, modify, merge, publish, distribute, sublicense, and/or
mjoun 1:6e512faaa17c 8 * sell copies of the Software, and to permit persons to whom the
mjoun 1:6e512faaa17c 9 * Software is furnished to do so, subject to the following
mjoun 1:6e512faaa17c 10 * conditions:
mjoun 1:6e512faaa17c 11 *
mjoun 1:6e512faaa17c 12 * The above copyright notice and this permission notice shall be
mjoun 1:6e512faaa17c 13 * included in all copies or substantial portions of the
mjoun 1:6e512faaa17c 14 * Software.
mjoun 1:6e512faaa17c 15 *
mjoun 1:6e512faaa17c 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
mjoun 1:6e512faaa17c 17 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
mjoun 1:6e512faaa17c 18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
mjoun 1:6e512faaa17c 19 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
mjoun 1:6e512faaa17c 20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mjoun 1:6e512faaa17c 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
mjoun 1:6e512faaa17c 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
mjoun 1:6e512faaa17c 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjoun 1:6e512faaa17c 24 */
mjoun 1:6e512faaa17c 25
mjoun 1:6e512faaa17c 26 #if defined(TARGET_RZ_A1H) || defined(TARGET_VK_RZ_A1H)
mjoun 1:6e512faaa17c 27
mjoun 1:6e512faaa17c 28 /*
mjoun 1:6e512faaa17c 29 This class can use the pipe1, pipe3 and pipe6 only. You should
mjoun 1:6e512faaa17c 30 re-program this class if you wanted to use other pipe.
mjoun 1:6e512faaa17c 31 */
mjoun 1:6e512faaa17c 32
mjoun 1:6e512faaa17c 33 /*************************************************************************/
mjoun 1:6e512faaa17c 34 extern "C"
mjoun 1:6e512faaa17c 35 {
mjoun 1:6e512faaa17c 36 #include "r_typedefs.h"
mjoun 1:6e512faaa17c 37 #include "iodefine.h"
mjoun 1:6e512faaa17c 38 }
mjoun 1:6e512faaa17c 39 #include "USBHAL.h"
mjoun 1:6e512faaa17c 40 #include "devdrv_usb_function_api.h"
mjoun 1:6e512faaa17c 41 #include "usb_iobitmask.h"
mjoun 1:6e512faaa17c 42 #include "rza_io_regrw.h"
mjoun 1:6e512faaa17c 43 #include "USBDevice_Types.h"
mjoun 1:6e512faaa17c 44 #include "usb_function_setting.h"
mjoun 1:6e512faaa17c 45
mjoun 1:6e512faaa17c 46
mjoun 1:6e512faaa17c 47 /*************************************************************************/
mjoun 1:6e512faaa17c 48 /* constants */
mjoun 1:6e512faaa17c 49 const struct PIPECFGREC {
mjoun 1:6e512faaa17c 50 uint16_t endpoint;
mjoun 1:6e512faaa17c 51 uint16_t pipesel;
mjoun 1:6e512faaa17c 52 uint16_t pipecfg;
mjoun 1:6e512faaa17c 53 uint16_t pipebuf;
mjoun 1:6e512faaa17c 54 uint16_t pipemaxp;
mjoun 1:6e512faaa17c 55 uint16_t pipeperi;
mjoun 1:6e512faaa17c 56 } def_pipecfg[] = {
mjoun 1:6e512faaa17c 57 /*EP0OUT and EP0IN are configured by USB IP*/
mjoun 1:6e512faaa17c 58 {
mjoun 1:6e512faaa17c 59 EP1OUT, /*EP1: Host -> Func, INT*/
mjoun 1:6e512faaa17c 60 6 | USB_FUNCTION_D0FIFO_USE,
mjoun 1:6e512faaa17c 61 USB_FUNCTION_INTERRUPT |
mjoun 1:6e512faaa17c 62 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 63 USB_FUNCTION_DBLBOFF |
mjoun 1:6e512faaa17c 64 USB_FUNCTION_CNTMDON |
mjoun 1:6e512faaa17c 65 USB_FUNCTION_SHTNAKOFF |
mjoun 1:6e512faaa17c 66 USB_FUNCTION_DIR_P_OUT |
mjoun 1:6e512faaa17c 67 USB_FUNCTION_EP1,
mjoun 1:6e512faaa17c 68 ( ( ( 64) / 64 - 1 ) << 10 ) | 0x04u,
mjoun 1:6e512faaa17c 69 MAX_PACKET_SIZE_EP1,
mjoun 1:6e512faaa17c 70 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 71 ( 3 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 72 },
mjoun 1:6e512faaa17c 73 {
mjoun 1:6e512faaa17c 74 EP1IN, /*EP1: Host <- Func, INT*/
mjoun 1:6e512faaa17c 75 7 | USB_FUNCTION_D1FIFO_USE,
mjoun 1:6e512faaa17c 76 USB_FUNCTION_INTERRUPT |
mjoun 1:6e512faaa17c 77 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 78 USB_FUNCTION_DBLBOFF |
mjoun 1:6e512faaa17c 79 USB_FUNCTION_CNTMDOFF |
mjoun 1:6e512faaa17c 80 USB_FUNCTION_SHTNAKOFF |
mjoun 1:6e512faaa17c 81 USB_FUNCTION_DIR_P_IN |
mjoun 1:6e512faaa17c 82 USB_FUNCTION_EP1,
mjoun 1:6e512faaa17c 83 ( ( ( 64) / 64 - 1 ) << 10 ) | 0x05u,
mjoun 1:6e512faaa17c 84 MAX_PACKET_SIZE_EP1,
mjoun 1:6e512faaa17c 85 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 86 ( 3 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 87 },
mjoun 1:6e512faaa17c 88 {
mjoun 1:6e512faaa17c 89 EP2OUT, /*EP2: Host -> Func, BULK*/
mjoun 1:6e512faaa17c 90 3 | USB_FUNCTION_D0FIFO_USE,
mjoun 1:6e512faaa17c 91 USB_FUNCTION_BULK |
mjoun 1:6e512faaa17c 92 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 93 USB_FUNCTION_DBLBON |
mjoun 1:6e512faaa17c 94 USB_FUNCTION_CNTMDON |
mjoun 1:6e512faaa17c 95 USB_FUNCTION_SHTNAKON |
mjoun 1:6e512faaa17c 96 USB_FUNCTION_DIR_P_OUT |
mjoun 1:6e512faaa17c 97 USB_FUNCTION_EP2,
mjoun 1:6e512faaa17c 98 ( ( (2048) / 64 - 1 ) << 10 ) | 0x30u,
mjoun 1:6e512faaa17c 99 MAX_PACKET_SIZE_EP2,
mjoun 1:6e512faaa17c 100 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 101 ( 0 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 102 },
mjoun 1:6e512faaa17c 103 {
mjoun 1:6e512faaa17c 104 EP2IN, /*EP2: Host <- Func, BULK*/
mjoun 1:6e512faaa17c 105 4 | USB_FUNCTION_D1FIFO_USE,
mjoun 1:6e512faaa17c 106 USB_FUNCTION_BULK |
mjoun 1:6e512faaa17c 107 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 108 USB_FUNCTION_DBLBOFF |
mjoun 1:6e512faaa17c 109 USB_FUNCTION_CNTMDON |
mjoun 1:6e512faaa17c 110 USB_FUNCTION_SHTNAKOFF |
mjoun 1:6e512faaa17c 111 USB_FUNCTION_DIR_P_IN |
mjoun 1:6e512faaa17c 112 USB_FUNCTION_EP2,
mjoun 1:6e512faaa17c 113 ( ( (2048) / 64 - 1 ) << 10 ) | 0x50u,
mjoun 1:6e512faaa17c 114 MAX_PACKET_SIZE_EP2,
mjoun 1:6e512faaa17c 115 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 116 ( 0 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 117 },
mjoun 1:6e512faaa17c 118 {
mjoun 1:6e512faaa17c 119 EP3OUT, /*EP3: Host -> Func, ISO*/
mjoun 1:6e512faaa17c 120 1 | USB_FUNCTION_D0FIFO_USE,
mjoun 1:6e512faaa17c 121 USB_FUNCTION_ISO |
mjoun 1:6e512faaa17c 122 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 123 USB_FUNCTION_DBLBON |
mjoun 1:6e512faaa17c 124 USB_FUNCTION_CNTMDOFF |
mjoun 1:6e512faaa17c 125 USB_FUNCTION_SHTNAKON |
mjoun 1:6e512faaa17c 126 USB_FUNCTION_DIR_P_OUT |
mjoun 1:6e512faaa17c 127 USB_FUNCTION_EP3,
mjoun 1:6e512faaa17c 128 ( ( ( 512) / 64 - 1 ) << 10 ) | 0x10u,
mjoun 1:6e512faaa17c 129 MAX_PACKET_SIZE_EP3,
mjoun 1:6e512faaa17c 130 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 131 ( 0 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 132 },
mjoun 1:6e512faaa17c 133 {
mjoun 1:6e512faaa17c 134 EP3IN, /*EP3: Host <- Func, ISO*/
mjoun 1:6e512faaa17c 135 2 | USB_FUNCTION_D1FIFO_USE,
mjoun 1:6e512faaa17c 136 USB_FUNCTION_ISO |
mjoun 1:6e512faaa17c 137 USB_FUNCTION_BFREOFF |
mjoun 1:6e512faaa17c 138 USB_FUNCTION_DBLBON |
mjoun 1:6e512faaa17c 139 USB_FUNCTION_CNTMDOFF |
mjoun 1:6e512faaa17c 140 USB_FUNCTION_SHTNAKOFF |
mjoun 1:6e512faaa17c 141 USB_FUNCTION_DIR_P_IN |
mjoun 1:6e512faaa17c 142 USB_FUNCTION_EP3,
mjoun 1:6e512faaa17c 143 ( ( ( 512) / 64 - 1 ) << 10 ) | 0x20u,
mjoun 1:6e512faaa17c 144 MAX_PACKET_SIZE_EP3,
mjoun 1:6e512faaa17c 145 DEVDRV_USBF_OFF |
mjoun 1:6e512faaa17c 146 ( 0 << USB_PIPEPERI_IITV_SHIFT ),
mjoun 1:6e512faaa17c 147 },
mjoun 1:6e512faaa17c 148 { /*terminator*/
mjoun 1:6e512faaa17c 149 0, 0, 0, 0, 0,
mjoun 1:6e512faaa17c 150 },
mjoun 1:6e512faaa17c 151 };
mjoun 1:6e512faaa17c 152
mjoun 1:6e512faaa17c 153
mjoun 1:6e512faaa17c 154 /*************************************************************************/
mjoun 1:6e512faaa17c 155 /* workareas */
mjoun 1:6e512faaa17c 156 USBHAL * USBHAL::instance;
mjoun 1:6e512faaa17c 157
mjoun 1:6e512faaa17c 158 static IRQn_Type int_id; /* interrupt ID */
mjoun 1:6e512faaa17c 159 static uint16_t int_level; /* initerrupt level */
mjoun 1:6e512faaa17c 160 static uint16_t clock_mode; /* input clock selector */
mjoun 1:6e512faaa17c 161 static uint16_t mode; /* USB speed (HIGH/FULL) */
mjoun 1:6e512faaa17c 162
mjoun 1:6e512faaa17c 163 //static DigitalOut *usbx_en;
mjoun 1:6e512faaa17c 164
mjoun 1:6e512faaa17c 165 static uint16_t EP0_read_status;
mjoun 1:6e512faaa17c 166 static uint16_t EPx_read_status;
mjoun 1:6e512faaa17c 167
mjoun 1:6e512faaa17c 168 static uint16_t setup_buffer[MAX_PACKET_SIZE_EP0 / 2];
mjoun 1:6e512faaa17c 169
mjoun 1:6e512faaa17c 170 /* 0: not used / other: a pipe number to use recv_buffer*/
mjoun 1:6e512faaa17c 171 static uint8_t recv_buffer[MAX_PACKET_SIZE_EPBULK];
mjoun 1:6e512faaa17c 172 volatile static uint16_t recv_error;
mjoun 1:6e512faaa17c 173
mjoun 1:6e512faaa17c 174
mjoun 1:6e512faaa17c 175 /*************************************************************************/
mjoun 1:6e512faaa17c 176 /* prototypes for C */
mjoun 1:6e512faaa17c 177 extern "C" {
mjoun 1:6e512faaa17c 178 void usbx_function_BRDYInterruptPIPE0 (uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 179 USBHAL *object, void (USBHAL::*EP0func)(void));
mjoun 1:6e512faaa17c 180
mjoun 1:6e512faaa17c 181 void usbx_function_BRDYInterrupt (uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 182 USBHAL *object, bool (USBHAL::*epCallback[])(void));
mjoun 1:6e512faaa17c 183
mjoun 1:6e512faaa17c 184 void usbx_function_NRDYInterruptPIPE0(uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 185 USBHAL *object, void (USBHAL::*EP0func)(void));
mjoun 1:6e512faaa17c 186
mjoun 1:6e512faaa17c 187 void usbx_function_NRDYInterrupt (uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 188 USBHAL *object, bool (USBHAL::*epCallback[])(void));
mjoun 1:6e512faaa17c 189
mjoun 1:6e512faaa17c 190 void usbx_function_BEMPInterruptPIPE0(uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 191 USBHAL *object, void (USBHAL::*EP0func)(void));
mjoun 1:6e512faaa17c 192
mjoun 1:6e512faaa17c 193 void usbx_function_BEMPInterrupt (uint16_t status, uint16_t intenb,
mjoun 1:6e512faaa17c 194 USBHAL *object, bool (USBHAL::*epCallback[])(void));
mjoun 1:6e512faaa17c 195 }
mjoun 1:6e512faaa17c 196
mjoun 1:6e512faaa17c 197
mjoun 1:6e512faaa17c 198 /*************************************************************************/
mjoun 1:6e512faaa17c 199 /* macros */
mjoun 1:6e512faaa17c 200
mjoun 1:6e512faaa17c 201 /******************************************************************************
mjoun 1:6e512faaa17c 202 * Function Name: usbx_function_BRDYInterruptPIPE0
mjoun 1:6e512faaa17c 203 * Description : Executes BRDY interrupt for pipe0.
mjoun 1:6e512faaa17c 204 * Arguments : uint16_t status ; BRDYSTS Register Value
mjoun 1:6e512faaa17c 205 * : uint16_t intenb ; BRDYENB Register Value
mjoun 1:6e512faaa17c 206 * Return Value : none
mjoun 1:6e512faaa17c 207 *****************************************************************************/
mjoun 1:6e512faaa17c 208 extern "C" {
mjoun 1:6e512faaa17c 209 void usbx_function_BRDYInterruptPIPE0 (
mjoun 1:6e512faaa17c 210 uint16_t status,
mjoun 1:6e512faaa17c 211 uint16_t intenb,
mjoun 1:6e512faaa17c 212 USBHAL *object,
mjoun 1:6e512faaa17c 213 void (USBHAL::*EP0func)(void)
mjoun 1:6e512faaa17c 214 )
mjoun 1:6e512faaa17c 215 {
mjoun 1:6e512faaa17c 216 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 217 uint16_t read_status;
mjoun 1:6e512faaa17c 218
mjoun 1:6e512faaa17c 219 USB20X.BRDYSTS =
mjoun 1:6e512faaa17c 220 (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 221 RZA_IO_RegWrite_16(
mjoun 1:6e512faaa17c 222 &USB20X.CFIFOSEL, USB_FUNCTION_PIPE0,
mjoun 1:6e512faaa17c 223 USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
mjoun 1:6e512faaa17c 224
mjoun 1:6e512faaa17c 225 g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0] =
mjoun 1:6e512faaa17c 226 g_usbx_function_data_count[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 227
mjoun 1:6e512faaa17c 228 read_status = usbx_function_read_buffer_c(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 229
mjoun 1:6e512faaa17c 230 g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0] -=
mjoun 1:6e512faaa17c 231 g_usbx_function_data_count[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 232
mjoun 1:6e512faaa17c 233 switch (read_status) {
mjoun 1:6e512faaa17c 234 case USB_FUNCTION_READING: /* Continue of data read */
mjoun 1:6e512faaa17c 235 case USB_FUNCTION_READEND: /* End of data read */
mjoun 1:6e512faaa17c 236 /* PID = BUF */
mjoun 1:6e512faaa17c 237 usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 238
mjoun 1:6e512faaa17c 239 /*callback*/
mjoun 1:6e512faaa17c 240 (object->*EP0func)();
mjoun 1:6e512faaa17c 241 break;
mjoun 1:6e512faaa17c 242
mjoun 1:6e512faaa17c 243 case USB_FUNCTION_READSHRT: /* End of data read */
mjoun 1:6e512faaa17c 244 usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 245 /* PID = BUF */
mjoun 1:6e512faaa17c 246 usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 247
mjoun 1:6e512faaa17c 248 /*callback*/
mjoun 1:6e512faaa17c 249 (object->*EP0func)();
mjoun 1:6e512faaa17c 250 break;
mjoun 1:6e512faaa17c 251
mjoun 1:6e512faaa17c 252 case USB_FUNCTION_READOVER: /* FIFO access error */
mjoun 1:6e512faaa17c 253 /* Buffer Clear */
mjoun 1:6e512faaa17c 254 USB20X.CFIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 255 usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 256 /* Req Error */
mjoun 1:6e512faaa17c 257 usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 258
mjoun 1:6e512faaa17c 259 /*callback*/
mjoun 1:6e512faaa17c 260 (object->*EP0func)();
mjoun 1:6e512faaa17c 261 break;
mjoun 1:6e512faaa17c 262
mjoun 1:6e512faaa17c 263 case DEVDRV_USBF_FIFOERROR: /* FIFO access error */
mjoun 1:6e512faaa17c 264 default:
mjoun 1:6e512faaa17c 265 usbx_function_disable_brdy_int(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 266 /* Req Error */
mjoun 1:6e512faaa17c 267 usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 268 break;
mjoun 1:6e512faaa17c 269 }
mjoun 1:6e512faaa17c 270 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 271 dumy_sts = USB20X.BRDYSTS;
mjoun 1:6e512faaa17c 272 }
mjoun 1:6e512faaa17c 273 }
mjoun 1:6e512faaa17c 274
mjoun 1:6e512faaa17c 275
mjoun 1:6e512faaa17c 276 /******************************************************************************
mjoun 1:6e512faaa17c 277 * Function Name: usbx_function_BRDYInterrupt
mjoun 1:6e512faaa17c 278 * Description : Executes BRDY interrupt uxclude pipe0.
mjoun 1:6e512faaa17c 279 * Arguments : uint16_t status ; BRDYSTS Register Value
mjoun 1:6e512faaa17c 280 * : uint16_t intenb ; BRDYENB Register Value
mjoun 1:6e512faaa17c 281 * Return Value : none
mjoun 1:6e512faaa17c 282 *****************************************************************************/
mjoun 1:6e512faaa17c 283 extern "C" {
mjoun 1:6e512faaa17c 284 void usbx_function_BRDYInterrupt(
mjoun 1:6e512faaa17c 285 uint16_t status,
mjoun 1:6e512faaa17c 286 uint16_t intenb,
mjoun 1:6e512faaa17c 287 USBHAL *object,
mjoun 1:6e512faaa17c 288 bool (USBHAL::*epCallback[])(void)
mjoun 1:6e512faaa17c 289 )
mjoun 1:6e512faaa17c 290 {
mjoun 1:6e512faaa17c 291 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 292
mjoun 1:6e512faaa17c 293 /**************************************************************
mjoun 1:6e512faaa17c 294 * Function Name: usbx_function_brdy_int
mjoun 1:6e512faaa17c 295 * Description : Executes BRDY interrupt(USB_FUNCTION_PIPE1-9).
mjoun 1:6e512faaa17c 296 * : According to the pipe that interrupt is generated in,
mjoun 1:6e512faaa17c 297 * : reads/writes buffer allocated in the pipe.
mjoun 1:6e512faaa17c 298 * : This function is executed in the BRDY
mjoun 1:6e512faaa17c 299 * : interrupt handler. This function
mjoun 1:6e512faaa17c 300 * : clears BRDY interrupt status and BEMP
mjoun 1:6e512faaa17c 301 * : interrupt status.
mjoun 1:6e512faaa17c 302 * Arguments : uint16_t Status ; BRDYSTS Register Value
mjoun 1:6e512faaa17c 303 * : uint16_t Int_enbl ; BRDYENB Register Value
mjoun 1:6e512faaa17c 304 * Return Value : none
mjoun 1:6e512faaa17c 305 *************************************************************/
mjoun 1:6e512faaa17c 306 /* copied from usbx_function_intrn.c */
mjoun 1:6e512faaa17c 307 uint32_t int_sense = 0;
mjoun 1:6e512faaa17c 308 uint16_t pipe;
mjoun 1:6e512faaa17c 309 uint16_t pipebit;
mjoun 1:6e512faaa17c 310 uint16_t ep;
mjoun 1:6e512faaa17c 311
mjoun 1:6e512faaa17c 312 for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
mjoun 1:6e512faaa17c 313 pipebit = g_usbx_function_bit_set[pipe];
mjoun 1:6e512faaa17c 314
mjoun 1:6e512faaa17c 315 if ((status & pipebit) && (intenb & pipebit)) {
mjoun 1:6e512faaa17c 316 USB20X.BRDYSTS = (uint16_t)~pipebit;
mjoun 1:6e512faaa17c 317 USB20X.BEMPSTS = (uint16_t)~pipebit;
mjoun 1:6e512faaa17c 318
mjoun 1:6e512faaa17c 319 switch (g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) {
mjoun 1:6e512faaa17c 320 case USB_FUNCTION_D0FIFO_DMA:
mjoun 1:6e512faaa17c 321 if (g_usbx_function_DmaStatus[USB_FUNCTION_D0FIFO] != USB_FUNCTION_DMA_READY) {
mjoun 1:6e512faaa17c 322 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 323 usbx_function_dma_interrupt_d0fifo(int_sense);
mjoun 1:6e512faaa17c 324 }
mjoun 1:6e512faaa17c 325
mjoun 1:6e512faaa17c 326 if (RZA_IO_RegRead_16(
mjoun 1:6e512faaa17c 327 &g_usbx_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0) {
mjoun 1:6e512faaa17c 328 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 329 usbx_function_read_dma(pipe);
mjoun 1:6e512faaa17c 330 usbx_function_disable_brdy_int(pipe);
mjoun 1:6e512faaa17c 331 } else {
mjoun 1:6e512faaa17c 332 USB20X.D0FIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 333 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 334 }
mjoun 1:6e512faaa17c 335 break;
mjoun 1:6e512faaa17c 336
mjoun 1:6e512faaa17c 337 case USB_FUNCTION_D1FIFO_DMA:
mjoun 1:6e512faaa17c 338 if (g_usbx_function_DmaStatus[USB_FUNCTION_D1FIFO] != USB_FUNCTION_DMA_READY) {
mjoun 1:6e512faaa17c 339 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 340 usbx_function_dma_interrupt_d1fifo(int_sense);
mjoun 1:6e512faaa17c 341 }
mjoun 1:6e512faaa17c 342
mjoun 1:6e512faaa17c 343 if (RZA_IO_RegRead_16(
mjoun 1:6e512faaa17c 344 &g_usbx_function_pipecfg[pipe], USB_PIPECFG_BFRE_SHIFT, USB_PIPECFG_BFRE) == 0) {
mjoun 1:6e512faaa17c 345 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 346 usbx_function_read_dma(pipe);
mjoun 1:6e512faaa17c 347 usbx_function_disable_brdy_int(pipe);
mjoun 1:6e512faaa17c 348 } else {
mjoun 1:6e512faaa17c 349 USB20X.D1FIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 350 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 351 }
mjoun 1:6e512faaa17c 352 break;
mjoun 1:6e512faaa17c 353
mjoun 1:6e512faaa17c 354 default:
mjoun 1:6e512faaa17c 355 ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
mjoun 1:6e512faaa17c 356 ep <<= 1;
mjoun 1:6e512faaa17c 357 if (RZA_IO_RegRead_16(
mjoun 1:6e512faaa17c 358 &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
mjoun 1:6e512faaa17c 359 /* read */
mjoun 1:6e512faaa17c 360 EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
mjoun 1:6e512faaa17c 361 (object->*(epCallback[ep - 2])) ();
mjoun 1:6e512faaa17c 362 EPx_read_status = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 363 } else {
mjoun 1:6e512faaa17c 364 /* write */
mjoun 1:6e512faaa17c 365 EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
mjoun 1:6e512faaa17c 366 (object->*(epCallback[ep - 2 + 1])) ();
mjoun 1:6e512faaa17c 367 EPx_read_status = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 368 usbx_function_write_buffer(pipe);
mjoun 1:6e512faaa17c 369 }
mjoun 1:6e512faaa17c 370 }
mjoun 1:6e512faaa17c 371 }
mjoun 1:6e512faaa17c 372 }
mjoun 1:6e512faaa17c 373 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 374 dumy_sts = USB20X.BRDYSTS;
mjoun 1:6e512faaa17c 375 }
mjoun 1:6e512faaa17c 376 }
mjoun 1:6e512faaa17c 377
mjoun 1:6e512faaa17c 378
mjoun 1:6e512faaa17c 379 /******************************************************************************
mjoun 1:6e512faaa17c 380 * Function Name: usbx_function_NRDYInterruptPIPE0
mjoun 1:6e512faaa17c 381 * Description : Executes NRDY interrupt for pipe0.
mjoun 1:6e512faaa17c 382 * Arguments : uint16_t status ; NRDYSTS Register Value
mjoun 1:6e512faaa17c 383 * : uint16_t intenb ; NRDYENB Register Value
mjoun 1:6e512faaa17c 384 * Return Value : none
mjoun 1:6e512faaa17c 385 *****************************************************************************/
mjoun 1:6e512faaa17c 386 extern "C" {
mjoun 1:6e512faaa17c 387 void usbx_function_NRDYInterruptPIPE0(
mjoun 1:6e512faaa17c 388 uint16_t status,
mjoun 1:6e512faaa17c 389 uint16_t intenb,
mjoun 1:6e512faaa17c 390 USBHAL *object,
mjoun 1:6e512faaa17c 391 void (USBHAL::*EP0func)(void)
mjoun 1:6e512faaa17c 392 )
mjoun 1:6e512faaa17c 393 {
mjoun 1:6e512faaa17c 394 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 395
mjoun 1:6e512faaa17c 396 USB20X.NRDYSTS =
mjoun 1:6e512faaa17c 397 (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 398
mjoun 1:6e512faaa17c 399 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 400 dumy_sts = USB20X.NRDYSTS;
mjoun 1:6e512faaa17c 401 }
mjoun 1:6e512faaa17c 402 }
mjoun 1:6e512faaa17c 403
mjoun 1:6e512faaa17c 404
mjoun 1:6e512faaa17c 405 /******************************************************************************
mjoun 1:6e512faaa17c 406 * Function Name: usbx_function_NRDYInterrupt
mjoun 1:6e512faaa17c 407 * Description : Executes NRDY interrupt exclude pipe0.
mjoun 1:6e512faaa17c 408 * Arguments : uint16_t status ; NRDYSTS Register Value
mjoun 1:6e512faaa17c 409 * : uint16_t intenb ; NRDYENB Register Value
mjoun 1:6e512faaa17c 410 * Return Value : none
mjoun 1:6e512faaa17c 411 *****************************************************************************/
mjoun 1:6e512faaa17c 412 extern "C" {
mjoun 1:6e512faaa17c 413 void usbx_function_NRDYInterrupt(
mjoun 1:6e512faaa17c 414 uint16_t status,
mjoun 1:6e512faaa17c 415 uint16_t intenb,
mjoun 1:6e512faaa17c 416 USBHAL *object,
mjoun 1:6e512faaa17c 417 bool (USBHAL::*epCallback[])(void)
mjoun 1:6e512faaa17c 418 )
mjoun 1:6e512faaa17c 419 {
mjoun 1:6e512faaa17c 420 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 421
mjoun 1:6e512faaa17c 422 /**************************************************************
mjoun 1:6e512faaa17c 423 * Function Name: usbx_function_nrdy_int
mjoun 1:6e512faaa17c 424 * Description : Executes NRDY interrupt(USB_FUNCTION_PIPE1-9).
mjoun 1:6e512faaa17c 425 * : Checks NRDY interrupt cause by PID. When the cause if STALL,
mjoun 1:6e512faaa17c 426 * : regards the pipe state as STALL and ends the processing.
mjoun 1:6e512faaa17c 427 * : Then the cause is not STALL, increments the error count to
mjoun 1:6e512faaa17c 428 * : communicate again. When the error count is 3, determines
mjoun 1:6e512faaa17c 429 * : the pipe state as DEVDRV_USBF_PIPE_NORES and ends the processing.
mjoun 1:6e512faaa17c 430 * : This function is executed in the NRDY interrupt handler.
mjoun 1:6e512faaa17c 431 * : This function clears NRDY interrupt status.
mjoun 1:6e512faaa17c 432 * Arguments : uint16_t status ; NRDYSTS Register Value
mjoun 1:6e512faaa17c 433 * : uint16_t int_enb ; NRDYENB Register Value
mjoun 1:6e512faaa17c 434 * Return Value : none
mjoun 1:6e512faaa17c 435 *************************************************************/
mjoun 1:6e512faaa17c 436 /* copied from usbx_function_intrn.c */
mjoun 1:6e512faaa17c 437 #if 0
mjoun 1:6e512faaa17c 438 uint16_t usefifo;
mjoun 1:6e512faaa17c 439 #endif
mjoun 1:6e512faaa17c 440 uint16_t pid;
mjoun 1:6e512faaa17c 441 uint16_t pipe;
mjoun 1:6e512faaa17c 442 uint16_t bitcheck;
mjoun 1:6e512faaa17c 443 #if 0
mjoun 1:6e512faaa17c 444 uint16_t mbw;
mjoun 1:6e512faaa17c 445 uint32_t size;
mjoun 1:6e512faaa17c 446 #endif
mjoun 1:6e512faaa17c 447 uint16_t ep;
mjoun 1:6e512faaa17c 448
mjoun 1:6e512faaa17c 449 bitcheck = (uint16_t)(status & intenb);
mjoun 1:6e512faaa17c 450
mjoun 1:6e512faaa17c 451 USB20X.NRDYSTS = (uint16_t)~status;
mjoun 1:6e512faaa17c 452
mjoun 1:6e512faaa17c 453
mjoun 1:6e512faaa17c 454 if (RZA_IO_RegRead_16(&USB20X.SYSCFG0, USB_SYSCFG_DCFM_SHIFT, USB_SYSCFG_DCFM) == 1) {
mjoun 1:6e512faaa17c 455 /* USB HOST */
mjoun 1:6e512faaa17c 456 /* not support */
mjoun 1:6e512faaa17c 457
mjoun 1:6e512faaa17c 458 } else {
mjoun 1:6e512faaa17c 459 /* USB Function */
mjoun 1:6e512faaa17c 460 for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
mjoun 1:6e512faaa17c 461 if ((bitcheck&g_usbx_function_bit_set[pipe]) != g_usbx_function_bit_set[pipe]) {
mjoun 1:6e512faaa17c 462 continue;
mjoun 1:6e512faaa17c 463 }
mjoun 1:6e512faaa17c 464
mjoun 1:6e512faaa17c 465 if (g_usbx_function_pipe_status[pipe] != DEVDRV_USBF_PIPE_WAIT) {
mjoun 1:6e512faaa17c 466 continue;
mjoun 1:6e512faaa17c 467 }
mjoun 1:6e512faaa17c 468
mjoun 1:6e512faaa17c 469 #if 0
mjoun 1:6e512faaa17c 470 usbx_function_set_pid_nak(pipe);
mjoun 1:6e512faaa17c 471
mjoun 1:6e512faaa17c 472 size = (uint32_t)g_usbx_function_data_count[pipe];
mjoun 1:6e512faaa17c 473 mbw = usbx_function_get_mbw(
mjoun 1:6e512faaa17c 474 size, (uint32_t)g_usbx_function_data_pointer[pipe]);
mjoun 1:6e512faaa17c 475
mjoun 1:6e512faaa17c 476 usefifo = (uint16_t)(g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE);
mjoun 1:6e512faaa17c 477 switch (usefifo) {
mjoun 1:6e512faaa17c 478
mjoun 1:6e512faaa17c 479 case USB_FUNCTION_D0FIFO_USE:
mjoun 1:6e512faaa17c 480 usbx_function_set_curpipe(
mjoun 1:6e512faaa17c 481 pipe, USB_FUNCTION_D0USE, DEVDRV_USBF_NO, mbw);
mjoun 1:6e512faaa17c 482 USB20X.D0FIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 483 break;
mjoun 1:6e512faaa17c 484
mjoun 1:6e512faaa17c 485 case USB_FUNCTION_D1FIFO_USE:
mjoun 1:6e512faaa17c 486 usbx_function_set_curpipe(
mjoun 1:6e512faaa17c 487 pipe, USB_FUNCTION_D1USE, DEVDRV_USBF_NO, mbw);
mjoun 1:6e512faaa17c 488 USB20X.D1FIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 489 break;
mjoun 1:6e512faaa17c 490
mjoun 1:6e512faaa17c 491 default:
mjoun 1:6e512faaa17c 492 usbx_function_set_curpipe(
mjoun 1:6e512faaa17c 493 pipe, USB_FUNCTION_CUSE, USB_FUNCTION_CFIFO_READ, mbw);
mjoun 1:6e512faaa17c 494 USB20X.CFIFOCTR = USB_FUNCTION_BITBCLR;
mjoun 1:6e512faaa17c 495 break;
mjoun 1:6e512faaa17c 496 }
mjoun 1:6e512faaa17c 497
mjoun 1:6e512faaa17c 498 usbx_function_aclrm(pipe);
mjoun 1:6e512faaa17c 499
mjoun 1:6e512faaa17c 500 usbx_function_enable_nrdy_int(pipe);
mjoun 1:6e512faaa17c 501 usbx_function_enable_brdy_int(pipe);
mjoun 1:6e512faaa17c 502
mjoun 1:6e512faaa17c 503 usbx_function_set_pid_buf(pipe);
mjoun 1:6e512faaa17c 504 #endif
mjoun 1:6e512faaa17c 505
mjoun 1:6e512faaa17c 506 pid = usbx_function_get_pid(pipe);
mjoun 1:6e512faaa17c 507 if ((pid == DEVDRV_USBF_PID_STALL) || (pid == DEVDRV_USBF_PID_STALL2)) {
mjoun 1:6e512faaa17c 508 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_STALL;
mjoun 1:6e512faaa17c 509 } else {
mjoun 1:6e512faaa17c 510 usbx_function_set_pid_buf(pipe);
mjoun 1:6e512faaa17c 511 }
mjoun 1:6e512faaa17c 512
mjoun 1:6e512faaa17c 513 ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
mjoun 1:6e512faaa17c 514 ep <<= 1;
mjoun 1:6e512faaa17c 515 if (RZA_IO_RegRead_16(
mjoun 1:6e512faaa17c 516 &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
mjoun 1:6e512faaa17c 517 /* read */
mjoun 1:6e512faaa17c 518 __NOP();
mjoun 1:6e512faaa17c 519 } else {
mjoun 1:6e512faaa17c 520 /* write */
mjoun 1:6e512faaa17c 521 __NOP();
mjoun 1:6e512faaa17c 522 }
mjoun 1:6e512faaa17c 523 }
mjoun 1:6e512faaa17c 524 }
mjoun 1:6e512faaa17c 525
mjoun 1:6e512faaa17c 526 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 527 dumy_sts = USB20X.NRDYSTS;
mjoun 1:6e512faaa17c 528 }
mjoun 1:6e512faaa17c 529 }
mjoun 1:6e512faaa17c 530
mjoun 1:6e512faaa17c 531 /******************************************************************************
mjoun 1:6e512faaa17c 532 * Function Name: usbx_function_BEMPInterruptPIPE0
mjoun 1:6e512faaa17c 533 * Description : Executes BEMP interrupt for pipe0.
mjoun 1:6e512faaa17c 534 * Arguments : uint16_t status ; BEMPSTS Register Value
mjoun 1:6e512faaa17c 535 * : uint16_t intenb ; BEMPENB Register Value
mjoun 1:6e512faaa17c 536 * Return Value : none
mjoun 1:6e512faaa17c 537 *****************************************************************************/
mjoun 1:6e512faaa17c 538 extern "C" {
mjoun 1:6e512faaa17c 539 void usbx_function_BEMPInterruptPIPE0(
mjoun 1:6e512faaa17c 540 uint16_t status,
mjoun 1:6e512faaa17c 541 uint16_t intenb,
mjoun 1:6e512faaa17c 542 USBHAL *object,
mjoun 1:6e512faaa17c 543 void (USBHAL::*EP0func)(void)
mjoun 1:6e512faaa17c 544 )
mjoun 1:6e512faaa17c 545 {
mjoun 1:6e512faaa17c 546 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 547
mjoun 1:6e512faaa17c 548 USB20X.BEMPSTS =
mjoun 1:6e512faaa17c 549 (uint16_t)~g_usbx_function_bit_set[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 550 RZA_IO_RegWrite_16(
mjoun 1:6e512faaa17c 551 &USB20X.CFIFOSEL, USB_FUNCTION_PIPE0,
mjoun 1:6e512faaa17c 552 USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
mjoun 1:6e512faaa17c 553
mjoun 1:6e512faaa17c 554 /*usbx_function_write_buffer_c(USB_FUNCTION_PIPE0);*/
mjoun 1:6e512faaa17c 555 (object->*EP0func)();
mjoun 1:6e512faaa17c 556
mjoun 1:6e512faaa17c 557 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 558 dumy_sts = USB20X.BEMPSTS;
mjoun 1:6e512faaa17c 559 }
mjoun 1:6e512faaa17c 560 }
mjoun 1:6e512faaa17c 561
mjoun 1:6e512faaa17c 562
mjoun 1:6e512faaa17c 563 /******************************************************************************
mjoun 1:6e512faaa17c 564 * Function Name: usbx_function_BEMPInterrupt
mjoun 1:6e512faaa17c 565 * Description : Executes BEMP interrupt exclude pipe0.
mjoun 1:6e512faaa17c 566 * Arguments : uint16_t status ; BEMPSTS Register Value
mjoun 1:6e512faaa17c 567 * : uint16_t intenb ; BEMPENB Register Value
mjoun 1:6e512faaa17c 568 * Return Value : none
mjoun 1:6e512faaa17c 569 *****************************************************************************/
mjoun 1:6e512faaa17c 570 extern "C" {
mjoun 1:6e512faaa17c 571 void usbx_function_BEMPInterrupt(
mjoun 1:6e512faaa17c 572 uint16_t status,
mjoun 1:6e512faaa17c 573 uint16_t intenb,
mjoun 1:6e512faaa17c 574 USBHAL *object,
mjoun 1:6e512faaa17c 575 bool (USBHAL::*epCallback[])(void)
mjoun 1:6e512faaa17c 576 )
mjoun 1:6e512faaa17c 577 {
mjoun 1:6e512faaa17c 578 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 579
mjoun 1:6e512faaa17c 580 /**************************************************************
mjoun 1:6e512faaa17c 581 * Function Name: usbx_function_bemp_int
mjoun 1:6e512faaa17c 582 * Description : Executes BEMP interrupt(USB_FUNCTION_PIPE1-9).
mjoun 1:6e512faaa17c 583 * Arguments : uint16_t status ; BEMPSTS Register Value
mjoun 1:6e512faaa17c 584 * : uint16_t intenb ; BEMPENB Register Value
mjoun 1:6e512faaa17c 585 * Return Value : none
mjoun 1:6e512faaa17c 586 *************************************************************/
mjoun 1:6e512faaa17c 587 /* copied from usbx_function_intrn.c */
mjoun 1:6e512faaa17c 588 uint16_t pid;
mjoun 1:6e512faaa17c 589 uint16_t pipe;
mjoun 1:6e512faaa17c 590 uint16_t bitcheck;
mjoun 1:6e512faaa17c 591 uint16_t inbuf;
mjoun 1:6e512faaa17c 592 uint16_t ep;
mjoun 1:6e512faaa17c 593
mjoun 1:6e512faaa17c 594 bitcheck = (uint16_t)(status & intenb);
mjoun 1:6e512faaa17c 595
mjoun 1:6e512faaa17c 596 USB20X.BEMPSTS = (uint16_t)~status;
mjoun 1:6e512faaa17c 597
mjoun 1:6e512faaa17c 598 for (pipe = USB_FUNCTION_PIPE1; pipe <= USB_FUNCTION_MAX_PIPE_NO; pipe++) {
mjoun 1:6e512faaa17c 599 if ((bitcheck&g_usbx_function_bit_set[pipe]) != g_usbx_function_bit_set[pipe]) {
mjoun 1:6e512faaa17c 600 continue;
mjoun 1:6e512faaa17c 601 }
mjoun 1:6e512faaa17c 602
mjoun 1:6e512faaa17c 603 pid = usbx_function_get_pid(pipe);
mjoun 1:6e512faaa17c 604
mjoun 1:6e512faaa17c 605 if ((pid == DEVDRV_USBF_PID_STALL) ||
mjoun 1:6e512faaa17c 606 (pid == DEVDRV_USBF_PID_STALL2)) {
mjoun 1:6e512faaa17c 607 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_STALL;
mjoun 1:6e512faaa17c 608
mjoun 1:6e512faaa17c 609 } else {
mjoun 1:6e512faaa17c 610 inbuf = usbx_function_get_inbuf(pipe);
mjoun 1:6e512faaa17c 611
mjoun 1:6e512faaa17c 612 if (inbuf == 0) {
mjoun 1:6e512faaa17c 613 usbx_function_disable_bemp_int(pipe);
mjoun 1:6e512faaa17c 614 usbx_function_set_pid_nak(pipe);
mjoun 1:6e512faaa17c 615 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 616
mjoun 1:6e512faaa17c 617 switch (g_usbx_function_PipeTbl[pipe] & USB_FUNCTION_FIFO_USE) {
mjoun 1:6e512faaa17c 618 case USB_FUNCTION_D0FIFO_DMA:
mjoun 1:6e512faaa17c 619 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 620 break;
mjoun 1:6e512faaa17c 621
mjoun 1:6e512faaa17c 622 case USB_FUNCTION_D1FIFO_DMA:
mjoun 1:6e512faaa17c 623 /*now, DMA is not supported*/
mjoun 1:6e512faaa17c 624 break;
mjoun 1:6e512faaa17c 625
mjoun 1:6e512faaa17c 626 default:
mjoun 1:6e512faaa17c 627 ep = (g_usbx_function_pipecfg[pipe] & USB_PIPECFG_EPNUM) >> USB_PIPECFG_EPNUM_SHIFT;
mjoun 1:6e512faaa17c 628 ep <<= 1;
mjoun 1:6e512faaa17c 629 if (RZA_IO_RegRead_16(
mjoun 1:6e512faaa17c 630 &g_usbx_function_pipecfg[pipe], USB_PIPECFG_DIR_SHIFT, USB_PIPECFG_DIR) == 0) {
mjoun 1:6e512faaa17c 631 /* read */
mjoun 1:6e512faaa17c 632 __NOP();
mjoun 1:6e512faaa17c 633 } else {
mjoun 1:6e512faaa17c 634 /* write */
mjoun 1:6e512faaa17c 635 EPx_read_status = DEVDRV_USBF_PIPE_WAIT;
mjoun 1:6e512faaa17c 636 (object->*(epCallback[ep - 2 + 1])) ();
mjoun 1:6e512faaa17c 637 EPx_read_status = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 638 }
mjoun 1:6e512faaa17c 639 }
mjoun 1:6e512faaa17c 640 }
mjoun 1:6e512faaa17c 641 }
mjoun 1:6e512faaa17c 642 }
mjoun 1:6e512faaa17c 643
mjoun 1:6e512faaa17c 644 /* Three dummy reads for clearing interrupt requests */
mjoun 1:6e512faaa17c 645 dumy_sts = USB20X.BEMPSTS;
mjoun 1:6e512faaa17c 646 }
mjoun 1:6e512faaa17c 647 }
mjoun 1:6e512faaa17c 648
mjoun 1:6e512faaa17c 649 /******************************************************************************
mjoun 1:6e512faaa17c 650 * Function Name: EP2PIPE
mjoun 1:6e512faaa17c 651 * Description : Converts from endpoint to pipe
mjoun 1:6e512faaa17c 652 * Arguments : number of endpoint
mjoun 1:6e512faaa17c 653 * Return Value : number of pipe
mjoun 1:6e512faaa17c 654 *****************************************************************************/
mjoun 1:6e512faaa17c 655 /*EP2PIPE converter is for pipe1, pipe3 and pipe6 only.*/
mjoun 1:6e512faaa17c 656 #define EP2PIPE(endpoint) ((uint32_t)usbx_function_EpToPipe(endpoint))
mjoun 1:6e512faaa17c 657
mjoun 1:6e512faaa17c 658
mjoun 1:6e512faaa17c 659 /******************************************************************************
mjoun 1:6e512faaa17c 660 * Function Name: usbx_function_save_request
mjoun 1:6e512faaa17c 661 * Description : Retains the USB request information in variables.
mjoun 1:6e512faaa17c 662 * Arguments : none
mjoun 1:6e512faaa17c 663 * Return Value : none
mjoun 1:6e512faaa17c 664 *****************************************************************************/
mjoun 1:6e512faaa17c 665 #define usbx_function_save_request() \
mjoun 1:6e512faaa17c 666 { \
mjoun 1:6e512faaa17c 667 uint16_t *bufO = &setup_buffer[0]; \
mjoun 1:6e512faaa17c 668 \
mjoun 1:6e512faaa17c 669 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITVALID; \
mjoun 1:6e512faaa17c 670 /*data[0] <= bmRequest, data[1] <= bmRequestType */ \
mjoun 1:6e512faaa17c 671 *bufO++ = USB20X.USBREQ; \
mjoun 1:6e512faaa17c 672 /*data[2] data[3] <= wValue*/ \
mjoun 1:6e512faaa17c 673 *bufO++ = USB20X.USBVAL; \
mjoun 1:6e512faaa17c 674 /*data[4] data[5] <= wIndex*/ \
mjoun 1:6e512faaa17c 675 *bufO++ = USB20X.USBINDX; \
mjoun 1:6e512faaa17c 676 /*data[6] data[6] <= wIndex*/ \
mjoun 1:6e512faaa17c 677 *bufO++ = USB20X.USBLENG; \
mjoun 1:6e512faaa17c 678 }
mjoun 1:6e512faaa17c 679
mjoun 1:6e512faaa17c 680
mjoun 1:6e512faaa17c 681 /*************************************************************************/
mjoun 1:6e512faaa17c 682 /*************************************************************************/
mjoun 1:6e512faaa17c 683 /*************************************************************************/
mjoun 1:6e512faaa17c 684
mjoun 1:6e512faaa17c 685 /*************************************************************************/
mjoun 1:6e512faaa17c 686 /* constructor */
mjoun 1:6e512faaa17c 687 USBHAL::USBHAL(void)
mjoun 1:6e512faaa17c 688 {
mjoun 1:6e512faaa17c 689 /* ---- P4_1 : P4_1 (USB0_EN for GR-PEACH) ---- */
mjoun 1:6e512faaa17c 690 //usbx_en = new DigitalOut(P4_1, 1);
mjoun 1:6e512faaa17c 691
mjoun 1:6e512faaa17c 692 /* some constants */
mjoun 1:6e512faaa17c 693 int_id = USBIX_IRQn;
mjoun 1:6e512faaa17c 694 int_level = ( 2 << 3 );
mjoun 1:6e512faaa17c 695 clock_mode = USBFCLOCK_X1_48MHZ;
mjoun 1:6e512faaa17c 696 #if (USB_FUNCTION_HISPEED == 0)
mjoun 1:6e512faaa17c 697 mode = USB_FUNCTION_FULL_SPEED;
mjoun 1:6e512faaa17c 698 #else
mjoun 1:6e512faaa17c 699 mode = USB_FUNCTION_HIGH_SPEED;
mjoun 1:6e512faaa17c 700 #endif
mjoun 1:6e512faaa17c 701 EP0_read_status = DEVDRV_USBF_WRITEEND;
mjoun 1:6e512faaa17c 702 EPx_read_status = DEVDRV_USBF_PIPE_DONE;
mjoun 1:6e512faaa17c 703
mjoun 1:6e512faaa17c 704 /* Disables interrupt for usb */
mjoun 1:6e512faaa17c 705 GIC_DisableIRQ(int_id);
mjoun 1:6e512faaa17c 706
mjoun 1:6e512faaa17c 707 /* Setup the end point */
mjoun 1:6e512faaa17c 708 epCallback[ 0] = &USBHAL::EP1_OUT_callback;
mjoun 1:6e512faaa17c 709 epCallback[ 1] = &USBHAL::EP1_IN_callback;
mjoun 1:6e512faaa17c 710 epCallback[ 2] = &USBHAL::EP2_OUT_callback;
mjoun 1:6e512faaa17c 711 epCallback[ 3] = &USBHAL::EP2_IN_callback;
mjoun 1:6e512faaa17c 712 epCallback[ 4] = &USBHAL::EP3_OUT_callback;
mjoun 1:6e512faaa17c 713 epCallback[ 5] = &USBHAL::EP3_IN_callback;
mjoun 1:6e512faaa17c 714 epCallback[ 6] = &USBHAL::EP4_OUT_callback;
mjoun 1:6e512faaa17c 715 epCallback[ 7] = &USBHAL::EP4_IN_callback;
mjoun 1:6e512faaa17c 716 epCallback[ 8] = &USBHAL::EP5_OUT_callback;
mjoun 1:6e512faaa17c 717 epCallback[ 9] = &USBHAL::EP5_IN_callback;
mjoun 1:6e512faaa17c 718 epCallback[10] = &USBHAL::EP6_OUT_callback;
mjoun 1:6e512faaa17c 719 epCallback[11] = &USBHAL::EP6_IN_callback;
mjoun 1:6e512faaa17c 720 epCallback[12] = &USBHAL::EP7_OUT_callback;
mjoun 1:6e512faaa17c 721 epCallback[13] = &USBHAL::EP7_IN_callback;
mjoun 1:6e512faaa17c 722 epCallback[14] = &USBHAL::EP8_OUT_callback;
mjoun 1:6e512faaa17c 723 epCallback[15] = &USBHAL::EP8_IN_callback;
mjoun 1:6e512faaa17c 724 epCallback[16] = &USBHAL::EP9_OUT_callback;
mjoun 1:6e512faaa17c 725 epCallback[17] = &USBHAL::EP9_IN_callback;
mjoun 1:6e512faaa17c 726 epCallback[18] = &USBHAL::EP10_OUT_callback;
mjoun 1:6e512faaa17c 727 epCallback[19] = &USBHAL::EP10_IN_callback;
mjoun 1:6e512faaa17c 728 epCallback[20] = &USBHAL::EP11_OUT_callback;
mjoun 1:6e512faaa17c 729 epCallback[21] = &USBHAL::EP11_IN_callback;
mjoun 1:6e512faaa17c 730 epCallback[22] = &USBHAL::EP12_OUT_callback;
mjoun 1:6e512faaa17c 731 epCallback[23] = &USBHAL::EP12_IN_callback;
mjoun 1:6e512faaa17c 732 epCallback[24] = &USBHAL::EP13_OUT_callback;
mjoun 1:6e512faaa17c 733 epCallback[25] = &USBHAL::EP13_IN_callback;
mjoun 1:6e512faaa17c 734 epCallback[26] = &USBHAL::EP14_OUT_callback;
mjoun 1:6e512faaa17c 735 epCallback[27] = &USBHAL::EP14_IN_callback;
mjoun 1:6e512faaa17c 736 epCallback[28] = &USBHAL::EP15_OUT_callback;
mjoun 1:6e512faaa17c 737 epCallback[29] = &USBHAL::EP15_IN_callback;
mjoun 1:6e512faaa17c 738
mjoun 1:6e512faaa17c 739 /* registers me */
mjoun 1:6e512faaa17c 740 instance = this;
mjoun 1:6e512faaa17c 741
mjoun 1:6e512faaa17c 742 /* Clear pipe table */
mjoun 1:6e512faaa17c 743 usbx_function_clear_pipe_tbl();
mjoun 1:6e512faaa17c 744
mjoun 1:6e512faaa17c 745 /******************************************************************************
mjoun 1:6e512faaa17c 746 * Function Name: usbx_api_function_init
mjoun 1:6e512faaa17c 747 * Description : Initializes the USB module in the USB function mode.
mjoun 1:6e512faaa17c 748 *****************************************************************************/
mjoun 1:6e512faaa17c 749 /* The clock of USB0 modules is permitted */
mjoun 1:6e512faaa17c 750 #if (USB_FUNCTION_CH == 0)
mjoun 1:6e512faaa17c 751 CPG.STBCR7 &= ~(CPG_STBCR7_MSTP71);
mjoun 1:6e512faaa17c 752 #else
mjoun 1:6e512faaa17c 753 CPG.STBCR7 &= ~(CPG_STBCR7_MSTP71 | CPG_STBCR7_MSTP70);
mjoun 1:6e512faaa17c 754 #endif
mjoun 1:6e512faaa17c 755 volatile uint8_t dummy8;
mjoun 1:6e512faaa17c 756 dummy8 = CPG.STBCR7;
mjoun 1:6e512faaa17c 757
mjoun 1:6e512faaa17c 758 {
mjoun 1:6e512faaa17c 759 /******************************************************************************
mjoun 1:6e512faaa17c 760 * Function Name: usbx_function_setting_interrupt
mjoun 1:6e512faaa17c 761 * Description : Sets the USB module interrupt level.
mjoun 1:6e512faaa17c 762 *****************************************************************************/
mjoun 1:6e512faaa17c 763 #if 0 /*DMA is not supported*/
mjoun 1:6e512faaa17c 764 IRQn_Type d0fifo_dmaintid;
mjoun 1:6e512faaa17c 765 IRQn_Type d1fifo_dmaintid;
mjoun 1:6e512faaa17c 766 #endif
mjoun 1:6e512faaa17c 767
mjoun 1:6e512faaa17c 768 InterruptHandlerRegister(int_id, &_usbisr);
mjoun 1:6e512faaa17c 769 GIC_SetPriority(int_id, int_level);
mjoun 1:6e512faaa17c 770 GIC_EnableIRQ(int_id);
mjoun 1:6e512faaa17c 771
mjoun 1:6e512faaa17c 772 #if 0 /*DMA is not supported*/
mjoun 1:6e512faaa17c 773 d0fifo_dmaintid = Userdef_USB_usbx_function_d0fifo_dmaintid();
mjoun 1:6e512faaa17c 774 if (d0fifo_dmaintid != 0xFFFF) {
mjoun 1:6e512faaa17c 775 InterruptHandlerRegister(d0fifo_dmaintid, usbx_function_dma_interrupt_d0fifo);
mjoun 1:6e512faaa17c 776 GIC_SetPriority(d0fifo_dmaintid, int_level);
mjoun 1:6e512faaa17c 777 GIC_EnableIRQ(d0fifo_dmaintid);
mjoun 1:6e512faaa17c 778 }
mjoun 1:6e512faaa17c 779 #endif
mjoun 1:6e512faaa17c 780
mjoun 1:6e512faaa17c 781 #if 0 /*DMA is not supported*/
mjoun 1:6e512faaa17c 782 d1fifo_dmaintid = Userdef_USB_usbx_function_d1fifo_dmaintid();
mjoun 1:6e512faaa17c 783 if (d1fifo_dmaintid != 0xFFFF) {
mjoun 1:6e512faaa17c 784 InterruptHandlerRegister(d1fifo_dmaintid, usbx_function_dma_interrupt_d1fifo);
mjoun 1:6e512faaa17c 785 GIC_SetPriority(d1fifo_dmaintid, int_level);
mjoun 1:6e512faaa17c 786 GIC_EnableIRQ(d1fifo_dmaintid);
mjoun 1:6e512faaa17c 787 }
mjoun 1:6e512faaa17c 788 #endif
mjoun 1:6e512faaa17c 789 /*****************************************************************************/
mjoun 1:6e512faaa17c 790 }
mjoun 1:6e512faaa17c 791
mjoun 1:6e512faaa17c 792 /* reset USB module with setting tranciever and HSE=1 */
mjoun 1:6e512faaa17c 793 usbx_function_reset_module(clock_mode);
mjoun 1:6e512faaa17c 794
mjoun 1:6e512faaa17c 795 /* clear variables */
mjoun 1:6e512faaa17c 796 usbx_function_init_status();
mjoun 1:6e512faaa17c 797
mjoun 1:6e512faaa17c 798 /* select USB Function and Interrupt Enable */
mjoun 1:6e512faaa17c 799 /* Detect USB Device to attach or detach */
mjoun 1:6e512faaa17c 800 usbx_function_InitModule(mode);
mjoun 1:6e512faaa17c 801
mjoun 1:6e512faaa17c 802 {
mjoun 1:6e512faaa17c 803 uint16_t buf;
mjoun 1:6e512faaa17c 804 buf = USB20X.INTENB0;
mjoun 1:6e512faaa17c 805 buf |= USB_INTENB0_SOFE;
mjoun 1:6e512faaa17c 806 USB20X.INTENB0 = buf;
mjoun 1:6e512faaa17c 807 }
mjoun 1:6e512faaa17c 808 }
mjoun 1:6e512faaa17c 809
mjoun 1:6e512faaa17c 810 /*************************************************************************/
mjoun 1:6e512faaa17c 811 USBHAL::~USBHAL(void)
mjoun 1:6e512faaa17c 812 {
mjoun 1:6e512faaa17c 813 /* Disables interrupt for usb */
mjoun 1:6e512faaa17c 814 GIC_DisableIRQ( int_id );
mjoun 1:6e512faaa17c 815 /* Unregisters interrupt function and priority */
mjoun 1:6e512faaa17c 816 InterruptHandlerRegister( int_id, (uint32_t)NULL );
mjoun 1:6e512faaa17c 817
mjoun 1:6e512faaa17c 818 //usbx_en = NULL;
mjoun 1:6e512faaa17c 819 instance = NULL;
mjoun 1:6e512faaa17c 820 }
mjoun 1:6e512faaa17c 821
mjoun 1:6e512faaa17c 822 /*************************************************************************/
mjoun 1:6e512faaa17c 823 void USBHAL::connect(void)
mjoun 1:6e512faaa17c 824 {
mjoun 1:6e512faaa17c 825 /* Activates USB0_EN */
mjoun 1:6e512faaa17c 826 //(*usbx_en) = 0;
mjoun 1:6e512faaa17c 827 }
mjoun 1:6e512faaa17c 828
mjoun 1:6e512faaa17c 829
mjoun 1:6e512faaa17c 830 /*************************************************************************/
mjoun 1:6e512faaa17c 831 void USBHAL::disconnect(void)
mjoun 1:6e512faaa17c 832 {
mjoun 1:6e512faaa17c 833 /* Deactivates USB0_EN */
mjoun 1:6e512faaa17c 834 //(*usbx_en) = 1;
mjoun 1:6e512faaa17c 835 }
mjoun 1:6e512faaa17c 836
mjoun 1:6e512faaa17c 837
mjoun 1:6e512faaa17c 838 /*************************************************************************/
mjoun 1:6e512faaa17c 839 void USBHAL::configureDevice(void)
mjoun 1:6e512faaa17c 840 {
mjoun 1:6e512faaa17c 841 /*The pipes set up in USBHAL::realiseEndpoint*/
mjoun 1:6e512faaa17c 842 /*usbx_function_clear_alt();*/ /* Alternate setting clear */
mjoun 1:6e512faaa17c 843 /*usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);*/
mjoun 1:6e512faaa17c 844 }
mjoun 1:6e512faaa17c 845
mjoun 1:6e512faaa17c 846
mjoun 1:6e512faaa17c 847 /*************************************************************************/
mjoun 1:6e512faaa17c 848 void USBHAL::unconfigureDevice(void)
mjoun 1:6e512faaa17c 849 {
mjoun 1:6e512faaa17c 850 /* The Interface would be managed by USBDevice */
mjoun 1:6e512faaa17c 851 /*usbx_function_clear_alt();*/ /* Alternate setting clear */
mjoun 1:6e512faaa17c 852 /*usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);*/
mjoun 1:6e512faaa17c 853 }
mjoun 1:6e512faaa17c 854
mjoun 1:6e512faaa17c 855
mjoun 1:6e512faaa17c 856 /*************************************************************************/
mjoun 1:6e512faaa17c 857 void USBHAL::setAddress(uint8_t address)
mjoun 1:6e512faaa17c 858 {
mjoun 1:6e512faaa17c 859 if (address <= 127) {
mjoun 1:6e512faaa17c 860 usbx_function_set_pid_buf(USB_FUNCTION_PIPE0); /* OK */
mjoun 1:6e512faaa17c 861 } else {
mjoun 1:6e512faaa17c 862 usbx_function_set_pid_stall(USB_FUNCTION_PIPE0); /* Not Spec */
mjoun 1:6e512faaa17c 863 }
mjoun 1:6e512faaa17c 864 }
mjoun 1:6e512faaa17c 865
mjoun 1:6e512faaa17c 866
mjoun 1:6e512faaa17c 867 /*************************************************************************/
mjoun 1:6e512faaa17c 868 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags)
mjoun 1:6e512faaa17c 869 {
mjoun 1:6e512faaa17c 870 const struct PIPECFGREC *cfg;
mjoun 1:6e512faaa17c 871 uint16_t pipe;
mjoun 1:6e512faaa17c 872 uint16_t buf;
mjoun 1:6e512faaa17c 873
mjoun 1:6e512faaa17c 874 if ( (EP0OUT == endpoint) || (EP0IN == endpoint) ) {
mjoun 1:6e512faaa17c 875 return true;
mjoun 1:6e512faaa17c 876 }
mjoun 1:6e512faaa17c 877
mjoun 1:6e512faaa17c 878 for (cfg = &def_pipecfg[0]; cfg->pipesel != 0; cfg++) {
mjoun 1:6e512faaa17c 879 if (cfg->endpoint == endpoint) {
mjoun 1:6e512faaa17c 880 break;
mjoun 1:6e512faaa17c 881 }
mjoun 1:6e512faaa17c 882 }
mjoun 1:6e512faaa17c 883 if (cfg->pipesel == 0) {
mjoun 1:6e512faaa17c 884 return false;
mjoun 1:6e512faaa17c 885 }
mjoun 1:6e512faaa17c 886
mjoun 1:6e512faaa17c 887 pipe = ((cfg->pipesel & USB_PIPESEL_PIPESEL) >> USB_PIPESEL_PIPESEL_SHIFT);
mjoun 1:6e512faaa17c 888
mjoun 1:6e512faaa17c 889 g_usbx_function_PipeTbl[ pipe ] = (uint16_t)(endpoint | ((cfg->pipesel & USB_FUNCTION_FIFO_USE) << 0));
mjoun 1:6e512faaa17c 890
mjoun 1:6e512faaa17c 891 /* There are maintenance routine of SHTNAK and BFRE bits
mjoun 1:6e512faaa17c 892 * in original sample program. This sample is not
mjoun 1:6e512faaa17c 893 * programmed. Do maintenance the "def_pipecfg" array if
mjoun 1:6e512faaa17c 894 * you want it. */
mjoun 1:6e512faaa17c 895
mjoun 1:6e512faaa17c 896 /* Interrupt Disable */
mjoun 1:6e512faaa17c 897 buf = USB20X.BRDYENB;
mjoun 1:6e512faaa17c 898 buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
mjoun 1:6e512faaa17c 899 USB20X.BRDYENB = buf;
mjoun 1:6e512faaa17c 900 buf = USB20X.NRDYENB;
mjoun 1:6e512faaa17c 901 buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
mjoun 1:6e512faaa17c 902 USB20X.NRDYENB = buf;
mjoun 1:6e512faaa17c 903 buf = USB20X.BEMPENB;
mjoun 1:6e512faaa17c 904 buf &= (uint16_t)~g_usbx_function_bit_set[pipe];
mjoun 1:6e512faaa17c 905 USB20X.BEMPENB = buf;
mjoun 1:6e512faaa17c 906
mjoun 1:6e512faaa17c 907 usbx_function_set_pid_nak(pipe);
mjoun 1:6e512faaa17c 908
mjoun 1:6e512faaa17c 909 /* CurrentPIPE Clear */
mjoun 1:6e512faaa17c 910 if (RZA_IO_RegRead_16(&USB20X.CFIFOSEL, USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE) == pipe) {
mjoun 1:6e512faaa17c 911 RZA_IO_RegWrite_16(&USB20X.CFIFOSEL, 0, USB_CFIFOSEL_CURPIPE_SHIFT, USB_CFIFOSEL_CURPIPE);
mjoun 1:6e512faaa17c 912 }
mjoun 1:6e512faaa17c 913
mjoun 1:6e512faaa17c 914 if (RZA_IO_RegRead_16(&USB20X.D0FIFOSEL, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE) == pipe) {
mjoun 1:6e512faaa17c 915 RZA_IO_RegWrite_16(&USB20X.D0FIFOSEL, 0, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE);
mjoun 1:6e512faaa17c 916 }
mjoun 1:6e512faaa17c 917
mjoun 1:6e512faaa17c 918 if (RZA_IO_RegRead_16(&USB20X.D1FIFOSEL, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE) == pipe) {
mjoun 1:6e512faaa17c 919 RZA_IO_RegWrite_16(&USB20X.D1FIFOSEL, 0, USB_DnFIFOSEL_CURPIPE_SHIFT, USB_DnFIFOSEL_CURPIPE);
mjoun 1:6e512faaa17c 920 }
mjoun 1:6e512faaa17c 921
mjoun 1:6e512faaa17c 922 /* PIPE Configuration */
mjoun 1:6e512faaa17c 923 USB20X.PIPESEL = pipe;
mjoun 1:6e512faaa17c 924 USB20X.PIPECFG = cfg->pipecfg;
mjoun 1:6e512faaa17c 925 USB20X.PIPEBUF = cfg->pipebuf;
mjoun 1:6e512faaa17c 926 USB20X.PIPEMAXP = cfg->pipemaxp;
mjoun 1:6e512faaa17c 927 USB20X.PIPEPERI = cfg->pipeperi;
mjoun 1:6e512faaa17c 928
mjoun 1:6e512faaa17c 929 g_usbx_function_pipecfg[pipe] = cfg->pipecfg;
mjoun 1:6e512faaa17c 930 g_usbx_function_pipebuf[pipe] = cfg->pipebuf;
mjoun 1:6e512faaa17c 931 g_usbx_function_pipemaxp[pipe] = cfg->pipemaxp;
mjoun 1:6e512faaa17c 932 g_usbx_function_pipeperi[pipe] = cfg->pipeperi;
mjoun 1:6e512faaa17c 933
mjoun 1:6e512faaa17c 934 /* Buffer Clear */
mjoun 1:6e512faaa17c 935 usbx_function_set_sqclr(pipe);
mjoun 1:6e512faaa17c 936 usbx_function_aclrm(pipe);
mjoun 1:6e512faaa17c 937
mjoun 1:6e512faaa17c 938 /* init Global */
mjoun 1:6e512faaa17c 939 g_usbx_function_pipe_status[pipe] = DEVDRV_USBF_PIPE_IDLE;
mjoun 1:6e512faaa17c 940 g_usbx_function_PipeDataSize[pipe] = 0;
mjoun 1:6e512faaa17c 941
mjoun 1:6e512faaa17c 942 return true;
mjoun 1:6e512faaa17c 943 }
mjoun 1:6e512faaa17c 944
mjoun 1:6e512faaa17c 945
mjoun 1:6e512faaa17c 946 /*************************************************************************/
mjoun 1:6e512faaa17c 947 // read setup packet
mjoun 1:6e512faaa17c 948 void USBHAL::EP0setup(uint8_t *buffer)
mjoun 1:6e512faaa17c 949 {
mjoun 1:6e512faaa17c 950 memcpy(buffer, setup_buffer, MAX_PACKET_SIZE_EP0);
mjoun 1:6e512faaa17c 951 }
mjoun 1:6e512faaa17c 952
mjoun 1:6e512faaa17c 953
mjoun 1:6e512faaa17c 954 /*************************************************************************/
mjoun 1:6e512faaa17c 955 void USBHAL::EP0readStage(void)
mjoun 1:6e512faaa17c 956 {
mjoun 1:6e512faaa17c 957 // No implements
mjoun 1:6e512faaa17c 958 }
mjoun 1:6e512faaa17c 959
mjoun 1:6e512faaa17c 960
mjoun 1:6e512faaa17c 961 /*************************************************************************/
mjoun 1:6e512faaa17c 962 void USBHAL::EP0read(void)
mjoun 1:6e512faaa17c 963 {
mjoun 1:6e512faaa17c 964 uint8_t *buffer;
mjoun 1:6e512faaa17c 965 uint32_t size;
mjoun 1:6e512faaa17c 966
mjoun 1:6e512faaa17c 967 /* remain of last writing */
mjoun 1:6e512faaa17c 968 while (EP0_read_status != DEVDRV_USBF_WRITEEND) {
mjoun 1:6e512faaa17c 969 static uint8_t bbb[2] = { 255, 255 };
mjoun 1:6e512faaa17c 970 EP0write(&bbb[0], 0);
mjoun 1:6e512faaa17c 971 }
mjoun 1:6e512faaa17c 972
mjoun 1:6e512faaa17c 973 buffer = (uint8_t*)(&setup_buffer[4]);
mjoun 1:6e512faaa17c 974 size = (MAX_PACKET_SIZE_EP0 / 2) - 8;
mjoun 1:6e512faaa17c 975 usbx_api_function_CtrlWriteStart(size, buffer);
mjoun 1:6e512faaa17c 976 }
mjoun 1:6e512faaa17c 977
mjoun 1:6e512faaa17c 978
mjoun 1:6e512faaa17c 979 /*************************************************************************/
mjoun 1:6e512faaa17c 980 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
mjoun 1:6e512faaa17c 981 {
mjoun 1:6e512faaa17c 982 memcpy(buffer, (uint8_t*)(&setup_buffer[4]), g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0]);
mjoun 1:6e512faaa17c 983
mjoun 1:6e512faaa17c 984 return g_usbx_function_PipeDataSize[USB_FUNCTION_PIPE0];
mjoun 1:6e512faaa17c 985 }
mjoun 1:6e512faaa17c 986
mjoun 1:6e512faaa17c 987
mjoun 1:6e512faaa17c 988 /*************************************************************************/
mjoun 1:6e512faaa17c 989 void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
mjoun 1:6e512faaa17c 990 {
mjoun 1:6e512faaa17c 991 /* zero byte writing */
mjoun 1:6e512faaa17c 992 if ( (size == 0) && (EP0_read_status == DEVDRV_USBF_WRITEEND) ) {
mjoun 1:6e512faaa17c 993 return;
mjoun 1:6e512faaa17c 994 }
mjoun 1:6e512faaa17c 995
mjoun 1:6e512faaa17c 996 if (EP0_read_status == DEVDRV_USBF_WRITEEND) {
mjoun 1:6e512faaa17c 997 /*1st block*/
mjoun 1:6e512faaa17c 998 EP0_read_status = usbx_api_function_CtrlReadStart(size, buffer);
mjoun 1:6e512faaa17c 999 } else {
mjoun 1:6e512faaa17c 1000 /* waits the last transmission */
mjoun 1:6e512faaa17c 1001 /*other blocks*/
mjoun 1:6e512faaa17c 1002 g_usbx_function_data_count[ USB_FUNCTION_PIPE0 ] = size;
mjoun 1:6e512faaa17c 1003 g_usbx_function_data_pointer [ USB_FUNCTION_PIPE0 ] = buffer;
mjoun 1:6e512faaa17c 1004 EP0_read_status = usbx_function_write_buffer_c(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 1005 }
mjoun 1:6e512faaa17c 1006 /*max size may be deblocking outside*/
mjoun 1:6e512faaa17c 1007 if (size == MAX_PACKET_SIZE_EP0) {
mjoun 1:6e512faaa17c 1008 EP0_read_status = DEVDRV_USBF_WRITING;
mjoun 1:6e512faaa17c 1009 }
mjoun 1:6e512faaa17c 1010 }
mjoun 1:6e512faaa17c 1011
mjoun 1:6e512faaa17c 1012
mjoun 1:6e512faaa17c 1013 /*************************************************************************/
mjoun 1:6e512faaa17c 1014 #if 0 // No implements
mjoun 1:6e512faaa17c 1015 void USBHAL::EP0getWriteResult(void)
mjoun 1:6e512faaa17c 1016 {
mjoun 1:6e512faaa17c 1017 }
mjoun 1:6e512faaa17c 1018 #endif
mjoun 1:6e512faaa17c 1019
mjoun 1:6e512faaa17c 1020 /*************************************************************************/
mjoun 1:6e512faaa17c 1021 void USBHAL::EP0stall(void)
mjoun 1:6e512faaa17c 1022 {
mjoun 1:6e512faaa17c 1023 stallEndpoint( 0 );
mjoun 1:6e512faaa17c 1024 }
mjoun 1:6e512faaa17c 1025
mjoun 1:6e512faaa17c 1026
mjoun 1:6e512faaa17c 1027 /*************************************************************************/
mjoun 1:6e512faaa17c 1028 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t max_size)
mjoun 1:6e512faaa17c 1029 {
mjoun 1:6e512faaa17c 1030 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1031 uint32_t pipe_size;
mjoun 1:6e512faaa17c 1032 uint16_t pipe_status;
mjoun 1:6e512faaa17c 1033 EP_STATUS status = EP_COMPLETED;
mjoun 1:6e512faaa17c 1034
mjoun 1:6e512faaa17c 1035 pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
mjoun 1:6e512faaa17c 1036
mjoun 1:6e512faaa17c 1037 switch (pipe_status) {
mjoun 1:6e512faaa17c 1038 case DEVDRV_USBF_PIPE_IDLE:
mjoun 1:6e512faaa17c 1039 case DEVDRV_USBF_PIPE_WAIT:
mjoun 1:6e512faaa17c 1040 usbx_api_function_set_pid_nak(pipe);
mjoun 1:6e512faaa17c 1041 usbx_api_function_clear_pipe_status(pipe);
mjoun 1:6e512faaa17c 1042
mjoun 1:6e512faaa17c 1043 usbx_api_function_start_receive_transfer(pipe, max_size, recv_buffer);
mjoun 1:6e512faaa17c 1044 break;
mjoun 1:6e512faaa17c 1045
mjoun 1:6e512faaa17c 1046 default:
mjoun 1:6e512faaa17c 1047 status = EP_PENDING;
mjoun 1:6e512faaa17c 1048 break;
mjoun 1:6e512faaa17c 1049 }
mjoun 1:6e512faaa17c 1050
mjoun 1:6e512faaa17c 1051 return status;
mjoun 1:6e512faaa17c 1052 }
mjoun 1:6e512faaa17c 1053
mjoun 1:6e512faaa17c 1054
mjoun 1:6e512faaa17c 1055 /*************************************************************************/
mjoun 1:6e512faaa17c 1056 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *buffer, uint32_t *bytes_read )
mjoun 1:6e512faaa17c 1057 {
mjoun 1:6e512faaa17c 1058 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1059 uint16_t pipe_status;
mjoun 1:6e512faaa17c 1060 uint16_t err;
mjoun 1:6e512faaa17c 1061 EP_STATUS status = EP_PENDING;
mjoun 1:6e512faaa17c 1062
mjoun 1:6e512faaa17c 1063
mjoun 1:6e512faaa17c 1064 if (EPx_read_status != DEVDRV_USBF_PIPE_WAIT) {
mjoun 1:6e512faaa17c 1065 return status;
mjoun 1:6e512faaa17c 1066 }
mjoun 1:6e512faaa17c 1067
mjoun 1:6e512faaa17c 1068 pipe_status = usbx_api_function_check_pipe_status(pipe, bytes_read);
mjoun 1:6e512faaa17c 1069 switch (pipe_status) {
mjoun 1:6e512faaa17c 1070 case DEVDRV_USBF_PIPE_IDLE:
mjoun 1:6e512faaa17c 1071 return EP_COMPLETED;
mjoun 1:6e512faaa17c 1072
mjoun 1:6e512faaa17c 1073 case DEVDRV_USBF_PIPE_DONE:
mjoun 1:6e512faaa17c 1074 return EP_COMPLETED;
mjoun 1:6e512faaa17c 1075
mjoun 1:6e512faaa17c 1076 case DEVDRV_USBF_PIPE_WAIT:
mjoun 1:6e512faaa17c 1077 break;
mjoun 1:6e512faaa17c 1078
mjoun 1:6e512faaa17c 1079 default:
mjoun 1:6e512faaa17c 1080 return status;
mjoun 1:6e512faaa17c 1081 }
mjoun 1:6e512faaa17c 1082
mjoun 1:6e512faaa17c 1083 /* sets the output buffer and size */
mjoun 1:6e512faaa17c 1084 g_usbx_function_data_pointer[pipe] = buffer;
mjoun 1:6e512faaa17c 1085
mjoun 1:6e512faaa17c 1086 /* receives data from pipe */
mjoun 1:6e512faaa17c 1087 err = usbx_function_read_buffer(pipe);
mjoun 1:6e512faaa17c 1088 recv_error = err;
mjoun 1:6e512faaa17c 1089 switch (err) {
mjoun 1:6e512faaa17c 1090 case USB_FUNCTION_READEND:
mjoun 1:6e512faaa17c 1091 case USB_FUNCTION_READSHRT:
mjoun 1:6e512faaa17c 1092 case USB_FUNCTION_READOVER:
mjoun 1:6e512faaa17c 1093 *bytes_read = g_usbx_function_PipeDataSize[pipe];
mjoun 1:6e512faaa17c 1094 break;
mjoun 1:6e512faaa17c 1095
mjoun 1:6e512faaa17c 1096 case USB_FUNCTION_READING:
mjoun 1:6e512faaa17c 1097 case DEVDRV_USBF_FIFOERROR:
mjoun 1:6e512faaa17c 1098 break;
mjoun 1:6e512faaa17c 1099 }
mjoun 1:6e512faaa17c 1100
mjoun 1:6e512faaa17c 1101 pipe_status = usbx_api_function_check_pipe_status(pipe, bytes_read);
mjoun 1:6e512faaa17c 1102 switch (pipe_status) {
mjoun 1:6e512faaa17c 1103 case DEVDRV_USBF_PIPE_DONE:
mjoun 1:6e512faaa17c 1104 status = EP_COMPLETED;
mjoun 1:6e512faaa17c 1105 break;
mjoun 1:6e512faaa17c 1106
mjoun 1:6e512faaa17c 1107 case DEVDRV_USBF_PIPE_IDLE:
mjoun 1:6e512faaa17c 1108 case DEVDRV_USBF_PIPE_NORES:
mjoun 1:6e512faaa17c 1109 case DEVDRV_USBF_PIPE_STALL:
mjoun 1:6e512faaa17c 1110 case DEVDRV_USBF_FIFOERROR:
mjoun 1:6e512faaa17c 1111 default:
mjoun 1:6e512faaa17c 1112 break;
mjoun 1:6e512faaa17c 1113 }
mjoun 1:6e512faaa17c 1114
mjoun 1:6e512faaa17c 1115 return status;
mjoun 1:6e512faaa17c 1116 }
mjoun 1:6e512faaa17c 1117
mjoun 1:6e512faaa17c 1118
mjoun 1:6e512faaa17c 1119 /*************************************************************************/
mjoun 1:6e512faaa17c 1120 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
mjoun 1:6e512faaa17c 1121 {
mjoun 1:6e512faaa17c 1122 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1123 uint32_t pipe_size;
mjoun 1:6e512faaa17c 1124 uint16_t pipe_status;
mjoun 1:6e512faaa17c 1125 uint16_t err;
mjoun 1:6e512faaa17c 1126 uint16_t count;
mjoun 1:6e512faaa17c 1127 EP_STATUS status = EP_PENDING;
mjoun 1:6e512faaa17c 1128
mjoun 1:6e512faaa17c 1129 pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
mjoun 1:6e512faaa17c 1130
mjoun 1:6e512faaa17c 1131 /* waits the last transmission */
mjoun 1:6e512faaa17c 1132 count = 30000;
mjoun 1:6e512faaa17c 1133 while ((pipe_status == DEVDRV_USBF_PIPE_WAIT) || (pipe_status == DEVDRV_USBF_PIPE_DONE)) {
mjoun 1:6e512faaa17c 1134 pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
mjoun 1:6e512faaa17c 1135 if( --count == 0 ) {
mjoun 1:6e512faaa17c 1136 pipe_status = DEVDRV_USBF_PIPE_STALL;
mjoun 1:6e512faaa17c 1137 break;
mjoun 1:6e512faaa17c 1138 }
mjoun 1:6e512faaa17c 1139 }
mjoun 1:6e512faaa17c 1140
mjoun 1:6e512faaa17c 1141 switch (pipe_status) {
mjoun 1:6e512faaa17c 1142 case DEVDRV_USBF_PIPE_IDLE:
mjoun 1:6e512faaa17c 1143 err = usbx_api_function_start_send_transfer(pipe, size, data);
mjoun 1:6e512faaa17c 1144
mjoun 1:6e512faaa17c 1145 switch (err) {
mjoun 1:6e512faaa17c 1146 /* finish to write */
mjoun 1:6e512faaa17c 1147 case DEVDRV_USBF_WRITEEND:
mjoun 1:6e512faaa17c 1148 /* finish to write, but data is short */
mjoun 1:6e512faaa17c 1149 case DEVDRV_USBF_WRITESHRT:
mjoun 1:6e512faaa17c 1150 /* continue to write */
mjoun 1:6e512faaa17c 1151 case DEVDRV_USBF_WRITING:
mjoun 1:6e512faaa17c 1152 /* use DMA */
mjoun 1:6e512faaa17c 1153 case DEVDRV_USBF_WRITEDMA:
mjoun 1:6e512faaa17c 1154 /* error */
mjoun 1:6e512faaa17c 1155 case DEVDRV_USBF_FIFOERROR:
mjoun 1:6e512faaa17c 1156 status = EP_PENDING;
mjoun 1:6e512faaa17c 1157 break;
mjoun 1:6e512faaa17c 1158 }
mjoun 1:6e512faaa17c 1159 break;
mjoun 1:6e512faaa17c 1160
mjoun 1:6e512faaa17c 1161 case DEVDRV_USBF_PIPE_WAIT:
mjoun 1:6e512faaa17c 1162 case DEVDRV_USBF_PIPE_DONE:
mjoun 1:6e512faaa17c 1163 status = EP_PENDING;
mjoun 1:6e512faaa17c 1164 break;
mjoun 1:6e512faaa17c 1165
mjoun 1:6e512faaa17c 1166 case DEVDRV_USBF_PIPE_NORES:
mjoun 1:6e512faaa17c 1167 case DEVDRV_USBF_PIPE_STALL:
mjoun 1:6e512faaa17c 1168 default:
mjoun 1:6e512faaa17c 1169 status = EP_STALLED;
mjoun 1:6e512faaa17c 1170 break;
mjoun 1:6e512faaa17c 1171 }
mjoun 1:6e512faaa17c 1172
mjoun 1:6e512faaa17c 1173 return status;
mjoun 1:6e512faaa17c 1174 }
mjoun 1:6e512faaa17c 1175
mjoun 1:6e512faaa17c 1176
mjoun 1:6e512faaa17c 1177 /*************************************************************************/
mjoun 1:6e512faaa17c 1178 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
mjoun 1:6e512faaa17c 1179 {
mjoun 1:6e512faaa17c 1180 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1181 uint32_t pipe_size;
mjoun 1:6e512faaa17c 1182 uint16_t pipe_status;
mjoun 1:6e512faaa17c 1183 EP_STATUS status = EP_PENDING;
mjoun 1:6e512faaa17c 1184
mjoun 1:6e512faaa17c 1185 pipe_status = usbx_api_function_check_pipe_status(pipe, &pipe_size);
mjoun 1:6e512faaa17c 1186
mjoun 1:6e512faaa17c 1187 switch (pipe_status) {
mjoun 1:6e512faaa17c 1188 case DEVDRV_USBF_PIPE_IDLE:
mjoun 1:6e512faaa17c 1189 status = EP_COMPLETED;
mjoun 1:6e512faaa17c 1190 break;
mjoun 1:6e512faaa17c 1191
mjoun 1:6e512faaa17c 1192 case DEVDRV_USBF_PIPE_WAIT:
mjoun 1:6e512faaa17c 1193 status = EP_PENDING;
mjoun 1:6e512faaa17c 1194 break;
mjoun 1:6e512faaa17c 1195
mjoun 1:6e512faaa17c 1196 case DEVDRV_USBF_PIPE_DONE:
mjoun 1:6e512faaa17c 1197 usbx_function_stop_transfer(pipe);
mjoun 1:6e512faaa17c 1198 status = EP_COMPLETED;
mjoun 1:6e512faaa17c 1199 break;
mjoun 1:6e512faaa17c 1200
mjoun 1:6e512faaa17c 1201 case DEVDRV_USBF_PIPE_NORES:
mjoun 1:6e512faaa17c 1202 status = EP_STALLED;
mjoun 1:6e512faaa17c 1203 break;
mjoun 1:6e512faaa17c 1204
mjoun 1:6e512faaa17c 1205 case DEVDRV_USBF_PIPE_STALL:
mjoun 1:6e512faaa17c 1206 status = EP_STALLED;
mjoun 1:6e512faaa17c 1207 break;
mjoun 1:6e512faaa17c 1208
mjoun 1:6e512faaa17c 1209 default:
mjoun 1:6e512faaa17c 1210 status = EP_PENDING;
mjoun 1:6e512faaa17c 1211 }
mjoun 1:6e512faaa17c 1212
mjoun 1:6e512faaa17c 1213 return status;
mjoun 1:6e512faaa17c 1214 }
mjoun 1:6e512faaa17c 1215
mjoun 1:6e512faaa17c 1216
mjoun 1:6e512faaa17c 1217 /*************************************************************************/
mjoun 1:6e512faaa17c 1218 void USBHAL::stallEndpoint(uint8_t endpoint)
mjoun 1:6e512faaa17c 1219 {
mjoun 1:6e512faaa17c 1220 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1221
mjoun 1:6e512faaa17c 1222 usbx_function_clear_pid_stall(pipe);
mjoun 1:6e512faaa17c 1223 }
mjoun 1:6e512faaa17c 1224
mjoun 1:6e512faaa17c 1225
mjoun 1:6e512faaa17c 1226 /*************************************************************************/
mjoun 1:6e512faaa17c 1227 void USBHAL::unstallEndpoint(uint8_t endpoint)
mjoun 1:6e512faaa17c 1228 {
mjoun 1:6e512faaa17c 1229 uint32_t pipe = EP2PIPE(endpoint);
mjoun 1:6e512faaa17c 1230
mjoun 1:6e512faaa17c 1231 usbx_function_set_pid_stall( pipe );
mjoun 1:6e512faaa17c 1232 }
mjoun 1:6e512faaa17c 1233
mjoun 1:6e512faaa17c 1234
mjoun 1:6e512faaa17c 1235 /*************************************************************************/
mjoun 1:6e512faaa17c 1236 bool USBHAL::getEndpointStallState(uint8_t endpoint)
mjoun 1:6e512faaa17c 1237 {
mjoun 1:6e512faaa17c 1238 // No implemens
mjoun 1:6e512faaa17c 1239 return false;
mjoun 1:6e512faaa17c 1240 }
mjoun 1:6e512faaa17c 1241
mjoun 1:6e512faaa17c 1242
mjoun 1:6e512faaa17c 1243 /*************************************************************************/
mjoun 1:6e512faaa17c 1244 #if 0 // No implements
mjoun 1:6e512faaa17c 1245 void USBHAL::remoteWakeup(void)
mjoun 1:6e512faaa17c 1246 {
mjoun 1:6e512faaa17c 1247 }
mjoun 1:6e512faaa17c 1248 #endif
mjoun 1:6e512faaa17c 1249
mjoun 1:6e512faaa17c 1250 /*************************************************************************/
mjoun 1:6e512faaa17c 1251 void USBHAL::_usbisr(void)
mjoun 1:6e512faaa17c 1252 {
mjoun 1:6e512faaa17c 1253 instance->usbisr();
mjoun 1:6e512faaa17c 1254 }
mjoun 1:6e512faaa17c 1255
mjoun 1:6e512faaa17c 1256
mjoun 1:6e512faaa17c 1257 /*************************************************************************/
mjoun 1:6e512faaa17c 1258 void USBHAL::usbisr(void)
mjoun 1:6e512faaa17c 1259 {
mjoun 1:6e512faaa17c 1260 uint16_t int_sts0;
mjoun 1:6e512faaa17c 1261 uint16_t int_sts1;
mjoun 1:6e512faaa17c 1262 uint16_t int_sts2;
mjoun 1:6e512faaa17c 1263 uint16_t int_sts3;
mjoun 1:6e512faaa17c 1264 uint16_t int_enb0;
mjoun 1:6e512faaa17c 1265 uint16_t int_enb2;
mjoun 1:6e512faaa17c 1266 uint16_t int_enb3;
mjoun 1:6e512faaa17c 1267 uint16_t int_enb4;
mjoun 1:6e512faaa17c 1268 volatile uint16_t dumy_sts;
mjoun 1:6e512faaa17c 1269
mjoun 1:6e512faaa17c 1270
mjoun 1:6e512faaa17c 1271 int_sts0 = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1272
mjoun 1:6e512faaa17c 1273 if (!(int_sts0 & (
mjoun 1:6e512faaa17c 1274 USB_FUNCTION_BITVBINT |
mjoun 1:6e512faaa17c 1275 USB_FUNCTION_BITRESM |
mjoun 1:6e512faaa17c 1276 USB_FUNCTION_BITSOFR |
mjoun 1:6e512faaa17c 1277 USB_FUNCTION_BITDVST |
mjoun 1:6e512faaa17c 1278 USB_FUNCTION_BITCTRT |
mjoun 1:6e512faaa17c 1279 USB_FUNCTION_BITBEMP |
mjoun 1:6e512faaa17c 1280 USB_FUNCTION_BITNRDY |
mjoun 1:6e512faaa17c 1281 USB_FUNCTION_BITBRDY ))) {
mjoun 1:6e512faaa17c 1282 return;
mjoun 1:6e512faaa17c 1283 }
mjoun 1:6e512faaa17c 1284
mjoun 1:6e512faaa17c 1285 int_sts1 = USB20X.BRDYSTS;
mjoun 1:6e512faaa17c 1286 int_sts2 = USB20X.NRDYSTS;
mjoun 1:6e512faaa17c 1287 int_sts3 = USB20X.BEMPSTS;
mjoun 1:6e512faaa17c 1288 int_enb0 = USB20X.INTENB0;
mjoun 1:6e512faaa17c 1289 int_enb2 = USB20X.BRDYENB;
mjoun 1:6e512faaa17c 1290 int_enb3 = USB20X.NRDYENB;
mjoun 1:6e512faaa17c 1291 int_enb4 = USB20X.BEMPENB;
mjoun 1:6e512faaa17c 1292
mjoun 1:6e512faaa17c 1293 if ((int_sts0 & USB_FUNCTION_BITRESM) &&
mjoun 1:6e512faaa17c 1294 (int_enb0 & USB_FUNCTION_BITRSME)) {
mjoun 1:6e512faaa17c 1295 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITRESM;
mjoun 1:6e512faaa17c 1296 RZA_IO_RegWrite_16(&USB20X.INTENB0, 0, USB_INTENB0_RSME_SHIFT, USB_INTENB0_RSME);
mjoun 1:6e512faaa17c 1297 /*usbx_function_USB_FUNCTION_Resume();*/
mjoun 1:6e512faaa17c 1298 suspendStateChanged(1);
mjoun 1:6e512faaa17c 1299 } else if (
mjoun 1:6e512faaa17c 1300 (int_sts0 & USB_FUNCTION_BITVBINT) &&
mjoun 1:6e512faaa17c 1301 (int_enb0 & USB_FUNCTION_BITVBSE)) {
mjoun 1:6e512faaa17c 1302 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITVBINT;
mjoun 1:6e512faaa17c 1303
mjoun 1:6e512faaa17c 1304 if (usbx_function_CheckVBUStaus() == DEVDRV_USBF_ON) {
mjoun 1:6e512faaa17c 1305 usbx_function_USB_FUNCTION_Attach();
mjoun 1:6e512faaa17c 1306 } else {
mjoun 1:6e512faaa17c 1307 usbx_function_USB_FUNCTION_Detach();
mjoun 1:6e512faaa17c 1308 }
mjoun 1:6e512faaa17c 1309 } else if (
mjoun 1:6e512faaa17c 1310 (int_sts0 & USB_FUNCTION_BITSOFR) &&
mjoun 1:6e512faaa17c 1311 (int_enb0 & USB_FUNCTION_BITSOFE)) {
mjoun 1:6e512faaa17c 1312 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITSOFR;
mjoun 1:6e512faaa17c 1313 SOF((USB20X.FRMNUM & USB_FRMNUM_FRNM) >> USB_FRMNUM_FRNM_SHIFT);
mjoun 1:6e512faaa17c 1314 } else if (
mjoun 1:6e512faaa17c 1315 (int_sts0 & USB_FUNCTION_BITDVST) &&
mjoun 1:6e512faaa17c 1316 (int_enb0 & USB_FUNCTION_BITDVSE)) {
mjoun 1:6e512faaa17c 1317 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITDVST;
mjoun 1:6e512faaa17c 1318 switch (int_sts0 & USB_FUNCTION_BITDVSQ) {
mjoun 1:6e512faaa17c 1319 case USB_FUNCTION_DS_POWR:
mjoun 1:6e512faaa17c 1320 break;
mjoun 1:6e512faaa17c 1321
mjoun 1:6e512faaa17c 1322 case USB_FUNCTION_DS_DFLT:
mjoun 1:6e512faaa17c 1323 /*****************************************************************************
mjoun 1:6e512faaa17c 1324 * Function Name: usbx_function_USB_FUNCTION_BusReset
mjoun 1:6e512faaa17c 1325 * Description : This function is executed when the USB device is transitioned
mjoun 1:6e512faaa17c 1326 * : to POWERD_STATE. Sets the device descriptor according to the
mjoun 1:6e512faaa17c 1327 * : connection speed determined by the USB reset hand shake.
mjoun 1:6e512faaa17c 1328 * Arguments : none
mjoun 1:6e512faaa17c 1329 * Return Value : none
mjoun 1:6e512faaa17c 1330 *****************************************************************************/
mjoun 1:6e512faaa17c 1331 usbx_function_init_status(); /* memory clear */
mjoun 1:6e512faaa17c 1332
mjoun 1:6e512faaa17c 1333 #if 0
mjoun 1:6e512faaa17c 1334 /* You would program those steps in USBCallback_busReset
mjoun 1:6e512faaa17c 1335 * if the system need the comment out steps.
mjoun 1:6e512faaa17c 1336 */
mjoun 1:6e512faaa17c 1337
mjoun 1:6e512faaa17c 1338 if (usbx_function_is_hispeed() == USB_FUNCTION_HIGH_SPEED) {
mjoun 1:6e512faaa17c 1339 /* Device Descriptor reset */
mjoun 1:6e512faaa17c 1340 usbx_function_ResetDescriptor(USB_FUNCTION_HIGH_SPEED);
mjoun 1:6e512faaa17c 1341 } else {
mjoun 1:6e512faaa17c 1342 /* Device Descriptor reset */
mjoun 1:6e512faaa17c 1343 usbx_function_ResetDescriptor(USB_FUNCTION_FULL_SPEED);
mjoun 1:6e512faaa17c 1344 }
mjoun 1:6e512faaa17c 1345 #endif
mjoun 1:6e512faaa17c 1346 /* Default Control PIPE reset */
mjoun 1:6e512faaa17c 1347 /*****************************************************************************
mjoun 1:6e512faaa17c 1348 * Function Name: usbx_function_ResetDCP
mjoun 1:6e512faaa17c 1349 * Description : Initializes the default control pipe(DCP).
mjoun 1:6e512faaa17c 1350 * Outline : Reset default control pipe
mjoun 1:6e512faaa17c 1351 * Arguments : none
mjoun 1:6e512faaa17c 1352 * Return Value : none
mjoun 1:6e512faaa17c 1353 *****************************************************************************/
mjoun 1:6e512faaa17c 1354 USB20X.DCPCFG = 0;
mjoun 1:6e512faaa17c 1355 USB20X.DCPMAXP = 64; /*TODO: This value is copied from sample*/
mjoun 1:6e512faaa17c 1356
mjoun 1:6e512faaa17c 1357 USB20X.CFIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
mjoun 1:6e512faaa17c 1358 USB20X.D0FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
mjoun 1:6e512faaa17c 1359 USB20X.D1FIFOSEL = (uint16_t)(USB_FUNCTION_BITMBW_8 | USB_FUNCTION_BITBYTE_LITTLE);
mjoun 1:6e512faaa17c 1360
mjoun 1:6e512faaa17c 1361 busReset();
mjoun 1:6e512faaa17c 1362 break;
mjoun 1:6e512faaa17c 1363
mjoun 1:6e512faaa17c 1364 case USB_FUNCTION_DS_ADDS:
mjoun 1:6e512faaa17c 1365 break;
mjoun 1:6e512faaa17c 1366
mjoun 1:6e512faaa17c 1367 case USB_FUNCTION_DS_CNFG:
mjoun 1:6e512faaa17c 1368 break;
mjoun 1:6e512faaa17c 1369
mjoun 1:6e512faaa17c 1370 case USB_FUNCTION_DS_SPD_POWR:
mjoun 1:6e512faaa17c 1371 case USB_FUNCTION_DS_SPD_DFLT:
mjoun 1:6e512faaa17c 1372 case USB_FUNCTION_DS_SPD_ADDR:
mjoun 1:6e512faaa17c 1373 case USB_FUNCTION_DS_SPD_CNFG:
mjoun 1:6e512faaa17c 1374 suspendStateChanged(0);
mjoun 1:6e512faaa17c 1375 /*usbx_function_USB_FUNCTION_Suspend();*/
mjoun 1:6e512faaa17c 1376 break;
mjoun 1:6e512faaa17c 1377
mjoun 1:6e512faaa17c 1378 default:
mjoun 1:6e512faaa17c 1379 break;
mjoun 1:6e512faaa17c 1380 }
mjoun 1:6e512faaa17c 1381 } else if (
mjoun 1:6e512faaa17c 1382 (int_sts0 & USB_FUNCTION_BITBEMP) &&
mjoun 1:6e512faaa17c 1383 (int_enb0 & USB_FUNCTION_BITBEMP) &&
mjoun 1:6e512faaa17c 1384 ((int_sts3 & int_enb4) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
mjoun 1:6e512faaa17c 1385 /* ==== BEMP PIPE0 ==== */
mjoun 1:6e512faaa17c 1386 usbx_function_BEMPInterruptPIPE0(int_sts3, int_enb4, this, &USBHAL::EP0in);
mjoun 1:6e512faaa17c 1387 } else if (
mjoun 1:6e512faaa17c 1388 (int_sts0 & USB_FUNCTION_BITBRDY) &&
mjoun 1:6e512faaa17c 1389 (int_enb0 & USB_FUNCTION_BITBRDY) &&
mjoun 1:6e512faaa17c 1390 ((int_sts1 & int_enb2) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
mjoun 1:6e512faaa17c 1391 /* ==== BRDY PIPE0 ==== */
mjoun 1:6e512faaa17c 1392 usbx_function_BRDYInterruptPIPE0(int_sts1, int_enb2, this, &USBHAL::EP0out);
mjoun 1:6e512faaa17c 1393 } else if (
mjoun 1:6e512faaa17c 1394 (int_sts0 & USB_FUNCTION_BITNRDY) &&
mjoun 1:6e512faaa17c 1395 (int_enb0 & USB_FUNCTION_BITNRDY) &&
mjoun 1:6e512faaa17c 1396 ((int_sts2 & int_enb3) & g_usbx_function_bit_set[USB_FUNCTION_PIPE0])) {
mjoun 1:6e512faaa17c 1397 /* ==== NRDY PIPE0 ==== */
mjoun 1:6e512faaa17c 1398 usbx_function_NRDYInterruptPIPE0(int_sts2, int_enb3, this, NULL);
mjoun 1:6e512faaa17c 1399 } else if (
mjoun 1:6e512faaa17c 1400 (int_sts0 & USB_FUNCTION_BITCTRT) && (int_enb0 & USB_FUNCTION_BITCTRE)) {
mjoun 1:6e512faaa17c 1401 int_sts0 = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1402 USB20X.INTSTS0 = (uint16_t)~USB_FUNCTION_BITCTRT;
mjoun 1:6e512faaa17c 1403
mjoun 1:6e512faaa17c 1404 if (((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_RDDS) ||
mjoun 1:6e512faaa17c 1405 ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRDS) ||
mjoun 1:6e512faaa17c 1406 ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRND)) {
mjoun 1:6e512faaa17c 1407
mjoun 1:6e512faaa17c 1408 /* remake EP0 into buffer */
mjoun 1:6e512faaa17c 1409 usbx_function_save_request();
mjoun 1:6e512faaa17c 1410 if ((USB20X.INTSTS0 & USB_FUNCTION_BITVALID) && (
mjoun 1:6e512faaa17c 1411 ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_RDDS) ||
mjoun 1:6e512faaa17c 1412 ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRDS) ||
mjoun 1:6e512faaa17c 1413 ((int_sts0 & USB_FUNCTION_BITCTSQ) == USB_FUNCTION_CS_WRND))) {
mjoun 1:6e512faaa17c 1414 /* New SETUP token received */
mjoun 1:6e512faaa17c 1415 /* Three dummy reads for cleearing interrupt requests */
mjoun 1:6e512faaa17c 1416 dumy_sts = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1417 dumy_sts = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1418 dumy_sts = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1419 return;
mjoun 1:6e512faaa17c 1420 }
mjoun 1:6e512faaa17c 1421 }
mjoun 1:6e512faaa17c 1422
mjoun 1:6e512faaa17c 1423 switch (int_sts0 & USB_FUNCTION_BITCTSQ) {
mjoun 1:6e512faaa17c 1424 case USB_FUNCTION_CS_IDST:
mjoun 1:6e512faaa17c 1425 if (g_usbx_function_TestModeFlag == DEVDRV_USBF_YES) {
mjoun 1:6e512faaa17c 1426 /* ==== Test Mode ==== */
mjoun 1:6e512faaa17c 1427 usbx_function_USB_FUNCTION_TestMode();
mjoun 1:6e512faaa17c 1428 }
mjoun 1:6e512faaa17c 1429 /* Needs not procedure in this state */
mjoun 1:6e512faaa17c 1430 break;
mjoun 1:6e512faaa17c 1431
mjoun 1:6e512faaa17c 1432 case USB_FUNCTION_CS_RDDS:
mjoun 1:6e512faaa17c 1433 /* Reads a setup packet */
mjoun 1:6e512faaa17c 1434 EP0setupCallback();
mjoun 1:6e512faaa17c 1435 break;
mjoun 1:6e512faaa17c 1436
mjoun 1:6e512faaa17c 1437 case USB_FUNCTION_CS_WRDS:
mjoun 1:6e512faaa17c 1438 /* Original code was the SetDescriptor was called */
mjoun 1:6e512faaa17c 1439 EP0setupCallback();
mjoun 1:6e512faaa17c 1440 break;
mjoun 1:6e512faaa17c 1441
mjoun 1:6e512faaa17c 1442 case USB_FUNCTION_CS_WRND:
mjoun 1:6e512faaa17c 1443 EP0setupCallback();
mjoun 1:6e512faaa17c 1444
mjoun 1:6e512faaa17c 1445 /*The EP0setupCallback should finish in successful */
mjoun 1:6e512faaa17c 1446 usbx_function_set_pid_buf(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 1447
mjoun 1:6e512faaa17c 1448 RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
mjoun 1:6e512faaa17c 1449 break;
mjoun 1:6e512faaa17c 1450
mjoun 1:6e512faaa17c 1451 case USB_FUNCTION_CS_RDSS:
mjoun 1:6e512faaa17c 1452 RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
mjoun 1:6e512faaa17c 1453 break;
mjoun 1:6e512faaa17c 1454
mjoun 1:6e512faaa17c 1455 case USB_FUNCTION_CS_WRSS:
mjoun 1:6e512faaa17c 1456 RZA_IO_RegWrite_16(&USB20X.DCPCTR, 1, USB_DCPCTR_CCPL_SHIFT, USB_DCPCTR_CCPL);
mjoun 1:6e512faaa17c 1457 break;
mjoun 1:6e512faaa17c 1458
mjoun 1:6e512faaa17c 1459 case USB_FUNCTION_CS_SQER:
mjoun 1:6e512faaa17c 1460 usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 1461 break;
mjoun 1:6e512faaa17c 1462
mjoun 1:6e512faaa17c 1463 default:
mjoun 1:6e512faaa17c 1464 usbx_function_set_pid_stall(USB_FUNCTION_PIPE0);
mjoun 1:6e512faaa17c 1465 break;
mjoun 1:6e512faaa17c 1466 }
mjoun 1:6e512faaa17c 1467 } else if (
mjoun 1:6e512faaa17c 1468 (int_sts0 & USB_FUNCTION_BITBEMP) &&
mjoun 1:6e512faaa17c 1469 (int_enb0 & USB_FUNCTION_BITBEMP) &&
mjoun 1:6e512faaa17c 1470 (int_sts3 & int_enb4) ) {
mjoun 1:6e512faaa17c 1471 /* ==== BEMP PIPEx ==== */
mjoun 1:6e512faaa17c 1472 usbx_function_BEMPInterrupt(int_sts3, int_enb4, this, epCallback);
mjoun 1:6e512faaa17c 1473 } else if (
mjoun 1:6e512faaa17c 1474 (int_sts0 & USB_FUNCTION_BITBRDY) &&
mjoun 1:6e512faaa17c 1475 (int_enb0 & USB_FUNCTION_BITBRDY) &&
mjoun 1:6e512faaa17c 1476 (int_sts1 & int_enb2) ) {
mjoun 1:6e512faaa17c 1477 /* ==== BRDY PIPEx ==== */
mjoun 1:6e512faaa17c 1478 usbx_function_BRDYInterrupt(int_sts1, int_enb2, this, epCallback);
mjoun 1:6e512faaa17c 1479 } else if (
mjoun 1:6e512faaa17c 1480 (int_sts0 & USB_FUNCTION_BITNRDY) &&
mjoun 1:6e512faaa17c 1481 (int_enb0 & USB_FUNCTION_BITNRDY) &&
mjoun 1:6e512faaa17c 1482 (int_sts2 & int_enb3)) {
mjoun 1:6e512faaa17c 1483 /* ==== NRDY PIPEx ==== */
mjoun 1:6e512faaa17c 1484 usbx_function_NRDYInterrupt(int_sts2, int_enb3, this, epCallback);
mjoun 1:6e512faaa17c 1485 } else {
mjoun 1:6e512faaa17c 1486 /* Do Nothing */
mjoun 1:6e512faaa17c 1487 }
mjoun 1:6e512faaa17c 1488
mjoun 1:6e512faaa17c 1489 /* Three dummy reads for cleearing interrupt requests */
mjoun 1:6e512faaa17c 1490 dumy_sts = USB20X.INTSTS0;
mjoun 1:6e512faaa17c 1491 dumy_sts = USB20X.INTSTS1;
mjoun 1:6e512faaa17c 1492 }
mjoun 1:6e512faaa17c 1493
mjoun 1:6e512faaa17c 1494 /*************************************************************************/
mjoun 1:6e512faaa17c 1495 #endif
mjoun 1:6e512faaa17c 1496 /*************************************************************************/
mjoun 1:6e512faaa17c 1497 /*EOF*/