ben-arduino-usb

Annotated shift.c

14:2da6d7a3fe1b
2013-02-13 Paul Boddie Introduced a specific control request function since the data involved in such requests appears to be handled separately from other outgoing data, as are the transactions associated with such setup data.
paul@0 1
#include <ubb/ubb.h>
paul@0 2
#include <stdio.h>
paul@0 3
#include <time.h>
paul@0 4
paul@0 5
/* Pin assignments:
paul@0 6
 *
paul@0 7
 *           M74HC595B1R
paul@0 8
 *
paul@0 9
 *           QB |----| VCC   UBB_VDD
paul@0 10
 *           QC |    | QA
paul@0 11
 *           QD |    | SI    UBB_DAT2
paul@0 12
 *           QE |    | G     UBB_DAT0
paul@0 13
 *           QF |    | RCK   UBB_DAT1
paul@0 14
 *           QG |    | SCK   UBB_CLK
paul@0 15
 *           QH |    | SCLR  UBB_CMD
paul@0 16
 * UBB_GND  GND |----| QH'   UBB_DAT3
paul@0 17
 */
paul@0 18
paul@0 19
#define SHIFT_SI    UBB_DAT2
paul@0 20
#define SHIFT_QH    UBB_DAT3
paul@0 21
#define SHIFT_SCLR  UBB_CMD
paul@0 22
#define SHIFT_SCK   UBB_CLK
paul@0 23
#define SHIFT_G     UBB_DAT0
paul@0 24
#define SHIFT_RCK   UBB_DAT1
paul@0 25
paul@0 26
struct timespec tSCKs   = {0, 15},
paul@0 27
                tWH     = {0, 15},
paul@0 28
                tWL     = {0, 15};
paul@0 29
paul@0 30
void shift_in(uint8_t v)
paul@0 31
{
paul@0 32
    uint8_t mask;
paul@0 33
paul@0 34
    for (mask = 0x80; mask; mask >>= 1)
paul@0 35
    {
paul@0 36
        if (v & mask)
paul@0 37
        {
paul@0 38
            printf("1");
paul@0 39
            SET(SHIFT_SI);
paul@0 40
        }
paul@0 41
        else
paul@0 42
        {
paul@0 43
            printf("0");
paul@0 44
            CLR(SHIFT_SI);
paul@0 45
        }
paul@0 46
paul@0 47
        nanosleep(&tSCKs, NULL);
paul@0 48
        SET(SHIFT_SCK);
paul@0 49
        nanosleep(&tWH, NULL);
paul@0 50
        CLR(SHIFT_SCK);
paul@0 51
    }
paul@0 52
paul@0 53
    printf("\n");
paul@0 54
}
paul@0 55
paul@0 56
uint8_t shift_out()
paul@0 57
{
paul@0 58
    uint8_t mask, result = 0;
paul@0 59
paul@0 60
    for (mask = 0x80; mask; mask >>= 1)
paul@0 61
    {
paul@0 62
        if (PIN(SHIFT_QH))
paul@0 63
        {
paul@0 64
            result |= mask;
paul@0 65
            printf("1");
paul@0 66
        }
paul@0 67
        else
paul@0 68
            printf("0");
paul@0 69
paul@0 70
        nanosleep(&tSCKs, NULL);
paul@0 71
        SET(SHIFT_SCK);
paul@0 72
        nanosleep(&tWH, NULL);
paul@0 73
        CLR(SHIFT_SCK);
paul@0 74
    }
paul@0 75
paul@0 76
    printf("\n");
paul@0 77
    return result;
paul@0 78
}
paul@0 79
paul@0 80
int main(int argc, char *argv[])
paul@0 81
{
paul@0 82
    uint8_t result = 0;
paul@0 83
paul@0 84
    if (ubb_open(0) < 0) {
paul@0 85
        perror("ubb_open");
paul@0 86
        return 1;
paul@0 87
    }
paul@0 88
paul@0 89
    ubb_power(1);
paul@0 90
    printf("Power on.\n");
paul@0 91
paul@0 92
    OUT(SHIFT_SI);
paul@0 93
    OUT(SHIFT_RCK);
paul@0 94
    OUT(SHIFT_SCLR);
paul@0 95
    OUT(SHIFT_SCK);
paul@0 96
    OUT(SHIFT_G);
paul@0 97
    IN(SHIFT_QH);
paul@0 98
paul@0 99
    CLR(SHIFT_SCK);
paul@0 100
    CLR(SHIFT_RCK);
paul@0 101
paul@0 102
    /* Clear the register. */
paul@0 103
paul@0 104
    CLR(SHIFT_SCLR);
paul@0 105
    SET(SHIFT_SCK);
paul@0 106
    nanosleep(&tWL, NULL);
paul@0 107
    CLR(SHIFT_SCK);
paul@0 108
    SET(SHIFT_SCLR);
paul@0 109
paul@0 110
    /* Shift in a value. */
paul@0 111
paul@0 112
    shift_in(0xa6);
paul@0 113
paul@0 114
    /* Shift out the value. */
paul@0 115
paul@0 116
    result = shift_out();
paul@0 117
paul@0 118
    printf("Result: %x\n", result);
paul@0 119
paul@0 120
    printf("Closing...\n");
paul@0 121
    ubb_close(0);
paul@0 122
paul@0 123
    return 0;
paul@0 124
}