[457] | 1 | /*
|
---|
| 2 | * TOPPERS/ASP Kernel
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
| 4 | * Advanced Standard Profile Kernel
|
---|
| 5 | *
|
---|
| 6 | * Copyright (C) 2008-2011 by Embedded and Real-Time Systems Laboratory
|
---|
| 7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 8 | * Copyright (C) 2015-2016 by TOPPERS PROJECT Educational Working Group.
|
---|
| 9 | *
|
---|
| 10 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 11 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 12 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 13 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 14 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 15 | * スコード中に含まれていること.
|
---|
| 16 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 17 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 18 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 19 | * の無保証規定を掲載すること.
|
---|
| 20 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 21 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 22 | * と.
|
---|
| 23 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 24 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 25 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 26 | * 報告すること.
|
---|
| 27 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 28 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 29 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 30 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 31 | * 免責すること.
|
---|
| 32 | *
|
---|
| 33 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 34 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 35 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 36 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 37 | * の責任を負わない.
|
---|
| 38 | *
|
---|
| 39 | * @(#) $Id$
|
---|
| 40 | */
|
---|
| 41 | /*
|
---|
| 42 | *
|
---|
| 43 | * STM32F4XX SPIデバイスドライバの外部宣言
|
---|
| 44 | *
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | #ifndef _SPI_H_
|
---|
| 48 | #define _SPI_H_
|
---|
| 49 |
|
---|
| 50 | #ifdef __cplusplus
|
---|
| 51 | extern "C" {
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | /*
|
---|
| 55 | * SPIポート定義
|
---|
| 56 | */
|
---|
| 57 | #define SPI1_PORTID 1
|
---|
| 58 | #define SPI2_PORTID 2
|
---|
| 59 | #define NUM_SPIPORT 2
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | * SPI状態定義
|
---|
| 63 | */
|
---|
| 64 | #define SPI_STATUS_RESET 0x00 /* SPI未使用状態 */
|
---|
| 65 | #define SPI_STATUS_READY 0x01 /* SPIレディ状態 */
|
---|
| 66 | #define SPI_STATUS_ERROR 0x02 /* SPIエラー状態 */
|
---|
| 67 | #define SPI_STATUS_BUSY 0x04 /* SPI処理中 */
|
---|
| 68 |
|
---|
| 69 | /*
|
---|
| 70 | * SPIエラー定義
|
---|
| 71 | */
|
---|
| 72 | #define SPI_ERROR_NONE 0x00000000 /* No error */
|
---|
| 73 | #define SPI_ERROR_MODF 0x00000001 /* MODF error */
|
---|
| 74 | #define SPI_ERROR_CRC 0x00000002 /* CRC error */
|
---|
| 75 | #define SPI_ERROR_OVR 0x00000004 /* OVR error */
|
---|
| 76 | #define SPI_ERROR_FRE 0x00000008 /* FRE error */
|
---|
| 77 | #define SPI_ERROR_DMA 0x00000010 /* DMA transfer error */
|
---|
| 78 | #define SPI_ERROR_TIMEOUT 0x00000020
|
---|
| 79 |
|
---|
| 80 | /*
|
---|
| 81 | * SPI転送モード
|
---|
| 82 | */
|
---|
| 83 | #define SPI_XMODE_TX 0x00000000 /* 送信モード */
|
---|
| 84 | #define SPI_XMODE_RX 0x00000001 /* 受信モード */
|
---|
| 85 | #define SPI_XMODE_TXRX 0x00000002 /* 送受信モード */
|
---|
| 86 |
|
---|
| 87 | /*
|
---|
| 88 | * SPIモード定義
|
---|
| 89 | */
|
---|
| 90 | #define SPI_MODE_SLAVE 0x00000000
|
---|
| 91 | #define SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI)
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * SPIデータサイズ定義
|
---|
| 96 | */
|
---|
| 97 | #define SPI_DATASIZE_4BIT 0x00000300
|
---|
| 98 | #define SPI_DATASIZE_5BIT 0x00000400
|
---|
| 99 | #define SPI_DATASIZE_6BIT 0x00000500
|
---|
| 100 | #define SPI_DATASIZE_7BIT 0x00000600
|
---|
| 101 | #define SPI_DATASIZE_8BIT 0x00000700
|
---|
| 102 | #define SPI_DATASIZE_9BIT 0x00000800
|
---|
| 103 | #define SPI_DATASIZE_10BIT 0x00000900
|
---|
| 104 | #define SPI_DATASIZE_11BIT 0x00000A00
|
---|
| 105 | #define SPI_DATASIZE_12BIT 0x00000B00
|
---|
| 106 | #define SPI_DATASIZE_13BIT 0x00000C00
|
---|
| 107 | #define SPI_DATASIZE_14BIT 0x00000D00
|
---|
| 108 | #define SPI_DATASIZE_15BIT 0x00000E00
|
---|
| 109 | #define SPI_DATASIZE_16BIT 0x00000F00
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | * SPI転送クロック極性定義
|
---|
| 113 | */
|
---|
| 114 | #define SPI_POLARITY_LOW 0x00000000
|
---|
| 115 | #define SPI_POLARITY_HIGH SPI_CR1_CPOL
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * SPIクロック位相定義
|
---|
| 119 | */
|
---|
| 120 | #define SPI_PHASE_1EDGE 0x00000000
|
---|
| 121 | #define SPI_PHASE_2EDGE SPI_CR1_CPHA
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * SPI NSS定義
|
---|
| 125 | */
|
---|
| 126 | #define SPI_NSS_SOFT SPI_CR1_SSM
|
---|
| 127 | #define SPI_NSS_HARD_INPUT 0x00000000
|
---|
| 128 | #define SPI_NSS_HARD_OUTPUT 0x00040000
|
---|
| 129 |
|
---|
| 130 | /*
|
---|
| 131 | * SPI MSB/LSB定義
|
---|
| 132 | */
|
---|
| 133 | #define SPI_DATA_MSB 0x00000000
|
---|
| 134 | #define SPI_DATA_LSB SPI_CR1_LSBFIRST
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | * SPIプリスケーラ設定
|
---|
| 138 | */
|
---|
| 139 | #define SPI_BAUDRATEPRESCALER_2 0x00000000
|
---|
| 140 | #define SPI_BAUDRATEPRESCALER_4 0x00000008
|
---|
| 141 | #define SPI_BAUDRATEPRESCALER_8 0x00000010
|
---|
| 142 | #define SPI_BAUDRATEPRESCALER_16 0x00000018
|
---|
| 143 | #define SPI_BAUDRATEPRESCALER_32 0x00000020
|
---|
| 144 | #define SPI_BAUDRATEPRESCALER_64 0x00000028
|
---|
| 145 | #define SPI_BAUDRATEPRESCALER_128 0x00000030
|
---|
| 146 | #define SPI_BAUDRATEPRESCALER_256 0x00000038
|
---|
| 147 |
|
---|
| 148 | /*
|
---|
| 149 | * CRC長定義
|
---|
| 150 | */
|
---|
| 151 | #define SPI_CRC_LENGTH_DATASIZE 0x00000000
|
---|
| 152 | #define SPI_CRC_LENGTH_8BIT 0x00000001
|
---|
| 153 | #define SPI_CRC_LENGTH_16BIT 0x00000002
|
---|
| 154 |
|
---|
| 155 | /*
|
---|
| 156 | * SPI TIモード設定定義
|
---|
| 157 | */
|
---|
| 158 | #define SPI_TIMODE_DISABLE 0x00000000
|
---|
| 159 | #define SPI_TIMODE_ENABLE SPI_CR2_FRF
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * SPIライン設定
|
---|
| 163 | */
|
---|
| 164 | #define SPI_DIRECTION_2LINES ((uint32_t)0x00000000)
|
---|
| 165 | #define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY
|
---|
| 166 | #define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODE
|
---|
| 167 |
|
---|
| 168 | /*
|
---|
| 169 | * SPI CRC設定定義
|
---|
| 170 | */
|
---|
| 171 | #define SPI_CRC_DISABLE 0x00000000
|
---|
| 172 | #define SPI_CRC_ENABLE SPI_CR1_CRCEN
|
---|
| 173 |
|
---|
| 174 | /*
|
---|
| 175 | * SPIハードウェア設定構造体
|
---|
| 176 | */
|
---|
| 177 |
|
---|
| 178 | typedef struct _SPI_PortControlBlock{
|
---|
| 179 | uint32_t base;
|
---|
| 180 | uint32_t gioclockbase;
|
---|
| 181 | uint32_t gioclockbit;
|
---|
| 182 | uint32_t gioclockbit2;
|
---|
| 183 | uint32_t spiclockbase;
|
---|
| 184 | uint32_t spiclockbit;
|
---|
| 185 | uint32_t dmaclockbase;
|
---|
| 186 | uint32_t dmaclockbit;
|
---|
| 187 |
|
---|
| 188 | uint32_t giobase1;
|
---|
| 189 | uint32_t giobase2;
|
---|
| 190 | uint32_t giobase3;
|
---|
| 191 | uint8_t sckpin;
|
---|
| 192 | uint8_t misopin;
|
---|
| 193 | uint8_t mosipin;
|
---|
| 194 | uint8_t apvalue;
|
---|
| 195 | uint32_t dmatxchannel;
|
---|
| 196 | uint32_t dmatxbase;
|
---|
| 197 | uint32_t dmarxchannel;
|
---|
| 198 | uint32_t dmarxbase;
|
---|
| 199 | } SPI_PortControlBlock;
|
---|
| 200 |
|
---|
| 201 | /*
|
---|
| 202 | * SPI 設定初期設定構造体
|
---|
| 203 | */
|
---|
| 204 | typedef struct
|
---|
| 205 | {
|
---|
| 206 | uint32_t Mode; /* SPIマスタースレーブ設定 */
|
---|
| 207 | uint32_t Direction; /* SPI転送方向 */
|
---|
| 208 | uint32_t DataSize; /* SPI転送データサイズ */
|
---|
| 209 | uint32_t CLKPolarity; /* SPI転送クロックの極性 */
|
---|
| 210 | uint32_t CLKPhase; /* SPIクロック位相 */
|
---|
| 211 | uint32_t NSS; /* SPI NSS */
|
---|
| 212 | uint32_t Prescaler; /* SPIクロック分周設定 */
|
---|
| 213 | uint32_t SignBit; /* SPI MSB/LSB設定 */
|
---|
| 214 | uint32_t TIMode; /* SPI TIモード */
|
---|
| 215 | uint32_t CRC; /* SPI CRC演算設定 */
|
---|
| 216 | uint32_t CRCPolynomial; /* SPI CRC多項式設定 */
|
---|
| 217 | uint32_t CRCLength; /* SPI CRC長 */
|
---|
| 218 | int semid; /* SPI 通信用セマフォ値 */
|
---|
| 219 | int semlock; /* SPI ロックセマフォ値 */
|
---|
| 220 | }SPI_Init_t;
|
---|
| 221 |
|
---|
| 222 | /*
|
---|
| 223 | * SPIハンドラ
|
---|
| 224 | */
|
---|
| 225 | typedef struct _SPI_Handle_t
|
---|
| 226 | {
|
---|
| 227 | uint32_t base; /* SPI registers base address */
|
---|
| 228 | SPI_Init_t Init; /* SPI communication parameters */
|
---|
| 229 | uint8_t *pTxBuffPtr; /* Pointer to SPI Tx transfer Buffer */
|
---|
| 230 | uint16_t TxXferSize; /* SPI Tx transfer size */
|
---|
| 231 | uint16_t TxXferCount; /* SPI Tx Transfer Counter */
|
---|
| 232 | uint8_t *pRxBuffPtr; /* Pointer to SPI Rx transfer Buffer */
|
---|
| 233 | uint16_t RxXferSize; /* SPI Rx transfer size */
|
---|
| 234 | uint16_t RxXferCount; /* SPI Rx Transfer Counter */
|
---|
| 235 | DMA_Handle_t *hdmatx; /* SPI Tx DMA handle parameters */
|
---|
| 236 | DMA_Handle_t *hdmarx; /* SPI Rx DMA handle parameters */
|
---|
| 237 | uint32_t xmode;
|
---|
| 238 | volatile uint32_t status; /* SPI communication state */
|
---|
| 239 | volatile uint32_t ErrorCode; /* SPI Error code */
|
---|
| 240 | }SPI_Handle_t;
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | extern SPI_Handle_t *spi_init(ID port, SPI_Init_t *spii);
|
---|
| 245 | extern ER spi_deinit(SPI_Handle_t *hspi);
|
---|
| 246 | extern ER spi_reset(SPI_Handle_t *hspi);
|
---|
| 247 | extern ER spi_transmit(SPI_Handle_t *hspi, uint8_t *pdata, uint16_t length);
|
---|
| 248 | extern ER spi_receive(SPI_Handle_t *hspi, uint8_t *pdata, uint16_t length);
|
---|
| 249 | extern ER spi_transrecv(SPI_Handle_t *hspi, uint8_t *ptxData, uint8_t *prxData, uint16_t length);
|
---|
| 250 | extern ER spi_wait(SPI_Handle_t *hspi, uint32_t timeout);
|
---|
| 251 | extern void spi_handler(SPI_Handle_t *hspi);
|
---|
| 252 | extern void spi_isr(intptr_t exinf);
|
---|
| 253 |
|
---|
| 254 | #ifdef __cplusplus
|
---|
| 255 | }
|
---|
| 256 | #endif
|
---|
| 257 |
|
---|
| 258 | #endif /* _SPI_H_ */
|
---|
| 259 |
|
---|