aboutsummaryrefslogtreecommitdiffstats
path: root/Demos/DualRole/ClassDriver/MouseHostDevice/Descriptors.c
blob: 71643be29ee108987bf82704c3929d0a623044fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
             LUFA Library
     Copyright (C) Dean Camera, 2012.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  USB Device Descriptors, for library use when in USB device mode. Descriptors are special
 *  computer-readable structures which the host requests upon device enumeration, to determine
 *  the device's capabilities and functions.
 */

#include "Descriptors.h"

/** HID class report descriptor. This is a special descriptor constructed with values from the
 *  USBIF HID class specification to describe the reports and capabilities of the HID device. This
 *  descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
 *  the device will send, and what it may be sent back from the host. Refer to the HID specification for
 *  more details on HID report descriptors.
 */
const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
{
	/* Use the HID class driver's standard Mouse report.
	 *   Min X/Y Axis values: -1
	 *   Max X/Y Axis values:  1
	 *   Min physical X/Y Axis values (used to determine resolution): -1
	 *   Max physical X/Y Axis values (used to determine resolution):  1
	 *   Buttons: 3
	 *   Absolute screen coordinates: false
	 */
	HID_DESCRIPTOR_MOUSE(-1, 1, -1, 1, 3, false)
};

/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
 *  device characteristics, including the supported USB version, control endpoint size and the
 *  number of device configurations. The descriptor is read out by the USB host when the enumeration
 *  process begins.
 */
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
	.Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},

	.USBSpecification       = VERSION_BCD(01.10),
	.Class                  = USB_CSCP_NoDeviceClass,
	.SubClass               = USB_CSCP_NoDeviceSubclass,
	.Protocol               = USB_CSCP_NoDeviceProtocol,

	.Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,

	.VendorID               = 0x03EB,
	.ProductID              = 0x2041,
	.ReleaseNumber          = 0x0000,

	.ManufacturerStrIndex   = 0x01,
	.ProductStrIndex        = 0x02,
	.SerialNumStrIndex      = NO_DESCRIPTOR,

	.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};

/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
 *  of the device in one of its supported configurations, including information about any device interfaces
 *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
 *  a configuration so that the host may correctly communicate with the USB device.
 */
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
	.Config =
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},

			.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
			.TotalInterfaces        = 1,

			.ConfigurationNumber    = 1,
			.ConfigurationStrIndex  = NO_DESCRIPTOR,

			.ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),

			.MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
		},

	.HID_Interface =
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

			.InterfaceNumber        = 0x00,
			.AlternateSetting       = 0x00,

			.TotalEndpoints         = 1,

			.Class                  = HID_CSCP_HIDClass,
			.SubClass               = HID_CSCP_BootSubclass,
			.Protocol               = HID_CSCP_MouseBootProtocol,

			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

	.HID_MouseHID =
		{
			.Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},

			.HIDSpec                = VERSION_BCD(01.11),
			.CountryCode            = 0x00,
			.TotalReportDescriptors = 1,
			.HIDReportType          = HID_DTYPE_Report,
			.HIDReportLength        = sizeof(MouseReport)
		},

	.HID_ReportINEndpoint =
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},

			.EndpointAddress        = MOUSE_EPADDR,
			.Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
			.EndpointSize           = MOUSE_EPSIZE,
			.PollingIntervalMS      = 0x05
		}
};

/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
 *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
 *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
 */
const USB_Descriptor_String_t PROGMEM LanguageString =
{
	.Header                 = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},

	.UnicodeString          = {LANGUAGE_ID_ENG}
};

/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
 *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
const USB_Descriptor_String_t PROGMEM ManufacturerString =
{
	.Header                 = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},

	.UnicodeString          = L"Dean Camera"
};

/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
 *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
const USB_Descriptor_String_t PROGMEM ProductString =
{
	.Header                 = {.Size = USB_STRING_LEN(15), .Type = DTYPE_String},

	.UnicodeString          = L"LUFA Mouse Demo"
};

