Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <wirish.h>
- #include <adc.h>
- #include <nvic.h>
- #define ADC_PIN 3
- void
- debug_str (const char *msg)
- {
- // wait for enter
- while (!SerialUSB.available ())
- ;
- while (SerialUSB.available ())
- SerialUSB.read ();
- // write debug message
- SerialUSB.println (msg);
- }
- void
- debug_int (int val)
- {
- // wait for enter
- while (!SerialUSB.available ())
- ;
- while (SerialUSB.available ())
- SerialUSB.read ();
- // write debug message
- SerialUSB.println (val);
- }
- void (*adc12_irq_handler) (void) = NULL;
- extern "C" void
- __irq_adc (void)
- {
- debug_str ("__irq_adc");
- if (adc12_irq_handler)
- adc12_irq_handler ();
- }
- void
- adc12_attach_interrupt (void (*handler) (void))
- {
- debug_str ("adc12_attach_interrupt");
- adc12_irq_handler = handler;
- nvic_irq_enable (NVIC_ADC_1_2);
- }
- void
- adc12_detach_interrupt ()
- {
- debug_str ("adc12_detach_interrupt");
- nvic_irq_disable (NVIC_ADC_1_2);
- adc12_irq_handler = NULL;
- }
- volatile uint8_t adc_done = 0;
- void
- adc_eoc_irq (void)
- {
- debug_str ("adc_eoc_irq");
- // read and write analog signal
- uint16_t val = ADC1->regs->DR & ADC_DR_DATA;
- debug_int (val);
- adc_done = 1;
- }
- void
- adc_run ()
- {
- debug_str ("adc_run");
- adc_done = 0;
- debug_str ("starting conversion");
- ADC1->regs->CR2 |= ADC_CR2_SWSTART; // XXX maple freezes here XXX
- debug_str ("conversion started");
- }
- void
- adc_block ()
- {
- debug_str ("adc_block");
- while (!adc_done)
- ;
- }
- void
- loop ()
- {
- adc_run ();
- adc_block ();
- }
- void
- setup ()
- {
- pinMode (ADC_PIN, INPUT_ANALOG);
- // set up ADC single conversion
- ADC1->regs->SQR3 = PIN_MAP[ADC_PIN].adc_channel;
- adc_set_reg_seqlen (ADC1, 1);
- // set up interrupt
- ADC1->regs->CR1 |= ADC_CR1_EOCIE; // enable end-of-conversion interrupt
- adc12_attach_interrupt (adc_eoc_irq); // attach end-of-conversion interrupt
- }
- __attribute__ ((constructor)) void
- premain ()
- {
- init ();
- }
- int
- main (void)
- {
- setup ();
- while (true)
- loop ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement