-
Notifications
You must be signed in to change notification settings - Fork 158
Description
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:
megaTinyCore/megaavr/libraries/PTC/src/ptc.c
Line 272 in 2caece5
| 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.