Browse Source

Work. Need code cleanup.

Vladimir N. Shilov 4 months ago
parent
commit
ac618ca91b
4 changed files with 35 additions and 29 deletions
  1. 1 1
      inc/stm32g0xx_it.h
  2. 22 15
      src/board.c
  3. 3 3
      src/main.c
  4. 9 10
      src/stm32g0xx_it.c

+ 1 - 1
inc/stm32g0xx_it.h

@@ -34,7 +34,7 @@ void HardFault_Handler(void);
 void SVC_Handler(void);
 void SVC_Handler(void);
 void PendSV_Handler(void);
 void PendSV_Handler(void);
 void ADC1_IRQHandler(void);
 void ADC1_IRQHandler(void);
-void TIM1_BRK_UP_TRG_COM_IRQHandler(void);
+//void TIM1_BRK_UP_TRG_COM_IRQHandler(void);
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }

+ 22 - 15
src/board.c

@@ -44,10 +44,6 @@ void Board_Init(void)
 
 
   /* Initialize all configured peripherals */
   /* Initialize all configured peripherals */
   GPIO_Init();
   GPIO_Init();
-  //GPIO_SetPinMode(GPIOA, GPIO_PIN_8, GPIO_MODE_OUT);
-  //GPIO_SetPinOutputType(GPIOA, GPIO_PIN_8, GPIO_OTYPE_PP);
-  //GPIO_SetPinSpeed(GPIOA, GPIO_PIN_8, GPIO_OSPEED_VH);
-  //GPIO_SetPinPull(GPIOA, GPIO_PIN_8, GPIO_PUPDR_NO);
 
 
   ADC_Init();
   ADC_Init();
 
 
@@ -97,6 +93,12 @@ static void GPIO_Init(void)
   GPIO_SetPinSpeed(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OSPEED_LW);
   GPIO_SetPinSpeed(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OSPEED_LW);
   GPIO_SetPinMode(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_MODE_AFF);
   GPIO_SetPinMode(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_MODE_AFF);
 
 
+  GPIO_SetAFPin_0_7(GPIOB, GPIO_PIN_3, GPIO_AF_1);
+  GPIO_SetPinOutputType(GPIOB, GPIO_PIN_3, GPIO_OTYPE_PP);
+  GPIO_SetPinPull(GPIOB, GPIO_PIN_3, GPIO_PUPDR_NO);
+  GPIO_SetPinSpeed(GPIOB, GPIO_PIN_3, GPIO_OSPEED_LW);
+  GPIO_SetPinMode(GPIOB, GPIO_PIN_3, GPIO_MODE_AFF);
+
   /* Photo_Pin: analog in, pull none */
   /* Photo_Pin: analog in, pull none */
   GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
   GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
   GPIO_SetPinMode(Photo_Port, Photo_Pin, GPIO_MODE_ANL);
   GPIO_SetPinMode(Photo_Port, Photo_Pin, GPIO_MODE_ANL);
@@ -134,16 +136,16 @@ static void ADC_Init(void)
   ADC1->ISR |= ADC_ISR_CCRDY;
   ADC1->ISR |= ADC_ISR_CCRDY;
 
 
   /* External trigger - Tim3 */
   /* External trigger - Tim3 */
-  //ADC1->CFGR1 |= (ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_EXTEN_0 | ADC_CFGR1_OVRMOD);
-  ADC1->CFGR1 = ADC_CFGR1_OVRMOD;
+  //ADC1->CFGR1 |= (ADC_CFGR1_EXTEN_0 | ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_OVRMOD);
+  ADC1->CFGR1 = (ADC_CFGR1_EXTEN_0 |ADC_CFGR1_OVRMOD);
 
 
-   /* Enable ADC internal voltage regulator */
-   ADC1->CR |= ADC_CR_ADVREGEN; // ???
+  /* Enable ADC internal voltage regulator */
+  ADC1->CR |= ADC_CR_ADVREGEN; // ???
 
 
   /** Configure Regular Channel */
   /** Configure Regular Channel */
   ADC1->CHSELR = ADC_CHSELR_CHSEL11;
   ADC1->CHSELR = ADC_CHSELR_CHSEL11;
 
 
-   /* Poll for ADC channel configuration ready */
+  /* Poll for ADC channel configuration ready */
   while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
   while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
   /* Clear flag ADC channel configuration ready */
   /* Clear flag ADC channel configuration ready */
   ADC1->ISR |= ADC_ISR_CCRDY;
   ADC1->ISR |= ADC_ISR_CCRDY;
