UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

mikroC, mikroBasic and mikroPascal for PRO ARM® MCUs, supporting STM32, Tiva, Kinetis, & CEC devices
Post Reply
Author
Message
p77
Posts: 10
Joined: 03 Nov 2016 16:33

UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#1 Post by p77 » 21 May 2023 08:12

Hello everyone,

I am sorry about the location of the topic, i really couldn't decide where to put this so I hope here will be fine.

Hardware/Software description:
A. Clicker 2 for STM32 (STM32F407ZG) - Coded in MIKROC PRO for ARM - Programmed with CODEGRIP
B. MIKROMEDIA 7 Capacitive FPI with SIBRAIN STM32F767ZG (MIKROE 3476) - Coded in NECTO / MIKROC AI for ARM - Programmed with CODEGRIP

Target:
I want to obtain UART communication between these two devices.

Issue:
Reading data is never triggered.

Oscilloscope Channels description:
Channel 1(Upper signal on pictures) - TX of CLICKER 2 / RX of MIKRO7
Channel 2(Lower signal on pictures) - RX of CLICKER 2 / TX of MIKRO7

CLICKER2 for STM32 Code:

Code: Select all

    //UART INITIALISATION ON Clicker2 for STM32 Board
        UART2_Init_Advanced(115200, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART2_PD56);
        UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
        Delay_ms(500);
    //END OF UART INITIALISATION

    //<While 1 function in Main>
    while(1)
    {
    OUT1_ON;
    LD1=1;
    Delay_ms(5);
    ScanCycle();
    LD1=0;
    datac=0x7F;
    UART2_Write(datac);
    datac=0xCC;
    UART2_Write(datac);
    }
Code above run on the CLICKER2 board is resulting in this signal on the TX line
Image

MIKRO7 Code for testing
Declarations:

Code: Select all

// UART driver context structure.
static uart_t uart;
// UART driver configuration structure.
static uart_config_t uart_cfg;
// Be careful to have large enough buffers.
static uint8_t uart_rx_buffer[128];
// Be careful to have large enough buffers.
static uint8_t uart_tx_buffer[128];
static size_t size;
INITIALISATION

Code: Select all

uart_configure_default(&uart_cfg);
uart_cfg.rx_pin = SHIELD_CONNECTOR_J2_PIN21;
uart_cfg.tx_pin = SHIELD_CONNECTOR_J2_PIN22;
uart_cfg.tx_ring_size = sizeof( uart_tx_buffer );
uart_cfg.rx_ring_size = sizeof( uart_rx_buffer );

// Initialize appropriate interrupt and allocate resources for UART module.
if ( uart_open( &uart, &uart_cfg ) == UART_ERROR )
{
  showme2();// Error handling strategy
}
// Set baud rate.
if ( uart_set_baud( &uart, 115200 ) == UART_ERROR )
{
  showme2();// Error handling strategy
}
CYCLIC TX TEST

Code: Select all

void application_task()
{

   main_screen_show(&vtft);
   vtft_process(&vtft);
   
   // CYCLIC TX TEST
   uart_tx_buffer[0]=0x7f;
   uart_tx_buffer[1]=0xCC;
   size=2;
   size = uart_write( &uart, uart_tx_buffer, size );
   Delay_ms(5);
    
   // READ ATTEMPT
   if ( uart_bytes_available( &uart ) )
    {
    size = uart_read( &uart, uart_rx_buffer, sizeof( uart_rx_buffer ) );
    showme();  
    }

}
Having both codes run simultaneously I could capture this:

Image

This is sufficient to say that both Boards / MCUs have properly initialized UART modules.
Both can Transmit and have the same Speed/Freq/Baud rate. Encoding is the same etc.

if ( uart_bytes_available( &uart ) ) on MIKRO7 was never triggered over counless variations of speed, baudrate, parity variations.

HARDWARE ISSUES
I can assure you all that the RX TX lines are crossed over between the devices and the connection is proper and good.
Both devices are on the same power supply, sharing GND.
Either way, oscilloscope captures can really rule out any wiring and pinout issues.

Can someone please give any clues on where i could be wrong and why my read function is never triggered?

Thank you all for taking time to read this.

Best regards,
Nermin

p77
Posts: 10
Joined: 03 Nov 2016 16:33

Re: UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#2 Post by p77 » 22 May 2023 19:57

Hello everyone,

I made some progress and workarounds. After all, it seems that the:

Code: Select all

if ( uart_bytes_available( &uart ) )
is always returning 0 so the reading never happens, but have changed into this:

Code: Select all

  size = uart_read( &uart, uart_rx_buffer, sizeof( uart_rx_buffer ) );
  if (size)
       {
       showme();    
       }
Which is far from ideal but seems to work. Luckily I went all in with the MCU's so a couple of additional cycles shouldn't make a problem.

BUT, I have another issue i need a quick assistance with:

