Atmel Software Framework

rtouch.c File Reference
#include <stdint.h>
#include <stdbool.h>
#include "board.h"
#include "compiler.h"
#include "assert.h"
#include "gpio.h"
#include "intc.h"
#include "rtouch.h"
#include "conf_rtouch.h"
#include "adc.h"

Enumerations

enum  rtouch_state_enum {
  RTOUCH_DISABLED,
  RTOUCH_NOT_TOUCHED,
  RTOUCH_READING_XL,
  RTOUCH_READING_XH,
  RTOUCH_READING_YL,
  RTOUCH_READING_YH,
  RTOUCH_PROCESSING,
  RTOUCH_TOUCHED
}
 Driver state names. More...
 

Functions

void rtouch_adc_int_handler (void)
 ADC conversion complete interrupt handler. More...
 
static void rtouch_clear_adc_flag (void)
 
static void rtouch_clear_detect_flag (void)
 
void rtouch_compute_calibration_matrix (rtouch_calibration_points_t const *points, rtouch_calibration_matrix_t *matrix)
 Compute a raw coordinate to screen coordinate calibration matrix. More...
 
void rtouch_detect_int_handler (void)
 Touch screen low-level detect interrupt handler. More...
 
void rtouch_disable (void)
 Disables the resistive touch screen driver. More...
 
static void rtouch_disable_adc_int (void)
 Disable interrupts for the touch channels. More...
 
static void rtouch_disable_detect_int (void)
 
void rtouch_enable (void)
 Enables touch detection of the driver once initialized. More...
 
static void rtouch_enable_adc_int (void)
 Enable interrupts for the touch channels. More...
 
static void rtouch_enable_detect_int (void)
 
static uint32_t rtouch_get_adc_value (void)
 Return last converted value. More...
 
void rtouch_get_calibration_matrix (rtouch_calibration_matrix_t *destination)
 Gets the currently set touch driver calibration matrix. More...
 
void rtouch_get_event (rtouch_event_t *event)
 Retrieve the last touch event. More...
 
rtouch_event_handler_t rtouch_get_event_handler (void)
 Gets the currently set touch screen event handler callback function. More...
 
static void rtouch_gradient_x_surface (void)
 Drive voltage gradient on X surface (XL=GND, XH=VDD). More...
 
static void rtouch_gradient_y_surface (void)
 Drive voltage gradient on Y surface (YL=GND, YH=VDD). More...
 
static void rtouch_ground_x_surface (void)
 Drive X lines to ground. More...
 
static void rtouch_ground_y_surface (void)
 Drive Y lines to ground. More...
 
void rtouch_init (void)
 
static bool rtouch_is_detect (void)
 Test if a touch is detected. More...
 
bool rtouch_is_touched (void)
 Determine if the screen is currently being touched. More...
 
static void rtouch_prepare_adc (void)
 Prepare ADC for touch measuring. More...
 
static void rtouch_prepare_detect (void)
 Prepare the rtouch detect function. More...
 
static void rtouch_process_samples (void)
 Called from ADC ISR to compute coordinates from raw samples. More...
 
static void rtouch_pullup_y_surface (void)
 Enable pull-ups for Y lines. More...
 
static void rtouch_resample (void)
 Called from rtouch_process_samples or from timer module to start a new sample. More...
 
void rtouch_set_calibration_matrix (rtouch_calibration_matrix_t const *source)
 Set the touch driver calibration matrix. More...
 
void rtouch_set_event_handler (rtouch_event_handler_t handler)
 Set the touch screen event handler callback function. More...
 
static void rtouch_start_read (uint32_t channel)
 
static void rtouch_tristate_x_surface (void)
 
static void rtouch_tristate_y_surface (void)
 

Variables

struct {
   rtouch_calibration_matrix_t *   calibration_matrix
 Current calibration matrix. More...
 
   rtouch_event_handler_t   event_handler
 Current event handler, NULL if no handler defined. More...
 
   rtouch_event_t   last_event
 Last touch event. More...
 
   int32_t   rawX
 Temporary raw sample values for X position. More...
 
   int32_t   rawY
 Temporary raw sample values for Y position. More...
 
