first release
This commit is contained in:
commit
098b046860
|
@ -0,0 +1,51 @@
|
|||
# Arduino core support for CH32 EVT Boards
|
||||
|
||||
* [Introduction](https://github.com/openwch/arduino_core_ch32#Introduction)<br>
|
||||
* [How to use](https://github.com/openwch/arduino_core_ch32#How-to-use)<br>
|
||||
* [Supported boards](https://github.com/openwch/arduino_core_ch32#Supported-boards)<br>
|
||||
* [Submit bugs](https://github.com/openwch/arduino_core_ch32#Submit-bugs)<br>
|
||||
|
||||
## Introduction
|
||||
|
||||
This repo adds the support of CH32 MCU in Arduino IDE.<br>
|
||||
|
||||
The file includes:
|
||||
* [Arduino_Core_CH32](https://github.com/openwch/arduino_core_ch32):Public library files.
|
||||
* [openocd](https://github.com/openwch/openocd_wch):can directly use WCH-LINKE to download and debug wch chips.
|
||||
* [riscv-none-embed-gcc](https://github.com/openwch/risc-none-embed-gcc):A toolchain that supports WCH custom half word and byte compression instruction extensions and hardware stack push/pop functions.
|
||||
|
||||
## How to use
|
||||
|
||||
You can add this software package directly on the IDE through the [Arduino Boards Manager](https://www.arduino.cc/en/guide/cores).
|
||||
|
||||
Add the following link in the "*Additional Boards Managers URLs*" field:
|
||||
|
||||
https://github.com/openwch/board_manager_files/raw/main/package_ch32v_index.json
|
||||
|
||||
Then you can search for "**wch**" through the "**board manager**", find the installation package, and install it.
|
||||
|
||||
## Supported boards
|
||||
|
||||
It will be a long-term support and maintenance project, unless we encounter force majeure factors.The current version supports the following development boards:
|
||||
|
||||
- [CH32V00x EVT Boards](#CH32V00x-EVT-Boards)
|
||||
- [CH32V20x EVT Boards](#CH32V20x-EVT-Boards)
|
||||
|
||||
### CH32V00x EVT Boards
|
||||
|
||||
| Status | Boards name | Release |
|
||||
| :----: | ---- | :-----: |
|
||||
| :heavy_check_mark: | CH32V003F4P | 1.0.0 |
|
||||
|
||||
### CH32V20x EVT Boards
|
||||
|
||||
| Status | Boards name | Release |
|
||||
| :----: | ---- | :-----: |
|
||||
| :heavy_check_mark: | CH32V203G8U | 1.0.0 |
|
||||
|
||||
## Submit bugs
|
||||
|
||||
If you have any questions, you could contact me through the email "*temperslee001@gmail.com*".
|
||||
Or you could [file an issue on GitHub](https://github.com/openwch/arduino_core_ch32/issues/new).
|
||||
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
# See: https://arduino.github.io/arduino-cli/latest/platform-specification/
|
||||
|
||||
menu.pnum=Board Select
|
||||
|
||||
menu.xserial=U(S)ART support
|
||||
menu.usb=USB support (if available)
|
||||
menu.xusb=USB speed (if available)
|
||||
menu.virtio=Virtual serial support
|
||||
|
||||
menu.opt=Optimize
|
||||
menu.dbg=Debug symbols and core logs
|
||||
menu.rtlib=C Runtime Library
|
||||
menu.upload_method=Upload method
|
||||
|
||||
|
||||
|
||||
#############################################################################
|
||||
##CH32V00x EVT Board
|
||||
|
||||
CH32V00x_EVT.name=CH32V00x
|
||||
CH32V00x_EVT.build.core=arduino
|
||||
CH32V00x_EVT.build.board=CH32V00x_EVT
|
||||
CH32V00x_EVT.upload.maximum_size=0
|
||||
CH32V00x_EVT.upload.maximum_data_size=0
|
||||
CH32V00x_EVT.build.variant_h=variant_{build.board}.h
|
||||
|
||||
|
||||
#CH32V003F4 EVT Board
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4=CH32V003F4 EVT
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.node=NODE_V003F4
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.upload.maximum_size=16384
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.upload.maximum_data_size=2048
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.mcu=QingKe-V2
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.board=CH32V003F4
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.series=CH32V00x
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.variant=CH32V00x/CH32V003F4
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.chip=CH32V003F4
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.march=rv32ecxw
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.mabi=ilp32e
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.math_lib_gcc=-lm
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.IQ_math_RV32=
|
||||
CH32V00x_EVT.menu.pnum.CH32V003F4.build.ch_extra_lib=-lprintf
|
||||
|
||||
|
||||
# Upload menu
|
||||
CH32V00x_EVT.menu.upload_method.swdMethod=WCH-SWD
|
||||
CH32V00x_EVT.menu.upload_method.swdMethod.upload.protocol=
|
||||
CH32V00x_EVT.menu.upload_method.swdMethod.upload.options=
|
||||
CH32V00x_EVT.menu.upload_method.swdMethod.upload.tool=WCH_linkE
|
||||
|
||||
|
||||
|
||||
# Optimizations
|
||||
CH32V00x_EVT.menu.opt.osstd=Smallest (-Os default)
|
||||
CH32V00x_EVT.menu.opt.osstd.build.flags.optimize=-Os
|
||||
CH32V00x_EVT.menu.opt.oslto=Smallest (-Os) with LTO
|
||||
CH32V00x_EVT.menu.opt.oslto.build.flags.optimize=-Os -flto
|
||||
CH32V00x_EVT.menu.opt.o1std=Fast (-O1)
|
||||
CH32V00x_EVT.menu.opt.o1std.build.flags.optimize=-O1
|
||||
CH32V00x_EVT.menu.opt.o1lto=Fast (-O1) with LTO
|
||||
CH32V00x_EVT.menu.opt.o1lto.build.flags.optimize=-O1 -flto
|
||||
CH32V00x_EVT.menu.opt.o2std=Faster (-O2)
|
||||
CH32V00x_EVT.menu.opt.o2std.build.flags.optimize=-O2
|
||||
CH32V00x_EVT.menu.opt.o2lto=Faster (-O2) with LTO
|
||||
CH32V00x_EVT.menu.opt.o2lto.build.flags.optimize=-O2 -flto
|
||||
CH32V00x_EVT.menu.opt.o3std=Fastest (-O3)
|
||||
CH32V00x_EVT.menu.opt.o3std.build.flags.optimize=-O3
|
||||
CH32V00x_EVT.menu.opt.o3lto=Fastest (-O3) with LTO
|
||||
CH32V00x_EVT.menu.opt.o3lto.build.flags.optimize=-O3 -flto
|
||||
CH32V00x_EVT.menu.opt.ogstd=Debug (-Og)
|
||||
CH32V00x_EVT.menu.opt.ogstd.build.flags.optimize=-Og
|
||||
CH32V00x_EVT.menu.opt.o0std=No Optimization (-O0)
|
||||
CH32V00x_EVT.menu.opt.o0std.build.flags.optimize=-O0
|
||||
|
||||
|
||||
# Debug information
|
||||
CH32V00x_EVT.menu.dbg.none=None
|
||||
CH32V00x_EVT.menu.dbg.none.build.flags.debug=
|
||||
CH32V00x_EVT.menu.dbg.enable_sym=Symbols Enabled (-g)
|
||||
CH32V00x_EVT.menu.dbg.enable_sym.build.flags.debug=-g -DNDEBUG
|
||||
CH32V00x_EVT.menu.dbg.enable_log=Core logs Enabled
|
||||
CH32V00x_EVT.menu.dbg.enable_log.build.flags.debug=
|
||||
CH32V00x_EVT.menu.dbg.enable_all=Core Logs and Symbols Enabled (-g)
|
||||
CH32V00x_EVT.menu.dbg.enable_all.build.flags.debug=-g
|
||||
|
||||
|
||||
# C Runtime Library
|
||||
CH32V00x_EVT.menu.rtlib.nano=Newlib Nano (default)
|
||||
CH32V00x_EVT.menu.rtlib.nano.build.flags.ldflags=--specs=nano.specs --specs=nosys.specs
|
||||
CH32V00x_EVT.menu.rtlib.nanofp=Newlib Nano + Float Printf
|
||||
CH32V00x_EVT.menu.rtlib.nanofp.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _printf_float
|
||||
CH32V00x_EVT.menu.rtlib.nanofs=Newlib Nano + Float Scanf
|
||||
CH32V00x_EVT.menu.rtlib.nanofs.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _scanf_float
|
||||
CH32V00x_EVT.menu.rtlib.nanofps=Newlib Nano + Float Printf/Scanf
|
||||
CH32V00x_EVT.menu.rtlib.nanofps.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _printf_float -u _scanf_float
|
||||
CH32V00x_EVT.menu.rtlib.full=Newlib Standard
|
||||
CH32V00x_EVT.menu.rtlib.full.build.flags.ldspecs=
|
||||
|
||||
|
||||
|
||||
#############################################################################
|
||||
##CH32V20x EVT Board
|
||||
|
||||
CH32V20x_EVT.name=CH32V20x
|
||||
CH32V20x_EVT.build.core=arduino
|
||||
CH32V20x_EVT.build.board=CH32V20x_EVT
|
||||
CH32V20x_EVT.upload.maximum_size=0
|
||||
CH32V20x_EVT.upload.maximum_data_size=0
|
||||
CH32V20x_EVT.build.variant_h=variant_{build.board}.h
|
||||
|
||||
|
||||
#CH32V203G8 EVT Board
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8=CH32V203G8 EVT
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.node=NODE_V203G8
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.upload.maximum_size=65536
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.upload.maximum_data_size=20480
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.mcu=QingKe-V4C
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.board=CH32V203G8
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.series=CH32V20x
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.variant=CH32V20x/CH32V203G8
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.chip=CH32V203
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.march=rv32imacxw
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.mabi=ilp32
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.math_lib_gcc=-lm
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.IQ_math_RV32=
|
||||
CH32V20x_EVT.menu.pnum.CH32V203G8.build.ch_extra_lib=-lprintf
|
||||
|
||||
|
||||
# Upload menu
|
||||
CH32V20x_EVT.menu.upload_method.swdMethod=WCH-SWD
|
||||
CH32V20x_EVT.menu.upload_method.swdMethod.upload.protocol=
|
||||
CH32V20x_EVT.menu.upload_method.swdMethod.upload.options=
|
||||
CH32V20x_EVT.menu.upload_method.swdMethod.upload.tool=WCH_linkE
|
||||
|
||||
|
||||
|
||||
# Optimizations
|
||||
CH32V20x_EVT.menu.opt.osstd=Smallest (-Os default)
|
||||
CH32V20x_EVT.menu.opt.osstd.build.flags.optimize=-Os
|
||||
CH32V20x_EVT.menu.opt.oslto=Smallest (-Os) with LTO
|
||||
CH32V20x_EVT.menu.opt.oslto.build.flags.optimize=-Os -flto
|
||||
CH32V20x_EVT.menu.opt.o1std=Fast (-O1)
|
||||
CH32V20x_EVT.menu.opt.o1std.build.flags.optimize=-O1
|
||||
CH32V20x_EVT.menu.opt.o1lto=Fast (-O1) with LTO
|
||||
CH32V20x_EVT.menu.opt.o1lto.build.flags.optimize=-O1 -flto
|
||||
CH32V20x_EVT.menu.opt.o2std=Faster (-O2)
|
||||
CH32V20x_EVT.menu.opt.o2std.build.flags.optimize=-O2
|
||||
CH32V20x_EVT.menu.opt.o2lto=Faster (-O2) with LTO
|
||||
CH32V20x_EVT.menu.opt.o2lto.build.flags.optimize=-O2 -flto
|
||||
CH32V20x_EVT.menu.opt.o3std=Fastest (-O3)
|
||||
CH32V20x_EVT.menu.opt.o3std.build.flags.optimize=-O3
|
||||
CH32V20x_EVT.menu.opt.o3lto=Fastest (-O3) with LTO
|
||||
CH32V20x_EVT.menu.opt.o3lto.build.flags.optimize=-O3 -flto
|
||||
CH32V20x_EVT.menu.opt.ogstd=Debug (-Og)
|
||||
CH32V20x_EVT.menu.opt.ogstd.build.flags.optimize=-Og
|
||||
CH32V20x_EVT.menu.opt.o0std=No Optimization (-O0)
|
||||
CH32V20x_EVT.menu.opt.o0std.build.flags.optimize=-O0
|
||||
|
||||
|
||||
# Debug information
|
||||
CH32V20x_EVT.menu.dbg.none=None
|
||||
CH32V20x_EVT.menu.dbg.none.build.flags.debug=
|
||||
CH32V20x_EVT.menu.dbg.enable_sym=Symbols Enabled (-g)
|
||||
CH32V20x_EVT.menu.dbg.enable_sym.build.flags.debug=-g -DNDEBUG
|
||||
CH32V20x_EVT.menu.dbg.enable_log=Core logs Enabled
|
||||
CH32V20x_EVT.menu.dbg.enable_log.build.flags.debug=
|
||||
CH32V20x_EVT.menu.dbg.enable_all=Core Logs and Symbols Enabled (-g)
|
||||
CH32V20x_EVT.menu.dbg.enable_all.build.flags.debug=-g
|
||||
|
||||
|
||||
# C Runtime Library
|
||||
CH32V20x_EVT.menu.rtlib.nano=Newlib Nano (default)
|
||||
CH32V20x_EVT.menu.rtlib.nano.build.flags.ldflags=--specs=nano.specs --specs=nosys.specs
|
||||
CH32V20x_EVT.menu.rtlib.nanofp=Newlib Nano + Float Printf
|
||||
CH32V20x_EVT.menu.rtlib.nanofp.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _printf_float
|
||||
CH32V20x_EVT.menu.rtlib.nanofs=Newlib Nano + Float Scanf
|
||||
CH32V20x_EVT.menu.rtlib.nanofs.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _scanf_float
|
||||
CH32V20x_EVT.menu.rtlib.nanofps=Newlib Nano + Float Printf/Scanf
|
||||
CH32V20x_EVT.menu.rtlib.nanofps.build.flags.ldspecs=--specs=nano.specs --specs=nosys.specs -u _printf_float -u _scanf_float
|
||||
CH32V20x_EVT.menu.rtlib.full=Newlib Standard
|
||||
CH32V20x_EVT.menu.rtlib.full.build.flags.ldspecs=
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Arduino.h - Main include file for the Arduino SDK
|
||||
Copyright (c) 2005-2013 Arduino Team. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef Arduino_h
|
||||
#define Arduino_h
|
||||
|
||||
#ifndef GCC_VERSION
|
||||
#define GCC_VERSION (__GNUC__ * 10000 \
|
||||
+ __GNUC_MINOR__ * 100 \
|
||||
+ __GNUC_PATCHLEVEL__)
|
||||
#endif
|
||||
#if GCC_VERSION < 60300
|
||||
#error "GCC version 6.3 or higher is required"
|
||||
#endif
|
||||
|
||||
#ifdef __IN_ECLIPSE__
|
||||
// #include "SrcWrapper.h"
|
||||
#endif
|
||||
|
||||
#include "wiring.h"
|
||||
|
||||
/* sketch */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
extern void setup(void) ;
|
||||
extern void loop(void) ;
|
||||
|
||||
// void yield(void)
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
// Include pins variant
|
||||
#include "pins_arduino.h"
|
||||
|
||||
#endif // Arduino_h
|
|
@ -0,0 +1,293 @@
|
|||
/*
|
||||
HardwareSerial.cpp - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Modified 23 November 2006 by David A. Mellis
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "Arduino.h"
|
||||
#include "HardwareSerial.h"
|
||||
|
||||
#if defined(UART_MODULE_ENABLED) && !defined(UART_MODULE_ONLY)
|
||||
|
||||
|
||||
HardwareSerial::HardwareSerial(void *peripheral)
|
||||
{
|
||||
setHandler(peripheral);
|
||||
|
||||
setRx(PIN_SERIAL_RX);
|
||||
|
||||
setTx(PIN_SERIAL_TX);
|
||||
|
||||
init(_serial.pin_rx, _serial.pin_tx);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void HardwareSerial::init(PinName _rx, PinName _tx, PinName _rts, PinName _cts)
|
||||
{
|
||||
if (_rx == _tx) {
|
||||
_serial.pin_rx = NC;
|
||||
} else {
|
||||
_serial.pin_rx = _rx;
|
||||
}
|
||||
_serial.pin_tx = _tx;
|
||||
_serial.pin_rts = _rts;
|
||||
_serial.pin_cts = _cts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Public Methods //////////////////////////////////////////////////////////////
|
||||
void HardwareSerial::begin(unsigned long baud, byte config)
|
||||
{
|
||||
uint32_t databits = 0;
|
||||
uint32_t stopbits = 0;
|
||||
uint32_t parity = 0;
|
||||
|
||||
_baud = baud;
|
||||
_config = config;
|
||||
|
||||
// Manage databits
|
||||
switch (config & 0x03) {
|
||||
case 0x00:
|
||||
databits = 6;
|
||||
break;
|
||||
case 0x01:
|
||||
databits = 7;
|
||||
break;
|
||||
case 0x02:
|
||||
databits = 8;
|
||||
break;
|
||||
case 0x03:
|
||||
databits = 9;
|
||||
break;
|
||||
default:
|
||||
databits = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
if ((config & 0x30) == 0x30) {
|
||||
parity = USART_Parity_Odd;
|
||||
} else if ((config & 0x20) == 0x20) {
|
||||
parity = USART_Parity_Even;
|
||||
} else {
|
||||
parity = USART_Parity_No;
|
||||
}
|
||||
|
||||
|
||||
switch ( (config & 0x0C) >> 2 ) {
|
||||
case 0x00:
|
||||
stopbits = USART_StopBits_1;
|
||||
break;
|
||||
case 0x01:
|
||||
stopbits = USART_StopBits_0_5;
|
||||
break;
|
||||
case 0x02:
|
||||
stopbits = USART_StopBits_2;
|
||||
break;
|
||||
case 0x03:
|
||||
stopbits = USART_StopBits_1_5;
|
||||
break;
|
||||
default:
|
||||
stopbits = USART_StopBits_1;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (databits)
|
||||
{
|
||||
#ifdef USART_WordLength_6b
|
||||
case 6:
|
||||
databits = USART_WordLength_6b;
|
||||
break;
|
||||
#endif
|
||||
#ifdef USART_WordLength_7b
|
||||
case 7:
|
||||
databits = USART_WordLength_7b;
|
||||
break;
|
||||
#endif
|
||||
case 8:
|
||||
databits = USART_WordLength_8b;
|
||||
break;
|
||||
case 9:
|
||||
databits = USART_WordLength_9b;
|
||||
break;
|
||||
default:
|
||||
case 0:
|
||||
Error_Handler();
|
||||
break;
|
||||
}
|
||||
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits);
|
||||
}
|
||||
|
||||
void HardwareSerial::end()
|
||||
{
|
||||
uart_deinit(&_serial);
|
||||
}
|
||||
|
||||
int HardwareSerial::available(void)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int HardwareSerial::peek(void)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int HardwareSerial::read(void)
|
||||
{
|
||||
|
||||
unsigned char c;
|
||||
if(uart_getc(&_serial, &c) == 0){
|
||||
return c;
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
|
||||
return uart_debug_write((uint8_t *)buffer, size);
|
||||
}
|
||||
|
||||
|
||||
size_t HardwareSerial::write(uint8_t c)
|
||||
{
|
||||
uint8_t buff = c;
|
||||
return write(&buff, 1);
|
||||
}
|
||||
|
||||
void HardwareSerial::setRx(uint32_t _rx)
|
||||
{
|
||||
_serial.pin_rx = digitalPinToPinName(_rx);
|
||||
}
|
||||
|
||||
void HardwareSerial::setTx(uint32_t _tx)
|
||||
{
|
||||
_serial.pin_tx = digitalPinToPinName(_tx);
|
||||
}
|
||||
|
||||
void HardwareSerial::setRx(PinName _rx)
|
||||
{
|
||||
_serial.pin_rx = _rx;
|
||||
}
|
||||
|
||||
void HardwareSerial::setTx(PinName _tx)
|
||||
{
|
||||
_serial.pin_tx = _tx;
|
||||
}
|
||||
|
||||
void HardwareSerial::setRts(uint32_t _rts)
|
||||
{
|
||||
_serial.pin_rts = digitalPinToPinName(_rts);
|
||||
}
|
||||
|
||||
void HardwareSerial::setCts(uint32_t _cts)
|
||||
{
|
||||
_serial.pin_cts = digitalPinToPinName(_cts);
|
||||
}
|
||||
|
||||
void HardwareSerial::setRtsCts(uint32_t _rts, uint32_t _cts)
|
||||
{
|
||||
_serial.pin_rts = digitalPinToPinName(_rts);
|
||||
_serial.pin_cts = digitalPinToPinName(_cts);
|
||||
}
|
||||
|
||||
void HardwareSerial::setRts(PinName _rts)
|
||||
{
|
||||
_serial.pin_rts = _rts;
|
||||
}
|
||||
|
||||
void HardwareSerial::setCts(PinName _cts)
|
||||
{
|
||||
_serial.pin_cts = _cts;
|
||||
}
|
||||
|
||||
void HardwareSerial::setRtsCts(PinName _rts, PinName _cts)
|
||||
{
|
||||
_serial.pin_rts = _rts;
|
||||
_serial.pin_cts = _cts;
|
||||
}
|
||||
|
||||
void HardwareSerial::setHandler(void *handler)
|
||||
{
|
||||
_serial.uart = (USART_TypeDef *) handler;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\
|
||||
defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\
|
||||
defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8)
|
||||
// SerialEvent functions are weak, so when the user doesn't define them,
|
||||
// the linker just sets their address to 0 (which is checked below).
|
||||
#if defined(HAVE_HWSERIAL1)
|
||||
HardwareSerial Serial1(USART1);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL2)
|
||||
HardwareSerial Serial2(USART2);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL3)
|
||||
HardwareSerial Serial3(USART3);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL4)
|
||||
#if defined(USART4)
|
||||
HardwareSerial Serial4(USART4);
|
||||
#else
|
||||
HardwareSerial Serial4(UART4);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL5)
|
||||
#if defined(UART5)
|
||||
HardwareSerial Serial5(UART5);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL6)
|
||||
HardwareSerial Serial6(USART6);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL7)
|
||||
#if defined(UART7)
|
||||
HardwareSerial Serial7(UART7);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_HWSERIAL8)
|
||||
#if defined(UART8)
|
||||
HardwareSerial Serial8(UART8);
|
||||
#endif
|
||||
#endif
|
||||
#endif // HAVE_HWSERIALx
|
||||
|
||||
|
||||
|
||||
#endif // UART_MODULE_ENABLED && !UART_MODULE_ONLY
|
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
HardwareSerial.h - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
#ifndef HardwareSerial_h
|
||||
#define HardwareSerial_h
|
||||
|
||||
#if 1
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "Stream.h"
|
||||
#include "uart.h"
|
||||
|
||||
// Define constants and variables for buffering incoming serial data. We're
|
||||
// using a ring buffer (I think), in which head is the index of the location
|
||||
// to which to write the next incoming character and tail is the index of the
|
||||
// location from which to read.
|
||||
// NOTE: a "power of 2" buffer size is recommended to dramatically
|
||||
// optimize all the modulo operations for ring buffers.
|
||||
// WARNING: When buffer sizes are increased to > 256, the buffer index
|
||||
// variables are automatically increased in size, but the extra
|
||||
// atomicity guards needed for that are not implemented. This will
|
||||
// often work, but occasionally a race condition can occur that makes
|
||||
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
|
||||
#if !defined(SERIAL_TX_BUFFER_SIZE)
|
||||
#define SERIAL_TX_BUFFER_SIZE 64
|
||||
#endif
|
||||
#if !defined(SERIAL_RX_BUFFER_SIZE)
|
||||
#define SERIAL_RX_BUFFER_SIZE 64
|
||||
#endif
|
||||
#if (SERIAL_TX_BUFFER_SIZE>256)
|
||||
typedef uint16_t tx_buffer_index_t;
|
||||
#else
|
||||
typedef uint8_t tx_buffer_index_t;
|
||||
#endif
|
||||
#if (SERIAL_RX_BUFFER_SIZE>256)
|
||||
typedef uint16_t rx_buffer_index_t;
|
||||
#else
|
||||
typedef uint8_t rx_buffer_index_t;
|
||||
#endif
|
||||
|
||||
// A bool should be enough for this
|
||||
// But it brings an build error due to ambiguous
|
||||
// call of overloaded HardwareSerial(int, int)
|
||||
// So defining a dedicated type
|
||||
typedef enum {
|
||||
HALF_DUPLEX_DISABLED,
|
||||
HALF_DUPLEX_ENABLED
|
||||
} HalfDuplexMode_t;
|
||||
|
||||
// Define config for Serial.begin(baud, config);
|
||||
/* databits 8 */
|
||||
#define SERIAL_8N1 0x02
|
||||
#define SERIAL_8N2 0x0A
|
||||
#define SERIAL_8N0_5 0x06
|
||||
#define SERIAL_8N1_5 0x0E
|
||||
#define SERIAL_8E1 0x22
|
||||
#define SERIAL_8E2 0x2A
|
||||
#define SERIAL_8E0_5 0x26
|
||||
#define SERIAL_8E1_5 0x2E
|
||||
#define SERIAL_8O1 0x32
|
||||
#define SERIAL_8O2 0x3A
|
||||
#define SERIAL_8O0_5 0x36
|
||||
#define SERIAL_8O1_5 0x3E
|
||||
/* databits 9 */
|
||||
#define SERIAL_9N1 0x03
|
||||
#define SERIAL_9N2 0x0B
|
||||
#define SERIAL_9N0_5 0x07
|
||||
#define SERIAL_9N1_5 0x0F
|
||||
#define SERIAL_9E1 0x23
|
||||
#define SERIAL_9E2 0x2B
|
||||
#define SERIAL_9E0_5 0x27
|
||||
#define SERIAL_9E1_5 0x2F
|
||||
#define SERIAL_9O1 0x33
|
||||
#define SERIAL_9O2 0x3B
|
||||
#define SERIAL_9O0_5 0x37
|
||||
#define SERIAL_9O1_5 0x3F
|
||||
|
||||
|
||||
|
||||
|
||||
class HardwareSerial : public Stream {
|
||||
|
||||
serial_t _serial;
|
||||
public:
|
||||
HardwareSerial(void *peripheral);
|
||||
|
||||
void begin(unsigned long baud)
|
||||
{
|
||||
begin(baud, SERIAL_8N1); //SERIAL_9E1_5 SERIAL_8N1
|
||||
}
|
||||
void begin(unsigned long, uint8_t);
|
||||
void end();
|
||||
|
||||
virtual int available(void);
|
||||
virtual int peek(void);
|
||||
virtual int read(void);
|
||||
|
||||
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
inline size_t write(unsigned long n)
|
||||
{
|
||||
return write((uint8_t)n);
|
||||
}
|
||||
inline size_t write(long n)
|
||||
{
|
||||
return write((uint8_t)n);
|
||||
}
|
||||
inline size_t write(unsigned int n)
|
||||
{
|
||||
return write((uint8_t)n);
|
||||
}
|
||||
inline size_t write(int n)
|
||||
{
|
||||
return write((uint8_t)n);
|
||||
}
|
||||
size_t write(const uint8_t *buffer, size_t size);
|
||||
using Print::write; // pull in write(str) from Print
|
||||
operator bool()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void setRx(uint32_t _rx);
|
||||
void setTx(uint32_t _tx);
|
||||
void setRx(PinName _rx);
|
||||
void setTx(PinName _tx);
|
||||
|
||||
// Enable HW flow control on RTS, CTS or both
|
||||
void setRts(uint32_t _rts);
|
||||
void setCts(uint32_t _cts);
|
||||
void setRtsCts(uint32_t _rts, uint32_t _cts);
|
||||
void setRts(PinName _rts);
|
||||
void setCts(PinName _cts);
|
||||
void setRtsCts(PinName _rts, PinName _cts);
|
||||
void setHandler(void *handler);
|
||||
private:
|
||||
uint8_t _config;
|
||||
unsigned long _baud;
|
||||
void init(PinName _rx, PinName _tx, PinName _rts = NC, PinName _cts = NC);
|
||||
};
|
||||
|
||||
#if defined(USART1)
|
||||
extern HardwareSerial Serial1;
|
||||
#endif
|
||||
#if defined(USART2)
|
||||
extern HardwareSerial Serial2;
|
||||
#endif
|
||||
#if defined(USART3)
|
||||
extern HardwareSerial Serial3;
|
||||
#endif
|
||||
#if defined(UART4) || defined(USART4)
|
||||
extern HardwareSerial Serial4;
|
||||
#endif
|
||||
#if defined(UART5) || defined(USART5)
|
||||
extern HardwareSerial Serial5;
|
||||
#endif
|
||||
#if defined(USART6)
|
||||
extern HardwareSerial Serial6;
|
||||
#endif
|
||||
#if defined(UART7) || defined(USART7)
|
||||
extern HardwareSerial Serial7;
|
||||
#endif
|
||||
#if defined(UART8) || defined(USART8)
|
||||
extern HardwareSerial Serial8;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#else
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,453 @@
|
|||
/*
|
||||
Copyright (c) 2014 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <math.h>
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
|
||||
// Public Methods //////////////////////////////////////////////////////////////
|
||||
|
||||
/* default implementation: may be overridden */
|
||||
size_t Print::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
size_t n = 0;
|
||||
while (size--) {
|
||||
if (write(*buffer++)) {
|
||||
n++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::print(const __FlashStringHelper *ifsh)
|
||||
{
|
||||
return print(reinterpret_cast<const char *>(ifsh));
|
||||
}
|
||||
|
||||
size_t Print::print(const String &s)
|
||||
{
|
||||
return write(s.c_str(), s.length());
|
||||
}
|
||||
|
||||
size_t Print::print(const char str[])
|
||||
{
|
||||
return write(str);
|
||||
}
|
||||
|
||||
size_t Print::print(char c)
|
||||
{
|
||||
return write(c);
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned char b, int base)
|
||||
{
|
||||
return print((unsigned long) b, base);
|
||||
}
|
||||
|
||||
size_t Print::print(int n, int base)
|
||||
{
|
||||
return print((long) n, base);
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned int n, int base)
|
||||
{
|
||||
return print((unsigned long) n, base);
|
||||
}
|
||||
|
||||
size_t Print::print(long n, int base)
|
||||
{
|
||||
if (base == 0) {
|
||||
return write(n);
|
||||
} else if (base == 10) {
|
||||
if (n < 0) {
|
||||
int t = print('-');
|
||||
n = -n;
|
||||
return printNumber(n, 10) + t;
|
||||
}
|
||||
return printNumber(n, 10);
|
||||
} else {
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long n, int base)
|
||||
{
|
||||
if (base == 0) {
|
||||
return write(n);
|
||||
} else {
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(long long n, int base)
|
||||
{
|
||||
if (base == 0) {
|
||||
return write(n);
|
||||
} else if (base == 10) {
|
||||
if (n < 0) {
|
||||
int t = print('-');
|
||||
n = -n;
|
||||
return printULLNumber(n, 10) + t;
|
||||
}
|
||||
return printULLNumber(n, 10);
|
||||
} else {
|
||||
return printULLNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long long n, int base)
|
||||
{
|
||||
if (base == 0) {
|
||||
return write(n);
|
||||
} else {
|
||||
return printULLNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(double n, int digits)
|
||||
{
|
||||
return printFloat(n, digits);
|
||||
}
|
||||
|
||||
size_t Print::println(const __FlashStringHelper *ifsh)
|
||||
{
|
||||
size_t n = print(ifsh);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::print(const Printable &x)
|
||||
{
|
||||
return x.printTo(*this);
|
||||
}
|
||||
|
||||
size_t Print::println(void)
|
||||
{
|
||||
return write("\r\n");
|
||||
}
|
||||
|
||||
size_t Print::println(const String &s)
|
||||
{
|
||||
size_t n = print(s);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(const char c[])
|
||||
{
|
||||
size_t n = print(c);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(char c)
|
||||
{
|
||||
size_t n = print(c);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned char b, int base)
|
||||
{
|
||||
size_t n = print(b, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(int num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned int num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(long long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned long long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(double num, int digits)
|
||||
{
|
||||
size_t n = print(num, digits);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(const Printable &x)
|
||||
{
|
||||
size_t n = print(x);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
__attribute__((weak))
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
switch (file) {
|
||||
case STDOUT_FILENO:
|
||||
case STDERR_FILENO:
|
||||
#if defined(UART_MODULE_ENABLED) && !defined(UART_MODULE_ONLY)
|
||||
uart_debug_write((uint8_t *)ptr, (uint32_t)len);
|
||||
#endif
|
||||
break;
|
||||
case STDIN_FILENO:
|
||||
break;
|
||||
default:
|
||||
((class Print *)file)->write((uint8_t *)ptr, len);
|
||||
break;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
int Print::printf(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
int retval = vdprintf((int)this, format, ap);
|
||||
va_end(ap);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int Print::printf(const __FlashStringHelper *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
int retval = vdprintf((int)this, (const char *)format, ap);
|
||||
va_end(ap);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int Print::vprintf(const char *format, va_list ap)
|
||||
{
|
||||
return vdprintf((int)this, format, ap);
|
||||
}
|
||||
|
||||
int Print::vprintf(const __FlashStringHelper *format, va_list ap)
|
||||
{
|
||||
return vdprintf((int)this, (const char *)format, ap);
|
||||
}
|
||||
|
||||
|
||||
// Private Methods /////////////////////////////////////////////////////////////
|
||||
|
||||
size_t Print::printNumber(unsigned long n, uint8_t base)
|
||||
{
|
||||
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||
char *str = &buf[sizeof(buf) - 1];
|
||||
|
||||
*str = '\0';
|
||||
|
||||
// prevent crash if called with base == 1
|
||||
if (base < 2) {
|
||||
base = 10;
|
||||
}
|
||||
|
||||
do {
|
||||
unsigned long m = n;
|
||||
n /= base;
|
||||
char c = m - base * n;
|
||||
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
} while (n);
|
||||
|
||||
return write(str);
|
||||
}
|
||||
|
||||
/*
|
||||
void Print::printULLNumber(uint64_t n, uint8_t base)
|
||||
{
|
||||
unsigned char buf[16 * sizeof(long)];
|
||||
unsigned int i = 0;
|
||||
|
||||
if (n == 0) {
|
||||
print((char)'0');
|
||||
return;
|
||||
}
|
||||
|
||||
while (n > 0) {
|
||||
buf[i++] = n % base;
|
||||
n /= base;
|
||||
}
|
||||
|
||||
for (; i > 0; i--) {
|
||||
print((char)(buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
|
||||
}
|
||||
}
|
||||
*/
|
||||
// REFERENCE IMPLEMENTATION FOR ULL
|
||||
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
|
||||
// {
|
||||
// // if limited to base 10 and 16 the bufsize can be smaller
|
||||
// char buf[65];
|
||||
// char *str = &buf[64];
|
||||
|
||||
// *str = '\0';
|
||||
|
||||
// // prevent crash if called with base == 1
|
||||
// if (base < 2) base = 10;
|
||||
|
||||
// do {
|
||||
// unsigned long long t = n / base;
|
||||
// char c = n - t * base; // faster than c = n%base;
|
||||
// n = t;
|
||||
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
// } while(n);
|
||||
|
||||
// return write(str);
|
||||
// }
|
||||
|
||||
// FAST IMPLEMENTATION FOR ULL
|
||||
size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
|
||||
{
|
||||
// if limited to base 10 and 16 the bufsize can be 20
|
||||
char buf[64];
|
||||
uint8_t i = 0;
|
||||
uint8_t innerLoops = 0;
|
||||
|
||||
// prevent crash if called with base == 1
|
||||
if (base < 2) {
|
||||
base = 10;
|
||||
}
|
||||
|
||||
// process chunks that fit in "16 bit math".
|
||||
uint16_t top = 0xFFFF / base;
|
||||
uint16_t th16 = 1;
|
||||
while (th16 < top) {
|
||||
th16 *= base;
|
||||
innerLoops++;
|
||||
}
|
||||
|
||||
while (n64 > th16) {
|
||||
// 64 bit math part
|
||||
uint64_t q = n64 / th16;
|
||||
uint16_t r = n64 - q * th16;
|
||||
n64 = q;
|
||||
|
||||
// 16 bit math loop to do remainder. (note buffer is filled reverse)
|
||||
for (uint8_t j = 0; j < innerLoops; j++) {
|
||||
uint16_t qq = r / base;
|
||||
buf[i++] = r - qq * base;
|
||||
r = qq;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t n16 = n64;
|
||||
while (n16 > 0) {
|
||||
uint16_t qq = n16 / base;
|
||||
buf[i++] = n16 - qq * base;
|
||||
n16 = qq;
|
||||
}
|
||||
|
||||
size_t bytes = i;
|
||||
for (; i > 0; i--) {
|
||||
write((char)(buf[i - 1] < 10 ?
|
||||
'0' + buf[i - 1] :
|
||||
'A' + buf[i - 1] - 10));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
size_t Print::printFloat(double number, uint8_t digits)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
if (isnan(number)) {
|
||||
return print("nan");
|
||||
}
|
||||
if (isinf(number)) {
|
||||
return print("inf");
|
||||
}
|
||||
if (number > 4294967040.0) {
|
||||
return print("ovf"); // constant determined empirically
|
||||
}
|
||||
if (number < -4294967040.0) {
|
||||
return print("ovf"); // constant determined empirically
|
||||
}
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0.0) {
|
||||
n += print('-');
|
||||
number = -number;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for (uint8_t i = 0; i < digits; ++i) {
|
||||
rounding /= 10.0;
|
||||
}
|
||||
|
||||
number += rounding;
|
||||
|
||||
// Extract the integer part of the number and print it
|
||||
unsigned long int_part = (unsigned long)number;
|
||||
double remainder = number - (double)int_part;
|
||||
n += print(int_part);
|
||||
|
||||
// Print the decimal point, but only if there are digits beyond
|
||||
if (digits > 0) {
|
||||
n += print('.');
|
||||
}
|
||||
|
||||
// Extract digits from the remainder one at a time
|
||||
while (digits-- > 0) {
|
||||
remainder *= 10.0;
|
||||
unsigned int toPrint = (unsigned int)remainder;
|
||||
n += print(toPrint);
|
||||
remainder -= toPrint;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
Copyright (c) 2016 Arduino LLC. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef Print_h
|
||||
#define Print_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h> // for size_t
|
||||
#include <stdarg.h> // for printf
|
||||
|
||||
#include "WString.h"
|
||||
#include "Printable.h"
|
||||
|
||||
#define DEC 10
|
||||
#define HEX 16
|
||||
#define OCT 8
|
||||
#define BIN 2
|
||||
|
||||
class Print {
|
||||
private:
|
||||
int write_error;
|
||||
size_t printNumber(unsigned long, uint8_t);
|
||||
size_t printULLNumber(unsigned long long, uint8_t);
|
||||
size_t printFloat(double, uint8_t);
|
||||
protected:
|
||||
void setWriteError(int err = 1)
|
||||
{
|
||||
write_error = err;
|
||||
}
|
||||
public:
|
||||
Print() : write_error(0) {}
|
||||
|
||||
int getWriteError()
|
||||
{
|
||||
return write_error;
|
||||
}
|
||||
void clearWriteError()
|
||||
{
|
||||
setWriteError(0);
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
size_t write(const char *str)
|
||||
{
|
||||
if (str == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return write((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
size_t write(const char *buffer, size_t size)
|
||||
{
|
||||
return write((const uint8_t *)buffer, size);
|
||||
}
|
||||
|
||||
// default to zero, meaning "a single write may block"
|
||||
// should be overridden by subclasses with buffering
|
||||
virtual int availableForWrite()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t print(const __FlashStringHelper *);
|
||||
size_t print(const String &);
|
||||
size_t print(const char[]);
|
||||
size_t print(char);
|
||||
size_t print(unsigned char, int = DEC);
|
||||
size_t print(int, int = DEC);
|
||||
size_t print(unsigned int, int = DEC);
|
||||
size_t print(long, int = DEC);
|
||||
size_t print(unsigned long, int = DEC);
|
||||
size_t print(long long, int = DEC);
|
||||
size_t print(unsigned long long, int = DEC);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(const Printable &);
|
||||
|
||||
size_t println(const __FlashStringHelper *);
|
||||
size_t println(const String &s);
|
||||
size_t println(const char[]);
|
||||
size_t println(char);
|
||||
size_t println(unsigned char, int = DEC);
|
||||
size_t println(int, int = DEC);
|
||||
size_t println(unsigned int, int = DEC);
|
||||
size_t println(long, int = DEC);
|
||||
size_t println(unsigned long, int = DEC);
|
||||
size_t println(long long, int = DEC);
|
||||
size_t println(unsigned long long, int = DEC);
|
||||
size_t println(double, int = 2);
|
||||
size_t println(const Printable &);
|
||||
size_t println(void);
|
||||
|
||||
int printf(const char *format, ...);
|
||||
int printf(const __FlashStringHelper *format, ...);
|
||||
int vprintf(const __FlashStringHelper *format, va_list ap);
|
||||
int vprintf(const char *format, va_list ap);
|
||||
|
||||
virtual void flush() { /* Empty implementation for backward compatibility */ }
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
Printable.h - Interface class that allows printing of complex types
|
||||
Copyright (c) 2011 Adrian McEwen. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef Printable_h
|
||||
#define Printable_h
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
class Print;
|
||||
|
||||
/** The Printable class provides a way for new classes to allow themselves to be printed.
|
||||
By deriving from Printable and implementing the printTo method, it will then be possible
|
||||
for users to print out instances of this class by passing them into the usual
|
||||
Print::print and Print::println methods.
|
||||
*/
|
||||
|
||||
class Printable {
|
||||
public:
|
||||
virtual size_t printTo(Print &p) const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,341 @@
|
|||
/*
|
||||
Stream.cpp - adds parsing methods to Stream class
|
||||
Copyright (c) 2008 David A. Mellis. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Created July 2011
|
||||
parsing functions based on TextFinder library by Michael Margolis
|
||||
|
||||
findMulti/findUntil routines written by Jim Leonard/Xuth
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Stream.h"
|
||||
|
||||
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
|
||||
|
||||
// private method to read stream with timeout
|
||||
int Stream::timedRead()
|
||||
{
|
||||
int c;
|
||||
_startMillis = millis();
|
||||
do {
|
||||
c = read();
|
||||
if (c >= 0) {
|
||||
return c;
|
||||
}
|
||||
} while (millis() - _startMillis < _timeout);
|
||||
return -1; // -1 indicates timeout
|
||||
}
|
||||
|
||||
// private method to peek stream with timeout
|
||||
int Stream::timedPeek()
|
||||
{
|
||||
int c;
|
||||
_startMillis = millis();
|
||||
do {
|
||||
c = peek();
|
||||
if (c >= 0) {
|
||||
return c;
|
||||
}
|
||||
} while (millis() - _startMillis < _timeout);
|
||||
return -1; // -1 indicates timeout
|
||||
}
|
||||
|
||||
// returns peek of the next digit in the stream or -1 if timeout
|
||||
// discards non-numeric characters
|
||||
int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
|
||||
{
|
||||
int c;
|
||||
while (1) {
|
||||
c = timedPeek();
|
||||
|
||||
if (c < 0 ||
|
||||
c == '-' ||
|
||||
(c >= '0' && c <= '9') ||
|
||||
(detectDecimal && c == '.')) {
|
||||
return c;
|
||||
}
|
||||
|
||||
switch (lookahead) {
|
||||
case SKIP_NONE: return -1; // Fail code.
|
||||
case SKIP_WHITESPACE:
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n': break;
|
||||
default: return -1; // Fail code.
|
||||
}
|
||||
case SKIP_ALL:
|
||||
break;
|
||||
}
|
||||
read(); // discard non-numeric
|
||||
}
|
||||
}
|
||||
|
||||
// Public Methods
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
|
||||
{
|
||||
_timeout = timeout;
|
||||
}
|
||||
|
||||
// find returns true if the target string is found
|
||||
bool Stream::find(const char *target)
|
||||
{
|
||||
return findUntil(target, strlen(target), NULL, 0);
|
||||
}
|
||||
|
||||
// reads data from the stream until the target string of given length is found
|
||||
// returns true if target string is found, false if timed out
|
||||
bool Stream::find(const char *target, size_t length)
|
||||
{
|
||||
return findUntil(target, length, NULL, 0);
|
||||
}
|
||||
|
||||
// as find but search ends if the terminator string is found
|
||||
bool Stream::findUntil(const char *target, const char *terminator)
|
||||
{
|
||||
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
||||
}
|
||||
|
||||
// reads data from the stream until the target string of the given length is found
|
||||
// search terminated if the terminator string is found
|
||||
// returns true if target string is found, false if terminated or timed out
|
||||
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
|
||||
{
|
||||
if (terminator == NULL) {
|
||||
MultiTarget t[1] = {{target, targetLen, 0}};
|
||||
return findMulti(t, 1) == 0 ? true : false;
|
||||
} else {
|
||||
MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
|
||||
return findMulti(t, 2) == 0 ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
// returns the first valid (long) integer value from the current position.
|
||||
// lookahead determines how parseInt looks ahead in the stream.
|
||||
// See LookaheadMode enumeration at the top of the file.
|
||||
// Lookahead is terminated by the first character that is not a valid part of an integer.
|
||||
// Once parsing commences, 'ignore' will be skipped in the stream.
|
||||
long Stream::parseInt(LookaheadMode lookahead, char ignore)
|
||||
{
|
||||
bool isNegative = false;
|
||||
long value = 0;
|
||||
int c;
|
||||
|
||||
c = peekNextDigit(lookahead, false);
|
||||
// ignore non numeric leading characters
|
||||
if (c < 0) {
|
||||
return 0; // zero returned if timeout
|
||||
}
|
||||
|
||||
do {
|
||||
if (c == ignore)
|
||||
; // ignore this character
|
||||
else if (c == '-') {
|
||||
isNegative = true;
|
||||
} else if (c >= '0' && c <= '9') { // is c a digit?
|
||||
value = value * 10 + c - '0';
|
||||
}
|
||||
read(); // consume the character we got with peek
|
||||
c = timedPeek();
|
||||
} while ((c >= '0' && c <= '9') || c == ignore);
|
||||
|
||||
if (isNegative) {
|
||||
value = -value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// as parseInt but returns a floating point value
|
||||
float Stream::parseFloat(LookaheadMode lookahead, char ignore)
|
||||
{
|
||||
bool isNegative = false;
|
||||
bool isFraction = false;
|
||||
long value = 0;
|
||||
int c;
|
||||
float fraction = 1.0;
|
||||
|
||||
c = peekNextDigit(lookahead, true);
|
||||
// ignore non numeric leading characters
|
||||
if (c < 0) {
|
||||
return 0; // zero returned if timeout
|
||||
}
|
||||
|
||||
do {
|
||||
if (c == ignore)
|
||||
; // ignore
|
||||
else if (c == '-') {
|
||||
isNegative = true;
|
||||
} else if (c == '.') {
|
||||
isFraction = true;
|
||||
} else if (c >= '0' && c <= '9') { // is c a digit?
|
||||
value = value * 10 + c - '0';
|
||||
if (isFraction) {
|
||||
fraction *= 0.1;
|
||||
}
|
||||
}
|
||||
read(); // consume the character we got with peek
|
||||
c = timedPeek();
|
||||
} while ((c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore);
|
||||
|
||||
if (isNegative) {
|
||||
value = -value;
|
||||
}
|
||||
if (isFraction) {
|
||||
return value * fraction;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// read characters from stream into buffer
|
||||
// terminates if length characters have been read, or timeout (see setTimeout)
|
||||
// returns the number of characters placed in the buffer
|
||||
// the buffer is NOT null terminated.
|
||||
//
|
||||
size_t Stream::readBytes(char *buffer, size_t length)
|
||||
{
|
||||
size_t count = 0;
|
||||
while (count < length) {
|
||||
int c = timedRead();
|
||||
if (c < 0) {
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char)c;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
// as readBytes with terminator character
|
||||
// terminates if length characters have been read, timeout, or if the terminator character detected
|
||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||
|
||||
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
|
||||
{
|
||||
if (length < 1) {
|
||||
return 0;
|
||||
}
|
||||
size_t index = 0;
|
||||
while (index < length) {
|
||||
int c = timedRead();
|
||||
if (c < 0 || c == terminator) {
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char)c;
|
||||
index++;
|
||||
}
|
||||
return index; // return number of characters, not including null terminator
|
||||
}
|
||||
|
||||
String Stream::readString()
|
||||
{
|
||||
String ret;
|
||||
int c = timedRead();
|
||||
while (c >= 0) {
|
||||
ret += (char)c;
|
||||
c = timedRead();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
String Stream::readStringUntil(char terminator)
|
||||
{
|
||||
String ret;
|
||||
int c = timedRead();
|
||||
while (c >= 0 && c != terminator) {
|
||||
ret += (char)c;
|
||||
c = timedRead();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Stream::findMulti(struct Stream::MultiTarget *targets, int tCount)
|
||||
{
|
||||
// any zero length target string automatically matches and would make
|
||||
// a mess of the rest of the algorithm.
|
||||
for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
|
||||
if (t->len <= 0) {
|
||||
return t - targets;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int c = timedRead();
|
||||
if (c < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
|
||||
// the simple case is if we match, deal with that first.
|
||||
if (c == t->str[t->index]) {
|
||||
if (++t->index == t->len) {
|
||||
return t - targets;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// if not we need to walk back and see if we could have matched further
|
||||
// down the stream (ie '1112' doesn't match the first position in '11112'
|
||||
// but it will match the second position so we can't just reset the current
|
||||
// index to 0 when we find a mismatch.
|
||||
if (t->index == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int origIndex = t->index;
|
||||
do {
|
||||
--t->index;
|
||||
// first check if current char works against the new current index
|
||||
if (c != t->str[t->index]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if it's the only char then we're good, nothing more to check
|
||||
if (t->index == 0) {
|
||||
t->index++;
|
||||
break;
|
||||
}
|
||||
|
||||
// otherwise we need to check the rest of the found string
|
||||
int diff = origIndex - t->index;
|
||||
size_t i;
|
||||
for (i = 0; i < t->index; ++i) {
|
||||
if (t->str[i] != t->str[i + diff]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if we successfully got through the previous loop then our current
|
||||
// index is good.
|
||||
if (i == t->index) {
|
||||
t->index++;
|
||||
break;
|
||||
}
|
||||
|
||||
// otherwise we just try the next index
|
||||
} while (t->index);
|
||||
}
|
||||
}
|
||||
// unreachable
|
||||
return -1;
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
Stream.h - base class for character-based streams.
|
||||
Copyright (c) 2010 David A. Mellis. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
parsing functions based on TextFinder library by Michael Margolis
|
||||
*/
|
||||
|
||||
#ifndef Stream_h
|
||||
#define Stream_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "Print.h"
|
||||
|
||||
// compatibility macros for testing
|
||||
/*
|
||||
#define getInt() parseInt()
|
||||
#define getInt(ignore) parseInt(ignore)
|
||||
#define getFloat() parseFloat()
|
||||
#define getFloat(ignore) parseFloat(ignore)
|
||||
#define getString( pre_string, post_string, buffer, length)
|
||||
readBytesBetween( pre_string, terminator, buffer, length)
|
||||
*/
|
||||
|
||||
// This enumeration provides the lookahead options for parseInt(), parseFloat()
|
||||
// The rules set out here are used until either the first valid character is found
|
||||
// or a time out occurs due to lack of input.
|
||||
enum LookaheadMode {
|
||||
SKIP_ALL, // All invalid characters are ignored.
|
||||
SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
|
||||
SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
|
||||
};
|
||||
|
||||
#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
|
||||
|
||||
class Stream : public Print {
|
||||
protected:
|
||||
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
||||
unsigned long _startMillis; // used for timeout measurement
|
||||
int timedRead(); // private method to read stream with timeout
|
||||
int timedPeek(); // private method to peek stream with timeout
|
||||
int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
|
||||
|
||||
public:
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int peek() = 0;
|
||||
|
||||
Stream()
|
||||
{
|
||||
_timeout = 1000;
|
||||
}
|
||||
|
||||
// parsing methods
|
||||
|
||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||
unsigned long getTimeout(void)
|
||||
{
|
||||
return _timeout;
|
||||
}
|
||||
|
||||
bool find(const char *target); // reads data from the stream until the target string is found
|
||||
bool find(const uint8_t *target)
|
||||
{
|
||||
return find((const char *)target);
|
||||
}
|
||||
// returns true if target string is found, false if timed out (see setTimeout)
|
||||
|
||||
bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found
|
||||
bool find(const uint8_t *target, size_t length)
|
||||
{
|
||||
return find((const char *)target, length);
|
||||
}
|
||||
// returns true if target string is found, false if timed out
|
||||
|
||||
bool find(char target)
|
||||
{
|
||||
return find(&target, 1);
|
||||
}
|
||||
|
||||
bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
|
||||
bool findUntil(const uint8_t *target, const char *terminator)
|
||||
{
|
||||
return findUntil((const char *)target, terminator);
|
||||
}
|
||||
|
||||
bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
|
||||
bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
|
||||
{
|
||||
return findUntil((const char *)target, targetLen, terminate, termLen);
|
||||
}
|
||||
|
||||
long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
|
||||
// returns the first valid (long) integer value from the current position.
|
||||
// lookahead determines how parseInt looks ahead in the stream.
|
||||
// See LookaheadMode enumeration at the top of the file.
|
||||
// Lookahead is terminated by the first character that is not a valid part of an integer.
|
||||
// Once parsing commences, 'ignore' will be skipped in the stream.
|
||||
|
||||
float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
|
||||
// float version of parseInt
|
||||
|
||||
virtual size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer
|
||||
size_t readBytes(uint8_t *buffer, size_t length)
|
||||
{
|
||||
return readBytes((char *)buffer, length);
|
||||
}
|
||||
// terminates if length characters have been read or timeout (see setTimeout)
|
||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||
|
||||
virtual size_t readBytesUntil(char terminator, char *buffer, size_t length); // as readBytes with terminator character
|
||||
size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length)
|
||||
{
|
||||
return readBytesUntil(terminator, (char *)buffer, length);
|
||||
}
|
||||
// terminates if length characters have been read, timeout, or if the terminator character detected
|
||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||
|
||||
// Arduino String functions to be added here
|
||||
String readString();
|
||||
String readStringUntil(char terminator);
|
||||
|
||||
protected:
|
||||
long parseInt(char ignore)
|
||||
{
|
||||
return parseInt(SKIP_ALL, ignore);
|
||||
}
|
||||
float parseFloat(char ignore)
|
||||
{
|
||||
return parseFloat(SKIP_ALL, ignore);
|
||||
}
|
||||
// These overload exists for compatibility with any class that has derived
|
||||
// Stream and used parseFloat/Int with a custom ignore character. To keep
|
||||
// the public API simple, these overload remains protected.
|
||||
|
||||
struct MultiTarget {
|
||||
const char *str; // string you're searching for
|
||||
size_t len; // length of string you're searching for
|
||||
size_t index; // index used by the search routine.
|
||||
};
|
||||
|
||||
// This allows you to search for an arbitrary number of strings.
|
||||
// Returns index of the target that is found first or -1 if timeout occurs.
|
||||
int findMulti(struct MultiTarget *targets, int tCount);
|
||||
};
|
||||
|
||||
#undef NO_IGNORE_CHAR
|
||||
#endif
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Copyright (c) 2011-2012 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "WInterrupts.h"
|
||||
#include "Arduino.h"
|
||||
#include "PinAF_ch32yyxx.h"
|
||||
|
||||
#include "interrupt.h"
|
||||
|
||||
|
||||
#if !defined(EXTI_MODULE_DISABLED)
|
||||
|
||||
void attachInterrupt(uint32_t pin, GPIOMode_TypeDef io_mode, void (*callback)(void), EXTIMode_TypeDef it_mode, EXTITrigger_TypeDef trigger_mode)
|
||||
{
|
||||
PinName p = digitalPinToPinName(pin);
|
||||
GPIO_TypeDef* port = set_GPIO_Port_Clock(CH_PORT(p));
|
||||
if (!port) return ;
|
||||
pinV32_DisconnectDebug(p);
|
||||
|
||||
ch32_interrupt_enable(port, io_mode, CH_GPIO_PIN(p), callback, it_mode, trigger_mode);
|
||||
}
|
||||
|
||||
|
||||
void detachInterrupt(uint32_t pin)
|
||||
{
|
||||
PinName p = digitalPinToPinName(pin);
|
||||
GPIO_TypeDef* port = get_GPIO_Port(CH_PORT(p));
|
||||
if (!port)
|
||||
return;
|
||||
ch32_interrupt_disable(port, CH_GPIO_PIN(p));
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
Copyright (c) 2011-2012 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _WIRING_INTERRUPTS_
|
||||
#define _WIRING_INTERRUPTS_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "interrupt.h"
|
||||
|
||||
|
||||
|
||||
void attachInterrupt(uint32_t pin,GPIOMode_TypeDef io_mode, void (*callback)(void), EXTIMode_TypeDef it_mode, EXTITrigger_TypeDef trigger_mode);
|
||||
void detachInterrupt(uint32_t pin);
|
||||
|
||||
#endif /* _WIRING_INTERRUPTS_ */
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Copyright (c) 2011 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
extern "C" {
|
||||
#include "stdlib.h"
|
||||
#include "stdint.h"
|
||||
}
|
||||
#include "WMath.h"
|
||||
|
||||
extern void randomSeed(uint32_t dwSeed)
|
||||
{
|
||||
if (dwSeed != 0) {
|
||||
srand(dwSeed) ;
|
||||
}
|
||||
}
|
||||
|
||||
extern long random(long howbig)
|
||||
{
|
||||
if (howbig == 0) {
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
return rand() % howbig;
|
||||
}
|
||||
|
||||
extern long random(long howsmall, long howbig)
|
||||
{
|
||||
if (howsmall >= howbig) {
|
||||
return howsmall;
|
||||
}
|
||||
|
||||
long diff = howbig - howsmall;
|
||||
|
||||
return random(diff) + howsmall;
|
||||
}
|
||||
|
||||
extern long map(long x, long in_min, long in_max, long out_min, long out_max)
|
||||
{
|
||||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
}
|
||||
|
||||
extern uint16_t makeWord(uint16_t w)
|
||||
{
|
||||
return w ;
|
||||
}
|
||||
|
||||
extern uint16_t makeWord(uint8_t h, uint8_t l)
|
||||
{
|
||||
return (h << 8) | l ;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
Copyright (c) 2011 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _WIRING_MATH_
|
||||
#define _WIRING_MATH_
|
||||
|
||||
extern long random(long) ;
|
||||
extern long random(long, long) ;
|
||||
extern void randomSeed(uint32_t dwSeed) ;
|
||||
extern long map(long, long, long, long, long) ;
|
||||
|
||||
extern uint16_t makeWord(uint16_t w) ;
|
||||
extern uint16_t makeWord(uint8_t h, uint8_t l) ;
|
||||
|
||||
#define word(...) makeWord(__VA_ARGS__)
|
||||
|
||||
|
||||
#endif /* _WIRING_MATH_ */
|
|
@ -0,0 +1,113 @@
|
|||
#ifndef WIRING_SERIAL_H
|
||||
#define WIRING_SERIAL_H
|
||||
|
||||
#include "variant.h"
|
||||
#include "HardwareSerial.h"
|
||||
|
||||
|
||||
#if defined(UART_MODULE_ENABLED) && !defined(UART_MODULE_ONLY)
|
||||
|
||||
#if !defined(HWSERIAL_NONE) && defined(SERIAL_UART_INSTANCE)
|
||||
|
||||
#if SERIAL_UART_INSTANCE == 1
|
||||
#define ENABLE_HWSERIAL1
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial1
|
||||
// #define serialEvent serialEvent1 //reserved
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 2
|
||||
#define ENABLE_HWSERIAL2
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial2
|
||||
// #define serialEvent serialEvent2
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 3
|
||||
#define ENABLE_HWSERIAL3
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial3
|
||||
// #define serialEvent serialEvent3
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 4
|
||||
#define ENABLE_HWSERIAL4
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial4
|
||||
// #define serialEvent serialEvent4
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 5
|
||||
#define ENABLE_HWSERIAL5
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial5
|
||||
// #define serialEvent serialEvent5
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 6
|
||||
#define ENABLE_HWSERIAL6
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial6
|
||||
// #define serialEvent serialEvent6
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 7
|
||||
#define ENABLE_HWSERIAL7
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial7
|
||||
// #define serialEvent serialEvent7
|
||||
#endif
|
||||
#elif SERIAL_UART_INSTANCE == 8
|
||||
#define ENABLE_HWSERIAL8
|
||||
#if !defined(Serial)
|
||||
#define Serial Serial8
|
||||
// #define serialEvent serialEvent8
|
||||
#endif
|
||||
#else
|
||||
#if !defined(Serial)
|
||||
#warning "No generic 'Serial' defined!"
|
||||
#endif
|
||||
|
||||
#endif /* SERIAL_UART_INSTANCE == x */
|
||||
|
||||
#endif /* !HWSERIAL_NONE && SERIAL_UART_INSTANCE */
|
||||
|
||||
|
||||
#if defined(ENABLE_HWSERIAL1)
|
||||
#if defined(USART1_BASE)
|
||||
#define HAVE_HWSERIAL1
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL2)
|
||||
#if defined(USART2_BASE)
|
||||
#define HAVE_HWSERIAL2
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL3)
|
||||
#if defined(USART3_BASE)
|
||||
#define HAVE_HWSERIAL3
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL4)
|
||||
#if defined(USART4_BASE) || defined(UART4_BASE)
|
||||
#define HAVE_HWSERIAL4
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL5)
|
||||
#if defined(UART5_BASE)
|
||||
#define HAVE_HWSERIAL5
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL6)
|
||||
#if defined(UART6_BASE)
|
||||
#define HAVE_HWSERIAL6
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL7)
|
||||
#if defined(UART7_BASE)
|
||||
#define HAVE_HWSERIAL7
|
||||
#endif
|
||||
#endif
|
||||
#if defined(ENABLE_HWSERIAL8)
|
||||
#if defined(UART8_BASE)
|
||||
#define HAVE_HWSERIAL8
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* WIRING_SERIAL_H */
|
|
@ -0,0 +1,899 @@
|
|||
/*
|
||||
WString.cpp - String library for Wiring & Arduino
|
||||
...mostly rewritten by Paul Stoffregen...
|
||||
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||
|
||||
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "WString.h"
|
||||
#include "itoa.h"
|
||||
#include "avr/dtostrf.h"
|
||||
|
||||
/*********************************************/
|
||||
/* Constructors */
|
||||
/*********************************************/
|
||||
|
||||
String::String(const char *cstr)
|
||||
{
|
||||
init();
|
||||
if (cstr) {
|
||||
copy(cstr, strlen(cstr));
|
||||
}
|
||||
}
|
||||
|
||||
String::String(const String &value)
|
||||
{
|
||||
init();
|
||||
*this = value;
|
||||
}
|
||||
|
||||
String::String(const __FlashStringHelper *pstr)
|
||||
{
|
||||
init();
|
||||
*this = pstr;
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
String::String(String &&rval)
|
||||
{
|
||||
init();
|
||||
move(rval);
|
||||
}
|
||||
String::String(StringSumHelper &&rval)
|
||||
{
|
||||
init();
|
||||
move(rval);
|
||||
}
|
||||
#endif
|
||||
|
||||
String::String(char c)
|
||||
{
|
||||
init();
|
||||
char buf[2];
|
||||
buf[0] = c;
|
||||
buf[1] = 0;
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(unsigned char value, unsigned char base)
|
||||
{
|
||||
init();
|
||||
char buf[1 + 8 * sizeof(unsigned char)];
|
||||
utoa(value, buf, base);
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(int value, unsigned char base)
|
||||
{
|
||||
init();
|
||||
char buf[2 + 8 * sizeof(int)];
|
||||
itoa(value, buf, base);
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(unsigned int value, unsigned char base)
|
||||
{
|
||||
init();
|
||||
char buf[1 + 8 * sizeof(unsigned int)];
|
||||
utoa(value, buf, base);
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(long value, unsigned char base)
|
||||
{
|
||||
init();
|
||||
char buf[2 + 8 * sizeof(long)];
|
||||
ltoa(value, buf, base);
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(unsigned long value, unsigned char base)
|
||||
{
|
||||
init();
|
||||
char buf[1 + 8 * sizeof(unsigned long)];
|
||||
ultoa(value, buf, base);
|
||||
*this = buf;
|
||||
}
|
||||
|
||||
String::String(float value, unsigned char decimalPlaces)
|
||||
{
|
||||
init();
|
||||
char buf[33];
|
||||
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
||||
}
|
||||
|
||||
String::String(double value, unsigned char decimalPlaces)
|
||||
{
|
||||
init();
|
||||
char buf[33];
|
||||
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
||||
}
|
||||
|
||||
String::~String()
|
||||
{
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Memory Management */
|
||||
/*********************************************/
|
||||
|
||||
inline void String::init(void)
|
||||
{
|
||||
buffer = NULL;
|
||||
capacity = 0;
|
||||
len = 0;
|
||||
}
|
||||
|
||||
void String::invalidate(void)
|
||||
{
|
||||
if (buffer) {
|
||||
free(buffer);
|
||||
}
|
||||
buffer = NULL;
|
||||
capacity = len = 0;
|
||||
}
|
||||
|
||||
unsigned char String::reserve(unsigned int size)
|
||||
{
|
||||
if (buffer && capacity >= size) {
|
||||
return 1;
|
||||
}
|
||||
if (changeBuffer(size)) {
|
||||
if (len == 0) {
|
||||
buffer[0] = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char String::changeBuffer(unsigned int maxStrLen)
|
||||
{
|
||||
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
|
||||
if (newbuffer) {
|
||||
buffer = newbuffer;
|
||||
capacity = maxStrLen;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Copy and Move */
|
||||
/*********************************************/
|
||||
|
||||
String &String::copy(const char *cstr, unsigned int length)
|
||||
{
|
||||
if (!reserve(length)) {
|
||||
invalidate();
|
||||
return *this;
|
||||
}
|
||||
len = length;
|
||||
strcpy(buffer, cstr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String &String::copy(const __FlashStringHelper *pstr, unsigned int length)
|
||||
{
|
||||
if (!reserve(length)) {
|
||||
invalidate();
|
||||
return *this;
|
||||
}
|
||||
len = length;
|
||||
strcpy_P(buffer, (PGM_P)pstr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
void String::move(String &rhs)
|
||||
{
|
||||
if (buffer) {
|
||||
if (rhs && capacity >= rhs.len) {
|
||||
strcpy(buffer, rhs.buffer);
|
||||
len = rhs.len;
|
||||
rhs.len = 0;
|
||||
return;
|
||||
} else {
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
buffer = rhs.buffer;
|
||||
capacity = rhs.capacity;
|
||||
len = rhs.len;
|
||||
rhs.buffer = NULL;
|
||||
rhs.capacity = 0;
|
||||
rhs.len = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
String &String::operator = (const String &rhs)
|
||||
{
|
||||
if (this == &rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
if (rhs.buffer) {
|
||||
copy(rhs.buffer, rhs.len);
|
||||
} else {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
String &String::operator = (String &&rval)
|
||||
{
|
||||
if (this != &rval) {
|
||||
move(rval);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
String &String::operator = (StringSumHelper &&rval)
|
||||
{
|
||||
if (this != &rval) {
|
||||
move(rval);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
String &String::operator = (const char *cstr)
|
||||
{
|
||||
if (cstr) {
|
||||
copy(cstr, strlen(cstr));
|
||||
} else {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
String &String::operator = (const __FlashStringHelper *pstr)
|
||||
{
|
||||
if (pstr) {
|
||||
copy(pstr, strlen_P((PGM_P)pstr));
|
||||
} else {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* concat */
|
||||
/*********************************************/
|
||||
|
||||
unsigned char String::concat(const String &s)
|
||||
{
|
||||
return concat(s.buffer, s.len);
|
||||
}
|
||||
|
||||
unsigned char String::concat(const char *cstr, unsigned int length)
|
||||
{
|
||||
unsigned int newlen = len + length;
|
||||
if (!cstr) {
|
||||
return 0;
|
||||
}
|
||||
if (length == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (!reserve(newlen)) {
|
||||
return 0;
|
||||
}
|
||||
strcpy(buffer + len, cstr);
|
||||
len = newlen;
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char String::concat(const char *cstr)
|
||||
{
|
||||
if (!cstr) {
|
||||
return 0;
|
||||
}
|
||||
return concat(cstr, strlen(cstr));
|
||||
}
|
||||
|
||||
unsigned char String::concat(char c)
|
||||
{
|
||||
char buf[2];
|
||||
buf[0] = c;
|
||||
buf[1] = 0;
|
||||
return concat(buf, 1);
|
||||
}
|
||||
|
||||
unsigned char String::concat(unsigned char num)
|
||||
{
|
||||
char buf[1 + 3 * sizeof(unsigned char)];
|
||||
itoa(num, buf, 10);
|
||||
return concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
unsigned char String::concat(int num)
|
||||
{
|
||||
char buf[2 + 3 * sizeof(int)];
|
||||
itoa(num, buf, 10);
|
||||
return concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
unsigned char String::concat(unsigned int num)
|
||||
{
|
||||
char buf[1 + 3 * sizeof(unsigned int)];
|
||||
utoa(num, buf, 10);
|
||||
return concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
unsigned char String::concat(long num)
|
||||
{
|
||||
char buf[2 + 3 * sizeof(long)];
|
||||
ltoa(num, buf, 10);
|
||||
return concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
unsigned char String::concat(unsigned long num)
|
||||
{
|
||||
char buf[1 + 3 * sizeof(unsigned long)];
|
||||
ultoa(num, buf, 10);
|
||||
return concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
unsigned char String::concat(float num)
|
||||
{
|
||||
char buf[20];
|
||||
char *string = dtostrf(num, 4, 2, buf);
|
||||
return concat(string, strlen(string));
|
||||
}
|
||||
|
||||
unsigned char String::concat(double num)
|
||||
{
|
||||
char buf[20];
|
||||
char *string = dtostrf(num, 4, 2, buf);
|
||||
return concat(string, strlen(string));
|
||||
}
|
||||
|
||||
unsigned char String::concat(const __FlashStringHelper *str)
|
||||
{
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
int length = strlen_P((const char *) str);
|
||||
if (length == 0) {
|
||||
return 1;
|
||||
}
|
||||
unsigned int newlen = len + length;
|
||||
if (!reserve(newlen)) {
|
||||
return 0;
|
||||
}
|
||||
strcpy_P(buffer + len, (const char *) str);
|
||||
len = newlen;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Concatenate */
|
||||
/*********************************************/
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, const String &rhs)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(rhs.buffer, rhs.len)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, const char *cstr)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!cstr || !a.concat(cstr, strlen(cstr))) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, char c)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(c)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, unsigned char num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, int num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, unsigned int num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, long num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, unsigned long num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, float num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, double num)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(num)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
StringSumHelper &operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
|
||||
{
|
||||
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
|
||||
if (!a.concat(rhs)) {
|
||||
a.invalidate();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Comparison */
|
||||
/*********************************************/
|
||||
|
||||
int String::compareTo(const String &s) const
|
||||
{
|
||||
if (!buffer || !s.buffer) {
|
||||
if (s.buffer && s.len > 0) {
|
||||
return 0 - *(unsigned char *)s.buffer;
|
||||
}
|
||||
if (buffer && len > 0) {
|
||||
return *(unsigned char *)buffer;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return strcmp(buffer, s.buffer);
|
||||
}
|
||||
|
||||
unsigned char String::equals(const String &s2) const
|
||||
{
|
||||
return (len == s2.len && compareTo(s2) == 0);
|
||||
}
|
||||
|
||||
unsigned char String::equals(const char *cstr) const
|
||||
{
|
||||
if (len == 0) {
|
||||
return (cstr == NULL || *cstr == 0);
|
||||
}
|
||||
if (cstr == NULL) {
|
||||
return buffer[0] == 0;
|
||||
}
|
||||
return strcmp(buffer, cstr) == 0;
|
||||
}
|
||||
|
||||
unsigned char String::operator<(const String &rhs) const
|
||||
{
|
||||
return compareTo(rhs) < 0;
|
||||
}
|
||||
|
||||
unsigned char String::operator>(const String &rhs) const
|
||||
{
|
||||
return compareTo(rhs) > 0;
|
||||
}
|
||||
|
||||
unsigned char String::operator<=(const String &rhs) const
|
||||
{
|
||||
return compareTo(rhs) <= 0;
|
||||
}
|
||||
|
||||
unsigned char String::operator>=(const String &rhs) const
|
||||
{
|
||||
return compareTo(rhs) >= 0;
|
||||
}
|
||||
|
||||
unsigned char String::equalsIgnoreCase(const String &s2) const
|
||||
{
|
||||
if (this == &s2) {
|
||||
return 1;
|
||||
}
|
||||
if (len != s2.len) {
|
||||
return 0;
|
||||
}
|
||||
if (len == 0) {
|
||||
return 1;
|
||||
}
|
||||
const char *p1 = buffer;
|
||||
const char *p2 = s2.buffer;
|
||||
while (*p1) {
|
||||
if (tolower(*p1++) != tolower(*p2++)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char String::startsWith(const String &s2) const
|
||||
{
|
||||
if (len < s2.len) {
|
||||
return 0;
|
||||
}
|
||||
return startsWith(s2, 0);
|
||||
}
|
||||
|
||||
unsigned char String::startsWith(const String &s2, unsigned int offset) const
|
||||
{
|
||||
if (offset > len - s2.len || !buffer || !s2.buffer) {
|
||||
return 0;
|
||||
}
|
||||
return strncmp(&buffer[offset], s2.buffer, s2.len) == 0;
|
||||
}
|
||||
|
||||
unsigned char String::endsWith(const String &s2) const
|
||||
{
|
||||
if (len < s2.len || !buffer || !s2.buffer) {
|
||||
return 0;
|
||||
}
|
||||
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Character Access */
|
||||
/*********************************************/
|
||||
|
||||
char String::charAt(unsigned int loc) const
|
||||
{
|
||||
return operator[](loc);
|
||||
}
|
||||
|
||||
void String::setCharAt(unsigned int loc, char c)
|
||||
{
|
||||
if (loc < len) {
|
||||
buffer[loc] = c;
|
||||
}
|
||||
}
|
||||
|
||||
char &String::operator[](unsigned int index)
|
||||
{
|
||||
static char dummy_writable_char;
|
||||
if (index >= len || !buffer) {
|
||||
dummy_writable_char = 0;
|
||||
return dummy_writable_char;
|
||||
}
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
char String::operator[](unsigned int index) const
|
||||
{
|
||||
if (index >= len || !buffer) {
|
||||
return 0;
|
||||
}
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
|
||||
{
|
||||
if (!bufsize || !buf) {
|
||||
return;
|
||||
}
|
||||
if (index >= len) {
|
||||
buf[0] = 0;
|
||||
return;
|
||||
}
|
||||
unsigned int n = bufsize - 1;
|
||||
if (n > len - index) {
|
||||
n = len - index;
|
||||
}
|
||||
strncpy((char *)buf, buffer + index, n);
|
||||
buf[n] = 0;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Search */
|
||||
/*********************************************/
|
||||
|
||||
int String::indexOf(char c) const
|
||||
{
|
||||
return indexOf(c, 0);
|
||||
}
|
||||
|
||||
int String::indexOf(char ch, unsigned int fromIndex) const
|
||||
{
|
||||
if (fromIndex >= len) {
|
||||
return -1;
|
||||
}
|
||||
const char *temp = strchr(buffer + fromIndex, ch);
|
||||
if (temp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return temp - buffer;
|
||||
}
|
||||
|
||||
int String::indexOf(const String &s2) const
|
||||
{
|
||||
return indexOf(s2, 0);
|
||||
}
|
||||
|
||||
int String::indexOf(const String &s2, unsigned int fromIndex) const
|
||||
{
|
||||
if (fromIndex >= len) {
|
||||
return -1;
|
||||
}
|
||||
const char *found = strstr(buffer + fromIndex, s2.buffer);
|
||||
if (found == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return found - buffer;
|
||||
}
|
||||
|
||||
int String::lastIndexOf(char theChar) const
|
||||
{
|
||||
return lastIndexOf(theChar, len - 1);
|
||||
}
|
||||
|
||||
int String::lastIndexOf(char ch, unsigned int fromIndex) const
|
||||
{
|
||||
if (fromIndex >= len) {
|
||||
return -1;
|
||||
}
|
||||
char tempchar = buffer[fromIndex + 1];
|
||||
buffer[fromIndex + 1] = '\0';
|
||||
char *temp = strrchr(buffer, ch);
|
||||
buffer[fromIndex + 1] = tempchar;
|
||||
if (temp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return temp - buffer;
|
||||
}
|
||||
|
||||
int String::lastIndexOf(const String &s2) const
|
||||
{
|
||||
return lastIndexOf(s2, len - s2.len);
|
||||
}
|
||||
|
||||
int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
|
||||
{
|
||||
if (s2.len == 0 || len == 0 || s2.len > len) {
|
||||
return -1;
|
||||
}
|
||||
if (fromIndex >= len) {
|
||||
fromIndex = len - 1;
|
||||
}
|
||||
int found = -1;
|
||||
for (char *p = buffer; p <= buffer + fromIndex; p++) {
|
||||
p = strstr(p, s2.buffer);
|
||||
if (!p) {
|
||||
break;
|
||||
}
|
||||
if ((unsigned int)(p - buffer) <= fromIndex) {
|
||||
found = p - buffer;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
String String::substring(unsigned int left, unsigned int right) const
|
||||
{
|
||||
if (left > right) {
|
||||
unsigned int temp = right;
|
||||
right = left;
|
||||
left = temp;
|
||||
}
|
||||
String out;
|
||||
if (left >= len) {
|
||||
return out;
|
||||
}
|
||||
if (right > len) {
|
||||
right = len;
|
||||
}
|
||||
char temp = buffer[right]; // save the replaced character
|
||||
buffer[right] = '\0';
|
||||
out = buffer + left; // pointer arithmetic
|
||||
buffer[right] = temp; //restore character
|
||||
return out;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Modification */
|
||||
/*********************************************/
|
||||
|
||||
void String::replace(char find, char replace)
|
||||
{
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
for (char *p = buffer; *p; p++) {
|
||||
if (*p == find) {
|
||||
*p = replace;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void String::replace(const String &find, const String &replace)
|
||||
{
|
||||
if (len == 0 || find.len == 0) {
|
||||
return;
|
||||
}
|
||||
int diff = replace.len - find.len;
|
||||
char *readFrom = buffer;
|
||||
char *foundAt;
|
||||
if (diff == 0) {
|
||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||
memcpy(foundAt, replace.buffer, replace.len);
|
||||
readFrom = foundAt + replace.len;
|
||||
}
|
||||
} else if (diff < 0) {
|
||||
char *writeTo = buffer;
|
||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||
unsigned int n = foundAt - readFrom;
|
||||
memcpy(writeTo, readFrom, n);
|
||||
writeTo += n;
|
||||
memcpy(writeTo, replace.buffer, replace.len);
|
||||
writeTo += replace.len;
|
||||
readFrom = foundAt + find.len;
|
||||
len += diff;
|
||||
}
|
||||
strcpy(writeTo, readFrom);
|
||||
} else {
|
||||
unsigned int size = len; // compute size needed for result
|
||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||
readFrom = foundAt + find.len;
|
||||
size += diff;
|
||||
}
|
||||
if (size == len) {
|
||||
return;
|
||||
}
|
||||
if (size > capacity && !changeBuffer(size)) {
|
||||
return; // XXX: tell user!
|
||||
}
|
||||
int index = len - 1;
|
||||
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
|
||||
readFrom = buffer + index + find.len;
|
||||
memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
|
||||
len += diff;
|
||||
buffer[len] = 0;
|
||||
memcpy(buffer + index, replace.buffer, replace.len);
|
||||
index--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void String::remove(unsigned int index)
|
||||
{
|
||||
// Pass the biggest integer as the count. The remove method
|
||||
// below will take care of truncating it at the end of the
|
||||
// string.
|
||||
remove(index, (unsigned int) -1);
|
||||
}
|
||||
|
||||
void String::remove(unsigned int index, unsigned int count)
|
||||
{
|
||||
if (index >= len) {
|
||||
return;
|
||||
}
|
||||
if (count <= 0) {
|
||||
return;
|
||||
}
|
||||
if (count > len - index) {
|
||||
count = len - index;
|
||||
}
|
||||
char *writeTo = buffer + index;
|
||||
len = len - count;
|
||||
memmove(writeTo, buffer + index + count, len - index);
|
||||
buffer[len] = 0;
|
||||
}
|
||||
|
||||
void String::toLowerCase(void)
|
||||
{
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
for (char *p = buffer; *p; p++) {
|
||||
*p = tolower(*p);
|
||||
}
|
||||
}
|
||||
|
||||
void String::toUpperCase(void)
|
||||
{
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
for (char *p = buffer; *p; p++) {
|
||||
*p = toupper(*p);
|
||||
}
|
||||
}
|
||||
|
||||
void String::trim(void)
|
||||
{
|
||||
if (!buffer || len == 0) {
|
||||
return;
|
||||
}
|
||||
char *begin = buffer;
|
||||
while (isspace(*begin)) {
|
||||
begin++;
|
||||
}
|
||||
char *end = buffer + len - 1;
|
||||
while (isspace(*end) && end >= begin) {
|
||||
end--;
|
||||
}
|
||||
len = end + 1 - begin;
|
||||
if (begin > buffer) {
|
||||
memcpy(buffer, begin, len);
|
||||
}
|
||||
buffer[len] = 0;
|
||||
}
|
||||
|
||||
/*********************************************/
|
||||
/* Parsing / Conversion */
|
||||
/*********************************************/
|
||||
|
||||
long String::toInt(void) const
|
||||
{
|
||||
if (buffer) {
|
||||
return atol(buffer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
float String::toFloat(void) const
|
||||
{
|
||||
return float(toDouble());
|
||||
}
|
||||
|
||||
double String::toDouble(void) const
|
||||
{
|
||||
if (buffer) {
|
||||
return atof(buffer);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,309 @@
|
|||
/*
|
||||
WString.h - String library for Wiring & Arduino
|
||||
...mostly rewritten by Paul Stoffregen...
|
||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||
|
||||
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef String_class_h
|
||||
#define String_class_h
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
// When compiling programs with this class, the following gcc parameters
|
||||
// dramatically increase performance and memory (RAM) efficiency, typically
|
||||
// with little or no increase in code size.
|
||||
// -felide-constructors
|
||||
// -std=c++0x
|
||||
|
||||
class __FlashStringHelper;
|
||||
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
|
||||
|
||||
// An inherited class for holding the result of a concatenation. These
|
||||
// result objects are assumed to be writable by subsequent concatenations.
|
||||
class StringSumHelper;
|
||||
|
||||
// The string class
|
||||
class String {
|
||||
// use a function pointer to allow for "if (s)" without the
|
||||
// complications of an operator bool(). for more information, see:
|
||||
// http://www.artima.com/cppsource/safebool.html
|
||||
typedef void (String::*StringIfHelperType)() const;
|
||||
void StringIfHelper() const {}
|
||||
|
||||
public:
|
||||
// constructors
|
||||
// creates a copy of the initial value.
|
||||
// if the initial value is null or invalid, or if memory allocation
|
||||
// fails, the string will be marked as invalid (i.e. "if (s)" will
|
||||
// be false).
|
||||
String(const char *cstr = "");
|
||||
String(const String &str);
|
||||
String(const __FlashStringHelper *str);
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
String(String &&rval);
|
||||
String(StringSumHelper &&rval);
|
||||
#endif
|
||||
explicit String(char c);
|
||||
explicit String(unsigned char, unsigned char base = 10);
|
||||
explicit String(int, unsigned char base = 10);
|
||||
explicit String(unsigned int, unsigned char base = 10);
|
||||
explicit String(long, unsigned char base = 10);
|
||||
explicit String(unsigned long, unsigned char base = 10);
|
||||
explicit String(float, unsigned char decimalPlaces = 2);
|
||||
explicit String(double, unsigned char decimalPlaces = 2);
|
||||
~String(void);
|
||||
|
||||
// memory management
|
||||
// return true on success, false on failure (in which case, the string
|
||||
// is left unchanged). reserve(0), if successful, will validate an
|
||||
// invalid string (i.e., "if (s)" will be true afterwards)
|
||||
unsigned char reserve(unsigned int size);
|
||||
inline unsigned int length(void) const
|
||||
{
|
||||
return len;
|
||||
}
|
||||
|
||||
// creates a copy of the assigned value. if the value is null or
|
||||
// invalid, or if the memory allocation fails, the string will be
|
||||
// marked as invalid ("if (s)" will be false).
|
||||
String &operator = (const String &rhs);
|
||||
String &operator = (const char *cstr);
|
||||
String &operator = (const __FlashStringHelper *str);
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
String &operator = (String &&rval);
|
||||
String &operator = (StringSumHelper &&rval);
|
||||
#endif
|
||||
|
||||
// concatenate (works w/ built-in types)
|
||||
|
||||
// returns true on success, false on failure (in which case, the string
|
||||
// is left unchanged). if the argument is null or invalid, the
|
||||
// concatenation is considered unsuccessful.
|
||||
unsigned char concat(const String &str);
|
||||
unsigned char concat(const char *cstr);
|
||||
unsigned char concat(char c);
|
||||
unsigned char concat(unsigned char c);
|
||||
unsigned char concat(int num);
|
||||
unsigned char concat(unsigned int num);
|
||||
unsigned char concat(long num);
|
||||
unsigned char concat(unsigned long num);
|
||||
unsigned char concat(float num);
|
||||
unsigned char concat(double num);
|
||||
unsigned char concat(const __FlashStringHelper *str);
|
||||
|
||||
// if there's not enough memory for the concatenated value, the string
|
||||
// will be left unchanged (but this isn't signalled in any way)
|
||||
String &operator += (const String &rhs)
|
||||
{
|
||||
concat(rhs);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (const char *cstr)
|
||||
{
|
||||
concat(cstr);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (char c)
|
||||
{
|
||||
concat(c);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (unsigned char num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (int num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (unsigned int num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (long num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (unsigned long num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (float num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (double num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String &operator += (const __FlashStringHelper *str)
|
||||
{
|
||||
concat(str);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, const String &rhs);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, const char *cstr);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, char c);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned char num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, int num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned int num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, long num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned long num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, float num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, double num);
|
||||
friend StringSumHelper &operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
||||
|
||||
// comparison (only works w/ Strings and "strings")
|
||||
operator StringIfHelperType() const
|
||||
{
|
||||
return buffer ? &String::StringIfHelper : 0;
|
||||
}
|
||||
int compareTo(const String &s) const;
|
||||
unsigned char equals(const String &s) const;
|
||||
unsigned char equals(const char *cstr) const;
|
||||
unsigned char operator == (const String &rhs) const
|
||||
{
|
||||
return equals(rhs);
|
||||
}
|
||||
unsigned char operator == (const char *cstr) const
|
||||
{
|
||||
return equals(cstr);
|
||||
}
|
||||
unsigned char operator != (const String &rhs) const
|
||||
{
|
||||
return !equals(rhs);
|
||||
}
|
||||
unsigned char operator != (const char *cstr) const
|
||||
{
|
||||
return !equals(cstr);
|
||||
}
|
||||
unsigned char operator < (const String &rhs) const;
|
||||
unsigned char operator > (const String &rhs) const;
|
||||
unsigned char operator <= (const String &rhs) const;
|
||||
unsigned char operator >= (const String &rhs) const;
|
||||
unsigned char equalsIgnoreCase(const String &s) const;
|
||||
unsigned char startsWith(const String &prefix) const;
|
||||
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
||||
unsigned char endsWith(const String &suffix) const;
|
||||
|
||||
// character access
|
||||
char charAt(unsigned int index) const;
|
||||
void setCharAt(unsigned int index, char c);
|
||||
char operator [](unsigned int index) const;
|
||||
char &operator [](unsigned int index);
|
||||
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
|
||||
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const
|
||||
{
|
||||
getBytes((unsigned char *)buf, bufsize, index);
|
||||
}
|
||||
const char *c_str() const
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
char *begin()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
char *end()
|
||||
{
|
||||
return buffer + length();
|
||||
}
|
||||
const char *begin() const
|
||||
{
|
||||
return c_str();
|
||||
}
|
||||
const char *end() const
|
||||
{
|
||||
return c_str() + length();
|
||||
}
|
||||
|
||||
// search
|
||||
int indexOf(char ch) const;
|
||||
int indexOf(char ch, unsigned int fromIndex) const;
|
||||
int indexOf(const String &str) const;
|
||||
int indexOf(const String &str, unsigned int fromIndex) const;
|
||||
int lastIndexOf(char ch) const;
|
||||
int lastIndexOf(char ch, unsigned int fromIndex) const;
|
||||
int lastIndexOf(const String &str) const;
|
||||
int lastIndexOf(const String &str, unsigned int fromIndex) const;
|
||||
String substring(unsigned int beginIndex) const
|
||||
{
|
||||
return substring(beginIndex, len);
|
||||
};
|
||||
String substring(unsigned int beginIndex, unsigned int endIndex) const;
|
||||
|
||||
// modification
|
||||
void replace(char find, char replace);
|
||||
void replace(const String &find, const String &replace);
|
||||
void remove(unsigned int index);
|
||||
void remove(unsigned int index, unsigned int count);
|
||||
void toLowerCase(void);
|
||||
void toUpperCase(void);
|
||||
void trim(void);
|
||||
|
||||
// parsing/conversion
|
||||
long toInt(void) const;
|
||||
float toFloat(void) const;
|
||||
double toDouble(void) const;
|
||||
|
||||
protected:
|
||||
char *buffer; // the actual char array
|
||||
unsigned int capacity; // the array length minus one (for the '\0')
|
||||
unsigned int len; // the String length (not counting the '\0')
|
||||
protected:
|
||||
void init(void);
|
||||
void invalidate(void);
|
||||
unsigned char changeBuffer(unsigned int maxStrLen);
|
||||
unsigned char concat(const char *cstr, unsigned int length);
|
||||
|
||||
// copy and move
|
||||
String ©(const char *cstr, unsigned int length);
|
||||
String ©(const __FlashStringHelper *pstr, unsigned int length);
|
||||
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
void move(String &rhs);
|
||||
#endif
|
||||
};
|
||||
|
||||
class StringSumHelper : public String {
|
||||
public:
|
||||
StringSumHelper(const String &s) : String(s) {}
|
||||
StringSumHelper(const char *p) : String(p) {}
|
||||
StringSumHelper(char c) : String(c) {}
|
||||
StringSumHelper(unsigned char num) : String(num) {}
|
||||
StringSumHelper(int num) : String(num) {}
|
||||
StringSumHelper(unsigned int num) : String(num) {}
|
||||
StringSumHelper(long num) : String(num) {}
|
||||
StringSumHelper(unsigned long num) : String(num) {}
|
||||
StringSumHelper(float num) : String(num) {}
|
||||
StringSumHelper(double num) : String(num) {}
|
||||
};
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // String_class_h
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
Copyright (c) 2014 Arduino. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
|
||||
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
|
||||
|
||||
void __cxa_pure_virtual(void) {
|
||||
// We might want to write some diagnostics to uart in this case
|
||||
//std::terminate();
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
||||
void __cxa_deleted_virtual(void) {
|
||||
// We might want to write some diagnostics to uart in this case
|
||||
//std::terminate();
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
dtostrf - Emulation for dtostrf function from avr-libc
|
||||
Copyright (c) 2013 Arduino. All rights reserved.
|
||||
Written by Cristian Maglie <c.maglie@arduino.cc>
|
||||
|
||||
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
|
||||
//Commented code is the original version
|
||||
/*char fmt[20];
|
||||
sprintf(fmt, "%%%d.%df", width, prec);
|
||||
sprintf(sout, fmt, val);
|
||||
return sout;*/
|
||||
|
||||
// Handle negative numbers
|
||||
unsigned int negative = 0;
|
||||
if (val < 0.0)
|
||||
{
|
||||
negative = 1;
|
||||
val = -val;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for (int i=0; i<prec; ++i)
|
||||
rounding /= 10.0;
|
||||
|
||||
val += rounding;
|
||||
|
||||
// Extract the integer part of the number
|
||||
unsigned long int_part = (unsigned long)val;
|
||||
double remainder = val - (double)int_part;
|
||||
|
||||
if(negative)
|
||||
int_part = -int_part;
|
||||
|
||||
// Extract digits from the remainder
|
||||
unsigned long dec_part = 0;
|
||||
double decade = 1.0;
|
||||
for(int i=0; i < prec; i++)
|
||||
decade *= 10.0;
|
||||
remainder *= decade;
|
||||
dec_part = (int)remainder;
|
||||
|
||||
sprintf(sout, "%ld.%ld", int_part, dec_part);
|
||||
|
||||
// Handle minimum field width of the output string
|
||||
// width is signed value, negative for left adjustment.
|
||||
// Range -128,127
|
||||
char fmt[129] = "";
|
||||
unsigned int w = width;
|
||||
if (width < 0) {
|
||||
negative = 1;
|
||||
w = -width;
|
||||
} else {
|
||||
negative = 0;
|
||||
}
|
||||
|
||||
if(strlen(sout) < w) {
|
||||
memset(fmt, ' ', 128);
|
||||
fmt[w-strlen(sout)] = '\0';
|
||||
if(negative == 0) {
|
||||
char *tmp = strdup(sout);
|
||||
strcpy(sout,fmt);
|
||||
strcat(sout, tmp);
|
||||
free(tmp);
|
||||
} else {
|
||||
// left adjustment
|
||||
strcat(sout, fmt);
|
||||
}
|
||||
}
|
||||
|
||||
return sout;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
dtostrf - Emulation for dtostrf function from avr-libc
|
||||
Copyright (c) 2013 Arduino. All rights reserved.
|
||||
Written by Cristian Maglie <c.maglie@arduino.cc>
|
||||
|
||||
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef __DTOSTRF_H_
|
||||
#define __DTOSTRF_H_ 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
char *dtostrf (double val, signed char width, unsigned char prec, char *sout);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,130 @@
|
|||
/* Simple compatibility headers for AVR code used with ARM chips
|
||||
* Copyright (c) 2015 Paul Stoffregen <paul@pjrc.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef __PGMSPACE_H_
|
||||
#define __PGMSPACE_H_ 1
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#define PROGMEM
|
||||
#define PGM_P const char *
|
||||
#define PSTR(str) (str)
|
||||
|
||||
#define _SFR_BYTE(n) (n)
|
||||
|
||||
typedef void prog_void;
|
||||
typedef char prog_char;
|
||||
typedef unsigned char prog_uchar;
|
||||
typedef int8_t prog_int8_t;
|
||||
typedef uint8_t prog_uint8_t;
|
||||
typedef int16_t prog_int16_t;
|
||||
typedef uint16_t prog_uint16_t;
|
||||
typedef int32_t prog_int32_t;
|
||||
typedef uint32_t prog_uint32_t;
|
||||
typedef int64_t prog_int64_t;
|
||||
typedef uint64_t prog_uint64_t;
|
||||
|
||||
#define memchr_P(str, c, len) memchr((str), (c), (len))
|
||||
#define memcmp_P(a, b, n) memcmp((a), (b), (n))
|
||||
#define memcpy_P(dest, src, num) memcpy((dest), (src), (num))
|
||||
#define memmem_P(a, alen, b, blen) memmem((a), (alen), (b), (blen))
|
||||
#define memrchr_P(str, val, len) memrchr((str), (val), (len))
|
||||
#define strcat_P(dest, src) strcat((dest), (src))
|
||||
#define strchr_P(str, c) strchr((str), (c))
|
||||
#define strchrnul_P(str, c) strchrnul((str), (c))
|
||||
#define strcmp_P(a, b) strcmp((a), (b))
|
||||
#define strcpy_P(dest, src) strcpy((dest), (src))
|
||||
#define strcasecmp_P(a, b) strcasecmp((a), (b))
|
||||
#define strcasestr_P(a, b) strcasestr((a), (b))
|
||||
#define strlcat_P(dest, src, len) strlcat((dest), (src), (len))
|
||||
#define strlcpy_P(dest, src, len) strlcpy((dest), (src), (len))
|
||||
#define strlen_P(s) strlen((const char *)(s))
|
||||
#define strnlen_P(str, len) strnlen((str), (len))
|
||||
#define strncmp_P(a, b, n) strncmp((a), (b), (n))
|
||||
#define strncasecmp_P(a, b, n) strncasecmp((a), (b), (n))
|
||||
#define strncat_P(a, b, n) strncat((a), (b), (n))
|
||||
#define strncpy_P(a, b, n) strncpy((a), (b), (n))
|
||||
#define strpbrk_P(str, chrs) strpbrk((str), (chrs))
|
||||
#define strrchr_P(str, c) strrchr((str), (c))
|
||||
#define strsep_P(strp, delim) strsep((strp), (delim))
|
||||
#define strspn_P(str, chrs) strspn((str), (chrs))
|
||||
#define strstr_P(a, b) strstr((a), (b))
|
||||
#define sprintf_P(s, ...) sprintf((s), __VA_ARGS__)
|
||||
#define vfprintf_P(fp, s, ...) vfprintf((fp), (s), __VA_ARGS__)
|
||||
#define printf_P(...) printf(__VA_ARGS__)
|
||||
#define snprintf_P(s, n, ...) snprintf((s), (n), __VA_ARGS__)
|
||||
#define vsprintf_P(s, ...) vsprintf((s), __VA_ARGS__)
|
||||
#define vsnprintf_P(s, n, ...) vsnprintf((s), (n), __VA_ARGS__)
|
||||
#define fprintf_P(fp, ...) fprintf((fp), __VA_ARGS__)
|
||||
#define strlen_PF(a) strlen((a))
|
||||
#define strnlen_PF(src, len) strnlen((src), (len))
|
||||
#define memcpy_PF(dest, src, len) memcpy((dest), (src), (len))
|
||||
#define strcpy_PF(dest, src) strcpy((dest), (src))
|
||||
#define strncpy_PF(dest, src, len) strncpy((dest), (src), (len))
|
||||
#define strcat_PF(dest, src) strcat((dest), (src))
|
||||
#define strlcat_PF(dest, src, len) strlcat((dest), (src), (len))
|
||||
#define strncat_PF(dest, src, len) strncat((dest), (src), (len))
|
||||
#define strcmp_PF(s1, s2) strcmp((s1), (s2))
|
||||
#define strncmp_PF(s1, s2, n) strncmp((s1), (s2), (n))
|
||||
#define strcasecmp_PF(s1, s2) strcasecmp((s1), (s2))
|
||||
#define strncasecmp_PF(s1, s2, n) strncasecmp((s1), (s2), (n))
|
||||
#define strstr_PF(s1, s2) strstr((s1), (s2))
|
||||
#define strlcpy_PF(dest, src, n) strlcpy((dest), (src), (n))
|
||||
#define memcmp_PF(s1, s2, n) memcmp((s1), (s2), (n))
|
||||
|
||||
|
||||
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
|
||||
#if 0
|
||||
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
|
||||
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
|
||||
#define pgm_read_float(addr) (*(const float *)(addr))
|
||||
#else
|
||||
#define pgm_read_word(addr) ({ \
|
||||
typeof(addr) _addr = (addr); \
|
||||
*(const unsigned short *)(_addr); \
|
||||
})
|
||||
#define pgm_read_dword(addr) ({ \
|
||||
typeof(addr) _addr = (addr); \
|
||||
*(const unsigned long *)(_addr); \
|
||||
})
|
||||
#define pgm_read_float(addr) ({ \
|
||||
typeof(addr) _addr = (addr); \
|
||||
*(const float *)(_addr); \
|
||||
})
|
||||
#define pgm_read_ptr(addr) ({ \
|
||||
typeof(addr) _addr = (addr); \
|
||||
*(void * const *)(_addr); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#define pgm_read_byte_near(addr) pgm_read_byte(addr)
|
||||
#define pgm_read_word_near(addr) pgm_read_word(addr)
|
||||
#define pgm_read_dword_near(addr) pgm_read_dword(addr)
|
||||
#define pgm_read_float_near(addr) pgm_read_float(addr)
|
||||
#define pgm_read_ptr_near(addr) pgm_read_ptr(addr)
|
||||
#define pgm_read_byte_far(addr) pgm_read_byte(addr)
|
||||
#define pgm_read_word_far(addr) pgm_read_word(addr)
|
||||
#define pgm_read_dword_far(addr) pgm_read_dword(addr)
|
||||
#define pgm_read_float_far(addr) pgm_read_float(addr)
|
||||
#define pgm_read_ptr_far(addr) pgm_read_ptr(addr)
|
||||
|
||||
#endif
|
|
@ -0,0 +1,541 @@
|
|||
/*
|
||||
binary.h - Definitions for binary constants
|
||||
Copyright (c) 2006 David A. Mellis. 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef Binary_h
|
||||
#define Binary_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define B0 0
|
||||
#define B00 0
|
||||
#define B000 0
|
||||
#define B0000 0
|
||||
#define B00000 0
|
||||
#define B000000 0
|
||||
#define B0000000 0
|
||||
#define B00000000 0
|
||||
#define B1 1
|
||||
#define B01 1
|
||||
#define B001 1
|
||||
#define B0001 1
|
||||
#define B00001 1
|
||||
#define B000001 1
|
||||
#define B0000001 1
|
||||
#define B00000001 1
|
||||
#define B10 2
|
||||
#define B010 2
|
||||
#define B0010 2
|
||||
#define B00010 2
|
||||
#define B000010 2
|
||||
#define B0000010 2
|
||||
#define B00000010 2
|
||||
#define B11 3
|
||||
#define B011 3
|
||||
#define B0011 3
|
||||
#define B00011 3
|
||||
#define B000011 3
|
||||
#define B0000011 3
|
||||
#define B00000011 3
|
||||
#define B100 4
|
||||
#define B0100 4
|
||||
#define B00100 4
|
||||
#define B000100 4
|
||||
#define B0000100 4
|
||||
#define B00000100 4
|
||||
#define B101 5
|
||||
#define B0101 5
|
||||
#define B00101 5
|
||||
#define B000101 5
|
||||
#define B0000101 5
|
||||
#define B00000101 5
|
||||
#define B110 6
|
||||
#define B0110 6
|
||||
#define B00110 6
|
||||
#define B000110 6
|
||||
#define B0000110 6
|
||||
#define B00000110 6
|
||||
#define B111 7
|
||||
#define B0111 7
|
||||
#define B00111 7
|
||||
#define B000111 7
|
||||
#define B0000111 7
|
||||
#define B00000111 7
|
||||
#define B1000 8
|
||||
#define B01000 8
|
||||
#define B001000 8
|
||||
#define B0001000 8
|
||||
#define B00001000 8
|
||||
#define B1001 9
|
||||
#define B01001 9
|
||||
#define B001001 9
|
||||
#define B0001001 9
|
||||
#define B00001001 9
|
||||
#define B1010 10
|
||||
#define B01010 10
|
||||
#define B001010 10
|
||||
#define B0001010 10
|
||||
#define B00001010 10
|
||||
#define B1011 11
|
||||
#define B01011 11
|
||||
#define B001011 11
|
||||
#define B0001011 11
|
||||
#define B00001011 11
|
||||
#define B1100 12
|
||||
#define B01100 12
|
||||
#define B001100 12
|
||||
#define B0001100 12
|
||||
#define B00001100 12
|
||||
#define B1101 13
|
||||
#define B01101 13
|
||||
#define B001101 13
|
||||
#define B0001101 13
|
||||
#define B00001101 13
|
||||
#define B1110 14
|
||||
#define B01110 14
|
||||
#define B001110 14
|
||||
#define B0001110 14
|
||||
#define B00001110 14
|
||||
#define B1111 15
|
||||
#define B01111 15
|
||||
#define B001111 15
|
||||
#define B0001111 15
|
||||
#define B00001111 15
|
||||
#define B10000 16
|
||||
#define B010000 16
|
||||
#define B0010000 16
|
||||
#define B00010000 16
|
||||
#define B10001 17
|
||||
#define B010001 17
|
||||
#define B0010001 17
|
||||
#define B00010001 17
|
||||
#define B10010 18
|
||||
#define B010010 18
|
||||
#define B0010010 18
|
||||
#define B00010010 18
|
||||
#define B10011 19
|
||||
#define B010011 19
|
||||
#define B0010011 19
|
||||
#define B00010011 19
|
||||
#define B10100 20
|
||||
#define B010100 20
|
||||
#define B0010100 20
|
||||
#define B00010100 20
|
||||
#define B10101 21
|
||||
#define B010101 21
|
||||
#define B0010101 21
|
||||
#define B00010101 21
|
||||
#define B10110 22
|
||||
#define B010110 22
|
||||
#define B0010110 22
|
||||
#define B00010110 22
|
||||
#define B10111 23
|
||||
#define B010111 23
|
||||
#define B0010111 23
|
||||
#define B00010111 23
|
||||
#define B11000 24
|
||||
#define B011000 24
|
||||
#define B0011000 24
|
||||
#define B00011000 24
|
||||
#define B11001 25
|
||||
#define B011001 25
|
||||
#define B0011001 25
|
||||
#define B00011001 25
|
||||
#define B11010 26
|
||||
#define B011010 26
|
||||
#define B0011010 26
|
||||
#define B00011010 26
|
||||
#define B11011 27
|
||||
#define B011011 27
|
||||
#define B0011011 27
|
||||
#define B00011011 27
|
||||
#define B11100 28
|
||||
#define B011100 28
|
||||
#define B0011100 28
|
||||
#define B00011100 28
|
||||
#define B11101 29
|
||||
#define B011101 29
|
||||
#define B0011101 29
|
||||
#define B00011101 29
|
||||
#define B11110 30
|
||||
#define B011110 30
|
||||
#define B0011110 30
|
||||
#define B00011110 30
|
||||
#define B11111 31
|
||||
#define B011111 31
|
||||
#define B0011111 31
|
||||
#define B00011111 31
|
||||
#define B100000 32
|
||||
#define B0100000 32
|
||||
#define B00100000 32
|
||||
#define B100001 33
|
||||
#define B0100001 33
|
||||
#define B00100001 33
|
||||
#define B100010 34
|
||||
#define B0100010 34
|
||||
#define B00100010 34
|
||||
#define B100011 35
|
||||
#define B0100011 35
|
||||
#define B00100011 35
|
||||
#define B100100 36
|
||||
#define B0100100 36
|
||||
#define B00100100 36
|
||||
#define B100101 37
|
||||
#define B0100101 37
|
||||
#define B00100101 37
|
||||
#define B100110 38
|
||||
#define B0100110 38
|
||||
#define B00100110 38
|
||||
#define B100111 39
|
||||
#define B0100111 39
|
||||
#define B00100111 39
|
||||
#define B101000 40
|
||||
#define B0101000 40
|
||||
#define B00101000 40
|
||||
#define B101001 41
|
||||
#define B0101001 41
|
||||
#define B00101001 41
|
||||
#define B101010 42
|
||||
#define B0101010 42
|
||||
#define B00101010 42
|
||||
#define B101011 43
|
||||
#define B0101011 43
|
||||
#define B00101011 43
|
||||
#define B101100 44
|
||||
#define B0101100 44
|
||||
#define B00101100 44
|
||||
#define B101101 45
|
||||
#define B0101101 45
|
||||
#define B00101101 45
|
||||
#define B101110 46
|
||||
#define B0101110 46
|
||||
#define B00101110 46
|
||||
#define B101111 47
|
||||
#define B0101111 47
|
||||
#define B00101111 47
|
||||
#define B110000 48
|
||||
#define B0110000 48
|
||||
#define B00110000 48
|
||||
#define B110001 49
|
||||
#define B0110001 49
|
||||
#define B00110001 49
|
||||
#define B110010 50
|
||||
#define B0110010 50
|
||||
#define B00110010 50
|
||||
#define B110011 51
|
||||
#define B0110011 51
|
||||
#define B00110011 51
|
||||
#define B110100 52
|
||||
#define B0110100 52
|
||||
#define B00110100 52
|
||||
#define B110101 53
|
||||
#define B0110101 53
|
||||
#define B00110101 53
|
||||
#define B110110 54
|
||||
#define B0110110 54
|
||||
#define B00110110 54
|
||||
#define B110111 55
|
||||
#define B0110111 55
|
||||
#define B00110111 55
|
||||
#define B111000 56
|
||||
#define B0111000 56
|
||||
#define B00111000 56
|
||||
#define B111001 57
|
||||
#define B0111001 57
|
||||
#define B00111001 57
|
||||
#define B111010 58
|
||||
#define B0111010 58
|
||||
#define B00111010 58
|
||||
#define B111011 59
|
||||
#define B0111011 59
|
||||
#define B00111011 59
|
||||
#define B111100 60
|
||||
#define B0111100 60
|
||||
#define B00111100 60
|
||||
#define B111101 61
|
||||
#define B0111101 61
|
||||
#define B00111101 61
|
||||
#define B111110 62
|
||||
#define B0111110 62
|
||||
#define B00111110 62
|
||||
#define B111111 63
|
||||
#define B0111111 63
|
||||
#define B00111111 63
|
||||
#define B1000000 64
|
||||
#define B01000000 64
|
||||
#define B1000001 65
|
||||
#define B01000001 65
|
||||
#define B1000010 66
|
||||
#define B01000010 66
|
||||
#define B1000011 67
|
||||
#define B01000011 67
|
||||
#define B1000100 68
|
||||
#define B01000100 68
|
||||
#define B1000101 69
|
||||
#define B01000101 69
|
||||
#define B1000110 70
|
||||
#define B01000110 70
|
||||
#define B1000111 71
|
||||
#define B01000111 71
|
||||
#define B1001000 72
|
||||
#define B01001000 72
|
||||
#define B1001001 73
|
||||
#define B01001001 73
|
||||
#define B1001010 74
|
||||
#define B01001010 74
|
||||
#define B1001011 75
|
||||
#define B01001011 75
|
||||
#define B1001100 76
|
||||
#define B01001100 76
|
||||
#define B1001101 77
|
||||
#define B01001101 77
|
||||
#define B1001110 78
|
||||
#define B01001110 78
|
||||
#define B1001111 79
|
||||
#define B01001111 79
|
||||
#define B1010000 80
|
||||
#define B01010000 80
|
||||
#define B1010001 81
|
||||
#define B01010001 81
|
||||
#define B1010010 82
|
||||
#define B01010010 82
|
||||
#define B1010011 83
|
||||
#define B01010011 83
|
||||
#define B1010100 84
|
||||
#define B01010100 84
|
||||
#define B1010101 85
|
||||
#define B01010101 85
|
||||
#define B1010110 86
|
||||
#define B01010110 86
|
||||
#define B1010111 87
|
||||
#define B01010111 87
|
||||
#define B1011000 88
|
||||
#define B01011000 88
|
||||
#define B1011001 89
|
||||
#define B01011001 89
|
||||
#define B1011010 90
|
||||
#define B01011010 90
|
||||
#define B1011011 91
|
||||
#define B01011011 91
|
||||
#define B1011100 92
|
||||
#define B01011100 92
|
||||
#define B1011101 93
|
||||
#define B01011101 93
|
||||
#define B1011110 94
|
||||
#define B01011110 94
|
||||
#define B1011111 95
|
||||
#define B01011111 95
|
||||
#define B1100000 96
|
||||
#define B01100000 96
|
||||
#define B1100001 97
|
||||
#define B01100001 97
|
||||
#define B1100010 98
|
||||
#define B01100010 98
|
||||
#define B1100011 99
|
||||
#define B01100011 99
|
||||
#define B1100100 100
|
||||
#define B01100100 100
|
||||
#define B1100101 101
|
||||
#define B01100101 101
|
||||
#define B1100110 102
|
||||
#define B01100110 102
|
||||
#define B1100111 103
|
||||
#define B01100111 103
|
||||
#define B1101000 104
|
||||
#define B01101000 104
|
||||
#define B1101001 105
|
||||
#define B01101001 105
|
||||
#define B1101010 106
|
||||
#define B01101010 106
|
||||
#define B1101011 107
|
||||
#define B01101011 107
|
||||
#define B1101100 108
|
||||
#define B01101100 108
|
||||
#define B1101101 109
|
||||
#define B01101101 109
|
||||
#define B1101110 110
|
||||
#define B01101110 110
|
||||
#define B1101111 111
|
||||
#define B01101111 111
|
||||
#define B1110000 112
|
||||
#define B01110000 112
|
||||
#define B1110001 113
|
||||
#define B01110001 113
|
||||
#define B1110010 114
|
||||
#define B01110010 114
|
||||
#define B1110011 115
|
||||
#define B01110011 115
|
||||
#define B1110100 116
|
||||
#define B01110100 116
|
||||
#define B1110101 117
|
||||
#define B01110101 117
|
||||
#define B1110110 118
|
||||
#define B01110110 118
|
||||
#define B1110111 119
|
||||
#define B01110111 119
|
||||
#define B1111000 120
|
||||
#define B01111000 120
|
||||
#define B1111001 121
|
||||
#define B01111001 121
|
||||
#define B1111010 122
|
||||
#define B01111010 122
|
||||
#define B1111011 123
|
||||
#define B01111011 123
|
||||
#define B1111100 124
|
||||
#define B01111100 124
|
||||
#define B1111101 125
|
||||
#define B01111101 125
|
||||
#define B1111110 126
|
||||
#define B01111110 126
|
||||
#define B1111111 127
|
||||
#define B01111111 127
|
||||
#define B10000000 128
|
||||
#define B10000001 129
|
||||
#define B10000010 130
|
||||
#define B10000011 131
|
||||
#define B10000100 132
|
||||
#define B10000101 133
|
||||
#define B10000110 134
|
||||
#define B10000111 135
|
||||
#define B10001000 136
|
||||
#define B10001001 137
|
||||
#define B10001010 138
|
||||
#define B10001011 139
|
||||
#define B10001100 140
|
||||
#define B10001101 141
|
||||
#define B10001110 142
|
||||
#define B10001111 143
|
||||
#define B10010000 144
|
||||
#define B10010001 145
|
||||
#define B10010010 146
|
||||
#define B10010011 147
|
||||
#define B10010100 148
|
||||
#define B10010101 149
|
||||
#define B10010110 150
|
||||
#define B10010111 151
|
||||
#define B10011000 152
|
||||
#define B10011001 153
|
||||
#define B10011010 154
|
||||
#define B10011011 155
|
||||
#define B10011100 156
|
||||
#define B10011101 157
|
||||
#define B10011110 158
|
||||
#define B10011111 159
|
||||
#define B10100000 160
|
||||
#define B10100001 161
|
||||
#define B10100010 162
|
||||
#define B10100011 163
|
||||
#define B10100100 164
|
||||
#define B10100101 165
|
||||
#define B10100110 166
|
||||
#define B10100111 167
|
||||
#define B10101000 168
|
||||
#define B10101001 169
|
||||
#define B10101010 170
|
||||
#define B10101011 171
|
||||
#define B10101100 172
|
||||
#define B10101101 173
|
||||
#define B10101110 174
|
||||
#define B10101111 175
|
||||
#define B10110000 176
|
||||
#define B10110001 177
|
||||
#define B10110010 178
|
||||
#define B10110011 179
|
||||
#define B10110100 180
|
||||
#define B10110101 181
|
||||
#define B10110110 182
|
||||
#define B10110111 183
|
||||
#define B10111000 184
|
||||
#define B10111001 185
|
||||
#define B10111010 186
|
||||
#define B10111011 187
|
||||
#define B10111100 188
|
||||
#define B10111101 189
|
||||
#define B10111110 190
|
||||
#define B10111111 191
|
||||
#define B11000000 192
|
||||
#define B11000001 193
|
||||
#define B11000010 194
|
||||
#define B11000011 195
|
||||
#define B11000100 196
|
||||
#define B11000101 197
|
||||
#define B11000110 198
|
||||
#define B11000111 199
|
||||
#define B11001000 200
|
||||
#define B11001001 201
|
||||
#define B11001010 202
|
||||
#define B11001011 203
|
||||
#define B11001100 204
|
||||
#define B11001101 205
|
||||
#define B11001110 206
|
||||
#define B11001111 207
|
||||
#define B11010000 208
|
||||
#define B11010001 209
|
||||
#define B11010010 210
|
||||
#define B11010011 211
|
||||
#define B11010100 212
|
||||
#define B11010101 213
|
||||
#define B11010110 214
|
||||
#define B11010111 215
|
||||
#define B11011000 216
|
||||
#define B11011001 217
|
||||
#define B11011010 218
|
||||
#define B11011011 219
|
||||
#define B11011100 220
|
||||
#define B11011101 221
|
||||
#define B11011110 222
|
||||
#define B11011111 223
|
||||
#define B11100000 224
|
||||
#define B11100001 225
|
||||
#define B11100010 226
|
||||
#define B11100011 227
|
||||
#define B11100100 228
|
||||
#define B11100101 229
|
||||
#define B11100110 230
|
||||
#define B11100111 231
|
||||
#define B11101000 232
|
||||
#define B11101001 233
|
||||
#define B11101010 234
|
||||
#define B11101011 235
|
||||
#define B11101100 236
|
||||
#define B11101101 237
|
||||
#define B11101110 238
|
||||
#define B11101111 239
|
||||
#define B11110000 240
|
||||
#define B11110001 241
|
||||
#define B11110010 242
|
||||
#define B11110011 243
|
||||
#define B11110100 244
|
||||
#define B11110101 245
|
||||
#define B11110110 246
|
||||
#define B11110111 247
|
||||
#define B11111000 248
|
||||
#define B11111001 249
|
||||
#define B11111010 250
|
||||
#define B11111011 251
|
||||
#define B11111100 252
|
||||
#define B11111101 253
|
||||
#define B11111110 254
|
||||
#define B11111111 255
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -0,0 +1,16 @@
|
|||
#include "board.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
WEAK void pre_init(void)
|
||||
{
|
||||
hw_config_init();
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef _BOARD_H_
|
||||
#define _BOARD_H_
|
||||
|
||||
|
||||
#include "interrupt.h"
|
||||
#include "analog.h"
|
||||
// #include "backup.h"
|
||||
#include "clock.h"
|
||||
// #include "core_callback.h"
|
||||
#include "digital_io.h"
|
||||
// #include "dwt.h"
|
||||
#include "hw_config.h"
|
||||
// #include "otp.h"
|
||||
// #include "timer.h"
|
||||
#include "uart.h"
|
||||
|
||||
|
||||
/*
|
||||
* Core and peripherals registers definitions
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void pre_init(void) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _BOARD_H_ */
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2014, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
#ifndef _PERIPHERALPINS_H
|
||||
#define _PERIPHERALPINS_H
|
||||
|
||||
#include "pinmap.h"
|
||||
#include "PinAF_ch32yyxx.h"
|
||||
|
||||
//*** ADC ***
|
||||
extern const PinMap PinMap_ADC[];
|
||||
|
||||
//*** DAC ***
|
||||
extern const PinMap PinMap_DAC[];
|
||||
|
||||
//*** I2C ***
|
||||
extern const PinMap PinMap_I2C_SDA[];
|
||||
extern const PinMap PinMap_I2C_SCL[];
|
||||
|
||||
//*** TIM ***
|
||||
/* For backward compatibility */
|
||||
#define PinMap_PWM PinMap_TIM
|
||||
extern const PinMap PinMap_TIM[];
|
||||
|
||||
//*** SERIAL ***
|
||||
extern const PinMap PinMap_UART_TX[];
|
||||
extern const PinMap PinMap_UART_RX[];
|
||||
extern const PinMap PinMap_UART_RTS[];
|
||||
extern const PinMap PinMap_UART_CTS[];
|
||||
|
||||
//*** SPI ***
|
||||
extern const PinMap PinMap_SPI_MOSI[];
|
||||
extern const PinMap PinMap_SPI_MISO[];
|
||||
extern const PinMap PinMap_SPI_SCLK[];
|
||||
extern const PinMap PinMap_SPI_SSEL[];
|
||||
|
||||
//*** CAN ***
|
||||
extern const PinMap PinMap_CAN_RD[];
|
||||
extern const PinMap PinMap_CAN_TD[];
|
||||
|
||||
//*** ETHERNET ***
|
||||
extern const PinMap PinMap_Ethernet[];
|
||||
|
||||
|
||||
//*** USB ***
|
||||
extern const PinMap PinMap_USB[];
|
||||
extern const PinMap PinMap_USB_OTG_FS[];
|
||||
extern const PinMap PinMap_USB_OTG_HS[];
|
||||
|
||||
//*** SD ***
|
||||
extern const PinMap PinMap_SD[];
|
||||
#endif
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _PINAF_CH32YYXX_H
|
||||
#define _PINAF_CH32YYXX_H
|
||||
|
||||
#if defined(CH32V20x) || defined(CH32V203xB) || defined(CH32V208)
|
||||
#include "../../../variants/CH32V20x/PinAF_CH32V20x.h"
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(CH32V00x)
|
||||
#include "../../../variants/CH32V00x/PinAF_CH32V00x.h"
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _PINAF_CH32YYXX_H */
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2017, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
#ifndef _PINCONFIGURED_H
|
||||
#define _PINCONFIGURED_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define PINCONF_INDEX(X) (CH_PORT(X)-FirstPort)
|
||||
|
||||
#define PINCONF_MASK 0x01
|
||||
#define PINCONF_SHIFT(X) (CH_PIN(X))
|
||||
#define PINCONF_BIT(X) (PINCONF_MASK << PINCONF_SHIFT(X))
|
||||
|
||||
#define PINCONF_VAL(X, Y) ((Y >> PINCONF_SHIFT(X)) & PINCONF_MASK)
|
||||
|
||||
#define is_pin_configured(pin, map) \
|
||||
(PINCONF_VAL(pin, map[PINCONF_INDEX(pin)]))
|
||||
#define set_pin_configured(pin, map) \
|
||||
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] | PINCONF_BIT(pin))
|
||||
#define reset_pin_configured(pin, map) \
|
||||
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin)))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,212 @@
|
|||
#ifndef _PINNAMES_H
|
||||
#define _PINNAMES_H
|
||||
|
||||
#include "PinNamesTypes.h"
|
||||
#include "PortNames.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Alternative possibilities which use other HW peripheral instances
|
||||
#define ALT0 0x000
|
||||
#define ALT1 0x100
|
||||
#define ALT2 0x200
|
||||
#define ALT3 0x300
|
||||
#define ALT4 0x400
|
||||
#define ALT5 0x500
|
||||
#define ALT6 0x600
|
||||
#define ALT7 0x700
|
||||
// ALTX mask
|
||||
#define ALTX_MASK 0x700
|
||||
|
||||
// Specific pinmap definition
|
||||
// Analog internal
|
||||
#define PNAME_ANALOG_INTERNAL_BASE 0x1000
|
||||
// Dual pad pin
|
||||
// Direct channels are connected to analog I/Os
|
||||
// (PY_C) to optimize ADC performance.
|
||||
#define PDUAL 0x2000
|
||||
// Remap pin
|
||||
#define PREMAP 0x3000
|
||||
// PinName mask
|
||||
#define PNAME_MASK 0xFF
|
||||
|
||||
typedef enum {
|
||||
// Not connected
|
||||
NC = 0xFFFFFFFF,
|
||||
|
||||
// Pin name definition
|
||||
PA_0 = (PortA << 4) + 0x00,
|
||||
PA_1 = (PortA << 4) + 0x01,
|
||||
PA_2 = (PortA << 4) + 0x02,
|
||||
PA_3 = (PortA << 4) + 0x03,
|
||||
PA_4 = (PortA << 4) + 0x04,
|
||||
PA_5 = (PortA << 4) + 0x05,
|
||||
PA_6 = (PortA << 4) + 0x06,
|
||||
PA_7 = (PortA << 4) + 0x07,
|
||||
PA_8 = (PortA << 4) + 0x08,
|
||||
PA_9 = (PortA << 4) + 0x09,
|
||||
PA_10 = (PortA << 4) + 0x0A,
|
||||
PA_11 = (PortA << 4) + 0x0B,
|
||||
PA_12 = (PortA << 4) + 0x0C,
|
||||
PA_13 = (PortA << 4) + 0x0D,
|
||||
PA_14 = (PortA << 4) + 0x0E,
|
||||
PA_15 = (PortA << 4) + 0x0F,
|
||||
#if defined GPIOB_BASE
|
||||
PB_0 = (PortB << 4) + 0x00,
|
||||
PB_1 = (PortB << 4) + 0x01,
|
||||
PB_2 = (PortB << 4) + 0x02,
|
||||
PB_3 = (PortB << 4) + 0x03,
|
||||
PB_4 = (PortB << 4) + 0x04,
|
||||
PB_5 = (PortB << 4) + 0x05,
|
||||
PB_6 = (PortB << 4) + 0x06,
|
||||
PB_7 = (PortB << 4) + 0x07,
|
||||
PB_8 = (PortB << 4) + 0x08,
|
||||
PB_9 = (PortB << 4) + 0x09,
|
||||
PB_10 = (PortB << 4) + 0x0A,
|
||||
PB_11 = (PortB << 4) + 0x0B,
|
||||
PB_12 = (PortB << 4) + 0x0C,
|
||||
PB_13 = (PortB << 4) + 0x0D,
|
||||
PB_14 = (PortB << 4) + 0x0E,
|
||||
PB_15 = (PortB << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOC_BASE
|
||||
PC_0 = (PortC << 4) + 0x00,
|
||||
PC_1 = (PortC << 4) + 0x01,
|
||||
PC_2 = (PortC << 4) + 0x02,
|
||||
PC_3 = (PortC << 4) + 0x03,
|
||||
PC_4 = (PortC << 4) + 0x04,
|
||||
PC_5 = (PortC << 4) + 0x05,
|
||||
PC_6 = (PortC << 4) + 0x06,
|
||||
PC_7 = (PortC << 4) + 0x07,
|
||||
PC_8 = (PortC << 4) + 0x08,
|
||||
PC_9 = (PortC << 4) + 0x09,
|
||||
PC_10 = (PortC << 4) + 0x0A,
|
||||
PC_11 = (PortC << 4) + 0x0B,
|
||||
PC_12 = (PortC << 4) + 0x0C,
|
||||
PC_13 = (PortC << 4) + 0x0D,
|
||||
PC_14 = (PortC << 4) + 0x0E,
|
||||
PC_15 = (PortC << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOD_BASE
|
||||
PD_0 = (PortD << 4) + 0x00,
|
||||
PD_1 = (PortD << 4) + 0x01,
|
||||
PD_2 = (PortD << 4) + 0x02,
|
||||
PD_3 = (PortD << 4) + 0x03,
|
||||
PD_4 = (PortD << 4) + 0x04,
|
||||
PD_5 = (PortD << 4) + 0x05,
|
||||
PD_6 = (PortD << 4) + 0x06,
|
||||
PD_7 = (PortD << 4) + 0x07,
|
||||
PD_8 = (PortD << 4) + 0x08,
|
||||
PD_9 = (PortD << 4) + 0x09,
|
||||
PD_10 = (PortD << 4) + 0x0A,
|
||||
PD_11 = (PortD << 4) + 0x0B,
|
||||
PD_12 = (PortD << 4) + 0x0C,
|
||||
PD_13 = (PortD << 4) + 0x0D,
|
||||
PD_14 = (PortD << 4) + 0x0E,
|
||||
PD_15 = (PortD << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOE_BASE
|
||||
PE_0 = (PortE << 4) + 0x00,
|
||||
PE_1 = (PortE << 4) + 0x01,
|
||||
PE_2 = (PortE << 4) + 0x02,
|
||||
PE_3 = (PortE << 4) + 0x03,
|
||||
PE_4 = (PortE << 4) + 0x04,
|
||||
PE_5 = (PortE << 4) + 0x05,
|
||||
PE_6 = (PortE << 4) + 0x06,
|
||||
PE_7 = (PortE << 4) + 0x07,
|
||||
PE_8 = (PortE << 4) + 0x08,
|
||||
PE_9 = (PortE << 4) + 0x09,
|
||||
PE_10 = (PortE << 4) + 0x0A,
|
||||
PE_11 = (PortE << 4) + 0x0B,
|
||||
PE_12 = (PortE << 4) + 0x0C,
|
||||
PE_13 = (PortE << 4) + 0x0D,
|
||||
PE_14 = (PortE << 4) + 0x0E,
|
||||
PE_15 = (PortE << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOF_BASE
|
||||
PF_0 = (PortF << 4) + 0x00,
|
||||
PF_1 = (PortF << 4) + 0x01,
|
||||
PF_2 = (PortF << 4) + 0x02,
|
||||
PF_3 = (PortF << 4) + 0x03,
|
||||
PF_4 = (PortF << 4) + 0x04,
|
||||
PF_5 = (PortF << 4) + 0x05,
|
||||
PF_6 = (PortF << 4) + 0x06,
|
||||
PF_7 = (PortF << 4) + 0x07,
|
||||
PF_8 = (PortF << 4) + 0x08,
|
||||
PF_9 = (PortF << 4) + 0x09,
|
||||
PF_10 = (PortF << 4) + 0x0A,
|
||||
PF_11 = (PortF << 4) + 0x0B,
|
||||
PF_12 = (PortF << 4) + 0x0C,
|
||||
PF_13 = (PortF << 4) + 0x0D,
|
||||
PF_14 = (PortF << 4) + 0x0E,
|
||||
PF_15 = (PortF << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOG_BASE
|
||||
PG_0 = (PortG << 4) + 0x00,
|
||||
PG_1 = (PortG << 4) + 0x01,
|
||||
PG_2 = (PortG << 4) + 0x02,
|
||||
PG_3 = (PortG << 4) + 0x03,
|
||||
PG_4 = (PortG << 4) + 0x04,
|
||||
PG_5 = (PortG << 4) + 0x05,
|
||||
PG_6 = (PortG << 4) + 0x06,
|
||||
PG_7 = (PortG << 4) + 0x07,
|
||||
PG_8 = (PortG << 4) + 0x08,
|
||||
PG_9 = (PortG << 4) + 0x09,
|
||||
PG_10 = (PortG << 4) + 0x0A,
|
||||
PG_11 = (PortG << 4) + 0x0B,
|
||||
PG_12 = (PortG << 4) + 0x0C,
|
||||
PG_13 = (PortG << 4) + 0x0D,
|
||||
PG_14 = (PortG << 4) + 0x0E,
|
||||
PG_15 = (PortG << 4) + 0x0F,
|
||||
#endif
|
||||
#if defined GPIOH_BASE
|
||||
PH_0 = (PortH << 4) + 0x00,
|
||||
PH_1 = (PortH << 4) + 0x01,
|
||||
PH_2 = (PortH << 4) + 0x02,
|
||||
PH_3 = (PortH << 4) + 0x03,
|
||||
PH_4 = (PortH << 4) + 0x04,
|
||||
PH_5 = (PortH << 4) + 0x05,
|
||||
PH_6 = (PortH << 4) + 0x06,
|
||||
PH_7 = (PortH << 4) + 0x07,
|
||||
PH_8 = (PortH << 4) + 0x08,
|
||||
PH_9 = (PortH << 4) + 0x09,
|
||||
PH_10 = (PortH << 4) + 0x0A,
|
||||
PH_11 = (PortH << 4) + 0x0B,
|
||||
PH_12 = (PortH << 4) + 0x0C,
|
||||
PH_13 = (PortH << 4) + 0x0D,
|
||||
PH_14 = (PortH << 4) + 0x0E,
|
||||
PH_15 = (PortH << 4) + 0x0F,
|
||||
#endif
|
||||
|
||||
// Specific pin name
|
||||
PADC_BASE = PNAME_ANALOG_INTERNAL_BASE,
|
||||
#if defined(ADC_Channel_TempSensor)
|
||||
PADC_TEMP,
|
||||
#endif
|
||||
#ifdef ADC_Channel_Vrefint
|
||||
PADC_VREF,
|
||||
#endif
|
||||
#ifdef ADC_Channel_Vbat
|
||||
PADC_VBAT,
|
||||
#endif
|
||||
ANA_START,
|
||||
#ifdef SYSCFG_PMCSETR_ANA0_SEL_Pos
|
||||
ANA_0,
|
||||
#endif
|
||||
#ifdef SYSCFG_PMCSETR_ANA1_SEL_Pos
|
||||
ANA_1,
|
||||
#endif
|
||||
// Specific pin name define in the variant
|
||||
#if __has_include("PinNamesVar.h")
|
||||
#include "PinNamesVar.h"
|
||||
#endif
|
||||
P_END = NC
|
||||
} PinName;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,221 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef _PINNAMESTYPES_H
|
||||
#define _PINNAMESTYPES_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* WCH PIN data as used in pin_function is coded on 32 bits as below
|
||||
* [1:0] MODE Input / Output / Output Speed
|
||||
* 00:input
|
||||
* 01:output,10MHz
|
||||
* 10:output,2MHz
|
||||
* 11:output,50MHz
|
||||
*
|
||||
* [3:2] CNF under input
|
||||
* 00:Analog input
|
||||
* 01:float input
|
||||
* 10:input with PU/PD
|
||||
* 11:reserved
|
||||
* CNF under output
|
||||
* 00:output PP
|
||||
* 01:output OD
|
||||
* 10:output AF_PP
|
||||
* 11:output AF_OD
|
||||
*
|
||||
* [5:4] input PUPD config
|
||||
* 00:No Pull
|
||||
* 01:Pull-up
|
||||
* 10:Pull-Down
|
||||
* 11:reserved
|
||||
* [7:6] reserved
|
||||
*
|
||||
* [14:8] AF_NUM 0-0x7F
|
||||
*
|
||||
* [19:15] Channel(Analog/Timer) 0-31
|
||||
*
|
||||
* [31:20] Reserved
|
||||
*
|
||||
*
|
||||
*
|
||||
* */
|
||||
|
||||
|
||||
#define CH_PIN_MODE_MASK 0x03
|
||||
#define CH_PIN_MODE_SHIFT 0
|
||||
#define CH_PIN_MODE_BITS (CH_PIN_MODE_MASK << CH_PIN_MODE_SHIFT)
|
||||
|
||||
#define CH_PIN_CNF_MASK 0x03
|
||||
#define CH_PIN_CNF_SHIFT 2
|
||||
#define CH_PIN_CNF_BITS (CH_PIN_CNF_MASK << CH_PIN_CNF_SHIFT)
|
||||
|
||||
#define CH_PIN_PUPD_MASK 0x03
|
||||
#define CH_PIN_PUPD_SHIFT 4
|
||||
#define CH_PIN_PUPD_BITS (CH_PIN_PUPD_MASK << CH_PIN_PUPD_SHIFT)
|
||||
|
||||
#define CH_PIN_AFNUM_MASK 0x7F
|
||||
#define CH_PIN_AFNUM_SHIFT 8
|
||||
#define CH_PIN_AFNUM_BITS (CH_PIN_AFNUM_MASK << CH_PIN_AFNUM_SHIFT)
|
||||
|
||||
|
||||
#define CH_PIN_CHAN_MASK 0x1F
|
||||
#define CH_PIN_CHAN_SHIFT 15
|
||||
#define CH_PIN_CHANNEL_BIT (CH_PIN_CHAN_MASK << CH_PIN_CHAN_SHIFT)
|
||||
|
||||
/*
|
||||
#define CH_PIN_FUNCTION_MASK 0x07
|
||||
#define CH_PIN_FUNCTION_SHIFT 0
|
||||
#define CH_PIN_FUNCTION_BITS (CH_PIN_FUNCTION_MASK << CH_PIN_FUNCTION_SHIFT)
|
||||
|
||||
#define CH_PIN_OD_MASK 0x01
|
||||
#define CH_PIN_OD_SHIFT 3
|
||||
#define CH_PIN_OD_BITS (CH_PIN_OD_MASK << CH_PIN_OD_SHIFT)
|
||||
|
||||
|
||||
#define CH_PIN_SPEED_MASK 0x03
|
||||
#define CH_PIN_SPEED_SHIFT 6
|
||||
#define CH_PIN_SPEED_BITS (CH_PIN_SPEED_MASK << CH_PIN_SPEED_SHIFT)
|
||||
|
||||
#define CH_PIN_INV_MASK 0x01
|
||||
#define CH_PIN_INV_SHIFT 20
|
||||
#define CH_PIN_INV_BIT (CH_PIN_INV_MASK << CH_PIN_INV_SHIFT)
|
||||
|
||||
#define CH_PIN_AN_CTRL_MASK 0x01
|
||||
#define CH_PIN_AN_CTRL_SHIFT 21
|
||||
#define CH_PIN_ANALOG_CONTROL_BIT (CH_PIN_AN_CTRL_MASK << CH_PIN_AN_CTRL_SHIFT)
|
||||
|
||||
#define CH_PIN_AN_CHAN_BANK_B_MASK 0x01
|
||||
#define CH_PIN_AN_CHAN_BANK_B_SHIFT 22
|
||||
#define CH_PIN_ANALOG_CHAN_BANK_B_BIT (CH_PIN_AN_CHAN_BANK_B_MASK << CH_PIN_AN_CHAN_BANK_B_SHIFT)
|
||||
*/
|
||||
|
||||
#define CH_PIN_MODE(x) (((x) >> CH_PIN_MODE_SHIFT) & CH_PIN_MODE_MASK)
|
||||
#define CH_PIN_CNF(x) (((x) >> CH_PIN_CNF_SHIFT) & CH_PIN_CNF_MASK)
|
||||
#define CH_PIN_PUPD(x) (((x) >> CH_PIN_PUPD_SHIFT) & CH_PIN_PUPD_MASK)
|
||||
#define CH_PIN_AFNUM(x) (((x) >> CH_PIN_AFNUM_SHIFT) & CH_PIN_AFNUM_MASK)
|
||||
#define CH_PIN_CHANNEL(X) (((X) >> CH_PIN_CHAN_SHIFT) & CH_PIN_CHAN_MASK)
|
||||
|
||||
/*
|
||||
#define CH_PIN_FUNCTION(X) (((X) >> CH_PIN_FUNCTION_SHIFT) & CH_PIN_FUNCTION_MASK)
|
||||
#define CH_PIN_OD(X) (((X) >> CH_PIN_OD_SHIFT) & CH_PIN_OD_MASK)
|
||||
#define CH_PIN_PUPD(X) (((X) >> CH_PIN_PUPD_SHIFT) & CH_PIN_PUPD_MASK)
|
||||
#define CH_PIN_SPEED(X) (((X) >> CH_PIN_SPEED_SHIFT) & CH_PIN_SPEED_MASK)
|
||||
#define CH_PIN_AFNUM(X) (((X) >> CH_PIN_AFNUM_SHIFT) & CH_PIN_AFNUM_MASK)
|
||||
#define CH_PIN_CHANNEL(X) (((X) >> CH_PIN_CHAN_SHIFT) & CH_PIN_CHAN_MASK)
|
||||
#define CH_PIN_INVERTED(X) (((X) >> CH_PIN_INV_SHIFT) & CH_PIN_INV_MASK)
|
||||
#define CH_PIN_ANALOG_CONTROL(X) (((X) >> CH_PIN_AN_CTRL_SHIFT) & CH_PIN_AN_CTRL_MASK)
|
||||
#define CH_PIN_ANALOG_CHANNEL_BANK_B(X) (((X) >> CH_PIN_AN_CHAN_BANK_B_SHIFT) & CH_PIN_AN_CHAN_BANK_B_MASK)
|
||||
#define CH_PIN_MODE(X) ((CH_PIN_OD((X)) << 4) | \
|
||||
(CH_PIN_FUNCTION((X)) & (~CH_PIN_OD_BITS)))
|
||||
*/
|
||||
|
||||
|
||||
#define CH_PIN_DEFINE(MODE, CNF, PUPD, AFNUM) ((int)(MODE) |\
|
||||
((CNF & CH_PIN_CNF_MASK) << CH_PIN_CNF_SHIFT) |\
|
||||
((PUPD & CH_PIN_PUPD_MASK) << CH_PIN_PUPD_SHIFT) |\
|
||||
((AFNUM & CH_PIN_AFNUM_MASK) << CH_PIN_AFNUM_SHIFT))
|
||||
|
||||
|
||||
#define CH_PIN_DEFINE_EXT(MODE, CNF, PUPD, AFNUM, CHAN) ((int)(MODE) |\
|
||||
((CNF & CH_PIN_CNF_MASK) << CH_PIN_CNF_SHIFT) |\
|
||||
((PUPD & CH_PIN_PUPD_MASK) << CH_PIN_PUPD_SHIFT) |\
|
||||
((AFNUM & CH_PIN_AFNUM_MASK) << CH_PIN_AFNUM_SHIFT) |\
|
||||
((CHAN & CH_PIN_CHAN_MASK) << CH_PIN_CHAN_SHIFT))
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
#define CH_PIN_DEFINE(FUNC_OD, PUPD, AFNUM) ((int)(FUNC_OD) |\
|
||||
((PUPD & CH_PIN_PUPD_MASK) << CH_PIN_PUPD_SHIFT) |\
|
||||
((AFNUM & CH_PIN_AFNUM_MASK) << CH_PIN_AFNUM_SHIFT))
|
||||
|
||||
#define CH_PIN_DEFINE_EXT(FUNC_OD, PUPD, AFNUM, CHAN, INV) \
|
||||
((int)(FUNC_OD) |\
|
||||
((PUPD & CH_PIN_PUPD_MASK) << CH_PIN_PUPD_SHIFT) |\
|
||||
((AFNUM & CH_PIN_AFNUM_MASK) << CH_PIN_AFNUM_SHIFT) |\
|
||||
((CHAN & CH_PIN_CHAN_MASK) << CH_PIN_CHAN_SHIFT) |\
|
||||
((INV & CH_PIN_INV_MASK) << CH_PIN_INV_SHIFT))
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* MACROS to support the legacy definition of PIN formats
|
||||
* The CH_MODE_ defines contain the function and the Push-pull/OpenDrain
|
||||
* configuration (legacy inheritance).
|
||||
*/
|
||||
#define CH_PIN_DATA(MODE, CNF, PUPD, AFNUM) \
|
||||
CH_PIN_DEFINE(MODE, CNF, PUPD, AFNUM)
|
||||
#define CH_PIN_DATA_EXT(MODE, CNF, PUPD, AFNUM, CHAN) \
|
||||
CH_PIN_DEFINE_EXT(MODE, CNF, PUPD, AFNUM, CHAN)
|
||||
|
||||
|
||||
/*
|
||||
typedef enum {
|
||||
CH_PIN_INPUT = 0,
|
||||
CH_PIN_OUTPUT = 1,
|
||||
CH_PIN_ALTERNATE = 2,
|
||||
CH_PIN_ANALOG = 3,
|
||||
} CHPinFunction;
|
||||
|
||||
#define CH_MODE_INPUT (CH_PIN_INPUT)
|
||||
#define CH_MODE_OUTPUT_PP (CH_PIN_OUTPUT)
|
||||
#define CH_MODE_OUTPUT_OD (CH_PIN_OUTPUT | CH_PIN_OD_BITS)
|
||||
#define CH_MODE_AF_PP (CH_PIN_ALTERNATE)
|
||||
#define CH_MODE_AF_OD (CH_PIN_ALTERNATE | CH_PIN_OD_BITS)
|
||||
#define CH_MODE_ANALOG (CH_PIN_ANALOG)
|
||||
#define CH_MODE_ANALOG_ADC_CONTROL (CH_PIN_ANALOG | CH_PIN_ANALOG_CONTROL_BIT)
|
||||
#define CH_MODE_ANALOG_ADC_CHANNEL_BANK_B (CH_PIN_ANALOG | CH_PIN_ANALOG_CHAN_BANK_B_BIT)
|
||||
*/
|
||||
|
||||
/* MODE */
|
||||
#define CH_MODE_INPUT (0)
|
||||
#define CH_MODE_OUTPUT_10MHz (1)
|
||||
#define CH_MODE_OUTPUT_2MHz (2)
|
||||
#define CH_MODE_OUTPUT_50MHz (3)
|
||||
/* CNF */
|
||||
#define CH_CNF_INPUT_ANALOG (0)
|
||||
#define CH_CNF_INPUT_FLOAT (1)
|
||||
#define CH_CNF_INPUT_PUPD (2)
|
||||
#define CH_CNF_OUTPUT_PP (0)
|
||||
#define CH_CNF_OUTPUT_OD (1)
|
||||
#define CH_CNF_OUTPUT_AFPP (2)
|
||||
#define CH_CNF_OUTPUT_AFOD (3)
|
||||
|
||||
/* PUPD */
|
||||
#define NOPULL (0)
|
||||
#define PULLUP (1)
|
||||
#define PILLDOWN (2)
|
||||
|
||||
// High nibble = port number (FirstPort <= PortName <= LastPort)
|
||||
// Low nibble = pin number
|
||||
#define CH_PORT(X) (((uint32_t)(X) >> 4) & 0xF)
|
||||
#define CH_PIN(X) ((uint32_t)(X) & 0xF)
|
||||
|
||||
// Check PinName is valid: FirstPort <= PortName <= LastPort
|
||||
// As FirstPort is equal to 0 and CH_PORT cast as an unsigned
|
||||
// (CH_PORT(X) >= FirstPort) is always true
|
||||
//#define CH_VALID_PINNAME(X) ((CH_PORT(X) >= FirstPort) && (CH_PORT(X) <= LastPort))
|
||||
#define CH_VALID_PINNAME(X) (CH_PORT(X) <= LastPort)
|
||||
|
||||
#define CH_GPIO_PIN(X) ((uint16_t)(1<<CH_PIN(X)))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2017, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
#include "PortNames.h"
|
||||
|
||||
GPIO_TypeDef *GPIOPort[MAX_NB_PORT] = {
|
||||
GPIOA
|
||||
#if defined PGIOB_BASE
|
||||
, GPIOB
|
||||
#endif
|
||||
#if defined GPIOC_BASE
|
||||
, GPIOC
|
||||
#endif
|
||||
#if defined GPIOD_BASE
|
||||
, GPIOD
|
||||
#endif
|
||||
#if defined GPIOE_BASE
|
||||
, GPIOE
|
||||
#endif
|
||||
#if defined GPIOF_BASE
|
||||
, GPIOF
|
||||
#endif
|
||||
#if defined GPIOG_BASE
|
||||
, GPIOG
|
||||
#endif
|
||||
#if defined GPIOH_BASE
|
||||
, GPIOH
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Enable GPIO clock and return GPIO base address */
|
||||
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx)
|
||||
{
|
||||
GPIO_TypeDef *gpioPort = 0;
|
||||
switch (port_idx) {
|
||||
case PortA:
|
||||
gpioPort = GPIOA;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||||
break;
|
||||
#if defined GPIOB_BASE
|
||||
case PortB:
|
||||
gpioPort = GPIOB;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOC_BASE
|
||||
case PortC:
|
||||
gpioPort = GPIOC;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOD_BASE
|
||||
case PortD:
|
||||
gpioPort = GPIOD;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOE_BASE
|
||||
case PortE:
|
||||
gpioPort = GPIOE;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOF_BASE
|
||||
case PortF:
|
||||
gpioPort = GPIOF;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOG_BASE
|
||||
case PortG:
|
||||
gpioPort = GPIOG;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
#if defined GPIOH_BASE
|
||||
case PortH:
|
||||
gpioPort = GPIOH;
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOH, ENABLE);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
// wrong port number
|
||||
//TBD: error management
|
||||
gpioPort = 0;
|
||||
break;
|
||||
}
|
||||
return gpioPort;
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2016, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
#ifndef _PORTNAMES_H
|
||||
#define _PORTNAMES_H
|
||||
|
||||
#include "ch32_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern GPIO_TypeDef *GPIOPort[];
|
||||
|
||||
typedef enum {
|
||||
FirstPort = 0x00,
|
||||
PortA = FirstPort,
|
||||
#if defined GPIOB_BASE
|
||||
PortB,
|
||||
#endif
|
||||
#if defined GPIOC_BASE
|
||||
PortC,
|
||||
#endif
|
||||
#if defined GPIOD_BASE
|
||||
PortD,
|
||||
#endif
|
||||
#if defined GPIOE_BASE
|
||||
PortE,
|
||||
#endif
|
||||
#if defined GPIOF_BASE
|
||||
PortF,
|
||||
#endif
|
||||
#if defined GPIOG_BASE
|
||||
PortG,
|
||||
#endif
|
||||
#if defined GPIOH_BASE
|
||||
PortH,
|
||||
#endif
|
||||
#if defined GPIOI_BASE
|
||||
PortI,
|
||||
#endif
|
||||
#if defined GPIOJ_BASE
|
||||
PortJ,
|
||||
#endif
|
||||
#if defined GPIOK_BASE
|
||||
PortK,
|
||||
#endif
|
||||
#if defined GPIOZ_BASE
|
||||
PortZ,
|
||||
#endif
|
||||
PortEND,
|
||||
LastPort = PortEND - 1
|
||||
} PortName;
|
||||
|
||||
#define MAX_NB_PORT (LastPort-FirstPort+1)
|
||||
|
||||
/* Return GPIO base address */
|
||||
#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)NULL)
|
||||
/* Enable GPIO clock and return GPIO base address */
|
||||
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -0,0 +1,660 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "analog.h"
|
||||
#include "ch32yyxx_adc.h"
|
||||
#include "PinAF_ch32yyxx.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Private_Variables */
|
||||
#if (defined(ADC_MODULE_ENABLED) && !defined(ADC_MODULE_ONLY)) ||\
|
||||
(defined(DAC_MODULE_ENABLED) && !defined(DAC_MODULE_ONLY))
|
||||
static PinName g_current_pin = NC;
|
||||
static int calibration_value = 0;
|
||||
#endif
|
||||
|
||||
|
||||
/* Private_Defines */
|
||||
#if defined(ADC_MODULE_ENABLED) && !defined(ADC_MODULE_ONLY)
|
||||
|
||||
#if !defined(CH32V00x)
|
||||
|
||||
/* Default to use maximum sampling period */
|
||||
#ifndef ADC_SAMPLINGTIME
|
||||
#if defined(ADC_SampleTime_239Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_239Cycles5;
|
||||
#elif defined(ADC_SampleTime_71Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_71Cycles5;
|
||||
#elif defined(ADC_SampleTime_55Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_55Cycles5;
|
||||
#elif defined(ADC_SampleTime_41Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_41Cycles5;
|
||||
#elif defined(ADC_SampleTime_28Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_28Cycles5;
|
||||
#elif defined(ADC_SampleTime_13Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_13Cycles5;
|
||||
#elif defined(ADC_SampleTime_7Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_7Cycles5;
|
||||
#elif defined(ADC_SampleTime_1Cycles5)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_1Cycles5
|
||||
#endif
|
||||
#endif /* !ADC_SAMPLINGTIME */
|
||||
|
||||
/*
|
||||
* Default to use maximum sampling period
|
||||
*/
|
||||
#ifndef ADC_SAMPLINGTIME_INTERNAL
|
||||
#if defined(ADC_SampleTime_239Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_239Cycles5
|
||||
#elif defined(ADC_SampleTime_71Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_71Cycles5
|
||||
#elif defined(ADC_SampleTime_55Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_55Cycles5
|
||||
#elif defined(ADC_SampleTime_41Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_41Cycles5
|
||||
#elif defined(ADC_SampleTime_28Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_28Cycles5
|
||||
#elif defined(ADC_SampleTime_13Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_13Cycles5
|
||||
#elif defined(ADC_SampleTime_7Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_7Cycles5
|
||||
#elif defined(ADC_SampleTime_1Cycles5)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_1Cycles5
|
||||
#else
|
||||
#error "ADC sampling time could not be defined for internal channels!"
|
||||
#endif
|
||||
#endif /* !ADC_SAMPLINGTIME_INTERNAL */
|
||||
|
||||
#ifndef ADC_CLOCK_DIV
|
||||
#ifdef RCC_PCLK2_Div8
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div8
|
||||
#elif defined(RCC_PCLK2_Div6)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div6
|
||||
#elif defined(RCC_PCLK2_Div4)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div4
|
||||
#elif defined(RCC_PCLK2_Div2)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div2
|
||||
#endif
|
||||
#endif /* !ADC_CLOCK_DIV */
|
||||
|
||||
#else
|
||||
|
||||
/* Default to use maximum sampling period */
|
||||
#ifndef ADC_SAMPLINGTIME
|
||||
#if defined(ADC_SampleTime_241Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_241Cycles;
|
||||
#elif defined(ADC_SampleTime_73Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_73Cycles;
|
||||
#elif defined(ADC_SampleTime_57Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_57Cycles;
|
||||
#elif defined(ADC_SampleTime_43Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_43Cycles;
|
||||
#elif defined(ADC_SampleTime_30Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_30Cycles;
|
||||
#elif defined(ADC_SampleTime_15Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_15Cycles;
|
||||
#elif defined(ADC_SampleTime_9Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_9Cycles;
|
||||
#elif defined(ADC_SampleTime_3Cycles)
|
||||
#define ADC_SAMPLINGTIME ADC_SampleTime_3Cycles
|
||||
#endif
|
||||
#endif /* !ADC_SAMPLINGTIME */
|
||||
|
||||
/*
|
||||
* Default to use maximum sampling period
|
||||
*/
|
||||
#ifndef ADC_SAMPLINGTIME_INTERNAL
|
||||
#if defined(ADC_SampleTime_241Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_241Cycles
|
||||
#elif defined(ADC_SampleTime_73Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_73Cycles
|
||||
#elif defined(ADC_SampleTime_57Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_57Cycles
|
||||
#elif defined(ADC_SampleTime_43Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_43Cycles
|
||||
#elif defined(ADC_SampleTime_30Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_30Cycles
|
||||
#elif defined(ADC_SampleTime_15Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_15Cycles
|
||||
#elif defined(ADC_SampleTime_9Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_9Cycles
|
||||
#elif defined(ADC_SampleTime_3Cycles)
|
||||
#define ADC_SAMPLINGTIME_INTERNAL ADC_SampleTime_3Cycles
|
||||
#else
|
||||
#error "ADC sampling time could not be defined for internal channels!"
|
||||
#endif
|
||||
#endif /* !ADC_SAMPLINGTIME_INTERNAL */
|
||||
|
||||
#ifndef ADC_CLOCK_DIV
|
||||
#ifdef RCC_PCLK2_Div8
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div8
|
||||
#elif defined(RCC_PCLK2_Div6)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div6
|
||||
#elif defined(RCC_PCLK2_Div4)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div4
|
||||
#elif defined(RCC_PCLK2_Div2)
|
||||
#define ADC_CLOCK_DIV RCC_PCLK2_Div2
|
||||
#endif
|
||||
#endif /* !ADC_CLOCK_DIV */
|
||||
|
||||
#endif /* !CH32V00x */
|
||||
|
||||
|
||||
#ifndef ADC_REGULAR_RANK_1
|
||||
#define ADC_REGULAR_RANK_1 1
|
||||
#endif
|
||||
|
||||
/* Exported Functions */
|
||||
/**
|
||||
* @brief Return ADC HAL channel linked to a PinName
|
||||
* @param pin: PinName
|
||||
* @retval Valid HAL channel
|
||||
*/
|
||||
uint32_t get_adc_channel(PinName pin)
|
||||
{
|
||||
uint32_t function = pinmap_function(pin, PinMap_ADC);
|
||||
uint32_t channel = 0;
|
||||
switch (CH_PIN_CHANNEL(function)) {
|
||||
#ifdef ADC_Channel_0
|
||||
case 0:
|
||||
channel = ADC_Channel_0;
|
||||
break;
|
||||
#endif
|
||||
case 1:
|
||||
channel = ADC_Channel_1;
|
||||
break;
|
||||
case 2:
|
||||
channel = ADC_Channel_2;
|
||||
break;
|
||||
case 3:
|
||||
channel = ADC_Channel_3;
|
||||
break;
|
||||
case 4:
|
||||
channel = ADC_Channel_4;
|
||||
break;
|
||||
case 5:
|
||||
channel = ADC_Channel_5;
|
||||
break;
|
||||
case 6:
|
||||
channel = ADC_Channel_6;
|
||||
break;
|
||||
case 7:
|
||||
channel = ADC_Channel_7;
|
||||
break;
|
||||
case 8:
|
||||
channel = ADC_Channel_8;
|
||||
break;
|
||||
case 9:
|
||||
channel = ADC_Channel_9;
|
||||
break;
|
||||
#ifdef ADC_Channel_10
|
||||
case 10:
|
||||
channel = ADC_Channel_10;
|
||||
break;
|
||||
case 11:
|
||||
channel = ADC_Channel_11;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_12
|
||||
case 12:
|
||||
channel = ADC_Channel_12;
|
||||
break;
|
||||
case 13:
|
||||
channel = ADC_Channel_13;
|
||||
break;
|
||||
case 14:
|
||||
channel = ADC_Channel_14;
|
||||
break;
|
||||
case 15:
|
||||
channel = ADC_Channel_15;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_16
|
||||
case 16:
|
||||
channel = ADC_Channel_16;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_17
|
||||
case 17:
|
||||
channel = ADC_Channel_17;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_18
|
||||
case 18:
|
||||
channel = ADC_Channel_18;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_19
|
||||
case 19:
|
||||
channel = ADC_Channel_19;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
_Error_Handler("ADC: Unknown adc channel", (int)(CH_PIN_CHANNEL(function)));
|
||||
break;
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return ADC HAL internal channel linked to a PinName
|
||||
* @param pin: specific PinName's for ADC internal. Value can be:
|
||||
* PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT
|
||||
* Note that not all of these values may be available for all series.
|
||||
* @retval Valid HAL internal channel.
|
||||
*/
|
||||
uint32_t get_adc_internal_channel(PinName pin)
|
||||
{
|
||||
uint32_t channel = 0;
|
||||
switch (pin) {
|
||||
#if defined(ADC_Channel_TempSensor)
|
||||
case PADC_TEMP:
|
||||
channel = ADC_Channel_TempSensor;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_Vrefint
|
||||
case PADC_VREF:
|
||||
channel = ADC_Channel_Vrefint;
|
||||
break;
|
||||
#endif
|
||||
#ifdef ADC_Channel_Vbat
|
||||
case PADC_VBAT:
|
||||
channel = ADC_CHANNEL_VBAT;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
_Error_Handler("ADC: Unknown adc internal PiName", (int)(pin));
|
||||
break;
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
#endif /* ADC_MODULE_ENABLED && !ADC_MODULE_ONLY */
|
||||
|
||||
|
||||
|
||||
#if defined(DAC_MODULE_ENABLED) && !defined(DAC_MODULE_ONLY)
|
||||
/**
|
||||
* @brief Return DAC HAL channel linked to a PinName
|
||||
* @param pin: specific PinName's for ADC internal.
|
||||
* @retval Valid channel
|
||||
*/
|
||||
uint32_t get_dac_channel(PinName pin)
|
||||
{
|
||||
uint32_t function = pinmap_function(pin, PinMap_DAC);
|
||||
uint32_t channel = 0;
|
||||
switch (CH_PIN_CHANNEL(function))
|
||||
{
|
||||
#ifdef DAC_Channel_1
|
||||
case 1:
|
||||
channel = DAC_Channel_1;
|
||||
break;
|
||||
#endif
|
||||
#ifdef DAC_Channel_2
|
||||
case 2:
|
||||
channel = DAC_Channel_2;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
_Error_Handler("DAC: Unknown dac channel", (int)(CH_PIN_CHANNEL(function)));
|
||||
break;
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
////////////////////////// DAC INTERFACE FUNCTIONS /////////////////////////////
|
||||
/**
|
||||
* @brief DAC Initialization
|
||||
* This function configures the hardware resources used in this example:
|
||||
* - Peripheral's clock enable
|
||||
* - Peripheral's GPIO Configuration
|
||||
* @param hdac: DAC pointer
|
||||
* @retval None
|
||||
*/
|
||||
void DAC_Clock_EN(DAC_TypeDef *dac)
|
||||
{
|
||||
/* DAC Periph clock enable */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
|
||||
/* Configure DAC GPIO pins */
|
||||
pinmap_pinout(g_current_pin, PinMap_DAC);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function will set the DAC to the required value
|
||||
* @param port : the gpio port to use
|
||||
* @param pin : the gpio pin to use
|
||||
* @param value : the value to push on the adc output
|
||||
* @param do_init : if set to 1 the initialization of the adc is done
|
||||
* @retval None
|
||||
*/
|
||||
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init)
|
||||
{
|
||||
DAC_TypeDef *pdac=NULL;
|
||||
DAC_InitTypeDef DAC_InitType = {0};
|
||||
|
||||
uint32_t dacChannel;
|
||||
|
||||
pdac = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);
|
||||
if (pdac == NP) {
|
||||
return;
|
||||
}
|
||||
dacChannel = get_dac_channel(pin);
|
||||
|
||||
if (do_init == 1)
|
||||
{
|
||||
/*##-1- Configure the DAC peripheral */
|
||||
g_current_pin = pin;
|
||||
|
||||
DAC_InitType.DAC_Trigger = DAC_Trigger_None;
|
||||
DAC_InitType.DAC_WaveGeneration = DAC_WaveGeneration_None;
|
||||
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
|
||||
DAC_InitType.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
|
||||
|
||||
/*##-2- Configure DAC channelx */
|
||||
DAC_Init(dacChannel, &DAC_InitType);
|
||||
|
||||
}
|
||||
/*##-3- Set DAC Channelx DHR register */
|
||||
switch (dacChannel)
|
||||
{
|
||||
case DAC_Channel_1:
|
||||
DAC_SetChannel1Data(DAC_Align_12b_R, value);
|
||||
break;
|
||||
case DAC_Channel_2:
|
||||
DAC_SetChannel2Data(DAC_Align_12b_R, value);
|
||||
break;
|
||||
default:
|
||||
DAC_SetChannel1Data(DAC_Align_12b_R, 0);
|
||||
DAC_SetChannel2Data(DAC_Align_12b_R, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
/*##-4- Enable DAC Channelx*/
|
||||
DAC_Cmd(dacChannel, ENABLE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function will stop the DAC
|
||||
* @param port : the gpio port to use
|
||||
* @param pin : the gpio pin to use
|
||||
* @retval None
|
||||
*/
|
||||
void dac_stop(PinName pin)
|
||||
{
|
||||
DAC_TypeDef *pdac=NULL;
|
||||
uint32_t dacChannel;
|
||||
|
||||
pdac = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);
|
||||
if (pdac == NP) {
|
||||
return;
|
||||
}
|
||||
dacChannel = get_dac_channel(pin);
|
||||
//disable channel
|
||||
DAC_Cmd(dacChannel, DISABLE);
|
||||
|
||||
DAC_DeInit( );
|
||||
}
|
||||
#endif //DAC_MODULE_ENABLED && !DAC_MODULE_ONLY
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(ADC_MODULE_ENABLED) && !defined(ADC_MODULE_ONLY)
|
||||
////////////////////////// ADC INTERFACE FUNCTIONS /////////////////////////////
|
||||
|
||||
/**
|
||||
* @brief ADC Initialization
|
||||
* This function configures the hardware resources used in this example:
|
||||
* - Peripheral's clock enable
|
||||
* - Peripheral's GPIO Configuration
|
||||
* @param padc: ADC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void ADC_Clock_EN(ADC_TypeDef *padc)
|
||||
{
|
||||
/*##-1- Enable peripherals and GPIO Clocks #################################*/
|
||||
/* ADC Periph clock enable */
|
||||
if (padc == ADC1)
|
||||
{
|
||||
#ifdef RCC_APB2Periph_ADC1
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE ); //ENABLE ADC1 Clock
|
||||
#endif
|
||||
}
|
||||
#ifdef ADC2
|
||||
else if(padc == ADC2)
|
||||
{
|
||||
#ifdef RCC_APB2Periph_ADC2
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2 , ENABLE ); //ENABLE ADC2 Clock
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
/* Configure ADC GPIO pin */
|
||||
if (!(g_current_pin & PADC_BASE))
|
||||
{
|
||||
pinmap_pinout(g_current_pin, PinMap_ADC);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief DeInitializes the ADC Moudle.
|
||||
* @param padc: ADC handle
|
||||
* @retval None
|
||||
*/
|
||||
void ADC_Stop(ADC_TypeDef *padc)
|
||||
{
|
||||
ADC_Cmd(padc,DISABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function will set the ADC to the required value
|
||||
* @param pin : the pin to use
|
||||
* @param resolution : resolution for converted data: 8/10/12
|
||||
* @retval the value of the adc
|
||||
*/
|
||||
uint16_t adc_read_value(PinName pin, uint32_t resolution)
|
||||
{
|
||||
|
||||
ADC_TypeDef *padc ;
|
||||
ADC_InitTypeDef ADC_InitStructure={0};
|
||||
|
||||
__IO uint16_t uhADCxConvertedValue = 0;
|
||||
uint32_t samplingTime = ADC_SAMPLINGTIME;
|
||||
uint32_t channel = 0;
|
||||
uint32_t bank = 0;
|
||||
|
||||
if ((pin & PADC_BASE) && (pin < ANA_START)) //internal channle
|
||||
{
|
||||
channel = get_adc_internal_channel(pin);
|
||||
samplingTime = ADC_SAMPLINGTIME_INTERNAL;
|
||||
padc = ADC1;
|
||||
}
|
||||
else
|
||||
{
|
||||
padc = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
|
||||
channel = get_adc_channel(pin);
|
||||
}
|
||||
|
||||
g_current_pin = pin;
|
||||
ADC_Clock_EN(padc);
|
||||
|
||||
#ifdef ADC_CLOCK_DIV
|
||||
RCC_ADCCLKConfig(ADC_CLOCK_DIV); /* (A)synchronous clock mode, input ADC clock divided */
|
||||
#endif
|
||||
#ifdef ADC_RESOLUTION_12B
|
||||
#else
|
||||
(void) resolution;
|
||||
#endif
|
||||
#ifdef ADC_DataAlign_Right
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* Right-alignment for converted data */
|
||||
#endif
|
||||
ADC_InitStructure.ADC_ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
|
||||
|
||||
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_NbrOfChannel = 1;
|
||||
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
||||
#if !defined(CH32V00x)
|
||||
ADC_InitStructure.ADC_OutputBuffer = ENABLE;
|
||||
#endif
|
||||
ADC_Init(padc, &ADC_InitStructure);
|
||||
padc->STATR = 0;
|
||||
ADC_Cmd(padc,ENABLE);
|
||||
|
||||
/*##-2- Configure ADC regular channel ######################################*/
|
||||
ADC_RegularChannelConfig(padc, channel, ADC_REGULAR_RANK_1, samplingTime );
|
||||
|
||||
#if defined(ADC_CTLR_ADCAL)
|
||||
/*##-2.1- Calibrate ADC then Start the conversion process ####################*/
|
||||
#if !defined(CH32V00x)
|
||||
ADC_BufferCmd(padc, DISABLE); //disable buffer
|
||||
#endif
|
||||
ADC_ResetCalibration(padc);
|
||||
while(ADC_GetResetCalibrationStatus(padc));
|
||||
ADC_StartCalibration(padc);
|
||||
while(ADC_GetCalibrationStatus(padc));
|
||||
calibration_value = Get_CalibrationValue(padc);
|
||||
ADC_BufferCmd(padc, ENABLE); //enable buffer
|
||||
#endif
|
||||
|
||||
/*##-3- Start the conversion process ####################*/
|
||||
ADC_SoftwareStartConvCmd(padc, ENABLE);
|
||||
/*##-4- Wait for the end of conversion #####################################*/
|
||||
/* For simplicity reasons, this example is just waiting till the end of the
|
||||
conversion, but application may perform other tasks while conversion
|
||||
operation is ongoing. */
|
||||
while(!ADC_GetFlagStatus(padc, ADC_FLAG_EOC ));
|
||||
uhADCxConvertedValue = padc->RDATAR;
|
||||
ADC_Stop(padc);
|
||||
ADC_DeInit(padc);
|
||||
|
||||
#if defined(ADC_CTLR_ADCAL)
|
||||
#if (ADC_RESOLUTION == 8)
|
||||
if(( calibration_value + uhADCxConvertedValue ) >= 255 )
|
||||
{
|
||||
return 255;
|
||||
}
|
||||
else if( ( calibration_value + uhADCxConvertedValue ) <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (uhADCxConvertedValue+calibration_value);
|
||||
}
|
||||
#endif
|
||||
#if (ADC_RESOLUTION == 10)
|
||||
if(( calibration_value + uhADCxConvertedValue ) >= 1023 )
|
||||
{
|
||||
return 1023;
|
||||
}
|
||||
else if( ( calibration_value + uhADCxConvertedValue ) <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (uhADCxConvertedValue+calibration_value);
|
||||
}
|
||||
#endif
|
||||
#if (ADC_RESOLUTION == 12)
|
||||
if(( calibration_value + uhADCxConvertedValue ) >= 4095 )
|
||||
{
|
||||
return 4095;
|
||||
}
|
||||
else if( ( calibration_value + uhADCxConvertedValue ) <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (uhADCxConvertedValue+calibration_value);
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
return uhADCxConvertedValue;
|
||||
#endif
|
||||
}
|
||||
#endif /* ADC_MODULE_ENABLED && !ADC_MODULE_ONLY*/
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(TIM_MODULE_ENABLED) && !defined(TIM_MODULE_ONLY)
|
||||
////////////////////////// PWM INTERFACE FUNCTIONS /////////////////////////////
|
||||
|
||||
/**
|
||||
* @brief This function will set the PWM to the required value
|
||||
* @param port : the gpio port to use
|
||||
* @param pin : the gpio pin to use
|
||||
* @param clock_freq : frequency of the tim clock
|
||||
* @param value : the value to push on the PWM output
|
||||
* @retval None
|
||||
*/
|
||||
void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareFormat_t resolution)
|
||||
{
|
||||
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);
|
||||
HardwareTimer *HT;
|
||||
TimerModes_t previousMode;
|
||||
uint32_t index = get_timer_index(Instance);
|
||||
if (HardwareTimer_Handle[index] == NULL) {
|
||||
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));
|
||||
}
|
||||
|
||||
HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
||||
|
||||
uint32_t channel = CH_PIN_CHANNEL(pinmap_function(pin, PinMap_TIM));
|
||||
|
||||
previousMode = HT->getMode(channel);
|
||||
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
|
||||
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
|
||||
}
|
||||
HT->setOverflow(PWM_freq, HERTZ_FORMAT);
|
||||
HT->setCaptureCompare(channel, value, resolution);
|
||||
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {
|
||||
HT->resume();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief This function will disable the PWM
|
||||
* @param port : the gpio port to use
|
||||
* @param pin : the gpio pin to use
|
||||
* @retval None
|
||||
*/
|
||||
void pwm_stop(PinName pin)
|
||||
{
|
||||
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);
|
||||
HardwareTimer *HT;
|
||||
uint32_t index = get_timer_index(Instance);
|
||||
if (HardwareTimer_Handle[index] == NULL) {
|
||||
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));
|
||||
}
|
||||
|
||||
HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
||||
if (HT != NULL) {
|
||||
delete (HT);
|
||||
HT = NULL;
|
||||
}
|
||||
}
|
||||
#endif /* TIM_MODULE_ENABLED && !TIM_MODULE_ONLY */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file analog.h
|
||||
* @author WI6LABS
|
||||
* @version V1.0.0
|
||||
* @date 01-August-2016
|
||||
* @brief Header for analog module
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __ANALOG_H
|
||||
#define __ANALOG_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ch32_def.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "variant.h"
|
||||
|
||||
// #include "HardwareTimer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
#if defined(ADC_MODULE_ENABLED) && !defined(ADC_MODULE_ONLY)
|
||||
uint32_t get_adc_channel(PinName pin);
|
||||
uint32_t get_adc_internal_channel(PinName pin);
|
||||
uint16_t adc_read_value(PinName pin, uint32_t resolution);
|
||||
|
||||
#endif
|
||||
#if defined(DAC_MODULE_ENABLED) && !defined(DAC_MODULE_ONLY)
|
||||
uint32_t get_dac_channel(PinName pin);
|
||||
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init);
|
||||
void dac_stop(PinName pin);
|
||||
#endif
|
||||
#if defined(TIM_MODULE_ENABLED) && !defined(TIM_MODULE_ONLY)
|
||||
void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
|
||||
void pwm_stop(PinName pin);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __ANALOG_H */
|
||||
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __BACKUP_H
|
||||
#define __BACKUP_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ch32_def.h"
|
||||
#include "ch32yyxx_rtc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#if !defined(RTC_BKP_BASE) && defined(BKP_DR1)
|
||||
#define RTC_BKP_BASE BKP_DR1
|
||||
#else
|
||||
#define RTC_BKP_BASE 0
|
||||
#endif
|
||||
#ifndef RTC_BKP_VALUE
|
||||
#define RTC_BKP_VALUE 0x4348
|
||||
#endif
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
static inline void resetBackupDomain(void)
|
||||
{
|
||||
#ifdef PWR_MODULE_ENABLED
|
||||
PWR_BackupAccessCmd(ENABLE);
|
||||
#endif
|
||||
RCC_BackupResetCmd(ENABLE);
|
||||
RCC_BackupResetCmd(DISABLE);
|
||||
}
|
||||
|
||||
static inline void enableBackupDomain(void)
|
||||
{
|
||||
#ifdef PWR_MODULE_ENABLED
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
|
||||
/* Allow access to Backup domain */
|
||||
PWR_BackupAccessCmd(ENABLE);
|
||||
#endif
|
||||
#ifdef RCC_APB1Periph_BKP
|
||||
/* Enable BKP CLK for backup registers */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void disableBackupDomain(void)
|
||||
{
|
||||
#ifdef PWR_MODULE_ENABLED
|
||||
/* Forbid access to Backup domain */
|
||||
PWR_BackupAccessCmd(DISABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, DISABLE);
|
||||
#endif
|
||||
#ifdef RCC_APB1Periph_BKP
|
||||
/* Disable BKP CLK for backup registers */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, DISABLE);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void setBackupRegister(uint32_t index, uint32_t value)
|
||||
{
|
||||
#if defined(BKP_BASE)
|
||||
BKP_WriteBackupRegister(index*4 + RTC_BKP_BASE, value);
|
||||
#else
|
||||
(void)(index);
|
||||
(void)(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline uint32_t getBackupRegister(uint32_t index)
|
||||
{
|
||||
#if defined(BKP_BASE)
|
||||
return BKP_ReadBackupRegister(index*4 + RTC_BKP_BASE);
|
||||
#else
|
||||
(void)(index);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __BACKUP_H */
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef _BOOTLOADER_H_
|
||||
#define _BOOTLOADER_H_
|
||||
|
||||
/* Ensure DTR_TOGGLING_SEQ enabled */
|
||||
#if defined(BL_LEGACY_LEAF) || defined(BL_HID)
|
||||
#ifndef DTR_TOGGLING_SEQ
|
||||
#define DTR_TOGGLING_SEQ
|
||||
#endif /* DTR_TOGGLING_SEQ || BL_HID */
|
||||
#endif /* BL_LEGACY_LEAF */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* _BOOTLOADER_H_ */
|
|
@ -0,0 +1,25 @@
|
|||
#include "ch32_def.h"
|
||||
#include "core_debug.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
#if !defined(NDEBUG)
|
||||
WEAK void _Error_Handler(const char *msg, int val)
|
||||
{
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
core_debug("Error: %s (%i)\n", msg, val);
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,88 @@
|
|||
#ifndef _CH32_DEF_
|
||||
#define _CH32_DEF_
|
||||
|
||||
|
||||
/**
|
||||
* @brief CH32V core version number
|
||||
*/
|
||||
#define CH32_CORE_VERSION_MAJOR (0x01U) /*!< [31:24] major version */
|
||||
#define CH32_CORE_VERSION_MINOR (0x00U) /*!< [23:16] minor version */
|
||||
#define CH32_CORE_VERSION_PATCH (0x00U) /*!< [15:8] patch version */
|
||||
/*
|
||||
* Extra label for development:
|
||||
* 0: official release
|
||||
* [1-9]: release candidate
|
||||
* F[0-9]: development
|
||||
*/
|
||||
#define CH32_CORE_VERSION_EXTRA (0x00U) /*!< [7:0] extra version */
|
||||
#define CH32_CORE_VERSION ((CH32_CORE_VERSION_MAJOR << 24U)\
|
||||
|(CH32_CORE_VERSION_MINOR << 16U)\
|
||||
|(CH32_CORE_VERSION_PATCH << 8U )\
|
||||
|(CH32_CORE_VERSION_EXTRA))
|
||||
|
||||
|
||||
/*
|
||||
*All defined
|
||||
*/
|
||||
#if defined(CH32V20x) || defined(CH32V203xB) || defined(CH32V208)
|
||||
#include "ch32v20x.h"
|
||||
#elif defined(CH32V30x)
|
||||
#include "ch32v30x.h"
|
||||
#elif defined(CH32V10x)
|
||||
#include "ch32v10x.h"
|
||||
#elif defined(CH32V00x)
|
||||
#include "ch32v00x.h"
|
||||
#else
|
||||
#error "CH32YYXX chip series is not defined in boards.txt."
|
||||
#endif
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU SystemCoreClock
|
||||
#endif
|
||||
|
||||
// Here define some compatibility
|
||||
#ifndef ADC1
|
||||
#define ADC1 ADC
|
||||
#endif
|
||||
#ifndef CAN1
|
||||
#define CAN1 CAN
|
||||
#endif
|
||||
#ifndef DAC1
|
||||
#define DAC1 DAC //two independent channels
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Libc porting layers
|
||||
*/
|
||||
#if defined ( __GNUC__ ) /* GCC CS3 */
|
||||
#define WEAK __attribute__ ((weak))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
|
||||
#if defined(NDEBUG)
|
||||
#if !defined(_Error_Handler)
|
||||
#define _Error_Handler(str, value) \
|
||||
while (1) {\
|
||||
}
|
||||
#endif
|
||||
#if !defined(Error_Handler)
|
||||
#define Error_Handler() \
|
||||
while (1) {\
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
void _Error_Handler(const char *, int);
|
||||
|
||||
#define Error_Handler() _Error_Handler(__FILE__, __LINE__)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif //_CH32_DEF_
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef _CH32_DEF_BUILD_
|
||||
#define _CH32_DEF_BUILD_
|
||||
|
||||
#if !defined(COM_STARTUP_FILE)
|
||||
#if defined(CH32V00x)
|
||||
#define COM_STARTUP_FILE "startup_ch32v00x.S"
|
||||
#elif defined(CH32V203)
|
||||
#define COM_STARTUP_FILE "startup_ch32v20x_D6.S"
|
||||
#define CH32V20x_D6
|
||||
#elif defined(CH32V203xB)
|
||||
#define COM_STARTUP_FILE "startup_ch32v20x_D8.S"
|
||||
#define CH32V20x_D8
|
||||
#elif defined(CH32V208)
|
||||
#define COM_STARTUP_FILE "startup_ch32v20x_D8W.S"
|
||||
#define CH32V20x_D8W
|
||||
#else
|
||||
#error "Unknow chip!"
|
||||
#endif
|
||||
#else
|
||||
#warning "No startup file defined !"
|
||||
#endif /* CHIP Define */
|
||||
#endif /* _CH32_DEF_BUILD_ */
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "backup.h"
|
||||
#include "clock.h"
|
||||
#include "core_riscv_ch32yyxx.h"
|
||||
#include "ch32yyxx_rcc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TICK_FREQ_1KHz 1L
|
||||
// #define TICK_FREQ_100Hz 10L
|
||||
// #define TICK_FREQ_10Hz 100L
|
||||
|
||||
|
||||
|
||||
|
||||
__IO uint64_t msTick=0;
|
||||
WEAK uint64_t GetTick(void)
|
||||
{
|
||||
return msTick;
|
||||
}
|
||||
|
||||
#if defined(CH32V20x) || defined(CH32V30x) || defined(CH32V00x)
|
||||
|
||||
uint32_t getCurrentMicros(void)
|
||||
{
|
||||
|
||||
uint64_t m0 = GetTick();
|
||||
__IO uint64_t u0 = SysTick->CNT;
|
||||
uint64_t m1 = GetTick();
|
||||
__IO uint32_t u1 = SysTick->CNT; //may be a interruption
|
||||
uint64_t tms = SysTick->CMP + 1;
|
||||
|
||||
if (m1 != m0) {
|
||||
return (m1 * 1000 + ((tms - u1) * 1000) / tms);
|
||||
} else {
|
||||
return (m0 * 1000 + ((tms - u0) * 1000) / tms);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function called wto read the current millisecond
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
uint32_t getCurrentMillis(void)
|
||||
{
|
||||
return GetTick();
|
||||
}
|
||||
|
||||
void osSystickHandler() __attribute__((weak, alias("noOsSystickHandler")));
|
||||
void noOsSystickHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SysTick_Handler
|
||||
*
|
||||
* @brief This function handles systick interrupt.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
msTick+=TICK_FREQ_1KHz;
|
||||
osSystickHandler();
|
||||
SysTick->SR = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for 10x serils (Qingke V3A)
|
||||
#if defined (CH32V10x)
|
||||
/*********************************************************************
|
||||
* @fn SysTick_Handler
|
||||
*
|
||||
* @brief This function handles systick interrupt.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
msTick+=TICK_FREQ_1KHz;
|
||||
//Further inprovement is needed
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __CLOCK_H
|
||||
#define __CLOCK_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ch32_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
uint32_t getCurrentMillis(void);
|
||||
uint32_t getCurrentMicros(void);
|
||||
|
||||
// void configIPClock(void);
|
||||
// void enableClock(sourceClock_t source);
|
||||
// void configHSECapacitorTuning(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __CLOCK_H */
|
||||
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file core_callback.h
|
||||
* @author WI6LABS
|
||||
* @version V1.0.0
|
||||
* @date 8-September-2017
|
||||
* @brief Header for callback methods.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __CALLBACK_H
|
||||
#define __CALLBACK_H
|
||||
#if defined(CORE_CALLBACK)
|
||||
#include "variant.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#ifndef CALLBACK_LIST_SIZE
|
||||
#define CALLBACK_LIST_SIZE 4
|
||||
#endif
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void registerCoreCallback(void (*func)(void));
|
||||
void unregisterCoreCallback(void (*func)(void));
|
||||
void CoreCallback(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // CORE_CALLBACK
|
||||
#endif /* __CALLBACK_H */
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file digital_io.h
|
||||
* @brief Header for digital_io module
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __DIGITAL_IO_H
|
||||
#define __DIGITAL_IO_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "wiring_constants.h"
|
||||
#include "PinNames.h"
|
||||
#include "pinmap.h"
|
||||
#include "ch32yyxx_gpio.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
/**
|
||||
* @brief This function set a value to an IO
|
||||
* @param port : one of the gpio port
|
||||
* @param pin : one of the gpio pin
|
||||
* @param val : 0 to set to low, any other value to set to high
|
||||
* @retval None
|
||||
*/
|
||||
static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
|
||||
{
|
||||
if (val) {
|
||||
GPIO_SetBits(port,pin);
|
||||
}
|
||||
else {
|
||||
GPIO_ResetBits(port,pin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function read the value of an IO
|
||||
* @param port : one of the gpio port
|
||||
* @param pin : one of the gpio pin
|
||||
* @retval The pin state (LOW or HIGH)
|
||||
*/
|
||||
static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
|
||||
{
|
||||
return GPIO_ReadInputDataBit(port, pin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function toggle value of an IO
|
||||
* @param port : one of the gpio port
|
||||
* @param pin : one of the gpio pin
|
||||
* @retval None
|
||||
*/
|
||||
static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin)
|
||||
{
|
||||
uint32_t odr = port->OUTDR;
|
||||
port->BSHR = ((odr & pin) << 16u) | (~odr & pin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function set a value to an IO
|
||||
* @param pn : Pin name
|
||||
* @param val : 0 to set to low, any other value to set to high
|
||||
* @retval None
|
||||
*/
|
||||
static inline void digitalWriteFast(PinName pn, uint32_t ulVal)
|
||||
{
|
||||
digital_io_write(get_GPIO_Port(CH_PORT(pn)), CH_GPIO_PIN(pn), ulVal);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function read the value of an IO
|
||||
* @param pn : Pin name
|
||||
* @retval The pin state (LOW or HIGH)
|
||||
*/
|
||||
static inline int digitalReadFast(PinName pn)
|
||||
{
|
||||
uint8_t level = 0;
|
||||
level = digital_io_read(get_GPIO_Port(CH_PORT(pn)), CH_GPIO_PIN(pn));
|
||||
return (level) ? HIGH : LOW;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function toggle value of an IO
|
||||
* @param port : one of the gpio port
|
||||
* @param pin : one of the gpio pin
|
||||
* @retval None
|
||||
*/
|
||||
static inline void digitalToggleFast(PinName pn)
|
||||
{
|
||||
digital_io_toggle(get_GPIO_Port(CH_PORT(pn)), CH_GPIO_PIN(pn));
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DIGITAL_IO_H */
|
||||
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
|
||||
#include "hw_config.h"
|
||||
#include "clock.h"
|
||||
#include "core_riscv_ch32yyxx.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void systick_init(void)
|
||||
{
|
||||
SysTick->SR = 0;
|
||||
SysTick->CTLR= 0;
|
||||
SysTick->CNT = 0;
|
||||
SysTick->CMP = SystemCoreClock / 1000 - 1;
|
||||
SysTick->CTLR= 0xF;
|
||||
NVIC_SetPriority(SysTicK_IRQn,0xFF);
|
||||
NVIC_EnableIRQ(SysTicK_IRQn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function performs the global init of the system (HAL, IOs...)
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void hw_config_init(void)
|
||||
{
|
||||
systick_init();
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
#ifndef __HW_CONFIG_H
|
||||
#define __HW_CONFIG_H
|
||||
|
||||
#include "ch32_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void hw_config_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __HW_CONFIG_H */
|
||||
|
||||
|
|
@ -0,0 +1,294 @@
|
|||
|
||||
#include "interrupt.h"
|
||||
#include "ch32yyxx_exti.h"
|
||||
|
||||
#if !defined(EXTI_MODULE_DISABLED)
|
||||
|
||||
/*As we can have only one interrupt/pin id, don't need to get the port info*/
|
||||
typedef struct {
|
||||
IRQn_Type irqnb;
|
||||
void (*callback)(void);
|
||||
} gpio_irq_conf_str;
|
||||
|
||||
/* Private_Defines */
|
||||
#ifdef CH32V00x
|
||||
#define NB_EXTI (8)
|
||||
#else
|
||||
#define NB_EXTI (16)
|
||||
#endif
|
||||
|
||||
|
||||
/* Private Variables */
|
||||
static gpio_irq_conf_str gpio_irq_conf[NB_EXTI] = {
|
||||
#if defined (CH32V00x)
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_0
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_1
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_2
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_3
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_4
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_5
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL}, //GPIO_PIN_6
|
||||
{.irqnb = EXTI7_0_IRQn, .callback = NULL} //GPIO_PIN_7
|
||||
#else
|
||||
{.irqnb = EXTI0_IRQn, .callback = NULL}, //GPIO_PIN_0
|
||||
{.irqnb = EXTI1_IRQn, .callback = NULL}, //GPIO_PIN_1
|
||||
{.irqnb = EXTI2_IRQn, .callback = NULL}, //GPIO_PIN_2
|
||||
{.irqnb = EXTI3_IRQn, .callback = NULL}, //GPIO_PIN_3
|
||||
{.irqnb = EXTI4_IRQn, .callback = NULL}, //GPIO_PIN_4
|
||||
{.irqnb = EXTI9_5_IRQn, .callback = NULL}, //GPIO_PIN_5
|
||||
{.irqnb = EXTI9_5_IRQn, .callback = NULL}, //GPIO_PIN_6
|
||||
{.irqnb = EXTI9_5_IRQn, .callback = NULL}, //GPIO_PIN_7
|
||||
{.irqnb = EXTI9_5_IRQn, .callback = NULL}, //GPIO_PIN_8
|
||||
{.irqnb = EXTI9_5_IRQn, .callback = NULL}, //GPIO_PIN_9
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL}, //GPIO_PIN_10
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL}, //GPIO_PIN_11
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL}, //GPIO_PIN_12
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL}, //GPIO_PIN_13
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL}, //GPIO_PIN_14
|
||||
{.irqnb = EXTI15_10_IRQn, .callback = NULL} //GPIO_PIN_15
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
static const uint32_t exti_lines[NB_EXTI] = {
|
||||
EXTI_Line0, EXTI_Line1, EXTI_Line2, EXTI_Line3,
|
||||
EXTI_Line4, EXTI_Line5, EXTI_Line6, EXTI_Line7,
|
||||
#if !defined(CH32V00x)
|
||||
EXTI_Line8, EXTI_Line9, EXTI_Line10, EXTI_Line11,
|
||||
EXTI_Line12, EXTI_Line13, EXTI_Line14, EXTI_Line15
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* Private Functions */
|
||||
/**
|
||||
* @brief This function returns the pin ID function of the HAL PIN definition
|
||||
* @param pin : one of the gpio pin
|
||||
* @retval None
|
||||
*/
|
||||
static uint8_t get_pin_id(uint16_t pin)
|
||||
{
|
||||
uint8_t id = 0;
|
||||
|
||||
while (pin != 0x0001) {
|
||||
pin = pin >> 1;
|
||||
id++;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void ch32_interrupt_enable(GPIO_TypeDef *port, GPIOMode_TypeDef io_mode,uint16_t pin, void (*callback)(void), EXTIMode_TypeDef it_mode, EXTITrigger_TypeDef trigger_mode)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct={0};
|
||||
EXTI_InitTypeDef EXTI_InitStruct={0};
|
||||
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
|
||||
uint8_t id = get_pin_id(pin);
|
||||
uint8_t gpio_port_souce=0;
|
||||
GPIO_InitStruct.GPIO_Pin = pin;
|
||||
GPIO_InitStruct.GPIO_Mode = io_mode;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(port, &GPIO_InitStruct);
|
||||
|
||||
#if defined(GPIOA_BASE)
|
||||
if(port == GPIOA) gpio_port_souce = GPIO_PortSourceGPIOA;
|
||||
#endif
|
||||
#if defined(GPIOB_BASE)
|
||||
if(port == GPIOB) gpio_port_souce = GPIO_PortSourceGPIOB;
|
||||
#endif
|
||||
#if defined(GPIOC_BASE)
|
||||
if(port == GPIOC) gpio_port_souce = GPIO_PortSourceGPIOC;
|
||||
#endif
|
||||
#if defined(GPIOD_BASE)
|
||||
if(port == GPIOD) gpio_port_souce = GPIO_PortSourceGPIOD;
|
||||
#endif
|
||||
#if defined(GPIOE_BASE)
|
||||
if(port == GPIOE) gpio_port_souce = GPIO_PortSourceGPIOE;
|
||||
#endif
|
||||
#if defined(GPIOF_BASE)
|
||||
if(port == GPIOF) gpio_port_souce = GPIO_PortSourceGPIOF;
|
||||
#endif
|
||||
#if defined(GPIOG_BASE)
|
||||
if(port == GPIOG) gpio_port_souce = GPIO_PortSourceGPIOG;
|
||||
#endif
|
||||
#if defined(GPIOH_BASE)
|
||||
if(port == GPIOH) gpio_port_souce = GPIO_PortSourceGPIOH;
|
||||
#endif
|
||||
|
||||
GPIO_EXTILineConfig(gpio_port_souce, id);
|
||||
EXTI_InitStruct.EXTI_Line = exti_lines[id];
|
||||
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
|
||||
EXTI_InitStruct.EXTI_Mode = it_mode;
|
||||
EXTI_InitStruct.EXTI_Trigger = trigger_mode;
|
||||
EXTI_Init(&EXTI_InitStruct);
|
||||
|
||||
gpio_irq_conf[id].callback = callback;
|
||||
|
||||
NVIC_SetPriority(gpio_irq_conf[id].irqnb, EXTI_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(gpio_irq_conf[id].irqnb);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function disable the interruption on the selected port/pin
|
||||
* @param port : one of the gpio port
|
||||
* @param pin : one of the gpio pin
|
||||
* @retval None
|
||||
*/
|
||||
void ch32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin)
|
||||
{
|
||||
uint8_t id = get_pin_id(pin);
|
||||
gpio_irq_conf[id].callback = NULL;
|
||||
|
||||
for (int i = 0; i < NB_EXTI; i++) {
|
||||
if (gpio_irq_conf[id].irqnb == gpio_irq_conf[i].irqnb
|
||||
&& gpio_irq_conf[i].callback != NULL) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
NVIC_DisableIRQ(gpio_irq_conf[id].irqnb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function his called by the HAL if the IRQ is valid
|
||||
* @param GPIO_Pin : one of the gpio pin
|
||||
* @retval None
|
||||
*/
|
||||
void _gpio_exti_callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
uint8_t irq_id = get_pin_id(GPIO_Pin);
|
||||
if (gpio_irq_conf[irq_id].callback != NULL) {
|
||||
gpio_irq_conf[irq_id].callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if defined(CH32V00x)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void EXTI7_0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI7_0_IRQHandler(void)
|
||||
{
|
||||
uint32_t pin;
|
||||
for (pin = GPIO_Pin_0; pin <= GPIO_Pin_7; pin = pin << 1)
|
||||
{
|
||||
EXTI_ClearITPendingBit(pin); //0x1 2 4 8 10 20 40 80
|
||||
_gpio_exti_callback(pin);
|
||||
}
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void EXTI0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI9_5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void EXTI15_10_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
/**
|
||||
* @brief This function handles external line 0 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI0_IRQHandler(void)
|
||||
{
|
||||
EXTI_ClearITPendingBit(EXTI_Line0);
|
||||
_gpio_exti_callback(EXTI_Line0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 1 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI1_IRQHandler(void)
|
||||
{
|
||||
EXTI_ClearITPendingBit(EXTI_Line1);
|
||||
_gpio_exti_callback(EXTI_Line1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 2 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI2_IRQHandler(void)
|
||||
{
|
||||
EXTI_ClearITPendingBit(EXTI_Line2);
|
||||
_gpio_exti_callback(EXTI_Line2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 3 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI3_IRQHandler(void)
|
||||
{
|
||||
EXTI_ClearITPendingBit(EXTI_Line3);
|
||||
_gpio_exti_callback(EXTI_Line3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 4 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI4_IRQHandler(void)
|
||||
{
|
||||
EXTI_ClearITPendingBit(EXTI_Line4);
|
||||
_gpio_exti_callback(EXTI_Line4);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 5 to 9 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI9_5_IRQHandler(void)
|
||||
{
|
||||
uint32_t pin;
|
||||
for (pin = GPIO_Pin_5; pin <= GPIO_Pin_9; pin = pin << 1) {
|
||||
EXTI_ClearITPendingBit(pin);
|
||||
_gpio_exti_callback(pin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles external line 10 to 15 interrupt request.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void EXTI15_10_IRQHandler(void)
|
||||
{
|
||||
uint32_t pin;
|
||||
for (pin = GPIO_Pin_10; pin <= GPIO_Pin_15; pin = pin << 1) {
|
||||
EXTI_ClearITPendingBit(pin);
|
||||
_gpio_exti_callback(pin);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CH32V00x */
|
||||
|
||||
#endif /* EXTI_MODULE_ENABLED */
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file interrupt.h
|
||||
* @author WI6LABS
|
||||
* @version V1.0.0
|
||||
* @date 01-August-2016
|
||||
* @brief Header for interrupt module
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
******************************************************************************
|
||||
Modified 1 may 2023 by TempersLee
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __INTERRUPT_H
|
||||
#define __INTERRUPT_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ch32_def.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#if !defined(EXTI_MODULE_DISABLED)
|
||||
|
||||
|
||||
#ifndef EXTI_IRQ_PRIO
|
||||
#define EXTI_IRQ_PRIO (0xFF)
|
||||
#endif /* EXTI_IRQ_PRIO */
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void ch32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin);
|
||||
void ch32_interrupt_enable(GPIO_TypeDef *port, GPIOMode_TypeDef io_mode,uint16_t pin, void (*callback)(void), EXTIMode_TypeDef it_mode, EXTITrigger_TypeDef trigger_mode);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* __INTERRUPT_H */
|
||||
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef _CH32YYXX_H_
|
||||
#define _CH32YYXX_H_
|
||||
|
||||
/* Include Low Layers drivers */
|
||||
#include "ch32yyxx_adc.h"
|
||||
#include "ch32yyxx_bkp.h"
|
||||
#include "ch32yyxx_can.h"
|
||||
#include "ch32yyxx_crc.h"
|
||||
#include "ch32yyxx_dac.h"
|
||||
#include "ch32yyxx_dbgmcu.h"
|
||||
#include "ch32yyxx_dma.h"
|
||||
#include "ch32yyxx_dvp.h"
|
||||
#include "ch32yyxx_eth.h"
|
||||
#include "ch32yyxx_exti.h"
|
||||
#include "ch32yyxx_flash.h"
|
||||
#include "ch32yyxx_fsmc.h"
|
||||
#include "ch32yyxx_gpio.h"
|
||||
#include "ch32yyxx_i2c.h"
|
||||
#include "ch32yyxx_iwdg.h"
|
||||
#include "ch32yyxx_misc.h"
|
||||
#include "ch32yyxx_opa.h"
|
||||
#include "ch32yyxx_pwr.h"
|
||||
#include "ch32yyxx_rcc.h"
|
||||
#include "ch32yyxx_rng.h"
|
||||
#include "ch32yyxx_rtc.h"
|
||||
#include "ch32yyxx_sdio.h"
|
||||
#include "ch32yyxx_spi.h"
|
||||
#include "ch32yyxx_tim.h"
|
||||
#include "ch32yyxx_usart.h"
|
||||
#include "ch32yyxx_wwdg.h"
|
||||
|
||||
#include "ch32yyxx_debug.h"
|
||||
#include "system_ch32yyxx.h"
|
||||
#include "core_riscv_cH32yyxx.h"
|
||||
#include "ch32yyxx_it.h"
|
||||
|
||||
|
||||
#endif /* _CH32YYXX_H_ */
|
|
@ -0,0 +1,13 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_adc.c"
|
||||
|
||||
#elif CH32V10x
|
||||
#include "ch32v10x_adc.c"
|
||||
|
||||
#elif CH32V20x
|
||||
#include "ch32v20x_adc.c"
|
||||
|
||||
#elif CH32V30x
|
||||
#include "ch32v30x_adc.c"
|
||||
#endif
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _CH32YYXX_ADC_H_
|
||||
#define _CH32YYXX_ADC_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_adc.h"
|
||||
|
||||
#elif CH32V10x
|
||||
#include "ch32v10x_adc.h"
|
||||
|
||||
#elif CH32V20x
|
||||
#include "ch32v20x_adc.h"
|
||||
|
||||
#elif CH32V30x
|
||||
#include "ch32v30x_adc.h"
|
||||
#endif
|
||||
|
||||
#endif /* _CH32YYXX_ADC_H_ */
|
|
@ -0,0 +1,11 @@
|
|||
#ifdef CH32V00x
|
||||
//nothing
|
||||
#elif CH32V10x
|
||||
#include "ch32v10x_bkp.c"
|
||||
|
||||
#elif CH32V20x
|
||||
#include "ch32v20x_bkp.c"
|
||||
|
||||
#elif CH32V30x
|
||||
#include "ch32v30x_bkp.c"
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _CH32YYXX__BKP_H_
|
||||
#define _CH32YYXX__BKP_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
//nothing
|
||||
|
||||
#elif CH32V10x
|
||||
#include "ch32v10x_bkp.h"
|
||||
|
||||
#elif CH32V20x
|
||||
#include "ch32v20x_bkp.h"
|
||||
|
||||
#elif CH32V30x
|
||||
#include "ch32v30x_bkp.h"
|
||||
#endif
|
||||
|
||||
#endif /* _CH32YYXX__BKP_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V20x
|
||||
#include "ch32v20x_can.c"
|
||||
#endif
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef __CH32YYXX_CAN_H_
|
||||
#define __CH32YYXX_CAN_H_
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_can.h"
|
||||
#endif
|
||||
#endif /*__CH32YYXX_CAN_H_*/
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V20x
|
||||
#include "ch32v20x_crc.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_CRC_H_
|
||||
#define __CH32YYXX_CRC_H_
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_crc.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_CRC_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_dac.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_DAC_H_
|
||||
#define __CH32YYXX_DAC_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_dac.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_DAC_H_ */
|
|
@ -0,0 +1,9 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_dbgmcu.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
|
||||
#include "ch32v20x_dbgmcu.c"
|
||||
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef _CH32YYXX_DBGMCU_H_
|
||||
#define _CH32YYXX_DBGMCU_H
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_dbgmcu.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_dbgmcu.h"
|
||||
#endif
|
||||
|
||||
#endif /* _CH32YYXX_DBGMCU_H_ */
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
|
||||
#include "debug.c"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef _CH32YYXX_DEBUG_H_
|
||||
#define _CH32YYXX_DEBUG_H_
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
#endif /* _CH32YYXX_ADC_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_dma.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_dma.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_DMA_H__
|
||||
#define __CH32YYXX_DMA_H__
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_dma.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_dma.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_DMA_H__ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_dvp.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_DVP_H_
|
||||
#define __CH32YYXX_DVP_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_dvp.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_DVP_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_eth.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_ETH_H_
|
||||
#define __CH32YYXX_ETH_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_eth.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_ETH_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_exti.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_exti.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_EXTI_H_
|
||||
#define __CH32YYXX_EXTI_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_exti.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_exti.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_EXTI_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_flash.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_flash.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_FLASH_H_
|
||||
#define __CH32YYXX_FLASH_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_flash.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_flash.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_FLASH_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_fsmc.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_FSMC_H_
|
||||
#define __CH32YYXX_FSMC_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_fsmc.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_FSMC_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_gpio.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_gpio.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_GPIO_H_
|
||||
#define __CH32YYXX_GPIO_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_gpio.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_gpio.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_GPIO_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_i2c.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_i2c.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef _CH32YYXX_I2C_H_
|
||||
#define _CH32YYXX_I2C_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_i2c.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_i2c.h"
|
||||
#endif
|
||||
|
||||
#endif /* _CH32YYXX_I2C_H_ */
|
|
@ -0,0 +1,8 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_it.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_it.c"
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef _CH32YYXX_IT_H_
|
||||
#define _CH32YYXX_IT_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_it.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_it.h"
|
||||
#endif
|
||||
|
||||
#endif /* _CH32YYXX_ADC_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_iwdg.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_iwdg.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_IWDG_H_
|
||||
#define __CH32YYXX_IWDG_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_iwdg.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_iwdg.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_IWDG_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_misc.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_misc.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_MISC_H_
|
||||
#define __CH32YYXX_MISC_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_misc.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_misc.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_MISC_H_ */
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_opa.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_opa.c"
|
||||
#endif
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef __CH32YYXX_OPA_H_
|
||||
#define __CH32YYXX_OPA_H_
|
||||
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_opa.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_opa.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_OPA_H_ */
|
|
@ -0,0 +1,8 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_pwr.c"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_pwr.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_PWR_H_
|
||||
#define __CH32YYXX_PWR_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_pwr.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_pwr.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_PWR_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_rcc.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_rcc.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_RCC_H_
|
||||
#define __CH32YYXX_RCC_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_rcc.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_rcc.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_RCC_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_rng.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_RNG_H_
|
||||
#define __CH32YYXX_RNG_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_rng.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_RNG_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V20x
|
||||
#include "ch32v20x_rtc.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_CTC_H_
|
||||
#define __CH32YYXX_CTC_H_
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_rtc.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_CTC_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
#ifdef CH32V30x
|
||||
#include "ch32v30x_sdio.c"
|
||||
#endif
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __CH32YYXX_SDIO_H_
|
||||
#define __CH32YYXX_SDIO_H_
|
||||
|
||||
#ifdef CH32V30x
|
||||
#include "ch32v30x_sdio.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_SDIO_H_ */
|
|
@ -0,0 +1,8 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_spi.c"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_spi.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_SPI_H_
|
||||
#define __CH32YYXX_SPI_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_spi.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_spi.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_SPI_H_ */
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef CH32V00x
|
||||
#include "ch32v00x_tim.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_tim.c"
|
||||
#endif
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CH32YYXX_TIM_H_
|
||||
#define __CH32YYXX_TIM_H_
|
||||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_tim.h"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_tim.h"
|
||||
#endif
|
||||
|
||||
#endif /* __CH32YYXX_TIM_H_ */
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
#ifdef CH32V00x
|
||||
#include "ch32v00x_usart.c"
|
||||
#endif
|
||||
|
||||
#ifdef CH32V20x
|
||||
#include "ch32v20x_usart.c"
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue