Skip to content

Use asynchronous fractional mode to configure UART #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,21 @@ void SERCOM::initUART(SercomUartMode mode, SercomUartSampleRate sampleRate, uint

if ( mode == UART_INT_CLOCK )
{
uint16_t sampleRateValue ;
uint16_t sampleRateValue;

if ( sampleRate == SAMPLE_RATE_x16 )
{
sampleRateValue = 16 ;
}
else
{
if ( sampleRate == SAMPLE_RATE_x8 )
{
sampleRateValue = 8 ;
}
else
{
sampleRateValue = 3 ;
}
if (sampleRate == SAMPLE_RATE_x16) {
sampleRateValue = 16;
} else {
sampleRateValue = 8;
}

// Asynchronous arithmetic mode
// 65535 * ( 1 - sampleRateValue * baudrate / SystemCoreClock);
// 65535 - 65535 * (sampleRateValue * baudrate / SystemCoreClock));
sercom->USART.BAUD.reg = 65535.0f * ( 1.0f - (float)(sampleRateValue) * (float)(baudrate) / (float)(SystemCoreClock));
// Asynchronous fractional mode (Table 24-2 in datasheet)
// BAUD = fref / (sampleRateValue * fbaud)
// (multiply by 8, to calculate fractional piece)
uint32_t baudTimes8 = (SystemCoreClock * 8) / (sampleRateValue * baudrate);

sercom->USART.BAUD.FRAC.FP = (baudTimes8 % 8);
sercom->USART.BAUD.FRAC.BAUD = (baudTimes8 / 8);
}
}
void SERCOM::initFrame(SercomUartCharSize charSize, SercomDataOrder dataOrder, SercomParityMode parityMode, SercomNumberStopBit nbStopBits)
Expand Down
5 changes: 2 additions & 3 deletions cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ typedef enum

typedef enum
{
SAMPLE_RATE_x16 = 0, //Arithmetic
SAMPLE_RATE_x8 = 0x2, //Arithmetic
SAMPLE_RATE_x3 = 0x3 //Arithmetic
SAMPLE_RATE_x16 = 0x1, //Fractional
SAMPLE_RATE_x8 = 0x3, //Fractional
} SercomUartSampleRate;

typedef enum
Expand Down