indicator_leds.c (3118B)
1 /* 2 Copyright 2016 Ralf Schmitt <ralf@bunkertor.net> 3 This program is free software: you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation, either version 2 of the License, or 6 (at your option) any later version. 7 This program is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU General Public License for more details. 11 You should have received a copy of the GNU General Public License 12 along with this program. If not, see <http://www.gnu.org/licenses/>. 13 */ 14 15 #include "indicator_leds.h" 16 #include <avr/interrupt.h> 17 #include <avr/io.h> 18 #include <util/delay.h> 19 20 #define LED_T1H 900 21 #define LED_T1L 600 22 #define LED_T0H 400 23 #define LED_T0L 900 24 25 void send_bit_d4(bool bitVal) 26 { 27 if(bitVal) { 28 asm volatile ( 29 "sbi %[port], %[bit] \n\t" 30 ".rept %[onCycles] \n\t" 31 "nop \n\t" 32 ".endr \n\t" 33 "cbi %[port], %[bit] \n\t" 34 ".rept %[offCycles] \n\t" 35 "nop \n\t" 36 ".endr \n\t" 37 :: 38 [port] "I" (_SFR_IO_ADDR(PORTD)), 39 [bit] "I" (4), 40 [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2), 41 [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2)); 42 } else { 43 asm volatile ( 44 "sbi %[port], %[bit] \n\t" 45 ".rept %[onCycles] \n\t" 46 "nop \n\t" 47 ".endr \n\t" 48 "cbi %[port], %[bit] \n\t" 49 ".rept %[offCycles] \n\t" 50 "nop \n\t" 51 ".endr \n\t" 52 :: 53 [port] "I" (_SFR_IO_ADDR(PORTD)), 54 [bit] "I" (4), 55 [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2), 56 [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2)); 57 } 58 } 59 60 void send_bit_d6(bool bitVal) 61 { 62 if(bitVal) { 63 asm volatile ( 64 "sbi %[port], %[bit] \n\t" 65 ".rept %[onCycles] \n\t" 66 "nop \n\t" 67 ".endr \n\t" 68 "cbi %[port], %[bit] \n\t" 69 ".rept %[offCycles] \n\t" 70 "nop \n\t" 71 ".endr \n\t" 72 :: 73 [port] "I" (_SFR_IO_ADDR(PORTD)), 74 [bit] "I" (6), 75 [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2), 76 [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2)); 77 } else { 78 asm volatile ( 79 "sbi %[port], %[bit] \n\t" 80 ".rept %[onCycles] \n\t" 81 "nop \n\t" 82 ".endr \n\t" 83 "cbi %[port], %[bit] \n\t" 84 ".rept %[offCycles] \n\t" 85 "nop \n\t" 86 ".endr \n\t" 87 :: 88 [port] "I" (_SFR_IO_ADDR(PORTD)), 89 [bit] "I" (6), 90 [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2), 91 [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2)); 92 } 93 } 94 95 void send_value(uint8_t byte, enum Device device) 96 { 97 for(uint8_t b = 0; b < 8; b++) { 98 if(device == Device_STATUSLED) { 99 send_bit_d4(byte & 0b10000000); 100 } 101 if(device == Device_PCBRGB) { 102 send_bit_d6(byte & 0b10000000); 103 } 104 byte <<= 1; 105 } 106 } 107 108 void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device) 109 { 110 send_value(r, device); 111 send_value(g, device); 112 send_value(b, device); 113 }