atmel-samd: Swap out pulseio for touchio based on FreeTouch.

This commit is contained in:
Scott Shawcroft 2017-04-17 16:43:46 -07:00
parent 225fdb0307
commit 67ec52d7a9
9 changed files with 45 additions and 2299 deletions

3
.gitmodules vendored
View File

@ -11,3 +11,6 @@
[submodule "lib/berkeley-db-1.xx"]
path = lib/berkeley-db-1.xx
url = https://github.com/pfalcon/berkeley-db-1.xx
[submodule "atmel-samd/freetouch"]
path = atmel-samd/freetouch
url = git@github.com:adafruit/Adafruit_FreeTouch.git

View File

@ -73,6 +73,7 @@ INC += $(addprefix -Iasf/sam0/,\
)
INC += -Iasf/thirdparty/CMSIS/Include
INC += -Iboards/$(BOARD)/
INC += -Ifreetouch/
INC += -I$(BUILD)
CFLAGS_CORTEX_M0 = \
@ -144,8 +145,8 @@ CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
endif
#LIBM_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-file-name=libm.a)
LDFLAGS = $(CFLAGS) -nostartfiles -fshort-enums -mthumb -mcpu=cortex-m0plus -Lasf/thirdparty/CMSIS/Lib/GCC/ -LQTouch/ -Wl,-nostdlib -Wl,-T,$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nano.specs
LIBS = -larm_cortexM0l_math -lsamd21_qtouch_gcc -lm -lgcc -lc
LDFLAGS = $(CFLAGS) -nostartfiles -fshort-enums -mthumb -mcpu=cortex-m0plus -Lasf/thirdparty/CMSIS/Lib/GCC/ -Wl,-nostdlib -Wl,-T,$(LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nano.specs
LIBS = -larm_cortexM0l_math -lm -lgcc -lc
SRC_ASF = $(addprefix asf/sam0/,\
drivers/adc/adc_sam_d_r/adc.c \
@ -201,6 +202,7 @@ SRC_C = \
asf/sam0/utils/syscalls/gcc/syscalls.c \
boards/$(BOARD)/init.c \
boards/$(BOARD)/pins.c \
freetouch/adafruit_ptc.c \
lib/fatfs/ff.c \
lib/fatfs/option/ccsbcs.c \
lib/timeutils/timeutils.c \
@ -236,17 +238,11 @@ SRC_BINDINGS = \
busio/UART.c \
neopixel_write/__init__.c \
time/__init__.c \
touchio/__init__.c \
touchio/TouchIn.c \
usb_hid/__init__.c \
usb_hid/Device.c
# Handle touch support on its own since it only fits in the image when external
# flash is used to store the file system.
# TODO(tannewt): Remove this after we switch to freetouch and rely on the linker
# to drop classes we don't need.
ifneq ($(FLASH_IMPL),internal_flash.c)
SRC_BINDINGS += touchio/__init__.c touchio/TouchIn.c
endif
SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))

File diff suppressed because it is too large Load Diff

View File

