Were can i find UART basic code?

LAMAR
2014-04-24
2014-04-27
  • LAMAR

    LAMAR - 2014-04-24

    Hi
    i am looking for a basic sample code to read data from UART(basic one, whit to DATA available and read)
    I already successes to send data to UART but no read,I am using atmega328 FREERTOS c code (AVR studio)
    can you please help me?
    maybe you have a basic project to read send data?
    thanks
    lama

     
  • Phillip Stevens

    Phillip Stevens - 2014-04-25

    There are a variety of different USART code types available here. Also look in arduino and avrfreak example code for help.

    I have three examples in the AVRfreeRTOS platform. Look in lib_io/serial.c for these options.

    avrSerialRead() avrSerialWrite() code uses polling, not interrupts, to receive and transmit data. This consumes resources to wait for characters to arrive though.

    xSerialGetChar() xSerialPutChar() code uses interrupts to receive and transmit data. Intermediate store of characters is in a fast ring buffer, written by Dean Camera.

    Alternative xSerialGetChar() xSerialPutChar() code using the freeRTOS queues is available in freeRTOS730 zip and earlier. This is slower code, but uses inherent freeRTOS queue functionality, rather than the ring buffer library.

    I've also abstracted the code to work with multiple USART ports. The selector for which USART is to be used is contained in the xComPortHandlePtr variable.

     
    Last edit: Phillip Stevens 2014-04-27
    • LAMAR

      LAMAR - 2014-04-25

      Hi Phillip
      Thank you very match for your help I am really Appreciate you.
      I try to check some function but I don’t know what the problem is, see the next code:
      1)
      While(1)
      {
      While(xSerialAvailableChar(&xSerialPort)==0);//sty here till data will receive

      Print_string(“data was receive”);//my function to print string
      }
      The “xSerialAvailableChar” function not working,the CPU is run at the while loop and not detect any incoming number of data.
      What I am doing wrong?,
      I am using atmega328 c code (avr studio)
      2)
      I try the avrSerialxRead function

             while(avrSerialxRead(&xSerialPort)!='s');//s is the start 
             do
              {
              data=avrSerialxRead(&xSerialPort);
              print_char(data);
              }
              while (data!='.');//exit if you get '.'
      

      it is work fine……but only inside the main function ,the same function not working at task function outside of the main.i am geting random data.
      What can be the problem???

      Thank
      lama

       
      • Phillip Stevens

        Phillip Stevens - 2014-04-27

        lama,

        you are mixing the two versions of the Serial code. The avrSerial or avrSerialx code is only for use before the FreeRTOS scheduler is in place.

        ONLY use avrSerial code before you enable the scheduler, OR after you expect the scheduler to be broken in an interrupt for errors or faults. You can see this usage in my main() loop in any example of my code.

        In the examples, avrSerial is used in main() because it is either before the vTaskStartScheduler() function is called, or after it returns. It only ever returns if there is an error, such as there being not enough space to create the idle task stack. i.e. there is a stack problem.

        Use the xSerial functions when the scheduler is working, which is all the time for normal operation. These functions use interrupts to read and write characters from ring buffers. Testing for available characters is then simply a matter of seeing if the receive ring buffer has non-zero contents. You'll need to test this in a loop, whenever you're ready to handle each character.

        The simplest example to do this is to use xSerialGetChar to test if a char is in the ring buffer, which returns false if there is none, and then use xSerialPutChar to echo it back to you.

        while (1)
        {
          if( xSerialGetChar( xSerialPort, &MyChar) )
            xSerialPutChar( xSerialPort, MyChar);
        }
        

        This will get characters from Port1 if they are available, and echo them back out the same port.

        I find it is useful to regularly flush the receive buffer, using a timer function, like this.

        stimer_set( &secs_timer, 4 ); // flush every 4 seconds
        while(1)
        {
          if( xSerialGetChar( &xSerial1Port, &character ) )
          {
            stimer_reset( &secs_timer );
            xSerialPutChar( &xSerialPort, character);
          }
        
          if( xSerialGetChar( &xSerialPort, &character ) )
          {
            stimer_reset( &secs_timer );
            xSerialPutChar( &xSerial1Port, character);
        //  xSerialPutChar( &xSerialPort, character); // debug, echo characters typed.
          }
        
          if(stimer_expired(&secs_timer))
          {
            xSerialFlush( &xSerialPort );  // always flush Rx regularly
            xSerialFlush( &xSerial1Port ); // always flush Rx regularly
            stimer_reset( &secs_timer );   // reset the timer once per flush cycle.
          }
        }
        

        Good luck. P.

         
        Last edit: Phillip Stevens 2014-04-27
    • redgecombe

      redgecombe - 2014-04-27

      Phillip:

      Hi,
      First, many thanks for your FreeRTOS 2560 work. I'm impressed with your work, and have just today got it working on the bench. (I had some hassles along the way - but all self-inflicted.)

      I need to have 2 UART ports running at the same time, and you mentioned
      "abstracted the code to work with multiple USART ports". Where can I find that code?

      (I'm currently running the existing lib-io/serial.c interrupt driven code. I'd like to stay with interrupt driven. Polling isn't going to do the job.)

      Thanks,

      Roger Edgecombe
      Melbourne, Australia.

       
  • Phillip Stevens

    Phillip Stevens - 2014-04-27

    Roger,

    thanks for the support. Its mainly a collection of other people's work, but I do feel happy that all those contributions come together so well in the ATmega platform.

    The lib_io/serial.c code is abstracted to use two USARTs currently, but there is no problem just to add more code along the same existing structure to add as many USARTs as the hardware has available.

    When you initialise each serial port with xSerialPortInitMinimal(), use the eCOMPort enumerator for either USART0 or USART1 (or finish the code for the additional USARTs you need). There is a xComPortHandle returned that you then save and use, to select which USART you are then reading from or writing to, for each xSerialx...() command. Check in the serial.c to see what is happening.

    Note again it is only the code that passes a xComPortHandlePtr that has multiple USART capability. The avrSerial and xSerial code is only implemented for USART0, for historical compatibility purposes.

    Note you have to use specific names for the two xComPortHandle that you define in your program, because their names are used by the Interrupt code in serial.c. Mainly because I couldn't invest time to work out how to further abstract the Interrupt code to allow arbitrary names for each of the xComPortHandle. And, I had to define a default xComPortHandle variable for the compatibility functions that aren't passed the xComPortHandle.

    /* Create reference to the handle for the serial port, USART0. */
    /* This variable is special, as it is used in the interrupt */
    extern xComPortHandle xSerialPort;
    
    /* Create reference to the handle for the other serial port, USART1. */
    /* This variable is special, as it is used in the interrupt */
    extern xComPortHandle xSerial1Port;
    

    The main extension for additional USARTs is just to set up the additional interrupt code. Any extension to more USARTS just needs to replicate the existing code.

    Let me know if this is too cryptic.

    Best. P.

     
    Last edit: Phillip Stevens 2014-04-27
    • redgecombe

      redgecombe - 2014-04-27

      Phillip:

      Many thanks for the prompt reply. Two UARTS will keep me happy. I'm about to take your response and ferret through the code until comprehension dawns.

      This will end up in a DRO (digital readout) on my milling machine.

      cheers
      rde

       
    • redgecombe

      redgecombe - 2014-04-27

      Phillip:

      Ouch! I somehow had a different serial.c,supporting but a single UART. All is now clear. Another own-goal for me. :(

      Thank you - I'll fix that - and I can get started on building my application.

      I gather you are in Australia. Whereabouts?

      cheers
      rde

       
  • LAMAR

    LAMAR - 2014-04-27

    Hi Phillip
    Thanks for your help
    Thanks
    lama

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks