Adding PWM API implementation

pull/1297/head
vimalrajr 2015-07-29 11:33:14 +05:30 committed by Karthik Purushothaman
parent 62a6bcaeeb
commit 987567ee59
12 changed files with 4552 additions and 10 deletions

View File

@ -95,15 +95,13 @@ typedef enum {
/* Pad 3 definitions */
MREPEAT(SERCOM_INST_NUM, _SERCOM_PAD_NAME, 3)
} SercomPadName;
/*
typedef enum {
PWM_1 = 1,
PWM_2,
PWM_3,
PWM_4,
PWM_5,
PWM_6
} PWMName;*/
PWM_0 = (0x42002000UL), /**< \brief (TCC0) APB Base Address */
PWM_1 = (0x42002400UL), /**< \brief (TCC1) APB Base Address */
PWM_2 = (0x42002800UL), /**< \brief (TCC2) APB Base Address */
} PWMName;
#define STDIO_UART_TX USBTX
#define STDIO_UART_RX USBRX

View File

@ -31,7 +31,7 @@ extern const PinMap PinMap_SERCOM_PAD[];
extern const PinMap PinMap_SERCOM_PADEx[];
/************PWM***************/
//extern const PinMap PinMap_PWM[];
extern const PinMap PinMap_PWM[];
/**********EXTINT*************/
extern const PinMap PinMap_EXTINT[];

View File

