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 "USBHAL.h"
boonshen 0:a35c40f49345 21 #include "USBHID.h"
boonshen 0:a35c40f49345 22
boonshen 0:a35c40f49345 23
boonshen 0:a35c40f49345 24 USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
boonshen 0:a35c40f49345 25 {
boonshen 0:a35c40f49345 26 output_length = output_report_length;
boonshen 0:a35c40f49345 27 input_length = input_report_length;
boonshen 0:a35c40f49345 28 if(connect) {
boonshen 0:a35c40f49345 29 USBDevice::connect();
boonshen 0:a35c40f49345 30 }
boonshen 0:a35c40f49345 31 }
boonshen 0:a35c40f49345 32
boonshen 0:a35c40f49345 33
boonshen 0:a35c40f49345 34 bool USBHID::send(HID_REPORT *report)
boonshen 0:a35c40f49345 35 {
boonshen 0:a35c40f49345 36 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
boonshen 0:a35c40f49345 37 }
boonshen 0:a35c40f49345 38
boonshen 0:a35c40f49345 39 bool USBHID::sendNB(HID_REPORT *report)
boonshen 0:a35c40f49345 40 {
boonshen 0:a35c40f49345 41 return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
boonshen 0:a35c40f49345 42 }
boonshen 0:a35c40f49345 43
boonshen 0:a35c40f49345 44
boonshen 0:a35c40f49345 45 bool USBHID::read(HID_REPORT *report)
boonshen 0:a35c40f49345 46 {
boonshen 0:a35c40f49345 47 uint32_t bytesRead = 0;
boonshen 0:a35c40f49345 48 bool result;
boonshen 0:a35c40f49345 49 result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
boonshen 0:a35c40f49345 50 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
boonshen 0:a35c40f49345 51 return false;
boonshen 0:a35c40f49345 52 report->length = bytesRead;
boonshen 0:a35c40f49345 53 return result;
boonshen 0:a35c40f49345 54 }
boonshen 0:a35c40f49345 55
boonshen 0:a35c40f49345 56
boonshen 0:a35c40f49345 57 bool USBHID::readNB(HID_REPORT *report)
boonshen 0:a35c40f49345 58 {
boonshen 0:a35c40f49345 59 uint32_t bytesRead = 0;
boonshen 0:a35c40f49345 60 bool result;
boonshen 0:a35c40f49345 61 result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
boonshen 0:a35c40f49345 62 // if readEP_NB did not succeed, does not issue a readStart
boonshen 0:a35c40f49345 63 if (!result)
boonshen 0:a35c40f49345 64 return false;
boonshen 0:a35c40f49345 65 report->length = bytesRead;
boonshen 0:a35c40f49345 66 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
boonshen 0:a35c40f49345 67 return false;
boonshen 0:a35c40f49345 68 return result;
boonshen 0:a35c40f49345 69 }
boonshen 0:a35c40f49345 70
boonshen 0:a35c40f49345 71
boonshen 0:a35c40f49345 72 uint16_t USBHID::reportDescLength() {
boonshen 0:a35c40f49345 73 reportDesc();
boonshen 0:a35c40f49345 74 return reportLength;
boonshen 0:a35c40f49345 75 }
boonshen 0:a35c40f49345 76
boonshen 0:a35c40f49345 77
boonshen 0:a35c40f49345 78
boonshen 0:a35c40f49345 79 //
boonshen 0:a35c40f49345 80 // Route callbacks from lower layers to class(es)
boonshen 0:a35c40f49345 81 //
boonshen 0:a35c40f49345 82
boonshen 0:a35c40f49345 83
boonshen 0:a35c40f49345 84 // Called in ISR context
boonshen 0:a35c40f49345 85 // Called by USBDevice on Endpoint0 request
boonshen 0:a35c40f49345 86 // This is used to handle extensions to standard requests
boonshen 0:a35c40f49345 87 // and class specific requests
boonshen 0:a35c40f49345 88 // Return true if class handles this request
boonshen 0:a35c40f49345 89 bool USBHID::USBCallback_request() {
boonshen 0:a35c40f49345 90 bool success = false;
boonshen 0:a35c40f49345 91 CONTROL_TRANSFER * transfer = getTransferPtr();
boonshen 0:a35c40f49345 92 uint8_t *hidDescriptor;
boonshen 0:a35c40f49345 93
boonshen 0:a35c40f49345 94 // Process additional standard requests
boonshen 0:a35c40f49345 95
boonshen 0:a35c40f49345 96 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
boonshen 0:a35c40f49345 97 {
boonshen 0:a35c40f49345 98 switch (transfer->setup.bRequest)
boonshen 0:a35c40f49345 99 {
boonshen 0:a35c40f49345 100 case GET_DESCRIPTOR:
boonshen 0:a35c40f49345 101 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
boonshen 0:a35c40f49345 102 {
boonshen 0:a35c40f49345 103 case REPORT_DESCRIPTOR:
boonshen 0:a35c40f49345 104 if ((reportDesc() != NULL) \
boonshen 0:a35c40f49345 105 && (reportDescLength() != 0))
boonshen 0:a35c40f49345 106 {
boonshen 0:a35c40f49345 107 transfer->remaining = reportDescLength();
boonshen 0:a35c40f49345 108 transfer->ptr = reportDesc();
boonshen 0:a35c40f49345 109 transfer->direction = DEVICE_TO_HOST;
boonshen 0:a35c40f49345 110 success = true;
boonshen 0:a35c40f49345 111 }
boonshen 0:a35c40f49345 112 break;
boonshen 0:a35c40f49345 113 case HID_DESCRIPTOR:
boonshen 0:a35c40f49345 114 // Find the HID descriptor, after the configuration descriptor
boonshen 0:a35c40f49345 115 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
boonshen 0:a35c40f49345 116 if (hidDescriptor != NULL)
boonshen 0:a35c40f49345 117 {
boonshen 0:a35c40f49345 118 transfer->remaining = HID_DESCRIPTOR_LENGTH;
boonshen 0:a35c40f49345 119 transfer->ptr = hidDescriptor;
boonshen 0:a35c40f49345 120 transfer->direction = DEVICE_TO_HOST;
boonshen 0:a35c40f49345 121 success = true;
boonshen 0:a35c40f49345 122 }
boonshen 0:a35c40f49345 123 break;
boonshen 0:a35c40f49345 124
boonshen 0:a35c40f49345 125 default:
boonshen 0:a35c40f49345 126 break;
boonshen 0:a35c40f49345 127 }
boonshen 0:a35c40f49345 128 break;
boonshen 0:a35c40f49345 129 default:
boonshen 0:a35c40f49345 130 break;
boonshen 0:a35c40f49345 131 }
boonshen 0:a35c40f49345 132 }
boonshen 0:a35c40f49345 133
boonshen 0:a35c40f49345 134 // Process class-specific requests
boonshen 0:a35c40f49345 135
boonshen 0:a35c40f49345 136 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
boonshen 0:a35c40f49345 137 {
boonshen 0:a35c40f49345 138 switch (transfer->setup.bRequest)
boonshen 0:a35c40f49345 139 {
boonshen 0:a35c40f49345 140 case SET_REPORT:
boonshen 0:a35c40f49345 141 // First byte will be used for report ID
boonshen 0:a35c40f49345 142 outputReport.data[0] = transfer->setup.wValue & 0xff;
boonshen 0:a35c40f49345 143 outputReport.length = transfer->setup.wLength + 1;
boonshen 0:a35c40f49345 144
boonshen 0:a35c40f49345 145 transfer->remaining = sizeof(outputReport.data) - 1;
boonshen 0:a35c40f49345 146 transfer->ptr = &outputReport.data[1];
boonshen 0:a35c40f49345 147 transfer->direction = HOST_TO_DEVICE;
boonshen 0:a35c40f49345 148 transfer->notify = true;
boonshen 0:a35c40f49345 149 success = true;
boonshen 0:a35c40f49345 150 default:
boonshen 0:a35c40f49345 151 break;
boonshen 0:a35c40f49345 152 }
boonshen 0:a35c40f49345 153 }
boonshen 0:a35c40f49345 154
boonshen 0:a35c40f49345 155 return success;
boonshen 0:a35c40f49345 156 }
boonshen 0:a35c40f49345 157
boonshen 0:a35c40f49345 158
boonshen 0:a35c40f49345 159 #define DEFAULT_CONFIGURATION (1)
boonshen 0:a35c40f49345 160
boonshen 0:a35c40f49345 161
boonshen 0:a35c40f49345 162 // Called in ISR context
boonshen 0:a35c40f49345 163 // Set configuration. Return false if the
boonshen 0:a35c40f49345 164 // configuration is not supported
boonshen 0:a35c40f49345 165 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
boonshen 0:a35c40f49345 166 if (configuration != DEFAULT_CONFIGURATION) {
boonshen 0:a35c40f49345 167 return false;
boonshen 0:a35c40f49345 168 }
boonshen 0:a35c40f49345 169
boonshen 0:a35c40f49345 170 // Configure endpoints > 0
boonshen 0:a35c40f49345 171 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
boonshen 0:a35c40f49345 172 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
boonshen 0:a35c40f49345 173
boonshen 0:a35c40f49345 174 // We activate the endpoint to be able to recceive data
boonshen 0:a35c40f49345 175 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
boonshen 0:a35c40f49345 176 return true;
boonshen 0:a35c40f49345 177 }
boonshen 0:a35c40f49345 178
boonshen 0:a35c40f49345 179
boonshen 0:a35c40f49345 180 uint8_t * USBHID::stringIinterfaceDesc() {
boonshen 0:a35c40f49345 181 static uint8_t stringIinterfaceDescriptor[] = {
boonshen 0:a35c40f49345 182 0x08, //bLength
boonshen 0:a35c40f49345 183 STRING_DESCRIPTOR, //bDescriptorType 0x03
boonshen 0:a35c40f49345 184 'H',0,'I',0,'D',0, //bString iInterface - HID
boonshen 0:a35c40f49345 185 };
boonshen 0:a35c40f49345 186 return stringIinterfaceDescriptor;
boonshen 0:a35c40f49345 187 }
boonshen 0:a35c40f49345 188
boonshen 0:a35c40f49345 189 uint8_t * USBHID::stringIproductDesc() {
boonshen 0:a35c40f49345 190 static uint8_t stringIproductDescriptor[] = {
boonshen 0:a35c40f49345 191 0x16, //bLength
boonshen 0:a35c40f49345 192 STRING_DESCRIPTOR, //bDescriptorType 0x03
boonshen 0:a35c40f49345 193 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
boonshen 0:a35c40f49345 194 };
boonshen 0:a35c40f49345 195 return stringIproductDescriptor;
boonshen 0:a35c40f49345 196 }
boonshen 0:a35c40f49345 197
boonshen 0:a35c40f49345 198
boonshen 0:a35c40f49345 199
boonshen 0:a35c40f49345 200 uint8_t * USBHID::reportDesc() {
boonshen 0:a35c40f49345 201 static uint8_t reportDescriptor[] = {
boonshen 0:a35c40f49345 202 0x06, LSB(0xFFAB), MSB(0xFFAB),
boonshen 0:a35c40f49345 203 0x0A, LSB(0x0200), MSB(0x0200),
boonshen 0:a35c40f49345 204 0xA1, 0x01, // Collection 0x01
boonshen 0:a35c40f49345 205 0x75, 0x08, // report size = 8 bits
boonshen 0:a35c40f49345 206 0x15, 0x00, // logical minimum = 0
boonshen 0:a35c40f49345 207 0x26, 0xFF, 0x00, // logical maximum = 255
boonshen 0:a35c40f49345 208 0x95, input_length, // report count
boonshen 0:a35c40f49345 209 0x09, 0x01, // usage
boonshen 0:a35c40f49345 210 0x81, 0x02, // Input (array)
boonshen 0:a35c40f49345 211 0x95, output_length,// report count
boonshen 0:a35c40f49345 212 0x09, 0x02, // usage
boonshen 0:a35c40f49345 213 0x91, 0x02, // Output (array)
boonshen 0:a35c40f49345 214 0xC0 // end collection
boonshen 0:a35c40f49345 215
boonshen 0:a35c40f49345 216 };
boonshen 0:a35c40f49345 217 reportLength = sizeof(reportDescriptor);
boonshen 0:a35c40f49345 218 return reportDescriptor;
boonshen 0:a35c40f49345 219 }
boonshen 0:a35c40f49345 220
boonshen 0:a35c40f49345 221 #define DEFAULT_CONFIGURATION (1)
boonshen 0:a35c40f49345 222 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
boonshen 0:a35c40f49345 223 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
boonshen 0:a35c40f49345 224 + (1 * HID_DESCRIPTOR_LENGTH) \
boonshen 0:a35c40f49345 225 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
boonshen 0:a35c40f49345 226
boonshen 0:a35c40f49345 227 uint8_t * USBHID::configurationDesc() {
boonshen 0:a35c40f49345 228 static uint8_t configurationDescriptor[] = {
boonshen 0:a35c40f49345 229 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
boonshen 0:a35c40f49345 230 CONFIGURATION_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 231 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
boonshen 0:a35c40f49345 232 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
boonshen 0:a35c40f49345 233 0x01, // bNumInterfaces
boonshen 0:a35c40f49345 234 DEFAULT_CONFIGURATION, // bConfigurationValue
boonshen 0:a35c40f49345 235 0x00, // iConfiguration
boonshen 0:a35c40f49345 236 C_RESERVED | C_SELF_POWERED, // bmAttributes
boonshen 0:a35c40f49345 237 C_POWER(0), // bMaxPower
boonshen 0:a35c40f49345 238
boonshen 0:a35c40f49345 239 INTERFACE_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 240 INTERFACE_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 241 0x00, // bInterfaceNumber
boonshen 0:a35c40f49345 242 0x00, // bAlternateSetting
boonshen 0:a35c40f49345 243 0x02, // bNumEndpoints
boonshen 0:a35c40f49345 244 HID_CLASS, // bInterfaceClass
boonshen 0:a35c40f49345 245 HID_SUBCLASS_NONE, // bInterfaceSubClass
boonshen 0:a35c40f49345 246 HID_PROTOCOL_NONE, // bInterfaceProtocol
boonshen 0:a35c40f49345 247 0x00, // iInterface
boonshen 0:a35c40f49345 248
boonshen 0:a35c40f49345 249 HID_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 250 HID_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 251 LSB(HID_VERSION_1_11), // bcdHID (LSB)
boonshen 0:a35c40f49345 252 MSB(HID_VERSION_1_11), // bcdHID (MSB)
boonshen 0:a35c40f49345 253 0x00, // bCountryCode
boonshen 0:a35c40f49345 254 0x01, // bNumDescriptors
boonshen 0:a35c40f49345 255 REPORT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 256 (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
boonshen 0:a35c40f49345 257 (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
boonshen 0:a35c40f49345 258
boonshen 0:a35c40f49345 259 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 260 ENDPOINT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 261 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
boonshen 0:a35c40f49345 262 E_INTERRUPT, // bmAttributes
boonshen 0:a35c40f49345 263 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
boonshen 0:a35c40f49345 264 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
boonshen 0:a35c40f49345 265 1, // bInterval (milliseconds)
boonshen 0:a35c40f49345 266
boonshen 0:a35c40f49345 267 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
boonshen 0:a35c40f49345 268 ENDPOINT_DESCRIPTOR, // bDescriptorType
boonshen 0:a35c40f49345 269 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
boonshen 0:a35c40f49345 270 E_INTERRUPT, // bmAttributes
boonshen 0:a35c40f49345 271 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
boonshen 0:a35c40f49345 272 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
boonshen 0:a35c40f49345 273 1, // bInterval (milliseconds)
boonshen 0:a35c40f49345 274 };
boonshen 0:a35c40f49345 275 return configurationDescriptor;
boonshen 0:a35c40f49345 276 }