├── .gitignore
├── LICENSE
├── README.md
├── bootloaderWebtool
├── ch55xbl.js
└── index.html
├── ch55xduino
├── ch55x
│ ├── boards.txt
│ ├── cores
│ │ └── ch55xduino
│ │ │ ├── Arduino.h
│ │ │ ├── HardwareSerial.h
│ │ │ ├── HardwareSerial0.c
│ │ │ ├── HardwareSerial0ISR.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
│ │ │ ├── include
│ │ │ ├── ch554.h
│ │ │ ├── ch554_datatypes.h
│ │ │ └── ch554_usb.h
│ │ │ ├── main.c
│ │ │ ├── weakOverride.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
│ │ │ │ └── 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
│ │ │ │ └── NoUsbExample
│ │ │ │ └── NoUsbExample.ino
│ │ ├── SPI
│ │ │ ├── examples
│ │ │ │ └── ADXL345SPI
│ │ │ │ │ └── ADXL345SPI.ino
│ │ │ ├── keywords.txt
│ │ │ ├── library.properties
│ │ │ └── src
│ │ │ │ ├── SPI.c
│ │ │ │ └── SPI.h
│ │ └── TouchKey
│ │ │ ├── examples
│ │ │ ├── TouchKeyExample
│ │ │ │ └── TouchKeyExample.ino
│ │ │ └── TouchKeyTuneParameter
│ │ │ │ └── TouchKeyTuneParameter.ino
│ │ │ ├── keywords.txt
│ │ │ ├── library.properties
│ │ │ └── src
│ │ │ ├── TouchKey.c
│ │ │ └── TouchKey.h
│ ├── platform.txt
│ └── variants
│ │ └── ch552
│ │ ├── pins_arduino.h
│ │ └── pins_arduino_include.h
└── tools
│ ├── macosx
│ └── vnproch55x
│ ├── win
│ └── libusb-1.0.dll
│ └── wrapper
│ ├── sdar.sh
│ ├── sdcc-link.sh
│ ├── sdcc.sh
│ └── sdsize.sh
├── docs
├── Zadig_bootloader.png
├── blinkLED.gif
├── simpleCH552Back.jpg
└── simpleCH552Front.jpg
├── package_ch55xduino_mcs51_index.json
└── pcb
├── .gitignore
├── 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/boards.txt:
--------------------------------------------------------------------------------
1 | # See: http://code.google.com/p/arduino/wiki/Platforms
2 |
3 | menu.usb_settings=USB Settings
4 | menu.upload_method=Upload method
5 | ##############################################################
6 |
7 | ch552.name=CH552 Board
8 | ch552.upload.protocol=ch55x2_3_1
9 | #ch552.upload.mcu=ch552
10 | #CH552 has 16K flash, but 2K is for bootloader, 14K usable
11 | ch552.upload.maximum_size=14336
12 | ch552.build.mcu=mcs51
13 | ch552.build.f_cpu=24000000L
14 | ch552.build.board=ch55x
15 | ch552.build.core=ch55xduino
16 | ch552.build.variant=ch552
17 |
18 | ch552.upload.use_1200bps_touch=true
19 | ch552.upload.wait_for_upload_port=false
20 |
21 | ## USB Memory Settings
22 | ## ----------------------------------------------
23 | ch552.menu.usb_settings.usbcdc=Default CDC
24 | ch552.menu.usb_settings.usbcdc.upload.maximum_data_size=876
25 | ch552.menu.usb_settings.usbcdc.upload.xdata_location=148
26 | ch552.menu.usb_settings.usbcdc.build.extra_flags=--model-small -DEP0_ADDR=0 -DEP1_ADDR=10 -DEP2_ADDR=20
27 | ## ----
28 | ch552.menu.usb_settings.user148=USER CODE w/ 148B USB ram
29 | ch552.menu.usb_settings.user148.upload.maximum_data_size=876
30 | ch552.menu.usb_settings.user148.upload.xdata_location=148
31 | ch552.menu.usb_settings.user148.build.extra_flags=--model-small -DUSER_USB_RAM=148
32 | ## ----
33 | ch552.menu.usb_settings.user0=USER CODE w/ 0B USB ram
34 | ch552.menu.usb_settings.user0.upload.maximum_data_size=1024
35 | ch552.menu.usb_settings.user0.upload.xdata_location=0
36 | ch552.menu.usb_settings.user0.build.extra_flags=--model-small -DUSER_USB_RAM=0
37 |
38 | ## ----------------------------------------------
39 | ch552.menu.upload_method.usb=USB
40 | ch552.menu.upload_method.usb.upload.tool=vnproch55x_usb
41 | ## ----
42 | ch552.menu.upload_method.serial=Serial
43 | ch552.menu.upload_method.serial.upload.tool=vnproch55x_serial
44 |
45 | # meaningless variables just to keep the makefile happy
46 |
47 | ch552.upload.speed=1
48 |
49 | ##############################################################
50 |
51 | ch551.name=CH551 Board
52 | ch551.upload.tool=vnproch55x_usb
53 | ch551.upload.protocol=ch55x2_3_1
54 | #CH551 has 10K flash
55 | ch551.upload.maximum_size=10240
56 | ch551.build.mcu=mcs51
57 | ch551.build.f_cpu=24000000L
58 | ch551.build.board=ch55x
59 | ch551.build.core=ch55xduino
60 | ch551.build.variant=ch552
61 |
62 | ch551.upload.use_1200bps_touch=true
63 | ch551.upload.wait_for_upload_port=false
64 |
65 | ## USB Memory Settings
66 | ## ----------------------------------------------
67 | ch551.menu.usb_settings.usbcdc=Default CDC
68 | ch551.menu.usb_settings.usbcdc.upload.maximum_data_size=364
69 | ch551.menu.usb_settings.usbcdc.upload.xdata_location=148
70 | ch551.menu.usb_settings.usbcdc.build.extra_flags=--model-small -DEP0_ADDR=0 -DEP1_ADDR=10 -DEP2_ADDR=20
71 | ## ----
72 | ch551.menu.usb_settings.user148=USER CODE w/ 148B USB ram
73 | ch551.menu.usb_settings.user148.upload.maximum_data_size=364
74 | ch551.menu.usb_settings.user148.upload.xdata_location=148
75 | ch551.menu.usb_settings.user148.build.extra_flags=--model-small -DUSER_USB_RAM=148
76 | ## ----
77 | ch551.menu.usb_settings.user0=USER CODE w/ 0B USB ram
78 | ch551.menu.usb_settings.user0.upload.maximum_data_size=512
79 | ch551.menu.usb_settings.user0.upload.xdata_location=0
80 | ch551.menu.usb_settings.user0.build.extra_flags=--model-small -DUSER_USB_RAM=0
81 |
82 | # meaningless variables just to keep the makefile happy
83 |
84 | ch551.upload.speed=1
85 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/HardwareSerial.h:
--------------------------------------------------------------------------------
1 | #ifndef HardwareSerial_h
2 | #define HardwareSerial_h
3 |
4 | #include
5 | #include
6 | #include "include/ch554.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 | #endif
29 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/HardwareSerial0.c:
--------------------------------------------------------------------------------
1 | #include "HardwareSerial.h"
2 |
3 | __xdata unsigned char serial0Initialized;
4 |
5 | extern __xdata uint8_t Receive_Uart0_Buf[]; //arduino style serial buffer
6 | extern __xdata uint8_t Transmit_Uart0_Buf[]; //arduino style serial buffer
7 | extern volatile __xdata uint8_t uart0_rx_buffer_head;
8 | extern volatile __xdata uint8_t uart0_rx_buffer_tail;
9 | extern volatile __xdata uint8_t uart0_tx_buffer_head;
10 | extern volatile __xdata uint8_t uart0_tx_buffer_tail;
11 | extern volatile __xdata uint8_t uart0_flags;
12 |
13 | //extern wait functions
14 | void delayMicroseconds(uint16_t us);
15 |
16 | uint8_t Serial0(void){
17 | return serial0Initialized;
18 | }
19 |
20 | void Serial0_begin(unsigned long baud){
21 |
22 | __xdata volatile uint32_t x; //this is compiled in small memory model, variable won't stay in stack.
23 | __xdata volatile uint8_t x2;
24 |
25 | x = 10 * F_CPU / baud / 16; //Make sure it doesn't overflow when baudrate is changed, default 9600
26 | x2 = ((uint16_t)x) % 10;
27 | x /= 10;
28 | if ( x2 >= 5 ) x ++; //round
29 |
30 | SM0 = 0;
31 | SM1 = 1;
32 | SM2 = 0; //use mode 0 for serial 0
33 | //use Timer1 for baudrate generator
34 | RCLK = 0; //UART0 receive clk
35 | TCLK = 0; //UART0 send clk
36 | PCON |= SMOD;
37 |
38 | TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1; //0X20,Timer1 as 8 bit autoload timer
39 | T2MOD = T2MOD | bTMR_CLK | bT1_CLK; //Timer1 clk selection. trade off: bTMR_CLK is low, will make Uart0 not accurate. bTMR_CLK is high, make T2 timeout is short, multiple timeout must be allowed in 1st edge, as reset may take long
40 | TH1 = 0-x; //baud/12 is real rate
41 | TR1 = 1; //start timer1
42 | TI = 0;
43 | REN = 1; //Enable serial 0 receive
44 |
45 | ES = 1; //Enable serial 0 interrupt
46 |
47 | serial0Initialized = 1;
48 | }
49 |
50 | uint8_t Serial0_write(uint8_t SendDat)
51 | {
52 | if ((uart0_tx_buffer_head == uart0_tx_buffer_tail) && ( (uart0_flags & UART0_FLG_SENDING)==0)){ //start to send
53 | uart0_flags |= UART0_FLG_SENDING;
54 | SBUF = SendDat;
55 | return 1;
56 | }
57 |
58 | uint8_t nextHeadPos = ((uint8_t)(uart0_tx_buffer_head + 1)) % SERIAL0_TX_BUFFER_SIZE;
59 |
60 | uint16_t waitWriteCount=0;
61 | while ((nextHeadPos == uart0_tx_buffer_tail) ){ //wait max 100ms or discard
62 | waitWriteCount++;
63 | delayMicroseconds(5);
64 | if (waitWriteCount>=20000) return 0;
65 | }
66 | Transmit_Uart0_Buf[uart0_tx_buffer_head]=SendDat;
67 |
68 | uart0_tx_buffer_head = nextHeadPos;
69 |
70 | return 1;
71 | }
72 |
73 | void Serial0_flush(void){
74 | while( (uart0_flags & UART0_FLG_SENDING) );
75 | }
76 |
77 | uint8_t Serial0_available(void){
78 | uint8_t rxBufLength = ((uint8_t)(SERIAL0_RX_BUFFER_SIZE + uart0_rx_buffer_head - uart0_rx_buffer_tail)) % SERIAL0_RX_BUFFER_SIZE;
79 | return rxBufLength;
80 | }
81 |
82 | uint8_t Serial0_read(void){
83 | uint8_t rxBufLength = ((uint8_t)(SERIAL0_RX_BUFFER_SIZE + uart0_rx_buffer_head - uart0_rx_buffer_tail)) % SERIAL0_RX_BUFFER_SIZE;
84 | if(rxBufLength>0){
85 | uint8_t result = Receive_Uart0_Buf[uart0_rx_buffer_tail];
86 | uart0_rx_buffer_tail = (((uint8_t)(uart0_rx_buffer_tail + 1)) % SERIAL0_RX_BUFFER_SIZE);
87 | return result;
88 | }
89 | return 0;
90 | }
91 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/HardwareSerial0ISR.c:
--------------------------------------------------------------------------------
1 | #include "HardwareSerial.h"
2 |
3 | __xdata uint8_t Receive_Uart0_Buf[SERIAL0_RX_BUFFER_SIZE]; //arduino style serial buffer
4 | __xdata uint8_t Transmit_Uart0_Buf[SERIAL0_TX_BUFFER_SIZE]; //arduino style serial buffer
5 | volatile __xdata uint8_t uart0_rx_buffer_head=0;
6 | volatile __xdata uint8_t uart0_rx_buffer_tail=0;
7 | volatile __xdata uint8_t uart0_tx_buffer_head=0;
8 | volatile __xdata uint8_t uart0_tx_buffer_tail=0;
9 | volatile __xdata uint8_t uart0_flags=0;
10 |
11 | void uart0IntRxHandler(){
12 | uint8_t nextHead = (uart0_rx_buffer_head + 1) % SERIAL0_RX_BUFFER_SIZE;
13 |
14 | if (nextHead != uart0_rx_buffer_tail) {
15 | Receive_Uart0_Buf[uart0_rx_buffer_head] = SBUF;
16 | uart0_rx_buffer_head = nextHead;
17 | }
18 | }
19 |
20 | void uart0IntTxHandler(){
21 | if ((uart0_flags & UART0_FLG_SENDING)){
22 | if (uart0_tx_buffer_head == uart0_tx_buffer_tail){
23 | //do no more
24 | uart0_flags &= ~(UART0_FLG_SENDING);
25 | }else{
26 | SBUF=Transmit_Uart0_Buf[uart0_tx_buffer_tail];
27 | uart0_tx_buffer_tail = (uart0_tx_buffer_tail + 1) % SERIAL0_TX_BUFFER_SIZE;
28 | }
29 | }
30 | }
31 |
32 |
33 |
--------------------------------------------------------------------------------
/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 | */
27 |
28 |
29 | //#include
30 | //#include
31 | //#include
32 | #include
33 | #include "Arduino.h"
34 |
35 | #include "Print.h"
36 |
37 |
38 |
39 | void printNL(void);
40 |
41 | // Public Methods //////////////////////////////////////////////////////////////
42 |
43 | uint8_t Print_print_sn(writefunc_p writefunc, uint8_t * __xdata buffer, __xdata uint8_t size)
44 | {
45 | uint8_t n = 0;
46 | while (size--) {
47 | if (writefunc(*buffer++)) n++;
48 | else break;
49 | }
50 | return n;
51 | }
52 |
53 | uint8_t Print_print_s(writefunc_p writefunc, char * __xdata str)
54 | {
55 | uint8_t n = 0;
56 | char c;
57 |
58 | if (!str) return 0;
59 |
60 | while ( c=*str++ ) { // assignment intented
61 | if (writefunc(c)) n++;
62 | else break;
63 | }
64 | return n;
65 | }
66 |
67 |
68 | uint8_t Print_print_u(writefunc_p writefunc, __xdata unsigned long n)
69 | {
70 | return printNumber(writefunc, n,10);
71 | }
72 |
73 | uint8_t Print_print_i(writefunc_p writefunc, __xdata long n)
74 | {
75 | return printInt(writefunc, n,10);
76 | }
77 |
78 | // (not so) Private Methods /////////////////////////////////////////////////////////////
79 |
80 | uint8_t Print_println(writefunc_p writefunc)
81 | {
82 | uint8_t n;
83 |
84 | n = writefunc(13);
85 | n += writefunc(10);
86 | return n;
87 | }
88 |
89 |
90 | uint8_t Print_print_ub(writefunc_p writefunc, __xdata unsigned long n, __xdata uint8_t base)
91 | {
92 | __xdata char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
93 | __xdata char *str = &buf[sizeof(buf) - 1];
94 |
95 | *str = '\0';
96 |
97 | // prevent crash if called with base == 1
98 | if (base < 2) base = 10;
99 |
100 | do {
101 | char c = n % base;
102 | n /= base;
103 |
104 | *--str = c < 10 ? c + '0' : c + 'A' - 10;
105 | } while(n);
106 |
107 | return Print_print_s(writefunc, str);
108 | }
109 |
110 | uint8_t Print_print_ib(writefunc_p writefunc, __xdata long n, __xdata uint8_t base)
111 | {
112 | if (base == 0) {
113 | return writefunc((unsigned char) n);
114 | } else if (base == 10) {
115 | if (n < 0) {
116 | int t = writefunc('-');
117 | n = -n;
118 | return printNumber(writefunc, n, 10) + t;
119 | }
120 | return printNumber(writefunc, n, 10);
121 | } else {
122 | return printNumber(writefunc, n, base);
123 | }
124 | }
125 |
126 |
--------------------------------------------------------------------------------
/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/USBCDC.c:
--------------------------------------------------------------------------------
1 | #ifndef USER_USB_RAM
2 |
3 | #include
4 | #include
5 | #include "include/ch554.h"
6 | #include "include/ch554_usb.h"
7 |
8 | extern __xdata uint8_t Ep0Buffer[];
9 | extern __xdata uint8_t Ep2Buffer[];
10 |
11 | #define LINE_CODEING_SIZE 7
12 | __xdata uint8_t LineCoding[LINE_CODEING_SIZE]={0x00,0xe1,0x00,0x00,0x00,0x00,0x08}; //Initialize for baudrate 57600, 1 stopbit, No parity, eight data bits
13 |
14 | volatile __xdata uint8_t USBByteCountEP2 = 0; //Bytes of received data on USB endpoint
15 | volatile __xdata uint8_t USBBufOutPointEP2 = 0; //Data pointer for fetching
16 |
17 | volatile __xdata uint8_t UpPoint2_Busy = 0; //Flag of whether upload pointer is busy
18 | volatile __xdata uint8_t controlLineState = 0;
19 |
20 | __xdata uint8_t usbWritePointer = 0;
21 |
22 | void delayMicroseconds(uint16_t us);
23 |
24 | void resetCDCParameters(){
25 |
26 | USBByteCountEP2 = 0; //Bytes of received data on USB endpoint
27 | UpPoint2_Busy = 0;
28 | }
29 |
30 | void setLineCodingHandler(){
31 | for (uint8_t i=0;i<((LINE_CODEING_SIZE<=USB_RX_LEN)?LINE_CODEING_SIZE:USB_RX_LEN);i++){
32 | LineCoding[i] = Ep0Buffer[i];
33 | }
34 |
35 | //!!!!!Config_Uart0(LineCoding);
36 | }
37 |
38 | uint16_t getLineCodingHandler(){
39 | uint16_t returnLen;
40 |
41 | returnLen = LINE_CODEING_SIZE;
42 | for (uint8_t i=0;i 0)
67 | result = true;
68 | //delay(10); not doing it for now
69 | return result;
70 | }
71 |
72 |
73 | void USBSerial_flush(void){
74 | if (!UpPoint2_Busy && usbWritePointer>0){
75 | UEP2_T_LEN = usbWritePointer;
76 | UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //Respond ACK
77 | UpPoint2_Busy = 1;
78 | usbWritePointer = 0;
79 | }
80 | }
81 |
82 | uint8_t USBSerial_write(char c){ //3 bytes generic pointer
83 | uint16_t waitWriteCount;
84 | if (controlLineState > 0) {
85 | while (true){
86 | waitWriteCount = 0;
87 | while (UpPoint2_Busy){//wait for 250ms or give up, on my mac it takes about 256us
88 | waitWriteCount++;
89 | delayMicroseconds(5);
90 | if (waitWriteCount>=50000) return 0;
91 | }
92 | if (usbWritePointer 0) {
107 | while (len>0){
108 | waitWriteCount = 0;
109 | while (UpPoint2_Busy){//wait for 250ms or give up, on my mac it takes about 256us
110 | waitWriteCount++;
111 | delayMicroseconds(5);
112 | if (waitWriteCount>=50000) return 0;
113 | }
114 | while (len>0){
115 | if (usbWritePointer> 8,
22 | 0x02,0x01,0x00,0x80,0x64, //Configuration descriptor (2 interfaces)
23 | // Interface Association Descriptor, IAD, this packes following 2 interfaces into 1
24 | 0x08,0x0B,0x00,0x02,0x02,0x02,0x01,0x04,
25 | // Interface 1 (CDC) descriptor
26 | 0x09,0x04,0x00,0x00,0x01,0x02,0x02,0x01,0x04, // CDC control description, 1 endpoint
27 | // Functional Descriptor refer to usbcdc11.pdf
28 | 0x05,0x24,0x00,0x10,0x01, //Header Functional Descriptor
29 | 0x05,0x24,0x01,0x00,0x00, //Call Management Functional Descriptor
30 | 0x04,0x24,0x02,0x02, //Direct Line Management Functional Descriptor, Support: Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, Serial_State
31 | 0x05,0x24,0x06,0x00,0x01, //Union Functional Descriptor, Communication class interface 0, Data Class Interface 1
32 | 0x07,0x05,0x81,0x03,0x08,0x00,0x40, //EndPoint descriptor (CDC Upload, Interrupt)
33 | // Interface 2 (Data Interface) descriptor
34 | 0x09,0x04,0x01,0x00,0x02,0x0a,0x00,0x00,0x04, //Data Class Interface descriptor
35 | 0x07,0x05,0x02,0x02,0x40,0x00,0x00, //endpoint descriptor
36 | 0x07,0x05,0x82,0x02,0x40,0x00,0x00, //endpoint descriptor
37 |
38 | };
39 |
40 | __code uint16_t CfgDescLen = sizeof(CfgDesc);
41 |
42 | //String Descriptors
43 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor
44 | __code uint16_t LangDesLen = sizeof(LangDes);
45 | __code uint8_t SerDes[]={ //Serial String Descriptor
46 | 0x0C,0x03,
47 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00
48 | };
49 | __code uint16_t SerDesLen = sizeof(SerDes);
50 | __code uint8_t Prod_Des[]={ //Produce String Descriptor
51 | 0x16,0x03,
52 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,'d',0x00,
53 | 'u',0x00,'i',0x00,'n',0x00,'o',0x00
54 | };
55 | __code uint16_t Prod_DesLen = sizeof(Prod_Des);
56 |
57 | __code uint8_t CDC_Des[]={
58 | 0x16,0x03,
59 | 'C',0x00,'D',0x00,'C',0x00,' ',0x00,'S',0x00,'e',0x00,
60 | 'r',0x00,'i',0x00,'a',0x00,'l',0x00
61 | };
62 | __code uint16_t CDC_DesLen = sizeof(CDC_Des);
63 |
64 | __code uint8_t Manuf_Des[]={
65 | 0x0E,0x03,
66 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00,
67 | };
68 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des);
69 |
70 | #endif
71 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/USBconstant.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONST_DATA_H__
2 | #define __CONST_DATA_H__
3 |
4 | #include
5 | #include "include/ch554.h"
6 | #include "include/ch554_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 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/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/ch554.h"
8 | #include "include/ch554_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 | #include "wiring_private.h"
2 |
3 | void nothing(void) {
4 | }
5 |
6 |
7 | __xdata voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = {
8 | #if EXTERNAL_NUM_INTERRUPTS > 1
9 | nothing,
10 | #endif
11 | #if EXTERNAL_NUM_INTERRUPTS > 0
12 | nothing,
13 | #endif
14 | };
15 |
16 | void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), __xdata uint8_t mode) {
17 | if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
18 | intFunc[interruptNum] = userFunc;
19 |
20 | // Configure the interrupt mode (trigger on low input, any change, rising
21 | // edge, or falling edge). The mode constants were chosen to correspond
22 | // to the configuration bits in the hardware register, so we simply shift
23 | // the mode into place.
24 |
25 | // Enable the interrupt.
26 |
27 | switch (interruptNum) {
28 | case 0:
29 | IT0 = mode;
30 | EX0 = 1;
31 | break;
32 | case 1:
33 | IT1 = mode;
34 | EX1 = 1;
35 | break;
36 | }
37 | }
38 | }
39 |
40 |
41 | void detachInterrupt(uint8_t interruptNum) {
42 | if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
43 | // Disable the interrupt. (We can't assume that interruptNum is equal
44 | // to the number of the EIMSK bit to clear, as this isn't true on the
45 | // ATmega8. There, INT0 is 6 and INT1 is 7.)
46 | switch (interruptNum) {
47 | case 0:
48 | EX0 = 0;
49 | break;
50 | case 1:
51 | EX1 = 0;
52 | break;
53 | }
54 | intFunc[interruptNum] = nothing;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/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/ch554.h"
3 | #include "include/ch554_usb.h"
4 |
5 |
6 | void sendCharDebug(char c) //8Mbps under 24M clk
7 | {
8 | uint8_t interruptOn = EA;
9 | EA = 0;
10 | //using P1.4
11 | __asm__( //any branch will cause unpridictable timing
12 | " mov a,dpl \n" //seems to be the parameter of func
13 |
14 | " clr c \n"
15 | " mov _P1_4,c \n"
16 | " rrc a \n"
17 | " mov _P1_4,c \n"
18 | " rrc a \n"
19 | " mov _P1_4,c \n"
20 | " rrc a \n"
21 | " mov _P1_4,c \n"
22 | " rrc a \n"
23 | " mov _P1_4,c \n"
24 | " rrc a \n"
25 | " mov _P1_4,c \n"
26 | " rrc a \n"
27 | " mov _P1_4,c \n"
28 | " rrc a \n"
29 | " mov _P1_4,c \n"
30 | " rrc a \n"
31 | " mov _P1_4,c \n"
32 | " setb c \n"
33 | " mov _P1_4,c \n"
34 |
35 | );
36 | if (interruptOn) EA = 1;
37 |
38 | // return charToSend;
39 | }
40 |
41 | /*
42 | void mDelayuS( uint16_t n )
43 | {
44 | #ifdef F_CPU
45 | #if F_CPU <= 6000000
46 | n >>= 2;
47 | #endif
48 | #if F_CPU <= 3000000
49 | n >>= 2;
50 | #endif
51 | #if F_CPU <= 750000
52 | n >>= 4;
53 | #endif
54 | #endif
55 | while ( n ) { // total = 12~13 Fsys cycles, 1uS @Fsys=12MHz
56 | ++ SAFE_MOD; // 2 Fsys cycles, for higher Fsys, add operation here
57 | #ifdef F_CPU
58 | #if F_CPU >= 14000000
59 | ++ SAFE_MOD;
60 | #endif
61 | #if F_CPU >= 16000000
62 | ++ SAFE_MOD;
63 | #endif
64 | #if F_CPU >= 18000000
65 | ++ SAFE_MOD;
66 | #endif
67 | #if F_CPU >= 20000000
68 | ++ SAFE_MOD;
69 | #endif
70 | #if F_CPU >= 22000000
71 | ++ SAFE_MOD;
72 | #endif
73 | #if F_CPU >= 24000000
74 | ++ SAFE_MOD;
75 | #endif
76 | #if F_CPU >= 26000000
77 | ++ SAFE_MOD;
78 | #endif
79 | #if F_CPU >= 28000000
80 | ++ SAFE_MOD;
81 | #endif
82 | #if F_CPU >= 30000000
83 | ++ SAFE_MOD;
84 | #endif
85 | #if F_CPU >= 32000000
86 | ++ SAFE_MOD;
87 | #endif
88 | #endif
89 | -- n;
90 | }
91 | }
92 |
93 | void mDelaymS( uint16_t n ){
94 | while ( n ) {
95 | #ifdef DELAY_MS_HW
96 | while ( ( TKEY_CTRL & bTKC_IF ) == 0 );
97 | while ( TKEY_CTRL & bTKC_IF );
98 | #else
99 | mDelayuS( 1000 );
100 | #endif
101 | -- n;
102 | }
103 | }*/
104 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/include/ch554_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/cores/ch55xduino/include/ch554_usb.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/ch55xduino/ch55x/cores/ch55xduino/include/ch554_usb.h
--------------------------------------------------------------------------------
/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);
24 |
25 | //unsigned char runSerialEvent;
26 |
27 | void DeviceUSBInterrupt(void) __interrupt (INT_NO_USB) //USB interrupt service
28 | {
29 | USBInterrupt();
30 | }
31 |
32 | // 0x08~0x0F belongs to register bank 1
33 | __idata __at (0x08) volatile uint32_t timer0_overflow_count = 0;
34 | __idata __at (0x0C) volatile uint8_t timer0_overflow_count_5th_byte = 0;
35 |
36 | void Timer0Interrupt(void) __interrupt (INT_NO_TMR0) __using(1) //using register bank 1
37 | {
38 | /*timer0_overflow_count++;
39 | */ //when putting timer0_millis and timer0_overflow_count in bank 1, C code is no longer correct
40 | __asm__ (";Increase timer0_overflow_count on R0~R4(5bytes)\n"
41 | " inc r0 \n"
42 | " cjne r0,#0,incTimer0_overflow_countOver$ \n"
43 | " inc r1 \n"
44 | " cjne r1,#0,incTimer0_overflow_countOver$ \n"
45 | " inc r2 \n"
46 | " cjne r2,#0,incTimer0_overflow_countOver$ \n"
47 | " inc r3 \n"
48 | " cjne r3,#0,incTimer0_overflow_countOver$ \n"
49 | " inc r4 \n"
50 | "incTimer0_overflow_countOver$: \n"
51 | );
52 | }
53 |
54 | void Uart0_ISR(void) __interrupt (INT_NO_UART0)
55 | {
56 | if (RI){
57 | uart0IntRxHandler();
58 | RI =0;
59 | }
60 | if (TI){
61 | uart0IntTxHandler();
62 | TI =0;
63 | }
64 | }
65 |
66 | typedef void (*voidFuncPtr)(void);
67 | extern __xdata voidFuncPtr intFunc[];
68 | void INT0_ISR(void) __interrupt (INT_NO_INT0)
69 | {
70 | intFunc[0]();
71 | }
72 | void INT1_ISR(void) __interrupt (INT_NO_INT1)
73 | {
74 | intFunc[1]();
75 | }
76 |
77 | __xdata voidFuncPtr touchKeyHandler = NULL;
78 | void TOUCHKEY_ISR(void) __interrupt (INT_NO_TKEY)
79 | {
80 | if (touchKeyHandler!=NULL){
81 | touchKeyHandler();
82 | }
83 | }
84 |
85 |
86 | void main(void)
87 | {
88 | init();
89 |
90 | //!!!initVariant();
91 |
92 | setup();
93 |
94 | for (;;) {
95 | loop();
96 | if (1) {
97 | #ifndef USER_USB_RAM
98 | USBSerial_flush();
99 | #endif
100 | //serialEvent();
101 | }
102 | }
103 |
104 | // return 0;
105 | }
106 |
107 | unsigned char _sdcc_external_startup (void) __nonbanked
108 | {
109 | return 0;
110 | }
111 |
112 |
113 |
--------------------------------------------------------------------------------
/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/wiring_analog.c:
--------------------------------------------------------------------------------
1 | #define ARDUINO_MAIN
2 | #include "wiring_private.h"
3 | #include "pins_arduino_include.h"
4 |
5 |
6 | uint8_t analogRead(uint8_t pin)
7 | {
8 | pin = analogPinToChannel(pin);
9 |
10 | if (pin == NOT_ANALOG) return 0;
11 |
12 | ADC_CFG = bADC_EN | bADC_CLK;
13 |
14 | ADC_CTRL = (ADC_CTRL & ~(0x03)) | (0x03&pin);
15 |
16 | ADC_START = 1;
17 |
18 | while(ADC_START);
19 |
20 | return ADC_DATA;
21 | }
22 |
23 |
24 | // Right now, PWM output only works on the pins with
25 | // hardware support. These are defined in the appropriate
26 | // pins_*.c file. For the rest of the pins, we default
27 | // to digital output.
28 | void analogWrite(uint8_t pin, __xdata uint16_t val)
29 | {
30 | // We need to make sure the PWM output is enabled for those pins
31 | // that support it, as we turn it off when digitally reading or
32 | // writing with them. Also, make sure the pin is in output mode
33 | // for consistenty with Wiring, which doesn't require a pinMode
34 | // call for the analog output pins.
35 | pinMode(pin, OUTPUT);
36 | if (val == 0)
37 | {
38 | digitalWrite(pin, LOW);
39 | }
40 | else if (val >= 256)
41 | {
42 | digitalWrite(pin, HIGH);
43 | }
44 | else
45 | {
46 | switch(digitalPinToPWM(pin))
47 | {
48 | case PIN_PWM1:
49 | PIN_FUNC &= ~(bPWM1_PIN_X);
50 | PWM_CTRL |= bPWM1_OUT_EN;
51 | PWM_DATA1 = val;
52 | break;
53 | case PIN_PWM2:
54 | PIN_FUNC &= ~(bPWM2_PIN_X);
55 | PWM_CTRL |= bPWM2_OUT_EN;
56 | PWM_DATA2 = val;
57 | break;
58 | case PIN_PWM1_:
59 | PIN_FUNC |= (bPWM1_PIN_X);
60 | PWM_CTRL |= bPWM1_OUT_EN;
61 | PWM_DATA1 = val;
62 | break;
63 | case PIN_PWM2_:
64 | PIN_FUNC |= (bPWM2_PIN_X);
65 | PWM_CTRL |= bPWM2_OUT_EN;
66 | PWM_DATA2 = val;
67 | break;
68 | case NOT_ON_PWM:
69 | default:
70 | if (val < 128) {
71 | digitalWrite(pin, LOW);
72 | } else {
73 | digitalWrite(pin, HIGH);
74 | }
75 | }
76 | }
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/cores/ch55xduino/wiring_digital.c:
--------------------------------------------------------------------------------
1 | #define ARDUINO_MAIN
2 | #include "wiring_private.h"
3 | #include "pins_arduino_include.h"
4 |
5 | #include "pins_arduino.h" //only include once in core
6 |
7 | void pinMode(uint8_t pin, __xdata uint8_t mode) //only P1 & P3 can set mode
8 | {
9 | uint8_t bit = digitalPinToBitMask(pin);
10 | uint8_t port = digitalPinToPort(pin);
11 |
12 | if (port == NOT_A_PIN) return;
13 |
14 | if (mode == INPUT) {
15 | if (port == P1PORT){
16 | P1_MOD_OC &= ~bit;
17 | P1_DIR_PU &= ~bit;
18 | }else if (port == P3PORT){
19 | P3_MOD_OC &= ~bit;
20 | P3_DIR_PU &= ~bit;
21 | }
22 | } else if (mode == INPUT_PULLUP) {
23 | if (port == P1PORT){
24 | P1_MOD_OC |= bit;
25 | P1_DIR_PU |= bit;
26 | }else if (port == P3PORT){
27 | P3_MOD_OC |= bit;
28 | P3_DIR_PU |= bit;
29 | }
30 | } else if (mode == OUTPUT) {
31 | if (port == P1PORT){
32 | P1_MOD_OC &= ~bit;
33 | P1_DIR_PU |= bit;
34 | }else if (port == P3PORT){
35 | P3_MOD_OC &= ~bit;
36 | P3_DIR_PU |= bit;
37 | }
38 | } else if (mode == OUTPUT_OD) {
39 | if (port == P1PORT){
40 | P1_MOD_OC |= bit;
41 | P1_DIR_PU &= ~bit;
42 | }else if (port == P3PORT){
43 | P3_MOD_OC |= bit;
44 | P3_DIR_PU &= ~bit;
45 | }
46 | }
47 | }
48 |
49 | static void turnOffPWM(uint8_t pwm)
50 | {
51 | switch (pwm)
52 | {
53 | case PIN_PWM1:
54 | if ((PIN_FUNC & bPWM1_PIN_X) == 0){
55 | PWM_CTRL &= ~bPWM1_OUT_EN;
56 | }
57 | break;
58 | case PIN_PWM2:
59 | if ((PIN_FUNC & bPWM2_PIN_X) == 0){
60 | PWM_CTRL &= ~bPWM2_OUT_EN;
61 | }
62 | break;
63 | case PIN_PWM1_:
64 | if ((PIN_FUNC & bPWM1_PIN_X) == 1){
65 | PWM_CTRL &= ~bPWM1_OUT_EN;
66 | }
67 | break;
68 | case PIN_PWM2_:
69 | if ((PIN_FUNC & bPWM2_PIN_X) == 1){
70 | PWM_CTRL &= ~bPWM2_OUT_EN;
71 | }
72 | break;
73 | }
74 | }
75 |
76 | uint8_t digitalRead(uint8_t pin)
77 | {
78 | uint8_t pwm = digitalPinToPWM(pin);
79 | uint8_t bit = digitalPinToBitMask(pin);
80 | uint8_t port = digitalPinToPort(pin);
81 |
82 | if (port == NOT_A_PIN) return LOW;
83 |
84 | // If the pin that support PWM output, we need to turn it off
85 | // before getting a digital reading.
86 | if (pwm != NOT_ON_PWM) turnOffPWM(pwm);
87 |
88 | uint8_t portBuf = 0;
89 |
90 | switch(port){
91 | case P1PORT:
92 | portBuf = P1;
93 | break;
94 | case P2PORT:
95 | portBuf = P2;
96 | break;
97 | case P3PORT:
98 | portBuf = P3;
99 | break;
100 | default:
101 | break;
102 | }
103 |
104 | if (portBuf & bit) return HIGH;
105 | return LOW;
106 | }
107 |
108 | void digitalWrite(uint8_t pin, __xdata uint8_t val)
109 | {
110 | uint8_t pwm = digitalPinToPWM(pin);
111 | uint8_t bit = digitalPinToBitMask(pin);
112 | uint8_t port = digitalPinToPort(pin);
113 |
114 | // If the pin that support PWM output, we need to turn it off
115 | // before doing a digital write.
116 | if (pwm != NOT_ON_PWM) turnOffPWM(pwm);
117 |
118 | //C pointers cannot be used to access the 8051's SFRs (special function registers).
119 |
120 | uint8_t interruptOn = EA;
121 | EA = 0;
122 |
123 | switch(port){
124 | case P1PORT:
125 | if (val == LOW) {
126 | P1 &= ~bit;
127 | } else {
128 | P1 |= bit;
129 | }
130 | break;
131 | case P2PORT:
132 | if (val == LOW) {
133 | P2 &= ~bit;
134 | } else {
135 | P2 |= bit;
136 | }
137 | break;
138 | case P3PORT:
139 | if (val == LOW) {
140 | P3 &= ~bit;
141 | } else {
142 | P3 |= bit;
143 | }
144 | break;
145 | default:
146 | break;
147 | }
148 |
149 | if (interruptOn) EA = 1;
150 | }
151 |
152 |
--------------------------------------------------------------------------------
/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/ch554.h"
7 | #include "include/ch554_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/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/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_u(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_u(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_f(voltage);
31 | // or with precision:
32 | //USBSerial_println_fd(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_s("Int0 triggered: ");
49 | USBSerial_println_i(int0Count);
50 |
51 | if (int0Count>=5){
52 | USBSerial_println_s("Int0 detached");
53 | detachInterrupt(0);
54 | }
55 | }
56 | if (int1Count != int1CountOld) {
57 | int1CountOld = int1Count;
58 | USBSerial_print_s("Int1 triggered: ");
59 | USBSerial_println_i(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_u(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_s("on");
61 | USBSerial_print_s("number of button pushes: ");
62 | USBSerial_println_u(buttonPushCounter);
63 | } else {
64 | // if the current state is LOW then the button went from on to off:
65 | USBSerial_println_s("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_s("sensor = ");
51 | USBSerial_print_u(sensorValue);
52 | USBSerial_print_s("\t output = ");
53 | USBSerial_println_u(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_u(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_s("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_s(", 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_u(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_ub(thisByte, DEC);
58 |
59 |
60 | USBSerial_print_s(", hex: ");
61 | // prints value as string in hexadecimal (base 16):
62 | USBSerial_print_ub(thisByte, HEX);
63 |
64 | USBSerial_print_s(", oct: ");
65 | // prints value as string in octal (base 8);
66 | USBSerial_print_ub(thisByte, OCT);
67 |
68 | USBSerial_print_s(", bin: ");
69 | // prints value as string in binary (base 2) also prints ending line break:
70 | USBSerial_println_ub(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.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Dimmer
3 |
4 | Demonstrates sending data from the computer to the Arduino board, in this case
5 | to control the brightness of an LED. The data is sent in individual bytes,
6 | each of which ranges from 0 to 255. Arduino reads these bytes and uses them to
7 | set the brightness of the LED.
8 |
9 | The circuit:
10 | - LED attached from digital pin P3.4 to ground.
11 | - Serial connection to Processing, Max/MSP, or another serial application
12 |
13 | created 2006
14 | by David A. Mellis
15 | modified 30 Aug 2011
16 | by Tom Igoe and Scott Fitzgerald
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/Dimmer
25 | */
26 |
27 | const int ledPin = 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)
28 |
29 | void setup() {
30 | // No need to init USBSerial
31 |
32 | // initialize the ledPin as an output:
33 | pinMode(ledPin, OUTPUT);
34 | }
35 |
36 | void loop() {
37 | byte brightness;
38 |
39 | // check if data has been sent from the computer:
40 | if (USBSerial_available()) {
41 | // read the most recent byte (which will be from 0 to 255):
42 | brightness = USBSerial_read();
43 | // set the brightness of the LED:
44 | analogWrite(ledPin, brightness);
45 | }
46 | }
47 |
48 | /* Processing code for this example
49 |
50 | // Dimmer - sends bytes over a serial port
51 |
52 | // by David A. Mellis
53 | // This example code is in the public domain.
54 |
55 | import processing.serial.*;
56 | Serial port;
57 |
58 | void setup() {
59 | size(256, 150);
60 |
61 | println("Available serial ports:");
62 | // if using Processing 2.1 or later, use Serial.printArray()
63 | println(Serial.list());
64 |
65 | // Uses the first port in this list (number 0). Change this to select the port
66 | // corresponding to your Arduino board. The last parameter (e.g. 9600) is the
67 | // speed of the communication. It has to correspond to the value passed to
68 | // Serial.begin() in your Arduino sketch.
69 | port = new Serial(this, Serial.list()[0], 9600);
70 |
71 | // If you know the name of the port used by the Arduino board, you can specify
72 | // it directly like this.
73 | //port = new Serial(this, "COM1", 9600);
74 | }
75 |
76 | void draw() {
77 | // draw a gradient from black to white
78 | for (int i = 0; i < 256; i++) {
79 | stroke(i);
80 | line(i, 0, i, 150);
81 | }
82 |
83 | // write the current X-position of the mouse to the serial port as
84 | // a single byte
85 | port.write(mouseX);
86 | }
87 |
88 | */
89 |
90 | /* Max/MSP v5 patch for this example
91 |
92 | ----------begin_max5_patcher----------
93 | 1008.3ocuXszaiaCD9r8uhA5rqAeHIa0aAMaAVf1S6hdoYQAsDiL6JQZHQ2M
94 | YWr+2KeX4vjnjXKKkKhhiGQ9MeyCNz+X9rnMp63sQvuB+MLa1OlOalSjUvrC
95 | ymEUytKuh05TKJWUWyk5nE9eSyuS6jesvHu4F4MxOuUzB6X57sPKWVzBLXiP
96 | xZtGj6q2vafaaT0.BzJfjj.p8ZPukazsQvpfcpFs8mXR3plh8BoBxURIOWyK
97 | rxspZ0YI.eTCEh5Vqp+wGtFXZMKe6CZc3yWZwTdCmYW.BBkdiby8v0r+ST.W
98 | sD9SdUkn8FYspPbqvnBNFtZWiUyLmleJWo0vuKzeuj2vpJLaWA7YiE7wREui
99 | FpDFDp1KcbAFcP5sJoVxp4NB5Jq40ougIDxJt1wo3GDZHiNocKhiIExx+owv
100 | AdOEAksDs.RRrOoww1Arc.9RvN2J9tamwjkcqknvAE0l+8WnjHqreNet8whK
101 | z6mukIK4d+Xknv3jstvJs8EirMMhxsZIusET25jXbX8xczIl5xPVxhPcTGFu
102 | xNDu9rXtUCg37g9Q8Yc+EuofIYmg8QdkPCrOnXsaHwYs3rWx9PGsO+pqueG2
103 | uNQBqWFh1X7qQG+3.VHcHrfO1nyR2TlqpTM9MDsLKNCQVz6KO.+Sfc5j1Ykj
104 | jzkn2jwNDRP7LVb3d9LtoWBAOnvB92Le6yRmZ4UF7YpQhiFi7A5Ka8zXhKdA
105 | 4r9TRGG7V4COiSbAJKdXrWNhhF0hNUh7uBa4Mba0l7JUK+omjDMwkSn95Izr
106 | TOwkdp7W.oPRmNRQsiKeu4j3CkfVgt.NYPEYqMGvvJ48vIlPiyzrIuZskWIS
107 | xGJPcmPiWOfLodybH3wjPbMYwlbFIMNHPHFOtLBNaLSa9sGk1TxMzCX5KTa6
108 | WIH2ocxSdngM0QPqFRxyPHFsprrhGc9Gy9xoBjz0NWdR2yW9DUa2F85jG2v9
109 | FgTO4Q8qiC7fzzQNpmNpsY3BrYPVJBMJQ1uVmoItRhw9NrVGO3NMNzYZ+zS7
110 | 3WTvTOnUydG5kHMKLqAOjTe7fN2bGSxOZDkMrBrGQ9J1gONBEy0k4gVo8qHc
111 | cxmfxVihWz6a3yqY9NazzUYkua9UnynadOtogW.JfsVGRVNEbWF8I+eHtcwJ
112 | +wLXqZeSdWLo+FQF6731Tva0BISKTx.cLwmgJsUTTvkg1YsnXmxDge.CDR7x
113 | D6YmX6fMznaF7kdczmJXwm.XSOOrdoHhNA7GMiZYLZZR.+4lconMaJP6JOZ8
114 | ftCs1YWHZI3o.sIXezX5ihMSuXzZtk3ai1mXRSczoCS32hAydeyXNEu5SHyS
115 | xqZqbd3ZLdera1iPqYxOm++v7SUSz
116 | -----------end_max5_patcher-----------
117 | */
118 |
--------------------------------------------------------------------------------
/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_s("ECHO:");
47 | USBSerial_println_s(recvStr);
48 | USBSerial_flush();
49 | stringComplete = false;
50 | recvStrPtr = 0;
51 |
52 | echoCounter++;
53 | USBSerial_print_s("echo count: ");
54 | USBSerial_println_i(echoCounter);
55 | USBSerial_flush();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/libraries/Generic_Examples/examples/04.Communication/Graph/Graph.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Graph
3 |
4 | A simple example of communication from the Arduino board to the computer: The
5 | value of analog input 0 is sent out the serial port. We call this "serial"
6 | communication because the connection appears to both the Arduino and the
7 | computer as a serial port, even though it may actually use a USB cable. Bytes
8 | are sent one after another (serially) from the Arduino to the computer.
9 |
10 | You can use the Arduino Serial Monitor to view the sent data, or it can be
11 | read by Processing, PD, Max/MSP, or any other program capable of reading data
12 | from a serial port. The Processing code below graphs the data received so you
13 | can see the value of the analog input changing over time.
14 |
15 | The circuit:
16 | - any analog input sensor attached to analog in pin P1.1
17 |
18 | created 2006
19 | by David A. Mellis
20 | modified 9 Apr 2012
21 | by Tom Igoe and Scott Fitzgerald
22 | modified 28 Feb 2017 for use with sduino
23 | by Michael Mayer
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/Graph
30 | */
31 |
32 | void setup() {
33 | // No need to init USBSerial
34 |
35 | }
36 |
37 | void loop() {
38 | // send the value of analog input P1.1. You may use P1.1, P1.4, P1.5 and P3.2
39 | USBSerial_println_u(analogRead(11));
40 | // wait a bit for the analog-to-digital converter to stabilize after the last
41 | // reading:
42 | delay(2);
43 | }
44 |
45 | /* Processing code for this example
46 |
47 | // Graphing sketch
48 |
49 | // This program takes ASCII-encoded strings from the serial port at 9600 baud
50 | // and graphs them. It expects values in the range 0 to 1023, followed by a
51 | // newline, or newline and carriage return
52 |
53 | // created 20 Apr 2005
54 | // updated 24 Nov 2015
55 | // by Tom Igoe
56 | // This example code is in the public domain.
57 |
58 | import processing.serial.*;
59 |
60 | Serial myPort; // The serial port
61 | int xPos = 1; // horizontal position of the graph
62 | float inByte = 0;
63 |
64 | void setup () {
65 | // set the window size:
66 | size(400, 300);
67 |
68 | // List all the available serial ports
69 | // if using Processing 2.1 or later, use Serial.printArray()
70 | println(Serial.list());
71 |
72 | // I know that the first port in the serial list on my Mac is always my
73 | // Arduino, so I open Serial.list()[0].
74 | // Open whatever port is the one you're using.
75 | myPort = new Serial(this, Serial.list()[0], 9600);
76 |
77 | // don't generate a serialEvent() unless you get a newline character:
78 | myPort.bufferUntil('\n');
79 |
80 | // set inital background:
81 | background(0);
82 | }
83 |
84 | void draw () {
85 | // draw the line:
86 | stroke(127, 34, 255);
87 | line(xPos, height, xPos, height - inByte);
88 |
89 | // at the edge of the screen, go back to the beginning:
90 | if (xPos >= width) {
91 | xPos = 0;
92 | background(0);
93 | } else {
94 | // increment the horizontal position:
95 | xPos++;
96 | }
97 | }
98 |
99 | void serialEvent (Serial myPort) {
100 | // get the ASCII string:
101 | String inString = myPort.readStringUntil('\n');
102 |
103 | if (inString != null) {
104 | // trim off any whitespace:
105 | inString = trim(inString);
106 | // convert to an int and map to the screen height:
107 | inByte = float(inString);
108 | println(inByte);
109 | inByte = map(inByte, 0, 1023, 0, height);
110 | }
111 | }
112 |
113 | */
114 |
115 | /* Max/MSP v5 patch for this example
116 |
117 | ----------begin_max5_patcher----------
118 | 1591.3oc0YszbaaCD9r7uBL5RalQUAO3CvdyS5zVenWZxs5NcfHgjPCIfJIT
119 | RTxj+6AOHkoTDooroUs0AQPR73a+1cwtK3WtZxzEpOwqlB9YveAlL4KWMYh6
120 | Q1GLo99ISKXeJMmU451zTUQAWpmNy+NM+SZ2y+sR1l02JuU9t0hJvFlNcMPy
121 | dOuBv.U5Rgb0LPpRpYBooM3529latArTUVvzZdFPtsXAuDrrTU.f.sBffXxL
122 | vGE50lIHkUVJXq3fRtdaoDvjYfbgjujaFJSCzq4.tLaN.bi1tJefWpqbO0uz
123 | 1IjIABoluxrJ1guxh2JfPO2B5zRNyBCLDFcqbwNvuv9fHCb8bvevyyEU2JKT
124 | YhkBSWPAfq2TZ6YhqmuMUo0feUn+rYpY4YtY+cFw3lUJdCMYAapZqzwUHX8S
125 | crjAd+SIOU6UBAwIygy.Q1+HAA1KH6EveWOFQlitUK92ehfal9kFhUxJ3tWc
126 | sgpxadigWExbt1o7Ps5dk3yttivyg20W0VcSmg1G90qtx92rAZbH4ez.ruy1
127 | nhmaDPidE07J+5n2sg6E6oKXxUSmc20o6E3SPRDbrkXnPGUYE.i5nCNB9TxQ
128 | jG.G0kCTZtH88f07Rt0ZMMWUw8VvbKVAaTk6GyoraPdZff7rQTejBN54lgyv
129 | HE0Ft7AvIvvgvIwO23jBdUkYOuSvIFSiNcjFhiSsUBwsUCh1AgfNSBAeNDBZ
130 | DIDqY.f8.YjfjV1HAn9XDTxyNFYatVTkKx3kcK9GraZpI5jv7GOx+Z37Xh82
131 | LSKHIDmDXaESoXRngIZQDKVkpxUkMCyXCQhcCK1z.G457gi3TzMz4RFD515F
132 | G3bIQQwcP3SOF0zlkGhiCBQ1kOHHFFlXaEBQIQnCwv9QF1LxPZ.A4jR5cyQs
133 | vbvHMJsLll01We+rE2LazX6zYmCraRrsPFwKg1ANBZFY.IAihr8Ox.aH0oAL
134 | hB8nQVw0FSJiZeunOykbT6t3r.NP8.iL+bnwNiXuVMNJH9H9YCm89CFXPBER
135 | bz422p8.O4dg6kRxdyjDqRwMIHTbT3QFLskxJ8tbmQK4tm0XGeZWF7wKKtYY
136 | aTAF.XPNFaaQBinQMJ4QLF0aNHF0JtYuHSxoUZfZY6.UU2ejJTb8lQw8Fo5k
137 | Rv6e2PI+fOM71o2ecY1VgTYdCSxxUqLokuYq9jYJi6lxPgD2NIPePLB0mwbG
138 | YA9Rgxdiu1k5xiLlSU6JVnx6wzg3sYHwTesB8Z5D7RiGZpXyvDNJY.DQX3.H
139 | hvmcUN4bP1yCkhpTle2P37jtBsKrLWcMScEmltOPv22ZfAqQAdKr9HzATQwZ
140 | q18PrUGt6Tst2XMCRUfGuhXs6ccn23YloomMqcTiC5iMGPsHsHRWhWFlaenV
141 | XcqwgCQiGGJzptyS2ZMODBz6fGza0bzmXBj7+DA94bvpR01MffAlueO7HwcI
142 | pWCwmzJdvi9ILgflLAFmyXB6O7ML0YbD26lenmcGxjVsZUN+A6pUK7AtTrPg
143 | M+eRYG0qD9j4I7eEbco8Xh6WcO.or9XDC6UCiewbXHkh6xm5LiPEkzpJDRTu
144 | mEB44Fgz4NCtJvX.SM1vo2SlTCZGAe7GZu6ahdRyzFOhYZ+mbVVSYptBw.K1
145 | tboIkatIA7c1cTKD1u.honLYV04VkluHsXe0szv9pQCE9Ro3jaVB1o15pz2X
146 | zYoBvO5KXCAe0LCYJybE8ZODf4fV8t9qW0zYxq.YJfTosj1bv0xc.SaC0+AV
147 | 9V9L.KKyV3SyTcRtmzi6rO.O16USvts4B5xe9EymDvebK0eMfW6+NIsNlE2m
148 | eqRyJ0utRq13+RjmqYKN1e.4d61jjdsauXe3.2p6jgi9hsNIv97CoyJ01xzl
149 | c3ZhUCtSHx3UZgjoEJYqNY+hYs5zZQVFW19L3JDYaTlMLqAAt1G2yXlnFg9a
150 | 53L1FJVcv.cOX0dh7mCVGCLce7GFcQwDdH5Ta3nyAS0pQbHxegr+tGIZORgM
151 | RnMj5vGl1Fs16drnk7Tf1XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES
152 | 1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd.
153 | HJ5hhnng3h9HPj4lud02.1bxGw.
154 | -----------end_max5_patcher-----------
155 |
156 | */
157 |
--------------------------------------------------------------------------------
/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/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 | while (USBSerial_available()) {
25 | char serialChar = USBSerial_read();
26 | Serial0_write(serialChar);
27 | }
28 | while (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_s("ECHO:");
53 | USBSerial_println_s(recvStr);
54 | USBSerial_flush();
55 | stringComplete = false;
56 | recvStrPtr = 0;
57 |
58 | echoCounter++;
59 | USBSerial_print_s("echo count: ");
60 | USBSerial_println_i(echoCounter);
61 | USBSerial_flush();
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CDCinUserCode/src/userUsbCdc/USBCDC.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "include/ch554.h"
4 | #include "include/ch554_usb.h"
5 | #include "USBconstant.h"
6 | #include "USBhandler.h"
7 |
8 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[];
9 | extern __xdata __at (EP2_ADDR) uint8_t Ep2Buffer[];
10 |
11 | #define LINE_CODEING_SIZE 7
12 | __xdata uint8_t LineCoding[LINE_CODEING_SIZE]={0x00,0xe1,0x00,0x00,0x00,0x00,0x08}; //Initialize for baudrate 57600, 1 stopbit, No parity, eight data bits
13 |
14 | volatile __xdata uint8_t USBByteCountEP2 = 0; //Bytes of received data on USB endpoint
15 | volatile __xdata uint8_t USBBufOutPointEP2 = 0; //Data pointer for fetching
16 |
17 | volatile __xdata uint8_t UpPoint2_Busy = 0; //Flag of whether upload pointer is busy
18 | volatile __xdata uint8_t controlLineState = 0;
19 |
20 | __xdata uint8_t usbWritePointer = 0;
21 |
22 | typedef void( *pTaskFn)( void );
23 |
24 | void delayMicroseconds(uint16_t us);
25 |
26 | void USBInit(){
27 | USBDeviceCfg(); //Device mode configuration
28 | USBDeviceEndPointCfg(); //Endpoint configuration
29 | USBDeviceIntCfg(); //Interrupt configuration
30 | UEP0_T_LEN = 0;
31 | UEP1_T_LEN = 0; //Pre-use send length must be cleared
32 | UEP2_T_LEN = 0;
33 | }
34 |
35 | void resetCDCParameters(){
36 |
37 | USBByteCountEP2 = 0; //Bytes of received data on USB endpoint
38 | UpPoint2_Busy = 0;
39 | }
40 |
41 | void setLineCodingHandler(){
42 | for (uint8_t i=0;i<((LINE_CODEING_SIZE<=USB_RX_LEN)?LINE_CODEING_SIZE:USB_RX_LEN);i++){
43 | LineCoding[i] = Ep0Buffer[i];
44 | }
45 |
46 | //!!!!!Config_Uart0(LineCoding);
47 | }
48 |
49 | uint16_t getLineCodingHandler(){
50 | uint16_t returnLen;
51 |
52 | returnLen = LINE_CODEING_SIZE;
53 | for (uint8_t i=0;i 0)
80 | result = true;
81 | //delay(10); not doing it for now
82 | return result;
83 | }
84 |
85 |
86 | void USBSerial_flush(void){
87 | if (!UpPoint2_Busy && usbWritePointer>0){
88 | UEP2_T_LEN = usbWritePointer;
89 | UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //Respond ACK
90 | UpPoint2_Busy = 1;
91 | usbWritePointer = 0;
92 | }
93 | }
94 |
95 | uint8_t USBSerial_write(char c){ //3 bytes generic pointer
96 | uint16_t waitWriteCount;
97 | if (controlLineState > 0) {
98 | while (true){
99 | waitWriteCount = 0;
100 | while (UpPoint2_Busy){//wait for 250ms or give up, on my mac it takes about 256us
101 | waitWriteCount++;
102 | delayMicroseconds(5);
103 | if (waitWriteCount>=50000) return 0;
104 | }
105 | if (usbWritePointer 0) {
120 | while (len>0){
121 | waitWriteCount = 0;
122 | while (UpPoint2_Busy){//wait for 250ms or give up, on my mac it takes about 256us
123 | waitWriteCount++;
124 | delayMicroseconds(5);
125 | if (waitWriteCount>=50000) return 0;
126 | }
127 | while (len>0){
128 | if (usbWritePointer
5 | #include "include/ch554.h"
6 | #include "include/ch554_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/ch554.h"
6 | #include "include/ch554_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/ch554.h"
6 | #include "include/ch554_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 | These pins works in OC mode with pull-up resistor (70K). Should be safe to connect 3.3V part.
9 |
10 | created 2020
11 | by Deqing Sun for use with CH55xduino
12 |
13 | This example code is in the public domain.
14 |
15 | */
16 |
17 |
18 | #ifndef USER_USB_RAM
19 | #error "This example needs to be compiled with a USER USB setting"
20 | #endif
21 |
22 | #include "src/CMSIS_DAPusb/DAP.h"
23 |
24 |
25 | extern volatile __xdata uint8_t USBByteCountEP1;
26 |
27 |
28 | void setup() {
29 | USBInit();
30 | }
31 |
32 | void loop() {
33 | if (USBByteCountEP1) {
34 | DAP_Thread();
35 | USBByteCountEP1 = 0 ;
36 |
37 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_R_RES | UEP_R_RES_ACK; //enable receive
38 |
39 | UEP1_T_LEN = 64;
40 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //enable send
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/DAP.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the License); you may
7 | * not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | *
18 | * ----------------------------------------------------------------------
19 | *
20 | * $Date: 1. December 2017
21 | * $Revision: V2.0.0
22 | *
23 | * Project: CMSIS-DAP Include
24 | * Title: DAP.h Definitions
25 | *
26 | *---------------------------------------------------------------------------*/
27 |
28 | #ifndef __DAP_H__
29 | #define __DAP_H__
30 |
31 | #include "include/ch554.h"
32 | #include
33 | #include
34 | #include
35 |
36 | // DAP Firmware Version
37 | #define DAP_FW_VER "1.0.0"
38 |
39 | // DAP Command IDs
40 | #define ID_DAP_Info 0x00U
41 | #define ID_DAP_HostStatus 0x01U
42 | #define ID_DAP_Connect 0x02U
43 | #define ID_DAP_Disconnect 0x03U
44 | #define ID_DAP_TransferConfigure 0x04U
45 | #define ID_DAP_Transfer 0x05U
46 | #define ID_DAP_TransferBlock 0x06U
47 | #define ID_DAP_TransferAbort 0x07U
48 | #define ID_DAP_WriteABORT 0x08U
49 | #define ID_DAP_Delay 0x09U
50 | #define ID_DAP_ResetTarget 0x0AU
51 | #define ID_DAP_SWJ_Pins 0x10U
52 | #define ID_DAP_SWJ_Clock 0x11U
53 | #define ID_DAP_SWJ_Sequence 0x12U
54 | #define ID_DAP_SWD_Configure 0x13U
55 | #define ID_DAP_SWD_Sequence 0x1DU
56 | #define ID_DAP_JTAG_Sequence 0x14U
57 | #define ID_DAP_JTAG_Configure 0x15U
58 | #define ID_DAP_JTAG_IDCODE 0x16U
59 | #define ID_DAP_SWO_Transport 0x17U
60 | #define ID_DAP_SWO_Mode 0x18U
61 | #define ID_DAP_SWO_Baudrate 0x19U
62 | #define ID_DAP_SWO_Control 0x1AU
63 | #define ID_DAP_SWO_Status 0x1BU
64 | #define ID_DAP_SWO_ExtendedStatus 0x1EU
65 | #define ID_DAP_SWO_Data 0x1CU
66 |
67 | #define ID_DAP_QueueCommands 0x7EU
68 | #define ID_DAP_ExecuteCommands 0x7FU
69 |
70 | // DAP Vendor Command IDs
71 | #define ID_DAP_Vendor0 0x80U
72 | #define ID_DAP_Vendor1 0x81U
73 | #define ID_DAP_Vendor2 0x82U
74 | #define ID_DAP_Vendor3 0x83U
75 | #define ID_DAP_Vendor4 0x84U
76 | #define ID_DAP_Vendor5 0x85U
77 | #define ID_DAP_Vendor6 0x86U
78 | #define ID_DAP_Vendor7 0x87U
79 | #define ID_DAP_Vendor8 0x88U
80 | #define ID_DAP_Vendor9 0x89U
81 | #define ID_DAP_Vendor10 0x8AU
82 | #define ID_DAP_Vendor11 0x8BU
83 | #define ID_DAP_Vendor12 0x8CU
84 | #define ID_DAP_Vendor13 0x8DU
85 | #define ID_DAP_Vendor14 0x8EU
86 | #define ID_DAP_Vendor15 0x8FU
87 | #define ID_DAP_Vendor16 0x90U
88 | #define ID_DAP_Vendor17 0x91U
89 | #define ID_DAP_Vendor18 0x92U
90 | #define ID_DAP_Vendor19 0x93U
91 | #define ID_DAP_Vendor20 0x94U
92 | #define ID_DAP_Vendor21 0x95U
93 | #define ID_DAP_Vendor22 0x96U
94 | #define ID_DAP_Vendor23 0x97U
95 | #define ID_DAP_Vendor24 0x98U
96 | #define ID_DAP_Vendor25 0x99U
97 | #define ID_DAP_Vendor26 0x9AU
98 | #define ID_DAP_Vendor27 0x9BU
99 | #define ID_DAP_Vendor28 0x9CU
100 | #define ID_DAP_Vendor29 0x9DU
101 | #define ID_DAP_Vendor30 0x9EU
102 | #define ID_DAP_Vendor31 0x9FU
103 |
104 | #define ID_DAP_Invalid 0xFFU
105 |
106 | // DAP Status Code
107 | #define DAP_OK 0U
108 | #define DAP_ERROR 0xFFU
109 |
110 | // DAP ID
111 | #define DAP_ID_VENDOR 1U
112 | #define DAP_ID_PRODUCT 2U
113 | #define DAP_ID_SER_NUM 3U
114 | #define DAP_ID_FW_VER 4U
115 | #define DAP_ID_DEVICE_VENDOR 5U
116 | #define DAP_ID_DEVICE_NAME 6U
117 | #define DAP_ID_CAPABILITIES 0xF0U
118 | #define DAP_ID_TIMESTAMP_CLOCK 0xF1U
119 | #define DAP_ID_SWO_BUFFER_SIZE 0xFDU
120 | #define DAP_ID_PACKET_COUNT 0xFEU
121 | #define DAP_ID_PACKET_SIZE 0xFFU
122 |
123 | // DAP Host Status
124 | #define DAP_DEBUGGER_CONNECTED 0U
125 | #define DAP_TARGET_RUNNING 1U
126 |
127 | // DAP Port
128 | #define DAP_PORT_AUTODETECT 0U // Autodetect Port
129 | #define DAP_PORT_DISABLED 0U // Port Disabled (I/O pins in High-Z)
130 | #define DAP_PORT_SWD 1U // SWD Port (SWCLK, SWDIO) + nRESET
131 | #define DAP_PORT_JTAG 2U // JTAG Port (TCK, TMS, TDI, TDO, nTRST) + nRESET
132 |
133 | // DAP SWJ Pins
134 | #define DAP_SWJ_SWCLK_TCK 0 // SWCLK/TCK
135 | #define DAP_SWJ_SWDIO_TMS 1 // SWDIO/TMS
136 | #define DAP_SWJ_TDI 2 // TDI
137 | #define DAP_SWJ_TDO 3 // TDO
138 | #define DAP_SWJ_nTRST 5 // nTRST
139 | #define DAP_SWJ_nRESET 7 // nRESET
140 |
141 | #define DAP_SWJ_SWCLK_TCK_BIT 0x01 // SWCLK/TCK
142 | #define DAP_SWJ_SWDIO_TMS_BIT 0x02 // SWDIO/TMS
143 | #define DAP_SWJ_TDI_BIT 0x04 // TDI
144 | #define DAP_SWJ_TDO_BIT 0x08 // TDO
145 | #define DAP_SWJ_nTRST_BIT 0x20 // nTRST
146 | #define DAP_SWJ_nRESET_BIT 0x80 // nRESET
147 |
148 | // DAP Transfer Request
149 | #define DAP_TRANSFER_APnDP (1U << 0)
150 | #define DAP_TRANSFER_RnW (1U << 1)
151 | #define DAP_TRANSFER_A2 (1U << 2)
152 | #define DAP_TRANSFER_A3 (1U << 3)
153 | #define DAP_TRANSFER_MATCH_VALUE (1U << 4)
154 | #define DAP_TRANSFER_MATCH_MASK (1U << 5)
155 | #define DAP_TRANSFER_TIMESTAMP (1U << 7)
156 |
157 | // DAP Transfer Response
158 | #define DAP_TRANSFER_OK (1U << 0)
159 | #define DAP_TRANSFER_WAIT (1U << 1)
160 | #define DAP_TRANSFER_FAULT (1U << 2)
161 | #define DAP_TRANSFER_ERROR (1U << 3)
162 | #define DAP_TRANSFER_MISMATCH (1U << 4)
163 |
164 | // DAP SWO Trace Mode
165 | #define DAP_SWO_OFF 0U
166 | #define DAP_SWO_UART 1U
167 | #define DAP_SWO_MANCHESTER 2U
168 |
169 | // DAP SWO Trace Status
170 | #define DAP_SWO_CAPTURE_ACTIVE (1U << 0)
171 | #define DAP_SWO_CAPTURE_PAUSED (1U << 1)
172 | #define DAP_SWO_STREAM_ERROR (1U << 6)
173 | #define DAP_SWO_BUFFER_OVERRUN (1U << 7)
174 |
175 | // Debug Port Register Addresses
176 | #define DP_IDCODE 0x00U // IDCODE Register (SW Read only)
177 | #define DP_ABORT 0x00U // Abort Register (SW Write only)
178 | #define DP_CTRL_STAT 0x04U // Control & Status
179 | #define DP_WCR 0x04U // Wire Control Register (SW Only)
180 | #define DP_SELECT 0x08U // Select Register (JTAG R/W & SW W)
181 | #define DP_RESEND 0x08U // Resend (SW Read Only)
182 | #define DP_RDBUFF 0x0CU // Read Buffer (Read Only)
183 |
184 | // JTAG IR Codes
185 | #define JTAG_ABORT 0x08U
186 | #define JTAG_DPACC 0x0AU
187 | #define JTAG_APACC 0x0BU
188 | #define JTAG_IDCODE 0x0EU
189 | #define JTAG_BYPASS 0x0FU
190 |
191 | // JTAG Sequence Info
192 | #define JTAG_SEQUENCE_TCK 0x3FU // TCK count
193 | #define JTAG_SEQUENCE_TMS 0x40U // TMS value
194 | #define JTAG_SEQUENCE_TDO 0x80U // TDO capture
195 |
196 | // SWD Sequence Info
197 | #define SWD_SEQUENCE_CLK 0x3FU // SWCLK count
198 | #define SWD_SEQUENCE_DIN 0x80U // SWDIO capture
199 |
200 | #define DAP_PACKET_COUNT 4
201 | #define DAP_PACKET_SIZE 64 //THIS_ENDP0_SIZE
202 | #define DAP_DEFAULT_PORT DAP_PORT_SWD
203 |
204 | #define RST P3_0
205 | #define SWK P3_1
206 | #define SWD P3_2
207 | #define LED P3_3
208 |
209 | extern __xdata uint8_t Ep0Buffer[];
210 | extern __xdata uint8_t Ep1Buffer[];
211 |
212 | extern __idata uint8_t turnaround;
213 | extern __idata uint8_t data_phase;
214 | extern __idata uint8_t idle_cycles;
215 |
216 | extern void DAP_Thread(void);
217 | extern void PORT_SWD_SETUP(void);
218 | extern uint8_t SWD_Transfer(uint8_t reqI, uint8_t __xdata *datas);
219 | extern void SWJ_Sequence(uint8_t count, const uint8_t *datas);
220 | extern void SWD_Sequence(uint8_t info, const uint8_t *swdo, uint8_t *swdi);
221 |
222 | #endif /* __DAP_H__ */
223 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/CMSIS_DAP/src/CMSIS_DAPusb/SW_DP.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/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/ch554.h"
4 | #include "include/ch554_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/ch554.h"
6 | #include "include/ch554_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.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, //PACKET_SIZE
10 | 0x09,0x12,0x50,0xC5, // VID PID
11 | 0x00,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 | 0x01, /* bNumInterfaces */
21 | 0x01, /* bConfigurationValue */
22 | 0x00, /* iConfiguration */
23 | 0x80, /* bmAttributes */
24 | 0x64, /* MaxPower */
25 |
26 |
27 | /* I/F descr: HID */
28 | 0x09, /* bLength */
29 | 0x04, /* bDescriptorType */
30 | 0x00, /* bInterfaceNumber */
31 | 0x00, /* bAlternateSetting */
32 | 0x02, /* bNumEndpoints */
33 | 0x03, /* bInterfaceClass */
34 | 0x00, /* bInterfaceSubClass */
35 | 0x00, /* bInterfaceProtocol */
36 | 0x00, /* iInterface */
37 |
38 | /* HID Descriptor */
39 | 0x09, /* Size of this descriptor in UINT8s. */
40 | 0x21, /* HID descriptor type. */
41 | 0x10, 0x01, /* HID Class Spec. release number. */
42 | 0x00, /* H/W target country. */
43 | 0x01, /* Number of HID class descriptors to follow. */
44 | 0x22, /* Descriptor type. */
45 | sizeof(ReportDesc) & 0xff,sizeof(ReportDesc) >> 8, /* Total length of report descriptor. */
46 |
47 | /* EP Descriptor: interrupt in. */
48 | 0x07, /* bLength */
49 | 0x05, /* bDescriptorType */
50 | 0x01, /* bEndpointAddress */
51 | 0x03, /* bmAttributes */
52 | 0x40, 0x00, /* wMaxPacketSize */
53 | 1, /* bInterval */
54 |
55 | /* EP Descriptor: interrupt out. */
56 | 0x07, /* bLength */
57 | 0x05, /* bDescriptorType */
58 | 0x81, /* bEndpointAddress */
59 | 0x03, /* bmAttributes */
60 | 0x40, 0x00, /* wMaxPacketSize */
61 | 1 /* bInterval */
62 |
63 |
64 | };
65 |
66 |
67 | __code uint16_t ReportDescLen = sizeof(ReportDesc);
68 |
69 | __code uint8_t ReportDesc[] ={
70 | 0x06, 0x00, 0xFF, // Usage Page = 0xFF00 (Vendor Defined Page 1)
71 | 0x09, 0x01, // Usage (Vendor Usage 1)
72 | 0xA1, 0x01, // Collection (Application)
73 | 0x19, 0x01, // Usage Minimum
74 | 0x29, 0x40, // Usage Maximum //64 input usages total (0x01 to 0x40)
75 | 0x15, 0x00, // Logical Minimum (data bytes in the report may have minimum value = 0x00)
76 | 0x26, 0xFF, 0x00, // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
77 | 0x75, 0x08, // Report Size: 8-bit field size
78 | 0x95, 0x40, // Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output", or "Feature" item)
79 | 0x81, 0x00, // Input (Data, Array, Abs): Instantiates input packet fields based on the above report size, count, logical min/max, and usage.
80 | 0x19, 0x01, // Usage Minimum
81 | 0x29, 0x40, // Usage Maximum //64 output usages total (0x01 to 0x40)
82 | 0x91, 0x00, // Output (Data, Array, Abs): Instantiates output packet fields. Uses same report size and count as "Input" fields, since nothing new/different was specified to the parser since the "Input" item.
83 | 0xC0 // End Collection
84 | };
85 |
86 | __code uint16_t CfgDescLen = sizeof(CfgDesc);
87 |
88 | //String Descriptors
89 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor
90 | __code uint16_t LangDesLen = sizeof(LangDes);
91 | __code uint8_t SerDes[]={ //Serial String Descriptor
92 | 0x0C,0x03,
93 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00
94 | };
95 | __code uint16_t SerDesLen = sizeof(SerDes);
96 | __code uint8_t Prod_Des[]={ //Produce String Descriptor
97 | 0x20,0x03,
98 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,' ',0x00,
99 | 'C',0x00,'M',0x00,'S',0x00,'I',0x00,'S',0x00,'-',0x00
100 | ,'D',0x00,'A',0x00, 'P',0x00
101 | };
102 | __code uint16_t Prod_DesLen = sizeof(Prod_Des);
103 |
104 | __code uint8_t Manuf_Des[]={
105 | 0x0E,0x03,
106 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00,
107 | };
108 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des);
109 |
--------------------------------------------------------------------------------
/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/ch554.h"
6 | #include "include/ch554_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/ch554.h"
6 | #include "include/ch554_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 |
13 | #ifndef USER_USB_RAM
14 | #error "This example needs to be compiled with a USER USB setting"
15 | #endif
16 |
17 | #include "src/userUsbHidKeyboard/USBHIDKeyboard.h"
18 |
19 | #define BUTTON1_PIN 30
20 | #define BUTTON2_PIN 31
21 | #define BUTTON3_PIN 32
22 |
23 | #define LED_BUILTIN 33
24 |
25 | bool button1PressPrev = false;
26 | bool button2PressPrev = false;
27 | bool button3PressPrev = false;
28 |
29 |
30 | void setup() {
31 | USBInit();
32 | pinMode(BUTTON1_PIN, INPUT_PULLUP);
33 | pinMode(BUTTON2_PIN, INPUT_PULLUP);
34 | pinMode(BUTTON3_PIN, INPUT_PULLUP);
35 | pinMode(LED_BUILTIN, OUTPUT);
36 | }
37 |
38 | void loop() {
39 | //button 1 is mapped to letter 'a'
40 | bool button1Press = !digitalRead(BUTTON1_PIN);
41 | if (button1PressPrev != button1Press) {
42 | button1PressPrev = button1Press;
43 | if (button1Press) {
44 | Keyboard_press('a');
45 | } else {
46 | Keyboard_release('a');
47 | }
48 | }
49 |
50 | //button 2 is mapped to string 'hello'
51 | bool button2Press = !digitalRead(BUTTON2_PIN);
52 | if (button2PressPrev != button2Press) {
53 | button2PressPrev = button2Press;
54 | if (button2Press) {
55 | Keyboard_write('H');
56 | Keyboard_write('e');
57 | Keyboard_write('l');
58 | Keyboard_write('l');
59 | Keyboard_write('o');
60 | }
61 | }
62 |
63 | //button 3 is mapped to Capslock
64 | bool button3Press = !digitalRead(BUTTON3_PIN);
65 | if (button3PressPrev != button3Press) {
66 | button3PressPrev = button3Press;
67 | if (button3Press) {
68 | Keyboard_press(KEY_CAPS_LOCK);
69 | delay(100); //a quick capslock press is not recognized on mac
70 | Keyboard_release(KEY_CAPS_LOCK);
71 | }
72 | }
73 |
74 | //map capsLock to LED
75 | //Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana,
76 | if (Keyboard_getLEDStatus() & 2) {
77 | digitalWrite(LED_BUILTIN, HIGH);
78 | } else {
79 | digitalWrite(LED_BUILTIN, LOW);
80 | }
81 |
82 | delay(50); //naive debouncing
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBHIDKeyboard.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "include/ch554.h"
4 | #include "include/ch554_usb.h"
5 | #include "USBconstant.h"
6 | #include "USBhandler.h"
7 |
8 | extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[];
9 | extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[];
10 |
11 | volatile __xdata uint8_t UpPoint1_Busy = 0; //Flag of whether upload pointer is busy
12 |
13 | __xdata uint8_t HIDKey[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
14 |
15 | #define SHIFT 0x80
16 | __code uint8_t _asciimap[128] =
17 | {
18 | 0x00, // NUL
19 | 0x00, // SOH
20 | 0x00, // STX
21 | 0x00, // ETX
22 | 0x00, // EOT
23 | 0x00, // ENQ
24 | 0x00, // ACK
25 | 0x00, // BEL
26 | 0x2a, // BS Backspace
27 | 0x2b, // TAB Tab
28 | 0x28, // LF Enter
29 | 0x00, // VT
30 | 0x00, // FF
31 | 0x00, // CR
32 | 0x00, // SO
33 | 0x00, // SI
34 | 0x00, // DEL
35 | 0x00, // DC1
36 | 0x00, // DC2
37 | 0x00, // DC3
38 | 0x00, // DC4
39 | 0x00, // NAK
40 | 0x00, // SYN
41 | 0x00, // ETB
42 | 0x00, // CAN
43 | 0x00, // EM
44 | 0x00, // SUB
45 | 0x00, // ESC
46 | 0x00, // FS
47 | 0x00, // GS
48 | 0x00, // RS
49 | 0x00, // US
50 |
51 | 0x2c, // ' '
52 | 0x1e|SHIFT, // !
53 | 0x34|SHIFT, // "
54 | 0x20|SHIFT, // #
55 | 0x21|SHIFT, // $
56 | 0x22|SHIFT, // %
57 | 0x24|SHIFT, // &
58 | 0x34, // '
59 | 0x26|SHIFT, // (
60 | 0x27|SHIFT, // )
61 | 0x25|SHIFT, // *
62 | 0x2e|SHIFT, // +
63 | 0x36, // ,
64 | 0x2d, // -
65 | 0x37, // .
66 | 0x38, // /
67 | 0x27, // 0
68 | 0x1e, // 1
69 | 0x1f, // 2
70 | 0x20, // 3
71 | 0x21, // 4
72 | 0x22, // 5
73 | 0x23, // 6
74 | 0x24, // 7
75 | 0x25, // 8
76 | 0x26, // 9
77 | 0x33|SHIFT, // :
78 | 0x33, // ;
79 | 0x36|SHIFT, // <
80 | 0x2e, // =
81 | 0x37|SHIFT, // >
82 | 0x38|SHIFT, // ?
83 | 0x1f|SHIFT, // @
84 | 0x04|SHIFT, // A
85 | 0x05|SHIFT, // B
86 | 0x06|SHIFT, // C
87 | 0x07|SHIFT, // D
88 | 0x08|SHIFT, // E
89 | 0x09|SHIFT, // F
90 | 0x0a|SHIFT, // G
91 | 0x0b|SHIFT, // H
92 | 0x0c|SHIFT, // I
93 | 0x0d|SHIFT, // J
94 | 0x0e|SHIFT, // K
95 | 0x0f|SHIFT, // L
96 | 0x10|SHIFT, // M
97 | 0x11|SHIFT, // N
98 | 0x12|SHIFT, // O
99 | 0x13|SHIFT, // P
100 | 0x14|SHIFT, // Q
101 | 0x15|SHIFT, // R
102 | 0x16|SHIFT, // S
103 | 0x17|SHIFT, // T
104 | 0x18|SHIFT, // U
105 | 0x19|SHIFT, // V
106 | 0x1a|SHIFT, // W
107 | 0x1b|SHIFT, // X
108 | 0x1c|SHIFT, // Y
109 | 0x1d|SHIFT, // Z
110 | 0x2f, // [
111 | 0x31, // bslash
112 | 0x30, // ]
113 | 0x23|SHIFT, // ^
114 | 0x2d|SHIFT, // _
115 | 0x35, // `
116 | 0x04, // a
117 | 0x05, // b
118 | 0x06, // c
119 | 0x07, // d
120 | 0x08, // e
121 | 0x09, // f
122 | 0x0a, // g
123 | 0x0b, // h
124 | 0x0c, // i
125 | 0x0d, // j
126 | 0x0e, // k
127 | 0x0f, // l
128 | 0x10, // m
129 | 0x11, // n
130 | 0x12, // o
131 | 0x13, // p
132 | 0x14, // q
133 | 0x15, // r
134 | 0x16, // s
135 | 0x17, // t
136 | 0x18, // u
137 | 0x19, // v
138 | 0x1a, // w
139 | 0x1b, // x
140 | 0x1c, // y
141 | 0x1d, // z
142 | 0x2f|SHIFT, // {
143 | 0x31|SHIFT, // |
144 | 0x30|SHIFT, // }
145 | 0x35|SHIFT, // ~
146 | 0 // DEL
147 | };
148 |
149 | typedef void( *pTaskFn)( void );
150 |
151 | void delayMicroseconds(uint16_t us);
152 |
153 | void USBInit(){
154 | USBDeviceCfg(); //Device mode configuration
155 | USBDeviceEndPointCfg(); //Endpoint configuration
156 | USBDeviceIntCfg(); //Interrupt configuration
157 | UEP0_T_LEN = 0;
158 | UEP1_T_LEN = 0; //Pre-use send length must be cleared
159 | UEP2_T_LEN = 0;
160 | }
161 |
162 |
163 |
164 | void USB_EP1_IN(){
165 | UEP1_T_LEN = 0;
166 | UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // Default NAK
167 | UpPoint1_Busy = 0; //Clear busy flag
168 | }
169 |
170 |
171 | void USB_EP1_OUT(){
172 | if ( U_TOG_OK ) // Discard unsynchronized packets
173 | {
174 |
175 | }
176 | }
177 |
178 | uint8_t USB_EP1_send(){
179 | uint16_t waitWriteCount = 0;
180 |
181 | waitWriteCount = 0;
182 | while (UpPoint1_Busy){//wait for 250ms or give up
183 | waitWriteCount++;
184 | delayMicroseconds(5);
185 | if (waitWriteCount>=50000) return 0;
186 | }
187 |
188 | for (uint8_t i=0;i= 136) { // it's a non-printing key (not a modifier)
202 | k = k - 136;
203 | } else if (k >= 128) { // it's a modifier key
204 | HIDKey[0] |= (1<<(k-128));
205 | k = 0;
206 | } else { // it's a printing key
207 | k = _asciimap[k];
208 | if (!k) {
209 | //setWriteError();
210 | return 0;
211 | }
212 | if (k & 0x80) { // it's a capital letter or other character reached with shift
213 | HIDKey[0] |= 0x02; // the left shift modifier
214 | k &= 0x7F;
215 | }
216 | }
217 |
218 | // Add k to the key report only if it's not already present
219 | // and if there is an empty slot.
220 | if (HIDKey[2] != k && HIDKey[3] != k &&
221 | HIDKey[4] != k && HIDKey[5] != k &&
222 | HIDKey[6] != k && HIDKey[7] != k) {
223 |
224 | for (i=2; i<8; i++) {
225 | if (HIDKey[i] == 0x00) {
226 | HIDKey[i] = k;
227 | break;
228 | }
229 | }
230 | if (i == 8) {
231 | //setWriteError();
232 | return 0;
233 | }
234 | }
235 | USB_EP1_send();
236 | return 1;
237 | }
238 |
239 | uint8_t Keyboard_release(uint8_t k) {
240 | uint8_t i;
241 | if (k >= 136) { // it's a non-printing key (not a modifier)
242 | k = k - 136;
243 | } else if (k >= 128) { // it's a modifier key
244 | HIDKey[0] &= ~(1<<(k-128));
245 | k = 0;
246 | } else { // it's a printing key
247 | k = _asciimap[k];
248 | if (!k) {
249 | return 0;
250 | }
251 | if (k & 0x80) { // it's a capital letter or other character reached with shift
252 | HIDKey[0] &= ~(0x02); // the left shift modifier
253 | k &= 0x7F;
254 | }
255 | }
256 |
257 | // Test the key report to see if k is present. Clear it if it exists.
258 | // Check all positions in case the key is present more than once (which it shouldn't be)
259 | for (i=2; i<8; i++) {
260 | if (0 != k && HIDKey[i] == k) {
261 | HIDKey[i] = 0x00;
262 | }
263 | }
264 |
265 | USB_EP1_send();
266 | return 1;
267 | }
268 |
269 | void Keyboard_releaseAll(void){
270 | for (uint8_t i=0;i
5 | #include "include/ch554.h"
6 | #include "include/ch554_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.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,0x50,0xC5, // VID PID
11 | 0x00,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 | 0x01,0x01,0x00,0x80,0x64, //Configuration descriptor (1 interface)
21 | // Interface 1 (HID) descriptor
22 | 0x09,0x04,0x00,0x00,0x02,0x03,0x01,0x01,0x00, // HID Keyboard, 2 endpoints
23 | 0x09,0x21,0x10,0x01,0x21,0x01,0x22,sizeof(ReportDesc) & 0xff,sizeof(ReportDesc) >> 8, //HID Descriptor
24 | 0x07,0x05,0x01,0x03,0x08,0x00,0x0A, //endpoint descriptor
25 | 0x07,0x05,0x81,0x03,0x08,0x00,0x0A, //endpoint descriptor
26 | };
27 |
28 | __code uint16_t ReportDescLen = sizeof(ReportDesc);
29 |
30 | __code uint8_t ReportDesc[] ={
31 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
32 | 0x09, 0x06, // USAGE (Keyboard)
33 | 0xa1, 0x01, // COLLECTION (Application)
34 | 0x05, 0x07, // USAGE_PAGE (Keyboard)
35 | 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
36 | 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
37 | 0x15, 0x00, // LOGICAL_MINIMUM (0)
38 | 0x25, 0x01, // LOGICAL_MAXIMUM (1)
39 | 0x95, 0x08, // REPORT_COUNT (8)
40 | 0x75, 0x01, // REPORT_SIZE (1)
41 | 0x81, 0x02, // INPUT (Data,Var,Abs)
42 | 0x95, 0x01, // REPORT_COUNT (1)
43 | 0x75, 0x08, // REPORT_SIZE (8)
44 | 0x81, 0x03, // INPUT (Cnst,Var,Abs)
45 | 0x95, 0x06, // REPORT_COUNT (6)
46 | 0x75, 0x08, // REPORT_SIZE (8)
47 | 0x15, 0x00, // LOGICAL_MINIMUM (0)
48 | 0x25, 0xff, // LOGICAL_MAXIMUM (255)
49 | 0x05, 0x07, // USAGE_PAGE (Keyboard)
50 | 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
51 | 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
52 | 0x81, 0x00, // INPUT (Data,Ary,Abs)
53 | 0x05, 0x08, // USAGE_PAGE (LEDs)
54 | 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
55 | 0x29, 0x05, // USAGE_MAXIMUM (Kana)
56 | 0x15, 0x00, // LOGICAL_MINIMUM (0)
57 | 0x25, 0x01, // LOGICAL_MAXIMUM (1)
58 | 0x95, 0x05, // REPORT_COUNT (5)
59 | 0x75, 0x01, // REPORT_SIZE (1)
60 | 0x91, 0x02, // OUTPUT (Data,Var,Abs)
61 | 0x95, 0x01, // REPORT_COUNT (1)
62 | 0x75, 0x03, // REPORT_SIZE (3)
63 | 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
64 | 0xc0 // END_COLLECTION
65 | };
66 |
67 | __code uint16_t CfgDescLen = sizeof(CfgDesc);
68 |
69 | //String Descriptors
70 | __code uint8_t LangDes[]={0x04,0x03,0x09,0x04}; //Language Descriptor
71 | __code uint16_t LangDesLen = sizeof(LangDes);
72 | __code uint8_t SerDes[]={ //Serial String Descriptor
73 | 0x14,0x03,
74 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,' ',0x00,'k',0x00,'b',0x00,'d',0x00
75 | };
76 | __code uint16_t SerDesLen = sizeof(SerDes);
77 | __code uint8_t Prod_Des[]={ //Produce String Descriptor
78 | 0x16,0x03,
79 | 'C',0x00,'H',0x00,'5',0x00,'5',0x00,'x',0x00,'d',0x00,
80 | 'u',0x00,'i',0x00,'n',0x00,'o',0x00
81 | };
82 | __code uint16_t Prod_DesLen = sizeof(Prod_Des);
83 |
84 | __code uint8_t Manuf_Des[]={
85 | 0x0E,0x03,
86 | 'D',0x00,'e',0x00,'q',0x00,'i',0x00,'n',0x00,'g',0x00,
87 | };
88 | __code uint16_t Manuf_DesLen = sizeof(Manuf_Des);
89 |
--------------------------------------------------------------------------------
/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/ch554.h"
6 | #include "include/ch554_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/ch554.h"
6 | #include "include/ch554_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/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/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_s("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_s("DEVID OK");
27 | } else {
28 | USBSerial_println_s("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_s("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_s("ACCEL: ");
71 | USBSerial_print_i(x);
72 | USBSerial_print_s(", ");
73 | USBSerial_print_i(y);
74 | USBSerial_print_s(", ");
75 | USBSerial_println_i(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/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_s("TIN");
36 | USBSerial_print_i(i);
37 | if (touchResult & (1 << i)) {
38 | USBSerial_print_s(" X ");
39 | } else {
40 | USBSerial_print_s(" _ ");
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_s("RawValue:");
53 | USBSerial_print_i(rawValueBuf);
54 | USBSerial_print_s(",touchThreshold:");
55 | USBSerial_print_i(touchBaseline[1] - touchThreshold);
56 | USBSerial_print_s(",releaseThreshold:");
57 | USBSerial_print_i(touchBaseline[1] - releaseThreshold);
58 | USBSerial_print_s(",Baseline:");
59 | USBSerial_println_i(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.c:
--------------------------------------------------------------------------------
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 |
12 | #include "TouchKey.h"
13 |
14 | typedef void (*voidFuncPtr)(void);
15 | extern __xdata voidFuncPtr touchKeyHandler;
16 |
17 | volatile __xdata uint16_t touchRawValue[6];
18 | volatile __xdata uint8_t touchISRCounter = 0;
19 | __xdata uint8_t channelEnabled = 0;
20 |
21 | __xdata uint8_t touchCounterProcessed = 0;
22 | __xdata uint8_t processIndex = 0;
23 |
24 | __xdata uint8_t touchCycleCounter = 0;
25 |
26 | __xdata uint16_t touchBaseline[6];
27 | __xdata uint8_t touchBaselineNoiseCount[6];
28 | __xdata uint8_t prevDiffBaselinePos;
29 | __xdata uint8_t touchNextCalibrateCycleCounter = 0;
30 |
31 | __xdata uint8_t touchMaxHalfDelta;
32 | __xdata uint8_t touchNoiseHalfDelta;
33 | __xdata uint8_t touchNoiseCountLimit;
34 | __xdata uint8_t touchFilterDelayLimit;
35 | __xdata uint8_t touchThreshold;
36 | __xdata uint8_t releaseThreshold;
37 |
38 | __xdata uint8_t touchKeyPressed;
39 |
40 | #pragma save
41 | #pragma nooverlay
42 | void TouchKey_ISR_Handler(void){
43 | uint8_t index = TKEY_CTRL & 0x07;
44 | touchRawValue[index-1]=TKEY_DAT;
45 |
46 | index++;
47 | if (index>6) index = 1;
48 |
49 | TKEY_CTRL = bTKC_2MS | index;
50 | touchISRCounter++;
51 |
52 | }
53 | #pragma restore
54 |
55 |
56 | //TIN0(P1.0), TIN1(P1.1), TIN2(P1.4), TIN3(P1.5), TIN4(P1.6), TIN5(P1.7)
57 | void TouchKey_begin(uint8_t channelToEnableBitMask){
58 |
59 | channelEnabled = channelToEnableBitMask;
60 |
61 | //set IO to input
62 | if (channelEnabled & (1<<0)){
63 | P1_DIR_PU &= ~(1<<0);
64 | P1_MOD_OC &= ~(1<<0);
65 | }
66 | if (channelEnabled & (1<<1)){
67 | P1_DIR_PU &= ~(1<<1);
68 | P1_MOD_OC &= ~(1<<1);
69 | }
70 | if (channelEnabled & (1<<2)){
71 | P1_DIR_PU &= ~(1<<4);
72 | P1_MOD_OC &= ~(1<<4);
73 | }
74 | if (channelEnabled & (1<<3)){
75 | P1_DIR_PU &= ~(1<<5);
76 | P1_MOD_OC &= ~(1<<5);
77 | }
78 | if (channelEnabled & (1<<4)){
79 | P1_DIR_PU &= ~(1<<6);
80 | P1_MOD_OC &= ~(1<<6);
81 | }
82 | if (channelEnabled & (1<<5)){
83 | P1_DIR_PU &= ~(1<<7);
84 | P1_MOD_OC &= ~(1<<7);
85 | }
86 |
87 | touchMaxHalfDelta = 5;
88 | touchNoiseHalfDelta = 2;
89 | touchNoiseCountLimit = 10;
90 | touchFilterDelayLimit = 5;
91 |
92 | touchThreshold = 100;
93 | releaseThreshold = 80;
94 |
95 | touchKeyHandler = TouchKey_ISR_Handler;
96 |
97 | //sample all channels to prefill baselines
98 | for (uint8_t i=0;i<6;i++){
99 | TKEY_CTRL = bTKC_2MS | (1+i);
100 | while((TKEY_CTRL&bTKC_IF) == 0);
101 | touchBaseline[i]=TKEY_DAT;
102 | }
103 |
104 | TKEY_CTRL = bTKC_2MS | 1;
105 |
106 | IE_TKEY = 1;
107 | }
108 |
109 | uint8_t TouchKey_Process(){ //call this function every 12ms or less.
110 | int8_t sampleToProcess = touchISRCounter-touchCounterProcessed;
111 | if (sampleToProcess > 0){
112 | for (uint8_t i=0;i-touchMaxHalfDelta) ){ //case 1. Small incremental changes to the system represent long term slow (environmental) changes in the system.
124 | touchBaseline[processIndex] = rawData;
125 | }else{
126 | uint8_t noiseCount;
127 | if (diff>0){
128 | if ( (prevDiffBaselinePos&indexBitMask) ){ //same side noise, case 2
129 | noiseCount = touchBaselineNoiseCount[processIndex];
130 | noiseCount++;
131 | if (noiseCount>=touchNoiseCountLimit){
132 | noiseCount = 0;
133 | touchBaseline[processIndex] += touchNoiseHalfDelta;
134 | }
135 | touchBaselineNoiseCount[processIndex] = noiseCount;
136 | }else{ //different side noise, case 3
137 | prevDiffBaselinePos|=indexBitMask;
138 | touchBaselineNoiseCount[processIndex] = 1;
139 | }
140 | }else{
141 | if ( (prevDiffBaselinePos&indexBitMask)==0 ){ //same side noise, case 2
142 | noiseCount = touchBaselineNoiseCount[processIndex];
143 | noiseCount++;
144 | if (noiseCount>=touchNoiseCountLimit){
145 | noiseCount = 0;
146 | touchBaseline[processIndex] -= touchNoiseHalfDelta;
147 | }
148 | touchBaselineNoiseCount[processIndex] = noiseCount;
149 | }else{ //different side noise, case 3
150 | prevDiffBaselinePos&=~indexBitMask;
151 | touchBaselineNoiseCount[processIndex] = 1;
152 | }
153 | }
154 | }
155 | }
156 |
157 | //use 2 threshold to avoid glitching
158 | if (touchKeyPressed & indexBitMask){
159 | if ((-diff)touchThreshold){
164 | touchKeyPressed |= indexBitMask;
165 | }
166 | }
167 |
168 | }
169 |
170 | touchCounterProcessed++;
171 | processIndex++;
172 | if (processIndex>=6){
173 | processIndex = 0;
174 | if (touchNextCalibrateCycleCounter == touchCycleCounter){
175 | touchNextCalibrateCycleCounter = touchCycleCounter + touchFilterDelayLimit;
176 | }
177 | touchCycleCounter++;
178 | }
179 | }
180 | }
181 | return touchCycleCounter;
182 | }
183 |
184 | uint8_t TouchKey_Get(){
185 | return touchKeyPressed;
186 | }
187 |
188 | void TouchKey_SetMaxHalfDelta(uint8_t val){
189 | touchMaxHalfDelta = val;
190 | }
191 |
192 | void TouchKey_SetNoiseHalfDelta(uint8_t val){
193 | touchNoiseHalfDelta = val;
194 | }
195 |
196 | void TouchKey_SetNoiseCountLimit(uint8_t val){
197 | touchNoiseCountLimit = val;
198 | }
199 |
200 | void TouchKey_SetFilterDelayLimit(uint8_t val){
201 | touchFilterDelayLimit = val;
202 | }
203 |
204 | void TouchKey_SetTouchThreshold(uint8_t val){
205 | touchThreshold = val;
206 | }
207 |
208 | void TouchKey_SetReleaseThreshold(uint8_t val){
209 | releaseThreshold = val;
210 | }
211 |
212 | void TouchKey_end(void){
213 | touchKeyHandler = NULL;
214 | TKEY_CTRL = 0;
215 | IE_TKEY = 0;
216 | }
--------------------------------------------------------------------------------
/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/platform.txt:
--------------------------------------------------------------------------------
1 |
2 | # Arduino CH55x Core and platform.
3 | # ------------------------------
4 | #
5 | # Part of the ch55xduino project: https://github.com/DeqingSun/ch55xduino
6 | # For more info on this file:
7 | # https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification
8 |
9 | name=CH55x Boards
10 | version=0.1.0
11 |
12 | # General folder structure variables
13 | # ----------------------------------
14 | # For Board Manager installs these variables are already predefined and will
15 | # not be overwritten. This assignment is done only for manual installs.
16 | #
17 | # Apparently, this trick only works for compiler settings, not for the
18 | # upload process. The upload process is not handled by arduino-build, but
19 | # the IDE itself and behaves slightly differently.
20 | # Problem is in Arduino/arduino-core/src/processing/app/BaseNoGui.java
21 | # function createToolPreferences():
22 | # PreferencesData.removeAllKeysWithPrefix(prefix);
23 | # FIXME: find a solution to allow for uploads with manual installations.
24 | runtime.tools.sdcc.path={runtime.hardware.path}/tools/sdcc
25 | runtime.tools.MCS51Tools.path={runtime.hardware.path}/tools
26 |
27 |
28 | # CH55x compile variables
29 | # ---------------------
30 |
31 | compiler.warning_flags=
32 | compiler.warning_flags.none=--less-pedantic
33 | compiler.warning_flags.default=
34 | compiler.warning_flags.more=-Wp-Wall
35 | compiler.warning_flags.all=-Wp-Wall -V
36 |
37 | # Default "compiler.path" is correct, change only if you want to override the initial value
38 | compiler.path={runtime.tools.sdcc.path}/bin
39 | compiler.wrapper.path={runtime.tools.MCS51Tools.path}/wrapper
40 | compiler.wrapper.path.windows={runtime.tools.MCS51Tools.path}/win/busybox" ash "{runtime.tools.MCS51Tools.path}/wrapper
41 | compiler.tools.path={runtime.tools.avr-gcc.path}/bin
42 |
43 | compiler.c.cmd=sdcc
44 | compiler.c.wrapper=sdcc.sh
45 | compiler.c.flags=-MMD -c -Ddouble=float -DUSE_STDINT -D__PROG_TYPES_COMPAT__ {compiler.warning_flags}
46 | compiler.c.elf.flags=--code-size {upload.maximum_size} --xram-size {upload.maximum_data_size} --xram-loc {upload.xdata_location}
47 |
48 | compiler.c.elf.cmd=sdcc
49 | compiler.c.elf.wrapper=sdcc-link.sh
50 | compiler.S.flags=-c -g -x assembler-with-cpp -flto
51 |
52 | compiler.cpp.cmd=sdcc
53 | compiler.cpp.wrapper=sdcc.sh
54 | compiler.cpp.flags={compiler.c.flags}
55 |
56 | compiler.ar.cmd=sdar
57 | compiler.ar.wrapper=sdar.sh
58 | compiler.ar.flags=rcs
59 |
60 | compiler.objcopy.cmd=avr-objcopy
61 | #compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
62 | #compiler.objcopy.eep.flags=-O ihex -j EEPROM EEPROM=0
63 |
64 | compiler.elf2hex.cmd=avr-objcopy
65 | compiler.elf2hex.flags=-O ihex -R DATA -R INITIALIZED -R SSEG
66 |
67 | compiler.syslibs.stdlib.path={runtime.tools.sdcc.path}/share/sdcc
68 | compiler.syslibs.stdlib.path.windows={runtime.tools.sdcc.path}
69 | #compiler.syslibs.spl.path=
70 |
71 | compiler.ldflags=
72 |
73 | #we use mem file directly, because we don't need to use size command
74 | compiler.size.cmd=cat
75 | compiler.size.cmd.windows={runtime.tools.MCS51Tools.path}/win/busybox" cat "
76 |
77 | compiler.systemincludes="-I{compiler.syslibs.stdlib.path}/include"
78 |
79 | # This can be overridden in boards.txt
80 | build.extra_flags=
81 |
82 | # These can be overridden in platform.local.txt
83 | compiler.c.extra_flags=
84 | compiler.c.elf.extra_flags=
85 | compiler.S.extra_flags=
86 | compiler.cpp.extra_flags=
87 | compiler.ar.extra_flags=
88 | compiler.objcopy.eep.extra_flags=
89 | compiler.elf2hex.extra_flags=
90 |
91 | # MCS51 compile patterns
92 | # --------------------
93 |
94 | ## Compile c files (re1)
95 | recipe.c.o.pattern="{compiler.wrapper.path}/{compiler.c.wrapper}" "{compiler.path}/{compiler.c.cmd}" "{source_file}" "{object_file}" re1 {compiler.c.flags} -mmcs51 -D{build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} {includes} {compiler.systemincludes}
96 |
97 | ## Compile c++ files (re2)
98 | recipe.cpp.o.pattern="{compiler.wrapper.path}/{compiler.cpp.wrapper}" "{compiler.path}/{compiler.cpp.cmd}" "{source_file}" "{object_file}" re2 {compiler.cpp.flags} -mmcs51 -D{build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} {compiler.systemincludes}
99 |
100 | ##FIXME Compile S files (re3)
101 | recipe.S.o.pattern="{compiler.path}/{compiler.c.cmd}" re3 {compiler.S.flags} -mmcs51 -D{build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.S.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"
102 |
103 | ## Create archives (re4)
104 | # archive_file_path is needed for backwards compatibility with IDE 1.6.5 or older, IDE 1.6.6 or newer overrides this value
105 | #archive_file_path={build.path}/{archive_file}
106 | recipe.ar.pattern="{compiler.wrapper.path}/{compiler.ar.wrapper}" "{compiler.path}/{compiler.ar.cmd}" "{archive_file_path}" "{object_file}" re4 {compiler.ar.flags} {compiler.ar.extra_flags}
107 |
108 | ## Combine gc-sections, archives, and objects
109 | recipe.c.combine.pattern="{compiler.wrapper.path}/{compiler.c.elf.wrapper}" "{compiler.path}/{compiler.c.elf.cmd}" --nostdlib "-L{build.path}" "-L{compiler.syslibs.stdlib.path}/lib/small" {compiler.c.elf.flags} -mmcs51 -D{build.mcu} {compiler.c.elf.extra_flags} {object_files} "{build.path}/{archive_file}" -l{build.mcu} -lmcs51 -llibsdcc -lliblong -llibint -llibfloat --out-fmt-ihx -o "{build.path}/{build.project_name}.elf"
110 |
111 | ## Create output files (.eep and .hex)
112 | #recipe.objcopy.eep.pattern="{compiler.tools.path}/{compiler.objcopy.cmd}" {compiler.objcopy.eep.flags} {compiler.objcopy.eep.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.eep"
113 | recipe.objcopy.hex.pattern="{compiler.tools.path}/{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
114 |
115 | ## Save hex
116 | recipe.output.tmp_file={build.project_name}.hex
117 | recipe.output.save_file={build.project_name}.{build.variant}.hex
118 |
119 | ## Compute size
120 | # flash usage = HOME + GSINIT + GSFINAL + CODE + INITIALIZER
121 | # RAM usage = DATA + INITIALIZED
122 | recipe.size.pattern="{compiler.size.cmd}" "{build.path}/{build.project_name}.mem"
123 | recipe.size.regex=^(?:\s+ROM\/EPROM\/FLASH)\s+0x[A-Fa-f0-9]+\s+0x[A-Fa-f0-9]+\s+([0-9]+).*
124 | recipe.size.regex.data=^(?:\s+EXTERNAL RAM)\s+0x[A-Fa-f0-9]+\s+0x[A-Fa-f0-9]+\s+([0-9]+).*
125 |
126 | ## Preprocessor (re11, re12)
127 | preproc.includes.flags=-M -MG -MP
128 | recipe.preproc.includes="{compiler.path.wrapper}/{compiler.cpp.wrapper}" "{compiler.path}/{compiler.cpp.cmd}" re11 {compiler.cpp.flags} {preproc.includes.flags} -mmcs51 -D{build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}"
129 | preproc.macros.flags=-E -MC
130 | recipe.preproc.macros="{compiler.wrapper.path}/{compiler.cpp.cmd}.sh" "{compiler.path}/{compiler.cpp.cmd}" "{source_file}" "{preprocessed_file_path}" re12 {compiler.cpp.flags} {preproc.macros.flags} -mmcs51 -D{build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} {compiler.systemincludes}
131 |
132 |
133 | # vnproch55x
134 | # -------------------------------------
135 |
136 | tools.vnproch55x_usb.cmd=vnproch55x
137 | # these lines are for manual install. For board manager installs they get converted to use
138 | # {runtime.tools.MCS51Tools.path} instead of {runtime.hardware.path}/tools
139 | tools.vnproch55x_usb.path={runtime.tools.MCS51Tools.path}/linux
140 | tools.vnproch55x_usb.path.windows={runtime.tools.MCS51Tools.path}/win
141 | tools.vnproch55x_usb.path.macosx={runtime.tools.MCS51Tools.path}/macosx
142 |
143 | tools.vnproch55x_usb.upload.params.verbose=
144 | tools.vnproch55x_usb.upload.params.quiet=
145 | tools.vnproch55x_usb.upload.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.hex"
146 |
147 | # ----
148 |
149 | tools.vnproch55x_serial.cmd=vnproch55x
150 | tools.vnproch55x_serial.path={runtime.tools.MCS51Tools.path}/linux
151 | tools.vnproch55x_serial.path.windows={runtime.tools.MCS51Tools.path}/win
152 | tools.vnproch55x_serial.path.macosx={runtime.tools.MCS51Tools.path}/macosx
153 |
154 | tools.vnproch55x_serial.upload.params.verbose=
155 | tools.vnproch55x_serial.upload.params.quiet=
156 | tools.vnproch55x_serial.upload.pattern="{path}/{cmd}" -s{serial.port} "{build.path}/{build.project_name}.hex"
157 |
158 |
--------------------------------------------------------------------------------
/ch55xduino/ch55x/variants/ch552/pins_arduino.h:
--------------------------------------------------------------------------------
1 | /*
2 | pins_arduino.h - Pin definition functions for Arduino
3 | Part of Arduino - http://www.arduino.cc/
4 |
5 | Copyright (c) 2007 David A. Mellis
6 |
7 | This library is free software; you can redistribute it and/or
8 | modify it under the terms of the GNU Lesser General Public
9 | License as published by the Free Software Foundation; either
10 | version 2.1 of the License, or (at your option) any later version.
11 |
12 | This library is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | Lesser General Public License for more details.
16 |
17 | You should have received a copy of the GNU Lesser General
18 | Public License along with this library; if not, write to the
19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 | Boston, MA 02111-1307 USA
21 | */
22 |
23 | #ifndef Pins_Arduino_h
24 | #define Pins_Arduino_h
25 |
26 | #include "pins_arduino_include.h"
27 |
28 | #ifndef _BV
29 | #define _BV(X) (1<<(X))
30 | #endif
31 |
32 | __code uint8_t digital_pin_to_pwm_PGM[] = {
33 | NOT_ON_PWM, //PIN00
34 | NOT_ON_PWM,
35 | NOT_ON_PWM,
36 | NOT_ON_PWM,
37 | NOT_ON_PWM,
38 | NOT_ON_PWM,
39 | NOT_ON_PWM,
40 | NOT_ON_PWM,
41 | NOT_ON_PWM, //not exist
42 | NOT_ON_PWM, //not exist
43 |
44 | NOT_ON_PWM, //PIN10
45 | NOT_ON_PWM,
46 | NOT_ON_PWM,
47 | NOT_ON_PWM,
48 | NOT_ON_PWM,
49 | PIN_PWM1,
50 | NOT_ON_PWM,
51 | NOT_ON_PWM,
52 | NOT_ON_PWM, //not exist
53 | NOT_ON_PWM, //not exist
54 |
55 | NOT_ON_PWM, //PIN20
56 | NOT_ON_PWM,
57 | NOT_ON_PWM,
58 | NOT_ON_PWM,
59 | NOT_ON_PWM,
60 | NOT_ON_PWM,
61 | NOT_ON_PWM,
62 | NOT_ON_PWM,
63 | NOT_ON_PWM, //not exist
64 | NOT_ON_PWM, //not exist
65 |
66 | PIN_PWM1_, //PIN30
67 | PIN_PWM2_,
68 | NOT_ON_PWM,
69 | NOT_ON_PWM,
70 | PIN_PWM2,
71 | NOT_ON_PWM,
72 | NOT_ON_PWM,
73 | NOT_ON_PWM,
74 | NOT_ON_PWM, //not exist
75 | NOT_ON_PWM, //not exist
76 | };
77 |
78 | __code uint8_t PROGMEM digital_pin_to_port_PGM[] = {
79 | P0PORT, //PIN00
80 | P0PORT,
81 | P0PORT,
82 | P0PORT,
83 | P0PORT,
84 | P0PORT,
85 | P0PORT,
86 | P0PORT,
87 | NOT_A_PORT, //not exist
88 | NOT_A_PORT, //not exist
89 |
90 | P1PORT, //PIN10
91 | P1PORT,
92 | P1PORT,
93 | P1PORT,
94 | P1PORT,
95 | P1PORT,
96 | P1PORT,
97 | P1PORT,
98 | NOT_A_PORT, //not exist
99 | NOT_A_PORT, //not exist
100 |
101 | P2PORT, //PIN20
102 | P2PORT,
103 | P2PORT,
104 | P2PORT,
105 | P2PORT,
106 | P2PORT,
107 | P2PORT,
108 | P2PORT,
109 | NOT_A_PORT, //not exist
110 | NOT_A_PORT, //not exist
111 |
112 | P3PORT, //PIN30
113 | P3PORT,
114 | P3PORT,
115 | P3PORT,
116 | P3PORT,
117 | P3PORT,
118 | P3PORT,
119 | P3PORT,
120 | NOT_A_PORT, //not exist
121 | NOT_A_PORT, //not exist
122 | };
123 |
124 | __code uint8_t digital_pin_to_bit_mask_PGM[] = {
125 | _BV(0), //PIN00
126 | _BV(1),
127 | _BV(2),
128 | _BV(3),
129 | _BV(4),
130 | _BV(5),
131 | _BV(6),
132 | _BV(7),
133 | 0, //not exist
134 | 0, //not exist
135 |
136 | _BV(0), //PIN10
137 | _BV(1),
138 | _BV(2),
139 | _BV(3),
140 | _BV(4),
141 | _BV(5),
142 | _BV(6),
143 | _BV(7),
144 | 0, //not exist
145 | 0, //not exist
146 |
147 | _BV(0), //PIN20
148 | _BV(1),
149 | _BV(2),
150 | _BV(3),
151 | _BV(4),
152 | _BV(5),
153 | _BV(6),
154 | _BV(7),
155 | 0, //not exist
156 | 0, //not exist
157 |
158 | _BV(0), //PIN30
159 | _BV(1),
160 | _BV(2),
161 | _BV(3),
162 | _BV(4),
163 | _BV(5),
164 | _BV(6),
165 | _BV(7),
166 | 0, //not exist
167 | 0, //not exist
168 | };
169 |
170 | __code uint8_t digital_pin_to_channel_PGM[] = {
171 | NOT_ANALOG, //PIN00
172 | NOT_ANALOG,
173 | NOT_ANALOG,
174 | NOT_ANALOG,
175 | NOT_ANALOG,
176 | NOT_ANALOG,
177 | NOT_ANALOG,
178 | NOT_ANALOG,
179 | NOT_ANALOG, //not exist
180 | NOT_ANALOG, //not exist
181 |
182 | NOT_ANALOG, //PIN10
183 | 0,
184 | NOT_ANALOG,
185 | NOT_ANALOG,
186 | 1,
187 | 2,
188 | NOT_ANALOG,
189 | NOT_ANALOG,
190 | NOT_ANALOG, //not exist
191 | NOT_ANALOG, //not exist
192 |
193 | NOT_ANALOG, //PIN20
194 | NOT_ANALOG,
195 | NOT_ANALOG,
196 | NOT_ANALOG,
197 | NOT_ANALOG,
198 | NOT_ANALOG,
199 | NOT_ANALOG,
200 | NOT_ANALOG,
201 | NOT_ANALOG, //not exist
202 | NOT_ANALOG, //not exist
203 |
204 | NOT_ANALOG, //PIN30
205 | NOT_ANALOG,
206 | 3,
207 | NOT_ANALOG,
208 | NOT_ANALOG,
209 | NOT_ANALOG,
210 | NOT_ANALOG,
211 | NOT_ANALOG,
212 | NOT_ANALOG, //not exist
213 | NOT_ANALOG, //not exist
214 | };
215 |
216 | #endif
217 |
--------------------------------------------------------------------------------
/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/tools/macosx/vnproch55x:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/ch55xduino/tools/macosx/vnproch55x
--------------------------------------------------------------------------------
/ch55xduino/tools/win/libusb-1.0.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/ch55xduino/tools/win/libusb-1.0.dll
--------------------------------------------------------------------------------
/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 | >&2 echo -ne "${CYAN}"
61 | >&2 echo -n "${@}"
62 | >&2 echo -e "${OFF}"
63 |
64 | # echo the mark id in green and the compiler call in white:
65 | SDAR=$1
66 | LIB=$2
67 | OBJ=${3%.o}.rel
68 | MARK=$4
69 | shift 4
70 |
71 | >&2 echo -ne "${GREEN}Mark $MARK:${OFF}"
72 | >&2 echo "$SDAR" "$@" "$LIB" "$OBJ"
73 |
74 | "$SDAR" "$@" "$LIB" "$OBJ"
75 | ERR=$?
76 | cp -a "$LIB" "${LIB%.a}.lib"
77 |
78 | # propagate the sdar exit code
79 | exit $ERR
80 |
--------------------------------------------------------------------------------
/ch55xduino/tools/wrapper/sdcc-link.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # usage: sdcc-link [options] [.lib and .rel files] re5 [other flags and files]
4 | #
5 | # possible script options (options in this order only):
6 | # -v: verbose
7 | # -d: debug mode (more verbose), includes coloring the output
8 | # -c: color the output
9 |
10 |
11 | ### local functions ######################################################
12 |
13 | # verbose print
14 | #
15 | # usage: vprint min_level msg
16 | #
17 | # prints a message if the verbosity level is equal or higher than the required
18 | # min_level
19 | #
20 | vprint ()
21 | {
22 | local level=$1
23 |
24 | shift
25 | if [ $VERBOSE -ge $level ]; then
26 | echo -e "$@"
27 | fi
28 | }
29 |
30 |
31 | # parse the script options
32 | #
33 | # This is very crude. The options are allowed only as the very first argments
34 | # and they are required to be used in exactly this order.
35 |
36 | VERBOSE=0
37 | USE_COLOR=0
38 | if [ "$1" == "-v" ]; then
39 | VERBOSE=1;
40 | shift
41 | elif [ "$1" == "-d" ]; then
42 | VERBOSE=2
43 | USE_COLOR=1
44 | set -x
45 | shift
46 | fi
47 | if [ "$1" == "-c" ]; then
48 | USE_COLOR=1;
49 | shift
50 | fi
51 |
52 |
53 | # define color codes
54 |
55 | if [ $USE_COLOR -gt 0 ]; then
56 | # ANSI color codes to beautify the output:
57 | BLACK='\033[0;30m'
58 | RED='\033[0;31m'
59 | GREEN='\033[0;32m'
60 | ORANGE='\033[0;33m'
61 | BLUE='\033[0;34m'
62 | PURPLE='\033[0;35m'
63 | CYAN='\033[0;36m'
64 | LGRAY='\033[0;37m'
65 | DGRAY='\033[1;30m'
66 | LRED='\033[1;31m'
67 | LGREEN='\033[1;32m'
68 | YELLOW='\033[1;33m'
69 | LBLUE='\033[1;34m'
70 | LPURPLE='\033[1;35m'
71 | LCYAN='\033[1;36m'
72 | WHITE='\033[1;37m'
73 | OFF='\033[0m'
74 | fi
75 |
76 |
77 | SDCC="$1"
78 | shift
79 |
80 | # echo the full command line in cyan on stderr:
81 | >&2 vprint 1 "${CYAN}${@}${OFF}"
82 |
83 |
84 | # The arduino system insists on a *.a file for a library, but sdar requires
85 | # them to be named *.lib.
86 | #
87 | # Workaround: copy all *.lib files into *.a files.
88 | #
89 | # Iterate over all positional parameters with a for loop.
90 | # The pattern match for filename is easy for bash and dash, but busybox ash
91 | # requires the use of the 'expr' command:
92 | #
93 | # bash, dash: if [[ "$FILE" == *.a ]]; then
94 | # ash uses 'expr': expr "$FILE" : ".*\.a$"; then
95 | #
96 | # This is all pure POSIX, it works for bash, dash and busybox ash
97 | vprint 2 "copy *.a to *.lib"
98 | for FILE; do
99 | vprint 2 "- checking parameter '$FILE'"
100 | # using expr works for bash, dash, busybox ash
101 | if expr "$FILE" : ".*\.a$" > /dev/null; then
102 | NEW=${FILE%.a}.lib
103 | vprint 1 "copy $FILE to $NEW"
104 | cp -a "$FILE" "$NEW"
105 | fi
106 | done
107 |
108 |
109 | # replace *.o with *.rel and *.a with *.lib
110 | #
111 | # On bash this is a simple pattern substituiton:
112 | # set -- "${@/.o/.rel}"
113 | # set -- "${@/.a/.lib}"
114 | #
115 | # Unfortunatly, this does not work with dash or ash. dash does not support
116 | # pattern substituition in general. busybox ash does not support arrays and
117 | # shortens the arg list to the first argument, deleting all the rest.
118 | #
119 | # As a workaround we combine the argument list into a single string. By
120 | # using TAB as a field separator we can even deal with spaces, backspaces
121 | # and colons in file names.
122 |
123 | # use tab as field separator
124 | IFS=$'\t'
125 |
126 |
127 | # combine all arguments into a single string with field separator and add a
128 | # TAB at the end. This allows the pattern below to match the last argument
129 | # as well.
130 | vprint 2 "Combine all arguments into a single string"
131 | line="$* "
132 |
133 | # do the filename replacements: (bash and ash, not dash)
134 | # Needs a double slash to replace all occurencies, not only the first one.
135 | #line=${line//.o/.rel}
136 | #line=${line//.a/.lib}
137 | # replace all the occurencies just before a field separator
138 | vprint 2 "- before substitution: $line"
139 | line="${line//.o /.rel }"
140 | line="${line//.a /.lib }"
141 | vprint 2 "- after substitution: $line"
142 |
143 | vprint 1 "cmd: ${ORANGE}$SDCC $line${OFF}"
144 | "$SDCC" $line
145 |
146 | # propagate the sdcc exit code
147 | exit $?
148 |
--------------------------------------------------------------------------------
/ch55xduino/tools/wrapper/sdcc.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # wrapper around SDCC to let the external interface look more gcc-alike
4 | #
5 | # Differences:
6 | # - if a .cpp files is given as input temporarly copy it to .c and compile it
7 | # as a c file. This will break the dependency check, as it expects the
8 | # full original filename, but as this happens only for the original .ino
9 | # file it is not a big loss.
10 | # - generate .rel files, but copy them as .o files as well to satisfy the
11 | # dependency checker on following builds
12 |
13 |
14 | VERBOSE=0
15 | USE_COLOR=0
16 | if [ "$1" == "-v" ]; then
17 | VERBOSE=1;
18 | shift
19 | elif [ "$1" == "-vv" ]; then
20 | VERBOSE=2
21 | # USE_COLOR=1
22 | set -x
23 | shift
24 | fi
25 |
26 |
27 | if [ $USE_COLOR -gt 0 ]; then
28 | # ANSI color codes to beautify the output:
29 | BLACK='\033[0;30m'
30 | RED='\033[0;31m'
31 | GREEN='\033[0;32m'
32 | ORANGE='\033[0;33m'
33 | BLUE='\033[0;34m'
34 | PURPLE='\033[0;35m'
35 | CYAN='\033[0;36m'
36 | LGRAY='\033[0;37m'
37 | DGRAY='\033[1;30m'
38 | LRED='\033[1;31m'
39 | LGREEN='\033[1;32m'
40 | YELLOW='\033[1;33m'
41 | LBLUE='\033[1;34m'
42 | LPURPLE='\033[1;35m'
43 | LCYAN='\033[1;36m'
44 | WHITE='\033[1;37m'
45 | OFF='\033[0m'
46 | fi
47 |
48 | # check if cp is in the path using 'command -v' (a builtin POSIX function)
49 | if ! command -v cp > /dev/null; then
50 | # Ok, this means we are on a Windows system and we have to find a
51 | # way to access cp and rm in ../win. A simple 'cd ../win' or
52 | # '../win/cp' does't work, as the current working directory is still
53 | # the Arduino binary directory.
54 | #
55 | # This looks ok, but it doesn't work on some Windows systems:
56 | # (No idea why)
57 | # PATH="${0%/wrapper/*}"/win:$PATH
58 | #
59 | # This is technically wrong, but surprisingly it works with Windows:
60 | # cd $0/../..
61 | # PATH=$(pwd)/win:$PATH
62 | #
63 | # Use cd/pwd as a replacement for 'realpath' using only builtins.
64 | # It has the positive side effect of converting from Windows to Unix
65 | # path syntax avoiding all these backslash issues.
66 | cd "${0%/wrapper/*}"
67 | PATH=$(pwd)/win:$PATH
68 | fi
69 |
70 | # echo the full command line in cyan:
71 | >&2 echo -ne "${CYAN}"
72 | >&2 echo -n "${@}"
73 | >&2 echo -e "${OFF}"
74 |
75 | # echo the mark id in green and the compiler call in white:
76 | SDCC=$1
77 | SRC=$2
78 | OBJ=$3
79 | REL=${OBJ%.o}.rel
80 | MARK=$4
81 | shift 4
82 | >&2 echo -ne "${GREEN}Mark $MARK:${OFF}"
83 | >&2 echo "$SDCC" "$@" "$SRC" -o "$OBJ"
84 |
85 | case "$SRC" in
86 | *.cpp)
87 | # rename .cpp to .c and compile
88 | >&2 echo -e "${RED}cpp found${OFF}";
89 | CSRC="${SRC%pp}"
90 | (
91 | # add a reference to main to pull in main.c
92 | echo "void main(void); void (*dummy_variable) () = main;"
93 | cat "$SRC"
94 | ) > "$CSRC"
95 | # cp -a "$SRC" "$CSRC"
96 | "$SDCC" "$@" "$CSRC" -o "$OBJ"
97 | ERR=$?
98 | rm -f "$CSRC"
99 | ;;
100 | *.c)
101 | # compile a .c file
102 | "$SDCC" "$@" "$SRC" -o "$OBJ"
103 | ERR=$?
104 | ;;
105 | esac
106 |
107 | # Deqing: For some reason my version of SDCC don't generate rel file, it creates o file directly, so I modified the SDCC command below
108 | # copy the generated .rel files as an .o file to avoid recompiling the next time
109 | # if OBJ is a .o file we copy back
110 | if [[ ${OBJ%.o} != $OBJ ]]
111 | then
112 | cp -a "${OBJ}" "${REL}"
113 | fi
114 |
115 | # propagate the sdcc exit code
116 | exit $ERR
117 |
--------------------------------------------------------------------------------
/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_bootloader.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/docs/Zadig_bootloader.png
--------------------------------------------------------------------------------
/docs/blinkLED.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/docs/blinkLED.gif
--------------------------------------------------------------------------------
/docs/simpleCH552Back.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/docs/simpleCH552Back.jpg
--------------------------------------------------------------------------------
/docs/simpleCH552Front.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/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/schematic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atc1441/ch55xduino-1/d463db044cfaf16bbc0e4d2ef054af92b015d3a3/pcb/schematic.png
--------------------------------------------------------------------------------