fremoclock  1_3A01
FREMO Clock - Software for UTG
ClockKernel.c
Go to the documentation of this file.
1 
37 /*
38  Copyright (C) 2007 Bernd Wisotzki
39 
40  This library is free software; you can redistribute it and/or
41  modify it under the terms of the GNU Lesser General Public
42  License as published by the Free Software Foundation; either
43  version 2.1 of the License, or (at your option) any later version.
44 
45  This library is distributed in the hope that it will be useful,
46  but WITHOUT ANY WARRANTY; without even the implied warranty of
47  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48  Lesser General Public License for more details.
49 
50  You should have received a copy of the GNU Lesser General Public
51  License along with this library; if not, write to the Free Software
52  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 */
54 
55 /* -- System wide includes -------------------------------------------------- */
56 #include <stdint.h> // typedef uint8_t, typedef uint16_t
57 #include <stdbool.h> // typedef bool, true, false
58 #include <util/atomic.h> // #define ATOMIC_BLOCK(), ATOMIC_RESTORESTATE
59 
60 /* -- Special Includes e.g. Onboard or direct attached Equipment ------------ */
61 //TODO: cleanup of imports, add comments what is imported
62 #include "sysdef.h"
63 #include "ClockKernel.h"
64 
65 /* -- global Variables ------------------------------------------------------ */
66 
67 //const fclock_t CLOCK_MIDNIGHT = { 0, 0 };
68 
69 /* -- Variables for clock kernel -------------------------------------------- */
70 
71 /* -- Variables shared between user interface and interrupt routine ----- */
73 static volatile uint16_t uiClockTimer = 0;
74 
76 static volatile uint16_t uiClockPeriod = 0;
77 
79 static volatile uint16_t uiClockPulse = 0;
80 
82 static volatile uint8_t ucClockStatus = 0;
83 
85 static volatile bool ucClockOddPulse = 0;
86 
88 static volatile fclock_t ucClock = CLOCK_MIDNIGHT;
89 
90 /* -- Variables used only by user interface ----------------------------- */
91 static uint8_t ucClockTimeFactor;
92 static uint8_t ucStartTimeMinute;
93 static uint8_t ucStartTimeHour;
94 static uint8_t ucEndTimeMinute;
95 static uint8_t ucEndTimeHour;
96 
97 /* -- Function prototypes internal functions -------------------------------- */
98 
99 // for comment see below
100 static void ClockGeneratePulse(void);
101 
102 /* -- Standard functions ---------------------------------------------------- */
103 
107 void ClockKernelInit(void)
108 {
109  // Initialize the port for pulse generation
110 
111  #if defined BOARD_PROTO_128 //ProtoBoardMega128
112  //---- Port G: mega128 only. For developement purposes
113  // Used for pulse generation and push button status
114  //PORTG = 0xee; // PG0-4 outputs -> driven low (LEDs)
115  CLOCK_PORT = 0x1F; // All LED off
116  //PORTG &=~_BV(PG0); //Bit clear
117  //PORTG |=_BV(PG0); //Bit set
118  PORTG &= ~_BV(PG3);
119  PORTG &= ~_BV(PG4);
120  CLOCK_DDR = 0x1f; //
121  #elif defined BOARD_PROTO_48 //ProtoBoardMega48
123  & ~_BV(CLOCK_PULSE_ODD) & ~_BV(CLOCK_PULSE_EVEN)); // All pulse related bits off
125  | _BV(CLOCK_PULSE_ODD) | _BV(CLOCK_PULSE_EVEN)); // All pulse related output
126  CLOCK_PORT |= (_BV(CLOCK_PULSE_ENABLE12) | _BV(CLOCK_PULSE_ENABLE34)); // Enable both clocl lines
127  #endif //Boardtype
128 }
129 
136 {
137  // add your functioncalls and code as apropriate
138 }
139 
146 {
147  // add your functioncalls and code as apropriate
148 }
149 
156 {
157  // add your functioncalls and code as apropriate
159 }
160 
161 /* -- User specific functions ----------------------------------------------- */
162 
163 /* -- Functions for Clock --------------------------------------------------- */
164 
165 /* Initalize clock */
166 /*void ClockInit(void)
167 {
168  // Wat machen mer denn da???
169  // Na logisch, einlesen der Werte ausm EEPROM, doofe Frage!
170 }*/
171 
187 void ClockSetStatus(uint8_t ucStatus)
188 {
189  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) // save SREG, cli() and restore SREG
190  {
191  ucClockStatus = ucStatus;
192  if (ucStatus >= _BV(CLOCK_STATUS_RUN))
193  {
195  }
196  if (ucStatus >= (_BV(CLOCK_STATUS_RUN) | _BV(CLOCK_STATUS_TIME)))
197  {
198  // Uups, we have to calculate odd/even from the actual time!!
199  ucClockOddPulse = (bool) (ucClock.minute & _BV(0)); // true=1, false=0
200  }
201  }
202 }
203 
212 #pragma GCC diagnostic push // avoid false positive on gcc 4.9.2
213 #pragma GCC diagnostic ignored "-Wreturn-type"
214 uint8_t ClockReadStatus(void) //TODO: never used
215 {
216  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
217  {
218  return ucClockStatus;
219  }
220 }
221 #pragma GCC diagnostic pop
222 
228 uint8_t ClockIsRunning(void) //FIXME: this is a function used as const value
229 {
230  return (_BV(CLOCK_STATUS_RUN) | _BV(CLOCK_STATUS_TIME));
231 }
232 
239 void ClockSetPeriod(uint16_t uiPeriod)
240 {
241  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
242  {
243  uiClockPeriod = uiPeriod;
244  }
245 }
246 
253 #pragma GCC diagnostic push // avoid false positive on gcc 4.9.2
254 #pragma GCC diagnostic ignored "-Wreturn-type"
255 uint16_t ClockReadPeriod(void)
256 {
257  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
258  {
259  return uiClockPeriod;
260  }
261 }
262 #pragma GCC diagnostic pop
263 
270 void ClockSetPulse(uint16_t uiPulse)
271 {
272  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
273  {
274  uiClockPulse = uiPulse;
275  }
276 }
277 
284 #pragma GCC diagnostic push // avoid false positive on gcc 4.9.2
285 #pragma GCC diagnostic ignored "-Wreturn-type"
286 uint16_t ClockReadPulse(void)
287 {
288  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
289  {
290  return uiClockPulse;
291  }
292 }
293 #pragma GCC diagnostic pop
294 
301 void ClockSetTime(fclock_t newValue)
302 {
303  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
304  {
305  ucClock = newValue;
306  }
307 }
308 
315 #pragma GCC diagnostic push // avoid false positive on gcc 4.9.2
316 #pragma GCC diagnostic ignored "-Wreturn-type"
318 {
319  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
320  {
321  return ucClock;
322  }
323 }
324 #pragma GCC diagnostic pop
325 
331 void ClockSetTimeFactor(uint8_t ucFactor)
332 {
333  if (ucFactor < CLOCK_TIME_FACTOR_MIN)
334  {
335  ucFactor = CLOCK_TIME_FACTOR_MIN;
336  }
337  else if (ucFactor > CLOCK_TIME_FACTOR_MAX)
338  {
339  ucFactor = CLOCK_TIME_FACTOR_MAX;
340  }
341  ucClockTimeFactor = ucFactor;
343 }
344 
350 uint8_t ClockReadTimeFactor(void)
351 {
352  return ucClockTimeFactor;
353 }
354 
362 void ClockSetStartTimeMinute(uint8_t ucMinute)
363 {
364  ucStartTimeMinute = ucMinute;
365 }
366 
375 {
376  return ucStartTimeMinute;
377 }
378 
386 void ClockSetStartTimeHour(uint8_t ucHour)
387 {
388  ucStartTimeHour = ucHour;
389 }
390 
399 {
400  return ucStartTimeHour;
401 }
402 
410 void ClockSetEndTimeMinute(uint8_t ucMinute)
411 {
412  ucEndTimeMinute = ucMinute;
413 }
414 
423 {
424  return ucEndTimeMinute;
425 }
426 
434 void ClockSetEndTimeHour(uint8_t ucHour)
435 {
436  ucEndTimeHour = ucHour;
437 }
438 
446 uint8_t ClockReadEndTimeHour(void)
447 {
448  return ucEndTimeHour;
449 }
450 
458 static void ClockGeneratePulse(void)
459 {
460  if (ucClockStatus >= _BV(CLOCK_STATUS_RUN))
461  {
462  uiClockTimer--;
463 
464  if (uiClockTimer == 0)
465  {
467  // pulse off!!
468  CLOCK_PORT &= ~_BV(CLOCK_PULSE_ODD); //Bit clear odd bit
469  CLOCK_PORT &= ~_BV(CLOCK_PULSE_EVEN); //Bit clear even bit
470  }
471 
472  if (uiClockTimer == uiClockPulse)
473  {
474  //Set pulse and toggle odd/even
475  if (ucClockOddPulse)
476  {
477  CLOCK_PORT |=_BV(CLOCK_PULSE_ODD); //Bit set odd bit
478  ucClockOddPulse = false;
479  }
480  else //(!ucClockOddPulse)
481  {
482  CLOCK_PORT |=_BV(CLOCK_PULSE_EVEN); //Bit set even bit
483  ucClockOddPulse = true;
484  }
485 
486  if (ucClockStatus >= (_BV(CLOCK_STATUS_RUN) | _BV(CLOCK_STATUS_TIME)))
487  {
488  // increment actual time
489  ucClock.minute++;
490  if (ucClock.minute >= 60)
491  {
492  ucClock.minute = 0;
493  ucClock.hour++;
494  }
495  if (ucClock.hour >= 24)
496  {
497  ucClock.hour = 0;
498  }
499  }
500  } // (uiClockTimer == uiClockPulse)
501  }
502  else
503  {
504  if ( ((CLOCK_PORT & _BV(CLOCK_PULSE_ODD)) != 0)
505  || ((CLOCK_PORT & _BV(CLOCK_PULSE_EVEN)) != 0) ) //pulse active?
506  {
507  uiClockTimer--;
508  if (uiClockTimer == 0)
509  {
511  // pulse off!!
512  CLOCK_PORT &= ~_BV(CLOCK_PULSE_ODD); //Bit clear odd bit
513  CLOCK_PORT &= ~_BV(CLOCK_PULSE_EVEN); //Bit clear even bit
514  }
515  }
516  }
517 }
uint8_t ClockIsRunning(void)
Value used as parameter to ClockSetStatus() to start clock.
Definition: ClockKernel.c:228
#define CLOCK_DDR
Datadirectionreg of port for clock pulsebits.
Definition: sysdef.h:129
#define CLOCK_PULSE_ENABLE12
Portbit for enable clock pulse line 1.
Definition: sysdef.h:132
static uint8_t ucClockTimeFactor
Definition: ClockKernel.c:91
A struct with hour and minute.
Definition: ClockKernel.h:69
void ClockSetTime(fclock_t newValue)
Set actual time.
Definition: ClockKernel.c:301
uint8_t ClockReadTimeFactor(void)
Get time factor.
Definition: ClockKernel.c:350
void ClockSetStatus(uint8_t ucStatus)
Set status of pulse generator.
Definition: ClockKernel.c:187
uint8_t ClockReadEndTimeMinute(void)
Get the minute of the end time.
Definition: ClockKernel.c:422
void ClockSetStartTimeMinute(uint8_t ucMinute)
Set the minute of the start time.
Definition: ClockKernel.c:362
#define CLOCK_TIME_FACTOR_MIN
minimum *10 (10 = 1:1,0)
Definition: ClockKernel.h:96
static void ClockGeneratePulse(void)
This function is called by SysHWTimer every 100ms.
Definition: ClockKernel.c:458
#define CLOCK_STATUS_TIME
Status Bit of pulse generator: with/without increment of actual time.
Definition: ClockKernel.h:92
static volatile uint16_t uiClockPulse
Length of pulse on the clock line.
Definition: ClockKernel.c:79
void ClockSetStartTimeHour(uint8_t ucHour)
Set the hour of the start time.
Definition: ClockKernel.c:386
uint8_t minute
minute value 0..59
Definition: ClockKernel.h:71
void ClockSetEndTimeHour(uint8_t ucHour)
Set the hour of the end time.
Definition: ClockKernel.c:434
void ClockSetEndTimeMinute(uint8_t ucMinute)
Set the minute of the end time.
Definition: ClockKernel.c:410
void ClockKernelInit(void)
Initialization of the pulse generator.
Definition: ClockKernel.c:107
#define CLOCK_STATUS_RUN
Status Bit of pulse generator: start/stop.
Definition: ClockKernel.h:85
#define CLOCK_PERIOD_10
10 times counts needed for 1 min in 1:1
Definition: sysdef.h:107
static volatile bool ucClockOddPulse
odd minute?!
Definition: ClockKernel.c:85
#define CLOCK_TIME_FACTOR_MAX
maximum *10 (119 = 1:11.9)
Definition: ClockKernel.h:97
void ClockSetPeriod(uint16_t uiPeriod)
Set length of model minute in 100ms.
Definition: ClockKernel.c:239
static uint8_t ucEndTimeHour
Definition: ClockKernel.c:95
#define CLOCK_PORT
Port for clock pulsebits.
Definition: sysdef.h:128
static volatile uint16_t uiClockPeriod
Value of minute in model time (60 in 1:1) at least 2*uiClockPulse.
Definition: ClockKernel.c:76
static uint8_t ucEndTimeMinute
Definition: ClockKernel.c:94
#define CLOCK_PULSE_ENABLE34
Portbit for enable clock pulse line 2.
Definition: sysdef.h:133
uint8_t ClockReadEndTimeHour(void)
Get the hour of the end time.
Definition: ClockKernel.c:446
uint8_t hour
hour value 0..23
Definition: ClockKernel.h:70
void ClockSetTimeFactor(uint8_t ucFactor)
Set time factor.
Definition: ClockKernel.c:331
uint16_t ClockReadPulse(void)
Get length of pulse length in 100ms.
Definition: ClockKernel.c:286
static volatile uint16_t uiClockTimer
Gets decremented every 100 milliseconds by interrupt.
Definition: ClockKernel.c:73
#define CLOCK_MIDNIGHT
"00:00" alias "24:00"
Definition: ClockKernel.h:74
void SysTimerUserInterrupt10(void)
This function is called by SysHWTimer every 10ms.
Definition: ClockKernel.c:145
System wide Definitions for FREMO Clock.
uint16_t ClockReadPeriod(void)
Get length of model minute in 100ms.
Definition: ClockKernel.c:255
#define CLOCK_PULSE_ODD
Portbit for odd clock pulse.
Definition: sysdef.h:130
static volatile uint8_t ucClockStatus
Clock status byte.
Definition: ClockKernel.c:82
uint8_t ClockReadStartTimeHour(void)
Get the hour of the start time.
Definition: ClockKernel.c:398
void SysTimerUserInterrupt(void)
This function is called by SysHWTimer every 1ms.
Definition: ClockKernel.c:135
void ClockSetPulse(uint16_t uiPulse)
Set length of pulse length in 100ms.
Definition: ClockKernel.c:270
uint8_t ClockReadStartTimeMinute(void)
Get the minute of the start time.
Definition: ClockKernel.c:374
uint8_t ClockReadStatus(void)
Read status of pulse generator.
Definition: ClockKernel.c:214
User function called by timer ISR.
static uint8_t ucStartTimeHour
Definition: ClockKernel.c:93
void SysTimerUserInterrupt100(void)
This function is called by SysHWTimer every 100ms.
Definition: ClockKernel.c:155
static volatile fclock_t ucClock
actual model time
Definition: ClockKernel.c:88
#define CLOCK_PULSE_EVEN
Portbit for even clock pulse.
Definition: sysdef.h:131
fclock_t ClockGetTime(void)
Get actual time.
Definition: ClockKernel.c:317
static uint8_t ucStartTimeMinute
Definition: ClockKernel.c:92