Add interrupt
This commit is contained in:
parent
207b889fef
commit
4d6a5f8700
123 changed files with 1181239 additions and 1181234 deletions
|
@ -4,8 +4,8 @@
|
|||
#include "flash.h"
|
||||
#include "reg_cah.h"
|
||||
|
||||
#define CAN_TIMEOUT 50
|
||||
#define CAN_SILENCE_TIMEOUT 500
|
||||
#define CAN_TIMEOUT 5
|
||||
#define CAN_SILENCE_TIMEOUT 5
|
||||
#define MAX_CAN_READS 2
|
||||
extern FLASH_RECORD *flash_rec;
|
||||
extern volatile uint16_t msg_id;
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include "wiring_analog.h"
|
||||
#include "wiring_constants.h"
|
||||
// clang-format on
|
||||
|
||||
#include "hal_conf_extra.h"
|
||||
#include "reg_cah.h"
|
||||
#include "flash.h"
|
||||
#include "config.h"
|
||||
|
@ -61,9 +61,11 @@ MotorControlInputs motor_control_inputs;
|
|||
volatile uint8_t crc_h;
|
||||
volatile uint8_t crc_l;
|
||||
|
||||
volatile bool rx_can = false;
|
||||
CAN_message_t msg;
|
||||
|
||||
void setup(){
|
||||
SCB->VTOR = (volatile uint32_t)0x08008004;
|
||||
|
||||
Serial.setRx(HARDWARE_SERIAL_RX_PIN);
|
||||
Serial.setTx(HARDWARE_SERIAL_TX_PIN);
|
||||
Serial.begin(115200);
|
||||
|
@ -74,19 +76,13 @@ MotorControlInputs motor_control_inputs;
|
|||
// Can.enableMBInterrupts();
|
||||
Can.begin();
|
||||
Can.setBaudRate(1000000);
|
||||
// Настройка прерываний CAN
|
||||
CAN2->IER |= CAN_IER_FMPIE0;
|
||||
|
||||
flash_rec = load_params(); //for update write_ptr
|
||||
if(flash_rec[firmw].value == FIRMWARE_FLAG) NVIC_SystemReset(); //if in flash go to the bootloader
|
||||
|
||||
// Initialize FOC system
|
||||
setup_foc(&encoder, &motor, &driver, ¤t_sense,flash_rec);
|
||||
|
||||
CAN2->IER |= CAN_IER_FMPIE0 | // Сообщение в FIFO0
|
||||
CAN_IER_FFIE0 | // FIFO0 full
|
||||
CAN_IER_FOVIE0; // FIFO0 overflow
|
||||
|
||||
|
||||
// Default motor configuration
|
||||
GPIOC->ODR |= GPIO_ODR_OD11; //set LED
|
||||
motor.torque_controller = TorqueControlType::foc_current;
|
||||
|
@ -96,21 +92,28 @@ MotorControlInputs motor_control_inputs;
|
|||
}
|
||||
|
||||
bool is_can_busy() {
|
||||
return (CAN2->TSR & (CAN_TSR_TME0 | CAN_TSR_TME1 | CAN_TSR_TME2)) != 0x07;
|
||||
return (CAN2->TSR & (CAN_TSR_TME0 | CAN_TSR_TME1 | CAN_TSR_TME2)) == 0;
|
||||
}
|
||||
|
||||
void wait_for_can_tx_complete() {
|
||||
uint32_t start_time = HAL_GetTick();
|
||||
|
||||
while (!(CAN2->TSR & (CAN_TSR_TME0 | CAN_TSR_TME1 | CAN_TSR_TME2))) {
|
||||
if (HAL_GetTick() - start_time > 500) break;
|
||||
}
|
||||
CAN2->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
process_can_messages();
|
||||
|
||||
static uint32_t last_send = 0;
|
||||
if(!send_blocked && !is_can_busy() && (HAL_GetTick() - last_send >= 50)) {
|
||||
send_angle();
|
||||
send_velocity();
|
||||
last_send = HAL_GetTick();
|
||||
}
|
||||
|
||||
foc_step(&motor);
|
||||
HAL_Delay(1);
|
||||
send_angle();
|
||||
HAL_Delay(1);
|
||||
if(rx_can){
|
||||
Can.read(msg);
|
||||
listen_can(msg);
|
||||
rx_can = false;
|
||||
HAL_Delay(1);
|
||||
}
|
||||
|
||||
|
||||
foc_step(&motor);
|
||||
}
|
||||
|
|
|
@ -166,7 +166,6 @@ void process_can_messages() {
|
|||
// block send while data cant
|
||||
send_blocked = true;
|
||||
|
||||
// Чтение сообщений с ограничением
|
||||
while(count < MAX_CAN_READS && Can.read(msg)) {
|
||||
listen_can(msg);
|
||||
last_received = HAL_GetTick();
|
||||
|
@ -215,7 +214,9 @@ void listen_can(const CAN_message_t &msg) {
|
|||
/* Message Structure: 0x691
|
||||
69 - Device address
|
||||
1 - Action code */
|
||||
if(id_x == flash_rec[addr_id].value){
|
||||
if(id_x != flash_rec[addr_id].value){
|
||||
return;
|
||||
}
|
||||
if(msg_ch == REG_WRITE){
|
||||
switch(msg.buf[0]) {
|
||||
case REG_ID:
|
||||
|
@ -327,5 +328,6 @@ void listen_can(const CAN_message_t &msg) {
|
|||
|
||||
}
|
||||
}
|
||||
HAL_Delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue