JN516XのUARTは一番重要なペリフェラルだ。デバイスへの書き込みはUART経由で行ってるしUART経由で文字列出力くらいできないとデバッグはウルトラ大変である。
SDK-APIのままだと少し使いづらいのでもう少し簡単に使えるようにライブラリを作ってみた。送受信とも割り込み処理されるのでユーザー側で割り込み処理を用意する必要はない。
UART0とUART1の2つをサポートし、全機能を網羅しつつArduino風に使えるようにしている。
Uart Serial0;
Uart Serial1;
上記2つのグローバル変数経由で利用でき、利用前にSerialX.begin()を呼び出すだけだ。
ボーレートは4800-4Mbpsまでの任意の速度が指定可能で規定は115200bps/8bit/stop1/noparityである。
【Uartライブラリ】
※未説明の”system.h”をインクルードしていることに注意!
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 |
/* uart.h - UART 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 "system.h" /* I/O Ports TXD RXD CTS RTS --------------------------------------------------- UART_0: DIO6/DIO14 DIO7/DIO15 DIO4/DIO12 DIO5/DIO13 UART_1: DIO14/DIO11 DIO15/DIO9 - - */ #ifndef UART_BUFFER_SIZE #define UART_BUFFER_SIZE 128 // Size of TX/RX Buffer, in range 16 to 2047 bytes #endif class Uart; extern Uart Serial0; extern Uart Serial1; class Uart { public: typedef enum { UART_0 = E_AHI_UART_0, UART_1 = E_AHI_UART_1, } UART; typedef enum { SPEED_4800 = 4800, SPEED_9600 = 9600, SPEED_14400 = 14400, SPEED_19200 = 19200, SPEED_38400 = 38400, SPEED_57600 = 57600, SPEED_115200 = 115200, SPEED_230400 = 230400, SPEED_460800 = 460800, SPEED_921600 = 921600, SPEED_2M = 2000000, SPEED_4M = 4000000, } SPEED; typedef enum { RTS_LOW = E_AHI_UART_RTS_LOW, RTS_HIGH = E_AHI_UART_RTS_HIGH, RTS_OFF, } RTS; typedef enum { MODE_5N1 = 0, MODE_6N1 = 1, MODE_7N1 = 2, MODE_8N1 = 3, MODE_5N2 = 4, MODE_6N2 = 5, MODE_7N2 = 6, MODE_8N2 = 7, MODE_5E1 = 8, MODE_6E1 = 9, MODE_7E1 = 10, MODE_8E1 = 11, MODE_5E2 = 12, MODE_6E2 = 13, MODE_7E2 = 14, MODE_8E2 = 15, MODE_5O1 = 16, MODE_6O1 = 17, MODE_7O1 = 18, MODE_8O1 = 19, MODE_5O2 = 20, MODE_6O2 = 21, MODE_7O2 = 22, MODE_8O2 = 23, } MODE; typedef enum { INTMASK_MODEM = 0x01, // CTS change detected for UART0 INTMASK_STATUS = 0x02, // break indication, framing error, parity error or over-run INTMASK_TXEMPTY = 0x04, // Transmit FIFO empty INTMASK_RXDATA = 0x08, // receive data available } INTMASK; typedef enum { INTLEVEL_1 = E_AHI_UART_FIFO_LEVEL_1, INTLEVEL_4 = E_AHI_UART_FIFO_LEVEL_4, INTLEVEL_8 = E_AHI_UART_FIFO_LEVEL_8, INTLEVEL_14 = E_AHI_UART_FIFO_LEVEL_14, } INTLEVEL; bool begin(SPEED speed = SPEED_115200, MODE mode = MODE_8N1, RTS rts = RTS_OFF, bool location = false, bool txOnly = false, /*INTMASK*/uint8 imask = 0, PR_HWINT_APPCALLBACK prUartcallback = 0, INTLEVEL level = INTLEVEL_1) { setup(); // Calculate Devisor and Cpb. uint32 u32Div = 0; uint32 u32Cpb; do u32Cpb = (F_PCK / ++u32Div) / speed; while (u32Cpb > 16); if ((u32Div > 255) || (u32Cpb < 4)) return false; // if (_uart == UART_0) vAHI_UartSetRTSCTS(_uart, rts < RTS_OFF); if (_uart == UART_1) vAHI_UartTxOnly(_uart, txOnly); vAHI_UartSetLocation(_uart, location); bool rv = bAHI_UartEnable(_uart, _au8TxBuf, UART_BUFFER_SIZE, _au8RxBuf, UART_BUFFER_SIZE); if (rv) { vAHI_UartSetBaudDivisor(_uart, u32Div); vAHI_UartSetClocksPerBit(_uart, u32Cpb - 1); vAHI_UartSetControl ( _uart, (mode >> 3) & 1 ? E_AHI_UART_EVEN_PARITY : E_AHI_UART_ODD_PARITY, (mode >> 3) ? E_AHI_UART_PARITY_ENABLE : E_AHI_UART_PARITY_DISABLE, (mode & 3), (mode >> 2) & 1 ? E_AHI_UART_2_STOP_BITS : E_AHI_UART_1_STOP_BIT, rts == RTS_HIGH ? RTS_HIGH : RTS_LOW ); if (prUartcallback) { switch (_uart) { case UART_0: vAHI_Uart0RegisterCallback(prUartcallback); break; case UART_1: vAHI_Uart1RegisterCallback(prUartcallback); break; } } else imask = 0; vAHI_UartSetInterrupt ( _uart, imask & INTMASK_MODEM, imask & INTMASK_STATUS, imask & INTMASK_TXEMPTY, imask & INTMASK_RXDATA, level ); } return rv; } void end(void) { vAHI_UartDisable(_uart); } void setRTS(RTS ctrl) { if ((_uart == UART_0) && (ctrl < RTS_OFF)) vAHI_UartSetRTS(_uart, ctrl); } typedef enum { RXLEVEL_8 = E_AHI_UART_FIFO_ARTS_LEVEL_8, RXLEVEL_11 = E_AHI_UART_FIFO_ARTS_LEVEL_11, RXLEVEL_13 = E_AHI_UART_FIFO_ARTS_LEVEL_13, RXLEVEL_15 = E_AHI_UART_FIFO_ARTS_LEVEL_15, } RXLEVEL; typedef enum { POLARITY_ACTIVE_LOW = false, POLARITY_ACTIVE_HIGH = true, } POLARITY; typedef enum { AUTORTS_DISABLE = false, AUTORTS_ENABLE = true, } AUTORTS; typedef enum { AUTOCTS_DISABLE = false, AUTOCTS_ENABLE = true, } AUTOCTS; void setAutoFlowCtrl(RXLEVEL level, POLARITY bFlowCtrlPolarity, AUTORTS bAutoRts, AUTOCTS bAutoCts) { if (_uart == UART_0) vAHI_UartSetAutoFlowCtrl(_uart, level, bFlowCtrlPolarity, bAutoRts, bAutoCts); } void setBreak(bool bBreak) { vAHI_UartSetBreak(_uart, bBreak); } void reset(bool bTxReset = true, bool bRxReset = true) { vAHI_UartReset(_uart, bTxReset, bRxReset); } typedef enum { LINESTATUS_ERROR = E_AHI_UART_LS_ERROR, LINESTATUS_TXREADY = E_AHI_UART_LS_TEMT, LINESTATUS_TXEMPTY = E_AHI_UART_LS_THRE, LINESTATUS_BI = E_AHI_UART_LS_BI, LINESTATUS_FE = E_AHI_UART_LS_FE, LINESTATUS_PE = E_AHI_UART_LS_PE, LINESTATUS_OE = E_AHI_UART_LS_OE, LINESTATUS_RXREADY = E_AHI_UART_LS_DR, } LINESTATUS; LINESTATUS lineStatus(void) { // bit combination return (LINESTATUS)u8AHI_UartReadLineStatus(_uart); } typedef enum { MODEMSTATUS_DCTS = E_AHI_UART_MS_DCTS, // CTS input has changed. MODEMSTATUS_CTS = E_AHI_UART_MS_CTS, // ‘1’ indicates CTS is high, ‘0’ indicates CTS is low } MODEMSTATUS; MODEMSTATUS modemStatus(void) { // bit combination return (MODEMSTATUS)(_uart == UART_0 ? u8AHI_UartReadModemStatus(_uart) : 0); } typedef enum { INTSTATUS_RXLINE = E_AHI_UART_INT_RXLINE, INTSTATUS_RXDADA = E_AHI_UART_INT_RXDATA, INTSTATUS_TIMEOUT = E_AHI_UART_INT_TIMEOUT, INTSTATUS_TX = E_AHI_UART_INT_TX, INTSTATUS_MODEM = E_AHI_UART_INT_MODEM, } INTSTATUS; INTSTATUS interruptStatus(void) { // bit combination return (INTSTATUS)u8AHI_UartReadInterruptStatus(_uart); } uint16 availableForWrite(void) { return UART_BUFFER_SIZE - u16AHI_UartReadTxFifoLevel(_uart); } uint16 write(uint8 u8Data) { if (!availableForWrite()) return 0; vAHI_UartWriteData(_uart, u8Data); return 1; } uint16 write(const uint8 *pu8Buf, uint16 u16Len) { return pu8Buf ? u16AHI_UartBlockWriteData(_uart, (uint8 *)pu8Buf, u16Len) : 0; } uint16 available(void) { return u16AHI_UartReadRxFifoLevel(_uart); } int read(uint32 timeout = 0) { uint32 now = System::micros(); do { if (available()) return u8AHI_UartReadData(_uart); yield(); } while (System::micros() - now < timeout); return -1; } uint16 read(uint8 *pu8Buf, uint16 u16Len, uint32 timeout = 0) { uint16 cnt = 0; if (pu8Buf) { uint32 now = System::micros(); do { if (available()) { cnt += u16AHI_UartBlockReadData(_uart, pu8Buf + cnt, u16Len - cnt); if (cnt >= u16Len) break; now = System::micros(); } yield(); } while (System::micros() - now < timeout); } return cnt; } private: static void setup(void) { Serial0._uart = UART_0; Serial1._uart = UART_1; } UART _uart; uint8 _au8TxBuf[UART_BUFFER_SIZE]; uint8 _au8RxBuf[UART_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 |
/* uart.cpp - UART 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 "uart.h" Uart Serial0; Uart Serial1; |
【Debugライブラリ】
Debugライブラリは、SDKに含まれる
“Components/Utilities/Source/printf.c”
を改良したものをクラス化してみた。
1 2 3 4 5 6 |
#include "debug.h" ... Debug::begin(); ... Debug::printf("result = %d\n", result); ... |
のような感じで使うことができTerminalソフト或いはBeyondStudioのTerminalペインに出力することができるので非常に便利だ。printfの書式は一般的な仕様と同じ。
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 |
/* debug.h - Debug Utility 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 <stdarg.h> #include <jendefs.h> #include "uart.h" #include "task.h" #ifndef DEBUG_UART #define DEBUG_UART UART_0 #endif class Debug { public: static void begin(Uart::UART uart = Uart::DEBUG_UART); static void printf(const char *format, ...); static void putch(char c); private: typedef struct { char fillchar; char alignchar; char signchar; char hexchar; uint8 width; uint8 precision; uint8 typelong; uint8 base; uint64 value; } format_t; static void putstr(format_t& option, char *str); static void nformat(format_t& option); static Uart *_output; static Mutex _mutex; }; |
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 |
/* debug.cpp - Debug Utility 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 <ctype.h> #include <string.h> #include "debug.h" #include "system.h" #include "muldiv.h" Uart *Debug::_output; Mutex Debug::_mutex; void Debug::begin(Uart::UART uart) { switch (uart) { case Uart::UART_0 : _output = &Serial0; break; case Uart::UART_1 : _output = &Serial1; break; } if (_output) _output->begin(); _mutex.init(); } void Debug::printf(const char *format, ...) { const char *p = format; char c; va_list ap; va_start(ap, format); _mutex.lock(); do { c = *p++; if (c == '%') { format_t option = {fillchar : ' '}; if ((*p == '-') || (*p == '+')) option.alignchar = *p++; if (*p == '0') option.fillchar = *p++; while (isdigit(*p)) option.width = option.width * 10 + (*p++ - '0'); if (*p == '.') { ++p; while (isdigit(*p)) option.precision = option.precision * 10 + (*p++ - '0'); } while (*p == 'l') { ++p; ++option.typelong; } switch (*p++) { /* %d - integer */ case 'd': option.value = option.typelong < 2 ? va_arg(ap, int32) : va_arg(ap, int64); if ((int64)option.value < 0) { option.value = 0 - option.value; option.signchar = '-'; } else if (option.alignchar == '+') option.signchar = '+'; option.base = 10; nformat(option); break; /* %u - unsigned integer */ case 'u': option.value = option.typelong < 2 ? va_arg(ap, uint32) : va_arg(ap, uint64); option.base = 10; nformat(option); break; /* %X - hex */ case 'X': option.value = option.typelong < 2 ? va_arg(ap, uint32) : va_arg(ap, uint64); option.hexchar = 'A'; option.base = 16; nformat(option); break; /* %x - hex */ case 'x': option.value = option.typelong < 2 ? va_arg(ap, uint32) : va_arg(ap, uint64); option.hexchar = 'a'; option.base = 16; nformat(option); break; /* %o - octal */ case 'o': option.value = option.typelong < 2 ? va_arg(ap, uint32) : va_arg(ap, uint64); option.base = 8; nformat(option); break; /* %b - binary */ case 'b': option.value = option.typelong < 2 ? va_arg(ap, uint32) : va_arg(ap, uint64); option.base = 2; nformat(option); break; /* %c - character */ case 'c': putch(va_arg(ap, int)); break; /* %s - string */ case 's': option.fillchar = ' '; putstr(option, va_arg(ap, char *)); break; /* %% - '%' character */ case '%': putch('%'); break; /* %something else not handled! */ default: putch('?'); break; } } else if (c) { putch(c); if (c == '\n') putch('\r'); } } while (c); _mutex.unlock(); va_end(ap); } void Debug::putch(char c) { if (_output) { _mutex.lock(); while (_output->write(c) == 0) yield(); _mutex.unlock(); } } void Debug::putstr(format_t& option, char *str) { size_t i, len = strlen(str); if (option.precision && (len > option.precision)) len = option.precision; if (option.alignchar == '-') { for (i = 0; i < len; ++i) putch(*str++); for (i = len; i < option.width; ++i) putch(option.fillchar); } else { for (i = len; i < option.width; ++i) putch(option.fillchar); for (i = 0; i < len; ++i) putch(*str++); } } void Debug::nformat(format_t& option) { char buf[128], *p = buf + sizeof(buf); *--p = 0; do { uint8 c; if (option.typelong < 2) { c = (uint32)option.value % option.base; option.value = (uint32)option.value / option.base; } else { uint64 r; option.value = MulDiv::divu64u64(option.value, option.base, &r); c = (uint8)r; } if (c < 10) *--p = '0' + c; else *--p = option.hexchar + (c - 10); } while (option.value); if (option.signchar) *--p = option.signchar; putstr(option, p); } |
次回は、Uartライブラリでインクルードされているシステムライブラリの予定。
【関連投稿】
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)