root/webserver/example/freeRTOSexample/Source/EnergyMeters/Meters.c

Revision 22, 3.7 kB (checked in by phil, 15 years ago)

deleted some test files,
gas meter values (unsigned long long) now updated.
added epoch time handling

Line 
1
2 #include "FreeRTOS.h"
3 #include "semphr.h"
4 #include "task.h"
5
6 #define metersSTACK_SIZE                configMINIMAL_STACK_SIZE+200
7 /* masks for RTC */
8 #define MASKSEC 0x3F                                                                            // Second 00..59   00000000:00000000:00xxxxxx
9 #define MASKMIN 0x3F00                                                                          // Minute 00..59   00000000:00xxxxxx:00000000
10 #define MASKHR  0x1F0000                                                                        // Hour 00..23     000xxxxx:00000000:00000000
11
12 #include "MetersIncludes.h"
13
14 extern xTickCount; /* tick counter (milliseconds) */
15
16 portBASE_TYPE Init_P2_0(void);
17 void basementGasCalculation(void);
18
19 portLONG basementGasReading = 0;
20 portTickType basementGasLastTime = 0;
21
22 /* The semaphore used to wake the Meters task when a pulse was received or the timer expired. */
23 xSemaphoreHandle xMetersSemaphore = NULL;
24
25 portTickType xLastMeterTaskRunTime; /* keeps the last time the meter calculation ran */
26
27
28   int rtcHOURS;
29   int rtcMINUTES;
30   int rtcSECONDS;
31 static portTASK_FUNCTION( vMeters_Task, pvParameters )
32 {
33
34
35
36   vSemaphoreCreateBinary( xMetersSemaphore );
37        
38   Init_P2_0(); /* init GPIO for meters */
39
40   for( ;; )
41   {
42
43     basementGasCalculation();
44
45     portENTER_CRITICAL();
46             // example: do stuff
47     portEXIT_CRITICAL();
48
49     /* We did not receive a pulse, and there was no periodic
50         processing to perform.  Block for a fixed period.  If a pulse
51         is received during this period we will be woken by the ISR
52         giving us the Semaphore. */
53     xLastMeterTaskRunTime = xTaskGetTickCount(); /* update last run time */
54
55     rtcHOURS = (RTC_CTIME0 & MASKHR)>>16;                                       // Read Hour
56     rtcMINUTES = (RTC_CTIME0 & MASKMIN)>>8;                                     // Read Minute
57     rtcSECONDS = RTC_CTIME0 & MASKSEC;                                          // Read Second
58
59     xSemaphoreTake( xMetersSemaphore, 1000 ); /* timeout 1 sec */
60
61           /* Yield in case cooperative scheduling is being used. */
62           #if configUSE_PREEMPTION == 0
63           {
64                   taskYIELD();
65           }
66           #endif
67   }
68 }
69
70
71
72 void vStartMetersTask( unsigned portBASE_TYPE uxPriority )
73 {
74
75   initMeterItems();
76   /* Spawn the task. */
77   xTaskCreate( vMeters_Task, ( signed portCHAR * ) "Meters", metersSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
78 }
79
80 void vP2_0_ISR_Wrapper( void );
81
82 // configure port-pins for use with LAN-controller,
83 // reset it and send the configuration-sequence
84
85 portBASE_TYPE Init_P2_0(void)
86 {
87   // not needed yet :   SCS |= 1; // Enable FAST GPIO
88   portBASE_TYPE xReturn = 1; // return value
89
90   /* Enable Pins. */
91
92   PINSEL4   &= 0xfffff3ff; /* reset bit 20 and 21, P2.10 = GPIO Port 2.10 */
93   PINMODE4  &= 0xfffff3ff; /* reset bit 20 and 21, enable on-chip pull-up resistor on Port 2.10 */
94   IO2_INT_EN_R |= (1<<10); /* enable rising edge interrupt for P2:10 */
95   IO2_INT_EN_F &= ~(1<<10); /* disable falling edge interrupt for P2:10 */
96
97   VICVectAddr17 = (portLONG) vP2_0_ISR_Wrapper; /* EINT-3 interrupt handler */
98
99   VICIntEnable |= (1<<17); /* Enable VIC index-17 (shared by EINT3 and GPIO) */
100
101   return xReturn;
102 }
103
104
105 void basementGasCalculation(void)
106 {
107
108 }
109
110 void initMeterItems(void)
111 {
112   int i;
113   for (i = 0; i < NUMBER_OF_METERS; i++)
114   {
115     meterItems[i].meterEnabled = 0; // disable all meters
116   }
117
118   // Init Gas meter
119   meterItems[METER_INDEX_GAS].meterEnabled = 1;  /* 0: item not used, 1: meter enabled */
120   meterItems[METER_INDEX_GAS].timeLastTick = getEpochTimeWithMs(); /* when did the last tick occur? epoch (seconds) */
121   meterItems[METER_INDEX_GAS].timeBeforeLastTick = getEpochTimeWithMs(); /* when did the tick before last tick occur? epoch (seconds) */
122   meterItems[METER_INDEX_GAS].valueLastTick = 0;   /* what value did we have at the last tick? */
123   meterItems[METER_INDEX_GAS].valueBeforeLastTick = 1;   /* what value did we have at the tick before the last tick? */
124
125 }
Note: See TracBrowser for help on using the browser.