|
5 | 5 | #include "gpio.h" |
6 | 6 |
|
7 | 7 |
|
8 | | -void setLed(LedPin led){ |
9 | | - clearPin(LED_PORT, led ^ (LED_RED|LED_GREEN)); |
10 | | - setPin(LED_PORT, led); |
| 8 | +void initializeTimer(){ |
| 9 | + uint16_t prescaler = (int16_t)(SystemCoreClock / 1000000) - 1; // 1Mhz clock |
| 10 | + uint16_t period = 1000000 / 20000; // 20 KHz for 1MHz prescaled |
| 11 | + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
| 12 | + TIM_TimeBaseInitTypeDef init = { |
| 13 | + .TIM_Prescaler = prescaler, |
| 14 | + .TIM_CounterMode = TIM_CounterMode_Up, |
| 15 | + .TIM_Period = period, |
| 16 | + .TIM_ClockDivision = TIM_CKD_DIV1, |
| 17 | + .TIM_RepetitionCounter = 0 |
| 18 | + }; |
| 19 | + TIM_TimeBaseInit(TIM3, &init); |
| 20 | + TIM_Cmd(TIM3, ENABLE); |
| 21 | +} |
| 22 | + // TIM3_CH1 PA6 no remap |
| 23 | + |
| 24 | +void setPWM(uint16_t pulse){ |
| 25 | + TIM_OCInitTypeDef outputChannelInit = {0,}; |
| 26 | + outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1; |
| 27 | + outputChannelInit.TIM_Pulse = pulse; |
| 28 | + outputChannelInit.TIM_OutputState = TIM_OutputState_Enable; |
| 29 | + outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High; |
| 30 | + TIM_OC1Init(TIM3, &outputChannelInit); |
| 31 | + TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); |
| 32 | +} |
| 33 | + |
| 34 | +void initializeLED(){ |
| 35 | + // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); |
| 36 | + GPIO_InitTypeDef gpioStructure; |
| 37 | + gpioStructure.GPIO_Pin = GPIO_Pin_6; |
| 38 | + gpioStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
| 39 | + gpioStructure.GPIO_Speed = GPIO_Speed_2MHz; |
| 40 | + GPIO_Init(GPIOA, &gpioStructure); |
| 41 | + // GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3); |
11 | 42 | } |
12 | 43 |
|
13 | | -void toggleLed(){ |
14 | | - togglePin(LED_PORT, LED_RED|LED_GREEN); |
| 44 | +void setLed(uint16_t brightness){ |
| 45 | + //void setLed(LedPin led){ |
| 46 | + setPWM(brightness); |
| 47 | + /* clearPin(LED_PORT, led ^ (LED_RED|LED_GREEN)); */ |
| 48 | + /* setPin(LED_PORT, led); */ |
15 | 49 | } |
16 | 50 |
|
| 51 | +/* void toggleLed(){ */ |
| 52 | +/* togglePin(LED_PORT, LED_RED|LED_GREEN); */ |
| 53 | +/* } */ |
| 54 | + |
17 | 55 | char* getFirmwareVersion(){ |
18 | 56 | return HARDWARE_VERSION "-" FIRMWARE_VERSION ; |
19 | 57 | } |
@@ -58,18 +96,21 @@ void dfu_reboot(void){ |
58 | 96 | while(1); |
59 | 97 | } |
60 | 98 |
|
61 | | -LedPin getLed(){ |
62 | | - if(getPin(LED_PORT, LED_GREEN)) |
63 | | - return GREEN; |
64 | | - else if(getPin(LED_PORT, LED_RED)) |
65 | | - return RED; |
66 | | - else |
67 | | - return NONE; |
68 | | -} |
| 99 | +/* LedPin getLed(){ */ |
| 100 | +/* if(getPin(LED_PORT, LED_GREEN)) */ |
| 101 | +/* return GREEN; */ |
| 102 | +/* else if(getPin(LED_PORT, LED_RED)) */ |
| 103 | +/* return RED; */ |
| 104 | +/* else */ |
| 105 | +/* return NONE; */ |
| 106 | +/* } */ |
69 | 107 |
|
70 | 108 | void ledSetup(){ |
71 | 109 | configureDigitalOutput(LED_PORT, LED_RED|LED_GREEN); |
72 | 110 | clearPin(LED_PORT, LED_RED|LED_GREEN); |
| 111 | + initializeLED(); |
| 112 | + initializeTimer(); |
| 113 | + setPWM(0); // pw: 1 to 50 |
73 | 114 | } |
74 | 115 |
|
75 | 116 | bool isPushButtonPressed(){ |
@@ -385,7 +426,7 @@ void dacSetup(){ |
385 | 426 | /* /\* Set DAC Channel2 data *\/ */ |
386 | 427 | /* DAC_SetChannel2Data(DAC_Align_12b_L, 0x7FF0); */ |
387 | 428 |
|
388 | | - /* /\* Start DAC Channel1 conversion by software *\/ */ |
| 429 | + /* Start DAC Channel1 conversion by software */ |
389 | 430 | DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); |
390 | 431 | DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); |
391 | 432 | } |
|
0 commit comments