November 22, 2024, 10:24:54 AM

Clock settings for the STM32 E407 [SOLVED]

Started by sparcz, September 20, 2012, 06:03:23 PM

Previous topic - Next topic

sparcz

Hi!
I have built an RS232 shield to my E407 board, but don't seem to be able to make it work. The olimex protoshields has the Rx and Tx from PB7 and PB6 which I configure for USART 1 (AF7). I suspect that it could be the clocks that aren't properly set up. The source I have used comes from examples on the Discovery board, and I don't know if the clock setup is identical. Is it?

Regards,
Peter

PA

//==============================================================================
// This example requires STM32F4xx StdPeriph Lib
//
#include "stm32f4xx.h"


//==============================================================================
//descri:   Setup USART and config
//params:    void
//return:    none
//
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  // clock enable  -----------------------------------------------------------
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  // In order to reach higher communication baudrates, it is possible to
  // enable the oversampling by 8 mode using the function USART_OverSampling8Cmd().
  //USART_OverSampling8Cmd(USART1,ENABLE); // DONT use if you are only using < 115200 BaudRate   
   
   
  //USART1 GPIO --------------------------------------------------------------
  //PB6   USART1_TX
  //PB7   USART1_RX   
  // Connect USART1 pins to AF7
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6 , GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7 , GPIO_AF_USART1);
  // Configure USART Tx and Rx as alternate function push-pull
  GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_NOPULL; // use GPIO_PuPd_UP if needed
  GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  // USART1 Configuration -----------------------------------------------------
  USART_InitStructure.USART_BaudRate = 38400;  // or what ever
  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(USART1, &USART_InitStructure);
     
  // Enable the USART Interrupts - ONLY IF YOU ARE USING UART INTs, IF SO YOU NEED "USART1_IRQHandler"
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  // Enable USART1 Receive and Transmit interrupts - ONLY IF YOU ARE USING UART INTs, IF SO YOU NEED  "USART1_IRQHandler"
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Transmit interrupt is activated upon sending of data

  // Enable the USART1
  USART_Cmd(USART1, ENABLE);
}
//==============================================================================

sparcz

I tried to compile and run the example above, but it doesn't seem to work on my board. I have verified my shield using an AVR board, so I know that the function there is correct. However, I suspect that it is the clock setup that is wrong since the examples come from the Discovery board that has a 25 MHz HSE, and the Olimex E407 have a 12 MHz HSE.

PA: Could you tell me what HSE and PLL settings you are using?

gdisirio

Hi,

ChibiOS/RT 2.5.0 includes a demo for that board, the use of Ethernet, SD Card and USB under the RTOS is demonstrated. Of course the clock setup is included. See my signature.

Giovanni

sparcz

#4
I got it to work, but I don't know which one of my changes that did the magic. First, I generated new clock settings in system_stm32f4xx.c. The patch can be found here http://pastebin.com/Yg6hQHSc and is quite self-explanatory. Note though, that the autogenerated text about the default HSE crystal speed is wrong. It is 12MHz.

Second, I used the example from Elias blog, http://eliaselectronics.com/2012/07/18/stm32f4-usart-example-with-interrupt/. The main difference in that example is the enabling of the clock for the GPIOB

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

Best regards,
Peter

Edit: The link to the blog above seems broken. Maybe this works: http://eliaselectronics.com/stm32f4-tutorials/stm32f4-usart-tutorial/