/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / configure USART2 in primer2

Username:     
Password:     
             

Forum

# 1   2010-07-23 14:25:52 configure USART2 in primer2

johnyo
New member
From: Bucaramanga
Registered: 2010-05-19
Posts: 2

configure USART2 in primer2

Hello,
I want to comunicate 2 primer2, so I connect the pins 16 and 18 of the primers.
I have read some prjects to help me. I new using primer and Im learning to use it. but the program I did, doesn`t work yet. the status of the flags in the programs, always remains in RESET, nothing is arriving to the primer receptor.
Could someone help me to find out why it doesnt work yet.
thanks.


/********************** (C) COPYRIGHT 2007-2009 RAISONANCE ********************
* File Name          :  Application.c
* Author             :
* Date First Issued  :
* Description        :  Circle_App CircleOS application template.
* Revision           :
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "circle_api.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"


/* Private defines -----------------------------------------------------------*/

// The following should be the minimal CircleOS version needed by your application
#define NEEDEDVERSION "V 3.7"

/* Private functions ---------------------------------------------------------*/
static enum MENU_code MsgVersion(void);


enum UART_MODE {TX,INIT,RX};    // Application mode enum
enum UART_MODE mode;            //*Application mode : Transmiter or Receiver*/
//u8 OldPLLFreq;                  // Old clock frequency
u8  RxData,TxData,status,status2;              // Vars holding received or transmited char
char  data;
int i;
/* Public variables ----------------------------------------------------------*/
const char Application_Name[8+1] = {"appmenu"};      // Max 8 characters
const char msg[3][64] = {                         // Messages to display
                         "",
                         "Send a char",
                         "Receive a char"
                        };
int currentMsgIdx;
/* Private function prototypes -----------------------------------------------*/

void GPIO_Configuration(void);

void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

   /* Configure USART2 Tx (PA.2) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  /* Configure USART2 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


   
}
/*******************************************************************************
* Function Name  : Application_Ini
* Description    : Initialization function of Circle_App. This function will
*                  be called only once by CircleOS.
* Input          : None
* Return         : MENU_CONTINUE_COMMAND
*******************************************************************************/
enum MENU_code Application_Ini(void)
    {
    // Ensure that the current OS version is recent enough
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
        {
        return MsgVersion();
        }


    // TODO: Write your application initialization function here.

    // Save old clock frequency
    /*OldPLLFreq = UTIL_GetPll();*/
   
    //Enable clock
    RCC_APB1PeriphClockCmd(       
        RCC_APB1Periph_USART2, ENABLE);
   
    /* Enable GPIOA clock */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
   
   
    void GPIO_Configuration(void);

    /* The following example illustrates how to initialize a
    USART_InitTypeDef structure */
    USART_InitTypeDef USART_InitStructure;
    USART_StructInit(&USART_InitStructure);
   
    //USART_DeInit(USART2);
    USART_Cmd(USART2, ENABLE);
    USART_SetPrescaler(USART2, 0x1);
   
    /* Enables the USART1 transmit interrupt */
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enables the USART1 Receive interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
   
    // Init App
    currentMsgIdx = 0;
    mode = INIT;
    TxData = 'a';
   

    return MENU_CONTINUE_COMMAND;
    }

/*******************************************************************************
* Function Name  : Application_Handler
* Description    : Management of the Circle_App.
*
* Input          : None
* Return         : MENU_CONTINUE
*******************************************************************************/
enum MENU_code Application_Handler(void)
    {


    // TODO: Write your application handling here.

   if(JOYSTICK_GetState()==JOYSTICK_RIGHT)
    {
        JOYSTICK_WaitForRelease();
        ClearScreen();
        currentMsgIdx = 2;
        mode++;
    }

    if(JOYSTICK_GetState()==JOYSTICK_LEFT)
    {
        JOYSTICK_WaitForRelease();
        ClearScreen();
        currentMsgIdx = 1;
        mode--;
    }

    switch(mode)
    {       
        // Transmission mode
        case TX:
           
            // Saving next char to be sended
             data = ((char)TxData)+1;
       
            // If the button is pressed...
            if(BUTTON_GetState() == BUTTON_PUSHED)
            {
                BUTTON_WaitForRelease();
           
                // ... we send a char
                USART_SendData(USART2, TxData);
           
                if (TxData == 'z')
                    TxData = 'a';
                else
            //for(i=1;i<10;i++){
                    TxData = ((char)TxData)+1;               
            //}
           
               status=USART_GetFlagStatus(USART2, USART_FLAG_TXE);
                if(status == SET){
             DRAW_DisplayString( 5, 56, "data: ", 6);
            DRAW_SetCursorPos(64, 56);
            DRAW_Putc(TxData);
                }else if(status==RESET){
                DRAW_DisplayString( 5, 36, "data: ", 6);
            DRAW_SetCursorPos(64, 36);
            DRAW_Putc(TxData);
                }
                } 
           
         
            // Display the next char
            DRAW_DisplayString( 5, 76, "data: ", 6);
            DRAW_SetCursorPos(64, 76);
            DRAW_Putc(TxData);
           
        break;
       
        // Reception mode
        case RX:
           
            // If something has been received...
            status2=USART_GetFlagStatus(USART2, USART_FLAG_RXNE);
        if(status2==SET){
                // ... we get it...
                RxData = USART_ReceiveData(USART2);
           
                // ... and display it !
                DRAW_DisplayString( 5, 76, "data: ", 6);
                DRAW_SetCursorPos(64, 76);
                DRAW_Putc(RxData);
            }else if(status2==RESET){
            RxData = USART_ReceiveData(USART2);
           
                // ... and display it !
                DRAW_DisplayString( 5, 36, "data: ", 6);
                DRAW_SetCursorPos(64, 36);
                DRAW_Putc(RxData);
            }
        break;
       
        // Main menu
        default:
            currentMsgIdx = 0;
            DRAW_DisplayString( 29, 74, "Receive-->", 10);
            DRAW_DisplayString( 25, 50, "<--Transmit", 11);
            DRAW_DisplayString( 19, 29, "Press button", 13);
            DRAW_DisplayString( 40, 5, "to exit", 7);

    // This routine will get called repeatedly by CircleOS, until we
    // return MENU_LEAVE

#if 1
    // If the button is pressed, the application is exited
    if(BUTTON_GetState() == BUTTON_PUSHED)
        {
        BUTTON_WaitForRelease();
        BUTTON_SetMode(BUTTON_ONOFF_FORMAIN);
        return MENU_Quit();
        }
#endif
break;
    DRAW_DisplayString( 40, 110, Application_Name, sizeof(Application_Name));
    DRAW_DisplayString( 5, 90, msg[currentMsgIdx], sizeof(msg[currentMsgIdx]));
    }
    return MENU_CONTINUE;   // Returning MENU_LEAVE will quit to CircleOS
    }

/*******************************************************************************
* Function Name  : MsgVersion
* Description    : Display the current CircleOS version and the version needed
*                  exit to main menu after 4 secondes
*
* Input          : None
* Return         : MENU_REFRESH
*******************************************************************************/
static enum MENU_code MsgVersion(void)
   {
   int hh,mm,ss,ss2;
   
   DRAW_DisplayString(5,60,"CircleOS",17);
   DRAW_DisplayString(80,60,UTIL_GetVersion(),6);
   DRAW_DisplayString(5,34,NEEDEDVERSION,6);
   DRAW_DisplayString(50,34," required",12);
   
   RTC_GetTime( &hh, &mm, &ss);
   ss = ss + 4;                  // 4 secondes
   ss = ss%60;
   
   do
        {
        RTC_GetTime( &hh, &mm, &ss2 );
        }
   while ( ss2 != ss );           // do while < 4 secondes
   
   DRAW_Clear();
   return MENU_REFRESH;
   }
void ClearScreen()
{
    LCD_FillRect ( 0,0,127,89, RGB_WHITE );
}

Offline

 

# 2   2010-07-26 00:55:17 configure USART2 in primer2

brucezcg
Member
Registered: 2010-06-21
Posts: 21

Re: configure USART2 in primer2

I'm trying to do this too.

Offline

 

# 3   2010-07-26 02:38:42 configure USART2 in primer2

logictechs
Member
Registered: 2009-05-07
Posts: 68

Re: configure USART2 in primer2

Here is the init code I use that seems to work for me:

NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

/* Enable GPIOx clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

/* Enable USART2 clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);   

  GPIO_InitTypeDef GPIO_InitStructure;
  /* Configure USART2 Tx (PA.2) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART2 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


/* USART2 configuration ------------------------------------------------------*/
  /* USART2 configured as follow:
        - BaudRate = 9600 baud 
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */

  USART_InitTypeDef USART_InitStructure;
  USART_ClockInitTypeDef USART_ClockInitStructure;
  /* USART configuration ------------------------------------------------------*/

  USART_InitStructure.USART_BaudRate = 9600;
  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(USART2, &USART_InitStructure);

  /* Configure USART2 */
  USART_Init(USART2, &USART_InitStructure);
   
  /* Enable USART2 Receive and Transmit interrupts */
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);     
 
  /* Enable the USART2 */
  USART_Cmd(USART2, ENABLE);

  OldHandler = (tHandler)UTIL_GetIrqHandler( 0xD8 );
  UTIL_SetIrqHandler(0xD8, MyUsart2Handler);

Inorder for my baud rate to be correct, I also need to use this code:
void RCC_Config(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz */
//   RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

}

Put this at the top of your code.  Allows you to use the printf to use the serial port:

#include "stm32f10x_usart.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_lib.h"
#include "stm32f10x_map.h"
#define __STM32F10x_H

#include "circle_api.h"

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

Offline

 

# 4   2010-07-28 06:04:52 configure USART2 in primer2

brucezcg
Member
Registered: 2010-06-21
Posts: 21

Re: configure USART2 in primer2

Thanks, It really works now,and I wonder where should "void RCC_Config(void)" put?

Offline

 

# 5   2010-07-29 00:20:40 configure USART2 in primer2

logictechs
Member
Registered: 2009-05-07
Posts: 68

Re: configure USART2 in primer2

Put it as a function anywhere.  Use it before you need to use the serial port.  If you do any speed config changes you should run it again.  Timing is everything!

Offline

 

# 6   2010-07-29 01:24:36 configure USART2 in primer2

brucezcg
Member
Registered: 2010-06-21
Posts: 21

Re: configure USART2 in primer2

OK, Thanks very much!!!

Offline

 

Board footer