I have easy understanding Question. Whet I do initialize my STM32 timer for 1 sek counts (TIM8, Prescaller = 16800-1, Period = 10000-1) and want to debug it (measure a pin output frequency) - it a timer frequency the same frequency like I can observe it on oscilloscope?
And it is a right configuration for TIM8 timer Interrupt?
void Second_timer_Init() {
GPIO_InitTypeDef GPIO_InitStructure; //GPIO Init structure definition
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //Enables AHB1 peripheral clock for GPIOC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //Enables AHB1 peripheral clock for TIM2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //Specifies the GPIO pins to be configured
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Specifies the operating output type for the selected pins
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO Alternate function Mode
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //Specifies the operating Pull-up/Pull down for the selected pins
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Specifies the speed for the selected pins
GPIO_Init(GPIOC, &GPIO_InitStructure); //Initializes the GPIOA peripheral
TIM_TimeBaseInitTypeDef Timer_InitStructure; //TIM8 Time Base Init structure definition
TIM_OCInitTypeDef Output_ChannelInit; //TIM8 Output Compare Init structure definition
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); // PC6 -> Connect TIM8 pins to AF3
Timer_InitStructure.TIM_Period = 10000 - 1; //Specifies the period value (Orig 1 Sek: 10000-1)
Timer_InitStructure.TIM_Prescaler = 16800-1; //Specifies the prescaler value used to divide the TIM clock (Orig 1 Sek: 16800-1)
Timer_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Specifies the clock division (0)
Timer_InitStructure.TIM_CounterMode = TIM_CounterMode_Up; //Specifies the counter mode
TIM_TimeBaseInit(TIM8, &Timer_InitStructure); //Initializes the TIM8 Time Base Unit peripheral
// TIM_OCStructInit(&Output_ChannelInit);
Output_ChannelInit.TIM_OCMode = TIM_OCMode_PWM1; //Specifies the TIM8 PWM mode
Output_ChannelInit.TIM_OutputState = TIM_OutputState_Enable;//Specifies the TIM8 Output Compare state
Output_ChannelInit.TIM_Pulse = 0; //Specifies the pulse value to be loaded into the Capture Compare Register
Output_ChannelInit.TIM_OCPolarity = TIM_OCPolarity_Low; //Specifies the output polarity
TIM_OC1Init(TIM8, &Output_ChannelInit); //Initializes the TIM8 Channel1
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //Enables the TIM2 peripheral Preload register on CCR1
TIM_ARRPreloadConfig(TIM8, ENABLE); //Enables TIM2 peripheral Preload register on ARR
TIM_Cmd(TIM8, ENABLE); //Enables the specified TIM8 peripheral
TIM_CtrlPWMOutputs(TIM8, ENABLE); //Enables the TIM peripheral Main Outputs ?
TIM8->CCR1 = 5000; //Set duty cycle to 50%
TIM_ClearITPendingBit(TIM8, TIM_IT_Update); //Clears the TIM8 interrupt pending bits
TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE); //Enables the specified TIM8 interrupts
NVIC_InitTypeDef NVIC_InitStructure; //NVIC Init Structure definition
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn; //Specifies the IRQ channel to be enabled
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel (0-15)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel (0-15)
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled
NVIC_Init(&NVIC_InitStructure); //Initializes the NVIC peripheral
}
IRQ Handler
void TIM8_CC_IRQHandler(){
if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET) //Checks whether the TIM8 interrupt has occurred
{
TIM_ClearITPendingBit(TIM8, TIM_IT_Update); //Clears the TIM8 interrupt pending bits
TIM3->CCR1 = 500; //Debug
}
else{
TIM3->CCR1 = 0; //Debug
}
}
As I see you change the TIM3 registers in the TIM8 interrupt.
Secondly you check for the wrong event in your interrupt routine (this routine is invoked then the CC event occurs not the UG event. Same when you set the interrupts. You enable the UG interrupt but serve the CC one. Some of the uC models have those interrupt vectors shared some not.
BTW What is the point of using HAL library if you change the registers?
Related
I have little bit problem when reading Rx Buffer in STM32 SPI. I can watch my signal when transmit or receive in my scope. But I never can get any data in my Rx Buffer. I just use coocox software for this project.
For this project, I use STM32F103 and LoRa module (SX1278). I use Full duplex communication for my SPI Configuration. There is 2 cycle to read register status in my LoRa module. First cycle is to write address and second cycle is for read/write register. My problem is reading register in my LoRa module.
This is my simple code to read register.
void SPI2_IRQHandler(void)
{
RxSPIBuff = &Buffer_Rx[0];
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
{
/* Store the I2S2 received data in the relative data table */
//Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI2);
//if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_FLAG_RXNE)==SET)
USART_SendData(USART1, SPI_I2S_ReceiveData(SPI2));
}
}
void InitSPI_Lora(void)
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// RCC Peripheral Configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// GPIO Configuration
GPIO_InitStructure.GPIO_Pin = MOSI_LoRa | SCLK_LoRa;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_LoRa, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NSS_LoRa;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PeriphNSS_LoRa, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MISO_LoRa;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_LoRa, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Reset_LoRa;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PeriphRst_LoRa, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DIO0_LoRa;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PeriphDI0_LoRa, &GPIO_InitStructure);
// SPI Configuration
SPI_InitStruct.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_32; // 7us every 8 bit data
SPI_InitStruct.SPI_CPHA=SPI_CPHA_1Edge;
SPI_InitStruct.SPI_CPOL=SPI_CPOL_Low;
SPI_InitStruct.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode=SPI_Mode_Master;
SPI_InitStruct.SPI_NSS=SPI_NSS_Soft;
// NVIC Configuration
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* SPI1 IRQ Channel configuration */
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
SPI_I2S_DeInit(SPI2);
/* Enable the I2S1 RxNE interrupt */
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE);
}
void SendSPI_Lora(unsigned short val, unsigned char status)
{
SPI_I2S_SendData(SPI2, val);
while(SPI_I2S_GetITStatus(SPI2, SPI_I2S_FLAG_TXE)==SET);
SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_TXE);
}
void AccessSPI(unsigned char Cmd, unsigned short *ptrBuff, unsigned char Operation)
{
unsigned short m, temp;
NSS_LO_LoRa;
SendSPI_Lora(Cmd, kWriteSPI); // Send Command
if (Operation==kWriteSPI)
{
temp=*ptrBuff;
SendSPI_Lora(temp, Operation);
}
else
{
RxIdx=0;
SendSPI_Lora(0, Operation);
ptrBuff = RxSPIBuff;
}
Delay(2);
NSS_HI_LoRa;
}
// Main Sequence
void test(void)
{
unsigned char statusLoRa,buff,irqFlags,newData,newOpMode;
unsigned char size = 0;
AccessSPI(R_REGISTER|RegVersion, &newOpMode, kReadSPI);
}
Pict 1. Write signal in MOSI pin
Pict 2. Read Signal in MISO pin
I think my problem is about delay to receive data after transmit data with SPI. But i cannot solve this problem because i should transmit data to receive. Is there any solution for this?
I'm not sure about this one, but I think MISO should also be configured as alternate mode. At least that's what works for me.
You have configured RXNE interrupt, but use TXE as interrupt also.
Use SPI_I2S_GetFlagStatus instead of SPI_I2S_GetITStatus
while(SPI_I2S_GetITStatus(SPI2, SPI_I2S_FLAG_TXE)==SET); is an error. Reference manual for STM32F103, page 710:
The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data.
So it should be while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
With RXNE interrupt you will be sending to USART also after sending the first byte (the register addess)
Here is a CMSIS code for similar transmission ("First cycle is to write address and second cycle is for read/write register.", but without using interrupts, and using STM32F4 with SPI configured as follows:
//af5, afrl
GPIOB->AFR[0] |= ( GPIO_AFRL_AFSEL3_2 | GPIO_AFRL_AFSEL3_0 |
GPIO_AFRL_AFSEL5_2 | GPIO_AFRL_AFSEL5_0 );
GPIOA->AFR[0] |= ( GPIO_AFRL_AFSEL6_2 | GPIO_AFRL_AFSEL6_0 );
//B3 SCK
//A6 MISO
//B5 MOSI
//B6 SS
GPIOA->MODER |= ( GPIO_MODER_MODE6_1 );
GPIOB->MODER |= ( GPIO_MODER_MODE3_1 |
GPIO_MODER_MODE5_1 |
GPIO_MODER_MODE6_0 ); //alternate, 6 output
GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD6_Msk ); // no pull
GPIOB->PUPDR |= GPIO_PUPDR_PUPD3_1;
Are you using a Nano board? If so into which connector did you plug your MISO? I've spent a week once because I have plugged it into Arduino connector that was connected to some other peripheral instead of Morpho connector.
I'm trying to configure the baudrate of USART1 on an STM32L152. When using the external Clock the baudrate is half of what I configured (e.g. 57600 instead of 115200). However, when using the internal HSI everything is correct. The internal is 16 MHz and the external is an 8 MHz crystal that is used to drive the PLL for a 32 MHz system clock.
This is the RCC init code, which is pretty much standard I guess.
int RCC_Configuration(void)
{
/* DISABLE HSI and target clocks prior to clock config */
RCC_HSICmd(DISABLE);
RCC_PLLCmd(DISABLE);
RCC_HSEConfig(RCC_HSE_OFF);
/* Set HSE as sys clock*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Enable ADC & SYSCFG clocks */
RCC_APB2Periph_SYSCFG , ENABLE);
/* Allow access to the RTC */
PWR_RTCAccessCmd(ENABLE);
/* Reset RTC Backup Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
/* LSI used as RTC source clock */
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait until LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable the RTC */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
// ENABLE HSE
RCC_HSEConfig(RCC_HSE_ON);
ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* 32Mhz = 8Mhz * 12 / 3 */
RCC_PLLConfig(RCC_PLLSource_HSE, RCC_PLLMul_12, RCC_PLLDiv_3);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x0C) // 0x0C = PLL
{
}
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR->CR = PWR_CR_VOS_0; /* Select the Voltage Range 1 (1.8V) */
while((PWR->CSR & PWR_CSR_VOSF) != 0); /* Wait for Voltage Regulator Ready */
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* Enable the GPIOs clocks */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC| RCC_AHBPeriph_GPIOD| RCC_AHBPeriph_GPIOE| RCC_AHBPeriph_GPIOH, ENABLE);
/* Enable comparator, LCD and PWR mngt clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP | RCC_APB1Periph_LCD | RCC_APB1Periph_PWR,ENABLE);
}
return 0;
}
I'm using STDperiph to configure the UART1, which on this mcu will run on PCLK2. Checked all the init methods and the register contents. The mantissa and fraction of the baudrate register are correctly calculated and should yield the correct baud rate no matter what the PCLK value is.
This is the UART init code:
void usartinit(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitStructure.USART_BaudRate = 115200 ;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(USARTx_GPIO, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(USARTx_GPIO, GPIO_PinSource10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = USARTx_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USARTx_GPIO, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = USARTx_RX;
GPIO_Init(USARTx_GPIO, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
}
The only possibility I can think of right now is that the crystal is just 4 MHz, but this is a Nucleo board and the MCO from the attached STLink is used for HSE, which definitely is 8 MHz.
What's the blatant mistake I'm making?
It seems the clock is messed up. Check the HSE_VALUE macro in your code. That might be using default Dev board crystal value. I would suggest to change that value to the crystal you are using. You can use this link to set the clock speed.
I think the previous answer is correct. When using HSI your system clock is 16MHz, whereas when using HSE you system clock is 32MHz.
I suspect HSE value is set to 16MHz.
You could also test this by setting the multiplier to 4 and the divider to 2 so that the system clock is 16MHz when running the HSE.
The HSE value will be in the startup code somewhere.
The value is used by the USART code so that it knows what frequency the USART is being driven with in order to calculate the baud rate.
Ok, finally figured it out. On the Nucleo boards in the default configuration, the HSE clock is connected to the MCO clock output of the STLink Programmer on the board. On multiple of my boards however this clock signal gets distorted so much that the target uC only sees 4 MHz. If I output the HSE on the MCO of the target it produces a 4 MHz square wave with a weird duty cycle of 75%. When probing the MCO input signal with a scope the probe capacitance is sufficent to produce the correct 8 MHz input.
So, I guess don't trust your eval boards... Will now get a few crystals and populate the "real" external clock on those boards.
I'm studying ADC programming on STM32 F103x and starting with the simplest case - single conversion.
The internal temperature sensor (connected to ADC1) value is measured and sending it to COM port by using USART. A target seems clear but when I try to download source code to flash, it doesn't send any data to COM port. USART function works well, I guess the problems come from ADC configuration part because I'm being hung in loop of waiting complete conversion:
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Wail for conversion complete
Here is my source code so far.
/* Includes ------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
uint16_t AD_value;
const uint16_t V25 = 1750; //when V25=1.41V
const uint16_t Avg_Slope = 5; //when avg_slc
uint16_t TemperatureC;
//Define output device
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
void Usart1Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* COnfig PA9 for USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* COnfig PA10 for USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
int main(void)
{
ADC_InitTypeDef ADC_InitStructure;
Usart1Init();
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADCCLK = PCLK22/6 = 72/6=12MHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 Clock
/* ADC1 configuration */
ADC_DeInit(ADC1); //Power-on default
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //Independent conversion mode (single)
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Convert single channel only
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Convert 1 time
ADC_InitStructure.ADC_ExternalTrigConv = DISABLE; //No external triggering
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Right 12-bit data alignment
ADC_InitStructure.ADC_NbrOfChannel = 1; //single channel conversion
ADC_Init(ADC1, &ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE); //wake up temperature sensor
ADC_Cmd(ADC1, ENABLE); //Enable ADC1
ADC_ResetCalibration(ADC1); //Enable ADC1 reset calibration register
while(ADC_GetResetCalibrationStatus(ADC1)); //check the end of ADC1 reset calibration register
ADC_StartCalibration(ADC1); //Start ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1)); //Check the end of ADC1 calibration
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_1Cycles5); //Select 1.5 cycles conversion for channel 16
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //Start ADC1 software conversion
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Wail for conversion complete
AD_value = ADC_GetConversionValue(ADC1); //Read ADC value
ADC_ClearFlag(ADC1, ADC_FLAG_EOC); //Clear EOC flag
printf("\r\n ADC value: %d \r\n", AD_value);
TemperatureC = (uint16_t)((V25-AD_value)/Avg_Slope+25);
printf("Temperature: %d%cC\r\n", TemperatureC, 176);
while (1)
{}
}
Any ideas are appreciated!
i'll try to explain what's going on with source code.
1- definition of disable is zero :
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
2- so when you write "ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;" you actually assign it to zero like this "ADC_InitStructure.ADC_ExternalTrigConv = 0;
3- this means your assigning the EXTSEL of ADC_CR2 register to zero.
4-if EXTSEL is zero, adc conversion depends on the timers to start.
5-if you want adc to be started by software, EXTSEL must be 0x000E0000;
6- and as you can guess the value of "ADC_ExternalTrigConv_None" is defined as 0x000E0000;
so while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); code will hung up here because it depens on the timers to start, it cannot end if it is not started.
Problem solved!
It's my fault to disable external trigger. Instead of using:
ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;
It shoud be like this:
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
What a silly!
I will not invent a wheel here by saing that you are hanging in one of those three while() loops. Make some output before each of then then you will know where you are hanging.
If you have debugger (it is present on every ST development board, and possibly you are using it to download program) don't hesitate to use it :)
Recently I've been trying to get the I2C bus working on the STM32F030F4P6 MCU, but with little luck.
I'm using an STM32F0 module and have found plenty of resources for the STM32F1 module I2C initialization, but nothing specific about the STM32F0 initialization/transfer process.
Here's my initialization code:
void i2c_init(uint8_t ownAddress)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
// Enable GPIOA clocks
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Configure I2C1 clock and GPIO
GPIO_StructInit(&GPIO_InitStructure);
/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* I2C1 SDA and SCL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* I2C1 Reset */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
/* Configure I2C1 */
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_StructInit(&I2C_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
//I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = ownAddress;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
//I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
In order to test to see if my setup was correct I designed some I2C transmission code that would transfer data in a never ending loop. Here's the code for that:
while(1)
{
I2C_SlaveAddressConfig(I2C1, RegName);
I2C_GenerateSTART(I2C1, ENABLE);
I2C_NumberOfBytesConfig(I2C1, 8);
I2C_SendData(I2C1,0b00000000);
I2C_GenerateSTOP(I2C1, ENABLE);
}
Where:
RegName = 0x75
SDA = GPIO_PIN_10 on GPIOA
SCL = GPIO_PIN_9 on GPIOA
I2C = I2C1
ownAdrress = 0x68
When I scope the I2C lines after I start this code I get a floating voltage around 160mV. When I step through the code every one of the I2C function calls happen and complete, so that's why I was thinking that it had something more so to do with my initialization of the pins themselves.
My problem is very similar to this thread, but was never answered:
https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a%2f%2fmy%2est%2ecom%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex_mx_stm32%2fSTM32F0%20I2C%20code%20doesn%27t%20work&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=1342
Edit 1: Here's my latest code; still not working (Edit 2: Updated to what I currently have; moved things into more correct locations):
void i2c_init(uint8_t ownAddress)
{
/* TypeDefs for GPIOA and I2C1 */
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* Enable GPIOA clocks and I2C1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Configure I2C1 clock and GPIO */
GPIO_StructInit(&GPIO_InitStructure);
/* I2C1 SDA and SCL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //UP
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
/* GPIO AF Configuration -> GPIO_AF_1: USART2, CEC, Tim3, USART1, USART2,EVENTOUT, I2C1, I2C2, TIM15 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* I2C1 Reset */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
//I2C_DeInit(I2C1);
/* Configure I2C1 */
I2C_StructInit(&I2C_InitStructure);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_OwnAddress1 = ownAddress;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
//I2C_AcknowledgeConfig(I2C1, ENABLE);
}
As you can see I added the two lines GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); and GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); where I found that GPIO_AF_1 had the alternate function of I2C1 from the STM32F0 Standard Peripheral bibliotheek.
Any other ideas? I've been playing around with the clocks to see if that changed anything and have been adding snippets of other people's code just to see if that has any effect on the output of my device.
Edit 3: I have tried pulling both the SDA and SCL lines up to VCC with a 1kohm resistor as instructed by this guide: https://electronics.stackexchange.com/questions/57121/i2c-pullup-resistor-calculations
->(3.3V-0.4)/3mA ~= 1kohm
Edit 4: After going through my code line by line I tried outputting various flag bit registers. Specifically these registers: isr = I2C1->ISR;, cr1 = I2C1->CR1;, and cr2 = I2C1->CR2;
The flag I get after initiating the I2C transfer handling with I2C_TransferHandling(I2C1, 0x02, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); was 0x8001 which can be deciphered down to two errors:
#define I2C_ISR_BUSY ((uint32_t)0x00008000) /*!< Bus busy */
and
#define I2C_ISR_TXE ((uint32_t)0x00000001) /*!< Transmit data register empty */
I've found some work arounds at this link here (remove the space after https: to go to the link -> stack overflow won't let me post more than 1 link for some reason): https: //my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSTM32L151RB%20I2C%20Busy%20flag%20always%20set&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=690 that I'm looking to implement and will report back as soon as I try them out.
I have I2C working as master and slave on the F0. The main issue I can see with your code is that in master mode you absolutely must set the I2C_Timing initialisation structure member. See RM0091 for sample values that correspond to the frequency you want to generate on SCL. In slave mode the clock is recovered from the master so the timing member doesn't appear to be used.
As the others have said, external pullups to Vcc on SCL and SDA are not optional and must be present once per bus not per peripheral as you incorrectly stated in a comment. You were right to use the calculator to choose suitable values because the internal pullups in the STM32 at about 30-50K are far too weak for use as I2C pullups.
The problem is that you don't configure proper AF (Alternate Function) source for I2C pins. After reset registers that configure AF are all 0, and I2C's function is 1, so your I2C peripheral is in fact disconnected from the GPIOs.
Technically it makes no difference when you configure that, but it's best to do that before GPIO configuration to minimize any unwanted transitions on the pins.
Edit: You MUST have the pullups on the I2C pins - without them you have "low" level there, and I2C peripheral detects that as bus error, which obviously prevents it from working properly. Either connect external resistors, or at least enable internal pullups instead of "no pullups/no pulldowns" configuration.
Moreover - it's just not possible for an "open drain" pin to work properly without pullup.
The Core of Cortex M0 is different then Core of Cortex M3, STM32f030f4 has AHB and APB1 (bridged) only. The C code of STM32f1xx never be run under STM32f0xx.
Use STM32F0xx_Snippets_Package for solve your key problems.
I just noticed this too:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
So you have disabled the clock after you enabled it. I don't see where you have re-enabled it later.
I notice that you are mapping the pins to AF_1 and that is for the UART function.
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
You need to map them to AF_4 to use the I2C function.
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_4);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_4);
Working on a simple ADC project reading voltages from a potential divider on a number of channels. I am using the STM32f103rb on a commercial and well made PCB. I am reading pins on PORTA and the following pins work and return a voltage:
PA0, PA1, PA4, PA5, PA6, PA7.
However the following pins do not work and return a raw value of around 2000-2700 approx:
PA8, PA11 and PA12.
The nature of the project and the fact that the PCB is of a fixed design means that we are stuck with these pin selections. The datasheet is quite specific about these pins being usable as AIN. All set-up and config is as per standard STM32, taken from basic example code and modified for our purposes. The included code is a debugging version we made in an attempt to find the cause but to no avail.
Voltage at the pins has been measured and is correct for the type of voltage divider.
Any help would be greatly appreciated on this.
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
// Standard STM peripheral config functions
void RCC_Configuration(void);
void GPIO_Configuration(void);
// ADC config - STM example code
void NEW_ADC_Configuration(void);
// Function to read ADC channel and output value
u16 NEW_readADC1(u8 channel);
// Variables
double voltage_su; // Variable to store supply voltage
double voltage7;
double voltage8;
//*****************************************************************************
// Main program
int main(void)
{
// Initialise peripheral modules
RCC_Configuration();
GPIO_Configuration();
NEW_ADC_Configuration();
// Infinate loop
while (1)
{
// Get value of supply voltage and convert
voltage_su = (((3.3 * NEW_readADC1(ADC_Channel_12)) / 4095));
}
}
//*****************************************************************************
// STM RCC config
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // Connect PORTC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Connect PORTB...
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Connect PORTA...
}
//*****************************************************************************
// STM GPIO config
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // Value for setting up the pins
// Sup
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//*****************************************************************************
void NEW_ADC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure; // Varibale used to setup the ADC
RCC_ADCCLKConfig(RCC_PCLK2_Div4); // Set ADC clock to /4
// Enable ADC 1 so we can use it
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// Conenct the port A to peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Restore to defaults
ADC_DeInit(ADC1);
/* ADC1 Configuration ----------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// Scan 1 at a time
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
// No continuous conversions - do them on demand
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
// Start conversion on software request - not bu external trigger
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
// Conversions are 12 bit - put them in the lower 12 bits of the result
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// How many channels are to be sued by the sequencer
ADC_InitStructure.ADC_NbrOfChannel = 1;
// Setup ADC
ADC_Init(ADC1, &ADC_InitStructure);
// Enable ADC 1
ADC_Cmd(ADC1, ENABLE);
// Enable ADC1 reset calibaration register
ADC_ResetCalibration(ADC1);
// Check end of reset calib reg
while(ADC_GetResetCalibrationStatus(ADC1));
//Start ADC1 calib
ADC_StartCalibration(ADC1);
// Check the end of ADC1 calib
while(ADC_GetCalibrationStatus(ADC1));
}
//*****************************************************************************
// Function to return the value of ADC ch
u16 NEW_readADC1(u8 channel)
{
// Config the channel to be sampled
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_239Cycles5);
// Start the conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait until conversion complete
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
// Get the conversion value
return ADC_GetConversionValue(ADC1);
}
//*****************************************************************************
I have never worked with this chip, but after looking at the hardware data sheet for 5 minutes I came up with the following naive conclusions:
The chip has 2 ADCs with 8 channels each, a total of 16.
PA0, PA1, PA4, PA5, PA6, PA7 sounds as if they will belong to the first ADC.
I then assume that PA8, PA11 and PA12 belong to the second ADC, called ADC2 by ST.
Your code seems to only ever mention and initialize ADC1. No trace of ADC2.
Maybe these pins don't work because you haven't even initialized or activated the ADC they belong to?
Also, you should check the data sheet for contention due to those pins being used as "Alternate Function".
Typically, they will list what needs to be done/disconnected in order to utilize the pins without contention (e.g. disconnect some resistor)