Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inclusion of Hardware Serial by menu item #115

Open
wants to merge 3 commits into
base: ch55xduino
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ch55xduino/ch55x/boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
menu.usb_settings=USB Settings
menu.upload_method=Upload method
menu.clock=Clock Source
menu.uart=Hardware Serial
##############################################################

ch552.name=CH552 Board
Expand Down Expand Up @@ -34,6 +35,15 @@ ch552.build.mcu=CH552
ch552.upload.use_1200bps_touch=true
ch552.upload.wait_for_upload_port=false

## Hardware UART Settings
ch552.menu.uart.uart0_uart1=UART0 & UART1
ch552.menu.uart.uart0=UART0
ch552.menu.uart.uart0.build.uart_flags=-DNO_UART1
ch552.menu.uart.uart1=UART1
ch552.menu.uart.uart1.build.uart_flags=-DNO_UART0
ch552.menu.uart.nouart=No UART
ch552.menu.uart.nouart.build.uart_flags=-DNO_UART0 -DNO_UART1

## USB Memory Settings
## ----------------------------------------------
ch552.menu.usb_settings.usbcdc=Default CDC
Expand Down Expand Up @@ -98,6 +108,12 @@ ch551.build.mcu=CH551
ch551.upload.use_1200bps_touch=true
ch551.upload.wait_for_upload_port=false

## Hardware UART Settings
ch551.menu.uart.uart0=UART0
ch551.menu.uart.uart0.build.uart_flags=-DNO_UART1
ch551.menu.uart.nouart=No UART
ch551.menu.uart.nouart.build.uart_flags=-DNO_UART0 -DNO_UART1

## USB Memory Settings
## ----------------------------------------------
ch551.menu.usb_settings.usbcdc=Default CDC
Expand Down Expand Up @@ -153,6 +169,15 @@ ch559.build.mcu=CH559
ch559.upload.use_1200bps_touch=true
ch559.upload.wait_for_upload_port=false

## Hardware UART Settings
ch559.menu.uart.uart0_uart1=UART0 & UART1
ch559.menu.uart.uart0=UART0
ch559.menu.uart.uart0.build.uart_flags=-DNO_UART1
ch559.menu.uart.uart1=UART1
ch559.menu.uart.uart1.build.uart_flags=-DNO_UART0
ch559.menu.uart.nouart=No UART
ch559.menu.uart.nouart.build.uart_flags=-DNO_UART0 -DNO_UART1

## USB Memory Settings
## ----------------------------------------------
ch559.menu.usb_settings.usbcdc=Default CDC
Expand Down Expand Up @@ -200,6 +225,15 @@ ch549.build.mcu=CH549
ch549.upload.use_1200bps_touch=true
ch549.upload.wait_for_upload_port=false

## Hardware UART Settings
ch549.menu.uart.uart0_uart1=UART0 & UART1
ch549.menu.uart.uart0=UART0
ch549.menu.uart.uart0.build.uart_flags=-DNO_UART1
ch549.menu.uart.uart1=UART1
ch549.menu.uart.uart1.build.uart_flags=-DNO_UART0
ch549.menu.uart.nouart=No UART
ch549.menu.uart.nouart.build.uart_flags=-DNO_UART0 -DNO_UART1

## USB Memory Settings
## ----------------------------------------------
ch549.menu.usb_settings.usbcdc=Default CDC
Expand Down
6 changes: 4 additions & 2 deletions ch55xduino/ch55x/cores/ch55xduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ char USBSerial_read();
#define USBSerial_println_fd(P,Q) ( Print_print_fd(USBSerial_write,(P),(Q) ) + Print_println(USBSerial_write) )
#define USBSerial_println_c(P) ( (USBSerial_write(P)) + Print_println(USBSerial_write) )


