Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

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