Skip to content

Commit 3981a74

Browse files
author
mars
committed
updated phase handling and pulse width propagation, added PWM LED output
1 parent b86eca3 commit 3981a74

6 files changed

Lines changed: 114 additions & 54 deletions

File tree

firmware/Build/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

firmware/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
TEMPLATEROOT = .
22

3-
# CFLAGS = -g -Wall -Wcpp -DUSE_FULL_ASSERT
4-
CFLAGS = -O2 -Wall -Wcpp -DUSE_FULL_ASSERT
3+
CFLAGS = -g -Wall -Wcpp -DUSE_FULL_ASSERT
4+
# CFLAGS = -O2 -Wall -Wcpp -DUSE_FULL_ASSERT
55
CXXFLAGS = -fno-rtti -fno-exceptions -std=c++11 $(CFLAGS)
66
# LDSCRIPT = Source/flash.ld
77

firmware/Source/DDS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#define DDS_SINE_ZERO_VALUE 2094
2222
#define DDS_RAMP_ZERO_VALUE 3900
2323

24-
#define DDS_MAX_RAMP_VALUE 3900
25-
#define DDS_MIN_RAMP_VALUE 810
24+
#define DDS_MAX_RAMP_VALUE 3700
25+
#define DDS_MIN_RAMP_VALUE 610
2626
#define DDS_RAMP_RANGE (DDS_MAX_RAMP_VALUE-DDS_MIN_RAMP_VALUE)
2727

2828
class DDS {

firmware/Source/TapTempo.cpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Synchroniser {
5858
int32_t period;
5959
bool isHigh;
6060
SynchroniserMode mode;
61+
public:
6162
uint16_t speed;
6263
public:
6364
Synchroniser() : trig(TRIGGER_LIMIT), period(0),
@@ -107,32 +108,42 @@ class Synchroniser {
107108
class TapTempo {
108109
private:
109110
int32_t counter;
110-
int32_t limit;
111+
int32_t goLow;
112+
int32_t goHigh;
111113
int32_t trig;
112114
bool isHigh;
113115
bool on;
114-
uint16_t speed;
115116
public:
116-
TapTempo() : counter(0), limit(TRIGGER_LIMIT/2), trig(TRIGGER_LIMIT),
117-
isHigh(false), on(false), speed(4095) {}
117+
uint16_t speed;
118+
TapTempo() : counter(0), goLow(TRIGGER_LIMIT>>2), goHigh(TRIGGER_LIMIT>>2),
119+
trig(TRIGGER_LIMIT), isHigh(false), on(false), speed(4095) {}
118120
void reset(){
119121
counter = 0;
120-
low();
122+
setLow();
121123
}
122-
void trigger(){
124+
void trigger(bool high){
123125
if(trig < TAP_THRESHOLD)
124126
return;
125127
if(trig < TRIGGER_LIMIT){
126-
high();
127-
limit = trig>>1; // toggle at period divided by 2
128-
counter = 0;
128+
if(high){
129+
setHigh();
130+
goHigh = trig;
131+
counter = 0;
132+
trig = 0;
133+
}else{
134+
setLow();
135+
goLow = trig;
136+
}
137+
}else if(high){
138+
trig = 0;
129139
}
130-
trig = 0;
131140
}
132141
void setSpeed(int16_t s){
133142
if(abs(speed-s) > 16){
134-
int64_t delta = (int64_t)limit*(speed-s)/2048;
135-
limit = max(1, limit+delta);
143+
int64_t delta = (int64_t)goLow*(speed-s)/2048;
144+
goLow = max(1, goLow+delta);
145+
delta = (int64_t)goHigh*(speed-s)/2048;
146+
goHigh = max(1, goHigh+delta);
136147
speed = s;
137148
}
138149
}
@@ -144,26 +155,32 @@ class TapTempo {
144155
void clock(){
145156
if(trig < TRIGGER_LIMIT)
146157
trig++;
147-
if(on && ++counter > limit){
158+
if(++counter >= goHigh)
148159
counter = 0;
149-
toggle();
160+
if(on){
161+
if(counter == 0)
162+
setHigh();
163+
else if(counter >= goLow && isHigh)
164+
setLow();
165+
else
166+
setLed(LED_FULL*(goHigh-counter)/goHigh);
150167
}
151168
}
152-
void low(){
169+
void setLow(){
153170
isHigh = false;
154171
setPin(TRIGGER_OUTPUT_PORT, TRIGGER_OUTPUT_PIN);
155-
setLed(NONE);
172+
setLed(0);
156173
}
157-
void high(){
174+
void setHigh(){
158175
isHigh = true;
159176
clearPin(TRIGGER_OUTPUT_PORT, TRIGGER_OUTPUT_PIN);
160-
setLed(RED);
177+
setLed(LED_FULL);
161178
}
162179
void toggle(){
163180
if(isHigh)
164-
low();
181+
setLow();
165182
else
166-
high();
183+
setHigh();
167184
}
168185
};
169186

@@ -172,12 +189,7 @@ TapTempo tempo;
172189

173190
// todo: proper debouncing with systick counter
174191
void buttonCallback(){
175-
if(isPushButtonPressed()){
176-
tempo.trigger();
177-
// toggleLed();
178-
}else{
179-
tempo.low();
180-
}
192+
tempo.trigger(isPushButtonPressed());
181193
}
182194

183195
void triggerCallback(){
@@ -226,14 +238,18 @@ void setup(){
226238
configureDigitalOutput(GPIOB, GPIO_Pin_10); // debug
227239
#endif
228240
ledSetup();
229-
setLed(RED);
241+
setLed(0);
230242
adcSetup();
231243
dacSetup();
232244
triggerInputSetup(triggerCallback);
233245
pushButtonSetup(buttonCallback);
234246
timerSetup(TIMER_PERIOD, timerCallback);
247+
updateSpeed();
248+
synchro.speed = getAnalogValue(0);
249+
tempo.speed = getAnalogValue(0);
235250
}
236251

252+
237253
void run(){
238254
for(;;){
239255
updateMode();

firmware/Source/periph.c

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,53 @@
55
#include "gpio.h"
66

77

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);
1142
}
1243

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); */
1549
}
1650

51+
/* void toggleLed(){ */
52+
/* togglePin(LED_PORT, LED_RED|LED_GREEN); */
53+
/* } */
54+
1755
char* getFirmwareVersion(){
1856
return HARDWARE_VERSION "-" FIRMWARE_VERSION ;
1957
}
@@ -58,18 +96,21 @@ void dfu_reboot(void){
5896
while(1);
5997
}
6098

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+
/* } */
69107

70108
void ledSetup(){
71109
configureDigitalOutput(LED_PORT, LED_RED|LED_GREEN);
72110
clearPin(LED_PORT, LED_RED|LED_GREEN);
111+
initializeLED();
112+
initializeTimer();
113+
setPWM(0); // pw: 1 to 50
73114
}
74115

75116
bool isPushButtonPressed(){
@@ -385,7 +426,7 @@ void dacSetup(){
385426
/* /\* Set DAC Channel2 data *\/ */
386427
/* DAC_SetChannel2Data(DAC_Align_12b_L, 0x7FF0); */
387428

388-
/* /\* Start DAC Channel1 conversion by software *\/ */
429+
/* Start DAC Channel1 conversion by software */
389430
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
390431
DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);
391432
}

firmware/Source/periph.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
extern "C" {
1212
#endif
1313

14-
typedef enum {
15-
NONE = 0,
16-
GREEN = LED_GREEN,
17-
RED = LED_RED
18-
} LedPin;
19-
20-
LedPin getLed();
21-
void setLed(LedPin led);
22-
void toggleLed();
14+
/* typedef enum { */
15+
/* NONE = 0, */
16+
/* GREEN = LED_GREEN, */
17+
/* RED = LED_RED */
18+
/* } LedPin; */
19+
20+
#define LED_FULL 50
21+
/* LedPin getLed(); */
22+
void setLed(uint16_t brightness);
23+
/* void setLed(LedPin led); */
24+
/* void toggleLed(); */
2325

2426
void ledSetup();
2527
void pushButtonSetup(void (*f)());

0 commit comments

Comments
 (0)