Unable to init PA1 with ADC1 with no HAL or LL STM32F103C6T8 bluepill
I am writing a bare-metal ADC driver from scratch and the issue that I am getting is on reading the value from PA1.
Here is my adc.c lib that I wrote.
#include "adc.h"
void ADC1_ADC_Init(void)
{
RCC->APB2ENR |= (1U << 2U); // IOPAEN I/O port A clk en // rm 146
GPIOA->CRL &= ~(1U << 5U); // MODE1 input mode // rm 171
GPIOA->CRL &= ~(1U << 4U); // MODE1 input mode // rm 171
GPIOA->CRL &= ~(1U << 7U); // CNF1 analog mode // rm 171
GPIOA->CRL &= ~(1U << 6U); // CNF1 analog mode // rm 171
RCC->APB2ENR |= (1U << 9U); // ADC1EN ADC1 interface clk en // rm 146
ADC1->SQR3 &= ~(1U << 4U); // SQ1 ADC_CH1 first conversion in reg seq // rm 249
ADC1->SQR3 &= ~(1U << 3U); // SQ1 ADC_CH1 first conversion in reg seq // rm 249
ADC1->SQR3 &= ~(1U << 2U); // SQ1 ADC_CH1 first conversion in reg seq // rm 249
ADC1->SQR3 &= ~(1U << 1U); // SQ1 ADC_CH1 first conversion in reg seq // rm 249
ADC1->SQR3 |= (1U << 0U); // SQ1 ADC_CH1 first conversion in reg seq // rm 249
ADC1->SQR1 &= ~(1U << 23U); // SQ1 reg ch seq len, 1 conversion // rm 247
ADC1->SQR1 &= ~(1U << 22U); // SQ1 reg ch seq len, 1 conversion // rm 247
ADC1->SQR1 &= ~(1U << 21U); // SQ1 reg ch seq len, 1 conversion // rm 247
ADC1->SQR1 &= ~(1U << 20U); // SQ1 reg ch seq len, 1 conversion // rm 247
ADC1->CR2 |= (1U << 0U); // ADON A/D converter on/off en ADC start conv // rm 243
}
void ADC1_ADC_StartConversion(void)
{
ADC1->CR2 |= (1U << 22U); // SWSTART start conversion of reg channels // rm 240
}
uint32_t ADC1_ADC_Read(void)
{
while(!(ADC1->SR & (1U << 1U))){}; // EOC end of conversion, conversion complete // rm 237
return ADC1->DR;
}Here is my main file.
#include "main.h"
#include "uart.h"
#include "adc.h"
uint32_t sensor_value;
int main(void)
{
// RCC->APB2ENR |= (1U << 4U); // IOPAEN I/O port C clk en // rm 146
// GPIOC->CRH |= (1U << 25U); // MODE14 out mode, max speed 2 MHz // rm 172
// GPIOC->CRH &= ~(1U << 24U); // MODE14 out mode, max speed 2 MHz // rm 172
// GPIOC->CRH &= ~(1U << 27U); // CNF14 gp out push-pull // rm 172
// GPIOC->CRH &= ~(1U << 26U); // CNF14 gp out push-pull // rm 172 // rm 172
USART1_UART_RxTxInit();
ADC1_ADC_Init();
ADC1_ADC_StartConversion();
while(1)
{
sensor_value = ADC1_ADC_Read();
printf("sensor value: %d\r\n", (int)sensor_value);
}
}One thing to note
void ADC1_ADC_StartConversion(void)
{
ADC1->CR2 |= (1U << 22U); // SWSTART start conversion of reg channels // rm 240
}When I watch the ADC1 CR2 register and try to set bit 22 it does NOT set for whatever reason.
The result is nothing gets printed to the UART1 to the terminal. I spent a few days on this and am reaching out here for assistance.
Thank you in advance.
