历史上的今天
今天是:2025年07月30日(星期三)
2021年07月30日 | MC9S12XEP100 SPI模块 驱动程序
2021-07-30 来源:eefocus
之前发了MC9S12XEP100数据手册上的SPI模块的翻译http://blog.csdn.net/lin_strong/article/details/79122482。
于是乎,现在对其的封装也来了。目前先完成了硬件驱动部分的封装。软件部分还没完全想好怎么弄。
这次的封装我尝试使用两个头文件的方式来区分protect与public。
全部内部使用的函数(不带参数检查)都放在了SPI_Internal.h中,而对用户的接口(可选参数检查)都放在了SPI.h中
使用时请只#include “SPI.h” 然后使用里头提供的接口。
下面上代码:
/*
*******************************************************************************************
*
*
* SPI(Serial Peripheral Interface) SUPPORT PACKAGE
* SPI支持包
*
* File : SPI.h
* By : Lin Shijun(http://blog.csdn.net/lin_strong)
* Date: 2019/03/05
* version: V1.1
* History: 2018/02/01 V1.0 the prototype
* 2019/03/05 V1.1 some modification to the malloc configuration.
* uncomment struct style initializer and change name to SPI_InitExt
* to include both initializers.
* NOTE(s):a.refer to the code of uCOS-II
* b.this file define the interface for user. user should include this file to use the
* spi module.
* c.this module has three files — SPI.h、SPI_Internal.h、SPI.c
* d.steps to use this module:
* 1. define malloc to configure the parameters in CONFIGURE and INITIALIZATION
* CONFIGURE, or use the default.
* 2. use the interface function to send/receive data. e.g.
* ......
* INT8U dataSend,dataRecv;
* SPI_Init(SPI0);
* SPI_Enable(SPI0);
* while(TRUE){
* dataRecv = SPI_ExchangeChar(SPI0,dataSend);
* // deal with the dataRecv.
* }
*********************************************************************************************
*/
#ifndef SPI_H
#define SPI_H
/*
*******************************************************************************************
* INCLUDES
*******************************************************************************************
*/
#include #include "common.h" /* ****************************************************************************************** * CONSTANT ****************************************************************************************** */ #define SPI0 0x00 /* SPI0 */ #define SPI1 0x01 /* SPI1 */ #define SPI2 0x02 /* SPI2 */ // spi behavior when cpu is in wait mode #define SPI_INWAITMODE_STOP 0 // SPI module will keep running when cpu in wait mode #define SPI_INWAITMODE_RUN 1 // SPI module will stop when cpu in wait mode // format of spi clock #define SPI_SCKFMT_MODE_0 0 // CPHA=0、CPOL=0 #define SPI_SCKFMT_MODE_1 1 // CPHA=0、CPOL=1 #define SPI_SCKFMT_MODE_2 2 // CPHA=1、CPOL=0 #define SPI_SCKFMT_MODE_3 3 // CPHA=1、CPOL=1 // the bit order when transmit/receive. #define SPI_BITORDER_LSB 0 // Least significant bit first #define SPI_BITORDER_MSB 1 // Most significant bit first // how spi system control SS pin #define SPI_SS_CTRL_OFF 0 // SPI module don't use SS pin. #define SPI_SS_CTRL_ON 1 // only for master, SS pin is used for slave control. #define SPI_SS_LISTEN_MODF 2 // only for master, SS pin is used for listening mode fault /* ******************************************************************************************* * CONFIGURATION 主配置 ******************************************************************************************* */ // TRUE: just use single port(declare in SPI_MONOPORT_USED), so the module can create // optimised code. // #define SPI_MONOPORT_NEEDED to specify that you will just use one SPI port #ifdef SPI_MONOPORT_NEEDED // if just use one SPI port, #define SPI_MONOPORT_USED to specify which port you use. // default SPI0 #ifndef SPI_MONOPORT_USED #define SPI_MONOPORT_USED SPI0 #endif #endif // #define SPI_ARGUMENT_CHECK_DISABLE to specify that you don't need the argument check // function of spi module. // #define SPI_ARGUMENT_CHECK_DISABLE /* **************************************************************************************** * ERROR CODES **************************************************************************************** */ #define SPI_NO_ERR 0 /* Function call was successful */ #define SPI_INVALID_PORT 1 /* Invalid communications port channel */ #define SPI_ARGUMENT_OUT 2 /* Argument out of range */ #define SPI_RX_EMPTY 3 /* Rx buffer is empty, no character available */ #define SPI_TX_FULL 4 /* Tx buffer is full, could not deposit character */ #define SPI_TX_EMPTY 5 /* If the Tx buffer is empty. */ #define SPI_RX_TIMEOUT 6 /* If a timeout occurred while waiting for a character*/ #define SPI_TX_TIMEOUT 7 /* If a timeout occurred while waiting to send a char.*/ #define SPI_ERR_UNKNOWN 8 /* ****************************************************************************************** * TYPE DEFINE ****************************************************************************************** */ // for struct init-style typedef struct spi_init_struct{ unsigned char isMaster; // TRUE: spi is master. FALSE: spi is slave. unsigned char SSctrl; // see SPI_SS_CTRL unsigned char bitorder; // see SPI_BITORDER unsigned char inWaitMode; // see SPI_INWAITMODE unsigned char sckfmt; // see SPI_SCKFMT // BaudRateDivisor = (SPPR + 1) * 2^(SPR + 1) // BaudRate = Busclock / BaudRateDivisor unsigned char SPR; // 0-7,SPI Baud Rate Selection unsigned char SPPR; // 0-7,SPI Baud Rate Preselection }SPI_INIT_STRUCT,*pSPI_INIT_STRUCT; /* ******************************************************************************************* * INITIALIZATION CONFIGURATION 初始化配置 ******************************************************************************************* */ // the default parameter to initialize the SPI system // 初始化SPI的默认参数 #ifndef SPI_INIT_ISMASTER #define SPI_INIT_ISMASTER TRUE // TRUE: spi is master. FALSE: spi is slave. #endif #ifndef SPI_INIT_SSCTRL #define SPI_INIT_SSCTRL SPI_SS_CTRL_OFF // see SPI_SS_CTRL #endif #ifndef SPI_INIT_BITORDER #define SPI_INIT_BITORDER SPI_BITORDER_MSB // see SPI_BITORDER
史海拾趣
|
谁做过驱动啊?告诉我什么得什么样的流程啊,我假如我对scsi进行编程怎么办? 谁做过驱动啊?告诉我什么得什么样的流程啊,我假如我对scsi进行编程怎么办? PCI有13个关于scsi的针脚,怎么样的过程或流程给我说下被,如何编写?但是寄存器的首地址无法确定 该怎么做呢?… 查看全部问答> |
|
求助:WinCE StandardSDK Emulator启动后死机 Downloading files Downloading file d:\\wince\\test.exe. Finished downloading. 之后在StandardSDK Emulator打开My Computer 窗口里只有cemgrc, Control Panel 没有test.exe, windows, program files等 并且emulator死机了 有朋友碰过这 ...… 查看全部问答> |
|
Linux下CAN驱动问题(HMs30c7202) c_candrv-1.2.0 目前接手一项目,头有点大,CAN在运行过程中,会出现不能收发的情况,从新上电后正常 使用的是c_candrv-1.2.0驱动程序,个人感觉可能是使用方法不对 #define can0file \"/dev/can0\" #define can1file \"/dev/can1\" #define can2file \"/dev/can ...… 查看全部问答> |
|
有没有人用过 LoadAnimatedCursor 这个API啊? 环境 evc4, Standard SDK for wince 5.0 我在 resource.h 下这么定义 #define IDB_BITMAP1 MAKEINTRESOURCE(104) #define IDB_BITMAP2   ...… 查看全部问答> |
|
两个F149之间的通信 你好,我现在正在做两个F149之间的通信,要求是:发送和接受全用中断,而且当接受正确时,给对方一个应答信号!!!急用!!!谢谢!!! 能给我个例子好吗? 我是一个新手,7月份才毕业,现在在公司是试用期,所以我特别的 ...… 查看全部问答> |




