Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Mon Nov 23 16:09:54 2015 +0000
Revision:
0:5e35c180ed4a
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Racer01014 0:5e35c180ed4a 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Racer01014 0:5e35c180ed4a 2 *
Racer01014 0:5e35c180ed4a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Racer01014 0:5e35c180ed4a 4 * and associated documentation files (the "Software"), to deal in the Software without
Racer01014 0:5e35c180ed4a 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Racer01014 0:5e35c180ed4a 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Racer01014 0:5e35c180ed4a 7 * Software is furnished to do so, subject to the following conditions:
Racer01014 0:5e35c180ed4a 8 *
Racer01014 0:5e35c180ed4a 9 * The above copyright notice and this permission notice shall be included in all copies or
Racer01014 0:5e35c180ed4a 10 * substantial portions of the Software.
Racer01014 0:5e35c180ed4a 11 *
Racer01014 0:5e35c180ed4a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Racer01014 0:5e35c180ed4a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Racer01014 0:5e35c180ed4a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Racer01014 0:5e35c180ed4a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Racer01014 0:5e35c180ed4a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Racer01014 0:5e35c180ed4a 17 */
Racer01014 0:5e35c180ed4a 18
Racer01014 0:5e35c180ed4a 19 #if defined(TARGET_KL25Z)
Racer01014 0:5e35c180ed4a 20
Racer01014 0:5e35c180ed4a 21 #include "USBHAL.h"
Racer01014 0:5e35c180ed4a 22
Racer01014 0:5e35c180ed4a 23 USBHAL * USBHAL::instance;
Racer01014 0:5e35c180ed4a 24
Racer01014 0:5e35c180ed4a 25 static volatile int epComplete = 0;
Racer01014 0:5e35c180ed4a 26
Racer01014 0:5e35c180ed4a 27 // Convert physical endpoint number to register bit
Racer01014 0:5e35c180ed4a 28 #define EP(endpoint) (1<<(endpoint))
Racer01014 0:5e35c180ed4a 29
Racer01014 0:5e35c180ed4a 30 // Convert physical to logical
Racer01014 0:5e35c180ed4a 31 #define PHY_TO_LOG(endpoint) ((endpoint)>>1)
Racer01014 0:5e35c180ed4a 32
Racer01014 0:5e35c180ed4a 33 // Get endpoint direction
Racer01014 0:5e35c180ed4a 34 #define IN_EP(endpoint) ((endpoint) & 1U ? true : false)
Racer01014 0:5e35c180ed4a 35 #define OUT_EP(endpoint) ((endpoint) & 1U ? false : true)
Racer01014 0:5e35c180ed4a 36
Racer01014 0:5e35c180ed4a 37 #define BD_OWN_MASK (1<<7)
Racer01014 0:5e35c180ed4a 38 #define BD_DATA01_MASK (1<<6)
Racer01014 0:5e35c180ed4a 39 #define BD_KEEP_MASK (1<<5)
Racer01014 0:5e35c180ed4a 40 #define BD_NINC_MASK (1<<4)
Racer01014 0:5e35c180ed4a 41 #define BD_DTS_MASK (1<<3)
Racer01014 0:5e35c180ed4a 42 #define BD_STALL_MASK (1<<2)
Racer01014 0:5e35c180ed4a 43
Racer01014 0:5e35c180ed4a 44 #define TX 1
Racer01014 0:5e35c180ed4a 45 #define RX 0
Racer01014 0:5e35c180ed4a 46 #define ODD 0
Racer01014 0:5e35c180ed4a 47 #define EVEN 1
Racer01014 0:5e35c180ed4a 48 // this macro waits a physical endpoint number
Racer01014 0:5e35c180ed4a 49 #define EP_BDT_IDX(ep, dir, odd) (((ep * 4) + (2 * dir) + (1 * odd)))
Racer01014 0:5e35c180ed4a 50
Racer01014 0:5e35c180ed4a 51 #define SETUP_TOKEN 0x0D
Racer01014 0:5e35c180ed4a 52 #define IN_TOKEN 0x09
Racer01014 0:5e35c180ed4a 53 #define OUT_TOKEN 0x01
Racer01014 0:5e35c180ed4a 54 #define TOK_PID(idx) ((bdt[idx].info >> 2) & 0x0F)
Racer01014 0:5e35c180ed4a 55
Racer01014 0:5e35c180ed4a 56 // for each endpt: 8 bytes
Racer01014 0:5e35c180ed4a 57 typedef struct BDT {
Racer01014 0:5e35c180ed4a 58 uint8_t info; // BD[0:7]
Racer01014 0:5e35c180ed4a 59 uint8_t dummy; // RSVD: BD[8:15]
Racer01014 0:5e35c180ed4a 60 uint16_t byte_count; // BD[16:32]
Racer01014 0:5e35c180ed4a 61 uint32_t address; // Addr
Racer01014 0:5e35c180ed4a 62 } BDT;
Racer01014 0:5e35c180ed4a 63
Racer01014 0:5e35c180ed4a 64
Racer01014 0:5e35c180ed4a 65 // there are:
Racer01014 0:5e35c180ed4a 66 // * 16 bidirectionnal endpt -> 32 physical endpt
Racer01014 0:5e35c180ed4a 67 // * as there are ODD and EVEN buffer -> 32*2 bdt
Racer01014 0:5e35c180ed4a 68 __attribute__((__aligned__(512))) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
Racer01014 0:5e35c180ed4a 69 uint8_t endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2][64];
Racer01014 0:5e35c180ed4a 70 uint8_t endpoint_buffer_iso[2*2][1023];
Racer01014 0:5e35c180ed4a 71
Racer01014 0:5e35c180ed4a 72 static uint8_t set_addr = 0;
Racer01014 0:5e35c180ed4a 73 static uint8_t addr = 0;
Racer01014 0:5e35c180ed4a 74
Racer01014 0:5e35c180ed4a 75 static uint32_t Data1 = 0x55555555;
Racer01014 0:5e35c180ed4a 76
Racer01014 0:5e35c180ed4a 77 static uint32_t frameNumber() {
Racer01014 0:5e35c180ed4a 78 return((USB0->FRMNUML | (USB0->FRMNUMH << 8) & 0x07FF));
Racer01014 0:5e35c180ed4a 79 }
Racer01014 0:5e35c180ed4a 80
Racer01014 0:5e35c180ed4a 81 uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
Racer01014 0:5e35c180ed4a 82 return 0;
Racer01014 0:5e35c180ed4a 83 }
Racer01014 0:5e35c180ed4a 84
Racer01014 0:5e35c180ed4a 85 USBHAL::USBHAL(void) {
Racer01014 0:5e35c180ed4a 86 // Disable IRQ
Racer01014 0:5e35c180ed4a 87 NVIC_DisableIRQ(USB0_IRQn);
Racer01014 0:5e35c180ed4a 88
Racer01014 0:5e35c180ed4a 89 // fill in callback array
Racer01014 0:5e35c180ed4a 90 epCallback[0] = &USBHAL::EP1_OUT_callback;
Racer01014 0:5e35c180ed4a 91 epCallback[1] = &USBHAL::EP1_IN_callback;
Racer01014 0:5e35c180ed4a 92 epCallback[2] = &USBHAL::EP2_OUT_callback;
Racer01014 0:5e35c180ed4a 93 epCallback[3] = &USBHAL::EP2_IN_callback;
Racer01014 0:5e35c180ed4a 94 epCallback[4] = &USBHAL::EP3_OUT_callback;
Racer01014 0:5e35c180ed4a 95 epCallback[5] = &USBHAL::EP3_IN_callback;
Racer01014 0:5e35c180ed4a 96 epCallback[6] = &USBHAL::EP4_OUT_callback;
Racer01014 0:5e35c180ed4a 97 epCallback[7] = &USBHAL::EP4_IN_callback;
Racer01014 0:5e35c180ed4a 98 epCallback[8] = &USBHAL::EP5_OUT_callback;
Racer01014 0:5e35c180ed4a 99 epCallback[9] = &USBHAL::EP5_IN_callback;
Racer01014 0:5e35c180ed4a 100 epCallback[10] = &USBHAL::EP6_OUT_callback;
Racer01014 0:5e35c180ed4a 101 epCallback[11] = &USBHAL::EP6_IN_callback;
Racer01014 0:5e35c180ed4a 102 epCallback[12] = &USBHAL::EP7_OUT_callback;
Racer01014 0:5e35c180ed4a 103 epCallback[13] = &USBHAL::EP7_IN_callback;
Racer01014 0:5e35c180ed4a 104 epCallback[14] = &USBHAL::EP8_OUT_callback;
Racer01014 0:5e35c180ed4a 105 epCallback[15] = &USBHAL::EP8_IN_callback;
Racer01014 0:5e35c180ed4a 106 epCallback[16] = &USBHAL::EP9_OUT_callback;
Racer01014 0:5e35c180ed4a 107 epCallback[17] = &USBHAL::EP9_IN_callback;
Racer01014 0:5e35c180ed4a 108 epCallback[18] = &USBHAL::EP10_OUT_callback;
Racer01014 0:5e35c180ed4a 109 epCallback[19] = &USBHAL::EP10_IN_callback;
Racer01014 0:5e35c180ed4a 110 epCallback[20] = &USBHAL::EP11_OUT_callback;
Racer01014 0:5e35c180ed4a 111 epCallback[21] = &USBHAL::EP11_IN_callback;
Racer01014 0:5e35c180ed4a 112 epCallback[22] = &USBHAL::EP12_OUT_callback;
Racer01014 0:5e35c180ed4a 113 epCallback[23] = &USBHAL::EP12_IN_callback;
Racer01014 0:5e35c180ed4a 114 epCallback[24] = &USBHAL::EP13_OUT_callback;
Racer01014 0:5e35c180ed4a 115 epCallback[25] = &USBHAL::EP13_IN_callback;
Racer01014 0:5e35c180ed4a 116 epCallback[26] = &USBHAL::EP14_OUT_callback;
Racer01014 0:5e35c180ed4a 117 epCallback[27] = &USBHAL::EP14_IN_callback;
Racer01014 0:5e35c180ed4a 118 epCallback[28] = &USBHAL::EP15_OUT_callback;
Racer01014 0:5e35c180ed4a 119 epCallback[29] = &USBHAL::EP15_IN_callback;
Racer01014 0:5e35c180ed4a 120
Racer01014 0:5e35c180ed4a 121
Racer01014 0:5e35c180ed4a 122 // choose usb src as PLL
Racer01014 0:5e35c180ed4a 123 SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK);
Racer01014 0:5e35c180ed4a 124
Racer01014 0:5e35c180ed4a 125 // enable OTG clock
Racer01014 0:5e35c180ed4a 126 SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK;
Racer01014 0:5e35c180ed4a 127
Racer01014 0:5e35c180ed4a 128 // Attach IRQ
Racer01014 0:5e35c180ed4a 129 instance = this;
Racer01014 0:5e35c180ed4a 130 NVIC_SetVector(USB0_IRQn, (uint32_t)&_usbisr);
Racer01014 0:5e35c180ed4a 131 NVIC_EnableIRQ(USB0_IRQn);
Racer01014 0:5e35c180ed4a 132
Racer01014 0:5e35c180ed4a 133 // USB Module Configuration
Racer01014 0:5e35c180ed4a 134 // Reset USB Module
Racer01014 0:5e35c180ed4a 135 USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK;
Racer01014 0:5e35c180ed4a 136 while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK);
Racer01014 0:5e35c180ed4a 137
Racer01014 0:5e35c180ed4a 138 // Set BDT Base Register
Racer01014 0:5e35c180ed4a 139 USB0->BDTPAGE1=(uint8_t)((uint32_t)bdt>>8);
Racer01014 0:5e35c180ed4a 140 USB0->BDTPAGE2=(uint8_t)((uint32_t)bdt>>16);
Racer01014 0:5e35c180ed4a 141 USB0->BDTPAGE3=(uint8_t)((uint32_t)bdt>>24);
Racer01014 0:5e35c180ed4a 142
Racer01014 0:5e35c180ed4a 143 // Clear interrupt flag
Racer01014 0:5e35c180ed4a 144 USB0->ISTAT = 0xff;
Racer01014 0:5e35c180ed4a 145
Racer01014 0:5e35c180ed4a 146 // USB Interrupt Enablers
Racer01014 0:5e35c180ed4a 147 USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK |
Racer01014 0:5e35c180ed4a 148 USB_INTEN_SOFTOKEN_MASK |
Racer01014 0:5e35c180ed4a 149 USB_INTEN_ERROREN_MASK |
Racer01014 0:5e35c180ed4a 150 USB_INTEN_USBRSTEN_MASK;
Racer01014 0:5e35c180ed4a 151
Racer01014 0:5e35c180ed4a 152 // Disable weak pull downs
Racer01014 0:5e35c180ed4a 153 USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
Racer01014 0:5e35c180ed4a 154
Racer01014 0:5e35c180ed4a 155 USB0->USBTRC0 |= 0x40;
Racer01014 0:5e35c180ed4a 156 }
Racer01014 0:5e35c180ed4a 157
Racer01014 0:5e35c180ed4a 158 USBHAL::~USBHAL(void) { }
Racer01014 0:5e35c180ed4a 159
Racer01014 0:5e35c180ed4a 160 void USBHAL::connect(void) {
Racer01014 0:5e35c180ed4a 161 // enable USB
Racer01014 0:5e35c180ed4a 162 USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
Racer01014 0:5e35c180ed4a 163 // Pull up enable
Racer01014 0:5e35c180ed4a 164 USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
Racer01014 0:5e35c180ed4a 165 }
Racer01014 0:5e35c180ed4a 166
Racer01014 0:5e35c180ed4a 167 void USBHAL::disconnect(void) {
Racer01014 0:5e35c180ed4a 168 // disable USB
Racer01014 0:5e35c180ed4a 169 USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
Racer01014 0:5e35c180ed4a 170 // Pull up disable
Racer01014 0:5e35c180ed4a 171 USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
Racer01014 0:5e35c180ed4a 172 }
Racer01014 0:5e35c180ed4a 173
Racer01014 0:5e35c180ed4a 174 void USBHAL::configureDevice(void) {
Racer01014 0:5e35c180ed4a 175 // not needed
Racer01014 0:5e35c180ed4a 176 }
Racer01014 0:5e35c180ed4a 177
Racer01014 0:5e35c180ed4a 178 void USBHAL::unconfigureDevice(void) {
Racer01014 0:5e35c180ed4a 179 // not needed
Racer01014 0:5e35c180ed4a 180 }
Racer01014 0:5e35c180ed4a 181
Racer01014 0:5e35c180ed4a 182 void USBHAL::setAddress(uint8_t address) {
Racer01014 0:5e35c180ed4a 183 // we don't set the address now otherwise the usb controller does not ack
Racer01014 0:5e35c180ed4a 184 // we set a flag instead
Racer01014 0:5e35c180ed4a 185 // see usbisr when an IN token is received
Racer01014 0:5e35c180ed4a 186 set_addr = 1;
Racer01014 0:5e35c180ed4a 187 addr = address;
Racer01014 0:5e35c180ed4a 188 }
Racer01014 0:5e35c180ed4a 189
Racer01014 0:5e35c180ed4a 190 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
Racer01014 0:5e35c180ed4a 191 uint32_t handshake_flag = 0;
Racer01014 0:5e35c180ed4a 192 uint8_t * buf;
Racer01014 0:5e35c180ed4a 193
Racer01014 0:5e35c180ed4a 194 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
Racer01014 0:5e35c180ed4a 195 return false;
Racer01014 0:5e35c180ed4a 196 }
Racer01014 0:5e35c180ed4a 197
Racer01014 0:5e35c180ed4a 198 uint32_t log_endpoint = PHY_TO_LOG(endpoint);
Racer01014 0:5e35c180ed4a 199
Racer01014 0:5e35c180ed4a 200 if ((flags & ISOCHRONOUS) == 0) {
Racer01014 0:5e35c180ed4a 201 handshake_flag = USB_ENDPT_EPHSHK_MASK;
Racer01014 0:5e35c180ed4a 202 if (IN_EP(endpoint))
Racer01014 0:5e35c180ed4a 203 buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0];
Racer01014 0:5e35c180ed4a 204 else
Racer01014 0:5e35c180ed4a 205 buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0];
Racer01014 0:5e35c180ed4a 206 } else {
Racer01014 0:5e35c180ed4a 207 if (IN_EP(endpoint))
Racer01014 0:5e35c180ed4a 208 buf = &endpoint_buffer_iso[2][0];
Racer01014 0:5e35c180ed4a 209 else
Racer01014 0:5e35c180ed4a 210 buf = &endpoint_buffer_iso[0][0];
Racer01014 0:5e35c180ed4a 211 }
Racer01014 0:5e35c180ed4a 212
Racer01014 0:5e35c180ed4a 213 // IN endpt -> device to host (TX)
Racer01014 0:5e35c180ed4a 214 if (IN_EP(endpoint)) {
Racer01014 0:5e35c180ed4a 215 USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint)
Racer01014 0:5e35c180ed4a 216 USB_ENDPT_EPTXEN_MASK; // en TX (IN) tran
Racer01014 0:5e35c180ed4a 217 bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].address = (uint32_t) buf;
Racer01014 0:5e35c180ed4a 218 bdt[EP_BDT_IDX(log_endpoint, TX, EVEN)].address = 0;
Racer01014 0:5e35c180ed4a 219 }
Racer01014 0:5e35c180ed4a 220 // OUT endpt -> host to device (RX)
Racer01014 0:5e35c180ed4a 221 else {
Racer01014 0:5e35c180ed4a 222 USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint)
Racer01014 0:5e35c180ed4a 223 USB_ENDPT_EPRXEN_MASK; // en RX (OUT) tran.
Racer01014 0:5e35c180ed4a 224 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].byte_count = maxPacket;
Racer01014 0:5e35c180ed4a 225 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].address = (uint32_t) buf;
Racer01014 0:5e35c180ed4a 226 bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].info = BD_OWN_MASK | BD_DTS_MASK;
Racer01014 0:5e35c180ed4a 227 bdt[EP_BDT_IDX(log_endpoint, RX, EVEN)].info = 0;
Racer01014 0:5e35c180ed4a 228 }
Racer01014 0:5e35c180ed4a 229
Racer01014 0:5e35c180ed4a 230 Data1 |= (1 << endpoint);
Racer01014 0:5e35c180ed4a 231
Racer01014 0:5e35c180ed4a 232 return true;
Racer01014 0:5e35c180ed4a 233 }
Racer01014 0:5e35c180ed4a 234
Racer01014 0:5e35c180ed4a 235 // read setup packet
Racer01014 0:5e35c180ed4a 236 void USBHAL::EP0setup(uint8_t *buffer) {
Racer01014 0:5e35c180ed4a 237 uint32_t sz;
Racer01014 0:5e35c180ed4a 238 endpointReadResult(EP0OUT, buffer, &sz);
Racer01014 0:5e35c180ed4a 239 }
Racer01014 0:5e35c180ed4a 240
Racer01014 0:5e35c180ed4a 241 void USBHAL::EP0readStage(void) {
Racer01014 0:5e35c180ed4a 242 Data1 &= ~1UL; // set DATA0
Racer01014 0:5e35c180ed4a 243 bdt[0].info = (BD_DTS_MASK | BD_OWN_MASK);
Racer01014 0:5e35c180ed4a 244 }
Racer01014 0:5e35c180ed4a 245
Racer01014 0:5e35c180ed4a 246 void USBHAL::EP0read(void) {
Racer01014 0:5e35c180ed4a 247 uint32_t idx = EP_BDT_IDX(PHY_TO_LOG(EP0OUT), RX, 0);
Racer01014 0:5e35c180ed4a 248 bdt[idx].byte_count = MAX_PACKET_SIZE_EP0;
Racer01014 0:5e35c180ed4a 249 }
Racer01014 0:5e35c180ed4a 250
Racer01014 0:5e35c180ed4a 251 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
Racer01014 0:5e35c180ed4a 252 uint32_t sz;
Racer01014 0:5e35c180ed4a 253 endpointReadResult(EP0OUT, buffer, &sz);
Racer01014 0:5e35c180ed4a 254 return sz;
Racer01014 0:5e35c180ed4a 255 }
Racer01014 0:5e35c180ed4a 256
Racer01014 0:5e35c180ed4a 257 void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
Racer01014 0:5e35c180ed4a 258 endpointWrite(EP0IN, buffer, size);
Racer01014 0:5e35c180ed4a 259 }
Racer01014 0:5e35c180ed4a 260
Racer01014 0:5e35c180ed4a 261 void USBHAL::EP0getWriteResult(void) {
Racer01014 0:5e35c180ed4a 262 }
Racer01014 0:5e35c180ed4a 263
Racer01014 0:5e35c180ed4a 264 void USBHAL::EP0stall(void) {
Racer01014 0:5e35c180ed4a 265 stallEndpoint(EP0OUT);
Racer01014 0:5e35c180ed4a 266 }
Racer01014 0:5e35c180ed4a 267
Racer01014 0:5e35c180ed4a 268 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
Racer01014 0:5e35c180ed4a 269 endpoint = PHY_TO_LOG(endpoint);
Racer01014 0:5e35c180ed4a 270 uint32_t idx = EP_BDT_IDX(endpoint, RX, 0);
Racer01014 0:5e35c180ed4a 271 bdt[idx].byte_count = maximumSize;
Racer01014 0:5e35c180ed4a 272 return EP_PENDING;
Racer01014 0:5e35c180ed4a 273 }
Racer01014 0:5e35c180ed4a 274
Racer01014 0:5e35c180ed4a 275 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
Racer01014 0:5e35c180ed4a 276 uint32_t n, sz, idx, setup = 0;
Racer01014 0:5e35c180ed4a 277 uint8_t not_iso;
Racer01014 0:5e35c180ed4a 278 uint8_t * ep_buf;
Racer01014 0:5e35c180ed4a 279
Racer01014 0:5e35c180ed4a 280 uint32_t log_endpoint = PHY_TO_LOG(endpoint);
Racer01014 0:5e35c180ed4a 281
Racer01014 0:5e35c180ed4a 282 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
Racer01014 0:5e35c180ed4a 283 return EP_INVALID;
Racer01014 0:5e35c180ed4a 284 }
Racer01014 0:5e35c180ed4a 285
Racer01014 0:5e35c180ed4a 286 // if read on a IN endpoint -> error
Racer01014 0:5e35c180ed4a 287 if (IN_EP(endpoint)) {
Racer01014 0:5e35c180ed4a 288 return EP_INVALID;
Racer01014 0:5e35c180ed4a 289 }
Racer01014 0:5e35c180ed4a 290
Racer01014 0:5e35c180ed4a 291 idx = EP_BDT_IDX(log_endpoint, RX, 0);
Racer01014 0:5e35c180ed4a 292 sz = bdt[idx].byte_count;
Racer01014 0:5e35c180ed4a 293 not_iso = USB0->ENDPOINT[log_endpoint].ENDPT & USB_ENDPT_EPHSHK_MASK;
Racer01014 0:5e35c180ed4a 294
Racer01014 0:5e35c180ed4a 295 //for isochronous endpoint, we don't wait an interrupt
Racer01014 0:5e35c180ed4a 296 if ((log_endpoint != 0) && not_iso && !(epComplete & EP(endpoint))) {
Racer01014 0:5e35c180ed4a 297 return EP_PENDING;
Racer01014 0:5e35c180ed4a 298 }
Racer01014 0:5e35c180ed4a 299
Racer01014 0:5e35c180ed4a 300 if ((log_endpoint == 0) && (TOK_PID(idx) == SETUP_TOKEN)) {
Racer01014 0:5e35c180ed4a 301 setup = 1;
Racer01014 0:5e35c180ed4a 302 }
Racer01014 0:5e35c180ed4a 303
Racer01014 0:5e35c180ed4a 304 // non iso endpoint
Racer01014 0:5e35c180ed4a 305 if (not_iso) {
Racer01014 0:5e35c180ed4a 306 ep_buf = endpoint_buffer[idx];
Racer01014 0:5e35c180ed4a 307 } else {
Racer01014 0:5e35c180ed4a 308 ep_buf = endpoint_buffer_iso[0];
Racer01014 0:5e35c180ed4a 309 }
Racer01014 0:5e35c180ed4a 310
Racer01014 0:5e35c180ed4a 311 for (n = 0; n < sz; n++) {
Racer01014 0:5e35c180ed4a 312 buffer[n] = ep_buf[n];
Racer01014 0:5e35c180ed4a 313 }
Racer01014 0:5e35c180ed4a 314
Racer01014 0:5e35c180ed4a 315 if (((Data1 >> endpoint) & 1) == ((bdt[idx].info >> 6) & 1)) {
Racer01014 0:5e35c180ed4a 316 if (setup && (buffer[6] == 0)) // if no setup data stage,
Racer01014 0:5e35c180ed4a 317 Data1 &= ~1UL; // set DATA0
Racer01014 0:5e35c180ed4a 318 else
Racer01014 0:5e35c180ed4a 319 Data1 ^= (1 << endpoint);
Racer01014 0:5e35c180ed4a 320 }
Racer01014 0:5e35c180ed4a 321
Racer01014 0:5e35c180ed4a 322 if (((Data1 >> endpoint) & 1)) {
Racer01014 0:5e35c180ed4a 323 bdt[idx].info = BD_DTS_MASK | BD_DATA01_MASK | BD_OWN_MASK;
Racer01014 0:5e35c180ed4a 324 }
Racer01014 0:5e35c180ed4a 325 else {
Racer01014 0:5e35c180ed4a 326 bdt[idx].info = BD_DTS_MASK | BD_OWN_MASK;
Racer01014 0:5e35c180ed4a 327 }
Racer01014 0:5e35c180ed4a 328
Racer01014 0:5e35c180ed4a 329 USB0->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK;
Racer01014 0:5e35c180ed4a 330 *bytesRead = sz;
Racer01014 0:5e35c180ed4a 331
Racer01014 0:5e35c180ed4a 332 epComplete &= ~EP(endpoint);
Racer01014 0:5e35c180ed4a 333 return EP_COMPLETED;
Racer01014 0:5e35c180ed4a 334 }
Racer01014 0:5e35c180ed4a 335
Racer01014 0:5e35c180ed4a 336 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
Racer01014 0:5e35c180ed4a 337 uint32_t idx, n;
Racer01014 0:5e35c180ed4a 338 uint8_t * ep_buf;
Racer01014 0:5e35c180ed4a 339
Racer01014 0:5e35c180ed4a 340 if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
Racer01014 0:5e35c180ed4a 341 return EP_INVALID;
Racer01014 0:5e35c180ed4a 342 }
Racer01014 0:5e35c180ed4a 343
Racer01014 0:5e35c180ed4a 344 // if write on a OUT endpoint -> error
Racer01014 0:5e35c180ed4a 345 if (OUT_EP(endpoint)) {
Racer01014 0:5e35c180ed4a 346 return EP_INVALID;
Racer01014 0:5e35c180ed4a 347 }
Racer01014 0:5e35c180ed4a 348
Racer01014 0:5e35c180ed4a 349 idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
Racer01014 0:5e35c180ed4a 350 bdt[idx].byte_count = size;
Racer01014 0:5e35c180ed4a 351
Racer01014 0:5e35c180ed4a 352
Racer01014 0:5e35c180ed4a 353 // non iso endpoint
Racer01014 0:5e35c180ed4a 354 if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) {
Racer01014 0:5e35c180ed4a 355 ep_buf = endpoint_buffer[idx];
Racer01014 0:5e35c180ed4a 356 } else {
Racer01014 0:5e35c180ed4a 357 ep_buf = endpoint_buffer_iso[2];
Racer01014 0:5e35c180ed4a 358 }
Racer01014 0:5e35c180ed4a 359
Racer01014 0:5e35c180ed4a 360 for (n = 0; n < size; n++) {
Racer01014 0:5e35c180ed4a 361 ep_buf[n] = data[n];
Racer01014 0:5e35c180ed4a 362 }
Racer01014 0:5e35c180ed4a 363
Racer01014 0:5e35c180ed4a 364 if ((Data1 >> endpoint) & 1) {
Racer01014 0:5e35c180ed4a 365 bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK;
Racer01014 0:5e35c180ed4a 366 } else {
Racer01014 0:5e35c180ed4a 367 bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK;
Racer01014 0:5e35c180ed4a 368 }
Racer01014 0:5e35c180ed4a 369
Racer01014 0:5e35c180ed4a 370 Data1 ^= (1 << endpoint);
Racer01014 0:5e35c180ed4a 371
Racer01014 0:5e35c180ed4a 372 return EP_PENDING;
Racer01014 0:5e35c180ed4a 373 }
Racer01014 0:5e35c180ed4a 374
Racer01014 0:5e35c180ed4a 375 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
Racer01014 0:5e35c180ed4a 376 if (epComplete & EP(endpoint)) {
Racer01014 0:5e35c180ed4a 377 epComplete &= ~EP(endpoint);
Racer01014 0:5e35c180ed4a 378 return EP_COMPLETED;
Racer01014 0:5e35c180ed4a 379 }
Racer01014 0:5e35c180ed4a 380
Racer01014 0:5e35c180ed4a 381 return EP_PENDING;
Racer01014 0:5e35c180ed4a 382 }
Racer01014 0:5e35c180ed4a 383
Racer01014 0:5e35c180ed4a 384 void USBHAL::stallEndpoint(uint8_t endpoint) {
Racer01014 0:5e35c180ed4a 385 USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT |= USB_ENDPT_EPSTALL_MASK;
Racer01014 0:5e35c180ed4a 386 }
Racer01014 0:5e35c180ed4a 387
Racer01014 0:5e35c180ed4a 388 void USBHAL::unstallEndpoint(uint8_t endpoint) {
Racer01014 0:5e35c180ed4a 389 USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
Racer01014 0:5e35c180ed4a 390 }
Racer01014 0:5e35c180ed4a 391
Racer01014 0:5e35c180ed4a 392 bool USBHAL::getEndpointStallState(uint8_t endpoint) {
Racer01014 0:5e35c180ed4a 393 uint8_t stall = (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPSTALL_MASK);
Racer01014 0:5e35c180ed4a 394 return (stall) ? true : false;
Racer01014 0:5e35c180ed4a 395 }
Racer01014 0:5e35c180ed4a 396
Racer01014 0:5e35c180ed4a 397 void USBHAL::remoteWakeup(void) {
Racer01014 0:5e35c180ed4a 398 // [TODO]
Racer01014 0:5e35c180ed4a 399 }
Racer01014 0:5e35c180ed4a 400
Racer01014 0:5e35c180ed4a 401
Racer01014 0:5e35c180ed4a 402 void USBHAL::_usbisr(void) {
Racer01014 0:5e35c180ed4a 403 instance->usbisr();
Racer01014 0:5e35c180ed4a 404 }
Racer01014 0:5e35c180ed4a 405
Racer01014 0:5e35c180ed4a 406
Racer01014 0:5e35c180ed4a 407 void USBHAL::usbisr(void) {
Racer01014 0:5e35c180ed4a 408 uint8_t i;
Racer01014 0:5e35c180ed4a 409 uint8_t istat = USB0->ISTAT;
Racer01014 0:5e35c180ed4a 410
Racer01014 0:5e35c180ed4a 411 // reset interrupt
Racer01014 0:5e35c180ed4a 412 if (istat & USB_ISTAT_USBRST_MASK) {
Racer01014 0:5e35c180ed4a 413 // disable all endpt
Racer01014 0:5e35c180ed4a 414 for(i = 0; i < 16; i++) {
Racer01014 0:5e35c180ed4a 415 USB0->ENDPOINT[i].ENDPT = 0x00;
Racer01014 0:5e35c180ed4a 416 }
Racer01014 0:5e35c180ed4a 417
Racer01014 0:5e35c180ed4a 418 // enable control endpoint
Racer01014 0:5e35c180ed4a 419 realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
Racer01014 0:5e35c180ed4a 420 realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
Racer01014 0:5e35c180ed4a 421
Racer01014 0:5e35c180ed4a 422 Data1 = 0x55555555;
Racer01014 0:5e35c180ed4a 423 USB0->CTL |= USB_CTL_ODDRST_MASK;
Racer01014 0:5e35c180ed4a 424
Racer01014 0:5e35c180ed4a 425 USB0->ISTAT = 0xFF; // clear all interrupt status flags
Racer01014 0:5e35c180ed4a 426 USB0->ERRSTAT = 0xFF; // clear all error flags
Racer01014 0:5e35c180ed4a 427 USB0->ERREN = 0xFF; // enable error interrupt sources
Racer01014 0:5e35c180ed4a 428 USB0->ADDR = 0x00; // set default address
Racer01014 0:5e35c180ed4a 429
Racer01014 0:5e35c180ed4a 430 return;
Racer01014 0:5e35c180ed4a 431 }
Racer01014 0:5e35c180ed4a 432
Racer01014 0:5e35c180ed4a 433 // resume interrupt
Racer01014 0:5e35c180ed4a 434 if (istat & USB_ISTAT_RESUME_MASK) {
Racer01014 0:5e35c180ed4a 435 USB0->ISTAT = USB_ISTAT_RESUME_MASK;
Racer01014 0:5e35c180ed4a 436 }
Racer01014 0:5e35c180ed4a 437
Racer01014 0:5e35c180ed4a 438 // SOF interrupt
Racer01014 0:5e35c180ed4a 439 if (istat & USB_ISTAT_SOFTOK_MASK) {
Racer01014 0:5e35c180ed4a 440 USB0->ISTAT = USB_ISTAT_SOFTOK_MASK;
Racer01014 0:5e35c180ed4a 441 // SOF event, read frame number
Racer01014 0:5e35c180ed4a 442 SOF(frameNumber());
Racer01014 0:5e35c180ed4a 443 }
Racer01014 0:5e35c180ed4a 444
Racer01014 0:5e35c180ed4a 445 // stall interrupt
Racer01014 0:5e35c180ed4a 446 if (istat & 1<<7) {
Racer01014 0:5e35c180ed4a 447 if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK)
Racer01014 0:5e35c180ed4a 448 USB0->ENDPOINT[0].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
Racer01014 0:5e35c180ed4a 449 USB0->ISTAT |= USB_ISTAT_STALL_MASK;
Racer01014 0:5e35c180ed4a 450 }
Racer01014 0:5e35c180ed4a 451
Racer01014 0:5e35c180ed4a 452 // token interrupt
Racer01014 0:5e35c180ed4a 453 if (istat & 1<<3) {
Racer01014 0:5e35c180ed4a 454 uint32_t num = (USB0->STAT >> 4) & 0x0F;
Racer01014 0:5e35c180ed4a 455 uint32_t dir = (USB0->STAT >> 3) & 0x01;
Racer01014 0:5e35c180ed4a 456 uint32_t ev_odd = (USB0->STAT >> 2) & 0x01;
Racer01014 0:5e35c180ed4a 457
Racer01014 0:5e35c180ed4a 458 // setup packet
Racer01014 0:5e35c180ed4a 459 if ((num == 0) && (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == SETUP_TOKEN)) {
Racer01014 0:5e35c180ed4a 460 Data1 &= ~0x02;
Racer01014 0:5e35c180ed4a 461 bdt[EP_BDT_IDX(0, TX, EVEN)].info &= ~BD_OWN_MASK;
Racer01014 0:5e35c180ed4a 462 bdt[EP_BDT_IDX(0, TX, ODD)].info &= ~BD_OWN_MASK;
Racer01014 0:5e35c180ed4a 463
Racer01014 0:5e35c180ed4a 464 // EP0 SETUP event (SETUP data received)
Racer01014 0:5e35c180ed4a 465 EP0setupCallback();
Racer01014 0:5e35c180ed4a 466
Racer01014 0:5e35c180ed4a 467 } else {
Racer01014 0:5e35c180ed4a 468 // OUT packet
Racer01014 0:5e35c180ed4a 469 if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == OUT_TOKEN) {
Racer01014 0:5e35c180ed4a 470 if (num == 0)
Racer01014 0:5e35c180ed4a 471 EP0out();
Racer01014 0:5e35c180ed4a 472 else {
Racer01014 0:5e35c180ed4a 473 epComplete |= (1 << EP(num));
Racer01014 0:5e35c180ed4a 474 if ((instance->*(epCallback[EP(num) - 2]))()) {
Racer01014 0:5e35c180ed4a 475 epComplete &= ~(1 << EP(num));
Racer01014 0:5e35c180ed4a 476 }
Racer01014 0:5e35c180ed4a 477 }
Racer01014 0:5e35c180ed4a 478 }
Racer01014 0:5e35c180ed4a 479
Racer01014 0:5e35c180ed4a 480 // IN packet
Racer01014 0:5e35c180ed4a 481 if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == IN_TOKEN) {
Racer01014 0:5e35c180ed4a 482 if (num == 0) {
Racer01014 0:5e35c180ed4a 483 EP0in();
Racer01014 0:5e35c180ed4a 484 if (set_addr == 1) {
Racer01014 0:5e35c180ed4a 485 USB0->ADDR = addr & 0x7F;
Racer01014 0:5e35c180ed4a 486 set_addr = 0;
Racer01014 0:5e35c180ed4a 487 }
Racer01014 0:5e35c180ed4a 488 }
Racer01014 0:5e35c180ed4a 489 else {
Racer01014 0:5e35c180ed4a 490 epComplete |= (1 << (EP(num) + 1));
Racer01014 0:5e35c180ed4a 491 if ((instance->*(epCallback[EP(num) + 1 - 2]))()) {
Racer01014 0:5e35c180ed4a 492 epComplete &= ~(1 << (EP(num) + 1));
Racer01014 0:5e35c180ed4a 493 }
Racer01014 0:5e35c180ed4a 494 }
Racer01014 0:5e35c180ed4a 495 }
Racer01014 0:5e35c180ed4a 496 }
Racer01014 0:5e35c180ed4a 497
Racer01014 0:5e35c180ed4a 498 USB0->ISTAT = USB_ISTAT_TOKDNE_MASK;
Racer01014 0:5e35c180ed4a 499 }
Racer01014 0:5e35c180ed4a 500
Racer01014 0:5e35c180ed4a 501 // sleep interrupt
Racer01014 0:5e35c180ed4a 502 if (istat & 1<<4) {
Racer01014 0:5e35c180ed4a 503 USB0->ISTAT |= USB_ISTAT_SLEEP_MASK;
Racer01014 0:5e35c180ed4a 504 }
Racer01014 0:5e35c180ed4a 505
Racer01014 0:5e35c180ed4a 506 // error interrupt
Racer01014 0:5e35c180ed4a 507 if (istat & USB_ISTAT_ERROR_MASK) {
Racer01014 0:5e35c180ed4a 508 USB0->ERRSTAT = 0xFF;
Racer01014 0:5e35c180ed4a 509 USB0->ISTAT |= USB_ISTAT_ERROR_MASK;
Racer01014 0:5e35c180ed4a 510 }
Racer01014 0:5e35c180ed4a 511 }
Racer01014 0:5e35c180ed4a 512
Racer01014 0:5e35c180ed4a 513
Racer01014 0:5e35c180ed4a 514 #endif