@ -96,6 +96,27 @@ const PinMap PinMap_SERCOM_PADEx[] = {
/************PWM***************/
const PinMap PinMap_PWM[] = {
{PA00, PWM_2, 4},
{PA01, PWM_2, 4},
{PA04, PWM_0, 4},
{PA05, PWM_0, 4},
{PA06, PWM_1, 4},
{PA07, PWM_1, 4},
{PA08, PWM_0, 4},
{PA09, PWM_0, 4},
{PA12, PWM_2, 4},
{PA13, PWM_2, 4},
{PA16, PWM_2, 4},
{PA17, PWM_2, 4},
{PA18, PWM_0, 5},
{PA19, PWM_0, 5},
{PA22, PWM_0, 5},
{PA23, PWM_0, 5},
{PA24, PWM_1, 5},
{PA25, PWM_1, 5},
{PA30, PWM_1, 4},
{PA31, PWM_1, 4},
/* Not connected */
{NC , NC , NC}
};

View File

@ -0,0 +1,97 @@
/**
* \page asfdoc_sam0_tcc_basic_use_case Quick Start Guide for TCC - Basic
*
* The supported board list:
* - SAM D21/R21/L21 Xplained Pro
*
* In this use case, the TCC will be used to generate a PWM signal. Here
* the pulse width is set to one quarter of the period.
* When connect PWM output to LED it makes the LED light. To see the waveform,
* you may need an oscilloscope.
*
* The PWM output is set up as follows:
* <table>
* <tr><th> Board </td><th> Pin </td><th> Connect to </td></tr>
* <tr><td> SAMD21 Xpro </td><td> PB30 </td><td> LED0 </td></tr>
* <tr><td> SAMR21 Xpro </td><td> PA19 </td><td> LED0 </td></tr>
* <tr><td> SAML21 Xpro </td><td> PB10 </td><td> LED0 </td></tr>
* </table>
*
* The TCC module will be set up as follows:
* - GCLK generator 0 (GCLK main) clock source
* - Use double buffering write when set top, compare, or pattern through API
* - No dithering on the counter or compare
* - No prescaler
* - Single Slope PWM wave generation
* - GCLK reload action
* - Don't run in standby
* - No fault or waveform extensions
* - No inversion of waveform output
* - No capture enabled
* - Count upward
* - Don't perform one-shot operations
* - No event input enabled
* - No event action
* - No event generation enabled
* - Counter starts on 0
* - Counter top set to 0xFFFF
* - Capture compare channel 0 set to 0xFFFF/4
*
* \section asfdoc_sam0_tcc_basic_use_case_setup Quick Start
*
* \subsection asfdoc_sam0_tcc_basic_use_case_prereq Prerequisites
* There are no prerequisites for this use case.
*
* \subsection asfdoc_sam0_tcc_basic_use_case_setup_code Code
*
* Add to the main application source file, before any functions:
* \snippet conf_quick_start.h definition_pwm
*
* Add to the main application source file, outside of any functions:
* \snippet qs_tcc_basic.c module_inst
*
* Copy-paste the following setup code to your user application:
* \snippet qs_tcc_basic.c setup
*
* Add to user application initialization (typically the start of \c main()):
* \snippet qs_tcc_basic.c setup_init
*
* \subsection asfdoc_sam0_tcc_basic_use_case_setup_flow Workflow
* -# Create a module software instance structure for the TCC module to store
* the TCC driver state while it is in use.
* \snippet qs_tcc_basic.c module_inst
* \note This should never go out of scope as long as the module is in use.
* In most cases, this should be global.
*
* -# Configure the TCC module.
* -# Create a TCC module configuration struct, which can be filled out to
* adjust the configuration of a physical TCC peripheral.
* \snippet qs_tcc_basic.c setup_config
* -# Initialize the TCC configuration struct with the module's default values.
* \snippet qs_tcc_basic.c setup_config_defaults
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
*
* -# Alter the TCC settings to configure the counter width, wave generation
* mode and the compare channel 0 value.
* \snippet qs_tcc_basic.c setup_change_config
* -# Alter the TCC settings to configure the PWM output on a physical device
* pin.
* \snippet qs_tcc_basic.c setup_change_config_pwm
* -# Configure the TCC module with the desired settings.
* \snippet qs_tcc_basic.c setup_set_config
* -# Enable the TCC module to start the timer and begin PWM signal generation.
* \snippet qs_tcc_basic.c setup_enable
*
*
* \section asfdoc_sam0_tcc_basic_use_case_main Use Case
*
* \subsection asfdoc_sam0_tcc_basic_use_case_main_code Code
* Copy-paste the following code to your user application:
* \snippet qs_tcc_basic.c main
*
* \subsection asfdoc_sam0_tcc_basic_use_case_main_flow Workflow
* -# Enter an infinite loop while the PWM wave is generated via the TCC module.
* \snippet qs_tcc_basic.c main_loop
*/

View File

@ -0,0 +1,103 @@
/**
* \page asfdoc_sam0_tcc_buffering_use_case Quick Start Guide for TCC - Double Buffering and Circular
*
* The supported board list:
* - SAM D21/R21/L21 Xplained Pro
*
* In this use case, the TCC will be used to generate a PWM signal. Here
* the pulse width alters in one quarter and three quarter of the period.
* When connect PWM output to LED it makes the LED light. To see the waveform,
* you may need an oscilloscope.
*
* The PWM output is set up as follows:
* <table>
* <tr><th> Board </td><th> Pin </td><th> Connect to </td></tr>
* <tr><td> SAMD21 Xpro </td><td> PB30 </td><td> LED0 </td></tr>
* <tr><td> SAMR21 Xpro </td><td> PA19 </td><td> LED0 </td></tr>
* <tr><td> SAML21 Xpro </td><td> PB10 </td><td> LED0 </td></tr>
* </table>
*
* The TCC module will be set up as follows:
* - GCLK generator 0 (GCLK main) clock source
* - Use double buffering write when set top, compare, or pattern through API
* - No dithering on the counter or compare
* - Prescaler is set to 1024
* - Single Slope PWM wave generation
* - GCLK reload action
* - Don't run in standby
* - No fault or waveform extensions
* - No inversion of waveform output
* - No capture enabled
* - Count upward
* - Don't perform one-shot operations
* - No event input enabled
* - No event action
* - No event generation enabled
* - Counter starts on 0
* - Counter top set to 8000
* - Capture compare channel set to 8000/4
* - Capture compare channel buffer set to 8000*3/4
* - Circular option for compare channel is enabled so that the compare
* values keep switching on update condition
*
* \section asfdoc_sam0_tcc_buffering_use_case_setup Quick Start
*
* \subsection asfdoc_sam0_tcc_buffering_use_case_prereq Prerequisites
* There are no prerequisites for this use case.
*
* \subsection asfdoc_sam0_tcc_buffering_use_case_setup_code Code
*
* Add to the main application source file, before any functions:
* \snippet conf_quick_start_buffering.h definition_pwm
*
* Add to the main application source file, outside of any functions:
* \snippet qs_tcc_buffering.c module_inst
*
* Copy-paste the following setup code to your user application:
* \snippet qs_tcc_buffering.c setup
*
* Add to user application initialization (typically the start of \c main()):
* \snippet qs_tcc_buffering.c setup_init
*
* \subsection asfdoc_sam0_tcc_buffering_use_case_setup_flow Workflow
* -# Create a module software instance structure for the TCC module to store
* the TCC driver state while it is in use.
* \snippet qs_tcc_buffering.c module_inst
* \note This should never go out of scope as long as the module is in use.
* In most cases, this should be global.
*
* -# Configure the TCC module.
* -# Create a TCC module configuration struct, which can be filled out to
* adjust the configuration of a physical TCC peripheral.
* \snippet qs_tcc_buffering.c setup_config
* -# Initialize the TCC configuration struct with the module's default values.
* \snippet qs_tcc_buffering.c setup_config_defaults
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
*
* -# Alter the TCC settings to configure the counter width, wave generation
* mode and the compare channel 0 value.
* \snippet qs_tcc_buffering.c setup_change_config
* -# Alter the TCC settings to configure the PWM output on a physical device
* pin.
* \snippet qs_tcc_buffering.c setup_change_config_pwm
* -# Configure the TCC module with the desired settings.
* \snippet qs_tcc_buffering.c setup_set_config
* -# Set to compare buffer value and enable circular of double buffered
* compare values.
* \snippet qs_tcc_buffering.c setup_set_buffering
* -# Enable the TCC module to start the timer and begin PWM signal generation.
* \snippet qs_tcc_buffering.c setup_enable
*
*
* \section asfdoc_sam0_tcc_buffering_use_case_main Use Case
*
* \subsection asfdoc_sam0_tcc_buffering_use_case_main_code Code
* Copy-paste the following code to your user application:
* \snippet qs_tcc_buffering.c main
*
* \subsection asfdoc_sam0_tcc_buffering_use_case_main_flow Workflow
* -# Enter an infinite loop while the PWM wave is generated via the TCC module.
* \snippet qs_tcc_buffering.c main_loop
*/

View File

@ -0,0 +1,220 @@
/**
* \page asfdoc_sam0_tcc_dma_use_case Quick Start Guide for Using DMA with TCC
*
* The supported board list:
* - SAM D21/R21/L21 Xplained Pro
*
* In this use case, the TCC will be used to generate a PWM signal. Here
* the pulse width varies through following values with the help of DMA
* transfer: one quarter of the period, half of the period, and three quarters
* of the period.
* The PWM output can be used to drive an LED. The waveform can also be
* viewed using an oscilloscope.
* The output signal is also fed back to another TCC channel by event system,
* the event stamps are captured and transferred to a buffer by DMA.
*
* The PWM output is set up as follows:
* <table>
* <tr><th> Board </td><th> Pin </td><th> Connect to </td></tr>
* <tr><td> SAMD21 Xpro </td><td> PB30 </td><td> LED0 </td></tr>
* <tr><td> SAMR21 Xpro </td><td> PA19 </td><td> LED0 </td></tr>
* <tr><td> SAML21 Xpro </td><td> PB10 </td><td> LED0 </td></tr>
* </table>
*
* The TCC module will be setup as follows:
* - GCLK generator 0 (GCLK main) clock source
* - Use double buffering write when set top, compare, or pattern through API
* - No dithering on the counter or compare
* - No prescaler
* - Single Slope PWM wave generation
* - GCLK reload action
* - Don't run in standby
* - No fault or waveform extensions
* - No inversion of waveform output
* - No capture enabled
* - Count upward
* - Don't perform one-shot operations
* - Counter starts on 0
* - Counter top set to 0x1000
* - Channel 0 (on SAM D21 Xpro) or 3 (on SAM R21 Xpro) is set to
* compare and match value 0x1000*3/4 and generate event
* - Channel 1 is set to capture on input event
*
* The event resource of EVSYS module will be setup as follows:
* - TCC match capture channel 0 (on SAM D21 Xpro) or 3 (on SAM R21 Xpro) is
* selected as event generator
* - Event generation is synchronous, with rising edge detected
* - TCC match capture channel 1 is the event user
*
* The DMA resource of DMAC module will be setup as follows:
* - Two DMA resources are used
* - Both DMA resources use peripheral trigger
* - Both DMA resources perform beat transfer on trigger
* - Both DMA resources use beat size of 16 bits
* - Both DMA resources are configured to transfer three beats and
* then repeat again in same buffer
* - On DMA resource which controls the compare value
* - TCC0 overflow triggers DMA transfer
* - The source address increment is enabled
* - The destination address is fixed to TCC channel 0 Compare/Capture
*register
* - On DMA resource which reads the captured value
* - TCC0 capture on channel 1 triggers DMA transfer
* - The source address is fixed to TCC channel 1 Compare/Capture register
* - The destination address increment is enabled
* - The captured value is transferred to an array in SRAM
*
* \section asfdoc_sam0_tcc_dma_use_case_setup Quick Start
*
* \subsection asfdoc_sam0_tcc_dma_use_case_prereq Prerequisites
* There are no prerequisites for this use case.
*
* \subsection asfdoc_sam0_tcc_dma_use_case_setup_code Code
*
* Add to the main application source file, before any functions, according to
* the kit used:
* - SAM D21 Xplained Pro.
* \snippet samd21_xplained_pro/conf_quick_start_dma.h definition_pwm
* \snippet samd21_xplained_pro/conf_quick_start_dma.h definition_feedback
* \snippet samd21_xplained_pro/conf_quick_start_dma.h definition_dma_compare_trigger
* \snippet samd21_xplained_pro/conf_quick_start_dma.h definition_dma_capture_trigger
* - SAM R21 Xplained Pro.
* \snippet samr21_xplained_pro/conf_quick_start_dma.h definition_pwm
* \snippet samr21_xplained_pro/conf_quick_start_dma.h definition_feedback
* \snippet samr21_xplained_pro/conf_quick_start_dma.h definition_dma_compare_trigger
* \snippet samr21_xplained_pro/conf_quick_start_dma.h definition_dma_capture_trigger
* - SAM L21 Xplained Pro.
* \snippet saml21_xplained_pro/conf_quick_start_dma.h definition_pwm
* \snippet saml21_xplained_pro/conf_quick_start_dma.h definition_feedback
* \snippet saml21_xplained_pro/conf_quick_start_dma.h definition_dma_compare_trigger
*
* Add to the main application source file, outside of any functions:
* \snippet qs_tcc_dma.c module_inst
* \snippet qs_tcc_dma.c capture_variables
* \snippet qs_tcc_dma.c compare_variables
*
* Copy-paste the following setup code to your user application:
* \snippet qs_tcc_dma.c config_event_for_capture
* \snippet qs_tcc_dma.c config_dma_for_capture
* \snippet qs_tcc_dma.c config_dma_for_wave
* \snippet qs_tcc_dma.c setup
*
* Add to user application initialization (typically the start of \c main()):
* \snippet qs_tcc_dma.c setup_init
*
* \subsection asfdoc_sam0_tcc_dma_use_case_setup_flow Workflow
* \subsubsection asfdoc_sam0_tcc_dma_use_case_setup_flow_tcc Configure the TCC
* -# Create a module software instance structure for the TCC module to store
* the TCC driver state while it is in use.
* \snippet qs_tcc_dma.c module_inst
* \note This should never go out of scope as long as the module is in use.
* In most cases, this should be global.
* -# Create a TCC module configuration struct, which can be filled out to
* adjust the configuration of a physical TCC peripheral.
* \snippet qs_tcc_dma.c setup_config
* -# Initialize the TCC configuration struct with the module's default values.
* \snippet qs_tcc_dma.c setup_config_defaults
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
* -# Alter the TCC settings to configure the counter width, wave generation
* mode and the compare channel 0 value.
* \snippet qs_tcc_dma.c setup_change_config
* -# Alter the TCC settings to configure the PWM output on a physical device
* pin.
* \snippet qs_tcc_dma.c setup_change_config_pwm
* -# Configure the TCC module with the desired settings.
* \snippet qs_tcc_dma.c setup_set_config
* -# Configure and enable the desired events for the TCC module.
* \snippet qs_tcc_dma.c setup_events
* \subsubsection asfdoc_sam0_tcc_dma_use_case_setup_flow_event Configure the Event System
* Configure the EVSYS module to wire channel 0 event to channel 1.
* -# Create an event resource instance.
* \snippet qs_tcc_dma.c capture_event_resource
* \note This should never go out of scope as long as the resource is in
* use. In most cases, this should be global.
*
* -# Create an event resource configuration struct.
* \snippet qs_tcc_dma.c event_setup_1
* -# Initialize the event resource configuration struct with default values.
* \snippet qs_tcc_dma.c event_setup_2
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
* -# Adjust the event resource configuration to desired values.
* \snippet qs_tcc_dma.c event_setup_3
* -# Allocate and configure the resource using the configuration structure.
* \snippet qs_tcc_dma.c event_setup_4
* -# Attach a user to the resource.
* \snippet qs_tcc_dma.c event_setup_5
* \subsubsection asfdoc_sam0_tcc_dma_use_case_setup_flow_dma_capture Configure the DMA for Capture TCC Channel 1
* Configure the DMAC module to obtain captured value from TCC channel 1.
* -# Create a DMA resource instance.
* \snippet qs_tcc_dma.c capture_dma_resource
* \note This should never go out of scope as long as the resource is in
* use. In most cases, this should be global.
* -# Create a DMA resource configuration struct.
* \snippet qs_tcc_dma.c dma_setup_1
* -# Initialize the DMA resource configuration struct with default values.
* \snippet qs_tcc_dma.c dma_setup_2
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
* -# Adjust the DMA resource configurations.
* \snippet qs_tcc_dma.c dma_setup_3
* -# Allocate a DMA resource with the configurations.
* \snippet qs_tcc_dma.c dma_setup_4
* -# Prepare DMA transfer descriptor.
* -# Create a DMA transfer descriptor.
* \snippet qs_tcc_dma.c capture_dma_descriptor
* \note When multiple descriptors are linked, the linked item should
* never go out of scope before it is loaded (to DMA Write-Back
* memory section). In most cases, if more than one descriptors are
* used, they should be global except the very first one.
* -# Create a DMA transfer descriptor struct.
* -# Create a DMA transfer descriptor configuration structure, which can be
* filled out to adjust the configuration of a single DMA transfer.
* \snippet qs_tcc_dma.c dma_setup_5
* -# Initialize the DMA transfer descriptor configuration struct with
* default values.
* \snippet qs_tcc_dma.c dma_setup_6
* \note This should always be performed before using the configuration
* struct to ensure that all values are initialized to known default
* settings.
* -# Adjust the DMA transfer descriptor configurations.
* \snippet qs_tcc_dma.c dma_setup_7
* -# Create the DMA transfer descriptor with the given configuration.
* \snippet qs_tcc_dma.c dma_setup_8
* -# Start DMA transfer job with prepared descriptor.
* -# Add the DMA transfer descriptor to the allocated DMA resource.
* \snippet qs_tcc_dma.c dma_setup_10
* \note When adding multiple descriptors, the last added one is linked
* at the end of descriptor queue. If ringed list is needed, just
* add the first descriptor again to build the circle.
* -# Start the DMA transfer job with the allocated DMA resource and
* transfer descriptor.
* \snippet qs_tcc_dma.c dma_setup_11
* \subsubsection asfdoc_sam0_tcc_dma_use_case_setup_flow_dma_compare Configure the DMA for Compare TCC Channel 0
* Configure the DMAC module to update TCC channel 0 compare value.
* The flow is similar to last DMA configure step for capture.
* -# Allocate and configure the DMA resource.
* \snippet qs_tcc_dma.c compare_dma_resource
* \snippet qs_tcc_dma.c config_dma_resource_for_wave
* -# Prepare DMA transfer descriptor.
* \snippet qs_tcc_dma.c compare_dma_descriptor
* \snippet qs_tcc_dma.c config_dma_descriptor_for_wave
* -# Start DMA transfer job with prepared descriptor.
* \snippet qs_tcc_dma.c config_dma_job_for_wave
* -# Enable the TCC module to start the timer and begin PWM signal generation.
* \snippet qs_tcc_dma.c setup_enable
*
* \section asfdoc_sam0_tcc_dma_use_case_main Use Case
*
* \subsection asfdoc_sam0_tcc_dma_use_case_main_code Code
* Copy-paste the following code to your user application:
* \snippet qs_tcc_dma.c main
*
* \subsection asfdoc_sam0_tcc_dma_use_case_main_flow Workflow
* -# Enter an infinite loop while the PWM wave is generated via the TCC module.
* \snippet qs_tcc_dma.c main_loop
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,8 @@
#include "PortNames.h"
#include "PeripheralNames.h"
#include "gpio_object.h"
#include "tc.h"
#include "tcc.h"
#include "adc.h"
#include "extint.h"
#include "i2c_master.h"
@ -72,6 +74,12 @@ struct analogin_s {
};
struct pwmout_s {
struct tcc_module tcc;
PinName pin;
uint32_t period;
float duty_cycle;
enum gclk_generator clock_source;
enum tc_clock_prescaler clock_prescaler;
};
struct i2c_s {

View File

@ -23,6 +23,37 @@
#include "pinmap_function.h"
struct pwm_pin_channel {
PinName pin;
PWMName pwm;
uint8_t channel_index;
};
static struct pwm_pin_channel pwn_pins[] = {
{PA00, PWM_2, 0},
{PA01, PWM_2, 1},
{PA04, PWM_0, 0},
{PA05, PWM_0, 1},
{PA06, PWM_1, 0},
{PA07, PWM_1, 1},
{PA08, PWM_0, 0},
{PA09, PWM_0, 1},
{PA12, PWM_2, 0},
{PA13, PWM_2, 1},
{PA16, PWM_2, 0},
{PA17, PWM_2, 1},
{PA18, PWM_0, 2},
{PA19, PWM_0, 3},
{PA22, PWM_0, 4},
{PA23, PWM_0, 5},
{PA24, PWM_1, 2},
{PA25, PWM_1, 3},
{PA30, PWM_1, 0},
{PA31, PWM_1, 1},
{NC, NC, NC}
};
static uint32_t pinmap_merge_pins(uint32_t a, uint32_t b)
{
// both are the same (inc both NC)
@ -218,4 +249,23 @@ uint32_t pinmap_peripheral_sercom(PinName pin, uint32_t sercom_index)
return NC;
}
return sercom_address[(sercom_index&0x0F)];
}
}
/** Find the channel index of a pin specific to a PWM instance
*
* @param[in] pin pin name
* @param[in] pwm pwm peripheral (unused now)
* @return Channel index of the specified pin
*/
uint32_t pinmap_channel_pwm(PinName pin, PWMName pwm)
{
struct pwm_pin_channel *pwm_ch = pwn_pins;
while (pwm_ch->pin != NC) {
if (pin == pwm_ch->pin) {
return (uint32_t)pwm_ch->channel_index;
}
pwm_ch++;
}
return NC;
}

View File

@ -75,6 +75,14 @@ uint32_t pinmap_pad_sercom(PinName pin, uint32_t sercom_index);
*/
uint32_t pinmap_peripheral_sercom(PinName pin, uint32_t sercom_index);
/** Find the channel index of a pin specific to a PWM instance
*
* @param[in] pin pin name
* @param[in] pwm pwm peripheral (unused now)
* @return Channel index of the specified pin
*/
uint32_t pinmap_channel_pwm(PinName pin, PWMName pwm);
#ifdef __cplusplus
}
#endif