Code: Select all

int RX_Comm_Protocol()
{ 
  unsigned receive;
  char delimiter[3];
  int i, j, k, l;
  int trash;
  delimiter[0]= 0x01;
  delimiter[1]= 0x03;

  
  
  if (UART2_Data_Ready() == 1)
    {
    //Reading 10 bytes
    for (i=0; i<=9; i++)
        {
        rx_data[i]=UART2_Read();       
        }
    // taking out the trash and clearing rx buffer    
    while  (UART2_Data_Ready() == 1) {trash=UART2_Read();} 
    }
  else
    {
    return 2;// No data ready
    }
    
  if ((rx_data[1]==0x10)&&(rx_data[2]==0x21))
  {
  //Proper Start
  strcpy(pass1,rx_data);
  }
  else
  {
  return 3;//GIBBERISH received
  }
    return 0;
}
Now I am having this code (Above) on Clicker2 for STM32, and the UART hardware buffer seems to be inactive so this is probably a question for developers:
- Is UART library on MikroC pro for ARM implementing hardware buffer or not?

Because when I send 10 Bytes to the MCU, all of the operations finish up until stepped into RX_Comm_Protocol() function where the function
UART2_Data_Ready() returns 1, but please take note that the actual transfer finished maybe 100ms before. (at this point I expect the received data to be in buffer ready for reading)
rx_data=UART2_Read(); gets stuck as if the buffer is empty eg. no bytes available. At this moment I need to send another 10 Bytes
which then read properly.

I would really like to use the hardware buffer because this is quite a large project and I am trying to avoid chopping up the code to bits and pieces with interrupts and watchdogs.

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#3 Post by IvanJeremic » 24 May 2023 09:41

Hi,

The Uart library does implement the hardware buffer both in Necto Studio and in MikroC PRO for ARM.

Also why not write the code for both of your boards in Necto Studio, since it does support both Clicker 2 for STM32 and the MiKROMEDIA 7 Capacitive?

Regards,

Ivan.

p77
Posts: 10
Joined: 03 Nov 2016 16:33

Re: UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#4 Post by p77 » 30 May 2023 09:02

Dear,

To be honest I would prefer to use MikroC pro for ARM for both boards but Mikroe nowadays makes a lot of hardware-software wise incompatible equipment, hence I buy MikroC pro for ARM to get the job done but it can't work with Mikromedia7 HMI and STM32F767ZG
NECTO is giving me nightmares. I had pretty nice communication functions working great and checking for errors etc. and all of a sudden a few errors pop out regarding CMAKE, fix them and now the uart cannot be initialized / set to correct baud rate. To elaborate right now I have this code running:

Code: Select all

uart_t uart;
uart_config_t uart_cfg;
uint8_t uart_rx_buffer[128];
uint8_t uart_tx_buffer[128];
size_t size;

int Comm_Init(){
    
uart_configure_default(&uart_cfg);
uart.tx_ring_buffer = uart_tx_buffer;
uart.rx_ring_buffer = uart_rx_buffer;
uart_cfg.rx_pin = SHIELD_CONNECTOR_J2_PIN21;
uart_cfg.tx_pin = SHIELD_CONNECTOR_J2_PIN22;
uart_cfg.tx_ring_size = sizeof( uart_tx_buffer );
uart_cfg.rx_ring_size = sizeof( uart_rx_buffer );

uart_open( &uart, &uart_cfg) ;
//Delay_us(10000);
uart_set_baud( &uart, 115200);
 return 1;   
}
But what is actually going on on the hardware side I have an actual baud rate of about 1500000.
I have tried countless variations, reducing to just uart on the whole project and turning off everything else.

My code stayed the same, it just stopped configuring the uart correctly.
I reinstalled the NECTO and done everything i can think of. Its amazing how much time and money can be lost looking at 10 lines of code and not being able to do anything more about it.
I am preparing another PC, currently reinstalling the OS as uninstalling NECTO does not get rid of all traces and data.
I suspect compiler issue, but honestly just thinking about throwing out all of this and changing hardware completely.

Image

Not even sure why i wrote this nor what to expect back....

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#5 Post by IvanJeremic » 09 Jun 2023 07:20

Hi,

Sorry i was not of much help here, if you get more issues with your project, you can send me the entire project so i can try to find what might be the problem.

Regards,

Ivan.

p77
Posts: 10
Joined: 03 Nov 2016 16:33

Re: UART ISSUES - CLICKER2 for STM32 to MIKROMEDIA 7

#6 Post by p77 » 16 Jun 2023 21:18

WOW!

That was impressive, 11 days for a "we're sorry" reply.
The issue has been fixed. The problem is somewhere on "your side" of the compilers but a Windows + Necto + MikroC reinstallation and copying code through Notepad++ worked.

Thanks for your h...
nevermind.

Post Reply

Return to “ARM PRO Compilers”