token-vesting icon indicating copy to clipboard operation
token-vesting copied to clipboard

PTC anomaly with channels above 8

Open ZimM-LostPolygon opened this issue 2 months ago • 5 comments

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:

https://github.com/SpenceKonde/megaTinyCore/blob/2caece51c053863152acd3aea0b9cfccb57e27a5/megaavr/libraries/PTC/src/ptc.c#L272

  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.

ZimM-LostPolygon avatar Oct 17 '25 14:10 ZimM-LostPolygon

@MX682X ? this is his work,

SpenceKonde avatar Nov 15 '25 15:11 SpenceKonde

@ZimM-LostPolygon Great inspection. I've been having a problem that X lines except X0 are malfunctioning on mutual capacitance sensor. Your fix made a big step forward in problem analysis.

zbn10 avatar Nov 16 '25 17:11 zbn10

I've taken a note, but I'm currently busy, Not sure when I will be able to tackle it. Maybe somewhen in December

MX682X avatar Nov 19 '25 13:11 MX682X

@ZimM-LostPolygon PTC_CB_EVENT_ERR_CALIB_LOW usually means that the capacitance of the pin is too low. Try connecting a (long) wire or electrode (copper pour)

MX682X avatar Nov 24 '25 15:11 MX682X

@MX682X Yes, that was my thought as well. However

  1. I get this error for every channel above 8, including pins where I do have some metal connected (including a long wire I specifically had for PTC on channel 13, which worked on channel 5)
  2. On channels <= 8, PTC works even by touching bare unconnected MCU pins

ZimM-LostPolygon avatar Nov 24 '25 15:11 ZimM-LostPolygon