24.8.5.1.2 Code

Add to the main application source file, before any functions, according to the kit used:
  • SAM D21 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC6_DMAC_ID_MC_0
    
  • SAM R21 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC3_DMAC_ID_MC_0
    
  • SAM D11 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC1_DMAC_ID_MC_0
    
  • SAM L21 Xplained Pro.
    #define PWM_MODULE      EXT2_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT2_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT2_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC0_DMAC_ID_MC_0
    
  • SAM L22 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC0_DMAC_ID_MC_0
    
  • SAM DA1 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC6_DMAC_ID_MC_0
    
  • SAM C21 Xplained Pro.
    #define PWM_MODULE      EXT1_PWM_MODULE
    
    #define PWM_OUT_PIN     EXT1_PWM_0_PIN
    
    #define PWM_OUT_MUX     EXT1_PWM_0_MUX
    
    #define M2M_DMAC_TRIGGER_ID TC0_DMAC_ID_MC_0
    
    Add to the main application source file, outside of any functions:
    struct tc_module tc_instance;
    
    struct dma_resource example_resource;
    
    #define TRANSFER_SIZE    (16)
    
    #define TRANSFER_COUNTER (32)
    
    static uint8_t source_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
    
    static uint8_t destination_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
    
    static volatile bool transfer_is_done = false;
    
    COMPILER_ALIGNED(16)
    DmacDescriptor example_descriptor;
    
    Copy-paste the following setup code to your user application:
    #define TRANSFER_SIZE    (16)
    
    #define TRANSFER_COUNTER (32)
    
    static uint8_t source_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
    
    static uint8_t destination_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
    
    static volatile bool transfer_is_done = false;
    
    COMPILER_ALIGNED(16)
    DmacDescriptor example_descriptor;
    
    void configure_tc(void)
    {
        struct tc_config config_tc;
        tc_get_config_defaults(&config_tc);
    
        config_tc.counter_size    = TC_COUNTER_SIZE_16BIT;
        config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
        config_tc.counter_16_bit.compare_capture_channel[0] = (0xFFFF / 4);
    
        config_tc.pwm_channel[0].enabled = true;
        config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
        config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
    
        tc_init(&tc_instance, PWM_MODULE, &config_tc);
    
        tc_enable(&tc_instance);
    }
    
    void transfer_done(struct dma_resource* const resource )
    {
        UNUSED(resource);
    
        transfer_is_done = true;
    }
    
    void configure_dma_resource(struct dma_resource *resource)
    {
        struct dma_resource_config config;
    
        dma_get_config_defaults(&config);
        config.peripheral_trigger = M2M_DMAC_TRIGGER_ID;
    
        dma_allocate(resource, &config);
    }
    
    void setup_dma_descriptor(DmacDescriptor *descriptor)
    {
        struct dma_descriptor_config descriptor_config;
    
        dma_descriptor_get_config_defaults(&descriptor_config);
    
        descriptor_config.block_transfer_count = TRANSFER_SIZE;
        descriptor_config.source_address = (uint32_t)source_memory + TRANSFER_SIZE;
        descriptor_config.destination_address =
                (uint32_t)destination_memory + TRANSFER_SIZE;
    
        dma_descriptor_create(descriptor, &descriptor_config);
    }
    
    
    Add to user application initialization (typically the start of main()):
    configure_tc();