1 /* 2 * Copyright (c) 2023 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef UART_LIN_H 9 #define UART_LIN_H 10 11 #include "hpm_clock_drv.h" 12 #include "hpm_gpio_drv.h" 13 #include "hpm_uart_drv.h" 14 15 /** 16 * 17 * @brief UART Lin component APIs 18 * @defgroup uart_lin_interface UART component APIs 19 * @ingroup io_interfaces 20 * @{ 21 */ 22 23 typedef struct uart_lin_pin { 24 GPIO_Type *ptr; 25 uint32_t baudrate; 26 void (*config_uart_pin)(UART_Type *ptr); 27 void (*config_uart_pin_as_gpio)(UART_Type *ptr); 28 void (*delay_us)(uint32_t us); 29 uint8_t tx_port; 30 uint8_t tx_pin; 31 } uart_lin_master_pin_ctrl_t; 32 33 typedef struct { 34 uint8_t *buff; 35 uint8_t length; 36 bool enhance_checksum; 37 } uart_lin_data_t; 38 39 typedef enum { 40 uart_lin_success = 0, 41 uart_lin_fail = 1, 42 uart_lin_invalid_argument = 2, 43 uart_lin_timeout = 3, 44 uart_lin_id_parity_error = 4, 45 uart_lin_checksum_error = 5, 46 uart_lin_frame_error = 6, /*<! data count error */ 47 } uart_lin_stat_t; 48 49 typedef struct { 50 UART_Type *ptr; 51 uint8_t id; /* master use id */ 52 uart_lin_data_t data; 53 uart_lin_master_pin_ctrl_t pin_ctrl; 54 } uart_lin_master_config_t; 55 56 typedef struct { 57 UART_Type *ptr; 58 uint8_t pid; /* slave use pid */ 59 uart_lin_data_t data; 60 } uart_lin_slave_config_t; 61 62 #ifdef __cplusplus 63 extern "C" { 64 #endif 65 66 /** 67 * @brief calculate lin pid from id 68 * 69 * @param [in] id id value 70 * 71 * @return pid pid value 72 */ 73 uint8_t hpm_uart_lin_calculate_protected_id(uint8_t id); 74 75 /** 76 * @brief master send lin frame, including break, sync, pid, data and checksum 77 * 78 * @note this function using polling way to check uart status 79 * 80 * @param [in] config uart_lin_master_config_t 81 * 82 * @return uart_lin_stat_t uart_lin_success if master send without error 83 */ 84 uart_lin_stat_t hpm_uart_lin_master_send_frame(uart_lin_master_config_t *config); 85 86 /** 87 * @brief master receive lin frame. master send break, sync, pid, then receive data and checksum 88 * 89 * @note this function using polling way to check uart status 90 * 91 * @param [in] config uart_lin_master_config_t 92 * 93 * @return uart_lin_stat_t uart_lin_success if master receive without error 94 */ 95 uart_lin_stat_t hpm_uart_lin_master_receive_frame(uart_lin_master_config_t *config); 96 97 /** 98 * @brief master send data and checksum 99 * 100 * @note this function write 0x55 and pid into tx fifo 101 * 102 * @param [in] config uart_lin_master_config_t 103 */ 104 void hpm_uart_lin_master_send_head(uart_lin_master_config_t *config); 105 106 /** 107 * @brief master send data and checksum 108 * 109 * @note this function write data and checksum into tx fifo 110 * 111 * @param [in] config uart_lin_master_config_t 112 */ 113 void hpm_uart_lin_master_send_data(uart_lin_master_config_t *config); 114 115 /** 116 * @brief master receive and check data&checksum 117 * 118 * @note this function read data and checksum already in rx fifo 119 * 120 * @param [in] config uart_lin_master_config_t 121 * 122 * @return uart_lin_stat_t uart_lin_success if receive without error 123 */ 124 uart_lin_stat_t hpm_uart_lin_master_receive_data(uart_lin_master_config_t *config); 125 126 /** 127 * @brief slave send data and checksum 128 * 129 * @note this function write data and checksum into tx fifo 130 * 131 * @param [in] config uart_lin_slave_config_t 132 */ 133 void hpm_uart_lin_slave_send_data(uart_lin_slave_config_t *config); 134 135 /** 136 * @brief salve receive and check data&checksum 137 * 138 * @note this function read data and checksum already in rx fifo 139 * 140 * @param [in] config uart_lin_slave_config_t 141 * 142 * @return uart_lin_stat_t uart_lin_success if receive without error 143 */ 144 uart_lin_stat_t hpm_uart_lin_slave_receive_data(uart_lin_slave_config_t *config); 145 146 #ifdef __cplusplus 147 } 148 #endif 149 150 /** 151 * @} 152 */ 153 154 #endif /* UART_LIN_H */ 155 156