There is a crossroads between mental health and criminal defense. It exists on many levels, but most notably, with the issue of competence.
You must be competent to proceed to trial. To be legally competent, you must understand the nature of the charges against you and you must be able to assist in your own defense.
If there is reason to suspect that you are incompetent for whatever reason (mental illness or mental retardation), the Court may appoint experts in the field of mental health who will examine you with respect to the issue of competence to proceed.
The experts will test to see if you appreciate the charges or allegations against you; appreciate the range and nature of possible penalties; understand the adversary nature of the legal process; disclose to your attorney facts pertinent to the proceedings at issue; manifest appropriate courtroom behavior; and can testify relevantly.
Typically three doctors will evaluate you for competence. If two of the three deem that you are incompetent, they will prepare reports telling the Court what factors, they feel, should be considered in determining how to restore your competence.
If deemed incompetent by the Court, you could be involuntarily hospitalized or required to attend some other form of competency restoration so that you can become competent. If you cannot be restored, then you are adjudged incompetent and cannot be prosecuted.