I have an implementation for USART communication from a GPS module to a STM32F091 microcontroller.
For some reason I only receive nonsense. I have already tried setting different Baud Rates, but none gave me a proper result.
Now I am unsure if maybe there is something funky with prescaler settings or something else I forgot.
The relevant code looks as follows:
peripherals.c
/* Clock settings */
void clock_init(void)
{
/* Enable HSI clock */
RCC->CR |= RCC_CR_HSION;
/* Enable clocks on Port A, B, F */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
/* Enable USART clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
/* Select Alternate Port Functions */
void alt_init(void)
{
/* Port A: USART1 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Port B: USART3 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);
}
usart.c
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Baud rate */
uint32_t bd = 9600;
void usart_init(void)
{
/* ##################### USART 1 ##################### */
/* USART GPIO Settings */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART Settings */
USART_InitStructure.USART_BaudRate = bd;
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 USART because it's disabled in USART_Init */
USART_Cmd(USART1, ENABLE);
/* ##################### USART 2 ##################### */
/* ##################### USART 3 ##################### */
/* Disable USART3 in order to use SWAP */
USART_Cmd(USART3, DISABLE);
/* Swap TX and RX pins */
USART3->CR2 |= USART_CR2_SWAP;
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
/* USART GPIO Settings */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART Settings */
USART_InitStructure.USART_BaudRate = bd;
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(USART3, &USART_InitStructure);
/* Enable USART because it's disabled in USART_Init */
USART_Cmd(USART3, ENABLE);
/* Enable USART Interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
/* Enable external interrupts */
NVIC_InitStructure.NVIC_IRQChannel = USART3_8_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* ##################### USART 4 ##################### */
}
stm32f0xx_it.c
#include <main.h>
void USART3_8_IRQHandler(void)
{
char t[] = "U";
uint16_t byte = 0;
if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET) // Data reception
{
byte = USART_ReceiveData(USART3);
(*pRXD3).Buffer[(*pRXD3).wr] = byte;
(*pRXD3).wr = ((*pRXD3).wr + 1);
USART_SendData(USART1, t[0]);
USART_GetFlagStatus(USART3, USART_IT_ORE);
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
return;
}
The USART_SendData bit is for communication with my PC, so I can see if I receive the letter U or more nonesense.
I have also checked the content of the DR register (by way of checking the Buffer I write its content to) of USART3 directly so I know the STM32F091 receives (or rather interperts) my GPS output wrong.
The GPS module uses a default baudrate of 9600 which I do not change.
Let me know if there's any additional info
After a lot of searching and the suggestions of everyone in the comments, I figured out that the System Clock was not set up properly.
The following pieces of code were necessary in addition to what was already in the 'clock_init'.
peripherals.c
/* Clock settings */
void clock_init(void)
{
....
/* Select System Clock to be HSI */
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
/* Select USART clock source to be System Clock */
RCC_USARTCLKConfig(RCC_USART1CLK_SYSCLK);
RCC_USARTCLKConfig(RCC_USART3CLK_SYSCLK);
....
}
Thanks to everyone for the help.
Related
i'm implementing a project using stm32f101cb microcontroller. I understand that this control have 3 timer in it. TIM2 and TIM4 had been set to encoder mode to capture 2 rotary encoder.
I'm now left me the only timer TIM3, I would to ask is there possible for me to set TIM3 to PWM mode and at the same time doing the normal timer interrupt? I could not find it in reference manual or programming manual.
Thanks!
It should be possible. I'm doing it for an STM32F091RC I've developed an application for where I'm controlling LED:s with PWM and generating a timer update interrupt (counter reset) with the same timer peripheral.
Here's some of my configuration code:
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* LED_TIM clock enable */
RCC_APB1PeriphClockCmd(TIM_LED_RCC, ENABLE);
/* LED_PORT Configuration: Channel 1, 2, 3 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = LED_PIN;
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;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(LED_PORT, LED_PIN_SOURCE, LED_TIM_AF);
/* Enable the TIM global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = LED_TIM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = LED_TIM_PSC;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Max 0xFFFF
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = LED_INIT_PULSE;
TIM_OC1Init(LED_TIM, &TIM_OCInitStructure); // Init Channel 1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM IT enable */
TIM_ITConfig(LED_TIM, TIM_IT_Update, ENABLE); // Enable interrupts for LED_TIM
/* LED_TIM counter enable */
TIM_Cmd(LED_TIM, ENABLE);
/* LED_TIM Main Output Enable */
TIM_CtrlPWMOutputs(LED_TIM, ENABLE);
Should be very similar for your device. If everything works correctly, you should be getting interrupts in your TIM3_IRQHandler ISR
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(LED_TIM, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(LED_TIM, TIM_IT_Update);
// Do ISR stuff here!
}
}
I have just started my adventures with STM32f407 Discovery board. I'm using latest stable release of CoIDE and using up to date toolchain/libraries.
I managed to write the following code in order to use USART1 from the board
int main(void){
GPIO_InitTypeDef GPIO_InitStructure; // Definicja struktury do inicjalizacji PINOW
USART_InitTypeDef USART_InitStructure;
// Initialize pins as alternating function
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
// Modify USART_InitStructure for non -default values , e.g.
// USART_InitStructure.USART_BaudRate = 38400;
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_Mode_Rx;
USART_Init(USART1 ,&USART_InitStructure);
USART_Cmd(USART1 , ENABLE);
while(1)
{
while (USART_GetFlagStatus(USART1 , USART_FLAG_TXE) == RESET);
USART1 ->DR = (uint16_t)(45 & 0x01FF);
Delay(0x3FFFFF);
}
}
Also I have made sure clock is configured correctly by setting HSE and other clock details
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
And also
/************************* PLL Parameters *************************************/
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 8
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
/******************************************************************************/
In console I just receive garbage and I have made the following to make sure the setup is correct :
- Checked USB-RS232 converter
- Switched between converter and STM32 board
- Switched converter
- Generated code using STM32CubeMX to compare it and it seems consistent (logic wise of course :) )
From my beginner perspective it is kinda "showstopper" :( I cannnot seem to find a root cause of this
Any help would be appreciated
UPDATE 1 : In order to find root cause I have decided to try one of the other USART modules. Below is my code for USART3 and this one WORKS OUT OF THE BOX which would lead me to incorrect init of USART1 in the initial question ?
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_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_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
SysTick_Config(SystemCoreClock / 1000);
while(1)
{
for (int var = 45; var < 128; var++)
{
USART_SendData(USART3, var);
Delay_SysTick(500); // 1 sek
}
}
}
Resolution: After a lot of digging and trying multiple things it appears that USART1 would collide with USB components and there is capacitor which collides with the transmission link to answer on ST forums
I wish I would have found this somewhere in documentation before.
Hope this helps someone and thanks everyone for your assistance
USART1 on this board (TX pin) has capacitor which collides with traffic. It is mentioned on ST forums.
Garbage on terminal often is caused when the sender has a different baudrate than the receiver.
Recently I have been trying to receive a data from my STM32F407 motion sensor. I have the following code:
uint8_t SPI1_Read(){
GPIO_ResetBits(GPIOA, GPIO_Pin_4); // CS low
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET){}
SPI_I2S_SendData(SPI1, 0x00);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET){}
return SPI_I2S_ReceiveData(SPI1);
}
void SPI1_Write(uint8_t data){
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET){}
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
SPI_I2S_ReceiveData(SPI1);
}
volatile static int vrednost, vrednost1, vrednost2;
int main(void)
{
// Vkljuci driver za GPIOD
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// Nastavi registre za LED diode
GPIO_InitTypeDef inicializacijskaStruktura;
inicializacijskaStruktura.GPIO_Mode = GPIO_Mode_OUT;
inicializacijskaStruktura.GPIO_OType = GPIO_OType_PP;
inicializacijskaStruktura.GPIO_PuPd = GPIO_PuPd_NOPULL;
inicializacijskaStruktura.GPIO_Speed = GPIO_Speed_100MHz;
inicializacijskaStruktura.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOD, &inicializacijskaStruktura);
SPI_and_GPIO_Init();
// init
SPI1_Write(0x20);
SPI1_Write(0x47);
SPI1_Write(0x0F);
SPI1_Write(0xBC);
while(1)
{
SPI1_Write(0x20);
vrednost=SPI1_Read();
SPI1_Write(0x24);
vrednost1=SPI1_Read();
SPI1_Write(0x28);
vrednost2=SPI1_Read();
if(vrednost<122){
GPIO_SetBits(GPIOD, GPIO_Pin_12);
}
else{
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
}
if(vrednost1<122)
GPIO_SetBits(GPIOD, GPIO_Pin_13);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_13);
if(vrednost2<122)
GPIO_SetBits(GPIOD, GPIO_Pin_14);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_14);
}
}
void SPI_and_GPIO_Init(){
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the SPI periph */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Enable SCK, MOSI and MISO GPIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);
/* Enable CS GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* SPI pins configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPI1);
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
/* Enable SPI1 */
SPI_SSOutputCmd(SPI1, ENABLE);
SPI_Cmd(SPI1, ENABLE);
/* Configure GPIO PIN for Lis Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Deselect : Chip Select high */
GPIO_SetBits(GPIOE, GPIO_Pin_3);
}
Upper code works fine, but the problem is that always when I try to read motion sensor, i get 0xFF or in another words 255. I read data by sending dummy text and then read data.
I have been also given an advice to set Slave Select pin (SS) to zero, but I don't know how to do that.
Does anybody know?
To hazard a guess - typically with SPI, SS is driven low (enable) at the start of each write/read then driven high (disable) at the end of each write/read to indicate end of transmission. Without pulling out the board, that's what I'd bet on.
If that's not it - ST is pretty good at providing examples. The source code on their product page includes an example using their MEMs accelerometer (Utilities/STM32F4-Discovery/stm32f4_discovery_lis302dl.c/.h).
I am working with the following evaluation board: SZWB-sail, STM32f103VET6 KIT v3.1
I want to use the stm32f103 usart in synchronous mode, and I used
STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\USART\Synchronous
I modified the code in an attempt to use USART2/SPI1, as opposed to the working code provided via STMicro, which uses USART1/SPI1.
The README for this example states that:
"USARTy and SPIy can be USART1 and SPI1 or USART2 and SPI3, depending on the STMicroelectronics EVAL board you are using."
Despite this, I attempted to physically connect USART2 Tx/Rx/Ck pins (PA2,PA3,PA4) to SPI1 SCK/MISO/MOSI (PA5,PA6,PA7). Is there a software reason for why this won't work? Or might it be hardware connections on the eval board?
Here is my code:
int main(void)
{
SystemInit();
Init_NVIC();
/* System Clocks Configuration */
RCC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
SPI_Configuration();
USART_Configuration();
while(NbrOfDataToRead2--)
{
USART2_Send_Byte(TxBuffer1[TxCounter1++]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
}
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
}
RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPI1);
}
USART2_Receive_Byte();
while(NbrOfDataToRead1--)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET)
{
}
SPI_I2S_SendData(SPI1, TxBuffer2[TxCounter2++]);
USART2_Send_Byte(DYMMY_BYTE);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
}
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{
}
RxBuffer1[RxCounter1++] = USART2_Receive_Byte();
}
TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
while(1)
{
}
}
void Init_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure1,GPIO_InitStructure2;
GPIO_InitTypeDef GPIO_InitStructure3,GPIO_InitStructure4,GPIO_InitStructure5,GPIO_InitStructure6;
/* Configure USART1 Rx as input floating */
GPIO_InitStructure1.GPIO_Pin =GPIO_Pin_10;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure1);
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure2.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure2);
/* Configure USART2 Rx as input floating */
GPIO_InitStructure3.GPIO_Pin =GPIO_Pin_3;
GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure3);
/* Configure USART2 Tx as alternate function push-pull */
GPIO_InitStructure4.GPIO_Pin =GPIO_Pin_2;
GPIO_InitStructure4.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure4.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure4);
/* Configure USART2 Ck as alternate function push-pull */
GPIO_InitStructure5.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure5.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure5.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure5);
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure6.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure6);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure1,USART_InitStructure2;
USART_ClockInitTypeDef USART_ClkInitStructure;
USART_InitStructure1.USART_BaudRate = 115200;
USART_InitStructure1.USART_WordLength =USART_WordLength_8b ;
USART_InitStructure1.USART_StopBits = USART_StopBits_1;
USART_InitStructure1.USART_Parity = USART_Parity_No;
USART_InitStructure1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART1 */
USART_Init(USART1,&USART_InitStructure1);
//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1,ENABLE);
USART_ClkInitStructure.USART_Clock=USART_Clock_Enable;
USART_ClkInitStructure.USART_CPOL=USART_CPOL_High;
USART_ClkInitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
USART_ClockInit(USART2, &USART_ClkInitStructure);
USART_InitStructure2.USART_BaudRate = 115200;
USART_InitStructure2.USART_WordLength =USART_WordLength_8b ;
USART_InitStructure2.USART_StopBits = USART_StopBits_1;
USART_InitStructure2.USART_Parity = USART_Parity_No;
USART_InitStructure2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure2.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART2 */
USART_Init(USART2,&USART_InitStructure2);
//USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
/* Enable the USART2 */
USART_Cmd(USART2,ENABLE);
}
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
SPI_I2S_DeInit(SPI1);
/* SPIy Config */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
/* Configure SPIy */
SPI_Init(SPI1, &SPI_InitStructure);
SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
/* SPIy enable */
SPI_Cmd(SPI1, ENABLE);
}
You are mixing polling mode with interrupt mode. This SPI configuration code is intended for SPI interrupt mode. Hence, SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) should not be used, because this is a function intended for Polling-Mode.
Instead, I believe you can use SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT); in conjunction with SPI_I2S_ReceiveData(SPI_TypeDef* SPIx) and SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT) (to clear any potential Error pending bits, just in-case).
Also, you might want to post your USART2_Send_Byte() code so we know what exactly it's doing, and whether it's calling any other functions...but go ahead and try this first to see if it fixes your problem.
SPI1 conflicts with USART2 synchronization mode.
SPI2 conflicts with USART3 synchronization mode.
There is no conflict between SPI1/2 and USART1 synchronization modes.
I'm coding for stm32 microcontroller in Keil, 2 days ago I copied my source and header file in a project to TASKING, after some problems, all done but there is an error about all my functions declared in source and header files.
would please some one say me whats the problem here?
The Header file:
#ifndef __CONFIG_H
#define __CONFIG_H
#include "stm32f10x.h"
void init(void);
void config_IO(void);
void config_EXTI(void);
void config_TIM4(void);
void config_TIM3(void);
void config_TIM2(void);
void config_USART3(void);
void config_RTC(void);
#endif
** Source file:**
#include "config.h"
// #include "main.h"
const int t_PLCoff = 1000; // time in ms
void init(void)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);// HCLK = 64 MHz, AHB
RCC_PCLK1Config(RCC_HCLK_Div2); // APB1 = 32 MHz
RCC_PCLK2Config(RCC_HCLK_Div1); // APB2 = 64 MHz
/* set up FLASH */
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 4MHz * 16 = 64 MHz */
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);
/* 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);
}
void config_IO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PB9 in Inpu pullup mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/* Initialize GPIOA */
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PC5 in Inpu pullup mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/* Initialize GPIOB */
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PC5 in Inpu pullup mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/* Initialize GPIOB */
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PC5 in Inpu pullup mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
/* Initialize GPIOB */
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void config_EXTI(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PC5 in Inpu pullup mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
/* Initialize GPIOC */
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Connect EXTI5 Line to PC.05 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);
/* Configure EXTI4 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line10;
// EXTI_InitStructure.EXTI_Line = EXTI_Line8 | EXTI_Line9 | EXTI_Line10;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
/* Initilize EXTI */
EXTI_Init(&EXTI_InitStructure);
// /* Enable and set EXTI9_5 Interrupt to the lowest priority */
// NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void config_TIM4(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/* Enable the TIM4 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIM_CKD_DIV1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Initialize TIM4 Interrupt */
NVIC_Init(&NVIC_InitStructure);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Time base configuration */
TIM_TimeBaseInitStructure.TIM_Period = 32000 - 1; // 100 KHz down to 10 Hz (100 ms)
TIM_TimeBaseInitStructure.TIM_Prescaler = 500 - 1; // 36 MHz Clock down to 100 KHz (adjust per your clock)
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* Initialize TIM4 */
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
/* TIM IT enable */
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
void config_TIM3(void) //used for 2Hz output signal timing
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIM_CKD_DIV1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Initialize TIM3 Interrupt */
NVIC_Init(&NVIC_InitStructure);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
TIM_TimeBaseInitStructure.TIM_Period = 32000 - 1; // 1 KHz down to 10 Hz (1 ms)
TIM_TimeBaseInitStructure.TIM_Prescaler = t_PLCoff - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* Initialize TIM3 */
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
/* TIM IT enable */
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
void config_TIM2(void) //used for 2Hz output signal timing
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIM_CKD_DIV1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Initialize TIM2 Interrupt */
NVIC_Init(&NVIC_InitStructure);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseInitStructure.TIM_Period = 32000 - 1; // 1 KHz down to 10 Hz (1 ms)
TIM_TimeBaseInitStructure.TIM_Prescaler = 100 - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* Initialize TIM2 */
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void config_USART1()
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB10 in AF PushPull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
/* Initialize GPIOB */
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB11 in Input mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
/* Initialize GPIOB */
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART3 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART1 in desired options */
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;
/* Initialize USART1 */
USART_Init(USART1, &USART_InitStructure);
/* enable USART1 */
USART_Cmd(USART1, ENABLE);
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
and the ERROR!
Description Resource Path Location ID Type lkarm E163: "init" redeclared with a different type config.c /third 6 7376 C/C++ Problem
This error also repeated for all other functions in my source file!
So I found that its needed to include source.h file in my main.c! but still I don't know WHY?
Problem solved!
thanks all