这个是利用DMA把TC的定时值,以DMA方式存到数组里。
这个也是根根快速指导里的说明一步一步地形成程序,只不过别忘加个DMA模块:
在形成程序时,别忘加上图中的这行:(我就忘加了,结果找了一会儿)
#define M2M_DMAC_TRIGGER_ID TC0_DMAC_ID_MC_0
程序中有两个很怪的地方:
COMPILER_ALIGNED(16)
这个是内联的定义
UNUSED(resource);
就是个无返回值函数的定义。
这两个都可通过,右键查出
以下是运行结果:
以下是程序:
#include <asf.h>
#define PWM_MODULE EXT2_PWM_MODULE
#define PWM_OUT_PIN EXT2_PWM_0_PIN
#define PWM_OUT_MUX EXT2_PWM_0_MUX
#define M2M_DMAC_TRIGGER_ID TC0_DMAC_ID_MC_0
struct tc_module tc_instance;
struct dma_resource example_resource;
#define TRANSFER_SIZE (16)
#define TRANSFER_COUNTER (32)
static uint8_t source_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
static uint8_t destination_memory[TRANSFER_SIZE*TRANSFER_COUNTER];
static volatile bool transfer_is_done = false;
COMPILER_ALIGNED(16)
DmacDescriptor example_descriptor;
void configure_tc(void);
void transfer_done(struct dma_resource* const resource );
void configure_dma_resource(struct dma_resource *resource);
void setup_dma_descriptor(DmacDescriptor *descriptor);
void configure_tc(void)
{
struct tc_config config_tc;
tc_get_config_defaults(&config_tc);
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
config_tc.counter_16_bit.compare_capture_channel[0] = (0xFFFF / 4);
config_tc.pwm_channel[0].enabled = true;
config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
tc_init(&tc_instance, PWM_MODULE, &config_tc);
tc_enable(&tc_instance);
}
void transfer_done(struct dma_resource* const resource )
{
UNUSED(resource);
transfer_is_done = true;}
void configure_dma_resource(struct dma_resource *resource)
{ struct dma_resource_config config;
dma_get_config_defaults(&config);
config.peripheral_trigger = M2M_DMAC_TRIGGER_ID;
dma_allocate(resource, &config);}
void setup_dma_descriptor(DmacDescriptor *descriptor)
{ struct dma_descriptor_config descriptor_config;
dma_descriptor_get_config_defaults(&descriptor_config);
descriptor_config.block_transfer_count = TRANSFER_SIZE;
descriptor_config.source_address = (uint32_t)source_memory + TRANSFER_SIZE;
descriptor_config.destination_address = (uint32_t)destination_memory + TRANSFER_SIZE;
dma_descriptor_create(descriptor, &descriptor_config);}
int main (void)
{
uint32_t i;
system_init();
configure_tc();
for(i=0;i<TRANSFER_COUNTER;i++)
{ transfer_is_done = false;
dma_start_transfer_job(&example_resource);
while (!transfer_is_done)
{ /* Wait for transfer done */ }
example_descriptor.SRCADDR.reg += TRANSFER_SIZE;
example_descriptor.DSTADDR.reg += TRANSFER_SIZE; }
while(1);
}
|