PWM Fix, plus debugging

This commit is contained in:
Hierophect 2020-01-04 13:35:53 -05:00
parent 28b1d718a3
commit 36088becc9
6 changed files with 186 additions and 49 deletions

View File

@ -85,6 +85,11 @@ ifeq ($(DEBUG), 1)
CFLAGS += -fno-inline -fno-ipa-sra
else
CFLAGS += -Os -DNDEBUG
CFLAGS += -ggdb
# CFLAGS += -fno-inline -fno-ipa-sra
#CFLAGS += -Os -DNDEBUG
# TODO: Test with -flto
### CFLAGS += -flto
endif

View File

@ -10,7 +10,7 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_DISP_CS), MP_ROM_PTR(&pin_PB12) },
{ MP_ROM_QSTR(MP_QSTR_DISP_DC), MP_ROM_PTR(&pin_PA08) },
{ MP_ROM_QSTR(MP_QSTR_DISP_RST), MP_ROM_PTR(&pin_PB10) },
{ MP_ROM_QSTR(MP_QSTR_DISP_BL), MP_ROM_PTR(&pin_PB03) }, //what is this, backlight?
{ MP_ROM_QSTR(MP_QSTR_DISP_BL), MP_ROM_PTR(&pin_PB03) }, //what is this, backlight?
{ MP_ROM_QSTR(MP_QSTR_BUZZ), MP_ROM_PTR(&pin_PB08) },
{ MP_ROM_QSTR(MP_QSTR_BTNA), MP_ROM_PTR(&pin_PB09) },
@ -32,8 +32,8 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PB07) },
{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PB06) },
// { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PC06) }, //these are wrong on Meowbit diagram.
// { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PA03) }, //they cannot be used together (UART2 vs 6)
// { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PC06) }, //these are wrong on Meowbit diagram.
// { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PA03) }, //they cannot be used together (UART2 vs 6)
{ MP_ROM_QSTR(MP_QSTR_NSS), MP_ROM_PTR(&pin_PA15) },
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PC12) },
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PC11) },

View File

