Go Down

Topic: Where do I find reference documentation for library calls I find in posted code? (Read 34 times) previous topic - next topic


Hi there,

I am new to Atmel MCUs but have been programming other MCUs for years (Freescale).  I am working on a PWM interrupt and found some Arduino code snippets with references to library calls like:


These are not native Arduino reserved words in the IDE, like 'pinMode' yet there is no header include for them.  So where are these defined in Arduino 1.6.13? 

How do I learn about how these write to the actual registers listed in the SAM3X data sheet (like PWM Enable Register, PWM_ENA.)?

Also, I see 'PWM_Handler(void)' in the code - is this predefined somewhere to tie out to the ISR vector for PWM?   In other words, how does the Arduino compiler know that the function my  source code called 'PWM_Handler(void)'  is actually an ISR and not just any old function?  Seems like magic.

All over the forum I see code snippets with references to library calls like this, but have not come across any clues to digging into them.  I found something about a libsam on a google search, but not finding it in the arduino directory (Arduino 1.6.13).

Where is a good source of info on the details of these magical libraries?  I am truly stumped.

BTW, below is the code I am looking at from stack overflow which works at producing a 192KHZ 50% duty cycle pulse on pin 8 of my Due.



uint32_t totalTime = 0; // total elapsed time

uint32_t pwmPin = 8; // PWM output pin
uint32_t irqPin = 5; // interrupt test pin
uint32_t channel = g_APinDescription[pwmPin].ulPWMChannel; // set channel for PWM out
uint32_t sampFreq = 192000ul; // sampling frequency (Hz)
uint16_t maxDutyCount = 255;
uint32_t clkAFreq = 42000000ul; // clock frequency (Hz)
uint32_t pwmFreq = (clkAFreq * 2)/sampFreq; // calculate PWM frequency
uint16_t dutyPercent = 128; // starting duty percent
uint16_t dutyAct = pwmFreq * (maxDutyCount-dutyPercent) / maxDutyCount; //

void setup() {
  // put your setup code here, to run once:
  pinMode(irqPin, OUTPUT);

  PWMC_ConfigureClocks(clkAFreq, 0, VARIANT_MCK);


   channel = g_APinDescription[pwmPin].ulPWMChannel; // channel 5
   PWMC_ConfigureChannel(PWM_INTERFACE, channel, clkAFreq, 0, 0);
   PWMC_SetPeriod(PWM_INTERFACE, channel, pwmFreq);
   PWMC_EnableChannel(PWM_INTERFACE, channel);
   PWMC_SetDutyCycle(PWM_INTERFACE, channel, dutyAct);
   PWM_INTERFACE->PWM_IER1 = 0x20; //enable interrupt on channel 5
   PWM_INTERFACE->PWM_IDR1 = 0xFFFFFFDF; //enable interrupt on channel 5
   PWM_INTERFACE->PWM_IER2 = 0x00002001; //enable interrupt on channel 5
   PWM_INTERFACE->PWM_IDR2 = 0xFFFFDFFE; //enable interrupt on channel 5

   NVIC_DisableIRQ(PWM_IRQn); // set up interrupt
   NVIC_SetPriority(PWM_IRQn, 0);
   NVIC_EnableIRQ((IRQn_Type)36); //NVIC_EnableIRQ(PWM_IRQn);
   PWMC_EnableChannel(PWM_INTERFACE, channel);
   //Enable of the Interrupts (writing CHIDx and FCHIDx
   //in PWM_IER1 register, and writing WRDYE, ENDTXE,
   //TXBUFE, UNRE, CMPMx and CMPUx in PWM_IER2 register)

void loop() {

void PWM_Handler(void) // PWM interrupt handler
   volatile long dummy = PWM_INTERFACE->PWM_ISR1; // clear interrupt flag
   dummy = PWM_INTERFACE->PWM_ISR2; // clear interrupt flag
//your code here!


These are Atmel-defined functions from "libsam" (which pre-dates the current "ASF" libraries.)
You can find the source code in .../packages/arduino/hardware/sam/1.6.9/system/libsam/source/ (whereever that happens to live in your IDE install.)

Last time I looked, I couldn't find libsam documentation :-(

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131