Project

General

Profile

Revision 206

Added by Kevin Woo almost 13 years ago

Moved the uart code to rs485_int since that describes it better.

View differences:

trunk/cardbox/uart.h
1
/********
2
 * This file is part of Tooltron.
3
 *
4
 * Tooltron is free software: you can redistribute it and/or modify
5
 * it under the terms of the Lesser GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Tooltron is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * Lesser GNU General Public License for more details.
13
 * You should have received a copy of the Lesser GNU General Public License
14
 * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
17
 *
18
 ********/
19
/** @file uart.h
20
 *
21
 *	@brief Initializes UART functions using the UART hardware module
22
 *
23
 *	@author Kevin Woo (kwoo)
24
 */
25

  
26
#ifndef UART_H
27
#define UART_H
28

  
29
#include <avr/io.h>
30
#include <avr/interrupt.h>
31
#include <stdint.h>
32
/** **/
33
#define UART_TX_OFF 0
34
#define UART_TX_ON 1
35

  
36
/** @brief RX Pin for the UART **/
37
#define RX		_BV(PORTD0)
38
#define TX		_BV(PORTD1)
39
#define TX_EN	_BV(PORTD5)
40

  
41
/** @brief The most recently received byte **/
42
extern uint8_t received_byte;
43
/** @brief If the value in received_byte has been read or not **/
44
extern uint8_t byte_ready;
45

  
46
/** @brief Initializes the UART registers and sets it to the buad rate 
47
 *         which msut be the value that is defined in the datasheet 
48
 *         for any particular speed (ie: 51 -> 9600bps)
49
 **/
50
void init_uart(uint16_t baud);
51
/** @brief Gets latest byte and returns it in output_byte. If the byte 
52
 *         was already read, returns -1 otherwise it returns 0 
53
 **/
54
int8_t uart_get_byte(uint8_t *output_byte);
55
/** @brief Sends a character array of size size. If we are currently 
56
 *         transmitting it will block until the the current transmit 
57
 *         is done. 
58
 **/
59
void uart_send_byte(uint8_t data);
60
void uart_toggle_transmit(uint8_t state);
61
#endif
trunk/cardbox/uart.c
1
/********
2
 * This file is part of Tooltron.
3
 *
4
 * Tooltron is free software: you can redistribute it and/or modify
5
 * it under the terms of the Lesser GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Tooltron is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * Lesser GNU General Public License for more details.
13
 * You should have received a copy of the Lesser GNU General Public License
14
 * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
17
 *
18
 ********/
19
/** @file uart.c
20
 *	
21
 *	@brief Implements UART functionality in hardware
22
 *
23
 *	@author Kevin Woo (kwoo)
24
 */
25

  
26
#include "uart.h"
27
#include <util/delay.h>
28
#include <stdint.h>
29

  
30
uint8_t received_byte;     //Byte received
31
uint8_t byte_ready;        //New byte has been received
32

  
33
void init_uart(uint16_t baud) {
34
	// Set baud rate
35
	UBRRH = (uint8_t)(baud>>8);
36
	UBRRL = (uint8_t)baud;
37
	
38
	// Enable RX/TX and RX/TX Interrupt
39
	UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXCIE) | _BV(TXEN);
40
 
41
	// Enable the TXEN pin as output
42
	DDRD |= TX_EN;
43

  
44
    // Initialize receive variables
45
    byte_ready = 0;
46
    received_byte = 0x0;
47
}
48

  
49
int8_t uart_get_byte(uint8_t *output_byte) {
50
	if (byte_ready) {
51
		byte_ready = 0;
52
		*output_byte = received_byte;
53
		return 0;
54
	} else {
55
		return -1;
56
	}
57
}
58

  
59
void uart_send_byte(uint8_t data) {
60
	//Waits until current transmit is done
61
    while (!(UCSRA & _BV(UDRE)));
62

  
63
    // Enable writes and send
64
	uart_toggle_transmit(UART_TX_ON);
65
    UDR = data;
66
	return;
67
}
68

  
69
void uart_toggle_transmit(uint8_t state) {
70
	if (state == UART_TX_ON) {
71
		PORTD |= TX_EN;
72
	} else {
73
		PORTD &= ~TX_EN;
74
	}
75
}
76

  
77
ISR(USART_RX_vect) {
78
    received_byte = UDR;
79
    byte_ready = 1;    
80
}
81

  
82
ISR(USART_TX_vect) {
83
    // Re-enable reads
84
    uart_toggle_transmit(UART_TX_OFF);
85
}
trunk/cardbox/rs485_int.c
1
/********
2
 * This file is part of Tooltron.
3
 *
4
 * Tooltron is free software: you can redistribute it and/or modify
5
 * it under the terms of the Lesser GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Tooltron is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * Lesser GNU General Public License for more details.
13
 * You should have received a copy of the Lesser GNU General Public License
14
 * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
17
 *
18
 ********/
