diff --git a/BluepillTrouba.ioc b/BluepillTrouba.ioc index d087fba..a6b189b 100644 --- a/BluepillTrouba.ioc +++ b/BluepillTrouba.ioc @@ -42,44 +42,44 @@ Mcu.CPN=STM32F103CBT6 Mcu.Family=STM32F1 Mcu.IP0=ADC1 Mcu.IP1=DMA -Mcu.IP10=USART1 +Mcu.IP10=TIM4 +Mcu.IP11=USART1 Mcu.IP2=FREERTOS Mcu.IP3=IWDG Mcu.IP4=NVIC Mcu.IP5=RCC -Mcu.IP6=SPI1 +Mcu.IP6=SPI2 Mcu.IP7=SYS Mcu.IP8=TIM2 -Mcu.IP9=TIM4 -Mcu.IPNb=11 +Mcu.IP9=TIM3 +Mcu.IPNb=12 Mcu.Name=STM32F103C(8-B)Tx Mcu.Package=LQFP48 Mcu.Pin0=PC13-TAMPER-RTC -Mcu.Pin1=PD0-OSC_IN -Mcu.Pin10=PB10 -Mcu.Pin11=PA9 -Mcu.Pin12=PA10 -Mcu.Pin13=PA13 -Mcu.Pin14=PA14 -Mcu.Pin15=PA15 -Mcu.Pin16=PB6 -Mcu.Pin17=PB7 -Mcu.Pin18=PB8 -Mcu.Pin19=VP_ADC1_TempSens_Input -Mcu.Pin2=PD1-OSC_OUT -Mcu.Pin20=VP_ADC1_Vref_Input -Mcu.Pin21=VP_FREERTOS_VS_CMSIS_V2 -Mcu.Pin22=VP_IWDG_VS_IWDG -Mcu.Pin23=VP_SYS_VS_tim1 -Mcu.Pin24=VP_TIM2_VS_ClockSourceINT -Mcu.Pin3=PA0-WKUP -Mcu.Pin4=PA1 -Mcu.Pin5=PA5 -Mcu.Pin6=PA6 -Mcu.Pin7=PA7 -Mcu.Pin8=PB0 -Mcu.Pin9=PB1 -Mcu.PinsNb=25 +Mcu.Pin1=PA0-WKUP +Mcu.Pin10=PA10 +Mcu.Pin11=PA13 +Mcu.Pin12=PA14 +Mcu.Pin13=PA15 +Mcu.Pin14=PB6 +Mcu.Pin15=PB7 +Mcu.Pin16=PB8 +Mcu.Pin17=VP_ADC1_TempSens_Input +Mcu.Pin18=VP_ADC1_Vref_Input +Mcu.Pin19=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin2=PA1 +Mcu.Pin20=VP_IWDG_VS_IWDG +Mcu.Pin21=VP_SYS_VS_tim1 +Mcu.Pin22=VP_TIM2_VS_ClockSourceINT +Mcu.Pin23=VP_TIM3_VS_ClockSourceINT +Mcu.Pin3=PA6 +Mcu.Pin4=PB0 +Mcu.Pin5=PB1 +Mcu.Pin6=PB10 +Mcu.Pin7=PB13 +Mcu.Pin8=PB15 +Mcu.Pin9=PA9 +Mcu.PinsNb=24 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F103CBTx @@ -123,18 +123,8 @@ PA15.GPIOParameters=GPIO_Speed,GPIO_Label PA15.GPIO_Label=BUZZER PA15.GPIO_Speed=GPIO_SPEED_FREQ_HIGH PA15.Signal=S_TIM2_CH1_ETR -PA5.GPIOParameters=GPIO_Label -PA5.GPIO_Label=OLED_SCLK -PA5.Mode=TX_Only_Simplex_Unidirect_Master -PA5.Signal=SPI1_SCK -PA6.GPIOParameters=GPIO_Label -PA6.GPIO_Label=HEATER PA6.Locked=true -PA6.Signal=GPIO_Output -PA7.GPIOParameters=GPIO_Label -PA7.GPIO_Label=OLED_MOSI -PA7.Mode=TX_Only_Simplex_Unidirect_Master -PA7.Signal=SPI1_MOSI +PA6.Signal=S_TIM3_CH1 PA9.Mode=Asynchronous PA9.Signal=USART1_TX PB0.GPIOParameters=GPIO_Label @@ -149,6 +139,10 @@ PB10.GPIOParameters=GPIO_Label PB10.GPIO_Label=OLED_RST PB10.Locked=true PB10.Signal=GPIO_Output +PB13.Mode=TX_Only_Simplex_Unidirect_Master +PB13.Signal=SPI2_SCK +PB15.Mode=TX_Only_Simplex_Unidirect_Master +PB15.Signal=SPI2_MOSI PB6.GPIOParameters=GPIO_PuPd,GPIO_Label PB6.GPIO_Label=KNOB_A PB6.GPIO_PuPd=GPIO_PULLUP @@ -165,10 +159,6 @@ PC13-TAMPER-RTC.GPIOParameters=GPIO_Label PC13-TAMPER-RTC.GPIO_Label=LED PC13-TAMPER-RTC.Locked=true PC13-TAMPER-RTC.Signal=GPIO_Output -PD0-OSC_IN.Mode=HSE-External-Oscillator -PD0-OSC_IN.Signal=RCC_OSC_IN -PD1-OSC_OUT.Mode=HSE-External-Oscillator -PD1-OSC_OUT.Signal=RCC_OSC_OUT PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false @@ -180,7 +170,7 @@ ProjectManager.DefaultFWLocation=true ProjectManager.DeletePrevious=true ProjectManager.DeviceId=STM32F103CBTx ProjectManager.FirmwarePackage=STM32Cube FW_F1 V1.8.4 -ProjectManager.FreePins=false +ProjectManager.FreePins=true ProjectManager.HalAssertFull=true ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true @@ -197,51 +187,56 @@ ProjectManager.StackSize=0x400 ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_IWDG_Init-IWDG-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_ADC1_Init-ADC1-false-HAL-true,6-MX_SPI1_Init-SPI1-false-HAL-true,7-MX_DMA_Init-DMA-false-HAL-true,8-MX_TIM4_Init-TIM4-false-HAL-true,9-MX_TIM2_Init-TIM2-false-HAL-true,10-MX_TIM3_Init-TIM3-false-HAL-true -RCC.ADCFreqValue=2250000 +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_IWDG_Init-IWDG-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_ADC1_Init-ADC1-false-HAL-true,6-MX_DMA_Init-DMA-false-HAL-true,7-MX_TIM4_Init-TIM4-false-HAL-true,8-MX_TIM2_Init-TIM2-false-HAL-true,9-MX_TIM3_Init-TIM3-false-HAL-true,10-MX_SPI2_Init-SPI2-false-HAL-true +RCC.ADCFreqValue=8000000 RCC.ADCPresc=RCC_ADCPCLK2_DIV8 -RCC.AHBFreq_Value=72000000 +RCC.AHBFreq_Value=64000000 RCC.APB1CLKDivider=RCC_HCLK_DIV2 -RCC.APB1Freq_Value=36000000 -RCC.APB1TimFreq_Value=72000000 -RCC.APB2CLKDivider=RCC_HCLK_DIV4 -RCC.APB2Freq_Value=18000000 -RCC.APB2TimFreq_Value=36000000 -RCC.FCLKCortexFreq_Value=72000000 +RCC.APB1Freq_Value=32000000 +RCC.APB1TimFreq_Value=64000000 +RCC.APB2Freq_Value=64000000 +RCC.APB2TimFreq_Value=64000000 +RCC.FCLKCortexFreq_Value=64000000 RCC.FamilyName=M -RCC.HCLKFreq_Value=72000000 -RCC.IPParameters=ADCFreqValue,ADCPresc,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2CLKDivider,APB2Freq_Value,APB2TimFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,PLLSourceVirtual,RTCClockSelection,RTCFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,TimSysFreq_Value,USBFreq_Value,VCOOutput2Freq_Value -RCC.MCOFreq_Value=72000000 -RCC.PLLCLKFreq_Value=72000000 -RCC.PLLMCOFreq_Value=36000000 -RCC.PLLMUL=RCC_PLL_MUL9 -RCC.PLLSourceVirtual=RCC_PLLSOURCE_HSE +RCC.HCLKFreq_Value=64000000 +RCC.IPParameters=ADCFreqValue,ADCPresc,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,RTCClockSelection,RTCFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,TimSysFreq_Value,USBFreq_Value,VCOOutput2Freq_Value +RCC.MCOFreq_Value=64000000 +RCC.PLLCLKFreq_Value=64000000 +RCC.PLLMCOFreq_Value=32000000 +RCC.PLLMUL=RCC_PLL_MUL16 RCC.RTCClockSelection=RCC_RTCCLKSOURCE_LSE RCC.RTCFreq_Value=32768 -RCC.SYSCLKFreq_VALUE=72000000 +RCC.SYSCLKFreq_VALUE=64000000 RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK -RCC.TimSysFreq_Value=72000000 -RCC.USBFreq_Value=72000000 -RCC.VCOOutput2Freq_Value=8000000 +RCC.TimSysFreq_Value=64000000 +RCC.USBFreq_Value=64000000 +RCC.VCOOutput2Freq_Value=4000000 SH.ADCx_IN0.0=ADC1_IN0,IN0 SH.ADCx_IN0.ConfNb=1 SH.ADCx_IN1.0=ADC1_IN1,IN1 SH.ADCx_IN1.ConfNb=1 SH.S_TIM2_CH1_ETR.0=TIM2_CH1,PWM Generation1 CH1 SH.S_TIM2_CH1_ETR.ConfNb=1 +SH.S_TIM3_CH1.0=TIM3_CH1,PWM Generation1 CH1 +SH.S_TIM3_CH1.ConfNb=1 SH.S_TIM4_CH1.0=TIM4_CH1,Encoder_Interface SH.S_TIM4_CH1.ConfNb=1 SH.S_TIM4_CH2.0=TIM4_CH2,Encoder_Interface SH.S_TIM4_CH2.ConfNb=1 -SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8 -SPI1.CalculateBaudRate=2.25 MBits/s -SPI1.Direction=SPI_DIRECTION_2LINES -SPI1.IPParameters=VirtualType,Mode,Direction,BaudRatePrescaler,CalculateBaudRate -SPI1.Mode=SPI_MODE_MASTER -SPI1.VirtualType=VM_MASTER +SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8 +SPI2.CalculateBaudRate=4.0 MBits/s +SPI2.Direction=SPI_DIRECTION_2LINES +SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler +SPI2.Mode=SPI_MODE_MASTER +SPI2.VirtualType=VM_MASTER TIM2.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM2.IPParameters=Channel-PWM Generation1 CH1,Prescaler TIM2.Prescaler=2 +TIM3.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 +TIM3.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Pulse-PWM Generation1 CH1,Period +TIM3.Period=64000 +TIM3.Prescaler=2000 +TIM3.Pulse-PWM\ Generation1\ CH1=32000 TIM4.IC2Filter=15 TIM4.IPParameters=IC2Filter,Prescaler TIM4.Prescaler=0 @@ -260,4 +255,6 @@ VP_SYS_VS_tim1.Mode=TIM1 VP_SYS_VS_tim1.Signal=SYS_VS_tim1 VP_TIM2_VS_ClockSourceINT.Mode=Internal VP_TIM2_VS_ClockSourceINT.Signal=TIM2_VS_ClockSourceINT +VP_TIM3_VS_ClockSourceINT.Mode=Internal +VP_TIM3_VS_ClockSourceINT.Signal=TIM3_VS_ClockSourceINT board=custom diff --git a/Core/Inc/main.h b/Core/Inc/main.h index ce59137..63e9374 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -63,12 +63,6 @@ void Error_Handler(void); #define ADC_PT100_GPIO_Port GPIOA #define ADC_SENSR_Pin GPIO_PIN_1 #define ADC_SENSR_GPIO_Port GPIOA -#define OLED_SCLK_Pin GPIO_PIN_5 -#define OLED_SCLK_GPIO_Port GPIOA -#define HEATER_Pin GPIO_PIN_6 -#define HEATER_GPIO_Port GPIOA -#define OLED_MOSI_Pin GPIO_PIN_7 -#define OLED_MOSI_GPIO_Port GPIOA #define OLED_CS_Pin GPIO_PIN_0 #define OLED_CS_GPIO_Port GPIOB #define OLED_DC_Pin GPIO_PIN_1 diff --git a/Core/Inc/spi.h b/Core/Inc/spi.h index f40913c..522da13 100644 --- a/Core/Inc/spi.h +++ b/Core/Inc/spi.h @@ -32,13 +32,13 @@ extern "C" { /* USER CODE END Includes */ -extern SPI_HandleTypeDef hspi1; +extern SPI_HandleTypeDef hspi2; /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ -void MX_SPI1_Init(void); +void MX_SPI2_Init(void); /* USER CODE BEGIN Prototypes */ diff --git a/Core/Inc/tim.h b/Core/Inc/tim.h index 280ba03..9f1cdf9 100644 --- a/Core/Inc/tim.h +++ b/Core/Inc/tim.h @@ -34,6 +34,8 @@ extern "C" { extern TIM_HandleTypeDef htim2; +extern TIM_HandleTypeDef htim3; + extern TIM_HandleTypeDef htim4; /* USER CODE BEGIN Private defines */ @@ -41,6 +43,7 @@ extern TIM_HandleTypeDef htim4; /* USER CODE END Private defines */ void MX_TIM2_Init(void); +void MX_TIM3_Init(void); void MX_TIM4_Init(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); diff --git a/Core/Src/app_heater.c b/Core/Src/app_heater.c index c6ce038..490b645 100644 --- a/Core/Src/app_heater.c +++ b/Core/Src/app_heater.c @@ -8,18 +8,16 @@ void app_heater_init() { -// HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); -// TIM3->ARR = 25714; + HAL_TIM_Base_Start(&htim3); + HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); } void app_heater_set(bool active) { -// if (active) { -// TIM3->CCR1 = TIM3->ARR / 2; // testing - 50% -// } else { -// TIM3->CCR1 = 0; -// } - - HAL_GPIO_WritePin(HEATER_GPIO_Port, HEATER_Pin, active); + if (active) { + TIM3->CCR1 = TIM3->ARR / 2; // testing - 50% + } else { + TIM3->CCR1 = 0; + } } diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 051092a..28712fa 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -38,6 +38,8 @@ * Output * EVENT_OUT * EXTI + * Free pins are configured automatically as Analog (this feature is enabled through + * the Code Generation settings) */ void MX_GPIO_Init(void) { @@ -53,9 +55,6 @@ void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(HEATER_GPIO_Port, HEATER_Pin, GPIO_PIN_RESET); - /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, OLED_CS_Pin|OLED_DC_Pin|OLED_RST_Pin, GPIO_PIN_RESET); @@ -66,12 +65,22 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pin : PtPin */ - GPIO_InitStruct.Pin = HEATER_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(HEATER_GPIO_Port, &GPIO_InitStruct); + /*Configure GPIO pins : PC14 PC15 */ + GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pins : PD0 PD1 */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); + + /*Configure GPIO pins : PA2 PA3 PA4 PA5 + PA7 PA8 PA11 PA12 */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5 + |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PBPin PBPin PBPin */ GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_DC_Pin|OLED_RST_Pin; @@ -80,12 +89,22 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pins : PB2 PB11 PB12 PB14 + PB3 PB4 PB5 PB9 */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14 + |GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = KNOB_PUSH_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(KNOB_PUSH_GPIO_Port, &GPIO_InitStruct); + /*Configure peripheral I/O remapping */ + __HAL_AFIO_REMAP_PD01_ENABLE(); + } /* USER CODE BEGIN 2 */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 8b9119f..d9d03ab 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -100,10 +100,11 @@ int main(void) MX_IWDG_Init(); MX_USART1_UART_Init(); MX_ADC1_Init(); - MX_SPI1_Init(); MX_DMA_Init(); MX_TIM4_Init(); MX_TIM2_Init(); + MX_TIM3_Init(); + MX_SPI2_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ @@ -139,14 +140,13 @@ void SystemClock_Config(void) /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); @@ -159,7 +159,7 @@ void SystemClock_Config(void) RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { diff --git a/Core/Src/oled.c b/Core/Src/oled.c index 78403b0..8f61ffc 100644 --- a/Core/Src/oled.c +++ b/Core/Src/oled.c @@ -8,6 +8,8 @@ #include "gpio.h" #include "spi.h" +#define OLED_HSPI hspi2 + #define SSD1309_HEIGHT 64 static inline void cs_select() { @@ -36,14 +38,14 @@ void oled_command16(uint8_t cmd, uint8_t arg) { buf[1] = arg; dc_command(); cs_select(); - HAL_SPI_Transmit(&hspi1, buf, 2, 1000); + HAL_SPI_Transmit(&OLED_HSPI, buf, 2, 1000); cs_deselect(); } void oled_command(uint8_t cmd) { dc_command(); cs_select(); - HAL_SPI_Transmit(&hspi1, &cmd, 1, 1000); + HAL_SPI_Transmit(&OLED_HSPI, &cmd, 1, 1000); cs_deselect(); } @@ -161,7 +163,7 @@ void oled_init() { void oled_data(uint8_t *data, size_t len) { dc_data(); cs_select(); - HAL_SPI_Transmit(&hspi1, data, len, 1000); + HAL_SPI_Transmit(&OLED_HSPI, data, len, 1000); cs_deselect(); } diff --git a/Core/Src/spi.c b/Core/Src/spi.c index 08b3036..b3f5ad1 100644 --- a/Core/Src/spi.c +++ b/Core/Src/spi.c @@ -24,38 +24,38 @@ /* USER CODE END 0 */ -SPI_HandleTypeDef hspi1; +SPI_HandleTypeDef hspi2; -/* SPI1 init function */ -void MX_SPI1_Init(void) +/* SPI2 init function */ +void MX_SPI2_Init(void) { - /* USER CODE BEGIN SPI1_Init 0 */ - - /* USER CODE END SPI1_Init 0 */ - - /* USER CODE BEGIN SPI1_Init 1 */ - - /* USER CODE END SPI1_Init 1 */ - hspi1.Instance = SPI1; - hspi1.Init.Mode = SPI_MODE_MASTER; - hspi1.Init.Direction = SPI_DIRECTION_2LINES; - hspi1.Init.DataSize = SPI_DATASIZE_8BIT; - hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; - hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; - hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi1.Init.TIMode = SPI_TIMODE_DISABLE; - hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi1.Init.CRCPolynomial = 10; - if (HAL_SPI_Init(&hspi1) != HAL_OK) + /* USER CODE BEGIN SPI2_Init 0 */ + + /* USER CODE END SPI2_Init 0 */ + + /* USER CODE BEGIN SPI2_Init 1 */ + + /* USER CODE END SPI2_Init 1 */ + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES; + hspi2.Init.DataSize = SPI_DATASIZE_8BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 10; + if (HAL_SPI_Init(&hspi2) != HAL_OK) { Error_Handler(); } - /* USER CODE BEGIN SPI1_Init 2 */ + /* USER CODE BEGIN SPI2_Init 2 */ - /* USER CODE END SPI1_Init 2 */ + /* USER CODE END SPI2_Init 2 */ } @@ -63,50 +63,50 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(spiHandle->Instance==SPI1) + if(spiHandle->Instance==SPI2) { - /* USER CODE BEGIN SPI1_MspInit 0 */ + /* USER CODE BEGIN SPI2_MspInit 0 */ - /* USER CODE END SPI1_MspInit 0 */ - /* SPI1 clock enable */ - __HAL_RCC_SPI1_CLK_ENABLE(); + /* USER CODE END SPI2_MspInit 0 */ + /* SPI2 clock enable */ + __HAL_RCC_SPI2_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - /**SPI1 GPIO Configuration - PA5 ------> SPI1_SCK - PA7 ------> SPI1_MOSI + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**SPI2 GPIO Configuration + PB13 ------> SPI2_SCK + PB15 ------> SPI2_MOSI */ - GPIO_InitStruct.Pin = OLED_SCLK_Pin|OLED_MOSI_Pin; + GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN SPI1_MspInit 1 */ + /* USER CODE BEGIN SPI2_MspInit 1 */ - /* USER CODE END SPI1_MspInit 1 */ + /* USER CODE END SPI2_MspInit 1 */ } } void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) { - if(spiHandle->Instance==SPI1) + if(spiHandle->Instance==SPI2) { - /* USER CODE BEGIN SPI1_MspDeInit 0 */ + /* USER CODE BEGIN SPI2_MspDeInit 0 */ - /* USER CODE END SPI1_MspDeInit 0 */ + /* USER CODE END SPI2_MspDeInit 0 */ /* Peripheral clock disable */ - __HAL_RCC_SPI1_CLK_DISABLE(); + __HAL_RCC_SPI2_CLK_DISABLE(); - /**SPI1 GPIO Configuration - PA5 ------> SPI1_SCK - PA7 ------> SPI1_MOSI + /**SPI2 GPIO Configuration + PB13 ------> SPI2_SCK + PB15 ------> SPI2_MOSI */ - HAL_GPIO_DeInit(GPIOA, OLED_SCLK_Pin|OLED_MOSI_Pin); + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_15); - /* USER CODE BEGIN SPI1_MspDeInit 1 */ + /* USER CODE BEGIN SPI2_MspDeInit 1 */ - /* USER CODE END SPI1_MspDeInit 1 */ + /* USER CODE END SPI2_MspDeInit 1 */ } } diff --git a/Core/Src/stm32f1xx_hal_timebase_tim.c b/Core/Src/stm32f1xx_hal_timebase_tim.c index fe33581..36dca2d 100644 --- a/Core/Src/stm32f1xx_hal_timebase_tim.c +++ b/Core/Src/stm32f1xx_hal_timebase_tim.c @@ -57,7 +57,7 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); /* Compute TIM1 clock */ - uwTimclock = 2*HAL_RCC_GetPCLK2Freq(); + uwTimclock = HAL_RCC_GetPCLK2Freq(); /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); diff --git a/Core/Src/tim.c b/Core/Src/tim.c index 54d2995..a89d69f 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -25,6 +25,7 @@ /* USER CODE END 0 */ TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; TIM_HandleTypeDef htim4; /* TIM2 init function */ @@ -80,6 +81,60 @@ void MX_TIM2_Init(void) /* USER CODE END TIM2_Init 2 */ HAL_TIM_MspPostInit(&htim2); +} +/* TIM3 init function */ +void MX_TIM3_Init(void) +{ + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 2000; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 64000; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 32000; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_Init 2 */ + HAL_TIM_MspPostInit(&htim3); + } /* TIM4 init function */ void MX_TIM4_Init(void) @@ -140,6 +195,17 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM2_MspInit 1 */ } + else if(tim_baseHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspInit 0 */ + + /* USER CODE END TIM3_MspInit 0 */ + /* TIM3 clock enable */ + __HAL_RCC_TIM3_CLK_ENABLE(); + /* USER CODE BEGIN TIM3_MspInit 1 */ + + /* USER CODE END TIM3_MspInit 1 */ + } } void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle) @@ -181,7 +247,6 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) /* USER CODE BEGIN TIM2_MspPostInit 0 */ /* USER CODE END TIM2_MspPostInit 0 */ - __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM2 GPIO Configuration PA15 ------> TIM2_CH1 @@ -197,6 +262,25 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) /* USER CODE END TIM2_MspPostInit 1 */ } + else if(timHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspPostInit 0 */ + + /* USER CODE END TIM3_MspPostInit 0 */ + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**TIM3 GPIO Configuration + PA6 ------> TIM3_CH1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM3_MspPostInit 1 */ + + /* USER CODE END TIM3_MspPostInit 1 */ + } } @@ -214,6 +298,17 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM2_MspDeInit 1 */ } + else if(tim_baseHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspDeInit 0 */ + + /* USER CODE END TIM3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM3_CLK_DISABLE(); + /* USER CODE BEGIN TIM3_MspDeInit 1 */ + + /* USER CODE END TIM3_MspDeInit 1 */ + } } void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* tim_encoderHandle) diff --git a/Makefile b/Makefile index 2d86b15..0a2e579 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Mon Mar 06 00:56:47 CET 2023] +# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Mon Mar 06 21:48:34 CET 2023] ########################################################################################################################## # ------------------------------------------------