Skip to content

Commit 84f0be4

Browse files
committed
Changed serial number to hex string of internal microcontroller UUID
1 parent c11c2db commit 84f0be4

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

inc/astrokey.h

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,47 @@
1414

1515
#include <SI_EFM8UB1_Defs.h>
1616
#include <stdint.h>
17+
#include <efm8_usb.h>
1718

18-
// Astrokey USB protocol
19+
//////////////////////////
20+
// Device Serial Number //
21+
//////////////////////////
22+
23+
// Converts a nibble to a hex character
24+
#define NIBBLE_TO_ASCII(x) ((x) >= 10? (x) - 10 + 'A' : (x) + '0')
25+
26+
// Struct for non-const string descriptors
27+
#define UTF16LE_PACKED_STRING_DESC(__name, __size) \
28+
SI_SEGMENT_VARIABLE(__name, USB_StringDescriptor_TypeDef, SI_SEG_XDATA) = \
29+
{ USB_STRING_DESCRIPTOR_UTF16LE_PACKED, __size * 2, USB_STRING_DESCRIPTOR }
30+
31+
// Serial number string descriptor
32+
extern SI_SEGMENT_VARIABLE(serDesc[], USB_StringDescriptor_TypeDef, SI_SEG_XDATA);
33+
34+
// MCU UUID Flash Address
35+
#define UUID_ADDR 0xFFC0
36+
37+
// MCU UUID Length in Bytes
38+
#define UUID_LEN 16
39+
40+
// Length of serial number string (2 characters per byte)
41+
#define SER_STR_LEN (UUID_LEN * 2)
42+
43+
// MCU UUID
44+
SI_VARIABLE_SEGMENT_POINTER(UUID, static const uint8_t, SI_SEG_CODE) = UUID_ADDR;
45+
46+
///////////////////////////
47+
// Astrokey USB protocol //
48+
///////////////////////////
1949

2050
// wIndex values
2151
#define ASTROKEY_SET_WORKFLOW 0x01
2252
#define ASTROKEY_GET_WORKFLOW 0x02
2353

54+
///////////////////////
55+
// Device Parameters //
56+
///////////////////////
57+
2458
// Switch configuration
2559
#define NUM_SWITCHES 5
2660
#define S0 P0_B0
@@ -32,10 +66,14 @@
3266
// Switch pressed
3367
#define PRESSED(x) (!x)
3468

35-
// No macro running
69+
////////////////////////
70+
// Workflow Constants //
71+
////////////////////////
72+
73+
// No workflow running
3674
#define NO_WORKFLOW 0xFF
3775

38-
// Macro action types
76+
// Workflow action types
3977
#define WORKFLOW_ACTION_DOWN 1
4078
#define WORKFLOW_ACTION_UP 2
4179
#define WORKFLOW_ACTION_PRESS 3
@@ -53,7 +91,7 @@
5391
#define MODIFIER_LEFTALT 0x04
5492
#define MODIFIER_LEFTGUI 0x08
5593

56-
// Macro action struct
94+
// Workflow action struct
5795
typedef struct {
5896
uint8_t actionType;
5997
uint8_t value;
@@ -74,15 +112,27 @@ typedef struct {
74112

75113
#define WORKFLOW_FLASH_ADDR USER_START_ADDR
76114

115+
////////////////////////
116+
// Workflow Functions //
117+
////////////////////////
118+
77119
void saveWorkflow(Action_TypeDef* workflowData, uint8_t saveIndex);
78120
void loadWorkflow(Action_TypeDef* workflowData, uint8_t loadIndex);
79121

122+
////////////////////////
123+
// Workflow Variables //
124+
////////////////////////
125+
80126
extern Action_TypeDef SI_SEG_XDATA workflow[WORKFLOW_MAX_SIZE];
81127
extern uint8_t workflowNumActions;
82128

83129
extern Action_TypeDef SI_SEG_XDATA tmpWorkflow[WORKFLOW_MAX_SIZE];
84130
extern volatile int8_t workflowUpdated;
85131

132+
////////////////////////
133+
// Astrokey Functions //
134+
////////////////////////
135+
86136
void astrokeyInit();
87137
void astrokeyPoll();
88138

src/astrokey.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ uint8_t workflowIndex = NO_WORKFLOW;
4444
// Index of current action in current workflow running;
4545
uint8_t actionIndices[NUM_SWITCHES] = {0};
4646

47+
// The UUID String descriptor
48+
UTF16LE_PACKED_STRING_DESC(serDesc[SER_STR_LEN + USB_STRING_DESCRIPTOR_NAME], SER_STR_LEN);
49+
4750
// Checks if a key is currently pressed by the workflow
4851
// Returns the index of the key in array of keys currently pressed,
4952
// -1 if the key is not currently being pressed
@@ -242,6 +245,16 @@ uint8_t checkKeyReleased(uint8_t bitMask, uint8_t pressed)
242245

243246
void astrokeyInit()
244247
{
248+
uint8_t i;
249+
// Read chip UUID and write hex string to serial string descriptor
250+
for (i = 0; i < UUID_LEN; i++)
251+
{
252+
serDesc[USB_STRING_DESCRIPTOR_NAME + 2 * i + 0] =
253+
NIBBLE_TO_ASCII((UUID[i] >> 8) & 0x0F);
254+
serDesc[USB_STRING_DESCRIPTOR_NAME + 2 * i + 1] =
255+
NIBBLE_TO_ASCII((UUID[i] >> 0) & 0x0F);
256+
}
257+
// Enter default device configuration
245258
enter_DefaultMode_from_RESET();
246259
}
247260

src/descriptors.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <stdlib.h>
1414
#include <string.h>
1515
#include <efm8_usb.h>
16+
#include "astrokey.h"
1617
#include "descriptors.h"
1718

1819

@@ -242,17 +243,14 @@ SI_SEGMENT_VARIABLE(msDesc, const MS_OS_20_DescriptorSet_TypeDef, SI_SEG_CODE) =
242243
#define MFR_SIZE 9
243244
#define PROD_STRING 'A','s','t','r','o','K','e','y','\0'
244245
#define PROD_SIZE 9
245-
#define SER_STRING '0','1','2','3','4','5','6','7','8','A','B','C','D','E','F','\0'
246-
#define SER_SIZE 16
247246

248247

249248
LANGID_STATIC_CONST_STRING_DESC( langDesc[], LANG_STRING);
250249
UTF16LE_PACKED_STATIC_CONST_STRING_DESC( mfrDesc[], MFR_STRING, MFR_SIZE );
251250
UTF16LE_PACKED_STATIC_CONST_STRING_DESC( prodDesc[], PROD_STRING, PROD_SIZE );
252-
UTF16LE_PACKED_STATIC_CONST_STRING_DESC( serDesc[], SER_STRING, SER_SIZE );
253251

254252
// USB String Table
255-
SI_SEGMENT_VARIABLE_SEGMENT_POINTER(myUsbStringTable_USEnglish[], static const USB_StringDescriptor_TypeDef, SI_SEG_GENERIC, const SI_SEG_CODE) =
253+
SI_SEGMENT_VARIABLE_SEGMENT_POINTER(myUsbStringTable_USEnglish[], static const USB_StringDescriptor_TypeDef, SI_SEG_GENERIC, const SI_SEG_CODE) =
256254
{
257255
(SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_CODE))langDesc,
258256
mfrDesc,

0 commit comments

Comments
 (0)