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 #include "stdint.h"
Perijah 0:ecc3925d3f8c 20 #include "USBCDC.h"
Perijah 0:ecc3925d3f8c 21
Perijah 0:ecc3925d3f8c 22 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
Perijah 0:ecc3925d3f8c 23
Perijah 0:ecc3925d3f8c 24 #define DEFAULT_CONFIGURATION (1)
Perijah 0:ecc3925d3f8c 25
Perijah 0:ecc3925d3f8c 26 #define CDC_SET_LINE_CODING 0x20
Perijah 0:ecc3925d3f8c 27 #define CDC_GET_LINE_CODING 0x21
Perijah 0:ecc3925d3f8c 28 #define CDC_SET_CONTROL_LINE_STATE 0x22
Perijah 0:ecc3925d3f8c 29
Perijah 0:ecc3925d3f8c 30 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
Perijah 0:ecc3925d3f8c 31
Perijah 0:ecc3925d3f8c 32 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Perijah 0:ecc3925d3f8c 33 terminal_connected = false;
Perijah 0:ecc3925d3f8c 34 USBDevice::connect();
Perijah 0:ecc3925d3f8c 35 }
Perijah 0:ecc3925d3f8c 36
Perijah 0:ecc3925d3f8c 37 bool USBCDC::USBCallback_request(void) {
Perijah 0:ecc3925d3f8c 38 /* Called in ISR context */
Perijah 0:ecc3925d3f8c 39
Perijah 0:ecc3925d3f8c 40 bool success = false;
Perijah 0:ecc3925d3f8c 41 CONTROL_TRANSFER * transfer = getTransferPtr();
Perijah 0:ecc3925d3f8c 42
Perijah 0:ecc3925d3f8c 43 /* Process class-specific requests */
Perijah 0:ecc3925d3f8c 44
Perijah 0:ecc3925d3f8c 45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Perijah 0:ecc3925d3f8c 46 switch (transfer->setup.bRequest) {
Perijah 0:ecc3925d3f8c 47 case CDC_GET_LINE_CODING:
Perijah 0:ecc3925d3f8c 48 transfer->remaining = 7;
Perijah 0:ecc3925d3f8c 49 transfer->ptr = cdc_line_coding;
Perijah 0:ecc3925d3f8c 50 transfer->direction = DEVICE_TO_HOST;
Perijah 0:ecc3925d3f8c 51 success = true;
Perijah 0:ecc3925d3f8c 52 break;
Perijah 0:ecc3925d3f8c 53 case CDC_SET_LINE_CODING:
Perijah 0:ecc3925d3f8c 54 transfer->remaining = 7;
Perijah 0:ecc3925d3f8c 55 success = true;
Perijah 0:ecc3925d3f8c 56 terminal_connected = true;
Perijah 0:ecc3925d3f8c 57 break;
Perijah 0:ecc3925d3f8c 58 case CDC_SET_CONTROL_LINE_STATE:
Perijah 0:ecc3925d3f8c 59 terminal_connected = false;
Perijah 0:ecc3925d3f8c 60 success = true;
Perijah 0:ecc3925d3f8c 61 break;
Perijah 0:ecc3925d3f8c 62 default:
Perijah 0:ecc3925d3f8c 63 break;
Perijah 0:ecc3925d3f8c 64 }
Perijah 0:ecc3925d3f8c 65 }
Perijah 0:ecc3925d3f8c 66
Perijah 0:ecc3925d3f8c 67 return success;
Perijah 0:ecc3925d3f8c 68 }
Perijah 0:ecc3925d3f8c 69
Perijah 0:ecc3925d3f8c 70
Perijah 0:ecc3925d3f8c 71 // Called in ISR context
Perijah 0:ecc3925d3f8c 72 // Set configuration. Return false if the
Perijah 0:ecc3925d3f8c 73 // configuration is not supported.
Perijah 0:ecc3925d3f8c 74 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
Perijah 0:ecc3925d3f8c 75 if (configuration != DEFAULT_CONFIGURATION) {
Perijah 0:ecc3925d3f8c 76 return false;
Perijah 0:ecc3925d3f8c 77 }
Perijah 0:ecc3925d3f8c 78
Perijah 0:ecc3925d3f8c 79 // Configure endpoints > 0
Perijah 0:ecc3925d3f8c 80 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Perijah 0:ecc3925d3f8c 81 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 82 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 83
Perijah 0:ecc3925d3f8c 84 // We activate the endpoint to be able to recceive data
Perijah 0:ecc3925d3f8c 85 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 86 return true;
Perijah 0:ecc3925d3f8c 87 }
Perijah 0:ecc3925d3f8c 88
Perijah 0:ecc3925d3f8c 89 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
Perijah 0:ecc3925d3f8c 90 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
Perijah 0:ecc3925d3f8c 91 }
Perijah 0:ecc3925d3f8c 92
Perijah 0:ecc3925d3f8c 93 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
Perijah 0:ecc3925d3f8c 94 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Perijah 0:ecc3925d3f8c 95 return false;
Perijah 0:ecc3925d3f8c 96 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Perijah 0:ecc3925d3f8c 97 return false;
Perijah 0:ecc3925d3f8c 98 return true;
Perijah 0:ecc3925d3f8c 99 }
Perijah 0:ecc3925d3f8c 100
Perijah 0:ecc3925d3f8c 101 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
Perijah 0:ecc3925d3f8c 102 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Perijah 0:ecc3925d3f8c 103 return false;
Perijah 0:ecc3925d3f8c 104 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Perijah 0:ecc3925d3f8c 105 return false;
Perijah 0:ecc3925d3f8c 106 return true;
Perijah 0:ecc3925d3f8c 107 }
Perijah 0:ecc3925d3f8c 108
Perijah 0:ecc3925d3f8c 109
Perijah 0:ecc3925d3f8c 110 uint8_t * USBCDC::deviceDesc() {
Perijah 0:ecc3925d3f8c 111 static uint8_t deviceDescriptor[] = {
Perijah 0:ecc3925d3f8c 112 18, // bLength
Perijah 0:ecc3925d3f8c 113 1, // bDescriptorType
Perijah 0:ecc3925d3f8c 114 0x10, 0x01, // bcdUSB
Perijah 0:ecc3925d3f8c 115 2, // bDeviceClass
Perijah 0:ecc3925d3f8c 116 0, // bDeviceSubClass
Perijah 0:ecc3925d3f8c 117 0, // bDeviceProtocol
Perijah 0:ecc3925d3f8c 118 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
Perijah 0:ecc3925d3f8c 119 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
Perijah 0:ecc3925d3f8c 120 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
Perijah 0:ecc3925d3f8c 121 0x00, 0x01, // bcdDevice
Perijah 0:ecc3925d3f8c 122 1, // iManufacturer
Perijah 0:ecc3925d3f8c 123 2, // iProduct
Perijah 0:ecc3925d3f8c 124 3, // iSerialNumber
Perijah 0:ecc3925d3f8c 125 1 // bNumConfigurations
Perijah 0:ecc3925d3f8c 126 };
Perijah 0:ecc3925d3f8c 127 return deviceDescriptor;
Perijah 0:ecc3925d3f8c 128 }
Perijah 0:ecc3925d3f8c 129
Perijah 0:ecc3925d3f8c 130 uint8_t * USBCDC::stringIinterfaceDesc() {
Perijah 0:ecc3925d3f8c 131 static uint8_t stringIinterfaceDescriptor[] = {
Perijah 0:ecc3925d3f8c 132 0x08,
Perijah 0:ecc3925d3f8c 133 STRING_DESCRIPTOR,
Perijah 0:ecc3925d3f8c 134 'C',0,'D',0,'C',0,
Perijah 0:ecc3925d3f8c 135 };
Perijah 0:ecc3925d3f8c 136 return stringIinterfaceDescriptor;
Perijah 0:ecc3925d3f8c 137 }
Perijah 0:ecc3925d3f8c 138
Perijah 0:ecc3925d3f8c 139 uint8_t * USBCDC::stringIproductDesc() {
Perijah 0:ecc3925d3f8c 140 static uint8_t stringIproductDescriptor[] = {
Perijah 0:ecc3925d3f8c 141 0x16,
Perijah 0:ecc3925d3f8c 142 STRING_DESCRIPTOR,
Perijah 0:ecc3925d3f8c 143 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
Perijah 0:ecc3925d3f8c 144 };
Perijah 0:ecc3925d3f8c 145 return stringIproductDescriptor;
Perijah 0:ecc3925d3f8c 146 }
Perijah 0:ecc3925d3f8c 147
Perijah 0:ecc3925d3f8c 148
Perijah 0:ecc3925d3f8c 149 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
Perijah 0:ecc3925d3f8c 150
Perijah 0:ecc3925d3f8c 151 uint8_t * USBCDC::configurationDesc() {
Perijah 0:ecc3925d3f8c 152 static uint8_t configDescriptor[] = {
Perijah 0:ecc3925d3f8c 153 // configuration descriptor
Perijah 0:ecc3925d3f8c 154 9, // bLength
Perijah 0:ecc3925d3f8c 155 2, // bDescriptorType
Perijah 0:ecc3925d3f8c 156 LSB(CONFIG1_DESC_SIZE), // wTotalLength
Perijah 0:ecc3925d3f8c 157 MSB(CONFIG1_DESC_SIZE),
Perijah 0:ecc3925d3f8c 158 2, // bNumInterfaces
Perijah 0:ecc3925d3f8c 159 1, // bConfigurationValue
Perijah 0:ecc3925d3f8c 160 0, // iConfiguration
Perijah 0:ecc3925d3f8c 161 0x80, // bmAttributes
Perijah 0:ecc3925d3f8c 162 50, // bMaxPower
Perijah 0:ecc3925d3f8c 163
Perijah 0:ecc3925d3f8c 164 // IAD to associate the two CDC interfaces
Perijah 0:ecc3925d3f8c 165 0x08, // bLength
Perijah 0:ecc3925d3f8c 166 0x0b, // bDescriptorType
Perijah 0:ecc3925d3f8c 167 0x00, // bFirstInterface
Perijah 0:ecc3925d3f8c 168 0x02, // bInterfaceCount
Perijah 0:ecc3925d3f8c 169 0x02, // bFunctionClass
Perijah 0:ecc3925d3f8c 170 0x02, // bFunctionSubClass
Perijah 0:ecc3925d3f8c 171 0, // bFunctionProtocol
Perijah 0:ecc3925d3f8c 172 0, // iFunction
Perijah 0:ecc3925d3f8c 173
Perijah 0:ecc3925d3f8c 174 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Perijah 0:ecc3925d3f8c 175 9, // bLength
Perijah 0:ecc3925d3f8c 176 4, // bDescriptorType
Perijah 0:ecc3925d3f8c 177 0, // bInterfaceNumber
Perijah 0:ecc3925d3f8c 178 0, // bAlternateSetting
Perijah 0:ecc3925d3f8c 179 1, // bNumEndpoints
Perijah 0:ecc3925d3f8c 180 0x02, // bInterfaceClass
Perijah 0:ecc3925d3f8c 181 0x02, // bInterfaceSubClass
Perijah 0:ecc3925d3f8c 182 0x01, // bInterfaceProtocol
Perijah 0:ecc3925d3f8c 183 0, // iInterface
Perijah 0:ecc3925d3f8c 184
Perijah 0:ecc3925d3f8c 185 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
Perijah 0:ecc3925d3f8c 186 5, // bFunctionLength
Perijah 0:ecc3925d3f8c 187 0x24, // bDescriptorType
Perijah 0:ecc3925d3f8c 188 0x00, // bDescriptorSubtype
Perijah 0:ecc3925d3f8c 189 0x10, 0x01, // bcdCDC
Perijah 0:ecc3925d3f8c 190
Perijah 0:ecc3925d3f8c 191 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
Perijah 0:ecc3925d3f8c 192 5, // bFunctionLength
Perijah 0:ecc3925d3f8c 193 0x24, // bDescriptorType
Perijah 0:ecc3925d3f8c 194 0x01, // bDescriptorSubtype
Perijah 0:ecc3925d3f8c 195 0x03, // bmCapabilities
Perijah 0:ecc3925d3f8c 196 1, // bDataInterface
Perijah 0:ecc3925d3f8c 197
Perijah 0:ecc3925d3f8c 198 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
Perijah 0:ecc3925d3f8c 199 4, // bFunctionLength
Perijah 0:ecc3925d3f8c 200 0x24, // bDescriptorType
Perijah 0:ecc3925d3f8c 201 0x02, // bDescriptorSubtype
Perijah 0:ecc3925d3f8c 202 0x06, // bmCapabilities
Perijah 0:ecc3925d3f8c 203
Perijah 0:ecc3925d3f8c 204 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
Perijah 0:ecc3925d3f8c 205 5, // bFunctionLength
Perijah 0:ecc3925d3f8c 206 0x24, // bDescriptorType
Perijah 0:ecc3925d3f8c 207 0x06, // bDescriptorSubtype
Perijah 0:ecc3925d3f8c 208 0, // bMasterInterface
Perijah 0:ecc3925d3f8c 209 1, // bSlaveInterface0
Perijah 0:ecc3925d3f8c 210
Perijah 0:ecc3925d3f8c 211 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Perijah 0:ecc3925d3f8c 212 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 213 ENDPOINT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 214 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Perijah 0:ecc3925d3f8c 215 E_INTERRUPT, // bmAttributes (0x03=intr)
Perijah 0:ecc3925d3f8c 216 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 217 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 218 16, // bInterval
Perijah 0:ecc3925d3f8c 219
Perijah 0:ecc3925d3f8c 220
Perijah 0:ecc3925d3f8c 221
Perijah 0:ecc3925d3f8c 222
Perijah 0:ecc3925d3f8c 223 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Perijah 0:ecc3925d3f8c 224 9, // bLength
Perijah 0:ecc3925d3f8c 225 4, // bDescriptorType
Perijah 0:ecc3925d3f8c 226 1, // bInterfaceNumber
Perijah 0:ecc3925d3f8c 227 0, // bAlternateSetting
Perijah 0:ecc3925d3f8c 228 2, // bNumEndpoints
Perijah 0:ecc3925d3f8c 229 0x0A, // bInterfaceClass
Perijah 0:ecc3925d3f8c 230 0x00, // bInterfaceSubClass
Perijah 0:ecc3925d3f8c 231 0x00, // bInterfaceProtocol
Perijah 0:ecc3925d3f8c 232 0, // iInterface
Perijah 0:ecc3925d3f8c 233
Perijah 0:ecc3925d3f8c 234 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Perijah 0:ecc3925d3f8c 235 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 236 ENDPOINT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 237 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Perijah 0:ecc3925d3f8c 238 E_BULK, // bmAttributes (0x02=bulk)
Perijah 0:ecc3925d3f8c 239 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 240 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 241 0, // bInterval
Perijah 0:ecc3925d3f8c 242
Perijah 0:ecc3925d3f8c 243 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Perijah 0:ecc3925d3f8c 244 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 245 ENDPOINT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 246 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Perijah 0:ecc3925d3f8c 247 E_BULK, // bmAttributes (0x02=bulk)
Perijah 0:ecc3925d3f8c 248 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 249 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 250 0 // bInterval
Perijah 0:ecc3925d3f8c 251 };
Perijah 0:ecc3925d3f8c 252 return configDescriptor;
Perijah 0:ecc3925d3f8c 253 }