3 #define RCC_TIM17EN 0x00040000 4 #define RCC_TIM17RST 0x00040000 25 static const uint8_t pin_A[2] = {GPIOA_RMOTA, GPIOA_LMOTA};
26 static const uint8_t pin_B[2] = {GPIOA_RMOTB, GPIOA_LMOTB};
28 static int8_t left_speed = 0U;
29 static int8_t right_speed = 0U;
31 #define PWM_FREQUENCY_KHZ 20 34 static PWMConfig pwm_config_tim2 = {
39 {PWM_OUTPUT_DISABLED, NULL},
40 {PWM_OUTPUT_DISABLED, NULL},
41 {PWM_OUTPUT_ACTIVE_HIGH, NULL},
42 {PWM_OUTPUT_DISABLED, NULL}
51 pwmStart(&PWMD2, &pwm_config_tim2);
54 rccEnableAPB2(RCC_TIM17EN, FALSE);
61 TIM17->PSC = CLK_KHZ/(PWM_FREQUENCY_KHZ *
MAX_COMMAND) - 1;
88 pwmEnableChannel(&PWMD2, 2, speed);
119 return motor_direction[motor];
131 motor_direction[motor] = direction;
133 if (rotation_direction[motor][direction] ==
DIRECTION_1) {
134 palSetPad(GPIOA, pin_A[motor]);
135 palClearPad(GPIOA, pin_B[motor]);
137 palClearPad(GPIOA, pin_A[motor]);
138 palSetPad(GPIOA, pin_B[motor]);
147 if (motor_direction[motor] ==
FORWARD) {
int motor_set_speed(motor_t motor, uint8_t speed)
Set the rotation speed of the specified motor.
int motor_set_direction(motor_t motor, motor_direction_t direction)
Set the rotation direction of the specified motor.
motor_direction_t
Alias for motor rotation sense.
motor_t
Alias to select the motor to work on.
motor_sense_t
Alias for motor orientation.
void motor_init(motor_sense_t motor_left_forward_sense, motor_sense_t motor_right_forward_sense)
Initialise the motor driver.
void motor_toggle_direction(motor_t motor)
Revert the rotation direction of the specified motor.
int8_t motor_get_speed(motor_t motor)
Get the rotation speed of the specified motor.
motor_direction_t motor_get_direction(motor_t motor)
Get the current rotation direction of the specified motor.