Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
KUn_4718781
Level 1
Level 1

Hi,

I think I found a memory overwrite issue, and would like to confirm my understanding of what is going on.

I have found something out about the automatic DMA buffer, and I would like to confirm my understanding.  I had tried IN_BUFFER[8] and OUT_BUFFER[8],

and my program wasn't working correctly.  A counting variable kept getting overridden at approximately the ### location.  I then changed to IN_BUFFER[32] and OUT_BUFFER[32]. 

With these new buffer lengths the issue was resolved.  Please see https://www.cypress.com/file/376416/download pg 63.

"The DMA reading from or writing into the endpoint buffer happens in 32 byte chunks per request".  

I contrast that with the code definition of ReadOutEp: "Valid values are between 0 and 1023", in which it speaks about the third parameter.

I think that I was overriding the other variables, because my buffer sizes were too small.  Can I get a confirmation of this please?

#include "project.h"

#include <stdio.h>

#define IN_ENDPOINT 0x01

#define OUT_ENDPOINT 0x02

#define MAX_NUM_BYTES 1

#define PACKET_OK 1

#define PACKET_BAD 0

#define maxLength 19    // 20 long -> array element 19

// These are for the keypads

#define Pin_DM_STRONG (0x06u)

#define Pin_DM_RES_UP (0x02u)

uint8 IN_Data_Buffer[8] = {0};

uint8 OUT_Data_Buffer[8] = {0} ;

uint8 PacketNum = 0;

uint8 packetOk = 0;

char textIn[20];

char textOut[20];

uint8 textInLength = 0;

uint8 textInIndex = 0;

uint8 textOutLength = 0;

uint8 textOutIndex = 0;

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    // Start the Screen

   

    LCD_Start();

    Timer_Start();

    keyTimer_Start();

    LCD_ISR_StartEx(LCD_Interrupt);

    keyISR_StartEx(Keypad_ISR);

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    USB_Start(0,USB_DWR_VDDD_OPERATION);

    #define UnassignedAddress 0

    while(USB_deviceAddress == UnassignedAddress)

    {

        // Wait for device address to be assigned

    }

    while(!(USB_GetConfiguration()));

    for(;;)

    {

        uint8 interruptState;

        interruptState = CyEnterCriticalSection();

        /* Place your application code here. */

        // the primary USB send and receive function

        OUT_COUNT = 0;

        // ensure that device is configured

        //if(USB_bGetConfiguration() )

        //{

            if(USB_IsConfigurationChanged()) //make happen every time

            {

                //USB_GetInterfaceSetting(0); // works without this line???

                USB_LoadEP(IN_ENDPOINT, IN_Data_Buffer, 8);

                USB_ReadOutEP(OUT_ENDPOINT, OUT_Data_Buffer, OUT_COUNT);

                USB_EnableOutEP(OUT_ENDPOINT);

            }

          //CyDelay(1);

//############################################################################################################

            if ((USB_GetEPState(IN_ENDPOINT) == USB_IN_BUFFER_EMPTY ) ) //Data to send

            {

                USB_LoadEP(IN_ENDPOINT, NULL, 8);

                //USB_EnableOutEP(OUT_ENDPOINT);

            }

            //CyDelay(1);

            if(USB_GetEPState(OUT_ENDPOINT) == USB_OUT_BUFFER_FULL)   /// Data to Receive

            {

//############################################################################################################

                OUT_COUNT = USB_GetEPCount(OUT_ENDPOINT);

                USB_EnableOutEP(OUT_ENDPOINT);

            }

        //}

        if (OUT_COUNT != 0)  

        {

            char tempChar = (char)OUT_Data_Buffer[0];

           

            // this loop resets the OUT line on LCD

            // once it is full and more text has arrived

            if(textOutLength > maxLength)

            {

                textOutLength = 0;

                //LCD_Position(1u,0u);

                // this loop blanks the OUT line on LCD

                // it is 21 elements long, even though the screen is 20

                // b/c I append a \0 when sending the text from PC

                for(int z = 0; z <= 20; z++)

                {

                    textOut = ' ';

                }

                int bob = 0;

            }    

            if (tempChar != 0)

            {

                textOut[textOutLength] = tempChar;

                textOutLength = textOutLength + 1;

               

                IN_Data_Buffer[0] = OUT_Data_Buffer[0];

                if(textInLength > maxLength)

                {

                    textInLength = 0;

                    textInIndex = 0;    // should I update this one here???

                    for(int i = 0; i < 20; i++)

                    {

                        textIn = ' ';

                    }

                }

                textIn[textInLength] = IN_Data_Buffer[0];

                textInLength = textInLength + 1;

            }    

        }

        CyExitCriticalSection(interruptState);

    }

}

0 Likes
1 Solution
Ekta
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hi,

You are correct, when using DMA with Automatic Buffer Management each Endpoint is assigned 32 bytes of space in the USB Buffer. In this case USBFS_ReadOutEP() function configure DMA to transfer data from endpoint buffer to system RAM (OUT_Buffert that you have created). and generates DMA request. For this Buffer Management data is transferred in chunks of 32 bytes from the endpoint to the OUT_Buffer, thus increasing the size of OUT Buffer in your case resolves the issue.

The value of the length parameter of the API can range from 0 to 1023. This parameter will help in deciding the number of DMA burst required for the complete data to be transferred.

Best Regards

Ekta

View solution in original post

0 Likes
1 Reply
Ekta
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hi,

You are correct, when using DMA with Automatic Buffer Management each Endpoint is assigned 32 bytes of space in the USB Buffer. In this case USBFS_ReadOutEP() function configure DMA to transfer data from endpoint buffer to system RAM (OUT_Buffert that you have created). and generates DMA request. For this Buffer Management data is transferred in chunks of 32 bytes from the endpoint to the OUT_Buffer, thus increasing the size of OUT Buffer in your case resolves the issue.

The value of the length parameter of the API can range from 0 to 1023. This parameter will help in deciding the number of DMA burst required for the complete data to be transferred.

Best Regards

Ekta

0 Likes