Subversion Repositories EngineBay2

Rev

Rev 28 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * misc.c
  3.  *
  4.  *  Created on: 21 Sep 2016
  5.  *      Author: Mike
  6.  */
  7. #include "stm32l1xx_hal.h"
  8. #include "misc.h"
  9. #include "main.h"
  10.  
  11.  
  12. extern TIM_HandleTypeDef htim2;
  13. extern TIM_HandleTypeDef htim3;
  14. extern TIM_HandleTypeDef htim6;
  15.  
  16.  
  17. unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
  18. unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
  19. unsigned volatile long RPM_Count; // incremented every reading
  20.  
  21.  
  22. // this is set if there is an ignition pulse interrupt
  23. unsigned char volatile ignitionPulse = 0;
  24. // this is set if there is a timer timeout interrupt
  25. unsigned char volatile periodPulse  = 0;
  26.  
  27. static void triggerTim3(void)
  28. {
  29.    htim3.Instance->CNT = 0;
  30.    htim3.Instance->CR1|= TIM_CR1_CEN;
  31.  
  32. }
  33.  
  34. void TIM2_IRQHandler(void)
  35. {
  36.         if(__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
  37.         {
  38.                   __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1) ;
  39.                         RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1);
  40.                         RPM_Level[RPM_Count]  =
  41.                                 HAL_GPIO_ReadPin(CB_Pulse_GPIO_Port, CB_Pulse_Pin);
  42.                         RPM_Count = (RPM_Count+1) % RPM_SAMPLES ;
  43.  
  44.                         if(periodPulse == 1)
  45.                           {
  46.                             periodPulse = 0;
  47.                             triggerTim3();
  48.                           }
  49.                 ignitionPulse = 1;
  50.         }
  51. }
  52.  
  53.  
  54. char blink = 0;
  55. // 100mS periodic sampler handler
  56. void  TIM6_IRQHandler(void)
  57. {
  58.         if(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE))
  59.         {
  60.           __HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE) ;
  61.  
  62.           blink = !blink;
  63.           HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  64.  
  65.           TimerFlag = 1;
  66.           if (NoSerialInCTR < 5) {
  67.             NoSerialInCTR++;
  68.                 if (NoSerialInCTR == 5) {
  69.                   NoSerialIn = 1;
  70.                 }
  71.           }
  72.  
  73.           if(ignitionPulse == 0)
  74.             triggerTim3();
  75.  
  76.           periodPulse = 1;
  77.         }
  78.  
  79. }
  80.  
  81. void resetSerialTimeout(void)
  82. {
  83.   __disable_irq();
  84.   NoSerialInCTR= 0 ;
  85.   NoSerialIn = 0;
  86.   __enable_irq();
  87. }
  88.  
  89. #if OLD_ENGINEBAY
  90. //-----------------------------------------------------------
  91. // This is the 10Hz timer
  92.  
  93. void interrupt TimerISR1(void) {
  94.         TimerFlag = 1;
  95.         if (NoSerialInCTR < 5) {
  96.                 NoSerialInCTR++;
  97.                 if (NoSerialInCTR == 5) {
  98.                         NoSerialIn = 1;
  99.                 }
  100.         }
  101. }
  102. //-----------------------------------------------------------
  103.  
  104.  
  105. volatile unsigned long RPM_Time[RPM_LIMIT];
  106. volatile unsigned long RPM_Count; // incremented every reading
  107.  
  108. void TimerISR0(void) {
  109.         // check the listing
  110.         asm volatile ("STMDB    SP!, {r0-r4} ");
  111. // sort out the captures
  112.         int stat = REG(TIMER0_IR);
  113.         if (stat & (1 << 4)) //CR0
  114.                         {
  115.                 RPM_Time[RPM_Count++] = REG(TIMER0_CR0);
  116.                 if (RPM_Count == RPM_LIMIT) {
  117.                         RPM_Count = 0;
  118.                 }
  119.         }
  120.         if (stat & (1 << 5)) //CR1
  121.                         {
  122. //      TDC_Time = REG(TIMER0_CR1);
  123. //      TDC_Count++;
  124.         }
  125.  
  126.         REG(TIMER0_IR) = stat; // clear the interrupts just handled.
  127.         REG(VICVectAddr) = 0; // reset the VIC
  128.         asm volatile ("LDMIA    SP!, {r0-r4} ");
  129.         asm volatile ("SUBS PC,lr,#4");
  130. }
  131.  
  132. //-----------------------------------------------------------
  133. // UART management
  134. // serial counter
  135. #define THRE (1<<6)
  136. #define RDR  (1<<0)
  137. #define BUFFSIZ 256
  138.  
  139. volatile char txbuff[BUFFSIZ];
  140. volatile int txptr = 0;
  141. volatile int txcnt = 0;
  142.  
  143. volatile char rxbuff[BUFFSIZ];
  144. volatile int rxptr = 0;
  145. volatile int rxcnt = 0;
  146.  
  147. void Uart0ISR(void) __attribute__((naked));
  148. void Uart0ISR(void) __attribute__((noinline));
  149. void Uart0ISR(void) {
  150.         asm volatile ("STMDB    SP!, {r0-r3} ");
  151.         int stat;
  152.         while (!((stat = REG(UART0_IIR)) & 1)) { // LSbit is inactive
  153.                 switch (stat & 0x0E) {
  154.                 case 4:
  155.                 case 12: {
  156.                         char c = REG(UART0_RBR);
  157.  
  158.                         NoSerialIn = NoSerialInCTR = 0;
  159.  
  160.                         if (rxcnt < (BUFFSIZ - 1)) {
  161.                                 rxbuff[(rxptr + rxcnt) % BUFFSIZ] = c;
  162.                                 rxcnt++;
  163.                         }
  164.                 }
  165.                         break;
  166.                 case 2: {
  167.                         if (txcnt > 0) {
  168.                                 REG(UART0_THR) = txbuff[txptr];
  169.                                 txcnt--;
  170.                                 txptr++;
  171.                                 txptr %= BUFFSIZ;
  172.                         } else {
  173.                                 REG(UART0_IER) &= ~(1 << 1); // disable TX
  174.                         }
  175.                 }
  176.                         break;
  177.                 }
  178.         }
  179.         REG(VICVectAddr) = 0; // reset the VIC
  180.         asm volatile ("LDMIA    SP!, {r0-r3} ");
  181.         asm volatile ("SUBS PC,lr,#4");
  182. }
  183.  
  184. int my_putchar(int c) {
  185.         int rc = 0;
  186.          {
  187.                 STI;
  188.                 if (!(REG(UART0_IER) & (1 << 1))) {
  189.                         REG(UART0_IER) |= (1 << 1);
  190.                     REG(UART0_THR) = c;
  191.                         rc=1;
  192.                 } else {
  193.  
  194.                         if (txcnt < (BUFFSIZ - 1)) {
  195.                                 txbuff[(txptr + txcnt) % BUFFSIZ] = c;
  196.                                 txcnt++;
  197.                                 rc = 1;
  198.                         }
  199.                 }
  200.                 CLI;
  201.         }
  202.         return rc;
  203. }
  204.  
  205. int my_getchar(void) {
  206.  
  207.         int c = -1;
  208.         STI;
  209.         if (rxcnt) {
  210.                 c = rxbuff[rxptr];
  211.                 rxcnt--;
  212.                 rxptr++;
  213.                 rxptr %= BUFFSIZ;
  214.         }
  215.         CLI;
  216.         return c;
  217. }
  218.  
  219.  
  220.  
  221. #endif
  222.