OSDN Git Service

[v2.0] Start to add UART terminal.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 31 Oct 2013 17:40:00 +0000 (02:40 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 31 Oct 2013 17:40:00 +0000 (02:40 +0900)
euart.c [new file with mode: 0644]
euart.h [new file with mode: 0644]
main.c
nbproject/Makefile-default.mk
nbproject/Makefile-genesis.properties
nbproject/configurations.xml
power.c
uart_termio.c [new file with mode: 0644]

diff --git a/euart.c b/euart.c
new file mode 100644 (file)
index 0000000..55b4406
--- /dev/null
+++ b/euart.c
@@ -0,0 +1,208 @@
+/*
+ * OpenI2CRADIO
+ * EUSART Handler
+ * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2,
+ *  or (at your option) any later version.
+ *  This library / program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *  See the GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this library; see the file COPYING. If not, write to the
+ *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  As a special exception, if you link this(includeed from sdcc) library
+ *  with other files, some of which are compiled with SDCC,
+ *  to produce an executable, this library does not by itself cause
+ *  the resulting executable to be covered by the GNU General Public License.
+ *  This exception does not however invalidate any other reasons why
+ *  the executable file might be covered by the GNU General Public License.
+ */
+
+#if defined(__SDCC)
+#include <sdcc-lib.h>
+#include <pic18fregs.h> /* ONLY FOR PIC18x */
+#include <delay.h>
+#else
+#include <xc.h>
+#endif
+#include <signal.h>
+
+#include "iodef.h"
+#include "idle.h"
+#include "ioports.h"
+#include "euart.h"
+
+static char uart_rfifo[UART_BUF_SIZE];
+
+static unsigned int uart_rx_rptr;
+static unsigned int uart_rx_wptr;
+static int uart_rx_bytes;
+static unsigned char uart_rx_sts;
+static unsigned char uart_rx_xon;
+static unsigned char uart_tx_xon;
+
+static unsigned char uart_rx_wakeup;
+
+void uart_sleep(void)
+{
+    PIR1bits.TX1IF = 0;
+    PIR1bits.RC1IF = 0;
+    IPR1bits.TX1IP = 0;  // Low
+    IPR1bits.RC1IP = 1; // High
+    RCSTA = 0b00010000; //SPEN, 8bit, ASYNC, CREN
+    BAUDCON = 0b00001011; // IDLE High, BRG16, ABDEN, WUE
+    TXSTA = 0b00100000; //8bit, ASYNC, TXEN, Break
+    PIE1bits.TX1IE = 0;
+    PIE1bits.RC1IE = 0;
+    uart_rx_wakeup = 0;
+}
+
+void uart_wakeup(void)
+{
+    PIR1bits.TX1IF = 0;
+    PIR1bits.RC1IF = 0;
+    IPR1bits.TX1IP = 0;  // Low
+    IPR1bits.RC1IP = 1; // High
+    RCSTA = 0b10010000; //SPEN, 8bit, ASYNC, CREN
+    BAUDCON = 0b00001011; // IDLE High, BRG16, ABDEN, WUE
+    TXSTA = 0b00100000; //8bit, ASYNC, TXEN, Break
+    PIE1bits.TX1IE = 0;
+    PIE1bits.RC1IE = 1;
+}
+
+void uart_init(void)
+{
+    uart_rx_rptr = 0;
+    uart_rx_wptr = 0;
+    uart_rx_bytes = 0;
+    uart_rx_wakeup = 0;
+    uart_rx_xon = 0xff;
+    uart_tx_xon = 0xff;
+    uart_wakeup();
+}
+
+
+void uart_inthdr_rx(void)
+{
+    unsigned char c;
+
+    if(uart_rx_wakeup == 0){
+        if(BAUDCONbits.WUE != 1){ // Okay, Wakeup interrupt
+            uart_rx_wakeup = 0xff;
+        }
+        goto _l0; // exit
+    }
+    if(BAUDCONbits.ABDEN == 1) { // Still configure baudrate
+        goto _l0; // Exit
+    }
+
+    if(RCSTAbits.OERR == 0) {
+        if(RCSTAbits.FERR == 0) {
+            uart_rx_sts = 0; // Clear error
+            c = RCREG;
+            if(c == UART_CH_XOFF) {
+                uart_tx_xon = 0;  // XOFF Sequence for TX
+             } else if(c == UART_CH_XON) { // XON Sequence for TX
+                 uart_tx_xon = 0xff;
+             }  else if(uart_rx_bytes < UART_BUF_SIZE) { //
+                        uart_rfifo[uart_rx_wptr++] = c;
+                        if(uart_rx_wptr >= UART_BUF_SIZE) uart_rx_wptr = 0;
+                        uart_rx_bytes++;
+                        if(uart_rx_bytes >= UART_BUF_SIZE) uart_rx_bytes = UART_BUF_SIZE;
+             }
+        } else { // Frame Error
+         uart_rx_sts != UART_FRAMEERROR;
+        }
+    } else {
+         uart_rx_sts != UART_OVERFLOW;
+    }
+
+_l0:  // Exit
+ PIR1bits.RC1IF = 0;
+ return;
+}
+
+/*
+ * Pull char from RX FIFO.
+ */
+unsigned char uart_pullchar(void)
+{
+    unsigned char c;
+    while(PIR1bits.RC1IF == 1) {
+        idle(100); // Wait for RX completed
+    }
+    if(uart_rx_bytes <= 0) return 0; // EMPTY
+    c = uart_rfifo[uart_rx_rptr++];
+    if(uart_rx_rptr > UART_BUF_SIZE) uart_rx_rptr = 0;
+    uart_rx_bytes--;
+    if(uart_rx_bytes <= 0) uart_rx_bytes = 0;
+    return c;
+}
+
+unsigned char uart_pushchar(unsigned char c, unsigned int timeout)
+{
+    unsigned int i;
+    if(timeout != 0){
+        for(i = timeout; i > 0; i--){
+            if((uart_tx_xon != 0) && (TXSTAbits.TRMT1 == 1)) break;
+            idle(100);
+        }
+        if(i != 0) goto _l1;  // Send and return;
+        return UART_TIMEOUT; // Timeout
+    }
+    if((TXSTAbits.TRMT1 == 0) || (uart_tx_xon == 0)) return UART_TIMEOUT; // If timeout=0 return immidiately.
+_l1:
+    TXREG = c;
+    return 0;
+}
+
+void uart_pushxon(unsigned int timeout)
+{
+    unsigned int i;
+    if(uart_rx_wakeup == 0) return;
+    if(timeout != 0) {
+        for(i = timeout; i > 0; i--){
+            if(TXSTAbits.TRMT1 == 1) break;
+            idle(100); // 100us
+        }
+        return;
+    } else {
+        while(TXSTAbits.TRMT1 == 0) idle(100);
+    }
+    TXREG = UART_CH_XON;
+}
+
+void uart_pushxoff(unsigned int timeout)
+{
+    unsigned int i;
+    if(uart_rx_wakeup == 0) return;
+    if(timeout != 0) {
+        for(i = timeout; i > 0; i--){
+            if(TXSTAbits.TRMT1 == 1) break;
+            idle(100); // 100us
+        }
+        return;
+    } else {
+        while(TXSTAbits.TRMT1 == 0) idle(100);
+    }
+    TXREG = UART_CH_XOFF;
+}
+
+void uart_break(void)
+{
+    TXSTAbits.SENDB1 = 1;
+}
+
+unsigned char uart_getstat(void)
+{
+    unsigned char s = uart_rx_sts;
+    if(uart_rx_wakeup != 0) s |= UART_WAKEUP;
+    return s;
+}
\ No newline at end of file
diff --git a/euart.h b/euart.h
new file mode 100644 (file)
index 0000000..071df60
--- /dev/null
+++ b/euart.h
@@ -0,0 +1,59 @@
+/*
+ * OpenI2CRADIO
+ * EUSART Handler
+ * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2,
+ *  or (at your option) any later version.
+ *  This library / program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *  See the GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this library; see the file COPYING. If not, write to the
+ *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  As a special exception, if you link this(includeed from sdcc) library
+ *  with other files, some of which are compiled with SDCC,
+ *  to produce an executable, this library does not by itself cause
+ *  the resulting executable to be covered by the GNU General Public License.
+ *  This exception does not however invalidate any other reasons why
+ *  the executable file might be covered by the GNU General Public License.
+ */
+
+#ifndef EUART_H
+#define        EUART_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define UART_BUF_SIZE 256
+#define UART_CH_XOFF 19
+#define UART_CH_XON 17
+#define UART_FRAMEERROR 0x01
+#define UART_OVERFLOW 0x02
+#define UART_TIMEOUT 0x04
+#define UART_WAKEUP 0x80
+
+extern void uart_sleep(void);
+extern void uart_wakeup(void);
+extern void uart_init(void);
+extern void uart_inthdr_rx(void);
+extern unsigned char uart_pullchar(void);
+extern unsigned char uart_pushchar(unsigned char c, unsigned int timeout);
+extern void uart_pushxon(unsigned int timeout);
+extern void uart_pushxff(unsigned int timeout);
+extern void uart_break(void);
+extern unsigned char uart_getstat(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* EUART_H */
+
diff --git a/main.c b/main.c
index 1f774e9..2f1ea7b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -55,6 +55,7 @@
 #include "i2c_eeprom.h"
 #include "backlight.h"
 #include "rencoder.h"
+#include "euart.h"
 
 /*
  * Config words.
@@ -85,7 +86,7 @@
                XINST=OFF
 #endif
 
-#define USE_RENCODER
+extern char term_shell(unsigned int timeout);
 
 /*
  * Statuses
@@ -212,6 +213,14 @@ SIGHANDLER(I2CBus_handler)
     PIR2bits.BCLIF = 0;
 }
 
+#ifdef __XC
+void UART_R_Handler(void)
+#else
+SIGHANDLER(UART_R_Handler)
+#endif
+{
+    uart_inthdr_rx();
+}
 
 
 #ifdef __SDCC
@@ -223,7 +232,7 @@ END_DEF
 
 DEF_INTHIGH(inthigh_handler)
  DEF_HANDLER(SIG_RBIF, RBIF_handler)
-// DEF_HANDLER(SIG_EEIF, EEPROM_handler)
+ DEF_HANDLER(SIG_RCIF, UART_R_handler)
 // DEF_HANDLER(SIG_TMR1, TMR1_Handler)
 // DEF_HANDLER(SIG_INT1, EXINT_Handler)
  DEF_HANDLER(SIG_INT2, EXINT_Handler)
@@ -242,7 +251,7 @@ void interrupt low_priority intlow_handler(void)
 void interrupt high_priority inthigh_handler(void)
 {
     if(INTCONbits.RBIF) RBIF_handler();
-//    if(PIR2bits.EEIF)   EEPROM_handler();
+    if(PIR1bits.RC1IF)   UART_R_Handler();
     if(PIR1bits.TMR1IF) TMR1_Handler();
 //    if(PIR2bits.TMR3IF) TMR3_Handler();
 //    if(INTCON3bits.INT1IF) EXINT_Handler();
@@ -267,6 +276,7 @@ void lowbatt(void)
     shutdown(1);
 }
 
+
 int main(void)
 {
     unsigned char c;
@@ -315,6 +325,7 @@ int main(void)
   /* Push default parameters to AKC6955*/
     setup_akc6955();
     _CLS();
+    uart_init();
     //_LOCATE(0,0);
     _PUTCHAR(' ');
     update_status();
@@ -322,13 +333,10 @@ int main(void)
     ClrWdt();
     ui_idlekey = setup.ui_idlecount / 92;
     ui_idlepad = setup.ui_idlecount % 23;
-#ifdef USE_RENCODER
     rencoder_init();
    rencoder_start();
-#endif
    do {
 
-#if 1
         if(battlevel < 340) { // 3.4V
                 lvcount++;
                 if(lvcount > 4) {
@@ -341,10 +349,9 @@ int main(void)
             } else {
                 lvcount = 0;
             }
-#endif
         /* Main routine*/
 
-        
+        term_shell(0); // Steel Shell
        for(i = 0; i < 4; i++) {
            c = pollkey_single_timeout(ui_idlekey, 1); // 23*41 = 943ms
            p = 0;
@@ -357,9 +364,7 @@ int main(void)
                setfreq_updown(c);
                update_status();
                 update_display();
-#ifdef USE_RENCODER
                rencoder_start();
-#endif
             }
             ClrWdt();
         }
index 0b6d3f8..529eaf9 100644 (file)
@@ -45,17 +45,17 @@ OBJECTDIR=build/${CND_CONF}/${IMAGE_TYPE}
 DISTDIR=dist/${CND_CONF}/${IMAGE_TYPE}
 
 # Source Files Quoted if spaced
-SOURCEFILES_QUOTED_IF_SPACED=ui.c i2c_io.c main.c idle.c lcd_acm1602.c akc6955.c eeprom.c ioports.c menu.c power.c adc_int.c menu_defs.c eepromutil.c ui_updown.c ui_display.c radio_getstat.c helps.c menu_memoryfreq.c i2c_eeprom.c backlight.c menu_scan.c menu_setup.c menu_userband.c menu_volume.c rencoder.c
+SOURCEFILES_QUOTED_IF_SPACED=ui.c i2c_io.c main.c idle.c lcd_acm1602.c akc6955.c eeprom.c ioports.c menu.c power.c adc_int.c menu_defs.c eepromutil.c ui_updown.c ui_display.c radio_getstat.c helps.c menu_memoryfreq.c i2c_eeprom.c backlight.c menu_scan.c menu_setup.c menu_userband.c menu_volume.c rencoder.c euart.c uart_termio.c
 
 # Object Files Quoted if spaced
-OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/ui.p1 ${OBJECTDIR}/i2c_io.p1 ${OBJECTDIR}/main.p1 ${OBJECTDIR}/idle.p1 ${OBJECTDIR}/lcd_acm1602.p1 ${OBJECTDIR}/akc6955.p1 ${OBJECTDIR}/eeprom.p1 ${OBJECTDIR}/ioports.p1 ${OBJECTDIR}/menu.p1 ${OBJECTDIR}/power.p1 ${OBJECTDIR}/adc_int.p1 ${OBJECTDIR}/menu_defs.p1 ${OBJECTDIR}/eepromutil.p1 ${OBJECTDIR}/ui_updown.p1 ${OBJECTDIR}/ui_display.p1 ${OBJECTDIR}/radio_getstat.p1 ${OBJECTDIR}/helps.p1 ${OBJECTDIR}/menu_memoryfreq.p1 ${OBJECTDIR}/i2c_eeprom.p1 ${OBJECTDIR}/backlight.p1 ${OBJECTDIR}/menu_scan.p1 ${OBJECTDIR}/menu_setup.p1 ${OBJECTDIR}/menu_userband.p1 ${OBJECTDIR}/menu_volume.p1 ${OBJECTDIR}/rencoder.p1
-POSSIBLE_DEPFILES=${OBJECTDIR}/ui.p1.d ${OBJECTDIR}/i2c_io.p1.d ${OBJECTDIR}/main.p1.d ${OBJECTDIR}/idle.p1.d ${OBJECTDIR}/lcd_acm1602.p1.d ${OBJECTDIR}/akc6955.p1.d ${OBJECTDIR}/eeprom.p1.d ${OBJECTDIR}/ioports.p1.d ${OBJECTDIR}/menu.p1.d ${OBJECTDIR}/power.p1.d ${OBJECTDIR}/adc_int.p1.d ${OBJECTDIR}/menu_defs.p1.d ${OBJECTDIR}/eepromutil.p1.d ${OBJECTDIR}/ui_updown.p1.d ${OBJECTDIR}/ui_display.p1.d ${OBJECTDIR}/radio_getstat.p1.d ${OBJECTDIR}/helps.p1.d ${OBJECTDIR}/menu_memoryfreq.p1.d ${OBJECTDIR}/i2c_eeprom.p1.d ${OBJECTDIR}/backlight.p1.d ${OBJECTDIR}/menu_scan.p1.d ${OBJECTDIR}/menu_setup.p1.d ${OBJECTDIR}/menu_userband.p1.d ${OBJECTDIR}/menu_volume.p1.d ${OBJECTDIR}/rencoder.p1.d
+OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/ui.p1 ${OBJECTDIR}/i2c_io.p1 ${OBJECTDIR}/main.p1 ${OBJECTDIR}/idle.p1 ${OBJECTDIR}/lcd_acm1602.p1 ${OBJECTDIR}/akc6955.p1 ${OBJECTDIR}/eeprom.p1 ${OBJECTDIR}/ioports.p1 ${OBJECTDIR}/menu.p1 ${OBJECTDIR}/power.p1 ${OBJECTDIR}/adc_int.p1 ${OBJECTDIR}/menu_defs.p1 ${OBJECTDIR}/eepromutil.p1 ${OBJECTDIR}/ui_updown.p1 ${OBJECTDIR}/ui_display.p1 ${OBJECTDIR}/radio_getstat.p1 ${OBJECTDIR}/helps.p1 ${OBJECTDIR}/menu_memoryfreq.p1 ${OBJECTDIR}/i2c_eeprom.p1 ${OBJECTDIR}/backlight.p1 ${OBJECTDIR}/menu_scan.p1 ${OBJECTDIR}/menu_setup.p1 ${OBJECTDIR}/menu_userband.p1 ${OBJECTDIR}/menu_volume.p1 ${OBJECTDIR}/rencoder.p1 ${OBJECTDIR}/euart.p1 ${OBJECTDIR}/uart_termio.p1
+POSSIBLE_DEPFILES=${OBJECTDIR}/ui.p1.d ${OBJECTDIR}/i2c_io.p1.d ${OBJECTDIR}/main.p1.d ${OBJECTDIR}/idle.p1.d ${OBJECTDIR}/lcd_acm1602.p1.d ${OBJECTDIR}/akc6955.p1.d ${OBJECTDIR}/eeprom.p1.d ${OBJECTDIR}/ioports.p1.d ${OBJECTDIR}/menu.p1.d ${OBJECTDIR}/power.p1.d ${OBJECTDIR}/adc_int.p1.d ${OBJECTDIR}/menu_defs.p1.d ${OBJECTDIR}/eepromutil.p1.d ${OBJECTDIR}/ui_updown.p1.d ${OBJECTDIR}/ui_display.p1.d ${OBJECTDIR}/radio_getstat.p1.d ${OBJECTDIR}/helps.p1.d ${OBJECTDIR}/menu_memoryfreq.p1.d ${OBJECTDIR}/i2c_eeprom.p1.d ${OBJECTDIR}/backlight.p1.d ${OBJECTDIR}/menu_scan.p1.d ${OBJECTDIR}/menu_setup.p1.d ${OBJECTDIR}/menu_userband.p1.d ${OBJECTDIR}/menu_volume.p1.d ${OBJECTDIR}/rencoder.p1.d ${OBJECTDIR}/euart.p1.d ${OBJECTDIR}/uart_termio.p1.d
 
 # Object Files
-OBJECTFILES=${OBJECTDIR}/ui.p1 ${OBJECTDIR}/i2c_io.p1 ${OBJECTDIR}/main.p1 ${OBJECTDIR}/idle.p1 ${OBJECTDIR}/lcd_acm1602.p1 ${OBJECTDIR}/akc6955.p1 ${OBJECTDIR}/eeprom.p1 ${OBJECTDIR}/ioports.p1 ${OBJECTDIR}/menu.p1 ${OBJECTDIR}/power.p1 ${OBJECTDIR}/adc_int.p1 ${OBJECTDIR}/menu_defs.p1 ${OBJECTDIR}/eepromutil.p1 ${OBJECTDIR}/ui_updown.p1 ${OBJECTDIR}/ui_display.p1 ${OBJECTDIR}/radio_getstat.p1 ${OBJECTDIR}/helps.p1 ${OBJECTDIR}/menu_memoryfreq.p1 ${OBJECTDIR}/i2c_eeprom.p1 ${OBJECTDIR}/backlight.p1 ${OBJECTDIR}/menu_scan.p1 ${OBJECTDIR}/menu_setup.p1 ${OBJECTDIR}/menu_userband.p1 ${OBJECTDIR}/menu_volume.p1 ${OBJECTDIR}/rencoder.p1
+OBJECTFILES=${OBJECTDIR}/ui.p1 ${OBJECTDIR}/i2c_io.p1 ${OBJECTDIR}/main.p1 ${OBJECTDIR}/idle.p1 ${OBJECTDIR}/lcd_acm1602.p1 ${OBJECTDIR}/akc6955.p1 ${OBJECTDIR}/eeprom.p1 ${OBJECTDIR}/ioports.p1 ${OBJECTDIR}/menu.p1 ${OBJECTDIR}/power.p1 ${OBJECTDIR}/adc_int.p1 ${OBJECTDIR}/menu_defs.p1 ${OBJECTDIR}/eepromutil.p1 ${OBJECTDIR}/ui_updown.p1 ${OBJECTDIR}/ui_display.p1 ${OBJECTDIR}/radio_getstat.p1 ${OBJECTDIR}/helps.p1 ${OBJECTDIR}/menu_memoryfreq.p1 ${OBJECTDIR}/i2c_eeprom.p1 ${OBJECTDIR}/backlight.p1 ${OBJECTDIR}/menu_scan.p1 ${OBJECTDIR}/menu_setup.p1 ${OBJECTDIR}/menu_userband.p1 ${OBJECTDIR}/menu_volume.p1 ${OBJECTDIR}/rencoder.p1 ${OBJECTDIR}/euart.p1 ${OBJECTDIR}/uart_termio.p1
 
 # Source Files
-SOURCEFILES=ui.c i2c_io.c main.c idle.c lcd_acm1602.c akc6955.c eeprom.c ioports.c menu.c power.c adc_int.c menu_defs.c eepromutil.c ui_updown.c ui_display.c radio_getstat.c helps.c menu_memoryfreq.c i2c_eeprom.c backlight.c menu_scan.c menu_setup.c menu_userband.c menu_volume.c rencoder.c
+SOURCEFILES=ui.c i2c_io.c main.c idle.c lcd_acm1602.c akc6955.c eeprom.c ioports.c menu.c power.c adc_int.c menu_defs.c eepromutil.c ui_updown.c ui_display.c radio_getstat.c helps.c menu_memoryfreq.c i2c_eeprom.c backlight.c menu_scan.c menu_setup.c menu_userband.c menu_volume.c rencoder.c euart.c uart_termio.c
 
 
 CFLAGS=
@@ -278,6 +278,22 @@ ${OBJECTDIR}/rencoder.p1: rencoder.c  nbproject/Makefile-${CND_CONF}.mk
        @-${MV} ${OBJECTDIR}/rencoder.d ${OBJECTDIR}/rencoder.p1.d 
        @${FIXDEPS} ${OBJECTDIR}/rencoder.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
        
+${OBJECTDIR}/euart.p1: euart.c  nbproject/Makefile-${CND_CONF}.mk
+       @${MKDIR} ${OBJECTDIR} 
+       @${RM} ${OBJECTDIR}/euart.p1.d 
+       @${RM} ${OBJECTDIR}/euart.p1 
+       ${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  -D__DEBUG=1 --debugger=pickit3  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=require --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,-config,+clib,+plib --output=-mcof,+elf "--errformat=%f:%l: error: %s" "--warnformat=%f:%l: warning: %s" "--msgformat=%f:%l: advisory: %s"    -o${OBJECTDIR}/euart.p1  euart.c 
+       @-${MV} ${OBJECTDIR}/euart.d ${OBJECTDIR}/euart.p1.d 
+       @${FIXDEPS} ${OBJECTDIR}/euart.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
+       
+${OBJECTDIR}/uart_termio.p1: uart_termio.c  nbproject/Makefile-${CND_CONF}.mk
+       @${MKDIR} ${OBJECTDIR} 
+       @${RM} ${OBJECTDIR}/uart_termio.p1.d 
+       @${RM} ${OBJECTDIR}/uart_termio.p1 
+       ${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  -D__DEBUG=1 --debugger=pickit3  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=require --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,-config,+clib,+plib --output=-mcof,+elf "--errformat=%f:%l: error: %s" "--warnformat=%f:%l: warning: %s" "--msgformat=%f:%l: advisory: %s"    -o${OBJECTDIR}/uart_termio.p1  uart_termio.c 
+       @-${MV} ${OBJECTDIR}/uart_termio.d ${OBJECTDIR}/uart_termio.p1.d 
+       @${FIXDEPS} ${OBJECTDIR}/uart_termio.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
+       
 else
 ${OBJECTDIR}/ui.p1: ui.c  nbproject/Makefile-${CND_CONF}.mk
        @${MKDIR} ${OBJECTDIR} 
@@ -479,6 +495,22 @@ ${OBJECTDIR}/rencoder.p1: rencoder.c  nbproject/Makefile-${CND_CONF}.mk
        @-${MV} ${OBJECTDIR}/rencoder.d ${OBJECTDIR}/rencoder.p1.d 
        @${FIXDEPS} ${OBJECTDIR}/rencoder.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
        
+${OBJECTDIR}/euart.p1: euart.c  nbproject/Makefile-${CND_CONF}.mk
+       @${MKDIR} ${OBJECTDIR} 
+       @${RM} ${OBJECTDIR}/euart.p1.d 
+       @${RM} ${OBJECTDIR}/euart.p1 
+       ${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=require --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,-config,+clib,+plib --output=-mcof,+elf "--errformat=%f:%l: error: %s" "--warnformat=%f:%l: warning: %s" "--msgformat=%f:%l: advisory: %s"    -o${OBJECTDIR}/euart.p1  euart.c 
+       @-${MV} ${OBJECTDIR}/euart.d ${OBJECTDIR}/euart.p1.d 
+       @${FIXDEPS} ${OBJECTDIR}/euart.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
+       
+${OBJECTDIR}/uart_termio.p1: uart_termio.c  nbproject/Makefile-${CND_CONF}.mk
+       @${MKDIR} ${OBJECTDIR} 
+       @${RM} ${OBJECTDIR}/uart_termio.p1.d 
+       @${RM} ${OBJECTDIR}/uart_termio.p1 
+       ${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=require --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,-config,+clib,+plib --output=-mcof,+elf "--errformat=%f:%l: error: %s" "--warnformat=%f:%l: warning: %s" "--msgformat=%f:%l: advisory: %s"    -o${OBJECTDIR}/uart_termio.p1  uart_termio.c 
+       @-${MV} ${OBJECTDIR}/uart_termio.d ${OBJECTDIR}/uart_termio.p1.d 
+       @${FIXDEPS} ${OBJECTDIR}/uart_termio.p1.d $(SILENT) -rsi ${MP_CC_DIR}../  
+       
 endif
 
 # ------------------------------------------------------------------------------------
index 44f8339..91d1d90 100644 (file)
@@ -1,5 +1,5 @@
 #
-#Wed Oct 30 20:33:22 JST 2013
+#Fri Nov 01 00:51:04 JST 2013
 default.languagetoolchain.dir=/opt/microchip/xc8/v1.21/bin
 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=0d2b1469ad71adb787c711a416386331
 default.languagetoolchain.version=1.21
index ddb6be4..e195899 100644 (file)
@@ -74,6 +74,9 @@
     <itemPath>menu_ui.h</itemPath>
     <itemPath>rencoder.c</itemPath>
     <itemPath>rencoder.h</itemPath>
+    <itemPath>euart.c</itemPath>
+    <itemPath>euart.h</itemPath>
+    <itemPath>uart_termio.c</itemPath>
   </logicalFolder>
   <sourceRootList>
     <Elem>/usr/local/share/sdcc/lib/src/pic16/libc</Elem>
diff --git a/power.c b/power.c
index 04d2895..d4e469e 100644 (file)
--- a/power.c
+++ b/power.c
@@ -32,6 +32,7 @@
 #include "menu.h"
 #include "backlight.h"
 #include "rencoder.h"
+#include "euart.h"
 
 /*
  * Detect reset condition.
@@ -151,8 +152,8 @@ void power_off(unsigned char savef)
 {
     unsigned char sts;
 
+    uart_sleep();
     if(savef != 0) {
-
         save_eeprom();
         save_userbands();
     }
diff --git a/uart_termio.c b/uart_termio.c
new file mode 100644 (file)
index 0000000..fce6877
--- /dev/null
@@ -0,0 +1,343 @@
+/*
+ * OpenI2CRADIO
+ * EUSART Handler
+ * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2,
+ *  or (at your option) any later version.
+ *  This library / program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *  See the GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this library; see the file COPYING. If not, write to the
+ *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  As a special exception, if you link this(includeed from sdcc) library
+ *  with other files, some of which are compiled with SDCC,
+ *  to produce an executable, this library does not by itself cause
+ *  the resulting executable to be covered by the GNU General Public License.
+ *  This exception does not however invalidate any other reasons why
+ *  the executable file might be covered by the GNU General Public License.
+ */
+
+#if defined(__SDCC)
+#include <sdcc-lib.h>
+#include <pic18fregs.h> /* ONLY FOR PIC18x */
+#include <delay.h>
+#else
+#include <xc.h>
+#endif
+#include <signal.h>
+
+#include "iodef.h"
+#include "idle.h"
+#include "ioports.h"
+#include "euart.h"
+#include "menu.h"
+
+#define TERM_NONSREC -1
+#define TERM_STRERR -2
+#define TERM_SUMERR -3
+
+unsigned char uart_term_putstr(unsigned char *s)
+{
+    unsigned char p = 0;
+    if((uart_getstat() & UART_WAKEUP) == 0) return 0; // Error
+    while(s[p] != 0x00)
+    {
+        uart_pushchar(s[p], 0x0000);
+        p++;
+        if(p >= 255) return 0; // Overlen
+    }
+    return 0xff;
+}
+
+void uart_term_getstr(unsigned char *s, unsigned char maxlen, unsigned char echo)
+{
+    unsigned int i = 0;
+    unsigned char c = 0x00;
+    while(c != '\n'){
+        c = uart_pullchar();
+        if(c != 0x00) {
+            s[i] = c;
+            if(echo != 0) uart_pushchar(c, 0); // Echoback
+            i++;
+            if(i >= maxlen) break;
+        }
+    }
+    s[i] = '\0';
+}
+
+unsigned char c2h(unsigned char c)
+{
+    if(c < '1') return 0xff;
+    if(c > 'F') return 0xff;
+    if(c <= '9') return c - '0';
+    if(c >= 'A') return c - 'A' + 10;
+    return 0xff;
+}
+
+char str_hex2bin(unsigned char *s, unsigned char *p, unsigned char len)
+{
+    unsigned char sum = 0;
+    unsigned char pp;
+    unsigned char i;
+    unsigned char h, l;
+    unsigned char c;
+    // Get Header
+    if((s[0] != 'S') || (s[1] != '0')) {
+        // Header
+        return TERM_NONSREC;
+    }
+    i = 2;
+    for(pp = 0; pp <= len ; pp++){
+        h = c2h(s[i]);
+        if(h >= 0x10) return TERM_STRERR;
+        l = c2h(s[i + 1]);
+        if(l >= 0x10) return TERM_STRERR;
+        c =  (h << 8 ) + l;
+        sum += c;
+        p[pp] = c;
+        i += 2;
+    }
+    h = c2h(s[i]);
+    if(h >= 0x10) return TERM_STRERR;
+    l = c2h(s[i + 1]);
+    if(l >= 0x10) return TERM_STRERR;
+    c = (h << 8) + l;
+    if(c != sum) return TERM_SUMERR;
+    return 0;
+}
+
+unsigned char shell_strlen(char *s)
+{
+    unsigned char i = 0;
+    while(i < 128) {
+        if(s[i] == '\0') break;
+        i++;
+    }
+    return i;
+}
+
+unsigned char shell_gettok(char *dst, char *src)
+{
+    unsigned char i;
+    i = 0;
+    while(src[i] != '\0') {
+        if(src[i] == ' ') break;
+        if(src[i] == '\t') break;
+        dst[i] = src[i];
+        i++;
+        if(i > 128) break;
+    }
+    dst[i] = '\0';
+    return i;
+}
+
+int shell_strcmp(char *from, char *to)
+{
+    unsigned char f, t;
+    unsigned char i;
+    int p = 0;
+    
+    f = strlen(from);
+    t = strlen(to);
+    i = f;
+    if(i > t) i = t;
+    while(i != 0){
+        if(from[p] != to[p]) return -1;
+        p++;
+        i--;
+    }
+    return p;
+}
+
+#define SHELL_CMD_NONE -1
+#define SHELL_CMD_NOTFOUND -2
+#define SHELL_CMD_ILLTOKEN -3
+#define SHELL_CMD_TOOLONG -4
+#define SHELL_CMD_OK 0
+
+char shellstr[192];
+
+#define SHELL_TOPCMDS 16
+const char *cmdstr[SHELL_TOPCMDS] = {
+    "HELP",
+    "FREQ",
+    "STAT",
+    "LOAD",
+    "SAVE",
+    "FM",
+    "AM",
+    "POFF",
+    "PON",
+    "SEQ",
+    "LOG",
+    "SCAN",
+    "BAND",
+    "?",
+    "?",
+    "?"
+};
+
+long term_getuint(char *pool)
+{
+    long i = 0;
+    unsigned char p = 0;
+    unsigned char c;
+
+    do {
+        c = pool[p];
+        if((c < '0') || (c > '9')) return -1;
+        if(c == '\0') break;
+        i *= 10;
+        i = i + (c - '0');
+        p++;
+    } while(p <= 10);
+
+    return i;
+}
+
+void term_printnum(char *pool, unsigned int num)
+{
+    char p[5];
+    unsigned char i, j, ii;
+
+    for(i = 0; i < 5; i++){
+        p[i] = num % 10;
+        num = num / 10;
+    }
+    i = 4;
+    do {
+        if(p[i] != 0) break;
+        i--;
+    } while(i != 0);
+    if(i == 0){
+        pool[0] = '0';
+        pool[1] = '\0';
+        return;
+    }
+    ii = i;
+    for(j = 0; j <= ii; j++) {
+        pool[j] = p[i] + '0';
+        i--;
+    }
+    pool[j] = '\0';
+    return;
+}
+/*
+ * Set Frequency: Arg1 = Freq.
+ */
+static void term_freq(char *p)
+{
+    unsigned int freq;
+    long l;
+    char pp[6];
+
+    l = term_getuint(p);
+    uart_term_putstr("\nSet Freq:");
+    if((l < 0) || (l > 30000)) {
+        uart_term_putstr("\nE:Illegal freq range.\n");
+        return;
+    }
+    freq = (unsigned int)l;
+    if(setup.fm == 0){
+        term_printnum(pp, setup.amfreq);
+        // AM
+        setup.amfreq = freq;
+    } else {
+        term_printnum(pp, setup.fmfreq);
+        // FM
+        setup.fmfreq = freq;
+    }
+    uart_term_putstr(pp);
+    uart_term_putstr("->");
+    term_printnum(pp, freq);
+    uart_term_putstr(pp);
+
+    akc6955_set_freq(freq);
+    uart_term_putstr("\ndone.\n");
+}
+
+/*
+ * Print status
+ * Arg = none.
+ */
+static void term_printstatus(char *p)
+{
+    char pp[6];
+    uart_term_putstr("\nStatus:\n");
+    if(setup.fm == 0){
+        uart_term_putstr("AM \nBand = ");
+        term_printnum(pp, setup.amband);
+        uart_term_putstr(pp);
+        uart_term_putstr("\nFreq = ");
+        term_printnum(pp, setup.amfreq);
+        uart_term_putstr(pp);
+        uart_term_putstr("\n");
+    } else {
+        uart_term_putstr("FM \nBand = ");
+        term_printnum(pp, setup.fmband);
+        uart_term_putstr(pp);
+        uart_term_putstr("\nFreq = ");
+        term_printnum(pp, setup.fmfreq);
+        uart_term_putstr(pp);
+        uart_term_putstr("\n");
+    }
+    
+    uart_term_putstr("\n");
+}
+/*
+ * Open the Shell: Return cmdcode.
+ */
+char term_shell(unsigned int timeout)
+{
+    unsigned int t;
+    unsigned char pp;
+    unsigned char c;
+    int i;
+    unsigned int ii;
+    char pool[128];
+
+    shellstr[0] = '\0';
+    if(timeout != 0) {
+        t = timeout;
+        while((uart_getstat() & UART_WAKEUP) == 0) {
+            if(t == 0) return SHELL_CMD_NONE;
+            t--;
+            idle_time_ms(1);
+        }
+    } else {
+        if((uart_getstat() & UART_WAKEUP) == 0) return SHELL_CMD_NONE;
+    }
+    uart_term_putstr("\n$>");
+    uart_term_getstr(shellstr, 128, 1); // With Echo
+
+    ii = shell_gettok(pool, shellstr);
+    if(ii >= 128) return SHELL_CMD_TOOLONG;
+
+    for(t = 0; t < SHELL_TOPCMDS; t++) 
+    {
+        i = shell_strcmp((char *)cmdstr[t], pool);
+        if(i > 0) break;
+    }
+    if(i <= 0) return SHELL_CMD_NOTFOUND;
+    ii = shell_gettok(pool, &shellstr[ii]);
+    switch(t){
+        case 1: // Freq
+            term_freq(pool);
+            break;
+        case 2:
+            term_printstatus(pool);
+            break;
+        default:
+            break;
+    }
+
+    return SHELL_CMD_OK;
+}
\ No newline at end of file