このライブラリは、マルチマスター、100/400/800KHzの通信速度、7/10bitアドレス、リピーテッドスタートに対応している。
スレーブクラスも存在するが仕様不明点が多く絶対動かないと思うのでデバッグすらしていない。無視してほしい。
【サンプル】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include "device.h" #define SLAVE 0x27 void setup(void) { Wire::begin(Wire::SPEED_100KHZ); } void loop(void) { uint8 wrbuf[] = {...}; uint8 rdbuf[]; ... /* write only */ Wire::write(SLAVE, wrbuf, sizeof(wrbuf)); /* read only */ Wire::read(SLAVE, rdbuf, sizeof(rdbuf)); /* write and read (repeated start) */ Wire::writeAndRead(SLAVE, wrbuf, sizeof(wrbuf), rdbuf, sizeof(rdbuf)); ... } |
【ライブラリ】
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 |
/* wire.h - Two Wire 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> /* I/O Ports SCK: DIO14/DIO16 SDA: DIO15/DIO17 */ #define WIRE_TENBIT_ADDRESS 0x8000 #ifndef WIRES_BUFFER_SIZE #define WIRES_BUFFER_SIZE 128 #endif class Wire { public: typedef enum { SPEED_100KHZ = 100000, // Standard SPEED_400KHZ = 400000, // Fast SPEED_800KHZ = 800000, // Fast-Plus (Max 1.0MHz) } SPEED; static void begin(SPEED speed = SPEED_100KHZ, bool bPulseSuppressionEnable = true) { vAHI_SiMasterConfigure(bPulseSuppressionEnable, false, F_PCK / speed / 5 - 1); } static void end(void) { vAHI_SiMasterDisable(); } static void location(bool bLocation) { // DIOs on which interface will operate: // TRUE - DIO16-17 // FALSE - DIO14-15 (default) vAHI_SiSetLocation(bLocation); } static bool write(uint16 slave, const uint8 *pu8Buf, uint16 u16Len, bool stop = true) { if (!pu8Buf && u16Len) return false; if (!start(slave, !u16Len && stop, false)) return false; while (u16Len) { vAHI_SiMasterWriteData8(*pu8Buf++); if (!command(!--u16Len && stop ? CMD_WRITE_STOP : CMD_WRITE)) return false; } return true; } static bool read(uint16 slave, uint8 *pu8Buf, uint16 u16Len, bool stop = true) { if (!pu8Buf && u16Len) return false; if (!start(slave, !u16Len && stop, true)) return false; while (u16Len) { if (!command(!--u16Len && stop ? CMD_READ_STOP : CMD_READ)) return false; *pu8Buf++ = u8AHI_SiMasterReadData8(); } return true; } static bool writeAndRead(uint16 slave, const uint8 *pu8Write, uint16 u16WriteLen, uint8 *pu8Read, uint16 u16ReadLen, bool stop = true) { return write(slave, pu8Write, u16WriteLen, false) && read(slave, pu8Read, u16ReadLen, stop); } private: typedef enum { // Start Stop Read Write Resulting Instruction to SI Bus CMD_IDLE = 0b0000, // No active command (idle) CMD_START = 0b1001, // Start followed by Write CMD_START_STOP = 0b1101, // Start followed by Write followed by Stop CMD_READ_STOP = 0b0110, // Read followed by Stop CMD_WRITE_STOP = 0b0101, // Write followed by Stop CMD_WRITE = 0b0001, // Write only CMD_READ = 0b0010, // Read only CMD_STOP = 0b0100, // Stop only } CMD; static void bus(CMD cmd) { bAHI_SiMasterSetCmdReg ( cmd & (CMD_START - CMD_WRITE) ? E_AHI_SI_START_BIT : E_AHI_SI_NO_START_BIT, // bSetSTA, cmd & CMD_STOP ? E_AHI_SI_STOP_BIT : E_AHI_SI_NO_STOP_BIT, // bSetSTO, cmd & CMD_READ ? E_AHI_SI_SLAVE_READ : E_AHI_SI_NO_SLAVE_READ, // bSetRD, cmd & CMD_WRITE ? E_AHI_SI_SLAVE_WRITE : E_AHI_SI_NO_SLAVE_WRITE, // bSetWR, cmd == CMD_READ ? E_AHI_SI_SEND_ACK : E_AHI_SI_SEND_NACK, // bSetAckCtrl E_AHI_SI_NO_IRQ_ACK // bSetIACK ); } static bool command(CMD cmd) { bus(cmd); while (bAHI_SiMasterPollTransferInProgress()) continue; if (!bAHI_SiMasterPollArbitrationLost() && !bAHI_SiMasterCheckRxNack()) return true; if (!(cmd & CMD_STOP)) bus(CMD_STOP); return false; } static bool start(uint16 slave, bool stop, bool read) { if (slave & ~0x7F) { vAHI_SiMasterWriteSlaveAddr(0x78 | ((slave >> 8) & 0x03), read); if (command(CMD_START)) { vAHI_SiMasterWriteData8(slave); if (command(stop ? CMD_WRITE_STOP : CMD_WRITE)) return true; } } else { vAHI_SiMasterWriteSlaveAddr(slave & 0x7F, read); if (command(stop ? CMD_START_STOP : CMD_START)) return true; } return false; } }; class SWire { public: typedef void (*SWIRECALLBACK)(const uint8 *pu8Buf, uint16 u16Len); typedef enum { IMASK_RR = E_AHI_SIS_DATA_RR_MASK, // Data buffer must be written with data to be read by SI master IMASK_RTKN = E_AHI_SIS_DATA_RTKN_MASK, // Data taken from buffer by SI master - buffer free for next data IMASK_WA = E_AHI_SIS_DATA_WA_MASK, // Data buffer contains data from SI master to be read by SI slave IMASK_LAST = E_AHI_SIS_LAST_DATA_MASK, // Last data transferred (end of burst) IMASK_ERROR = E_AHI_SIS_ERROR_MASK, // I2C protocol error } IMASK; static void begin(SWIRECALLBACK prSWireCallback, uint16 u16SlaveAddress, bool bPulseSuppressionEnable = true) { _prSWireCallback = prSWireCallback; _rdcount = 0; _wrcount = 0; _wrindex = 0; vAHI_SiRegisterCallback(SiCallback); vAHI_SiSlaveConfigure(u16SlaveAddress & 0x3FF, u16SlaveAddress & ~0x7F, bPulseSuppressionEnable, IMASK_RR | IMASK_RTKN | IMASK_WA | IMASK_LAST | IMASK_ERROR, false); } static void end(void) { vAHI_SiSlaveDisable(); } static void write(uint8 u8Out) { if (_wrcount < WIRES_BUFFER_SIZE) _wrbuffer[_wrcount++] = u8Out; } static void write(uint8 *pu8Out, uint16 u16Len) { while (u16Len--) write(*pu8Out++); } private: static void SiCallback(uint32 u32Device, uint32 u32ItemBitmap) { // reset if (u32ItemBitmap & (IMASK_RTKN | IMASK_ERROR)) { _rdcount = 0; _wrcount = 0; _wrindex = 0; } // write if (u32ItemBitmap & IMASK_RR) { if (_wrindex < _wrcount) vAHI_SiSlaveWriteData8(_wrbuffer[_wrindex++]); } // read if (u32ItemBitmap & IMASK_WA) { uint8 b = u8AHI_SiSlaveReadData8(); if (_rdcount < WIRES_BUFFER_SIZE) _rdbuffer[_rdcount++] = b; } // callback if ((u32ItemBitmap & IMASK_LAST) && _prSWireCallback) _prSWireCallback(_rdbuffer, _rdcount); } static SWIRECALLBACK _prSWireCallback; static uint16 _rdcount; static uint8 _rdbuffer[WIRES_BUFFER_SIZE]; static uint16 _wrindex; static uint16 _wrcount; static uint8 _wrbuffer[WIRES_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 27 28 |
/* wire.cpp - Two Wire 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 "wire.h" SWire::SWIRECALLBACK SWire::_prSWireCallback; uint16 SWire::_rdcount; uint8 SWire::_rdbuffer[]; uint16 SWire::_wrindex; uint16 SWire::_wrcount; uint8 SWire::_wrbuffer[]; |
次回は、SPIの予定
【関連投稿】
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)