hw_46.h
Go to the documentation of this file.
1 /*
2  Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * hw_40.6
20  *
21  * Created on: 22 nov 2014
22  * Author: benjamin
23  */
24 
25 #ifndef HW_46_H_
26 #define HW_46_H_
27 
28 // Macros
29 #define ENABLE_GATE() palSetPad(GPIOC, 10)
30 #define DISABLE_GATE() palClearPad(GPIOC, 10)
31 #define DCCAL_ON() palSetPad(GPIOB, 12)
32 #define DCCAL_OFF() palClearPad(GPIOB, 12)
33 #define IS_DRV_FAULT() (!palReadPad(GPIOC, 12))
34 
35 #define LED_GREEN_ON() palSetPad(GPIOC, 4)
36 #define LED_GREEN_OFF() palClearPad(GPIOC, 4)
37 #define LED_RED_ON() palSetPad(GPIOA, 7)
38 #define LED_RED_OFF() palClearPad(GPIOA, 7)
39 
40 /*
41  * ADC Vector
42  *
43  * 0: IN0 SENS3
44  * 1: IN1 SENS2
45  * 2: IN2 SENS1
46  * 3: IN5 CURR2
47  * 4: IN6 CURR1
48  * 5: IN3 NC
49  * 6: Vrefint
50  * 7: IN11 NC
51  * 8: IN12 AN_IN
52  * 9: IN4 TEMP_MOSFET
53  * 10: IN15 ADC_EXT
54  * 11: IN10 TEMP_MOTOR
55  */
56 
57 #define HW_ADC_CHANNELS 12
58 #define HW_ADC_NBR_CONV 4
59 
60 // ADC Indexes
61 #define ADC_IND_SENS1 2
62 #define ADC_IND_SENS2 1
63 #define ADC_IND_SENS3 0
64 #define ADC_IND_CURR1 4
65 #define ADC_IND_CURR2 3
66 #define ADC_IND_VIN_SENS 8
67 #define ADC_IND_EXT 10
68 #define ADC_IND_TEMP_MOS1 9
69 #define ADC_IND_TEMP_MOS2 9
70 #define ADC_IND_TEMP_MOS3 9
71 #define ADC_IND_TEMP_MOS4 9
72 #define ADC_IND_TEMP_MOS5 9
73 #define ADC_IND_TEMP_MOS6 9
74 #define ADC_IND_TEMP_PCB 9
75 #define ADC_IND_VREFINT 6
76 
77 // ADC macros and settings
78 
79 // Component parameters (can be overridden)
80 #ifndef V_REG
81 //#define V_REG 3.3
82 #endif
83 #ifndef VIN_R1
84 #define VIN_R1 39000.0
85 #endif
86 #ifndef VIN_R2
87 #define VIN_R2 2200.0
88 #endif
89 #ifndef CURRENT_AMP_GAIN
90 #define CURRENT_AMP_GAIN 10.0
91 #endif
92 #ifndef CURRENT_SHUNT_RES
93 #define CURRENT_SHUNT_RES 0.001
94 #endif
95 
96 // Input voltage
97 #define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2))
98 
99 // Voltage on ADC channel
100 #define ADC_VOLTS(ch) ((float)ADC_Value[ch] / 4095.0 * V_REG)
101 
102 // NTC Termistors
103 //#define NTC_RES(adc_val) (10000.0 / ((4096.0 / (float)adc_val) - 1.0))
104 #define NTC_RES(adc_val) ((4095.0 * 10000.0) / adc_val - 10000.0)
105 #define NTC_TEMP(adc_ind) (1.0 / ((logf(NTC_RES(ADC_Value[adc_ind]) / 10000.0) / 3434.0) + (1.0 / 298.15)) - 273.15)
106 
107 // Double samples in beginning and end for positive current measurement.
108 // Useful when the shunt sense traces have noise that causes offset.
109 #ifndef CURR1_DOUBLE_SAMPLE
110 #define CURR1_DOUBLE_SAMPLE 1
111 #endif
112 #ifndef CURR2_DOUBLE_SAMPLE
113 #define CURR2_DOUBLE_SAMPLE 0
114 #endif
115 
116 // Number of servo outputs
117 #define HW_SERVO_NUM 2
118 
119 // UART Peripheral
120 #define HW_UART_DEV UARTD6
121 #define HW_UART_GPIO_AF GPIO_AF_USART6
122 #define HW_UART_TX_PORT GPIOC
123 #define HW_UART_TX_PIN 6
124 #define HW_UART_RX_PORT GPIOC
125 #define HW_UART_RX_PIN 7
126 
127 // ICU Peripheral for servo decoding
128 #define HW_ICU_CHANNEL ICU_CHANNEL_2
129 #define HW_ICU_GPIO_AF GPIO_AF_TIM3
130 #define HW_ICU_GPIO GPIOB
131 #define HW_ICU_PIN 5
132 
133 // I2C Peripheral
134 #define HW_I2C_DEV I2CD2
135 #define HW_I2C_GPIO_AF GPIO_AF_I2C2
136 #define HW_I2C_SCL_PORT GPIOB
137 #define HW_I2C_SCL_PIN 10
138 #define HW_I2C_SDA_PORT GPIOB
139 #define HW_I2C_SDA_PIN 11
140 
141 // Hall/encoder pins
142 #define HW_HALL_ENC_GPIO1 GPIOB
143 #define HW_HALL_ENC_PIN1 6
144 #define HW_HALL_ENC_GPIO2 GPIOB
145 #define HW_HALL_ENC_PIN2 7
146 #define HW_HALL_ENC_GPIO3 GPIOC
147 #define HW_HALL_ENC_PIN3 11
148 #define HW_ENC_TIM TIM4
149 #define HW_ENC_TIM_AF GPIO_AF_TIM4
150 #define HW_ENC_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE)
151 #define HW_ENC_EXTI_PORTSRC EXTI_PortSourceGPIOC
152 #define HW_ENC_EXTI_PINSRC EXTI_PinSource11
153 #define HW_ENC_EXTI_CH EXTI15_10_IRQn
154 #define HW_ENC_EXTI_LINE EXTI_Line11
155 #define HW_ENC_EXTI_ISR_VEC EXTI15_10_IRQHandler
156 
157 // NRF pins
158 #define NRF_PORT_CSN HW_ICU_GPIO
159 #define NRF_PIN_CSN HW_ICU_PIN
160 #define NRF_PORT_SCK GPIOC
161 #define NRF_PIN_SCK 5
162 #define NRF_PORT_MOSI HW_I2C_SDA_PORT
163 #define NRF_PIN_MOSI HW_I2C_SDA_PIN
164 #define NRF_PORT_MISO HW_I2C_SCL_PORT
165 #define NRF_PIN_MISO HW_I2C_SCL_PIN
166 
167 // Measurement macros
168 #define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
169 #define ADC_V_L2 ADC_Value[ADC_IND_SENS2]
170 #define ADC_V_L3 ADC_Value[ADC_IND_SENS3]
171 #define ADC_V_ZERO (ADC_Value[ADC_IND_VIN_SENS] / 2)
172 
173 // Macros
174 #define READ_HALL1() palReadPad(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1)
175 #define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2)
176 #define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
177 
178 #endif /* HW_40_H_ */