max32630fthr quad spi , unexpected spi behavior

Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boonshen 0:a35c40f49345 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
boonshen 0:a35c40f49345 2 *
boonshen 0:a35c40f49345 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
boonshen 0:a35c40f49345 4 * and associated documentation files (the "Software"), to deal in the Software without
boonshen 0:a35c40f49345 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
boonshen 0:a35c40f49345 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
boonshen 0:a35c40f49345 7 * Software is furnished to do so, subject to the following conditions:
boonshen 0:a35c40f49345 8 *
boonshen 0:a35c40f49345 9 * The above copyright notice and this permission notice shall be included in all copies or
boonshen 0:a35c40f49345 10 * substantial portions of the Software.
boonshen 0:a35c40f49345 11 *
boonshen 0:a35c40f49345 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
boonshen 0:a35c40f49345 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
boonshen 0:a35c40f49345 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
boonshen 0:a35c40f49345 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
boonshen 0:a35c40f49345 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
boonshen 0:a35c40f49345 17 */
boonshen 0:a35c40f49345 18
boonshen 0:a35c40f49345 19 #include "stdint.h"
boonshen 0:a35c40f49345 20 #include "USBCDC.h"
boonshen 0:a35c40f49345 21
boonshen 0:a35c40f49345 22 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
boonshen 0:a35c40f49345 23
boonshen 0:a35c40f49345 24 #define DEFAULT_CONFIGURATION (1)
boonshen 0:a35c40f49345 25
boonshen 0:a35c40f49345 26 #define CDC_SET_LINE_CODING 0x20
boonshen 0:a35c40f49345 27 #define CDC_GET_LINE_CODING 0x21
boonshen 0:a35c40f49345 28 #define CDC_SET_CONTROL_LINE_STATE 0x22
boonshen 0:a35c40f49345 29
boonshen 0:a35c40f49345 30 // Control Line State bits
boonshen 0:a35c40f49345 31 #define CLS_DTR (1 << 0)
boonshen 0:a35c40f49345 32 #define CLS_RTS (1 << 1)
boonshen 0:a35c40f49345 33
boonshen 0:a35c40f49345 34 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
boonshen 0:a35c40f49345 35
boonshen 0:a35c40f49345 36 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking): USBDevice(vendor_id, product_id, product_release) {
boonshen 0:a35c40f49345 37 terminal_connected = false;
boonshen 0:a35c40f49345 38 USBDevice::connect(connect_blocking);
boonshen 0:a35c40f49345 39 }
boonshen 0:a35c40f49345 40
boonshen 0:a35c40f49345 41 bool USBCDC::USBCallback_request(void) {
boonshen 0:a35c40f49345 42 /* Called in ISR context */
boonshen 0:a35c40f49345 43
boonshen 0:a35c40f49345 44 bool success = false;
boonshen 0:a35c40f49345 45 CONTROL_TRANSFER * transfer = getTransferPtr();
boonshen 0:a35c40f49345 46
boonshen 0:a35c40f49345 47 /* Process class-specific requests */
boonshen 0:a35c40f49345 48
boonshen 0:a35c40f49345 49 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
boonshen 0:a35c40f49345 50 switch (transfer->setup.bRequest) {
boonshen 0:a35c40f49345 51 case CDC_GET_LINE_CODING:
boonshen 0:a35c40f49345 52 transfer->remaining = 7;
boonshen 0:a35c40f49345 53 transfer->ptr = cdc_line_coding;
boonshen 0:a35c40f49345 54 transfer->direction = DEVICE_TO_HOST;
boonshen 0:a35c40f49345 55 success = true;
boonshen 0:a35c40f49345 56 break;
boonshen 0:a35c40f49345 57 case CDC_SET_LINE_CODING:
boonshen 0:a35c40f49345 58 transfer->remaining = 7;
boonshen 0:a35c40f49345 59 transfer->notify = true;
boonshen 0:a35c40f49345 60 success = true;
boonshen 0:a35c40f49345 61 break;
boonshen 0:a35c40f49345 62 case CDC_SET_CONTROL_LINE_STATE:
boonshen 0:a35c40f49345 63 if (transfer->setup.wValue & CLS_DTR) {
boonshen 0:a35c40f49345 64 terminal_connected = true;
boonshen 0:a35c40f49345 65 } else {
boonshen 0:a35c40f49345 66 terminal_connected = false;
boonshen 0:a35c40f49345 67 }
boonshen 0:a35c40f49345 68 success = true;
boonshen 0:a35c40f49345 69 break;
boonshen 0:a35c40f49345 70 default:
boonshen 0:a35c40f49345 71 break;
boonshen 0:a35c40f49345 72 }
boonshen 0:a35c40f49345 73 }
boonshen 0:a35c40f49345 74
boonshen 0:a35c40f49345 75 return success;
boonshen 0:a35c40f49345 76 }
boonshen 0:a35c40f49345 77
boonshen 0:a35c40f49345 78 void USBCDC::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
boonshen 0:a35c40f49345 79 // Request of setting line coding has 7 bytes
boonshen 0:a35c40f49345 80 if (length != 7) {
boonshen 0:a35c40f49345 81 return;
boonshen 0:a35c40f49345 82 }
boonshen 0:a35c40f49345 83
boonshen 0:a35c40f49345 84 CONTROL_TRANSFER * transfer = getTransferPtr();
boonshen 0:a35c40f49345 85
boonshen 0:a35c40f49345 86 /* Process class-specific requests */
boonshen 0:a35c40f49345 87 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
boonshen 0:a35c40f49345 88 if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
boonshen 0:a35c40f49345 89 if (memcmp(cdc_line_coding, buf, 7)) {
boonshen 0:a35c40f49345 90 memcpy(cdc_line_coding, buf, 7);
boonshen 0:a35c40f49345 91
boonshen 0:a35c40f49345 92 int baud = buf[0] + (buf[1] << 8)
boonshen 0:a35c40f49345 93 + (buf[2] << 16) + (buf[3] << 24);
boonshen 0:a35c40f49345 94 int stop = buf[4];
boonshen 0:a35c40f49345 95 int bits = buf[6];
boonshen 0:a35c40f49345 96 int parity = buf[5];
boonshen 0:a35c40f49345 97
boonshen 0:a35c40f49345 98 lineCodingChanged(baud, bits, parity, stop);
boonshen 0:a35c40f49345 99 }
boonshen 0:a35c40f49345 100 }
boonshen 0:a35c40f49345 101 }
boonshen 0:a35c40f49345 102 }
boonshen 0:a35c40f49345 103
boonshen 0:a35c40f49345 104 // Called in ISR context
boonshen 0:a35c40f49345 105 // Set configuration. Return false if the
boonshen 0:a35c40f49345 106 // configuration is not supported.
boonshen 0:a35c40f49345 107 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
boonshen 0:a35c40f49345 108 if (configuration != DEFAULT_CONFIGURATION) {
boonshen 0:a35c40f49345 109 return false;
boonshen 0:a35c40f49345 110 }
boonshen 0:a35c40f49345 111
boonshen 0:a35c40f49345 112 // Configure endpoints > 0
boonshen 0:a35c40f49345 113 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
boonshen 0:a35c40f49345 114 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
boonshen 0:a35c40f49345 115 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
boonshen 0:a35c40f49345 116
boonshen 0:a35c40f49345 117 // We activate the endpoint to be able to recceive data
boonshen 0:a35c40f49345 118 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
boonshen 0:a35c40f49345 119 return true;
boonshen 0:a35c40f49345 120 }
boonshen 0:a35c40f49345 121
boonshen 0:a35c40f49345 122 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
boonshen 0:a35c40f49345 123 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
boonshen 0:a35c40f49345 124 }
boonshen 0:a35c40f49345 125
boonshen 0:a35c40f49345 126 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
boonshen 0:a35c40f49345 127 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
boonshen 0:a35c40f49345 128 return false;
boonshen 0:a35c40f49345 129 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
boonshen 0:a35c40f49345 130 return false;
boonshen 0:a35c40f49345 131 return true;
boonshen 0:a35c40f49345 132 }
boonshen 0:a35c40f49345 133
boonshen 0:a35c40f49345 134 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
boonshen 0:a35c40f49345 135 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
boonshen 0:a35c40f49345 136 return false;
boonshen 0:a35c40f49345 137 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
boonshen 0:a35c40f49345 138 return false;
boonshen 0:a35c40f49345 139 return true;
boonshen 0:a35c40f49345 140 }
boonshen 0:a35c40f49345 141
boonshen 0:a35c40f49345 142
boonshen 0:a35c40f49345 143 uint8_t * USBCDC::deviceDesc() {
boonshen 0:a35c40f49345 144 static uint8_t deviceDescriptor[] = {
boonshen 0:a35c40f49345 145 18, // bLength
boonshen 0:a35c40f49345 146 1, // bDescriptorType
boonshen 0:a35c40f49345 147 0x10, 0x01, // bcdUSB
boonshen 0:a35c40f49345 148 2, // bDeviceClass
boonshen 0:a35c40f49345 149 0, // bDeviceSubClass
boonshen 0:a35c40f49345 150 0, // bDeviceProtocol
boonshen 0:a35c40f49345 151 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
boonshen 0:a35c40f49345 152 (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)), // idVendor
boonshen 0:a35c40f49345 153 (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct
boonshen 0:a35c40f49345 154 0x00, 0x01, // bcdDevice
boonshen 0:a35c40f49345 155 1, // iManufacturer
boonshen 0:a35c40f49345 156 2, // iProduct
boonshen 0:a35c40f49345 157 3, // iSerialNumber
boonshen 0:a35c40f49345 158 1 // bNumConfigurations
boonshen 0:a35c40f49345 159 };
boonshen 0:a35c40f49345 160 return deviceDescriptor;
boonshen 0:a35c40f49345 161 }
boonshen 0:a35c40f49345 162
boonshen 0:a35c40f49345 163 uint8_t * USBCDC::stringIinterfaceDesc() {
boonshen 0:a35c40f49345 164 static uint8_t stringIinterfaceDescriptor[] = {
boonshen 0:a35c40f49345 165 0x08,
boonshen 0:a35c40f49345 166 STRING_DESCRIPTOR,
boonshen 0:a35c40f49345 167 'C',0,'D',0,'C',0,
boonshen 0:a35c40f49345 168 };
boonshen 0:a35c40f49345 169 return stringIinterfaceDescriptor;
boonshen 0:a35c40f49345 170 }
boonshen 0:a35c40f49345 171
boonshen 0:a35c40f49345 172 uint8_t * USBCDC::stringIproductDesc() {
boonshen 0:a35c40f49345 173 static uint8_t stringIproductDescriptor[] = {
boonshen 0:a35c40f49345 174 0x16,
boonshen 0:a35c40f49345 175 STRING_DESCRIPTOR,
boonshen 0:a35c40f49345 176 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
boonshen 0:a35c40f49345 177 };
boonshen 0:a35c40f49345 178 return stringIproductDescriptor;
boonshen 0:a35c40f49345 179 }
boonshen 0:a35c40f49345 180
boonshen 0:a35c40f49345 181
boonshen 0:a35c40f49345 182 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
boonshen 0:a35c40f49345 183
boonshen 0:a35c40f49345 184 uint8_t * USBCDC::configurationDesc() {
boonshen 0:a35c40f49345 185 static uint8_t configDescriptor[] = {
boonshen 0:a35c40f49345 186 // configuration descriptor
boonshen 0:a35c40f49345 187 9, // bLength
boonshen 0:a35c40f49345 188 2, // bDescriptorType
boonshen 0:a35c40f49345 189 LSB(CONFIG1_DESC_SIZE), // wTotalLength
boonshen 0:a35c40f49345 190 MSB(CONFIG1_DESC_SIZE),
boonshen 0:a35c40f49345 191 2, // bNumInterfaces
boonshen 0:a35c40f49345 192 1, // bConfigurationValue
boonshen 0:a35c40f49345 193 0, // iConfiguration
boonshen 0:a35c40f49345 194 0x80, // bmAttributes
boonshen 0:a35c40f49345 195 50, // bMaxPower
boonshen 0:a35c40f49345 196
boonshen 0:a35c40f49345 197 // IAD to associate the two CDC interfaces
boonshen 0:a35c40f49345 198 0x08, // bLength
boonshen 0:a35c40f49345 199 0x0b, // bDescriptorType
boonshen 0:a35c40f49345 200 0x00, // bFirstInterface
boonshen 0:a35c40f49345 201 0x02, // bInterfaceCount
boonshen 0:a35c40f49345 202 0x02, // bFunctionClass
boonshen 0:a35c40f49345 203 0x02, // bFunctionSubClass
boonshen 0:a35c40f49345 204 0, // bFunctionProtocol
boonshen 0:a35c40f49345 205 0, // iFunction
boonshen 0:a35c40f49345 206
boonshen 0:a35c40f49345 207 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
boonshen 0:a35c40f49345 208 9, // bLength
boonshen 0:a35c40f49345 209 4, // bDescriptorType
boonshen 0:a35c40f49345 210 0, // bInterfaceNumber
boonshen 0:a35c40f49345 211 0, // bAlternateSetting
boonshen 0:a35c40f49345 212 1, // bNumEndpoints
boonshen 0:a35c40f49345 213 0x02, // bInterfaceClass
boonshen 0:a35c40f49345 214 0x02, // bInterfaceSubClass
boonshen 0:a35c40f49345 215 0x01, // bInterfaceProtocol
boonshen 0:a35c40f49345 216 0, // iInterface
boonshen 0:a35c40f49345 217
boonshen 0:a35c40f49345 218 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
boonshen 0:a35c40f49345 219 5, // bFunctionLength
boonshen 0:a35c40f49345 220 0x24, // bDescriptorType
boonshen 0:a35c40f49345 221 0x00, // bDescriptorSubtype
boonshen 0:a35c40f49345 222 0x10, 0x01, // bcdCDC
boonshen 0:a35c40f49345 223
boonshen 0:a35c40f49345 224 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
boonshen 0:a35c40f49345 225 5, // bFunctionLength
boonshen 0:a35c40f49345 226 0x24, // bDescriptorType
boonshen 0:a35c40f49345 227 0x01, // bDescriptorSubtype
boonshen 0:a35c40f49345 228 0x03, // bmCapabilities
boonshen 0:a35c40f49345 229 1, // bDataInterface
boonshen 0:a35c40f49345 230
boonshen 0:a35c40f49345 231 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
boonshen 0:a35c40f49345 232 4, // bFunctionLength
boonshen 0:a35c40f49345 233 0x24, // bDescriptorType
boonshen 0:a35c40f49345 234 0x02, // bDescriptorSubtype
boonshen 0:a35c40f49345 235 0x06, // bmCapabilities
boonshen 0:a35c40f49345 236
boonshen 0:a35c40f49345 237 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
boonshen 0:a35c40f49345 238 5, // bFunctionLength
boonshen 0:a35c40f49345 239 0x24, // bDescriptorType
boonshen 0:a35c40f49345 240 0x06, // bDescriptorSubtype
boonshen 0:a35c40f49345 241 0, // bMasterInterface
boonshen 0:a35c40f49345 242 1, // bSlaveInterface0
boonshen 0:a35c40f49345 243
boonshen 0:a35c40f49345 244 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
boonshen 0:a35c40f49345 245 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 246 ENDPOINT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 247 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
boonshen 0:a35c40f49345 248 E_INTERRUPT, // bmAttributes (0x03=intr)
boonshen 0:a35c40f49345 249 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
boonshen 0:a35c40f49345 250 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
boonshen 0:a35c40f49345 251 16, // bInterval
boonshen 0:a35c40f49345 252
boonshen 0:a35c40f49345 253
boonshen 0:a35c40f49345 254
boonshen 0:a35c40f49345 255
boonshen 0:a35c40f49345 256 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
boonshen 0:a35c40f49345 257 9, // bLength
boonshen 0:a35c40f49345 258 4, // bDescriptorType
boonshen 0:a35c40f49345 259 1, // bInterfaceNumber
boonshen 0:a35c40f49345 260 0, // bAlternateSetting
boonshen 0:a35c40f49345 261 2, // bNumEndpoints
boonshen 0:a35c40f49345 262 0x0A, // bInterfaceClass
boonshen 0:a35c40f49345 263 0x00, // bInterfaceSubClass
boonshen 0:a35c40f49345 264 0x00, // bInterfaceProtocol
boonshen 0:a35c40f49345 265 0, // iInterface
boonshen 0:a35c40f49345 266
boonshen 0:a35c40f49345 267 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
boonshen 0:a35c40f49345 268 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 269 ENDPOINT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 270 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
boonshen 0:a35c40f49345 271 E_BULK, // bmAttributes (0x02=bulk)
boonshen 0:a35c40f49345 272 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
boonshen 0:a35c40f49345 273 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
boonshen 0:a35c40f49345 274 0, // bInterval
boonshen 0:a35c40f49345 275
boonshen 0:a35c40f49345 276 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
boonshen 0:a35c40f49345 277 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 278 ENDPOINT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 279 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
boonshen 0:a35c40f49345 280 E_BULK, // bmAttributes (0x02=bulk)
boonshen 0:a35c40f49345 281 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
boonshen 0:a35c40f49345 282 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
boonshen 0:a35c40f49345 283 0 // bInterval
boonshen 0:a35c40f49345 284 };
boonshen 0:a35c40f49345 285 return configDescriptor;
boonshen 0:a35c40f49345 286 }