5.3.1.1 Application Logic

In this section, only fractional baud rate part will be explained, whereas the remaining explanation is covered in the Basic Configuration section. The fractional baud rate of 11000 bps is used in the application. The macro FRAC_BAUD_RATE contains the fractional baud value. Adhere to the steps outlined for the basic configuration of the SAM D21 Curiosity Nano Evaluation Kit.

  1. To add and configure MPLAB Harmony components using the MCC, see SAM D21 Curiosity Nano Evaluation Kit.
  2. Add the following macros:
    #define RX_BUFFER_SIZE 1
    #define FRAC_BAUD_RATE 11000
    #define USART_SAMPLE_NUM 16 
    
    Figure 5-37. Defining the Macro for Fractional Baud Rate
  3. Add the following variables:
    uint16_t baud;
    uint8_t fp; 
    
  4. Add event handlers, see Step 1 in the Application Logic section.
  5. Add the following code outside the main() function:
    // Function to calculate the fractional baud value
    void calculate_fractional_baud_value(const uint32_t baudrate, const uint32_t peripheral_clock, uint8_t sample_num)
    {
        uint32_t mul_ratio;
        mul_ratio = (uint64_t)((uint64_t)peripheral_clock * (uint64_t)1000) / (uint64_t)(baudrate * sample_num);
        baud = mul_ratio / 1000;
        fp = ((mul_ratio - (baud * 1000)) * 8) / 1000;
    }
    
    // USART initialization with fractional baud rate settings
    void ext_usart_init(void)
    {
        calculate_fractional_baud_value(FRAC_BAUD_RATE, SERCOM1_USART_FrequencyGet(), USART_SAMPLE_NUM);
        
        SERCOM1_USART_Disable();
        SERCOM1_REGS->USART_INT.SERCOM_CTRLA |= SERCOM_USART_INT_CTRLA_SAMPR(1UL);
        SERCOM1_REGS->USART_INT.SERCOM_BAUD = SERCOM_USART_INT_BAUD_FRAC_BAUD(baud) | SERCOM_USART_INT_BAUD_FRAC_FP(fp);
        SERCOM1_USART_Enable();
    } 
    
  6. In the following code examples, the value of the fractional baud rate is calculated using the function, calculate_fractional_baud_value () . In the ext_usart_init () function, SERCOM1 is disabled, enabled, and also sets the CTRLA and BAUD registers. The value of the fractional baud rate is calculated using the function, calculate_fractional_baud_value () . In the ext_usart_init () function, SERCOM1 is disabled, enabled, and also sets the CTRLA and BAUD registers.
    Figure 5-38. Implementation of Fraction Baud Rate Configuration
  7. Add the remaining code as shown in the Step 4 of the Application Logic section.
    Figure 5-39. Host Side
    Figure 5-40. Client Side