Skip to content

Commit

Permalink
make ADC work
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelsadok committed May 15, 2018
1 parent 08fd408 commit 8339311
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Firmware/Board/v3/Src/gpio.c
Expand Up @@ -284,6 +284,7 @@ GPIO_TypeDef* get_gpio_port_by_pin(uint16_t GPIO_pin){
case 3: return GPIO_3_GPIO_Port; break;
case 4: return GPIO_4_GPIO_Port; break;
case 5: return GPIO_5_GPIO_Port; break;
default: return GPIO_1_GPIO_Port;
}
}

Expand All @@ -294,6 +295,7 @@ uint16_t get_gpio_pin_by_pin(uint16_t GPIO_pin){
case 3: return GPIO_3_Pin; break;
case 4: return GPIO_4_Pin; break;
case 5: return GPIO_5_Pin; break;
default: return GPIO_1_Pin;
}
}

Expand Down
20 changes: 20 additions & 0 deletions Firmware/Board/v3/Src/prev_board_ver/adc_V3_2.c
Expand Up @@ -2,6 +2,7 @@
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;
DMA_HandleTypeDef hdma_adc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
Expand Down Expand Up @@ -195,6 +196,25 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA2_Stream0;
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);

/* ADC1 interrupt Init */
HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
Expand Down
20 changes: 20 additions & 0 deletions Firmware/Board/v3/Src/prev_board_ver/adc_V3_4.c
Expand Up @@ -2,6 +2,7 @@
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;
DMA_HandleTypeDef hdma_adc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
Expand Down Expand Up @@ -194,6 +195,25 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA2_Stream0;
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);

/* ADC1 interrupt Init */
HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
Expand Down
2 changes: 1 addition & 1 deletion Firmware/MotorControl/axis.hpp
Expand Up @@ -26,7 +26,7 @@ struct AxisConfig_t {
bool startup_encoder_offset_calibration = false; //<! run encoder offset calibration after startup, skip otherwise
bool startup_closed_loop_control = false; //<! enable closed loop control after calibration/startup
bool startup_sensorless_control = false; //<! enable sensorless control after calibration/startup
bool enable_step_dir = true; //<! enable step/dir input after calibration
bool enable_step_dir = false; //<! enable step/dir input after calibration
// For M0 this has no effect if enable_uart is true

float counts_per_step = 2.0f;
Expand Down
17 changes: 17 additions & 0 deletions Firmware/MotorControl/main.cpp
Expand Up @@ -104,6 +104,23 @@ int odrive_main(void) {
// Load persistent configuration (or defaults)
load_configuration();

// Init general user ADC on some GPIOs.
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pin = GPIO_1_Pin;
HAL_GPIO_Init(GPIO_1_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_2_Pin;
HAL_GPIO_Init(GPIO_2_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_3_Pin;
HAL_GPIO_Init(GPIO_3_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_4_Pin;
HAL_GPIO_Init(GPIO_4_GPIO_Port, &GPIO_InitStruct);
#if HW_VERSION_MAJOR == 3 && HW_VERSION_MAJOR >= 5
GPIO_InitStruct.Pin = GPIO_5_Pin;
HAL_GPIO_Init(GPIO_5_GPIO_Port, &GPIO_InitStruct);
#endif

// Construct all objects.
for (size_t i = 0; i < AXIS_COUNT; ++i) {
Encoder *encoder = new Encoder(hw_configs[i].encoder_config,
Expand Down
6 changes: 3 additions & 3 deletions Firmware/communication/communication.cpp
Expand Up @@ -18,7 +18,7 @@
//#include <usbd_cdc_if.h>
//#include <usb_device.h>
//#include <usart.h>
//#include <gpio.h>
#include <gpio.h>

#include <type_traits>

Expand Down Expand Up @@ -92,7 +92,7 @@ class StaticFunctions {
void NVIC_SystemReset_helper() { NVIC_SystemReset(); }
void enter_dfu_mode_helper() { enter_dfu_mode(); }
float get_oscilloscope_val(uint32_t index) { return oscilloscope[index]; }
//float get_adc_voltage_(uint32_t gpio) { return get_adc_voltage(gpio_port(gpio), gpio_pin(gpio)); }
float get_adc_voltage_(uint32_t gpio) { return get_adc_voltage(get_gpio_port_by_pin(gpio), get_gpio_pin_by_pin(gpio)); }
int32_t test_function(int32_t delta) { static int cnt = 0; return cnt += delta; }
} static_functions;

Expand Down Expand Up @@ -140,7 +140,7 @@ static inline auto make_obj_tree() {
make_protocol_property("test_property", &test_property),
make_protocol_function("test_function", static_functions, &StaticFunctions::test_function, "delta"),
make_protocol_function("get_oscilloscope_val", static_functions, &StaticFunctions::get_oscilloscope_val, "index"),
//make_protocol_function("get_adc_voltage", static_functions, &StaticFunctions::get_adc_voltage_, "channel"),
make_protocol_function("get_adc_voltage", static_functions, &StaticFunctions::get_adc_voltage_, "gpio"),
make_protocol_function("save_configuration", static_functions, &StaticFunctions::save_configuration_helper),
make_protocol_function("erase_configuration", static_functions, &StaticFunctions::erase_configuration_helper),
make_protocol_function("reboot", static_functions, &StaticFunctions::NVIC_SystemReset_helper),
Expand Down

0 comments on commit 8339311

Please sign in to comment.