hdo  beta
/home/mrazik/Documents/web/old/hdo/main.c
Zobrazit dokumentaci tohoto souboru.
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, Atmel Corporation, Mrazík
00005  * ----------------------------------------------------------------------------
00006  */
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 //------------------------------------------------------------------------------
00100 
00101 
00102 
00103 //------------------------------------------------------------------------------
00104 //         Headers
00105 //------------------------------------------------------------------------------
00106 
00107 #include <board.h>
00108 #include <pio/pio.h>
00109 #include <pit/pit.h>
00110 #include <dbgu/dbgu.h>
00111 #include <aic/aic.h>
00112 #include <utility/trace.h>
00113 #include <utility/led.h>
00114 #include <adc/adc.h>
00115 
00116 #include <stdio.h>
00117 #include "goertzel.h"
00118 #include "hdo.h"
00119 
00121 #define BOARD_ADC_FREQ 6000000
00122 
00123 #define PIT_PERIOD          1000
00124 //------------------------------------------------------------------------------
00125 //         Local variables
00126 //------------------------------------------------------------------------------
00127 
00129 static const Pin pinsADC[] = {PINS_ADC};
00131 static volatile unsigned int timestamp = 0;
00132 
00133 static volatile unsigned int counter = 0;   
00134 static short result;                        
00135 static short buf_a [GOERTZEL_N];            
00136 static short buf_b [GOERTZEL_N];            
00137 static volatile int   pswitch = 0;          
00138 static volatile int   change  = 0;          
00139 
00140 #define ADC_NUM_1  ADC_CHANNEL_4
00141 //------------------------------------------------------------------------------
00142 //         Local functions
00143 //------------------------------------------------------------------------------
00144 //------------------------------------------------------------------------------
00146 //------------------------------------------------------------------------------
00147 static void ISR_Pit (void) {
00148   unsigned int status;
00149 
00150   // Read the PIT status register
00151   status = PIT_GetStatus() & AT91C_PITC_PITS;
00152   if (status != 0) {
00153     // Read the PIVR to acknowledge interrupt and get number of ticks
00154     timestamp += (PIT_GetPIVR() >> 20);
00155   }
00156   ADC_EnableIt (AT91C_BASE_ADC, ADC_NUM_1);
00157     // Start measurement
00158   ADC_StartConversion (AT91C_BASE_ADC);
00159 }
00160 
00161 //------------------------------------------------------------------------------
00165 //------------------------------------------------------------------------------
00166 static void ISR_Adc (void) {
00167   unsigned int status;
00168   unsigned int id_channel = ADC_NUM_1;
00169 
00170   status = ADC_GetStatus (AT91C_BASE_ADC);
00171   TRACE_DEBUG ("status = 0x%X\n\r", status);
00172 
00173   if (ADC_IsChannelInterruptStatusSet (status, id_channel)) {
00174 
00175     TRACE_DEBUG ("channel %d\n\r", id_channel);
00176     ADC_DisableIt (AT91C_BASE_ADC, id_channel);
00177     result  = ADC_GetConvertedData (AT91C_BASE_ADC, id_channel);
00178     result -= 512;
00179     if (pswitch == 0) buf_a [counter] = result;
00180     else              buf_b [counter] = result;
00181     counter++;
00182     if (counter == GOERTZEL_N) {
00183       counter = 0;
00184       if (pswitch == 0) pswitch = 1;
00185       else              pswitch = 0;
00186       change = 1;
00187     }
00188   }
00189 }
00190 
00191 //------------------------------------------------------------------------------
00194 //------------------------------------------------------------------------------
00195 static void ConfigurePit (void) {
00196   // Initialize the PIT to the desired frequency
00197   PIT_Init (PIT_PERIOD, BOARD_MCK / 1000000);
00198 
00199   // Configure interrupt on PIT
00200   AIC_DisableIT (AT91C_ID_SYS);
00201   AIC_ConfigureIT (AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, ISR_Pit);
00202   AIC_EnableIT (AT91C_ID_SYS);
00203   PIT_EnableIT();
00204 
00205   // Enable the pit
00206   PIT_Enable();
00207 }
00208 //------------------------------------------------------------------------------
00209 //         Global functions
00210 //------------------------------------------------------------------------------
00211 
00212 //------------------------------------------------------------------------------
00214 //------------------------------------------------------------------------------
00215 int main (void) {
00216   char telegram [64];
00217   int  coeff;
00218   int  gresult;
00219   int  tmp;
00220 
00221   TRACE_CONFIGURE (DBGU_STANDARD, 115200, BOARD_MCK);
00222   printf ("-- ADC Projekt HDO %s --\n\r", SOFTPACK_VERSION);
00223   printf ("-- %s\n\r", BOARD_NAME);
00224   printf ("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
00225 
00226   LED_Configure (0);
00227   LED_Configure (1);
00228   LED_Configure (2);
00229   LED_Configure (3);
00230 
00231   PIO_Configure (pinsADC, PIO_LISTSIZE (pinsADC));
00232 
00233   ADC_Initialize (AT91C_BASE_ADC,
00234                   AT91C_ID_ADC,
00235                   AT91C_ADC_TRGEN_DIS,
00236                   0,
00237                   AT91C_ADC_SLEEP_NORMAL_MODE,
00238                   AT91C_ADC_LOWRES_10_BIT,
00239                   BOARD_MCK,
00240                   BOARD_ADC_FREQ,
00241                   10,
00242                   1200);
00243 
00244   ADC_EnableChannel (AT91C_BASE_ADC, ADC_NUM_1);
00245 
00246   ConfigurePit();
00247 
00248   AIC_ConfigureIT (AT91C_ID_ADC, 0, ISR_Adc);
00249   AIC_EnableIT (AT91C_ID_ADC);
00250   coeff = calc_coeff (0.2166);
00251   hdo_init (300);
00252 
00253   //printf ("-I- Press any key to perform a measurement on the ADC ...\n\r");
00254 
00255   // Wait for user input
00256   //DBGU_GetChar();
00257 
00258   // Infinite loop
00259   while (1) {
00260 
00261     if (pswitch == 0) {
00262       gresult = goertzel (coeff, buf_b, GOERTZEL_N);
00263       LED_Set (0);
00264     }
00265     else {
00266       gresult = goertzel (coeff, buf_a, GOERTZEL_N);
00267       LED_Clear (0);
00268     }
00269     if    ( change) TRACE_ERROR ("Nestiham !\r\n");
00270     while (!change);
00271     change = 0;
00272 
00273     tmp = tlg_decode (gresult, telegram);
00274     if (tmp) {
00275       printf (": %s\r\n", telegram);
00276     }
00277     if (tmp >= 0) {
00278       tmp = hdo_action (telegram, "A1B2DP2");
00279       if      (tmp == +1) LED_Set   (2);
00280       else if (tmp == -1) LED_Clear (2);
00281       tmp = hdo_action (telegram, "A1B2DP4");
00282       if      (tmp == +1) LED_Set   (3);
00283       else if (tmp == -1) LED_Clear (3);
00284     }
00285     if (gresult > 150) LED_Set   (1);
00286     else               LED_Clear (1);
00287 
00288     timestamp = 0;
00289   }
00290 }
00291