58 lines
1.2 KiB
C
58 lines
1.2 KiB
C
|
/*
|
||
|
*
|
||
|
* Copyright (c) 2019
|
||
|
* Author: Daniele Lacamera <root@danielinux.net>
|
||
|
* GPL2.0
|
||
|
*/
|
||
|
#include <stdint.h>
|
||
|
#include "adc.h"
|
||
|
#include "system.h"
|
||
|
|
||
|
|
||
|
int adc_init(void)
|
||
|
{
|
||
|
int i;
|
||
|
uint32_t val;
|
||
|
/* Enable clock */
|
||
|
APB2_CLOCK_ER |= ADC1_APB2_CLOCK_ER_VAL;
|
||
|
|
||
|
/* Power off */
|
||
|
ADC1_CR2 &= ~(ADC_CR2_EN);
|
||
|
|
||
|
/* Set common clock prescaler */
|
||
|
ADC_COM_CCR &= ~(0x03 << 16);
|
||
|
|
||
|
/* Disable scan mode */
|
||
|
ADC1_CR1 &= ~(ADC_CR1_SCAN);
|
||
|
|
||
|
/* Set one-shot (disable continuous mode) */
|
||
|
ADC1_CR2 &= ~(ADC_CR2_CONT);
|
||
|
|
||
|
/* Set sample time for all channels */
|
||
|
val = ADC1_SMPR1;
|
||
|
|
||
|
for (i = 0; i < 10; i++) {
|
||
|
val |= ADC_SMPR_SMP_480CYC << (i * 3);
|
||
|
ADC1_SMPR1 = val;
|
||
|
val = ADC1_SMPR2;
|
||
|
for (i = 10; i < 18; i++)
|
||
|
val |= ADC_SMPR_SMP_480CYC << ((i-10) * 3);
|
||
|
}
|
||
|
ADC1_SMPR2 = val;
|
||
|
ADC1_CR2 &= ~(ADC_CR2_EN);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void adc_pin_val(uint32_t ch, uint16_t *val)
|
||
|
{
|
||
|
ADC1_SQR3 = ch;
|
||
|
ADC1_CR2 |= ADC_CR2_EN;
|
||
|
ADC1_CR2 |= ADC_CR2_SWSTART;
|
||
|
while (ADC1_CR2 & ADC_CR2_SWSTART);;
|
||
|
while ((ADC1_SR & ADC_SR_EOC) == 0);;
|
||
|
*val= ADC1_DR;
|
||
|
printf("Channel %d val %hu\r\n", ch, *val);
|
||
|
ADC1_SQR3 = 0;
|
||
|
}
|
||
|
|