Created
October 26, 2017 14:59
-
-
Save arpruss/7fca16ee6b426749735d27f7f611d51f to your computer and use it in GitHub Desktop.
Arduino stm32f1 code to do PWM with DMA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dma_private.h> | |
#define PIN_TO_PULSE PB12 | |
#define DMA_PWM_MAX_DUTY 32 | |
uint32_t data[DMA_PWM_MAX_DUTY]; | |
void setDMAPWM(uint32_t pin, uint32_t duty_cycle) { | |
gpio_dev *dev = digitalPinToPort(pin); | |
uint32_t maskOn = digitalPinToBitMask(pin); | |
uint32_t maskOff = digitalPinToBitMask(pin) << 16; | |
if (duty_cycle == 0 || duty_cycle >= DMA_PWM_MAX_DUTY) { | |
dma_disable(DMA1, DMA_CH1); | |
gpio_write_bit(dev, PIN_MAP[pin].gpio_bit, duty_cycle != 0); | |
return; | |
} | |
for (uint32_t i=0; i<duty_cycle; i++) | |
data[i] = maskOn; | |
for (uint32_t i=duty_cycle; i<DMA_PWM_MAX_DUTY; i++) | |
data[i] = maskOff; | |
dma_init(DMA1); | |
dma_setup_transfer(DMA1, DMA_CH1, data, DMA_SIZE_32BITS, &(dev->regs->BSRR), | |
DMA_SIZE_32BITS, DMA_PINC_MODE | DMA_MEM_2_MEM | DMA_CIRC_MODE); | |
dma_set_num_transfers(DMA1, DMA_CH1, DMA_PWM_MAX_DUTY); | |
dma_enable(DMA1, DMA_CH1); | |
} | |
void setup() | |
{ | |
pinMode(PIN_TO_PULSE, OUTPUT); | |
setDMAPWM(PIN_TO_PULSE, 1); | |
} | |
void loop() | |
{ | |
for(int i=0;i<DMA_PWM_MAX_DUTY;i++) { | |
setDMAPWM(PIN_TO_PULSE, i); | |
delay(50); | |
} | |
for(int i=0;i<DMA_PWM_MAX_DUTY;i++) { | |
setDMAPWM(PIN_TO_PULSE, DMA_PWM_MAX_DUTY-1-i); | |
delay(50); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment