Commits

Anonymous committed a4eda68

Removed old UART code.

  • Participants
  • Parent commits 66ade41

Comments (0)

Files changed (4)

File uart.c

-/* UART Example for Teensy USB Development Board
- * http://www.pjrc.com/teensy/
- * Copyright (c) 2009 PJRC.COM, LLC
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-// Version 1.0: Initial Release
-// Version 1.1: Add support for Teensy 2.0, minor optimizations
-
-#define F_CPU 1000000UL  /* 1 MHz CPU clock */
-
-
-#include <avr/io.h>
-#include <avr/interrupt.h>
-
-#include "uart.h"
-
-// These buffers may be any size from 2 to 256 bytes.
-#define RX_BUFFER_SIZE 64
-#define TX_BUFFER_SIZE 40
-
-static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
-static volatile uint8_t tx_buffer_head;
-static volatile uint8_t tx_buffer_tail;
-static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
-static volatile uint8_t rx_buffer_head;
-static volatile uint8_t rx_buffer_tail;
-
-// Initialize the UART
-void uart_init(uint32_t baud)
-{
-	cli();
-	UBRR1 = (F_CPU / 4 / baud - 1) / 2;
-	UCSR1A = (1<<U2X1);
-	UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);
-	UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
-	tx_buffer_head = tx_buffer_tail = 0;
-	rx_buffer_head = rx_buffer_tail = 0;
-	sei();
-}
-
-// Transmit a byte
-void uart_putchar(uint8_t c)
-{
-	uint8_t i;
-
-	i = tx_buffer_head + 1;
-	if (i >= TX_BUFFER_SIZE) i = 0;
-	while (tx_buffer_tail == i) ; // wait until space in buffer
-	//cli();
-	tx_buffer[i] = c;
-	tx_buffer_head = i;
-	UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1) | (1<<UDRIE1);
-	//sei();
-}
-
-// Receive a byte
-uint8_t uart_getchar(void)
-{
-        uint8_t c, i;
-
-	while (rx_buffer_head == rx_buffer_tail) ; // wait for character
-        i = rx_buffer_tail + 1;
-        if (i >= RX_BUFFER_SIZE) i = 0;
-        c = rx_buffer[i];
-        rx_buffer_tail = i;
-        return c;
-}
-
-// Return the number of bytes waiting in the receive buffer.
-// Call this before uart_getchar() to check if it will need
-// to wait for a byte to arrive.
-uint8_t uart_available(void)
-{
-	uint8_t head, tail;
-
-	head = rx_buffer_head;
-	tail = rx_buffer_tail;
-	if (head >= tail) return head - tail;
-	return RX_BUFFER_SIZE + head - tail;
-}
-
-// Transmit Interrupt
-ISR(USART1_UDRE_vect)
-{
-	uint8_t i;
-
-	if (tx_buffer_head == tx_buffer_tail) {
-		// buffer is empty, disable transmit interrupt
-		UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);
-	} else {
-		i = tx_buffer_tail + 1;
-		if (i >= TX_BUFFER_SIZE) i = 0;
-		UDR1 = tx_buffer[i];
-		tx_buffer_tail = i;
-	}
-}
-
-// Receive Interrupt
-ISR(USART1_RX_vect)
-{
-	uint8_t c, i;
-
-	c = UDR1;
-	i = rx_buffer_head + 1;
-	if (i >= RX_BUFFER_SIZE) i = 0;
-	if (i != rx_buffer_tail) {
-		rx_buffer[i] = c;
-		rx_buffer_head = i;
-	}
-}

File uart.h

-#ifndef _uart_included_h_
-#define _uart_included_h_
-
-#include <stdint.h>
-
-void uart_init(uint32_t baud);
-void uart_putchar(uint8_t c);
-uint8_t uart_getchar(void);
-uint8_t uart_available(void);
-
-#endif

File usart.c

