00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <string.h>
00047
00048 #include "compiler.h"
00049 #include "gpio.h"
00050
00051 #include "FreeRTOS.h"
00052 #include "task.h"
00053
00054 #include "datalog.h"
00055 #include "shell.h"
00056 #include "board.h"
00057 #include "sensor.h"
00058 #include "MMI.h"
00059 #include "pushb.h"
00060
00061 #include "config_file.h"
00062 #include "fsaccess.h"
00063
00064 #include "BasicSMTP.h"
00065
00066
00067
00069 #define PUSHB_GETCONF_MAXLEN 12
00070
00072 #define PUSHB_EVENT_PRESS TRUE
00073 #define PUSHB_EVENT_RELEASE FALSE
00074
00075 #define SENSOR_PB1_CONFIG_FILE "A:/CFG/PB1.TXT"
00076 #define SENSOR_PB2_CONFIG_FILE "A:/CFG/PB2.TXT"
00077 #define SENSOR_PB3_CONFIG_FILE "A:/CFG/PB3.TXT"
00078
00079 #define PB1_POSITION GPIO_PUSH_BUTTON_0
00080 #define PB2_POSITION GPIO_PUSH_BUTTON_1
00081 #define PB3_POSITION GPIO_PUSH_BUTTON_2
00082
00083
00084
00085
00087 const signed portCHAR *acpc_pushb_events[2] = {
00088 (signed portCHAR *)"RELEASE",
00089 (signed portCHAR *)"PRESS"
00090 };
00091
00093 static Bool b_pushb1 = PUSHB_EVENT_RELEASE;
00094 static Bool b_pushb2 = PUSHB_EVENT_RELEASE;
00095 static Bool b_pushb3 = PUSHB_EVENT_RELEASE;
00096
00098 static Bool bAlarm1 = pdFALSE;
00099 static Bool bAlarm2 = pdFALSE;
00100 static Bool bAlarm3 = pdFALSE;
00101
00103 extern xSemaphoreHandle xCFGMutex;
00104
00105
00106
00107
00108 #if defined(__GNUC__)
00109 __attribute__((__naked__))
00110 #elif defined(__ICCAVR32__)
00111 #pragma shadow_registers = full // Naked.
00112 #endif
00113 void vpushb_ISR( void );
00114
00115
00116 #ifdef __GNUC__
00117 __attribute__((__noinline__))
00118 #endif
00119 static portBASE_TYPE prvpushb_ISR_NonNakedBehaviour( void );
00120
00121 static eExecStatus prv_e_pushb_get_config( signed portCHAR **ppcStringReply,
00122 Bool bAlarm );
00123
00124 static eExecStatus prv_e_pushb_set_config(Bool * bAlarm, portCHAR * filename, signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] );
00125
00126
00127
00133 Bool b_pushb1_init ( void )
00134 {
00135 portCHAR token[6];
00136
00137
00138 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 20 ) )
00139 {
00140
00141 if (config_file_get_value(SENSOR_PB1_CONFIG_FILE, "alarm" , token) >= 0)
00142 {
00143
00144 if (!strcmp(token, "on"))
00145 {
00146 bAlarm1 = pdTRUE;
00147 }
00148 }
00149
00150 x_supervisor_SemaphoreGive( xCFGMutex );
00151 }
00152
00153 gpio_enable_pin_interrupt(PB1_POSITION , GPIO_PIN_CHANGE);
00154
00155 Disable_global_interrupt();
00156
00157 INTC_register_interrupt( (__int_handler)&vpushb_ISR, AVR32_GPIO_IRQ_0 + (PB1_POSITION/8), AVR32_INTC_INT3);
00158
00159 Enable_global_interrupt();
00160 return (TRUE);
00161 }
00162
00166 void v_pushb1_stop( void )
00167 {
00168
00169 gpio_disable_pin_interrupt( PB1_POSITION );
00170 }
00171
00177 Bool b_pushb2_init ( void )
00178 {
00179 portCHAR token[6];
00180
00181
00182 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 20 ) )
00183 {
00184
00185 if (config_file_get_value(SENSOR_PB2_CONFIG_FILE, "alarm" , token) >= 0)
00186 {
00187
00188 if (!strcmp(token, "on"))
00189 {
00190 bAlarm2 = pdTRUE;
00191 }
00192 }
00193
00194 x_supervisor_SemaphoreGive( xCFGMutex );
00195 }
00196
00197 gpio_enable_pin_interrupt(PB2_POSITION , GPIO_PIN_CHANGE);
00198
00199 Disable_global_interrupt();
00200
00201 INTC_register_interrupt( (__int_handler)&vpushb_ISR, AVR32_GPIO_IRQ_0 + (PB2_POSITION/8), AVR32_INTC_INT3);
00202
00203 Enable_global_interrupt();
00204 return (TRUE);
00205 }
00206
00210 void v_pushb2_stop( void )
00211 {
00212
00213 gpio_disable_pin_interrupt( PB2_POSITION );
00214 }
00215
00221 Bool b_pushb3_init ( void )
00222 {
00223 portCHAR token[6];
00224
00225
00226 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 20 ) )
00227 {
00228
00229 if (config_file_get_value(SENSOR_PB3_CONFIG_FILE, "alarm" , token) >= 0)
00230 {
00231
00232 if (!strcmp(token, "on"))
00233 {
00234 bAlarm3 = pdTRUE;
00235 }
00236 }
00237
00238 x_supervisor_SemaphoreGive( xCFGMutex );
00239 }
00240
00241 gpio_enable_pin_interrupt(PB3_POSITION , GPIO_PIN_CHANGE);
00242
00243 Disable_global_interrupt();
00244
00245 INTC_register_interrupt( (__int_handler)&vpushb_ISR, AVR32_GPIO_IRQ_0 + (PB3_POSITION/8), AVR32_INTC_INT3);
00246
00247 Enable_global_interrupt();
00248 return (TRUE);
00249 }
00250
00254 void v_pushb3_stop( void )
00255 {
00256
00257 gpio_disable_pin_interrupt( PB3_POSITION );
00258 }
00259
00269 eExecStatus e_pushb1_get_config( signed portCHAR **ppcStringReply )
00270 {
00271 return( prv_e_pushb_get_config( ppcStringReply, bAlarm1 ) );
00272 }
00273
00274
00284 eExecStatus e_pushb2_get_config( signed portCHAR **ppcStringReply )
00285 {
00286 return( prv_e_pushb_get_config( ppcStringReply, bAlarm2 ) );
00287 }
00288
00289
00299 eExecStatus e_pushb3_get_config( signed portCHAR **ppcStringReply )
00300 {
00301 return( prv_e_pushb_get_config( ppcStringReply, bAlarm3 ) );
00302 }
00303
00304
00318 eExecStatus e_pushb1_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
00319 {
00320 return (prv_e_pushb_set_config(&bAlarm1, SENSOR_PB1_CONFIG_FILE, ppcStringReply, ac, av));
00321 }
00322
00333 eExecStatus e_pushb2_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
00334 {
00335 return (prv_e_pushb_set_config(&bAlarm2, SENSOR_PB2_CONFIG_FILE, ppcStringReply, ac, av));
00336 }
00337
00348 eExecStatus e_pushb3_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
00349 {
00350 return (prv_e_pushb_set_config(&bAlarm3, SENSOR_PB3_CONFIG_FILE, ppcStringReply, ac, av));
00351 }
00352
00360 Bool b_pushb1_get_value( xLogDef *pxLog )
00361 {
00362
00363 pxLog->pfFreeStringLog = NULL;
00364 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb1];
00365
00366 return( TRUE );
00367 }
00368
00376 Bool b_pushb2_get_value( xLogDef *pxLog )
00377 {
00378
00379 pxLog->pfFreeStringLog = NULL;
00380 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb2];
00381
00382 return( TRUE );
00383 }
00384
00392 Bool b_pushb3_get_value( xLogDef *pxLog )
00393 {
00394
00395 pxLog->pfFreeStringLog = NULL;
00396 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb3];
00397
00398 return( TRUE );
00399 }
00400
00401
00402
00403
00404
00405
00413 static eExecStatus prv_e_pushb_get_config( signed portCHAR **ppcStringReply,
00414 Bool bAlarm )
00415 {
00416 if ( bAlarm == TRUE )
00417 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00418 else
00419 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00420 return( SHELL_EXECSTATUS_OK_NO_FREE );
00421 }
00422
00433 static eExecStatus prv_e_pushb_set_config(Bool * bAlarm, portCHAR * filename, signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
00434 {
00435 if (config_file_set_value(filename, ac, av) != 0)
00436 {
00437 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00438
00439 return( SHELL_EXECSTATUS_KO );
00440 }
00441 if (!strcmp((char *)av[1] , "on"))
00442 {
00443 *bAlarm = pdTRUE;
00444 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00445 return( SHELL_EXECSTATUS_OK_NO_FREE );
00446 }
00447 else if (!strcmp( (char *)av[1], "off"))
00448 {
00449 *bAlarm = pdFALSE;
00450 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00451 return( SHELL_EXECSTATUS_OK_NO_FREE );
00452 }
00453 else
00454 {
00455 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00456 return( SHELL_EXECSTATUS_KO );
00457 }
00458 }
00459
00463 #if defined(__GNUC__)
00464 __attribute__((__naked__))
00465 #elif defined(__ICCAVR32__)
00466 #pragma shadow_registers = full // Naked.
00467 #endif
00468 void vpushb_ISR( void )
00469 {
00470
00471
00472
00473 portENTER_SWITCHING_ISR();
00474
00475 prvpushb_ISR_NonNakedBehaviour();
00476
00477 portEXIT_SWITCHING_ISR();
00478 }
00479
00483 #if defined(__GNUC__)
00484 __attribute__((__noinline__))
00485 #elif defined(__ICCAVR32__)
00486 #pragma optimize = no_inline
00487 #endif
00488 static portBASE_TYPE prvpushb_ISR_NonNakedBehaviour( void )
00489 {
00490 xLogDef *pxLog;
00491 portBASE_TYPE xSwitchRequired = pdFALSE;
00492
00493
00494 if (gpio_get_pin_interrupt_flag(PB1_POSITION))
00495 {
00496
00497 if (gpio_get_pin_value(PB1_POSITION))
00498 {
00499 b_pushb1 = PUSHB_EVENT_RELEASE;
00500 }
00501 else
00502 {
00503 b_pushb1 = PUSHB_EVENT_PRESS;
00504 if ( bAlarm1 == pdTRUE )
00505 {
00506
00507 v_SMTP_PostFromISR("Push Button 1 Alarm", NULL);
00508 }
00509 }
00510
00511 pxLog = pxdatalog_log_alloc_init_FromISR();
00512 if( NULL != pxLog )
00513 {
00514
00515 pxLog->id = DATALOG_ID_PB1;
00516
00517 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb1];
00518
00519 pxLog->pfFreeStringLog = NULL;
00520
00521 xSwitchRequired = x_datalog_AddLogFromISR( pxLog );
00522 }
00523
00524 gpio_clear_pin_interrupt_flag(PB1_POSITION);
00525 }
00526 if (gpio_get_pin_interrupt_flag(PB2_POSITION))
00527 {
00528
00529 if (gpio_get_pin_value(PB2_POSITION))
00530 {
00531 b_pushb2 = PUSHB_EVENT_RELEASE;
00532 }
00533 else
00534 {
00535 b_pushb2 = PUSHB_EVENT_PRESS;
00536 if ( bAlarm2 == pdTRUE )
00537 {
00538
00539 v_SMTP_PostFromISR("Push Button 2 Alarm", NULL);
00540 }
00541 }
00542
00543 pxLog = pxdatalog_log_alloc_init_FromISR();
00544 if( NULL != pxLog )
00545 {
00546
00547 pxLog->id = DATALOG_ID_PB2;
00548
00549 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb2];
00550
00551 pxLog->pfFreeStringLog = NULL;
00552
00553 xSwitchRequired = x_datalog_AddLogFromISR( pxLog );
00554 }
00555
00556 gpio_clear_pin_interrupt_flag(PB2_POSITION);
00557 }
00558 if (gpio_get_pin_interrupt_flag(PB3_POSITION))
00559 {
00560
00561 if (gpio_get_pin_value(PB3_POSITION))
00562 {
00563 b_pushb3 = PUSHB_EVENT_RELEASE;
00564 }
00565 else
00566 {
00567 b_pushb3 = PUSHB_EVENT_PRESS;
00568 if ( bAlarm3 == pdTRUE )
00569 {
00570
00571 v_SMTP_PostFromISR("Push Button 3 Alarm", NULL);
00572 }
00573 }
00574
00575 pxLog = pxdatalog_log_alloc_init_FromISR();
00576 if( NULL != pxLog )
00577 {
00578
00579 pxLog->id = DATALOG_ID_PB3;
00580
00581 pxLog->pcStringLog = (portCHAR *)acpc_pushb_events[b_pushb3];
00582
00583 pxLog->pfFreeStringLog = NULL;
00584
00585 xSwitchRequired = x_datalog_AddLogFromISR( pxLog );
00586 }
00587
00588 gpio_clear_pin_interrupt_flag(PB3_POSITION);
00589 }
00590 return( xSwitchRequired );
00591 }