8 Replies Latest reply on Apr 20, 2012 9:42 AM by kartik.mankad

    Simple USB to UART

    user_183705841

       Hi,

         

      Can anyone please help me how to transfer UART to USB interface using the PSoC5: I have TX,RX,RTS,CTS connected to the PSoC5 pins and I want to transfer them into USB interface like the FTDI chip. I looked in all the examples I can find in the Creator2.0 and still don't understand how to make this simple...

         

      Thanks,

         

      Michael H.

        • 1. Re: Simple USB to UART
          udayan.umapathi

          From your statment I understand that, you have an FTDI connected to your PC via USB and you want to interface PSoC to FTDI via UART RX TX, correct ? If that is the case, may be you should simply place an UART component on the TopDesign, configure it, assign proper pins and put some additional code to initialise the component and to transfer data. You can get a very basic UART code example by right click on your UART component and selecting "Find Example Project".

          • 2. Re: Simple USB to UART
            udayan.umapathi

             You can as well drag and drop the USBUART Macro component available in the component catalog on to your TopDesign. In this case you can connect PSoC to PC directly and use the USBUART inetrface provided by PSoC.

            • 3. Re: Simple USB to UART
              user_183705841

               NO, I don't have ftdi chip on my board. I have TX, RX, CTS, RTS connected to the PSoC5 and I want to convert them to the USB Interfave (pins P15_6 and P15_7). In another words I need a module that can do that: UART -> PSoC5 -> USB 

              • 4. Re: Simple USB to UART
                user_183705841

                 To make my question even simpler, what I need to change in the following USBUART example code in order to do what I asked above: 

                   

                 

                   

                  /* Start USBFS Operation with 3V operation */

                   

                    USBUART_1_Start(0, USBUART_1_3V_OPERATION);

                   

                /* Wait for Device to enumerate */

                   

                    while(!USBUART_1_GetConfiguration());

                   

                    /* Enumeration is done, enable OUT endpoint for receive data from Host */

                   

                    USBUART_1_CDC_Init();

                   

                 

                   

                for(;;)

                   

                    {

                   

                        if(USBUART_1_DataIsReady() != 0u)                /* Check for input data from PC */

                   

                        {   

                   

                            count = USBUART_1_GetAll(buffer);           /* Read received data and re-enable OUT endpoint */

                   

                            if(count != 0u)

                   

                            {

                   

                                while(USBUART_1_CDCIsReady() == 0u);    /* Wait till component is ready to send more data to the PC */ 

                   

                                USBUART_1_PutData(buffer, count);        /* Send data back to PC */

                   

                            }

                   

                        }  

                   

                        

                   

                        state = USBUART_1_IsLineChanged();              /* Check for Line settings changed */

                   

                        if(state != 0u)

                   

                        {  

                   

                            if(state & USBUART_1_LINE_CODING_CHANGED)   /* Show new settings */

                   

                            {

                   

                                sprintf(lineStr,"BR:%4ld,DB:%d",USBUART_1_GetDTERate(),(uint16)USBUART_1_GetDataBits());

                   

                                //LCD_Position(0,0);

                   

                                //LCD_PrintString("                    ");

                   

                                //LCD_Position(0,0);

                   

                                //LCD_PrintString(lineStr);

                   

                                //sprintf(lineStr,"SB:%s,Parity:%s", stop[(uint16)USBUART_1_GetCharFormat()], \

                   

                                //                                     parity[(uint16)USBUART_1_GetParityType()]);

                   

                                //LCD_Position(1,0);

                   

                                //LCD_PrintString("                    ");

                   

                                //LCD_Position(1,0);

                   

                                //LCD_PrintString(lineStr);

                   

                            }

                   

                            if(state & USBUART_1_LINE_CONTROL_CHANGED)  /* Show new settings */

                   

                            {   

                   

                                state = USBUART_1_GetLineControl();

                   

                                sprintf(lineStr,"DTR:%s,RTS:%s",  (state & USBUART_1_LINE_CONTROL_DTR) ? "ON" : "OFF", \

                   

                                                                    (state & USBUART_1_LINE_CONTROL_RTS) ? "ON" : "OFF");

                   

                                //LCD_Position(1,0);

                   

                                //LCD_PrintString("                    ");

                   

                                //LCD_Position(1,0);

                   

                                //LCD_PrintString(lineStr);

                   

                            

                   

                            }

                   

                        }

                   

                    }   

                   

                Thanks,

                   

                Michael H.

                • 5. Re: Simple USB to UART
                  udayan.umapathi

                   Michael, I understand that you have data coming in from UART over to PSoC5 and you need to throw this out of PSoC via USB correct ?

                     

                   

                     

                   

                     

                  If that is the case you need have a UART interface to first receive data over UART as shown above. Configure the UART for necessry BAUD RATE. And make Pin connections as necessary. Assign RX and TX pin in the CYDWR file to Pins of your choice. First intiliase your UART component in the code you pasted above by calling, UART_Start(); Remove all the code from the for loop. Use the API UART_GetByte(), and use the API. Copy the data read from UART in to a buffer. Then use the API 

                     

                                  USBUART_1_PutData(buffer, count);

                     

                   

                     

                  to transfer data over USBUART to PC. However note that you need to have the handshaking and status check code around this simple code to get a relibly working code. I hope this should atleast get you going.

                     
                  • 6. Re: Simple USB to UART
                    user_183705841

                     OK, I will try to do that. Thanks!

                    • 7. Re: Simple USB to UART
                      user_183705841

                       OK, the code is working great now. This is the working code for simple USB to UART:

                         

                      // Before you copy this code make sure to copy and paste the USBUART_1 module from the USB_UART example 

                         

                      void main()

                         

                      {

                         

                          char8 buffer[1]; //buffer array

                         

                       

                         

                         /* Enable Global Interrupts */

                         

                          CyGlobalIntEnable;  

                         

                       

                         

                      /* Start UART */

                         

                          UART_Start(); 

                         

                          UART_ClearTxBuffer();

                         

                       

                         

                      /* Start USBFS Operation with 3V operation */

                         

                      USBUART_1_Start(0, USBUART_1_3V_OPERATION);

                         

                      /* Wait for Device to enumerate */

                         

                      while(!USBUART_1_GetConfiguration());

                         

                         /* Enumeration is done, enable OUT endpoint for receive data from Host */

                         

                      USBUART_1_CDC_Init();

                         

                      // ---- USB Interface Starts---- //

                         

                          while(1)

                         

                          {

                         

                      buffer[0]=UART_GetChar();

                         

                      if (buffer[0]>0) //If byte received 

                         

                      {

                         

                      while(USBUART_1_CDCIsReady() == 0u); //Wait till component is ready to send more data to the PC 

                         

                      USBUART_1_PutData(buffer,1);

                         

                      }

                         

                       

                         

                      if(USBUART_1_DataIsReady() != 0u) //Check for input data from PC  

                         

                      {

                         

                      USBUART_1_GetData(buffer, 1);

                         

                      UART_PutChar(buffer[0]);

                         

                      }

                         

                       

                         

                      }// End While(1)

                         

                      // ---- USB Interface Ends---- //

                         

                      }// End Main

                      • 8. Re: Simple USB to UART
                        kartik.mankad

                        Shows how simple the PSoC is to use! .. if only it were that cheap.. :-P