diff --git a/Makefile b/Makefile index fa09158..842bd1b 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ endif C_SOURCES := $(foreach folder, $(SRC_DIR), $(wildcard $(folder)/*.c)) C_OBJECTS := $(C_SOURCES:%.c=$(OBJ_DIR)/%.o) -C_DEPS := $(SOURCES:%.c=$(OBJ_DIR)/%.d) +C_DEPS := $(C_SOURCES:%.c=$(OBJ_DIR)/%.d) all: $(MAIN_FILE) diff --git a/config/make/stm32f4xx.mk b/config/make/stm32f4xx.mk index f5c58bc..1f3f231 100644 --- a/config/make/stm32f4xx.mk +++ b/config/make/stm32f4xx.mk @@ -57,7 +57,8 @@ C_FLAGS += \ -Wno-unused-parameter \ -Wno-sign-compare \ -Wno-missing-prototypes \ - -Wno-missing-declarations + -Wno-missing-declarations \ + -Wno-missing-field-initializers L_FLAGS := \ -T mem.ld \ @@ -66,7 +67,8 @@ L_FLAGS := \ -nostartfiles \ -Xlinker --gc-sections \ -L"config/linker" \ - --specs=nano.specs + --specs=nano.specs \ + -u _printf_float ifeq ($(DEBUG),y) OPTIM = g diff --git a/source/firmware/kernel/include/isr.h b/source/firmware/kernel/include/isr.h index 28b78c2..33d826e 100644 --- a/source/firmware/kernel/include/isr.h +++ b/source/firmware/kernel/include/isr.h @@ -8,8 +8,8 @@ #ifndef ISR_H_ #define ISR_H_ -#ifdef ARCH_MSP430 -#include "msp430_isr.h" +#ifdef ARCH_STM32F4XX +#include "stm32f4xx_isr.h" #endif #endif /* ISR_H_ */ diff --git a/source/test/pwm/main.c b/source/test/pwm/main.c index 5dc1f09..0f7789a 100644 --- a/source/test/pwm/main.c +++ b/source/test/pwm/main.c @@ -6,6 +6,8 @@ */ #include +#include +#include #include "driver.h" #include "board.h" @@ -16,8 +18,79 @@ #include "list.h" #include "shell.h" +static TIM_HandleTypeDef timer_handle = { + .Instance = TIM2, + .Init.Prescaler = 1000, + .Init.CounterMode = TIM_COUNTERMODE_UP, + .Init.Period = 0xffff, + .Init.ClockDivision = TIM_CLOCKDIVISION_DIV1, + .Init.RepetitionCounter = 0, +}; + +static TIM_IC_InitTypeDef input_capture_init = { + .ICPrescaler = TIM_ICPSC_DIV1, + .ICFilter = 0, + .ICPolarity = TIM_ICPOLARITY_FALLING, + .ICSelection = TIM_ICSELECTION_INDIRECTTI, +}; + +static TIM_SlaveConfigTypeDef slave_config = { + .SlaveMode = TIM_SLAVEMODE_RESET, + .InputTrigger = TIM_TS_TI1FP1, + .TriggerPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE, + .TriggerFilter = 0, +}; + +static GPIO_InitTypeDef port_cfg_b10 = { + .Pin = GPIO_PIN_10, + .Mode = GPIO_MODE_AF_OD, + .Speed = GPIO_SPEED_FREQ_HIGH, + .Pull = GPIO_NOPULL, + .Alternate = GPIO_AF1_TIM2, +}; + +static const struct stm32f4_gpio b10_gpio = { + .port = GPIOB, + .pin = &port_cfg_b10, +}; + +static const struct gpio __gpio_b10 = { + (void*)&b10_gpio, + &gpio_fp +}; + +static const struct driver gpio_b10 = { + DRIVER_TYPE_GPIO, + &__gpio_b10, +}; + +static uint32_t ch3 = 0, ch4 = 0; +static int period = 0, pulse = 0; + + +void config_input(void) +{ + __HAL_RCC_TIM2_CLK_ENABLE(); + drv_open(&gpio_b10); + HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1); + + /* cfg timer */ + HAL_TIM_IC_Init(&timer_handle); + HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_4); + input_capture_init.ICPolarity = TIM_ICPOLARITY_RISING; + input_capture_init.ICSelection = TIM_ICSELECTION_DIRECTTI; + HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_3); + + HAL_TIM_SlaveConfigSynchronization(&timer_handle, &slave_config); + HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_4); + HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_3); + + HAL_NVIC_EnableIRQ(TIM2_IRQn); +} + int main(void) { + char print_buffer[80]; drv_open(&pwm_1); drv_open(&pwm_2); drv_open(&pwm_3); @@ -25,22 +98,104 @@ int main(void) drv_open(&pwm5_c2); + drv_open(&uart_1); + + config_input(); + while(1) { for(unsigned int duty = 0; duty < 100; duty++) { drv_ioctl(&pwm_1, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_2, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); - sleep_ms(10); + sleep_ms(100); + if((period >= 0) && (pulse >= 0)) { +// sprintf(print_buffer, "period: %u,\tpulse: %u\r\n", period, pulse); +// drv_write(&uart_1, print_buffer, strlen(print_buffer)); + + float dist = 84000.0 / pulse; + dist = 343.0 / dist; + dist /= 2.0; + dist *= 100; // m -> cm + int d = (int)dist; + sprintf(print_buffer, "distance: %d cm\r\n", d); + drv_write(&uart_1, print_buffer, strlen(print_buffer)); + } } for(unsigned int duty = 98; duty > 0; duty--) { drv_ioctl(&pwm_1, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_2, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); - sleep_ms(10); + sleep_ms(100); + if((period >= 0) && (pulse >= 0)) { +// sprintf(print_buffer, "period: %u,\tpulse: %u\r\n", period, pulse); +// drv_write(&uart_1, print_buffer, strlen(print_buffer)); + + float dist = 84000.0 / pulse; + dist = 343.0 / dist; + dist /= 2.0; + dist *= 100; // m -> cm + int d = (int)dist; + sprintf(print_buffer, "distance: %d cm\r\n", d); + drv_write(&uart_1, print_buffer, strlen(print_buffer)); + } } } return 0; } + +#include "isr.h" +void TIM2_IRQHandler(void) +{ + enter_isr(); + TIM_HandleTypeDef *htim = &timer_handle; + uint32_t tmpreg = 0U; + + if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET) { + if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET) { + __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1); + /* Input capture event */ + if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U) { + tmpreg = htim->Instance->CCR1; + } + } + } + /* Capture compare 2 event */ + if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET) { + if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) !=RESET) { + __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2); + /* Input capture event */ + if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U) { + tmpreg = htim->Instance->CCR2; + } + } + } + /* Capture compare 3 event */ + if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET) { + if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) !=RESET) { + __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3); + /* Input capture event */ + if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U) { + ch3 = htim->Instance->CCR3; + } + } + } + /* Capture compare 4 event */ + if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET) { + if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET) { + __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4); + /* Input capture event */ + if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U) { + uint32_t ch4_old = ch4; + ch4 = htim->Instance->CCR4; + period = ch4 - ch4_old; + pulse = ch4 - ch3; + } + } + } + tmpreg = tmpreg; + + exit_isr(); +}