Skip to content

Commit

Permalink
auto reset the MCU when the raspberrypi CPU is reseted by the key
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbearishappy committed Jun 12, 2021
1 parent 47ef36e commit 4c4f7b8
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 5 deletions.
1 change: 1 addition & 0 deletions extras/reTerminal/STM32G030F6_R2/Core/Inc/stm32g0xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel1_IRQHandler(void);
void TIM14_IRQHandler(void);
void I2C1_IRQHandler(void);
/* USER CODE BEGIN EFP */

Expand Down
51 changes: 51 additions & 0 deletions extras/reTerminal/STM32G030F6_R2/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ I2C_HandleTypeDef hi2c1;
I2C_HandleTypeDef hi2c2;

TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim14;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
uint16_t AdcVal[ADC_CH_MAX] = { 0 };
uint32_t i2c_int_cnt = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand All @@ -68,6 +70,7 @@ static void MX_I2C1_Init(void);
static void MX_I2C2_Init(void);
static void MX_TIM3_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM14_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */
Expand Down Expand Up @@ -109,7 +112,10 @@ int main(void)
MX_I2C2_Init();
MX_TIM3_Init();
MX_ADC1_Init();
MX_TIM14_Init();
/* USER CODE BEGIN 2 */
__HAL_TIM_CLEAR_FLAG(&htim14, TIM_SR_UIF);
HAL_TIM_Base_Start_IT(&htim14);
DBG_PRINT("ReTerminal stm32!\n");
I2C_Slave_Init(&hi2c1);
TP_Init(&hi2c2);
Expand Down Expand Up @@ -390,6 +396,37 @@ static void MX_TIM3_Init(void)

}

/**
* @brief TIM14 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM14_Init(void)
{

/* USER CODE BEGIN TIM14_Init 0 */

/* USER CODE END TIM14_Init 0 */

/* USER CODE BEGIN TIM14_Init 1 */

/* USER CODE END TIM14_Init 1 */
htim14.Instance = TIM14;
htim14.Init.Prescaler = 39999;
htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
htim14.Init.Period = 29999;
htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM14_Init 2 */

/* USER CODE END TIM14_Init 2 */

}

/**
* @brief USART2 Initialization Function
* @param None
Expand Down Expand Up @@ -475,6 +512,20 @@ static void MX_GPIO_Init(void)
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == (&htim14)) {
if(i2c_int_cnt > 30){
//DBG_PRINT("if %d %d\n", TIM14->ARR, i2c_int_cnt);
TIM14->ARR = 800;
} else {
//DBG_PRINT("else %d %d\n", TIM14->ARR, i2c_int_cnt);
TIM14->ARR = 9999;
NVIC_SystemReset();
}
i2c_int_cnt = 0;
}
}
/* USER CODE END 4 */

/**
Expand Down
50 changes: 50 additions & 0 deletions extras/reTerminal/STM32G030F6_R2/Core/Src/stm32g0xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,31 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)

}

/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM14)
{
/* USER CODE BEGIN TIM14_MspInit 0 */

/* USER CODE END TIM14_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM14_CLK_ENABLE();
/* TIM14 interrupt Init */
HAL_NVIC_SetPriority(TIM14_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM14_IRQn);
/* USER CODE BEGIN TIM14_MspInit 1 */

/* USER CODE END TIM14_MspInit 1 */
}

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
Expand Down Expand Up @@ -356,6 +381,31 @@ void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)

}

/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM14)
{
/* USER CODE BEGIN TIM14_MspDeInit 0 */

/* USER CODE END TIM14_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM14_CLK_DISABLE();

/* TIM14 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM14_IRQn);
/* USER CODE BEGIN TIM14_MspDeInit 1 */

/* USER CODE END TIM14_MspDeInit 1 */
}

}

/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
Expand Down
15 changes: 15 additions & 0 deletions extras/reTerminal/STM32G030F6_R2/Core/Src/stm32g0xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
extern I2C_HandleTypeDef hi2c1;
extern TIM_HandleTypeDef htim14;
/* USER CODE BEGIN EV */

/* USER CODE END EV */
Expand Down Expand Up @@ -155,6 +156,20 @@ void DMA1_Channel1_IRQHandler(void)
/* USER CODE END DMA1_Channel1_IRQn 1 */
}

/**
* @brief This function handles TIM14 global interrupt.
*/
void TIM14_IRQHandler(void)
{
/* USER CODE BEGIN TIM14_IRQn 0 */

/* USER CODE END TIM14_IRQn 0 */
HAL_TIM_IRQHandler(&htim14);
/* USER CODE BEGIN TIM14_IRQn 1 */

/* USER CODE END TIM14_IRQn 1 */
}

/**
* @brief This function handles I2C1 event global interrupt / I2C1 wake-up interrupt through EXTI line 23.
*/
Expand Down
6 changes: 4 additions & 2 deletions extras/reTerminal/STM32G030F6_R2/Core/User/i2c_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ static uint8_t TxCnt = 0;
static uint8_t TxBuf[TP_BUF_SIZE] = { 0 };
static uint8_t TxTotal = 0;
static uint8_t HandleFlag = 0;

