cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 simple uart transmit interrupt

rolly loysney
Associate II
Posted on April 10, 2018 at 13:14

Hello,

i've followed many tutorial to try to get UART with interrupt working on my stm32l476rg (nucleo board) :

at the main.c, before while(1) loop :

HAL_UART_Transmit_IT(&huart4, (uint8_t *)welcome_str,sizeof(welcome_str));

this doesn't work, but in polling mode, it's working :

HAL_UART_Transmit(&huart4, (uint8_t *)welcome_str,sizeof(welcome_str),100);

i'm using hc-06 bluetooth dongle, and receiving it on android Phone, it should be able to receive interrupt without any problem...?

what i'm missing here ? any triggering stuff or ..?

thank you

#uart-it #stm32l4+
1 ACCEPTED SOLUTION

Accepted Solutions
rolly loysney
Associate II
Posted on April 11, 2018 at 00:16

hum, it seems Interrupt wasn't enabled in NVIC uart init... :

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(huart->Instance==UART4)

  {

  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_UART4_CLK_ENABLE();

 

    /**UART4 GPIO Configuration    

    PC10     ------> UART4_TX

    PC11     ------> UART4_RX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;

    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(UART4_IRQn);

  }

}

after adding last two lines it's fine (y)

View solution in original post

10 REPLIES 10
rolly loysney
Associate II
Posted on April 10, 2018 at 22:17

up!

Posted on April 10, 2018 at 23:04

For the interrupt to work:

It must be enabled on the device

It must be enabled on the NVIC

The vector table needs to point to a UART4_IRQHandler

You need your own UART4_IRQHandler

For the HAL the IRQ handler needs to call the HAL_UART_IRQHandler(&UartHandle4)

>>what i'm missing here ? any triggering stuff or ..?

Probably, got zero visibility into your implementation from here..

Perhaps review some examples?

STM32Cube_FW_L4_V1.10.0\Projects\STM32L476RG-Nucleo\Examples\UART\UART_TwoBoards_ComIT

Use strlen() rather than sizeof()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rolly loysney
Associate II
Posted on April 11, 2018 at 00:16

hum, it seems Interrupt wasn't enabled in NVIC uart init... :

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(huart->Instance==UART4)

  {

  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_UART4_CLK_ENABLE();

 

    /**UART4 GPIO Configuration    

    PC10     ------> UART4_TX

    PC11     ------> UART4_RX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;

    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(UART4_IRQn);

  }

}

after adding last two lines it's fine (y)

Posted on April 10, 2018 at 23:08

>>up!

Yeah, let's not do that, it's annoying.

Consider if the lack of responses is due to inadequacies in the original post/question. If you feel the need to bump things, try to add something to the context either with more code, or project content instead of pulling two lines out of the air and wondering why it doesn't work.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 10, 2018 at 23:53

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6qn&d=%2Fa%2F0X0000000bwj%2FqjqCkaEwR6NQ_vAjQ4cQU.qO2svB9_wdFj.hFYGdVZs&asPdf=false
Posted on April 11, 2018 at 01:39

rolly loysney wrote:

after adding last two lines it's fine (y)

So please mark the thread as answered:

https://community.st.com/0D50X00009XkXxjSAF

 
Poonam Deoghare
Associate III
Posted on June 05, 2018 at 12:20

Hi,

I am working on STM32F413H disco EVAL. UART2 is interfaced to BLE modem.

From BLE modem , it is further sent to the iOS app.

I have enabled interrupt in NVIC setting also.

If I send the data in following way, I am able to see it on the app.

uint8_t *ConnectResp = 'ConnectOK\n\r';

HAL_UART_Transmit_IT(&huart2,ConnectResp,11);

I have following issue.

I want to send the CRC with data so I am trying to send the data in following way.

uint8_t payload[20];

    payload[0] = 0x39;   //0x39 is 9 which is length of the data

    payload[1] = 'C';

    payload[2] = 'o';

    payload[3] = 'n';

    payload[4] = 'n';

    payload[5] = 'e';

    payload[6] = 'c';

    payload[7] = 't';

    payload[8] = 'O';

    payload[9] = 'K';

    payload[10] = (uint8_t)((CRC_ConnectResp & 0xFFFFFFFF) >> 24); // CRC-ConnectResp is holding the 4byte CRC

    payload[11] = (uint8_t)((CRC_ConnectResp & 0xFFFFFF) >> 16);

    payload[12] = (uint8_t)((CRC_ConnectResp & 0xFFFF) >> 8);

    payload[13] = (uint8_t)(CRC_ConnectResp & 0xFF);

     payload[14] = '\n';

    payload[15] = '\r';

HAL_UART_Transmit_IT(&huart2,payload,16);

When I print this payload -->  '9ConnectOKøtg'

On the app ---> getting data in hex format only once.

Poonam Deoghare
Associate III
Posted on June 05, 2018 at 14:02

Thanks Clive.

I have one more issue w.r.t. transmit.

 If I follow sequence 'receive after transmit or vice-versa' then it works fine.

But if I do 'transmit transmit ' then I am able to get only first few bytes of first transmit, even though I am checking txDone=1.

My Code looks like this:-

volatile bool  txDone = 0; 

uint8_t Msg1[21] = 'Raw Data-Transfer\n\r';

HAL_UART_Transmit_IT(&huart2,payload,16);  //payload is same as above

if(txDone == 1)   //txDone = 1 in tx callback function

{

HAL_UART_Transmit_IT(&huart2,Msg1,21);

txDone = 0;

}

I am able to get only -- > '9Con'  //first 4 bytes of first transmit.

Anything wrong with my code or transmit should follow receive ?

Thanks,

Poonam

Posted on June 05, 2018 at 13:34

As the CRC is going to use the full 8-bit space you're going to get non-printable ASCII characters.

If you need ASCII consider using sprintf(buf,'%08X', CRC_ConnectResp);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..