adc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2004 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>,
  3. * Kernelconcepts http://www.kernelconcepts.de
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*!
  35. * \file arch/avr/dev/adc.c
  36. * \brief AVR adc driver
  37. *
  38. * \verbatim
  39. * $Id: adc.c 4937 2013-01-22 11:38:42Z haraldkipp $
  40. * \endverbatim
  41. */
  42. /*!
  43. * \addtogroup xgAvrAdc
  44. */
  45. /*@{*/
  46. /* Not ported. */
  47. #ifdef __GNUC__
  48. #include <string.h>
  49. #include <avr/sleep.h>
  50. #include <sys/heap.h>
  51. #include <sys/atom.h>
  52. #include <sys/nutconfig.h>
  53. #include <dev/irqreg.h>
  54. #include <dev/adc.h>
  55. #ifndef ADC_INITIAL_CHANNEL
  56. #define ADC_INITIAL_CHANNEL ADC0
  57. #endif
  58. #ifndef ADC_INITIAL_REF
  59. #define ADC_INITIAL_REF AVCC
  60. #endif
  61. #ifndef ADC_INITIAL_MODE
  62. #define ADC_INITIAL_MODE SINGLE_CONVERSION
  63. #endif
  64. #ifndef ADC_INITIAL_PRESCALE
  65. #define ADC_INITIAL_PRESCALE ADC_PRESCALE_DIV64
  66. #endif
  67. #define ADC_BUF_SIZE 16 // this may only be a power of two
  68. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  69. uint8_t adc_sleep_mode = SLEEP_MODE_ADC;
  70. /* AT90CAN128 uses a different register to enter sleep mode */
  71. #if defined(SMCR)
  72. #define AVR_SLEEP_CTRL_REG SMCR
  73. #else
  74. #define AVR_SLEEP_CTRL_REG MCUCR
  75. #endif
  76. #endif
  77. /********** DRIVER GLOBALS **********/
  78. adc_mode_t current_mode = ADC_OFF;
  79. // The buffers are FIFO buffers implemented as char
  80. // arrays with head and tail pointers.
  81. // All read and write functions return a 0 for
  82. // success and a 1 for fail, if a successfull read,
  83. // the character is placed into *read.
  84. // buffer: [data1 data2 ... datalast head tail]
  85. #define _adc_buf_head ADC_BUF_SIZE
  86. #define _adc_buf_tail (ADC_BUF_SIZE+1)
  87. uint16_t *ADC_buffer = NULL;
  88. inline int ADCBufRead(uint16_t * buf, uint16_t * read)
  89. {
  90. uint8_t tail, head;
  91. tail = buf[_adc_buf_tail];
  92. head = buf[_adc_buf_head];
  93. if (head != tail) {
  94. *read = buf[tail];
  95. buf[_adc_buf_tail] = (tail + 1) & (ADC_BUF_SIZE-1);
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. inline int ADCBufWrite(uint16_t * buf, uint16_t * write)
  101. {
  102. uint8_t tail, head;
  103. tail = buf[_adc_buf_tail];
  104. head = buf[_adc_buf_head];
  105. if ((head + 1) % ADC_BUF_SIZE != tail) {
  106. buf[head] = *write;
  107. buf[_adc_buf_head] = (head + 1) & (ADC_BUF_SIZE-1);
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. void ADCBufInit(uint16_t * buf)
  113. {
  114. buf[_adc_buf_head] = 0;
  115. buf[_adc_buf_tail] = 0;
  116. }
  117. static void ADCInterrupt(void *arg)
  118. {
  119. uint16_t ADC_value;
  120. #ifdef ADC
  121. ADC_value = inw(ADC);
  122. #else
  123. ADC_value = inw(ADCW);
  124. #endif
  125. if (ADCBufWrite(ADC_buffer, &ADC_value) != 0) {
  126. // Send error message
  127. }
  128. }
  129. void ADCInit()
  130. {
  131. if (ADC_buffer) return;
  132. ADCSetChannel(ADC_INITIAL_CHANNEL);
  133. ADCSetRef(ADC_INITIAL_REF);
  134. ADCSetMode(ADC_INITIAL_MODE);
  135. ADCSetPrescale(ADC_INITIAL_PRESCALE);
  136. ADC_buffer = NutHeapAlloc(sizeof(uint16_t) * (ADC_BUF_SIZE + 2));
  137. ADCBufInit(ADC_buffer);
  138. if (NutRegisterIrqHandler(&sig_ADC, ADCInterrupt, NULL)) {
  139. // MR 2006-01-11: we do not free buffer as this would cost ROM and is not likely
  140. return;
  141. }
  142. // Enable ADC
  143. sbi(ADCSR, ADEN);
  144. // Enable ADC interrupt
  145. sbi(ADCSR, ADIE);
  146. }
  147. void ADCSetRef(adc_ref_t reference)
  148. {
  149. ADCStopConversion();
  150. #ifdef __AVR_ENHANCED__
  151. switch (reference) {
  152. case AVCC:
  153. cbi(ADMUX, REFS1);
  154. sbi(ADMUX, REFS0);
  155. break;
  156. case AREF:
  157. cbi(ADMUX, REFS1);
  158. cbi(ADMUX, REFS0);
  159. break;
  160. case INTERNAL_256:
  161. sbi(ADMUX, REFS1);
  162. sbi(ADMUX, REFS0);
  163. break;
  164. }
  165. #endif /* __AVR_ENHANCED__ */
  166. }
  167. void ADCSetMode(adc_mode_t mode)
  168. {
  169. ADCStopConversion();
  170. switch (mode) {
  171. case FREE_RUNNING:
  172. sbi(ADCSR, ADFR);
  173. break;
  174. case SINGLE_CONVERSION:
  175. cbi(ADCSR, ADFR);
  176. break;
  177. case ADC_OFF:
  178. break;
  179. }
  180. current_mode = mode;
  181. }
  182. uint8_t ADCSetPrescale(uint8_t prescalar)
  183. {
  184. ADCStopConversion();
  185. if (prescalar > 128) {
  186. prescalar = 128;
  187. }
  188. switch (prescalar) {
  189. case ADC_PRESCALE_DIV2:
  190. cbi(ADCSR, ADPS2);
  191. cbi(ADCSR, ADPS1);
  192. cbi(ADCSR, ADPS0);
  193. break;
  194. case ADC_PRESCALE_DIV4:
  195. cbi(ADCSR, ADPS2);
  196. sbi(ADCSR, ADPS1);
  197. cbi(ADCSR, ADPS0);
  198. break;
  199. case ADC_PRESCALE_DIV8:
  200. cbi(ADCSR, ADPS2);
  201. sbi(ADCSR, ADPS1);
  202. sbi(ADCSR, ADPS0);
  203. break;
  204. case ADC_PRESCALE_DIV16:
  205. sbi(ADCSR, ADPS2);
  206. cbi(ADCSR, ADPS1);
  207. cbi(ADCSR, ADPS0);
  208. break;
  209. case ADC_PRESCALE_DIV32:
  210. sbi(ADCSR, ADPS2);
  211. cbi(ADCSR, ADPS1);
  212. sbi(ADCSR, ADPS0);
  213. break;
  214. case ADC_PRESCALE_DIV64:
  215. sbi(ADCSR, ADPS2);
  216. sbi(ADCSR, ADPS1);
  217. cbi(ADCSR, ADPS0);
  218. break;
  219. case ADC_PRESCALE_DIV128:
  220. sbi(ADCSR, ADPS2);
  221. sbi(ADCSR, ADPS1);
  222. sbi(ADCSR, ADPS0);
  223. break;
  224. default:
  225. return 1;
  226. break;
  227. }
  228. return 0;
  229. }
  230. void ADCSetChannel(adc_channel_t adc_channel)
  231. {
  232. uint8_t current_admux;
  233. current_admux = inb(ADMUX) & 0xF8;
  234. outb(ADMUX, (current_admux | adc_channel));
  235. }
  236. void ADCBufferFlush(void)
  237. {
  238. ADCBufInit(ADC_buffer);
  239. }
  240. void ADCStartConversion()
  241. {
  242. sbi(ADCSR, ADSC);
  243. }
  244. void ADCStartLowNoiseConversion()
  245. {
  246. ADCSetMode(SINGLE_CONVERSION);
  247. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  248. {
  249. uint8_t sleep_mode = AVR_SLEEP_CTRL_REG & _SLEEP_MODE_MASK;
  250. set_sleep_mode(adc_sleep_mode);
  251. /* Note: avr-libc has a sleep_mode() function, but it's broken for
  252. AT90CAN128 with avr-libc version earlier than 1.2 */
  253. AVR_SLEEP_CTRL_REG |= _BV(SE);
  254. __asm__ __volatile__ ("sleep" "\n\t" :: );
  255. AVR_SLEEP_CTRL_REG &= ~_BV(SE);
  256. set_sleep_mode(sleep_mode);
  257. }
  258. #else
  259. sbi(ADCSR, ADSC);
  260. #endif
  261. }
  262. void ADCStopConversion()
  263. {
  264. if (current_mode != FREE_RUNNING) {
  265. // Send warning message
  266. return;
  267. }
  268. // Terminate and restart the ADC
  269. // When restarted, start_conversion needs to be
  270. // called again
  271. cbi(ADCSR, ADEN);
  272. cbi(ADCSR, ADSC);
  273. sbi(ADCSR, ADEN);
  274. }
  275. uint8_t ADCRead(uint16_t * value)
  276. {
  277. return ADCBufRead(ADC_buffer, value);
  278. }
  279. inline adc_mode_t ADCGetMode(void)
  280. {
  281. return (current_mode);
  282. }
  283. #endif
  284. /*@}*/