   enum rtouch_state_enum   state
 Current driver state. More...
 
rtouch
 
static const gpio_map_t rtouch_gpio_xmap
 
static const gpio_map_t rtouch_gpio_ymap
 

Driver state names.

Todo:
Handle PRR registers, especially shared access to ADC.
Enumerator
RTOUCH_DISABLED 

Driver is disabled, no sampling will occur.

RTOUCH_NOT_TOUCHED 

Driver enabled, but panel not touched.

RTOUCH_READING_XL 

Waiting for XL line sample.

RTOUCH_READING_XH 

Waiting for XH line sample.

RTOUCH_READING_YL 

Waiting for YL line sample.

RTOUCH_READING_YH 

Waiting for YH line sample.

RTOUCH_PROCESSING 

Waiting for sample processing.

RTOUCH_TOUCHED 

Panel is currently touched.

void rtouch_adc_int_handler ( void  )

ADC conversion complete interrupt handler.

This interrupt handler is called repeatedly as multiple samples are taken from all touch panel lines in proper order. When all samples have been taken, the ISR defers further handling to the TOUCH_ProcessSamples() function.

References Assert, rtouch, rtouch_get_adc_value(), rtouch_gradient_x_surface(), rtouch_ground_x_surface(), rtouch_process_samples(), RTOUCH_PROCESSING, rtouch_pullup_y_surface(), RTOUCH_READING_XH, RTOUCH_READING_XL, RTOUCH_READING_YH, RTOUCH_READING_YL, rtouch_start_read(), and rtouch_tristate_y_surface().

Referenced by rtouch_prepare_adc().

static void rtouch_clear_adc_flag ( void  )
static

Referenced by rtouch_enable().

static void rtouch_clear_detect_flag ( void  )
inlinestatic
void rtouch_detect_int_handler ( void  )

Touch screen low-level detect interrupt handler.

This interrupt handler is called when a low level on the YL/YH lines is detected, which means the panel is touched. The driver sets up and starts a sample of the XL line, and defers further handling to the ADC interrupt.

References Assert, rtouch, rtouch_disable_detect_int(), rtouch_gradient_y_surface(), RTOUCH_NOT_TOUCHED, RTOUCH_READING_XL, rtouch_start_read(), RTOUCH_TOUCHED, and rtouch_tristate_x_surface().

Referenced by rtouch_prepare_detect().

static void rtouch_disable_adc_int ( void  )
inlinestatic

Disable interrupts for the touch channels.

References adc.

Referenced by rtouch_disable().

static void rtouch_disable_detect_int ( void  )
inlinestatic
static void rtouch_enable_adc_int ( void  )
inlinestatic

Enable interrupts for the touch channels.

References adc.

Referenced by rtouch_enable().

static void rtouch_enable_detect_int ( void  )
inlinestatic
static uint32_t rtouch_get_adc_value ( void  )
inlinestatic

Return last converted value.

References adc.

Referenced by rtouch_adc_int_handler().

static void rtouch_gradient_x_surface ( void  )
inlinestatic

Drive voltage gradient on X surface (XL=GND, XH=VDD).

References gpio_clr_gpio_pin(), gpio_disable_pin_pull_up(), gpio_enable_gpio_pin(), and gpio_set_gpio_pin().

Referenced by rtouch_adc_int_handler().

static void rtouch_gradient_y_surface ( void  )
inlinestatic

Drive voltage gradient on Y surface (YL=GND, YH=VDD).

References gpio_clr_gpio_pin(), gpio_disable_pin_pull_up(), gpio_enable_gpio_pin(), and gpio_set_gpio_pin().

Referenced by rtouch_detect_int_handler(), and rtouch_resample().

static void rtouch_ground_x_surface ( void  )
inlinestatic

Drive X lines to ground.

References gpio_clr_gpio_pin(), and gpio_enable_gpio_pin().

Referenced by rtouch_adc_int_handler(), rtouch_disable(), and rtouch_enable().

static void rtouch_ground_y_surface ( void  )
inlinestatic

Drive Y lines to ground.

References gpio_clr_gpio_pin(), and gpio_enable_gpio_pin().

Referenced by rtouch_disable().

static bool rtouch_is_detect ( void  )
inlinestatic

Test if a touch is detected.

Checks if one of the Y lines is pulled low due to a touch on the surface. The Y lines must be pulled up and the X lines must be GND in order to work.

Returns
true if a touch is detected and false if no touch is detected.

References gpio_get_pin_value().

Referenced by rtouch_process_samples(), and rtouch_resample().

static void rtouch_prepare_adc ( void  )
inlinestatic

Prepare ADC for touch measuring.

Register the interrupt handler, set sample, hold and startup time.

References adc, adc_configure(), Disable_global_interrupt, Enable_global_interrupt, FOSC0, INTC_register_interrupt(), and rtouch_adc_int_handler().

Referenced by rtouch_init().

static void rtouch_prepare_detect ( void  )
inlinestatic

Prepare the rtouch detect function.

A touch can be detected by setting the Y lines (or X lines) as inputs and enabling the internal pull-ups for these pins. The other lines, X in this case are connected to GND. By a touch the Y lines will be pulled low and an interrupt can be triggered on a low level detect.

This function does the registering of the interrupt handler for the touch detect. Currently we use a GPIO interrupt, but here it is only possible to trigger on edges but actually we should trigger on a low level. Anyway this should work too. Here we use the YL line as source input for the edge detection but it is also possible to use YH.

References Disable_global_interrupt, Enable_global_interrupt, INTC_register_interrupt(), and rtouch_detect_int_handler().

Referenced by rtouch_init().

static void rtouch_process_samples ( void  )
static

Called from ADC ISR to compute coordinates from raw samples.

This function takes care of the final processing and calibration of the raw panel sample values, or lack of touch.

This function is called by the ADC interrupt handler.

This function will also automatically start a resample of the panel, either immediately or delayed via the timer module, depending on the TOUCH_USE_IMMEDIATE_RESAMPLE setting from "config_touch.h".

References NULL, rtouch, rtouch_clear_detect_flag(), rtouch_enable_detect_int(), rtouch_is_detect(), RTOUCH_MOVE, RTOUCH_NO_EVENT, RTOUCH_NOT_TOUCHED, RTOUCH_PRESS, RTOUCH_RELEASE, rtouch_resample(), and RTOUCH_TOUCHED.

Referenced by rtouch_adc_int_handler().

static void rtouch_pullup_y_surface ( void  )
inlinestatic

Enable pull-ups for Y lines.

Used to be able to trigger an interrupt upon a touch.

References gpio_enable_gpio_pin(), and gpio_enable_pin_pull_up().

Referenced by rtouch_adc_int_handler(), and rtouch_enable().

static void rtouch_resample ( void  )
static

Called from rtouch_process_samples or from timer module to start a new sample.

This function starts a new sampling of the touch panel.

It will be called by the driver repeatedly as long as the touch panel stays touched.

References Assert, NULL, rtouch, rtouch_clear_detect_flag(), rtouch_enable_detect_int(), rtouch_gradient_y_surface(), rtouch_is_detect(), RTOUCH_NOT_TOUCHED, RTOUCH_READING_XL, RTOUCH_RELEASE, rtouch_start_read(), RTOUCH_TOUCHED, and rtouch_tristate_x_surface().

Referenced by rtouch_process_samples().

static void rtouch_start_read ( uint32_t  channel)
static
static void rtouch_tristate_x_surface ( void  )
inlinestatic
static void rtouch_tristate_y_surface ( void  )
inlinestatic

rtouch_calibration_matrix_t* calibration_matrix

Current calibration matrix.

rtouch_event_handler_t event_handler

Current event handler, NULL if no handler defined.

rtouch_event_t last_event

Last touch event.

int32_t rawX

Temporary raw sample values for X position.

int32_t rawY

Temporary raw sample values for Y position.

const gpio_map_t rtouch_gpio_xmap
static
Initial value:
= {
{
.pin = RTOUCH_XL_PIN,
.function = RTOUCH_XL_PIN_FUNCTION
},
{
.pin = RTOUCH_XH_PIN,
.function = RTOUCH_XH_PIN_FUNCTION
}
}
const gpio_map_t rtouch_gpio_ymap
static
Initial value:
= {
{
.pin = RTOUCH_YL_PIN,
.function = RTOUCH_YL_PIN_FUNCTION
},
{
.pin = RTOUCH_YH_PIN,
.function = RTOUCH_YH_PIN_FUNCTION
}
}
enum rtouch_state_enum state

Current driver state.