PTC anomaly with channels above 8
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.
@MX682X ? this is his work,
@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.
I've taken a note, but I'm currently busy, Not sure when I will be able to tackle it. Maybe somewhen in December
@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 Yes, that was my thought as well. However
- 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)
- On channels <= 8, PTC works even by touching bare unconnected MCU pins