aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/machine/machine_stm32_tim.go
blob: 4898ed9f6f174fbabedfc8d42448120fe1d990f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//go:build stm32

package machine

// The type alias `arrtype` should be defined to either uint32 or uint16
// depending on the size of that register in the MCU's TIM_Type structure.

import (
	"device/stm32"
	"runtime/interrupt"
	"runtime/volatile"
)

const PWM_MODE1 = 0x6

type TimerCallback func()
type ChannelCallback func(channel uint8)

type PinFunction struct {
	Pin     Pin
	AltFunc uint8
}

type TimerChannel struct {
	Pins []PinFunction
}

type TIM struct {
	EnableRegister *volatile.Register32
	EnableFlag     uint32
	Device         *stm32.TIM_Type
	Channels       [4]TimerChannel
	UpInterrupt    interrupt.Interrupt
	OCInterrupt    interrupt.Interrupt

	wraparoundCallback TimerCallback
	channelCallbacks   [4]ChannelCallback

	busFreq uint64
}

// Configure enables and configures this PWM.
func (t *TIM) Configure(config PWMConfig) error {
	// Enable device
	t.EnableRegister.SetBits(t.EnableFlag)

	err := t.setPeriod(config.Period, true)
	if err != nil {
		return err
	}

	// Auto-repeat
	t.Device.EGR.SetBits(stm32.TIM_EGR_UG)

	// Enable the timer
	t.Device.CR1.SetBits(stm32.TIM_CR1_CEN | stm32.TIM_CR1_ARPE)

	return nil
}

func (t *TIM) Count() uint32 {
	return uint32(t.Device.CNT.Get())
}

// SetWraparoundInterrupt configures a callback to be called each
// time the timer 'wraps-around'.
//
// For example, if `Configure(PWMConfig{Period:1000000})` is used,
// to set the timer period to 1ms, this callback will be called every
// 1ms.
func (t *TIM) SetWraparoundInterrupt(callback TimerCallback) error {
	// Disable this interrupt to prevent race conditions
	//t.UpInterrupt.Disable()

	// Ensure the interrupt handler for Update events is registered
	t.UpInterrupt = t.registerUPInterrupt()

	// Clear update flag
	t.Device.SR.ClearBits(stm32.TIM_SR_UIF)

	t.wraparoundCallback = callback
	t.UpInterrupt.SetPriority(0xc1)
	t.UpInterrupt.Enable()

	// Enable the hardware interrupt
	t.Device.DIER.SetBits(stm32.TIM_DIER_UIE)

	return nil
}

// Sets a callback to be called when a channel reaches it's set-point.
//
// For example, if `t.Set(ch, t.Top() / 4)` is used then the callback will
// be called every quarter-period of the timer's base Period.
func (t *TIM) SetMatchInterrupt(channel uint8, callback ChannelCallback) error {
	t.channelCallbacks[channel] = callback

	// Ensure the interrupt handler for Output Compare events is registered
	t.OCInterrupt = t.registerOCInterrupt()

	// Clear the interrupt flag
	t.Device.SR.ClearBits(stm32.TIM_SR_CC1IF << channel)

	// Enable the interrupt
	t.OCInterrupt.SetPriority(0xc1)
	t.OCInterrupt.Enable()

	// Enable the hardware interrupt
	t.Device.DIER.SetBits(stm32.TIM_DIER_CC1IE << channel)

	return nil
}

// SetPeriod updates the period of this PWM peripheral.
// To set a particular frequency, use the following formula:
//
//	period = 1e9 / frequency
//
// If you use a period of 0, a period that works well for LEDs will be picked.
//
// SetPeriod will not change the prescaler, but also won't change the current
// value in any of the channels. This means that you may need to update the
// value for the particular channel.
//
// Note that you cannot pick any arbitrary period after the PWM peripheral has
// been configured. If you want to switch between frequencies, pick the lowest
// frequency (longest period) once when calling Configure and adjust the
// frequency here as needed.
func (t *TIM) SetPeriod(period uint64) error {
	return t.setPeriod(period, false)
}

func (t *TIM) setPeriod(period uint64, updatePrescaler bool) error {
	var top uint64
	if period == 0 {
		top = ARR_MAX
	} else {
		top = (period / 1000) * (t.busFreq / 1000) / 1000
	}

	var psc uint64
	if updatePrescaler {
		if top > ARR_MAX*PSC_MAX {
			return ErrPWMPeriodTooLong
		}

		// Select the minimum PSC that scales the ARR value into
		// range to maintain precision in ARR for changing frequencies
		// later
		psc = ceil(top, ARR_MAX)
		top = top / psc

		t.Device.PSC.Set(uint32(psc - 1))
	} else {
		psc = uint64(t.Device.PSC.Get()) + 1
		top = top / psc

		if top > ARR_MAX {
			return ErrPWMPeriodTooLong
		}
	}

	t.Device.ARR.Set(arrtype(top - 1))
	return nil
}

// Top returns the current counter top, for use in duty cycle calculation. It
// will only change with a call to Configure or SetPeriod, otherwise it is
// constant.
//
// The value returned here is hardware dependent. In general, it's best to treat
// it as an opaque value that can be divided by some number and passed to
// pwm.Set (see pwm.Set for more information).
func (t *TIM) Top() uint32 {
	return uint32(t.Device.ARR.Get()) + 1
}

// Channel returns a PWM channel for the given pin.
func (t *TIM) Channel(pin Pin) (uint8, error) {

	for chi, ch := range t.Channels {
		for _, p := range ch.Pins {
			if p.Pin == pin {
				t.configurePin(uint8(chi), p)
				//p.Pin.ConfigureAltFunc(PinConfig{Mode: PinModePWMOutput}, p.AltFunc)
				return uint8(chi), nil
			}
		}
	}

	return 0, ErrInvalidOutputPin
}

// Set updates the channel value. This is used to control the channel duty
// cycle. For example, to set it to a 25% duty cycle, use:
//
//	t.Set(ch, t.Top() / 4)
//
// ch.Set(0) will set the output to low and ch.Set(ch.Top()) will set the output
// to high, assuming the output isn't inverted.
func (t *TIM) Set(channel uint8, value uint32) {
	t.enableMainOutput()

	ccr := t.channelCCR(channel)
	ccmr, offset := t.channelCCMR(channel)

	// Disable interrupts whilst programming to prevent spurious OC interrupts
	mask := interrupt.Disable()

	// Set the PWM to Mode 1 (active below set value, inactive above)
	// Preload is disabled so we can change OC value within one update period.
	var ccmrVal uint32
	ccmrVal |= PWM_MODE1 << stm32.TIM_CCMR1_Output_OC1M_Pos
	ccmr.ReplaceBits(ccmrVal, 0xFF, offset)

	// Set the compare value
	ccr.Set(arrtype(value))

	// Enable the channel (if not already)
	t.Device.CCER.ReplaceBits(stm32.TIM_CCER_CC1E, 0xD, channel*4)

	// Force update
	t.Device.EGR.SetBits(stm32.TIM_EGR_CC1G << channel)

	// Reset Interrupt Flag
	t.Device.SR.ClearBits(stm32.TIM_SR_CC1IF << channel)

	// Restore interrupts
	interrupt.Restore(mask)
}

// Unset disables a channel, including any configured interrupts.
func (t *TIM) Unset(channel uint8) {
	// Disable interrupts whilst programming to prevent spurious OC interrupts
	mask := interrupt.Disable()

	// Disable the channel
	t.Device.CCER.ReplaceBits(0, 0xD, channel*4)

	// Reset to zero value
	ccr := t.channelCCR(channel)
	ccr.Set(0)

	// Disable the hardware interrupt
	t.Device.DIER.ClearBits(stm32.TIM_DIER_CC1IE << channel)

	// Clear the interrupt flag
	t.Device.SR.ClearBits(stm32.TIM_SR_CC1IF << channel)

	// Restore interrupts
	interrupt.Restore(mask)
}

// SetInverting sets whether to invert the output of this channel.
// Without inverting, a 25% duty cycle would mean the output is high for 25% of
// the time and low for the rest. Inverting flips the output as if a NOT gate
// was placed at the output, meaning that the output would be 25% low and 75%
// high with a duty cycle of 25%.
func (t *TIM) SetInverting(channel uint8, inverting bool) {
	// Enable the channel (if not already)

	var val = uint32(0)
	if inverting {
		val |= stm32.TIM_CCER_CC1P
	}

	t.Device.CCER.ReplaceBits(val, stm32.TIM_CCER_CC1P_Msk, channel*4)
}

func (t *TIM) handleUPInterrupt(interrupt.Interrupt) {
	if t.Device.SR.HasBits(stm32.TIM_SR_UIF) {
		// clear the update flag
		t.Device.SR.ClearBits(stm32.TIM_SR_UIF)

		if t.wraparoundCallback != nil {
			t.wraparoundCallback()
		}
	}
}

func (t *TIM) handleOCInterrupt(interrupt.Interrupt) {
	if t.Device.SR.HasBits(stm32.TIM_SR_CC1IF) {
		if t.channelCallbacks[0] != nil {
			t.channelCallbacks[0](0)
		}
	}
	if t.Device.SR.HasBits(stm32.TIM_SR_CC2IF) {
		if t.channelCallbacks[1] != nil {
			t.channelCallbacks[1](1)
		}
	}
	if t.Device.SR.HasBits(stm32.TIM_SR_CC3IF) {
		if t.channelCallbacks[2] != nil {
			t.channelCallbacks[2](2)
		}
	}
	if t.Device.SR.HasBits(stm32.TIM_SR_CC4IF) {
		if t.channelCallbacks[3] != nil {
			t.channelCallbacks[3](3)
		}
	}

	// Reset interrupt flags
	t.Device.SR.ClearBits(stm32.TIM_SR_CC1IF | stm32.TIM_SR_CC2IF | stm32.TIM_SR_CC3IF | stm32.TIM_SR_CC4IF)
}

func (t *TIM) channelCCR(channel uint8) *arrRegType {
	switch channel {
	case 0:
		return &t.Device.CCR1
	case 1:
		return &t.Device.CCR2
	case 2:
		return &t.Device.CCR3
	case 3:
		return &t.Device.CCR4
	}

	return nil
}

func (t *TIM) channelCCMR(channel uint8) (reg *volatile.Register32, offset uint8) {
	switch channel {
	case 0:
		return &t.Device.CCMR1_Output, 0
	case 1:
		return &t.Device.CCMR1_Output, 8
	case 2:
		return &t.Device.CCMR2_Output, 0
	case 3:
		return &t.Device.CCMR2_Output, 8
	}

	return nil, 0
}

//go:inline
func ceil(num uint64, denom uint64) uint64 {
	return (num + denom - 1) / denom
}