#if !defined(NO_UART0)
#define Serial0_print_s(P) ( Print_print_s(Serial0_write,(P)) )
#define Serial0_print_sn(P,Q) ( Print_print_sn(Serial0_write,(P),(Q)) )
#define Serial0_print_i(P) ( Print_print_i(Serial0_write,(P)) )
Expand All @@ -305,8 +305,9 @@ char USBSerial_read();
#define Serial0_println_f(P) ( Print_print_f(Serial0_write,(P)) + Print_println(Serial0_write) )
#define Serial0_println_fd(P,Q) ( Print_print_fd(Serial0_write,(P),(Q) ) + Print_println(Serial0_write) )
#define Serial0_println_c(P) ( (Serial0_write(P)) + Print_println(Serial0_write) )
#endif


#if !defined(NO_UART1)
#define Serial1_print_s(P) ( Print_print_s(Serial1_write,(P)) )
#define Serial1_print_sn(P,Q) ( Print_print_sn(Serial1_write,(P),(Q)) )
#define Serial1_print_i(P) ( Print_print_i(Serial1_write,(P)) )
Expand All @@ -327,6 +328,7 @@ char USBSerial_read();
#define Serial1_println_f(P) ( Print_print_f(Serial1_write,(P)) + Print_println(Serial1_write) )
#define Serial1_println_fd(P,Q) ( Print_print_fd(Serial1_write,(P),(Q) ) + Print_println(Serial1_write) )
#define Serial1_println_c(P) ( (Serial1_write(P)) + Print_println(Serial1_write) )
#endif

//10K lifecycle DataFlash access on CH551/CH552.
#define eeprom_write_byte(ADDR,VAL) { DPL=(VAL);DPH=(ADDR);eeprom_write_byte_2_params_DPTR(); }
Expand Down
8 changes: 6 additions & 2 deletions ch55xduino/ch55x/cores/ch55xduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#include <stdio.h>
#include "include/ch5xx.h"

#if !defined(NO_UART0)
#define SERIAL0_TX_BUFFER_SIZE 16
#define SERIAL0_RX_BUFFER_SIZE 16
#define SERIAL1_TX_BUFFER_SIZE 16
#define SERIAL1_RX_BUFFER_SIZE 16

#define UART0_FLG_SENDING (1<<0)

Expand All @@ -24,7 +23,11 @@ void Serial0_end(void);

void uart0IntRxHandler();
void uart0IntTxHandler();
#endif

#if !defined(NO_UART1)
#define SERIAL1_TX_BUFFER_SIZE 16
#define SERIAL1_RX_BUFFER_SIZE 16

uint8_t Serial1(void);
void Serial1_begin(unsigned long baud);
Expand All @@ -38,5 +41,6 @@ void Serial1_end(void);

void uart1IntRxHandler();
void uart1IntTxHandler();
#endif

#endif
23 changes: 12 additions & 11 deletions ch55xduino/ch55x/cores/ch55xduino/HardwareSerial0.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
created by Deqing Sun for use with CH55xduino
*/

#if !defined(NO_UART0)
#include "HardwareSerial.h"

__xdata unsigned char serial0Initialized;
Expand All @@ -25,7 +25,7 @@ void Serial0_begin(unsigned long baud){

uint32_t x;
uint8_t x2;

x = 10 * F_CPU / baud / 16; //Make sure it doesn't overflow when baudrate is changed, default 9600
x2 = ((uint16_t)x) % 10;
x /= 10;
Expand All @@ -38,16 +38,16 @@ void Serial0_begin(unsigned long baud){
RCLK = 0; //UART0 receive clk
TCLK = 0; //UART0 send clk
PCON |= SMOD;

TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1; //0X20,Timer1 as 8 bit autoload timer
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
TH1 = 0-x; //baud/12 is real rate
TR1 = 1; //start timer1
TI = 0;
REN = 1; //Enable serial 0 receive

ES = 1; //Enable serial 0 interrupt

serial0Initialized = 1;
}

Expand All @@ -61,22 +61,22 @@ uint8_t Serial0_write(uint8_t SendDat)
if (interruptOn) EA = 1;
return 1;
}

uint8_t nextHeadPos = ((uint8_t)(uart0_tx_buffer_head + 1)) % SERIAL0_TX_BUFFER_SIZE;

uint16_t waitWriteCount=0;
while ((nextHeadPos == uart0_tx_buffer_tail) ){ //wait max 100ms or discard
if (interruptOn) EA = 1;
waitWriteCount++;
delayMicroseconds(5);
delayMicroseconds(5);
if (waitWriteCount>=20000) return 0;
}
Transmit_Uart0_Buf[uart0_tx_buffer_head]=SendDat;

uart0_tx_buffer_head = nextHeadPos;

if (interruptOn) EA = 1;

return 1;
}

Expand All @@ -98,3 +98,4 @@ uint8_t Serial0_read(void){
}
return 0;
}
#endif
7 changes: 3 additions & 4 deletions ch55xduino/ch55x/cores/ch55xduino/HardwareSerial0ISR.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
created by Deqing Sun for use with CH55xduino
*/

#if !defined(NO_UART0)
#include "HardwareSerial.h"

__xdata uint8_t Receive_Uart0_Buf[SERIAL0_RX_BUFFER_SIZE]; //arduino style serial buffer
Expand All @@ -14,7 +14,7 @@ volatile __bit uart0_flag_sending=0;

void uart0IntRxHandler(){
uint8_t nextHead = (uart0_rx_buffer_head + 1) % SERIAL0_RX_BUFFER_SIZE;

if (nextHead != uart0_rx_buffer_tail) {
Receive_Uart0_Buf[uart0_rx_buffer_head] = SBUF;
uart0_rx_buffer_head = nextHead;
Expand All @@ -32,5 +32,4 @@ void uart0IntTxHandler(){
}
}
}


#endif
8 changes: 5 additions & 3 deletions ch55xduino/ch55x/cores/ch55xduino/HardwareSerial1.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !defined(NO_UART1)
#include "HardwareSerial.h"

__xdata unsigned char serial1Initialized;
Expand All @@ -18,7 +19,7 @@ uint8_t Serial1(void){
}

void Serial1_begin(unsigned long baud){

#if defined(CH551) || defined(CH552)
U1SM0 = 0;
U1SMOD = 1; //use mode 1 for serial 1
Expand Down Expand Up @@ -60,7 +61,7 @@ uint8_t Serial1_write(uint8_t SendDat)
{
uint8_t interruptOn = EA;
EA = 0;

if ( (uart1_tx_buffer_head == uart1_tx_buffer_tail) && (uart1_flag_sending==0) ){ //start to send
uart1_flag_sending = 1;
#if defined(CH551) || defined(CH552)
Expand All @@ -86,7 +87,7 @@ uint8_t Serial1_write(uint8_t SendDat)
Transmit_Uart1_Buf[uart1_tx_buffer_head]=SendDat;

uart1_tx_buffer_head = nextHeadPos;

if (interruptOn) EA = 1;

return 1;
Expand All @@ -110,3 +111,4 @@ uint8_t Serial1_read(void){
}
return 0;
}
#endif
4 changes: 2 additions & 2 deletions ch55xduino/ch55x/cores/ch55xduino/HardwareSerial1ISR.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !defined(NO_UART1)
#include "HardwareSerial.h"

__xdata uint8_t Receive_Uart1_Buf[SERIAL1_RX_BUFFER_SIZE]; //arduino style serial buffer
Expand Down Expand Up @@ -40,5 +41,4 @@ void uart1IntTxHandler(){
}
}
}


#endif
8 changes: 8 additions & 0 deletions ch55xduino/ch55x/cores/ch55xduino/genericPrintSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void USBSerial_print_sn_func(char * s, __xdata uint8_t size);
void USBSerial_print_f_func(float f);
void USBSerial_print_fd_func(float f, __xdata uint8_t digits);

#if !defined(NO_UART0)
void Serial0_print_i_func(long i);
void Serial0_print_ib_func(long i, __xdata uint8_t base);
void Serial0_print_u_func(unsigned long u);
Expand All @@ -21,7 +22,9 @@ void Serial0_print_s_func(char * s);
void Serial0_print_sn_func(char * s, __xdata uint8_t size);
void Serial0_print_f_func(float f);
void Serial0_print_fd_func(float f, __xdata uint8_t digits);
#endif

