⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/** *  uart.c - A serial driver sample for device driver framwork. * *  Copyright (C) 2008  ZhangHu *  All rights reserved. *  E-MAIL: anmnmnly@gmail.com * *  This program 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. * *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. */#include "include/types.h"#include "include/mem.h"#include "include/sem.h"#include "include/irq.h"#include "drivers/chdev.h"#include "hw_uart.h"uart_t *puart0;static word_t uart0_init(struct uart_mode Mode) {    uword_t err;    buf_t *pbufr;    sem_t *pmutex;    if((puart0 = (uart_t*)kmalloc(sizeof(uart_t))) == NULL) {        return FALSE;    }    if((pbufr = bufInit(32)) == NULL) {        kfree(puart0);        return FALSE;    }    if((pmutex = semInitMutex(UART0_SEM_NO, &err)) == NULL) {        kfree(puart0);        bufDel(pbufr);        return FALSE;    }    puart0->psem = pmutex;    puart0->prevbuf = pbufr;    puart0->psndbuf = NULL;    init_uart_args(Mode);    return TRUE;}static word_t uart0_open(void) {    uword_t err;	semGain(0, puart0->psem, &err);	if(err != SEM_OK) {		return FALSE;	}    return TRUE;}static word_t uart0_close(void) {   	semPost(0, puart0->psem);    return TRUE;}static word_t uart0_write(const char_t *buf, uword_t len, loff_t loff) {	word_t i;	for(i=0;i<len;i++) {		if(uart_send_byte(buf[i]) != TRUE) {			break;		}	}	return i;}static word_t uart0_read(char_t *buf, uword_t len, loff_t loff) {    return uart_rev_str(buf);}static word_t uart0_ioctl(uword_t cmd, void *arg) {    struct uart_mode *p;    struct uart_mode mode;    if(cmd == UART_CMD_SETMODE) {        if(arg == NULL) {            return FALSE;        }        p = (struct uart_mode*)arg;        mode.Baud = p->Baud;        mode.DataLen = p->DataLen;        mode.Parity = p->Parity;        mode.StopLen = p->StopLen;        init_uart_args(mode);    } else if(cmd == UART_CMD_RESET)    {        bufClear(puart0->prevbuf);    }    return TRUE;}static void uart0_release(void) {    semDel(0, puart0->psem);    if(puart0->prevbuf != NULL) {        bufDel(puart0->prevbuf);    }    if(puart0->psndbuf != NULL) {		bufDel(puart0->psndbuf);	}	kfree(puart0);    puart0 = NULL;}struct chdev_operations uart0_op = {    uart0_open,    uart0_close,    NULL,    uart0_write,    uart0_read,    uart0_ioctl,    uart0_release,};void uart_register(void) {	struct uart_mode mode;    mode.DataLen = 8;    mode.StopLen = 1;    mode.Parity = UART_PARITY_NONE;    mode.Baud = 115200;	uart0_init(mode);    chdevRegister(UART0_MID, UART0_SID, "uart0", &uart0_op);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -