libpandac  0.0.0
A library designed for a comm.ai Panda.
pandadefinitions.h
1 /*
2  Author: Matt Bunting
3  Copyright (c) 2020 Arizona Board of Regents
4  All rights reserved.
5 
6  Permission is hereby granted, without written agreement and without
7  license or royalty fees, to use, copy, modify, and distribute this
8  software and its documentation for any purpose, provided that the
9  above copyright notice and the following two paragraphs appear in
10  all copies of this software.
11 
12  IN NO EVENT SHALL THE ARIZONA BOARD OF REGENTS BE LIABLE TO ANY PARTY
13  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
16  SUCH DAMAGE.
17 
18  THE ARIZONA BOARD OF REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER
21  IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION
22  TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 
24  */
25 
26 #ifndef PANDA_DEFINITIONS_H
27 #define PANDA_DEFINITIONS_H
28 
29 #define CAN_DEVICE_RX (0xFFFF) // for clearing CAN ring buffer
30 #define CAN_DEVICE_TX_1 (0x0000) // for clearing CAN ring buffer
31 #define CAN_DEVICE_TX_2 (0x0001) // for clearing CAN ring buffer
32 #define CAN_DEVICE_TX_3 (0x0002) // for clearing CAN ring buffer
33 #define CAN_DEVICE_TXGMLAN (0x0003) // for clearing CAN ring buffer
34 #define UART_DEVICE_GPS (1) // there are other UARTS, unsure if used
35 
36 #define UART_BUFFER_READ_LENGTH (64)
37 #define UART_BUFFER_WRITE_LENGTH (0x20)
38 
39 #define INIT_GPS_BAUD 9600
40 #define GPS_BAUD 460800
41 
42 // These are from the Panda python init:
43 // Request Types:
44 // The difference between out and in is determined by LIBUSB_ENDPOINT_DIR_MASK (0x80)
45 // The address is determeined by LIBUSB_ENDPOINT_ADDRESS_MASK (0x0F)
46 // i.e. from libusb:
51 #define REQUEST_TYPE_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE) // = 0xC0
52 //#define REQUEST_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
53 //#define REQUEST_TYPE_OUT 0xc0 // NOT EQUAL TO (LIBUSB_ENDPOINT_OUT | TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
54 // Not sure about this but it's what boardd.cc does, same as REQUEST_TYPE_IN
55 #define REQUEST_TYPE_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE) // = 0x40 <= ptyhon Panda class
56 //#define REQUEST_TYPE_WRITE 0x40 // Not sure of the difference why this is called vs REQUEST_TYPE_OUT
57 
58 // Endpoints:
59 // The difference between out and in is determined by LIBUSB_ENDPOINT_DIR_MASK (0x80)
60 // The address is determined by LIBUSB_ENDPOINT_ADDRESS_MASK (0x0F)
61 #define ENDPOINT_CAN_OUT 0x03 // = LIBUSB_ENDPOINT_OUT | 0x03 - Panda firmware: USBx_OUTEP(3)
62 #define ENDPOINT_CAN_IN 0x81 // = LIBUSB_ENDPOINT_IN | 0x01 - Panda firmware: USBx_INEP(1)
63 #define ENDPOINT_UART_OUT 0x02 // = LIBUSB_ENDPOINT_OUT | 0x02 - Panda firmware: USBx_OUTEP(2)
64 #define ENDPOINT_UART_IN // doesn't exist in Panda firmware as far as I can tell
65 
66 
67 #define REQUEST_RTC 0xa0 // Reading panda's built-in RTC
68 #define REQUEST_FAN_SPEED 0xb2 // for fan RPM. Not the EON, not sure what this is for
69 // The following definitions are interpreted from openpilot/panda/board/main.c:246 usb_cb_control_msg(
70 // Requests involving REQUEST_TYPE_OUT:
71 #define REQUEST_CAN_DEBUG 0xc0 // CAN debug information
72 #define REQUEST_HARDWARE 0xc1 // determine Panda model (black/grey/white/uno)
73 #define REQUEST_SERIAL 0xd0
74 #define REQUEST_BOOTLOADER 0xd1 // For flashing
75 #define REQUEST_CAN_HEALTH 0xd2 // Can get ignition info here
76 #define REQUEST_FIRMWARE_LOW 0xd3 // not in main.c
77 #define REQUEST_FIRMWARE_HIGH 0xd4 // not in main.c
78 #define REQUEST_VERSION 0xd6 //
79 #define REQUEST_RESET_ST 0xd8 //
80 #define REQUEST_ESP_POWER 0xd9 // For setting GPS power
81 #define REQUEST_ESP_RESET 0xda // Can put ESP into optional bootmode
82 #define REQUEST_GMLAN_CAN_ENABLE 0xdb // Set GMLAN (white/grey) or OBD CAN (black) multiplexing mode
83 #define REQUEST_SAFETY_MODE 0xdc // Maybe enabling writing to CAN bus? See below SAFETY_xxxxx definitions
84 #define REQUEST_CAN_FORWARDING 0xdd // eEnable CAN forwarding,wValue = Can Bus Num to forward from, wIndex = Can Bus Num to forward to
85 #define REQUEST_CAN_BAUD 0xde // CAN bitrate
86 #define REQUEST_LONG_CONTROLS 0xdf // Enable long controls, whatever that is
87 #define REQUEST_UART_READ 0xe0 // GPS reading
88 #define REQUEST_UART_BAUD_SMALL 0xe1 // Not needed, for simple buad rates. Use REQUEST_UART_BAUD
89 #define REQUEST_UART_PARITY 0xe2 // For GPS, default to 9600 8N1
90 #define REQUEST_UART_BAUD 0xe4 // For GPS, default to 9600 8N1
91 #define REQUEST_CAN_LOOPBACK 0xe5 // For testing CAN
92 #define REQUEST_USB_POWER_MODE 0xe6 // This deals with charging mode
93 #define REQUEST_USB_POWER_SAVE 0xe7 // Power save state enable?
94 #define REQUEST_K_LONE_PULSE 0xf0 // Pulses UART2, needed for Acura
95 #define REQUEST_CAN_RING_CLEAR 0xf1 // Clear CAN ring buffer
96 #define REQUEST_UART_RING_CLEAR 0xf2 // Clear UART ring buffer
97 #define REQUEST_HEARTBEAT 0xf3 //
98 
99 // Requests but for REQUEST_TYPE_WRITE
100 #define REQUEST_RTC_YEAR 0xa1 // For setting RTC
101 #define REQUEST_RTC_MONTH 0xa2
102 #define REQUEST_RTC_MDAY 0xa3
103 #define REQUEST_RTC_WDAY 0xa4 // Not needed, use REQUEST_RTC_MDAY
104 #define REQUEST_RTC_HOUR 0xa5
105 #define REQUEST_RTC_MINUTE 0xa6
106 #define REQUEST_RTC_SECONDS 0xa7
107 
108 // From openpilot/panda/board/safety:20
109 #define SAFETY_NOOUTPUT 0U
110 #define SAFETY_HONDA 1U
111 #define SAFETY_TOYOTA 2U
112 #define SAFETY_ELM327 3U
113 #define SAFETY_GM 4U
114 #define SAFETY_HONDA_BOSCH 5U
115 #define SAFETY_FORD 6U
116 #define SAFETY_CADILLAC 7U
117 #define SAFETY_HYUNDAI 8U
118 #define SAFETY_CHRYSLER 9U
119 #define SAFETY_TESLA 10U
120 #define SAFETY_SUBARU 11U
121 #define SAFETY_GM_PASSIVE 12U
122 #define SAFETY_MAZDA 13U
123 #define SAFETY_TOYOTA_IPAS 16U
124 #define SAFETY_ALLOUTPUT 17U
125 #define SAFETY_GM_ASCM 18U
126 
127 
128 // copied from harness.h from comma.ai panda firmware
129 #define HARNESS_STATUS_NC 0U
130 #define HARNESS_STATUS_NORMAL 1U
131 #define HARNESS_STATUS_FLIPPED 2U
132 
133 // copied from faults.h from comma.ai panda firmware
134 #define FAULT_STATUS_NONE 0U
135 #define FAULT_STATUS_TEMPORARY 1U
136 #define FAULT_STATUS_PERMANENT 2U
137 
138 #define FAULT_RELAY_MALFUNCTION (1U << 0)
139 #define FAULT_UNUSED_INTERRUPT_HANDLED (1U << 1)
140 #define FAULT_INTERRUPT_RATE_CAN_1 (1U << 2)
141 #define FAULT_INTERRUPT_RATE_CAN_2 (1U << 3)
142 #define FAULT_INTERRUPT_RATE_CAN_3 (1U << 4)
143 #define FAULT_INTERRUPT_RATE_TACH (1U << 5)
144 #define FAULT_INTERRUPT_RATE_GMLAN (1U << 6)
145 #define FAULT_INTERRUPT_RATE_INTERRUPTS (1U << 7)
146 #define FAULT_INTERRUPT_RATE_SPI_DMA (1U << 8)
147 #define FAULT_INTERRUPT_RATE_SPI_CS (1U << 9)
148 #define FAULT_INTERRUPT_RATE_UART_1 (1U << 10)
149 #define FAULT_INTERRUPT_RATE_UART_2 (1U << 11)
150 #define FAULT_INTERRUPT_RATE_UART_3 (1U << 12)
151 #define FAULT_INTERRUPT_RATE_UART_5 (1U << 13)
152 #define FAULT_INTERRUPT_RATE_UART_DMA (1U << 14)
153 #define FAULT_INTERRUPT_RATE_USB (1U << 15)
154 #define FAULT_INTERRUPT_RATE_TIM1 (1U << 16)
155 #define FAULT_INTERRUPT_RATE_TIM3 (1U << 17)
156 #define FAULT_REGISTER_DIVERGENT (1U << 18)
157 #define FAULT_INTERRUPT_RATE_KLINE_INIT (1U << 19)
158 #define FAULT_INTERRUPT_RATE_CLOCK_SOURCE (1U << 20)
159 #define FAULT_INTERRUPT_RATE_TIM9 (1U << 21)
160 
161 // copied from board_declarations.h from comma.ai panda firmware
162 // USB power modes (from cereal.log.health)
163 #define USB_POWER_NONE 0U
164 #define USB_POWER_CLIENT 1U
165 #define USB_POWER_CDP 2U
166 #define USB_POWER_DCP 3U
167 
168 // copied from power_saving.h from comma.ai panda firmware
169 #define POWER_SAVE_STATUS_DISABLED 0
170 #define POWER_SAVE_STATUS_ENABLED 1
171 
172 
173 // This order was copied from cereal based nt he boardd.cc code. The order however seems wrong.
174 // This is known to work, based on tests, for the white, grey and black pandas
175 enum {
176  HARDWARE_UNKNOWN=2, // I don't know what this is
177  HARDWARE_WHITE_PANDA=0, // based on experimentation
178  HARDWARE_GREY_PANDA=1, // based on experimentation
179  HARDWARE_BLACK_PANDA=3, // based on experimentation
180  HARDWARE_PEDAL=4, // I don't know what this is
181  HARDWARE_UNO=5, // I don't know what this is
182 };
183 
184 // copied from panda/board/main.c
185 //typedef struct __attribute__((packed)) _PandaHealth {
186 typedef struct __attribute__((packed)) _PandaHealth {
187  uint32_t uptime;
188  uint32_t voltage;
189  uint32_t current;
190  uint32_t can_rx_errs;
191  uint32_t can_send_errs;
192  uint32_t can_fwd_errs;
193  uint32_t gmlan_send_errs;
194  uint32_t faults;
195  uint8_t ignition_line;
196  uint8_t ignition_can;
197  uint8_t controls_allowed;
198  uint8_t gas_interceptor_detected;
199  uint8_t car_harness_status;
200  uint8_t usb_power_mode;
201  uint8_t safety_model;
202  uint8_t fault_status;
203  uint8_t power_save_enabled;
204 } PandaHealth;
205 
206 
207 #endif