From 9c74a081313a8681d2a47a15b9a0e228289a5c25 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Sun, 25 Nov 2012 23:33:10 +0100 Subject: [PATCH] implemented MCU touchscreen driver --- drivers/touchscreen/MCU/touchscreen_lld.c | 111 +++++++++++++----- .../touchscreen/MCU/touchscreen_lld_config.h | 21 ++-- src/touchscreen.c | 8 +- 3 files changed, 98 insertions(+), 42 deletions(-) diff --git a/drivers/touchscreen/MCU/touchscreen_lld.c b/drivers/touchscreen/MCU/touchscreen_lld.c index 46db4fa0..f8c32ffd 100644 --- a/drivers/touchscreen/MCU/touchscreen_lld.c +++ b/drivers/touchscreen/MCU/touchscreen_lld.c @@ -32,16 +32,38 @@ #if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/ +#define ADC_NUM_CHANNELS 2 +#define ADC_BUF_DEPTH 1 /*===========================================================================*/ /* Driver local variables. */ /*===========================================================================*/ -#if !defined(__DOXYGEN__) - /* Local copy of the current touchpad driver */ - static const TouchscreenDriver *tsDriver; +static const TouchscreenDriver *ts; -#endif +static const ADCConversionGroup adc_y_config = { + FALSE, + ADC_NUM_CHANNELS, + NULL, + NULL, + 0, 0, + 0, 0, + ADC_SQR1_NUM_CH(ADC_NUM_CHANNELS), + 0, + ADC_SQR3_SQ2_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN13) +}; + +static const ADCConversionGroup adc_x_config = { + FALSE, + ADC_NUM_CHANNELS, + NULL, + NULL, + 0, 0, + 0, 0, + ADC_SQR1_NUM_CH(ADC_NUM_CHANNELS), + 0, + ADC_SQR3_SQ2_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11) +}; /** * @brief Low level Touchscreen driver initialization. @@ -50,31 +72,10 @@ * * @notapi */ -void ts_lld_init(const TouchscreenDriver *ts) { - tsDriver = ts; +void ts_lld_init(const TouchscreenDriver *ts_init) { + ts = ts_init; - /* set pins to analog input */ - palSetPadMode(ts->ts_yd_port, ts->ts_yd_pin, PAL_MODE_INPUT_ANALOG); - palSetPadMode(ts->ts_yu_port, ts->ts_yu_pin, PAL_MODE_INPUT_ANALOG); - palSetPadMode(ts->ts_xl_port, ts->ts_xl_pin, PAL_MODE_INPUT_ANALOG); - palSetPadMode(ts->ts_xr_port, ts->ts_xr_pin, PAL_MODE_INPUT_ANALOG); -} - - -/** - * @brief Reads a conversion from the touchscreen - * - * @param[in] cmd The command bits to send to the touchscreen - * - * @return The read value 12-bit right-justified - * - * @note This function only reads data, it is assumed that the pins are - * configured properly and the bus has been acquired beforehand - * - * @notapi - */ -uint16_t ts_lld_read_value(uint8_t cmd) { - return 0; + adcStart(ts->adc_driver, NULL); } /** @@ -85,6 +86,7 @@ uint16_t ts_lld_read_value(uint8_t cmd) { * @notapi */ static void ts_lld_filter(void) { + return 0; } @@ -96,7 +98,27 @@ static void ts_lld_filter(void) { * @notapi */ uint16_t ts_lld_read_x(void) { - return 0; + uint16_t val1, val2; + adcsample_t samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH]; + + palSetPadMode(ts->yd_port, ts->yd_pin, PAL_MODE_INPUT_ANALOG); + palSetPadMode(ts->yu_port, ts->yu_pin, PAL_MODE_INPUT_ANALOG); + palSetPadMode(ts->xl_port, ts->xl_pin, PAL_MODE_OUTPUT_PUSHPULL); + palSetPadMode(ts->xr_port, ts->xr_pin, PAL_MODE_OUTPUT_PUSHPULL); + + palSetPad(ts->xl_port, ts->xl_pin); + palClearPad(ts->xr_port, ts->xr_pin); + chThdSleepMilliseconds(1); + adcConvert(&ADCD1, &adc_x_config, samples, ADC_BUF_DEPTH); + val1 = ((samples[0] + samples[1])/2); + + palClearPad(ts->xr_port, ts->xr_pin); + palSetPad(ts->xl_port, ts->xl_pin); + chThdSleepMilliseconds(1); + adcConvert(&ADCD1, &adc_x_config, samples, ADC_BUF_DEPTH); + val2 = ((samples[0] + samples[1])/2); + + return ((val1+((1<<12)-val2))/4); } /** @@ -105,7 +127,27 @@ uint16_t ts_lld_read_x(void) { * @notapi */ uint16_t ts_lld_read_y(void) { - return 0; + uint16_t val1, val2; + adcsample_t samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH]; + + palSetPadMode(ts->xl_port, ts->xl_pin, PAL_MODE_INPUT_ANALOG); + palSetPadMode(ts->xr_port, ts->xr_pin, PAL_MODE_INPUT_ANALOG); + palSetPadMode(ts->yd_port, ts->yd_pin, PAL_MODE_OUTPUT_PUSHPULL); + palSetPadMode(ts->yu_port, ts->yu_pin, PAL_MODE_OUTPUT_PUSHPULL); + + palSetPad(ts->yu_port, ts->yu_pin); + palClearPad(ts->yd_port, ts->yd_pin); + chThdSleepMilliseconds(1); + adcConvert(&ADCD1, &adc_y_config, samples, ADC_BUF_DEPTH); + val1 = ((samples[0] + samples[1])/2); + + palClearPad(ts->yu_port, ts->yu_pin); + palSetPad(ts->yd_port, ts->yd_pin); + chThdSleepMilliseconds(1); + adcConvert(&ADCD1, &adc_y_config, samples, ADC_BUF_DEPTH); + val2 = ((samples[0] + samples[1])/2); + + return ((val1+((1<<12)-val2))/4); } /* @@ -116,9 +158,14 @@ uint16_t ts_lld_read_y(void) { * @notapi */ uint8_t ts_lld_pressed(void) { - return 0; -} + palSetPadMode(ts->yd_port, ts->yd_pin, PAL_MODE_INPUT_PULLDOWN); + palSetPadMode(ts->yu_port, ts->yu_pin, PAL_MODE_INPUT); + palSetPadMode(ts->xl_port, ts->xl_pin, PAL_MODE_INPUT); + palSetPadMode(ts->xr_port, ts->xr_pin, PAL_MODE_OUTPUT_PUSHPULL); + palSetPad(ts->xr_port, ts->xr_pin); + return palReadPad(ts->yd_port, ts->yd_pin); +} #endif /* GFX_USE_TOUCHSCREEN */ /** @} */ diff --git a/drivers/touchscreen/MCU/touchscreen_lld_config.h b/drivers/touchscreen/MCU/touchscreen_lld_config.h index e57d5723..800518ae 100644 --- a/drivers/touchscreen/MCU/touchscreen_lld_config.h +++ b/drivers/touchscreen/MCU/touchscreen_lld_config.h @@ -38,15 +38,20 @@ #define TOUCHSCREEN_HAS_PRESSED TRUE #define TOUCHSCREEN_HAS_PRESSURE FALSE +/** + * @brief The touchscreen driver struct + * @details Pointer to this will be passed to tsInit() + */ struct TouchscreenDriver { - ioportid_t ts_yd_port; - ioportmask_t ts_yd_pin; - ioportid_t ts_yu_port; - ioportmask_t ts_yu_pin; - ioportid_t ts_xl_port; - ioportmask_t ts_xl_pin; - ioportid_t ts_xr_port; - ioportmask_t ts_xr_pin; + ADCDriver *adc_driver; + ioportid_t yd_port; + ioportmask_t yd_pin; + ioportid_t yu_port; + ioportmask_t yu_pin; + ioportid_t xl_port; + ioportmask_t xl_pin; + ioportid_t xr_port; + ioportmask_t xr_pin; }; #endif /* GFX_USE_TOUCHSCREEN */ diff --git a/src/touchscreen.c b/src/touchscreen.c index 0fa5402e..265243a4 100644 --- a/src/touchscreen.c +++ b/src/touchscreen.c @@ -184,7 +184,7 @@ coord_t tsReadX(void) { y = _tsReadRealY(); #endif - _tsTransform(&x, &y); + //_tsTransform(&x, &y); switch(gdispGetOrientation()) { case GDISP_ROTATE_0: @@ -218,7 +218,7 @@ coord_t tsReadY(void) { y = _tsReadRealY(); #endif - _tsTransform(&x, &y); + //_tsTransform(&x, &y); switch(gdispGetOrientation()) { case GDISP_ROTATE_0: @@ -276,6 +276,9 @@ coord_t tsReadY(void) { * @api */ void tsCalibrate(void) { +#if 0 + + const uint16_t height = gdispGetHeight(); const uint16_t width = gdispGetWidth(); const coord_t cross[][2] = {{(width / 4), (height / 4)}, @@ -362,6 +365,7 @@ calibrate: #if TOUCHSCREEN_STORE_CALIBRATION ts_store_calibration_lld(cal); #endif +#endif } #endif /* GFX_USE_TOUCHSCREEN */