19
/** @file uart.c
20
 *	
21
 *	@brief Implements UART functionality in hardware
22
 *
23
 *	@author Kevin Woo (kwoo)
24
 */
25

  
26
#include "uart.h"
27
#include <util/delay.h>
28
#include <stdint.h>
29

  
30
uint8_t received_byte;     //Byte received
31
uint8_t byte_ready;        //New byte has been received
32

  
33
void init_uart(uint16_t baud) {
34
	// Set baud rate
35
	UBRRH = (uint8_t)(baud>>8);
36
	UBRRL = (uint8_t)baud;
37
	
38
	// Enable RX/TX and RX/TX Interrupt
39
	UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXCIE) | _BV(TXEN);
40
 
41
	// Enable the TXEN pin as output
42
	DDRD |= TX_EN;
43

  
44
    // Initialize receive variables
45
    byte_ready = 0;
46
    received_byte = 0x0;
47
}
48

  
49
int8_t uart_get_byte(uint8_t *output_byte) {
50
	if (byte_ready) {
51
		byte_ready = 0;
52
		*output_byte = received_byte;
53
		return 0;
54
	} else {
55
		return -1;
56
	}
57
}
58

  
59
void uart_send_byte(uint8_t data) {
60
	//Waits until current transmit is done
61
    while (!(UCSRA & _BV(UDRE)));
62

  
63
    // Enable writes and send
64
	uart_toggle_transmit(UART_TX_ON);
65
    UDR = data;
66
	return;
67
}
68

  
69
void uart_toggle_transmit(uint8_t state) {
70
	if (state == UART_TX_ON) {
71
		PORTD |= TX_EN;
72
	} else {
73
		PORTD &= ~TX_EN;
74
	}
75
}
76

  
77
ISR(USART_RX_vect) {
78
    received_byte = UDR;
79
    byte_ready = 1;    
80
}
81

  
82
ISR(USART_TX_vect) {
83
    // Re-enable reads
84
    uart_toggle_transmit(UART_TX_OFF);
85
}
trunk/cardbox/main.c
17 17
 *
18 18
 ********/
19 19
#include <util/delay.h>
20
#include "serial.h"
20
#include "uart.h"
21 21
#include <avr/interrupt.h>
22 22
#include <avr/io.h>
23 23

  
trunk/cardbox/rs485_int.h
1
/********
2
 * This file is part of Tooltron.
3
 *
4
 * Tooltron is free software: you can redistribute it and/or modify
5
 * it under the terms of the Lesser GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Tooltron is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * Lesser GNU General Public License for more details.
13
 * You should have received a copy of the Lesser GNU General Public License
14
 * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
17
 *
18
 ********/
19
/** @file uart.h
20
 *
21
 *	@brief Initializes UART functions using the UART hardware module
22
 *
23
 *	@author Kevin Woo (kwoo)
24
 */
25

  
26
#ifndef UART_H
27
#define UART_H
28

  
29
#include <avr/io.h>
30
#include <avr/interrupt.h>
31
#include <stdint.h>
32
/** **/
33
#define UART_TX_OFF 0
34
#define UART_TX_ON 1
35

  
36
/** @brief RX Pin for the UART **/
37
#define RX		_BV(PORTD0)
38
#define TX		_BV(PORTD1)
39
#define TX_EN	_BV(PORTD5)
40

  
41
/**
42
 * @brief This si the value to pass into UBRR tos et the baudrate.
43
 * @note this assumes an 8MHz system clock
44
 */
45
#define BAUD9600    51
46

  
47
/** @brief The most recently received byte **/
48
extern uint8_t received_byte;
49
/** @brief If the value in received_byte has been read or not **/
50
extern uint8_t byte_ready;
51

  
52
/** @brief Initializes the UART registers and sets it to the buad rate 
53
 *         which msut be the value that is defined in the datasheet 
54
 *         for any particular speed (ie: 51 -> 9600bps)
55
 **/
56
void init_uart(uint16_t baud);
57
/** @brief Gets latest byte and returns it in output_byte. If the byte 
58
 *         was already read, returns -1 otherwise it returns 0 
59
 **/
60
int8_t uart_get_byte(uint8_t *output_byte);
61
/** @brief Sends a character array of size size. If we are currently 
62
 *         transmitting it will block until the the current transmit 
63
 *         is done. 
64
 **/
65
void uart_send_byte(uint8_t data);
66
void uart_toggle_transmit(uint8_t state);
67
#endif

Also available in: Unified diff