2022-02-19 06:37:12 -08:00
|
|
|
/*
|
|
|
|
Copyright 2016 - 2022 Benjamin Vedder benjamin@vedder.se
|
|
|
|
Copyright 2022 Marcos Chaparro mchaparro@powerdesigns.ca
|
|
|
|
Copyright 2022 Jakub Tomczak
|
|
|
|
|
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
#include "enc_mt6816.h"
|
2021-12-14 07:40:40 -08:00
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "stm32f4xx_conf.h"
|
|
|
|
#include "hw.h"
|
|
|
|
#include "mc_interface.h"
|
|
|
|
#include "utils.h"
|
2021-12-26 09:44:33 -08:00
|
|
|
#include "spi_bb.h"
|
2021-12-14 07:40:40 -08:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define MT6816_NO_MAGNET_ERROR_MASK 0x0002
|
|
|
|
|
2022-01-20 12:23:37 -08:00
|
|
|
static MT6816_config_t MT6816_config_now = { 0 };
|
2021-12-14 07:40:40 -08:00
|
|
|
|
|
|
|
static float spi_error_rate = 0.0;
|
|
|
|
static float encoder_no_magnet_error_rate = 0.0;
|
|
|
|
static float encoder_no_magnet_error_cnt = 0.0;
|
|
|
|
static float last_enc_angle = 0.0;
|
|
|
|
static uint32_t spi_error_cnt = 0;
|
|
|
|
static uint32_t spi_val = 0;
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
encoder_ret_t enc_mt6816_init(MT6816_config_t *mt6816_config) {
|
2022-02-19 14:28:58 -08:00
|
|
|
if (mt6816_config->spi_dev == NULL) {
|
|
|
|
return ENCODER_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:23:37 -08:00
|
|
|
MT6816_config_now = *mt6816_config;
|
2021-12-26 09:44:33 -08:00
|
|
|
|
2022-02-19 14:28:58 -08:00
|
|
|
palSetPadMode(MT6816_config_now.sck_gpio, MT6816_config_now.sck_pin,
|
2021-12-15 05:17:43 -08:00
|
|
|
PAL_MODE_ALTERNATE(6) | PAL_STM32_OSPEED_HIGHEST);
|
2022-02-19 14:28:58 -08:00
|
|
|
palSetPadMode(MT6816_config_now.miso_gpio, MT6816_config_now.miso_pin,
|
2021-12-15 05:17:43 -08:00
|
|
|
PAL_MODE_ALTERNATE(6) | PAL_STM32_OSPEED_HIGHEST);
|
2021-12-26 09:44:33 -08:00
|
|
|
|
2022-02-19 14:28:58 -08:00
|
|
|
palSetPadMode(MT6816_config_now.nss_gpio, MT6816_config_now.nss_pin,
|
|
|
|
PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
|
2021-12-14 07:40:40 -08:00
|
|
|
|
2022-02-19 14:28:58 -08:00
|
|
|
palSetPadMode(MT6816_config_now.mosi_gpio, MT6816_config_now.mosi_pin,
|
|
|
|
PAL_MODE_ALTERNATE(6) | PAL_STM32_OSPEED_HIGHEST);
|
2021-12-17 07:18:07 -08:00
|
|
|
|
2022-02-19 14:28:58 -08:00
|
|
|
spiStart(MT6816_config_now.spi_dev, &(MT6816_config_now.hw_spi_cfg));
|
2021-12-14 07:40:40 -08:00
|
|
|
|
2021-12-15 05:17:43 -08:00
|
|
|
nvicEnableVector(HW_ENC_TIM_ISR_CH, 6);
|
|
|
|
spi_error_rate = 0.0;
|
|
|
|
encoder_no_magnet_error_rate = 0.0;
|
2021-12-14 07:40:40 -08:00
|
|
|
|
2022-01-09 08:10:40 -08:00
|
|
|
return ENCODER_OK;
|
2022-02-19 14:28:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void enc_mt6816_deinit(void) {
|
|
|
|
if (MT6816_config_now.spi_dev == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
palSetPadMode(MT6816_config_now.miso_gpio,
|
|
|
|
MT6816_config_now.miso_pin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
palSetPadMode(MT6816_config_now.sck_gpio,
|
|
|
|
MT6816_config_now.sck_pin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
palSetPadMode(MT6816_config_now.nss_gpio,
|
|
|
|
MT6816_config_now.nss_pin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
|
|
|
|
palSetPadMode(MT6816_config_now.mosi_gpio, MT6816_config_now.mosi_pin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
|
|
|
|
spiStop(MT6816_config_now.spi_dev);
|
|
|
|
|
|
|
|
last_enc_angle = 0.0;
|
|
|
|
spi_error_rate = 0.0;
|
2021-12-14 07:40:40 -08:00
|
|
|
}
|
2021-12-20 05:18:51 -08:00
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
float enc_mt6816_read_deg(void) {
|
2021-12-20 05:18:51 -08:00
|
|
|
return last_enc_angle;
|
|
|
|
}
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
void enc_mt6816_routine(float rate) {
|
2021-12-15 05:17:43 -08:00
|
|
|
uint16_t pos;
|
|
|
|
uint16_t reg_data_03;
|
|
|
|
uint16_t reg_data_04;
|
|
|
|
uint16_t reg_addr_03 = 0x8300;
|
|
|
|
uint16_t reg_addr_04 = 0x8400;
|
|
|
|
|
2022-02-19 14:28:58 -08:00
|
|
|
#define SPI_BEGIN() spi_bb_delay(); palClearPad(MT6816_config_now.nss_gpio, MT6816_config_now.nss_pin); spi_bb_delay();
|
|
|
|
#define SPI_END() spi_bb_delay(); palSetPad(MT6816_config_now.nss_gpio, MT6816_config_now.nss_pin); spi_bb_delay();
|
|
|
|
|
|
|
|
SPI_BEGIN();
|
|
|
|
reg_data_03 = spiPolledExchange(MT6816_config_now.spi_dev, reg_addr_03);
|
|
|
|
SPI_END();
|
2021-12-26 09:44:33 -08:00
|
|
|
spi_bb_delay();
|
2022-02-19 14:28:58 -08:00
|
|
|
SPI_BEGIN();
|
|
|
|
reg_data_04 = spiPolledExchange(MT6816_config_now.spi_dev, reg_addr_04);
|
|
|
|
SPI_END();
|
2021-12-15 05:17:43 -08:00
|
|
|
|
|
|
|
pos = (reg_data_03 << 8) | reg_data_04;
|
|
|
|
spi_val = pos;
|
|
|
|
|
2021-12-26 09:44:33 -08:00
|
|
|
if (spi_bb_check_parity(pos)) {
|
2021-12-15 05:17:43 -08:00
|
|
|
if (pos & MT6816_NO_MAGNET_ERROR_MASK) {
|
|
|
|
++encoder_no_magnet_error_cnt;
|
2022-02-19 12:00:52 -08:00
|
|
|
UTILS_LP_FAST(encoder_no_magnet_error_rate, 1.0, 1.0 / rate);
|
2021-12-15 05:17:43 -08:00
|
|
|
} else {
|
|
|
|
pos = pos >> 2;
|
|
|
|
last_enc_angle = ((float) pos * 360.0) / 16384.0;
|
2022-02-19 12:00:52 -08:00
|
|
|
UTILS_LP_FAST(spi_error_rate, 0.0, 1.0 / rate);
|
|
|
|
UTILS_LP_FAST(encoder_no_magnet_error_rate, 0.0, 1.0 / rate);
|
2021-12-15 05:17:43 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++spi_error_cnt;
|
2022-02-19 12:00:52 -08:00
|
|
|
UTILS_LP_FAST(spi_error_rate, 1.0, 1.0 / rate);
|
2021-12-15 05:17:43 -08:00
|
|
|
}
|
2021-12-14 07:40:40 -08:00
|
|
|
}
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
uint32_t enc_mt6816_spi_get_val(void) {
|
2021-12-14 07:40:40 -08:00
|
|
|
return spi_val;
|
|
|
|
}
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
uint32_t enc_mt6816_spi_get_error_cnt(void) {
|
2021-12-14 07:40:40 -08:00
|
|
|
return spi_error_cnt;
|
|
|
|
}
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
uint32_t enc_mt6816_get_no_magnet_error_cnt(void) {
|
2021-12-15 05:17:43 -08:00
|
|
|
return encoder_no_magnet_error_cnt;
|
2021-12-14 07:40:40 -08:00
|
|
|
}
|
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
uint32_t enc_mt6816_get_no_magnet_error_rate(void) {
|
2021-12-15 05:17:43 -08:00
|
|
|
return encoder_no_magnet_error_rate;
|
2021-12-14 07:40:40 -08:00
|
|
|
}
|