hal_sio.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 220 行

C
220
字号
/**********************************************************************************/
/*                                                                                */
/*    Copyright (C) 2006 Oki Electric Industry Co., LTD.                          */
/*                                                                                */
/*    System Name    :  uPLAT7D series                                            */
/*    Module Name    :  uPLAT7D system SIO HAL program                            */
/*    File   Name    :  hal_sio.c                                                 */
/*    Revision       :  01.00                                                     */
/*    Date           :  2006/1/16 initial version                                 */
/*                                                                                */
/**********************************************************************************/
#if defined(__arm)
#include "ml675050.h"
#elif defined(_WIN32)
#include "ml675050sim.h"
#else
#error COMPILER ERROR
#endif

#include "common.h"
#include "hal_common.h"
#include "hal_sio.h"
#include "hal_interrupt.h"

/***********************************/
/*    type definition              */
/***********************************/
struct SioStatus {
    uint8_t receive_data;       /* receive data */
    uint8_t receive_flag;       /* receive flag */
    int16_t trans_status;       /* transmit status */
};
#define SIO_SIOSTA_REG_CLEAR 0x37
#define SIO_SIOTCN_REG_CLEAR 0x00

static void _sio_handler(void);

/***********************************/
/*     Static functions prototype  */
/***********************************/
struct SioStatus sio_status;

/******************************/
/*     Public defines         */
/******************************/
FP sio_handler = _sio_handler;

/*****************************************************************************/
/*                                                                           */
/*  Function Name   : int16_t HalSio_SioInit( struct uPLAT_SioParam *param ) */
/*  Input           : Transmission parameter                                 */
/*                  : baudrate: 115200bps, 38400bps                          */
/*                  :           (if baudrate == 0 then 38400bps)             */
/*                  : data length: !=0                                       */
/*                  : stop bit: 1 stop bit(0)                                */
/*                  : parity: non parity(0)                                  */
/*  Output          : HAL_OK(1)                                              */
/*                  : HAL_PARAM_ERROR(-2)                                    */
/*                                                                           */
/*  Note : Initialize SIO.                                                   */
/*                                                                           */
/*****************************************************************************/
int16_t HalSio_SioInit( struct uPLAT_SioParam *param )
{
    int16_t rtnVal = HAL_OK;
    uint32_t value_of_SIOBT;

    if((param->baudrate) == 0){
        rtnVal = HAL_PARAM_ERROR;
    }

    if(rtnVal == HAL_OK){
        /* initialize a buffer */
        sio_status.receive_data = 0;
        sio_status.receive_flag = 0;
        sio_status.trans_status = 0;
        
        OkiCLib_clr32bit(SIOBCN, SIOBCN_BGRUN);   /* stop baud rate timer */
        OkiCLib_set32bit(CLKCNT, CLKCNT_SIOCLKSEL);       /* set SIOCLK amano test 03/14 */
        
        OkiCLib_write32(SIOSTA, SIO_SIOSTA_REG_CLEAR);   /* clear SIO Status */
        OkiCLib_write32(SIOTCN, SIO_SIOTCN_REG_CLEAR);   /* clear SIO Test control */
        
        /* set baudrate */
        value_of_SIOBT = ((256 * (16 * param->baudrate) - CLKSIO) / (16 * param->baudrate));
        OkiCLib_write32(SIOBT, value_of_SIOBT);
        /* set data length */
        OkiCLib_clr32bit(SIOCON, SIOCON_LN);    /* set 8bit */
        
        /* set stop bit */
        OkiCLib_set32bit(SIOCON, SIOCON_TSTB);  /* set 1 stop bit */
        
        /* set parity bit */
        OkiCLib_clr32bit(SIOCON, SIOCON_PEN);   /* Non parity */
        
        OkiCLib_set32bit(SIOBCN, SIOBCN_BGRUN); /* start baud rate timer */
    }

    return rtnVal;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name   : int16_t HalSio_SioRxData( int8_t *buff )               */
/*  Input           : Pointer to transmission data                           */
/*  Output          : HAL_OK(1)                                              */
/*                  : HAL_PARAM_ERROR(-2)                                    */
/*                                                                           */
/*  Note : It receives the data.                                             */
/*                                                                           */
/*****************************************************************************/
int16_t HalSio_SioRxData( int8_t *buff )
{
    int16_t rtnVal = HAL_OK;

    if (buff == NULL) {
        rtnVal = HAL_PARAM_ERROR;
    }

    if(rtnVal == HAL_OK){
        *buff = (int8_t)sio_status.receive_data;    /* input one character */
        sio_status.receive_flag = 0;
    }

    return rtnVal;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name   : int16_t HalSio_SioTxData( int8_t *buff )               */
/*  Input           : Pointer to transmission data                           */
/*  Output          : HAL_OK(1)                                              */
/*                  : HAL_PARAM_ERROR(-2)                                    */
/*                                                                           */
/*  Note : Data is transmitted.                                              */
/*                                                                           */
/*****************************************************************************/
int16_t HalSio_SioTxData( int8_t *buff )
{
    int16_t rtnVal = HAL_OK;
    uint8_t *pt = (uint8_t *)buff;

    OkiCLib_write32(SIOBUF, (uint32_t)(*pt));    /* output one character */

    return rtnVal;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name   : int16_t HalSio_SioReadStatus( int16_t *status )        */
/*  Input           : Pointer to transmission status                         */
/*  Output          : HAL_OK(1)                                              */
/*                  : HAL_PARAM_ERROR(-2)                                    */
/*                                                                           */
/*  Note : Read transmission status.                                         */
/*                                                                           */
/*****************************************************************************/
int16_t HalSio_SioReadStatus( int16_t *status )
{
    int16_t rtnVal = HAL_OK;

    if (status == NULL) {
        rtnVal = HAL_PARAM_ERROR;
    }
    
    if(rtnVal == HAL_OK){
        *status = sio_status.trans_status;    /* State of RxD result  */
    }

    return rtnVal;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name   : void sio_handler(void)                                 */
/*  Input           : Nothing                                                */
/*  Output          : Nothing                                                */
/*                                                                           */
/*  Note : SIO handler.                                                      */
/*                                                                           */
/*****************************************************************************/
static void _sio_handler(void)
{
    uint32_t sta = OkiCLib_read32(SIOSTA);
    uint32_t receive_buf;
    
    if(sta & SIOSTA_RVIRQ){     /*  received interrupt ? */
        /* receive */
        OkiCLib_set32bit(SIOSTA, SIOSTA_RVIRQ);    /* clear RVIRQ flag */
        receive_buf = OkiCLib_read32(SIOBUF);
        if(sio_status.receive_flag == 0){
            /* error happened */
            if((sta & 0x07) & SIOSTA_OERR){
                sio_status.trans_status = HAL_SIO_OVR_ERROR;       /* overrun error */
            }else if((sta & 0x07) & SIOSTA_PERR){
                sio_status.trans_status = HAL_SIO_PRT_ERROR;       /* parity error */
            }else if((sta & 0x07) & SIOSTA_FERR){
                sio_status.trans_status = HAL_SIO_FRM_ERROR;       /* framing error */
            }else{
                /* error did not happen */
                sio_status.trans_status = HAL_SIO_OK;              /* No error */
                sio_status.receive_flag = 1;
            }
            OkiCLib_write32(SIOSTA, (sta & 0x07));    /* clear error status */
        }else{
            sio_status.trans_status = HAL_SIO_OVF_ERROR;           /* overflow error ? */
        }
        sio_status.receive_data = (uint8_t)receive_buf;
        CALL_BACK_TABLE[INT_SIO](CALLBACK_STATE_SIO_RXD);          /*  Notice receive completion interruption */
    }
    else if(sta & SIOSTA_TRIRQ){
        /* transmit */
        OkiCLib_set32bit(SIOSTA, SIOSTA_TRIRQ);    /* clear TRIRQ flag */
        CALL_BACK_TABLE[INT_SIO](CALLBACK_STATE_SIO_TXD);          /*  Notice transmit completion interruption */
    }else{
        sio_status.trans_status = HAL_SIO_OTR_ERROR;               /* others error */
    }
}

⌨️ 快捷键说明

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