This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can I merge the Csense_drv and saadc application together?

Hello,

I am trying to sample capacitance change as well as voltage change from sensors. I combined the simplified sample codes from Csense_drv and saadc sample. If I only enable either csense_drv or saadc, I can read value. But if I combine them together, I always get "fatal" error. I understand csense_drv also use saadc (I disable COMP) but don't know where the conflict/problem is.

Can anybody take a look at my [source] file?

Thanks a lot! -Lei

  • Please debug your application, to see which function call returns an error code, and what error code is returned.

  • Hi Jorgen, Thanks for your reply. I added a nrf_drv_saadc_uninit() after saadc application was done. I could get both voltage reading (from saadc) and cap reading (from csense), but then got another "fatal" error and restarted the program. I tried to debug the program. It seemed the error coming from app_timer_create (,, csense_timerout_handler). The program stuck in " while (loop);" in app_error.c. I could not see more detailed info about how this happened. I am wondering if there is a conflict between csense library and saadc liabrary. It seemed I had to uninitialize the saadc before doing csense sampling to get csense reading even with fatal error afterward.

    Can you please take a look at my code? Really appreciate it. I updated full Keil files in my original post.

  • added comments: a lit more details after debugging. The first time sampling is okay to give me saadc and csense value. Then the program tried to do second sampling, started the first saadc sampling. But I got a return a err_code vaule of "8", I searched old post. The error code of "8" meant "NRF_ERROR_INVALID_STATE". It seemed the saadc already initiated. But even I added another"nrf_drv_saadc_uninit();" it still showed me the same error code of "8". Thanks.

    Another question is how I can just initiate the saadc once and then reuse it for csense application. So I won't have to "uninitiate" the saadc after the sampling. Thanks.

  • You are initializing the saadc driver, then starting a repeating timer that does sampling, and then you uninitialize the saadc driver. When the timer times out, it will try to sample the saadc, but the saadc driver is not initialized, which is why you get the NRF_ERROR_INVALID_STATE (from call to nrf_drv_saadc_sample() in saadc_timeout_handler(). Csense is not initialized in this code at all, but if it was, initialization of saadc would return an error, as the csense driver have already initialized the saadc driver. You need to init the drivers before each sampling and uninit when sampling is done, to get this to work.

    You could init the saadc once and use this for both csense and other sampling, the problem is to keep track of what sampled value is for which use case. The csense driver is not written for supporting this, meaning you would have to modify the driver to do this.

  • Hi Jorgen, thanks a lot for your explanation. It's working now without error.

Related