Topic : Code Example abt RS232 com btwn REVA & Hyperterminal (STM32F107)

Forum : ARM

Original Post
Post Information Post
November 25, 2010 - 10:58am
Guest

Hi all!

I'm tryng to establish a RS232 communication between my RevA (with a STM32F107 daughterboard) and the Hyperterminal. I want to display simply "Helloworld" on my monitor.
My program (including ST libraries) run, but doesn't work because when I put an oscilloscope probe on the TX line (PD5/USART2-TX, pin 86), there is any signal...

Does it exist an RS232 working example program for STM32F107 which can help me?

Thank you!

Replies
Post Information Post
+1
0
-1
November 26, 2010 - 9:39am
Raisonance Support Team

Hi,

You can find an "Hello World" example in the C:\Program Files\Raisonance\Ride\Examples\ARM\. You just have to change the processor from STR7 to STM32F107 and it will work.

Regards,

+1
0
-1
December 6, 2010 - 4:04pm
Guest

Hi,

I'm sorry, but I already try to change the target device, and download this example but it still doesn't work.
However, I check my hyperterminal connexion and configuration and all seem to be right.

Are you sure there is any adds or modifications to bring for adapt the code for a STM32F107VCT6 use?

Thx for your help, Bruno!

+1
0
-1
December 7, 2010 - 9:47am
Raisonance Support Team

Hi,

Using REva and the STM32F107, the hello World example should work right away.

Few things to check:
- Does the "Toggle" application work on your board?
- Is the Hello World project configured as RLink (not Simulator) mode?
- Did you properly place the Tx and Rx jumpers on the board?

Once this is done, you may have various problems with the use of Hyperterminal. The best thing to do is to check the UART output using an oscilloscope. We usually write something such as

while(1)
    putchar('U');

'U' has the property of being 01010101 in binary, so it generates nice patterns on the scope.

The bit width on your scope should be 104us (9600bps).

Check whether the signal is present, and whether it is 104us wide. If it is, then your problem revolves around Hyperterminal and your COM ports.

Best Regards,

+1
0
-1
December 7, 2010 - 5:45pm
Raisonance Support Team

Wooops sorry,

I gave an invalid information: The putchar/getchar functions provided with the RKit-ARM use USART1 for communication, although USART2 is connected to the Tx/Rx pins through USART2!

This means that you cannot directly call the integrated putchar, you need to write your own init/putchar/getchar routines that use USART2 instead of USART1.

More on that later, I will post some source code that fixes the problem so that you do not have to worry about writing the functions by yourself.

Best Regards,
Bruno

+1
0
-1
December 8, 2010 - 3:56pm
Raisonance Support Team

Hi,

There is an additional oddity on our STM32F107 REva Daughterboard: The USART is the USART2, but REMAPPED.

Here is a piece of code that performs proper initialization on both STM32F103 and STM32F107 daughterboards.
You can use the code without limitation. It does not use the ST Library, hence is quite portable.

However, you will need to adapt the baud rate, which is currently hardcoded to 9600 bps (no parity, 8 data bits, 1 stop bit).


/**
 * STM32F10X_IO_putchar.c
 *
 * This file implements the io_putchar and io_getchar functions
 * for the STM32F10x devices.
 *
 * It does not use the ST firmware Libraries.
 *
 * You can duplicate it and include the copy in your project,
 * then you'll be able to modify it for using different parameters:
 * baudrate, stop bits, other UART, etc.
 *
 * The default settings permit a 9600/N/8/1 settings for an 8MHz clock.
 */

typedef unsigned long u32;
typedef volatile unsigned long vu32;
typedef volatile unsigned short vu16;

// RCC registers
#define RCC_CR                          (*(vu32*)0x40021000)
#define RCC_CFGR                        (*(vu32*)0x40021004)
#define RCC_CIR                         (*(vu32*)0x40021008)
#define RCC_APB2RSTR                    (*(vu32*)0x4002100C)
#define RCC_APB1RSTR                    (*(vu32*)0x40021010)
#define RCC_AHBENR                      (*(vu32*)0x40021014)
#define RCC_APB2ENR                     (*(vu32*)0x40021018)
#define RCC_APB1ENR                     (*(vu32*)0x4002101C)
#define RCC_BDCR                        (*(vu32*)0x40021020)
#define RCC_CSR                         (*(vu32*)0x40021024)
#ifdef STM32F10X_CL
#define RCC_AHBRSTR                     (*(vu32*)0x40021028)
#define RCC_CFGR2                       (*(vu32*)0x4002102C)
#endif /* STM32F10X_CL */

#define RCC_APB2Periph_AFIO             0x00000001
#define RCC_APB2Periph_GPIOA            0x00000004
#define RCC_APB2Periph_GPIOB            0x00000008
#define RCC_APB2Periph_GPIOC            0x00000010
#define RCC_APB2Periph_GPIOD            0x00000020
#define RCC_APB2Periph_GPIOE            0x00000040
#define RCC_APB2Periph_GPIOF            0x00000080
#define RCC_APB2Periph_GPIOG            0x00000100
#define RCC_APB2Periph_ADC1             0x00000200
#define RCC_APB2Periph_ADC2             0x00000400
#define RCC_APB2Periph_TIM1             0x00000800
#define RCC_APB2Periph_SPI1             0x00001000
#define RCC_APB2Periph_TIM8             0x00002000
#define RCC_APB2Periph_USART1           0x00004000
#define RCC_APB2Periph_ADC3             0x00008000

#define RCC_APB1Periph_USART2           0x00020000

//Alternate function IO remapping
#define AFIO_MAPR                       (*(vu32*)0x40010004)

#define AFIO_MAPR_USART2_REMAP          0x00000008


// GPIOA registers
#define GPIOA_CRL                       (*(vu32*)0x40010800)
#define GPIOA_CRH                       (*(vu32*)0x40010804)
#define GPIOA_IDR                       (*(vu32*)0x40010808)
#define GPIOA_ODR                       (*(vu32*)0x4001080C)
#define GPIOA_BSRR                      (*(vu32*)0x40010810)
#define GPIOA_BRR                       (*(vu32*)0x40010814)
#define GPIOA_LCKR                      (*(vu32*)0x40010818)

// GPIOD registers
#define GPIOD_CRL                       (*(vu32*)0x40011400)
#define GPIOD_CRH                       (*(vu32*)0x40011404)
#define GPIOD_IDR                       (*(vu32*)0x40011408)
#define GPIOD_ODR                       (*(vu32*)0x4001140C)
#define GPIOD_BSRR                      (*(vu32*)0x40011410)
#define GPIOD_BRR                       (*(vu32*)0x40011414)
#define GPIOD_LCKR                      (*(vu32*)0x40011418)

#define GPIOPin_0                       0
#define GPIOPin_1                       1
#define GPIOPin_2                       2
#define GPIOPin_3                       3
#define GPIOPin_4                       4
#define GPIOPin_5                       5
#define GPIOPin_6                       6
#define GPIOPin_7                       7
#define GPIOPin_8                       8
#define GPIOPin_9                       9
#define GPIOPin_10                      10
#define GPIOPin_11                      11
#define GPIOPin_12                      12
#define GPIOPin_13                      13
#define GPIOPin_14                      14
#define GPIOPin_15                      15

/* Warning: These are NOT identical to the ST library macros! */
#define GPIO_Mode_IN_FLOAT              0x04
#define GPIO_Mode_AF_PP_50MHz           0x0B


// USART1 registers (these are 16-bit, not 32-bit registers)
#define USART1_SR                       (*(vu16*)0x40013800)
#define USART1_DR                       (*(vu16*)0x40013804)
#define USART1_BRR                      (*(vu16*)0x40013808)
#define USART1_CR1                      (*(vu16*)0x4001380C)
#define USART1_CR2                      (*(vu16*)0x40013810)
#define USART1_CR3                      (*(vu16*)0x40013814)
#define USART1_GTPR                     (*(vu16*)0x40013818)

// USART2 registers (these are 16-bit, not 32-bit registers)
#define USART2_SR                       (*(vu16*)0x40004400)
#define USART2_DR                       (*(vu16*)0x40004404)
#define USART2_BRR                      (*(vu16*)0x40004408)
#define USART2_CR1                      (*(vu16*)0x4000440C)
#define USART2_CR2                      (*(vu16*)0x40004410)
#define USART2_CR3                      (*(vu16*)0x40004414)
#define USART2_GTPR                     (*(vu16*)0x40004418)

#define USART_FLAG_CTS                  0x0200
#define USART_FLAG_LBD                  0x0100
#define USART_FLAG_TXE                  0x0080
#define USART_FLAG_TC                   0x0040
#define USART_FLAG_RXNE                 0x0020
#define USART_FLAG_IDLE                 0x0010
#define USART_FLAG_ORE                  0x0008
#define USART_FLAG_NE                   0x0004
#define USART_FLAG_FE                   0x0002
#define USART_FLAG_PE                   0x0001
#define USART_CR1_Enable                0x2000
#define USART_CR1_Transmit_Enable       0x0008
#define USART_CR1_Receive_Enable        0x0004


/**
 * For the REva STM32F103 daughterboard, the UART is mapped onto USART1,
 * with Tx on PA9 and Rx on PA10.
 * For the REva STM32F107 daughterboard, the UART is mapped onto USART2,
 * with Tx on PD5 and Rx on PD6.
 */
#ifdef  _STM32F107VCT6_

#define USARTx_SR  USART2_SR
#define USARTx_DR  USART2_DR
#define USARTx_CR1 USART2_CR1

void __io_init( void )
  {
  /* Enable GPIOx and AFIO clocks */
  RCC_APB2ENR |= RCC_APB2Periph_GPIOD;
  RCC_APB2ENR |= RCC_APB2Periph_AFIO;

  RCC_APB1ENR |= RCC_APB1Periph_USART2;

  /* Remap USART2 for STM32F107 REva daughterboard */
  AFIO_MAPR |= AFIO_MAPR_USART2_REMAP;

  /* Configure the GPIO ports */
  /* Configure USARTx_Tx (PD5) as alternate function push-pull */
  u32 pinmask = 0x0F << ((GPIOPin_5) << 2);
  GPIOD_CRL &= ~pinmask;
  GPIOD_CRL |= GPIO_Mode_AF_PP_50MHz << ((GPIOPin_5) << 2);

  /* Configure USARTx_Rx (PD6) as input floating */
  pinmask = 0x0F << ((GPIOPin_6) << 2);
  GPIOD_CRL &= ~pinmask;
  GPIOD_CRL |= GPIO_Mode_IN_FLOAT << ((GPIOPin_6) << 2);


  /* USARTx configured as 9600 baud, 8 Bits length, One Stop Bit, No parity */
  USART2_CR2 = 0x0000;
  USART2_CR1 = USART_CR1_Transmit_Enable | USART_CR1_Receive_Enable;
  USART2_CR3 = 0x0000;
  USART2_BRR = 0x0341;                  // Baud rate should be adapted if you clock is not 8MHz. 

  USART2_CR1 |= USART_CR1_Enable;       // Enable USART
  }

#else   // _STM32F107VCT6_

#define USARTx_SR  USART1_SR
#define USARTx_DR  USART1_DR
#define USARTx_CR1 USART1_CR1

void __io_init( void )
  {
  /* Enable GPIOx and AFIO clocks */
  RCC_APB2ENR |= RCC_APB2Periph_GPIOA;
  RCC_APB2ENR |= RCC_APB2Periph_AFIO;
  RCC_APB2ENR |= RCC_APB2Periph_USART1;
  

  /* Configure the GPIO ports */
  /* Configure USARTx_Tx (PA9) as alternate function push-pull */
  u32 pinmask = 0x0F << ((GPIOPin_9 - 8) << 2);
  GPIOA_CRH &= ~pinmask;
  GPIOA_CRH |= GPIO_Mode_AF_PP_50MHz << ((GPIOPin_9 - 8) << 2);

  /* Configure USARTx_Rx (PA10) as input floating */
  pinmask = 0x0F << ((GPIOPin_10 - 8) << 2);
  GPIOA_CRH &= ~pinmask;
  GPIOA_CRH |= GPIO_Mode_IN_FLOAT << ((GPIOPin_10 - 8) << 2);


  /* USARTx configured as 9600 baud, 8 Bits length, One Stop Bit, No parity */
  USART1_CR2 = 0x0000;
  USART1_CR1 = USART_CR1_Transmit_Enable | USART_CR1_Receive_Enable;
  USART1_CR3 = 0x0000;
  USART1_BRR = 0x0341;                  // Baud rate should be adapted if you clock is not 8MHz. 

  USART1_CR1 |= USART_CR1_Enable;       // Enable USART
  }

#endif   // _STM32F107VCT6_


// Send the character on UART
void __io_putchar( char c )
   {
   //init UART if needed
   if(!(USARTx_CR1 & USART_CR1_Enable))
      {
      __io_init();
      }

   // \n is not enough. Need \r too!
   if( c == 0x0A )
      {
      __io_putchar( 0x0D );
      }

   while((USARTx_SR & USART_FLAG_TXE) == 0);

   USARTx_DR = c;
   }

int putchar( int c )
    {
    __io_putchar( (char) c );
    return c;
    }


int __io_getchar( void )
   {
   unsigned short value;
   unsigned short wStatus;

   //init UART if needed
   if(!(USARTx_CR1 & USART_CR1_Enable))
      {
      __io_init();
      }

   while((USARTx_SR & USART_FLAG_RXNE) == 0);

   value = USARTx_DR;

   return value;
   }


int getchar( void )
   {
   return __io_getchar();
   }

This code will be available in the next release USART_putchar library offered in the RKit-ARM, which should be next week.

Best Regards,

+1
0
-1
January 5, 2011 - 12:10pm
Guest

Hi!

Thank you Bruno for your help!
I succeed to write "HelloWorld" on my hyperterminal, your code helped me to understand many things and to check my RS232 connexion (about hardware and configurations).

It is possible that I need your knows soon ;)