Rev 47 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mjames | 1 | /* |
2 | * misc.c |
||
3 | * |
||
4 | * Created on: 21 Sep 2016 |
||
5 | * Author: Mike |
||
6 | */ |
||
37 | mjames | 7 | #include "stm32f1xx_hal.h" |
9 | mjames | 8 | #include "misc.h" |
9 | #include "main.h" |
||
3 | mjames | 10 | |
29 | mjames | 11 | // this is set if there is a timer timeout interrupt |
31 | mjames | 12 | unsigned char volatile periodPulse = 0; |
29 | mjames | 13 | |
48 | mjames | 14 | // this is set when timer 3 was triggerd |
39 | mjames | 15 | unsigned char volatile tim3triggered = 0; |
16 | |||
48 | mjames | 17 | // this is exported |
18 | void |
||
45 | mjames | 19 | triggerTim3(void) |
29 | mjames | 20 | { |
45 | mjames | 21 | htim3.Instance->CNT = 0; |
22 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
||
29 | mjames | 23 | } |
24 | |||
37 | mjames | 25 | // timer variable shared between TIM3 and TIM4 handler. |
26 | static char chtTimer = 0; |
||
8 | mjames | 27 | |
45 | mjames | 28 | void TIM3_IRQHandler(void) |
31 | mjames | 29 | { |
30 | if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE)) |
||
45 | mjames | 31 | { |
32 | __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE); |
||
33 | |||
34 | tim3triggered = 0; |
||
35 | if (chtTimer >= 3) // every 300mS |
||
31 | mjames | 36 | { |
45 | mjames | 37 | chtTimer = 0; |
6 | mjames | 38 | |
45 | mjames | 39 | resetTempCS(); |
40 | for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++) |
||
41 | { |
||
42 | uint8_t buffer[2]; |
||
41 | mjames | 43 | |
45 | mjames | 44 | nextTempCS(); |
45 | HAL_SPI_Receive(&hspi1, buffer, 2, 2); |
||
6 | mjames | 46 | |
45 | mjames | 47 | uint16_t obs = (buffer[0] << 8) | buffer[1]; |
6 | mjames | 48 | |
45 | mjames | 49 | // good observation if the status bit is clear, and the reading is less than 1023 |
6 | mjames | 50 | |
45 | mjames | 51 | uint16_t temp_c = obs >> 5; |
6 | mjames | 52 | |
45 | mjames | 53 | uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250); |
6 | mjames | 54 | |
45 | mjames | 55 | if (good) |
56 | { |
||
57 | Temp_Observations[instance] = temp_c; |
||
58 | } |
||
59 | } |
||
60 | nextTempCS(); // clock CS one more time to deselect all chips |
||
31 | mjames | 61 | } |
45 | mjames | 62 | } |
6 | mjames | 63 | } |
64 | |||
37 | mjames | 65 | // 100mS periodic sampler handler |
45 | mjames | 66 | void TIM4_IRQHandler(void) |
37 | mjames | 67 | { |
68 | static char blink = 0; |
||
69 | if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE)) |
||
45 | mjames | 70 | { |
71 | __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE); |
||
37 | mjames | 72 | |
45 | mjames | 73 | blink = !blink; |
74 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, |
||
75 | blink ? GPIO_PIN_SET : GPIO_PIN_RESET); |
||
37 | mjames | 76 | |
45 | mjames | 77 | TimerFlag = 1; |
78 | if (NoSerialInCTR < 5) |
||
79 | { |
||
80 | NoSerialInCTR++; |
||
81 | if (NoSerialInCTR == 5) |
||
82 | { |
||
83 | NoSerialIn = 1; |
||
84 | } |
||
85 | } |
||
37 | mjames | 86 | |
45 | mjames | 87 | if (periodPulse == 1) |
88 | { |
||
89 | triggerTim3(); |
||
37 | mjames | 90 | } |
45 | mjames | 91 | // indicate that timer 4 firing is owning the timer 3 trigger |
92 | periodPulse = 1; |
||
37 | mjames | 93 | |
45 | mjames | 94 | chtTimer++; |
95 | } |
||
37 | mjames | 96 | } |
97 | |||
45 | mjames | 98 | void resetSerialTimeout(void) |
31 | mjames | 99 | { |
45 | mjames | 100 | __disable_irq(); |
31 | mjames | 101 | NoSerialInCTR = 0; |
102 | NoSerialIn = 0; |
||
45 | mjames | 103 | __enable_irq(); |
6 | mjames | 104 | } |