They're unrelated... start with checking grounds for the codes 41 and 24 if you have already tested the AFM and the TPS. As for the knock sensor, replace it... you can use the knock sensor off of a lot of same year Toyotas so hit a salvage yard and collect a handful of them for $10.