Version initiale
Dependencies: mbed
Fork of Le_Pont_V10116 by
Port_Serie.cpp
00001 /*******************************************************************/ 00002 /* */ 00003 /* Port Série */ 00004 /* */ 00005 /* Gestion du port série par interruption */ 00006 /* */ 00007 /* */ 00008 /*******************************************************************/ 00009 00010 #include "mbed.h" 00011 #include "Port_Serie.h" 00012 #include "Modbus.h" 00013 00014 Serial RS(p28 , p27) ; 00015 Serial PC8(USBTX, USBRX) ; 00016 Timer Chrono_Serie ; 00017 00018 volatile U8 Buffer_Emission_U8[TAILLE_BUFFER_EMISSION] ; 00019 volatile U8 Pointeur_Emission_U8 ; 00020 volatile U8 Nb_Caracteres_A_Emettre_U8 ; 00021 volatile U8 Buffer_Reception_U8[TAILLE_BUFFER_RECEPTION] ; 00022 volatile U8 Pointeur_Reception_U8 ; 00023 volatile U8 Nb_Caracteres_Attendus ; 00024 volatile U8 Numero_Ordre_En_Reception_U8 ; 00025 volatile U8 Numero_Ordre_En_Emission_U8 ; 00026 volatile U8 Index ; 00027 volatile S32 Date_Dernier_Caractere_S32 ; 00028 volatile S32 Temps_alloue_Reception_S32 ; 00029 static S32 Temps_alloue_Caractere_S32 ; 00030 00031 /******* Réception sous interruption ***************************/ 00032 void iReception_Serie () 00033 { 00034 U8 Car ; 00035 // Si premier caractere recu, enclenche le chrono 00036 if ( Pointeur_Reception_U8 == 0 ) 00037 { 00038 //Chrono_Serie.start() ; 00039 } 00040 // Si il y a des caractères dans le buffer 00041 while ( RS.readable() ) 00042 {// Réception d'un caractère 00043 Car=RS.getc(); 00044 Buffer_Reception_U8 [ Pointeur_Reception_U8 ] = Car ; 00045 //RS.putc(Car) ; 00046 Pointeur_Reception_U8++ ; 00047 } 00048 if ( ( Pointeur_Reception_U8 > 0 ) 00049 &&( Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 == ATTENTE ) ) 00050 {// Si au moins un caractère recu, la réception est en cours 00051 Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 = RECEPTION ; 00052 } 00053 //PC8.printf("\r\n Serie : Reception %s ", &Buffer_Reception_U8 [0]) ; 00054 00055 //Date_Dernier_Caractere_S32 = Chrono_Serie.read_us() ; 00056 // Analyse de la trame 00057 if ( ( Buffer_Reception_U8 [ 0 ] == SLAVE_ID ) 00058 || ( Buffer_Reception_U8 [ 0 ] == MB_ADDRESSE_BROADCAST ) ) 00059 {// Adresse de l'esclave, ou Adresse de broadcast 00060 //PC8.printf("\r\n Serie : Adresse %i ", Buffer_Reception_U8 [0]) ; 00061 if ( Pointeur_Reception_U8 >= 6 ) 00062 { 00063 //PC8.printf("\r\n Serie : Func %i ", Buffer_Reception_U8 [1]) ; 00064 if (( Buffer_Reception_U8 [ 1 ] == MB_FUNC_READ_HOLDING_REGISTER ) 00065 ||( Buffer_Reception_U8 [ 1 ] == MB_FUNC_READ_INPUT_REGISTER )) 00066 {// Fonction 3 ou 4, Lecture de registres 00067 Nb_Caracteres_Attendus = 8 ; 00068 //PC8.printf("\r\n Serie : Func %i ", Buffer_Reception_U8 [1]) ; 00069 } 00070 else if ( Buffer_Reception_U8 [ 1 ] == MB_FUNC_WRITE_MULTIPLE_REGISTERS ) 00071 {// Fonction 16 (0x10), écriture de registres 00072 Nb_Caracteres_Attendus = 9 + Buffer_Reception_U8 [ 5 ] * 2 ; 00073 } 00074 } 00075 00076 if ( Pointeur_Reception_U8 >= Nb_Caracteres_Attendus ) 00077 { 00078 // On a recu une trame complète 00079 //PC8.printf("\r\n Serie : trame %d complete %d / %d",Numero_Ordre_En_Reception_U8,Pointeur_Reception_U8,Nb_Caracteres_Attendus) ; 00080 00081 Index = 0 ; 00082 //Chrono_Serie.stop() ; 00083 while( Index < Pointeur_Reception_U8 ) 00084 {// Copie la trame dans l'ordre en cours de réception 00085 Ordres[Numero_Ordre_En_Reception_U8].Trame_Recue_aU8[Index] = Buffer_Reception_U8 [ Index ] ; 00086 Index++ ; 00087 } 00088 // Nombre de caractères de l'ordre 00089 Ordres[Numero_Ordre_En_Reception_U8].Nb_Caracteres_Recus_U8 = Pointeur_Reception_U8 ; 00090 // Fin de réception 00091 Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 = RECU ; 00092 00093 } 00094 00095 } 00096 else 00097 { 00098 // Le message n'est pas pour nous, on purge le buffer 00099 Pointeur_Reception_U8 = 0 ; 00100 Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 = ATTENTE ; 00101 Nb_Caracteres_Attendus = TAILLE_BUFFER_RECEPTION ; 00102 //Chrono_Serie.stop() ; 00103 PC8.printf("\r\n Serie : pas pour nous %i ",Buffer_Reception_U8 [ 1 ]) ; 00104 } 00105 00106 } 00107 /************ Enclenchement de la réception *****************/ 00108 void vPort_Serie_Reception ( U8 Numero_Ordre_U8 ) 00109 { 00110 if ( (Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 != RECEPTION ) 00111 && (Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 != ATTENTE )) 00112 {// La réception en cours est terminée 00113 if ( ( Ordres[Numero_Ordre_U8].Etat_U8 == RECU ) 00114 ||( Ordres[Numero_Ordre_U8].Etat_U8 == TRAITE ) 00115 ||( Ordres[Numero_Ordre_U8].Etat_U8 == ARRET ) ) 00116 {// L'ordre est pret à être recu 00117 Numero_Ordre_En_Reception_U8 = Numero_Ordre_U8 ; 00118 Ordres[Numero_Ordre_En_Reception_U8].Etat_U8 = ATTENTE ; 00119 Pointeur_Reception_U8 = 0 ; 00120 Nb_Caracteres_Attendus = TAILLE_BUFFER_RECEPTION ; 00121 //PC8.printf("\r\n Debut reception %i Etat %i ",Numero_Ordre_En_Reception_U8,Ordres[Numero_Ordre_En_Reception_U8].Etat_U8) ; 00122 } 00123 //PC8.printf("\r\n Reception1 %i Etat %i ",Numero_Ordre_U8,Ordres[Numero_Ordre_U8].Etat_U8) ; 00124 } 00125 //PC8.printf("\r\n Reception2 %i Etat %i ",Numero_Ordre_En_Reception_U8,Ordres[Numero_Ordre_En_Reception_U8].Etat_U8) ; 00126 } 00127 /************ Emission de la trame ************************/ 00128 void iEmission ( void ) 00129 { 00130 // Si le buffer n'est pas saturé et qu'il reste des caractères à émettre 00131 while ( ( Pointeur_Emission_U8 < Nb_Caracteres_A_Emettre_U8 ) ) 00132 {// Emission d'un caractère 00133 RS.putc( (U8) Buffer_Emission_U8[ Pointeur_Emission_U8 ] ) ; 00134 Pointeur_Emission_U8++ ; 00135 //PC8.printf("\r\n Serie : Emission %i ", Buffer_Emission_U8[ Pointeur_Emission_U8-1 ]) ; 00136 } 00137 if ( Pointeur_Emission_U8 >= Nb_Caracteres_A_Emettre_U8 ) 00138 {// Tous les caractères sont émis, cloture l'émission 00139 Ordres[Numero_Ordre_En_Emission_U8].Etat_U8 = FIN ; 00140 Pointeur_Emission_U8 = 0 ; 00141 Nb_Caracteres_A_Emettre_U8 = 0 ; 00142 } 00143 else 00144 {// Le buffer est saturé, on reviendra plus tard 00145 00146 } 00147 } 00148 /************ Emission de la réponse ************************/ 00149 void vPort_Serie_Emission ( U8 Numero_Ordre_U8 ) 00150 { 00151 U8 Index_U8 ; 00152 00153 00154 if (Ordres[Numero_Ordre_En_Emission_U8].Etat_U8 != EMISSION ) 00155 {// L'émission en cours est terminée 00156 if (Ordres[Numero_Ordre_U8].Etat_U8 == TRAITE ) 00157 {// L'ordre est pret à être émis 00158 Ordres[Numero_Ordre_U8].Etat_U8 = EMISSION ; 00159 Numero_Ordre_En_Emission_U8 = Numero_Ordre_U8 ; 00160 Nb_Caracteres_A_Emettre_U8 = Ordres[Numero_Ordre_U8].Nb_Caracteres_A_Emettre_U8 ; 00161 Index_U8 = 0 ; 00162 // Copie la trame dans le buffer 00163 while( Index_U8 < Nb_Caracteres_A_Emettre_U8 ) 00164 { 00165 Buffer_Emission_U8 [ Index_U8 ] = Ordres[Numero_Ordre_U8].Trame_Reponse_aU8 [Index_U8] ; 00166 Index_U8++ ; 00167 } 00168 Pointeur_Emission_U8 = 0 ; 00169 } 00170 00171 //PC8.printf("\r\n Serie : Emission %i : %i/%i",Numero_Ordre_En_Emission_U8, Pointeur_Emission_U8,Nb_Caracteres_A_Emettre_U8) ; 00172 } 00173 // Emission de la trame 00174 iEmission() ; 00175 } 00176 /************ Initialisation du Port ************************/ 00177 void vPort_Serie_Init(int Baudrate) 00178 { 00179 // Initialisation du port RS 00180 RS.baud(Baudrate) ; 00181 // Purge des buffers 00182 for ( Pointeur_Emission_U8 = 0 ; Pointeur_Emission_U8 < TAILLE_BUFFER_EMISSION ; Pointeur_Emission_U8++ ) 00183 { 00184 Buffer_Emission_U8[Pointeur_Emission_U8] = 0 ; 00185 } 00186 for ( Pointeur_Reception_U8 = 0 ; Pointeur_Reception_U8 < TAILLE_BUFFER_RECEPTION ; Pointeur_Reception_U8++ ) 00187 { 00188 Buffer_Reception_U8[Pointeur_Reception_U8] = 0 ; 00189 } 00190 00191 Pointeur_Emission_U8 = 0 ; 00192 Pointeur_Reception_U8 = 0 ; 00193 Numero_Ordre_En_Reception_U8 = 0 ; 00194 Numero_Ordre_En_Emission_U8 = 0 ; 00195 //PC8.printf("\r\n Serie : Init %i ", Baudrate) ; 00196 // Temps alloué pour 1 caractere = 10bits / Baudrate x 1E6 us 00197 Temps_alloue_Caractere_S32 = 10000000 / Baudrate * 3 ; 00198 } 00199 00200 /************ Ouverture du port Série **********************/ 00201 void vPort_Serie_Ouvre(void) 00202 { 00203 // Purge du buffer de réception 00204 while( RS.readable() ) 00205 { 00206 Buffer_Reception_U8[0] = RS.getc() ; 00207 } 00208 Buffer_Reception_U8[0] = 0 ; 00209 00210 vPort_Serie_Reception ( 0 ) ; 00211 RS.attach (&iReception_Serie , RS.RxIrq) ; 00212 00213 //RS.printf ("Debut") ; 00214 //PC8.printf("\r\n Serie : Attach ") ; 00215 00216 } 00217 00218 /************ Controle de réception **********************/ 00219 U8 cControle_Reception( U8 Numero_Ordre_U8 ) 00220 {/* 00221 if ( Numero_Ordre_U8 != Numero_Ordre_En_Reception_U8 ) 00222 {// La réception est arrétée 00223 //Chrono_Serie.stop() ; 00224 return ( ARRET ) ; 00225 } 00226 if ( Chrono_Serie.read_us() > ( Temps_alloue_Caractere_S32 * Nb_Caracteres_Attendus ) ) 00227 {// Dépassement du temps alloué 00228 //Chrono_Serie.stop() ; 00229 Numero_Ordre_En_Reception_U8++ ; 00230 Pointeur_Reception_U8 = 0 ; 00231 Nb_Caracteres_Attendus = TAILLE_BUFFER_RECEPTION ; 00232 00233 return ( TIMEOUT ) ; 00234 }*/ 00235 return ( RECEPTION ) ; 00236 } 00237 00238 /************ Fermeture du port Série **********************/ 00239 void vPort_Serie_Ferme( void ) 00240 { 00241 //RS.detach() ; 00242 } 00243 00244 /************ Cloture du port Série **********************/ 00245 void vPort_Serie_Cloture( void ) 00246 { 00247 }
Generated on Tue Jul 12 2022 23:24:06 by
1.7.2
