Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
284 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* Target configuration */ | ||
#define F_CPU (16000000L) | ||
#define JUMP_ADDR (0x00) | ||
|
||
/* SWUART pin-configuration */ | ||
#define CONFIG_SWUART_RX_BIT (PB3) | ||
#define CONFIG_SWUART_RX_PORT (PORTB) | ||
#define CONFIG_SWUART_RX_PIN (PINB) | ||
#define CONFIG_SWUART_RX_DIR (DDRB) | ||
|
||
#define CONFIG_SWUART_TX_BIT (PB4) | ||
#define CONFIG_SWUART_TX_PORT (PORTB) | ||
#define CONFIG_SWUART_TX_DIR (DDRB) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* Copyright (C) 2010-2011 Magnus Olsson | ||
* | ||
* This file is part of magboot | ||
* Windnode is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Windnode is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with magboot. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <stdint.h> | ||
#include <avr/io.h> | ||
|
||
void uart_putc(uint8_t ch) | ||
{ | ||
loop_until_bit_is_set(UCSR0A, UDRE0); | ||
UDR0 = ch; | ||
} | ||
|
||
uint8_t uart_getc(void) | ||
{ | ||
uint8_t ch; | ||
|
||
loop_until_bit_is_set(UCSR0A, RXC0); | ||
ch = UDR0; | ||
|
||
return ch; | ||
} | ||
|
||
void uart_init() | ||
{ | ||
UCSR0A = _BV(U2X0); | ||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); | ||
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01); | ||
UBRR0L = (uint8_t)((F_CPU + BAUD_RATE * 4L)/(BAUD_RATE * 8L) - 1); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* Copyright (C) 2010-2011 Magnus Olsson | ||
* | ||
* This file is part of magboot | ||
* Windnode is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Windnode is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with magboot. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <avr/io.h> | ||
#include <avr/interrupt.h> | ||
#include <stdint.h> | ||
|
||
#include "uart.h" | ||
|
||
#define STARTBIT (0) | ||
#define STOPBIT (1) | ||
|
||
/* Calculate baudtime using timer pre-scaler 64 */ | ||
#define BAUDTIME ((F_CPU)/(BAUD_RATE*64l)) | ||
|
||
#if (BAUDTIME > 255) | ||
#error Baudrate is too low! | ||
#endif | ||
|
||
#if (BAUDTIME < 1) | ||
#error Baudrate is too high! | ||
#endif | ||
|
||
static void __bit_delay(void) | ||
{ | ||
TCCR0B |= _BV(CS01) | _BV(CS00); /* Timer pre-scaler 64 */ | ||
while (TCNT0 < BAUDTIME) { /* NOP */ } | ||
TCCR0B &= ~(_BV(CS01) | _BV(CS00)); | ||
} | ||
|
||
static void bit_delay(void) | ||
{ | ||
TCNT0 = 0; | ||
__bit_delay(); | ||
} | ||
|
||
static void half_bit_delay(void) | ||
{ | ||
TCNT0 = BAUDTIME/2; | ||
__bit_delay(); | ||
} | ||
|
||
static uint8_t rx_pin(void) | ||
{ | ||
return (CONFIG_SWUART_RX_PIN & _BV(CONFIG_SWUART_RX_BIT)) != 0; | ||
} | ||
|
||
static void tx_pin(uint8_t hi) | ||
{ | ||
if (hi) | ||
CONFIG_SWUART_TX_PORT |= _BV(CONFIG_SWUART_TX_BIT); | ||
else | ||
CONFIG_SWUART_TX_PORT &= ~(_BV(CONFIG_SWUART_TX_BIT)); | ||
} | ||
|
||
/* uart_putc() -- blocking 8N1 transmit */ | ||
void uart_putc(uint8_t ch) | ||
{ | ||
uint8_t bit; | ||
|
||
tx_pin(STARTBIT); | ||
bit_delay(); | ||
for (bit = 0; bit < 8; bit++) { | ||
tx_pin(ch & 0x1); | ||
ch = ch >> 1; | ||
bit_delay(); | ||
} | ||
tx_pin(STOPBIT); | ||
bit_delay(); | ||
} | ||
|
||
static int __getc(void) | ||
{ | ||
uint8_t bit; | ||
uint8_t ch; | ||
|
||
half_bit_delay(); | ||
if (rx_pin() != STARTBIT) | ||
return -1; /* Glitch */ | ||
bit_delay(); | ||
for (bit = 0; bit < 8; bit++) { | ||
ch = ch >> 1; | ||
if (rx_pin()) | ||
ch |= 1 << 7; | ||
bit_delay(); | ||
} | ||
|
||
if (rx_pin() != STOPBIT) | ||
return -2; /* Frame error */ | ||
|
||
return (int) ch; | ||
} | ||
|
||
/* uart_getc() -- blocking 8N1 receive */ | ||
uint8_t uart_getc(void) | ||
{ | ||
while (1) { | ||
int ch; | ||
while (rx_pin() != STARTBIT) { /* NOP */ } | ||
|
||
ch = __getc(); | ||
if (ch >= 0) | ||
return (uint8_t) ch; | ||
} | ||
} | ||
|
||
void uart_init() | ||
{ | ||
CONFIG_SWUART_RX_DIR &= ~(_BV(CONFIG_SWUART_RX_BIT)); | ||
CONFIG_SWUART_RX_PORT |= _BV(CONFIG_SWUART_RX_BIT); | ||
CONFIG_SWUART_TX_DIR |= _BV(CONFIG_SWUART_TX_BIT); | ||
tx_pin(STOPBIT); | ||
} | ||
|
Oops, something went wrong.