#if !defined(NO_UART1)
void Serial1_print_i_func(long i);
void Serial1_print_ib_func(long i, __xdata uint8_t base);
void Serial1_print_u_func(unsigned long u);
Expand All @@ -30,6 +33,7 @@ void Serial1_print_s_func(char * s);
void Serial1_print_sn_func(char * s, __xdata uint8_t size);
void Serial1_print_f_func(float f);
void Serial1_print_fd_func(float f, __xdata uint8_t digits);
#endif

void printNothing();

Expand Down Expand Up @@ -67,6 +71,7 @@ void printNothing();
)
#define USBSerial_println(...) {USBSerial_print(__VA_ARGS__);Print_println(USBSerial_write);}

#if !defined(NO_UART0)
#define Serial0_print(...) SERIAL0_SELECT(__VA_ARGS__)(__VA_ARGS__)
#define SERIAL0_SELECT(...) CONCAT(SERIAL0_SELECT_, NARG(__VA_ARGS__))(__VA_ARGS__)
#define SERIAL0_SELECT_0() printNothing
Expand Down Expand Up @@ -99,7 +104,9 @@ void printNothing();
unsigned long: _Generic((_2), default: Serial0_print_ub_func) \
)
#define Serial0_println(...) {Serial0_print(__VA_ARGS__);Print_println(Serial0_write);}
#endif

#if !defined(NO_UART1)
#define Serial1_print(...) SERIAL1_SELECT(__VA_ARGS__)(__VA_ARGS__)
#define SERIAL1_SELECT(...) CONCAT(SERIAL1_SELECT_, NARG(__VA_ARGS__))(__VA_ARGS__)
#define SERIAL1_SELECT_0() printNothing
Expand Down Expand Up @@ -132,6 +139,7 @@ void printNothing();
unsigned long: _Generic((_2), default: Serial1_print_ub_func) \
)
#define Serial1_println(...) {Serial1_print(__VA_ARGS__);Print_println(Serial1_write);}
#endif


#define CONCAT(X, Y) CONCAT_(X, Y)
Expand Down
3 changes: 2 additions & 1 deletion ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial0.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
created by Deqing Sun for use with CH55xduino
need SDCC 13402 or higher version
*/

#if !defined(NO_UART0)
#include "Arduino.h"

void Serial0_print_i_func(long i) {
Expand All @@ -23,3 +23,4 @@ void Serial0_print_s_func(char * s) {
void Serial0_print_sn_func(char * s, __xdata uint8_t size) {
Print_print_sn(Serial0_write, s, size);
}
#endif
3 changes: 2 additions & 1 deletion ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial0Float.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
need SDCC 13402 or higher version
float has a separate file to avoid unnecessary linking
*/

#if !defined(NO_UART0)
#include "Arduino.h"

void Serial0_print_f_func(float f) {
Expand All @@ -13,3 +13,4 @@ void Serial0_print_f_func(float f) {
void Serial0_print_fd_func(float f, __xdata uint8_t digits) {
Print_print_fd(Serial0_write, f, digits);
}
#endif
3 changes: 2 additions & 1 deletion ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
created by Deqing Sun for use with CH55xduino
need SDCC 13402 or higher version
*/

#if !defined(NO_UART1)
#include "Arduino.h"

void Serial1_print_i_func(long i) {
Expand All @@ -23,3 +23,4 @@ void Serial1_print_s_func(char * s) {
void Serial1_print_sn_func(char * s, __xdata uint8_t size) {
Print_print_sn(Serial1_write, s, size);
}
#endif
3 changes: 2 additions & 1 deletion ch55xduino/ch55x/cores/ch55xduino/genericPrintSerial1Float.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
need SDCC 13402 or higher version
float has a separate file to avoid unnecessary linking
*/

#if !defined(NO_UART1)
#include "Arduino.h"

void Serial1_print_f_func(float f) {
Expand All @@ -13,3 +13,4 @@ void Serial1_print_f_func(float f) {
void Serial1_print_fd_func(float f, __xdata uint8_t digits) {
Print_print_fd(Serial1_write, f, digits);
}
#endif
Loading