aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPiotr Esden-Tempski2011-01-27 21:30:07 -0800
committerPiotr Esden-Tempski2011-01-27 22:18:34 -0800
commit5975750e5e22ffb047303a1e1e7700012e09e18c (patch)
treeacda082e99bd8c023291d70e1e0a81710aae9549 /examples
parentd608049563aa5cf71b100ceb77eb432aa0d4e35f (diff)
Added output compare channel convenience functions and adapted the pwm_6step example accordingly.
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32/mb525/pwmleds/pwmleds.c102
-rw-r--r--examples/stm32/stm32-h103/pwm_6step/pwm_6step.c239
2 files changed, 118 insertions, 223 deletions
diff --git a/examples/stm32/mb525/pwmleds/pwmleds.c b/examples/stm32/mb525/pwmleds/pwmleds.c
index db7c9ca..393928f 100644
--- a/examples/stm32/mb525/pwmleds/pwmleds.c
+++ b/examples/stm32/mb525/pwmleds/pwmleds.c
@@ -240,12 +240,12 @@ void clock_setup(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
- /* Enable TIM3 clock. */
- rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM3EN);
+ /* Enable TIM1 clock. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN);
/* Enable GPIOC, Alternate Function clocks. */
rcc_peripheral_enable_clock(&RCC_APB2ENR,
- RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN);
+ RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN);
}
void gpio_setup(void)
@@ -254,81 +254,95 @@ void gpio_setup(void)
* Set GPIO6 (in GPIO port C) to
* 'output alternate function push-pull'.
*/
- gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
- GPIO_TIM3_FR_CH1 |
- GPIO_TIM3_FR_CH2 |
- GPIO_TIM3_FR_CH3 |
- GPIO_TIM3_FR_CH4);
+ GPIO_TIM1_CH1 |
+ GPIO_TIM1_CH2 |
+ GPIO_TIM1_CH3 |
+ GPIO_TIM1_CH4);
- /* Remap TIM3:
+ /* Remap TIM1:
* CH1 -> PC6
* CH2 -> PC7
* CH3 -> PC8
* CH4 -> PC9
*/
- AFIO_MAPR |= AFIO_MAPR_TIM3_REMAP_FULL_REMAP;
+ //AFIO_MAPR |= AFIO_MAPR_TIM3_REMAP_FULL_REMAP;
}
void tim_setup(void)
{
+#if 0
+ TIM1_CR1 = TIM_CR1_CMS_CENTER_1 | TIM_CR1_ARPE;
+ TIM1_CCMR1 = TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE | TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE;
+ TIM1_CCMR2 = TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE;
+
+ TIM1_CCER &= ~TIM_CCER_CC1P;
+#endif
+#if 1
+ TIM1_SMCR &= ~TIM_SMCR_SMS_MASK;
+ TIM1_CR1 &= ~TIM_CR1_CEN;
+
/* Clock division and mode */
- TIM3_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE;
+ TIM1_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE;
/* Period */
- TIM3_ARR = 65535;
+ TIM1_ARR = 65535;
/* Prescaler */
- TIM3_PSC = 0;
- TIM3_EGR = TIM_EGR_UG;
+ TIM1_PSC = 2;
+ TIM1_EGR = TIM_EGR_UG;
/* ---- */
/* Output compare 1 mode and preload */
- TIM3_CCMR1 |= TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE;
+ TIM1_CCMR1 |= TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE;
/* Polarity and state */
- // TIM3_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
- TIM3_CCER |= TIM_CCER_CC1E;
+ // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
+ TIM1_CCER |= TIM_CCER_CC1E;
/* Capture compare value */
- TIM3_CCR1 = 0;
+ TIM1_CCR1 = 1000;
/* ---- */
/* Output compare 2 mode and preload */
- TIM3_CCMR1 |= TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE;
+ TIM1_CCMR1 |= TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE;
/* Polarity and state */
- // TIM3_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
- TIM3_CCER |= TIM_CCER_CC2E;
+ // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
+ TIM1_CCER |= TIM_CCER_CC2E;
/* Capture compare value */
- TIM3_CCR2 = 0;
+ TIM1_CCR2 = 1000;
/* ---- */
/* Output compare 3 mode and preload */
- TIM3_CCMR2 |= TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE;
+ TIM1_CCMR2 |= TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE;
/* Polarity and state */
- // TIM3_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
- TIM3_CCER |= TIM_CCER_CC3E;
+ // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
+ TIM1_CCER |= TIM_CCER_CC3E;
/* Capture compare value */
- TIM3_CCR3 = 0;
+ TIM1_CCR3 = 1000;
/* ---- */
/* Output compare 4 mode and preload */
- TIM3_CCMR2 |= TIM_CCMR2_OC4M_PWM1 | TIM_CCMR2_OC4PE;
+ TIM1_CCMR2 |= TIM_CCMR2_OC4M_PWM1 | TIM_CCMR2_OC4PE;
/* Polarity and state */
- // TIM3_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
- TIM3_CCER |= TIM_CCER_CC4E;
+ // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E;
+ TIM1_CCER |= TIM_CCER_CC4E;
/* Capture compare value */
- TIM3_CCR4 = 0;
+ TIM1_CCR4 = 1000;
/* ---- */
/* ARR reload enable */
- TIM3_CR1 |= TIM_CR1_ARPE;
+ TIM1_CR1 |= TIM_CR1_ARPE;
+
+ TIM1_BDTR |= TIM_BDTR_MOE;
/* Counter enable */
- TIM3_CR1 |= TIM_CR1_CEN;
+ TIM1_CR1 |= TIM_CR1_CEN;
+#endif
}
int main(void)
@@ -349,25 +363,25 @@ int main(void)
j3 = 0;
d3 = 1;
while (1) {
- TIM3_CCR1 = gamma_table_linear[j0];
+ TIM1_CCR1 = gamma_table_linear[j0];
j0 += d0;
if (j0 == 255)
d0 =- 1;
if (j0 == 0)
d0 = 1;
- TIM3_CCR2 = gamma_table_1_3[j1];
+ TIM1_CCR2 = gamma_table_1_3[j1];
j1 += d1;
if (j1 == 255)
d1 =- 1;
if (j1 == 0)
d1 = 1;
- TIM3_CCR3 = gamma_table_2_5[j2];
+ TIM1_CCR3 = gamma_table_2_5[j2];
j2 += d2;
if (j2 == 255)
d2 =- 1;
if (j2 == 0)
d2 = 1;
- TIM3_CCR4= gamma_table_3_0[j3];
+ TIM1_CCR4= gamma_table_3_0[j3];
j3 += d3;
if (j3 == 255)
d3 =- 1;
@@ -387,25 +401,25 @@ int main(void)
j3 = 128;
d3 = -1;
while (1) {
- TIM3_CCR1 = GAMMA_TABLE[j0];
+ TIM1_CCR1 = GAMMA_TABLE[j0];
j0 += d0;
if (j0 == 255)
d0 =- 1;
if (j0 == 0)
d0 = 1;
- TIM3_CCR2 = GAMMA_TABLE[j1];
+ TIM1_CCR2 = GAMMA_TABLE[j1];
j1 += d1;
if (j1 == 255)
d1 =- 1;
if (j1 == 0)
d1 = 1;
- TIM3_CCR3 = GAMMA_TABLE[j2];
+ TIM1_CCR3 = GAMMA_TABLE[j2];
j2 += d2;
if (j2 == 255)
d2 =- 1;
if (j2 == 0)
d2 = 1;
- TIM3_CCR4 = GAMMA_TABLE[j3];
+ TIM1_CCR4 = GAMMA_TABLE[j3];
j3 += d3;
if (j3 == 255)
d3 =- 1;
@@ -428,25 +442,25 @@ int main(void)
k = 0;
kd = 1;
while (1) {
- TIM3_CCR1 = GAMMA_TABLE[j0];
+ TIM1_CCR1 = GAMMA_TABLE[j0];
j0 += d0;
if (j0 == 255)
d0 =- 1;
if (j0 == 19)
j0 = 20;
- TIM3_CCR2 = GAMMA_TABLE[j1];
+ TIM1_CCR2 = GAMMA_TABLE[j1];
j1 += d1;
if (j1 == 255)
d1 =- 1;
if (j1 == 19)
j1 = 20;
- TIM3_CCR3 = GAMMA_TABLE[j2];
+ TIM1_CCR3 = GAMMA_TABLE[j2];
j2 += d2;
if (j2 == 255)
d2 =- 1;
if (j2 == 19)
j2 = 20;
- TIM3_CCR4 = GAMMA_TABLE[j3];
+ TIM1_CCR4 = GAMMA_TABLE[j3];
j3 += d3;
if (j3 == 255)
d3 =- 1;
diff --git a/examples/stm32/stm32-h103/pwm_6step/pwm_6step.c b/examples/stm32/stm32-h103/pwm_6step/pwm_6step.c
index af26c5a..b35a4ba 100644
--- a/examples/stm32/stm32-h103/pwm_6step/pwm_6step.c
+++ b/examples/stm32/stm32-h103/pwm_6step/pwm_6step.c
@@ -87,222 +87,103 @@ void tim_setup(void)
/* Enable preload. */
timer_enable_preload(TIM1);
- //timer_disable_preload(TIM1);
/* Continous mode. */
timer_continuous_mode(TIM1);
/* Period (32kHz) */
- TIM1_ARR = 72000000 / 32000;
+ timer_set_period(TIM1, 72000000 / 32000);
/* -- OC1 and OC1N configuration -- */
- {
- u16 tmp_ccmr1 = 0, tmp_ccer = 0, tmp_cr2 = 0;
- /** Disable OC1. **/
- TIM1_CCER &= ~TIM_CCER_CC1E;
+ /* Disable outputs. */
+ timer_disable_oc_output(TIM1, TIM_OC1);
+ timer_disable_oc_output(TIM1, TIM_OC1N);
- /** Disable OC1N. **/
- TIM1_CCER &= ~TIM_CCER_CC1NE;
+ /* Configure global mode of line 1. */
+ timer_disable_oc_clear(TIM1, TIM_OC1);
+ timer_enable_oc_preload(TIM1, TIM_OC1);
+ timer_set_oc_slow_mode(TIM1, TIM_OC1);
+ timer_set_oc_mode(TIM1, TIM_OC1, TIM_CCMR1_OC1M_PWM1);
- /** Get registers. **/
- tmp_ccmr1 = TIM1_CCMR1;
- tmp_ccer = TIM1_CCER;
- tmp_cr2 = TIM1_CR2;
+ /* Configure OC1. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC1);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC1);
- /** Configure global mode of line 1 **/
+ /* Configure OC1N. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC1N);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC1N);
- /* Disable OC1 clear. (esden: What is that?) */
- tmp_ccmr1 &= ~TIM_CCMR1_OC1CE;
+ /* Set the capture compare value for OC1. */
+ timer_set_oc_value(TIM1, TIM_OC1, 100);
- /* Set CC1 to output mode. */
- tmp_ccmr1 &= ~TIM_CCMR1_CC1S_MASK;
- tmp_ccmr1 |= TIM_CCMR1_CC1S_OUT;
-
- /* Enable OC1 preload enable. */
- //tmp_ccmr1 |= TIM_CCMR1_OC1PE;
- tmp_ccmr1 &= ~TIM_CCMR1_OC1PE;
-
- /* Disable OC1 fast mode. */
- tmp_ccmr1 &= ~TIM_CCMR1_OC1FE;
-
- /* Set OC1 mode to PWM1. */
- tmp_ccmr1 &= ~TIM_CCMR1_OC1M_MASK;
- tmp_ccmr1 |= TIM_CCMR1_OC1M_PWM1;
-
- /** Configure OC1. **/
-
- /* Set output polarity level, high. */
- tmp_ccer &= ~TIM_CCER_CC1P;
-
- /* Enable OC1 output */
- tmp_ccer |= TIM_CCER_CC1E;
-
- /* Set OC1 idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS1;
-
- /** Configure OC1N. **/
-
- /* Set output polarity level, high. (TIM1 and TIM8 only) */
- tmp_ccer &= ~TIM_CCER_CC1NP;
-
- /* Enable OC1N output. (TIM1 and TIM8 only) */
- tmp_ccer |= TIM_CCER_CC1NE;
-
- /* Set OC1N idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS1N;
-
- /** Set the capture compare value for OC1 **/
- TIM1_CCR1 = 100;
-
- /** Write register values **/
- TIM1_CR2 = tmp_cr2;
- TIM1_CCMR1 = tmp_ccmr1;
- TIM1_CCER = tmp_ccer;
-
- }
+ /* Reenable outputs. */
+ timer_enable_oc_output(TIM1, TIM_OC1);
+ timer_enable_oc_output(TIM1, TIM_OC1N);
/* -- OC2 and OC2N configuration -- */
- {
- u16 tmp_ccmr1 = 0, tmp_ccer = 0, tmp_cr2 = 0;
-
- /** Disable OC2. **/
- TIM1_CCER &= ~TIM_CCER_CC2E;
-
- /** Disable OC2N. **/
- TIM1_CCER &= ~TIM_CCER_CC2NE;
-
- /** Get registers. **/
- tmp_ccmr1 = TIM1_CCMR1;
- tmp_ccer = TIM1_CCER;
- tmp_cr2 = TIM1_CR2;
-
- /** Configure global mode of line 1 **/
-
- /* Disable OC2 clear. (esden: What is that?) */
- tmp_ccmr1 &= ~TIM_CCMR1_OC2CE;
-
- /* Set CC2 to output mode. */
- tmp_ccmr1 &= ~TIM_CCMR1_CC2S_MASK;
- tmp_ccmr1 |= TIM_CCMR1_CC2S_OUT;
-
- /* Enable OC2 preload enable. */
- tmp_ccmr1 |= TIM_CCMR1_OC2PE;
-
- /* Disable OC2 fast mode. */
- tmp_ccmr1 &= ~TIM_CCMR1_OC2FE;
- /* Set OC2 mode to PWM1. */
- tmp_ccmr1 &= ~TIM_CCMR1_OC2M_MASK;
- tmp_ccmr1 |= TIM_CCMR1_OC2M_PWM1;
+ /* Disable outputs. */
+ timer_disable_oc_output(TIM1, TIM_OC2);
+ timer_disable_oc_output(TIM1, TIM_OC2N);
- /** Configure OC2. **/
+ /* Configure global mode of line 2. */
+ timer_disable_oc_clear(TIM1, TIM_OC2);
+ timer_enable_oc_preload(TIM1, TIM_OC2);
+ timer_set_oc_slow_mode(TIM1, TIM_OC2);
+ timer_set_oc_mode(TIM1, TIM_OC2, TIM_CCMR1_OC2M_PWM1);
- /* Set output polarity level, high. */
- tmp_ccer &= ~TIM_CCER_CC2P;
+ /* Configure OC2. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC2);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC2);
- /* Enable OC2 output */
- tmp_ccer |= TIM_CCER_CC2E;
+ /* Configure OC2N. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC2N);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC2N);
- /* Set OC2 idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS2;
+ /* Set the capture compare value for OC1. */
+ timer_set_oc_value(TIM1, TIM_OC2, 200);
- /** Configure OC2N. **/
-
- /* Set output polarity level, high. (TIM1 and TIM8 only) */
- tmp_ccer &= ~TIM_CCER_CC2NP;
-
- /* Enable OC2N output. (TIM1 and TIM8 only) */
- tmp_ccer |= TIM_CCER_CC2NE;
-
- /* Set OC2N idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS2N;
-
- /** Set the capture compare value for OC2 **/
- TIM1_CCR2 = 200;
-
- /** Write register values **/
- TIM1_CR2 = tmp_cr2;
- TIM1_CCMR1 = tmp_ccmr1;
- TIM1_CCER = tmp_ccer;
-
- }
+ /* Reenable outputs. */
+ timer_enable_oc_output(TIM1, TIM_OC2);
+ timer_enable_oc_output(TIM1, TIM_OC2N);
/* -- OC3 and OC3N configuration -- */
- {
- u16 tmp_ccmr2 = 0, tmp_ccer = 0, tmp_cr2 = 0;
-
- /** Disable OC3. **/
- TIM1_CCER &= ~TIM_CCER_CC3E;
-
- /** Disable OC3N. **/
- TIM1_CCER &= ~TIM_CCER_CC3NE;
-
- /** Get registers. **/
- tmp_ccmr2 = TIM1_CCMR2;
- tmp_ccer = TIM1_CCER;
- tmp_cr2 = TIM1_CR2;
-
- /** Configure global mode of line 1 **/
-
- /* Disable OC3 clear. (esden: What is that?) */
- tmp_ccmr2 &= ~TIM_CCMR2_OC3CE;
- /* Set CC3 to output mode. */
- tmp_ccmr2 &= ~TIM_CCMR2_CC3S_MASK;
- tmp_ccmr2 |= TIM_CCMR2_CC3S_OUT;
+ /* Disable outputs. */
+ timer_disable_oc_output(TIM1, TIM_OC3);
+ timer_disable_oc_output(TIM1, TIM_OC3N);
- /* Enable OC3 preload enable. */
- tmp_ccmr2 |= TIM_CCMR2_OC3PE;
+ /* Configure global mode of line 3. */
+ timer_disable_oc_clear(TIM1, TIM_OC3);
+ timer_enable_oc_preload(TIM1, TIM_OC3);
+ timer_set_oc_slow_mode(TIM1, TIM_OC3);
+ timer_set_oc_mode(TIM1, TIM_OC3, TIM_CCMR2_OC3M_PWM1);
- /* Disable OC3 fast mode. */
- tmp_ccmr2 &= ~TIM_CCMR2_OC3FE;
+ /* Configure OC3. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC3);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC3);
- /* Set OC3 mode to PWM1. */
- tmp_ccmr2 &= ~TIM_CCMR2_OC3M_MASK;
- tmp_ccmr2 |= TIM_CCMR2_OC3M_PWM1;
+ /* Configure OC3N. */
+ timer_set_oc_polarity_high(TIM1, TIM_OC3N);
+ timer_set_oc_idle_state_set(TIM1, TIM_OC3N);
- /** Configure OC3. **/
+ /* Set the capture compare value for OC3. */
+ timer_set_oc_value(TIM1, TIM_OC3, 300);
- /* Set output polarity level, high. */
- tmp_ccer &= ~TIM_CCER_CC3P;
-
- /* Enable OC3 output */
- tmp_ccer |= TIM_CCER_CC3E;
-
- /* Set OC3 idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS3;
-
- /** Configure OC3N. **/
-
- /* Set output polarity level, high. (TIM1 and TIM8 only) */
- tmp_ccer &= ~TIM_CCER_CC3NP;
-
- /* Enable OC3N output. (TIM1 and TIM8 only) */
- tmp_ccer |= TIM_CCER_CC3NE;
-
- /* Set OC3N idle state to "set". (TIM1 and TIM8 only) */
- tmp_cr2 |= TIM_CR2_OIS3N;
-
- /** Set the capture compare value for OC3 **/
- TIM1_CCR3 = 300;
-
- /** Write register values **/
- TIM1_CR2 = tmp_cr2;
- TIM1_CCMR2 = tmp_ccmr2;
- TIM1_CCER = tmp_ccer;
-
- }
+ /* Reenable outputs. */
+ timer_enable_oc_output(TIM1, TIM_OC3);
+ timer_enable_oc_output(TIM1, TIM_OC3N);
/* ---- */
/* ARR reload enable */
- TIM1_CR1 |= TIM_CR1_ARPE;
+ timer_enable_preload(TIM1);
/* Enable outputs in the break subsystem */
TIM1_BDTR |= TIM_BDTR_MOE;
/* Counter enable */
- TIM1_CR1 |= TIM_CR1_CEN;
+ timer_enable_counter(TIM1);
}
int main(void)