/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / Unable to transmit data over USART2

Username:     
Password:     
             

Forum

# 1   2009-05-22 16:56:14 Unable to transmit data over USART2

pitu82
Member
Registered: 2009-03-07
Posts: 14

Unable to transmit data over USART2

Hi guys,
I am trying to develop X10 433Mhz RF transmitter using USART2. Basically, I am using a transmitter module from RF solutions RTFQ1-433Mhz. It has an Enable Pin, which I am connecting to VCC of Primer to maintain a Logic one, and INPUT Pin is connected to USART2.
I am initializing the USART2 Pin and enabling it but when I debug it I cant get anything. can someone please help its ma project else I will fail for sure.... heres the code.

//USART initialization
#include "ut2.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_lib.h"
#include "circle_api.h"
#include "stm32f10x_map.h"

#define _USART2
#ifdef _USART2
#define USART2 ((USART_TypeDef *) USART2_BASE)
#endif 


void InitUSART()
{


GPIO_InitTypeDef  GPIO_InitStructure;
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, DISABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  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);



   
  /* 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_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_Tx;
 
  USART_Init(USART2, &USART_InitStructure);

  /* Enable the USART2 */
USART_Cmd(USART2, ENABLE); 

}

I am not quiet sure whether I need Hardware Flow control.... I think yes... Is that a prob?

//main code...... Here I am worried about delays.....

#include "stm32f10x_lib.h"
#include "x10a.h"
#include "circle_api.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_map.h"
#include "ut2.h"


#define Bit_SET 1
#define Bit_RESET 0
#define _USART2


#ifdef _USART2
#define USART2 ((USART_TypeDef *) USART2_BASE)
#endif /*_USART2 */

/************************************************************************************
*Function name: x10_sendpreamble
*Description: sends 1 for 9msec & 0 for 4.5msec
*Input: None
*Output: None
*Return :None
*****************************************************************************/

void x10_sendpreamble()
{
USART_SendData(USART2, Bit_RESET);
/*delay_ms(40); */

USART_SendData(USART2, Bit_SET);
/* delay_ms(8);*/

USART_SendData(USART2, Bit_RESET);
/* delay_ms(4); */

}
// In X10 if the bit to be send is 1 only 1 is send but if its 0 then 1 and 0 is sent. Eg. to send 5 which is 101 we will have to send 1101.
void x10_sendbit(int thebit)
{

    {
if(thebit==0)
   
{
USART_SendData(USART2, Bit_SET);

/* delay_us(400);*/
USART_SendData(USART2, Bit_RESET);
/* delay_us(700);*/

}
else   
{
USART_SendData(USART2, Bit_SET);

/* delay_us(400);*/
USART_SendData(USART2, Bit_SET);
delay_us(400);

}
}
return;
}

void x10_sendbyte(int thebyte)
{
int i;

for (i=0;i<8;i++)
{
x10_sendbit(thebyte<<2);
}
return;
}

void x10_send(char hc, int unit, int action)
{

int ufcbyte; // unit function code
x10_sendpreamble();
// here we set the house code
if (unit > 8) { hc=0b00000100; } else { hc=0b00000000; }

switch (hc)
{
case 'P':  hc=hc|0b00110000; break; 
case 'O':  hc=hc|0b00100000; break;
case 'N':  hc=hc|0b00010000; break;
case 'M':  hc=hc|0b00000000; break;
case 'L':  hc=hc|0b11010000; break;
case 'K':  hc=hc|0b11000000; break;
case 'J':  hc=hc|0b11110000; break;
case 'I':  hc=hc|0b11100000; break;
case 'H':  hc=hc|0b10110000; break;
case 'G':  hc=hc|0b10100000; break;
case 'F':  hc=hc|0b10010000; break;
case 'E':  hc=hc|0b10000000; break;
case 'D':  hc=hc|0b01010000; break;
case 'C':  hc=hc|0b01000000; break;
case 'B':  hc=hc|0b01110000; break;
case 'A':
default:   hc=hc|0b01100000; break;
}

x10_sendbyte(hc);
x10_sendbyte(~hc);
// send the house code and its inverse
                    // here, we set the unit and function code bits

switch (action) {     // need to support dim/bright
case 0: { ufcbyte=0b10000111; break; } // on
case 1: { ufcbyte=0b10100111; break; } // off
default: { ufcbyte=0b11111111; break; } // A1on
}

switch (unit) {
case 8:
case 16: { ufcbyte=ufcbyte|0b01011000; break; }

case 7:
case 15: { ufcbyte=ufcbyte|0b01001000; break; }

case 6:
case 14: { ufcbyte=ufcbyte|0b01010000; break; }

case 5:
case 13: { ufcbyte=ufcbyte|0b01000000; break; }

case 4:
case 12: { ufcbyte=ufcbyte|0b00011000; break; }

case 3:
case 11: { ufcbyte=ufcbyte|0b00001000; break; }

case 2:
case 10: { ufcbyte=ufcbyte|0b00010000; break; }

case 1:
case 9: default: { ufcbyte=ufcbyte|0b00000000; break; }
}
x10_sendbyte(ufcbyte);
x10_sendbyte(~ufcbyte);
// send the unit/function code and inverse

x10_sendbit(1); // gratuitous 1 at the end…
   
return;
}

//-------------------
void delay_ms(int n)
{
//delay n ms
unsigned int x;

  while(n--)
  {
    x=2300;       
    while(x--);
  }
}

//-------------------
void delay_us(int n)
{
//delay n 100us
unsigned int x;
n=n/1000;

  while(n--)


  {
    x=230;
    while(x--);
  }


}

Please help me guys......... would really appreciate it.....

Last edited by pitu82 (2009-05-22 16:57:37)

Offline

 

# 2   2009-05-22 19:20:49 Unable to transmit data over USART2

Saulis
Member
From: Lithuania
Registered: 2009-04-09
Posts: 34

Re: Unable to transmit data over USART2

here is mine usart init and i know it works:

Code:

  USART_InitTypeDef USART_InitStructure;
  USART_ClockInitTypeDef USART_ClockInitStructure;
  
  /* Enable USART2 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  
  /* USART configuration ------------------------------------------------------*/
  USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  USART_ClockInit(USART2, &USART_ClockInitStructure);

  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 the USART */
  USART_Init(USART2, &USART_InitStructure);

  /* Enable the USART */
  USART_Cmd(USART2, ENABLE);
  
  /* Configure USART2 Tx (PA.2) and USART2 CK(PA.4) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Configure USART1 Rx (PA.3) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  

  USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  USART_ClearITPendingBit(USART2, USART_IT_TC);

  // In order to output USART2_CK, the SSOE bit in the SPI1_CR2
  // register must be set to configure the pin in output mode.
  SPI1->CR2 |= SPI_CR2_SSOE;

the difference here is that usart initialized using sunchronous mode with clock output(SPI master mode)

Offline

 

# 3   2009-05-23 06:23:29 Unable to transmit data over USART2

pitu82
Member
Registered: 2009-03-07
Posts: 14

Re: Unable to transmit data over USART2

hi buddy,

i tried inserting the section of your code into my program as it was similar, but i really didnt understand the SSOE part.
do i have to initialise the SPI?
during the debugging I get the SSOE as "undeclared"..
i am using RF transmitter RTFQ1, i connected pin PA2 to the input, do i connect pin PA4 to the Enable?

please help i am desparate..it is my final assessment for my degree project and have 3 days to go..

Offline

 

# 4   2009-05-23 16:40:00 Unable to transmit data over USART2

Saulis
Member
From: Lithuania
Registered: 2009-04-09
Posts: 34

Re: Unable to transmit data over USART2

SPI protocol needs three lines, tx, rx and clk. Usart most popular use is asynchronous mode, thus clk is not needed. this SSOE  part is described in STM32 errata spec., that is i want to output alternate function on PA4, need to enable ssoe in spi1 Cr2 control register.

Offline

 

# 5   2009-05-23 16:46:51 Unable to transmit data over USART2

Saulis
Member
From: Lithuania
Registered: 2009-04-09
Posts: 34

Re: Unable to transmit data over USART2

Oh, and i wrote program for primer2, may be a bit different pins for primer

Offline

 

# 6   2009-05-27 12:23:05 Unable to transmit data over USART2

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

Re: Unable to transmit data over USART2

Hi,

I had a baud rate problem until I added this code to my project

ErrorStatus HSEStartUpStatus;  // add this at the top of .c file

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);  //leave the input clock alone and do not multiply it

    /* 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)
    {
    }
  }
}

To reset your Primer2 speed.  Leave the program and reconfig for your desired speed in the config menu.  This was the only way I could get correct baud rate generation.  Please let me know if there is another solution.

Offline

 

# 7   2009-05-27 15:58:47 Unable to transmit data over USART2

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: Unable to transmit data over USART2

You can set and reset the speed by calling the functions UTIL_SetPll and UTIL_GetPll.

Offline

 

# 8   2009-05-28 13:39:33 Unable to transmit data over USART2

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

Re: Unable to transmit data over USART2

Hi Francis,

SetPll and GetPll do not allow the correct baud rate to be generated on my Primer2 with 3.7OS.  Setting to lowest speed sets HSI as clock source instead of PLL also.  Multipliers are not set as my RCC_config settings are also.

Offline

 

Board footer