view touch.c @ 50:d7207a9d3c3b

Add write support. LFN still broken though. Make sure we wait for the card to be done after a read or write (didn't seem to break reading but hosed writes).
author Daniel O'Connor <darius@dons.net.au>
date Fri, 05 Apr 2013 00:08:31 +1030
parents 58d76cf522ff
children
line wrap: on
line source

#include <stdint.h>

#include "stm32f10x.h"
#include "delay.h"
#include "spi.h"
#include "touch.h"

#define TP_SELECT()	GPIO_ResetBits(GPIOB, GPIO_Pin_7)
#define TP_DESELECT()	GPIO_SetBits(GPIOB, GPIO_Pin_7)

uint16_t
tp_read(uint8_t type) { 
    uint16_t x = 0;

    /* Select device */
    TP_SELECT();

    /* Do conversion */
    delay(10);
    SPI_WriteByte(type);

    /* Read result */
    delay(10);
    x = SPI_WriteByte(0x00);
    x <<= 8;
    x |= SPI_WriteByte(0x00);
    delay(10);

    /* De-select device */
    TP_DESELECT();

    /* Right justify 12 bit result */
    x = x >> 3;
    return (x);
}

void
tp_getcoords(uint16_t *x, uint16_t *y, uint16_t *z1, uint16_t *z2, float *t, float *t2) {
    *x = tp_read(TP_READ_SEL(TP_CHAN_X, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
    *y = tp_read(TP_READ_SEL(TP_CHAN_Y, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
    *z1 = tp_read(TP_READ_SEL(TP_CHAN_Z1, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
    *z2 = tp_read(TP_READ_SEL(TP_CHAN_Z2, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
    *t = ((float)*x / 4096.0) * (((float)*z2 / (float)*z1) - 1);
    *t2 = (((float)*x / 4096) * ((4096.0 / (float)*z1) - 1)) - (1 - ((float)*y / (float)4096.0));
}