AnsweredAssumed Answered

stm32F105rb usart

Question asked by darek on Apr 22, 2014
Latest reply on Apr 22, 2014 by Clive One
Hi,
i have problems with usart communication with other cpu. I use stm32f150rb, UART4. My code:
01.RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
02.RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
03.RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
04.RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
05. 
06.GPIO_InitTypeDef gpioInit;
07.// rx = PC11
08.gpioInit.GPIO_Speed = GPIO_Speed_2MHz;
09.gpioInit.GPIO_Mode = GPIO_Mode_IN_FLOATING;
10.gpioInit.GPIO_Pin = GPIO_Pin_11;
11.GPIO_Init(GPIOC, &gpioInit);
12. 
13.// tx = PC10
14.gpioInit.GPIO_Speed = GPIO_Speed_2MHz;
15.gpioInit.GPIO_Mode = GPIO_Mode_AF_PP;
16.gpioInit.GPIO_Pin = GPIO_Pin_10;
17.GPIO_Init(GPIOC, &gpioInit);
18. 
19./* Configure the NVIC Preemption Priority Bits */
20.NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0 );
21. 
22./* Enable the USARTy Interrupt */
23.NVIC_InitTypeDef NVIC_InitStructure;
24.NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
25.NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
26.NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
27.NVIC_Init(&NVIC_InitStructure);
28. 
29.USART_DeInit(UART4 );
30.USART_InitTypeDef usartInit;
31.usartInit.USART_BaudRate = 9600;
32.usartInit.USART_WordLength = USART_WordLength_8b;
33.usartInit.USART_StopBits = USART_StopBits_1;
34.usartInit.USART_Parity = USART_Parity_No ;
35.usartInit.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
36.usartInit.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
37.USART_Init(UART4, &usartInit);
38.USART_Cmd(UART4, ENABLE);
39. 
40.USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
41. 
42.void UART4_IRQHandler() {
43.    uint16_t rec = USART_ReceiveData(UART4 );
44.    reciv_arr[receiv++] = rec & 0xFF;
45.    if (receiv == 100 || reciv_arr[receiv - 1] == '\r'
46.            || reciv_arr[receiv - 1] == '\n') {
47.        receiv = 0;
48.    }
49.}
50. 
51.void send(char* buffer, int len) {
52.    int i = 0;
53.    for (; i < len; ++i) {
54.        while (USART_GetFlagStatus(UART4, USART_FLAG_TXE ) == RESET)
55.            ;
56.        USART_SendData(UART4, buffer[i]);
57.    }
58.}
Sending looks ok - TXE is changing. RX cpu pin is connected to GND with 5k6 resistor if this matters. RX interrupt is never called.
Does the init code looks ok?

Outcomes