/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
 *  documentation) by the application code so that the address and size of a requested descriptor can be given
 *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
 *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
 *  USB host.
 */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
                                    const uint8_t wIndex,
                                    const void** const DescriptorAddress)
{
	const uint8_t  DescriptorType   = (wValue >> 8);
	const uint8_t  DescriptorNumber = (wValue & 0xFF);

	const void* Address = NULL;
	uint16_t    Size    = NO_DESCRIPTOR;

	switch (DescriptorType)
	{
		case DTYPE_Device:
			Address = &DeviceDescriptor;
			Size    = sizeof(USB_Descriptor_Device_t);
			break;
		case DTYPE_Configuration:
			Address = &ConfigurationDescriptor;
			Size    = sizeof(USB_Descriptor_Configuration_t);
			break;
		case DTYPE_String:
			switch (DescriptorNumber)
			{
				case 0x00:
					Address = &LanguageString;
					Size    = pgm_read_byte(&LanguageString.Header.Size);
					break;
				case 0x01:
					Address = &ManufacturerString;
					Size    = pgm_read_byte(&ManufacturerString.Header.Size);
					break;
				case 0x02:
					Address = &ProductString;
					Size    = pgm_read_byte(&ProductString.Header.Size);
					break;
			}

			break;
		case HID_DTYPE_HID:
			Address = &ConfigurationDescriptor.HID_MouseHID;
			Size    = sizeof(USB_HID_Descriptor_HID_t);
			break;
		case HID_DTYPE_Report:
			Address = &MouseReport;
			Size    = sizeof(MouseReport);
			break;
	}

	*DescriptorAddress = Address;
	return Size;
}
an class="p">, buf); } /** * @brief Receives a single byte response. * * @param[in] mmcp pointer to the @p MMCDriver object * @return The response as an @p uint8_t value. * @retval 0xFF timed out. * * @notapi */ static uint8_t recvr1(MMCDriver *mmcp) { int i; uint8_t r1[1]; for (i = 0; i < 9; i++) { spiReceive(mmcp->spip, 1, r1); if (r1[0] != 0xFF) return r1[0]; } return 0xFF; } /** * @brief Sends a command an returns a single byte response. * * @param[in] mmcp pointer to the @p MMCDriver object * @param cmd[in] the command id * @param arg[in] the command argument * @return The response as an @p uint8_t value. * @retval 0xFF timed out. * * @notapi */ static uint8_t send_command(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) { uint8_t r1; spiSelect(mmcp->spip); send_hdr(mmcp, cmd, arg); r1 = recvr1(mmcp); spiUnselect(mmcp->spip); return r1; } /** * @brief Waits that the card reaches an idle state. * * @param[in] mmcp pointer to the @p MMCDriver object * * @notapi */ static void sync(MMCDriver *mmcp) { uint8_t buf[1]; spiSelect(mmcp->spip); while (TRUE) { spiReceive(mmcp->spip, 1, buf); if (buf[0] == 0xFF) break; #ifdef MMC_NICE_WAITING chThdSleep(1); /* Trying to be nice with the other threads.*/ #endif } spiUnselect(mmcp->spip); } /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ /** * @brief MMC over SPI driver initialization. * @note This function is implicitly invoked by @p halInit(), there is * no need to explicitly initialize the driver. * * @init */ void mmcInit(void) { } /** * @brief Initializes an instance. * * @param[out] mmcp pointer to the @p MMCDriver object * @param[in] spip pointer to the SPI driver to be used as interface * @param[in] lscfg low speed configuration for the SPI driver * @param[in] hscfg high speed configuration for the SPI driver * @param[in] is_protected function that returns the card write protection * setting * @param[in] is_inserted function that returns the card insertion sensor * status * * @init */ void mmcObjectInit(MMCDriver *mmcp, SPIDriver *spip, const SPIConfig *lscfg, const SPIConfig *hscfg, mmcquery_t is_protected, mmcquery_t is_inserted) { mmcp->state = MMC_STOP; mmcp->config = NULL; mmcp->spip = spip; mmcp->lscfg = lscfg; mmcp->hscfg = hscfg; mmcp->is_protected = is_protected; mmcp->is_inserted = is_inserted; chEvtInit(&mmcp->inserted_event); chEvtInit(&mmcp->removed_event); } /** * @brief Configures and activates the MMC peripheral. * * @param[in] mmcp pointer to the @p MMCDriver object * @param[in] config pointer to the @p MMCConfig object. Must be @p NULL. * * @api */ void mmcStart(MMCDriver *mmcp, const MMCConfig *config) { chDbgCheck((mmcp != NULL) && (config == NULL), "mmcStart"); chSysLock(); chDbgAssert(mmcp->state == MMC_STOP, "mmcStart(), #1", "invalid state"); mmcp->config = config; mmcp->state = MMC_WAIT; mmcp->cnt = MMC_POLLING_INTERVAL; chVTSetI(&mmcp->vt, MS2ST(MMC_POLLING_DELAY), tmrfunc, mmcp); chSysUnlock(); } /** * @brief Disables the MMC peripheral. * * @param[in] mmcp pointer to the @p MMCDriver object * * @api */ void mmcStop(MMCDriver *mmcp) { chDbgCheck(mmcp != NULL, "mmcStop"); chSysLock(); chDbgAssert((mmcp->state != MMC_UNINIT) && (mmcp->state != MMC_READING) && (mmcp->state != MMC_WRITING), "mmcStop(), #1", "invalid state"); if (mmcp->state != MMC_STOP) { mmcp->state = MMC_STOP; chVTResetI(&mmcp->vt); } chSysUnlock(); spiStop(mmcp->spip); } /** * @brief Performs the initialization procedure on the inserted card. * @details This function should be invoked when a card is inserted and * brings the driver in the @p MMC_READY state where it is possible * to perform read and write operations. * @note It is possible to invoke this function from the insertion event * handler. * * @param[in] mmcp pointer to the @p MMCDriver object * @return The operation status. * @retval FALSE the operation succeeded and the driver is now * in the @p MMC_READY state. * @retval TRUE the operation failed. * * @api */ bool_t mmcConnect(MMCDriver *mmcp) { unsigned i; bool_t result; chDbgCheck(mmcp != NULL, "mmcConnect"); chDbgAssert((mmcp->state != MMC_UNINIT) && (mmcp->state != MMC_STOP), "mmcConnect(), #1", "invalid state"); if (mmcp->state == MMC_INSERTED) { /* Slow clock mode and 128 clock pulses.*/ spiStart(mmcp->spip, mmcp->lscfg); spiIgnore(mmcp->spip, 16); /* SPI mode selection.*/ i = 0; while (TRUE) { if (send_command(mmcp, MMC_CMDGOIDLE, 0) == 0x01) break; if (++i >= MMC_CMD0_RETRY) return TRUE; chThdSleepMilliseconds(10); } /* Initialization. */ i = 0; while (TRUE) { uint8_t b = send_command(mmcp, MMC_CMDINIT, 0); if (b == 0x00) break; if (b != 0x01) return TRUE; if (++i >= MMC_CMD1_RETRY) return TRUE; chThdSleepMilliseconds(10); } /* Initialization complete, full speed. */ spiStart(mmcp->spip, mmcp->hscfg); /* Setting block size.*/ if (send_command(mmcp, MMC_CMDSETBLOCKLEN, MMC_SECTOR_SIZE) != 0x00) return TRUE; /* Transition to MMC_READY state (if not extracted).*/ chSysLock(); if (mmcp->state == MMC_INSERTED) { mmcp->state = MMC_READY; result = FALSE; } else result = TRUE; chSysUnlock(); return result; } if (mmcp->state == MMC_READY) return FALSE; /* Any other state is invalid.*/ return TRUE; } /** * @brief Brings the driver in a state safe for card removal. * * @param[in] mmcp pointer to the @p MMCDriver object * @return The operation status. * @retval FALSE the operation succeeded and the driver is now * in the @p MMC_INSERTED state. * @retval TRUE the operation failed. * * @api */ bool_t mmcDisconnect(MMCDriver *mmcp) { bool_t status; chDbgCheck(mmcp != NULL, "mmcDisconnect"); chDbgAssert((mmcp->state != MMC_UNINIT) && (mmcp->state != MMC_STOP), "mmcDisconnect(), #1", "invalid state"); switch (mmcp->state) { case MMC_READY: /* Wait for the pending write operations to complete.*/ sync(mmcp); chSysLock(); if (mmcp->state == MMC_READY) mmcp->state = MMC_INSERTED; chSysUnlock(); case MMC_INSERTED: status = FALSE; default: status = TRUE; } spiStop(mmcp->spip); return status; } /** * @brief Starts a sequential read. * * @param[in] mmcp pointer to the @p MMCDriver object * @param[in] startblk first block to read * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk) { chDbgCheck(mmcp != NULL, "mmcStartSequentialRead"); chSysLock(); if (mmcp->state != MMC_READY) { chSysUnlock(); return TRUE; } mmcp->state = MMC_READING; chSysUnlock(); spiStart(mmcp->spip, mmcp->hscfg); spiSelect(mmcp->spip); send_hdr(mmcp, MMC_CMDREADMULTIPLE, startblk * MMC_SECTOR_SIZE); if (recvr1(mmcp) != 0x00) { spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_READING) mmcp->state = MMC_READY; chSysUnlock(); return TRUE; } return FALSE; } /** * @brief Reads a block within a sequential read operation. * * @param[in] mmcp pointer to the @p MMCDriver object * @param[out] buffer pointer to the read buffer * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer) { int i; chDbgCheck((mmcp != NULL) && (buffer != NULL), "mmcSequentialRead"); chSysLock(); if (mmcp->state != MMC_READING) { chSysUnlock(); return TRUE; } chSysUnlock(); for (i = 0; i < MMC_WAIT_DATA; i++) { spiReceive(mmcp->spip, 1, buffer); if (buffer[0] == 0xFE) { spiReceive(mmcp->spip, MMC_SECTOR_SIZE, buffer); /* CRC ignored. */ spiIgnore(mmcp->spip, 2); return FALSE; } } /* Timeout.*/ spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_READING) mmcp->state = MMC_READY; chSysUnlock(); return TRUE; } /** * @brief Stops a sequential read gracefully. * * @param[in] mmcp pointer to the @p MMCDriver object * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcStopSequentialRead(MMCDriver *mmcp) { static const uint8_t stopcmd[] = {0x40 | MMC_CMDSTOP, 0, 0, 0, 0, 1, 0xFF}; bool_t result; chDbgCheck(mmcp != NULL, "mmcStopSequentialRead"); chSysLock(); if (mmcp->state != MMC_READING) { chSysUnlock(); return TRUE; } chSysUnlock(); spiSend(mmcp->spip, sizeof(stopcmd), stopcmd); /* result = recvr1(mmcp) != 0x00;*/ /* Note, ignored r1 response, it can be not zero, unknown issue.*/ recvr1(mmcp); result = FALSE; spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_READING) mmcp->state = MMC_READY; chSysUnlock(); return result; } /** * @brief Starts a sequential write. * * @param[in] mmcp pointer to the @p MMCDriver object * @param[in] startblk first block to write * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk) { chDbgCheck(mmcp != NULL, "mmcStartSequentialWrite"); chSysLock(); if (mmcp->state != MMC_READY) { chSysUnlock(); return TRUE; } mmcp->state = MMC_WRITING; chSysUnlock(); spiStart(mmcp->spip, mmcp->hscfg); spiSelect(mmcp->spip); send_hdr(mmcp, MMC_CMDWRITEMULTIPLE, startblk * MMC_SECTOR_SIZE); if (recvr1(mmcp) != 0x00) { spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_WRITING) mmcp->state = MMC_READY; chSysUnlock(); return TRUE; } return FALSE; } /** * @brief Writes a block within a sequential write operation. * * @param[in] mmcp pointer to the @p MMCDriver object * @param[out] buffer pointer to the write buffer * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer) { static const uint8_t start[] = {0xFF, 0xFC}; uint8_t b[1]; chDbgCheck((mmcp != NULL) && (buffer != NULL), "mmcSequentialWrite"); chSysLock(); if (mmcp->state != MMC_WRITING) { chSysUnlock(); return TRUE; } chSysUnlock(); spiSend(mmcp->spip, sizeof(start), start); /* Data prologue. */ spiSend(mmcp->spip, MMC_SECTOR_SIZE, buffer); /* Data. */ spiIgnore(mmcp->spip, 2); /* CRC ignored. */ spiReceive(mmcp->spip, 1, b); if ((b[0] & 0x1F) == 0x05) { wait(mmcp); return FALSE; } /* Error.*/ spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_WRITING) mmcp->state = MMC_READY; chSysUnlock(); return TRUE; } /** * @brief Stops a sequential write gracefully. * * @param[in] mmcp pointer to the @p MMCDriver object * @return The operation status. * @retval FALSE the operation succeeded. * @retval TRUE the operation failed. * * @api */ bool_t mmcStopSequentialWrite(MMCDriver *mmcp) { static const uint8_t stop[] = {0xFD, 0xFF}; chDbgCheck(mmcp != NULL, "mmcStopSequentialWrite"); chSysLock(); if (mmcp->state != MMC_WRITING) { chSysUnlock(); return TRUE; } chSysUnlock(); spiSend(mmcp->spip, sizeof(stop), stop); spiUnselect(mmcp->spip); chSysLock(); if (mmcp->state == MMC_WRITING) { mmcp->state = MMC_READY; chSysUnlock(); return FALSE; } chSysUnlock(); return TRUE; } #endif /* HAL_USE_MMC_SPI */ /** @} */