@ -35,154 +35,20 @@
#include "tick.h"
#include "QTouch/touch_api_ptc.h"
#include "adafruit_ptc.h"
#define DEF_SELFCAP_CAL_SEQ1_COUNT 8
#define DEF_SELFCAP_CAL_SEQ2_COUNT 4
#define DEF_SELFCAP_CC_CAL_CLK_PRESCALE_PER_NODE DEF_SELFCAP_CLK_PRESCALE_PER_NODE
#define DEF_SELFCAP_CC_CAL_SENSE_RESISTOR_PER_NODE DEF_SELFCAP_SENSE_RESISTOR_PER_NODE
#define DEF_SELFCAP_QUICK_REBURST_ENABLE 1u
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
adafruit_ptc_start_conversion(PTC, &self->config);
/**
* Mutual Cap sensors measured data pointer.
* Note: This pointer is initialized by the QTouch library once the
* touch_mutlcap_sensors_init API is called. */
touch_measure_data_t *p_mutlcap_measure_data = NULL;
/**
* Self Cap sensors measured data pointer.
* Note: This pointer is initialized by the QTouch library once the
* touch_selfcap_sensors_init API is called. */
touch_measure_data_t *p_selfcap_measure_data = NULL;
/**
* Self Cap sensors data block provided as input to Touch library.
*/
static uint8_t selfcap_data_blk[PRIV_SELFCAP_DATA_BLK_SIZE];
/**
* Self Cap sensors Pins Info.
*/
uint32_t selfcap_y_nodes[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_LINES};
filter_level_t selfcap_filter_level_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_FILTER_LEVEL_PER_NODE};
uint8_t selfcap_auto_os_per_node[DEF_SELFCAP_NUM_CHANNELS]= {DEF_SELFCAP_AUTO_OS_PER_NODE};
gain_t selfcap_gain_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_GAIN_PER_NODE};
rsel_val_t selfcap_resistor_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_SENSE_RESISTOR_PER_NODE};
prsc_div_sel_t selfcap_prsc_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_CLK_PRESCALE_PER_NODE};
prsc_div_sel_t selfcap_boot_prsc_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_CC_CAL_CLK_PRESCALE_PER_NODE};
rsel_val_t selfcap_boot_resistor_per_node[DEF_SELFCAP_NUM_CHANNELS] = { DEF_SELFCAP_CC_CAL_SENSE_RESISTOR_PER_NODE};
freq_hop_sel_t selfcap_freq_hops[3u] = {DEF_SELFCAP_HOP_FREQS};
/**
* Self Cap Configuration structure provided as input to Touch Library.
*
* Note: Use the touch.h configuration header file to fill in
* the elements of this structure. DO NOT modify any of the input values
* directly in this structure.
*/
touch_selfcap_config_t selfcap_config = {
0, /* Self Cap number of channels. */
0, /* Self Cap number of sensors. */
DEF_SELFCAP_NUM_ROTORS_SLIDERS, /* Self Cap number of rotors and
* sliders. */
/* Self Cap GLOBAL SENSOR CONFIGURATION INFO. */
{
DEF_SELFCAP_DI, /* uint8_t di; Sensor detect
* integration (DI) limit. */
/* Interchanging Negative and Positive Drift rate, since Signal
* increases on Touch. */
DEF_SELFCAP_ATCH_DRIFT_RATE, /* uint8_t neg_drift_rate;
* Sensor
* negative drift rate. */
DEF_SELFCAP_TCH_DRIFT_RATE, /* uint8_t pos_drift_rate;
* Sensor
* positive drift rate. */
DEF_SELFCAP_MAX_ON_DURATION, /* uint8_t max_on_duration;
* Sensor
* maximum on duration. */
DEF_SELFCAP_DRIFT_HOLD_TIME, /* uint8_t drift_hold_time;
* Sensor
* drift hold time. */
DEF_SELFCAP_ATCH_RECAL_DELAY, /* uint8_t pos_recal_delay;
* Sensor positive recalibration
* delay. */
DEF_SELFCAP_CAL_SEQ1_COUNT,
DEF_SELFCAP_CAL_SEQ2_COUNT,
DEF_SELFCAP_ATCH_RECAL_THRESHOLD, /* recal_threshold_t
* recal_threshold; Sensor
* recalibration threshold. */
DEF_SELFCAP_TOUCH_POSTPROCESS_MODE,
DEF_SELFCAP_AUTO_OS_SIGNAL_STABILITY_LIMIT,
DEF_SELFCAP_FREQ_AUTO_TUNE_SIGNAL_STABILITY_LIMIT,
DEF_SELFCAP_FREQ_AUTO_TUNE_IN_CNT,
DEF_SELFCAP_NOISE_MEAS_SIGNAL_STABILITY_LIMIT, /* signal
*stability */
DEF_SELFCAP_NOISE_LIMIT,
DEF_SELFCAP_LOCKOUT_SEL,
DEF_SELFCAP_LOCKOUT_CNTDOWN,
#if ((SAMC20) || (SAMC21) || (SAML22))
DEF_SELF_CAP_CSD_VALUE,
while (!adafruit_ptc_is_conversion_finished(PTC)) {
// wait
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
},
{
selfcap_gain_per_node, /* Self Cap channel gain setting. */
DEF_SELFCAP_FREQ_MODE, /* Self Cap noise counter measure
* enable/disable. */
selfcap_prsc_per_node,
selfcap_resistor_per_node,
selfcap_boot_prsc_per_node,
selfcap_boot_resistor_per_node,
selfcap_freq_hops,
selfcap_filter_level_per_node, /* Self Cap filter level setting. */
selfcap_auto_os_per_node, /* Self Cap auto oversamples
*setting. */
},
selfcap_data_blk, /* Self Cap data block index. */
PRIV_SELFCAP_DATA_BLK_SIZE, /* Self Cap data block size. */
selfcap_y_nodes, /* Self Cap channel nodes. */
DEF_SELFCAP_QUICK_REBURST_ENABLE,
DEF_SELFCAP_FILTER_CALLBACK, /* Self Cap filter callback
* function
* pointer. */
DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE,
DEF_SELFCAP_NOISE_MEAS_ENABLE,
DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT,
DEF_SELFCAP_MOIS_TOLERANCE_ENABLE,
DEF_SELFCAP_NUM_MOIS_GROUPS,
DEF_SELFCAP_MOIS_QUICK_REBURST_ENABLE,
DEF_SELFCAP_PTC_GPIO_STATE,
.tlib_feature_list ={
.lk_chk = DEF_LOCKOUT_FUNC,
.auto_os_init = DEF_AUTO_OS_FUNC,//auto_os_resolve,
.auto_tune_init = DEF_SELF_AUTO_TUNE_FUNC,// self_auto_tuning
.enable_aks = DEF_AKS_FUNC, /*AKS grouping function */
}
};
}
/**
* Touch Library input configuration structure.
*/
touch_config_t touch_config = {
NULL,
&selfcap_config, /* Pointer to Self Cap configuration
* structure. */
DEF_TOUCH_PTC_ISR_LVL, /* PTC interrupt level. */
};
touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
return adafruit_ptc_get_conversion_result(PTC);
}
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
const mcu_pin_obj_t *pin) {
@ -191,32 +57,6 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
}
claim_pin(pin);
if (selfcap_config.num_channels > 0) {
// Deinit the touch sensor, we're going to reinitialize it.
touch_selfcap_sensors_deinit();
// Remove holes in the active list from sensors that may have been
// disabled.
int first_hole = -1;
for (int i = 0; i < selfcap_config.num_channels; i++) {
if (active_touchin_obj[i] == NULL && first_hole == -1) {
first_hole = i;
} else if (active_touchin_obj[i] != NULL && first_hole != -1) {
active_touchin_obj[first_hole] = active_touchin_obj[i];
first_hole = i;
}
}
if (first_hole > -1) {
selfcap_config.num_channels = first_hole;
selfcap_config.num_sensors = first_hole;
}
}
// Add our sensor to the end of the list.
self->pin = pin;
selfcap_y_nodes[selfcap_config.num_channels++] = Y(pin->touch_y_line);
active_touchin_obj[selfcap_config.num_sensors++] = self;
/* Setup and enable generic clock source for PTC module. */
struct system_gclk_chan_config gclk_chan_conf;
system_gclk_chan_get_config_defaults(&gclk_chan_conf);
@ -225,83 +65,28 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
system_gclk_chan_enable(PTC_GCLK_ID);
system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, PM_APBCMASK_PTC);
touch_ret_t status;
/* Initialize touch library for Self Cap operation. */
status = touch_selfcap_sensors_init_with_rs_table(&touch_config,
PRIV_SELFCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT,
PRIV_FREQ_AUTO_TUNE_CHK, PRIV_MOIS_TOLERANCE_CHK);
if (status != TOUCH_SUCCESS) {
mp_raise_OSError(MP_EIO);
}
for (int i = 0; i < selfcap_config.num_channels; i++) {
status = touch_selfcap_sensor_config(SENSOR_TYPE_KEY, i, i,
NO_AKS_GROUP, 10u, HYST_25, RES_8_BIT, &active_touchin_obj[i]->sensor_id);
if (status != TOUCH_SUCCESS) {
mp_raise_OSError(MP_EIO);
}
}
status = touch_selfcap_sensors_calibrate(AUTO_TUNE_RSEL);
if (status != TOUCH_SUCCESS) {
mp_raise_OSError(MP_EIO);
}
adafruit_ptc_get_config_default(&self->config);
self->config.pin = pin->pin;
self->config.yline = pin->touch_y_line;
mp_printf(&mp_plat_print, "pin %d line %d\n", pin->pin, pin->touch_y_line);
// Run a measurement to get calibrated.
common_hal_touchio_touchin_get_value(self);
adafruit_ptc_init(PTC, &self->config);
self->threshold = 2 * get_raw_reading(self);
//mp_printf(&mp_plat_print, "%d\n", self->threshold);
}
// This may get called twice, once on exit or deinit and once from the finaliser
// (__del__). We need the finaliser to reduce the chance of us writing to memory
// we no longer own because we keep track of Python pointers in active_touchin_obj.
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
touch_selfcap_sensor_disable(self->sensor_id);
// Remove ourselves from the list of active objects. We don't change the
// selfcap settings at all because we don't want to reinit now. Instead,
// we'll collect blank spots later on reinit.
for (int i = 0; i < DEF_SELFCAP_NUM_CHANNELS; i++) {
if (active_touchin_obj[i] == self) {
active_touchin_obj[i] = NULL;
break;
}
}
// TODO(tannewt): Reset the PTC.
reset_pin(self->pin->pin);
}
volatile bool touch_read_ready = false;
volatile touch_acq_status_t touch_acq_status;
void touch_selfcap_measure_complete_callback(void)
{
touch_read_ready = true;
touch_acq_status = p_selfcap_measure_data->acq_status;
void touchin_reset() {
// TODO(tannewt): Reset the PTC.
}
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) {
mp_raise_RuntimeError("Touch calibration error");
}
touch_acq_status = TOUCH_BURST_AGAIN;
uint64_t start_ticks = ticks_ms;
while((touch_acq_status & TOUCH_BURST_AGAIN) && ticks_ms - start_ticks < 1000) {
touch_read_ready = false;
touch_ret_t touch_ret = touch_selfcap_sensors_measure(
ticks_ms,
NORMAL_ACQ_MODE,
touch_selfcap_measure_complete_callback);
if (touch_ret != TOUCH_SUCCESS) {
mp_raise_OSError(MP_EIO);
}
while(!touch_read_ready && ticks_ms - start_ticks < 1000) {
// wait
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
}
if (touch_acq_status & TOUCH_BURST_AGAIN) {
mp_raise_OSError(MP_EIO);
}
return (p_selfcap_measure_data->p_sensor_states[self->sensor_id / 8] & (1 << (self->sensor_id % 8))) != 0;
uint16_t reading = get_raw_reading(self);
//mp_printf(&mp_plat_print, "%d\n", reading);
return reading > self->threshold;
}

