├── .gitignore ├── LICENSE ├── README.md ├── bootloaderWebtool ├── ch55xbl.js └── index.html ├── ch55xduino ├── ch55x │ ├── boards.txt │ ├── cores │ │ └── ch55xduino │ │ │ ├── Arduino.h │ │ │ ├── HardwareSerial.h │ │ │ ├── HardwareSerial0.c │ │ │ ├── HardwareSerial0ISR.c │ │ │ ├── HardwareSerial1.c │ │ │ ├── HardwareSerial1ISR.c │ │ │ ├── Print-float.c │ │ │ ├── Print.c │ │ │ ├── Print.h │ │ │ ├── USBCDC.c │ │ │ ├── USBconstant.c │ │ │ ├── USBconstant.h │ │ │ ├── USBhandler.c │ │ │ ├── USBhandler.h │ │ │ ├── WInterrupts.c │ │ │ ├── WMath.c │ │ │ ├── debug_util.c │ │ │ ├── dummy_variable_main.h │ │ │ ├── eeprom.c │ │ │ ├── genericPrintSelection.h │ │ │ ├── genericPrintSerial0.c │ │ │ ├── genericPrintSerial0Float.c │ │ │ ├── genericPrintSerial1.c │ │ │ ├── genericPrintSerial1Float.c │ │ │ ├── genericPrintUSBSerial.c │ │ │ ├── genericPrintUSBSerialFloat.c │ │ │ ├── main.c │ │ │ ├── weakOverride.c │ │ │ ├── weakOverrideGPIO.c │ │ │ ├── weakOverrideTimer2.c │ │ │ ├── wiring.c │ │ │ ├── wiring_analog.c │ │ │ ├── wiring_digital.c │ │ │ ├── wiring_digital_fast.h │ │ │ └── wiring_private.h │ ├── libraries │ │ ├── Generic_Examples │ │ │ ├── empty.h │ │ │ └── examples │ │ │ │ ├── 01.Basics │ │ │ │ ├── AnalogReadSerial │ │ │ │ │ ├── AnalogReadSerial.ino │ │ │ │ │ └── AnalogReadSerial.txt │ │ │ │ ├── BareMinimum │ │ │ │ │ ├── BareMinimum.ino │ │ │ │ │ └── BareMinimum.txt │ │ │ │ ├── Blink │ │ │ │ │ ├── Blink.ino │ │ │ │ │ └── Blink.txt │ │ │ │ ├── DigitalReadSerial │ │ │ │ │ ├── DigitalReadSerial.ino │ │ │ │ │ └── DigitalReadSerial.txt │ │ │ │ ├── Fade │ │ │ │ │ ├── Fade.ino │ │ │ │ │ └── Fade.txt │ │ │ │ └── ReadAnalogVoltage │ │ │ │ │ ├── ReadAnalogVoltage.ino │ │ │ │ │ └── ReadAnalogVoltage.txt │ │ │ │ ├── 02.Digital │ │ │ │ ├── AttachInterrupts │ │ │ │ │ └── AttachInterrupts.ino │ │ │ │ ├── BlinkFastFunction │ │ │ │ │ └── BlinkFastFunction.ino │ │ │ │ ├── BlinkWithoutDelay │ │ │ │ │ ├── BlinkWithoutDelay.ino │ │ │ │ │ └── BlinkWithoutDelay.txt │ │ │ │ ├── Button │ │ │ │ │ ├── Button.ino │ │ │ │ │ └── Button.txt │ │ │ │ ├── Debounce │ │ │ │ │ ├── Debounce.ino │ │ │ │ │ └── Debounce.txt │ │ │ │ ├── DigitalInputPullup │ │ │ │ │ ├── DigitalInputPullup.ino │ │ │ │ │ └── DigitalInputPullup.txt │ │ │ │ └── StateChangeDetection │ │ │ │ │ ├── StateChangeDetection.ino │ │ │ │ │ └── StateChangeDetection.txt │ │ │ │ ├── 03.Analog │ │ │ │ ├── AnalogInOutSerial │ │ │ │ │ ├── AnalogInOutSerial.ino │ │ │ │ │ └── AnalogInOutSerial.txt │ │ │ │ ├── AnalogInput │ │ │ │ │ ├── AnalogInput.ino │ │ │ │ │ └── AnalogInput.txt │ │ │ │ ├── Calibration │ │ │ │ │ ├── Calibration.ino │ │ │ │ │ └── Calibration.txt │ │ │ │ ├── Fading │ │ │ │ │ ├── Fading.ino │ │ │ │ │ └── Fading.txt │ │ │ │ └── Smoothing │ │ │ │ │ ├── Smoothing.ino │ │ │ │ │ └── Smoothing.txt │ │ │ │ ├── 04.Communication │ │ │ │ ├── ASCIITable │ │ │ │ │ ├── ASCIITable.ino │ │ │ │ │ └── ASCIITable.txt │ │ │ │ ├── Dimmer │ │ │ │ │ ├── Dimmer.ino │ │ │ │ │ └── Dimmer.txt │ │ │ │ ├── Echo │ │ │ │ │ └── Echo.ino │ │ │ │ ├── Graph │ │ │ │ │ ├── Graph.ino │ │ │ │ │ └── Graph.txt │ │ │ │ ├── SumpLogicAnalyzer │ │ │ │ │ ├── SumpLogicAnalyzer.ino │ │ │ │ │ ├── captureInline5mhz.ino │ │ │ │ │ ├── captureInline6mhz.ino │ │ │ │ │ └── captureInlineWithT2.ino │ │ │ │ └── UsbSerialAdaptor │ │ │ │ │ └── UsbSerialAdaptor.ino │ │ │ │ ├── 05.USB │ │ │ │ ├── CDCinUserCode │ │ │ │ │ ├── CDCinUserCode.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── userUsbCdc │ │ │ │ │ │ ├── USBCDC.c │ │ │ │ │ │ ├── USBCDC.h │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ ├── CMSIS_DAP │ │ │ │ │ ├── CMSIS_DAP.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── CMSIS_DAPusb │ │ │ │ │ │ ├── DAP.c │ │ │ │ │ │ ├── DAP.h │ │ │ │ │ │ ├── SW_DP.c │ │ │ │ │ │ ├── USBHID.c │ │ │ │ │ │ ├── USBHID.h │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ ├── HidKeyboard │ │ │ │ │ ├── HidKeyboard.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── userUsbHidKeyboard │ │ │ │ │ │ ├── USBHIDKeyboard.c │ │ │ │ │ │ ├── USBHIDKeyboard.h │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ ├── HidKeyboardMouse │ │ │ │ │ ├── HidKeyboardMouse.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── userUsbHidKeyboardMouse │ │ │ │ │ │ ├── USBHIDKeyboardMouse.c │ │ │ │ │ │ ├── USBHIDKeyboardMouse.h │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ ├── HidMediaKeyboard │ │ │ │ │ ├── HidMediaKeyboard.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── userUsbHidMediaKeyboard │ │ │ │ │ │ ├── USBHIDMediaKeyboard.c │ │ │ │ │ │ ├── USBHIDMediaKeyboard.h │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ ├── MassStorage │ │ │ │ │ ├── MassStorage.ino │ │ │ │ │ └── src │ │ │ │ │ │ └── userMassStorage │ │ │ │ │ │ ├── USBMassStorage.c │ │ │ │ │ │ ├── USBMassStorage.h │ │ │ │ │ │ ├── USBMediumAccess.c │ │ │ │ │ │ ├── USBMediumAccess.h │ │ │ │ │ │ ├── USBSCSI.h │ │ │ │ │ │ ├── USBSCSIData.c │ │ │ │ │ │ ├── USBconstant.c │ │ │ │ │ │ ├── USBconstant.h │ │ │ │ │ │ ├── USBhandler.c │ │ │ │ │ │ └── USBhandler.h │ │ │ │ └── NoUsbExample │ │ │ │ │ └── NoUsbExample.ino │ │ │ │ └── 06.EEPROM │ │ │ │ └── EEPROM_cycle_write │ │ │ │ └── EEPROM_cycle_write.ino │ │ ├── SPI │ │ │ ├── examples │ │ │ │ └── ADXL345SPI │ │ │ │ │ └── ADXL345SPI.ino │ │ │ ├── keywords.txt │ │ │ ├── library.properties │ │ │ └── src │ │ │ │ ├── SPI.c │ │ │ │ └── SPI.h │ │ ├── SoftI2C │ │ │ ├── examples │ │ │ │ ├── dump24c32 │ │ │ │ │ └── dump24c32.ino │ │ │ │ └── i2cScanner │ │ │ │ │ └── i2cScanner.ino │ │ │ ├── keywords.txt │ │ │ ├── library.properties │ │ │ └── src │ │ │ │ ├── SoftI2C.c │ │ │ │ └── SoftI2C.h │ │ ├── TouchKey │ │ │ ├── examples │ │ │ │ ├── TouchKeyExample │ │ │ │ │ └── TouchKeyExample.ino │ │ │ │ └── TouchKeyTuneParameter │ │ │ │ │ └── TouchKeyTuneParameter.ino │ │ │ ├── keywords.txt │ │ │ ├── library.properties │ │ │ └── src │ │ │ │ ├── TouchKey.c │ │ │ │ └── TouchKey.h │ │ └── WS2812 │ │ │ ├── examples │ │ │ └── chaseTest │ │ │ │ └── chaseTest.ino │ │ │ ├── keywords.txt │ │ │ ├── library.properties │ │ │ └── src │ │ │ ├── WS2812.c │ │ │ ├── WS2812.h │ │ │ └── template │ │ │ ├── WS2812_PIN_._c │ │ │ ├── WS2812_pins_header.h │ │ │ ├── generateCode.py │ │ │ ├── optionalLink_WS2812_P1_0.c │ │ │ ├── optionalLink_WS2812_P1_1.c │ │ │ ├── optionalLink_WS2812_P1_2.c │ │ │ ├── optionalLink_WS2812_P1_3.c │ │ │ ├── optionalLink_WS2812_P1_4.c │ │ │ ├── optionalLink_WS2812_P1_5.c │ │ │ ├── optionalLink_WS2812_P1_6.c │ │ │ ├── optionalLink_WS2812_P1_7.c │ │ │ ├── optionalLink_WS2812_P3_0.c │ │ │ ├── optionalLink_WS2812_P3_1.c │ │ │ ├── optionalLink_WS2812_P3_2.c │ │ │ ├── optionalLink_WS2812_P3_3.c │ │ │ ├── optionalLink_WS2812_P3_4.c │ │ │ ├── optionalLink_WS2812_P3_5.c │ │ │ ├── optionalLink_WS2812_P3_6.c │ │ │ └── optionalLink_WS2812_P3_7.c │ ├── platform.txt │ └── variants │ │ ├── ch549 │ │ ├── include │ │ │ ├── CH549.H │ │ │ ├── ch5xx.h │ │ │ └── ch5xx_usb.h │ │ ├── pins_arduino.h │ │ └── pins_arduino_include.h │ │ ├── ch552 │ │ ├── include │ │ │ ├── ch5xx.h │ │ │ ├── ch5xx_datatypes.h │ │ │ └── ch5xx_usb.h │ │ ├── pins_arduino.h │ │ └── pins_arduino_include.h │ │ └── ch559 │ │ ├── include │ │ ├── ch5xx.h │ │ ├── ch5xx_datatypes.h │ │ └── ch5xx_usb.h │ │ ├── pins_arduino.h │ │ └── pins_arduino_include.h ├── filePacker │ ├── gen_platform_entry.sh │ ├── gen_tools_entry.sh │ ├── minimize-sdcc.sh │ ├── packCurrentVersion.sh │ └── packedFiles │ │ └── .gitignore └── tools │ ├── linux │ ├── 99-ch55xbl.rules │ └── vnproch55x │ ├── macosx │ └── vnproch55x │ ├── win │ ├── CH375DLL.dll │ ├── busybox.exe │ ├── libgcc_s_sjlj-1.dll │ ├── libusb-1.0.dll │ ├── libusbK.dll │ └── vnproch55x.exe │ └── wrapper │ ├── sdar.sh │ ├── sdcc-link.sh │ ├── sdcc.sh │ └── sdsize.sh ├── docs ├── Zadig_CDC.png ├── Zadig_bootloader.png ├── Zadig_bootloader_libusb.png ├── blinkLED.gif ├── convertJsonGitee.py ├── fetchBz2FromGiteeRelease.txt ├── package_ch55xduino_mcs51_index_gitee.json ├── simpleCH552Back.jpg └── simpleCH552Front.jpg ├── package_ch55xduino_mcs51_index.json └── pcb ├── .gitignore ├── ch55xRebootTool ├── .gitignore ├── README.md ├── RebootToolTest │ ├── RebootToolTest.ino │ ├── reboot.py │ └── src │ │ └── userUsbCdc │ │ ├── USBCDC.c │ │ ├── USBCDC.h │ │ ├── USBconstant.c │ │ ├── USBconstant.h │ │ ├── USBhandler.c │ │ └── USBhandler.h ├── ch55xRebootTool.brd ├── ch55xRebootTool.jpg ├── ch55xRebootTool.pdf └── ch55xRebootTool.sch ├── schematic.png ├── simpleCH552.brd └── simpleCH552.sch /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *bak 3 | .*.swp 4 | *.jsone 5 | # ignore all kinds of binary files: 6 | *.o 7 | *.bin 8 | *.ihx 9 | *.hex 10 | #*.exe 11 | tags 12 | # ignore files generated by sdcc: 13 | *.cdb 14 | *.adb 15 | *.ihx 16 | *.asm 17 | *.lst 18 | *.rel 19 | *.rst 20 | *.sym 21 | *.lk 22 | *.map 23 | # ignore files generated by the Arduino make system: 24 | build-*/ 25 | # ignore test log files: 26 | *test.log 27 | # ignore files generated by patch: 28 | *.orig 29 | *.rej 30 | # ignore mkdocs build directory 31 | site/ 32 | # ignore my scratch directory 33 | notes/ 34 | .DS_Store 35 | -------------------------------------------------------------------------------- /bootloaderWebtool/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ch55x bootloader tool 6 | 7 | 8 | 9 | 10 |

11 | 12 |

13 |

14 | 15 | 16 |

17 |
18 |

Or Drag hex to this Drop Zone ...

