Skip to content

Commit

Permalink
使用config.h内的配置动态填充USB描述符;增加USB序列号功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-kirisame committed Jul 28, 2019
1 parent 6dff642 commit f4cbec8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
"endpoints.h": "c",
"custom_hook.h": "c",
"ble_keyboard.h": "c",
"led_rgb.h": "c"
"led_rgb.h": "c",
"ch554_sdcc.h": "c",
"compiler.h": "c"
},
"C_Cpp.default.includePath": [
"C:/Program Files (x86)/GNU Tools ARM Embedded/7 2018-q2-update/lib/gcc/arm-none-eabi/7.3.1/include",
Expand Down
81 changes: 71 additions & 10 deletions usb/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,44 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"
#include "usb_descriptor.h"
#include <stdint.h>
// #include <stdio.h>
#include <string.h>

const uint8_t itoa[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
static uint8_t __xdata descBuffer[64];

static uint8_t fillDescBuffer(char* str)
{
uint8_t len = strlen(str);

descBuffer[0] = len * 2 + 2;
descBuffer[1] = 0x03;

for (uint8_t i = 0; i < len; i++) {
descBuffer[(i + 1) * 2] = str[i];
descBuffer[(i + 1) * 2 + 1] = 0x00;
}
return len * 2 + 2;
}

static uint8_t getSerial()
{
uint8_t i = 1;
descBuffer[i++] = 0x03;

for (uint16_t addr = 0x3FFC; addr <= 0x3FFF; addr++) {
uint16_t se = (uint16_t)(*((const uint8_t __code*)(addr)));
descBuffer[i++] = itoa[(se >> 4) % 0xF];
descBuffer[i++] = 0x00;
descBuffer[i++] = itoa[(se >> 0) % 0xF];
descBuffer[i++] = 0x00;
}

descBuffer[0] = i;
return i;
}

/** \brief 获取文本描述符
*
Expand All @@ -29,16 +64,42 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
static uint8_t getStringDescriptor(uint8_t order, uint8_t** strPtor)
{
uint8_t header = 0, strlen = 0;
do {
header += strlen;
if (header >= sizeof(StringDescriptor)) // 超过长度就直接返回
{
return 0xFF;
}
strlen = StringDescriptor[header];
} while (order--);
switch (order) {
case 0:
*strPtor = (uint8_t*)&LangStringDesc[0];
strlen = LangStringDesc[0];
break;
case INTF_STRING_INDEX:
case INTF_STRING_INDEX + 1:
case INTF_STRING_INDEX + 2:
order -= INTF_STRING_INDEX;
do {
header += strlen;
if (header >= sizeof(InterfaceStringDesc)) // 超过长度就直接返回
{
return 0xFF;
}
strlen = InterfaceStringDesc[header];
} while (order--);
*strPtor = (uint8_t*)&InterfaceStringDesc[header];
break;
case 1:
strlen = fillDescBuffer(MANUFACTURER);
*strPtor = descBuffer;
break;
case 2:
strlen = fillDescBuffer(PRODUCT);
*strPtor = descBuffer;
break;
case 3:
strlen = getSerial();
*strPtor = descBuffer;
break;
default:
strlen = 0xFF;
break;
}

*strPtor = (uint8_t*)&StringDescriptor[header];
return strlen;
}

Expand Down
36 changes: 4 additions & 32 deletions usb/usb_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,42 +213,14 @@ uint8_t const ConfigDescriptor []=
/*-----------------------------------------------------------------------------+
| String Descriptor |
|-----------------------------------------------------------------------------*/
uint8_t const StringDescriptor[] = {
const uint8_t LangStringDesc[] = {
// String index0, language support

4, // Length of language descriptor ID
3, // LANGID tag
0x09, 0x04, // 0x0409 for English
};


// String index1, Manufacturer

14, // Length of this string descriptor
3, // bDescriptorType
'L',0x00,'o',0x00,'t',0x00,'l',0x00,'a',0x00,'b',0x00,

// String index2, Product

30, // Length of this string descriptor
3, // bDescriptorType
'n',0x00,'R',0x00,'F',0x00,'5',0x00,'2',0x00,' ',0x00,
'K',0x00,'e',0x00,'y',0x00,'b',0x00,'o',0x00,'a',0x00,
'r',0x00,'d',0x00,

// String index3, Serial Number

14, // Length of this string descriptor
3, // bDescriptorType
'1',0x00,'1',0x00,'4',0x00,'5',0x00,'1',0x00,'4',0x00,

// String index4, Configuration String

22, // Length of this string descriptor
3, // bDescriptorType
'M',0x00,'S',0x00,'P',0x00,'4',0x00,'3',0x00,'0',0x00,
' ',0x00,'U',0x00,'S',0x00,'B',0x00,


const uint8_t InterfaceStringDesc[] = {
// String index5, Interface String
26, // Length of this string descriptor
3, // bDescriptorType
Expand All @@ -262,7 +234,7 @@ uint8_t const StringDescriptor[] = {
'K',0x00,'e',0x00,'y',0x00,' ',0x00,'S',0x00,'u',0x00,
'p',0x00,'p',0x00,'o',0x00,'r',0x00,'t',0x00,

// String index6, Interface String
// String index7, Interface String
32, // Length of this string descriptor
3, // bDescriptorType
'K',0x00,'e',0x00,'y',0x00,'b',0x00,'o',0x00,'a',0x00,
Expand Down

0 comments on commit f4cbec8

Please sign in to comment.