API GUIDE / TNPU / HAL

TNPU HAL 接口

HAL API

bool hal_tnpu_is_initialized(const hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_init(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_deinit(hal_tnpu_handle_t *handle);

uint32_t hal_tnpu_boot_firmware_size_get(void);
error_code_t hal_tnpu_boot_area_get(hal_tnpu_handle_t *handle,
                                    __IO uint8_t **area_out);
error_code_t hal_tnpu_boot_area_write(hal_tnpu_handle_t *handle,
                                      uint32_t offset,
                                      const void *data,
                                      size_t size);

error_code_t hal_tnpu_arguments_write(hal_tnpu_handle_t *handle,
                                      uint32_t target_pc,
                                      uint32_t arg0, uint32_t arg1,
                                      uint32_t arg2, uint32_t arg3);
error_code_t hal_tnpu_result_get(hal_tnpu_handle_t *handle,
                                 uint32_t *result_out);
error_code_t hal_tnpu_stack_guard_write(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_stack_guard_is_valid(hal_tnpu_handle_t *handle,
                                           bool *is_valid_out);
error_code_t hal_tnpu_task_start(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_mailbox_command_send(hal_tnpu_handle_t *handle,
                                           uint32_t command);
error_code_t hal_tnpu_mailbox_response_get(hal_tnpu_handle_t *handle,
                                            uint32_t *response_out);
error_code_t hal_tnpu_mailbox_response_clear(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_interrupt_disable(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_callback_register(hal_tnpu_handle_t *handle,
                                        hal_tnpu_callback_t callback);
error_code_t hal_tnpu_user_data_set(hal_tnpu_handle_t *handle,
                                    void *user_data);
void hal_tnpu_irq_handler(hal_tnpu_handle_t *handle);
error_code_t hal_tnpu_interrupt_flags_get(hal_tnpu_handle_t *handle,
                                          uint32_t *flags_out);
error_code_t hal_tnpu_error_status_get(hal_tnpu_handle_t *handle,
                                       uint32_t *status_out);
error_code_t hal_tnpu_error_epc_get(hal_tnpu_handle_t *handle,
                                    uint32_t *epc_out);

hal_tnpu_init() 会依次:

  1. 使能 HAL_CPM_ID_GATE_TNPU 时钟门;
  2. 复位 TNPU;
  3. 配置 PMA 和异常控制;
  4. 写入并校验 boot 固件;
  5. 释放复位;
  6. 等待 sleep/ready 指示;
  7. 关闭 ORAM 时钟门控。

它不会提交或等待任务。它会初始化调用方提供的 handle 存储;TNPU 是单硬件块,因此如果已有另一个活动的 HAL 或 middleware 会话,会返回 IM_ERR_BUSY。hal_tnpu_is_initialized() 用于判断给定 handle 是否拥有该活动会话。

boot 区写入 API 会保护内置固件占用区。用户可以把 DRAM 驻留内核放到 1 KiB boot 区的剩余空间。