<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl-pl">
<link rel="self" type="application/atom+xml" href="https://forum.atnel.pl/feed.php?f=4&amp;t=9971&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2015-01-01T16:26:52+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=4&amp;t=9971&amp;mode</id>
<entry>
<author><name><![CDATA[norbert22]]></name></author>
<updated>2015-01-01T16:26:52+01:00</updated>
<published>2015-01-01T16:26:52+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=9971&amp;p=111610#p111610</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=9971&amp;p=111610#p111610"/>
<title type="html"><![CDATA[UART w XMEGA problem z przesyłaniem większej ilości danych]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=9971&amp;p=111610#p111610"><![CDATA[
Witam, mam spory problem z uartem w xmedze 16d4. Używam gotowej biblioteki atmela:<br /><br />[syntax=c]/* This file has been prepared for Doxygen automatic documentation generation.*/<br />/*! \file *********************************************************************<br /> *<br /> * \brief<br /> *      XMEGA USART driver source file.<br /> *<br /> *      This file contains the function implementations the XMEGA interrupt<br /> *      and polled USART driver.<br /> *<br /> *      The driver is not intended for size and/or speed critical code, since<br /> *      most functions are just a few lines of code, and the function call<br /> *      overhead would decrease code performance. The driver is intended for<br /> *      rapid prototyping and documentation purposes for getting started with<br /> *      the XMEGA ADC module.<br /> *<br /> *      For size and/or speed critical code, it is recommended to copy the<br /> *      function contents directly into your application instead of making<br /> *      a function call.<br /> *<br /> *      Some functions use the following construct:<br /> *          &quot;some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ...&quot;<br /> *      Although the use of the ternary operator ( if ? then : else ) is discouraged,<br /> *      in some occasions the operator makes it possible to write pretty clean and<br /> *      neat code. In this driver, the construct is used to set or not set a<br /> *      configuration bit based on a boolean input parameter, such as<br /> *      the &quot;some_parameter&quot; in the example above.<br /> *<br /> * \par Application note:<br /> *      AVR1307: Using the XMEGA USART<br /> *<br /> * \par Documentation<br /> *      For comprehensive code documentation, supported compilers, compiler<br /> *      settings and supported devices see readme.html<br /> *<br /> * \author<br /> *      Atmel Corporation: http://www.atmel.com \n<br /> *      Support email: avr@atmel.com<br /> *<br /> * $Revision: 1694 $<br /> * $Date: 2008-07-29 14:21:58 +0200 (ti, 29 jul 2008) $  \n<br /> *<br /> * Copyright (c) 2008, Atmel Corporation All rights reserved.<br /> *<br /> * Redistribution and use in source and binary forms, with or without<br /> * modification, are permitted provided that the following conditions are met:<br /> *<br /> * 1. Redistributions of source code must retain the above copyright notice,<br /> * this list of conditions and the following disclaimer.<br /> *<br /> * 2. Redistributions in binary form must reproduce the above copyright notice,<br /> * this list of conditions and the following disclaimer in the documentation<br /> * and/or other materials provided with the distribution.<br /> *<br /> * 3. The name of ATMEL may not be used to endorse or promote products derived<br /> * from this software without specific prior written permission.<br /> *<br /> * THIS SOFTWARE IS PROVIDED BY ATMEL &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED<br /> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF<br /> * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND<br /> * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,<br /> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br /> * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;<br /> * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br /> * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT<br /> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF<br /> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br /> *****************************************************************************/<br />#include &quot;usart_driver.h&quot;<br /><br /><br /><br />/*! \brief Initializes buffer and selects what USART module to use.<br /> *<br /> *  Initializes receive and transmit buffer and selects what USART module to use,<br /> *  and stores the data register empty interrupt level.<br /> *<br /> *  \param usart_data           The USART_data_t struct instance.<br /> *  \param usart                The USART module.<br /> *  \param dreIntLevel          Data register empty interrupt level.<br /> */<br />void USART_InterruptDriver_Initialize(USART_data_t * usart_data,<br />                                      USART_t * usart,<br />                                      USART_DREINTLVL_t dreIntLevel)<br />{<br />usart_data-&gt;usart = usart;<br />usart_data-&gt;dreIntLevel = dreIntLevel;<br /><br />usart_data-&gt;buffer.RX_Tail = 0;<br />usart_data-&gt;buffer.RX_Head = 0;<br />usart_data-&gt;buffer.TX_Tail = 0;<br />usart_data-&gt;buffer.TX_Head = 0;<br />}<br /><br /><br />/*! \brief Set USART DRE interrupt level.<br /> *<br /> *  Set the interrupt level on Data Register interrupt.<br /> *<br /> *  \note Changing the DRE interrupt level in the interrupt driver while it is<br /> *        running will not change the DRE interrupt level in the USART before the<br /> *        DRE interrupt have been disabled and enabled again.<br /> *<br /> *  \param usart_data         The USART_data_t struct instance<br /> *  \param dreIntLevel        Interrupt level of the DRE interrupt.<br /> */<br />void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data,<br />                                                 USART_DREINTLVL_t dreIntLevel)<br />{<br />usart_data-&gt;dreIntLevel = dreIntLevel;<br />}<br /><br /><br />/*! \brief Test if there is data in the transmitter software buffer.<br /> *<br /> *  This function can be used to test if there is free space in the transmitter<br /> *  software buffer.<br /> *<br /> *  \param usart_data The USART_data_t struct instance.<br /> *<br /> *  \retval true      There is data in the receive buffer.<br /> *  \retval false     The receive buffer is empty.<br /> */<br />bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data)<br />{<br />/* Make copies to make sure that volatile access is specified. */<br />uint8_t tempHead = (usart_data-&gt;buffer.TX_Head + 1) &amp; USART_TX_BUFFER_MASK;<br />uint8_t tempTail = usart_data-&gt;buffer.TX_Tail;<br /><br />/* There are data left in the buffer unless Head and Tail are equal. */<br />return (tempHead != tempTail);<br />}<br /><br /><br /><br />/*! \brief Put data (5-8 bit character).<br /> *<br /> *  Stores data byte in TX software buffer and enables DRE interrupt if there<br /> *  is free space in the TX software buffer.<br /> *<br /> *  \param usart_data The USART_data_t struct instance.<br /> *  \param data       The data to send.<br /> */<br />bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data)<br />{<br />uint8_t tempCTRLA;<br />uint8_t tempTX_Head;<br />bool TXBuffer_FreeSpace;<br />USART_Buffer_t * TXbufPtr;<br /><br />TXbufPtr = &amp;usart_data-&gt;buffer;<br />TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data);<br /><br /><br />if(TXBuffer_FreeSpace)<br />{<br />  tempTX_Head = TXbufPtr-&gt;TX_Head;<br />  TXbufPtr-&gt;TX&#91;tempTX_Head&#93;= data;<br />/* Advance buffer head. */<br />TXbufPtr-&gt;TX_Head = (tempTX_Head + 1) &amp; USART_TX_BUFFER_MASK;<br /><br />/* Enable DRE interrupt. */<br />tempCTRLA = usart_data-&gt;usart-&gt;CTRLA;<br />tempCTRLA = (tempCTRLA &amp; ~USART_DREINTLVL_gm) | usart_data-&gt;dreIntLevel;<br />usart_data-&gt;usart-&gt;CTRLA = tempCTRLA;<br />}<br />return TXBuffer_FreeSpace;<br />}<br /><br /><br /><br />/*! \brief Test if there is data in the receive software buffer.<br /> *<br /> *  This function can be used to test if there is data in the receive software<br /> *  buffer.<br /> *<br /> *  \param usart_data         The USART_data_t struct instance<br /> *<br /> *  \retval true      There is data in the receive buffer.<br /> *  \retval false     The receive buffer is empty.<br /> */<br />bool USART_RXBufferData_Available(USART_data_t * usart_data)<br />{<br />/* Make copies to make sure that volatile access is specified. */<br />uint8_t tempHead = usart_data-&gt;buffer.RX_Head;<br />uint8_t tempTail = usart_data-&gt;buffer.RX_Tail;<br /><br />/* There are data left in the buffer unless Head and Tail are equal. */<br />return (tempHead != tempTail);<br />}<br /><br /><br /><br />/*! \brief Get received data (5-8 bit character).<br /> *<br /> *  The function USART_RXBufferData_Available should be used before this<br /> *  function is used to ensure that data is available.<br /> *<br /> *  Returns data from RX software buffer.<br /> *<br /> *  \param usart_data       The USART_data_t struct instance.<br /> *<br /> *  \return         Received data.<br /> */<br />uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data)<br />{<br />USART_Buffer_t * bufPtr;<br />uint8_t ans;<br /><br />bufPtr = &amp;usart_data-&gt;buffer;<br />ans = (bufPtr-&gt;RX&#91;bufPtr-&gt;RX_Tail&#93;);<br /><br />/* Advance buffer tail. */<br />bufPtr-&gt;RX_Tail = (bufPtr-&gt;RX_Tail + 1) &amp; USART_RX_BUFFER_MASK;<br /><br />return ans;<br />}<br /><br /><br /><br />/*! \brief RX Complete Interrupt Service Routine.<br /> *<br /> *  RX Complete Interrupt Service Routine.<br /> *  Stores received data in RX software buffer.<br /> *<br /> *  \param usart_data      The USART_data_t struct instance.<br /> */<br />bool USART_RXComplete(USART_data_t * usart_data)<br />{<br />USART_Buffer_t * bufPtr;<br />bool ans;<br /><br />bufPtr = &amp;usart_data-&gt;buffer;<br />/* Advance buffer head. */<br />uint8_t tempRX_Head = (bufPtr-&gt;RX_Head + 1) &amp; USART_RX_BUFFER_MASK;<br /><br />/* Check for overflow. */<br />uint8_t tempRX_Tail = bufPtr-&gt;RX_Tail;<br />uint8_t data = usart_data-&gt;usart-&gt;DATA;<br /><br />if (tempRX_Head == tempRX_Tail) {<br />  ans = false;<br />}else{<br />ans = true;<br />usart_data-&gt;buffer.RX&#91;usart_data-&gt;buffer.RX_Head&#93; = data;<br />usart_data-&gt;buffer.RX_Head = tempRX_Head;<br />}<br />return ans;<br />}<br /><br /><br /><br />/*! \brief Data Register Empty Interrupt Service Routine.<br /> *<br /> *  Data Register Empty Interrupt Service Routine.<br /> *  Transmits one byte from TX software buffer. Disables DRE interrupt if buffer<br /> *  is empty. Argument is pointer to USART (USART_data_t).<br /> *<br /> *  \param usart_data      The USART_data_t struct instance.<br /> */<br />void USART_DataRegEmpty(USART_data_t * usart_data)<br />{<br />USART_Buffer_t * bufPtr;<br />bufPtr = &amp;usart_data-&gt;buffer;<br /><br />/* Check if all data is transmitted. */<br />uint8_t tempTX_Tail = usart_data-&gt;buffer.TX_Tail;<br />if (bufPtr-&gt;TX_Head == tempTX_Tail){<br />    /* Disable DRE interrupts. */<br />uint8_t tempCTRLA = usart_data-&gt;usart-&gt;CTRLA;<br />tempCTRLA = (tempCTRLA &amp; ~USART_DREINTLVL_gm) | USART_DREINTLVL_OFF_gc;<br />usart_data-&gt;usart-&gt;CTRLA = tempCTRLA;<br /><br />}else{<br />/* Start transmitting. */<br />uint8_t data = bufPtr-&gt;TX&#91;usart_data-&gt;buffer.TX_Tail&#93;;<br />usart_data-&gt;usart-&gt;DATA = data;<br /><br />/* Advance buffer tail. */<br />bufPtr-&gt;TX_Tail = (bufPtr-&gt;TX_Tail + 1) &amp; USART_TX_BUFFER_MASK;<br />}<br />}<br /><br /><br />/*! \brief Put data (9 bit character).<br /> *<br /> *  Use the function USART_IsTXDataRegisterEmpty before using this function to<br /> *  put 9 bit character to the TX register.<br /> *<br /> *  \param usart      The USART module.<br /> *  \param data       The data to send.<br /> */<br />void USART_NineBits_PutChar(USART_t * usart, uint16_t data)<br />{<br />if(data &amp; 0x0100) {<br />usart-&gt;CTRLB |= USART_TXB8_bm;<br />}else {<br />usart-&gt;CTRLB &amp;= ~USART_TXB8_bm;<br />}<br /><br />usart-&gt;DATA = (data &amp; 0x00FF);<br />}<br /><br /><br />/*! \brief Get received data (9 bit character).<br /> *<br /> *  This function reads out the received 9 bit character (uint16_t).<br /> *  Use the function USART_IsRXComplete to check if anything is received.<br /> *<br /> *  \param usart      The USART module.<br /> *<br /> *  \retval           Received data.<br /> */<br />uint16_t USART_NineBits_GetChar(USART_t * usart)<br />{<br />if(usart-&gt;CTRLB &amp; USART_RXB8_bm) {<br />return(0x0100 | usart-&gt;DATA);<br />}else {<br />return(usart-&gt;DATA);<br />}<br />}<br /><br />bool USART_TXBuffer_PutString(USART_data_t * usart_data, const char* str)<br />{<br />while(*str != 0)<br />{<br />USART_TXBuffer_PutByte( usart_data, (uint8_t)*str );<br />str++;<br />}<br />return true;<br />}[/syntax]<br /><br />[syntax=c]void USART_init()<br />{<br /><br />PORTC_OUTSET=PIN3_bm;<br />PORTC_DIRSET=PIN3_bm;                          //Pin TxD musi być wyjściem<br />USART_InterruptDriver_Initialize(&amp;USART_data, &amp;USART, USART_DREINTLVL_LO_gc);<br />/* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */<br />USART_Format_Set(USART_data.usart, USART_CHSIZE_8BIT_gc,<br />                     USART_PMODE_DISABLED_gc, false);<br /><br />/* Enable RXC interrupt. */<br />USART_RxdInterruptLevel_Set(USART_data.usart, USART_RXCINTLVL_LO_gc);<br /><br />/* Set Baudrate to 9600 bps:<br /> * Use the default I/O clock frequency that is 2 MHz.<br /> * Do not use the baudrate scale factor<br /> *<br /> * Baudrate select = (1/(16*(((I/O clock frequency)/Baudrate)-1)<br /> *                 = 12<br /> */<br />USART_Baudrate_Set(&amp;USART, 150 , -7);<br /><br />/* Enable both RX and TX. */<br />USART_Rx_Enable(USART_data.usart);<br />USART_Tx_Enable(USART_data.usart);<br /><br />/* Enable PMIC interrupt level low. */<br />PMIC.CTRL |= PMIC_LOLVLEX_bm;<br />} <br /><br />ISR(USARTC0_RXC_vect)<br />{<br />USART_RXComplete(&amp;USART_data);<br /><br />}<br /><br />ISR(USARTC0_DRE_vect)<br />{<br />USART_DataRegEmpty(&amp;USART_data);<br /><br />}[/syntax]<br /><br />Po wysłaniu zwykłej instrukcji:<br />[syntax=c]for(int g=0;g&lt;20;g++)<br />USART_TXBuffer_PutByte(&amp;USART_data,'i');<br />break;[/syntax]<br />na terminalu pojawia się tylko 10 znaków i zamiast 20. Co jest nie tak?<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=5632">norbert22</a> — 1 sty 2015, o 16:26</p><hr />
]]></content>
</entry>
</feed>