pin multiplexing in SAMD21 - Page 1
Forum section: electronics / general

author: aliyesami
author_rank: Newbie
timestamp: March 29, 2016, 02:01:52 am
content: the following code is used for PWM in SAMD21, can you please tell me how it works ?Code:int main(void){SystemInit();PORT->Group.DIRSET.reg=PORT_PA19;PORT->Group.PINCFG.reg |= PORT_PINCFG_PMUXEN;PORT->Group.PMUX.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.reg = 100;while (TC3->COUNT8.STATUS.bit.SYNCBUSY);TC3->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;while (TC3->COUNT8.STATUS.bit.SYNCBUSY);while (1){}}

author: eddie0x7c3
author_rank: Newbie
timestamp: March 29, 2016, 02:14:12 am
content: Code:PORT->Group.PMUX.bit.PMUXO = 4;The Pmux index for that pin would be 9 NOT 19 (PORT_PA19 = 1u<<19)Code:Pmux  = Pins 0 and 1Pmux  = pins 2 and 3Pmux  = pins 4 and 5Pmux  = 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 pinsso it should readCode:PORT->Group.PMUX.bit.PMUXO = 4

author: ataradov
author_rank: Super Contributor
timestamp: March 29, 2016, 02:46:56 am
content: 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/samd21and this file specificallyhttps://github.com/ataradov/mcu-starter-projects/blob/master/samd21/hal_gpio.h

author: ataradov
author_rank: Super Contributor
timestamp: March 29, 2016, 02:54:23 am
content: 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:PORT->Group.DIRSET.reg=PORT_PA19;PORT->Group.PINCFG.reg |= PORT_PINCFG_PMUXEN;PORT->Group.PMUX.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 readCode:PORT->Group.DIRSET.reg=PORT_PA19;PORT->Group.PINCFG.reg |= PORT_PINCFG_PMUXEN;PORT->Group.PMUX[19 / 2].bit.PMUXO = 4;Or, if you were to use my macros:Code:HAL_GPIO_PIN(PWM_OUT, A, 19) // Define a pinHAL_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:PM->APBCMASK.reg |= PM_APBCMASK_TC3;Enable clock to the TC3.3.Code: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.

author: westfw
author_rank: Super Contributor
timestamp: March 29, 2016, 05:00:43 pm
content: 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...

author: eddie0x7c3
author_rank: Newbie
timestamp: March 30, 2016, 01:48:45 am
content: qualitative research ... leads to quantitative research .O0 <-- they need a light colored fro

author: ataradov
author_rank: Super Contributor
timestamp: March 30, 2016, 01:56:35 am
content: 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.

author: acaball1
author_rank: Newbie
timestamp: June 27, 2016, 04:36:18 am
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 27, 2016, 04:44:45 am
content: Quote from: acaball1 on June 27, 2016, 04:36:18 amThere 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.Quote from: acaball1 on June 27, 2016, 04:36:18 amThere 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.