yarf 0.1
Yet Another RepRap Firmware
|
00001 /* 00002 * clock.c 00003 * 00004 * Copyright 2011 Pieter Agten 00005 * 00006 * This file is part of Yarf. 00007 * 00008 * Yarf is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * Yarf is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with Yarf. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00031 #include "clock.h" 00032 00033 #include "yarf.h" 00034 #include "fastio.h" 00035 #include "scheduling/periodic.h" 00036 #include "scheduling/periodic_notify.h" 00037 #include "movement/advance.h" //TODO: decrease coupling between clock.c and advance 00038 #include "util/math.h" 00039 00040 #include <avr/interrupt.h> 00041 #include <util/atomic.h> 00042 00046 #define TIMER_TICKS_PER_MS (F_CPU/8/1000) 00047 00048 00052 static volatile unsigned long millis; 00053 00057 static void 00058 timer1_init(void) 00059 { 00060 /* Disable timer 1 */ 00061 TCCR1B = 0; 00062 00063 /* Clear all timer 1 interrupt flags */ 00064 TIFR1 = _BV(ICF1)|_BV(OCF1B)|_BV(OCF1A)|_BV(TOV1); 00065 00066 /* Enable the timer 1 OCR1A interrupt */ 00067 TIMSK1 = _BV(OCIE1B); 00068 00069 #if ADVANCE_ALGORITHM 00070 TIMSK1 |= _BV(OCIE1A); //TODO: decrease coupling between clock.c and advance 00071 #endif 00072 00073 /* 16-bit register writes using TEMP must be performed atomically */ 00074 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 00075 /* Clear the timer 1 value */ 00076 TCNT1 = 0; 00077 /* Set the timer 1 B top value */ 00078 OCR1B = TIMER_TICKS_PER_MS; 00079 00080 #if ADVANCE_ALGORITHM 00081 /* Set the timer 1 A top value */ 00082 OCR1A = EXTRUDER_TIMER_TICKS; //TODO: decrease coupling between clock.c and advance 00083 #endif 00084 } 00085 00086 /* Enable timer 1 with /8 prescaler in normal mode */ 00087 TCCR1A = 0; 00088 TCCR1B = (2 << CS10); 00089 } 00090 00091 00098 ISR(TIMER1_COMPB_vect, ISR_NOBLOCK) 00099 { 00100 /* 16-bit register writes using TEMP must be performed atomically */ 00101 ATOMIC_BLOCK(ATOMIC_FORCEON) { 00102 /* Set the timer 1 B top value */ 00103 OCR1B += TIMER_TICKS_PER_MS; 00104 } 00105 00106 millis += 1; 00107 periodic_notify_ms(); 00108 } 00109 00110 void 00111 clock_init(void) 00112 { 00113 millis = 0; 00114 periodic_init(); 00115 timer1_init(); 00116 } 00117 00118 unsigned long 00119 clock_millis(void) 00120 { 00121 return millis; 00122 } 00123 00124