Commit | Line | Data |
---|---|---|
365b8635 KA |
1 | /** |
2 | * Backlighting code for PS2AVRGB boards (ATMEGA32A) | |
3 | * Kenneth A. (github.com/krusli | krusli.me) | |
4 | */ | |
5 | ||
6 | #include "backlight.h" | |
7 | #include "quantum.h" | |
8 | ||
9 | #include <avr/pgmspace.h> | |
10 | #include <avr/interrupt.h> | |
11 | ||
12 | #include "backlight_custom.h" | |
13 | #include "breathing_custom.h" | |
14 | ||
15 | // DEBUG | |
16 | #include <stdlib.h> | |
17 | #include <stdio.h> | |
18 | ||
19 | // Port D: digital pins of the AVR chipset | |
f2bc70a2 KA |
20 | #define NUMLOCK_PORT (1 << 0) // D0 |
21 | #define CAPSLOCK_PORT (1 << 1) // D1 | |
22 | #define BACKLIGHT_PORT (1 << 4) // D4 | |
23 | #define SCROLLLOCK_PORT (1 << 6) // D6 | |
365b8635 KA |
24 | |
25 | #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64 | |
26 | #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default | |
27 | ||
28 | #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask | |
29 | ||
30 | #define PWM_MAX 0xFF | |
31 | #define TIMER_TOP 255 // 8 bit PWM | |
32 | ||
33 | extern backlight_config_t backlight_config; | |
34 | ||
35 | /** | |
36 | * References | |
37 | * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation | |
38 | * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b | |
39 | * Timers: http://www.avrbeginners.net/architecture/timers/timers.html | |
40 | * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/ | |
41 | * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware | |
42 | */ | |
43 | ||
44 | // @Override | |
45 | // turn LEDs on and off depending on USB caps/num/scroll lock states. | |
f2bc70a2 | 46 | __attribute__ ((weak)) |
365b8635 KA |
47 | void led_set_user(uint8_t usb_led) { |
48 | if (usb_led & (1 << USB_LED_NUM_LOCK)) { | |
49 | // turn on | |
50 | DDRD |= NUMLOCK_PORT; | |
51 | PORTD |= NUMLOCK_PORT; | |
52 | } else { | |
53 | // turn off | |
54 | DDRD &= ~NUMLOCK_PORT; | |
55 | PORTD &= ~NUMLOCK_PORT; | |
56 | } | |
57 | ||
58 | if (usb_led & (1 << USB_LED_CAPS_LOCK)) { | |
59 | DDRD |= CAPSLOCK_PORT; | |
60 | PORTD |= CAPSLOCK_PORT; | |
61 | } else { | |
62 | DDRD &= ~CAPSLOCK_PORT; | |
63 | PORTD &= ~CAPSLOCK_PORT; | |
64 | } | |
65 | ||
66 | if (usb_led & (1 << USB_LED_SCROLL_LOCK)) { | |
67 | DDRD |= SCROLLLOCK_PORT; | |
68 | PORTD |= SCROLLLOCK_PORT; | |
69 | } else { | |
70 | DDRD &= ~SCROLLLOCK_PORT; | |
71 | PORTD &= ~SCROLLLOCK_PORT; | |
72 | } | |
73 | } | |
74 | ||
75 | #ifdef BACKLIGHT_ENABLE | |
76 | ||
77 | // sets up Timer 1 for 8-bit PWM | |
78 | void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE | |
79 | // default 8 bit mode | |
80 | TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH | |
81 | TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW | |
82 | ||
83 | // clear output compare value A | |
84 | // outb(OCR1AH, 0); | |
85 | // outb(OCR1AL, 0); | |
86 | ||
87 | // clear output comparator registers for B | |
88 | OCR1BH = 0; // outb(OCR1BH, 0); | |
89 | OCR1BL = 0; // outb(OCR1BL, 0); | |
90 | } | |
91 | ||
92 | bool is_init = false; | |
93 | void timer1Init(void) { | |
94 | // timer1SetPrescaler(TIMER1PRESCALE) | |
95 | // set to DIV/64 | |
96 | (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE; | |
97 | ||
98 | // reset TCNT1 | |
99 | TCNT1H = 0; // outb(TCNT1H, 0); | |
100 | TCNT1L = 0; // outb(TCNT1L, 0); | |
101 | ||
102 | // TOIE1: Timer Overflow Interrupt Enable (Timer 1); | |
103 | TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1); | |
104 | ||
105 | is_init = true; | |
106 | } | |
107 | ||
108 | void timer1UnInit(void) { | |
109 | // set prescaler back to NONE | |
110 | (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP | |
111 | ||
112 | // disable timer overflow interrupt | |
113 | TIMSK &= ~_BV(TOIE1); // overflow bit? | |
114 | ||
115 | setPWM(0); | |
116 | ||
117 | is_init = false; | |
118 | } | |
119 | ||
120 | ||
121 | // handle TCNT1 overflow | |
122 | //! Interrupt handler for tcnt1 overflow interrupt | |
123 | ISR(TIMER1_OVF_vect, ISR_NOBLOCK) | |
124 | { | |
125 | // sei(); | |
126 | // handle breathing here | |
127 | #ifdef BACKLIGHT_BREATHING | |
128 | if (is_breathing()) { | |
129 | custom_breathing_handler(); | |
130 | } | |
131 | #endif | |
132 | ||
133 | // TODO call user defined function | |
134 | } | |
135 | ||
136 | // enable timer 1 PWM | |
137 | // timer1PWMBOn() | |
138 | void timer1PWMBEnable(void) { | |
139 | // turn on channel B (OC1B) PWM output | |
140 | // set OC1B as non-inverted PWM | |
141 | TCCR1A |= _BV(COM1B1); | |
142 | TCCR1A &= ~_BV(COM1B0); | |
143 | } | |
144 | ||
145 | // disable timer 1 PWM | |
146 | // timer1PWMBOff() | |
147 | void timer1PWMBDisable(void) { | |
148 | TCCR1A &= ~_BV(COM1B1); | |
149 | TCCR1A &= ~_BV(COM1B0); | |
150 | } | |
151 | ||
152 | void enableBacklight(void) { | |
153 | DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output | |
154 | PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high | |
155 | } | |
156 | ||
157 | void disableBacklight(void) { | |
158 | // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input | |
159 | PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low | |
160 | } | |
161 | ||
162 | void startPWM(void) { | |
163 | timer1Init(); | |
164 | timer1PWMBEnable(); | |
165 | enableBacklight(); | |
166 | } | |
167 | ||
168 | void stopPWM(void) { | |
169 | timer1UnInit(); | |
170 | disableBacklight(); | |
171 | timer1PWMBDisable(); | |
172 | } | |
173 | ||
174 | void b_led_init_ports(void) { | |
175 | /* turn backlight on/off depending on user preference */ | |
176 | #if BACKLIGHT_ON_STATE == 0 | |
177 | // DDRx register: sets the direction of Port D | |
178 | // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input | |
179 | PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low | |
180 | #else | |
181 | DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output | |
182 | PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high | |
183 | #endif | |
184 | ||
185 | timer1PWMSetup(); | |
186 | startPWM(); | |
187 | ||
188 | #ifdef BACKLIGHT_BREATHING | |
189 | breathing_enable(); | |
190 | #endif | |
191 | } | |
192 | ||
193 | void b_led_set(uint8_t level) { | |
194 | if (level > BACKLIGHT_LEVELS) { | |
195 | level = BACKLIGHT_LEVELS; | |
196 | } | |
197 | ||
198 | setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS)); | |
199 | } | |
200 | ||
201 | // called every matrix scan | |
202 | void b_led_task(void) { | |
203 | // do nothing for now | |
204 | } | |
205 | ||
206 | void setPWM(uint16_t xValue) { | |
207 | if (xValue > TIMER_TOP) { | |
208 | xValue = TIMER_TOP; | |
209 | } | |
210 | OCR1B = xValue; // timer1PWMBSet(xValue); | |
211 | } | |
212 | ||
213 | #endif // BACKLIGHT_ENABLE |