1515
1616 By default, no locking is performed. User have to manually enable the flags that
1717 turn on the locking. Furthermore, user have to send a SPACE character on serial
18- console when prompted to do any locking.
19- Default settings use ATSHA204 on pin 7.
18+ console when prompted to do any locking. On boards that does not provide UART
19+ input it is possible to configure the sketch to skip this confirmation.
20+ Default settings use ATSHA204 on pin 17 (A3).
2021*/
2122#include < sha204_library.h>
2223#include < sha204_lib_return_codes.h>
2324
2425// The pin the ATSHA204 is connected on
25- #define ATSHA204_PIN 7
26+ #define ATSHA204_PIN 17 // A3
2627
2728// Uncomment this to enable locking the configuration zone.
2829// *** BE AWARE THAT THIS PREVENTS ANY FUTURE CONFIGURATION CHANGE TO THE CHIP ***
4445// Uncomment this to skip key generation and use 'user_key_data' as key instead.
4546// #define USER_KEY_DATA
4647
48+ // Uncomment this for boards that lack UART
49+ // IMPORTANT: No confirmation will be required for locking any zones with this
50+ // configuration!
51+ // Also, key generation is not permitted in this mode as there is no way of
52+ // presenting the generated key.
53+ // #define SKIP_UART_CONFIRMATION
54+
55+ #if defined(SKIP_UART_CONFIRMATION) && !defined(USER_KEY_DATA)
56+ #error You have to define USER_KEY_DATA for boards that does not have UART
57+ #endif
58+
4759#ifdef USER_KEY_DATA
48- const uint8_t user_key_data[32 ] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
49- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
50- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
51- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
60+ const uint8_t user_key_data[32 ] = {
61+ 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
62+ 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
63+ 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
64+ 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
65+ };
5266#endif
5367
5468const int sha204Pin = ATSHA204_PIN;
@@ -727,6 +741,7 @@ void setup()
727741
728742#ifdef LOCK_CONFIGURATION
729743 // Purge serial input buffer
744+ #ifndef SKIP_UART_CONFIRMATION
730745 while (Serial.available ())
731746 {
732747 Serial.read ();
@@ -735,6 +750,7 @@ void setup()
735750
736751 while (Serial.available () == 0 );
737752 if (Serial.read () == ' ' )
753+ #endif // not SKIP_UART_CONFIRMATION
738754 {
739755 Serial.println (F (" Locking configuration..." ));
740756
@@ -773,10 +789,12 @@ void setup()
773789 }
774790 }
775791 }
792+ #ifndef SKIP_UART_CONFIRMATION
776793 else
777794 {
778795 Serial.println (F (" Unexpected answer. Skipping lock." ));
779796 }
797+ #endif // not SKIP_UART_CONFIRMATION
780798#else // LOCK_CONFIGURATION
781799 Serial.println (F (" Dry-run. Configuration not locked. Define LOCK_CONFIGURATION to lock for real." ));
782800#endif
@@ -793,7 +811,7 @@ void setup()
793811#else
794812#ifdef USER_KEY_DATA
795813 memcpy (key, user_key_data, 32 );
796- Serial.print (F (" Using this user supplied key: " ));
814+ Serial.println (F (" Using this user supplied key:" ));
797815#else
798816 // Retrieve random value to use as key
799817 ret_code = sha204.sha204m_random (tx_buffer, rx_buffer, RANDOM_SEED_UPDATE);
@@ -817,11 +835,14 @@ void setup()
817835#endif
818836 for (int i=0 ; i<32 ; i++)
819837 {
838+ Serial.print (" 0x" );
820839 if (key[i] < 0x10 )
821840 {
822841 Serial.print (' 0' ); // Because Serial.print does not 0-pad HEX
823842 }
824843 Serial.print (key[i], HEX);
844+ if (i < 31 ) Serial.print (' ,' );
845+ if (!((i+1 ) % 8 )) Serial.println ();
825846 }
826847 Serial.println ();
827848
@@ -842,13 +863,15 @@ void setup()
842863 if (lockValue != 0x00 )
843864 {
844865#ifdef LOCK_DATA
866+ #ifndef SKIP_UART_CONFIRMATION
845867 while (Serial.available ())
846868 {
847869 Serial.read ();
848870 }
849871 Serial.println (F (" Send SPACE character to lock data..." ));
850872 while (Serial.available () == 0 );
851873 if (Serial.read () == ' ' )
874+ #endif // not SKIP_UART_CONFIRMATION
852875 {
853876 // Correct sequence, resync chip
854877 ret_code = sha204.sha204c_resync (SHA204_RSP_SIZE_MAX, rx_buffer);
@@ -907,10 +930,12 @@ void setup()
907930 }
908931 }
909932 }
933+ #ifndef SKIP_UART_CONFIRMATION
910934 else
911935 {
912936 Serial.println (F (" Unexpected answer. Skipping lock." ));
913937 }
938+ #endif // not SKIP_UART_CONFIRMATION
914939#else // LOCK_DATA
915940 Serial.println (F (" Dry-run. Data not locked. Define LOCK_DATA to lock for real." ));
916941#endif
0 commit comments