SPIは1-16MHzの速度に対応。スレーブ機能はデバッグしてないので動かないかも。
【ループバックテストのサンプル】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include "device.h" SpiConfig spi; void setup(void) { spi.mode(SpiConfig::MODE_3); } void loop(void) { spi.beginTransaction(); Spi::select(Spi::SEL_0); for (uint16 i = 0; i < 256; ++i) { uint8 b = Spi::transfer((uint8)i); if (b != i) { /* Spi Error */ break; } } Spi::unselect(); spi.endTransaction(); } |
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
/* spi.h - SPI Library for NXP-JN516x Copyright (c) 2022 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <jendefs.h> #include <AppHardwareApi.h> #include "printf.h" /* I/O Ports SCLK: DO0 MISO: DO1 MOSI: DIO18 SEL0: DIO19 SEL1: DIO0/DIO14 SEL2: DIO1/DIO15 */ #ifndef SPI_DEFAULT_SPEED #define SPI_DEFAULT_SPEED SPEED_8MHZ #endif #ifndef SPI_SLAVE_BUFFER_SIZE #define SPI_SLAVE_BUFFER_SIZE 128 // 1 to 255 bytes #endif #ifndef SPI_SLAVE_TIMEOUT #define SPI_SLAVE_TIMEOUT 1000 // 0 to 4095 us #endif #ifndef SPI_SLAVE_THRESHOLD #define SPI_SLAVE_THRESHOLD 10 // 0 to 100 % #endif class SpiConfig { public: typedef enum { MODE_0 = 0, MODE_1 = 1, MODE_2 = 2, MODE_3 = 3, } MODE; typedef enum { SPEED_16MHZ = 0, SPEED_8MHZ = 1, SPEED_4MHZ = 2, SPEED_2MHZ = 3, SPEED_1MHZ = 4, } SPEED; /* must be first call */ SpiConfig& mode(MODE mode) { _eMode = mode; _eSpeed = SPI_DEFAULT_SPEED; _bLsbFirst = false; _u8SlaveEnable = 2; // bit0, 1, 2 _bAutoSelect = false; _delayReadEdge = false; _prSpiCallback = 0; _configuration = 0; _bLocation = 0; _changed = true; return *this; } /* Clock divisor in the range 0 to 63. Peripheral clock is divided by 2 x u8ClockDivider, but 0 is a special value used when no clock division is required */ SpiConfig& speed(SPEED value) { if (_eSpeed != value) _changed = true; _eSpeed = value; return *this; } /* Enable/disable data transfer with the least significant bit (LSB) transferred first: TRUE - enable FALSE - disable */ SpiConfig& lsbFirst(bool value) { if (_bLsbFirst != value) _changed = true; _bLsbFirst = value; return *this; } /* Number of SPI slaves to control. Valid values are 0-3 (higher values are truncated to 3) */ SpiConfig& slaveEnable(uint8 value) { if (_u8SlaveEnable != value) _changed = true; _u8SlaveEnable = value; return *this; } /* Enable/disable automatic slave selection: TRUE - enable FALSE - disable */ SpiConfig& autoSelect(bool value) { if (_bAutoSelect != value) _changed = true; _bAutoSelect = value; return *this; } typedef enum { SELLOC_1 = E_AHI_SPISEL_1, SELLOC_2 = E_AHI_SPISEL_2, } SELLOC; /* DIO on which specified slave select line will operate: TRUE - DIO14 (SPISEL1) or DIO15 (SPISEL2) FALSE - DIO0 (SPISEL1) or DIO1 (SPISEL2) */ SpiConfig& location(SELLOC sel, bool value) { switch (sel) { case SELLOC_1: if (value) _bLocation |= (1 << SELLOC_1); else _bLocation &= ~(1 << SELLOC_1); break; case SELLOC_2: if (value) _bLocation |= (1 << SELLOC_2); else _bLocation &= ~(1 << SELLOC_2); break; } return *this; } SpiConfig& delayReadEdge(bool value) { _delayReadEdge = value; return *this; } /* * Pointer to callback function to be registered */ SpiConfig& callback(PR_HWINT_APPCALLBACK prSpiCallback) { if (_prSpiCallback != prSpiCallback) _changed = true; _prSpiCallback = prSpiCallback; return *this; } void beginTransaction(void) { if (!_changed) vAHI_SpiRestoreConfiguration(&_configuration); else { _changed = false; vAHI_SpiConfigure ( _u8SlaveEnable, _bLsbFirst, _eMode >> 1, _eMode & 1, _eSpeed, _prSpiCallback != 0, _bAutoSelect ); vAHI_SpiReadConfiguration(&_configuration); } vAHI_SpiSelSetLocation(E_AHI_SPISEL_1, _bLocation & (1 << SELLOC_1)); vAHI_SpiSelSetLocation(E_AHI_SPISEL_2, _bLocation & (1 << SELLOC_2)); vAHI_SpiSetDelayReadEdge(_delayReadEdge); if (_prSpiCallback) vAHI_SpiRegisterCallback(_prSpiCallback); _configured = true; } void endTransaction(void) { } static void end(void) { if (_configured) { _configured = false; vAHI_SpiDisable(); } } private: tSpiConfiguration _configuration; uint8 _bLocation; MODE _eMode; SPEED _eSpeed; bool _bLsbFirst; uint8 _u8SlaveEnable; bool _bAutoSelect; bool _delayReadEdge; PR_HWINT_APPCALLBACK _prSpiCallback; uint8 _changed; static uint8 _configured; friend class Spi; }; class Spi { public: typedef enum { SEL_0 = 1, // DIO19 SEL_1 = 2, // DIO0/DIO14 SEL_2 = 4, // DIO1/DIO15 } SEL; static void select(SEL u8SlaveMask) { vAHI_SpiSelect(u8SlaveMask); } static void unselect(void) { vAHI_SpiStop(); } static void writeAndRead(uint16 slave, const uint8 *pu8Write, uint16 u16WriteLen, uint8 *pu8Read, uint16 u16ReadLen) { select((SEL)slave); write(pu8Write, u16WriteLen); read(pu8Read, u16ReadLen); unselect(); } static void write(uint8 u8Out) { transfer(u8Out); } static void write(const uint8 *pu8Buf, uint16 u16Len) { if (pu8Buf) { while (u16Len--) write(*pu8Buf++); } } static void write16(uint16 u16Out) { transfer16(u16Out); } static void write16(const uint16 *pu16Buf, uint16 u16Len) { if (pu16Buf) { while (u16Len--) write16(*pu16Buf++); } } static void write32(uint32 u32Out) { transfer32(u32Out); } static void write32(const uint32 *pu32Buf, uint16 u16Len) { if (pu32Buf) { while (u16Len--) write32(*pu32Buf++); } } static uint8 read(void) { return transfer((uint8)-1); } static void read(uint8 *pu8Buf, uint16 u16Len) { if (pu8Buf) { for (uint32 i = 0; i < u16Len; ++i) *pu8Buf++ = read(); } } static uint16 read16(void) { return transfer16((uint16)-1); } static void read16(uint16 *pu16Buf, uint16 u16Len) { if (pu16Buf) { for (uint32 i = 0; i < u16Len; ++i) *pu16Buf++ = read16(); } } static uint32 read32(void) { return transfer32((uint32)-1); } static void read32(uint32 *pu32Buf, uint16 u16Len) { if (pu32Buf) { for (uint32 i = 0; i < u16Len; ++i) *pu32Buf++ = read32(); } } static uint8 transfer(uint8 u8Out) { return transfer(u8Out, 7); } static uint16 transfer16(uint16 u16Out) { return transfer(u16Out, 15); } static uint32 transfer32(uint32 u32Out) { return transfer(u32Out, 31); } static uint32 transfer(uint32 u32Out, uint8 u8CharLen) { vAHI_SpiStartTransfer(u8CharLen, u32Out); wait(); return u32AHI_SpiReadTransfer32(); } private: static bool busy(void) { // This function polls the SPI master to determine whether it is currently busy // performing a data transfer. return bAHI_SpiPollBusy(); } static void wait(void) { // This function waits for the SPI master to complete a transfer and then returns. vAHI_SpiWaitBusy(); } }; class SSpi { public: typedef enum { INTMASK_RX_FIRST = E_AHI_SPIS_INT_RX_FIRST_MASK, INTMASK_TX_LAST = E_AHI_SPIS_INT_TX_LAST_MASK, INTMASK_RX_CLIMB = E_AHI_SPIS_INT_RX_CLIMB_MASK, INTMASK_TX_FALL = E_AHI_SPIS_INT_TX_FALL_MASK, INTMASK_RX_OVER = E_AHI_SPIS_INT_RX_OVER_MASK, INTMASK_TX_OVER = E_AHI_SPIS_INT_TX_OVER_MASK, INTMASK_RX_UNDER = E_AHI_SPIS_INT_RX_UNDER_MASK, INTMASK_TX_UNDER = E_AHI_SPIS_INT_TX_UNDER_MASK, INTMASK_RX_TIMEOUT = E_AHI_SPIS_INT_RX_TIMEOUT_MASK, } INTMASK; static bool begin(bool bPinLocation = false, bool bLsbFirst = false, /*INTMASK*/uint16 imask = 0, PR_HWINT_APPCALLBACK prSpiCallback = 0) { if (prSpiCallback) vAHI_SpiSlaveRegisterCallback(prSpiCallback); else imask = 0; return bAHI_SpiSlaveEnable ( bPinLocation, bLsbFirst, _u8TxBuffer, SPI_SLAVE_BUFFER_SIZE, SPI_SLAVE_BUFFER_SIZE * SPI_SLAVE_THRESHOLD / 100, // an interrupt will be generated when the amount of data in the buffer falls below this level _u8RxBuffer, SPI_SLAVE_BUFFER_SIZE, SPI_SLAVE_BUFFER_SIZE * (100 - SPI_SLAVE_THRESHOLD) / 100, // an interrupt will be generated when the amount of data in the buffer rises above this level SPI_SLAVE_TIMEOUT, imask ); } static void end(void) { vAHI_SpiSlaveDisable(); } static void reset(bool bTxReset, bool bRxReset) { vAHI_SpiSlaveReset(bTxReset, bRxReset); } static uint8 availableForWrite(void) { return SPI_SLAVE_BUFFER_SIZE - u8AHI_SpiSlaveTxFillLevel(); } static uint16 write8(uint8 u8Byte) { if (!availableForWrite()) return 0; vAHI_SpiSlaveTxWriteByte(u8Byte); return 1; } static uint16 write8(const uint8 *pu8Buf, uint16 u16Len) { uint16 cnt; for (cnt = 0; cnt < u16Len; ++cnt) { if (!write8(*pu8Buf++)) break; } return cnt; } static uint8 available(void) { return u8AHI_SpiSlaveRxFillLevel(); } static int read8(void) { return available() ? u8AHI_SpiSlaveRxReadByte() : -1; } static uint16 read8(uint8 *pu8Buf, uint16 u16Len) { uint16 cnt; for (cnt = 0; cnt < u16Len; ++cnt) { int b; if ((b = read8()) < 0) break; *pu8Buf++ = b; } return cnt; } static uint8 status(void) { return u8AHI_SpiSlaveStatus(); } private: static uint8 _u8TxBuffer[SPI_SLAVE_BUFFER_SIZE]; static uint8 _u8RxBuffer[SPI_SLAVE_BUFFER_SIZE]; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* spi.cpp - SPI Library for NXP-JN516x Copyright (c) 2022 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "spi.h" uint8 SpiConfig::_configured; uint8 SSpi::_u8TxBuffer[]; uint8 SSpi::_u8RxBuffer[]; |
次回は、ADCの予定
【関連投稿】
NXP JN516X (TWELITE) をプログラミングする(開発環境の構築)
NXP JN516X (TWELITE) をプログラミングする(メイン・ルーチン)
NXP JN516X (TWELITE) をプログラミングする(TICKTIMER)
NXP JN516X (TWELITE) をプログラミングする(UART)
NXP JN516X (TWELITE) をプログラミングする(SYSTEM)
NXP JN516X (TWELITE) をプログラミングする(GPIO)
NXP JN516X (TWELITE) をプログラミングする(TIMER)
NXP JN516X (TWELITE) をプログラミングする(ALARM)
NXP JN516X (TWELITE) をプログラミングする(WAKETIMER)
NXP JN516X (TWELITE) をプログラミングする(WATCHDOG)
NXP JN516X (TWELITE) をプログラミングする(I2C)
NXP JN516X (TWELITE) をプログラミングする(SPI)
NXP JN516X (TWELITE) をプログラミングする(ADC)
NXP JN516X (TWELITE) をプログラミングする(COMPARATOR)
NXP JN516X (TWELITE) をプログラミングする(CLOCK)
NXP JN516X (TWELITE) をプログラミングする(BROWNOUT)
NXP JN516X (TWELITE) をプログラミングする(PULSCOUNTER)
NXP JN516X (TWELITE) をプログラミングする(INFRARED)
NXP JN516X (TWELITE) をプログラミングする(RANDOM-GENERATOR)
NXP JN516X (TWELITE) をプログラミングする(FLASH)
NXP JN516X (TWELITE) をプログラミングする(EEPROM)
NXP JN516X (TWELITE) をプログラミングする(WPAN)
NXP JN516X (TWELITE) をプログラミングする(Eclipse-CDT+MWSTAGE)
NXP JN516X (TWELITE) をプログラミングする(乗算と除算)
NXP JN516X (TWELITE) をプログラミングする(マルチタスク)
NXP JN516X (TWELITE) をプログラミングする(フラッシュ・プログラマー)
NXP JN516X (TWELITE) をプログラミングする(OTA UPDATE)
NXP JN516X (TWELITE) をプログラミングする(TWELITE CUE/MC3630)
NXP JN516X (TWELITE) をプログラミングする(LED)
NXP JN516X (TWELITE) をプログラミングする(AES)
NXP JN516X (TWELITE) をプログラミングする(Downloads)