-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
679 lines (550 loc) · 17.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/********************************************************************
Fichier : main.c
---------------------------------------------------------------------
Auteur : ISMAIL BEN SALAH
Compilateur : MPLAB X
Date : 20.03.2014
Version : 2.05
---------------------------------------------------------------------
Description : Fonctionnement du capteur de température.
--------------------------Modifications------------------------------
Version : 1.8 - 13.08.2014
Modification : Fonctionnement de l'écriture de flottant sur la mémoire
*********************************************************************/
#include "General_func.h"
#include <stdio.h>
#include <stdlib.h>
#include <plib.h>
#include <p32xxxx.h>
#include <conio.h>
#include <proc/p32mx795f512l.h>
#include <xc.h>
#include "KTY_81_110.h"
#include "SPI_function.h"
#include "TL2350R.h"
#include "Bluetooth_module.h"
#include "RTCC.h"
#include "User_Gestion.h"
#include "Timer1.h"
#include "Gestion_Chauffage.h"
#include "Gestion_Porte.h"
#include "Notes_Gestion.h"
//Definitions
#define UART
#define BUFFER_SIZE 128
#undef DEBUG
#undef MODULE2
#define CREATEUSER 0
#define CHECKLOGIN 1
#define GETUSERS 2
#define DELETEUSER 3
#define ADMIN_ADD_USER 4
#define MODIFYPASSWORD 5
#define MODIFYUSERNAME 6
#define OPENDOOR 7
#define CHANGEDOORPASS 8
#define ADDNOTE 9
#define DELETENOTE 10
#define READALLNOTE 11
#define READPERSONALNOTE 12
#define READPUBLICNOTE 13
#define ADD_SCHEDULING 14
#define DELETE_SCHEDULING 15
#define SETTEMPCONSIGN 16
#define ACTIVEBUZZER 17
#define WRITEBYTE 20
#define READBYTE 21
#define FLOATWRITE 22
#define FLOATREAD 23
#define STRINGWRITE 24
#define STRINGREAD 25
#define CHIPERASE 26
#define CHECKUSER 30
#define LED4ON 31
#define LED4OFF 32
#define LED5ON 33
#define LED5OFF 34
#define CHECKDOORPASSWORD 35
#define READTEMP 36
#define GETTIME 37
#define READLOGADRESS 38
#define LISTINIT 70
#define INITNOTE 71
#define INITCHAUFFAGE 100
#define SETCHAUFFAGETEMP 101
#define GETCHAUFFAGEHISTORY 102
#define ADDCHAUFFAGETEMP 103
#define INIT_SYSTEM 200
#define DISCONNECT 236
//Prototypes de fonctions
void WriteByte (void);
void ReadByte (void);
void FloatWrite (void);
void FloatRead (void);
void StringWrite(void);
void StringRead (void);
void GetTime (void);
void GetLight (void);
/*Configurations Bits*/
// PIC32MX795F512L Configuration Bit Settings
// DEVCFG3
// USERID = No Setting
#pragma config FSRSSEL = PRIORITY_0 // SRS Select (SRS Priority 0)
#pragma config FMIIEN = OFF // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config FCANIO = OFF // CAN I/O Pin Select (Alternate CAN I/O)
#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO= OFF // USB VBUS ON Selection (Controlled by Port Function)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_18 // PLL Multiplier (18x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
// DEVCFG1
#pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
//Variables globales externes
extern char buffer [BUFFER_SIZE];
extern char user [BUFFER_SIZE];
extern char password [BUFFER_SIZE];
extern char function [BUFFER_SIZE];
extern char done;
extern char cnt_debug;
extern int CMD;
extern char buffer_s [BUFFER_SIZE];
extern unsigned char isLogged;
//Variables globales
union myadress var;
int count = 1;
int count2 = 1;
int count3 = 1;
char buffer2 [BUFFER_SIZE];
void Init_module (void)
{
//Fonction d'initialisation
InitADC_R();
initUART3A();
SPI2Init();
RTCCInit();
TimerInit();
InitPWM();
isLogged = 0;
//Configuration des interrupts
// Configure UART RX Interrupt
INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID),INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID),INT_PRIORITY_LEVEL_4);
//Configure Timer 1 Interrupt
//ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
//Configure Timer 45 Interrupt
//ConfigIntTimer45(T45_INT_ON | T45_INT_PRIOR_3);
//configure interrupt capteur de mouvement.
// mINT0SetIntPriority(1); //On set la priorité de l'interrupt du capteur de mouvement.
// mINT0IntEnable(0); //On s'assure que l'interrupt du capteur de mouvement est désactivée au démarrage.
// INTCONbits.INT0EP = 1; // L'interrupt du capteur de mouvement s'activera lors des fronts montants uniquement.
// Enable multi-vector interrupts
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
INTEnableInterrupts();
//Initialisaiton des buffers
CMD = 5555;
sprintf(buffer,"");
sprintf(user,"");
sprintf(password,"");
sprintf(function,"");
sprintf(buffer_s,"");
done = 0;
}
int main(int argc, char** argv)
{
Init_module();
char p_subject [9];
char p_user[9];
char p_date[11];
float p_temp;
float value;
/*Configuration des entrées sorties*/
PORTSetPinsDigitalOut(IOPORT_C,BIT_1);
PORTSetPinsDigitalOut(IOPORT_A,BIT_3);
PORTSetPinsDigitalOut(IOPORT_E,BIT_0|BIT_1|BIT_2|BIT_3); // On indique que les bits 0 à 3 du port E sont des sorties
PORTSetPinsDigitalIn(IOPORT_E,BIT_4|BIT_5|BIT_6|BIT_7); // On idique que les bits 4 à 7 du port E sont des entrées
PORTSetPinsDigitalOut(IOPORT_G,BIT_6|BIT_8|BIT_9); // On indique que les bits 6,8 et 9du port E sont des sorties
PORTSetPinsDigitalIn(IOPORT_G,BIT_7); // On indique que le bit 7 du port G sont des sorties
/**/
#ifdef UART
LED5 = 0;
LED4 = 0;
putsU3("Hello it's the Smarthome system\n");
while(1)
{
if (done == 1)
{
#ifdef DEBUG
sprintf(buffer,"CMD : %d PARAM : %s\n",CMD,function);
putsU3(buffer);
isLogged = 1;
#endif
}
#ifndef DEBUG
if(done==1 && isLogged == 0)
{
switch (CMD)
{
case CREATEUSER:
{
//test du rajout d'utilisateur
sscanf(function,"%s,%s",buffer2,password);
//Ecriture du compte en mémoire
AddUser(buffer2,password);
break;
}
case CHECKLOGIN:
{
sscanf(function,"%s",password);
CheckLogin(user,password);
done = 0;
break;
}
}
}
#endif
if(done==1 && isLogged == 1){
switch (CMD)
{
case CHECKUSER:
{
CheckLogin(user,password);
break;
}
case LED4ON:
{
LED4 = 1;
putsU3("La led 4 est allumée\n\r");
break;
}
case LED4OFF:
{
LED4 = 0;
putsU3("La led 4 est éteinte\n\r");
break;
}
case LED5ON:
{
LED5 = 1;
putsU3("La led 5 est allumée\n\r");
break;
}
case LED5OFF:
{
LED5 = 0;
putsU3("La led 5 est éteinte\n\r");
break;
}
case OPENDOOR:
{
int temp;
//Command to check if the user have send the right pin code
sscanf(function,"%d",&temp);
OpenDoor(temp);
break;
}
case CHANGEDOORPASS:
{
int temp;
//Pour set le code pin de la porte
sscanf(function,"%d",&temp);
SetDoorCode(temp);
break;
}
case ADDNOTE:
{
//Fonction d'ajout d'une note
sscanf(function,"%s,%s,%s,%s",p_subject,buffer2,p_user,p_date);
AddNote(p_subject,buffer2,p_user,p_date);
break;
}
case DELETENOTE:
{
//Fonction de suppresion d'une note
sscanf(function,"%s,%s",p_subject,p_user);
DeleteNote(p_subject,p_user);
break;
}
case READALLNOTE:
{
//Fonction de lecture d'une note
sscanf(function,"%s,%s",p_subject,p_user);
ReadNoteAll();
break;
}
case READPERSONALNOTE:
{
//Fonction de lecture d'une note
sscanf(function,"%s",p_user);
ReadPersonnalNotes(p_user);
break;
}
case READPUBLICNOTE:
{
ReadPublicNotes();
break;
}
case CHECKDOORPASSWORD:
{
int temp;
//Command to check if the user have send the right pin code
sscanf(function,"%d",&temp);
CheckDoorPassword(temp);
break;
}
case READTEMP:
{
float temp = read_kty_81_220(0); //0 pour température intérieure.
sprintf(buffer,"la température actuelle est de %f °C",temp);
putsU3(buffer);
break;
}
case WRITEBYTE:
{
WriteByte();
break;
}
case READBYTE:
{
ReadByte();
break;
}
case FLOATWRITE:
{
FloatWrite();
break;
}
case FLOATREAD:
{
FloatRead();
break;
}
case STRINGWRITE:
{
StringWrite();
break;
}
case STRINGREAD:
{
StringRead();
break;
}
case CHIPERASE:
{
CE_cmd();
putsU3("CHIP ERASED");
break;
}
case GETTIME:
{
GetTime();
break;
}
case 50:
{
break;
}
case GETUSERS:
{
GetUsers();
break;
}
case ADMIN_ADD_USER:
{
//test du rajout d'utilisateur
sscanf(function,"%s,%s",buffer2,password);
//Ecriture du compte en mémoire
AddUser(buffer2,password);
break;
}
case DELETEUSER:
{
//test du rajout d'utilisateur
sscanf(function,"%s",buffer2);
//Ecriture du compte en mémoire
DeleteUser(buffer2);
break;
}
case MODIFYPASSWORD:
{
//Test de la modification du mot de passe.
sscanf(function,"%s,%s",buffer2,buffer_s);
//Modification du mot de passe.
ModifyPassWord(buffer2,buffer_s);
break;
}
case CHECKLOGIN:
{
//test de la vérification d'un USER
sscanf(function,"%s,%s",buffer2,buffer_s);
//Ecriture du compte en mémoire
CheckLogin(buffer2,buffer_s);
break;
}
case MODIFYUSERNAME:
{
//Modification du username
sscanf(function,"%s,%s",buffer2,buffer_s);
//Modification du Username
ModifiyUsername(buffer2,buffer_s);
break;
}
case LISTINIT:
{
ListInit();
break;
}
case INITNOTE:
{
//Initialisation
InitNote();
break;
}
case INITCHAUFFAGE:
{
Init_Gestion_Chauffage();
break;
}
case SETCHAUFFAGETEMP:
{
//Fonction pour changer la consigne
sscanf(function,"%f",&p_temp);
break;
}
case ADDCHAUFFAGETEMP:
{
Add_temperature();
break;
}
case GETCHAUFFAGEHISTORY:
{
Get_Historique();
break;
}
case ACTIVEBUZZER:
{
int i;
SetDCOC4PWM(1023);
for(i=0;i<0xffff;i++);
SetDCOC4PWM(0);
break;
}
case INIT_SYSTEM:
{
Init_user_gestion();
InitNote();
Init_Gestion_Chauffage();
break;
}
case DISCONNECT:
{
isLogged = 0;
break;
}
default:
{
}
}
done=0;
}
#ifdef MODULE2
gestion_chauffage(p_temp); //La température demandée est une constante pour l'instant.
#endif
}
#endif
}
// UART 2 interrupt handler, set at priority level 4
void __ISR(_UART2_VECTOR, ipl4) IntUart2Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART3A)))
{
INTClearFlag(INT_SOURCE_UART_RX(UART3A)); // Clear the RX interrupt Flag
INTDisableInterrupts(); //On désactive les interrupts le temps de l'acquisiton
memset(buffer,0,128); //On vide le buffer
memset(function,0,128); //On remet la chaine des paramètres à zéro
memset(buffer_s,0,128); //On remet le buffer à zéro.
memset(user,0,128); //ON remet le user à zéro.
memset(password,0,128); //ON remet le user à zéro.
CMD = 8012; //On remet la variable par défaut
if(U2STAbits.FERR == 0 && U2STAbits.PERR == 0 )
{
putU3('K');
getsU3(buffer,128); //On récupère le message
done=1; //On indique qu'une transmission c'est faite
}
INTEnableInterrupts(); //On réactive les interrupts
INTClearFlag(INT_SOURCE_UART_RX(UART3A)); // Clear the RX interrupt Flag
sscanf(buffer,"%s,%d,%s",user,&CMD,function);
//printf("user : %s \n cmd : %d \n function : %s \n",user,CMD,function);
}
// We don't care about TX interrupt
if ( INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)) )
{
INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
}
}
//TIMER 1 Interrupt Handler,set at priotity level 2
void __ISR (_TIMER_1_VECTOR,ipl2) IntTimer1Handler(void)
{
if(count>643)// test effectué 643 fois -> 643 fois sec = 300 sec
//2.145 fois permet d'avoir une seconde.
{
count=1; // reset du compteur
mT1ClearIntFlag();
}
else
{
count ++;
mT1ClearIntFlag();
}
}
// Interruption appelée par le capeur de mouvement. (Pin 3)
//_EXTERNAL_0_VECTOR est le vecteur pour le capteur de mouvement
// ipl7 est le niveau de priorité le plus haut: le premier à être servi.
/*void __ISR(_TIMER_45_VECTOR,ipl2) IntTimer45Handler(void)
{
if(count2 > 45)
{
mINT0IntEnable(1); //On active l'interrupt du capteur de mouvement.
mT45IntEnable(0); //On désactive l'interrupt du timer 45.
mT45SetIntPriority(3);
mT45IntEnable(1);
}
else
{
count2++;
}
mT45ClearIntFlag();
}*/
//Timer 45 Interrupt Handler,set at priority level 3
void __ISR(_TIMER_45_VECTOR,ipl3) IntTimer45Handler(void)
{
if(count3>10)
{
Add_temperature();
count3 = 1;
}
else
{
count3++;
}
mT45ClearIntFlag();
}