gpio_test.c 5.61 KB
#include "pi_util.h"
#include <PR/bbsim.h>

#define __GPIO_OUT_MASK(val) ((val)&0xff)


/*
 * GPIO, 1 (GPIO board-id)
 */
void gpioBoardId()
{
    u32 val,initId;
    int i;
    int result = FAIL;

    /* record initial value of board id */
    initId = IO_READ(PI_GPIO_REG);
    initId = ((initId&PI_GPIO_ID_MASK)>>PI_GPIO_ID_SHIFT)&0xffff;

    for(i=6; i<16; i++){
        /* take ide bus out of reset */
        val = IO_READ(PI_IDE_CONFIG_REG);
        IO_WRITE(PI_IDE_CONFIG_REG,val&(~PI_IDE_CONFIG_RESET));
        
        IDE_WRITE(PI_IDE3_SET_BOARDID,(1<<i));
        
        pin_reset();
        
        /* enter secure mode again after pin reset */
        IO_READ(MI_SEC_MODE_REG);
        IO_READ(BOOT_RAM_LO_START);
        
        val = IO_READ(PI_GPIO_REG);

        if((((val&PI_GPIO_ID_MASK)>>PI_GPIO_ID_SHIFT)&0xffff) != (1<<i)){
            _TRACE(DERROR, fprintf(LogFp,
                                   "  ERROR: board id bit %d.\n",i));
            goto printresult;        
        }
    }

    result = PASS;

 printresult:

    /* reset ide bus */
    val = IO_READ(PI_IDE_CONFIG_REG);
    IO_WRITE(PI_IDE_CONFIG_REG,val&(~PI_IDE_CONFIG_RESET));

    /* restore initial board id */
    IDE_WRITE(PI_IDE3_SET_BOARDID,initId);
    pin_reset();

    /* enter secure mode again after pin reset */
    IO_READ(MI_SEC_MODE_REG);
    IO_READ(BOOT_RAM_LO_START);

    val = IO_READ(PI_GPIO_REG);

    printf("gpioBoardId ");
    OUTPUT_TEST_PASSFAIL(result);
    fflush(NULL);

}


/*
 * GPIO, 2 (GPIO signals)
 */
