diff --git a/panda/board/boards/black.h b/panda/board/boards/black.h index 133c11fb..3f198179 100644 --- a/panda/board/boards/black.h +++ b/panda/board/boards/black.h @@ -26,33 +26,6 @@ static void black_enable_can_transceiver(uint8_t transceiver, bool enabled) { } } -static void black_enable_can_transceivers(bool enabled) { - for(uint8_t i=1U; i<=4U; i++){ - // Leave main CAN always on for CAN-based ignition detection - if((harness.status == HARNESS_STATUS_FLIPPED) ? (i == 3U) : (i == 1U)){ - black_enable_can_transceiver(i, true); - } else { - black_enable_can_transceiver(i, enabled); - } - } -} - -static void black_set_led(uint8_t color, bool enabled) { - switch (color){ - case LED_RED: - set_gpio_output(GPIOC, 9, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 6, !enabled); - break; - default: - break; - } -} - static void black_set_usb_load_switch(bool enabled) { set_gpio_output(GPIOB, 1, !enabled); } @@ -102,41 +75,12 @@ static void black_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - // GPS OFF set_gpio_output(GPIOC, 5, 0); set_gpio_output(GPIOC, 12, 0); - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - // Turn on USB load switch. black_set_usb_load_switch(true); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - black_enable_can_transceivers(true); - - // Disable LEDs - black_set_led(LED_RED, false); - black_set_led(LED_GREEN, false); - black_set_led(LED_BLUE, false); - - // Set normal CAN mode - black_set_can_mode(CAN_MODE_NORMAL); } static void black_init_bootloader(void) { @@ -162,7 +106,6 @@ static harness_configuration black_harness_config = { board board_black = { .set_bootkick = unused_set_bootkick, .harness_config = &black_harness_config, - .has_obd = true, .has_spi = false, .has_canfd = false, .fan_max_rpm = 0U, @@ -173,8 +116,8 @@ board board_black = { .init = black_init, .init_bootloader = black_init_bootloader, .enable_can_transceiver = black_enable_can_transceiver, - .enable_can_transceivers = black_enable_can_transceivers, - .set_led = black_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .set_can_mode = black_set_can_mode, .check_ignition = black_check_ignition, .read_voltage_mV = white_read_voltage_mV, diff --git a/panda/board/boards/board_declarations.h b/panda/board/boards/board_declarations.h index 61e9ce2d..f96812e9 100644 --- a/panda/board/boards/board_declarations.h +++ b/panda/board/boards/board_declarations.h @@ -13,8 +13,6 @@ typedef enum { typedef void (*board_init)(void); typedef void (*board_init_bootloader)(void); typedef void (*board_enable_can_transceiver)(uint8_t transceiver, bool enabled); -typedef void (*board_enable_can_transceivers)(bool enabled); -typedef void (*board_set_led)(uint8_t color, bool enabled); typedef void (*board_set_can_mode)(uint8_t mode); typedef bool (*board_check_ignition)(void); typedef uint32_t (*board_read_voltage_mV)(void); @@ -28,7 +26,8 @@ typedef void (*board_set_amp_enabled)(bool enabled); struct board { harness_configuration *harness_config; - const bool has_obd; + GPIO_TypeDef * const led_GPIO[3]; + const uint8_t led_pin[3]; const bool has_spi; const bool has_canfd; const uint16_t fan_max_rpm; @@ -39,8 +38,6 @@ struct board { board_init init; board_init_bootloader init_bootloader; board_enable_can_transceiver enable_can_transceiver; - board_enable_can_transceivers enable_can_transceivers; - board_set_led set_led; board_set_can_mode set_can_mode; board_check_ignition check_ignition; board_read_voltage_mV read_voltage_mV; @@ -67,11 +64,6 @@ struct board { #define HW_TYPE_TRES 9U #define HW_TYPE_CUATRO 10U -// LED colors -#define LED_RED 0U -#define LED_GREEN 1U -#define LED_BLUE 2U - // USB power modes (from cereal.log.health) #define USB_POWER_NONE 0U #define USB_POWER_CLIENT 1U diff --git a/panda/board/boards/cuatro.h b/panda/board/boards/cuatro.h index 3d630f02..4407903b 100644 --- a/panda/board/boards/cuatro.h +++ b/panda/board/boards/cuatro.h @@ -6,22 +6,6 @@ // Cuatro (STM32H7) + Harness // // ////////////////////////// // -static void cuatro_set_led(uint8_t color, bool enabled) { - switch (color) { - case LED_RED: - set_gpio_output(GPIOC, 6, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 9, !enabled); - break; - default: - break; - } -} - static void cuatro_enable_can_transceiver(uint8_t transceiver, bool enabled) { switch (transceiver) { case 1U: @@ -41,18 +25,6 @@ static void cuatro_enable_can_transceiver(uint8_t transceiver, bool enabled) { } } -static void cuatro_enable_can_transceivers(bool enabled) { - uint8_t main_bus = (harness.status == HARNESS_STATUS_FLIPPED) ? 3U : 1U; - for (uint8_t i=1U; i<=4U; i++) { - // Leave main CAN always on for CAN-based ignition detection - if (i == main_bus) { - cuatro_enable_can_transceiver(i, true); - } else { - cuatro_enable_can_transceiver(i, enabled); - } - } -} - static uint32_t cuatro_read_voltage_mV(void) { return adc_get_mV(8) * 11U; } @@ -68,7 +40,7 @@ static void cuatro_set_fan_enabled(bool enabled) { static void cuatro_set_bootkick(BootState state) { set_gpio_output(GPIOA, 0, state != BOOT_BOOTKICK); // TODO: confirm we need this - set_gpio_output(GPIOC, 12, state != BOOT_RESET); + //set_gpio_output(GPIOC, 12, state != BOOT_RESET); } static void cuatro_set_amp_enabled(bool enabled){ @@ -76,14 +48,9 @@ static void cuatro_set_amp_enabled(bool enabled){ } static void cuatro_init(void) { - red_chiplet_init(); + common_init_gpio(); - // init LEDs as open drain - set_gpio_output_type(GPIOC, 6, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 7, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 9, OUTPUT_TYPE_OPEN_DRAIN); - - // more open drain + // open drain set_gpio_output_type(GPIOD, 3, OUTPUT_TYPE_OPEN_DRAIN); // FAN_EN set_gpio_output_type(GPIOC, 12, OUTPUT_TYPE_OPEN_DRAIN); // VBAT_EN @@ -114,9 +81,6 @@ static void cuatro_init(void) { gpio_uart7_init(); uart_init(&uart_ring_som_debug, 115200); - // SPI init - gpio_spi_init(); - // fan setup set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT8); // open drain @@ -142,8 +106,7 @@ static void cuatro_init(void) { } board board_cuatro = { - .harness_config = &red_chiplet_harness_config, - .has_obd = true, + .harness_config = &tres_harness_config, .has_spi = true, .has_canfd = true, .fan_max_rpm = 12500U, @@ -154,9 +117,9 @@ board board_cuatro = { .init = cuatro_init, .init_bootloader = unused_init_bootloader, .enable_can_transceiver = cuatro_enable_can_transceiver, - .enable_can_transceivers = cuatro_enable_can_transceivers, - .set_led = cuatro_set_led, - .set_can_mode = red_chiplet_set_can_mode, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {6, 7, 9}, + .set_can_mode = tres_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = cuatro_read_voltage_mV, .read_current_mA = cuatro_read_current_mA, diff --git a/panda/board/boards/dos.h b/panda/board/boards/dos.h index d96cee4c..11cc8b5d 100644 --- a/panda/board/boards/dos.h +++ b/panda/board/boards/dos.h @@ -26,33 +26,6 @@ static void dos_enable_can_transceiver(uint8_t transceiver, bool enabled) { } } -static void dos_enable_can_transceivers(bool enabled) { - for(uint8_t i=1U; i<=4U; i++){ - // Leave main CAN always on for CAN-based ignition detection - if((harness.status == HARNESS_STATUS_FLIPPED) ? (i == 3U) : (i == 1U)){ - dos_enable_can_transceiver(i, true); - } else { - dos_enable_can_transceiver(i, enabled); - } - } -} - -static void dos_set_led(uint8_t color, bool enabled) { - switch (color){ - case LED_RED: - set_gpio_output(GPIOC, 9, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 6, !enabled); - break; - default: - break; - } -} - static void dos_set_bootkick(BootState state) { set_gpio_output(GPIOC, 4, state != BOOT_BOOTKICK); } @@ -117,25 +90,6 @@ static void dos_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - -#ifdef ENABLE_SPI - // SPI init - gpio_spi_init(); -#endif - // C8: FAN PWM aka TIM3_CH3 set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -148,24 +102,9 @@ static void dos_init(void) { pwm_init(TIM4, 2); dos_set_ir_power(0U); - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - dos_enable_can_transceivers(true); - - // Disable LEDs - dos_set_led(LED_RED, false); - dos_set_led(LED_GREEN, false); - dos_set_led(LED_BLUE, false); - // Bootkick dos_set_bootkick(true); - // Set normal CAN mode - dos_set_can_mode(CAN_MODE_NORMAL); - // Init clock source (camera strobe) using PWM clock_source_init(); } @@ -186,7 +125,6 @@ static harness_configuration dos_harness_config = { board board_dos = { .harness_config = &dos_harness_config, - .has_obd = true, #ifdef ENABLE_SPI .has_spi = true, #else @@ -201,8 +139,8 @@ board board_dos = { .init = dos_init, .init_bootloader = unused_init_bootloader, .enable_can_transceiver = dos_enable_can_transceiver, - .enable_can_transceivers = dos_enable_can_transceivers, - .set_led = dos_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .set_can_mode = dos_set_can_mode, .check_ignition = dos_check_ignition, .read_voltage_mV = white_read_voltage_mV, diff --git a/panda/board/boards/grey.h b/panda/board/boards/grey.h index b3e40bc6..6ba07b4d 100644 --- a/panda/board/boards/grey.h +++ b/panda/board/boards/grey.h @@ -11,7 +11,6 @@ board board_grey = { .set_bootkick = unused_set_bootkick, .harness_config = &white_harness_config, - .has_obd = false, .has_spi = false, .has_canfd = false, .fan_max_rpm = 0U, @@ -22,8 +21,8 @@ board board_grey = { .init = white_grey_init, .init_bootloader = white_grey_init_bootloader, .enable_can_transceiver = white_enable_can_transceiver, - .enable_can_transceivers = white_enable_can_transceivers, - .set_led = white_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .set_can_mode = white_set_can_mode, .check_ignition = white_check_ignition, .read_voltage_mV = white_read_voltage_mV, diff --git a/panda/board/boards/red.h b/panda/board/boards/red.h index f1d8d9c4..9871f959 100644 --- a/panda/board/boards/red.h +++ b/panda/board/boards/red.h @@ -25,34 +25,6 @@ static void red_enable_can_transceiver(uint8_t transceiver, bool enabled) { } } -static void red_enable_can_transceivers(bool enabled) { - uint8_t main_bus = (harness.status == HARNESS_STATUS_FLIPPED) ? 3U : 1U; - for (uint8_t i=1U; i<=4U; i++) { - // Leave main CAN always on for CAN-based ignition detection - if (i == main_bus) { - red_enable_can_transceiver(i, true); - } else { - red_enable_can_transceiver(i, enabled); - } - } -} - -static void red_set_led(uint8_t color, bool enabled) { - switch (color) { - case LED_RED: - set_gpio_output(GPIOE, 4, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOE, 3, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOE, 2, !enabled); - break; - default: - break; - } -} - static void red_set_can_mode(uint8_t mode) { red_enable_can_transceiver(2U, false); red_enable_can_transceiver(4U, false); @@ -107,17 +79,6 @@ static uint32_t red_read_voltage_mV(void){ static void red_init(void) { common_init_gpio(); - //C10,C11 : OBD_SBU1_RELAY, OBD_SBU2_RELAY - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOC, 10, PULL_NONE); - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_output(GPIOC, 10, 1); - - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOC, 11, PULL_NONE); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output(GPIOC, 11, 1); - // G11,B3,D7,B4: transceiver enable set_gpio_pullup(GPIOG, 11, PULL_NONE); set_gpio_mode(GPIOG, 11, MODE_OUTPUT); @@ -140,21 +101,6 @@ static void red_init(void) { set_gpio_pullup(GPIOB, 14, PULL_UP); set_gpio_mode(GPIOB, 14, MODE_OUTPUT); set_gpio_output(GPIOB, 14, 1); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - red_enable_can_transceivers(true); - - // Disable LEDs - red_set_led(LED_RED, false); - red_set_led(LED_GREEN, false); - red_set_led(LED_BLUE, false); - - // Set normal CAN mode - red_set_can_mode(CAN_MODE_NORMAL); } static harness_configuration red_harness_config = { @@ -174,7 +120,6 @@ static harness_configuration red_harness_config = { board board_red = { .set_bootkick = unused_set_bootkick, .harness_config = &red_harness_config, - .has_obd = true, .has_spi = false, .has_canfd = true, .fan_max_rpm = 0U, @@ -185,8 +130,8 @@ board board_red = { .init = red_init, .init_bootloader = unused_init_bootloader, .enable_can_transceiver = red_enable_can_transceiver, - .enable_can_transceivers = red_enable_can_transceivers, - .set_led = red_set_led, + .led_GPIO = {GPIOE, GPIOE, GPIOE}, + .led_pin = {4, 3, 2}, .set_can_mode = red_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = red_read_voltage_mV, diff --git a/panda/board/boards/tres.h b/panda/board/boards/tres.h index f0602ae6..585487b2 100644 --- a/panda/board/boards/tres.h +++ b/panda/board/boards/tres.h @@ -9,7 +9,7 @@ static bool tres_ir_enabled; static bool tres_fan_enabled; static void tres_update_fan_ir_power(void) { - red_chiplet_set_fan_or_usb_load_switch(tres_ir_enabled || tres_fan_enabled); + set_gpio_output(GPIOD, 3, tres_ir_enabled || tres_fan_enabled); } static void tres_set_ir_power(uint8_t percentage){ @@ -29,6 +29,67 @@ static void tres_set_fan_enabled(bool enabled) { tres_update_fan_ir_power(); } +static void tres_enable_can_transceiver(uint8_t transceiver, bool enabled) { + switch (transceiver) { + case 1U: + set_gpio_output(GPIOG, 11, !enabled); + break; + case 2U: + set_gpio_output(GPIOB, 10, !enabled); + break; + case 3U: + set_gpio_output(GPIOD, 7, !enabled); + break; + case 4U: + set_gpio_output(GPIOB, 11, !enabled); + break; + default: + break; + } +} + +static void tres_set_can_mode(uint8_t mode) { + current_board->enable_can_transceiver(2U, false); + current_board->enable_can_transceiver(4U, false); + switch (mode) { + case CAN_MODE_NORMAL: + case CAN_MODE_OBD_CAN2: + if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { + // B12,B13: disable normal mode + set_gpio_pullup(GPIOB, 12, PULL_NONE); + set_gpio_mode(GPIOB, 12, MODE_ANALOG); + + set_gpio_pullup(GPIOB, 13, PULL_NONE); + set_gpio_mode(GPIOB, 13, MODE_ANALOG); + + // B5,B6: FDCAN2 mode + set_gpio_pullup(GPIOB, 5, PULL_NONE); + set_gpio_alternate(GPIOB, 5, GPIO_AF9_FDCAN2); + + set_gpio_pullup(GPIOB, 6, PULL_NONE); + set_gpio_alternate(GPIOB, 6, GPIO_AF9_FDCAN2); + current_board->enable_can_transceiver(2U, true); + } else { + // B5,B6: disable normal mode + set_gpio_pullup(GPIOB, 5, PULL_NONE); + set_gpio_mode(GPIOB, 5, MODE_ANALOG); + + set_gpio_pullup(GPIOB, 6, PULL_NONE); + set_gpio_mode(GPIOB, 6, MODE_ANALOG); + // B12,B13: FDCAN2 mode + set_gpio_pullup(GPIOB, 12, PULL_NONE); + set_gpio_alternate(GPIOB, 12, GPIO_AF9_FDCAN2); + + set_gpio_pullup(GPIOB, 13, PULL_NONE); + set_gpio_alternate(GPIOB, 13, GPIO_AF9_FDCAN2); + current_board->enable_can_transceiver(4U, true); + } + break; + default: + break; + } +} + static bool tres_read_som_gpio (void) { return (get_gpio_input(GPIOC, 2) != 0); } @@ -39,7 +100,7 @@ static void tres_init(void) { register_set_bits(&(PWR->CR3), PWR_CR3_USB33DEN); while ((PWR->CR3 & PWR_CR3_USB33RDY) == 0U); - red_chiplet_init(); + common_init_gpio(); // C2: SOM GPIO used as input (fan control at boot) set_gpio_mode(GPIOC, 2, MODE_INPUT); @@ -54,9 +115,6 @@ static void tres_init(void) { gpio_uart7_init(); uart_init(&uart_ring_som_debug, 115200); - // SPI init - gpio_spi_init(); - // fan setup set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -74,9 +132,22 @@ static void tres_init(void) { clock_source_init(); } +static harness_configuration tres_harness_config = { + .has_harness = true, + .GPIO_SBU1 = GPIOC, + .GPIO_SBU2 = GPIOA, + .GPIO_relay_SBU1 = GPIOA, + .GPIO_relay_SBU2 = GPIOA, + .pin_SBU1 = 4, + .pin_SBU2 = 1, + .pin_relay_SBU1 = 8, + .pin_relay_SBU2 = 3, + .adc_channel_SBU1 = 4, // ADC12_INP4 + .adc_channel_SBU2 = 17 // ADC1_INP17 +}; + board board_tres = { - .harness_config = &red_chiplet_harness_config, - .has_obd = true, + .harness_config = &tres_harness_config, .has_spi = true, .has_canfd = true, .fan_max_rpm = 6600U, @@ -86,10 +157,10 @@ board board_tres = { .fan_enable_cooldown_time = 3U, .init = tres_init, .init_bootloader = unused_init_bootloader, - .enable_can_transceiver = red_chiplet_enable_can_transceiver, - .enable_can_transceivers = red_chiplet_enable_can_transceivers, - .set_led = red_set_led, - .set_can_mode = red_chiplet_set_can_mode, + .enable_can_transceiver = tres_enable_can_transceiver, + .led_GPIO = {GPIOE, GPIOE, GPIOE}, + .led_pin = {4, 3, 2}, + .set_can_mode = tres_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = red_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/panda/board/boards/uno.h b/panda/board/boards/uno.h index d1728f1f..fd71460a 100644 --- a/panda/board/boards/uno.h +++ b/panda/board/boards/uno.h @@ -26,33 +26,6 @@ static void uno_enable_can_transceiver(uint8_t transceiver, bool enabled) { } } -static void uno_enable_can_transceivers(bool enabled) { - for(uint8_t i=1U; i<=4U; i++){ - // Leave main CAN always on for CAN-based ignition detection - if((harness.status == HARNESS_STATUS_FLIPPED) ? (i == 3U) : (i == 1U)){ - uno_enable_can_transceiver(i, true); - } else { - uno_enable_can_transceiver(i, enabled); - } - } -} - -static void uno_set_led(uint8_t color, bool enabled) { - switch (color){ - case LED_RED: - set_gpio_output(GPIOC, 9, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 6, !enabled); - break; - default: - break; - } -} - static void uno_set_bootkick(BootState state) { if (state == BOOT_BOOTKICK) { set_gpio_output(GPIOB, 14, false); @@ -118,25 +91,11 @@ static void uno_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - // GPS off set_gpio_output(GPIOB, 1, 0); set_gpio_output(GPIOC, 5, 0); set_gpio_output(GPIOC, 12, 0); - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - // C8: FAN PWM aka TIM3_CH3 set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -148,21 +107,6 @@ static void uno_init(void) { pwm_init(TIM4, 2); uno_set_ir_power(0U); - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - uno_enable_can_transceivers(true); - - // Disable LEDs - uno_set_led(LED_RED, false); - uno_set_led(LED_GREEN, false); - uno_set_led(LED_BLUE, false); - - // Set normal CAN mode - uno_set_can_mode(CAN_MODE_NORMAL); - // Switch to phone usb mode if harness connection is powered by less than 7V if(white_read_voltage_mV() < 7000U){ uno_set_usb_switch(true); @@ -197,7 +141,6 @@ static harness_configuration uno_harness_config = { board board_uno = { .harness_config = &uno_harness_config, - .has_obd = true, .has_spi = false, .has_canfd = false, .fan_max_rpm = 5100U, @@ -208,8 +151,8 @@ board board_uno = { .init = uno_init, .init_bootloader = uno_init_bootloader, .enable_can_transceiver = uno_enable_can_transceiver, - .enable_can_transceivers = uno_enable_can_transceivers, - .set_led = uno_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .set_can_mode = uno_set_can_mode, .check_ignition = uno_check_ignition, .read_voltage_mV = white_read_voltage_mV, diff --git a/panda/board/boards/white.h b/panda/board/boards/white.h index c4a25593..5f43dda6 100644 --- a/panda/board/boards/white.h +++ b/panda/board/boards/white.h @@ -17,30 +17,6 @@ static void white_enable_can_transceiver(uint8_t transceiver, bool enabled) { case 3U: set_gpio_output(GPIOA, 0, !enabled); break; - default: - print("Invalid CAN transceiver ("); puth(transceiver); print("): enabling failed\n"); - break; - } -} - -static void white_enable_can_transceivers(bool enabled) { - uint8_t t1 = enabled ? 1U : 2U; // leave transceiver 1 enabled to detect CAN ignition - for(uint8_t i=t1; i<=3U; i++) { - white_enable_can_transceiver(i, enabled); - } -} - -static void white_set_led(uint8_t color, bool enabled) { - switch (color){ - case LED_RED: - set_gpio_output(GPIOC, 9, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 6, !enabled); - break; default: break; } @@ -152,18 +128,6 @@ static void white_grey_init(void) { set_gpio_alternate(GPIOC, 11, GPIO_AF7_USART3); set_gpio_pullup(GPIOC, 11, PULL_UP); - - // Enable CAN transceivers - white_enable_can_transceivers(true); - - // Disable LEDs - white_set_led(LED_RED, false); - white_set_led(LED_GREEN, false); - white_set_led(LED_BLUE, false); - - // Set normal CAN mode - white_set_can_mode(CAN_MODE_NORMAL); - // Init usb power mode // Init in CDP mode only if panda is powered by 12V. // Otherwise a PC would not be able to flash a standalone panda @@ -191,7 +155,6 @@ static harness_configuration white_harness_config = { board board_white = { .set_bootkick = unused_set_bootkick, .harness_config = &white_harness_config, - .has_obd = false, .has_spi = false, .has_canfd = false, .fan_max_rpm = 0U, @@ -202,8 +165,8 @@ board board_white = { .init = white_grey_init, .init_bootloader = white_grey_init_bootloader, .enable_can_transceiver = white_enable_can_transceiver, - .enable_can_transceivers = white_enable_can_transceivers, - .set_led = white_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .set_can_mode = white_set_can_mode, .check_ignition = white_check_ignition, .read_voltage_mV = white_read_voltage_mV, diff --git a/panda/board/bootstub.c b/panda/board/bootstub.c index aee665e7..5e05fa66 100644 --- a/panda/board/bootstub.c +++ b/panda/board/bootstub.c @@ -6,6 +6,7 @@ // ********************* Includes ********************* #include "config.h" +#include "drivers/led.h" #include "drivers/pwm.h" #include "drivers/usb.h" diff --git a/panda/board/drivers/bxcan.h b/panda/board/drivers/bxcan.h index 721a7ee4..75bd05d5 100644 --- a/panda/board/drivers/bxcan.h +++ b/panda/board/drivers/bxcan.h @@ -178,7 +178,7 @@ void can_rx(uint8_t can_number) { safety_rx_invalid += safety_rx_hook(&to_push) ? 0U : 1U; ignition_can_hook(&to_push); - current_board->set_led(LED_BLUE, true); + led_set(LED_BLUE, true); rx_buffer_overflow += can_push(&can_rx_q, &to_push) ? 0U : 1U; // next diff --git a/panda/board/drivers/can_common.h b/panda/board/drivers/can_common.h index eeaf8738..da997d91 100644 --- a/panda/board/drivers/can_common.h +++ b/panda/board/drivers/can_common.h @@ -209,6 +209,12 @@ void ignition_can_hook(CANPacket_t *to_push) { ignition_can_cnt = 0U; } + // Volkswagen MEB exception + if ((addr == 0x3C0) && (len == 4)) { + ignition_can = GET_BIT(to_push, 17U); + ignition_can_cnt = 0U; + } + } } diff --git a/panda/board/drivers/fdcan.h b/panda/board/drivers/fdcan.h index 2f032d03..f3f8d997 100644 --- a/panda/board/drivers/fdcan.h +++ b/panda/board/drivers/fdcan.h @@ -223,7 +223,7 @@ void can_rx(uint8_t can_number) { safety_rx_invalid += safety_rx_hook(&to_push) ? 0U : 1U; ignition_can_hook(&to_push); - current_board->set_led(LED_BLUE, true); + led_set(LED_BLUE, true); rx_buffer_overflow += can_push(&can_rx_q, &to_push) ? 0U : 1U; // Enable CAN FD and BRS if CAN FD message was received diff --git a/panda/board/drivers/harness.h b/panda/board/drivers/harness.h index cfb6965c..efe319d0 100644 --- a/panda/board/drivers/harness.h +++ b/panda/board/drivers/harness.h @@ -94,6 +94,12 @@ void harness_tick(void) { } void harness_init(void) { + // init OBD_SBUx_RELAY + set_gpio_output_type(current_board->harness_config->GPIO_relay_SBU1, current_board->harness_config->pin_relay_SBU1, OUTPUT_TYPE_OPEN_DRAIN); + set_gpio_output_type(current_board->harness_config->GPIO_relay_SBU2, current_board->harness_config->pin_relay_SBU2, OUTPUT_TYPE_OPEN_DRAIN); + set_gpio_output(current_board->harness_config->GPIO_relay_SBU1, current_board->harness_config->pin_relay_SBU1, 1); + set_gpio_output(current_board->harness_config->GPIO_relay_SBU2, current_board->harness_config->pin_relay_SBU2, 1); + // try to detect orientation harness.status = harness_detect_orientation(); if (harness.status != HARNESS_STATUS_NC) { diff --git a/panda/board/early_init.h b/panda/board/early_init.h index 1837019d..e48a8063 100644 --- a/panda/board/early_init.h +++ b/panda/board/early_init.h @@ -56,10 +56,11 @@ void early_initialization(void) { detect_board_type(); if (enter_bootloader_mode == ENTER_BOOTLOADER_MAGIC) { + led_init(); #ifdef PANDA current_board->init_bootloader(); #endif - current_board->set_led(LED_GREEN, 1); + led_set(LED_GREEN, 1); jump_to_bootloader(); } } diff --git a/panda/board/fake_stm.h b/panda/board/fake_stm.h index b73a4e89..984fde14 100644 --- a/panda/board/fake_stm.h +++ b/panda/board/fake_stm.h @@ -31,3 +31,5 @@ uint32_t microsecond_timer_get(void); uint32_t microsecond_timer_get(void) { return MICROSECOND_TIMER->CNT; } + +typedef uint32_t GPIO_TypeDef; diff --git a/panda/board/flasher.h b/panda/board/flasher.h index 9a046a25..ba9d77a8 100644 --- a/panda/board/flasher.h +++ b/panda/board/flasher.h @@ -35,7 +35,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) { flash_unlock(); resp[1] = 0xff; } - current_board->set_led(LED_GREEN, 1); + led_set(LED_GREEN, 1); unlocked = true; prog_ptr = (uint32_t *)APP_START_ADDRESS; break; @@ -112,14 +112,14 @@ int comms_can_read(uint8_t *data, uint32_t max_len) { void refresh_can_tx_slots_available(void) {} void comms_endpoint2_write(const uint8_t *data, uint32_t len) { - current_board->set_led(LED_RED, 0); + led_set(LED_RED, 0); for (uint32_t i = 0; i < len/4; i++) { flash_write_word(prog_ptr, *(uint32_t*)(data+(i*4))); //*(uint64_t*)(&spi_tx_buf[0x30+(i*4)]) = *prog_ptr; prog_ptr++; } - current_board->set_led(LED_RED, 1); + led_set(LED_RED, 1); } @@ -132,6 +132,7 @@ void soft_flasher_start(void) { gpio_usart2_init(); gpio_usb_init(); + led_init(); // enable USB usb_init(); @@ -143,15 +144,15 @@ void soft_flasher_start(void) { } // green LED on for flashing - current_board->set_led(LED_GREEN, 1); + led_set(LED_GREEN, 1); enable_interrupts(); for (;;) { // blink the green LED fast - current_board->set_led(LED_GREEN, 0); + led_set(LED_GREEN, 0); delay(500000); - current_board->set_led(LED_GREEN, 1); + led_set(LED_GREEN, 1); delay(500000); } } diff --git a/panda/board/jungle/boards/board_declarations.h b/panda/board/jungle/boards/board_declarations.h index 26b093fa..e6c6601b 100644 --- a/panda/board/jungle/boards/board_declarations.h +++ b/panda/board/jungle/boards/board_declarations.h @@ -1,6 +1,5 @@ // ******************** Prototypes ******************** typedef void (*board_init)(void); -typedef void (*board_set_led)(uint8_t color, bool enabled); typedef void (*board_board_tick)(void); typedef bool (*board_get_button)(void); typedef void (*board_set_panda_power)(bool enabled); @@ -15,11 +14,12 @@ typedef float (*board_get_channel_power)(uint8_t channel); typedef uint16_t (*board_get_sbu_mV)(uint8_t channel, uint8_t sbu); struct board { + GPIO_TypeDef * const led_GPIO[3]; + const uint8_t led_pin[3]; const bool has_canfd; const bool has_sbu_sense; const uint16_t avdd_mV; board_init init; - board_set_led set_led; board_board_tick board_tick; board_get_button get_button; board_set_panda_power set_panda_power; @@ -42,11 +42,6 @@ struct board { #define HW_TYPE_V1 1U #define HW_TYPE_V2 2U -// LED colors -#define LED_RED 0U -#define LED_GREEN 1U -#define LED_BLUE 2U - // CAN modes #define CAN_MODE_NORMAL 0U #define CAN_MODE_OBD_CAN2 3U diff --git a/panda/board/jungle/boards/board_v1.h b/panda/board/jungle/boards/board_v1.h index 833f49f9..44e90b8f 100644 --- a/panda/board/jungle/boards/board_v1.h +++ b/panda/board/jungle/boards/board_v1.h @@ -2,22 +2,6 @@ // Jungle board v1 (STM32F4) // // ///////////////////////// // -void board_v1_set_led(uint8_t color, bool enabled) { - switch (color) { - case LED_RED: - set_gpio_output(GPIOC, 9, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOC, 7, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOC, 6, !enabled); - break; - default: - break; - } -} - void board_v1_enable_can_transceiver(uint8_t transceiver, bool enabled) { switch (transceiver) { case 1U: @@ -140,11 +124,6 @@ void board_v1_init(void) { board_v1_enable_can_transceiver(i, true); } - // Disable LEDs - board_v1_set_led(LED_RED, false); - board_v1_set_led(LED_GREEN, false); - board_v1_set_led(LED_BLUE, false); - // Set normal CAN mode board_v1_set_can_mode(CAN_MODE_NORMAL); @@ -162,7 +141,8 @@ board board_v1 = { .has_sbu_sense = false, .avdd_mV = 3300U, .init = &board_v1_init, - .set_led = &board_v1_set_led, + .led_GPIO = {GPIOC, GPIOC, GPIOC}, + .led_pin = {9, 7, 6}, .board_tick = &board_v1_tick, .get_button = &board_v1_get_button, .set_panda_power = &board_v1_set_panda_power, diff --git a/panda/board/jungle/boards/board_v2.h b/panda/board/jungle/boards/board_v2.h index bfd37969..ea0a099d 100644 --- a/panda/board/jungle/boards/board_v2.h +++ b/panda/board/jungle/boards/board_v2.h @@ -65,22 +65,6 @@ adc_channel_t sbu2_channels[] = { {.adc = ADC3, .channel = 11}, }; -void board_v2_set_led(uint8_t color, bool enabled) { - switch (color) { - case LED_RED: - set_gpio_output(GPIOE, 4, !enabled); - break; - case LED_GREEN: - set_gpio_output(GPIOE, 3, !enabled); - break; - case LED_BLUE: - set_gpio_output(GPIOE, 2, !enabled); - break; - default: - break; - } -} - void board_v2_set_harness_orientation(uint8_t orientation) { switch (orientation) { case HARNESS_ORIENTATION_NONE: @@ -252,11 +236,6 @@ uint16_t board_v2_get_sbu_mV(uint8_t channel, uint8_t sbu) { void board_v2_init(void) { common_init_gpio(); - // Disable LEDs - board_v2_set_led(LED_RED, false); - board_v2_set_led(LED_GREEN, false); - board_v2_set_led(LED_BLUE, false); - // Normal CAN mode board_v2_set_can_mode(CAN_MODE_NORMAL); @@ -312,7 +291,8 @@ board board_v2 = { .has_sbu_sense = true, .avdd_mV = 3300U, .init = &board_v2_init, - .set_led = &board_v2_set_led, + .led_GPIO = {GPIOE, GPIOE, GPIOE}, + .led_pin = {4, 3, 2}, .board_tick = &board_v2_tick, .get_button = &board_v2_get_button, .set_panda_power = &board_v2_set_panda_power, diff --git a/panda/board/jungle/main.c b/panda/board/jungle/main.c index 6b660f6c..1c73edd7 100644 --- a/panda/board/jungle/main.c +++ b/panda/board/jungle/main.c @@ -3,6 +3,7 @@ #include "safety.h" +#include "board/drivers/led.h" #include "board/drivers/pwm.h" #include "board/drivers/usb.h" @@ -76,7 +77,7 @@ void tick_handler(void) { check_registers(); // turn off the blue LED, turned on by CAN - current_board->set_led(LED_BLUE, false); + led_set(LED_BLUE, false); // Blink and OBD CAN #ifdef FINAL_PROVISIONING @@ -87,7 +88,7 @@ void tick_handler(void) { uptime_cnt += 1U; } - current_board->set_led(LED_GREEN, green_led_enabled); + led_set(LED_GREEN, green_led_enabled); // Check on button bool current_button_status = current_board->get_button(); @@ -146,8 +147,8 @@ int main(void) { peripherals_init(); detect_board_type(); // red+green leds enabled until succesful USB init, as a debug indicator - current_board->set_led(LED_RED, true); - current_board->set_led(LED_GREEN, true); + led_set(LED_RED, true); + led_set(LED_GREEN, true); // print hello print("\n\n\n************************ MAIN START ************************\n"); @@ -176,8 +177,8 @@ int main(void) { // enable USB (right before interrupts or enum can fail!) usb_init(); - current_board->set_led(LED_RED, false); - current_board->set_led(LED_GREEN, false); + led_set(LED_RED, false); + led_set(LED_GREEN, false); print("**** INTERRUPTS ON ****\n"); enable_interrupts(); @@ -223,16 +224,16 @@ int main(void) { // useful for debugging, fade breaks = panda is overloaded for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) { - current_board->set_led(LED_RED, true); + led_set(LED_RED, true); delay(fade >> 4); - current_board->set_led(LED_RED, false); + led_set(LED_RED, false); delay((MAX_LED_FADE - fade) >> 4); } for (uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= 1U) { - current_board->set_led(LED_RED, true); + led_set(LED_RED, true); delay(fade >> 4); - current_board->set_led(LED_RED, false); + led_set(LED_RED, false); delay((MAX_LED_FADE - fade) >> 4); } } diff --git a/panda/board/jungle/obj/bootstub.panda_jungle.bin b/panda/board/jungle/obj/bootstub.panda_jungle.bin index 38b86458..d2f4b10e 100755 Binary files a/panda/board/jungle/obj/bootstub.panda_jungle.bin and b/panda/board/jungle/obj/bootstub.panda_jungle.bin differ diff --git a/panda/board/jungle/obj/bootstub.panda_jungle.elf b/panda/board/jungle/obj/bootstub.panda_jungle.elf index 331ff449..5466ac70 100755 Binary files a/panda/board/jungle/obj/bootstub.panda_jungle.elf and b/panda/board/jungle/obj/bootstub.panda_jungle.elf differ diff --git a/panda/board/jungle/obj/bootstub.panda_jungle_h7.bin b/panda/board/jungle/obj/bootstub.panda_jungle_h7.bin index 487b1aa7..14f33af1 100755 Binary files a/panda/board/jungle/obj/bootstub.panda_jungle_h7.bin and b/panda/board/jungle/obj/bootstub.panda_jungle_h7.bin differ diff --git a/panda/board/jungle/obj/bootstub.panda_jungle_h7.elf b/panda/board/jungle/obj/bootstub.panda_jungle_h7.elf index 415ea41c..056b4a66 100755 Binary files a/panda/board/jungle/obj/bootstub.panda_jungle_h7.elf and b/panda/board/jungle/obj/bootstub.panda_jungle_h7.elf differ diff --git a/panda/board/jungle/obj/panda_jungle.bin b/panda/board/jungle/obj/panda_jungle.bin index 9a148bcc..77783724 100755 Binary files a/panda/board/jungle/obj/panda_jungle.bin and b/panda/board/jungle/obj/panda_jungle.bin differ diff --git a/panda/board/jungle/obj/panda_jungle.bin.signed b/panda/board/jungle/obj/panda_jungle.bin.signed index 42ce869d..b954ead9 100644 Binary files a/panda/board/jungle/obj/panda_jungle.bin.signed and b/panda/board/jungle/obj/panda_jungle.bin.signed differ diff --git a/panda/board/jungle/obj/panda_jungle.elf b/panda/board/jungle/obj/panda_jungle.elf index 33e14cc1..95b41944 100755 Binary files a/panda/board/jungle/obj/panda_jungle.elf and b/panda/board/jungle/obj/panda_jungle.elf differ diff --git a/panda/board/jungle/obj/panda_jungle_h7.bin b/panda/board/jungle/obj/panda_jungle_h7.bin index e1577458..35063e03 100755 Binary files a/panda/board/jungle/obj/panda_jungle_h7.bin and b/panda/board/jungle/obj/panda_jungle_h7.bin differ diff --git a/panda/board/jungle/obj/panda_jungle_h7.bin.signed b/panda/board/jungle/obj/panda_jungle_h7.bin.signed index 8b03fb6f..e59cbd84 100644 Binary files a/panda/board/jungle/obj/panda_jungle_h7.bin.signed and b/panda/board/jungle/obj/panda_jungle_h7.bin.signed differ diff --git a/panda/board/jungle/obj/panda_jungle_h7.elf b/panda/board/jungle/obj/panda_jungle_h7.elf index d947ed55..8d1cb240 100755 Binary files a/panda/board/jungle/obj/panda_jungle_h7.elf and b/panda/board/jungle/obj/panda_jungle_h7.elf differ diff --git a/panda/board/main.c b/panda/board/main.c index 7183b26f..9770f836 100644 --- a/panda/board/main.c +++ b/panda/board/main.c @@ -1,6 +1,7 @@ // ********************* Includes ********************* #include "config.h" +#include "drivers/led.h" #include "drivers/pwm.h" #include "drivers/usb.h" #include "drivers/simple_watchdog.h" @@ -76,14 +77,14 @@ void set_safety_mode(uint16_t mode, uint16_t param) { switch (mode_copy) { case SAFETY_SILENT: set_intercept_relay(false, false); - if (current_board->has_obd) { + if (current_board->harness_config->has_harness) { current_board->set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_SILENT; break; case SAFETY_NOOUTPUT: set_intercept_relay(false, false); - if (current_board->has_obd) { + if (current_board->harness_config->has_harness) { current_board->set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_LIVE; @@ -92,7 +93,7 @@ void set_safety_mode(uint16_t mode, uint16_t param) { set_intercept_relay(false, false); heartbeat_counter = 0U; heartbeat_lost = false; - if (current_board->has_obd) { + if (current_board->harness_config->has_harness) { // Clear any pending messages in the can core (i.e. sending while comma power is unplugged) // TODO: rewrite using hardware queues rather than fifo to cancel specific messages can_clear_send(CANIF_FROM_CAN_NUM(1), 1); @@ -108,7 +109,7 @@ void set_safety_mode(uint16_t mode, uint16_t param) { set_intercept_relay(true, false); heartbeat_counter = 0U; heartbeat_lost = false; - if (current_board->has_obd) { + if (current_board->harness_config->has_harness) { current_board->set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_LIVE; @@ -189,11 +190,11 @@ static void tick_handler(void) { #endif // set green LED to be controls allowed - current_board->set_led(LED_GREEN, controls_allowed | green_led_enabled); + led_set(LED_GREEN, controls_allowed | green_led_enabled); // turn off the blue LED, turned on by CAN // unless we are in power saving mode - current_board->set_led(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED)); + led_set(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED)); const bool recent_heartbeat = heartbeat_counter == 0U; @@ -303,9 +304,10 @@ int main(void) { clock_init(); peripherals_init(); detect_board_type(); + led_init(); // red+green leds enabled until succesful USB/SPI init, as a debug indicator - current_board->set_led(LED_RED, true); - current_board->set_led(LED_GREEN, true); + led_set(LED_RED, true); + led_set(LED_GREEN, true); adc_init(); // print hello @@ -319,6 +321,10 @@ int main(void) { // init board current_board->init(); + current_board->set_can_mode(CAN_MODE_NORMAL); + if (current_board->harness_config->has_harness) { + harness_init(); + } // panda has an FPU, let's use it! enable_fpu(); @@ -334,7 +340,7 @@ int main(void) { set_safety_mode(SAFETY_SILENT, 0U); // enable CAN TXs - current_board->enable_can_transceivers(true); + enable_can_transceivers(true); // init watchdog for heartbeat loop, fed at 8Hz simple_watchdog_init(FAULT_HEARTBEAT_LOOP_WATCHDOG, (3U * 1000000U / 8U)); @@ -351,12 +357,14 @@ int main(void) { #ifdef ENABLE_SPI if (current_board->has_spi) { + gpio_spi_init(); spi_init(); } #endif - current_board->set_led(LED_RED, false); - current_board->set_led(LED_GREEN, false); + led_set(LED_RED, false); + led_set(LED_GREEN, false); + led_set(LED_BLUE, false); print("**** INTERRUPTS ON ****\n"); enable_interrupts(); @@ -369,24 +377,24 @@ int main(void) { #endif // useful for debugging, fade breaks = panda is overloaded for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) { - current_board->set_led(LED_RED, true); + led_set(LED_RED, true); delay(fade >> 4); - current_board->set_led(LED_RED, false); + led_set(LED_RED, false); delay((MAX_LED_FADE - fade) >> 4); } for (uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= 1U) { - current_board->set_led(LED_RED, true); + led_set(LED_RED, true); delay(fade >> 4); - current_board->set_led(LED_RED, false); + led_set(LED_RED, false); delay((MAX_LED_FADE - fade) >> 4); } #ifdef DEBUG_FAULTS } else { - current_board->set_led(LED_RED, 1); + led_set(LED_RED, 1); delay(512000U); - current_board->set_led(LED_RED, 0); + led_set(LED_RED, 0); delay(512000U); } #endif diff --git a/panda/board/main_comms.h b/panda/board/main_comms.h index e366b19a..90056869 100644 --- a/panda/board/main_comms.h +++ b/panda/board/main_comms.h @@ -212,7 +212,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) { break; // **** 0xdb: set OBD CAN multiplexing mode case 0xdb: - if (current_board->has_obd) { + if (current_board->harness_config->has_harness) { if (req->param1 == 1U) { // Enable OBD CAN current_board->set_can_mode(CAN_MODE_OBD_CAN2); diff --git a/panda/board/power_saving.h b/panda/board/power_saving.h index 7fcc181e..dea46491 100644 --- a/panda/board/power_saving.h +++ b/panda/board/power_saving.h @@ -5,8 +5,15 @@ int power_save_status = POWER_SAVE_STATUS_DISABLED; -void set_power_save_state(int state) { +void enable_can_transceivers(bool enabled) { + // Leave main CAN always on for CAN-based ignition detection + uint8_t main_bus = (current_board->harness_config->has_harness && (harness.status == HARNESS_STATUS_FLIPPED)) ? 3U : 1U; + for(uint8_t i=1U; i<=4U; i++){ + current_board->enable_can_transceiver(i, (i == main_bus) || enabled); + } +} +void set_power_save_state(int state) { bool is_valid_state = (state == POWER_SAVE_STATUS_ENABLED) || (state == POWER_SAVE_STATUS_DISABLED); if (is_valid_state && (state != power_save_status)) { bool enable = false; @@ -33,7 +40,7 @@ void set_power_save_state(int state) { enable = true; } - current_board->enable_can_transceivers(enable); + enable_can_transceivers(enable); // Switch off IR when in power saving if(!enable){ diff --git a/panda/board/stm32f4/board.h b/panda/board/stm32f4/board.h index 0cbaae60..cd10c173 100644 --- a/panda/board/stm32f4/board.h +++ b/panda/board/stm32f4/board.h @@ -39,4 +39,7 @@ void detect_board_type(void) { hw_type = HW_TYPE_BLACK_PANDA; current_board = &board_black; } + + // Return A13 to the alt mode to fix SWD + set_gpio_alternate(GPIOA, 13, GPIO_AF0_SWJ); } diff --git a/panda/board/stm32f4/stm32f4_config.h b/panda/board/stm32f4/stm32f4_config.h index eae9222a..ee2759a7 100644 --- a/panda/board/stm32f4/stm32f4_config.h +++ b/panda/board/stm32f4/stm32f4_config.h @@ -73,7 +73,7 @@ void early_gpio_float(void) { RCC->AHB1ENR = RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN; - GPIOA->MODER = 0; GPIOB->MODER = 0; GPIOC->MODER = 0; + GPIOB->MODER = 0; GPIOC->MODER = 0; GPIOA->ODR = 0; GPIOB->ODR = 0; GPIOC->ODR = 0; GPIOA->PUPDR = 0; GPIOB->PUPDR = 0; GPIOC->PUPDR = 0; } diff --git a/panda/board/stm32h7/board.h b/panda/board/stm32h7/board.h index 6ab75f55..da47465d 100644 --- a/panda/board/stm32h7/board.h +++ b/panda/board/stm32h7/board.h @@ -14,7 +14,6 @@ #include "stm32h7/sound.h" #include "drivers/clock_source.h" #include "boards/red.h" -#include "boards/red_chiplet.h" #include "boards/tres.h" #include "boards/cuatro.h" diff --git a/panda/board/stm32h7/peripherals.h b/panda/board/stm32h7/peripherals.h index cae04652..a0b541cc 100644 --- a/panda/board/stm32h7/peripherals.h +++ b/panda/board/stm32h7/peripherals.h @@ -31,26 +31,6 @@ void gpio_uart7_init(void) { // Common GPIO initialization void common_init_gpio(void) { - /// E2,E3,E4: RGB LED - set_gpio_pullup(GPIOE, 2, PULL_NONE); - set_gpio_mode(GPIOE, 2, MODE_OUTPUT); - set_gpio_output_type(GPIOE, 2, OUTPUT_TYPE_OPEN_DRAIN); - - set_gpio_pullup(GPIOE, 3, PULL_NONE); - set_gpio_mode(GPIOE, 3, MODE_OUTPUT); - set_gpio_output_type(GPIOE, 3, OUTPUT_TYPE_OPEN_DRAIN); - - set_gpio_pullup(GPIOE, 4, PULL_NONE); - set_gpio_mode(GPIOE, 4, MODE_OUTPUT); - set_gpio_output_type(GPIOE, 4, OUTPUT_TYPE_OPEN_DRAIN); - - //C4,A1: OBD_SBU1, OBD_SBU2 - set_gpio_pullup(GPIOC, 4, PULL_NONE); - set_gpio_mode(GPIOC, 4, MODE_ANALOG); - - set_gpio_pullup(GPIOA, 1, PULL_NONE); - set_gpio_mode(GPIOA, 1, MODE_ANALOG); - //F11: VOLT_S set_gpio_pullup(GPIOF, 11, PULL_NONE); set_gpio_mode(GPIOF, 11, MODE_ANALOG); diff --git a/selfdrive/controls/controlsd.py b/selfdrive/controls/controlsd.py index f6fabfc4..0e934ce4 100755 --- a/selfdrive/controls/controlsd.py +++ b/selfdrive/controls/controlsd.py @@ -201,6 +201,7 @@ class Controls: # carControl cc_send = messaging.new_message('carControl') cc_send.valid = CS.canValid + CC.currentCurvature = cs.curvature cc_send.carControl = CC self.pm.send('carControl', cc_send) diff --git a/selfdrive/controls/lib/lateral_mpc_lib/c_generated_code/libacados_ocp_solver_lat.so b/selfdrive/controls/lib/lateral_mpc_lib/c_generated_code/libacados_ocp_solver_lat.so index e8f44138..c139bc2d 100755 Binary files a/selfdrive/controls/lib/lateral_mpc_lib/c_generated_code/libacados_ocp_solver_lat.so and b/selfdrive/controls/lib/lateral_mpc_lib/c_generated_code/libacados_ocp_solver_lat.so differ diff --git a/selfdrive/controls/lib/longitudinal_mpc_lib/c_generated_code/libacados_ocp_solver_long.so b/selfdrive/controls/lib/longitudinal_mpc_lib/c_generated_code/libacados_ocp_solver_long.so index da1c4a48..734e67e1 100755 Binary files a/selfdrive/controls/lib/longitudinal_mpc_lib/c_generated_code/libacados_ocp_solver_long.so and b/selfdrive/controls/lib/longitudinal_mpc_lib/c_generated_code/libacados_ocp_solver_long.so differ diff --git a/selfdrive/controls/radard.py b/selfdrive/controls/radard.py index 84420e35..98fce1cb 100755 --- a/selfdrive/controls/radard.py +++ b/selfdrive/controls/radard.py @@ -6,6 +6,7 @@ from typing import Any import capnp from cereal import messaging, log, car +from openpilot.common.filter_simple import FirstOrderFilter from openpilot.common.params import Params from openpilot.common.realtime import DT_MDL, Priority, config_realtime_process from openpilot.common.swaglog import cloudlog @@ -51,7 +52,7 @@ class Track: def __init__(self, identifier: int, v_lead: float, kalman_params: KalmanParams): self.identifier = identifier self.cnt = 0 - self.aLeadTau = _LEAD_ACCEL_TAU + self.aLeadTau = FirstOrderFilter(_LEAD_ACCEL_TAU, 0.45, DT_MDL) self.K_A = kalman_params.A self.K_C = kalman_params.C self.K_K = kalman_params.K @@ -74,17 +75,12 @@ class Track: # Learn if constant acceleration if abs(self.aLeadK) < 0.5: - self.aLeadTau = _LEAD_ACCEL_TAU + self.aLeadTau.x = _LEAD_ACCEL_TAU else: - self.aLeadTau *= 0.9 + self.aLeadTau.update(0.0) self.cnt += 1 - def reset_a_lead(self, aLeadK: float, aLeadTau: float): - self.kf = KF1D([[self.vLead], [aLeadK]], self.K_A, self.K_C, self.K_K) - self.aLeadK = aLeadK - self.aLeadTau = aLeadTau - def get_RadarState(self, model_prob: float = 0.0): return { "dRel": float(self.dRel), @@ -93,7 +89,7 @@ class Track: "vLead": float(self.vLead), "vLeadK": float(self.vLeadK), "aLeadK": float(self.aLeadK), - "aLeadTau": float(self.aLeadTau), + "aLeadTau": float(self.aLeadTau.x), "status": True, "fcw": self.is_potential_fcw(model_prob), "modelProb": model_prob, diff --git a/selfdrive/modeld/constants.py b/selfdrive/modeld/constants.py index 2bb7b810..5ca0a86b 100644 --- a/selfdrive/modeld/constants.py +++ b/selfdrive/modeld/constants.py @@ -14,8 +14,14 @@ class ModelConstants: # model inputs constants MODEL_FREQ = 20 + HISTORY_FREQ = 5 + HISTORY_LEN_SECONDS = 5 + TEMPORAL_SKIP = MODEL_FREQ // HISTORY_FREQ + FULL_HISTORY_BUFFER_LEN = MODEL_FREQ * HISTORY_LEN_SECONDS + INPUT_HISTORY_BUFFER_LEN = HISTORY_FREQ * HISTORY_LEN_SECONDS + FEATURE_LEN = 512 - FULL_HISTORY_BUFFER_LEN = 100 + DESIRE_LEN = 8 TRAFFIC_CONVENTION_LEN = 2 LAT_PLANNER_STATE_LEN = 4 diff --git a/selfdrive/modeld/modeld.py b/selfdrive/modeld/modeld.py index ee825d15..1e557942 100755 --- a/selfdrive/modeld/modeld.py +++ b/selfdrive/modeld/modeld.py @@ -56,16 +56,24 @@ class ModelState: prev_desire: np.ndarray # for tracking the rising edge of the pulse def __init__(self, context: CLContext): - self.frames = {'input_imgs': DrivingModelFrame(context), 'big_input_imgs': DrivingModelFrame(context)} + self.frames = { + 'input_imgs': DrivingModelFrame(context, ModelConstants.TEMPORAL_SKIP), + 'big_input_imgs': DrivingModelFrame(context, ModelConstants.TEMPORAL_SKIP) + } self.prev_desire = np.zeros(ModelConstants.DESIRE_LEN, dtype=np.float32) + self.full_features_buffer = np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.FEATURE_LEN), dtype=np.float32) + self.full_desire = np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.DESIRE_LEN), dtype=np.float32) + self.full_prev_desired_curv = np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.PREV_DESIRED_CURV_LEN), dtype=np.float32) + self.temporal_idxs = slice(-1-(ModelConstants.TEMPORAL_SKIP*(ModelConstants.INPUT_HISTORY_BUFFER_LEN-1)), None, ModelConstants.TEMPORAL_SKIP) + # policy inputs self.numpy_inputs = { - 'desire': np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.DESIRE_LEN), dtype=np.float32), + 'desire': np.zeros((1, ModelConstants.INPUT_HISTORY_BUFFER_LEN, ModelConstants.DESIRE_LEN), dtype=np.float32), 'traffic_convention': np.zeros((1, ModelConstants.TRAFFIC_CONVENTION_LEN), dtype=np.float32), 'lateral_control_params': np.zeros((1, ModelConstants.LATERAL_CONTROL_PARAMS_LEN), dtype=np.float32), - 'prev_desired_curv': np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.PREV_DESIRED_CURV_LEN), dtype=np.float32), - 'features_buffer': np.zeros((1, ModelConstants.FULL_HISTORY_BUFFER_LEN, ModelConstants.FEATURE_LEN), dtype=np.float32), + 'prev_desired_curv': np.zeros((1, ModelConstants.INPUT_HISTORY_BUFFER_LEN, ModelConstants.PREV_DESIRED_CURV_LEN), dtype=np.float32), + 'features_buffer': np.zeros((1, ModelConstants.INPUT_HISTORY_BUFFER_LEN, ModelConstants.FEATURE_LEN), dtype=np.float32), } with open(VISION_METADATA_PATH, 'rb') as f: @@ -104,8 +112,9 @@ class ModelState: new_desire = np.where(inputs['desire'] - self.prev_desire > .99, inputs['desire'], 0) self.prev_desire[:] = inputs['desire'] - self.numpy_inputs['desire'][0,:-1] = self.numpy_inputs['desire'][0,1:] - self.numpy_inputs['desire'][0,-1] = new_desire + self.full_desire[0,:-1] = self.full_desire[0,1:] + self.full_desire[0,-1] = new_desire + self.numpy_inputs['desire'][:] = self.full_desire.reshape((1,ModelConstants.INPUT_HISTORY_BUFFER_LEN,ModelConstants.TEMPORAL_SKIP,-1)).max(axis=2) self.numpy_inputs['traffic_convention'][:] = inputs['traffic_convention'] self.numpy_inputs['lateral_control_params'][:] = inputs['lateral_control_params'] @@ -128,15 +137,17 @@ class ModelState: self.vision_output = self.vision_run(**self.vision_inputs).numpy().flatten() vision_outputs_dict = self.parser.parse_vision_outputs(self.slice_outputs(self.vision_output, self.vision_output_slices)) - self.numpy_inputs['features_buffer'][0,:-1] = self.numpy_inputs['features_buffer'][0,1:] - self.numpy_inputs['features_buffer'][0,-1] = vision_outputs_dict['hidden_state'][0, :] + self.full_features_buffer[0,:-1] = self.full_features_buffer[0,1:] + self.full_features_buffer[0,-1] = vision_outputs_dict['hidden_state'][0, :] + self.numpy_inputs['features_buffer'][:] = self.full_features_buffer[0, self.temporal_idxs] self.policy_output = self.policy_run(**self.policy_inputs).numpy().flatten() policy_outputs_dict = self.parser.parse_policy_outputs(self.slice_outputs(self.policy_output, self.policy_output_slices)) # TODO model only uses last value now - self.numpy_inputs['prev_desired_curv'][0,:-1] = self.numpy_inputs['prev_desired_curv'][0,1:] - self.numpy_inputs['prev_desired_curv'][0,-1,:] = policy_outputs_dict['desired_curvature'][0, :] + self.full_prev_desired_curv[0,:-1] = self.full_prev_desired_curv[0,1:] + self.full_prev_desired_curv[0,-1,:] = policy_outputs_dict['desired_curvature'][0, :] + self.numpy_inputs['prev_desired_curv'][:] = self.full_prev_desired_curv[0, self.temporal_idxs] combined_outputs_dict = {**vision_outputs_dict, **policy_outputs_dict} if SEND_RAW_PRED: diff --git a/selfdrive/modeld/models/commonmodel.cc b/selfdrive/modeld/models/commonmodel.cc index 9973d185..d3341e76 100644 --- a/selfdrive/modeld/models/commonmodel.cc +++ b/selfdrive/modeld/models/commonmodel.cc @@ -5,11 +5,12 @@ #include "common/clutil.h" -DrivingModelFrame::DrivingModelFrame(cl_device_id device_id, cl_context context) : ModelFrame(device_id, context) { +DrivingModelFrame::DrivingModelFrame(cl_device_id device_id, cl_context context, int _temporal_skip) : ModelFrame(device_id, context) { input_frames = std::make_unique(buf_size); + temporal_skip = _temporal_skip; input_frames_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE, buf_size, NULL, &err)); - img_buffer_20hz_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE, 2*frame_size_bytes, NULL, &err)); - region.origin = 1 * frame_size_bytes; + img_buffer_20hz_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE, (temporal_skip+1)*frame_size_bytes, NULL, &err)); + region.origin = temporal_skip * frame_size_bytes; region.size = frame_size_bytes; last_img_cl = CL_CHECK_ERR(clCreateSubBuffer(img_buffer_20hz_cl, CL_MEM_READ_WRITE, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err)); @@ -20,7 +21,7 @@ DrivingModelFrame::DrivingModelFrame(cl_device_id device_id, cl_context context) cl_mem* DrivingModelFrame::prepare(cl_mem yuv_cl, int frame_width, int frame_height, int frame_stride, int frame_uv_offset, const mat3& projection) { run_transform(yuv_cl, MODEL_WIDTH, MODEL_HEIGHT, frame_width, frame_height, frame_stride, frame_uv_offset, projection); - for (int i = 0; i < 1; i++) { + for (int i = 0; i < temporal_skip; i++) { CL_CHECK(clEnqueueCopyBuffer(q, img_buffer_20hz_cl, img_buffer_20hz_cl, (i+1)*frame_size_bytes, i*frame_size_bytes, frame_size_bytes, 0, nullptr, nullptr)); } loadyuv_queue(&loadyuv, q, y_cl, u_cl, v_cl, last_img_cl); @@ -36,6 +37,7 @@ cl_mem* DrivingModelFrame::prepare(cl_mem yuv_cl, int frame_width, int frame_hei DrivingModelFrame::~DrivingModelFrame() { deinit_transform(); loadyuv_destroy(&loadyuv); + CL_CHECK(clReleaseMemObject(input_frames_cl)); CL_CHECK(clReleaseMemObject(img_buffer_20hz_cl)); CL_CHECK(clReleaseMemObject(last_img_cl)); CL_CHECK(clReleaseCommandQueue(q)); @@ -57,5 +59,6 @@ cl_mem* MonitoringModelFrame::prepare(cl_mem yuv_cl, int frame_width, int frame_ MonitoringModelFrame::~MonitoringModelFrame() { deinit_transform(); + CL_CHECK(clReleaseMemObject(input_frame_cl)); CL_CHECK(clReleaseCommandQueue(q)); } diff --git a/selfdrive/modeld/models/commonmodel.h b/selfdrive/modeld/models/commonmodel.h index 14409943..176d7eb6 100644 --- a/selfdrive/modeld/models/commonmodel.h +++ b/selfdrive/modeld/models/commonmodel.h @@ -64,20 +64,21 @@ protected: class DrivingModelFrame : public ModelFrame { public: - DrivingModelFrame(cl_device_id device_id, cl_context context); + DrivingModelFrame(cl_device_id device_id, cl_context context, int _temporal_skip); ~DrivingModelFrame(); cl_mem* prepare(cl_mem yuv_cl, int frame_width, int frame_height, int frame_stride, int frame_uv_offset, const mat3& projection); const int MODEL_WIDTH = 512; const int MODEL_HEIGHT = 256; const int MODEL_FRAME_SIZE = MODEL_WIDTH * MODEL_HEIGHT * 3 / 2; - const int buf_size = MODEL_FRAME_SIZE * 2; + const int buf_size = MODEL_FRAME_SIZE * 2; // 2 frames are temporal_skip frames apart const size_t frame_size_bytes = MODEL_FRAME_SIZE * sizeof(uint8_t); private: LoadYUVState loadyuv; cl_mem img_buffer_20hz_cl, last_img_cl, input_frames_cl; cl_buffer_region region; + int temporal_skip; }; class MonitoringModelFrame : public ModelFrame { diff --git a/selfdrive/modeld/models/commonmodel.pxd b/selfdrive/modeld/models/commonmodel.pxd index b4f08b12..4ac64d91 100644 --- a/selfdrive/modeld/models/commonmodel.pxd +++ b/selfdrive/modeld/models/commonmodel.pxd @@ -20,7 +20,7 @@ cdef extern from "selfdrive/modeld/models/commonmodel.h": cppclass DrivingModelFrame: int buf_size - DrivingModelFrame(cl_device_id, cl_context) + DrivingModelFrame(cl_device_id, cl_context, int) cppclass MonitoringModelFrame: int buf_size diff --git a/selfdrive/modeld/models/commonmodel_pyx.cpp b/selfdrive/modeld/models/commonmodel_pyx.cpp index cd0b6ba7..6dc1b1a0 100644 --- a/selfdrive/modeld/models/commonmodel_pyx.cpp +++ b/selfdrive/modeld/models/commonmodel_pyx.cpp @@ -3548,6 +3548,7 @@ static const char __pyx_k_stringsource[] = ""; static const char __pyx_k_version_info[] = "version_info"; static const char __pyx_k_class_getitem[] = "__class_getitem__"; static const char __pyx_k_reduce_cython[] = "__reduce_cython__"; +static const char __pyx_k_temporal_skip[] = "temporal_skip"; static const char __pyx_k_AssertionError[] = "AssertionError"; static const char __pyx_k_buffer_from_cl[] = "buffer_from_cl"; static const char __pyx_k_View_MemoryView[] = "View.MemoryView"; @@ -3664,7 +3665,7 @@ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFr static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFrame_4buffer_from_cl(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_ModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLMem *__pyx_v_in_frames); /* proto */ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFrame_6__reduce_cython__(CYTHON_UNUSED struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_ModelFrame *__pyx_v_self); /* proto */ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFrame_8__setstate_cython__(CYTHON_UNUSED struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_ModelFrame *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v___pyx_state); /* proto */ -static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context); /* proto */ +static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context, int __pyx_v_temporal_skip); /* proto */ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame_2__reduce_cython__(CYTHON_UNUSED struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self); /* proto */ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame_4__setstate_cython__(CYTHON_UNUSED struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v___pyx_state); /* proto */ static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_20MonitoringModelFrame___cinit__(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_MonitoringModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context); /* proto */ @@ -3939,6 +3940,7 @@ typedef struct { PyObject *__pyx_kp_s_stringsource; PyObject *__pyx_n_s_struct; PyObject *__pyx_n_s_sys; + PyObject *__pyx_n_s_temporal_skip; PyObject *__pyx_n_s_test; PyObject *__pyx_kp_s_unable_to_allocate_array_data; PyObject *__pyx_kp_s_unable_to_allocate_shape_and_str; @@ -4225,6 +4227,7 @@ static int __pyx_m_clear(PyObject *m) { Py_CLEAR(clear_module_state->__pyx_kp_s_stringsource); Py_CLEAR(clear_module_state->__pyx_n_s_struct); Py_CLEAR(clear_module_state->__pyx_n_s_sys); + Py_CLEAR(clear_module_state->__pyx_n_s_temporal_skip); Py_CLEAR(clear_module_state->__pyx_n_s_test); Py_CLEAR(clear_module_state->__pyx_kp_s_unable_to_allocate_array_data); Py_CLEAR(clear_module_state->__pyx_kp_s_unable_to_allocate_shape_and_str); @@ -4489,6 +4492,7 @@ static int __pyx_m_traverse(PyObject *m, visitproc visit, void *arg) { Py_VISIT(traverse_module_state->__pyx_kp_s_stringsource); Py_VISIT(traverse_module_state->__pyx_n_s_struct); Py_VISIT(traverse_module_state->__pyx_n_s_sys); + Py_VISIT(traverse_module_state->__pyx_n_s_temporal_skip); Py_VISIT(traverse_module_state->__pyx_n_s_test); Py_VISIT(traverse_module_state->__pyx_kp_s_unable_to_allocate_array_data); Py_VISIT(traverse_module_state->__pyx_kp_s_unable_to_allocate_shape_and_str); @@ -4799,6 +4803,7 @@ static int __pyx_m_traverse(PyObject *m, visitproc visit, void *arg) { #define __pyx_kp_s_stringsource __pyx_mstate_global->__pyx_kp_s_stringsource #define __pyx_n_s_struct __pyx_mstate_global->__pyx_n_s_struct #define __pyx_n_s_sys __pyx_mstate_global->__pyx_n_s_sys +#define __pyx_n_s_temporal_skip __pyx_mstate_global->__pyx_n_s_temporal_skip #define __pyx_n_s_test __pyx_mstate_global->__pyx_n_s_test #define __pyx_kp_s_unable_to_allocate_array_data __pyx_mstate_global->__pyx_kp_s_unable_to_allocate_array_data #define __pyx_kp_s_unable_to_allocate_shape_and_str __pyx_mstate_global->__pyx_kp_s_unable_to_allocate_shape_and_str @@ -21642,8 +21647,8 @@ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFr /* "selfdrive/modeld/models/commonmodel_pyx.pyx":62 * cdef cppDrivingModelFrame * _frame * - * def __cinit__(self, CLContext context): # <<<<<<<<<<<<<< - * self._frame = new cppDrivingModelFrame(context.device_id, context.context) + * def __cinit__(self, CLContext context, int temporal_skip): # <<<<<<<<<<<<<< + * self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) * self.frame = (self._frame) */ @@ -21651,9 +21656,10 @@ static PyObject *__pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_10ModelFr static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context = 0; + int __pyx_v_temporal_skip; CYTHON_UNUSED Py_ssize_t __pyx_nargs; CYTHON_UNUSED PyObject *const *__pyx_kwvalues; - PyObject* values[1] = {0}; + PyObject* values[2] = {0,0}; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; @@ -21667,10 +21673,12 @@ static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF #endif __pyx_kwvalues = __Pyx_KwValues_VARARGS(__pyx_args, __pyx_nargs); { - PyObject **__pyx_pyargnames[] = {&__pyx_n_s_context,0}; + PyObject **__pyx_pyargnames[] = {&__pyx_n_s_context,&__pyx_n_s_temporal_skip,0}; if (__pyx_kwds) { Py_ssize_t kw_args; switch (__pyx_nargs) { + case 2: values[1] = __Pyx_Arg_VARARGS(__pyx_args, 1); + CYTHON_FALLTHROUGH; case 1: values[0] = __Pyx_Arg_VARARGS(__pyx_args, 0); CYTHON_FALLTHROUGH; case 0: break; @@ -21685,21 +21693,33 @@ static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF } else if (unlikely(PyErr_Occurred())) __PYX_ERR(1, 62, __pyx_L3_error) else goto __pyx_L5_argtuple_error; + CYTHON_FALLTHROUGH; + case 1: + if (likely((values[1] = __Pyx_GetKwValue_VARARGS(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_temporal_skip)) != 0)) { + (void)__Pyx_Arg_NewRef_VARARGS(values[1]); + kw_args--; + } + else if (unlikely(PyErr_Occurred())) __PYX_ERR(1, 62, __pyx_L3_error) + else { + __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 2, 2, 1); __PYX_ERR(1, 62, __pyx_L3_error) + } } if (unlikely(kw_args > 0)) { const Py_ssize_t kwd_pos_args = __pyx_nargs; if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_kwvalues, __pyx_pyargnames, 0, values + 0, kwd_pos_args, "__cinit__") < 0)) __PYX_ERR(1, 62, __pyx_L3_error) } - } else if (unlikely(__pyx_nargs != 1)) { + } else if (unlikely(__pyx_nargs != 2)) { goto __pyx_L5_argtuple_error; } else { values[0] = __Pyx_Arg_VARARGS(__pyx_args, 0); + values[1] = __Pyx_Arg_VARARGS(__pyx_args, 1); } __pyx_v_context = ((struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *)values[0]); + __pyx_v_temporal_skip = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_temporal_skip == (int)-1) && PyErr_Occurred())) __PYX_ERR(1, 62, __pyx_L3_error) } goto __pyx_L6_skip; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, __pyx_nargs); __PYX_ERR(1, 62, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 2, 2, __pyx_nargs); __PYX_ERR(1, 62, __pyx_L3_error) __pyx_L6_skip:; goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; @@ -21714,7 +21734,7 @@ static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF return -1; __pyx_L4_argument_unpacking_done:; if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_context), __pyx_ptype_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext, 1, "context", 0))) __PYX_ERR(1, 62, __pyx_L1_error) - __pyx_r = __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(((struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *)__pyx_v_self), __pyx_v_context); + __pyx_r = __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(((struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *)__pyx_v_self), __pyx_v_context, __pyx_v_temporal_skip); /* function exit code */ goto __pyx_L0; @@ -21731,22 +21751,22 @@ static int __pyx_pw_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF return __pyx_r; } -static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context) { +static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelFrame___cinit__(struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_DrivingModelFrame *__pyx_v_self, struct __pyx_obj_9selfdrive_6modeld_6models_15commonmodel_pyx_CLContext *__pyx_v_context, int __pyx_v_temporal_skip) { int __pyx_r; int __pyx_t_1; /* "selfdrive/modeld/models/commonmodel_pyx.pyx":63 * - * def __cinit__(self, CLContext context): - * self._frame = new cppDrivingModelFrame(context.device_id, context.context) # <<<<<<<<<<<<<< + * def __cinit__(self, CLContext context, int temporal_skip): + * self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) # <<<<<<<<<<<<<< * self.frame = (self._frame) * self.buf_size = self._frame.buf_size */ - __pyx_v_self->_frame = new DrivingModelFrame(__pyx_v_context->__pyx_base.device_id, __pyx_v_context->__pyx_base.context); + __pyx_v_self->_frame = new DrivingModelFrame(__pyx_v_context->__pyx_base.device_id, __pyx_v_context->__pyx_base.context, __pyx_v_temporal_skip); /* "selfdrive/modeld/models/commonmodel_pyx.pyx":64 - * def __cinit__(self, CLContext context): - * self._frame = new cppDrivingModelFrame(context.device_id, context.context) + * def __cinit__(self, CLContext context, int temporal_skip): + * self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) * self.frame = (self._frame) # <<<<<<<<<<<<<< * self.buf_size = self._frame.buf_size * @@ -21754,7 +21774,7 @@ static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF __pyx_v_self->__pyx_base.frame = ((ModelFrame *)__pyx_v_self->_frame); /* "selfdrive/modeld/models/commonmodel_pyx.pyx":65 - * self._frame = new cppDrivingModelFrame(context.device_id, context.context) + * self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) * self.frame = (self._frame) * self.buf_size = self._frame.buf_size # <<<<<<<<<<<<<< * @@ -21766,8 +21786,8 @@ static int __pyx_pf_9selfdrive_6modeld_6models_15commonmodel_pyx_17DrivingModelF /* "selfdrive/modeld/models/commonmodel_pyx.pyx":62 * cdef cppDrivingModelFrame * _frame * - * def __cinit__(self, CLContext context): # <<<<<<<<<<<<<< - * self._frame = new cppDrivingModelFrame(context.device_id, context.context) + * def __cinit__(self, CLContext context, int temporal_skip): # <<<<<<<<<<<<<< + * self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) * self.frame = (self._frame) */ @@ -24144,6 +24164,7 @@ static int __Pyx_CreateStringTabAndInitStrings(void) { {&__pyx_kp_s_stringsource, __pyx_k_stringsource, sizeof(__pyx_k_stringsource), 0, 0, 1, 0}, {&__pyx_n_s_struct, __pyx_k_struct, sizeof(__pyx_k_struct), 0, 0, 1, 1}, {&__pyx_n_s_sys, __pyx_k_sys, sizeof(__pyx_k_sys), 0, 0, 1, 1}, + {&__pyx_n_s_temporal_skip, __pyx_k_temporal_skip, sizeof(__pyx_k_temporal_skip), 0, 0, 1, 1}, {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1}, {&__pyx_kp_s_unable_to_allocate_array_data, __pyx_k_unable_to_allocate_array_data, sizeof(__pyx_k_unable_to_allocate_array_data), 0, 0, 1, 0}, {&__pyx_kp_s_unable_to_allocate_shape_and_str, __pyx_k_unable_to_allocate_shape_and_str, sizeof(__pyx_k_unable_to_allocate_shape_and_str), 0, 0, 1, 0}, diff --git a/selfdrive/modeld/models/commonmodel_pyx.pyx b/selfdrive/modeld/models/commonmodel_pyx.pyx index 7b3a5bb3..5b7d11bc 100644 --- a/selfdrive/modeld/models/commonmodel_pyx.pyx +++ b/selfdrive/modeld/models/commonmodel_pyx.pyx @@ -59,8 +59,8 @@ cdef class ModelFrame: cdef class DrivingModelFrame(ModelFrame): cdef cppDrivingModelFrame * _frame - def __cinit__(self, CLContext context): - self._frame = new cppDrivingModelFrame(context.device_id, context.context) + def __cinit__(self, CLContext context, int temporal_skip): + self._frame = new cppDrivingModelFrame(context.device_id, context.context, temporal_skip) self.frame = (self._frame) self.buf_size = self._frame.buf_size diff --git a/selfdrive/modeld/models/commonmodel_pyx.so b/selfdrive/modeld/models/commonmodel_pyx.so index 54b5d654..d52356b7 100755 Binary files a/selfdrive/modeld/models/commonmodel_pyx.so and b/selfdrive/modeld/models/commonmodel_pyx.so differ diff --git a/selfdrive/modeld/models/dmonitoring_model_tinygrad.pkl b/selfdrive/modeld/models/dmonitoring_model_tinygrad.pkl index 78b24e90..fd8bc927 100644 Binary files a/selfdrive/modeld/models/dmonitoring_model_tinygrad.pkl and b/selfdrive/modeld/models/dmonitoring_model_tinygrad.pkl differ diff --git a/selfdrive/modeld/models/driving_policy_metadata.pkl b/selfdrive/modeld/models/driving_policy_metadata.pkl index 6cc4bab0..e5ceaf0b 100644 Binary files a/selfdrive/modeld/models/driving_policy_metadata.pkl and b/selfdrive/modeld/models/driving_policy_metadata.pkl differ diff --git a/selfdrive/modeld/models/driving_policy_tinygrad.pkl b/selfdrive/modeld/models/driving_policy_tinygrad.pkl index 96017790..4e2d3836 100644 Binary files a/selfdrive/modeld/models/driving_policy_tinygrad.pkl and b/selfdrive/modeld/models/driving_policy_tinygrad.pkl differ diff --git a/selfdrive/modeld/models/driving_vision_metadata.pkl b/selfdrive/modeld/models/driving_vision_metadata.pkl index bf4bc470..89c7477e 100644 Binary files a/selfdrive/modeld/models/driving_vision_metadata.pkl and b/selfdrive/modeld/models/driving_vision_metadata.pkl differ diff --git a/selfdrive/modeld/models/driving_vision_tinygrad.pkl b/selfdrive/modeld/models/driving_vision_tinygrad.pkl index 31ad1940..6b2930e4 100644 Binary files a/selfdrive/modeld/models/driving_vision_tinygrad.pkl and b/selfdrive/modeld/models/driving_vision_tinygrad.pkl differ diff --git a/selfdrive/pandad/panda.cc b/selfdrive/pandad/panda.cc index 7b5fc9a9..93e139f0 100644 --- a/selfdrive/pandad/panda.cc +++ b/selfdrive/pandad/panda.cc @@ -66,6 +66,25 @@ void Panda::set_alternative_experience(uint16_t alternative_experience) { handle->control_write(0xdf, alternative_experience, 0); } +std::string Panda::serial_read(int port_number) { + std::string ret; + char buffer[USBPACKET_MAX_SIZE] = {}; + + while (true) { + int bytes_read = handle->control_read(0xe0, port_number, 0, (unsigned char *)buffer, USBPACKET_MAX_SIZE); + if (bytes_read <= 0) { + break; + } + ret.append(buffer, bytes_read); + } + + return ret; +} + +void Panda::set_uart_baud(int uart, int rate) { + handle->control_write(0xe4, uart, int(rate / 300)); +} + cereal::PandaState::PandaType Panda::get_hw_type() { unsigned char hw_query[1] = {0}; diff --git a/selfdrive/pandad/panda.h b/selfdrive/pandad/panda.h index 6ae2c777..5cbce44f 100644 --- a/selfdrive/pandad/panda.h +++ b/selfdrive/pandad/panda.h @@ -64,6 +64,8 @@ public: cereal::PandaState::PandaType get_hw_type(); void set_safety_model(cereal::CarParams::SafetyModel safety_model, uint16_t safety_param=0U); void set_alternative_experience(uint16_t alternative_experience); + std::string serial_read(int port_number = 0); + void set_uart_baud(int uart, int rate); void set_fan_speed(uint16_t fan_speed); uint16_t get_fan_speed(); void set_ir_pwr(uint16_t ir_pwr); diff --git a/selfdrive/pandad/pandad b/selfdrive/pandad/pandad index 4cbbcd5e..9663b36b 100755 Binary files a/selfdrive/pandad/pandad and b/selfdrive/pandad/pandad differ diff --git a/selfdrive/pandad/pandad.cc b/selfdrive/pandad/pandad.cc index e6ef4a40..db7dd387 100644 --- a/selfdrive/pandad/pandad.cc +++ b/selfdrive/pandad/pandad.cc @@ -452,6 +452,14 @@ void pandad_run(std::vector &pandas) { send_peripheral_state(peripheral_panda, &pm); } + // Forward logs from pandas to cloudlog if available + for (auto *panda : pandas) { + std::string log = panda->serial_read(); + if (!log.empty()) { + LOGD("%s", log.c_str()); + } + } + rk.keepTime(); } diff --git a/tinygrad_repo/extra/accel/ane/2_compile/ane.py b/tinygrad_repo/extra/accel/ane/2_compile/ane.py index f35d8507..2e430c0f 120000 --- a/tinygrad_repo/extra/accel/ane/2_compile/ane.py +++ b/tinygrad_repo/extra/accel/ane/2_compile/ane.py @@ -1 +1,222 @@ -../lib/ane.py \ No newline at end of file +#!/usr/bin/env python3 +from pathlib import Path +from ctypes import * +import json +import collections +import numpy as np +import faulthandler +import struct +faulthandler.enable() + +basedir = Path(__file__).resolve().parent + +libane = None +aneregs = None +def init_libane(): + global libane, aneregs + libane = cdll.LoadLibrary((basedir / "libane.dylib").as_posix()) + + libane.ANE_Compile.argtypes = [c_char_p, c_int] + libane.ANE_Compile.restype = c_void_p + + libane.ANE_TensorCreate.restype = c_void_p + + libane.ANE_TensorData.argtypes = [c_void_p] + libane.ANE_TensorData.restype = POINTER(c_uint16) + + libane.ANE_Run.argtypes = [c_void_p]*4 + libane.ANE_Run.restype = c_int + + #libane.ANE_RegDebug.restype = c_char_p + + with open(basedir / "aneregs.json") as f: + aneregs = json.load(f) + +ANE_Struct = [ +# aneTD.Header + ("u32", 0x1C, "NextCommandOffset"), + +# KernelDMASrc @ section @ 0x2C len 0xF4 + # reloc 0x2c-0x34?? = weights + # u32[16] 0x34-0x74 = 0x80 | 1 if used + # u32[16] 0x74-0xB4 = + # u32[16] 0xB4-0xF4 = + +# Common @ section @ 0x128 len 0x3C (conv) + ("u16", 0x128, "InputWidth"), + ("u16", 0x12A, "InputHeight"), + ("u16", 0x12C, "InputDepth"), + + ("u32", 0x130, "InputOutputType"), # (OutputType * 0x10) | InputType + # UInt8 = 0, Int8 = 1, Float16 = 2 + + ("u32", 0x134, "InputChannels"), + ("u32", 0x138, "OutputChannels"), + + ("u16", 0x13C, "OutputWidth"), + ("u16", 0x13E, "OutputHeight"), + ("u16", 0x140, "OutputDepth"), + + ("u16", 0x144, "KernelSize"), # 0xa000 | (KernelHeight * 0x20) | KernelWidth + ("u16", 0x146, "Padding"), # 0x5000 | (PadTop * 0x40) | (PadLeft * 2) + + ("u16", 0x14C, "BatchSize"), + +# TileDMASrc @ section @ 0x16C len 0x6C (input) + # reloc 0x16c-0x174 = image + ("u32", 0x178, "InputRowStride"), + ("u32", 0x17C, "InputPlaneStride"), + ("u32", 0x180, "InputDepthStride"), + ("u32", 0x184, "InputBatchStride"), + + ("u8", 0x1A7, "InputInterleave"), + +# L2 @ section @ 0x1E0 len 0x44 + # [0x1ec, 0x1f0, 0x1f4, 0x1f8, 0x214] = number of engines + # [0x1f0, 0x1f4, 0x1f8, 0x214] = engines for inconv? + # [0x21c, 0x220, 0x224] = engines for outconv? + +# NE @ section @ 0x22c len 0xC (scaling) + ("u16", 0x230, "BiasScalar"), + ("u16", 0x232, "ScaleScalar"), + +# section @ 0x240 len 0x10 + ("u16", 0x246, "NeuronType"), # 0x10 = copy, 0x11 = ReLU, 0x12 = custom + ("u32", 0x250, "PostScale"), + +# TileDMADst @ section @ 0x258 len 0x18 + +# HandleTileDmaDstConfig + # 0x258 -- *(uint *)(this + 0x334) = *(uint *)(this + 0x334) & 0xfffffc3f | 0xc0; + # (GetCacheHintRegisterValue & 0xf) << 6; + ("u32", 0x25C, "OutputOffset"), # offset into output buffer to write at? + + # 0x260 -- *(uint *)(this + 0x33c) = *(uint *)(this + 0x33c) & 0x3f | (int)uVar10 << 6; + ("u32", 0x260, "OutputRowStride"), + ("u32", 0x264, "OutputPlaneStride"), + ("u32", 0x268, "OutputDepthStride"), + ("u32", 0x26C, "OutputBatchStride"), + + # 0x270 -- *(uint *)(this + 0x34c) = *(uint *)(this + 0x34c) & 0xf0ffffff | 0x1000000; + # uVar6 = *(uint *)(this + 0x34c) & 0xffffcfcc | 0x2031; + # (ZinTensorDescriptorDmaInterleave & 0xf) << 0x18; + ("u8", 0x273, "OutputInterleave"), # i also have this at 0x211? +] + +ANE_Struct_Dict = {} +for typ, num, nam in ANE_Struct: + styp = {"u32": "I", "u16": "H", "u8": "B"}[typ] + ANE_Struct_Dict[nam] = (styp, num) + +class ANETensor: + def __init__(self, *shape): + self.shape = shape + self.dtype = np.float16 + self.sz = int(np.prod(shape)) + assert(self.sz <= 0x4000) + self.tt = libane.ANE_TensorCreate(self.sz, 1) + assert(self.tt is not None) + + def data(self): + data = libane.ANE_TensorData(self.tt) + assert(data is not None) + #print(hex(addressof(data.contents))) + buf = np.ctypeslib.as_array(data, shape=(self.sz,)) + ret = np.frombuffer(buf, dtype=self.dtype) + #print(ret.data) + return ret + +class ANE: + def __init__(self): + init_libane() + libane.ANE_Open() + + def compile(self, dat): + ret = libane.ANE_Compile(create_string_buffer(dat), len(dat)) + assert(ret is not None) + return ret + + def run(self, prog, tin, tout, tweights=None): + libane.ANE_Run(prog, tin.tt, tout.tt, tweights.tt if tweights is not None else 0) + + def tensor(self, shape): + return ANETensor(shape) + + def unpack(self, dat): + dat = struct.unpack("Q"*(len(dat)//8), dat) + ret = {} + for k,v in aneregs: + by,bi,sz = v + bi += (by%8)*8 + by //= 8 + rv = (dat[by] >> bi) & ((1 << sz)-1) + ret[k] = rv + return ret + + def pack(self, pk, dat): + dat = list(struct.unpack("Q"*(len(dat)//8), dat)) + for k,v in aneregs: + by,bi,sz = v + bi += (by%8)*8 + by //= 8 + dat[by] &= ~(((1 << sz)-1) << bi) + dat[by] |= pk[k] << bi + dat = struct.pack("Q"*len(dat), *dat) + return dat + + def debug(self, dat, mems=0): + add = [0x30, 0x1d4, 0x220, 0x29c, 0x2f0, 0x30c, 0x32c] + lens = [244, 60, 108, 68, 12, 16, 24] + ptr = 0x2b + ddat = dat[0:0x28] + for a, pm in zip(add, lens): + #assert pm == dat[ptr] + ddat += b"\x00" * (a-len(ddat)) + ddat += dat[ptr+1:ptr+1+pm+4] + ptr += pm+8 + ddat += b"\x00" * 0x100 + ret = collections.OrderedDict() + for ln in libane.ANE_RegDebug(0, create_string_buffer(ddat), mems).decode('utf-8').strip().split("\n"): + lnn = ln.split(" = ") + if len(lnn) == 2: + ret[lnn[0]] = int(lnn[1]) + return ret + + def filln(self, dat, nvdict, base=0x4000): + for n,v in nvdict.items(): + styp, num = ANE_Struct_Dict[n] + dat = self.fill(dat, [num], styp, v) + return dat + + def fill(self, dat, addrs, type, val, base=0x4000): + x = struct.pack(type, val) + for a in addrs: + dat[base+a:base+a+len(x)] = x + return dat + +if __name__ == "__main__": + ane = ANE() + + tin = ANETensor(16) + tout = ANETensor(16) + + tind = tin.data() + toutd = tout.data() + + tind[0:4] = [-1,1,-2,2] + print("** before **") + print(tind) + print(toutd) + + dat = open("../ops/relu.hwx", "rb").read() + md = dat[0x4000:0x4300] + dd = ane.unpack(md) + mdf = ane.pack(dd, md) + assert(md == mdf) + + comp = ane.compile(dat) + ret = ane.run(comp, tin, tout) + print("** after **") + print(tind) + print(toutd) + diff --git a/tinygrad_repo/extra/accel/ane/2_compile/aneregs.json b/tinygrad_repo/extra/accel/ane/2_compile/aneregs.json index 2d097904..3862d695 120000 --- a/tinygrad_repo/extra/accel/ane/2_compile/aneregs.json +++ b/tinygrad_repo/extra/accel/ane/2_compile/aneregs.json @@ -1 +1,2066 @@ -../lib/aneregs.json \ No newline at end of file +[ + [ + "aneTD.Header[0].TID", + [ + 0, + 0, + 16 + ] + ], + [ + "aneTD.Header[0].NID", + [ + 2, + 0, + 8 + ] + ], + [ + "aneTD.Header[0].LNID", + [ + 3, + 0, + 1 + ] + ], + [ + "aneTD.Header[0].EON", + [ + 3, + 1, + 1 + ] + ], + [ + "aneTD.Header[1].ExeCycles", + [ + 4, + 0, + 16 + ] + ], + [ + "aneTD.Header[1].NextSize", + [ + 6, + 0, + 9 + ] + ], + [ + "aneTD.Header[2].LogEvents", + [ + 8, + 0, + 24 + ] + ], + [ + "aneTD.Header[3].Exceptions", + [ + 12, + 0, + 24 + ] + ], + [ + "aneTD.Header[4].DebugLogEvents", + [ + 16, + 0, + 24 + ] + ], + [ + "aneTD.Header[5].DebugExceptions", + [ + 20, + 0, + 24 + ] + ], + [ + "aneTD.Header[6].DisallowAbort", + [ + 25, + 0, + 1 + ] + ], + [ + "aneTD.Header[6].TDSkip", + [ + 25, + 1, + 1 + ] + ], + [ + "aneTD.Header[6].KPC", + [ + 25, + 2, + 1 + ] + ], + [ + "aneTD.Header[6].SPL", + [ + 25, + 3, + 1 + ] + ], + [ + "aneTD.Header[6].TSR", + [ + 25, + 4, + 1 + ] + ], + [ + "aneTD.Header[6].SPC", + [ + 25, + 5, + 1 + ] + ], + [ + "aneTD.Header[6].DPC", + [ + 25, + 6, + 1 + ] + ], + [ + "aneTD.Header[6].TSE", + [ + 25, + 7, + 1 + ] + ], + [ + "aneTD.Header[6].NextPriority", + [ + 26, + 0, + 6 + ] + ], + [ + "aneTD.Header[6].TDE", + [ + 27, + 0, + 1 + ] + ], + [ + "aneTD.Header[6].SrcLoc", + [ + 27, + 4, + 1 + ] + ], + [ + "aneTD.Header[6].DstLoc", + [ + 27, + 5, + 1 + ] + ], + [ + "aneTD.Header[6].TQDis", + [ + 27, + 7, + 1 + ] + ], + [ + "aneTD.Header[7].NextPointer", + [ + 28, + 0, + 32 + ] + ], + [ + "aneTD.Header[8].RBase0", + [ + 32, + 0, + 5 + ] + ], + [ + "aneTD.Header[8].RBE0", + [ + 32, + 5, + 1 + ] + ], + [ + "aneTD.Header[8].RBase1", + [ + 32, + 6, + 5 + ] + ], + [ + "aneTD.Header[8].RBE1", + [ + 33, + 3, + 1 + ] + ], + [ + "aneTD.Header[8].WBase", + [ + 33, + 4, + 5 + ] + ], + [ + "aneTD.Header[8].WBE", + [ + 34, + 1, + 1 + ] + ], + [ + "aneTD.Header[8].TBase", + [ + 34, + 2, + 5 + ] + ], + [ + "aneTD.Header[8].TBE", + [ + 34, + 7, + 1 + ] + ], + [ + "aneTD.Header[8].ENE", + [ + 35, + 0, + 3 + ] + ], + [ + "aneTD.Header[9].KBase0", + [ + 36, + 0, + 5 + ] + ], + [ + "aneTD.Header[9].KBE0", + [ + 36, + 5, + 1 + ] + ], + [ + "aneTD.Header[9].KBase1", + [ + 36, + 6, + 5 + ] + ], + [ + "aneTD.Header[9].KBE1", + [ + 37, + 3, + 1 + ] + ], + [ + "aneTD.Header[9].KBase2", + [ + 37, + 4, + 5 + ] + ], + [ + "aneTD.Header[9].KBE2", + [ + 38, + 1, + 1 + ] + ], + [ + "aneTD.Header[9].KBase3", + [ + 38, + 2, + 5 + ] + ], + [ + "aneTD.Header[9].KBE3", + [ + 38, + 7, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[0].En", + [ + 52, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[0].CrH", + [ + 52, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[0].CacheHint", + [ + 52, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[0].PrefetchParticipateEn", + [ + 55, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[1].En", + [ + 56, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[1].CrH", + [ + 56, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[1].CacheHint", + [ + 56, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[1].PrefetchParticipateEn", + [ + 59, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[2].En", + [ + 60, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[2].CrH", + [ + 60, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[2].CacheHint", + [ + 60, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[2].PrefetchParticipateEn", + [ + 63, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[3].En", + [ + 64, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[3].CrH", + [ + 64, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[3].CacheHint", + [ + 64, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[3].PrefetchParticipateEn", + [ + 67, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[4].En", + [ + 68, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[4].CrH", + [ + 68, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[4].CacheHint", + [ + 68, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[4].PrefetchParticipateEn", + [ + 71, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[5].En", + [ + 72, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[5].CrH", + [ + 72, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[5].CacheHint", + [ + 72, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[5].PrefetchParticipateEn", + [ + 75, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[6].En", + [ + 76, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[6].CrH", + [ + 76, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[6].CacheHint", + [ + 76, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[6].PrefetchParticipateEn", + [ + 79, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[7].En", + [ + 80, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[7].CrH", + [ + 80, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[7].CacheHint", + [ + 80, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[7].PrefetchParticipateEn", + [ + 83, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[8].En", + [ + 84, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[8].CrH", + [ + 84, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[8].CacheHint", + [ + 84, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[8].PrefetchParticipateEn", + [ + 87, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[9].En", + [ + 88, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[9].CrH", + [ + 88, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[9].CacheHint", + [ + 88, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[9].PrefetchParticipateEn", + [ + 91, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[10].En", + [ + 92, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[10].CrH", + [ + 92, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[10].CacheHint", + [ + 92, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[10].PrefetchParticipateEn", + [ + 95, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[11].En", + [ + 96, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[11].CrH", + [ + 96, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[11].CacheHint", + [ + 96, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[11].PrefetchParticipateEn", + [ + 99, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[12].En", + [ + 100, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[12].CrH", + [ + 100, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[12].CacheHint", + [ + 100, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[12].PrefetchParticipateEn", + [ + 103, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[13].En", + [ + 104, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[13].CrH", + [ + 104, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[13].CacheHint", + [ + 104, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[13].PrefetchParticipateEn", + [ + 107, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[14].En", + [ + 108, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[14].CrH", + [ + 108, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[14].CacheHint", + [ + 108, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[14].PrefetchParticipateEn", + [ + 111, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[15].En", + [ + 112, + 0, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[15].CrH", + [ + 112, + 4, + 2 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[15].CacheHint", + [ + 112, + 6, + 4 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffDMAConfig[15].PrefetchParticipateEn", + [ + 115, + 4, + 1 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[0].Addr", + [ + 116, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[1].Addr", + [ + 120, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[2].Addr", + [ + 124, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[3].Addr", + [ + 128, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[4].Addr", + [ + 132, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[5].Addr", + [ + 136, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[6].Addr", + [ + 140, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[7].Addr", + [ + 144, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[8].Addr", + [ + 148, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[9].Addr", + [ + 152, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[10].Addr", + [ + 156, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[11].Addr", + [ + 160, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[12].Addr", + [ + 164, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[13].Addr", + [ + 168, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[14].Addr", + [ + 172, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBaseAddr[15].Addr", + [ + 176, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[0].MemBfrSize", + [ + 180, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[1].MemBfrSize", + [ + 184, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[2].MemBfrSize", + [ + 188, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[3].MemBfrSize", + [ + 192, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[4].MemBfrSize", + [ + 196, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[5].MemBfrSize", + [ + 200, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[6].MemBfrSize", + [ + 204, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[7].MemBfrSize", + [ + 208, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[8].MemBfrSize", + [ + 212, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[9].MemBfrSize", + [ + 216, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[10].MemBfrSize", + [ + 220, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[11].MemBfrSize", + [ + 224, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[12].MemBfrSize", + [ + 228, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[13].MemBfrSize", + [ + 232, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[14].MemBfrSize", + [ + 236, + 6, + 26 + ] + ], + [ + "aneRegs.KernelDMASrc.CoeffBfrSize[15].MemBfrSize", + [ + 240, + 6, + 26 + ] + ], + [ + "aneRegs.Common.InDim.Win", + [ + 296, + 0, + 15 + ] + ], + [ + "aneRegs.Common.InDim.Hin", + [ + 298, + 0, + 15 + ] + ], + [ + "aneRegs.Common.ChCfg.InFmt", + [ + 304, + 0, + 2 + ] + ], + [ + "aneRegs.Common.ChCfg.OutFmt", + [ + 304, + 4, + 2 + ] + ], + [ + "aneRegs.Common.Cin.Cin", + [ + 308, + 0, + 17 + ] + ], + [ + "aneRegs.Common.Cout.Cout", + [ + 312, + 0, + 17 + ] + ], + [ + "aneRegs.Common.OutDim.Wout", + [ + 316, + 0, + 15 + ] + ], + [ + "aneRegs.Common.OutDim.Hout", + [ + 318, + 0, + 15 + ] + ], + [ + "aneRegs.Common.ConvCfg.Kw", + [ + 324, + 0, + 5 + ] + ], + [ + "aneRegs.Common.ConvCfg.Kh", + [ + 324, + 5, + 5 + ] + ], + [ + "aneRegs.Common.ConvCfg.OCGSize", + [ + 325, + 2, + 3 + ] + ], + [ + "aneRegs.Common.ConvCfg.Sx", + [ + 325, + 5, + 2 + ] + ], + [ + "aneRegs.Common.ConvCfg.Sy", + [ + 325, + 7, + 2 + ] + ], + [ + "aneRegs.Common.ConvCfg.Px", + [ + 326, + 1, + 5 + ] + ], + [ + "aneRegs.Common.ConvCfg.Py", + [ + 326, + 6, + 5 + ] + ], + [ + "aneRegs.Common.ConvCfg.Ox", + [ + 327, + 4, + 2 + ] + ], + [ + "aneRegs.Common.ConvCfg.Oy", + [ + 327, + 6, + 2 + ] + ], + [ + "aneRegs.Common.GroupConvCfg.NumGroups", + [ + 332, + 0, + 13 + ] + ], + [ + "aneRegs.Common.GroupConvCfg.UnicastEn", + [ + 333, + 6, + 1 + ] + ], + [ + "aneRegs.Common.GroupConvCfg.ElemMultMode", + [ + 333, + 7, + 1 + ] + ], + [ + "aneRegs.Common.GroupConvCfg.UnicastCin", + [ + 334, + 0, + 16 + ] + ], + [ + "aneRegs.Common.TileCfg.TileHeight", + [ + 336, + 0, + 15 + ] + ], + [ + "aneRegs.Common.Cfg.SmallSourceMode", + [ + 348, + 2, + 1 + ] + ], + [ + "aneRegs.Common.Cfg.ShPref", + [ + 349, + 0, + 3 + ] + ], + [ + "aneRegs.Common.Cfg.ShMin", + [ + 349, + 4, + 3 + ] + ], + [ + "aneRegs.Common.Cfg.ShMax", + [ + 350, + 0, + 3 + ] + ], + [ + "aneRegs.Common.Cfg.ActiveNE", + [ + 350, + 3, + 3 + ] + ], + [ + "aneRegs.Common.Cfg.ContextSwitchIn", + [ + 350, + 6, + 1 + ] + ], + [ + "aneRegs.Common.Cfg.ContextSwitchOut", + [ + 351, + 0, + 1 + ] + ], + [ + "aneRegs.Common.Cfg.AccDoubleBufEn", + [ + 351, + 2, + 1 + ] + ], + [ + "aneRegs.Common.TaskInfo.TaskID", + [ + 352, + 0, + 16 + ] + ], + [ + "aneRegs.Common.TaskInfo.TaskQ", + [ + 354, + 0, + 4 + ] + ], + [ + "aneRegs.Common.TaskInfo.NID", + [ + 354, + 4, + 8 + ] + ], + [ + "aneRegs.Common.DPE.Category", + [ + 356, + 0, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.En", + [ + 364, + 0, + 1 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.CrH", + [ + 364, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.CacheHint", + [ + 364, + 6, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.CacheHintReuse", + [ + 365, + 2, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.CacheHintNoReuse", + [ + 365, + 6, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.DMAConfig.DependencyMode", + [ + 366, + 2, + 2 + ] + ], + [ + "aneRegs.TileDMASrc.BaseAddr.Addr", + [ + 372, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMASrc.RowStride.Stride", + [ + 376, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMASrc.PlaneStride.PlaneStride", + [ + 380, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMASrc.DepthStride.Stride", + [ + 384, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMASrc.GroupStride.Stride", + [ + 388, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.FmtMode", + [ + 420, + 0, + 2 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.Truncate", + [ + 420, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.Shift", + [ + 421, + 0, + 1 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.MemFmt", + [ + 421, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.OffsetCh", + [ + 422, + 0, + 3 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.Interleave", + [ + 423, + 0, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.Fmt.CmpVec", + [ + 423, + 4, + 4 + ] + ], + [ + "aneRegs.TileDMASrc.PixelOffset[0].Offset", + [ + 444, + 0, + 16 + ] + ], + [ + "aneRegs.TileDMASrc.PixelOffset[1].Offset", + [ + 448, + 0, + 16 + ] + ], + [ + "aneRegs.TileDMASrc.PixelOffset[2].Offset", + [ + 452, + 0, + 16 + ] + ], + [ + "aneRegs.TileDMASrc.PixelOffset[3].Offset", + [ + 456, + 0, + 16 + ] + ], + [ + "aneRegs.L2.L2Cfg.InputReLU", + [ + 480, + 0, + 1 + ] + ], + [ + "aneRegs.L2.L2Cfg.PaddingMode", + [ + 480, + 2, + 2 + ] + ], + [ + "aneRegs.L2.SourceCfg.SourceType", + [ + 484, + 0, + 2 + ] + ], + [ + "aneRegs.L2.SourceCfg.Dependent", + [ + 484, + 2, + 2 + ] + ], + [ + "aneRegs.L2.SourceCfg.AliasConvSrc", + [ + 484, + 4, + 1 + ] + ], + [ + "aneRegs.L2.SourceCfg.AliasConvRslt", + [ + 484, + 5, + 1 + ] + ], + [ + "aneRegs.L2.SourceCfg.DMAFmt", + [ + 484, + 6, + 2 + ] + ], + [ + "aneRegs.L2.SourceCfg.DMAInterleave", + [ + 485, + 0, + 4 + ] + ], + [ + "aneRegs.L2.SourceCfg.DMACmpVec", + [ + 485, + 4, + 4 + ] + ], + [ + "aneRegs.L2.SourceCfg.DMAOffsetCh", + [ + 486, + 0, + 3 + ] + ], + [ + "aneRegs.L2.SourceCfg.AliasPlanarSrc", + [ + 486, + 4, + 1 + ] + ], + [ + "aneRegs.L2.SourceCfg.AliasPlanarRslt", + [ + 486, + 6, + 1 + ] + ], + [ + "aneRegs.L2.SourceBase.Addr", + [ + 488, + 4, + 17 + ] + ], + [ + "aneRegs.L2.SourceChannelStride.Stride", + [ + 492, + 4, + 17 + ] + ], + [ + "aneRegs.L2.SourceRowStride.Stride", + [ + 496, + 4, + 17 + ] + ], + [ + "aneRegs.L2.ResultCfg.ResultType", + [ + 528, + 0, + 2 + ] + ], + [ + "aneRegs.L2.ResultCfg.L2BfrMode", + [ + 528, + 3, + 1 + ] + ], + [ + "aneRegs.L2.ResultCfg.AliasConvSrc", + [ + 528, + 4, + 1 + ] + ], + [ + "aneRegs.L2.ResultCfg.AliasConvRslt", + [ + 528, + 5, + 1 + ] + ], + [ + "aneRegs.L2.ResultCfg.DMAFmt", + [ + 528, + 6, + 2 + ] + ], + [ + "aneRegs.L2.ResultCfg.DMAInterleave", + [ + 529, + 0, + 4 + ] + ], + [ + "aneRegs.L2.ResultCfg.DMACmpVec", + [ + 529, + 4, + 4 + ] + ], + [ + "aneRegs.L2.ResultCfg.DMAOffsetCh", + [ + 530, + 0, + 3 + ] + ], + [ + "aneRegs.L2.ResultCfg.AliasPlanarSrc", + [ + 530, + 4, + 1 + ] + ], + [ + "aneRegs.L2.ResultCfg.AliasPlanarRslt", + [ + 530, + 6, + 1 + ] + ], + [ + "aneRegs.L2.ResultBase.Addr", + [ + 532, + 4, + 17 + ] + ], + [ + "aneRegs.L2.ConvResultChannelStride.Stride", + [ + 536, + 4, + 17 + ] + ], + [ + "aneRegs.L2.ConvResultRowStride.Stride", + [ + 540, + 4, + 17 + ] + ], + [ + "aneRegs.NE.KernelCfg.KernelFmt", + [ + 576, + 0, + 2 + ] + ], + [ + "aneRegs.NE.KernelCfg.PalettizedEn", + [ + 576, + 2, + 1 + ] + ], + [ + "aneRegs.NE.KernelCfg.PalettizedBits", + [ + 576, + 4, + 4 + ] + ], + [ + "aneRegs.NE.KernelCfg.SparseFmt", + [ + 577, + 0, + 1 + ] + ], + [ + "aneRegs.NE.KernelCfg.GroupKernelReuse", + [ + 577, + 2, + 1 + ] + ], + [ + "aneRegs.NE.MACCfg.OpMode", + [ + 580, + 0, + 3 + ] + ], + [ + "aneRegs.NE.MACCfg.KernelMode", + [ + 580, + 3, + 1 + ] + ], + [ + "aneRegs.NE.MACCfg.BiasMode", + [ + 580, + 4, + 1 + ] + ], + [ + "aneRegs.NE.MACCfg.MatrixBiasEn", + [ + 580, + 6, + 1 + ] + ], + [ + "aneRegs.NE.MACCfg.BinaryPoint", + [ + 581, + 0, + 5 + ] + ], + [ + "aneRegs.NE.MACCfg.PostScaleMode", + [ + 581, + 6, + 1 + ] + ], + [ + "aneRegs.NE.MACCfg.NonlinearMode", + [ + 582, + 0, + 2 + ] + ], + [ + "aneRegs.NE.MatrixVectorBias.MatrixVectorBias", + [ + 584, + 0, + 16 + ] + ], + [ + "aneRegs.NE.AccBias.AccBias", + [ + 588, + 0, + 16 + ] + ], + [ + "aneRegs.NE.AccBias.AccBiasShift", + [ + 590, + 0, + 5 + ] + ], + [ + "aneRegs.NE.PostScale.PostScale", + [ + 592, + 0, + 16 + ] + ], + [ + "aneRegs.NE.PostScale.PostRightShift", + [ + 594, + 0, + 5 + ] + ], + [ + "aneRegs.TileDMADst.DMAConfig.En", + [ + 600, + 0, + 1 + ] + ], + [ + "aneRegs.TileDMADst.DMAConfig.CrH", + [ + 600, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMADst.DMAConfig.CacheHint", + [ + 600, + 6, + 4 + ] + ], + [ + "aneRegs.TileDMADst.DMAConfig.L2BfrMode", + [ + 603, + 2, + 1 + ] + ], + [ + "aneRegs.TileDMADst.DMAConfig.BypassEOW", + [ + 603, + 3, + 1 + ] + ], + [ + "aneRegs.TileDMADst.BaseAddr.Addr", + [ + 604, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMADst.RowStride.RowStride", + [ + 608, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMADst.PlaneStride.PlaneStride", + [ + 612, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMADst.DepthStride.DepthStride", + [ + 616, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMADst.GroupStride.GroupStride", + [ + 620, + 6, + 26 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.FmtMode", + [ + 624, + 0, + 2 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.Truncate", + [ + 624, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.Shift", + [ + 625, + 0, + 1 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.MemFmt", + [ + 625, + 4, + 2 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.OffsetCh", + [ + 626, + 0, + 3 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.ZeroPadLast", + [ + 626, + 4, + 1 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.ZeroPadFirst", + [ + 626, + 5, + 1 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.CmpVecFill", + [ + 626, + 6, + 1 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.Interleave", + [ + 627, + 0, + 4 + ] + ], + [ + "aneRegs.TileDMADst.Fmt.CmpVec", + [ + 627, + 4, + 4 + ] + ] +] \ No newline at end of file diff --git a/tinygrad_repo/extra/accel/ane/lib/entitlements.xml b/tinygrad_repo/extra/accel/ane/lib/entitlements.xml index 6cdb8700..ffb6ff1b 120000 --- a/tinygrad_repo/extra/accel/ane/lib/entitlements.xml +++ b/tinygrad_repo/extra/accel/ane/lib/entitlements.xml @@ -1 +1,9 @@ -../3_run/entitlements.xml \ No newline at end of file + + + + + + com.apple.ane.iokit-user-access + + + diff --git a/tinygrad_repo/extra/accel/ane/lib/h11ane.h b/tinygrad_repo/extra/accel/ane/lib/h11ane.h index 53c61bf6..3cc1e544 120000 --- a/tinygrad_repo/extra/accel/ane/lib/h11ane.h +++ b/tinygrad_repo/extra/accel/ane/lib/h11ane.h @@ -1 +1,150 @@ -../3_run/h11ane.h \ No newline at end of file +enum ANEDeviceUsageType { + UsageNoProgram, + UsageWithProgram, // used in running process + UsageCompile // used in aned +}; + +struct H11ANEDeviceInfoStruct { + uint64_t program_handle; + uint64_t program_auth_code; + uint64_t sleep_timer; + uint64_t junk[0x100]; +}; + +struct H11ANEStatusStruct { + uint64_t junk[0x100]; +}; + +struct H11ANEProgramCreateArgsStruct { + void *program; + uint64_t program_length; + uint64_t empty[4]; + char has_signature; +}; + +struct H11ANEProgramCreateArgsStructOutput { + uint64_t program_handle; + int unknown[0x2000]; +}; + +struct H11ANEProgramPrepareArgsStruct { + uint64_t program_handle; + uint64_t flags; + uint64_t empty[0x100]; +}; + +struct H11ANEProgramRequestArgsStruct { + uint64_t args[0x1000]; +}; + +namespace H11ANE { + class H11ANEDevice; + + class H11ANEDeviceController { + public: + H11ANEDeviceController( + int (*callback)(H11ANE::H11ANEDeviceController*, void*, H11ANE::H11ANEDevice*), + void *arg); + int SetupDeviceController(); + private: // size is 0x50 + CFArrayRef array_ref; + mach_port_t *master_port; + IONotificationPortRef port_ref; + CFRunLoopSourceRef source_ref; + int (*callback)(H11ANE::H11ANEDeviceController*, void*, H11ANE::H11ANEDevice*); + void *callback_arg; + CFRunLoopRef run_loop_ref; + io_iterator_t io_iterator; + pthread_t thread_self; + uint64_t unused; + }; + + // we should switch to the IOKit kernel interface, it's likely a lot more stable + // actually this probably isn't true. ANEServices is normal dynamic links + // https://googleprojectzero.blogspot.com/2020/11/oops-i-missed-it-again.html + + // H11ANEInDirectPathClient + // _ANE_DeviceOpen + // _ANE_DeviceClose + // _ANE_ProgramSendRequest + + // * if they need kernel debugger attached + // H11ANEInUserClient + // _ANE_DeviceOpen + // _ANE_DeviceClose + // _ANE_ProgramSendRequest + // _ANE_ProgramCreate + // _ANE_ProgramPrepare + // _ANE_ProgramUnprepare + // _ANE_ProgramDestroy + // _ANE_GetStatus + // _ANE_PowerOn + // _ANE_PowerOff + // _ANE_IsPowered + // * _ANE_LoadFirmware + // * _ANE_ForgetFirmware + // * _ANE_SendCommand + // _ANE_SetPowerManagement + // _ANE_GetTime + // * _ANE_SetDriverLoggingFlags + // * _ANE_ShowSharedMemoryAllocations + // * _ANE_SetDARTCacheTTL + // * _ANE_SetFirmwareBootArg + // * _ANE_SetThrottlingPercentage + // * _ANE_AddPersistentClient + // * _ANE_RemovePersistentClient + // * _ANE_CreateClientLoggingSession + // * _ANE_TerminateClientLoggingSession + // _ANE_GetDriverLoggingFlags + // * _ANE_FlushInactiveDARTMappings + // _ANE_GetVersion + // _ANE_RegisterFirmwareWorkProcessor + // _ANE_UnregisterFirmwareWorkProcessor + // * _ANE_GetFirmwareWorkProcessorItem + // _ANE_CompleteFirmwareWorkProcessorItem + // _ANE_ReleaseFirmwareWorkProcessorBuffers + // * _ANE_ReadANERegister + // * _ANE_WriteANERegister + // _ANE_ProgramCreateInstance + + // note, this is not the raw IOKit class, it's in ANEServices.framework + class H11ANEDevice { + public: + H11ANEDevice(H11ANE::H11ANEDeviceController *param_1, unsigned int param_2); + + unsigned long H11ANEDeviceOpen( + int (*callback)(H11ANE::H11ANEDevice*, unsigned int, void*, void*), + void *param_2, ANEDeviceUsageType param_3, H11ANEDeviceInfoStruct *param_4); + + void EnableDeviceMessages(); + int ANE_AddPersistentClient(); + int ANE_GetStatus(H11ANEStatusStruct *param_1); + + // power management + int ANE_IsPowered(); + int ANE_PowerOn(); + int ANE_PowerOff(); + + // logging (e00002c7 error, needs PE_i_can_has_debugger) + int ANE_CreateClientLoggingSession(unsigned int log_iosurface); + int ANE_TerminateClientLoggingSession(unsigned int log_iosurface); + int ANE_GetDriverLoggingFlags(unsigned int *flags); + int ANE_SetDriverLoggingFlags(unsigned int flags); + + // program creation + int ANE_ProgramCreate(H11ANEProgramCreateArgsStruct*, + H11ANEProgramCreateArgsStructOutput*); + int ANE_ProgramPrepare(H11ANEProgramPrepareArgsStruct*); + int ANE_ProgramSendRequest(H11ANEProgramRequestArgsStruct*, mach_port_t); + + // need PE_i_can_has_debugger + int ANE_ReadANERegister(unsigned int param_1, unsigned int *param_2); + int ANE_ForgetFirmware(); + + + private: // size is 0x88 + unsigned char unknown[0x88]; + }; + +}; +