forked from ECE198ProjectTracker/SecureEdu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFINAL_ENCODER.c
647 lines (557 loc) · 18.4 KB
/
FINAL_ENCODER.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Text Selection System with LCD Display
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "liquidcrystal_i2c.h"
/* Constants */
#define MAX_PARAGRAPHS 3
#define MAX_SENTENCES 10
#define AES_BLOCK_SIZE 16
#define KEY_SIZE 16
#define ACCESS_KEY_SIZE 8
#define MAX_TEXT_SIZE 10240 // Maximum size for selected text
#define TX_BUFFER_SIZE 1024 // Transmission buffer size
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2; // Keep for debug output
UART_HandleTypeDef huart1; // Add for transmission to decoder
static uint8_t text_buffer[MAX_TEXT_SIZE];
static uint8_t encrypted_buffer[MAX_TEXT_SIZE];
static uint8_t tx_buffer[TX_BUFFER_SIZE];
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
void Error_Handler(void);
static void MX_USART1_UART_Init(void);
/* Encryption structures and variables */
typedef struct {
uint8_t key[KEY_SIZE];
uint8_t access_key[ACCESS_KEY_SIZE + 1];
uint32_t data_size;
uint32_t timestamp;
} EncryptionInfo;
static EncryptionInfo encInfo = {0};
/* Text Content */
const char* PARAGRAPHS[MAX_PARAGRAPHS][MAX_SENTENCES] = {
{
"What is the derivative of 2x^3 + 3x?",
"The derivative is 6x^2 + 3.",
NULL
},
{
"What is the moment of inertia of a rolling disk?",
"I = 1/2MR^2.",
NULL
},
{
"When the determinant does not equal to 0, is the matrix invertible?",
"The matrix is indeed invertible if the det(A) != 0.",
NULL
}
};
/* Selection State */
typedef struct {
int paragraph;
int line;
uint8_t isSet;
} TextPosition;
typedef enum {
INPUT_PARAGRAPH,
INPUT_LINE
} InputState;
TextPosition startPos = {0, 0, 0};
TextPosition endPos = {0, 0, 0};
InputState currentState = INPUT_PARAGRAPH;
TextPosition* currentPos = &startPos;
/* Helper Functions */
void updateLCDStatus(const char* line1, const char* line2) {
HD44780_Clear();
HD44780_SetCursor(0,0);
HD44780_PrintStr(line1);
if(line2) {
HD44780_SetCursor(0,1);
HD44780_PrintStr(line2);
}
}
int _write(int file, char *ptr, int len) {
// Fixed: Removed incorrect timestamp transmission
HAL_UART_Transmit(&huart2, (uint8_t*)ptr, len, HAL_MAX_DELAY);
return len;
}
void clearScreen(void) {
printf("\033[2J\033[H");
}
void printLegend(void) {
printf("\r\n=== Button Legend =============================\r\n");
printf("A - 0 | B - 1 | C - 2 | D - ENTER\r\n");
printf("=============================================\r\n");
}
int readRow(void) {
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) return 0;
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET) return 1;
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_SET) return 2;
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_SET) return 3;
return -1;
}
void printPrompt(const char* phase, const char* promptType) {
printf("\r\n%s", phase);
printf("\r\n%s:", promptType);
printf("\r\n_");
}
void updateInput(const char* promptType, int value) {
printf("\r\033[K");
printf("\r%d", value);
}
/* Encryption Functions */
void generateAccessKey(void) {
// Use only numbers and uppercase letters for clarity
const char charset[] = "123456AB";
srand(HAL_GetTick());
printf("Generating access key: ");
for(int i = 0; i < ACCESS_KEY_SIZE; i++) {
int index = rand() % strlen(charset);
encInfo.access_key[i] = charset[index];
printf("%c", encInfo.access_key[i]);
}
encInfo.access_key[ACCESS_KEY_SIZE] = '\0';
printf("\r\n");
}
void deriveKeyFromAccessKey(void) {
uint32_t timestamp = HAL_GetTick();
for(int i = 0; i < KEY_SIZE; i++) {
encInfo.key[i] = encInfo.access_key[i % ACCESS_KEY_SIZE] ^
((timestamp >> (i % 32)) & 0xFF) ^ 0x5A;
}
}
void encryptData(uint8_t* data, size_t length) {
uint8_t keyStream[KEY_SIZE];
memcpy(keyStream, encInfo.key, KEY_SIZE);
for(size_t i = 0; i < length; i++) {
if(i > 0 && (i % KEY_SIZE) == 0) {
for(int j = 0; j < KEY_SIZE; j++) {
keyStream[j] = keyStream[j] ^ encInfo.key[j] ^ (i & 0xFF);
}
}
data[i] ^= keyStream[i % KEY_SIZE];
}
}
void transmitWithBuffer(const uint8_t* data, size_t length) {
size_t bytes_sent = 0;
while (bytes_sent < length) {
size_t chunk_size = ((length - bytes_sent) > 16) ? 16 : (length - bytes_sent);
memcpy(tx_buffer, &data[bytes_sent], chunk_size);
HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, tx_buffer, chunk_size, HAL_MAX_DELAY);
if (status != HAL_OK) {
printf("Error transmitting chunk at byte %zu\r\n", bytes_sent);
return;
}
bytes_sent += chunk_size;
HAL_Delay(10); // Increased delay between chunks
}
}
// Fixed: Updated transmitEncryptedData function
void transmitEncryptedData(void) {
printf("\r\nStarting transmission...\r\n");
// Send start marker
uint8_t startMarker = 0xAA;
printf("Sending start marker (0xAA)...\r\n");
HAL_UART_Transmit(&huart1, &startMarker, 1, HAL_MAX_DELAY);
HAL_Delay(50);
// Print access key for debugging
printf("Access key before sending: ");
for(int i = 0; i < ACCESS_KEY_SIZE; i++) {
printf("%c", encInfo.access_key[i]);
}
printf("\r\n");
// Send access key with explicit length checking
printf("Sending access key...\r\n");
for(int i = 0; i < ACCESS_KEY_SIZE; i++) {
HAL_UART_Transmit(&huart1, &encInfo.access_key[i], 1, HAL_MAX_DELAY);
printf("Sent byte %d: '%c' (0x%02X)\r\n", i, encInfo.access_key[i], encInfo.access_key[i]);
HAL_Delay(5);
}
HAL_Delay(50);
// Send timestamp as 4 separate bytes
printf("Sending timestamp: %lu\r\n", (unsigned long)encInfo.timestamp);
uint32_t timestamp = encInfo.timestamp;
uint8_t timestamp_bytes[4] = {
(uint8_t)(timestamp & 0xFF),
(uint8_t)((timestamp >> 8) & 0xFF),
(uint8_t)((timestamp >> 16) & 0xFF),
(uint8_t)((timestamp >> 24) & 0xFF)
};
for(int i = 0; i < 4; i++) {
HAL_UART_Transmit(&huart1, ×tamp_bytes[i], 1, HAL_MAX_DELAY);
printf("Sent timestamp byte %d: 0x%02X\r\n", i, timestamp_bytes[i]);
HAL_Delay(5);
}
HAL_Delay(50);
// Send data size as 4 separate bytes
uint32_t size = encInfo.data_size;
uint8_t size_bytes[4] = {
(uint8_t)(size & 0xFF),
(uint8_t)((size >> 8) & 0xFF),
(uint8_t)((size >> 16) & 0xFF),
(uint8_t)((size >> 24) & 0xFF)
};
printf("Sending data size: %lu bytes\r\n", (unsigned long)encInfo.data_size);
for(int i = 0; i < 4; i++) {
HAL_UART_Transmit(&huart1, &size_bytes[i], 1, HAL_MAX_DELAY);
printf("Sent size byte %d: 0x%02X\r\n", i, size_bytes[i]);
HAL_Delay(5);
}
HAL_Delay(50);
// Send encrypted data in chunks
printf("Sending encrypted data...\r\n");
size_t sent = 0;
while (sent < encInfo.data_size) {
size_t chunk = (encInfo.data_size - sent > 16) ? 16 : encInfo.data_size - sent;
memcpy(tx_buffer, &encrypted_buffer[sent], chunk);
HAL_UART_Transmit(&huart1, tx_buffer, chunk, HAL_MAX_DELAY);
sent += chunk;
if (sent % 128 == 0 || sent == encInfo.data_size) {
printf("Sent %zu of %lu bytes\r\n", sent, (unsigned long)encInfo.data_size);
}
HAL_Delay(10);
}
HAL_Delay(50);
// Send end marker
uint8_t endMarker = 0x55;
printf("Sending end marker (0x55)...\r\n");
HAL_UART_Transmit(&huart1, &endMarker, 1, HAL_MAX_DELAY);
HAL_Delay(50);
printf("\r\nTransmission complete!\r\n");
}
void encryptSelectedText(void) {
size_t total_len = 0; // Declare this at the beginning
size_t padded_size; // Declare this at the beginning
// Copy selected text to buffer
for (int p = startPos.paragraph; p <= endPos.paragraph; p++) {
for (int l = 0; PARAGRAPHS[p][l] != NULL; l++) {
if ((p == startPos.paragraph && l >= startPos.line) ||
(p == endPos.paragraph && l <= endPos.line) ||
(p > startPos.paragraph && p < endPos.paragraph)) {
size_t line_len = strlen(PARAGRAPHS[p][l]);
if (total_len + line_len + 1 > MAX_TEXT_SIZE) {
printf("\r\nError: Selected text too large\r\n");
updateLCDStatus("Error:", "Text too large!");
return;
}
memcpy(&text_buffer[total_len], PARAGRAPHS[p][l], line_len);
total_len += line_len;
text_buffer[total_len++] = '\n';
}
}
}
// Calculate padded size
padded_size = ((total_len + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
// Now add the debug prints after we have the values
printf("\r\nPreparing text for encryption:\r\n");
printf("Total text length: %zu\r\n", total_len);
printf("Padded size: %zu\r\n", padded_size);
printf("First 32 bytes of text: ");
for(int i = 0; i < 32 && i < total_len; i++) {
printf("%02X ", text_buffer[i]);
}
printf("\r\n");
// Copy to encrypted buffer and pad
memcpy(encrypted_buffer, text_buffer, total_len);
memset(&encrypted_buffer[total_len], 0, padded_size - total_len);
encInfo.data_size = padded_size;
// Generate access key and encrypt
updateLCDStatus("Generating", "Access Key...");
generateAccessKey();
encInfo.timestamp = HAL_GetTick();
deriveKeyFromAccessKey();
updateLCDStatus("Encrypting...", "Please Wait");
encryptData(encrypted_buffer, padded_size);
// Transmit encrypted data
transmitEncryptedData();
char keyBuffer[16];
snprintf(keyBuffer, 16, "Key: %s", encInfo.access_key);
updateLCDStatus("Encrypted!", keyBuffer);
printf("\r\n=== Encryption Complete =========================\r\n");
printf("Access Key: %s\r\n", encInfo.access_key);
printf("Data Size: %lu bytes\r\n", (unsigned long)padded_size);
printf("Store this access key to decrypt the text!\r\n");
printf("===============================================\r\n\n");
}
void printSelectedText(void) {
clearScreen();
printLegend();
printf("\r\n=== Selected Text ===============================\r\n");
printf("From: P%d, L%d\r\n", startPos.paragraph, startPos.line);
printf("To: P%d, L%d\r\n", endPos.paragraph, endPos.line);
printf("=============================================\r\n\n");
char lcdBuffer[16];
snprintf(lcdBuffer, 16, "From P%d,L%d", startPos.paragraph, startPos.line);
updateLCDStatus(lcdBuffer, "Processing...");
if (endPos.paragraph < startPos.paragraph ||
(endPos.paragraph == startPos.paragraph && endPos.line < startPos.line)) {
printf("Invalid selection! End must be after start.\r\n");
updateLCDStatus("Error:", "Invalid Range!");
return;
}
for (int p = startPos.paragraph; p <= endPos.paragraph; p++) {
for (int l = 0; PARAGRAPHS[p][l] != NULL; l++) {
if ((p == startPos.paragraph && l >= startPos.line) ||
(p == endPos.paragraph && l <= endPos.line) ||
(p > startPos.paragraph && p < endPos.paragraph)) {
printf("%-75s\r\n", PARAGRAPHS[p][l]);
}
}
}
printf("\r\n");
encryptSelectedText();
}
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init(); // Keep for debug output
MX_USART1_UART_Init();
/* Initialize LCD */
HD44780_Init(2);
HD44780_Clear();
updateLCDStatus("Text Selection", "System Ready");
HAL_Delay(2000);
clearScreen();
printf("\r\n=== Text Selection System ========================");
printLegend();
printPrompt("START INDEX", "ENTER PARAGRAPH #");
updateLCDStatus("Start Index", "Enter Para #");
uint8_t inputReceived = 0;
uint8_t buttonReleased = 1;
while (1)
{
int row = readRow();
if (row != -1 && buttonReleased) {
buttonReleased = 0;
char lcdBuffer[16];
if (row >= 0 && row <= 2 && !inputReceived) {
switch (currentState) {
case INPUT_PARAGRAPH:
if (row < MAX_PARAGRAPHS) {
currentPos->paragraph = row;
updateInput("ENTER PARAGRAPH #", row);
snprintf(lcdBuffer, 16, "Para #%d", row);
updateLCDStatus(lcdBuffer, "Press Enter");
inputReceived = 1;
}
break;
case INPUT_LINE:
if (PARAGRAPHS[currentPos->paragraph][row] != NULL) {
currentPos->line = row;
updateInput("ENTER LINE #", row);
snprintf(lcdBuffer, 16, "Line #%d", row);
updateLCDStatus(lcdBuffer, "Press Enter");
inputReceived = 1;
}
break;
}
}
else if (row == 3 && inputReceived) {
switch (currentState) {
case INPUT_PARAGRAPH:
currentState = INPUT_LINE;
printPrompt("", "ENTER LINE #");
updateLCDStatus("Enter Line #", "");
inputReceived = 0;
break;
case INPUT_LINE:
currentPos->isSet = 1;
if (currentPos == &startPos) {
currentPos = &endPos;
currentState = INPUT_PARAGRAPH;
printPrompt("\nEND INDEX", "ENTER PARAGRAPH #");
updateLCDStatus("End Index", "Enter Para #");
} else {
printSelectedText();
}
inputReceived = 0;
break;
}
}
}
else if (row == -1) {
buttonReleased = 1;
}
HAL_Delay(10);
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* Configure GPIO pins for UART2 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; // PA2 is TX, PA3 is RX
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX; // TX only for encoder
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/* Configure I2C1 GPIO Configuration */
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Configure input pins */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Configure UART pins */
GPIO_InitStruct.Pin = GPIO_PIN_9; // PA9 is TX for UART1
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */