hw_45.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_45.h
20  *
21  * Created on: 19 okt 2014
22  * Author: benjamin
23  */
24 
25 #ifndef HW_45_H_
26 #define HW_45_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: IN13 NC
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_VREFINT 6
69 
70 // ADC macros and settings
71 
72 // Component parameters (can be overridden)
73 #ifndef V_REG
74 #define V_REG 3.3
75 #endif
76 #ifndef VIN_R1
77 #define VIN_R1 33000.0
78 #endif
79 #ifndef VIN_R2
80 #define VIN_R2 2200.0
81 #endif
82 #ifndef CURRENT_AMP_GAIN
83 #define CURRENT_AMP_GAIN 10.0
84 #endif
85 #ifndef CURRENT_SHUNT_RES
86 #define CURRENT_SHUNT_RES 0.001
87 #endif
88 
89 // Input voltage
90 #define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2))
91 
92 // Voltage on ADC channel
93 #define ADC_VOLTS(ch) ((float)ADC_Value[ch] / 4095.0 * V_REG)
94 
95 // NTC Termistors
96 #define NTC_RES(adc_val) (0.0)
97 #define NTC_TEMP(adc_ind) hw45_get_temp()
98 
99 // Double samples in beginning and end for positive current measurement.
100 // Useful when the shunt sense traces have noise that causes offset.
101 #ifndef CURR1_DOUBLE_SAMPLE
102 #define CURR1_DOUBLE_SAMPLE 1
103 #endif
104 #ifndef CURR2_DOUBLE_SAMPLE
105 #define CURR2_DOUBLE_SAMPLE 0
106 #endif
107 
108 // Number of servo outputs
109 #define HW_SERVO_NUM 2
110 
111 // UART Peripheral
112 #define HW_UART_DEV UARTD6
113 #define HW_UART_GPIO_AF GPIO_AF_USART6
114 #define HW_UART_TX_PORT GPIOC
115 #define HW_UART_TX_PIN 6
116 #define HW_UART_RX_PORT GPIOC
117 #define HW_UART_RX_PIN 7
118 
119 // ICU Peripheral for servo decoding
120 #define HW_ICU_CHANNEL ICU_CHANNEL_2
121 #define HW_ICU_GPIO_AF GPIO_AF_TIM3
122 #define HW_ICU_GPIO GPIOB
123 #define HW_ICU_PIN 5
124 
125 // I2C Peripheral
126 #define HW_I2C_DEV I2CD2
127 #define HW_I2C_GPIO_AF GPIO_AF_I2C2
128 #define HW_I2C_SCL_PORT GPIOB
129 #define HW_I2C_SCL_PIN 10
130 #define HW_I2C_SDA_PORT GPIOB
131 #define HW_I2C_SDA_PIN 11
132 
133 // Hall/encoder pins
134 #define HW_HALL_ENC_GPIO1 GPIOB
135 #define HW_HALL_ENC_PIN1 6
136 #define HW_HALL_ENC_GPIO2 GPIOB
137 #define HW_HALL_ENC_PIN2 7
138 #define HW_HALL_ENC_GPIO3 GPIOC
139 #define HW_HALL_ENC_PIN3 11
140 #define HW_ENC_TIM TIM4
141 #define HW_ENC_TIM_AF GPIO_AF_TIM4
142 #define HW_ENC_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE)
143 #define HW_ENC_EXTI_PORTSRC EXTI_PortSourceGPIOC
144 #define HW_ENC_EXTI_PINSRC EXTI_PinSource11
145 #define HW_ENC_EXTI_CH EXTI15_10_IRQn
146 #define HW_ENC_EXTI_LINE EXTI_Line11
147 #define HW_ENC_EXTI_ISR_VEC EXTI15_10_IRQHandler
148 
149 // NRF pins
150 #define NRF_PORT_CSN HW_ICU_GPIO
151 #define NRF_PIN_CSN HW_ICU_PIN
152 #define NRF_PORT_SCK GPIOC
153 #define NRF_PIN_SCK 5
154 #define NRF_PORT_MOSI HW_I2C_SDA_PORT
155 #define NRF_PIN_MOSI HW_I2C_SDA_PIN
156 #define NRF_PORT_MISO HW_I2C_SCL_PORT
157 #define NRF_PIN_MISO HW_I2C_SCL_PIN
158 
159 // Measurement macros
160 #define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
161 #define ADC_V_L2 ADC_Value[ADC_IND_SENS2]
162 #define ADC_V_L3 ADC_Value[ADC_IND_SENS3]
163 #define ADC_V_ZERO (ADC_Value[ADC_IND_VIN_SENS] / 2)
164 
165 // Macros
166 #define READ_HALL1() palReadPad(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1)
167 #define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2)
168 #define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
169 
170 // HW-specific functions
171 float hw45_get_temp(void);
172 
173 #endif /* HW_40_H_ */
float hw45_get_temp(void)