i'm new to ChiBios, i have to use it, because its the firmware of my VESC Motorcontroller I want to add a custom app (thread) to this firmware and wants to use a pin change interrupt. I tried to use some lines from the existing code as example, but its not running and the device reboots Maybe it is just a small mistake and if someone hast time to look on my code, i would appricate it.
some defines:
Code: Select all
//Trigger and Speed Hall Pins
#define HW_HALL_TRIGGER_GPIO GPIOC
#define HW_HALL_TRIGGER_PIN 13
#define HW_HALL_ROTARY_A_GPIO GPIOC
#define HW_HALL_ROTARY_A_PIN 14
#define HW_HALL_ROTARY_B_GPIO GPIOC
#define HW_HALL_ROTARY_B_PIN 15
#define HW_HALL_ROTARY_A_EXTI_PORTSRC EXTI_PortSourceGPIOC
#define HW_HALL_ROTARY_A_EXTI_PINSRC EXTI_PinSource14
#define HW_HALL_ROTARY_A_EXTI_CH EXTI15_10_IRQn
#define HW_HALL_ROTARY_A_EXTI_LINE EXTI_Line14
#define HW_HALL_ROTARY_A_EXTI_ISR_VEC EXTI15_10_IRQHandler
and the "custom app" with thread definition. at the moment the isr does nothing
Code: Select all
#include "ch.h" // ChibiOS
#include "hal.h" // ChibiOS HAL
#include "mc_interface.h" // Motor control functions
#include "hw.h" // Pin mapping on this hardware
#include "timeout.h" // To reset the timeout
#include "commands.h" //just for printf in terminal
#define AVAILABLE_POWERS 3
CH_IRQ_HANDLER(HW_HALL_ROTARY_A_EXTI_ISR_VEC) {
if (EXTI_GetITStatus(HW_HALL_ROTARY_A_EXTI_LINE) != RESET) {
commands_printf("Rotary A Interrupt fired");
// Clear the EXTI line pending bit
EXTI_ClearITPendingBit(HW_HALL_ROTARY_A_EXTI_LINE);
}
}
static THD_FUNCTION(dpv_thread, arg);
static THD_WORKING_AREA(dpv_thread_wa, 2048); // 2kb stack for this thread
void app_dpv_init(void) {
EXTI_InitTypeDef EXTI_InitStructure;
commands_printf("DPV App init");
// Set the UART TX pin as an input with pulldown
palSetPadMode(HW_HALL_TRIGGER_GPIO, HW_HALL_TRIGGER_PIN, PAL_MODE_INPUT_PULLUP);
palSetPadMode(HW_HALL_ROTARY_A_GPIO, HW_HALL_ROTARY_B_PIN, PAL_MODE_INPUT_PULLUP);
palSetPadMode(HW_HALL_ROTARY_B_GPIO, HW_HALL_ROTARY_B_PIN, PAL_MODE_INPUT_PULLUP);
// Interrupt on HALL ROTARY A Pin
// Connect EXTI Line to pin
SYSCFG_EXTILineConfig(HW_HALL_ROTARY_A_EXTI_PORTSRC, HW_HALL_ROTARY_A_EXTI_PINSRC);
// Configure EXTI Line
EXTI_InitStructure.EXTI_Line = HW_HALL_ROTARY_A_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// Enable and set EXTI Line Interrupt to the highest priority
nvicEnableVector(HW_HALL_ROTARY_A_EXTI_CH, 0);
// Start the dv thread
chThdCreateStatic(dpv_thread_wa, sizeof(dpv_thread_wa),
NORMALPRIO, dpv_thread, NULL);
}
static THD_FUNCTION(dpv_thread, arg) {
(void)arg;
chRegSetThreadName("app_dpv");
static bool need_to_change_power_in_future = false;
static float power[AVAILABLE_POWERS] = {0.7, 1, 0.5};
static int power_index = 0;
static bool first=true;
for(;;) {
if ( ! palReadPad(HW_HALL_TRIGGER_GPIO, HW_HALL_TRIGGER_PIN)) {
if (first) {
commands_printf("Trigger: %f",power[power_index]);
first=false;
}
mc_interface_set_duty(power[power_index]);
need_to_change_power_in_future = true;
} else {
first=true;
if(need_to_change_power_in_future){
need_to_change_power_in_future = false;
power_index=(power_index+1)%AVAILABLE_POWERS;
}
// If the button is not pressed, release the motor
mc_interface_release_motor();
}
// Run this loop at 100Hz
chThdSleepMilliseconds(10);
// Reset the timeout
timeout_reset();
}
}
Thanks in advance
Georg