/ * @brief * Initialize the specified ADC unit * * @details * * @note * * @param[in] device * Pointer to device descriptor * * @param[in] unitNumber * Unit number * * @return * Pointer to ADC device ******************************************************************************/
| 730 | * Pointer to ADC device |
| 731 | ******************************************************************************/ |
| 732 | static struct efm32_adc_device_t *rt_hw_adc_unit_init( |
| 733 | rt_device_t device, |
| 734 | rt_uint8_t unitNumber) |
| 735 | { |
| 736 | struct efm32_adc_device_t *adc; |
| 737 | CMU_Clock_TypeDef adcClock; |
| 738 | ADC_Init_TypeDef init = ADC_INIT_DEFAULT; |
| 739 | |
| 740 | do |
| 741 | { |
| 742 | /* Allocate device and set default value */ |
| 743 | adc = rt_malloc(sizeof(struct efm32_adc_device_t)); |
| 744 | if (adc == RT_NULL) |
| 745 | { |
| 746 | adc_debug("no memory for ADC%d driver\n", unitNumber); |
| 747 | break; |
| 748 | } |
| 749 | adc->mode = 0; |
| 750 | adc->singleCount = 0; |
| 751 | adc->singleDmaChannel = (rt_uint8_t)EFM32_NO_DMA; |
| 752 | adc->scanCount = 0; |
| 753 | adc->scanDmaChannel = (rt_uint8_t)EFM32_NO_DMA; |
| 754 | |
| 755 | /* Initialization */ |
| 756 | if (unitNumber >= ADC_COUNT) |
| 757 | { |
| 758 | break; |
| 759 | } |
| 760 | switch (unitNumber) |
| 761 | { |
| 762 | case 0: |
| 763 | adc->adc_device = ADC0; |
| 764 | adcClock = (CMU_Clock_TypeDef)cmuClock_ADC0; |
| 765 | break; |
| 766 | |
| 767 | default: |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | /* Enable ADC clock */ |
| 772 | CMU_ClockEnable(adcClock, true); |
| 773 | |
| 774 | /* Reset */ |
| 775 | ADC_Reset(adc->adc_device); |
| 776 | |
| 777 | /* Configure ADC */ |
| 778 | // TODO: Fixed oversampling rate? |
| 779 | init.ovsRateSel = adcOvsRateSel4096; |
| 780 | init.timebase = ADC_TimebaseCalc(0); |
| 781 | init.prescale = ADC_PrescaleCalc(ADC_CONVERT_FREQUENCY, 0); |
| 782 | ADC_Init(adc->adc_device, &init); |
| 783 | |
| 784 | return adc; |
| 785 | } while(0); |
| 786 | |
| 787 | if (adc) |
| 788 | { |
| 789 | rt_free(adc); |
no test coverage detected