Motor Driver Evaluation Kit NEVB-MTR1-t01-1.1.0
Firmware for NEVB-MTR1-KIT1 for trapezoidal control of BLDC motors using Hall-effect sensors
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1/* This file has been prepared for Doxygen automatic documentation generation.*/
21
22#ifndef _MAIN_H_
23#define _MAIN_H_
24
26#define __AVR_ATmega32U4__ 1
27
28// Include main Arduino library for basic Arduino functions
29#include <Arduino.h>
30
31// Include AVR input/output definitions for low-level hardware control
32#include <avr/io.h>
33
34// Define ISR macro for IntelliSense, else include AVR interrupt handling
35// library
36#ifdef __INTELLISENSE__
37#define ISR(vector) void vector(void)
38#else
39#include <avr/interrupt.h>
40#endif
41
42// Define __LPM macro for IntelliSense, else include AVR program space utility
43// library
44#ifdef __INTELLISENSE__
45#define __LPM(x) 0
46#else
47#include <avr/pgmspace.h>
48#endif
49
50// Include math definitions
51#include <math.h>
52
53// Include standard integer type definitions
54#include <stdint.h>
55
65
74
85#define MOTOR_POLES 8
86
99#define F_MOSFET_MAX 100000UL
100
115#define F_MOSFET_MIN 7183UL
116
157#define F_MOSFET 20000UL
158#if (F_MOSFET > F_MOSFET_MAX)
159#error "ADVISORY WARNING: F_MOSFET should not be set above F_MOSFET_MAX."
160#endif
161#if (F_MOSFET < F_MOSFET_MIN)
162#error "ADVISORY WARNING: F_MOSFET should not be set below F_MOSFET_MIN."
163#endif
164
186#define DEAD_TIME 350UL
187#if DEAD_TIME < 350
188#error "ADVISORY WARNING: DEAD_TIME should not be set below 350 ns. If you want to still continue, please uncomment and compile again."
189#endif
190
201#define HALL_PULLUP_ENABLE TRUE
202
220#define EMULATE_HALL FALSE
221
236#define TIM3_FREQ 200UL
237
246#define COMMUTATION_TICKS_STOPPED 6000
247
259#define TURN_OFF_MODE TURN_OFF_MODE_RAMP
260
276#define IPHASE_GAIN 20
277
294#define IPHASE_SENSE_RESISTOR 2500
295
314#define IBUS_GAIN 50
315
332#define IBUS_SENSE_RESISTOR 4000
333
367#define IBUS_WARNING_THRESHOLD 307
368
406#define IBUS_ERROR_THRESHOLD 410
407
419#define IBUS_FAULT_ENABLE TRUE
420
433#define SPEED_CONTROL_METHOD SPEED_CONTROL_OPEN_LOOP
434
448#define SPEED_CONTROLLER_TIME_BASE 200
449
469#define SPEED_CONTROLLER_MAX_DELTA 1
470
490#define SPEED_CONTROLLER_MAX_SPEED 400
491
509#define PID_K_P 100
510
528#define PID_K_I 10
529
547#define PID_K_D_ENABLE TRUE
548
566#define PID_K_D 0
567
586#define VBUS_RTOP 100000
587
606#define VBUS_RBOTTOM 6200
607
633#define WAIT_FOR_BOARD TRUE
634#if (WAIT_FOR_BOARD == FALSE) && (IBUS_FAULT_ENABLE == TRUE)
635#warning "CONFIG WARNING: IBUS_FAULT_ENABLE is TRUE while WAIT_FOR_BOARD is FALSE - IBUS ADC may float when inverter not connected, causing false OC faults"
636#endif
637
644#define REMOTE_DEBUG_MODE FALSE
645
647
656
657// Boolean values
659#define FALSE 0
661#define TRUE (!FALSE)
662
664#define F_HST 64000000UL
665
666// PWM pin definitions
668#define AH_PIN PB6
670#define BH_PIN PC7
672#define CH_PIN PD7
674#define AL_PIN PB5
676#define BL_PIN PC6
678#define CL_PIN PD6
679
680// PWM PORT patterns
682#define PWM_PATTERN_PORTB ((1 << AH_PIN) | (1 << AL_PIN))
684#define PWM_PATTERN_PORTC ((1 << BH_PIN) | (1 << BL_PIN))
686#define PWM_PATTERN_PORTD ((1 << CH_PIN) | (1 << CL_PIN))
689#define OC_ENABLE_PORTB ((1 << OC4OE3) | (1 << OC4OE2))
692#define OC_ENABLE_PORTC ((1 << OC4OE1) | (1 << OC4OE0))
695#define OC_ENABLE_PORTD ((1 << OC4OE5) | (1 << OC4OE4))
696
697// Direction macro definitions
699#define DIRECTION_FORWARD 0
701#define DIRECTION_REVERSE 1
703#define DIRECTION_UNKNOWN 3
704
705// Hall pins definitions
707#define HALL_PIN PINB
709#define H1_PIN PB1
711#define H2_PIN PB2
713#define H3_PIN PB3
714
715// ADC ADMUX selections
718#define ADC_MUX_L_SPEED ADC_MUX_L_ADC4
721#define ADC_MUX_H_SPEED ADC_MUX_H_ADC4
723#define ADC_MUX_L_IBUS ADC_MUX_L_ADC5
725#define ADC_MUX_H_IBUS ADC_MUX_H_ADC5
727#define ADC_MUX_L_IPHASE_U ADC_MUX_L_ADC1
729#define ADC_MUX_H_IPHASE_U ADC_MUX_H_ADC1
731#define ADC_MUX_L_IPHASE_V ADC_MUX_L_ADC7
733#define ADC_MUX_H_IPHASE_V ADC_MUX_H_ADC7
735#define ADC_MUX_L_IPHASE_W ADC_MUX_L_ADC0
737#define ADC_MUX_H_IPHASE_W ADC_MUX_H_ADC0
739#define ADC_MUX_L_VBUSVREF ADC_MUX_L_ADC6
741#define ADC_MUX_H_VBUSVREF ADC_MUX_H_ADC6
742
743// ADC configurations
745#define ADC_PRESCALER ADC_PRESCALER_DIV_128
747#define ADC_REFERENCE_VOLTAGE ADC_REFERENCE_VOLTAGE_VCC
749#define ADC_TRIGGER ADC_TRIGGER_TIMER0_OVF
750
751// Input pin definitions
753#define DIRECTION_COMMAND_PIN PD2
755#define ENABLE_PIN PD0
757#define REMOTE_PIN PD3
759#define IBUS_PIN PF5
760
761// Speed Input Source definitions (only during remote mode)
763#define SPEED_INPUT_SOURCE_LOCAL 0
765#define SPEED_INPUT_SOURCE_REMOTE 1
766
767// Fault pin definitions
769#define FAULT_PIN_1 PD4
771#define FAULT_PIN_2 PB4
773#define FAULT_PIN_3 PB7
774
775// Waveform macro definitions
777#define WAVEFORM_BLOCK_COMMUTATION 0
779#define WAVEFORM_UNDEFINED 3
780
781// Turn off mode macro definitions
783#define TURN_OFF_MODE_COAST 0
785#define TURN_OFF_MODE_RAMP 1
786
787// Speed control macro definitions
789#define SPEED_CONTROL_OPEN_LOOP 0
791#define SPEED_CONTROL_CLOSED_LOOP 1
792
805#define SPEED_CONTROLLER_MAX_INPUT 255
806
808#define CHOOSE_TIM4_PRESCALER(tim4Freq) ((tim4Freq) < 15625 ? 4 : ((tim4Freq) < 31250 ? 2 : 1))
809
826#define CHOOSE_DT_PRESCALER(deadTime) \
827 ((deadTime) <= 234 ? 1 : (deadTime) <= 468 ? 2 \
828 : (deadTime) <= 937 ? 4 \
829 : (deadTime) <= 1875 ? 8 \
830 : 0)
831
843#if defined(__INTELLISENSE__) || defined(__DOXYGEN__)
844#define FORCE_INLINE inline
845#else
846#define FORCE_INLINE inline __attribute__((always_inline))
847#endif
848
859#if defined(__INTELLISENSE__) || defined(__DOXYGEN__)
860#define FAST_ACCESS(register_address)
861#else
862#define FAST_ACCESS(register_address) __attribute__((address(register_address)))
863#endif
864
865#ifdef __INTELLISENSE__
875#define sei() ((void)0)
876
886#define cli() ((void)0)
887#endif
888
897#define PLL_POSTSCALER_OFF ((0 << PLLTM1) | (0 << PLLTM0))
899#define PLL_POSTSCALER_DIV_1_0 ((0 << PLLTM1) | (1 << PLLTM0))
901#define PLL_POSTSCALER_DIV_1_5 ((1 << PLLTM1) | (0 << PLLTM0))
903#define PLL_POSTSCALER_DIV_2_0 ((1 << PLLTM1) | (1 << PLLTM0))
905
913#define DT_PRESCALER_DIV_1 ((0 << DTPS41) | (0 << DTPS40))
915#define DT_PRESCALER_DIV_2 ((0 << DTPS41) | (1 << DTPS40))
917#define DT_PRESCALER_DIV_4 ((1 << DTPS41) | (0 << DTPS40))
919#define DT_PRESCALER_DIV_8 ((1 << DTPS41) | (1 << DTPS40))
921
930#define TIM1_CLOCK_OFF ((0 << CS12) | (0 << CS11) | (0 << CS10))
932#define TIM1_CLOCK_DIV_1 ((0 << CS12) | (0 << CS11) | (1 << CS10))
934#define TIM1_CLOCK_DIV_8 ((0 << CS12) | (1 << CS11) | (0 << CS10))
936#define TIM1_CLOCK_DIV_64 ((0 << CS12) | (1 << CS11) | (1 << CS10))
938#define TIM1_CLOCK_DIV_256 ((1 << CS12) | (0 << CS11) | (0 << CS10))
940#define TIM1_CLOCK_DIV_1024 ((1 << CS12) | (0 << CS11) | (1 << CS10))
942#define TIM1_CLOCK_EXT_FALLING ((1 << CS12) | (1 << CS11) | (0 << CS10))
944#define TIM1_CLOCK_EXT_RISING ((1 << CS12) | (1 << CS11) | (1 << CS10))
946
954#define TIM4_PRESCALER_OFF ((0 << CS43) | (0 << CS42) | (0 << CS41) | (0 << CS40))
956#define TIM4_PRESCALER_DIV_1 ((0 << CS43) | (0 << CS42) | (0 << CS41) | (1 << CS40))
958#define TIM4_PRESCALER_DIV_2 ((0 << CS43) | (0 << CS42) | (1 << CS41) | (0 << CS40))
960#define TIM4_PRESCALER_DIV_4 ((0 << CS43) | (0 << CS42) | (1 << CS41) | (1 << CS40))
962
971#define ADC_MUX_L_BITS ((1 << MUX4) | (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
973#define ADC_MUX_H_BITS (1 << MUX5)
975#define ADC_MUX_L_ADC0 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0))
977#define ADC_MUX_H_ADC0 (0 << MUX5)
979#define ADC_MUX_L_ADC1 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (1 << MUX0))
981#define ADC_MUX_H_ADC1 (0 << MUX5)
983#define ADC_MUX_L_ADC4 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (0 << MUX0))
985#define ADC_MUX_H_ADC4 (0 << MUX5)
987#define ADC_MUX_L_ADC5 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (1 << MUX0))
989#define ADC_MUX_H_ADC5 (0 << MUX5)
991#define ADC_MUX_L_ADC6 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0))
993#define ADC_MUX_H_ADC6 (0 << MUX5)
995#define ADC_MUX_L_ADC7 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
997#define ADC_MUX_H_ADC7 (0 << MUX5)
999#define ADC_MUX_L_ADC8 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0))
1001#define ADC_MUX_H_ADC8 (1 << MUX5)
1003#define ADC_MUX_L_ADC9 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (1 << MUX0))
1005#define ADC_MUX_H_ADC9 (1 << MUX5)
1007#define ADC_MUX_L_ADC10 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0))
1009#define ADC_MUX_H_ADC10 (1 << MUX5)
1011#define ADC_MUX_L_ADC11 ((0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (1 << MUX0))
1013#define ADC_MUX_H_ADC11 (1 << MUX5)
1015#define ADC_MUX_L_ADC12 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (0 << MUX0))
1017#define ADC_MUX_H_ADC12 (1 << MUX5)
1019#define ADC_MUX_L_ADC13 ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (0 << MUX1) | (1 << MUX0))
1021#define ADC_MUX_H_ADC13 (1 << MUX5)
1023#define ADC_MUX_L_TEMP_SENSOR ((0 << MUX4) | (0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
1025#define ADC_MUX_H_TEMP_SENSOR (1 << MUX5)
1027#define ADC_MUX_L_1V1 ((1 << MUX4) | (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0))
1029#define ADC_MUX_H_1V1 (0 << MUX5)
1031#define ADC_MUX_L_0V ((1 << MUX4) | (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0))
1033#define ADC_MUX_H_0V (0 << MUX5)
1035
1043#define ADC_PRESCALER_DIV_2 ((0 << ADPS2) | (0 << ADPS1) | (0 << ADPS0))
1045#define ADC_PRESCALER_DIV_4 ((0 << ADPS2) | (1 << ADPS1) | (0 << ADPS0))
1047#define ADC_PRESCALER_DIV_8 ((0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
1049#define ADC_PRESCALER_DIV_16 ((1 << ADPS2) | (0 << ADPS1) | (0 << ADPS0))
1051#define ADC_PRESCALER_DIV_32 ((1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0))
1053#define ADC_PRESCALER_DIV_64 ((1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0))
1055#define ADC_PRESCALER_DIV_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
1057
1065#define ADC_REFERENCE_VOLTAGE_AREF ((0 << REFS1) | (0 << REFS0))
1067#define ADC_REFERENCE_VOLTAGE_VCC ((0 << REFS1) | (1 << REFS0))
1069#define ADC_REFERENCE_VOLTAGE_INTERNAL ((1 << REFS1) | (1 << REFS0))
1071
1079#define ADC_TRIGGER_FREE ((0 << ADTS3) | (0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
1081#define ADC_TRIGGER_ANALOG_COMP ((0 << ADTS3) | (0 << ADTS2) | (0 << ADTS1) | (1 << ADTS0))
1083#define ADC_TRIGGER_INT0 ((0 << ADTS3) | (0 << ADTS2) | (1 << ADTS1) | (0 << ADTS0))
1085#define ADC_TRIGGER_TIMER0_COMPA ((0 << ADTS3) | (0 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
1087#define ADC_TRIGGER_TIMER0_OVF ((0 << ADTS3) | (1 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
1089#define ADC_TRIGGER_TIMER1_COMPB ((0 << ADTS3) | (1 << ADTS2) | (0 << ADTS1) | (1 << ADTS0))
1091#define ADC_TRIGGER_TIMER1_OVF ((0 << ADTS3) | (1 << ADTS2) | (1 << ADTS1) | (0 << ADTS0))
1093#define ADC_TRIGGER_TIMER1_CAPT ((0 << ADTS3) | (1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
1095#define ADC_TRIGGER_TIMER4_OVF ((1 << ADTS3) | (0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
1097#define ADC_TRIGGER_TIMER4_COMPA ((1 << ADTS3) | (0 << ADTS2) | (0 << ADTS1) | (1 << ADTS0))
1099#define ADC_TRIGGER_TIMER4_COMPB ((1 << ADTS3) | (0 << ADTS2) | (1 << ADTS1) | (0 << ADTS0))
1101#define ADC_TRIGGER_TIMER4_COMPD ((1 << ADTS3) | (0 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
1103
1104// Typedefs
1109typedef struct motorflags
1110{
1112 uint8_t reserved : 7;
1116 uint8_t remote : 1;
1118 uint8_t enable : 1;
1120 uint8_t actualDirection : 2;
1122 uint8_t desiredDirection : 1;
1124 uint8_t driveWaveform : 2;
1126 uint8_t fatalFault : 1;
1128
1133typedef struct faultflags
1134{
1136 uint8_t reserved : 1;
1138 uint8_t reverseDirection : 1;
1140 uint8_t motorStopped : 1;
1142 uint8_t overCurrent : 1;
1146 uint8_t userFlag1 : 1;
1148 uint8_t userFlag2 : 1;
1150 uint8_t userFlag3 : 1;
1152
1174
1180typedef struct motorconfigs
1181{
1183 uint32_t tim4Freq : 17; // max value 131071
1185 uint16_t tim4Top : 10; // max value 1023
1187 uint16_t tim4DeadTime : 11; // max value 2047
1189 uint8_t speedInputSource : 1;
1191
1193
1202
1209#define TIM4_PRESCALER_DIV_PATTERN(tim4Prescaler) \
1210 ((tim4Prescaler) == 1 ? TIM4_PRESCALER_DIV_1 : (tim4Prescaler) == 2 ? TIM4_PRESCALER_DIV_2 \
1211 : (tim4Prescaler) == 4 ? TIM4_PRESCALER_DIV_4 \
1212 : 0)
1213
1216#define TIM4_TOP(tim4Freq) (((F_HST / ((uint32_t)tim4Freq * CHOOSE_TIM4_PRESCALER(tim4Freq))) >> 1) - 1)
1217
1219#define TIM4_TOP_MAX 0x03ff
1220
1227#define DT_PRESCALER_DIV_PATTERN(dtPrescaler) \
1228 ((dtPrescaler) == 1 ? DT_PRESCALER_DIV_1 : (dtPrescaler) == 2 ? DT_PRESCALER_DIV_2 \
1229 : (dtPrescaler) == 4 ? DT_PRESCALER_DIV_4 \
1230 : (dtPrescaler) == 8 ? DT_PRESCALER_DIV_8 \
1231 : 0)
1232
1237#define DEAD_TIME_HALF(deadTime) (((uint8_t)(ceil((double)deadTime * F_HST / ((double)CHOOSE_DT_PRESCALER(deadTime) * 1000000000)))))
1238
1245#define TIM3_TOP (((F_CPU / TIM3_FREQ / 3) >> 4) - 1)
1246
1248#define TIM3_TOP_MAX 0xffff
1249#if TIM3_TOP_ > TIM3_TOP_MAX
1250#error "Invalid TIM3_FREQ set"
1251#endif
1252
1253#if ((EMULATE_HALL == TRUE) && (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP))
1254#error "Invalid combination of EMULATE_HALL and SPEED_CONTROL_METHOD"
1255#endif
1256
1258
1293
1364
1430
1456
1467
1482
1483#endif /* _MAIN_H_ */
struct motorflags motorflags_t
Collection of all motor control flags.
fault_flag_t
Enumeration of fault flags.
Definition config.h:1156
struct motorconfigs motorconfigs_t
Collection of motor configurations.
struct faultflags faultflags_t
Collection of all fault flags.
@ FAULT_OVER_CURRENT
Has it tripped the over current limit?
Definition config.h:1164
@ FAULT_USER_FLAG1
Is user flag 1 set?
Definition config.h:1168
@ FAULT_NO_HALL_CONNECTIONS
Is there no hall connections?
Definition config.h:1166
@ FAULT_USER_FLAG2
Is user flag 2 set?
Definition config.h:1170
@ FAULT_RESERVED
Reserved flag, always false.
Definition config.h:1158
@ FAULT_USER_FLAG3
Is user flag 3 set?
Definition config.h:1172
@ FAULT_REVERSE_DIRECTION
Is motor spinning in an unexpected direction?
Definition config.h:1160
@ FAULT_MOTOR_STOPPED
Is motor stopped?
Definition config.h:1162
Collection of all fault flags.
Definition config.h:1134
uint8_t userFlag2
Is user flag 2 set?
Definition config.h:1148
uint8_t motorStopped
Is motor stopped?
Definition config.h:1140
uint8_t userFlag3
Is user flag 3 set?
Definition config.h:1150
uint8_t reserved
Reserved bit(s).
Definition config.h:1136
uint8_t noHallConnections
Is there no hall connections?
Definition config.h:1144
uint8_t overCurrent
Has it tripped the over current limit?
Definition config.h:1142
uint8_t reverseDirection
Is motor spinning in an unexpected direction?
Definition config.h:1138
uint8_t userFlag1
Is user flag 1 set?
Definition config.h:1146
Collection of motor configurations.
Definition config.h:1181
uint16_t tim4Top
Corresponding TIM4 top value for TIM4 frequency.
Definition config.h:1185
uint8_t speedInputSource
SpeedInput source select (only for remote mode).
Definition config.h:1189
uint16_t tim4DeadTime
Corresponding dead time for TIM4 output.
Definition config.h:1187
uint32_t tim4Freq
TIM4 or gate switching frequency.
Definition config.h:1183
Collection of all motor control flags.
Definition config.h:1110
uint8_t desiredDirection
The desired direction of rotation.
Definition config.h:1122
uint8_t enable
Is the motor enabled?
Definition config.h:1118
uint8_t actualDirection
The actual direction of rotation.
Definition config.h:1120
uint8_t driveWaveform
The current waveform that should be produced.
Definition config.h:1124
uint8_t fatalFault
Fatal fault.
Definition config.h:1126
uint8_t reserved
Reserved bit(s).
Definition config.h:1112
uint8_t remote
Is the remote enabled?
Definition config.h:1116
uint8_t speedControllerRun
Should speed controller run?
Definition config.h:1114