#include "uart_interface.h" #include "elog.h" UartInterface_TypeDef * UartInterface_Init( ) { UartInterface_TypeDef *Handle = (UartInterface_TypeDef *)malloc(sizeof(UartInterface_TypeDef)); if (Handle == NULL) { return NULL; } // Handle->send_status = 0; Handle->interface_type = 0; #if MAX485_ENABLE Handle->de485_gpio = NULL; Handle->interface_type = 1; #endif #if MAX3160_ENABLE Handle->de485_gpio = NULL; Handle->sel_gpio = NULL; Handle->dplx_gpio = NULL; Handle->interface_type = 2; #endif return Handle; } /** * @brief interface-> 0:default, 1:485, 2:3160 * @param [in] myuart * @param [in] interface * @return int * * @details */ int UartInterface_Setup_Interface_type( UartInterface_TypeDef * interface, Uart_Interface_Type_Typedef interface_type ) { interface->interface_type = interface_type; return 0; } int UartInterface_Setup_Mode_232_485( UartInterface_TypeDef * interface, Uart_RS_Mode_TypeDef mode_232_485 ) { interface->mode_232_485 = mode_232_485; if (interface->interface_type == 0) UartInterface_Set_232( interface); if (interface->interface_type == 1) UartInterface_Set_485( interface); if (interface->interface_type ==2 && interface->mode_232_485 ==Uart_RSMODE_232) UartInterface_Set_232( interface); if (interface->interface_type ==2 && interface->mode_232_485 ==Uart_RSMODE_485) UartInterface_Set_485( interface); return 0; } void UartInterface_Set_Sel_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin) { if (gpio!=NULL && pin>=0) { #if MAX3160_ENABLE interface->sel_gpio = gpio; interface->sel_pin = pin; #endif } } void UartInterface_Set_Dplx_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin) { if (gpio!=NULL && pin>=0) { #if MAX3160_ENABLE interface->dplx_gpio = gpio; interface->dplx_pin = pin; #endif } // #if MAX3160_ENABLE // interface->dplx_gpio = gpio; // interface->dplx_pin = pin; // #endif } void UartInterface_Set_DE485_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin) { if (gpio!=NULL && pin>=0) { #if MAX3160_ENABLE interface->de485_gpio = gpio; interface->de485_pin = pin; #endif #if MAX485_ENABLE interface->de485_gpio = gpio; interface->de485_pin = pin; #endif } // interface->de485_gpio = gpio; // interface->de485_pin = pin; } void UartInterface_Set_232(UartInterface_TypeDef * interface) { interface->mode_232_485 = Uart_RSMODE_232; #if MAX3160_ENABLE if (interface->sel_gpio != NULL) HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); interface->sel = SEL_232; if (interface->sel_gpio != NULL) HAL_GPIO_WritePin( interface->sel_gpio, interface->sel_pin, interface->sel ); #endif // interface->mode_232_485 = 0; } void UartInterface_Set_485(UartInterface_TypeDef * interface) { interface->mode_232_485 = Uart_RSMODE_485; // log_i(".......set 485 "); #if MAX3160_ENABLE interface->sel = SEL_485; if (interface->sel_gpio != NULL) // log_i(".......set 485 set dplx "); HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_HALF); if (interface->de485_gpio != NULL) // log_i(".......set 485 set de "); HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND ); if (interface->sel_gpio != NULL) // log_i(".......set 485 set sel... "); HAL_GPIO_WritePin( interface->sel_gpio, interface->sel_pin, interface->sel ); // log_i(" UartInterface_Set_485 222 interface->sel : %d duplex: %d ",interface->sel , HAL_GPIO_ReadPin(interface->dplx_gpio,interface->dplx_pin) ); #endif #if MAX485_ENABLE if (interface->de485_gpio != NULL) // log_i(".......set 485 set de "); HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND ); #endif } /** * @brief 发送前操作 GPIO * @param [in] myuart * @return int * * @details */ int UartInterface_Set_GPIO_For_Transmit( UartInterface_TypeDef * interface ) // int UartInterface_Send_TxPrepare_Callback( UartInterface_TypeDef * interface ) { #if MAX3160_ENABLE if (interface->mode_232_485 == 0) { HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); }; if ( interface->interface_type == 2 && interface->mode_232_485 == 1 ) { // log_i( " ---- UartInterface_Set_GPIO_For_Transmit " ); HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); // 全双工启动发送 HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND); return 0; } #endif #if MAX485_ENABLE if (interface->interface_type == 1) { HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND); } #endif return 0; // #if MAX3160_ENABLE // if (interface->interface_type == 2 && ) // { // switch (interface->sel) // { // case SEL_232: // HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); // // return __UartInterface_Send(myuart,buf,size); // break; // case SEL_485: // log_i(" 485 mode writepin...... "); // HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); // HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND); // // return __UartInterface_Send(myuart,buf,size); // break; // default: // break; // } // } // #endif // #if MAX485_ENABLE // if (interface->interface_type == 1) // { // HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); // HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND); // return __Max3160_Send(max3160,buf,size); // } // #endif } /** * @brief 发送完成后操作 GPIO * @param [in] myuart * @return int * * @details */ // 接收完成,切换到接收状态, 全双工接收 int UartInterface_Set_GPIO_For_Trans_Cplt( UartInterface_TypeDef * interface ) { // interface->send_status = 0; #if MAX3160_ENABLE if ( interface->interface_type == 2 && interface->mode_232_485 == 1) { HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_HALF); // 半双工接收 HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND); return 0; } if (interface->mode_232_485 == 0) return -1; #endif #if MAX485_ENABLE if (interface->interface_type == 1) { HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND); return 0; } if (interface->mode_232_485 == 0) return -1; #endif return 0; }