Communication uart STM32F4
Répondre à la discussion
Affichage des résultats 1 à 9 sur 9

Communication uart STM32F4



  1. #1
    hichooo1991

    Communication uart STM32F4


    ------

    salutation
    je veux realiser une communication entre deux carte STM32F4 pour l'echange d'une simple donné une chaine de caracteres , si quelqu'un peut m'aider je suis un peut présser par le temps et merci bien d'avance

    -----

  2. #2
    Jack
    Modérateur

    Re : Communication uart STM32F4

    C'est quoi cette manie de se greffer à une discussion ancienne et terminée? La règle est d'en ouvrir une nouvelle.

  3. #3
    PIXEL

    Re : Communication uart STM32F4

    c'est manifestement un devoir à faire....

    et nous ne faisons pas les devoirs

  4. #4
    hichooo1991

    Re : Communication uart STM32F4

    désolé pour cette demande mais jai essayé d'utiliser la fonction usart mais j'ai pas réussi
    mon objectif est de crypter une donner et de la transmettre d'une carte a une autre jai réalisé l'étape de cryptage de donné mais lors de l'épate d'echange de la donnée cryptée j'ai pas réussi
    svp votre aide
    et merci bien d'avance

  5. A voir en vidéo sur Futura
  6. #5
    PIXEL

    Re : Communication uart STM32F4

    et bien , fais voir ce que tu as fait , avec un lien sur les cartes...

    les cadors du microcontrôleur interviendront

  7. #6
    hichooo1991

    Re : Communication uart STM32F4

    Code:
    #include <stm32f4xx.h>
    #include <misc.h>			 // I recommend you have a look at these in the ST firmware folder
    #include <stm32f4xx_usart.h> // under Libraries/STM32F4xx_StdPeriph_Driver/inc and src
    #include "aes256.h"
    #include "uart.h"
    #define MAX_STRLEN 16 // this is the maximum string length of our string in characters
    unsigned char received_string[MAX_STRLEN+1]; // this will hold the recieved string
    
    void Delay(__IO uint32_t nCount) {
      while(nCount--) {
      }
    }
    
    /* This funcion initializes the USART1 peripheral
     * 
     * Arguments: baudrate --> the baudrate at which the USART is 
     * 						   supposed to operate
     */
    void init_USART1(uint32_t baudrate){
    	
    	/* This is a concept that has to do with the libraries provided by ST
    	 * to make development easier the have made up something similar to 
    	 * classes, called TypeDefs, which actually just define the common
    	 * parameters that every peripheral needs to work correctly
    	 * 
    	 * They make our life easier because we don't have to mess around with 
    	 * the low level stuff of setting bits in the correct registers
    	 */
    	GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
    	USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
    	NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
    	
    	/* enable APB2 peripheral clock for USART1 
    	 * note that only USART1 and USART6 are connected to APB2
    	 * the other USARTs are connected to APB1
    	 */
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    	
    	/* enable the peripheral clock for the pins used by 
    	 * USART1, PB6 for TX and PB7 for RX
    	 */
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    	
    	/* This sequence sets up the TX and RX pins 
    	 * so they work correctly with the USART1 peripheral
    	 */
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;		// this defines the IO speed and has nothing to do with the baudrate!
    	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;			// this defines the output type as push pull mode (as opposed to open drain)
    	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
    	GPIO_Init(GPIOB, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers
    	
    	/* The RX and TX pins are now connected to their AF
    	 * so that the USART1 can take over control of the 
    	 * pins
    	 */
    	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); //
    	GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
    	
    	/* Now the USART_InitStruct is used to define the 
    	 * properties of USART1 
    	 */
    	USART_InitStruct.USART_BaudRate = baudrate;				// the baudrate is set to the value we passed into this init function
    	USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
    	USART_InitStruct.USART_StopBits = USART_StopBits_1;		// we want 1 stop bit (standard)
    	USART_InitStruct.USART_Parity = USART_Parity_No;		// we don't want a parity bit (standard)
    	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
    	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
    	USART_Init(USART1, &USART_InitStruct);					// again all the properties are passed to the USART_Init function which takes care of all the bit setting
    	
    	
    	/* Here the USART1 receive interrupt is enabled
    	 * and the interrupt controller is configured 
    	 * to jump to the USART1_IRQHandler() function
    	 * if the USART1 receive interrupt occurs
    	 */
    	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt 
    	
    	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		 // we want to configure the USART1 interrupts
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts
    	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 // this sets the subpriority inside the group
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			 // the USART1 interrupts are globally enabled
    	NVIC_Init(&NVIC_InitStructure);							 // the properties are passed to the NVIC_Init function which takes care of the low level stuff	
    
    	// finally this enables the complete USART1 peripheral
    	USART_Cmd(USART1, ENABLE);
    }
    
    /* This function is used to transmit a string of characters via 
     * the USART specified in USARTx.
     * 
     * It takes two arguments: USARTx --> can be any of the USARTs e.g. USART1, USART2 etc.
     * 						   (volatile) char *s is the string you want to send
     * 
     * Note: The string has to be passed to the function as a pointer because
     * 		 the compiler doesn't know the 'string' data type. In standard
     * 		 C a string is just an array of characters
     * 
     * Note 2: At the moment it takes a volatile char because the received_string variable
     * 		   declared as volatile char --> otherwise the compiler will spit out warnings
     * */
    void USART_puts(USART_TypeDef* USARTx, unsigned char *s){
    
    	while(*s){
    		// wait until data register is empty
    		while( !(USARTx->SR & 0x00000040) ); 
    		USART_SendData(USARTx, *s);
    		*s++;
    	}
    }
    
    int main(void) {
      
    	static const struct {
         int keylen;
         unsigned char key[32], pt[16], ct[16];
     } FIPS197_C3[] = {
        {
          32,
          { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
          { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
          { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 
            0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
        }
     };
     
      symmetric_key skey;
     
      int i;
      unsigned char *cr;
     
      unsigned char  *plain =" test stm32 AES";
      
      init_USART1(9600); // initialize USART1 @ 9600 baud
    
     SETUP(FIPS197_C3[0].key, 32, 14, &skey); 
    
    				rijndael_ecb_encrypt(plain,cr, &skey);
    	for(i=0;i<16;i++){
    while( !(USART1->SR & 0x00000040) ); 
    		USART_SendData(USART1, cr[i]);
    	}		
      //USART_puts(USART1, "Init complete! Hello World!\r\n"); // just send a message to indicate that it works
    //USART_puts(USART1, "\r\n");
    //USART_puts(USART1, plain);
    //USART_puts(USART1, "\r\n");
     //USART_puts(USART1, cr);
     //USART_puts(USART1, "\r\n");
    //rijndael_ecb_encrypt(cr,cr1, &skey);
    //USART_puts(USART1, "\r\n");
     //USART_puts(USART1, cr1);
      while (1){  
        /*
         * You can do whatever you want in here 
         */
      }
    }
    
    // this is the interrupt request handler (IRQ) for ALL USART1 interrupts
    void USART1_IRQHandler(void){
    	
    	// check if the USART1 receive interrupt flag was set
    	if( USART_GetITStatus(USART1, USART_IT_RXNE) ){
    		
    		static uint8_t cnt = 0; // this counter is used to determine the string length
    		char t = USART1->DR; // the character from the USART1 data register is saved in t
    			static const struct {
         int keylen;
         unsigned char key[32], pt[16], ct[16];
     } FIPS197_C3[] = {
        {
          32,
          { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
          { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
          { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 
            0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
        }
     };
     
      symmetric_key skey;
      unsigned char *cr;
     int i;
    		/* check if the received character is not the LF character (used to determine end of string) 
    		 * or the if the maximum string length has been been reached 
    		 */
    		if( (t != '\n') && (cnt < MAX_STRLEN) ){ 
    			received_string[cnt] = t;
    			cnt++;
    		}
    		else{ // otherwise reset the character counter and print the received string
    			cnt = 0;
    			//USART_puts(USART1, received_string);
    SETUP(FIPS197_C3[0].key, 32, 14, &skey); 
    
    				rijndael_ecb_encrypt(received_string,cr, &skey);
    	for(i=0;i<16;i++){
    while( !(USART1->SR & 0x00000040) ); 
    		USART_SendData(USART1, cr[i]);
    	}	
    
    
    }
    	}
    }
    Dernière modification par Jack ; 22/06/2015 à 19h51. Motif: balises code

  8. #7
    HAYAC

    Re : Communication uart STM32F4

    Bonjour,

    Citation Envoyé par hichooo1991 Voir le message
    désolé pour cette demande mais jai essayé d'utiliser la fonction usart mais j'ai pas réussi
    mon objectif est de crypter une donner et de la transmettre d'une carte a une autre jai réalisé l'étape de cryptage de donné mais lors de l'épate d'echange de la donnée cryptée j'ai pas réussi
    svp votre aide
    et merci bien d'avance
    Je n'ai volontairement pas lu le code car il y à un détail qui me dérange sur la demande et la procédure de réflexion mise en place:

    Avez-vous déjà communiqué de cette façon entre les deux carte sans cryptage ?

  9. #8
    hichooo1991

    Re : Communication uart STM32F4

    c'est ça, mon problème c'est de réaliser l’échange d'une simple donnée avec USART jai la fonction de transmission mais jai pas la fonction de réception

  10. #9
    HAYAC

    Re : Communication uart STM32F4

    Dans ce cas, la procédure est simple, comme dans toute recherche de panne :
    - On identifie le problème clairement (Chose faite),
    - On l'isole (A faire),
    - On le corrige (A faire),
    - Et on le réintegre dans le système (A faire).
    Si vous êtes d'accord avec le principe suivant, vous allez simplement avec nos conseilles ressoude votre problème tout seul, ce qui je trouve est beaucoup plus formateur :
    Dans cette optique :
    - épurez tout votre code pour en arriver à un simple code de transition "Helo word".
    - Ouvrez la doc de votre STM32F4
    - et référez vous aux documentations spécifiques ex:
    http://www.ue.pwr.wroc.pl/systemy_rt/RTE9.pdf
    http://stm32f4-discovery.com/2014/04...er-with-usart/
    ...
    - A la suite de la résolution de votre problème de réception (Indiqué dans les docs) il ne restera qu'à ré-implémenter votre code de cryptage et le tour sera joué.

Discussions similaires

  1. raspberry: communication UART
    Par docEmmettBrown dans le forum Électronique
    Réponses: 1
    Dernier message: 26/04/2015, 14h48
  2. Communication UART entre Pic et RS232
    Par csjan dans le forum Électronique
    Réponses: 2
    Dernier message: 01/02/2013, 00h06
  3. UART en mode LOOPBACK avec STM32F4 Discovery
    Par Etudiant_GEII dans le forum Électronique
    Réponses: 0
    Dernier message: 27/05/2012, 14h46
  4. UART compatible avec une communication USART
    Par invite48707525 dans le forum Électronique
    Réponses: 2
    Dernier message: 13/08/2011, 09h28
  5. communication UART
    Par invitea302684b dans le forum Électronique
    Réponses: 0
    Dernier message: 07/01/2010, 08h53
Découvrez nos comparatifs produits sur l'informatique et les technologies.