ECE471/571 (RTOS) STM32 C Example - Board to Board UART

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. enable UART3
  4. enable UART2
  5. disable Ethernet (optional)
  6. disable USB_OTG_FS (optional)

Adapt and compile a project (example 1)

  1. Open source/main.c
  2. Locate int main() function
  3. Inspect the GPIO initialization called from main()
  4. Locate the forever loop in main()
  5. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
            ** HERE1 **
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE2 **
        }
        /* USER CODE END 3 */
    
  6. Code for ** HERE1 **
      GPIO_PinState state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
      unsigned int sync_count = 100; // how often to send a sync data frame
    
  7. Code for ** HERE2 **
        unsigned char buffer[2];
        GPIO_PinState new_state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
        if ( state!=new_state || sync_count==0 ) { // send new data only if data changed or if time for sync data frame
            state=new_state;
            sync_count = 100;
            if ( state ) {
                buffer[0]='1';
            } else {
                buffer[0]='0';
            }
            HAL_UART_Transmit(&huart2, buffer, 1, 5);
        } else {
            if (sync_count>0) {
                sync_count--;
            }
        }
    
        HAL_StatusTypeDef status = HAL_UART_Receive(&huart2, buffer, 1, 0);
        // Note: timeout of 0ms means that either a byte is already received and waiting or timeout is returned
        if ( HAL_OK == status ) {
          if ( buffer[0]=='0' ) {
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
          } else if ( buffer[0]=='1' ) {
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
          } else{
            // nothing - unrecognized command
          }
        }
    
        HAL_Delay(10);
    

web site front local main page local list page general bookmarks software bookmarks go back copyright info