Advertisement
Guest User

libmaple ADC Interrupt example

a guest
Jun 17th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. #include <wirish.h>
  4. #include <adc.h>
  5. #include <nvic.h>
  6.  
  7. #define ADC_PIN 3
  8.  
  9. void
  10. debug_str (const char *msg)
  11. {
  12.     // wait for enter
  13.     while (!SerialUSB.available ())
  14.         ;
  15.     while (SerialUSB.available ())
  16.         SerialUSB.read ();
  17.     // write debug message
  18.     SerialUSB.println (msg);
  19. }
  20.  
  21. void
  22. debug_int (int val)
  23. {
  24.     // wait for enter
  25.     while (!SerialUSB.available ())
  26.         ;
  27.     while (SerialUSB.available ())
  28.         SerialUSB.read ();
  29.     // write debug message
  30.     SerialUSB.println (val);
  31. }
  32.  
  33. void (*adc12_irq_handler) (void) = NULL;
  34.  
  35. extern "C" void
  36. __irq_adc (void)
  37. {
  38.     debug_str ("__irq_adc");
  39.  
  40.     if (adc12_irq_handler)
  41.         adc12_irq_handler ();
  42. }
  43.  
  44. void
  45. adc12_attach_interrupt (void (*handler) (void))
  46. {
  47.     debug_str ("adc12_attach_interrupt");
  48.  
  49.     adc12_irq_handler = handler;
  50.     nvic_irq_enable (NVIC_ADC_1_2);
  51. }
  52.  
  53. void
  54. adc12_detach_interrupt ()
  55. {
  56.     debug_str ("adc12_detach_interrupt");
  57.  
  58.     nvic_irq_disable (NVIC_ADC_1_2);
  59.     adc12_irq_handler = NULL;
  60. }
  61.  
  62. volatile uint8_t adc_done = 0;
  63.  
  64. void
  65. adc_eoc_irq (void)
  66. {
  67.     debug_str ("adc_eoc_irq");
  68.  
  69.     // read and write analog signal
  70.     uint16_t val = ADC1->regs->DR & ADC_DR_DATA;
  71.     debug_int (val);
  72.  
  73.     adc_done = 1;
  74. }
  75.  
  76. void
  77. adc_run ()
  78. {
  79.     debug_str ("adc_run");
  80.  
  81.     adc_done = 0;
  82.  
  83.     debug_str ("starting conversion");
  84.     ADC1->regs->CR2 |= ADC_CR2_SWSTART; // XXX maple freezes here XXX
  85.     debug_str ("conversion started");
  86. }
  87.  
  88. void
  89. adc_block ()
  90. {
  91.     debug_str ("adc_block");
  92.  
  93.     while (!adc_done)
  94.         ;
  95. }
  96.  
  97. void
  98. loop ()
  99. {
  100.     adc_run ();
  101.     adc_block ();
  102. }
  103.  
  104. void
  105. setup ()
  106. {
  107.     pinMode (ADC_PIN, INPUT_ANALOG);
  108.  
  109.     // set up ADC single conversion
  110.     ADC1->regs->SQR3 = PIN_MAP[ADC_PIN].adc_channel;
  111.     adc_set_reg_seqlen (ADC1, 1);
  112.  
  113.     // set up interrupt
  114.     ADC1->regs->CR1 |= ADC_CR1_EOCIE; // enable end-of-conversion interrupt
  115.     adc12_attach_interrupt (adc_eoc_irq); // attach end-of-conversion interrupt
  116. }
  117.  
  118. __attribute__ ((constructor)) void
  119. premain ()
  120. {
  121.   init ();
  122. }
  123.  
  124. int
  125. main (void)
  126. {
  127.   setup ();
  128.  
  129.   while (true)
  130.     loop ();
  131.  
  132.   return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement