drv_sio.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 209 行
C
209 行
/**********************************************************************************/
/* */
/* Copyright (C) 2006 Oki Electric Industry Co., LTD. */
/* */
/* System Name : uPLAT7D series */
/* Module Name : uPLAT7D system SIO drives program */
/* File Name : drv_sio.c */
/* Revision : 01.00 */
/* Date : 2006/1/16 initial version */
/* */
/**********************************************************************************/
#include "common.h"
#include "drv_common.h"
#include "drv_sio.h"
#include "hal_interrupt.h"
#include "hal_api.h"
#include "intrinsics.h"
#define DRV_SIO_PORT_SEL_MASK 0x3fff3fff
#define DRV_SIO_PORT_SEL_SET 0x80008000
/***********************************/
/* Static functions prototype */
/***********************************/
void DrvSIO_sio_handler(uint16_t state);
static volatile int8_t transmit_txd_flag;
static volatile int8_t transmit_rxd_flag;
/*****************************************************************************/
/* */
/* Function Name : int16_t smpDrv_OpenSIO(struct uPLAT_SioParam *init_sio)*/
/* Input : Pointer to transmission information */
/* : baudrate: 115200bps, 34400bps */
/* : data length: !=0 */
/* : stop bit: 1 stop bit(0) */
/* : parity: non parity(0) */
/* Output : DRV_OK(1) */
/* : DRV_PARAM_ERROR(-2) */
/* */
/* Note : Initialize SIO. */
/* */
/*****************************************************************************/
int16_t smpDrv_OpenSIO(struct uPLAT_SioParam *init_sio)
{
int16_t rtnVal = DRV_OK;
uPLAT_InterruptParam param;
uPLAT_PioFunctionParam port_param;
/* check parameter */
switch(init_sio->baudrate){
case BPS_115200:
break;
case BPS_38400:
break;
default:
rtnVal = DRV_PARAM_ERROR;
break;
}
if(init_sio->data_len != LENGTH_8BIT){
rtnVal = DRV_PARAM_ERROR;
}
if(init_sio->stop_bit != STOP_1BIT){
rtnVal = DRV_PARAM_ERROR;
}
if(init_sio->parity != NON_PARITY){
rtnVal = DRV_PARAM_ERROR;
}
/* Get port function. */
if(rtnVal == OK){
rtnVal = uplat7dHAL_PioGetFunction(&port_param);
if (rtnVal != OK) {
rtnVal = DRV_PARAM_ERROR;
}
}
if(rtnVal == OK){
/* Set SIO port function. */
port_param.port_sel4 = (port_param.port_sel4&DRV_SIO_PORT_SEL_MASK)|DRV_SIO_PORT_SEL_SET;
rtnVal = uplat7dHAL_PioSetFunction(&port_param);
if (rtnVal != OK) {
rtnVal = DRV_PARAM_ERROR;
}
}
if(rtnVal == DRV_OK){
transmit_txd_flag = 0;
transmit_rxd_flag = 0;
rtnVal = uplat7dHAL_SioInit(init_sio); /* Initialize SIO */
/* Registration Callback function */
param.primary = DRV_INT_SIO; /* SIO interrupt */
param.level = ILC1_ILR10 & INT_LV7; /* level 7 */
param.mode = 0; /* */
param.callback = DrvSIO_sio_handler; /* Call back function for SIO driver */
HALInterrupt_SetPrimary(¶m);
__enable_interrupt(); //irq_en();
}
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : int16_t smpDrv_CloseSIO(void) */
/* Input : Noting */
/* Output : DRV_OK(1) */
/* */
/* Note : Close SIO. */
/* */
/*****************************************************************************/
int16_t smpDrv_CloseSIO(void)
{
int16_t rtnVal = DRV_OK;
__disable_interrupt(); //irq_dis(); /* Disable Interrupt */
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : int16_t smpDrv_WriteSIO(int8_t *buff, uint16_t size) */
/* Input : Pointer of write data */
/* : Size of write data */
/* Output : DRV_OK(1) */
/* : DRV_PARAM_ERROR(-2) */
/* */
/* Note : Write character string. */
/* */
/*****************************************************************************/
int16_t smpDrv_WriteSIO(int8_t *buff, uint16_t size)
{
int16_t rtnVal = DRV_OK;
uint32_t i;
for(i=1; (i<=size)&&(rtnVal==DRV_OK); i++){
rtnVal = uplat7dHAL_SioTxData(&buff[i-1]);
while(transmit_txd_flag == 0) /* complete transmit ? */
{
;
}
transmit_txd_flag = 0;
}
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : int16_t smpDrv_ReadSIO(int8_t *buff, uint16_t size) */
/* Input : Pointer of read data */
/* : Size of read data */
/* Output : DRV_OK(1) */
/* : DRV_PARAM_ERROR(-2) */
/* */
/* Note : read character string. */
/* */
/*****************************************************************************/
int16_t smpDrv_ReadSIO(int8_t *buff, uint16_t size)
{
int16_t rtnVal = DRV_OK;
int16_t status;
uint32_t i;
if(buff == NULL){
rtnVal = DRV_PARAM_ERROR;
}
if(rtnVal == DRV_OK){
for(i=1; (i<=size)&&(rtnVal==DRV_OK); i++){
while(transmit_rxd_flag == 0) /* complete transmit ? */
{
;
}
transmit_rxd_flag = 0;
rtnVal = uplat7dHAL_SioReadStatus(&status);
if (rtnVal == DRV_OK) {
if(status == DRV_SIO_OK){
rtnVal = uplat7dHAL_SioRxData(buff);
buff++;
}else{
rtnVal = status;
}
}
}
}
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : void DrvSIO_sio_handler(uint16_t state) */
/* Input : State of read/write */
/* Output : Notiong */
/* */
/* Note : Callback fanction for transmit complete tell. */
/* */
/*****************************************************************************/
void DrvSIO_sio_handler(uint16_t state)
{
if(state == CALLBACK_STATE_SIO_RXD){
transmit_rxd_flag = 1;
}else if(state == CALLBACK_STATE_SIO_TXD){
transmit_txd_flag = 1;
}else{
/* Non Function */
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?