source: trunk/firmware/SES/Src/precharge.c@ 8

Last change on this file since 8 was 7, checked in by f.jahn, 2 years ago

12V variant is working good.

File size: 4.1 KB
Line 
1#include <stdio.h>
2
3#include "main.h"
4#include "dac.h"
5#include "dma.h"
6#include "precharge.h"
7
8#define DELAYS_NUM 7U
9
10extern void (*MOSFETS_Management)(void);
11extern void DoNothing(void);
12
13static void (*WhereToGoAfterPrecharge)(void);
14
15//------------------------------------------------------------------------------
16
17void SetReturnFunction(void (*WhereToReturnControl)(void))
18{
19 if (WhereToReturnControl != NULL) WhereToGoAfterPrecharge = WhereToReturnControl;
20}
21
22//------------------------------------------------------------------------------
23
24#ifdef USE_RAM_FUNC
25__RAM_FUNC void PreChargeStage(void)
26#else
27 void PreChargeStage(void)
28#endif
29{
30 const int32_t MHz = 1000000U;
31 static int stage = 0;
32 static int cnt = 0;
33 static int mal = 0;
34 // DAC = 0 DAC = 3V
35 const int32_t delay_µs[DELAYS_NUM] = { 3, 3, // 0, 1 - 6710µs is MAX @320kHz, [0,1,2,3] = 3µs, [4] = 5.5µs
36 700, 3, // 2, 3
37 700, 4, // 4, 5
38 700 // 6
39 };
40 const int32_t delay_cnt[DELAYS_NUM] = { (delay_µs[ 0 ] * CURRENT_INTEGRAL_FREQ) / MHz,
41 (delay_µs[ 1 ] * CURRENT_INTEGRAL_FREQ) / MHz,
42 (delay_µs[ 2 ] * CURRENT_INTEGRAL_FREQ) / MHz,
43 (delay_µs[ 3 ] * CURRENT_INTEGRAL_FREQ) / MHz,
44 (delay_µs[ 4 ] * CURRENT_INTEGRAL_FREQ) / MHz,
45 (delay_µs[ 5 ] * CURRENT_INTEGRAL_FREQ) / MHz,
46 (delay_µs[ 6 ] * CURRENT_INTEGRAL_FREQ) / MHz,
47 };
48
49 switch(stage)
50 {
51 case 0:
52 // Setting new values for DAC
53 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
54 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
55
56 if (cnt++ >= delay_cnt[0]) // 1c
57 {
58 cnt = 0;
59 stage++;
60 }
61 break;
62
63 case 1:
64 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_3V >> 1;
65 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_3V >> 1;
66
67 if (cnt++ >= delay_cnt[1]) // 1c
68 {
69 cnt = 0;
70 stage++;
71 }
72 break;
73
74 case 2:
75 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
76 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
77
78 if (cnt++ >= delay_cnt[2]) // 300c
79 {
80 cnt = 0;
81 stage++;
82 }
83 break;
84
85 case 3:
86 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = 0xBFF;//DAC_3V;
87 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = 0xBFF;//DAC_3V;
88
89 if (cnt++ >= delay_cnt[3]) // 1c
90 {
91 cnt = 0;
92 stage++;
93 }
94 break;
95
96 case 4:
97 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
98 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
99
100 if (cnt++ >= delay_cnt[4]) // 50c
101 {
102 cnt = 0;
103 stage++;
104 }
105 break;
106
107 case 5:
108 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_3V;
109 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_3V;
110
111 if (cnt++ >= delay_cnt[5]) // 1c
112 {
113 cnt = 0;
114 stage++;
115 }
116 break;
117
118 case 6:
119 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_A_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
120 *(__IO uint32_t *)((uint32_t)DAC_HANDLE.Instance + DAC_CH_B_ALIGNMENT(DAC_ALIGN_12B_R)) = DAC_0V;
121
122 if (cnt++ >= delay_cnt[6]) // 50c
123 {
124 cnt = 0;
125 stage = 5;
126
127 if (mal++ >= 100/*12000*/) // approx 4.25s
128 {
129 mal = 0;
130 stage = 0;
131 HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
132 if (WhereToGoAfterPrecharge != NULL) MOSFETS_Management = WhereToGoAfterPrecharge;
133 else MOSFETS_Management = &DoNothing;
134 HAL_NVIC_EnableIRQ(ADC_DMA_IRQ);
135 }
136 }
137 break;
138 }
139}
140
141//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.