drv_uart.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 237 行
C
237 行
/**************************************************************************/
/* */
/* Copyright (C) 2006 Oki Electric Industry Co., LTD. */
/* */
/* System Name : uPLAT7D series */
/* Module Name : uPLAT7D uart drivers program */
/* File Name : drv_uart.c */
/* Revision : 01.00 */
/* Date : 2005/12/15 initial version */
/* */
/**************************************************************************/
#include <string.h>
#include "common.h"
#if defined(__arm)
#include "ml675050.h"
#else
#include "ml675050sim.h"
#endif
#include "drv_common.h"
#include "hal_api.h"
#include "drv_uart.h"
#include "intrinsics.h"
#define UART_PORT_SEL_MASK 0xffff0000
#define UART_PORT_SEL_SET 0x00005555
/***********************************/
/* Static functions prototype */
/***********************************/
static volatile int8_t transmit_txd_flag;
static volatile int8_t transmit_rxd_flag;
static volatile int8_t transmit_toi_flag;
/************************************************************************/
/* */
/* Function Name : smpDrv_OpenUART */
/* Input : struct uPLAT_UartParam *init_uart */
/* Output : int16_t DRV_OK(1) */
/* DRV_PARAM_ERROR(-2) */
/* */
/* Note : open uart. */
/* */
/************************************************************************/
int16_t smpDrv_OpenUART(struct uPLAT_UartParam *init_uart) {
int16_t rtnVal = DRV_OK;
uPLAT_InterruptParam param;
uPLAT_PioFunctionParam port_param;
/* Get port function. */
rtnVal = uplat7dHAL_PioGetFunction(&port_param);
if (rtnVal != DRV_OK) {
return rtnVal;
}
/* Set UART0 port secondary function. */
port_param.port_sel3 = (port_param.port_sel3 & UART_PORT_SEL_MASK)
| UART_PORT_SEL_SET;
rtnVal = uplat7dHAL_PioSetFunction(&port_param);
if (rtnVal != DRV_OK) {
return rtnVal;
}
/* check parameter */
switch (init_uart->baudrate) {
case BPS_115200:
break;
default:
rtnVal = DRV_PARAM_ERROR;
break;
}
if (init_uart->data_len != 8) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->stop_bit != 1) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->parity != 0) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->fifo_mode != 1) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->fifo_byte_num != 4) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->dma_mode != 0) {
rtnVal = DRV_PARAM_ERROR;
}
if (init_uart->flow_ctrl != 0) {
rtnVal = DRV_PARAM_ERROR;
}
if (rtnVal == DRV_OK) {
transmit_txd_flag = 0;
transmit_rxd_flag = 0;
/* Interrupt setting. */
param.primary = INT_UART0;
param.level = EXILCC_ILC48 & INT_LV7;
param.callback = DrvUART_uart_handler;
rtnVal = uplat7dHAL_InterruptSetPrimary(¶m);
if (rtnVal == DRV_OK) {
/* Initialize UART */
rtnVal= uplat7dHAL_UartInit(init_uart);
/* Enable irq interrupt. */
__enable_interrupt(); //irq_en();
}
}
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : smpDrv_CloseUART */
/* Input : void */
/* Output : int16_t DRV_OK(1) */
/* */
/* Note : close uart. */
/* */
/************************************************************************/
int16_t smpDrv_CloseUART(void)
{
int16_t rtnVal = DRV_OK;
/* Disable irq interrupt. */
__disable_interrupt(); //irq_dis(); /* Disable Interrupt */
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : smpDrv_ReadUART */
/* Input : int8_t*buff,uint16_t size */
/* Output : int16_t DRV_OK(1) */
/* DRV_PARAM_ERROR(-2) */
/* */
/* Note : read uart. */
/* */
/************************************************************************/
int16_t smpDrv_ReadUART(int8_t *buff, uint16_t size)
{
int16_t rtnVal = DRV_OK;
int16_t status;
int8_t rx_buff[UART_FIFO_MAX + 1];
uint16_t rx_length;
uint16_t i = 0;
uint8_t j = 0;
int16_t temp = (int16_t)size;
while (transmit_rxd_flag == 0) { /* complete transmit ? */
;
}
do{
rtnVal = uplat7dHAL_UartReadStatus(&status);
if (status == DRV_OK) {
rtnVal = uplat7dHAL_UartRxData(rx_buff);
rx_length = (uint16_t)strlen(rx_buff);
if (rtnVal == DRV_OK) {
for (j = 0; j < rx_length; j++) {
buff[i] = rx_buff[j];
i++;
}
}
temp -= (int16_t)rx_length;
}
else {
rtnVal = status;
}
} while ((temp > 0) && (rtnVal == DRV_OK));
while (i < size) {
buff[i] = '\0';
i++;
}
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : smpDrv_WriteUART */
/* Input : int8_t*buff,uint16_t size */
/* Output : int16_t DRV_OK(1) */
/* DRV_PARAM_ERROR(-2) */
/* */
/* Note : write uart. */
/* */
/************************************************************************/
int16_t smpDrv_WriteUART(int8_t *buff, uint16_t size)
{
int16_t rtnVal = DRV_OK;
uint16_t i;
for (i = 0; (i < size) && (rtnVal == DRV_OK); i++) {
rtnVal = uplat7dHAL_UartTxData(&buff[i]);
}
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : DrvUART_uart_handler */
/* Input : uint16_t state */
/* Output : void */
/* */
/* */
/* Note : uart handler. */
/* */
/************************************************************************/
void DrvUART_uart_handler(uint16_t state)
{
if(state == CALLBACK_STATE_UART_RXD){
transmit_rxd_flag = 1;
}else if(state == CALLBACK_STATE_UART_TXD){
transmit_txd_flag = 1;
}else{
transmit_rxd_flag = 1;
transmit_toi_flag = 1;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?