extern uint32_t i2c_int_cnt;
void I2C_Slave_Process(void)
{
if (!HandleFlag) return;
HandleFlag = 0;

MDBG("RxCnt=%d\n", RxCnt);
if (RxCnt == 1) { // set addr
if (RxBuf[0] >= 0x80) {
Expand Down Expand Up @@ -147,6 +147,8 @@ void I2C_Slave_ISR(I2C_HandleTypeDef *hi2c)
{
uint32_t isr = hi2c->Instance->ISR;

i2c_int_cnt++;

MDBG("[%d]0x%x\n", cnt++, isr);
if (isr & I2C_FLAG_ALERT) {
MDBG("ALERT\n");
Expand Down
15 changes: 12 additions & 3 deletions extras/reTerminal/STM32G030F6_R2/STM32G030F6_R2.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RCC.CortexFreq_Value=64000000
I2C1.I2C_Speed_Mode=I2C_Fast_Plus
ProjectManager.KeepUserCode=true
Mcu.UserName=STM32G030F6Px
TIM14.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_DISABLE
ADC1.ContinuousConvMode=ENABLE
Dma.ADC1.0.Polarity=HAL_DMAMUX_REQ_GEN_RISING
PA12\ [PA10].Mode=I2C
Expand Down Expand Up @@ -38,7 +39,7 @@ SH.S_TIM3_CH2.ConfNb=1
Mcu.ThirdPartyNb=0
RCC.HCLKFreq_Value=64000000
PA12\ [PA10].Signal=I2C2_SDA
Mcu.IPNb=9
Mcu.IPNb=10
ProjectManager.PreviousToolchain=STM32CubeIDE
I2C2.I2C_Speed_Mode=I2C_Fast
TIM3.Period=PERIOD_VALUE
Expand Down Expand Up @@ -68,6 +69,7 @@ PA11\ [PA9].GPIOParameters=GPIO_Speed,GPIO_PuPdOD,PinState,GPIO_Mode
PA4.Locked=true
USART2.VirtualMode-Asynchronous=VM_ASYNC
TIM3.OCFastMode_PWM-PWM\ Generation2\ CH2=TIM_OCFAST_ENABLE
TIM14.Period=29999
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true
PA4.GPIO_Speed=GPIO_SPEED_FREQ_VERY_HIGH
PA12\ [PA10].GPIOParameters=GPIO_Speed,GPIO_PuPdOD,PinState,GPIO_Mode
Expand Down Expand Up @@ -113,7 +115,9 @@ PA5.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label
RCC.PLLPoutputFreq_Value=64000000
TIM3.Pulse-PWM\ Generation2\ CH2=PULSE_DEF_VALUE
I2C2.GeneralCallMode=I2C_GENERALCALL_DISABLE
VP_TIM14_VS_ClockSourceINT.Mode=Enable_Timer
PA6.GPIO_Speed=GPIO_SPEED_FREQ_HIGH
TIM14.IPParameters=Prescaler,Period,AutoReloadPreload
I2C1.IPParametersWithoutCheck=OwnAddress
Dma.Request0=ADC1
PA4.GPIO_Label=TP_INT
Expand All @@ -123,13 +127,15 @@ PA3.Signal=USART2_RX
PB7.GPIO_Speed=GPIO_SPEED_FREQ_HIGH
PA2.Locked=true
PA6.GPIOParameters=GPIO_Speed,PinState,GPIO_PuPd,GPIO_Label
NVIC.TIM14_IRQn=true\:0\:0\:false\:false\:true\:true\:true
ProjectManager.ProjectFileName=STM32G030F6_R2.ioc
Dma.ADC1.0.Instance=DMA1_Channel1
ADC1.Rank-0\#ChannelRegularConversion=1
PA11\ [PA9].GPIO_Speed=GPIO_SPEED_FREQ_HIGH
PA12\ [PA10].GPIO_Speed=GPIO_SPEED_FREQ_HIGH
I2C2.I2C_Coeff_DF=0
Mcu.PinsNb=16
Mcu.PinsNb=17
VP_TIM14_VS_ClockSourceINT.Signal=TIM14_VS_ClockSourceINT
ProjectManager.NoMain=false
Dma.ADC1.0.SyncRequestNumber=1
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master,SelectedChannel,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,NbrOfConversion,ContinuousConvMode,DMAContinuousRequests
Expand Down Expand Up @@ -185,10 +191,12 @@ RCC.IPParameters=ADCFreq_Value,AHBFreq_Value,APBFreq_Value,APBTimFreq_Value,Cort
ProjectManager.AskForMigrate=true
PA11\ [PA9].Signal=I2C2_SCL
Mcu.Name=STM32G030F6Px
TIM14.Prescaler=39999
PA2.Signal=USART2_TX
ProjectManager.UnderRoot=true
I2C1.OwnAddress=I2C_SLAVE_ADDR
Mcu.IP8=USART2
Mcu.IP8=TIM14
Mcu.IP9=USART2
Mcu.IP6=SYS
Mcu.IP7=TIM3
ProjectManager.CoupleFile=false
Expand All @@ -213,6 +221,7 @@ Dma.ADC1.0.MemInc=DMA_MINC_ENABLE
ProjectManager.HeapSize=0x200
Mcu.Pin15=VP_SYS_VS_Systick
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
Mcu.Pin16=VP_TIM14_VS_ClockSourceINT
Mcu.Pin13=PA14-BOOT0
Mcu.Pin14=PB6
ProjectManager.ComputerToolchain=false
Expand Down

0 comments on commit 4c4f7b8

Please sign in to comment.