View File

@ -29,14 +29,18 @@
#include "common-hal/microcontroller/Pin.h"
#include "QTouch/touch_api_ptc.h"
#include "samd21_ptc_component.h"
#include "adafruit_ptc.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t * pin;
sensor_id_t sensor_id;
struct adafruit_ptc_config config;
uint16_t threshold;
} touchio_touchin_obj_t;
void touchin_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H__

1
atmel-samd/freetouch Submodule

@ -0,0 +1 @@
Subproject commit 1f21fde3fed141950b9dcc1b608733bce2ed212e

View File

@ -32,7 +32,6 @@
#ifdef EXPRESS_BOARD
#include "common-hal/touchio/TouchIn.h"
#include "QTouch/touch_api_ptc.h"
#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x100)
#else
#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x010000 - 0x100)
@ -139,10 +138,6 @@ void reset_mp(void) {
mp_obj_list_init(mp_sys_argv, 0);
}
#ifdef EXPRESS_BOARD
extern touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
extern touch_selfcap_config_t selfcap_config;
#endif
extern volatile bool mp_msc_enabled;
void reset_samd21(void) {
@ -163,12 +158,7 @@ void reset_samd21(void) {
}
#ifdef EXPRESS_BOARD
touch_selfcap_sensors_deinit();
for (int i = 0; i < selfcap_config.num_channels; i++) {
active_touchin_obj[i] = NULL;
}
selfcap_config.num_channels = 0;
selfcap_config.num_sensors = 0;
touchin_reset();
#endif
analogin_reset();

View File

@ -136,14 +136,14 @@ extern const struct _mp_obj_module_t time_module;
extern const struct _mp_obj_module_t neopixel_write_module;
extern const struct _mp_obj_module_t uheap_module;
extern const struct _mp_obj_module_t samd_module;
extern const struct _mp_obj_module_t touchio_module;
extern const struct _mp_obj_module_t usb_hid_module;
// Internal flash size dependent settings.
#if BOARD_FLASH_SIZE > 192000
extern const struct _mp_obj_module_t touchio_module;
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define EXTRA_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }
#define EXPRESS_BOARD
#else
@ -152,10 +152,10 @@ extern const struct _mp_obj_module_t usb_hid_module;
#endif
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \