Skip to content
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

Timer based UART for sending and receiving bytes on the FX2 #23

Open
wants to merge 1 commit into
base: linux-descriptors
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions examples/download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash -e

DEVS=$(lsusb|grep -E '(2a19|16c0|04b4|1d50|fb9a|1443)' |sed 's/:.*//;s/Bus //;s/Device //;s/ /\//')

if [ -z "$1" ]; then
echo "$0: usage: $0 <file>"
exit 1;
fi

for dev in $DEVS;do
echo "Downloading $1 to $dev"
/sbin/fxload -D /dev/bus/usb/$dev -t fx2lp -I $1
done

exit 0
8 changes: 8 additions & 0 deletions examples/uart_timer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FX2LIBDIR=../..
BASENAME = uart_timer
SOURCES=uart_timer.c
DSCR_AREA=
INT2JT=
include $(FX2LIBDIR)/lib/fx2.mk
fx2_download:
../download.sh build/$(BASENAME).ihx
61 changes: 61 additions & 0 deletions examples/uart_timer/uart_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <fx2regs.h>
#include <stdio.h>
#include <fx2ints.h>
#include <uart/api.h>
#include <uart/timer_uart.h>
#include <fx2macros.h>

//Used for setting the baud rate.
//Currently unimplemented
enum uart_baud baud;
//Extern declaration.
extern void process_isr();
void main(void)
{
baud = BAUD_9600;
uartX_init(baud);
printf("Hello");
while (TRUE)
{
uart_rx_service();
uart_tx_service();

}
}

void putchar(char c)
{
uartX_tx(c);
}


/**
* \brief This function is actually an ISR
* It is called periodically to check if data is ready to be transmitted
* The receive logic looks at the rx pin, sampling it continously. The moment
* a start bit is detected, it begins shifting the data in
* and finally sets a flag stating the receive is complete. This flag is reset
* in the uart_rx_service. This helps achieve non blocking behaviour.
**/
void timer1_isr ()
__interrupt TF1_ISR
{
process_isr();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process_isr? That isn't a good name for something...

}
79 changes: 79 additions & 0 deletions include/uart/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/** \file include/uart/api.h
* This file is for defining a common API for accessing UARTs.
**/

#ifndef UART_API_H
#define UART_API_H

#include "fx2types.h"
#include "stdarg.h"

/**
* enum Standard available baud rates
*
**/
enum uart_baud { BAUD_2400, BAUD_4800, BAUD_9600, BAUD_19200, BAUD_38400, BAUD_57600, BAUD_115200, BAUD_ANY, BAUD_FASTEST };

/**
* \brief initalizes UART.
* Returns 0 if initialization is successful.
* \param rate See uartX_set_baud()
**/
BOOL uartX_init(enum uart_baud rate, ...);

/**
* \brief Sets the UART baud rate to one of the allowed parameters.
* Possible Baud rates:
* \li 2400
* \li 4800
* \li 9600
* \li 19200
* \li 28800
* \li 38400
* \li 57600
* \li 115200
* Returns 0 if successful.
**/
BOOL uartX_set_baud(enum uart_baud rate);

/**
* \brief Returns the baud rate currently being used.
**/
enum uart_baud uartX_get_baud();

/**
* \brief transmits data through UART
* \param c The character to be sent out
**/

void uartX_tx(char c);

/**
* \brief Returns if the transmit is blocking or not
* TRUE - Blocking
* FALSE -Non Blocking
**/

BOOL uartX_tx_willblock();

/**
* \brief receives data through UART.
* Returns one byte at a time from the queue
*
**/
char uartX_rx();

/**
* \brief Returns if the receive is blocking or not
* 0 - Non Blocking
* 1 - Blocking
**/
BOOL uartX_check_rx_blocking();

/**
* \brief Returns count number of bytes present in the buffer
*
**/
BYTE uartX_check_receive_buffer();

#endif
43 changes: 43 additions & 0 deletions include/uart/timer_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef TIMER_UART_H
#define TIMER_UART_H
#include "fx2regs.h"
#include "fx2types.h"

/**
* Enum for controlling transmitter state
* Only 2 states in the enum, it is either
* in IDLE or BUSY
**/
enum uart_tx_state{IDLE, BUSY};

/**
* Enum for controlling receiver state
* Only 4 states in the enum
* 0x00 - IDLE
* 0x01 - Data Reception complete
* 0x02 - Start bit detect
* 0x03 - Data currently being read
**/
enum uart_rx_state{IDLE_RX,DATA_COMPLETE,START_DETECT,BUSY_RX};

void timer_init();
void softuart_init( void );
void uart_tx_service();
void uart_rx_service();
void QueueInitTX(void);
__bit QueuePutTX(unsigned char data);
__bit QueueGetTX(unsigned char *old);
__bit QueueCheckTX();
void QueueInitRX(void);
__bit QueuePutRX(unsigned char data);
__bit QueueGetRX(unsigned char * old);
__bit QueueCheckRX();
void process_isr();
extern unsigned char volatile tx_buffer;
extern unsigned char volatile rx_buffer;
extern unsigned char volatile tx_count;
extern unsigned char volatile rx_count;
extern unsigned char volatile tx_bits_sent;
extern unsigned char volatile rx_bits_rcvd;
extern unsigned char volatile rx_busy;
#endif
2 changes: 1 addition & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

AS8051?=sdas8051
SOURCES = serial.c i2c.c delay.c setupdat.c gpif.c eputils.c $(wildcard interrupts/*.c)
SOURCES = serial.c i2c.c delay.c setupdat.c gpif.c eputils.c $(wildcard interrupts/*.c) $(wildcard uart/*.c)
FX2_OBJS = $(patsubst %.c,%.rel, $(SOURCES)) usbav.rel
INCLUDES = -I../include
SDCC = sdcc -mmcs51 $(SDCCFLAGS)
Expand Down
Loading