@ -26,6 +26,7 @@
*/
#include <stdint.h>
#include <string.h> //add
#include "py/runtime.h"
#include "common-hal/pulseio/PWMOut.h"
#include "shared-bindings/pulseio/PWMOut.h"
@ -88,6 +89,7 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
}
void pwmout_reset(void) {
mp_printf(&mp_plat_print, "reset\n");
uint16_t never_reset_mask = 0x00;
for(int i=0;i<TIM_BANK_ARRAY_LEN;i++) {
if (!never_reset_tim[i]) {
@ -131,9 +133,10 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
bool tim_taken_f_mismatch = false;
bool var_freq_mismatch = false;
bool first_time_setup = true;
mcu_tim_pin_obj_t l_tim = {0};
for(uint i = 0; i < tim_num; i++) {
mcu_tim_pin_obj_t l_tim = mcu_tim_pin_list[i];
l_tim = mcu_tim_pin_list[i];
uint8_t l_tim_index = l_tim.tim_index-1;
uint8_t l_tim_channel = l_tim.channel_index-1;
@ -187,6 +190,9 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
}
}
mp_printf(&mp_plat_print, "X:%d Y:%d\n", TIMx, first_time_setup);
//OG
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(pin->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
@ -194,63 +200,195 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = self->tim->altfn_index;
HAL_GPIO_Init(pin_port(pin->port), &GPIO_InitStruct);
mp_printf(&mp_plat_print, "Good GPIO init\n");
// __HAL_RCC_GPIOA_CLK_ENABLE();
// /**TIM2 GPIO Configuration
// PA1 ------> TIM2_CH2
// */
// GPIO_InitTypeDef GPIO_InitStruct = {0};
// GPIO_InitStruct.Pin = GPIO_PIN_1;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
mp_printf(&mp_plat_print, "PWM Pin:%d Port:%d\n", pin->number, pin->port);
//OG
tim_clock_enable(1<<(self->tim->tim_index - 1));
//translate channel into handle value
//__HAL_RCC_TIM2_CLK_ENABLE();
mp_printf(&mp_plat_print, "Good clock init\n");
//OG
// //translate channel into handle value
self->channel = 4 * (self->tim->channel_index - 1);
uint32_t prescaler = 0; //prescaler is 15 bit
uint32_t period = 0; //period is 16 bit
timer_get_optimal_divisors(&period, &prescaler,frequency,timer_get_source_freq(self->tim->tim_index));
mp_printf(&mp_plat_print, "Per:%d presc:%d\n", period, prescaler);
//Timer init
self->handle.Instance = TIMx;
self->handle.Init.Period = period - 1;
self->handle.Init.Prescaler = prescaler - 1;
// uint32_t prescaler = 0; //prescaler is 15 bit
// uint32_t period = 0; //period is 16 bit
// timer_get_optimal_divisors(&period, &prescaler,frequency,timer_get_source_freq(self->tim->tim_index));
// //Timer init
self->handle.Instance = TIM2; //TIMx;
self->handle.Init.Period = 8749;//period - 1;
self->handle.Init.Prescaler = 0;//prescaler - 1;
self->handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
self->handle.Init.CounterMode = TIM_COUNTERMODE_UP;
self->handle.Init.RepetitionCounter = 0;
self->handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
//self->handle.Init.RepetitionCounter = 0;
//only run init if this is the first instance of this timer
if (first_time_setup) {
// if (first_time_setup) {
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("Could not initialize timer"));
}
// }
/* USER CODE END TIM2_Init 1 */
// TIM_HandleTypeDef htim2;
// htim2.Instance = TIM2;
// htim2.Init.Prescaler = 0;
// htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
// htim2.Init.Period = 8749;
// htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
// htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
// if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
// {
// mp_raise_ValueError(translate("Could not start PWM1"));
// }
//OG not needed
TIM_MasterConfigTypeDef sMasterConfig = {0};
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&self->handle, &sMasterConfig) != HAL_OK)
{
mp_raise_ValueError(translate("Internal Error"));
}
//Channel/PWM init
//OG
// //Channel/PWM init
//memset(&(self->chan_handle), 0, sizeof(TIM_OC_InitTypeDef)); //doesn't seem to affect it
self->chan_handle.OCNPolarity = 0;
self->chan_handle.OCIdleState = 0;
self->chan_handle.OCNIdleState = 0;
self->chan_handle.OCMode = TIM_OCMODE_PWM1;
self->chan_handle.Pulse = timer_get_internal_duty(duty, period);
self->chan_handle.OCPolarity = TIM_OCPOLARITY_LOW;
self->chan_handle.Pulse = 7000;
self->chan_handle.OCPolarity = TIM_OCPOLARITY_HIGH;
self->chan_handle.OCFastMode = TIM_OCFAST_DISABLE;
self->chan_handle.OCNPolarity = TIM_OCNPOLARITY_LOW; // needed for TIM1 and TIM8
self->chan_handle.OCIdleState = TIM_OCIDLESTATE_SET; // needed for TIM1 and TIM8
self->chan_handle.OCNIdleState = TIM_OCNIDLESTATE_SET; // needed for TIM1 and TIM8
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
//TIM_OC_InitTypeDef sConfigOC = self->chan_handle;
mp_printf(&mp_plat_print, "Complete");
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &(self->chan_handle), (uint32_t)self->channel) != HAL_OK)
{
mp_raise_ValueError(translate("Could not initialize channel"));
}
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
mp_raise_ValueError(translate("Could not start PWM"));
}
self->variable_frequency = variable_frequency;
self->frequency = frequency;
self->duty_cycle = duty;
self->period = period;
// TIM_OC_InitTypeDef sConfigOC = {0};
// sConfigOC.OCNPolarity = 0;
// sConfigOC.OCIdleState = 0;
// sConfigOC.OCNIdleState = 0;
// sConfigOC.OCMode = TIM_OCMODE_PWM1;
// sConfigOC.Pulse = 7000;
// sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
// sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
// if (HAL_TIM_PWM_ConfigChannel(&self->handle, &sConfigOC, self->channel) != HAL_OK)
// {
// mp_raise_ValueError(translate("Could not initialize channel"));
// }
//OG
// if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
// mp_raise_ValueError(translate("Could not start PWM"));
// }
//HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&self->handle, self->channel);
mp_printf(&mp_plat_print, "Complete");
// period, prescaler, timer_get_internal_duty(duty, period));
// mp_printf(&mp_plat_print, "SrcFreq: %d Per:%d presc:%d, doot:%d \n",timer_get_source_freq(self->tim->tim_index),
// period, prescaler, timer_get_internal_duty(duty, period));
// self->variable_frequency = variable_frequency;
// self->frequency = frequency;
// self->duty_cycle = duty;
// self->period = period;
//FULL REPLACE
// __HAL_RCC_TIM2_CLK_ENABLE();
// TIM_MasterConfigTypeDef sMasterConfig = {0};
// TIM_OC_InitTypeDef sConfigOC = {0};
// TIM_HandleTypeDef htim2;
// /* USER CODE BEGIN TIM2_Init 1 */
// /* USER CODE END TIM2_Init 1 */
// htim2.Instance = TIM2;
// htim2.Init.Prescaler = 0;
// htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
// htim2.Init.Period = 8749;
// htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
// htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
// if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
// {
// mp_raise_ValueError(translate("Could not start PWM1"));
// }
// sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
// sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
// if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
// {
// mp_raise_ValueError(translate("Could not start PWM2"));
// }
// sConfigOC.OCMode = TIM_OCMODE_PWM1;
// sConfigOC.Pulse = 7000;
// sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
// sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
// if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
// {
// mp_raise_ValueError(translate("Could not start PWM3"));
// }
// /* USER CODE BEGIN TIM2_Init 2 */
// /* USER CODE END TIM2_Init 2 */
// __HAL_RCC_GPIOA_CLK_ENABLE();
// /**TIM2 GPIO Configuration
// PA1 ------> TIM2_CH2
// */
// GPIO_InitTypeDef GPIO_InitStruct = {0};
// GPIO_InitStruct.Pin = GPIO_PIN_1;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
return PWMOUT_OK;
}
bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
mp_printf(&mp_plat_print, "Deinited\n");
return self->tim == mp_const_none;
}
void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
mp_printf(&mp_plat_print, "Deinit\n");
if (common_hal_pulseio_pwmout_deinited(self)) {
return;
}
@ -272,16 +410,19 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
}
void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
mp_printf(&mp_plat_print, "setduty\n");
uint32_t internal_duty_cycle = timer_get_internal_duty(duty, self->period);
__HAL_TIM_SET_COMPARE(&self->handle, self->channel, internal_duty_cycle);
self->duty_cycle = duty;
}
uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
mp_printf(&mp_plat_print, "getduty\n");
return self->duty_cycle;
}
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) {
mp_printf(&mp_plat_print, "setfreq\n");
//don't halt setup for the same frequency
if (frequency == self->frequency) return;
@ -316,10 +457,12 @@ void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_
}
uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {
mp_printf(&mp_plat_print, "getfreq\n");
return self->frequency;
}
bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
mp_printf(&mp_plat_print, "getvarfreq\n");
return self->variable_frequency;
}

View File

@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PERIPH_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PERIPH_H
//I2C
extern I2C_TypeDef * mcu_i2c_banks[3];
@ -54,4 +54,4 @@ extern const mcu_uart_rx_obj_t mcu_uart_rx_list[6];
TIM_TypeDef * mcu_tim_banks[TIM_BANK_ARRAY_LEN];
const mcu_tim_pin_obj_t mcu_tim_pin_list[TIM_PIN_ARRAY_LEN];
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PERIPH_H

View File

@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PINS_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PINS_H
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PINS_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PINS_H
//Pins in datasheet order: DocID026289 Rev 7 page 38. LQFP100 only
//pg 38
@ -118,4 +118,4 @@ extern const mcu_pin_obj_t pin_PE00;
extern const mcu_pin_obj_t pin_PE01;
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PINS_H
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F401XE_PINS_H

View File

@ -52,17 +52,6 @@ safe_mode_t port_init(void) {
tick_init();
board_init();
//Configure LED pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
return NO_SAFE_MODE;
}