2024-05-03 05:50 AM - last edited on 2024-05-03 11:17 AM by SofLit
I want to establish can communication on CAN1 in which message being transmitted ,after every 5 m,/s. Below is my main.c and mcu.c .
This is mcu.c file.
#include "Mcu.h"
#include "Mcu_Config.h"
#include "Mcu_Internal.h"
#include "Mcu_RegTypes.h"
#include "stm32f407xx.h"
Mcu_ConfigType* Mcu_pConfigPtr = NULL_PTR;
#define RCC_CRREG ((uint32*)RCCREG_BASE)
static FUNC(void, MCU_CODE) Mcu_Pwr_Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_Flash_Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_AllPeriphralReset(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_AHB1Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_AHB2Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_AHB3Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_APB1Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_APB2Init(VAR( void, AUTOMATIC));
static FUNC(void, MCU_CODE) Mcu_Pwr_Init(VAR( void, AUTOMATIC))
{
PwrConfigType* LpPwrConfigPtr;
PWR_RegTypedef* LpPWR_Reg = PWR_REGADDRESS;
LpPwrConfigPtr = Mcu_pConfigPtr->PWRConfgSet;
#if(MCU_VOS_SCALE_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.VOS;
#endif
#if(MCU_FPDS_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.FPDS;
#endif
#if(MCU_DBP_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.DBP;
#endif
#if(MCU_PLS_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.PLS;
#endif
#if(MCU_PVDE_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.PVDE;
#endif
#if(MCU_PDDS_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.PDDS;
#endif
#if(MCU_LPDS_USED == STD_ON)
LpPWR_Reg->CR |= LpPwrConfigPtr->PwrCRConfgVal.LPDS;
#endif
#if(MCU_BRE_USED == STD_ON)
LpPWR_Reg->CSR |= LpPwrConfigPtr->PwrCSRConfgVal.BRE;
#endif
#if(MCU_EWUP_USED == STD_ON)
LpPWR_Reg->CSR |= LpPwrConfigPtr->PwrCSRConfgVal.EWUP;
#endif
}
static FUNC(void, MCU_CODE) Mcu_Flash_Init(VAR( void, AUTOMATIC))
{
FLASHConfigType* LpFlashconfigPtr;
FLASH_RegTypedef* LpFLASH_Reg = FLASH_REGADDRESS;
LpFlashconfigPtr = Mcu_pConfigPtr->FlashConfgSet;
#if(MCU_DCRST_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.DCRST;
#endif
#if(MCU_ICRST_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.ICRST;
#endif
#if(MCU_LATENCY_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.LATENCY;
#endif
#if(MCU_DCEN_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.DCEN;
#endif
#if(MCU_ICEN_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.ICEN;
#endif
#if(MCU_PRFTEN_USED == STD_ON)
LpFLASH_Reg->ACR |= LpFlashconfigPtr->FLASHACRConfgVal.PRFTEN;
#endif
}
static FUNC(void, MCU_CODE) Mcu_AllPeriphralReset(VAR( void, AUTOMATIC))
{
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
/*Reset the MCU Peripherals */
LpRCC_Reg->AHB1RSTR =(uint32)(RCC_AHB1RSTR_RESET);
LpRCC_Reg->AHB1RSTR =(uint32)(0x00000000);
LpRCC_Reg->AHB2RSTR =(uint32)(RCC_AHB2RSTR_RESET);
LpRCC_Reg->AHB2RSTR =(uint32)(0x00000000);
LpRCC_Reg->AHB3RSTR =(uint32)(RCC_AHB3RSTR_RESET);
LpRCC_Reg->AHB3RSTR =(uint32)(0x00000000);
LpRCC_Reg->APB1RSTR =(uint32)(RCC_APB1RSTR_RESET);
LpRCC_Reg->APB1RSTR =(uint32)(0x00000000);
LpRCC_Reg->APB2RSTR =(uint32)(RCC_APB2RSTR_RESET);
LpRCC_Reg->APB2RSTR =(uint32)(0x00000000);
/*Reset the MCU Peripherals */
}
static FUNC(void, MCU_CODE) Mcu_AHB1Init(VAR( void, AUTOMATIC))
{
McuClockConfigType* LpMcuClockConfigPtr;
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
/*McuAHB1ENRConfgVal*/
#ifdef MCU_USB_OTG_USED
#if(OTGHSULPIEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.OTGHSULPIEN;
#endif
#if(OTGHSEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.OTGHSEN;
#endif
#endif
#ifdef MCU_ETHMAC_USED
#if(ETHMACPTPEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.ETHMACPTPEN;
#endif
#if(ETHMACRXEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.ETHMACRXEN;
#endif
#if(ETHMACTXEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.ETHMACTXEN;
#endif
#if(ETHMACEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.ETHMACEN;
#endif
#endif
#ifdef MCU_DMA_USED
#if(DMA2EN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.DMA2EN;
#endif
#if(DMA1EN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.DMA1EN;
#endif
#endif
#ifdef MCU_CCM_DATA_RAM_USED
#if(CCMDATARAMEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.CCMDATARAMEN;
#endif
#endif
#ifdef MCU_BKP_SRAM_USED
#if(BKPSRAMEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.BKPSRAMEN;
#endif
#endif
#ifdef MCU_CRC_USED
#if(CRCEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.CRCEN;
#endif
#endif
#ifdef MCU_GPIO_USED
#if(GPIOIEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOIEN;
#endif
#if(GPIOHEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOHEN;
#endif
#if(GPIOGEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOGEN;
#endif
#if(GPIOFEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOFEN;
#endif
#if(GPIOEEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOEEN;
#endif
#if(GPIODEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIODEN;
#endif
#if(GPIOCEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOCEN;
#endif
#if(GPIOBEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOBEN;
#endif
#if(GPIOAEN_USED == STD_ON)
LpRCC_Reg->AHB1ENR |= LpMcuClockConfigPtr->McuAHB1ENRConfgVal.GPIOAEN;
#endif
#endif
}
static FUNC(void, MCU_CODE) Mcu_AHB2Init(VAR( void, AUTOMATIC))
{
//McuClockConfigType* LpMcuClockConfigPtr;
//RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
//LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
/*McuAHB2ENRConfgVal*/
#if(OTGFSEN_USED == STD_ON)
LpRCC_Reg->AHB2ENR |= LpMcuClockConfigPtr->McuAHB2ENRConfgVal.OTGFSEN;
#endif
#if(RNGEN_USED == STD_ON)
LpRCC_Reg->AHB2ENR |= LpMcuClockConfigPtr->McuAHB2ENRConfgVal.RNGEN;
#endif
#if(HASHEN_USED == STD_ON)
LpRCC_Reg->AHB2ENR |= LpMcuClockConfigPtr->McuAHB2ENRConfgVal.HASHEN;
#endif
#if(CRYPEN_USED == STD_ON)
LpRCC_Reg->AHB2ENR |= LpMcuClockConfigPtr->McuAHB2ENRConfgVal.CRYPEN;
#endif
#if(DCMIEN_USED == STD_ON)
LpRCC_Reg->AHB2ENR |= LpMcuClockConfigPtr->McuAHB2ENRConfgVal.DCMIEN;
#endif
}
static FUNC(void, MCU_CODE) Mcu_AHB3Init(VAR( void, AUTOMATIC))
{
//McuClockConfigType* LpMcuClockConfigPtr;
//RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
//LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
/*McuAHB3ENRConfgVal*/
#if(DCMIEN_USED == STD_ON)
LpRCC_Reg->AHB3ENR |= LpMcuClockConfigPtr->McuAHB3ENRConfgVal.FSMCEN;
#endif
}
static FUNC(void, MCU_CODE) Mcu_APB1Init(VAR( void, AUTOMATIC))
{
McuClockConfigType* LpMcuClockConfigPtr;
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
/*McuAPB1ENRConfgVal*/
#ifdef MCU_DAC_USED
#if(DACEN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.DACEN;
#endif
#endif
#ifdef MCU_PWR_USED
#if(PWREN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.PWREN;
#endif
#endif
#ifdef MCU_CAN_USED
#if(CAN2EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.CAN2EN;
#endif
#if(CAN1EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.CAN1EN;
#endif
#endif
#ifdef MCU_I2C_USED
#if(I2C3EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.I2C3EN;
#endif
#if(I2C2EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.I2C2EN;
#endif
#if(I2C1EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.I2C1EN;
#endif
#endif
#ifdef MCU_UART_USED
#if(UART5EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.UART5EN;
#endif
#if(UART4EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.UART4EN;
#endif
#endif
#ifdef MCU_USART_USED
#if(USART3EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.USART3EN;
#endif
#if(USART2EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.USART2EN;
#endif
#endif
#ifdef MCU_SPI_USED
#if(SPI3EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.SPI3EN;
#endif
#if(SPI2EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.SPI2EN;
#endif
#endif
#if(WWDGEN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.WWDGEN;
#endif
#ifdef MCU_TIM_USED
#if(TIM14EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.UART4EN;
#endif
#if(TIM13EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM13EN;
#endif
#if(TIM12EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM12EN;
#endif
#if(TIM7EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM7EN;
#endif
#if(TIM6EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM6EN;
#endif
#if(TIM5EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM5EN;
#endif
#if(TIM4EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM4EN;
#endif
#if(TIM3EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM3EN;
#endif
#if(TIM2EN_USED == STD_ON)
LpRCC_Reg->APB1ENR |= LpMcuClockConfigPtr->McuAPB1ENRConfgVal.TIM2EN;
#endif
#endif
}
static FUNC(void, MCU_CODE) Mcu_APB2Init(VAR( void, AUTOMATIC))
{
//McuClockConfigType* LpMcuClockConfigPtr;
//RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
//LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
/*McuAPB2ENRConfgVal*/
#ifdef MCU_TIM_USED
#if(TIM11EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.TIM11EN;
#endif
#if(TIM10EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.TIM10EN;
#endif
#if(TIM9EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.TIM9EN;
#endif
#endif
#if(SYSCFGEN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.SYSCFGEN;
#endif
#if(SPI1EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.SPI1EN;
#endif
#if(SPI1EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.SDIOEN;
#endif
#ifdef MCU_ADC_USED
#if(ADC3EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.ADC3EN;
#endif
#if(ADC3EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.ADC2EN;
#endif
#if(ADC3EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.ADC1EN;
#endif
#endif
#ifdef MCU_USART_USED
#if(USART6EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.USART6EN;
#endif
#if(USART5EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.USART1EN;
#endif
#endif
#ifdef MCU_TIM_USED
#if(TIM8EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.TIM8EN;
#endif
#if(TIM1EN_USED == STD_ON)
LpRCC_Reg->APB2ENR |= LpMcuClockConfigPtr->McuAPB2ENRConfgVal.TIM1EN;
#endif
#endif
}
/*
* Enable Peripherals
* Mcu_AHB1Init();
* Mcu_AHB2Init();
* Mcu_AHB3Init();
* Mcu_APB1Init();
* Mcu_APB2Init(); */
FUNC(void, MCU_CODE) Mcu_Init( Mcu_ConfigType* pConfigPtr)
{
Mcu_pConfigPtr = pConfigPtr;
Mcu_AllPeriphralReset();
Mcu_Pwr_Init();
Mcu_Flash_Init();
Mcu_AHB1Init();
Mcu_AHB2Init();
Mcu_AHB3Init();
Mcu_APB1Init();
Mcu_APB2Init();
}
/*
* Enable HSE or HSI
* Configure PLLCFGR
* Configure CFGR (if applicable)
* Configuring PLL */
FUNC( Std_ReturnType, MCU_CODE) Mcu_InitClock( VAR( uint8, AUTOMATIC) ClockSetting)
{
McuClockConfigType* LpMcuClockConfigPtr;
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.HSITRIM;
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.HSION;
/*HSI TRIM -> Set to Default value -->to be Implemented*/
#if(MCU_HSE_USED == STD_ON)
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.HSEON;
/* Wait for HSEON ready*/
while (!(LpRCC_Reg->CR & (1 << 17)));
#if(MCU_PLL_USED == STD_OFF)
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.SW;
#endif
#endif
/* Configure AHB PreScaler->HCLK,
* APB1 Prescaler->PCLK1,
* APB2 Prescaler->PCLK2*/
/*Clear SW, HPRE, PPRE1, and PPRE2 bits*/
RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2);
/*RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
RCC->CFGR |= RCC_CFGR_PPRE1_DIV8;
RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;*/
/* Configure AHB prescaler*/
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.HPRE;
/* Configure APB1 prescaler*/
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.PPRE1;
/*Configure APB2 prescaler*/
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.PPRE2;
LpRCC_Reg->PLLCFGR &= 0xFFFF8000;
#if(MCU_PLL_USED == STD_ON)
LpRCC_Reg->PLLCFGR |=LpMcuClockConfigPtr->McuPLLCFGRConfgVal.PLLM;
LpRCC_Reg->PLLCFGR |=LpMcuClockConfigPtr->McuPLLCFGRConfgVal.PLLN;
LpRCC_Reg->PLLCFGR |=LpMcuClockConfigPtr->McuPLLCFGRConfgVal.PLLP;
LpRCC_Reg->PLLCFGR |=LpMcuClockConfigPtr->McuPLLCFGRConfgVal.PLLSRC;
LpRCC_Reg->PLLCFGR |=LpMcuClockConfigPtr->McuPLLCFGRConfgVal.PLLQ;
#endif
#if(MCU_MCO2_USED == STD_ON)
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.MCO2;
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.MCO2PRE;
#endif
#if(MCU_MCO1_USED == STD_ON)
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.MCO1;
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.MCO1PRE;
#endif
#if(MCU_I2SSRC_USED == STD_ON)
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.I2SSRC;
#endif
#if(MCU_RTC_USED == STD_ON)
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.RTCPRE;
#endif
#if 0
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.PLLI2SRDYIE;
#endif
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.PLLRDYIE;
#endif
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.HSERDYIE;
#endif
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.HSIRDYIE;
#endif
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.LSERDYIE;
#endif
#if(MCU_RTC_PREHSE_1_USED == STD_ON)
LpRCC_Reg->CIR|=LpMcuClockConfigPtr->McuCIRConfgVal.LSIRDYIE;
#endif
#endif
#if(MCU_PLL_USED == STD_ON)
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.PLLON;
#endif
#if(MCU_PLLI2S_USED == STD_ON)
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.PLLI2SON;
#endif
#if 0
LpRCC_Reg->BDCR = LpMcuClockConfigPtr->McuBDCRConfgVal.BDRST;
LpRCC_Reg->BDCR = LpMcuClockConfigPtr->McuBDCRConfgVal.RTCEN;
LpRCC_Reg->BDCR = LpMcuClockConfigPtr->McuBDCRConfgVal.RTCSEL;
LpRCC_Reg->BDCR = LpMcuClockConfigPtr->McuBDCRConfgVal.LSEBYP;
LpRCC_Reg->BDCR = LpMcuClockConfigPtr->McuBDCRConfgVal.LSEON;
LpRCC_Reg->CSR = LpMcuClockConfigPtr->McuCSRConfgVal.LSION;
LpRCC_Reg->SSCGR = LpMcuClockConfigPtr->McuSSCGRConfgVal.SSCGEN;
LpRCC_Reg->SSCGR = LpMcuClockConfigPtr->McuSSCGRConfgVal.SPREADSEL;
LpRCC_Reg->SSCGR = LpMcuClockConfigPtr->McuSSCGRConfgVal.INCSTEP;
LpRCC_Reg->SSCGR = LpMcuClockConfigPtr->McuSSCGRConfgVal.MODPER;
LpRCC_Reg->PLLI2SCFGR = LpMcuClockConfigPtr->McuPLLI2SCFGRConfgVal.PLLI2SR;
LpRCC_Reg->PLLI2SCFGR = LpMcuClockConfigPtr->McuPLLI2SCFGRConfgVal.PLLI2SN;
#endif
return E_OK;
}
/* Get Status of PLL after turning PLL to ON in CR */
FUNC( Std_ReturnType, MCU_CODE) Mcu_GetPllStatus(VAR( void, AUTOMATIC))
{
static Mcu_PllStatusType status = MCU_PLL_STATUS_UNDEFINED;
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
#if(MCU_PLL_USED == STD_OFF)
status = MCU_PLL_LOCKED;
#else
// Check if PLL is ready
if(LpRCC_Reg->CR & (1<<25)) {
status = MCU_PLL_LOCKED;
} else {
status = MCU_PLL_UNLOCKED;
}
return status;
#endif
}
/* Distribute HSI/ HSE/ PLL via CFGR->SW1_SW0*/
FUNC( Std_ReturnType, MCU_CODE) Mcu_DistributePllClock(VAR( void, AUTOMATIC))
{
McuClockConfigType* LpMcuClockConfigPtr;
RCC_RegTypedef* LpRCC_Reg = RCC_REGADDRESS;
LpMcuClockConfigPtr = Mcu_pConfigPtr->McuClockConfgSet;
#if(MCU_CSS_USED == STD_ON)
LpRCC_Reg->CR |= LpMcuClockConfigPtr->McuCRConfgVal.CSSON;
#endif
LpRCC_Reg->CFGR |= LpMcuClockConfigPtr->McuCFGRConfgVal.SW;
return E_OK;
}
this is main.c file.
#include "Mcu.h"
#include "stm32f4xx.h"
#include "Mcu_RegTypes.h"
#include "Mcu_InternalTypes.h"
#define LED_PIN 12 // Assuming the LED is connected to GPIO Port D, Pin 12
/*void delay(uint32_t milliseconds)
{
// Assuming the system clock is running at 16MHz
uint32_t ticks = milliseconds * 16000;
while (ticks--);
}
*/
void TIM2_IRQHandler()
{
if (TIM2->SR & TIM_SR_UIF) // Check if update interrupt flag is set
{
TIM2->SR &= ~TIM_SR_UIF; // Clear the update interrupt flag
// Toggle pin 12
GPIOD->ODR ^= GPIO_ODR_OD12;
}
}
int main()
{
Mcu_Init(&Mcu_ConfigSet[0]);
Mcu_InitClock(0);
while (MCU_PLL_LOCKED != Mcu_GetPllStatus());
Mcu_DistributePllClock();
// Configure PD12 as output
GPIOD->MODER |= GPIO_MODER_MODER12_0;
// Configure output type as push-pull
GPIOD->OTYPER &= ~GPIO_OTYPER_OT_12; // Push-pull output
// Enable TIM2 clock
//RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
// Already enable through config.c
// Set TIM2 prescaler to get 1ms ticks assuming 16MHz clock
TIM2->PSC = 16000 - 1;
// Set TIM2 auto-reload value to get a 5s period
TIM2->ARR = 5000 - 1;
// Enable TIM2 update interrupt
TIM2->DIER |= TIM_DIER_UIE;
// Set TIM2 priority
NVIC_SetPriority(28, 0x01);
// Enable TIM2 interrupt in NVIC
NVIC_EnableIRQ(28);
NVIC_SetVector(28,(uint32)&TIM2_IRQHandler);
// Start TIM2
TIM2->CR1 |= TIM_CR1_CEN;
while (1)
{
// Main program loop, no need for delay here
}
}
2024-05-03 09:50 AM - edited 2024-05-03 02:02 PM
Well, so what is the problem? and I don't see any CAN code in your main!
2024-05-03 10:15 AM
Don't use the RMW form here
TIM2->SR &= ~TIM_SR_UIF; // Clear the update interrupt flag
Just write the mask, it's safer and lacks secondary effects
TIM2->SR = ~TIM_SR_UIF; // Clear the update interrupt flag