-/**  Project:    Wacky Racers
-  *  Subproject: Image sensor and WiFi board firmware
-  *  Authors:    Lazar Sumar, 
-  *              Regan Gunther,
-  *              Tracy Jackson
-  *
-  *  Company:    University of Canterbury
-  *  Department: Electrical and Computer Engineering
-  *  Course:     Computer Hardware II (ENEL429)
-  *
-  *  Group:         3
-  *  Group members: Lazar Sumar, Simon Gow, Tracy Jackson,
-  *                 Sean Thomas, Regan Gunther, Jared Hall
-  *
-  *  File:             usart.c
-  *  File description: Code for control of the two microcontroller USART modules.
-  *                    Detailed description can be found in the "usart.h" file.
-  */
-#include "usart.h"
-
-void usart0_init(uint16_t baud) {
-	/* Set Asynchronous mode of operation */
-	UCSR0B |= GET_MASK(RXEN0) | // Enable receive
-	          GET_MASK(TXEN0);  // Enable transmit
-			  
-	UCSR0C |= GET_MASK(UCSZ01) | // set 8 bit mode with no parity
-	          GET_MASK(UCSZ00);  // and 1 stop bit
-			  
-	UBRR0 = baud & 0x3F; // 12 bit register
-}
-
-uint8_t usart0_receive(void) {
-	/* Wait for data to be received */
-	while ( !(UCSR0A & GET_MASK(RXC0)) );
-	/* Get and return received data from buffer */
-	return UDR0;
-}
-
-void usart0_transmit(uint8_t data) {
-	/* Wait for empty transmit buffer */
-	while ( !(UCSR0A & GET_MASK(UDRE0)) );
-	/* Put data into buffer, sends the data */
-	UDR0 = data;
-}
-
-void usart0_send_str(char* string) {
-	uint8_t i = 0;
-	
-	for (i = 0; i <255 && string[i] != 0; i++) {
-		usart0_transmit((uint8_t)string[i]);
-	}
-}
-
-
-void usart1_init(uint16_t baud) {
-	UCSR1B |= GET_MASK(RXEN1) | // Enable receive
-	          GET_MASK(TXEN1);  // Enable transmit
-			  
-	UCSR1C |= GET_MASK(UCSZ11) | // set 8 bit mode with no parity
-	          GET_MASK(UCSZ10);  // and 1 stop bit
-			  
-	UBRR1 = baud & 0x3F; // 12 bit register
-}
-
-uint8_t usart1_receive(void) {
-	/* Wait for data to be received */
-	while ( !(UCSR1A & GET_MASK(RXC1)) );
-	/* Get and return received data from buffer */
-	return UDR1;
-}
-
-void usart1_transmit(uint8_t data) {
-	/* Wait for empty transmit buffer */
-	while ( !(UCSR1A & GET_MASK(UDRE1)) );
-	/* Put data into buffer, sends the data */
-	UDR1 = data;
-}
-
-void usart1_send_str(char* string) {
-	uint8_t i = 0;
-	
-	for (i = 0; i <255 && string[i] != 0; i++) {
-		usart1_transmit((uint8_t)string[i]);
-	}
-}

File usart.h

-/**  Project:    Wacky Racers
-  *  Subproject: Image sensor and WiFi board firmware
-  *  Authors:    Lazar Sumar, 
-  *              Regan Gunther,
-  *              Tracy Jackson
-  *
-  *  Company:    University of Canterbury
-  *  Department: Electrical and Computer Engineering
-  *  Course:     Computer Hardware II (ENEL429)
-  *
-  *  Group:         3
-  *  Group members: Lazar Sumar, Simon Gow, Tracy Jackson,
-  *                 Sean Thomas, Regan Gunther, Jared Hall
-  *
-  *  File:             usart.h
-  *  File description: Code for control of the two microcontroller USART modules.
-  *                    The uarts will be interrupt driven and will read data 
-  *                    into specified buffers. They will also allow for callback 
-  *                    functions to be set for events such as "buffer full" and 
-  *                    "received data".
-  */
-#ifndef USART_H
-#define USART_H
-
-// AVR INCLUDES
-
-// LOCAL INCLUDES
-#include "util.h"
-
-// DEFINITIONS
-
-
-// MACROS
-
-
-// USART0
-/* --------------------------------------------------------------------- *
- * function   : usart0_init
- * description:
- *    Initialises the USART0. Initialisation requires but is not limited
- *    to:
- *        - Set mode of operation (Sync or Async)
- *        - Set baud rate (eg. 96000 b/s)
- *        - Set frame format (number of data bits, parity bits, stop bits)
- */
-void usart0_init(uint16_t baud);
-/* --------------------------------------------------------------------- *
- * function   : usart0_receive
- * description:
- *    Blocking function. Returns once data is received
- */
-uint8_t usart0_receive(void);
-/* --------------------------------------------------------------------- *
- * function   : usart0_transmit:
- * description:
- *    Blocking function. Transmits once the transmit register is ready.
- */
-void usart0_transmit(uint8_t data);
-/* --------------------------------------------------------------------- *
- * function   : usart0_send_str:
- * description:
- *    Blocking function. Transmits a string over USART0.
- */
-void usart0_send_str(char* string);
-/* --------------------------------------------------------------------- */
-
-// USART1
-/* --------------------------------------------------------------------- *
- * function   : usart1_init
- * description:
- *    Initialises the USART1
- */
-void usart1_init(uint16_t baud);
-/* --------------------------------------------------------------------- *
- * function   : usart1_receive
- * description:
- *    Blocking function. Returns once data is received
- */
-uint8_t usart1_receive(void);
-/* --------------------------------------------------------------------- *
- * function   : usart1_transmit:
- * description:
- *    Blocking function. Transmits once the transmit register is ready.
- */
-void usart1_transmit(uint8_t data);
-/* --------------------------------------------------------------------- *
- * function   : usart1_send_str:
- * description:
- *    Blocking function. Transmits a string over the USART 1.
- */
-void usart1_send_str(char* string);
-/* --------------------------------------------------------------------- */
-
-#endif
-