AnsweredAssumed Answered

How to make stm32f4 communicate with hyperterminal

Question asked by ashraf.salahuddin on Apr 19, 2014
Latest reply on Nov 20, 2015 by Pac-Man
Hi everyone, I'm trying to make stm32f4 communicate with hyperterminal 
this is the code 
<b>
// STM32 USART6 (Tx PC.6, Rx PC.7) STM32F4DIS-BB - sourcer32@gmail.com
 
#include "stm32f4_discovery.h"


//functions prototypes
int SendChar(int ch);               
int GetChar(void);


//a counter variable to change the data sent every iteration
static uint16_t cnt = 0; 


/**************************************************************************************/
  
void RCC_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------
      * enables clocks for USART6 and GPIOC
      */
     
  /* USART6 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  
  /* GPIOC clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
}
  
/**************************************************************************************/
void GPIOD_Configuration(void)
{
          /*put 1 in the bit you want to enable its corresponding peripheral
           *PortA bit0, PortD bit3  -->  RCC->AHB1ENR=00..0010001*/
          RCC->AHB1ENR |= 0x9;                   //IO PortD clock enable / port A clock enable
                
    //setup Port A ports
          /*Each pin in portA has two corresponding bits in MODER, 00->input, 01->output
        *Her ~3=00 then we clear bits 0,1 int MODER to make pin 0 in portA as input */
    GPIOA->MODER &= ~3;                    // Set PortA Bit 0 Active input (the user button is on PA0)
    GPIOA->PUPDR &= ~3;                                      // step lightly , no pull up nor pull down resistors
          
    // Port D setup
    //GPIOD->MODER = 0x55000000;                          //Set GPIOD 12/13/14/15 as outputs PIN
          GPIOD->MODER = (GPIOD->MODER & ~0xFF000000) | 0x55000000;  //Set GPIOD 12/13/14/15 as outputs PIN
    GPIOD->OTYPER  &= ~0xF000;             //Set output as Push-Pull mode (if 0-> push-pull, 1->open-drain)
    GPIOD->OSPEEDR &= ~0xFF000000;         //Set output speed 2MHz low speed
    GPIOD->PUPDR   &= ~0xFF000000;         //Set no pull up and pull down
}

void GPIO_Configuration(void)
{
     /*This function configure GPIOC
      - pin 6       --> USART6_TX
      - pin 7       --> USART6_RX
      - GPIO mode   --> alternate function (to use USART)
      - output type --> push pull
      - input type  --> no pull up nor pull down
      - input speed --> 2 MHz
      */
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /*-------------------------- GPIO Configuration ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PC.6 USART6_TX, PC.7 USART6_RX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* Connect USART pins to AF */
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
}
  
/**************************************************************************************/


void USART6_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;
  
  /* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  USART_Init(USART6, &USART_InitStructure);
 
  USART_Cmd(USART6, ENABLE);
}
 
/**************************************************************************************/
void delay(int cnt)
{
while(cnt--)
     ;
}

int main(void)
{
  RCC_Configuration();               /*Configure the Clock*/


  GPIO_Configuration();               /*Configure GPIO*/
     
     GPIOD_Configuration();    
     
  USART6_Configuration();       /*Configure USART6*/
 
  while(1) // Don't want to exit
  {
    static volatile uint16_t sent_data = 0;                         /*A 16-bit variable to hold the received data*/
          static volatile uint16_t received_data = 0;                         /*A 16-bit variable to hold the received data*/
          
          cnt++;
          
          /*Send data*/
    sent_data = SendChar(cnt);   //send
          sent_data++;
          
          /*Receive sent data*/
          received_data = GetChar();               //receive
          received_data++;
          
          //blink LEDs to make sure if the program works properly 
          GPIOD->ODR |= 0xF000;
          delay(10000000*2);
          GPIOD->ODR &= ~0xF000;
          delay(10000000*2);
  }
}
/**************************************************************************/
 
int SendChar(int ch)
{
     /*This function checks if DR is empty, indicating that the previous sent data is sent, with checking <TXE> bit
      *if so, it puts the data want to be sent in DR to send it.
      */
     while(!( USART6->SR & USART_FLAG_TXE))
     /*wait for data register to be empty, indicating that the privious data has been sent*/
     ;     

     USART6->DR = (ch & 0xFF);                  //put the data you want to send in data register

return (ch);                                                            //return the data sent
}

int GetChar(void)
{
     /*This function checks if data is recceived or not with checking <RXNE> bit.
      *if so, it get this data received and return it
      */
     //USART6->SR &= ~USART_FLAG_RXNE;  //Clear the "Received data register not empty flag"
     
     while(!(USART6->SR & USART_FLAG_RXNE))
     /*wait until data register is not empty, indicating that there is data received*/
     
return ((int)(USART6->DR & 0xFF));   //return the received data 
}


#ifdef  USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  while (1)
  {}
}
#endif
 
/**************************************************************************/
</b>

I've USB-to-Serial module 
I really searched a lot for a tutorial explaining how to make stm32f4 communicate with hyperterminal but I didn't find any .. and I don't know how the newbies learned it ?? 
I'll be grateful if someone show me how to do this in details .. 
Thanks a lot

Outcomes