Przeglądaj źródła

CMSIS-DAP: Update examples (derive serial number from device unique ID)

David Lesnjak 4 lat temu
rodzic
commit
a3703595e3

+ 20 - 18
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/CMSIS_DAP.uvoptx

@@ -124,16 +124,6 @@
         </SetRegEntry>
       </TargetDriverDllRegistry>
       <Breakpoint/>
-      <ScvdPack>
-        <Filename>C:\ARM\PACK\ARM\CMSIS\5.7.0\CMSIS\RTOS2\RTX\RTX5.scvd</Filename>
-        <Type>ARM.CMSIS.5.7.0</Type>
-        <SubType>1</SubType>
-      </ScvdPack>
-      <ScvdPack>
-        <Filename>C:\ARM\PACK\Keil\MDK-Middleware\7.12.0\USB\USB.scvd</Filename>
-        <Type>Keil.MDK-Middleware.7.12.0</Type>
-        <SubType>1</SubType>
-      </ScvdPack>
       <Tracepoint>
         <THDelay>0</THDelay>
       </Tracepoint>
@@ -376,6 +366,18 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
+      <PathWithFileName>.\ser_num.c</PathWithFileName>
+      <FilenameWithoutPath>ser_num.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>1</GroupNumber>
+      <FileNumber>3</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
       <PathWithFileName>.\USBD_User_CustomClass_0.c</PathWithFileName>
       <FilenameWithoutPath>USBD_User_CustomClass_0.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
@@ -383,7 +385,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>3</FileNumber>
+      <FileNumber>4</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -403,7 +405,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>4</FileNumber>
+      <FileNumber>5</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -423,7 +425,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>5</FileNumber>
+      <FileNumber>6</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -435,7 +437,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>6</FileNumber>
+      <FileNumber>7</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -447,7 +449,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>7</FileNumber>
+      <FileNumber>8</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -459,7 +461,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>8</FileNumber>
+      <FileNumber>9</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -471,7 +473,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>9</FileNumber>
+      <FileNumber>10</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -483,7 +485,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>10</FileNumber>
+      <FileNumber>11</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 61 - 0
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/CMSIS_DAP.uvprojx

@@ -388,6 +388,62 @@
               <FileType>1</FileType>
               <FilePath>.\main.c</FilePath>
             </File>
+            <File>
+              <FileName>ser_num.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>.\ser_num.c</FilePath>
+              <FileOption>
+                <CommonProperty>
+                  <UseCPPCompiler>2</UseCPPCompiler>
+                  <RVCTCodeConst>0</RVCTCodeConst>
+                  <RVCTZI>0</RVCTZI>
+                  <RVCTOtherData>0</RVCTOtherData>
+                  <ModuleSelection>0</ModuleSelection>
+                  <IncludeInBuild>0</IncludeInBuild>
+                  <AlwaysBuild>0</AlwaysBuild>
+                  <GenerateAssemblyFile>0</GenerateAssemblyFile>
+                  <AssembleAssemblyFile>0</AssembleAssemblyFile>
+                  <PublicsOnly>2</PublicsOnly>
+                  <StopOnExitCode>11</StopOnExitCode>
+                  <CustomArgument></CustomArgument>
+                  <IncludeLibraryModules></IncludeLibraryModules>
+                  <ComprImg>1</ComprImg>
+                </CommonProperty>
+                <FileArmAds>
+                  <Cads>
+                    <interw>2</interw>
+                    <Optim>0</Optim>
+                    <oTime>2</oTime>
+                    <SplitLS>2</SplitLS>
+                    <OneElfS>2</OneElfS>
+                    <Strict>2</Strict>
+                    <EnumInt>2</EnumInt>
+                    <PlainCh>2</PlainCh>
+                    <Ropi>2</Ropi>
+                    <Rwpi>2</Rwpi>
+                    <wLevel>0</wLevel>
+                    <uThumb>2</uThumb>
+                    <uSurpInc>2</uSurpInc>
+                    <uC99>2</uC99>
+                    <uGnu>2</uGnu>
+                    <useXO>2</useXO>
+                    <v6Lang>0</v6Lang>
+                    <v6LangP>0</v6LangP>
+                    <vShortEn>2</vShortEn>
+                    <vShortWch>2</vShortWch>
+                    <v6Lto>2</v6Lto>
+                    <v6WtE>2</v6WtE>
+                    <v6Rtti>2</v6Rtti>
+                    <VariousControls>
+                      <MiscControls></MiscControls>
+                      <Define></Define>
+                      <Undefine></Undefine>
+                      <IncludePath></IncludePath>
+                    </VariousControls>
+                  </Cads>
+                </FileArmAds>
+              </FileOption>
+            </File>
             <File>
               <FileName>USBD_User_CustomClass_0.c</FileName>
               <FileType>1</FileType>
@@ -841,6 +897,11 @@
               <FileType>1</FileType>
               <FilePath>.\main.c</FilePath>
             </File>
+            <File>
+              <FileName>ser_num.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>.\ser_num.c</FilePath>
+            </File>
             <File>
               <FileName>USBD_User_CustomClass_0.c</FileName>
               <FileType>1</FileType>

