Changeset 77
- Timestamp:
- Sep 9, 2026, 3:57:36 PM (11 days ago)
- Location:
- trunk/fw_g473rct/Core
- Files:
-
- 2 edited
-
Inc/main.h (modified) (1 diff)
-
Src/main.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/fw_g473rct/Core/Inc/main.h
r76 r77 58 58 59 59 #define VREF 3000 60 61 #define BUZZER_ON GPIO_PIN_SET 62 #define BUZZER_OFF GPIO_PIN_RESET 63 64 #define BTN_PRESSED GPIO_PIN_RESET 65 #define BTN_NOT_PRESSED GPIO_PIN_SET 66 60 67 /* USER CODE END EC */ 61 68 -
trunk/fw_g473rct/Core/Src/main.c
r76 r77 122 122 void processUSB(ring_buffer_t* const ring_in_buff, ring_buffer_t* const ring_out_buff); 123 123 void ADC2_SetOffset(uint32_t channel, int16_t offset); 124 unsigned setSlaveAddr(void); 124 125 /* USER CODE END PFP */ 125 126 … … 218 219 HAL_IWDG_Refresh(&hiwdg); 219 220 220 if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) 221 // Set Slave Address via user button 222 unsigned slaveAddr = setSlaveAddr(); 223 if (slaveAddr) 224 { 225 sys_data.s.parameter.slave_address = slaveAddr; 226 EEPROM_storeConfig(&sys_data, 0); 227 } 228 229 /*if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) 221 230 { 222 231 HAL_Delay(50); … … 226 235 EEPROM_factoryRestore(&sys_data, 1); 227 236 } 228 } 237 }*/ 229 238 HAL_IWDG_Refresh(&hiwdg); 230 239 LoadBackupRegister(); … … 591 600 /* USER CODE BEGIN 4 */ 592 601 602 unsigned setSlaveAddr(void) 603 { 604 const unsigned MAX_PERCENT = 80U; 605 const unsigned SCAN_RATE_MS = 5U; 606 const unsigned MAX_SLAVE_ADDR = 16U; 607 608 uint16_t buff = 0U; 609 unsigned startTime = HAL_GetTick(); 610 while (HAL_GetTick() - startTime < 500U) 611 { 612 if (BTN_PRESSED == HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin)) 613 buff = (buff << 1U) | 1U; 614 else 615 buff = (buff << 1U); 616 HAL_Delay(15U); 617 } 618 619 unsigned ones = __builtin_popcount(buff); // 1's counter 620 if (ones < (sizeof(buff) * 8U * MAX_PERCENT) / 100U) // if we have 80% of 1 then we assume that a button was pressed for 1 s 621 { 622 #ifdef DEBUG 623 printf("Button was NOT kept pressed!\n"); 624 #endif 625 return 0; 626 } 627 628 #ifdef DEBUG 629 printf("Button was kept pressed for a enough time\n"); 630 #endif 631 632 // Buzzer signalize that we are now in Slave Address Setting mode 633 startTime = HAL_GetTick(); 634 while (HAL_GetTick() - startTime < 300U) 635 { 636 HAL_GPIO_TogglePin(BUZZER_GPIO_Port, BUZZER_Pin); 637 HAL_Delay(20U); 638 } 639 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_OFF); 640 641 // We need to wait till button release(10s) 642 startTime = HAL_GetTick(); 643 while (BTN_PRESSED == HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin)) 644 { 645 if (HAL_GetTick() - startTime >= 60U * 1000U) return 0; 646 HAL_Delay(10U); 647 } 648 649 typedef enum ButtonState 650 { 651 BTN_STATE_NOT_PRESSED, 652 BTN_STATE_PRESSED, 653 } ButtonState_t; 654 ButtonState_t btn_state = BTN_STATE_NOT_PRESSED; 655 656 unsigned slaveAddr = 0; 657 // Waiting for button press about 2min 658 startTime = HAL_GetTick(); 659 while (HAL_GetTick() - startTime < 2U * 60U * 1000U) 660 { 661 if (BTN_PRESSED == HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin)) 662 buff = (buff << 1U) | 1U; 663 else 664 buff = (buff << 1U); 665 666 HAL_Delay(SCAN_RATE_MS); 667 668 switch(btn_state) 669 { 670 case BTN_STATE_NOT_PRESSED: 671 if ((buff & UINT16_MAX) == UINT16_MAX) 672 { // Button down event is detected 673 btn_state = BTN_STATE_PRESSED; 674 buff = 0xABCD; 675 #ifdef DEBUG 676 printf("Key was pressed\n"); 677 #endif 678 } 679 else if (buff == 0) 680 { 681 #ifdef DEBUG 682 static unsigned tickStart = 0; 683 if (HAL_GetTick() - tickStart >= 1000U) 684 { 685 tickStart = HAL_GetTick(); 686 printf("Key is not pressed\n"); 687 } 688 #endif 689 } 690 break; 691 692 case BTN_STATE_PRESSED: 693 if ((buff & UINT16_MAX) == UINT16_MAX) 694 { // Button down event is detected 695 btn_state = BTN_STATE_PRESSED; 696 buff = 0xBCDE; 697 static unsigned pressCnt; 698 if (++pressCnt > 2000U / (SCAN_RATE_MS * sizeof(buff) * 8U))// 2s 699 { 700 pressCnt = 0U; 701 702 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_ON); 703 HAL_Delay(1000U); 704 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_OFF); 705 HAL_Delay(200U); 706 #ifdef DEBUG 707 printf("Key long press was detected\n"); 708 #endif 709 return slaveAddr; 710 } 711 #ifdef DEBUG 712 static unsigned tickStart = 0; 713 if (HAL_GetTick() - tickStart >= 250U) 714 { 715 tickStart = HAL_GetTick(); 716 printf("Key is kept pressed\n"); 717 } 718 #endif 719 } 720 else if (buff == 0) 721 { 722 btn_state = BTN_STATE_NOT_PRESSED; 723 ++slaveAddr; 724 if (slaveAddr > MAX_SLAVE_ADDR) 725 { // We allow a Slave address to be lower or equal to 16 726 for (int i = 0; i < 3; ++i) 727 { 728 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_ON); 729 HAL_Delay(400U); 730 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_OFF); 731 HAL_Delay(75U); 732 } 733 return 0; 734 } 735 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_ON); 736 HAL_Delay(50U); 737 HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, BUZZER_OFF); 738 #ifdef DEBUG 739 printf("Key was released\n"); 740 printf("Slave address = %u\n", slaveAddr); 741 #endif 742 } 743 break; 744 } 745 } 746 747 return 0; 748 } 749 750 593 751 #ifndef NO_USB_SUPPORT 594 752 void processUSB(ring_buffer_t* const ring_in_buff, ring_buffer_t* const ring_out_buff)
Note:
See TracChangeset
for help on using the changeset viewer.
