Author Topic: pin multiplexing in SAMD21  (Read 5806 times)

0 Members and 1 Guest are viewing this topic.

Offline aliyesamiTopic starter

  • Newbie
  • Posts: 3
  • Country: us
pin multiplexing in SAMD21
« on: March 29, 2016, 02:01:52 am »
the following code is used for PWM in SAMD21, can you please  tell me how it works ?

Code: [Select]
int main(void)
{
   
    SystemInit();
     PORT->Group[0].DIRSET.reg=PORT_PA19;
    PORT->Group[0].PINCFG[PORT_PA19].reg |= PORT_PINCFG_PMUXEN;
     PORT->Group[0].PMUX[PORT_PA19].bit.PMUXO = 4;
     
         
     PM->APBCMASK.reg |= PM_APBCMASK_TC3;
     
     GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(27/*TC3_GCLK_ID*/) |
     GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
     
     TC3->COUNT8.CTRLA.reg =
     TC_CTRLA_MODE(TC_CTRLA_MODE_COUNT8_Val) |
     TC_CTRLA_WAVEGEN(TC_CTRLA_WAVEGEN_NPWM_Val) |
     TC_CTRLA_PRESCALER(TC_CTRLA_PRESCALER_DIV1_Val) |
     TC_CTRLA_PRESCSYNC(TC_CTRLA_PRESCSYNC_PRESC_Val);
     while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT8.CTRLC.bit.INVEN1=1;
      while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT8.CTRLC.bit.CPTEN1=1;
      while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT8.COUNT.reg = 0;
     while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT8.PER.reg = 255;
     while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT8.CC[1].reg = 100;
     while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
     
     TC3->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
     while (TC3->COUNT8.STATUS.bit.SYNCBUSY);
    while (1)
    {
       
       
    }
}
 

Offline eddie0x7c3

  • Newbie
  • Posts: 9
  • Country: us
Re: pin multiplexing in SAMD21
« Reply #1 on: March 29, 2016, 02:14:12 am »
Code: [Select]
 
    PORT->Group[0].PMUX[PORT_PA19].bit.PMUXO = 4;



The Pmux index for that pin would be 9 NOT 19 (PORT_PA19 = 1u<<19)

Code: [Select]
Pmux [0]  = Pins 0 and 1
Pmux [1] = pins 2 and 3
Pmux [2] = pins 4 and 5
Pmux [3] = pins 6 and 7
....... and so on.......
Bits 0 - 3  in the register are for even numbered pins and the last 4 bits are for odd numbered pins


so it should read
Code: [Select]
PORT->Group[0].PMUX[9].bit.PMUXO = 4
« Last Edit: March 29, 2016, 02:18:36 am by eddie0x7c3 »
EE Student: Year 1 | West Palm Beach FL | "“It is not a dream, it is a simple feat of scientific electrical engineering, only expensive — blind, faint-hearted, doubting world! "    -NT
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11248
  • Country: us
    • Personal site
Re: pin multiplexing in SAMD21
« Reply #2 on: March 29, 2016, 02:46:56 am »
Also, have a look at my starter project that include more intuitive macros for manipulating GPIO pins: https://github.com/ataradov/mcu-starter-projects/tree/master/samd21 and this file specifically https://github.com/ataradov/mcu-starter-projects/blob/master/samd21/hal_gpio.h
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11248
  • Country: us
    • Personal site
Re: pin multiplexing in SAMD21
« Reply #3 on: March 29, 2016, 02:54:23 am »
Or more detailed:

1. Configure pin PA19 to be an output and connect it to the peripheral (PWM in this case) as opposed to the GPIO block.

Although this code looks incorrect:
Code: [Select]
     PORT->Group[0].DIRSET.reg=PORT_PA19;
    PORT->Group[0].PINCFG[PORT_PA19].reg |= PORT_PINCFG_PMUXEN;
     PORT->Group[0].PMUX[PORT_PA19].bit.PMUXO = 4;
I don't know what PORT_PA19 unrolls in ASF, but I assume it is (1 << 19). In this case it should read

Code: [Select]
     PORT->Group[0].DIRSET.reg=PORT_PA19;
    PORT->Group[0].PINCFG[19].reg |= PORT_PINCFG_PMUXEN;
     PORT->Group[0].PMUX[19 / 2].bit.PMUXO = 4;

Or, if you were to use my macros:

Code: [Select]
HAL_GPIO_PIN(PWM_OUT, A, 19) // Define a pin

HAL_GPIO_PWM_OUT_out(); // This is actually not necessary if you use XXX_pmuxen(), but I do it anyway for documentation purposes.
HAL_GPIO_PWM_OUT_pmuxen(PORT_PMUX_PMUXE_D_Val);


2.
Code: [Select]
     PM->APBCMASK.reg |= PM_APBCMASK_TC3;
Enable clock to the TC3.

3.
Code: [Select]
     GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(27/*TC3_GCLK_ID*/) |
     GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
Configure TC3 to be clocked from generator 0.

4. Setup TC3 according to the requirements. The code is pretty self explanatory.
« Last Edit: March 29, 2016, 02:58:17 am by ataradov »
Alex
 
The following users thanked this post: acaball1

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: pin multiplexing in SAMD21
« Reply #4 on: March 29, 2016, 05:00:43 pm »
Man, this subject is getting useful (?) answers on at least three different forums.  I hope the O.P. puts together a final summary and posts it in each of them...
 

Offline eddie0x7c3

  • Newbie
  • Posts: 9
  • Country: us
Re: pin multiplexing in SAMD21
« Reply #5 on: March 30, 2016, 01:48:45 am »
qualitative research ... leads to quantitative research .


 O0 <-- they need a light colored fro
EE Student: Year 1 | West Palm Beach FL | "“It is not a dream, it is a simple feat of scientific electrical engineering, only expensive — blind, faint-hearted, doubting world! "    -NT
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11248
  • Country: us
    • Personal site
Re: pin multiplexing in SAMD21
« Reply #6 on: March 30, 2016, 01:56:35 am »
Well, to be fair, here this question was asked because I redirect all people PMing me on AVRFreaks here, since that forum turned into unmanageable mess.
Alex
 

Offline acaball1

  • Newbie
  • Posts: 3
  • Country: us
Re: pin multiplexing in SAMD21
« Reply #7 on: June 27, 2016, 04:36:18 am »
Alex,
After successfully using your example I got a timer running PWM at 350KHz. With ASF & ATmel Start the maximum I could get was 13KHz.
Therefore, I'm now trying to learn bare metal programming. I've downloaded your mcu-starter-projects-master package from github. I'm using Atmel Studio 7.
My question is: Where can I find information on the library? There seems to be a lot of definitions and maybe functions already included.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11248
  • Country: us
    • Personal site
Re: pin multiplexing in SAMD21
« Reply #8 on: June 27, 2016, 04:44:45 am »
Where can I find information on the library?
There is no library. All the definitions are in header files and all the names and values correspond to the datasheet. So DS is your documentation. After some time you will be able to blindly write code, just assuming that things you need are defined.

The beauty of this approach is that it is applicable to all MCU vendors, unlike ASF and other manufacturer-supplied frameworks.

There seems to be a lot of definitions and maybe functions already included.
There are some useful basic functions (mostly abstractions for special instructions) defined in core_cm0plus.h, core_cmFunc.h and core_cmInstr.h. Those files contain good commentary and you can always go back to the manufacturer documentation for description on specific instruction.
Alex
 
The following users thanked this post: acaball1


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf