Subversion Repositories chibiosIgnition

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2.  
  3.  
  4.  
  5. #include "ch.h"  // needs for all ChibiOS programs
  6. #include "hal.h" // hardware abstraction layer header
  7.  
  8. #include "useAdc.h"
  9.  
  10.  
  11. // Lets configure our ADC first
  12.  
  13.  
  14. // ADCConfig structure for stm32 MCUs is empty
  15. static ADCConfig adccfg = {};
  16.  
  17. // Create buffer to store ADC results. This is
  18. // one-dimensional interleaved array
  19. #define ADC_BUF_DEPTH 2 // depth of buffer
  20. #define ADC_CH_NUM 2    // number of used ADC channels
  21. static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
  22.  
  23. // Fill ADCConversionGroup structure fields
  24. static ADCConversionGroup adccg = {
  25.    // this 3 fields are common for all MCUs
  26.       // set to TRUE if need circular buffer, set FALSE otherwise
  27.       TRUE,
  28.       // number of channels
  29.       (uint16_t)(ADC_CH_NUM),
  30.       // callback function, set to NULL for begin
  31.       NULL,
  32.           // error callback function
  33.           NULL,
  34.    // Resent fields are stm32 specific. They contain ADC control registers data.
  35.    // Please, refer to ST manual RM0008.pdf to understand what we do.
  36.       // CR1 register content, set to zero for begin
  37.       0,
  38.       // CR2 register content, set to zero for begin
  39.       0,
  40.       // SMRP1 register content, set to zero for begin
  41.       0,
  42.       // SMRP2 register content, set to zero for begin
  43.       0,
  44.       // SQR1 register content. Set channel sequence length
  45.       ((ADC_CH_NUM - 1) << 20),
  46.       // SQR2 register content, set to zero for begin
  47.       0,
  48.       // SQR3 register content. We must select 2 channels
  49.       // For example 2nd and 3rd channels. Refer to the
  50.       // pinout of your MCU to select other pins you need.
  51.       // On STM32-P103 board they connected to PA2 and PA3 contacts
  52.       (2 | (3 << 5)),
  53. };
  54.  
  55. // Thats all with configuration
  56.  
  57. void useAdc(void)
  58. {
  59.  
  60.   // Following 3 functions use previously created configuration
  61.   // to initialize ADC block, start ADC block and start conversion.
  62.   // &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL.
  63.   // Other arguments defined ourself earlier.
  64.   adcStart(&ADCD1, &adccfg);
  65.   adcStartConversion(&ADCD1, &adccg, &samples_buf[0], ADC_BUF_DEPTH);
  66.  
  67.    // samples in to buffer
  68. }
  69.  
  70. adcsample_t getAdc(uint8_t chan)
  71. {
  72.         return(samples_buf[chan & 1]);
  73. }
  74.  
  75.  
  76.  
  77.