SAMV71 Xplained Ultra Software Package 1.5

usart_cdc.c

00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 |
00036                                   US_MR_NBSTOP_1_BIT | US_MR_CHMODE_NORMAL);
00037 
00038 void USART_HANDLER(void)
00039 {
00040     uint32_t sr = USART_GetStatus(USART_BASE);
00041 
00042     if (sr & US_CSR_RXRDY) {
00043         /* Data received */
00044         ui_com_tx_start();
00045         uint32_t value;
00046 
00047         if ((!USART_IsRxReady(USART_BASE))
00048             || (sr & (US_CSR_FRAME | US_CSR_TIMEOUT | US_CSR_PARE))) {
00049             USART_ResetRx(USART_BASE);
00050             USART_EnableRx(USART_BASE);
00051             ui_com_error();
00052         } else
00053             value = USART_BASE->US_RHR;
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         ui_com_tx_stop();
00063         return;
00064     }
00065 
00066     if (sr & US_CSR_TXRDY) {
00067         /* Data ready to be sent */
00068         if (uhi_cdc_is_rx_ready(0)) {
00069             /* Transmit next data */
00070             ui_com_rx_start();
00071             int c = uhi_cdc_getc(0);
00072             USART_Write(USART_BASE, c, 0);
00073             //TRACE_INFO_WP("%c", c);
00074         } else {
00075             /* Fifo empty then Stop UART transmission */
00076             USART_DisableTx(USART_BASE);
00077             USART_DisableIt(USART_BASE, US_IDR_TXRDY);
00078             ui_com_rx_stop();
00079         }
00080     }
00081 }
00082 
00083 void uart_rx_notify(void)
00084 {
00085     /* If UART is open */
00086     if (USART_GetItMask(USART_BASE)
00087         & US_IMR_RXRDY) {
00088         /* Enable UART TX interrupt to send a new value */
00089         USART_EnableTx(USART_BASE);
00090         USART_EnableIt(USART_BASE, US_IER_TXRDY);
00091     }
00092 }
00093 
00094 void uart_config(CDCLineCoding *cfg)
00095 {
00096     uint32_t stopbits, parity, databits;
00097     uint32_t imr;
00098 
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 
00151     default:
00152     case 8:
00153         databits = US_MR_CHRL_8_BIT;
00154         TRACE_INFO_WP("\n\rUS_MR_CHRL_8_BIT");
00155         break;
00156     }
00157 
00158     /* Options for USART. */
00159     usart_options = (databits | parity | stopbits | US_MR_CHMODE_NORMAL);
00160 
00161     imr = USART_GetItMask(USART_BASE);
00162     USART_DisableIt(USART_BASE, 0xFFFFFFFF);
00163     TRACE_INFO_WP("\n\r115200 BAUDRATE");
00164     USART_Configure(USART_BASE, usart_options, cfg->dwDTERate, BOARD_MCK);
00165     /* Restore both RX and TX */
00166     USART_SetTransmitterEnabled(USART_BASE, true);
00167     USART_SetReceiverEnabled(USART_BASE, true);
00168     USART_EnableIt(USART_BASE, imr);
00169 }
00170 
00171 void uart_open(void)
00172 {
00173     /* IO is initialized in board init
00174      * Enable interrupt with priority higher than USB
00175      */
00176     NVIC_SetPriority(USART_INT_IRQn, USART_INT_LEVEL);
00177     NVIC_EnableIRQ(USART_INT_IRQn);
00178 
00179     /* Initialize it in RS232 mode. */
00180     PMC_EnablePeripheral(USART_ID);
00181     USART_Configure(USART_BASE, usart_options, 115200, BOARD_MCK)
00182 
00183     /* Enable USART */
00184     USART_ENABLE();
00185 
00186     /* Enable both RX and TX */
00187     USART_SetTransmitterEnabled(USART_BASE, true);
00188     USART_SetReceiverEnabled(USART_BASE, true);
00189     /* Enable interrupts */
00190     USART_EnableIt(USART_BASE, US_IER_RXRDY | US_IER_TXRDY);
00191 }
00192 
00193 void uart_close(void)
00194 {
00195     /* Disable interrupts */
00196     USART_DisableIt(USART_BASE, 0xFFFFFFFF);
00197     /* Close RS232 communication */
00198     USART_DISABLE();
00199 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines