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

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

12V variant is working good.

File size: 4.1 KB
RevLine 
[1]1#include <stdio.h>
2
3#include "main.h"
4#include "dac.h"
5#include "dma.h"
6#include "precharge.h"
7
[3]8#define DELAYS_NUM 7U
[1]9
10extern void (*MOSFETS_Management)(void);
11extern void DoNothing(void);
12
[7]13static void (*WhereToGoAfterPrecharge)(void);
[1]14
15//------------------------------------------------------------------------------
16
17void SetReturnFunction(void (*WhereToReturnControl)(void))
18{
[7]19 if (WhereToReturnControl != NULL) WhereToGoAfterPrecharge = WhereToReturnControl;
[1]20}
21
22//------------------------------------------------------------------------------
23
24#ifdef USE_RAM_FUNC
25__RAM_FUNC void PreChargeStage(void)
26#else
27 void PreChargeStage(void)
28#endif
29{
[3]30 const int32_t MHz = 1000000U;
[1]31 static int stage = 0;
32 static int cnt = 0;
33 static int mal = 0;
[3]34 // DAC = 0 DAC = 3V
[7]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
[3]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 };
[1]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
[3]56 if (cnt++ >= delay_cnt[0]) // 1c
[1]57 {
58 cnt = 0;
59 stage++;
60 }
61 break;
62
63 case 1:
[3]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;
[1]66
[3]67 if (cnt++ >= delay_cnt[1]) // 1c
[1]68 {
69 cnt = 0;
70 stage++;
71 }
72 break;
73
[3]74 case 2:
[1]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
[3]78 if (cnt++ >= delay_cnt[2]) // 300c
[1]79 {
80 cnt = 0;
81 stage++;
82 }
83 break;
84
85 case 3:
[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;
[1]88
[3]89 if (cnt++ >= delay_cnt[3]) // 1c
[1]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
[3]100 if (cnt++ >= delay_cnt[4]) // 50c
[1]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
[3]111 if (cnt++ >= delay_cnt[5]) // 1c
[1]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
[3]122 if (cnt++ >= delay_cnt[6]) // 50c
[1]123 {
124 cnt = 0;
[3]125 stage = 5;
[1]126
[7]127 if (mal++ >= 100/*12000*/) // approx 4.25s
[1]128 {
129 mal = 0;
[3]130 stage = 0;
[1]131 HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
[7]132 if (WhereToGoAfterPrecharge != NULL) MOSFETS_Management = WhereToGoAfterPrecharge;
[1]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.