Skip to content

PTC anomaly with channels above 8 #1248

@ZimM-LostPolygon

Description

@ZimM-LostPolygon

I have been using an Attiny1616 (VQFN 20-pin variant) with the PTC library from mTC. I only needed a single selfcap touch button, and it worked great immediately! That is, until I had to change the pin. I was initially using the PB0 pin, which corresponds to PTC channel 5. However, after switching to pin PB4 (PTC channel 13, according to the datasheet), ptc_add_selfcap_node suddenly started PTC_LIB_BAD_ARGUMENT. After looking into the code, I think I've found the problem:

ptc_ch_bm_t xCh = (ptc_ch_bm_t)pCh[0];

  ptc_ch_bm_t xCh = (ptc_ch_bm_t)pCh[0];
  ptc_ch_bm_t yCh = (ptc_ch_bm_t)pCh[typesize];

ptc_ch_bm_t is defined as uint16_t on parts with more than 8 PTC channels, however pCh is uint8_t*, so what happens, is that only the lower byte of the channel mask gets casted to ptc_ch_bm_t, with the result being 0 for all channels above 8. Changing those two lines to

  ptc_ch_bm_t xCh = *(ptc_ch_bm_t*)&pCh[0];
  ptc_ch_bm_t yCh = *(ptc_ch_bm_t*)&pCh[typesize];

... makes the library return PTC_LIB_SUCCESS, and seemingly that should be it, but... the node always immediately fails to calibrate with PTC_CB_EVENT_ERR_CALIB_LOW no matter what I try. There is nothing connected to that pin. So I suspect there might be a deeper issue which I don't understand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions