Excellent thread !
2006 M35 base with about 125K miles.
Had CEL exact same code pus VDC off light and SLIP light on.
Was hard accelerating and it bogged down. At restart took longer to crank.
Luckily only had less than 10 city miles to get it back home.
Really screams again now !
Code P0340 Circuit "A" (Right, Passenger side bank)
In place camshaft position sensor appeared original as its o-ring was flattened.
Numbers on in place sensor: SGVB000 5414B5 A29-640 C2O
Here's the replacement sensor I installed:
https://www.amazon.com/gp/product/B003U ... UTF8&psc=1
Here's and excellent tutorial showing sensor location and correct plug removal:
https://g35driver.com/forums/engine-dri ... rsion.html
Here's the Bluetooth Diagnostic OBDII Reader / Scanner for Android Devices I use:
https://www.amazon.com/gp/product/B005N ... UTF8&psc=1
Here's the best app on earth to use in conjunction with the reader/scanner:
https://www.amazon.com/gp/product/B00IJ ... UTF8&psc=1