hw_r2.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_r2.h
20  *
21  * Created on: 14 apr 2014
22  * Author: benjamin
23  */
24 
25 #ifndef HW_R2_H_
26 #define HW_R2_H_
27 
28 // Macros
29 #define ENABLE_GATE() palSetPad(GPIOC, 9)
30 #define DISABLE_GATE() palClearPad(GPIOC, 9)
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(GPIOB, 6)
36 #define LED_GREEN_OFF() palClearPad(GPIOB, 6)
37 #define LED_RED_ON() palSetPad(GPIOB, 7)
38 #define LED_RED_OFF() palClearPad(GPIOB, 7)
39 
40 /*
41  * ADC Vector
42  *
43  * 0: IN0 SENS3
44  * 1: IN1 SENS2
45  * 2: IN2 SENS1
46  * 3: IN5 CURR1
47  * 4: IN6 CURR2
48  * 5: IN10 TEMP_MOTOR
49  * 6: IN4 TEMP_PCB
50  * 7: IN9 TEMP_MOS_5
51  * 8: IN12 TEMP_MOS_2
52  * 9: Vrefint
53  * 10: IN15 ADC_EXT
54  * 11: IN11 TEMP_MOS_1
55  * 12: IN14 AN_IN
56  * 13: IN8 TEMP_MOS_4
57  * 14: IN13 TEMP_MOS_3
58  */
59 
60 #define HW_ADC_CHANNELS 15
61 #define HW_ADC_NBR_CONV 5
62 
63 // ADC Indexes
64 #define ADC_IND_SENS1 2
65 #define ADC_IND_SENS2 1
66 #define ADC_IND_SENS3 0
67 #define ADC_IND_CURR1 3
68 #define ADC_IND_CURR2 4
69 #define ADC_IND_VIN_SENS 12
70 #define ADC_IND_EXT 10
71 #define ADC_IND_TEMP_MOS1 11
72 #define ADC_IND_TEMP_MOS2 8
73 #define ADC_IND_TEMP_MOS3 14
74 #define ADC_IND_TEMP_MOS4 13
75 #define ADC_IND_TEMP_MOS5 7
76 #define ADC_IND_TEMP_MOS6 7 // TODO! Same as MOS5
77 #define ADC_IND_TEMP_PCB 6
78 #define ADC_IND_VREFINT 9
79 
80 // ADC macros and settings
81 
82 // Component parameters (can be overridden)
83 #ifndef V_REG
84 #define V_REG 3.3
85 #endif
86 #ifndef VIN_R1
87 #define VIN_R1 33000.0
88 #endif
89 #ifndef VIN_R2
90 #define VIN_R2 2200.0
91 #endif
92 #ifndef CURRENT_AMP_GAIN
93 #define CURRENT_AMP_GAIN 10.0
94 #endif
95 #ifndef CURRENT_SHUNT_RES
96 #define CURRENT_SHUNT_RES 0.001
97 #endif
98 
99 // Input voltage
100 #define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2))
101 
102 // NTC Termistors
103 #define NTC_RES(adc_val) (10000.0 / ((4095.0 / (float)adc_val) - 1.0))
104 #define NTC_TEMP(adc_ind) (1.0 / ((logf(NTC_RES(ADC_Value[adc_ind]) / 10000.0) / 3434.0) + (1.0 / 298.15)) - 273.15)
105 
106 // Voltage on ADC channel
107 #define ADC_VOLTS(ch) ((float)ADC_Value[ch] / 4096.0 * V_REG)
108 
109 // Double samples in beginning and end for positive current measurement.
110 // Useful when the shunt sense traces have noise that causes offset.
111 #ifndef CURR1_DOUBLE_SAMPLE
112 #define CURR1_DOUBLE_SAMPLE 0
113 #endif
114 #ifndef CURR2_DOUBLE_SAMPLE
115 #define CURR2_DOUBLE_SAMPLE 0
116 #endif
117 
118 // Number of servo outputs
119 #define HW_SERVO_NUM 2
120 
121 // UART Peripheral
122 #define HW_UART_DEV UARTD3
123 #define HW_UART_GPIO_AF GPIO_AF_USART3
124 #define HW_UART_TX_PORT GPIOC
125 #define HW_UART_TX_PIN 10
126 #define HW_UART_RX_PORT GPIOC
127 #define HW_UART_RX_PIN 11
128 
129 // ICU Peripheral for servo decoding
130 #define HW_ICU_CHANNEL ICU_CHANNEL_2
131 #define HW_ICU_GPIO_AF GPIO_AF_TIM3
132 #define HW_ICU_GPIO GPIOB
133 #define HW_ICU_PIN 5
134 
135 // I2C Peripheral
136 #define HW_I2C_DEV I2CD2
137 #define HW_I2C_GPIO_AF GPIO_AF_I2C2
138 #define HW_I2C_SCL_PORT GPIOB
139 #define HW_I2C_SCL_PIN 10
140 #define HW_I2C_SDA_PORT GPIOB
141 #define HW_I2C_SDA_PIN 11
142 
143 // Hall/encoder pins
144 #define HW_HALL_ENC_GPIO1 GPIOC
145 #define HW_HALL_ENC_PIN1 6
146 #define HW_HALL_ENC_GPIO2 GPIOC
147 #define HW_HALL_ENC_PIN2 7
148 #define HW_HALL_ENC_GPIO3 GPIOC
149 #define HW_HALL_ENC_PIN3 8
150 #define HW_ENC_TIM TIM3
151 #define HW_ENC_TIM_AF GPIO_AF_TIM3
152 #define HW_ENC_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE)
153 #define HW_ENC_EXTI_PORTSRC EXTI_PortSourceGPIOC
154 #define HW_ENC_EXTI_PINSRC EXTI_PinSource8
155 #define HW_ENC_EXTI_CH EXTI9_5_IRQn
156 #define HW_ENC_EXTI_LINE EXTI_Line8
157 #define HW_ENC_EXTI_ISR_VEC EXTI9_5_IRQHandler
158 
159 // NRF pins
160 #define NRF_PORT_CSN HW_ICU_GPIO
161 #define NRF_PIN_CSN HW_ICU_PIN
162 #define NRF_PORT_SCK GPIOC
163 #define NRF_PIN_SCK 5
164 #define NRF_PORT_MOSI HW_I2C_SDA_PORT
165 #define NRF_PIN_MOSI HW_I2C_SDA_PIN
166 #define NRF_PORT_MISO HW_I2C_SCL_PORT
167 #define NRF_PIN_MISO HW_I2C_SCL_PIN
168 
169 // Measurement macros
170 #define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
171 #define ADC_V_L2 ADC_Value[ADC_IND_SENS2]
172 #define ADC_V_L3 ADC_Value[ADC_IND_SENS3]
173 #define ADC_V_ZERO (ADC_Value[ADC_IND_VIN_SENS] / 2)
174 
175 // Macros
176 #define READ_HALL1() palReadPad(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1)
177 #define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2)
178 #define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
179 
180 #endif /* HW_R2_H_ */