SAM3X8E - PDC is shifting data by 1 byte

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

amkaer
Posts: 2
Joined: Wed Oct 22, 2014 2:07 pm

SAM3X8E - PDC is shifting data by 1 byte

Wed Oct 22, 2014 2:16 pm

Hi

I'm experiencing something peculiar with my MCU.
I'm using the PDC to read data from a TWI with an accelerometer. The first read after reset looks correct, but the next reads are shifted one byte and therefore begins with a zero or 255.
I'm reading 6 bytes, but made accel_data 7 bytes in case the last byte was ending up after the second last.
Any idea what could be doing this?

Log of data read:

Code: Select all

2014-10-22 14:02:36,118 - INFO # > Using pos: 200735A8
2014-10-22 14:02:36,119 - INFO # 113 255 202 255 055 255 000 
2014-10-22 14:02:46,118 - INFO # Using pos: 200735A8
2014-10-22 14:02:46,122 - INFO # 000 113 255 203 255 054 000 
2014-10-22 14:02:56,120 - INFO # Using pos: 200735A8
2014-10-22 14:02:56,124 - INFO # 255 114 255 202 255 054 000 
2014-10-22 14:03:06,122 - INFO # Using pos: 200735A8
2014-10-22 14:03:06,127 - INFO # 255 113 255 202 255 055 000 
2014-10-22 14:03:16,125 - INFO # Using pos: 200735A8
2014-10-22 14:03:16,129 - INFO # 255 114 255 203 255 053 000 
2014-10-22 14:03:26,127 - INFO # Using pos: 200735A8
2014-10-22 14:03:26,131 - INFO # 255 113 255 203 255 055 000 
2014-10-22 14:03:36,130 - INFO # Using pos: 200735A8
2014-10-22 14:03:36,132 - INFO # 255 114 255 202 255 055 000 
2014-10-22 14:03:46,133 - INFO # Using pos: 200735A8
2014-10-22 14:03:46,134 - INFO # 255 114 255 203 255 056 000 
2014-10-22 14:03:56,132 - INFO # Using pos: 200735A8
2014-10-22 14:03:56,136 - INFO # 255 114 255 202 255 054 000 
2014-10-22 14:04:06,134 - INFO # Using pos: 200735A8
2014-10-22 14:04:06,139 - INFO # 255 114 255 201 255 055 000 
2014-10-22 14:04:16,137 - INFO # Using pos: 200735A8
2014-10-22 14:04:16,141 - INFO # 255 113 255 201 255 053 000 
2014-10-22 14:04:26,140 - INFO # Using pos: 200735A8
2014-10-22 14:04:26,143 - INFO # 255 113 255 203 255 055 000 
2014-10-22 14:04:36,142 - INFO # Using pos: 200735A8
2014-10-22 14:04:36,146 - INFO # 255 114 255 201 255 052 000
Code for reading:

Code: Select all

uint8_t accel_data[7];
TWI0->TWI_RPR = accel_data;
            TWI0->TWI_RCR = sizeof(accel_data)-1;
            TWI0->TWI_MMR = 0;
            TWI0->TWI_MMR = TWI_MMR_DADR(ACCEL_DEVADDRESS) | TWI_MMR_MREAD | TWI_MMR_IADRSZ_1_BYTE;
            TWI0->TWI_IADR = ACCEL_DATAX0;
            TWI0->TWI_IER = TWI_IER_ENDRX;




            TWI0->TWI_CR = TWI_CR_START;
            TWI0->TWI_PTCR = TWI_PTCR_RXTEN;

Interrupt:

Code: Select all

void isr_twi0(void)
{
    TWI0->TWI_IDR = TWI_IDR_ENDRX;
    TWI0->TWI_CR = TWI_CR_STOP;
    int16_t xaccel = (int16_t)((accel_data[1]<<8)|accel_data[0]);
    int16_t yaccel = (int16_t)((accel_data[3]<<8)|accel_data[2]);
    int16_t zaccel = (int16_t)((accel_data[5]<<8)|accel_data[4]);
    int i;
//    printf("ACCEL : X=%d\t Y=%d\t Z=%d\n",xaccel,yaccel,zaccel);
    for  (i = 0; i < 7; i++)
    {
        printf("%03d ",accel_data[i]);
    }
        puts("");
    initiator_state = 0;
}

EDIT: With a logic analyzer I've looked at the bus and the data is coming over correctly, and it is the same whether it is the first read command or subsequent ones.
amkaer
Posts: 2
Joined: Wed Oct 22, 2014 2:07 pm

Re: SAM3X8E - PDC is shifting data by 1 byte

Wed Oct 22, 2014 3:31 pm

A lecturer of mine solved it.
Apparently the Receive Holding Register is not reset afterwards, so a 
TWI0->TWI_RHR;
fixes the issue.

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests