SAMV71 Xplained Ultra Software Package 1.4

usart_cdc.c

00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2015, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 #include "conf_usb_host.h"
00031 #include "board.h"
00032 
00033 
00034 /* Default option */
00035 static uint32_t usart_options = ( US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL);
00036 
00037 void USART_HANDLER(void)
00038 {
00039     uint32_t sr = USART_GetStatus(USART_BASE);
00040     if (sr & US_CSR_RXRDY) {
00041         /* Data received */
00042         ui_com_tx_start();
00043         uint32_t value;
00044         if ((!USART_IsRxReady)|| (sr & (US_CSR_FRAME | US_CSR_TIMEOUT | US_CSR_PARE)))
00045         {
00046             USART_ResetRx(USART_BASE);
00047             USART_EnableRx(USART_BASE);
00048             ui_com_error();
00049         }
00050         else
00051         {
00052          value = USART_BASE->US_RHR ;
00053         }
00054 
00055         /* Transfer UART RX fifo to CDC TX */
00056         if (!uhi_cdc_is_tx_ready(0)) {
00057             /* Fifo full */
00058             ui_com_overflow();
00059         } else {
00060             uhi_cdc_putc(0, value);
00061         }
00062 
00063         ui_com_tx_stop();
00064         return;
00065     }
00066 
00067     if (sr & US_CSR_TXRDY) {
00068         /* Data ready to be sent */
00069         if (uhi_cdc_is_rx_ready(0)) {
00070             /* Transmit next data */
00071             ui_com_rx_start();
00072             int c = uhi_cdc_getc(0);
00073             USART_Write(USART_BASE, c, 0);
00074             //TRACE_INFO_WP("%c", c);
00075         } else {
00076             /* Fifo empty then Stop UART transmission */
00077             USART_DisableTx(USART_BASE);
00078             USART_DisableIt(USART_BASE, US_IDR_TXRDY);
00079             ui_com_rx_stop();
00080         }
00081     }
00082 }
00083 
00084 void uart_rx_notify(void)
00085 {
00086     /* If UART is open */
00087     if (USART_GetItMask(USART_BASE)
00088         & US_IMR_RXRDY) {
00089         /* Enable UART TX interrupt to send a new value */
00090         USART_EnableTx(USART_BASE);
00091         USART_EnableIt(USART_BASE, US_IER_TXRDY);
00092     }
00093 }
00094 
00095 void uart_config(CDCLineCoding *cfg)
00096 {
00097     uint32_t stopbits, parity, databits;
00098     uint32_t imr;
00099     switch (cfg->bCharFormat) {
00100     case CDCLineCoding_TWOSTOPBITS:
00101         stopbits = US_MR_NBSTOP_2_BIT;
00102         TRACE_INFO_WP("\n\rUS_MR_NBSTOP_2_BIT");
00103         break;
00104 
00105     case CDCLineCoding_ONE5STOPBIT:
00106         stopbits = US_MR_NBSTOP_1_5_BIT;
00107         TRACE_INFO_WP("\n\rUS_MR_NBSTOP_1_5_BIT");
00108         break;
00109 
00110     case CDCLineCoding_ONESTOPBIT:
00111     default:
00112         /* Default stop bit = 1 stop bit */
00113         stopbits = US_MR_NBSTOP_1_BIT;
00114         TRACE_INFO_WP("\n\rUS_MR_NBSTOP_1_BIT");
00115         break;
00116     }
00117 
00118     switch (cfg->bParityType) {
00119     case CDCLineCoding_EVENPARITY:
00120         parity = US_MR_PAR_EVEN;
00121         TRACE_INFO_WP("\n\rUS_MR_PAR_EVEN");
00122         break;
00123 
00124     case CDCLineCoding_ODDPARITY:
00125         parity = US_MR_PAR_ODD;
00126         TRACE_INFO_WP("\n\rUS_MR_PAR_ODD");
00127         break;
00128 
00129     case CDCLineCoding_MARKPARITY:
00130         parity = US_MR_PAR_MARK;
00131         TRACE_INFO_WP("\n\rUS_MR_PAR_MARK");
00132         break;
00133 
00134     case CDCLineCoding_SPACEPARITY:
00135         parity = US_MR_PAR_SPACE;
00136         TRACE_INFO_WP("\n\rUS_MR_PAR_SPACE");
00137         break;
00138 
00139     default:
00140     case CDCLineCoding_NOPARITY:
00141         parity = US_MR_PAR_NO;
00142         TRACE_INFO_WP("\n\rUS_MR_PAR_NO");
00143         break;
00144     }
00145 
00146     switch(cfg->bDataBits) {
00147     case 5: case 6: case 7:
00148         databits = cfg->bDataBits - 5;
00149         break;
00150     default:
00151     case 8:
00152         databits = US_MR_CHRL_8_BIT;
00153         TRACE_INFO_WP("\n\rUS_MR_CHRL_8_BIT");
00154         break;
00155     }
00156 
00157     /* Options for USART. */
00158     usart_options = (databits | parity |stopbits | US_MR_CHMODE_NORMAL);
00159     
00160     imr = USART_GetItMask(USART_BASE);
00161     USART_DisableIt(USART_BASE, 0xFFFFFFFF);
00162     TRACE_INFO_WP("\n\r115200 BAUDRATE");
00163     USART_Configure(USART_BASE, usart_options, cfg->dwDTERate, BOARD_MCK);
00164     /* Restore both RX and TX */
00165     USART_SetTransmitterEnabled(USART_BASE, true);
00166     USART_SetReceiverEnabled(USART_BASE, true);
00167     USART_EnableIt(USART_BASE, imr);
00168 }
00169 
00170 void uart_open(void)
00171 {
00172     /* IO is initialized in board init
00173      * Enable interrupt with priority higher than USB
00174      */
00175     NVIC_SetPriority(USART_INT_IRQn, USART_INT_LEVEL);
00176     NVIC_EnableIRQ(USART_INT_IRQn);
00177 
00178     /* Initialize it in RS232 mode. */
00179     PMC_EnablePeripheral(USART_ID);
00180     USART_Configure(USART_BASE, usart_options, 115200, BOARD_MCK)
00181 
00182     /* Enable USART */
00183     USART_ENABLE();
00184 
00185     /* Enable both RX and TX */
00186     USART_SetTransmitterEnabled(USART_BASE, true);
00187     USART_SetReceiverEnabled(USART_BASE, true);
00188     /* Enable interrupts */
00189     USART_EnableIt(USART_BASE, US_IER_RXRDY | US_IER_TXRDY);
00190 }
00191 
00192 void uart_close(void)
00193 {
00194     /* Disable interrupts */
00195     USART_DisableIt(USART_BASE, 0xFFFFFFFF);
00196     /* Close RS232 communication */
00197     USART_DISABLE();
00198 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines