0
\$\begingroup\$

I have an STM32F4-Discovery Board with an attached STM32F4DIS-BB Base Board attached, giving me access to a Serial Port to use for the Discovery board. I am trying to program the board to read data sent to the serial port from the PC. I am having trouble finding information online on how to do this, so I am posting here trying to get some help.

I am trying to use the Discovery Board's USART1 connection, and have been initializing it with the following code:

void Init_USART() {
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  RCC_AHB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  USART_InitStruct.USART_BaudRate = 115200;
  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  USART_InitStruct.USART_Parity = USART_Parity_No;
  USART_InitStruct.USART_StopBits = USART_StopBits_1;
  USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  USART_Init(USART1, &USART_InitStruct);  
  USART_Cmd(USART1, ENABLE);
}

I am trying to use an interrupt handler, and have initialized it with the following code:

void Init_Interrupt() {
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStruct);
}

Now I am trying to see if the interrupt handler is even working, by just putting a printf() statement within the handler's code like so:

void USART1_IRQHandler(void) {
  printf("test\n");
}

When I run my program however, I am getting nothing coming through the console window, which to me is indicating there is a problem somewhere with what I am trying to do, however I am not sure exactly what it is that is wrong. If anyone could help it would be greatly appreciated.

\$\endgroup\$
  • 1
    \$\begingroup\$ Printing from an IRQ handler is generally a bad idea. Start by having your program print in an infinite loop within main(), or better yet print an incrementing count. Then look with a scope if you don't get any received data. \$\endgroup\$ – Chris Stratton Mar 4 '18 at 18:56
  • \$\begingroup\$ @ChrisStratton you mean in main run a print that prints off a value that is incremented in the IRQHandler? \$\endgroup\$ – Skitzafreak Mar 4 '18 at 19:02
  • 1
    \$\begingroup\$ No, don't use an interrupt or worry about receiving at all. Start by just confirming that you can send unprompted unidirectional data from the STM32 to the PC, or at least make the lines wiggle. Then move on, for example, non-interrupt receive. When you try to do everything at once, it ends up likely that you are fighting several mistakes and oversights at the same time. \$\endgroup\$ – Chris Stratton Mar 4 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.