+ 18 - 1
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/DAP_config.h

@@ -51,6 +51,11 @@ This information includes:
 #include "device.h"                             // Debug Unit Cortex-M Processor Header File
 #endif
 
+#ifdef LPC_LINK2_ONBOARD
+#include <string.h>
+#include "ser_num.h"
+#endif
+
 /// Processor Clock of the Cortex-M MCU used in the Debug Unit.
 /// This value is used to calculate the SWD/JTAG clock speed.
 #define CPU_CLOCK               180000000U      ///< Specifies the CPU Clock in Hz.
@@ -170,8 +175,20 @@ __STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
 \return String length.
 */
 __STATIC_INLINE uint8_t DAP_GetSerNumString (char *str) {
+  uint8_t len = 0U;
+
+#ifdef LPC_LINK2_ONBOARD
+  char *ser_num;
+
+  ser_num = GetSerialNum();
+  if (ser_num != NULL) {
+    strcpy(str, ser_num);
+    len = strlen(str);
+  }
+#else
   (void)str;
-  return (0U);
+#endif
+  return (len);
 }
 
 ///@}

+ 7 - 0
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/main.c

@@ -38,6 +38,13 @@ __NO_RETURN void app_main (void *argument) {
   DAP_Setup();                          // DAP Setup 
 
   USBD_Initialize(0U);                  // USB Device Initialization
+#ifdef LPC_LINK2_ONBOARD
+  char *ser_num;
+  ser_num = GetSerialNum();
+  if (ser_num != NULL) {
+    USBD_SetSerialNumber(0U, ser_num);  // Update Serial Number
+  }
+#endif
   USBD_Connect(0U);                     // USB Device Connect
 
   while (!USBD_Configured(0U));         // Wait for USB Device to configure

+ 93 - 0
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/ser_num.c

@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2021 ARM Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ----------------------------------------------------------------------
+ *
+ * $Date:        27. May 2021
+ * $Revision:    V1.0.0
+ *
+ * Project:      CMSIS-DAP Examples LPC-Link2
+ * Title:        ser_num.c CMSIS-DAP Serial Number module for LPC-Link2
+ *
+ *---------------------------------------------------------------------------*/
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "ser_num.h"
+
+// Serial Number
+#define SER_NUM_PREFIX  "00A1"
+static char SerialNum[32];
+
+#define IAP_LOCATION *(volatile unsigned int *)(0x10400100)
+#define IAP_READ_DEVICE_SERIAL_NUMBER  58U
+typedef void (*IAP)(unsigned int [],unsigned int[]);
+
+/**
+  \brief        Calculate 32-bit CRC (polynom: 0x04C11DB7, init value: 0xFFFFFFFF)
+  \param[in]    data  pointer to data
+  \param[in]    len   data length (in bytes)
+  \return             CRC32 value
+*/
+static uint32_t crc32 (const uint8_t *data, uint32_t len) {
+  uint32_t crc32;
+  uint32_t n;
+
+  crc32 = 0xFFFFFFFFU;
+  while (len != 0U) {
+    crc32 ^= ((uint32_t)*data++) << 24U;
+    for (n = 8U; n; n--) {
+      if (crc32 & 0x80000000U) {
+        crc32 <<= 1U;
+        crc32  ^= 0x04C11DB7U;
+      } else {
+        crc32 <<= 1U;
+      }
+    }
+    len--;
+  }
+  return (crc32);
+}
+
+/**
+  \brief        Get serial number string. First characters are fixed. Last eight
+                characters are Unique (calculated from devices's unique ID)
+  \return       Serial number string or NULL (callculation of unique ID failed)
+*/
+char *GetSerialNum (void) {
+  uint32_t command_param[5];
+  uint32_t status_result[5];
+  uint32_t uid;
+  char *str;
+  IAP   iap_entry;
+
+  memset(command_param, 0, sizeof(command_param));
+  memset(status_result, 0, sizeof(status_result));
+  iap_entry = (IAP)IAP_LOCATION;
+  command_param[0] = IAP_READ_DEVICE_SERIAL_NUMBER;
+  iap_entry(command_param, status_result);
+  str = NULL;
+  if (status_result[0] == 0U) {
+    uid = crc32 ((uint8_t *)&status_result[1], 16U);
+    snprintf(SerialNum, sizeof(SerialNum), "%s%08X", SER_NUM_PREFIX, uid);
+    str = SerialNum;
+  }
+
+  return (str);
+}

+ 33 - 0
CMSIS/DAP/Firmware/Examples/LPC-Link2/V2/ser_num.h

@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2021 ARM Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ----------------------------------------------------------------------
+ *
+ * $Date:        27. May 2021
+ * $Revision:    V1.0.0
+ *
+ * Project:      CMSIS-DAP Examples LPC-Link2
+ * Title:        ser_num.h CMSIS-DAP Serial Number module for LPC-Link2
+ *
+ *---------------------------------------------------------------------------*/
+
+#ifndef __SER_NUM_H__
+#define __SER_NUM_H__
+
+char *GetSerialNum (void);
+
+#endif /* __SER_NUM_H__ */