/*
* Uart driver source -- simplified version of uart1 driver.
*
* $Id: uart.h, 2013/09/09 Shuai,bai designed by.
*
* Notice:if any body want to use this,please contanct me:QQ,287996432.
*/
#include "configure.h"
#define TX_READY (1<<2) //send data ok
#define RX_READY (1) //recieve data ok
static char *driver_name = "/dev/uart1";
static char device_count = 0;
/** declare functions ***/
rtems_device_driver uart1_initialize(major_t,minor_t,void*);
rtems_device_driver uart1_open(major_t,minor_t,void*);
rtems_device_driver uart1_close(major_t,minor_t,void*);
rtems_device_driver uart1_read(major_t,minor_t,void*);
rtems_device_driver uart1_write(major_t,minor_t,void*);
rtems_device_driver uart1_ioctl(major_t,minor_t,void*);
/**
* entry table
**/
rtems_driver_address_table uart1_table =
{
uart1_initialize,
uart1_open,
uart1_close,
uart1_read,
uart1_write,
uart1_ioctl
};
/**
* function: void uart_init()
* decription: init uart
**/
void uart_init()
{
GPHCON |= 0xa00; //gph4,gph5-->01,rx1,tx1
GPHUP = 0x0; //gph4,gph5-->up
ULCON1 = 0x03; //8n1
UCON1 = 0x05; //pclk,lun xun,interrupt
UFCON1 = 0x0; // no FIFO
UMCON1 = 0x00; //no AFC
UBRDIV1 = 26; //115200hz
}
/**
* function : void sendc(char)
* description: tx a char
**/
void sendc(uchar c)
{
while(!(UTRSTAT1 & TX_READY));
UTXH1 = c;
}
/**
* funciton: uchar getc(void)
* description: rx a char
**/
uchar getc(void)
{
while(!(UTRSTAT1 & RX_READY));
return URXH1;
}
/**
* function: void sends(char *);
* description: send a string
**/
void sends(uchar *s)
{
while(*s)
{
sendc(*s++);
}
}
/**
* function: char* gets(uchar *s)
* description: get a string
**/
uchar* gets(uchar *s)
{
int len = 0;
uchar c;
for(;; len++)
{
c = getc();
s[len] = c;
if(0 ==c)
{
s[len] = '\0';
break;
}
}
return s;
}
/**
* function:uart1_initialize()
* description:init the uart1
*/
rtems_device_driver uart1_initialize(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *arg
)
{
rtems_status_code status;
//init uart
uart_init();
//registe device name
status = rtems_io_register_name(driver_name,major,minor);
if(RTEMS_SUCCESSFUL != status)
{
printk("[Driver],regist name error!,Too many device!\n");
return status;
}
printk("[Driver]: initialize uart1 ok!\n");
return RTEMS_SUCCESSFUL;
}
/**
* function:uart1_open()
* description:open the uart1
*/
rtems_device_driver uart1_open(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *argument
)
{
rtems_status_code status = RTEMS_SUCCESSFUL;
if(device_count > 0)
{
return RTEMS_RESOURCE_IN_USE;
}
device_count++;
printk("[Driver]: open uart1 ok!\n");
return status;
}
/**
* function: uart1_close()
* description:close the uart1
*/
rtems_device_driver uart1_close(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *argument
)
{
rtems_status_code status = RTEMS_SUCCESSFUL;
if(RTEMS_SUCCESSFUL != rtems_io_unregister_driver(major))
{
printk("[Driver]:unregister uart1 failed!\n");
}
device_count--;
printk("[Driver]: close uart1 ok!\n");
return status;
}
/**
* function: uart1_write()
* description: write the uart1
*/
rtems_device_driver uart1_write(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *u_buff
)
{
sends((uchar *)u_buff);
printk("[Driver]: write uart1 ok!user buff is:%s!\n",(char *)u_buff);
return RTEMS_SUCCESSFUL;
}
/**
* function:uart1_read()
* description:read the uart1
*/
rtems_device_driver uart1_read(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *u_buff
)
{
printk("[Driver]: read uart1 ok!\n");
uchar *s = NULL;
s = gets(s);
strcpy((char*)u_buff,(char*)s);
return RTEMS_SUCCESSFUL;
}
/**
* function:uart1_ioctl()
* description:ioctl the uart1
*/
rtems_device_driver uart1_ioctl(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *cmd
)
{
rtems_status_code status = RTEMS_SUCCESSFUL;
int *d_cmd = cmd;
printk("[Driver]: ioctl uart1 ok!current cmd is %d!\n",*d_cmd);
return status;
}
/*
* end of the uart1
*/