19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | #ifndef HardwareSerial_h 2 | #define HardwareSerial_h 3 | 4 | #include 5 | #include 6 | #include "include/ch5xx.h" 7 | 8 | #define SERIAL0_TX_BUFFER_SIZE 16 9 | #define SERIAL0_RX_BUFFER_SIZE 16 10 | #define SERIAL1_TX_BUFFER_SIZE 16 11 | #define SERIAL1_RX_BUFFER_SIZE 16 12 | 13 | #define UART0_FLG_SENDING (1<<0) 14 | 15 | uint8_t Serial0(void); 16 | void Serial0_begin(unsigned long baud); 17 | 18 | uint8_t Serial0_available(void); 19 | uint8_t Serial0_read(void); 20 | uint8_t Serial0_write(uint8_t c); 21 | void Serial0_flush(void); 22 | 23 | void Serial0_end(void); 24 | 25 | void uart0IntRxHandler(); 26 | void uart0IntTxHandler(); 27 | 28 | 29 | uint8_t Serial1(void); 30 | void Serial1_begin(unsigned long baud); 31 | 32 | uint8_t Serial1_available(void); 33 | uint8_t Serial1_read(void); 34 | uint8_t Serial1_write(uint8_t c); 35 | void Serial1_flush(void); 36 | 37 | void Serial1_end(void); 38 | 39 | void uart1IntRxHandler(); 40 | void uart1IntTxHandler(); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/HardwareSerial0ISR.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | */ 4 | 5 | #include "HardwareSerial.h" 6 | 7 | __xdata uint8_t Receive_Uart0_Buf[SERIAL0_RX_BUFFER_SIZE]; //arduino style serial buffer 8 | __xdata uint8_t Transmit_Uart0_Buf[SERIAL0_TX_BUFFER_SIZE]; //arduino style serial buffer 9 | volatile __xdata uint8_t uart0_rx_buffer_head=0; 10 | volatile __xdata uint8_t uart0_rx_buffer_tail=0; 11 | volatile __xdata uint8_t uart0_tx_buffer_head=0; 12 | volatile __xdata uint8_t uart0_tx_buffer_tail=0; 13 | volatile __bit uart0_flag_sending=0; 14 | 15 | void uart0IntRxHandler(){ 16 | uint8_t nextHead = (uart0_rx_buffer_head + 1) % SERIAL0_RX_BUFFER_SIZE; 17 | 18 | if (nextHead != uart0_rx_buffer_tail) { 19 | Receive_Uart0_Buf[uart0_rx_buffer_head] = SBUF; 20 | uart0_rx_buffer_head = nextHead; 21 | } 22 | } 23 | 24 | void uart0IntTxHandler(){ 25 | if (uart0_flag_sending){ 26 | if (uart0_tx_buffer_head == uart0_tx_buffer_tail){ 27 | //do no more 28 | uart0_flag_sending &= 0; 29 | }else{ 30 | SBUF=Transmit_Uart0_Buf[uart0_tx_buffer_tail]; 31 | uart0_tx_buffer_tail = (uart0_tx_buffer_tail + 1) % SERIAL0_TX_BUFFER_SIZE; 32 | } 33 | } 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/HardwareSerial1ISR.c: -------------------------------------------------------------------------------- 1 | #include "HardwareSerial.h" 2 | 3 | __xdata uint8_t Receive_Uart1_Buf[SERIAL1_RX_BUFFER_SIZE]; //arduino style serial buffer 4 | __xdata uint8_t Transmit_Uart1_Buf[SERIAL1_TX_BUFFER_SIZE]; //arduino style serial buffer 5 | volatile __xdata uint8_t uart1_rx_buffer_head=0; 6 | volatile __xdata uint8_t uart1_rx_buffer_tail=0; 7 | volatile __xdata uint8_t uart1_tx_buffer_head=0; 8 | volatile __xdata uint8_t uart1_tx_buffer_tail=0; 9 | volatile __bit uart1_flag_sending=0; 10 | 11 | void uart1IntRxHandler(){ 12 | uint8_t nextHead = (uart1_rx_buffer_head + 1) % SERIAL1_RX_BUFFER_SIZE; 13 | 14 | if (nextHead != uart1_rx_buffer_tail) { 15 | #if defined(CH551) || defined(CH552) 16 | Receive_Uart1_Buf[uart1_rx_buffer_head] = SBUF1; 17 | #elif defined(CH559) 18 | Receive_Uart1_Buf[uart1_rx_buffer_head] = SER1_RBR; 19 | #elif defined(CH549) 20 | Receive_Uart1_Buf[uart1_rx_buffer_head] = SBUF1; 21 | #endif 22 | uart1_rx_buffer_head = nextHead; 23 | } 24 | } 25 | 26 | void uart1IntTxHandler(){ 27 | if (uart1_flag_sending){ 28 | if (uart1_tx_buffer_head == uart1_tx_buffer_tail){ 29 | //do no more 30 | uart1_flag_sending &= 0; 31 | }else{ 32 | #if defined(CH551) || defined(CH552) 33 | SBUF1=Transmit_Uart1_Buf[uart1_tx_buffer_tail]; 34 | #elif defined(CH559) 35 | SER1_THR=Transmit_Uart1_Buf[uart1_tx_buffer_tail]; 36 | #elif defined(CH549) 37 | SBUF1=Transmit_Uart1_Buf[uart1_tx_buffer_tail]; 38 | #endif 39 | uart1_tx_buffer_tail = (uart1_tx_buffer_tail + 1) % SERIAL1_TX_BUFFER_SIZE; 40 | } 41 | } 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/Print-float.c: -------------------------------------------------------------------------------- 1 | /* 2 | Print.c - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | The float functions are moved to a separate file Print-float.c. This way 6 | we can keep the linker from pulling in all the float functions even when no 7 | floats are used in the sketch. 8 | 9 | This library is free software; you can redistribute it and/or 10 | modify it under the terms of the GNU Lesser General Public 11 | License as published by the Free Software Foundation; either 12 | version 2.1 of the License, or (at your option) any later version. 13 | 14 | This library is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | Lesser General Public License for more details. 18 | 19 | You should have received a copy of the GNU Lesser General Public 20 | License along with this library; if not, write to the Free Software 21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | 23 | Modified 23 November 2006 by David A. Mellis 24 | Modified 03 August 2015 by Chuck Todd 25 | Modified 30 December 2016 by Michael Mayer 26 | */ 27 | 28 | 29 | //#include 30 | //#include 31 | //#include 32 | #include 33 | #include "Arduino.h" 34 | 35 | #include "Print.h" 36 | 37 | // Public Methods ////////////////////////////////////////////////////////////// 38 | 39 | 40 | uint8_t Print_print_fd(writefunc_p writefunc, __xdata double number, __xdata uint8_t digits) 41 | { 42 | __xdata uint8_t n = 0; 43 | __xdata uint8_t i; 44 | __xdata unsigned long int_part; 45 | __xdata double remainder, rounding; 46 | __xdata unsigned int toPrint; 47 | 48 | if (isnan(number)) return printStr(writefunc,"nan"); 49 | if (isinf(number)) return printStr(writefunc,"inf"); 50 | if (number > 4294967040.0) return printStr (writefunc,"ovf"); // constant determined empirically 51 | if (number <-4294967040.0) return printStr (writefunc,"ovf"); // constant determined empirically 52 | 53 | // Handle negative numbers 54 | if (number < 0.0) 55 | { 56 | n += writefunc('-'); 57 | number = -number; 58 | } 59 | 60 | // Round correctly so that print(1.999, 2) prints as "2.00" 61 | rounding = 0.5; 62 | for (i=0; i 0) { 74 | n += writefunc('.'); 75 | } 76 | 77 | // Extract digits from the remainder one at a time 78 | while (digits-- > 0) 79 | { 80 | remainder *= 10.0; 81 | toPrint = (unsigned int)(remainder); 82 | n += printNumber(writefunc,toPrint,10); 83 | remainder -= toPrint; 84 | } 85 | 86 | return n; 87 | } 88 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/Print.c: -------------------------------------------------------------------------------- 1 | /* 2 | Print.c - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | The float functions are moved to a separate file Print-float.c. This way 6 | we can keep the linker from pulling in all the float functions even when no 7 | floats are used in the sketch. 8 | 9 | This library is free software; you can redistribute it and/or 10 | modify it under the terms of the GNU Lesser General Public 11 | License as published by the Free Software Foundation; either 12 | version 2.1 of the License, or (at your option) any later version. 13 | 14 | This library is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | Lesser General Public License for more details. 18 | 19 | You should have received a copy of the GNU Lesser General Public 20 | License along with this library; if not, write to the Free Software 21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | 23 | Modified 23 November 2006 by David A. Mellis 24 | Modified 03 August 2015 by Chuck Todd 25 | Modified 30 December 2016 by Michael Mayer 26 | Modified 4 July 2020 by Deqing Sun for use with CH55xduino 27 | */ 28 | 29 | 30 | //#include 31 | //#include 32 | //#include 33 | #include 34 | #include "Arduino.h" 35 | 36 | #include "Print.h" 37 | 38 | 39 | 40 | void printNL(void); 41 | 42 | // Public Methods ////////////////////////////////////////////////////////////// 43 | 44 | uint8_t Print_print_sn(writefunc_p writefunc, uint8_t * __xdata buffer, __xdata uint8_t size) 45 | { 46 | uint8_t n = 0; 47 | while (size--) { 48 | if (writefunc(*buffer++)) n++; 49 | else break; 50 | } 51 | return n; 52 | } 53 | 54 | uint8_t Print_print_s(writefunc_p writefunc, char * __xdata str) 55 | { 56 | uint8_t n = 0; 57 | char c; 58 | 59 | if (!str) return 0; 60 | 61 | while ( c=*str++ ) { // assignment intented 62 | if (writefunc(c)) n++; 63 | else break; 64 | } 65 | return n; 66 | } 67 | 68 | 69 | uint8_t Print_print_u(writefunc_p writefunc, __xdata unsigned long n) 70 | { 71 | return printNumber(writefunc, n,10); 72 | } 73 | 74 | uint8_t Print_print_i(writefunc_p writefunc, __xdata long n) 75 | { 76 | return printInt(writefunc, n,10); 77 | } 78 | 79 | // (not so) Private Methods ///////////////////////////////////////////////////////////// 80 | 81 | uint8_t Print_println(writefunc_p writefunc) 82 | { 83 | uint8_t n; 84 | 85 | n = writefunc(13); 86 | n += writefunc(10); 87 | return n; 88 | } 89 | 90 | 91 | uint8_t Print_print_ub(writefunc_p writefunc, __xdata unsigned long n, __xdata uint8_t base) 92 | { 93 | __xdata char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. 94 | __xdata char *str = &buf[sizeof(buf) - 1]; 95 | 96 | *str = '\0'; 97 | 98 | // prevent crash if called with base == 1 99 | if (base < 2) base = 10; 100 | 101 | do { 102 | char c = n % base; 103 | n /= base; 104 | 105 | *--str = c < 10 ? c + '0' : c + 'A' - 10; 106 | } while(n); 107 | 108 | return Print_print_s(writefunc, str); 109 | } 110 | 111 | uint8_t Print_print_ib(writefunc_p writefunc, __xdata long n, __xdata uint8_t base) 112 | { 113 | if (base == 0) { 114 | return writefunc((unsigned char) n); 115 | } else if (base == 10) { 116 | if (n < 0) { 117 | int t = writefunc('-'); 118 | n = -n; 119 | return printNumber(writefunc, n, 10) + t; 120 | } 121 | return printNumber(writefunc, n, 10); 122 | } else { 123 | return printNumber(writefunc, n, base); 124 | } 125 | } 126 | 127 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Print.h - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Print_h 21 | #define Print_h 22 | 23 | //#include 24 | 25 | 26 | //#include "WString.h" 27 | //#include "Printable.h" 28 | 29 | #define DEC 10 30 | #define HEX 16 31 | #define OCT 8 32 | #define BIN 2 33 | 34 | 35 | // for the function pointer to the actual write function 36 | typedef uint8_t (*writefunc_p)(uint8_t c); 37 | 38 | // abreviations of the actual function names, mostly for internal use 39 | #define printBuf Print_print_sn 40 | #define printStr Print_print_s 41 | #define printNumber Print_print_ub 42 | #define printInt Print_print_ib 43 | #define println Print_println 44 | 45 | 46 | // the more sophisticated Arduino-Style functions. Variants of the standard 47 | // Serial.print() function: Separate impementations for different datatype 48 | // to mimic polymorphism. 49 | 50 | // print strings (C-Style or buffer contents) 51 | uint8_t Print_print_s(writefunc_p writefunc, char * __xdata str); 52 | uint8_t Print_print_sn(writefunc_p writefunc, uint8_t * __xdata buffer, __xdata uint8_t size); 53 | 54 | // print signed/unsigned values (char, short, int, long) as decimal values 55 | uint8_t Print_print_i(writefunc_p writefunc, __xdata long n); 56 | uint8_t Print_print_u(writefunc_p writefunc, __xdata unsigned long n); 57 | 58 | // print signed/unsigned integer values (char, short, int, long) to base B 59 | uint8_t Print_print_ib(writefunc_p writefunc, __xdata long n, __xdata uint8_t base); 60 | uint8_t Print_print_ub(writefunc_p writefunc, __xdata unsigned long n, __xdata uint8_t base); 61 | 62 | // print float values with two digits precision/specified number of digits 63 | uint8_t Print_print_fd(writefunc_p writefunc, __xdata double number, __xdata uint8_t digits); 64 | #define Print_print_f(W,N) Print_print_fd(W,N,2) 65 | //inline uint8_t Print_print_f(writefunc_p writefunc, __xdata double number) { 66 | // return Print_print_fd(writefunc, number, 2); 67 | //} 68 | 69 | // Variants of the above with a newline added at the and: 70 | uint8_t Print_println(writefunc_p writefunc); 71 | 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/USBconstant.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | */ 4 | 5 | #ifndef USER_USB_RAM 6 | 7 | #include "USBconstant.h" 8 | 9 | 10 | //Device descriptor 11 | __code uint8_t DevDesc[] = { 12 | 0x12,0x01, 13 | 0x10,0x01, //USB spec release number in BCD format, USB1.1 (0x10, 0x01). 14 | 0xEF,0x02,0x01, //bDeviceClass, bDeviceSubClass, bDeviceProtocol 15 | DEFAULT_ENDP0_SIZE, //bNumConfigurations 16 | 0x09,0x12,0x50,0xC5, // VID PID 17 | 0x01,0x01, //version 18 | 0x01,0x02,0x03, //bString 19 | 0x01 //bNumConfigurations 20 | }; 21 | 22 | __code uint16_t DevDescLen = sizeof(DevDesc); 23 | 24 | __code uint8_t CfgDesc[] ={ 25 | 0x09,0x02,sizeof(CfgDesc) & 0xff,sizeof(CfgDesc) >> 8, 26 | 0x02,0x01,0x00,0x80,0x64, //Configuration descriptor (2 interfaces) 27 | // Interface Association Descriptor, IAD, this packes following 2 interfaces into 1 28 | 0x08,0x0B,0x00,0x02,0x02,0x02,0x01,0x04, 29 | // Interface 1 (CDC) descriptor 30 | 0x09,0x04,0x00,0x00,0x01,0x02,0x02,0x01,0x04, // CDC control description, 1 endpoint 31 | // Functional Descriptor refer to usbcdc11.pdf 32 | 0x05,0x24,0x00,0x10,0x01, //Header Functional Descriptor 33 | 0x05,0x24,0x01,0x00,0x00, //Call Management Functional Descriptor 34 | 0x04,0x24,0x02,0x02, //Direct Line Management Functional Descriptor, Support: Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, Serial_State 35 | 0x05,0x24,0x06,0x00,0x01, //Union Functional Descriptor, Communication class interface 0, Data Class Interface 1 36 | 0x07,0x05,0x81,0x03,0x08,0x00,0x40, //EndPoint descriptor (CDC Upload, Interrupt) 37 | // Interface 2 (Data Interface) descriptor 38 | 0x09,0x04,0x01,0x00,0x02,0x0a,0x00,0x00,0x04, //Data Class Interface descriptor 39 | 0x07,0x05,0x02,0x02,0x40,0x00,0x00, //endpoint descriptor 40 | 0x07,0x05,0x82,0x02,0x40,0x00,0x00, //endpoint descriptor 41 | 42 | }; 43 | 44 | __code uint16_t CfgDescLen = sizeof(CfgDesc); 45 | 46 | //String Descriptors 47 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor 48 | __code uint16_t LangDesLen = sizeof(LangDes); 49 | __code uint16_t SerDes[]={ //Serial String Descriptor 50 | 0x030C, 51 | 'C','H','5','5','x', 52 | }; 53 | __code uint16_t SerDesLen = sizeof(SerDes); 54 | __code uint16_t Prod_Des[]={ //Produce String Descriptor 55 | 0x0316, 56 | 'C','H','5','5','x','d','u','i','n','o', 57 | }; 58 | __code uint16_t Prod_DesLen = sizeof(Prod_Des); 59 | 60 | __code uint16_t CDC_Des[]={ 61 | 0x0316, 62 | 'C','D','C',' ','S','e','r','i','a','l', 63 | }; 64 | __code uint16_t CDC_DesLen = sizeof(CDC_Des); 65 | 66 | __code uint16_t Manuf_Des[]={ //SDCC is little endian 67 | 0x030E, 68 | 'D','e','q','i','n','g', 69 | }; 70 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONST_DATA_H__ 2 | #define __CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define SET_LINE_CODING 0X20 // Configures DTE rate, stop-bits, parity, and number-of-character 9 | #define GET_LINE_CODING 0X21 // This request allows the host to find out the currently configured line coding. 10 | #define SET_CONTROL_LINE_STATE 0X22 // This request generates RS-232/V.24 style control signals. 11 | 12 | 13 | extern __code uint8_t DevDesc[]; 14 | extern __code uint8_t CfgDesc[]; 15 | extern __code uint8_t LangDes[]; 16 | extern __code uint16_t SerDes[]; 17 | extern __code uint16_t Prod_Des[]; 18 | extern __code uint16_t CDC_Des[]; 19 | extern __code uint16_t Manuf_Des[]; 20 | 21 | 22 | 23 | extern __code uint16_t DevDescLen; 24 | extern __code uint16_t CfgDescLen; 25 | extern __code uint16_t LangDesLen; 26 | extern __code uint16_t SerDesLen; 27 | extern __code uint16_t Prod_DesLen; 28 | extern __code uint16_t CDC_DesLen; 29 | extern __code uint16_t Manuf_DesLen; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef USER_USB_RAM 2 | 3 | #ifndef __USB_HANDLER_H__ 4 | #define __USB_HANDLER_H__ 5 | 6 | #include 7 | #include "include/ch5xx.h" 8 | #include "include/ch5xx_usb.h" 9 | 10 | 11 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 12 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 13 | extern __xdata __at (EP2_ADDR) uint8_t Ep2Buffer[]; 14 | 15 | extern uint16_t SetupLen; 16 | extern uint8_t SetupReq,UsbConfig; 17 | extern const __code uint8_t *pDescr; 18 | 19 | 20 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 21 | 22 | // Out 23 | #define EP0_OUT_Callback USB_EP0_OUT 24 | #define EP1_OUT_Callback NOP_Process 25 | #define EP2_OUT_Callback USB_EP2_OUT 26 | #define EP3_OUT_Callback NOP_Process 27 | #define EP4_OUT_Callback NOP_Process 28 | 29 | // SOF 30 | #define EP0_SOF_Callback NOP_Process 31 | #define EP1_SOF_Callback NOP_Process 32 | #define EP2_SOF_Callback NOP_Process 33 | #define EP3_SOF_Callback NOP_Process 34 | #define EP4_SOF_Callback NOP_Process 35 | 36 | // IN 37 | #define EP0_IN_Callback USB_EP0_IN 38 | #define EP1_IN_Callback USB_EP1_IN 39 | #define EP2_IN_Callback USB_EP2_IN 40 | #define EP3_IN_Callback NOP_Process 41 | #define EP4_IN_Callback NOP_Process 42 | 43 | // SETUP 44 | #define EP0_SETUP_Callback USB_EP0_SETUP 45 | #define EP1_SETUP_Callback NOP_Process 46 | #define EP2_SETUP_Callback NOP_Process 47 | #define EP3_SETUP_Callback NOP_Process 48 | #define EP4_SETUP_Callback NOP_Process 49 | 50 | void USBInterrupt(void); 51 | 52 | 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/WInterrupts.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | */ 4 | 5 | #include "wiring_private.h" 6 | 7 | void nothing(void) { 8 | } 9 | 10 | 11 | __xdata voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = { 12 | #if EXTERNAL_NUM_INTERRUPTS > 1 13 | nothing, 14 | #endif 15 | #if EXTERNAL_NUM_INTERRUPTS > 0 16 | nothing, 17 | #endif 18 | }; 19 | 20 | void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), __xdata uint8_t mode) { 21 | if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { 22 | intFunc[interruptNum] = userFunc; 23 | 24 | // Configure the interrupt mode (trigger on low input, any change, rising 25 | // edge, or falling edge). The mode constants were chosen to correspond 26 | // to the configuration bits in the hardware register, so we simply shift 27 | // the mode into place. 28 | 29 | // Enable the interrupt. 30 | 31 | switch (interruptNum) { 32 | case 0: 33 | IT0 = mode; 34 | EX0 = 1; 35 | break; 36 | case 1: 37 | IT1 = mode; 38 | EX1 = 1; 39 | break; 40 | } 41 | } 42 | } 43 | 44 | 45 | void detachInterrupt(uint8_t interruptNum) { 46 | if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { 47 | // Disable the interrupt. (We can't assume that interruptNum is equal 48 | // to the number of the EIMSK bit to clear, as this isn't true on the 49 | // ATmega8. There, INT0 is 6 and INT1 is 7.) 50 | switch (interruptNum) { 51 | case 0: 52 | EX0 = 0; 53 | break; 54 | case 1: 55 | EX1 = 0; 56 | break; 57 | } 58 | intFunc[interruptNum] = nothing; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/WMath.c: -------------------------------------------------------------------------------- 1 | /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 | 3 | /* 4 | Part of the Wiring project - http://wiring.org.co 5 | Copyright (c) 2004-06 Hernando Barragan 6 | Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ 7 | 8 | This library is free software; you can redistribute it and/or 9 | modify it under the terms of the GNU Lesser General Public 10 | License as published by the Free Software Foundation; either 11 | version 2.1 of the License, or (at your option) any later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General 19 | Public License along with this library; if not, write to the 20 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 21 | Boston, MA 02111-1307 USA 22 | */ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #include "stdlib.h" 27 | } 28 | #else 29 | #include "stdlib.h" 30 | #endif 31 | 32 | void randomSeed(unsigned long seed) 33 | { 34 | if (seed != 0) { 35 | srand(seed); 36 | } 37 | } 38 | 39 | long random(long howbig) 40 | { 41 | if (howbig == 0) { 42 | return 0; 43 | } 44 | return rand() % howbig; 45 | } 46 | 47 | long random_minmax(long howsmall, __xdata long howbig) 48 | { 49 | long diff; 50 | 51 | if (howsmall >= howbig) { 52 | return howsmall; 53 | } 54 | diff = howbig - howsmall; 55 | return random(diff) + howsmall; 56 | } 57 | 58 | long map(long x, __xdata long in_min, __xdata long in_max, __xdata long out_min, __xdata long out_max) 59 | { 60 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 61 | } 62 | 63 | //unsigned int makeWord(unsigned int w) { return w; } 64 | //unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } 65 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/debug_util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "include/ch5xx.h" 3 | #include "include/ch5xx_usb.h" 4 | 5 | //in SDCC, caller will save the registers (R0-R7) 6 | //DPL, DPH, B and ACC are for parameter/return value passing 7 | //At this moment, sendCharDebug is not included in any header 8 | //use pragma callee_saves when you declare sendCharDebug 9 | #pragma callee_saves sendCharDebug 10 | void sendCharDebug(char c) //8Mbps under 24M clk 11 | { 12 | c; //avoid unreferenced function argument warning 13 | //uint8_t interruptOn = EA; 14 | //EA = 0; 15 | __asm__(" mov c,_EA \n" 16 | " clr a \n" 17 | " rlc a \n" 18 | " mov b,a \n" 19 | " clr _EA \n"); 20 | 21 | //using P1.4 22 | __asm__( //any branch will cause unpredictable timing due to code alignment 23 | " mov a,dpl \n" //the parameter of func 24 | 25 | " clr c \n" 26 | " mov _P1_4,c \n" 27 | " rrc a \n" 28 | " mov _P1_4,c \n" 29 | " rrc a \n" 30 | " mov _P1_4,c \n" 31 | " rrc a \n" 32 | " mov _P1_4,c \n" 33 | " rrc a \n" 34 | " mov _P1_4,c \n" 35 | " rrc a \n" 36 | " mov _P1_4,c \n" 37 | " rrc a \n" 38 | " mov _P1_4,c \n" 39 | " rrc a \n" 40 | " mov _P1_4,c \n" 41 | " rrc a \n" 42 | " mov _P1_4,c \n" 43 | " setb c \n" 44 | " mov _P1_4,c \n" 45 | ); 46 | //if (interruptOn) EA = 1; 47 | 48 | __asm__(" mov a,b \n" 49 | " jz skipSetEADebug$\n" 50 | " setb _EA \n" 51 | "skipSetEADebug$: \n"); 52 | } 53 | 54 | /* 55 | void mDelayuS( uint16_t n ) 56 | { 57 | #ifdef F_CPU 58 | #if F_CPU <= 6000000 59 | n >>= 2; 60 | #endif 61 | #if F_CPU <= 3000000 62 | n >>= 2; 63 | #endif 64 | #if F_CPU <= 750000 65 | n >>= 4; 66 | #endif 67 | #endif 68 | while ( n ) { // total = 12~13 Fsys cycles, 1uS @Fsys=12MHz 69 | ++ SAFE_MOD; // 2 Fsys cycles, for higher Fsys, add operation here 70 | #ifdef F_CPU 71 | #if F_CPU >= 14000000 72 | ++ SAFE_MOD; 73 | #endif 74 | #if F_CPU >= 16000000 75 | ++ SAFE_MOD; 76 | #endif 77 | #if F_CPU >= 18000000 78 | ++ SAFE_MOD; 79 | #endif 80 | #if F_CPU >= 20000000 81 | ++ SAFE_MOD; 82 | #endif 83 | #if F_CPU >= 22000000 84 | ++ SAFE_MOD; 85 | #endif 86 | #if F_CPU >= 24000000 87 | ++ SAFE_MOD; 88 | #endif 89 | #if F_CPU >= 26000000 90 | ++ SAFE_MOD; 91 | #endif 92 | #if F_CPU >= 28000000 93 | ++ SAFE_MOD; 94 | #endif 95 | #if F_CPU >= 30000000 96 | ++ SAFE_MOD; 97 | #endif 98 | #if F_CPU >= 32000000 99 | ++ SAFE_MOD; 100 | #endif 101 | #endif 102 | -- n; 103 | } 104 | } 105 | 106 | void mDelaymS( uint16_t n ){ 107 | while ( n ) { 108 | #ifdef DELAY_MS_HW 109 | while ( ( TKEY_CTRL & bTKC_IF ) == 0 ); 110 | while ( TKEY_CTRL & bTKC_IF ); 111 | #else 112 | mDelayuS( 1000 ); 113 | #endif 114 | -- n; 115 | } 116 | }*/ 117 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/dummy_variable_main.h: -------------------------------------------------------------------------------- 1 | //add a reference to main to pull in main.c 2 | void main(void); void (*dummy_variable) () = main; 3 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/eeprom.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | */ 4 | 5 | #include 6 | #include "include/ch5xx.h" 7 | #include "include/ch5xx_usb.h" 8 | 9 | void eeprom_write_byte_2_params_DPTR (uint16_t addr_val){ 10 | //using a single parameter of 16bit number utilize both DPL and DPH, avoid using memory to pass parameters 11 | #define ADDR_PARAM ((addr_val>>8)&0xff) 12 | #define VAL_PARAM ((addr_val>>0)&0xff) 13 | 14 | #if defined(CH551) || defined(CH552) 15 | 16 | if (ADDR_PARAM>=128){ 17 | return; 18 | } 19 | 20 | SAFE_MOD = 0x55; 21 | SAFE_MOD = 0xAA; //Enter Safe mode 22 | GLOBAL_CFG |= bDATA_WE; //Enable DataFlash write 23 | SAFE_MOD = 0; //Exit Safe mode 24 | ROM_ADDR_H = DATA_FLASH_ADDR >> 8; 25 | ROM_ADDR_L = ADDR_PARAM<<1; 26 | ROM_DATA_L = VAL_PARAM; 27 | if ( ROM_STATUS & bROM_ADDR_OK ) { // Valid access Address 28 | ROM_CTRL = ROM_CMD_WRITE; // Write 29 | } 30 | SAFE_MOD = 0x55; 31 | SAFE_MOD = 0xAA; //Enter Safe mode 32 | GLOBAL_CFG &= ~bDATA_WE; //Disable DataFlash write 33 | SAFE_MOD = 0; //Exit Safe mode 34 | 35 | #else 36 | addr_val; //TBD 37 | return; 38 | #endif 39 | } 40 | 41 | uint8_t eeprom_read_byte (uint8_t addr){ 42 | #if defined(CH551) || defined(CH552) 43 | 44 | ROM_ADDR_H = DATA_FLASH_ADDR >> 8; 45 | ROM_ADDR_L = addr<<1; //Addr must be even 46 | ROM_CTRL = ROM_CMD_READ; 47 | return ROM_DATA_L; 48 | #else 49 | addr; //TBD 50 | return 0; 51 | #endif 52 | } 53 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial0.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | */ 5 | 6 | #include "Arduino.h" 7 | 8 | void Serial0_print_i_func(long i) { 9 | Print_print_i(Serial0_write, i); 10 | } 11 | void Serial0_print_ib_func(long i, __xdata uint8_t base) { 12 | Print_print_ib(Serial0_write, i, base); 13 | } 14 | void Serial0_print_u_func(unsigned long u) { 15 | Print_print_u(Serial0_write, u); 16 | } 17 | void Serial0_print_ub_func(unsigned long u, __xdata uint8_t base) { 18 | Print_print_ib(Serial0_write, u, base); 19 | } 20 | void Serial0_print_s_func(char * s) { 21 | Print_print_s(Serial0_write, s); 22 | } 23 | void Serial0_print_sn_func(char * s, __xdata uint8_t size) { 24 | Print_print_sn(Serial0_write, s, size); 25 | } 26 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial0Float.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | float has a separate file to avoid unnecessary linking 5 | */ 6 | 7 | #include "Arduino.h" 8 | 9 | void Serial0_print_f_func(float f) { 10 | Print_print_f(Serial0_write, f); 11 | } 12 | 13 | void Serial0_print_fd_func(float f, __xdata uint8_t digits) { 14 | Print_print_fd(Serial0_write, f, digits); 15 | } 16 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial1.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | */ 5 | 6 | #include "Arduino.h" 7 | 8 | void Serial1_print_i_func(long i) { 9 | Print_print_i(Serial1_write, i); 10 | } 11 | void Serial1_print_ib_func(long i, __xdata uint8_t base) { 12 | Print_print_ib(Serial1_write, i, base); 13 | } 14 | void Serial1_print_u_func(unsigned long u) { 15 | Print_print_u(Serial1_write, u); 16 | } 17 | void Serial1_print_ub_func(unsigned long u, __xdata uint8_t base) { 18 | Print_print_ib(Serial1_write, u, base); 19 | } 20 | void Serial1_print_s_func(char * s) { 21 | Print_print_s(Serial1_write, s); 22 | } 23 | void Serial1_print_sn_func(char * s, __xdata uint8_t size) { 24 | Print_print_sn(Serial1_write, s, size); 25 | } 26 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial1Float.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | float has a separate file to avoid unnecessary linking 5 | */ 6 | 7 | #include "Arduino.h" 8 | 9 | void Serial1_print_f_func(float f) { 10 | Print_print_f(Serial1_write, f); 11 | } 12 | 13 | void Serial1_print_fd_func(float f, __xdata uint8_t digits) { 14 | Print_print_fd(Serial1_write, f, digits); 15 | } 16 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintUSBSerial.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | */ 5 | 6 | #include "Arduino.h" 7 | 8 | void printNothing(){ //only appear in this file 9 | } 10 | 11 | void USBSerial_print_i_func(long i) { 12 | Print_print_i(USBSerial_write, i); 13 | } 14 | void USBSerial_print_ib_func(long i, __xdata uint8_t base) { 15 | Print_print_ib(USBSerial_write, i, base); 16 | } 17 | void USBSerial_print_u_func(unsigned long u) { 18 | Print_print_u(USBSerial_write, u); 19 | } 20 | void USBSerial_print_ub_func(unsigned long u, __xdata uint8_t base) { 21 | Print_print_ib(USBSerial_write, u, base); 22 | } 23 | void USBSerial_print_s_func(char * s) { 24 | Print_print_s(USBSerial_write, s); 25 | } 26 | void USBSerial_print_sn_func(char * s, __xdata uint8_t size) { 27 | Print_print_sn(USBSerial_write, s, size); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/genericPrintUSBSerialFloat.c: -------------------------------------------------------------------------------- 1 | /* 2 | created by Deqing Sun for use with CH55xduino 3 | need SDCC 13402 or higher version 4 | float has a separate file to avoid unnecessary linking 5 | */ 6 | 7 | #include "Arduino.h" 8 | 9 | void USBSerial_print_f_func(float f) { 10 | Print_print_f(USBSerial_write, f); 11 | } 12 | 13 | void USBSerial_print_fd_func(float f, __xdata uint8_t digits) { 14 | Print_print_fd(USBSerial_write, f, digits); 15 | } 16 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | main.cpp - Main loop for Arduino sketches 3 | Copyright (c) 2005-2013 Arduino Team. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | // make sure to define prototypes for all used interrupts 23 | void USBInterrupt(void); //USBInterrupt does not need to saves the context 24 | void Timer2Interrupt(void) __interrupt (INT_NO_TMR2); //Timer2Interrupt NEEDs to saves the context 25 | void GPIOInterrupt(void) __interrupt (INT_NO_GPIO); //GPIOInterrupt NEEDs to saves the context 26 | 27 | //unsigned char runSerialEvent; 28 | 29 | void DeviceUSBInterrupt(void) __interrupt (INT_NO_USB) //USB interrupt service 30 | { 31 | USBInterrupt(); 32 | } 33 | 34 | // 0x08~0x0F belongs to register bank 1 35 | __idata __at (0x08) volatile uint32_t timer0_overflow_count = 0; 36 | __idata __at (0x0C) volatile uint8_t timer0_overflow_count_5th_byte = 0; 37 | 38 | void Timer0Interrupt(void) __interrupt (INT_NO_TMR0) __using(1); //located in wiring.c, using register bank 1 39 | 40 | void Uart0_ISR(void) __interrupt (INT_NO_UART0) 41 | { 42 | if (RI){ 43 | uart0IntRxHandler(); 44 | RI =0; 45 | } 46 | if (TI){ 47 | uart0IntTxHandler(); 48 | TI =0; 49 | } 50 | } 51 | 52 | void Uart1_ISR(void) __interrupt (INT_NO_UART1) 53 | { 54 | #if defined(CH551) || defined(CH552) 55 | if (U1RI){ 56 | uart1IntRxHandler(); 57 | U1RI =0; 58 | } 59 | if (U1TI){ 60 | uart1IntTxHandler(); 61 | U1TI =0; 62 | } 63 | #elif defined(CH559) 64 | uint8_t interruptStatus = SER1_IIR & 0x0f; 65 | switch(interruptStatus) 66 | { 67 | case U1_INT_RECV_RDY: 68 | uart1IntRxHandler(); 69 | break; 70 | case U1_INT_THR_EMPTY: 71 | uart1IntTxHandler(); 72 | break; 73 | } 74 | #elif defined(CH549) 75 | if (SIF1&bU1RI){ 76 | uart1IntRxHandler(); 77 | SIF1 = bU1RI; 78 | } 79 | if (SIF1&bU1TI){ 80 | uart1IntTxHandler(); 81 | SIF1 = bU1TI; 82 | } 83 | #endif 84 | } 85 | 86 | typedef void (*voidFuncPtr)(void); 87 | extern __xdata voidFuncPtr intFunc[]; 88 | void INT0_ISR(void) __interrupt (INT_NO_INT0) 89 | { 90 | intFunc[0](); 91 | } 92 | void INT1_ISR(void) __interrupt (INT_NO_INT1) 93 | { 94 | intFunc[1](); 95 | } 96 | 97 | #if defined(CH551) || defined(CH552) 98 | __xdata voidFuncPtr touchKeyHandler = NULL; 99 | void TOUCHKEY_ISR(void) __interrupt (INT_NO_TKEY) 100 | { 101 | if (touchKeyHandler!=NULL){ 102 | touchKeyHandler(); 103 | } 104 | } 105 | #endif 106 | 107 | 108 | void main(void) 109 | { 110 | init(); 111 | 112 | //!!!initVariant(); 113 | 114 | setup(); 115 | 116 | for (;;) { 117 | loop(); 118 | if (1) { 119 | #ifndef USER_USB_RAM 120 | USBSerial_flush(); 121 | #endif 122 | //serialEvent(); 123 | } 124 | } 125 | 126 | // return 0; 127 | } 128 | 129 | unsigned char _sdcc_external_startup (void) __nonbanked 130 | { 131 | return 0; 132 | } 133 | 134 | 135 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/weakOverride.c: -------------------------------------------------------------------------------- 1 | //SDCC doesn't support weak attribute. But function in sketch can override function in library. 2 | //Arduino compiles core as an archive and linked with the sketch 3 | 4 | #ifdef USER_USB_RAM 5 | void USBInterrupt(void){ 6 | //weak 7 | } 8 | #endif 9 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/weakOverrideGPIO.c: -------------------------------------------------------------------------------- 1 | //SDCC doesn't support weak attribute. But function in sketch can override function in library. 2 | //Arduino compiles core as an archive and linked with the sketch 3 | void GPIOInterrupt(void) __interrupt { 4 | //weak 5 | } 6 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/weakOverrideTimer2.c: -------------------------------------------------------------------------------- 1 | //SDCC doesn't support weak attribute. But function in sketch can override function in library. 2 | //Arduino compiles core as an archive and linked with the sketch 3 | void Timer2Interrupt(void) __interrupt { 4 | //weak 5 | } 6 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/wiring_digital_fast.h: -------------------------------------------------------------------------------- 1 | #ifndef WiringDigitalFast_h 2 | #define WiringDigitalFast_h 3 | 4 | //To stringify the result of expansion of a macro argument, two levels of macros must be used 5 | 6 | #define XpinModeFast(__PORT,__PIN,__MODE) \ 7 | (__MODE == INPUT ? (P##__PORT##_MOD_OC &= ~(1<<__PIN),P##__PORT##_DIR_PU &= ~(1<<__PIN)) : \ 8 | (__MODE == INPUT_PULLUP ? (P##__PORT##_MOD_OC |= (1<<__PIN),P##__PORT##_DIR_PU |= (1<<__PIN)) : \ 9 | (__MODE == OUTPUT ? (P##__PORT##_MOD_OC &= ~(1<<__PIN),P##__PORT##_DIR_PU |= (1<<__PIN)) : \ 10 | (__MODE == OUTPUT_OD ? (P##__PORT##_MOD_OC |= (1<<__PIN),P##__PORT##_DIR_PU &= ~(1<<__PIN)) : (0) \ 11 | )))) 12 | 13 | #define pinModeFast(__PORT,__PIN,__MODE) XpinModeFast(__PORT,__PIN,__MODE) 14 | 15 | #define XdigitalWriteFast(__PORT,__PIN,__LEVEL) P##__PORT##_##__PIN=__LEVEL 16 | 17 | #define digitalWriteFast(__PORT,__PIN,__LEVEL) XdigitalWriteFast(__PORT,__PIN,__LEVEL) 18 | 19 | #define XdigitalReadFast(__PORT,__PIN) P##__PORT##_##__PIN 20 | 21 | #define digitalReadFast(__PORT,__PIN) XdigitalReadFast(__PORT,__PIN) 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/cores/ch55xduino/wiring_private.h: -------------------------------------------------------------------------------- 1 | #ifndef WiringPrivate_h 2 | #define WiringPrivate_h 3 | 4 | #include 5 | #include 6 | #include "include/ch5xx.h" 7 | #include "include/ch5xx_usb.h" 8 | 9 | #include "Arduino.h" 10 | 11 | 12 | typedef void (*voidFuncPtr)(void); 13 | 14 | #define EXTERNAL_INT_0 0 15 | #define EXTERNAL_INT_1 1 16 | 17 | #define EXTERNAL_NUM_INTERRUPTS 2 18 | 19 | #endif -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/empty.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/ch55x/libraries/Generic_Examples/empty.h -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/AnalogReadSerial/AnalogReadSerial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | AnalogReadSerial 3 | 4 | Reads an analog input on pin P1.1, prints the result to the Serial Monitor. 5 | Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu). 6 | Attach the center pin of a potentiometer to pin P1.1, and the outside pins to 5V and ground. 7 | 8 | This example code is in the public domain. 9 | 10 | http://www.arduino.cc/en/Tutorial/AnalogReadSerial 11 | */ 12 | 13 | #include 14 | 15 | // the setup routine runs once when you press reset: 16 | void setup() { 17 | // No need to init USBSerial 18 | 19 | // By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 20 | pinMode(11, INPUT); 21 | } 22 | 23 | // the loop routine runs over and over again forever: 24 | void loop() { 25 | // read the input on analog pin P1.1. You may use P1.1, P1.4, P1.5 and P3.2 26 | int sensorValue = analogRead(11); 27 | // print out the value you read: 28 | USBSerial_println(sensorValue); 29 | delay(1); // delay in between reads for stability 30 | } 31 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/AnalogReadSerial/AnalogReadSerial.txt: -------------------------------------------------------------------------------- 1 | Read a potentiometer, print its state out to the Arduino Serial Monitor. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/BareMinimum/BareMinimum.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | // put your setup code here, to run once: 3 | 4 | } 5 | 6 | void loop() { 7 | // put your main code here, to run repeatedly: 8 | 9 | } 10 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/BareMinimum/BareMinimum.txt: -------------------------------------------------------------------------------- 1 | The bare minimum of code needed to start an Arduino sketch. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Blink 3 | 4 | Turns an LED on for one second, then off for one second, repeatedly. 5 | 6 | Most Arduinos have an on-board LED you can control. On the simpleCH552 7 | it is attached to digital pin P3.3 8 | 9 | If you want to know what pin the on-board LED is connected to on your Arduino 10 | model, check the Technical Specs of your board at: 11 | https://www.arduino.cc/en/Main/Products 12 | 13 | modified 8 May 2014 14 | by Scott Fitzgerald 15 | modified 2 Sep 2016 16 | by Arturo Guadalupi 17 | modified 8 Sep 2016 18 | by Colby Newman 19 | modified 13 Jun 2020 20 | by Deqing Sun for use with CH55xduino 21 | 22 | This example code is in the public domain. 23 | 24 | http://www.arduino.cc/en/Tutorial/Blink 25 | */ 26 | 27 | #define LED_BUILTIN 33 28 | 29 | // the setup function runs once when you press reset or power the board 30 | void setup() { 31 | // initialize digital pin LED_BUILTIN as an output. 32 | pinMode(LED_BUILTIN, OUTPUT); 33 | } 34 | 35 | // the loop function runs over and over again forever 36 | void loop() { 37 | digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) 38 | delay(1000); // wait for a second 39 | digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW 40 | delay(1000); // wait for a second 41 | } 42 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/Blink/Blink.txt: -------------------------------------------------------------------------------- 1 | Turn an LED on and off. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/DigitalReadSerial/DigitalReadSerial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | DigitalReadSerial 3 | 4 | Reads a digital input on pin P1.1, prints the result to the Serial Monitor 5 | 6 | This example code is in the public domain. 7 | 8 | http://www.arduino.cc/en/Tutorial/DigitalReadSerial 9 | */ 10 | 11 | #include 12 | 13 | // digital pin P1.1 has a pushbutton attached to it. Give it a name: 14 | int pushButton = 11; 15 | 16 | // the setup routine runs once when you press reset: 17 | void setup() { 18 | // No need to init USBSerial 19 | 20 | // make the pushbutton's pin an input: 21 | pinMode(pushButton, INPUT_PULLUP); 22 | } 23 | 24 | // the loop routine runs over and over again forever: 25 | void loop() { 26 | // read the input pin: 27 | int buttonState = digitalRead(pushButton); 28 | // print out the state of the button: 29 | USBSerial_println(buttonState); 30 | delay(1); // delay in between reads for stability 31 | } 32 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/DigitalReadSerial/DigitalReadSerial.txt: -------------------------------------------------------------------------------- 1 | Read a switch, print the state out to the Arduino Serial Monitor. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/Fade/Fade.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Fade 3 | 4 | This example shows how to fade an LED on pin P3.4 using the analogWrite() 5 | function. 6 | 7 | The analogWrite() function uses PWM, so if you want to change the pin you're 8 | using, be sure to use another PWM capable pin. On CH552, You may use 9 | P1.5/P3.0 (mutually exclusive) and P3.4/P3.1 (mutually exclusive) 10 | 11 | This example code is in the public domain. 12 | 13 | http://www.arduino.cc/en/Tutorial/Fade 14 | */ 15 | 16 | int led = 34; // the PWM pin the LED is attached to, You may use P1.5/P3.0 (mutually exclusive) and P3.4/P3.1 (mutually exclusive) 17 | int brightness = 0; // how bright the LED is 18 | int fadeAmount = 5; // how many points to fade the LED by 19 | 20 | // the setup routine runs once when you press reset: 21 | void setup() { 22 | // declare pin 9 to be an output: 23 | pinMode(led, OUTPUT); 24 | } 25 | 26 | // the loop routine runs over and over again forever: 27 | void loop() { 28 | // set the brightness of pin 9: 29 | analogWrite(led, brightness); 30 | 31 | // change the brightness for next time through the loop: 32 | brightness = brightness + fadeAmount; 33 | 34 | // reverse the direction of the fading at the ends of the fade: 35 | if (brightness <= 0 || brightness >= 255) { 36 | fadeAmount = -fadeAmount; 37 | } 38 | // wait for 30 milliseconds to see the dimming effect 39 | delay(30); 40 | } 41 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/Fade/Fade.txt: -------------------------------------------------------------------------------- 1 | Demonstrates the use of analogWrite() to fade an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ReadAnalogVoltage 3 | 4 | Reads an analog input on pin P1.1, converts it to voltage, and prints the result to the Serial Monitor. 5 | Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu). 6 | Attach the center pin of a potentiometer to pin P1.1, and the outside pins to +5V and ground. 7 | 8 | This example code is in the public domain. 9 | 10 | http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage 11 | */ 12 | 13 | #include 14 | 15 | // the setup routine runs once when you press reset: 16 | void setup() { 17 | // No need to init USBSerial 18 | 19 | // By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 20 | pinMode(11, INPUT); 21 | } 22 | 23 | // the loop routine runs over and over again forever: 24 | void loop() { 25 | // read the input on analog pin 0, P1.1: 26 | int sensorValue = analogRead(11); 27 | // Convert the analog reading (which goes from 0 - 255) to a voltage (0 - 5V): 28 | float voltage = sensorValue * (5.0 / 255.0); 29 | // print out the value you read: 30 | USBSerial_println(voltage); 31 | // or with precision: 32 | //USBSerial_println(voltage,1); 33 | 34 | delay(10); 35 | } 36 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.txt: -------------------------------------------------------------------------------- 1 | Reads an analog input and prints the voltage to the Serial Monitor. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/AttachInterrupts/AttachInterrupts.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Attach Interrupts 3 | 4 | Add counter value using attachInterrupt function. 5 | 6 | The circuit: 7 | - Connect a push button between P3.2 and ground 8 | Connect another push button between P3.3 and ground 9 | No debouncing is implemented here. Add a debouncing capacitor if needed. 10 | 11 | created 2020 12 | by Deqing Sun for use with CH55xduino 13 | 14 | This example code is in the public domain. 15 | 16 | http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay 17 | */ 18 | 19 | volatile uint8_t int0Count = 0; 20 | uint8_t int0CountOld = 0; 21 | volatile uint8_t int1Count = 0; 22 | uint8_t int1CountOld = 0; 23 | 24 | #pragma save 25 | #pragma nooverlay 26 | void int0Callback() { 27 | int0Count++; 28 | } 29 | #pragma restore 30 | 31 | #pragma save 32 | #pragma nooverlay 33 | void int1Callback() { 34 | int1Count++; 35 | } 36 | #pragma restore 37 | 38 | void setup() { 39 | // put your setup code here, to run once: 40 | attachInterrupt(0, int0Callback, FALLING);//P3.2 41 | attachInterrupt(1, int1Callback, FALLING);//P3.3 42 | } 43 | 44 | void loop() { 45 | //if you see a number increases a lot when you tie the pin Low with something mechanical, it is caused by bouncing. A small capacitor between pin and GND may help. 46 | if (int0Count != int0CountOld) { 47 | int0CountOld = int0Count; 48 | USBSerial_print("Int0 triggered: "); 49 | USBSerial_println(int0Count); 50 | 51 | if (int0Count>=5){ 52 | USBSerial_println("Int0 detached"); 53 | detachInterrupt(0); 54 | } 55 | } 56 | if (int1Count != int1CountOld) { 57 | int1CountOld = int1Count; 58 | USBSerial_print("Int1 triggered: "); 59 | USBSerial_println(int1Count); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/BlinkFastFunction/BlinkFastFunction.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Blink 3 | 4 | Turns an LED on for one second, then off for one second, repeatedly. 5 | 6 | Most Arduinos have an on-board LED you can control. On the simpleCH552 7 | it is attached to digital pin P3.3 8 | 9 | this example uses marco-based function to accelerate IO access speed. 10 | Note these functions will not disable analog output(PWM) 11 | And they only accept constants. 12 | 13 | If you want to know what pin the on-board LED is connected to on your Arduino 14 | model, check the Technical Specs of your board at: 15 | https://www.arduino.cc/en/Main/Products 16 | 17 | modified 8 May 2014 18 | by Scott Fitzgerald 19 | modified 2 Sep 2016 20 | by Arturo Guadalupi 21 | modified 8 Sep 2016 22 | by Colby Newman 23 | modified 13 Jun 2020 24 | by Deqing Sun for use with CH55xduino 25 | 26 | This example code is in the public domain. 27 | 28 | http://www.arduino.cc/en/Tutorial/Blink 29 | */ 30 | 31 | //P33 32 | #define LED_BUILTIN_PORT 3 33 | #define LED_BUILTIN_PIN 3 34 | 35 | 36 | 37 | // the setup function runs once when you press reset or power the board 38 | void setup() { 39 | // initialize digital pin LED_BUILTIN as an output. 40 | pinModeFast(LED_BUILTIN_PORT,LED_BUILTIN_PIN, OUTPUT); 41 | } 42 | 43 | // the loop function runs over and over again forever 44 | void loop() { 45 | digitalWriteFast(LED_BUILTIN_PORT,LED_BUILTIN_PIN, HIGH); // turn the LED on (HIGH is the voltage level) 46 | delay(1000); // wait for a second 47 | digitalWriteFast(LED_BUILTIN_PORT,LED_BUILTIN_PIN, LOW); // turn the LED off by making the voltage LOW 48 | delay(1000); // wait for a second 49 | } 50 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Blink without Delay 3 | 4 | Turns on and off a light emitting diode (LED) connected to a digital pin, 5 | without using the delay() function. This means that other code can run at the 6 | same time without being interrupted by the LED code. 7 | 8 | The circuit: 9 | - Use the onboard LED. 10 | - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA 11 | and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN 12 | is set to the correct LED pin independent of which board is used. 13 | If you want to know what pin the on-board LED is connected to on your 14 | Arduino model, check the Technical Specs of your board at: 15 | https://www.arduino.cc/en/Main/Products 16 | 17 | created 2005 18 | by David A. Mellis 19 | modified 8 Feb 2010 20 | by Paul Stoffregen 21 | modified 11 Nov 2013 22 | by Scott Fitzgerald 23 | modified 9 Jan 2017 24 | by Arturo Guadalupi 25 | modified 13 Feb 2017 for use with sduino 26 | by Michael Mayer 27 | modified 13 Jun 2020 28 | by Deqing Sun for use with CH55xduino 29 | 30 | This example code is in the public domain. 31 | 32 | http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay 33 | */ 34 | 35 | #define LED_BUILTIN 33 36 | // constants won't change. Used here to set a pin number: 37 | const int ledPin = LED_BUILTIN;// the number of the LED pin 38 | 39 | // Variables will change: 40 | int ledState = LOW; // ledState used to set the LED 41 | 42 | // Generally, you should use "unsigned long" for variables that hold time 43 | // The value will quickly become too large for an int to store 44 | unsigned long previousMillis = 0; // will store last time LED was updated 45 | 46 | // constants won't change: 47 | const long interval = 1000; // interval at which to blink (milliseconds) 48 | 49 | void setup() { 50 | // set the digital pin as output: 51 | pinMode(ledPin, OUTPUT); 52 | } 53 | 54 | void loop() { 55 | // here is where you'd put code that needs to be running all the time. 56 | 57 | // check to see if it's time to blink the LED; that is, if the difference 58 | // between the current time and last time you blinked the LED is bigger than 59 | // the interval at which you want to blink the LED. 60 | unsigned long currentMillis = millis(); 61 | 62 | if (currentMillis - previousMillis >= interval) { 63 | // save the last time you blinked the LED 64 | previousMillis = currentMillis; 65 | 66 | // if the LED is off turn it on and vice-versa: 67 | if (ledState == LOW) { 68 | ledState = HIGH; 69 | } else { 70 | ledState = LOW; 71 | } 72 | 73 | // set the LED with the ledState of the variable: 74 | digitalWrite(ledPin, ledState); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.txt: -------------------------------------------------------------------------------- 1 | Blinking an LED without using the delay() function. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/Button/Button.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Button 3 | 4 | Turns on and off a light emitting diode(LED) connected to digital pin P3.3, 5 | when pressing a pushbutton attached to pin P1.1. 6 | 7 | The circuit: 8 | - LED attached from pin P3.3 to ground 9 | - pushbutton attached to pin P1.1 from ground 10 | 11 | - Note: on the simpleCH552 breakout board there is already an LED on the board 12 | attached to pin P3.3. 13 | 14 | created 2005 15 | by DojoDave 16 | modified 30 Aug 2011 17 | by Tom Igoe 18 | modified 13 Feb 2017 for use with sduino 19 | by Michael Mayer 20 | modified 13 Jun 2020 21 | by Deqing Sun for use with CH55xduino 22 | 23 | This example code is in the public domain. 24 | 25 | http://www.arduino.cc/en/Tutorial/Button 26 | */ 27 | 28 | // constants won't change. They're used here to set pin numbers: 29 | const int buttonPin = 11; // the number of the pushbutton pin 30 | const int ledPin = 33; // the number of the LED pin 31 | 32 | // variables will change: 33 | int buttonState = 0; // variable for reading the pushbutton status 34 | 35 | void setup() { 36 | // initialize the LED pin as an output: 37 | pinMode(ledPin, OUTPUT); 38 | // initialize the pushbutton pin as an input: 39 | pinMode(buttonPin, INPUT_PULLUP); 40 | } 41 | 42 | void loop() { 43 | // read the state of the pushbutton value: 44 | buttonState = digitalRead(buttonPin); 45 | 46 | // check if the pushbutton is pressed. If it is, the buttonState is HIGH: 47 | if (buttonState == LOW) { 48 | // turn LED on: 49 | digitalWrite(ledPin, HIGH); 50 | } else { 51 | // turn LED off: 52 | digitalWrite(ledPin, LOW); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/Button/Button.txt: -------------------------------------------------------------------------------- 1 | Use a pushbutton to control an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/Debounce/Debounce.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Debounce 3 | 4 | Each time the input pin goes from LOW to HIGH (e.g. because of a push-button 5 | press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a 6 | minimum delay between toggles to debounce the circuit (i.e. to ignore noise). 7 | 8 | The circuit: 9 | - LED attached from pin P3.3 to ground 10 | - pushbutton attached from pin P1.1 to ground 11 | 12 | - Note: On the simpleCH552 breakout board, there is already an LED on the board connected 13 | to pin P3.3, so you don't need any extra components for this example. 14 | 15 | created 21 Nov 2006 16 | by David A. Mellis 17 | modified 30 Aug 2011 18 | by Limor Fried 19 | modified 28 Dec 2012 20 | by Mike Walters 21 | modified 30 Aug 2016 22 | by Arturo Guadalupi 23 | modified 13 Feb 2017 for use with sduino 24 | by Michael Mayer 25 | modified 13 Jun 2020 26 | by Deqing Sun for use with CH55xduino 27 | 28 | This example code is in the public domain. 29 | 30 | http://www.arduino.cc/en/Tutorial/Debounce 31 | */ 32 | 33 | // constants won't change. They're used here to set pin numbers: 34 | const int buttonPin = 11; // the number of the pushbutton pin 35 | const int ledPin = 33; // the number of the LED pin 36 | 37 | // Variables will change: 38 | int ledState = HIGH; // the current state of the output pin 39 | int buttonState; // the current reading from the input pin 40 | int lastButtonState = LOW; // the previous reading from the input pin 41 | 42 | // the following variables are unsigned longs because the time, measured in 43 | // milliseconds, will quickly become a bigger number than can be stored in an int. 44 | unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 45 | unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers 46 | 47 | void setup() { 48 | pinMode(buttonPin, INPUT_PULLUP); 49 | pinMode(ledPin, OUTPUT); 50 | 51 | // set initial LED state 52 | digitalWrite(ledPin, ledState); 53 | } 54 | 55 | void loop() { 56 | // read the state of the switch into a local variable: 57 | int reading = digitalRead(buttonPin); 58 | 59 | // check to see if you just pressed the button 60 | // (i.e. the input went from LOW to HIGH), and you've waited long enough 61 | // since the last press to ignore any noise: 62 | 63 | // If the switch changed, due to noise or pressing: 64 | if (reading != lastButtonState) { 65 | // reset the debouncing timer 66 | lastDebounceTime = millis(); 67 | } 68 | 69 | if ((millis() - lastDebounceTime) > debounceDelay) { 70 | // whatever the reading is at, it's been there for longer than the debounce 71 | // delay, so take it as the actual current state: 72 | 73 | // if the button state has changed: 74 | if (reading != buttonState) { 75 | buttonState = reading; 76 | 77 | // only toggle the LED if the new button state is LOW 78 | if (buttonState == LOW) { 79 | ledState = !ledState; 80 | } 81 | } 82 | } 83 | 84 | // set the LED: 85 | digitalWrite(ledPin, ledState); 86 | 87 | // save the reading. Next time through the loop, it'll be the lastButtonState: 88 | lastButtonState = reading; 89 | } 90 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/Debounce/Debounce.txt: -------------------------------------------------------------------------------- 1 | Read a pushbutton, filtering noise. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Input Pull-up Serial 3 | 4 | This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital 5 | input on pin P1.1 and prints the results to the Serial Monitor. 6 | 7 | The circuit: 8 | - momentary switch attached from pin P1.1 to ground 9 | - built-in LED on pin P3.3 10 | 11 | Unlike pinMode(INPUT), there is no pull-down or pull-up resistor necessary. An internal 12 | 70K-ohm resistor is pulled to 5V. This configuration causes the input to read 13 | HIGH when the switch is open, and LOW when it is closed. 14 | 15 | created 14 Mar 2012 16 | by Scott Fitzgerald 17 | modified 13 Feb 2017 for use with sduino 18 | by Michael Mayer 19 | modified 13 Jun 2020 20 | by Deqing Sun for use with CH55xduino 21 | 22 | This example code is in the public domain. 23 | 24 | http://www.arduino.cc/en/Tutorial/InputPullupSerial 25 | */ 26 | 27 | #include 28 | 29 | void setup() { 30 | // No need to init USBSerial 31 | 32 | //configure pin 2 as an input and enable the internal pull-up resistor 33 | pinMode(11, INPUT_PULLUP); 34 | pinMode(33, OUTPUT); 35 | 36 | } 37 | 38 | void loop() { 39 | //read the pushbutton value into a variable 40 | int sensorVal = digitalRead(11); 41 | //print out the value of the pushbutton 42 | USBSerial_println(sensorVal); 43 | 44 | // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes 45 | // HIGH when it's open, and LOW when it's pressed. Turn on pin 3 when the 46 | // button's pressed, and off when it's not: 47 | if (sensorVal == HIGH) { 48 | digitalWrite(33, LOW); 49 | } else { 50 | digitalWrite(33, HIGH); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/DigitalInputPullup/DigitalInputPullup.txt: -------------------------------------------------------------------------------- 1 | Demonstrates the use of INPUT_PULLUP with pinMode(). -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/StateChangeDetection/StateChangeDetection.ino: -------------------------------------------------------------------------------- 1 | /* 2 | State change detection (edge detection) 3 | 4 | Often, you don't need to know the state of a digital input all the time, but 5 | you just need to know when the input changes from one state to another. 6 | For example, you want to know when a button goes from OFF to ON. This is called 7 | state change detection, or edge detection. 8 | 9 | This example shows how to detect when a button or button changes from off to on 10 | and on to off. 11 | 12 | The circuit: 13 | - pushbutton attached to pin P1.1 from ground 14 | - LED attached from pin P3.3 to VCC (or use the built-in LED on 15 | the simpleCH552 breakout board) 16 | 17 | created 27 Sep 2005 18 | modified 30 Aug 2011 19 | by Tom Igoe 20 | modified 13 Feb 2017 for use with sduino 21 | by Michael Mayer 22 | modified 13 Jun 2020 23 | by Deqing Sun for use with CH55xduino 24 | 25 | This example code is in the public domain. 26 | 27 | http://www.arduino.cc/en/Tutorial/ButtonStateChange 28 | */ 29 | 30 | #include 31 | 32 | // this constant won't change: 33 | const int buttonPin = 11; // the pin that the pushbutton is attached to 34 | const int ledPin = 33; // the pin that the LED is attached to 35 | 36 | // Variables will change: 37 | int buttonPushCounter = 0; // counter for the number of button presses 38 | int buttonState = 0; // current state of the button 39 | int lastButtonState = 0; // previous state of the button 40 | 41 | void setup() { 42 | // initialize the button pin as a input: 43 | pinMode(buttonPin, INPUT_PULLUP); 44 | // initialize the LED as an output: 45 | pinMode(ledPin, OUTPUT); 46 | // No need to init USBSerial 47 | } 48 | 49 | 50 | void loop() { 51 | // read the pushbutton input pin: 52 | buttonState = digitalRead(buttonPin); 53 | 54 | // compare the buttonState to its previous state 55 | if (buttonState != lastButtonState) { 56 | // if the state has changed, increment the counter 57 | if (buttonState == HIGH) { 58 | // if the current state is HIGH then the button went from off to on: 59 | buttonPushCounter++; 60 | USBSerial_println("on"); 61 | USBSerial_print("number of button pushes: "); 62 | USBSerial_println(buttonPushCounter); 63 | } else { 64 | // if the current state is LOW then the button went from on to off: 65 | USBSerial_println("off"); 66 | } 67 | // Delay a little bit to avoid bouncing 68 | delay(50); 69 | } 70 | // save the current state as the last state, for next time through the loop 71 | lastButtonState = buttonState; 72 | 73 | 74 | // turns on the LED every four button pushes by checking the modulo of the 75 | // button push counter. the modulo function gives you the remainder of the 76 | // division of two numbers: 77 | if (buttonPushCounter % 4 == 0) { 78 | digitalWrite(ledPin, HIGH); 79 | } else { 80 | digitalWrite(ledPin, LOW); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/02.Digital/StateChangeDetection/StateChangeDetection.txt: -------------------------------------------------------------------------------- 1 | Counting the number of button pushes. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Analog input, analog output, serial output 3 | 4 | Reads an analog input pin, maps the result to a range from 0 to 255 and uses 5 | the result to set the pulse width modulation (PWM) of an output pin. 6 | Also prints the results to the Serial Monitor. 7 | 8 | The circuit: 9 | - potentiometer connected to analog pin P1.1. 10 | Center pin of the potentiometer goes to the analog pin. 11 | side pins of the potentiometer go to +5V and ground 12 | - LED connected from digital pin P3.4 to ground 13 | 14 | created 29 Dec. 2008 15 | modified 9 Apr 2012 16 | by Tom Igoe 17 | modified 28 Feb 2017 for use with sduino 18 | by Michael Mayer 19 | modified 13 Jun 2020 20 | by Deqing Sun for use with CH55xduino 21 | 22 | This example code is in the public domain. 23 | 24 | http://www.arduino.cc/en/Tutorial/AnalogInOutSerial 25 | */ 26 | 27 | // These constants won't change. They're used to give names to the pins used: 28 | const int analogInPin = 11; // Analog input pin that the potentiometer is attached to. You may use P1.1, P1.4, P1.5 and P3.2 29 | const int analogOutPin = 34; // Analog output pin that the LED is attached to. You may use P1.5/P3.0 (mutually exclusive) and P3.4/P3.1 (mutually exclusive) 30 | 31 | int sensorValue = 0; // value read from the pot 32 | int outputValue = 0; // value output to the PWM (analog out) 33 | 34 | void setup() { 35 | // No need to init USBSerial 36 | 37 | //By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 38 | pinMode(analogInPin,INPUT); 39 | } 40 | 41 | void loop() { 42 | // read the analog in value: 43 | sensorValue = analogRead(analogInPin); 44 | // No need to map, the are both 0~255: 45 | outputValue = sensorValue; 46 | // change the analog out value: 47 | analogWrite(analogOutPin, outputValue); 48 | 49 | // print the results to the Serial Monitor: 50 | USBSerial_print("sensor = "); 51 | USBSerial_print(sensorValue); 52 | USBSerial_print("\t output = "); 53 | USBSerial_println(outputValue); 54 | 55 | // wait 2 milliseconds before the next loop for the analog-to-digital 56 | // converter to settle after the last reading: 57 | delay(2); 58 | } 59 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.txt: -------------------------------------------------------------------------------- 1 | Read an analog input pin, map the result, and then use that data to dim or brighten an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/AnalogInput/AnalogInput.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Analog Input 3 | 4 | Demonstrates analog input by reading an analog sensor on analog pin P1.1 and 5 | turning on and off a light emitting diode(LED) connected to digital pin P3.3. 6 | The amount of time the LED will be on and off depends on the value obtained 7 | by analogRead(). 8 | 9 | The circuit: 10 | - potentiometer 11 | center pin of the potentiometer to the analog input P1.1 12 | one side pin (either one) to ground 13 | the other side pin to +5V 14 | - LED 15 | anode (long leg) attached to digital output P3.3 16 | cathode (short leg) attached to ground 17 | 18 | - Note: because the simpleCH552 breakout board has a built-in LED attached to 19 | pin P3.3 on the board, the LED is optional. 20 | 21 | created by David Cuartielles 22 | modified 30 Aug 2011 23 | By Tom Igoe 24 | modified 13 Jun 2020 25 | by Deqing Sun for use with CH55xduino 26 | 27 | This example code is in the public domain. 28 | 29 | http://www.arduino.cc/en/Tutorial/AnalogInput 30 | */ 31 | #define LED_BUILTIN 33 32 | 33 | int sensorPin = 11; // select the input pin for the potentiometer. You may use P1.1, P1.4, P1.5 and P3.2 34 | int ledPin = LED_BUILTIN; // select the pin for the LED 35 | int sensorValue = 0; // variable to store the value coming from the sensor 36 | 37 | void setup() { 38 | // declare the ledPin as an OUTPUT: 39 | pinMode(ledPin, OUTPUT); 40 | //By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 41 | pinMode(sensorPin, INPUT); 42 | } 43 | 44 | void loop() { 45 | // read the value from the sensor: 46 | sensorValue = analogRead(sensorPin); 47 | // turn the ledPin on 48 | digitalWrite(ledPin, HIGH); 49 | // stop the program for milliseconds: 50 | delay(sensorValue); 51 | // turn the ledPin off: 52 | digitalWrite(ledPin, LOW); 53 | // stop the program for for milliseconds: 54 | delay(sensorValue); 55 | } 56 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/AnalogInput/AnalogInput.txt: -------------------------------------------------------------------------------- 1 | Use a potentiometer to control the blinking of an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Calibration/Calibration.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Calibration 3 | 4 | Demonstrates one technique for calibrating sensor input. The sensor readings 5 | during the first five seconds of the sketch execution define the minimum and 6 | maximum of expected values attached to the sensor pin. 7 | 8 | The sensor minimum and maximum initial values may seem backwards. Initially, 9 | you set the minimum high and listen for anything lower, saving it as the new 10 | minimum. Likewise, you set the maximum low and listen for anything higher as 11 | the new maximum. 12 | 13 | The circuit: 14 | - analog sensor (potentiometer will do) attached to analog input P1.1 15 | - LED attached from digital pin P3.4 to ground 16 | 17 | created 29 Oct 2008 18 | by David A Mellis 19 | modified 30 Aug 2011 20 | by Tom Igoe 21 | modified 28 Feb 2017 for use with sduino 22 | by Michael Mayer 23 | modified 13 Jun 2020 24 | by Deqing Sun for use with CH55xduino 25 | 26 | This example code is in the public domain. 27 | 28 | http://www.arduino.cc/en/Tutorial/Calibration 29 | */ 30 | 31 | // These constants won't change: 32 | const int sensorPin = 11; // pin that the sensor is attached to. You may use P1.1, P1.4, P1.5 and P3.2 33 | const int ledPin = 33; // pin that the LED is attached to 34 | const int ledPinFade = 34; // pin that the LED is attached to. You may use P1.5/P3.0 (mutually exclusive) and P3.4/P3.1 (mutually exclusive) 35 | 36 | 37 | // variables: 38 | int sensorValue = 0; // the sensor value 39 | int sensorMin = 1023; // minimum sensor value 40 | int sensorMax = 0; // maximum sensor value 41 | 42 | 43 | void setup() { 44 | // turn on LED to signal the start of the calibration period: 45 | pinMode(ledPin, OUTPUT); 46 | pinMode(ledPinFade, OUTPUT); 47 | // By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 48 | pinMode(sensorPin, INPUT); 49 | digitalWrite(ledPin, HIGH); 50 | 51 | // calibrate during the first five seconds 52 | while (millis() < 5000) { 53 | sensorValue = analogRead(sensorPin); 54 | 55 | // record the maximum sensor value 56 | if (sensorValue > sensorMax) { 57 | sensorMax = sensorValue; 58 | } 59 | 60 | // record the minimum sensor value 61 | if (sensorValue < sensorMin) { 62 | sensorMin = sensorValue; 63 | } 64 | } 65 | 66 | // signal the end of the calibration period 67 | digitalWrite(ledPin, LOW); 68 | } 69 | 70 | void loop() { 71 | // read the sensor: 72 | sensorValue = analogRead(sensorPin); 73 | 74 | // apply the calibration to the sensor reading 75 | sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); 76 | 77 | // in case the sensor value is outside the range seen during calibration 78 | sensorValue = constrain(sensorValue, 0, 255); 79 | 80 | // fade the LED using the calibrated value: 81 | analogWrite(ledPinFade, sensorValue); 82 | } 83 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Calibration/Calibration.txt: -------------------------------------------------------------------------------- 1 | Define a maximum and minimum for expected analog sensor values. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Fading/Fading.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Fading 3 | 4 | This example shows how to fade an LED using the analogWrite() function. 5 | 6 | The circuit: 7 | - LED attached from digital pin P3.4 to ground. 8 | 9 | created 1 Nov 2008 10 | by David A. Mellis 11 | modified 30 Aug 2011 12 | by Tom Igoe 13 | modified 28 Feb 2017 for use with sduino 14 | by Michael Mayer 15 | modified 13 Jun 2020 16 | by Deqing Sun for use with CH55xduino 17 | 18 | This example code is in the public domain. 19 | 20 | http://www.arduino.cc/en/Tutorial/Fading 21 | */ 22 | 23 | int ledPin = 34; // LED connected to digital pin 9. You may use P1.5/P3.0 (mutually exclusive) and P3.4/P3.1 (mutually exclusive) 24 | 25 | void setup() { 26 | // nothing happens in setup 27 | } 28 | 29 | void loop() { 30 | // fade in from min to max in increments of 5 points: 31 | for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 32 | // sets the value (range from 0 to 255): 33 | analogWrite(ledPin, fadeValue); 34 | // wait for 30 milliseconds to see the dimming effect 35 | delay(30); 36 | } 37 | 38 | // fade out from max to min in increments of 5 points: 39 | for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 40 | // sets the value (range from 0 to 255): 41 | analogWrite(ledPin, fadeValue); 42 | // wait for 30 milliseconds to see the dimming effect 43 | delay(30); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Fading/Fading.txt: -------------------------------------------------------------------------------- 1 | Use an analog output (PWM pin) to fade an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Smoothing/Smoothing.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Smoothing 4 | 5 | Reads repeatedly from an analog input, calculating a running average and 6 | printing it to the computer. Keeps ten readings in an array and continually 7 | averages them. 8 | 9 | The circuit: 10 | - analog sensor (potentiometer will do) attached to analog input P1.1 11 | 12 | created 22 Apr 2007 13 | by David A. Mellis 14 | modified 9 Apr 2012 15 | by Tom Igoe 16 | modified 28 Feb 2017 for use with sduino 17 | by Michael Mayer 18 | modified 13 Jun 2020 19 | by Deqing Sun for use with CH55xduino 20 | 21 | This example code is in the public domain. 22 | 23 | http://www.arduino.cc/en/Tutorial/Smoothing 24 | */ 25 | 26 | // Define the number of samples to keep track of. The higher the number, the 27 | // more the readings will be smoothed, but the slower the output will respond to 28 | // the input. Using a constant rather than a normal variable lets us use this 29 | // value to determine the size of the readings array. 30 | #define numReadings 10 31 | 32 | //This is a fairly large array, store it in external memory with keyword __xdata 33 | __xdata int readings[numReadings]; // the readings from the analog input. 34 | int readIndex = 0; // the index of the current reading 35 | int total = 0; // the running total 36 | int average = 0; // the average 37 | 38 | int inputPin = 11; // You may use P1.1, P1.4, P1.5 and P3.2 39 | 40 | void setup() { 41 | // No need to init USBSerial 42 | // By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. 43 | pinMode(inputPin,INPUT); 44 | 45 | // initialize all the readings to 0: 46 | for (int thisReading = 0; thisReading < numReadings; thisReading++) { 47 | readings[thisReading] = 0; 48 | } 49 | } 50 | 51 | void loop() { 52 | // subtract the last reading: 53 | total = total - readings[readIndex]; 54 | // read from the sensor: 55 | readings[readIndex] = analogRead(inputPin); 56 | // add the reading to the total: 57 | total = total + readings[readIndex]; 58 | // advance to the next position in the array: 59 | readIndex = readIndex + 1; 60 | 61 | // if we're at the end of the array... 62 | if (readIndex >= numReadings) { 63 | // ...wrap around to the beginning: 64 | readIndex = 0; 65 | } 66 | 67 | // calculate the average: 68 | average = total / numReadings; 69 | // send it to the computer as ASCII digits 70 | USBSerial_println(average); 71 | delay(1); // delay in between reads for stability 72 | } 73 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/03.Analog/Smoothing/Smoothing.txt: -------------------------------------------------------------------------------- 1 | Smooth multiple readings of an analog input. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/ASCIITable/ASCIITable.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ASCII table 3 | 4 | Prints out byte values in all possible formats: 5 | - as raw binary values 6 | - as ASCII-encoded decimal, hex, octal, and binary values 7 | 8 | For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII 9 | 10 | The circuit: No external hardware needed. 11 | 12 | created 2006 13 | by Nicholas Zambetti 14 | modified 9 Apr 2012 15 | by Tom Igoe 16 | modified 28 Feb 2017 for use with sduino 17 | by Michael Mayer 18 | modified 13 Jun 2020 19 | by Deqing Sun for use with CH55xduino 20 | 21 | This example code is in the public domain. 22 | 23 | http://www.arduino.cc/en/Tutorial/ASCIITable 24 | */ 25 | 26 | void setup() { 27 | // No need to init USBSerial 28 | 29 | while (!USBSerial()) { 30 | ; // wait for serial port to connect. Needed for native USB port only 31 | } 32 | 33 | // prints title with ending line break 34 | USBSerial_println("ASCII Table ~ Character Map"); 35 | } 36 | 37 | // first visible ASCIIcharacter '!' is number 33: 38 | int thisByte = 33; 39 | // you can also write ASCII characters in single quotes. 40 | // for example, '!' is the same as 33, so you could also use this: 41 | // int thisByte = '!'; 42 | 43 | void loop() { 44 | // prints value unaltered, i.e. the raw binary version of the byte. 45 | // The Serial Monitor interprets all bytes as ASCII, so 33, the first number, 46 | // will show up as '!' 47 | USBSerial_write(thisByte); 48 | 49 | USBSerial_print(", dec: "); 50 | // prints value as string as an ASCII-encoded decimal (base 10). 51 | // Decimal is the default format for Serial.print() and Serial.println(), 52 | // so no modifier is needed: 53 | USBSerial_print(thisByte); 54 | // But you can declare the modifier for decimal if you want to. 55 | // this also works if you uncomment it: 56 | 57 | // Serial_print(thisByte, DEC); 58 | 59 | 60 | USBSerial_print(", hex: "); 61 | // prints value as string in hexadecimal (base 16): 62 | USBSerial_print(thisByte, HEX); 63 | 64 | USBSerial_print(", oct: "); 65 | // prints value as string in octal (base 8); 66 | USBSerial_print(thisByte, OCT); 67 | 68 | USBSerial_print(", bin: "); 69 | // prints value as string in binary (base 2) also prints ending line break: 70 | USBSerial_println(thisByte, BIN); 71 | 72 | // if printed last visible character '~' or 126, stop: 73 | if (thisByte == 126) { // you could also use if (thisByte == '~') { 74 | // This loop loops forever and does nothing 75 | while (true) { 76 | continue; 77 | } 78 | } 79 | // go on to the next character 80 | thisByte++; 81 | } 82 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/ASCIITable/ASCIITable.txt: -------------------------------------------------------------------------------- 1 | Demonstrates Arduino's advanced serial output functions. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/Dimmer/Dimmer.txt: -------------------------------------------------------------------------------- 1 | Move the mouse to change the brightness of an LED. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/Echo/Echo.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Echo 3 | 4 | A simple example echoes back every line of data it receives. 5 | Also it prints how many lines it has echoed. 6 | 7 | created 2020 8 | by Deqing Sun for use with CH55xduino 9 | 10 | This example code is in the public domain. 11 | 12 | */ 13 | 14 | //This is a fairly large array, store it in external memory with keyword __xdata 15 | __xdata char recvStr[64]; 16 | uint8_t recvStrPtr = 0; 17 | bool stringComplete = false; 18 | uint16_t echoCounter = 0; 19 | 20 | void setup() { 21 | // put your setup code here, to run once: 22 | } 23 | 24 | void loop() { 25 | 26 | while (USBSerial_available()) { 27 | char serialChar = USBSerial_read(); 28 | if ((serialChar == '\n') || (serialChar == '\r') ) { 29 | recvStr[recvStrPtr] = '\0'; 30 | if (recvStrPtr > 0) { 31 | stringComplete = true; 32 | break; 33 | } 34 | } else { 35 | recvStr[recvStrPtr] = serialChar; 36 | recvStrPtr++; 37 | if (recvStrPtr == 63) { 38 | recvStr[recvStrPtr] = '\0'; 39 | stringComplete = true; 40 | break; 41 | } 42 | } 43 | } 44 | 45 | if (stringComplete) { 46 | USBSerial_print("ECHO:"); 47 | USBSerial_println(recvStr); 48 | USBSerial_flush(); 49 | stringComplete = false; 50 | recvStrPtr = 0; 51 | 52 | echoCounter++; 53 | USBSerial_print("echo count: "); 54 | USBSerial_println(echoCounter); 55 | USBSerial_flush(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/Graph/Graph.txt: -------------------------------------------------------------------------------- 1 | Send data to the computer and graph it in Processing. -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/SumpLogicAnalyzer/captureInline5mhz.ino: -------------------------------------------------------------------------------- 1 | void captureInline5mhz() { 2 | 3 | // basic trigger, wait until all trigger conditions are met on port. 4 | // this needs further testing, but basic tests work as expected. 5 | 6 | // disable interrupts during capture to maintain precision. 7 | // we cannot afford any timing interference so we absolutely 8 | // cannot have any interrupts firing. 9 | 10 | EA = 0; 11 | 12 | //prepare R0 DPTR 13 | __asm__(" mov r0,#_swapByte \n" 14 | " inc _XBUS_AUX \n" //select DPTR1 15 | " mov dptr,#(_logicdata-0) \n" 16 | " dec _XBUS_AUX \n" //select DPTR0 , set it back 17 | ); 18 | 19 | __asm__(" sjmp notRet5M$ \n" //only return when UEP2_CTRL changes 20 | 21 | "abortSample5M$: \n" 22 | " setb _EA \n" 23 | " ret \n" 24 | 25 | "notRet5M$:" 26 | " mov a,_trigger \n" //if trigger is set 27 | " jz NoTrigger5M$ \n" 28 | " mov r5,_UEP2_CTRL \n" //backup UEP2_CTRL 29 | 30 | "checkTrigger5M$:" 31 | 32 | " mov a,_UEP2_CTRL \n" //abort if UEP2_CTRL changes 33 | " xrl a,r5 \n" 34 | " jnz abortSample5M$ \n" 35 | 36 | " mov a,_P1 \n" //(trigger_values ^ P1) & trigger. If result is 0, got triggered. 37 | " xrl a,_trigger_values\n" 38 | " anl a,_trigger \n" 39 | " jnz checkTrigger5M$ \n" 40 | 41 | "NoTrigger5M$: \n" 42 | ); 43 | 44 | //P1 is at address 0x90, but it can not be indirectly accessed, because it is on "top" of the ram 45 | 46 | //a5-> MOVX @DPTR1,A & INC DPTR1 47 | 48 | //===768 pairs 49 | 50 | __asm__(" mov r6,#153 \n" 51 | 52 | ".odd \n loop10Samples$: \n" //even address is faster, not sure why need odd. 53 | 54 | " mov a,_P1 \n swap a \n mov @r0,a \n " 55 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 56 | 57 | " mov a,_P1 \n swap a \n mov @r0,a \n " 58 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 59 | 60 | " nop \n" 61 | 62 | " mov a,_P1 \n swap a \n mov @r0,a \n " 63 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 64 | 65 | " nop \n" 66 | 67 | " mov a,_P1 \n swap a \n mov @r0,a \n " 68 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 69 | 70 | " dec r6 \n" 71 | 72 | " mov a,_P1 \n swap a \n mov @r0,a \n " 73 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 74 | 75 | " mov a,r6 \n" //1+2+2 = 5clk for loop 76 | " jnz loop10Samples$\n"); 77 | 78 | 79 | //765 pair finished, 3 more needed. 80 | __asm__( 81 | " mov a,_P1 \n swap a \n mov @r0,a \n " 82 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 83 | 84 | " nop \n" 85 | 86 | " mov a,_P1 \n swap a \n mov @r0,a \n " 87 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n" 88 | 89 | " nop \n" 90 | 91 | " mov a,_P1 \n swap a \n mov @r0,a \n " 92 | " mov a,_P1 \n xchd A,@r0 \n .db #0xa5 \n"); 93 | 94 | //===768 pairs end 95 | 96 | 97 | // re-enable interrupts now that we're done sampling. 98 | EA = 1; 99 | 100 | // dump the samples back to the SUMP client. nothing special 101 | // is done for any triggers, this is effectively the 0/100 buffer split. 102 | 103 | //pulseview use reversed order 104 | 105 | for (unsigned int i = 0 ; i < readCount; i++ ) { 106 | unsigned int rev_i = readCount - i - 1; 107 | if ((rev_i & 1) == 0) { 108 | USBSerial_write(logicdata[rev_i / 2] & 0x0F); 109 | } else { 110 | USBSerial_write((logicdata[rev_i / 2]) >> 4); 111 | } 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/UsbSerialAdaptor/UsbSerialAdaptor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | UsbSerialAdaptor 3 | 4 | A simple example turns CH552 to a serial adaptor, 5 | relaying data between USB serial and Serial0. 6 | 7 | Baudrate of Serial0 is determined by USB serial's rate. 8 | 9 | created 2020 10 | by Deqing Sun for use with CH55xduino 11 | 12 | This example code is in the public domain. 13 | 14 | */ 15 | 16 | extern __xdata uint8_t LineCoding[]; //lineCoding of CDC is located in this array 17 | __xdata uint32_t oldBaudRate = 9600; 18 | 19 | void setup() { 20 | Serial0_begin(9600); 21 | } 22 | 23 | void loop() { 24 | if (USBSerial_available()) { 25 | char serialChar = USBSerial_read(); 26 | Serial0_write(serialChar); 27 | } 28 | if (Serial0_available()) { 29 | char serialChar = Serial0_read(); 30 | USBSerial_write(serialChar); 31 | } 32 | 33 | __xdata uint32_t currentBaudRate = *((__xdata uint32_t *)LineCoding); //both linecoding and sdcc are little-endian 34 | 35 | if (oldBaudRate != currentBaudRate) { 36 | oldBaudRate = currentBaudRate; 37 | Serial0_begin(currentBaudRate); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/CDCinUserCode.ino: -------------------------------------------------------------------------------- 1 | /* 2 | CDCinUserCode 3 | 4 | A simple example echoes back every line of data it receives. 5 | Also it prints how many lines it has echoed. 6 | 7 | created 2020 8 | by Deqing Sun for use with CH55xduino 9 | 10 | This example code is in the public domain. 11 | 12 | */ 13 | 14 | 15 | #ifndef USER_USB_RAM 16 | #error "This example needs to be compiled with a USER USB setting" 17 | #endif 18 | 19 | #include "src/userUsbCdc/USBCDC.h" 20 | 21 | //This is a fairly large array, store it in external memory with keyword __xdata 22 | __xdata char recvStr[64]; 23 | uint8_t recvStrPtr = 0; 24 | bool stringComplete = false; 25 | uint16_t echoCounter = 0; 26 | 27 | void setup() { 28 | USBInit(); 29 | } 30 | 31 | void loop() { 32 | while (USBSerial_available()) { 33 | char serialChar = USBSerial_read(); 34 | if ((serialChar == '\n') || (serialChar == '\r') ) { 35 | recvStr[recvStrPtr] = '\0'; 36 | if (recvStrPtr > 0) { 37 | stringComplete = true; 38 | break; 39 | } 40 | } else { 41 | recvStr[recvStrPtr] = serialChar; 42 | recvStrPtr++; 43 | if (recvStrPtr == 63) { 44 | recvStr[recvStrPtr] = '\0'; 45 | stringComplete = true; 46 | break; 47 | } 48 | } 49 | } 50 | 51 | if (stringComplete) { 52 | USBSerial_print("ECHO:"); 53 | USBSerial_println(recvStr); 54 | USBSerial_flush(); 55 | stringComplete = false; 56 | recvStrPtr = 0; 57 | 58 | echoCounter++; 59 | USBSerial_print("echo count: "); 60 | USBSerial_println(echoCounter); 61 | USBSerial_flush(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/src/userUsbCdc/USBCDC.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_CDC_H__ 2 | #define __USB_CDC_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | void USBInit(void); 13 | 14 | #ifdef __cplusplus 15 | } // extern "C" 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/src/userUsbCdc/USBconstant.c: -------------------------------------------------------------------------------- 1 | #include "USBconstant.h" 2 | 3 | 4 | //Device descriptor 5 | __code uint8_t DevDesc[] = { 6 | 0x12,0x01, 7 | 0x10,0x01, //USB spec release number in BCD format, USB1.1 (0x10, 0x01). 8 | 0xEF,0x02,0x01, //bDeviceClass, bDeviceSubClass, bDeviceProtocol 9 | DEFAULT_ENDP0_SIZE, //bNumConfigurations 10 | 0x09,0x12,0x50,0xC5, // VID PID 11 | 0x01,0x01, //version 12 | 0x01,0x02,0x03, //bString 13 | 0x01 //bNumConfigurations 14 | }; 15 | 16 | __code uint16_t DevDescLen = sizeof(DevDesc); 17 | 18 | __code uint8_t CfgDesc[] ={ 19 | 0x09,0x02,sizeof(CfgDesc) & 0xff,sizeof(CfgDesc) >> 8, 20 | 0x02,0x01,0x00,0x80,0x64, //Configuration descriptor (2 interfaces) 21 | // Interface Association Descriptor, IAD, this packes following 2 interfaces into 1 22 | 0x08,0x0B,0x00,0x02,0x02,0x02,0x01,0x04, 23 | // Interface 1 (CDC) descriptor 24 | 0x09,0x04,0x00,0x00,0x01,0x02,0x02,0x01,0x04, // CDC control description, 1 endpoint 25 | // Functional Descriptor refer to usbcdc11.pdf 26 | 0x05,0x24,0x00,0x10,0x01, //Header Functional Descriptor 27 | 0x05,0x24,0x01,0x00,0x00, //Call Management Functional Descriptor 28 | 0x04,0x24,0x02,0x02, //Direct Line Management Functional Descriptor, Support: Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, Serial_State 29 | 0x05,0x24,0x06,0x00,0x01, //Union Functional Descriptor, Communication class interface 0, Data Class Interface 1 30 | 0x07,0x05,0x81,0x03,0x08,0x00,0x40, //EndPoint descriptor (CDC Upload, Interrupt) 31 | // Interface 2 (Data Interface) descriptor 32 | 0x09,0x04,0x01,0x00,0x02,0x0a,0x00,0x00,0x04, //Data Class Interface descriptor 33 | 0x07,0x05,0x02,0x02,0x40,0x00,0x00, //endpoint descriptor 34 | 0x07,0x05,0x82,0x02,0x40,0x00,0x00, //endpoint descriptor 35 | 36 | }; 37 | 38 | __code uint16_t CfgDescLen = sizeof(CfgDesc); 39 | 40 | //String Descriptors 41 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor 42 | __code uint16_t LangDesLen = sizeof(LangDes); 43 | __code uint8_t SerDes[]={ //Serial String Descriptor 44 | 0x0C,0x03, 45 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00 46 | }; 47 | __code uint16_t SerDesLen = sizeof(SerDes); 48 | __code uint8_t Prod_Des[]={ //Produce String Descriptor 49 | 0x16,0x03, 50 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,'d',0x00, 51 | 'u',0x00,'i',0x00,'n',0x00,'o',0x00 52 | }; 53 | __code uint16_t Prod_DesLen = sizeof(Prod_Des); 54 | 55 | __code uint8_t CDC_Des[]={ 56 | 0x16,0x03, 57 | 'C',0x00,'D',0x00,'C',0x00,' ',0x00,'S',0x00,'e',0x00, 58 | 'r',0x00,'i',0x00,'a',0x00,'l',0x00 59 | }; 60 | __code uint16_t CDC_DesLen = sizeof(CDC_Des); 61 | 62 | __code uint8_t Manuf_Des[]={ 63 | 0x0E,0x03, 64 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00, 65 | }; 66 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des); 67 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/src/userUsbCdc/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONST_DATA_H__ 2 | #define __CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | #define EP2_ADDR 20 11 | 12 | #define SET_LINE_CODING 0X20 // Configures DTE rate, stop-bits, parity, and number-of-character 13 | #define GET_LINE_CODING 0X21 // This request allows the host to find out the currently configured line coding. 14 | #define SET_CONTROL_LINE_STATE 0X22 // This request generates RS-232/V.24 style control signals. 15 | 16 | 17 | extern __code uint8_t DevDesc[]; 18 | extern __code uint8_t CfgDesc[]; 19 | extern __code uint8_t LangDes[]; 20 | extern __code uint8_t SerDes[]; 21 | extern __code uint8_t Prod_Des[]; 22 | extern __code uint8_t CDC_Des[]; 23 | extern __code uint8_t WEBUSB_Des[]; 24 | extern __code uint8_t Manuf_Des[]; 25 | 26 | 27 | 28 | extern __code uint16_t DevDescLen; 29 | extern __code uint16_t CfgDescLen; 30 | extern __code uint16_t LangDesLen; 31 | extern __code uint16_t SerDesLen; 32 | extern __code uint16_t Prod_DesLen; 33 | extern __code uint16_t CDC_DesLen; 34 | extern __code uint16_t Manuf_DesLen; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/src/userUsbCdc/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 10 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 11 | extern __xdata __at (EP2_ADDR) uint8_t Ep2Buffer[]; 12 | 13 | extern uint16_t SetupLen; 14 | extern uint8_t SetupReq,UsbConfig; 15 | extern const __code uint8_t *pDescr; 16 | 17 | 18 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 19 | 20 | // Out 21 | #define EP0_OUT_Callback USB_EP0_OUT 22 | #define EP1_OUT_Callback NOP_Process 23 | #define EP2_OUT_Callback USB_EP2_OUT 24 | #define EP3_OUT_Callback NOP_Process 25 | #define EP4_OUT_Callback NOP_Process 26 | 27 | // SOF 28 | #define EP0_SOF_Callback NOP_Process 29 | #define EP1_SOF_Callback NOP_Process 30 | #define EP2_SOF_Callback NOP_Process 31 | #define EP3_SOF_Callback NOP_Process 32 | #define EP4_SOF_Callback NOP_Process 33 | 34 | // IN 35 | #define EP0_IN_Callback USB_EP0_IN 36 | #define EP1_IN_Callback USB_EP1_IN 37 | #define EP2_IN_Callback USB_EP2_IN 38 | #define EP3_IN_Callback NOP_Process 39 | #define EP4_IN_Callback NOP_Process 40 | 41 | // SETUP 42 | #define EP0_SETUP_Callback USB_EP0_SETUP 43 | #define EP1_SETUP_Callback NOP_Process 44 | #define EP2_SETUP_Callback NOP_Process 45 | #define EP3_SETUP_Callback NOP_Process 46 | #define EP4_SETUP_Callback NOP_Process 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | void USBInterrupt(void); 53 | void USBDeviceCfg(); 54 | void USBDeviceIntCfg(); 55 | void USBDeviceEndPointCfg(); 56 | 57 | #ifdef __cplusplus 58 | } // extern "C" 59 | #endif 60 | 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/CMSIS_DAP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | CMSIS_DAP 3 | 4 | Modified form ljbfly's implementation, working but a lot to improve 5 | RST -> P30 6 | SWCLK -> P31 7 | SWDIO -> P32 8 | 9 | These pins works in OC mode with pull-up resistor (70K). Should be safe to connect 3.3V part. 10 | However, CH552 is designed to use 2 clock of OUTPUT on rising egde in classic OC mode. Double check if this is a problem for your target. 11 | 12 | nerdralph also reported reflections/ringing on some of SWD connection and cause error. Since CH552 doesn't have slew rate control, termination resistors may be required. 13 | nerdralph used 68Ohm series termination resistors to fix his ringing issue with 28AWG Dupont jumper wire. 14 | 15 | created 2020 16 | by Deqing Sun for use with CH55xduino 17 | 18 | This example code is in the public domain. 19 | 20 | */ 21 | 22 | //For windows user, if you ever played with other HID device with the same PID C55D 23 | //You may need to uninstall the previous driver completely 24 | 25 | 26 | #ifndef USER_USB_RAM 27 | #error "This example needs to be compiled with a USER USB setting" 28 | #endif 29 | 30 | #include "src/CMSIS_DAPusb/DAP.h" 31 | 32 | 33 | extern volatile __xdata uint8_t USBByteCountEP1; 34 | 35 | 36 | void setup() { 37 | USBInit(); 38 | } 39 | 40 | void loop() { 41 | uint8_t dapSendLength = 0; 42 | if (USBByteCountEP1) { 43 | dapSendLength = DAP_Thread(); 44 | USBByteCountEP1 = 0; 45 | 46 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_R_RES | UEP_R_RES_ACK; //enable receive 47 | 48 | UEP1_T_LEN = 64; 49 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //enable send 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/SW_DP.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/SW_DP.c -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/USBHID.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "include/ch5xx.h" 4 | #include "include/ch5xx_usb.h" 5 | #include "USBconstant.h" 6 | #include "USBhandler.h" 7 | 8 | extern __xdata uint8_t Ep0Buffer[]; 9 | extern __xdata uint8_t Ep1Buffer[]; 10 | 11 | volatile __xdata uint8_t USBByteCountEP1 = 0; //Bytes of received data on USB endpoint 12 | 13 | void USBInit(){ 14 | USBDeviceCfg(); //Device mode configuration 15 | USBDeviceEndPointCfg(); //Endpoint configuration 16 | USBDeviceIntCfg(); //Interrupt configuration 17 | UEP0_T_LEN = 0; 18 | UEP1_T_LEN = 0; //Pre-use send length must be cleared 19 | UEP2_T_LEN = 0; 20 | } 21 | 22 | 23 | void USB_EP1_IN(){ 24 | UEP1_T_LEN = 0; // No data to send anymore 25 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //Respond NAK by default 26 | 27 | } 28 | 29 | void USB_EP1_OUT(){ 30 | if ( U_TOG_OK ){ // Discard unsynchronized packets 31 | USBByteCountEP1 = USB_RX_LEN; 32 | if (USBByteCountEP1){ 33 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_R_RES | UEP_R_RES_NAK; //Respond NAK after a packet. Let main code change response after handling. 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/USBHID.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HID_H__ 2 | #define __USB_HID_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | void USBInit(void); 13 | 14 | #ifdef __cplusplus 15 | } // extern "C" 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONST_DATA_H__ 2 | #define __CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | #define EP2_ADDR 20 11 | 12 | extern __code uint8_t DevDesc[]; 13 | extern __code uint8_t CfgDesc[]; 14 | extern __code uint8_t LangDes[]; 15 | extern __code uint8_t ReportDesc[]; 16 | extern __code uint8_t SerDes[]; 17 | extern __code uint8_t Prod_Des[]; 18 | extern __code uint8_t Manuf_Des[]; 19 | 20 | extern __code uint16_t DevDescLen; 21 | extern __code uint16_t CfgDescLen; 22 | extern __code uint16_t LangDesLen; 23 | extern __code uint16_t ReportDescLen; 24 | extern __code uint16_t SerDesLen; 25 | extern __code uint16_t Prod_DesLen; 26 | extern __code uint16_t Manuf_DesLen; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern uint16_t SetupLen; 10 | extern uint8_t SetupReq,UsbConfig; 11 | extern const __code uint8_t *pDescr; 12 | 13 | 14 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 15 | 16 | // Out 17 | #define EP0_OUT_Callback USB_EP0_OUT 18 | #define EP1_OUT_Callback USB_EP1_OUT 19 | #define EP2_OUT_Callback NOP_Process 20 | #define EP3_OUT_Callback NOP_Process 21 | #define EP4_OUT_Callback NOP_Process 22 | 23 | // SOF 24 | #define EP0_SOF_Callback NOP_Process 25 | #define EP1_SOF_Callback NOP_Process 26 | #define EP2_SOF_Callback NOP_Process 27 | #define EP3_SOF_Callback NOP_Process 28 | #define EP4_SOF_Callback NOP_Process 29 | 30 | // IN 31 | #define EP0_IN_Callback USB_EP0_IN 32 | #define EP1_IN_Callback USB_EP1_IN 33 | #define EP2_IN_Callback NOP_Process 34 | #define EP3_IN_Callback NOP_Process 35 | #define EP4_IN_Callback NOP_Process 36 | 37 | // SETUP 38 | #define EP0_SETUP_Callback USB_EP0_SETUP 39 | #define EP1_SETUP_Callback NOP_Process 40 | #define EP2_SETUP_Callback NOP_Process 41 | #define EP3_SETUP_Callback NOP_Process 42 | #define EP4_SETUP_Callback NOP_Process 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | void USBInterrupt(void); 49 | void USBDeviceCfg(); 50 | void USBDeviceIntCfg(); 51 | void USBDeviceEndPointCfg(); 52 | 53 | #ifdef __cplusplus 54 | } // extern "C" 55 | #endif 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/HidKeyboard.ino: -------------------------------------------------------------------------------- 1 | /* 2 | HID Keyboard example 3 | 4 | 5 | created 2020 6 | by Deqing Sun for use with CH55xduino 7 | 8 | This example code is in the public domain. 9 | 10 | */ 11 | 12 | //For windows user, if you ever played with other HID device with the same PID C55D 13 | //You may need to uninstall the previous driver completely 14 | 15 | 16 | #ifndef USER_USB_RAM 17 | #error "This example needs to be compiled with a USER USB setting" 18 | #endif 19 | 20 | #include "src/userUsbHidKeyboard/USBHIDKeyboard.h" 21 | 22 | #define BUTTON1_PIN 30 23 | #define BUTTON2_PIN 31 24 | #define BUTTON3_PIN 32 25 | 26 | #define LED_BUILTIN 33 27 | 28 | bool button1PressPrev = false; 29 | bool button2PressPrev = false; 30 | bool button3PressPrev = false; 31 | 32 | 33 | void setup() { 34 | USBInit(); 35 | pinMode(BUTTON1_PIN, INPUT_PULLUP); 36 | pinMode(BUTTON2_PIN, INPUT_PULLUP); 37 | pinMode(BUTTON3_PIN, INPUT_PULLUP); 38 | pinMode(LED_BUILTIN, OUTPUT); 39 | } 40 | 41 | void loop() { 42 | //button 1 is mapped to letter 'a' 43 | bool button1Press = !digitalRead(BUTTON1_PIN); 44 | if (button1PressPrev != button1Press) { 45 | button1PressPrev = button1Press; 46 | if (button1Press) { 47 | Keyboard_press('a'); 48 | } else { 49 | Keyboard_release('a'); 50 | } 51 | } 52 | 53 | //button 2 is mapped to string 'hello' 54 | bool button2Press = !digitalRead(BUTTON2_PIN); 55 | if (button2PressPrev != button2Press) { 56 | button2PressPrev = button2Press; 57 | if (button2Press) { 58 | Keyboard_write('H'); 59 | Keyboard_write('e'); 60 | Keyboard_write('l'); 61 | Keyboard_write('l'); 62 | Keyboard_write('o'); 63 | } 64 | } 65 | 66 | //button 3 is mapped to Capslock 67 | bool button3Press = !digitalRead(BUTTON3_PIN); 68 | if (button3PressPrev != button3Press) { 69 | button3PressPrev = button3Press; 70 | if (button3Press) { 71 | Keyboard_press(KEY_CAPS_LOCK); 72 | delay(100); //a quick capslock press is not recognized on mac 73 | Keyboard_release(KEY_CAPS_LOCK); 74 | } 75 | } 76 | 77 | //map capsLock to LED 78 | //Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana, 79 | if (Keyboard_getLEDStatus() & 2) { 80 | digitalWrite(LED_BUILTIN, HIGH); 81 | } else { 82 | digitalWrite(LED_BUILTIN, LOW); 83 | } 84 | 85 | delay(50); //naive debouncing 86 | 87 | } 88 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBHIDKeyboard.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HID_KBD_H__ 2 | #define __USB_HID_KBD_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | 9 | #define KEY_LEFT_CTRL 0x80 10 | #define KEY_LEFT_SHIFT 0x81 11 | #define KEY_LEFT_ALT 0x82 12 | #define KEY_LEFT_GUI 0x83 13 | #define KEY_RIGHT_CTRL 0x84 14 | #define KEY_RIGHT_SHIFT 0x85 15 | #define KEY_RIGHT_ALT 0x86 16 | #define KEY_RIGHT_GUI 0x87 17 | 18 | #define KEY_UP_ARROW 0xDA 19 | #define KEY_DOWN_ARROW 0xD9 20 | #define KEY_LEFT_ARROW 0xD8 21 | #define KEY_RIGHT_ARROW 0xD7 22 | #define KEY_BACKSPACE 0xB2 23 | #define KEY_TAB 0xB3 24 | #define KEY_RETURN 0xB0 25 | #define KEY_ESC 0xB1 26 | #define KEY_INSERT 0xD1 27 | #define KEY_DELETE 0xD4 28 | #define KEY_PAGE_UP 0xD3 29 | #define KEY_PAGE_DOWN 0xD6 30 | #define KEY_HOME 0xD2 31 | #define KEY_END 0xD5 32 | #define KEY_CAPS_LOCK 0xC1 33 | #define KEY_F1 0xC2 34 | #define KEY_F2 0xC3 35 | #define KEY_F3 0xC4 36 | #define KEY_F4 0xC5 37 | #define KEY_F5 0xC6 38 | #define KEY_F6 0xC7 39 | #define KEY_F7 0xC8 40 | #define KEY_F8 0xC9 41 | #define KEY_F9 0xCA 42 | #define KEY_F10 0xCB 43 | #define KEY_F11 0xCC 44 | #define KEY_F12 0xCD 45 | #define KEY_F13 0xF0 46 | #define KEY_F14 0xF1 47 | #define KEY_F15 0xF2 48 | #define KEY_F16 0xF3 49 | #define KEY_F17 0xF4 50 | #define KEY_F18 0xF5 51 | #define KEY_F19 0xF6 52 | #define KEY_F20 0xF7 53 | #define KEY_F21 0xF8 54 | #define KEY_F22 0xF9 55 | #define KEY_F23 0xFA 56 | #define KEY_F24 0xFB 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | void USBInit(void); 63 | 64 | uint8_t Keyboard_press(uint8_t k); 65 | uint8_t Keyboard_release(uint8_t k); 66 | void Keyboard_releaseAll(void); 67 | 68 | uint8_t Keyboard_write(uint8_t c); 69 | 70 | uint8_t Keyboard_getLEDStatus(); 71 | 72 | #ifdef __cplusplus 73 | } // extern "C" 74 | #endif 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_CONST_DATA_H__ 2 | #define __USB_CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | 11 | extern __code uint8_t DevDesc[]; 12 | extern __code uint8_t CfgDesc[]; 13 | extern __code uint8_t LangDes[]; 14 | extern __code uint8_t ReportDesc[]; 15 | extern __code uint8_t SerDes[]; 16 | extern __code uint8_t Prod_Des[]; 17 | extern __code uint8_t Manuf_Des[]; 18 | 19 | extern __code uint16_t DevDescLen; 20 | extern __code uint16_t CfgDescLen; 21 | extern __code uint16_t LangDesLen; 22 | extern __code uint16_t ReportDescLen; 23 | extern __code uint16_t SerDesLen; 24 | extern __code uint16_t Prod_DesLen; 25 | extern __code uint16_t Manuf_DesLen; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 10 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 11 | 12 | extern uint16_t SetupLen; 13 | extern uint8_t SetupReq,UsbConfig; 14 | extern const __code uint8_t *pDescr; 15 | 16 | 17 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 18 | 19 | // Out 20 | #define EP0_OUT_Callback USB_EP0_OUT 21 | #define EP1_OUT_Callback USB_EP1_OUT 22 | #define EP2_OUT_Callback NOP_Process 23 | #define EP3_OUT_Callback NOP_Process 24 | #define EP4_OUT_Callback NOP_Process 25 | 26 | // SOF 27 | #define EP0_SOF_Callback NOP_Process 28 | #define EP1_SOF_Callback NOP_Process 29 | #define EP2_SOF_Callback NOP_Process 30 | #define EP3_SOF_Callback NOP_Process 31 | #define EP4_SOF_Callback NOP_Process 32 | 33 | // IN 34 | #define EP0_IN_Callback USB_EP0_IN 35 | #define EP1_IN_Callback USB_EP1_IN 36 | #define EP2_IN_Callback NOP_Process 37 | #define EP3_IN_Callback NOP_Process 38 | #define EP4_IN_Callback NOP_Process 39 | 40 | // SETUP 41 | #define EP0_SETUP_Callback USB_EP0_SETUP 42 | #define EP1_SETUP_Callback NOP_Process 43 | #define EP2_SETUP_Callback NOP_Process 44 | #define EP3_SETUP_Callback NOP_Process 45 | #define EP4_SETUP_Callback NOP_Process 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif 50 | 51 | void USBInterrupt(void); 52 | void USBDeviceCfg(); 53 | void USBDeviceIntCfg(); 54 | void USBDeviceEndPointCfg(); 55 | 56 | #ifdef __cplusplus 57 | } // extern "C" 58 | #endif 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboardMouse/HidKeyboardMouse.ino: -------------------------------------------------------------------------------- 1 | /* 2 | HID Keyboard mouse combo example 3 | 4 | 5 | created 2022 6 | by Deqing Sun for use with CH55xduino 7 | 8 | This example code is in the public domain. 9 | 10 | */ 11 | 12 | //For windows user, if you ever played with other HID device with the same PID C55D 13 | //You may need to uninstall the previous driver completely 14 | 15 | 16 | #ifndef USER_USB_RAM 17 | #error "This example needs to be compiled with a USER USB setting" 18 | #endif 19 | 20 | #include "src/userUsbHidKeyboardMouse/USBHIDKeyboardMouse.h" 21 | 22 | #define BUTTON1_PIN 30 23 | #define BUTTON2_PIN 31 24 | #define BUTTON3_PIN 32 25 | 26 | #define LED_BUILTIN 33 27 | 28 | bool button1PressPrev = false; 29 | bool button2PressPrev = false; 30 | bool button3PressPrev = false; 31 | 32 | 33 | void setup() { 34 | USBInit(); 35 | pinMode(BUTTON1_PIN, INPUT_PULLUP); 36 | pinMode(BUTTON2_PIN, INPUT_PULLUP); 37 | pinMode(BUTTON3_PIN, INPUT_PULLUP); 38 | pinMode(LED_BUILTIN, OUTPUT); 39 | } 40 | 41 | void loop() { 42 | //button 1 is mapped to letter 'a' 43 | bool button1Press = !digitalRead(BUTTON1_PIN); 44 | if (button1PressPrev != button1Press) { 45 | button1PressPrev = button1Press; 46 | if (button1Press) { 47 | Keyboard_press('a'); 48 | } else { 49 | Keyboard_release('a'); 50 | } 51 | } 52 | 53 | //button 2 is mapped to left click 54 | bool button2Press = !digitalRead(BUTTON2_PIN); 55 | if (button2PressPrev != button2Press) { 56 | button2PressPrev = button2Press; 57 | if (button2Press) { 58 | Mouse_click(MOUSE_LEFT); 59 | } 60 | } 61 | 62 | //button 3 is mapped to move cursor 63 | bool button3Press = !digitalRead(BUTTON3_PIN); 64 | if (button3PressPrev != button3Press) { 65 | button3PressPrev = button3Press; 66 | if (button3Press) { 67 | Mouse_move(10, 10); 68 | } 69 | } 70 | 71 | delay(50); //naive debouncing 72 | 73 | } 74 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboardMouse/src/userUsbHidKeyboardMouse/USBHIDKeyboardMouse.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HID_KBD_H__ 2 | #define __USB_HID_KBD_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | 9 | #define KEY_LEFT_CTRL 0x80 10 | #define KEY_LEFT_SHIFT 0x81 11 | #define KEY_LEFT_ALT 0x82 12 | #define KEY_LEFT_GUI 0x83 13 | #define KEY_RIGHT_CTRL 0x84 14 | #define KEY_RIGHT_SHIFT 0x85 15 | #define KEY_RIGHT_ALT 0x86 16 | #define KEY_RIGHT_GUI 0x87 17 | 18 | #define KEY_UP_ARROW 0xDA 19 | #define KEY_DOWN_ARROW 0xD9 20 | #define KEY_LEFT_ARROW 0xD8 21 | #define KEY_RIGHT_ARROW 0xD7 22 | #define KEY_BACKSPACE 0xB2 23 | #define KEY_TAB 0xB3 24 | #define KEY_RETURN 0xB0 25 | #define KEY_ESC 0xB1 26 | #define KEY_INSERT 0xD1 27 | #define KEY_DELETE 0xD4 28 | #define KEY_PAGE_UP 0xD3 29 | #define KEY_PAGE_DOWN 0xD6 30 | #define KEY_HOME 0xD2 31 | #define KEY_END 0xD5 32 | #define KEY_CAPS_LOCK 0xC1 33 | #define KEY_F1 0xC2 34 | #define KEY_F2 0xC3 35 | #define KEY_F3 0xC4 36 | #define KEY_F4 0xC5 37 | #define KEY_F5 0xC6 38 | #define KEY_F6 0xC7 39 | #define KEY_F7 0xC8 40 | #define KEY_F8 0xC9 41 | #define KEY_F9 0xCA 42 | #define KEY_F10 0xCB 43 | #define KEY_F11 0xCC 44 | #define KEY_F12 0xCD 45 | #define KEY_F13 0xF0 46 | #define KEY_F14 0xF1 47 | #define KEY_F15 0xF2 48 | #define KEY_F16 0xF3 49 | #define KEY_F17 0xF4 50 | #define KEY_F18 0xF5 51 | #define KEY_F19 0xF6 52 | #define KEY_F20 0xF7 53 | #define KEY_F21 0xF8 54 | #define KEY_F22 0xF9 55 | #define KEY_F23 0xFA 56 | #define KEY_F24 0xFB 57 | 58 | enum MOUSE_BUTTON { 59 | MOUSE_LEFT = 1, 60 | MOUSE_RIGHT = 2, 61 | MOUSE_MIDDLE = 4, 62 | }; 63 | 64 | #ifdef __cplusplus 65 | extern "C" { 66 | #endif 67 | 68 | void USBInit(void); 69 | 70 | uint8_t Keyboard_press(uint8_t k); 71 | uint8_t Keyboard_release(uint8_t k); 72 | void Keyboard_releaseAll(void); 73 | 74 | uint8_t Keyboard_write(uint8_t c); 75 | 76 | uint8_t Keyboard_getLEDStatus(); 77 | 78 | uint8_t Mouse_press(uint8_t k); 79 | uint8_t Mouse_release(uint8_t k); 80 | uint8_t Mouse_click(uint8_t k); 81 | uint8_t Mouse_move(int8_t x, int8_t y); 82 | uint8_t Mouse_scroll(int8_t tilt); 83 | 84 | #ifdef __cplusplus 85 | } // extern "C" 86 | #endif 87 | 88 | #endif 89 | 90 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboardMouse/src/userUsbHidKeyboardMouse/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_CONST_DATA_H__ 2 | #define __USB_CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | 11 | extern __code uint8_t DevDesc[]; 12 | extern __code uint8_t CfgDesc[]; 13 | extern __code uint8_t LangDes[]; 14 | extern __code uint8_t ReportDesc[]; 15 | extern __code uint8_t SerDes[]; 16 | extern __code uint8_t Prod_Des[]; 17 | extern __code uint8_t Manuf_Des[]; 18 | 19 | extern __code uint16_t DevDescLen; 20 | extern __code uint16_t CfgDescLen; 21 | extern __code uint16_t LangDesLen; 22 | extern __code uint16_t ReportDescLen; 23 | extern __code uint16_t SerDesLen; 24 | extern __code uint16_t Prod_DesLen; 25 | extern __code uint16_t Manuf_DesLen; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboardMouse/src/userUsbHidKeyboardMouse/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 10 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 11 | 12 | extern uint16_t SetupLen; 13 | extern uint8_t SetupReq,UsbConfig; 14 | extern const __code uint8_t *pDescr; 15 | 16 | 17 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 18 | 19 | // Out 20 | #define EP0_OUT_Callback USB_EP0_OUT 21 | #define EP1_OUT_Callback USB_EP1_OUT 22 | #define EP2_OUT_Callback NOP_Process 23 | #define EP3_OUT_Callback NOP_Process 24 | #define EP4_OUT_Callback NOP_Process 25 | 26 | // SOF 27 | #define EP0_SOF_Callback NOP_Process 28 | #define EP1_SOF_Callback NOP_Process 29 | #define EP2_SOF_Callback NOP_Process 30 | #define EP3_SOF_Callback NOP_Process 31 | #define EP4_SOF_Callback NOP_Process 32 | 33 | // IN 34 | #define EP0_IN_Callback USB_EP0_IN 35 | #define EP1_IN_Callback USB_EP1_IN 36 | #define EP2_IN_Callback NOP_Process 37 | #define EP3_IN_Callback NOP_Process 38 | #define EP4_IN_Callback NOP_Process 39 | 40 | // SETUP 41 | #define EP0_SETUP_Callback USB_EP0_SETUP 42 | #define EP1_SETUP_Callback NOP_Process 43 | #define EP2_SETUP_Callback NOP_Process 44 | #define EP3_SETUP_Callback NOP_Process 45 | #define EP4_SETUP_Callback NOP_Process 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif 50 | 51 | void USBInterrupt(void); 52 | void USBDeviceCfg(); 53 | void USBDeviceIntCfg(); 54 | void USBDeviceEndPointCfg(); 55 | 56 | #ifdef __cplusplus 57 | } // extern "C" 58 | #endif 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMediaKeyboard/HidMediaKeyboard.ino: -------------------------------------------------------------------------------- 1 | /* 2 | HID Keyboard example 3 | 4 | 5 | created 2020 6 | by Deqing Sun for use with CH55xduino 7 | 8 | This example code is in the public domain. 9 | 10 | */ 11 | 12 | //For windows user, if you ever played with other HID device with the same PID C55D 13 | //You may need to uninstall the previous driver completely 14 | 15 | 16 | #ifndef USER_USB_RAM 17 | #error "This example needs to be compiled with a USER USB setting" 18 | #endif 19 | 20 | #include "src/userUsbHidMediaKeyboard/USBHIDMediaKeyboard.h" 21 | 22 | #define BUTTON1_PIN 11 23 | 24 | #define LED_BUILTIN 33 25 | 26 | bool button1PressPrev = false; 27 | 28 | 29 | void setup() { 30 | USBInit(); 31 | pinMode(BUTTON1_PIN, INPUT_PULLUP); 32 | pinMode(LED_BUILTIN, OUTPUT); 33 | } 34 | 35 | void loop() { 36 | //button 1 is mapped to MEDIA_VOL_UP, refer to USBHIDMediaKeyboard.h for more media keys 37 | bool button1Press = !digitalRead(BUTTON1_PIN); 38 | if (button1PressPrev != button1Press) { 39 | button1PressPrev = button1Press; 40 | if (button1Press) { 41 | Consumer_press(MEDIA_VOL_UP); 42 | } else { 43 | Consumer_release(MEDIA_VOL_UP); 44 | } 45 | } 46 | 47 | delay(50); //naive debouncing 48 | } 49 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMediaKeyboard/src/userUsbHidMediaKeyboard/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_CONST_DATA_H__ 2 | #define __USB_CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | 11 | extern __code uint8_t DevDesc[]; 12 | extern __code uint8_t CfgDesc[]; 13 | extern __code uint8_t LangDes[]; 14 | extern __code uint8_t ReportDesc[]; 15 | extern __code uint8_t SerDes[]; 16 | extern __code uint8_t Prod_Des[]; 17 | extern __code uint8_t Manuf_Des[]; 18 | 19 | extern __code uint16_t DevDescLen; 20 | extern __code uint16_t CfgDescLen; 21 | extern __code uint16_t LangDesLen; 22 | extern __code uint16_t ReportDescLen; 23 | extern __code uint16_t SerDesLen; 24 | extern __code uint16_t Prod_DesLen; 25 | extern __code uint16_t Manuf_DesLen; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMediaKeyboard/src/userUsbHidMediaKeyboard/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 10 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 11 | 12 | extern uint16_t SetupLen; 13 | extern uint8_t SetupReq,UsbConfig; 14 | extern const __code uint8_t *pDescr; 15 | 16 | 17 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 18 | 19 | // Out 20 | #define EP0_OUT_Callback USB_EP0_OUT 21 | #define EP1_OUT_Callback USB_EP1_OUT 22 | #define EP2_OUT_Callback NOP_Process 23 | #define EP3_OUT_Callback NOP_Process 24 | #define EP4_OUT_Callback NOP_Process 25 | 26 | // SOF 27 | #define EP0_SOF_Callback NOP_Process 28 | #define EP1_SOF_Callback NOP_Process 29 | #define EP2_SOF_Callback NOP_Process 30 | #define EP3_SOF_Callback NOP_Process 31 | #define EP4_SOF_Callback NOP_Process 32 | 33 | // IN 34 | #define EP0_IN_Callback USB_EP0_IN 35 | #define EP1_IN_Callback USB_EP1_IN 36 | #define EP2_IN_Callback NOP_Process 37 | #define EP3_IN_Callback NOP_Process 38 | #define EP4_IN_Callback NOP_Process 39 | 40 | // SETUP 41 | #define EP0_SETUP_Callback USB_EP0_SETUP 42 | #define EP1_SETUP_Callback NOP_Process 43 | #define EP2_SETUP_Callback NOP_Process 44 | #define EP3_SETUP_Callback NOP_Process 45 | #define EP4_SETUP_Callback NOP_Process 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif 50 | 51 | void USBInterrupt(void); 52 | void USBDeviceCfg(); 53 | void USBDeviceIntCfg(); 54 | void USBDeviceEndPointCfg(); 55 | 56 | #ifdef __cplusplus 57 | } // extern "C" 58 | #endif 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/MassStorage/src/userMassStorage/USBMediumAccess.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_MAL_H 2 | #define __USB_MAL_H 3 | 4 | #include 5 | 6 | #define MAL_MAX_LUN 0 7 | 8 | #define MAL_OK 0 9 | #define MAL_FAIL 1 10 | 11 | extern __code const uint8_t emuDisk_Inquiry_Data[]; 12 | 13 | //make sure they match 14 | extern __code const uint8_t diskCapacity[8]; 15 | extern __code const uint8_t formatCapacity[8]; 16 | #define MASS_BLOCK_COUNT 0x4000 17 | #define MASS_BLOCK_SIZE 512 18 | 19 | typedef void( *pFileCBFn)( uint16_t ); 20 | 21 | union FileCBPtrUnion { 22 | pFileCBFn funcPtr; 23 | __code uint8_t *constPtr; 24 | }; 25 | enum FileCBPtrType{CONST_DATA_FILE,FUNCTION_CALLBACK_FILE}; 26 | 27 | typedef struct _File_Entry { 28 | uint8_t filename[11]; 29 | uint8_t filetime[2]; 30 | uint8_t filedate[2]; 31 | uint16_t filesize; 32 | uint8_t fileCallBackType; 33 | union FileCBPtrUnion fileReadHandler; 34 | pFileCBFn fileWriteHandler; 35 | }File_Entry; 36 | 37 | //Time format (16Bits): 38 | // Bits15~11 Hour, ranging from 0~23 39 | // Bits10~5 Minute, ranging from 0~59 40 | // Bits4~0 Second, ranging from 0~29. Each unit is 2 seconds. 41 | 42 | //Date format (16Bits): 43 | // Bits15~9 Year, ranging from 0~127. Represent difference from 1980. 44 | // Bits8~5 Month, ranging from 1~12 45 | // Bits4~0 Day, ranging from 1~31 46 | 47 | //Get high byte of time format in 16bits format 48 | #define TIME_HB(H,M,S) (((((H)<<3))|((M)>>3))) 49 | //Get low byte of time format in 16bits format 50 | #define TIME_LB(H,M,S) (((0))|((M)<<5)|(S)) 51 | 52 | //Get high byte of date format in 16bits format 53 | #define DATE_HB(Y,M,D) (((((Y)-1980)<<1)|((M)>>3))) 54 | //Get low byte of date format in 16bits format 55 | #define DATE_LB(Y,M,D) ((0)|((M)<<5)|(D)) 56 | 57 | uint8_t LUN_GetStatus(); 58 | void LUN_Eject (); 59 | // Read BULK_MAX_PACKET_SIZE bytes of data from device to BOT_Tx_Buf 60 | void LUN_Read (uint32_t curAddr); 61 | // Write BULK_MAX_PACKET_SIZE bytes of data from BOT_Rx_Buf to device 62 | void LUN_Write (uint32_t curAddr); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/MassStorage/src/userMassStorage/USBSCSIData.c: -------------------------------------------------------------------------------- 1 | #include "USBSCSI.h" 2 | 3 | __code const uint8_t Page00_Inquiry_Data[] = 4 | { 5 | 0x00, /* PERIPHERAL QUALIFIER & PERIPHERAL DEVICE TYPE*/ 6 | 0x00, 7 | 0x00, 8 | 0x00, 9 | 0x00 /* Supported Pages 00*/ 10 | }; 11 | 12 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 13 | __code const uint8_t Mode_Sense6_data[] = 14 | { 15 | 0x03, 16 | 0x00, 17 | 0x00, //refer to SBC-3, Table 97 - DEVICE-SPECIFIC PARAMETER field for direct-access block devices, 0x80 is WP (Whole disk is read only). 0x00 is normal 18 | 0x00, 19 | }; 20 | 21 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 22 | 23 | __code const uint8_t Mode_Sense10_data[] = 24 | { 25 | 0x00, 26 | 0x06, 27 | 0x00, 28 | 0x00, 29 | 0x00, 30 | 0x00, 31 | 0x00, 32 | 0x00 33 | }; 34 | 35 | __code const uint8_t Scsi_Sense_Data[] = 36 | { 37 | 0x70, /*RespCode*/ 38 | 0x00, /*SegmentNumber*/ 39 | NO_SENSE, /* Sens_Key*/ 40 | 0x00, 41 | 0x00, 42 | 0x00, 43 | 0x00, /*Information*/ 44 | 0x0A, /*AdditionalSenseLength*/ 45 | 0x00, 46 | 0x00, 47 | 0x00, 48 | 0x00, /*CmdInformation*/ 49 | NO_SENSE, /*Asc*/ 50 | 0x00, /*ASCQ*/ 51 | 0x00, /*FRUC*/ 52 | 0x00, /*TBD*/ 53 | 0x00, 54 | 0x00 /*SenseKeySpecific*/ 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/MassStorage/src/userMassStorage/USBconstant.c: -------------------------------------------------------------------------------- 1 | #include "USBconstant.h" 2 | 3 | 4 | //Device descriptor 5 | __code uint8_t DevDesc[] = { 6 | 0x12,0x01, 7 | 0x10,0x01, //USB spec release number in BCD format, USB1.1 (0x10, 0x01). 8 | 0x00,0x00,0x00, //bDeviceClass, bDeviceSubClass, bDeviceProtocol 9 | DEFAULT_ENDP0_SIZE, //bNumConfigurations 10 | 0x09,0x12,0x5E,0xC5, // VID PID 11 | 0x01,0x01, //version 12 | 0x01,0x02,0x03, //bString 13 | 0x01 //bNumConfigurations 14 | }; 15 | 16 | __code uint16_t DevDescLen = sizeof(DevDesc); 17 | 18 | __code uint8_t CfgDesc[] ={ 19 | // Configuration Descriptor 20 | 9, // Length of the descriptor 21 | 0x02, // Type: Configuration Descriptor 22 | // Total length of this and following descriptors 23 | sizeof(CfgDesc) & 0xff,sizeof(CfgDesc) >> 8, 24 | 1, // Number of interfaces 25 | 0x01, // Value used to select this configuration 26 | 0x00, // Index of corresponding string descriptor 27 | 0x80, // Attributes, D7 must be 1, D6 Self-powered, D5 Remote Wakeup, D4-D0=0 28 | 0x32, // Max current drawn by device, in units of 2mA 29 | 30 | // Descriptor of Mass Storage interface 31 | 9, // bLength: Interface Descriptor size 32 | 0x04, // bDescriptorType: 33 | // Interface descriptor type 34 | 0x00, // bInterfaceNumber: Number of Interface 35 | 0x00, // bAlternateSetting: Alternate setting 36 | 0x02, // bNumEndpoints 37 | 0x08, // bInterfaceClass: MASS STORAGE Class 38 | 0x06, // bInterfaceSubClass : SCSI transparent 39 | 0x50, // nInterfaceProtocol 40 | 0, // iInterface: 41 | // 18 42 | 7, //Endpoint descriptor length = 7 43 | 0x05, //Endpoint descriptor type 44 | 0x81, //Endpoint address (IN, address 1) 45 | 0x02, //Bulk endpoint type 46 | 0x40, //Maximum packet size (64 bytes) 47 | 0x00, 48 | 0x00, //Polling interval in milliseconds 49 | // 25 50 | 0x07, //Endpoint descriptor length = 7 51 | 0x05, //Endpoint descriptor type 52 | 0x01, //Endpoint address (OUT, address 2) 53 | 0x02, //Bulk endpoint type 54 | 0x40, //Maximum packet size (64 bytes) 55 | 0x00, 56 | 0x00 //Polling interval in milliseconds 57 | 58 | }; 59 | 60 | __code uint16_t CfgDescLen = sizeof(CfgDesc); 61 | 62 | //String Descriptors 63 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor 64 | __code uint16_t LangDesLen = sizeof(LangDes); 65 | __code uint8_t SerDes[]={ //Serial String Descriptor 66 | 0x0C,0x03, 67 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00 68 | }; 69 | __code uint16_t SerDesLen = sizeof(SerDes); 70 | __code uint8_t Prod_Des[]={ //Produce String Descriptor 71 | 0x1E,0x03, 72 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,'d',0x00, 73 | 'u',0x00,'i',0x00,'n',0x00,'o',0x00, 74 | ' ',0x00,'M',0x00,'S',0x00,'D',0x00 75 | }; 76 | __code uint16_t Prod_DesLen = sizeof(Prod_Des); 77 | 78 | __code uint8_t Manuf_Des[]={ 79 | 0x0E,0x03, 80 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00, 81 | }; 82 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des); 83 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/MassStorage/src/userMassStorage/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONST_DATA_H__ 2 | #define __CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | 11 | 12 | 13 | extern __code uint8_t DevDesc[]; 14 | extern __code uint8_t CfgDesc[]; 15 | extern __code uint8_t LangDes[]; 16 | extern __code uint8_t SerDes[]; 17 | extern __code uint8_t Prod_Des[]; 18 | extern __code uint8_t CDC_Des[]; 19 | extern __code uint8_t WEBUSB_Des[]; 20 | extern __code uint8_t Manuf_Des[]; 21 | 22 | 23 | 24 | extern __code uint16_t DevDescLen; 25 | extern __code uint16_t CfgDescLen; 26 | extern __code uint16_t LangDesLen; 27 | extern __code uint16_t SerDesLen; 28 | extern __code uint16_t Prod_DesLen; 29 | extern __code uint16_t CDC_DesLen; 30 | extern __code uint16_t Manuf_DesLen; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/MassStorage/src/userMassStorage/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern uint16_t SetupLen; 10 | extern uint8_t SetupReq,UsbConfig; 11 | extern const __code uint8_t *pDescr; 12 | 13 | void Mass_Storage_In (void); 14 | void Mass_Storage_Out (void); 15 | 16 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 17 | 18 | // Out 19 | #define EP0_OUT_Callback USB_EP0_OUT 20 | #define EP1_OUT_Callback Mass_Storage_Out 21 | #define EP2_OUT_Callback NOP_Process 22 | #define EP3_OUT_Callback NOP_Process 23 | #define EP4_OUT_Callback NOP_Process 24 | 25 | // SOF 26 | #define EP0_SOF_Callback NOP_Process 27 | #define EP1_SOF_Callback NOP_Process 28 | #define EP2_SOF_Callback NOP_Process 29 | #define EP3_SOF_Callback NOP_Process 30 | #define EP4_SOF_Callback NOP_Process 31 | 32 | // IN 33 | #define EP0_IN_Callback USB_EP0_IN 34 | #define EP1_IN_Callback Mass_Storage_In 35 | #define EP2_IN_Callback NOP_Process 36 | #define EP3_IN_Callback NOP_Process 37 | #define EP4_IN_Callback NOP_Process 38 | 39 | // SETUP 40 | #define EP0_SETUP_Callback USB_EP0_SETUP 41 | #define EP1_SETUP_Callback NOP_Process 42 | #define EP2_SETUP_Callback NOP_Process 43 | #define EP3_SETUP_Callback NOP_Process 44 | #define EP4_SETUP_Callback NOP_Process 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | void USBInterrupt(void); 51 | void USBDeviceCfg(); 52 | void USBDeviceIntCfg(); 53 | void USBDeviceEndPointCfg(); 54 | 55 | #ifdef __cplusplus 56 | } // extern "C" 57 | #endif 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/NoUsbExample/NoUsbExample.ino: -------------------------------------------------------------------------------- 1 | #ifndef USER_USB_RAM 2 | #error "This example needs to be compiled with a USER USB setting" 3 | #endif 4 | 5 | void USBInterrupt(void){ 6 | } 7 | 8 | void setup() { 9 | // put your setup code here, to run once: 10 | 11 | } 12 | 13 | void loop() { 14 | // put your main code here, to run repeatedly: 15 | 16 | } 17 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/Generic_Examples/examples/06.EEPROM/EEPROM_cycle_write/EEPROM_cycle_write.ino: -------------------------------------------------------------------------------- 1 | /* 2 | EEPROM_cycle_write 3 | 4 | A simple example write eeprom content one by one. 5 | It will continue after power cycle. 6 | 7 | created 2020 8 | by Deqing Sun for use with CH55xduino 9 | 10 | This example code is in the public domain. 11 | 12 | */ 13 | 14 | uint8_t writePtr = 0; 15 | uint8_t writeData = 0; 16 | 17 | void dumpEEPROM() { 18 | USBSerial_println("DataFalsh Dump:"); 19 | for (uint8_t i = 0; i < 128; i++) { 20 | uint8_t eepromData = eeprom_read_byte(i); 21 | if (eepromData < 0x10) USBSerial_print((char)'0'); 22 | USBSerial_print(eepromData, HEX); 23 | USBSerial_print((char)','); 24 | if ((i & 15) == 15) USBSerial_println(); 25 | } 26 | USBSerial_flush(); 27 | } 28 | 29 | void setup() { 30 | for (writePtr = 0; writePtr < 128; writePtr++) { 31 | uint8_t eepromData = eeprom_read_byte(writePtr); 32 | if (writePtr != eepromData) break; 33 | } 34 | writeData = writePtr; 35 | if (writePtr >= 128) writePtr = 0; 36 | } 37 | 38 | void loop() { 39 | delay(5000); 40 | USBSerial_print("Write "); 41 | USBSerial_print(writeData, HEX); 42 | USBSerial_print(" to addr: "); 43 | USBSerial_println(writePtr, HEX); 44 | eeprom_write_byte(writePtr, writeData); 45 | writeData++; 46 | writePtr++; 47 | if (writePtr >= 128) writePtr = 0; 48 | dumpEEPROM(); 49 | } 50 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SPI/examples/ADXL345SPI/ADXL345SPI.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //On CH552: SCK->P1.7, MISO->P1.6 MOSI->P1.5 4 | 5 | #define CS_PIN 14 6 | 7 | void setup() { 8 | digitalWrite(CS_PIN, HIGH); 9 | pinMode(CS_PIN, OUTPUT); 10 | 11 | SPI_begin(); 12 | SPI_beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3)); 13 | 14 | while (!USBSerial()) { 15 | ; // wait for serial port to connect. Needed for native USB port only 16 | } 17 | 18 | USBSerial_println("Access ADXL345 with SPI."); 19 | 20 | digitalWrite(CS_PIN, LOW); 21 | SPI_transfer(0x00 | 0x80); //read DEVID 0x00 22 | uint8_t ret = SPI_transfer(0xFF); 23 | digitalWrite(CS_PIN, HIGH); 24 | 25 | if (ret == 0xE5) { 26 | USBSerial_println("DEVID OK"); 27 | } else { 28 | USBSerial_println("DEVID NOT OK"); 29 | while (1); 30 | } 31 | 32 | digitalWrite(CS_PIN, LOW); 33 | SPI_transfer(0x2D); //write 0x2D 34 | SPI_transfer(0); // Wakeup 35 | digitalWrite(CS_PIN, HIGH); 36 | 37 | digitalWrite(CS_PIN, LOW); 38 | SPI_transfer(0x2D); //write 0x2D 39 | SPI_transfer(0x10); //Auto_Sleep 40 | digitalWrite(CS_PIN, HIGH); 41 | 42 | digitalWrite(CS_PIN, LOW); 43 | SPI_transfer(0x2D); //write 0x2D 44 | SPI_transfer(0x08); // Measure 45 | digitalWrite(CS_PIN, HIGH); 46 | } 47 | 48 | void loop() { 49 | // put your main code here, to run repeatedly: 50 | delay(100); 51 | 52 | int16_t x = 0, y = 0, z = 0; 53 | uint8_t ret1, ret2; 54 | 55 | digitalWrite(CS_PIN, LOW); 56 | USBSerial_println("send:"); 57 | 58 | SPI_transfer(0x32 | 0x80 | 0x40); //read DATAX0 0x32, multiple bytes 59 | ret1 = SPI_transfer(0xFF); 60 | ret2 = SPI_transfer(0xFF); 61 | x = (int16_t)((((int)ret2) << 8) | ret1); 62 | ret1 = SPI_transfer(0xFF); 63 | ret2 = SPI_transfer(0xFF); 64 | y = (int16_t)((((int)ret2) << 8) | ret1); 65 | ret1 = SPI_transfer(0xFF); 66 | ret2 = SPI_transfer(0xFF); 67 | z = (int16_t)((((int)ret2) << 8) | ret1); 68 | digitalWrite(CS_PIN, HIGH); 69 | 70 | USBSerial_print("ACCEL: "); 71 | USBSerial_print(x); 72 | USBSerial_print(", "); 73 | USBSerial_print(y); 74 | USBSerial_print(", "); 75 | USBSerial_println(z); 76 | } 77 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SPI/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For SPI 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | 14 | ####################################### 15 | # Instances (KEYWORD2) 16 | ####################################### 17 | 18 | 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | 25 | SPI_MODE0 LITERAL1 26 | SPI_MODE3 LITERAL1 -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SPI/library.properties: -------------------------------------------------------------------------------- 1 | name=SPI 2 | version=1.0 3 | author=Deqing Sun 4 | maintainer=Deqing Sun 5 | sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. 6 | paragraph=SPI is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It uses three lines common to all devices (MISO, MOSI and SCK) and one specific for each device. 7 | category=Communication 8 | url=https://github.com/DeqingSun/ch55xduino 9 | architectures=mcs51 10 | 11 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SPI/src/SPI.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * Copyright (c) 2014 by Paul Stoffregen (Transaction API) 4 | * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) 5 | * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) 6 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 7 | * SPI Master library for arduino. 8 | * 9 | * This file is free software; you can redistribute it and/or modify 10 | * it under the terms of either the GNU General Public License version 2 11 | * or the GNU Lesser General Public License version 2.1, both as 12 | * published by the Free Software Foundation. 13 | */ 14 | 15 | 16 | #include "SPI.h" 17 | 18 | void SPI_begin(void){ 19 | SPI0_SETUP = 0; 20 | SPI0_CK_SE = 0x20; 21 | SPI0_CTRL = bS0_MOSI_OE | bS0_SCK_OE; 22 | } 23 | 24 | void SPI_beginTransaction(uint16_t settings){ 25 | SPI0_CK_SE = settings&0xFF; 26 | if (settings & (1<<8)){ 27 | SPI0_SETUP &= ~bS0_BIT_ORDER; 28 | }else{ 29 | SPI0_SETUP |= bS0_BIT_ORDER; 30 | } 31 | if (settings & (1<<9)){ 32 | SPI0_CTRL |= bS0_MST_CLK; 33 | }else{ 34 | SPI0_CTRL &= ~bS0_MST_CLK; 35 | } 36 | } 37 | 38 | inline uint8_t SPI_transfer(uint8_t data) 39 | { 40 | SPI0_DATA = data; 41 | while (S0_FREE == 0); 42 | return SPI0_DATA; 43 | } 44 | 45 | 46 | void SPI_end(void) { 47 | SPI0_CTRL &= ~(bS0_MISO_OE | bS0_MOSI_OE | bS0_SCK_OE); 48 | } -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SPI/src/SPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * Copyright (c) 2014 by Paul Stoffregen (Transaction API) 4 | * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) 5 | * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) 6 | * Copyright (c) 2016 by Michael Mayer (c version for STM8 port) 7 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 8 | * SPI Master library for arduino. 9 | * 10 | * This file is free software; you can redistribute it and/or modify 11 | * it under the terms of either the GNU General Public License version 2 12 | * or the GNU Lesser General Public License version 2.1, both as 13 | * published by the Free Software Foundation. 14 | */ 15 | 16 | #ifndef _SPI_H_INCLUDED 17 | #define _SPI_H_INCLUDED 18 | 19 | //#include 20 | #include 21 | 22 | // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(), 23 | // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) 24 | #define SPI_HAS_TRANSACTION 1 25 | 26 | // SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method 27 | #define SPI_HAS_NOTUSINGINTERRUPT 1 28 | 29 | // SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version. 30 | // This way when there is a bug fix you can check this define to alert users 31 | // of your code if it uses better version of this library. 32 | // This also implies everything that SPI_HAS_TRANSACTION as documented above is 33 | // available too. 34 | #define SPI_ATOMIC_VERSION 1 35 | 36 | // Uncomment this line to add detection of mismatched begin/end transactions. 37 | // A mismatch occurs if other libraries fail to use SPI.endTransaction() for 38 | // each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn 39 | // on if any mismatch is ever detected. 40 | //#define SPI_TRANSACTION_MISMATCH_LED 5 41 | 42 | #ifndef LSBFIRST 43 | #define LSBFIRST 0 44 | #endif 45 | #ifndef MSBFIRST 46 | #define MSBFIRST 1 47 | #endif 48 | 49 | #define SPI_MODE0 0x00 50 | //#define SPI_MODE1 0x01 51 | //#define SPI_MODE2 0x02 52 | #define SPI_MODE3 0x03 53 | 54 | // instead of a SPISettings class object just use a 16 bit integer value. 55 | // bit 0..7: clock divider 56 | // bit 8: bitOrder 57 | // bit 9: dataMode 58 | 59 | #define SPISettings(C,O,M) ( ((F_CPU/(C))&0xFF) | ((O)?1<<8:0) | ((M)?1<<9:0) ) 60 | 61 | 62 | void SPI_begin(void); 63 | 64 | void SPI_beginTransaction(uint16_t settings); 65 | 66 | inline uint8_t SPI_transfer(uint8_t data); 67 | 68 | void SPI_end(void); 69 | 70 | #endif -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/examples/dump24c32/dump24c32.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | scl_pin = 30; //extern variable in SoftI2C.h 5 | sda_pin = 31; 6 | I2CInit(); 7 | } 8 | 9 | void loop() { 10 | 11 | uint8_t ack_bit, readData1, readData2; 12 | 13 | USBSerial_println("\nDump first 8 bytes of 24C32 on 0x50 addr:"); 14 | USBSerial_flush(); 15 | I2CStart(); 16 | ack_bit = I2CSend(0x50 << 1 | 1); //last bit is r(1)/w(0). 17 | I2CStop(); 18 | 19 | if (ack_bit == 0) { 20 | for (uint8_t i = 0; i < 8; i += 2) { 21 | I2CStart(); 22 | ack_bit = I2CSend(0x50 << 1 | 0); //last bit is r(1)/w(0). 23 | if (ack_bit != 0) break; 24 | ack_bit = I2CSend(0); //high EEPROM addr 25 | if (ack_bit != 0) break; 26 | ack_bit = I2CSend(i); //low EEPROM addr 27 | if (ack_bit != 0) break; 28 | I2CRestart(); 29 | ack_bit = I2CSend(0x50 << 1 | 1); //last bit is r(1)/w(0). 30 | if (ack_bit != 0) break; 31 | readData1 = I2CRead(); 32 | I2CAck(); 33 | readData2 = I2CRead(); 34 | I2CNak(); 35 | I2CStop(); 36 | USBSerial_print("Get 2 bytes start from addr "); 37 | USBSerial_flush(); 38 | USBSerial_print(i, HEX); 39 | USBSerial_print(": "); 40 | USBSerial_print(readData1, HEX); 41 | USBSerial_print(", "); 42 | USBSerial_println(readData2, HEX); 43 | USBSerial_flush(); 44 | } 45 | 46 | } else { 47 | USBSerial_println("No response from 0x50"); 48 | USBSerial_flush(); 49 | } 50 | 51 | delay(1000); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/examples/i2cScanner/i2cScanner.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | scl_pin = 30; //extern variable in SoftI2C.h 5 | sda_pin = 31; 6 | I2CInit(); 7 | } 8 | 9 | void loop() { 10 | 11 | uint8_t ack_bit; 12 | 13 | USBSerial_println("\nScanning:"); 14 | 15 | for (uint8_t i = 0; i < 128; i++) { 16 | I2CStart(); 17 | ack_bit = I2CSend(i << 1 | 1); //last bit is r(1)/w(0). 18 | I2CStop(); 19 | delay(1); 20 | if (ack_bit == 0) { 21 | USBSerial_print("I2C got ACK from: 0x"); 22 | USBSerial_println(i, HEX); 23 | } 24 | } 25 | delay(1000); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For SoftI2C 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | 14 | ####################################### 15 | # Instances (KEYWORD2) 16 | ####################################### 17 | 18 | 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/library.properties: -------------------------------------------------------------------------------- 1 | name=SoftI2C 2 | version=1.0 3 | author=Deqing Sun 4 | maintainer=Deqing Sun 5 | sentence=Enables the communication with devices that use the Inter-Integrated Circuit (I2C) Bus. 6 | paragraph=The Inter-Integrated Circuit (I2C) Protocol is a protocol intended to allow multiple "peripheral" digital integrated circuits ("chips") to communicate with one or more "controller" chips. It only requires two signal wires to exchange information. 7 | category=Communication 8 | url=https://github.com/DeqingSun/ch55xduino 9 | architectures=mcs51 10 | 11 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/src/SoftI2C.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 3 | * Soft I2C library for arduino. 4 | * 5 | * This file is free software; you can redistribute it and/or modify 6 | * it under the terms of either the GNU General Public License version 2 7 | * or the GNU Lesser General Public License version 2.1, both as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | 12 | #include "SoftI2C.h" 13 | 14 | __xdata uint8_t scl_pin = 30; 15 | __xdata uint8_t sda_pin = 31; 16 | 17 | void I2CInit(){ 18 | pinMode(scl_pin, OUTPUT_OD); 19 | pinMode(sda_pin, OUTPUT_OD); 20 | 21 | digitalWrite(sda_pin, HIGH); 22 | digitalWrite(scl_pin, HIGH); 23 | } 24 | 25 | void I2CStart(){ 26 | digitalWrite(sda_pin, LOW); 27 | digitalWrite(scl_pin, LOW); 28 | } 29 | 30 | void I2CRestart(){ 31 | digitalWrite(sda_pin, HIGH); 32 | digitalWrite(scl_pin, HIGH); 33 | digitalWrite(sda_pin, LOW); 34 | digitalWrite(scl_pin, LOW); 35 | } 36 | 37 | void I2CStop(){ 38 | digitalWrite(scl_pin, LOW); 39 | digitalWrite(sda_pin, LOW); 40 | digitalWrite(scl_pin, HIGH); 41 | digitalWrite(sda_pin, HIGH); 42 | } 43 | 44 | void I2CAck(){ 45 | digitalWrite(sda_pin, LOW); 46 | digitalWrite(scl_pin, HIGH); 47 | digitalWrite(scl_pin, LOW); 48 | digitalWrite(sda_pin, HIGH); 49 | } 50 | 51 | void I2CNak(){ 52 | digitalWrite(sda_pin, HIGH); 53 | digitalWrite(scl_pin, HIGH); 54 | digitalWrite(scl_pin, LOW); 55 | digitalWrite(sda_pin, HIGH); 56 | } 57 | 58 | uint8_t I2CSend(uint8_t i2cData){ 59 | uint8_t i, ack_bit; 60 | for (i = 0; i < 8; i++) { 61 | if ((i2cData & 0x80) == 0) 62 | digitalWrite(sda_pin, LOW); 63 | else 64 | digitalWrite(sda_pin, HIGH); 65 | digitalWrite(scl_pin, HIGH); 66 | delayMicroseconds(3); 67 | digitalWrite(scl_pin, LOW); 68 | i2cData <<= 1; 69 | } 70 | digitalWrite(sda_pin, HIGH); 71 | digitalWrite(scl_pin, HIGH); 72 | ack_bit = digitalRead(sda_pin); 73 | digitalWrite(scl_pin, LOW); 74 | return ack_bit; 75 | } 76 | 77 | uint8_t I2CRead(){ 78 | uint8_t i, Data=0; 79 | for (i = 0; i < 8; i++) { 80 | digitalWrite(scl_pin, HIGH); 81 | if(digitalRead(sda_pin)) 82 | Data |=1; 83 | if(i<7) 84 | Data<<=1; 85 | digitalWrite(scl_pin, LOW); 86 | } 87 | return Data; 88 | } 89 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/SoftI2C/src/SoftI2C.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 3 | * Soft I2C library for arduino. 4 | * 5 | * This file is free software; you can redistribute it and/or modify 6 | * it under the terms of either the GNU General Public License version 2 7 | * or the GNU Lesser General Public License version 2.1, both as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | #ifndef _SOFTI2C_H_INCLUDED 12 | #define _SOFTI2C_H_INCLUDED 13 | 14 | #include 15 | 16 | extern __xdata uint8_t scl_pin; 17 | extern __xdata uint8_t sda_pin; 18 | 19 | void I2CInit(); 20 | void I2CStart(); 21 | void I2CRestart(); 22 | void I2CStop(); 23 | void I2CAck(); 24 | void I2CNak(); 25 | uint8_t I2CSend(uint8_t i2cData); 26 | uint8_t I2CRead(); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/TouchKey/examples/TouchKeyExample/TouchKeyExample.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define LED_BUILTIN 33 4 | 5 | uint8_t prevSensorValue = 0; 6 | 7 | void setup() { 8 | // put your setup code here, to run once: 9 | pinMode(LED_BUILTIN, OUTPUT); 10 | TouchKey_begin( (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) ); //Enable all 6 channels: TIN0(P1.0), TIN1(P1.1), TIN2(P1.4), TIN3(P1.5), TIN4(P1.6), TIN5(P1.7) 11 | 12 | // In most cases you don't need to adjust parameters. But if you do, test with TouchKeyTuneParameter example 13 | /* 14 | //refer to AN3891 MPR121 Baseline System for more details 15 | TouchKey_SetMaxHalfDelta(5); //increase if sensor value are more noisy 16 | TouchKey_SetNoiseHalfDelta(2); //If baseline need to adjust at higher rate, increase this value 17 | TouchKey_SetNoiseCountLimit(10); //If baseline need to adjust faster, increase this value 18 | TouchKey_SetFilterDelayLimit(5); //make overall adjustment slopwer 19 | 20 | TouchKey_SetTouchThreshold(100); //Bigger touch pad can use a bigger value 21 | TouchKey_SetReleaseThreshold(80); //Smaller than touch threshold 22 | */ 23 | } 24 | 25 | void loop() { 26 | // put your main code here, to run repeatedly: 27 | TouchKey_Process(); 28 | uint8_t touchResult = TouchKey_Get(); 29 | if (touchResult) { 30 | digitalWrite(LED_BUILTIN, HIGH); 31 | } else { 32 | digitalWrite(LED_BUILTIN, LOW); 33 | } 34 | for (uint8_t i = 0; i < 6; i++) { 35 | USBSerial_print("TIN"); 36 | USBSerial_print(i); 37 | if (touchResult & (1 << i)) { 38 | USBSerial_print(" X "); 39 | } else { 40 | USBSerial_print(" _ "); 41 | } 42 | } 43 | USBSerial_println(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/TouchKey/examples/TouchKeyTuneParameter/TouchKeyTuneParameter.ino: -------------------------------------------------------------------------------- 1 | /* 2 | TouchKey Tune Parameter 3 | 4 | An example help user to understand effect of each parameter of the TouchKey library. 5 | This example only monitor Touch channel 1 (P1.1). 6 | You may open Serial Plotter to see how the value changes when you touch P1.1 7 | 8 | created 2020 9 | by Deqing Sun for use with CH55xduino 10 | 11 | This example code is in the public domain. 12 | 13 | */ 14 | 15 | #include 16 | 17 | #define LED_BUILTIN 33 18 | 19 | uint8_t prevCounter = 0; 20 | 21 | //following data for debug only 22 | extern volatile __xdata uint16_t touchRawValue[]; 23 | extern __xdata uint16_t touchBaseline[]; 24 | extern __xdata uint8_t touchThreshold; 25 | extern __xdata uint8_t releaseThreshold; 26 | 27 | void setup() { 28 | // put your setup code here, to run once: 29 | pinMode(LED_BUILTIN, OUTPUT); 30 | TouchKey_begin(1 << 1); //only do test on P1.1 31 | 32 | //refer to AN3891 MPR121 Baseline System for more details 33 | TouchKey_SetMaxHalfDelta(5); //increase if sensor value are more noisy 34 | TouchKey_SetNoiseHalfDelta(2); //If baseline need to adjust at higher rate, increase this value 35 | TouchKey_SetNoiseCountLimit(10); //If baseline need to adjust faster, increase this value 36 | TouchKey_SetFilterDelayLimit(5); //make overall adjustment slopwer 37 | 38 | TouchKey_SetTouchThreshold(100); //Bigger touch pad can use a bigger value 39 | TouchKey_SetReleaseThreshold(80); //Smaller than touch threshold 40 | 41 | } 42 | 43 | void loop() { 44 | // put your main code here, to run repeatedly: 45 | uint8_t counter = TouchKey_Process(); 46 | if (prevCounter != counter) { 47 | prevCounter = counter; //this is a new cycle of touch data. 48 | uint16_t rawValueBuf; 49 | EA = 0; 50 | rawValueBuf = touchRawValue[1]; //prevent interrupt from changing it 51 | EA = 1; 52 | USBSerial_print("RawValue:"); 53 | USBSerial_print(rawValueBuf); 54 | USBSerial_print(",touchThreshold:"); 55 | USBSerial_print(touchBaseline[1] - touchThreshold); 56 | USBSerial_print(",releaseThreshold:"); 57 | USBSerial_print(touchBaseline[1] - releaseThreshold); 58 | USBSerial_print(",Baseline:"); 59 | USBSerial_println(touchBaseline[1]); 60 | } 61 | uint8_t touchResult = TouchKey_Get(); 62 | if (touchResult & (1 << 1)) { 63 | digitalWrite(LED_BUILTIN, HIGH); 64 | } else { 65 | digitalWrite(LED_BUILTIN, LOW); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/TouchKey/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For TouchKey 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | 14 | ####################################### 15 | # Instances (KEYWORD2) 16 | ####################################### 17 | 18 | 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/TouchKey/library.properties: -------------------------------------------------------------------------------- 1 | name=Touch Key 2 | version=1.0 3 | author=Deqing Sun 4 | maintainer=Deqing Sun 5 | sentence=Enables the touch key feature on CH552. 6 | paragraph=CH552 has internal capacititve touch key module that supports 6 channels. This is the library enables that. 7 | category=Sensors 8 | url=https://github.com/DeqingSun/ch55xduino 9 | architectures=mcs51 10 | 11 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/TouchKey/src/TouchKey.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 3 | * Touch key library for arduino CH552. 4 | * 5 | * This file is free software; you can redistribute it and/or modify 6 | * it under the terms of either the GNU General Public License version 2 7 | * or the GNU Lesser General Public License version 2.1, both as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | #ifndef _TOUCHKEY_H_INCLUDED 12 | #define _TOUCHKEY_H_INCLUDED 13 | #include 14 | 15 | void TouchKey_begin(uint8_t channelToEnableBitMask); 16 | void TouchKey_end(void); 17 | uint8_t TouchKey_Process(); 18 | uint8_t TouchKey_Get(); 19 | 20 | void TouchKey_SetMaxHalfDelta(uint8_t val); 21 | void TouchKey_SetNoiseHalfDelta(uint8_t val); 22 | void TouchKey_SetNoiseCountLimit(uint8_t val); 23 | void TouchKey_SetFilterDelayLimit(uint8_t val); 24 | void TouchKey_SetTouchThreshold(uint8_t val); 25 | void TouchKey_SetReleaseThreshold(uint8_t val); 26 | 27 | #endif -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/examples/chaseTest/chaseTest.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NUM_LEDS 8 4 | #define COLOR_PER_LEDS 3 5 | #define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS) 6 | 7 | #if NUM_BYTES > 255 8 | #error "NUM_BYTES can not be larger than 255." 9 | #endif 10 | 11 | __xdata uint8_t ledData[NUM_BYTES]; 12 | 13 | void setup() { 14 | pinMode(15, OUTPUT); //Possible to use other pins. 15 | } 16 | 17 | void loop() { 18 | 19 | for (uint8_t i = 0; i < NUM_LEDS; i++) { 20 | set_pixel_for_GRB_LED(ledData, i, 1, 0, 0); //Choose the color order depending on the LED you use. 21 | neopixel_show_P1_5(ledData, NUM_BYTES); //Possible to use other pins. 22 | delay(100); 23 | } 24 | for (uint8_t i = 0; i < NUM_LEDS; i++) { 25 | set_pixel_for_GRB_LED(ledData, i, 0, 1, 0); 26 | neopixel_show_P1_5(ledData, NUM_BYTES); 27 | delay(100); 28 | } 29 | for (uint8_t i = 0; i < NUM_LEDS; i++) { 30 | set_pixel_for_GRB_LED(ledData, i, 0, 0, 1); 31 | neopixel_show_P1_5(ledData, NUM_BYTES); 32 | delay(100); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For WS2812 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | 14 | ####################################### 15 | # Instances (KEYWORD2) 16 | ####################################### 17 | 18 | 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/library.properties: -------------------------------------------------------------------------------- 1 | name=WS2812 2 | version=1.0 3 | author=Deqing Sun 4 | maintainer=Deqing Sun 5 | sentence=Use WS2812 (NeoPixel or other compatibles) on CH552. 6 | paragraph=Use WS2812 (NeoPixel or other compatibles) on CH552. This version supports 24M clock for 800Khz signal. 7 | category=Display 8 | url=https://github.com/DeqingSun/ch55xduino 9 | architectures=mcs51 10 | 11 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/src/WS2812.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 3 | * WS2812 library for arduino CH552. 4 | * 5 | * This file is free software; you can redistribute it and/or modify 6 | * it under the terms of either the GNU General Public License version 2 7 | * or the GNU Lesser General Public License version 2.1, both as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | 12 | #include "WS2812.h" 13 | 14 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/src/WS2812.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 by Deqing Sun (c version for CH552 port) 3 | * Touch key library for arduino CH552. 4 | * 5 | * This file is free software; you can redistribute it and/or modify 6 | * it under the terms of either the GNU General Public License version 2 7 | * or the GNU Lesser General Public License version 2.1, both as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | #ifndef _WS2812_H_INCLUDED 12 | #define _WS2812_H_INCLUDED 13 | #include 14 | #include "template/WS2812_pins_header.h" 15 | 16 | #define set_pixel_for_RGB_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(R);ptr[1]=(G);ptr[2]=(B);}; 17 | #define set_pixel_for_GRB_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(G);ptr[1]=(R);ptr[2]=(B);}; 18 | #define set_pixel_for_RBG_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(R);ptr[1]=(B);ptr[2]=(G);}; 19 | #define set_pixel_for_GBR_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(G);ptr[1]=(B);ptr[2]=(R);}; 20 | #define set_pixel_for_BRG_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(B);ptr[1]=(R);ptr[2]=(G);}; 21 | #define set_pixel_for_BGR_LED(ADDR,INDEX,R,G,B) {__xdata uint8_t *ptr=(ADDR)+((INDEX)*3);ptr[0]=(B);ptr[1]=(G);ptr[2]=(R);}; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/src/template/WS2812_pins_header.h: -------------------------------------------------------------------------------- 1 | //This file is generated by a script. 2 | #ifndef _WS2812_PIND_HEADER_H_INCLUDED 3 | #define _WS2812_PIND_HEADER_H_INCLUDED 4 | void neopixel_show_long_P1_0(uint32_t dataAndLen); 5 | #define neopixel_show_P1_0(ADDR,LEN) neopixel_show_long_P1_0((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 6 | void neopixel_show_long_P1_1(uint32_t dataAndLen); 7 | #define neopixel_show_P1_1(ADDR,LEN) neopixel_show_long_P1_1((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 8 | void neopixel_show_long_P1_2(uint32_t dataAndLen); 9 | #define neopixel_show_P1_2(ADDR,LEN) neopixel_show_long_P1_2((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 10 | void neopixel_show_long_P1_3(uint32_t dataAndLen); 11 | #define neopixel_show_P1_3(ADDR,LEN) neopixel_show_long_P1_3((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 12 | void neopixel_show_long_P1_4(uint32_t dataAndLen); 13 | #define neopixel_show_P1_4(ADDR,LEN) neopixel_show_long_P1_4((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 14 | void neopixel_show_long_P1_5(uint32_t dataAndLen); 15 | #define neopixel_show_P1_5(ADDR,LEN) neopixel_show_long_P1_5((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 16 | void neopixel_show_long_P1_6(uint32_t dataAndLen); 17 | #define neopixel_show_P1_6(ADDR,LEN) neopixel_show_long_P1_6((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 18 | void neopixel_show_long_P1_7(uint32_t dataAndLen); 19 | #define neopixel_show_P1_7(ADDR,LEN) neopixel_show_long_P1_7((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 20 | void neopixel_show_long_P3_0(uint32_t dataAndLen); 21 | #define neopixel_show_P3_0(ADDR,LEN) neopixel_show_long_P3_0((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 22 | void neopixel_show_long_P3_1(uint32_t dataAndLen); 23 | #define neopixel_show_P3_1(ADDR,LEN) neopixel_show_long_P3_1((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 24 | void neopixel_show_long_P3_2(uint32_t dataAndLen); 25 | #define neopixel_show_P3_2(ADDR,LEN) neopixel_show_long_P3_2((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 26 | void neopixel_show_long_P3_3(uint32_t dataAndLen); 27 | #define neopixel_show_P3_3(ADDR,LEN) neopixel_show_long_P3_3((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 28 | void neopixel_show_long_P3_4(uint32_t dataAndLen); 29 | #define neopixel_show_P3_4(ADDR,LEN) neopixel_show_long_P3_4((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 30 | void neopixel_show_long_P3_5(uint32_t dataAndLen); 31 | #define neopixel_show_P3_5(ADDR,LEN) neopixel_show_long_P3_5((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 32 | void neopixel_show_long_P3_6(uint32_t dataAndLen); 33 | #define neopixel_show_P3_6(ADDR,LEN) neopixel_show_long_P3_6((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 34 | void neopixel_show_long_P3_7(uint32_t dataAndLen); 35 | #define neopixel_show_P3_7(ADDR,LEN) neopixel_show_long_P3_7((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); 36 | #endif 37 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/libraries/WS2812/src/template/generateCode.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | scriptPath = os.path.dirname(os.path.realpath(__file__)) 4 | 5 | templateFile = open(scriptPath+"/WS2812_PIN_._c") 6 | templateContent = templateFile.read() 7 | templateFile.close() 8 | 9 | generatePinP1 = ("P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7") 10 | generatePinP3 = ("P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7") 11 | generatePins = generatePinP1 + generatePinP3 12 | 13 | for pin in generatePins: 14 | generateFileContent = "//This file is generated by a script. \r\n" +templateContent.replace("PX_X",pin); 15 | #print generateFileContent 16 | outputFile = open(scriptPath+"/optionalLink_WS2812_"+pin+".c", "w") 17 | outputFile.write(generateFileContent) 18 | outputFile.close() 19 | 20 | #generateHeader 21 | outputFile = open(scriptPath+"/WS2812_pins_header.h", "w") 22 | outputFile.write("//This file is generated by a script. \r\n") 23 | outputFile.write("#ifndef _WS2812_PIND_HEADER_H_INCLUDED\r\n") 24 | outputFile.write("#define _WS2812_PIND_HEADER_H_INCLUDED\r\n") 25 | 26 | for pin in generatePins: 27 | outputFile.write("void neopixel_show_long_"+pin+"(uint32_t dataAndLen); \r\n") 28 | outputFile.write("#define neopixel_show_"+pin+"(ADDR,LEN) neopixel_show_long_"+pin+"((((uint16_t)(ADDR)) & 0xFFFF) | (((uint32_t)(LEN) & 0xFF) << 16)); \r\n") 29 | 30 | outputFile.write("#endif \r\n") 31 | outputFile.close() 32 | 33 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch549/include/CH549.H: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/ch55x/variants/ch549/include/CH549.H -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch549/pins_arduino_include.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef Pins_Arduino_Include_h 4 | #define Pins_Arduino_Include_h 5 | 6 | #ifndef _BV 7 | #define _BV(X) (1<<(X)) 8 | #endif 9 | 10 | #define NOT_A_PIN 0 11 | #define NOT_A_PORT 0 12 | 13 | #define NOT_AN_INTERRUPT -1 14 | #define NOT_ANALOG 255 15 | 16 | 17 | #define P0PORT 1 18 | #define P1PORT 2 19 | #define P2PORT 3 20 | #define P3PORT 4 21 | #define P4PORT 5 22 | #define P5PORT 6 23 | 24 | 25 | enum { 26 | NOT_ON_PWM = 0, 27 | //PIN_PWM1, todo: add pwm 28 | //PIN_PWM2, 29 | //PIN_PWM1_, 30 | //PIN_PWM2_, 31 | NUM_PWM_PINS 32 | }; 33 | 34 | 35 | extern __code uint8_t digital_pin_to_pwm_PGM[]; 36 | 37 | extern __code uint8_t digital_pin_to_port_PGM[]; 38 | 39 | extern __code uint8_t digital_pin_to_bit_mask_PGM[]; 40 | 41 | extern __code uint8_t digital_pin_to_channel_PGM[]; 42 | 43 | // Get the bit location within the hardware port of the given virtual pin. 44 | // This comes from the pins_*.c file for the active board configuration. 45 | // 46 | #define digitalPinToPort(P) ( digital_pin_to_port_PGM[(P)] ) 47 | #define digitalPinToBitMask(P) ( digital_pin_to_bit_mask_PGM[(P)] ) 48 | #define digitalPinToPWM(P) ( digital_pin_to_pwm_PGM[(P)] ) 49 | #define analogPinToChannel(P) ( digital_pin_to_channel_PGM[(P)] ) 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch552/include/ch5xx_datatypes.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------- 2 | CH554.H 3 | Header file for CH554 microcontrollers. 4 | **************************************** 5 | ** Copyright (C) W.ch 1999-2014 ** 6 | ** Web: http://wch.cn ** 7 | **************************************** 8 | --------------------------------------------------------------------------*/ 9 | 10 | #ifndef __BASE_TYPE__ 11 | #define __BASE_TYPE__ 12 | 13 | /*----- constant and type define -----------------------------------------*/ 14 | 15 | #ifndef TRUE 16 | #define TRUE 1 17 | #define FALSE 0 18 | #endif 19 | #ifndef NULL 20 | #define NULL 0 21 | #endif 22 | 23 | #ifndef BOOL 24 | typedef bit BOOL; 25 | #endif 26 | #ifndef UINT8 27 | typedef unsigned char UINT8; 28 | #endif 29 | #ifndef UINT16 30 | typedef unsigned short UINT16; 31 | #endif 32 | #ifndef UINT32 33 | typedef unsigned long UINT32; 34 | #endif 35 | #ifndef UINT8D 36 | typedef unsigned char data UINT8D; 37 | #endif 38 | #ifndef UINT16D 39 | typedef unsigned short data UINT16D; 40 | #endif 41 | #ifndef UINT32D 42 | typedef unsigned long data UINT32D; 43 | #endif 44 | #ifndef UINT8I 45 | typedef unsigned char idata UINT8I; 46 | #endif 47 | #ifndef UINT16I 48 | typedef unsigned short idata UINT16I; 49 | #endif 50 | #ifndef UINT32I 51 | typedef unsigned long idata UINT32I; 52 | #endif 53 | #ifndef UINT8X 54 | typedef unsigned char xdata UINT8X; 55 | #endif 56 | #ifndef UINT16X 57 | typedef unsigned short xdata UINT16X; 58 | #endif 59 | #ifndef UINT32X 60 | typedef unsigned long xdata UINT32X; 61 | #endif 62 | #ifndef UINT8V 63 | typedef unsigned char volatile UINT8V; 64 | #endif 65 | #ifndef UINT8DV 66 | typedef unsigned char volatile data UINT8DV; 67 | #endif 68 | #ifndef UINT8XV 69 | typedef unsigned char volatile xdata UINT8XV; 70 | #endif 71 | #ifndef UINT8PV 72 | typedef unsigned char volatile pdata UINT8PV; 73 | #endif 74 | #ifndef UINT8C 75 | typedef const unsigned char code UINT8C; 76 | #endif 77 | #ifndef PUINT8 78 | typedef unsigned char *PUINT8; 79 | #endif 80 | #ifndef PUINT16 81 | typedef unsigned short *PUINT16; 82 | #endif 83 | #ifndef PUINT32 84 | typedef unsigned long *PUINT32; 85 | #endif 86 | #ifndef PUINT8I 87 | typedef unsigned char idata *PUINT8I; 88 | #endif 89 | #ifndef PUINT16I 90 | typedef unsigned short idata *PUINT16I; 91 | #endif 92 | #ifndef PUINT32I 93 | typedef unsigned long idata *PUINT32I; 94 | #endif 95 | #ifndef PUINT8X 96 | typedef unsigned char xdata *PUINT8X; 97 | #endif 98 | #ifndef PUINT16X 99 | typedef unsigned short xdata *PUINT16X; 100 | #endif 101 | #ifndef PUINT32X 102 | typedef unsigned long xdata *PUINT32X; 103 | #endif 104 | #ifndef PUINT8V 105 | typedef unsigned char volatile *PUINT8V; 106 | #endif 107 | #ifndef PUINT8DV 108 | typedef unsigned char volatile data *PUINT8DV; 109 | #endif 110 | #ifndef PUINT8XV 111 | typedef unsigned char volatile xdata *PUINT8XV; 112 | #endif 113 | #ifndef PUINT8PV 114 | typedef unsigned char volatile pdata *PUINT8PV; 115 | #endif 116 | #ifndef PUINT8C 117 | typedef const unsigned char code *PUINT8C; 118 | #endif 119 | 120 | #ifndef STRUCT_OFFSET 121 | #define STRUCT_OFFSET( s, m ) ( (UINT8)( & (((s) *)0) -> (m) ) ) /* get the offset address for a member of a structure */ 122 | #endif 123 | 124 | #endif // __BASE_TYPE__ 125 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch552/include/ch5xx_usb.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/ch55x/variants/ch552/include/ch5xx_usb.h -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch552/pins_arduino_include.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef Pins_Arduino_Include_h 4 | #define Pins_Arduino_Include_h 5 | 6 | #ifndef _BV 7 | #define _BV(X) (1<<(X)) 8 | #endif 9 | 10 | #define NOT_A_PIN 0 11 | #define NOT_A_PORT 0 12 | 13 | #define NOT_AN_INTERRUPT -1 14 | #define NOT_ANALOG 255 15 | 16 | 17 | #define P0PORT 1 18 | #define P1PORT 2 19 | #define P2PORT 3 20 | #define P3PORT 4 21 | 22 | 23 | enum { 24 | NOT_ON_PWM = 0, 25 | PIN_PWM1, 26 | PIN_PWM2, 27 | PIN_PWM1_, 28 | PIN_PWM2_, 29 | NUM_PWM_PINS 30 | }; 31 | 32 | 33 | extern __code uint8_t digital_pin_to_pwm_PGM[]; 34 | 35 | extern __code uint8_t digital_pin_to_port_PGM[]; 36 | 37 | extern __code uint8_t digital_pin_to_bit_mask_PGM[]; 38 | 39 | extern __code uint8_t digital_pin_to_channel_PGM[]; 40 | 41 | // Get the bit location within the hardware port of the given virtual pin. 42 | // This comes from the pins_*.c file for the active board configuration. 43 | // 44 | #define digitalPinToPort(P) ( digital_pin_to_port_PGM[(P)] ) 45 | #define digitalPinToBitMask(P) ( digital_pin_to_bit_mask_PGM[(P)] ) 46 | #define digitalPinToPWM(P) ( digital_pin_to_pwm_PGM[(P)] ) 47 | #define analogPinToChannel(P) ( digital_pin_to_channel_PGM[(P)] ) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch559/include/ch5xx_datatypes.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------- 2 | CH554.H 3 | Header file for CH554 microcontrollers. 4 | **************************************** 5 | ** Copyright (C) W.ch 1999-2014 ** 6 | ** Web: http://wch.cn ** 7 | **************************************** 8 | --------------------------------------------------------------------------*/ 9 | 10 | #ifndef __BASE_TYPE__ 11 | #define __BASE_TYPE__ 12 | 13 | /*----- constant and type define -----------------------------------------*/ 14 | 15 | #ifndef TRUE 16 | #define TRUE 1 17 | #define FALSE 0 18 | #endif 19 | #ifndef NULL 20 | #define NULL 0 21 | #endif 22 | 23 | #ifndef BOOL 24 | typedef bit BOOL; 25 | #endif 26 | #ifndef UINT8 27 | typedef unsigned char UINT8; 28 | #endif 29 | #ifndef UINT16 30 | typedef unsigned short UINT16; 31 | #endif 32 | #ifndef UINT32 33 | typedef unsigned long UINT32; 34 | #endif 35 | #ifndef UINT8D 36 | typedef unsigned char data UINT8D; 37 | #endif 38 | #ifndef UINT16D 39 | typedef unsigned short data UINT16D; 40 | #endif 41 | #ifndef UINT32D 42 | typedef unsigned long data UINT32D; 43 | #endif 44 | #ifndef UINT8I 45 | typedef unsigned char idata UINT8I; 46 | #endif 47 | #ifndef UINT16I 48 | typedef unsigned short idata UINT16I; 49 | #endif 50 | #ifndef UINT32I 51 | typedef unsigned long idata UINT32I; 52 | #endif 53 | #ifndef UINT8X 54 | typedef unsigned char xdata UINT8X; 55 | #endif 56 | #ifndef UINT16X 57 | typedef unsigned short xdata UINT16X; 58 | #endif 59 | #ifndef UINT32X 60 | typedef unsigned long xdata UINT32X; 61 | #endif 62 | #ifndef UINT8V 63 | typedef unsigned char volatile UINT8V; 64 | #endif 65 | #ifndef UINT8DV 66 | typedef unsigned char volatile data UINT8DV; 67 | #endif 68 | #ifndef UINT8XV 69 | typedef unsigned char volatile xdata UINT8XV; 70 | #endif 71 | #ifndef UINT8PV 72 | typedef unsigned char volatile pdata UINT8PV; 73 | #endif 74 | #ifndef UINT8C 75 | typedef const unsigned char code UINT8C; 76 | #endif 77 | #ifndef PUINT8 78 | typedef unsigned char *PUINT8; 79 | #endif 80 | #ifndef PUINT16 81 | typedef unsigned short *PUINT16; 82 | #endif 83 | #ifndef PUINT32 84 | typedef unsigned long *PUINT32; 85 | #endif 86 | #ifndef PUINT8I 87 | typedef unsigned char idata *PUINT8I; 88 | #endif 89 | #ifndef PUINT16I 90 | typedef unsigned short idata *PUINT16I; 91 | #endif 92 | #ifndef PUINT32I 93 | typedef unsigned long idata *PUINT32I; 94 | #endif 95 | #ifndef PUINT8X 96 | typedef unsigned char xdata *PUINT8X; 97 | #endif 98 | #ifndef PUINT16X 99 | typedef unsigned short xdata *PUINT16X; 100 | #endif 101 | #ifndef PUINT32X 102 | typedef unsigned long xdata *PUINT32X; 103 | #endif 104 | #ifndef PUINT8V 105 | typedef unsigned char volatile *PUINT8V; 106 | #endif 107 | #ifndef PUINT8DV 108 | typedef unsigned char volatile data *PUINT8DV; 109 | #endif 110 | #ifndef PUINT8XV 111 | typedef unsigned char volatile xdata *PUINT8XV; 112 | #endif 113 | #ifndef PUINT8PV 114 | typedef unsigned char volatile pdata *PUINT8PV; 115 | #endif 116 | #ifndef PUINT8C 117 | typedef const unsigned char code *PUINT8C; 118 | #endif 119 | 120 | #ifndef STRUCT_OFFSET 121 | #define STRUCT_OFFSET( s, m ) ( (UINT8)( & (((s) *)0) -> (m) ) ) /* get the offset address for a member of a structure */ 122 | #endif 123 | 124 | #endif // __BASE_TYPE__ 125 | -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch559/include/ch5xx_usb.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/ch55x/variants/ch559/include/ch5xx_usb.h -------------------------------------------------------------------------------- /ch55xduino/ch55x/variants/ch559/pins_arduino_include.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef Pins_Arduino_Include_h 4 | #define Pins_Arduino_Include_h 5 | 6 | #ifndef _BV 7 | #define _BV(X) (1<<(X)) 8 | #endif 9 | 10 | #define NOT_A_PIN 0 11 | #define NOT_A_PORT 0 12 | 13 | #define NOT_AN_INTERRUPT -1 14 | #define NOT_ANALOG 255 15 | 16 | 17 | #define P0PORT 1 18 | #define P1PORT 2 19 | #define P2PORT 3 20 | #define P3PORT 4 21 | #define P4PORT 5 22 | #define P5PORT 6 23 | 24 | 25 | enum { 26 | NOT_ON_PWM = 0, 27 | PIN_PWM1, 28 | PIN_PWM2, 29 | PIN_PWM1_, 30 | PIN_PWM2_, 31 | // todo: add pwm3 32 | NUM_PWM_PINS 33 | }; 34 | 35 | 36 | extern __code uint8_t digital_pin_to_pwm_PGM[]; 37 | 38 | extern __code uint8_t digital_pin_to_port_PGM[]; 39 | 40 | extern __code uint8_t digital_pin_to_bit_mask_PGM[]; 41 | 42 | extern __code uint8_t digital_pin_to_channel_PGM[]; 43 | 44 | // Get the bit location within the hardware port of the given virtual pin. 45 | // This comes from the pins_*.c file for the active board configuration. 46 | // 47 | #define digitalPinToPort(P) ( digital_pin_to_port_PGM[(P)] ) 48 | #define digitalPinToBitMask(P) ( digital_pin_to_bit_mask_PGM[(P)] ) 49 | #define digitalPinToPWM(P) ( digital_pin_to_pwm_PGM[(P)] ) 50 | #define analogPinToChannel(P) ( digital_pin_to_channel_PGM[(P)] ) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /ch55xduino/filePacker/gen_platform_entry.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # helper script for automatic releases. 4 | # 5 | # Prints one platform entry for the given file to be added to the 6 | # package_*.json file. 7 | 8 | 9 | if [ $# -ne 4 ]; then 10 | echo 11 | echo "helper script for automatic releases." 12 | echo 13 | echo "Prints one platform entry for the given file to be added to the" 14 | echo "package_*_index.json file." 15 | echo 16 | echo "usage: $0 corefile coreversion sdccversion toolsversion" 17 | exit 1 18 | fi 19 | 20 | COREFILE=$1 21 | COREVERSION=$2 22 | SDCCVERSION=$3 23 | TOOLSVERSION=$4 24 | PACKAGER=CH55xDuino 25 | 26 | BASEURL=https://github.com/DeqingSun/ch55xduino/releases/download/${COREVERSION} 27 | 28 | ### helper functions ##################################################### 29 | 30 | # format ID information for a file 31 | print_filedata() 32 | { 33 | FILENAME=$(basename "$1") 34 | URL=${BASEURL}/${FILENAME} 35 | SIZE=$(stat -f%z $1) 36 | CHKSUM=$(shasum -a 256 $1|cut "-d " -f1) 37 | cat << EOF 38 | "url": "$URL", 39 | "archiveFileName": "$FILENAME", 40 | "checksum": "SHA-256:$CHKSUM", 41 | "size": "$SIZE" 42 | EOF 43 | } 44 | 45 | 46 | 47 | # list of supported boards in current boards.txt 48 | list_boards() 49 | { 50 | echo -n " \"boards\": [" 51 | n=0 52 | sed -n "s/.*\.name=//p" ../ch55x/boards.txt |\ 53 | while read line; do 54 | if [ $n -ne 0 ]; then echo -n ","; fi 55 | echo 56 | echo -n " {\"name\": \"$line\"}" 57 | n=$((n+1)) 58 | done 59 | echo 60 | echo " ]," 61 | } 62 | 63 | 64 | 65 | ### print a platform entry for the given file ############################ 66 | 67 | cat << EOF 68 | { 69 | "name": "CH55xDuino MCS51 plain C core (non-C++)", 70 | "architecture": "mcs51", 71 | "version": "$COREVERSION", 72 | "category": "Contributed", 73 | EOF 74 | list_boards 75 | cat << EOF 76 | "toolsDependencies": [ 77 | { 78 | "name": "MCS51Tools", 79 | "version": "$TOOLSVERSION", 80 | "packager": "$PACKAGER" 81 | }, 82 | { 83 | "name": "sdcc", 84 | "version": "build.$SDCCVERSION", 85 | "packager": "$PACKAGER" 86 | } 87 | ], 88 | EOF 89 | print_filedata "$COREFILE" 90 | echo "}," 91 | -------------------------------------------------------------------------------- /ch55xduino/filePacker/gen_tools_entry.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # helper script for automatic releases. 4 | # 5 | # Prints one platform entry for the given file to be added to the 6 | # package_*.json file. 7 | 8 | 9 | if [ $# -ne 3 ]; then 10 | echo 11 | echo "helper script for automatic releases." 12 | echo 13 | echo "Prints a full tool entry for the given filename stem to be added to the" 14 | echo "package_*_index.json file. It will include all OS variants found with the" 15 | echo "same filename stem and the same version specifier." 16 | echo 17 | echo "usage: $0 toolfile-stem toolversion coreversion" 18 | echo 19 | echo "Lists info for all files matching the filename pattern" 20 | echo "[toolsfile-stem]*[version]*" 21 | echo 22 | echo "The coreversion is only needed to build the download link." 23 | echo 24 | echo "Example: $0 release/sduino-tools 2017-10-21 0.3.1" 25 | echo " prints information for all files matching the filename pattern" 26 | echo " release/sduino-tools*2017-10-21* and generates download links" 27 | echo " for a github release directory download/v0.3.1." 28 | echo 29 | exit 1 30 | fi 31 | 32 | TRUNK=$1 33 | VERSION=$2 34 | COREVERSION=$3 35 | 36 | BASEURL=https://github.com/DeqingSun/ch55xduino/releases/download/${COREVERSION} 37 | 38 | ### helper functions ##################################################### 39 | 40 | # format ID information for a file 41 | # 42 | # usage: print_filedata filename 43 | # 44 | print_filedata() 45 | { 46 | FILENAME=$(basename "$1") 47 | URL=${BASEURL}/${FILENAME} 48 | SIZE=$(stat -f%z $1) 49 | CHKSUM=$(shasum -a 256 $1|cut "-d " -f1) 50 | cat << EOF 51 | "url": "$URL", 52 | "archiveFileName": "$FILENAME", 53 | "checksum": "SHA-256:$CHKSUM", 54 | "size": "$SIZE" 55 | EOF 56 | } 57 | 58 | 59 | # detect the host system type for the given file 60 | detect_hosttype() 61 | { 62 | case $1 in 63 | *amd64-unknown-linux* | *linux64* | *amd64-ubuntu*-linux* ) 64 | HOST="x86_64-pc-linux-gnu" 65 | ;; 66 | *mingw32*) 67 | HOST="i686-mingw32" 68 | ;; 69 | *i386-unknown-linux* | *linux32* ) 70 | HOST="i686-pc-linux-gnu" 71 | ;; 72 | *macosx*) 73 | HOST="x86_64-apple-darwin" 74 | ;; 75 | esac 76 | } 77 | 78 | 79 | # detect the tool type/name for the given file 80 | detect_tooltype() 81 | { 82 | case $1 in 83 | *ch55xduino-tools* ) 84 | NAME=MCS51Tools 85 | VERSIONSTRING=$VERSION 86 | ;; 87 | *sdcc* ) 88 | NAME=sdcc 89 | VERSIONSTRING=build.$VERSION 90 | ;; 91 | esac 92 | } 93 | 94 | 95 | 96 | ### print a tool entry for the given file ############################ 97 | 98 | 99 | 100 | detect_tooltype "$TRUNK" 101 | cat << EOF 102 | { 103 | "name": "$NAME", 104 | "version": "$VERSIONSTRING", 105 | "systems": [ 106 | { 107 | EOF 108 | n=0 109 | for FILE in $TRUNK*$VERSION.*; do 110 | if [ $n -gt 0 ]; then 111 | echo " },{" 112 | fi 113 | detect_hosttype "$FILE" 114 | echo " \"host\": \"$HOST\"," 115 | print_filedata "$FILE" 116 | n=$((n+1)) 117 | done 118 | cat << EOF 119 | } 120 | ] 121 | }, 122 | EOF 123 | -------------------------------------------------------------------------------- /ch55xduino/filePacker/minimize-sdcc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -lt 1 ]; then 4 | echo "Minimize a SDCC snapshot build by leaving out all non-MCS51 files." 5 | echo 6 | echo "usage: $0 sdcc-snapshot-filename" 7 | exit 1 8 | fi 9 | 10 | 11 | #FILE=~/Downloads/sdcc-snapshot-amd64-unknown-linux2.5-20170919-9998.tar.bz2 12 | FILE=$(realpath $1) 13 | 14 | case "$FILE" in 15 | *.tar.bz2 | *.tbz) 16 | TARFLAG=j 17 | ;; 18 | *.tar.gz | *.tgz) 19 | TARFLAG=z 20 | ;; 21 | *.zip) 22 | TARFLAG=zip 23 | ;; 24 | esac 25 | 26 | # set to v for verbose mode 27 | VERBOSE= 28 | 29 | # transform the filename from "*-snapshot-*" to "*-mcs51-*" 30 | NAME=$(basename "$FILE") 31 | NAME=${NAME/snapshot/mcs51} 32 | 33 | # remove all suffixes 34 | NAME=${NAME%%.t*} 35 | NAME=${NAME%%.z*} 36 | 37 | # patterns to exclude all unneeded files from unpacking 38 | TAR_EXCLUDE='--exclude=doc --exclude=src --exclude=non-free 39 | --exclude=stlcs --exclude=ds80c390.h --exclude=pic* 40 | --exclude=*00 --exclude=*08 --exclude=*2k 41 | --exclude=include/mcs51 --exclude=*80 --exclude=*90 42 | --exclude=*gb --exclude=*ka --exclude=*.info 43 | --exclude=*pdk* --exclude=*rab --exclude=huge 44 | --exclude=large* --exclude=medium --exclude=stm* 45 | --exclude=mos6502* --exclude=z80* --exclude=sm83 46 | --exclude=man --exclude=small-stack-auto 47 | --exclude=bin/ucsim* --exclude=bin/as2gbmap --exclude=bin/makebin 48 | --exclude=bin/packihx --exclude=bin/s51 --exclude=bin/savr 49 | --exclude=bin/*stm* --exclude=bin/sdcdb* --exclude=bin/sdnm 50 | --exclude=bin/sdobjcopy --exclude=bin/sdranlib --exclude=bin/serialview 51 | --exclude=bin/sm* --exclude=bin/sp* --exclude=bin/sr* --exclude=bin/ss* --exclude=bin/sx* 52 | ' 53 | 54 | ZIP_EXCLUDE='-x */doc/* */man/* */non-free/* */src/* *stlcs* */pic* 55 | *00* *08* *z80* *z180* *90* *2k* *gb* *ka* */info/* *pdk* *rab* *stm* 56 | */huge/* */large* */medium/* */mos6502* */sm83* */small-stack-auto/* 57 | */ucsim*.exe */include/mcs51/* 58 | */bin/makebin.exe */bin/packihx.exe */bin/s51.exe */bin/savr.exe 59 | */bin/sdcdb* */bin/sdnm.exe */bin/sdobjcopy.exe 60 | */bin/sdranlib.exe */bin/sm*.exe */bin/sp*.exe */bin/sr*.exe */bin/ss*.exe */bin/sx*.exe 61 | ' 62 | 63 | TMP=$(mktemp -d sdcc-repack-XXXXXX) 64 | 65 | echo "Unpacking into $TMP..." 66 | if [ $TARFLAG == zip ]; then 67 | # unzip "$FILE" -d "$TMP" $ZIP_EXCLUDE 68 | cd "$TMP" 69 | unzip "$FILE" $ZIP_EXCLUDE 70 | cd - 71 | else 72 | tar x${VERBOSE}${TARFLAG}f "$FILE" -C "$TMP" $TAR_EXCLUDE 73 | fi 74 | 75 | # always repack into a tar.bz2 file, even if was a zip before. 76 | echo "Repacking into file $NAME" 77 | #Stop OS X tar from including hidden ._ files in archives 78 | export COPYFILE_DISABLE=1; 79 | tar -jc${VERBOSE} --exclude="*DS_Store" -f "$NAME.tar.bz2" -C "$TMP" sdcc 80 | 81 | echo "cleaning up temporary files" 82 | rm -rf "$TMP" 83 | 84 | echo "done." 85 | -------------------------------------------------------------------------------- /ch55xduino/filePacker/packCurrentVersion.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #prepared for mac 4 | 5 | COREVERSION=0.0.11 6 | SDCCVERSION=13407 7 | TOOLSVERSION=2022.04.07 8 | COREFILE=ch55xduino-core-$COREVERSION.tar.bz2 9 | 10 | #pack core 11 | 12 | #Stop OS X tar from including hidden ._ files in archives 13 | export COPYFILE_DISABLE=1; 14 | 15 | tar -jcv --exclude="*DS_Store" -f packedFiles/$COREFILE -C ../ ch55x 16 | 17 | ./gen_platform_entry.sh packedFiles/$COREFILE $COREVERSION $SDCCVERSION $TOOLSVERSION > packedFiles/platform_$COREVERSION.txt 18 | 19 | #pack tools 20 | TOOLFILE_MAC=ch55xduino-tools_macosx-$TOOLSVERSION.tar.bz2 21 | tar -jcv --exclude="*DS_Store" -f packedFiles/$TOOLFILE_MAC -C ../ tools/wrapper tools/macosx 22 | TOOLFILE_LINUX=ch55xduino-tools_linux64-$TOOLSVERSION.tar.bz2 23 | tar -jcv --exclude="*DS_Store" -f packedFiles/$TOOLFILE_LINUX -C ../ tools/wrapper tools/linux 24 | TOOLFILE_WIN=ch55xduino-tools_mingw32-$TOOLSVERSION.tar.bz2 25 | tar -jcv --exclude="*DS_Store" -f packedFiles/$TOOLFILE_WIN -C ../ tools/wrapper tools/win 26 | ./gen_tools_entry.sh packedFiles/ch55xduino-tools $TOOLSVERSION $COREVERSION > packedFiles/tools_$TOOLSVERSION.txt 27 | 28 | ./gen_tools_entry.sh sdcc $SDCCVERSION $COREVERSION > packedFiles/sdcc_$SDCCVERSION.txt 29 | -------------------------------------------------------------------------------- /ch55xduino/filePacker/packedFiles/.gitignore: -------------------------------------------------------------------------------- 1 | *.tar.bz2 2 | *.txt 3 | 4 | -------------------------------------------------------------------------------- /ch55xduino/tools/linux/99-ch55xbl.rules: -------------------------------------------------------------------------------- 1 | # CH55x bootloader 2 | # copy to /etc/udev/rules.d/ 3 | 4 | SUBSYSTEM=="usb", ATTRS{idVendor}=="4348", ATTRS{idProduct}=="55e0", MODE="0666" 5 | -------------------------------------------------------------------------------- /ch55xduino/tools/linux/vnproch55x: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/linux/vnproch55x -------------------------------------------------------------------------------- /ch55xduino/tools/macosx/vnproch55x: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/macosx/vnproch55x -------------------------------------------------------------------------------- /ch55xduino/tools/win/CH375DLL.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/CH375DLL.dll -------------------------------------------------------------------------------- /ch55xduino/tools/win/busybox.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/busybox.exe -------------------------------------------------------------------------------- /ch55xduino/tools/win/libgcc_s_sjlj-1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/libgcc_s_sjlj-1.dll -------------------------------------------------------------------------------- /ch55xduino/tools/win/libusb-1.0.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/libusb-1.0.dll -------------------------------------------------------------------------------- /ch55xduino/tools/win/libusbK.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/libusbK.dll -------------------------------------------------------------------------------- /ch55xduino/tools/win/vnproch55x.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/ch55xduino/tools/win/vnproch55x.exe -------------------------------------------------------------------------------- /ch55xduino/tools/wrapper/sdar.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | VERBOSE=0 4 | USE_COLOR=0 5 | if [ "$1" == "-v" ]; then 6 | VERBOSE=1; 7 | shift 8 | elif [ "$1" == "-vv" ]; then 9 | VERBOSE=2 10 | # USE_COLOR=1 11 | set -x 12 | shift 13 | fi 14 | 15 | 16 | if [ $USE_COLOR -gt 0 ]; then 17 | # ANSI color codes to beautify the output: 18 | BLACK='\033[0;30m' 19 | RED='\033[0;31m' 20 | GREEN='\033[0;32m' 21 | ORANGE='\033[0;33m' 22 | BLUE='\033[0;34m' 23 | PURPLE='\033[0;35m' 24 | CYAN='\033[0;36m' 25 | LGRAY='\033[0;37m' 26 | DGRAY='\033[1;30m' 27 | LRED='\033[1;31m' 28 | LGREEN='\033[1;32m' 29 | YELLOW='\033[1;33m' 30 | LBLUE='\033[1;34m' 31 | LPURPLE='\033[1;35m' 32 | LCYAN='\033[1;36m' 33 | WHITE='\033[1;37m' 34 | OFF='\033[0m' 35 | fi 36 | 37 | # check if cp is in the path using 'command -v' (a builtin POSIX function) 38 | if ! command -v cp > /dev/null; then 39 | # Ok, this means we are on a Windows system and we have to find a 40 | # way to access cp and rm in ../win. A simple 'cd ../win' or 41 | # '../win/cp' does't work, as the current working directory is still 42 | # the Arduino binary directory. 43 | # 44 | # This looks ok, but it doesn't work on some Windows systems: 45 | # (No idea why) 46 | # PATH="${0%/wrapper/*}"/win:$PATH 47 | # 48 | # This is technically wrong, but surprisingly it works with Windows: 49 | # cd $0/../.. 50 | # PATH=$(pwd)/win:$PATH 51 | # 52 | # Use cd/pwd as a replacement for 'realpath' using only builtins. 53 | # It has the positive side effect of converting from Windows to Unix 54 | # path syntax avoiding all these backslash issues. 55 | cd "${0%/wrapper/*}" 56 | PATH=$(pwd)/win:$PATH 57 | fi 58 | 59 | # echo the full command line in cyan: 60 | if [ $VERBOSE -gt 0 ]; then 61 | >&2 echo -ne "${CYAN}" 62 | >&2 echo -n "${@}" 63 | >&2 echo -e "${OFF}" 64 | fi 65 | 66 | # echo the mark id in green and the compiler call in white: 67 | SDAR=$1 68 | LIB=$2 69 | OBJ=${3%.o}.rel 70 | MARK=$4 71 | shift 4 72 | 73 | if [ $VERBOSE -gt 0 ]; then 74 | >&2 echo -ne "${GREEN}Mark $MARK:${OFF}" 75 | >&2 echo "$SDAR" "$@" "$LIB" "$OBJ" 76 | fi 77 | 78 | "$SDAR" "$@" "$LIB" "$OBJ" 79 | ERR=$? 80 | cp -a "$LIB" "${LIB%.a}.lib" 81 | 82 | # propagate the sdar exit code 83 | exit $ERR 84 | -------------------------------------------------------------------------------- /ch55xduino/tools/wrapper/sdsize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # works on bash, dash or busybox ash 3 | 4 | print_usage () 5 | { 6 | cat << EOF 7 | Print the memory usage of a binary compiled by SDCC 8 | 9 | usage: $0 mapfile 10 | 11 | The display format mimics the output of the size tool. The values are 12 | calculated from the segment definitions in the map file as follows: 13 | 14 | text (code in flash) 15 | - CODE: program code 16 | - GSINIT: code to copy the globaly initialized data 17 | - GSFINAL: code after the initialization is done 18 | 19 | data (initialized data in flash) 20 | - HOME: Interrupt vectors etc. 21 | - CONST: Constants (tables) 22 | - INITIALIZER: Content of initialized variables (to be copied to 23 | INITIALIZED) 24 | 25 | bss (uninitialized, zeroed and initialized, total RAM usage) 26 | - DATA: uninitialized 27 | - INITIALIZED: initialized, copied from INITIALIZER 28 | 29 | dec (total flash usage in decimal) 30 | - text+data 31 | 32 | hex (total flash usage in hex) 33 | - text+data 34 | 35 | filename 36 | - the filename of the map file 37 | 38 | LGPL-2.1, (c) 2018 M. Mayer 39 | EOF 40 | exit 1 41 | } 42 | 43 | 44 | VERBOSE=0 45 | 46 | while getopts ":hv" opt; do 47 | case "$opt" in 48 | v) 49 | echo "verbose!" 50 | VERBOSE=1 51 | ;; 52 | *) 53 | print_usage 54 | ;; 55 | esac 56 | done 57 | shift $((OPTIND-1)) 58 | 59 | if [ $# -lt 1 ]; then 60 | print_usage; 61 | fi 62 | 63 | FILENAME="$1" 64 | 65 | awk -v filename="$FILENAME" -v verbose=$VERBOSE ' 66 | /^[A-Z]+ / { 67 | if (verbose) print $1, $3, $5; size[$1]=$5; 68 | } 69 | END { 70 | text = size["CODE"]+size["GSINIT"]+size["GSFINAL"]; 71 | data = size["HOME"]+size["CONST"]+size["INITIALIZER"]; 72 | bss = size["DATA"]+size["INITIALIZED"]; 73 | print " text\t data\t bss\t dec\t hex\tfilename"; 74 | # print text, data, bss; 75 | printf "%7d\t%7d\t%7d\t%7d\t%7x\t%s\n", text, data, bss, text+data, text+data, filename; 76 | }' "$FILENAME" 77 | -------------------------------------------------------------------------------- /docs/Zadig_CDC.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/Zadig_CDC.png -------------------------------------------------------------------------------- /docs/Zadig_bootloader.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/Zadig_bootloader.png -------------------------------------------------------------------------------- /docs/Zadig_bootloader_libusb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/Zadig_bootloader_libusb.png -------------------------------------------------------------------------------- /docs/blinkLED.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/blinkLED.gif -------------------------------------------------------------------------------- /docs/convertJsonGitee.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import re 3 | 4 | #GITEE doesn't allow direct fetch of webpage form python, using API is not necessary. 5 | #we extract from brower's console 6 | 7 | bz2OnGitee = ("https://gitee.com/deqings/ch55xduino/attach_files/491764/download/ch55xduino-tools_macosx-2020.10.05.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491765/download/ch55xduino-tools_mingw32-2020.10.05.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491766/download/ch55xduino-core-0.0.6.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491757/download/sdcc-mcs51-i586-mingw32msvc-20200526-11630.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491758/download/ch55xduino-tools_mingw32-2020.05.31.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491759/download/ch55xduino-tools_macosx-2020.05.31.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491760/download/ch55xduino-core-0.0.1.tar.bz2","https://gitee.com/deqings/ch55xduino/attach_files/491761/download/sdcc-mcs51-x86_64-apple-macosx-20200526-11630.tar.bz2",) 8 | 9 | #print(bz2OnGitee) 10 | 11 | originalJSON = open("../package_ch55xduino_mcs51_index.json","r") 12 | jsonLines = originalJSON.readlines() 13 | originalJSON.close() 14 | 15 | for i in range(len(jsonLines)): 16 | line = jsonLines[i] 17 | bzFileMatch = (re.search('\"(http.+bz2)\"',line)) 18 | if (bzFileMatch): 19 | originalBz2URL = bzFileMatch.group(1) 20 | #print(originalBz2URL) 21 | releaseVerMatch = (re.search('download/(.+)/(.+\.bz2)',line)) 22 | if releaseVerMatch==None: break; 23 | releaseVer = releaseVerMatch.group(1) 24 | bz2File = releaseVerMatch.group(2) 25 | bzInGitee = False 26 | for bzStr in bz2OnGitee: 27 | if (bz2File in bzStr): 28 | bzInGitee = True 29 | #print(" "+bz2File + " in " + bzStr) 30 | jsonLines[i] = line.replace(originalBz2URL,bzStr) 31 | break 32 | 33 | 34 | for line in jsonLines: 35 | pass 36 | # print(line) 37 | 38 | with open('package_ch55xduino_mcs51_index_gitee.json', 'w') as the_file: 39 | for line in jsonLines: 40 | the_file.write(line) 41 | -------------------------------------------------------------------------------- /docs/fetchBz2FromGiteeRelease.txt: -------------------------------------------------------------------------------- 1 | var urls = document.getElementsByTagName('a'); 2 | pythonBz2 = "bz2OnGitee = (" 3 | for (url in urls) { 4 | oneURL = urls[url].href; 5 | if ( (typeof oneURL === 'string' || oneURL instanceof String) && oneURL.endsWith('.bz2')){ 6 | pythonBz2 = pythonBz2 + "\""+oneURL+"\"," 7 | } 8 | } 9 | pythonBz2 = pythonBz2 + ")" 10 | console.log (pythonBz2); -------------------------------------------------------------------------------- /docs/simpleCH552Back.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/simpleCH552Back.jpg -------------------------------------------------------------------------------- /docs/simpleCH552Front.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/docs/simpleCH552Front.jpg -------------------------------------------------------------------------------- /pcb/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore list for Eagle, a PCB layout tool 2 | 3 | # Backup files 4 | *.s#? 5 | *.b#? 6 | *.l#? 7 | *.b$? 8 | *.s$? 9 | *.l$? 10 | 11 | # Eagle project file 12 | # It contains a serial number and references to the file structure 13 | # on your computer. 14 | # comment the following line if you want to have your project file included. 15 | eagle.epf 16 | 17 | # Autorouter files 18 | *.pro 19 | *.job 20 | 21 | # CAM files 22 | *.$$$ 23 | *.cmp 24 | *.ly2 25 | *.l15 26 | *.sol 27 | *.plc 28 | *.stc 29 | *.sts 30 | *.crc 31 | *.crs 32 | 33 | *.dri 34 | *.drl 35 | *.gpi 36 | *.pls 37 | *.ger 38 | *.xln 39 | 40 | *.drd 41 | *.drd.* 42 | 43 | *.s#* 44 | *.b#* 45 | 46 | *.info 47 | 48 | *.eps 49 | 50 | # file locks introduced since 7.x 51 | *.lck 52 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore list for Eagle, a PCB layout tool 2 | 3 | # Backup files 4 | *.s#? 5 | *.b#? 6 | *.l#? 7 | *.b$? 8 | *.s$? 9 | *.l$? 10 | 11 | # Eagle project file 12 | # It contains a serial number and references to the file structure 13 | # on your computer. 14 | # comment the following line if you want to have your project file included. 15 | eagle.epf 16 | 17 | # Autorouter files 18 | *.pro 19 | *.job 20 | 21 | # CAM files 22 | *.$$$ 23 | *.cmp 24 | *.ly2 25 | *.l15 26 | *.sol 27 | *.plc 28 | *.stc 29 | *.sts 30 | *.crc 31 | *.crs 32 | 33 | *.dri 34 | *.drl 35 | *.gpi 36 | *.pls 37 | *.ger 38 | *.xln 39 | 40 | *.drd 41 | *.drd.* 42 | 43 | *.s#* 44 | *.b#* 45 | 46 | *.info 47 | 48 | *.eps 49 | 50 | # file locks introduced since 7.x 51 | *.lck 52 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/README.md: -------------------------------------------------------------------------------- 1 | # ch55xRebootTool 2 | 3 | ![ch55xRebootTool photo](https://raw.githubusercontent.com/DeqingSun/ch55xduino/playground/pcb/ch55xRebootTool/ch55xRebootTool.jpg) 4 | 5 | ch55xRebootTool is a hardware tool to reset a WCH chip target into the bootloader without touching the board with your finger. It solves 2 problems: 6 | 7 | 1. Some chips can not jump to bootloader from user code. 8 | 9 | 2. When you are developing the USB host code, the USB port will be occupied by a device. 10 | 11 | ## How to use the tool 12 | 13 | ### Basic usage 14 | 15 | Connect 2 micro USB cables from the computer to the 2 micro USB ports on the left. The upper one is connected to the target, and the lower one is to send commands to the controller on board. 16 | 17 | The left MicroUSB port and the USB A port next to it are connected. The target can be plugged into either port. 18 | 19 | If your target chip needs to pull down a pin to enter the bootloader, connect that pin to the P1.6 on the board. If the target uses D+, no connection is needed. 20 | 21 | Open the ch55xRebootTool serial port @ 1200 rate and it will kick target into the bootloader. 22 | 23 | ### Use the serial adapter 24 | 25 | This board can also be used as a serial adapter. Just use the 3-pin header on the right. Note the board can not work at the 1200 rate or higher than 115200. 26 | 27 | ### Use a USB device for host code development 28 | 29 | Plug the device on the USB port on the right side of the top. When the P1.7 on the board gets pulled down. The board will supply power to the device, and switch the target from the computer to the device. You may use a pin on the target to do the control 30 | 31 | Note the target will switch back to the computer in bootloader mode. 32 | 33 | ## Enter the bootloader 34 | 35 | Most WCH chips can upload firmware via USB. However, the chip must enter the bootloader to upload the firmware. Generally, there are 2 ways to enter bootloader: 36 | 37 | 1. Pull a specific pin to GND or D+ to VCC, depending on your settings in the configuration byte. Then power the chip. 38 | 39 | 2. Jump into the bootloader within the user code. But this does not work for chips such as CH549. 40 | 41 | If you already played with the CH55xduino on a CH55x chip, you may not feel the pain. Because when you upload the code, the Arduino environment will open the virtual serial port on the CH55x chip at a 1200 baud rate. The CH55xduino firmware will jump to the bootloader automatically and receive the code from the computer. The user experience is just pressing a button on your computer and everything gets ready. 42 | 43 | But if the firmware is not working properly or you are using chips like CH549, the chip won't enter the bootloader automatically. You may need to unplug the USB cable, hold a button and plug the USB cable back, that is a pain when you repeat the process over and over again. 44 | 45 | Instead of doing it manually, the ch55xRebootTool will act as a serial adapter. When you open the serial port at a 1200 baud rate, the tool will disconnect the power from the target chip using a PMOS, connect a 10K resistor to D+, and pulldown the pin P1.6, before restoring the power cycle. Basically, the ch55xRebootTool does the power cycle and pulls the pin for you. 46 | 47 | ## Use a USB device 48 | 49 | When developing the host code, you may need to switch the target back and forth between the computer and the device. This tool does it automatically for you. You just need to pull the Pin 1.7 low to connect the device. When it is released, the target will be connected back to the computer. 50 | 51 | When I design the board. I wish the target can send a reset command on the USB bus, and the controller knows when the target left the bootloader, and can switch to the device automatically. But I have not got it working so far. 52 | 53 | 54 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/RebootToolTest.ino: -------------------------------------------------------------------------------- 1 | #ifndef USER_USB_RAM 2 | #error "This example needs to be compiled with a USER USB setting" 3 | #endif 4 | 5 | #include "src/userUsbCdc/USBCDC.h" 6 | extern __xdata uint8_t LineCoding[]; //lineCoding of CDC is located in this array 7 | extern __xdata uint8_t controlLineState; 8 | __xdata uint32_t oldBaudRate = 9600; 9 | __xdata uint8_t oldLineState = 0; 10 | 11 | #define USB_SWITCH_PIN 33 12 | #define POWER_TARGET_PIN 34 13 | #define USBDP_PULLUP_TARGET_PIN 14 14 | #define BOOTLOADER_PULLDOWN_TARGET_PIN 16 15 | 16 | #define SENSE_TARGET_SWTICH_PERF 17 17 | #define POWER_PERF_PIN 11 18 | 19 | void bootloaderRoutine() { 20 | USB_CTRL = 0; 21 | EA = 0; //Disabling all interrupts is required. 22 | delayMicroseconds(50000); 23 | delayMicroseconds(50000); 24 | __asm__ ("lcall #0x3800"); //Jump to bootloader code 25 | while (1); 26 | } 27 | 28 | void setup() { 29 | USBInit(); 30 | 31 | pinMode(USB_SWITCH_PIN, OUTPUT); 32 | digitalWrite(USB_SWITCH_PIN, LOW); //connect target to computer USB 33 | 34 | pinMode(POWER_TARGET_PIN, OUTPUT); 35 | digitalWrite(POWER_TARGET_PIN, LOW); 36 | 37 | pinMode(USBDP_PULLUP_TARGET_PIN, OUTPUT); 38 | digitalWrite(USBDP_PULLUP_TARGET_PIN, HIGH); 39 | 40 | pinMode(BOOTLOADER_PULLDOWN_TARGET_PIN, INPUT); 41 | 42 | pinMode(SENSE_TARGET_SWTICH_PERF, INPUT_PULLUP); 43 | 44 | pinMode(POWER_PERF_PIN, OUTPUT); 45 | digitalWrite(POWER_PERF_PIN, HIGH); 46 | 47 | Serial0_begin(9600); 48 | 49 | } 50 | 51 | void loop() { 52 | __xdata uint32_t currentBaudRate = *((__xdata uint32_t *)LineCoding); //both linecoding and sdcc are little-endian 53 | __xdata uint8_t currentLineState = controlLineState; //both linecoding and sdcc are little-endian 54 | 55 | while (USBSerial_available()) { 56 | char serialChar = USBSerial_read(); 57 | Serial0_write(serialChar); 58 | } 59 | Serial0_flush(); 60 | 61 | while (Serial0_available()) { 62 | char serialChar = Serial0_read(); 63 | USBSerial_write(serialChar); 64 | } 65 | USBSerial_flush(); 66 | 67 | if (oldBaudRate != currentBaudRate) { 68 | oldBaudRate = currentBaudRate; 69 | Serial0_begin(currentBaudRate); 70 | } 71 | 72 | if (oldLineState != currentLineState) { 73 | if ( ((currentLineState & 0x01) == 0) && ((oldLineState & 0x01) == 1)) { 74 | if (currentBaudRate == 1200) { 75 | digitalWrite(POWER_TARGET_PIN, HIGH); //Cut target power 76 | digitalWrite(POWER_PERF_PIN, HIGH); //Cut peripheral power 77 | digitalWrite(USB_SWITCH_PIN, LOW); //connect target to computer USB 78 | delay(100); 79 | digitalWrite(USBDP_PULLUP_TARGET_PIN, LOW); //Connect pull up resistor to target 80 | digitalWrite(BOOTLOADER_PULLDOWN_TARGET_PIN, LOW); //if the bootloader require a pin to be pulled low, use this pin 81 | pinMode(BOOTLOADER_PULLDOWN_TARGET_PIN, OUTPUT); 82 | delay(10); 83 | digitalWrite(POWER_TARGET_PIN, LOW); //Restore target power 84 | delay(50); 85 | digitalWrite(USBDP_PULLUP_TARGET_PIN, HIGH); //Disconnect pull up resistor to target 86 | pinMode(BOOTLOADER_PULLDOWN_TARGET_PIN, INPUT); 87 | } 88 | } 89 | oldLineState = currentLineState; 90 | } 91 | 92 | if (digitalRead(15) == LOW) { 93 | delay(10); 94 | if (digitalRead(15) == LOW) { 95 | bootloaderRoutine(); 96 | } 97 | } 98 | 99 | if (digitalRead(SENSE_TARGET_SWTICH_PERF)) { 100 | digitalWrite(POWER_PERF_PIN, HIGH); //Cut peripheral power 101 | digitalWrite(USB_SWITCH_PIN, LOW); //connect target to computer USB 102 | } else { 103 | digitalWrite(POWER_PERF_PIN, LOW); //Set peripheral power 104 | digitalWrite(USB_SWITCH_PIN, HIGH); //connect target to peripheral USB 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/reboot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import serial 4 | import serial.tools.list_ports 5 | from time import sleep 6 | 7 | comlist = serial.tools.list_ports.comports() 8 | ch55xRebootToolDevice = None 9 | for element in comlist: 10 | if (element.vid == 0x1209 and element.pid == 0xC550): 11 | if (element.product == "CH55xReboot"): 12 | ch55xRebootToolDevice = element.device 13 | 14 | if (ch55xRebootToolDevice!=None): 15 | print("ch55xRebootTool Found on: " + ch55xRebootToolDevice) 16 | ch55xRebootToolSerial = serial.Serial(ch55xRebootToolDevice,baudrate=1200,timeout=0.01,rtscts=1) 17 | sleep(0.1) 18 | ch55xRebootToolSerial.close() 19 | #sleep(1) 20 | else: 21 | print("ch55xRebootTool not found.") 22 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/src/userUsbCdc/USBCDC.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_CDC_H__ 2 | #define __USB_CDC_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | void USBInit(void); 13 | 14 | #ifdef __cplusplus 15 | } // extern "C" 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/src/userUsbCdc/USBconstant.c: -------------------------------------------------------------------------------- 1 | #include "USBconstant.h" 2 | 3 | 4 | //Device descriptor 5 | __code uint8_t DevDesc[] = { 6 | 0x12,0x01, 7 | 0x10,0x01, //USB spec release number in BCD format, USB1.1 (0x10, 0x01). 8 | 0xEF,0x02,0x01, //bDeviceClass, bDeviceSubClass, bDeviceProtocol 9 | DEFAULT_ENDP0_SIZE, //bNumConfigurations 10 | 0x09,0x12,0x50,0xC5, // VID PID 11 | 0x01,0x01, //version 12 | 0x01,0x02,0x03, //bString 13 | 0x01 //bNumConfigurations 14 | }; 15 | 16 | __code uint16_t DevDescLen = sizeof(DevDesc); 17 | 18 | __code uint8_t CfgDesc[] ={ 19 | 0x09,0x02,sizeof(CfgDesc) & 0xff,sizeof(CfgDesc) >> 8, 20 | 0x02,0x01,0x00,0x80,0x64, //Configuration descriptor (2 interfaces) 21 | // Interface Association Descriptor, IAD, this packes following 2 interfaces into 1 22 | 0x08,0x0B,0x00,0x02,0x02,0x02,0x01,0x04, 23 | // Interface 1 (CDC) descriptor 24 | 0x09,0x04,0x00,0x00,0x01,0x02,0x02,0x01,0x04, // CDC control description, 1 endpoint 25 | // Functional Descriptor refer to usbcdc11.pdf 26 | 0x05,0x24,0x00,0x10,0x01, //Header Functional Descriptor 27 | 0x05,0x24,0x01,0x00,0x00, //Call Management Functional Descriptor 28 | 0x04,0x24,0x02,0x02, //Direct Line Management Functional Descriptor, Support: Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, Serial_State 29 | 0x05,0x24,0x06,0x00,0x01, //Union Functional Descriptor, Communication class interface 0, Data Class Interface 1 30 | 0x07,0x05,0x81,0x03,0x08,0x00,0x40, //EndPoint descriptor (CDC Upload, Interrupt) 31 | // Interface 2 (Data Interface) descriptor 32 | 0x09,0x04,0x01,0x00,0x02,0x0a,0x00,0x00,0x04, //Data Class Interface descriptor 33 | 0x07,0x05,0x02,0x02,0x40,0x00,0x00, //endpoint descriptor 34 | 0x07,0x05,0x82,0x02,0x40,0x00,0x00, //endpoint descriptor 35 | 36 | }; 37 | 38 | __code uint16_t CfgDescLen = sizeof(CfgDesc); 39 | 40 | //String Descriptors 41 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor 42 | __code uint16_t LangDesLen = sizeof(LangDes); 43 | __code uint8_t SerDes[]={ //Serial String Descriptor 44 | 0x0E,0x03, 45 | 'C',0x00,'H',0x00,'_',0x00,'R',0x00,'b',0x00,'t',0x00 46 | }; 47 | __code uint16_t SerDesLen = sizeof(SerDes); 48 | __code uint8_t Prod_Des[]={ //Produce String Descriptor 49 | 0x18,0x03, 50 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,'R',0x00, 51 | 'e',0x00,'b',0x00,'o',0x00,'o',0x00,'t',0x00 52 | }; 53 | __code uint16_t Prod_DesLen = sizeof(Prod_Des); 54 | 55 | __code uint8_t CDC_Des[]={ 56 | 0x16,0x03, 57 | 'C',0x00,'D',0x00,'C',0x00,' ',0x00,'S',0x00,'e',0x00, 58 | 'r',0x00,'i',0x00,'a',0x00,'l',0x00 59 | }; 60 | __code uint16_t CDC_DesLen = sizeof(CDC_Des); 61 | 62 | __code uint8_t Manuf_Des[]={ 63 | 0x0E,0x03, 64 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00, 65 | }; 66 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des); 67 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/src/userUsbCdc/USBconstant.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONST_DATA_H__ 2 | #define __CONST_DATA_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | 8 | #define EP0_ADDR 0 9 | #define EP1_ADDR 10 10 | #define EP2_ADDR 20 11 | 12 | #define SET_LINE_CODING 0X20 // Configures DTE rate, stop-bits, parity, and number-of-character 13 | #define GET_LINE_CODING 0X21 // This request allows the host to find out the currently configured line coding. 14 | #define SET_CONTROL_LINE_STATE 0X22 // This request generates RS-232/V.24 style control signals. 15 | 16 | 17 | extern __code uint8_t DevDesc[]; 18 | extern __code uint8_t CfgDesc[]; 19 | extern __code uint8_t LangDes[]; 20 | extern __code uint8_t SerDes[]; 21 | extern __code uint8_t Prod_Des[]; 22 | extern __code uint8_t CDC_Des[]; 23 | extern __code uint8_t WEBUSB_Des[]; 24 | extern __code uint8_t Manuf_Des[]; 25 | 26 | 27 | 28 | extern __code uint16_t DevDescLen; 29 | extern __code uint16_t CfgDescLen; 30 | extern __code uint16_t LangDesLen; 31 | extern __code uint16_t SerDesLen; 32 | extern __code uint16_t Prod_DesLen; 33 | extern __code uint16_t CDC_DesLen; 34 | extern __code uint16_t Manuf_DesLen; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/RebootToolTest/src/userUsbCdc/USBhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_HANDLER_H__ 2 | #define __USB_HANDLER_H__ 3 | 4 | #include 5 | #include "include/ch5xx.h" 6 | #include "include/ch5xx_usb.h" 7 | #include "USBconstant.h" 8 | 9 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[]; 10 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[]; 11 | extern __xdata __at (EP2_ADDR) uint8_t Ep2Buffer[]; 12 | 13 | extern uint16_t SetupLen; 14 | extern uint8_t SetupReq,UsbConfig; 15 | extern const __code uint8_t *pDescr; 16 | 17 | 18 | #define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) 19 | 20 | // Out 21 | #define EP0_OUT_Callback USB_EP0_OUT 22 | #define EP1_OUT_Callback NOP_Process 23 | #define EP2_OUT_Callback USB_EP2_OUT 24 | #define EP3_OUT_Callback NOP_Process 25 | #define EP4_OUT_Callback NOP_Process 26 | 27 | // SOF 28 | #define EP0_SOF_Callback NOP_Process 29 | #define EP1_SOF_Callback NOP_Process 30 | #define EP2_SOF_Callback NOP_Process 31 | #define EP3_SOF_Callback NOP_Process 32 | #define EP4_SOF_Callback NOP_Process 33 | 34 | // IN 35 | #define EP0_IN_Callback USB_EP0_IN 36 | #define EP1_IN_Callback USB_EP1_IN 37 | #define EP2_IN_Callback USB_EP2_IN 38 | #define EP3_IN_Callback NOP_Process 39 | #define EP4_IN_Callback NOP_Process 40 | 41 | // SETUP 42 | #define EP0_SETUP_Callback USB_EP0_SETUP 43 | #define EP1_SETUP_Callback NOP_Process 44 | #define EP2_SETUP_Callback NOP_Process 45 | #define EP3_SETUP_Callback NOP_Process 46 | #define EP4_SETUP_Callback NOP_Process 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | void USBInterrupt(void); 53 | void USBDeviceCfg(); 54 | void USBDeviceIntCfg(); 55 | void USBDeviceEndPointCfg(); 56 | 57 | #ifdef __cplusplus 58 | } // extern "C" 59 | #endif 60 | 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/ch55xRebootTool.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/pcb/ch55xRebootTool/ch55xRebootTool.jpg -------------------------------------------------------------------------------- /pcb/ch55xRebootTool/ch55xRebootTool.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/pcb/ch55xRebootTool/ch55xRebootTool.pdf -------------------------------------------------------------------------------- /pcb/schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeActStudio/ch55xduino/16816c693f5158f09bc7d0412b1a0f7bface3bff/pcb/schematic.png --------------------------------------------------------------------------------