static inline void STOP_RX_HND(UART_Type *Uart, UART_DRIVER_INFO * drv_info, HANDLE hnd) { STOP_RX(Uart); drv_info->drv_data->hnd_rcv = hnd->next; hnd->len = drv_info->drv_data->rx_remaining; usr_HND_SET_STATUS(hnd, RES_SIG_OK); if( (hnd=drv_info->drv_data->hnd_rcv) ) START_RX_HND(Uart, drv_info, hnd); else START_RX_BUF(Uart, drv_info, drv_info->drv_data); }
/** Stop receiving hnd * * @param pUsart * @param drv_data * @param hnd */ static void STOP_RX_HND(Usart* pUsart, UART_DRIVER_DATA drv_data, HANDLE hnd) { STOP_RX(pUsart); drv_data->hnd_rcv = hnd->next; hnd->len =pUsart->US_RCR; hnd->dst.as_int = pUsart->US_RPR; usr_HND_SET_STATUS(hnd, RES_SIG_OK); if( (hnd=drv_data->hnd_rcv) ) START_RX_HND(pUsart, drv_data, hnd); else START_RX_BUF(pUsart, drv_data); }
//*---------------------------------------------------------------------------- //* ISR function //*---------------------------------------------------------------------------- void UART_ISR(UART_INFO drv_info ) { HANDLE hnd; unsigned int status; Uart* pUart; UART_DRIVER_DATA drv_data; drv_data = drv_info->drv_data; pUart = drv_info->hw_base; status = pUart->UART_SR; status &= pUart->UART_IMR; // check the transmitter if(status & UART_SR_TXBUFE) { pUart->UART_IDR = UART_IDR_TXBUFE; if( (hnd=drv_data->hnd_snd) ) { drv_data->hnd_snd = hnd->next; hnd->len = 0; hnd->src.as_int = pUart->UART_TPR; usr_HND_SET_STATUS(hnd, RES_SIG_OK); if( drv_data->hnd_snd ) START_TX_HND(pUart, drv_data->hnd_snd); } } //check the receiver if(status & UART_SR_ENDRX) { if( (hnd=drv_data->hnd_rcv) ) { STOP_RX_HND(pUart, drv_data, hnd); } else START_RX_BUF(pUart, drv_data); } else { // process tout if( (hnd=drv_data->hnd_rcv) ) if(hnd->dst.as_int != pUart->UART_RPR) if(drv_data->rtout) if(!--drv_data->rtout) { STOP_RX_HND(pUart, drv_data, hnd); } } }
/** USART DCR * * @param drv_info * @param reason * @param param */ void USART_DCR(USART_INFO drv_info, unsigned int reason, HANDLE param) { UART_DRIVER_DATA drv_data = drv_info->drv_data; unsigned int temp; switch(reason) { case DCR_RESET: drv_data->buf_size = drv_info->buf_size; USART_OFF(drv_info); break; case DCR_OPEN: { DRV_UART_MODE pMode = (DRV_UART_MODE)(param->mode.as_voidptr); if(pMode) { if(drv_data->cnt) { if( (pMode->mode != drv_data->mode) || (pMode->baudrate != drv_data->baudrate)) return; } else { drv_data->mode = pMode->mode; drv_data->baudrate =pMode->baudrate; USART_CFG(drv_info, pMode); START_RX_BUF(drv_info->hw_base, drv_data); } drv_data->cnt++; param->res = RES_OK; } break; } case DCR_CLOSE: if(drv_data->cnt) if(!--drv_data->cnt) USART_OFF(drv_info); break; case DCR_CANCEL: { if (param->cmd & FLAG_READ) { if(param == drv_data->hnd_rcv) { STOP_RX(drv_info->hw_base); temp = drv_info->hw_base->US_RCR; if(param->len > temp) { param->len = temp; param->dst.as_int = drv_info->hw_base->US_RPR; temp = RES_OK; } else temp = RES_SIG_IDLE; drv_data->hnd_rcv = param->next; if( drv_data->hnd_rcv ) START_RX_HND(drv_info->hw_base, drv_data, drv_data->hnd_rcv); else START_RX_BUF(drv_info->hw_base, drv_data); svc_HND_SET_STATUS(param, temp); } else param->svc_list_cancel(drv_data->hnd_rcv); } else { if(param == drv_data->hnd_snd) { STOP_TX(drv_info->hw_base); temp = drv_info->hw_base->US_TCR; if(param->len > temp) { param->len = temp; param->src.as_int = drv_info->hw_base->US_TPR; temp = RES_OK; } else temp = RES_SIG_IDLE; drv_data->hnd_snd = param->next; if( drv_data->hnd_snd ) START_TX_HND(drv_info->hw_base, drv_data->hnd_snd); svc_HND_SET_STATUS(param, temp); } else param->svc_list_cancel(drv_data->hnd_snd); } break; } case DCR_CLOCK: if(drv_data->cnt) drv_info->hw_base->US_BRGR = AT91_GetDiv(drv_data->baudrate); break; } }
void isr_SerilaDriver(UART_DRIVER_INFO* drv_info ) { UART_Type * Uart = drv_info->hw_base; UART_DRIVER_DATA * drv_data = drv_info->drv_data; HANDLE hnd; unsigned int Status = Uart->UARTIntStatus(true); Uart->UARTIntClear(Status); if( Status & UART_INT_TX) { if((hnd=drv_data->hnd_snd)) { while(Uart->UARTSpaceAvail()) { if(!hnd->len) { drv_data->hnd_snd = hnd->next; usr_HND_SET_STATUS(hnd, RES_SIG_OK); if(!(hnd=drv_data->hnd_snd)) { STOP_TX(Uart); break; } } hnd->len--; Uart->DR = *hnd->src.as_charptr++; } if(hnd) { Uart->UARTTxIntModeSet(UART_TXINT_MODE_FIFO); } } else { STOP_TX(Uart); } } //check the receiver if(Status&(UART_INT_RT|UART_INT_RX)) { while(Uart->UARTCharsAvail()) { drv_data->rx_remaining--; *drv_data->rx_wrptr++ = Uart->DR; if(drv_data->hnd_rcv && !drv_data->hnd_rcv->mode0) drv_data->hnd_rcv->mode0 = 1; if(!drv_data->rx_remaining ) { if( (hnd=drv_data->hnd_rcv) ) STOP_RX_HND(Uart, drv_info, hnd); else START_RX_BUF(Uart, drv_info, drv_data); } #ifdef HW_VER_10 else { if(drv_data->mode.hw_flow && drv_info->info.drv_index != UART1_IRQn) if((!drv_data->hnd_rcv) && (drv_data->rx_remaining < (RX_BUF_SIZE/4))) { if(drv_info->uart_pins[RTS_PIN]) PIO_SetOutput(drv_info->uart_pins[RTS_PIN]); } } #endif } if( (Status&UART_INT_RT) && drv_data->mode.rx_tout ) { if( (hnd=drv_data->hnd_rcv) ) { if(drv_data->rx_remaining != hnd->len) STOP_RX_HND(Uart, drv_info, hnd); } } } }
void dcr_SerialDriver(UART_DRIVER_INFO* drv_info, unsigned int reason, HANDLE hnd) { UART_Type * Uart = drv_info->hw_base; UART_DRIVER_DATA *drv_data = drv_info->drv_data; switch(reason) { case DCR_ISR: TX_CTS(drv_data, Uart, (unsigned int)hnd); return; case DCR_RESET: SysCtlPeripheralReset(drv_info->info.peripheral_indx); SysCtlPeripheralDisable(drv_info->info.peripheral_indx); // ??? turn off return; case DCR_OPEN: { UART_DRIVER_MODE *uart_mode = (UART_DRIVER_MODE *)(hnd->mode.as_voidptr); if(uart_mode) { //unsigned long mode, baudrate; if(drv_data->cnt) { if( uart_mode->mode != drv_data->mode.mode || uart_mode->baudrate != drv_data->mode.baudrate || uart_mode->hw_flow != drv_data->mode.hw_flow ) { return; } } else { // Enable AND Reset the UART peripheral SysCtlPeripheralReset(drv_info->info.peripheral_indx); if(drv_data->mode.hw_flow) { PIO_Cfg_List((PIN_DESC *)&drv_info->uart_pins[UART_LIST_ALL_PINS]); ConfigureUart(drv_info, drv_data, uart_mode); #ifdef HW_VER_10 if(drv_info->info.drv_index != UART1_IRQn) TX_CTS(drv_data, Uart, PIO_Read(drv_info->uart_pins[CTS_PIN])); #endif } else { PIO_Cfg_List((PIN_DESC *)&drv_info->uart_pins[UART_LIST_RX_TX_PINS]); ConfigureUart(drv_info, drv_data, uart_mode); } START_RX_BUF(Uart, drv_info, drv_data); } drv_data->cnt++; hnd->res = RES_OK; } return; } case DCR_CLOSE: if(drv_data->cnt) drv_data->cnt--; case DCR_CANCEL: { if( !(hnd->res & FLG_BUSY)) return; if (hnd->cmd & FLAG_READ) { if(hnd == drv_data->hnd_rcv) { STOP_RX(Uart); if(hnd->len > drv_data->rx_remaining || hnd->mode0 ) { hnd->len = drv_data->rx_remaining; hnd->res = RES_SIG_OK; } else hnd->res = RES_SIG_IDLE; drv_data->hnd_rcv = hnd->next; svc_HND_SET_STATUS(hnd, hnd->res); if( (hnd=drv_data->hnd_rcv) ) START_RX_HND(Uart, drv_info, hnd); else START_RX_BUF(Uart, drv_info, drv_data); } else { // try cnacel hnd->svc_list_cancel(drv_data->hnd_rcv); } } else { if(hnd == drv_data->hnd_snd) { STOP_TX(Uart); drv_data->hnd_snd = hnd->next; svc_HND_SET_STATUS(hnd, RES_SIG_IDLE); if( (hnd=drv_data->hnd_snd) ) START_TX_HND(Uart, hnd); } else { hnd->svc_list_cancel(drv_data->hnd_snd); } } if(!drv_data->cnt) { NVIC->NVIC_DisableIRQ(drv_info->info.drv_index); Uart->UARTDisable(); STOP_RX(Uart); STOP_TX(Uart); SysCtlPeripheralDisable(drv_info->info.peripheral_indx); SYSCTL->SysCtlPeripheralDeepSleepDisable(drv_info->info.peripheral_indx); SYSCTL->SysCtlPeripheralSleepDisable(drv_info->info.peripheral_indx); if(drv_data->mode.hw_flow) PIO_Free_List((PIN_DESC *)&drv_info->uart_pins[UART_LIST_ALL_PINS]); else PIO_Free_List((PIN_DESC *)&drv_info->uart_pins[UART_LIST_RX_TX_PINS]); } return; } case DCR_CLOCK: return; } }