SAMD21 multiple PWM using ASF

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello to all,

I'm trying to configure multiple PWM output using ASF. I need 8 PWM output.

 

I started using the basic TCC Callback example, modifying it to manage the 8 output, but I only get PWM one at a time, or two from pin with different TCC.

 

Is there a way to configure multiple PWM using ASF?

 

Many Thanks

Clemente

Last Edited: Fri. Jan 17, 2020 - 10:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes it is possible but to get 8 outputs you will have to use all 3 TCC instances (it's the number of compare channels that is significant and they have 4, 2 and 2).

 

#include <asf.h>

static void configure_tcc(void);

struct tcc_module tcc_instance;
struct tcc_module tcc_instance1;
struct tcc_module tcc_instance2;

static void configure_tcc(void)
{
    struct tcc_config config_tcc;
    
    tcc_get_config_defaults(&config_tcc, TCC0);
    config_tcc.counter.period = 0x1FF;
    config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
    config_tcc.compare.match[0] = 0x23;
    config_tcc.compare.match[1] = 0x33;
    config_tcc.compare.match[2] = 0x43;
    config_tcc.compare.match[3] = 0x53;
    
    config_tcc.pins.enable_wave_out_pin[0] = true;
    config_tcc.pins.wave_out_pin[0]        = PIN_PA04E_TCC0_WO0;
    config_tcc.pins.wave_out_pin_mux[0]    = MUX_PA04E_TCC0_WO0;
    
    config_tcc.pins.enable_wave_out_pin[1] = true;
    config_tcc.pins.wave_out_pin[1]        = PIN_PA05E_TCC0_WO1;
    config_tcc.pins.wave_out_pin_mux[1]    = MUX_PA05E_TCC0_WO1;
    
    config_tcc.pins.enable_wave_out_pin[2] = true;
    config_tcc.pins.wave_out_pin[2]        = PIN_PA10F_TCC0_WO2;
    config_tcc.pins.wave_out_pin_mux[2]    = MUX_PA10F_TCC0_WO2;
    
    config_tcc.pins.enable_wave_out_pin[3] = true;
    config_tcc.pins.wave_out_pin[3]        = PIN_PA11F_TCC0_WO3;
    config_tcc.pins.wave_out_pin_mux[3]    = MUX_PA11F_TCC0_WO3;
    
    tcc_init(&tcc_instance, TCC0, &config_tcc);
    
    tcc_get_config_defaults(&config_tcc, TCC1);
    config_tcc.counter.period = 0x1FF;
    config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
    config_tcc.compare.match[0] = 0x63;
    config_tcc.compare.match[1] = 0x73;
    
    config_tcc.pins.enable_wave_out_pin[0] = true;
    config_tcc.pins.wave_out_pin[0]        = PIN_PA06E_TCC1_WO0;
    config_tcc.pins.wave_out_pin_mux[0]    = MUX_PA06E_TCC1_WO0;
    
    config_tcc.pins.enable_wave_out_pin[1] = true;
    config_tcc.pins.wave_out_pin[1]        = PIN_PA07E_TCC1_WO1;
    config_tcc.pins.wave_out_pin_mux[1]    = MUX_PA07E_TCC1_WO1;
    
    tcc_init(&tcc_instance1, TCC1, &config_tcc);
    
    tcc_get_config_defaults(&config_tcc, TCC2);
    config_tcc.counter.period = 0x1FF;
    config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
    config_tcc.compare.match[0] = 0x83;
    config_tcc.compare.match[1] = 0x93;
    
    config_tcc.pins.enable_wave_out_pin[0] = true;
    config_tcc.pins.wave_out_pin[0]        = PIN_PA12E_TCC2_WO0;
    config_tcc.pins.wave_out_pin_mux[0]    = MUX_PA12E_TCC2_WO0;
    
    config_tcc.pins.enable_wave_out_pin[1] = true;
    config_tcc.pins.wave_out_pin[1]        = PIN_PA13E_TCC2_WO1;
    config_tcc.pins.wave_out_pin_mux[1]    = MUX_PA13E_TCC2_WO1;
    
    tcc_init(&tcc_instance2, TCC2, &config_tcc);
    
    tcc_enable(&tcc_instance);
    tcc_enable(&tcc_instance1);	
    tcc_enable(&tcc_instance2);	
}

int main(void)
{
    system_init();
    configure_tcc();
    while(1);
}

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Lajon,

many thanks for your responce.

 

Lajon wrote:

Yes it is possible but to get 8 outputs you will have to use all 3 TCC instances (it's the number of compare channels that is significant and they have 4, 2 and 2).

/Lars

Ah! This was not clear to me. I have this pin assignement:
 

    PWM_OUT1 PA9  TCC1/WO[3]   CC1	0
    PWM_OUT2 PA8  TCC1/WO[2]   CC0	1
    PWM_OUT3 PA15 TCC0/WO[5]   CC1	2
    PWM_OUT4 PA20 TCC0/WO[6]** CC2	3
    PWM_OUT5 PA6  TCC1/WO[0]   CC0	4
    PWM_OUT6 PA7  TCC1/WO[1]   CC1	5
    PWM_OUT7 PB12 TCC0/WO[6]** CC2	6
    PWM_OUT8 PB13 TCC0/WO[7]   CC3	7

and if I undestand what you say, about the compare channel (4,2,2), I can't use four TCC1 channels concurrently.

I need to free 2 pins that route to TCC2, isn't it?

 

Thanks again for your time.

Clemente

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right, you need to use TCC2 also.

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Lars.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lajon/Lars,

 

Have you used the code posted above ( Wed. Feb 15, 2017 - 11:40 PM ) to produce PWM Wave-forms on multiple channels on same TCCx simultaneously?

 

I am using similar code; but when I enable Channel 1, Channel 0 stops. I can have two simultaneous wave-forms from TCC0 and TCC1.

 

C. Dicaprio, Did you get 8 simultaneous outputs working?

 

Thanks,

Betta

 

Betta

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes it works, I would not post the code if it did not work.

SAMD21 8 pwm from TCC0,TCC1 and TCC2

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Lars,

 

I'm a bit confused as to what the Waveform output 5 to 8 do and how to use them. I need a total of 24 PWM (for driving 8 RGB LEDs). The Microchip selector tool lists a total of 24 PWM outputs, but I am not sure which pins to select to enable that.

Last Edited: Tue. Apr 3, 2018 - 04:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lars,

Thank you very much!

Nick

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tabarnakos wrote:

Hello Lars,

 

I'm a bit confused as to what the Waveform output 5 to 8 do and how to use them. I need a total of 24 PWM (for driving 8 RGB LEDs). The Microchip selector tool lists a total of 24 PWM outputs, but I am not sure which pins to select to enable that.

 

 

I was wondering exactly the same thing. I don't understand the number 24, which would assume that all instances of the TCC have 8 INDEPENDENT PWM channels. But since the number of CC regs is limited I don't think that there are really 24 TRUE channels?

Correct me if I am wrong.

The data sheet is quite thick and is very unclear ion how to use all 8 outputs for each instance of the TCC.

-42-

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that you are replying to a post from nearly 2 years ago!

 

Also, the SAMD21 is not an AVR - so you'd be better in the SAM forums:

 

https://community.atmel.com/atmel-smart-arm-based-mcus

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 But since the number of CC regs is limited I don't think that there are really 24 TRUE channels?

I agree, the number of independent PWM outputs is the same as the number of compare channels. Need to count the ones from TC instances also (if there are unique output pins available for each).

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I know that. I just replied to the post in the original thread which I found in a search.

I see that this forum IS in the SAM(ARM) section now.

Thanks

 

-42-

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The SAMD21 D variant has an extra TCC which is a copy of TCC0, with 4 compare channels, then you only have to combine two TCCs. The variant D is, however, not very common

Jerry