void gpioSignals()
{
    u32 val,gpioIoMask,ideIoMask,ideWriteNibble,gpioWriteNibble;
    int i;
    int result = FAIL;

    /* reset ide bus */
    val = IO_READ(PI_IDE_CONFIG_REG);
    IO_WRITE(PI_IDE_CONFIG_REG,val&(~PI_IDE_CONFIG_RESET));

    /* all gpio pins for read */
    IO_WRITE(PI_GPIO_REG,
             (0x0<<PI_GPIO_ENABLE_SHIFT) | (0x0<<PI_GPIO_DATA_SHIFT));
    IDE_WRITE(PI_IDE3_GPIO_LOOPBACK,
              (0xf<<PI_GPIO_ENABLE_SHIFT) | (0xf<<PI_GPIO_DATA_SHIFT) );
    val = IO_READ(PI_GPIO_REG);
    if( ((val & PI_GPIO_DATA_MASK)>>PI_GPIO_DATA_SHIFT) != 0xf ){
        _TRACE(DERROR, fprintf(LogFp,
                               "  ERROR: all gpio read 1.\n"));
        goto printresult;        
    }
    IDE_WRITE(PI_IDE3_GPIO_LOOPBACK,
              (0xf<<PI_GPIO_ENABLE_SHIFT) | (0x0<<PI_GPIO_DATA_SHIFT) );
    val = IO_READ(PI_GPIO_REG);
    if( ((val & PI_GPIO_DATA_MASK)>>PI_GPIO_DATA_SHIFT) != 0x0 ){
        _TRACE(DERROR, fprintf(LogFp,
                               "  ERROR: all gpio read 0.\n"));
        goto printresult;        
    }

    /* all gpio pins for write */
    IO_WRITE(PI_GPIO_REG,
             (0xf<<PI_GPIO_ENABLE_SHIFT) | (0xf<<PI_GPIO_DATA_SHIFT));
    IDE_WRITE(PI_IDE3_GPIO_LOOPBACK,
              (0x0<<PI_GPIO_ENABLE_SHIFT) | (0x0<<PI_GPIO_DATA_SHIFT) );
    val = (u32)IDE_READ(PI_IDE3_GPIO_LOOPBACK);
    if( ((val & PI_GPIO_DATA_MASK)>>PI_GPIO_DATA_SHIFT) != 0xf ){
        _TRACE(DERROR, fprintf(LogFp,
                               "  ERROR: all gpio write 1.\n"));
        goto printresult;        
    }
    IO_WRITE(PI_GPIO_REG,
             (0xf<<PI_GPIO_ENABLE_SHIFT) | (0x0<<PI_GPIO_DATA_SHIFT));
    val = (u32)IDE_READ(PI_IDE3_GPIO_LOOPBACK);
    if( ((val & PI_GPIO_DATA_MASK)>>PI_GPIO_DATA_SHIFT) != 0x0 ){
        _TRACE(DERROR, fprintf(LogFp,
                               "  ERROR: all gpio write 0.\n"));
        goto printresult;        
    }

    /* one gpio pin enabled for read at a time */
    for(i=0; i<4; i++){
        ideIoMask = 1<<(PI_GPIO_ENABLE_SHIFT+i);
        gpioIoMask = (~ideIoMask)&PI_GPIO_ENABLE_MASK;

        ideWriteNibble = 0xf<<PI_GPIO_DATA_SHIFT;
        gpioWriteNibble = 0x0<<PI_GPIO_DATA_SHIFT;

        IO_WRITE(PI_GPIO_REG,gpioIoMask+gpioWriteNibble);
        IDE_WRITE(PI_IDE3_GPIO_LOOPBACK,ideIoMask+ideWriteNibble);

        /* insure read values are correct */
        val = IO_READ(PI_GPIO_REG);
        if( (val & (ideIoMask>>PI_GPIO_ENABLE_SHIFT)) != 
            (ideWriteNibble&(ideIoMask>>PI_GPIO_ENABLE_SHIFT)) ){
            _TRACE(DERROR, fprintf(LogFp,
                                   "  ERROR: gpio read pin %d, read 1.\n",i));
            goto printresult;
        }

        /* insure write values are correct */
        val = (u32)IDE_READ(PI_IDE3_GPIO_LOOPBACK);
        if( (val & (gpioIoMask>>PI_GPIO_ENABLE_SHIFT)) != 
            (gpioWriteNibble&(gpioIoMask>>PI_GPIO_ENABLE_SHIFT)) ){
            _TRACE(DERROR, fprintf(LogFp,
                                   "  ERROR: gpio read pin %d, write 0.\n",i));
            goto printresult;
        }

        ideWriteNibble = 0x0<<PI_GPIO_DATA_SHIFT;
        gpioWriteNibble = 0xf<<PI_GPIO_DATA_SHIFT;

        IO_WRITE(PI_GPIO_REG,gpioIoMask+gpioWriteNibble);
        IDE_WRITE(PI_IDE3_GPIO_LOOPBACK,ideIoMask+ideWriteNibble);

        /* insure read values are correct */
        val = IO_READ(PI_GPIO_REG);
        if( (val & (ideIoMask>>PI_GPIO_ENABLE_SHIFT)) != 
            (ideWriteNibble&(ideIoMask>>PI_GPIO_ENABLE_SHIFT)) ){
            _TRACE(DERROR, fprintf(LogFp,
                                   "  ERROR: gpio read pin %d, read 0.\n",i));
            goto printresult;
        }

        /* insure write values are correct */
        val = (u32)IDE_READ(PI_IDE3_GPIO_LOOPBACK);
        if( (val & (gpioIoMask>>PI_GPIO_ENABLE_SHIFT)) != 
            (gpioWriteNibble&(gpioIoMask>>PI_GPIO_ENABLE_SHIFT)) ){
            _TRACE(DERROR, fprintf(LogFp,
                                   "  ERROR: gpio read pin %d, write 1.\n",i));
            goto printresult;
        }
    }

    result = PASS;

 printresult:
    printf("gpioSignals ");
    OUTPUT_TEST_PASSFAIL(result);
    fflush(NULL);
    
}