2 * Backlighting code for PS2AVRGB boards (ATMEGA32A)
3 * Kenneth A. (github.com/krusli | krusli.me)
9 #include <avr/pgmspace.h>
10 #include <avr/interrupt.h>
12 #include "backlight_custom.h"
13 #include "breathing_custom.h"
19 // Port D: digital pins of the AVR chipset
20 #define NUMLOCK_PORT (1 << 0) // 0th pin of Port D (digital)
21 #define CAPSLOCK_PORT (1 << 1) // 1st pin
22 #define BACKLIGHT_PORT (1 << 4) // D4
23 //#define SCROLLLOCK_PORT (1 << 6) // D6
25 #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64
26 #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default
28 #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask
31 #define TIMER_TOP 255 // 8 bit PWM
33 extern backlight_config_t backlight_config;
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
45 // turn LEDs on and off depending on USB caps/num/scroll lock states.
46 __attribute__ ((weak))
47 void led_set_user(uint8_t usb_led) {
48 if (usb_led & (1 << USB_LED_NUM_LOCK)) {
51 PORTD |= NUMLOCK_PORT;
54 DDRD &= ~NUMLOCK_PORT;
55 PORTD &= ~NUMLOCK_PORT;
58 if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
59 DDRD |= CAPSLOCK_PORT;
60 PORTD |= CAPSLOCK_PORT;
62 DDRD &= ~CAPSLOCK_PORT;
63 PORTD &= ~CAPSLOCK_PORT;
66 /* YMD96 does not have scroll lock led
67 if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
68 DDRD |= SCROLLLOCK_PORT;
69 PORTD |= SCROLLLOCK_PORT;
71 DDRD &= ~SCROLLLOCK_PORT;
72 PORTD &= ~SCROLLLOCK_PORT;
76 #ifdef BACKLIGHT_ENABLE
78 // sets up Timer 1 for 8-bit PWM
79 void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE
81 TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH
82 TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW
84 // clear output compare value A
88 // clear output comparator registers for B
89 OCR1BH = 0; // outb(OCR1BH, 0);
90 OCR1BL = 0; // outb(OCR1BL, 0);
94 void timer1Init(void) {
95 // timer1SetPrescaler(TIMER1PRESCALE)
97 (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
100 TCNT1H = 0; // outb(TCNT1H, 0);
101 TCNT1L = 0; // outb(TCNT1L, 0);
103 // TOIE1: Timer Overflow Interrupt Enable (Timer 1);
104 TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1);
109 void timer1UnInit(void) {
110 // set prescaler back to NONE
111 (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP
113 // disable timer overflow interrupt
114 TIMSK &= ~_BV(TOIE1); // overflow bit?
122 // handle TCNT1 overflow
123 //! Interrupt handler for tcnt1 overflow interrupt
124 ISR(TIMER1_OVF_vect, ISR_NOBLOCK)
127 // handle breathing here
128 #ifdef BACKLIGHT_BREATHING
129 if (is_breathing()) {
130 custom_breathing_handler();
134 // TODO call user defined function
137 // enable timer 1 PWM
139 void timer1PWMBEnable(void) {
140 // turn on channel B (OC1B) PWM output
141 // set OC1B as non-inverted PWM
142 TCCR1A |= _BV(COM1B1);
143 TCCR1A &= ~_BV(COM1B0);
146 // disable timer 1 PWM
148 void timer1PWMBDisable(void) {
149 TCCR1A &= ~_BV(COM1B1);
150 TCCR1A &= ~_BV(COM1B0);
153 void enableBacklight(void) {
154 DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
155 PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
158 void disableBacklight(void) {
159 // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
160 PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
163 void startPWM(void) {
175 void b_led_init_ports(void) {
176 /* turn backlight on/off depending on user preference */
177 #if BACKLIGHT_ON_STATE == 0
178 // DDRx register: sets the direction of Port D
179 // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
180 PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
182 DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
183 PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
189 #ifdef BACKLIGHT_BREATHING
194 void b_led_set(uint8_t level) {
195 if (level > BACKLIGHT_LEVELS) {
196 level = BACKLIGHT_LEVELS;
199 setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS));
202 // called every matrix scan
203 void b_led_task(void) {
204 // do nothing for now
207 void setPWM(uint16_t xValue) {
208 if (xValue > TIMER_TOP) {
211 OCR1B = xValue; // timer1PWMBSet(xValue);
214 #endif // BACKLIGHT_ENABLE