stmbl/shared/common.h

164 lines
3.9 KiB
C
Raw Permalink Normal View History

2017-09-13 12:43:23 +00:00
/*
* This file is part of the stmbl project.
*
* Copyright (C) 2013-2017 Rene Hopf <renehopf@mac.com>
* Copyright (C) 2013-2017 Nico Stute <crinq@crinq.de>
*
* This program 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.
*
* This program 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/>.
*/
2015-07-20 20:48:18 +00:00
#pragma once
2015-10-10 21:39:08 +00:00
#include <stdint.h>
2018-03-20 23:45:27 +00:00
#include "stmbl_talk.h"
2015-07-20 20:48:18 +00:00
2016-01-09 22:45:27 +00:00
#if __GNUC__ < 5
2017-09-06 02:20:06 +00:00
#error gcc to old (< 5.0)
2016-01-09 22:45:27 +00:00
#endif
2017-09-06 02:20:06 +00:00
#define DATABAUD 3000000 //baudrate used for communication
2015-07-20 20:48:18 +00:00
2018-04-06 20:10:57 +00:00
#pragma pack(push, 1)
2018-03-20 23:45:27 +00:00
typedef struct {
stmbl_talk_header_t header;
uint32_t addr;
uint32_t value;
2018-04-06 23:36:17 +00:00
enum {
2018-04-06 22:38:21 +00:00
BOOTLOADER_OPCODE_NOP = 0,
BOOTLOADER_OPCODE_READ,
2018-04-06 23:36:17 +00:00
BOOTLOADER_OPCODE_WRITE, // MEM[addr] = value
BOOTLOADER_OPCODE_PAGEERASE, // clear PAGE[addr]
BOOTLOADER_OPCODE_CRCCHECK, // CRC(APP) == 0
2018-03-20 23:45:27 +00:00
} cmd : 8;
enum {
BOOTLOADER_STATE_OK = 0,
BOOTLOADER_STATE_NAK,
} state : 8;
uint16_t padding;
} packet_bootloader_t;
2018-04-11 23:31:58 +00:00
_Static_assert(sizeof(packet_bootloader_t) == 24, "packet_bootloader_t size error");
2018-03-20 23:45:27 +00:00
2017-09-13 12:43:23 +00:00
//process data from f3 to f4
2017-09-06 02:20:06 +00:00
typedef struct {
2018-03-21 01:40:27 +00:00
stmbl_talk_header_t header;
2017-09-06 02:20:06 +00:00
float d_fb;
float q_fb;
2018-03-21 01:40:27 +00:00
uint8_t fault;
uint8_t buf;
uint16_t padding;
2017-06-02 18:41:21 +00:00
} packet_from_hv_t;
2018-04-11 23:31:58 +00:00
_Static_assert(sizeof(packet_from_hv_t) == 24, "packet_from_hv_t size error");
2015-07-20 21:53:36 +00:00
2017-09-13 12:43:23 +00:00
//process data from f4 to f3
2017-09-06 02:20:06 +00:00
typedef struct {
2018-03-21 01:40:27 +00:00
stmbl_talk_header_t header;
2017-09-06 02:20:06 +00:00
float d_cmd;
float q_cmd;
float pos;
float vel;
union {
struct {
2018-04-06 22:38:21 +00:00
uint8_t buf;
2018-04-12 00:13:33 +00:00
uint32_t enable : 1;
2017-09-06 02:20:06 +00:00
enum packet_to_hv_cmd_type_t {
VOLT_MODE = 0,
CURRENT_MODE,
} cmd_type : 1;
enum packet_to_hv_phase_type_t {
PHASE_90_3PH = 0,
PHASE_90_4PH,
PHASE_120_3PH,
PHASE_180_2PH,
PHASE_180_3PH,
} phase_type : 3;
2018-04-12 00:13:33 +00:00
uint32_t ignore_fault_pin : 1;
2017-09-06 00:00:49 +00:00
} flags;
2018-03-21 01:40:27 +00:00
uint32_t padding;
2017-09-06 00:00:49 +00:00
};
2017-06-02 18:41:21 +00:00
} packet_to_hv_t;
2018-04-11 23:31:58 +00:00
_Static_assert(sizeof(packet_to_hv_t) == 32, "packet_to_hv_t size error");
2015-07-20 20:48:18 +00:00
2017-09-13 12:43:23 +00:00
//config data for f3
2017-01-20 22:44:58 +00:00
typedef union {
2017-09-06 02:20:06 +00:00
struct f3_config_data_temp {
float r;
float l;
float psi;
float cur_p;
float cur_i;
float cur_ff;
float cur_ind;
float max_y;
float max_cur;
2018-03-23 02:46:46 +00:00
float dac;
2017-09-06 02:20:06 +00:00
} pins;
float data[sizeof(struct f3_config_data_temp) / 4];
2017-01-20 22:44:58 +00:00
} f3_config_data_t;
2017-09-13 12:43:23 +00:00
//status reply from f3
2017-01-20 22:44:58 +00:00
typedef union {
2017-09-06 02:20:06 +00:00
struct f3_state_data_temp {
float u_fb;
float v_fb;
float w_fb;
float hv_temp;
float mot_temp;
float core_temp;
2018-03-21 01:40:27 +00:00
float dc_volt;
float pwm_volt;
2017-09-06 02:20:06 +00:00
float y;
} pins;
float data[sizeof(struct f3_state_data_temp) / 4];
2017-01-20 22:44:58 +00:00
} f3_state_data_t;
2018-04-06 20:10:57 +00:00
#pragma pack(pop)
2018-04-06 17:14:50 +00:00
//fault state
typedef enum {
DISABLED = 0,
ENABLED,
PHASING,
SOFT_FAULT,
HARD_FAULT,
LED_TEST,
} state_t;
//fault error codes
typedef enum {
NO_ERROR = 0,
CMD_ERROR,
MOT_FB_ERROR,
COM_FB_ERROR,
JOINT_FB_ERROR,
POS_ERROR,
SAT_ERROR,
MOT_TEMP_ERROR,
HV_CRC_ERROR,
HV_TIMEOUT_ERROR,
HV_TEMP_ERROR,
HV_VOLT_ERROR,
HV_FAULT_ERROR,
HV_CURRENT_OFFSET_FAULT,
HV_OVERCURRENT_RMS,
HV_OVERCURRENT_PEAK,
HV_OVERCURRENT_HW,
} fault_t;
2017-09-13 12:43:23 +00:00
//check if structs can be send at 5kHz with DATABAUD
2016-09-07 17:41:24 +00:00
_Static_assert(sizeof(packet_to_hv_t) <= DATABAUD / 11 / 5000 - 1 - 5, "to_hv struct to large");
_Static_assert(sizeof(packet_from_hv_t) <= DATABAUD / 11 / 5000 - 1 - 5, "from_hv struct to large");
2017-09-13 12:43:23 +00:00
//check if structs are word aligned
2017-06-02 18:41:21 +00:00
_Static_assert(!(sizeof(packet_to_hv_t) % 4), "to_hv struct not word aligned");
_Static_assert(!(sizeof(packet_from_hv_t) % 4), "from_hv struct not word aligned");