View File

@ -20,43 +20,195 @@
#include "pinmap.h"
#include "PeripheralPins.h"
#include "pinmap_function.h"
#define PWMOUT_CTRL_CHANNEL 3
const uint32_t tcc_prescaler[] = {
TCC_CLOCK_PRESCALER_DIV1,
TCC_CLOCK_PRESCALER_DIV2,
TCC_CLOCK_PRESCALER_DIV4,
TCC_CLOCK_PRESCALER_DIV8,
TCC_CLOCK_PRESCALER_DIV16,
TCC_CLOCK_PRESCALER_DIV64,
TCC_CLOCK_PRESCALER_DIV256,
TCC_CLOCK_PRESCALER_DIV1024
};
extern const uint32_t _tcc_maxs[TCC_INST_NUM];
static void pwmout_set_period(pwmout_t* obj, int period_us)
{
uint32_t i;
uint32_t freq_hz;
double us_per_cycle;
uint64_t max_period = 0;
uint32_t us_period = period_us;
/* Sanity check arguments */
MBED_ASSERT(obj);
/* TCC instance index */
uint8_t module_index = _tcc_get_inst_index(obj->tcc.hw);
uint32_t count_max = _tcc_maxs[module_index];
freq_hz = system_gclk_gen_get_hz(obj->clock_source);
for (i=0; i<sizeof(tcc_prescaler); i++) {
us_per_cycle = 1000000.00 / (freq_hz >> tcc_prescaler[i]);
max_period = us_per_cycle * count_max;
if (max_period >= us_period) {
obj->clock_prescaler = tcc_prescaler[i];
obj->period = us_period / us_per_cycle;
break;
}
}
}
void pwmout_init_hw(pwmout_t* obj)
{
uint32_t mux_func = NC;
uint32_t pwm = NC;
PinName pin;
uint32_t ch_index = NC;
struct tcc_config config_tcc;
/* Sanity check arguments */
MBED_ASSERT(obj);
pin = obj->pin;
pwm = pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (uint32_t)NC) return; /* Pin not supported */
mux_func = pinmap_function(pin, PinMap_PWM);
ch_index = pinmap_channel_pwm(pin, pwm);
if ((mux_func == (uint32_t)NC) || (ch_index == (uint32_t)NC)) {
/* Pin not supported */
return;
}
tcc_get_config_defaults(&config_tcc, (Tcc*)pwm);
config_tcc.counter.clock_source = obj->clock_source;
config_tcc.counter.clock_prescaler = obj->clock_prescaler;
config_tcc.counter.period = obj->period;
config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
config_tcc.compare.match[PWMOUT_CTRL_CHANNEL] = obj->period * obj->duty_cycle;
config_tcc.pins.enable_wave_out_pin[ch_index] = true;
config_tcc.pins.wave_out_pin[ch_index] = pin;
config_tcc.pins.wave_out_pin_mux[ch_index] = mux_func;
tcc_init(&obj->tcc, (Tcc*)pwm, &config_tcc);
}
void pwmout_init(pwmout_t* obj, PinName pin)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
obj->pin = pin;
obj->period = 0xFFFF;
obj->duty_cycle = 1;
obj->clock_source = GCLK_GENERATOR_0; /* 8Mhz input clock */
obj->clock_prescaler = TCC_CLOCK_PRESCALER_DIV8; /* Default to 1MHz for 8Mhz input clock */
pwmout_init_hw(obj);
tcc_enable(&obj->tcc);
}
void pwmout_free(pwmout_t* obj)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
tcc_disable(&obj->tcc);
}
void pwmout_write(pwmout_t* obj, float value)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
if (value < 0.0f) {
value = 0;
} else if (value > 1.0f) {
value = 1;
}
/* Modify the pulse width keeping period same */
obj->duty_cycle = value;
/* Disable PWM Module */
tcc_disable(&obj->tcc);
/* Update the changes */
pwmout_init_hw(obj);
/* Enable PWM Module */
tcc_enable(&obj->tcc);
}
float pwmout_read(pwmout_t* obj)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
return obj->duty_cycle;
}
void pwmout_period(pwmout_t* obj, float seconds)
{
pwmout_period_us(obj, seconds * 1000000.0f);
}
void pwmout_period_ms(pwmout_t* obj, int ms)
{
pwmout_period_us(obj, ms * 1000);
}
void pwmout_period_us(pwmout_t* obj, int us)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
/* Disable PWM Module */
tcc_disable(&obj->tcc);
/* TODO: Find and set the period */
pwmout_set_period(obj, us);
/* Update the changes */
pwmout_init_hw(obj);
/* Enable PWM Module */
tcc_enable(&obj->tcc);
}
void pwmout_pulsewidth(pwmout_t* obj, float seconds)
{
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
}
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
{
pwmout_pulsewidth_us(obj, ms * 1000);
}
void pwmout_pulsewidth_us(pwmout_t* obj, int us)
{
/* Sanity check arguments */
MBED_ASSERT(obj);
/* Find the new duty cycle */
double duty_cycle = us / (double)obj->period;
/* This call updates pulse width as well as period */
pwmout_write(obj, duty_cycle);
}