- UART takes baudrate on init
- UART calculate optimized baud prescaler git-svn-id: http://moon:8086/svn/projects/HendiControl@47 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "machine.h"
|
||||||
#include "fifo.h"
|
#include "fifo.h"
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
@@ -23,16 +24,8 @@
|
|||||||
#include "mcp42x6.h"
|
#include "mcp42x6.h"
|
||||||
|
|
||||||
#define PRINT(a,...) printf(a, ##__VA_ARGS__)
|
#define PRINT(a,...) printf(a, ##__VA_ARGS__)
|
||||||
#define ARDUINO_NANO 1
|
|
||||||
|
|
||||||
#define PRINT_PROMPT putchar(0x0D);putchar(0x0A);putchar(':');
|
#define PRINT_PROMPT putchar(0x0D);putchar(0x0A);putchar(':');
|
||||||
#if ARDUINO_NANO
|
#define USART_BAUDRATE 115200UL
|
||||||
#define F_CPU (16000000UL) // MHz
|
|
||||||
#else
|
|
||||||
#define F_CPU (18432000UL) // MHz
|
|
||||||
#endif
|
|
||||||
#define USART_BAUDRATE 9600UL
|
|
||||||
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
|
|
||||||
#define TIMER_HW_CLOCK (F_CPU/8)
|
#define TIMER_HW_CLOCK (F_CPU/8)
|
||||||
#define TIMER_IRQ_CLOCK 1000UL
|
#define TIMER_IRQ_CLOCK 1000UL
|
||||||
#define TIMER_HW_RELOAD (0xFFFF-(TIMER_HW_CLOCK/TIMER_IRQ_CLOCK))
|
#define TIMER_HW_RELOAD (0xFFFF-(TIMER_HW_CLOCK/TIMER_IRQ_CLOCK))
|
||||||
@@ -96,7 +89,7 @@ int main(void)
|
|||||||
fifo_init(&messageFifo, 8, sizeof(Msg_t), "Fifo");
|
fifo_init(&messageFifo, 8, sizeof(Msg_t), "Fifo");
|
||||||
|
|
||||||
/* Replace with your application code */
|
/* Replace with your application code */
|
||||||
uart_init(&messageFifo, BAUD_PRESCALE);
|
uart_init(&messageFifo, USART_BAUDRATE);
|
||||||
timer_init(&messageFifo, TIMER_HW_RELOAD, TimerClockSel_PS_8);
|
timer_init(&messageFifo, TIMER_HW_RELOAD, TimerClockSel_PS_8);
|
||||||
adc_init(&messageFifo);
|
adc_init(&messageFifo);
|
||||||
i2c_init();
|
i2c_init();
|
||||||
|
|||||||
@@ -33,13 +33,31 @@ ISR(USART_RX_vect)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE)
|
void uart_init(Fifo *pFifo, uint32_t baudrate)
|
||||||
{
|
{
|
||||||
g_pFifo = pFifo;
|
g_pFifo = pFifo;
|
||||||
|
|
||||||
|
uint16_t prescale8 = UART_PRESCALE(baudrate, 8UL);
|
||||||
|
uint16_t prescale16 = UART_PRESCALE(baudrate, 16UL);
|
||||||
|
|
||||||
|
uint32_t baud8 = ((prescale8+1) * 8);
|
||||||
|
uint32_t baud16 = ((prescale16+1) * 16);
|
||||||
|
|
||||||
|
uint32_t err8 = abs(baudrate - baud8);
|
||||||
|
uint32_t err16 = abs(baudrate - baud16);
|
||||||
|
|
||||||
|
uint16_t prescale = prescale16;
|
||||||
|
if (err8 < err16)
|
||||||
|
{
|
||||||
|
prescale = prescale8;
|
||||||
|
|
||||||
|
// Double UART speed
|
||||||
|
UCSR0A |= 1 << U2X0;
|
||||||
|
}
|
||||||
|
|
||||||
// Set baud rate
|
// Set baud rate
|
||||||
UBRR0L = (uint8_t)BAUD_PRESCALE;
|
UBRR0L = (uint8_t)prescale;
|
||||||
UBRR0H = (uint8_t)(BAUD_PRESCALE >> 8);
|
UBRR0H = (uint8_t)(prescale >> 8);
|
||||||
|
|
||||||
// Enable receiver and transmitter
|
// Enable receiver and transmitter
|
||||||
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
|
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
|
||||||
|
|||||||
@@ -9,11 +9,15 @@
|
|||||||
#ifndef UART_H_
|
#ifndef UART_H_
|
||||||
#define UART_H_
|
#define UART_H_
|
||||||
|
|
||||||
|
#include "machine.h"
|
||||||
#include "fifo.h"
|
#include "fifo.h"
|
||||||
|
|
||||||
void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE);
|
#define UART_PRESCALE(baudrate, smpPerBit) ((5+ 10*F_CPU / (baudrate*smpPerBit) - 1)/10)
|
||||||
|
|
||||||
|
void uart_init(Fifo *pFifo, uint32_t baudrate);
|
||||||
void uart_putc(char c);
|
void uart_putc(char c);
|
||||||
int uart_putchar(char c, FILE *stream);
|
int uart_putchar(char c, FILE *stream);
|
||||||
void uart_puts(char const *str);
|
void uart_puts(char const *str);
|
||||||
|
|
||||||
|
|
||||||
#endif /* UART_H_ */
|
#endif /* UART_H_ */
|
||||||
@@ -95,6 +95,7 @@
|
|||||||
<avrgcc.compiler.symbols.DefSymbols>
|
<avrgcc.compiler.symbols.DefSymbols>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>NDEBUG</Value>
|
<Value>NDEBUG</Value>
|
||||||
|
<Value>ARDUINO_NANO=1</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.compiler.symbols.DefSymbols>
|
</avrgcc.compiler.symbols.DefSymbols>
|
||||||
<avrgcc.compiler.directories.IncludePaths>
|
<avrgcc.compiler.directories.IncludePaths>
|
||||||
@@ -117,7 +118,7 @@
|
|||||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value>
|
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.assembler.general.IncludePaths>
|
</avrgcc.assembler.general.IncludePaths>
|
||||||
</AvrGcc>
|
</AvrGcc>
|
||||||
</ToolchainSettings>
|
</ToolchainSettings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
@@ -134,6 +135,7 @@
|
|||||||
<avrgcc.compiler.symbols.DefSymbols>
|
<avrgcc.compiler.symbols.DefSymbols>
|
||||||
<ListValues>
|
<ListValues>
|
||||||
<Value>DEBUG</Value>
|
<Value>DEBUG</Value>
|
||||||
|
<Value>ARDUINO_NANO=1</Value>
|
||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.compiler.symbols.DefSymbols>
|
</avrgcc.compiler.symbols.DefSymbols>
|
||||||
<avrgcc.compiler.directories.IncludePaths>
|
<avrgcc.compiler.directories.IncludePaths>
|
||||||
@@ -157,7 +159,7 @@
|
|||||||
</ListValues>
|
</ListValues>
|
||||||
</avrgcc.assembler.general.IncludePaths>
|
</avrgcc.assembler.general.IncludePaths>
|
||||||
<avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel>
|
<avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel>
|
||||||
</AvrGcc>
|
</AvrGcc>
|
||||||
</ToolchainSettings>
|
</ToolchainSettings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -179,6 +181,9 @@
|
|||||||
<Compile Include="i2c.h">
|
<Compile Include="i2c.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="machine.h">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
<Compile Include="main.c">
|
<Compile Include="main.c">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
Reference in New Issue
Block a user