Numworks Epsilon  1.4.1
Graphing Calculator Operating System
led.cpp
Go to the documentation of this file.
1 #include <ion/led.h>
2 #include <ion/display.h>
3 #include "led.h"
4 #include "regs/regs.h"
5 
6 // Public Ion::LED methods
7 
12 }
13 
14 // Private Ion::Device::LED methods
15 
16 namespace Ion {
17 namespace LED {
18 namespace Device {
19 
20 void init() {
21  initGPIO();
22  initTimer();
23 }
24 
25 void shutdown() {
26  shutdownTimer();
27  shutdownGPIO();
28 }
29 
30 void initGPIO() {
31  /* RED_LED(PC7), GREEN_LED(PB1), and BLUE_LED(PB0) are driven using a timer,
32  * which is an alternate function. More precisely, we will use AF2, which maps
33  * PB0 to TIM3_CH2, PB1 to TIM3_CH4, and PC7 to TIM3_CH2. */
34  for(const GPIOPin & g : RGBPins) {
35  g.group().MODER()->setMode(g.pin(), GPIO::MODER::Mode::AlternateFunction);
36  g.group().AFR()->setAlternateFunction(g.pin(), GPIO::AFR::AlternateFunction::AF2);
37  }
38 }
39 
40 void shutdownGPIO() {
41  for(const GPIOPin & g : RGBPins) {
42  g.group().MODER()->setMode(g.pin(), GPIO::MODER::Mode::Analog);
43  g.group().PUPDR()->setPull(g.pin(), GPIO::PUPDR::Pull::None);
44  }
45 }
46 
47 void initTimer() {
48  /* Let's set the prescaler to 1. Increasing the prescaler would slow down the
49  * modulation, which can be useful when debugging. */
50  TIM3.PSC()->set(1);
51 
52  /* Pulse width modulation mode allows you to generate a signal with a
53  * frequency determined by the value of the TIMx_ARR register and a duty cycle
54  * determined by the value of the TIMx_CCRx register. */
55  TIM3.ARR()->set(PWMPeriod);
56  TIM3.CCR2()->set(0);
57  TIM3.CCR3()->set(0);
58  TIM3.CCR4()->set(0);
59 
60  // Set Channels 2-4 as outputs, PWM mode 1
61  TIM3.CCMR()->setOC2M(TIM::CCMR::OCM::PWM1);
62  TIM3.CCMR()->setOC3M(TIM::CCMR::OCM::PWM1);
63  TIM3.CCMR()->setOC4M(TIM::CCMR::OCM::PWM1);
64 
65  // Output preload enable for channels 2-4
66  TIM3.CCMR()->setOC2PE(true);
67  TIM3.CCMR()->setOC3PE(true);
68  TIM3.CCMR()->setOC4PE(true);
69 
70  // Auto-reload preload enable
71  TIM3.CR1()->setARPE(true);
72 
73  // Enable Capture/Compare for channel 2 to 4
74  TIM3.CCER()->setCC2E(true);
75  TIM3.CCER()->setCC3E(true);
76  TIM3.CCER()->setCC4E(true);
77 
78  TIM3.BDTR()->setMOE(true);
79 
80  TIM3.CR1()->setCEN(true);
81 }
82 
83 void shutdownTimer() {
87 }
88 
89 void enforceState(bool red, bool green, bool blue) {
90  bool states[3] = {red, green, blue};
91  for (int i=0; i<3; i++) {
92  GPIOPin p = RGBPins[i];
93  if (states[i]) {
95  p.group().ODR()->set(p.pin(), true);
96  } else {
99  }
100  }
101 }
102 
103 }
104 }
105 }
constexpr uint16_t PWMPeriod
Definition: led.h:32
void shutdown()
Definition: led.cpp:25
void init()
Definition: led.cpp:20
Definition: tim.h:59
void shutdownGPIO()
Definition: led.cpp:40
uint8_t pin() const
Definition: gpio.h:99
Definition: tim.h:69
void enforceState(bool red, bool green, bool blue)
Definition: led.cpp:89
constexpr TIM TIM3(3)
Definition: tim.h:67
void set(Register< T > value) volatile
Definition: register.h:12
void initTimer()
Definition: led.cpp:47
Definition: gpio.h:47
Definition: tim.h:64
c(generic_all_nodes)
Definition: tim.h:8
void initGPIO()
Definition: led.cpp:30
void LED(const char *input)
Definition: led.cpp:11
void shutdownTimer()
Definition: led.cpp:83
Definition: tim.h:51
Definition: tim.h:14
void setPull(int index, Pull pull) volatile
Definition: gpio.h:39
void setColor(KDColor c)
Definition: led.cpp:8
Definition: color.h:6
Definition: tim.h:65
Definition: gpio.h:95
GPIO group() const
Definition: gpio.h:98
void setMode(int index, Mode mode) volatile
Definition: gpio.h:17
uint16_t dutyCycleForUInt8(uint8_t value)
Definition: led.h:34
Definition: backlight.h:6
Definition: tim.h:68
void set(int index, bool state) volatile
Definition: gpio.h:50