@@ -171,25 +173,30 @@ static void ADC_Init(void)
 static void TIM1_Init(void)
 static void TIM1_Init(void)
 {
 {
   /* TIM1 interrupt Init */
   /* TIM1 interrupt Init */
-  NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0);
-  NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
+  //NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0);
+  //NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
   /* target clock */
   /* target clock */
   TIM1->PSC = TIM1_PSC; // prescaler
   TIM1->PSC = TIM1_PSC; // prescaler
   TIM1->ARR = TIM1_ARR; // auto reload value
   TIM1->ARR = TIM1_ARR; // auto reload value
   TIM1->CR1 = TIM_CR1_ARPE;
   TIM1->CR1 = TIM_CR1_ARPE;
   // initial pwm value
   // initial pwm value
   TIM1->CCR1 = SERVO_INIT_VAL;
   TIM1->CCR1 = SERVO_INIT_VAL;
+  TIM1->CCR2 = SERVO_INIT_VAL;
   TIM1->CCR4 = SERVO_INIT_VAL;
   TIM1->CCR4 = SERVO_INIT_VAL;
   // pwm mode 1 for chanels
   // pwm mode 1 for chanels
   TIM1->CCMR1 = (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
   TIM1->CCMR1 = (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
+  TIM1->CCMR1 |= (TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2PE);
   TIM1->CCMR2 = (TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE);
   TIM1->CCMR2 = (TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE);
   //TIM1->SR |= TIM_SR_UIF;
   //TIM1->SR |= TIM_SR_UIF;
   TIM1->BDTR = TIM_BDTR_MOE; // enable main output
   TIM1->BDTR = TIM_BDTR_MOE; // enable main output
   TIM1->EGR = TIM_EGR_UG; // force timer update
   TIM1->EGR = TIM_EGR_UG; // force timer update
   /* TIM1 CC_EnableChannel */
   /* TIM1 CC_EnableChannel */
   TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC4E | TIM_CCER_CC4P);
   TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC4E | TIM_CCER_CC4P);
+  TIM1->CCER |= (TIM_CCER_CC2E | TIM_CCER_CC2P);
+  /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
+  TIM1->CR2 |= TIM_CR2_MMS2_1; // update event
   /* Enable interrupts */
   /* Enable interrupts */
-  TIM1->DIER = TIM_DIER_UIE;
+  //TIM1->DIER = TIM_DIER_UIE;
   /* TIM_EnableCounter */
   /* TIM_EnableCounter */
   TIM1->CR1 |= TIM_CR1_CEN;
   TIM1->CR1 |= TIM_CR1_CEN;
 }
 }
@@ -204,11 +211,11 @@ static void TIM3_Init(void)
   /* target clock */
   /* target clock */
   TIM3->PSC = TIM3_PSC; // prescaler
   TIM3->PSC = TIM3_PSC; // prescaler
   TIM3->ARR = TIM3_ARR; // auto reload value
   TIM3->ARR = TIM3_ARR; // auto reload value
-  TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_DIR;
+  TIM3->CR1 = (TIM_CR1_ARPE | TIM_CR1_DIR);
   // launch timer
   // launch timer
-  TIM3->EGR = TIM_EGR_UG; // force timer update
+  TIM3->EGR = TIM_EGR_TG | TIM_EGR_UG; // force timer update
   /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
   /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
-  TIM3->CR2 |= TIM_CR2_MMS2_2; // update event
+  TIM3->CR2 |= TIM_CR2_MMS2_1; // update event
   /* TIM3 enable */
   /* TIM3 enable */
   TIM3->CR1 |= TIM_CR1_CEN;
   TIM3->CR1 |= TIM_CR1_CEN;
 }
 }

+ 3 - 3
src/main.c

@@ -47,7 +47,7 @@ int main(void)
 {
 {
   /* Initialize onBoard Hardware */
   /* Initialize onBoard Hardware */
   Board_Init();
   Board_Init();
-  uart_init(115200);
+  //uart_init(115200);
   uart_puts("\033[2J\033[1;1H"); // clear screen
   uart_puts("\033[2J\033[1;1H"); // clear screen
   xdev_out(uart_putc);
   xdev_out(uart_putc);
   xputs("Skid\n");
   xputs("Skid\n");
@@ -116,7 +116,7 @@ int main(void)
 
 
 static void servo_open(void) {
 static void servo_open(void) {
   int i;
   int i;
-  for (i=SERVO_INIT_VAL; i <= SERVO_OPEN_VAL; i+=20) {
+  for (i=SERVO_INIT_VAL; i <= SERVO_OPEN_VAL; i+=50) {
     TIM1->CCR1 = i;
     TIM1->CCR1 = i;
     TIM1->CCR4 = i;
     TIM1->CCR4 = i;
     delay_ms(40);
     delay_ms(40);
@@ -125,7 +125,7 @@ static void servo_open(void) {
 
 
 static void servo_close(void) {
 static void servo_close(void) {
   int i;
   int i;
-  for (i=SERVO_OPEN_VAL; i >= SERVO_INIT_VAL; i-=20) {
+  for (i=SERVO_OPEN_VAL; i >= SERVO_INIT_VAL; i-=50) {
     TIM1->CCR1 = i;
     TIM1->CCR1 = i;
     TIM1->CCR4 = i;
     TIM1->CCR4 = i;
     delay_ms(40);
     delay_ms(40);

+ 9 - 10
src/stm32g0xx_it.c

@@ -88,19 +88,18 @@ void PendSV_Handler(void)
   */
   */
 void ADC1_IRQHandler(void)
 void ADC1_IRQHandler(void)
 {
 {
+  static int i=8;
+  static uint32_t val=0;
   /* read ADC value */
   /* read ADC value */
-  photo_Value = ADC1->DR;
+  val += ADC1->DR;
+  i --;
+  if (i == 0) {
+    i = 8;
+    photo_Value = (val / 8);
+    val = 0;
+  }
   /* Clear the EOC flag */
   /* Clear the EOC flag */
   ADC1->ISR |= ADC_ISR_EOC;
   ADC1->ISR |= ADC_ISR_EOC;
 }
 }
 
 
-void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
-{
-  if ((TIM1->SR & TIM_SR_UIF) != 0) {
-    /* Update interrupt flag */
-    TIM1->SR &= ~TIM_SR_UIF;
-  }
-  ADC1->CR |= ADC_CR_ADSTART;
-}
-
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/