From a293dbb321211e4b8d01822668b662a50523a912 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Sat, 12 Dec 2015 11:28:36 -0600 Subject: [PATCH 1/5] QT_GUI: Fix build of QT GUI under QT5 --- scripts/BusPirateGUI/GUI/BPSettings.cpp | 2 +- scripts/BusPirateGUI/GUI/BPSettings.h | 2 +- scripts/BusPirateGUI/GUI/BinMode.cpp | 2 +- scripts/BusPirateGUI/GUI/GUI.pro | 2 ++ scripts/BusPirateGUI/GUI/Interface.h | 2 +- scripts/BusPirateGUI/GUI/Interface_i2c.cpp | 4 ++-- scripts/BusPirateGUI/GUI/Interface_jtag.cpp | 2 +- scripts/BusPirateGUI/GUI/Interface_onewire.cpp | 2 +- scripts/BusPirateGUI/GUI/Interface_power.cpp | 2 +- scripts/BusPirateGUI/GUI/Interface_rawtext.cpp | 2 +- scripts/BusPirateGUI/GUI/Interface_rawwire.cpp | 2 +- scripts/BusPirateGUI/GUI/Interface_spi.cpp | 2 +- scripts/BusPirateGUI/GUI/MainWin.cpp | 2 +- scripts/BusPirateGUI/GUI/MainWin.h | 2 +- scripts/BusPirateGUI/GUI/main.cpp | 2 +- scripts/BusPirateGUI/qextserialport/posix_qextserialport.cpp | 4 ++-- 16 files changed, 19 insertions(+), 17 deletions(-) diff --git a/scripts/BusPirateGUI/GUI/BPSettings.cpp b/scripts/BusPirateGUI/GUI/BPSettings.cpp index 7eeb8959..4e9ba101 100644 --- a/scripts/BusPirateGUI/GUI/BPSettings.cpp +++ b/scripts/BusPirateGUI/GUI/BPSettings.cpp @@ -1,4 +1,4 @@ -#include +#include #include "qextserialport/qextserialport.h" #include "MainWin.h" #include "BPSettings.h" diff --git a/scripts/BusPirateGUI/GUI/BPSettings.h b/scripts/BusPirateGUI/GUI/BPSettings.h index 0adcb41a..97c91e9e 100644 --- a/scripts/BusPirateGUI/GUI/BPSettings.h +++ b/scripts/BusPirateGUI/GUI/BPSettings.h @@ -1,7 +1,7 @@ #ifndef __BPSETTINGS_H #define __BPSETTINGS_H -#include +#include class BinMode; class MainWidgetFrame; diff --git a/scripts/BusPirateGUI/GUI/BinMode.cpp b/scripts/BusPirateGUI/GUI/BinMode.cpp index ee75c164..d7e2f2e9 100644 --- a/scripts/BusPirateGUI/GUI/BinMode.cpp +++ b/scripts/BusPirateGUI/GUI/BinMode.cpp @@ -1,4 +1,4 @@ -#include +#include #include "qextserialport/qextserialport.h" #include "Events.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/GUI.pro b/scripts/BusPirateGUI/GUI/GUI.pro index ed21a49c..e6024866 100644 --- a/scripts/BusPirateGUI/GUI/GUI.pro +++ b/scripts/BusPirateGUI/GUI/GUI.pro @@ -10,6 +10,8 @@ CONFIG += debug_and_release CONFIG += staticlib CONFIG += static +QT += widgets + CONFIG(debug, debug|release) { LIBS += -L../qextserialport/build -lqextserialportd } else { diff --git a/scripts/BusPirateGUI/GUI/Interface.h b/scripts/BusPirateGUI/GUI/Interface.h index d0d88232..76dfce09 100644 --- a/scripts/BusPirateGUI/GUI/Interface.h +++ b/scripts/BusPirateGUI/GUI/Interface.h @@ -1,7 +1,7 @@ #ifndef __IFACE_H #define __IFACE_H -#include +#include enum bbio_pins { diff --git a/scripts/BusPirateGUI/GUI/Interface_i2c.cpp b/scripts/BusPirateGUI/GUI/Interface_i2c.cpp index 27605930..dfe7a872 100644 --- a/scripts/BusPirateGUI/GUI/Interface_i2c.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_i2c.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" @@ -108,7 +108,7 @@ void I2CGui::search_i2c() parent->bp->i2c_stop(); dev_addr = QString("%1 (%2 %3)").arg(dev, 0, 16).arg(addr, 0, 16).arg(rw); - postMsgEvent(dev_addr.toAscii()); + postMsgEvent(dev_addr.toLatin1()); } QCoreApplication::sendEvent(parent->parent, new BPStatusMsgEvent(end_msg)); } diff --git a/scripts/BusPirateGUI/GUI/Interface_jtag.cpp b/scripts/BusPirateGUI/GUI/Interface_jtag.cpp index d99608d1..fe640ef2 100644 --- a/scripts/BusPirateGUI/GUI/Interface_jtag.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_jtag.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/Interface_onewire.cpp b/scripts/BusPirateGUI/GUI/Interface_onewire.cpp index 79541be0..529d43ee 100644 --- a/scripts/BusPirateGUI/GUI/Interface_onewire.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_onewire.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/Interface_power.cpp b/scripts/BusPirateGUI/GUI/Interface_power.cpp index f0a8b378..29578df7 100644 --- a/scripts/BusPirateGUI/GUI/Interface_power.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_power.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/Interface_rawtext.cpp b/scripts/BusPirateGUI/GUI/Interface_rawtext.cpp index 59d0a9d9..ae305540 100644 --- a/scripts/BusPirateGUI/GUI/Interface_rawtext.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_rawtext.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/Interface_rawwire.cpp b/scripts/BusPirateGUI/GUI/Interface_rawwire.cpp index cc7d4d8a..15ce1b9a 100644 --- a/scripts/BusPirateGUI/GUI/Interface_rawwire.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_rawwire.cpp @@ -1,4 +1,4 @@ -#include +#include #include "BinMode.h" #include "BPSettings.h" #include "MainWin.h" diff --git a/scripts/BusPirateGUI/GUI/Interface_spi.cpp b/scripts/BusPirateGUI/GUI/Interface_spi.cpp index 1691ad1b..bb7bdf65 100644 --- a/scripts/BusPirateGUI/GUI/Interface_spi.cpp +++ b/scripts/BusPirateGUI/GUI/Interface_spi.cpp @@ -1,4 +1,4 @@ -#include +#include #include "configure.h" #include "BinMode.h" #include "BPSettings.h" diff --git a/scripts/BusPirateGUI/GUI/MainWin.cpp b/scripts/BusPirateGUI/GUI/MainWin.cpp index d1cee3e8..be63e973 100644 --- a/scripts/BusPirateGUI/GUI/MainWin.cpp +++ b/scripts/BusPirateGUI/GUI/MainWin.cpp @@ -1,4 +1,4 @@ -#include +#include #include "Interface.h" #include "BPSettings.h" #include "BinMode.h" diff --git a/scripts/BusPirateGUI/GUI/MainWin.h b/scripts/BusPirateGUI/GUI/MainWin.h index 6dca11f2..705ecb1b 100644 --- a/scripts/BusPirateGUI/GUI/MainWin.h +++ b/scripts/BusPirateGUI/GUI/MainWin.h @@ -1,7 +1,7 @@ #ifndef __QMAIN_H #define __QMAIN_H -#include +#include class SpiGui; class JtagGui; diff --git a/scripts/BusPirateGUI/GUI/main.cpp b/scripts/BusPirateGUI/GUI/main.cpp index f3bc480f..630c4cdb 100644 --- a/scripts/BusPirateGUI/GUI/main.cpp +++ b/scripts/BusPirateGUI/GUI/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include "MainWin.h" diff --git a/scripts/BusPirateGUI/qextserialport/posix_qextserialport.cpp b/scripts/BusPirateGUI/qextserialport/posix_qextserialport.cpp index 19aa1dce..578a48d8 100644 --- a/scripts/BusPirateGUI/qextserialport/posix_qextserialport.cpp +++ b/scripts/BusPirateGUI/qextserialport/posix_qextserialport.cpp @@ -728,9 +728,9 @@ bool QextSerialPort::open(OpenMode mode) if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { - qDebug() << "trying to open file" << port.toAscii(); + qDebug() << "trying to open file" << port.toLatin1(); //note: linux 2.6.21 seems to ignore O_NDELAY flag - if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) { + if ((fd = ::open(port.toLatin1() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) { qDebug("file opened succesfully"); setOpenMode(mode); // Flag the port as opened From 9507687fda99fba2b7617926ebd7e94c756e844b Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Wed, 30 Mar 2016 16:11:02 -0500 Subject: [PATCH 2/5] automatic: dos2unix conversion --- BP4_XSVFplayer_Win/buspirate.c | 486 +- BP4_XSVFplayer_Win/buspirate.h | 52 +- BP4_XSVFplayer_Win/main.c | 792 ++-- BP4_XSVFplayer_Win/serial.c | 894 ++-- BP4_XSVFplayer_Win/serial.h | 30 +- BPv3-bootloader/upgrader-v2tov4/firmware.h | 314 +- BPv3-bootloader/upgrader-v2tov4/flash.h | 40 +- BPv3-bootloader/upgrader-v2tov4/hardware.h | 104 +- BPv3-bootloader/upgrader-v2tov4/hardwarev0a.h | 206 +- BPv3-bootloader/upgrader-v2tov4/hardwarev1a.h | 228 +- BPv3-bootloader/upgrader-v2tov4/hardwarev25.h | 226 +- BPv3-bootloader/upgrader-v2tov4/hardwarev2a.h | 218 +- BPv3-bootloader/upgrader-v2tov4/hardwarev3.h | 234 +- BPv3-bootloader/upgrader-v2tov4/main.c | 668 +-- BPv3-bootloader/upgrader-v4tov4/firmware.h | 342 +- BPv3-bootloader/upgrader-v4tov4/flash.h | 42 +- BPv3-bootloader/upgrader-v4tov4/hardware.h | 112 +- BPv3-bootloader/upgrader-v4tov4/hardwarev0a.h | 206 +- BPv3-bootloader/upgrader-v4tov4/hardwarev1a.h | 228 +- BPv3-bootloader/upgrader-v4tov4/hardwarev25.h | 226 +- BPv3-bootloader/upgrader-v4tov4/hardwarev2a.h | 218 +- BPv3-bootloader/upgrader-v4tov4/hardwarev3.h | 234 +- BPv3-bootloader/upgrader-v4tov4/main.c | 742 ++-- BPv4-bootloader/firmware-v1/Compiler.h | 402 +- BPv4-bootloader/firmware-v1/GenericTypeDefs.h | 1096 ++--- BPv4-bootloader/firmware-v1/boot_config.h | 184 +- BPv4-bootloader/firmware-v1/bootloader.c | 518 +-- BPv4-bootloader/firmware-v1/bootloader.h | 940 ++-- BPv4-bootloader/firmware-v1/cdc.c | 1088 ++--- BPv4-bootloader/firmware-v1/cdc.h | 230 +- BPv4-bootloader/firmware-v1/configwords.h | 64 +- BPv4-bootloader/firmware-v1/descriptors.h | 324 +- BPv4-bootloader/firmware-v1/globals.h | 74 +- BPv4-bootloader/firmware-v1/main.c | 302 +- BPv4-bootloader/firmware-v1/picusb.h | 1074 ++--- BPv4-bootloader/firmware-v1/usb_lang.h | 242 +- BPv4-bootloader/firmware-v1/usb_stack.c | 1450 +++--- BPv4-bootloader/firmware-v1/usb_stack.h | 412 +- BPv4-bootloader/pirate-loader/pirate-loader.c | 2512 +++++------ Firmware-Demos/ADC2UART/main.c | 228 +- Firmware-Demos/Blink_MODE_LED/main.c | 74 +- Firmware-Demos/BusPirateADCDemo/main.c | 128 +- .../BusPirateUARTDEMOTEST/main.c | 74 +- Firmware/1wire.c | 1320 +++--- Firmware/1wire.h | 74 +- Firmware/1wire_lib.c | 356 +- Firmware/1wire_lib.h | 120 +- Firmware/AUXpin.c | 1490 +++---- Firmware/AUXpin.h | 60 +- Firmware/Compiler.h | 402 +- Firmware/GenericTypeDefs.h | 1096 ++--- Firmware/HD44780.c | 848 ++-- Firmware/HD44780.h | 70 +- Firmware/I2C.c | 2038 ++++----- Firmware/I2C.h | 62 +- Firmware/JTR_SUMP.c | 620 +-- Firmware/OpenOCD.c | 626 +-- Firmware/OpenOCD.h | 12 +- Firmware/SPI.c | 1668 +++---- Firmware/SPI.h | 66 +- Firmware/SUMP.c | 520 +-- Firmware/SUMP.h | 10 +- Firmware/UART.c | 1654 +++---- Firmware/UART.h | 60 +- Firmware/base.c | 458 +- Firmware/base.h | 594 +-- Firmware/baseIO.c | 1312 +++--- Firmware/baseIO.h | 278 +- Firmware/baseUI.c | 160 +- Firmware/baseUI.h | 60 +- Firmware/basic.c | 3350 +++++++------- Firmware/basic.h | 292 +- Firmware/binIO.c | 794 ++-- Firmware/binIO.h | 38 +- Firmware/binIOhelpers.c | 162 +- Firmware/binIOhelpers.h | 12 +- Firmware/binwire.c | 1224 +++--- Firmware/binwire.h | 4 +- Firmware/bitbang.c | 584 +-- Firmware/bitbang.h | 100 +- Firmware/busPirateCore.c | 794 ++-- Firmware/busPirateCore.h | 246 +- Firmware/configwords.h | 44 +- Firmware/descriptors.h | 268 +- Firmware/dio.c | 94 +- Firmware/dio.h | 40 +- Firmware/dp_usb/cdc.c | 868 ++-- Firmware/dp_usb/cdc.h | 214 +- Firmware/dp_usb/picusb.h | 1068 ++--- Firmware/dp_usb/usb_stack.c | 1196 ++--- Firmware/dp_usb/usb_stack.h | 578 +-- Firmware/dp_usb/usb_stack_globals.h | 32 +- Firmware/globals.h | 44 +- Firmware/hardwarev1a.h | 316 +- Firmware/hardwarev3.h | 346 +- Firmware/hardwarev4a.h | 558 +-- Firmware/jtag.c | 786 ++-- Firmware/jtag.h | 66 +- Firmware/jtag/micro.c | 4 +- Firmware/main.c | 490 +-- Firmware/onboardEEPROM.c | 282 +- Firmware/onboardEEPROM.h | 86 +- Firmware/pc_at_keyboard.c | 626 +-- Firmware/pc_at_keyboard.h | 48 +- Firmware/pic.c | 882 ++-- Firmware/pic.h | 98 +- Firmware/prj_usb_config.h | 98 +- Firmware/procMenu.c | 3896 ++++++++--------- Firmware/procMenu.h | 98 +- Firmware/raw2wire.c | 598 +-- Firmware/raw2wire.h | 80 +- Firmware/raw3wire.c | 414 +- Firmware/raw3wire.h | 70 +- Firmware/selftest.c | 548 +-- Firmware/selftest.h | 32 +- Firmware/smps.c | 142 +- Firmware/smps.h | 40 +- Firmware/translations/BPv3_en_US.h | 516 +-- Firmware/translations/BPv4_en_US.h | 596 +-- Firmware/translations/tools/translate/main.c | 456 +- Firmware/uart2io.c | 196 +- Firmware/uart2io.h | 42 +- Firmware/usb_lang.h | 242 +- firmware-STK500v2/UART.c | 78 +- firmware-STK500v2/UART.h | 8 +- firmware-STK500v2/base.c | 142 +- firmware-STK500v2/base.h | 128 +- firmware-STK500v2/command.h | 234 +- firmware-STK500v2/hardwarev1a.h | 228 +- firmware-STK500v2/hardwarev3.h | 220 +- firmware-STK500v2/main.c | 56 +- firmware-STK500v2/redefines.h | 38 +- firmware-STK500v2/spi.c | 198 +- firmware-XSVFplayer/UART.c | 52 +- firmware-XSVFplayer/UART.h | 14 +- firmware-XSVFplayer/base.c | 266 +- firmware-XSVFplayer/base.h | 116 +- firmware-XSVFplayer/hardwarev0a.h | 174 +- firmware-XSVFplayer/hardwarev1a.h | 186 +- firmware-XSVFplayer/hardwarev25.h | 184 +- firmware-XSVFplayer/hardwarev2a.h | 184 +- firmware-XSVFplayer/jtag.c | 766 ++-- firmware-XSVFplayer/jtag.h | 66 +- firmware-XSVFplayer/jtag/micro.c | 4 +- firmware-XSVFplayer/jtag/ports.c | 82 +- firmware-XSVFplayer/jtag/ports.h | 20 +- firmware-XSVFplayer/main.c | 90 +- .../pirate-loader-v4-source/pirate-loader.c | 2512 +++++------ .../ft232testapp0/ft232testapp0/ftd2xx.h | 2022 ++++----- .../ft232testapp0/ft232testapp0/stdafx.h | 30 +- .../ft232testapp0/ft232testapp0/targetver.h | 16 +- scripts/BPXSVFPlayer/buspirate.c | 268 +- scripts/BPXSVFPlayer/buspirate.h | 30 +- scripts/BPXSVFPlayer/main.c | 684 +-- scripts/BPXSVFPlayer/serial.c | 94 +- scripts/BPXSVFPlayer/serial.h | 30 +- scripts/HVPselftest/main.c | 614 +-- scripts/I2CEEPROMWIN.c | 278 +- scripts/powertools/BP_MMA_Selftest/main.c | 646 +-- scripts/powertools/HVPselftest/main.c | 574 +-- scripts/powertools/LCDv2-tester/main-text.c | 560 +-- scripts/powertools/LCDv2-tester/main.c | 610 +-- .../SPISniffer/linux-version/buspirate.c | 212 +- .../SPISniffer/linux-version/buspirate.h | 24 +- .../SPISniffer/linux-version/main.c | 662 +-- .../SPISniffer/linux-version/serial.c | 102 +- .../SPISniffer/linux-version/serial.h | 30 +- scripts/powertools/SPISniffer/main.c | 588 +-- scripts/powertools/framework/buspirate.c | 262 +- scripts/powertools/framework/buspirate.h | 30 +- scripts/powertools/framework/serial.c | 88 +- scripts/powertools/framework/serial.h | 30 +- scripts/powertools/self-test/main.c | 622 +-- 173 files changed, 37071 insertions(+), 37071 deletions(-) diff --git a/BP4_XSVFplayer_Win/buspirate.c b/BP4_XSVFplayer_Win/buspirate.c index 665b4523..51a3ff0d 100644 --- a/BP4_XSVFplayer_Win/buspirate.c +++ b/BP4_XSVFplayer_Win/buspirate.c @@ -1,243 +1,243 @@ -#include -#include -#include -#include -#include "serial.h" -#include "buspirate.h" - -//#ifndef WIN32 -#define TRUE 1 -#define FALSE 0 -//#define Sleep(x) usleep(x) -#define usleep(x) Sleep(x) -//#endif - -const char *modes[]={ - "BBIO", - "SPI", - "I2C", - "ART", - "1W0", - "RAW" -// Added JM - , - "0x06", - "0x07", - "0x08", - "0x09", - "0x0a", - "0x0b", - "0x0c", - "0x0d", - "0x0e", - "0x0f", - "0x10", - "0x11", - "0x12", - "0x13", - "0x14", - "0x15", - "0x16", - "0x17", - "XSVF" -// End added JM -}; - -//static struct BP_t pBP; -//static uint8_t BP_reversebyte(uint8_t c); -//static char bpbuf[4096]; -//static int bpbufcnt; -extern int disable_comport; -extern int dumphandle; -extern int verbose; -extern int modem; -//extern char *modes[]; -//low lever send command, get reply function - -uint32_t BP_WriteToPirate(int fd, char * val) { - int res = -1; - char ret = 0; - - - serial_write(fd, val, 1); - Sleep(1); - res = serial_read(fd, &ret, 1); - - if( ret != '\x01') { - if (modem==TRUE){ - printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); - return 0; - } - else { - printf(" ERROR: BusPirate replied with 0x%x instead of 0x01 \n",ret); - return -1; - } - } - //printf(" BusPirate said: OK\n"); - return 0; -} - -uint32_t BP_WriteToPirateNoCheck(int fd, char * val) { - int res = -1; - char ret = 0; - - - serial_write(fd, val, 1); - Sleep(1); - res = serial_read(fd, &ret, 1); - - return 0; -} - -int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not -{ - int ret; - char tmp[100] = { [0 ... 20] = 0x00 }; - int done = 0; - //int cmd_sent = 0; - int tries=0; - - printf(" Entering binary mode ... "); //--- modified JM - - if (fd==-1) //added because the fd has already returned null - { - printf("Port does not exist!"); - ret=ERR; - - } - else - { - - - while (!done) { - tmp[0]=0x00; - // printf("Sending 0X%X to port\n",tmp[0]); - serial_write(fd, tmp, 1); - tries++; - // printf("tries: %i Ret %i\n",tries,ret); - usleep(1); - ret = serial_read(fd, tmp, 5); -//--- Added JM - // printf("tries: %i Ret %i\n",tries,ret); -//--- End added JM - if (modem==TRUE) - { - printf("\n Modem Responded = %i\n",ret); - done=1; - } - else { - if (ret != 5 && tries>20) { - fprintf(stderr, " Buspirate did not respond correctly :( %i \n", ret ); - ret=ERR; - } - else if (strncmp(tmp, "BBIO1", 5) == 0) { -//--- Added JM - printf("ok\n"); -//--- End added JM - done=1; - ret=BBIO; - } - } - - if (tries>25){ - printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); - break; - ret=ERR; - } - } - } - return ret; -} - - - - -int BP_EnableMode(int fd, char bbmode) -{ - int ret,response,current_mode,c; - char tmp[100] = { [0 ... 20] = 0x00 }; - - //int cmd_sent = 0; - int tries=0; - current_mode=(int)bbmode; - printf(" Switching to %s mode ... ",modes[current_mode]); //--- Modified JM - - - tmp[0] = bbmode; - //printf("Sending 0X%X to port\n",tmp[0]); - serial_write(fd, tmp, 1); - tries++; - usleep(1); - ret = serial_read(fd, tmp, 4); - if (modem==TRUE) - { - printf(" Modem Responded = %i with value of ",ret); - if(ret>0){ - for(c=0; c +#include +#include +#include +#include "serial.h" +#include "buspirate.h" + +//#ifndef WIN32 +#define TRUE 1 +#define FALSE 0 +//#define Sleep(x) usleep(x) +#define usleep(x) Sleep(x) +//#endif + +const char *modes[]={ + "BBIO", + "SPI", + "I2C", + "ART", + "1W0", + "RAW" +// Added JM + , + "0x06", + "0x07", + "0x08", + "0x09", + "0x0a", + "0x0b", + "0x0c", + "0x0d", + "0x0e", + "0x0f", + "0x10", + "0x11", + "0x12", + "0x13", + "0x14", + "0x15", + "0x16", + "0x17", + "XSVF" +// End added JM +}; + +//static struct BP_t pBP; +//static uint8_t BP_reversebyte(uint8_t c); +//static char bpbuf[4096]; +//static int bpbufcnt; +extern int disable_comport; +extern int dumphandle; +extern int verbose; +extern int modem; +//extern char *modes[]; +//low lever send command, get reply function + +uint32_t BP_WriteToPirate(int fd, char * val) { + int res = -1; + char ret = 0; + + + serial_write(fd, val, 1); + Sleep(1); + res = serial_read(fd, &ret, 1); + + if( ret != '\x01') { + if (modem==TRUE){ + printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); + return 0; + } + else { + printf(" ERROR: BusPirate replied with 0x%x instead of 0x01 \n",ret); + return -1; + } + } + //printf(" BusPirate said: OK\n"); + return 0; +} + +uint32_t BP_WriteToPirateNoCheck(int fd, char * val) { + int res = -1; + char ret = 0; + + + serial_write(fd, val, 1); + Sleep(1); + res = serial_read(fd, &ret, 1); + + return 0; +} + +int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not +{ + int ret; + char tmp[100] = { [0 ... 20] = 0x00 }; + int done = 0; + //int cmd_sent = 0; + int tries=0; + + printf(" Entering binary mode ... "); //--- modified JM + + if (fd==-1) //added because the fd has already returned null + { + printf("Port does not exist!"); + ret=ERR; + + } + else + { + + + while (!done) { + tmp[0]=0x00; + // printf("Sending 0X%X to port\n",tmp[0]); + serial_write(fd, tmp, 1); + tries++; + // printf("tries: %i Ret %i\n",tries,ret); + usleep(1); + ret = serial_read(fd, tmp, 5); +//--- Added JM + // printf("tries: %i Ret %i\n",tries,ret); +//--- End added JM + if (modem==TRUE) + { + printf("\n Modem Responded = %i\n",ret); + done=1; + } + else { + if (ret != 5 && tries>20) { + fprintf(stderr, " Buspirate did not respond correctly :( %i \n", ret ); + ret=ERR; + } + else if (strncmp(tmp, "BBIO1", 5) == 0) { +//--- Added JM + printf("ok\n"); +//--- End added JM + done=1; + ret=BBIO; + } + } + + if (tries>25){ + printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); + break; + ret=ERR; + } + } + } + return ret; +} + + + + +int BP_EnableMode(int fd, char bbmode) +{ + int ret,response,current_mode,c; + char tmp[100] = { [0 ... 20] = 0x00 }; + + //int cmd_sent = 0; + int tries=0; + current_mode=(int)bbmode; + printf(" Switching to %s mode ... ",modes[current_mode]); //--- Modified JM + + + tmp[0] = bbmode; + //printf("Sending 0X%X to port\n",tmp[0]); + serial_write(fd, tmp, 1); + tries++; + usleep(1); + ret = serial_read(fd, tmp, 4); + if (modem==TRUE) + { + printf(" Modem Responded = %i with value of ",ret); + if(ret>0){ + for(c=0; c - - - -#endif - - -#define ERR -1 -#define BBIO 0x00 -#define SPI 0x01 -#define I2C 0x02 -#define ART 0x03 -#define OWD 0x04 -#define RAW 0X05 -// Added JM -#define JTAG 0x18 -// End added JM - - -uint32_t BP_WriteToPirate(int , char *); -int BP_EnableBinary(int); -int BP_EnableMode(int , char ); -uint32_t BP_WriteToPirateNoCheck(int fd, char * val); +#ifndef BUSPIRATE_h_ +#define BUSPIRATE_h_ + +#include + + + +#endif + + +#define ERR -1 +#define BBIO 0x00 +#define SPI 0x01 +#define I2C 0x02 +#define ART 0x03 +#define OWD 0x04 +#define RAW 0X05 +// Added JM +#define JTAG 0x18 +// End added JM + + +uint32_t BP_WriteToPirate(int , char *); +int BP_EnableBinary(int); +int BP_EnableMode(int , char ); +uint32_t BP_WriteToPirateNoCheck(int fd, char * val); diff --git a/BP4_XSVFplayer_Win/main.c b/BP4_XSVFplayer_Win/main.c index f8198d9f..68fa17e9 100644 --- a/BP4_XSVFplayer_Win/main.c +++ b/BP4_XSVFplayer_Win/main.c @@ -1,396 +1,396 @@ -#include -#include -#include -#include -#include -#include -#ifdef WIN32 -#include - #include - -#else -//#include -#endif - -#include "serial.h" -#include "buspirate.h" - - -#define JTAG_RESET 0x01 -#define JTAG_CHAIN_SCAN 0x02 -#define XSVF_PLAYER 0x03 - - -#define XSVF_ERROR_NONE 0x00 -#define XSVF_ERROR_UNKNOWN 0x01 -#define XSVF_ERROR_TDOMISMATCH 0x02 -#define XSVF_ERROR_MAXRETRIES 0x03 -#define XSVF_ERROR_ILLEGALCMD 0x04 -#define XSVF_ERROR_ILLEGALSTATE 0x05 -#define XSVF_ERROR_DATAOVERFLOW 0x06 -#define XSVF_ERROR_LAST 0x07 -#define XSVF_READY_FOR_DATA 0xFF - -//#ifndef WIN32 -#define usleep(x) Sleep(x); -//#define Sleep(x) usleep(x); -#define TRUE 1 -#define FALSE 0 -//#endif - -char *dumpfile; -#ifdef WIN32 -HANDLE dumphandle; -#endif - -int modem =FALSE; -int cnt=0; -uint8_t *bin_buf; -uint32_t bin_buf_size; -#define FREE(x) if(x) free(x); -#define MAX_BUFFER 4096 -//#define MAX_BUFFER 255 //255 bytes - -//http://www.whereisian.com/files/j-xsvf_002.swf - -int print_usage(char * appname) -{ - //print usage - printf("\n"); - printf("\n"); - printf(" Help Menu\n"); - printf(" Usage: \n"); - printf(" %s -p device -f filename.xsvf -s speed [-x] [-r] \n ",appname); - printf("\n"); - printf(" Example Usage: %s -p COM1 -s 115200 -f example.xsvf \n",appname); - printf("\n"); - printf(" Where: -p device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf(" -f Filename of XSVF file \n"); - printf(" -x Perform a JTAG Chain Scan by sending 0x02 command. -f is optional. \n"); - printf(" -r Perform a JTAG Reset Scan by sending 0x01 command. -f is optional. \n"); - printf("\n"); - - printf("-----------------------------------------------------------------------------\n"); - - - return 0; -} - -int main(int argc, char** argv) -{ - int opt; - uint8_t buffer[MAX_BUFFER]={0}; - uint8_t temp[2]={0}; // command buffer -// struct stat stbuf; - int fd,timeout_counter; - int res,c, nparam_bytechunks, bytePointer, readSize; - long fileSize; - FILE *XSVF; -// int xsvf; - int timer_out=0; - char *param_port = NULL; - char *param_speed = NULL; - char *param_XSVF=NULL; - char *param_bytechunks=NULL; - int jtag_reset=FALSE; - int chainscan=FALSE; - - const char *XSVF_ERROR[]={ "XSVF_ERROR_NONE", - "XSVF_ERROR_UNKNOWN", - "XSVF_ERROR_TDOMISMATCH", - "XSVF_ERROR_MAXRETRIES", - "XSVF_ERROR_ILLEGALCMD", - "XSVF_ERROR_ILLEGALSTATE", - "XSVF_ERROR_DATAOVERFLOW", - "XSVF_ERROR_LAST", - "XSVF_READY_FOR_DATA", - 0 }; - - printf("-----------------------------------------------------------------------------\n"); - printf("\n"); - printf(" BusPirate XSVF Player V.02\n"); - printf(" http://www.dangerousprototypes.com\n"); - printf("\n"); - printf("-----------------------------------------------------------------------------\n"); - - - - if (argc <= 1) { - print_usage(argv[0]); - exit(-1); - } - - - while ((opt = getopt(argc, argv, "s:p:f:rx")) != -1) { - - switch (opt) { - case 'p': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf("Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 'r': - jtag_reset=TRUE; - break; - case 'x': - chainscan=TRUE; - break; - case 'f': - if (param_XSVF != NULL) { - printf(" No XSVF file \n"); - exit(-1); - } - param_XSVF = strdup(optarg); - - break; - - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - exit(-1); - break; - } - } - - if (param_port==NULL){ - printf(" No serial port specified\n"); - print_usage(argv[0]); - exit(-1); - } - - nparam_bytechunks=MAX_BUFFER; - - if (param_speed==NULL) { - param_speed=strdup("115200"); //default is 115200kbps - } - - - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - - //setup port and speed - serial_setup(fd,(speed_t) atoi(param_speed)); - -//--- Added JM - res = BP_EnableBinary(fd); // Activate Binary mode (Sends 20 OxOO) - if (res == ERR){ - printf (" Unable to activate BP4 Binary mode \n"); - return -1; - }; - res = BP_EnableMode(fd,0x18); // Activate XSVF Player Sub-Mode - if (res == ERR){ - printf (" Unable to activate XSVF player mode \n"); - return -1; - }; - -//--- End added JM - - if (jtag_reset==TRUE){ - printf(" Performing Reset..\n"); - temp[0]=0x01; - serial_write( fd, (char *)temp, 1 ); - Sleep(1); - printf(" Done \n\n"); - } - - // perform Chain scan - // Send 0x02 to perform a chain scan - // Wait for 1 byte, the number of bytes that will be returned - // Get that many bytes - if (chainscan==TRUE) { - printf(" Performing Chain Scan..\n"); - temp[0]=0x02; - serial_write( fd, (char *)temp, 1 ); - Sleep(1); - res=0; - timeout_counter=0; - -// while (res==0) { - while ((res<=0) && (timeout_counter < 5)) { // Modified JM - res= serial_read(fd, (char *)buffer, sizeof(buffer)); - Sleep(1); - timeout_counter++; - } // Line Added JM - -/* //--- Removed JM - if (timeout_counter>=5) { - printf(" Got no reply for a Chain scan\n"); - } else { - printf(" Waiting for a chain scan reply\n"); - } - } //--- End removed JM */ - - if (res >0) { - printf(" Chain Scan Result:" ); - for(c=0;c0){ - printf("ok\n"); - // wait for 0xFF and send data, or error - if ((buffer[0]!=XSVF_READY_FOR_DATA) || (fileSize==0)) { - c=buffer[0]; - if (c==0xFF) - c=8; - printf(" End of operation reply: %s \n",XSVF_ERROR[c]); - switch (buffer[0]) { - case XSVF_ERROR_NONE : - printf(" Success!\n"); - break; - case XSVF_ERROR_UNKNOWN: - printf(" Unknown error: XSVF_ERROR_UNKNOWN \n"); - break; - case XSVF_ERROR_TDOMISMATCH: - printf(" Device did not respond as expected: XSVF_ERROR_TDOMISMATCH \n"); - break; - case XSVF_ERROR_MAXRETRIES: - printf(" Device did not respond: XSVF_ERROR_MAXRETRIES \n"); - break; - case XSVF_ERROR_ILLEGALCMD : - printf(" Unknown XSVF command: XSVF_ERROR_ILLEGALCMD \n"); - break; - case XSVF_ERROR_ILLEGALSTATE: - printf(" Unknown JTAG state: XSVF_ERROR_ILLEGALSTATE \n"); - break; - case XSVF_ERROR_DATAOVERFLOW : - printf(" Error, data overflow: XSVF_ERROR_DATAOVERFLOW \n"); - break; - case XSVF_ERROR_LAST: - printf(" Some other error I don't remember, probably isn't active: XSVF_ERROR_LAST \n"); - break; - case XSVF_READY_FOR_DATA: - if (fileSize==0) { - printf(" End of file reached. \n"); - } else { - printf(" Programmer says more data: XSVF_READY_FOR_DATA \n"); - } - break; - default: - printf(" Unknown error\n "); - - } - - } - break; //break loop and send data - }else{ - printf("\n Waiting for reply..."); - // Sleep(1); - timeout_counter++; - if(timeout_counter > 4){ - printf("\n No reply.... Quitting.\n "); - timeout_counter=0; - timer_out=-1; - break; - } - } - } - if (fileSize==0) { - break; - } - - if (timer_out==-1) - break; - //send data - if(fileSize>8); - temp[1]=readSize; - cnt=cnt+readSize; - - printf(" Sending %i Bytes (%04X)...",readSize, cnt); - serial_write( fd, (char *)temp,2 ); - serial_write( fd, (char *) &bin_buf[bytePointer],readSize ); - bytePointer=bytePointer+readSize;//start 1 chunk in next itme - fileSize=fileSize-readSize; //deincrement the remaining byte count - - } - - printf(" Thank you for playing! :-)\n\n"); -#ifdef WIN32 - fclose(XSVF); - FREE(param_port); - FREE(param_speed); - FREE(param_bytechunks); - FREE(param_XSVF); - FREE( bin_buf); -#endif - return 0; - } //end main() +#include +#include +#include +#include +#include +#include +#ifdef WIN32 +#include + #include + +#else +//#include +#endif + +#include "serial.h" +#include "buspirate.h" + + +#define JTAG_RESET 0x01 +#define JTAG_CHAIN_SCAN 0x02 +#define XSVF_PLAYER 0x03 + + +#define XSVF_ERROR_NONE 0x00 +#define XSVF_ERROR_UNKNOWN 0x01 +#define XSVF_ERROR_TDOMISMATCH 0x02 +#define XSVF_ERROR_MAXRETRIES 0x03 +#define XSVF_ERROR_ILLEGALCMD 0x04 +#define XSVF_ERROR_ILLEGALSTATE 0x05 +#define XSVF_ERROR_DATAOVERFLOW 0x06 +#define XSVF_ERROR_LAST 0x07 +#define XSVF_READY_FOR_DATA 0xFF + +//#ifndef WIN32 +#define usleep(x) Sleep(x); +//#define Sleep(x) usleep(x); +#define TRUE 1 +#define FALSE 0 +//#endif + +char *dumpfile; +#ifdef WIN32 +HANDLE dumphandle; +#endif + +int modem =FALSE; +int cnt=0; +uint8_t *bin_buf; +uint32_t bin_buf_size; +#define FREE(x) if(x) free(x); +#define MAX_BUFFER 4096 +//#define MAX_BUFFER 255 //255 bytes + +//http://www.whereisian.com/files/j-xsvf_002.swf + +int print_usage(char * appname) +{ + //print usage + printf("\n"); + printf("\n"); + printf(" Help Menu\n"); + printf(" Usage: \n"); + printf(" %s -p device -f filename.xsvf -s speed [-x] [-r] \n ",appname); + printf("\n"); + printf(" Example Usage: %s -p COM1 -s 115200 -f example.xsvf \n",appname); + printf("\n"); + printf(" Where: -p device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf(" -f Filename of XSVF file \n"); + printf(" -x Perform a JTAG Chain Scan by sending 0x02 command. -f is optional. \n"); + printf(" -r Perform a JTAG Reset Scan by sending 0x01 command. -f is optional. \n"); + printf("\n"); + + printf("-----------------------------------------------------------------------------\n"); + + + return 0; +} + +int main(int argc, char** argv) +{ + int opt; + uint8_t buffer[MAX_BUFFER]={0}; + uint8_t temp[2]={0}; // command buffer +// struct stat stbuf; + int fd,timeout_counter; + int res,c, nparam_bytechunks, bytePointer, readSize; + long fileSize; + FILE *XSVF; +// int xsvf; + int timer_out=0; + char *param_port = NULL; + char *param_speed = NULL; + char *param_XSVF=NULL; + char *param_bytechunks=NULL; + int jtag_reset=FALSE; + int chainscan=FALSE; + + const char *XSVF_ERROR[]={ "XSVF_ERROR_NONE", + "XSVF_ERROR_UNKNOWN", + "XSVF_ERROR_TDOMISMATCH", + "XSVF_ERROR_MAXRETRIES", + "XSVF_ERROR_ILLEGALCMD", + "XSVF_ERROR_ILLEGALSTATE", + "XSVF_ERROR_DATAOVERFLOW", + "XSVF_ERROR_LAST", + "XSVF_READY_FOR_DATA", + 0 }; + + printf("-----------------------------------------------------------------------------\n"); + printf("\n"); + printf(" BusPirate XSVF Player V.02\n"); + printf(" http://www.dangerousprototypes.com\n"); + printf("\n"); + printf("-----------------------------------------------------------------------------\n"); + + + + if (argc <= 1) { + print_usage(argv[0]); + exit(-1); + } + + + while ((opt = getopt(argc, argv, "s:p:f:rx")) != -1) { + + switch (opt) { + case 'p': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf("Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 'r': + jtag_reset=TRUE; + break; + case 'x': + chainscan=TRUE; + break; + case 'f': + if (param_XSVF != NULL) { + printf(" No XSVF file \n"); + exit(-1); + } + param_XSVF = strdup(optarg); + + break; + + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + exit(-1); + break; + } + } + + if (param_port==NULL){ + printf(" No serial port specified\n"); + print_usage(argv[0]); + exit(-1); + } + + nparam_bytechunks=MAX_BUFFER; + + if (param_speed==NULL) { + param_speed=strdup("115200"); //default is 115200kbps + } + + + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + + //setup port and speed + serial_setup(fd,(speed_t) atoi(param_speed)); + +//--- Added JM + res = BP_EnableBinary(fd); // Activate Binary mode (Sends 20 OxOO) + if (res == ERR){ + printf (" Unable to activate BP4 Binary mode \n"); + return -1; + }; + res = BP_EnableMode(fd,0x18); // Activate XSVF Player Sub-Mode + if (res == ERR){ + printf (" Unable to activate XSVF player mode \n"); + return -1; + }; + +//--- End added JM + + if (jtag_reset==TRUE){ + printf(" Performing Reset..\n"); + temp[0]=0x01; + serial_write( fd, (char *)temp, 1 ); + Sleep(1); + printf(" Done \n\n"); + } + + // perform Chain scan + // Send 0x02 to perform a chain scan + // Wait for 1 byte, the number of bytes that will be returned + // Get that many bytes + if (chainscan==TRUE) { + printf(" Performing Chain Scan..\n"); + temp[0]=0x02; + serial_write( fd, (char *)temp, 1 ); + Sleep(1); + res=0; + timeout_counter=0; + +// while (res==0) { + while ((res<=0) && (timeout_counter < 5)) { // Modified JM + res= serial_read(fd, (char *)buffer, sizeof(buffer)); + Sleep(1); + timeout_counter++; + } // Line Added JM + +/* //--- Removed JM + if (timeout_counter>=5) { + printf(" Got no reply for a Chain scan\n"); + } else { + printf(" Waiting for a chain scan reply\n"); + } + } //--- End removed JM */ + + if (res >0) { + printf(" Chain Scan Result:" ); + for(c=0;c0){ + printf("ok\n"); + // wait for 0xFF and send data, or error + if ((buffer[0]!=XSVF_READY_FOR_DATA) || (fileSize==0)) { + c=buffer[0]; + if (c==0xFF) + c=8; + printf(" End of operation reply: %s \n",XSVF_ERROR[c]); + switch (buffer[0]) { + case XSVF_ERROR_NONE : + printf(" Success!\n"); + break; + case XSVF_ERROR_UNKNOWN: + printf(" Unknown error: XSVF_ERROR_UNKNOWN \n"); + break; + case XSVF_ERROR_TDOMISMATCH: + printf(" Device did not respond as expected: XSVF_ERROR_TDOMISMATCH \n"); + break; + case XSVF_ERROR_MAXRETRIES: + printf(" Device did not respond: XSVF_ERROR_MAXRETRIES \n"); + break; + case XSVF_ERROR_ILLEGALCMD : + printf(" Unknown XSVF command: XSVF_ERROR_ILLEGALCMD \n"); + break; + case XSVF_ERROR_ILLEGALSTATE: + printf(" Unknown JTAG state: XSVF_ERROR_ILLEGALSTATE \n"); + break; + case XSVF_ERROR_DATAOVERFLOW : + printf(" Error, data overflow: XSVF_ERROR_DATAOVERFLOW \n"); + break; + case XSVF_ERROR_LAST: + printf(" Some other error I don't remember, probably isn't active: XSVF_ERROR_LAST \n"); + break; + case XSVF_READY_FOR_DATA: + if (fileSize==0) { + printf(" End of file reached. \n"); + } else { + printf(" Programmer says more data: XSVF_READY_FOR_DATA \n"); + } + break; + default: + printf(" Unknown error\n "); + + } + + } + break; //break loop and send data + }else{ + printf("\n Waiting for reply..."); + // Sleep(1); + timeout_counter++; + if(timeout_counter > 4){ + printf("\n No reply.... Quitting.\n "); + timeout_counter=0; + timer_out=-1; + break; + } + } + } + if (fileSize==0) { + break; + } + + if (timer_out==-1) + break; + //send data + if(fileSize>8); + temp[1]=readSize; + cnt=cnt+readSize; + + printf(" Sending %i Bytes (%04X)...",readSize, cnt); + serial_write( fd, (char *)temp,2 ); + serial_write( fd, (char *) &bin_buf[bytePointer],readSize ); + bytePointer=bytePointer+readSize;//start 1 chunk in next itme + fileSize=fileSize-readSize; //deincrement the remaining byte count + + } + + printf(" Thank you for playing! :-)\n\n"); +#ifdef WIN32 + fclose(XSVF); + FREE(param_port); + FREE(param_speed); + FREE(param_bytechunks); + FREE(param_XSVF); + FREE( bin_buf); +#endif + return 0; + } //end main() diff --git a/BP4_XSVFplayer_Win/serial.c b/BP4_XSVFplayer_Win/serial.c index 12c4cf42..8eee8b00 100644 --- a/BP4_XSVFplayer_Win/serial.c +++ b/BP4_XSVFplayer_Win/serial.c @@ -1,447 +1,447 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -/* - * OS independent serial interface - * - * Heavily based on Pirate-Loader: - * http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c - * - */ - -#include -#include -#include - -#include - -#include "serial.h" -extern int disable_comport; -extern char *dumpfile; -#ifdef WIN32 -extern HANDLE dumphandle; -#endif -#ifndef WIN32 -//#define usleep(x) Sleep(x); -#define Sleep(x) usleep(x); -#endif -/* -#ifdef WIN32 - int write(int fd, const void* buf, int len) - { - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bwritten = 0; - - - res = WriteFile(hCom, buf, len, &bwritten, NULL); - - if( res == FALSE ) { - return -1; - } else { - return bwritten; - } - } - - int read(int fd, void* buf, int len) - { - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bread = 0; - - res = ReadFile(hCom, buf, len, &bread, NULL); - - if( res == FALSE ) { - return -1; - } else { - return bread; - } - } - - int close(int fd) - { - HANDLE hCom = (HANDLE)fd; - - CloseHandle(hCom); - return 0; - } - - int open(const char* path, unsigned long flags) - { - static char full_path[32] = {0}; - - HANDLE hCom = NULL; - - if( path[0] != '\\' ) { - _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); - path = full_path; - } - - hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if( !hCom || hCom == INVALID_HANDLE_VALUE ) { - return -1; - } else { - return (int)hCom; - } - } - - int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) - { - time_t maxtc = time(0) + (timeout->tv_sec); - COMSTAT cs = {0}; - unsigned long dwErrors = 0; - - if( readfds->fd_count != 1 ) { - return -1; - } - - while( time(0) <= maxtc ) - { //only one file supported - if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ){ - return -1; - } - - if( cs.cbInQue > 0 ) { - return 1; - } - - Sleep(10); - } - return 0; - } - unsigned int sleep(unsigned int sec) - { - Sleep(sec * 1000); - - return 0; - } -#else -#endif -*/ -int serial_setup(int fd, speed_t speed) -{ -#ifdef WIN32 - COMMTIMEOUTS timeouts; - DCB dcb = {0}; - HANDLE hCom = (HANDLE)fd; - - dcb.DCBlength = sizeof(dcb); - - dcb.BaudRate = speed; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - if( !SetCommState(hCom, &dcb) ){ - return -1; - } - - - timeouts.ReadIntervalTimeout = 100; - timeouts.ReadTotalTimeoutMultiplier = 10; - timeouts.ReadTotalTimeoutConstant = 100; - timeouts.WriteTotalTimeoutMultiplier = 10; - timeouts.WriteTotalTimeoutConstant = 100; - - if (!SetCommTimeouts(hCom, &timeouts)) { - return -1; - } - - return 0; -#else - struct termios t_opt; - - /* set the serial port parameters */ - fcntl(fd, F_SETFL, 0); - tcgetattr(fd, &t_opt); - cfsetispeed(&t_opt, speed); - cfsetospeed(&t_opt, speed); - t_opt.c_cflag |= (CLOCAL | CREAD); - t_opt.c_cflag &= ~PARENB; - t_opt.c_cflag &= ~CSTOPB; - t_opt.c_cflag &= ~CSIZE; - t_opt.c_cflag |= CS8; - t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); - t_opt.c_iflag &= ~(IXON | IXOFF | IXANY); - t_opt.c_oflag &= ~OPOST; - t_opt.c_cc[VMIN] = 0; - t_opt.c_cc[VTIME] = 10; - tcflush(fd, TCIFLUSH); - tcsetattr(fd, TCSANOW, &t_opt); -#endif - return 0; -} - -int serial_write(int fd, char *buf, int size) -{ - int ret = 0; -#ifdef WIN32 - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bwritten = 0; - - - res = WriteFile(hCom, buf, size, &bwritten, NULL); - - //printf("Serial: Written %i bytes: ",size); - //int i; - //for (i = 0; i < size; i++){ - //printf("%02X ", buf[i]); - //} - //printf("\n"); - - if( res == FALSE ) { - ret = -1; - } else { - ret = bwritten; - } -#else - ret = write(fd, buf, size); -#endif - -// fprintf(stderr, "size = %d ret = %d\n", size, ret); - //buspirate_print_buffer(buf, size); - - if (ret != size) - fprintf(stderr, "Error sending data"); - return ret; - - -} - -int serial_read(int fd, char *buf, int size) -{ - int len = 0; - int ret = 0; -#ifndef WIN32 - int timeout = 0; -#endif -#ifdef WIN32 - HANDLE hCom = (HANDLE)fd; - unsigned long bread = 0; - - ret = ReadFile(hCom, buf, size, &bread, NULL); - - if( ret == FALSE || ret==-1 || bread<1) { - len= -1; - } else { - len=bread; - } - -#else - while (len < size) { - ret = read(fd, buf+len, size-len); - if (ret == -1){ - //printf("ret -1"); - return -1; - } - - if (ret == 0) { - timeout++; - - if (timeout >= 10) - break; - - continue; - } - - len += ret; - } -#endif - //printf("should have read = %i actual size = %i \n", size, len); - //fprintf(stderr, "should have read = %d actual size = %d \n", size, len); - //buspirate_print_buffer(buf, len); - -return len; -} - -int serial_open(char *port) -{ - int fd; -#ifdef WIN32 - static char full_path[32] = {0}; - - HANDLE hCom = NULL; - - if( port[0] != '\\' ) { - _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", port); - port = full_path; - } - - hCom = CreateFileA(port, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if( !hCom || hCom == INVALID_HANDLE_VALUE ) { - fd = -1; - } else { - fd = (int)hCom; - } - - //added to create a dumpfile - if (dumpfile !=NULL) { - dumphandle = CreateFileA(dumpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if( !dumphandle || dumphandle == INVALID_HANDLE_VALUE ) { - printf("Invalid dump file: %s\n",dumpfile); - return -1; - } - } -#else - fd = open(port, O_RDWR | O_NOCTTY); - if (fd == -1) { - fprintf(stderr, "Could not open serial port."); - return -1; - } -#endif - return fd; -} - -int serial_close(int fd) -{ -#ifdef WIN32 - HANDLE hCom = (HANDLE)fd; - - CloseHandle(hCom); - - //close dumpfile if it was created - if (dumpfile !=NULL) { - if (dumphandle != NULL) { - CloseHandle(dumphandle); - } - } -#else - close(fd); -#endif - return 0; -} - -/* -int readWithTimeout(int fd, uint8_t *out, int length, int timeout) -{ - fd_set fds; - struct timeval tv = {timeout, 0}; - int res = -1; - int got = 0; - - do { - - FD_ZERO(&fds); - FD_SET(fd, &fds); - - res = select(fd + 1, &fds, NULL, NULL, &tv); - - if( res > 0 ) { - res = read(fd, out, length); - if( res > 0 ) { - length -= res; - got += res; - out += res; - } else { - break; - } - } else { - return res; - } - } while( length > 0); - - return got; -} - -int configurePort(int fd, unsigned long baudrate) -{ -#ifdef WIN32 - COMMTIMEOUTS timeouts; - DCB dcb = {0}; - HANDLE hCom = (HANDLE)fd; - - dcb.DCBlength = sizeof(dcb); - - dcb.BaudRate = baudrate; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - if( !SetCommState(hCom, &dcb) ){ - return -1; - } - - - timeouts.ReadIntervalTimeout = 100; - timeouts.ReadTotalTimeoutMultiplier = 10; - timeouts.ReadTotalTimeoutConstant = 100; - timeouts.WriteTotalTimeoutMultiplier = 10; - timeouts.WriteTotalTimeoutConstant = 100; - - if (!SetCommTimeouts(hCom, &timeouts)) { - return -1; - } - - return (int)hCom; -#else - speed_t baud = B921600; - struct termios g_new_tio; - - switch (baudrate) { - case 921600: - baud = B921600; - break; - case 115200: - baud = B115200; - break; - case 1000000: - baud = B1000000; - break; - case 1500000: - baud = B1500000; - default: - printf("unknown speed setting \n"); - return -1; - break; - } - - memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); - cfmakeraw(&g_new_tio); - - g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); - g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); - g_new_tio.c_oflag = 0; - g_new_tio.c_lflag = 0; - - g_new_tio.c_cc[VTIME] = 0; - g_new_tio.c_cc[VMIN] = 1; - -#ifdef MACOSX - - if( tcsetattr(fd, TCSANOW, &g_new_tio) < 0 ) { - return -1; - } - - return ioctl( fd, IOSSIOSPEED, &baud ); -#else - cfsetispeed (&g_new_tio, baudrate); - cfsetospeed (&g_new_tio, baudrate); - - tcflush(fd, TCIOFLUSH); - - return tcsetattr(fd, TCSANOW, &g_new_tio); -#endif //#ifdef MACOSX - -#endif -} - -int openPort(const char* dev, unsigned long flags) -{ - return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); -} -*/ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +/* + * OS independent serial interface + * + * Heavily based on Pirate-Loader: + * http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c + * + */ + +#include +#include +#include + +#include + +#include "serial.h" +extern int disable_comport; +extern char *dumpfile; +#ifdef WIN32 +extern HANDLE dumphandle; +#endif +#ifndef WIN32 +//#define usleep(x) Sleep(x); +#define Sleep(x) usleep(x); +#endif +/* +#ifdef WIN32 + int write(int fd, const void* buf, int len) + { + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bwritten = 0; + + + res = WriteFile(hCom, buf, len, &bwritten, NULL); + + if( res == FALSE ) { + return -1; + } else { + return bwritten; + } + } + + int read(int fd, void* buf, int len) + { + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bread = 0; + + res = ReadFile(hCom, buf, len, &bread, NULL); + + if( res == FALSE ) { + return -1; + } else { + return bread; + } + } + + int close(int fd) + { + HANDLE hCom = (HANDLE)fd; + + CloseHandle(hCom); + return 0; + } + + int open(const char* path, unsigned long flags) + { + static char full_path[32] = {0}; + + HANDLE hCom = NULL; + + if( path[0] != '\\' ) { + _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); + path = full_path; + } + + hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if( !hCom || hCom == INVALID_HANDLE_VALUE ) { + return -1; + } else { + return (int)hCom; + } + } + + int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) + { + time_t maxtc = time(0) + (timeout->tv_sec); + COMSTAT cs = {0}; + unsigned long dwErrors = 0; + + if( readfds->fd_count != 1 ) { + return -1; + } + + while( time(0) <= maxtc ) + { //only one file supported + if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ){ + return -1; + } + + if( cs.cbInQue > 0 ) { + return 1; + } + + Sleep(10); + } + return 0; + } + unsigned int sleep(unsigned int sec) + { + Sleep(sec * 1000); + + return 0; + } +#else +#endif +*/ +int serial_setup(int fd, speed_t speed) +{ +#ifdef WIN32 + COMMTIMEOUTS timeouts; + DCB dcb = {0}; + HANDLE hCom = (HANDLE)fd; + + dcb.DCBlength = sizeof(dcb); + + dcb.BaudRate = speed; + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + if( !SetCommState(hCom, &dcb) ){ + return -1; + } + + + timeouts.ReadIntervalTimeout = 100; + timeouts.ReadTotalTimeoutMultiplier = 10; + timeouts.ReadTotalTimeoutConstant = 100; + timeouts.WriteTotalTimeoutMultiplier = 10; + timeouts.WriteTotalTimeoutConstant = 100; + + if (!SetCommTimeouts(hCom, &timeouts)) { + return -1; + } + + return 0; +#else + struct termios t_opt; + + /* set the serial port parameters */ + fcntl(fd, F_SETFL, 0); + tcgetattr(fd, &t_opt); + cfsetispeed(&t_opt, speed); + cfsetospeed(&t_opt, speed); + t_opt.c_cflag |= (CLOCAL | CREAD); + t_opt.c_cflag &= ~PARENB; + t_opt.c_cflag &= ~CSTOPB; + t_opt.c_cflag &= ~CSIZE; + t_opt.c_cflag |= CS8; + t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + t_opt.c_iflag &= ~(IXON | IXOFF | IXANY); + t_opt.c_oflag &= ~OPOST; + t_opt.c_cc[VMIN] = 0; + t_opt.c_cc[VTIME] = 10; + tcflush(fd, TCIFLUSH); + tcsetattr(fd, TCSANOW, &t_opt); +#endif + return 0; +} + +int serial_write(int fd, char *buf, int size) +{ + int ret = 0; +#ifdef WIN32 + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bwritten = 0; + + + res = WriteFile(hCom, buf, size, &bwritten, NULL); + + //printf("Serial: Written %i bytes: ",size); + //int i; + //for (i = 0; i < size; i++){ + //printf("%02X ", buf[i]); + //} + //printf("\n"); + + if( res == FALSE ) { + ret = -1; + } else { + ret = bwritten; + } +#else + ret = write(fd, buf, size); +#endif + +// fprintf(stderr, "size = %d ret = %d\n", size, ret); + //buspirate_print_buffer(buf, size); + + if (ret != size) + fprintf(stderr, "Error sending data"); + return ret; + + +} + +int serial_read(int fd, char *buf, int size) +{ + int len = 0; + int ret = 0; +#ifndef WIN32 + int timeout = 0; +#endif +#ifdef WIN32 + HANDLE hCom = (HANDLE)fd; + unsigned long bread = 0; + + ret = ReadFile(hCom, buf, size, &bread, NULL); + + if( ret == FALSE || ret==-1 || bread<1) { + len= -1; + } else { + len=bread; + } + +#else + while (len < size) { + ret = read(fd, buf+len, size-len); + if (ret == -1){ + //printf("ret -1"); + return -1; + } + + if (ret == 0) { + timeout++; + + if (timeout >= 10) + break; + + continue; + } + + len += ret; + } +#endif + //printf("should have read = %i actual size = %i \n", size, len); + //fprintf(stderr, "should have read = %d actual size = %d \n", size, len); + //buspirate_print_buffer(buf, len); + +return len; +} + +int serial_open(char *port) +{ + int fd; +#ifdef WIN32 + static char full_path[32] = {0}; + + HANDLE hCom = NULL; + + if( port[0] != '\\' ) { + _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", port); + port = full_path; + } + + hCom = CreateFileA(port, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if( !hCom || hCom == INVALID_HANDLE_VALUE ) { + fd = -1; + } else { + fd = (int)hCom; + } + + //added to create a dumpfile + if (dumpfile !=NULL) { + dumphandle = CreateFileA(dumpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if( !dumphandle || dumphandle == INVALID_HANDLE_VALUE ) { + printf("Invalid dump file: %s\n",dumpfile); + return -1; + } + } +#else + fd = open(port, O_RDWR | O_NOCTTY); + if (fd == -1) { + fprintf(stderr, "Could not open serial port."); + return -1; + } +#endif + return fd; +} + +int serial_close(int fd) +{ +#ifdef WIN32 + HANDLE hCom = (HANDLE)fd; + + CloseHandle(hCom); + + //close dumpfile if it was created + if (dumpfile !=NULL) { + if (dumphandle != NULL) { + CloseHandle(dumphandle); + } + } +#else + close(fd); +#endif + return 0; +} + +/* +int readWithTimeout(int fd, uint8_t *out, int length, int timeout) +{ + fd_set fds; + struct timeval tv = {timeout, 0}; + int res = -1; + int got = 0; + + do { + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + res = select(fd + 1, &fds, NULL, NULL, &tv); + + if( res > 0 ) { + res = read(fd, out, length); + if( res > 0 ) { + length -= res; + got += res; + out += res; + } else { + break; + } + } else { + return res; + } + } while( length > 0); + + return got; +} + +int configurePort(int fd, unsigned long baudrate) +{ +#ifdef WIN32 + COMMTIMEOUTS timeouts; + DCB dcb = {0}; + HANDLE hCom = (HANDLE)fd; + + dcb.DCBlength = sizeof(dcb); + + dcb.BaudRate = baudrate; + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + if( !SetCommState(hCom, &dcb) ){ + return -1; + } + + + timeouts.ReadIntervalTimeout = 100; + timeouts.ReadTotalTimeoutMultiplier = 10; + timeouts.ReadTotalTimeoutConstant = 100; + timeouts.WriteTotalTimeoutMultiplier = 10; + timeouts.WriteTotalTimeoutConstant = 100; + + if (!SetCommTimeouts(hCom, &timeouts)) { + return -1; + } + + return (int)hCom; +#else + speed_t baud = B921600; + struct termios g_new_tio; + + switch (baudrate) { + case 921600: + baud = B921600; + break; + case 115200: + baud = B115200; + break; + case 1000000: + baud = B1000000; + break; + case 1500000: + baud = B1500000; + default: + printf("unknown speed setting \n"); + return -1; + break; + } + + memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); + cfmakeraw(&g_new_tio); + + g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); + g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); + g_new_tio.c_oflag = 0; + g_new_tio.c_lflag = 0; + + g_new_tio.c_cc[VTIME] = 0; + g_new_tio.c_cc[VMIN] = 1; + +#ifdef MACOSX + + if( tcsetattr(fd, TCSANOW, &g_new_tio) < 0 ) { + return -1; + } + + return ioctl( fd, IOSSIOSPEED, &baud ); +#else + cfsetispeed (&g_new_tio, baudrate); + cfsetospeed (&g_new_tio, baudrate); + + tcflush(fd, TCIOFLUSH); + + return tcsetattr(fd, TCSANOW, &g_new_tio); +#endif //#ifdef MACOSX + +#endif +} + +int openPort(const char* dev, unsigned long flags) +{ + return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); +} +*/ diff --git a/BP4_XSVFplayer_Win/serial.h b/BP4_XSVFplayer_Win/serial.h index 9dc8a545..a7293021 100644 --- a/BP4_XSVFplayer_Win/serial.h +++ b/BP4_XSVFplayer_Win/serial.h @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * diff --git a/BPv3-bootloader/upgrader-v2tov4/firmware.h b/BPv3-bootloader/upgrader-v2tov4/firmware.h index 3e4670f1..bcec6ab4 100644 --- a/BPv3-bootloader/upgrader-v2tov4/firmware.h +++ b/BPv3-bootloader/upgrader-v2tov4/firmware.h @@ -1,157 +1,157 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "hardware.h" //for debug status - -#ifndef DEBUG - #define FWLOCATION 0xA800 //actual last page -#else // use a safe location for now :D since i don't have a proper programmer (someone wants to send me one?? :P) - #define FWLOCATION 0x9000 -#endif - -// test firmware (size should be exactly 1 page!) -// replace with the right bootloader -const unsigned char firmware [512*3] = { -0x20, 0x2F, 0x8C, 0xA9, 0x42, 0x27, 0xA9, 0x45, 0x07, 0xAE, 0x42, 0xA7, // 0 -0x37, 0xFE, 0xFF, 0x2F, 0xF0, 0xFF, 0x88, 0x60, 0x19, 0xA9, 0xCC, 0x22, // 1 -0xA8, 0xC8, 0x22, 0xA8, 0x68, 0xA0, 0xA9, 0xCC, 0x02, 0xA9, 0xC8, 0x02, // 2 -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0xCA, 0x22, 0x37, 0x95, 0x00, // 3 -0x80, 0x26, 0x35, 0x80, 0x27, 0x36, 0xA8, 0xA4, 0x06, 0xA9, 0xA4, 0x26, // 4 -0xA8, 0xA4, 0x46, 0xA9, 0xA4, 0x66, 0xA9, 0xA4, 0x86, 0xA8, 0xC4, 0x06, // 5 -0xA8, 0xC4, 0x26, 0xA9, 0xC4, 0x46, 0xA9, 0xC4, 0x66, 0xA9, 0xC4, 0x86, // 6 -0xA8, 0xC4, 0x22, 0xA9, 0xC0, 0x22, 0xEB, 0x80, 0x00, 0x20, 0x20, 0x02, // 7 -0x88, 0x40, 0x11, 0xA8, 0x20, 0x62, 0xA8, 0x21, 0xE2, 0xA8, 0x23, 0x42, // 8 -0x07, 0x68, 0x00, 0xB1, 0x10, 0x0C, 0x3A, 0x75, 0x00, 0x20, 0x40, 0x0D, // 9 -0x88, 0x20, 0x11, 0x20, 0x10, 0x00, 0x88, 0x20, 0x11, 0x20, 0x20, 0x00, // 10 -0x88, 0x20, 0x11, 0x20, 0xB0, 0x04, 0x88, 0x20, 0x11, 0xEB, 0x00, 0x07, // 11 -0x07, 0x5C, 0x00, 0x88, 0x90, 0x01, 0x07, 0x5A, 0x00, 0xB7, 0x03, 0xE1, // 12 -0x07, 0x58, 0x00, 0xB7, 0x02, 0xE1, 0xBF, 0x02, 0x81, 0x78, 0x80, 0x02, // 13 -0x78, 0x00, 0x02, 0x07, 0x53, 0x00, 0x78, 0x80, 0x06, 0x07, 0x51, 0x00, // 14 -0x78, 0x80, 0x01, 0x20, 0x02, 0x80, 0x07, 0x4E, 0x00, 0x78, 0x00, 0x59, // 15 -0xE9, 0x83, 0x01, 0x3A, 0xFC, 0xFF, 0xE0, 0x0E, 0x04, 0x32, 0x03, 0x00, // 16 -0x20, 0xE0, 0x04, 0x88, 0x20, 0x11, 0x37, 0xE8, 0xFF, 0x2A, 0x03, 0x7C, // 17 -0xE1, 0x03, 0x28, 0x3E, 0x10, 0x00, 0xE0, 0x05, 0x00, 0x3A, 0x12, 0x00, // 18 -0x20, 0x02, 0x80, 0xB3, 0x40, 0xC0, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 19 -0x78, 0x00, 0x59, 0xB3, 0x80, 0xCA, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 20 -0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 21 -0x78, 0x00, 0x59, 0x37, 0x04, 0x00, 0xEB, 0x80, 0x00, 0x20, 0x00, 0x05, // 22 -0x88, 0x20, 0x11, 0x37, 0xD1, 0xFF, 0x20, 0x02, 0x80, 0xA7, 0x0D, 0x10, // 23 -0x37, 0x02, 0x00, 0x2F, 0xF1, 0xFF, 0x37, 0xCA, 0xFF, 0xA6, 0x01, 0x00, // 24 -0x37, 0x04, 0x00, 0xBB, 0x85, 0x0A, 0x24, 0x20, 0x04, 0x07, 0x1A, 0x00, // 25 -0xEB, 0x80, 0x00, 0x20, 0x03, 0x04, 0xBB, 0xB2, 0xCA, 0xBB, 0xB2, 0x5A, // 26 -0xBB, 0xB2, 0x5A, 0xE9, 0x83, 0x01, 0x3A, 0xFB, 0xFF, 0x24, 0x10, 0x00, // 27 -0x07, 0x11, 0x00, 0x20, 0x03, 0x04, 0x20, 0x02, 0x80, 0xBA, 0x14, 0xC0, // 28 -0xE1, 0x32, 0x04, 0x3A, 0x09, 0x00, 0xBA, 0x34, 0x40, 0xE1, 0x32, 0x04, // 29 -0x3A, 0x06, 0x00, 0xBA, 0x34, 0x40, 0xE1, 0x32, 0x04, 0x3A, 0x03, 0x00, // 30 -0xE9, 0x83, 0x01, 0x3A, 0xF5, 0xFF, 0x37, 0xAE, 0xFF, 0x20, 0x60, 0x05, // 31 -0x88, 0x20, 0x11, 0x37, 0xAD, 0xFF, 0x88, 0x00, 0x3B, 0x20, 0x50, 0x05, // 32 -0x88, 0x30, 0x3B, 0x20, 0xA0, 0x0A, 0x88, 0x30, 0x3B, 0xA8, 0x61, 0xE7, // 33 -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0x61, 0xE7, 0x37, 0xFE, 0xFF, // 34 -0x06, 0x00, 0x00, 0x20, 0x0A, 0x00, 0xEB, 0x80, 0x05, 0xFE, 0x00, 0x60, // 35 -0xAE, 0x22, 0x02, 0x37, 0x03, 0x00, 0x80, 0x30, 0x11, 0x47, 0x00, 0x07, // 36 -0x06, 0x00, 0x00, 0xE9, 0x8B, 0x05, 0x3A, 0xF8, 0xFF, 0xE9, 0x0A, 0x05, // 37 -0x3A, 0xF5, 0xFF, 0x20, 0x2F, 0x8C, 0xAE, 0xCA, 0x22, 0x37, 0x74, 0xFF, // 38 -0xA9, 0x85, 0x60, 0xA9, 0x85, 0x80, 0xA9, 0x23, 0x42, 0xA9, 0x21, 0xE2, // 39 -0xEF, 0x02, 0x21, 0xA9, 0xC4, 0x22, 0xA8, 0xC0, 0x22, 0x88, 0x26, 0x35, // 40 -0x88, 0x27, 0x36, 0xA9, 0x68, 0xA0, 0xA8, 0xC8, 0x02, 0x20, 0x00, 0x00, // 41 -0x88, 0x60, 0x19, 0x37, 0x54, 0xFF, 0x00, 0x00, 0x08, 0x00, 0xC2, 0x00, // 42 -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0xFF, 0xFF, 0xFF, // 43 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 44 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 45 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 46 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 47 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 48 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 49 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 50 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 51 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 52 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 53 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 54 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 55 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 56 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 57 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 58 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 59 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 60 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 61 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 62 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 63 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 64 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 65 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 66 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 67 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 68 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 69 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 71 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 72 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 73 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 74 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 75 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 76 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 77 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 78 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 79 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 81 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 82 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 83 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 84 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 85 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 86 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 87 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 88 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 89 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 90 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 91 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 92 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 93 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 94 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 95 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 96 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 97 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 98 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 99 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 100 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 101 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 102 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 103 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 104 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 105 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 106 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 107 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 108 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 109 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 110 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 111 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 112 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 113 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 114 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 115 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 116 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 117 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 118 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 119 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 120 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 121 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 122 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 123 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 124 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 125 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 126 -0x37, 0x13, 0xFE, 0x00, 0x01, 0x04, // 127 -0x00, 0xDF, 0xF9, 0x00, 0x7F, 0x3F, // 128 -}; - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "hardware.h" //for debug status + +#ifndef DEBUG + #define FWLOCATION 0xA800 //actual last page +#else // use a safe location for now :D since i don't have a proper programmer (someone wants to send me one?? :P) + #define FWLOCATION 0x9000 +#endif + +// test firmware (size should be exactly 1 page!) +// replace with the right bootloader +const unsigned char firmware [512*3] = { +0x20, 0x2F, 0x8C, 0xA9, 0x42, 0x27, 0xA9, 0x45, 0x07, 0xAE, 0x42, 0xA7, // 0 +0x37, 0xFE, 0xFF, 0x2F, 0xF0, 0xFF, 0x88, 0x60, 0x19, 0xA9, 0xCC, 0x22, // 1 +0xA8, 0xC8, 0x22, 0xA8, 0x68, 0xA0, 0xA9, 0xCC, 0x02, 0xA9, 0xC8, 0x02, // 2 +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0xCA, 0x22, 0x37, 0x95, 0x00, // 3 +0x80, 0x26, 0x35, 0x80, 0x27, 0x36, 0xA8, 0xA4, 0x06, 0xA9, 0xA4, 0x26, // 4 +0xA8, 0xA4, 0x46, 0xA9, 0xA4, 0x66, 0xA9, 0xA4, 0x86, 0xA8, 0xC4, 0x06, // 5 +0xA8, 0xC4, 0x26, 0xA9, 0xC4, 0x46, 0xA9, 0xC4, 0x66, 0xA9, 0xC4, 0x86, // 6 +0xA8, 0xC4, 0x22, 0xA9, 0xC0, 0x22, 0xEB, 0x80, 0x00, 0x20, 0x20, 0x02, // 7 +0x88, 0x40, 0x11, 0xA8, 0x20, 0x62, 0xA8, 0x21, 0xE2, 0xA8, 0x23, 0x42, // 8 +0x07, 0x68, 0x00, 0xB1, 0x10, 0x0C, 0x3A, 0x75, 0x00, 0x20, 0x40, 0x0D, // 9 +0x88, 0x20, 0x11, 0x20, 0x10, 0x00, 0x88, 0x20, 0x11, 0x20, 0x20, 0x00, // 10 +0x88, 0x20, 0x11, 0x20, 0xB0, 0x04, 0x88, 0x20, 0x11, 0xEB, 0x00, 0x07, // 11 +0x07, 0x5C, 0x00, 0x88, 0x90, 0x01, 0x07, 0x5A, 0x00, 0xB7, 0x03, 0xE1, // 12 +0x07, 0x58, 0x00, 0xB7, 0x02, 0xE1, 0xBF, 0x02, 0x81, 0x78, 0x80, 0x02, // 13 +0x78, 0x00, 0x02, 0x07, 0x53, 0x00, 0x78, 0x80, 0x06, 0x07, 0x51, 0x00, // 14 +0x78, 0x80, 0x01, 0x20, 0x02, 0x80, 0x07, 0x4E, 0x00, 0x78, 0x00, 0x59, // 15 +0xE9, 0x83, 0x01, 0x3A, 0xFC, 0xFF, 0xE0, 0x0E, 0x04, 0x32, 0x03, 0x00, // 16 +0x20, 0xE0, 0x04, 0x88, 0x20, 0x11, 0x37, 0xE8, 0xFF, 0x2A, 0x03, 0x7C, // 17 +0xE1, 0x03, 0x28, 0x3E, 0x10, 0x00, 0xE0, 0x05, 0x00, 0x3A, 0x12, 0x00, // 18 +0x20, 0x02, 0x80, 0xB3, 0x40, 0xC0, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 19 +0x78, 0x00, 0x59, 0xB3, 0x80, 0xCA, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 20 +0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, 0x78, 0x00, 0x59, 0xB3, 0x00, 0xC0, // 21 +0x78, 0x00, 0x59, 0x37, 0x04, 0x00, 0xEB, 0x80, 0x00, 0x20, 0x00, 0x05, // 22 +0x88, 0x20, 0x11, 0x37, 0xD1, 0xFF, 0x20, 0x02, 0x80, 0xA7, 0x0D, 0x10, // 23 +0x37, 0x02, 0x00, 0x2F, 0xF1, 0xFF, 0x37, 0xCA, 0xFF, 0xA6, 0x01, 0x00, // 24 +0x37, 0x04, 0x00, 0xBB, 0x85, 0x0A, 0x24, 0x20, 0x04, 0x07, 0x1A, 0x00, // 25 +0xEB, 0x80, 0x00, 0x20, 0x03, 0x04, 0xBB, 0xB2, 0xCA, 0xBB, 0xB2, 0x5A, // 26 +0xBB, 0xB2, 0x5A, 0xE9, 0x83, 0x01, 0x3A, 0xFB, 0xFF, 0x24, 0x10, 0x00, // 27 +0x07, 0x11, 0x00, 0x20, 0x03, 0x04, 0x20, 0x02, 0x80, 0xBA, 0x14, 0xC0, // 28 +0xE1, 0x32, 0x04, 0x3A, 0x09, 0x00, 0xBA, 0x34, 0x40, 0xE1, 0x32, 0x04, // 29 +0x3A, 0x06, 0x00, 0xBA, 0x34, 0x40, 0xE1, 0x32, 0x04, 0x3A, 0x03, 0x00, // 30 +0xE9, 0x83, 0x01, 0x3A, 0xF5, 0xFF, 0x37, 0xAE, 0xFF, 0x20, 0x60, 0x05, // 31 +0x88, 0x20, 0x11, 0x37, 0xAD, 0xFF, 0x88, 0x00, 0x3B, 0x20, 0x50, 0x05, // 32 +0x88, 0x30, 0x3B, 0x20, 0xA0, 0x0A, 0x88, 0x30, 0x3B, 0xA8, 0x61, 0xE7, // 33 +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, 0x61, 0xE7, 0x37, 0xFE, 0xFF, // 34 +0x06, 0x00, 0x00, 0x20, 0x0A, 0x00, 0xEB, 0x80, 0x05, 0xFE, 0x00, 0x60, // 35 +0xAE, 0x22, 0x02, 0x37, 0x03, 0x00, 0x80, 0x30, 0x11, 0x47, 0x00, 0x07, // 36 +0x06, 0x00, 0x00, 0xE9, 0x8B, 0x05, 0x3A, 0xF8, 0xFF, 0xE9, 0x0A, 0x05, // 37 +0x3A, 0xF5, 0xFF, 0x20, 0x2F, 0x8C, 0xAE, 0xCA, 0x22, 0x37, 0x74, 0xFF, // 38 +0xA9, 0x85, 0x60, 0xA9, 0x85, 0x80, 0xA9, 0x23, 0x42, 0xA9, 0x21, 0xE2, // 39 +0xEF, 0x02, 0x21, 0xA9, 0xC4, 0x22, 0xA8, 0xC0, 0x22, 0x88, 0x26, 0x35, // 40 +0x88, 0x27, 0x36, 0xA9, 0x68, 0xA0, 0xA8, 0xC8, 0x02, 0x20, 0x00, 0x00, // 41 +0x88, 0x60, 0x19, 0x37, 0x54, 0xFF, 0x00, 0x00, 0x08, 0x00, 0xC2, 0x00, // 42 +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0xFF, 0xFF, 0xFF, // 43 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 44 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 45 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 46 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 47 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 48 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 49 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 50 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 51 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 52 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 53 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 54 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 55 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 56 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 57 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 58 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 59 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 60 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 61 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 62 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 63 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 64 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 65 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 66 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 67 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 68 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 69 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 71 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 72 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 73 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 74 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 75 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 76 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 77 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 78 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 79 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 81 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 82 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 83 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 84 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 85 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 86 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 87 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 88 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 89 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 90 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 91 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 92 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 93 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 94 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 95 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 96 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 97 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 98 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 99 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 100 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 101 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 102 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 103 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 104 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 105 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 106 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 107 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 108 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 109 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 110 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 111 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 112 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 113 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 114 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 115 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 116 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 117 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 118 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 119 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 120 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 121 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 122 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 123 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 124 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 125 +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 126 +0x37, 0x13, 0xFE, 0x00, 0x01, 0x04, // 127 +0x00, 0xDF, 0xF9, 0x00, 0x7F, 0x3F, // 128 +}; + diff --git a/BPv3-bootloader/upgrader-v2tov4/flash.h b/BPv3-bootloader/upgrader-v2tov4/flash.h index a5fd1efb..66d8bbbc 100644 --- a/BPv3-bootloader/upgrader-v2tov4/flash.h +++ b/BPv3-bootloader/upgrader-v2tov4/flash.h @@ -1,20 +1,20 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -// CvD: actual subs are copied from/inspired by the C30 bootloader (http://mrmackey.no-ip.org/elektronik/ds30loader/) - -void Ferase(int page); -void Fwrite(unsigned char *buf, int address); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +// CvD: actual subs are copied from/inspired by the C30 bootloader (http://mrmackey.no-ip.org/elektronik/ds30loader/) + +void Ferase(int page); +void Fwrite(unsigned char *buf, int address); + diff --git a/BPv3-bootloader/upgrader-v2tov4/hardware.h b/BPv3-bootloader/upgrader-v2tov4/hardware.h index cd71f658..23ea319a 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardware.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardware.h @@ -1,52 +1,52 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - - - -/* CvD: most of this is copied and pasted from the svn trunk from the buspirate */ -/* CvD: should we link to there or leave as is? */ - -#ifndef BASE -#define BASE -#include - -//#define DEBUG //safe debug release, won't overwrite bootloader v2 - -//this sets the hardware version -//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ -//#define BUSPIRATEV2A -//#define BUSPIRATEV25 //AKA Bus Pirate v2go -//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ -#define BUSPIRATEV3 - -#if defined(BUSPIRATEV0A) - #include "hardwarev0a.h" -#elif defined(BUSPIRATEV1A) - #include "hardwarev1a.h" -#elif defined(BUSPIRATEV2A) - #define BUSPIRATEV2 - #include "hardwarev2a.h" -#elif defined(BUSPIRATEV25) - #define BUSPIRATEV2 - #include "hardwarev25.h" -#elif defined(BUSPIRATEV3) - #define BUSPIRATEV2 - #include "hardwarev3.h" -#else - #error "No hardware defined in hardware.h." -#endif - -#endif - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + + + +/* CvD: most of this is copied and pasted from the svn trunk from the buspirate */ +/* CvD: should we link to there or leave as is? */ + +#ifndef BASE +#define BASE +#include + +//#define DEBUG //safe debug release, won't overwrite bootloader v2 + +//this sets the hardware version +//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ +//#define BUSPIRATEV2A +//#define BUSPIRATEV25 //AKA Bus Pirate v2go +//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ +#define BUSPIRATEV3 + +#if defined(BUSPIRATEV0A) + #include "hardwarev0a.h" +#elif defined(BUSPIRATEV1A) + #include "hardwarev1a.h" +#elif defined(BUSPIRATEV2A) + #define BUSPIRATEV2 + #include "hardwarev2a.h" +#elif defined(BUSPIRATEV25) + #define BUSPIRATEV2 + #include "hardwarev25.h" +#elif defined(BUSPIRATEV3) + #define BUSPIRATEV2 + #include "hardwarev3.h" +#else + #error "No hardware defined in hardware.h." +#endif + +#endif + diff --git a/BPv3-bootloader/upgrader-v2tov4/hardwarev0a.h b/BPv3-bootloader/upgrader-v2tov4/hardwarev0a.h index 94cbce81..dcde7975 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardwarev0a.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardwarev0a.h @@ -1,103 +1,103 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the first, through-hole Bus Pirate. -//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ - -#define BP_VERSION_STRING "Pirate v0a" -#define BP_VERSION "v0a" - -//this configures the proper placement of the serial port for -//the PC side terminal interface -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 4 - -//this is a different type of pin identifier used by the bitbang library -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB5 -#define BP_PULLUP1 PORTBbits.RB11 -#define BP_PULLUP2 PORTBbits.RB10 -#define BP_LEDMODE PORTBbits.RB14 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB5 -#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA -#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL -#define BP_LEDMODE_DIR TRISBbits.TRISB14 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB5 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 5 -#define BP_AUX_RPOUT RPOR2bits.RP5R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for pullup resistors -#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 -#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 - -//define device config bit, ID, and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the first, through-hole Bus Pirate. +//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ + +#define BP_VERSION_STRING "Pirate v0a" +#define BP_VERSION "v0a" + +//this configures the proper placement of the serial port for +//the PC side terminal interface +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 4 + +//this is a different type of pin identifier used by the bitbang library +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB5 +#define BP_PULLUP1 PORTBbits.RB11 +#define BP_PULLUP2 PORTBbits.RB10 +#define BP_LEDMODE PORTBbits.RB14 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB5 +#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA +#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL +#define BP_LEDMODE_DIR TRISBbits.TRISB14 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB5 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 5 +#define BP_AUX_RPOUT RPOR2bits.RP5R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for pullup resistors +#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 +#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 + +//define device config bit, ID, and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v2tov4/hardwarev1a.h b/BPv3-bootloader/upgrader-v2tov4/hardwarev1a.h index 271f6dd9..142a30c6 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardwarev1a.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardwarev1a.h @@ -1,114 +1,114 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the second, surface mount Bus Pirate. -//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface - -#define BP_VERSION_STRING "Bus Pirate v1a" -#define BP_VERSION "v1a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -//when we move to a USB PIC, we'll need to do that.... -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 3 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_PIN6 PORTBbits.RB11 -#define BP_LEDMODE PORTBbits.RB4 -#define BP_LEDPWR PORTAbits.RA3 -#define BP_EN50V PORTAbits.RA4 -#define BP_EN33V PORTBbits.RB5 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_PIN6_DIR TRISBbits.TRISB11 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_LEDPWR_DIR TRISAbits.TRISA3 -#define BP_EN50V_DIR TRISAbits.TRISA4 -#define BP_EN33V_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 -#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 - -#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 -#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 -#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the second, surface mount Bus Pirate. +//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface + +#define BP_VERSION_STRING "Bus Pirate v1a" +#define BP_VERSION "v1a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +//when we move to a USB PIC, we'll need to do that.... +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 3 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_PIN6 PORTBbits.RB11 +#define BP_LEDMODE PORTBbits.RB4 +#define BP_LEDPWR PORTAbits.RA3 +#define BP_EN50V PORTAbits.RA4 +#define BP_EN33V PORTBbits.RB5 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_PIN6_DIR TRISBbits.TRISB11 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_LEDPWR_DIR TRISAbits.TRISA3 +#define BP_EN50V_DIR TRISAbits.TRISA4 +#define BP_EN33V_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 +#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 + +#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 +#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 +#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v2tov4/hardwarev25.h b/BPv3-bootloader/upgrader-v2tov4/hardwarev25.h index 39167933..d45e5735 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardwarev25.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardwarev25.h @@ -1,113 +1,113 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this profile is for the Bus Pirate v2go hardware -//http:// - -#define BP_VERSION_STRING "Bus Pirate v2go" -#define BP_VERSION "v2go" - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB1 -#define BP_POST_DIR TRISBbits.TRISB1 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this profile is for the Bus Pirate v2go hardware +//http:// + +#define BP_VERSION_STRING "Bus Pirate v2go" +#define BP_VERSION "v2go" + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB1 +#define BP_POST_DIR TRISBbits.TRISB1 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v2tov4/hardwarev2a.h b/BPv3-bootloader/upgrader-v2tov4/hardwarev2a.h index 652dfb7d..e7da418f 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardwarev2a.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardwarev2a.h @@ -1,109 +1,109 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for hardware V2a. -//http:// - -#define BP_VERSION_STRING "Bus Pirate v2a" -#define BP_VERSION "v2a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 2 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_VREGEN_DIR TRISAbits.TRISA4 -#define BP_PULLUP_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for hardware V2a. +//http:// + +#define BP_VERSION_STRING "Bus Pirate v2a" +#define BP_VERSION "v2a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 2 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_VREGEN_DIR TRISAbits.TRISA4 +#define BP_PULLUP_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v2tov4/hardwarev3.h b/BPv3-bootloader/upgrader-v2tov4/hardwarev3.h index 2dac548b..e6f3c567 100644 --- a/BPv3-bootloader/upgrader-v2tov4/hardwarev3.h +++ b/BPv3-bootloader/upgrader-v2tov4/hardwarev3.h @@ -1,117 +1,117 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com -#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" -#define BP_VERSION "v3" - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB0 -#define BP_POST_DIR TRISBbits.TRISB0 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//flow control connections for FTDI chip -//used in uart brige -#define FTDI_RTS_DIR TRISAbits.TRISA4 -#define FTDI_RTS PORTAbits.RA4 -#define FTDI_CTS_DIR TRISAbits.TRISA3 -#define FTDI_CTS PORTAbits.RA3 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com +#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" +#define BP_VERSION "v3" + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB0 +#define BP_POST_DIR TRISBbits.TRISB0 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//flow control connections for FTDI chip +//used in uart brige +#define FTDI_RTS_DIR TRISAbits.TRISA4 +#define FTDI_RTS PORTAbits.RA4 +#define FTDI_CTS_DIR TRISAbits.TRISA3 +#define FTDI_CTS PORTAbits.RA3 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v2tov4/main.c b/BPv3-bootloader/upgrader-v2tov4/main.c index 77d5c1ca..68a3f040 100644 --- a/BPv3-bootloader/upgrader-v2tov4/main.c +++ b/BPv3-bootloader/upgrader-v2tov4/main.c @@ -1,334 +1,334 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -// CvD: buspirate subs copied from the bus pirate svn -// CvD: Did I already mention it is highly alpha?? use with caution and only if you have a programmer to undo this!! - - -#include "hardware.h" -#include "flash.h" -#include "firmware.h" - -// functions -void BpInit(void); -int main(void); -void bpWstring(char *s); -void bpWinthex(unsigned int c); -void bpWhex(unsigned int c); -void bpWstring(char *s); -void bpWline(char *s); -void UART1TX(char c); -void bpDelayUS(const unsigned char delay); -void bpDelayMS(const unsigned char delay); -unsigned char UART1RXRdy(void); -unsigned char UART1RX(void); -void usermessage(void); -unsigned char checkChar(unsigned char c); - -// cvD: needed?? -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need - -/* - Store delay timeout value & user reset vector at start of user space - - Value of userReset should be the start of actual program code since - these variables will be stored in the same area. - - some heritage from the old bootloader -*/ -unsigned int userReset __attribute__ ((space(prog),section(".init"))) = 0xC04 ; -unsigned char timeout __attribute__ ((space(prog),section(".init"))) = 0x00 ; - - -// it all happens from here -int main(void) -{ int i,j; - unsigned char buf[3*64]; - - BpInit(); - BP_LEDMODE=1; - - //wait for confirmation - while(1){ - usermessage(); - if(checkChar('Y')) // absorb 'Yes' and echo it back - { // if any typo is made it should cancel and start over again - if(checkChar('E')) - { - if(checkChar('S')) - { - bpWline(""); - break; - } - } - } - - bpWline(" Upgrade cancelled."); - bpWline(""); - } - bpWline(""); - bpWline("Sit back and enjoy the show"); - - // the user is really sure - // erase the page before the bootloader - bpWstring("Erasing "); - bpWinthex(FWLOCATION-0x0400); - bpWstring("..."); // I love dots ;) - Ferase(FWLOCATION); - bpWline(" done"); - - // erase the bootloader location - bpWstring("Erasing "); - bpWinthex(FWLOCATION); - bpWstring("..."); // I love dots ;) - Ferase(FWLOCATION); - bpWline(" done"); - - // write 8 rows (8*64=512 'words') - bpWstring("Writing row "); - for(i=0; i<8; i++) - { UART1TX(0x30+i); - UART1TX(' '); - - for(j=0; j<3*64; j++) // fill buffer - { buf[j]=firmware[(i*192)+j]; - } - Fwrite(buf, FWLOCATION+(i*128)); // 1 row = 64 words - } - bpWline("done"); - - #ifndef DEBUG - // erase 1st page and write jump to new bootloader - bpWstring("Erasing 0x0000..."); - Ferase(0x0000); - bpWline(" done"); - - bpWstring("Writing row 0... "); - for(j=0; j<(3*64); j++) buf[j]=0xFF;// 1's are easier to write - buf[0]=0x04; // goto FWLOCATION - buf[1]=(FWLOCATION&0x00FF); - buf[2]=((FWLOCATION&0xFF00)>>8); - buf[3]=0x00; // goto FWLOCATION - buf[4]=0x00; - buf[5]=0x00; - Fwrite(buf, 0); - bpWline("done"); - #endif - - // finish up - bpWline(""); - bpWline("Success!"); - bpWline("Upload v4+ firmware with the ds30 Loader app to complete the upgrade.");// Yo American! can the buspirate sniff serial comma's? :P - - while(1) - { BP_LEDMODE=1; // we want some attention! - bpDelayMS(250); - bpDelayMS(250); - BP_LEDMODE=0; - bpDelayMS(250); - bpDelayMS(250); - } - - return 0; -} - -unsigned char checkChar(unsigned char c){ - unsigned char d; - d=UART1RX(); - UART1TX(d); - if(d>='a' && d<='z') d-=0x20; //convert to upper - if(c==d) return 1; - return 0; -} - -void usermessage(void){ - bpWline(""); - bpWline("DS30 Loader installer v0.2 (ALPHA)"); - bpWline("(C)hris 2010"); - bpWline("Released under Creative Commons null license."); - #ifdef DEBUG - bpWline("RUNNING IN DEBUG MODE!!!!"); - #endif - bpWline("This will install bootloader v4.1."); - bpWline("Type 'Yes' to program the new bootloader."); - bpWline("Disconnect power if unsure (no harm done)."); - bpWline(""); -} - -// mostly copied from base.c, baseIO.c and baseUI.h -// CvD: how to keep synchronized with changes in the buspirate trunk?? - -void BpInit(void) -{ - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; - - BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; - - BP_MOSI_DIR=1; - BP_CLK_DIR=1; - BP_MISO_DIR=1; - BP_CS_DIR=1; - BP_AUX_DIR=1; - BP_LEDMODE=0; //mode LED OFF - BP_LEDMODE_DIR=0; - #ifdef BUSPIRATEV1A - BP_PIN6_DIR=1; //AUX2... unused - BP_LEDPWR=1; //light power led - BP_LEDPWR_DIR=0; - #endif - - #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) - BP_PULLUP_OFF(); - #endif - - #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) - - BP_VREG_OFF();//disable the VREG - - //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! - //AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V - //AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V - //AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup - //AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT - //AD1PCFG&=(~0b11110);//analog pins - - //configure the ADC - //AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - //AD1CSSL = 0; - //AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - //AD1CON2 = 0; - #endif - - U1BRG = 34; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; - -} - -//Write a string to the user terminal -void bpWstring(char *s){ - char c; - while((c = *s++)) UART1TX(c); -} - -//write a string to the user terminal, finish with a line break -void bpWline(char *s){ - char c; - while((c = *s++)) UART1TX(c); - UART1TX(0x0d); - UART1TX(0x0a); - -} - -//output an 8bit/byte hex value to the user terminal -const unsigned char HEXASCII[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; -void bpWhex(unsigned int c){ - unsigned int b; - - bpWstring("0x"); - b = (c>>4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - -// output a 16bit hex value to the user terminal -void bpWinthex(unsigned int c) { - unsigned int b; - - bpWstring("0x"); - b = (c>>12) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c>>8) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c>>4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//delay in MS -void bpDelayMS(const unsigned char delay){ - volatile unsigned int i; - volatile unsigned char c; - - for(c=0;c>8); + buf[3]=0x00; // goto FWLOCATION + buf[4]=0x00; + buf[5]=0x00; + Fwrite(buf, 0); + bpWline("done"); + #endif + + // finish up + bpWline(""); + bpWline("Success!"); + bpWline("Upload v4+ firmware with the ds30 Loader app to complete the upgrade.");// Yo American! can the buspirate sniff serial comma's? :P + + while(1) + { BP_LEDMODE=1; // we want some attention! + bpDelayMS(250); + bpDelayMS(250); + BP_LEDMODE=0; + bpDelayMS(250); + bpDelayMS(250); + } + + return 0; +} + +unsigned char checkChar(unsigned char c){ + unsigned char d; + d=UART1RX(); + UART1TX(d); + if(d>='a' && d<='z') d-=0x20; //convert to upper + if(c==d) return 1; + return 0; +} + +void usermessage(void){ + bpWline(""); + bpWline("DS30 Loader installer v0.2 (ALPHA)"); + bpWline("(C)hris 2010"); + bpWline("Released under Creative Commons null license."); + #ifdef DEBUG + bpWline("RUNNING IN DEBUG MODE!!!!"); + #endif + bpWline("This will install bootloader v4.1."); + bpWline("Type 'Yes' to program the new bootloader."); + bpWline("Disconnect power if unsure (no harm done)."); + bpWline(""); +} + +// mostly copied from base.c, baseIO.c and baseUI.h +// CvD: how to keep synchronized with changes in the buspirate trunk?? + +void BpInit(void) +{ + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; + + BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; + BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; + + BP_MOSI_DIR=1; + BP_CLK_DIR=1; + BP_MISO_DIR=1; + BP_CS_DIR=1; + BP_AUX_DIR=1; + BP_LEDMODE=0; //mode LED OFF + BP_LEDMODE_DIR=0; + #ifdef BUSPIRATEV1A + BP_PIN6_DIR=1; //AUX2... unused + BP_LEDPWR=1; //light power led + BP_LEDPWR_DIR=0; + #endif + + #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) + BP_PULLUP_OFF(); + #endif + + #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) + + BP_VREG_OFF();//disable the VREG + + //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! + //AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V + //AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V + //AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup + //AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT + //AD1PCFG&=(~0b11110);//analog pins + + //configure the ADC + //AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + //AD1CSSL = 0; + //AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + //AD1CON2 = 0; + #endif + + U1BRG = 34; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; + +} + +//Write a string to the user terminal +void bpWstring(char *s){ + char c; + while((c = *s++)) UART1TX(c); +} + +//write a string to the user terminal, finish with a line break +void bpWline(char *s){ + char c; + while((c = *s++)) UART1TX(c); + UART1TX(0x0d); + UART1TX(0x0a); + +} + +//output an 8bit/byte hex value to the user terminal +const unsigned char HEXASCII[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; +void bpWhex(unsigned int c){ + unsigned int b; + + bpWstring("0x"); + b = (c>>4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + +// output a 16bit hex value to the user terminal +void bpWinthex(unsigned int c) { + unsigned int b; + + bpWstring("0x"); + b = (c>>12) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c>>8) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c>>4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//delay in MS +void bpDelayMS(const unsigned char delay){ + volatile unsigned int i; + volatile unsigned char c; + + for(c=0;c - -// if you want debug select debug in the MPLAB ide!!! - -#define BPBLVERLOC 0xABFA - -//this sets the hardware version -//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ -//#define BUSPIRATEV2A -//#define BUSPIRATEV25 //AKA Bus Pirate v2go -//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ -#define BUSPIRATEV3 - -#if defined(BUSPIRATEV0A) - #include "hardwarev0a.h" -#elif defined(BUSPIRATEV1A) - #include "hardwarev1a.h" -#elif defined(BUSPIRATEV2A) - #define BUSPIRATEV2 - #include "hardwarev2a.h" -#elif defined(BUSPIRATEV25) - #define BUSPIRATEV2 - #include "hardwarev25.h" -#elif defined(BUSPIRATEV3) - #define BUSPIRATEV2 - #include "hardwarev3.h" -#else - #error "No hardware defined in hardware.h." -#endif - -#endif - - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + + + +/* CvD: most of this is copied and pasted from the svn trunk from the buspirate */ +/* CvD: should we link to there or leave as is? */ + +#ifndef BASE +#define BASE +#include + +// if you want debug select debug in the MPLAB ide!!! + +#define BPBLVERLOC 0xABFA + +//this sets the hardware version +//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ +//#define BUSPIRATEV2A +//#define BUSPIRATEV25 //AKA Bus Pirate v2go +//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ +#define BUSPIRATEV3 + +#if defined(BUSPIRATEV0A) + #include "hardwarev0a.h" +#elif defined(BUSPIRATEV1A) + #include "hardwarev1a.h" +#elif defined(BUSPIRATEV2A) + #define BUSPIRATEV2 + #include "hardwarev2a.h" +#elif defined(BUSPIRATEV25) + #define BUSPIRATEV2 + #include "hardwarev25.h" +#elif defined(BUSPIRATEV3) + #define BUSPIRATEV2 + #include "hardwarev3.h" +#else + #error "No hardware defined in hardware.h." +#endif + +#endif + + + diff --git a/BPv3-bootloader/upgrader-v4tov4/hardwarev0a.h b/BPv3-bootloader/upgrader-v4tov4/hardwarev0a.h index 94cbce81..dcde7975 100644 --- a/BPv3-bootloader/upgrader-v4tov4/hardwarev0a.h +++ b/BPv3-bootloader/upgrader-v4tov4/hardwarev0a.h @@ -1,103 +1,103 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the first, through-hole Bus Pirate. -//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ - -#define BP_VERSION_STRING "Pirate v0a" -#define BP_VERSION "v0a" - -//this configures the proper placement of the serial port for -//the PC side terminal interface -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 4 - -//this is a different type of pin identifier used by the bitbang library -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB5 -#define BP_PULLUP1 PORTBbits.RB11 -#define BP_PULLUP2 PORTBbits.RB10 -#define BP_LEDMODE PORTBbits.RB14 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB5 -#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA -#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL -#define BP_LEDMODE_DIR TRISBbits.TRISB14 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB5 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 5 -#define BP_AUX_RPOUT RPOR2bits.RP5R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for pullup resistors -#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 -#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 - -//define device config bit, ID, and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the first, through-hole Bus Pirate. +//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ + +#define BP_VERSION_STRING "Pirate v0a" +#define BP_VERSION "v0a" + +//this configures the proper placement of the serial port for +//the PC side terminal interface +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 4 + +//this is a different type of pin identifier used by the bitbang library +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB5 +#define BP_PULLUP1 PORTBbits.RB11 +#define BP_PULLUP2 PORTBbits.RB10 +#define BP_LEDMODE PORTBbits.RB14 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB5 +#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA +#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL +#define BP_LEDMODE_DIR TRISBbits.TRISB14 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB5 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 5 +#define BP_AUX_RPOUT RPOR2bits.RP5R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for pullup resistors +#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 +#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 + +//define device config bit, ID, and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v4tov4/hardwarev1a.h b/BPv3-bootloader/upgrader-v4tov4/hardwarev1a.h index 271f6dd9..142a30c6 100644 --- a/BPv3-bootloader/upgrader-v4tov4/hardwarev1a.h +++ b/BPv3-bootloader/upgrader-v4tov4/hardwarev1a.h @@ -1,114 +1,114 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the second, surface mount Bus Pirate. -//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface - -#define BP_VERSION_STRING "Bus Pirate v1a" -#define BP_VERSION "v1a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -//when we move to a USB PIC, we'll need to do that.... -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 3 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_PIN6 PORTBbits.RB11 -#define BP_LEDMODE PORTBbits.RB4 -#define BP_LEDPWR PORTAbits.RA3 -#define BP_EN50V PORTAbits.RA4 -#define BP_EN33V PORTBbits.RB5 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_PIN6_DIR TRISBbits.TRISB11 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_LEDPWR_DIR TRISAbits.TRISA3 -#define BP_EN50V_DIR TRISAbits.TRISA4 -#define BP_EN33V_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 -#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 - -#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 -#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 -#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the second, surface mount Bus Pirate. +//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface + +#define BP_VERSION_STRING "Bus Pirate v1a" +#define BP_VERSION "v1a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +//when we move to a USB PIC, we'll need to do that.... +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 3 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_PIN6 PORTBbits.RB11 +#define BP_LEDMODE PORTBbits.RB4 +#define BP_LEDPWR PORTAbits.RA3 +#define BP_EN50V PORTAbits.RA4 +#define BP_EN33V PORTBbits.RB5 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_PIN6_DIR TRISBbits.TRISB11 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_LEDPWR_DIR TRISAbits.TRISA3 +#define BP_EN50V_DIR TRISAbits.TRISA4 +#define BP_EN33V_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 +#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 + +#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 +#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 +#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v4tov4/hardwarev25.h b/BPv3-bootloader/upgrader-v4tov4/hardwarev25.h index 39167933..d45e5735 100644 --- a/BPv3-bootloader/upgrader-v4tov4/hardwarev25.h +++ b/BPv3-bootloader/upgrader-v4tov4/hardwarev25.h @@ -1,113 +1,113 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this profile is for the Bus Pirate v2go hardware -//http:// - -#define BP_VERSION_STRING "Bus Pirate v2go" -#define BP_VERSION "v2go" - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB1 -#define BP_POST_DIR TRISBbits.TRISB1 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this profile is for the Bus Pirate v2go hardware +//http:// + +#define BP_VERSION_STRING "Bus Pirate v2go" +#define BP_VERSION "v2go" + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB1 +#define BP_POST_DIR TRISBbits.TRISB1 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v4tov4/hardwarev2a.h b/BPv3-bootloader/upgrader-v4tov4/hardwarev2a.h index 652dfb7d..e7da418f 100644 --- a/BPv3-bootloader/upgrader-v4tov4/hardwarev2a.h +++ b/BPv3-bootloader/upgrader-v4tov4/hardwarev2a.h @@ -1,109 +1,109 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for hardware V2a. -//http:// - -#define BP_VERSION_STRING "Bus Pirate v2a" -#define BP_VERSION "v2a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 2 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_VREGEN_DIR TRISAbits.TRISA4 -#define BP_PULLUP_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for hardware V2a. +//http:// + +#define BP_VERSION_STRING "Bus Pirate v2a" +#define BP_VERSION "v2a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 2 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_VREGEN_DIR TRISAbits.TRISA4 +#define BP_PULLUP_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v4tov4/hardwarev3.h b/BPv3-bootloader/upgrader-v4tov4/hardwarev3.h index 2dac548b..e6f3c567 100644 --- a/BPv3-bootloader/upgrader-v4tov4/hardwarev3.h +++ b/BPv3-bootloader/upgrader-v4tov4/hardwarev3.h @@ -1,117 +1,117 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com -#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" -#define BP_VERSION "v3" - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB0 -#define BP_POST_DIR TRISBbits.TRISB0 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//flow control connections for FTDI chip -//used in uart brige -#define FTDI_RTS_DIR TRISAbits.TRISA4 -#define FTDI_RTS PORTAbits.RA4 -#define FTDI_CTS_DIR TRISAbits.TRISA3 -#define FTDI_CTS PORTAbits.RA3 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com +#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" +#define BP_VERSION "v3" + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB0 +#define BP_POST_DIR TRISBbits.TRISB0 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//flow control connections for FTDI chip +//used in uart brige +#define FTDI_RTS_DIR TRISAbits.TRISA4 +#define FTDI_RTS PORTAbits.RA4 +#define FTDI_CTS_DIR TRISAbits.TRISA3 +#define FTDI_CTS PORTAbits.RA3 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/BPv3-bootloader/upgrader-v4tov4/main.c b/BPv3-bootloader/upgrader-v4tov4/main.c index 26c3781d..31a791d3 100644 --- a/BPv3-bootloader/upgrader-v4tov4/main.c +++ b/BPv3-bootloader/upgrader-v4tov4/main.c @@ -1,371 +1,371 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -// CvD: buspirate subs copied from the bus pirate svn -// CvD: Did I already mention it is highly alpha?? use with caution and only if you have a programmer to undo this!! - - -#include "hardware.h" -#include "flash.h" -#include "firmware.h" - -// functions -void BpInit(void); -int main(void); -void bpWstring(char *s); -void bpWinthex(unsigned int c); -void bpWhex(unsigned int c); -void bpWstring(char *s); -void bpWline(char *s); -void UART1TX(char c); -void bpDelayUS(const unsigned char delay); -void bpDelayMS(const unsigned char delay); -unsigned char UART1RXRdy(void); -unsigned char UART1RX(void); -void usermessage(void); -unsigned char checkChar(unsigned char c); -void bpWintdec(unsigned int i); - - -//config fuses. These will be protected and always set like this for all bootloaded firmwares. -// CVD: config fuses deleted so it doesn't upset the new loader ;) -// also disabled them in the workspace/project -//Undid this because p24qp.py erases who chip and update without config bits will brick the pirate -//needed for v2 to v4 legacy upgrades -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need - - -// it all happens from here -int main(void) -{ int i,j; - unsigned char buf[3*64]; - - BpInit(); - BP_LEDMODE=0; // to avoid confusing? - - //wait for confirmation - while(1){ - usermessage(); - if(checkChar('Y')) // absorb 'Yes' and echo it back - { // if any typo is made it should cancel and start over again - if(checkChar('E')) - { - if(checkChar('S')) - { - bpWline(""); - break; - } - } - } - - bpWline(" Upgrade cancelled."); - bpWline(""); - } - bpWline(""); - BP_LEDMODE=1; // on when upgrade active - - // the user is really sure - // erase the page before the bootloader - bpWstring("Erasing "); - bpWinthex(FWLOCATION-0x0400); - bpWstring("..."); // I love dots ;) - Ferase(FWLOCATION); - bpWline(" done"); - - // erase the bootloader location - bpWstring("Erasing "); - bpWinthex(FWLOCATION); - bpWstring("..."); // I love dots ;) - Ferase(FWLOCATION); - bpWline(" done"); - - // write 8 rows from firmware array in firmware.h (8*64=512 'words') - bpWstring("Writing row "); - for(i=0; i<8; i++) - { UART1TX(0x30+i); - UART1TX(' '); - - for(j=0; j<3*64; j++) // fill buffer - { buf[j]=firmware[(i*192)+j]; - } - Fwrite(buf, FWLOCATION+(i*128)); // 1 row = 64 words - } - bpWline("done"); - - // __DEBUG is set in the MPLAB ide/. - - #ifndef __DEBUG - // erase 1st page and write jump to new bootloader - bpWstring("Erasing 0x0000..."); - Ferase(0x0000); - bpWline(" done"); - - bpWstring("Writing row 0... "); - for(j=0; j<(3*64); j++) buf[j]=0xFF;// 1's are easier to write - buf[0]=0x04; // goto FWLOCATION - buf[1]=(FWLOCATION&0x00FF); - buf[2]=((FWLOCATION&0xFF00)>>8); - buf[3]=0x00; // goto FWLOCATION - buf[4]=0x00; - buf[5]=0x00; - Fwrite(buf, 0); - bpWline("done"); - #endif - - // finish up - bpWline(""); - bpWline("Success!"); - bpWline("Upload new firmware with the ds30 Loader app to complete the upgrade."); - - while(U1STAbits.TRMT==0); //wait untill TX finishes - //jump straight into new bootloader - asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location - "goto w1 \n"); - - while(1) - { BP_LEDMODE=1; // we want some attention! - bpDelayMS(250); - bpDelayMS(250); - BP_LEDMODE=0; - bpDelayMS(250); - bpDelayMS(250); - } - - return 0; -} - -unsigned char checkChar(unsigned char c){ - unsigned char d; - d=UART1RX(); - UART1TX(d); - if(d>='a' && d<='z') d-=0x20; //convert to upper - if(c==d) return 1; - return 0; -} - -void usermessage(void){ - bpWline(""); - bpWline("Universal DS30 Loader installer v0.4 (C)hris 2010"); - bpWline(""); - bpWline("Released under Creative Commons null license."); - #ifdef __DEBUG - bpWline("RUNNING IN DEBUG MODE!!!!"); - #endif - - bpWstring("Found bootloader v"); - if(Fread(BPBLVERLOC)==0xFFFF) - { bpWstring(" unknown (probably v2)"); - } - else - { bpWintdec(Fread(BPBLVERLOC)>>8); - UART1TX('.'); - bpWintdec(Fread(BPBLVERLOC)&0x00FF); - } - bpWline(""); - - bpWstring("Will install bootloader v"); - bpWintdec(firmware[FWBLVERLOC_MAJ]); - UART1TX('.'); - bpWintdec(firmware[FWBLVERLOC_MIN]); - bpWline(""); - - bpWline(""); - bpWline("Type 'Yes' to program the new bootloader."); - bpWline("Disconnect power if unsure (no harm done)."); - bpWline(""); -} - -// mostly copied from base.c, baseIO.c and baseUI.h -// CvD: how to keep synchronized with changes in the buspirate trunk?? - -void BpInit(void) -{ - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; - - BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; - - BP_MOSI_DIR=1; - BP_CLK_DIR=1; - BP_MISO_DIR=1; - BP_CS_DIR=1; - BP_AUX_DIR=1; - BP_LEDMODE=0; //mode LED OFF - BP_LEDMODE_DIR=0; - #ifdef BUSPIRATEV1A - BP_PIN6_DIR=1; //AUX2... unused - BP_LEDPWR=1; //light power led - BP_LEDPWR_DIR=0; - #endif - - #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) - BP_PULLUP_OFF(); - #endif - - #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) - - BP_VREG_OFF();//disable the VREG - - //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! - //AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V - //AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V - //AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup - //AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT - //AD1PCFG&=(~0b11110);//analog pins - - //configure the ADC - //AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - //AD1CSSL = 0; - //AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - //AD1CON2 = 0; - #endif - - U1BRG = 34; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; - -} - -//Write a string to the user terminal -void bpWstring(char *s){ - char c; - while((c = *s++)) UART1TX(c); -} - -//write a string to the user terminal, finish with a line break -void bpWline(char *s){ - char c; - while((c = *s++)) UART1TX(c); - UART1TX(0x0d); - UART1TX(0x0a); - -} - -//output an 8bit/byte hex value to the user terminal -const unsigned char HEXASCII[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; -void bpWhex(unsigned int c){ - unsigned int b; - - bpWstring("0x"); - b = (c>>4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - -// output a 16bit hex value to the user terminal -void bpWinthex(unsigned int c) { - unsigned int b; - - bpWstring("0x"); - b = (c>>12) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c>>8) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c>>4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//delay in MS -void bpDelayMS(const unsigned char delay){ - volatile unsigned int i; - volatile unsigned char c; - - for(c=0;c>8); + buf[3]=0x00; // goto FWLOCATION + buf[4]=0x00; + buf[5]=0x00; + Fwrite(buf, 0); + bpWline("done"); + #endif + + // finish up + bpWline(""); + bpWline("Success!"); + bpWline("Upload new firmware with the ds30 Loader app to complete the upgrade."); + + while(U1STAbits.TRMT==0); //wait untill TX finishes + //jump straight into new bootloader + asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location + "goto w1 \n"); + + while(1) + { BP_LEDMODE=1; // we want some attention! + bpDelayMS(250); + bpDelayMS(250); + BP_LEDMODE=0; + bpDelayMS(250); + bpDelayMS(250); + } + + return 0; +} + +unsigned char checkChar(unsigned char c){ + unsigned char d; + d=UART1RX(); + UART1TX(d); + if(d>='a' && d<='z') d-=0x20; //convert to upper + if(c==d) return 1; + return 0; +} + +void usermessage(void){ + bpWline(""); + bpWline("Universal DS30 Loader installer v0.4 (C)hris 2010"); + bpWline(""); + bpWline("Released under Creative Commons null license."); + #ifdef __DEBUG + bpWline("RUNNING IN DEBUG MODE!!!!"); + #endif + + bpWstring("Found bootloader v"); + if(Fread(BPBLVERLOC)==0xFFFF) + { bpWstring(" unknown (probably v2)"); + } + else + { bpWintdec(Fread(BPBLVERLOC)>>8); + UART1TX('.'); + bpWintdec(Fread(BPBLVERLOC)&0x00FF); + } + bpWline(""); + + bpWstring("Will install bootloader v"); + bpWintdec(firmware[FWBLVERLOC_MAJ]); + UART1TX('.'); + bpWintdec(firmware[FWBLVERLOC_MIN]); + bpWline(""); + + bpWline(""); + bpWline("Type 'Yes' to program the new bootloader."); + bpWline("Disconnect power if unsure (no harm done)."); + bpWline(""); +} + +// mostly copied from base.c, baseIO.c and baseUI.h +// CvD: how to keep synchronized with changes in the buspirate trunk?? + +void BpInit(void) +{ + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; + + BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; + BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; + + BP_MOSI_DIR=1; + BP_CLK_DIR=1; + BP_MISO_DIR=1; + BP_CS_DIR=1; + BP_AUX_DIR=1; + BP_LEDMODE=0; //mode LED OFF + BP_LEDMODE_DIR=0; + #ifdef BUSPIRATEV1A + BP_PIN6_DIR=1; //AUX2... unused + BP_LEDPWR=1; //light power led + BP_LEDPWR_DIR=0; + #endif + + #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) + BP_PULLUP_OFF(); + #endif + + #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) + + BP_VREG_OFF();//disable the VREG + + //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! + //AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V + //AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V + //AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup + //AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT + //AD1PCFG&=(~0b11110);//analog pins + + //configure the ADC + //AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + //AD1CSSL = 0; + //AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + //AD1CON2 = 0; + #endif + + U1BRG = 34; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; + +} + +//Write a string to the user terminal +void bpWstring(char *s){ + char c; + while((c = *s++)) UART1TX(c); +} + +//write a string to the user terminal, finish with a line break +void bpWline(char *s){ + char c; + while((c = *s++)) UART1TX(c); + UART1TX(0x0d); + UART1TX(0x0a); + +} + +//output an 8bit/byte hex value to the user terminal +const unsigned char HEXASCII[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; +void bpWhex(unsigned int c){ + unsigned int b; + + bpWstring("0x"); + b = (c>>4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + +// output a 16bit hex value to the user terminal +void bpWinthex(unsigned int c) { + unsigned int b; + + bpWstring("0x"); + b = (c>>12) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c>>8) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c>>4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//delay in MS +void bpDelayMS(const unsigned char delay){ + volatile unsigned int i; + volatile unsigned char c; + + for(c=0;c -#elif defined(__PICC18__) && defined(HI_TECH_C) - // PIC18 processor with (Microchip) HI-TECH PICC-18 compiler - #if !defined(__18CXX) - #define __18CXX - #endif - #define COMPILER_HITECH_PICC18 - #include -#elif (defined(__PIC24F__) || defined(__PIC24FK__)) && defined(__C30__) // Microchip C30 compiler - // PIC24F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__PIC24H__) && defined(__C30__) // Microchip C30 compiler - // PIC24H processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__PIC24E__) && defined(__C30__) // Microchip C30 compiler - // PIC24E processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler - // dsPIC33F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC33E__) && defined(__C30__) // Microchip C30 compiler - // dsPIC33E processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC30F__) && defined(__C30__) // Microchip C30 compiler - // dsPIC30F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__C30__) // Microchip C30 compiler, but targeting "generic-16bit" processor. - #define COMPILER_MPLAB_C30 - #include - // Define some useful inline assembly functions which are normally in the - // processor header files, but absent from the generic p30sim.h file. - #if !defined(Nop) - #define Nop() __builtin_nop() - #define ClrWdt() {__asm__ volatile ("clrwdt");} - #define Sleep() {__asm__ volatile ("pwrsav #0");} - #define Idle() {__asm__ volatile ("pwrsav #1");} - #endif -#elif defined(__PIC32MX__) // Microchip C32 compiler - #if !defined(__C32__) - #define __C32__ - #endif - #define COMPILER_MPLAB_C32 - #include - #include -#else - #error Unknown processor or compiler. See Compiler.h -#endif - -#include -#include -#include - - -// Base RAM and ROM pointer types for given architecture -#if defined(__PIC32MX__) - #define PTR_BASE unsigned long - #define ROM_PTR_BASE unsigned long -#elif defined(__C30__) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned short -#elif defined(COMPILER_MPLAB_C18) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned short long -#elif defined(COMPILER_HITECH_PICC18) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned long -#endif - - -// Definitions that apply to all except Microchip MPLAB C Compiler for PIC18 MCUs (C18) -#if !defined(COMPILER_MPLAB_C18) - #define memcmppgm2ram(a,b,c) memcmp(a,b,c) - #define strcmppgm2ram(a,b) strcmp(a,b) - #define memcpypgm2ram(a,b,c) memcpy(a,b,c) - #define strcpypgm2ram(a,b) strcpy(a,b) - #define strncpypgm2ram(a,b,c) strncpy(a,b,c) - #define strstrrampgm(a,b) strstr(a,b) - #define strlenpgm(a) strlen(a) - #define strchrpgm(a,b) strchr(a,b) - #define strcatpgm2ram(a,b) strcat(a,b) -#endif - - -// Definitions that apply to all 8-bit products -// (PIC18) -#if defined(__18CXX) - #define __attribute__(a) - - #define FAR far - - // Microchip C18 specific defines - #if defined(COMPILER_MPLAB_C18) - #define ROM rom - #endif - - // HI TECH PICC-18 specific defines - #if defined(COMPILER_HITECH_PICC18) - #define ROM const - #define rom - #define Nop() asm("NOP"); - #define ClrWdt() asm("CLRWDT"); - #define Reset() asm("RESET"); - #endif - -// Definitions that apply to all 16-bit and 32-bit products -// (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32) -#else - #define ROM const - - // 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F) - #if defined(__C30__) - #define Reset() asm("reset") - #define FAR __attribute__((far)) - #endif - - // 32-bit specific defines (PIC32) - #if defined(__PIC32MX__) - #define persistent - #define far - #define FAR - #define Reset() SoftReset() - #define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK) - - // MPLAB C Compiler for PIC32 MCUs version 1.04 and below don't have a - // Nop() function. However, version 1.05 has Nop() declared as _nop(). - #if !defined(Nop) && (__C32_VERSION__ <= 104) - #define Nop() asm("nop") - #endif - #endif -#endif - - - -#endif +/********************************************************************* + * + * Compiler and hardware specific definitions + * + ********************************************************************* + * FileName: Compiler.h + * Dependencies: None + * Processor: PIC18, PIC24F, PIC24H, PIC24E, dsPIC30F, dsPIC33F, + * dsPIC33E, PIC32 + * Compiler: Microchip C32 v1.00 or higher + * Microchip C30 v3.01 or higher + * Microchip C18 v3.13 or higher + * HI-TECH PICC-18 PRO 9.63 or higher + * Company: Microchip Technology, Inc. + * + * Software License Agreement + * + * Copyright (C) 2002-2010 Microchip Technology Inc. All rights + * reserved. + * + * Microchip licenses to you the right to use, modify, copy, and + * distribute: + * (i) the Software when embedded on a Microchip microcontroller or + * digital signal controller product ("Device") which is + * integrated into Licensee's product; or + * (ii) ONLY the Software driver source files ENC28J60.c and + * ENC28J60.h ported to a non-Microchip device used in + * conjunction with a Microchip ethernet controller for the + * sole purpose of interfacing with the ethernet controller. + * + * You should refer to the license agreement accompanying this + * Software for additional information regarding your rights and + * obligations. + * + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + * + * Date Comment + *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * 10/03/2006 Original, copied from old Compiler.h + * 11/07/2007 Reorganized and simplified + * 03/31/2010 Removed dependency on WORD and DWORD typedefs + * 04/14/2010 Added defines to uniquely identify each compiler + ********************************************************************/ +#ifndef __COMPILER_H +#define __COMPILER_H + +// Include proper device header file +#if defined(__18CXX) && !defined(HI_TECH_C) + // PIC18 processor with Microchip C18 compiler + #define COMPILER_MPLAB_C18 + #include +#elif defined(__PICC18__) && defined(HI_TECH_C) + // PIC18 processor with (Microchip) HI-TECH PICC-18 compiler + #if !defined(__18CXX) + #define __18CXX + #endif + #define COMPILER_HITECH_PICC18 + #include +#elif (defined(__PIC24F__) || defined(__PIC24FK__)) && defined(__C30__) // Microchip C30 compiler + // PIC24F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__PIC24H__) && defined(__C30__) // Microchip C30 compiler + // PIC24H processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__PIC24E__) && defined(__C30__) // Microchip C30 compiler + // PIC24E processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler + // dsPIC33F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC33E__) && defined(__C30__) // Microchip C30 compiler + // dsPIC33E processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC30F__) && defined(__C30__) // Microchip C30 compiler + // dsPIC30F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__C30__) // Microchip C30 compiler, but targeting "generic-16bit" processor. + #define COMPILER_MPLAB_C30 + #include + // Define some useful inline assembly functions which are normally in the + // processor header files, but absent from the generic p30sim.h file. + #if !defined(Nop) + #define Nop() __builtin_nop() + #define ClrWdt() {__asm__ volatile ("clrwdt");} + #define Sleep() {__asm__ volatile ("pwrsav #0");} + #define Idle() {__asm__ volatile ("pwrsav #1");} + #endif +#elif defined(__PIC32MX__) // Microchip C32 compiler + #if !defined(__C32__) + #define __C32__ + #endif + #define COMPILER_MPLAB_C32 + #include + #include +#else + #error Unknown processor or compiler. See Compiler.h +#endif + +#include +#include +#include + + +// Base RAM and ROM pointer types for given architecture +#if defined(__PIC32MX__) + #define PTR_BASE unsigned long + #define ROM_PTR_BASE unsigned long +#elif defined(__C30__) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned short +#elif defined(COMPILER_MPLAB_C18) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned short long +#elif defined(COMPILER_HITECH_PICC18) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned long +#endif + + +// Definitions that apply to all except Microchip MPLAB C Compiler for PIC18 MCUs (C18) +#if !defined(COMPILER_MPLAB_C18) + #define memcmppgm2ram(a,b,c) memcmp(a,b,c) + #define strcmppgm2ram(a,b) strcmp(a,b) + #define memcpypgm2ram(a,b,c) memcpy(a,b,c) + #define strcpypgm2ram(a,b) strcpy(a,b) + #define strncpypgm2ram(a,b,c) strncpy(a,b,c) + #define strstrrampgm(a,b) strstr(a,b) + #define strlenpgm(a) strlen(a) + #define strchrpgm(a,b) strchr(a,b) + #define strcatpgm2ram(a,b) strcat(a,b) +#endif + + +// Definitions that apply to all 8-bit products +// (PIC18) +#if defined(__18CXX) + #define __attribute__(a) + + #define FAR far + + // Microchip C18 specific defines + #if defined(COMPILER_MPLAB_C18) + #define ROM rom + #endif + + // HI TECH PICC-18 specific defines + #if defined(COMPILER_HITECH_PICC18) + #define ROM const + #define rom + #define Nop() asm("NOP"); + #define ClrWdt() asm("CLRWDT"); + #define Reset() asm("RESET"); + #endif + +// Definitions that apply to all 16-bit and 32-bit products +// (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32) +#else + #define ROM const + + // 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F) + #if defined(__C30__) + #define Reset() asm("reset") + #define FAR __attribute__((far)) + #endif + + // 32-bit specific defines (PIC32) + #if defined(__PIC32MX__) + #define persistent + #define far + #define FAR + #define Reset() SoftReset() + #define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK) + + // MPLAB C Compiler for PIC32 MCUs version 1.04 and below don't have a + // Nop() function. However, version 1.05 has Nop() declared as _nop(). + #if !defined(Nop) && (__C32_VERSION__ <= 104) + #define Nop() asm("nop") + #endif + #endif +#endif + + + +#endif diff --git a/BPv4-bootloader/firmware-v1/GenericTypeDefs.h b/BPv4-bootloader/firmware-v1/GenericTypeDefs.h index 03253b6a..a2819e4e 100644 --- a/BPv4-bootloader/firmware-v1/GenericTypeDefs.h +++ b/BPv4-bootloader/firmware-v1/GenericTypeDefs.h @@ -1,548 +1,548 @@ -/******************************************************************* - - Generic Type Definitions - -******************************************************************** - FileName: GenericTypeDefs.h - Dependencies: None - Processor: PIC10, PIC12, PIC16, PIC18, PIC24, dsPIC, PIC32 - Compiler: MPLAB C Compilers for PIC18, PIC24, dsPIC, & PIC32 - Hi-Tech PICC PRO, Hi-Tech PICC18 PRO - Company: Microchip Technology Inc. - - Software License Agreement - - The software supplied herewith by Microchip Technology Incorporated - (the "Company") is intended and supplied to you, the Company's - customer, for use solely and exclusively with products manufactured - by the Company. - - The software is owned by the Company and/or its supplier, and is - protected under applicable copyright laws. All rights are reserved. - Any use in violation of the foregoing restrictions may subject the - user to criminal sanctions under applicable laws, as well as to - civil liability for the breach of the terms and conditions of this - license. - - THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, - WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED - TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, - IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR - CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. - -******************************************************************** - File Description: - - Change History: - Rev Date Description - 1.1 09/11/06 Add base signed types - 1.2 02/28/07 Add QWORD, LONGLONG, QWORD_VAL - 1.3 02/06/08 Add def's for PIC32 - 1.4 08/08/08 Remove LSB/MSB Macros, adopted by Peripheral lib - 1.5 08/14/08 Simplify file header - 2.0 07/13/09 Updated for new release of coding standards -*******************************************************************/ - -#ifndef __GENERIC_TYPE_DEFS_H_ -#define __GENERIC_TYPE_DEFS_H_ - -/* Specify an extension for GCC based compilers */ -#if defined(__GNUC__) -#define __EXTENSION __extension__ -#else -#define __EXTENSION -#endif - -#if !defined(__PACKED) - #define __PACKED -#endif - -/* get compiler defined type definitions (NULL, size_t, etc) */ -#include - -typedef enum _BOOL { FALSE = 0, TRUE } BOOL; /* Undefined size */ -typedef enum _BIT { CLEAR = 0, SET } BIT; - -#define PUBLIC /* Function attributes */ -#define PROTECTED -#define PRIVATE static - -/* INT is processor specific in length may vary in size */ -typedef signed int INT; -typedef signed char INT8; -typedef signed short int INT16; -typedef signed long int INT32; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -__EXTENSION typedef signed long long INT64; -#endif - -/* UINT is processor specific in length may vary in size */ -typedef unsigned int UINT; -typedef unsigned char UINT8; -typedef unsigned short int UINT16; -/* 24-bit type only available on C18 */ -#if defined(__18CXX) -typedef unsigned short long UINT24; -#endif -typedef unsigned long int UINT32; /* other name for 32-bit integer */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -__EXTENSION typedef unsigned long long UINT64; -#endif - -typedef union -{ - UINT8 Val; - struct - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - } bits; -} UINT8_VAL, UINT8_BITS; - -typedef union -{ - UINT16 Val; - UINT8 v[2] __PACKED; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - } byte; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - } bits; -} UINT16_VAL, UINT16_BITS; - -/* 24-bit type only available on C18 */ -#if defined(__18CXX) -typedef union -{ - UINT24 Val; - UINT8 v[3] __PACKED; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - UINT8 UB; - } byte; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - } bits; -} UINT24_VAL, UINT24_BITS; -#endif - -typedef union -{ - UINT32 Val; - UINT16 w[2] __PACKED; - UINT8 v[4] __PACKED; - struct __PACKED - { - UINT16 LW; - UINT16 HW; - } word; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - UINT8 UB; - UINT8 MB; - } byte; - struct __PACKED - { - UINT16_VAL low; - UINT16_VAL high; - }wordUnion; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - __EXTENSION UINT8 b24:1; - __EXTENSION UINT8 b25:1; - __EXTENSION UINT8 b26:1; - __EXTENSION UINT8 b27:1; - __EXTENSION UINT8 b28:1; - __EXTENSION UINT8 b29:1; - __EXTENSION UINT8 b30:1; - __EXTENSION UINT8 b31:1; - } bits; -} UINT32_VAL; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -typedef union -{ - UINT64 Val; - UINT32 d[2] __PACKED; - UINT16 w[4] __PACKED; - UINT8 v[8] __PACKED; - struct __PACKED - { - UINT32 LD; - UINT32 HD; - } dword; - struct __PACKED - { - UINT16 LW; - UINT16 HW; - UINT16 UW; - UINT16 MW; - } word; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - __EXTENSION UINT8 b24:1; - __EXTENSION UINT8 b25:1; - __EXTENSION UINT8 b26:1; - __EXTENSION UINT8 b27:1; - __EXTENSION UINT8 b28:1; - __EXTENSION UINT8 b29:1; - __EXTENSION UINT8 b30:1; - __EXTENSION UINT8 b31:1; - __EXTENSION UINT8 b32:1; - __EXTENSION UINT8 b33:1; - __EXTENSION UINT8 b34:1; - __EXTENSION UINT8 b35:1; - __EXTENSION UINT8 b36:1; - __EXTENSION UINT8 b37:1; - __EXTENSION UINT8 b38:1; - __EXTENSION UINT8 b39:1; - __EXTENSION UINT8 b40:1; - __EXTENSION UINT8 b41:1; - __EXTENSION UINT8 b42:1; - __EXTENSION UINT8 b43:1; - __EXTENSION UINT8 b44:1; - __EXTENSION UINT8 b45:1; - __EXTENSION UINT8 b46:1; - __EXTENSION UINT8 b47:1; - __EXTENSION UINT8 b48:1; - __EXTENSION UINT8 b49:1; - __EXTENSION UINT8 b50:1; - __EXTENSION UINT8 b51:1; - __EXTENSION UINT8 b52:1; - __EXTENSION UINT8 b53:1; - __EXTENSION UINT8 b54:1; - __EXTENSION UINT8 b55:1; - __EXTENSION UINT8 b56:1; - __EXTENSION UINT8 b57:1; - __EXTENSION UINT8 b58:1; - __EXTENSION UINT8 b59:1; - __EXTENSION UINT8 b60:1; - __EXTENSION UINT8 b61:1; - __EXTENSION UINT8 b62:1; - __EXTENSION UINT8 b63:1; - } bits; -} UINT64_VAL; -#endif /* __18CXX */ - -/***********************************************************************************/ - -/* Alternate definitions */ -typedef void VOID; - -typedef char CHAR8; -typedef unsigned char UCHAR8; - -typedef unsigned char BYTE; /* 8-bit unsigned */ -typedef unsigned short int WORD; /* 16-bit unsigned */ -typedef unsigned long DWORD; /* 32-bit unsigned */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -__EXTENSION -typedef unsigned long long QWORD; /* 64-bit unsigned */ -typedef signed char CHAR; /* 8-bit signed */ -typedef signed short int SHORT; /* 16-bit signed */ -typedef signed long LONG; /* 32-bit signed */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -__EXTENSION -typedef signed long long LONGLONG; /* 64-bit signed */ -typedef union -{ - BYTE Val; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - } bits; -} BYTE_VAL, BYTE_BITS; - -typedef union -{ - WORD Val; - BYTE v[2] __PACKED; - struct __PACKED - { - BYTE LB; - BYTE HB; - } byte; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - } bits; -} WORD_VAL, WORD_BITS; - -typedef union -{ - DWORD Val; - WORD w[2] __PACKED; - BYTE v[4] __PACKED; - struct __PACKED - { - WORD LW; - WORD HW; - } word; - struct __PACKED - { - BYTE LB; - BYTE HB; - BYTE UB; - BYTE MB; - } byte; - struct __PACKED - { - WORD_VAL low; - WORD_VAL high; - }wordUnion; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - __EXTENSION BYTE b16:1; - __EXTENSION BYTE b17:1; - __EXTENSION BYTE b18:1; - __EXTENSION BYTE b19:1; - __EXTENSION BYTE b20:1; - __EXTENSION BYTE b21:1; - __EXTENSION BYTE b22:1; - __EXTENSION BYTE b23:1; - __EXTENSION BYTE b24:1; - __EXTENSION BYTE b25:1; - __EXTENSION BYTE b26:1; - __EXTENSION BYTE b27:1; - __EXTENSION BYTE b28:1; - __EXTENSION BYTE b29:1; - __EXTENSION BYTE b30:1; - __EXTENSION BYTE b31:1; - } bits; -} DWORD_VAL; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -typedef union -{ - QWORD Val; - DWORD d[2] __PACKED; - WORD w[4] __PACKED; - BYTE v[8] __PACKED; - struct __PACKED - { - DWORD LD; - DWORD HD; - } dword; - struct __PACKED - { - WORD LW; - WORD HW; - WORD UW; - WORD MW; - } word; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - __EXTENSION BYTE b16:1; - __EXTENSION BYTE b17:1; - __EXTENSION BYTE b18:1; - __EXTENSION BYTE b19:1; - __EXTENSION BYTE b20:1; - __EXTENSION BYTE b21:1; - __EXTENSION BYTE b22:1; - __EXTENSION BYTE b23:1; - __EXTENSION BYTE b24:1; - __EXTENSION BYTE b25:1; - __EXTENSION BYTE b26:1; - __EXTENSION BYTE b27:1; - __EXTENSION BYTE b28:1; - __EXTENSION BYTE b29:1; - __EXTENSION BYTE b30:1; - __EXTENSION BYTE b31:1; - __EXTENSION BYTE b32:1; - __EXTENSION BYTE b33:1; - __EXTENSION BYTE b34:1; - __EXTENSION BYTE b35:1; - __EXTENSION BYTE b36:1; - __EXTENSION BYTE b37:1; - __EXTENSION BYTE b38:1; - __EXTENSION BYTE b39:1; - __EXTENSION BYTE b40:1; - __EXTENSION BYTE b41:1; - __EXTENSION BYTE b42:1; - __EXTENSION BYTE b43:1; - __EXTENSION BYTE b44:1; - __EXTENSION BYTE b45:1; - __EXTENSION BYTE b46:1; - __EXTENSION BYTE b47:1; - __EXTENSION BYTE b48:1; - __EXTENSION BYTE b49:1; - __EXTENSION BYTE b50:1; - __EXTENSION BYTE b51:1; - __EXTENSION BYTE b52:1; - __EXTENSION BYTE b53:1; - __EXTENSION BYTE b54:1; - __EXTENSION BYTE b55:1; - __EXTENSION BYTE b56:1; - __EXTENSION BYTE b57:1; - __EXTENSION BYTE b58:1; - __EXTENSION BYTE b59:1; - __EXTENSION BYTE b60:1; - __EXTENSION BYTE b61:1; - __EXTENSION BYTE b62:1; - __EXTENSION BYTE b63:1; - } bits; -} QWORD_VAL; - -#undef __EXTENSION - -#endif /* __GENERIC_TYPE_DEFS_H_ */ +/******************************************************************* + + Generic Type Definitions + +******************************************************************** + FileName: GenericTypeDefs.h + Dependencies: None + Processor: PIC10, PIC12, PIC16, PIC18, PIC24, dsPIC, PIC32 + Compiler: MPLAB C Compilers for PIC18, PIC24, dsPIC, & PIC32 + Hi-Tech PICC PRO, Hi-Tech PICC18 PRO + Company: Microchip Technology Inc. + + Software License Agreement + + The software supplied herewith by Microchip Technology Incorporated + (the "Company") is intended and supplied to you, the Company's + customer, for use solely and exclusively with products manufactured + by the Company. + + The software is owned by the Company and/or its supplier, and is + protected under applicable copyright laws. All rights are reserved. + Any use in violation of the foregoing restrictions may subject the + user to criminal sanctions under applicable laws, as well as to + civil liability for the breach of the terms and conditions of this + license. + + THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, + WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED + TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, + IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR + CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + +******************************************************************** + File Description: + + Change History: + Rev Date Description + 1.1 09/11/06 Add base signed types + 1.2 02/28/07 Add QWORD, LONGLONG, QWORD_VAL + 1.3 02/06/08 Add def's for PIC32 + 1.4 08/08/08 Remove LSB/MSB Macros, adopted by Peripheral lib + 1.5 08/14/08 Simplify file header + 2.0 07/13/09 Updated for new release of coding standards +*******************************************************************/ + +#ifndef __GENERIC_TYPE_DEFS_H_ +#define __GENERIC_TYPE_DEFS_H_ + +/* Specify an extension for GCC based compilers */ +#if defined(__GNUC__) +#define __EXTENSION __extension__ +#else +#define __EXTENSION +#endif + +#if !defined(__PACKED) + #define __PACKED +#endif + +/* get compiler defined type definitions (NULL, size_t, etc) */ +#include + +typedef enum _BOOL { FALSE = 0, TRUE } BOOL; /* Undefined size */ +typedef enum _BIT { CLEAR = 0, SET } BIT; + +#define PUBLIC /* Function attributes */ +#define PROTECTED +#define PRIVATE static + +/* INT is processor specific in length may vary in size */ +typedef signed int INT; +typedef signed char INT8; +typedef signed short int INT16; +typedef signed long int INT32; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +__EXTENSION typedef signed long long INT64; +#endif + +/* UINT is processor specific in length may vary in size */ +typedef unsigned int UINT; +typedef unsigned char UINT8; +typedef unsigned short int UINT16; +/* 24-bit type only available on C18 */ +#if defined(__18CXX) +typedef unsigned short long UINT24; +#endif +typedef unsigned long int UINT32; /* other name for 32-bit integer */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +__EXTENSION typedef unsigned long long UINT64; +#endif + +typedef union +{ + UINT8 Val; + struct + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + } bits; +} UINT8_VAL, UINT8_BITS; + +typedef union +{ + UINT16 Val; + UINT8 v[2] __PACKED; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + } byte; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + } bits; +} UINT16_VAL, UINT16_BITS; + +/* 24-bit type only available on C18 */ +#if defined(__18CXX) +typedef union +{ + UINT24 Val; + UINT8 v[3] __PACKED; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + UINT8 UB; + } byte; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + } bits; +} UINT24_VAL, UINT24_BITS; +#endif + +typedef union +{ + UINT32 Val; + UINT16 w[2] __PACKED; + UINT8 v[4] __PACKED; + struct __PACKED + { + UINT16 LW; + UINT16 HW; + } word; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + UINT8 UB; + UINT8 MB; + } byte; + struct __PACKED + { + UINT16_VAL low; + UINT16_VAL high; + }wordUnion; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + __EXTENSION UINT8 b24:1; + __EXTENSION UINT8 b25:1; + __EXTENSION UINT8 b26:1; + __EXTENSION UINT8 b27:1; + __EXTENSION UINT8 b28:1; + __EXTENSION UINT8 b29:1; + __EXTENSION UINT8 b30:1; + __EXTENSION UINT8 b31:1; + } bits; +} UINT32_VAL; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +typedef union +{ + UINT64 Val; + UINT32 d[2] __PACKED; + UINT16 w[4] __PACKED; + UINT8 v[8] __PACKED; + struct __PACKED + { + UINT32 LD; + UINT32 HD; + } dword; + struct __PACKED + { + UINT16 LW; + UINT16 HW; + UINT16 UW; + UINT16 MW; + } word; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + __EXTENSION UINT8 b24:1; + __EXTENSION UINT8 b25:1; + __EXTENSION UINT8 b26:1; + __EXTENSION UINT8 b27:1; + __EXTENSION UINT8 b28:1; + __EXTENSION UINT8 b29:1; + __EXTENSION UINT8 b30:1; + __EXTENSION UINT8 b31:1; + __EXTENSION UINT8 b32:1; + __EXTENSION UINT8 b33:1; + __EXTENSION UINT8 b34:1; + __EXTENSION UINT8 b35:1; + __EXTENSION UINT8 b36:1; + __EXTENSION UINT8 b37:1; + __EXTENSION UINT8 b38:1; + __EXTENSION UINT8 b39:1; + __EXTENSION UINT8 b40:1; + __EXTENSION UINT8 b41:1; + __EXTENSION UINT8 b42:1; + __EXTENSION UINT8 b43:1; + __EXTENSION UINT8 b44:1; + __EXTENSION UINT8 b45:1; + __EXTENSION UINT8 b46:1; + __EXTENSION UINT8 b47:1; + __EXTENSION UINT8 b48:1; + __EXTENSION UINT8 b49:1; + __EXTENSION UINT8 b50:1; + __EXTENSION UINT8 b51:1; + __EXTENSION UINT8 b52:1; + __EXTENSION UINT8 b53:1; + __EXTENSION UINT8 b54:1; + __EXTENSION UINT8 b55:1; + __EXTENSION UINT8 b56:1; + __EXTENSION UINT8 b57:1; + __EXTENSION UINT8 b58:1; + __EXTENSION UINT8 b59:1; + __EXTENSION UINT8 b60:1; + __EXTENSION UINT8 b61:1; + __EXTENSION UINT8 b62:1; + __EXTENSION UINT8 b63:1; + } bits; +} UINT64_VAL; +#endif /* __18CXX */ + +/***********************************************************************************/ + +/* Alternate definitions */ +typedef void VOID; + +typedef char CHAR8; +typedef unsigned char UCHAR8; + +typedef unsigned char BYTE; /* 8-bit unsigned */ +typedef unsigned short int WORD; /* 16-bit unsigned */ +typedef unsigned long DWORD; /* 32-bit unsigned */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +__EXTENSION +typedef unsigned long long QWORD; /* 64-bit unsigned */ +typedef signed char CHAR; /* 8-bit signed */ +typedef signed short int SHORT; /* 16-bit signed */ +typedef signed long LONG; /* 32-bit signed */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +__EXTENSION +typedef signed long long LONGLONG; /* 64-bit signed */ +typedef union +{ + BYTE Val; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + } bits; +} BYTE_VAL, BYTE_BITS; + +typedef union +{ + WORD Val; + BYTE v[2] __PACKED; + struct __PACKED + { + BYTE LB; + BYTE HB; + } byte; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + } bits; +} WORD_VAL, WORD_BITS; + +typedef union +{ + DWORD Val; + WORD w[2] __PACKED; + BYTE v[4] __PACKED; + struct __PACKED + { + WORD LW; + WORD HW; + } word; + struct __PACKED + { + BYTE LB; + BYTE HB; + BYTE UB; + BYTE MB; + } byte; + struct __PACKED + { + WORD_VAL low; + WORD_VAL high; + }wordUnion; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + __EXTENSION BYTE b16:1; + __EXTENSION BYTE b17:1; + __EXTENSION BYTE b18:1; + __EXTENSION BYTE b19:1; + __EXTENSION BYTE b20:1; + __EXTENSION BYTE b21:1; + __EXTENSION BYTE b22:1; + __EXTENSION BYTE b23:1; + __EXTENSION BYTE b24:1; + __EXTENSION BYTE b25:1; + __EXTENSION BYTE b26:1; + __EXTENSION BYTE b27:1; + __EXTENSION BYTE b28:1; + __EXTENSION BYTE b29:1; + __EXTENSION BYTE b30:1; + __EXTENSION BYTE b31:1; + } bits; +} DWORD_VAL; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +typedef union +{ + QWORD Val; + DWORD d[2] __PACKED; + WORD w[4] __PACKED; + BYTE v[8] __PACKED; + struct __PACKED + { + DWORD LD; + DWORD HD; + } dword; + struct __PACKED + { + WORD LW; + WORD HW; + WORD UW; + WORD MW; + } word; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + __EXTENSION BYTE b16:1; + __EXTENSION BYTE b17:1; + __EXTENSION BYTE b18:1; + __EXTENSION BYTE b19:1; + __EXTENSION BYTE b20:1; + __EXTENSION BYTE b21:1; + __EXTENSION BYTE b22:1; + __EXTENSION BYTE b23:1; + __EXTENSION BYTE b24:1; + __EXTENSION BYTE b25:1; + __EXTENSION BYTE b26:1; + __EXTENSION BYTE b27:1; + __EXTENSION BYTE b28:1; + __EXTENSION BYTE b29:1; + __EXTENSION BYTE b30:1; + __EXTENSION BYTE b31:1; + __EXTENSION BYTE b32:1; + __EXTENSION BYTE b33:1; + __EXTENSION BYTE b34:1; + __EXTENSION BYTE b35:1; + __EXTENSION BYTE b36:1; + __EXTENSION BYTE b37:1; + __EXTENSION BYTE b38:1; + __EXTENSION BYTE b39:1; + __EXTENSION BYTE b40:1; + __EXTENSION BYTE b41:1; + __EXTENSION BYTE b42:1; + __EXTENSION BYTE b43:1; + __EXTENSION BYTE b44:1; + __EXTENSION BYTE b45:1; + __EXTENSION BYTE b46:1; + __EXTENSION BYTE b47:1; + __EXTENSION BYTE b48:1; + __EXTENSION BYTE b49:1; + __EXTENSION BYTE b50:1; + __EXTENSION BYTE b51:1; + __EXTENSION BYTE b52:1; + __EXTENSION BYTE b53:1; + __EXTENSION BYTE b54:1; + __EXTENSION BYTE b55:1; + __EXTENSION BYTE b56:1; + __EXTENSION BYTE b57:1; + __EXTENSION BYTE b58:1; + __EXTENSION BYTE b59:1; + __EXTENSION BYTE b60:1; + __EXTENSION BYTE b61:1; + __EXTENSION BYTE b62:1; + __EXTENSION BYTE b63:1; + } bits; +} QWORD_VAL; + +#undef __EXTENSION + +#endif /* __GENERIC_TYPE_DEFS_H_ */ diff --git a/BPv4-bootloader/firmware-v1/boot_config.h b/BPv4-bootloader/firmware-v1/boot_config.h index ba73f6b2..434c5b40 100644 --- a/BPv4-bootloader/firmware-v1/boot_config.h +++ b/BPv4-bootloader/firmware-v1/boot_config.h @@ -1,92 +1,92 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California,\ - * - 94105, - USA. - -JTR -This file operates as the global header for implementation -It is applicable to both PIC18 and PIC24. - -This is the main BOSS configuration file. It is to be included -in: - -usb_stack.c -cdc.c or other CLASS file. -configwords.h -main.c -descriptors.h - -In this file you can define: - -ENDPOINT SIZES Applies to EP0 and other class specific endpoints -PING-PONG MODE -BUS or SELF POWERED -VID & PID -USB TRANSCEIVER MODE -USB_NUM_CONFIGURATIONS -USB_NUM_INTERFACES -USB_NUM_ENDPOINTS -USB_NUM_STRINGS - -USB configured LED -Other misc LEDs and hardware. - -Low power request macro -*/ - -#ifndef __BOOT_CONFIG_H__ -#define __BOOT_CONFIG_H__ - -#define MODELED LATBbits.LATB8 -#define USBLED LATBbits.LATB10 //Active LOW - -#define USBLEDON() USBLED = 0 - -#define USB_VID (0x04d8) -#define USB_PID (0xFAFF) //; DangerousPrototypes Bootloader VID/PID -#define USB_DEV 0x0002 - -#define USB_NUM_CONFIGURATIONS 1u -#define USB_NUM_INTERFACES 2u -#define USB_NUM_ENDPOINTS 3u -#define USB_NUM_STRINGS 4u - -#define MAX_EPNUM_USED 2u - -#define USB_BUS_POWERED 1 -#define USB_INTERNAL_TRANSCIEVER 1 -#define USB_INTERNAL_PULLUPS 1 -#define USB_INTERNAL_VREG 1 -#define USB_FULL_SPEED_DEVICE 1 - -//#define USB_INTERRUPT 1 - -/* PingPong Buffer Mode - * Valid values - * 0 - No PingPong Buffers - * 1 - PingPong on EP0 - * 2 - PingPong on all EP - * 3 - PingPong on all except EP0 - */ -#define USB_PP_BUF_MODE 0 -#define USB_EP0_BUFFER_SIZE 8u -#define CDC_BUFFER_SIZE 64u -#define CDC_NOTICE_BUFFER_SIZE 10u - -/* Low Power Request - * Optional user supplied subroutine to set the circuit - * into low power mode during usb suspend. - * Probably needed when bus powered as only 2.5mA should - * be drawn from bus i suspend mode */ -//#define usb_low_power_request() Nop() - - -#endif /* BOOT_CONFIG_H */ +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California,\ + * + 94105, + USA. + +JTR +This file operates as the global header for implementation +It is applicable to both PIC18 and PIC24. + +This is the main BOSS configuration file. It is to be included +in: + +usb_stack.c +cdc.c or other CLASS file. +configwords.h +main.c +descriptors.h + +In this file you can define: + +ENDPOINT SIZES Applies to EP0 and other class specific endpoints +PING-PONG MODE +BUS or SELF POWERED +VID & PID +USB TRANSCEIVER MODE +USB_NUM_CONFIGURATIONS +USB_NUM_INTERFACES +USB_NUM_ENDPOINTS +USB_NUM_STRINGS + +USB configured LED +Other misc LEDs and hardware. + +Low power request macro +*/ + +#ifndef __BOOT_CONFIG_H__ +#define __BOOT_CONFIG_H__ + +#define MODELED LATBbits.LATB8 +#define USBLED LATBbits.LATB10 //Active LOW + +#define USBLEDON() USBLED = 0 + +#define USB_VID (0x04d8) +#define USB_PID (0xFAFF) //; DangerousPrototypes Bootloader VID/PID +#define USB_DEV 0x0002 + +#define USB_NUM_CONFIGURATIONS 1u +#define USB_NUM_INTERFACES 2u +#define USB_NUM_ENDPOINTS 3u +#define USB_NUM_STRINGS 4u + +#define MAX_EPNUM_USED 2u + +#define USB_BUS_POWERED 1 +#define USB_INTERNAL_TRANSCIEVER 1 +#define USB_INTERNAL_PULLUPS 1 +#define USB_INTERNAL_VREG 1 +#define USB_FULL_SPEED_DEVICE 1 + +//#define USB_INTERRUPT 1 + +/* PingPong Buffer Mode + * Valid values + * 0 - No PingPong Buffers + * 1 - PingPong on EP0 + * 2 - PingPong on all EP + * 3 - PingPong on all except EP0 + */ +#define USB_PP_BUF_MODE 0 +#define USB_EP0_BUFFER_SIZE 8u +#define CDC_BUFFER_SIZE 64u +#define CDC_NOTICE_BUFFER_SIZE 10u + +/* Low Power Request + * Optional user supplied subroutine to set the circuit + * into low power mode during usb suspend. + * Probably needed when bus powered as only 2.5mA should + * be drawn from bus i suspend mode */ +//#define usb_low_power_request() Nop() + + +#endif /* BOOT_CONFIG_H */ diff --git a/BPv4-bootloader/firmware-v1/bootloader.c b/BPv4-bootloader/firmware-v1/bootloader.c index c4e1fd40..9ca754fd 100644 --- a/BPv4-bootloader/firmware-v1/bootloader.c +++ b/BPv4-bootloader/firmware-v1/bootloader.c @@ -1,259 +1,259 @@ - -#include "globals.h" -#include "bootloader.h" - -extern struct _irtoyusbbuffer usbbuf; -void usbbufservice(void); -void bootloader(void); -void usb_handler(void); -void WritePage(void); -void __builtin_write_NVM(void); -void __builtin_tblwtl(unsigned int offset, unsigned int data); -void __builtin_tblwth(unsigned int offset, unsigned int data); -unsigned int __builtin_tblrdl(unsigned int offset); -unsigned int __builtin_tblrdh(unsigned int offset); - - -BYTE inbyte; // JTR3 Global -BYTE errflag; -long fulladdress; - -BYTE bldone = 0; -extern BYTE cdc_In_buffer[64]; -extern BYTE cdc_Out_buffer[64]; -#define VER_H 0x04 -#define VER_L 0x06 - -unsigned int userversion __attribute__((space(prog),address(BLENDADDR-9))) = ((VER_H<<8)|VER_L); - -int foo __attribute__((section("mysection"),address(0x2000))); - -struct _bootstruct { - BYTE enableerase; - BYTE blreturn; - BYTE addrU; - BYTE addrH; - BYTE addrL; - BYTE cmd; - BYTE datasize; - BYTE checksum; - BYTE data[64 * 3]; - -} bootstruct; - -#define HAS_PAGES -#define PROT_BL -#define PROT_GOTO -#define PROT_CONFIG - - -void bootloader(void) { - volatile unsigned int i,j; - BYTE crc; - - bootstruct.enableerase = 0; - - do { - do { - usb_handler(); - usbbufservice(); - } while (usbbufgetbyte(&inbyte) == 0); - - } while (inbyte != 0xc1); //wait for bootloader hello - - //send device and protocol info - WaitInReady(); - cdc_In_buffer[0] = DEVICEID; //answer OK - cdc_In_buffer[1] = VER_H; - cdc_In_buffer[2] = VER_L; - putUnsignedCharArrayUsbUsart(cdc_In_buffer, 3); - - //if no errors below we return K for OK - bootstruct.blreturn = 'K'; - - // MAIN BOOTLOADER LOOP HERE - do { -error: - usb_handler(); - WaitInReady(); - cdc_In_buffer[0] = bootstruct.blreturn; //answer OK - putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1); - - crc=0; - - //get bootloader command - usbbufgetbyte(&bootstruct.addrU); - crc+=bootstruct.addrU; - usbbufgetbyte(&bootstruct.addrH); - crc+=bootstruct.addrH; - usbbufgetbyte(&bootstruct.addrL); - crc+=bootstruct.addrL; - usbbufgetbyte(&bootstruct.cmd); - crc+=bootstruct.cmd; - usbbufgetbyte(&bootstruct.datasize); - crc+=bootstruct.datasize; - - TBLPAG = bootstruct.addrU; - - //get data, if any - if (bootstruct.datasize > 1) { - for (i = 0; i < bootstruct.datasize - 1; i++) { - usbbufgetbyte(&bootstruct.data[i]); - crc+=bootstruct.data[i]; - } - } - - //get checksum - usbbufgetbyte(&bootstruct.checksum); - crc+=bootstruct.checksum; - // TODO add checksum computation and check - if(crc!=0){ - bootstruct.blreturn='N';//return checksum error - goto error; - - } - - //calculate flash address - //fulladdress = ( ((bootstruct.addrU) << 16) + ((bootstruct.addrH) << 8) + bootstruct.addrL); - fulladdress = (bootstruct.addrU); - fulladdress=fulladdress<<8; - fulladdress+=bootstruct.addrH; - fulladdress=fulladdress<<8; - fulladdress+=bootstruct.addrL; - - //do command - switch (bootstruct.cmd) { - case 1: // enable erase, actual erase is before the next write - bootstruct.enableerase = 1; - break; - case 2: //protect the bootloader and write the row - WritePage(); - break; - case 0xff: - U1CONbits.USBEN=0; //USB off - //delay a while so computer sees us turn off USB - j=0xFFFF; - while(j--){ - i=0xFFFF; - while(i--); - } - asm("RESET");//reset - default: //unknown command - bootstruct.blreturn = 'U'; - break; - }//switch(c) - } while (!bldone); -} - -void WritePage() { - BYTE i; - int dataword; - unsigned int offset; - BYTE errflag = 0; - - //check that we don't overwrite any important bootloader stuff - - - #ifdef PROT_GOTO - if (fulladdress == 0x0000) {//protect jump vector - bootstruct.data[0] = 0x04; //replace with bootloader start address - bootstruct.data[1] = (unsigned char) (BLSTARTADDR); - bootstruct.data[2] = (unsigned char) (BLSTARTADDR >> 8); - bootstruct.data[3] = 0x00; - bootstruct.data[4] = (unsigned char) ((BLSTARTADDR >> 16) && 0xFF); - bootstruct.data[5] = 0x00; - } // EDIT to add this - #endif - - #ifdef PROT_BL - if ((fulladdress <= BLENDADDR) && (fulladdress >= BLSTARTADDR)) {//protect config words - bootstruct.blreturn = 'P'; - errflag = 1; - } - #endif - - #ifdef PROT_CONFIG // best guess not tested yet - if (fulladdress >= (FLASHSIZE - (2 * (PAGESIZER * ROWSIZEW)))) {//protect config page - bootstruct.blreturn = 'P'; - errflag = 1; - } - #endif -/* - - if (fulladdress == 0x0000) {//protect jump vector -#ifdef PROT_CONFIG - bootstruct.data[0]=0x04; //replace with bootloader start address - bootstruct.data[1]=(unsigned char)(BLSTARTADDR); - bootstruct.data[2]=(unsigned char)(BLSTARTADDR>>8); - bootstruct.data[3]=0x00; - bootstruct.data[4]=(unsigned char) ((BLSTARTADDR >> 16) && 0xFF); - bootstruct.data[5]=0x00; -#endif - }else if ((fulladdress<=BLENDADDR) && (fulladdress>=BLSTARTADDR)) {//protect bootloader -#ifdef PROT_BL - bootstruct.blreturn = 'P'; - errflag = 1; -#endif - }else if (fulladdress >= (FLASHSIZE - (2 * (PAGESIZER * ROWSIZEW)))) {//protect config page -#ifdef PROT_CONFIG - bootstruct.blreturn = 'P'; - errflag = 1; -#endif - } -*/ - if (!errflag) { //if no protection error, write row - - //erase page if enabled - if (bootstruct.enableerase) { - NVMCON = 0x4042; //Erase page on next WR - - __builtin_tblwtl((unsigned int) fulladdress, 0xFFFF); - __builtin_write_NVM(); - while (NVMCONbits.WR == 1); - bootstruct.enableerase = 0; - } - - offset = (unsigned int) fulladdress; - - NVMCON = 0x4001; //setup row writes - //write data to buffer - for (i = 0; i < (bootstruct.datasize-1);) - { - dataword = bootstruct.data[i]; - __builtin_tblwth(offset, dataword); - i++; - dataword = bootstruct.data[i]; - i++; - dataword |= (bootstruct.data[i] << 8); - i++; - __builtin_tblwtl(offset, dataword); - offset +=2; - } - //write buffer to flash - __builtin_write_NVM(); - while (NVMCONbits.WR == 1); - NVMCONbits.WREN = 0; - - offset = (unsigned int) fulladdress; - bootstruct.blreturn = 'K'; - //read data back and compare - for (i = 0; i < (bootstruct.datasize-1);) - { - dataword = __builtin_tblrdh((unsigned int) (offset)); - dataword &= 0xFF; - if (dataword != bootstruct.data[i]) - bootstruct.blreturn = 'V'; - i++; - dataword = __builtin_tblrdl((unsigned int) offset); - if ((BYTE) dataword != bootstruct.data[i]) - bootstruct.blreturn = 'V'; - i++; - if ((BYTE) (dataword >> 8) != bootstruct.data[i]) - bootstruct.blreturn = 'V'; - i++; - offset +=2; - } - - } -} - + +#include "globals.h" +#include "bootloader.h" + +extern struct _irtoyusbbuffer usbbuf; +void usbbufservice(void); +void bootloader(void); +void usb_handler(void); +void WritePage(void); +void __builtin_write_NVM(void); +void __builtin_tblwtl(unsigned int offset, unsigned int data); +void __builtin_tblwth(unsigned int offset, unsigned int data); +unsigned int __builtin_tblrdl(unsigned int offset); +unsigned int __builtin_tblrdh(unsigned int offset); + + +BYTE inbyte; // JTR3 Global +BYTE errflag; +long fulladdress; + +BYTE bldone = 0; +extern BYTE cdc_In_buffer[64]; +extern BYTE cdc_Out_buffer[64]; +#define VER_H 0x04 +#define VER_L 0x06 + +unsigned int userversion __attribute__((space(prog),address(BLENDADDR-9))) = ((VER_H<<8)|VER_L); + +int foo __attribute__((section("mysection"),address(0x2000))); + +struct _bootstruct { + BYTE enableerase; + BYTE blreturn; + BYTE addrU; + BYTE addrH; + BYTE addrL; + BYTE cmd; + BYTE datasize; + BYTE checksum; + BYTE data[64 * 3]; + +} bootstruct; + +#define HAS_PAGES +#define PROT_BL +#define PROT_GOTO +#define PROT_CONFIG + + +void bootloader(void) { + volatile unsigned int i,j; + BYTE crc; + + bootstruct.enableerase = 0; + + do { + do { + usb_handler(); + usbbufservice(); + } while (usbbufgetbyte(&inbyte) == 0); + + } while (inbyte != 0xc1); //wait for bootloader hello + + //send device and protocol info + WaitInReady(); + cdc_In_buffer[0] = DEVICEID; //answer OK + cdc_In_buffer[1] = VER_H; + cdc_In_buffer[2] = VER_L; + putUnsignedCharArrayUsbUsart(cdc_In_buffer, 3); + + //if no errors below we return K for OK + bootstruct.blreturn = 'K'; + + // MAIN BOOTLOADER LOOP HERE + do { +error: + usb_handler(); + WaitInReady(); + cdc_In_buffer[0] = bootstruct.blreturn; //answer OK + putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1); + + crc=0; + + //get bootloader command + usbbufgetbyte(&bootstruct.addrU); + crc+=bootstruct.addrU; + usbbufgetbyte(&bootstruct.addrH); + crc+=bootstruct.addrH; + usbbufgetbyte(&bootstruct.addrL); + crc+=bootstruct.addrL; + usbbufgetbyte(&bootstruct.cmd); + crc+=bootstruct.cmd; + usbbufgetbyte(&bootstruct.datasize); + crc+=bootstruct.datasize; + + TBLPAG = bootstruct.addrU; + + //get data, if any + if (bootstruct.datasize > 1) { + for (i = 0; i < bootstruct.datasize - 1; i++) { + usbbufgetbyte(&bootstruct.data[i]); + crc+=bootstruct.data[i]; + } + } + + //get checksum + usbbufgetbyte(&bootstruct.checksum); + crc+=bootstruct.checksum; + // TODO add checksum computation and check + if(crc!=0){ + bootstruct.blreturn='N';//return checksum error + goto error; + + } + + //calculate flash address + //fulladdress = ( ((bootstruct.addrU) << 16) + ((bootstruct.addrH) << 8) + bootstruct.addrL); + fulladdress = (bootstruct.addrU); + fulladdress=fulladdress<<8; + fulladdress+=bootstruct.addrH; + fulladdress=fulladdress<<8; + fulladdress+=bootstruct.addrL; + + //do command + switch (bootstruct.cmd) { + case 1: // enable erase, actual erase is before the next write + bootstruct.enableerase = 1; + break; + case 2: //protect the bootloader and write the row + WritePage(); + break; + case 0xff: + U1CONbits.USBEN=0; //USB off + //delay a while so computer sees us turn off USB + j=0xFFFF; + while(j--){ + i=0xFFFF; + while(i--); + } + asm("RESET");//reset + default: //unknown command + bootstruct.blreturn = 'U'; + break; + }//switch(c) + } while (!bldone); +} + +void WritePage() { + BYTE i; + int dataword; + unsigned int offset; + BYTE errflag = 0; + + //check that we don't overwrite any important bootloader stuff + + + #ifdef PROT_GOTO + if (fulladdress == 0x0000) {//protect jump vector + bootstruct.data[0] = 0x04; //replace with bootloader start address + bootstruct.data[1] = (unsigned char) (BLSTARTADDR); + bootstruct.data[2] = (unsigned char) (BLSTARTADDR >> 8); + bootstruct.data[3] = 0x00; + bootstruct.data[4] = (unsigned char) ((BLSTARTADDR >> 16) && 0xFF); + bootstruct.data[5] = 0x00; + } // EDIT to add this + #endif + + #ifdef PROT_BL + if ((fulladdress <= BLENDADDR) && (fulladdress >= BLSTARTADDR)) {//protect config words + bootstruct.blreturn = 'P'; + errflag = 1; + } + #endif + + #ifdef PROT_CONFIG // best guess not tested yet + if (fulladdress >= (FLASHSIZE - (2 * (PAGESIZER * ROWSIZEW)))) {//protect config page + bootstruct.blreturn = 'P'; + errflag = 1; + } + #endif +/* + + if (fulladdress == 0x0000) {//protect jump vector +#ifdef PROT_CONFIG + bootstruct.data[0]=0x04; //replace with bootloader start address + bootstruct.data[1]=(unsigned char)(BLSTARTADDR); + bootstruct.data[2]=(unsigned char)(BLSTARTADDR>>8); + bootstruct.data[3]=0x00; + bootstruct.data[4]=(unsigned char) ((BLSTARTADDR >> 16) && 0xFF); + bootstruct.data[5]=0x00; +#endif + }else if ((fulladdress<=BLENDADDR) && (fulladdress>=BLSTARTADDR)) {//protect bootloader +#ifdef PROT_BL + bootstruct.blreturn = 'P'; + errflag = 1; +#endif + }else if (fulladdress >= (FLASHSIZE - (2 * (PAGESIZER * ROWSIZEW)))) {//protect config page +#ifdef PROT_CONFIG + bootstruct.blreturn = 'P'; + errflag = 1; +#endif + } +*/ + if (!errflag) { //if no protection error, write row + + //erase page if enabled + if (bootstruct.enableerase) { + NVMCON = 0x4042; //Erase page on next WR + + __builtin_tblwtl((unsigned int) fulladdress, 0xFFFF); + __builtin_write_NVM(); + while (NVMCONbits.WR == 1); + bootstruct.enableerase = 0; + } + + offset = (unsigned int) fulladdress; + + NVMCON = 0x4001; //setup row writes + //write data to buffer + for (i = 0; i < (bootstruct.datasize-1);) + { + dataword = bootstruct.data[i]; + __builtin_tblwth(offset, dataword); + i++; + dataword = bootstruct.data[i]; + i++; + dataword |= (bootstruct.data[i] << 8); + i++; + __builtin_tblwtl(offset, dataword); + offset +=2; + } + //write buffer to flash + __builtin_write_NVM(); + while (NVMCONbits.WR == 1); + NVMCONbits.WREN = 0; + + offset = (unsigned int) fulladdress; + bootstruct.blreturn = 'K'; + //read data back and compare + for (i = 0; i < (bootstruct.datasize-1);) + { + dataword = __builtin_tblrdh((unsigned int) (offset)); + dataword &= 0xFF; + if (dataword != bootstruct.data[i]) + bootstruct.blreturn = 'V'; + i++; + dataword = __builtin_tblrdl((unsigned int) offset); + if ((BYTE) dataword != bootstruct.data[i]) + bootstruct.blreturn = 'V'; + i++; + if ((BYTE) (dataword >> 8) != bootstruct.data[i]) + bootstruct.blreturn = 'V'; + i++; + offset +=2; + } + + } +} + diff --git a/BPv4-bootloader/firmware-v1/bootloader.h b/BPv4-bootloader/firmware-v1/bootloader.h index 8cbec266..273bc16a 100644 --- a/BPv4-bootloader/firmware-v1/bootloader.h +++ b/BPv4-bootloader/firmware-v1/bootloader.h @@ -1,470 +1,470 @@ -// ;----------------------------------------------------------------------------- -// ; This file is part of ds30 Loader. -// ; -// ; ds30 Loader 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. -// ; -// ; ds30 Loader 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 ds30 Loader. If not see . -// ;------------------------------------------------------------------------------ - -#include "boot_config.h" -#define PAGESIZER 8 -#define ROWSIZEW 64 -#ifdef __PIC24FJ256GB106__ -//#include "p24FJ256GB106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 241 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x1FFFL -#endif -#ifdef __PIC24FJ128GB206__ -//#include "p24FJ128GB206.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 9 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128GB210__ -//#include "p24FJ128GB210.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 17 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256GB206__ -//#include "p24FJ256GB206.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 18 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256GB210__ -//#include "p24FJ256GB210.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 19 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256DA206__ -//#include "p24FJ256DA206.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 191 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256DA210__ -//#include "p24FJ256DA210.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 192 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif - -#ifdef __PIC24FJ64GB106__ -//#include "p24FJ64GB106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0xAC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 217 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ64GB108__ -//#include "p24FJ64GB108.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0xAC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 218 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ64GB110__ -//#include "p24FJ64GB110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0xAC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 219 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128GB106__ -//#include "p24FJ128GB106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 229 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128GB108__ -//#include "p24FJ128GB108.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 230 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128GB110__ -//#include "p24FJ128GB110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 231 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ192GB106__ -//#include "p24FJ192GB106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x20C00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 235 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ192GB108__ -//#include "p24FJ192GB108.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x20C00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 236 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ192GB110__ -//#include "p24FJ192GB110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x20C00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 237 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256GB108__ -//#include "p24FJ256GB108.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 242 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256GB110__ -//#include "p24FJ256GB110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 16384 -#define DEVICEID 243 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ32GB002__ -//#include "p24FJ32GB002.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x5800 -#define RAM_START 0x800 -#define RAM_SIZEB 8192 -#define DEVICEID 244 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ32GB004__ -//#include "p24FJ32GB004.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x5800 -#define RAM_START 0x800 -#define RAM_SIZEB 8192 -#define DEVICEID 245 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ64GB002__ -//#include "p24FJ64GB002.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0xAC00 -#define RAM_START 0x800 -#define RAM_SIZEB 8192 -#define DEVICEID 246 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ64GB004__ -//#include "p24FJ64GB004.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0xAC00 -#define RAM_START 0x800 -#define RAM_SIZEB 8192 -#define DEVICEID 247 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128DA106__ -//#include "p24FJ128DA106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 24576 -#define DEVICEID 250 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128DA110__ -//#include "p24FJ128DA110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 24576 -#define DEVICEID 251 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128DA206__ -//#include "p24FJ128DA206.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 252 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ128DA210__ -//#include "p24FJ128DA210.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x15800 -#define RAM_START 0x800 -#define RAM_SIZEB 98304 -#define DEVICEID 253 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256DA106__ -//#include "p24FJ256DA106.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 24576 -#define DEVICEID 254 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif -#ifdef __PIC24FJ256DA110__ -//#include "p24FJ256DA110.inc" -#define IS_24FJ 1 -#define VALID_DEV 1 -#define FLASHSIZE 0x2AC00 -#define RAM_START 0x800 -#define RAM_SIZEB 24576 -#define DEVICEID 255 -#define EESIZEB 0 -#define HAS_UART2 1 -#define HAS_UART3 1 -#define HAS_UART4 1 -#define HAS_PPS 1 -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif - -#ifndef USE_SECURE -#define BLPLP 2 ;bootloader placement pages from end -#define BLSIZEP 1 ;bootloader size [pages] used by bootloader protection -#else -#define BLPLP 2 ;bootloader placement pages from end -#define BLSIZEP 2 ;bootloader size [pages] used by bootloader protection -#define BLSTARTADDR 0x400L -#define BLENDADDR 0x23FFL -#endif +// ;----------------------------------------------------------------------------- +// ; This file is part of ds30 Loader. +// ; +// ; ds30 Loader 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. +// ; +// ; ds30 Loader 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 ds30 Loader. If not see . +// ;------------------------------------------------------------------------------ + +#include "boot_config.h" +#define PAGESIZER 8 +#define ROWSIZEW 64 +#ifdef __PIC24FJ256GB106__ +//#include "p24FJ256GB106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 241 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x1FFFL +#endif +#ifdef __PIC24FJ128GB206__ +//#include "p24FJ128GB206.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 9 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128GB210__ +//#include "p24FJ128GB210.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 17 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256GB206__ +//#include "p24FJ256GB206.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 18 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256GB210__ +//#include "p24FJ256GB210.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 19 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256DA206__ +//#include "p24FJ256DA206.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 191 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256DA210__ +//#include "p24FJ256DA210.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 192 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif + +#ifdef __PIC24FJ64GB106__ +//#include "p24FJ64GB106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0xAC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 217 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ64GB108__ +//#include "p24FJ64GB108.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0xAC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 218 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ64GB110__ +//#include "p24FJ64GB110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0xAC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 219 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128GB106__ +//#include "p24FJ128GB106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 229 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128GB108__ +//#include "p24FJ128GB108.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 230 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128GB110__ +//#include "p24FJ128GB110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 231 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ192GB106__ +//#include "p24FJ192GB106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x20C00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 235 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ192GB108__ +//#include "p24FJ192GB108.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x20C00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 236 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ192GB110__ +//#include "p24FJ192GB110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x20C00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 237 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256GB108__ +//#include "p24FJ256GB108.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 242 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256GB110__ +//#include "p24FJ256GB110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 16384 +#define DEVICEID 243 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ32GB002__ +//#include "p24FJ32GB002.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x5800 +#define RAM_START 0x800 +#define RAM_SIZEB 8192 +#define DEVICEID 244 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ32GB004__ +//#include "p24FJ32GB004.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x5800 +#define RAM_START 0x800 +#define RAM_SIZEB 8192 +#define DEVICEID 245 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ64GB002__ +//#include "p24FJ64GB002.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0xAC00 +#define RAM_START 0x800 +#define RAM_SIZEB 8192 +#define DEVICEID 246 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ64GB004__ +//#include "p24FJ64GB004.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0xAC00 +#define RAM_START 0x800 +#define RAM_SIZEB 8192 +#define DEVICEID 247 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128DA106__ +//#include "p24FJ128DA106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 24576 +#define DEVICEID 250 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128DA110__ +//#include "p24FJ128DA110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 24576 +#define DEVICEID 251 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128DA206__ +//#include "p24FJ128DA206.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 252 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ128DA210__ +//#include "p24FJ128DA210.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x15800 +#define RAM_START 0x800 +#define RAM_SIZEB 98304 +#define DEVICEID 253 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256DA106__ +//#include "p24FJ256DA106.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 24576 +#define DEVICEID 254 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif +#ifdef __PIC24FJ256DA110__ +//#include "p24FJ256DA110.inc" +#define IS_24FJ 1 +#define VALID_DEV 1 +#define FLASHSIZE 0x2AC00 +#define RAM_START 0x800 +#define RAM_SIZEB 24576 +#define DEVICEID 255 +#define EESIZEB 0 +#define HAS_UART2 1 +#define HAS_UART3 1 +#define HAS_UART4 1 +#define HAS_PPS 1 +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif + +#ifndef USE_SECURE +#define BLPLP 2 ;bootloader placement pages from end +#define BLSIZEP 1 ;bootloader size [pages] used by bootloader protection +#else +#define BLPLP 2 ;bootloader placement pages from end +#define BLSIZEP 2 ;bootloader size [pages] used by bootloader protection +#define BLSTARTADDR 0x400L +#define BLENDADDR 0x23FFL +#endif diff --git a/BPv4-bootloader/firmware-v1/cdc.c b/BPv4-bootloader/firmware-v1/cdc.c index 9024b63c..785d3ad1 100644 --- a/BPv4-bootloader/firmware-v1/cdc.c +++ b/BPv4-bootloader/firmware-v1/cdc.c @@ -1,544 +1,544 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - */ - -// JTR V0.1a - -// JTR added - - -#include "globals.h" -#include -BYTE FAST_usb_handler(void); // JTR2 added fast USB service. Pops non EP0 transfer flags from FIFO. Sevices RESET and SETUP - -enum stopbits { - one = 0, oneandahalf = 1, two = 2 -}; - -enum parity { - none = 0, odd = 1, even = 2, mark = 3, space = 4 -}; -const char parity_str[] = {'N', 'O', 'E', 'M', 'S'}; - -struct cdc_LineCodeing { - unsigned long int dwDTERate; - enum stopbits bCharFormat; - enum parity bParityType; - unsigned char bDataBits; -} linecodeing; - -struct _cdc_ControlLineState { - int DTR : 1; - int RTS : 1; - int unused1 : 6; - BYTE unused2; -}; -struct _cdc_ControlLineState cls; -#pragma udata -BYTE cdc_In_len; // total cdc In length -BYTE cdc_Out_len; // total cdc out length -BYTE IsInBufferA; -BYTE IsOutBufferA; -BYTE *InPtr; -BYTE *OutPtr; -BYTE CDC_In_count; -BYTE LineStateUpdated = 0; -BYTE CDCFunctionError = 0; -unsigned char *data, *data_end; -BDentry *Outbdp, *Inbdp; -//volatile unsigned char CDC_trf_state; // JTR don't see that it is really volatile in current context may be in future. - -#pragma udata usb_data -//unsigned char cdc _acm _out _buffer[CDC_BUFFER_SIZE]; //JTR removed and trashed. not required with CDC - ACM. -unsigned char cdc_Out_buffer[CDC_BUFFER_SIZE]; -unsigned char cdc_In_buffer[CDC_BUFFER_SIZE]; -/* -BYTE cdc_In_bufferA[64]; -BYTE cdc_In_bufferB[64]; -BYTE cdc_Out_bufferA[64]; -BYTE cdc_Out_bufferB[64]; - */ - -unsigned char cdc_acm_in_buffer[CDC_NOTICE_BUFFER_SIZE]; //JTR NEWLY defined NOTICE BUFFER SIZE and increased from 8 to 10 bytes in usb_config.h - -#pragma udata - -static struct _bootloaderusbbuffer { - BYTE inBuf[CDC_BUFFER_SIZE]; - unsigned char cnt; - unsigned char rdptr; -} usbbuf; - -unsigned int SOFCOUNT; - -void initCDC(void) { - linecodeing.dwDTERate = 115200; - linecodeing.bCharFormat = one; - linecodeing.bParityType = none; - linecodeing.bDataBits = 8; - cls.DTR = 0; - cls.RTS = 0; - usb_register_class_setup_handler(cdc_setup); - data = data_end = 0; -} - -void user_configured_init(void) { - // JTR NEW FUNCTION - // After the device is enumerated and configured then we set up non EP0 endpoints. - // We only enable the endpoints we are using, not all of them. - // Prior to this they are held in a disarmed state. - - // This function belongs to the current USB function and IS NOT generic. This is CLASS specific - // and will vary from implementation to implementation. - - usb_unset_in_handler(1); - usb_unset_in_handler(2); - usb_unset_out_handler(2); // JTR Macro has bug fix - - // JTR remove all USB_UEP[x] indexing from stack due to pointer bug (fixed?). - // JTR experiment is the UEPx pointer system working? Lets try! - // PASSED on PIC24 - - //USB_UEP[1] = USB_EP_IN; - //USB_UEP[2] = USB_EP_INOUT; - - USB_UEP1 = USB_EP_IN; - USB_UEP2 = USB_EP_INOUT; - - /* Configure buffer descriptors */ -#if USB_PP_BUF_MODE == 0 - // JTR Setup CDC LINE_NOTICE EP (Interrupt IN) - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_acm_in_buffer; - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 - - usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = CDC_BUFFER_SIZE; // JTR N/A endpoints[i].buffer_size; - usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = cdc_Out_buffer; - usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; - - usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_In_buffer; - usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 - -#else - // TODO: Implement Ping-Pong buffering setup. -#error "PP Mode not implemented yet" -#endif - usb_register_class_setup_handler(cdc_setup); - Outbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)]; - Inbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)]; -} - -void cdc_setup(void) { - vLedToggle(); - unsigned char *packet; - size_t reply_len; - packet = bdp->BDADDR; - switch (packet[USB_bmRequestType] & (USB_bmRequestType_TypeMask | USB_bmRequestType_RecipientMask)) { - case (USB_bmRequestType_Class | USB_bmRequestType_Interface): - switch (packet[USB_bRequest]) { - - //JTR This is just a dummy, nothing defined to do for CDC ACM - case CDC_SEND_ENCAPSULATED_COMMAND: // - usb_ack_dat1(rbdp, 0); - break; - - //JTR This is just a dummy, nothing defined to do for CDC ACM - case CDC_GET_ENCAPSULATED_RESPONSE: - usb_ack_dat1(rbdp, 0); - break; - - case CDC_SET_COMM_FEATURE: // Optional - case CDC_GET_COMM_FEATURE: // Optional - case CDC_CLEAR_COMM_FEATURE: // Optional - usb_RequestError(); // Not advertised in ACM functional descriptor - break; - - case CDC_SET_LINE_CODING: // Optional, strongly recomended - usb_set_out_handler(0, cdc_set_line_coding_data); // Register out handler function - break; - - case CDC_GET_LINE_CODING: // Optional, strongly recomended - reply_len = *((unsigned int *) &packet[USB_wLength]); - if (sizeof (struct cdc_LineCodeing) < reply_len) { - reply_len = sizeof (struct cdc_LineCodeing); - } - memcpy(rbdp->BDADDR, (const void *) &linecodeing, reply_len); - usb_ack_dat1(rbdp, reply_len); // JTR common addition for STD and CLASS ACK - //usb_set_in_handler(0, cdc_get_line_coding); // JTR why bother? There is nothing more to do. - break; - - case CDC_SET_CONTROL_LINE_STATE: // Optional - cls = *((struct _cdc_ControlLineState *) &packet[USB_wValue]); - usb_set_in_handler(0, cdc_set_control_line_state_status); // JTR why bother? - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - break; - - case CDC_SEND_BREAK: // Optional - default: - usb_RequestError(); - } - break; - default: - usb_RequestError(); - } -} - -void cdc_get_line_coding(void) { - usb_unset_in_handler(0); // Unregister IN handler; -} - -void cdc_set_line_coding_data(void) { // JTR handling an OUT token In the CDC stack this is the only function that handles an OUT data stage. - memcpy(&linecodeing, (const void *) bdp->BDADDR, sizeof (struct cdc_LineCodeing)); - usb_unset_out_handler(0); // Unregister OUT handler; JTR serious bug fix in macro! - usb_set_in_handler(0, cdc_set_line_coding_status); // JTR why bother? - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - - // JTR This part of the USB-CDC stack is worth highlighting - // This is the only place that we have an OUT DATA packet on - // EP0. At this point it has been completed. This stack unlike - // the microchip stack does not have a common IN or OUT data - // packet complete tail and therefore it is the responsibility - // of each section to ensure that EP0 is set-up correctly for - // the next setup packet. - - // This next line inverts the DTS so that it is now ready for - // a DAT0 packet. However it only works because we had one data - // packet. For any amount of EVEN data packets it would not be - // correct. - // usb_ack_out(bdp); // JTR N/A Good for only odd number of data packets. - - // The correct thing to do is to force EP0 OUT to the DAT0 state - // after we have all our data packets. - bdp->BDCNT = USB_EP0_BUFFER_SIZE; - bdp->BDSTAT = UOWN | DTSEN; -} - -void cdc_set_line_coding_status(void) { - usb_unset_in_handler(0); -} - -void cdc_set_control_line_state_status(void) { - usb_unset_in_handler(0); -} - -// JTR N/A. There is no LINE_NOTIFICATION_OUT in CDC-ACM -//void cdc _acm _out( void ) { -//} - -// JTR keep this fragment as it may be useful later if reworked a little -//void cdc_acm_in( void ) { - -// JTR LINE_NOTIFICATION EP has been increased to ten bytes. -// This is because if it were ever to be used it is more likely -// that it will be for the Serial State Notification which has -// two bytes of data to include in the packet. - -// Also we will not actually come to this code if the LINE_NOTIFICATION -// endpoint is not already armed and we do not arm this end point -// until we have something to send. Therefore we have a chicken and egg -// deadlock and this function is of no value the way it is currently coded. -// There is no IN token if the endpoint is not armed! -// No IN handler is required nor a state machine as there is no need for a ZLP - -/* -if (0) { // Response Available Notification - // Not TODO: Probably never implement this, we're not a modem. - // Is this correct placement of the response notification? - bdp->BDADDR[USB_bmRequestType] = USB_bmRequestType_D2H | USB_bmRequestType_Class | USB_bmRequestType_Interface; - bdp->BDADDR[USB_bRequest] = CDC_RESPONSE_AVAILABLE; - bdp->BDADDR[USB_wValue] = 0; - bdp->BDADDR[USB_wValueHigh] = 0; - bdp->BDADDR[USB_wIndex] = 0; - bdp->BDADDR[USB_wIndexHigh] = 0; - bdp->BDADDR[USB_wLength] = 0; - bdp->BDADDR[USB_wLengthHigh] = 0; - // JTR past below bdp->BDCNT = 8; - usb_ack_dat1(bdp, 8); -} else if (0) { // Network Connection Notification -} else if (0) { // Serial State Notification -} -} */ - -/* END OF CDC CLASS REQUESTS HANDLING - - Below are the CDC USBUART functions */ - -void sof_counter_handler() { - if (SOFCOUNT == 0) return; - SOFCOUNT--; -} - -BYTE WaitOutReady() // JTR2 added reduced overhead -{ - while ((Outbdp->BDSTAT & UOWN)) { - if (!TestUsbInterruptEnabled()) { - if (0 != FAST_usb_handler()) // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. - return 0; - } - } - return 1; -}//end WaitOutReady - -/**********************************************************************************/ - -/********************************************************************************** - Function: - BYTE WaitInReady() - - // JTR2 addition - Summary: - As this CDC stack writes directly into USB RAM this function checks for - for its availability. - It is a semi-blocking function. It waits for the cdc In Buffer to - become available before returning. However if the USB interrupt is - not enabled it call the fast USB housekeeper to ensure that the USB - stack is always serviced in the meantime. If in polling mode and a - setup packet or bus reset happens it returns FALSE (00) to alert the calling - function that the transfer may have been interfered with. - - *****************************************************************************/ - -/*****************************************************************************/ -BYTE WaitInReady() // JTR2 added reduced overhead -{ - while ((Inbdp->BDSTAT & UOWN)) { - if (!TestUsbInterruptEnabled()) { - if (0 != FAST_usb_handler()) // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. - return 0; - } - } - return 1; -}//end WaitInReady - -/********************************************************************************** - Function: - BYTE putPARTARRAYUSBUSART() - - // JTR2 addition - Summary: - Fast, low overhead CDC IN packet does not track or send ZLP. - It is a semi-blocking function. It waits for the cdc In Buffer to - become available before returning. However if the USB interrupt is - not enabled it call the fast USB housekeeper to ensure that the USB - stack is always serviced in the meantime. If in polling mode and a - setup packet or bus reset happens it returns 0xFF to alert the calling - function that the transfer may have been interfered with. - - Note that the data must reside in USB RAM. - - *****************************************************************************/ -/*****************************************************************************/ -// JTR2 Does NOT send ZLP after each packet only one at the end is required. - -BYTE putPARTARRAYUSBUSART(BYTE *data, BYTE length) // JTR2 added reduced overhead -{ - while ((Inbdp->BDSTAT & UOWN)) { - if (!TestUsbInterruptEnabled()) { - - if (0 != FAST_usb_handler()) { // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. - return 0xff; - } - } - } - Inbdp->BDADDR = data; - Inbdp->BDCNT = length; - Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - FAST_usb_handler(); - return length; -}//end putUSBUSARTFAST - -/********************************************************************************** - Function: - BYTE putFULLARRAYUSBUSART() - - // JTR2 addition - Summary: - Fast, low overhead CDC In packet does not track or send ZLP. - It is a non-blocking function. It does not wait for the cdc In Buffer to - become available. Instead it returns '0' to notify the caller that - data has not been sent. Otherwise it return the endpoint size. - - *****************************************************************************/ -/*****************************************************************************/ -/* -BYTE putFULLARRAYUSBUSART() // JTR2 added reduced overhead -{ - if ((Inbdp->BDSTAT & UOWN)) { - return 0; - } else { - Inbdp->BDCNT = CDC_BUFFER_SIZE; - Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - } - return CDC_BUFFER_SIZE; -}//end putUSBUSARTFAST - */ - -/**********************************************************************************/ -/* -BYTE getCDC_Out_ArmNext(void) { - - CDCFunctionError = 0; - - - //first, get 64 bytes OR to FF FF - if (WaitOutReady()) { - - if (IsOutBufferA) { - OutPtr = &cdc_Out_bufferA[0]; - Outbdp->BDADDR = &cdc_Out_bufferB[0]; - } else { - OutPtr = &cdc_Out_bufferB[0]; - Outbdp->BDADDR = &cdc_Out_bufferA[0]; - } - IsOutBufferA ^= 0xFF; - cdc_Out_len = Outbdp->BDCNT; - Outbdp->BDCNT = CDC_BUFFER_SIZE; - Outbdp->BDSTAT = ((Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - - } else { - cdc_Out_len = 0; - CDCFunctionError = 1; - } - return cdc_Out_len; -}//end getCDC_Out_ArmNext - -BYTE SendCDC_In_ArmNext(BYTE count) { - - CDCFunctionError = 0; - if (WaitInReady()) { - - if (IsInBufferA) { - Inbdp->BDADDR = cdc_In_bufferA; - InPtr = cdc_In_bufferB; - } else { - Inbdp->BDADDR = cdc_In_bufferB; - InPtr = cdc_In_bufferA; - } - Inbdp->BDCNT = CDC_In_count; - Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - IsInBufferA ^= 0xFF; - CDC_In_count = 0; - } else { - CDCFunctionError = 1; - } - return CDCFunctionError; -} - -void ArmCDCOutDB(void) { - Outbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim out buffer - Outbdp->BDADDR = &cdc_Out_bufferA[0]; - Outbdp->BDCNT = CDC_BUFFER_SIZE; - Outbdp->BDSTAT |= UOWN; - IsOutBufferA = 0xFF; -} - -void ArmCDCInDB(void) { - WaitInReady(); - //Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer - Inbdp->BDADDR = &cdc_In_bufferA[0]; - Inbdp->BDCNT = CDC_BUFFER_SIZE; - //Inbdp->BDSTAT |= UOWN; - IsInBufferA = 0xFF; - InPtr = cdc_In_bufferA; - CDC_In_count = 0; -} - -void DisArmCDCOutDB(void) { - Outbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim out buffer - Outbdp->BDADDR = &cdc_Out_buffer[0]; - Outbdp->BDCNT = CDC_BUFFER_SIZE; - Outbdp->BDSTAT |= UOWN; -} - -void DisArmCDCInDB(void) { - WaitInReady(); - Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer - Inbdp->BDADDR = &cdc_In_buffer[0]; - Inbdp->BDCNT = CDC_BUFFER_SIZE; - Inbdp->BDSTAT |= UOWN; -} - -void SendZLP(void) { - Inbdp->BDCNT = 0; - Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - -} - */ - -/**********************************************************************************/ -BYTE getOutReady(void) { - - return !(Outbdp->BDSTAT & UOWN); // Do we have a packet from host? -} - -/**********************************************************************************/ -BYTE getInReady(void) { - - return !(Inbdp->BDSTAT & UOWN); // Is the CDC In buffer ready? -} - -/************************************************************************/ - -BYTE getsUSBUSART(BYTE *buffer, BYTE len) { - cdc_Out_len = 0; - if (0 == (Outbdp->BDSTAT & UOWN)) // Do we have a packet from host? - { - // Adjust the expected number of BYTEs to equal - // the actual number of BYTEs received. - - if (len > Outbdp->BDCNT) - len = Outbdp->BDCNT; - - // Copy data from dual-ram buffer to user's buffer - - for (cdc_Out_len = 0; cdc_Out_len < len; cdc_Out_len++) - buffer[cdc_Out_len] = cdc_Out_buffer[cdc_Out_len]; - - // Prepare dual-ram buffer for next OUT transaction - - if (!TestUsbInterruptEnabled()) { - FAST_usb_handler(); // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. - } - Outbdp->BDCNT = CDC_BUFFER_SIZE; - Outbdp->BDSTAT = ((Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - }//end if - - return cdc_Out_len; - -}//end getsUSBUSART - -void usbbufservice(void) { - - if (usbbuf.cnt == 0) {//if the buffer is empty, get more data - usbbuf.cnt = getsUSBUSART(usbbuf.inBuf, CDC_BUFFER_SIZE); //JTR2 - usb_handler(); - usbbuf.rdptr = 0; - } -} - -//puts a byte from the buffer in the byte, returns 1 if byte - -unsigned char usbbufgetbyte(unsigned char* c) { - do { - usbbufservice(); - } while (usbbuf.cnt == 0); - *c = usbbuf.inBuf[usbbuf.rdptr]; - usbbuf.cnt--; - usbbuf.rdptr++; - return 1; -} - - - - - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + */ + +// JTR V0.1a + +// JTR added + + +#include "globals.h" +#include +BYTE FAST_usb_handler(void); // JTR2 added fast USB service. Pops non EP0 transfer flags from FIFO. Sevices RESET and SETUP + +enum stopbits { + one = 0, oneandahalf = 1, two = 2 +}; + +enum parity { + none = 0, odd = 1, even = 2, mark = 3, space = 4 +}; +const char parity_str[] = {'N', 'O', 'E', 'M', 'S'}; + +struct cdc_LineCodeing { + unsigned long int dwDTERate; + enum stopbits bCharFormat; + enum parity bParityType; + unsigned char bDataBits; +} linecodeing; + +struct _cdc_ControlLineState { + int DTR : 1; + int RTS : 1; + int unused1 : 6; + BYTE unused2; +}; +struct _cdc_ControlLineState cls; +#pragma udata +BYTE cdc_In_len; // total cdc In length +BYTE cdc_Out_len; // total cdc out length +BYTE IsInBufferA; +BYTE IsOutBufferA; +BYTE *InPtr; +BYTE *OutPtr; +BYTE CDC_In_count; +BYTE LineStateUpdated = 0; +BYTE CDCFunctionError = 0; +unsigned char *data, *data_end; +BDentry *Outbdp, *Inbdp; +//volatile unsigned char CDC_trf_state; // JTR don't see that it is really volatile in current context may be in future. + +#pragma udata usb_data +//unsigned char cdc _acm _out _buffer[CDC_BUFFER_SIZE]; //JTR removed and trashed. not required with CDC - ACM. +unsigned char cdc_Out_buffer[CDC_BUFFER_SIZE]; +unsigned char cdc_In_buffer[CDC_BUFFER_SIZE]; +/* +BYTE cdc_In_bufferA[64]; +BYTE cdc_In_bufferB[64]; +BYTE cdc_Out_bufferA[64]; +BYTE cdc_Out_bufferB[64]; + */ + +unsigned char cdc_acm_in_buffer[CDC_NOTICE_BUFFER_SIZE]; //JTR NEWLY defined NOTICE BUFFER SIZE and increased from 8 to 10 bytes in usb_config.h + +#pragma udata + +static struct _bootloaderusbbuffer { + BYTE inBuf[CDC_BUFFER_SIZE]; + unsigned char cnt; + unsigned char rdptr; +} usbbuf; + +unsigned int SOFCOUNT; + +void initCDC(void) { + linecodeing.dwDTERate = 115200; + linecodeing.bCharFormat = one; + linecodeing.bParityType = none; + linecodeing.bDataBits = 8; + cls.DTR = 0; + cls.RTS = 0; + usb_register_class_setup_handler(cdc_setup); + data = data_end = 0; +} + +void user_configured_init(void) { + // JTR NEW FUNCTION + // After the device is enumerated and configured then we set up non EP0 endpoints. + // We only enable the endpoints we are using, not all of them. + // Prior to this they are held in a disarmed state. + + // This function belongs to the current USB function and IS NOT generic. This is CLASS specific + // and will vary from implementation to implementation. + + usb_unset_in_handler(1); + usb_unset_in_handler(2); + usb_unset_out_handler(2); // JTR Macro has bug fix + + // JTR remove all USB_UEP[x] indexing from stack due to pointer bug (fixed?). + // JTR experiment is the UEPx pointer system working? Lets try! + // PASSED on PIC24 + + //USB_UEP[1] = USB_EP_IN; + //USB_UEP[2] = USB_EP_INOUT; + + USB_UEP1 = USB_EP_IN; + USB_UEP2 = USB_EP_INOUT; + + /* Configure buffer descriptors */ +#if USB_PP_BUF_MODE == 0 + // JTR Setup CDC LINE_NOTICE EP (Interrupt IN) + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_acm_in_buffer; + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 + + usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = CDC_BUFFER_SIZE; // JTR N/A endpoints[i].buffer_size; + usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = cdc_Out_buffer; + usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; + + usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_In_buffer; + usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 + +#else + // TODO: Implement Ping-Pong buffering setup. +#error "PP Mode not implemented yet" +#endif + usb_register_class_setup_handler(cdc_setup); + Outbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)]; + Inbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)]; +} + +void cdc_setup(void) { + vLedToggle(); + unsigned char *packet; + size_t reply_len; + packet = bdp->BDADDR; + switch (packet[USB_bmRequestType] & (USB_bmRequestType_TypeMask | USB_bmRequestType_RecipientMask)) { + case (USB_bmRequestType_Class | USB_bmRequestType_Interface): + switch (packet[USB_bRequest]) { + + //JTR This is just a dummy, nothing defined to do for CDC ACM + case CDC_SEND_ENCAPSULATED_COMMAND: // + usb_ack_dat1(rbdp, 0); + break; + + //JTR This is just a dummy, nothing defined to do for CDC ACM + case CDC_GET_ENCAPSULATED_RESPONSE: + usb_ack_dat1(rbdp, 0); + break; + + case CDC_SET_COMM_FEATURE: // Optional + case CDC_GET_COMM_FEATURE: // Optional + case CDC_CLEAR_COMM_FEATURE: // Optional + usb_RequestError(); // Not advertised in ACM functional descriptor + break; + + case CDC_SET_LINE_CODING: // Optional, strongly recomended + usb_set_out_handler(0, cdc_set_line_coding_data); // Register out handler function + break; + + case CDC_GET_LINE_CODING: // Optional, strongly recomended + reply_len = *((unsigned int *) &packet[USB_wLength]); + if (sizeof (struct cdc_LineCodeing) < reply_len) { + reply_len = sizeof (struct cdc_LineCodeing); + } + memcpy(rbdp->BDADDR, (const void *) &linecodeing, reply_len); + usb_ack_dat1(rbdp, reply_len); // JTR common addition for STD and CLASS ACK + //usb_set_in_handler(0, cdc_get_line_coding); // JTR why bother? There is nothing more to do. + break; + + case CDC_SET_CONTROL_LINE_STATE: // Optional + cls = *((struct _cdc_ControlLineState *) &packet[USB_wValue]); + usb_set_in_handler(0, cdc_set_control_line_state_status); // JTR why bother? + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + break; + + case CDC_SEND_BREAK: // Optional + default: + usb_RequestError(); + } + break; + default: + usb_RequestError(); + } +} + +void cdc_get_line_coding(void) { + usb_unset_in_handler(0); // Unregister IN handler; +} + +void cdc_set_line_coding_data(void) { // JTR handling an OUT token In the CDC stack this is the only function that handles an OUT data stage. + memcpy(&linecodeing, (const void *) bdp->BDADDR, sizeof (struct cdc_LineCodeing)); + usb_unset_out_handler(0); // Unregister OUT handler; JTR serious bug fix in macro! + usb_set_in_handler(0, cdc_set_line_coding_status); // JTR why bother? + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + + // JTR This part of the USB-CDC stack is worth highlighting + // This is the only place that we have an OUT DATA packet on + // EP0. At this point it has been completed. This stack unlike + // the microchip stack does not have a common IN or OUT data + // packet complete tail and therefore it is the responsibility + // of each section to ensure that EP0 is set-up correctly for + // the next setup packet. + + // This next line inverts the DTS so that it is now ready for + // a DAT0 packet. However it only works because we had one data + // packet. For any amount of EVEN data packets it would not be + // correct. + // usb_ack_out(bdp); // JTR N/A Good for only odd number of data packets. + + // The correct thing to do is to force EP0 OUT to the DAT0 state + // after we have all our data packets. + bdp->BDCNT = USB_EP0_BUFFER_SIZE; + bdp->BDSTAT = UOWN | DTSEN; +} + +void cdc_set_line_coding_status(void) { + usb_unset_in_handler(0); +} + +void cdc_set_control_line_state_status(void) { + usb_unset_in_handler(0); +} + +// JTR N/A. There is no LINE_NOTIFICATION_OUT in CDC-ACM +//void cdc _acm _out( void ) { +//} + +// JTR keep this fragment as it may be useful later if reworked a little +//void cdc_acm_in( void ) { + +// JTR LINE_NOTIFICATION EP has been increased to ten bytes. +// This is because if it were ever to be used it is more likely +// that it will be for the Serial State Notification which has +// two bytes of data to include in the packet. + +// Also we will not actually come to this code if the LINE_NOTIFICATION +// endpoint is not already armed and we do not arm this end point +// until we have something to send. Therefore we have a chicken and egg +// deadlock and this function is of no value the way it is currently coded. +// There is no IN token if the endpoint is not armed! +// No IN handler is required nor a state machine as there is no need for a ZLP + +/* +if (0) { // Response Available Notification + // Not TODO: Probably never implement this, we're not a modem. + // Is this correct placement of the response notification? + bdp->BDADDR[USB_bmRequestType] = USB_bmRequestType_D2H | USB_bmRequestType_Class | USB_bmRequestType_Interface; + bdp->BDADDR[USB_bRequest] = CDC_RESPONSE_AVAILABLE; + bdp->BDADDR[USB_wValue] = 0; + bdp->BDADDR[USB_wValueHigh] = 0; + bdp->BDADDR[USB_wIndex] = 0; + bdp->BDADDR[USB_wIndexHigh] = 0; + bdp->BDADDR[USB_wLength] = 0; + bdp->BDADDR[USB_wLengthHigh] = 0; + // JTR past below bdp->BDCNT = 8; + usb_ack_dat1(bdp, 8); +} else if (0) { // Network Connection Notification +} else if (0) { // Serial State Notification +} +} */ + +/* END OF CDC CLASS REQUESTS HANDLING + + Below are the CDC USBUART functions */ + +void sof_counter_handler() { + if (SOFCOUNT == 0) return; + SOFCOUNT--; +} + +BYTE WaitOutReady() // JTR2 added reduced overhead +{ + while ((Outbdp->BDSTAT & UOWN)) { + if (!TestUsbInterruptEnabled()) { + if (0 != FAST_usb_handler()) // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. + return 0; + } + } + return 1; +}//end WaitOutReady + +/**********************************************************************************/ + +/********************************************************************************** + Function: + BYTE WaitInReady() + + // JTR2 addition + Summary: + As this CDC stack writes directly into USB RAM this function checks for + for its availability. + It is a semi-blocking function. It waits for the cdc In Buffer to + become available before returning. However if the USB interrupt is + not enabled it call the fast USB housekeeper to ensure that the USB + stack is always serviced in the meantime. If in polling mode and a + setup packet or bus reset happens it returns FALSE (00) to alert the calling + function that the transfer may have been interfered with. + + *****************************************************************************/ + +/*****************************************************************************/ +BYTE WaitInReady() // JTR2 added reduced overhead +{ + while ((Inbdp->BDSTAT & UOWN)) { + if (!TestUsbInterruptEnabled()) { + if (0 != FAST_usb_handler()) // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. + return 0; + } + } + return 1; +}//end WaitInReady + +/********************************************************************************** + Function: + BYTE putPARTARRAYUSBUSART() + + // JTR2 addition + Summary: + Fast, low overhead CDC IN packet does not track or send ZLP. + It is a semi-blocking function. It waits for the cdc In Buffer to + become available before returning. However if the USB interrupt is + not enabled it call the fast USB housekeeper to ensure that the USB + stack is always serviced in the meantime. If in polling mode and a + setup packet or bus reset happens it returns 0xFF to alert the calling + function that the transfer may have been interfered with. + + Note that the data must reside in USB RAM. + + *****************************************************************************/ +/*****************************************************************************/ +// JTR2 Does NOT send ZLP after each packet only one at the end is required. + +BYTE putPARTARRAYUSBUSART(BYTE *data, BYTE length) // JTR2 added reduced overhead +{ + while ((Inbdp->BDSTAT & UOWN)) { + if (!TestUsbInterruptEnabled()) { + + if (0 != FAST_usb_handler()) { // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. + return 0xff; + } + } + } + Inbdp->BDADDR = data; + Inbdp->BDCNT = length; + Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + FAST_usb_handler(); + return length; +}//end putUSBUSARTFAST + +/********************************************************************************** + Function: + BYTE putFULLARRAYUSBUSART() + + // JTR2 addition + Summary: + Fast, low overhead CDC In packet does not track or send ZLP. + It is a non-blocking function. It does not wait for the cdc In Buffer to + become available. Instead it returns '0' to notify the caller that + data has not been sent. Otherwise it return the endpoint size. + + *****************************************************************************/ +/*****************************************************************************/ +/* +BYTE putFULLARRAYUSBUSART() // JTR2 added reduced overhead +{ + if ((Inbdp->BDSTAT & UOWN)) { + return 0; + } else { + Inbdp->BDCNT = CDC_BUFFER_SIZE; + Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + } + return CDC_BUFFER_SIZE; +}//end putUSBUSARTFAST + */ + +/**********************************************************************************/ +/* +BYTE getCDC_Out_ArmNext(void) { + + CDCFunctionError = 0; + + + //first, get 64 bytes OR to FF FF + if (WaitOutReady()) { + + if (IsOutBufferA) { + OutPtr = &cdc_Out_bufferA[0]; + Outbdp->BDADDR = &cdc_Out_bufferB[0]; + } else { + OutPtr = &cdc_Out_bufferB[0]; + Outbdp->BDADDR = &cdc_Out_bufferA[0]; + } + IsOutBufferA ^= 0xFF; + cdc_Out_len = Outbdp->BDCNT; + Outbdp->BDCNT = CDC_BUFFER_SIZE; + Outbdp->BDSTAT = ((Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + + } else { + cdc_Out_len = 0; + CDCFunctionError = 1; + } + return cdc_Out_len; +}//end getCDC_Out_ArmNext + +BYTE SendCDC_In_ArmNext(BYTE count) { + + CDCFunctionError = 0; + if (WaitInReady()) { + + if (IsInBufferA) { + Inbdp->BDADDR = cdc_In_bufferA; + InPtr = cdc_In_bufferB; + } else { + Inbdp->BDADDR = cdc_In_bufferB; + InPtr = cdc_In_bufferA; + } + Inbdp->BDCNT = CDC_In_count; + Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + IsInBufferA ^= 0xFF; + CDC_In_count = 0; + } else { + CDCFunctionError = 1; + } + return CDCFunctionError; +} + +void ArmCDCOutDB(void) { + Outbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim out buffer + Outbdp->BDADDR = &cdc_Out_bufferA[0]; + Outbdp->BDCNT = CDC_BUFFER_SIZE; + Outbdp->BDSTAT |= UOWN; + IsOutBufferA = 0xFF; +} + +void ArmCDCInDB(void) { + WaitInReady(); + //Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer + Inbdp->BDADDR = &cdc_In_bufferA[0]; + Inbdp->BDCNT = CDC_BUFFER_SIZE; + //Inbdp->BDSTAT |= UOWN; + IsInBufferA = 0xFF; + InPtr = cdc_In_bufferA; + CDC_In_count = 0; +} + +void DisArmCDCOutDB(void) { + Outbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim out buffer + Outbdp->BDADDR = &cdc_Out_buffer[0]; + Outbdp->BDCNT = CDC_BUFFER_SIZE; + Outbdp->BDSTAT |= UOWN; +} + +void DisArmCDCInDB(void) { + WaitInReady(); + Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer + Inbdp->BDADDR = &cdc_In_buffer[0]; + Inbdp->BDCNT = CDC_BUFFER_SIZE; + Inbdp->BDSTAT |= UOWN; +} + +void SendZLP(void) { + Inbdp->BDCNT = 0; + Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + +} + */ + +/**********************************************************************************/ +BYTE getOutReady(void) { + + return !(Outbdp->BDSTAT & UOWN); // Do we have a packet from host? +} + +/**********************************************************************************/ +BYTE getInReady(void) { + + return !(Inbdp->BDSTAT & UOWN); // Is the CDC In buffer ready? +} + +/************************************************************************/ + +BYTE getsUSBUSART(BYTE *buffer, BYTE len) { + cdc_Out_len = 0; + if (0 == (Outbdp->BDSTAT & UOWN)) // Do we have a packet from host? + { + // Adjust the expected number of BYTEs to equal + // the actual number of BYTEs received. + + if (len > Outbdp->BDCNT) + len = Outbdp->BDCNT; + + // Copy data from dual-ram buffer to user's buffer + + for (cdc_Out_len = 0; cdc_Out_len < len; cdc_Out_len++) + buffer[cdc_Out_len] = cdc_Out_buffer[cdc_Out_len]; + + // Prepare dual-ram buffer for next OUT transaction + + if (!TestUsbInterruptEnabled()) { + FAST_usb_handler(); // JTR2 Pop non-EP0 (USB IN) tranfers from the FIFO and also give SETUP PACKETs a chance. + } + Outbdp->BDCNT = CDC_BUFFER_SIZE; + Outbdp->BDSTAT = ((Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + }//end if + + return cdc_Out_len; + +}//end getsUSBUSART + +void usbbufservice(void) { + + if (usbbuf.cnt == 0) {//if the buffer is empty, get more data + usbbuf.cnt = getsUSBUSART(usbbuf.inBuf, CDC_BUFFER_SIZE); //JTR2 + usb_handler(); + usbbuf.rdptr = 0; + } +} + +//puts a byte from the buffer in the byte, returns 1 if byte + +unsigned char usbbufgetbyte(unsigned char* c) { + do { + usbbufservice(); + } while (usbbuf.cnt == 0); + *c = usbbuf.inBuf[usbbuf.rdptr]; + usbbuf.cnt--; + usbbuf.rdptr++; + return 1; +} + + + + + diff --git a/BPv4-bootloader/firmware-v1/cdc.h b/BPv4-bootloader/firmware-v1/cdc.h index f378b0e9..1555a516 100644 --- a/BPv4-bootloader/firmware-v1/cdc.h +++ b/BPv4-bootloader/firmware-v1/cdc.h @@ -1,115 +1,115 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. -*/ -#ifndef __CDC_H__ -#define __CDC_H__ - -//#include "globals.h" - -void baudCDC(unsigned char baudconfig); -void cdc_setup(void); -void cdc_set_line_coding_data(void); -void cdc_set_line_coding_status(void); -void cdc_get_line_coding(void); -void cdc_set_control_line_state_status(void); -void user_configured_init(void); // JTR added. Sets up CDC endpoints after device configured. -void usb_ack_dat1(BDentry *rbdp, int bdcnt); // JTR added standard for both STD and CLASS -//void usbbufservice(void); -unsigned char usbbufgetbyte(unsigned char *c); -//unsigned char PEEKusbbufgetbyte(unsigned char *c); -void usbbufflush(void); -BYTE getsUSBUSART(BYTE* buffer, BYTE len); -BYTE putPARTARRAYUSBUSART(BYTE *data, BYTE length); -BYTE putFULLARRAYUSBUSART(void); -BYTE WaitInReady(void); -BYTE WaitOutReady(void); -BYTE getInReady(void); -BYTE getOutReady(void); -BYTE getCDC_Out_ArmNext(void); -BYTE SendCDC_In_ArmNext(BYTE count); -void ArmCDCOutDB(void); -void ArmCDCInDB(void); -void DisArmCDCOutDB(void); -void DisArmCDCInDB(void); -void SendZLP(void); -void user_configured_init(void); - - - -#define putUnsignedCharArrayUsbUsart(u8Array,Num) putPARTARRAYUSBUSART((BYTE*)(u8Array),(Num)) -#define getUnsignedCharArrayUsbUart(u8Array,Num) getsUSBUSART((BYTE*)(u8Array),(Num)) - -// CDC Request Codes -#define CDC_SEND_ENCAPSULATED_COMMAND 0x00 -#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 -#define CDC_SET_COMM_FEATURE 0x02 -#define CDC_GET_COMM_FEATURE 0x03 -#define CDC_CLEAR_COMM_FEATURE 0x04 -// RESERVED (future use) 0x05-0x0F -#define CDC_SET_AUX_LINE_STATE 0x10 -#define CDC_SET_HOOK_STATE 0x11 -#define CDC_PULSE_SETUP 0x12 -#define CDC_SEND_PULSE 0x13 -#define CDC_SET_PULSE_TIME 0x14 -#define CDC_RING_AUX_JACK 0x15 -// RESERVED (future use) 0x16-0x1F -#define CDC_SET_LINE_CODING 0x20 -#define CDC_GET_LINE_CODING 0x21 -#define CDC_SET_CONTROL_LINE_STATE 0x22 -#define CDC_SEND_BREAK 0x23 -// RESERVED (future use) 0x24-0x2F -#define CDC_SET_RINGER_PARMS 0x30 -#define CDC_GET_RINGER_PARMS 0x31 -#define CDC_SET_OPERATION_PARMS 0x32 -#define CDC_GET_OPERATION_PARMS 0x33 -#define CDC_SET_LINE_PARMS 0x34 -#define CDC_GET_LINE_PARMS 0x35 -#define CDC_DIAL_DIGITS 0x36 -#define CDC_SET_UNIT_PARAMETER 0x37 -#define CDC_GET_UNIT_PARAMETER 0x38 -#define CDC_CLEAR_UNIT_PARAMETER 0x39 -#define CDC_GET_PROFILE 0x3A -// RESERVED (future use) 0x3B-0x3F -#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 -#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x41 -#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x42 -#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 -#define CDC_GET_ETHERNET_STATISTIC 0x44 -// RESERVED (future use) 0x45-0x4F -#define CDC_SET_ATM_DATA_FORMAT 0x50 -#define CDC_GET_ATM_DEVICE_STATISTICS 0x51 -#define CDC_SET_ATM_DEFAULT_VC 0x52 -#define CDC_GET_ATM_VC_STATISTICS 0x53 -// RESERVED (future use) 0x54-0x5F -// MDLM Semantic-Model specific Requests 0x60–0x7F -// RESERVED (future use) 0x80-0xFF - -// CDC Notification Codes -#define CDC_NETWORK_CONNECTION 0x00 -#define CDC_RESPONSE_AVAILABLE 0x01 -// RESERVED (future use) 0x02-0x07 -#define CDC_AUX_JACK_HOOK_STATE 0x08 -#define CDC_RING_DETECT 0x09 -// RESERVED (future use) 0x0A-0x1F -#define CDC_SERIAL_STATE 0x20 -// RESERVED (future use) 0x21-0x27 -#define CDC_CALL_STATE_CHANGE 0x28 -#define CDC_LINE_STATE_CHANGE 0x29 -#define CDC_CONNECTION_SPEED_CHANGE 0x2A -// RESERVED 0x2B-0x3F -// MDML SEMANTIC-MODEL-SPECIFIC NOTIFICATION 0x40-0x5F -// RESERVED (future use) 0x60-0xFF -#endif - - - - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. +*/ +#ifndef __CDC_H__ +#define __CDC_H__ + +//#include "globals.h" + +void baudCDC(unsigned char baudconfig); +void cdc_setup(void); +void cdc_set_line_coding_data(void); +void cdc_set_line_coding_status(void); +void cdc_get_line_coding(void); +void cdc_set_control_line_state_status(void); +void user_configured_init(void); // JTR added. Sets up CDC endpoints after device configured. +void usb_ack_dat1(BDentry *rbdp, int bdcnt); // JTR added standard for both STD and CLASS +//void usbbufservice(void); +unsigned char usbbufgetbyte(unsigned char *c); +//unsigned char PEEKusbbufgetbyte(unsigned char *c); +void usbbufflush(void); +BYTE getsUSBUSART(BYTE* buffer, BYTE len); +BYTE putPARTARRAYUSBUSART(BYTE *data, BYTE length); +BYTE putFULLARRAYUSBUSART(void); +BYTE WaitInReady(void); +BYTE WaitOutReady(void); +BYTE getInReady(void); +BYTE getOutReady(void); +BYTE getCDC_Out_ArmNext(void); +BYTE SendCDC_In_ArmNext(BYTE count); +void ArmCDCOutDB(void); +void ArmCDCInDB(void); +void DisArmCDCOutDB(void); +void DisArmCDCInDB(void); +void SendZLP(void); +void user_configured_init(void); + + + +#define putUnsignedCharArrayUsbUsart(u8Array,Num) putPARTARRAYUSBUSART((BYTE*)(u8Array),(Num)) +#define getUnsignedCharArrayUsbUart(u8Array,Num) getsUSBUSART((BYTE*)(u8Array),(Num)) + +// CDC Request Codes +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00 +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 +#define CDC_SET_COMM_FEATURE 0x02 +#define CDC_GET_COMM_FEATURE 0x03 +#define CDC_CLEAR_COMM_FEATURE 0x04 +// RESERVED (future use) 0x05-0x0F +#define CDC_SET_AUX_LINE_STATE 0x10 +#define CDC_SET_HOOK_STATE 0x11 +#define CDC_PULSE_SETUP 0x12 +#define CDC_SEND_PULSE 0x13 +#define CDC_SET_PULSE_TIME 0x14 +#define CDC_RING_AUX_JACK 0x15 +// RESERVED (future use) 0x16-0x1F +#define CDC_SET_LINE_CODING 0x20 +#define CDC_GET_LINE_CODING 0x21 +#define CDC_SET_CONTROL_LINE_STATE 0x22 +#define CDC_SEND_BREAK 0x23 +// RESERVED (future use) 0x24-0x2F +#define CDC_SET_RINGER_PARMS 0x30 +#define CDC_GET_RINGER_PARMS 0x31 +#define CDC_SET_OPERATION_PARMS 0x32 +#define CDC_GET_OPERATION_PARMS 0x33 +#define CDC_SET_LINE_PARMS 0x34 +#define CDC_GET_LINE_PARMS 0x35 +#define CDC_DIAL_DIGITS 0x36 +#define CDC_SET_UNIT_PARAMETER 0x37 +#define CDC_GET_UNIT_PARAMETER 0x38 +#define CDC_CLEAR_UNIT_PARAMETER 0x39 +#define CDC_GET_PROFILE 0x3A +// RESERVED (future use) 0x3B-0x3F +#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 +#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x41 +#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x42 +#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 +#define CDC_GET_ETHERNET_STATISTIC 0x44 +// RESERVED (future use) 0x45-0x4F +#define CDC_SET_ATM_DATA_FORMAT 0x50 +#define CDC_GET_ATM_DEVICE_STATISTICS 0x51 +#define CDC_SET_ATM_DEFAULT_VC 0x52 +#define CDC_GET_ATM_VC_STATISTICS 0x53 +// RESERVED (future use) 0x54-0x5F +// MDLM Semantic-Model specific Requests 0x60–0x7F +// RESERVED (future use) 0x80-0xFF + +// CDC Notification Codes +#define CDC_NETWORK_CONNECTION 0x00 +#define CDC_RESPONSE_AVAILABLE 0x01 +// RESERVED (future use) 0x02-0x07 +#define CDC_AUX_JACK_HOOK_STATE 0x08 +#define CDC_RING_DETECT 0x09 +// RESERVED (future use) 0x0A-0x1F +#define CDC_SERIAL_STATE 0x20 +// RESERVED (future use) 0x21-0x27 +#define CDC_CALL_STATE_CHANGE 0x28 +#define CDC_LINE_STATE_CHANGE 0x29 +#define CDC_CONNECTION_SPEED_CHANGE 0x2A +// RESERVED 0x2B-0x3F +// MDML SEMANTIC-MODEL-SPECIFIC NOTIFICATION 0x40-0x5F +// RESERVED (future use) 0x60-0xFF +#endif + + + + diff --git a/BPv4-bootloader/firmware-v1/configwords.h b/BPv4-bootloader/firmware-v1/configwords.h index c9a1053e..0b55ead3 100644 --- a/BPv4-bootloader/firmware-v1/configwords.h +++ b/BPv4-bootloader/firmware-v1/configwords.h @@ -1,32 +1,32 @@ -/* - * - * License: creative commons - attribution, share-alike - * Copyright Ian Lesnet and friends 2011 - * http://dangerousprototypes.com - * - */ - -#ifndef CONFIG_H -#define CONFIG_H - -#include "boot_config.h" - -#if defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) -_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV3 & IOL1WAY_ON & PLL_96MHZ_ON) - -#elif defined(JTR_PIC24FJXXXDA) -_CONFIG1(FWDTEN_OFF & ICS_PGx2 & GWRP_OFF & GCP_OFF & JTAGEN_OFF) -_CONFIG2(POSCMOD_HS & IOL1WAY_ON & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_PRIPLL & PLL96MHZ_ON & PLLDIV_DIV2 & IESO_OFF) - -#elif defined(JTR_PIC24FJXXGB2XX) -_CONFIG1(FWDTEN_OFF & ICS_PGx2 & GWRP_OFF & GCP_OFF & JTAGEN_OFF) -_CONFIG2(POSCMOD_HS & IOL1WAY_ON & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_PRIPLL & PLL96MHZ_ON & PLLDIV_DIV2 & IESO_OFF) - -#else -#error "ERROR wrong processor defined or no processor defined." -#endif - -#endif - - +/* + * + * License: creative commons - attribution, share-alike + * Copyright Ian Lesnet and friends 2011 + * http://dangerousprototypes.com + * + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "boot_config.h" + +#if defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) +_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV3 & IOL1WAY_ON & PLL_96MHZ_ON) + +#elif defined(JTR_PIC24FJXXXDA) +_CONFIG1(FWDTEN_OFF & ICS_PGx2 & GWRP_OFF & GCP_OFF & JTAGEN_OFF) +_CONFIG2(POSCMOD_HS & IOL1WAY_ON & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_PRIPLL & PLL96MHZ_ON & PLLDIV_DIV2 & IESO_OFF) + +#elif defined(JTR_PIC24FJXXGB2XX) +_CONFIG1(FWDTEN_OFF & ICS_PGx2 & GWRP_OFF & GCP_OFF & JTAGEN_OFF) +_CONFIG2(POSCMOD_HS & IOL1WAY_ON & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_PRIPLL & PLL96MHZ_ON & PLLDIV_DIV2 & IESO_OFF) + +#else +#error "ERROR wrong processor defined or no processor defined." +#endif + +#endif + + diff --git a/BPv4-bootloader/firmware-v1/descriptors.h b/BPv4-bootloader/firmware-v1/descriptors.h index c372a310..8783ac1f 100644 --- a/BPv4-bootloader/firmware-v1/descriptors.h +++ b/BPv4-bootloader/firmware-v1/descriptors.h @@ -1,162 +1,162 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. -*/ - -#ifndef __DESCRIPTORS_H__ -#define __DESCRIPTORS_H__ - -// JTR v0.1a - -#include "usb_stack.h" -#include "usb_lang.h" -#include "boot_config.h" - - - -/* String identifiers */ -#define USB_iManufacturer 1u -#define USB_iProduct 2u -#define USB_iSerialNum 3u - - -const unsigned char cdc_device_descriptor[] = { - 0x12, // bLength - USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType - 0x10, // bcdUSB (low byte) - 0x01, // bcdUSB (high byte) - 0x02, // bDeviceClass - 0x00, // bDeviceSubClass - 0x00, // bDeviceProtocol -// JTR removed USB_MAX_BUFFER_SIZE, // bMaxPacketSize - USB_EP0_BUFFER_SIZE, - - LOWB(USB_VID), // idVendor (low byte) - HIGHB(USB_VID), // idVendor (high byte) - LOWB(USB_PID), // idProduct (low byte) - HIGHB(USB_PID), // idProduct (high byte) - LOWB(USB_DEV), // bcdDevice (low byte) - HIGHB(USB_DEV), // bcdDevice (high byte) - USB_iManufacturer, // iManufacturer - USB_iProduct, // iProduct - USB_iSerialNum, // iSerialNumber (none) - USB_NUM_CONFIGURATIONS // bNumConfigurations -}; - -#define USB_CONFIG_DESC_TOT_LENGTH (9+9+5+4+5+5+7+9+7+7) -const unsigned char cdc_config_descriptor[] = { - 0x09, // bLength - USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType - LOWB(USB_CONFIG_DESC_TOT_LENGTH), // wTotalLength (low byte), TODO: Automatic calculation - sizeof doesn't work here - HIGHB(USB_CONFIG_DESC_TOT_LENGTH), // wTotalLength (high byte) - USB_NUM_INTERFACES, // bNumInterfaces - 0x01, // bConfigurationValue - 0x00, // iConfiguration (0=none) - 0x80, // bmAttributes (0x80 = bus powered) - 0x64, // bMaxPower (in 2 mA units, 50=100 mA) - - 0x09, // bLength (Interface0 descriptor starts here) - USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType - 0x00, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x01, // bNumEndpoints (excluding EP0) - 0x02, // bInterfaceClass (0x00=per endpoint specified, 0xFF=vendor specific) - 0x02, // bInterfaceSubClass (0x00=per endpoint specified, 0xFF=vendor specific) - 0x01, // bInterfaceProtocol (0x00=no protocol, 0xFE=as by command set, 0xFF=vendor specific) - 0x00, // iInterface (none) - - 0x05, // bFunctionLength - 0x24, // bDescriptorType - 0x00, // bDescriptorSubtype (CDC header descriptor) - 0x10, // bcdCDC (low byte) - 0x01, // bcdCDC (high byte) - - 0x04, // bFunctionLength - 0x24, // bDescriptorType - 0x02, // bDescriptorSubtype (CDC abstract control management descriptor) - 0x02, // bmCapabilities - - 0x05, // bFunctionLength - 0x24, // bDescriptorType - 0x06, // bDescriptorSubtype (CDC union descriptor) - 0x00, // bControlInterface - 0x01, // bSubordinateInterface0 - - 0x05, // bFunctionLength - 0x24, // bDescriptorType - 0x01, // bDescriptorSubtype (Call management descriptor) - 0x01, // bmCapabilities - 0x01, // bInterfaceNum - - 0x07, // bLength (Endpoint1 descriptor) - USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType - 0x81, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - LOWB(CDC_NOTICE_BUFFER_SIZE), // wMaxPacketSize (low byte) - HIGHB(CDC_NOTICE_BUFFER_SIZE), // wMaxPacketSize (high byte) - 0x40, // bInterval - - 0x09, // bLength (Interface1 descriptor) - USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType - 0x01, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x02, // bNumEndpoints - 0x0A, // bInterfaceClass - 0x00, // bInterfaceSubClass - 0x00, // bInterfaceProtocol (0x00=no protocol, 0xFE=functional unit, 0xFF=vendor specific) - 0x00, // iInterface - - 0x07, // bLength (Enpoint2 descriptor) - USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType - 0x02, // bEndpointAddress - 0x02, // bmAttributes (0x02=bulk) - LOWB(CDC_BUFFER_SIZE), // wMaxPacketSize (low byte) - HIGHB(CDC_BUFFER_SIZE), // wMaxPacketSize (high byte) - 0x40, // bInterval - - 0x07, // bLength - USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType - 0x82, // bEndpointAddress - 0x02, // bmAttributes (0x02=bulk) - LOWB(CDC_BUFFER_SIZE), // wMaxPacketSize (low byte) - HIGHB(CDC_BUFFER_SIZE), // wMaxPacketSize (high byte) - 0x40 // bInterval -/* - 0x09, // bLength (Interface2 descriptor) - USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType - 0x02, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x00, // bNumEndpoints (None, only control pipe used) - 0xFE, // bInterfaceClass - 0x01, // bInterfaceSubClass - 0x01, // bInterfaceProtocol - 0x00, // iInterface (none) - - 0x09, // bFunctionLength (DFU functional descriptor) - 0x21, // bDescriptorType - 0x03, // bmAttributes (!bitWillDetatch + !bitManifestationTolerant + bitCanUpload + bitCanDownload) - 0x80, 0x00, // wDetachTimeout (128 ms initial guess) - 0x20, 0x00, // wTransferSize (32 bytes) - 0x10, 0x01 // bcdDFUVersion */ -}; - -// TODO: Rework to ordinary datastructures. - -unsigned char cdc_str_descs[] = { - /* 0 */ 4, USB_STRING_DESCRIPTOR_TYPE, LOWB(USB_LANGID_English_United_States), HIGHB(USB_LANGID_English_United_States), - /* USB_iManufacturer */ 42, USB_STRING_DESCRIPTOR_TYPE, 'D',0,'a',0,'n',0,'g',0,'e',0,'r',0,'o',0,'u',0,'s',0,' ',0,'P',0,'r',0,'o',0,'t',0,'o',0,'t',0,'y',0,'p',0,'e',0,'s',0, - /* USB_iProduct */ 18, USB_STRING_DESCRIPTOR_TYPE, 'C',0,'D',0,'C',0,' ',0,'T',0,'e',0,'s',0,'t',0, - /* USB_iSerialNum */ 18, USB_STRING_DESCRIPTOR_TYPE, '0',0,'0',0,'0',0,'0',0,'0',0,'0',0,'0',0,'1',0 -}; - -/* TODO: Develop prebuild script for generating all complex structures */ - -#endif//__DESCRIPTORS_H__ +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. +*/ + +#ifndef __DESCRIPTORS_H__ +#define __DESCRIPTORS_H__ + +// JTR v0.1a + +#include "usb_stack.h" +#include "usb_lang.h" +#include "boot_config.h" + + + +/* String identifiers */ +#define USB_iManufacturer 1u +#define USB_iProduct 2u +#define USB_iSerialNum 3u + + +const unsigned char cdc_device_descriptor[] = { + 0x12, // bLength + USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType + 0x10, // bcdUSB (low byte) + 0x01, // bcdUSB (high byte) + 0x02, // bDeviceClass + 0x00, // bDeviceSubClass + 0x00, // bDeviceProtocol +// JTR removed USB_MAX_BUFFER_SIZE, // bMaxPacketSize + USB_EP0_BUFFER_SIZE, + + LOWB(USB_VID), // idVendor (low byte) + HIGHB(USB_VID), // idVendor (high byte) + LOWB(USB_PID), // idProduct (low byte) + HIGHB(USB_PID), // idProduct (high byte) + LOWB(USB_DEV), // bcdDevice (low byte) + HIGHB(USB_DEV), // bcdDevice (high byte) + USB_iManufacturer, // iManufacturer + USB_iProduct, // iProduct + USB_iSerialNum, // iSerialNumber (none) + USB_NUM_CONFIGURATIONS // bNumConfigurations +}; + +#define USB_CONFIG_DESC_TOT_LENGTH (9+9+5+4+5+5+7+9+7+7) +const unsigned char cdc_config_descriptor[] = { + 0x09, // bLength + USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType + LOWB(USB_CONFIG_DESC_TOT_LENGTH), // wTotalLength (low byte), TODO: Automatic calculation - sizeof doesn't work here + HIGHB(USB_CONFIG_DESC_TOT_LENGTH), // wTotalLength (high byte) + USB_NUM_INTERFACES, // bNumInterfaces + 0x01, // bConfigurationValue + 0x00, // iConfiguration (0=none) + 0x80, // bmAttributes (0x80 = bus powered) + 0x64, // bMaxPower (in 2 mA units, 50=100 mA) + + 0x09, // bLength (Interface0 descriptor starts here) + USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType + 0x00, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x01, // bNumEndpoints (excluding EP0) + 0x02, // bInterfaceClass (0x00=per endpoint specified, 0xFF=vendor specific) + 0x02, // bInterfaceSubClass (0x00=per endpoint specified, 0xFF=vendor specific) + 0x01, // bInterfaceProtocol (0x00=no protocol, 0xFE=as by command set, 0xFF=vendor specific) + 0x00, // iInterface (none) + + 0x05, // bFunctionLength + 0x24, // bDescriptorType + 0x00, // bDescriptorSubtype (CDC header descriptor) + 0x10, // bcdCDC (low byte) + 0x01, // bcdCDC (high byte) + + 0x04, // bFunctionLength + 0x24, // bDescriptorType + 0x02, // bDescriptorSubtype (CDC abstract control management descriptor) + 0x02, // bmCapabilities + + 0x05, // bFunctionLength + 0x24, // bDescriptorType + 0x06, // bDescriptorSubtype (CDC union descriptor) + 0x00, // bControlInterface + 0x01, // bSubordinateInterface0 + + 0x05, // bFunctionLength + 0x24, // bDescriptorType + 0x01, // bDescriptorSubtype (Call management descriptor) + 0x01, // bmCapabilities + 0x01, // bInterfaceNum + + 0x07, // bLength (Endpoint1 descriptor) + USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType + 0x81, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + LOWB(CDC_NOTICE_BUFFER_SIZE), // wMaxPacketSize (low byte) + HIGHB(CDC_NOTICE_BUFFER_SIZE), // wMaxPacketSize (high byte) + 0x40, // bInterval + + 0x09, // bLength (Interface1 descriptor) + USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType + 0x01, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x02, // bNumEndpoints + 0x0A, // bInterfaceClass + 0x00, // bInterfaceSubClass + 0x00, // bInterfaceProtocol (0x00=no protocol, 0xFE=functional unit, 0xFF=vendor specific) + 0x00, // iInterface + + 0x07, // bLength (Enpoint2 descriptor) + USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType + 0x02, // bEndpointAddress + 0x02, // bmAttributes (0x02=bulk) + LOWB(CDC_BUFFER_SIZE), // wMaxPacketSize (low byte) + HIGHB(CDC_BUFFER_SIZE), // wMaxPacketSize (high byte) + 0x40, // bInterval + + 0x07, // bLength + USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType + 0x82, // bEndpointAddress + 0x02, // bmAttributes (0x02=bulk) + LOWB(CDC_BUFFER_SIZE), // wMaxPacketSize (low byte) + HIGHB(CDC_BUFFER_SIZE), // wMaxPacketSize (high byte) + 0x40 // bInterval +/* + 0x09, // bLength (Interface2 descriptor) + USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType + 0x02, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x00, // bNumEndpoints (None, only control pipe used) + 0xFE, // bInterfaceClass + 0x01, // bInterfaceSubClass + 0x01, // bInterfaceProtocol + 0x00, // iInterface (none) + + 0x09, // bFunctionLength (DFU functional descriptor) + 0x21, // bDescriptorType + 0x03, // bmAttributes (!bitWillDetatch + !bitManifestationTolerant + bitCanUpload + bitCanDownload) + 0x80, 0x00, // wDetachTimeout (128 ms initial guess) + 0x20, 0x00, // wTransferSize (32 bytes) + 0x10, 0x01 // bcdDFUVersion */ +}; + +// TODO: Rework to ordinary datastructures. + +unsigned char cdc_str_descs[] = { + /* 0 */ 4, USB_STRING_DESCRIPTOR_TYPE, LOWB(USB_LANGID_English_United_States), HIGHB(USB_LANGID_English_United_States), + /* USB_iManufacturer */ 42, USB_STRING_DESCRIPTOR_TYPE, 'D',0,'a',0,'n',0,'g',0,'e',0,'r',0,'o',0,'u',0,'s',0,' ',0,'P',0,'r',0,'o',0,'t',0,'o',0,'t',0,'y',0,'p',0,'e',0,'s',0, + /* USB_iProduct */ 18, USB_STRING_DESCRIPTOR_TYPE, 'C',0,'D',0,'C',0,' ',0,'T',0,'e',0,'s',0,'t',0, + /* USB_iSerialNum */ 18, USB_STRING_DESCRIPTOR_TYPE, '0',0,'0',0,'0',0,'0',0,'0',0,'0',0,'0',0,'1',0 +}; + +/* TODO: Develop prebuild script for generating all complex structures */ + +#endif//__DESCRIPTORS_H__ diff --git a/BPv4-bootloader/firmware-v1/globals.h b/BPv4-bootloader/firmware-v1/globals.h index 42629801..6e8d1b78 100644 --- a/BPv4-bootloader/firmware-v1/globals.h +++ b/BPv4-bootloader/firmware-v1/globals.h @@ -1,37 +1,37 @@ -#ifndef GLOBALS_H -#define GLOBALS_H - -// =================== GLOBAL TYPEDEFS -typedef unsigned char u8; -typedef unsigned int u16; -typedef unsigned long u32; -typedef unsigned char BYTE; - -#define INVALID 0xFF - -#define FALSE 0 -#define TRUE (!FALSE) - -// =================== GLOBAL HEADERS -#include -#include -#include - -#include "boot_config.h" -#include "usb_stack.h" -#include "cdc.h" - -#define LedSetup() TRISBbits.TRISB8=0;TRISBbits.TRISB9=0;TRISBbits.TRISB10=0 - -#define uLedOn() LATBbits.LATB10 = 0 -#define uLedOff() LATBbits.LATB10 = 1 -#define uLedToggle() LATBbits.LATB10 ^= LATBbits.LATB10 - -#define mLedOn() LATBbits.LATB8 = 1 -#define mLedOff() LATBbits.LATB8 = 0 -#define mLedToggle() LATBbits.LATB8 ^= LATBbits.LATB8 - -#define vLedOn() LATBbits.LATB9 = 1 -#define vLedOff() LATBbits.LATB9 = 0 -#define vLedToggle() LATBbits.LATB9 ^= LATBbits.LATB9 -#endif +#ifndef GLOBALS_H +#define GLOBALS_H + +// =================== GLOBAL TYPEDEFS +typedef unsigned char u8; +typedef unsigned int u16; +typedef unsigned long u32; +typedef unsigned char BYTE; + +#define INVALID 0xFF + +#define FALSE 0 +#define TRUE (!FALSE) + +// =================== GLOBAL HEADERS +#include +#include +#include + +#include "boot_config.h" +#include "usb_stack.h" +#include "cdc.h" + +#define LedSetup() TRISBbits.TRISB8=0;TRISBbits.TRISB9=0;TRISBbits.TRISB10=0 + +#define uLedOn() LATBbits.LATB10 = 0 +#define uLedOff() LATBbits.LATB10 = 1 +#define uLedToggle() LATBbits.LATB10 ^= LATBbits.LATB10 + +#define mLedOn() LATBbits.LATB8 = 1 +#define mLedOff() LATBbits.LATB8 = 0 +#define mLedToggle() LATBbits.LATB8 ^= LATBbits.LATB8 + +#define vLedOn() LATBbits.LATB9 = 1 +#define vLedOff() LATBbits.LATB9 = 0 +#define vLedToggle() LATBbits.LATB9 ^= LATBbits.LATB9 +#endif diff --git a/BPv4-bootloader/firmware-v1/main.c b/BPv4-bootloader/firmware-v1/main.c index 374183aa..5004a433 100644 --- a/BPv4-bootloader/firmware-v1/main.c +++ b/BPv4-bootloader/firmware-v1/main.c @@ -1,151 +1,151 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//BOOTLOADER PROJECT MAIN FILE - -#include "globals.h" -#include "configwords.h" // JTR only included in main.c -#include "descriptors.h" // JTR Only included in main.c -#include "bootloader.h" - -void usb_start(void); -void initCDC(void); -void usb_init(ROMPTR const unsigned char*, ROMPTR const unsigned char*, ROMPTR const unsigned char*, int); -void Initialize(void); -void bootloader(void); -void userjumpin(void); -extern volatile unsigned char usb_device_state; -void startpoint(void); - -#pragma code - -int main(void) { - volatile int i; - //asm("DISI #16"); //Disable interrupts - - //configure PGC PGD for jumper test - //PGC is output - #define PGC_OUT LATBbits.LATB6 - #define PGC_TRIS TRISBbits.TRISB6 - #define PGC_PU CNPU2bits.CN24PUE - #define PGC_IN PORTBbits.RB6 - - #define PGD_IN PORTBbits.RB7 - #define PGD_TRIS TRISBbits.TRISB7 - #define PGD_PU CNPU2bits.CN25PUE - - AD1PCFGL=0xFFFF;//all digital - - //PGD is input with pullup - //TRISB|=0b10000000; // - PGD_TRIS=1; //input - //CNPU2|=0b1000000000; // - PGD_PU=1; //pullup on - - PGC_OUT=0; - PGC_TRIS=0; - - i=5000; - while(i--); - - for(i=0; i<20; i++){ -//(PORTCbits.RC14==1)|| - //if((PORTBbits.RB7==1)){ //go to user space on first mis-match - if((PGD_IN==1)){ //go to user space on first mis-match - //continue to bootloader, or exit - asm (".equ BLJUMPADDRESS, 0x2000"); - asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location - "goto w1 \n"); - - } - } - - PGD_PU=0; //pullup off - PGC_TRIS=1;//input - startpoint(); - return 0; -} - - -//fixed location to jump from user space -//really bad! Can't figure out how to put a goto to a label from C, was easy in ASM... -void __attribute__((space(prog),address(BLENDADDR-7))) userjumpin(){ - startpoint(); -} - -void startpoint(){ - Initialize(); //setup bus pirate - LedSetup(); - uLedOff(); - mLedOff(); - vLedOn(); - - usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro - usb_start(); - initCDC(); // Setup CDC defaults. - vLedOff(); - //wait for the USB connection to enumerate - do { - //if (!TestUsbInterruptEnabled()) //JTR3 added - - usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE - //if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ - // } else if (usb_device_state < CONFIGURED_STATE) { - // } - } while (usb_device_state < CONFIGURED_STATE); // JTR addition. Do not proceed until device is configured. - vLedOn(); - bootloader(); -} - -void Initialize(void) { - CORCONbits.PSV = 1; - PSVPAG = 0; // - CLKDIV = 0x0000; // Set PLL prescaler (1:1) - TBLPAG = 0; - - //all high-z to protect everything - AD1PCFGL=0xFFFF;//all digital - AD1PCFGH = 0x2; - //TRISA=0xFFFF; - TRISB=0xFFFF; - TRISC=0xFFFF; - TRISD=0xFFFF; - TRISE=0xFFFF; - TRISF=0xFFFF; - TRISG=0xFFFF; - OSCCONbits.SOSCEN = 0; -} - -#ifdef USB_INTERRUPT -#pragma interrupt _USB1Interrupt - -void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() { - - //USB interrupt - //IRQ enable IEC5bits.USB1IE - //IRQ flag IFS5bits.USB1IF - //IRQ priority IPC21<10:8> - //{ - usb_handler(); - IFS5bits.USB1IF = 0; // PIR2bits.USBIF = 0; - //} -} -#endif - - - - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//BOOTLOADER PROJECT MAIN FILE + +#include "globals.h" +#include "configwords.h" // JTR only included in main.c +#include "descriptors.h" // JTR Only included in main.c +#include "bootloader.h" + +void usb_start(void); +void initCDC(void); +void usb_init(ROMPTR const unsigned char*, ROMPTR const unsigned char*, ROMPTR const unsigned char*, int); +void Initialize(void); +void bootloader(void); +void userjumpin(void); +extern volatile unsigned char usb_device_state; +void startpoint(void); + +#pragma code + +int main(void) { + volatile int i; + //asm("DISI #16"); //Disable interrupts + + //configure PGC PGD for jumper test + //PGC is output + #define PGC_OUT LATBbits.LATB6 + #define PGC_TRIS TRISBbits.TRISB6 + #define PGC_PU CNPU2bits.CN24PUE + #define PGC_IN PORTBbits.RB6 + + #define PGD_IN PORTBbits.RB7 + #define PGD_TRIS TRISBbits.TRISB7 + #define PGD_PU CNPU2bits.CN25PUE + + AD1PCFGL=0xFFFF;//all digital + + //PGD is input with pullup + //TRISB|=0b10000000; // + PGD_TRIS=1; //input + //CNPU2|=0b1000000000; // + PGD_PU=1; //pullup on + + PGC_OUT=0; + PGC_TRIS=0; + + i=5000; + while(i--); + + for(i=0; i<20; i++){ +//(PORTCbits.RC14==1)|| + //if((PORTBbits.RB7==1)){ //go to user space on first mis-match + if((PGD_IN==1)){ //go to user space on first mis-match + //continue to bootloader, or exit + asm (".equ BLJUMPADDRESS, 0x2000"); + asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location + "goto w1 \n"); + + } + } + + PGD_PU=0; //pullup off + PGC_TRIS=1;//input + startpoint(); + return 0; +} + + +//fixed location to jump from user space +//really bad! Can't figure out how to put a goto to a label from C, was easy in ASM... +void __attribute__((space(prog),address(BLENDADDR-7))) userjumpin(){ + startpoint(); +} + +void startpoint(){ + Initialize(); //setup bus pirate + LedSetup(); + uLedOff(); + mLedOff(); + vLedOn(); + + usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro + usb_start(); + initCDC(); // Setup CDC defaults. + vLedOff(); + //wait for the USB connection to enumerate + do { + //if (!TestUsbInterruptEnabled()) //JTR3 added + + usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE + //if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ + // } else if (usb_device_state < CONFIGURED_STATE) { + // } + } while (usb_device_state < CONFIGURED_STATE); // JTR addition. Do not proceed until device is configured. + vLedOn(); + bootloader(); +} + +void Initialize(void) { + CORCONbits.PSV = 1; + PSVPAG = 0; // + CLKDIV = 0x0000; // Set PLL prescaler (1:1) + TBLPAG = 0; + + //all high-z to protect everything + AD1PCFGL=0xFFFF;//all digital + AD1PCFGH = 0x2; + //TRISA=0xFFFF; + TRISB=0xFFFF; + TRISC=0xFFFF; + TRISD=0xFFFF; + TRISE=0xFFFF; + TRISF=0xFFFF; + TRISG=0xFFFF; + OSCCONbits.SOSCEN = 0; +} + +#ifdef USB_INTERRUPT +#pragma interrupt _USB1Interrupt + +void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() { + + //USB interrupt + //IRQ enable IEC5bits.USB1IE + //IRQ flag IFS5bits.USB1IF + //IRQ priority IPC21<10:8> + //{ + usb_handler(); + IFS5bits.USB1IF = 0; // PIR2bits.USBIF = 0; + //} +} +#endif + + + + + diff --git a/BPv4-bootloader/firmware-v1/picusb.h b/BPv4-bootloader/firmware-v1/picusb.h index 67278faa..386004e3 100644 --- a/BPv4-bootloader/firmware-v1/picusb.h +++ b/BPv4-bootloader/firmware-v1/picusb.h @@ -1,537 +1,537 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - -Revision: -1 15th March 2011 // Please track your changes! -// JTR v0.1a -Comments by JTR. - -This file is a combined header for all the PIC18F and PIC24F -USB PICs for the Honken USB stack. It has been corrected for -the PIC24 parts and slightly rearranged to reduce duplicate -entries common to both PIC families. - - - */ - -#ifndef picusb_h -#define picusb_h - -// JTR TODO Add support for the PIC24E and dsPIC33E parts when they are released. - -#if defined(__18F2450) || defined(__18F2455) || defined(__18F2458) || defined(__18F2550) || defined(__18F2553)|| defined(__18F4450) || defined(__18F4455) || defined(__18F4458) || defined(__18F4550) || defined(__18F4553) -#define PIC_18F -#define PIC18Fxx5x -#define BD_RAM = 0x400 - -#elif defined(__18F13K50) || defined(__18F14K50) //JTR add -#define PIC_18F -#define PIC18FxxK -#define BD_RAM 0x200 - -#elif defined(__18F24J50) || defined(__18F25J50) || defined(__18F26J50) || defined(__18F44J50) || defined(__18F45J50) || defined(__18F46J50) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define BD_RAM = 0x400 - -#elif defined(__18F26J53) || defined(__18F27J53) || defined(__18F46J53) || defined(__18F47J53) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define BD_RAM = 0xd00 - -#elif defined(__18F65J50) || defined(__18F66J50) || defined(__18F66J55) || defined(__18F67J50) || defined(__18F85J50) || defined(__18F86J50) || defined(__18F66J55) || defined(__18F67J50) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define USE_ALT_ANCON -#define BD_RAM = 0x400 - -#elif defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) -#define PIC_24F - -#elif defined(__PIC24FJ32GB002__) || defined(__PIC24FJ32GB004__) || defined(__PIC24FJ64GB002__) || defined(__PIC24FJ64GB004__) -#define PIC_24F -#define PLLEN_REQD - -#elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(PIC24FJ256GB206__) || defined(PIC24FJ256GB210__) -#define PIC_24F -#define USE_ANS - -#elif defined(__PIC24FJ128DA106__) || defined(__PIC24FJ128DA110__) || defined(__PIC24FJ128DA206__) || defined(__PIC24FJ128DA210__) || defined(__PIC24FJ256DA106__) || defined(__PIC24FJ256DA110__) || defined(_PIC24FJ256DA206__) || defined(__PIC24FJ256DA210__) -#define PIC_24F -#define PLLEN_REQD -#define USE_ANS -#endif - -#if defined(__18F13K50) || defined(__18F14K50) //JTR add -#define MAX_CHIP_EP (8u) -#else -#define MAX_CHIP_EP (16u) -#endif - -/* COMMON PIC DEFINES TO BOTH PIC18 AND PIC24 */ - -#define UOWN 0x80 -#define DTS 0x40 -#define KEN 0x20 -#define INCDIS 0x10 -#define DTSEN 0x08 -#define BSTALL 0x04 -#define BC98 0x03 - - -#define USB_DIR_OUT 0 -#define USB_DIR_IN 1 -#define USB_PP_EVEN 0 -#define USB_PP_ODD 1 - - -/* PIC DEFINES SPECIFIC TO PIC18 */ - -#if defined(PIC_18F) - -#define MyProcessor // JTR check that a PIC is defined -#include - -#define USTAT_ODD_EVEN (2) // JTR may be required for ping-pong BD* calculations and are different from PIC18 to PIC24 -#define USTAT_ODD_EVEN_SHIFT (1) - -// JTR removed not used and the values??? -// #define USB_EP_INTERRUPT (0) -// #define USB_EP_BULK (1) -// #define USB_EP_ISOCHRONOUS (2) - -#define USB_UEP_EPSTALL (0x01) // JTR Note. to self. these values are different between the PIC18 and PIC24 -#define USB_UEP_EPINEN (0x02) -#define USB_UEP_EPOUTEN (0x04) -#define USB_UEP_EPCONDIS (0x08) -#define USB_UEP_EPHSHK (0x10) - -#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) -#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) -#define USB_EP_NONE (0x00) - -typedef unsigned char usb_uep_t; -#define USB_UEP ((usb_uep_t*) (&UEP0)) -#define USB_UEP0 UEP0 -#define USB_UEP1 UEP1 -#define USB_UEP2 UEP2 -#define USB_UEP3 UEP3 -#define USB_UEP4 UEP4 -#define USB_UEP5 UEP5 -#define USB_UEP6 UEP6 -#define USB_UEP7 UEP7 - -#ifndef PIC18FxxK -#define USB_UEP8 UEP8 -#define USB_UEP9 UEP9 -#define USB_UEP10 UEP10 -#define USB_UEP11 UEP11 -#define USB_UEP12 UEP12 -#define USB_UEP13 UEP13 -#define USB_UEP14 UEP14 -#define USB_UEP15 UEP15 -#endif - -/* Interrupt */ -#define USB_SOF 0x40 -#define USB_STALL 0x20 -#define USB_IDLE 0x10 -#define USB_TRN 0x08 -#define USB_ACTIV 0x04 -#define USB_UERR 0x02 -#define USB_URST 0x01 - -#define USB_RESET_FLAG UIRbits.URSTIF -#define USB_ERROR_FLAG UIRbits.UERRIF -#define USB_ACTIVITY_FLAG UIRbits.ACTVIF -#define USB_IDLE_FLAG UIRbits.IDLEIF -#define USB_STALL_FLAG UIRbits.STALLIF -#define USB_SOF_FLAG UIRbits.SOFIF -#define USB_TRANSACTION_FLAG UIRbits.TRNIF - -#define UsbInterruptFlags() (UIR) -#define UsbErrorInterruptFlags() (UEIR) - -#define TestGlobalUsbInterruptFlag() PIR2bits.USBIF -#define TestUsbTrfInterruptFlag() UIR (x) -#define ClearGlobalUsbInterruptFlag() PIR2bits.USBIF = 0 -#define ClearUsbInterruptFlag(x) UIR &= ~(x) -#define ClearAllUsbInterruptFlags() UIR = 0 -#define ClearUsbErrorInterruptFlag(x) UEIR &= ~(x) -#define ClearAllUsbErrorInterruptFlags() UEIR = 0 -#define DisableUsbInterrupts() PIE2bits.USBIE = 0 -#define DisableUsbInterrupt(x) UIE &= ~(x) -#define DisableAllUsbInterrupts() UIE = 0 -#define DisableUsbErrorInterrupt(x) UEIE &= ~(x) -#define DisableAllUsbErrorInterrupts() UEIE = 0 -#define EnableUsbInterrupts() PIE2bits.USBIE = 1 -#define EnableUsbInterrupt(x) UIE |= (x) -#define EnableAllUsbInterrupts() UIE = 0xFF -#define EnableUsbErrorInterrupt(x) UEIE |= (x) -#define EnableAllUsbErrorInterrupts() UEIE = 0xFF -#define EnableUSBHighInterrupts() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new -#define EnableUSBLowInterrupts() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new -// JTR TODO define for NO priority interrupt. - -/* UCON */ -#define ResetPPbuffers() do {UCONbits.PPBRST = 1; UCONbits.PPBRST=0;} while(0) -#define SingleEndedZeroIsSet() (UCONbits.SE0) -#define EnablePacketTransfer() UCONbits.PKTDIS = 0 -#define EnableUsb() UCONbits.USBEN = 1 -#define SignalResume() do {UCONbits.RESUME = 1; delay_ms(10); UCONbits.RESUME = 0;} while(0) -#define SuspendUsb() UCONbits.SUSPND = 1 -#define WakeupUsb() do {UCONbits.SUSPND = 0; while(USB_ACTIVITY_FLAG){USB_ACTIVITY_FLAG = 0;}} while(0) - -/* UADDR */ -#define SetUsbAddress(x) (UADDR = (x)) -#define GetUsbAddress() (UADDR) - -/* USTAT */ -// typedef unsigned char usb_status_t; -#define GetUsbTransaction() (USTAT) -#define USB_STAT2EP(x) ((x>>3)&0x0F) -#define USB_STAT2DIR(x) ((x>>2)&0x01) -#define USB_STAT2ADDR(x) ((x>>2)&0x1F) -#define USB_STAT2PPI(x) ((x>>1)&0x01) -#define DIRBIT 0x4 // JTR addition. Different between PIC18 and PIC24 -/* Hardware implementations */ - -#if defined USB_INTERNAL_PULLUPS -#define USB_UCFG_UPUEN_VALUE (1<<4) -#elif defined USB_EXTERNAL_PULLUPS -#define USB_UCFG_UPUEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_INTERNAL_TRANSCIEVER -#define USB_UCFG_UTRDIS_VALUE (0) -#elif defined USB_EXTERNAL_TRANSCIEVER -#define USB_UCFG_UTRDIS_VALUE (1<<3) -#else -#error "Neither internal nor external transciever defined" -#endif - -#if defined USB_FULL_SPEED_DEVICE -#define USB_UCFG_FSEN_VALUE (1<<2) -#elif defined USB_LOW_SPEED_DEVICE -#define USB_UCFG_FSEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_BUS_POWERED -#ifndef usb_low_power_request -/* Default low power mode is DUD */ -#define usb_low_power_request() Nop() -#endif -#ifndef usb_low_power_resume -#define usb_low_power_resume() Nop() -#endif -#elif defined USB_SELF_POWERED -#define usb_low_power_request() Nop() -#define usb_low_power_resume() Nop() -#else -#error "No source of device power defined" -#endif - -#ifndef USB_INTERNAL_VREG -#warning "Use of internal voltage regulator not defined. User must supply 3.3V on Vusb pin." -#endif - -//#define USB_DIR_OUT 0 -//#define USB_DIR_IN 1 -//#define USB_PP_EVEN 0 -//#define USB_PP_ODD 1 - -/* PingPong buffer descriptor table index calculations */ -#if USB_PP_BUF_MODE == 0 -#define USB_USTAT2BD(X) ( (X)/4 ) -#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) -#elif USB_PP_BUF_MODE == 1 -#error "USB_PP_BUF_MODE outside scope." -#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) -#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) -#elif USB_PP_BUF_MODE == 2 -#error "USB_PP_BUF_MODE outside scope." -#define USB_USTAT2BD(X) ( (X)/2 ) -#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) -#elif USB_PP_BUF_MODE == 3 -#error "USB_PP_BUF_MODE outside scope." - -#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) -#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) - - -#else -#error "USB_PP_BUF_MODE outside scope." -#endif - -#define USB_UCFG_REGISTER_VALUE ((USB_UCFG_UPUEN_VALUE) | \ - (USB_UCFG_UTRDIS_VALUE) | \ - (USB_UCFG_FSEN_VALUE) | \ - (USB_PP_BUF_MODE)) - -#define ConfigureUsbHardware() do {UCFG = USB_UCFG_REGISTER_VALUE;} while(0) - -#define ROM far rom -#define ROMPTR far rom -#define ARCH_memcpy memcpypgm2ram - -typedef struct BDENTRY { - unsigned char - /* struct { - unsigned BCH:2; - unsigned BSTALL:1; - unsigned DTSEN:1; - unsigned INCDIS:1; - unsigned KEN:1; - unsigned DTS:1; - unsigned UOWN:1; - }*/ - BDSTAT; - unsigned char BDCNT; - unsigned char *BDADDR; -} BDentry; -#endif // End of specific PIC18 defines. - - -/* PIC DEFINES SPECIFIC TO PIC24 FOLLOW */ - -#if defined(PIC_24F) - -#define MyProcessor -#include - -#define USTAT_ODD_EVEN (4) // JTR PIC24 fixup potentially ?? Only required when ping-pong buffering is enabled. -#define USTAT_ODD_EVEN_SHIFT (2) // JTR these are required for BD* calculations and are different for the PIC24 - -/* Bitmasks */ -#define USB_UEP_EPHSHK (0x01) -#define USB_UEP_EPSTALL (0x02) -#define USB_UEP_EPINEN (0x04) -#define USB_UEP_EPOUTEN (0x08) -#define USB_UEP_EPCONDIS (0x10) - -#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) -#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) -#define USB_EP_NONE (0x00) - -// JTR removed not used and the values?? -// #define USB_EP_INTERRUPT (0) -// #define USB_EP_BULK (1) -// #define USB_EP_ISOCHRONOUS (2) - -typedef unsigned int usb_uep_t; // JTR PIC24 fixup potentially ?? changed from char. -// See http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=150#p17703 - -// However the stack as modified by JTR does not actually use it. Then again it might as it is tested and works. - -#define USB_UEP ((usb_uep_t*)(&U1EP0)) -#define USB_UEP0 U1EP0 -#define USB_UEP1 U1EP1 -#define USB_UEP2 U1EP2 -#define USB_UEP3 U1EP3 -#define USB_UEP4 U1EP4 -#define USB_UEP5 U1EP5 -#define USB_UEP6 U1EP6 -#define USB_UEP7 U1EP7 -#define USB_UEP8 U1EP8 -#define USB_UEP9 U1EP9 -#define USB_UEP10 U1EP10 -#define USB_UEP11 U1EP11 -#define USB_UEP12 U1EP12 -#define USB_UEP13 U1EP13 -#define USB_UEP14 U1EP14 -#define USB_UEP15 U1EP15 - -/* Interrupt */ -#define USB_STALL 0x0080 -#define USB_ACTIV 0x0020 -#define USB_IDLE 0x0010 -#define USB_TRN 0x0008 -#define USB_SOF 0x0004 -#define USB_UERR 0x0002 -#define USB_URST 0x0001 - -#define USB_RESET_FLAG U1IRbits.URSTIF -#define USB_ERROR_FLAG U1IRbits.UERRIF -#define USB_ACTIVITY_FLAG U1IRbits.RESUMEIF -#define USB_IDLE_FLAG U1IRbits.IDLEIF -#define USB_STALL_FLAG U1IRbits.STALLIF -#define USB_SOF_FLAG U1IRbits.SOFIF -#define USB_TRANSACTION_FLAG U1IRbits.TRNIF - -#define UsbInterruptFlags() (U1IR) -#define UsbErrorInterruptFlags() (U1EIR) -#define TestGlobalUsbInterruptFlag() IFS5bits.USB1IF -#define ClearGlobalUsbInterruptFlag() IFS5bits.USB1IF = 0 -#define ClearUsbInterruptFlag(x) U1IR = x -#define ClearAllUsbInterruptFlags() U1IR = 0xFF -#define ClearUsbErrorInterruptFlag(x) U1EIR = x -#define ClearAllUsbErrorInterruptFlags() U1EIR = 0xFF -#define DisableUsbInterrupts() IEC5bits.USB1IE = 0 -#define DisableUsbInterrupt(x) U1IE &= ~(x) -#define DisableAllUsbInterrupts() U1IE = 0 -#define DisableUsbErrorInterrupt(x) U1EIE &= ~(x) -#define DisableAllUsbErrorInterrupts() U1EIE = 0 -#define TestUsbInterruptEnabled() IEC5bits.USB1IE - -#ifdef USB_INTERRUPT -#define EnableUsbInterrupts() IEC5bits.USB1IE=1 -#define EnableUsbInterrupt(x) U1IE |= (x) -#define EnableAllUsbInterrupts() U1IE = 0x00FF -#define EnableUsbErrorInterrupt(x) U1EIE |= (x) -#define EnableAllUsbErrorInterrupts() U1EIE = 0x00FF -#define EnableUSBHighInterrupts() -#else -#define EnableUsbInterrupts() -#define EnableUsbInterrupt(x) -#define EnableAllUsbInterrupts() -#define EnableUsbErrorInterrupt(x) -#define EnableAllUsbErrorInterrupts() -#define EnableUSBHighInterrupts() -#endif - -/* UCON */ -#define ResetPPbuffers() do {U1CONbits.PPBRST = 1; U1CONbits.PPBRST=0;} while(0) -#define SingleEndedZeroIsSet() (U1CONbits.SE0) -#define EnablePacketTransfer() U1CONbits.PKTDIS = 0 -#define EnableUsb() while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;} // U1CONbits.USBEN = 1 -#define SignalResume() do {U1CONbits.RESUME = 1; delay_ms(10); U1CONbits.RESUME = 0;} while(0) -#define SuspendUsb() U1PWRCbits.USUSPND = 1 -#define WakeupUsb() do {U1PWRCbits.USUSPND = 0; while(USB_ACTIVITY_FLAG){USB_ACTIVITY_FLAG = 0;}} while(0) - - -/* UADDR */ -#define SetUsbAddress(x) (U1ADDR = (x)) -#define GetUsbAddress() (U1ADDR) - -#define GetUsbTransaction() (U1STAT) -#define USB_STAT2EP(x) ((x>>4)&0x0F) //((x>>3)&0x0F) JTR PIC24 fixups -#define USB_STAT2DIR(x) ((x>>3)&0x01) //((x>>2)&0x01) "" -#define USB_STAT2ADDR(x) ((x>>3)&0x1F) //((x>>2)&0x1F) "" -#define USB_STAT2PPI(x) ((x>>2 &0x01) //((x>>1)&0x01) "" -#define DIRBIT 0x8 // JTR add PIC24 fixup as the mask is 0x4 for the PIC18 - - -/* Hardware implementations */ - -#if defined USB_INTERNAL_PULLUPS -#elif defined USB_EXTERNAL_PULLUPS -#define USB_U1OTGCON_UPUEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_INTERNAL_TRANSCIEVER -#define USB_U1CNFG2_UTRDIS_VALUE (0) -#elif defined USB_EXTERNAL_TRANSCIEVER -#define USB_U1CNFG2_UTRDIS_VALUE (1) -#else -#error "Neither internal nor external transciever defined" -#endif - -#if defined USB_FULL_SPEED_DEVICE -#if defined USB_INTERNAL_PULLUPS -#define USB_U1OTGCON_DPPULUP_VALUE (1<<7) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#else -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#endif -#elif defined USB_LOW_SPEED_DEVICE -#if defined USB_INTERNAL_PULLUPS -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (1<<6) -#else -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#endif -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_BUS_POWERED -#ifndef usb_low_power_request -/* Default low power mode is DUD */ -#define usb_low_power_request() Nop() -#endif -#ifndef usb_low_power_resume -#define usb_low_power_resume() Nop() -#endif -#elif defined USB_SELF_POWERED -#define usb_low_power_request() Nop() -#define usb_low_power_resume() Nop() -#else -#error "No source of device power defined" -#endif - -#ifndef USB_INTERNAL_VREG -#warning "Use of internal voltage regulator not implemented. User must supply 3.3V on Vusb pin." -#endif - -/* PingPong buffer descriptor table index calculations */ -#if USB_PP_BUF_MODE == 0 -#define USB_USTAT2BD(X) ( (X)/8 ) //JTR PIC24 fixups -#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) - -// JTR TODO these values may need to be changed for the PIC24 -//#elif USB_PP_BUF_MODE == 1 -//#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) -//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) -//#elif USB_PP_BUF_MODE == 2 -//#define USB_USTAT2BD(X) ( (X)/2 ) -//#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) -//#elif USB_PP_BUF_MODE == 3 -//#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) -//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) - -#else -#error "USB_PP_BUF_MODE outside scope." -#endif - -// JTR PIC24 fixups some may not required but are "more correct." The added /256 is required! -#define ConfigureUsbHardware() do { \ - U1CNFG1 = USB_PP_BUF_MODE; \ - U1CNFG2 = USB_U1CNFG2_UTRDIS_VALUE; \ - U1BDTP1 = (unsigned int) usb_bdt/256; \ - U1OTGCON = USB_U1OTGCON_DPPULUP_VALUE | \ - USB_U1OTGCON_DMPULUP_VALUE; \ - U1PWRCbits.USBPWR = 1; \ - } while(0) - -#define ROM __attribute__((space(auto_psv))) -#define ROMPTR -#define ARCH_memcpy memcpy - -typedef struct BDENTRY { - unsigned char BDCNT; // JTR PIC24 fixup Note that the endianness is swapped from the PIC18 - // This is probably what had people hitting their heads! - unsigned char BDSTAT; - unsigned char *BDADDR; -} BDentry; - -#endif - -#ifndef MyProcessor -#error "No PIC family defined" -#endif - -#endif +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + +Revision: +1 15th March 2011 // Please track your changes! +// JTR v0.1a +Comments by JTR. + +This file is a combined header for all the PIC18F and PIC24F +USB PICs for the Honken USB stack. It has been corrected for +the PIC24 parts and slightly rearranged to reduce duplicate +entries common to both PIC families. + + + */ + +#ifndef picusb_h +#define picusb_h + +// JTR TODO Add support for the PIC24E and dsPIC33E parts when they are released. + +#if defined(__18F2450) || defined(__18F2455) || defined(__18F2458) || defined(__18F2550) || defined(__18F2553)|| defined(__18F4450) || defined(__18F4455) || defined(__18F4458) || defined(__18F4550) || defined(__18F4553) +#define PIC_18F +#define PIC18Fxx5x +#define BD_RAM = 0x400 + +#elif defined(__18F13K50) || defined(__18F14K50) //JTR add +#define PIC_18F +#define PIC18FxxK +#define BD_RAM 0x200 + +#elif defined(__18F24J50) || defined(__18F25J50) || defined(__18F26J50) || defined(__18F44J50) || defined(__18F45J50) || defined(__18F46J50) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define BD_RAM = 0x400 + +#elif defined(__18F26J53) || defined(__18F27J53) || defined(__18F46J53) || defined(__18F47J53) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define BD_RAM = 0xd00 + +#elif defined(__18F65J50) || defined(__18F66J50) || defined(__18F66J55) || defined(__18F67J50) || defined(__18F85J50) || defined(__18F86J50) || defined(__18F66J55) || defined(__18F67J50) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define USE_ALT_ANCON +#define BD_RAM = 0x400 + +#elif defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) +#define PIC_24F + +#elif defined(__PIC24FJ32GB002__) || defined(__PIC24FJ32GB004__) || defined(__PIC24FJ64GB002__) || defined(__PIC24FJ64GB004__) +#define PIC_24F +#define PLLEN_REQD + +#elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(PIC24FJ256GB206__) || defined(PIC24FJ256GB210__) +#define PIC_24F +#define USE_ANS + +#elif defined(__PIC24FJ128DA106__) || defined(__PIC24FJ128DA110__) || defined(__PIC24FJ128DA206__) || defined(__PIC24FJ128DA210__) || defined(__PIC24FJ256DA106__) || defined(__PIC24FJ256DA110__) || defined(_PIC24FJ256DA206__) || defined(__PIC24FJ256DA210__) +#define PIC_24F +#define PLLEN_REQD +#define USE_ANS +#endif + +#if defined(__18F13K50) || defined(__18F14K50) //JTR add +#define MAX_CHIP_EP (8u) +#else +#define MAX_CHIP_EP (16u) +#endif + +/* COMMON PIC DEFINES TO BOTH PIC18 AND PIC24 */ + +#define UOWN 0x80 +#define DTS 0x40 +#define KEN 0x20 +#define INCDIS 0x10 +#define DTSEN 0x08 +#define BSTALL 0x04 +#define BC98 0x03 + + +#define USB_DIR_OUT 0 +#define USB_DIR_IN 1 +#define USB_PP_EVEN 0 +#define USB_PP_ODD 1 + + +/* PIC DEFINES SPECIFIC TO PIC18 */ + +#if defined(PIC_18F) + +#define MyProcessor // JTR check that a PIC is defined +#include + +#define USTAT_ODD_EVEN (2) // JTR may be required for ping-pong BD* calculations and are different from PIC18 to PIC24 +#define USTAT_ODD_EVEN_SHIFT (1) + +// JTR removed not used and the values??? +// #define USB_EP_INTERRUPT (0) +// #define USB_EP_BULK (1) +// #define USB_EP_ISOCHRONOUS (2) + +#define USB_UEP_EPSTALL (0x01) // JTR Note. to self. these values are different between the PIC18 and PIC24 +#define USB_UEP_EPINEN (0x02) +#define USB_UEP_EPOUTEN (0x04) +#define USB_UEP_EPCONDIS (0x08) +#define USB_UEP_EPHSHK (0x10) + +#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) +#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) +#define USB_EP_NONE (0x00) + +typedef unsigned char usb_uep_t; +#define USB_UEP ((usb_uep_t*) (&UEP0)) +#define USB_UEP0 UEP0 +#define USB_UEP1 UEP1 +#define USB_UEP2 UEP2 +#define USB_UEP3 UEP3 +#define USB_UEP4 UEP4 +#define USB_UEP5 UEP5 +#define USB_UEP6 UEP6 +#define USB_UEP7 UEP7 + +#ifndef PIC18FxxK +#define USB_UEP8 UEP8 +#define USB_UEP9 UEP9 +#define USB_UEP10 UEP10 +#define USB_UEP11 UEP11 +#define USB_UEP12 UEP12 +#define USB_UEP13 UEP13 +#define USB_UEP14 UEP14 +#define USB_UEP15 UEP15 +#endif + +/* Interrupt */ +#define USB_SOF 0x40 +#define USB_STALL 0x20 +#define USB_IDLE 0x10 +#define USB_TRN 0x08 +#define USB_ACTIV 0x04 +#define USB_UERR 0x02 +#define USB_URST 0x01 + +#define USB_RESET_FLAG UIRbits.URSTIF +#define USB_ERROR_FLAG UIRbits.UERRIF +#define USB_ACTIVITY_FLAG UIRbits.ACTVIF +#define USB_IDLE_FLAG UIRbits.IDLEIF +#define USB_STALL_FLAG UIRbits.STALLIF +#define USB_SOF_FLAG UIRbits.SOFIF +#define USB_TRANSACTION_FLAG UIRbits.TRNIF + +#define UsbInterruptFlags() (UIR) +#define UsbErrorInterruptFlags() (UEIR) + +#define TestGlobalUsbInterruptFlag() PIR2bits.USBIF +#define TestUsbTrfInterruptFlag() UIR (x) +#define ClearGlobalUsbInterruptFlag() PIR2bits.USBIF = 0 +#define ClearUsbInterruptFlag(x) UIR &= ~(x) +#define ClearAllUsbInterruptFlags() UIR = 0 +#define ClearUsbErrorInterruptFlag(x) UEIR &= ~(x) +#define ClearAllUsbErrorInterruptFlags() UEIR = 0 +#define DisableUsbInterrupts() PIE2bits.USBIE = 0 +#define DisableUsbInterrupt(x) UIE &= ~(x) +#define DisableAllUsbInterrupts() UIE = 0 +#define DisableUsbErrorInterrupt(x) UEIE &= ~(x) +#define DisableAllUsbErrorInterrupts() UEIE = 0 +#define EnableUsbInterrupts() PIE2bits.USBIE = 1 +#define EnableUsbInterrupt(x) UIE |= (x) +#define EnableAllUsbInterrupts() UIE = 0xFF +#define EnableUsbErrorInterrupt(x) UEIE |= (x) +#define EnableAllUsbErrorInterrupts() UEIE = 0xFF +#define EnableUSBHighInterrupts() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new +#define EnableUSBLowInterrupts() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new +// JTR TODO define for NO priority interrupt. + +/* UCON */ +#define ResetPPbuffers() do {UCONbits.PPBRST = 1; UCONbits.PPBRST=0;} while(0) +#define SingleEndedZeroIsSet() (UCONbits.SE0) +#define EnablePacketTransfer() UCONbits.PKTDIS = 0 +#define EnableUsb() UCONbits.USBEN = 1 +#define SignalResume() do {UCONbits.RESUME = 1; delay_ms(10); UCONbits.RESUME = 0;} while(0) +#define SuspendUsb() UCONbits.SUSPND = 1 +#define WakeupUsb() do {UCONbits.SUSPND = 0; while(USB_ACTIVITY_FLAG){USB_ACTIVITY_FLAG = 0;}} while(0) + +/* UADDR */ +#define SetUsbAddress(x) (UADDR = (x)) +#define GetUsbAddress() (UADDR) + +/* USTAT */ +// typedef unsigned char usb_status_t; +#define GetUsbTransaction() (USTAT) +#define USB_STAT2EP(x) ((x>>3)&0x0F) +#define USB_STAT2DIR(x) ((x>>2)&0x01) +#define USB_STAT2ADDR(x) ((x>>2)&0x1F) +#define USB_STAT2PPI(x) ((x>>1)&0x01) +#define DIRBIT 0x4 // JTR addition. Different between PIC18 and PIC24 +/* Hardware implementations */ + +#if defined USB_INTERNAL_PULLUPS +#define USB_UCFG_UPUEN_VALUE (1<<4) +#elif defined USB_EXTERNAL_PULLUPS +#define USB_UCFG_UPUEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_INTERNAL_TRANSCIEVER +#define USB_UCFG_UTRDIS_VALUE (0) +#elif defined USB_EXTERNAL_TRANSCIEVER +#define USB_UCFG_UTRDIS_VALUE (1<<3) +#else +#error "Neither internal nor external transciever defined" +#endif + +#if defined USB_FULL_SPEED_DEVICE +#define USB_UCFG_FSEN_VALUE (1<<2) +#elif defined USB_LOW_SPEED_DEVICE +#define USB_UCFG_FSEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_BUS_POWERED +#ifndef usb_low_power_request +/* Default low power mode is DUD */ +#define usb_low_power_request() Nop() +#endif +#ifndef usb_low_power_resume +#define usb_low_power_resume() Nop() +#endif +#elif defined USB_SELF_POWERED +#define usb_low_power_request() Nop() +#define usb_low_power_resume() Nop() +#else +#error "No source of device power defined" +#endif + +#ifndef USB_INTERNAL_VREG +#warning "Use of internal voltage regulator not defined. User must supply 3.3V on Vusb pin." +#endif + +//#define USB_DIR_OUT 0 +//#define USB_DIR_IN 1 +//#define USB_PP_EVEN 0 +//#define USB_PP_ODD 1 + +/* PingPong buffer descriptor table index calculations */ +#if USB_PP_BUF_MODE == 0 +#define USB_USTAT2BD(X) ( (X)/4 ) +#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) +#elif USB_PP_BUF_MODE == 1 +#error "USB_PP_BUF_MODE outside scope." +#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) +#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) +#elif USB_PP_BUF_MODE == 2 +#error "USB_PP_BUF_MODE outside scope." +#define USB_USTAT2BD(X) ( (X)/2 ) +#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) +#elif USB_PP_BUF_MODE == 3 +#error "USB_PP_BUF_MODE outside scope." + +#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) +#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) + + +#else +#error "USB_PP_BUF_MODE outside scope." +#endif + +#define USB_UCFG_REGISTER_VALUE ((USB_UCFG_UPUEN_VALUE) | \ + (USB_UCFG_UTRDIS_VALUE) | \ + (USB_UCFG_FSEN_VALUE) | \ + (USB_PP_BUF_MODE)) + +#define ConfigureUsbHardware() do {UCFG = USB_UCFG_REGISTER_VALUE;} while(0) + +#define ROM far rom +#define ROMPTR far rom +#define ARCH_memcpy memcpypgm2ram + +typedef struct BDENTRY { + unsigned char + /* struct { + unsigned BCH:2; + unsigned BSTALL:1; + unsigned DTSEN:1; + unsigned INCDIS:1; + unsigned KEN:1; + unsigned DTS:1; + unsigned UOWN:1; + }*/ + BDSTAT; + unsigned char BDCNT; + unsigned char *BDADDR; +} BDentry; +#endif // End of specific PIC18 defines. + + +/* PIC DEFINES SPECIFIC TO PIC24 FOLLOW */ + +#if defined(PIC_24F) + +#define MyProcessor +#include + +#define USTAT_ODD_EVEN (4) // JTR PIC24 fixup potentially ?? Only required when ping-pong buffering is enabled. +#define USTAT_ODD_EVEN_SHIFT (2) // JTR these are required for BD* calculations and are different for the PIC24 + +/* Bitmasks */ +#define USB_UEP_EPHSHK (0x01) +#define USB_UEP_EPSTALL (0x02) +#define USB_UEP_EPINEN (0x04) +#define USB_UEP_EPOUTEN (0x08) +#define USB_UEP_EPCONDIS (0x10) + +#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) +#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) +#define USB_EP_NONE (0x00) + +// JTR removed not used and the values?? +// #define USB_EP_INTERRUPT (0) +// #define USB_EP_BULK (1) +// #define USB_EP_ISOCHRONOUS (2) + +typedef unsigned int usb_uep_t; // JTR PIC24 fixup potentially ?? changed from char. +// See http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=150#p17703 + +// However the stack as modified by JTR does not actually use it. Then again it might as it is tested and works. + +#define USB_UEP ((usb_uep_t*)(&U1EP0)) +#define USB_UEP0 U1EP0 +#define USB_UEP1 U1EP1 +#define USB_UEP2 U1EP2 +#define USB_UEP3 U1EP3 +#define USB_UEP4 U1EP4 +#define USB_UEP5 U1EP5 +#define USB_UEP6 U1EP6 +#define USB_UEP7 U1EP7 +#define USB_UEP8 U1EP8 +#define USB_UEP9 U1EP9 +#define USB_UEP10 U1EP10 +#define USB_UEP11 U1EP11 +#define USB_UEP12 U1EP12 +#define USB_UEP13 U1EP13 +#define USB_UEP14 U1EP14 +#define USB_UEP15 U1EP15 + +/* Interrupt */ +#define USB_STALL 0x0080 +#define USB_ACTIV 0x0020 +#define USB_IDLE 0x0010 +#define USB_TRN 0x0008 +#define USB_SOF 0x0004 +#define USB_UERR 0x0002 +#define USB_URST 0x0001 + +#define USB_RESET_FLAG U1IRbits.URSTIF +#define USB_ERROR_FLAG U1IRbits.UERRIF +#define USB_ACTIVITY_FLAG U1IRbits.RESUMEIF +#define USB_IDLE_FLAG U1IRbits.IDLEIF +#define USB_STALL_FLAG U1IRbits.STALLIF +#define USB_SOF_FLAG U1IRbits.SOFIF +#define USB_TRANSACTION_FLAG U1IRbits.TRNIF + +#define UsbInterruptFlags() (U1IR) +#define UsbErrorInterruptFlags() (U1EIR) +#define TestGlobalUsbInterruptFlag() IFS5bits.USB1IF +#define ClearGlobalUsbInterruptFlag() IFS5bits.USB1IF = 0 +#define ClearUsbInterruptFlag(x) U1IR = x +#define ClearAllUsbInterruptFlags() U1IR = 0xFF +#define ClearUsbErrorInterruptFlag(x) U1EIR = x +#define ClearAllUsbErrorInterruptFlags() U1EIR = 0xFF +#define DisableUsbInterrupts() IEC5bits.USB1IE = 0 +#define DisableUsbInterrupt(x) U1IE &= ~(x) +#define DisableAllUsbInterrupts() U1IE = 0 +#define DisableUsbErrorInterrupt(x) U1EIE &= ~(x) +#define DisableAllUsbErrorInterrupts() U1EIE = 0 +#define TestUsbInterruptEnabled() IEC5bits.USB1IE + +#ifdef USB_INTERRUPT +#define EnableUsbInterrupts() IEC5bits.USB1IE=1 +#define EnableUsbInterrupt(x) U1IE |= (x) +#define EnableAllUsbInterrupts() U1IE = 0x00FF +#define EnableUsbErrorInterrupt(x) U1EIE |= (x) +#define EnableAllUsbErrorInterrupts() U1EIE = 0x00FF +#define EnableUSBHighInterrupts() +#else +#define EnableUsbInterrupts() +#define EnableUsbInterrupt(x) +#define EnableAllUsbInterrupts() +#define EnableUsbErrorInterrupt(x) +#define EnableAllUsbErrorInterrupts() +#define EnableUSBHighInterrupts() +#endif + +/* UCON */ +#define ResetPPbuffers() do {U1CONbits.PPBRST = 1; U1CONbits.PPBRST=0;} while(0) +#define SingleEndedZeroIsSet() (U1CONbits.SE0) +#define EnablePacketTransfer() U1CONbits.PKTDIS = 0 +#define EnableUsb() while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;} // U1CONbits.USBEN = 1 +#define SignalResume() do {U1CONbits.RESUME = 1; delay_ms(10); U1CONbits.RESUME = 0;} while(0) +#define SuspendUsb() U1PWRCbits.USUSPND = 1 +#define WakeupUsb() do {U1PWRCbits.USUSPND = 0; while(USB_ACTIVITY_FLAG){USB_ACTIVITY_FLAG = 0;}} while(0) + + +/* UADDR */ +#define SetUsbAddress(x) (U1ADDR = (x)) +#define GetUsbAddress() (U1ADDR) + +#define GetUsbTransaction() (U1STAT) +#define USB_STAT2EP(x) ((x>>4)&0x0F) //((x>>3)&0x0F) JTR PIC24 fixups +#define USB_STAT2DIR(x) ((x>>3)&0x01) //((x>>2)&0x01) "" +#define USB_STAT2ADDR(x) ((x>>3)&0x1F) //((x>>2)&0x1F) "" +#define USB_STAT2PPI(x) ((x>>2 &0x01) //((x>>1)&0x01) "" +#define DIRBIT 0x8 // JTR add PIC24 fixup as the mask is 0x4 for the PIC18 + + +/* Hardware implementations */ + +#if defined USB_INTERNAL_PULLUPS +#elif defined USB_EXTERNAL_PULLUPS +#define USB_U1OTGCON_UPUEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_INTERNAL_TRANSCIEVER +#define USB_U1CNFG2_UTRDIS_VALUE (0) +#elif defined USB_EXTERNAL_TRANSCIEVER +#define USB_U1CNFG2_UTRDIS_VALUE (1) +#else +#error "Neither internal nor external transciever defined" +#endif + +#if defined USB_FULL_SPEED_DEVICE +#if defined USB_INTERNAL_PULLUPS +#define USB_U1OTGCON_DPPULUP_VALUE (1<<7) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#else +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#endif +#elif defined USB_LOW_SPEED_DEVICE +#if defined USB_INTERNAL_PULLUPS +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (1<<6) +#else +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#endif +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_BUS_POWERED +#ifndef usb_low_power_request +/* Default low power mode is DUD */ +#define usb_low_power_request() Nop() +#endif +#ifndef usb_low_power_resume +#define usb_low_power_resume() Nop() +#endif +#elif defined USB_SELF_POWERED +#define usb_low_power_request() Nop() +#define usb_low_power_resume() Nop() +#else +#error "No source of device power defined" +#endif + +#ifndef USB_INTERNAL_VREG +#warning "Use of internal voltage regulator not implemented. User must supply 3.3V on Vusb pin." +#endif + +/* PingPong buffer descriptor table index calculations */ +#if USB_PP_BUF_MODE == 0 +#define USB_USTAT2BD(X) ( (X)/8 ) //JTR PIC24 fixups +#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) + +// JTR TODO these values may need to be changed for the PIC24 +//#elif USB_PP_BUF_MODE == 1 +//#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) +//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) +//#elif USB_PP_BUF_MODE == 2 +//#define USB_USTAT2BD(X) ( (X)/2 ) +//#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) +//#elif USB_PP_BUF_MODE == 3 +//#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) +//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) + +#else +#error "USB_PP_BUF_MODE outside scope." +#endif + +// JTR PIC24 fixups some may not required but are "more correct." The added /256 is required! +#define ConfigureUsbHardware() do { \ + U1CNFG1 = USB_PP_BUF_MODE; \ + U1CNFG2 = USB_U1CNFG2_UTRDIS_VALUE; \ + U1BDTP1 = (unsigned int) usb_bdt/256; \ + U1OTGCON = USB_U1OTGCON_DPPULUP_VALUE | \ + USB_U1OTGCON_DMPULUP_VALUE; \ + U1PWRCbits.USBPWR = 1; \ + } while(0) + +#define ROM __attribute__((space(auto_psv))) +#define ROMPTR +#define ARCH_memcpy memcpy + +typedef struct BDENTRY { + unsigned char BDCNT; // JTR PIC24 fixup Note that the endianness is swapped from the PIC18 + // This is probably what had people hitting their heads! + unsigned char BDSTAT; + unsigned char *BDADDR; +} BDentry; + +#endif + +#ifndef MyProcessor +#error "No PIC family defined" +#endif + +#endif diff --git a/BPv4-bootloader/firmware-v1/usb_lang.h b/BPv4-bootloader/firmware-v1/usb_lang.h index c9250a23..72d7e77d 100644 --- a/BPv4-bootloader/firmware-v1/usb_lang.h +++ b/BPv4-bootloader/firmware-v1/usb_lang.h @@ -1,121 +1,121 @@ -#ifndef __USB_LANG_H__ -#define __USB_LANG_H__ - -#define USB_LANGID_Chinese_Macau_SAR 0x1404 -#define USB_LANGID_Croatian 0x041a -#define USB_LANGID_Czech 0x0405 -#define USB_LANGID_Danish 0x0406 -#define USB_LANGID_Dutch_Netherlands 0x0413 -#define USB_LANGID_Dutch_Belgium 0x0813 -#define USB_LANGID_English_United_States 0x0409 -#define USB_LANGID_English_United_Kingdom 0x0809 -#define USB_LANGID_English_Australian 0x0c09 -#define USB_LANGID_English_Canadian 0x1009 -#define USB_LANGID_English_New_Zealand 0x1409 -#define USB_LANGID_English_Ireland 0x1809 -#define USB_LANGID_English_South_Africa 0x1c09 -#define USB_LANGID_English_Jamaica 0x2009 -#define USB_LANGID_English_Caribbean 0x2409 -#define USB_LANGID_English_Belize 0x2809 -#define USB_LANGID_English_Trinidad 0x2c09 -#define USB_LANGID_English_Zimbabwe 0x3009 -#define USB_LANGID_English_Philippines 0x3409 -#define USB_LANGID_Estonian 0x0425 -#define USB_LANGID_Faeroese 0x0438 -#define USB_LANGID_Farsi 0x0429 -#define USB_LANGID_Finnish 0x040b -#define USB_LANGID_French_Standard 0x040c -#define USB_LANGID_French_Belgian 0x080c -#define USB_LANGID_French_Canadian 0x0c0c -#define USB_LANGID_French_Switzerland 0x100c -#define USB_LANGID_French_Luxembourg 0x140c -#define USB_LANGID_French_Monaco 0x180c -#define USB_LANGID_Georgian 0x0437 -#define USB_LANGID_German_Standard 0x0407 -#define USB_LANGID_German_Switzerland 0x0807 -#define USB_LANGID_German_Austria 0x0c07 -#define USB_LANGID_German_Luxembourg 0x1007 -#define USB_LANGID_German_Liechtenstein 0x1407 -#define USB_LANGID_Greek 0x0408 -#define USB_LANGID_Gujarati 0x0447 -#define USB_LANGID_Hebrew 0x040d -#define USB_LANGID_Hindi 0x0439 -#define USB_LANGID_Hungarian 0x040e -#define USB_LANGID_Icelandic 0x040f -#define USB_LANGID_Indonesian 0x0421 -#define USB_LANGID_Italian_Standard 0x0410 -#define USB_LANGID_Italian_Switzerland 0x0810 -#define USB_LANGID_Japanese 0x0411 -#define USB_LANGID_Kannada 0x044b -#define USB_LANGID_Kashmiri_India 0x0860 -#define USB_LANGID_Kazakh 0x043f -#define USB_LANGID_Konkani 0x0457 -#define USB_LANGID_Korean 0x0412 -#define USB_LANGID_Korean_Johab 0x0812 -#define USB_LANGID_Latvian 0x0426 -#define USB_LANGID_Lithuanian 0x0427 -#define USB_LANGID_Lithuanian_Classic 0x0827 -#define USB_LANGID_Macedonian 0x042f -#define USB_LANGID_Malay_Malaysian 0x043e -#define USB_LANGID_Malay_Brunei_Darussalam 0x083e -#define USB_LANGID_Malayalam 0x044c -#define USB_LANGID_Manipuri 0x0458 -#define USB_LANGID_Marathi 0x044e -#define USB_LANGID_Nepali_India 0x0861 -#define USB_LANGID_Norwegian_Bokmal 0x0414 -#define USB_LANGID_Norwegian_Nynorsk 0x0814 -#define USB_LANGID_Oriya 0x0448 -#define USB_LANGID_Polish 0x0415 -#define USB_LANGID_Portuguese_Brazil 0x0416 -#define USB_LANGID_Portuguese_Standard 0x0816 -#define USB_LANGID_Punjabi 0x0446 -#define USB_LANGID_Romanian 0x0418 -#define USB_LANGID_Russian 0x0419 -#define USB_LANGID_Sanskrit 0x044f -#define USB_LANGID_Serbian_Cyrillic 0x0c1a -#define USB_LANGID_Serbian_Latin 0x081a -#define USB_LANGID_Sindhi 0x0459 -#define USB_LANGID_Slovak 0x041b -#define USB_LANGID_Slovenian 0x0424 -#define USB_LANGID_Spanish_Traditional_Sort 0x040a -#define USB_LANGID_Spanish_Mexican 0x080a -#define USB_LANGID_Spanish_Modern_Sort 0x0c0a -#define USB_LANGID_Spanish_Guatemala 0x100a -#define USB_LANGID_Spanish_Costa_Rica 0x140a -#define USB_LANGID_Spanish_Panama 0x180a -#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a -#define USB_LANGID_Spanish_Venezuela 0x200a -#define USB_LANGID_Spanish_Colombia 0x240a -#define USB_LANGID_Spanish_Peru 0x280a -#define USB_LANGID_Spanish_Argentina 0x2c0a -#define USB_LANGID_Spanish_Ecuador 0x300a -#define USB_LANGID_Spanish_Chile 0x340a -#define USB_LANGID_Spanish_Uruguay 0x380a -#define USB_LANGID_Spanish_Paraguay 0x3c0a -#define USB_LANGID_Spanish_Bolivia 0x400a -#define USB_LANGID_Spanish_El_Salvador 0x440a -#define USB_LANGID_Spanish_Honduras 0x480a -#define USB_LANGID_Spanish_Nicaragua 0x4c0a -#define USB_LANGID_Spanish_Puerto_Rico 0x500a -#define USB_LANGID_Sutu 0x0430 -#define USB_LANGID_Swahili_Kenya 0x0441 -#define USB_LANGID_Swedish 0x041d -#define USB_LANGID_Swedish_Finland 0x081d -#define USB_LANGID_Tamil 0x0449 -#define USB_LANGID_Tatar_Tatarstan 0x0444 -#define USB_LANGID_Telugu 0x044a -#define USB_LANGID_Thai 0x041e -#define USB_LANGID_Turkish 0x041f -#define USB_LANGID_Ukrainian 0x0422 -#define USB_LANGID_Urdu_Pakistan 0x0420 -#define USB_LANGID_Urdu_India 0x0820 -#define USB_LANGID_Uzbek_Latin 0x0443 -#define USB_LANGID_Uzbek_Cyrillic 0x0843 -#define USB_LANGID_Vietnamese 0x042a -#define USB_LANGID_HID_UDD 0x04ff -#define USB_LANGID_HID_Vendor1 0xf0ff -#define USB_LANGID_HID_Vendor2 0xf4ff -#define USB_LANGID_HID_Vendor3 0xf8ff -#define USB_LANGID_HID_Vendor4 0xfcff - -#endif /*__USB_LANG_H__*/ +#ifndef __USB_LANG_H__ +#define __USB_LANG_H__ + +#define USB_LANGID_Chinese_Macau_SAR 0x1404 +#define USB_LANGID_Croatian 0x041a +#define USB_LANGID_Czech 0x0405 +#define USB_LANGID_Danish 0x0406 +#define USB_LANGID_Dutch_Netherlands 0x0413 +#define USB_LANGID_Dutch_Belgium 0x0813 +#define USB_LANGID_English_United_States 0x0409 +#define USB_LANGID_English_United_Kingdom 0x0809 +#define USB_LANGID_English_Australian 0x0c09 +#define USB_LANGID_English_Canadian 0x1009 +#define USB_LANGID_English_New_Zealand 0x1409 +#define USB_LANGID_English_Ireland 0x1809 +#define USB_LANGID_English_South_Africa 0x1c09 +#define USB_LANGID_English_Jamaica 0x2009 +#define USB_LANGID_English_Caribbean 0x2409 +#define USB_LANGID_English_Belize 0x2809 +#define USB_LANGID_English_Trinidad 0x2c09 +#define USB_LANGID_English_Zimbabwe 0x3009 +#define USB_LANGID_English_Philippines 0x3409 +#define USB_LANGID_Estonian 0x0425 +#define USB_LANGID_Faeroese 0x0438 +#define USB_LANGID_Farsi 0x0429 +#define USB_LANGID_Finnish 0x040b +#define USB_LANGID_French_Standard 0x040c +#define USB_LANGID_French_Belgian 0x080c +#define USB_LANGID_French_Canadian 0x0c0c +#define USB_LANGID_French_Switzerland 0x100c +#define USB_LANGID_French_Luxembourg 0x140c +#define USB_LANGID_French_Monaco 0x180c +#define USB_LANGID_Georgian 0x0437 +#define USB_LANGID_German_Standard 0x0407 +#define USB_LANGID_German_Switzerland 0x0807 +#define USB_LANGID_German_Austria 0x0c07 +#define USB_LANGID_German_Luxembourg 0x1007 +#define USB_LANGID_German_Liechtenstein 0x1407 +#define USB_LANGID_Greek 0x0408 +#define USB_LANGID_Gujarati 0x0447 +#define USB_LANGID_Hebrew 0x040d +#define USB_LANGID_Hindi 0x0439 +#define USB_LANGID_Hungarian 0x040e +#define USB_LANGID_Icelandic 0x040f +#define USB_LANGID_Indonesian 0x0421 +#define USB_LANGID_Italian_Standard 0x0410 +#define USB_LANGID_Italian_Switzerland 0x0810 +#define USB_LANGID_Japanese 0x0411 +#define USB_LANGID_Kannada 0x044b +#define USB_LANGID_Kashmiri_India 0x0860 +#define USB_LANGID_Kazakh 0x043f +#define USB_LANGID_Konkani 0x0457 +#define USB_LANGID_Korean 0x0412 +#define USB_LANGID_Korean_Johab 0x0812 +#define USB_LANGID_Latvian 0x0426 +#define USB_LANGID_Lithuanian 0x0427 +#define USB_LANGID_Lithuanian_Classic 0x0827 +#define USB_LANGID_Macedonian 0x042f +#define USB_LANGID_Malay_Malaysian 0x043e +#define USB_LANGID_Malay_Brunei_Darussalam 0x083e +#define USB_LANGID_Malayalam 0x044c +#define USB_LANGID_Manipuri 0x0458 +#define USB_LANGID_Marathi 0x044e +#define USB_LANGID_Nepali_India 0x0861 +#define USB_LANGID_Norwegian_Bokmal 0x0414 +#define USB_LANGID_Norwegian_Nynorsk 0x0814 +#define USB_LANGID_Oriya 0x0448 +#define USB_LANGID_Polish 0x0415 +#define USB_LANGID_Portuguese_Brazil 0x0416 +#define USB_LANGID_Portuguese_Standard 0x0816 +#define USB_LANGID_Punjabi 0x0446 +#define USB_LANGID_Romanian 0x0418 +#define USB_LANGID_Russian 0x0419 +#define USB_LANGID_Sanskrit 0x044f +#define USB_LANGID_Serbian_Cyrillic 0x0c1a +#define USB_LANGID_Serbian_Latin 0x081a +#define USB_LANGID_Sindhi 0x0459 +#define USB_LANGID_Slovak 0x041b +#define USB_LANGID_Slovenian 0x0424 +#define USB_LANGID_Spanish_Traditional_Sort 0x040a +#define USB_LANGID_Spanish_Mexican 0x080a +#define USB_LANGID_Spanish_Modern_Sort 0x0c0a +#define USB_LANGID_Spanish_Guatemala 0x100a +#define USB_LANGID_Spanish_Costa_Rica 0x140a +#define USB_LANGID_Spanish_Panama 0x180a +#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a +#define USB_LANGID_Spanish_Venezuela 0x200a +#define USB_LANGID_Spanish_Colombia 0x240a +#define USB_LANGID_Spanish_Peru 0x280a +#define USB_LANGID_Spanish_Argentina 0x2c0a +#define USB_LANGID_Spanish_Ecuador 0x300a +#define USB_LANGID_Spanish_Chile 0x340a +#define USB_LANGID_Spanish_Uruguay 0x380a +#define USB_LANGID_Spanish_Paraguay 0x3c0a +#define USB_LANGID_Spanish_Bolivia 0x400a +#define USB_LANGID_Spanish_El_Salvador 0x440a +#define USB_LANGID_Spanish_Honduras 0x480a +#define USB_LANGID_Spanish_Nicaragua 0x4c0a +#define USB_LANGID_Spanish_Puerto_Rico 0x500a +#define USB_LANGID_Sutu 0x0430 +#define USB_LANGID_Swahili_Kenya 0x0441 +#define USB_LANGID_Swedish 0x041d +#define USB_LANGID_Swedish_Finland 0x081d +#define USB_LANGID_Tamil 0x0449 +#define USB_LANGID_Tatar_Tatarstan 0x0444 +#define USB_LANGID_Telugu 0x044a +#define USB_LANGID_Thai 0x041e +#define USB_LANGID_Turkish 0x041f +#define USB_LANGID_Ukrainian 0x0422 +#define USB_LANGID_Urdu_Pakistan 0x0420 +#define USB_LANGID_Urdu_India 0x0820 +#define USB_LANGID_Uzbek_Latin 0x0443 +#define USB_LANGID_Uzbek_Cyrillic 0x0843 +#define USB_LANGID_Vietnamese 0x042a +#define USB_LANGID_HID_UDD 0x04ff +#define USB_LANGID_HID_Vendor1 0xf0ff +#define USB_LANGID_HID_Vendor2 0xf4ff +#define USB_LANGID_HID_Vendor3 0xf8ff +#define USB_LANGID_HID_Vendor4 0xfcff + +#endif /*__USB_LANG_H__*/ diff --git a/BPv4-bootloader/firmware-v1/usb_stack.c b/BPv4-bootloader/firmware-v1/usb_stack.c index df33c346..77eb5b16 100644 --- a/BPv4-bootloader/firmware-v1/usb_stack.c +++ b/BPv4-bootloader/firmware-v1/usb_stack.c @@ -1,725 +1,725 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - -Influence and inspiration taken from http://pe.ece.olin.edu/ece/projects.html - */ - -// JTR v0.1d - -#include "globals.h" // Compiler STD etc. Not function related. - -#if USB_EP0_BUFFER_SIZE == 8u -#elif USB_EP0_BUFFER_SIZE == 16u -#elif USB_EP0_BUFFER_SIZE == 32u -#elif USB_EP0_BUFFER_SIZE == 64u -#else -#error "USB_EP0_BUFFER_SIZE needs to be 8, 16, 32 or 64 bytes" -#endif - - -ROMPTR const unsigned char *usb_device_descriptor; -ROMPTR const unsigned char *usb_config_descriptor; -ROMPTR const unsigned char *usb_string_descriptor; -int usb_num_string_descriptors; - -usb_handler_t sof_handler; -usb_handler_t class_setup_handler, vendor_setup_handler; - -usb_ep_t endpoints[MAX_CHIP_EP]; // JTR change. MAX_CHIP_EP is the number of hardware endpoints on the silicon. See picusb.h - -// JTR comments. This below goes part way to ridding us of the need for a linker script (PIC18). -// It floats the EP0 buffers to occupy space immediately behind the buffer descriptors. -// However there are problems. It only works for the EP0 buffers because they are the only -// buffers defined in this block. It is not possible to do arithmatic within a #pragma -// therefore there seems to be no way to cause EPs in other blocks to float. -// All the buffers would need to be defined here and this would break the universal nature -// of this module. As such efforts to create a custom linker script free stack have been -// put on hold for now but can be revisited at any stage. -// Please see http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=120#p17401 - - -#pragma udata usb_bdt -BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED] __attribute__((aligned(512))); // JTR changed index from 32 to variable TODO: Dynamic allocation reflecting number of used endpoints. (How to do counting in preprocessor?) -#if USB_PP_BUF_MODE == 0 -unsigned char usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; -unsigned char usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; -#else -#error "Ping pong buffer not implemented yet!" -#endif - - -#pragma udata -unsigned int usb_device_status; -unsigned int usb_current_cfg; -volatile unsigned char usb_device_state; -unsigned char usb_addr_pending; -usb_status_t trn_status; // Global since it is needed everywere -BDentry *bdp, *rbdp; // Dito - -const unsigned char *usb_desc_ptr; -size_t usb_desc_len; - -/* Forward Reference Prototypes */ -void usb_handle_error(void); -void usb_handle_reset(void); -void usb_handle_transaction(void); -void usb_handle_setup(void); -void usb_handle_out(void); -void usb_handle_in(void); -void usb_handle_StandardDeviceRequest(BDentry*); -void usb_handle_StandardInterfaceRequest(BDentry*); -void usb_handle_StandardEndpointRequest(BDentry*); -//void usb_handle_ClassRequest( void ); -//void usb_handle_VendorRequest( void ); -void usb_RequestError(void); - -void usb_ack_dat1(BDentry *rbdp, int bdcnt); // JTR added -void usb_set_address(void); -void usb_send_descriptor(void); -void user_configured_init(void); //JTR added.Non EP0 endpoints are arm after USB device enters CONFIGURED STATE. This is a CALLBACK function. -BYTE FAST_usb_handler(void); // JTR2 added fast USB service. Pops non EP0 transfer flags from FIFO. Sevices RESET and SETUP - -void usb_init(ROMPTR const unsigned char *device_descriptor, - ROMPTR const unsigned char *config_descriptor, - ROMPTR const unsigned char *string_descriptor, - int num_string_descriptors) { - int i; - - - usb_device_descriptor = device_descriptor; - usb_config_descriptor = config_descriptor; - usb_string_descriptor = string_descriptor; - usb_num_string_descriptors = num_string_descriptors; - - SetUsbAddress(0); // JTR added here. Probably not required though - ResetPPbuffers(); - DisableUsbInterrupts(); - DisableAllUsbInterrupts(); - ClearAllUsbErrorInterruptFlags(); - ClearAllUsbInterruptFlags(); - ConfigureUsbHardware(); - - sof_handler = NULL; - class_setup_handler = NULL; - vendor_setup_handler = NULL; - - for (i = 0; i < MAX_CHIP_EP; i++) { - endpoints[i].out_handler = NULL; - endpoints[i].in_handler = NULL; - } - // Register ep0 - no handlers - - usb_unset_in_handler(0); - usb_unset_out_handler(0); - - // JTR All UEPx SPRs are hard coded. I cannot see much point of tables or indexing for these. - // On the PIC16C765 such indexing was required for the STALL bit but now the STALL feature - // is in the Buffer descriptor table not in the UEPx SPRs. - // See changes to "USB_REQUEST_GET_STATUS" - - // USB_UEP[0] = endpoints[0].type; - - USB_UEP0 = USB_EP_CONTROL; - /* Configure endpoints TODO: Only ep0 ? */ - // JTR Right! code for other end points snipped...At this point we are only setting up EP0 - -#ifdef USB_SELF_POWERED - usb_device_status = 0x0001; -#else - usb_device_status = 0x0000; -#endif - usb_device_state = 0x00; // JTR added flag byte for enumeration state - usb_current_cfg = 0; // JTR formally usb_configured - usb_addr_pending = 0x00; - -#if USB_PP_BUF_MODE == 0 - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS; // Set DTS => First packet inverts, ie. is Data0 -#else - // TODO: Implement Ping-Pong buffering setup. -#error "PP Mode not implemented yet" -#endif -} - -void usb_start(void) { - EnableUsb(); // Enable USB-hardware - //uLedOn(); - while (SingleEndedZeroIsSet()); // Busywait for initial power-up - uLedOn(); - -#ifdef USB_INTERRUPT - // EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_SOF + USB_UERR + USB_URST); //We don't enable ACTIV unless we are going into suspend mode. - EnableAllUsbErrorInterrupts(); - EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST); - EnableUSBHighInterrupts(); -#endif -} - -void usb_handler(void) { - if (USB_RESET_FLAG) { - usb_handle_reset(); - ClearUsbInterruptFlag(USB_URST); - } else if (USB_ERROR_FLAG) { - usb_handle_error(); - ClearUsbInterruptFlag(USB_UERR); - } else if (USB_ACTIVITY_FLAG) { - /* Activity - unsuspend */ - //WakeupUsb(); - ClearUsbInterruptFlag(USB_ACTIV); - } else if (USB_IDLE_FLAG) { - /* Idle - suspend */ - //SuspendUsb(); - //usb_low_power_request(); - ClearUsbInterruptFlag(USB_IDLE); - } else if (USB_STALL_FLAG) { - /* Stall detected - * Not sure when this interrupt fires - * as the host never should send out a stall. - * Perhaps as a respons to our own stalls? - * For now just ignore it. */ - ClearUsbInterruptFlag(USB_STALL); - } else if (USB_SOF_FLAG) { - /* Start-of-frame */ - //if (sof_handler) sof_handler(); - { - ClearUsbInterruptFlag(USB_SOF); - } - } else { - // process all pending transactions - while (USB_TRANSACTION_FLAG) { - usb_handle_transaction(); - ClearUsbInterruptFlag(USB_TRN); - } // Side effect: advance USTAT Fifo - } -} - -void usb_handle_error(void) { - /* No errorhandler for now, just clear offending flag*/ - ClearAllUsbErrorInterruptFlags(); -} - -void usb_handle_reset(void) { - int i; - mLedToggle(); -#ifdef USB_SELF_POWERED // JTR see note about this in usb_init() - usb_device_status = 0x0001; -#else - usb_device_status = 0x0000; -#endif - - do { - ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. - usb_current_cfg = 0; - usb_device_state = 0x00; // This creates the requied 6 cycle delay for TRNF to reassert. - usb_addr_pending = 0x00; - } while (USB_TRANSACTION_FLAG); - - // for (i=0; i < MAX_CHIP_EP; i++ ) // JTR this loop seems to work proving that the USB_UEP ptr is now correct. - // { - // USB_UEP[i] = 0; // JTR Char on PIC18 and int ptr on PIC24 - // } - USB_UEP0 = 0; // Disable all endpoints - USB_UEP1 = 0; - USB_UEP2 = 0; - USB_UEP3 = 0; - USB_UEP4 = 0; - USB_UEP5 = 0; - USB_UEP6 = 0; - USB_UEP7 = 0; -#if MAX_CHIP_EP > 8 // JTR alteration - USB_UEP8 = 0; - USB_UEP9 = 0; - USB_UEP10 = 0; - USB_UEP11 = 0; - USB_UEP12 = 0; - USB_UEP13 = 0; - USB_UEP14 = 0; - USB_UEP15 = 0; - -#endif - SetUsbAddress(0); // After reset we don't have an address - ClearAllUsbInterruptFlags(); - ClearAllUsbErrorInterruptFlags(); - - // JTR added. Clear all BD STAT registers so that they are owned by the CPU and it is safe to change them. - - for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i += 4) { - usb_bdt[i].BDSTAT = 0; - } - // USB_UEP[0] = endpoints[0].type; // JTR removed all such indexing on UEPx - USB_UEP0 = USB_EP_CONTROL; // JTR hard coded instead. - - /* Configure buffer descriptors */ - -#if USB_PP_BUF_MODE == 0 - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; //endpoints[0].buffer_size; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS; // Set DTS => First packet inverts, ie. is Data0 - -#else - // TODO: Implement Ping-Pong buffering setup. -#error "PP Mode not implemented yet" -#endif - EnablePacketTransfer(); -} - -void usb_handle_transaction(void) { - uLedToggle(); - trn_status = GetUsbTransaction(); - bdp = &usb_bdt[USB_USTAT2BD(trn_status)]; - // JTR PIC24 fixup Added DIRBIT as it is different for PIC24F ( and different again for the PIC24E/dsPIC33E) - // the USB_USTAT2BD macro is also altered to shift the result the correct number of places. - - rbdp = &usb_bdt[USB_USTAT2BD(trn_status | DIRBIT)]; // All replies in IN direction - - switch (bdp->BDSTAT & USB_TOKEN_Mask) { - case USB_TOKEN_SETUP: - usb_handle_setup(); - break; - case USB_TOKEN_OUT: - usb_handle_out(); - break; - case USB_TOKEN_IN: - usb_handle_in(); - break; - //default: - /* Default case of unknown TOKEN - discard */ - } -} - -void usb_handle_setup(void) { - rbdp->BDSTAT = DTSEN; // Reclaim reply buffer - switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) { - case USB_bmRequestType_Standard: - switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) { - case USB_bmRequestType_Device: - usb_handle_StandardDeviceRequest(bdp); - break; - case USB_bmRequestType_Interface: - usb_handle_StandardInterfaceRequest(bdp); - break; - case USB_bmRequestType_Endpoint: - usb_handle_StandardEndpointRequest(bdp); - break; - default: - usb_RequestError(); - } - break; - case USB_bmRequestType_Class: - if (class_setup_handler) class_setup_handler(); - break; - case USB_bmRequestType_Vendor: - if (vendor_setup_handler) class_setup_handler(); - break; - default: - usb_RequestError(); - } - /* Prepare endpoint for new reception */ - - bdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size of EP0, should always be ep0? (JTR in practice YES) - - // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)? - // note that this is not an entirely robust way of doing things. See the microchip stack for - // further comments and a better system as this does not account for errors and retries. - - // JTR Breakdown of what is happening here FYI. - - //bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && - // JTR meaning. If transfer was a CONTROL OUT then B7 (USB_bmRequestType_PhaseMask) of bmRequestType is '0' - - // && - - //bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh]))? UOWN + DTS + DTSEN : UOWN + DTSEN; - // If there is a CONTROL OUT DATA PACKET to follow then its count in (int) wLength will be != 0 - // When both conditions are true then set for DAT1 (UOWN + DTS + DTSEN) - // else set for DAT0 (UOWN + DTSEN) - - // See USB 2.0 spec 8.5.3 - - bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && - (bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN; - - // JTR Note. For rbdp after a setup packet, the standard or class request handler will force the correct DTS state - // JTR Note. that CONTROL IN and OUT DATA packet transfers do not come back here and there is no - // univesal way and place of setting up EP0 after these DATA transfers in this stack. - - // JTR Note. that there is a PIC18 silicon errata issue that this does not address by being here. See DS80220F-page 6 - EnablePacketTransfer(); -} - -// JTR comment on the standard device requests handler. -// This does not address that some requests need to be -// handled differently depending on what USB defined -// "STATE" of enumeration we are at. In fact this stack -// does not even track any of the different states. -// except that I have added basic tracking to determine -// if the device is configured or not. This is a minimum. -// Later the stack can be improved. - -void usb_handle_StandardDeviceRequest(BDentry *bdp) { - unsigned char *packet = bdp->BDADDR; - int i; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - rbdp->BDADDR[0] = usb_device_status & 0xFF; - rbdp->BDADDR[1] = usb_device_status >> 8; - - // JTR I added usb_ack_dat1() simply so that the handling - // of STANDARD and CLASS requests (in cdc.c) were consistant - // rather than have the same thing done in two different ways. - - usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK - break; - case USB_REQUEST_CLEAR_FEATURE: - if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) - usb_device_status &= ~0x0002; - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_FEATURE: - if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) - usb_device_status |= 0x0002; - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_ADDRESS: - if (0x00u == packet[USB_wValueHigh] && 0x7Fu >= packet[USB_wValue]) { - usb_addr_pending = packet[USB_wValue]; - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - usb_set_in_handler(0, usb_set_address); - } else - usb_RequestError(); - break; - - - case USB_REQUEST_GET_DESCRIPTOR: - switch (packet[USB_bDescriptorType]) { - case USB_DEVICE_DESCRIPTOR_TYPE: - usb_desc_ptr = usb_device_descriptor; - usb_desc_len = usb_device_descriptor[0]; - if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_desc_ptr[0])) - usb_desc_len = packet[USB_wLength]; - break; - case USB_CONFIGURATION_DESCRIPTOR_TYPE: - if (packet[USB_bDescriptorIndex] >= usb_device_descriptor[17]) // TODO: remove magic - usb_RequestError(); - usb_desc_ptr = usb_config_descriptor; - usb_desc_len = usb_desc_ptr[2] + usb_desc_ptr[3] * 256; - for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration - usb_desc_ptr += usb_desc_len; - usb_desc_len = usb_desc_ptr[2] + usb_desc_ptr[3] * 256; - } - if ((packet[USB_wLengthHigh] < usb_desc_ptr[3]) || - (packet[USB_wLengthHigh] == usb_desc_ptr[3] && packet[USB_wLength] < usb_desc_ptr[2])) - usb_desc_len = packet[USB_wLength] + packet[USB_wLengthHigh] * 256; - break; - case USB_STRING_DESCRIPTOR_TYPE: - // TODO: Handle language request. For now return standard language. - if (packet[USB_bDescriptorIndex] >= usb_num_string_descriptors) - usb_RequestError(); - usb_desc_ptr = usb_string_descriptor; - usb_desc_len = usb_desc_ptr[0]; - for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration - usb_desc_ptr += usb_desc_len; - usb_desc_len = usb_desc_ptr[0]; - } - if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_desc_ptr[0])) - usb_desc_len = packet[USB_wLength]; - break; - case USB_INTERFACE_DESCRIPTOR_TYPE: - case USB_ENDPOINT_DESCRIPTOR_TYPE: - default: - usb_RequestError(); - } - usb_send_descriptor(); // Send first part of packet right away - usb_set_in_handler(0, usb_send_descriptor); - break; - case USB_REQUEST_GET_CONFIGURATION: - rbdp->BDADDR[0] = usb_current_cfg; - usb_ack_dat1(rbdp, 1); // JTR common addition for STD and CLASS ACK - // rbdp->BDCNT = 1; - // rbdp->BDSTAT = UOWN + DTS + DTSEN; - break; - - case USB_REQUEST_SET_CONFIGURATION: - if (USB_NUM_CONFIGURATIONS >= packet[USB_wValue]) { - // TODO: Support multiple configurations - /* Configure endpoints (USB_UEPn - registers) */ - usb_current_cfg = packet[USB_wValue]; - if (usb_current_cfg != 0) { - - // JTR user_configured_init major addition. This is a CALLBACK to the USER when the device is enumerated. - // This is when we setup non EP0 endpoints. - // TODO: This really could be a function pointer - - usb_device_state = CONFIGURED_STATE; - user_configured_init(); - } else { - usb_device_state = ADDRESS_STATE; - } - - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - - } else - usb_RequestError(); - break; - - case USB_REQUEST_SET_DESCRIPTOR: - default: - usb_RequestError(); - } -} - -void usb_handle_StandardInterfaceRequest(BDentry *bdp) { - unsigned char *packet = bdp->BDADDR; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - rbdp->BDADDR[0] = 0x00; - rbdp->BDADDR[1] = 0x00; - usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK - // rbdp->BDCNT = 2; - // rbdp->BDSTAT = UOWN + DTS + DTSEN; - break; - case USB_REQUEST_GET_INTERFACE: - if (USB_NUM_INTERFACES > packet[USB_bInterface]) { - // TODO: Implement alternative interfaces, or move responsibility to class/vendor functions. - rbdp->BDADDR[0] = 0; - usb_ack_dat1(rbdp, 1); // JTR common addition for STD and CLASS ACK - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_INTERFACE: - if (USB_NUM_INTERFACES > packet[USB_bInterface] && 0u == packet[USB_wValue]) { - // TODO: Implement alternative interfaces... - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - } else - usb_RequestError(); - break; - case USB_REQUEST_CLEAR_FEATURE: // JTR N/A for interface - case USB_REQUEST_SET_FEATURE: // This is correct and finished code. - default: - usb_RequestError(); - } -} - -void usb_handle_StandardEndpointRequest(BDentry *bdp) { - unsigned char *packet; - unsigned char epnum; - unsigned char dir; - BDentry *epbd; - - packet = bdp->BDADDR; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - rbdp->BDADDR[0] = 0x00; // Assume no stall - rbdp->BDADDR[1] = 0x00; // Same for stall or not - epnum = packet[USB_wIndex] & 0x0F; - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - if (epbd->BDSTAT &= ~BSTALL) - rbdp->BDADDR[0] = 0x01; // EVEN BD is stall flag set? - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - if (epbd->BDSTAT &= ~BSTALL) - rbdp->BDADDR[0] = 0x01; // ODD BD is stall flag set? - usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK - break; - - case USB_REQUEST_CLEAR_FEATURE: - epnum = packet[USB_wIndex] & 0x0F; - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - epbd->BDSTAT &= ~BSTALL; - if (dir) epbd->BDSTAT |= DTS; // JTR added IN EP set DTS as it will be toggled to zero next transfer - if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added - - // JTR this pointless ATM. If ping-pong is enabled then you need to track PPBI - // and set up ODD and EVEN BDs in respect to this. See complicated system in - // microchip stack >= 2.8 - - // epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - // epbd->BDSTAT &= ~BSTALL; - // if (dir) epbd->BDSTAT |= DTS; // JTR added - // if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added - - - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - // rbdp->BDCNT = 0; - // rbdp->BDSTAT = UOWN + DTS + DTSEN; - break; - - - case USB_REQUEST_SET_FEATURE: - epnum = packet[USB_wIndex] & 0x0F; - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - epbd->BDSTAT |= BSTALL; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - epbd->BDSTAT |= BSTALL; - usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK - // rbdp->BDCNT = 0; - // rbdp->BDSTAT = UOWN + DTS + DTSEN; - break; - case USB_REQUEST_SYNCH_FRAME: - default: - usb_RequestError(); - } -} - -void usb_handle_in(void) { - if (endpoints[USB_STAT2EP(trn_status)].in_handler) { - endpoints[USB_STAT2EP(trn_status)].in_handler(); - } -} - -void usb_handle_out(void) { - if (endpoints[USB_STAT2EP(trn_status)].out_handler) { - endpoints[USB_STAT2EP(trn_status)].out_handler(); - } -} - -void usb_register_sof_handler(usb_handler_t handler) { - sof_handler = handler; -} - -void usb_register_class_setup_handler(usb_handler_t handler) { - class_setup_handler = handler; -} - -void usb_register_vendor_setup_handler(usb_handler_t handler) { - vendor_setup_handler = handler; -} - -void usb_set_in_handler(int ep, usb_handler_t in_handler) { - endpoints[ep].in_handler = in_handler; -} - -void usb_set_out_handler(int ep, usb_handler_t out_handler) { - endpoints[ep].out_handler = out_handler; -} - -// JTR not actually using these two helper functions below -// but they are still useful and could be put back to use -// as they aught to result in smaller code. For now I am -// using the usb_ack_dat1() as it more correctly forces a -// DAT1 transfer and passes a varible for the transfer count. - -void usb_ack(BDentry *bd) { - bd->BDSTAT = ((bd->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // TODO: Accomodate for >=256 byte buffers -} - -void usb_ack_zero(BDentry *bd) { - bd->BDCNT = 0; - bd->BDSTAT = ((bd->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - -} -// JTR New added helper function use extensively by -// the standard and class request handlers. All status -// IN packets are DAT1 as is the first DATA packet of -// a IN transfer. Currently with this CDC stack the -// only IN DATA transfers that are > 8 bytes is the -// descriptor transfer and these are transfered in -// usb_send_descriptor() - -void usb_ack_dat1(BDentry *rbdp, int bdcnt) { - rbdp->BDCNT = (bdcnt & 0xFF); - rbdp->BDSTAT = (DTS | UOWN | DTSEN); // | ((bdcnt & 0x300) >> 8)); -} - -void usb_RequestError(void) { - - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDCNT = USB_EP0_BUFFER_SIZE; - - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; - - // JTR TODO: Should also kill the IN and OUT handlers? - -} - -void usb_set_address(void) { - // if (0x00u <= usb_addr_pending && 0x80u > usb_addr_pending) { // JTR changed. The USB specs allow for a device to be de-addressed - if (0x80u > usb_addr_pending) { - SetUsbAddress(usb_addr_pending); - usb_addr_pending = 0xFF; - } - // JTR TODO: If we are going to bounds check the pending address - // (again) and it is out of bounds we should return a - // protocol stall here. - usb_unset_in_handler(0); // Unregister handler -} - -void usb_send_descriptor(void) { - - // JTR comment While this function is being used only for the descriptors - // there is no reason why it cannot be a generic CONTROL IN transfer handler. - // To that ends it needs a flag to denote if it is copying from a RAM or ROM - // pointer - - unsigned int i; - // JTR N/A BDentry *bd; - size_t packet_len; - if (usb_desc_len) { - packet_len = (usb_desc_len < USB_EP0_BUFFER_SIZE) ? usb_desc_len : USB_EP0_BUFFER_SIZE; // JTR changed from MAX_BUFFER_SIZE - for (i = 0; i < packet_len; i++) { - rbdp->BDADDR[i] = usb_desc_ptr[i]; - } - } else { - - // JTR comment. So the transfer is now completed. - packet_len = 0; // Send a last ack - usb_unset_in_handler(0); - } - - rbdp->BDCNT = (unsigned char) packet_len; - rbdp->BDSTAT = ((rbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // Packet length always less then 256 on endpoint 0 - - usb_desc_ptr += packet_len; - usb_desc_len -= packet_len; -} - -BYTE FAST_usb_handler(void) { - if (TestGlobalUsbInterruptFlag()) { - if (USB_RESET_FLAG) { - usb_handle_reset(); - ClearUsbInterruptFlag(USB_URST); - return 0xFF; - } - if (USB_TRANSACTION_FLAG) { - trn_status = GetUsbTransaction(); - if (USB_STAT2EP(trn_status)) { - usb_handle_transaction(); - ClearUsbInterruptFlag(USB_TRN); // non-EP0 only - return 0xFF; - } else { - ClearUsbInterruptFlag(USB_TRN); // non-EP0 only - } - } - ClearGlobalUsbInterruptFlag(); - } - return 0; -} - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + +Influence and inspiration taken from http://pe.ece.olin.edu/ece/projects.html + */ + +// JTR v0.1d + +#include "globals.h" // Compiler STD etc. Not function related. + +#if USB_EP0_BUFFER_SIZE == 8u +#elif USB_EP0_BUFFER_SIZE == 16u +#elif USB_EP0_BUFFER_SIZE == 32u +#elif USB_EP0_BUFFER_SIZE == 64u +#else +#error "USB_EP0_BUFFER_SIZE needs to be 8, 16, 32 or 64 bytes" +#endif + + +ROMPTR const unsigned char *usb_device_descriptor; +ROMPTR const unsigned char *usb_config_descriptor; +ROMPTR const unsigned char *usb_string_descriptor; +int usb_num_string_descriptors; + +usb_handler_t sof_handler; +usb_handler_t class_setup_handler, vendor_setup_handler; + +usb_ep_t endpoints[MAX_CHIP_EP]; // JTR change. MAX_CHIP_EP is the number of hardware endpoints on the silicon. See picusb.h + +// JTR comments. This below goes part way to ridding us of the need for a linker script (PIC18). +// It floats the EP0 buffers to occupy space immediately behind the buffer descriptors. +// However there are problems. It only works for the EP0 buffers because they are the only +// buffers defined in this block. It is not possible to do arithmatic within a #pragma +// therefore there seems to be no way to cause EPs in other blocks to float. +// All the buffers would need to be defined here and this would break the universal nature +// of this module. As such efforts to create a custom linker script free stack have been +// put on hold for now but can be revisited at any stage. +// Please see http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=120#p17401 + + +#pragma udata usb_bdt +BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED] __attribute__((aligned(512))); // JTR changed index from 32 to variable TODO: Dynamic allocation reflecting number of used endpoints. (How to do counting in preprocessor?) +#if USB_PP_BUF_MODE == 0 +unsigned char usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; +unsigned char usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; +#else +#error "Ping pong buffer not implemented yet!" +#endif + + +#pragma udata +unsigned int usb_device_status; +unsigned int usb_current_cfg; +volatile unsigned char usb_device_state; +unsigned char usb_addr_pending; +usb_status_t trn_status; // Global since it is needed everywere +BDentry *bdp, *rbdp; // Dito + +const unsigned char *usb_desc_ptr; +size_t usb_desc_len; + +/* Forward Reference Prototypes */ +void usb_handle_error(void); +void usb_handle_reset(void); +void usb_handle_transaction(void); +void usb_handle_setup(void); +void usb_handle_out(void); +void usb_handle_in(void); +void usb_handle_StandardDeviceRequest(BDentry*); +void usb_handle_StandardInterfaceRequest(BDentry*); +void usb_handle_StandardEndpointRequest(BDentry*); +//void usb_handle_ClassRequest( void ); +//void usb_handle_VendorRequest( void ); +void usb_RequestError(void); + +void usb_ack_dat1(BDentry *rbdp, int bdcnt); // JTR added +void usb_set_address(void); +void usb_send_descriptor(void); +void user_configured_init(void); //JTR added.Non EP0 endpoints are arm after USB device enters CONFIGURED STATE. This is a CALLBACK function. +BYTE FAST_usb_handler(void); // JTR2 added fast USB service. Pops non EP0 transfer flags from FIFO. Sevices RESET and SETUP + +void usb_init(ROMPTR const unsigned char *device_descriptor, + ROMPTR const unsigned char *config_descriptor, + ROMPTR const unsigned char *string_descriptor, + int num_string_descriptors) { + int i; + + + usb_device_descriptor = device_descriptor; + usb_config_descriptor = config_descriptor; + usb_string_descriptor = string_descriptor; + usb_num_string_descriptors = num_string_descriptors; + + SetUsbAddress(0); // JTR added here. Probably not required though + ResetPPbuffers(); + DisableUsbInterrupts(); + DisableAllUsbInterrupts(); + ClearAllUsbErrorInterruptFlags(); + ClearAllUsbInterruptFlags(); + ConfigureUsbHardware(); + + sof_handler = NULL; + class_setup_handler = NULL; + vendor_setup_handler = NULL; + + for (i = 0; i < MAX_CHIP_EP; i++) { + endpoints[i].out_handler = NULL; + endpoints[i].in_handler = NULL; + } + // Register ep0 - no handlers + + usb_unset_in_handler(0); + usb_unset_out_handler(0); + + // JTR All UEPx SPRs are hard coded. I cannot see much point of tables or indexing for these. + // On the PIC16C765 such indexing was required for the STALL bit but now the STALL feature + // is in the Buffer descriptor table not in the UEPx SPRs. + // See changes to "USB_REQUEST_GET_STATUS" + + // USB_UEP[0] = endpoints[0].type; + + USB_UEP0 = USB_EP_CONTROL; + /* Configure endpoints TODO: Only ep0 ? */ + // JTR Right! code for other end points snipped...At this point we are only setting up EP0 + +#ifdef USB_SELF_POWERED + usb_device_status = 0x0001; +#else + usb_device_status = 0x0000; +#endif + usb_device_state = 0x00; // JTR added flag byte for enumeration state + usb_current_cfg = 0; // JTR formally usb_configured + usb_addr_pending = 0x00; + +#if USB_PP_BUF_MODE == 0 + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS; // Set DTS => First packet inverts, ie. is Data0 +#else + // TODO: Implement Ping-Pong buffering setup. +#error "PP Mode not implemented yet" +#endif +} + +void usb_start(void) { + EnableUsb(); // Enable USB-hardware + //uLedOn(); + while (SingleEndedZeroIsSet()); // Busywait for initial power-up + uLedOn(); + +#ifdef USB_INTERRUPT + // EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_SOF + USB_UERR + USB_URST); //We don't enable ACTIV unless we are going into suspend mode. + EnableAllUsbErrorInterrupts(); + EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST); + EnableUSBHighInterrupts(); +#endif +} + +void usb_handler(void) { + if (USB_RESET_FLAG) { + usb_handle_reset(); + ClearUsbInterruptFlag(USB_URST); + } else if (USB_ERROR_FLAG) { + usb_handle_error(); + ClearUsbInterruptFlag(USB_UERR); + } else if (USB_ACTIVITY_FLAG) { + /* Activity - unsuspend */ + //WakeupUsb(); + ClearUsbInterruptFlag(USB_ACTIV); + } else if (USB_IDLE_FLAG) { + /* Idle - suspend */ + //SuspendUsb(); + //usb_low_power_request(); + ClearUsbInterruptFlag(USB_IDLE); + } else if (USB_STALL_FLAG) { + /* Stall detected + * Not sure when this interrupt fires + * as the host never should send out a stall. + * Perhaps as a respons to our own stalls? + * For now just ignore it. */ + ClearUsbInterruptFlag(USB_STALL); + } else if (USB_SOF_FLAG) { + /* Start-of-frame */ + //if (sof_handler) sof_handler(); + { + ClearUsbInterruptFlag(USB_SOF); + } + } else { + // process all pending transactions + while (USB_TRANSACTION_FLAG) { + usb_handle_transaction(); + ClearUsbInterruptFlag(USB_TRN); + } // Side effect: advance USTAT Fifo + } +} + +void usb_handle_error(void) { + /* No errorhandler for now, just clear offending flag*/ + ClearAllUsbErrorInterruptFlags(); +} + +void usb_handle_reset(void) { + int i; + mLedToggle(); +#ifdef USB_SELF_POWERED // JTR see note about this in usb_init() + usb_device_status = 0x0001; +#else + usb_device_status = 0x0000; +#endif + + do { + ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. + usb_current_cfg = 0; + usb_device_state = 0x00; // This creates the requied 6 cycle delay for TRNF to reassert. + usb_addr_pending = 0x00; + } while (USB_TRANSACTION_FLAG); + + // for (i=0; i < MAX_CHIP_EP; i++ ) // JTR this loop seems to work proving that the USB_UEP ptr is now correct. + // { + // USB_UEP[i] = 0; // JTR Char on PIC18 and int ptr on PIC24 + // } + USB_UEP0 = 0; // Disable all endpoints + USB_UEP1 = 0; + USB_UEP2 = 0; + USB_UEP3 = 0; + USB_UEP4 = 0; + USB_UEP5 = 0; + USB_UEP6 = 0; + USB_UEP7 = 0; +#if MAX_CHIP_EP > 8 // JTR alteration + USB_UEP8 = 0; + USB_UEP9 = 0; + USB_UEP10 = 0; + USB_UEP11 = 0; + USB_UEP12 = 0; + USB_UEP13 = 0; + USB_UEP14 = 0; + USB_UEP15 = 0; + +#endif + SetUsbAddress(0); // After reset we don't have an address + ClearAllUsbInterruptFlags(); + ClearAllUsbErrorInterruptFlags(); + + // JTR added. Clear all BD STAT registers so that they are owned by the CPU and it is safe to change them. + + for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i += 4) { + usb_bdt[i].BDSTAT = 0; + } + // USB_UEP[0] = endpoints[0].type; // JTR removed all such indexing on UEPx + USB_UEP0 = USB_EP_CONTROL; // JTR hard coded instead. + + /* Configure buffer descriptors */ + +#if USB_PP_BUF_MODE == 0 + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; //endpoints[0].buffer_size; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS; // Set DTS => First packet inverts, ie. is Data0 + +#else + // TODO: Implement Ping-Pong buffering setup. +#error "PP Mode not implemented yet" +#endif + EnablePacketTransfer(); +} + +void usb_handle_transaction(void) { + uLedToggle(); + trn_status = GetUsbTransaction(); + bdp = &usb_bdt[USB_USTAT2BD(trn_status)]; + // JTR PIC24 fixup Added DIRBIT as it is different for PIC24F ( and different again for the PIC24E/dsPIC33E) + // the USB_USTAT2BD macro is also altered to shift the result the correct number of places. + + rbdp = &usb_bdt[USB_USTAT2BD(trn_status | DIRBIT)]; // All replies in IN direction + + switch (bdp->BDSTAT & USB_TOKEN_Mask) { + case USB_TOKEN_SETUP: + usb_handle_setup(); + break; + case USB_TOKEN_OUT: + usb_handle_out(); + break; + case USB_TOKEN_IN: + usb_handle_in(); + break; + //default: + /* Default case of unknown TOKEN - discard */ + } +} + +void usb_handle_setup(void) { + rbdp->BDSTAT = DTSEN; // Reclaim reply buffer + switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) { + case USB_bmRequestType_Standard: + switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) { + case USB_bmRequestType_Device: + usb_handle_StandardDeviceRequest(bdp); + break; + case USB_bmRequestType_Interface: + usb_handle_StandardInterfaceRequest(bdp); + break; + case USB_bmRequestType_Endpoint: + usb_handle_StandardEndpointRequest(bdp); + break; + default: + usb_RequestError(); + } + break; + case USB_bmRequestType_Class: + if (class_setup_handler) class_setup_handler(); + break; + case USB_bmRequestType_Vendor: + if (vendor_setup_handler) class_setup_handler(); + break; + default: + usb_RequestError(); + } + /* Prepare endpoint for new reception */ + + bdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size of EP0, should always be ep0? (JTR in practice YES) + + // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)? + // note that this is not an entirely robust way of doing things. See the microchip stack for + // further comments and a better system as this does not account for errors and retries. + + // JTR Breakdown of what is happening here FYI. + + //bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && + // JTR meaning. If transfer was a CONTROL OUT then B7 (USB_bmRequestType_PhaseMask) of bmRequestType is '0' + + // && + + //bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh]))? UOWN + DTS + DTSEN : UOWN + DTSEN; + // If there is a CONTROL OUT DATA PACKET to follow then its count in (int) wLength will be != 0 + // When both conditions are true then set for DAT1 (UOWN + DTS + DTSEN) + // else set for DAT0 (UOWN + DTSEN) + + // See USB 2.0 spec 8.5.3 + + bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && + (bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN; + + // JTR Note. For rbdp after a setup packet, the standard or class request handler will force the correct DTS state + // JTR Note. that CONTROL IN and OUT DATA packet transfers do not come back here and there is no + // univesal way and place of setting up EP0 after these DATA transfers in this stack. + + // JTR Note. that there is a PIC18 silicon errata issue that this does not address by being here. See DS80220F-page 6 + EnablePacketTransfer(); +} + +// JTR comment on the standard device requests handler. +// This does not address that some requests need to be +// handled differently depending on what USB defined +// "STATE" of enumeration we are at. In fact this stack +// does not even track any of the different states. +// except that I have added basic tracking to determine +// if the device is configured or not. This is a minimum. +// Later the stack can be improved. + +void usb_handle_StandardDeviceRequest(BDentry *bdp) { + unsigned char *packet = bdp->BDADDR; + int i; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + rbdp->BDADDR[0] = usb_device_status & 0xFF; + rbdp->BDADDR[1] = usb_device_status >> 8; + + // JTR I added usb_ack_dat1() simply so that the handling + // of STANDARD and CLASS requests (in cdc.c) were consistant + // rather than have the same thing done in two different ways. + + usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK + break; + case USB_REQUEST_CLEAR_FEATURE: + if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) + usb_device_status &= ~0x0002; + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_FEATURE: + if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) + usb_device_status |= 0x0002; + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_ADDRESS: + if (0x00u == packet[USB_wValueHigh] && 0x7Fu >= packet[USB_wValue]) { + usb_addr_pending = packet[USB_wValue]; + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + usb_set_in_handler(0, usb_set_address); + } else + usb_RequestError(); + break; + + + case USB_REQUEST_GET_DESCRIPTOR: + switch (packet[USB_bDescriptorType]) { + case USB_DEVICE_DESCRIPTOR_TYPE: + usb_desc_ptr = usb_device_descriptor; + usb_desc_len = usb_device_descriptor[0]; + if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_desc_ptr[0])) + usb_desc_len = packet[USB_wLength]; + break; + case USB_CONFIGURATION_DESCRIPTOR_TYPE: + if (packet[USB_bDescriptorIndex] >= usb_device_descriptor[17]) // TODO: remove magic + usb_RequestError(); + usb_desc_ptr = usb_config_descriptor; + usb_desc_len = usb_desc_ptr[2] + usb_desc_ptr[3] * 256; + for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration + usb_desc_ptr += usb_desc_len; + usb_desc_len = usb_desc_ptr[2] + usb_desc_ptr[3] * 256; + } + if ((packet[USB_wLengthHigh] < usb_desc_ptr[3]) || + (packet[USB_wLengthHigh] == usb_desc_ptr[3] && packet[USB_wLength] < usb_desc_ptr[2])) + usb_desc_len = packet[USB_wLength] + packet[USB_wLengthHigh] * 256; + break; + case USB_STRING_DESCRIPTOR_TYPE: + // TODO: Handle language request. For now return standard language. + if (packet[USB_bDescriptorIndex] >= usb_num_string_descriptors) + usb_RequestError(); + usb_desc_ptr = usb_string_descriptor; + usb_desc_len = usb_desc_ptr[0]; + for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration + usb_desc_ptr += usb_desc_len; + usb_desc_len = usb_desc_ptr[0]; + } + if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_desc_ptr[0])) + usb_desc_len = packet[USB_wLength]; + break; + case USB_INTERFACE_DESCRIPTOR_TYPE: + case USB_ENDPOINT_DESCRIPTOR_TYPE: + default: + usb_RequestError(); + } + usb_send_descriptor(); // Send first part of packet right away + usb_set_in_handler(0, usb_send_descriptor); + break; + case USB_REQUEST_GET_CONFIGURATION: + rbdp->BDADDR[0] = usb_current_cfg; + usb_ack_dat1(rbdp, 1); // JTR common addition for STD and CLASS ACK + // rbdp->BDCNT = 1; + // rbdp->BDSTAT = UOWN + DTS + DTSEN; + break; + + case USB_REQUEST_SET_CONFIGURATION: + if (USB_NUM_CONFIGURATIONS >= packet[USB_wValue]) { + // TODO: Support multiple configurations + /* Configure endpoints (USB_UEPn - registers) */ + usb_current_cfg = packet[USB_wValue]; + if (usb_current_cfg != 0) { + + // JTR user_configured_init major addition. This is a CALLBACK to the USER when the device is enumerated. + // This is when we setup non EP0 endpoints. + // TODO: This really could be a function pointer + + usb_device_state = CONFIGURED_STATE; + user_configured_init(); + } else { + usb_device_state = ADDRESS_STATE; + } + + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + + } else + usb_RequestError(); + break; + + case USB_REQUEST_SET_DESCRIPTOR: + default: + usb_RequestError(); + } +} + +void usb_handle_StandardInterfaceRequest(BDentry *bdp) { + unsigned char *packet = bdp->BDADDR; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + rbdp->BDADDR[0] = 0x00; + rbdp->BDADDR[1] = 0x00; + usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK + // rbdp->BDCNT = 2; + // rbdp->BDSTAT = UOWN + DTS + DTSEN; + break; + case USB_REQUEST_GET_INTERFACE: + if (USB_NUM_INTERFACES > packet[USB_bInterface]) { + // TODO: Implement alternative interfaces, or move responsibility to class/vendor functions. + rbdp->BDADDR[0] = 0; + usb_ack_dat1(rbdp, 1); // JTR common addition for STD and CLASS ACK + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_INTERFACE: + if (USB_NUM_INTERFACES > packet[USB_bInterface] && 0u == packet[USB_wValue]) { + // TODO: Implement alternative interfaces... + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + } else + usb_RequestError(); + break; + case USB_REQUEST_CLEAR_FEATURE: // JTR N/A for interface + case USB_REQUEST_SET_FEATURE: // This is correct and finished code. + default: + usb_RequestError(); + } +} + +void usb_handle_StandardEndpointRequest(BDentry *bdp) { + unsigned char *packet; + unsigned char epnum; + unsigned char dir; + BDentry *epbd; + + packet = bdp->BDADDR; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + rbdp->BDADDR[0] = 0x00; // Assume no stall + rbdp->BDADDR[1] = 0x00; // Same for stall or not + epnum = packet[USB_wIndex] & 0x0F; + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + if (epbd->BDSTAT &= ~BSTALL) + rbdp->BDADDR[0] = 0x01; // EVEN BD is stall flag set? + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + if (epbd->BDSTAT &= ~BSTALL) + rbdp->BDADDR[0] = 0x01; // ODD BD is stall flag set? + usb_ack_dat1(rbdp, 2); // JTR common addition for STD and CLASS ACK + break; + + case USB_REQUEST_CLEAR_FEATURE: + epnum = packet[USB_wIndex] & 0x0F; + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + epbd->BDSTAT &= ~BSTALL; + if (dir) epbd->BDSTAT |= DTS; // JTR added IN EP set DTS as it will be toggled to zero next transfer + if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added + + // JTR this pointless ATM. If ping-pong is enabled then you need to track PPBI + // and set up ODD and EVEN BDs in respect to this. See complicated system in + // microchip stack >= 2.8 + + // epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + // epbd->BDSTAT &= ~BSTALL; + // if (dir) epbd->BDSTAT |= DTS; // JTR added + // if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added + + + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + // rbdp->BDCNT = 0; + // rbdp->BDSTAT = UOWN + DTS + DTSEN; + break; + + + case USB_REQUEST_SET_FEATURE: + epnum = packet[USB_wIndex] & 0x0F; + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + epbd->BDSTAT |= BSTALL; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + epbd->BDSTAT |= BSTALL; + usb_ack_dat1(rbdp, 0); // JTR common addition for STD and CLASS ACK + // rbdp->BDCNT = 0; + // rbdp->BDSTAT = UOWN + DTS + DTSEN; + break; + case USB_REQUEST_SYNCH_FRAME: + default: + usb_RequestError(); + } +} + +void usb_handle_in(void) { + if (endpoints[USB_STAT2EP(trn_status)].in_handler) { + endpoints[USB_STAT2EP(trn_status)].in_handler(); + } +} + +void usb_handle_out(void) { + if (endpoints[USB_STAT2EP(trn_status)].out_handler) { + endpoints[USB_STAT2EP(trn_status)].out_handler(); + } +} + +void usb_register_sof_handler(usb_handler_t handler) { + sof_handler = handler; +} + +void usb_register_class_setup_handler(usb_handler_t handler) { + class_setup_handler = handler; +} + +void usb_register_vendor_setup_handler(usb_handler_t handler) { + vendor_setup_handler = handler; +} + +void usb_set_in_handler(int ep, usb_handler_t in_handler) { + endpoints[ep].in_handler = in_handler; +} + +void usb_set_out_handler(int ep, usb_handler_t out_handler) { + endpoints[ep].out_handler = out_handler; +} + +// JTR not actually using these two helper functions below +// but they are still useful and could be put back to use +// as they aught to result in smaller code. For now I am +// using the usb_ack_dat1() as it more correctly forces a +// DAT1 transfer and passes a varible for the transfer count. + +void usb_ack(BDentry *bd) { + bd->BDSTAT = ((bd->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // TODO: Accomodate for >=256 byte buffers +} + +void usb_ack_zero(BDentry *bd) { + bd->BDCNT = 0; + bd->BDSTAT = ((bd->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + +} +// JTR New added helper function use extensively by +// the standard and class request handlers. All status +// IN packets are DAT1 as is the first DATA packet of +// a IN transfer. Currently with this CDC stack the +// only IN DATA transfers that are > 8 bytes is the +// descriptor transfer and these are transfered in +// usb_send_descriptor() + +void usb_ack_dat1(BDentry *rbdp, int bdcnt) { + rbdp->BDCNT = (bdcnt & 0xFF); + rbdp->BDSTAT = (DTS | UOWN | DTSEN); // | ((bdcnt & 0x300) >> 8)); +} + +void usb_RequestError(void) { + + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDCNT = USB_EP0_BUFFER_SIZE; + + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; + + // JTR TODO: Should also kill the IN and OUT handlers? + +} + +void usb_set_address(void) { + // if (0x00u <= usb_addr_pending && 0x80u > usb_addr_pending) { // JTR changed. The USB specs allow for a device to be de-addressed + if (0x80u > usb_addr_pending) { + SetUsbAddress(usb_addr_pending); + usb_addr_pending = 0xFF; + } + // JTR TODO: If we are going to bounds check the pending address + // (again) and it is out of bounds we should return a + // protocol stall here. + usb_unset_in_handler(0); // Unregister handler +} + +void usb_send_descriptor(void) { + + // JTR comment While this function is being used only for the descriptors + // there is no reason why it cannot be a generic CONTROL IN transfer handler. + // To that ends it needs a flag to denote if it is copying from a RAM or ROM + // pointer + + unsigned int i; + // JTR N/A BDentry *bd; + size_t packet_len; + if (usb_desc_len) { + packet_len = (usb_desc_len < USB_EP0_BUFFER_SIZE) ? usb_desc_len : USB_EP0_BUFFER_SIZE; // JTR changed from MAX_BUFFER_SIZE + for (i = 0; i < packet_len; i++) { + rbdp->BDADDR[i] = usb_desc_ptr[i]; + } + } else { + + // JTR comment. So the transfer is now completed. + packet_len = 0; // Send a last ack + usb_unset_in_handler(0); + } + + rbdp->BDCNT = (unsigned char) packet_len; + rbdp->BDSTAT = ((rbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // Packet length always less then 256 on endpoint 0 + + usb_desc_ptr += packet_len; + usb_desc_len -= packet_len; +} + +BYTE FAST_usb_handler(void) { + if (TestGlobalUsbInterruptFlag()) { + if (USB_RESET_FLAG) { + usb_handle_reset(); + ClearUsbInterruptFlag(USB_URST); + return 0xFF; + } + if (USB_TRANSACTION_FLAG) { + trn_status = GetUsbTransaction(); + if (USB_STAT2EP(trn_status)) { + usb_handle_transaction(); + ClearUsbInterruptFlag(USB_TRN); // non-EP0 only + return 0xFF; + } else { + ClearUsbInterruptFlag(USB_TRN); // non-EP0 only + } + } + ClearGlobalUsbInterruptFlag(); + } + return 0; +} + diff --git a/BPv4-bootloader/firmware-v1/usb_stack.h b/BPv4-bootloader/firmware-v1/usb_stack.h index 841f64c2..1b84d268 100644 --- a/BPv4-bootloader/firmware-v1/usb_stack.h +++ b/BPv4-bootloader/firmware-v1/usb_stack.h @@ -1,206 +1,206 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - */ - -#ifndef __USB_STACK_H__ -#define __USB_STACK_H__ - -// JTR v0.1b - -#include "picusb.h" - -#define USB_TOKEN_Mask 0b00111100 -#define USB_TOKEN_OUT 0b00000100 -#define USB_TOKEN_IN 0b00100100 -#define USB_TOKEN_SOF 0b00010100 -#define USB_TOKEN_SETUP 0b00110100 -#define USB_TOKEN_DATA0 0b00001100 -#define USB_TOKEN_DATA1 0b00101100 -#define USB_TOKEN_DATA2 0b00011100 /* High speed isochronous endpoints only */ -#define USB_TOKEN_MDATA 0b00111100 /* High speed isochronous enpoints and hub devices only */ -#define USB_TOKEN_ACK 0b00001000 -#define USB_TOKEN_NAK 0b00101000 -#define USB_TOKEN_STALL 0b00111000 -#define USB_TOKEN_NYET 0b00011000 /* High speed devices only */ -#define USB_TOKEN_PRE 0b00110000 -#define USB_TOKEN_ERR 0b00110000 -#define USB_TOKEN_SPLIT 0b00100000 /* Hub devices only */ -#define USB_TOKEN_PING 0b00010000 /* High speed devices only */ - -/* Descriptor Types */ -#define USB_DEVICE_DESCRIPTOR_TYPE 1u -#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2u -#define USB_STRING_DESCRIPTOR_TYPE 3u -#define USB_INTERFACE_DESCRIPTOR_TYPE 4u -#define USB_ENDPOINT_DESCRIPTOR_TYPE 5u -#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6u -#define USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR_TYPE 7u -#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8u -#define USB_OTG_DESCRIPTOR_TYPE 9u -#define USB_DEBUG_DESCRIPTOR_TYPE 10u -#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11u - -#define USB_bmRequestType 0 -#define USB_bRequest 1 -#define USB_wValue 2 -#define USB_bDescriptorIndex 2 -#define USB_wValueHigh 3 -#define USB_bDescriptorType 3 -#define USB_wIndex 4 -#define USB_bInterface 4 -#define USB_wIndexHigh 5 -#define USB_wLength 6 -#define USB_wLengthHigh 7 - -#define USB_bmRequestType_PhaseMask 0b10000000 -#define USB_bmRequestType_H2D 0b00000000 -#define USB_bmRequestType_D2H 0b10000000 -#define USB_bmRequestType_TypeMask 0b01100000 -#define USB_bmRequestType_Standard 0b00000000 -#define USB_bmRequestType_Class 0b00100000 -#define USB_bmRequestType_Vendor 0b01000000 -#define USB_bmRequestType_RecipientMask 0b00000011 -#define USB_bmRequestType_Device 0b00000000 -#define USB_bmRequestType_Interface 0b00000001 -#define USB_bmRequestType_Endpoint 0b00000010 -#define USB_bmRequestType_Other 0b00000011 - -#define USB_REQUEST_GET_STATUS 0 -#define USB_REQUEST_CLEAR_FEATURE 1 -#define USB_REQUEST_SET_FEATURE 3 -#define USB_REQUEST_SET_ADDRESS 5 -#define USB_REQUEST_GET_DESCRIPTOR 6 -#define USB_REQUEST_SET_DESCRIPTOR 7 -#define USB_REQUEST_GET_CONFIGURATION 8 -#define USB_REQUEST_SET_CONFIGURATION 9 -#define USB_REQUEST_GET_INTERFACE 10 -#define USB_REQUEST_SET_INTERFACE 11 -#define USB_REQUEST_SYNCH_FRAME 12 - -typedef enum -{ -DETACHED_STATE = 0x00, -ATTACHED_STATE = 0x01, -POWERED_STATE = 0x02, -DEFAULT_STATE = 0x04, -ADR_PENDING_STATE = 0x08, -ADDRESS_STATE = 0x10, -CONFIGURED_STATE = 0x20 -} USB_DEVICE_STATE; - -/* Structs for defining endpoints */ -//typedef void(*)(unsigned char *) usb_ep_callback; -typedef void(*usb_handler_t)(void); - -typedef struct USB_EP_TYPE { -// usb_uep_t type; -// unsigned int buffer_size; -// unsigned char *in_buffer, *out_buffer; - usb_handler_t in_handler, out_handler; -} usb_ep_t; - -/* Misc */ -#define HIGHB(x) ((x)>>8) -#define LOWB(x) ((x) & 0xFF) - -#define XCAT(x,y) x ## y -#define CAT(x,y) XCAT(x,y) - -/* Descriptors */ -#if USB_NUM_CONFIGURATIONS > 1 -#error "More than 1 configuration not supported yet" -#endif -/* -#if defined class_init - #ifndef class_setup - #error "No Class setup handler defined" - #endif -#elif defined vendor_init - #ifndef vendor_setup - #error "No Vendor setup handler defined" - #endif -#else - #error "Niether Class nor Vendor initialization function defined" -#endif - */ - -/* JTR I moved this block into picusb.h - * My rational is to keep this header free from - * conditional compile directives based on what - * PIC family is being compiled for. It just helps - * to keep things orderly and makes for less work - * when someone adds support for the new PIC24E parts. - - -#if defined(PIC_18F) -typedef struct BDENTRY { - unsigned char -// struct { -// unsigned BCH:2; -// unsigned BSTALL:1; -// unsigned DTSEN:1; -// unsigned INCDIS:1; -// unsigned KEN:1; -// unsigned DTS:1; -// unsigned UOWN:1; -// } - BDSTAT; - unsigned char BDCNT; - unsigned char *BDADDR; -} BDentry; -#else -typedef struct BDENTRY { - unsigned char BDCNT; // JTR PIC24 fixup Note that BDCNT & BDSTAT are swapped from the PIC18!! - unsigned char BDSTAT; // Smacks head. Go on, yo know you want to. - unsigned char *BDADDR; -} BDentry; -#endif - */ - -extern BDentry usb_bdt[]; - -typedef struct USB_DEVICE_REQUEST { - unsigned char bmRequestType; - unsigned char bRequest; - unsigned int wValue; - unsigned int wIndex; - unsigned int wLength; -} usb_device_request; - -typedef unsigned char usb_status_t; -extern usb_status_t trn_status; -extern BDentry *bdp, *rbdp; - -extern void usb_init(ROMPTR const unsigned char *dev_descriptor, - ROMPTR const unsigned char *config_descriptor, - ROMPTR const unsigned char *string_descriptor, int num_string_descriptors); - - -extern void usb_start(void); -extern void usb_register_sof_handler(usb_handler_t handler); -extern void usb_register_class_setup_handler(usb_handler_t handler); -extern void usb_register_vendor_setup_handler(usb_handler_t handler); -extern void usb_set_in_handler(int ep, usb_handler_t handler); -extern void usb_set_out_handler(int ep, usb_handler_t handler); -#define usb_unset_in_handler(ep) usb_set_in_handler(ep, (usb_handler_t) 0) -#define usb_unset_out_handler(ep) usb_set_out_handler(ep, (usb_handler_t) 0) // I will friggin die if this turns out to be the PIC18 bug! - -extern void usb_handler(void); -extern void usb_ack(BDentry *); -extern void usb_ack_zero(BDentry *); -// JTR N/A extern void usb_ack_out(BDentry *); -extern void usb_RequestError(void); -unsigned char FAST_usb_handler(void); -extern void user_configured_init(void); - -#endif /* USB_STACK_H */ - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + */ + +#ifndef __USB_STACK_H__ +#define __USB_STACK_H__ + +// JTR v0.1b + +#include "picusb.h" + +#define USB_TOKEN_Mask 0b00111100 +#define USB_TOKEN_OUT 0b00000100 +#define USB_TOKEN_IN 0b00100100 +#define USB_TOKEN_SOF 0b00010100 +#define USB_TOKEN_SETUP 0b00110100 +#define USB_TOKEN_DATA0 0b00001100 +#define USB_TOKEN_DATA1 0b00101100 +#define USB_TOKEN_DATA2 0b00011100 /* High speed isochronous endpoints only */ +#define USB_TOKEN_MDATA 0b00111100 /* High speed isochronous enpoints and hub devices only */ +#define USB_TOKEN_ACK 0b00001000 +#define USB_TOKEN_NAK 0b00101000 +#define USB_TOKEN_STALL 0b00111000 +#define USB_TOKEN_NYET 0b00011000 /* High speed devices only */ +#define USB_TOKEN_PRE 0b00110000 +#define USB_TOKEN_ERR 0b00110000 +#define USB_TOKEN_SPLIT 0b00100000 /* Hub devices only */ +#define USB_TOKEN_PING 0b00010000 /* High speed devices only */ + +/* Descriptor Types */ +#define USB_DEVICE_DESCRIPTOR_TYPE 1u +#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2u +#define USB_STRING_DESCRIPTOR_TYPE 3u +#define USB_INTERFACE_DESCRIPTOR_TYPE 4u +#define USB_ENDPOINT_DESCRIPTOR_TYPE 5u +#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6u +#define USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR_TYPE 7u +#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8u +#define USB_OTG_DESCRIPTOR_TYPE 9u +#define USB_DEBUG_DESCRIPTOR_TYPE 10u +#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11u + +#define USB_bmRequestType 0 +#define USB_bRequest 1 +#define USB_wValue 2 +#define USB_bDescriptorIndex 2 +#define USB_wValueHigh 3 +#define USB_bDescriptorType 3 +#define USB_wIndex 4 +#define USB_bInterface 4 +#define USB_wIndexHigh 5 +#define USB_wLength 6 +#define USB_wLengthHigh 7 + +#define USB_bmRequestType_PhaseMask 0b10000000 +#define USB_bmRequestType_H2D 0b00000000 +#define USB_bmRequestType_D2H 0b10000000 +#define USB_bmRequestType_TypeMask 0b01100000 +#define USB_bmRequestType_Standard 0b00000000 +#define USB_bmRequestType_Class 0b00100000 +#define USB_bmRequestType_Vendor 0b01000000 +#define USB_bmRequestType_RecipientMask 0b00000011 +#define USB_bmRequestType_Device 0b00000000 +#define USB_bmRequestType_Interface 0b00000001 +#define USB_bmRequestType_Endpoint 0b00000010 +#define USB_bmRequestType_Other 0b00000011 + +#define USB_REQUEST_GET_STATUS 0 +#define USB_REQUEST_CLEAR_FEATURE 1 +#define USB_REQUEST_SET_FEATURE 3 +#define USB_REQUEST_SET_ADDRESS 5 +#define USB_REQUEST_GET_DESCRIPTOR 6 +#define USB_REQUEST_SET_DESCRIPTOR 7 +#define USB_REQUEST_GET_CONFIGURATION 8 +#define USB_REQUEST_SET_CONFIGURATION 9 +#define USB_REQUEST_GET_INTERFACE 10 +#define USB_REQUEST_SET_INTERFACE 11 +#define USB_REQUEST_SYNCH_FRAME 12 + +typedef enum +{ +DETACHED_STATE = 0x00, +ATTACHED_STATE = 0x01, +POWERED_STATE = 0x02, +DEFAULT_STATE = 0x04, +ADR_PENDING_STATE = 0x08, +ADDRESS_STATE = 0x10, +CONFIGURED_STATE = 0x20 +} USB_DEVICE_STATE; + +/* Structs for defining endpoints */ +//typedef void(*)(unsigned char *) usb_ep_callback; +typedef void(*usb_handler_t)(void); + +typedef struct USB_EP_TYPE { +// usb_uep_t type; +// unsigned int buffer_size; +// unsigned char *in_buffer, *out_buffer; + usb_handler_t in_handler, out_handler; +} usb_ep_t; + +/* Misc */ +#define HIGHB(x) ((x)>>8) +#define LOWB(x) ((x) & 0xFF) + +#define XCAT(x,y) x ## y +#define CAT(x,y) XCAT(x,y) + +/* Descriptors */ +#if USB_NUM_CONFIGURATIONS > 1 +#error "More than 1 configuration not supported yet" +#endif +/* +#if defined class_init + #ifndef class_setup + #error "No Class setup handler defined" + #endif +#elif defined vendor_init + #ifndef vendor_setup + #error "No Vendor setup handler defined" + #endif +#else + #error "Niether Class nor Vendor initialization function defined" +#endif + */ + +/* JTR I moved this block into picusb.h + * My rational is to keep this header free from + * conditional compile directives based on what + * PIC family is being compiled for. It just helps + * to keep things orderly and makes for less work + * when someone adds support for the new PIC24E parts. + + +#if defined(PIC_18F) +typedef struct BDENTRY { + unsigned char +// struct { +// unsigned BCH:2; +// unsigned BSTALL:1; +// unsigned DTSEN:1; +// unsigned INCDIS:1; +// unsigned KEN:1; +// unsigned DTS:1; +// unsigned UOWN:1; +// } + BDSTAT; + unsigned char BDCNT; + unsigned char *BDADDR; +} BDentry; +#else +typedef struct BDENTRY { + unsigned char BDCNT; // JTR PIC24 fixup Note that BDCNT & BDSTAT are swapped from the PIC18!! + unsigned char BDSTAT; // Smacks head. Go on, yo know you want to. + unsigned char *BDADDR; +} BDentry; +#endif + */ + +extern BDentry usb_bdt[]; + +typedef struct USB_DEVICE_REQUEST { + unsigned char bmRequestType; + unsigned char bRequest; + unsigned int wValue; + unsigned int wIndex; + unsigned int wLength; +} usb_device_request; + +typedef unsigned char usb_status_t; +extern usb_status_t trn_status; +extern BDentry *bdp, *rbdp; + +extern void usb_init(ROMPTR const unsigned char *dev_descriptor, + ROMPTR const unsigned char *config_descriptor, + ROMPTR const unsigned char *string_descriptor, int num_string_descriptors); + + +extern void usb_start(void); +extern void usb_register_sof_handler(usb_handler_t handler); +extern void usb_register_class_setup_handler(usb_handler_t handler); +extern void usb_register_vendor_setup_handler(usb_handler_t handler); +extern void usb_set_in_handler(int ep, usb_handler_t handler); +extern void usb_set_out_handler(int ep, usb_handler_t handler); +#define usb_unset_in_handler(ep) usb_set_in_handler(ep, (usb_handler_t) 0) +#define usb_unset_out_handler(ep) usb_set_out_handler(ep, (usb_handler_t) 0) // I will friggin die if this turns out to be the PIC18 bug! + +extern void usb_handler(void); +extern void usb_ack(BDentry *); +extern void usb_ack_zero(BDentry *); +// JTR N/A extern void usb_ack_out(BDentry *); +extern void usb_RequestError(void); +unsigned char FAST_usb_handler(void); +extern void user_configured_init(void); + +#endif /* USB_STACK_H */ + diff --git a/BPv4-bootloader/pirate-loader/pirate-loader.c b/BPv4-bootloader/pirate-loader/pirate-loader.c index 2de44b0a..c95ca8ae 100644 --- a/BPv4-bootloader/pirate-loader/pirate-loader.c +++ b/BPv4-bootloader/pirate-loader/pirate-loader.c @@ -1,1256 +1,1256 @@ -/* - - Pirate-Loader for Bootloader v4 - - Version : 1.0.2 - - Changelog: - +2010-06-28 - Made HEX parser case-insensative - - + 2010-02-04 - Changed polling interval to 10ms on Windows select wrapper, suggested by Michal (robots) - - + 2010-02-04 - Added sleep(0) between write instructions, patch submitted by kbulgrien - - + 2010-01-22 - Added loader version number to the console output and source code - - + 2010-01-19 - Fixed BigEndian incompatibility - - Added programming simulate switch ( --simulate ) for data verification - - + 2010-01-18 - Initial release - - - Building: - - UNIX family systems: - - gcc pirate-loader.c -o pirate-loader - - WINDOWS: - - cl pirate-loader.c /DWIN32=1 - - - Usage: - - Run ./pirate-loader --help for more information on usage and possible switches - - */ - -#include -#include -#include -#include -#include - -#define PIRATE_LOADER_VERSION "1.0.2" - -#define STR_EXPAND(tok) #tok -#define OS_NAME(tok) STR_EXPAND(tok) - - -#ifdef WIN32 -#include -#include - -#define O_NOCTTY 0 -#define O_NDELAY 0 -#define B115200 921600 - -#define OS WINDOWS - -int write(int fd, const void* buf, int len) -{ - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bwritten = 0; - - - res = WriteFile(hCom, buf, len, &bwritten, NULL); - - if( res == FALSE ) - { - return -1; - } - else - { - return bwritten; - } -} - -int read(int fd, void* buf, int len) -{ - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bread = 0; - - res = ReadFile(hCom, buf, len, &bread, NULL); - - if( res == FALSE ) - { - return -1; - } - else - { - return bread; - } -} - -int close(int fd) -{ - HANDLE hCom = (HANDLE)fd; - - CloseHandle(hCom); - return 0; -} - -int open(const char* path, unsigned long flags) -{ - static char full_path[32] = {0}; - - HANDLE hCom = NULL; - - if( path[0] != '\\' ) - { - _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); - path = full_path; - } - - hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if( !hCom || hCom == INVALID_HANDLE_VALUE ) - { - return -1; - } - else - { - return (int)hCom; - } -} - -int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) -{ - time_t maxtc = time(0) + (timeout->tv_sec); - COMSTAT cs = {0}; -// JTR not used so get rig of warning unsigned long dwErrors = 0; - - if( readfds->fd_count != 1 ) - { - return -1; - } - - while( time(0) <= maxtc ) - { - //only one file supported - if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ) - { - return -1; - } - - if( cs.cbInQue > 0 ) - { - return 1; - } - - Sleep(10); - } - return 0; -} - -unsigned int sleep(unsigned int sec) -{ - Sleep(sec * 1000); - - return 0; -} - -#else -#include -#include -#include -#include -#include -#endif - -/* macro definitions */ - -/* type definitions */ - -typedef unsigned char uint8; -typedef unsigned short uint16; -typedef unsigned long uint32; - -#if !defined OS -#define OS UNKNOWN -#endif - -#define BOOTLOADER_HELLO_STR "\xC1" -#define BOOTLOADER_OK 0x4B -#define BOOTLOADER_PROT 'P' -#define PIC_WORD_SIZE (3) -#define PIC_NUM_ROWS_IN_PAGE 8 -#define PIC_NUM_WORDS_IN_ROW 64 -#define PIC_ROW_SIZE (PIC_NUM_WORDS_IN_ROW * PIC_WORD_SIZE) -#define PIC_PAGE_SIZE (PIC_NUM_ROWS_IN_PAGE * PIC_ROW_SIZE) -#define PIC_ROW_ADDR(p,r) (((p) * PIC_PAGE_SIZE) + ((r) * PIC_ROW_SIZE)) -#define PIC_WORD_ADDR(p,r,w) (PIC_ROW_ADDR(p,r) + ((w) * PIC_WORD_SIZE)) -#define PIC_PAGE_ADDR(p) (PIC_PAGE_SIZE * (p)) - -#define PAYLOAD_OFFSET 5 -#define HEADER_LENGTH PAYLOAD_OFFSET -#define LENGTH_OFFSET 4 -#define COMMAND_OFFSET 3 -#define IS_24FJ 1 -#define PIC_NUM_PAGES 512 - -//#define flashsize 0x2AC00 //was 0xac00 -//#define PIC_NUM_PAGES 512 - -//unsigned short pic_num_pages; -unsigned short family = IS_24FJ; -unsigned long flashsize = 0x2AC00; -unsigned short eesizeb = 0; -unsigned long blstartaddr = 0x400L; // not currently used -unsigned long blendaddr = 0x23FFL; // not currently used - - -/* global settings, command line arguments */ - -uint8 g_verbose = 0; -uint8 g_hello_only = 0; -uint8 g_simulate = 0; -const char* g_device_path = NULL; -const char* g_hexfile_path = NULL; - -/* functions */ - -int readWithTimeout(int fd, uint8* out, int length, int timeout) -{ - fd_set fds; - struct timeval tv = {timeout, 0}; - int res = -1; - int got = 0; - - do - { - - FD_ZERO(&fds); - FD_SET(fd, &fds); - - res = select(fd + 1, &fds, NULL, NULL, &tv); - - if( res > 0 ) - { - res = read(fd, out, length); - if( res > 0 ) - { - length -= res; - got += res; - out += res; - } - else - { - break; - } - } - else - { - return res; - } - } - while( length > 0); - - return got; -} - -unsigned char hexdec(const char* pc) -{ - unsigned char temp; - - if(pc[0]>='a') - { - temp=pc[0]-'a'+10; - } - else if(pc[0] >= 'A') - { - temp=pc[0]-'A'+10; - } - else - { - temp=pc[0] - '0'; - } - temp=temp<<4; - - if(pc[1]>='a') - { - temp|=pc[1]-'a'+10; - } - else if(pc[1] >= 'A') - { - temp|=pc[1]-'A'+10; - } - else - { - temp|=pc[1] - '0'; - } - - return(temp & 0x0FF); - - - //return (((pc[0] >= 'A') ? ( pc[0] - 'A' + 10 ) : ( pc[0] - '0' ) ) << 4 | - // ((pc[1] >= 'A') ? ( pc[1] - 'A' + 10 ) : ( pc[1] - '0' ) )) & 0x0FF; - -} - -void dumpHex(uint8* buf, uint32 len) -{ - uint32 i=0; - - for(i=0; i pline && *pc <= ' ' ) - { - *pc-- = 0; - res--; - } - - if( res & 0x01 || res > 512 || res < 10) - { - fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); - return -1; - } - - hex_crc = 0; - - for( pc = pline, i = 0; i> 1] = hexdec(pc); - hex_crc += linebin[i >> 1]; - } - - binlen = res / 2; - - if( hex_crc != 0 ) - { - fprintf(stderr, "Checksum does not match, line %d\n", line_no); - return -1; - } - - hex_addr = (linebin[1] << 8) | linebin[2]; - hex_len = linebin[0]; - hex_type = linebin[3]; - - if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) - { - fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); - return -1; - } - - if( hex_type == 0x00 ) - { - f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU - - if( hex_len % 4 ) - { - fprintf(stderr, "Misaligned data, line %d\n", line_no); - return -1; - } - else if( f_addr >= flashsize ) - { - fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); - return -1; - } - - hex_words = hex_len / 4; - o_addr = (f_addr / 2) * PIC_WORD_SIZE; //BYTES - - for( i=0; i= flashsize ) - { - fprintf(stderr, "Address out of flash\n"); - return -1; - } - - //erase page - command[0] = (u_addr & 0x00FF0000) >> 16; - command[1] = (u_addr & 0x0000FF00) >> 8; - command[2] = (u_addr & 0x000000FF) >> 0; - command[COMMAND_OFFSET] = 0x01; //erase command - command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC - command[PAYLOAD_OFFSET] = makeCrc(command, 5); - - if( g_verbose ) - { - dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); - } - - printf("Erasing page %ld, %04lx...", page, u_addr); - - if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) - { - return -1; - } - - puts("OK"); - - //write 8 rows - for( row = 0; row < PIC_NUM_ROWS_IN_PAGE; row ++, u_addr += (PIC_NUM_WORDS_IN_ROW * 2)) - { - command[0] = (u_addr & 0x00FF0000) >> 16; - command[1] = (u_addr & 0x0000FF00) >> 8; - command[2] = (u_addr & 0x000000FF) >> 0; - command[COMMAND_OFFSET] = 0x02; //write command - command[LENGTH_OFFSET ] = PIC_ROW_SIZE + 0x01; //DATA_LENGTH + CRC - - memcpy(&command[PAYLOAD_OFFSET], &data[PIC_ROW_ADDR(page, row)], PIC_ROW_SIZE); - - command[PAYLOAD_OFFSET + PIC_ROW_SIZE] = makeCrc(command, HEADER_LENGTH + PIC_ROW_SIZE); - - printf("Writing page %ld row %ld, %04lx...", page, row + page*PIC_NUM_ROWS_IN_PAGE, u_addr); - - if( g_verbose ) - { - dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); - } - - if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) - { - return -1; - } - - puts("OK"); - - sleep(0); - - - done += PIC_ROW_SIZE; - } - } - - return done; -} - - -/* non-firmware functions */ - -int configurePort(int fd, unsigned long baudrate) -{ -#ifdef WIN32 - DCB dcb = {0}; - HANDLE hCom = (HANDLE)fd; - - dcb.DCBlength = sizeof(dcb); - - dcb.BaudRate = baudrate; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - if( !SetCommState(hCom, &dcb) ) - { - return -1; - } - - return (int)hCom; -#else - struct termios g_new_tio; - - memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); - cfmakeraw(&g_new_tio); - - g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); - g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); - g_new_tio.c_oflag = 0; - g_new_tio.c_lflag = 0; - - - g_new_tio.c_cc[VTIME] = 0; - g_new_tio.c_cc[VMIN] = 1; - - cfsetispeed (&g_new_tio, baudrate); - cfsetospeed (&g_new_tio, baudrate); - - tcflush(fd, TCIOFLUSH); - - return tcsetattr(fd, TCSANOW, &g_new_tio); -#endif -} - -int openPort(const char* dev, unsigned long flags) -{ - return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); -} - -int parseCommandLine(int argc, const char** argv) -{ - int i = 0; - - for(i=1; i flashsize ) - { - fprintf(stderr, "Could not load HEX file, result=%d\n", res); - goto Error; - } - - printf("Found %d words (%d bytes)\n", res, res * 3); - - //printf("Fixing bootloader/userprogram jumps\n"); - //fixJumps(bin_buff, pages_used); - } - - if( g_simulate ) - { - sendFirmware(dev_fd, bin_buff, pages_used); - goto Finished; - } - - if( !g_device_path ) - { - fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); - return -1; - } - - printf("Opening serial device %s...", g_device_path); - - dev_fd = openPort(g_device_path, 0); - - if( dev_fd < 0 ) - { - puts("ERROR"); - fprintf(stderr, "Could not open %s\n", g_device_path); - goto Error; - } - puts("OK"); - - printf("Configuring serial port settings..."); - - if( configurePort(dev_fd, B115200) < 0 ) - { - puts("ERROR"); - fprintf(stderr, "Could not configure device, errno=%d\n", errno); - goto Error; - } - puts("OK"); - - printf("Sending Hello to the Bootloader..."); - - //send HELLO - res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); - - res = readWithTimeout(dev_fd, buffer, 4, 3); - - if( res != 4 || buffer[3] != BOOTLOADER_OK ) - { - puts("ERROR"); - fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); - fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); - goto Error; - } - puts("OK\n"); //extra LF for spacing - - printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); - - printf("Device ID [%02x]:",buffer[0]); - switch(buffer[0]) - { -// case 0xd4: -// printf("PIC24FJ64GA002\n"); -// break; - case 9: - printf("PIC24FJ128GB206\n"); - // __PIC24FJ128GB206__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 17: - printf("PIC24FJ128GB210\n"); -// __PIC24FJ128GB210__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - - break; - case 18: - printf("PIC24FJ256GB206\n"); - // __PIC24FJ256GB206__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 19: - printf("PIC24FJ256GB210\n"); - // __PIC24FJ256GB210__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 191: - printf("PIC24FJ256DA206\n"); - // __PIC24FJ256DA206__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 192: - printf("PIC24FJ256DA210\n"); - // __PIC24FJ256DA210__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 217: - printf("PIC24FJ64GB106\n"); - // __PIC24FJ64GB106__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 218: - printf("PIC24FJ64GB108\n"); - // __PIC24FJ64GB108__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 219: - printf("PIC24FJ64GB110\n"); - // __PIC24FJ64GB110__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 229: - printf("PIC24FJ128GB106\n"); - // __PIC24FJ128GB106__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 230: - printf("PIC24FJ128GB108\n"); - // __PIC24FJ128GB108__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 231: - printf("PIC24FJ128GB110\n"); - // __PIC24FJ128GB110__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 235: - printf("PIC24FJ192GB106\n"); - // __PIC24FJ192GB106__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 236: - printf("PIC24FJ192GB108\n"); - // __PIC24FJ192GB108__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 237: - printf("PIC24FJ192GB110\n"); - // __PIC24FJ192GB110__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 241: - printf("PIC24FJ256GB106\n"); - // __PIC24FJ256GB106__ -#define IS_24FJ 1 - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 242: - printf("PIC24FJ256GB108\n"); - // __PIC24FJ256GB108__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 243: - printf("PIC24FJ256GB110\n"); - // __PIC24FJ256GB110__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 244: - printf("PIC24FJ32GB002\n"); - // __PIC24FJ32GB002__ - family = IS_24FJ; - flashsize = 0x5800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 245: - printf("PIC24FJ32GB004\n"); - // __PIC24FJ32GB004__ - family = IS_24FJ; - flashsize = 0x5800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 246: - printf("PIC24FJ64GB002\n"); - // __PIC24FJ64GB002__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 247: - printf("PIC24FJ64GB004\n"); - // __PIC24FJ64GB004__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 250: - printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 251: - printf("PIC24FJ128DA110\n"); - // __PIC24FJ128DA110__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 252: - printf("PIC24FJ128DA206\n"); - // __PIC24FJ128DA206__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 253: - printf("PIC24FJ128DA210\n"); - // __PIC24FJ128DA210__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 254: - printf("PIC24FJ256DA106\n"); - // __PIC24FJ256DA106__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 255: - printf("PIC24FJ256DA110\n"); - // __PIC24FJ256DA110__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 206: - printf("PIC24FJ16GA002\n"); - flashsize = 0x2C00; - - break; - - case 207: - printf("PIC24FJ16GA004\n"); - flashsize = 0x2C00; - - break; - - - case 208: - printf("PIC24FJ32GA002\n"); - flashsize = 0x5800; - - break; - - case 209: - printf("PIC24FJ32GA004\n"); - flashsize = 0x5800; - - break; - - case 210: - printf("PIC24FJ48GA002\n"); - flashsize = 0x8400; - - break; - - case 211: - printf("PIC24FJ48GA004\n"); - flashsize = 0x8400; - - break; - - - case 212: - printf("PIC24FJ64GA002\n"); - flashsize = 0xAC00; - - break; - - case 213: - printf("PIC24FJ64GA004\n"); - flashsize = 0xAC00; - - break; - - case 214: - printf("PIC24FJ64GA006\n"); - flashsize = 0xAC00; - - break; - - printf("PIC24FJ64GA008\n"); - flashsize = 0xAC00; - case 215: - break; - - case 216: - printf("PIC24FJ64GA010\n"); - flashsize = 0xAC00; - - break; - - case 220: - printf("PIC24FJ96GA006\n"); - flashsize = 0x10000; - - break; - - case 221: - printf("PIC24FJ96GA008\n"); - flashsize = 0x10000; - - break; - - case 222: - printf("PIC24FJ96GA010\n"); - flashsize = 0x10000; - - break; - - - case 223: - printf("PIC24FJ128GA006\n"); - flashsize = 0x15800; - - break; - - case 224: - printf("PIC24FJ128GA008\n"); - flashsize = 0x15800; - - break; - - case 225: - printf("PIC24FJ128GA010\n"); - flashsize = 0x15800; - - break; - - case 226: - printf("PIC24FJ128GA106\n"); - flashsize = 0x15800; - - break; - - case 227: - printf("PIC24FJ128GA108\n"); - flashsize = 0x15800; - break; - - case 228: - printf("PIC24FJ128GA110\n"); - flashsize = 0x15800; - break; - - case 232: - printf("PIC24FJ192GA106\n"); - flashsize = 0x20C00; - break; - - case 233: - printf("PIC24FJ192GA108\n"); - flashsize = 0x20C00; - break; - - case 234: - printf("PIC24FJ192GA110\n"); - flashsize = 0x20C00; - break; - - case 238: - printf("PIC24FJ256GA106\n"); - flashsize = 0x2AC00; - - break; - - case 239: - printf("PIC24FJ256GA108\n"); - flashsize = 0x2AC00; - break; - - case 240: - printf("PIC24FJ256GA110\n"); - flashsize = 0x2AC00; - break; - - - default: - printf("UNKNOWN"); - fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); - goto Error; - break; - } - - if( !g_hello_only ) - { - - res = sendFirmware(dev_fd, bin_buff, pages_used); - - if( res > 0 ) - { - puts("\nFirmware updated successfully :)!"); - //printf("Use screen %s 115200 to verify\n", g_device_path); - } - else - { - puts("\nError updating firmware :("); - goto Error; - } - - } - -Finished: - if( bin_buff ) - { - free( bin_buff ); - } - if( dev_fd >= 0 ) - { - close(dev_fd); - } - return 0; - -Error: - if( bin_buff ) - { - free( bin_buff ); - } - if( dev_fd >= 0 ) - { - close(dev_fd); - } - return -1; -} +/* + + Pirate-Loader for Bootloader v4 + + Version : 1.0.2 + + Changelog: + +2010-06-28 - Made HEX parser case-insensative + + + 2010-02-04 - Changed polling interval to 10ms on Windows select wrapper, suggested by Michal (robots) + + + 2010-02-04 - Added sleep(0) between write instructions, patch submitted by kbulgrien + + + 2010-01-22 - Added loader version number to the console output and source code + + + 2010-01-19 - Fixed BigEndian incompatibility + - Added programming simulate switch ( --simulate ) for data verification + + + 2010-01-18 - Initial release + + + Building: + + UNIX family systems: + + gcc pirate-loader.c -o pirate-loader + + WINDOWS: + + cl pirate-loader.c /DWIN32=1 + + + Usage: + + Run ./pirate-loader --help for more information on usage and possible switches + + */ + +#include +#include +#include +#include +#include + +#define PIRATE_LOADER_VERSION "1.0.2" + +#define STR_EXPAND(tok) #tok +#define OS_NAME(tok) STR_EXPAND(tok) + + +#ifdef WIN32 +#include +#include + +#define O_NOCTTY 0 +#define O_NDELAY 0 +#define B115200 921600 + +#define OS WINDOWS + +int write(int fd, const void* buf, int len) +{ + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bwritten = 0; + + + res = WriteFile(hCom, buf, len, &bwritten, NULL); + + if( res == FALSE ) + { + return -1; + } + else + { + return bwritten; + } +} + +int read(int fd, void* buf, int len) +{ + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bread = 0; + + res = ReadFile(hCom, buf, len, &bread, NULL); + + if( res == FALSE ) + { + return -1; + } + else + { + return bread; + } +} + +int close(int fd) +{ + HANDLE hCom = (HANDLE)fd; + + CloseHandle(hCom); + return 0; +} + +int open(const char* path, unsigned long flags) +{ + static char full_path[32] = {0}; + + HANDLE hCom = NULL; + + if( path[0] != '\\' ) + { + _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); + path = full_path; + } + + hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if( !hCom || hCom == INVALID_HANDLE_VALUE ) + { + return -1; + } + else + { + return (int)hCom; + } +} + +int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) +{ + time_t maxtc = time(0) + (timeout->tv_sec); + COMSTAT cs = {0}; +// JTR not used so get rig of warning unsigned long dwErrors = 0; + + if( readfds->fd_count != 1 ) + { + return -1; + } + + while( time(0) <= maxtc ) + { + //only one file supported + if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ) + { + return -1; + } + + if( cs.cbInQue > 0 ) + { + return 1; + } + + Sleep(10); + } + return 0; +} + +unsigned int sleep(unsigned int sec) +{ + Sleep(sec * 1000); + + return 0; +} + +#else +#include +#include +#include +#include +#include +#endif + +/* macro definitions */ + +/* type definitions */ + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned long uint32; + +#if !defined OS +#define OS UNKNOWN +#endif + +#define BOOTLOADER_HELLO_STR "\xC1" +#define BOOTLOADER_OK 0x4B +#define BOOTLOADER_PROT 'P' +#define PIC_WORD_SIZE (3) +#define PIC_NUM_ROWS_IN_PAGE 8 +#define PIC_NUM_WORDS_IN_ROW 64 +#define PIC_ROW_SIZE (PIC_NUM_WORDS_IN_ROW * PIC_WORD_SIZE) +#define PIC_PAGE_SIZE (PIC_NUM_ROWS_IN_PAGE * PIC_ROW_SIZE) +#define PIC_ROW_ADDR(p,r) (((p) * PIC_PAGE_SIZE) + ((r) * PIC_ROW_SIZE)) +#define PIC_WORD_ADDR(p,r,w) (PIC_ROW_ADDR(p,r) + ((w) * PIC_WORD_SIZE)) +#define PIC_PAGE_ADDR(p) (PIC_PAGE_SIZE * (p)) + +#define PAYLOAD_OFFSET 5 +#define HEADER_LENGTH PAYLOAD_OFFSET +#define LENGTH_OFFSET 4 +#define COMMAND_OFFSET 3 +#define IS_24FJ 1 +#define PIC_NUM_PAGES 512 + +//#define flashsize 0x2AC00 //was 0xac00 +//#define PIC_NUM_PAGES 512 + +//unsigned short pic_num_pages; +unsigned short family = IS_24FJ; +unsigned long flashsize = 0x2AC00; +unsigned short eesizeb = 0; +unsigned long blstartaddr = 0x400L; // not currently used +unsigned long blendaddr = 0x23FFL; // not currently used + + +/* global settings, command line arguments */ + +uint8 g_verbose = 0; +uint8 g_hello_only = 0; +uint8 g_simulate = 0; +const char* g_device_path = NULL; +const char* g_hexfile_path = NULL; + +/* functions */ + +int readWithTimeout(int fd, uint8* out, int length, int timeout) +{ + fd_set fds; + struct timeval tv = {timeout, 0}; + int res = -1; + int got = 0; + + do + { + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + res = select(fd + 1, &fds, NULL, NULL, &tv); + + if( res > 0 ) + { + res = read(fd, out, length); + if( res > 0 ) + { + length -= res; + got += res; + out += res; + } + else + { + break; + } + } + else + { + return res; + } + } + while( length > 0); + + return got; +} + +unsigned char hexdec(const char* pc) +{ + unsigned char temp; + + if(pc[0]>='a') + { + temp=pc[0]-'a'+10; + } + else if(pc[0] >= 'A') + { + temp=pc[0]-'A'+10; + } + else + { + temp=pc[0] - '0'; + } + temp=temp<<4; + + if(pc[1]>='a') + { + temp|=pc[1]-'a'+10; + } + else if(pc[1] >= 'A') + { + temp|=pc[1]-'A'+10; + } + else + { + temp|=pc[1] - '0'; + } + + return(temp & 0x0FF); + + + //return (((pc[0] >= 'A') ? ( pc[0] - 'A' + 10 ) : ( pc[0] - '0' ) ) << 4 | + // ((pc[1] >= 'A') ? ( pc[1] - 'A' + 10 ) : ( pc[1] - '0' ) )) & 0x0FF; + +} + +void dumpHex(uint8* buf, uint32 len) +{ + uint32 i=0; + + for(i=0; i pline && *pc <= ' ' ) + { + *pc-- = 0; + res--; + } + + if( res & 0x01 || res > 512 || res < 10) + { + fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); + return -1; + } + + hex_crc = 0; + + for( pc = pline, i = 0; i> 1] = hexdec(pc); + hex_crc += linebin[i >> 1]; + } + + binlen = res / 2; + + if( hex_crc != 0 ) + { + fprintf(stderr, "Checksum does not match, line %d\n", line_no); + return -1; + } + + hex_addr = (linebin[1] << 8) | linebin[2]; + hex_len = linebin[0]; + hex_type = linebin[3]; + + if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) + { + fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); + return -1; + } + + if( hex_type == 0x00 ) + { + f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU + + if( hex_len % 4 ) + { + fprintf(stderr, "Misaligned data, line %d\n", line_no); + return -1; + } + else if( f_addr >= flashsize ) + { + fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); + return -1; + } + + hex_words = hex_len / 4; + o_addr = (f_addr / 2) * PIC_WORD_SIZE; //BYTES + + for( i=0; i= flashsize ) + { + fprintf(stderr, "Address out of flash\n"); + return -1; + } + + //erase page + command[0] = (u_addr & 0x00FF0000) >> 16; + command[1] = (u_addr & 0x0000FF00) >> 8; + command[2] = (u_addr & 0x000000FF) >> 0; + command[COMMAND_OFFSET] = 0x01; //erase command + command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC + command[PAYLOAD_OFFSET] = makeCrc(command, 5); + + if( g_verbose ) + { + dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); + } + + printf("Erasing page %ld, %04lx...", page, u_addr); + + if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) + { + return -1; + } + + puts("OK"); + + //write 8 rows + for( row = 0; row < PIC_NUM_ROWS_IN_PAGE; row ++, u_addr += (PIC_NUM_WORDS_IN_ROW * 2)) + { + command[0] = (u_addr & 0x00FF0000) >> 16; + command[1] = (u_addr & 0x0000FF00) >> 8; + command[2] = (u_addr & 0x000000FF) >> 0; + command[COMMAND_OFFSET] = 0x02; //write command + command[LENGTH_OFFSET ] = PIC_ROW_SIZE + 0x01; //DATA_LENGTH + CRC + + memcpy(&command[PAYLOAD_OFFSET], &data[PIC_ROW_ADDR(page, row)], PIC_ROW_SIZE); + + command[PAYLOAD_OFFSET + PIC_ROW_SIZE] = makeCrc(command, HEADER_LENGTH + PIC_ROW_SIZE); + + printf("Writing page %ld row %ld, %04lx...", page, row + page*PIC_NUM_ROWS_IN_PAGE, u_addr); + + if( g_verbose ) + { + dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); + } + + if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) + { + return -1; + } + + puts("OK"); + + sleep(0); + + + done += PIC_ROW_SIZE; + } + } + + return done; +} + + +/* non-firmware functions */ + +int configurePort(int fd, unsigned long baudrate) +{ +#ifdef WIN32 + DCB dcb = {0}; + HANDLE hCom = (HANDLE)fd; + + dcb.DCBlength = sizeof(dcb); + + dcb.BaudRate = baudrate; + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + if( !SetCommState(hCom, &dcb) ) + { + return -1; + } + + return (int)hCom; +#else + struct termios g_new_tio; + + memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); + cfmakeraw(&g_new_tio); + + g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); + g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); + g_new_tio.c_oflag = 0; + g_new_tio.c_lflag = 0; + + + g_new_tio.c_cc[VTIME] = 0; + g_new_tio.c_cc[VMIN] = 1; + + cfsetispeed (&g_new_tio, baudrate); + cfsetospeed (&g_new_tio, baudrate); + + tcflush(fd, TCIOFLUSH); + + return tcsetattr(fd, TCSANOW, &g_new_tio); +#endif +} + +int openPort(const char* dev, unsigned long flags) +{ + return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); +} + +int parseCommandLine(int argc, const char** argv) +{ + int i = 0; + + for(i=1; i flashsize ) + { + fprintf(stderr, "Could not load HEX file, result=%d\n", res); + goto Error; + } + + printf("Found %d words (%d bytes)\n", res, res * 3); + + //printf("Fixing bootloader/userprogram jumps\n"); + //fixJumps(bin_buff, pages_used); + } + + if( g_simulate ) + { + sendFirmware(dev_fd, bin_buff, pages_used); + goto Finished; + } + + if( !g_device_path ) + { + fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); + return -1; + } + + printf("Opening serial device %s...", g_device_path); + + dev_fd = openPort(g_device_path, 0); + + if( dev_fd < 0 ) + { + puts("ERROR"); + fprintf(stderr, "Could not open %s\n", g_device_path); + goto Error; + } + puts("OK"); + + printf("Configuring serial port settings..."); + + if( configurePort(dev_fd, B115200) < 0 ) + { + puts("ERROR"); + fprintf(stderr, "Could not configure device, errno=%d\n", errno); + goto Error; + } + puts("OK"); + + printf("Sending Hello to the Bootloader..."); + + //send HELLO + res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); + + res = readWithTimeout(dev_fd, buffer, 4, 3); + + if( res != 4 || buffer[3] != BOOTLOADER_OK ) + { + puts("ERROR"); + fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); + fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); + goto Error; + } + puts("OK\n"); //extra LF for spacing + + printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); + + printf("Device ID [%02x]:",buffer[0]); + switch(buffer[0]) + { +// case 0xd4: +// printf("PIC24FJ64GA002\n"); +// break; + case 9: + printf("PIC24FJ128GB206\n"); + // __PIC24FJ128GB206__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 17: + printf("PIC24FJ128GB210\n"); +// __PIC24FJ128GB210__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + + break; + case 18: + printf("PIC24FJ256GB206\n"); + // __PIC24FJ256GB206__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 19: + printf("PIC24FJ256GB210\n"); + // __PIC24FJ256GB210__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 191: + printf("PIC24FJ256DA206\n"); + // __PIC24FJ256DA206__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 192: + printf("PIC24FJ256DA210\n"); + // __PIC24FJ256DA210__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 217: + printf("PIC24FJ64GB106\n"); + // __PIC24FJ64GB106__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 218: + printf("PIC24FJ64GB108\n"); + // __PIC24FJ64GB108__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 219: + printf("PIC24FJ64GB110\n"); + // __PIC24FJ64GB110__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 229: + printf("PIC24FJ128GB106\n"); + // __PIC24FJ128GB106__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 230: + printf("PIC24FJ128GB108\n"); + // __PIC24FJ128GB108__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 231: + printf("PIC24FJ128GB110\n"); + // __PIC24FJ128GB110__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 235: + printf("PIC24FJ192GB106\n"); + // __PIC24FJ192GB106__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 236: + printf("PIC24FJ192GB108\n"); + // __PIC24FJ192GB108__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 237: + printf("PIC24FJ192GB110\n"); + // __PIC24FJ192GB110__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 241: + printf("PIC24FJ256GB106\n"); + // __PIC24FJ256GB106__ +#define IS_24FJ 1 + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 242: + printf("PIC24FJ256GB108\n"); + // __PIC24FJ256GB108__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 243: + printf("PIC24FJ256GB110\n"); + // __PIC24FJ256GB110__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 244: + printf("PIC24FJ32GB002\n"); + // __PIC24FJ32GB002__ + family = IS_24FJ; + flashsize = 0x5800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 245: + printf("PIC24FJ32GB004\n"); + // __PIC24FJ32GB004__ + family = IS_24FJ; + flashsize = 0x5800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 246: + printf("PIC24FJ64GB002\n"); + // __PIC24FJ64GB002__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 247: + printf("PIC24FJ64GB004\n"); + // __PIC24FJ64GB004__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 250: + printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 251: + printf("PIC24FJ128DA110\n"); + // __PIC24FJ128DA110__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 252: + printf("PIC24FJ128DA206\n"); + // __PIC24FJ128DA206__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 253: + printf("PIC24FJ128DA210\n"); + // __PIC24FJ128DA210__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 254: + printf("PIC24FJ256DA106\n"); + // __PIC24FJ256DA106__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 255: + printf("PIC24FJ256DA110\n"); + // __PIC24FJ256DA110__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 206: + printf("PIC24FJ16GA002\n"); + flashsize = 0x2C00; + + break; + + case 207: + printf("PIC24FJ16GA004\n"); + flashsize = 0x2C00; + + break; + + + case 208: + printf("PIC24FJ32GA002\n"); + flashsize = 0x5800; + + break; + + case 209: + printf("PIC24FJ32GA004\n"); + flashsize = 0x5800; + + break; + + case 210: + printf("PIC24FJ48GA002\n"); + flashsize = 0x8400; + + break; + + case 211: + printf("PIC24FJ48GA004\n"); + flashsize = 0x8400; + + break; + + + case 212: + printf("PIC24FJ64GA002\n"); + flashsize = 0xAC00; + + break; + + case 213: + printf("PIC24FJ64GA004\n"); + flashsize = 0xAC00; + + break; + + case 214: + printf("PIC24FJ64GA006\n"); + flashsize = 0xAC00; + + break; + + printf("PIC24FJ64GA008\n"); + flashsize = 0xAC00; + case 215: + break; + + case 216: + printf("PIC24FJ64GA010\n"); + flashsize = 0xAC00; + + break; + + case 220: + printf("PIC24FJ96GA006\n"); + flashsize = 0x10000; + + break; + + case 221: + printf("PIC24FJ96GA008\n"); + flashsize = 0x10000; + + break; + + case 222: + printf("PIC24FJ96GA010\n"); + flashsize = 0x10000; + + break; + + + case 223: + printf("PIC24FJ128GA006\n"); + flashsize = 0x15800; + + break; + + case 224: + printf("PIC24FJ128GA008\n"); + flashsize = 0x15800; + + break; + + case 225: + printf("PIC24FJ128GA010\n"); + flashsize = 0x15800; + + break; + + case 226: + printf("PIC24FJ128GA106\n"); + flashsize = 0x15800; + + break; + + case 227: + printf("PIC24FJ128GA108\n"); + flashsize = 0x15800; + break; + + case 228: + printf("PIC24FJ128GA110\n"); + flashsize = 0x15800; + break; + + case 232: + printf("PIC24FJ192GA106\n"); + flashsize = 0x20C00; + break; + + case 233: + printf("PIC24FJ192GA108\n"); + flashsize = 0x20C00; + break; + + case 234: + printf("PIC24FJ192GA110\n"); + flashsize = 0x20C00; + break; + + case 238: + printf("PIC24FJ256GA106\n"); + flashsize = 0x2AC00; + + break; + + case 239: + printf("PIC24FJ256GA108\n"); + flashsize = 0x2AC00; + break; + + case 240: + printf("PIC24FJ256GA110\n"); + flashsize = 0x2AC00; + break; + + + default: + printf("UNKNOWN"); + fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); + goto Error; + break; + } + + if( !g_hello_only ) + { + + res = sendFirmware(dev_fd, bin_buff, pages_used); + + if( res > 0 ) + { + puts("\nFirmware updated successfully :)!"); + //printf("Use screen %s 115200 to verify\n", g_device_path); + } + else + { + puts("\nError updating firmware :("); + goto Error; + } + + } + +Finished: + if( bin_buff ) + { + free( bin_buff ); + } + if( dev_fd >= 0 ) + { + close(dev_fd); + } + return 0; + +Error: + if( bin_buff ) + { + free( bin_buff ); + } + if( dev_fd >= 0 ) + { + close(dev_fd); + } + return -1; +} diff --git a/Firmware-Demos/ADC2UART/main.c b/Firmware-Demos/ADC2UART/main.c index 9cd56ce9..ae4ab77e 100644 --- a/Firmware-Demos/ADC2UART/main.c +++ b/Firmware-Demos/ADC2UART/main.c @@ -1,115 +1,115 @@ -#include - -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need - -#define VREG_DIR TRISAbits.TRISA0 //sets VREG33_DIR to TRISBIT A0 -#define VREG_EN LATAbits.LATA0 //sets VREG_EN to LAT bit A0 (output latch) -#define MODE_LED_DIR TRISAbits.TRISA1 //sets the MODE_LED_DIR to TRISBIT A1 -#define MODE_LED LATAbits.LATA1 //sets the MODE_LED to LAT bit A1 (output latch) - -#pragma code - - -void initADC(); -unsigned int getADC(); -unsigned char UART1RX(void); -void UART1TX(char c); -void InitializeUART1(void); - -//this loop services user input and passes it to be processed on -int main(void){ - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; //Oscilator setup, registar-OSCON bits-SOSCEN - MODE_LED_DIR = 0; //Mode LED set as output - VREG_DIR =0; //VREG is set as output - VREG_EN = 1; //Sets the VREG pin to 1, turns on the Voltage regulators. - MODE_LED = 1; //Turns the MOD LED 0N - - InitializeUART1(); //Initialize UART1 - initADC(); //Initialise ADC - - unsigned int voltage; - - /////FOREVER///LOOP////////// - while(1) - { - if(UART1RX() == 'a') //Checks if the recived byte is 'a' if so it sends the two RAW bytes form ADC - { - voltage = getADC(); //reads the ADC pin - UART1TX(voltage>>8); //seds the top byte of ADC to UART - UART1TX(voltage); //sends the bottom byte of ADC to UART - } - } -} - -//////////////////////////////////////////////////////////////////////////////////// -/////Analog to digital converter intitilisationn function/////////////////////////// -/////Setch the ADC to read the AN12 chanel, whic is connected ot the BPv3 pin ADC/// -//////////////////////////////////////////////////////////////////////////////////// -void initADC() -{ -////////////////////////////////////////////////////////////// -///////////////ADC config////////////////////////////////// - AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT - - //configure the ADC - AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - AD1CSSL = 0; - AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - AD1CON2 = 0; //CSCNA =0 -does not scan inputs, - //BUFS =0 -A/D is currently filling buffer 00-07, - //SMPI=0 -Interrupts at the completion of conversion for each sample/convert sequence, - //BUFM =0 -Buffer configured as one 16-word buffer, - //ALTS = 0 -Always uses MUX A input multiplexer settings - - AD1CHS =12; //chanell selected 12, this is the AN12 pin which is conected to the voltage devider and the ADC pin on the BPv3 -///////////////////////////////////////////////////////////////// -} - -unsigned int getADC() -{ - AD1CON1bits.ADON =1; //turns the ADC module on - AD1CON1bits.SAMP=1; //start sample - AD1CON1bits.DONE=0; //starts conversion - while(AD1CON1bits.DONE==0);//wait for conversion to finish - AD1CON1bits.ADON =0; //turns the ADC module off - return ADC1BUF0; -} - - -//get a byte from UART -unsigned char UART1RX(void){ - - while(U1STAbits.URXDA == 0); - return U1RXREG; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -void InitializeUART1(void){ - - TRISBbits.TRISB4=1; //UART 1 RX set as input - //set pin configuration using peripheral pin select - RPINR18bits.U1RXR = 5; // UART1 RX assign to RP5 - RPOR2bits.RP4R = 3; //UART1 TX assign to RP4 - - U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; +#include + +_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need + +#define VREG_DIR TRISAbits.TRISA0 //sets VREG33_DIR to TRISBIT A0 +#define VREG_EN LATAbits.LATA0 //sets VREG_EN to LAT bit A0 (output latch) +#define MODE_LED_DIR TRISAbits.TRISA1 //sets the MODE_LED_DIR to TRISBIT A1 +#define MODE_LED LATAbits.LATA1 //sets the MODE_LED to LAT bit A1 (output latch) + +#pragma code + + +void initADC(); +unsigned int getADC(); +unsigned char UART1RX(void); +void UART1TX(char c); +void InitializeUART1(void); + +//this loop services user input and passes it to be processed on +int main(void){ + + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; //Oscilator setup, registar-OSCON bits-SOSCEN + MODE_LED_DIR = 0; //Mode LED set as output + VREG_DIR =0; //VREG is set as output + VREG_EN = 1; //Sets the VREG pin to 1, turns on the Voltage regulators. + MODE_LED = 1; //Turns the MOD LED 0N + + InitializeUART1(); //Initialize UART1 + initADC(); //Initialise ADC + + unsigned int voltage; + + /////FOREVER///LOOP////////// + while(1) + { + if(UART1RX() == 'a') //Checks if the recived byte is 'a' if so it sends the two RAW bytes form ADC + { + voltage = getADC(); //reads the ADC pin + UART1TX(voltage>>8); //seds the top byte of ADC to UART + UART1TX(voltage); //sends the bottom byte of ADC to UART + } + } +} + +//////////////////////////////////////////////////////////////////////////////////// +/////Analog to digital converter intitilisationn function/////////////////////////// +/////Setch the ADC to read the AN12 chanel, whic is connected ot the BPv3 pin ADC/// +//////////////////////////////////////////////////////////////////////////////////// +void initADC() +{ +////////////////////////////////////////////////////////////// +///////////////ADC config////////////////////////////////// + AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT + + //configure the ADC + AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + AD1CSSL = 0; + AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + AD1CON2 = 0; //CSCNA =0 -does not scan inputs, + //BUFS =0 -A/D is currently filling buffer 00-07, + //SMPI=0 -Interrupts at the completion of conversion for each sample/convert sequence, + //BUFM =0 -Buffer configured as one 16-word buffer, + //ALTS = 0 -Always uses MUX A input multiplexer settings + + AD1CHS =12; //chanell selected 12, this is the AN12 pin which is conected to the voltage devider and the ADC pin on the BPv3 +///////////////////////////////////////////////////////////////// +} + +unsigned int getADC() +{ + AD1CON1bits.ADON =1; //turns the ADC module on + AD1CON1bits.SAMP=1; //start sample + AD1CON1bits.DONE=0; //starts conversion + while(AD1CON1bits.DONE==0);//wait for conversion to finish + AD1CON1bits.ADON =0; //turns the ADC module off + return ADC1BUF0; +} + + +//get a byte from UART +unsigned char UART1RX(void){ + + while(U1STAbits.URXDA == 0); + return U1RXREG; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +void InitializeUART1(void){ + + TRISBbits.TRISB4=1; //UART 1 RX set as input + //set pin configuration using peripheral pin select + RPINR18bits.U1RXR = 5; // UART1 RX assign to RP5 + RPOR2bits.RP4R = 3; //UART1 TX assign to RP4 + + U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; } \ No newline at end of file diff --git a/Firmware-Demos/Blink_MODE_LED/main.c b/Firmware-Demos/Blink_MODE_LED/main.c index 5045daea..c4fb12e4 100644 --- a/Firmware-Demos/Blink_MODE_LED/main.c +++ b/Firmware-Demos/Blink_MODE_LED/main.c @@ -1,38 +1,38 @@ -#include - -_CONFIG1( JTAGEN_OFF & //JTAG port is disabled - GCP_OFF & //GSP Memory Code Protection OFF - GWRP_OFF & //GCC Flash Write Protection OFF - COE_OFF & // - FWDTEN_OFF & //Watchdog Timer OFF - ICS_PGx1) //debug over PGD1 and PGC1 - -_CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL - OSCIOFNC_ON & //RA3 is clk out (fosc/2) - POSCMOD_NONE & //Primary oscillator disabled - I2C1SEL_PRI) //Use default SCL1/SDA1 pins - - -#pragma code - -int main(void) -{ - unsigned long i; //a 32 bit variable to use as a timer - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; //Disables the secondary oscilator - - TRISAbits.TRISA1 = 0; //sets the Mode LED pin RA1 as output - LATAbits.LATA1 = 0; //turns LED off - -////////////////////////////////////////////////////////////////// -///FOREVER LOOP/////////////////////////////////////////////////// -////////////////////////////////////////////////////////////////// - while(1) - { - LATAbits.LATA1 = ~LATAbits.LATA1; //toggles the MODE LED - i =0xFFFFF; //sets i to 1048575 - while (i--); //delay function - } //decrements i until 0 +#include + +_CONFIG1( JTAGEN_OFF & //JTAG port is disabled + GCP_OFF & //GSP Memory Code Protection OFF + GWRP_OFF & //GCC Flash Write Protection OFF + COE_OFF & // + FWDTEN_OFF & //Watchdog Timer OFF + ICS_PGx1) //debug over PGD1 and PGC1 + +_CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL + OSCIOFNC_ON & //RA3 is clk out (fosc/2) + POSCMOD_NONE & //Primary oscillator disabled + I2C1SEL_PRI) //Use default SCL1/SDA1 pins + + +#pragma code + +int main(void) +{ + unsigned long i; //a 32 bit variable to use as a timer + + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; //Disables the secondary oscilator + + TRISAbits.TRISA1 = 0; //sets the Mode LED pin RA1 as output + LATAbits.LATA1 = 0; //turns LED off + +////////////////////////////////////////////////////////////////// +///FOREVER LOOP/////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////// + while(1) + { + LATAbits.LATA1 = ~LATAbits.LATA1; //toggles the MODE LED + i =0xFFFFF; //sets i to 1048575 + while (i--); //delay function + } //decrements i until 0 } \ No newline at end of file diff --git a/Firmware-Demos/BusPirateADCDemo/main.c b/Firmware-Demos/BusPirateADCDemo/main.c index 04e67194..656f874c 100644 --- a/Firmware-Demos/BusPirateADCDemo/main.c +++ b/Firmware-Demos/BusPirateADCDemo/main.c @@ -1,65 +1,65 @@ -#include - -_CONFIG1( JTAGEN_OFF & //JTAG port is disabled - GCP_OFF & //GSP Memory Code Protection OFF - GWRP_OFF & //GCC Flash Write Protection OFF - COE_OFF & // - FWDTEN_OFF & //Watchdog Timer OFF - ICS_PGx1) //debug over PGD1 and PGC1 - -_CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL - OSCIOFNC_ON & //RA3 is clk out (fosc/2) - POSCMOD_NONE & //Primary oscillator disabled - I2C1SEL_PRI) //Use default SCL1/SDA1 pins - -#define VREG33_DIR TRISAbits.TRISA0 -#define VREG33_EN LATAbits.LATA0 -#define MODE_LED_DIR TRISAbits.TRISA1 -#define MODE_LED LATAbits.LATA1 - - -#pragma code - -int main(void) -{ - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; //Disables the secondary oscilator - - MODE_LED_DIR = 0; //sets the Mode LED pin RA1 as output - MODE_LED = 0; //turns LED off - VREG33_DIR =0; //sets teh VREG pin RA0 as output - VREG33_EN = 1; //turns on the voltage regulator - - unsigned long int i,voltage; -////////////////////////////////////////////////////////////// -///////////////ADC config////////////////////////////////// - AD1PCFGbits.PCFG12=0; //configure RB12 as analog - AD1CON1bits.SSRC = 0b111; // SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - AD1CHS =12; //ADC channel select 12 - AD1CON1bits.ADON =1; // turn ADC on -///FOREVER LOOP/////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////// - while(1) - { - //this just gives us a little delay between measurements - i =0xFFFFF; //sets i to 1048575 - while (i--); //delay function - - //start a measurement with the ADC - AD1CON1bits.DONE=0; //resets DONE bit - AD1CON1bits.SAMP=1; //start sample - - while(AD1CON1bits.DONE==0); //wait for conversion to finish - - //get the measurement and use it to control the LED - voltage = ADC1BUF0; //get the voltage measurement - if (voltage > 0x1D1) MODE_LED = 1; //enable LED if measurement is > 3volts - else MODE_LED = 0; //disable LED if less than 3volts - } +#include + +_CONFIG1( JTAGEN_OFF & //JTAG port is disabled + GCP_OFF & //GSP Memory Code Protection OFF + GWRP_OFF & //GCC Flash Write Protection OFF + COE_OFF & // + FWDTEN_OFF & //Watchdog Timer OFF + ICS_PGx1) //debug over PGD1 and PGC1 + +_CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL + OSCIOFNC_ON & //RA3 is clk out (fosc/2) + POSCMOD_NONE & //Primary oscillator disabled + I2C1SEL_PRI) //Use default SCL1/SDA1 pins + +#define VREG33_DIR TRISAbits.TRISA0 +#define VREG33_EN LATAbits.LATA0 +#define MODE_LED_DIR TRISAbits.TRISA1 +#define MODE_LED LATAbits.LATA1 + + +#pragma code + +int main(void) +{ + + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; //Disables the secondary oscilator + + MODE_LED_DIR = 0; //sets the Mode LED pin RA1 as output + MODE_LED = 0; //turns LED off + VREG33_DIR =0; //sets teh VREG pin RA0 as output + VREG33_EN = 1; //turns on the voltage regulator + + unsigned long int i,voltage; +////////////////////////////////////////////////////////////// +///////////////ADC config////////////////////////////////// + AD1PCFGbits.PCFG12=0; //configure RB12 as analog + AD1CON1bits.SSRC = 0b111; // SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + AD1CHS =12; //ADC channel select 12 + AD1CON1bits.ADON =1; // turn ADC on +///FOREVER LOOP/////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////// + while(1) + { + //this just gives us a little delay between measurements + i =0xFFFFF; //sets i to 1048575 + while (i--); //delay function + + //start a measurement with the ADC + AD1CON1bits.DONE=0; //resets DONE bit + AD1CON1bits.SAMP=1; //start sample + + while(AD1CON1bits.DONE==0); //wait for conversion to finish + + //get the measurement and use it to control the LED + voltage = ADC1BUF0; //get the voltage measurement + if (voltage > 0x1D1) MODE_LED = 1; //enable LED if measurement is > 3volts + else MODE_LED = 0; //disable LED if less than 3volts + } } \ No newline at end of file diff --git a/Firmware-Demos/BusPirateUARTDEMOTEST/BusPirateUARTDEMOTEST/main.c b/Firmware-Demos/BusPirateUARTDEMOTEST/BusPirateUARTDEMOTEST/main.c index 030e6c8f..96a250b5 100644 --- a/Firmware-Demos/BusPirateUARTDEMOTEST/BusPirateUARTDEMOTEST/main.c +++ b/Firmware-Demos/BusPirateUARTDEMOTEST/BusPirateUARTDEMOTEST/main.c @@ -1,38 +1,38 @@ -#include -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need - - -#pragma code - -int main(void){ - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; //Default all pins to digital - OSCCONbits.SOSCEN=0; - - //Initialize UART1 - RPINR18bits.U1RXR = 5; // UART1 RX assign to RP5 - RPOR2bits.RP4R = 3; //UART1 TX assign to RP4 - - U1BRG = 34; - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; - - char c; - //Main Loop - while(1){ - //wait for byte from USB to serial converter - while(U1STAbits.URXDA == 0); - c=U1RXREG;//get byte - - //wait for empty spot in transmit buffer - while(U1STAbits.UTXBF == 1); - //echo received character back to USB to serial converter - U1TXREG = c; - } +#include +_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need + + +#pragma code + +int main(void){ + + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; //Default all pins to digital + OSCCONbits.SOSCEN=0; + + //Initialize UART1 + RPINR18bits.U1RXR = 5; // UART1 RX assign to RP5 + RPOR2bits.RP4R = 3; //UART1 TX assign to RP4 + + U1BRG = 34; + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; + + char c; + //Main Loop + while(1){ + //wait for byte from USB to serial converter + while(U1STAbits.URXDA == 0); + c=U1RXREG;//get byte + + //wait for empty spot in transmit buffer + while(U1STAbits.UTXBF == 1); + //echo received character back to USB to serial converter + U1TXREG = c; + } } \ No newline at end of file diff --git a/Firmware/1wire.c b/Firmware/1wire.c index 5197e4ad..256dbe09 100644 --- a/Firmware/1wire.c +++ b/Firmware/1wire.c @@ -1,660 +1,660 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * One wire search code taken from here: http://www.maxim-ic.com/appnotes.cfm/appnote_number/187 - * - * We claim no copyright on our code, but there may be different licenses for some of the code in this file. - * - * To the extent possible under law, the Bus Pirate project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" -#ifdef BP_USE_1WIRE -#include "1wire_lib.h" -#include "1wire.h" -#include "binIOhelpers.h" - -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; - -//the roster stores the first OW_DEV_ROSTER_SLOTS 1-wire addresses found during a ROM SEARCH command -//these addresses are available as MACROs for quick address entry -#define OW_DEV_ROSTER_SLOTS 10 //how many devices max to store addresses as MACROs -struct _OWID{ -// unsigned char familyID; //to lazy to do it right, for now... - unsigned char id[8]; -// unsigned char crc; -}; - -struct _OWIDREG{ - unsigned char num; - struct _OWID dev[OW_DEV_ROSTER_SLOTS]; -} ; - -struct _OWIDREG OWroster; - -void DS1wireID(unsigned char famID); - -unsigned char OWFirst(void); -unsigned char OWNext(void); -unsigned char OWSearch(void); -unsigned char OWVerify(void); -unsigned char docrc8(unsigned char value); - -//because 1wire uses bit times, setting the data line high or low with (_-) has no effect -//we have to save the desired bus state, and then clock in the proper value during a clock(^) -static unsigned char DS1wireDataState=0;//data bits are low by default. - -// global search state, -//these lovely globals are provided courtesy of MAXIM's code -//need to be put in a struct.... -unsigned char ROM_NO[8]; -unsigned char SearchChar=0xf0; //toggle between ROM and ALARM search types -unsigned char LastDiscrepancy; -unsigned char LastFamilyDiscrepancy; -unsigned char LastDeviceFlag; -unsigned char crc8; - -// implementation new framework other part in busPirateCore.c -unsigned int OWread(void) -{ return (OWReadByte()); -} - -unsigned int OWwrite(unsigned int c) -{ OWWriteByte(c); - return 0x100; -} - -unsigned int OWbitr(void) -{ return(OWReadBit()); -} - -void OWbitclk(void) -{ OWWriteBit(DS1wireDataState); - //bpEchoState(DS1wireDataState); -} -unsigned int OWstate(void){ - return DS1wireDataState; -} - -void OWdatl(void) -{ //bpWstring(OUMSG_1W_BIT_WRITE); - //BPMSG1000; - DS1wireDataState=0; - //bpEchoState(0); - //bpWline(OUMSG_1W_BIT_WRITE_NOTE); - BPMSG1001; - //OWWriteBit(DS1wireDataState); -} - -void OWdath(void) -{ //bpWstring(OUMSG_1W_BIT_WRITE); - //BPMSG1000; - DS1wireDataState=1; - //bpEchoState(1); - //bpWline(OUMSG_1W_BIT_WRITE_NOTE); - BPMSG1001; - //OWWriteBit(DS1wireDataState); -} - -void OWsetup(void) -{ - modeConfig.HiZ=1;//yes, always HiZ - - OWroster.num=0;//clear any old 1-wire bus enumeration rosters - //-- Ensure pins are in high impedance mode -- - SDA_TRIS=1; - //writes to the PORTs write to the LATCH - SDA=0; //B9 sda - //bpWline("1WIRE routines Copyright (C) 2000 Michael Pearce"); - //bpWline("Released under GNU General Public License"); - //BPMSG1002; - //BPMSG1003; -} - -void OWmacro(unsigned int macro) -{ static unsigned char c,j; - static unsigned int i; - static unsigned char devID[8]; - - if(macro>0 && macro<51){ - macro--;//adjust down one for roster array index - if(macro>=OWroster.num){//no device #X on the bus, try ROM SEARCH (0xF0) - //bpWline(OUMSG_1W_MACRO_ADDRESS_NODEVICE); - BPMSG1004; - return; - } - //write out the address of the device in the macro - //bpWstring(OUMSG_1W_MACRO_ADDRESS);//xxx WRITE BUS #X ID: - BPMSG1005; - bpWdec(macro+1); - bpWstring(": "); - for(j=0;j<8;j++){ - bpWbyte(OWroster.dev[macro].id[j]); - bpSP; - OWWriteByte(OWroster.dev[macro].id[j]); - } //write address - bpBR; - return; - } - switch(macro){ - case 0://menu - //bpWline(OUMSG_1W_MACRO_MENU); - //bpWline(OUMSG_1W_MACRO_SEARCH_ROM_HEADER); - BPMSG1006; - BPMSG1007; - //write out roster of devices and macros, or SEARCH ROM NOT RUN, TRY (0xf0) - if(OWroster.num==0){ - //bpWline(OUMSG_1W_MACRO_ADDRESS_NODEVICE); - BPMSG1004; - }else{ - for(c=0;c 0); - else - // if equal to last pick 1, if not then pick 0 - search_direction = (id_bit_number == LastDiscrepancy); - - // if 0 was picked then record its position in LastZero - if (search_direction == 0) - { - last_zero = id_bit_number; - - // check for Last discrepancy in family - if (last_zero < 9) - LastFamilyDiscrepancy = last_zero; - } - } - - // set or clear the bit in the ROM byte rom_byte_number - // with mask rom_byte_mask - if (search_direction == 1) - ROM_NO[rom_byte_number] |= rom_byte_mask; - else - ROM_NO[rom_byte_number] &= ~rom_byte_mask; - - // serial number search direction write bit - OWWriteBit(search_direction); - - // increment the byte counter id_bit_number - // and shift the mask rom_byte_mask - id_bit_number++; - rom_byte_mask <<= 1; - - // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask - if (rom_byte_mask == 0) - { - docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC - rom_byte_number++; - rom_byte_mask = 1; - } - } - } - while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 - - // if the search was successful then - if (!((id_bit_number < 65) || (crc8 != 0))) - { - // search successful so set LastDiscrepancy,LastDeviceFlag,search_result - LastDiscrepancy = last_zero; - - // check for last device - if (LastDiscrepancy == 0) - LastDeviceFlag = TRUE; - - search_result = TRUE; - } - } - - // if no device found then reset counters so next 'search' will be like a first - if (!search_result || !ROM_NO[0]) - { - LastDiscrepancy = 0; - LastDeviceFlag = FALSE; - LastFamilyDiscrepancy = 0; - search_result = FALSE; - } - - return search_result; -} - -//-------------------------------------------------------------------------- -// Verify the device with the ROM number in ROM_NO buffer is present. -// Return TRUE : device verified present -// FALSE : device not present -// -unsigned char OWVerify() -{ - unsigned char rom_backup[8]; - unsigned char i,rslt,ld_backup,ldf_backup,lfd_backup; - - // keep a backup copy of the current state - for (i = 0; i < 8; i++) - rom_backup[i] = ROM_NO[i]; - ld_backup = LastDiscrepancy; - ldf_backup = LastDeviceFlag; - lfd_backup = LastFamilyDiscrepancy; - - // set search to find the same device - LastDiscrepancy = 64; - LastDeviceFlag = FALSE; - - if (OWSearch()) - { - // check if same device found - rslt = TRUE; - for (i = 0; i < 8; i++) - { - if (rom_backup[i] != ROM_NO[i]) - { - rslt = FALSE; - break; - } - } - } - else - rslt = FALSE; - - // restore the search state - for (i = 0; i < 8; i++) - ROM_NO[i] = rom_backup[i]; - LastDiscrepancy = ld_backup; - LastDeviceFlag = ldf_backup; - LastFamilyDiscrepancy = lfd_backup; - - // return the result of the verify - return rslt; -} - -// TEST BUILD -static unsigned char dscrc_table[] = { - 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, - 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, - 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, - 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, - 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, - 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, - 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, - 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, - 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, - 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, - 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, - 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, - 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, - 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, - 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, - 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; - -//-------------------------------------------------------------------------- -// Calculate the CRC8 of the byte value provided with the current -// global 'crc8' value. -// Returns current global crc8 value -// -unsigned char docrc8(unsigned char value) -{ - // See Application Note 27 - // TEST BUILD - crc8 = dscrc_table[crc8 ^ value]; - return crc8; -} - - -/* -binary1WIRE mode: -# 00000000 - reset to BBIO -# 00000001 ? mode version string (1W01) -# 00000010 ? 1wire reset -# 00000100 - read byte -# 00001000 - ROM search macro (0xf0) -# 00001001 - ALARM search macro (0xec) -# 0001xxxx ? Bulk transfer, send 1-16 bytes (0=1byte!) -# 0100wxyz ? Configure peripherals w=power, x=pullups, y=AUX, z=CS ( -# 0101wxyz ? read peripherals (planned, not implemented) -*/ -void bin1WIREversionString(void); - -void bin1WIREversionString(void){bpWstring("1W01");} - -void bin1WIRE(void){ - static unsigned char inByte, rawCommand, i,c; - - SDA_TRIS=1; //pin to input - SDA=0; //pin to ground - - BP_CS_DIR=0; //set CS pin direction to output on setup - - modeConfig.HiZ=1;//yes, always hiz, sets CS to open drain state - modeConfig.lsbEN=0;//just in case! - - bin1WIREversionString();//reply string - - while(1){ - - // JTR usb port while(UART1RXRdy == 0);//wait for a byte - inByte=UART1RX(); /// JTR usb port; */ //grab it - rawCommand=(inByte>>4);//get command bits in seperate variable - - switch(rawCommand){ - case 0://reset/setup/config commands - switch(inByte){ - case 0://0, reset exit - //cleanup!!!!!!!!!! - return; //exit - break; - case 1://id reply string - bin1WIREversionString();//reply string - break; - case 2://reset (might want to do something with c in the future) - c=OWReset(); - UART1TX(1); - break; - case 4://read byte - UART1TX(OWReadByte()); - break; - case 8://1wire search macro results - case 9://alarm search - UART1TX(1); - - if(inByte==9)SearchChar=0xec; //search alarm - else SearchChar=0xf0; //search ROM - - // find ALL devices - c = OWFirst(); - while (c){ - // print address - for (i = 0; i <8; i++) UART1TX(ROM_NO[i]); - c = OWNext(); - } - - //send 8x 0xff - for(i=0; i<8; i++) UART1TX(0xff); - - break; - default: - UART1TX(0); - break; - } - break; - - case 0b0001://get x+1 bytes - inByte&=(~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1);//send 1/OK - - for(i=0;i0 && macro<51){ + macro--;//adjust down one for roster array index + if(macro>=OWroster.num){//no device #X on the bus, try ROM SEARCH (0xF0) + //bpWline(OUMSG_1W_MACRO_ADDRESS_NODEVICE); + BPMSG1004; + return; + } + //write out the address of the device in the macro + //bpWstring(OUMSG_1W_MACRO_ADDRESS);//xxx WRITE BUS #X ID: + BPMSG1005; + bpWdec(macro+1); + bpWstring(": "); + for(j=0;j<8;j++){ + bpWbyte(OWroster.dev[macro].id[j]); + bpSP; + OWWriteByte(OWroster.dev[macro].id[j]); + } //write address + bpBR; + return; + } + switch(macro){ + case 0://menu + //bpWline(OUMSG_1W_MACRO_MENU); + //bpWline(OUMSG_1W_MACRO_SEARCH_ROM_HEADER); + BPMSG1006; + BPMSG1007; + //write out roster of devices and macros, or SEARCH ROM NOT RUN, TRY (0xf0) + if(OWroster.num==0){ + //bpWline(OUMSG_1W_MACRO_ADDRESS_NODEVICE); + BPMSG1004; + }else{ + for(c=0;c 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == LastDiscrepancy); + + // if 0 was picked then record its position in LastZero + if (search_direction == 0) + { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == 1) + ROM_NO[rom_byte_number] |= rom_byte_mask; + else + ROM_NO[rom_byte_number] &= ~rom_byte_mask; + + // serial number search direction write bit + OWWriteBit(search_direction); + + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) + { + docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 + + // if the search was successful then + if (!((id_bit_number < 65) || (crc8 != 0))) + { + // search successful so set LastDiscrepancy,LastDeviceFlag,search_result + LastDiscrepancy = last_zero; + + // check for last device + if (LastDiscrepancy == 0) + LastDeviceFlag = TRUE; + + search_result = TRUE; + } + } + + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || !ROM_NO[0]) + { + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + search_result = FALSE; + } + + return search_result; +} + +//-------------------------------------------------------------------------- +// Verify the device with the ROM number in ROM_NO buffer is present. +// Return TRUE : device verified present +// FALSE : device not present +// +unsigned char OWVerify() +{ + unsigned char rom_backup[8]; + unsigned char i,rslt,ld_backup,ldf_backup,lfd_backup; + + // keep a backup copy of the current state + for (i = 0; i < 8; i++) + rom_backup[i] = ROM_NO[i]; + ld_backup = LastDiscrepancy; + ldf_backup = LastDeviceFlag; + lfd_backup = LastFamilyDiscrepancy; + + // set search to find the same device + LastDiscrepancy = 64; + LastDeviceFlag = FALSE; + + if (OWSearch()) + { + // check if same device found + rslt = TRUE; + for (i = 0; i < 8; i++) + { + if (rom_backup[i] != ROM_NO[i]) + { + rslt = FALSE; + break; + } + } + } + else + rslt = FALSE; + + // restore the search state + for (i = 0; i < 8; i++) + ROM_NO[i] = rom_backup[i]; + LastDiscrepancy = ld_backup; + LastDeviceFlag = ldf_backup; + LastFamilyDiscrepancy = lfd_backup; + + // return the result of the verify + return rslt; +} + +// TEST BUILD +static unsigned char dscrc_table[] = { + 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, + 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, + 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, + 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, + 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, + 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, + 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, + 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, + 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, + 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, + 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, + 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, + 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, + 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, + 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, + 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; + +//-------------------------------------------------------------------------- +// Calculate the CRC8 of the byte value provided with the current +// global 'crc8' value. +// Returns current global crc8 value +// +unsigned char docrc8(unsigned char value) +{ + // See Application Note 27 + // TEST BUILD + crc8 = dscrc_table[crc8 ^ value]; + return crc8; +} + + +/* +binary1WIRE mode: +# 00000000 - reset to BBIO +# 00000001 ? mode version string (1W01) +# 00000010 ? 1wire reset +# 00000100 - read byte +# 00001000 - ROM search macro (0xf0) +# 00001001 - ALARM search macro (0xec) +# 0001xxxx ? Bulk transfer, send 1-16 bytes (0=1byte!) +# 0100wxyz ? Configure peripherals w=power, x=pullups, y=AUX, z=CS ( +# 0101wxyz ? read peripherals (planned, not implemented) +*/ +void bin1WIREversionString(void); + +void bin1WIREversionString(void){bpWstring("1W01");} + +void bin1WIRE(void){ + static unsigned char inByte, rawCommand, i,c; + + SDA_TRIS=1; //pin to input + SDA=0; //pin to ground + + BP_CS_DIR=0; //set CS pin direction to output on setup + + modeConfig.HiZ=1;//yes, always hiz, sets CS to open drain state + modeConfig.lsbEN=0;//just in case! + + bin1WIREversionString();//reply string + + while(1){ + + // JTR usb port while(UART1RXRdy == 0);//wait for a byte + inByte=UART1RX(); /// JTR usb port; */ //grab it + rawCommand=(inByte>>4);//get command bits in seperate variable + + switch(rawCommand){ + case 0://reset/setup/config commands + switch(inByte){ + case 0://0, reset exit + //cleanup!!!!!!!!!! + return; //exit + break; + case 1://id reply string + bin1WIREversionString();//reply string + break; + case 2://reset (might want to do something with c in the future) + c=OWReset(); + UART1TX(1); + break; + case 4://read byte + UART1TX(OWReadByte()); + break; + case 8://1wire search macro results + case 9://alarm search + UART1TX(1); + + if(inByte==9)SearchChar=0xec; //search alarm + else SearchChar=0xf0; //search ROM + + // find ALL devices + c = OWFirst(); + while (c){ + // print address + for (i = 0; i <8; i++) UART1TX(ROM_NO[i]); + c = OWNext(); + } + + //send 8x 0xff + for(i=0; i<8; i++) UART1TX(0xff); + + break; + default: + UART1TX(0); + break; + } + break; + + case 0b0001://get x+1 bytes + inByte&=(~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1);//send 1/OK + + for(i=0;i>=1; - if(t) { OWbyte |= 0x80; } - } - bpDelayUS(8); - return OWbyte; -} - - - - - - -/* *********************************************************************************** - Function: OWShortOnBus - Args[0]: - Return: 1=Yes there is a short (bad) or 0=No short (good) - - Desc: Just threw this in for usage in 1wire.c -*********************************************************************************** */ -#if(USE_OWGLOBALS) -#if(OW_INCLUDE_BUSSHORT) -unsigned char OWShortOnBus(void) -{ - return OWShort; -} -#endif -#endif - - - - - - -/* BrentBXR [2011] - No License, No Nothing! -Enjoy :) For questions join the Dangerous Prototypes Forums */ -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * We claim no copyright on our code, but there may be different licenses for some of the code in this file. + * + * To the extent possible under law, the Bus Pirate project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + + +#include "base.h" + +#ifdef BP_USE_1WIRE + +#include "1wire_lib.h" + +#ifndef BP_MOSI + #error BP_MOSI Must Be Defined for 1-wire Bus I/O +#endif + +#ifndef BP_MOSI_DIR + #error BP_MOSI_DIR Must be defined for 1-wire Bus I/O +#endif + + +#if(USE_OWGLOBALS) +// OWShort: If 1; there is a short on the bus (set during OWReset) +static unsigned char OWShort=0; +// OWDevice: If 1; There is atleast one device on the bus (set during OWReset) +static unsigned char OWDevice=0; +#endif + + +/* *********************************************************************************** + Function: OWReset + Args[0]: Void + Return: Made it mimic the old source. + 0 = OK + 1 = Short + 2 = No device. + + Desc: OneWire reset bus procedure. See return values for details. +*********************************************************************************** */ +unsigned char OWReset(void){ + unsigned int Presence=0,i; + + OW_bpResPins(); //found out i needed this after all. + + SDA_TRIS=0; //go low + + //Maxim says a minimum of 480. + for(i=0;i<34;i++) { //BP Timing seems off; adjusting it looks like each FOR takes 4us. + bpDelayUS(10); //So 10+4=14 ## I want ~490 so (((TIMEWANTED)/(DELAY_TIME)+(ADJUST))=(LOOPS_NEEDED)) OR (490/(10+4))=(35) + } //-- above is old. 35x10=506. So im setting it at 34 which is about 490 (turns out to be 491 :) + + SDA_TRIS=1; //release. My logic analyzer says we stayed low for exatly 488us. Thats pretty damn near perfect. + + bpDelayUS(65); // ADJUSTED. Timing looks great now with the odd numbers. + if(SDA) // if lines still high, then no device + Presence=2; // if no device then return 2. + + for(i=0;i<35;i++) { //delay for 506us (found above) which is way after the 480 reccomended. thats ok. + bpDelayUS(10); + } + + if(SDA==0) // if lines still low; then theres a short + { + #if(USE_OWGLOBALS) + OWShort=1; + #endif + return 1; + } + + #if(USE_OWGLOBALS) + if(Presence==0) + OWDevice=1; + #endif + + return Presence; +} + + +/* *********************************************************************************** + Function: OWBit + Args[0]: uChar [Bit to send. Logic 1 or 0] - or 1 to recieve + Return: Returns bit value on bus + + Desc: OWBit works as both a sending and reciving of 1 bit value on the OWBus. + To get a bit value send a logical 1 (OWBit 1) This will pulse the line + as needed then release the bus and wait a few US before sampling if the + OW device has sent data. +*********************************************************************************** */ +unsigned char OWBit(unsigned char OWbit){ + OW_bpResPins(); // I found not including this leaves the lines odd... :/ + + SDA_TRIS=0; + bpDelayUS(4); + if(OWbit) { + SDA_TRIS=1; + } + bpDelayUS(8); + + if(OWbit) { // This is where the magic happens. If a OWbit value of 1 is sent to this function + OWbit = SDA; // well thats the same timing needed to get a value (not just send it) so why not + bpDelayUS(32); // perform both? So sending one will not only send 1 bit; it will also read one bit + } else { // it all depends on what the iDevice is in the mood to do. If its in send mode then + bpDelayUS(25); // it will sends its data, if its in recive mode. Then we will send ours. + SDA_TRIS=1; // magical, i know. :) + bpDelayUS(7); + } + + bpDelayUS(5); //Just an adjust. 70us perfect. + + return OWbit; +} + + + + + + +/* *********************************************************************************** + Function: OWByte + Args[0]: uChar [Byte to send to bus] - or 0xFF to recieve + Return: Returns a byte from the OWBus + + Desc: Like OWBit; OWByte works for both sending and getting. OWTime slots are the same + for sending and getting; so only one function is needed to perform both tasks. + +*********************************************************************************** */ +unsigned char OWByte(unsigned char OWbyte){ + unsigned char i, t=0; // nothing much to say about this function. pretty standard; do this 8 times and collect results. + // except that sends and GETS data. Sending a value of 0xFF will have the OWBit function return + for(i=0;i<8;i++) // bits; this will collect those returns and spit them out. Same with send. It all depends on + { // what the iDevice is looking for at the time this command is sent. + t = OWBit(OWbyte&1); + OWbyte>>=1; + if(t) { OWbyte |= 0x80; } + } + bpDelayUS(8); + return OWbyte; +} + + + + + + +/* *********************************************************************************** + Function: OWShortOnBus + Args[0]: + Return: 1=Yes there is a short (bad) or 0=No short (good) + + Desc: Just threw this in for usage in 1wire.c +*********************************************************************************** */ +#if(USE_OWGLOBALS) +#if(OW_INCLUDE_BUSSHORT) +unsigned char OWShortOnBus(void) +{ + return OWShort; +} +#endif +#endif + + + + + + +/* BrentBXR [2011] - No License, No Nothing! -Enjoy :) For questions join the Dangerous Prototypes Forums */ +#endif diff --git a/Firmware/1wire_lib.h b/Firmware/1wire_lib.h index c96dcec3..b500a900 100644 --- a/Firmware/1wire_lib.h +++ b/Firmware/1wire_lib.h @@ -1,60 +1,60 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - - - - /////////////////////////// -// USE_OWGLOBALS: Define non-zero to enable OWDevice and OWShort globals. -// DESC: I am not sure the point of these; but the other source had them -// so really im only adding them because of that. I am disabling -// them as default. -#define USE_OWGLOBALS 0 -//////////////////////////// -// OW_INCLUDE_BUSSHORT -// DESC: If this is a non-zero value then a 'OWShortOnBus' function is -// enabled; using which will bring a true/false (1/0) value back -// if the OWShort global has been set in OWReset or not. -// NOTE> USE_OW_GLOBALS must also be non-zero to enable this function -#define OW_INCLUDE_BUSSHORT 0 - - - /////////////////////////// -// SOURCE DEFINES & FUNCTION DEFINES -// NOTE: These are not settings. do not edit unless you know what your doing. :) -#define SDA BP_MOSI -#define SDA_TRIS BP_MOSI_DIR -#define OW_bpResPins() SDA_TRIS=1; SDA=0 - -unsigned char OWReset(void); -unsigned char OWBit(unsigned char c); -unsigned char OWByte(unsigned char OWbyte); - -void OWTest(void); - -#if(USE_OWGLOBALS) -#if(OW_INCLUDE_BUSSHORT) -unsigned char OWShortOnBus(void); -#endif -#endif - - ////////////////////////// -// Back Compatability defines. -// DESC: With these defines the new m_1wire_213 files are drop-in compatible -// with the original 1wire.c file and the bus pirate software. -#define OWWriteByte(d) OWByte(d) -#define OWReadByte() OWByte(0xFF) -#define OWReadBit() OWBit(1) -#define OWWriteBit(b) OWBit(b) +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + + + + /////////////////////////// +// USE_OWGLOBALS: Define non-zero to enable OWDevice and OWShort globals. +// DESC: I am not sure the point of these; but the other source had them +// so really im only adding them because of that. I am disabling +// them as default. +#define USE_OWGLOBALS 0 +//////////////////////////// +// OW_INCLUDE_BUSSHORT +// DESC: If this is a non-zero value then a 'OWShortOnBus' function is +// enabled; using which will bring a true/false (1/0) value back +// if the OWShort global has been set in OWReset or not. +// NOTE> USE_OW_GLOBALS must also be non-zero to enable this function +#define OW_INCLUDE_BUSSHORT 0 + + + /////////////////////////// +// SOURCE DEFINES & FUNCTION DEFINES +// NOTE: These are not settings. do not edit unless you know what your doing. :) +#define SDA BP_MOSI +#define SDA_TRIS BP_MOSI_DIR +#define OW_bpResPins() SDA_TRIS=1; SDA=0 + +unsigned char OWReset(void); +unsigned char OWBit(unsigned char c); +unsigned char OWByte(unsigned char OWbyte); + +void OWTest(void); + +#if(USE_OWGLOBALS) +#if(OW_INCLUDE_BUSSHORT) +unsigned char OWShortOnBus(void); +#endif +#endif + + ////////////////////////// +// Back Compatability defines. +// DESC: With these defines the new m_1wire_213 files are drop-in compatible +// with the original 1wire.c file and the bus pirate software. +#define OWWriteByte(d) OWByte(d) +#define OWReadByte() OWByte(0xFF) +#define OWReadBit() OWBit(1) +#define OWWriteBit(b) OWBit(b) diff --git a/Firmware/AUXpin.c b/Firmware/AUXpin.c index da3c4cc2..bdb363a3 100644 --- a/Firmware/AUXpin.c +++ b/Firmware/AUXpin.c @@ -1,745 +1,745 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -#include "procMenu.h" - - //TRISDbits.TRISD5 -#define AUXPIN_DIR BP_AUX0_DIR - - // 20 -#define AUXPIN_RPIN BP_AUX_RPIN - - // RPOR10bits.RP20R -#define AUXPIN_RPOUT BP_AUX_RPOUT - -extern struct _modeConfig modeConfig; - -static enum _auxmode - { - AUX_IO=0, - AUX_FREQ, - AUX_PWM, - } AUXmode=AUX_IO; - -unsigned long bpFreq_count(void); -unsigned long bpPeriod_count(unsigned int n); - -int PWMfreq; -int PWMduty; - -//setup PWM frequency using user values in global variables -void updatePWM(void) -{ unsigned int PWM_period, PWM_dutycycle, PWM_div; - - //cleanup timers - T2CON=0; // clear settings - T4CON=0; - OC5CON =0; //#BPV4 - should be OC5CON1/2 - - if(PWMfreq==0) - { AUXPIN_RPOUT = 0; //remove output from AUX pin - AUXmode=AUX_IO; - return; - } - - if(PWMfreq<4) - { //use 256 //actual max is 62500hz - PWM_div=62;//actually 62500 - T2CONbits.TCKPS1=1; - T2CONbits.TCKPS0=1; - } - else if(PWMfreq<31) - { //use 64 - PWM_div=250; - T2CONbits.TCKPS1=1; - T2CONbits.TCKPS0=0; - } - else if(PWMfreq<245) - { //use 8 - PWM_div=2000; - T2CONbits.TCKPS1=0; - T2CONbits.TCKPS0=1; - } - else - { //use 1 - PWM_div=16000; - T2CONbits.TCKPS1=0; - T2CONbits.TCKPS0=0; - } - PWM_period=(PWM_div/PWMfreq)-1; - PR2 = PWM_period; - - PWM_dutycycle=(PWM_period*PWMduty)/100; - - - //assign pin with PPS - AUXPIN_RPOUT = OC5_IO; - // Should be fine on bpv4 - - OC5R = PWM_dutycycle; - OC5RS = PWM_dutycycle; - OC5CON = 0x6; - T2CONbits.TON = 1; - - AUXmode=AUX_PWM; - -} - -//setup the PWM/frequency generator -void bpPWM(void){ - unsigned int PWM_period, PWM_dutycycle, PWM_freq, PWM_div; - int done; - float PWM_pd; - - //cleanup timers - T2CON=0; // clear settings - T4CON=0; - OC5CON =0; - - if(AUXmode==AUX_PWM){ //PWM is on, stop it - AUXPIN_RPOUT = 0; //remove output from AUX pin - //bpWline(OUMSG_AUX_PWM_OFF); - BPMSG1028; - AUXmode=AUX_IO; - - if(cmdbuf[((cmdstart+1)&CMDLENMSK)]==0x00) return; // return if no arguments to function - } - - done=0; - - cmdstart=(cmdstart+1)&CMDLENMSK; - //cmdstart&=CMDLENMSK; - - //get any compound commandline variables - consumewhitechars(); - PWM_freq=getint(); - consumewhitechars(); - PWM_pd=getint(); - - //sanity check values - if((PWM_freq>0)&&(PWM_freq<4000)) done++; - if((PWM_pd>0)&&(PWM_pd<100)) done++; - - - //calculate frequency: - if(done!=2)//no command line variables, prompt for PWM frequency - { cmderror=0; - - //bpWline(OUMSG_AUX_PWM_NOTE); - BPMSG1029; - //bpWstring(OUMSG_AUX_PWM_FREQ); - BPMSG1030; - PWM_freq=getnumber(50,1, 4000, 0); - } - - //choose proper multiplier for whole range - //bpWstring(OUMSG_AUX_PWM_PRESCALE); - //BPMSG1031; - if(PWM_freq<4){//use 256 //actual max is 62500hz - //bpWline("256"); - PWM_div=62;//actually 62500 - T2CONbits.TCKPS1=1; - T2CONbits.TCKPS0=1; - }else if(PWM_freq<31){//use 64 - //bpWline("64"); - PWM_div=250; - T2CONbits.TCKPS1=1; - T2CONbits.TCKPS0=0; - }else if(PWM_freq<245){//use 8 - //bpWline("8"); - PWM_div=2000; - T2CONbits.TCKPS1=0; - T2CONbits.TCKPS0=1; - }else{//use 1 - //bpWline("1"); - PWM_div=16000; - T2CONbits.TCKPS1=0; - T2CONbits.TCKPS0=0; - } - PWM_period=(PWM_div/PWM_freq)-1; - //bpWstring("PR2:"); - //BPMSG1032; - //bpWintdec(PWM_period); //echo the calculated value - //bpBR; - - if(done!=2)//if no commandline vairable, prompt for duty cycle - { //bpWstring(OUMSG_AUX_PWM_DUTY); - BPMSG1033; - // PWM_pd=bpUserNumberPrompt(2, 99, 50); - PWM_pd=getnumber(50,0,99,0); - } - - PWM_pd/=100; - PWM_dutycycle=PWM_period * PWM_pd; - //bpWdec(PWM_dutycycle); - - //assign pin with PPS - AUXPIN_RPOUT = OC5_IO; - // should be fine on bpv4 - - OC5R = PWM_dutycycle; - OC5RS = PWM_dutycycle; - OC5CON = 0x6; - PR2 = PWM_period; - T2CONbits.TON = 1; - - //bpWline(OUMSG_AUX_PWM_ON); - BPMSG1034; - AUXmode=AUX_PWM; - -} - -//frequency measurement -void bpFreq(void){ - // frequency accuracy optimized by selecting measurement method, either - // counting frequency or measuring period, to maximize resolution. - // Note: long long int division routine used by C30 is not open-coded */ - unsigned long long f, p; - - if(AUXmode==AUX_PWM){ - //bpWline(OUMSG_AUX_FREQ_PWM); - BPMSG1037; - return; - } - - //bpWstring(OUMSG_AUX_FREQCOUNT); - BPMSG1038; - //setup timer - T4CON=0; //make sure the counters are off - T2CON=0; - - //timer 2 external - AUXPIN_DIR=1;//aux input - - RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input - // should be good on bpv4 - - T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); // prescale to 256 - - f=bpFreq_count(); // all measurements within 26bits (<67MHz) - - // counter only seems to be good til around 6.7MHz, - // use 4.2MHz (nearest power of 2 without exceeding 6.7MHz) for reliable reading - if(f>0x3fff){ // if >4.2MHz prescaler required - f*=256; // adjust for prescaler - }else { // get a more accurate reading without prescaler - //bpWline("Autorange"); - BPMSG1245; - T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0 - f=bpFreq_count(); - } - // at 4000Hz 1 bit resolution of frequency measurement = 1 bit resolution of period measurement - if(f>3999){ // when < 4 KHz counting edges is inferior to measuring period(s) - bpWlongdecf(f); // this function uses comma's to seperate thousands. - bpWline(" Hz"); - }else if (f>0) { - BPMSG1245; - p=bpPeriod_count(f); - // don't output fractions of frequency that are less then the frequency - // resolution provided by an increment of the period timer count. - if (p>400000) { // f <= 40 Hz - // 4e5 < p <= 1,264,911 (625us tics) - // 12.61911 < f <= 40 Hz - // output resolution of 1e-5 - f=16e11/p; - bpWlongdecf(f/100000); - UART1TX('.'); - f = f % 100000; - if (f < 10000) UART1TX('0'); - if (f < 1000) UART1TX('0'); - if (f < 100) UART1TX('0'); - if (f < 10) UART1TX('0'); - bpWlongdec(f); - // at p=126,491.1 frequency resolution is .001 - } else if (p>126491) { // f <= 126.4911 - // 126,491 < p <= 4e5 (625us tics) - // 40 < f <= 126.4911 Hz - // output resolution of .0001 - f=16e10/p; - bpWlongdecf(f/10000); - UART1TX('.'); - f = f % 10000; - if (f < 1000) UART1TX('0'); - if (f < 100) UART1TX('0'); - if (f < 10) UART1TX('0'); - bpWintdec(f); - // at p=40,000 frequency resolution is .01 - } else if (p>40000) { // f <= 400 Hz - // 4e4 < p <= 126,491 (625us tics) - // 126.4911 < f <= 400 Hz - // output resolution of .001 - f=16e9/p; - bpWlongdecf(f/1000); - UART1TX('.'); - f = f % 1000; // frequency resolution < 1e-2 - if (f < 100) UART1TX('0'); - if (f < 10) UART1TX('0'); - bpWintdec(f); - // at p=12,649.11 frequency resolution is .1 - }else if (p>12649) { // f <= 1264.911 - // 12,649 < p <= 4e4 (625us tics) - // 400 < f < 1,264.911 Hz - // output resolution of .01 - f=16e8/p; - bpWlongdecf(f/100); - UART1TX('.'); - f = f % 100; // frequency resolution < 1e-1 - if (f < 10) UART1TX('0'); - bpWdec(f); - // at p=4,000 frequency resolution is 1 - }else { // 4,000 < p <= 12,649 (625us tics) - // 1,264.911 < f < 4,000 Hz - // output resolution of .1 - f=16e7/p; - bpWlongdecf(f/10); - UART1TX('.'); - f = f % 10; // frequency resolution < 1 - bpWdec(f); - } - bpWline(" Hz"); - //END of IF(f>0) - }else bpWline("Frequencies < 1Hz are not supported."); - - //return clock input to other pin - RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing - T4CON=0; //make sure the counters are off - T2CON=0; -} - -//frequency measurement -unsigned long bpBinFreq(void){ - //static unsigned int j,k; - unsigned long l; - - //setup timer - T4CON=0; //make sure the counters are off - T2CON=0; - - //timer 2 external - AUXPIN_DIR=1;//aux input - RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input - - T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); - - l=bpFreq_count(); - if(l>0xff){//got count - l*=256;//adjust for prescaler... - }else{//no count, maybe it's less than prescaler (256hz) - T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0 - l=bpFreq_count(); - } - - //return clock input to other pin - RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing - T4CON=0; //make sure the counters are off - T2CON=0; - return l; -} - - -unsigned long bpFreq_count(void){ - static unsigned int j; - static unsigned long l; - - PR3=0xffff;//most significant word - PR2=0xffff;//least significant word - - //clear counter, first write hold, then tmr2.... - TMR3HLD=0x00; - TMR2=0x00; - - //timer 4 internal, measures interval - TMR5HLD=0x00; - TMR4=0x00; - T4CON=0b1000; //.T32=1, bit 3 - - //one second of counting time - PR5=0xf4;//most significant word - PR4=0x2400;//least significant word - IFS1bits.T5IF=0;//clear interrupt flag - - //start timer4 - T4CONbits.TON=1; - //start count (timer2) - T2CONbits.TON=1; - - //wait for timer4 (timer 5 interrupt) - while(IFS1bits.T5IF==0); - - //stop count (timer2) - T2CONbits.TON=0; - T4CONbits.TON=0; - - //spit out 32bit value - j=TMR2; - l=TMR3HLD; - l=(l<<16)+j; - return l; -} - -//bpPeriod_count function for frequency measurment uses input compare periphers -//because BP v4 and v3 have different IC peripherals the function is implemented through #if defs -#if defined (BUSPIRATEV4) -// BPv4 implementation of the bpPeriod_count function -unsigned long bpPeriod_count(unsigned int n){ - static unsigned int i; - static unsigned long j, k, l, m, d, s; - - IFS0bits.IC2IF=0; // clear input capture interrupt flag - IFS0bits.IC1IF=0; // clear input capture interrupt flag - - // configure IC1 to RP20 (AUX) - RPINR7bits.IC2R=AUXPIN_RPIN; - RPINR7bits.IC1R=AUXPIN_RPIN; - - //timer 4 internal, measures interval - TMR5HLD=0x00; - TMR4=0x00; - T4CON=0b1000; //.T32=1, bit 3 - //start timer4 - T4CONbits.TON=1; - - // unimplemented: [15:14]=0b00, - // ICSIDL: [13]=0b0, input capture module continues to operate in CPU idle mode - // ICTSEL[2:0]: [12:10]=0b010=TMR4, 0b011=TMR5 (unimplemented for 16-bit capture) - // unimplemented: [9:8]=0b00 - // ICTMR: [7]=0b0=TMR3, 0b1=TMR2 (unimplemented for 32-bit capture) - // ICI[1:0]: [6:5]=0b00, 1 capture per interrupt - // ICOV,ICBNE: [4:3]=0b00, read-only buffer overflow and not empty - // ICM[2:0]: [2:0]=0b011, capture every rising edge - IC2CON1=0x0C03; // fails with ICM 0 or 3 (0 always read from IC2BUF) - - IC1CON1=0x0803; - - // unimplemented: [15:9]=0b0000000 - // IC32: [8]=0b0 - // ICTRIG: [7]=0b0, synchronize with SYNCSEL specified source - // TRIGSTAT: [6]=0b0, cleared by SW, holds timer in reset when low, trigger chosen by syncsel sets bit and releases timer from reset. - // unimplemented: [5]=0b0 - // SYNCSEL[4:0]: [4:0]=0b10100, selects trigger/synchronization source to be IC1. - - IC2CON2=0x0014; - - IC1CON2=0x0014; - - // read input capture bits n times - while(IC1CON1bits.ICBNE) // clear buffer - j = IC1BUF; - - while(IC2CON1bits.ICBNE) // clear buffer - k = IC2BUF; - - while(!IC1CON1bits.ICBNE); // wait for ICBNE - - k = IC1BUF; - m = IC2BUF; - for(i=s=0; i 180)) { - cmderror = 0; - BPMSG1254; - PWM_pd = getnumber(90, 0, 180, 0); - entryloop=1; - } - - - // Setup multiplier for 50 Hz -servoset: T2CONbits.TCKPS1 = 1; - T2CONbits.TCKPS0 = 1; - PWM_period = 1250;; - PWM_pd /= 3500; - PWM_dutycycle = (PWM_period * PWM_pd) + 62; - - //assign pin with PPS - AUXPIN_RPOUT = OC5_IO; - OC5R = PWM_dutycycle; - OC5RS = PWM_dutycycle; - OC5CON = 0x6; - PR2 = PWM_period; - T2CONbits.TON = 1; - BPMSG1255; - AUXmode=AUX_PWM; - - if(entryloop==1){ - PWM_pd = getnumber(-1, 0, 180, 1); - if(PWM_pd<0){ - bpWBR; - return; - } - goto servoset; - } - -} - - - -/*1. Set the PWM period by writing to the selected -Timer Period register (PRy). -2. Set the PWM duty cycle by writing to the OCxRS -register. -3. Write the OCxR register with the initial duty cycle. -4. Enable interrupts, if required, for the timer and -output compare modules. The output compare -interrupt is required for PWM Fault pin utilization. -5. Configure the output compare module for one of -two PWM Operation modes by writing to the -Output Compare Mode bits, OCM<2:0> -(OCxCON<2:0>). -6. Set the TMRy prescale value and enable the time -base by setting TON (TxCON<15>) = 1.*/ - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +#include "procMenu.h" + + //TRISDbits.TRISD5 +#define AUXPIN_DIR BP_AUX0_DIR + + // 20 +#define AUXPIN_RPIN BP_AUX_RPIN + + // RPOR10bits.RP20R +#define AUXPIN_RPOUT BP_AUX_RPOUT + +extern struct _modeConfig modeConfig; + +static enum _auxmode + { + AUX_IO=0, + AUX_FREQ, + AUX_PWM, + } AUXmode=AUX_IO; + +unsigned long bpFreq_count(void); +unsigned long bpPeriod_count(unsigned int n); + +int PWMfreq; +int PWMduty; + +//setup PWM frequency using user values in global variables +void updatePWM(void) +{ unsigned int PWM_period, PWM_dutycycle, PWM_div; + + //cleanup timers + T2CON=0; // clear settings + T4CON=0; + OC5CON =0; //#BPV4 - should be OC5CON1/2 + + if(PWMfreq==0) + { AUXPIN_RPOUT = 0; //remove output from AUX pin + AUXmode=AUX_IO; + return; + } + + if(PWMfreq<4) + { //use 256 //actual max is 62500hz + PWM_div=62;//actually 62500 + T2CONbits.TCKPS1=1; + T2CONbits.TCKPS0=1; + } + else if(PWMfreq<31) + { //use 64 + PWM_div=250; + T2CONbits.TCKPS1=1; + T2CONbits.TCKPS0=0; + } + else if(PWMfreq<245) + { //use 8 + PWM_div=2000; + T2CONbits.TCKPS1=0; + T2CONbits.TCKPS0=1; + } + else + { //use 1 + PWM_div=16000; + T2CONbits.TCKPS1=0; + T2CONbits.TCKPS0=0; + } + PWM_period=(PWM_div/PWMfreq)-1; + PR2 = PWM_period; + + PWM_dutycycle=(PWM_period*PWMduty)/100; + + + //assign pin with PPS + AUXPIN_RPOUT = OC5_IO; + // Should be fine on bpv4 + + OC5R = PWM_dutycycle; + OC5RS = PWM_dutycycle; + OC5CON = 0x6; + T2CONbits.TON = 1; + + AUXmode=AUX_PWM; + +} + +//setup the PWM/frequency generator +void bpPWM(void){ + unsigned int PWM_period, PWM_dutycycle, PWM_freq, PWM_div; + int done; + float PWM_pd; + + //cleanup timers + T2CON=0; // clear settings + T4CON=0; + OC5CON =0; + + if(AUXmode==AUX_PWM){ //PWM is on, stop it + AUXPIN_RPOUT = 0; //remove output from AUX pin + //bpWline(OUMSG_AUX_PWM_OFF); + BPMSG1028; + AUXmode=AUX_IO; + + if(cmdbuf[((cmdstart+1)&CMDLENMSK)]==0x00) return; // return if no arguments to function + } + + done=0; + + cmdstart=(cmdstart+1)&CMDLENMSK; + //cmdstart&=CMDLENMSK; + + //get any compound commandline variables + consumewhitechars(); + PWM_freq=getint(); + consumewhitechars(); + PWM_pd=getint(); + + //sanity check values + if((PWM_freq>0)&&(PWM_freq<4000)) done++; + if((PWM_pd>0)&&(PWM_pd<100)) done++; + + + //calculate frequency: + if(done!=2)//no command line variables, prompt for PWM frequency + { cmderror=0; + + //bpWline(OUMSG_AUX_PWM_NOTE); + BPMSG1029; + //bpWstring(OUMSG_AUX_PWM_FREQ); + BPMSG1030; + PWM_freq=getnumber(50,1, 4000, 0); + } + + //choose proper multiplier for whole range + //bpWstring(OUMSG_AUX_PWM_PRESCALE); + //BPMSG1031; + if(PWM_freq<4){//use 256 //actual max is 62500hz + //bpWline("256"); + PWM_div=62;//actually 62500 + T2CONbits.TCKPS1=1; + T2CONbits.TCKPS0=1; + }else if(PWM_freq<31){//use 64 + //bpWline("64"); + PWM_div=250; + T2CONbits.TCKPS1=1; + T2CONbits.TCKPS0=0; + }else if(PWM_freq<245){//use 8 + //bpWline("8"); + PWM_div=2000; + T2CONbits.TCKPS1=0; + T2CONbits.TCKPS0=1; + }else{//use 1 + //bpWline("1"); + PWM_div=16000; + T2CONbits.TCKPS1=0; + T2CONbits.TCKPS0=0; + } + PWM_period=(PWM_div/PWM_freq)-1; + //bpWstring("PR2:"); + //BPMSG1032; + //bpWintdec(PWM_period); //echo the calculated value + //bpBR; + + if(done!=2)//if no commandline vairable, prompt for duty cycle + { //bpWstring(OUMSG_AUX_PWM_DUTY); + BPMSG1033; + // PWM_pd=bpUserNumberPrompt(2, 99, 50); + PWM_pd=getnumber(50,0,99,0); + } + + PWM_pd/=100; + PWM_dutycycle=PWM_period * PWM_pd; + //bpWdec(PWM_dutycycle); + + //assign pin with PPS + AUXPIN_RPOUT = OC5_IO; + // should be fine on bpv4 + + OC5R = PWM_dutycycle; + OC5RS = PWM_dutycycle; + OC5CON = 0x6; + PR2 = PWM_period; + T2CONbits.TON = 1; + + //bpWline(OUMSG_AUX_PWM_ON); + BPMSG1034; + AUXmode=AUX_PWM; + +} + +//frequency measurement +void bpFreq(void){ + // frequency accuracy optimized by selecting measurement method, either + // counting frequency or measuring period, to maximize resolution. + // Note: long long int division routine used by C30 is not open-coded */ + unsigned long long f, p; + + if(AUXmode==AUX_PWM){ + //bpWline(OUMSG_AUX_FREQ_PWM); + BPMSG1037; + return; + } + + //bpWstring(OUMSG_AUX_FREQCOUNT); + BPMSG1038; + //setup timer + T4CON=0; //make sure the counters are off + T2CON=0; + + //timer 2 external + AUXPIN_DIR=1;//aux input + + RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input + // should be good on bpv4 + + T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); // prescale to 256 + + f=bpFreq_count(); // all measurements within 26bits (<67MHz) + + // counter only seems to be good til around 6.7MHz, + // use 4.2MHz (nearest power of 2 without exceeding 6.7MHz) for reliable reading + if(f>0x3fff){ // if >4.2MHz prescaler required + f*=256; // adjust for prescaler + }else { // get a more accurate reading without prescaler + //bpWline("Autorange"); + BPMSG1245; + T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0 + f=bpFreq_count(); + } + // at 4000Hz 1 bit resolution of frequency measurement = 1 bit resolution of period measurement + if(f>3999){ // when < 4 KHz counting edges is inferior to measuring period(s) + bpWlongdecf(f); // this function uses comma's to seperate thousands. + bpWline(" Hz"); + }else if (f>0) { + BPMSG1245; + p=bpPeriod_count(f); + // don't output fractions of frequency that are less then the frequency + // resolution provided by an increment of the period timer count. + if (p>400000) { // f <= 40 Hz + // 4e5 < p <= 1,264,911 (625us tics) + // 12.61911 < f <= 40 Hz + // output resolution of 1e-5 + f=16e11/p; + bpWlongdecf(f/100000); + UART1TX('.'); + f = f % 100000; + if (f < 10000) UART1TX('0'); + if (f < 1000) UART1TX('0'); + if (f < 100) UART1TX('0'); + if (f < 10) UART1TX('0'); + bpWlongdec(f); + // at p=126,491.1 frequency resolution is .001 + } else if (p>126491) { // f <= 126.4911 + // 126,491 < p <= 4e5 (625us tics) + // 40 < f <= 126.4911 Hz + // output resolution of .0001 + f=16e10/p; + bpWlongdecf(f/10000); + UART1TX('.'); + f = f % 10000; + if (f < 1000) UART1TX('0'); + if (f < 100) UART1TX('0'); + if (f < 10) UART1TX('0'); + bpWintdec(f); + // at p=40,000 frequency resolution is .01 + } else if (p>40000) { // f <= 400 Hz + // 4e4 < p <= 126,491 (625us tics) + // 126.4911 < f <= 400 Hz + // output resolution of .001 + f=16e9/p; + bpWlongdecf(f/1000); + UART1TX('.'); + f = f % 1000; // frequency resolution < 1e-2 + if (f < 100) UART1TX('0'); + if (f < 10) UART1TX('0'); + bpWintdec(f); + // at p=12,649.11 frequency resolution is .1 + }else if (p>12649) { // f <= 1264.911 + // 12,649 < p <= 4e4 (625us tics) + // 400 < f < 1,264.911 Hz + // output resolution of .01 + f=16e8/p; + bpWlongdecf(f/100); + UART1TX('.'); + f = f % 100; // frequency resolution < 1e-1 + if (f < 10) UART1TX('0'); + bpWdec(f); + // at p=4,000 frequency resolution is 1 + }else { // 4,000 < p <= 12,649 (625us tics) + // 1,264.911 < f < 4,000 Hz + // output resolution of .1 + f=16e7/p; + bpWlongdecf(f/10); + UART1TX('.'); + f = f % 10; // frequency resolution < 1 + bpWdec(f); + } + bpWline(" Hz"); + //END of IF(f>0) + }else bpWline("Frequencies < 1Hz are not supported."); + + //return clock input to other pin + RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing + T4CON=0; //make sure the counters are off + T2CON=0; +} + +//frequency measurement +unsigned long bpBinFreq(void){ + //static unsigned int j,k; + unsigned long l; + + //setup timer + T4CON=0; //make sure the counters are off + T2CON=0; + + //timer 2 external + AUXPIN_DIR=1;//aux input + RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input + + T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); + + l=bpFreq_count(); + if(l>0xff){//got count + l*=256;//adjust for prescaler... + }else{//no count, maybe it's less than prescaler (256hz) + T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0 + l=bpFreq_count(); + } + + //return clock input to other pin + RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing + T4CON=0; //make sure the counters are off + T2CON=0; + return l; +} + + +unsigned long bpFreq_count(void){ + static unsigned int j; + static unsigned long l; + + PR3=0xffff;//most significant word + PR2=0xffff;//least significant word + + //clear counter, first write hold, then tmr2.... + TMR3HLD=0x00; + TMR2=0x00; + + //timer 4 internal, measures interval + TMR5HLD=0x00; + TMR4=0x00; + T4CON=0b1000; //.T32=1, bit 3 + + //one second of counting time + PR5=0xf4;//most significant word + PR4=0x2400;//least significant word + IFS1bits.T5IF=0;//clear interrupt flag + + //start timer4 + T4CONbits.TON=1; + //start count (timer2) + T2CONbits.TON=1; + + //wait for timer4 (timer 5 interrupt) + while(IFS1bits.T5IF==0); + + //stop count (timer2) + T2CONbits.TON=0; + T4CONbits.TON=0; + + //spit out 32bit value + j=TMR2; + l=TMR3HLD; + l=(l<<16)+j; + return l; +} + +//bpPeriod_count function for frequency measurment uses input compare periphers +//because BP v4 and v3 have different IC peripherals the function is implemented through #if defs +#if defined (BUSPIRATEV4) +// BPv4 implementation of the bpPeriod_count function +unsigned long bpPeriod_count(unsigned int n){ + static unsigned int i; + static unsigned long j, k, l, m, d, s; + + IFS0bits.IC2IF=0; // clear input capture interrupt flag + IFS0bits.IC1IF=0; // clear input capture interrupt flag + + // configure IC1 to RP20 (AUX) + RPINR7bits.IC2R=AUXPIN_RPIN; + RPINR7bits.IC1R=AUXPIN_RPIN; + + //timer 4 internal, measures interval + TMR5HLD=0x00; + TMR4=0x00; + T4CON=0b1000; //.T32=1, bit 3 + //start timer4 + T4CONbits.TON=1; + + // unimplemented: [15:14]=0b00, + // ICSIDL: [13]=0b0, input capture module continues to operate in CPU idle mode + // ICTSEL[2:0]: [12:10]=0b010=TMR4, 0b011=TMR5 (unimplemented for 16-bit capture) + // unimplemented: [9:8]=0b00 + // ICTMR: [7]=0b0=TMR3, 0b1=TMR2 (unimplemented for 32-bit capture) + // ICI[1:0]: [6:5]=0b00, 1 capture per interrupt + // ICOV,ICBNE: [4:3]=0b00, read-only buffer overflow and not empty + // ICM[2:0]: [2:0]=0b011, capture every rising edge + IC2CON1=0x0C03; // fails with ICM 0 or 3 (0 always read from IC2BUF) + + IC1CON1=0x0803; + + // unimplemented: [15:9]=0b0000000 + // IC32: [8]=0b0 + // ICTRIG: [7]=0b0, synchronize with SYNCSEL specified source + // TRIGSTAT: [6]=0b0, cleared by SW, holds timer in reset when low, trigger chosen by syncsel sets bit and releases timer from reset. + // unimplemented: [5]=0b0 + // SYNCSEL[4:0]: [4:0]=0b10100, selects trigger/synchronization source to be IC1. + + IC2CON2=0x0014; + + IC1CON2=0x0014; + + // read input capture bits n times + while(IC1CON1bits.ICBNE) // clear buffer + j = IC1BUF; + + while(IC2CON1bits.ICBNE) // clear buffer + k = IC2BUF; + + while(!IC1CON1bits.ICBNE); // wait for ICBNE + + k = IC1BUF; + m = IC2BUF; + for(i=s=0; i 180)) { + cmderror = 0; + BPMSG1254; + PWM_pd = getnumber(90, 0, 180, 0); + entryloop=1; + } + + + // Setup multiplier for 50 Hz +servoset: T2CONbits.TCKPS1 = 1; + T2CONbits.TCKPS0 = 1; + PWM_period = 1250;; + PWM_pd /= 3500; + PWM_dutycycle = (PWM_period * PWM_pd) + 62; + + //assign pin with PPS + AUXPIN_RPOUT = OC5_IO; + OC5R = PWM_dutycycle; + OC5RS = PWM_dutycycle; + OC5CON = 0x6; + PR2 = PWM_period; + T2CONbits.TON = 1; + BPMSG1255; + AUXmode=AUX_PWM; + + if(entryloop==1){ + PWM_pd = getnumber(-1, 0, 180, 1); + if(PWM_pd<0){ + bpWBR; + return; + } + goto servoset; + } + +} + + + +/*1. Set the PWM period by writing to the selected +Timer Period register (PRy). +2. Set the PWM duty cycle by writing to the OCxRS +register. +3. Write the OCxR register with the initial duty cycle. +4. Enable interrupts, if required, for the timer and +output compare modules. The output compare +interrupt is required for PWM Fault pin utilization. +5. Configure the output compare module for one of +two PWM Operation modes by writing to the +Output Compare Mode bits, OCM<2:0> +(OCxCON<2:0>). +6. Set the TMRy prescale value and enable the time +base by setting TON (TxCON<15>) = 1.*/ + diff --git a/Firmware/AUXpin.h b/Firmware/AUXpin.h index 3d423348..9850c783 100644 --- a/Firmware/AUXpin.h +++ b/Firmware/AUXpin.h @@ -1,30 +1,30 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -void bpFreq(void); -unsigned long bpBinFreq(void); -void bpPWM(void); - -void updatePWM(void); -void bpAuxHiZ(void); -void bpAuxHigh(void); -void bpAuxLow(void); -unsigned int bpAuxRead(void); - -void bpServo(void); - -extern int PWMfreq; -extern int PWMduty; - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +void bpFreq(void); +unsigned long bpBinFreq(void); +void bpPWM(void); + +void updatePWM(void); +void bpAuxHiZ(void); +void bpAuxHigh(void); +void bpAuxLow(void); +unsigned int bpAuxRead(void); + +void bpServo(void); + +extern int PWMfreq; +extern int PWMduty; + diff --git a/Firmware/Compiler.h b/Firmware/Compiler.h index 0075a776..c9ffd802 100644 --- a/Firmware/Compiler.h +++ b/Firmware/Compiler.h @@ -1,201 +1,201 @@ -/********************************************************************* - * - * Compiler and hardware specific definitions - * - ********************************************************************* - * FileName: Compiler.h - * Dependencies: None - * Processor: PIC18, PIC24F, PIC24H, PIC24E, dsPIC30F, dsPIC33F, - * dsPIC33E, PIC32 - * Compiler: Microchip C32 v1.00 or higher - * Microchip C30 v3.01 or higher - * Microchip C18 v3.13 or higher - * HI-TECH PICC-18 PRO 9.63 or higher - * Company: Microchip Technology, Inc. - * - * Software License Agreement - * - * Copyright (C) 2002-2010 Microchip Technology Inc. All rights - * reserved. - * - * Microchip licenses to you the right to use, modify, copy, and - * distribute: - * (i) the Software when embedded on a Microchip microcontroller or - * digital signal controller product ("Device") which is - * integrated into Licensee's product; or - * (ii) ONLY the Software driver source files ENC28J60.c and - * ENC28J60.h ported to a non-Microchip device used in - * conjunction with a Microchip ethernet controller for the - * sole purpose of interfacing with the ethernet controller. - * - * You should refer to the license agreement accompanying this - * Software for additional information regarding your rights and - * obligations. - * - * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT - * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT - * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL - * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR - * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF - * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS - * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE - * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER - * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT - * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. - * - * - * Date Comment - *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * 10/03/2006 Original, copied from old Compiler.h - * 11/07/2007 Reorganized and simplified - * 03/31/2010 Removed dependency on WORD and DWORD typedefs - * 04/14/2010 Added defines to uniquely identify each compiler - ********************************************************************/ -#ifndef __COMPILER_H -#define __COMPILER_H - -// Include proper device header file -#if defined(__18CXX) && !defined(HI_TECH_C) - // PIC18 processor with Microchip C18 compiler - #define COMPILER_MPLAB_C18 - #include -#elif defined(__PICC18__) && defined(HI_TECH_C) - // PIC18 processor with (Microchip) HI-TECH PICC-18 compiler - #if !defined(__18CXX) - #define __18CXX - #endif - #define COMPILER_HITECH_PICC18 - #include -#elif (defined(__PIC24F__) || defined(__PIC24FK__)) && defined(__C30__) // Microchip C30 compiler - // PIC24F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__PIC24H__) && defined(__C30__) // Microchip C30 compiler - // PIC24H processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__PIC24E__) && defined(__C30__) // Microchip C30 compiler - // PIC24E processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler - // dsPIC33F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC33E__) && defined(__C30__) // Microchip C30 compiler - // dsPIC33E processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__dsPIC30F__) && defined(__C30__) // Microchip C30 compiler - // dsPIC30F processor - #define COMPILER_MPLAB_C30 - #include -#elif defined(__C30__) // Microchip C30 compiler, but targeting "generic-16bit" processor. - #define COMPILER_MPLAB_C30 - #include - // Define some useful inline assembly functions which are normally in the - // processor header files, but absent from the generic p30sim.h file. - #if !defined(Nop) - #define Nop() __builtin_nop() - #define ClrWdt() {__asm__ volatile ("clrwdt");} - #define Sleep() {__asm__ volatile ("pwrsav #0");} - #define Idle() {__asm__ volatile ("pwrsav #1");} - #endif -#elif defined(__PIC32MX__) // Microchip C32 compiler - #if !defined(__C32__) - #define __C32__ - #endif - #define COMPILER_MPLAB_C32 - #include - #include -#else - #error Unknown processor or compiler. See Compiler.h -#endif - -#include -#include -#include - - -// Base RAM and ROM pointer types for given architecture -#if defined(__PIC32MX__) - #define PTR_BASE unsigned long - #define ROM_PTR_BASE unsigned long -#elif defined(__C30__) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned short -#elif defined(COMPILER_MPLAB_C18) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned short long -#elif defined(COMPILER_HITECH_PICC18) - #define PTR_BASE unsigned short - #define ROM_PTR_BASE unsigned long -#endif - - -// Definitions that apply to all except Microchip MPLAB C Compiler for PIC18 MCUs (C18) -#if !defined(COMPILER_MPLAB_C18) - #define memcmppgm2ram(a,b,c) memcmp(a,b,c) - #define strcmppgm2ram(a,b) strcmp(a,b) - #define memcpypgm2ram(a,b,c) memcpy(a,b,c) - #define strcpypgm2ram(a,b) strcpy(a,b) - #define strncpypgm2ram(a,b,c) strncpy(a,b,c) - #define strstrrampgm(a,b) strstr(a,b) - #define strlenpgm(a) strlen(a) - #define strchrpgm(a,b) strchr(a,b) - #define strcatpgm2ram(a,b) strcat(a,b) -#endif - - -// Definitions that apply to all 8-bit products -// (PIC18) -#if defined(__18CXX) - #define __attribute__(a) - - #define FAR far - - // Microchip C18 specific defines - #if defined(COMPILER_MPLAB_C18) - #define ROM rom - #endif - - // HI TECH PICC-18 specific defines - #if defined(COMPILER_HITECH_PICC18) - #define ROM const - #define rom - #define Nop() asm("NOP"); - #define ClrWdt() asm("CLRWDT"); - #define Reset() asm("RESET"); - #endif - -// Definitions that apply to all 16-bit and 32-bit products -// (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32) -#else - #define ROM const - - // 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F) - #if defined(__C30__) - #define Reset() asm("reset") - #define FAR __attribute__((far)) - #endif - - // 32-bit specific defines (PIC32) - #if defined(__PIC32MX__) - #define persistent - #define far - #define FAR - #define Reset() SoftReset() - #define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK) - - // MPLAB C Compiler for PIC32 MCUs version 1.04 and below don't have a - // Nop() function. However, version 1.05 has Nop() declared as _nop(). - #if !defined(Nop) && (__C32_VERSION__ <= 104) - #define Nop() asm("nop") - #endif - #endif -#endif - - - -#endif +/********************************************************************* + * + * Compiler and hardware specific definitions + * + ********************************************************************* + * FileName: Compiler.h + * Dependencies: None + * Processor: PIC18, PIC24F, PIC24H, PIC24E, dsPIC30F, dsPIC33F, + * dsPIC33E, PIC32 + * Compiler: Microchip C32 v1.00 or higher + * Microchip C30 v3.01 or higher + * Microchip C18 v3.13 or higher + * HI-TECH PICC-18 PRO 9.63 or higher + * Company: Microchip Technology, Inc. + * + * Software License Agreement + * + * Copyright (C) 2002-2010 Microchip Technology Inc. All rights + * reserved. + * + * Microchip licenses to you the right to use, modify, copy, and + * distribute: + * (i) the Software when embedded on a Microchip microcontroller or + * digital signal controller product ("Device") which is + * integrated into Licensee's product; or + * (ii) ONLY the Software driver source files ENC28J60.c and + * ENC28J60.h ported to a non-Microchip device used in + * conjunction with a Microchip ethernet controller for the + * sole purpose of interfacing with the ethernet controller. + * + * You should refer to the license agreement accompanying this + * Software for additional information regarding your rights and + * obligations. + * + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + * + * Date Comment + *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * 10/03/2006 Original, copied from old Compiler.h + * 11/07/2007 Reorganized and simplified + * 03/31/2010 Removed dependency on WORD and DWORD typedefs + * 04/14/2010 Added defines to uniquely identify each compiler + ********************************************************************/ +#ifndef __COMPILER_H +#define __COMPILER_H + +// Include proper device header file +#if defined(__18CXX) && !defined(HI_TECH_C) + // PIC18 processor with Microchip C18 compiler + #define COMPILER_MPLAB_C18 + #include +#elif defined(__PICC18__) && defined(HI_TECH_C) + // PIC18 processor with (Microchip) HI-TECH PICC-18 compiler + #if !defined(__18CXX) + #define __18CXX + #endif + #define COMPILER_HITECH_PICC18 + #include +#elif (defined(__PIC24F__) || defined(__PIC24FK__)) && defined(__C30__) // Microchip C30 compiler + // PIC24F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__PIC24H__) && defined(__C30__) // Microchip C30 compiler + // PIC24H processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__PIC24E__) && defined(__C30__) // Microchip C30 compiler + // PIC24E processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler + // dsPIC33F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC33E__) && defined(__C30__) // Microchip C30 compiler + // dsPIC33E processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__dsPIC30F__) && defined(__C30__) // Microchip C30 compiler + // dsPIC30F processor + #define COMPILER_MPLAB_C30 + #include +#elif defined(__C30__) // Microchip C30 compiler, but targeting "generic-16bit" processor. + #define COMPILER_MPLAB_C30 + #include + // Define some useful inline assembly functions which are normally in the + // processor header files, but absent from the generic p30sim.h file. + #if !defined(Nop) + #define Nop() __builtin_nop() + #define ClrWdt() {__asm__ volatile ("clrwdt");} + #define Sleep() {__asm__ volatile ("pwrsav #0");} + #define Idle() {__asm__ volatile ("pwrsav #1");} + #endif +#elif defined(__PIC32MX__) // Microchip C32 compiler + #if !defined(__C32__) + #define __C32__ + #endif + #define COMPILER_MPLAB_C32 + #include + #include +#else + #error Unknown processor or compiler. See Compiler.h +#endif + +#include +#include +#include + + +// Base RAM and ROM pointer types for given architecture +#if defined(__PIC32MX__) + #define PTR_BASE unsigned long + #define ROM_PTR_BASE unsigned long +#elif defined(__C30__) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned short +#elif defined(COMPILER_MPLAB_C18) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned short long +#elif defined(COMPILER_HITECH_PICC18) + #define PTR_BASE unsigned short + #define ROM_PTR_BASE unsigned long +#endif + + +// Definitions that apply to all except Microchip MPLAB C Compiler for PIC18 MCUs (C18) +#if !defined(COMPILER_MPLAB_C18) + #define memcmppgm2ram(a,b,c) memcmp(a,b,c) + #define strcmppgm2ram(a,b) strcmp(a,b) + #define memcpypgm2ram(a,b,c) memcpy(a,b,c) + #define strcpypgm2ram(a,b) strcpy(a,b) + #define strncpypgm2ram(a,b,c) strncpy(a,b,c) + #define strstrrampgm(a,b) strstr(a,b) + #define strlenpgm(a) strlen(a) + #define strchrpgm(a,b) strchr(a,b) + #define strcatpgm2ram(a,b) strcat(a,b) +#endif + + +// Definitions that apply to all 8-bit products +// (PIC18) +#if defined(__18CXX) + #define __attribute__(a) + + #define FAR far + + // Microchip C18 specific defines + #if defined(COMPILER_MPLAB_C18) + #define ROM rom + #endif + + // HI TECH PICC-18 specific defines + #if defined(COMPILER_HITECH_PICC18) + #define ROM const + #define rom + #define Nop() asm("NOP"); + #define ClrWdt() asm("CLRWDT"); + #define Reset() asm("RESET"); + #endif + +// Definitions that apply to all 16-bit and 32-bit products +// (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32) +#else + #define ROM const + + // 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F) + #if defined(__C30__) + #define Reset() asm("reset") + #define FAR __attribute__((far)) + #endif + + // 32-bit specific defines (PIC32) + #if defined(__PIC32MX__) + #define persistent + #define far + #define FAR + #define Reset() SoftReset() + #define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK) + + // MPLAB C Compiler for PIC32 MCUs version 1.04 and below don't have a + // Nop() function. However, version 1.05 has Nop() declared as _nop(). + #if !defined(Nop) && (__C32_VERSION__ <= 104) + #define Nop() asm("nop") + #endif + #endif +#endif + + + +#endif diff --git a/Firmware/GenericTypeDefs.h b/Firmware/GenericTypeDefs.h index 03253b6a..a2819e4e 100644 --- a/Firmware/GenericTypeDefs.h +++ b/Firmware/GenericTypeDefs.h @@ -1,548 +1,548 @@ -/******************************************************************* - - Generic Type Definitions - -******************************************************************** - FileName: GenericTypeDefs.h - Dependencies: None - Processor: PIC10, PIC12, PIC16, PIC18, PIC24, dsPIC, PIC32 - Compiler: MPLAB C Compilers for PIC18, PIC24, dsPIC, & PIC32 - Hi-Tech PICC PRO, Hi-Tech PICC18 PRO - Company: Microchip Technology Inc. - - Software License Agreement - - The software supplied herewith by Microchip Technology Incorporated - (the "Company") is intended and supplied to you, the Company's - customer, for use solely and exclusively with products manufactured - by the Company. - - The software is owned by the Company and/or its supplier, and is - protected under applicable copyright laws. All rights are reserved. - Any use in violation of the foregoing restrictions may subject the - user to criminal sanctions under applicable laws, as well as to - civil liability for the breach of the terms and conditions of this - license. - - THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, - WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED - TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, - IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR - CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. - -******************************************************************** - File Description: - - Change History: - Rev Date Description - 1.1 09/11/06 Add base signed types - 1.2 02/28/07 Add QWORD, LONGLONG, QWORD_VAL - 1.3 02/06/08 Add def's for PIC32 - 1.4 08/08/08 Remove LSB/MSB Macros, adopted by Peripheral lib - 1.5 08/14/08 Simplify file header - 2.0 07/13/09 Updated for new release of coding standards -*******************************************************************/ - -#ifndef __GENERIC_TYPE_DEFS_H_ -#define __GENERIC_TYPE_DEFS_H_ - -/* Specify an extension for GCC based compilers */ -#if defined(__GNUC__) -#define __EXTENSION __extension__ -#else -#define __EXTENSION -#endif - -#if !defined(__PACKED) - #define __PACKED -#endif - -/* get compiler defined type definitions (NULL, size_t, etc) */ -#include - -typedef enum _BOOL { FALSE = 0, TRUE } BOOL; /* Undefined size */ -typedef enum _BIT { CLEAR = 0, SET } BIT; - -#define PUBLIC /* Function attributes */ -#define PROTECTED -#define PRIVATE static - -/* INT is processor specific in length may vary in size */ -typedef signed int INT; -typedef signed char INT8; -typedef signed short int INT16; -typedef signed long int INT32; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -__EXTENSION typedef signed long long INT64; -#endif - -/* UINT is processor specific in length may vary in size */ -typedef unsigned int UINT; -typedef unsigned char UINT8; -typedef unsigned short int UINT16; -/* 24-bit type only available on C18 */ -#if defined(__18CXX) -typedef unsigned short long UINT24; -#endif -typedef unsigned long int UINT32; /* other name for 32-bit integer */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -__EXTENSION typedef unsigned long long UINT64; -#endif - -typedef union -{ - UINT8 Val; - struct - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - } bits; -} UINT8_VAL, UINT8_BITS; - -typedef union -{ - UINT16 Val; - UINT8 v[2] __PACKED; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - } byte; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - } bits; -} UINT16_VAL, UINT16_BITS; - -/* 24-bit type only available on C18 */ -#if defined(__18CXX) -typedef union -{ - UINT24 Val; - UINT8 v[3] __PACKED; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - UINT8 UB; - } byte; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - } bits; -} UINT24_VAL, UINT24_BITS; -#endif - -typedef union -{ - UINT32 Val; - UINT16 w[2] __PACKED; - UINT8 v[4] __PACKED; - struct __PACKED - { - UINT16 LW; - UINT16 HW; - } word; - struct __PACKED - { - UINT8 LB; - UINT8 HB; - UINT8 UB; - UINT8 MB; - } byte; - struct __PACKED - { - UINT16_VAL low; - UINT16_VAL high; - }wordUnion; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - __EXTENSION UINT8 b24:1; - __EXTENSION UINT8 b25:1; - __EXTENSION UINT8 b26:1; - __EXTENSION UINT8 b27:1; - __EXTENSION UINT8 b28:1; - __EXTENSION UINT8 b29:1; - __EXTENSION UINT8 b30:1; - __EXTENSION UINT8 b31:1; - } bits; -} UINT32_VAL; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -#if !defined(__18CXX) -typedef union -{ - UINT64 Val; - UINT32 d[2] __PACKED; - UINT16 w[4] __PACKED; - UINT8 v[8] __PACKED; - struct __PACKED - { - UINT32 LD; - UINT32 HD; - } dword; - struct __PACKED - { - UINT16 LW; - UINT16 HW; - UINT16 UW; - UINT16 MW; - } word; - struct __PACKED - { - __EXTENSION UINT8 b0:1; - __EXTENSION UINT8 b1:1; - __EXTENSION UINT8 b2:1; - __EXTENSION UINT8 b3:1; - __EXTENSION UINT8 b4:1; - __EXTENSION UINT8 b5:1; - __EXTENSION UINT8 b6:1; - __EXTENSION UINT8 b7:1; - __EXTENSION UINT8 b8:1; - __EXTENSION UINT8 b9:1; - __EXTENSION UINT8 b10:1; - __EXTENSION UINT8 b11:1; - __EXTENSION UINT8 b12:1; - __EXTENSION UINT8 b13:1; - __EXTENSION UINT8 b14:1; - __EXTENSION UINT8 b15:1; - __EXTENSION UINT8 b16:1; - __EXTENSION UINT8 b17:1; - __EXTENSION UINT8 b18:1; - __EXTENSION UINT8 b19:1; - __EXTENSION UINT8 b20:1; - __EXTENSION UINT8 b21:1; - __EXTENSION UINT8 b22:1; - __EXTENSION UINT8 b23:1; - __EXTENSION UINT8 b24:1; - __EXTENSION UINT8 b25:1; - __EXTENSION UINT8 b26:1; - __EXTENSION UINT8 b27:1; - __EXTENSION UINT8 b28:1; - __EXTENSION UINT8 b29:1; - __EXTENSION UINT8 b30:1; - __EXTENSION UINT8 b31:1; - __EXTENSION UINT8 b32:1; - __EXTENSION UINT8 b33:1; - __EXTENSION UINT8 b34:1; - __EXTENSION UINT8 b35:1; - __EXTENSION UINT8 b36:1; - __EXTENSION UINT8 b37:1; - __EXTENSION UINT8 b38:1; - __EXTENSION UINT8 b39:1; - __EXTENSION UINT8 b40:1; - __EXTENSION UINT8 b41:1; - __EXTENSION UINT8 b42:1; - __EXTENSION UINT8 b43:1; - __EXTENSION UINT8 b44:1; - __EXTENSION UINT8 b45:1; - __EXTENSION UINT8 b46:1; - __EXTENSION UINT8 b47:1; - __EXTENSION UINT8 b48:1; - __EXTENSION UINT8 b49:1; - __EXTENSION UINT8 b50:1; - __EXTENSION UINT8 b51:1; - __EXTENSION UINT8 b52:1; - __EXTENSION UINT8 b53:1; - __EXTENSION UINT8 b54:1; - __EXTENSION UINT8 b55:1; - __EXTENSION UINT8 b56:1; - __EXTENSION UINT8 b57:1; - __EXTENSION UINT8 b58:1; - __EXTENSION UINT8 b59:1; - __EXTENSION UINT8 b60:1; - __EXTENSION UINT8 b61:1; - __EXTENSION UINT8 b62:1; - __EXTENSION UINT8 b63:1; - } bits; -} UINT64_VAL; -#endif /* __18CXX */ - -/***********************************************************************************/ - -/* Alternate definitions */ -typedef void VOID; - -typedef char CHAR8; -typedef unsigned char UCHAR8; - -typedef unsigned char BYTE; /* 8-bit unsigned */ -typedef unsigned short int WORD; /* 16-bit unsigned */ -typedef unsigned long DWORD; /* 32-bit unsigned */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -__EXTENSION -typedef unsigned long long QWORD; /* 64-bit unsigned */ -typedef signed char CHAR; /* 8-bit signed */ -typedef signed short int SHORT; /* 16-bit signed */ -typedef signed long LONG; /* 32-bit signed */ -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -__EXTENSION -typedef signed long long LONGLONG; /* 64-bit signed */ -typedef union -{ - BYTE Val; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - } bits; -} BYTE_VAL, BYTE_BITS; - -typedef union -{ - WORD Val; - BYTE v[2] __PACKED; - struct __PACKED - { - BYTE LB; - BYTE HB; - } byte; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - } bits; -} WORD_VAL, WORD_BITS; - -typedef union -{ - DWORD Val; - WORD w[2] __PACKED; - BYTE v[4] __PACKED; - struct __PACKED - { - WORD LW; - WORD HW; - } word; - struct __PACKED - { - BYTE LB; - BYTE HB; - BYTE UB; - BYTE MB; - } byte; - struct __PACKED - { - WORD_VAL low; - WORD_VAL high; - }wordUnion; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - __EXTENSION BYTE b16:1; - __EXTENSION BYTE b17:1; - __EXTENSION BYTE b18:1; - __EXTENSION BYTE b19:1; - __EXTENSION BYTE b20:1; - __EXTENSION BYTE b21:1; - __EXTENSION BYTE b22:1; - __EXTENSION BYTE b23:1; - __EXTENSION BYTE b24:1; - __EXTENSION BYTE b25:1; - __EXTENSION BYTE b26:1; - __EXTENSION BYTE b27:1; - __EXTENSION BYTE b28:1; - __EXTENSION BYTE b29:1; - __EXTENSION BYTE b30:1; - __EXTENSION BYTE b31:1; - } bits; -} DWORD_VAL; - -/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ -typedef union -{ - QWORD Val; - DWORD d[2] __PACKED; - WORD w[4] __PACKED; - BYTE v[8] __PACKED; - struct __PACKED - { - DWORD LD; - DWORD HD; - } dword; - struct __PACKED - { - WORD LW; - WORD HW; - WORD UW; - WORD MW; - } word; - struct __PACKED - { - __EXTENSION BYTE b0:1; - __EXTENSION BYTE b1:1; - __EXTENSION BYTE b2:1; - __EXTENSION BYTE b3:1; - __EXTENSION BYTE b4:1; - __EXTENSION BYTE b5:1; - __EXTENSION BYTE b6:1; - __EXTENSION BYTE b7:1; - __EXTENSION BYTE b8:1; - __EXTENSION BYTE b9:1; - __EXTENSION BYTE b10:1; - __EXTENSION BYTE b11:1; - __EXTENSION BYTE b12:1; - __EXTENSION BYTE b13:1; - __EXTENSION BYTE b14:1; - __EXTENSION BYTE b15:1; - __EXTENSION BYTE b16:1; - __EXTENSION BYTE b17:1; - __EXTENSION BYTE b18:1; - __EXTENSION BYTE b19:1; - __EXTENSION BYTE b20:1; - __EXTENSION BYTE b21:1; - __EXTENSION BYTE b22:1; - __EXTENSION BYTE b23:1; - __EXTENSION BYTE b24:1; - __EXTENSION BYTE b25:1; - __EXTENSION BYTE b26:1; - __EXTENSION BYTE b27:1; - __EXTENSION BYTE b28:1; - __EXTENSION BYTE b29:1; - __EXTENSION BYTE b30:1; - __EXTENSION BYTE b31:1; - __EXTENSION BYTE b32:1; - __EXTENSION BYTE b33:1; - __EXTENSION BYTE b34:1; - __EXTENSION BYTE b35:1; - __EXTENSION BYTE b36:1; - __EXTENSION BYTE b37:1; - __EXTENSION BYTE b38:1; - __EXTENSION BYTE b39:1; - __EXTENSION BYTE b40:1; - __EXTENSION BYTE b41:1; - __EXTENSION BYTE b42:1; - __EXTENSION BYTE b43:1; - __EXTENSION BYTE b44:1; - __EXTENSION BYTE b45:1; - __EXTENSION BYTE b46:1; - __EXTENSION BYTE b47:1; - __EXTENSION BYTE b48:1; - __EXTENSION BYTE b49:1; - __EXTENSION BYTE b50:1; - __EXTENSION BYTE b51:1; - __EXTENSION BYTE b52:1; - __EXTENSION BYTE b53:1; - __EXTENSION BYTE b54:1; - __EXTENSION BYTE b55:1; - __EXTENSION BYTE b56:1; - __EXTENSION BYTE b57:1; - __EXTENSION BYTE b58:1; - __EXTENSION BYTE b59:1; - __EXTENSION BYTE b60:1; - __EXTENSION BYTE b61:1; - __EXTENSION BYTE b62:1; - __EXTENSION BYTE b63:1; - } bits; -} QWORD_VAL; - -#undef __EXTENSION - -#endif /* __GENERIC_TYPE_DEFS_H_ */ +/******************************************************************* + + Generic Type Definitions + +******************************************************************** + FileName: GenericTypeDefs.h + Dependencies: None + Processor: PIC10, PIC12, PIC16, PIC18, PIC24, dsPIC, PIC32 + Compiler: MPLAB C Compilers for PIC18, PIC24, dsPIC, & PIC32 + Hi-Tech PICC PRO, Hi-Tech PICC18 PRO + Company: Microchip Technology Inc. + + Software License Agreement + + The software supplied herewith by Microchip Technology Incorporated + (the "Company") is intended and supplied to you, the Company's + customer, for use solely and exclusively with products manufactured + by the Company. + + The software is owned by the Company and/or its supplier, and is + protected under applicable copyright laws. All rights are reserved. + Any use in violation of the foregoing restrictions may subject the + user to criminal sanctions under applicable laws, as well as to + civil liability for the breach of the terms and conditions of this + license. + + THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, + WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED + TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, + IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR + CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + +******************************************************************** + File Description: + + Change History: + Rev Date Description + 1.1 09/11/06 Add base signed types + 1.2 02/28/07 Add QWORD, LONGLONG, QWORD_VAL + 1.3 02/06/08 Add def's for PIC32 + 1.4 08/08/08 Remove LSB/MSB Macros, adopted by Peripheral lib + 1.5 08/14/08 Simplify file header + 2.0 07/13/09 Updated for new release of coding standards +*******************************************************************/ + +#ifndef __GENERIC_TYPE_DEFS_H_ +#define __GENERIC_TYPE_DEFS_H_ + +/* Specify an extension for GCC based compilers */ +#if defined(__GNUC__) +#define __EXTENSION __extension__ +#else +#define __EXTENSION +#endif + +#if !defined(__PACKED) + #define __PACKED +#endif + +/* get compiler defined type definitions (NULL, size_t, etc) */ +#include + +typedef enum _BOOL { FALSE = 0, TRUE } BOOL; /* Undefined size */ +typedef enum _BIT { CLEAR = 0, SET } BIT; + +#define PUBLIC /* Function attributes */ +#define PROTECTED +#define PRIVATE static + +/* INT is processor specific in length may vary in size */ +typedef signed int INT; +typedef signed char INT8; +typedef signed short int INT16; +typedef signed long int INT32; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +__EXTENSION typedef signed long long INT64; +#endif + +/* UINT is processor specific in length may vary in size */ +typedef unsigned int UINT; +typedef unsigned char UINT8; +typedef unsigned short int UINT16; +/* 24-bit type only available on C18 */ +#if defined(__18CXX) +typedef unsigned short long UINT24; +#endif +typedef unsigned long int UINT32; /* other name for 32-bit integer */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +__EXTENSION typedef unsigned long long UINT64; +#endif + +typedef union +{ + UINT8 Val; + struct + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + } bits; +} UINT8_VAL, UINT8_BITS; + +typedef union +{ + UINT16 Val; + UINT8 v[2] __PACKED; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + } byte; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + } bits; +} UINT16_VAL, UINT16_BITS; + +/* 24-bit type only available on C18 */ +#if defined(__18CXX) +typedef union +{ + UINT24 Val; + UINT8 v[3] __PACKED; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + UINT8 UB; + } byte; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + } bits; +} UINT24_VAL, UINT24_BITS; +#endif + +typedef union +{ + UINT32 Val; + UINT16 w[2] __PACKED; + UINT8 v[4] __PACKED; + struct __PACKED + { + UINT16 LW; + UINT16 HW; + } word; + struct __PACKED + { + UINT8 LB; + UINT8 HB; + UINT8 UB; + UINT8 MB; + } byte; + struct __PACKED + { + UINT16_VAL low; + UINT16_VAL high; + }wordUnion; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + __EXTENSION UINT8 b24:1; + __EXTENSION UINT8 b25:1; + __EXTENSION UINT8 b26:1; + __EXTENSION UINT8 b27:1; + __EXTENSION UINT8 b28:1; + __EXTENSION UINT8 b29:1; + __EXTENSION UINT8 b30:1; + __EXTENSION UINT8 b31:1; + } bits; +} UINT32_VAL; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +#if !defined(__18CXX) +typedef union +{ + UINT64 Val; + UINT32 d[2] __PACKED; + UINT16 w[4] __PACKED; + UINT8 v[8] __PACKED; + struct __PACKED + { + UINT32 LD; + UINT32 HD; + } dword; + struct __PACKED + { + UINT16 LW; + UINT16 HW; + UINT16 UW; + UINT16 MW; + } word; + struct __PACKED + { + __EXTENSION UINT8 b0:1; + __EXTENSION UINT8 b1:1; + __EXTENSION UINT8 b2:1; + __EXTENSION UINT8 b3:1; + __EXTENSION UINT8 b4:1; + __EXTENSION UINT8 b5:1; + __EXTENSION UINT8 b6:1; + __EXTENSION UINT8 b7:1; + __EXTENSION UINT8 b8:1; + __EXTENSION UINT8 b9:1; + __EXTENSION UINT8 b10:1; + __EXTENSION UINT8 b11:1; + __EXTENSION UINT8 b12:1; + __EXTENSION UINT8 b13:1; + __EXTENSION UINT8 b14:1; + __EXTENSION UINT8 b15:1; + __EXTENSION UINT8 b16:1; + __EXTENSION UINT8 b17:1; + __EXTENSION UINT8 b18:1; + __EXTENSION UINT8 b19:1; + __EXTENSION UINT8 b20:1; + __EXTENSION UINT8 b21:1; + __EXTENSION UINT8 b22:1; + __EXTENSION UINT8 b23:1; + __EXTENSION UINT8 b24:1; + __EXTENSION UINT8 b25:1; + __EXTENSION UINT8 b26:1; + __EXTENSION UINT8 b27:1; + __EXTENSION UINT8 b28:1; + __EXTENSION UINT8 b29:1; + __EXTENSION UINT8 b30:1; + __EXTENSION UINT8 b31:1; + __EXTENSION UINT8 b32:1; + __EXTENSION UINT8 b33:1; + __EXTENSION UINT8 b34:1; + __EXTENSION UINT8 b35:1; + __EXTENSION UINT8 b36:1; + __EXTENSION UINT8 b37:1; + __EXTENSION UINT8 b38:1; + __EXTENSION UINT8 b39:1; + __EXTENSION UINT8 b40:1; + __EXTENSION UINT8 b41:1; + __EXTENSION UINT8 b42:1; + __EXTENSION UINT8 b43:1; + __EXTENSION UINT8 b44:1; + __EXTENSION UINT8 b45:1; + __EXTENSION UINT8 b46:1; + __EXTENSION UINT8 b47:1; + __EXTENSION UINT8 b48:1; + __EXTENSION UINT8 b49:1; + __EXTENSION UINT8 b50:1; + __EXTENSION UINT8 b51:1; + __EXTENSION UINT8 b52:1; + __EXTENSION UINT8 b53:1; + __EXTENSION UINT8 b54:1; + __EXTENSION UINT8 b55:1; + __EXTENSION UINT8 b56:1; + __EXTENSION UINT8 b57:1; + __EXTENSION UINT8 b58:1; + __EXTENSION UINT8 b59:1; + __EXTENSION UINT8 b60:1; + __EXTENSION UINT8 b61:1; + __EXTENSION UINT8 b62:1; + __EXTENSION UINT8 b63:1; + } bits; +} UINT64_VAL; +#endif /* __18CXX */ + +/***********************************************************************************/ + +/* Alternate definitions */ +typedef void VOID; + +typedef char CHAR8; +typedef unsigned char UCHAR8; + +typedef unsigned char BYTE; /* 8-bit unsigned */ +typedef unsigned short int WORD; /* 16-bit unsigned */ +typedef unsigned long DWORD; /* 32-bit unsigned */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +__EXTENSION +typedef unsigned long long QWORD; /* 64-bit unsigned */ +typedef signed char CHAR; /* 8-bit signed */ +typedef signed short int SHORT; /* 16-bit signed */ +typedef signed long LONG; /* 32-bit signed */ +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +__EXTENSION +typedef signed long long LONGLONG; /* 64-bit signed */ +typedef union +{ + BYTE Val; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + } bits; +} BYTE_VAL, BYTE_BITS; + +typedef union +{ + WORD Val; + BYTE v[2] __PACKED; + struct __PACKED + { + BYTE LB; + BYTE HB; + } byte; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + } bits; +} WORD_VAL, WORD_BITS; + +typedef union +{ + DWORD Val; + WORD w[2] __PACKED; + BYTE v[4] __PACKED; + struct __PACKED + { + WORD LW; + WORD HW; + } word; + struct __PACKED + { + BYTE LB; + BYTE HB; + BYTE UB; + BYTE MB; + } byte; + struct __PACKED + { + WORD_VAL low; + WORD_VAL high; + }wordUnion; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + __EXTENSION BYTE b16:1; + __EXTENSION BYTE b17:1; + __EXTENSION BYTE b18:1; + __EXTENSION BYTE b19:1; + __EXTENSION BYTE b20:1; + __EXTENSION BYTE b21:1; + __EXTENSION BYTE b22:1; + __EXTENSION BYTE b23:1; + __EXTENSION BYTE b24:1; + __EXTENSION BYTE b25:1; + __EXTENSION BYTE b26:1; + __EXTENSION BYTE b27:1; + __EXTENSION BYTE b28:1; + __EXTENSION BYTE b29:1; + __EXTENSION BYTE b30:1; + __EXTENSION BYTE b31:1; + } bits; +} DWORD_VAL; + +/* MPLAB C Compiler for PIC18 does not support 64-bit integers */ +typedef union +{ + QWORD Val; + DWORD d[2] __PACKED; + WORD w[4] __PACKED; + BYTE v[8] __PACKED; + struct __PACKED + { + DWORD LD; + DWORD HD; + } dword; + struct __PACKED + { + WORD LW; + WORD HW; + WORD UW; + WORD MW; + } word; + struct __PACKED + { + __EXTENSION BYTE b0:1; + __EXTENSION BYTE b1:1; + __EXTENSION BYTE b2:1; + __EXTENSION BYTE b3:1; + __EXTENSION BYTE b4:1; + __EXTENSION BYTE b5:1; + __EXTENSION BYTE b6:1; + __EXTENSION BYTE b7:1; + __EXTENSION BYTE b8:1; + __EXTENSION BYTE b9:1; + __EXTENSION BYTE b10:1; + __EXTENSION BYTE b11:1; + __EXTENSION BYTE b12:1; + __EXTENSION BYTE b13:1; + __EXTENSION BYTE b14:1; + __EXTENSION BYTE b15:1; + __EXTENSION BYTE b16:1; + __EXTENSION BYTE b17:1; + __EXTENSION BYTE b18:1; + __EXTENSION BYTE b19:1; + __EXTENSION BYTE b20:1; + __EXTENSION BYTE b21:1; + __EXTENSION BYTE b22:1; + __EXTENSION BYTE b23:1; + __EXTENSION BYTE b24:1; + __EXTENSION BYTE b25:1; + __EXTENSION BYTE b26:1; + __EXTENSION BYTE b27:1; + __EXTENSION BYTE b28:1; + __EXTENSION BYTE b29:1; + __EXTENSION BYTE b30:1; + __EXTENSION BYTE b31:1; + __EXTENSION BYTE b32:1; + __EXTENSION BYTE b33:1; + __EXTENSION BYTE b34:1; + __EXTENSION BYTE b35:1; + __EXTENSION BYTE b36:1; + __EXTENSION BYTE b37:1; + __EXTENSION BYTE b38:1; + __EXTENSION BYTE b39:1; + __EXTENSION BYTE b40:1; + __EXTENSION BYTE b41:1; + __EXTENSION BYTE b42:1; + __EXTENSION BYTE b43:1; + __EXTENSION BYTE b44:1; + __EXTENSION BYTE b45:1; + __EXTENSION BYTE b46:1; + __EXTENSION BYTE b47:1; + __EXTENSION BYTE b48:1; + __EXTENSION BYTE b49:1; + __EXTENSION BYTE b50:1; + __EXTENSION BYTE b51:1; + __EXTENSION BYTE b52:1; + __EXTENSION BYTE b53:1; + __EXTENSION BYTE b54:1; + __EXTENSION BYTE b55:1; + __EXTENSION BYTE b56:1; + __EXTENSION BYTE b57:1; + __EXTENSION BYTE b58:1; + __EXTENSION BYTE b59:1; + __EXTENSION BYTE b60:1; + __EXTENSION BYTE b61:1; + __EXTENSION BYTE b62:1; + __EXTENSION BYTE b63:1; + } bits; +} QWORD_VAL; + +#undef __EXTENSION + +#endif /* __GENERIC_TYPE_DEFS_H_ */ diff --git a/Firmware/HD44780.c b/Firmware/HD44780.c index e283fb6d..67d0d191 100644 --- a/Firmware/HD44780.c +++ b/Firmware/HD44780.c @@ -1,424 +1,424 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "SPI.h" -#include "base.h" -#include "procMenu.h" - -#ifdef BP_USE_LCD - -//direction registers -#define SPIMOSI_TRIS BP_MOSI_DIR -#define SPICLK_TRIS BP_CLK_DIR -#define SPIMISO_TRIS BP_MISO_DIR -#define SPICS_TRIS BP_CS_DIR - -//pin control registers -#define SPIMOSI BP_MOSI -#define SPICLK BP_CLK -#define SPIMISO BP_MISO -#define SPICS BP_CS - -//Define how the PCF8574 pins connect to the LCD -#define HCT595_LCD_LED 0b00000001 -#define HCT595_LCD_RS 0b00000010 -#define HCT595_LCD_RW 0b00000100 -#define HCT595_LCD_EN 0b00001000 -#define HCT595_LCD_D4 0b00010000 -#define HCT595_LCD_D5 0b00100000 -#define HCT595_LCD_D6 0b01000000 -#define HCT595_LCD_D7 0b10000000 - - -//RS (register select) pin states -#define HD44780_COMMAND 0 -#define HD44780_DATA 1 - -// -//HD44780 commands and related options -// -#define CMD_CLEARDISPLAY 0b00000001 //82us-1.64ms - -#define CMD_RETURNHOME 0b00000010 //40us-1.64ms - -#define CMD_ENTRYMODESET 0b00000100 //40us -#define INCREMENT 0b10 -#define DECREMENT 0b00 -#define DISPLAYSHIFTON 0b1 -#define DISPLAYSHIFTOFF 0 - -#define CMD_DISPLAYCONTROL 0b00001000 //40us -#define DISPLAYON 0b100 -#define DISPLAYOFF 0 -#define CURSERON 0b10 -#define CURSEROFF 0 -#define BLINKON 0b1 -#define BLINKOFF 0 - -#define CMD_CURSERDISPLAYSHIFT 0b00010000 //40us -#define DISPLAYSHIFT 0b1000 -#define CURSERMOVE 0 -#define SHIFTRIGHT 0b100 -#define SHIFTLEFT 0 - -#define CMD_FUNCTIONSET 0b00100000 //40us -#define DATAWIDTH8 0b10000 -#define DATAWIDTH4 0 -#define DISPLAYLINES2 0b1000 -#define DISPLAYLINES1 0 -#define FONT5X10 0b100 -#define FONT5X7 0 -#define MODULE24X4 0b1 - -#define CMD_SETCGRAMADDR 0b01000000 //40us -//6bit character generator RAM address - -#define CMD_SETDDRAMADDR 0b10000000 //40us -//7bit display data RAM address - -//configuration structure -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; - -struct _HD44780_interface { - unsigned char EN:1; //clock, active high - unsigned char RS:1; //register select, 0=command, 1=text - unsigned char RW:1; //read write, 0=write, 1=read - unsigned char LED:1; //LED on IO board, 0=on, 1=off - //unsigned char dat; //8 data bits -} HD44780; - -void HD44780_Reset(void); //reset the LCD to 4 bit mode -void HD44780_Init(unsigned char displaylines); //initialize LCD to 4bit mode with typical settings and X displaylines -void HD44780_WriteString(char *s); //write a string to the LCD -void HD44780_WriteByte(unsigned char reg, unsigned char dat); //write a byte to LCD to register REG -void HD44780_WriteNibble(unsigned char reg, unsigned char dat);//write 4 bits to LCD to register REG -void HD44780_SPIwrite(unsigned char datout); //abstracts data output to PCF8574 IO expander over I2C bus -void LCDsetup_exc(void); - -unsigned int LCDwrite(unsigned int c) -{ - HD44780_WriteByte(HD44780.RS, c); - return 0x100; -} - -void LCDstart(void) -{ HD44780.RS=HD44780_COMMAND; - //bpWline(OUMSG_LCD_COMMAND_MODE); - BPMSG1213; -} - -void LCDstop(void) -{ HD44780.RS=HD44780_DATA; - //bpWline(OUMSG_LCD_DATA_MODE); - BPMSG1214; -} - -void LCDsetup(void) -{ - modeConfig.HiZ=1;//yes, always HiZ -} - -void LCDsetup_exc(void) -{ - HD44780.RS=HD44780_DATA; -/* - consumewhitechars(); - type=getint(); - consumewhitechars(); - address=getint(); - - if(!((type==1)&&(address>=0x00)&&(address<=0xFF))) - { cmderror=0; - - //bpWline(OUMSG_LCD_SETUP_ADAPTER); - BPMSG1217; - //c=bpUserNumberPrompt(1, 1, 1); - getnumber(1,1,1,0); // schiet mij maar in een kapotje :S - - // address of expander? - BPMSG1215; - HD44780.PCF8574=getnumber(0x40,0, 255, 0); - } - else - { BPMSG1218; - bpWdec(type); bpSP; - bpWdec(address); bpSP; - BPMSG1162; - HD44780.PCF8574=address; - } -*/ - - //PPS Setup - // Inputs - RPINR20bits.SDI1R = BP_MISO_RPIN; //MISO - // Outputs - BP_MOSI_RPOUT = SDO1_IO; //B9 MOSI - BP_CLK_RPOUT = SCK1OUT_IO; //B8 CLK - -// #if defined(BUSPIRATEV3) -// // Inputs -// RPINR20bits.SDI1R = 7; //B7 MISO -// // Outputs -// RPOR4bits.RP9R = SDO1_IO; //B9 MOSI -// RPOR4bits.RP8R = SCK1OUT_IO; //B8 CLK -// #elif defined(BUSPIRATEV4) -// // Inputs -// RPINR20bits.SDI1R = 22; //B7 MISO -// // Outputs -// RPOR12bits.RP24R = SDO1_IO; //B9 MOSI -// RPOR11bits.RP23R = SCK1OUT_IO; //B8 CLK -// #endif - - SPICS=0; //B6 cs low - SPICS_TRIS=0; //B6 cs output - - //pps configures pins and this doesn't really matter.... - SPICLK_TRIS=0; //B8 sck output - SPIMISO_TRIS=1; //B7 SDI input - SPIMOSI_TRIS=0; //B9 SDO output - - /* CKE=1, CKP=0, SMP=0 */ - SPI1CON1 = 0b0100111101;//(SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. - //SPI1CON1=0b11101; - //SPI1CON1bits.MSTEN=1; - //SPI1CON1bits.CKP=0; - //SPI1CON1bits.CKE=1; - //SPI1CON1bits.SMP=0; - SPI1CON2 = 0; - SPI1STAT = 0; // clear SPI - SPI1STATbits.SPIEN = 1; - - //bpWmessage(MSG_ADAPTER); - BPMSG1216; -} - -void LCDmacro(unsigned int c) -{ int input,i; - - cmdstart=(cmdstart+1)&CMDLENMSK; - //cmdstart&=CMDLENMSK; - consumewhitechars(); - input=getint(); - cmderror=0; - - switch(c) - { case 0://menu - //bpWline(OUMSG_LCD_MACRO_MENU); - BPMSG1219; - break; - case 1: - case 2: - //bpWline(OUMSG_LCD_MACRO_RESET); - BPMSG1093; - HD44780_Reset(); - if(c==1) break; - - if(!((input>=1)&&(input<=2))) - { //bpWline(OUMSG_LCD_MACRO_INIT_DISPLAYLINES); - BPMSG1220; - //c=bpUserNumberPrompt(1, 2, 2); - input=getnumber(2,1,2, 0); - if(input==1) HD44780_Init(DISPLAYLINES1); else HD44780_Init(DISPLAYLINES2); - //bpWline(OUMSG_LCD_MACRO_INIT); - } - BPMSG1221; - break; - case 3: //Clear LCD and return home - HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); - bpDelayMS(15);//delay 15ms - //bpWline(OUMSG_LCD_MACRO_CLEAR); - BPMSG1222; - break; - case 4: - HD44780_WriteByte(HD44780_COMMAND, CMD_SETDDRAMADDR | (unsigned char)input); - //bpWline(OUMSG_LCD_MACRO_CURSOR); - BPMSG1223; - break; - case 6: //write numbers - HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY);//Clear LCD and return home - bpDelayMS(15);//delay 15ms - c=0x30; - if(input==0) input=80; - for(i=0; i0x39) c=0x30; - HD44780_WriteByte(HD44780_DATA, c); - UART1TX(c); - c++; - } - break; - case 7://write characters - HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); //Clear LCD and return home - bpDelayMS(15);//delay 15ms - c=0x21; //start character (!) - if(input==0) input=80; - for(i=0; i127)c=0x21; - HD44780_WriteByte(HD44780_DATA, c); - UART1TX(c); - c++; - } - break; -/* case 8://terminal mode/pass through //superseeded by send string command - bpWline(OUMSG_LCD_MACRO_TEXT); - HD44780_Term(); - break; */ - default: - //bpWmessage(MSG_ERROR_MACRO); - BPMSG1016; - } - -} - -void LCDpins(void) { - #if defined(BUSPIRATEV4) - BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); - #else - BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); - #endif -} - - -//initialize LCD to 4bits with standard features -//displaylines=0 for single line displays, displaylines=1 for multiline displays -void HD44780_Init(unsigned char displaylines){ - //Function set - HD44780_WriteByte(HD44780_COMMAND, (CMD_FUNCTIONSET + DATAWIDTH4 + FONT5X7 + displaylines)); //0x28, 0b101000 - bpDelayMS(15);//delay 15ms - - //Turn display off - HD44780_WriteByte(HD44780_COMMAND, CMD_DISPLAYCONTROL + DISPLAYOFF + CURSEROFF + BLINKOFF);//0x08, 0b1000 - bpDelayMS(15);//delay 15ms - - //Clear LCD and return home - HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); - bpDelayMS(15);//delay 15ms - - //Turn on display, turn off cursor and blink - HD44780_WriteByte(HD44780_COMMAND, CMD_DISPLAYCONTROL + DISPLAYON + CURSERON + BLINKOFF); // 0x0f, 0b1111 - bpDelayMS(15);//delay 15ms -} - -//reset LCD to 4bit mode -void HD44780_Reset(void){ - - HD44780_SPIwrite(0); //clear IO pins to HD44780 - //# Wait more than 15 msecs after power is applied. - bpDelayMS(15); - //# Write 0x03 to LCD and wait 5 msecs for the instruction to complete - HD44780_WriteNibble(HD44780_COMMAND, 0x03); - bpDelayMS(5); - //# Write 0x03 to LCD and wait 160 usecs for instruction to complete - HD44780_WriteNibble(HD44780_COMMAND, 0x03); - bpDelayUS(160); - //# Write 0x03 AGAIN to LCD and wait 160 usecs (or poll the Busy Flag) - HD44780_WriteNibble(HD44780_COMMAND, 0x03); - bpDelayUS(160); - //Set the Operating Characteristics of the LCD - //* Write 0x02 to the LCD to Enable Four Bit Mode - HD44780_WriteNibble(HD44780_COMMAND, 0x02); - bpDelayUS(160); -} - -//write byte dat to register reg -void HD44780_WriteByte(unsigned char reg, unsigned char dat){ - HD44780_WriteNibble(reg, (dat>>4) ); - HD44780_WriteNibble(reg, (dat & 0x0F)); -} - -//write 4 bits dat to register reg -void HD44780_WriteNibble(unsigned char reg, unsigned char dat){ - //EN pin should already be low - //RW bit should be 0 (already 0 in dat) - //LED bit should be 0 (already 0 in dat) - - //databits are aligned with the upper four bits of 595 - //push them from the lower four bits of dat ot the upper four bits - dat=dat<<4; - - dat|=HCT595_LCD_LED; //keep LED on - - if(reg==HD44780_DATA){ //does RS need to be set? - dat |= HCT595_LCD_RS; //set register select flag for text - }//leave as 0 for a command - - HD44780_SPIwrite(dat); //load values - - dat |= HCT595_LCD_EN; //raise the EN line to clock in the values - HD44780_SPIwrite(dat); - - //optional setup delay - - dat &= (~HCT595_LCD_EN); //lower EN line - HD44780_SPIwrite(dat); - -} -#ifndef BP_MAIN - -//open drain control registers for OUTPUT pins -#define SPIMOSI_ODC BP_MISO_ODC -#define SPICLK_ODC BP_CLK_ODC -#define SPICS_ODC BP_CS_ODC - - -// copied from spi.c, but with thesplitfirmware it was gone.. -unsigned char spiWriteByte(unsigned char c){ - - SPI1BUF = c; - while(!IFS0bits.SPI1IF); - c=SPI1BUF; - IFS0bits.SPI1IF = 0; - return c; -} - -void spiDisable(void){ - SPI1STATbits.SPIEN = 0; - RPINR20bits.SDI1R=0b11111; //B7 MISO - - //PPS Disable - BP_MOSI_RPOUT=0; - BP_CLK_RPOUT=0; - -// #if defined(BUSPIRATEV3) -// RPOR4bits.RP9R=0; //B9 MOSI -// RPOR4bits.RP8R=0; //B8 CLK -// #elif defined(BUSPIRATEV4) -// RPOR12bits.RP24R=0; //B9 MOSI -// RPOR11bits.RP23R=0; //B8 CLK -// #endif - - - //disable all open drain control register bits - SPIMOSI_ODC=0; - SPICLK_ODC=0; - SPICS_ODC=0; - //make all input maybe??? -} - - -#endif - - -//write value to 595 IO expander, message and return on ACK error -void HD44780_SPIwrite(unsigned char datout){ - spiWriteByte(datout); - SPICS=1; - //bpDelayUS(255); - SPICS=0; -} - - -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "SPI.h" +#include "base.h" +#include "procMenu.h" + +#ifdef BP_USE_LCD + +//direction registers +#define SPIMOSI_TRIS BP_MOSI_DIR +#define SPICLK_TRIS BP_CLK_DIR +#define SPIMISO_TRIS BP_MISO_DIR +#define SPICS_TRIS BP_CS_DIR + +//pin control registers +#define SPIMOSI BP_MOSI +#define SPICLK BP_CLK +#define SPIMISO BP_MISO +#define SPICS BP_CS + +//Define how the PCF8574 pins connect to the LCD +#define HCT595_LCD_LED 0b00000001 +#define HCT595_LCD_RS 0b00000010 +#define HCT595_LCD_RW 0b00000100 +#define HCT595_LCD_EN 0b00001000 +#define HCT595_LCD_D4 0b00010000 +#define HCT595_LCD_D5 0b00100000 +#define HCT595_LCD_D6 0b01000000 +#define HCT595_LCD_D7 0b10000000 + + +//RS (register select) pin states +#define HD44780_COMMAND 0 +#define HD44780_DATA 1 + +// +//HD44780 commands and related options +// +#define CMD_CLEARDISPLAY 0b00000001 //82us-1.64ms + +#define CMD_RETURNHOME 0b00000010 //40us-1.64ms + +#define CMD_ENTRYMODESET 0b00000100 //40us +#define INCREMENT 0b10 +#define DECREMENT 0b00 +#define DISPLAYSHIFTON 0b1 +#define DISPLAYSHIFTOFF 0 + +#define CMD_DISPLAYCONTROL 0b00001000 //40us +#define DISPLAYON 0b100 +#define DISPLAYOFF 0 +#define CURSERON 0b10 +#define CURSEROFF 0 +#define BLINKON 0b1 +#define BLINKOFF 0 + +#define CMD_CURSERDISPLAYSHIFT 0b00010000 //40us +#define DISPLAYSHIFT 0b1000 +#define CURSERMOVE 0 +#define SHIFTRIGHT 0b100 +#define SHIFTLEFT 0 + +#define CMD_FUNCTIONSET 0b00100000 //40us +#define DATAWIDTH8 0b10000 +#define DATAWIDTH4 0 +#define DISPLAYLINES2 0b1000 +#define DISPLAYLINES1 0 +#define FONT5X10 0b100 +#define FONT5X7 0 +#define MODULE24X4 0b1 + +#define CMD_SETCGRAMADDR 0b01000000 //40us +//6bit character generator RAM address + +#define CMD_SETDDRAMADDR 0b10000000 //40us +//7bit display data RAM address + +//configuration structure +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; + +struct _HD44780_interface { + unsigned char EN:1; //clock, active high + unsigned char RS:1; //register select, 0=command, 1=text + unsigned char RW:1; //read write, 0=write, 1=read + unsigned char LED:1; //LED on IO board, 0=on, 1=off + //unsigned char dat; //8 data bits +} HD44780; + +void HD44780_Reset(void); //reset the LCD to 4 bit mode +void HD44780_Init(unsigned char displaylines); //initialize LCD to 4bit mode with typical settings and X displaylines +void HD44780_WriteString(char *s); //write a string to the LCD +void HD44780_WriteByte(unsigned char reg, unsigned char dat); //write a byte to LCD to register REG +void HD44780_WriteNibble(unsigned char reg, unsigned char dat);//write 4 bits to LCD to register REG +void HD44780_SPIwrite(unsigned char datout); //abstracts data output to PCF8574 IO expander over I2C bus +void LCDsetup_exc(void); + +unsigned int LCDwrite(unsigned int c) +{ + HD44780_WriteByte(HD44780.RS, c); + return 0x100; +} + +void LCDstart(void) +{ HD44780.RS=HD44780_COMMAND; + //bpWline(OUMSG_LCD_COMMAND_MODE); + BPMSG1213; +} + +void LCDstop(void) +{ HD44780.RS=HD44780_DATA; + //bpWline(OUMSG_LCD_DATA_MODE); + BPMSG1214; +} + +void LCDsetup(void) +{ + modeConfig.HiZ=1;//yes, always HiZ +} + +void LCDsetup_exc(void) +{ + HD44780.RS=HD44780_DATA; +/* + consumewhitechars(); + type=getint(); + consumewhitechars(); + address=getint(); + + if(!((type==1)&&(address>=0x00)&&(address<=0xFF))) + { cmderror=0; + + //bpWline(OUMSG_LCD_SETUP_ADAPTER); + BPMSG1217; + //c=bpUserNumberPrompt(1, 1, 1); + getnumber(1,1,1,0); // schiet mij maar in een kapotje :S + + // address of expander? + BPMSG1215; + HD44780.PCF8574=getnumber(0x40,0, 255, 0); + } + else + { BPMSG1218; + bpWdec(type); bpSP; + bpWdec(address); bpSP; + BPMSG1162; + HD44780.PCF8574=address; + } +*/ + + //PPS Setup + // Inputs + RPINR20bits.SDI1R = BP_MISO_RPIN; //MISO + // Outputs + BP_MOSI_RPOUT = SDO1_IO; //B9 MOSI + BP_CLK_RPOUT = SCK1OUT_IO; //B8 CLK + +// #if defined(BUSPIRATEV3) +// // Inputs +// RPINR20bits.SDI1R = 7; //B7 MISO +// // Outputs +// RPOR4bits.RP9R = SDO1_IO; //B9 MOSI +// RPOR4bits.RP8R = SCK1OUT_IO; //B8 CLK +// #elif defined(BUSPIRATEV4) +// // Inputs +// RPINR20bits.SDI1R = 22; //B7 MISO +// // Outputs +// RPOR12bits.RP24R = SDO1_IO; //B9 MOSI +// RPOR11bits.RP23R = SCK1OUT_IO; //B8 CLK +// #endif + + SPICS=0; //B6 cs low + SPICS_TRIS=0; //B6 cs output + + //pps configures pins and this doesn't really matter.... + SPICLK_TRIS=0; //B8 sck output + SPIMISO_TRIS=1; //B7 SDI input + SPIMOSI_TRIS=0; //B9 SDO output + + /* CKE=1, CKP=0, SMP=0 */ + SPI1CON1 = 0b0100111101;//(SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. + //SPI1CON1=0b11101; + //SPI1CON1bits.MSTEN=1; + //SPI1CON1bits.CKP=0; + //SPI1CON1bits.CKE=1; + //SPI1CON1bits.SMP=0; + SPI1CON2 = 0; + SPI1STAT = 0; // clear SPI + SPI1STATbits.SPIEN = 1; + + //bpWmessage(MSG_ADAPTER); + BPMSG1216; +} + +void LCDmacro(unsigned int c) +{ int input,i; + + cmdstart=(cmdstart+1)&CMDLENMSK; + //cmdstart&=CMDLENMSK; + consumewhitechars(); + input=getint(); + cmderror=0; + + switch(c) + { case 0://menu + //bpWline(OUMSG_LCD_MACRO_MENU); + BPMSG1219; + break; + case 1: + case 2: + //bpWline(OUMSG_LCD_MACRO_RESET); + BPMSG1093; + HD44780_Reset(); + if(c==1) break; + + if(!((input>=1)&&(input<=2))) + { //bpWline(OUMSG_LCD_MACRO_INIT_DISPLAYLINES); + BPMSG1220; + //c=bpUserNumberPrompt(1, 2, 2); + input=getnumber(2,1,2, 0); + if(input==1) HD44780_Init(DISPLAYLINES1); else HD44780_Init(DISPLAYLINES2); + //bpWline(OUMSG_LCD_MACRO_INIT); + } + BPMSG1221; + break; + case 3: //Clear LCD and return home + HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); + bpDelayMS(15);//delay 15ms + //bpWline(OUMSG_LCD_MACRO_CLEAR); + BPMSG1222; + break; + case 4: + HD44780_WriteByte(HD44780_COMMAND, CMD_SETDDRAMADDR | (unsigned char)input); + //bpWline(OUMSG_LCD_MACRO_CURSOR); + BPMSG1223; + break; + case 6: //write numbers + HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY);//Clear LCD and return home + bpDelayMS(15);//delay 15ms + c=0x30; + if(input==0) input=80; + for(i=0; i0x39) c=0x30; + HD44780_WriteByte(HD44780_DATA, c); + UART1TX(c); + c++; + } + break; + case 7://write characters + HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); //Clear LCD and return home + bpDelayMS(15);//delay 15ms + c=0x21; //start character (!) + if(input==0) input=80; + for(i=0; i127)c=0x21; + HD44780_WriteByte(HD44780_DATA, c); + UART1TX(c); + c++; + } + break; +/* case 8://terminal mode/pass through //superseeded by send string command + bpWline(OUMSG_LCD_MACRO_TEXT); + HD44780_Term(); + break; */ + default: + //bpWmessage(MSG_ERROR_MACRO); + BPMSG1016; + } + +} + +void LCDpins(void) { + #if defined(BUSPIRATEV4) + BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); + #else + BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); + #endif +} + + +//initialize LCD to 4bits with standard features +//displaylines=0 for single line displays, displaylines=1 for multiline displays +void HD44780_Init(unsigned char displaylines){ + //Function set + HD44780_WriteByte(HD44780_COMMAND, (CMD_FUNCTIONSET + DATAWIDTH4 + FONT5X7 + displaylines)); //0x28, 0b101000 + bpDelayMS(15);//delay 15ms + + //Turn display off + HD44780_WriteByte(HD44780_COMMAND, CMD_DISPLAYCONTROL + DISPLAYOFF + CURSEROFF + BLINKOFF);//0x08, 0b1000 + bpDelayMS(15);//delay 15ms + + //Clear LCD and return home + HD44780_WriteByte(HD44780_COMMAND, CMD_CLEARDISPLAY); + bpDelayMS(15);//delay 15ms + + //Turn on display, turn off cursor and blink + HD44780_WriteByte(HD44780_COMMAND, CMD_DISPLAYCONTROL + DISPLAYON + CURSERON + BLINKOFF); // 0x0f, 0b1111 + bpDelayMS(15);//delay 15ms +} + +//reset LCD to 4bit mode +void HD44780_Reset(void){ + + HD44780_SPIwrite(0); //clear IO pins to HD44780 + //# Wait more than 15 msecs after power is applied. + bpDelayMS(15); + //# Write 0x03 to LCD and wait 5 msecs for the instruction to complete + HD44780_WriteNibble(HD44780_COMMAND, 0x03); + bpDelayMS(5); + //# Write 0x03 to LCD and wait 160 usecs for instruction to complete + HD44780_WriteNibble(HD44780_COMMAND, 0x03); + bpDelayUS(160); + //# Write 0x03 AGAIN to LCD and wait 160 usecs (or poll the Busy Flag) + HD44780_WriteNibble(HD44780_COMMAND, 0x03); + bpDelayUS(160); + //Set the Operating Characteristics of the LCD + //* Write 0x02 to the LCD to Enable Four Bit Mode + HD44780_WriteNibble(HD44780_COMMAND, 0x02); + bpDelayUS(160); +} + +//write byte dat to register reg +void HD44780_WriteByte(unsigned char reg, unsigned char dat){ + HD44780_WriteNibble(reg, (dat>>4) ); + HD44780_WriteNibble(reg, (dat & 0x0F)); +} + +//write 4 bits dat to register reg +void HD44780_WriteNibble(unsigned char reg, unsigned char dat){ + //EN pin should already be low + //RW bit should be 0 (already 0 in dat) + //LED bit should be 0 (already 0 in dat) + + //databits are aligned with the upper four bits of 595 + //push them from the lower four bits of dat ot the upper four bits + dat=dat<<4; + + dat|=HCT595_LCD_LED; //keep LED on + + if(reg==HD44780_DATA){ //does RS need to be set? + dat |= HCT595_LCD_RS; //set register select flag for text + }//leave as 0 for a command + + HD44780_SPIwrite(dat); //load values + + dat |= HCT595_LCD_EN; //raise the EN line to clock in the values + HD44780_SPIwrite(dat); + + //optional setup delay + + dat &= (~HCT595_LCD_EN); //lower EN line + HD44780_SPIwrite(dat); + +} +#ifndef BP_MAIN + +//open drain control registers for OUTPUT pins +#define SPIMOSI_ODC BP_MISO_ODC +#define SPICLK_ODC BP_CLK_ODC +#define SPICS_ODC BP_CS_ODC + + +// copied from spi.c, but with thesplitfirmware it was gone.. +unsigned char spiWriteByte(unsigned char c){ + + SPI1BUF = c; + while(!IFS0bits.SPI1IF); + c=SPI1BUF; + IFS0bits.SPI1IF = 0; + return c; +} + +void spiDisable(void){ + SPI1STATbits.SPIEN = 0; + RPINR20bits.SDI1R=0b11111; //B7 MISO + + //PPS Disable + BP_MOSI_RPOUT=0; + BP_CLK_RPOUT=0; + +// #if defined(BUSPIRATEV3) +// RPOR4bits.RP9R=0; //B9 MOSI +// RPOR4bits.RP8R=0; //B8 CLK +// #elif defined(BUSPIRATEV4) +// RPOR12bits.RP24R=0; //B9 MOSI +// RPOR11bits.RP23R=0; //B8 CLK +// #endif + + + //disable all open drain control register bits + SPIMOSI_ODC=0; + SPICLK_ODC=0; + SPICS_ODC=0; + //make all input maybe??? +} + + +#endif + + +//write value to 595 IO expander, message and return on ACK error +void HD44780_SPIwrite(unsigned char datout){ + spiWriteByte(datout); + SPICS=1; + //bpDelayUS(255); + SPICS=0; +} + + +#endif diff --git a/Firmware/HD44780.h b/Firmware/HD44780.h index 94785351..69c74636 100644 --- a/Firmware/HD44780.h +++ b/Firmware/HD44780.h @@ -1,35 +1,35 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//void HD44780Process(void); - -unsigned int LCDread(void); -unsigned int LCDwrite(unsigned int c); -void LCDstart(void); -void LCDstop(void); -void LCDsetup(void); -void LCDsetup_exc(void); -void LCDmacro(unsigned int c); -void LCDpins(void); - - -// to satisfy the compiler when split firmware -#ifndef BP_MAIN -void spiDisable(void); -#endif - - - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//void HD44780Process(void); + +unsigned int LCDread(void); +unsigned int LCDwrite(unsigned int c); +void LCDstart(void); +void LCDstop(void); +void LCDsetup(void); +void LCDsetup_exc(void); +void LCDmacro(unsigned int c); +void LCDpins(void); + + +// to satisfy the compiler when split firmware +#ifndef BP_MAIN +void spiDisable(void); +#endif + + + + diff --git a/Firmware/I2C.c b/Firmware/I2C.c index fd5578ec..288ceb05 100644 --- a/Firmware/I2C.c +++ b/Firmware/I2C.c @@ -1,1019 +1,1019 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -#include "bitbang.h" -#include "busPirateCore.h"//need access to bpConfig -#include "binIOhelpers.h" -#include "AUXpin.h" - -#include "procMenu.h" // for the userinteraction subs - -//software or hardware I2C mode defines -#define SOFT 0 -#define HARD 1 -#if defined (BUSPIRATEV4) -#define BP_USE_I2C_HW -unsigned char i2cinternal = 0; -#endif - -#define SCL BP_CLK -#define SCL_TRIS BP_CLK_DIR //-- The SCL Direction Register Bit -#define SDA BP_MOSI //-- The SDA output pin -#define SDA_TRIS BP_MOSI_DIR //-- The SDA Direction Register Bit - -extern struct _bpConfig bpConfig; //holds persistant bus pirate settings (see base.h) need hardware version info -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; - -/* -static struct _i2csniff { - unsigned char bits; - unsigned char data; - unsigned char ACK:1; - unsigned char datastate:1; //are we collecting data yet? - unsigned char I2CD:2; - unsigned char I2CC:2; //prevous and current clock pin state -} I2Csniff; - */ -//hardware functions -void hwi2cSetup(void); -void hwi2cstart(void); -void hwi2cstop(void); -void hwi2csendack(unsigned char ack); -unsigned char hwi2cgetack(void); -void hwi2cwrite(unsigned char c); -unsigned char hwi2cread(void); -void binI2CversionString(void); - -#ifdef BP_USE_I2C_HW -static unsigned char I2Cspeed[] = {157, 37, 13}; //100,400,1000khz; datasheet pg 145 -#endif - -//software functions -void I2C_Setup(void); -void I2Csetup_exc(void); -void I2C_SnifferSetup(void); -void I2C_Sniffer(unsigned char termMode); - -int i2cmode; -int ackPending; - -/* -extern int getnumber(int def, int max); //linker happy? everybody happy :D -// move into a .h or other .c??? -int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* -int getint(void); -int getrepeat(void); -void consumewhitechars(void); -extern int cmderror; - */ - -unsigned int I2Cread(void) { - unsigned char c = 0; - if (ackPending) { - bpSP; - //bpWmessage(MSG_ACK); - BPMSG1060; - bpSP; - if (i2cmode == SOFT) { - bbI2Cack(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2csendack(0); //all other reads get an ACK - } -#endif - ackPending = 0; - } - - if (i2cmode == SOFT) { - c = bbReadByte(); - } -#ifdef BP_USE_I2C_HW - else { - c = hwi2cread(); - } -#endif - ackPending = 1; - return c; -} - -unsigned int I2Cwrite(unsigned int c) { //unsigned char c; - if (ackPending) { - bpSP; - //bpWmessage(MSG_ACK); - BPMSG1060; - bpSP; - if (i2cmode == SOFT) { - bbI2Cack(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2csendack(0); //all other reads get an ACK - } -#endif - ackPending = 0; - } - - if (i2cmode == SOFT) { - bbWriteByte(c); - c = bbReadBit(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2cwrite(c); - c = hwi2cgetack(); - } -#endif - bpSP; - if (c == 0) { //bpWmessage(MSG_ACK); - BPMSG1060; - return 0x300; // bit 9=ack - } else { //bpWmessage(MSG_NACK); - BPMSG1061; - return 0x100; // bit 9=ack - } -} - -void I2Cstart(void) { - if (ackPending) { //bpWmessage(MSG_NACK); - BPMSG1061; - bpBR; //bpWline(OUMSG_I2C_READ_PEND_NACK); - if (i2cmode == SOFT) { - bbI2Cnack(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2csendack(1); //the last read before a stop/start condition gets an NACK - } -#endif - ackPending = 0; - } - - if (i2cmode == SOFT) { - if (bbI2Cstart()) {//bus contention - BPMSG1019; //warning - BPMSG1020; //short or no pullups - bpBR; - } - } -#ifdef BP_USE_I2C_HW - else { - hwi2cstart(); - } -#endif - //bpWmessage(MSG_I2C_START); - BPMSG1062; -} - -void I2Cstop(void) { - if (ackPending) { //bpWmessage(MSG_NACK); - BPMSG1061; - bpBR; //bpWline(OUMSG_I2C_READ_PEND_NACK); - if (i2cmode == SOFT) { - bbI2Cnack(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2csendack(1); //the last read before a stop/start condition gets an NACK - } -#endif - ackPending = 0; - } - - if (i2cmode == SOFT) { - bbI2Cstop(); - } -#ifdef BP_USE_I2C_HW - else { - hwi2cstop(); - } -#endif - //bpWmessage(MSG_I2C_STOP); - BPMSG1063; -} - -void I2Csettings(void) { //bpWstring("I2C (mod spd)=( "); - BPMSG1068; -#ifdef BP_USE_I2C_HW - bpWdec(i2cmode); - bpSP; -#else - bpWdec(0); - bpSP; // softmode -#endif - bpWdec(modeConfig.speed); - bpSP; - bpWline(")"); -} - -void I2Csetup(void) { - int HW, speed; - - HW = 0; // keep compiler happy if BP_USE_HW is not defined - -#ifdef BP_USE_I2C_HW - consumewhitechars(); - HW = getint(); -#else - i2cmode = SOFT; -#endif - - consumewhitechars(); - speed = getint(); - -#ifdef BP_USE_I2C_HW - if ((HW > 0) && (HW <= 2)) { - i2cmode = HW - 1; - } else { - speed = 0; - } -#endif - - if ((speed > 0) && (speed <= 4)) { - modeConfig.speed = speed - 1; - } else { - speed = 0; - } - - if (speed == 0) { - cmderror = 0; - -#ifdef BP_USE_I2C_HW - //bpWline(OUMSG_I2C_CON); - BPMSG1064; - i2cmode = (getnumber(1, 1, 2, 0) - 1); -#else - i2cmode = SOFT; -#endif - - if (i2cmode == SOFT) { - //bpWmessage(MSG_OPT_BB_SPEED); - BPMSG1065; - modeConfig.speed = (getnumber(1, 1, 4, 0) - 1); - } else { -#if defined (BUSPIRATEV1A)||defined (BUSPIRATEV2) - // There is a hardware incompatibility with > 1)); - if ((i & 0b1) == 0) {//if the first bit is set it's a read address, send a byte plus nack to clean up - bpWstring(" W"); - } else { - if (i2cmode == SOFT) { - bbReadByte(); - bbI2Cnack(); //bbWriteBit(1);//high bit is NACK - } -#ifdef BP_USE_I2C_HW - else { - hwi2cread(); - hwi2csendack(1); //high bit is NACK - } -#endif - bpWstring(" R"); - } - bpWstring(")"); - bpSP; - } - if (i2cmode == SOFT) bbI2Cstop(); -#ifdef BP_USE_I2C_HW - else hwi2cstop(); -#endif - } - bpWBR; - - break; - case 2: - if (i2cmode == HARD)I2C1CONbits.I2CEN = 0; //disable I2C module - - //bpWline(OUMSG_I2C_MACRO_SNIFFER); - BPMSG1071; - BPMSG1250; - I2C_Sniffer(1); //set for terminal output - -#ifdef BP_USE_I2C_HW - if (i2cmode == HARD) hwi2cSetup(); //setup hardware I2C again -#endif - break; -#if defined (BUSPIRATEV4) - case 3: //in hardware mode (or software, I guess) we can edit the on-board EEPROM -software mode unimplemented... - bpWline("Now using on-board EEPROM I2C interface"); - i2cinternal = 1; - I2C1CONbits.A10M = 0; - I2C1CONbits.SCLREL = 0; - - I2C1ADD = 0; - I2C1MSK = 0; - - // Enable SMBus - I2C1CONbits.SMEN = 0; - - // Baud rate setting - I2C1BRG = I2Cspeed[modeConfig.speed]; - - // Enable I2C module - I2C1CONbits.I2CEN = 1; - - // disable other I2C module - I2C3CONbits.I2CEN = 0; - break; - case 4: - if (i2cinternal == 1) { - bpWline("On-board EEPROM write protect disabled"); - BP_EE_WP = 0; - } - break; -#endif - default: - //bpWmessage(MSG_ERROR_MACRO); - BPMSG1016; - } -} - -void I2Cpins(void) { - #if defined(BUSPIRATEV4) - BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); - #else - BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); - #endif -} -// -// -// HARDWARE I2C BASE FUNCTIONS -// -// -#if defined BP_USE_I2C_HW - -void hwi2cstart(void) { - -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - // Enable a start condition - I2C3CONbits.SEN = 1; - while (I2C3CONbits.SEN == 1); //wait - return; - } -#endif - // Enable a start condition - I2C1CONbits.SEN = 1; - while (I2C1CONbits.SEN == 1); //wait -} - -void hwi2cstop(void) { - -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - I2C3CONbits.PEN = 1; - while (I2C3CONbits.PEN == 1); //wait - return; - } -#endif - I2C1CONbits.PEN = 1; - while (I2C1CONbits.PEN == 1); //wait -} - -unsigned char hwi2cgetack(void) { - -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - return I2C3STATbits.ACKSTAT; - } -#endif - return I2C1STATbits.ACKSTAT; -} - -void hwi2csendack(unsigned char ack) { -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - I2C3CONbits.ACKDT = ack; //send ACK (0) or NACK(1)? - I2C3CONbits.ACKEN = 1; - while (I2C3CONbits.ACKEN == 1); - return; - } -#endif - I2C1CONbits.ACKDT = ack; //send ACK (0) or NACK(1)? - I2C1CONbits.ACKEN = 1; - while (I2C1CONbits.ACKEN == 1); -} - -void hwi2cwrite(unsigned char c) { -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - I2C3TRN = c; - while (I2C3STATbits.TRSTAT == 1); - return; - } -#endif - I2C1TRN = c; - while (I2C1STATbits.TRSTAT == 1); -} - -unsigned char hwi2cread(void) { - unsigned char c; -#if defined (BUSPIRATEV4) - if (i2cinternal == 0) { - I2C3CONbits.RCEN = 1; - while (I2C3CONbits.RCEN == 1); - c = I2C3RCV; - return c; - } -#endif - I2C1CONbits.RCEN = 1; - while (I2C1CONbits.RCEN == 1); - c = I2C1RCV; - return c; -} - -void hwi2cSetup(void) { - I2C3CONbits.A10M = 0; - I2C3CONbits.SCLREL = 0; - - I2C3ADD = 0; - I2C3MSK = 0; - - // Enable SMBus - I2C3CONbits.SMEN = 0; - - - // Baud rate setting - I2C3BRG = I2Cspeed[modeConfig.speed]; - - // Enable I2C module - I2C3CONbits.I2CEN = 1; - - // - // This work around didn't work for me... - // - /* - //NICE TRY, BUT NO CIGAR - //for revision 3, the SDA has to be hit once manually before - //it will work, we use the connected pullup resistor to jump start - //the broken hardware module. - bpDelayUS(200); - LATBbits.LATB11=0;//hold to ground - TRISBbits.TRISB11=0;//SDA Pullup - bpDelayUS(250); - TRISBbits.TRISB11=1;//SDA Pullup - LATBbits.LATB11=1;//release - */ - -} - -#endif -//*******************/ -// -// -// sofware I2C sniffer (very alpha) -// -// -//*******************/ -/* -void I2C_SnifferSetup(void){ -//we dont actually use interrupts, we poll the interrupt flag - -//1. Ensure that the CN pin is configured as a digital input by setting the associated bit in the -//TRISx register. -//2. Enable interrupts for the selected CN pins by setting the appropriate bits in the CNENx -//registers. -//3. Turn on the weak pull-up devices (if desired) for the selected CN pins by setting the -//appropriate bits in the CNPUx registers. -//4. Clear the CNxIF interrupt flag. - - //-- Ensure pins are in high impedance mode -- - SDA_TRIS=1; - SCL_TRIS=1; - //writes to the PORTs write to the LATCH - SCL=0; //B8 scl - SDA=0; //B9 sda - - //enable change notice on SCL and SDA - CNEN2bits.CN21IE=1;//MOSI - CNEN2bits.CN22IE=1; - - //clear the CNIF interrupt flag - IFS1bits.CNIF=0; - - I2Csniff.datastate=0; - I2Csniff.bits=0; - I2Csniff.I2CD|=SDA; //save current pin state in var - I2Csniff.I2CC|=SCL; //use to see which pin changes on interrupt - -} - */ - - -// \ - escape character -//[ - start -//0xXX - data -//+ - ACK + -//- - NACK - -//] - stop -#define ESCAPE_CHAR '\\' - -/* -void I2C_Sniffer(unsigned char termMode){ - unsigned char c; - - //setup ring buffer pointers - UARTbufSetup(); - I2C_SnifferSetup(); - - while(1){ - - //user IO service - UARTbufService(); - if(UART1RXRdy == 1){//any key pressed, exit - c=UART1RX(); // JTR usb port - bpBR; - break; - } - - //check for change in pin state, if none, return - if(IFS1bits.CNIF==0) continue; - - IFS1bits.CNIF=0; - I2Csniff.I2CD|=SDA; //save current pin state in var - I2Csniff.I2CC|=SCL; //use to see which pin changes on interrupt - - if (I2Csniff.datastate==1 && I2Csniff.I2CC==0b01){//sample when clock goes low to high - - if(I2Csniff.bits<8){ - //the next 8 bits are data - I2Csniff.data <<=1; //move over one bit - I2Csniff.data |= (I2Csniff.I2CD & (~0b10)); //set bit, clear previous bit - I2Csniff.bits++; - }else{ - I2Csniff.ACK=SDA; //check for ACK/NACK - - if(termMode){//output for the terminal - bpWhexBuf(I2Csniff.data); - }else{ //output for binary mode - UARTbuf(ESCAPE_CHAR); //escape character - UARTbuf(I2Csniff.data); //write byte value - } - - if(I2Csniff.ACK) - UARTbuf('-'); - else - UARTbuf('+'); //write ACK status - - I2Csniff.bits=0; - } - - }else if(I2Csniff.I2CC==0b11){//clock high, must be data transition - //if data changed while clock is high, start condition (HL) or stop condition (LH) - - if(I2Csniff.I2CD==0b10){//start condition - I2Csniff.datastate=1;//start condition, allow data byte collection - I2Csniff.bits=0; - UARTbuf('[');//say start, use bus pirate syntax to display data - }else if(I2Csniff.I2CD==0b01){//stop condition - I2Csniff.datastate=0; //stop condition, don't allow byte collection - I2Csniff.bits=0; - UARTbuf(']');//say stop - } - - } - - //move current pin state to previous pin state - I2Csniff.I2CD<<=1; //pSDA=I2Cpin.cSDA; - I2Csniff.I2CC<<=1; //pin.pSCL=I2Cpin.cSCL; - - } -} - */ - -void I2C_Sniffer(unsigned char termMode) { - unsigned char SDANew, SDAOld; - unsigned char SCLNew, SCLOld; - - unsigned char DataState = 0; - unsigned char DataBits = 0; - unsigned char dat = 0; - - //unsigned char *BitBuffer=bpConfig.terminalInput; - //unsigned short BufferPos=0; - //unsigned short AckPos=0; - //unsigned short DataPos=0; - - //setup ring buffer pointers - UARTbufSetup(); - - SDA_TRIS = 1; // -- Ensure pins are in high impedance mode -- - SCL_TRIS = 1; - - SCL = 0; // writes to the PORTs write to the LATCH - SDA = 0; - - BP_MOSI_CN = 1; // enable change notice on SCL and SDA - BP_CLK_CN = 1; - - IFS1bits.CNIF = 0; // clear the change interrupt flag - - SDAOld = SDANew = SDA; - SCLOld = SDANew = SCL; - - //while(!UART1RXRdy&&(BufferPos<32768)) // BitBuffer Space = 4096*8 bits - while (1) { - if (!IFS1bits.CNIF) {//check change notice interrupt - //user IO service - UARTbufService(); - if (UART1RXRdy()) { - dat = UART1RX(); - break; - } - continue; - - } - - IFS1bits.CNIF = 0; //clear interrupt flag - - SDANew = SDA; //store current state right away - SCLNew = SCL; - - if (DataState && !SCLOld && SCLNew) // Sample When SCL Goes Low To High - { - if (DataBits < 8) //we're still collecting data bits - { - dat = dat << 1; - if (SDANew) { - dat |= 1; - } - - DataBits++; - } else { - //put the data byte in the terminal or binary output - if (termMode) {//output for the terminal - bpWhexBuf(dat); - } else { //output for binary mode - UARTbuf(ESCAPE_CHAR); //escape character - UARTbuf(dat); //write byte value - } - - if (SDANew) // SDA High Means NACK - { - UARTbuf('-'); - } else // SDA Low Means ACK - { - UARTbuf('+'); //write ACK status - } - - DataBits = 0; // Ready For Next Data Byte - - } - } else if (SCLOld && SCLNew) // SCL High, Must Be Data Transition - { - if (SDAOld && !SDANew) // Start Condition (High To Low) - { - DataState = 1; // Allow Data Collection - DataBits = 0; - - UARTbuf('['); //say start, use bus pirate syntax to display data - - } else if (!SDAOld && SDANew) // Stop Condition (Low To High) - { - DataState = 0; // Don't Allow Data Collection - DataBits = 0; - - UARTbuf(']'); //say stop - - } - } - - SDAOld = SDANew; // Save Last States - SCLOld = SCLNew; - } - - BP_MOSI_CN = 0; // clear change notice - BP_CLK_CN = 0; - - if (termMode) { - bpBR; - } -} - -/* -rawI2C mode: -# 00000000//reset to BBIO -# 00000001 � mode version string (I2C1) -# 00000010 � I2C start bit -# 00000011 � I2C stop bit -# 00000100 - I2C read byte -# 00000110 - ACK bit -# 00000111 - NACK bit -# 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) -# (0110)000x - Set I2C speed, 3 = 400khz 2=100khz 1=50khz 0=5khz -# (0111)000x - Read speed, (planned) -# (0100)wxyz � Configure peripherals w=power, x=pullups, y=AUX, z=CS (was 0110) -# (0101)wxyz � read peripherals (planned, not implemented) - */ -void binI2CversionString(void) { - bpWstring("I2C1"); -} - -void binI2C(void) { - static unsigned char inByte, rawCommand, i; - unsigned int j, fw, fr; - //I2C setup - SDA_TRIS = 1; - SCL_TRIS = 1; - SCL = 0; //B8 scl - SDA = 0; //B9 sda - - - //set CS pin direction to output on setup - BP_CS_DIR = 0; //B6 cs output - - modeConfig.HiZ = 1; //yes, always hiz (bbio uses this setting, should be changed to a setup variable because stringing the modeconfig struct everyhwere is getting ugly!) - modeConfig.lsbEN = 0; //just in case! - bbSetup(2, 0xff); //configure the bitbang library for 2-wire, set the speed to default/high - binI2CversionString(); //reply string - - while (1) { - - //JTR Not requiredwhile (UART1RXRdy == 0); //wait for a byte - inByte = UART1RX(); - rawCommand = (inByte >> 4); //get command bits in seperate variable - - switch (rawCommand) { - case 0://reset/setup/config commands - switch (inByte) { - case 0://0, reset exit - //cleanup!!!!!!!!!! - return; //exit - break; - case 1://1 - id reply string - binI2CversionString(); //reply string - break; - case 2://I2C start bit - bbI2Cstart(); - UART1TX(1); - break; - case 3://I2C stop bit - bbI2Cstop(); - UART1TX(1); - break; - case 4://I2C read byte - UART1TX(bbReadByte()); - break; - case 6://I2C send ACK - bbI2Cack(); - UART1TX(1); - break; - case 7://I2C send NACK - bbI2Cnack(); - UART1TX(1); - break; - case 8: //write-then-read - //get the number of commands that will follow - //JTR Not required while (!UART1RXRdy()); //wait for a byte - fw = UART1RX(); - /* JTR usb port; */; //get byte - fw = fw << 8; - //JTR Not required while (!UART1RXRdy()); //wait for a byte - fw |= UART1RX(); - /* JTR usb port; */; //get byte - - //get the number of reads to do - //JTR Not required while (!UART1RXRdy()); //wait for a byte - fr = UART1RX(); - /* JTR usb port; */; //get byte - fr = fr << 8; - //JTR Not required while (!UART1RXRdy()); //wait for a byte - fr |= UART1RX(); - /* JTR usb port; */; //get byte - - - //check length and report error - if (fw > TERMINAL_BUFFER || fr > TERMINAL_BUFFER) { -I2C_write_read_error: //use this for the read error too - UART1TX(0); - break; - } - - //get bytes - for (j = 0; j < fw; j++) { - //JTR Not required while (!UART1RXRdy()); //wait for a byte - bpConfig.terminalInput[j] = UART1RX(); - /* JTR usb port; */; - } - - //start - bbI2Cstart(); - - for (j = 0; j < fw; j++) { - //get ACK - //if no ack, goto error - bbWriteByte(bpConfig.terminalInput[j]); //send byte - if (bbReadBit() == 1) goto I2C_write_read_error; - } - - fw = fr - 1; //reuse fw - for (j = 0; j < fr; j++) { //read bulk bytes from SPI - //send ack - //i flast byte, send NACK - bpConfig.terminalInput[j] = bbReadByte(); - - if (j < fw) { - bbI2Cack(); - } else { - bbI2Cnack(); - } - } - //I2C stop - bbI2Cstop(); - - UART1TX(1); //send 1/OK - - for (j = 0; j < fr; j++) { //send the read buffer contents over serial - UART1TX(bpConfig.terminalInput[j]); - } - - break;//00001001 xxxxxxxx - case 9: //extended AUX command - UART1TX(1); //confirm that the command is known - //inByte - used as extended commmand - //fr - used as result - while(U1STAbits.URXDA == 0);//wait for subcommand byte - inByte=U1RXREG; //get byte - //0x00 - AUX/CS low - //0x01 - AUX/CS high - //0x02 - AUX/CS HiZ - //0x03 - AUX read - //0x10 - use AUX - //0x20 - use CS - fr=1; - switch( inByte ) { - case 0x00: - bpAuxLow(); - break; - case 0x01: - bpAuxHigh(); - break; - case 0x02: - bpAuxHiZ(); - break; - case 0x03: - fr = bpAuxRead(); - break; - case 0x10: - modeConfig.altAUX = 0; - break; - case 0x20: - modeConfig.altAUX = 1; - break; - default: - fw = 0; - break; - } - UART1TX(fr);//result - break; - case 0b1111: - I2C_Sniffer(0); //set for raw output - UART1TX(1); - break; - default: - UART1TX(0); - break; - } - break; - - case 0b0001://get x+1 bytes - inByte &= (~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1); //send 1/OK - - for (i = 0; i < inByte; i++) { - //JTR Not required while (UART1RXRdy() == 0); //wait for a byte - bbWriteByte(UART1RX()); // JTR usb port //send byte - UART1TX(bbReadBit()); //return ACK0 or NACK1 - } - - break; - - case 0b0110://set speed - inByte &= (~0b11111100); //clear command portion - bbSetup(2, inByte); //set I2C speed - UART1TX(1); - break; - - case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS - binIOperipheralset(inByte); - UART1TX(1); //send 1/OK - break; -#ifdef BUSPIRATEV4 - case 0b0101: - UART1TX(binBBpullVoltage(inByte)); - break; -#endif - default: - UART1TX(0x00); //send 0/Error - break; - }//command switch - }//while loop - -} - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +#include "bitbang.h" +#include "busPirateCore.h"//need access to bpConfig +#include "binIOhelpers.h" +#include "AUXpin.h" + +#include "procMenu.h" // for the userinteraction subs + +//software or hardware I2C mode defines +#define SOFT 0 +#define HARD 1 +#if defined (BUSPIRATEV4) +#define BP_USE_I2C_HW +unsigned char i2cinternal = 0; +#endif + +#define SCL BP_CLK +#define SCL_TRIS BP_CLK_DIR //-- The SCL Direction Register Bit +#define SDA BP_MOSI //-- The SDA output pin +#define SDA_TRIS BP_MOSI_DIR //-- The SDA Direction Register Bit + +extern struct _bpConfig bpConfig; //holds persistant bus pirate settings (see base.h) need hardware version info +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; + +/* +static struct _i2csniff { + unsigned char bits; + unsigned char data; + unsigned char ACK:1; + unsigned char datastate:1; //are we collecting data yet? + unsigned char I2CD:2; + unsigned char I2CC:2; //prevous and current clock pin state +} I2Csniff; + */ +//hardware functions +void hwi2cSetup(void); +void hwi2cstart(void); +void hwi2cstop(void); +void hwi2csendack(unsigned char ack); +unsigned char hwi2cgetack(void); +void hwi2cwrite(unsigned char c); +unsigned char hwi2cread(void); +void binI2CversionString(void); + +#ifdef BP_USE_I2C_HW +static unsigned char I2Cspeed[] = {157, 37, 13}; //100,400,1000khz; datasheet pg 145 +#endif + +//software functions +void I2C_Setup(void); +void I2Csetup_exc(void); +void I2C_SnifferSetup(void); +void I2C_Sniffer(unsigned char termMode); + +int i2cmode; +int ackPending; + +/* +extern int getnumber(int def, int max); //linker happy? everybody happy :D +// move into a .h or other .c??? +int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* +int getint(void); +int getrepeat(void); +void consumewhitechars(void); +extern int cmderror; + */ + +unsigned int I2Cread(void) { + unsigned char c = 0; + if (ackPending) { + bpSP; + //bpWmessage(MSG_ACK); + BPMSG1060; + bpSP; + if (i2cmode == SOFT) { + bbI2Cack(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2csendack(0); //all other reads get an ACK + } +#endif + ackPending = 0; + } + + if (i2cmode == SOFT) { + c = bbReadByte(); + } +#ifdef BP_USE_I2C_HW + else { + c = hwi2cread(); + } +#endif + ackPending = 1; + return c; +} + +unsigned int I2Cwrite(unsigned int c) { //unsigned char c; + if (ackPending) { + bpSP; + //bpWmessage(MSG_ACK); + BPMSG1060; + bpSP; + if (i2cmode == SOFT) { + bbI2Cack(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2csendack(0); //all other reads get an ACK + } +#endif + ackPending = 0; + } + + if (i2cmode == SOFT) { + bbWriteByte(c); + c = bbReadBit(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2cwrite(c); + c = hwi2cgetack(); + } +#endif + bpSP; + if (c == 0) { //bpWmessage(MSG_ACK); + BPMSG1060; + return 0x300; // bit 9=ack + } else { //bpWmessage(MSG_NACK); + BPMSG1061; + return 0x100; // bit 9=ack + } +} + +void I2Cstart(void) { + if (ackPending) { //bpWmessage(MSG_NACK); + BPMSG1061; + bpBR; //bpWline(OUMSG_I2C_READ_PEND_NACK); + if (i2cmode == SOFT) { + bbI2Cnack(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2csendack(1); //the last read before a stop/start condition gets an NACK + } +#endif + ackPending = 0; + } + + if (i2cmode == SOFT) { + if (bbI2Cstart()) {//bus contention + BPMSG1019; //warning + BPMSG1020; //short or no pullups + bpBR; + } + } +#ifdef BP_USE_I2C_HW + else { + hwi2cstart(); + } +#endif + //bpWmessage(MSG_I2C_START); + BPMSG1062; +} + +void I2Cstop(void) { + if (ackPending) { //bpWmessage(MSG_NACK); + BPMSG1061; + bpBR; //bpWline(OUMSG_I2C_READ_PEND_NACK); + if (i2cmode == SOFT) { + bbI2Cnack(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2csendack(1); //the last read before a stop/start condition gets an NACK + } +#endif + ackPending = 0; + } + + if (i2cmode == SOFT) { + bbI2Cstop(); + } +#ifdef BP_USE_I2C_HW + else { + hwi2cstop(); + } +#endif + //bpWmessage(MSG_I2C_STOP); + BPMSG1063; +} + +void I2Csettings(void) { //bpWstring("I2C (mod spd)=( "); + BPMSG1068; +#ifdef BP_USE_I2C_HW + bpWdec(i2cmode); + bpSP; +#else + bpWdec(0); + bpSP; // softmode +#endif + bpWdec(modeConfig.speed); + bpSP; + bpWline(")"); +} + +void I2Csetup(void) { + int HW, speed; + + HW = 0; // keep compiler happy if BP_USE_HW is not defined + +#ifdef BP_USE_I2C_HW + consumewhitechars(); + HW = getint(); +#else + i2cmode = SOFT; +#endif + + consumewhitechars(); + speed = getint(); + +#ifdef BP_USE_I2C_HW + if ((HW > 0) && (HW <= 2)) { + i2cmode = HW - 1; + } else { + speed = 0; + } +#endif + + if ((speed > 0) && (speed <= 4)) { + modeConfig.speed = speed - 1; + } else { + speed = 0; + } + + if (speed == 0) { + cmderror = 0; + +#ifdef BP_USE_I2C_HW + //bpWline(OUMSG_I2C_CON); + BPMSG1064; + i2cmode = (getnumber(1, 1, 2, 0) - 1); +#else + i2cmode = SOFT; +#endif + + if (i2cmode == SOFT) { + //bpWmessage(MSG_OPT_BB_SPEED); + BPMSG1065; + modeConfig.speed = (getnumber(1, 1, 4, 0) - 1); + } else { +#if defined (BUSPIRATEV1A)||defined (BUSPIRATEV2) + // There is a hardware incompatibility with > 1)); + if ((i & 0b1) == 0) {//if the first bit is set it's a read address, send a byte plus nack to clean up + bpWstring(" W"); + } else { + if (i2cmode == SOFT) { + bbReadByte(); + bbI2Cnack(); //bbWriteBit(1);//high bit is NACK + } +#ifdef BP_USE_I2C_HW + else { + hwi2cread(); + hwi2csendack(1); //high bit is NACK + } +#endif + bpWstring(" R"); + } + bpWstring(")"); + bpSP; + } + if (i2cmode == SOFT) bbI2Cstop(); +#ifdef BP_USE_I2C_HW + else hwi2cstop(); +#endif + } + bpWBR; + + break; + case 2: + if (i2cmode == HARD)I2C1CONbits.I2CEN = 0; //disable I2C module + + //bpWline(OUMSG_I2C_MACRO_SNIFFER); + BPMSG1071; + BPMSG1250; + I2C_Sniffer(1); //set for terminal output + +#ifdef BP_USE_I2C_HW + if (i2cmode == HARD) hwi2cSetup(); //setup hardware I2C again +#endif + break; +#if defined (BUSPIRATEV4) + case 3: //in hardware mode (or software, I guess) we can edit the on-board EEPROM -software mode unimplemented... + bpWline("Now using on-board EEPROM I2C interface"); + i2cinternal = 1; + I2C1CONbits.A10M = 0; + I2C1CONbits.SCLREL = 0; + + I2C1ADD = 0; + I2C1MSK = 0; + + // Enable SMBus + I2C1CONbits.SMEN = 0; + + // Baud rate setting + I2C1BRG = I2Cspeed[modeConfig.speed]; + + // Enable I2C module + I2C1CONbits.I2CEN = 1; + + // disable other I2C module + I2C3CONbits.I2CEN = 0; + break; + case 4: + if (i2cinternal == 1) { + bpWline("On-board EEPROM write protect disabled"); + BP_EE_WP = 0; + } + break; +#endif + default: + //bpWmessage(MSG_ERROR_MACRO); + BPMSG1016; + } +} + +void I2Cpins(void) { + #if defined(BUSPIRATEV4) + BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); + #else + BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); + #endif +} +// +// +// HARDWARE I2C BASE FUNCTIONS +// +// +#if defined BP_USE_I2C_HW + +void hwi2cstart(void) { + +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + // Enable a start condition + I2C3CONbits.SEN = 1; + while (I2C3CONbits.SEN == 1); //wait + return; + } +#endif + // Enable a start condition + I2C1CONbits.SEN = 1; + while (I2C1CONbits.SEN == 1); //wait +} + +void hwi2cstop(void) { + +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + I2C3CONbits.PEN = 1; + while (I2C3CONbits.PEN == 1); //wait + return; + } +#endif + I2C1CONbits.PEN = 1; + while (I2C1CONbits.PEN == 1); //wait +} + +unsigned char hwi2cgetack(void) { + +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + return I2C3STATbits.ACKSTAT; + } +#endif + return I2C1STATbits.ACKSTAT; +} + +void hwi2csendack(unsigned char ack) { +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + I2C3CONbits.ACKDT = ack; //send ACK (0) or NACK(1)? + I2C3CONbits.ACKEN = 1; + while (I2C3CONbits.ACKEN == 1); + return; + } +#endif + I2C1CONbits.ACKDT = ack; //send ACK (0) or NACK(1)? + I2C1CONbits.ACKEN = 1; + while (I2C1CONbits.ACKEN == 1); +} + +void hwi2cwrite(unsigned char c) { +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + I2C3TRN = c; + while (I2C3STATbits.TRSTAT == 1); + return; + } +#endif + I2C1TRN = c; + while (I2C1STATbits.TRSTAT == 1); +} + +unsigned char hwi2cread(void) { + unsigned char c; +#if defined (BUSPIRATEV4) + if (i2cinternal == 0) { + I2C3CONbits.RCEN = 1; + while (I2C3CONbits.RCEN == 1); + c = I2C3RCV; + return c; + } +#endif + I2C1CONbits.RCEN = 1; + while (I2C1CONbits.RCEN == 1); + c = I2C1RCV; + return c; +} + +void hwi2cSetup(void) { + I2C3CONbits.A10M = 0; + I2C3CONbits.SCLREL = 0; + + I2C3ADD = 0; + I2C3MSK = 0; + + // Enable SMBus + I2C3CONbits.SMEN = 0; + + + // Baud rate setting + I2C3BRG = I2Cspeed[modeConfig.speed]; + + // Enable I2C module + I2C3CONbits.I2CEN = 1; + + // + // This work around didn't work for me... + // + /* + //NICE TRY, BUT NO CIGAR + //for revision 3, the SDA has to be hit once manually before + //it will work, we use the connected pullup resistor to jump start + //the broken hardware module. + bpDelayUS(200); + LATBbits.LATB11=0;//hold to ground + TRISBbits.TRISB11=0;//SDA Pullup + bpDelayUS(250); + TRISBbits.TRISB11=1;//SDA Pullup + LATBbits.LATB11=1;//release + */ + +} + +#endif +//*******************/ +// +// +// sofware I2C sniffer (very alpha) +// +// +//*******************/ +/* +void I2C_SnifferSetup(void){ +//we dont actually use interrupts, we poll the interrupt flag + +//1. Ensure that the CN pin is configured as a digital input by setting the associated bit in the +//TRISx register. +//2. Enable interrupts for the selected CN pins by setting the appropriate bits in the CNENx +//registers. +//3. Turn on the weak pull-up devices (if desired) for the selected CN pins by setting the +//appropriate bits in the CNPUx registers. +//4. Clear the CNxIF interrupt flag. + + //-- Ensure pins are in high impedance mode -- + SDA_TRIS=1; + SCL_TRIS=1; + //writes to the PORTs write to the LATCH + SCL=0; //B8 scl + SDA=0; //B9 sda + + //enable change notice on SCL and SDA + CNEN2bits.CN21IE=1;//MOSI + CNEN2bits.CN22IE=1; + + //clear the CNIF interrupt flag + IFS1bits.CNIF=0; + + I2Csniff.datastate=0; + I2Csniff.bits=0; + I2Csniff.I2CD|=SDA; //save current pin state in var + I2Csniff.I2CC|=SCL; //use to see which pin changes on interrupt + +} + */ + + +// \ - escape character +//[ - start +//0xXX - data +//+ - ACK + +//- - NACK - +//] - stop +#define ESCAPE_CHAR '\\' + +/* +void I2C_Sniffer(unsigned char termMode){ + unsigned char c; + + //setup ring buffer pointers + UARTbufSetup(); + I2C_SnifferSetup(); + + while(1){ + + //user IO service + UARTbufService(); + if(UART1RXRdy == 1){//any key pressed, exit + c=UART1RX(); // JTR usb port + bpBR; + break; + } + + //check for change in pin state, if none, return + if(IFS1bits.CNIF==0) continue; + + IFS1bits.CNIF=0; + I2Csniff.I2CD|=SDA; //save current pin state in var + I2Csniff.I2CC|=SCL; //use to see which pin changes on interrupt + + if (I2Csniff.datastate==1 && I2Csniff.I2CC==0b01){//sample when clock goes low to high + + if(I2Csniff.bits<8){ + //the next 8 bits are data + I2Csniff.data <<=1; //move over one bit + I2Csniff.data |= (I2Csniff.I2CD & (~0b10)); //set bit, clear previous bit + I2Csniff.bits++; + }else{ + I2Csniff.ACK=SDA; //check for ACK/NACK + + if(termMode){//output for the terminal + bpWhexBuf(I2Csniff.data); + }else{ //output for binary mode + UARTbuf(ESCAPE_CHAR); //escape character + UARTbuf(I2Csniff.data); //write byte value + } + + if(I2Csniff.ACK) + UARTbuf('-'); + else + UARTbuf('+'); //write ACK status + + I2Csniff.bits=0; + } + + }else if(I2Csniff.I2CC==0b11){//clock high, must be data transition + //if data changed while clock is high, start condition (HL) or stop condition (LH) + + if(I2Csniff.I2CD==0b10){//start condition + I2Csniff.datastate=1;//start condition, allow data byte collection + I2Csniff.bits=0; + UARTbuf('[');//say start, use bus pirate syntax to display data + }else if(I2Csniff.I2CD==0b01){//stop condition + I2Csniff.datastate=0; //stop condition, don't allow byte collection + I2Csniff.bits=0; + UARTbuf(']');//say stop + } + + } + + //move current pin state to previous pin state + I2Csniff.I2CD<<=1; //pSDA=I2Cpin.cSDA; + I2Csniff.I2CC<<=1; //pin.pSCL=I2Cpin.cSCL; + + } +} + */ + +void I2C_Sniffer(unsigned char termMode) { + unsigned char SDANew, SDAOld; + unsigned char SCLNew, SCLOld; + + unsigned char DataState = 0; + unsigned char DataBits = 0; + unsigned char dat = 0; + + //unsigned char *BitBuffer=bpConfig.terminalInput; + //unsigned short BufferPos=0; + //unsigned short AckPos=0; + //unsigned short DataPos=0; + + //setup ring buffer pointers + UARTbufSetup(); + + SDA_TRIS = 1; // -- Ensure pins are in high impedance mode -- + SCL_TRIS = 1; + + SCL = 0; // writes to the PORTs write to the LATCH + SDA = 0; + + BP_MOSI_CN = 1; // enable change notice on SCL and SDA + BP_CLK_CN = 1; + + IFS1bits.CNIF = 0; // clear the change interrupt flag + + SDAOld = SDANew = SDA; + SCLOld = SDANew = SCL; + + //while(!UART1RXRdy&&(BufferPos<32768)) // BitBuffer Space = 4096*8 bits + while (1) { + if (!IFS1bits.CNIF) {//check change notice interrupt + //user IO service + UARTbufService(); + if (UART1RXRdy()) { + dat = UART1RX(); + break; + } + continue; + + } + + IFS1bits.CNIF = 0; //clear interrupt flag + + SDANew = SDA; //store current state right away + SCLNew = SCL; + + if (DataState && !SCLOld && SCLNew) // Sample When SCL Goes Low To High + { + if (DataBits < 8) //we're still collecting data bits + { + dat = dat << 1; + if (SDANew) { + dat |= 1; + } + + DataBits++; + } else { + //put the data byte in the terminal or binary output + if (termMode) {//output for the terminal + bpWhexBuf(dat); + } else { //output for binary mode + UARTbuf(ESCAPE_CHAR); //escape character + UARTbuf(dat); //write byte value + } + + if (SDANew) // SDA High Means NACK + { + UARTbuf('-'); + } else // SDA Low Means ACK + { + UARTbuf('+'); //write ACK status + } + + DataBits = 0; // Ready For Next Data Byte + + } + } else if (SCLOld && SCLNew) // SCL High, Must Be Data Transition + { + if (SDAOld && !SDANew) // Start Condition (High To Low) + { + DataState = 1; // Allow Data Collection + DataBits = 0; + + UARTbuf('['); //say start, use bus pirate syntax to display data + + } else if (!SDAOld && SDANew) // Stop Condition (Low To High) + { + DataState = 0; // Don't Allow Data Collection + DataBits = 0; + + UARTbuf(']'); //say stop + + } + } + + SDAOld = SDANew; // Save Last States + SCLOld = SCLNew; + } + + BP_MOSI_CN = 0; // clear change notice + BP_CLK_CN = 0; + + if (termMode) { + bpBR; + } +} + +/* +rawI2C mode: +# 00000000//reset to BBIO +# 00000001 � mode version string (I2C1) +# 00000010 � I2C start bit +# 00000011 � I2C stop bit +# 00000100 - I2C read byte +# 00000110 - ACK bit +# 00000111 - NACK bit +# 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) +# (0110)000x - Set I2C speed, 3 = 400khz 2=100khz 1=50khz 0=5khz +# (0111)000x - Read speed, (planned) +# (0100)wxyz � Configure peripherals w=power, x=pullups, y=AUX, z=CS (was 0110) +# (0101)wxyz � read peripherals (planned, not implemented) + */ +void binI2CversionString(void) { + bpWstring("I2C1"); +} + +void binI2C(void) { + static unsigned char inByte, rawCommand, i; + unsigned int j, fw, fr; + //I2C setup + SDA_TRIS = 1; + SCL_TRIS = 1; + SCL = 0; //B8 scl + SDA = 0; //B9 sda + + + //set CS pin direction to output on setup + BP_CS_DIR = 0; //B6 cs output + + modeConfig.HiZ = 1; //yes, always hiz (bbio uses this setting, should be changed to a setup variable because stringing the modeconfig struct everyhwere is getting ugly!) + modeConfig.lsbEN = 0; //just in case! + bbSetup(2, 0xff); //configure the bitbang library for 2-wire, set the speed to default/high + binI2CversionString(); //reply string + + while (1) { + + //JTR Not requiredwhile (UART1RXRdy == 0); //wait for a byte + inByte = UART1RX(); + rawCommand = (inByte >> 4); //get command bits in seperate variable + + switch (rawCommand) { + case 0://reset/setup/config commands + switch (inByte) { + case 0://0, reset exit + //cleanup!!!!!!!!!! + return; //exit + break; + case 1://1 - id reply string + binI2CversionString(); //reply string + break; + case 2://I2C start bit + bbI2Cstart(); + UART1TX(1); + break; + case 3://I2C stop bit + bbI2Cstop(); + UART1TX(1); + break; + case 4://I2C read byte + UART1TX(bbReadByte()); + break; + case 6://I2C send ACK + bbI2Cack(); + UART1TX(1); + break; + case 7://I2C send NACK + bbI2Cnack(); + UART1TX(1); + break; + case 8: //write-then-read + //get the number of commands that will follow + //JTR Not required while (!UART1RXRdy()); //wait for a byte + fw = UART1RX(); + /* JTR usb port; */; //get byte + fw = fw << 8; + //JTR Not required while (!UART1RXRdy()); //wait for a byte + fw |= UART1RX(); + /* JTR usb port; */; //get byte + + //get the number of reads to do + //JTR Not required while (!UART1RXRdy()); //wait for a byte + fr = UART1RX(); + /* JTR usb port; */; //get byte + fr = fr << 8; + //JTR Not required while (!UART1RXRdy()); //wait for a byte + fr |= UART1RX(); + /* JTR usb port; */; //get byte + + + //check length and report error + if (fw > TERMINAL_BUFFER || fr > TERMINAL_BUFFER) { +I2C_write_read_error: //use this for the read error too + UART1TX(0); + break; + } + + //get bytes + for (j = 0; j < fw; j++) { + //JTR Not required while (!UART1RXRdy()); //wait for a byte + bpConfig.terminalInput[j] = UART1RX(); + /* JTR usb port; */; + } + + //start + bbI2Cstart(); + + for (j = 0; j < fw; j++) { + //get ACK + //if no ack, goto error + bbWriteByte(bpConfig.terminalInput[j]); //send byte + if (bbReadBit() == 1) goto I2C_write_read_error; + } + + fw = fr - 1; //reuse fw + for (j = 0; j < fr; j++) { //read bulk bytes from SPI + //send ack + //i flast byte, send NACK + bpConfig.terminalInput[j] = bbReadByte(); + + if (j < fw) { + bbI2Cack(); + } else { + bbI2Cnack(); + } + } + //I2C stop + bbI2Cstop(); + + UART1TX(1); //send 1/OK + + for (j = 0; j < fr; j++) { //send the read buffer contents over serial + UART1TX(bpConfig.terminalInput[j]); + } + + break;//00001001 xxxxxxxx + case 9: //extended AUX command + UART1TX(1); //confirm that the command is known + //inByte - used as extended commmand + //fr - used as result + while(U1STAbits.URXDA == 0);//wait for subcommand byte + inByte=U1RXREG; //get byte + //0x00 - AUX/CS low + //0x01 - AUX/CS high + //0x02 - AUX/CS HiZ + //0x03 - AUX read + //0x10 - use AUX + //0x20 - use CS + fr=1; + switch( inByte ) { + case 0x00: + bpAuxLow(); + break; + case 0x01: + bpAuxHigh(); + break; + case 0x02: + bpAuxHiZ(); + break; + case 0x03: + fr = bpAuxRead(); + break; + case 0x10: + modeConfig.altAUX = 0; + break; + case 0x20: + modeConfig.altAUX = 1; + break; + default: + fw = 0; + break; + } + UART1TX(fr);//result + break; + case 0b1111: + I2C_Sniffer(0); //set for raw output + UART1TX(1); + break; + default: + UART1TX(0); + break; + } + break; + + case 0b0001://get x+1 bytes + inByte &= (~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1); //send 1/OK + + for (i = 0; i < inByte; i++) { + //JTR Not required while (UART1RXRdy() == 0); //wait for a byte + bbWriteByte(UART1RX()); // JTR usb port //send byte + UART1TX(bbReadBit()); //return ACK0 or NACK1 + } + + break; + + case 0b0110://set speed + inByte &= (~0b11111100); //clear command portion + bbSetup(2, inByte); //set I2C speed + UART1TX(1); + break; + + case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS + binIOperipheralset(inByte); + UART1TX(1); //send 1/OK + break; +#ifdef BUSPIRATEV4 + case 0b0101: + UART1TX(binBBpullVoltage(inByte)); + break; +#endif + default: + UART1TX(0x00); //send 0/Error + break; + }//command switch + }//while loop + +} + + diff --git a/Firmware/I2C.h b/Firmware/I2C.h index 9fa19e92..2a137de4 100644 --- a/Firmware/I2C.h +++ b/Firmware/I2C.h @@ -1,31 +1,31 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -void i2cProcess(void); -void binI2C(void); - - -unsigned int I2Cread(void); -unsigned int I2Cwrite(unsigned int c); -void I2Cstart(void); -void I2Cstop(void); -void I2Csetup(void); -void I2Csetup_exc(void); -void I2Ccleanup(void); -void I2Cmacro(unsigned int c); -void I2Cpins(void); -void I2Csettings(void); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +void i2cProcess(void); +void binI2C(void); + + +unsigned int I2Cread(void); +unsigned int I2Cwrite(unsigned int c); +void I2Cstart(void); +void I2Cstop(void); +void I2Csetup(void); +void I2Csetup_exc(void); +void I2Ccleanup(void); +void I2Cmacro(unsigned int c); +void I2Cpins(void); +void I2Csettings(void); + diff --git a/Firmware/JTR_SUMP.c b/Firmware/JTR_SUMP.c index 6a093f2d..a87e70fc 100644 --- a/Firmware/JTR_SUMP.c +++ b/Firmware/JTR_SUMP.c @@ -1,310 +1,310 @@ -/* - * - * USB infrared remote control receiver transmitter firmware v1.0 - * License: creative commons - attribution, share-alike - * Copyright Ian Lesnet 2010 - * http://dangerousprototypes.com - * - */ -/* ***************************************************************************** - BPv4 JTR-SUMP A folked version by JTR. Please see changelog.txt for details. - - * JTR SUMP Version 0.1 beta - ********************************************************************************/ - -#include "base.h" -#include "SUMP.h" - -//commandset -//http://www.sump.org/projects/analyzer/protocol/ -#define SUMP_RESET 0x00 -#define SUMP_RUN 0x01 -#define SUMP_ID 0x02 -#define SUMP_DESC 0x04 -#define SUMP_XON 0x11 -#define SUMP_XOFF 0x13 -#define SUMP_DIV 0x80 -#define SUMP_CNT 0x81 -#define SUMP_FLAGS 0x82 -#define SUMP_TRIG 0xc0 -#define SUMP_TRIG_VALS 0xc1 -#define LA_SAMPLE_SIZE 4096 - -extern BYTE *InPtr, *OutPtr, cdc_In_len, cdc_timeout_count; -extern BDentry *CDC_Outbdp, *CDC_Inbdp; -//extern CDC_FLUSH_CONTROL cdc_Flush_Control; -static unsigned int sumpSamples; - -// REMAPPING OF THE CHANGE NOTIFICATIONS BPv3 Vs BPv4 -//BPv3 -// PORTB0, PORTB1, PORTB2, PORTB3, PORTB4, PORTB5, PORTB6, PORTB7, PORTB8, PORTB9, PORTB10 -//PIN 4 5 6 7 11 14 15 16 17 18 21 -//CN_No 4 5 6 7 1 27 24 23 22 21 16 -//CNEN 2:8 2:7 2:6 2:5 2:0 -//Func CS MISO CLK MOSI AUX1 - - -//BPv4 -// PORTD0, PORTD1, PORTD2, PORTD3, PORTD4, PORTD5, PORTD6, PORTD7, PORTD8 -//PIN 46 49 50 51 52 53 54 55 42 -//CN_N0 49 50 51 52 13 14 15 16 53 -//CNEN 4:1 4:2 4:3 4:4 1:13 1:14 1:15 2:0 4:5 -//Func AUX2 MOSI CLK MISO CS AUX0 AUX1 - -void SUMP(void) { - BYTE RECVedByte, sump_mode; - static unsigned int i; - static unsigned long l; - unsigned int sump_port; - - struct _sumpRX { - BYTE command[5]; - BYTE parameters; - BYTE parCnt; - } sumpRX; - -#ifndef BUSPIRATEV4 - IODIR |= (AUX + MOSI + CLK + MISO + CS); //AUX, MOSI, CLK, MISO, CS pins to input -#else - IODIR |= (AUX0 + AUX1 + AUX2 + MOSI + CLK + MISO + CS + SUMP_SPARE6 + SUMP_SPARE7); // BPv4 PORTD SUMP inputs x9 -#endif - - SUMPreset(); - sump_mode = 1; // Remains in SUMP mode until sump_mode == 0 - bpWstring("1ALS"); - - do { // Remains in this loop until a SUMP RESET (0x00) is received. -#ifndef USB_INTERRUPTS - usb_handler(); -#endif - if (poll_getc_cdc(&RECVedByte) == 1) {//process any command bytes, otherwise it does nothing but loop - - switch (RECVedByte) {//switch on the current byte - case SUMP_RESET://reset - SUMPreset(); - sump_mode = 0; // Will exit at a single exit point below at the end of te do ... while loop - break; - case SUMP_ID://SLA0 or 1 backwards: 1ALS - bpWstring("1ALS"); - break; - - case SUMP_RUN://arm the triger, when completed exits SUMP mode. - - BP_LEDMODE = 1; //ARMED, turn on LED - - //setup timer - T4CON = 0; //make sure the counters are off - //timer 4 internal, measures interval - TMR5HLD = 0x00; - TMR4 = 0x00; - T4CON = 0b1000; //.T32=1, bit 3 - - //setup of change notice interrupt done in meta handler below - - IFS1bits.CNIF = 0; //clear interrupt flag - IEC1bits.CNIE = 0; //interrupts DISABLED - IPC4bits.CNIP = 1; //priority to 0, not actual interupt - - WaitInReady(); - cdc_In_len = 0; - CDC_Flush_In_Now(); // Start with a clean slate. Reset InPtr and count etc. - CDC_Flush_In_Now(); - -#ifndef BUSPIRATEV4 - do { - - } while ((0 == IFS1bits.CNIF) && CNEN2); -#else - do { -#ifndef USB_INTERRUPTS - usb_handler(); -#endif - } while (0 == IFS1bits.CNIF); - -#endif - //OK! Ready to sample - // Add LED control here. - // BP_VREGEN = 0; // JTR this is for debug ATM - - T4CONbits.TON = 1; //start timer4 - IFS1bits.T5IF = 0; //clear interrupt flag//setup timer and wait - - // Critical timed loop, inversely proportional to our acheivable bandwidth - for (i = 0; i < sumpSamples; i++) { //take SAMPLE_SIZE samples -#ifndef BUSPIRATEV4 - *InPtr++ = (PORTB >> 6); //change to pointer for faster use... -#else -#ifdef BPv4_SUMP_SOFT_WIRE // See hardwarev4a.h - sump_port = (PORTD & 0x13f); - if (sump_port & 0x100) - sump_port |= 0x80; - - // Debugging. Chose one - * InPtr++ = sump_port; - // * InPtr++ = i; -#else - * InPtr++ = (BYTE)(PORTD & 0xff); //sump_port; //change to pointer for faster use... -#endif - -#endif - cdc_In_len += 1; - cdc_timeout_count = 0; // Must do this to prevent CDC_FlushOnTimeOut firing. - - if (cdc_In_len == CDC_BUFFER_SIZE) { - putda_cdc(CDC_BUFFER_SIZE); - cdc_In_len = 0; - usb_handler(); - } - while (IFS1bits.T5IF == 0); //wait for timer4 (timer 5 interrupt) - IFS1bits.T5IF = 0; //clear interrupt flag - } -#ifdef BUSPIRATEV4 - CNEN1 &= 0b1001111111111111; //turn off used pins change notice - CNEN4 &= 0b1111111111000001; -#else - CNEN2 = 0; //change notice off -#endif - T4CON = 0; //stop count -#ifndef USB_INTERRUPTS - usb_handler(); -#endif - WaitInReady(); - - // JTR debug only - // putc_cdc('q'); - // putc_cdc('w'); - // putc_cdc('e'); - // BP_VREGEN = 1; - // End JTR debug. - SendZLP(); - SUMPreset(); - sump_mode = 0; - break; - - case SUMP_DESC: - // device name string - UART1TX(0x01); -#ifdef BUSPIRATEV4 - bpWstring("BPv4"); -#else - bpWstring("BPv3"); -#endif - UART1TX(0x00); - //sample memory (4096) meaningless in JTR_SUMP mode - UART1TX(0x21); - UART1TX(0x00); - UART1TX(0x00); - UART1TX(0x10); - UART1TX(0x00); - //sample rate (1MHz) - UART1TX(0x23); - UART1TX(0x00); - UART1TX(0x0F); - UART1TX(0x42); - UART1TX(0x40); - //number of probes (5 or 8 on BPv4) - UART1TX(0x40); - UART1TX(NUM_OF_SUMP_CHANNELS); - //protocol version (2) - UART1TX(0x41); - UART1TX(0x02); - UART1TX(0x00); - break; - case SUMP_XON://resume send data - // xflow=1; - //break; - case SUMP_XOFF://pause send data - // xflow=0; - break; - - default://long command - sumpRX.command[0] = RECVedByte; //store first command byte - sumpRX.command[1] = getc_cdc(); //store first command byte - sumpRX.command[2] = getc_cdc(); //store first command byte - sumpRX.command[3] = getc_cdc(); //store first command byte - sumpRX.command[4] = getc_cdc(); //store first command byte - switch (sumpRX.command[0]) { - - case SUMP_TRIG: //set CN on these pins -#ifdef BUSPIRATEV4 - if (sumpRX.command[1] & 0b1000000) CNEN4 |= 0b10; //AUX2 -#ifdef BPv4_SUMP_SOFT_WIRE // See hardwarev4a.h - if (sumpRX.command[1] & 0b100000) CNEN4 |= 0b100000; //AUX1 on PORTD:8 will be moved in firmware to B7 of SUMP byte -#else // possible GREEN WIRE mode PORTD8 to PORTD7 - if (sumpRX.command[1] & 0b100000) CNEN2 |= 0b1; //AUX1 on PORTD:7 -#endif - if (sumpRX.command[1] & 0b10000) CNEN1 |= 0b100000000000000; //AUX0 - if (sumpRX.command[1] & 0b1000) CNEN4 |= 0b100; // MOSI - if (sumpRX.command[1] & 0b100) CNEN4 |= 0b1000; // CLK - if (sumpRX.command[1] & 0b10) CNEN4 |= 0b10000; // MISO - if (sumpRX.command[1] & 0b1) CNEN1 |= 0b10000000000000; // CS -#else - if (sumpRX.command[1] & 0b10000) CNEN2 |= 0b1; //AUX - if (sumpRX.command[1] & 0b1000) CNEN2 |= 0b100000; // MOSI - if (sumpRX.command[1] & 0b100) CNEN2 |= 0b1000000; // CLK - if (sumpRX.command[1] & 0b10) CNEN2 |= 0b10000000; // MISO - if (sumpRX.command[1] & 0b1) CNEN2 |= 0b100000000; // CS -#endif - - case SUMP_CNT: - sumpSamples = sumpRX.command[2]; - sumpSamples <<= 8; - sumpSamples |= sumpRX.command[1]; - sumpSamples = (sumpSamples + 1)*4; -#ifndef BUSPIRATEV4 - //prevent buffer overruns - if (sumpSamples > LA_SAMPLE_SIZE) sumpSamples = LA_SAMPLE_SIZE; -#endif - break; - case SUMP_DIV: - l = sumpRX.command[3]; - l <<= 8; - l |= sumpRX.command[2]; - l <<= 8; - l |= sumpRX.command[1]; - - //convert from SUMP 100MHz clock to our 16MIPs - l = ((l + 1)*4) / 25; - - //adjust downwards a bit - if (l > 0x10) - l -= 0x10; - else //fast as possible - l = 1; - - //setup PR register - PR5 = (l >> 16); //most significant word - PR4 = l; //least significant word - break; - } // sumpRX.command - } // switch outbyte - } // if byte received - } while (1 == sump_mode); -} - -void SUMPreset(void) { - BP_LEDMODE = 0; //LED -#ifdef BUSPIRATEV4 - CNPU1 &= 0b1001111111111111; //turn off pullups on used pins - CNPU4 &= 0b1111111111000001; - CNEN1 &= 0b1001111111111111; //turn off used pins change notice - CNEN4 &= 0b1111111111000001; - -#else - CNPU1 = 0; //pullups off - CNPU2 = 0; - CNEN1 = 0; //all change notice off - CNEN2 = 0; -#endif - - T4CON = 0; //stop count - IPC4bits.CNIP = 0; - - //default speed and samples - //setup PR register - PR5 = 0; //most significant word - PR4 = 0x640; //least significant word - sumpSamples = LA_SAMPLE_SIZE; -} - - +/* + * + * USB infrared remote control receiver transmitter firmware v1.0 + * License: creative commons - attribution, share-alike + * Copyright Ian Lesnet 2010 + * http://dangerousprototypes.com + * + */ +/* ***************************************************************************** + BPv4 JTR-SUMP A folked version by JTR. Please see changelog.txt for details. + + * JTR SUMP Version 0.1 beta + ********************************************************************************/ + +#include "base.h" +#include "SUMP.h" + +//commandset +//http://www.sump.org/projects/analyzer/protocol/ +#define SUMP_RESET 0x00 +#define SUMP_RUN 0x01 +#define SUMP_ID 0x02 +#define SUMP_DESC 0x04 +#define SUMP_XON 0x11 +#define SUMP_XOFF 0x13 +#define SUMP_DIV 0x80 +#define SUMP_CNT 0x81 +#define SUMP_FLAGS 0x82 +#define SUMP_TRIG 0xc0 +#define SUMP_TRIG_VALS 0xc1 +#define LA_SAMPLE_SIZE 4096 + +extern BYTE *InPtr, *OutPtr, cdc_In_len, cdc_timeout_count; +extern BDentry *CDC_Outbdp, *CDC_Inbdp; +//extern CDC_FLUSH_CONTROL cdc_Flush_Control; +static unsigned int sumpSamples; + +// REMAPPING OF THE CHANGE NOTIFICATIONS BPv3 Vs BPv4 +//BPv3 +// PORTB0, PORTB1, PORTB2, PORTB3, PORTB4, PORTB5, PORTB6, PORTB7, PORTB8, PORTB9, PORTB10 +//PIN 4 5 6 7 11 14 15 16 17 18 21 +//CN_No 4 5 6 7 1 27 24 23 22 21 16 +//CNEN 2:8 2:7 2:6 2:5 2:0 +//Func CS MISO CLK MOSI AUX1 + + +//BPv4 +// PORTD0, PORTD1, PORTD2, PORTD3, PORTD4, PORTD5, PORTD6, PORTD7, PORTD8 +//PIN 46 49 50 51 52 53 54 55 42 +//CN_N0 49 50 51 52 13 14 15 16 53 +//CNEN 4:1 4:2 4:3 4:4 1:13 1:14 1:15 2:0 4:5 +//Func AUX2 MOSI CLK MISO CS AUX0 AUX1 + +void SUMP(void) { + BYTE RECVedByte, sump_mode; + static unsigned int i; + static unsigned long l; + unsigned int sump_port; + + struct _sumpRX { + BYTE command[5]; + BYTE parameters; + BYTE parCnt; + } sumpRX; + +#ifndef BUSPIRATEV4 + IODIR |= (AUX + MOSI + CLK + MISO + CS); //AUX, MOSI, CLK, MISO, CS pins to input +#else + IODIR |= (AUX0 + AUX1 + AUX2 + MOSI + CLK + MISO + CS + SUMP_SPARE6 + SUMP_SPARE7); // BPv4 PORTD SUMP inputs x9 +#endif + + SUMPreset(); + sump_mode = 1; // Remains in SUMP mode until sump_mode == 0 + bpWstring("1ALS"); + + do { // Remains in this loop until a SUMP RESET (0x00) is received. +#ifndef USB_INTERRUPTS + usb_handler(); +#endif + if (poll_getc_cdc(&RECVedByte) == 1) {//process any command bytes, otherwise it does nothing but loop + + switch (RECVedByte) {//switch on the current byte + case SUMP_RESET://reset + SUMPreset(); + sump_mode = 0; // Will exit at a single exit point below at the end of te do ... while loop + break; + case SUMP_ID://SLA0 or 1 backwards: 1ALS + bpWstring("1ALS"); + break; + + case SUMP_RUN://arm the triger, when completed exits SUMP mode. + + BP_LEDMODE = 1; //ARMED, turn on LED + + //setup timer + T4CON = 0; //make sure the counters are off + //timer 4 internal, measures interval + TMR5HLD = 0x00; + TMR4 = 0x00; + T4CON = 0b1000; //.T32=1, bit 3 + + //setup of change notice interrupt done in meta handler below + + IFS1bits.CNIF = 0; //clear interrupt flag + IEC1bits.CNIE = 0; //interrupts DISABLED + IPC4bits.CNIP = 1; //priority to 0, not actual interupt + + WaitInReady(); + cdc_In_len = 0; + CDC_Flush_In_Now(); // Start with a clean slate. Reset InPtr and count etc. + CDC_Flush_In_Now(); + +#ifndef BUSPIRATEV4 + do { + + } while ((0 == IFS1bits.CNIF) && CNEN2); +#else + do { +#ifndef USB_INTERRUPTS + usb_handler(); +#endif + } while (0 == IFS1bits.CNIF); + +#endif + //OK! Ready to sample + // Add LED control here. + // BP_VREGEN = 0; // JTR this is for debug ATM + + T4CONbits.TON = 1; //start timer4 + IFS1bits.T5IF = 0; //clear interrupt flag//setup timer and wait + + // Critical timed loop, inversely proportional to our acheivable bandwidth + for (i = 0; i < sumpSamples; i++) { //take SAMPLE_SIZE samples +#ifndef BUSPIRATEV4 + *InPtr++ = (PORTB >> 6); //change to pointer for faster use... +#else +#ifdef BPv4_SUMP_SOFT_WIRE // See hardwarev4a.h + sump_port = (PORTD & 0x13f); + if (sump_port & 0x100) + sump_port |= 0x80; + + // Debugging. Chose one + * InPtr++ = sump_port; + // * InPtr++ = i; +#else + * InPtr++ = (BYTE)(PORTD & 0xff); //sump_port; //change to pointer for faster use... +#endif + +#endif + cdc_In_len += 1; + cdc_timeout_count = 0; // Must do this to prevent CDC_FlushOnTimeOut firing. + + if (cdc_In_len == CDC_BUFFER_SIZE) { + putda_cdc(CDC_BUFFER_SIZE); + cdc_In_len = 0; + usb_handler(); + } + while (IFS1bits.T5IF == 0); //wait for timer4 (timer 5 interrupt) + IFS1bits.T5IF = 0; //clear interrupt flag + } +#ifdef BUSPIRATEV4 + CNEN1 &= 0b1001111111111111; //turn off used pins change notice + CNEN4 &= 0b1111111111000001; +#else + CNEN2 = 0; //change notice off +#endif + T4CON = 0; //stop count +#ifndef USB_INTERRUPTS + usb_handler(); +#endif + WaitInReady(); + + // JTR debug only + // putc_cdc('q'); + // putc_cdc('w'); + // putc_cdc('e'); + // BP_VREGEN = 1; + // End JTR debug. + SendZLP(); + SUMPreset(); + sump_mode = 0; + break; + + case SUMP_DESC: + // device name string + UART1TX(0x01); +#ifdef BUSPIRATEV4 + bpWstring("BPv4"); +#else + bpWstring("BPv3"); +#endif + UART1TX(0x00); + //sample memory (4096) meaningless in JTR_SUMP mode + UART1TX(0x21); + UART1TX(0x00); + UART1TX(0x00); + UART1TX(0x10); + UART1TX(0x00); + //sample rate (1MHz) + UART1TX(0x23); + UART1TX(0x00); + UART1TX(0x0F); + UART1TX(0x42); + UART1TX(0x40); + //number of probes (5 or 8 on BPv4) + UART1TX(0x40); + UART1TX(NUM_OF_SUMP_CHANNELS); + //protocol version (2) + UART1TX(0x41); + UART1TX(0x02); + UART1TX(0x00); + break; + case SUMP_XON://resume send data + // xflow=1; + //break; + case SUMP_XOFF://pause send data + // xflow=0; + break; + + default://long command + sumpRX.command[0] = RECVedByte; //store first command byte + sumpRX.command[1] = getc_cdc(); //store first command byte + sumpRX.command[2] = getc_cdc(); //store first command byte + sumpRX.command[3] = getc_cdc(); //store first command byte + sumpRX.command[4] = getc_cdc(); //store first command byte + switch (sumpRX.command[0]) { + + case SUMP_TRIG: //set CN on these pins +#ifdef BUSPIRATEV4 + if (sumpRX.command[1] & 0b1000000) CNEN4 |= 0b10; //AUX2 +#ifdef BPv4_SUMP_SOFT_WIRE // See hardwarev4a.h + if (sumpRX.command[1] & 0b100000) CNEN4 |= 0b100000; //AUX1 on PORTD:8 will be moved in firmware to B7 of SUMP byte +#else // possible GREEN WIRE mode PORTD8 to PORTD7 + if (sumpRX.command[1] & 0b100000) CNEN2 |= 0b1; //AUX1 on PORTD:7 +#endif + if (sumpRX.command[1] & 0b10000) CNEN1 |= 0b100000000000000; //AUX0 + if (sumpRX.command[1] & 0b1000) CNEN4 |= 0b100; // MOSI + if (sumpRX.command[1] & 0b100) CNEN4 |= 0b1000; // CLK + if (sumpRX.command[1] & 0b10) CNEN4 |= 0b10000; // MISO + if (sumpRX.command[1] & 0b1) CNEN1 |= 0b10000000000000; // CS +#else + if (sumpRX.command[1] & 0b10000) CNEN2 |= 0b1; //AUX + if (sumpRX.command[1] & 0b1000) CNEN2 |= 0b100000; // MOSI + if (sumpRX.command[1] & 0b100) CNEN2 |= 0b1000000; // CLK + if (sumpRX.command[1] & 0b10) CNEN2 |= 0b10000000; // MISO + if (sumpRX.command[1] & 0b1) CNEN2 |= 0b100000000; // CS +#endif + + case SUMP_CNT: + sumpSamples = sumpRX.command[2]; + sumpSamples <<= 8; + sumpSamples |= sumpRX.command[1]; + sumpSamples = (sumpSamples + 1)*4; +#ifndef BUSPIRATEV4 + //prevent buffer overruns + if (sumpSamples > LA_SAMPLE_SIZE) sumpSamples = LA_SAMPLE_SIZE; +#endif + break; + case SUMP_DIV: + l = sumpRX.command[3]; + l <<= 8; + l |= sumpRX.command[2]; + l <<= 8; + l |= sumpRX.command[1]; + + //convert from SUMP 100MHz clock to our 16MIPs + l = ((l + 1)*4) / 25; + + //adjust downwards a bit + if (l > 0x10) + l -= 0x10; + else //fast as possible + l = 1; + + //setup PR register + PR5 = (l >> 16); //most significant word + PR4 = l; //least significant word + break; + } // sumpRX.command + } // switch outbyte + } // if byte received + } while (1 == sump_mode); +} + +void SUMPreset(void) { + BP_LEDMODE = 0; //LED +#ifdef BUSPIRATEV4 + CNPU1 &= 0b1001111111111111; //turn off pullups on used pins + CNPU4 &= 0b1111111111000001; + CNEN1 &= 0b1001111111111111; //turn off used pins change notice + CNEN4 &= 0b1111111111000001; + +#else + CNPU1 = 0; //pullups off + CNPU2 = 0; + CNEN1 = 0; //all change notice off + CNEN2 = 0; +#endif + + T4CON = 0; //stop count + IPC4bits.CNIP = 0; + + //default speed and samples + //setup PR register + PR5 = 0; //most significant word + PR4 = 0x640; //least significant word + sumpSamples = LA_SAMPLE_SIZE; +} + + diff --git a/Firmware/OpenOCD.c b/Firmware/OpenOCD.c index 87e90259..bcb7f967 100644 --- a/Firmware/OpenOCD.c +++ b/Firmware/OpenOCD.c @@ -1,313 +1,313 @@ -#include "base.h" -#include "baseIO.h" -#include "busPirateCore.h" -#include "binIOhelpers.h" - -extern struct _modeConfig modeConfig; -extern struct _bpConfig bpConfig; -// tris registers -#define OOCD_TDO_TRIS BP_MISO_DIR -#define OOCD_TMS_TRIS BP_CS_DIR -#define OOCD_CLK_TRIS BP_CLK_DIR -#define OOCD_TDI_TRIS BP_MOSI_DIR -#define OOCD_SRST_TRIS BP_AUX0_DIR -#if defined (BUSPIRATEV3) -#define OOCD_TRST_TRIS BP_PGD_DIR -#endif - -// io ports -#define OOCD_TDO BP_MISO -#define OOCD_TMS BP_CS -#define OOCD_CLK BP_CLK -#define OOCD_TDI BP_MOSI -#define OOCD_SRST BP_AUX0 -#if defined (BUSPIRATEV3) -#define OOCD_TRST BP_PGD -#endif - -// open-drain control -#define OOCD_TDO_ODC BP_MISO -#define OOCD_TMS_ODC BP_CS -#define OOCD_CLK_ODC BP_CLK -#define OOCD_TDI_ODC BP_MOSI -#define OOCD_SRST_ODC BP_AUX0 -#if defined (BUSPIRATEV3) -#define OOCD_TRST_ODC BP_PGD -#endif - -#define CMD_UNKNOWN 0x00 -#define CMD_PORT_MODE 0x01 -#define CMD_FEATURE 0x02 -#define CMD_READ_ADCS 0x03 -//#define CMD_TAP_SHIFT 0x04 // old protocol -#define CMD_TAP_SHIFT 0x05 -#define CMD_ENTER_OOCD 0x06 // this is the same as in binIO -#define CMD_UART_SPEED 0x07 -#define CMD_JTAG_SPEED 0x08 - -static void binOpenOCDString(void); -static void binOpenOCDPinMode(unsigned char mode); -static void binOpenOCDHandleFeature(unsigned char feat, unsigned char action); -static void binOpenOCDAnswer(unsigned char *buf, unsigned int len); -extern void binOpenOCDTapShiftFast(unsigned char *in_buf, unsigned char *out_buf, unsigned int bits, unsigned int delay); - - -enum { - FEATURE_LED=0x01, - FEATURE_VREG=0x02, - FEATURE_TRST=0x04, - FEATURE_SRST=0x08, - FEATURE_PULLUP=0x10 -}; - -enum { - SERIAL_NORMAL=0, - SERIAL_FAST=1 -}; - -enum { - MODE_HIZ=0, - MODE_JTAG=1, - MODE_JTAG_OD=2, // open-drain outputs -}; - - -static unsigned int OpenOCDJtagDelay; - -static void binOpenOCDString(void){bpWstring("OCD1");} - -void binOpenOCD(void){ - unsigned char *buf = bpConfig.terminalInput; // for simplicity :) - unsigned int i,j; - unsigned char inByte; - unsigned char inByte2; - - OpenOCDJtagDelay = 1; - - binOpenOCDString(); - - while(1){ -/* -this will misbehave when polling is turned off in OpenOCD - - // enable timeout timer (1sec) taken from AUX measurement - T4CON=0; - TMR5HLD=0x00; // reset the counter - TMR4=0x00; - T4CON=0b1000; // 32bit timer - PR5=0x1e8; // 0xf4; - PR4=0x4800; // 0x2400; - IFS1bits.T5IF=0; - - // enable timer - T4CONbits.TON=1; - - // wait for byte or timeout - while(U1STAbits.URXDA == 0 && IFS1bits.T5IF==0); - - if (IFS1bits.T5IF==1) { - // disable timer - T4CONbits.TON=0; - // we timeouted, set serial to normal speed - UART1Speed(UART_NORMAL_SPEED); - return; - } - - // disable timer - T4CONbits.TON=0; - - // read the byte - inByte=U1RXREG; -*/ - inByte=UART1RX(); - - switch(inByte){ - case CMD_UNKNOWN: - return; - case CMD_ENTER_OOCD: - binOpenOCDString(); - break; - case CMD_READ_ADCS: - buf[0] = CMD_READ_ADCS; - buf[1] = 8; - AD1CON1bits.ADON = 1; // turn ADC ON - i=bpADC(12); // ADC pin - buf[2] = (unsigned char)(i>>8); - buf[3] = (unsigned char)(i); - i=bpADC(11); // VEXT pin - buf[4] = (unsigned char)(i>>8); - buf[5] = (unsigned char)(i); - i=bpADC(10); // V33 pin - buf[6] = (unsigned char)(i>>8); - buf[7] = (unsigned char)(i); - i=bpADC(9); // V50 pin - buf[8] = (unsigned char)(i>>8); - buf[9] = (unsigned char)(i); - AD1CON1bits.ADON = 0; // turn ADC OFF - binOpenOCDAnswer(buf, 10); - break; - case CMD_PORT_MODE: - inByte=UART1RX(); - binOpenOCDPinMode(inByte); - break; - case CMD_FEATURE: - inByte=UART1RX(); - inByte2=UART1RX(); - binOpenOCDHandleFeature(inByte, inByte2); - break; - case CMD_JTAG_SPEED: - inByte=UART1RX(); - inByte2=UART1RX(); - OpenOCDJtagDelay = (inByte << 8) | inByte2; - break; - case CMD_UART_SPEED: - inByte=UART1RX(); - i = inByte; - if (inByte == SERIAL_FAST) { - UART1Speed(UART_FAST_SPEED); - } else { - UART1Speed(UART_NORMAL_SPEED); - } - - inByte=UART1RX(); - inByte2=UART1RX(); - if ((inByte != 0xAA) || (inByte2 != 0x55)) { - i = SERIAL_NORMAL; - UART1Speed(UART_NORMAL_SPEED); - } - - buf[0] = CMD_UART_SPEED; - buf[1] = (unsigned char)i; - binOpenOCDAnswer(buf, 2); - break; - case CMD_TAP_SHIFT: - inByte=UART1RX(); - inByte2=UART1RX(); - - IFS0bits.U1RXIF = 0; // reset the RX flag - - j = (inByte << 8) | inByte2; // number of bit sequences - - // this fixes possible buffer overflow - if (j > 0x2000) - j = 0x2000; - - i = (j+7)/8; // number of bytes used - buf[0] = CMD_TAP_SHIFT; - buf[1] = inByte; - buf[2] = inByte2; - binOpenOCDAnswer(buf, 3); - - // prepare the interrupt transfer - UART1RXBuf = (unsigned char*)bpConfig.terminalInput; - UART1RXToRecv = 2*i; - UART1RXRecvd = 0; - - UART1TXBuf = (unsigned char*)(bpConfig.terminalInput + 2100); // 2048 bytes + 3 command header + to be sure - UART1TXSent = 0; - UART1TXAvailable = 0; - - // enable RX interrupt - IEC0bits.U1RXIE = 1; - - binOpenOCDTapShiftFast(UART1RXBuf, UART1TXBuf, j, OpenOCDJtagDelay); - break; - default: - buf[0] = 0x00; // unknown command - buf[1] = 0x00; - binOpenOCDAnswer(buf, 1); - break; - } - } -} - -static void binOpenOCDPinMode(unsigned char mode) { - // reset all pins - OOCD_TMS=0; - OOCD_TDI=0; - OOCD_CLK=0; - OOCD_SRST=0; -#if defined (BUSPIRATEV3) - OOCD_TRST=0; -#endif - // setup open-drain if necessary - if (mode == MODE_JTAG_OD) { - OOCD_TMS_ODC=1; - OOCD_CLK_ODC=1; - OOCD_TDI_ODC=1; - OOCD_SRST_ODC=1; -#if defined (BUSPIRATEV3) - OOCD_TRST_ODC=1; -#endif - } else { - OOCD_TMS_ODC=0; - OOCD_CLK_ODC=0; - OOCD_TDI_ODC=0; - OOCD_SRST_ODC=0; -#if defined (BUSPIRATEV3) - OOCD_TRST_ODC=0; -#endif - } - // make pins output - if (mode == MODE_JTAG || mode == MODE_JTAG_OD) { - OOCD_TMS_TRIS=0; - OOCD_TDI_TRIS=0; - OOCD_CLK_TRIS=0; - OOCD_SRST_TRIS=0; -#if defined (BUSPIRATEV3) - OOCD_TRST_TRIS=0; -#endif - OOCD_TDO_TRIS=1; - } else { - OOCD_TMS_TRIS=1; - OOCD_TDI_TRIS=1; - OOCD_CLK_TRIS=1; - OOCD_SRST_TRIS=1; -#if defined (BUSPIRATEV3) - OOCD_TRST_TRIS=1; -#endif - OOCD_TDO_TRIS=1; - } -} - -static void binOpenOCDAnswer(unsigned char *buf, unsigned int len) { - unsigned int i; - for (i=0; i < len; i++ ){ - UART1TX(buf[i]); - } -} - -static void binOpenOCDHandleFeature(unsigned char feat, unsigned char action) { - switch (feat) { - case FEATURE_LED: - BP_LEDMODE_DIR=0; //LED to output - BP_LEDMODE=action; - break; - case FEATURE_VREG: - if (action) { - BP_VREG_ON(); - } else { - BP_VREG_OFF(); - } - break; - case FEATURE_PULLUP: -#if defined (BUSPIRATEV3) - if (action) { - BP_PULLUP_ON(); - } else { - BP_PULLUP_OFF(); - } -#endif - break; -#if defined (BUSPIRATEV3) - case FEATURE_TRST: - OOCD_TRST=action; - break; -#endif - case FEATURE_SRST: - OOCD_SRST=action; - break; - default: - break; - } -} +#include "base.h" +#include "baseIO.h" +#include "busPirateCore.h" +#include "binIOhelpers.h" + +extern struct _modeConfig modeConfig; +extern struct _bpConfig bpConfig; +// tris registers +#define OOCD_TDO_TRIS BP_MISO_DIR +#define OOCD_TMS_TRIS BP_CS_DIR +#define OOCD_CLK_TRIS BP_CLK_DIR +#define OOCD_TDI_TRIS BP_MOSI_DIR +#define OOCD_SRST_TRIS BP_AUX0_DIR +#if defined (BUSPIRATEV3) +#define OOCD_TRST_TRIS BP_PGD_DIR +#endif + +// io ports +#define OOCD_TDO BP_MISO +#define OOCD_TMS BP_CS +#define OOCD_CLK BP_CLK +#define OOCD_TDI BP_MOSI +#define OOCD_SRST BP_AUX0 +#if defined (BUSPIRATEV3) +#define OOCD_TRST BP_PGD +#endif + +// open-drain control +#define OOCD_TDO_ODC BP_MISO +#define OOCD_TMS_ODC BP_CS +#define OOCD_CLK_ODC BP_CLK +#define OOCD_TDI_ODC BP_MOSI +#define OOCD_SRST_ODC BP_AUX0 +#if defined (BUSPIRATEV3) +#define OOCD_TRST_ODC BP_PGD +#endif + +#define CMD_UNKNOWN 0x00 +#define CMD_PORT_MODE 0x01 +#define CMD_FEATURE 0x02 +#define CMD_READ_ADCS 0x03 +//#define CMD_TAP_SHIFT 0x04 // old protocol +#define CMD_TAP_SHIFT 0x05 +#define CMD_ENTER_OOCD 0x06 // this is the same as in binIO +#define CMD_UART_SPEED 0x07 +#define CMD_JTAG_SPEED 0x08 + +static void binOpenOCDString(void); +static void binOpenOCDPinMode(unsigned char mode); +static void binOpenOCDHandleFeature(unsigned char feat, unsigned char action); +static void binOpenOCDAnswer(unsigned char *buf, unsigned int len); +extern void binOpenOCDTapShiftFast(unsigned char *in_buf, unsigned char *out_buf, unsigned int bits, unsigned int delay); + + +enum { + FEATURE_LED=0x01, + FEATURE_VREG=0x02, + FEATURE_TRST=0x04, + FEATURE_SRST=0x08, + FEATURE_PULLUP=0x10 +}; + +enum { + SERIAL_NORMAL=0, + SERIAL_FAST=1 +}; + +enum { + MODE_HIZ=0, + MODE_JTAG=1, + MODE_JTAG_OD=2, // open-drain outputs +}; + + +static unsigned int OpenOCDJtagDelay; + +static void binOpenOCDString(void){bpWstring("OCD1");} + +void binOpenOCD(void){ + unsigned char *buf = bpConfig.terminalInput; // for simplicity :) + unsigned int i,j; + unsigned char inByte; + unsigned char inByte2; + + OpenOCDJtagDelay = 1; + + binOpenOCDString(); + + while(1){ +/* +this will misbehave when polling is turned off in OpenOCD + + // enable timeout timer (1sec) taken from AUX measurement + T4CON=0; + TMR5HLD=0x00; // reset the counter + TMR4=0x00; + T4CON=0b1000; // 32bit timer + PR5=0x1e8; // 0xf4; + PR4=0x4800; // 0x2400; + IFS1bits.T5IF=0; + + // enable timer + T4CONbits.TON=1; + + // wait for byte or timeout + while(U1STAbits.URXDA == 0 && IFS1bits.T5IF==0); + + if (IFS1bits.T5IF==1) { + // disable timer + T4CONbits.TON=0; + // we timeouted, set serial to normal speed + UART1Speed(UART_NORMAL_SPEED); + return; + } + + // disable timer + T4CONbits.TON=0; + + // read the byte + inByte=U1RXREG; +*/ + inByte=UART1RX(); + + switch(inByte){ + case CMD_UNKNOWN: + return; + case CMD_ENTER_OOCD: + binOpenOCDString(); + break; + case CMD_READ_ADCS: + buf[0] = CMD_READ_ADCS; + buf[1] = 8; + AD1CON1bits.ADON = 1; // turn ADC ON + i=bpADC(12); // ADC pin + buf[2] = (unsigned char)(i>>8); + buf[3] = (unsigned char)(i); + i=bpADC(11); // VEXT pin + buf[4] = (unsigned char)(i>>8); + buf[5] = (unsigned char)(i); + i=bpADC(10); // V33 pin + buf[6] = (unsigned char)(i>>8); + buf[7] = (unsigned char)(i); + i=bpADC(9); // V50 pin + buf[8] = (unsigned char)(i>>8); + buf[9] = (unsigned char)(i); + AD1CON1bits.ADON = 0; // turn ADC OFF + binOpenOCDAnswer(buf, 10); + break; + case CMD_PORT_MODE: + inByte=UART1RX(); + binOpenOCDPinMode(inByte); + break; + case CMD_FEATURE: + inByte=UART1RX(); + inByte2=UART1RX(); + binOpenOCDHandleFeature(inByte, inByte2); + break; + case CMD_JTAG_SPEED: + inByte=UART1RX(); + inByte2=UART1RX(); + OpenOCDJtagDelay = (inByte << 8) | inByte2; + break; + case CMD_UART_SPEED: + inByte=UART1RX(); + i = inByte; + if (inByte == SERIAL_FAST) { + UART1Speed(UART_FAST_SPEED); + } else { + UART1Speed(UART_NORMAL_SPEED); + } + + inByte=UART1RX(); + inByte2=UART1RX(); + if ((inByte != 0xAA) || (inByte2 != 0x55)) { + i = SERIAL_NORMAL; + UART1Speed(UART_NORMAL_SPEED); + } + + buf[0] = CMD_UART_SPEED; + buf[1] = (unsigned char)i; + binOpenOCDAnswer(buf, 2); + break; + case CMD_TAP_SHIFT: + inByte=UART1RX(); + inByte2=UART1RX(); + + IFS0bits.U1RXIF = 0; // reset the RX flag + + j = (inByte << 8) | inByte2; // number of bit sequences + + // this fixes possible buffer overflow + if (j > 0x2000) + j = 0x2000; + + i = (j+7)/8; // number of bytes used + buf[0] = CMD_TAP_SHIFT; + buf[1] = inByte; + buf[2] = inByte2; + binOpenOCDAnswer(buf, 3); + + // prepare the interrupt transfer + UART1RXBuf = (unsigned char*)bpConfig.terminalInput; + UART1RXToRecv = 2*i; + UART1RXRecvd = 0; + + UART1TXBuf = (unsigned char*)(bpConfig.terminalInput + 2100); // 2048 bytes + 3 command header + to be sure + UART1TXSent = 0; + UART1TXAvailable = 0; + + // enable RX interrupt + IEC0bits.U1RXIE = 1; + + binOpenOCDTapShiftFast(UART1RXBuf, UART1TXBuf, j, OpenOCDJtagDelay); + break; + default: + buf[0] = 0x00; // unknown command + buf[1] = 0x00; + binOpenOCDAnswer(buf, 1); + break; + } + } +} + +static void binOpenOCDPinMode(unsigned char mode) { + // reset all pins + OOCD_TMS=0; + OOCD_TDI=0; + OOCD_CLK=0; + OOCD_SRST=0; +#if defined (BUSPIRATEV3) + OOCD_TRST=0; +#endif + // setup open-drain if necessary + if (mode == MODE_JTAG_OD) { + OOCD_TMS_ODC=1; + OOCD_CLK_ODC=1; + OOCD_TDI_ODC=1; + OOCD_SRST_ODC=1; +#if defined (BUSPIRATEV3) + OOCD_TRST_ODC=1; +#endif + } else { + OOCD_TMS_ODC=0; + OOCD_CLK_ODC=0; + OOCD_TDI_ODC=0; + OOCD_SRST_ODC=0; +#if defined (BUSPIRATEV3) + OOCD_TRST_ODC=0; +#endif + } + // make pins output + if (mode == MODE_JTAG || mode == MODE_JTAG_OD) { + OOCD_TMS_TRIS=0; + OOCD_TDI_TRIS=0; + OOCD_CLK_TRIS=0; + OOCD_SRST_TRIS=0; +#if defined (BUSPIRATEV3) + OOCD_TRST_TRIS=0; +#endif + OOCD_TDO_TRIS=1; + } else { + OOCD_TMS_TRIS=1; + OOCD_TDI_TRIS=1; + OOCD_CLK_TRIS=1; + OOCD_SRST_TRIS=1; +#if defined (BUSPIRATEV3) + OOCD_TRST_TRIS=1; +#endif + OOCD_TDO_TRIS=1; + } +} + +static void binOpenOCDAnswer(unsigned char *buf, unsigned int len) { + unsigned int i; + for (i=0; i < len; i++ ){ + UART1TX(buf[i]); + } +} + +static void binOpenOCDHandleFeature(unsigned char feat, unsigned char action) { + switch (feat) { + case FEATURE_LED: + BP_LEDMODE_DIR=0; //LED to output + BP_LEDMODE=action; + break; + case FEATURE_VREG: + if (action) { + BP_VREG_ON(); + } else { + BP_VREG_OFF(); + } + break; + case FEATURE_PULLUP: +#if defined (BUSPIRATEV3) + if (action) { + BP_PULLUP_ON(); + } else { + BP_PULLUP_OFF(); + } +#endif + break; +#if defined (BUSPIRATEV3) + case FEATURE_TRST: + OOCD_TRST=action; + break; +#endif + case FEATURE_SRST: + OOCD_SRST=action; + break; + default: + break; + } +} diff --git a/Firmware/OpenOCD.h b/Firmware/OpenOCD.h index 3b18faca..5f39220d 100644 --- a/Firmware/OpenOCD.h +++ b/Firmware/OpenOCD.h @@ -1,6 +1,6 @@ -#ifndef OPENOCD_H_ -#define OPENOCD_H_ - -void binOpenOCD(void); - -#endif +#ifndef OPENOCD_H_ +#define OPENOCD_H_ + +void binOpenOCD(void); + +#endif diff --git a/Firmware/SPI.c b/Firmware/SPI.c index 61478ad8..34d98133 100644 --- a/Firmware/SPI.c +++ b/Firmware/SPI.c @@ -1,834 +1,834 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "SPI.h" -#include "base.h" -#include "busPirateCore.h" -#include "binIOhelpers.h" - -#include "procMenu.h" // for the userinteraction subs - -//#define USE_SPICS //the CS hardware pin on silicone REV 3 doesn't work, optionally enable it here - -#ifdef BP_USE_HWSPI -// enable special AVR-specific commands for bulk flash reading and other purposes -#define AVR_EXTENDED_COMMANDS - -//direction registers -#define SPIMOSI_TRIS BP_MOSI_DIR -#define SPICLK_TRIS BP_CLK_DIR -#define SPIMISO_TRIS BP_MISO_DIR -#define SPICS_TRIS BP_CS_DIR - -//pin control registers -#define SPIMOSI BP_MOSI -#define SPICLK BP_CLK -#define SPIMISO BP_MISO -#define SPICS BP_CS - -//open drain control registers for OUTPUT pins -#define SPIMOSI_ODC BP_MOSI_ODC -#define SPICLK_ODC BP_CLK_ODC -#define SPICS_ODC BP_CS_ODC - -#define SPICS_RPIN BP_CS_RPIN - -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; -extern struct _bpConfig bpConfig; //we use the big buffer - -void binSPIversionString(void); -//void spiSetup(unsigned char spiSpeed); -//void spiDisable(void); -//unsigned char spiWriteByte(unsigned char c); -void spiSlaveDisable(void); -void spiSlaveSetup(void); -void spiSniffer(unsigned char csState, unsigned char termMode); - -struct _SPI { - unsigned char ckp : 1; - unsigned char cke : 1; - unsigned char smp : 1; - // unsigned char wwr:1; - unsigned char csl : 1; // to /CS or not to CS -} spiSettings; - -static unsigned char SPIspeed[] = {0b00000, 0b11000, 0b11100, 0b11101}; //30,125,250,1000khz; datasheet pg 142 - -/* -// move into a .h or other .c??? -int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* -int getint(void); -int getrepeat(void); -void consumewhitechars(void); -extern int cmderror; - */ - -void SPIstartr(void) { - modeConfig.wwr = 1; - if (spiSettings.csl) { - SPICS = 0; - } else { - SPICS = 1; - } - ///bpWmessage(MSG_CS_ENABLED); - if (spiSettings.csl) UART1TX('/'); - BPMSG1159; -} - -void SPIstart(void) { - modeConfig.wwr = 0; - if (spiSettings.csl) { - SPICS = 0; - } else { - SPICS = 1; - } - //bpWmessage(MSG_CS_ENABLED); - if (spiSettings.csl) UART1TX('/'); - BPMSG1159; -} - -void SPIstop(void) { - if (spiSettings.csl) { - SPICS = 1; - } else { - SPICS = 0; - } - - //bpWmessage(MSG_CS_DISABLED); - if (spiSettings.csl) UART1TX('/'); - BPMSG1160; -} - -unsigned int SPIread(void) { - return (spiWriteByte(0xff)); -} - -unsigned int SPIwrite(unsigned int c) { - unsigned char r; - - r = spiWriteByte(c); - if (modeConfig.wwr == 1) { - return r; - } - //FIXME what to return if wwr=0? we need an uint here - return 0; //JTR just to get rid of the warning msg -} - -void SPIsettings(void) { //bpWstring("SPI (spd ckp ske smp hiz)=( "); - BPMSG1191; - bpWdec((modeConfig.speed + 1)); - bpSP; - bpWdec(spiSettings.ckp); - bpSP; - bpWdec(spiSettings.cke); - bpSP; - bpWdec(spiSettings.smp); - bpSP; - bpWdec(spiSettings.csl); - bpSP; - bpWdec(modeConfig.HiZ); - bpSP; - //bpWline(")\r\n"); - BPMSG1162; -} - -void SPIsetup(void) { - int speed, clkpol, clkedge, sample, output, cslow; - - consumewhitechars(); - speed = getint(); - consumewhitechars(); - clkpol = getint(); - consumewhitechars(); - clkedge = getint(); - consumewhitechars(); - sample = getint(); - consumewhitechars(); - cslow = getint(); - consumewhitechars(); - output = getint(); - - // bpWdec(speed); bpSP; - // bpWdec(clkpol); bpSP; - // bpWdec(clkedge); bpSP; - // bpWdec(sample); bpSP; - // bpWdec(output); bpBR; - - // check for userinput (and sanitycheck it!!) - if ((speed > 0) && (speed <= 4)) { - modeConfig.speed = speed - 1; - } else { - speed = 0; // when speed is 0 we ask the user - } - - if ((clkpol > 0) && (clkpol <= 2)) { - spiSettings.ckp = clkpol - 1; - } else { - speed = 0; // when speed is 0 we ask the user - } - - if ((clkedge > 0) && (clkedge <= 2)) { - spiSettings.cke = clkedge - 1; - } else { - speed = 0; // when speed is 0 we ask the user - } - - if ((sample > 0) && (sample <= 2)) { - spiSettings.smp = sample - 1; - } else { - speed = 0; // when speed is 0 we ask the user - } - - if ((cslow > 0) && (cslow <= 2)) { - spiSettings.csl = (cslow - 1); - } else { - speed = 0; // when speed is 0 we ask the user - } - - - if ((output > 0) && (output <= 2)) { - modeConfig.HiZ = (~(output - 1)); - } else { - speed = 0; // when speed is 0 we ask the user - } - - - if (speed == 0) // no (valid) cmdline options found - { - cmderror = 0; // reset errorflag because of no cmdlineinput - - //bpWstring("Set speed:\x0D\x0A 1. 30KHz\x0D\x0A 2. 125KHz\x0D\x0A 3. 250KHz\x0D\x0A 4. 1MHz\x0D\x0A"); - //bpWline(OUMSG_SPI_SPEED); - BPMSG1187; - //modeConfig.speed=(bpUserNumberPrompt(1, 4, 1)-1); - modeConfig.speed = getnumber(1, 1, 4, 0) - 1; - - //bpWstring("Clock polarity:\x0D\x0A 1. Idle low *default\x0D\x0A 2. Idle high\x0D\x0A"); - //bpWmessage(MSG_OPT_CKP); - BPMSG1188; - //spiSettings.ckp=(bpUserNumberPrompt(1, 2, 1)-1); - spiSettings.ckp = getnumber(1, 1, 2, 0) - 1; - - //bpWstring("Output clock edge:\x0D\x0A 1. Idle to active\x0D\x0A 2. Active to idle *default\x0D\x0A"); - //bpWmessage(MSG_OPT_CKE); - BPMSG1189; - //spiSettings.cke=(bpUserNumberPrompt(1, 2, 2)-1); - spiSettings.cke = getnumber(2, 1, 2, 0) - 1; - - //bpWstring("Input sample phase:\x0D\x0A 1. Middle *default\x0D\x0A 2. End\x0D\x0A"); - //bpWmessage(MSG_OPT_SMP); - BPMSG1190; - //spiSettings.smp=(bpUserNumberPrompt(1, 2, 1)-1); - spiSettings.smp = getnumber(1, 1, 2, 0) - 1; - - //bpWline("CS:\r\n 1. CS\r\n 2. /CS *default"); - BPMSG1253; - spiSettings.csl = getnumber(2, 1, 2, 0) - 1; - - //bpWmessage(MSG_OPT_OUTPUT_TYPE); - BPMSG1142; - //modeConfig.HiZ=(~(bpUserNumberPrompt(1, 2, 1)-1)); - modeConfig.HiZ = (~(getnumber(1, 1, 2, 0) - 1)); - } else { - SPIsettings(); - } - modeConfig.wwr = 0; -} - -void SPIsetup_exc(void) -{ - //do SPI peripheral setup - spiSetup(SPIspeed[modeConfig.speed]); - - // set cs the way the user wants - SPICS = spiSettings.csl; -} - -void SPIcleanup(void) { - spiDisable(); -} - -void SPImacro(unsigned int macro) { - - switch (macro) { - case 0: - //bpWline(OUMSG_SPI_MACRO_MENU); - BPMSG1192; - break; - case 1://sniff CS low - BPMSG1071; //moved to a more generic message - BPMSG1250; - spiSniffer(0, 1); //configure for terminal mode - break; - case 2://sniff all - BPMSG1071; //moved to a more generic message - BPMSG1250; - spiSniffer(1, 1); //configure for terminal mode - break; - case 3: //sniff CS high - break; - case 10: - spiSettings.ckp = 0; - goto SPImacro_settings_cleanup; - case 11: - spiSettings.ckp = 1; - goto SPImacro_settings_cleanup; - case 12: - spiSettings.cke = 0; - goto SPImacro_settings_cleanup; - case 13: - spiSettings.cke = 1; - goto SPImacro_settings_cleanup; - case 14: - spiSettings.smp = 0; - goto SPImacro_settings_cleanup; - case 15: - spiSettings.smp = 1; -SPImacro_settings_cleanup: - SPI1CON1bits.CKP = spiSettings.ckp; - SPI1CON1bits.CKE = spiSettings.cke; - SPI1CON1bits.SMP = spiSettings.smp; - SPIsettings(); - break; - default: - //bpWmessage(MSG_ERROR_MACRO); - BPMSG1016; - } -} - -void SPIpins(void) { - #if defined(BUSPIRATEV4) - BPMSG1258; //bpWline("CS\tMISO\tCLK\tMOSI"); - #else - BPMSG1225; //bpWline("CLK\tMOSI\tCS\tMISO"); - #endif -} - -void spiSetup(unsigned char spiSpeed) { - SPI1STATbits.SPIEN = 0; //disable, just in case... - - //use open drain control register to - //enable Hi-Z mode on hardware module outputs - //inputs are already HiZ - if (modeConfig.HiZ == 1) { - SPIMOSI_ODC = 1; - SPICLK_ODC = 1; - SPICS_ODC = 1; - } else { - SPIMOSI_ODC = 0; - SPICLK_ODC = 0; - SPICS_ODC = 0; - } - - //PPS Setup - // Inputs - RPINR20bits.SDI1R = BP_MISO_RPIN; //B7 MISO - // Outputs - BP_MOSI_RPOUT = SDO1_IO; //B9 MOSI - BP_CLK_RPOUT = SCK1OUT_IO; //B8 CLK - - //#if defined(BUSPIRATEV3) - // // Inputs - // RPINR20bits.SDI1R = 7; //B7 MISO - // // Outputs - // RPOR4bits.RP9R = SDO1_IO; //B9 MOSI - // RPOR4bits.RP8R = SCK1OUT_IO; //B8 CLK - //#elif defined(BUSPIRATEV4) - // // Inputs - // RPINR20bits.SDI1R = 22; //B7 MISO - // // Outputs - // RPOR12bits.RP24R = SDO1_IO; //B9 MOSI - // RPOR11bits.RP23R = SCK1OUT_IO; //B8 CLK - //#endif - - SPICS = 1; //B6 cs high - SPICS_TRIS = 0; //B6 cs output - - //pps configures pins and this doesn't really matter.... - SPICLK_TRIS = 0; //B8 sck output - SPIMISO_TRIS = 1; //B7 SDI input - SPIMOSI_TRIS = 0; //B9 SDO output - - /* CKE=1, CKP=0, SMP=0 */ - SPI1CON1 = spiSpeed; //(SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. - SPI1CON1bits.MSTEN = 1; - SPI1CON1bits.CKP = spiSettings.ckp; - SPI1CON1bits.CKE = spiSettings.cke; - SPI1CON1bits.SMP = spiSettings.smp; - SPI1CON2 = 0; - SPI1STAT = 0; // clear SPI - SPI1STATbits.SPIEN = 1; -} - -void spiDisable(void) { - SPI1STATbits.SPIEN = 0; - RPINR20bits.SDI1R = 0b11111; //B7 MISO - - //PPS Disable - BP_MOSI_RPOUT=0; - BP_CLK_RPOUT=0; - -// #if defined(BUSPIRATEV3) -// RPOR4bits.RP9R=0; //B9 MOSI -// RPOR4bits.RP8R=0; //B8 CLK -// #elif defined(BUSPIRATEV4) -// RPOR12bits.RP24R=0; //B9 MOSI -// RPOR11bits.RP23R=0; //B8 CLK -// #endif - - //disable all open drain control register bits - SPIMOSI_ODC = 0; - SPICLK_ODC = 0; - SPICS_ODC = 0; - //make all input maybe??? -} - -unsigned char spiWriteByte(unsigned char c) { - - SPI1BUF = c; - while (!IFS0bits.SPI1IF); - c = SPI1BUF; - IFS0bits.SPI1IF = 0; - return c; -} - -// -// -// SPI Sniffer -// -// -#define USE_SPICS //enabled SPI CS pin - -void spiSniffer(unsigned char csState, unsigned char termMode) { - unsigned char c, lastCS; - -spiSnifferStart: - lastCS = 1; - - UARTbufSetup(); - spiDisable(); - spiSlaveSetup(); - - if (csState == 0) { //mode 0, use CS pin - SPI1CON1bits.SSEN = 1; //CS pin active - SPI2CON1bits.SSEN = 1; //CS pin active - } - - if (csState < 2) { //mode 0 & 1, always on - SPI1STATbits.SPIEN = 1; - SPI2STATbits.SPIEN = 1; - } - - while (1) { - - //detect when CS changes. works independently of the data interrupts - if (lastCS == 0 && SPICS == 1) { - UARTbuf(']'); //bpWBR; //cs disabled - lastCS = 1; - } - - if (SPI1STATbits.SRXMPT == 0 && SPI2STATbits.SRXMPT == 0) {//rx buffer NOT empty, get and display byte - c = SPI1BUF; - - if (lastCS == 1) { - UARTbuf('['); //bpWBR; //CS enabled - lastCS = 0; //SPICS; - } - - if (termMode) { //show hex output in terminal mode - bpWhexBuf(c); - } else { //escaped byte value in binary mode - UARTbuf('\\'); - UARTbuf(c); - } - - c = SPI2BUF; - - if (termMode) { //show hex output in terminal mode - UARTbuf('('); //only show the () in terminal mode - bpWhexBuf(c); - UARTbuf(')'); - } else { //binary mode - UARTbuf(c); - } - - } - - if (SPI1STATbits.SPIROV == 1 || SPI2STATbits.SPIROV == 1 || bpConfig.overflow == 1) {//we weren't fast enough, buffer overflow - - if (bpConfig.overflow == 0) UARTbufFlush(); - SPI1STAT = 0; - SPI2STAT = 0; - - if (termMode) { - bpWline("Couldn't keep up"); - goto spiSnifferStart; - } - - BP_LEDMODE = 0; - break; - } - - UARTbufService(); - if (UART1RXRdy() == 1) {//any key pressed, exit - c = UART1RX(); - /* JTR usb port; */; - if (termMode) bpBR; //fixed in 5.1: also sent br to binmode - break; - } - } - spiSlaveDisable(); - - spiSetup(SPIspeed[modeConfig.speed]); -} - -//configure both SPI units for slave mode on different pins -//use current settings - -void spiSlaveSetup(void) { - // unsigned char c; - //SPI1STATbits.SPIEN=0; //SPI module off - - //assign pins for SPI slave mode - SPICS_TRIS = 1; //B6 cs input - SPICLK_TRIS = 1; //B8 sck input - SPIMISO_TRIS = 1; //B7 SDI input - SPIMOSI_TRIS = 1; //b9 SDO input - - //More PPS - //#ifdef USE_SPICS - RPINR21bits.SS1R = BP_CS_RPIN; //SPICS_RPIN; //assign CS function to bus pirate CS pin - RPINR23bits.SS2R = BP_CS_RPIN; - //#endif - RPINR20bits.SDI1R = BP_MOSI_RPIN; //B9 MOSI - RPINR20bits.SCK1R = BP_CLK_RPIN; //SPICLK_RPIN; //assign SPI1 CLK input to bus pirate CLK pin - RPINR22bits.SDI2R = BP_MISO_RPIN; //B7 MiSo - RPINR22bits.SCK2R = BP_CLK_RPIN; //SPICLK_RPIN; //assign SPI2 CLK input to bus pirate CLK pin - -// //#ifdef USE_SPICS -// RPINR21bits.SS1R = 6; //SPICS_RPIN; //assign CS function to bus pirate CS pin -// RPINR23bits.SS2R = 6; -// //#endif -// RPINR20bits.SDI1R = 9; //B9 MOSI -// RPINR20bits.SCK1R = 8; //SPICLK_RPIN; //assign SPI1 CLK input to bus pirate CLK pin -// RPINR22bits.SDI2R = 7; //B7 MiSo -// RPINR22bits.SCK2R = 8; //SPICLK_RPIN; //assign SPI2 CLK input to bus pirate CLK pin - - //clear old SPI settings first - SPI1CON1 = (SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. - SPI1CON1bits.CKP = spiSettings.ckp; - SPI1CON1bits.CKE = spiSettings.cke; - //SPI1CON1bits.SMP=spiSettings.smp; - SPI1CON2 = 0; - SPI1STAT = 0; // clear SPI - - SPI2CON1 = (SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. - SPI2CON1bits.CKP = spiSettings.ckp; - SPI2CON1bits.CKE = spiSettings.cke; - SPI2CON2 = 0; - SPI2STAT = 0; // clear SPI - - //To set up the SPI module for the Enhanced Buffer - //Slave mode of operation: - //1. Clear the SPIxBUF register. - SPI1BUF = 0; - SPI2BUF = 0; - //3. Write the desired settings to the SPIxCON1 and SPIxCON2 registers with MSTEN (SPIxCON1<5>) = 0. - //4. Clear the SMP bit. - SPI1CON1bits.SMP = 0; - SPI2CON1bits.SMP = 0; - - // SPI1CON1bits.SSEN=1; //CS pin active - // SPI2CON1bits.SSEN=1; //CS pin active - - SPI1CON1bits.DISSDO = 1; //Disable SDO pin in slave mode - SPI1CON1bits.MSTEN = 0; - SPI2CON1bits.DISSDO = 1; //Disable SDO pin in slave mode - SPI2CON1bits.MSTEN = 0; - //5. If the CKE bit is set, then the SSEN bit must be set, thus enabling the SSx pin. - //6. Clear the SPIROV bit (SPIxSTAT<6>). - SPI1STATbits.SPIROV = 0; - SPI2STATbits.SPIROV = 0; - //7. Select Enhanced Buffer mode by setting the SPIBEN bit (SPIxCON2<0>). - SPI1CON2bits.SPIBEN = 1; - SPI2CON2bits.SPIBEN = 1; - //8. Enable SPI operation by setting the SPIEN bit(SPIxSTAT<15>). - //SPI1STATbits.SPIEN=1; - -} - -void spiSlaveDisable(void) { - SPI1STATbits.SPIEN = 0; //SPI module off - SPI1CON1bits.DISSDO = 0; //restore SDO pin - SPI2STATbits.SPIEN = 0; //SPI module off - SPI2CON1bits.DISSDO = 0; //restore SDO pin - - //#ifdef USE_SPICS - RPINR21bits.SS1R = 0b11111; //assign CS input to none - RPINR23bits.SS2R = 0b11111; //assign CS input to none - //#endif - RPINR20bits.SDI1R = 0b11111; - RPINR20bits.SCK1R = 0b11111; //assign CLK input to none - RPINR22bits.SDI2R = 0b11111; - RPINR22bits.SCK2R = 0b11111; //assign CLK input to none - -} - -/* -rawSPI mode: - * 00000000 � Enter raw bitbang mode, reset to raw bitbang mode - * 00000001 � SPI mode/rawSPI version string (SPI1) - * 00000010 � CS low (0) - * 00000011 � CS high (1) - * Sniffers - * 0001xxxx � Bulk SPI transfer, send 1-16 bytes (0=1byte!) - * 0100wxyz � Configure peripherals, w=power, x=pullups, y=AUX, z=CS - * 01100xxx � Set SPI speed, 30, 125, 250khz; 1, 2, 2.6, 4, 8MHz - * 1000wxyz � SPI config, w=output type, x=idle, y=clock edge, z=sample - * 00000110 - AVR Extended Commands - * 00000000 - Null operation - verifies extended commands are available. - * 00000001 - Return version (2 bytes) - * 00000010 - Bulk Memory Read from Flash - - */ -static unsigned char binSPIspeed[]={0b00000,0b11000,0b11100,0b11101,0b00011,0b01011,0b10011,0b11011}; //00=30,01=125,10=250,11=1000khz, 100=2mhz,101=2.667mhz, 110=4mhz, 111=8mhz; datasheet pg 142 - -void binSPIversionString(void) { - bpWstring("SPI1"); -} - -void binSPI(void) { - static unsigned char inByte, rawCommand, i; - unsigned int j, fw, fr; -#ifdef AVR_EXTENDED_COMMANDS - unsigned long saddr, length; -#endif - - //useful default values - /* CKE=1, CKP=0, SMP=0 */ - modeConfig.speed = 1; - spiSettings.ckp = 0; - spiSettings.cke = 1; - spiSettings.smp = 0; - modeConfig.HiZ = 1; - spiSetup(binSPIspeed[modeConfig.speed]); //start with 250khz (30,125,250,1000khz) - binSPIversionString(); //1 - SPI setup and reply string - - while (1) { - - inByte = UART1RX(); /* JTR usb port; */ //grab it - rawCommand = (inByte >> 4); //get command bits in seperate variable - - switch (rawCommand) { - case 0://reset/setup/config commands - switch (inByte) { - case 0://0, reset exit - spiDisable(); - return; //exit - break; - case 1://1 - SPI setup and reply string - binSPIversionString(); - break; - case 2: - IOLAT &= (~CS); //SPICS=0; //cs enable/low - UART1TX(1); - break; - case 3: - IOLAT |= CS; //SPICS=1; //cs disable/high - UART1TX(1); - break; - case 0b1101: //all traffic 13 - UART1TX(1); - spiSniffer(1, 0); - break; - case 0b1110://cs low 14 - UART1TX(1); - spiSniffer(0, 0); - break; - //case 0b1111://cs high - // spiSniffer(1, 0); - // UART1TX(1); - // break; - case 4: //write-then-read, with !CS/CS - case 5: //write-then-read, NO CS! - //get the number of commands that will follow - fw = UART1RX(); - /* JTR usb port; */; //get byte - fw = fw << 8; - fw |= UART1RX(); - /* JTR usb port; */; //get byte - - //get the number of reads to do - fr = UART1RX(); - /* JTR usb port; */; //get byte - fr = fr << 8; - fr |= UART1RX(); - /* JTR usb port; */; //get byte - - - //check length and report error - if (fw > TERMINAL_BUFFER || fr > TERMINAL_BUFFER) { - UART1TX(0); - break; - } - - //get bytes - for (j = 0; j < fw; j++) { - bpConfig.terminalInput[j] = UART1RX(); - /* JTR usb port; */; - } - - if (inByte == 4) SPICS = 0; - for (j = 0; j < fw; j++) { - spiWriteByte(bpConfig.terminalInput[j]); - } - bpDelayUS(1); - for (j = 0; j < fr; j++) { //read bulk bytes from SPI - bpConfig.terminalInput[j] = spiWriteByte(0xff); - } - if (inByte == 4) SPICS = 1; - - UART1TX(1); //send 1/OK - - for (j = 0; j < fr; j++) { //send the read buffer contents over serial - UART1TX(bpConfig.terminalInput[j]); - } - - break; -#ifdef AVR_EXTENDED_COMMANDS - case 6: // AVR Extended Commands - UART1TX(1); // send 1/OK (ie, AVR Extended Commands accepted) - - inByte = UART1RX(); //grab it - - switch (inByte) - { - case 0x00: // null operation, return OK - UART1TX(1); // send 1/OK - break; - case 0x01: // version check - UART1TX(1); // send 1/OK - UART1TX(0x00); - UART1TX(0x01); // version 1 - break; - case 0x02: // bulk memory read from flash - // read in the start address (4 bytes, MSB first) - saddr = 0; - for (j = 0; j < 4; j++) - { - - inByte = UART1RX(); //grab it - saddr = (saddr << 8) | inByte; - } - - // read in the bytes to read (4 bytes, MSB first) [inclusive] - length = 0; - for (j = 0; j < 4; j++) - { - - inByte = UART1RX(); //grab it - length = (length << 8) | inByte; - } - - // FIXME - Can't handle pages past the first 64kb - if (saddr > 0xFFFF || length > 0xFFFF || (saddr + length) > 0xFFFF) - { - UART1TX(0); - } - else - { - // just assume it'll work... - UART1TX(0x01); // send 1/OK - - for (j = saddr; length > 0; j++) - { - // fetch low byte from this memory word - spiWriteByte(0x20); - spiWriteByte(j >> 8); - spiWriteByte(j & 0xFF); - UART1TX(spiWriteByte(0x00)); // fetch byte that was read - length--; - - if (length == 0) break; - - // fetch high byte from this memory word - spiWriteByte(0x28); - spiWriteByte(j >> 8); - spiWriteByte(j & 0xFF); - UART1TX(spiWriteByte(0x00)); // fetch byte that was read - length--; - } - } - break; - default: - UART1TX(0); - break; - } -#endif - break; - default: - UART1TX(0); - break; - } - break; - case 0b0001://get x+1 bytes - inByte &= (~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1); //send 1/OK - - for (i = 0; i < inByte; i++) { - UART1TX(spiWriteByte(UART1RX())); - } - - break; - case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS - binIOperipheralset(inByte); - UART1TX(1); //send 1/OK - break; - -#ifdef BUSPIRATEV4 - case 0b0101: - UART1TX(binBBpullVoltage(inByte)); - break; -#endif - case 0b0110://set speed - inByte &= (~0b11111000); //clear command portion - modeConfig.speed = inByte; - spiSetup(binSPIspeed[modeConfig.speed]); //resetup SPI - UART1TX(1); //send 1/OK - break; - case 0b1000: //set SPI config - //wxyz //w=HiZ(0)/3.3v(1), x=CKP idle (low=0), y=CKE clock edge (active to idle=1), z=SMP sample (middle=0) - spiSettings.ckp = 0; - spiSettings.cke = 0; - spiSettings.smp = 0; - modeConfig.HiZ = 0; - if (inByte & 0b100) spiSettings.ckp = 1; //set idle - if (inByte & 0b10) spiSettings.cke = 1; //set edge - if (inByte & 0b1) spiSettings.smp = 1; //set sample time - if ((inByte & 0b1000) == 0) modeConfig.HiZ = 1; //hiz output if this bit is 1 - spiSetup(binSPIspeed[modeConfig.speed]); //resetup SPI - UART1TX(1); //send 1/OK - break; - default: - UART1TX(0x00); //send 0/Error - break; - }//command switch - }//while loop -}//function - -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "SPI.h" +#include "base.h" +#include "busPirateCore.h" +#include "binIOhelpers.h" + +#include "procMenu.h" // for the userinteraction subs + +//#define USE_SPICS //the CS hardware pin on silicone REV 3 doesn't work, optionally enable it here + +#ifdef BP_USE_HWSPI +// enable special AVR-specific commands for bulk flash reading and other purposes +#define AVR_EXTENDED_COMMANDS + +//direction registers +#define SPIMOSI_TRIS BP_MOSI_DIR +#define SPICLK_TRIS BP_CLK_DIR +#define SPIMISO_TRIS BP_MISO_DIR +#define SPICS_TRIS BP_CS_DIR + +//pin control registers +#define SPIMOSI BP_MOSI +#define SPICLK BP_CLK +#define SPIMISO BP_MISO +#define SPICS BP_CS + +//open drain control registers for OUTPUT pins +#define SPIMOSI_ODC BP_MOSI_ODC +#define SPICLK_ODC BP_CLK_ODC +#define SPICS_ODC BP_CS_ODC + +#define SPICS_RPIN BP_CS_RPIN + +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; +extern struct _bpConfig bpConfig; //we use the big buffer + +void binSPIversionString(void); +//void spiSetup(unsigned char spiSpeed); +//void spiDisable(void); +//unsigned char spiWriteByte(unsigned char c); +void spiSlaveDisable(void); +void spiSlaveSetup(void); +void spiSniffer(unsigned char csState, unsigned char termMode); + +struct _SPI { + unsigned char ckp : 1; + unsigned char cke : 1; + unsigned char smp : 1; + // unsigned char wwr:1; + unsigned char csl : 1; // to /CS or not to CS +} spiSettings; + +static unsigned char SPIspeed[] = {0b00000, 0b11000, 0b11100, 0b11101}; //30,125,250,1000khz; datasheet pg 142 + +/* +// move into a .h or other .c??? +int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* +int getint(void); +int getrepeat(void); +void consumewhitechars(void); +extern int cmderror; + */ + +void SPIstartr(void) { + modeConfig.wwr = 1; + if (spiSettings.csl) { + SPICS = 0; + } else { + SPICS = 1; + } + ///bpWmessage(MSG_CS_ENABLED); + if (spiSettings.csl) UART1TX('/'); + BPMSG1159; +} + +void SPIstart(void) { + modeConfig.wwr = 0; + if (spiSettings.csl) { + SPICS = 0; + } else { + SPICS = 1; + } + //bpWmessage(MSG_CS_ENABLED); + if (spiSettings.csl) UART1TX('/'); + BPMSG1159; +} + +void SPIstop(void) { + if (spiSettings.csl) { + SPICS = 1; + } else { + SPICS = 0; + } + + //bpWmessage(MSG_CS_DISABLED); + if (spiSettings.csl) UART1TX('/'); + BPMSG1160; +} + +unsigned int SPIread(void) { + return (spiWriteByte(0xff)); +} + +unsigned int SPIwrite(unsigned int c) { + unsigned char r; + + r = spiWriteByte(c); + if (modeConfig.wwr == 1) { + return r; + } + //FIXME what to return if wwr=0? we need an uint here + return 0; //JTR just to get rid of the warning msg +} + +void SPIsettings(void) { //bpWstring("SPI (spd ckp ske smp hiz)=( "); + BPMSG1191; + bpWdec((modeConfig.speed + 1)); + bpSP; + bpWdec(spiSettings.ckp); + bpSP; + bpWdec(spiSettings.cke); + bpSP; + bpWdec(spiSettings.smp); + bpSP; + bpWdec(spiSettings.csl); + bpSP; + bpWdec(modeConfig.HiZ); + bpSP; + //bpWline(")\r\n"); + BPMSG1162; +} + +void SPIsetup(void) { + int speed, clkpol, clkedge, sample, output, cslow; + + consumewhitechars(); + speed = getint(); + consumewhitechars(); + clkpol = getint(); + consumewhitechars(); + clkedge = getint(); + consumewhitechars(); + sample = getint(); + consumewhitechars(); + cslow = getint(); + consumewhitechars(); + output = getint(); + + // bpWdec(speed); bpSP; + // bpWdec(clkpol); bpSP; + // bpWdec(clkedge); bpSP; + // bpWdec(sample); bpSP; + // bpWdec(output); bpBR; + + // check for userinput (and sanitycheck it!!) + if ((speed > 0) && (speed <= 4)) { + modeConfig.speed = speed - 1; + } else { + speed = 0; // when speed is 0 we ask the user + } + + if ((clkpol > 0) && (clkpol <= 2)) { + spiSettings.ckp = clkpol - 1; + } else { + speed = 0; // when speed is 0 we ask the user + } + + if ((clkedge > 0) && (clkedge <= 2)) { + spiSettings.cke = clkedge - 1; + } else { + speed = 0; // when speed is 0 we ask the user + } + + if ((sample > 0) && (sample <= 2)) { + spiSettings.smp = sample - 1; + } else { + speed = 0; // when speed is 0 we ask the user + } + + if ((cslow > 0) && (cslow <= 2)) { + spiSettings.csl = (cslow - 1); + } else { + speed = 0; // when speed is 0 we ask the user + } + + + if ((output > 0) && (output <= 2)) { + modeConfig.HiZ = (~(output - 1)); + } else { + speed = 0; // when speed is 0 we ask the user + } + + + if (speed == 0) // no (valid) cmdline options found + { + cmderror = 0; // reset errorflag because of no cmdlineinput + + //bpWstring("Set speed:\x0D\x0A 1. 30KHz\x0D\x0A 2. 125KHz\x0D\x0A 3. 250KHz\x0D\x0A 4. 1MHz\x0D\x0A"); + //bpWline(OUMSG_SPI_SPEED); + BPMSG1187; + //modeConfig.speed=(bpUserNumberPrompt(1, 4, 1)-1); + modeConfig.speed = getnumber(1, 1, 4, 0) - 1; + + //bpWstring("Clock polarity:\x0D\x0A 1. Idle low *default\x0D\x0A 2. Idle high\x0D\x0A"); + //bpWmessage(MSG_OPT_CKP); + BPMSG1188; + //spiSettings.ckp=(bpUserNumberPrompt(1, 2, 1)-1); + spiSettings.ckp = getnumber(1, 1, 2, 0) - 1; + + //bpWstring("Output clock edge:\x0D\x0A 1. Idle to active\x0D\x0A 2. Active to idle *default\x0D\x0A"); + //bpWmessage(MSG_OPT_CKE); + BPMSG1189; + //spiSettings.cke=(bpUserNumberPrompt(1, 2, 2)-1); + spiSettings.cke = getnumber(2, 1, 2, 0) - 1; + + //bpWstring("Input sample phase:\x0D\x0A 1. Middle *default\x0D\x0A 2. End\x0D\x0A"); + //bpWmessage(MSG_OPT_SMP); + BPMSG1190; + //spiSettings.smp=(bpUserNumberPrompt(1, 2, 1)-1); + spiSettings.smp = getnumber(1, 1, 2, 0) - 1; + + //bpWline("CS:\r\n 1. CS\r\n 2. /CS *default"); + BPMSG1253; + spiSettings.csl = getnumber(2, 1, 2, 0) - 1; + + //bpWmessage(MSG_OPT_OUTPUT_TYPE); + BPMSG1142; + //modeConfig.HiZ=(~(bpUserNumberPrompt(1, 2, 1)-1)); + modeConfig.HiZ = (~(getnumber(1, 1, 2, 0) - 1)); + } else { + SPIsettings(); + } + modeConfig.wwr = 0; +} + +void SPIsetup_exc(void) +{ + //do SPI peripheral setup + spiSetup(SPIspeed[modeConfig.speed]); + + // set cs the way the user wants + SPICS = spiSettings.csl; +} + +void SPIcleanup(void) { + spiDisable(); +} + +void SPImacro(unsigned int macro) { + + switch (macro) { + case 0: + //bpWline(OUMSG_SPI_MACRO_MENU); + BPMSG1192; + break; + case 1://sniff CS low + BPMSG1071; //moved to a more generic message + BPMSG1250; + spiSniffer(0, 1); //configure for terminal mode + break; + case 2://sniff all + BPMSG1071; //moved to a more generic message + BPMSG1250; + spiSniffer(1, 1); //configure for terminal mode + break; + case 3: //sniff CS high + break; + case 10: + spiSettings.ckp = 0; + goto SPImacro_settings_cleanup; + case 11: + spiSettings.ckp = 1; + goto SPImacro_settings_cleanup; + case 12: + spiSettings.cke = 0; + goto SPImacro_settings_cleanup; + case 13: + spiSettings.cke = 1; + goto SPImacro_settings_cleanup; + case 14: + spiSettings.smp = 0; + goto SPImacro_settings_cleanup; + case 15: + spiSettings.smp = 1; +SPImacro_settings_cleanup: + SPI1CON1bits.CKP = spiSettings.ckp; + SPI1CON1bits.CKE = spiSettings.cke; + SPI1CON1bits.SMP = spiSettings.smp; + SPIsettings(); + break; + default: + //bpWmessage(MSG_ERROR_MACRO); + BPMSG1016; + } +} + +void SPIpins(void) { + #if defined(BUSPIRATEV4) + BPMSG1258; //bpWline("CS\tMISO\tCLK\tMOSI"); + #else + BPMSG1225; //bpWline("CLK\tMOSI\tCS\tMISO"); + #endif +} + +void spiSetup(unsigned char spiSpeed) { + SPI1STATbits.SPIEN = 0; //disable, just in case... + + //use open drain control register to + //enable Hi-Z mode on hardware module outputs + //inputs are already HiZ + if (modeConfig.HiZ == 1) { + SPIMOSI_ODC = 1; + SPICLK_ODC = 1; + SPICS_ODC = 1; + } else { + SPIMOSI_ODC = 0; + SPICLK_ODC = 0; + SPICS_ODC = 0; + } + + //PPS Setup + // Inputs + RPINR20bits.SDI1R = BP_MISO_RPIN; //B7 MISO + // Outputs + BP_MOSI_RPOUT = SDO1_IO; //B9 MOSI + BP_CLK_RPOUT = SCK1OUT_IO; //B8 CLK + + //#if defined(BUSPIRATEV3) + // // Inputs + // RPINR20bits.SDI1R = 7; //B7 MISO + // // Outputs + // RPOR4bits.RP9R = SDO1_IO; //B9 MOSI + // RPOR4bits.RP8R = SCK1OUT_IO; //B8 CLK + //#elif defined(BUSPIRATEV4) + // // Inputs + // RPINR20bits.SDI1R = 22; //B7 MISO + // // Outputs + // RPOR12bits.RP24R = SDO1_IO; //B9 MOSI + // RPOR11bits.RP23R = SCK1OUT_IO; //B8 CLK + //#endif + + SPICS = 1; //B6 cs high + SPICS_TRIS = 0; //B6 cs output + + //pps configures pins and this doesn't really matter.... + SPICLK_TRIS = 0; //B8 sck output + SPIMISO_TRIS = 1; //B7 SDI input + SPIMOSI_TRIS = 0; //B9 SDO output + + /* CKE=1, CKP=0, SMP=0 */ + SPI1CON1 = spiSpeed; //(SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. + SPI1CON1bits.MSTEN = 1; + SPI1CON1bits.CKP = spiSettings.ckp; + SPI1CON1bits.CKE = spiSettings.cke; + SPI1CON1bits.SMP = spiSettings.smp; + SPI1CON2 = 0; + SPI1STAT = 0; // clear SPI + SPI1STATbits.SPIEN = 1; +} + +void spiDisable(void) { + SPI1STATbits.SPIEN = 0; + RPINR20bits.SDI1R = 0b11111; //B7 MISO + + //PPS Disable + BP_MOSI_RPOUT=0; + BP_CLK_RPOUT=0; + +// #if defined(BUSPIRATEV3) +// RPOR4bits.RP9R=0; //B9 MOSI +// RPOR4bits.RP8R=0; //B8 CLK +// #elif defined(BUSPIRATEV4) +// RPOR12bits.RP24R=0; //B9 MOSI +// RPOR11bits.RP23R=0; //B8 CLK +// #endif + + //disable all open drain control register bits + SPIMOSI_ODC = 0; + SPICLK_ODC = 0; + SPICS_ODC = 0; + //make all input maybe??? +} + +unsigned char spiWriteByte(unsigned char c) { + + SPI1BUF = c; + while (!IFS0bits.SPI1IF); + c = SPI1BUF; + IFS0bits.SPI1IF = 0; + return c; +} + +// +// +// SPI Sniffer +// +// +#define USE_SPICS //enabled SPI CS pin + +void spiSniffer(unsigned char csState, unsigned char termMode) { + unsigned char c, lastCS; + +spiSnifferStart: + lastCS = 1; + + UARTbufSetup(); + spiDisable(); + spiSlaveSetup(); + + if (csState == 0) { //mode 0, use CS pin + SPI1CON1bits.SSEN = 1; //CS pin active + SPI2CON1bits.SSEN = 1; //CS pin active + } + + if (csState < 2) { //mode 0 & 1, always on + SPI1STATbits.SPIEN = 1; + SPI2STATbits.SPIEN = 1; + } + + while (1) { + + //detect when CS changes. works independently of the data interrupts + if (lastCS == 0 && SPICS == 1) { + UARTbuf(']'); //bpWBR; //cs disabled + lastCS = 1; + } + + if (SPI1STATbits.SRXMPT == 0 && SPI2STATbits.SRXMPT == 0) {//rx buffer NOT empty, get and display byte + c = SPI1BUF; + + if (lastCS == 1) { + UARTbuf('['); //bpWBR; //CS enabled + lastCS = 0; //SPICS; + } + + if (termMode) { //show hex output in terminal mode + bpWhexBuf(c); + } else { //escaped byte value in binary mode + UARTbuf('\\'); + UARTbuf(c); + } + + c = SPI2BUF; + + if (termMode) { //show hex output in terminal mode + UARTbuf('('); //only show the () in terminal mode + bpWhexBuf(c); + UARTbuf(')'); + } else { //binary mode + UARTbuf(c); + } + + } + + if (SPI1STATbits.SPIROV == 1 || SPI2STATbits.SPIROV == 1 || bpConfig.overflow == 1) {//we weren't fast enough, buffer overflow + + if (bpConfig.overflow == 0) UARTbufFlush(); + SPI1STAT = 0; + SPI2STAT = 0; + + if (termMode) { + bpWline("Couldn't keep up"); + goto spiSnifferStart; + } + + BP_LEDMODE = 0; + break; + } + + UARTbufService(); + if (UART1RXRdy() == 1) {//any key pressed, exit + c = UART1RX(); + /* JTR usb port; */; + if (termMode) bpBR; //fixed in 5.1: also sent br to binmode + break; + } + } + spiSlaveDisable(); + + spiSetup(SPIspeed[modeConfig.speed]); +} + +//configure both SPI units for slave mode on different pins +//use current settings + +void spiSlaveSetup(void) { + // unsigned char c; + //SPI1STATbits.SPIEN=0; //SPI module off + + //assign pins for SPI slave mode + SPICS_TRIS = 1; //B6 cs input + SPICLK_TRIS = 1; //B8 sck input + SPIMISO_TRIS = 1; //B7 SDI input + SPIMOSI_TRIS = 1; //b9 SDO input + + //More PPS + //#ifdef USE_SPICS + RPINR21bits.SS1R = BP_CS_RPIN; //SPICS_RPIN; //assign CS function to bus pirate CS pin + RPINR23bits.SS2R = BP_CS_RPIN; + //#endif + RPINR20bits.SDI1R = BP_MOSI_RPIN; //B9 MOSI + RPINR20bits.SCK1R = BP_CLK_RPIN; //SPICLK_RPIN; //assign SPI1 CLK input to bus pirate CLK pin + RPINR22bits.SDI2R = BP_MISO_RPIN; //B7 MiSo + RPINR22bits.SCK2R = BP_CLK_RPIN; //SPICLK_RPIN; //assign SPI2 CLK input to bus pirate CLK pin + +// //#ifdef USE_SPICS +// RPINR21bits.SS1R = 6; //SPICS_RPIN; //assign CS function to bus pirate CS pin +// RPINR23bits.SS2R = 6; +// //#endif +// RPINR20bits.SDI1R = 9; //B9 MOSI +// RPINR20bits.SCK1R = 8; //SPICLK_RPIN; //assign SPI1 CLK input to bus pirate CLK pin +// RPINR22bits.SDI2R = 7; //B7 MiSo +// RPINR22bits.SCK2R = 8; //SPICLK_RPIN; //assign SPI2 CLK input to bus pirate CLK pin + + //clear old SPI settings first + SPI1CON1 = (SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. + SPI1CON1bits.CKP = spiSettings.ckp; + SPI1CON1bits.CKE = spiSettings.cke; + //SPI1CON1bits.SMP=spiSettings.smp; + SPI1CON2 = 0; + SPI1STAT = 0; // clear SPI + + SPI2CON1 = (SPIspeed[modeConfig.speed]); // CKE (output edge) active to idle, CKP idle low, SMP data sampled middle of output time. + SPI2CON1bits.CKP = spiSettings.ckp; + SPI2CON1bits.CKE = spiSettings.cke; + SPI2CON2 = 0; + SPI2STAT = 0; // clear SPI + + //To set up the SPI module for the Enhanced Buffer + //Slave mode of operation: + //1. Clear the SPIxBUF register. + SPI1BUF = 0; + SPI2BUF = 0; + //3. Write the desired settings to the SPIxCON1 and SPIxCON2 registers with MSTEN (SPIxCON1<5>) = 0. + //4. Clear the SMP bit. + SPI1CON1bits.SMP = 0; + SPI2CON1bits.SMP = 0; + + // SPI1CON1bits.SSEN=1; //CS pin active + // SPI2CON1bits.SSEN=1; //CS pin active + + SPI1CON1bits.DISSDO = 1; //Disable SDO pin in slave mode + SPI1CON1bits.MSTEN = 0; + SPI2CON1bits.DISSDO = 1; //Disable SDO pin in slave mode + SPI2CON1bits.MSTEN = 0; + //5. If the CKE bit is set, then the SSEN bit must be set, thus enabling the SSx pin. + //6. Clear the SPIROV bit (SPIxSTAT<6>). + SPI1STATbits.SPIROV = 0; + SPI2STATbits.SPIROV = 0; + //7. Select Enhanced Buffer mode by setting the SPIBEN bit (SPIxCON2<0>). + SPI1CON2bits.SPIBEN = 1; + SPI2CON2bits.SPIBEN = 1; + //8. Enable SPI operation by setting the SPIEN bit(SPIxSTAT<15>). + //SPI1STATbits.SPIEN=1; + +} + +void spiSlaveDisable(void) { + SPI1STATbits.SPIEN = 0; //SPI module off + SPI1CON1bits.DISSDO = 0; //restore SDO pin + SPI2STATbits.SPIEN = 0; //SPI module off + SPI2CON1bits.DISSDO = 0; //restore SDO pin + + //#ifdef USE_SPICS + RPINR21bits.SS1R = 0b11111; //assign CS input to none + RPINR23bits.SS2R = 0b11111; //assign CS input to none + //#endif + RPINR20bits.SDI1R = 0b11111; + RPINR20bits.SCK1R = 0b11111; //assign CLK input to none + RPINR22bits.SDI2R = 0b11111; + RPINR22bits.SCK2R = 0b11111; //assign CLK input to none + +} + +/* +rawSPI mode: + * 00000000 � Enter raw bitbang mode, reset to raw bitbang mode + * 00000001 � SPI mode/rawSPI version string (SPI1) + * 00000010 � CS low (0) + * 00000011 � CS high (1) + * Sniffers + * 0001xxxx � Bulk SPI transfer, send 1-16 bytes (0=1byte!) + * 0100wxyz � Configure peripherals, w=power, x=pullups, y=AUX, z=CS + * 01100xxx � Set SPI speed, 30, 125, 250khz; 1, 2, 2.6, 4, 8MHz + * 1000wxyz � SPI config, w=output type, x=idle, y=clock edge, z=sample + * 00000110 - AVR Extended Commands + * 00000000 - Null operation - verifies extended commands are available. + * 00000001 - Return version (2 bytes) + * 00000010 - Bulk Memory Read from Flash + + */ +static unsigned char binSPIspeed[]={0b00000,0b11000,0b11100,0b11101,0b00011,0b01011,0b10011,0b11011}; //00=30,01=125,10=250,11=1000khz, 100=2mhz,101=2.667mhz, 110=4mhz, 111=8mhz; datasheet pg 142 + +void binSPIversionString(void) { + bpWstring("SPI1"); +} + +void binSPI(void) { + static unsigned char inByte, rawCommand, i; + unsigned int j, fw, fr; +#ifdef AVR_EXTENDED_COMMANDS + unsigned long saddr, length; +#endif + + //useful default values + /* CKE=1, CKP=0, SMP=0 */ + modeConfig.speed = 1; + spiSettings.ckp = 0; + spiSettings.cke = 1; + spiSettings.smp = 0; + modeConfig.HiZ = 1; + spiSetup(binSPIspeed[modeConfig.speed]); //start with 250khz (30,125,250,1000khz) + binSPIversionString(); //1 - SPI setup and reply string + + while (1) { + + inByte = UART1RX(); /* JTR usb port; */ //grab it + rawCommand = (inByte >> 4); //get command bits in seperate variable + + switch (rawCommand) { + case 0://reset/setup/config commands + switch (inByte) { + case 0://0, reset exit + spiDisable(); + return; //exit + break; + case 1://1 - SPI setup and reply string + binSPIversionString(); + break; + case 2: + IOLAT &= (~CS); //SPICS=0; //cs enable/low + UART1TX(1); + break; + case 3: + IOLAT |= CS; //SPICS=1; //cs disable/high + UART1TX(1); + break; + case 0b1101: //all traffic 13 + UART1TX(1); + spiSniffer(1, 0); + break; + case 0b1110://cs low 14 + UART1TX(1); + spiSniffer(0, 0); + break; + //case 0b1111://cs high + // spiSniffer(1, 0); + // UART1TX(1); + // break; + case 4: //write-then-read, with !CS/CS + case 5: //write-then-read, NO CS! + //get the number of commands that will follow + fw = UART1RX(); + /* JTR usb port; */; //get byte + fw = fw << 8; + fw |= UART1RX(); + /* JTR usb port; */; //get byte + + //get the number of reads to do + fr = UART1RX(); + /* JTR usb port; */; //get byte + fr = fr << 8; + fr |= UART1RX(); + /* JTR usb port; */; //get byte + + + //check length and report error + if (fw > TERMINAL_BUFFER || fr > TERMINAL_BUFFER) { + UART1TX(0); + break; + } + + //get bytes + for (j = 0; j < fw; j++) { + bpConfig.terminalInput[j] = UART1RX(); + /* JTR usb port; */; + } + + if (inByte == 4) SPICS = 0; + for (j = 0; j < fw; j++) { + spiWriteByte(bpConfig.terminalInput[j]); + } + bpDelayUS(1); + for (j = 0; j < fr; j++) { //read bulk bytes from SPI + bpConfig.terminalInput[j] = spiWriteByte(0xff); + } + if (inByte == 4) SPICS = 1; + + UART1TX(1); //send 1/OK + + for (j = 0; j < fr; j++) { //send the read buffer contents over serial + UART1TX(bpConfig.terminalInput[j]); + } + + break; +#ifdef AVR_EXTENDED_COMMANDS + case 6: // AVR Extended Commands + UART1TX(1); // send 1/OK (ie, AVR Extended Commands accepted) + + inByte = UART1RX(); //grab it + + switch (inByte) + { + case 0x00: // null operation, return OK + UART1TX(1); // send 1/OK + break; + case 0x01: // version check + UART1TX(1); // send 1/OK + UART1TX(0x00); + UART1TX(0x01); // version 1 + break; + case 0x02: // bulk memory read from flash + // read in the start address (4 bytes, MSB first) + saddr = 0; + for (j = 0; j < 4; j++) + { + + inByte = UART1RX(); //grab it + saddr = (saddr << 8) | inByte; + } + + // read in the bytes to read (4 bytes, MSB first) [inclusive] + length = 0; + for (j = 0; j < 4; j++) + { + + inByte = UART1RX(); //grab it + length = (length << 8) | inByte; + } + + // FIXME - Can't handle pages past the first 64kb + if (saddr > 0xFFFF || length > 0xFFFF || (saddr + length) > 0xFFFF) + { + UART1TX(0); + } + else + { + // just assume it'll work... + UART1TX(0x01); // send 1/OK + + for (j = saddr; length > 0; j++) + { + // fetch low byte from this memory word + spiWriteByte(0x20); + spiWriteByte(j >> 8); + spiWriteByte(j & 0xFF); + UART1TX(spiWriteByte(0x00)); // fetch byte that was read + length--; + + if (length == 0) break; + + // fetch high byte from this memory word + spiWriteByte(0x28); + spiWriteByte(j >> 8); + spiWriteByte(j & 0xFF); + UART1TX(spiWriteByte(0x00)); // fetch byte that was read + length--; + } + } + break; + default: + UART1TX(0); + break; + } +#endif + break; + default: + UART1TX(0); + break; + } + break; + case 0b0001://get x+1 bytes + inByte &= (~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1); //send 1/OK + + for (i = 0; i < inByte; i++) { + UART1TX(spiWriteByte(UART1RX())); + } + + break; + case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS + binIOperipheralset(inByte); + UART1TX(1); //send 1/OK + break; + +#ifdef BUSPIRATEV4 + case 0b0101: + UART1TX(binBBpullVoltage(inByte)); + break; +#endif + case 0b0110://set speed + inByte &= (~0b11111000); //clear command portion + modeConfig.speed = inByte; + spiSetup(binSPIspeed[modeConfig.speed]); //resetup SPI + UART1TX(1); //send 1/OK + break; + case 0b1000: //set SPI config + //wxyz //w=HiZ(0)/3.3v(1), x=CKP idle (low=0), y=CKE clock edge (active to idle=1), z=SMP sample (middle=0) + spiSettings.ckp = 0; + spiSettings.cke = 0; + spiSettings.smp = 0; + modeConfig.HiZ = 0; + if (inByte & 0b100) spiSettings.ckp = 1; //set idle + if (inByte & 0b10) spiSettings.cke = 1; //set edge + if (inByte & 0b1) spiSettings.smp = 1; //set sample time + if ((inByte & 0b1000) == 0) modeConfig.HiZ = 1; //hiz output if this bit is 1 + spiSetup(binSPIspeed[modeConfig.speed]); //resetup SPI + UART1TX(1); //send 1/OK + break; + default: + UART1TX(0x00); //send 0/Error + break; + }//command switch + }//while loop +}//function + +#endif diff --git a/Firmware/SPI.h b/Firmware/SPI.h index c9db60d8..22bd75c1 100644 --- a/Firmware/SPI.h +++ b/Firmware/SPI.h @@ -1,33 +1,33 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -void spiProcess(void); -void binSPI(void); - -void SPIstart(void); -void SPIstartr(void); -void SPIstop(void); -unsigned int SPIread(void); -unsigned int SPIwrite(unsigned int c); -void SPIsetup(void); -void SPIsetup_exc(void); -void SPIcleanup(void); -void SPImacro(unsigned int macro); -void SPIpins(void); -void spiSetup(unsigned char spiSpeed); -void spiDisable(void); -unsigned char spiWriteByte(unsigned char c); -void SPIsettings(void); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +void spiProcess(void); +void binSPI(void); + +void SPIstart(void); +void SPIstartr(void); +void SPIstop(void); +unsigned int SPIread(void); +unsigned int SPIwrite(unsigned int c); +void SPIsetup(void); +void SPIsetup_exc(void); +void SPIcleanup(void); +void SPImacro(unsigned int macro); +void SPIpins(void); +void spiSetup(unsigned char spiSpeed); +void spiDisable(void); +unsigned char spiWriteByte(unsigned char c); +void SPIsettings(void); + diff --git a/Firmware/SUMP.c b/Firmware/SUMP.c index fa6abc7f..dfb59b70 100644 --- a/Firmware/SUMP.c +++ b/Firmware/SUMP.c @@ -1,260 +1,260 @@ -// -// SUMP LA -// -//#ifdef SUMP_LA -#include "base.h" -#include "UART.h" -#include "busPirateCore.h" -#include "SUMP.h" - -//commandset -//http://www.sump.org/projects/analyzer/protocol/ -#define SUMP_RESET 0x00 -#define SUMP_RUN 0x01 -#define SUMP_ID 0x02 -#define SUMP_DESC 0x04 -#define SUMP_XON 0x11 -#define SUMP_XOFF 0x13 -#define SUMP_DIV 0x80 -#define SUMP_CNT 0x81 -#define SUMP_FLAGS 0x82 -#define SUMP_TRIG 0xc0 -#define SUMP_TRIG_VALS 0xc1 - -extern struct _bpConfig bpConfig; //holds persistant bus pirate settings (see busPirateCore.h) - -static enum _LAstate { - LA_IDLE = 0, - LA_ARMED, -} LAstate = LA_IDLE; - -#define LA_SAMPLE_SIZE TERMINAL_BUFFER //(see busPirateCore.h) -//static unsigned char samples[LA_SAMPLE_SIZE]; -static unsigned char sumpPadBytes; -static unsigned int sumpSamples; - - -void SUMP(void){ - - IODIR|=(AUX+MOSI+CLK+MISO+CS);//AUX, MOSI, CLK, MISO, CS pins to input - //SUMPlogicCommand(SUMP_RESET); - SUMPreset(); - SUMPlogicCommand(SUMP_ID); - while(1){ - if(UART1RXRdy()){ - if(SUMPlogicCommand(UART1RX())) return; - } - if(SUMPlogicService())return; //exit at end of sampling - } -} - -void SUMPreset(void){ - BP_LEDMODE=0;//LED - CNPU1=0; //pullups off - CNPU2=0; - CNEN1=0; //all change notice off - CNEN2=0; - T4CON=0; //stop count - IPC4bits.CNIP=0; - - //default speed and samples - //setup PR register - PR5=0;//most significant word - PR4=0x640;//least significant word - sumpSamples=LA_SAMPLE_SIZE; - sumpPadBytes=0; - LAstate=LA_IDLE; -} - -unsigned char SUMPlogicCommand(unsigned char inByte){ -// static unsigned char i; - static unsigned long l; - - static enum _SUMP { - C_IDLE = 0, - C_PARAMETERS, - C_PROCESS, - } sumpRXstate = C_IDLE; - - static struct _sumpRX { - unsigned char command[5]; - unsigned char parameters; - unsigned char parCnt; - } sumpRX; - - switch(sumpRXstate){ //this is a state machine that grabs the incoming commands one byte at a time - - case C_IDLE: - - switch(inByte){//switch on the current byte - case SUMP_RESET://reset - SUMPreset(); - return 1; - break; - case SUMP_ID://SLA0 or 1 backwards: 1ALS - bpWstring("1ALS"); - break; - case SUMP_RUN://arm the triger - BP_LEDMODE=1;//ARMED, turn on LED - - //setup timer - T4CON=0; //make sure the counters are off - //timer 4 internal, measures interval - TMR5HLD=0x00; - TMR4=0x00; - T4CON=0b1000; //.T32=1, bit 3 - - //setup change notice interrupt - //CNEN2=0b111100001; //change notice on all pins - //CNEN2=0b100000; //change notice on CS pin - IFS1bits.CNIF=0; //clear interrupt flag - IEC1bits.CNIE=0; //interrupts DISABLED - IPC4bits.CNIP=1; //priority to 0, not actual interupt - - LAstate=LA_ARMED; - break; - case SUMP_DESC: - // device name string - UART1TX(0x01); - bpWstring("BPv3"); - UART1TX(0x00); - //sample memory (4096) - UART1TX(0x21); - UART1TX(0x00); - UART1TX(0x00); - UART1TX(0x10); - UART1TX(0x00); - //sample rate (1MHz) - UART1TX(0x23); - UART1TX(0x00); - UART1TX(0x0F); - UART1TX(0x42); - UART1TX(0x40); - //number of probes (5) - UART1TX(0x40); - UART1TX(0x05); - //protocol version (2) - UART1TX(0x41); - UART1TX(0x02); - UART1TX(0x00); - break; - case SUMP_XON://resume send data - // xflow=1; - //break; - case SUMP_XOFF://pause send data - // xflow=0; - break; - default://long command - sumpRX.command[0]=inByte;//store first command byte - sumpRX.parameters=4; //all long commands are 5 bytes, get 4 parameters - sumpRX.parCnt=0;//reset the parameter counter - sumpRXstate=C_PARAMETERS; - break; - } - break; - case C_PARAMETERS: - sumpRX.parCnt++; - sumpRX.command[sumpRX.parCnt]=inByte;//store each parameter - if(sumpRX.parCntLA_SAMPLE_SIZE) sumpSamples=LA_SAMPLE_SIZE; - break; - case SUMP_DIV: - l=sumpRX.command[3]; - l<<=8; - l|=sumpRX.command[2]; - l<<=8; - l|=sumpRX.command[1]; - - //convert from SUMP 100MHz clock to our 16MIPs - //l=((l+1)*16)/100; - l=((l+1)*4)/25; - - //adjust downwards a bit - if(l>0x10) - l-=0x10; - else //fast as possible - l=1; - - //setup PR register - PR5=(l>>16);//most significant word - PR4=l;//least significant word - break; - } - - sumpRXstate=C_IDLE; - break; - } - return 0; -} - -// -// -// To avoid rewriting interrupt vectors with the bootloader, -// this firmware currently uses polling to read the trigger and timer -// A final version should use interrupts after lots of testing. -// -// -unsigned char SUMPlogicService(void){ - static unsigned int i; -// static unsigned char j; - - switch(LAstate){//dump data - case LA_ARMED: //check interrupt flags - if(IFS1bits.CNIF==0){//no flags - if(CNEN2) //if no trigger just continue - break; - } - - //else sample - T4CONbits.TON=1;//start timer4 - IFS1bits.T5IF=0;//clear interrupt flag//setup timer and wait - - for(i=0;i>6); //change to pointer for faster use... - while(IFS1bits.T5IF==0); //wait for timer4 (timer 5 interrupt) - IFS1bits.T5IF=0;//clear interrupt flag - } - - CNEN2=0;//change notice off - T4CON=0; //stop count - - for(i=sumpSamples; i>0; i--){ //send back to SUMP, backwards - UART1TX(bpConfig.terminalInput[(i-1)]); - //for(j=0; jLA_SAMPLE_SIZE) sumpSamples=LA_SAMPLE_SIZE; + break; + case SUMP_DIV: + l=sumpRX.command[3]; + l<<=8; + l|=sumpRX.command[2]; + l<<=8; + l|=sumpRX.command[1]; + + //convert from SUMP 100MHz clock to our 16MIPs + //l=((l+1)*16)/100; + l=((l+1)*4)/25; + + //adjust downwards a bit + if(l>0x10) + l-=0x10; + else //fast as possible + l=1; + + //setup PR register + PR5=(l>>16);//most significant word + PR4=l;//least significant word + break; + } + + sumpRXstate=C_IDLE; + break; + } + return 0; +} + +// +// +// To avoid rewriting interrupt vectors with the bootloader, +// this firmware currently uses polling to read the trigger and timer +// A final version should use interrupts after lots of testing. +// +// +unsigned char SUMPlogicService(void){ + static unsigned int i; +// static unsigned char j; + + switch(LAstate){//dump data + case LA_ARMED: //check interrupt flags + if(IFS1bits.CNIF==0){//no flags + if(CNEN2) //if no trigger just continue + break; + } + + //else sample + T4CONbits.TON=1;//start timer4 + IFS1bits.T5IF=0;//clear interrupt flag//setup timer and wait + + for(i=0;i>6); //change to pointer for faster use... + while(IFS1bits.T5IF==0); //wait for timer4 (timer 5 interrupt) + IFS1bits.T5IF=0;//clear interrupt flag + } + + CNEN2=0;//change notice off + T4CON=0; //stop count + + for(i=sumpSamples; i>0; i--){ //send back to SUMP, backwards + UART1TX(bpConfig.terminalInput[(i-1)]); + //for(j=0; j0)&&(speed<=10)) - { modeConfig.speed=speed-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - - if((dbp>0)&&(dbp<=4)) - { uartSettings.dbp=dbp-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - - if((sb>0)&&(sb<=2)) - { uartSettings.sb=sb-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - - if((rxp>0)&&(rxp<=2)) - { uartSettings.rxp=rxp-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - - if((output>0)&&(output<=2)) - { modeConfig.HiZ=(~(output-1)); - } - else - { speed=0; // when speed is 0 we ask the user - } - - if(speed==0) - { cmderror=0; - - - BPMSG1133; - - #if defined(BUSPIRATEV4) - // BPv4 Mode; has custom BAUD entry and auto-baud detection - - modeConfig.speed=getnumber(1,1,11,0)-1; //get user reply - - if(modeConfig.speed==10) - { - modeConfig.speed=8; //Set to 115200 for now - abd=1; //trigger to run baud detection - uartSettings.autoBaudF = 1; - bpWline("Baud detection selected.."); - } - - if(modeConfig.speed==9) - { - BPMSG1248; //say input custom BAUD rate - abd=getlong(115200,1,999999,0); //get the baud rate from user - abd=(((32000000/abd)/8)-1); //calculate BRG - brg=abd; //set BRG - abd=0; //set abd to 0; so 'Auto Baud Detection' routine isnt ran below - //hack hack hakc - U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. - } - - #else - // Normal mode; input BRG and no autobaud detection - modeConfig.speed=getnumber(1,1,10,0)-1; //get user reply - - if(modeConfig.speed==9) - { BPMSG1248; - brg=getnumber(34,1,32767,0); - //hack hack hack - U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. - } - #endif - - - - //bpWstring("Data bits and parity:\x0D\x0A 1. 8, NONE *default \x0D\x0A 2. 8, EVEN \x0D\x0A 3. 8, ODD \x0D\x0A 4. 9, NONE \x0D\x0A"); - //bpWline(OUMSG_UART_DATABITS_PARITY); //write text (data bit and parity) - BPMSG1199; - uartSettings.dbp=getnumber(1,1,4,0)-1; - - //bpWstring("Stop bits:\x0D\x0A 1. 1 *default\x0D\x0A 2. 2 \x0D\x0A"); - //bpWline(OUMSG_UART_STOPBITS); //write text - BPMSG1200; - uartSettings.sb=getnumber(1,1,2,0)-1; - - //string("Receive polarity:\x0D\x0A 1. Idle 1 *default\x0D\x0A 2. Idle 0\x0D\x0A"); - //bpWline(OUMSG_UART_RXPOLARITY); //write text - BPMSG1201; - uartSettings.rxp=getnumber(1,1,2,0)-1; - - //bpWmessage(MSG_OPT_OUTPUT_TYPE); - BPMSG1142; - modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); - - } - else - { - if(modeConfig.speed==9) - { - abd = brg; - abd=(((32000000/abd)/8)-1); //calculate BRG - brg=abd; //set BRG - abd=0; //set abd to 0; so 'Auto Baud Detection' routine isnt ran below - //hack hack hakc - U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. - } - UARTsettings(); - } -} - -void UARTsetup_exc(void) -{ - if(modeConfig.speed==9) - { - //hack hack hack - UART2Setup(U2BRG,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); //U2BRG passed insted of brg, collected in UARTsetup - } - else - { UART2Setup(UART2speed[modeConfig.speed],modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); - } - - if(uartSettings.dbp==3) // 9 bits - { modeConfig.numbits=9; - } - - UART2Enable(); - -#if !defined (BUSPIRATEV4) || defined(BPV4_DEBUG) - //BPv4 does not use U2BRG to communicate with PC unless it is in debug mode - if(U2BRG lower+(upper-lower)/2)?upper:lower; - break; - } - } - return target; -} - -/* -unsigned long UARTgetbaud_EstimatedBaud(unsigned long _abr_) -{ - if((_abr_>=250) && (_abr_<=450)) { - return 300; - } else if ((_abr_>=451)&&(_abr_<=900)) { - return 600; - } else if ((_abr_>=901)&&(_abr_<=1800)) { - return 1200; - } else if ((_abr_>=1801)&&(_abr_<=3500)) { - return 2400; - } else if ((_abr_>=3501)&&(_abr_<=7200)) { - return 4800; - } else if ((_abr_>=7201)&&(_abr_<=12000)) { - return 9600; - } else if ((_abr_>=12001)&&(_abr_<=16800)) { - return 14400; - } else if ((_abr_>=16801)&&(_abr_<=21600)) { - return 19200; - } else if ((_abr_>=21601)&&(_abr_<=31200)) { - return 28800; - } else if ((_abr_>=31201)&&(_abr_<=40800)) { - return 38400; - } else if ((_abr_>=40801)&&(_abr_<=56800)) { - return 56000; - } else if ((_abr_>=56801)&&(_abr_<=86400)) { - return 57600; - } else if ((_abr_>=86401)&&(_abr_<=121600)) { - return 115200; - } else if ((_abr_>=121601)&&(_abr_<=192000)) { - return 128000; - } else if((_abr_>=192001)&&(_abr_<=300000)) { - return 256000; - } else { - return 0; - } -} -*/ - -unsigned long UARTgetbaud(int DataOnly) -{ - unsigned int i=0; - unsigned long CurrentSample=0,BitSample=0; - - // CalculatedBaud define just for readability - #define CalculatedBaud BitSample - - BP_MISO=0; - BP_MISO_DIR=1; - - if(DataOnly==0) { - //bpWline("Awaiting Activity...\n\r(Notice: Any key to exit at this point only...)\n\r"); - BPMSG1280; // -BXRADDED- - } - - - while(BP_MISO==1 && U1STAbits.URXDA==0) { // Wait for activity (stabilize) - asm( "nop" ); // you can exit by hitting any key at this point. - } - - if (U1STAbits.URXDA==1) { // Emergency Exit. - i=U1RXREG; // Get rid of the char from queue - UARTgetbaud_clrTimer(); - //bpWline("\n\r** Early Exit...\n\r"); - BPMSG1281; //-BXRADDED- - return 0; - } - - for(i=0;i<25;i++) { // 25 samples?! Really 5 is good enough. - UARTgetbaud_InitTimer(); // Init the 32bit Timer - - while(BP_MISO==0) { } // Wait until the line goes high == start of activity - - T4CONbits.TON=1; // Start counter. (from Auxpin.c's GetFreq() function.) - T2CONbits.TON=1; // []nil] - - while(BP_MISO==1) { } // The timer is doing the work now. count while high. - - T2CONbits.TON=0; // Stop counter. - T4CONbits.TON=0; // []nil] - - RPINR3bits.T2CKR=0b11111; // Assign T2 clock input to nothing - T4CON=0; // Make sure the counters are off. - T2CON=0; // []nil] - - //j=TMR2; // (1) Get timer/counter values - //k=TMR3HLD; // (2) and format a number with - CurrentSample=TMR3HLD; // (3) the two registers that make - CurrentSample<<=16; // (4) up the 32bit counter. - CurrentSample+=TMR2; // (5) []nil] - - if(i!=0) { // (1) This little jewel is the - if(BitSample==0) { // (2) most important peice of code - BitSample=CurrentSample; // (3) it compares every sample with - } else { // (4) the last and smalest and - if(BitSample>CurrentSample) { // (5) only lets the smallest sample - BitSample=CurrentSample; // (6) through, which should be a single - } // (7) bit. - } // (8) []nil] - } // (9) Note the first sample is kicked. - CurrentSample=0; - } - - UARTgetbaud_clrTimer(); // Disable timer/counter and cleanup. - #define BP_CPU_MIPS 16000000 - CalculatedBaud = (BP_CPU_MIPS/BitSample); - - if(DataOnly==0) { - - if((CalculatedBaud)>150000) - { - //bpWline("\n\rNOTICE! ( Baud >= 256000 )\n\rThe sampled bus has a baud rate to fast for the BP hardware to"); - //bpWline("calculate appropriatly. No estimated baud rate will be supplied. In order"); - //bpWline("to get the baud rate, on this bus, you will need to use a logic analyzer"); - //bpWline("at speeds around 50Mhz+. Open Bench Logic Sniffer reccommended. ;)"); - BPMSG1282; //-BXRADDED- - - } else { - BPMSG1283; //bpWstring("\n\rActual Calculated Baud Rate: \t"); - bpWlongdec(CalculatedBaud); - BPMSG1285; //bpWstring(" bps (Estimated)"); - - BPMSG1284; //bpWstring("\n\rNearest Common Baud Rate: \t"); - bpWlongdec(UARTgetbaud_EstimatedBaud(CalculatedBaud)); - BPMSG1285; //bpWstring(" bps"); - } - - //bpWline("\n\r\n\rEnd of Function. Good bye."); - } - return CalculatedBaud; -} - - -/* -databits and parity (2bits) -1. 8, NONE *default \x0D\x0A 2. 8, EVEN \x0D\x0A 3. 8, ODD \x0D\x0A 4. 9, NONE -Stop bits:\x0D\x0A 1. 1 *default\x0D\x0A 2. 2 \x0D\x0A -Receive polarity:\x0D\x0A 1. Idle 1 *default\x0D\x0A 2. Idle 0\x0D\x0A -output type (hiz or regular -peripheral settings - -# 00000000//reset to BBIO -# 00000001 � mode version string (ART1) -# 00000010 � UART start echo uart RX -# 00000011 � UART stop echo uart RX -# 00000111 - UART speed manual config, 2 bytes (BRGH, BRGL) -# 00001111 - bridge mode (reset to exit) -# 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) -# 0100wxyz � Set peripheral w=power, x=pullups, y=AUX, z=CS -# 0101wxyz � read peripherals -# 0110xxxx - Set speed,0000=300,0001=1200,10=2400,4800,9600,19200,31250, 38400,57600,1010=115200, -# 0111xxxx - Read speed, -# 100wxxyz � config, w=output type, xx=databits and parity, y=stop bits, z=rx polarity (default :00000) -# 101wxxyz � read config -*/ -static unsigned int binUARTspeed[]={13332,3332,1666,832,416,207,127,103,68,34,};//BRG:300,1200,2400,4800,9600,19200,31250,38400,57600,115200 - -void binUARTversionString(void){bpWstring("ART1");} - -void binUART(void){ - static unsigned char inByte, rawCommand,i; - static unsigned int BRGval; - - uartSettings.dbp=0; //startup defaults - uartSettings.sb=0; - uartSettings.rxp=0; - modeConfig.HiZ=1; - BRGval=binUARTspeed[0]; //start at 300bps - uartSettings.eu=0; - UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); - UART2Enable(); - binUARTversionString(); - - while(1){ - - //check for incomming bytes on UART2 - //if echo enabled, send to USB - //else, just clear the buffer - if( UART2RXRdy()){ - if(uartSettings.eu==1){ - UART1TX(UART2RX()); - }else{ - UART2RX();//clear the buffer.... - } - } - if(U2STAbits.OERR) U2STA &= (~0b10); //clear overrun error if exists - - //process commands - if(UART1RXRdy() == 1){//wait for a byte - inByte=UART1RX(); /* JTR usb port; */ //grab it - rawCommand=(inByte>>4);//get command bits in seperate variable - - switch(rawCommand){ - case 0://reset/setup/config commands - switch(inByte){ - case 0://0, reset exit - UART2Disable(); - return; //exit - break; - case 1://reply string - binUARTversionString(); - break; - case 2://00000010 � Show UART input - UART1TX(1); - if(U2STAbits.OERR) U2STA &= (~0b10); //clear overrun error if exists - uartSettings.eu=1; - break; - case 3://00000011 � Don't output UART input - uartSettings.eu=0; - UART1TX(1); - break; - case 7://00000111 - UART speed manual config, 2 bytes (BRGH, BRGL) - UART1TX(1); - UART2Disable(); - BRGval=(unsigned int)(UART1RX()<<8); - UART1TX(1); - BRGval|=UART1RX(); /* JTR usb port; */ - UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); - UART2Enable(); - UART1TX(1); - break; - case 15://00001111 - bridge mode (reset to exit) - UART1TX(1); - U2STA &= (~0b10); //clear overrun error if exists - while(1){//never ending loop, reset Bus Pirate to get out - - #if defined(BUSPIRATEV4) - // This fix suggested by TES on http://dangerousprototypes.com/forum/viewtopic.php?f=28&t=3441 - if((U2STAbits.URXDA==1)){ - UART1TX(U2RXREG); - } - #else - if((U2STAbits.URXDA==1)&& (U1STAbits.UTXBF == 0)){ - U1TXREG = U2RXREG; //URXDA doesn't get cleared untill this happens - } - #endif - if((UART1RXRdy()==1)&& (U2STAbits.UTXBF == 0)){ - U2TXREG = UART1RX(); /* JTR usb port; */; //URXDA doesn't get cleared untill this happens - } - } - default: - UART1TX(0); - break; - } - break; - case 0b0001://get x+1 bytes - inByte&=(~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1);//send 1/OK - - for(i=0;i0b1010) inByte=0b1010; //safe default if out of range - BRGval=binUARTspeed[inByte]; - UART2Disable(); - UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); - UART2Enable(); - UART1TX(1);//send 1/OK - break; - case 0b1000: //set config - case 0b1001: //set config - //100wxxyz � config, w=output type, xx=databits and parity, y=stop bits, z=rx polarity (default :00000) - uartSettings.dbp=0; - uartSettings.sb=0; - uartSettings.rxp=0; - modeConfig.HiZ=0; - if(inByte&0b1000) uartSettings.dbp|=0b10;//set - if(inByte&0b100) uartSettings.dbp|=0b1;//set - if(inByte&0b10) uartSettings.sb=1;//set - if(inByte&0b1) uartSettings.rxp=1;//set - if((inByte&0b10000)==0) modeConfig.HiZ=1;//hiz output if this bit is 1 - UART2Disable(); - UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); - UART2Enable(); - UART1TX(1);//send 1/OK - break; - default: - UART1TX(0x00);//send 0/Error - break; - }//command switch - }//if inbyte - }//while loop -}//function - -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" + +#ifdef BP_USE_HWUART + +#include "uart2io.h" +#include "binIOhelpers.h" + +#include "procMenu.h" // for the userinteraction subs + + +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; + +/* +// move into a .h or other .c??? +int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* +int getint(void); +int getrepeat(void); +void consumewhitechars(void); +extern int cmderror; +*/ + +void UARTgetbaud_InitTimer(void); +void UARTgetbaud_clrTimer(void); +unsigned long UARTgetbaud_EstimatedBaud(unsigned long _abr_); +unsigned long UARTgetbaud(int DataOnly); + + +struct _UART{ + unsigned char dbp:2; //databits and parity + unsigned char sb:1;//stop bits + unsigned char rxp:1;//receive polarity + unsigned char eu:1;//echo uart +#if defined(BUSPIRATEV4) + unsigned char autoBaudF : 1; //autoBaud on or off, only on BPv4 +#endif +} uartSettings; + +void UARTsetup_exc(void); + +static unsigned int UART2speed[]={13332,3332,1666,832,416,207,103,68,34,127};//BRG:300,1200,2400,4800,9600,19200,38400,57600,115200, 31250, + +unsigned int UARTread(void) +{ unsigned int c; + if(UART2RXRdy()) + { if(U2STAbits.PERR) BPMSG1194; //bpWstring("-p "); //show any errors + if(U2STAbits.FERR) BPMSG1195; //bpWstring("-f "); + c=UART2RX(); + + if(U2STAbits.OERR) + { //bpWstring("*Bytes dropped*"); + BPMSG1196; + U2STA &= (~0b10); //clear overrun error if exists + } + return c; + } + else + { //bpWline(OUMSG_UART_READ_FAIL); + BPMSG1197; + } + return 0; +} + +unsigned int UARTwrite(unsigned int c) +{ UART2TX(c); //send byte + return 0; +} + +// todo: read from cmdline for now it is ok + +void UARTsettings(void) +{ //bpWstring("UART (spd dbp sb rxp hiz)=( "); + BPMSG1202; + bpWdec(modeConfig.speed); bpSP; + if(modeConfig.speed==9) + { bpWintdec(U2BRG); bpSP; + } + else + { bpWintdec(UART2speed[modeConfig.speed]); bpSP; + } + bpWdec(uartSettings.dbp); bpSP; + bpWdec(uartSettings.sb); bpSP; + bpWdec(uartSettings.rxp); bpSP; + bpWdec(modeConfig.HiZ); bpSP; + //bpWline(")\r\n"); + BPMSG1162; +} + + +void UARTsetup(void) +{ int speed, dbp, sb, rxp, output, brg=0; + //autobaud detection; multi uses + unsigned long abd=0; + + #if defined(BUSPIRATEV4) + uartSettings.autoBaudF = 0; //start with autobaud off. + //#define DetectedBaud abd + //#define CalculatedBRG abd + #endif + + consumewhitechars(); + speed=getint(); + if(speed==10) //weird this is totaly ignored later as the Speed == 0 check later skips the calculation I added it below.. + { consumewhitechars(); + brg=getint(); + } + consumewhitechars(); + dbp=getint(); + consumewhitechars(); + sb=getint(); + consumewhitechars(); + rxp=getint(); + consumewhitechars(); + output=getint(); + + if((speed>0)&&(speed<=10)) + { modeConfig.speed=speed-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + + if((dbp>0)&&(dbp<=4)) + { uartSettings.dbp=dbp-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + + if((sb>0)&&(sb<=2)) + { uartSettings.sb=sb-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + + if((rxp>0)&&(rxp<=2)) + { uartSettings.rxp=rxp-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + + if((output>0)&&(output<=2)) + { modeConfig.HiZ=(~(output-1)); + } + else + { speed=0; // when speed is 0 we ask the user + } + + if(speed==0) + { cmderror=0; + + + BPMSG1133; + + #if defined(BUSPIRATEV4) + // BPv4 Mode; has custom BAUD entry and auto-baud detection + + modeConfig.speed=getnumber(1,1,11,0)-1; //get user reply + + if(modeConfig.speed==10) + { + modeConfig.speed=8; //Set to 115200 for now + abd=1; //trigger to run baud detection + uartSettings.autoBaudF = 1; + bpWline("Baud detection selected.."); + } + + if(modeConfig.speed==9) + { + BPMSG1248; //say input custom BAUD rate + abd=getlong(115200,1,999999,0); //get the baud rate from user + abd=(((32000000/abd)/8)-1); //calculate BRG + brg=abd; //set BRG + abd=0; //set abd to 0; so 'Auto Baud Detection' routine isnt ran below + //hack hack hakc + U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. + } + + #else + // Normal mode; input BRG and no autobaud detection + modeConfig.speed=getnumber(1,1,10,0)-1; //get user reply + + if(modeConfig.speed==9) + { BPMSG1248; + brg=getnumber(34,1,32767,0); + //hack hack hack + U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. + } + #endif + + + + //bpWstring("Data bits and parity:\x0D\x0A 1. 8, NONE *default \x0D\x0A 2. 8, EVEN \x0D\x0A 3. 8, ODD \x0D\x0A 4. 9, NONE \x0D\x0A"); + //bpWline(OUMSG_UART_DATABITS_PARITY); //write text (data bit and parity) + BPMSG1199; + uartSettings.dbp=getnumber(1,1,4,0)-1; + + //bpWstring("Stop bits:\x0D\x0A 1. 1 *default\x0D\x0A 2. 2 \x0D\x0A"); + //bpWline(OUMSG_UART_STOPBITS); //write text + BPMSG1200; + uartSettings.sb=getnumber(1,1,2,0)-1; + + //string("Receive polarity:\x0D\x0A 1. Idle 1 *default\x0D\x0A 2. Idle 0\x0D\x0A"); + //bpWline(OUMSG_UART_RXPOLARITY); //write text + BPMSG1201; + uartSettings.rxp=getnumber(1,1,2,0)-1; + + //bpWmessage(MSG_OPT_OUTPUT_TYPE); + BPMSG1142; + modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); + + } + else + { + if(modeConfig.speed==9) + { + abd = brg; + abd=(((32000000/abd)/8)-1); //calculate BRG + brg=abd; //set BRG + abd=0; //set abd to 0; so 'Auto Baud Detection' routine isnt ran below + //hack hack hakc + U2BRG = brg; //passing the brg variable to U2BRG so the UARTsetup_exc can use it to start UART2setup.. + } + UARTsettings(); + } +} + +void UARTsetup_exc(void) +{ + if(modeConfig.speed==9) + { + //hack hack hack + UART2Setup(U2BRG,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); //U2BRG passed insted of brg, collected in UARTsetup + } + else + { UART2Setup(UART2speed[modeConfig.speed],modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); + } + + if(uartSettings.dbp==3) // 9 bits + { modeConfig.numbits=9; + } + + UART2Enable(); + +#if !defined (BUSPIRATEV4) || defined(BPV4_DEBUG) + //BPv4 does not use U2BRG to communicate with PC unless it is in debug mode + if(U2BRG lower+(upper-lower)/2)?upper:lower; + break; + } + } + return target; +} + +/* +unsigned long UARTgetbaud_EstimatedBaud(unsigned long _abr_) +{ + if((_abr_>=250) && (_abr_<=450)) { + return 300; + } else if ((_abr_>=451)&&(_abr_<=900)) { + return 600; + } else if ((_abr_>=901)&&(_abr_<=1800)) { + return 1200; + } else if ((_abr_>=1801)&&(_abr_<=3500)) { + return 2400; + } else if ((_abr_>=3501)&&(_abr_<=7200)) { + return 4800; + } else if ((_abr_>=7201)&&(_abr_<=12000)) { + return 9600; + } else if ((_abr_>=12001)&&(_abr_<=16800)) { + return 14400; + } else if ((_abr_>=16801)&&(_abr_<=21600)) { + return 19200; + } else if ((_abr_>=21601)&&(_abr_<=31200)) { + return 28800; + } else if ((_abr_>=31201)&&(_abr_<=40800)) { + return 38400; + } else if ((_abr_>=40801)&&(_abr_<=56800)) { + return 56000; + } else if ((_abr_>=56801)&&(_abr_<=86400)) { + return 57600; + } else if ((_abr_>=86401)&&(_abr_<=121600)) { + return 115200; + } else if ((_abr_>=121601)&&(_abr_<=192000)) { + return 128000; + } else if((_abr_>=192001)&&(_abr_<=300000)) { + return 256000; + } else { + return 0; + } +} +*/ + +unsigned long UARTgetbaud(int DataOnly) +{ + unsigned int i=0; + unsigned long CurrentSample=0,BitSample=0; + + // CalculatedBaud define just for readability + #define CalculatedBaud BitSample + + BP_MISO=0; + BP_MISO_DIR=1; + + if(DataOnly==0) { + //bpWline("Awaiting Activity...\n\r(Notice: Any key to exit at this point only...)\n\r"); + BPMSG1280; // -BXRADDED- + } + + + while(BP_MISO==1 && U1STAbits.URXDA==0) { // Wait for activity (stabilize) + asm( "nop" ); // you can exit by hitting any key at this point. + } + + if (U1STAbits.URXDA==1) { // Emergency Exit. + i=U1RXREG; // Get rid of the char from queue + UARTgetbaud_clrTimer(); + //bpWline("\n\r** Early Exit...\n\r"); + BPMSG1281; //-BXRADDED- + return 0; + } + + for(i=0;i<25;i++) { // 25 samples?! Really 5 is good enough. + UARTgetbaud_InitTimer(); // Init the 32bit Timer + + while(BP_MISO==0) { } // Wait until the line goes high == start of activity + + T4CONbits.TON=1; // Start counter. (from Auxpin.c's GetFreq() function.) + T2CONbits.TON=1; // []nil] + + while(BP_MISO==1) { } // The timer is doing the work now. count while high. + + T2CONbits.TON=0; // Stop counter. + T4CONbits.TON=0; // []nil] + + RPINR3bits.T2CKR=0b11111; // Assign T2 clock input to nothing + T4CON=0; // Make sure the counters are off. + T2CON=0; // []nil] + + //j=TMR2; // (1) Get timer/counter values + //k=TMR3HLD; // (2) and format a number with + CurrentSample=TMR3HLD; // (3) the two registers that make + CurrentSample<<=16; // (4) up the 32bit counter. + CurrentSample+=TMR2; // (5) []nil] + + if(i!=0) { // (1) This little jewel is the + if(BitSample==0) { // (2) most important peice of code + BitSample=CurrentSample; // (3) it compares every sample with + } else { // (4) the last and smalest and + if(BitSample>CurrentSample) { // (5) only lets the smallest sample + BitSample=CurrentSample; // (6) through, which should be a single + } // (7) bit. + } // (8) []nil] + } // (9) Note the first sample is kicked. + CurrentSample=0; + } + + UARTgetbaud_clrTimer(); // Disable timer/counter and cleanup. + #define BP_CPU_MIPS 16000000 + CalculatedBaud = (BP_CPU_MIPS/BitSample); + + if(DataOnly==0) { + + if((CalculatedBaud)>150000) + { + //bpWline("\n\rNOTICE! ( Baud >= 256000 )\n\rThe sampled bus has a baud rate to fast for the BP hardware to"); + //bpWline("calculate appropriatly. No estimated baud rate will be supplied. In order"); + //bpWline("to get the baud rate, on this bus, you will need to use a logic analyzer"); + //bpWline("at speeds around 50Mhz+. Open Bench Logic Sniffer reccommended. ;)"); + BPMSG1282; //-BXRADDED- + + } else { + BPMSG1283; //bpWstring("\n\rActual Calculated Baud Rate: \t"); + bpWlongdec(CalculatedBaud); + BPMSG1285; //bpWstring(" bps (Estimated)"); + + BPMSG1284; //bpWstring("\n\rNearest Common Baud Rate: \t"); + bpWlongdec(UARTgetbaud_EstimatedBaud(CalculatedBaud)); + BPMSG1285; //bpWstring(" bps"); + } + + //bpWline("\n\r\n\rEnd of Function. Good bye."); + } + return CalculatedBaud; +} + + +/* +databits and parity (2bits) +1. 8, NONE *default \x0D\x0A 2. 8, EVEN \x0D\x0A 3. 8, ODD \x0D\x0A 4. 9, NONE +Stop bits:\x0D\x0A 1. 1 *default\x0D\x0A 2. 2 \x0D\x0A +Receive polarity:\x0D\x0A 1. Idle 1 *default\x0D\x0A 2. Idle 0\x0D\x0A +output type (hiz or regular +peripheral settings + +# 00000000//reset to BBIO +# 00000001 � mode version string (ART1) +# 00000010 � UART start echo uart RX +# 00000011 � UART stop echo uart RX +# 00000111 - UART speed manual config, 2 bytes (BRGH, BRGL) +# 00001111 - bridge mode (reset to exit) +# 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) +# 0100wxyz � Set peripheral w=power, x=pullups, y=AUX, z=CS +# 0101wxyz � read peripherals +# 0110xxxx - Set speed,0000=300,0001=1200,10=2400,4800,9600,19200,31250, 38400,57600,1010=115200, +# 0111xxxx - Read speed, +# 100wxxyz � config, w=output type, xx=databits and parity, y=stop bits, z=rx polarity (default :00000) +# 101wxxyz � read config +*/ +static unsigned int binUARTspeed[]={13332,3332,1666,832,416,207,127,103,68,34,};//BRG:300,1200,2400,4800,9600,19200,31250,38400,57600,115200 + +void binUARTversionString(void){bpWstring("ART1");} + +void binUART(void){ + static unsigned char inByte, rawCommand,i; + static unsigned int BRGval; + + uartSettings.dbp=0; //startup defaults + uartSettings.sb=0; + uartSettings.rxp=0; + modeConfig.HiZ=1; + BRGval=binUARTspeed[0]; //start at 300bps + uartSettings.eu=0; + UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); + UART2Enable(); + binUARTversionString(); + + while(1){ + + //check for incomming bytes on UART2 + //if echo enabled, send to USB + //else, just clear the buffer + if( UART2RXRdy()){ + if(uartSettings.eu==1){ + UART1TX(UART2RX()); + }else{ + UART2RX();//clear the buffer.... + } + } + if(U2STAbits.OERR) U2STA &= (~0b10); //clear overrun error if exists + + //process commands + if(UART1RXRdy() == 1){//wait for a byte + inByte=UART1RX(); /* JTR usb port; */ //grab it + rawCommand=(inByte>>4);//get command bits in seperate variable + + switch(rawCommand){ + case 0://reset/setup/config commands + switch(inByte){ + case 0://0, reset exit + UART2Disable(); + return; //exit + break; + case 1://reply string + binUARTversionString(); + break; + case 2://00000010 � Show UART input + UART1TX(1); + if(U2STAbits.OERR) U2STA &= (~0b10); //clear overrun error if exists + uartSettings.eu=1; + break; + case 3://00000011 � Don't output UART input + uartSettings.eu=0; + UART1TX(1); + break; + case 7://00000111 - UART speed manual config, 2 bytes (BRGH, BRGL) + UART1TX(1); + UART2Disable(); + BRGval=(unsigned int)(UART1RX()<<8); + UART1TX(1); + BRGval|=UART1RX(); /* JTR usb port; */ + UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); + UART2Enable(); + UART1TX(1); + break; + case 15://00001111 - bridge mode (reset to exit) + UART1TX(1); + U2STA &= (~0b10); //clear overrun error if exists + while(1){//never ending loop, reset Bus Pirate to get out + + #if defined(BUSPIRATEV4) + // This fix suggested by TES on http://dangerousprototypes.com/forum/viewtopic.php?f=28&t=3441 + if((U2STAbits.URXDA==1)){ + UART1TX(U2RXREG); + } + #else + if((U2STAbits.URXDA==1)&& (U1STAbits.UTXBF == 0)){ + U1TXREG = U2RXREG; //URXDA doesn't get cleared untill this happens + } + #endif + if((UART1RXRdy()==1)&& (U2STAbits.UTXBF == 0)){ + U2TXREG = UART1RX(); /* JTR usb port; */; //URXDA doesn't get cleared untill this happens + } + } + default: + UART1TX(0); + break; + } + break; + case 0b0001://get x+1 bytes + inByte&=(~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1);//send 1/OK + + for(i=0;i0b1010) inByte=0b1010; //safe default if out of range + BRGval=binUARTspeed[inByte]; + UART2Disable(); + UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); + UART2Enable(); + UART1TX(1);//send 1/OK + break; + case 0b1000: //set config + case 0b1001: //set config + //100wxxyz � config, w=output type, xx=databits and parity, y=stop bits, z=rx polarity (default :00000) + uartSettings.dbp=0; + uartSettings.sb=0; + uartSettings.rxp=0; + modeConfig.HiZ=0; + if(inByte&0b1000) uartSettings.dbp|=0b10;//set + if(inByte&0b100) uartSettings.dbp|=0b1;//set + if(inByte&0b10) uartSettings.sb=1;//set + if(inByte&0b1) uartSettings.rxp=1;//set + if((inByte&0b10000)==0) modeConfig.HiZ=1;//hiz output if this bit is 1 + UART2Disable(); + UART2Setup(BRGval,modeConfig.HiZ, uartSettings.rxp, uartSettings.dbp, uartSettings.sb ); + UART2Enable(); + UART1TX(1);//send 1/OK + break; + default: + UART1TX(0x00);//send 0/Error + break; + }//command switch + }//if inbyte + }//while loop +}//function + +#endif diff --git a/Firmware/UART.h b/Firmware/UART.h index fead1146..39245949 100644 --- a/Firmware/UART.h +++ b/Firmware/UART.h @@ -1,30 +1,30 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -void uartProcess(void); -void binUART(void); - -unsigned UARTwrite(unsigned int c); -unsigned int UARTread(void); -void UARTsetup(void); -void UARTsetup_exc(void); -void UARTcleanup(void); -void UARTmacro(unsigned int c); -void UARTstart(void); -void UARTstop(void); -unsigned int UARTperiodic(void); -void UARTpins(void); -void UARTsettings(void); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +void uartProcess(void); +void binUART(void); + +unsigned UARTwrite(unsigned int c); +unsigned int UARTread(void); +void UARTsetup(void); +void UARTsetup_exc(void); +void UARTcleanup(void); +void UARTmacro(unsigned int c); +void UARTstart(void); +void UARTstop(void); +unsigned int UARTperiodic(void); +void UARTpins(void); +void UARTsettings(void); + diff --git a/Firmware/base.c b/Firmware/base.c index 38b71601..57a992a5 100644 --- a/Firmware/base.c +++ b/Firmware/base.c @@ -1,229 +1,229 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" -#include "busPirateCore.h" - -extern struct _bpConfig bpConfig; //global config variables -extern struct _modeConfig modeConfig; //mode config variables - -void clearModeConfig(void); //private function to clear modeConfig on reset - -//private function to clear modeConfig on reset -void clearModeConfig(void){ - //reset the options structure here... - modeConfig.HiZ=0; - modeConfig.speed=0; - modeConfig.periodicService=0; - modeConfig.altAUX=0; - modeConfig.lsbEN=0; -} - -//get bus pirate ready after first start -//or clean up after any dirty modules -//modules MUST teardown any PPS functions on the CMD_CLEANUP command -void bpInit(void){ - //do this manually so there aren't problems with hardware - //that has I/O on different ports - BP_MOSI_DIR=1; - BP_CLK_DIR=1; - BP_MISO_DIR=1; - BP_CS_DIR=1; - BP_AUX0_DIR=1; - BP_LEDMODE=0; //mode LED OFF - BP_LEDMODE_DIR=0; - #ifdef BUSPIRATEV4 - BP_USBLED_OFF(); - BPV4_HWI2CPINS_SETUP(); - BP_BUTTON_SETUP(); - BP_3V3PU_OFF(); - //BP_5VPU_OFF(); - BP_EEPROM_SETUP(); - #endif - #ifdef BUSPIRATEV1A - BP_PIN6_DIR=1; //AUX2... unused - BP_LEDPWR=1; //light power led - BP_LEDPWR_DIR=0; - #endif - - BP_AUX_RPOUT = 0; //remove output from AUX pin (PWM/servo modes) - - bpConfig.busMode=HIZ; - - clearModeConfig(); //reset the mode settings structure - - #ifndef BUSPIRATEV1A - BP_PULLUP_OFF(); - #endif - - BP_VREG_OFF();//disable the VREG - - //setup voltage monitoring on ADC. see hardwarevx.h! - BP_ADC_PINSETUP(); - - //configure the ADC - AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - AD1CSSL = 0; - AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - AD1CON2 = 0; -} - -//take an ADC measurement on the specified channel -unsigned int bpADC(unsigned char c){ - AD1CHS=c;//set channel - AD1CON1bits.SAMP=1;//start sample - AD1CON1bits.DONE=0;//clear done (not really needed) - while(AD1CON1bits.DONE==0);//wait for conversion to finish - return ADC1BUF0; -} - -//takes a measurement from the ADC probe and prints the result to the user terminal -void bpADCprobe(void){ - AD1CON1bits.ADON = 1; // turn ADC ON - bpWvolts(bpADC(BP_ADC_PROBE)); //print measurement - AD1CON1bits.ADON = 0; // turn ADC OFF -} - -// measure constantly -void bpADCCprobe(void) -{ unsigned int temp; - - //bpWline(OUMSG_PS_ADCC); - BPMSG1042; - //bpWline(OUMSG_PS_ANY_KEY); - BPMSG1250; //BPMSG1043; - //bpWstring(OUMSG_PS_ADC_VOLT_PROBE); - BPMSG1044; - bpWvolts(0); // print dummy (0v) - //bpWstring(OUMSG_PS_ADC_VOLTS); - BPMSG1045; - while(!UART1RXRdy()) // wait for keypress - { AD1CON1bits.ADON = 1; // turn ADC ON - temp=bpADC(BP_ADC_PROBE); - AD1CON1bits.ADON = 0; // turn ADC OFF - bpWstring("\x08\x08\x08\x08\x08"); // 5x backspace ( e.g. 5.00V ) - //BPMSG1046; - bpWvolts(temp); // print measurement - //bpWstring(OUMSG_PS_ADC_VOLTS); - BPMSG1045; - - // CvD: wait xx ms?? - } - UART1RX(); - bpWline(""); // need a linefeed :D -} - - -//print byte c to the user terminal in the format -// specified by the bpConfig.displayMode setting -void bpWbyte(unsigned int c) -{ if(modeConfig.numbits<16) - { c&=(0x7FFF>>((16-modeConfig.numbits)-1)); - } - switch(bpConfig.displayMode){ - case HEX: - if(modeConfig.int16) bpWinthex(c); else bpWhex(c); - break; - case DEC: - if(modeConfig.int16) bpWintdec(c); else bpWdec(c); - break; - case BIN: - if(modeConfig.int16) - { bpWbin(c); bpSP; - } - bpWbin(c); - break; - case RAW: - if(modeConfig.int16) - { UART1TX(c>>8); - } - UART1TX(c&0x0FF); - break; - } -} - -//delay in MS -//void bpDelayMS(const unsigned char delay) -void bpDelayMS(int delay) -{ int i, c; - - for(c=0;c>=(8-modeConfig.numbits); - } - return r; -} -*/ - -unsigned int bpRevByte(unsigned int c) -{ unsigned int r=0, i; - - for(i=0b1; i!=0; i=i<<1) - { r=r<<1; - if(c&i)r|=0b1; - } - - if(modeConfig.numbits!=16) - { r>>=(16-modeConfig.numbits); - } - return r; -} - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "base.h" +#include "busPirateCore.h" + +extern struct _bpConfig bpConfig; //global config variables +extern struct _modeConfig modeConfig; //mode config variables + +void clearModeConfig(void); //private function to clear modeConfig on reset + +//private function to clear modeConfig on reset +void clearModeConfig(void){ + //reset the options structure here... + modeConfig.HiZ=0; + modeConfig.speed=0; + modeConfig.periodicService=0; + modeConfig.altAUX=0; + modeConfig.lsbEN=0; +} + +//get bus pirate ready after first start +//or clean up after any dirty modules +//modules MUST teardown any PPS functions on the CMD_CLEANUP command +void bpInit(void){ + //do this manually so there aren't problems with hardware + //that has I/O on different ports + BP_MOSI_DIR=1; + BP_CLK_DIR=1; + BP_MISO_DIR=1; + BP_CS_DIR=1; + BP_AUX0_DIR=1; + BP_LEDMODE=0; //mode LED OFF + BP_LEDMODE_DIR=0; + #ifdef BUSPIRATEV4 + BP_USBLED_OFF(); + BPV4_HWI2CPINS_SETUP(); + BP_BUTTON_SETUP(); + BP_3V3PU_OFF(); + //BP_5VPU_OFF(); + BP_EEPROM_SETUP(); + #endif + #ifdef BUSPIRATEV1A + BP_PIN6_DIR=1; //AUX2... unused + BP_LEDPWR=1; //light power led + BP_LEDPWR_DIR=0; + #endif + + BP_AUX_RPOUT = 0; //remove output from AUX pin (PWM/servo modes) + + bpConfig.busMode=HIZ; + + clearModeConfig(); //reset the mode settings structure + + #ifndef BUSPIRATEV1A + BP_PULLUP_OFF(); + #endif + + BP_VREG_OFF();//disable the VREG + + //setup voltage monitoring on ADC. see hardwarevx.h! + BP_ADC_PINSETUP(); + + //configure the ADC + AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + AD1CSSL = 0; + AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + AD1CON2 = 0; +} + +//take an ADC measurement on the specified channel +unsigned int bpADC(unsigned char c){ + AD1CHS=c;//set channel + AD1CON1bits.SAMP=1;//start sample + AD1CON1bits.DONE=0;//clear done (not really needed) + while(AD1CON1bits.DONE==0);//wait for conversion to finish + return ADC1BUF0; +} + +//takes a measurement from the ADC probe and prints the result to the user terminal +void bpADCprobe(void){ + AD1CON1bits.ADON = 1; // turn ADC ON + bpWvolts(bpADC(BP_ADC_PROBE)); //print measurement + AD1CON1bits.ADON = 0; // turn ADC OFF +} + +// measure constantly +void bpADCCprobe(void) +{ unsigned int temp; + + //bpWline(OUMSG_PS_ADCC); + BPMSG1042; + //bpWline(OUMSG_PS_ANY_KEY); + BPMSG1250; //BPMSG1043; + //bpWstring(OUMSG_PS_ADC_VOLT_PROBE); + BPMSG1044; + bpWvolts(0); // print dummy (0v) + //bpWstring(OUMSG_PS_ADC_VOLTS); + BPMSG1045; + while(!UART1RXRdy()) // wait for keypress + { AD1CON1bits.ADON = 1; // turn ADC ON + temp=bpADC(BP_ADC_PROBE); + AD1CON1bits.ADON = 0; // turn ADC OFF + bpWstring("\x08\x08\x08\x08\x08"); // 5x backspace ( e.g. 5.00V ) + //BPMSG1046; + bpWvolts(temp); // print measurement + //bpWstring(OUMSG_PS_ADC_VOLTS); + BPMSG1045; + + // CvD: wait xx ms?? + } + UART1RX(); + bpWline(""); // need a linefeed :D +} + + +//print byte c to the user terminal in the format +// specified by the bpConfig.displayMode setting +void bpWbyte(unsigned int c) +{ if(modeConfig.numbits<16) + { c&=(0x7FFF>>((16-modeConfig.numbits)-1)); + } + switch(bpConfig.displayMode){ + case HEX: + if(modeConfig.int16) bpWinthex(c); else bpWhex(c); + break; + case DEC: + if(modeConfig.int16) bpWintdec(c); else bpWdec(c); + break; + case BIN: + if(modeConfig.int16) + { bpWbin(c); bpSP; + } + bpWbin(c); + break; + case RAW: + if(modeConfig.int16) + { UART1TX(c>>8); + } + UART1TX(c&0x0FF); + break; + } +} + +//delay in MS +//void bpDelayMS(const unsigned char delay) +void bpDelayMS(int delay) +{ int i, c; + + for(c=0;c>=(8-modeConfig.numbits); + } + return r; +} +*/ + +unsigned int bpRevByte(unsigned int c) +{ unsigned int r=0, i; + + for(i=0b1; i!=0; i=i<<1) + { r=r<<1; + if(c&i)r|=0b1; + } + + if(modeConfig.numbits!=16) + { r>>=(16-modeConfig.numbits); + } + return r; +} + + diff --git a/Firmware/base.h b/Firmware/base.h index 52c63b9e..b1b656b0 100644 --- a/Firmware/base.h +++ b/Firmware/base.h @@ -1,297 +1,297 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#ifndef BASE -#define BASE -#include -#include -#include -#include - -//typedef unsigned char u8; -//typedef unsigned int u16; -//typedef unsigned long u32; -//typedef unsigned char BYTE; - -#define FALSE 0 -#define TRUE (!FALSE) - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -////////////////////////////////////// [ BUS PIRATE SETTINGS ] //////////////////////////////////////////////////// -/////////// NOTE: -/////////// Configuration and Settings for building the Bus Pirate firmware. For more information and support -/////////// On building your own firmware. Please visit the forums on DangerousPrototypes.com. Enjoy the BP :) -/////////// -/////////// Bus Pirate - Brought to you by DangerousPrototypes.com - -/////////////////////////////////////// -// HARDWARE VERSION -#if defined(__PIC24FJ256GB106__) //v4 chip - #define BUSPIRATEV4 -#elif defined(__PIC24FJ64GA002__) //v3/v2go/v1a chip - // Uncomment the hardware version you are building for - #define BUSPIRATEV3 // V3 is also V2G0 - //#define BUSPIRATEV1A //probably no longer supported... -#endif - -//////////////////////////////////////// -// FIRMWARE VERSION STRING -// Build Firmware Version String -// used in 'i' and startup -#define BP_FIRMWARE_STRING "Firmware v6.3-beta1 r2151 " - - -//////////////////////////////////////// -// LANGUAGE -// Select language (US AND DE Currently Supported) -// If you know anyone bi-le -#define LANGUAGE_EN_US -//#define LANGUAGE_DE_DE -//#define LANGUAGE_IT_IT //Not yet supported -//#define LANGUAGE_ES_ES //Not yet supported - - -//////////////////////////////////////// -// MODE SELECTION -// Default Mode Selection -// BP_MAIN is the default mode setting; varys by hardware version -// BP_ADDONS is for special builds with special modes -// BP_CUSTOM is for selecting your own modes below (find: BP_CUSTOM) and uncomment wanted modes -#define BP_MAIN -//#define BP_ADDONS -//#define BP_CUSTOM - - -//////////////////////////////////////// -// OTHER CONFIGS -// BP_ENABLE_HISTORY = Enable the 'h' command for whatever reason it was disabled this enables it. can be disabled again. -// These settings should be left within the BUSPIRATEV4 define due to lack of space on v3. Unless you disable a mode or two -// for a custom v3 build, only include the following options on a v4 build. -#if defined(BUSPIRATEV4) -// Enables the 'h' command to show history -#define BP_ENABLE_HISTORY - -#endif - -/////////////////////// -///////////////////////////// [ END OF CONFIGURATION ]////////////////// -//////////////////////////////////////////////////////////// -///////////////////////////////////// - - -// Buspirate version 3 and v2go use the same everything; so this just -// fixs if the user selected v2go. -#if defined(BUSPIRATEV2GO) - #define BUSPIRATEV3 -#endif - - -#ifdef BUSPIRATEV4 - #include "hardwarev4a.h" -#include "dp_usb/usb_stack_globals.h" - #include "onboardEEPROM.h" -#endif - -#if defined(BUSPIRATEV1A) - #include "hardwarev1a.h" -#elif defined(BUSPIRATEV3) - #define BUSPIRATEV2 //v25 (2go) and v3 are about the same, enable the same featue set for both - #include "hardwarev3.h" -#elif defined(BUSPIRATEV4) - -#else -#error "No hardware defined in base.h." -#endif - -#include "baseIO.h" -#include "baseUI.h" - -//include/exclude modules here -// standard protocols -//#define BP_USE_RAW2WIRE -//#define BP_USE_RAW3WIRE - -//Debugging mode for BPv4, comment out for normal compiling -//Adds alternative communicaton to UART1 over AUX1 and AUX2 instead of USB. -//#define BPV4_DEBUG - - -#if defined(BP_MAIN) - - #define BP_USE_1WIRE - //#ifndef BUSPIRATEV4 - #define BP_USE_HWUART //hardware uart (now also MIDI) - //#endif - #define BP_USE_I2C - //#define BP_USE_I2C_HW - #define BP_USE_HWSPI //hardware spi - #define BP_USE_RAW2WIRE - #define BP_USE_RAW3WIRE - #define BP_USE_LCD // include HD44780 LCD library - #define BP_USE_BASIC - - #if defined(BUSPIRATEV4) - #define BP_USE_DIO //binary mode - #define BP_USE_PCATKB - #define BP_USE_PIC - #endif - -#elif defined(BP_ADDONS) - // most used protos - //#define BP_USE_1WIRE - //#define BP_USE_HWUART //hardware uart (now also MIDI) - //#define BP_USE_I2C - //#define BP_USE_I2C_HW - //#define BP_USE_HWSPI //hardware spi - #define BP_USE_RAW2WIRE - #define BP_USE_RAW3WIRE - #define BP_USE_PCATKB - #define BP_USE_LCD // include HD44780 LCD library - #define BP_USE_PIC - #define BP_USE_DIO //binary mode - -#elif defined(BP_CUSTOM) - // most used protos - //#define BP_USE_1WIRE - #define BP_USE_HWUART //hardware uart (now also MIDI) - //#define BP_USE_I2C - //#define BP_USE_I2C_HW - //#define BP_USE_HWSPI //hardware spi - //#define BP_USE_RAW2WIRE - //#define BP_USE_RAW3WIRE - //#define BP_USE_PCATKB - //#define BP_USE_LCD // include HD44780 LCD library - //#define BP_USE_PIC - //#define BP_USE_DIO //binary mode - -#else -#error "No Bus Pirate configuration defined." -#endif - -//ENABLE BASIC SCRIPTING -#if defined(BUSPIRATEV4) - #define BP_USE_BASIC // basic subsystem - //#define BP_USE_BASICI2C // use an i2ceeprom for storing -#endif - - -// only 1 should be uncommented -//#define BASICTEST -//#define BASICTEST_I2C -//#define BASICTEST_UART -//#define BASICTEST_R3W -//#define BASICTEST_PIC10 // program blink a led -//#define BASICTEST_PIC10_2 // read whole pic - -//sets the address in the bootloader to jump to on the bootloader command -//must be defined in asm -asm (".equ BLJUMPADDRESS, 0xABF8"); - -// -// -// -// END CONFIG AREA -// -// -// - -//// left out for now -/* my italian and spanish aint that good ;) -#elif defined(LANGUAGE_IT_IT) - #include "translations/it-IT.h" -// #include "translations/en-US.h" -#elif defined(LANGUAGE_ES_ES) - #include "translations/es-ES.h" - */ - - -// If BUSPIRATEv4 then use the en_US.h otherwise -// use the v3 one. The version3 one does not include -// the modes that the v3 cannot fit. This way we dont -// waste space on strings we wont see. -// -// Also note; the BPV4 project file uses the en_US.s file -// the buspurate v3 uses the other one... -#if defined(BUSPIRATEV4) - #if defined(LANGUAGE_EN_US) - #include "translations/BPv4_en_US.h" - #elif defined(LANGUAGE_DE_DE) - #include "translations/BPv4_de_DE.h" - #else - #error "No language defined in base.h." - #endif -#else - #if defined(LANGUAGE_EN_US) - #include "translations/BPv3_en_US.h" - #elif defined(LANGUAGE_DE_DE) - #include "translations/BPv3_de_DE.h" - #else - #error "No language defined in base.h." - #endif -#endif - - - - - - -//these settings are destroyed between modes. -//this is used to pass stuff from protocol libraries to BP -//for example, allowpullup determines if the P menu is available -//TO DO: add global scratch buffer to setting array for use in -// 1-Wire enumeration, JTAG input buffer, etc... - -struct _modeConfig { - unsigned char speed; - unsigned char numbits; - unsigned char buf[16]; - unsigned char altAUX : 2; // there are 4 AUX on BUSPIRATEV4 - unsigned char periodicService : 1; - unsigned char lsbEN : 1; - unsigned char HiZ : 1; - unsigned char int16 : 1; // 16 bits output? - unsigned char wwr : 1; // write with read -}; - -struct _command { - unsigned char cmd; - unsigned int num; - unsigned int repeat; -}; - -unsigned int bpRevByte(unsigned int c); - -//reset all peripherals before configuring a new bus protocol -void bpInit(void); - -//take an ADC measurement on channel c -unsigned int bpADC(unsigned char c); -//takes a measurement from the ADC probe and prints the result to the user terminal -void bpADCprobe(void); -void bpADCCprobe(void); - -//print byte c to the user terminal in the format -// specified by the bpConfig.displayMode setting -void bpWbyte(unsigned int c); - -//delays used by many libraries -//void bpDelayMS(const unsigned char delay); -//void bpDelayUS(const unsigned char delay); -void bpDelayMS(int delay); -void bpDelayUS(int delay); -#endif - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#ifndef BASE +#define BASE +#include +#include +#include +#include + +//typedef unsigned char u8; +//typedef unsigned int u16; +//typedef unsigned long u32; +//typedef unsigned char BYTE; + +#define FALSE 0 +#define TRUE (!FALSE) + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////// [ BUS PIRATE SETTINGS ] //////////////////////////////////////////////////// +/////////// NOTE: +/////////// Configuration and Settings for building the Bus Pirate firmware. For more information and support +/////////// On building your own firmware. Please visit the forums on DangerousPrototypes.com. Enjoy the BP :) +/////////// +/////////// Bus Pirate - Brought to you by DangerousPrototypes.com + +/////////////////////////////////////// +// HARDWARE VERSION +#if defined(__PIC24FJ256GB106__) //v4 chip + #define BUSPIRATEV4 +#elif defined(__PIC24FJ64GA002__) //v3/v2go/v1a chip + // Uncomment the hardware version you are building for + #define BUSPIRATEV3 // V3 is also V2G0 + //#define BUSPIRATEV1A //probably no longer supported... +#endif + +//////////////////////////////////////// +// FIRMWARE VERSION STRING +// Build Firmware Version String +// used in 'i' and startup +#define BP_FIRMWARE_STRING "Firmware v6.3-beta1 r2151 " + + +//////////////////////////////////////// +// LANGUAGE +// Select language (US AND DE Currently Supported) +// If you know anyone bi-le +#define LANGUAGE_EN_US +//#define LANGUAGE_DE_DE +//#define LANGUAGE_IT_IT //Not yet supported +//#define LANGUAGE_ES_ES //Not yet supported + + +//////////////////////////////////////// +// MODE SELECTION +// Default Mode Selection +// BP_MAIN is the default mode setting; varys by hardware version +// BP_ADDONS is for special builds with special modes +// BP_CUSTOM is for selecting your own modes below (find: BP_CUSTOM) and uncomment wanted modes +#define BP_MAIN +//#define BP_ADDONS +//#define BP_CUSTOM + + +//////////////////////////////////////// +// OTHER CONFIGS +// BP_ENABLE_HISTORY = Enable the 'h' command for whatever reason it was disabled this enables it. can be disabled again. +// These settings should be left within the BUSPIRATEV4 define due to lack of space on v3. Unless you disable a mode or two +// for a custom v3 build, only include the following options on a v4 build. +#if defined(BUSPIRATEV4) +// Enables the 'h' command to show history +#define BP_ENABLE_HISTORY + +#endif + +/////////////////////// +///////////////////////////// [ END OF CONFIGURATION ]////////////////// +//////////////////////////////////////////////////////////// +///////////////////////////////////// + + +// Buspirate version 3 and v2go use the same everything; so this just +// fixs if the user selected v2go. +#if defined(BUSPIRATEV2GO) + #define BUSPIRATEV3 +#endif + + +#ifdef BUSPIRATEV4 + #include "hardwarev4a.h" +#include "dp_usb/usb_stack_globals.h" + #include "onboardEEPROM.h" +#endif + +#if defined(BUSPIRATEV1A) + #include "hardwarev1a.h" +#elif defined(BUSPIRATEV3) + #define BUSPIRATEV2 //v25 (2go) and v3 are about the same, enable the same featue set for both + #include "hardwarev3.h" +#elif defined(BUSPIRATEV4) + +#else +#error "No hardware defined in base.h." +#endif + +#include "baseIO.h" +#include "baseUI.h" + +//include/exclude modules here +// standard protocols +//#define BP_USE_RAW2WIRE +//#define BP_USE_RAW3WIRE + +//Debugging mode for BPv4, comment out for normal compiling +//Adds alternative communicaton to UART1 over AUX1 and AUX2 instead of USB. +//#define BPV4_DEBUG + + +#if defined(BP_MAIN) + + #define BP_USE_1WIRE + //#ifndef BUSPIRATEV4 + #define BP_USE_HWUART //hardware uart (now also MIDI) + //#endif + #define BP_USE_I2C + //#define BP_USE_I2C_HW + #define BP_USE_HWSPI //hardware spi + #define BP_USE_RAW2WIRE + #define BP_USE_RAW3WIRE + #define BP_USE_LCD // include HD44780 LCD library + #define BP_USE_BASIC + + #if defined(BUSPIRATEV4) + #define BP_USE_DIO //binary mode + #define BP_USE_PCATKB + #define BP_USE_PIC + #endif + +#elif defined(BP_ADDONS) + // most used protos + //#define BP_USE_1WIRE + //#define BP_USE_HWUART //hardware uart (now also MIDI) + //#define BP_USE_I2C + //#define BP_USE_I2C_HW + //#define BP_USE_HWSPI //hardware spi + #define BP_USE_RAW2WIRE + #define BP_USE_RAW3WIRE + #define BP_USE_PCATKB + #define BP_USE_LCD // include HD44780 LCD library + #define BP_USE_PIC + #define BP_USE_DIO //binary mode + +#elif defined(BP_CUSTOM) + // most used protos + //#define BP_USE_1WIRE + #define BP_USE_HWUART //hardware uart (now also MIDI) + //#define BP_USE_I2C + //#define BP_USE_I2C_HW + //#define BP_USE_HWSPI //hardware spi + //#define BP_USE_RAW2WIRE + //#define BP_USE_RAW3WIRE + //#define BP_USE_PCATKB + //#define BP_USE_LCD // include HD44780 LCD library + //#define BP_USE_PIC + //#define BP_USE_DIO //binary mode + +#else +#error "No Bus Pirate configuration defined." +#endif + +//ENABLE BASIC SCRIPTING +#if defined(BUSPIRATEV4) + #define BP_USE_BASIC // basic subsystem + //#define BP_USE_BASICI2C // use an i2ceeprom for storing +#endif + + +// only 1 should be uncommented +//#define BASICTEST +//#define BASICTEST_I2C +//#define BASICTEST_UART +//#define BASICTEST_R3W +//#define BASICTEST_PIC10 // program blink a led +//#define BASICTEST_PIC10_2 // read whole pic + +//sets the address in the bootloader to jump to on the bootloader command +//must be defined in asm +asm (".equ BLJUMPADDRESS, 0xABF8"); + +// +// +// +// END CONFIG AREA +// +// +// + +//// left out for now +/* my italian and spanish aint that good ;) +#elif defined(LANGUAGE_IT_IT) + #include "translations/it-IT.h" +// #include "translations/en-US.h" +#elif defined(LANGUAGE_ES_ES) + #include "translations/es-ES.h" + */ + + +// If BUSPIRATEv4 then use the en_US.h otherwise +// use the v3 one. The version3 one does not include +// the modes that the v3 cannot fit. This way we dont +// waste space on strings we wont see. +// +// Also note; the BPV4 project file uses the en_US.s file +// the buspurate v3 uses the other one... +#if defined(BUSPIRATEV4) + #if defined(LANGUAGE_EN_US) + #include "translations/BPv4_en_US.h" + #elif defined(LANGUAGE_DE_DE) + #include "translations/BPv4_de_DE.h" + #else + #error "No language defined in base.h." + #endif +#else + #if defined(LANGUAGE_EN_US) + #include "translations/BPv3_en_US.h" + #elif defined(LANGUAGE_DE_DE) + #include "translations/BPv3_de_DE.h" + #else + #error "No language defined in base.h." + #endif +#endif + + + + + + +//these settings are destroyed between modes. +//this is used to pass stuff from protocol libraries to BP +//for example, allowpullup determines if the P menu is available +//TO DO: add global scratch buffer to setting array for use in +// 1-Wire enumeration, JTAG input buffer, etc... + +struct _modeConfig { + unsigned char speed; + unsigned char numbits; + unsigned char buf[16]; + unsigned char altAUX : 2; // there are 4 AUX on BUSPIRATEV4 + unsigned char periodicService : 1; + unsigned char lsbEN : 1; + unsigned char HiZ : 1; + unsigned char int16 : 1; // 16 bits output? + unsigned char wwr : 1; // write with read +}; + +struct _command { + unsigned char cmd; + unsigned int num; + unsigned int repeat; +}; + +unsigned int bpRevByte(unsigned int c); + +//reset all peripherals before configuring a new bus protocol +void bpInit(void); + +//take an ADC measurement on channel c +unsigned int bpADC(unsigned char c); +//takes a measurement from the ADC probe and prints the result to the user terminal +void bpADCprobe(void); +void bpADCCprobe(void); + +//print byte c to the user terminal in the format +// specified by the bpConfig.displayMode setting +void bpWbyte(unsigned int c); + +//delays used by many libraries +//void bpDelayMS(const unsigned char delay); +//void bpDelayUS(const unsigned char delay); +void bpDelayMS(int delay); +void bpDelayUS(int delay); +#endif + + diff --git a/Firmware/baseIO.c b/Firmware/baseIO.c index e293e776..e2ae9cbf 100644 --- a/Firmware/baseIO.c +++ b/Firmware/baseIO.c @@ -1,656 +1,656 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -#include "busPirateCore.h"//need access to bpConfig -extern struct _bpConfig bpConfig; //holds persistant bus pirate settings (see base.h) need hardware version info - -#if defined (BUSPIRATEV4) -extern BYTE cdc_In_len; -extern BYTE cdc_Out_len; -#endif - -//echo ASCII 0 or 1, given unsigned char c - -void bpEchoState(unsigned int c) { - if (c) UART1TX('1'); - else UART1TX('0'); -} - -// -// -//functions for terminal output of various types and lengths -// -// - -//Write a string to the user terminal - -void bpWstring(char *s) { - char c; - while ((c = *s++)) UART1TX(c); -} - -//write a string to the user terminal, finish with a line break - -void bpWline(char *s) { - char c; - while ((c = *s++)) UART1TX(c); - UART1TX(0x0d); - UART1TX(0x0a); - -} - -//output an 8bit/byte binary value to the user terminal - -void bpWbin(unsigned char c) { - unsigned char i, j; - j = 0b10000000; - - bpWstring("0b"); - - for (i = 0; i < 8; i++) { - if (c & j) { - UART1TX('1'); - } else { - UART1TX('0'); - } - j >>= 1; - } - -} - -//output an 32bit/long decimal value to the user terminal - -void bpWlongdec(unsigned long l) { - unsigned long c, m; - unsigned char j, k = 0; - - c = 100000000; - for (j = 0; j < 8; j++) { - m = l / c; - if (k || m) { - UART1TX(m + '0'); - l = l - (m * c); - k = 1; - } - c /= 10; - } - UART1TX(l + '0'); - -} - -// userfriendly printing of looooonng ints - -void bpWlongdecf(unsigned long l) { - unsigned long int temp; - int mld, mil; - - mld = 0; - mil = 0; - temp = 0; - - if (l >= 1000000) { - temp = l / 1000000; - bpWintdec(temp); - UART1TX(','); - l %= 1000000; - if (l < 1000) bpWstring("000,"); - mld = 1; - mil = 1; - } - if (l >= 1000) { - temp = l / 1000; - if (temp >= 100) { - bpWintdec(temp); - } else if (mld) { - if (temp >= 10) { - UART1TX('0'); // 1 leading zero - } else { - bpWstring("00"); - } - bpWintdec(temp); - } else bpWintdec(temp); - UART1TX(','); - l %= 1000; - mil = 1; - } - if (l >= 100) { - bpWintdec(l); - } else if (mil) { - if (l >= 10) { - UART1TX('0'); // 1 leading zero - } else { - bpWstring("00"); - } - bpWintdec(l); - } else bpWintdec(l); -} - - -//output an 16bit/integer decimal value to the user terminal - -void bpWintdec(unsigned int i) { - unsigned int c, m; - unsigned char j, k = 0; - - c = 10000; - for (j = 0; j < 4; j++) { - m = i / c; - if (k || m) { - UART1TX(m + '0'); - i = i - (m * c); - k = 1; - } - c /= 10; - } - UART1TX(i + '0'); -} - -//output an 8bit/byte decimal value to the user terminal - -void bpWdec(unsigned char c) { - unsigned char d, j, m, k = 0; - - d = 100; - for (j = 0; j < 2; j++) { - m = c / d; - if (k || m) { - UART1TX(m + '0'); - c = c - (m * d); - k = 1; - } - d /= 10; - } - UART1TX(c + '0'); -} - -//output an 8bit/byte hex value to the user terminal -const unsigned char HEXASCII[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - -void bpWhex(unsigned int c) { - unsigned int b; - - bpWstring("0x"); - b = (c >> 4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - -void bpWhexBuf(unsigned int c) { - unsigned int b; - - UARTbuf('0'); - UARTbuf('x'); - b = (c >> 4) & 0x0F; - UARTbuf(HEXASCII[b]); - b = c & 0x0F; - UARTbuf(HEXASCII[b]); - return; -} - -// output a 16bit hex value to the user terminal - -void bpWinthex(unsigned int c) { - unsigned int b; - - bpWstring("0x"); - b = (c >> 12) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c >> 8) & 0x0F; - UART1TX(HEXASCII[b]); - b = (c >> 4) & 0x0F; - UART1TX(HEXASCII[b]); - b = c & 0x0F; - UART1TX(HEXASCII[b]); - return; -} - - -//print an ADC measurement in decimal form - -void bpWvolts(const unsigned int adc) { - unsigned char c; - - // input voltage is divided by two and compared to 3.3V - // volt = adc / 1024 * 3.3V * 2 - // centivolt = adc / 1024 * 3.3 * 2 * 100 = adc * 165 / 256 - // This is approximately (adc * 29 / 45), making the calculation - // fit in an unsigned int. The error is less than 1mV. - const unsigned int centivolt = (adc * 29) / 45; - - bpWdec(centivolt / 100); - - UART1TX('.'); - - c = centivolt % 100; - - if (c < 10) // need extra zero? - UART1TX('0'); - - bpWdec(centivolt % 100); -} - - - -// Read the lower 16 bits from programming flash memory - -unsigned int bpReadFlash(unsigned int page, unsigned int addr) { - unsigned int tblold; - unsigned flash; - - tblold = TBLPAG; - TBLPAG = page; - flash = (__builtin_tblrdh(addr) << 8) | __builtin_tblrdl(addr); - TBLPAG = tblold; - - return flash; -} - -#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) -// -// -// Base user terminal UART functions -// -// - - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -static unsigned int UARTspeed[] = {13332, 3332, 1666, 832, 416, 207, 103, 68, 34,}; //BRG:300,1200,2400,4800,9600,19200,38400,57600,115200 - -void InitializeUART1(void) { // if termspeed==9 it is custom - // dunno if the additional settings are needed but justin kaas! - if (bpConfig.termSpeed != 9) U1BRG = UARTspeed[bpConfig.termSpeed]; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; -} -unsigned char UART1TXRdy(void) { - return U1STAbits.UTXBF; -} - -unsigned char UART1RXRdy(void) { - return U1STAbits.URXDA; -} - -//new UART ring buffer -//uses user terminal input buffer to buffer UART output -//any existing user input will be destroyed -//best used for binary mode and sniffers -//static struct _UARTRINGBUF{ -static unsigned int writepointer; -static unsigned int readpointer; -//}ringBuf; - -void UARTbufSetup(void) { - //setup ring buffer pointers - readpointer = 0; - writepointer = 1; - bpConfig.overflow = 0; -} - -void UARTbufService(void) { - unsigned int i; - if (U1STAbits.UTXBF == 0) {//check first for free slot - - i = readpointer + 1; - if (i == TERMINAL_BUFFER) i = 0; //check for wrap - if (i == writepointer) return; //buffer empty, - readpointer = i; - U1TXREG = bpConfig.terminalInput[readpointer]; //move a byte to UART - } -} - -void UARTbufFlush(void) { - unsigned int i; - - while (1) { - i = readpointer + 1; - if (i == TERMINAL_BUFFER) i = 0; //check for wrap - if (i == writepointer) return; //buffer empty, - - if (U1STAbits.UTXBF == 0) {//free slot, move a byte to UART - readpointer = i; - U1TXREG = bpConfig.terminalInput[readpointer]; - } - } -} - -void UARTbuf(char c) { - if (writepointer == readpointer) { - BP_LEDMODE = 0; //drop byte, buffer full LED off - bpConfig.overflow = 1; - } else { - bpConfig.terminalInput[writepointer] = c; - writepointer++; - if (writepointer == TERMINAL_BUFFER) writepointer = 0; //check for wrap - } -} - -//get a byte from UART - -unsigned char UART1RX(void) { - while (U1STAbits.URXDA == 0); - return U1RXREG; -} - -void WAITTXEmpty(void) { - while (U1STAbits.TRMT == 0); -} -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer - -void UART1TX(char c) { - if (bpConfig.quiet) return; - while (U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -void UART1Speed(unsigned char brg) { - U1BRG = brg; -} - -unsigned char CheckCommsError(void) { - return U1STAbits.OERR; //check for user terminal buffer overflow error -} - -void ClearCommsError(void) { - U1STA &= (~0b10); //clear overrun error if exists -} - -/* interrupt transfer related stuff */ -unsigned char __attribute__((section(".bss.filereg"))) * UART1RXBuf; -unsigned int __attribute__((section(".bss.filereg"))) UART1RXToRecv; -unsigned int __attribute__((section(".bss.filereg"))) UART1RXRecvd; -unsigned char __attribute__((section(".bss.filereg"))) * UART1TXBuf; -unsigned int __attribute__((section(".bss.filereg"))) UART1TXSent; -unsigned int __attribute__((section(".bss.filereg"))) UART1TXAvailable; - -void UART1TXInt() { - if (IEC0bits.U1TXIE == 1) - return; - if (UART1TXAvailable == UART1TXSent) - return; - - while (U1STAbits.UTXBF == 1); //if buffer is full, wait - - IFS0bits.U1TXIF = 0; - - IEC0bits.U1TXIE = 1; - - U1TXREG = UART1TXBuf[UART1TXSent]; -} - -// UART1 ISRs - -void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) { - UART1RXBuf[UART1RXRecvd] = U1RXREG; - UART1RXRecvd++; - - if (UART1RXRecvd == UART1RXToRecv) { - // disable UART1 RX interrupt - IEC0bits.U1RXIE = 0; - } - - IFS0bits.U1RXIF = 0; -} - -void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void) { - UART1TXSent++; - if (UART1TXSent == UART1TXAvailable) { - // if everything is sent disale interrupts - IEC0bits.U1TXIE = 0; - } else { - // feed next byte - U1TXREG = UART1TXBuf[UART1TXSent]; - } - - IFS0bits.U1TXIF = 0; -} - - -#endif - -#if defined(BUSPIRATEV4) && !defined (BPV4_DEBUG) - -extern BDentry *CDC_Outbdp, *CDC_Inbdp; - -void UART1TX(char c) { - if (bpConfig.quiet) return; - putc_cdc(c); -} - -void UARTbuf(char c) { - UART1TX(c); -} - -void WAITTXEmpty(void) { - WaitInReady(); -} - -unsigned char UART1TXRdy(void) { - return 1; -} - -//is data available? - -unsigned char UART1RXRdy(void) { - if (cdc_Out_len) return 1; - if (getOutReady()) return 1; - return 0; -} - -//get a byte from UART - -unsigned char UART1RX(void) { - return getc_cdc(); -} - -void UARTbufFlush(void) { - CDC_Flush_In_Now(); -} - -unsigned char CheckCommsError(void) { - return 0; //check for user terminal buffer overflow error -} - -void UARTbufSetup(void) { -} - -void UARTbufService(void) { -} - -void ClearCommsError(void) { -} - -void InitializeUART1(void) { -} - -void UART1Speed(unsigned char brg) { -} -#endif - -#if defined(BUSPIRATEV4) && defined (BPV4_DEBUG) - -// -// -// Base user terminal UART functions -// -// - - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -static unsigned int UARTspeed[] = {13332, 3332, 1666, 832, 416, 207, 103, 68, 34,}; //BRG:300,1200,2400,4800,9600,19200,38400,57600,115200 - -void InitializeUART1(void) { // if termspeed==9 it is custom - // dunno if the additional settings are needed but justin kaas! - if (bpConfig.termSpeed != 9) U1BRG = UARTspeed[bpConfig.termSpeed]; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; -} -unsigned char UART1TXRdy(void) { - return U1STAbits.UTXBF; -} - -unsigned char UART1RXRdy(void) { - return U1STAbits.URXDA; -} - -//new UART ring buffer -//uses user terminal input buffer to buffer UART output -//any existing user input will be destroyed -//best used for binary mode and sniffers -//static struct _UARTRINGBUF{ -static unsigned int writepointer; -static unsigned int readpointer; -//}ringBuf; - -void UARTbufSetup(void) { - //setup ring buffer pointers - readpointer = 0; - writepointer = 1; - bpConfig.overflow = 0; -} - -void UARTbufService(void) { - unsigned int i; - if (U1STAbits.UTXBF == 0) {//check first for free slot - - i = readpointer + 1; - if (i == TERMINAL_BUFFER) i = 0; //check for wrap - if (i == writepointer) return; //buffer empty, - readpointer = i; - U1TXREG = bpConfig.terminalInput[readpointer]; //move a byte to UART - } -} - -void UARTbufFlush(void) { - unsigned int i; - - while (1) { - i = readpointer + 1; - if (i == TERMINAL_BUFFER) i = 0; //check for wrap - if (i == writepointer) return; //buffer empty, - - if (U1STAbits.UTXBF == 0) {//free slot, move a byte to UART - readpointer = i; - U1TXREG = bpConfig.terminalInput[readpointer]; - } - } -} - -void UARTbuf(char c) { - if (writepointer == readpointer) { - BP_LEDMODE = 0; //drop byte, buffer full LED off - bpConfig.overflow = 1; - } else { - bpConfig.terminalInput[writepointer] = c; - writepointer++; - if (writepointer == TERMINAL_BUFFER) writepointer = 0; //check for wrap - } -} - -//get a byte from UART - -unsigned char UART1RX(void) { - while (U1STAbits.URXDA == 0); - return U1RXREG; -} - -void WAITTXEmpty(void) { - while (U1STAbits.TRMT == 0); -} -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer - -void UART1TX(char c) { - if (bpConfig.quiet) return; - while (U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -void UART1Speed(unsigned char brg) { - U1BRG = brg; -} - -unsigned char CheckCommsError(void) { - return U1STAbits.OERR; //check for user terminal buffer overflow error -} - -void ClearCommsError(void) { - U1STA &= (~0b10); //clear overrun error if exists -} - -/* interrupt transfer related stuff */ -unsigned char __attribute__((section(".bss.filereg"))) * UART1RXBuf; -unsigned int __attribute__((section(".bss.filereg"))) UART1RXToRecv; -unsigned int __attribute__((section(".bss.filereg"))) UART1RXRecvd; -unsigned char __attribute__((section(".bss.filereg"))) * UART1TXBuf; -unsigned int __attribute__((section(".bss.filereg"))) UART1TXSent; -unsigned int __attribute__((section(".bss.filereg"))) UART1TXAvailable; - -void UART1TXInt() { - if (IEC0bits.U1TXIE == 1) - return; - if (UART1TXAvailable == UART1TXSent) - return; - - while (U1STAbits.UTXBF == 1); //if buffer is full, wait - - IFS0bits.U1TXIF = 0; - - IEC0bits.U1TXIE = 1; - - U1TXREG = UART1TXBuf[UART1TXSent]; -} - -// UART1 ISRs - -void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) { - UART1RXBuf[UART1RXRecvd] = U1RXREG; - UART1RXRecvd++; - - if (UART1RXRecvd == UART1RXToRecv) { - // disable UART1 RX interrupt - IEC0bits.U1RXIE = 0; - } - - IFS0bits.U1RXIF = 0; -} - -void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void) { - UART1TXSent++; - if (UART1TXSent == UART1TXAvailable) { - // if everything is sent disale interrupts - IEC0bits.U1TXIE = 0; - } else { - // feed next byte - U1TXREG = UART1TXBuf[UART1TXSent]; - } - - IFS0bits.U1TXIF = 0; -} - - -#endif - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +#include "busPirateCore.h"//need access to bpConfig +extern struct _bpConfig bpConfig; //holds persistant bus pirate settings (see base.h) need hardware version info + +#if defined (BUSPIRATEV4) +extern BYTE cdc_In_len; +extern BYTE cdc_Out_len; +#endif + +//echo ASCII 0 or 1, given unsigned char c + +void bpEchoState(unsigned int c) { + if (c) UART1TX('1'); + else UART1TX('0'); +} + +// +// +//functions for terminal output of various types and lengths +// +// + +//Write a string to the user terminal + +void bpWstring(char *s) { + char c; + while ((c = *s++)) UART1TX(c); +} + +//write a string to the user terminal, finish with a line break + +void bpWline(char *s) { + char c; + while ((c = *s++)) UART1TX(c); + UART1TX(0x0d); + UART1TX(0x0a); + +} + +//output an 8bit/byte binary value to the user terminal + +void bpWbin(unsigned char c) { + unsigned char i, j; + j = 0b10000000; + + bpWstring("0b"); + + for (i = 0; i < 8; i++) { + if (c & j) { + UART1TX('1'); + } else { + UART1TX('0'); + } + j >>= 1; + } + +} + +//output an 32bit/long decimal value to the user terminal + +void bpWlongdec(unsigned long l) { + unsigned long c, m; + unsigned char j, k = 0; + + c = 100000000; + for (j = 0; j < 8; j++) { + m = l / c; + if (k || m) { + UART1TX(m + '0'); + l = l - (m * c); + k = 1; + } + c /= 10; + } + UART1TX(l + '0'); + +} + +// userfriendly printing of looooonng ints + +void bpWlongdecf(unsigned long l) { + unsigned long int temp; + int mld, mil; + + mld = 0; + mil = 0; + temp = 0; + + if (l >= 1000000) { + temp = l / 1000000; + bpWintdec(temp); + UART1TX(','); + l %= 1000000; + if (l < 1000) bpWstring("000,"); + mld = 1; + mil = 1; + } + if (l >= 1000) { + temp = l / 1000; + if (temp >= 100) { + bpWintdec(temp); + } else if (mld) { + if (temp >= 10) { + UART1TX('0'); // 1 leading zero + } else { + bpWstring("00"); + } + bpWintdec(temp); + } else bpWintdec(temp); + UART1TX(','); + l %= 1000; + mil = 1; + } + if (l >= 100) { + bpWintdec(l); + } else if (mil) { + if (l >= 10) { + UART1TX('0'); // 1 leading zero + } else { + bpWstring("00"); + } + bpWintdec(l); + } else bpWintdec(l); +} + + +//output an 16bit/integer decimal value to the user terminal + +void bpWintdec(unsigned int i) { + unsigned int c, m; + unsigned char j, k = 0; + + c = 10000; + for (j = 0; j < 4; j++) { + m = i / c; + if (k || m) { + UART1TX(m + '0'); + i = i - (m * c); + k = 1; + } + c /= 10; + } + UART1TX(i + '0'); +} + +//output an 8bit/byte decimal value to the user terminal + +void bpWdec(unsigned char c) { + unsigned char d, j, m, k = 0; + + d = 100; + for (j = 0; j < 2; j++) { + m = c / d; + if (k || m) { + UART1TX(m + '0'); + c = c - (m * d); + k = 1; + } + d /= 10; + } + UART1TX(c + '0'); +} + +//output an 8bit/byte hex value to the user terminal +const unsigned char HEXASCII[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + +void bpWhex(unsigned int c) { + unsigned int b; + + bpWstring("0x"); + b = (c >> 4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + +void bpWhexBuf(unsigned int c) { + unsigned int b; + + UARTbuf('0'); + UARTbuf('x'); + b = (c >> 4) & 0x0F; + UARTbuf(HEXASCII[b]); + b = c & 0x0F; + UARTbuf(HEXASCII[b]); + return; +} + +// output a 16bit hex value to the user terminal + +void bpWinthex(unsigned int c) { + unsigned int b; + + bpWstring("0x"); + b = (c >> 12) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c >> 8) & 0x0F; + UART1TX(HEXASCII[b]); + b = (c >> 4) & 0x0F; + UART1TX(HEXASCII[b]); + b = c & 0x0F; + UART1TX(HEXASCII[b]); + return; +} + + +//print an ADC measurement in decimal form + +void bpWvolts(const unsigned int adc) { + unsigned char c; + + // input voltage is divided by two and compared to 3.3V + // volt = adc / 1024 * 3.3V * 2 + // centivolt = adc / 1024 * 3.3 * 2 * 100 = adc * 165 / 256 + // This is approximately (adc * 29 / 45), making the calculation + // fit in an unsigned int. The error is less than 1mV. + const unsigned int centivolt = (adc * 29) / 45; + + bpWdec(centivolt / 100); + + UART1TX('.'); + + c = centivolt % 100; + + if (c < 10) // need extra zero? + UART1TX('0'); + + bpWdec(centivolt % 100); +} + + + +// Read the lower 16 bits from programming flash memory + +unsigned int bpReadFlash(unsigned int page, unsigned int addr) { + unsigned int tblold; + unsigned flash; + + tblold = TBLPAG; + TBLPAG = page; + flash = (__builtin_tblrdh(addr) << 8) | __builtin_tblrdl(addr); + TBLPAG = tblold; + + return flash; +} + +#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) +// +// +// Base user terminal UART functions +// +// + + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +static unsigned int UARTspeed[] = {13332, 3332, 1666, 832, 416, 207, 103, 68, 34,}; //BRG:300,1200,2400,4800,9600,19200,38400,57600,115200 + +void InitializeUART1(void) { // if termspeed==9 it is custom + // dunno if the additional settings are needed but justin kaas! + if (bpConfig.termSpeed != 9) U1BRG = UARTspeed[bpConfig.termSpeed]; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; +} +unsigned char UART1TXRdy(void) { + return U1STAbits.UTXBF; +} + +unsigned char UART1RXRdy(void) { + return U1STAbits.URXDA; +} + +//new UART ring buffer +//uses user terminal input buffer to buffer UART output +//any existing user input will be destroyed +//best used for binary mode and sniffers +//static struct _UARTRINGBUF{ +static unsigned int writepointer; +static unsigned int readpointer; +//}ringBuf; + +void UARTbufSetup(void) { + //setup ring buffer pointers + readpointer = 0; + writepointer = 1; + bpConfig.overflow = 0; +} + +void UARTbufService(void) { + unsigned int i; + if (U1STAbits.UTXBF == 0) {//check first for free slot + + i = readpointer + 1; + if (i == TERMINAL_BUFFER) i = 0; //check for wrap + if (i == writepointer) return; //buffer empty, + readpointer = i; + U1TXREG = bpConfig.terminalInput[readpointer]; //move a byte to UART + } +} + +void UARTbufFlush(void) { + unsigned int i; + + while (1) { + i = readpointer + 1; + if (i == TERMINAL_BUFFER) i = 0; //check for wrap + if (i == writepointer) return; //buffer empty, + + if (U1STAbits.UTXBF == 0) {//free slot, move a byte to UART + readpointer = i; + U1TXREG = bpConfig.terminalInput[readpointer]; + } + } +} + +void UARTbuf(char c) { + if (writepointer == readpointer) { + BP_LEDMODE = 0; //drop byte, buffer full LED off + bpConfig.overflow = 1; + } else { + bpConfig.terminalInput[writepointer] = c; + writepointer++; + if (writepointer == TERMINAL_BUFFER) writepointer = 0; //check for wrap + } +} + +//get a byte from UART + +unsigned char UART1RX(void) { + while (U1STAbits.URXDA == 0); + return U1RXREG; +} + +void WAITTXEmpty(void) { + while (U1STAbits.TRMT == 0); +} +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer + +void UART1TX(char c) { + if (bpConfig.quiet) return; + while (U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +void UART1Speed(unsigned char brg) { + U1BRG = brg; +} + +unsigned char CheckCommsError(void) { + return U1STAbits.OERR; //check for user terminal buffer overflow error +} + +void ClearCommsError(void) { + U1STA &= (~0b10); //clear overrun error if exists +} + +/* interrupt transfer related stuff */ +unsigned char __attribute__((section(".bss.filereg"))) * UART1RXBuf; +unsigned int __attribute__((section(".bss.filereg"))) UART1RXToRecv; +unsigned int __attribute__((section(".bss.filereg"))) UART1RXRecvd; +unsigned char __attribute__((section(".bss.filereg"))) * UART1TXBuf; +unsigned int __attribute__((section(".bss.filereg"))) UART1TXSent; +unsigned int __attribute__((section(".bss.filereg"))) UART1TXAvailable; + +void UART1TXInt() { + if (IEC0bits.U1TXIE == 1) + return; + if (UART1TXAvailable == UART1TXSent) + return; + + while (U1STAbits.UTXBF == 1); //if buffer is full, wait + + IFS0bits.U1TXIF = 0; + + IEC0bits.U1TXIE = 1; + + U1TXREG = UART1TXBuf[UART1TXSent]; +} + +// UART1 ISRs + +void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) { + UART1RXBuf[UART1RXRecvd] = U1RXREG; + UART1RXRecvd++; + + if (UART1RXRecvd == UART1RXToRecv) { + // disable UART1 RX interrupt + IEC0bits.U1RXIE = 0; + } + + IFS0bits.U1RXIF = 0; +} + +void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void) { + UART1TXSent++; + if (UART1TXSent == UART1TXAvailable) { + // if everything is sent disale interrupts + IEC0bits.U1TXIE = 0; + } else { + // feed next byte + U1TXREG = UART1TXBuf[UART1TXSent]; + } + + IFS0bits.U1TXIF = 0; +} + + +#endif + +#if defined(BUSPIRATEV4) && !defined (BPV4_DEBUG) + +extern BDentry *CDC_Outbdp, *CDC_Inbdp; + +void UART1TX(char c) { + if (bpConfig.quiet) return; + putc_cdc(c); +} + +void UARTbuf(char c) { + UART1TX(c); +} + +void WAITTXEmpty(void) { + WaitInReady(); +} + +unsigned char UART1TXRdy(void) { + return 1; +} + +//is data available? + +unsigned char UART1RXRdy(void) { + if (cdc_Out_len) return 1; + if (getOutReady()) return 1; + return 0; +} + +//get a byte from UART + +unsigned char UART1RX(void) { + return getc_cdc(); +} + +void UARTbufFlush(void) { + CDC_Flush_In_Now(); +} + +unsigned char CheckCommsError(void) { + return 0; //check for user terminal buffer overflow error +} + +void UARTbufSetup(void) { +} + +void UARTbufService(void) { +} + +void ClearCommsError(void) { +} + +void InitializeUART1(void) { +} + +void UART1Speed(unsigned char brg) { +} +#endif + +#if defined(BUSPIRATEV4) && defined (BPV4_DEBUG) + +// +// +// Base user terminal UART functions +// +// + + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +static unsigned int UARTspeed[] = {13332, 3332, 1666, 832, 416, 207, 103, 68, 34,}; //BRG:300,1200,2400,4800,9600,19200,38400,57600,115200 + +void InitializeUART1(void) { // if termspeed==9 it is custom + // dunno if the additional settings are needed but justin kaas! + if (bpConfig.termSpeed != 9) U1BRG = UARTspeed[bpConfig.termSpeed]; //13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; +} +unsigned char UART1TXRdy(void) { + return U1STAbits.UTXBF; +} + +unsigned char UART1RXRdy(void) { + return U1STAbits.URXDA; +} + +//new UART ring buffer +//uses user terminal input buffer to buffer UART output +//any existing user input will be destroyed +//best used for binary mode and sniffers +//static struct _UARTRINGBUF{ +static unsigned int writepointer; +static unsigned int readpointer; +//}ringBuf; + +void UARTbufSetup(void) { + //setup ring buffer pointers + readpointer = 0; + writepointer = 1; + bpConfig.overflow = 0; +} + +void UARTbufService(void) { + unsigned int i; + if (U1STAbits.UTXBF == 0) {//check first for free slot + + i = readpointer + 1; + if (i == TERMINAL_BUFFER) i = 0; //check for wrap + if (i == writepointer) return; //buffer empty, + readpointer = i; + U1TXREG = bpConfig.terminalInput[readpointer]; //move a byte to UART + } +} + +void UARTbufFlush(void) { + unsigned int i; + + while (1) { + i = readpointer + 1; + if (i == TERMINAL_BUFFER) i = 0; //check for wrap + if (i == writepointer) return; //buffer empty, + + if (U1STAbits.UTXBF == 0) {//free slot, move a byte to UART + readpointer = i; + U1TXREG = bpConfig.terminalInput[readpointer]; + } + } +} + +void UARTbuf(char c) { + if (writepointer == readpointer) { + BP_LEDMODE = 0; //drop byte, buffer full LED off + bpConfig.overflow = 1; + } else { + bpConfig.terminalInput[writepointer] = c; + writepointer++; + if (writepointer == TERMINAL_BUFFER) writepointer = 0; //check for wrap + } +} + +//get a byte from UART + +unsigned char UART1RX(void) { + while (U1STAbits.URXDA == 0); + return U1RXREG; +} + +void WAITTXEmpty(void) { + while (U1STAbits.TRMT == 0); +} +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer + +void UART1TX(char c) { + if (bpConfig.quiet) return; + while (U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +void UART1Speed(unsigned char brg) { + U1BRG = brg; +} + +unsigned char CheckCommsError(void) { + return U1STAbits.OERR; //check for user terminal buffer overflow error +} + +void ClearCommsError(void) { + U1STA &= (~0b10); //clear overrun error if exists +} + +/* interrupt transfer related stuff */ +unsigned char __attribute__((section(".bss.filereg"))) * UART1RXBuf; +unsigned int __attribute__((section(".bss.filereg"))) UART1RXToRecv; +unsigned int __attribute__((section(".bss.filereg"))) UART1RXRecvd; +unsigned char __attribute__((section(".bss.filereg"))) * UART1TXBuf; +unsigned int __attribute__((section(".bss.filereg"))) UART1TXSent; +unsigned int __attribute__((section(".bss.filereg"))) UART1TXAvailable; + +void UART1TXInt() { + if (IEC0bits.U1TXIE == 1) + return; + if (UART1TXAvailable == UART1TXSent) + return; + + while (U1STAbits.UTXBF == 1); //if buffer is full, wait + + IFS0bits.U1TXIF = 0; + + IEC0bits.U1TXIE = 1; + + U1TXREG = UART1TXBuf[UART1TXSent]; +} + +// UART1 ISRs + +void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) { + UART1RXBuf[UART1RXRecvd] = U1RXREG; + UART1RXRecvd++; + + if (UART1RXRecvd == UART1RXToRecv) { + // disable UART1 RX interrupt + IEC0bits.U1RXIE = 0; + } + + IFS0bits.U1RXIF = 0; +} + +void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void) { + UART1TXSent++; + if (UART1TXSent == UART1TXAvailable) { + // if everything is sent disale interrupts + IEC0bits.U1TXIE = 0; + } else { + // feed next byte + U1TXREG = UART1TXBuf[UART1TXSent]; + } + + IFS0bits.U1TXIF = 0; +} + + +#endif + diff --git a/Firmware/baseIO.h b/Firmware/baseIO.h index a5030343..5274b169 100644 --- a/Firmware/baseIO.h +++ b/Firmware/baseIO.h @@ -1,139 +1,139 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#ifndef baseIO_h -#define baseIO_h - -//manage user terminal input -unsigned int bpUserNumberPrompt(unsigned int maxBytes, unsigned int maxValue, unsigned int defValue); - -//manage user terminal input -unsigned int bpGetUserInput(unsigned int *currentByte, unsigned int maxBytes, unsigned char *terminalInput ); - -//reads forward to grab ASCII number string, returns byte value -unsigned int bpGetASCIInumber(unsigned int totalBytes, unsigned int *currentByte, unsigned char *commandArr); - -//convert ASCII number string (HEX, BIN, DEC) to INT value -unsigned int ASCII2INT(unsigned char totalDigits, unsigned char *numberArr); - -//echo ASCII 0 or 1, given unsigned char c -//#define bpEchoState(c) UART1TX(c+0x30) -void bpEchoState(unsigned int c); - -// -// -//functions for terminal output of various types and lengths -// -// - -//Write a string to the user terminal -void bpWstring(char *s); - -//write a string and line break to the user terminal -void bpWline(char *s); - -//output an 8bit/byte hex value to the user terminal -void bpWhex(unsigned int c); - -//output an 16bit/2byte hex value to the user terminal -void bpWinthex(unsigned int c); - -//output an 8bit/byte binary value to the user terminal -void bpWbin(unsigned char c); - -//output an 8bit/byte decimal value to the user terminal -void bpWdec(unsigned char c); - -//output an 16bit/integer decimal value to the user terminal -void bpWintdec(unsigned int i); - -//output an 32bit/long decimal value to the user terminal -void bpWlongdec(unsigned long l); -//friendly version -void bpWlongdecf(unsigned long l); - -//print an ADC measurement in decimal form -void bpWvolts(unsigned int adc); - -//pseudofuncion break sequences -//#define bpBR UART1TX('\x0D');UART1TX('\x0A') //macro for line break - -#define bpBR bpWline(""); // saves 258 bytes.. \\o \o/ o// - -#define bpWBR bpBR -#define bpSP UART1TX(' ') //macro for space -#define bpWchar(x) UART1TX(x) - -// -// -// Base user terminal UART functions -// -// -#define UART_NORMAL_SPEED 34 -#define UART_FAST_SPEED 3 - -/* interrupt transfer related stuff */ -extern unsigned char *UART1RXBuf; -extern unsigned int UART1RXToRecv; -extern unsigned int UART1RXRecvd; -extern unsigned char *UART1TXBuf; -extern unsigned int UART1TXSent; -extern unsigned int UART1TXAvailable; - -// starts interrupt TX -void UART1TXInt(void); - -//is byte available in RX buffer? -unsigned char UART1RXRdy(void); -unsigned char UART1TXRdy(void); -//get a byte from UART -unsigned char UART1RX(void); -void WAITTXEmpty(void); - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c); - -void ClearCommsError(void); -unsigned char CheckCommsError(void); -// sets the uart baudrate generator -void UART1Speed(unsigned char brg); -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -void InitializeUART1(void); -unsigned char UART1TXRdy(void); -// -// -// Ring buffer for UART -// -// -void UARTbufService(void); -void UARTbufFlush(void); -void UARTbufSetup(void); -void UARTbuf(char c); -void bpWhexBuf(unsigned int c); //write a hex value to ring buffer - - -unsigned char USBUSARTIsTxTrfReady(void); -//void putUSBUSART(char *data, unsigned char length); - -// -// Hardware functions and definitions -// -// Read the lower 16 bits from programming flash memory -unsigned int bpReadFlash(unsigned int page, unsigned int addr); - -#endif - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#ifndef baseIO_h +#define baseIO_h + +//manage user terminal input +unsigned int bpUserNumberPrompt(unsigned int maxBytes, unsigned int maxValue, unsigned int defValue); + +//manage user terminal input +unsigned int bpGetUserInput(unsigned int *currentByte, unsigned int maxBytes, unsigned char *terminalInput ); + +//reads forward to grab ASCII number string, returns byte value +unsigned int bpGetASCIInumber(unsigned int totalBytes, unsigned int *currentByte, unsigned char *commandArr); + +//convert ASCII number string (HEX, BIN, DEC) to INT value +unsigned int ASCII2INT(unsigned char totalDigits, unsigned char *numberArr); + +//echo ASCII 0 or 1, given unsigned char c +//#define bpEchoState(c) UART1TX(c+0x30) +void bpEchoState(unsigned int c); + +// +// +//functions for terminal output of various types and lengths +// +// + +//Write a string to the user terminal +void bpWstring(char *s); + +//write a string and line break to the user terminal +void bpWline(char *s); + +//output an 8bit/byte hex value to the user terminal +void bpWhex(unsigned int c); + +//output an 16bit/2byte hex value to the user terminal +void bpWinthex(unsigned int c); + +//output an 8bit/byte binary value to the user terminal +void bpWbin(unsigned char c); + +//output an 8bit/byte decimal value to the user terminal +void bpWdec(unsigned char c); + +//output an 16bit/integer decimal value to the user terminal +void bpWintdec(unsigned int i); + +//output an 32bit/long decimal value to the user terminal +void bpWlongdec(unsigned long l); +//friendly version +void bpWlongdecf(unsigned long l); + +//print an ADC measurement in decimal form +void bpWvolts(unsigned int adc); + +//pseudofuncion break sequences +//#define bpBR UART1TX('\x0D');UART1TX('\x0A') //macro for line break + +#define bpBR bpWline(""); // saves 258 bytes.. \\o \o/ o// + +#define bpWBR bpBR +#define bpSP UART1TX(' ') //macro for space +#define bpWchar(x) UART1TX(x) + +// +// +// Base user terminal UART functions +// +// +#define UART_NORMAL_SPEED 34 +#define UART_FAST_SPEED 3 + +/* interrupt transfer related stuff */ +extern unsigned char *UART1RXBuf; +extern unsigned int UART1RXToRecv; +extern unsigned int UART1RXRecvd; +extern unsigned char *UART1TXBuf; +extern unsigned int UART1TXSent; +extern unsigned int UART1TXAvailable; + +// starts interrupt TX +void UART1TXInt(void); + +//is byte available in RX buffer? +unsigned char UART1RXRdy(void); +unsigned char UART1TXRdy(void); +//get a byte from UART +unsigned char UART1RX(void); +void WAITTXEmpty(void); + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c); + +void ClearCommsError(void); +unsigned char CheckCommsError(void); +// sets the uart baudrate generator +void UART1Speed(unsigned char brg); +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +void InitializeUART1(void); +unsigned char UART1TXRdy(void); +// +// +// Ring buffer for UART +// +// +void UARTbufService(void); +void UARTbufFlush(void); +void UARTbufSetup(void); +void UARTbuf(char c); +void bpWhexBuf(unsigned int c); //write a hex value to ring buffer + + +unsigned char USBUSARTIsTxTrfReady(void); +//void putUSBUSART(char *data, unsigned char length); + +// +// Hardware functions and definitions +// +// Read the lower 16 bits from programming flash memory +unsigned int bpReadFlash(unsigned int page, unsigned int addr); + +#endif + diff --git a/Firmware/baseUI.c b/Firmware/baseUI.c index 38980d5a..c7e929fc 100644 --- a/Firmware/baseUI.c +++ b/Firmware/baseUI.c @@ -1,80 +1,80 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" - -extern void bpmessages(void); - -void bpMsg(int offset, int length) -{ int i; - - for(i=offset; i<(offset+length); i++) - { if((i%3)==0) UART1TX(__builtin_tblrdl((int)&bpmessages+((i/3)<<1))&0x00FF); - if((i%3)==1) UART1TX((__builtin_tblrdl((int)&bpmessages+((i/3)<<1))>>8)&0xFF); - if((i%3)==2) UART1TX(__builtin_tblrdh((int)&bpmessages+((i/3)<<1))&0x00FF); - } - -} -void bpMsgBR(int offset, int length) -{ bpMsg(offset, length); - bpBR; -} - -// new printHelp function -// are separate functioncalls memorywise beter then 1 huge string? time would tell ;) - -void printHelp(void) -{ HLP1000; - HLP1001; - HLP1002; - HLP1003; - HLP1004; - HLP1005; - HLP1006; - HLP1007; - HLP1008; - HLP1009; - HLP1010; - HLP1011; - HLP1012; - HLP1013; - HLP1014; - HLP1015; - HLP1016; - HLP1017; - HLP1018; - HLP1019; - HLP1020; - HLP1021; - HLP1022; -} - -int agree(void) -{ char c; - - //bpWstring("Are you sure? "); - BPMSG1135; - - // JTR Not required while(!UART1RXRdy()); - c=UART1RX(); - UART1TX(c); - bpBR; - - if((c=='y')||(c=='Y')) - { return 1; - } - - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" + +extern void bpmessages(void); + +void bpMsg(int offset, int length) +{ int i; + + for(i=offset; i<(offset+length); i++) + { if((i%3)==0) UART1TX(__builtin_tblrdl((int)&bpmessages+((i/3)<<1))&0x00FF); + if((i%3)==1) UART1TX((__builtin_tblrdl((int)&bpmessages+((i/3)<<1))>>8)&0xFF); + if((i%3)==2) UART1TX(__builtin_tblrdh((int)&bpmessages+((i/3)<<1))&0x00FF); + } + +} +void bpMsgBR(int offset, int length) +{ bpMsg(offset, length); + bpBR; +} + +// new printHelp function +// are separate functioncalls memorywise beter then 1 huge string? time would tell ;) + +void printHelp(void) +{ HLP1000; + HLP1001; + HLP1002; + HLP1003; + HLP1004; + HLP1005; + HLP1006; + HLP1007; + HLP1008; + HLP1009; + HLP1010; + HLP1011; + HLP1012; + HLP1013; + HLP1014; + HLP1015; + HLP1016; + HLP1017; + HLP1018; + HLP1019; + HLP1020; + HLP1021; + HLP1022; +} + +int agree(void) +{ char c; + + //bpWstring("Are you sure? "); + BPMSG1135; + + // JTR Not required while(!UART1RXRdy()); + c=UART1RX(); + UART1TX(c); + bpBR; + + if((c=='y')||(c=='Y')) + { return 1; + } + + return 0; +} diff --git a/Firmware/baseUI.h b/Firmware/baseUI.h index c8adff7d..44004124 100644 --- a/Firmware/baseUI.h +++ b/Firmware/baseUI.h @@ -1,30 +1,30 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//print text in UImessages[s] array (member s) to the user terminal -//message text defined in /translations/*-*.h -void bpWmessage(unsigned char s); - -//print the help info in help[] to the user terminal -//message text defined in /translations/*-*.h -void printHelp(void); - -void bpMsg(int offset, int length); -void bpMsgBR(int offset, int length); - -int agree(void); //are you sure? - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//print text in UImessages[s] array (member s) to the user terminal +//message text defined in /translations/*-*.h +void bpWmessage(unsigned char s); + +//print the help info in help[] to the user terminal +//message text defined in /translations/*-*.h +void printHelp(void); + +void bpMsg(int offset, int length); +void bpMsgBR(int offset, int length); + +int agree(void); //are you sure? + + diff --git a/Firmware/basic.c b/Firmware/basic.c index 7e15e72e..e33e06e1 100644 --- a/Firmware/basic.c +++ b/Firmware/basic.c @@ -1,1675 +1,1675 @@ - -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Initial written by Chris van Dongen, 2010. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - - -#include "base.h" -#include "basic.h" -#include "AUXpin.h" -#include "busPirateCore.h" -#include "procMenu.h" -#include "bitbang.h" - - -extern struct _bpConfig bpConfig; -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; -extern proto protos[MAXPROTO]; - -#ifdef BP_USE_BASIC - -int vars[26]; // var a-z -int stack_[GOSUBMAX]; // max 5 gosubs -struct forloop forloops[FORMAX]; // max 2 nested forloop -int pc; //programcounter -int fors; // current for -int gosubs; // current gosubs -int datapos; // read pointer. - - -char *tokens[NUMTOKEN+1]= -{ STAT_LET, //0x80 - STAT_IF, //0x81 - STAT_THEN, //0x82 - STAT_ELSE, //0x83 - STAT_GOTO, //0x84 - STAT_GOSUB, //0x85 - STAT_RETURN, //0x86 - STAT_REM, //0x87 - STAT_PRINT, //0x88 - STAT_INPUT, //0x89 - STAT_FOR, //0x8A - STAT_TO, //0x8b - STAT_NEXT, //0x8c - STAT_READ, //0x8d - STAT_DATA, //0x8e - STAT_STARTR, //0x8f - STAT_START, //0x90 - STAT_STOPR, //0x91 - STAT_STOP, //0x92 - STAT_SEND, //0x93 - STAT_RECEIVE, //0x94 - STAT_CLK, //0x95 - STAT_DAT, //0x96 - STAT_BITREAD, //0x97 - STAT_ADC, //0x98 - STAT_AUXPIN, //0x99 - STAT_PSU, //0x9a - STAT_PULLUP, //0x9b - STAT_DELAY, //0x9c - STAT_AUX, //0x9d - STAT_FREQ, //0x9e - STAT_DUTY, //0x9f - - STAT_MACRO, //0xA0 - STAT_END, //0xa1 -}; - - - - -// insert your favarite basicprogram here: -unsigned char pgmspace[PGMSIZE]; /*={ // 1kb basic memory - -// basic basic test :D -#ifdef BASICTEST -TOK_LEN+10, 0, 100, TOK_REM, 'b', 'a', 's', 'i', 'c', 't', 'e', 's', 't', -TOK_LEN+ 7, 0, 110, TOK_LET, 'A', '=', 'C', '+', '1', '6', -TOK_LEN+ 6, 0, 120, TOK_FOR, 'B', '=', '1', TOK_TO, '3', -TOK_LEN+ 6, 0, 125, TOK_FOR, 'D', '=', '0', TOK_TO, '1', -TOK_LEN+23, 0, 130, TOK_PRINT, '\"', 'A', '=', '\"', ';', 'A', ';', '\"', ' ', 'B', '=', '\"', ';', 'B', ';', '\"', ' ', 'D', '=', '\"', ';', 'D', //';', -TOK_LEN+ 2, 0, 135, TOK_NEXT, 'D', -TOK_LEN+ 2, 0, 140, TOK_NEXT, 'B', -TOK_LEN+12, 0, 200, TOK_INPUT, '\"', 'E', 'n', 't', 'e', 'r', ' ', 'C', '\"', ',','C', -TOK_LEN+ 2, 0, 201, TOK_READ, 'C', -TOK_LEN+ 5, 0, 202, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 2, 0, 203, TOK_READ, 'C', -TOK_LEN+ 5, 0, 204, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 2, 0, 205, TOK_READ, 'C', -TOK_LEN+ 5, 0, 206, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 2, 0, 207, TOK_READ, 'C', -TOK_LEN+ 5, 0, 210, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+26, 0, 220, TOK_IF, 'C', '=', '2', '0', TOK_THEN, TOK_PRINT, '\"', 'C', '=', '2', '0', '!', '!', '\"', ';', TOK_ELSE, TOK_PRINT, '\"', 'C', '!', '=', '2', '0', '"', ';', -TOK_LEN+ 1, 0, 230, TOK_END, -TOK_LEN+ 7, 3, 232, TOK_PRINT, '\"', 'C', '=', '\"', ';', 'C', -TOK_LEN+ 1, 3, 242, TOK_RETURN, -TOK_LEN+ 6, 7, 208, TOK_DATA, '1', ',', '2', ',', '3', -TOK_LEN+ 3, 7, 218, TOK_DATA, '2', '0', -TOK_LEN+ 1, 255, 255, TOK_END, -#endif - - -// I2C basic test (24lc02) - -#ifdef BASICTEST_I2C -TOK_LEN+18, 0, 100, TOK_REM, 'I', '2', 'C', ' ', 't', 'e', 's', 't', ' ', '(', '2', '4', 'l', 'c', '0', '2', ')', -TOK_LEN+ 2, 0, 110, TOK_PULLUP, '1', -TOK_LEN+ 2, 0, 120, TOK_PSU, '1', -TOK_LEN+ 4, 0, 130, TOK_DELAY, '2', '5', '5', -TOK_LEN+ 1, 0, 140, TOK_STOP, -TOK_LEN+ 5, 0, 150, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 1, 0, 200, TOK_START, -TOK_LEN+ 4, 0, 210, TOK_SEND, '1', '6', '0', -TOK_LEN+ 2, 0, 220, TOK_SEND, '0', -TOK_LEN+ 6, 0, 230, TOK_FOR, 'A', '=', '1', TOK_TO, '8', -TOK_LEN+ 2, 0, 240, TOK_READ, 'B', -TOK_LEN+ 2, 0, 250, TOK_SEND, 'B', -TOK_LEN+ 2, 0, 200, TOK_NEXT, 'A', -TOK_LEN+ 1, 1, 4, TOK_STOP, -TOK_LEN+ 4, 1, 14, TOK_DELAY, '2', '5', '5', -TOK_LEN+ 5, 1, 24, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 2, 1, 34, TOK_PSU, '0', -TOK_LEN+ 2, 1, 44, TOK_PULLUP, '0', -TOK_LEN+ 1, 1, 54, TOK_END, -TOK_LEN+13, 3, 232, TOK_REM, 'D', 'u', 'm', 'p', ' ', '8', ' ', 'b', 'y', 't', 'e', 's', -TOK_LEN+ 1, 3, 242, TOK_START, -TOK_LEN+ 4, 3, 252, TOK_SEND, '1', '6', '0', -TOK_LEN+ 2, 4, 6, TOK_SEND, '0', -TOK_LEN+ 1, 4, 16, TOK_START, -TOK_LEN+ 4, 4, 26, TOK_SEND, '1', '6', '1', -TOK_LEN+ 7, 4, 36, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 46, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 56, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 66, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 76, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 86, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 7, 4, 96, TOK_PRINT, TOK_RECEIVE, ';', '"', ' ', '"', ';', -TOK_LEN+ 2, 4, 106, TOK_PRINT, TOK_RECEIVE, -TOK_LEN+ 1, 4, 116, TOK_STOP, -TOK_LEN+ 1, 4, 116, TOK_RETURN, -TOK_LEN+16, 7, 208, TOK_DATA, '2', '5' ,'5', ',', '2', '5' ,'5', ',','2', '5' ,'5', ',','2', '5' ,'5', -TOK_LEN+16, 7, 218, TOK_DATA, '2', '5' ,'5', ',', '2', '5' ,'5', ',','2', '5' ,'5', ',','2', '5' ,'5', -#endif - -// UART test (serial rfid reader from seed) -#ifdef BASICTEST_UART -TOK_LEN+15, 0, 100, TOK_REM, 'U', 'A', 'R', 'T', ' ', 't', 'e', 's', 't', ' ', 'r', 'f', 'i', 'd', -TOK_LEN+ 2, 0, 110, TOK_PSU, '1', -TOK_LEN+ 4, 0, 120, TOK_DELAY, '2', '5', '5', -TOK_LEN+ 5, 0, 130, TOK_GOSUB, '1', '0', '0', '0', -TOK_LEN+ 3, 0, 135, TOK_DELAY, '1', '0', -//TOK_LEN+12, 0, 140, TOK_IF, TOK_RECEIVE, '!', '=', '5', '2', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 145, TOK_DELAY, '1', -//TOK_LEN+12, 0, 150, TOK_IF, TOK_RECEIVE, '!', '=', '5', '4', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 155, TOK_DELAY, '1', -//TOK_LEN+12, 0, 160, TOK_IF, TOK_RECEIVE, '!', '=', '4', '8', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 165, TOK_DELAY, '1', -//TOK_LEN+12, 0, 170, TOK_IF, TOK_RECEIVE, '!', '=', '4', '8', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 175, TOK_DELAY, '1', -//TOK_LEN+12, 0, 180, TOK_IF, TOK_RECEIVE, '!', '=', '5', '4', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 185, TOK_DELAY, '1', -//TOK_LEN+12, 0, 190, TOK_IF, TOK_RECEIVE, '!', '=', '5', '3', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 195, TOK_DELAY, '1', -//TOK_LEN+12, 0, 200, TOK_IF, TOK_RECEIVE, '!', '=', '5', '5', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 205, TOK_DELAY, '1', -//TOK_LEN+12, 0, 210, TOK_IF, TOK_RECEIVE, '!', '=', '5', '5', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 215, TOK_DELAY, '1', -//TOK_LEN+12, 0, 220, TOK_IF, TOK_RECEIVE, '!', '=', '5', '7', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 225, TOK_DELAY, '1', -//TOK_LEN+12, 0, 230, TOK_IF, TOK_RECEIVE, '!', '=', '5', '4', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 235, TOK_DELAY, '1', -//TOK_LEN+12, 0, 240, TOK_IF, TOK_RECEIVE, '!', '=', '6', '7', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -//TOK_LEN+ 2, 0, 245, TOK_DELAY, '1', -//TOK_LEN+12, 0, 250, TOK_IF, TOK_RECEIVE, '!', '=', '5', '0', TOK_THEN, TOK_GOTO, '2', '0', '2', '0', -TOK_LEN+ 6, 0, 140, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 145, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 150, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 155, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 160, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 165, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 170, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 175, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 180, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 185, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 190, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 195, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 200, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 205, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 210, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 215, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 220, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 225, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 230, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 235, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 240, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 3, 0, 245, TOK_DELAY, '5', '0', -TOK_LEN+ 6, 0, 250, TOK_PRINT, TOK_RECEIVE, '"', ' ', '"', ';', -//TOK_LEN+ 5, 1, 4, TOK_GOTO, '2', '0', '0', '0', -TOK_LEN+ 4, 1, 14, TOK_GOTO, '1', '3', '0', -TOK_LEN+13, 3, 232, TOK_REM, 'W', 'a', 'i', 't', ' ', 'f', 'o', 'r', ' ', 'S', 'T', 'X', -TOK_LEN+ 4, 3, 242, TOK_LET, 'A', '=', TOK_RECEIVE, -TOK_LEN+12, 3, 252, TOK_IF, 'A', '=', '2', TOK_THEN, TOK_RETURN, TOK_ELSE, TOK_GOTO, '1', '0', '1', '0', -//TOK_LEN+ 8, 7, 208, TOK_PRINT, '"', 'V', 'A', 'L', 'I', 'D', '"', -//TOK_LEN+ 4, 7, 218, TOK_GOTO, '1', '3', '0', -//TOK_LEN+10, 7, 228, TOK_PRINT, '"', 'I', 'N', 'V', 'A', 'L', 'I', 'D', '"', -//TOK_LEN+ 4, 7, 238, TOK_GOTO, '1', '3', '0', -#endif - -// raw3wire test (atiny85) -#ifdef BASICTEST_R3W -TOK_LEN+22, 0, 10, TOK_REM, 'r', '2', 'w', 'i', 'r', 'e', ' ', 't', 'e', 's', 't', ' ', '(', 'a', 't', 'i', 'n', 'y', '8', '5', ')', -TOK_LEN+ 2, 0, 100, TOK_PULLUP, '1', -TOK_LEN+ 2, 0, 110, TOK_CLK, '0', -TOK_LEN+ 2, 0, 120, TOK_DAT, '0', -TOK_LEN+ 2, 0, 130, TOK_AUX, '0', -TOK_LEN+ 2, 0, 140, TOK_PSU, '1', -TOK_LEN+ 4, 0, 150, TOK_DELAY, '2', '5', '5', -TOK_LEN+ 1, 0, 160, TOK_STARTR, -TOK_LEN+ 7, 0, 170, TOK_LET, 'A', '=', TOK_SEND, '1', '7', '2', -TOK_LEN+ 6, 0, 180, TOK_LET, 'B', '=', TOK_SEND, '8', '3', -TOK_LEN+ 5, 0, 190, TOK_LET, 'C', '=', TOK_SEND, '0', -TOK_LEN+ 5, 0, 200, TOK_LET, 'D', '=', TOK_SEND, '0', -TOK_LEN+ 8, 0, 210, TOK_IF, 'C', '!', '=', '8', '3', TOK_THEN, TOK_END, -TOK_LEN+15, 0, 220, TOK_PRINT, '"', 'F', 'O', 'U', 'N', 'D', ' ', 'D', 'E', 'V', 'I', 'C', 'E', '"', -TOK_LEN+13, 0, 230, TOK_PRINT, '"', 'd', 'e', 'v', 'i', 'c', 'e', 'I', 'D', ':', '"', ';', -TOK_LEN+ 6, 0, 240, TOK_LET, 'A', '=', TOK_SEND, '4', '8', -TOK_LEN+ 5, 0, 250, TOK_LET, 'B', '=', TOK_SEND, '0', -TOK_LEN+ 5, 1, 4, TOK_LET, 'C', '=', TOK_SEND, '0', -TOK_LEN+ 5, 1, 14, TOK_LET, 'D', '=', TOK_SEND, '0', -TOK_LEN+ 7, 1, 24, TOK_PRINT, 'D', ';', '"', ' ', '"', ';', -TOK_LEN+ 6, 1, 34, TOK_LET, 'A', '=', TOK_SEND, '4', '8', -TOK_LEN+ 5, 1, 44, TOK_LET, 'B', '=', TOK_SEND, '0', -TOK_LEN+ 5, 1, 54, TOK_LET, 'C', '=', TOK_SEND, '1', -TOK_LEN+ 5, 1, 64, TOK_LET, 'D', '=', TOK_SEND, '0', -TOK_LEN+ 7, 1, 74, TOK_PRINT, 'D', ';', '"', ' ', '"', ';', -TOK_LEN+ 6, 1, 84, TOK_LET, 'A', '=', TOK_SEND, '4', '8', -TOK_LEN+ 5, 1, 94, TOK_LET, 'B', '=', TOK_SEND, '0', -TOK_LEN+ 5, 1, 104, TOK_LET, 'C', '=', TOK_SEND, '2', -TOK_LEN+ 5, 1, 114, TOK_LET, 'D', '=', TOK_SEND, '0', -TOK_LEN+ 2, 1, 124, TOK_PRINT, 'D', -TOK_LEN+14, 1, 134, TOK_PRINT, '"', 'd', 'e', 'v', 'i', 'c', 'e', ' ', 'i', 's', ' ', '"', ';', -TOK_LEN+ 6, 1, 84, TOK_LET, 'A', '=', TOK_SEND, '8', '8', -TOK_LEN+ 5, 1, 94, TOK_LET, 'B', '=', TOK_SEND, '0', -TOK_LEN+ 5, 1, 104, TOK_LET, 'C', '=', TOK_SEND, '2', -TOK_LEN+ 5, 1, 114, TOK_LET, 'D', '=', TOK_SEND, '0', -TOK_LEN+26, 1, 124, TOK_IF, 'D', '=', '3', TOK_THEN, TOK_PRINT, '"', 'U', 'N', 'L', 'O', 'C', 'K', 'E', 'D', '"', TOK_ELSE, TOK_PRINT, '"', 'L', 'O', 'C', 'K', 'E', 'D','"', -TOK_LEN+ 1, 1, 134, TOK_END, -#endif - -#ifdef BASICTEST_PIC10 - -TOK_LEN+15, 0, 100, TOK_REM, 'P', 'R', 'O', 'G', 'R', 'A', 'M', ' ', 'P', 'I', 'C', '1', '0', 'F', -TOK_LEN+ 7, 0, 110, TOK_FOR, 'A', '=', '1', TOK_TO, '2', '4', -TOK_LEN+ 1, 0, 120, TOK_START, -TOK_LEN+ 2, 0, 130, TOK_SEND, '2', -TOK_LEN+ 1, 0, 140, TOK_STOP, -TOK_LEN+ 2, 0, 150, TOK_READ, 'B', -TOK_LEN+ 2, 0, 160, TOK_SEND, 'B', -TOK_LEN+ 1, 0, 170, TOK_START, -TOK_LEN+ 2, 0, 180, TOK_SEND, '8', -TOK_LEN+ 2, 0, 190, TOK_DELAY, '2', -TOK_LEN+ 3, 0, 200, TOK_SEND, '1', '4', -TOK_LEN+ 2, 0, 210, TOK_SEND, '6', -TOK_LEN+ 1, 0, 210, TOK_STOP, -TOK_LEN+ 2, 0, 220, TOK_NEXT, 'A', -TOK_LEN+ 1, 0, 230, TOK_START, -TOK_LEN+ 9, 0, 240, TOK_FOR, 'A', '=', '2', '5', TOK_TO, '5', '1', '2', -TOK_LEN+ 2, 0, 250, TOK_SEND, '6', -TOK_LEN+ 2, 1, 4, TOK_NEXT, 'A', -TOK_LEN+ 2, 1, 14, TOK_SEND, '2', -TOK_LEN+ 1, 1, 24, TOK_STOP, -TOK_LEN+ 2, 1, 34, TOK_READ, 'B', -TOK_LEN+ 2, 1, 44, TOK_SEND, 'B', -TOK_LEN+ 1, 1, 54, TOK_START, -TOK_LEN+ 2, 1, 64, TOK_SEND, '8', -TOK_LEN+ 2, 1, 74, TOK_DELAY, '2', -TOK_LEN+ 3, 1, 84, TOK_SEND, '1', '4', -TOK_LEN+ 1, 1, 94, TOK_STOP, -TOK_LEN+ 1, 1, 104, TOK_END, -//TOK_LEN+11, 3, 232, TOK_REM, 'C', 'O', 'N', 'F', 'I', 'G', 'W', 'O', 'R', 'D', -TOK_LEN+ 5, 3, 232, TOK_DATA, '4', '0', '7', '5', -//TOK_LEN+ 5, 7, 208, TOK_REM, 'M', 'A', 'I', 'N', -TOK_LEN+26, 7, 208, TOK_DATA, '3', '7', ',', '1', '0', '2', '9', ',', '2', '5', '7', '3', ',', '3', '3', '2', '2', ',', '4', '9', ',', '3', '3', '2', '7', -TOK_LEN+26, 7, 218, TOK_DATA, '5', '0', ',', '7', '5', '3', ',', '2', '5', '7', '0', ',', '2', '0', '4', '8', ',', '7', '5', '4', ',', '2', '5', '7', '0', -TOK_LEN+25, 7, 228, TOK_DATA, '2', '5', '6', '7', ',', '3', '2', '6', '4', ',', '2', ',', '3', '0', '7', '2', ',', '3', '8', ',', '3', '3', '2', '3', -TOK_LEN+21, 7, 238, TOK_DATA, '6', ',', '3', '0', '7', '6', ',', '4', '2', '2', ',', '2', '3', '0', '7', ',', '2', '5', '7', '9', -//TOK_LEN+ 7,11, 184, TOK_REM, 'O', 'S', 'C', 'C', 'A', 'L', -TOK_LEN+ 5,11, 184, TOK_DATA, '3', '0', '9', '6', - -// data statements and rems aren't mixing well! -#endif -#ifdef BASICTEST_PIC10_2 - -TOK_LEN+12, 0, 100, TOK_REM, 'D', 'U', 'M', 'P', ' ', 'P', 'I', 'C', '1', '0', 'F', -TOK_LEN+ 8, 0, 110, TOK_FOR, 'A', '=', '1', TOK_TO, '5', '1', '8', -TOK_LEN+ 1, 0, 120, TOK_START, -TOK_LEN+ 2, 0, 130, TOK_SEND, '4', -TOK_LEN+ 1, 0, 140, TOK_STOP, -TOK_LEN+ 7, 0, 150, TOK_PRINT, TOK_RECEIVE, ';', '"', ',', '"', ';', -TOK_LEN+ 1, 0, 160, TOK_START, -TOK_LEN+ 2, 0, 170, TOK_SEND, '6', -TOK_LEN+ 2, 0, 180, TOK_NEXT, 'A', -TOK_LEN+ 1, 0, 190, TOK_END, -#endif - - -0x00,0x00, -}; -*/ - - - -// basicinterpreter starts here - -void handleelse(void) -{ if(pgmspace[pc]==TOK_ELSE) - { pc++; - while(pgmspace[pc]<=TOK_LEN) - { pc++; - } - } -} - -int searchlineno(unsigned int line) -{ int i; - int len; - int lineno; - - i=0; - - //bpWintdec(line); - //UART1TX('?'); - - while(1) - { if(pgmspace[i]<=TOK_LEN) - { return -1; - } - len=pgmspace[i]-TOK_LEN; - lineno=(pgmspace[i+1]<<8)+pgmspace[i+2]; -// if(i>PGMSIZE) -// { return -1; -// } - if(line==lineno) - { return i; - } - i+=len+3; - //bpWintdec(i); bpSP; - //bpWintdec(lineno); bpSP; - } - return -1; -} - -int getnumvar(void) -{ int temp; - temp=0; - - if((pgmspace[pc]=='(')) - { - pc++; - temp=assign(); -// temp=evaluate(); - if((pgmspace[pc]==')')) - { pc++; - } - } - else if((pgmspace[pc]>='A')&&(pgmspace[pc]<='Z')) - { //bpWstring("var "); - //bpWhex(pgmspace[pc]); - //bpSP; - return vars[pgmspace[pc++]-'A']; //increment pc - } - else if(pgmspace[pc]>TOKENS) // looks for tokens like aux, clk and dat - { switch(pgmspace[pc++]) // increment pc - { case TOK_RECEIVE: //temp=protoread(); - temp=protos[bpConfig.busMode].protocol_read(); - break; - case TOK_SEND: //temp=protoread(); - temp=protos[bpConfig.busMode].protocol_send(assign()); - break; - case TOK_AUX: //temp=protogetaux(); - temp=bpAuxRead(); - break; - case TOK_DAT: //temp=protogetdat(); - temp=protos[bpConfig.busMode].protocol_dats(); - break; - case TOK_BITREAD: //temp=protobitr(); - temp=protos[bpConfig.busMode].protocol_bitr(); - break; - case TOK_PSU: temp=BP_VREGEN; //modeConfig.vregEN; - break; -#ifndef BUSPIRATEV1A - case TOK_PULLUP: temp=(~BP_PULLUP); //modeConfig.pullupEN; - break; -#endif - case TOK_ADC: //temp=bpADC(BP_ADC_PROBE); - ADCON(); // turn ADC ON - temp=bpADC(BP_ADC_PROBE); - ADCOFF(); // turn ADC OFF - -// temp=1234; - break; - default: temp=0; - } - } - else - { while((pgmspace[pc]>='0')&&(pgmspace[pc]<='9')) - { temp*=10; - temp+=pgmspace[pc]-'0'; - pc++; - } - } - //bpWstring("int "); - //bpWinthex(temp); - //bpSP; - return temp; -} - -int getmultdiv(void) -{ int temp; - temp=getnumvar(); - while (1) { - if((pgmspace[pc]!='*')&&(pgmspace[pc]!='/')&&(pgmspace[pc]!='&')&&(pgmspace[pc]!='|')) - { return temp; - } - else // assume operand - { //bpWstring("op "); - //UART1TX(pgmspace[pc]); - //bpSP; - switch(pgmspace[pc++]) - { case '*': //UART1TX('*'); - temp*=getnumvar(); - break; - case '/': //UART1TX('/'); - temp/=getnumvar(); - break; - case '&': //UART1TX('/'); - temp&=getnumvar(); - break; - case '|': //UART1TX('/'); - temp|=getnumvar(); - break; - default: break; - } - } - } - - -} - -int assign(void) -{ unsigned int temp; - - //pc+=2; - temp=getmultdiv(); - - while (1) { - if((pgmspace[pc]!='-')&&(pgmspace[pc]!='+')&&(pgmspace[pc]!='<')&&(pgmspace[pc]!='>')&&(pgmspace[pc]!='=')) - { return temp; - } - else // assume operand - { //bpWstring("op "); - //UART1TX(pgmspace[pc]); - //bpSP; - switch(pgmspace[pc++]) - { case '-': //UART1TX('-'); - temp-=getmultdiv(); - break; - case '+': //UART1TX('+'); - temp+=getmultdiv(); - break; - case '>': //UART1TX('+'); - if(pgmspace[pc+1]=='=') - { temp=(temp>=getmultdiv()?1:0); - pc++; - } - else - { temp=(temp>getmultdiv()?1:0); - } - break; - case '<': //UART1TX('+'); - if(pgmspace[pc+1]=='>') - { temp=(temp!=getmultdiv()?1:0); - pc++; - } - else if(pgmspace[pc+1]=='=') - { temp=(temp<=getmultdiv()?1:0); - pc++; - } - else - { temp=(temp='A')&&(pgmspace[pc]<='Z')))&&(!((pgmspace[pc]>='0')&&(pgmspace[pc]<='9')))) - { op[i]=pgmspace[pc++]; - i++; - } - - right=assign(); - - //bpSP; - //bpWinthex(left); bpSP; - //UART1TX(op[0]); - //UART1TX(op[1]); bpSP; - //bpWinthex(right); bpSP; - - - switch (op[0]) - { case '=': return (left==right); - case '>': if(op[1]=='=') - { return (left>=right); - } - else - { return (left>right); - } - case '<': if(op[1]=='=') - { return (left<=right); - } - else - { return (left='A')&&(pgmspace[pc]<='Z'))||((pgmspace[pc]>=TOKENS)&&(pgmspace[pc](1+i)) - //{ UART1TX(pgmspace[pc+4+i]); // print to - // i++; - //} - - pcupdated=1; - pc+=4; - if(fors=TOK_LEN) - { forloops[fors].forstart=pc; - } - else - { stop=SYNTAXERROR; - } - //bpSP; - //bpWinthex(forloops[fors].var); bpSP; - //bpWinthex(forloops[fors].to); bpSP; - //bpWinthex(forloops[fors].forstart); bpSP; - //bpBR; - - handleelse(); - - break; - case TOK_NEXT: //bpWstring(STAT_NEXT); - //bpSP; - //for(i=4; iTOK_LEN) - { if(pgmspace[datapos+3]!=TOK_DATA) - { datapos=0; // rolover - } - else - { datapos+=4; - } - } - - handleelse(); - - break; - case TOK_DATA: pcupdated=1; - pc+=len+3; - break; - - -// buspirate subs - - case TOK_START: pcupdated=1; - pc+=4; - - protos[bpConfig.busMode].protocol_start(); - handleelse(); - //protostart(); - break; - case TOK_STARTR: pcupdated=1; - pc+=4; - - protos[bpConfig.busMode].protocol_startR(); - //protostartr(); - handleelse(); - break; - case TOK_STOP: pcupdated=1; - pc+=4; - - protos[bpConfig.busMode].protocol_stop(); - //protostop(); - handleelse(); - break; - case TOK_STOPR: pcupdated=1; - pc+=4; - - protos[bpConfig.busMode].protocol_stopR(); - //protostopr(); - handleelse(); - break; - case TOK_SEND: pcupdated=1; - pc+=4; - //protowrite(getnumvar()); - protos[bpConfig.busMode].protocol_send((int)assign()); - handleelse(); - break; - case TOK_AUX: pcupdated=1; - pc+=4; - - if(assign()) - { //protoauxh(); - bpAuxHigh(); - } - else - { //protoauxl(); - bpAuxLow(); - } - handleelse(); - - break; - case TOK_PSU: pcupdated=1; - pc+=4; - - if(assign()) - { //protopsuon(); - BP_VREG_ON(); - //modeConfig.vregEN=1; - } - else - { //protopsuoff(); - BP_VREG_OFF(); - //modeConfig.vregEN=0; - } - handleelse(); - - break; - - case TOK_AUXPIN: pcupdated=1; - pc+=4; - - if(assign()) - { modeConfig.altAUX=1; - } - else - { modeConfig.altAUX=1; - } - handleelse(); - - break; - case TOK_FREQ: pcupdated=1; - pc+=4; - - PWMfreq=assign(); - if(PWMfreq<0) PWMfreq=0; - if(PWMfreq>4000) PWMfreq=4000; - if(PWMduty<2) PWMduty=2; - if(PWMduty>99) PWMduty=99; - - updatePWM(); - handleelse(); - - break; - case TOK_DUTY: pcupdated=1; - pc+=4; - - PWMduty=assign(); - if(PWMfreq<0) PWMfreq=0; - if(PWMfreq>4000) PWMfreq=4000; - if(PWMduty<2) PWMduty=2; - if(PWMduty>99) PWMduty=99; - - updatePWM(); - handleelse(); - - break; - - - case TOK_DAT: pcupdated=1; - pc+=4; - - if(assign()) - { //protodath(); - protos[bpConfig.busMode].protocol_dath(); - } - else - { //protodatl(); - protos[bpConfig.busMode].protocol_datl(); - } - handleelse(); - - break; - case TOK_CLK: pcupdated=1; - pc+=4; - - switch(assign()) - { case 0: //protoclkl(); - protos[bpConfig.busMode].protocol_clkl(); - break; - case 1: //protoclkh(); - protos[bpConfig.busMode].protocol_clkh(); - break; - case 2: //protoclk(); - protos[bpConfig.busMode].protocol_clk(); - break; - } - handleelse(); - - break; - case TOK_PULLUP: pcupdated=1; - pc+=4; - -#ifndef BUSPIRATEV1A -//#if(0) - if(assign()) - { //protopullupon(); - BP_PULLUP_ON(); - //modeConfig.pullupEN=1; - } - else - { //protopullupoff(); - BP_PULLUP_OFF(); - //modeConfig.pullupEN=0; - } -#else - pc+=len-1; // fail silently -#endif - handleelse(); - - break; - case TOK_DELAY: pcupdated=1; - pc+=4; - temp=assign(); - bpDelayMS(temp); - handleelse(); - - break; - case TOK_MACRO: pcupdated=1; - pc+=4; - temp=assign(); - protos[bpConfig.busMode].protocol_macro(temp); - handleelse(); - break; - case TOK_END: //bpWstring(STAT_END); - stop=1; - break; - default: stop=SYNTAXERROR; - break; - } - - if(!pcupdated) - { pc+=len+3; - //bpBR; - } - pcupdated=0; - } - - bpConfig.quiet=0; // display on - - if(stop!=NOERROR) - { //bpWstring("Error("); - BPMSG1047; - bpWintdec(stop); - //bpWstring(") @line:"); - BPMSG1048; - bpWintdec(lineno); - //bpWstring(" @pgmspace:"); - BPMSG1049; - bpWintdec(pc); - bpBR; - } - - - //bpWinthex(vars[0]); bpSP; - //bpWinthex(vars[1]); bpSP; - //bpWinthex(vars[2]); bpSP; -} - -void printstat(char *s) -{ bpSP; - bpWstring(s); - bpSP; -} - -void list(void) -{ unsigned char c; - unsigned int lineno; - - pc=0; - - while(pgmspace[pc]) - { c=pgmspace[pc]; - if(cTOK_LEN) - { bpBR; - //bpWintdec(pc); bpSP; - lineno=(pgmspace[pc+1]<<8)+pgmspace[pc+2]; - pc+=2; - bpWintdec(lineno); - bpSP; - } - else - { bpSP; - bpWstring(tokens[c-TOKENS]); - bpSP; - } - pc++; - } - bpBR; - bpWintdec(pc-1); - //bpWline(" bytes."); - BPMSG1050; -} - -int compare(char *p) -{ int oldstart; - - oldstart=cmdstart; - while(*p) - { if(*p!=cmdbuf[cmdstart]) - { cmdstart=oldstart; - return 0; - } - cmdstart=(cmdstart+1)&CMDLENMSK; - - p++; - } - return 1; -} - -unsigned char gettoken(void) -{ int i; - - for(i=0; i='a')&&(cmdbuf[i]<='z')) cmdbuf[i]&=0xDF; - i++; - i&=CMDLENMSK; - } - - i=0; - // command or a new line? - if((cmdbuf[cmdstart]>='0')&&(cmdbuf[cmdstart]<='9')) - { //bpWline("new line"); - - for(i=0; i<35; i++) - { line[i]=0; - } - - temp=getint(); - line[1]=temp>>8; - line[2]=temp&0xFF; - - //bpWstring("search for line "); - //bpWintdec(temp); bpBR; - - pos=searchlineno(temp); - //bpWstring("pos="); - //bpWintdec(pos); bpSP; - if(pos!=-1) // if it already exist remove it first - { //bpWstring("replace/remove line @"); - //bpWintdec(pos); bpBR - len=(pgmspace[pos]-TOK_LEN)+3; - //bpWstring("pos="); - //bpWintdec(pos); bpSP; - for(i=pos; i35) - { //bpWline("Too long!"); - BPMSG1051; - return; - } - } - //bpWstring("i="); - //bpWintdec(i); bpSP; - - if(i==3) return; // no need to insert an empty line - if(i==4) return; // no need to insert an empty line - - line[0]=TOK_LEN+(i-4); - - //UART1TX('['); - //for(i=0; i<35; i++) - //{ UART1TX(line[i]); - //} - //UART1TX(']'); - - i=0; - end=0; - pos=0; - - while(!end) - { if(pgmspace[i]>TOK_LEN) // valid line - { len=pgmspace[i]-TOK_LEN; - //bpWstring("len="); - //bpWintdec(len); bpSP; - - lineno1=(pgmspace[i+1]<<8)+pgmspace[i+2]; - lineno2=(line[1]<<8)+line[2]; - if(lineno1>=1; - } - - HIZbbH(BASSCL, BASI2CCLK); - i=HIZbbR(BASSDA); - HIZbbL(BASSCL, BASI2CCLK); - - return (i^0x01); -} - -int checkeeprom(void) -{ // just to be sure - basi2cstop(); - basi2cstop(); - basi2cstart(); - if(!basi2cwrite(I2CADDR)) - { //bpWline("No EEPROM"); - BPMSG1053; - return 0; - } - basi2cwrite(0x00); - basi2cwrite(0x00); - basi2cstart(); - if(basi2cread(1)==0x00) // check for any data - { bpWline("No EEPROM"); // if 0 prolly no pullup and eeprom (PROLLY!) - BPMSG1053; - return 0; - } - basi2cstop(); - return 1; -} - -void format(void) -{ int i,j; - - basi2cstop(); - basi2cstart(); - if(!basi2cwrite(I2CADDR)) - { //bpWline("No EEPROM"); - BPMSG1053; - return; - } - basi2cstop(); - - //bpWstring("Erasing"); - BPMSG1054; - for(i=0; i>8)); - basi2cwrite((i&0x0FF)); - for(j=0; j(EEPROMSIZE/PGMSIZE)) - { //bpWline("Invalid slot"); - BPMSG1057; - return; - } - - if(!checkeeprom()) - { return; - } - - slot*=PGMSIZE; - - basi2cstop(); - basi2cwrite(I2CADDR); - basi2cwrite(slot>>8); - basi2cwrite(slot&0x0FF); - basi2cstart(); - basi2cwrite(I2CADDR+1); - - slot*=EEPROMPAGE; - - for(i=0; i>8); - basi2cwrite((slot+i)&0x0FF); - for(j=0; j(EEPROMSIZE/PGMSIZE)) - { //bpWline("Invalid slot"); - BPMSG1057; - return; - } - - if(!checkeeprom()) - { return; - } - - slot*=PGMSIZE; - - basi2cstop(); - basi2cwrite(I2CADDR); - basi2cwrite(slot>>8); - basi2cwrite(slot&0x0FF); - basi2cstart(); - basi2cwrite(I2CADDR+1); - - for(i=0; iPGMSIZE) +// { return -1; +// } + if(line==lineno) + { return i; + } + i+=len+3; + //bpWintdec(i); bpSP; + //bpWintdec(lineno); bpSP; + } + return -1; +} + +int getnumvar(void) +{ int temp; + temp=0; + + if((pgmspace[pc]=='(')) + { + pc++; + temp=assign(); +// temp=evaluate(); + if((pgmspace[pc]==')')) + { pc++; + } + } + else if((pgmspace[pc]>='A')&&(pgmspace[pc]<='Z')) + { //bpWstring("var "); + //bpWhex(pgmspace[pc]); + //bpSP; + return vars[pgmspace[pc++]-'A']; //increment pc + } + else if(pgmspace[pc]>TOKENS) // looks for tokens like aux, clk and dat + { switch(pgmspace[pc++]) // increment pc + { case TOK_RECEIVE: //temp=protoread(); + temp=protos[bpConfig.busMode].protocol_read(); + break; + case TOK_SEND: //temp=protoread(); + temp=protos[bpConfig.busMode].protocol_send(assign()); + break; + case TOK_AUX: //temp=protogetaux(); + temp=bpAuxRead(); + break; + case TOK_DAT: //temp=protogetdat(); + temp=protos[bpConfig.busMode].protocol_dats(); + break; + case TOK_BITREAD: //temp=protobitr(); + temp=protos[bpConfig.busMode].protocol_bitr(); + break; + case TOK_PSU: temp=BP_VREGEN; //modeConfig.vregEN; + break; +#ifndef BUSPIRATEV1A + case TOK_PULLUP: temp=(~BP_PULLUP); //modeConfig.pullupEN; + break; +#endif + case TOK_ADC: //temp=bpADC(BP_ADC_PROBE); + ADCON(); // turn ADC ON + temp=bpADC(BP_ADC_PROBE); + ADCOFF(); // turn ADC OFF + +// temp=1234; + break; + default: temp=0; + } + } + else + { while((pgmspace[pc]>='0')&&(pgmspace[pc]<='9')) + { temp*=10; + temp+=pgmspace[pc]-'0'; + pc++; + } + } + //bpWstring("int "); + //bpWinthex(temp); + //bpSP; + return temp; +} + +int getmultdiv(void) +{ int temp; + temp=getnumvar(); + while (1) { + if((pgmspace[pc]!='*')&&(pgmspace[pc]!='/')&&(pgmspace[pc]!='&')&&(pgmspace[pc]!='|')) + { return temp; + } + else // assume operand + { //bpWstring("op "); + //UART1TX(pgmspace[pc]); + //bpSP; + switch(pgmspace[pc++]) + { case '*': //UART1TX('*'); + temp*=getnumvar(); + break; + case '/': //UART1TX('/'); + temp/=getnumvar(); + break; + case '&': //UART1TX('/'); + temp&=getnumvar(); + break; + case '|': //UART1TX('/'); + temp|=getnumvar(); + break; + default: break; + } + } + } + + +} + +int assign(void) +{ unsigned int temp; + + //pc+=2; + temp=getmultdiv(); + + while (1) { + if((pgmspace[pc]!='-')&&(pgmspace[pc]!='+')&&(pgmspace[pc]!='<')&&(pgmspace[pc]!='>')&&(pgmspace[pc]!='=')) + { return temp; + } + else // assume operand + { //bpWstring("op "); + //UART1TX(pgmspace[pc]); + //bpSP; + switch(pgmspace[pc++]) + { case '-': //UART1TX('-'); + temp-=getmultdiv(); + break; + case '+': //UART1TX('+'); + temp+=getmultdiv(); + break; + case '>': //UART1TX('+'); + if(pgmspace[pc+1]=='=') + { temp=(temp>=getmultdiv()?1:0); + pc++; + } + else + { temp=(temp>getmultdiv()?1:0); + } + break; + case '<': //UART1TX('+'); + if(pgmspace[pc+1]=='>') + { temp=(temp!=getmultdiv()?1:0); + pc++; + } + else if(pgmspace[pc+1]=='=') + { temp=(temp<=getmultdiv()?1:0); + pc++; + } + else + { temp=(temp='A')&&(pgmspace[pc]<='Z')))&&(!((pgmspace[pc]>='0')&&(pgmspace[pc]<='9')))) + { op[i]=pgmspace[pc++]; + i++; + } + + right=assign(); + + //bpSP; + //bpWinthex(left); bpSP; + //UART1TX(op[0]); + //UART1TX(op[1]); bpSP; + //bpWinthex(right); bpSP; + + + switch (op[0]) + { case '=': return (left==right); + case '>': if(op[1]=='=') + { return (left>=right); + } + else + { return (left>right); + } + case '<': if(op[1]=='=') + { return (left<=right); + } + else + { return (left='A')&&(pgmspace[pc]<='Z'))||((pgmspace[pc]>=TOKENS)&&(pgmspace[pc](1+i)) + //{ UART1TX(pgmspace[pc+4+i]); // print to + // i++; + //} + + pcupdated=1; + pc+=4; + if(fors=TOK_LEN) + { forloops[fors].forstart=pc; + } + else + { stop=SYNTAXERROR; + } + //bpSP; + //bpWinthex(forloops[fors].var); bpSP; + //bpWinthex(forloops[fors].to); bpSP; + //bpWinthex(forloops[fors].forstart); bpSP; + //bpBR; + + handleelse(); + + break; + case TOK_NEXT: //bpWstring(STAT_NEXT); + //bpSP; + //for(i=4; iTOK_LEN) + { if(pgmspace[datapos+3]!=TOK_DATA) + { datapos=0; // rolover + } + else + { datapos+=4; + } + } + + handleelse(); + + break; + case TOK_DATA: pcupdated=1; + pc+=len+3; + break; + + +// buspirate subs + + case TOK_START: pcupdated=1; + pc+=4; + + protos[bpConfig.busMode].protocol_start(); + handleelse(); + //protostart(); + break; + case TOK_STARTR: pcupdated=1; + pc+=4; + + protos[bpConfig.busMode].protocol_startR(); + //protostartr(); + handleelse(); + break; + case TOK_STOP: pcupdated=1; + pc+=4; + + protos[bpConfig.busMode].protocol_stop(); + //protostop(); + handleelse(); + break; + case TOK_STOPR: pcupdated=1; + pc+=4; + + protos[bpConfig.busMode].protocol_stopR(); + //protostopr(); + handleelse(); + break; + case TOK_SEND: pcupdated=1; + pc+=4; + //protowrite(getnumvar()); + protos[bpConfig.busMode].protocol_send((int)assign()); + handleelse(); + break; + case TOK_AUX: pcupdated=1; + pc+=4; + + if(assign()) + { //protoauxh(); + bpAuxHigh(); + } + else + { //protoauxl(); + bpAuxLow(); + } + handleelse(); + + break; + case TOK_PSU: pcupdated=1; + pc+=4; + + if(assign()) + { //protopsuon(); + BP_VREG_ON(); + //modeConfig.vregEN=1; + } + else + { //protopsuoff(); + BP_VREG_OFF(); + //modeConfig.vregEN=0; + } + handleelse(); + + break; + + case TOK_AUXPIN: pcupdated=1; + pc+=4; + + if(assign()) + { modeConfig.altAUX=1; + } + else + { modeConfig.altAUX=1; + } + handleelse(); + + break; + case TOK_FREQ: pcupdated=1; + pc+=4; + + PWMfreq=assign(); + if(PWMfreq<0) PWMfreq=0; + if(PWMfreq>4000) PWMfreq=4000; + if(PWMduty<2) PWMduty=2; + if(PWMduty>99) PWMduty=99; + + updatePWM(); + handleelse(); + + break; + case TOK_DUTY: pcupdated=1; + pc+=4; + + PWMduty=assign(); + if(PWMfreq<0) PWMfreq=0; + if(PWMfreq>4000) PWMfreq=4000; + if(PWMduty<2) PWMduty=2; + if(PWMduty>99) PWMduty=99; + + updatePWM(); + handleelse(); + + break; + + + case TOK_DAT: pcupdated=1; + pc+=4; + + if(assign()) + { //protodath(); + protos[bpConfig.busMode].protocol_dath(); + } + else + { //protodatl(); + protos[bpConfig.busMode].protocol_datl(); + } + handleelse(); + + break; + case TOK_CLK: pcupdated=1; + pc+=4; + + switch(assign()) + { case 0: //protoclkl(); + protos[bpConfig.busMode].protocol_clkl(); + break; + case 1: //protoclkh(); + protos[bpConfig.busMode].protocol_clkh(); + break; + case 2: //protoclk(); + protos[bpConfig.busMode].protocol_clk(); + break; + } + handleelse(); + + break; + case TOK_PULLUP: pcupdated=1; + pc+=4; + +#ifndef BUSPIRATEV1A +//#if(0) + if(assign()) + { //protopullupon(); + BP_PULLUP_ON(); + //modeConfig.pullupEN=1; + } + else + { //protopullupoff(); + BP_PULLUP_OFF(); + //modeConfig.pullupEN=0; + } +#else + pc+=len-1; // fail silently +#endif + handleelse(); + + break; + case TOK_DELAY: pcupdated=1; + pc+=4; + temp=assign(); + bpDelayMS(temp); + handleelse(); + + break; + case TOK_MACRO: pcupdated=1; + pc+=4; + temp=assign(); + protos[bpConfig.busMode].protocol_macro(temp); + handleelse(); + break; + case TOK_END: //bpWstring(STAT_END); + stop=1; + break; + default: stop=SYNTAXERROR; + break; + } + + if(!pcupdated) + { pc+=len+3; + //bpBR; + } + pcupdated=0; + } + + bpConfig.quiet=0; // display on + + if(stop!=NOERROR) + { //bpWstring("Error("); + BPMSG1047; + bpWintdec(stop); + //bpWstring(") @line:"); + BPMSG1048; + bpWintdec(lineno); + //bpWstring(" @pgmspace:"); + BPMSG1049; + bpWintdec(pc); + bpBR; + } + + + //bpWinthex(vars[0]); bpSP; + //bpWinthex(vars[1]); bpSP; + //bpWinthex(vars[2]); bpSP; +} + +void printstat(char *s) +{ bpSP; + bpWstring(s); + bpSP; +} + +void list(void) +{ unsigned char c; + unsigned int lineno; + + pc=0; + + while(pgmspace[pc]) + { c=pgmspace[pc]; + if(cTOK_LEN) + { bpBR; + //bpWintdec(pc); bpSP; + lineno=(pgmspace[pc+1]<<8)+pgmspace[pc+2]; + pc+=2; + bpWintdec(lineno); + bpSP; + } + else + { bpSP; + bpWstring(tokens[c-TOKENS]); + bpSP; + } + pc++; + } + bpBR; + bpWintdec(pc-1); + //bpWline(" bytes."); + BPMSG1050; +} + +int compare(char *p) +{ int oldstart; + + oldstart=cmdstart; + while(*p) + { if(*p!=cmdbuf[cmdstart]) + { cmdstart=oldstart; + return 0; + } + cmdstart=(cmdstart+1)&CMDLENMSK; + + p++; + } + return 1; +} + +unsigned char gettoken(void) +{ int i; + + for(i=0; i='a')&&(cmdbuf[i]<='z')) cmdbuf[i]&=0xDF; + i++; + i&=CMDLENMSK; + } + + i=0; + // command or a new line? + if((cmdbuf[cmdstart]>='0')&&(cmdbuf[cmdstart]<='9')) + { //bpWline("new line"); + + for(i=0; i<35; i++) + { line[i]=0; + } + + temp=getint(); + line[1]=temp>>8; + line[2]=temp&0xFF; + + //bpWstring("search for line "); + //bpWintdec(temp); bpBR; + + pos=searchlineno(temp); + //bpWstring("pos="); + //bpWintdec(pos); bpSP; + if(pos!=-1) // if it already exist remove it first + { //bpWstring("replace/remove line @"); + //bpWintdec(pos); bpBR + len=(pgmspace[pos]-TOK_LEN)+3; + //bpWstring("pos="); + //bpWintdec(pos); bpSP; + for(i=pos; i35) + { //bpWline("Too long!"); + BPMSG1051; + return; + } + } + //bpWstring("i="); + //bpWintdec(i); bpSP; + + if(i==3) return; // no need to insert an empty line + if(i==4) return; // no need to insert an empty line + + line[0]=TOK_LEN+(i-4); + + //UART1TX('['); + //for(i=0; i<35; i++) + //{ UART1TX(line[i]); + //} + //UART1TX(']'); + + i=0; + end=0; + pos=0; + + while(!end) + { if(pgmspace[i]>TOK_LEN) // valid line + { len=pgmspace[i]-TOK_LEN; + //bpWstring("len="); + //bpWintdec(len); bpSP; + + lineno1=(pgmspace[i+1]<<8)+pgmspace[i+2]; + lineno2=(line[1]<<8)+line[2]; + if(lineno1>=1; + } + + HIZbbH(BASSCL, BASI2CCLK); + i=HIZbbR(BASSDA); + HIZbbL(BASSCL, BASI2CCLK); + + return (i^0x01); +} + +int checkeeprom(void) +{ // just to be sure + basi2cstop(); + basi2cstop(); + basi2cstart(); + if(!basi2cwrite(I2CADDR)) + { //bpWline("No EEPROM"); + BPMSG1053; + return 0; + } + basi2cwrite(0x00); + basi2cwrite(0x00); + basi2cstart(); + if(basi2cread(1)==0x00) // check for any data + { bpWline("No EEPROM"); // if 0 prolly no pullup and eeprom (PROLLY!) + BPMSG1053; + return 0; + } + basi2cstop(); + return 1; +} + +void format(void) +{ int i,j; + + basi2cstop(); + basi2cstart(); + if(!basi2cwrite(I2CADDR)) + { //bpWline("No EEPROM"); + BPMSG1053; + return; + } + basi2cstop(); + + //bpWstring("Erasing"); + BPMSG1054; + for(i=0; i>8)); + basi2cwrite((i&0x0FF)); + for(j=0; j(EEPROMSIZE/PGMSIZE)) + { //bpWline("Invalid slot"); + BPMSG1057; + return; + } + + if(!checkeeprom()) + { return; + } + + slot*=PGMSIZE; + + basi2cstop(); + basi2cwrite(I2CADDR); + basi2cwrite(slot>>8); + basi2cwrite(slot&0x0FF); + basi2cstart(); + basi2cwrite(I2CADDR+1); + + slot*=EEPROMPAGE; + + for(i=0; i>8); + basi2cwrite((slot+i)&0x0FF); + for(j=0; j(EEPROMSIZE/PGMSIZE)) + { //bpWline("Invalid slot"); + BPMSG1057; + return; + } + + if(!checkeeprom()) + { return; + } + + slot*=PGMSIZE; + + basi2cstop(); + basi2cwrite(I2CADDR); + basi2cwrite(slot>>8); + basi2cwrite(slot&0x0FF); + basi2cstart(); + basi2cwrite(I2CADDR+1); + + for(i=0; i> 8)); //send upper 8 bits - UART1TX(i); //send lower 8 bits - } else if (inByte == 0b10101) {//ADC reading (x/1024)*6.6volts - AD1CON1bits.ADON = 1; // turn ADC ON - while (1) { - i = bpADC(BP_ADC_PROBE); //take measurement - WAITTXEmpty(); - UART1TX((i >> 8)); //send upper 8 bits - //while(UART1TXRdy==0); - UART1TX(i); //send lower 8 bits - - if (UART1RXRdy() == 1) {//any key pressed, exit - i = UART1RX(); // /* JTR usb port; */; - break; - } - } - AD1CON1bits.ADON = 0; // turn ADC OFF - }else if (inByte==0b10110){ //binary frequency count access - unsigned long l; - l=bpBinFreq(); - UART1TX((l>>(8*3))); - UART1TX((l>>(8*2))); - UART1TX((l>>(8*1))); - UART1TX((l)); -//--- Added JM -#ifdef BUSPIRATEV4 - } else if (inByte == 0b11000) { //XSVF Player to program CPLD - BP_VREGEN = 1; - bpWstring("XSV1"); - jtag(); -#endif -//--- End added JM - } else if ((inByte >> 5)&0b010) {//set pin direction, return read - UART1TX(binBBpindirectionset(inByte)); - } else {//unknown command, error - UART1TX(0); - } - - } else {//data for pins - UART1TX(binBBpinset(inByte)); - }//if - }//while -}//function - -unsigned char getRXbyte(void) { - //JTR Not required while (UART1RXRdy() == 0); //wait for a byte - return UART1RX(); ///* JTR usb port; */ //grab it -} - -void binReset(void) { -#if defined(BUSPIRATEV4) //Shut down the pull up voltages - BP_3V3PU_OFF(); -#endif - binBBpindirectionset(0xff); //pins to input on start - binBBpinset(0); //startup everything off, pins at ground -#if defined(BUSPIRATEV1A) //aux2 pin to input on v1a - BP_AUX2_IN(); -#endif -} - -unsigned char binBBpindirectionset(unsigned char inByte) { - unsigned char i; - //setup pin TRIS - //using this method is long and nasty, - //but it makes it work for all hardware versions - //without special adjustments - i = 0; - if (inByte & 0b10000)i = 1; - BP_AUX0_DIR = i; - - i = 0; - if (inByte & 0b1000)i = 1; - BP_MOSI_DIR = i; - - i = 0; - if (inByte & 0b100)i = 1; - BP_CLK_DIR = i; - - i = 0; - if (inByte & 0b10)i = 1; - BP_MISO_DIR = i; - - i = 0; - if (inByte & 0b1)i = 1; - BP_CS_DIR = i; - - //delay for a brief period - bpDelayUS(5); - - //return PORT read - inByte &= (~0b00011111); - if (BP_AUX0 != 0)inByte |= 0b10000; - if (BP_MOSI != 0)inByte |= 0b1000; - if (BP_CLK != 0)inByte |= 0b100; - if (BP_MISO != 0)inByte |= 0b10; - if (BP_CS != 0)inByte |= 0b1; - - return inByte; //return the read -} - -unsigned char binBBpinset(unsigned char inByte) { - unsigned char i; - - if (inByte & 0b1000000) { - BP_VREG_ON(); //power on - } else { - BP_VREG_OFF(); //power off - } - -#ifndef BUSPIRATEV1A - if (inByte & 0b100000) { - BP_PULLUP_ON(); //pullups on - } else { - BP_PULLUP_OFF(); - } -#endif - - //set pin LAT - //using this method is long and nasty, - //but it makes it work for all hardware versions - //without special adjustments - i = 0; - if (inByte & 0b10000)i = 1; - BP_AUX0 = i; - - i = 0; - if (inByte & 0b1000)i = 1; - BP_MOSI = i; - - i = 0; - if (inByte & 0b100)i = 1; - BP_CLK = i; - - i = 0; - if (inByte & 0b10)i = 1; - BP_MISO = i; - - i = 0; - if (inByte & 0b1)i = 1; - BP_CS = i; - - //delay for a brief period - bpDelayUS(5); - - //return PORT read - inByte &= (~0b00011111); - if (BP_AUX0 != 0)inByte |= 0b10000; - if (BP_MOSI != 0)inByte |= 0b1000; - if (BP_CLK != 0)inByte |= 0b100; - if (BP_MISO != 0)inByte |= 0b10; - if (BP_CS != 0)inByte |= 0b1; - - return inByte; //return the read -} - -#ifndef BUSPIRATEV1A - -void binSelfTest(unsigned char jumperTest) { - static volatile unsigned int tick = 0; - unsigned char errors, inByte; - - errors = selfTest(0, jumperTest); //silent self-test - if (errors) BP_LEDMODE = 1; //light MODE LED if errors - UART1TX(errors); //reply with number of errors - - while (1) { - //echo incoming bytes + errors - //tests FTDI chip, UART, retrieves results of test - if (UART1RXRdy()) { - inByte = UART1RX(); //check input - if (inByte != 0xff) { - UART1TX(inByte + errors); - } else { - UART1TX(0x01); - return; //exit if we get oxff, else send back byte+errors - } - } - - if (!errors) { - if (tick == 0) { - tick = 0xFFFF; - BP_LEDMODE ^= 1; //toggle LED - } - tick--; - } - - } - -} -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +/* Binary access modes for Bus Pirate scripting */ + +#include "base.h" +#include "bitbang.h" +#include "selftest.h" +#include "SPI.h" +#include "I2C.h" +#include "UART.h" +#include "1wire.h" +#include "binwire.h" +#include "OpenOCD.h" +#include "pic.h" +#include "binIO.h" +#include "AUXpin.h" +//--- Added JM +#ifdef BUSPIRATEV4 +#include "jtag.h" +#endif +//--- End added JM + + + +extern struct _modeConfig modeConfig; + +//unsigned char binBBpindirectionset(unsigned char inByte); +//unsigned char binBBpinset(unsigned char inByte); +void binBBversion(void); +void binSelfTest(unsigned char jumperTest); +void binReset(void); +unsigned char getRXbyte(void); + +/* +Bitbang is like a player piano or bitmap. The 1 and 0 represent the pins. +So for the four Bus Pirate pins we use the the bits as follows: +COMMAND|POWER|PULLUP|AUX|CS|MISO|CLK|MOSI. + +The Bus pirate also responds to each write with a read byte showing the current state of the pins. + +The bits control the state of each of those pins when COMMAND=1. +When COMMAND=0 then up to 127 command codes can be entered on the lower bits. +0x00 resets the Bus Pirate to bitbang mode. + +Data: +1xxxxxxx //COMMAND|POWER|PULLUP|AUX|MOSI|CLK|MISO|CS + +Commands: +00000000 //Reset to raw BB mode, get raw BB version string +00000001 //enter rawSPI mode +00000010 //enter raw I2C mode +00000011 //enter raw UART mode +00000100 // enter raw 1-wire +00000101 //enter raw wire mode +00000110 // enter openOCD +00000111 // pic programming mode +00001111 //reset, return to user terminal +00010000 //short self test +00010001 //full self test with jumpers +00010010 // setup PWM +00010011 // clear PWM +00010100 // ADC measurement + +// Added JM Only with BP4 +00010101 // ADC .... +00010110 // ADC Stop +00011000 // XSVF Player +// End added JM +// +010xxxxx //set input(1)/output(0) pin state (returns pin read) + */ +void binBBversion(void) { + bpWstring("BBIO1"); +} + +void binBB(void) { + static unsigned char inByte; + unsigned int i; + + BP_LEDMODE = 1; //light MODE LED + binReset(); + binBBversion(); //send mode name and version + + while (1) { + + inByte = getRXbyte(); + + if ((inByte & 0b10000000) == 0) {//if command bit cleared, process command + if (inByte == 0) {//reset, send BB version + binBBversion(); + } else if (inByte == 1) {//goto SPI mode + binReset(); +#ifdef BP_USE_HWSPI + binSPI(); //go into rawSPI loop +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 2) {//goto I2C mode + binReset(); +#ifdef BP_USE_I2C + binI2C(); +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 3) {//goto UART mode + binReset(); +#ifdef BP_USE_HWUART + binUART(); +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 4) {//goto 1WIRE mode + binReset(); +#ifdef BP_USE_1WIRE + bin1WIRE(); +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 5) {//goto RAW WIRE mode + binReset(); + binwire(); + binReset(); + binBBversion(); //say name on return + } else if (inByte == 6) {//goto OpenOCD mode + binReset(); +#ifndef BUSPIRATEV4 + binOpenOCD(); +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 7) {//goto pic mode + binReset(); +#ifdef BP_USE_PIC + binpic(); +#endif + binReset(); + binBBversion(); //say name on return + } else if (inByte == 0b1111) {//return to terminal + UART1TX(1); + BP_LEDMODE = 0; //light MODE LED + WAITTXEmpty(); //wait untill TX finishes +#ifndef BUSPIRATEV4 + asm("RESET"); +#endif +#ifdef BUSPIRATEV4 //cannot use ASM reset on BPv4 + binReset(); + return; +#endif + //self test is only for v2go and v3 +#ifndef BUSPIRATEV1A + } else if (inByte == 0b10000) {//short self test + binSelfTest(0); + } else if (inByte == 0b10001) {//full self test with jumpers + binSelfTest(1); +#endif + } else if (inByte == 0b10010) {//setup PWM + + //cleanup timers from FREQ measure + T2CON = 0; //16 bit mode + T4CON = 0; + OC5CON = 0; //clear PWM settings + + BP_AUX_RPOUT = OC5_IO; //setup pin + + //get one byte + i = getRXbyte(); + if (i & 0b10) T2CONbits.TCKPS1 = 1; //set prescalers + if (i & 0b1) T2CONbits.TCKPS0 = 1; + + //get two bytes + i = (getRXbyte() << 8); + i |= getRXbyte(); + OC5R = i; //Write duty cycle to both registers + OC5RS = i; + OC5CON = 0x6; // PWM mode on OC, Fault pin disabled + + //get two bytes + i = (getRXbyte() << 8); + i |= getRXbyte(); + PR2 = i; // write period + + T2CONbits.TON = 1; // Start Timer2 + UART1TX(1); + } else if (inByte == 0b10011) {//clear PWM + T2CON = 0; // stop Timer2 + OC5CON = 0; + BP_AUX_RPOUT = 0; //remove output from AUX pin + UART1TX(1); + //ADC only for v1, v2, v3 + } else if (inByte == 0b10100) {//ADC reading (x/1024)*6.6volts + AD1CON1bits.ADON = 1; // turn ADC ON + i = bpADC(BP_ADC_PROBE); //take measurement + AD1CON1bits.ADON = 0; // turn ADC OFF + UART1TX((i >> 8)); //send upper 8 bits + UART1TX(i); //send lower 8 bits + } else if (inByte == 0b10101) {//ADC reading (x/1024)*6.6volts + AD1CON1bits.ADON = 1; // turn ADC ON + while (1) { + i = bpADC(BP_ADC_PROBE); //take measurement + WAITTXEmpty(); + UART1TX((i >> 8)); //send upper 8 bits + //while(UART1TXRdy==0); + UART1TX(i); //send lower 8 bits + + if (UART1RXRdy() == 1) {//any key pressed, exit + i = UART1RX(); // /* JTR usb port; */; + break; + } + } + AD1CON1bits.ADON = 0; // turn ADC OFF + }else if (inByte==0b10110){ //binary frequency count access + unsigned long l; + l=bpBinFreq(); + UART1TX((l>>(8*3))); + UART1TX((l>>(8*2))); + UART1TX((l>>(8*1))); + UART1TX((l)); +//--- Added JM +#ifdef BUSPIRATEV4 + } else if (inByte == 0b11000) { //XSVF Player to program CPLD + BP_VREGEN = 1; + bpWstring("XSV1"); + jtag(); +#endif +//--- End added JM + } else if ((inByte >> 5)&0b010) {//set pin direction, return read + UART1TX(binBBpindirectionset(inByte)); + } else {//unknown command, error + UART1TX(0); + } + + } else {//data for pins + UART1TX(binBBpinset(inByte)); + }//if + }//while +}//function + +unsigned char getRXbyte(void) { + //JTR Not required while (UART1RXRdy() == 0); //wait for a byte + return UART1RX(); ///* JTR usb port; */ //grab it +} + +void binReset(void) { +#if defined(BUSPIRATEV4) //Shut down the pull up voltages + BP_3V3PU_OFF(); +#endif + binBBpindirectionset(0xff); //pins to input on start + binBBpinset(0); //startup everything off, pins at ground +#if defined(BUSPIRATEV1A) //aux2 pin to input on v1a + BP_AUX2_IN(); +#endif +} + +unsigned char binBBpindirectionset(unsigned char inByte) { + unsigned char i; + //setup pin TRIS + //using this method is long and nasty, + //but it makes it work for all hardware versions + //without special adjustments + i = 0; + if (inByte & 0b10000)i = 1; + BP_AUX0_DIR = i; + + i = 0; + if (inByte & 0b1000)i = 1; + BP_MOSI_DIR = i; + + i = 0; + if (inByte & 0b100)i = 1; + BP_CLK_DIR = i; + + i = 0; + if (inByte & 0b10)i = 1; + BP_MISO_DIR = i; + + i = 0; + if (inByte & 0b1)i = 1; + BP_CS_DIR = i; + + //delay for a brief period + bpDelayUS(5); + + //return PORT read + inByte &= (~0b00011111); + if (BP_AUX0 != 0)inByte |= 0b10000; + if (BP_MOSI != 0)inByte |= 0b1000; + if (BP_CLK != 0)inByte |= 0b100; + if (BP_MISO != 0)inByte |= 0b10; + if (BP_CS != 0)inByte |= 0b1; + + return inByte; //return the read +} + +unsigned char binBBpinset(unsigned char inByte) { + unsigned char i; + + if (inByte & 0b1000000) { + BP_VREG_ON(); //power on + } else { + BP_VREG_OFF(); //power off + } + +#ifndef BUSPIRATEV1A + if (inByte & 0b100000) { + BP_PULLUP_ON(); //pullups on + } else { + BP_PULLUP_OFF(); + } +#endif + + //set pin LAT + //using this method is long and nasty, + //but it makes it work for all hardware versions + //without special adjustments + i = 0; + if (inByte & 0b10000)i = 1; + BP_AUX0 = i; + + i = 0; + if (inByte & 0b1000)i = 1; + BP_MOSI = i; + + i = 0; + if (inByte & 0b100)i = 1; + BP_CLK = i; + + i = 0; + if (inByte & 0b10)i = 1; + BP_MISO = i; + + i = 0; + if (inByte & 0b1)i = 1; + BP_CS = i; + + //delay for a brief period + bpDelayUS(5); + + //return PORT read + inByte &= (~0b00011111); + if (BP_AUX0 != 0)inByte |= 0b10000; + if (BP_MOSI != 0)inByte |= 0b1000; + if (BP_CLK != 0)inByte |= 0b100; + if (BP_MISO != 0)inByte |= 0b10; + if (BP_CS != 0)inByte |= 0b1; + + return inByte; //return the read +} + +#ifndef BUSPIRATEV1A + +void binSelfTest(unsigned char jumperTest) { + static volatile unsigned int tick = 0; + unsigned char errors, inByte; + + errors = selfTest(0, jumperTest); //silent self-test + if (errors) BP_LEDMODE = 1; //light MODE LED if errors + UART1TX(errors); //reply with number of errors + + while (1) { + //echo incoming bytes + errors + //tests FTDI chip, UART, retrieves results of test + if (UART1RXRdy()) { + inByte = UART1RX(); //check input + if (inByte != 0xff) { + UART1TX(inByte + errors); + } else { + UART1TX(0x01); + return; //exit if we get oxff, else send back byte+errors + } + } + + if (!errors) { + if (tick == 0) { + tick = 0xFFFF; + BP_LEDMODE ^= 1; //toggle LED + } + tick--; + } + + } + +} +#endif diff --git a/Firmware/binIO.h b/Firmware/binIO.h index 0d31f340..d97d68fc 100644 --- a/Firmware/binIO.h +++ b/Firmware/binIO.h @@ -1,19 +1,19 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -void binBB(void); -unsigned char binBBpindirectionset(unsigned char inByte); -unsigned char binBBpinset(unsigned char inByte); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +void binBB(void); +unsigned char binBBpindirectionset(unsigned char inByte); +unsigned char binBBpinset(unsigned char inByte); + diff --git a/Firmware/binIOhelpers.c b/Firmware/binIOhelpers.c index 77b39d64..d8f35714 100644 --- a/Firmware/binIOhelpers.c +++ b/Firmware/binIOhelpers.c @@ -1,81 +1,81 @@ -#include "base.h" -extern struct _modeConfig modeConfig; //holds persistant bus pirate settings (see base.h) - - -void binIOperipheralset(unsigned char inByte){ - - if(inByte&0b1000){ - BP_VREG_ON();//power on - }else{ - BP_VREG_OFF();//power off - } - - #ifndef BUSPIRATEV1A - if(inByte&0b100){ - BP_PULLUP_ON();//pullups on - }else{ - BP_PULLUP_OFF(); - } - #endif - #if defined(BUSPIRATEV1A) - if(inByte&0b100){ - BP_AUX2_HI();//AUX2 control - }else{ - BP_AUX2_LOW(); - } - #endif - - //AUX pin, high/low only - if(inByte&0b10){ - BP_AUX0_DIR=0;//aux output - BP_AUX0=1;//aux high - }else{ - BP_AUX0_DIR=0;//aux output - BP_AUX0=0;//aux low - } - - //CS pin, follows HiZ setting - if(inByte&0b1){ - if(modeConfig.HiZ==1){ - IODIR|=CS; //CS iput in open drain mode - }else{ - IOLAT|=CS; //CS high - IODIR&=(~CS); //CS output - } - }else{ - IOLAT&=(~CS); //BP_CS=0; - IODIR&=(~CS); //CS output - } - - //UART1TX(1);//send 1/OK -} - -#ifdef BUSPIRATEV4 -//checks if voltage is present on VUEXTERN -unsigned char binBBpullVoltage(unsigned char ctrlB) -{ - unsigned char temp=1; - if(modeConfig.HiZ == 0) - { - temp = 0; - } - else - { - BP_3V3PU_OFF(); //disable any existing pullup - bpDelayMS(2); - ADCON(); - if (bpADC(BP_ADC_VPU) > 0x100) - { //is there already an external voltage? - temp =0; - } - ADCOFF(); - } - if(temp) - { - if(ctrlB == 0x51){BP_3V3PU_ON();} //turns on Pull up voltage 3.3v - else if(ctrlB == 0x52){BP_5VPU_ON();} //turns on Pull up voltage 5v - else {BP_3V3PU_OFF();} - } - return temp; -} -#endif +#include "base.h" +extern struct _modeConfig modeConfig; //holds persistant bus pirate settings (see base.h) + + +void binIOperipheralset(unsigned char inByte){ + + if(inByte&0b1000){ + BP_VREG_ON();//power on + }else{ + BP_VREG_OFF();//power off + } + + #ifndef BUSPIRATEV1A + if(inByte&0b100){ + BP_PULLUP_ON();//pullups on + }else{ + BP_PULLUP_OFF(); + } + #endif + #if defined(BUSPIRATEV1A) + if(inByte&0b100){ + BP_AUX2_HI();//AUX2 control + }else{ + BP_AUX2_LOW(); + } + #endif + + //AUX pin, high/low only + if(inByte&0b10){ + BP_AUX0_DIR=0;//aux output + BP_AUX0=1;//aux high + }else{ + BP_AUX0_DIR=0;//aux output + BP_AUX0=0;//aux low + } + + //CS pin, follows HiZ setting + if(inByte&0b1){ + if(modeConfig.HiZ==1){ + IODIR|=CS; //CS iput in open drain mode + }else{ + IOLAT|=CS; //CS high + IODIR&=(~CS); //CS output + } + }else{ + IOLAT&=(~CS); //BP_CS=0; + IODIR&=(~CS); //CS output + } + + //UART1TX(1);//send 1/OK +} + +#ifdef BUSPIRATEV4 +//checks if voltage is present on VUEXTERN +unsigned char binBBpullVoltage(unsigned char ctrlB) +{ + unsigned char temp=1; + if(modeConfig.HiZ == 0) + { + temp = 0; + } + else + { + BP_3V3PU_OFF(); //disable any existing pullup + bpDelayMS(2); + ADCON(); + if (bpADC(BP_ADC_VPU) > 0x100) + { //is there already an external voltage? + temp =0; + } + ADCOFF(); + } + if(temp) + { + if(ctrlB == 0x51){BP_3V3PU_ON();} //turns on Pull up voltage 3.3v + else if(ctrlB == 0x52){BP_5VPU_ON();} //turns on Pull up voltage 5v + else {BP_3V3PU_OFF();} + } + return temp; +} +#endif diff --git a/Firmware/binIOhelpers.h b/Firmware/binIOhelpers.h index 84d7de8b..140998fd 100644 --- a/Firmware/binIOhelpers.h +++ b/Firmware/binIOhelpers.h @@ -1,6 +1,6 @@ -void binIOperipheralset(unsigned char inByte); - -#ifdef BUSPIRATEV4 -unsigned char binBBpullVoltage(unsigned char ctrlB); -#endif - +void binIOperipheralset(unsigned char inByte); + +#ifdef BUSPIRATEV4 +unsigned char binBBpullVoltage(unsigned char ctrlB); +#endif + diff --git a/Firmware/binwire.c b/Firmware/binwire.c index b4c081f1..1dd4128f 100644 --- a/Firmware/binwire.c +++ b/Firmware/binwire.c @@ -1,612 +1,612 @@ -#include "base.h" -#include "bitbang.h" -#include "busPirateCore.h" -#include "binIOhelpers.h" -#ifdef BUSPIRATEV4 -#include "smps.h" -#endif - -extern struct _modeConfig modeConfig; -extern struct _bpConfig bpConfig; -void binrawversionString(void); -void PIC24NOP(void); - -void PIC614Write(unsigned char cmd, unsigned char datl, unsigned char dath); -void PIC614Read(unsigned char c); - -void PIC416Read(unsigned char c); -void PIC416Write(unsigned char cmd, unsigned char datl, unsigned char dath); -void PIC424Write_internal(unsigned long cmd, unsigned char pn); -void PIC424Write(unsigned char *cmd, unsigned char pn); -void PIC424Read(void); - -#define R3WMOSI_TRIS BP_MOSI_DIR -#define R3WCLK_TRIS BP_CLK_DIR -#define R3WMISO_TRIS BP_MISO_DIR -#define R3WCS_TRIS BP_CS_DIR - - -#define R3WMOSI BP_MOSI -#define R3WCLK BP_CLK -#define R3WMISO BP_MISO -#define R3WCS BP_CS - -/* - * 00000000 � Enter raw bitbang mode, reset to raw bitbang mode - * 00000001 � Mode version string (RAW1) - * 00000010 - I2C style start bit - * 00000011 - I2C style stop bit - * 00000100 - CS low (0) (respects hiz setting) - * 00000101 - CS high (1) - * 00000110 - Read byte - * 00000111 - Read bit - * 00001000 - Peek at input pin - * 00001001 - Clock tick - * 00001010 - Clock low - * 00001011 - Clock high - * 00001100 - Data low - * 00001101 - Data high - * 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) - * 0010xxxx - Bulk clock ticks, send 1-16 ticks - * 0011xxxx - Bulk bits, send 1-8 bits of the next byte (0=1bit!) - * 0100wxyz � Configure peripherals, w=power, x=pullups, y=AUX, z=CS - * 0101xxxx - Bulk read, read 1-16bytes (0=1byte!) - * 0110000x � Set speed - * 1000wxyz � Config, w=output type, x=3wire, y=lsb, z=n/a - ****************** BPv4 Specific Instructions ********************* - * 11110000 - Return SMPS output voltage - * 11110001 - Stop SMPS operation - * 1111xxxx - Start SMPS operation (xxxx and next byte give requested output voltage) - Lowest possible value is 512 = 0b0010 0000 - - */ - -void binrawversionString(void) { - bpWstring("RAW1"); -} - -enum { - PICUNK = 0, - PIC416, - PIC424, - PIC614, -}; - -void binwire(void) { - static unsigned char inByte, rawCommand, i, c, wires, picMode = PIC614; - static unsigned int cmds, cmdw, cmdr, j; - #ifdef BUSPIRATEV4 - static unsigned int V_out; - #endif - - modeConfig.HiZ = 1; //yes, always hiz (bbio uses this setting, should be changed to a setup variable because stringing the modeconfig struct everyhwere is getting ugly!) - modeConfig.lsbEN = 0; //just in case! - modeConfig.speed = 1; - modeConfig.numbits = 8; - //startup in raw2wire mode - wires = 2; - //configure for raw3wire mode - bbSetup(2, 0xff); //setup the bitbang library, must be done before calling bbCS below - //setup pins (pins are input/low when we start) - //MOSI output, low - //clock output, low - //MISO input - //CS output, high - R3WMOSI_TRIS = 0; - R3WCLK_TRIS = 0; - R3WMISO_TRIS = 1; - bbCS(1); //takes care of custom HiZ settings too - - binrawversionString(); //reply ID string - - while (1) { - - - inByte = UART1RX(); // /* JTR usb port; */; //grab it - rawCommand = (inByte >> 4); //get command bits in seperate variable - - switch (rawCommand) { - case 0://reset/setup/config commands - switch (inByte) { - case 0://0, reset exit - //cleanup!!!!!!!!!! - return; //exit - break; - case 1://id reply string - binrawversionString(); //reply string - break; - case 2://start bit - bbI2Cstart(); - UART1TX(1); - break; - case 3://stop bit - bbI2Cstop(); - UART1TX(1); - break; - case 4: //cs low - bbCS(0); - UART1TX(1); - break; - case 5://cs high - bbCS(1); - UART1TX(1); - break; - case 6://read byte - if (wires == 2) { - i = bbReadByte(); - } else { - i = bbReadWriteByte(0xff); - } - if (modeConfig.lsbEN == 1) {//adjust bitorder - i = bpRevByte(i); - } - UART1TX(i); - break; - case 7://read bit - UART1TX(bbReadBit()); - break; - case 8://peek bit - UART1TX(bbMISO()); - break; - case 9://clock tick - bbClockTicks(1); - UART1TX(1); - break; - case 10://clock low - bbCLK(0); - UART1TX(1); - break; - case 11://clock high - bbCLK(1); - UART1TX(1); - break; - case 12://data low - bbMOSI(0); - UART1TX(1); - break; - case 13://data high - bbMOSI(1); - UART1TX(1); - break; - default: - UART1TX(0); - break; - } - break; - - case 0b0001://get x+1 bytes - inByte &= (~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1); //send 1/OK - - for (i = 0; i < inByte; i++) { - c = UART1RX(); // /* JTR usb port; */; - if (modeConfig.lsbEN == 1) {//adjust bitorder - c = bpRevByte(c); - } - if (wires == 2) {//2 wire, send 1 - bbWriteByte(c); //send byte - UART1TX(1); - } else { //3 wire, return read byte - c = bbReadWriteByte(c); //send byte - if (modeConfig.lsbEN == 1) {//adjust bitorder - c = bpRevByte(c); - } - UART1TX(c); - } - } - - break; - - case 0b0010://bulk clock ticks - inByte &= (~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - bbClockTicks(inByte); - UART1TX(1); //send 1/OK - break; - - case 0b0011: //# 0011xxxx - Bulk bits, send 1-8 bits of the next byte (0=1bit!) - inByte &= (~0b11110000); //clear command portion - inByte++; //increment by 1, 0=1byte - UART1TX(1); //send 1/OK - - rawCommand = UART1RX(); // //get byte, reuse rawCommand variable - for (i = 0; i < inByte; i++) { - if (rawCommand & 0b10000000) {//send 1 - bbWriteBit(1); //send bit - } else { //send 0 - bbWriteBit(0); //send bit - } - rawCommand = rawCommand << 1; //pop the MSB off - } - UART1TX(1); - break; - - case 0b1010:// PIC commands - - switch (inByte) { - case 0b10100000: - - picMode = UART1RX(); // /* JTR usb port; */; //get byte - UART1TX(1); //send 1/OK - break; - case 0b10100100: //write - switch (picMode) { - case PIC416: - - //get the number of commands that will follow - cmds = UART1RX(); // //get byte, reuse rawCommand variable - cmds = cmds * 3; //make sure an int - //get command byte, two data bytes - for (j = 0; j < cmds; j++) { - - bpConfig.terminalInput[j] = UART1RX(); // /* JTR usb port; */; - } - - for (j = 0; j < cmds; j = j + 3) { - PIC416Write(bpConfig.terminalInput[j], bpConfig.terminalInput[j + 1], bpConfig.terminalInput[j + 2]); - } - - UART1TX(1); //send 1/OK - break; - case PIC424: - //get the number of commands that will follow - cmds = UART1RX(); // /* JTR usb port; */; //get byte, reuse rawCommand variable - cmds = cmds * 4; //make sure an int - //get three byte command, 1 byte pre-post NOP - for (j = 0; j < cmds; j++) { - - bpConfig.terminalInput[j] = UART1RX(); // /* JTR usb port; */; - } - - for (j = 0; j < cmds; j = j + 4) { - //do any pre instruction NOPs - - //send four bit SIX command (write) - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - - //send data payload - bbWriteByte(bpConfig.terminalInput[j]); //send byte - bbWriteByte(bpConfig.terminalInput[j + 1]); //send byte - bbWriteByte(bpConfig.terminalInput[j + 2]); //send byte - - //do any post instruction NOPs - bpConfig.terminalInput[j + 3] &= 0x0F; - for (i = 0; i < bpConfig.terminalInput[j + 3]; i++) { - PIC24NOP(); - } - } - UART1TX(1); //send 1/OK - break; - default: - UART1TX(0); //send 1/OK - break; - } - break; - case 0b10100101://write x bit command, read x bits and return in 2 bytes - switch (picMode) { - case PIC416: - - //get the number of commands that will follow - cmds = UART1RX(); // //get byte, reuse rawCommand variable - //cmds=cmds; //make sure an int - //get teh command to send on each read.... - - rawCommand = UART1RX(); // /* JTR usb port; */; - - - for (j = 0; j < cmds; j++) { - //write command - c = rawCommand; //temporary varaible - for (i = 0; i < 4; i++) { - if (c & 0b1) {//send 1 - bbWriteBit(1); //send bit - } else { //send 0 - bbWriteBit(0); //send bit - } - c = c >> 1; //pop the LSB off - } - bbReadByte(); //dummy byte, setup input - UART1TX(bbReadByte()); - } - break; - case PIC424: - //get the number of commands that will follow - cmds = UART1RX(); // //get byte, reuse rawCommand variable - - for (j = 0; j < cmds; j++) { - //write command - PIC424Write_internal(0xBA0B96, 2); - PIC424Read(); - - PIC424Write_internal(0xBADBB6, 2); - PIC424Write_internal(0xBAD3D6, 2); - PIC424Read(); - - PIC424Write_internal(0xBA0BB6, 2); - PIC424Read(); - - } - break; - } - break; - case 0b10100111: // x write, y read commands. - cmdw = UART1RX(); - cmdr = UART1RX(); - - if (picMode == PIC424) { - cmds = cmdw * 5 + cmdr; - } else if (picMode == PIC416) { - cmds = cmdw * 4 + cmdr * 2; - } else if (picMode == PIC614) { - cmds = cmdw * 4 + cmdr * 2; - } else { - UART1TX(0); - break; - } - - for (j = 0; j < cmds; j++) { - bpConfig.terminalInput[j] = UART1RX(); - } - - if (cmdr != 0) - UART1TX(1); // ACK - - j=0; - while (j < cmds) { - if (bpConfig.terminalInput[j] == 1) { // write command - if (picMode == PIC614) { - PIC614Write(bpConfig.terminalInput[j+1], bpConfig.terminalInput[j + 2], bpConfig.terminalInput[j + 3]); - j += 4; - } else if (picMode == PIC416) { - PIC416Write(bpConfig.terminalInput[j+1], bpConfig.terminalInput[j + 2], bpConfig.terminalInput[j + 3]); - j += 4; - } else if (picMode == PIC424) { - PIC424Write(&bpConfig.terminalInput[j + 1], bpConfig.terminalInput[j + 4]); - j += 5; - } - } else if (bpConfig.terminalInput[j] == 2) { // read command - if (picMode == PIC614) { - PIC614Read(bpConfig.terminalInput[j+1]); - j += 2; - } else if (picMode == PIC416) { - PIC416Read(bpConfig.terminalInput[j+1]); - j += 2; - } else if (picMode == PIC424) { - PIC424Read(); - j++; - } - } - } - - if (cmdr == 0) - UART1TX(1); // ACK - - break; - default: - UART1TX(0x00); //send 0/Error - break; - } - - break; - - //case 0b0101: //# 0101xxxx - Bulk read, read 1-16bytes (0=1byte!) - - case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS - binIOperipheralset(inByte); - UART1TX(1); //send 1/OK - break; - -#ifdef BUSPIRATEV4 - case 0b0101: - UART1TX(binBBpullVoltage(inByte)); - break; -#endif - - case 0b0110://set speed - inByte &= (~0b11111100); //clear command portion - modeConfig.speed = inByte; - bbSetup(wires, modeConfig.speed); - bbCS(1); //takes care of custom HiZ settings too - UART1TX(1); - break; - - case 0b1000: //set config - //wxyz //w=HiZ(0)/3.3v(1), x=3wireenable, y=lsb, z=n/a - modeConfig.HiZ = 0; - if ((inByte & 0b1000) == 0) modeConfig.HiZ = 1; //hiz output if this bit is 1 - - wires = 2; - if (inByte & 0b100) wires = 3; //3wire/2wire toggle - - modeConfig.lsbEN = 0; - if (inByte & 0b10) modeConfig.lsbEN = 1; //lsb/msb, bit order - - //if(inByte&0b1) //bit unused - - bbSetup(wires, modeConfig.speed); //setup the bitbang library, must be done before calling bbCS below - bbCS(1); //takes care of custom HiZ settings too - UART1TX(1); //send 1/OK - break; -#ifdef BUSPIRATEV4 - case 0b1111: // SMPS commands - switch (inByte) { - case 0xf0: - smpsADC(); // Send raw ADC reading - break; - case 0xf1: - smpsStop(); // Stop SMPS operation - UART1TX(1); // Send 1/OK - break; - default: - V_out = inByte & 0x0f; - V_out <<= 8; - V_out |= UART1RX(); - smpsStart(V_out); - UART1TX(1); // Send 1/OK - break; - } - break; -#endif - default: - UART1TX(0x00); //send 0/Error - break; - }//command switch - }//while loop - -} - -void PIC614Read(unsigned char c) { - unsigned char i; - - for (i = 0; i < 6; i++) { - bbWriteBit(c & 0b1); //send bit - c = c >> 1; //pop the LSB off - } - - UART1TX(bbReadByte()); - UART1TX(bbReadByte()); -} - -void PIC614Write(unsigned char cmd, unsigned char datl, unsigned char dath) { - unsigned char i, nodata; - - // MSB tells that there is no data output - nodata = cmd & 0x80; - - for (i = 0; i < 6; i++) { - bbWriteBit(cmd & 0b1); //send bit - cmd = cmd >> 1; //pop the LSB off - } - - if (nodata) - return; - - bbWriteByte(datl); //send byte - bbWriteByte(dath); //send byte - -} - -void PIC416Read(unsigned char c) { - unsigned char i; - - for (i = 0; i < 4; i++) { - if (c & 0b1) {//send 1 - bbWriteBit(1); //send bit - } else { //send 0 - bbWriteBit(0); //send bit - } - c = c >> 1; //pop the LSB off - } - - bbReadByte(); //dummy byte, setup input - UART1TX(bbReadByte()); -} - -void PIC416Write(unsigned char cmd, unsigned char datl, unsigned char dath) { - unsigned char i, delay; - - //use upper 2 bits of pic[0] to determine a delay, if any. - delay = cmd >> 6; - - for (i = 0; i < 4; i++) { - - //hold data for write time - if (i == 3 && (delay > 0)) { - bbCLK(1); - bpDelayMS(delay); - bbCLK(0); - continue; - } - - if (cmd & 0b1) {//send 1 - bbWriteBit(1); //send bit - } else { //send 0 - bbWriteBit(0); //send bit - } - cmd = cmd >> 1; //pop the LSB off - } - - bbWriteByte(datl); //send byte - bbWriteByte(dath); //send byte - -} - -void PIC24NOP(void) { - //send four bit SIX command (write) - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - - //send data payload - bbWriteByte(0x00); //send byte - bbWriteByte(0x00); //send byte - bbWriteByte(0x00); //send byte - -} - -void PIC424Write(unsigned char *cmd, unsigned char pn) { - //send four bit SIX command (write) - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - - //send data payload - bbWriteByte(cmd[0]); //send byte - bbWriteByte(cmd[1]); //send byte - bbWriteByte(cmd[2]); //send byte - - //do any post instruction NOPs - pn &= 0x0F; - while(pn--) { - PIC24NOP(); - } -} - -void PIC424Write_internal(unsigned long cmd, unsigned char pn) { - unsigned char i; - //send four bit SIX command (write) - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - - //send data payload 0xBA0B96 0xBADBB6 0xBA0BB6 - bbWriteByte(bpRevByte(cmd)); //send byte - bbWriteByte(bpRevByte(cmd >> 8)); //send byte - bbWriteByte(bpRevByte(cmd >> 16)); //send byte - - //do any post instruction NOPs - pn &= 0x0F; - for (i = 0; i < pn; i++) { - PIC24NOP(); - } -} - -void PIC424Read(void) { - unsigned char c; - - //send four bit REGOUT command (read) - bbWriteBit(1); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - bbWriteBit(0); //send bit - - //one byte output - bbWriteByte(0x00); //send byte - - //read 2 bytes - //return bytes in little endian format - c = bbReadByte(); - UART1TX(bbReadByte()); - UART1TX(c); - - //ALWAYS POST nop TWICE after a read - PIC24NOP(); - PIC24NOP(); -} +#include "base.h" +#include "bitbang.h" +#include "busPirateCore.h" +#include "binIOhelpers.h" +#ifdef BUSPIRATEV4 +#include "smps.h" +#endif + +extern struct _modeConfig modeConfig; +extern struct _bpConfig bpConfig; +void binrawversionString(void); +void PIC24NOP(void); + +void PIC614Write(unsigned char cmd, unsigned char datl, unsigned char dath); +void PIC614Read(unsigned char c); + +void PIC416Read(unsigned char c); +void PIC416Write(unsigned char cmd, unsigned char datl, unsigned char dath); +void PIC424Write_internal(unsigned long cmd, unsigned char pn); +void PIC424Write(unsigned char *cmd, unsigned char pn); +void PIC424Read(void); + +#define R3WMOSI_TRIS BP_MOSI_DIR +#define R3WCLK_TRIS BP_CLK_DIR +#define R3WMISO_TRIS BP_MISO_DIR +#define R3WCS_TRIS BP_CS_DIR + + +#define R3WMOSI BP_MOSI +#define R3WCLK BP_CLK +#define R3WMISO BP_MISO +#define R3WCS BP_CS + +/* + * 00000000 � Enter raw bitbang mode, reset to raw bitbang mode + * 00000001 � Mode version string (RAW1) + * 00000010 - I2C style start bit + * 00000011 - I2C style stop bit + * 00000100 - CS low (0) (respects hiz setting) + * 00000101 - CS high (1) + * 00000110 - Read byte + * 00000111 - Read bit + * 00001000 - Peek at input pin + * 00001001 - Clock tick + * 00001010 - Clock low + * 00001011 - Clock high + * 00001100 - Data low + * 00001101 - Data high + * 0001xxxx � Bulk transfer, send 1-16 bytes (0=1byte!) + * 0010xxxx - Bulk clock ticks, send 1-16 ticks + * 0011xxxx - Bulk bits, send 1-8 bits of the next byte (0=1bit!) + * 0100wxyz � Configure peripherals, w=power, x=pullups, y=AUX, z=CS + * 0101xxxx - Bulk read, read 1-16bytes (0=1byte!) + * 0110000x � Set speed + * 1000wxyz � Config, w=output type, x=3wire, y=lsb, z=n/a + ****************** BPv4 Specific Instructions ********************* + * 11110000 - Return SMPS output voltage + * 11110001 - Stop SMPS operation + * 1111xxxx - Start SMPS operation (xxxx and next byte give requested output voltage) + Lowest possible value is 512 = 0b0010 0000 + + */ + +void binrawversionString(void) { + bpWstring("RAW1"); +} + +enum { + PICUNK = 0, + PIC416, + PIC424, + PIC614, +}; + +void binwire(void) { + static unsigned char inByte, rawCommand, i, c, wires, picMode = PIC614; + static unsigned int cmds, cmdw, cmdr, j; + #ifdef BUSPIRATEV4 + static unsigned int V_out; + #endif + + modeConfig.HiZ = 1; //yes, always hiz (bbio uses this setting, should be changed to a setup variable because stringing the modeconfig struct everyhwere is getting ugly!) + modeConfig.lsbEN = 0; //just in case! + modeConfig.speed = 1; + modeConfig.numbits = 8; + //startup in raw2wire mode + wires = 2; + //configure for raw3wire mode + bbSetup(2, 0xff); //setup the bitbang library, must be done before calling bbCS below + //setup pins (pins are input/low when we start) + //MOSI output, low + //clock output, low + //MISO input + //CS output, high + R3WMOSI_TRIS = 0; + R3WCLK_TRIS = 0; + R3WMISO_TRIS = 1; + bbCS(1); //takes care of custom HiZ settings too + + binrawversionString(); //reply ID string + + while (1) { + + + inByte = UART1RX(); // /* JTR usb port; */; //grab it + rawCommand = (inByte >> 4); //get command bits in seperate variable + + switch (rawCommand) { + case 0://reset/setup/config commands + switch (inByte) { + case 0://0, reset exit + //cleanup!!!!!!!!!! + return; //exit + break; + case 1://id reply string + binrawversionString(); //reply string + break; + case 2://start bit + bbI2Cstart(); + UART1TX(1); + break; + case 3://stop bit + bbI2Cstop(); + UART1TX(1); + break; + case 4: //cs low + bbCS(0); + UART1TX(1); + break; + case 5://cs high + bbCS(1); + UART1TX(1); + break; + case 6://read byte + if (wires == 2) { + i = bbReadByte(); + } else { + i = bbReadWriteByte(0xff); + } + if (modeConfig.lsbEN == 1) {//adjust bitorder + i = bpRevByte(i); + } + UART1TX(i); + break; + case 7://read bit + UART1TX(bbReadBit()); + break; + case 8://peek bit + UART1TX(bbMISO()); + break; + case 9://clock tick + bbClockTicks(1); + UART1TX(1); + break; + case 10://clock low + bbCLK(0); + UART1TX(1); + break; + case 11://clock high + bbCLK(1); + UART1TX(1); + break; + case 12://data low + bbMOSI(0); + UART1TX(1); + break; + case 13://data high + bbMOSI(1); + UART1TX(1); + break; + default: + UART1TX(0); + break; + } + break; + + case 0b0001://get x+1 bytes + inByte &= (~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1); //send 1/OK + + for (i = 0; i < inByte; i++) { + c = UART1RX(); // /* JTR usb port; */; + if (modeConfig.lsbEN == 1) {//adjust bitorder + c = bpRevByte(c); + } + if (wires == 2) {//2 wire, send 1 + bbWriteByte(c); //send byte + UART1TX(1); + } else { //3 wire, return read byte + c = bbReadWriteByte(c); //send byte + if (modeConfig.lsbEN == 1) {//adjust bitorder + c = bpRevByte(c); + } + UART1TX(c); + } + } + + break; + + case 0b0010://bulk clock ticks + inByte &= (~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + bbClockTicks(inByte); + UART1TX(1); //send 1/OK + break; + + case 0b0011: //# 0011xxxx - Bulk bits, send 1-8 bits of the next byte (0=1bit!) + inByte &= (~0b11110000); //clear command portion + inByte++; //increment by 1, 0=1byte + UART1TX(1); //send 1/OK + + rawCommand = UART1RX(); // //get byte, reuse rawCommand variable + for (i = 0; i < inByte; i++) { + if (rawCommand & 0b10000000) {//send 1 + bbWriteBit(1); //send bit + } else { //send 0 + bbWriteBit(0); //send bit + } + rawCommand = rawCommand << 1; //pop the MSB off + } + UART1TX(1); + break; + + case 0b1010:// PIC commands + + switch (inByte) { + case 0b10100000: + + picMode = UART1RX(); // /* JTR usb port; */; //get byte + UART1TX(1); //send 1/OK + break; + case 0b10100100: //write + switch (picMode) { + case PIC416: + + //get the number of commands that will follow + cmds = UART1RX(); // //get byte, reuse rawCommand variable + cmds = cmds * 3; //make sure an int + //get command byte, two data bytes + for (j = 0; j < cmds; j++) { + + bpConfig.terminalInput[j] = UART1RX(); // /* JTR usb port; */; + } + + for (j = 0; j < cmds; j = j + 3) { + PIC416Write(bpConfig.terminalInput[j], bpConfig.terminalInput[j + 1], bpConfig.terminalInput[j + 2]); + } + + UART1TX(1); //send 1/OK + break; + case PIC424: + //get the number of commands that will follow + cmds = UART1RX(); // /* JTR usb port; */; //get byte, reuse rawCommand variable + cmds = cmds * 4; //make sure an int + //get three byte command, 1 byte pre-post NOP + for (j = 0; j < cmds; j++) { + + bpConfig.terminalInput[j] = UART1RX(); // /* JTR usb port; */; + } + + for (j = 0; j < cmds; j = j + 4) { + //do any pre instruction NOPs + + //send four bit SIX command (write) + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + + //send data payload + bbWriteByte(bpConfig.terminalInput[j]); //send byte + bbWriteByte(bpConfig.terminalInput[j + 1]); //send byte + bbWriteByte(bpConfig.terminalInput[j + 2]); //send byte + + //do any post instruction NOPs + bpConfig.terminalInput[j + 3] &= 0x0F; + for (i = 0; i < bpConfig.terminalInput[j + 3]; i++) { + PIC24NOP(); + } + } + UART1TX(1); //send 1/OK + break; + default: + UART1TX(0); //send 1/OK + break; + } + break; + case 0b10100101://write x bit command, read x bits and return in 2 bytes + switch (picMode) { + case PIC416: + + //get the number of commands that will follow + cmds = UART1RX(); // //get byte, reuse rawCommand variable + //cmds=cmds; //make sure an int + //get teh command to send on each read.... + + rawCommand = UART1RX(); // /* JTR usb port; */; + + + for (j = 0; j < cmds; j++) { + //write command + c = rawCommand; //temporary varaible + for (i = 0; i < 4; i++) { + if (c & 0b1) {//send 1 + bbWriteBit(1); //send bit + } else { //send 0 + bbWriteBit(0); //send bit + } + c = c >> 1; //pop the LSB off + } + bbReadByte(); //dummy byte, setup input + UART1TX(bbReadByte()); + } + break; + case PIC424: + //get the number of commands that will follow + cmds = UART1RX(); // //get byte, reuse rawCommand variable + + for (j = 0; j < cmds; j++) { + //write command + PIC424Write_internal(0xBA0B96, 2); + PIC424Read(); + + PIC424Write_internal(0xBADBB6, 2); + PIC424Write_internal(0xBAD3D6, 2); + PIC424Read(); + + PIC424Write_internal(0xBA0BB6, 2); + PIC424Read(); + + } + break; + } + break; + case 0b10100111: // x write, y read commands. + cmdw = UART1RX(); + cmdr = UART1RX(); + + if (picMode == PIC424) { + cmds = cmdw * 5 + cmdr; + } else if (picMode == PIC416) { + cmds = cmdw * 4 + cmdr * 2; + } else if (picMode == PIC614) { + cmds = cmdw * 4 + cmdr * 2; + } else { + UART1TX(0); + break; + } + + for (j = 0; j < cmds; j++) { + bpConfig.terminalInput[j] = UART1RX(); + } + + if (cmdr != 0) + UART1TX(1); // ACK + + j=0; + while (j < cmds) { + if (bpConfig.terminalInput[j] == 1) { // write command + if (picMode == PIC614) { + PIC614Write(bpConfig.terminalInput[j+1], bpConfig.terminalInput[j + 2], bpConfig.terminalInput[j + 3]); + j += 4; + } else if (picMode == PIC416) { + PIC416Write(bpConfig.terminalInput[j+1], bpConfig.terminalInput[j + 2], bpConfig.terminalInput[j + 3]); + j += 4; + } else if (picMode == PIC424) { + PIC424Write(&bpConfig.terminalInput[j + 1], bpConfig.terminalInput[j + 4]); + j += 5; + } + } else if (bpConfig.terminalInput[j] == 2) { // read command + if (picMode == PIC614) { + PIC614Read(bpConfig.terminalInput[j+1]); + j += 2; + } else if (picMode == PIC416) { + PIC416Read(bpConfig.terminalInput[j+1]); + j += 2; + } else if (picMode == PIC424) { + PIC424Read(); + j++; + } + } + } + + if (cmdr == 0) + UART1TX(1); // ACK + + break; + default: + UART1TX(0x00); //send 0/Error + break; + } + + break; + + //case 0b0101: //# 0101xxxx - Bulk read, read 1-16bytes (0=1byte!) + + case 0b0100: //configure peripherals w=power, x=pullups, y=AUX, z=CS + binIOperipheralset(inByte); + UART1TX(1); //send 1/OK + break; + +#ifdef BUSPIRATEV4 + case 0b0101: + UART1TX(binBBpullVoltage(inByte)); + break; +#endif + + case 0b0110://set speed + inByte &= (~0b11111100); //clear command portion + modeConfig.speed = inByte; + bbSetup(wires, modeConfig.speed); + bbCS(1); //takes care of custom HiZ settings too + UART1TX(1); + break; + + case 0b1000: //set config + //wxyz //w=HiZ(0)/3.3v(1), x=3wireenable, y=lsb, z=n/a + modeConfig.HiZ = 0; + if ((inByte & 0b1000) == 0) modeConfig.HiZ = 1; //hiz output if this bit is 1 + + wires = 2; + if (inByte & 0b100) wires = 3; //3wire/2wire toggle + + modeConfig.lsbEN = 0; + if (inByte & 0b10) modeConfig.lsbEN = 1; //lsb/msb, bit order + + //if(inByte&0b1) //bit unused + + bbSetup(wires, modeConfig.speed); //setup the bitbang library, must be done before calling bbCS below + bbCS(1); //takes care of custom HiZ settings too + UART1TX(1); //send 1/OK + break; +#ifdef BUSPIRATEV4 + case 0b1111: // SMPS commands + switch (inByte) { + case 0xf0: + smpsADC(); // Send raw ADC reading + break; + case 0xf1: + smpsStop(); // Stop SMPS operation + UART1TX(1); // Send 1/OK + break; + default: + V_out = inByte & 0x0f; + V_out <<= 8; + V_out |= UART1RX(); + smpsStart(V_out); + UART1TX(1); // Send 1/OK + break; + } + break; +#endif + default: + UART1TX(0x00); //send 0/Error + break; + }//command switch + }//while loop + +} + +void PIC614Read(unsigned char c) { + unsigned char i; + + for (i = 0; i < 6; i++) { + bbWriteBit(c & 0b1); //send bit + c = c >> 1; //pop the LSB off + } + + UART1TX(bbReadByte()); + UART1TX(bbReadByte()); +} + +void PIC614Write(unsigned char cmd, unsigned char datl, unsigned char dath) { + unsigned char i, nodata; + + // MSB tells that there is no data output + nodata = cmd & 0x80; + + for (i = 0; i < 6; i++) { + bbWriteBit(cmd & 0b1); //send bit + cmd = cmd >> 1; //pop the LSB off + } + + if (nodata) + return; + + bbWriteByte(datl); //send byte + bbWriteByte(dath); //send byte + +} + +void PIC416Read(unsigned char c) { + unsigned char i; + + for (i = 0; i < 4; i++) { + if (c & 0b1) {//send 1 + bbWriteBit(1); //send bit + } else { //send 0 + bbWriteBit(0); //send bit + } + c = c >> 1; //pop the LSB off + } + + bbReadByte(); //dummy byte, setup input + UART1TX(bbReadByte()); +} + +void PIC416Write(unsigned char cmd, unsigned char datl, unsigned char dath) { + unsigned char i, delay; + + //use upper 2 bits of pic[0] to determine a delay, if any. + delay = cmd >> 6; + + for (i = 0; i < 4; i++) { + + //hold data for write time + if (i == 3 && (delay > 0)) { + bbCLK(1); + bpDelayMS(delay); + bbCLK(0); + continue; + } + + if (cmd & 0b1) {//send 1 + bbWriteBit(1); //send bit + } else { //send 0 + bbWriteBit(0); //send bit + } + cmd = cmd >> 1; //pop the LSB off + } + + bbWriteByte(datl); //send byte + bbWriteByte(dath); //send byte + +} + +void PIC24NOP(void) { + //send four bit SIX command (write) + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + + //send data payload + bbWriteByte(0x00); //send byte + bbWriteByte(0x00); //send byte + bbWriteByte(0x00); //send byte + +} + +void PIC424Write(unsigned char *cmd, unsigned char pn) { + //send four bit SIX command (write) + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + + //send data payload + bbWriteByte(cmd[0]); //send byte + bbWriteByte(cmd[1]); //send byte + bbWriteByte(cmd[2]); //send byte + + //do any post instruction NOPs + pn &= 0x0F; + while(pn--) { + PIC24NOP(); + } +} + +void PIC424Write_internal(unsigned long cmd, unsigned char pn) { + unsigned char i; + //send four bit SIX command (write) + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + + //send data payload 0xBA0B96 0xBADBB6 0xBA0BB6 + bbWriteByte(bpRevByte(cmd)); //send byte + bbWriteByte(bpRevByte(cmd >> 8)); //send byte + bbWriteByte(bpRevByte(cmd >> 16)); //send byte + + //do any post instruction NOPs + pn &= 0x0F; + for (i = 0; i < pn; i++) { + PIC24NOP(); + } +} + +void PIC424Read(void) { + unsigned char c; + + //send four bit REGOUT command (read) + bbWriteBit(1); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + bbWriteBit(0); //send bit + + //one byte output + bbWriteByte(0x00); //send byte + + //read 2 bytes + //return bytes in little endian format + c = bbReadByte(); + UART1TX(bbReadByte()); + UART1TX(c); + + //ALWAYS POST nop TWICE after a read + PIC24NOP(); + PIC24NOP(); +} diff --git a/Firmware/binwire.h b/Firmware/binwire.h index 800a9298..c5151a2e 100644 --- a/Firmware/binwire.h +++ b/Firmware/binwire.h @@ -1,2 +1,2 @@ -void binwire(void); - +void binwire(void); + diff --git a/Firmware/bitbang.c b/Firmware/bitbang.c index f72d0768..97205708 100644 --- a/Firmware/bitbang.c +++ b/Firmware/bitbang.c @@ -1,292 +1,292 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//Bus Pirate bitbang base library -//consolidates all bitbang code into one place - -// The software i2c routines were written in c from public domain pseudo code: -/// **** I2C Driver V1.1 Written by V.Himpe. Released as Public Domain **** / -// http://www.esacademy.com/faq/i2c/general/i2cpseud.htm -#include "base.h" -#include "bitbang.h" //need own functions - -#define BB_5KHZSPEED_SETTLE 20 //~5KHz -#define BB_5KHZSPEED_CLOCK 100 -#define BB_5KHZSPEED_HALFCLOCK BB_5KHZSPEED_CLOCK/2 - -#define BB_50KHZSPEED_SETTLE 2 //~50KHz -#define BB_50KHZSPEED_CLOCK 10 -#define BB_50KHZSPEED_HALFCLOCK BB_50KHZSPEED_CLOCK/2 - -#define BB_100KHZSPEED_SETTLE 1 //~100KHz -#define BB_100KHZSPEED_CLOCK 5 -#define BB_100KHZSPEED_HALFCLOCK 2 - -#define BB_MAXSPEED_SETTLE 0 //~400KHz -#define BB_MAXSPEED_CLOCK 0 -#define BB_MAXSPEED_HALFCLOCK 0 - -extern struct _modeConfig modeConfig; - -struct _bitbang{ - unsigned char pins; - unsigned int MOpin; - unsigned int MIpin; - unsigned char delaySettle; - unsigned char delayClock; - unsigned char delayHalfClock; -} bitbang; - -void bbSetup(unsigned char pins, unsigned char speed){ - - bitbang.pins=pins; - - //define pins for 2 or 3 wire modes (do we use a seperate input pin) - if(pins==3){ //SPI-like - bitbang.MOpin=MOSI; - bitbang.MIpin=MISO; - }else{ //I2C-like - bitbang.MOpin=MOSI; - bitbang.MIpin=MOSI; - } - - - //define delays for differnt speeds - // I2C Bus timing in uS - switch(speed){ - case 0: - bitbang.delaySettle = BB_5KHZSPEED_SETTLE; - bitbang.delayClock = BB_5KHZSPEED_CLOCK; - bitbang.delayHalfClock = BB_5KHZSPEED_HALFCLOCK; - break; - case 1: - bitbang.delaySettle = BB_50KHZSPEED_SETTLE; - bitbang.delayClock = BB_50KHZSPEED_CLOCK; - bitbang.delayHalfClock = BB_50KHZSPEED_HALFCLOCK; - break; - case 2: - bitbang.delaySettle = BB_100KHZSPEED_SETTLE; - bitbang.delayClock = BB_100KHZSPEED_CLOCK; - bitbang.delayHalfClock = BB_100KHZSPEED_HALFCLOCK; - break; - default: - bitbang.delaySettle = BB_MAXSPEED_SETTLE; - bitbang.delayClock = BB_MAXSPEED_CLOCK; - bitbang.delayHalfClock = BB_MAXSPEED_HALFCLOCK; - break; - } - - -} - -// -// HELPER functions -// - -int bbI2Cstart(void){ - int error=0; -//http://www.esacademy.com/faq/i2c/busevents/i2cstast.htm - //setup both lines high first - bbH(MOSI+CLK, bitbang.delayClock); - - //check bus state, return error if held low - if(BP_CLK==0 || BP_MOSI==0) error=1; - - //now take data low while clock is high - bbL(MOSI, bitbang.delayClock); - - //next take clock low too - bbL(CLK, bitbang.delayClock); - - //example suggests returning SDA to high - bbH(MOSI, bitbang.delayClock); - - return error; - -} - - -int bbI2Cstop(void){ -//http://www.esacademy.com/faq/i2c/busevents/i2cstast.htm - - //setup both lines low first - //example suggests just SDA, but some chips are flakey. - bbL(MOSI+CLK, bitbang.delayClock); - - //take clock high - bbH(CLK, bitbang.delayClock); - - //with clock high, bring data high too - bbH(MOSI, bitbang.delayClock); - - //return clock low, importatnt for raw2wire smartcard - //bbL(CLK, bitbang.delayClock); - return 0; -} - -// -//BYTE functions -// - -// ** Read with write for 3-wire protocols ** // - -//unsigned char bbReadWriteByte(unsigned char c){ -unsigned int bbReadWriteByte(unsigned int c){ - unsigned int i,bt,tem,di,dat=0; - - //begin with clock low... - bt=1<<(modeConfig.numbits-1); - - tem=c;//???? -// for(i=0;i<8;i++){ - for(i=0;i - -enum stopbits { - one = 0, oneandahalf = 1, two = 2 -}; - -enum parity { - none = 0, odd = 1, even = 2, mark = 3, space = 4 -}; -const char parity_str[] = {'N', 'O', 'E', 'M', 'S'}; - -struct cdc_LineCodeing { - unsigned long int dwDTERate; - enum stopbits bCharFormat; - enum parity bParityType; - BYTE bDataBits; -} linecodeing; - - -#pragma udata usb_data -BYTE cdc_acm_in_buffer[CDC_NOTICE_BUFFER_SIZE]; //JTR NEWLY defined NOTICE BUFFER SIZE and increased from 8 to 10 bytes in usb_config.h - -#pragma udata usb_data3 -BYTE cdc_In_bufferA[CDC_BUFFER_SIZE]; -BYTE cdc_In_bufferB[CDC_BUFFER_SIZE]; -BYTE cdc_Out_bufferA[CDC_BUFFER_SIZE]; -BYTE cdc_Out_bufferB[CDC_BUFFER_SIZE]; - -#pragma udata - -struct _cdc_ControlLineState cls; -BYTE cdc_In_len; // total cdc In length -volatile BYTE cdc_Out_len; // total cdc out length -BYTE IsInBufferA; -BYTE IsOutBufferA; -BYTE *InPtr; -BYTE *OutPtr; -BYTE LineStateUpdated = 0; -BYTE cdc_timeout_count = 0; -BYTE ZLPpending = 0; -BYTE lock = 0; - -BDentry *CDC_Outbdp, *CDC_Inbdp; -BYTE CDCFunctionError; - -volatile BYTE cdc_trf_state; // JTR don't see that it is really volatile in current context may be in future. - -void initCDC(void) { - - // JTR The function usb_init() is now called from main.c prior to anything else belonging to the CDC CLASS - // If we have multiple functions we want the USB initialization to be in only one consistant place. - // The sort of things we would do in InitCDC would be to setup I/O pins and the HARDWARE UART so it - // is not transmitting junk between a RESET and the device being enumerated. Hardware CTS/RTS - // would also be setup here if being used. - - linecodeing.dwDTERate = 115200; - linecodeing.bCharFormat = one; - linecodeing.bParityType = none; - linecodeing.bDataBits = 8; - cls.DTR = 0; - cls.RTS = 0; - usb_register_class_setup_handler(cdc_setup); -} - -void user_configured_init(void) { - // JTR NEW FUNCTION - // After the device is enumerated and configured then we set up non EP0 endpoints. - // We only enable the endpoints we are using, not all of them. - // Prior to this they are held in a disarmed state. - - // This function belongs to the current USB function and IS NOT generic. This is CLASS specific - // and will vary from implementation to implementation. - - usb_unset_in_handler(1); - usb_unset_in_handler(2); - usb_unset_out_handler(2); - - USB_UEP1 = USB_EP_IN; - USB_UEP2 = USB_EP_INOUT; - - /* Configure buffer descriptors */ -#if USB_PP_BUF_MODE == 0 - // JTR Setup CDC LINE_NOTICE EP (Interrupt IN) - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_acm_in_buffer; - usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 -#else - // TODO: Implement Ping-Pong buffering setup. -#error "PP Mode not implemented yet" -#endif - - usb_register_class_setup_handler(cdc_setup); - cdc_trf_state = 0; - CDC_Outbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)]; - CDC_Inbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)]; - - IsInBufferA = 0xFF; - InPtr = cdc_In_bufferA; - cdc_In_len = 0; - CDC_Inbdp->BDADDR = &cdc_In_bufferA[0]; - CDC_Inbdp->BDCNT = 0; - CDC_Inbdp->BDSTAT = DTS + DTSEN; - - cdc_Out_len = 0; - IsOutBufferA = 0xFF; - OutPtr = cdc_Out_bufferA; - CDC_Outbdp->BDCNT = CDC_BUFFER_SIZE; - CDC_Outbdp->BDADDR = &cdc_Out_bufferA[0]; - CDC_Outbdp->BDSTAT = UOWN + DTSEN; -} - -void cdc_setup(void) { - BYTE *packet; - size_t reply_len; - packet = EP0_Outbdp->BDADDR; - - switch (packet[USB_bmRequestType] & (USB_bmRequestType_TypeMask | USB_bmRequestType_RecipientMask)) { - case (USB_bmRequestType_Class | USB_bmRequestType_Interface): - switch (packet[USB_bRequest]) { - - //JTR This is just a dummy, nothing defined to do for CDC ACM - case CDC_SEND_ENCAPSULATED_COMMAND: - usb_ack_dat1(0); - break; - - //JTR This is just a dummy, nothing defined to do for CDC ACM - case CDC_GET_ENCAPSULATED_RESPONSE: - //usb_ack_zero(rbdp); - usb_ack_dat1(0); - break; - - case CDC_SET_COMM_FEATURE: // Optional - case CDC_GET_COMM_FEATURE: // Optional - case CDC_CLEAR_COMM_FEATURE: // Optional - usb_RequestError(); // Not advertised in ACM functional descriptor - break; - - case CDC_SET_LINE_CODING: // Optional, strongly recomended - usb_set_out_handler(0, cdc_set_line_coding_data); // Register out handler function - break; - - case CDC_GET_LINE_CODING: // Optional, strongly recomended - // JTR reply length (7) is always going to be less than minimum EP0 size (8) - - reply_len = *((unsigned int *) &packet[USB_wLength]); - if (sizeof (struct cdc_LineCodeing) < reply_len) { - reply_len = sizeof (struct cdc_LineCodeing); - } - memcpy(EP0_Inbdp->BDADDR, (const void *) &linecodeing, reply_len); - usb_ack_dat1(reply_len); // JTR common addition for STD and CLASS ACK - usb_set_in_handler(0, cdc_get_line_coding); - break; - - case CDC_SET_CONTROL_LINE_STATE: // Optional - cls = *((struct _cdc_ControlLineState *) &packet[USB_wValue]); - usb_set_in_handler(0, cdc_set_control_line_state_status); // JTR why bother? - usb_ack_dat1(0); // JTR common addition for STD and CLASS ACK - LineStateUpdated = 1; - break; - - case CDC_SEND_BREAK: // Optional - default: - usb_RequestError(); - } - break; - default: - usb_RequestError(); - } -} - -void cdc_get_line_coding(void) { - usb_unset_in_handler(0); // Unregister IN handler; -} - -void cdc_set_line_coding_data(void) { // JTR handling an OUT token In the CDC stack this is the only function that handles an OUT data stage. - unsigned long dwBaud, dwBaudrem; - - memcpy(&linecodeing, (const void *) EP0_Outbdp->BDADDR, sizeof (struct cdc_LineCodeing)); - - dwBaud = BAUDCLOCK_FREQ / linecodeing.dwDTERate; - dwBaudrem = BAUDCLOCK_FREQ % linecodeing.dwDTERate; - if (linecodeing.dwDTERate > (dwBaudrem << 1)) - dwBaud--; - - UART_BAUD_setup(dwBaud); - - usb_unset_out_handler(0); // Unregister OUT handler; JTR serious bug fix in macro! - usb_set_in_handler(0, cdc_set_line_coding_status); // JTR why bother? - usb_ack_dat1(0); // JTR common addition for STD and CLASS ACK - - // JTR This part of the USB-CDC stack is worth highlighting - // This is the only place that we have an OUT DATA packet on - // EP0. At this point it has been completed. This stack unlike - // the microchip stack does not have a common IN or OUT data - // packet complete tail and therefore it is the responsibility - // of each section to ensure that EP0 is set-up correctly for - // the next setup packet. - - - // Force EP0 OUT to the DAT0 state - // after we have all our data packets. - EP0_Outbdp->BDCNT = USB_EP0_BUFFER_SIZE; - EP0_Outbdp->BDSTAT = UOWN | DTSEN; -} - -void cdc_set_line_coding_status(void) { - usb_unset_in_handler(0); -} - -void cdc_set_control_line_state_status(void) { - usb_unset_in_handler(0); -} - -/*****************************************************************************/ -void WaitOutReady() // JTR2 added reduced overhead -{ - while ((CDC_Outbdp->BDSTAT & UOWN)); -} - -/******************************************************************************/ - -void WaitInReady() // JTR2 added reduced overhead -{ - while ((CDC_Inbdp->BDSTAT & UOWN)); -}//end WaitInReady - -/******************************************************************************/ -BYTE getOutReady(void) { - - return !(CDC_Outbdp->BDSTAT & UOWN); // Do we have a packet from host? -} - -/******************************************************************************/ -BYTE getInReady(void) { - - return !(CDC_Inbdp->BDSTAT & UOWN); // Is the CDC In buffer ready? -} - -/******************************************************************************/ -BYTE getda_cdc(void) { - - CDCFunctionError = 0; - - WaitOutReady(); - - if ((IsOutBufferA & 1)) { - OutPtr = &cdc_Out_bufferA[0]; - CDC_Outbdp->BDADDR = &cdc_Out_bufferB[0]; - } else { - OutPtr = &cdc_Out_bufferB[0]; - CDC_Outbdp->BDADDR = &cdc_Out_bufferA[0]; - } - IsOutBufferA ^= 0xFF; - cdc_Out_len = CDC_Outbdp->BDCNT; - CDC_Outbdp->BDCNT = CDC_BUFFER_SIZE; - CDC_Outbdp->BDSTAT = ((CDC_Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; -#ifndef USB_INTERRUPTS - usb_handler(); -#endif - return cdc_Out_len; -}//end getCDC_Out_ArmNext - -BYTE putda_cdc(BYTE count) { - - // CDCFunctionError = 0; - // WaitInReady(); - while ((CDC_Inbdp->BDSTAT & UOWN)); - if (IsInBufferA) { - CDC_Inbdp->BDADDR = cdc_In_bufferA; - InPtr = cdc_In_bufferB; - } else { - CDC_Inbdp->BDADDR = cdc_In_bufferB; - InPtr = cdc_In_bufferA; - } - CDC_Inbdp->BDCNT = count; - CDC_Inbdp->BDSTAT = ((CDC_Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; - IsInBufferA ^= 0xFF; -#ifndef USB_INTERRUPTS - usb_handler(); -#endif - return 0; //CDCFunctionError; -} - -void SendZLP(void) { - putda_cdc(0); -} - -/******************************************************************************/ -void CDC_Flush_In_Now(void) { - if (cdc_In_len > 0) { - while (!getInReady()); - putda_cdc(cdc_In_len); - if (cdc_In_len == CDC_BUFFER_SIZE) { - ZLPpending = 1; - } else { - ZLPpending = 0; - } - cdc_In_len = 0; - cdc_timeout_count = 0; - } -} - -/******************************************************************************/ -void CDCFlushOnTimeout(void) { - - if (cdc_timeout_count >= CDC_FLUSH_MS) { // For timeout value see: cdc_config.h -> [hardware] -> CDC_FLUSH_MS - - if (cdc_In_len > 0) { - if ((lock == 0) && getInReady()) { - putda_cdc(cdc_In_len); - if (cdc_In_len == CDC_BUFFER_SIZE) { - ZLPpending = 1; - } else { - ZLPpending = 0; - } - cdc_In_len = 0; - cdc_timeout_count = 0; - } - } else if (ZLPpending) { - putda_cdc(0); - ZLPpending = 0; - cdc_timeout_count = 0; - } - } else { - cdc_timeout_count++; - } -} - -/******************************************************************************/ -void putc_cdc(BYTE c) { - lock = 1; // Stops CDCFlushOnTimeout() from sending per chance it is on interrupts. - *InPtr = c; - InPtr++; - cdc_In_len++; - ZLPpending = 0; - if (cdc_In_len == CDC_BUFFER_SIZE) { - putda_cdc(cdc_In_len); // This will stall tranfers if both buffers are full then return when a buffer is available. - cdc_In_len = 0; - ZLPpending = 1; // timeout handled in the SOF handler below. - } - lock = 0; - cdc_timeout_count = 0; //setup timer to throw data if the buffer doesn't fill -} - -/******************************************************************************/ -// Waits for a byte to be available and returns that byte as a -// function return value. The byte is removed from the CDC OUT queue. -// No return count is required as this function always returns one byte. - -BYTE getc_cdc(void) { // Must be used only in double buffer mode. - BYTE c = 0; - - if (cdc_Out_len == 0) { - do { - cdc_Out_len = getda_cdc(); - } while (cdc_Out_len == 0); // Skip any ZLP - } - c = *OutPtr; - OutPtr++; - cdc_Out_len--; - return c; -} - -/******************************************************************************/ -// Checks to see if there is a byte available in the CDC buffer. -// If so, it returns that byte at the dereferenced pointer *C -// and the function returns a count of 1. The byte is effectively -// removed from the queue. -// IF no byte is available function returns immediately with a count of zero. - -BYTE poll_getc_cdc(BYTE * c) { // Must be used only in double buffer mode. - - if (cdc_Out_len) { // Do we have a byte waiting? - *c = *OutPtr; // pass it on and adjust OutPtr and count - OutPtr++; - cdc_Out_len--; - return 1; // Return byte count, always one. - } - if (getOutReady()) { // No byte in queue check for new arrivals. - cdc_Out_len = getda_cdc(); - if (cdc_Out_len) { - *c = *OutPtr; - OutPtr++; - cdc_Out_len--; - return 1; - } - } - return 0; -} - -/******************************************************************************/ -// Checks (PEEKS) to see if there is a byte available in the CDC buffer. -// If so, it returns that byte at the dereferenced pointer *C -// and the function returns a count of 1. The byte however is NOT -// removed from the queue and can still be read with the poll_getc_cdc() -// and getc_cdc() functions that will remove it from the queue. -// IF no byte is available function returns immediately with a count of zero. - -BYTE peek_getc_cdc(BYTE * c) { // Must be used only in double buffer mode. - - if (cdc_Out_len) { - *c = *OutPtr; - return 1; - } - if (getOutReady()) { - cdc_Out_len = getda_cdc(); - if (cdc_Out_len) { - *c = *OutPtr; - return 1; - } - } - return 0; -} - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + */ + +// JTR V0.1a +// JTR v0.1b +// JTR v0.1c // tidy up added code line code handler to set the SPBRG based on line coding. +// JTR V0.2a // 26th Jan 2012 + +#include "../dp_usb/usb_stack_globals.h" // USB stack only defines Not function related. + +#include + +enum stopbits { + one = 0, oneandahalf = 1, two = 2 +}; + +enum parity { + none = 0, odd = 1, even = 2, mark = 3, space = 4 +}; +const char parity_str[] = {'N', 'O', 'E', 'M', 'S'}; + +struct cdc_LineCodeing { + unsigned long int dwDTERate; + enum stopbits bCharFormat; + enum parity bParityType; + BYTE bDataBits; +} linecodeing; + + +#pragma udata usb_data +BYTE cdc_acm_in_buffer[CDC_NOTICE_BUFFER_SIZE]; //JTR NEWLY defined NOTICE BUFFER SIZE and increased from 8 to 10 bytes in usb_config.h + +#pragma udata usb_data3 +BYTE cdc_In_bufferA[CDC_BUFFER_SIZE]; +BYTE cdc_In_bufferB[CDC_BUFFER_SIZE]; +BYTE cdc_Out_bufferA[CDC_BUFFER_SIZE]; +BYTE cdc_Out_bufferB[CDC_BUFFER_SIZE]; + +#pragma udata + +struct _cdc_ControlLineState cls; +BYTE cdc_In_len; // total cdc In length +volatile BYTE cdc_Out_len; // total cdc out length +BYTE IsInBufferA; +BYTE IsOutBufferA; +BYTE *InPtr; +BYTE *OutPtr; +BYTE LineStateUpdated = 0; +BYTE cdc_timeout_count = 0; +BYTE ZLPpending = 0; +BYTE lock = 0; + +BDentry *CDC_Outbdp, *CDC_Inbdp; +BYTE CDCFunctionError; + +volatile BYTE cdc_trf_state; // JTR don't see that it is really volatile in current context may be in future. + +void initCDC(void) { + + // JTR The function usb_init() is now called from main.c prior to anything else belonging to the CDC CLASS + // If we have multiple functions we want the USB initialization to be in only one consistant place. + // The sort of things we would do in InitCDC would be to setup I/O pins and the HARDWARE UART so it + // is not transmitting junk between a RESET and the device being enumerated. Hardware CTS/RTS + // would also be setup here if being used. + + linecodeing.dwDTERate = 115200; + linecodeing.bCharFormat = one; + linecodeing.bParityType = none; + linecodeing.bDataBits = 8; + cls.DTR = 0; + cls.RTS = 0; + usb_register_class_setup_handler(cdc_setup); +} + +void user_configured_init(void) { + // JTR NEW FUNCTION + // After the device is enumerated and configured then we set up non EP0 endpoints. + // We only enable the endpoints we are using, not all of them. + // Prior to this they are held in a disarmed state. + + // This function belongs to the current USB function and IS NOT generic. This is CLASS specific + // and will vary from implementation to implementation. + + usb_unset_in_handler(1); + usb_unset_in_handler(2); + usb_unset_out_handler(2); + + USB_UEP1 = USB_EP_IN; + USB_UEP2 = USB_EP_INOUT; + + /* Configure buffer descriptors */ +#if USB_PP_BUF_MODE == 0 + // JTR Setup CDC LINE_NOTICE EP (Interrupt IN) + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDADDR = cdc_acm_in_buffer; + usb_bdt[USB_CALC_BD(1, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 +#else + // TODO: Implement Ping-Pong buffering setup. +#error "PP Mode not implemented yet" +#endif + + usb_register_class_setup_handler(cdc_setup); + cdc_trf_state = 0; + CDC_Outbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_OUT, USB_PP_EVEN)]; + CDC_Inbdp = &usb_bdt[USB_CALC_BD(2, USB_DIR_IN, USB_PP_EVEN)]; + + IsInBufferA = 0xFF; + InPtr = cdc_In_bufferA; + cdc_In_len = 0; + CDC_Inbdp->BDADDR = &cdc_In_bufferA[0]; + CDC_Inbdp->BDCNT = 0; + CDC_Inbdp->BDSTAT = DTS + DTSEN; + + cdc_Out_len = 0; + IsOutBufferA = 0xFF; + OutPtr = cdc_Out_bufferA; + CDC_Outbdp->BDCNT = CDC_BUFFER_SIZE; + CDC_Outbdp->BDADDR = &cdc_Out_bufferA[0]; + CDC_Outbdp->BDSTAT = UOWN + DTSEN; +} + +void cdc_setup(void) { + BYTE *packet; + size_t reply_len; + packet = EP0_Outbdp->BDADDR; + + switch (packet[USB_bmRequestType] & (USB_bmRequestType_TypeMask | USB_bmRequestType_RecipientMask)) { + case (USB_bmRequestType_Class | USB_bmRequestType_Interface): + switch (packet[USB_bRequest]) { + + //JTR This is just a dummy, nothing defined to do for CDC ACM + case CDC_SEND_ENCAPSULATED_COMMAND: + usb_ack_dat1(0); + break; + + //JTR This is just a dummy, nothing defined to do for CDC ACM + case CDC_GET_ENCAPSULATED_RESPONSE: + //usb_ack_zero(rbdp); + usb_ack_dat1(0); + break; + + case CDC_SET_COMM_FEATURE: // Optional + case CDC_GET_COMM_FEATURE: // Optional + case CDC_CLEAR_COMM_FEATURE: // Optional + usb_RequestError(); // Not advertised in ACM functional descriptor + break; + + case CDC_SET_LINE_CODING: // Optional, strongly recomended + usb_set_out_handler(0, cdc_set_line_coding_data); // Register out handler function + break; + + case CDC_GET_LINE_CODING: // Optional, strongly recomended + // JTR reply length (7) is always going to be less than minimum EP0 size (8) + + reply_len = *((unsigned int *) &packet[USB_wLength]); + if (sizeof (struct cdc_LineCodeing) < reply_len) { + reply_len = sizeof (struct cdc_LineCodeing); + } + memcpy(EP0_Inbdp->BDADDR, (const void *) &linecodeing, reply_len); + usb_ack_dat1(reply_len); // JTR common addition for STD and CLASS ACK + usb_set_in_handler(0, cdc_get_line_coding); + break; + + case CDC_SET_CONTROL_LINE_STATE: // Optional + cls = *((struct _cdc_ControlLineState *) &packet[USB_wValue]); + usb_set_in_handler(0, cdc_set_control_line_state_status); // JTR why bother? + usb_ack_dat1(0); // JTR common addition for STD and CLASS ACK + LineStateUpdated = 1; + break; + + case CDC_SEND_BREAK: // Optional + default: + usb_RequestError(); + } + break; + default: + usb_RequestError(); + } +} + +void cdc_get_line_coding(void) { + usb_unset_in_handler(0); // Unregister IN handler; +} + +void cdc_set_line_coding_data(void) { // JTR handling an OUT token In the CDC stack this is the only function that handles an OUT data stage. + unsigned long dwBaud, dwBaudrem; + + memcpy(&linecodeing, (const void *) EP0_Outbdp->BDADDR, sizeof (struct cdc_LineCodeing)); + + dwBaud = BAUDCLOCK_FREQ / linecodeing.dwDTERate; + dwBaudrem = BAUDCLOCK_FREQ % linecodeing.dwDTERate; + if (linecodeing.dwDTERate > (dwBaudrem << 1)) + dwBaud--; + + UART_BAUD_setup(dwBaud); + + usb_unset_out_handler(0); // Unregister OUT handler; JTR serious bug fix in macro! + usb_set_in_handler(0, cdc_set_line_coding_status); // JTR why bother? + usb_ack_dat1(0); // JTR common addition for STD and CLASS ACK + + // JTR This part of the USB-CDC stack is worth highlighting + // This is the only place that we have an OUT DATA packet on + // EP0. At this point it has been completed. This stack unlike + // the microchip stack does not have a common IN or OUT data + // packet complete tail and therefore it is the responsibility + // of each section to ensure that EP0 is set-up correctly for + // the next setup packet. + + + // Force EP0 OUT to the DAT0 state + // after we have all our data packets. + EP0_Outbdp->BDCNT = USB_EP0_BUFFER_SIZE; + EP0_Outbdp->BDSTAT = UOWN | DTSEN; +} + +void cdc_set_line_coding_status(void) { + usb_unset_in_handler(0); +} + +void cdc_set_control_line_state_status(void) { + usb_unset_in_handler(0); +} + +/*****************************************************************************/ +void WaitOutReady() // JTR2 added reduced overhead +{ + while ((CDC_Outbdp->BDSTAT & UOWN)); +} + +/******************************************************************************/ + +void WaitInReady() // JTR2 added reduced overhead +{ + while ((CDC_Inbdp->BDSTAT & UOWN)); +}//end WaitInReady + +/******************************************************************************/ +BYTE getOutReady(void) { + + return !(CDC_Outbdp->BDSTAT & UOWN); // Do we have a packet from host? +} + +/******************************************************************************/ +BYTE getInReady(void) { + + return !(CDC_Inbdp->BDSTAT & UOWN); // Is the CDC In buffer ready? +} + +/******************************************************************************/ +BYTE getda_cdc(void) { + + CDCFunctionError = 0; + + WaitOutReady(); + + if ((IsOutBufferA & 1)) { + OutPtr = &cdc_Out_bufferA[0]; + CDC_Outbdp->BDADDR = &cdc_Out_bufferB[0]; + } else { + OutPtr = &cdc_Out_bufferB[0]; + CDC_Outbdp->BDADDR = &cdc_Out_bufferA[0]; + } + IsOutBufferA ^= 0xFF; + cdc_Out_len = CDC_Outbdp->BDCNT; + CDC_Outbdp->BDCNT = CDC_BUFFER_SIZE; + CDC_Outbdp->BDSTAT = ((CDC_Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; +#ifndef USB_INTERRUPTS + usb_handler(); +#endif + return cdc_Out_len; +}//end getCDC_Out_ArmNext + +BYTE putda_cdc(BYTE count) { + + // CDCFunctionError = 0; + // WaitInReady(); + while ((CDC_Inbdp->BDSTAT & UOWN)); + if (IsInBufferA) { + CDC_Inbdp->BDADDR = cdc_In_bufferA; + InPtr = cdc_In_bufferB; + } else { + CDC_Inbdp->BDADDR = cdc_In_bufferB; + InPtr = cdc_In_bufferA; + } + CDC_Inbdp->BDCNT = count; + CDC_Inbdp->BDSTAT = ((CDC_Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; + IsInBufferA ^= 0xFF; +#ifndef USB_INTERRUPTS + usb_handler(); +#endif + return 0; //CDCFunctionError; +} + +void SendZLP(void) { + putda_cdc(0); +} + +/******************************************************************************/ +void CDC_Flush_In_Now(void) { + if (cdc_In_len > 0) { + while (!getInReady()); + putda_cdc(cdc_In_len); + if (cdc_In_len == CDC_BUFFER_SIZE) { + ZLPpending = 1; + } else { + ZLPpending = 0; + } + cdc_In_len = 0; + cdc_timeout_count = 0; + } +} + +/******************************************************************************/ +void CDCFlushOnTimeout(void) { + + if (cdc_timeout_count >= CDC_FLUSH_MS) { // For timeout value see: cdc_config.h -> [hardware] -> CDC_FLUSH_MS + + if (cdc_In_len > 0) { + if ((lock == 0) && getInReady()) { + putda_cdc(cdc_In_len); + if (cdc_In_len == CDC_BUFFER_SIZE) { + ZLPpending = 1; + } else { + ZLPpending = 0; + } + cdc_In_len = 0; + cdc_timeout_count = 0; + } + } else if (ZLPpending) { + putda_cdc(0); + ZLPpending = 0; + cdc_timeout_count = 0; + } + } else { + cdc_timeout_count++; + } +} + +/******************************************************************************/ +void putc_cdc(BYTE c) { + lock = 1; // Stops CDCFlushOnTimeout() from sending per chance it is on interrupts. + *InPtr = c; + InPtr++; + cdc_In_len++; + ZLPpending = 0; + if (cdc_In_len == CDC_BUFFER_SIZE) { + putda_cdc(cdc_In_len); // This will stall tranfers if both buffers are full then return when a buffer is available. + cdc_In_len = 0; + ZLPpending = 1; // timeout handled in the SOF handler below. + } + lock = 0; + cdc_timeout_count = 0; //setup timer to throw data if the buffer doesn't fill +} + +/******************************************************************************/ +// Waits for a byte to be available and returns that byte as a +// function return value. The byte is removed from the CDC OUT queue. +// No return count is required as this function always returns one byte. + +BYTE getc_cdc(void) { // Must be used only in double buffer mode. + BYTE c = 0; + + if (cdc_Out_len == 0) { + do { + cdc_Out_len = getda_cdc(); + } while (cdc_Out_len == 0); // Skip any ZLP + } + c = *OutPtr; + OutPtr++; + cdc_Out_len--; + return c; +} + +/******************************************************************************/ +// Checks to see if there is a byte available in the CDC buffer. +// If so, it returns that byte at the dereferenced pointer *C +// and the function returns a count of 1. The byte is effectively +// removed from the queue. +// IF no byte is available function returns immediately with a count of zero. + +BYTE poll_getc_cdc(BYTE * c) { // Must be used only in double buffer mode. + + if (cdc_Out_len) { // Do we have a byte waiting? + *c = *OutPtr; // pass it on and adjust OutPtr and count + OutPtr++; + cdc_Out_len--; + return 1; // Return byte count, always one. + } + if (getOutReady()) { // No byte in queue check for new arrivals. + cdc_Out_len = getda_cdc(); + if (cdc_Out_len) { + *c = *OutPtr; + OutPtr++; + cdc_Out_len--; + return 1; + } + } + return 0; +} + +/******************************************************************************/ +// Checks (PEEKS) to see if there is a byte available in the CDC buffer. +// If so, it returns that byte at the dereferenced pointer *C +// and the function returns a count of 1. The byte however is NOT +// removed from the queue and can still be read with the poll_getc_cdc() +// and getc_cdc() functions that will remove it from the queue. +// IF no byte is available function returns immediately with a count of zero. + +BYTE peek_getc_cdc(BYTE * c) { // Must be used only in double buffer mode. + + if (cdc_Out_len) { + *c = *OutPtr; + return 1; + } + if (getOutReady()) { + cdc_Out_len = getda_cdc(); + if (cdc_Out_len) { + *c = *OutPtr; + return 1; + } + } + return 0; +} + diff --git a/Firmware/dp_usb/cdc.h b/Firmware/dp_usb/cdc.h index 08f45fcf..e32190bb 100644 --- a/Firmware/dp_usb/cdc.h +++ b/Firmware/dp_usb/cdc.h @@ -1,107 +1,107 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - -// JTR V0.2a // 26th Jan 2012 - */ -#ifndef __CDC_H__ -#define __CDC_H__ - -void cdc_setup(void); -void cdc_set_line_coding_data(void); -void cdc_set_line_coding_status(void); -void cdc_get_line_coding(void); -void cdc_set_control_line_state_status(void); -void user_configured_init(void); // JTR added. Sets up CDC endpoints after device configured. -void WaitInReady(void); -void WaitOutReady(void); -BYTE getInReady(void); -BYTE getOutReady(void); -BYTE getda_cdc(void); -BYTE putda_cdc(BYTE count); -void SendZLP(void); -BYTE getc_cdc(void); -void putc_cdc(BYTE c); -void CDC_Flush_In_Now(void); -void CDCFlushOnTimeout(void); -BYTE poll_getc_cdc(BYTE * c); -BYTE peek_getc_cdc(BYTE * c); -void initCDC(void); - - -struct _cdc_ControlLineState { - int DTR : 1; - int RTS : 1; - int unused1 : 6; - BYTE unused2; -}; - -// CDC Request Codes -#define CDC_SEND_ENCAPSULATED_COMMAND 0x00 -#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 -#define CDC_SET_COMM_FEATURE 0x02 -#define CDC_GET_COMM_FEATURE 0x03 -#define CDC_CLEAR_COMM_FEATURE 0x04 -// RESERVED (future use) 0x05-0x0F -#define CDC_SET_AUX_LINE_STATE 0x10 -#define CDC_SET_HOOK_STATE 0x11 -#define CDC_PULSE_SETUP 0x12 -#define CDC_SEND_PULSE 0x13 -#define CDC_SET_PULSE_TIME 0x14 -#define CDC_RING_AUX_JACK 0x15 -// RESERVED (future use) 0x16-0x1F -#define CDC_SET_LINE_CODING 0x20 -#define CDC_GET_LINE_CODING 0x21 -#define CDC_SET_CONTROL_LINE_STATE 0x22 -#define CDC_SEND_BREAK 0x23 -// RESERVED (future use) 0x24-0x2F -#define CDC_SET_RINGER_PARMS 0x30 -#define CDC_GET_RINGER_PARMS 0x31 -#define CDC_SET_OPERATION_PARMS 0x32 -#define CDC_GET_OPERATION_PARMS 0x33 -#define CDC_SET_LINE_PARMS 0x34 -#define CDC_GET_LINE_PARMS 0x35 -#define CDC_DIAL_DIGITS 0x36 -#define CDC_SET_UNIT_PARAMETER 0x37 -#define CDC_GET_UNIT_PARAMETER 0x38 -#define CDC_CLEAR_UNIT_PARAMETER 0x39 -#define CDC_GET_PROFILE 0x3A -// RESERVED (future use) 0x3B-0x3F -#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 -#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x41 -#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x42 -#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 -#define CDC_GET_ETHERNET_STATISTIC 0x44 -// RESERVED (future use) 0x45-0x4F -#define CDC_SET_ATM_DATA_FORMAT 0x50 -#define CDC_GET_ATM_DEVICE_STATISTICS 0x51 -#define CDC_SET_ATM_DEFAULT_VC 0x52 -#define CDC_GET_ATM_VC_STATISTICS 0x53 -// RESERVED (future use) 0x54-0x5F -// MDLM Semantic-Model specific Requests 0x60–0x7F -// RESERVED (future use) 0x80-0xFF - -// CDC Notification Codes -#define CDC_NETWORK_CONNECTION 0x00 -#define CDC_RESPONSE_AVAILABLE 0x01 -// RESERVED (future use) 0x02-0x07 -#define CDC_AUX_JACK_HOOK_STATE 0x08 -#define CDC_RING_DETECT 0x09 -// RESERVED (future use) 0x0A-0x1F -#define CDC_SERIAL_STATE 0x20 -// RESERVED (future use) 0x21-0x27 -#define CDC_CALL_STATE_CHANGE 0x28 -#define CDC_LINE_STATE_CHANGE 0x29 -#define CDC_CONNECTION_SPEED_CHANGE 0x2A -// RESERVED 0x2B-0x3F -// MDML SEMANTIC-MODEL-SPECIFIC NOTIFICATION 0x40-0x5F -// RESERVED (future use) 0x60-0xFF -#endif +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + +// JTR V0.2a // 26th Jan 2012 + */ +#ifndef __CDC_H__ +#define __CDC_H__ + +void cdc_setup(void); +void cdc_set_line_coding_data(void); +void cdc_set_line_coding_status(void); +void cdc_get_line_coding(void); +void cdc_set_control_line_state_status(void); +void user_configured_init(void); // JTR added. Sets up CDC endpoints after device configured. +void WaitInReady(void); +void WaitOutReady(void); +BYTE getInReady(void); +BYTE getOutReady(void); +BYTE getda_cdc(void); +BYTE putda_cdc(BYTE count); +void SendZLP(void); +BYTE getc_cdc(void); +void putc_cdc(BYTE c); +void CDC_Flush_In_Now(void); +void CDCFlushOnTimeout(void); +BYTE poll_getc_cdc(BYTE * c); +BYTE peek_getc_cdc(BYTE * c); +void initCDC(void); + + +struct _cdc_ControlLineState { + int DTR : 1; + int RTS : 1; + int unused1 : 6; + BYTE unused2; +}; + +// CDC Request Codes +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00 +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 +#define CDC_SET_COMM_FEATURE 0x02 +#define CDC_GET_COMM_FEATURE 0x03 +#define CDC_CLEAR_COMM_FEATURE 0x04 +// RESERVED (future use) 0x05-0x0F +#define CDC_SET_AUX_LINE_STATE 0x10 +#define CDC_SET_HOOK_STATE 0x11 +#define CDC_PULSE_SETUP 0x12 +#define CDC_SEND_PULSE 0x13 +#define CDC_SET_PULSE_TIME 0x14 +#define CDC_RING_AUX_JACK 0x15 +// RESERVED (future use) 0x16-0x1F +#define CDC_SET_LINE_CODING 0x20 +#define CDC_GET_LINE_CODING 0x21 +#define CDC_SET_CONTROL_LINE_STATE 0x22 +#define CDC_SEND_BREAK 0x23 +// RESERVED (future use) 0x24-0x2F +#define CDC_SET_RINGER_PARMS 0x30 +#define CDC_GET_RINGER_PARMS 0x31 +#define CDC_SET_OPERATION_PARMS 0x32 +#define CDC_GET_OPERATION_PARMS 0x33 +#define CDC_SET_LINE_PARMS 0x34 +#define CDC_GET_LINE_PARMS 0x35 +#define CDC_DIAL_DIGITS 0x36 +#define CDC_SET_UNIT_PARAMETER 0x37 +#define CDC_GET_UNIT_PARAMETER 0x38 +#define CDC_CLEAR_UNIT_PARAMETER 0x39 +#define CDC_GET_PROFILE 0x3A +// RESERVED (future use) 0x3B-0x3F +#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 +#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x41 +#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER 0x42 +#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 +#define CDC_GET_ETHERNET_STATISTIC 0x44 +// RESERVED (future use) 0x45-0x4F +#define CDC_SET_ATM_DATA_FORMAT 0x50 +#define CDC_GET_ATM_DEVICE_STATISTICS 0x51 +#define CDC_SET_ATM_DEFAULT_VC 0x52 +#define CDC_GET_ATM_VC_STATISTICS 0x53 +// RESERVED (future use) 0x54-0x5F +// MDLM Semantic-Model specific Requests 0x60–0x7F +// RESERVED (future use) 0x80-0xFF + +// CDC Notification Codes +#define CDC_NETWORK_CONNECTION 0x00 +#define CDC_RESPONSE_AVAILABLE 0x01 +// RESERVED (future use) 0x02-0x07 +#define CDC_AUX_JACK_HOOK_STATE 0x08 +#define CDC_RING_DETECT 0x09 +// RESERVED (future use) 0x0A-0x1F +#define CDC_SERIAL_STATE 0x20 +// RESERVED (future use) 0x21-0x27 +#define CDC_CALL_STATE_CHANGE 0x28 +#define CDC_LINE_STATE_CHANGE 0x29 +#define CDC_CONNECTION_SPEED_CHANGE 0x2A +// RESERVED 0x2B-0x3F +// MDML SEMANTIC-MODEL-SPECIFIC NOTIFICATION 0x40-0x5F +// RESERVED (future use) 0x60-0xFF +#endif diff --git a/Firmware/dp_usb/picusb.h b/Firmware/dp_usb/picusb.h index c360442d..212d4f8e 100644 --- a/Firmware/dp_usb/picusb.h +++ b/Firmware/dp_usb/picusb.h @@ -1,534 +1,534 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - - -// JTR v0.2a 26th Jan 2012 -// JTR v0.2b 10th March 2012 -Comments by JTR. - -This file is a combined header for all the PIC18F and PIC24F -USB PICs for the Honken USB stack. It has been corrected for -the PIC24 parts and slightly rearranged to reduce duplicate -entries common to both PIC families. - - - */ - -#ifndef _picusb -#define _picusb - -// JTR TODO Add support for the PIC24E and dsPIC33E parts when they are released. - -#if defined(__18F2450) || defined(__18F2455) || defined(__18F2458) || defined(__18F2550) || defined(__18F2553)|| defined(__18F4450) || defined(__18F4455) || defined(__18F4458) || defined(__18F4550) || defined(__18F4553) -#define PIC_18F -#define PIC18Fxx5x -#define BD_RAM = 0x400 - -#elif defined(__18F13K50) || defined(__18F14K50) //JTR add -#define PIC_18F -#define PIC18FxxK -#define BD_RAM 0x200 - -#elif defined(__18F24J50) || defined(__18F25J50) || defined(__18F26J50) || defined(__18F44J50) || defined(__18F45J50) || defined(__18F46J50) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define BD_RAM = 0x400 - -#elif defined(__18F26J53) || defined(__18F27J53) || defined(__18F46J53) || defined(__18F47J53) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define BD_RAM = 0xd00 - -#elif defined(__18F65J50) || defined(__18F66J50) || defined(__18F66J55) || defined(__18F67J50) || defined(__18F85J50) || defined(__18F86J50) || defined(__18F66J55) || defined(__18F67J50) -#define PIC_18F -#define IS_18J -#define PLLEN_REQD -#define USE_ALT_ANCON -#define BD_RAM = 0x400 - -#elif defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) -#define PIC_24F - -#elif defined(__PIC24FJ32GB002__) || defined(__PIC24FJ32GB004__) || defined(__PIC24FJ64GB002__) || defined(__PIC24FJ64GB004__) -#define PIC_24F -#define PLLEN_REQD - -#elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(PIC24FJ256GB206__) || defined(PIC24FJ256GB210__) -#define PIC_24F -#define USE_ANS - -#elif defined(__PIC24FJ128DA106__) || defined(__PIC24FJ128DA110__) || defined(__PIC24FJ128DA206__) || defined(__PIC24FJ128DA210__) || defined(__PIC24FJ256DA106__) || defined(__PIC24FJ256DA110__) || defined(_PIC24FJ256DA206__) || defined(__PIC24FJ256DA210__) -#define PIC_24F -#define PLLEN_REQD -#define USE_ANS -#endif - -#if defined(__18F13K50) || defined(__18F14K50) //JTR add -#define MAX_CHIP_EP (8u) -#else -#define MAX_CHIP_EP (16u) -#endif - -/* COMMON PIC DEFINES TO BOTH PIC18 AND PIC24 */ - -// JTR moved from usb_stack.h -#define UOWN 0x80 -#define DTS 0x40 -#define KEN 0x20 -#define INCDIS 0x10 -#define DTSEN 0x08 -#define BSTALL 0x04 -#define BC98 0x03 - - -#define USB_DIR_OUT 0 -#define USB_DIR_IN 1 -#define USB_PP_EVEN 0 -#define USB_PP_ODD 1 - - -/* PIC DEFINES SPECIFIC TO PIC18 */ - -#if defined(PIC_18F) - -#define MyProcessor // JTR check that a PIC is defined -#include - -#define USTAT_ODD_EVEN (2) // JTR may be required for ping-pong BD* calculations and are different from PIC18 to PIC24 -#define USTAT_ODD_EVEN_SHIFT (1) - -// JTR removed not used and the values??? -// #define USB_EP_INTERRUPT (0) -// #define USB_EP_BULK (1) -// #define USB_EP_ISOCHRONOUS (2) - -#define USB_UEP_EPSTALL (0x01) // JTR Note. to self. these values are different between the PIC18 and PIC24 -#define USB_UEP_EPINEN (0x02) -#define USB_UEP_EPOUTEN (0x04) -#define USB_UEP_EPCONDIS (0x08) -#define USB_UEP_EPHSHK (0x10) - -#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) -#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) -#define USB_EP_NONE (0x00) - -typedef unsigned char usb_uep_t; -#define USB_UEP ((usb_uep_t*) (&UEP0)) -#define USB_UEP0 UEP0 -#define USB_UEP1 UEP1 -#define USB_UEP2 UEP2 -#define USB_UEP3 UEP3 -#define USB_UEP4 UEP4 -#define USB_UEP5 UEP5 -#define USB_UEP6 UEP6 -#define USB_UEP7 UEP7 - -#ifndef PIC18FxxK -#define USB_UEP8 UEP8 -#define USB_UEP9 UEP9 -#define USB_UEP10 UEP10 -#define USB_UEP11 UEP11 -#define USB_UEP12 UEP12 -#define USB_UEP13 UEP13 -#define USB_UEP14 UEP14 -#define USB_UEP15 UEP15 -#endif - -/* Interrupt */ -#define USB_SOF 0x40 -#define USB_STALL 0x20 -#define USB_IDLE 0x10 -#define USB_TRN 0x08 -#define USB_RESUM 0x04 -#define USB_UERR 0x02 -#define USB_URST 0x01 - -#define USB_RESET_FLAG UIRbits.URSTIF -#define USB_ERROR_FLAG UIRbits.UERRIF -#define USB_RESUME_FLAG UIRbits.ACTVIF -#define USB_IDLE_FLAG UIRbits.IDLEIF -#define USB_STALL_FLAG UIRbits.STALLIF -#define USB_SOF_FLAG UIRbits.SOFIF -#define USB_TRANSACTION_FLAG UIRbits.TRNIF - -#define UsbInterruptFlags() (UIR) -#define UsbErrorInterruptFlags() (UEIR) -#define ClearGlobalUsbInterruptFlag() PIR2bits.USBIF = 0 -#define TestUsbTrfInterruptFlag() UIR (x) -#define ClearUsbInterruptFlag(x) UIR &= ~(x) -#define ClearAllUsbInterruptFlags() UIR = 0 -#define ClearUsbErrorInterruptFlag(x) UEIR &= ~(x) -#define ClearAllUsbErrorInterruptFlags() UEIR = 0 -#define DisableGlobalUsbInterrupt() PIE2bits.USBIE = 0 -#define DisableUsbInterrupt(x) UIE &= ~(x) -#define DisableAllUsbInterrupts() UIE = 0 -#define DisableUsbErrorInterrupt(x) UEIE &= ~(x) -#define DisableAllUsbErrorInterrupts() UEIE = 0 -#define EnableUsbGlobalInterrupt() PIE2bits.USBIE = 1 -#define EnableUsbPerifInterrupts(x) UIE |= (x) -#define TestGlobalUsbInterruptEnable() (PIE2bits.USBIE) -#define EnableAllUsbInterrupts() UIE = 0xFF -#define EnableUsbErrorInterrupt(x) UEIE |= (x) -#define EnableAllUsbErrorInterrupts() UEIE = 0xFF - -// Depreciated and eliminated March 10 2012 -//#define EnableUsbHighPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new -//#define EnableUsbLowPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new -// JTR TODO define for NO priority interrupt. - -/* UCON */ -#define ResetPPbuffers() do {UCONbits.PPBRST = 1; UCONbits.PPBRST=0;} while(0) -#define SingleEndedZeroIsSet() (UCONbits.SE0) -#define EnablePacketTransfer() UCONbits.PKTDIS = 0 -#define EnableUsb() UCONbits.USBEN = 1 -#define SignalResume() do {UCONbits.RESUME = 1; delay_ms(10); UCONbits.RESUME = 0;} while(0) -#define SuspendUsb() UCONbits.SUSPND = 1 -#define WakeupUsb() do {UCONbits.SUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0) - -/* UADDR */ -#define SetUsbAddress(x) (UADDR = (x)) -#define GetUsbAddress() (UADDR) - -/* USTAT */ -// typedef unsigned char usb_status_t; -#define GetUsbTransaction() (USTAT) -#define USB_STAT2EP(x) ((x>>3)&0x0F) -#define USB_STAT2DIR(x) ((x>>2)&0x01) -#define USB_STAT2ADDR(x) ((x>>2)&0x1F) -#define USB_STAT2PPI(x) ((x>>1)&0x01) -#define DIRBIT 0x4 // JTR addition. Different between PIC18 and PIC24 -/* Hardware implementations */ - -#if defined USB_INTERNAL_PULLUPS -#define USB_UCFG_UPUEN_VALUE (1<<4) -#elif defined USB_EXTERNAL_PULLUPS -#define USB_UCFG_UPUEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_INTERNAL_TRANSCIEVER -#define USB_UCFG_UTRDIS_VALUE (0) -#elif defined USB_EXTERNAL_TRANSCIEVER -#define USB_UCFG_UTRDIS_VALUE (1<<3) -#else -#error "Neither internal nor external transciever defined" -#endif - -#if defined USB_FULL_SPEED_DEVICE -#define USB_UCFG_FSEN_VALUE (1<<2) -#elif defined USB_LOW_SPEED_DEVICE -#define USB_UCFG_FSEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_BUS_POWERED -#ifndef usb_low_power_request -/* Default low power mode is DUD */ -#define usb_low_power_request() Nop() -#endif -#ifndef usb_low_power_resume -#define usb_low_power_resume() Nop() -#endif -#elif defined USB_SELF_POWERED -#define usb_low_power_request() Nop() -#define usb_low_power_resume() Nop() -#else -#error "No source of device power defined" -#endif - -#ifndef USB_INTERNAL_VREG -#warning "Use of internal voltage regulator not defined. User must supply 3.3V on Vusb pin." -#endif - -//#define USB_DIR_OUT 0 -//#define USB_DIR_IN 1 -//#define USB_PP_EVEN 0 -//#define USB_PP_ODD 1 - -/* PingPong buffer descriptor table index calculations */ -#if USB_PP_BUF_MODE == NO_PINGPONG -#define USB_USTAT2BD(X) ( (X)/4 ) -#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) -#elif USB_PP_BUF_MODE == 1 -#error "USB_PP_BUF_MODE outside scope." -#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) -#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) -#elif USB_PP_BUF_MODE == 2 -#error "USB_PP_BUF_MODE outside scope." -#define USB_USTAT2BD(X) ( (X)/2 ) -#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) -#elif USB_PP_BUF_MODE == 3 -#error "USB_PP_BUF_MODE outside scope." - -#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) -#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) - - -#else -#error "USB_PP_BUF_MODE outside scope." -#endif - -#define USB_UCFG_REGISTER_VALUE ((USB_UCFG_UPUEN_VALUE) | \ - (USB_UCFG_UTRDIS_VALUE) | \ - (USB_UCFG_FSEN_VALUE) | \ - (USB_PP_BUF_MODE)) - -#define ConfigureUsbHardware() do {UCFG = USB_UCFG_REGISTER_VALUE;} while(0) - -#define ROM far rom -#define ROMPTR far rom -#define ARCH_memcpy memcpypgm2ram - -typedef struct BDENTRY { - unsigned char - /* struct { - unsigned BCH:2; - unsigned BSTALL:1; - unsigned DTSEN:1; - unsigned INCDIS:1; - unsigned KEN:1; - unsigned DTS:1; - unsigned UOWN:1; - }*/ - BDSTAT; - unsigned char BDCNT; - unsigned char *BDADDR; -} BDentry; -#endif // End of specific PIC18 defines. - - -/* PIC DEFINES SPECIFIC TO PIC24 FOLLOW */ - -#if defined(PIC_24F) - -#define MyProcessor -#include - -#define USTAT_ODD_EVEN (4) // JTR PIC24 fixup potentially ?? Only required when ping-pong buffering is enabled. -#define USTAT_ODD_EVEN_SHIFT (2) // JTR these are required for BD* calculations and are different for the PIC24 - -/* Bitmasks */ -#define USB_UEP_EPHSHK (0x01) -#define USB_UEP_EPSTALL (0x02) -#define USB_UEP_EPINEN (0x04) -#define USB_UEP_EPOUTEN (0x08) -#define USB_UEP_EPCONDIS (0x10) - -#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) -#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) -#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) -#define USB_EP_NONE (0x00) - -// JTR removed not used and the values?? -// #define USB_EP_INTERRUPT (0) -// #define USB_EP_BULK (1) -// #define USB_EP_ISOCHRONOUS (2) - -typedef unsigned int usb_uep_t; // JTR PIC24 fixup potentially ?? changed from char. -// See http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=150#p17703 - -// However the stack as modified by JTR does not actually use it. Then again it might as it is tested and works. - -#define USB_UEP ((usb_uep_t*)(&U1EP0)) -#define USB_UEP0 U1EP0 -#define USB_UEP1 U1EP1 -#define USB_UEP2 U1EP2 -#define USB_UEP3 U1EP3 -#define USB_UEP4 U1EP4 -#define USB_UEP5 U1EP5 -#define USB_UEP6 U1EP6 -#define USB_UEP7 U1EP7 -#define USB_UEP8 U1EP8 -#define USB_UEP9 U1EP9 -#define USB_UEP10 U1EP10 -#define USB_UEP11 U1EP11 -#define USB_UEP12 U1EP12 -#define USB_UEP13 U1EP13 -#define USB_UEP14 U1EP14 -#define USB_UEP15 U1EP15 - -/* Interrupt */ -#define USB_STALL 0x0080 -#define USB_RESUM 0x0020 -#define USB_IDLE 0x0010 -#define USB_TRN 0x0008 -#define USB_SOF 0x0004 -#define USB_UERR 0x0002 -#define USB_URST 0x0001 - -#define USB_RESET_FLAG U1IRbits.URSTIF -#define USB_ERROR_FLAG U1IRbits.UERRIF -#define USB_RESUME_FLAG U1IRbits.RESUMEIF -#define USB_IDLE_FLAG U1IRbits.IDLEIF -#define USB_STALL_FLAG U1IRbits.STALLIF -#define USB_SOF_FLAG U1IRbits.SOFIF -#define USB_TRANSACTION_FLAG U1IRbits.TRNIF - -#define UsbInterruptFlags() (U1IR) -#define UsbErrorInterruptFlags() (U1EIR) - -#define ClearUsbInterruptFlag(x) U1IR = x -#define ClearAllUsbInterruptFlags() U1IR = 0xFF -#define ClearUsbErrorInterruptFlag(x) U1EIR = x -#define ClearAllUsbErrorInterruptFlags() U1EIR = 0xFF -#define DisableGlobalUsbInterrupt() IEC5bits.USB1IE=0 //PIE2bits.USBIE = 0 /*FIX*/ -#define DisableUsbInterrupt(x) U1IE &= ~(x) -#define DisableAllUsbInterrupts() U1IE = 0 -#define DisableUsbErrorInterrupt(x) U1EIE &= ~(x) -#define DisableAllUsbErrorInterrupts() U1EIE = 0 -#define EnableUsbGlobalInterrupt() IEC5bits.USB1IE=1 //PIE2bits.USBIE = 1 /*FIX*/ -#define TestGlobalUsbInterruptEnable() (IEC5bits.USB1IE) -#define EnableUsbPerifInterrupts(x) U1IE |= (x) -#define EnableAllUsbInterrupts() U1IE = 0x00FF -#define EnableUsbErrorInterrupt(x) U1EIE |= (x) -#define EnableAllUsbErrorInterrupts() U1EIE = 0x00FF -#define EnableUsbHighPriInterrupt() -#define ClearGlobalUsbInterruptFlag() IFS5bits.USB1IF = 0 - -/* UCON */ -#define ResetPPbuffers() do {U1CONbits.PPBRST = 1; U1CONbits.PPBRST=0;} while(0) -#define SingleEndedZeroIsSet() (U1CONbits.SE0) -#define EnablePacketTransfer() U1CONbits.PKTDIS = 0 -#define EnableUsb() while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;} // U1CONbits.USBEN = 1 -#define SignalResume() do {U1CONbits.RESUME = 1; delay_ms(10); U1CONbits.RESUME = 0;} while(0) -#define SuspendUsb() U1PWRCbits.USUSPND = 1 -#define WakeupUsb() do {U1PWRCbits.USUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0) - - -/* UADDR */ -#define SetUsbAddress(x) (U1ADDR = (x)) -#define GetUsbAddress() (U1ADDR) - -/* USTAT */ - -// JTR moved to usb_stack.h -//typedef unsigned char usb_status_t; - -#define GetUsbTransaction() (U1STAT) -#define USB_STAT2EP(x) ((x>>4)&0x0F) //((x>>3)&0x0F) JTR PIC24 fixups -#define USB_STAT2DIR(x) ((x>>3)&0x01) //((x>>2)&0x01) "" -#define USB_STAT2ADDR(x) ((x>>3)&0x1F) //((x>>2)&0x1F) "" -#define USB_STAT2PPI(x) ((x>>2 &0x01) //((x>>1)&0x01) "" -#define DIRBIT 0x8 // JTR add PIC24 fixup as the mask is 0x4 for the PIC18 - - -/* Hardware implementations */ - -#if defined USB_INTERNAL_PULLUPS -#elif defined USB_EXTERNAL_PULLUPS -#define USB_U1OTGCON_UPUEN_VALUE (0) -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_INTERNAL_TRANSCIEVER -#define USB_U1CNFG2_UTRDIS_VALUE (0) -#elif defined USB_EXTERNAL_TRANSCIEVER -#define USB_U1CNFG2_UTRDIS_VALUE (1) -#else -#error "Neither internal nor external transciever defined" -#endif - -#if defined USB_FULL_SPEED_DEVICE -#if defined USB_INTERNAL_PULLUPS -#define USB_U1OTGCON_DPPULUP_VALUE (1<<7) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#else -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#endif -#elif defined USB_LOW_SPEED_DEVICE -#if defined USB_INTERNAL_PULLUPS -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (1<<6) -#else -#define USB_U1OTGCON_DPPULUP_VALUE (0) -#define USB_U1OTGCON_DMPULUP_VALUE (0) -#endif -#else -#error "Neither internal nor external pullups defined" -#endif - -#if defined USB_BUS_POWERED -#ifndef usb_low_power_request -/* Default low power mode is DUD */ -#define usb_low_power_request() Nop() -#endif -#ifndef usb_low_power_resume -#define usb_low_power_resume() Nop() -#endif -#elif defined USB_SELF_POWERED -#define usb_low_power_request() Nop() -#define usb_low_power_resume() Nop() -#else -#error "No source of device power defined" -#endif - -#ifndef USB_INTERNAL_VREG -#warning "Use of internal voltage regulator not implemented. User must supply 3.3V on Vusb pin." -#endif - -/* PingPong buffer descriptor table index calculations */ -#if USB_PP_BUF_MODE == NO_PINGPONG -#define USB_USTAT2BD(X) ( (X)/8 ) //JTR PIC24 fixups -#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) - -// JTR TODO these values may need to be changed for the PIC24 -//#elif USB_PP_BUF_MODE == 1 -//#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) -//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) -//#elif USB_PP_BUF_MODE == 2 -//#define USB_USTAT2BD(X) ( (X)/2 ) -//#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) -//#elif USB_PP_BUF_MODE == 3 -//#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) -//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) - -#else -#error "USB_PP_BUF_MODE outside scope." -#endif - -// JTR PIC24 fixups some may not required but are "more correct." The added /256 is required! -#define ConfigureUsbHardware() do { \ - U1CNFG1 = USB_PP_BUF_MODE; \ - U1CNFG2 = USB_U1CNFG2_UTRDIS_VALUE; \ - U1BDTP1 = (unsigned int) usb_bdt/256; \ - U1OTGCON = USB_U1OTGCON_DPPULUP_VALUE | \ - USB_U1OTGCON_DMPULUP_VALUE; \ - U1PWRCbits.USBPWR = 1; \ - } while(0) - -#define ROM __attribute__((space(auto_psv))) -#define ROMPTR -#define ARCH_memcpy memcpy - -typedef struct BDENTRY { - unsigned char BDCNT; // JTR PIC24 fixup Note that the endianness is swapped from the PIC18 - // This is probably what had people hitting their heads! - unsigned char BDSTAT; - unsigned char *BDADDR; -} BDentry; - -#endif - -#ifndef MyProcessor -#error "No PIC family defined" -#endif - -#endif +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + + +// JTR v0.2a 26th Jan 2012 +// JTR v0.2b 10th March 2012 +Comments by JTR. + +This file is a combined header for all the PIC18F and PIC24F +USB PICs for the Honken USB stack. It has been corrected for +the PIC24 parts and slightly rearranged to reduce duplicate +entries common to both PIC families. + + + */ + +#ifndef _picusb +#define _picusb + +// JTR TODO Add support for the PIC24E and dsPIC33E parts when they are released. + +#if defined(__18F2450) || defined(__18F2455) || defined(__18F2458) || defined(__18F2550) || defined(__18F2553)|| defined(__18F4450) || defined(__18F4455) || defined(__18F4458) || defined(__18F4550) || defined(__18F4553) +#define PIC_18F +#define PIC18Fxx5x +#define BD_RAM = 0x400 + +#elif defined(__18F13K50) || defined(__18F14K50) //JTR add +#define PIC_18F +#define PIC18FxxK +#define BD_RAM 0x200 + +#elif defined(__18F24J50) || defined(__18F25J50) || defined(__18F26J50) || defined(__18F44J50) || defined(__18F45J50) || defined(__18F46J50) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define BD_RAM = 0x400 + +#elif defined(__18F26J53) || defined(__18F27J53) || defined(__18F46J53) || defined(__18F47J53) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define BD_RAM = 0xd00 + +#elif defined(__18F65J50) || defined(__18F66J50) || defined(__18F66J55) || defined(__18F67J50) || defined(__18F85J50) || defined(__18F86J50) || defined(__18F66J55) || defined(__18F67J50) +#define PIC_18F +#define IS_18J +#define PLLEN_REQD +#define USE_ALT_ANCON +#define BD_RAM = 0x400 + +#elif defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__) +#define PIC_24F + +#elif defined(__PIC24FJ32GB002__) || defined(__PIC24FJ32GB004__) || defined(__PIC24FJ64GB002__) || defined(__PIC24FJ64GB004__) +#define PIC_24F +#define PLLEN_REQD + +#elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(PIC24FJ256GB206__) || defined(PIC24FJ256GB210__) +#define PIC_24F +#define USE_ANS + +#elif defined(__PIC24FJ128DA106__) || defined(__PIC24FJ128DA110__) || defined(__PIC24FJ128DA206__) || defined(__PIC24FJ128DA210__) || defined(__PIC24FJ256DA106__) || defined(__PIC24FJ256DA110__) || defined(_PIC24FJ256DA206__) || defined(__PIC24FJ256DA210__) +#define PIC_24F +#define PLLEN_REQD +#define USE_ANS +#endif + +#if defined(__18F13K50) || defined(__18F14K50) //JTR add +#define MAX_CHIP_EP (8u) +#else +#define MAX_CHIP_EP (16u) +#endif + +/* COMMON PIC DEFINES TO BOTH PIC18 AND PIC24 */ + +// JTR moved from usb_stack.h +#define UOWN 0x80 +#define DTS 0x40 +#define KEN 0x20 +#define INCDIS 0x10 +#define DTSEN 0x08 +#define BSTALL 0x04 +#define BC98 0x03 + + +#define USB_DIR_OUT 0 +#define USB_DIR_IN 1 +#define USB_PP_EVEN 0 +#define USB_PP_ODD 1 + + +/* PIC DEFINES SPECIFIC TO PIC18 */ + +#if defined(PIC_18F) + +#define MyProcessor // JTR check that a PIC is defined +#include + +#define USTAT_ODD_EVEN (2) // JTR may be required for ping-pong BD* calculations and are different from PIC18 to PIC24 +#define USTAT_ODD_EVEN_SHIFT (1) + +// JTR removed not used and the values??? +// #define USB_EP_INTERRUPT (0) +// #define USB_EP_BULK (1) +// #define USB_EP_ISOCHRONOUS (2) + +#define USB_UEP_EPSTALL (0x01) // JTR Note. to self. these values are different between the PIC18 and PIC24 +#define USB_UEP_EPINEN (0x02) +#define USB_UEP_EPOUTEN (0x04) +#define USB_UEP_EPCONDIS (0x08) +#define USB_UEP_EPHSHK (0x10) + +#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) +#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) +#define USB_EP_NONE (0x00) + +typedef unsigned char usb_uep_t; +#define USB_UEP ((usb_uep_t*) (&UEP0)) +#define USB_UEP0 UEP0 +#define USB_UEP1 UEP1 +#define USB_UEP2 UEP2 +#define USB_UEP3 UEP3 +#define USB_UEP4 UEP4 +#define USB_UEP5 UEP5 +#define USB_UEP6 UEP6 +#define USB_UEP7 UEP7 + +#ifndef PIC18FxxK +#define USB_UEP8 UEP8 +#define USB_UEP9 UEP9 +#define USB_UEP10 UEP10 +#define USB_UEP11 UEP11 +#define USB_UEP12 UEP12 +#define USB_UEP13 UEP13 +#define USB_UEP14 UEP14 +#define USB_UEP15 UEP15 +#endif + +/* Interrupt */ +#define USB_SOF 0x40 +#define USB_STALL 0x20 +#define USB_IDLE 0x10 +#define USB_TRN 0x08 +#define USB_RESUM 0x04 +#define USB_UERR 0x02 +#define USB_URST 0x01 + +#define USB_RESET_FLAG UIRbits.URSTIF +#define USB_ERROR_FLAG UIRbits.UERRIF +#define USB_RESUME_FLAG UIRbits.ACTVIF +#define USB_IDLE_FLAG UIRbits.IDLEIF +#define USB_STALL_FLAG UIRbits.STALLIF +#define USB_SOF_FLAG UIRbits.SOFIF +#define USB_TRANSACTION_FLAG UIRbits.TRNIF + +#define UsbInterruptFlags() (UIR) +#define UsbErrorInterruptFlags() (UEIR) +#define ClearGlobalUsbInterruptFlag() PIR2bits.USBIF = 0 +#define TestUsbTrfInterruptFlag() UIR (x) +#define ClearUsbInterruptFlag(x) UIR &= ~(x) +#define ClearAllUsbInterruptFlags() UIR = 0 +#define ClearUsbErrorInterruptFlag(x) UEIR &= ~(x) +#define ClearAllUsbErrorInterruptFlags() UEIR = 0 +#define DisableGlobalUsbInterrupt() PIE2bits.USBIE = 0 +#define DisableUsbInterrupt(x) UIE &= ~(x) +#define DisableAllUsbInterrupts() UIE = 0 +#define DisableUsbErrorInterrupt(x) UEIE &= ~(x) +#define DisableAllUsbErrorInterrupts() UEIE = 0 +#define EnableUsbGlobalInterrupt() PIE2bits.USBIE = 1 +#define EnableUsbPerifInterrupts(x) UIE |= (x) +#define TestGlobalUsbInterruptEnable() (PIE2bits.USBIE) +#define EnableAllUsbInterrupts() UIE = 0xFF +#define EnableUsbErrorInterrupt(x) UEIE |= (x) +#define EnableAllUsbErrorInterrupts() UEIE = 0xFF + +// Depreciated and eliminated March 10 2012 +//#define EnableUsbHighPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new +//#define EnableUsbLowPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new +// JTR TODO define for NO priority interrupt. + +/* UCON */ +#define ResetPPbuffers() do {UCONbits.PPBRST = 1; UCONbits.PPBRST=0;} while(0) +#define SingleEndedZeroIsSet() (UCONbits.SE0) +#define EnablePacketTransfer() UCONbits.PKTDIS = 0 +#define EnableUsb() UCONbits.USBEN = 1 +#define SignalResume() do {UCONbits.RESUME = 1; delay_ms(10); UCONbits.RESUME = 0;} while(0) +#define SuspendUsb() UCONbits.SUSPND = 1 +#define WakeupUsb() do {UCONbits.SUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0) + +/* UADDR */ +#define SetUsbAddress(x) (UADDR = (x)) +#define GetUsbAddress() (UADDR) + +/* USTAT */ +// typedef unsigned char usb_status_t; +#define GetUsbTransaction() (USTAT) +#define USB_STAT2EP(x) ((x>>3)&0x0F) +#define USB_STAT2DIR(x) ((x>>2)&0x01) +#define USB_STAT2ADDR(x) ((x>>2)&0x1F) +#define USB_STAT2PPI(x) ((x>>1)&0x01) +#define DIRBIT 0x4 // JTR addition. Different between PIC18 and PIC24 +/* Hardware implementations */ + +#if defined USB_INTERNAL_PULLUPS +#define USB_UCFG_UPUEN_VALUE (1<<4) +#elif defined USB_EXTERNAL_PULLUPS +#define USB_UCFG_UPUEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_INTERNAL_TRANSCIEVER +#define USB_UCFG_UTRDIS_VALUE (0) +#elif defined USB_EXTERNAL_TRANSCIEVER +#define USB_UCFG_UTRDIS_VALUE (1<<3) +#else +#error "Neither internal nor external transciever defined" +#endif + +#if defined USB_FULL_SPEED_DEVICE +#define USB_UCFG_FSEN_VALUE (1<<2) +#elif defined USB_LOW_SPEED_DEVICE +#define USB_UCFG_FSEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_BUS_POWERED +#ifndef usb_low_power_request +/* Default low power mode is DUD */ +#define usb_low_power_request() Nop() +#endif +#ifndef usb_low_power_resume +#define usb_low_power_resume() Nop() +#endif +#elif defined USB_SELF_POWERED +#define usb_low_power_request() Nop() +#define usb_low_power_resume() Nop() +#else +#error "No source of device power defined" +#endif + +#ifndef USB_INTERNAL_VREG +#warning "Use of internal voltage regulator not defined. User must supply 3.3V on Vusb pin." +#endif + +//#define USB_DIR_OUT 0 +//#define USB_DIR_IN 1 +//#define USB_PP_EVEN 0 +//#define USB_PP_ODD 1 + +/* PingPong buffer descriptor table index calculations */ +#if USB_PP_BUF_MODE == NO_PINGPONG +#define USB_USTAT2BD(X) ( (X)/4 ) +#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) +#elif USB_PP_BUF_MODE == 1 +#error "USB_PP_BUF_MODE outside scope." +#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) +#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) +#elif USB_PP_BUF_MODE == 2 +#error "USB_PP_BUF_MODE outside scope." +#define USB_USTAT2BD(X) ( (X)/2 ) +#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) +#elif USB_PP_BUF_MODE == 3 +#error "USB_PP_BUF_MODE outside scope." + +#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) +#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) + + +#else +#error "USB_PP_BUF_MODE outside scope." +#endif + +#define USB_UCFG_REGISTER_VALUE ((USB_UCFG_UPUEN_VALUE) | \ + (USB_UCFG_UTRDIS_VALUE) | \ + (USB_UCFG_FSEN_VALUE) | \ + (USB_PP_BUF_MODE)) + +#define ConfigureUsbHardware() do {UCFG = USB_UCFG_REGISTER_VALUE;} while(0) + +#define ROM far rom +#define ROMPTR far rom +#define ARCH_memcpy memcpypgm2ram + +typedef struct BDENTRY { + unsigned char + /* struct { + unsigned BCH:2; + unsigned BSTALL:1; + unsigned DTSEN:1; + unsigned INCDIS:1; + unsigned KEN:1; + unsigned DTS:1; + unsigned UOWN:1; + }*/ + BDSTAT; + unsigned char BDCNT; + unsigned char *BDADDR; +} BDentry; +#endif // End of specific PIC18 defines. + + +/* PIC DEFINES SPECIFIC TO PIC24 FOLLOW */ + +#if defined(PIC_24F) + +#define MyProcessor +#include + +#define USTAT_ODD_EVEN (4) // JTR PIC24 fixup potentially ?? Only required when ping-pong buffering is enabled. +#define USTAT_ODD_EVEN_SHIFT (2) // JTR these are required for BD* calculations and are different for the PIC24 + +/* Bitmasks */ +#define USB_UEP_EPHSHK (0x01) +#define USB_UEP_EPSTALL (0x02) +#define USB_UEP_EPINEN (0x04) +#define USB_UEP_EPOUTEN (0x08) +#define USB_UEP_EPCONDIS (0x10) + +#define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN) +#define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS) +#define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS) +#define USB_EP_NONE (0x00) + +// JTR removed not used and the values?? +// #define USB_EP_INTERRUPT (0) +// #define USB_EP_BULK (1) +// #define USB_EP_ISOCHRONOUS (2) + +typedef unsigned int usb_uep_t; // JTR PIC24 fixup potentially ?? changed from char. +// See http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=150#p17703 + +// However the stack as modified by JTR does not actually use it. Then again it might as it is tested and works. + +#define USB_UEP ((usb_uep_t*)(&U1EP0)) +#define USB_UEP0 U1EP0 +#define USB_UEP1 U1EP1 +#define USB_UEP2 U1EP2 +#define USB_UEP3 U1EP3 +#define USB_UEP4 U1EP4 +#define USB_UEP5 U1EP5 +#define USB_UEP6 U1EP6 +#define USB_UEP7 U1EP7 +#define USB_UEP8 U1EP8 +#define USB_UEP9 U1EP9 +#define USB_UEP10 U1EP10 +#define USB_UEP11 U1EP11 +#define USB_UEP12 U1EP12 +#define USB_UEP13 U1EP13 +#define USB_UEP14 U1EP14 +#define USB_UEP15 U1EP15 + +/* Interrupt */ +#define USB_STALL 0x0080 +#define USB_RESUM 0x0020 +#define USB_IDLE 0x0010 +#define USB_TRN 0x0008 +#define USB_SOF 0x0004 +#define USB_UERR 0x0002 +#define USB_URST 0x0001 + +#define USB_RESET_FLAG U1IRbits.URSTIF +#define USB_ERROR_FLAG U1IRbits.UERRIF +#define USB_RESUME_FLAG U1IRbits.RESUMEIF +#define USB_IDLE_FLAG U1IRbits.IDLEIF +#define USB_STALL_FLAG U1IRbits.STALLIF +#define USB_SOF_FLAG U1IRbits.SOFIF +#define USB_TRANSACTION_FLAG U1IRbits.TRNIF + +#define UsbInterruptFlags() (U1IR) +#define UsbErrorInterruptFlags() (U1EIR) + +#define ClearUsbInterruptFlag(x) U1IR = x +#define ClearAllUsbInterruptFlags() U1IR = 0xFF +#define ClearUsbErrorInterruptFlag(x) U1EIR = x +#define ClearAllUsbErrorInterruptFlags() U1EIR = 0xFF +#define DisableGlobalUsbInterrupt() IEC5bits.USB1IE=0 //PIE2bits.USBIE = 0 /*FIX*/ +#define DisableUsbInterrupt(x) U1IE &= ~(x) +#define DisableAllUsbInterrupts() U1IE = 0 +#define DisableUsbErrorInterrupt(x) U1EIE &= ~(x) +#define DisableAllUsbErrorInterrupts() U1EIE = 0 +#define EnableUsbGlobalInterrupt() IEC5bits.USB1IE=1 //PIE2bits.USBIE = 1 /*FIX*/ +#define TestGlobalUsbInterruptEnable() (IEC5bits.USB1IE) +#define EnableUsbPerifInterrupts(x) U1IE |= (x) +#define EnableAllUsbInterrupts() U1IE = 0x00FF +#define EnableUsbErrorInterrupt(x) U1EIE |= (x) +#define EnableAllUsbErrorInterrupts() U1EIE = 0x00FF +#define EnableUsbHighPriInterrupt() +#define ClearGlobalUsbInterruptFlag() IFS5bits.USB1IF = 0 + +/* UCON */ +#define ResetPPbuffers() do {U1CONbits.PPBRST = 1; U1CONbits.PPBRST=0;} while(0) +#define SingleEndedZeroIsSet() (U1CONbits.SE0) +#define EnablePacketTransfer() U1CONbits.PKTDIS = 0 +#define EnableUsb() while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;} // U1CONbits.USBEN = 1 +#define SignalResume() do {U1CONbits.RESUME = 1; delay_ms(10); U1CONbits.RESUME = 0;} while(0) +#define SuspendUsb() U1PWRCbits.USUSPND = 1 +#define WakeupUsb() do {U1PWRCbits.USUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0) + + +/* UADDR */ +#define SetUsbAddress(x) (U1ADDR = (x)) +#define GetUsbAddress() (U1ADDR) + +/* USTAT */ + +// JTR moved to usb_stack.h +//typedef unsigned char usb_status_t; + +#define GetUsbTransaction() (U1STAT) +#define USB_STAT2EP(x) ((x>>4)&0x0F) //((x>>3)&0x0F) JTR PIC24 fixups +#define USB_STAT2DIR(x) ((x>>3)&0x01) //((x>>2)&0x01) "" +#define USB_STAT2ADDR(x) ((x>>3)&0x1F) //((x>>2)&0x1F) "" +#define USB_STAT2PPI(x) ((x>>2 &0x01) //((x>>1)&0x01) "" +#define DIRBIT 0x8 // JTR add PIC24 fixup as the mask is 0x4 for the PIC18 + + +/* Hardware implementations */ + +#if defined USB_INTERNAL_PULLUPS +#elif defined USB_EXTERNAL_PULLUPS +#define USB_U1OTGCON_UPUEN_VALUE (0) +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_INTERNAL_TRANSCIEVER +#define USB_U1CNFG2_UTRDIS_VALUE (0) +#elif defined USB_EXTERNAL_TRANSCIEVER +#define USB_U1CNFG2_UTRDIS_VALUE (1) +#else +#error "Neither internal nor external transciever defined" +#endif + +#if defined USB_FULL_SPEED_DEVICE +#if defined USB_INTERNAL_PULLUPS +#define USB_U1OTGCON_DPPULUP_VALUE (1<<7) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#else +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#endif +#elif defined USB_LOW_SPEED_DEVICE +#if defined USB_INTERNAL_PULLUPS +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (1<<6) +#else +#define USB_U1OTGCON_DPPULUP_VALUE (0) +#define USB_U1OTGCON_DMPULUP_VALUE (0) +#endif +#else +#error "Neither internal nor external pullups defined" +#endif + +#if defined USB_BUS_POWERED +#ifndef usb_low_power_request +/* Default low power mode is DUD */ +#define usb_low_power_request() Nop() +#endif +#ifndef usb_low_power_resume +#define usb_low_power_resume() Nop() +#endif +#elif defined USB_SELF_POWERED +#define usb_low_power_request() Nop() +#define usb_low_power_resume() Nop() +#else +#error "No source of device power defined" +#endif + +#ifndef USB_INTERNAL_VREG +#warning "Use of internal voltage regulator not implemented. User must supply 3.3V on Vusb pin." +#endif + +/* PingPong buffer descriptor table index calculations */ +#if USB_PP_BUF_MODE == NO_PINGPONG +#define USB_USTAT2BD(X) ( (X)/8 ) //JTR PIC24 fixups +#define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) ) + +// JTR TODO these values may need to be changed for the PIC24 +//#elif USB_PP_BUF_MODE == 1 +//#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 ) +//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) ) +//#elif USB_PP_BUF_MODE == 2 +//#define USB_USTAT2BD(X) ( (X)/2 ) +//#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) ) +//#elif USB_PP_BUF_MODE == 3 +//#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 ) +//#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 ) + +#else +#error "USB_PP_BUF_MODE outside scope." +#endif + +// JTR PIC24 fixups some may not required but are "more correct." The added /256 is required! +#define ConfigureUsbHardware() do { \ + U1CNFG1 = USB_PP_BUF_MODE; \ + U1CNFG2 = USB_U1CNFG2_UTRDIS_VALUE; \ + U1BDTP1 = (unsigned int) usb_bdt/256; \ + U1OTGCON = USB_U1OTGCON_DPPULUP_VALUE | \ + USB_U1OTGCON_DMPULUP_VALUE; \ + U1PWRCbits.USBPWR = 1; \ + } while(0) + +#define ROM __attribute__((space(auto_psv))) +#define ROMPTR +#define ARCH_memcpy memcpy + +typedef struct BDENTRY { + unsigned char BDCNT; // JTR PIC24 fixup Note that the endianness is swapped from the PIC18 + // This is probably what had people hitting their heads! + unsigned char BDSTAT; + unsigned char *BDADDR; +} BDentry; + +#endif + +#ifndef MyProcessor +#error "No PIC family defined" +#endif + +#endif diff --git a/Firmware/dp_usb/usb_stack.c b/Firmware/dp_usb/usb_stack.c index e6b475bb..6b56972a 100644 --- a/Firmware/dp_usb/usb_stack.c +++ b/Firmware/dp_usb/usb_stack.c @@ -1,598 +1,598 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - -Influence and inspiration taken from http://pe.ece.olin.edu/ece/projects.html - */ - -// JTR v0.2a Jan 26th 2012 - - -#include "../dp_usb/usb_stack_globals.h" // USB stack only defines Not function related. - -#if USB_EP0_BUFFER_SIZE == 8u -#elif USB_EP0_BUFFER_SIZE == 16u -#elif USB_EP0_BUFFER_SIZE == 32u -#elif USB_EP0_BUFFER_SIZE == 64u -#else -#error "USB_EP0_BUFFER_SIZE needs to be 8, 16, 32 or 64 bytes" -#endif - - -ROMPTR const BYTE *usb_device_descriptor; -ROMPTR const BYTE *usb_config_descriptor; -ROMPTR const BYTE *usb_string_descriptor; -int usb_num_string_descriptors; - -usb_handler_t sof_handler; -usb_handler_t class_setup_handler, vendor_setup_handler; - - -usb_ep_t endpoints[MAX_CHIP_EP]; // JTR change. MAX_CHIP_EP is the number of hardware endpoints on the silicon. See picusb.h - -/* Allocate buffers for buffer description table and the actual buffers */ -// CvD: linkscript puts it in the right memory location - - -// JTR comments. This below goes part way to ridding us of the need for a linker script (PIC18). -// It floats the EP0 buffers to occupy space immediately behind the buffer descriptors. -// However there are problems. It only works for the EP0 buffers because they are the only -// buffers defined in this block. It is not possible to do arithmatic within a #pragma -// therefore there seems to be no way to cause EPs in other blocks to float. -// All the buffers would need to be defined here and this would break the universal nature -// of this module. As such efforts to create a custom linker script free stack have been -// put on hold for now but can be revisited at any stage. -// Please see http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=120#p17401 - -#if defined(PIC_18F) -#pragma udata usb_bdt -BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED]; // JTR changed index from 32 to variable -#pragma udata usb_data -//* Only claim buffer for ep 0 */ -#if USB_PP_BUF_MODE == 0 -BYTE usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; -BYTE usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; -#else -#error "Ping pong buffer not implemented yet!" -#endif - -#elif defined(PIC_24F) -#pragma udata usb_bdt -BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED] __attribute__((aligned(512))); // JTR changed index from 32 to variable TODO: Dynamic allocation reflecting number of used endpoints. (How to do counting in preprocessor?) -#if USB_PP_BUF_MODE == 0 -BYTE usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; -BYTE usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; -#else -#error "Ping pong buffer not implemented yet!" -#endif -#endif - -#pragma udata -unsigned int usb_device_status; -unsigned int usb_current_cfg; -volatile BYTE usb_device_state; -BYTE usb_addr_pending; -BYTE trn_status; // Global since it is needed everywere -BDentry *EP0_Outbdp, *EP0_Inbdp; // Dito -BYTE IsSuspended = 0; -ROMPTR const BYTE *usb_rom_ptr; -size_t usb_rom_len; -volatile BYTE usbrequesterrorflag; - -void usb_init(ROMPTR const BYTE *device_descriptor, - ROMPTR const BYTE *config_descriptor, - ROMPTR const BYTE *string_descriptor, - int num_string_descriptors) { - - usb_device_descriptor = device_descriptor; - usb_config_descriptor = config_descriptor; - usb_string_descriptor = string_descriptor; - usb_num_string_descriptors = num_string_descriptors; - sof_handler = NULL; - class_setup_handler = NULL; - vendor_setup_handler = NULL; - usb_unset_in_handler(0); - usb_unset_out_handler(0); - ClearUSBtoDefault(); - ConfigureUsbHardware(); - EnablePacketTransfer(); -} - -void usb_start(void) { - EnableUsb(); // Enable USB-hardware - usb_device_state = ATTACHED_STATE; - while (SingleEndedZeroIsSet()); // Busywait for initial power-up - usb_device_state = DEFAULT_STATE; //JTR2 -} - -void usb_handle_error(void) { - /* No errorhandler for now, just clear offending flag*/ - ClearAllUsbErrorInterruptFlags(); -} - -void usb_handle_reset(void) { - do { - ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. - usb_current_cfg = 0; - usb_device_state = DEFAULT_STATE; // This BLOCK creates the requied 6 cycle delay for TRNF to reassert. - usb_addr_pending = 0x00; - } while (USB_TRANSACTION_FLAG); - - ClearUSBtoDefault(); - EnablePacketTransfer(); -} - -void ClearUSBtoDefault(void) { - int i; - sof_handler = NULL; - class_setup_handler = NULL; - vendor_setup_handler = NULL; - - SetUsbAddress(0); // After reset we don't have an address - ResetPPbuffers(); - ClearAllUsbErrorInterruptFlags(); - - for (i = 0; i < MAX_CHIP_EP; i++) { - endpoints[i].out_handler = NULL; - endpoints[i].in_handler = NULL; - USB_UEP[i] = 0; - } - - for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i++) { - usb_bdt[i].BDSTAT = 0; - } - - USB_UEP0 = USB_EP_CONTROL; // Configure Only ep0 At this point. - //usbrequesterrorflag = 0; - -#ifdef USB_SELF_POWERED - - // JTR TODO this isn't actually 100% correct. "usb_device_status" is a runtime variable - // In the case of a bus powered device it will always be 0x000 but in the case of - // self powered with bus powered option it becames variable to the current powered - // State. This is a minor thing and for now but it may need to be addressed if there is - // any hardware that is dual powered. - - usb_device_status = 0x0001; -#else - usb_device_status = 0x0000; -#endif - usb_device_state = DETACHED_STATE; // JTR added flag byte for enumeration state - usb_current_cfg = 0; // JTR formally usb_configured - usb_addr_pending = 0x00; - -#if USB_PP_BUF_MODE == NO_PINGPONG - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 -#else -#error "Invalid PING_PONG mode" -#endif -} - -void usb_handler(void) { - - if (USB_IDLE_FLAG) { - /* Idle - suspend */ - USBSuspend(); // // Must be defined in user code. - ClearUsbInterruptFlag(USB_IDLE); - } - - if (USB_RESET_FLAG) { - usb_handle_reset(); - ClearUsbInterruptFlag(USB_URST); - } - if (USB_ERROR_FLAG) { - // usb_handle_error(); - ClearAllUsbErrorInterruptFlags(); - ClearUsbInterruptFlag(USB_UERR); - } - if (USB_STALL_FLAG) { - ClearUsbInterruptFlag(USB_STALL); - } - if (USB_SOF_FLAG) { - /* Start-of-frame */ - if (sof_handler) sof_handler(); - ClearUsbInterruptFlag(USB_SOF); - } - - if (USB_TRANSACTION_FLAG) { - if (!USB_STAT2EP(GetUsbTransaction())) - usb_handle_transaction(); // Only handle EP0 transactions. - ClearUsbInterruptFlag(USB_TRN); // JTR Missing! This is why Ian was only getting one interrupt?? - } // Side effect: advance USTAT Fifo -} - -void usb_handle_transaction(void) { - - usbrequesterrorflag = 0; - - trn_status = GetUsbTransaction(); - EP0_Outbdp = &usb_bdt[USB_USTAT2BD(trn_status)]; - EP0_Inbdp = &usb_bdt[USB_USTAT2BD(trn_status | DIRBIT)]; // All replies in IN direction - - switch (EP0_Outbdp->BDSTAT & USB_TOKEN_Mask) { - case USB_TOKEN_SETUP: - usb_handle_setup(); - break; - case USB_TOKEN_OUT: - usb_handle_out(); - break; - case USB_TOKEN_IN: - usb_handle_in(); - break; - //default: - /* Default case of unknown TOKEN - discard */ - } -} - -void usb_handle_setup(void) { - - EP0_Inbdp->BDSTAT = DTSEN; // Reclaim reply buffer - - EnablePacketTransfer(); // JTR this is placed here to overcome a errate issue with early PIC18 USB pics. - - switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) { - case USB_bmRequestType_Standard: - switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) { - case USB_bmRequestType_Device: - usb_handle_StandardDeviceRequest(EP0_Outbdp); - break; - case USB_bmRequestType_Interface: - usb_handle_StandardInterfaceRequest(EP0_Outbdp); - break; - case USB_bmRequestType_Endpoint: - usb_handle_StandardEndpointRequest(EP0_Outbdp); - break; - default: - usb_RequestError(); - } - break; - case USB_bmRequestType_Class: - if (class_setup_handler) class_setup_handler(); - break; - case USB_bmRequestType_Vendor: - //ROBOTS FIX: http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=3849&view=unread#unread - // did call class_setup_handler(); - if (vendor_setup_handler) vendor_setup_handler(); - break; - default: - usb_RequestError(); - } - /* Prepare endpoint for new reception */ - - EP0_Outbdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size of EP0, should always be ep0? - - // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)? - // note that this is not an entirely robust way of doing things. See the microchip stack for - // further comments and a better system as this does not account for errors and retries - // and it results in the SIE not accepting the final out ZLP status packet for IN transfers - // with a data stage. However it works but it is not anything to be proud of... - - EP0_Outbdp->BDSTAT = (!(EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && - (EP0_Outbdp->BDADDR[USB_wLength] || EP0_Outbdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN; -} - -void usb_handle_StandardDeviceRequest(BDentry *bdp) { - BYTE *packet = bdp->BDADDR; - int i; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - EP0_Inbdp->BDADDR[0] = usb_device_status & 0xFF; - EP0_Inbdp->BDADDR[1] = usb_device_status >> 8; - usb_ack_dat1(2); - break; - case USB_REQUEST_CLEAR_FEATURE: - if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) - usb_device_status &= ~0x0002; - usb_ack_dat1(0); - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_FEATURE: - if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) - usb_device_status |= 0x0002; - usb_ack_dat1(0); - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_ADDRESS: - if (0x00u == packet[USB_wValueHigh] && 0x7Fu >= packet[USB_wValue]) { - usb_addr_pending = packet[USB_wValue]; - usb_set_in_handler(0, usb_set_address); - usb_ack_dat1(0); - } else - usb_RequestError(); - break; - - - case USB_REQUEST_GET_DESCRIPTOR: - switch (packet[USB_bDescriptorType]) { - case USB_DEVICE_DESCRIPTOR_TYPE: // There is only every one in pratice. - usb_rom_ptr = usb_device_descriptor; - usb_rom_len = usb_device_descriptor[0]; // Get BYTE length from descriptor always at byte [0] - if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_rom_ptr[0])) - usb_rom_len = packet[USB_wLength]; // If the HOST asked for LESS then must adjust count to the smaller number - break; - - case USB_CONFIGURATION_DESCRIPTOR_TYPE: - if (packet[USB_bDescriptorIndex] >= usb_device_descriptor[17]) { - flag_usb_RequestError(); - break; - } - - usb_rom_ptr = usb_config_descriptor; - usb_rom_len = usb_rom_ptr[2] + usb_rom_ptr[3] * 256; // Get WORD length from descriptor always at bytes 2&3 (Low-High) - for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration - usb_rom_ptr += usb_rom_len; - usb_rom_len = usb_rom_ptr[2] + usb_rom_ptr[3] * 256; // Get (next) WORD length from descriptor always at bytes 2&3 (Low-High) - } - if ((packet[USB_wLengthHigh] < usb_rom_ptr[3]) || - (packet[USB_wLengthHigh] == usb_rom_ptr[3] && packet[USB_wLength] < usb_rom_ptr[2])) - usb_rom_len = packet[USB_wLength] + packet[USB_wLengthHigh] * 256; // If the HOST asked for LESS then must adjust count to the smaller number - break; - case USB_STRING_DESCRIPTOR_TYPE: - // TODO: Handle language request. For now return standard language. - if (packet[USB_bDescriptorIndex] >= usb_num_string_descriptors) { - flag_usb_RequestError(); - break; - } - usb_rom_ptr = usb_string_descriptor; - usb_rom_len = usb_rom_ptr[0]; // Get BYTE length from descriptor always at byte [0] - for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration - usb_rom_ptr += usb_rom_len; - usb_rom_len = usb_rom_ptr[0]; - } - if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_rom_ptr[0])) - usb_rom_len = packet[USB_wLength]; - break; - case USB_INTERFACE_DESCRIPTOR_TYPE: - case USB_ENDPOINT_DESCRIPTOR_TYPE: - default: - flag_usb_RequestError(); - } - if (0 == usbrequesterrorflag) { - usb_send_rom(); // Send first part of packet right away, the rest is handled by the EP0 IN handler. - usb_set_in_handler(0, usb_send_rom); - } else { - usb_RequestError(); - } - - break; - case USB_REQUEST_GET_CONFIGURATION: - EP0_Inbdp->BDADDR[0] = usb_current_cfg; - usb_ack_dat1(1); - break; - - case USB_REQUEST_SET_CONFIGURATION: - if (USB_NUM_CONFIGURATIONS >= packet[USB_wValue]) { - // TODO: Support multiple configurations - /* Configure endpoints (USB_UEPn - registers) */ - usb_current_cfg = packet[USB_wValue]; - if (usb_current_cfg != 0) { - - // JTR user_configured_init major addition. This is a CALLBACK to the USER when the device is enumerated. - // This is when we setup non EP0 endpoints. - // TODO: This really could be a function pointer - - usb_device_state = CONFIGURED_STATE; - user_configured_init(); - } else { - usb_device_state = ADDRESS_STATE; - } - - usb_ack_dat1(0); - - } else - usb_RequestError(); - break; - - case USB_REQUEST_SET_DESCRIPTOR: - default: - usb_RequestError(); - } -} - -void usb_handle_StandardInterfaceRequest(BDentry *bdp) { - BYTE *packet = bdp->BDADDR; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - EP0_Inbdp->BDADDR[0] = 0x00; - EP0_Inbdp->BDADDR[1] = 0x00; - usb_ack_dat1(2); - break; - case USB_REQUEST_GET_INTERFACE: - if (USB_NUM_INTERFACES > packet[USB_bInterface]) { - // TODO: Implement alternative interfaces, or move responsibility to class/vendor functions. - EP0_Inbdp->BDADDR[0] = 0; - usb_ack_dat1(1); - } else - usb_RequestError(); - break; - case USB_REQUEST_SET_INTERFACE: - if (USB_NUM_INTERFACES > packet[USB_bInterface] && 0u == packet[USB_wValue]) { - // TODO: Implement alternative interfaces... - usb_ack_dat1(0); - } else - usb_RequestError(); - break; - case USB_REQUEST_CLEAR_FEATURE: // JTR N/A for interface - case USB_REQUEST_SET_FEATURE: // This is correct and finished code. - default: - usb_RequestError(); - } -} - -void usb_handle_StandardEndpointRequest(BDentry *bdp) { - BYTE *packet; - BYTE epnum; - BYTE dir; - BDentry *epbd; - usb_uep_t *pUEP; - - packet = bdp->BDADDR; - - switch (packet[USB_bRequest]) { - case USB_REQUEST_GET_STATUS: - EP0_Inbdp->BDADDR[0] = 0x00; // Assume no stall - EP0_Inbdp->BDADDR[1] = 0x00; // Same for stall or not - epnum = packet[USB_wIndex] & 0x0F; - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - if (epbd->BDSTAT &= ~BSTALL) - EP0_Inbdp->BDADDR[0] = 0x01; // EVEN BD is stall flag set? - //epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - //if (epbd->BDSTAT &= ~BSTALL) - // rbdp->BDADDR[0] = 0x01; // ODD BD is stall flag set? - usb_ack_dat1(2); - break; - - case USB_REQUEST_CLEAR_FEATURE: - // As this is really is an application event and there - // should be a call back and protocol for handling the - // possible lost of a data packet. - // TODO: ping-ping support. - - epnum = packet[USB_wIndex] & 0x0F; // JTR Added V0.2 after microchip stuff up with their documentation. - pUEP = USB_UEP; - pUEP += epnum; - *pUEP &= ~USB_UEP_EPSTALL; - - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - epbd->BDSTAT &= ~BSTALL; - if (dir) epbd->BDSTAT |= DTS; // JTR added IN EP set DTS as it will be toggled to zero next transfer - if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added - - // JTR this pointless ATM. If ping-pong is enabled then you need to track PPBI - // and set up ODD and EVEN BDs in respect to this. See complicated system in - // microchip stack >= 2.8 - - // epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - // epbd->BDSTAT &= ~BSTALL; - // if (dir) epbd->BDSTAT |= DTS; // JTR added - // if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added - - - usb_ack_dat1(0); - break; - - - case USB_REQUEST_SET_FEATURE: - epnum = packet[USB_wIndex] & 0x0F; - dir = packet[USB_wIndex] >> 7; - epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; - epbd->BDSTAT |= BSTALL; - //epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; - //epbd->BDSTAT |= BSTALL; - usb_ack_dat1(0); - break; - case USB_REQUEST_SYNCH_FRAME: - default: - usb_RequestError(); - } -} - -void usb_handle_in(void) { - if (endpoints[USB_STAT2EP(trn_status)].in_handler) { - endpoints[USB_STAT2EP(trn_status)].in_handler(); - } -} - -void usb_handle_out(void) { - if (endpoints[USB_STAT2EP(trn_status)].out_handler) { - endpoints[USB_STAT2EP(trn_status)].out_handler(); - } -} - -void usb_register_sof_handler(usb_handler_t handler) { - sof_handler = handler; -} - -void usb_register_class_setup_handler(usb_handler_t handler) { - class_setup_handler = handler; -} - -void usb_register_vendor_setup_handler(usb_handler_t handler) { - vendor_setup_handler = handler; -} - -void usb_set_in_handler(int ep, usb_handler_t in_handler) { - endpoints[ep].in_handler = in_handler; -} - -void usb_set_out_handler(int ep, usb_handler_t out_handler) { - endpoints[ep].out_handler = out_handler; -} - -// JTR New added helper function use extensively by the standard and class -// request handlers. All status IN packets are DAT1 as is the first DATA packet -// of a IN transfer. Currently with this CDC stack the only IN DATA transfers -// that are > 8 bytes is the descriptor transfer and these are transfered in -// usb_send_rom() - -void usb_ack_dat1(int bdcnt) { - EP0_Inbdp->BDCNT = (bdcnt & 0xFF); - EP0_Inbdp->BDSTAT = (DTS | UOWN | DTSEN); // | ((bdcnt & 0x300) >> 8)); -} - -void usb_RequestError(void) { - - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; - //usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDCNT = USB_EP0_BUFFER_SIZE; - - usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; - usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; - //usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; - //usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; - - // JTR TODO: Should also kill the IN and OUT handlers? - -} - -void usb_set_address(void) { - if (0x00u == usb_addr_pending) { - usb_device_state = DEFAULT_STATE; - } else { - usb_device_state = ADDRESS_STATE; - } - SetUsbAddress(usb_addr_pending); - usb_addr_pending = 0xFF; - usb_unset_in_handler(0); // Unregister handler -} - -void usb_send_rom(void) { - - unsigned int i; - size_t packet_len; - if (usb_rom_len) { - packet_len = (usb_rom_len < USB_EP0_BUFFER_SIZE) ? usb_rom_len : USB_EP0_BUFFER_SIZE; // JTR changed from MAX_BUFFER_SIZE - - for (i = 0; i < packet_len; i++) { - EP0_Inbdp->BDADDR[i] = usb_rom_ptr[i]; - } - } else { - packet_len = 0; - usb_unset_in_handler(0); - } - - EP0_Inbdp->BDCNT = (BYTE) packet_len; - EP0_Inbdp->BDSTAT = ((EP0_Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // Packet length always less then 256 on endpoint 0 - - usb_rom_ptr += packet_len; - usb_rom_len -= packet_len; -} - +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + +Influence and inspiration taken from http://pe.ece.olin.edu/ece/projects.html + */ + +// JTR v0.2a Jan 26th 2012 + + +#include "../dp_usb/usb_stack_globals.h" // USB stack only defines Not function related. + +#if USB_EP0_BUFFER_SIZE == 8u +#elif USB_EP0_BUFFER_SIZE == 16u +#elif USB_EP0_BUFFER_SIZE == 32u +#elif USB_EP0_BUFFER_SIZE == 64u +#else +#error "USB_EP0_BUFFER_SIZE needs to be 8, 16, 32 or 64 bytes" +#endif + + +ROMPTR const BYTE *usb_device_descriptor; +ROMPTR const BYTE *usb_config_descriptor; +ROMPTR const BYTE *usb_string_descriptor; +int usb_num_string_descriptors; + +usb_handler_t sof_handler; +usb_handler_t class_setup_handler, vendor_setup_handler; + + +usb_ep_t endpoints[MAX_CHIP_EP]; // JTR change. MAX_CHIP_EP is the number of hardware endpoints on the silicon. See picusb.h + +/* Allocate buffers for buffer description table and the actual buffers */ +// CvD: linkscript puts it in the right memory location + + +// JTR comments. This below goes part way to ridding us of the need for a linker script (PIC18). +// It floats the EP0 buffers to occupy space immediately behind the buffer descriptors. +// However there are problems. It only works for the EP0 buffers because they are the only +// buffers defined in this block. It is not possible to do arithmatic within a #pragma +// therefore there seems to be no way to cause EPs in other blocks to float. +// All the buffers would need to be defined here and this would break the universal nature +// of this module. As such efforts to create a custom linker script free stack have been +// put on hold for now but can be revisited at any stage. +// Please see http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=120#p17401 + +#if defined(PIC_18F) +#pragma udata usb_bdt +BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED]; // JTR changed index from 32 to variable +#pragma udata usb_data +//* Only claim buffer for ep 0 */ +#if USB_PP_BUF_MODE == 0 +BYTE usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; +BYTE usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; +#else +#error "Ping pong buffer not implemented yet!" +#endif + +#elif defined(PIC_24F) +#pragma udata usb_bdt +BDentry usb_bdt[2 + 2 * MAX_EPNUM_USED] __attribute__((aligned(512))); // JTR changed index from 32 to variable TODO: Dynamic allocation reflecting number of used endpoints. (How to do counting in preprocessor?) +#if USB_PP_BUF_MODE == 0 +BYTE usb_ep0_out_buf[USB_EP0_BUFFER_SIZE]; +BYTE usb_ep0_in_buf[USB_EP0_BUFFER_SIZE]; +#else +#error "Ping pong buffer not implemented yet!" +#endif +#endif + +#pragma udata +unsigned int usb_device_status; +unsigned int usb_current_cfg; +volatile BYTE usb_device_state; +BYTE usb_addr_pending; +BYTE trn_status; // Global since it is needed everywere +BDentry *EP0_Outbdp, *EP0_Inbdp; // Dito +BYTE IsSuspended = 0; +ROMPTR const BYTE *usb_rom_ptr; +size_t usb_rom_len; +volatile BYTE usbrequesterrorflag; + +void usb_init(ROMPTR const BYTE *device_descriptor, + ROMPTR const BYTE *config_descriptor, + ROMPTR const BYTE *string_descriptor, + int num_string_descriptors) { + + usb_device_descriptor = device_descriptor; + usb_config_descriptor = config_descriptor; + usb_string_descriptor = string_descriptor; + usb_num_string_descriptors = num_string_descriptors; + sof_handler = NULL; + class_setup_handler = NULL; + vendor_setup_handler = NULL; + usb_unset_in_handler(0); + usb_unset_out_handler(0); + ClearUSBtoDefault(); + ConfigureUsbHardware(); + EnablePacketTransfer(); +} + +void usb_start(void) { + EnableUsb(); // Enable USB-hardware + usb_device_state = ATTACHED_STATE; + while (SingleEndedZeroIsSet()); // Busywait for initial power-up + usb_device_state = DEFAULT_STATE; //JTR2 +} + +void usb_handle_error(void) { + /* No errorhandler for now, just clear offending flag*/ + ClearAllUsbErrorInterruptFlags(); +} + +void usb_handle_reset(void) { + do { + ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. + usb_current_cfg = 0; + usb_device_state = DEFAULT_STATE; // This BLOCK creates the requied 6 cycle delay for TRNF to reassert. + usb_addr_pending = 0x00; + } while (USB_TRANSACTION_FLAG); + + ClearUSBtoDefault(); + EnablePacketTransfer(); +} + +void ClearUSBtoDefault(void) { + int i; + sof_handler = NULL; + class_setup_handler = NULL; + vendor_setup_handler = NULL; + + SetUsbAddress(0); // After reset we don't have an address + ResetPPbuffers(); + ClearAllUsbErrorInterruptFlags(); + + for (i = 0; i < MAX_CHIP_EP; i++) { + endpoints[i].out_handler = NULL; + endpoints[i].in_handler = NULL; + USB_UEP[i] = 0; + } + + for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i++) { + usb_bdt[i].BDSTAT = 0; + } + + USB_UEP0 = USB_EP_CONTROL; // Configure Only ep0 At this point. + //usbrequesterrorflag = 0; + +#ifdef USB_SELF_POWERED + + // JTR TODO this isn't actually 100% correct. "usb_device_status" is a runtime variable + // In the case of a bus powered device it will always be 0x000 but in the case of + // self powered with bus powered option it becames variable to the current powered + // State. This is a minor thing and for now but it may need to be addressed if there is + // any hardware that is dual powered. + + usb_device_status = 0x0001; +#else + usb_device_status = 0x0000; +#endif + usb_device_state = DETACHED_STATE; // JTR added flag byte for enumeration state + usb_current_cfg = 0; // JTR formally usb_configured + usb_addr_pending = 0x00; + +#if USB_PP_BUF_MODE == NO_PINGPONG + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS + DTSEN; // Set DTS => First packet inverts, ie. is Data0 +#else +#error "Invalid PING_PONG mode" +#endif +} + +void usb_handler(void) { + + if (USB_IDLE_FLAG) { + /* Idle - suspend */ + USBSuspend(); // // Must be defined in user code. + ClearUsbInterruptFlag(USB_IDLE); + } + + if (USB_RESET_FLAG) { + usb_handle_reset(); + ClearUsbInterruptFlag(USB_URST); + } + if (USB_ERROR_FLAG) { + // usb_handle_error(); + ClearAllUsbErrorInterruptFlags(); + ClearUsbInterruptFlag(USB_UERR); + } + if (USB_STALL_FLAG) { + ClearUsbInterruptFlag(USB_STALL); + } + if (USB_SOF_FLAG) { + /* Start-of-frame */ + if (sof_handler) sof_handler(); + ClearUsbInterruptFlag(USB_SOF); + } + + if (USB_TRANSACTION_FLAG) { + if (!USB_STAT2EP(GetUsbTransaction())) + usb_handle_transaction(); // Only handle EP0 transactions. + ClearUsbInterruptFlag(USB_TRN); // JTR Missing! This is why Ian was only getting one interrupt?? + } // Side effect: advance USTAT Fifo +} + +void usb_handle_transaction(void) { + + usbrequesterrorflag = 0; + + trn_status = GetUsbTransaction(); + EP0_Outbdp = &usb_bdt[USB_USTAT2BD(trn_status)]; + EP0_Inbdp = &usb_bdt[USB_USTAT2BD(trn_status | DIRBIT)]; // All replies in IN direction + + switch (EP0_Outbdp->BDSTAT & USB_TOKEN_Mask) { + case USB_TOKEN_SETUP: + usb_handle_setup(); + break; + case USB_TOKEN_OUT: + usb_handle_out(); + break; + case USB_TOKEN_IN: + usb_handle_in(); + break; + //default: + /* Default case of unknown TOKEN - discard */ + } +} + +void usb_handle_setup(void) { + + EP0_Inbdp->BDSTAT = DTSEN; // Reclaim reply buffer + + EnablePacketTransfer(); // JTR this is placed here to overcome a errate issue with early PIC18 USB pics. + + switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) { + case USB_bmRequestType_Standard: + switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) { + case USB_bmRequestType_Device: + usb_handle_StandardDeviceRequest(EP0_Outbdp); + break; + case USB_bmRequestType_Interface: + usb_handle_StandardInterfaceRequest(EP0_Outbdp); + break; + case USB_bmRequestType_Endpoint: + usb_handle_StandardEndpointRequest(EP0_Outbdp); + break; + default: + usb_RequestError(); + } + break; + case USB_bmRequestType_Class: + if (class_setup_handler) class_setup_handler(); + break; + case USB_bmRequestType_Vendor: + //ROBOTS FIX: http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=3849&view=unread#unread + // did call class_setup_handler(); + if (vendor_setup_handler) vendor_setup_handler(); + break; + default: + usb_RequestError(); + } + /* Prepare endpoint for new reception */ + + EP0_Outbdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size of EP0, should always be ep0? + + // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)? + // note that this is not an entirely robust way of doing things. See the microchip stack for + // further comments and a better system as this does not account for errors and retries + // and it results in the SIE not accepting the final out ZLP status packet for IN transfers + // with a data stage. However it works but it is not anything to be proud of... + + EP0_Outbdp->BDSTAT = (!(EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) && + (EP0_Outbdp->BDADDR[USB_wLength] || EP0_Outbdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN; +} + +void usb_handle_StandardDeviceRequest(BDentry *bdp) { + BYTE *packet = bdp->BDADDR; + int i; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + EP0_Inbdp->BDADDR[0] = usb_device_status & 0xFF; + EP0_Inbdp->BDADDR[1] = usb_device_status >> 8; + usb_ack_dat1(2); + break; + case USB_REQUEST_CLEAR_FEATURE: + if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) + usb_device_status &= ~0x0002; + usb_ack_dat1(0); + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_FEATURE: + if (0x01u == packet[USB_wValue]) { // TODO: Remove magic (REMOTE_WAKEUP_FEATURE) + usb_device_status |= 0x0002; + usb_ack_dat1(0); + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_ADDRESS: + if (0x00u == packet[USB_wValueHigh] && 0x7Fu >= packet[USB_wValue]) { + usb_addr_pending = packet[USB_wValue]; + usb_set_in_handler(0, usb_set_address); + usb_ack_dat1(0); + } else + usb_RequestError(); + break; + + + case USB_REQUEST_GET_DESCRIPTOR: + switch (packet[USB_bDescriptorType]) { + case USB_DEVICE_DESCRIPTOR_TYPE: // There is only every one in pratice. + usb_rom_ptr = usb_device_descriptor; + usb_rom_len = usb_device_descriptor[0]; // Get BYTE length from descriptor always at byte [0] + if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_rom_ptr[0])) + usb_rom_len = packet[USB_wLength]; // If the HOST asked for LESS then must adjust count to the smaller number + break; + + case USB_CONFIGURATION_DESCRIPTOR_TYPE: + if (packet[USB_bDescriptorIndex] >= usb_device_descriptor[17]) { + flag_usb_RequestError(); + break; + } + + usb_rom_ptr = usb_config_descriptor; + usb_rom_len = usb_rom_ptr[2] + usb_rom_ptr[3] * 256; // Get WORD length from descriptor always at bytes 2&3 (Low-High) + for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration + usb_rom_ptr += usb_rom_len; + usb_rom_len = usb_rom_ptr[2] + usb_rom_ptr[3] * 256; // Get (next) WORD length from descriptor always at bytes 2&3 (Low-High) + } + if ((packet[USB_wLengthHigh] < usb_rom_ptr[3]) || + (packet[USB_wLengthHigh] == usb_rom_ptr[3] && packet[USB_wLength] < usb_rom_ptr[2])) + usb_rom_len = packet[USB_wLength] + packet[USB_wLengthHigh] * 256; // If the HOST asked for LESS then must adjust count to the smaller number + break; + case USB_STRING_DESCRIPTOR_TYPE: + // TODO: Handle language request. For now return standard language. + if (packet[USB_bDescriptorIndex] >= usb_num_string_descriptors) { + flag_usb_RequestError(); + break; + } + usb_rom_ptr = usb_string_descriptor; + usb_rom_len = usb_rom_ptr[0]; // Get BYTE length from descriptor always at byte [0] + for (i = 0; i < packet[USB_bDescriptorIndex]; i++) { // Implicit linked list traversal until requested configuration + usb_rom_ptr += usb_rom_len; + usb_rom_len = usb_rom_ptr[0]; + } + if ((0 == packet[USB_wLengthHigh] && packet[USB_wLength] < usb_rom_ptr[0])) + usb_rom_len = packet[USB_wLength]; + break; + case USB_INTERFACE_DESCRIPTOR_TYPE: + case USB_ENDPOINT_DESCRIPTOR_TYPE: + default: + flag_usb_RequestError(); + } + if (0 == usbrequesterrorflag) { + usb_send_rom(); // Send first part of packet right away, the rest is handled by the EP0 IN handler. + usb_set_in_handler(0, usb_send_rom); + } else { + usb_RequestError(); + } + + break; + case USB_REQUEST_GET_CONFIGURATION: + EP0_Inbdp->BDADDR[0] = usb_current_cfg; + usb_ack_dat1(1); + break; + + case USB_REQUEST_SET_CONFIGURATION: + if (USB_NUM_CONFIGURATIONS >= packet[USB_wValue]) { + // TODO: Support multiple configurations + /* Configure endpoints (USB_UEPn - registers) */ + usb_current_cfg = packet[USB_wValue]; + if (usb_current_cfg != 0) { + + // JTR user_configured_init major addition. This is a CALLBACK to the USER when the device is enumerated. + // This is when we setup non EP0 endpoints. + // TODO: This really could be a function pointer + + usb_device_state = CONFIGURED_STATE; + user_configured_init(); + } else { + usb_device_state = ADDRESS_STATE; + } + + usb_ack_dat1(0); + + } else + usb_RequestError(); + break; + + case USB_REQUEST_SET_DESCRIPTOR: + default: + usb_RequestError(); + } +} + +void usb_handle_StandardInterfaceRequest(BDentry *bdp) { + BYTE *packet = bdp->BDADDR; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + EP0_Inbdp->BDADDR[0] = 0x00; + EP0_Inbdp->BDADDR[1] = 0x00; + usb_ack_dat1(2); + break; + case USB_REQUEST_GET_INTERFACE: + if (USB_NUM_INTERFACES > packet[USB_bInterface]) { + // TODO: Implement alternative interfaces, or move responsibility to class/vendor functions. + EP0_Inbdp->BDADDR[0] = 0; + usb_ack_dat1(1); + } else + usb_RequestError(); + break; + case USB_REQUEST_SET_INTERFACE: + if (USB_NUM_INTERFACES > packet[USB_bInterface] && 0u == packet[USB_wValue]) { + // TODO: Implement alternative interfaces... + usb_ack_dat1(0); + } else + usb_RequestError(); + break; + case USB_REQUEST_CLEAR_FEATURE: // JTR N/A for interface + case USB_REQUEST_SET_FEATURE: // This is correct and finished code. + default: + usb_RequestError(); + } +} + +void usb_handle_StandardEndpointRequest(BDentry *bdp) { + BYTE *packet; + BYTE epnum; + BYTE dir; + BDentry *epbd; + usb_uep_t *pUEP; + + packet = bdp->BDADDR; + + switch (packet[USB_bRequest]) { + case USB_REQUEST_GET_STATUS: + EP0_Inbdp->BDADDR[0] = 0x00; // Assume no stall + EP0_Inbdp->BDADDR[1] = 0x00; // Same for stall or not + epnum = packet[USB_wIndex] & 0x0F; + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + if (epbd->BDSTAT &= ~BSTALL) + EP0_Inbdp->BDADDR[0] = 0x01; // EVEN BD is stall flag set? + //epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + //if (epbd->BDSTAT &= ~BSTALL) + // rbdp->BDADDR[0] = 0x01; // ODD BD is stall flag set? + usb_ack_dat1(2); + break; + + case USB_REQUEST_CLEAR_FEATURE: + // As this is really is an application event and there + // should be a call back and protocol for handling the + // possible lost of a data packet. + // TODO: ping-ping support. + + epnum = packet[USB_wIndex] & 0x0F; // JTR Added V0.2 after microchip stuff up with their documentation. + pUEP = USB_UEP; + pUEP += epnum; + *pUEP &= ~USB_UEP_EPSTALL; + + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + epbd->BDSTAT &= ~BSTALL; + if (dir) epbd->BDSTAT |= DTS; // JTR added IN EP set DTS as it will be toggled to zero next transfer + if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added + + // JTR this pointless ATM. If ping-pong is enabled then you need to track PPBI + // and set up ODD and EVEN BDs in respect to this. See complicated system in + // microchip stack >= 2.8 + + // epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + // epbd->BDSTAT &= ~BSTALL; + // if (dir) epbd->BDSTAT |= DTS; // JTR added + // if (0 == dir) epbd->BDSTAT &= ~DTS; // JTR added + + + usb_ack_dat1(0); + break; + + + case USB_REQUEST_SET_FEATURE: + epnum = packet[USB_wIndex] & 0x0F; + dir = packet[USB_wIndex] >> 7; + epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_EVEN)]; + epbd->BDSTAT |= BSTALL; + //epbd = &usb_bdt[USB_CALC_BD(epnum, dir, USB_PP_ODD)]; + //epbd->BDSTAT |= BSTALL; + usb_ack_dat1(0); + break; + case USB_REQUEST_SYNCH_FRAME: + default: + usb_RequestError(); + } +} + +void usb_handle_in(void) { + if (endpoints[USB_STAT2EP(trn_status)].in_handler) { + endpoints[USB_STAT2EP(trn_status)].in_handler(); + } +} + +void usb_handle_out(void) { + if (endpoints[USB_STAT2EP(trn_status)].out_handler) { + endpoints[USB_STAT2EP(trn_status)].out_handler(); + } +} + +void usb_register_sof_handler(usb_handler_t handler) { + sof_handler = handler; +} + +void usb_register_class_setup_handler(usb_handler_t handler) { + class_setup_handler = handler; +} + +void usb_register_vendor_setup_handler(usb_handler_t handler) { + vendor_setup_handler = handler; +} + +void usb_set_in_handler(int ep, usb_handler_t in_handler) { + endpoints[ep].in_handler = in_handler; +} + +void usb_set_out_handler(int ep, usb_handler_t out_handler) { + endpoints[ep].out_handler = out_handler; +} + +// JTR New added helper function use extensively by the standard and class +// request handlers. All status IN packets are DAT1 as is the first DATA packet +// of a IN transfer. Currently with this CDC stack the only IN DATA transfers +// that are > 8 bytes is the descriptor transfer and these are transfered in +// usb_send_rom() + +void usb_ack_dat1(int bdcnt) { + EP0_Inbdp->BDCNT = (bdcnt & 0xFF); + EP0_Inbdp->BDSTAT = (DTS | UOWN | DTSEN); // | ((bdcnt & 0x300) >> 8)); +} + +void usb_RequestError(void) { + + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; + //usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDCNT = USB_EP0_BUFFER_SIZE; + + usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; + usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + BSTALL; + //usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; + //usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_ODD)].BDSTAT = UOWN + BSTALL; + + // JTR TODO: Should also kill the IN and OUT handlers? + +} + +void usb_set_address(void) { + if (0x00u == usb_addr_pending) { + usb_device_state = DEFAULT_STATE; + } else { + usb_device_state = ADDRESS_STATE; + } + SetUsbAddress(usb_addr_pending); + usb_addr_pending = 0xFF; + usb_unset_in_handler(0); // Unregister handler +} + +void usb_send_rom(void) { + + unsigned int i; + size_t packet_len; + if (usb_rom_len) { + packet_len = (usb_rom_len < USB_EP0_BUFFER_SIZE) ? usb_rom_len : USB_EP0_BUFFER_SIZE; // JTR changed from MAX_BUFFER_SIZE + + for (i = 0; i < packet_len; i++) { + EP0_Inbdp->BDADDR[i] = usb_rom_ptr[i]; + } + } else { + packet_len = 0; + usb_unset_in_handler(0); + } + + EP0_Inbdp->BDCNT = (BYTE) packet_len; + EP0_Inbdp->BDSTAT = ((EP0_Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN; // Packet length always less then 256 on endpoint 0 + + usb_rom_ptr += packet_len; + usb_rom_len -= packet_len; +} + diff --git a/Firmware/dp_usb/usb_stack.h b/Firmware/dp_usb/usb_stack.h index 78da1510..778a1a58 100644 --- a/Firmware/dp_usb/usb_stack.h +++ b/Firmware/dp_usb/usb_stack.h @@ -1,289 +1,289 @@ -/* -This work is licensed under the Creative Commons Attribution 3.0 Unported License. -To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ -or send a letter to - Creative Commons, - 171 Second Street, - Suite 300, - San Francisco, - California, - 94105, - USA. - */ - -#ifndef __USB_STACK_H__ -#define __USB_STACK_H__ - -// JTR v0.2a - -#include "../dp_usb/picusb.h" // JTR combined PIC_18F and PIC_24F header. This is the only included header. - -#define USB_TOKEN_Mask 0b00111100 -#define USB_TOKEN_OUT 0b00000100 -#define USB_TOKEN_IN 0b00100100 -#define USB_TOKEN_SOF 0b00010100 -#define USB_TOKEN_SETUP 0b00110100 -#define USB_TOKEN_DATA0 0b00001100 -#define USB_TOKEN_DATA1 0b00101100 -#define USB_TOKEN_DATA2 0b00011100 /* High speed isochronous endpoints only */ -#define USB_TOKEN_MDATA 0b00111100 /* High speed isochronous enpoints and hub devices only */ -#define USB_TOKEN_ACK 0b00001000 -#define USB_TOKEN_NAK 0b00101000 -#define USB_TOKEN_STALL 0b00111000 -#define USB_TOKEN_NYET 0b00011000 /* High speed devices only */ -#define USB_TOKEN_PRE 0b00110000 -#define USB_TOKEN_ERR 0b00110000 -#define USB_TOKEN_SPLIT 0b00100000 /* Hub devices only */ -#define USB_TOKEN_PING 0b00010000 /* High speed devices only */ - -/* Descriptor Types */ -#define USB_DEVICE_DESCRIPTOR_TYPE 1u -#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2u -#define USB_STRING_DESCRIPTOR_TYPE 3u -#define USB_INTERFACE_DESCRIPTOR_TYPE 4u -#define USB_ENDPOINT_DESCRIPTOR_TYPE 5u -#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6u -#define USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR_TYPE 7u -#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8u -#define USB_OTG_DESCRIPTOR_TYPE 9u -#define USB_DEBUG_DESCRIPTOR_TYPE 10u -#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11u - -#define USB_bmRequestType 0 -#define USB_bRequest 1 -#define USB_wValue 2 -#define USB_bDescriptorIndex 2 -#define USB_wValueHigh 3 -#define USB_bDescriptorType 3 -#define USB_wIndex 4 -#define USB_bInterface 4 -#define USB_wIndexHigh 5 -#define USB_wLength 6 -#define USB_wLengthHigh 7 - -#define USB_bmRequestType_PhaseMask 0b10000000 -#define USB_bmRequestType_H2D 0b00000000 -#define USB_bmRequestType_D2H 0b10000000 -#define USB_bmRequestType_TypeMask 0b01100000 -#define USB_bmRequestType_Standard 0b00000000 -#define USB_bmRequestType_Class 0b00100000 -#define USB_bmRequestType_Vendor 0b01000000 -#define USB_bmRequestType_RecipientMask 0b00000011 -#define USB_bmRequestType_Device 0b00000000 -#define USB_bmRequestType_Interface 0b00000001 -#define USB_bmRequestType_Endpoint 0b00000010 -#define USB_bmRequestType_Other 0b00000011 - -#define USB_REQUEST_GET_STATUS 0 -#define USB_REQUEST_CLEAR_FEATURE 1 -#define USB_REQUEST_SET_FEATURE 3 -#define USB_REQUEST_SET_ADDRESS 5 -#define USB_REQUEST_GET_DESCRIPTOR 6 -#define USB_REQUEST_SET_DESCRIPTOR 7 -#define USB_REQUEST_GET_CONFIGURATION 8 -#define USB_REQUEST_SET_CONFIGURATION 9 -#define USB_REQUEST_GET_INTERFACE 10 -#define USB_REQUEST_SET_INTERFACE 11 -#define USB_REQUEST_SYNCH_FRAME 12 - -typedef enum -{ -DETACHED_STATE = 0x00, -ATTACHED_STATE = 0x01, -POWERED_STATE = 0x02, -DEFAULT_STATE = 0x04, -ADR_PENDING_STATE = 0x08, -ADDRESS_STATE = 0x10, -CONFIGURED_STATE = 0x20 -} USB_DEVICE_STATE; - - -/* Structs for defining endpoints */ -//typedef void(*)(BYTE *) usb_ep_callback; -typedef void(*usb_handler_t)(void); - -typedef struct USB_EP_TYPE { -// usb_uep_t type; -// unsigned int buffer_size; -// BYTE *in_buffer, *out_buffer; - usb_handler_t in_handler, out_handler; -} usb_ep_t; - -/* Misc */ -#define HIGHB(x) ((x)>>8) -#define LOWB(x) ((x) & 0xFF) - -#define XCAT(x,y) x ## y -#define CAT(x,y) XCAT(x,y) - -/* Descriptors */ -#if USB_NUM_CONFIGURATIONS > 1 -#error "More than 1 configuration not supported yet" -#endif - -extern BDentry usb_bdt[]; - -typedef struct USB_DEVICE_REQUEST { - BYTE bmRequestType; - BYTE bRequest; - unsigned int wValue; - unsigned int wIndex; - unsigned int wLength; -} usb_device_request; - -extern BYTE trn_status; -extern BDentry *EP0_Outbdp, *EP0_Inbdp; - -void usb_init(ROMPTR const BYTE *dev_descriptor, - ROMPTR const BYTE *config_descriptor, - ROMPTR const BYTE *string_descriptor, int num_string_descriptors); - - -/* Forward Reference Prototypes */ -void usb_start(void); -void usb_register_sof_handler(usb_handler_t handler); -void usb_register_class_setup_handler(usb_handler_t handler); -void usb_register_vendor_setup_handler(usb_handler_t handler); -void usb_set_in_handler(int ep, usb_handler_t handler); -void usb_set_out_handler(int ep, usb_handler_t handler); -void usb_handle_transaction(void); -void usb_handler(void); -void usb_RequestError(void); -void USBWakeFromSuspend(void); -void USBSuspend(void); -void usb_ack_dat1(int bdcnt); // JTR added standard for both STD and CLASS -void usb_handle_error(void); -void usb_handle_reset(void); -void usb_handle_setup(void); -void usb_handle_out(void); -void usb_handle_in(void); -void usb_handle_StandardDeviceRequest(BDentry*); -void usb_handle_StandardInterfaceRequest(BDentry*); -void usb_handle_StandardEndpointRequest(BDentry*); -void usb_RequestError(void); -void usb_set_address(void); -void usb_send_rom(void); -void ClearUSBtoDefault(void); - -#define usb_unset_in_handler(ep) usb_set_in_handler(ep, (usb_handler_t) 0) -#define usb_unset_out_handler(ep) usb_set_out_handler(ep, (usb_handler_t) 0) -#define flag_usb_RequestError() usbrequesterrorflag = 1 - -#define USB_LANGID_Chinese_Macau_SAR 0x1404 -#define USB_LANGID_Croatian 0x041a -#define USB_LANGID_Czech 0x0405 -#define USB_LANGID_Danish 0x0406 -#define USB_LANGID_Dutch_Netherlands 0x0413 -#define USB_LANGID_Dutch_Belgium 0x0813 -#define USB_LANGID_English_United_States 0x0409 -#define USB_LANGID_English_United_Kingdom 0x0809 -#define USB_LANGID_English_Australian 0x0c09 -#define USB_LANGID_English_Canadian 0x1009 -#define USB_LANGID_English_New_Zealand 0x1409 -#define USB_LANGID_English_Ireland 0x1809 -#define USB_LANGID_English_South_Africa 0x1c09 -#define USB_LANGID_English_Jamaica 0x2009 -#define USB_LANGID_English_Caribbean 0x2409 -#define USB_LANGID_English_Belize 0x2809 -#define USB_LANGID_English_Trinidad 0x2c09 -#define USB_LANGID_English_Zimbabwe 0x3009 -#define USB_LANGID_English_Philippines 0x3409 -#define USB_LANGID_Estonian 0x0425 -#define USB_LANGID_Faeroese 0x0438 -#define USB_LANGID_Farsi 0x0429 -#define USB_LANGID_Finnish 0x040b -#define USB_LANGID_French_Standard 0x040c -#define USB_LANGID_French_Belgian 0x080c -#define USB_LANGID_French_Canadian 0x0c0c -#define USB_LANGID_French_Switzerland 0x100c -#define USB_LANGID_French_Luxembourg 0x140c -#define USB_LANGID_French_Monaco 0x180c -#define USB_LANGID_Georgian 0x0437 -#define USB_LANGID_German_Standard 0x0407 -#define USB_LANGID_German_Switzerland 0x0807 -#define USB_LANGID_German_Austria 0x0c07 -#define USB_LANGID_German_Luxembourg 0x1007 -#define USB_LANGID_German_Liechtenstein 0x1407 -#define USB_LANGID_Greek 0x0408 -#define USB_LANGID_Gujarati 0x0447 -#define USB_LANGID_Hebrew 0x040d -#define USB_LANGID_Hindi 0x0439 -#define USB_LANGID_Hungarian 0x040e -#define USB_LANGID_Icelandic 0x040f -#define USB_LANGID_Indonesian 0x0421 -#define USB_LANGID_Italian_Standard 0x0410 -#define USB_LANGID_Italian_Switzerland 0x0810 -#define USB_LANGID_Japanese 0x0411 -#define USB_LANGID_Kannada 0x044b -#define USB_LANGID_Kashmiri_India 0x0860 -#define USB_LANGID_Kazakh 0x043f -#define USB_LANGID_Konkani 0x0457 -#define USB_LANGID_Korean 0x0412 -#define USB_LANGID_Korean_Johab 0x0812 -#define USB_LANGID_Latvian 0x0426 -#define USB_LANGID_Lithuanian 0x0427 -#define USB_LANGID_Lithuanian_Classic 0x0827 -#define USB_LANGID_Macedonian 0x042f -#define USB_LANGID_Malay_Malaysian 0x043e -#define USB_LANGID_Malay_Brunei_Darussalam 0x083e -#define USB_LANGID_Malayalam 0x044c -#define USB_LANGID_Manipuri 0x0458 -#define USB_LANGID_Marathi 0x044e -#define USB_LANGID_Nepali_India 0x0861 -#define USB_LANGID_Norwegian_Bokmal 0x0414 -#define USB_LANGID_Norwegian_Nynorsk 0x0814 -#define USB_LANGID_Oriya 0x0448 -#define USB_LANGID_Polish 0x0415 -#define USB_LANGID_Portuguese_Brazil 0x0416 -#define USB_LANGID_Portuguese_Standard 0x0816 -#define USB_LANGID_Punjabi 0x0446 -#define USB_LANGID_Romanian 0x0418 -#define USB_LANGID_Russian 0x0419 -#define USB_LANGID_Sanskrit 0x044f -#define USB_LANGID_Serbian_Cyrillic 0x0c1a -#define USB_LANGID_Serbian_Latin 0x081a -#define USB_LANGID_Sindhi 0x0459 -#define USB_LANGID_Slovak 0x041b -#define USB_LANGID_Slovenian 0x0424 -#define USB_LANGID_Spanish_Traditional_Sort 0x040a -#define USB_LANGID_Spanish_Mexican 0x080a -#define USB_LANGID_Spanish_Modern_Sort 0x0c0a -#define USB_LANGID_Spanish_Guatemala 0x100a -#define USB_LANGID_Spanish_Costa_Rica 0x140a -#define USB_LANGID_Spanish_Panama 0x180a -#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a -#define USB_LANGID_Spanish_Venezuela 0x200a -#define USB_LANGID_Spanish_Colombia 0x240a -#define USB_LANGID_Spanish_Peru 0x280a -#define USB_LANGID_Spanish_Argentina 0x2c0a -#define USB_LANGID_Spanish_Ecuador 0x300a -#define USB_LANGID_Spanish_Chile 0x340a -#define USB_LANGID_Spanish_Uruguay 0x380a -#define USB_LANGID_Spanish_Paraguay 0x3c0a -#define USB_LANGID_Spanish_Bolivia 0x400a -#define USB_LANGID_Spanish_El_Salvador 0x440a -#define USB_LANGID_Spanish_Honduras 0x480a -#define USB_LANGID_Spanish_Nicaragua 0x4c0a -#define USB_LANGID_Spanish_Puerto_Rico 0x500a -#define USB_LANGID_Sutu 0x0430 -#define USB_LANGID_Swahili_Kenya 0x0441 -#define USB_LANGID_Swedish 0x041d -#define USB_LANGID_Swedish_Finland 0x081d -#define USB_LANGID_Tamil 0x0449 -#define USB_LANGID_Tatar_Tatarstan 0x0444 -#define USB_LANGID_Telugu 0x044a -#define USB_LANGID_Thai 0x041e -#define USB_LANGID_Turkish 0x041f -#define USB_LANGID_Ukrainian 0x0422 -#define USB_LANGID_Urdu_Pakistan 0x0420 -#define USB_LANGID_Urdu_India 0x0820 -#define USB_LANGID_Uzbek_Latin 0x0443 -#define USB_LANGID_Uzbek_Cyrillic 0x0843 -#define USB_LANGID_Vietnamese 0x042a -#define USB_LANGID_HID_UDD 0x04ff -#define USB_LANGID_HID_Vendor1 0xf0ff -#define USB_LANGID_HID_Vendor2 0xf4ff -#define USB_LANGID_HID_Vendor3 0xf8ff -#define USB_LANGID_HID_Vendor4 0xfcff - -#endif /* USB_STACK_H */ +/* +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +or send a letter to + Creative Commons, + 171 Second Street, + Suite 300, + San Francisco, + California, + 94105, + USA. + */ + +#ifndef __USB_STACK_H__ +#define __USB_STACK_H__ + +// JTR v0.2a + +#include "../dp_usb/picusb.h" // JTR combined PIC_18F and PIC_24F header. This is the only included header. + +#define USB_TOKEN_Mask 0b00111100 +#define USB_TOKEN_OUT 0b00000100 +#define USB_TOKEN_IN 0b00100100 +#define USB_TOKEN_SOF 0b00010100 +#define USB_TOKEN_SETUP 0b00110100 +#define USB_TOKEN_DATA0 0b00001100 +#define USB_TOKEN_DATA1 0b00101100 +#define USB_TOKEN_DATA2 0b00011100 /* High speed isochronous endpoints only */ +#define USB_TOKEN_MDATA 0b00111100 /* High speed isochronous enpoints and hub devices only */ +#define USB_TOKEN_ACK 0b00001000 +#define USB_TOKEN_NAK 0b00101000 +#define USB_TOKEN_STALL 0b00111000 +#define USB_TOKEN_NYET 0b00011000 /* High speed devices only */ +#define USB_TOKEN_PRE 0b00110000 +#define USB_TOKEN_ERR 0b00110000 +#define USB_TOKEN_SPLIT 0b00100000 /* Hub devices only */ +#define USB_TOKEN_PING 0b00010000 /* High speed devices only */ + +/* Descriptor Types */ +#define USB_DEVICE_DESCRIPTOR_TYPE 1u +#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2u +#define USB_STRING_DESCRIPTOR_TYPE 3u +#define USB_INTERFACE_DESCRIPTOR_TYPE 4u +#define USB_ENDPOINT_DESCRIPTOR_TYPE 5u +#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6u +#define USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR_TYPE 7u +#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8u +#define USB_OTG_DESCRIPTOR_TYPE 9u +#define USB_DEBUG_DESCRIPTOR_TYPE 10u +#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11u + +#define USB_bmRequestType 0 +#define USB_bRequest 1 +#define USB_wValue 2 +#define USB_bDescriptorIndex 2 +#define USB_wValueHigh 3 +#define USB_bDescriptorType 3 +#define USB_wIndex 4 +#define USB_bInterface 4 +#define USB_wIndexHigh 5 +#define USB_wLength 6 +#define USB_wLengthHigh 7 + +#define USB_bmRequestType_PhaseMask 0b10000000 +#define USB_bmRequestType_H2D 0b00000000 +#define USB_bmRequestType_D2H 0b10000000 +#define USB_bmRequestType_TypeMask 0b01100000 +#define USB_bmRequestType_Standard 0b00000000 +#define USB_bmRequestType_Class 0b00100000 +#define USB_bmRequestType_Vendor 0b01000000 +#define USB_bmRequestType_RecipientMask 0b00000011 +#define USB_bmRequestType_Device 0b00000000 +#define USB_bmRequestType_Interface 0b00000001 +#define USB_bmRequestType_Endpoint 0b00000010 +#define USB_bmRequestType_Other 0b00000011 + +#define USB_REQUEST_GET_STATUS 0 +#define USB_REQUEST_CLEAR_FEATURE 1 +#define USB_REQUEST_SET_FEATURE 3 +#define USB_REQUEST_SET_ADDRESS 5 +#define USB_REQUEST_GET_DESCRIPTOR 6 +#define USB_REQUEST_SET_DESCRIPTOR 7 +#define USB_REQUEST_GET_CONFIGURATION 8 +#define USB_REQUEST_SET_CONFIGURATION 9 +#define USB_REQUEST_GET_INTERFACE 10 +#define USB_REQUEST_SET_INTERFACE 11 +#define USB_REQUEST_SYNCH_FRAME 12 + +typedef enum +{ +DETACHED_STATE = 0x00, +ATTACHED_STATE = 0x01, +POWERED_STATE = 0x02, +DEFAULT_STATE = 0x04, +ADR_PENDING_STATE = 0x08, +ADDRESS_STATE = 0x10, +CONFIGURED_STATE = 0x20 +} USB_DEVICE_STATE; + + +/* Structs for defining endpoints */ +//typedef void(*)(BYTE *) usb_ep_callback; +typedef void(*usb_handler_t)(void); + +typedef struct USB_EP_TYPE { +// usb_uep_t type; +// unsigned int buffer_size; +// BYTE *in_buffer, *out_buffer; + usb_handler_t in_handler, out_handler; +} usb_ep_t; + +/* Misc */ +#define HIGHB(x) ((x)>>8) +#define LOWB(x) ((x) & 0xFF) + +#define XCAT(x,y) x ## y +#define CAT(x,y) XCAT(x,y) + +/* Descriptors */ +#if USB_NUM_CONFIGURATIONS > 1 +#error "More than 1 configuration not supported yet" +#endif + +extern BDentry usb_bdt[]; + +typedef struct USB_DEVICE_REQUEST { + BYTE bmRequestType; + BYTE bRequest; + unsigned int wValue; + unsigned int wIndex; + unsigned int wLength; +} usb_device_request; + +extern BYTE trn_status; +extern BDentry *EP0_Outbdp, *EP0_Inbdp; + +void usb_init(ROMPTR const BYTE *dev_descriptor, + ROMPTR const BYTE *config_descriptor, + ROMPTR const BYTE *string_descriptor, int num_string_descriptors); + + +/* Forward Reference Prototypes */ +void usb_start(void); +void usb_register_sof_handler(usb_handler_t handler); +void usb_register_class_setup_handler(usb_handler_t handler); +void usb_register_vendor_setup_handler(usb_handler_t handler); +void usb_set_in_handler(int ep, usb_handler_t handler); +void usb_set_out_handler(int ep, usb_handler_t handler); +void usb_handle_transaction(void); +void usb_handler(void); +void usb_RequestError(void); +void USBWakeFromSuspend(void); +void USBSuspend(void); +void usb_ack_dat1(int bdcnt); // JTR added standard for both STD and CLASS +void usb_handle_error(void); +void usb_handle_reset(void); +void usb_handle_setup(void); +void usb_handle_out(void); +void usb_handle_in(void); +void usb_handle_StandardDeviceRequest(BDentry*); +void usb_handle_StandardInterfaceRequest(BDentry*); +void usb_handle_StandardEndpointRequest(BDentry*); +void usb_RequestError(void); +void usb_set_address(void); +void usb_send_rom(void); +void ClearUSBtoDefault(void); + +#define usb_unset_in_handler(ep) usb_set_in_handler(ep, (usb_handler_t) 0) +#define usb_unset_out_handler(ep) usb_set_out_handler(ep, (usb_handler_t) 0) +#define flag_usb_RequestError() usbrequesterrorflag = 1 + +#define USB_LANGID_Chinese_Macau_SAR 0x1404 +#define USB_LANGID_Croatian 0x041a +#define USB_LANGID_Czech 0x0405 +#define USB_LANGID_Danish 0x0406 +#define USB_LANGID_Dutch_Netherlands 0x0413 +#define USB_LANGID_Dutch_Belgium 0x0813 +#define USB_LANGID_English_United_States 0x0409 +#define USB_LANGID_English_United_Kingdom 0x0809 +#define USB_LANGID_English_Australian 0x0c09 +#define USB_LANGID_English_Canadian 0x1009 +#define USB_LANGID_English_New_Zealand 0x1409 +#define USB_LANGID_English_Ireland 0x1809 +#define USB_LANGID_English_South_Africa 0x1c09 +#define USB_LANGID_English_Jamaica 0x2009 +#define USB_LANGID_English_Caribbean 0x2409 +#define USB_LANGID_English_Belize 0x2809 +#define USB_LANGID_English_Trinidad 0x2c09 +#define USB_LANGID_English_Zimbabwe 0x3009 +#define USB_LANGID_English_Philippines 0x3409 +#define USB_LANGID_Estonian 0x0425 +#define USB_LANGID_Faeroese 0x0438 +#define USB_LANGID_Farsi 0x0429 +#define USB_LANGID_Finnish 0x040b +#define USB_LANGID_French_Standard 0x040c +#define USB_LANGID_French_Belgian 0x080c +#define USB_LANGID_French_Canadian 0x0c0c +#define USB_LANGID_French_Switzerland 0x100c +#define USB_LANGID_French_Luxembourg 0x140c +#define USB_LANGID_French_Monaco 0x180c +#define USB_LANGID_Georgian 0x0437 +#define USB_LANGID_German_Standard 0x0407 +#define USB_LANGID_German_Switzerland 0x0807 +#define USB_LANGID_German_Austria 0x0c07 +#define USB_LANGID_German_Luxembourg 0x1007 +#define USB_LANGID_German_Liechtenstein 0x1407 +#define USB_LANGID_Greek 0x0408 +#define USB_LANGID_Gujarati 0x0447 +#define USB_LANGID_Hebrew 0x040d +#define USB_LANGID_Hindi 0x0439 +#define USB_LANGID_Hungarian 0x040e +#define USB_LANGID_Icelandic 0x040f +#define USB_LANGID_Indonesian 0x0421 +#define USB_LANGID_Italian_Standard 0x0410 +#define USB_LANGID_Italian_Switzerland 0x0810 +#define USB_LANGID_Japanese 0x0411 +#define USB_LANGID_Kannada 0x044b +#define USB_LANGID_Kashmiri_India 0x0860 +#define USB_LANGID_Kazakh 0x043f +#define USB_LANGID_Konkani 0x0457 +#define USB_LANGID_Korean 0x0412 +#define USB_LANGID_Korean_Johab 0x0812 +#define USB_LANGID_Latvian 0x0426 +#define USB_LANGID_Lithuanian 0x0427 +#define USB_LANGID_Lithuanian_Classic 0x0827 +#define USB_LANGID_Macedonian 0x042f +#define USB_LANGID_Malay_Malaysian 0x043e +#define USB_LANGID_Malay_Brunei_Darussalam 0x083e +#define USB_LANGID_Malayalam 0x044c +#define USB_LANGID_Manipuri 0x0458 +#define USB_LANGID_Marathi 0x044e +#define USB_LANGID_Nepali_India 0x0861 +#define USB_LANGID_Norwegian_Bokmal 0x0414 +#define USB_LANGID_Norwegian_Nynorsk 0x0814 +#define USB_LANGID_Oriya 0x0448 +#define USB_LANGID_Polish 0x0415 +#define USB_LANGID_Portuguese_Brazil 0x0416 +#define USB_LANGID_Portuguese_Standard 0x0816 +#define USB_LANGID_Punjabi 0x0446 +#define USB_LANGID_Romanian 0x0418 +#define USB_LANGID_Russian 0x0419 +#define USB_LANGID_Sanskrit 0x044f +#define USB_LANGID_Serbian_Cyrillic 0x0c1a +#define USB_LANGID_Serbian_Latin 0x081a +#define USB_LANGID_Sindhi 0x0459 +#define USB_LANGID_Slovak 0x041b +#define USB_LANGID_Slovenian 0x0424 +#define USB_LANGID_Spanish_Traditional_Sort 0x040a +#define USB_LANGID_Spanish_Mexican 0x080a +#define USB_LANGID_Spanish_Modern_Sort 0x0c0a +#define USB_LANGID_Spanish_Guatemala 0x100a +#define USB_LANGID_Spanish_Costa_Rica 0x140a +#define USB_LANGID_Spanish_Panama 0x180a +#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a +#define USB_LANGID_Spanish_Venezuela 0x200a +#define USB_LANGID_Spanish_Colombia 0x240a +#define USB_LANGID_Spanish_Peru 0x280a +#define USB_LANGID_Spanish_Argentina 0x2c0a +#define USB_LANGID_Spanish_Ecuador 0x300a +#define USB_LANGID_Spanish_Chile 0x340a +#define USB_LANGID_Spanish_Uruguay 0x380a +#define USB_LANGID_Spanish_Paraguay 0x3c0a +#define USB_LANGID_Spanish_Bolivia 0x400a +#define USB_LANGID_Spanish_El_Salvador 0x440a +#define USB_LANGID_Spanish_Honduras 0x480a +#define USB_LANGID_Spanish_Nicaragua 0x4c0a +#define USB_LANGID_Spanish_Puerto_Rico 0x500a +#define USB_LANGID_Sutu 0x0430 +#define USB_LANGID_Swahili_Kenya 0x0441 +#define USB_LANGID_Swedish 0x041d +#define USB_LANGID_Swedish_Finland 0x081d +#define USB_LANGID_Tamil 0x0449 +#define USB_LANGID_Tatar_Tatarstan 0x0444 +#define USB_LANGID_Telugu 0x044a +#define USB_LANGID_Thai 0x041e +#define USB_LANGID_Turkish 0x041f +#define USB_LANGID_Ukrainian 0x0422 +#define USB_LANGID_Urdu_Pakistan 0x0420 +#define USB_LANGID_Urdu_India 0x0820 +#define USB_LANGID_Uzbek_Latin 0x0443 +#define USB_LANGID_Uzbek_Cyrillic 0x0843 +#define USB_LANGID_Vietnamese 0x042a +#define USB_LANGID_HID_UDD 0x04ff +#define USB_LANGID_HID_Vendor1 0xf0ff +#define USB_LANGID_HID_Vendor2 0xf4ff +#define USB_LANGID_HID_Vendor3 0xf8ff +#define USB_LANGID_HID_Vendor4 0xfcff + +#endif /* USB_STACK_H */ diff --git a/Firmware/dp_usb/usb_stack_globals.h b/Firmware/dp_usb/usb_stack_globals.h index d0feb869..b59a1c28 100644 --- a/Firmware/dp_usb/usb_stack_globals.h +++ b/Firmware/dp_usb/usb_stack_globals.h @@ -1,16 +1,16 @@ -#ifndef GLOBALS_H -#define GLOBALS_H - -// JTR V0.2a 26th Jan 2012 - -typedef unsigned char BYTE; - -// =================== GLOBAL HEADERS -#include "../prj_usb_config.h" // from parent folder. -#include "usb_stack.h" -#include "cdc.h" - -#include - - -#endif +#ifndef GLOBALS_H +#define GLOBALS_H + +// JTR V0.2a 26th Jan 2012 + +typedef unsigned char BYTE; + +// =================== GLOBAL HEADERS +#include "../prj_usb_config.h" // from parent folder. +#include "usb_stack.h" +#include "cdc.h" + +#include + + +#endif diff --git a/Firmware/globals.h b/Firmware/globals.h index 8d4bc87c..57bc4563 100644 --- a/Firmware/globals.h +++ b/Firmware/globals.h @@ -1,22 +1,22 @@ -#ifndef GLOBALS_H -#define GLOBALS_H - -// =================== GLOBAL TYPEDEFS -/*typedef unsigned char u8; -typedef unsigned int u16; -typedef unsigned long u32; -typedef unsigned char BYTE; -*/ -#define INVALID 0xFF - -#define FALSE 0 -#define TRUE (!FALSE) - -// =================== GLOBAL HEADERS -#include -#include -#include - -#include "base.h" - -#endif +#ifndef GLOBALS_H +#define GLOBALS_H + +// =================== GLOBAL TYPEDEFS +/*typedef unsigned char u8; +typedef unsigned int u16; +typedef unsigned long u32; +typedef unsigned char BYTE; +*/ +#define INVALID 0xFF + +#define FALSE 0 +#define TRUE (!FALSE) + +// =================== GLOBAL HEADERS +#include +#include +#include + +#include "base.h" + +#endif diff --git a/Firmware/hardwarev1a.h b/Firmware/hardwarev1a.h index 651eac75..cfe3066a 100644 --- a/Firmware/hardwarev1a.h +++ b/Firmware/hardwarev1a.h @@ -1,158 +1,158 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the second, surface mount Bus Pirate. -//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface - -#define BP_VERSION_STRING "Bus Pirate v1a" -#define BP_VERSION "v1a" - -#define NUM_OF_SUMP_CHANNELS 5 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -//when we move to a USB PIC, we'll need to do that.... -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 3 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB -#define ALLIO (MOSI+CLK+MISO+CS) - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_PIN6 PORTBbits.RB11 -#define BP_LEDMODE PORTBbits.RB4 -#define BP_LEDPWR PORTAbits.RA3 -#define BP_EN50V PORTAbits.RA4 -#define BP_EN33V PORTBbits.RB5 -#define BP_VREGEN BP_EN33V //duplicate so it's compatiable wtih v2 code in procMenu.c -#define BP_PULLUP BP_EN33V //duplicate so it's compatiable wtih v2 code in procMenu.c - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_PIN6_DIR TRISBbits.TRISB11 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_LEDPWR_DIR TRISAbits.TRISA3 -#define BP_EN50V_DIR TRISAbits.TRISA4 -#define BP_EN33V_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//Change notice assignment -#define BP_MOSI_CN CNEN2bits.CN21IE -#define BP_CLK_CN CNEN2bits.CN22IE - -//ADC assignment -#define BP_ADC_PROBE 12 -#define BP_ADC_3V3 10 -#define BP_ADC_5V0 9 -#define BP_ADC_VPU 11 - -//calculate the acceptable range of voltages for the power supplies -//these values will be compared to the ADC reading from the supply monitor -#define V5BASE 0x307 //(((5/2)/3.3)*1024)) -#define V5H V5BASE+0x50 -#define V5L V5BASE-0x50 - -#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) -#define V33H V33BASE+0x50 -#define V33L V33BASE-0x50 - -#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON -#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF - -//ADC pin configuration -#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG12 //B12/AN12/ADC1/EXT -#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG10 //B14/AN10/ADC3/33V -#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG9 //B15/AN9/ADC4/50V -#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG11 //B13/AN11/ADC2/Vextpullup - -#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0 - - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 -#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 - -#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 -#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 -#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define BL_ADDR_VER 0xABFA //location of the bootloader version info -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 - -#define PIC_REV_A3 0x3003 -#define PIC_REV_B4 0x3042 -#define PIC_REV_B5 0x3043 -#define PIC_REV_B8 0x3046 - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the second, surface mount Bus Pirate. +//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface + +#define BP_VERSION_STRING "Bus Pirate v1a" +#define BP_VERSION "v1a" + +#define NUM_OF_SUMP_CHANNELS 5 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +//when we move to a USB PIC, we'll need to do that.... +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 3 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB +#define ALLIO (MOSI+CLK+MISO+CS) + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_PIN6 PORTBbits.RB11 +#define BP_LEDMODE PORTBbits.RB4 +#define BP_LEDPWR PORTAbits.RA3 +#define BP_EN50V PORTAbits.RA4 +#define BP_EN33V PORTBbits.RB5 +#define BP_VREGEN BP_EN33V //duplicate so it's compatiable wtih v2 code in procMenu.c +#define BP_PULLUP BP_EN33V //duplicate so it's compatiable wtih v2 code in procMenu.c + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_PIN6_DIR TRISBbits.TRISB11 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_LEDPWR_DIR TRISAbits.TRISA3 +#define BP_EN50V_DIR TRISAbits.TRISA4 +#define BP_EN33V_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//Change notice assignment +#define BP_MOSI_CN CNEN2bits.CN21IE +#define BP_CLK_CN CNEN2bits.CN22IE + +//ADC assignment +#define BP_ADC_PROBE 12 +#define BP_ADC_3V3 10 +#define BP_ADC_5V0 9 +#define BP_ADC_VPU 11 + +//calculate the acceptable range of voltages for the power supplies +//these values will be compared to the ADC reading from the supply monitor +#define V5BASE 0x307 //(((5/2)/3.3)*1024)) +#define V5H V5BASE+0x50 +#define V5L V5BASE-0x50 + +#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) +#define V33H V33BASE+0x50 +#define V33L V33BASE-0x50 + +#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON +#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF + +//ADC pin configuration +#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG12 //B12/AN12/ADC1/EXT +#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG10 //B14/AN10/ADC3/33V +#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG9 //B15/AN9/ADC4/50V +#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG11 //B13/AN11/ADC2/Vextpullup + +#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0 + + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 +#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 + +#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 +#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 +#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define BL_ADDR_VER 0xABFA //location of the bootloader version info +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 + +#define PIC_REV_A3 0x3003 +#define PIC_REV_B4 0x3042 +#define PIC_REV_B5 0x3043 +#define PIC_REV_B8 0x3046 + diff --git a/Firmware/hardwarev3.h b/Firmware/hardwarev3.h index 57a1b274..d47da946 100644 --- a/Firmware/hardwarev3.h +++ b/Firmware/hardwarev3.h @@ -1,173 +1,173 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com -#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" -#define BP_VERSION "v3" - -#define NUM_OF_SUMP_CHANNELS 5 - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB0 -#define BP_POST_DIR TRISBbits.TRISB0 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB -#define ALLIO (MOSI+CLK+MISO+CS) - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX0 PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a -#define BP_PGD PORTBbits.RB0 //PGD pin on programming header - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX0_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 -#define BP_PGD_DIR TRISBbits.TRISB0 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX0_ODC ODCBbits.ODB10 - -//Change notice assignment -#define BP_MOSI_CN CNEN2bits.CN21IE -#define BP_CLK_CN CNEN2bits.CN22IE - -//ADC assignment -#define BP_ADC_PROBE 12 -#define BP_ADC_3V3 10 -#define BP_ADC_5V0 9 -#define BP_ADC_VPU 11 - -//calculate the acceptable range of voltages for the power supplies -//these values will be compared to the ADC reading from the supply monitor -#define V5BASE 0x307 //(((5/2)/3.3)*1024)) -#define V5H V5BASE+0x50 -#define V5L V5BASE-0x50 - -#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) -#define V33H V33BASE+0x50 -#define V33L V33BASE-0x50 - -#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON -#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF - -//ADC pin configuration -#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG12 //B12/AN12/ADC1/EXT -#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG10 //B14/AN10/ADC3/33V -#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG9 //B15/AN9/ADC4/50V -#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG11 //B13/AN11/ADC2/Vextpullup - -#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0 - -//flow control connections for FTDI chip -//used in uart brige -#define FTDI_RTS_DIR TRISAbits.TRISA4 -#define FTDI_RTS PORTAbits.RA4 -#define FTDI_CTS_DIR TRISAbits.TRISA3 -#define FTDI_CTS PORTAbits.RA3 - -//defines the PPS input/output for MISO and MOSI pins -#define BP_MISO_RPIN 7 -#define BP_MISO_RPOUT RPOR3bits.RP7R -#define BP_MOSI_RPIN 9 -#define BP_MOSI_RPOUT RPOR4bits.RP9R - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//define the PPS input/output for CLK pin -#define BP_CLK_RPIN 8 -#define BP_CLK_RPOUT RPOR4bits.RP8R - -//pseudofunctions for pullup resistors -//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -#define BP_MODELED_ON() LATA|=0b10 -#define BP_MODELED_OFF()LATA&=(~0b10) - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define BL_ADDR_VER 0xABFA //location of the bootloader version info -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 - -#define PIC_REV_A3 0x3003 -#define PIC_REV_B4 0x3042 -#define PIC_REV_B5 0x3043 -#define PIC_REV_B8 0x3046 - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com +#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" +#define BP_VERSION "v3" + +#define NUM_OF_SUMP_CHANNELS 5 + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB0 +#define BP_POST_DIR TRISBbits.TRISB0 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB +#define ALLIO (MOSI+CLK+MISO+CS) + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX0 PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a +#define BP_PGD PORTBbits.RB0 //PGD pin on programming header + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX0_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 +#define BP_PGD_DIR TRISBbits.TRISB0 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX0_ODC ODCBbits.ODB10 + +//Change notice assignment +#define BP_MOSI_CN CNEN2bits.CN21IE +#define BP_CLK_CN CNEN2bits.CN22IE + +//ADC assignment +#define BP_ADC_PROBE 12 +#define BP_ADC_3V3 10 +#define BP_ADC_5V0 9 +#define BP_ADC_VPU 11 + +//calculate the acceptable range of voltages for the power supplies +//these values will be compared to the ADC reading from the supply monitor +#define V5BASE 0x307 //(((5/2)/3.3)*1024)) +#define V5H V5BASE+0x50 +#define V5L V5BASE-0x50 + +#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) +#define V33H V33BASE+0x50 +#define V33L V33BASE-0x50 + +#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON +#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF + +//ADC pin configuration +#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG12 //B12/AN12/ADC1/EXT +#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG10 //B14/AN10/ADC3/33V +#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG9 //B15/AN9/ADC4/50V +#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG11 //B13/AN11/ADC2/Vextpullup + +#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0 + +//flow control connections for FTDI chip +//used in uart brige +#define FTDI_RTS_DIR TRISAbits.TRISA4 +#define FTDI_RTS PORTAbits.RA4 +#define FTDI_CTS_DIR TRISAbits.TRISA3 +#define FTDI_CTS PORTAbits.RA3 + +//defines the PPS input/output for MISO and MOSI pins +#define BP_MISO_RPIN 7 +#define BP_MISO_RPOUT RPOR3bits.RP7R +#define BP_MOSI_RPIN 9 +#define BP_MOSI_RPOUT RPOR4bits.RP9R + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//define the PPS input/output for CLK pin +#define BP_CLK_RPIN 8 +#define BP_CLK_RPOUT RPOR4bits.RP8R + +//pseudofunctions for pullup resistors +//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +#define BP_MODELED_ON() LATA|=0b10 +#define BP_MODELED_OFF()LATA&=(~0b10) + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define BL_ADDR_VER 0xABFA //location of the bootloader version info +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 + +#define PIC_REV_A3 0x3003 +#define PIC_REV_B4 0x3042 +#define PIC_REV_B5 0x3043 +#define PIC_REV_B8 0x3046 + diff --git a/Firmware/hardwarev4a.h b/Firmware/hardwarev4a.h index 8dec256b..81d9fce2 100644 --- a/Firmware/hardwarev4a.h +++ b/Firmware/hardwarev4a.h @@ -1,279 +1,279 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//This profile is for the Bus Pirate v4 hardware only. -#define BP_VERSION_STRING "Bus Pirate v4"// (Seeed Studio)" -#define BP_VERSION "v4" - -// If you want to route AUX1 to channel 7 of the JTR SUMP mode in firmware -// then uncomment this line. Otherwise you can "GREEN WIRE" pins 42 to 55 -// on the PIC@$FJ256GB106 or just have 6 channels that do not include AUX1 - -#define BPv4_SUMP_SOFT_WIRE -#define NUM_OF_SUMP_CHANNELS 8 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 11 - - -//this is a different type of pin identifier used by the bitbang library -#define AUX2 0b1 //B0 -#define MOSI 0b10 //B1 -#define CLK 0b100 //B2 -#define MISO 0b1000 //B3 -#define CS 0b10000 //B4 -#define AUX0 0b100000 //B5 -#define SUMP_SPARE6 0b1000000 //B6 -#define SUMP_SPARE7 0b10000000 //B7 -#define AUX1 0b100000000 //B8 - -#define IOPOR PORTD -#define IOLAT LATD -#define IODIR TRISD -#define ALLIO (MOSI+CLK+MISO+CS) -#define AUX AUX0 - -//these macros set the pin IO used in each module -#define BP_MOSI PORTDbits.RD1 -#define BP_CLK PORTDbits.RD2 -#define BP_MISO PORTDbits.RD3 -#define BP_CS PORTDbits.RD4 -#define BP_AUX0 PORTDbits.RD5 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTBbits.RB8 //single MODE led on v2a -#define BP_VREGEN PORTBbits.RB9 //single vreg enable pin on v2a -#define BP_PULLUP PORTEbits.RE4 //Pull-up pin on V2a -#define BP_PGD PORTBbits.RB7 //PGD pin on programming header - -//new in v4 -#define BP_LEDUSB PORTBbits.RB10 -#define BP_BUTTON PORTCbits.RC14 -#define BP_AUX1 PORTDbits.RD8 -#define BP_AUX2 PORTDbits.RD0 -#define BP_EE_SDA PORTDbits.RD9 -#define BP_EE_SCL PORTDbits.RD10 -#define BP_EE_WP PORTDbits.RD11 -#define BP_PUVSEL33 PORTFbits.RF4 -#define BP_PUVSEL50 PORTFbits.RF5 -/* -//these macros set the pin IO used in each module -//#define BP_MOSI PORTDbits.RD1 -//#define BP_CLK PORTDbits.RD2 -//#define BP_MISO PORTDbits.RD3 -//#define BP_CS PORTDbits.RD4 -#define BP_AUX_LAT LATDbits.LATD5 //moved pin5 (AUX) to RB10 -//#define BP_LEDMODE PORTBbits.RB8 //single MODE led on v2a -//#define BP_VREGEN PORTBbits.RB9 //single vreg enable pin on v2a -//#define BP_PULLUP PORTEbits.RE4 //Pull-up pin on V2a -//#define BP_PGD PORTBbits.RB7 //PGD pin on programming header - -//new in v4 -//#define BP_LEDUSB PORTBbits.RB10 -//#define BP_BUTTON PORTCbits.RC14 -//#define BP_AUX1 PORTFbits.RF5 -//#define BP_AUX2 PORTFbits.RF4 -//#define BP_EE_SDA PORTDbits.RD9 -//#define BP_EE_SCL PORTDbits.RD10 -//#define BP_EE_WP PORTDbits.RD11 -#define BP_PUVSEL33_LAT LATBbits.LATB3 -#define BP_PUVSEL50_LAT LATBbits.LATB4 - */ -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISDbits.TRISD1 -#define BP_CLK_DIR TRISDbits.TRISD2 -#define BP_MISO_DIR TRISDbits.TRISD3 -#define BP_CS_DIR TRISDbits.TRISD4 -#define BP_AUX0_DIR TRISDbits.TRISD5 -#define BP_LEDMODE_DIR TRISBbits.TRISB8 -#define BP_VREGEN_DIR TRISBbits.TRISB9 -#define BP_PULLUP_DIR TRISEbits.TRISE4 -#define BP_PGD_DIR TRISBbits.TRISB7 - -//new in v4 -#define BP_LEDUSB_DIR TRISBbits.TRISB10 -#define BP_BUTTON_DIR TRISCbits.TRISC14 -#define BP_AUX1_DIR TRISDbits.TRISD8 -#define BP_AUX2_DIR TRISDbits.TRISD0 -#define BP_EE_SDA_DIR TRISDbits.TRISD9 -#define BP_EE_SCL_DIR TRISDbits.TRISD10 -#define BP_EE_WP_DIR TRISDbits.TRISD11 -#define BP_PUVSEL33_DIR TRISFbits.TRISF4 -#define BP_PUVSEL50_DIR TRISFbits.TRISF5 - - -//secondary MISO/CLK/MOSI connection setup (input, low) -#define BPV4_HWI2CPINS_SETUP() TRISE|=0b11100000; LATE&=(~0b11100000) - -//////////// On-Board EEPROM (OBE) Definitions and Functions -// OBE Hardware Setup -#define BP_EEPROM_SETUP() BP_EE_SDA_DIR=1; BP_EE_SCL_DIR=1;BP_EE_WP_DIR=0;BP_EE_SCL=0; BP_EE_SDA=0; BP_EE_WP=1 //EEPROM pins to input, write protect on -// OBE Write Protection -#define BP_EEPROM_WP_ON() BP_EE_WP=0 -#define BP_EEPROM_WP_OFF() BP_EE_WP=1 -#define BP_EEPROM_WR_GET() BP_EE_WP -// OBE Addressing -#define BP_EEPROM_ID 0xA0 -#define BP_EEPROM_ID_W 0xA0 -#define BP_EEPROM_ID_R 0xA1 -// OBE Size -#define BP_EEPROM_MIN 1 -#define BP_EEPROM_MAX 8192 - -// Hardware 'NORMAL' button on BPv4 definitions -#define BP_EEPROM_SETUP() BP_EE_SDA_DIR=1; BP_EE_SCL_DIR=1;BP_EE_WP_DIR=0;BP_EE_SCL=0; BP_EE_SDA=0; BP_EE_WP=1 //EEPROM pins to input, write protect on - -#define BP_BUTTON_IF IFS1bits.CNIF -#define BP_BUTTON_SETUP() BP_BUTTON_DIR=1; CNPU1|=0b1; CNEN1|=0b1; IEC1bits.CNIE=0; BP_BUTTON_IF=0; -#define BP_BUTTON_ISDOWN() (!BP_BUTTON) //0=DOWN (PRESSED) / 1=UP (UNPRESSED) - -//pullup voltage enable/disable -//always disables the other pullup -#define BP_3V3PU_ON() BP_PUVSEL50_DIR=1; BP_PUVSEL33=0; BP_PUVSEL33_DIR=0 -#define BP_3V3PU_OFF() BP_PUVSEL50_DIR=1; BP_PUVSEL33_DIR=1 - -#define BP_5VPU_ON() BP_PUVSEL33_DIR=1; BP_PUVSEL50=0; BP_PUVSEL50_DIR=0 -#define BP_5VPU_OFF() BP_PUVSEL33_DIR=1; BP_PUVSEL50_DIR=1 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCDbits.ODD1 -#define BP_CLK_ODC ODCDbits.ODD2 -#define BP_MISO_ODC ODCDbits.ODD3 -#define BP_CS_ODC ODCDbits.ODD4 -#define BP_AUX_ODC ODCDbits.ODD5 - -//Change notice assignment -#define BP_MOSI_CN CNEN4bits.CN50IE -#define BP_CLK_CN CNEN4bits.CN51IE - -//ADC assignment -#define BP_ADC_PROBE 1 -#define BP_ADC_3V3 0 -#define BP_ADC_5V0 2 -#define BP_ADC_VPU 5 -#define BP_ADC_USB 15 - -//calculate the acceptable range of voltages for the power supplies -//these values will be compared to the ADC reading from the supply monitor -#define V5BASE 0x307 //(((5/2)/3.3)*1024)) -#define V5H V5BASE+0x50 -#define V5L V5BASE-0x50 - -#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) -#define V33H V33BASE+0x50 -#define V33L V33BASE-0x50 - -#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON -#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF - -//ADC pin configuration -#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG2 //AN1/ADC1/EXT -#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG1 //AN0/ADC3/33V -#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG0 //AN2/ADC4/50V -#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG5 //AN5/ADC2/Vextpullup -#define BP_ADC_USB_CFG AD1PCFGbits.PCFG15 //AN15/ADC5/Vusb - -#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0;BP_ADC_USB_CFG=0 - -//error fixing defines (find with keyword #BPV4 -#define OC5CON OC5CON1 - - -//////////// BPv4 Peripheral Pin Select (PPS) Definitions -// Input/Output PPS Definitions -// MISO / MOSI / CS / CLK -#define BP_MISO_RPIN 22 -#define BP_MISO_RPOUT RPOR11bits.RP22R -#define BP_MOSI_RPIN 24 -#define BP_MOSI_RPOUT RPOR12bits.RP24R -#define BP_CS_RPIN 25 -#define BP_CS_RPOUT RPOR12bits.RP25R -#define BP_CLK_RPIN 23 -#define BP_CLK_RPOUT RPOR11bits.RP23R -// AUX0:2 (AUX0 = AUX / AUX = AUX0) -#define BP_AUX0_RPIN 20 -#define BP_AUX0_RPOUT RPOR10bits.RP20R -#define BP_AUX1_RPIN 2 -#define BP_AUX1_RPOUT RPOR1bits.RP2R -#define BP_AUX2_RPIN 11 -#define BP_AUX2_RPOUT RPOR5bits.RP11R -// OTHER -#define BP_ADC_RPIN 1 -#define BP_ADC_RPOUT RPOR1bits.RP1R - -// Standard Output (PPS) Definitions -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 - -// PPS Compatability -#define BP_AUX_RPIN BP_AUX0_RPIN -#define BP_AUX_RPOUT BP_AUX0_RPOUT - -//pseudofunctions for pullup resistors -//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//pseudofunctions for USB, MODE LEDs -#define BP_USBLED_ON() TRISB&=(~0x400);LATB|=0x400 -#define BP_USBLED_OFF() TRISB&=(~0x400);LATB&=(~0x400) -#define BP_USBLED_T() TRISB&=(~0x400);LATB^=0x400 -#define BP_MODELED_ON() LATB|=0x100 -#define BP_MODELED_OFF() LATB&=(~0x100) - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0001 //should be 0x0001, but it crashes... -#define BL_ADDR_VER 0xABFA //location of the bootloader version info -#define CFG_ADDR_0 0x57FA -#define CFG_ADDR_1 0x57FC -#define CFG_ADDR_2 0x57FE - -//these are unneeded, but the stack complains if they're not defined -//#define USE_SELF_POWER_SENSE_IO -#define tris_self_power TRISCbits.TRISC2 -#define self_power 1 - -//#define USE_USB_BUS_SENSE_IO -#define tris_usb_bus_sense TRISCbits.TRISC2 -#define USB_BUS_SENSE U1OTGSTATbits.SESVD - -#define PIC_REV_A3 0x0001 -#define PIC_REV_A5 0x0003 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//This profile is for the Bus Pirate v4 hardware only. +#define BP_VERSION_STRING "Bus Pirate v4"// (Seeed Studio)" +#define BP_VERSION "v4" + +// If you want to route AUX1 to channel 7 of the JTR SUMP mode in firmware +// then uncomment this line. Otherwise you can "GREEN WIRE" pins 42 to 55 +// on the PIC@$FJ256GB106 or just have 6 channels that do not include AUX1 + +#define BPv4_SUMP_SOFT_WIRE +#define NUM_OF_SUMP_CHANNELS 8 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 11 + + +//this is a different type of pin identifier used by the bitbang library +#define AUX2 0b1 //B0 +#define MOSI 0b10 //B1 +#define CLK 0b100 //B2 +#define MISO 0b1000 //B3 +#define CS 0b10000 //B4 +#define AUX0 0b100000 //B5 +#define SUMP_SPARE6 0b1000000 //B6 +#define SUMP_SPARE7 0b10000000 //B7 +#define AUX1 0b100000000 //B8 + +#define IOPOR PORTD +#define IOLAT LATD +#define IODIR TRISD +#define ALLIO (MOSI+CLK+MISO+CS) +#define AUX AUX0 + +//these macros set the pin IO used in each module +#define BP_MOSI PORTDbits.RD1 +#define BP_CLK PORTDbits.RD2 +#define BP_MISO PORTDbits.RD3 +#define BP_CS PORTDbits.RD4 +#define BP_AUX0 PORTDbits.RD5 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTBbits.RB8 //single MODE led on v2a +#define BP_VREGEN PORTBbits.RB9 //single vreg enable pin on v2a +#define BP_PULLUP PORTEbits.RE4 //Pull-up pin on V2a +#define BP_PGD PORTBbits.RB7 //PGD pin on programming header + +//new in v4 +#define BP_LEDUSB PORTBbits.RB10 +#define BP_BUTTON PORTCbits.RC14 +#define BP_AUX1 PORTDbits.RD8 +#define BP_AUX2 PORTDbits.RD0 +#define BP_EE_SDA PORTDbits.RD9 +#define BP_EE_SCL PORTDbits.RD10 +#define BP_EE_WP PORTDbits.RD11 +#define BP_PUVSEL33 PORTFbits.RF4 +#define BP_PUVSEL50 PORTFbits.RF5 +/* +//these macros set the pin IO used in each module +//#define BP_MOSI PORTDbits.RD1 +//#define BP_CLK PORTDbits.RD2 +//#define BP_MISO PORTDbits.RD3 +//#define BP_CS PORTDbits.RD4 +#define BP_AUX_LAT LATDbits.LATD5 //moved pin5 (AUX) to RB10 +//#define BP_LEDMODE PORTBbits.RB8 //single MODE led on v2a +//#define BP_VREGEN PORTBbits.RB9 //single vreg enable pin on v2a +//#define BP_PULLUP PORTEbits.RE4 //Pull-up pin on V2a +//#define BP_PGD PORTBbits.RB7 //PGD pin on programming header + +//new in v4 +//#define BP_LEDUSB PORTBbits.RB10 +//#define BP_BUTTON PORTCbits.RC14 +//#define BP_AUX1 PORTFbits.RF5 +//#define BP_AUX2 PORTFbits.RF4 +//#define BP_EE_SDA PORTDbits.RD9 +//#define BP_EE_SCL PORTDbits.RD10 +//#define BP_EE_WP PORTDbits.RD11 +#define BP_PUVSEL33_LAT LATBbits.LATB3 +#define BP_PUVSEL50_LAT LATBbits.LATB4 + */ +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISDbits.TRISD1 +#define BP_CLK_DIR TRISDbits.TRISD2 +#define BP_MISO_DIR TRISDbits.TRISD3 +#define BP_CS_DIR TRISDbits.TRISD4 +#define BP_AUX0_DIR TRISDbits.TRISD5 +#define BP_LEDMODE_DIR TRISBbits.TRISB8 +#define BP_VREGEN_DIR TRISBbits.TRISB9 +#define BP_PULLUP_DIR TRISEbits.TRISE4 +#define BP_PGD_DIR TRISBbits.TRISB7 + +//new in v4 +#define BP_LEDUSB_DIR TRISBbits.TRISB10 +#define BP_BUTTON_DIR TRISCbits.TRISC14 +#define BP_AUX1_DIR TRISDbits.TRISD8 +#define BP_AUX2_DIR TRISDbits.TRISD0 +#define BP_EE_SDA_DIR TRISDbits.TRISD9 +#define BP_EE_SCL_DIR TRISDbits.TRISD10 +#define BP_EE_WP_DIR TRISDbits.TRISD11 +#define BP_PUVSEL33_DIR TRISFbits.TRISF4 +#define BP_PUVSEL50_DIR TRISFbits.TRISF5 + + +//secondary MISO/CLK/MOSI connection setup (input, low) +#define BPV4_HWI2CPINS_SETUP() TRISE|=0b11100000; LATE&=(~0b11100000) + +//////////// On-Board EEPROM (OBE) Definitions and Functions +// OBE Hardware Setup +#define BP_EEPROM_SETUP() BP_EE_SDA_DIR=1; BP_EE_SCL_DIR=1;BP_EE_WP_DIR=0;BP_EE_SCL=0; BP_EE_SDA=0; BP_EE_WP=1 //EEPROM pins to input, write protect on +// OBE Write Protection +#define BP_EEPROM_WP_ON() BP_EE_WP=0 +#define BP_EEPROM_WP_OFF() BP_EE_WP=1 +#define BP_EEPROM_WR_GET() BP_EE_WP +// OBE Addressing +#define BP_EEPROM_ID 0xA0 +#define BP_EEPROM_ID_W 0xA0 +#define BP_EEPROM_ID_R 0xA1 +// OBE Size +#define BP_EEPROM_MIN 1 +#define BP_EEPROM_MAX 8192 + +// Hardware 'NORMAL' button on BPv4 definitions +#define BP_EEPROM_SETUP() BP_EE_SDA_DIR=1; BP_EE_SCL_DIR=1;BP_EE_WP_DIR=0;BP_EE_SCL=0; BP_EE_SDA=0; BP_EE_WP=1 //EEPROM pins to input, write protect on + +#define BP_BUTTON_IF IFS1bits.CNIF +#define BP_BUTTON_SETUP() BP_BUTTON_DIR=1; CNPU1|=0b1; CNEN1|=0b1; IEC1bits.CNIE=0; BP_BUTTON_IF=0; +#define BP_BUTTON_ISDOWN() (!BP_BUTTON) //0=DOWN (PRESSED) / 1=UP (UNPRESSED) + +//pullup voltage enable/disable +//always disables the other pullup +#define BP_3V3PU_ON() BP_PUVSEL50_DIR=1; BP_PUVSEL33=0; BP_PUVSEL33_DIR=0 +#define BP_3V3PU_OFF() BP_PUVSEL50_DIR=1; BP_PUVSEL33_DIR=1 + +#define BP_5VPU_ON() BP_PUVSEL33_DIR=1; BP_PUVSEL50=0; BP_PUVSEL50_DIR=0 +#define BP_5VPU_OFF() BP_PUVSEL33_DIR=1; BP_PUVSEL50_DIR=1 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCDbits.ODD1 +#define BP_CLK_ODC ODCDbits.ODD2 +#define BP_MISO_ODC ODCDbits.ODD3 +#define BP_CS_ODC ODCDbits.ODD4 +#define BP_AUX_ODC ODCDbits.ODD5 + +//Change notice assignment +#define BP_MOSI_CN CNEN4bits.CN50IE +#define BP_CLK_CN CNEN4bits.CN51IE + +//ADC assignment +#define BP_ADC_PROBE 1 +#define BP_ADC_3V3 0 +#define BP_ADC_5V0 2 +#define BP_ADC_VPU 5 +#define BP_ADC_USB 15 + +//calculate the acceptable range of voltages for the power supplies +//these values will be compared to the ADC reading from the supply monitor +#define V5BASE 0x307 //(((5/2)/3.3)*1024)) +#define V5H V5BASE+0x50 +#define V5L V5BASE-0x50 + +#define V33BASE 0x200 //(((3.3/2)/3.3)*1024)) +#define V33H V33BASE+0x50 +#define V33L V33BASE-0x50 + +#define ADCON() AD1CON1bits.ADON = 1 // turn ADC ON +#define ADCOFF() AD1CON1bits.ADON = 0 // turn ADC OFF + +//ADC pin configuration +#define BP_ADC_PROBE_CFG AD1PCFGbits.PCFG2 //AN1/ADC1/EXT +#define BP_ADC_3V3_CFG AD1PCFGbits.PCFG1 //AN0/ADC3/33V +#define BP_ADC_5V0_CFG AD1PCFGbits.PCFG0 //AN2/ADC4/50V +#define BP_ADC_VPU_CFG AD1PCFGbits.PCFG5 //AN5/ADC2/Vextpullup +#define BP_ADC_USB_CFG AD1PCFGbits.PCFG15 //AN15/ADC5/Vusb + +#define BP_ADC_PINSETUP() BP_ADC_PROBE_CFG=0;BP_ADC_3V3_CFG=0;BP_ADC_5V0_CFG=0;BP_ADC_VPU_CFG=0;BP_ADC_USB_CFG=0 + +//error fixing defines (find with keyword #BPV4 +#define OC5CON OC5CON1 + + +//////////// BPv4 Peripheral Pin Select (PPS) Definitions +// Input/Output PPS Definitions +// MISO / MOSI / CS / CLK +#define BP_MISO_RPIN 22 +#define BP_MISO_RPOUT RPOR11bits.RP22R +#define BP_MOSI_RPIN 24 +#define BP_MOSI_RPOUT RPOR12bits.RP24R +#define BP_CS_RPIN 25 +#define BP_CS_RPOUT RPOR12bits.RP25R +#define BP_CLK_RPIN 23 +#define BP_CLK_RPOUT RPOR11bits.RP23R +// AUX0:2 (AUX0 = AUX / AUX = AUX0) +#define BP_AUX0_RPIN 20 +#define BP_AUX0_RPOUT RPOR10bits.RP20R +#define BP_AUX1_RPIN 2 +#define BP_AUX1_RPOUT RPOR1bits.RP2R +#define BP_AUX2_RPIN 11 +#define BP_AUX2_RPOUT RPOR5bits.RP11R +// OTHER +#define BP_ADC_RPIN 1 +#define BP_ADC_RPOUT RPOR1bits.RP1R + +// Standard Output (PPS) Definitions +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 + +// PPS Compatability +#define BP_AUX_RPIN BP_AUX0_RPIN +#define BP_AUX_RPOUT BP_AUX0_RPOUT + +//pseudofunctions for pullup resistors +//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//pseudofunctions for USB, MODE LEDs +#define BP_USBLED_ON() TRISB&=(~0x400);LATB|=0x400 +#define BP_USBLED_OFF() TRISB&=(~0x400);LATB&=(~0x400) +#define BP_USBLED_T() TRISB&=(~0x400);LATB^=0x400 +#define BP_MODELED_ON() LATB|=0x100 +#define BP_MODELED_OFF() LATB&=(~0x100) + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0001 //should be 0x0001, but it crashes... +#define BL_ADDR_VER 0xABFA //location of the bootloader version info +#define CFG_ADDR_0 0x57FA +#define CFG_ADDR_1 0x57FC +#define CFG_ADDR_2 0x57FE + +//these are unneeded, but the stack complains if they're not defined +//#define USE_SELF_POWER_SENSE_IO +#define tris_self_power TRISCbits.TRISC2 +#define self_power 1 + +//#define USE_USB_BUS_SENSE_IO +#define tris_usb_bus_sense TRISCbits.TRISC2 +#define USB_BUS_SENSE U1OTGSTATbits.SESVD + +#define PIC_REV_A3 0x0001 +#define PIC_REV_A5 0x0003 diff --git a/Firmware/jtag.c b/Firmware/jtag.c index 156303aa..aee30674 100644 --- a/Firmware/jtag.c +++ b/Firmware/jtag.c @@ -1,393 +1,393 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -#include "base.h" -#include "jtag.h" -#include "jtag/micro.h" -#include "jtag/ports.h" - -#define JTAGDATASETTLE 20 -#define JTAGCLOCK 100 - -#define RESET 0 -#define IDLE 1 -#define SHIFTIR 2 -#define SHIFTDR 3 - -//private functions -void jtagSetup(void); - -//these are JTAG state machine related -void jtagSetState(unsigned char c); -void jtagLeaveState(void); -void jtagReset(void); -void jtagCleanPendingBit(void); -//high level byte operations -unsigned char jtagWriteByte(unsigned char c); -unsigned char jtagReadByte(void); -//bit level functions -unsigned char jtagWriteBit(unsigned char c); -unsigned char jtagReadBit(void); -unsigned char jtagReadDataState(void); -//bus level control -void jtagDataLow(void); -void jtagDataHigh(void); -void jtagClockLow(void); -void jtagClockHigh(void); -void jtagClockTicks(unsigned char c); -void jtagTMSHigh(void); -void jtagTMSLow(void); - -static unsigned char jtagState=0, jtagBitPending=0, jtagDelayedBit=0;//static configuration variables for highZ and LSB - -struct _JTAG{ - unsigned char HiZ:1; -} jtagSettings; - - -void jtag(void){ - static unsigned char c, cmd; - static unsigned int i; - - jtagSetup(); - jtagSettings.HiZ=1; - - while(1){ - cmd=UART1RX(); - switch(cmd){ - //bpWline("JTAG READY"); - case 1://jtag reset - jtagReset();//reset - //bpWline("JTAGSM: RESET"); - jtagLeaveState();//move chain to idle (gives own message) - break; - case 2://read ID, chain length, # devices - //bpWline("JTAG INIT CHAIN"); - jtagReset();//reset - //bpWline("JTAGSM: RESET"); - //data high - jtagDataHigh(); - //how many devices? - //[0xffx255]{while not 1} - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTIR); //shift IR to enter data - jtagClockTicks(0xff); - jtagClockTicks(0xff); - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTDR); - i=0; - while(jtagReadBit()==0){ - i++; - if(i<250)break;//250 device timout/limit... - } - jtagLeaveState(); //clean up from previous state - //reset - jtagReset(); - //bpWline("JTAGSM: RESET"); - - //read ID#s (32 bits * devices) {r: (4*devices)} - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTDR); - //bpWline("JTAG CHAIN REPORT:"); - UART1TX(i*4); //how many bytes are we returning - for(c=0;c lenVal MAX_LEN buffer size*/ - /* Insert new errors here */ - #define XSVF_ERROR_LAST 7 - i=xsvfExecute(); - UART1TX(i); - break; - default: - break;//bpWmessage(MSG_ERROR_MACRO); - }//switch - }//while -}//function - - -//exits an existing JTAG state, moves to IDLE -void jtagLeaveState(void){ - //move to IDLE -// bpWstring("JTAGSM: "); - switch(jtagState){ - case IDLE://already in idle - //bpWline("ALREADY IDLE"); - break; - case RESET://move to idle 0 - jtagTMSLow(); - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("RESET->IDLE"); - break; - case SHIFTIR://clean up pending writes... - if(jtagBitPending==1){ - //set proper bit direction - if(jtagDelayedBit==1){ - jtagDataHigh(); - }else{ - jtagDataLow(); - } - jtagBitPending=0;//clear pending - //bpWstring("(WROTE DELAYED BIT) "); - } - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("IR->IDLE"); - break; - case SHIFTDR://both same path 110 - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("DR->IDLE"); - break; - default: - //couldn't change state, error, try resetting state machine... - //bpWline("UNKNOWN STATE"); - break; - } -} - -void jtagReset(void){ - jtagTMSHigh(); - jtagClockTicks(10);//one extra if clk starts high - jtagTMSLow();//always return to low for writes - jtagState=RESET; -} - -//moves to specified state from IDLE (reset from anywhere) -void jtagSetState(unsigned char c){ - //if(jtagState!=IDLE) bpWline("JTAGSM: WARNING OUT OF SYNC"); - //bpWstring("JTAGSM: "); - - //move to desired state - switch(c){ - case IDLE://alread idle - //bpWline("ALREADY IDLE"); - break; - case RESET://always return to reset with 11111 - jtagReset(); - //bpWline("RESET"); - break; - case SHIFTDR://100 from IDLE - jtagTMSHigh(); - jtagClockTicks(1); - jtagTMSLow();//always return to low for writes - jtagClockTicks(2); - jtagState=SHIFTDR; - //bpWline("IDLE->Data Register"); - break; - case SHIFTIR: //1100 from IDLE - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(2); - jtagState=SHIFTIR; - //bpWline("IDLE->Instruction Register (DELAYED ONE BIT FOR TMS)"); - break; - default: - //unknown state, try resetting the JTAG state machine - //bpWline("UNKNOWN STATE, TRY A RESET MACRO (1)"); - break; - } -} - -void jtagSetup(void){ - JTAGTD0_TRIS=1;//input from chain - JTAGTCK_TRIS=0; - JTAGTDI_TRIS=0; - JTAGTMS_TRIS=0; //B6 cs output is state machine control - //writes to the PORTs write to the LATCH - JTAGTDO=0; - JTAGTCK=0; - JTAGTDI=0; - JTAGTMS=0; - -} - -//this is a new write routine, untested. See old below... -unsigned char jtagWriteByte(unsigned char c){ - unsigned char i,j,a,l; - - jtagClockLow();//begin with clock low... - - //clean up any pending bits - if(jtagBitPending==1){ - jtagWriteBit(jtagDelayedBit); - jtagBitPending=0;//clear pending - //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); - } - - //if(modeConfig.lsbEN==1) l=0x01; else l=0b10000000; - l=0x01; - - for(i=0;i<8;i++){ - if( (c & l)== 0) jtagDataLow(); else jtagDataHigh();//setup the data pin - - jtagClockHigh();//set clock high - - j=JTAGTDO; - //if(modeConfig.lsbEN==1){ - c=c>>1; //-- Shift next bit into position - a=a>>1; - if(j==1)a+=0b10000000; - //}else{ - // c=c<<1; //-- Shift next bit into position - // a=a<<1; - // if(j==1)a++; - //} - - jtagClockLow();//set clock low - - //catch bit seven and delay the write until we do the next byte or exit state with TMS=1.... - if(jtagState==SHIFTIR && i==6){ - jtagBitPending=1; - if((c & l)==0) jtagDelayedBit=0; else jtagDelayedBit=1; - return a;//meaningless....rather, 1 bit short - } - } - return a; -} - -unsigned char jtagReadByte(void){ - unsigned char i,j,a; - - jtagClockLow();//begin with clock low... - - if(jtagBitPending==1){ - jtagWriteBit(jtagDelayedBit); - jtagBitPending=0;//clear pending - //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); - } - - for(i=0;i<8;i++){ - jtagClockHigh();//set clock high - j=JTAGTDO; - //if(modeConfig.lsbEN==0){ - // a=a<<1; - // if(j)a++; - //}else{ - a=a>>1; - if(j)a+=0b10000000; - //} - jtagClockLow();//set clock low - } - - return a; -} - -unsigned char jtagWriteBit(unsigned char c){ - unsigned char i; - - if(c==0){ - jtagDataLow(); - }else{ - jtagDataHigh(); - } - jtagClockHigh();//set clock high - i=JTAGTDO; - jtagClockLow();//set clock low - return i; -} - -unsigned char jtagReadBit(void){ - unsigned char i; - - jtagClockHigh();//set clock high - i=JTAGTDO; - jtagClockLow();//set clock low - return i; -} - -unsigned char jtagReadDataState(void){ - return JTAGTDO; -} - -void jtagDataHigh(void){ - JTAGTDI_TRIS=(~jtagSettings.HiZ);//set output - JTAGTDI=jtagSettings.HiZ;//data - bpDelayUS(JTAGDATASETTLE);//delay -} - -void jtagDataLow(void){ - JTAGTDI=0; //data low - JTAGTDI_TRIS=0;//set to output for HIGHZ low - bpDelayUS(JTAGDATASETTLE);//delay -} - -void jtagClockTicks(unsigned char c){ - unsigned char i; - - for(i=0;i + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +#include "base.h" +#include "jtag.h" +#include "jtag/micro.h" +#include "jtag/ports.h" + +#define JTAGDATASETTLE 20 +#define JTAGCLOCK 100 + +#define RESET 0 +#define IDLE 1 +#define SHIFTIR 2 +#define SHIFTDR 3 + +//private functions +void jtagSetup(void); + +//these are JTAG state machine related +void jtagSetState(unsigned char c); +void jtagLeaveState(void); +void jtagReset(void); +void jtagCleanPendingBit(void); +//high level byte operations +unsigned char jtagWriteByte(unsigned char c); +unsigned char jtagReadByte(void); +//bit level functions +unsigned char jtagWriteBit(unsigned char c); +unsigned char jtagReadBit(void); +unsigned char jtagReadDataState(void); +//bus level control +void jtagDataLow(void); +void jtagDataHigh(void); +void jtagClockLow(void); +void jtagClockHigh(void); +void jtagClockTicks(unsigned char c); +void jtagTMSHigh(void); +void jtagTMSLow(void); + +static unsigned char jtagState=0, jtagBitPending=0, jtagDelayedBit=0;//static configuration variables for highZ and LSB + +struct _JTAG{ + unsigned char HiZ:1; +} jtagSettings; + + +void jtag(void){ + static unsigned char c, cmd; + static unsigned int i; + + jtagSetup(); + jtagSettings.HiZ=1; + + while(1){ + cmd=UART1RX(); + switch(cmd){ + //bpWline("JTAG READY"); + case 1://jtag reset + jtagReset();//reset + //bpWline("JTAGSM: RESET"); + jtagLeaveState();//move chain to idle (gives own message) + break; + case 2://read ID, chain length, # devices + //bpWline("JTAG INIT CHAIN"); + jtagReset();//reset + //bpWline("JTAGSM: RESET"); + //data high + jtagDataHigh(); + //how many devices? + //[0xffx255]{while not 1} + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTIR); //shift IR to enter data + jtagClockTicks(0xff); + jtagClockTicks(0xff); + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTDR); + i=0; + while(jtagReadBit()==0){ + i++; + if(i<250)break;//250 device timout/limit... + } + jtagLeaveState(); //clean up from previous state + //reset + jtagReset(); + //bpWline("JTAGSM: RESET"); + + //read ID#s (32 bits * devices) {r: (4*devices)} + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTDR); + //bpWline("JTAG CHAIN REPORT:"); + UART1TX(i*4); //how many bytes are we returning + for(c=0;c lenVal MAX_LEN buffer size*/ + /* Insert new errors here */ + #define XSVF_ERROR_LAST 7 + i=xsvfExecute(); + UART1TX(i); + break; + default: + break;//bpWmessage(MSG_ERROR_MACRO); + }//switch + }//while +}//function + + +//exits an existing JTAG state, moves to IDLE +void jtagLeaveState(void){ + //move to IDLE +// bpWstring("JTAGSM: "); + switch(jtagState){ + case IDLE://already in idle + //bpWline("ALREADY IDLE"); + break; + case RESET://move to idle 0 + jtagTMSLow(); + jtagClockTicks(1); + jtagState=IDLE; + //bpWline("RESET->IDLE"); + break; + case SHIFTIR://clean up pending writes... + if(jtagBitPending==1){ + //set proper bit direction + if(jtagDelayedBit==1){ + jtagDataHigh(); + }else{ + jtagDataLow(); + } + jtagBitPending=0;//clear pending + //bpWstring("(WROTE DELAYED BIT) "); + } + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(1); + jtagState=IDLE; + //bpWline("IR->IDLE"); + break; + case SHIFTDR://both same path 110 + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(1); + jtagState=IDLE; + //bpWline("DR->IDLE"); + break; + default: + //couldn't change state, error, try resetting state machine... + //bpWline("UNKNOWN STATE"); + break; + } +} + +void jtagReset(void){ + jtagTMSHigh(); + jtagClockTicks(10);//one extra if clk starts high + jtagTMSLow();//always return to low for writes + jtagState=RESET; +} + +//moves to specified state from IDLE (reset from anywhere) +void jtagSetState(unsigned char c){ + //if(jtagState!=IDLE) bpWline("JTAGSM: WARNING OUT OF SYNC"); + //bpWstring("JTAGSM: "); + + //move to desired state + switch(c){ + case IDLE://alread idle + //bpWline("ALREADY IDLE"); + break; + case RESET://always return to reset with 11111 + jtagReset(); + //bpWline("RESET"); + break; + case SHIFTDR://100 from IDLE + jtagTMSHigh(); + jtagClockTicks(1); + jtagTMSLow();//always return to low for writes + jtagClockTicks(2); + jtagState=SHIFTDR; + //bpWline("IDLE->Data Register"); + break; + case SHIFTIR: //1100 from IDLE + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(2); + jtagState=SHIFTIR; + //bpWline("IDLE->Instruction Register (DELAYED ONE BIT FOR TMS)"); + break; + default: + //unknown state, try resetting the JTAG state machine + //bpWline("UNKNOWN STATE, TRY A RESET MACRO (1)"); + break; + } +} + +void jtagSetup(void){ + JTAGTD0_TRIS=1;//input from chain + JTAGTCK_TRIS=0; + JTAGTDI_TRIS=0; + JTAGTMS_TRIS=0; //B6 cs output is state machine control + //writes to the PORTs write to the LATCH + JTAGTDO=0; + JTAGTCK=0; + JTAGTDI=0; + JTAGTMS=0; + +} + +//this is a new write routine, untested. See old below... +unsigned char jtagWriteByte(unsigned char c){ + unsigned char i,j,a,l; + + jtagClockLow();//begin with clock low... + + //clean up any pending bits + if(jtagBitPending==1){ + jtagWriteBit(jtagDelayedBit); + jtagBitPending=0;//clear pending + //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); + } + + //if(modeConfig.lsbEN==1) l=0x01; else l=0b10000000; + l=0x01; + + for(i=0;i<8;i++){ + if( (c & l)== 0) jtagDataLow(); else jtagDataHigh();//setup the data pin + + jtagClockHigh();//set clock high + + j=JTAGTDO; + //if(modeConfig.lsbEN==1){ + c=c>>1; //-- Shift next bit into position + a=a>>1; + if(j==1)a+=0b10000000; + //}else{ + // c=c<<1; //-- Shift next bit into position + // a=a<<1; + // if(j==1)a++; + //} + + jtagClockLow();//set clock low + + //catch bit seven and delay the write until we do the next byte or exit state with TMS=1.... + if(jtagState==SHIFTIR && i==6){ + jtagBitPending=1; + if((c & l)==0) jtagDelayedBit=0; else jtagDelayedBit=1; + return a;//meaningless....rather, 1 bit short + } + } + return a; +} + +unsigned char jtagReadByte(void){ + unsigned char i,j,a; + + jtagClockLow();//begin with clock low... + + if(jtagBitPending==1){ + jtagWriteBit(jtagDelayedBit); + jtagBitPending=0;//clear pending + //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); + } + + for(i=0;i<8;i++){ + jtagClockHigh();//set clock high + j=JTAGTDO; + //if(modeConfig.lsbEN==0){ + // a=a<<1; + // if(j)a++; + //}else{ + a=a>>1; + if(j)a+=0b10000000; + //} + jtagClockLow();//set clock low + } + + return a; +} + +unsigned char jtagWriteBit(unsigned char c){ + unsigned char i; + + if(c==0){ + jtagDataLow(); + }else{ + jtagDataHigh(); + } + jtagClockHigh();//set clock high + i=JTAGTDO; + jtagClockLow();//set clock low + return i; +} + +unsigned char jtagReadBit(void){ + unsigned char i; + + jtagClockHigh();//set clock high + i=JTAGTDO; + jtagClockLow();//set clock low + return i; +} + +unsigned char jtagReadDataState(void){ + return JTAGTDO; +} + +void jtagDataHigh(void){ + JTAGTDI_TRIS=(~jtagSettings.HiZ);//set output + JTAGTDI=jtagSettings.HiZ;//data + bpDelayUS(JTAGDATASETTLE);//delay +} + +void jtagDataLow(void){ + JTAGTDI=0; //data low + JTAGTDI_TRIS=0;//set to output for HIGHZ low + bpDelayUS(JTAGDATASETTLE);//delay +} + +void jtagClockTicks(unsigned char c){ + unsigned char i; + + for(i=0;i - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//we need the pin definitions here for use in port.c of the XSVF programmer -#define JTAGTDI_TRIS BP_MOSI_DIR -#define JTAGTCK_TRIS BP_CLK_DIR -#define JTAGTD0_TRIS BP_MISO_DIR -#define JTAGTMS_TRIS BP_CS_DIR - -#define JTAGTDI BP_MOSI -#define JTAGTCK BP_CLK -#define JTAGTDO BP_MISO -#define JTAGTMS BP_CS - -void jtag(void); - - - - +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//we need the pin definitions here for use in port.c of the XSVF programmer +#define JTAGTDI_TRIS BP_MOSI_DIR +#define JTAGTCK_TRIS BP_CLK_DIR +#define JTAGTD0_TRIS BP_MISO_DIR +#define JTAGTMS_TRIS BP_CS_DIR + +#define JTAGTDI BP_MOSI +#define JTAGTCK BP_CLK +#define JTAGTDO BP_MISO +#define JTAGTMS BP_CS + +void jtag(void); + + + + diff --git a/Firmware/jtag/micro.c b/Firmware/jtag/micro.c index ccdb0bdf..da310b19 100644 --- a/Firmware/jtag/micro.c +++ b/Firmware/jtag/micro.c @@ -1617,14 +1617,14 @@ int xsvfExecute() { SXsvfInfo xsvfInfo; - xsvfInitialize( &xsvfInfo ); + xsvfInitialize( &xsvfInfo ); while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) ) { xsvfRun( &xsvfInfo ); } - xsvfCleanup( &xsvfInfo ); + xsvfCleanup( &xsvfInfo ); return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) ); } diff --git a/Firmware/main.c b/Firmware/main.c index 6a3ede67..e61946a1 100644 --- a/Firmware/main.c +++ b/Firmware/main.c @@ -1,245 +1,245 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" - /////////////////////////////////////////// - /////////////[ NOTE ]////////////////////////////////////// - /// The settings and configuration are on [base.h] please open that file if you are new to - /// compiling the Bus Pirate Firmware. - /// - /// DangerousPrototypes.com - //// For support and information - ///// visit the DangerousPrototypes Forum - /////////// - - -#include "busPirateCore.h" -#include "procMenu.h" -#include "selftest.h" -#include "basic.h" - - -#if defined (BUSPIRATEV4) -#include "descriptors.h" - -void USBSuspend(void); -void _USB1Interrupt(void); -extern volatile BYTE usb_device_state; // JTR added -#endif - -#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) -//set custom configuration for PIC 24F (now always set in bootloader page, not needed here) -//_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -//_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need -#endif -#if defined (BUSPIRATEV4) -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) -_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV3 & IOL1WAY_ON & PLL_96MHZ_ON & DISUVREG_OFF) -#endif - -unsigned char irqFlag = 0; - -void ISRTable(); //Pseudo function to hold ISR remap jump table -void Initialize(void); - -static unsigned char __attribute__((section(".bss.end"))) _buffer[TERMINAL_BUFFER]; -struct _bpConfig bpConfig = {.terminalInput = _buffer}; //holds persistant bus pirate settings (see busPirateCore.h) -struct _modeConfig modeConfig; //holds mode info, cleared between modes -struct _command bpCommand; //holds the current active command so we don't ahve to put so many variables on the stack - -unsigned char binmodecnt = 0; //, terminalInput[TERMINAL_BUFFER]; - -#pragma code -//this loop services user input and passes it to be processed on - -int main(void) { - - Initialize(); //setup bus pirate - - //wait for the USB connection to enumerate -#if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG) - BP_LEDUSB_DIR = 0; - //BP_LEDUSB = 1; - BP_USBLED_ON(); - //BP_VREGEN_DIR = 0; - // BP_VREGEN = 1; - //BP_LEDMODE_DIR = 0; - //BP_LEDMODE = 1; -#ifdef USB_INTERRUPTS - EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST); - EnableUsbGlobalInterrupt(); -#endif - - do { -#ifndef USB_INTERRUPTS -// if (!TestGlobalUsbInterruptEnable()) //JTR3 added - usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE -#endif - // if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ - // } else if (usb_device_state < CONFIGURED_STATE) { - // } - } while (usb_device_state < CONFIGURED_STATE); // JTR addition. Do not proceed until device is configured. - BP_USBLED_OFF(); - usb_register_sof_handler(CDCFlushOnTimeout); // For timeout value see: cdc_config.h -> BPv4 -> CDC_FLUSH_MS - -#endif - serviceuser(); - return 0; -} - -//bus pirate initialization -//setup clock, terminal UART, pins, LEDs, and display version info - -void Initialize(void) { -#if defined (BUSPIRATEV2) - unsigned char i; -#endif - - volatile unsigned long delay = 0xffff; - - // volatile unsigned long delay = 0xffff; - // TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) -#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) - CLKDIVbits.RCDIV0 = 0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital -#elif defined (BUSPIRATEV4) - INTCON1bits.NSTDIS = 1; - CLKDIV = 0x0000; // Set PLL prescaler (1:1) - BP_LEDUSB_DIR = 0; // output - CORCONbits.PSV = 1; // JTR PIC24 fixup ?? PSV not being initialized. May have been done by c_init though. - PSVPAG = 0; // - OSCCONbits.SOSCEN = 0; - AD1PCFGL = 0x7FD8; //BUSPIRATEV4 has five analog pins b0, b1, b2, b5, b15 - AD1PCFGH = 0x2; - // usb_register_sof_handler(0); -#endif - - OSCCONbits.SOSCEN = 0; - - - while (delay--); - //set pin configuration using peripheral pin select -#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) - BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; -#elif defined (BUSPIRATEV4) && defined (BPV4_DEBUG) - BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=11;//AUX2 - BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP2R=U1TX_IO;//AUX1 -#endif - - //put startup values in config (do first) - bpConfig.termSpeed = 8; //default PC side port speed, startup in 115200, or saved state (later).... - bpConfig.displayMode = HEX; - - bpInit(); //put startup values in config (do first)clean up, exit in HI-Z - -#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) - InitializeUART1(); //init the PC side serial port -#endif - -#if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG) - initCDC(); - usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); - usb_start(); -#endif - -#if defined (BUSPIRATEV4) && defined (BPV4_DEBUG) - InitializeUART1(); //init the PC side serial port -#endif - -#if defined (BUSPIRATEV2) - //find the Bus Pirate revision - //pullup on, do it now so it can settle during the next operations - CNPU1bits.CN6PUE = 1; - CNPU1bits.CN7PUE = 1; -#endif - //#ifndef BUSPIRATEV4 - // Get the chip type and revision - bpConfig.dev_type = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_TYPE); - bpConfig.dev_rev = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_REV); - //#endif - -#if defined (BUSPIRATEV2) - //now check the revision - //Version | RB3 | RB2 - //2go, 3a | 1 | 1 - //v3b | 1 | 0 - //v3.5 | 0 | 0 - i=PORTB; //get settings - i=i>>2; //remove unused - i&=(~0b11111100); //clear others - if (i==0b11) { - bpConfig.HWversion = 'a'; - } else if(i==0b10){ - bpConfig.HWversion = 'b'; - }else if(i==0){ - bpConfig.HWversion = '5'; - } - //pullup off - CNPU1bits.CN6PUE = 0; - CNPU1bits.CN7PUE = 0; -#else - bpConfig.HWversion = 0; -#endif - - bpConfig.quiet = 0; // turn output on (default) - modeConfig.numbits = 8; -#ifdef BP_USE_BASIC - initpgmspace(); -#endif - - TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) - -#ifndef BUSPIRATEV4 - bpWBR; //send a line feed - versionInfo(); //prints hardware and firmware version info (base.c) -#endif - -} - -//Interrupt Remap method 2: Using Goto and jump table -/* -void __attribute__ ((interrupt,no_auto_psv)) _T1Interrupt(){ - IFS0bits.T1IF = 0; - IEC0bits.T1IE = 0; - PR1 = 0xFFFF; - T1CON = 0; - irqFlag=1; -} - */ - -#ifdef BUSPIRATEV4 - -void USBSuspend(void) { - -} -#pragma interrupt _USB1Interrupt - -void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() { - - //USB interrupt - //IRQ enable IEC5bits.USB1IE - //IRQ flag IFS5bits.USB1IF - //IRQ priority IPC21<10:8> - //{ - usb_handler(); - IFS5bits.USB1IF = 0; // PIR2bits.USBIF = 0; - //} -} -#pragma code -#endif - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "base.h" + /////////////////////////////////////////// + /////////////[ NOTE ]////////////////////////////////////// + /// The settings and configuration are on [base.h] please open that file if you are new to + /// compiling the Bus Pirate Firmware. + /// + /// DangerousPrototypes.com + //// For support and information + ///// visit the DangerousPrototypes Forum + /////////// + + +#include "busPirateCore.h" +#include "procMenu.h" +#include "selftest.h" +#include "basic.h" + + +#if defined (BUSPIRATEV4) +#include "descriptors.h" + +void USBSuspend(void); +void _USB1Interrupt(void); +extern volatile BYTE usb_device_state; // JTR added +#endif + +#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) +//set custom configuration for PIC 24F (now always set in bootloader page, not needed here) +//_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz +//_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need +#endif +#if defined (BUSPIRATEV4) +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) +_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV3 & IOL1WAY_ON & PLL_96MHZ_ON & DISUVREG_OFF) +#endif + +unsigned char irqFlag = 0; + +void ISRTable(); //Pseudo function to hold ISR remap jump table +void Initialize(void); + +static unsigned char __attribute__((section(".bss.end"))) _buffer[TERMINAL_BUFFER]; +struct _bpConfig bpConfig = {.terminalInput = _buffer}; //holds persistant bus pirate settings (see busPirateCore.h) +struct _modeConfig modeConfig; //holds mode info, cleared between modes +struct _command bpCommand; //holds the current active command so we don't ahve to put so many variables on the stack + +unsigned char binmodecnt = 0; //, terminalInput[TERMINAL_BUFFER]; + +#pragma code +//this loop services user input and passes it to be processed on + +int main(void) { + + Initialize(); //setup bus pirate + + //wait for the USB connection to enumerate +#if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG) + BP_LEDUSB_DIR = 0; + //BP_LEDUSB = 1; + BP_USBLED_ON(); + //BP_VREGEN_DIR = 0; + // BP_VREGEN = 1; + //BP_LEDMODE_DIR = 0; + //BP_LEDMODE = 1; +#ifdef USB_INTERRUPTS + EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST); + EnableUsbGlobalInterrupt(); +#endif + + do { +#ifndef USB_INTERRUPTS +// if (!TestGlobalUsbInterruptEnable()) //JTR3 added + usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE +#endif + // if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ + // } else if (usb_device_state < CONFIGURED_STATE) { + // } + } while (usb_device_state < CONFIGURED_STATE); // JTR addition. Do not proceed until device is configured. + BP_USBLED_OFF(); + usb_register_sof_handler(CDCFlushOnTimeout); // For timeout value see: cdc_config.h -> BPv4 -> CDC_FLUSH_MS + +#endif + serviceuser(); + return 0; +} + +//bus pirate initialization +//setup clock, terminal UART, pins, LEDs, and display version info + +void Initialize(void) { +#if defined (BUSPIRATEV2) + unsigned char i; +#endif + + volatile unsigned long delay = 0xffff; + + // volatile unsigned long delay = 0xffff; + // TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) +#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) + CLKDIVbits.RCDIV0 = 0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital +#elif defined (BUSPIRATEV4) + INTCON1bits.NSTDIS = 1; + CLKDIV = 0x0000; // Set PLL prescaler (1:1) + BP_LEDUSB_DIR = 0; // output + CORCONbits.PSV = 1; // JTR PIC24 fixup ?? PSV not being initialized. May have been done by c_init though. + PSVPAG = 0; // + OSCCONbits.SOSCEN = 0; + AD1PCFGL = 0x7FD8; //BUSPIRATEV4 has five analog pins b0, b1, b2, b5, b15 + AD1PCFGH = 0x2; + // usb_register_sof_handler(0); +#endif + + OSCCONbits.SOSCEN = 0; + + + while (delay--); + //set pin configuration using peripheral pin select +#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) + BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; + BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; +#elif defined (BUSPIRATEV4) && defined (BPV4_DEBUG) + BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=11;//AUX2 + BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP2R=U1TX_IO;//AUX1 +#endif + + //put startup values in config (do first) + bpConfig.termSpeed = 8; //default PC side port speed, startup in 115200, or saved state (later).... + bpConfig.displayMode = HEX; + + bpInit(); //put startup values in config (do first)clean up, exit in HI-Z + +#if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) + InitializeUART1(); //init the PC side serial port +#endif + +#if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG) + initCDC(); + usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); + usb_start(); +#endif + +#if defined (BUSPIRATEV4) && defined (BPV4_DEBUG) + InitializeUART1(); //init the PC side serial port +#endif + +#if defined (BUSPIRATEV2) + //find the Bus Pirate revision + //pullup on, do it now so it can settle during the next operations + CNPU1bits.CN6PUE = 1; + CNPU1bits.CN7PUE = 1; +#endif + //#ifndef BUSPIRATEV4 + // Get the chip type and revision + bpConfig.dev_type = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_TYPE); + bpConfig.dev_rev = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_REV); + //#endif + +#if defined (BUSPIRATEV2) + //now check the revision + //Version | RB3 | RB2 + //2go, 3a | 1 | 1 + //v3b | 1 | 0 + //v3.5 | 0 | 0 + i=PORTB; //get settings + i=i>>2; //remove unused + i&=(~0b11111100); //clear others + if (i==0b11) { + bpConfig.HWversion = 'a'; + } else if(i==0b10){ + bpConfig.HWversion = 'b'; + }else if(i==0){ + bpConfig.HWversion = '5'; + } + //pullup off + CNPU1bits.CN6PUE = 0; + CNPU1bits.CN7PUE = 0; +#else + bpConfig.HWversion = 0; +#endif + + bpConfig.quiet = 0; // turn output on (default) + modeConfig.numbits = 8; +#ifdef BP_USE_BASIC + initpgmspace(); +#endif + + TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) + +#ifndef BUSPIRATEV4 + bpWBR; //send a line feed + versionInfo(); //prints hardware and firmware version info (base.c) +#endif + +} + +//Interrupt Remap method 2: Using Goto and jump table +/* +void __attribute__ ((interrupt,no_auto_psv)) _T1Interrupt(){ + IFS0bits.T1IF = 0; + IEC0bits.T1IE = 0; + PR1 = 0xFFFF; + T1CON = 0; + irqFlag=1; +} + */ + +#ifdef BUSPIRATEV4 + +void USBSuspend(void) { + +} +#pragma interrupt _USB1Interrupt + +void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() { + + //USB interrupt + //IRQ enable IEC5bits.USB1IE + //IRQ flag IFS5bits.USB1IF + //IRQ priority IPC21<10:8> + //{ + usb_handler(); + IFS5bits.USB1IF = 0; // PIR2bits.USBIF = 0; + //} +} +#pragma code +#endif + + diff --git a/Firmware/onboardEEPROM.c b/Firmware/onboardEEPROM.c index cb0d5918..ed580df9 100644 --- a/Firmware/onboardEEPROM.c +++ b/Firmware/onboardEEPROM.c @@ -1,141 +1,141 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" -#include "onboardEEPROM.h" - -unsigned char eetest(void){ - unsigned char c,l; - eei2cSetup(); - l=eeGetWP(); - - eeEnableWrite(); - - eeWriteByte(0xFF,0); - bpDelayMS(10); - - eeWriteByte(0x10,0); - bpDelayMS(10); - if (eeReadByte(0)==0x10) - c= 0; //in selftest 0 = good. - else - c= 1; - - eeWriteByte(0xFF,0); - - BP_EE_WP = l; //store last WP setting and post it. - return c; -} - -void eestart(void){ - // Enable a start condition - I2C1CONbits.SEN = 1; - while(I2C1CONbits.SEN==1);//wait -} - -void eestop(void){ - I2C1CONbits.PEN = 1; - while(I2C1CONbits.PEN==1);//wait -} - -unsigned char eegetack(void){ - return I2C1STATbits.ACKSTAT; -} - -void eesendack(unsigned char ack){ - I2C1CONbits.ACKDT=ack;//send ACK (0) or NACK(1)? - I2C1CONbits.ACKEN=1; - while(I2C1CONbits.ACKEN==1); -} - -void eewrite(unsigned char c){ - I2C1TRN=c; - while(I2C1STATbits.TRSTAT==1); -} - -unsigned char eeread(void){ - unsigned char c; - - I2C1CONbits.RCEN=1; - while(I2C1CONbits.RCEN==1); - c=I2C1RCV; - return c; -} - -void eei2cSetup(void){ - I2C1CONbits.A10M=0; - I2C1CONbits.SCLREL=0; - - I2C1ADD=0; - I2C1MSK=0; - - // Enable SMBus - I2C1CONbits.SMEN = 0; - - // Baud rate setting - I2C1BRG = 37; - - // Enable I2C module - I2C1CONbits.I2CEN = 1; -} - -/* -void eei2cSetup(void); -unsigned char eeread(void); -void eewrite(unsigned char c); -void eesendack(unsigned char ack); -unsigned char eegetack(void); -void eestop(void); -void eestop(void); -void eestart(void); -unsigned char eetest(void); */ - -unsigned char eeReadByte(unsigned long wAddr) -{ - char RetVal; - - eestart(); - eewrite(BP_EEPROM_ID_W); - eewrite((wAddr >> 8)); //Sent write Address high bits first - eewrite(wAddr); //Send write address low bits section - - eestart(); - eewrite(BP_EEPROM_ID_R); - - RetVal = eeread(); //Write the single bit. - - eesendack(1); //send nak? - eestop(); - - return RetVal; -} - -unsigned int eeWriteByte(char wByte, unsigned long wAddr) -{ - char RetVal; - eestart(); - eewrite(BP_EEPROM_ID_W); - //test1=eegetack(); - - eewrite((wAddr >> 8)); //Sent write Address high bits first - eewrite(wAddr); //Send write address low bits section - //eegetack(); - eewrite(wByte); //Write the single bit. - RetVal = eegetack(); - eestop(); - - return RetVal; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "base.h" +#include "onboardEEPROM.h" + +unsigned char eetest(void){ + unsigned char c,l; + eei2cSetup(); + l=eeGetWP(); + + eeEnableWrite(); + + eeWriteByte(0xFF,0); + bpDelayMS(10); + + eeWriteByte(0x10,0); + bpDelayMS(10); + if (eeReadByte(0)==0x10) + c= 0; //in selftest 0 = good. + else + c= 1; + + eeWriteByte(0xFF,0); + + BP_EE_WP = l; //store last WP setting and post it. + return c; +} + +void eestart(void){ + // Enable a start condition + I2C1CONbits.SEN = 1; + while(I2C1CONbits.SEN==1);//wait +} + +void eestop(void){ + I2C1CONbits.PEN = 1; + while(I2C1CONbits.PEN==1);//wait +} + +unsigned char eegetack(void){ + return I2C1STATbits.ACKSTAT; +} + +void eesendack(unsigned char ack){ + I2C1CONbits.ACKDT=ack;//send ACK (0) or NACK(1)? + I2C1CONbits.ACKEN=1; + while(I2C1CONbits.ACKEN==1); +} + +void eewrite(unsigned char c){ + I2C1TRN=c; + while(I2C1STATbits.TRSTAT==1); +} + +unsigned char eeread(void){ + unsigned char c; + + I2C1CONbits.RCEN=1; + while(I2C1CONbits.RCEN==1); + c=I2C1RCV; + return c; +} + +void eei2cSetup(void){ + I2C1CONbits.A10M=0; + I2C1CONbits.SCLREL=0; + + I2C1ADD=0; + I2C1MSK=0; + + // Enable SMBus + I2C1CONbits.SMEN = 0; + + // Baud rate setting + I2C1BRG = 37; + + // Enable I2C module + I2C1CONbits.I2CEN = 1; +} + +/* +void eei2cSetup(void); +unsigned char eeread(void); +void eewrite(unsigned char c); +void eesendack(unsigned char ack); +unsigned char eegetack(void); +void eestop(void); +void eestop(void); +void eestart(void); +unsigned char eetest(void); */ + +unsigned char eeReadByte(unsigned long wAddr) +{ + char RetVal; + + eestart(); + eewrite(BP_EEPROM_ID_W); + eewrite((wAddr >> 8)); //Sent write Address high bits first + eewrite(wAddr); //Send write address low bits section + + eestart(); + eewrite(BP_EEPROM_ID_R); + + RetVal = eeread(); //Write the single bit. + + eesendack(1); //send nak? + eestop(); + + return RetVal; +} + +unsigned int eeWriteByte(char wByte, unsigned long wAddr) +{ + char RetVal; + eestart(); + eewrite(BP_EEPROM_ID_W); + //test1=eegetack(); + + eewrite((wAddr >> 8)); //Sent write Address high bits first + eewrite(wAddr); //Send write address low bits section + //eegetack(); + eewrite(wByte); //Write the single bit. + RetVal = eegetack(); + eestop(); + + return RetVal; +} diff --git a/Firmware/onboardEEPROM.h b/Firmware/onboardEEPROM.h index 23d66160..7e2898ec 100644 --- a/Firmware/onboardEEPROM.h +++ b/Firmware/onboardEEPROM.h @@ -1,43 +1,43 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - - -// Addressing the BP eeprom -#define BP_EEPROM_ID 0xA0 -#define BP_EEPROM_ID_W 0xA0 -#define BP_EEPROM_ID_R 0xA1 - -// Eeprom Max and Min values -#define BP_EEPROM_MIN 1 -#define BP_EEPROM_MAX 8192 - -// couple defines to enable and disable WP -#define eeEnableWrite() BP_EE_WP=0 -#define eeDisableWrite() BP_EE_WP=1 -#define eeGetWP() BP_EE_WP - -void eei2cSetup(void); -unsigned char eeread(void); -void eewrite(unsigned char c); -void eesendack(unsigned char ack); -unsigned char eegetack(void); -void eestop(void); -void eestop(void); -void eestart(void); -unsigned char eetest(void); - -unsigned char eeReadByte(unsigned long wAddr); -unsigned int eeWriteByte(char wByte, unsigned long wAddr); +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + + +// Addressing the BP eeprom +#define BP_EEPROM_ID 0xA0 +#define BP_EEPROM_ID_W 0xA0 +#define BP_EEPROM_ID_R 0xA1 + +// Eeprom Max and Min values +#define BP_EEPROM_MIN 1 +#define BP_EEPROM_MAX 8192 + +// couple defines to enable and disable WP +#define eeEnableWrite() BP_EE_WP=0 +#define eeDisableWrite() BP_EE_WP=1 +#define eeGetWP() BP_EE_WP + +void eei2cSetup(void); +unsigned char eeread(void); +void eewrite(unsigned char c); +void eesendack(unsigned char ack); +unsigned char eegetack(void); +void eestop(void); +void eestop(void); +void eestart(void); +unsigned char eetest(void); + +unsigned char eeReadByte(unsigned long wAddr); +unsigned int eeWriteByte(char wByte, unsigned long wAddr); diff --git a/Firmware/pc_at_keyboard.c b/Firmware/pc_at_keyboard.c index b77e3501..5f368ffc 100644 --- a/Firmware/pc_at_keyboard.c +++ b/Firmware/pc_at_keyboard.c @@ -1,313 +1,313 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" -//#include "pc_at_keyboard.h" - -#define KBCLK_TRIS BP_CLK_DIR -#define KBCLK BP_CLK - -#define KBDIO_TRIS BP_MOSI_DIR -#define KBDIO BP_MOSI - -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; - -#ifdef BP_USE_PCATKB - -void kbSetup(void); -void KEYBsetup_exc(void); -unsigned char kbReadBit(void); -unsigned char kbReadCode(void); -unsigned char kbReadByte(void); -unsigned char kbWriteByte(unsigned char c); -unsigned char kbWriteBit(unsigned char c); -void kbScancodeResults(unsigned char c); -unsigned char kbWaitClock(unsigned char c); - -struct _kbframe{ - unsigned char startbit:1; - unsigned char code; - unsigned char paritybit:1; - unsigned char stopbit:1; - unsigned char parityerror:1; -} kbScancode; - - -void KEYBsetup(void) -{ modeConfig.HiZ=1;//yes, always HiZ -} - -void KEYBsetup_exc(void) -{ - kbSetup(); -} - -unsigned int KEYBread(void) -{ kbScancodeResults(kbReadByte()); - return kbScancode.code; -} - -unsigned int KEYBwrite(unsigned int c) -{ unsigned char temp; - - temp=kbWriteByte(c);//send to bus - if(temp==0) //ack bit - { //bpWmessage(MSG_ACK); - BPMSG1060; - } - else if (temp==1) - { //bpWmessage(MSG_NACK); - BPMSG1061; - } - else - { //bpWstring(OUMSG_KB_TIMEOUT); - BPMSG1237; - } - return 0x100; -} - -void KEYBmacro(unsigned char c) -{ switch(c) - { case 0: //bpWline(OUMSG_KB_MACRO_MENU); - BPMSG1238; - break; - case 1: //bpWline(OUMSG_KB__MACRO_LIVE); - BPMSG1239; - BPMSG1250; - while(1) - { if(kbReadByte()==0) - { bpWbyte(kbScancode.code); - bpSP; - } - if(UART1RXRdy() == 1) //any key pressed, exit - { c=UART1RX(); /* JTR usb port; */ - bpBR; - break; - } - } - break; - default: BPMSG1016; - } -} - -void kbScancodeResults(unsigned char c) -{ switch(c) - { case 0: // all ok - //bpWbyte(kbScancode.code); - //bpSP; - break; - case 1: - //bpWstring(OUMSG_KB_ERROR_STARTBIT); - BPMSG1240; - break; - case 2: - //bpWbyte(kbScancode.code); - //bpWstring(OUMSG_KB_ERROR_PARITY); - BPMSG1241; - break; - case 3: - //bpWbyte(kbScancode.code); - //bpWstring(OUMSG_KB_ERROR_STOPBIT); - BPMSG1242; - break; - case 4: - //bpWstring(OUMSG_KB_ERROR_TIMEOUT); - bpSP; - BPMSG1237; - break; - case 0xff://no data - //bpWstring(OUMSG_KB_ERROR_NONE); - BPMSG1243; - break; - default: - //bpWstring(OUMSG_KB_ERROR_UNK); - BPMSG1244; - break; - } -} - -void kbSetup(void){ - //writes to the PORTs write to the LATCH - KBDIO_TRIS=1;//data input - KBCLK_TRIS=0;//clock output/low - KBCLK=0; //B8 scl - KBDIO=0; //B9 sda -} - - -unsigned char kbReadBit(void){ - unsigned char j; - - if(kbWaitClock(0)!=0) return 2; //wait for clock High->LOW (ACTIVE) - - j=KBDIO; //read the data pin - - if(kbWaitClock(1)!=0) return 2; //wait for clock low->HIGH (IDLE) - - return j; -} - -//reads scancode into kbScancode structure -//returns 0 - success -//1 - startbit error -//2 - parity error -//3 - stopbit error -//4 - read bit timeout error -unsigned char kbReadCode(void){ - unsigned char i, par=0,c=0, b; - - //get startbit - kbScancode.startbit=KBDIO; - if(kbScancode.startbit!=0) return 1;//startbit should be 0 - - //while(KBCLK==0);//wait for clock HIGH (IDLE) - if(kbWaitClock(1)!=0) return 4; - - //grab the eight databits LSB - for(i=0;i<8;i++){ - c=c>>1; - b=kbReadBit(); - if(b==2) return 4; //bit timeout - if(b==1){ - c+=0b10000000; - par^=1; - } - } - kbScancode.code=c; - - //get parity bit and check - b=kbReadBit(); - if(b==2) return 4; //bit timeout - kbScancode.paritybit=b; - //odd parity:if these are the same, there is an error - //condense to bitwise op... - if(kbScancode.paritybit==par){ - kbScancode.parityerror=1; - return 2; //error - }else{ - kbScancode.parityerror=0;//ok - } - - //get stopbit - b=kbReadBit(); - if(b==2) return 4; //bit timeout - kbScancode.stopbit=b;//stopbit should be 1 - KBCLK_TRIS=0;//set clock low again to idle keyboard - if(kbScancode.stopbit!=1) return 3; - - //success! - return 0; -} - -unsigned char kbReadByte(void){ - unsigned char i,c=0xff; - - //let clk float, clk and data will drop if there is data - KBDIO_TRIS=1;//data input - KBCLK_TRIS=1;//clock input/high (keyboard will go low if scan code ready) - - for(i=0;i<255;i++){//do a loop, wait for change - //while(1){//for debugging - if(KBCLK==0){//check clock status - c=kbReadCode();//read the scancode - if(c==0 || c==4)break;//success or error, break the loop - } - bpDelayUS(5);//delay - } - - //disable any further data by dropping clock line - KBCLK_TRIS=0;//set clock low again - - return c; -} - -//writes a bit using the KB clock signal. -// 0=success, 2=timeout -unsigned char kbWriteBit(unsigned char c){ - - if(kbWaitClock(1)!=0) return 2; //wait for low-high transition - KBDIO_TRIS=c;//set data direction - if(kbWaitClock(0)!=0) return 2; //wait for high-low transition - return 0; //data now entered, you have a tiny bit of time before next bit -} - -//@c - byte to send -//@returns - ack bit -unsigned char kbWriteByte(unsigned char c){ - unsigned char par=0; - unsigned int i; - - //take clk low to own the bus - //clock should already be low - KBCLK_TRIS=0;//clock low - KBCLK=0; - - bpDelayUS(60);//delay at least 1 bit period (60us), interrupt any other data transfer - - //data low to signal host->kb transfer - KBDIO=0; - KBDIO_TRIS=0;//data low - - //take clock high again, KB will start own clock - KBCLK_TRIS=1;//clock high/input - bpDelayUS(1); - - //while(KBCLK==1);//wait for first falling edge - if(kbWaitClock(0)!=0) return 4; //timeout - - //bang out the bits LSB first out using the kb clock signal - for(i=0;i<8;i++){ - if((c&1)==1){ - if(kbWriteBit(1)!=0)return 4;//send LSB first - par^=1;//track parity bit - }else{ - if(kbWriteBit(0)!=0)return 4;//send LSB first - } - c=c>>1;//-- Shift next bit into position - } - - //write parity bit - par^=1;//shift parity bit once more so it is opposite - if(kbWriteBit(par)!=0)return 4;//send parity bit - - KBDIO_TRIS=1;//configure data for input - KBDIO=0; - - //while(KBCLK==0);//wait for low->high transition to align with a readbit - if(kbWaitClock(1)!=0) return 4; //timeout - - if(kbReadBit()==2) return 4;//junk bit (stopbit/high) - i=kbReadBit();//ack bit - if(i==2) return 4; - - KBCLK_TRIS=0;//set clock low again to halt data - KBCLK=0; - - return i;//return ack bit -} - - -unsigned char kbWaitClock(unsigned char c){ - unsigned int i=0xffff; - while(1){ - if(KBCLK==c)return 0; //clock changed - i--; - if(i==0)return 1;//timeout - } -} - -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "base.h" +//#include "pc_at_keyboard.h" + +#define KBCLK_TRIS BP_CLK_DIR +#define KBCLK BP_CLK + +#define KBDIO_TRIS BP_MOSI_DIR +#define KBDIO BP_MOSI + +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; + +#ifdef BP_USE_PCATKB + +void kbSetup(void); +void KEYBsetup_exc(void); +unsigned char kbReadBit(void); +unsigned char kbReadCode(void); +unsigned char kbReadByte(void); +unsigned char kbWriteByte(unsigned char c); +unsigned char kbWriteBit(unsigned char c); +void kbScancodeResults(unsigned char c); +unsigned char kbWaitClock(unsigned char c); + +struct _kbframe{ + unsigned char startbit:1; + unsigned char code; + unsigned char paritybit:1; + unsigned char stopbit:1; + unsigned char parityerror:1; +} kbScancode; + + +void KEYBsetup(void) +{ modeConfig.HiZ=1;//yes, always HiZ +} + +void KEYBsetup_exc(void) +{ + kbSetup(); +} + +unsigned int KEYBread(void) +{ kbScancodeResults(kbReadByte()); + return kbScancode.code; +} + +unsigned int KEYBwrite(unsigned int c) +{ unsigned char temp; + + temp=kbWriteByte(c);//send to bus + if(temp==0) //ack bit + { //bpWmessage(MSG_ACK); + BPMSG1060; + } + else if (temp==1) + { //bpWmessage(MSG_NACK); + BPMSG1061; + } + else + { //bpWstring(OUMSG_KB_TIMEOUT); + BPMSG1237; + } + return 0x100; +} + +void KEYBmacro(unsigned char c) +{ switch(c) + { case 0: //bpWline(OUMSG_KB_MACRO_MENU); + BPMSG1238; + break; + case 1: //bpWline(OUMSG_KB__MACRO_LIVE); + BPMSG1239; + BPMSG1250; + while(1) + { if(kbReadByte()==0) + { bpWbyte(kbScancode.code); + bpSP; + } + if(UART1RXRdy() == 1) //any key pressed, exit + { c=UART1RX(); /* JTR usb port; */ + bpBR; + break; + } + } + break; + default: BPMSG1016; + } +} + +void kbScancodeResults(unsigned char c) +{ switch(c) + { case 0: // all ok + //bpWbyte(kbScancode.code); + //bpSP; + break; + case 1: + //bpWstring(OUMSG_KB_ERROR_STARTBIT); + BPMSG1240; + break; + case 2: + //bpWbyte(kbScancode.code); + //bpWstring(OUMSG_KB_ERROR_PARITY); + BPMSG1241; + break; + case 3: + //bpWbyte(kbScancode.code); + //bpWstring(OUMSG_KB_ERROR_STOPBIT); + BPMSG1242; + break; + case 4: + //bpWstring(OUMSG_KB_ERROR_TIMEOUT); + bpSP; + BPMSG1237; + break; + case 0xff://no data + //bpWstring(OUMSG_KB_ERROR_NONE); + BPMSG1243; + break; + default: + //bpWstring(OUMSG_KB_ERROR_UNK); + BPMSG1244; + break; + } +} + +void kbSetup(void){ + //writes to the PORTs write to the LATCH + KBDIO_TRIS=1;//data input + KBCLK_TRIS=0;//clock output/low + KBCLK=0; //B8 scl + KBDIO=0; //B9 sda +} + + +unsigned char kbReadBit(void){ + unsigned char j; + + if(kbWaitClock(0)!=0) return 2; //wait for clock High->LOW (ACTIVE) + + j=KBDIO; //read the data pin + + if(kbWaitClock(1)!=0) return 2; //wait for clock low->HIGH (IDLE) + + return j; +} + +//reads scancode into kbScancode structure +//returns 0 - success +//1 - startbit error +//2 - parity error +//3 - stopbit error +//4 - read bit timeout error +unsigned char kbReadCode(void){ + unsigned char i, par=0,c=0, b; + + //get startbit + kbScancode.startbit=KBDIO; + if(kbScancode.startbit!=0) return 1;//startbit should be 0 + + //while(KBCLK==0);//wait for clock HIGH (IDLE) + if(kbWaitClock(1)!=0) return 4; + + //grab the eight databits LSB + for(i=0;i<8;i++){ + c=c>>1; + b=kbReadBit(); + if(b==2) return 4; //bit timeout + if(b==1){ + c+=0b10000000; + par^=1; + } + } + kbScancode.code=c; + + //get parity bit and check + b=kbReadBit(); + if(b==2) return 4; //bit timeout + kbScancode.paritybit=b; + //odd parity:if these are the same, there is an error + //condense to bitwise op... + if(kbScancode.paritybit==par){ + kbScancode.parityerror=1; + return 2; //error + }else{ + kbScancode.parityerror=0;//ok + } + + //get stopbit + b=kbReadBit(); + if(b==2) return 4; //bit timeout + kbScancode.stopbit=b;//stopbit should be 1 + KBCLK_TRIS=0;//set clock low again to idle keyboard + if(kbScancode.stopbit!=1) return 3; + + //success! + return 0; +} + +unsigned char kbReadByte(void){ + unsigned char i,c=0xff; + + //let clk float, clk and data will drop if there is data + KBDIO_TRIS=1;//data input + KBCLK_TRIS=1;//clock input/high (keyboard will go low if scan code ready) + + for(i=0;i<255;i++){//do a loop, wait for change + //while(1){//for debugging + if(KBCLK==0){//check clock status + c=kbReadCode();//read the scancode + if(c==0 || c==4)break;//success or error, break the loop + } + bpDelayUS(5);//delay + } + + //disable any further data by dropping clock line + KBCLK_TRIS=0;//set clock low again + + return c; +} + +//writes a bit using the KB clock signal. +// 0=success, 2=timeout +unsigned char kbWriteBit(unsigned char c){ + + if(kbWaitClock(1)!=0) return 2; //wait for low-high transition + KBDIO_TRIS=c;//set data direction + if(kbWaitClock(0)!=0) return 2; //wait for high-low transition + return 0; //data now entered, you have a tiny bit of time before next bit +} + +//@c - byte to send +//@returns - ack bit +unsigned char kbWriteByte(unsigned char c){ + unsigned char par=0; + unsigned int i; + + //take clk low to own the bus + //clock should already be low + KBCLK_TRIS=0;//clock low + KBCLK=0; + + bpDelayUS(60);//delay at least 1 bit period (60us), interrupt any other data transfer + + //data low to signal host->kb transfer + KBDIO=0; + KBDIO_TRIS=0;//data low + + //take clock high again, KB will start own clock + KBCLK_TRIS=1;//clock high/input + bpDelayUS(1); + + //while(KBCLK==1);//wait for first falling edge + if(kbWaitClock(0)!=0) return 4; //timeout + + //bang out the bits LSB first out using the kb clock signal + for(i=0;i<8;i++){ + if((c&1)==1){ + if(kbWriteBit(1)!=0)return 4;//send LSB first + par^=1;//track parity bit + }else{ + if(kbWriteBit(0)!=0)return 4;//send LSB first + } + c=c>>1;//-- Shift next bit into position + } + + //write parity bit + par^=1;//shift parity bit once more so it is opposite + if(kbWriteBit(par)!=0)return 4;//send parity bit + + KBDIO_TRIS=1;//configure data for input + KBDIO=0; + + //while(KBCLK==0);//wait for low->high transition to align with a readbit + if(kbWaitClock(1)!=0) return 4; //timeout + + if(kbReadBit()==2) return 4;//junk bit (stopbit/high) + i=kbReadBit();//ack bit + if(i==2) return 4; + + KBCLK_TRIS=0;//set clock low again to halt data + KBCLK=0; + + return i;//return ack bit +} + + +unsigned char kbWaitClock(unsigned char c){ + unsigned int i=0xffff; + while(1){ + if(KBCLK==c)return 0; //clock changed + i--; + if(i==0)return 1;//timeout + } +} + +#endif diff --git a/Firmware/pc_at_keyboard.h b/Firmware/pc_at_keyboard.h index a5d871e0..0fbc50cd 100644 --- a/Firmware/pc_at_keyboard.h +++ b/Firmware/pc_at_keyboard.h @@ -1,24 +1,24 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -void KEYBsetup(void); -void KEYBsetup_exc(void); -unsigned int KEYBread(void); -unsigned int KEYBwrite(unsigned int c); -void KEYBmacro(unsigned int c); - - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +void KEYBsetup(void); +void KEYBsetup_exc(void); +unsigned int KEYBread(void); +unsigned int KEYBwrite(unsigned int c); +void KEYBmacro(unsigned int c); + + + diff --git a/Firmware/pic.c b/Firmware/pic.c index 0d570ac7..bbf4885f 100644 --- a/Firmware/pic.c +++ b/Firmware/pic.c @@ -1,441 +1,441 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Initial written by Chris van Dongen, 2010. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -/* - -these are lowlevel interface subs for programming pics - -supported platform (should) are: - -pic10, pic12, pic14, pic16 and pic18 - -{0xFF} sends 4 byte cmd (pic18) -[0xFF] sends 6 byte cmd (pic10, 12, 14, 16) - -*/ -#include "base.h" -#include "pic.h" -#include "bitbang.h" -#include "baseIO.h" -#include "busPirateCore.h" -#include "procMenu.h" // for the userinteraction subs -#include "AUXpin.h" - -extern struct _bpConfig bpConfig; -extern struct _modeConfig modeConfig; - -#ifdef BP_USE_PIC - -int picmode; -int piccmddelay; - -void picinit(void) -{ int mode, delay; - int interactive; - - consumewhitechars(); - mode=getint(); - consumewhitechars(); - delay=getint(); - interactive=0; - - if(!((mode>0)&&(mode<=2))) - { interactive=1; - } - - if((delay>0)&&(delay<=2)) - { piccmddelay=delay; - } - else - { interactive=1; - } - - if(interactive) - { cmderror=0; - - //bpWline("Commandmode"); - //bpWline("1. 6b/14b"); - //bpWline("2. 4b/16b"); - BPMSG1072; - - mode=getnumber(1,1,2,0); - - //bpWline("Delay"); - BPMSG1073; - delay=getnumber(1,1,2,0); - } - - switch(mode) - { case 1: picmode=PICMODE6; - break; - case 2: picmode=PICMODE4; - break; - default: break; - } - piccmddelay=delay; - - if(!interactive) - { //bpWstring("PIC(mod dly)=("); - BPMSG1074; - bpWdec(picmode); bpSP; - bpWdec(piccmddelay); - bpWline(")"); - } - - modeConfig.HiZ=1; // to allow different Vcc - modeConfig.int16=1; - bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp -} - -//Doesn't do much as the protocol defines that the pins need to be connected bedro power is applied. -void picinit_exc(void){modeConfig.int16=1;} - -void piccleanup(void) -{ modeConfig.int16=0; // other things are cleared except this one :D (we introduced it :D) -} - -void picstart(void) // switch to commandmode -{ picmode|=PICCMD; - //bpWstring("CMD"); - BPMSG1075; - UART1TX(0x30+(picmode&PICMODEMSK)); // display #commandbits - modeConfig.int16=0; - bpBR; -} - -void picstop(void) // switch to datamode -{ picmode&=PICMODEMSK; - modeConfig.int16=1; // data is 14-16 bit - //bpWline("DTA"); - BPMSG1076; -} - -unsigned int picread(void) -{ int i; - unsigned int c; - - if(picmode&PICCMDMSK) - { //bpWline("no read"); - BPMSG1077; - return 0; - } - - c=0; - - switch(picmode&PICMODEMSK) // make it future proof - { case PICMODE6: bbR(MOSI); // switch in to input - bbH(CLK, PICSPEED/2); - bbL(CLK, PICSPEED/2); - for(i=0; i<14; i++) - { c>>=1; - bbH(CLK, PICSPEED/2); - if(bbR(MOSI)) c|=0x2000; // bit14 - bbL(CLK, PICSPEED/2); - } - bbH(CLK, PICSPEED/2); - bbL(CLK, PICSPEED/2); - bbL(MOSI, PICSPEED/5); - break; - case PICMODE4: bbR(MOSI); - for(i=0; i<16; i++) - { c>>=1; - bbH(CLK, PICSPEED/2); - if(bbR(MOSI)) c|=0x8000; // bit16 - bbL(CLK, PICSPEED/2); - } - bbH(CLK, PICSPEED/2); - bbL(CLK, PICSPEED/2); - bbL(MOSI, PICSPEED/5); - break; - default: bpWline("unknown mode"); - BPMSG1078; - return 0; - } - -// bpWbin(c>>8); bpSP; -// bpWbin(c&0x0FF); bpSP; - return c; -} - -unsigned int picwrite(unsigned int c) -{ int i; - int mask; - - mask=0x01; - - if(picmode&PICCMDMSK) // we got a command - { switch(picmode&PICMODEMSK) // make it future proof - { case PICMODE6: for(i=0; i<6; i++) - { bbH(CLK, PICSPEED/4); - if(c&mask) - { bbH(MOSI, PICSPEED/4); - } - else - { bbL(MOSI, PICSPEED/4); - } - bbL(CLK, PICSPEED/4); - bbL(MOSI, PICSPEED/4); // both dat and clk low - mask<<=1; - } - break; - case PICMODE4: for(i=0; i<4; i++) - { bbH(CLK, PICSPEED/4); - if(c&mask) - { bbH(MOSI, PICSPEED/4); - } - else - { bbL(MOSI, PICSPEED/4); - } - bbL(CLK, PICSPEED/4); - bbL(MOSI, PICSPEED/4); // both dat and clk low - mask<<=1; - } - break; - default: //bpWline("unknown"); - BPMSG1078; - return 0; - } - bpDelayMS(piccmddelay); - } - else // send data - { switch(picmode&PICMODEMSK) // make it future proof - { case PICMODE6: bbH(CLK, PICSPEED/4); // send leading 0 - bbL(MOSI, PICSPEED/4); - bbL(CLK, PICSPEED/4); - bbL(CLK, PICSPEED/4); - for(i=0; i<14; i++) // 14 bits - { bbH(CLK, PICSPEED/4); - if(c&mask) - { bbH(MOSI, PICSPEED/4); - } - else - { bbL(MOSI, PICSPEED/4); - } - bbL(CLK, PICSPEED/4); - bbL(MOSI, PICSPEED/4); // both dat and clk low - mask<<=1; - } - bbH(CLK, PICSPEED/4); // send trailing 0 - bbL(MOSI, PICSPEED/4); - bbL(CLK, PICSPEED/4); - bbL(CLK, PICSPEED/4); - break; - case PICMODE4: for(i=0; i<16; i++) // does 16 bits at a time - { bbH(CLK, PICSPEED/4); - if(c&mask) - { bbH(MOSI, PICSPEED/4); - } - else - { bbL(MOSI, PICSPEED/4); - } - bbL(CLK, PICSPEED/4); - bbL(MOSI, PICSPEED/4); // both dat and clk low - mask<<=1; - } - break; - default: //bpWline("unknown"); - BPMSG1078; - return 0; - } - } - return 0x100; // no data to display -} - -void picmacro(unsigned int macro) -{ unsigned int temp; - int i; - - switch(macro) - { case 0: //bpWline("(1) get devID"); - BPMSG1079; - break; - case 1: switch(picmode&PICMODEMSK) - { case PICMODE6: bpConfig.quiet=1; // turn echoing off - picstart(); - picwrite(0); - picstop(); - picwrite(0); // advance to 0x2006 (devid) - picstart(); - for(i=0; i<6; i++) - { picwrite(6); - } - picwrite(4); - picstop(); - temp=picread(); - bpConfig.quiet=0; // turn echoing on - //bpWstring("DevID = "); - BPMSG1080; - bpWinthex(temp>>5); - //bpWstring(" Rev = "); - BPMSG1081; - bpWhex(temp&0x1f); - bpBR; - break; - case PICMODE4: - default: //bpWline("Not implemented (yet)"); - BPMSG1082; - } - //bpWline("Please exit PIC programming mode"); - BPMSG1083; - break; - default: //bpWmessage(MSG_ERROR_MACRO); - BPMSG1016; - } -} - -void picpins(void) { - #if defined(BUSPIRATEV4) - BPMSG1262; //bpWline("-\t-\tPGC\tPGD"); - #else - BPMSG1232; //bpWline("PGC\tPGD\t-\t-"); - #endif -} - -/* -0000 0000 return to main -0000 0001 id=PIC1 -0000 0010 6b cmd -0000 0011 4b cmd -0000 01xx xx ms delay - -0000 1xxx unimplemented - -0001 0xyz PWM|VREG|PULLUP -0001 1xyz AUX|MISO|CS - -01xx xxxx just send cmd xxxxxxx -10xx xxxx send cmd xxxxxxx and next two bytes (14/16 bits) -11xx xxxx send cmd xxxxxxx and read two bytes - -*/ - -void binpic(void) -{ unsigned char cmd; - int ok; - unsigned int temp; - - bpWstring("PIC1"); - modeConfig.HiZ=1; // to allow different Vcc - bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp - picmode=PICMODE6; - piccmddelay=2; - - while(1) - { cmd=UART1RX(); - - switch(cmd&0xC0) - { case 0x00: ok=1; - switch(cmd&0xF0) - { case 0x00: switch(cmd) - { case 0x00: return; - case 0x01: bpWstring("PIC1"); - break; - case 0x02: picmode=PICMODE6; - break; - case 0x03: picmode=PICMODE4; - break; - case 0x04: - case 0x05: - case 0x06: - case 0x07: piccmddelay=(cmd-0x04); - break; - default: ok=0; - } - break; - case 0x10: if(cmd&0x08) - { if(cmd&0x04) - { bbH(AUX ,5); - } - else - { bbL(AUX ,5); - } - if(cmd&0x02) - { bbH(MISO ,5); - } - else - { bbL(MISO ,5); - } - if(cmd&0x01) - { bbH(CS ,5); - } - else - { bbL(CS ,5); - } - } - else - { if(cmd&0x04) // pwm? - { PWMfreq=100; - PWMduty=50; - updatePWM(); - } - else - { PWMfreq=0; - updatePWM(); - } - if(cmd&0x02) // vreg on - { BP_VREG_ON(); - //modeConfig.vregEN=1; - } - else - { BP_VREG_OFF(); - //modeConfig.vregEN=0; - } - if(cmd&0x01) // pullup on -#ifndef BUSPIRATEV1A - { BP_PULLUP_ON(); -// modeConfig.pullupEN=1; - } - else - { BP_PULLUP_OFF(); -// modeConfig.pullupEN=0; - } -#endif - } - break; - default: ok=0; - } - if(ok) - { UART1TX(1); - } - else - { UART1TX(0); - } - break; - case 0x40: picmode|=PICCMD; - picwrite(cmd&0x3F); - picmode&=PICMODEMSK; - UART1TX(1); - break; - case 0x80: picmode|=PICCMD; - picwrite(cmd&0x3F); - picmode&=PICMODEMSK; - temp=UART1RX(); - temp<<=8; - temp|=UART1RX(); - picwrite(temp); - UART1TX(1); - break; - case 0xC0: picmode|=PICCMD; - picwrite(cmd&0x3F); - picmode&=PICMODEMSK; - UART1TX(1); - temp=picread(); - UART1TX(temp>>8); - UART1TX(temp&0x0FF); - break; - } - } -} -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Initial written by Chris van Dongen, 2010. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +/* + +these are lowlevel interface subs for programming pics + +supported platform (should) are: + +pic10, pic12, pic14, pic16 and pic18 + +{0xFF} sends 4 byte cmd (pic18) +[0xFF] sends 6 byte cmd (pic10, 12, 14, 16) + +*/ +#include "base.h" +#include "pic.h" +#include "bitbang.h" +#include "baseIO.h" +#include "busPirateCore.h" +#include "procMenu.h" // for the userinteraction subs +#include "AUXpin.h" + +extern struct _bpConfig bpConfig; +extern struct _modeConfig modeConfig; + +#ifdef BP_USE_PIC + +int picmode; +int piccmddelay; + +void picinit(void) +{ int mode, delay; + int interactive; + + consumewhitechars(); + mode=getint(); + consumewhitechars(); + delay=getint(); + interactive=0; + + if(!((mode>0)&&(mode<=2))) + { interactive=1; + } + + if((delay>0)&&(delay<=2)) + { piccmddelay=delay; + } + else + { interactive=1; + } + + if(interactive) + { cmderror=0; + + //bpWline("Commandmode"); + //bpWline("1. 6b/14b"); + //bpWline("2. 4b/16b"); + BPMSG1072; + + mode=getnumber(1,1,2,0); + + //bpWline("Delay"); + BPMSG1073; + delay=getnumber(1,1,2,0); + } + + switch(mode) + { case 1: picmode=PICMODE6; + break; + case 2: picmode=PICMODE4; + break; + default: break; + } + piccmddelay=delay; + + if(!interactive) + { //bpWstring("PIC(mod dly)=("); + BPMSG1074; + bpWdec(picmode); bpSP; + bpWdec(piccmddelay); + bpWline(")"); + } + + modeConfig.HiZ=1; // to allow different Vcc + modeConfig.int16=1; + bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp +} + +//Doesn't do much as the protocol defines that the pins need to be connected bedro power is applied. +void picinit_exc(void){modeConfig.int16=1;} + +void piccleanup(void) +{ modeConfig.int16=0; // other things are cleared except this one :D (we introduced it :D) +} + +void picstart(void) // switch to commandmode +{ picmode|=PICCMD; + //bpWstring("CMD"); + BPMSG1075; + UART1TX(0x30+(picmode&PICMODEMSK)); // display #commandbits + modeConfig.int16=0; + bpBR; +} + +void picstop(void) // switch to datamode +{ picmode&=PICMODEMSK; + modeConfig.int16=1; // data is 14-16 bit + //bpWline("DTA"); + BPMSG1076; +} + +unsigned int picread(void) +{ int i; + unsigned int c; + + if(picmode&PICCMDMSK) + { //bpWline("no read"); + BPMSG1077; + return 0; + } + + c=0; + + switch(picmode&PICMODEMSK) // make it future proof + { case PICMODE6: bbR(MOSI); // switch in to input + bbH(CLK, PICSPEED/2); + bbL(CLK, PICSPEED/2); + for(i=0; i<14; i++) + { c>>=1; + bbH(CLK, PICSPEED/2); + if(bbR(MOSI)) c|=0x2000; // bit14 + bbL(CLK, PICSPEED/2); + } + bbH(CLK, PICSPEED/2); + bbL(CLK, PICSPEED/2); + bbL(MOSI, PICSPEED/5); + break; + case PICMODE4: bbR(MOSI); + for(i=0; i<16; i++) + { c>>=1; + bbH(CLK, PICSPEED/2); + if(bbR(MOSI)) c|=0x8000; // bit16 + bbL(CLK, PICSPEED/2); + } + bbH(CLK, PICSPEED/2); + bbL(CLK, PICSPEED/2); + bbL(MOSI, PICSPEED/5); + break; + default: bpWline("unknown mode"); + BPMSG1078; + return 0; + } + +// bpWbin(c>>8); bpSP; +// bpWbin(c&0x0FF); bpSP; + return c; +} + +unsigned int picwrite(unsigned int c) +{ int i; + int mask; + + mask=0x01; + + if(picmode&PICCMDMSK) // we got a command + { switch(picmode&PICMODEMSK) // make it future proof + { case PICMODE6: for(i=0; i<6; i++) + { bbH(CLK, PICSPEED/4); + if(c&mask) + { bbH(MOSI, PICSPEED/4); + } + else + { bbL(MOSI, PICSPEED/4); + } + bbL(CLK, PICSPEED/4); + bbL(MOSI, PICSPEED/4); // both dat and clk low + mask<<=1; + } + break; + case PICMODE4: for(i=0; i<4; i++) + { bbH(CLK, PICSPEED/4); + if(c&mask) + { bbH(MOSI, PICSPEED/4); + } + else + { bbL(MOSI, PICSPEED/4); + } + bbL(CLK, PICSPEED/4); + bbL(MOSI, PICSPEED/4); // both dat and clk low + mask<<=1; + } + break; + default: //bpWline("unknown"); + BPMSG1078; + return 0; + } + bpDelayMS(piccmddelay); + } + else // send data + { switch(picmode&PICMODEMSK) // make it future proof + { case PICMODE6: bbH(CLK, PICSPEED/4); // send leading 0 + bbL(MOSI, PICSPEED/4); + bbL(CLK, PICSPEED/4); + bbL(CLK, PICSPEED/4); + for(i=0; i<14; i++) // 14 bits + { bbH(CLK, PICSPEED/4); + if(c&mask) + { bbH(MOSI, PICSPEED/4); + } + else + { bbL(MOSI, PICSPEED/4); + } + bbL(CLK, PICSPEED/4); + bbL(MOSI, PICSPEED/4); // both dat and clk low + mask<<=1; + } + bbH(CLK, PICSPEED/4); // send trailing 0 + bbL(MOSI, PICSPEED/4); + bbL(CLK, PICSPEED/4); + bbL(CLK, PICSPEED/4); + break; + case PICMODE4: for(i=0; i<16; i++) // does 16 bits at a time + { bbH(CLK, PICSPEED/4); + if(c&mask) + { bbH(MOSI, PICSPEED/4); + } + else + { bbL(MOSI, PICSPEED/4); + } + bbL(CLK, PICSPEED/4); + bbL(MOSI, PICSPEED/4); // both dat and clk low + mask<<=1; + } + break; + default: //bpWline("unknown"); + BPMSG1078; + return 0; + } + } + return 0x100; // no data to display +} + +void picmacro(unsigned int macro) +{ unsigned int temp; + int i; + + switch(macro) + { case 0: //bpWline("(1) get devID"); + BPMSG1079; + break; + case 1: switch(picmode&PICMODEMSK) + { case PICMODE6: bpConfig.quiet=1; // turn echoing off + picstart(); + picwrite(0); + picstop(); + picwrite(0); // advance to 0x2006 (devid) + picstart(); + for(i=0; i<6; i++) + { picwrite(6); + } + picwrite(4); + picstop(); + temp=picread(); + bpConfig.quiet=0; // turn echoing on + //bpWstring("DevID = "); + BPMSG1080; + bpWinthex(temp>>5); + //bpWstring(" Rev = "); + BPMSG1081; + bpWhex(temp&0x1f); + bpBR; + break; + case PICMODE4: + default: //bpWline("Not implemented (yet)"); + BPMSG1082; + } + //bpWline("Please exit PIC programming mode"); + BPMSG1083; + break; + default: //bpWmessage(MSG_ERROR_MACRO); + BPMSG1016; + } +} + +void picpins(void) { + #if defined(BUSPIRATEV4) + BPMSG1262; //bpWline("-\t-\tPGC\tPGD"); + #else + BPMSG1232; //bpWline("PGC\tPGD\t-\t-"); + #endif +} + +/* +0000 0000 return to main +0000 0001 id=PIC1 +0000 0010 6b cmd +0000 0011 4b cmd +0000 01xx xx ms delay + +0000 1xxx unimplemented + +0001 0xyz PWM|VREG|PULLUP +0001 1xyz AUX|MISO|CS + +01xx xxxx just send cmd xxxxxxx +10xx xxxx send cmd xxxxxxx and next two bytes (14/16 bits) +11xx xxxx send cmd xxxxxxx and read two bytes + +*/ + +void binpic(void) +{ unsigned char cmd; + int ok; + unsigned int temp; + + bpWstring("PIC1"); + modeConfig.HiZ=1; // to allow different Vcc + bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp + picmode=PICMODE6; + piccmddelay=2; + + while(1) + { cmd=UART1RX(); + + switch(cmd&0xC0) + { case 0x00: ok=1; + switch(cmd&0xF0) + { case 0x00: switch(cmd) + { case 0x00: return; + case 0x01: bpWstring("PIC1"); + break; + case 0x02: picmode=PICMODE6; + break; + case 0x03: picmode=PICMODE4; + break; + case 0x04: + case 0x05: + case 0x06: + case 0x07: piccmddelay=(cmd-0x04); + break; + default: ok=0; + } + break; + case 0x10: if(cmd&0x08) + { if(cmd&0x04) + { bbH(AUX ,5); + } + else + { bbL(AUX ,5); + } + if(cmd&0x02) + { bbH(MISO ,5); + } + else + { bbL(MISO ,5); + } + if(cmd&0x01) + { bbH(CS ,5); + } + else + { bbL(CS ,5); + } + } + else + { if(cmd&0x04) // pwm? + { PWMfreq=100; + PWMduty=50; + updatePWM(); + } + else + { PWMfreq=0; + updatePWM(); + } + if(cmd&0x02) // vreg on + { BP_VREG_ON(); + //modeConfig.vregEN=1; + } + else + { BP_VREG_OFF(); + //modeConfig.vregEN=0; + } + if(cmd&0x01) // pullup on +#ifndef BUSPIRATEV1A + { BP_PULLUP_ON(); +// modeConfig.pullupEN=1; + } + else + { BP_PULLUP_OFF(); +// modeConfig.pullupEN=0; + } +#endif + } + break; + default: ok=0; + } + if(ok) + { UART1TX(1); + } + else + { UART1TX(0); + } + break; + case 0x40: picmode|=PICCMD; + picwrite(cmd&0x3F); + picmode&=PICMODEMSK; + UART1TX(1); + break; + case 0x80: picmode|=PICCMD; + picwrite(cmd&0x3F); + picmode&=PICMODEMSK; + temp=UART1RX(); + temp<<=8; + temp|=UART1RX(); + picwrite(temp); + UART1TX(1); + break; + case 0xC0: picmode|=PICCMD; + picwrite(cmd&0x3F); + picmode&=PICMODEMSK; + UART1TX(1); + temp=picread(); + UART1TX(temp>>8); + UART1TX(temp&0x0FF); + break; + } + } +} +#endif diff --git a/Firmware/pic.h b/Firmware/pic.h index a48b6e62..0de6b34d 100644 --- a/Firmware/pic.h +++ b/Firmware/pic.h @@ -1,49 +1,49 @@ - -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Initial written by Chris van Dongen, 2010. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -/* - -these are lowlevel interface subs for programming pics - -supported platforms (should) are pic10, pic12, pic14, pic16 and pic18 - -{ 0xff } or [ 0xff] sends 4 byte cmd (pic18) or 6 byte cmd (pic10, 12, 14, 16) - -*/ - - -#define PICMODE6 6 -#define PICMODE4 4 - -#define PICSPEED 100 // +- 5KHz - -#define PICCMD 1 -#define PICDATA 0 -#define PICMODEMSK 0xFE -#define PICCMDMSK 0x01 - -void picinit(void); -void picinit_exc(void); -void picstart(void); -void picstop(void); -unsigned int picread(void); -unsigned int picwrite(unsigned int c); -void piccleanup(void); -void binpic(void); -void picmacro(unsigned int macro); -void picpins(void); - - + +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Initial written by Chris van Dongen, 2010. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +/* + +these are lowlevel interface subs for programming pics + +supported platforms (should) are pic10, pic12, pic14, pic16 and pic18 + +{ 0xff } or [ 0xff] sends 4 byte cmd (pic18) or 6 byte cmd (pic10, 12, 14, 16) + +*/ + + +#define PICMODE6 6 +#define PICMODE4 4 + +#define PICSPEED 100 // +- 5KHz + +#define PICCMD 1 +#define PICDATA 0 +#define PICMODEMSK 0xFE +#define PICCMDMSK 0x01 + +void picinit(void); +void picinit_exc(void); +void picstart(void); +void picstop(void); +unsigned int picread(void); +unsigned int picwrite(unsigned int c); +void piccleanup(void); +void binpic(void); +void picmacro(unsigned int macro); +void picpins(void); + + diff --git a/Firmware/prj_usb_config.h b/Firmware/prj_usb_config.h index 8fabb77f..094b441f 100644 --- a/Firmware/prj_usb_config.h +++ b/Firmware/prj_usb_config.h @@ -1,49 +1,49 @@ -#ifndef PRJ_USB_PROFILE_H -#define PRJ_USB_PROFILE_H - - - -#define CLOCK_FREQ 32000000 -#define BAUDCLOCK_FREQ 16000000 -#define UART_BAUD_setup(x) U1BRG = x -#define CDC_FLUSH_MS 4 // how many ms timeout before cdc in to host is sent - -#define USB_INTERRUPTS 1 - -#define USB_VID (0x4d8) -#define USB_PID (0xFB00) // BPv4 -#define USB_DEV 0x0002 - -#define USB_NUM_CONFIGURATIONS 1u -#define USB_NUM_INTERFACES 2u -#define USB_NUM_ENDPOINTS 3u - - -#define MAX_EPNUM_USED 2u - -#define USB_BUS_POWERED 1 -#define USB_INTERNAL_TRANSCIEVER 1 -#define USB_INTERNAL_PULLUPS 1 -#define USB_INTERNAL_VREG 1 -#define USB_FULL_SPEED_DEVICE 1 - -/* PingPong Buffer Mode - * Valid values - * 0 - No PingPong Buffers - * 1 - PingPong on EP0 - * 2 - PingPong on all EP - * 3 - PingPong on all except EP0 - */ -#define USB_PP_BUF_MODE 0 -#define USB_EP0_BUFFER_SIZE 8u -#define CDC_BUFFER_SIZE 64u -#define CDC_NOTICE_BUFFER_SIZE 10u - -/* Low Power Request - * Optional user supplied subroutine to set the circuit - * into low power mode during usb suspend. - * Probably needed when bus powered as only 2.5mA should - * be drawn from bus i suspend mode */ -//#define usb_low_power_request() Nop() - -#endif +#ifndef PRJ_USB_PROFILE_H +#define PRJ_USB_PROFILE_H + + + +#define CLOCK_FREQ 32000000 +#define BAUDCLOCK_FREQ 16000000 +#define UART_BAUD_setup(x) U1BRG = x +#define CDC_FLUSH_MS 4 // how many ms timeout before cdc in to host is sent + +#define USB_INTERRUPTS 1 + +#define USB_VID (0x4d8) +#define USB_PID (0xFB00) // BPv4 +#define USB_DEV 0x0002 + +#define USB_NUM_CONFIGURATIONS 1u +#define USB_NUM_INTERFACES 2u +#define USB_NUM_ENDPOINTS 3u + + +#define MAX_EPNUM_USED 2u + +#define USB_BUS_POWERED 1 +#define USB_INTERNAL_TRANSCIEVER 1 +#define USB_INTERNAL_PULLUPS 1 +#define USB_INTERNAL_VREG 1 +#define USB_FULL_SPEED_DEVICE 1 + +/* PingPong Buffer Mode + * Valid values + * 0 - No PingPong Buffers + * 1 - PingPong on EP0 + * 2 - PingPong on all EP + * 3 - PingPong on all except EP0 + */ +#define USB_PP_BUF_MODE 0 +#define USB_EP0_BUFFER_SIZE 8u +#define CDC_BUFFER_SIZE 64u +#define CDC_NOTICE_BUFFER_SIZE 10u + +/* Low Power Request + * Optional user supplied subroutine to set the circuit + * into low power mode during usb suspend. + * Probably needed when bus powered as only 2.5mA should + * be drawn from bus i suspend mode */ +//#define usb_low_power_request() Nop() + +#endif diff --git a/Firmware/procMenu.c b/Firmware/procMenu.c index 94f2c080..9fc3f60d 100644 --- a/Firmware/procMenu.c +++ b/Firmware/procMenu.c @@ -1,1948 +1,1948 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -#include "AUXpin.h" -#include "busPirateCore.h" -#include "procMenu.h" //need our public versionInfo() function -#ifndef BUSPIRATEV1A -#include "selftest.h" -#endif -#include "binIO.h" -#include "SUMP.h" -#include "basic.h" - -extern struct _bpConfig bpConfig; -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; -extern proto protos[MAXPROTO]; - -#ifdef BUSPIRATEV4 -extern volatile BYTE cdc_Out_len; -#endif - -void walkdungeon(void); - -void setMode(void); //change protocol/bus mode -void setDisplayMode(void); //user terminal number display mode dialog (eg HEX, DEC, BIN, RAW) -void setPullups(void); //pullup resistor dialog -void measureSupplyVoltages(void); //display supply voltage measurements for 3.3, 5, and vpullup -void setBitOrder(void); //LSB/MSB menu for modes that allow it -void setAltAuxPin(void); //configure AUX syntax to control AUX or CS pin -void setBaudRate(void); //configure user terminal side UART baud rate -void statusInfo(void); //display properties of the current bus mode (pullups, vreg, lsb, output type, etc) -void convert(void); //convert input to HEX/DEC/BIN -void pinDirection(unsigned int pin); -void pinState(unsigned int pin); -void pinStates(void); - -#ifdef BUSPIRATEV4 -void setPullupVoltage(void); // onboard Vpu selection -#endif - -//global vars move to bpconfig structure? -char cmdbuf[CMDBUFLEN]; -unsigned int cmdend; -unsigned int cmdstart; -int cmderror; - -//int currentproto; // port to the other way :) - -#define USRMACROS 5 -#define USRMACROLEN 32 - -char usrmacros[USRMACROS][USRMACROLEN]; -int usrmacro; - -void serviceuser(void) { - int cmd, stop; - int newstart; - int oldstart; - unsigned int sendw, received; - int repeat; - unsigned char c; - int temp; - int temp2; - int binmodecnt; - int numbits; - unsigned int tmpcmdend, histcnt, tmphistcnt; - - // init - cmd = 0; - cmdstart = 0; - cmdend = 0; - tmpcmdend = cmdend; - histcnt = 0; - tmphistcnt = 0; - bpConfig.busMode = HIZ; - temp2 = 0; - cmderror = 0; // we don't want to start with error do we? - binmodecnt = 0; - - for (repeat = 0; repeat < USRMACROS; repeat++) { - for (temp = 0; temp < 32; temp++) { - usrmacros[repeat][temp] = 0; - } - } - usrmacro = 0; - - while (1) { - bpWstring(protos[bpConfig.busMode].protocol_name); - if (bpConfig.basic) { //bpWstring("(BASIC)"); - BPMSG1084; - } - bpWstring(">"); - while (!cmd) { - if (usrmacro) { - usrmacro--; - temp = 0; - while (usrmacros[usrmacro][temp]) { - cmdbuf[cmdend] = usrmacros[usrmacro][temp]; - UART1TX(usrmacros[usrmacro][temp]); - cmdend++; - temp++; - cmdend &= CMDLENMSK; - } - usrmacro = 0; - } - - while (!UART1RXRdy()) // as long as there is no user input poll periodicservice - { - if (modeConfig.periodicService == 1) { - if (protos[bpConfig.busMode].protocol_periodic()) // did we print something? - { - bpWstring(protos[bpConfig.busMode].protocol_name); - bpWstring(">"); - if (cmdstart != cmdend) { - for (temp = cmdstart; temp != cmdend; temp++) { - UART1TX(cmdbuf[temp]); - temp &= CMDLENMSK; - } - } - } - } -#ifdef BUSPIRATEV4 - if (!BP_BUTTON) { // button pressed - } else { // button not pressed - } -#endif - } - - if (CheckCommsError()) { //check for user terminal buffer overflow error - ClearCommsError(); - continue; //resume getting more user input - } else { - c = UART1RX(); //no error, process byte - } - - switch (c) { - case 0x08: // backspace(^H) - if (tmpcmdend != cmdstart) // not at begining? - { - if (tmpcmdend == cmdend) // at the end? - { - cmdend = (cmdend - 1) & CMDLENMSK; - cmdbuf[cmdend] = 0x00; // add end marker - tmpcmdend = cmdend; // update temp - bpWstring("\x08 \x08"); // destructive backspace ian !! :P - } else // not at end, left arrow used - { - repeat = 0; // use as temp, not valid here anyway - tmpcmdend = (tmpcmdend - 1) & CMDLENMSK; - bpWstring("\x1B[D"); // move left - for (temp = tmpcmdend; temp != cmdend; temp = (temp + 1) & CMDLENMSK) { - cmdbuf[temp] = cmdbuf[temp + 1]; - if (cmdbuf[temp]) // not NULL - UART1TX(cmdbuf[temp]); - else - UART1TX(0x20); - repeat++; - } - cmdend = (cmdend - 1) & CMDLENMSK; // end pointer moves left one - bpWstring("\x1B["); // move left - bpWdec(repeat); // to original - bpWstring("D"); // cursor position - } - } else { - UART1TX(BELL); // beep, at begining - } - break; - case 0x04: // delete (^D) - case 0x7F: // delete key - if (tmpcmdend != cmdend) // not at the end - { - repeat = 0; // use as temp, not valid here anyway - for (temp = tmpcmdend; temp != cmdend; temp = (temp + 1) & CMDLENMSK) { - cmdbuf[temp] = cmdbuf[temp + 1]; - if (cmdbuf[temp]) // not NULL - UART1TX(cmdbuf[temp]); - else - UART1TX(0x20); - repeat++; - } - cmdend = (cmdend - 1) & CMDLENMSK; // end pointer moves left one - bpWstring("\x1B["); // move left - bpWdec(repeat); // to original - bpWstring("D"); // cursor position - } else { - UART1TX(BELL); // beep, at end - } - break; - case 0x1B: // escape - c = UART1RX(); // get next char - if (c == '[') // got CSI - { - c = UART1RX(); // get next char - switch (c) { - case 'D': // left arrow - goto left; - break; - case 'C': // right arrow - goto right; - break; - case 'A': // up arrow - goto up; - break; - case 'B': // down arrow - goto down; - break; - case '1': // VT100+ home key (example use in PuTTY) - c = UART1RX(); - if (c == '~') goto home; - break; - case '4': // VT100+ end key (example use in PuTTY) - c = UART1RX(); - if (c == '~') goto end; - break; - } - } - break; -left: - case 0x02: // ^B (left arrow) or SUMP - if (binmodecnt >= 5) { - SUMP(); - binmodecnt = 0; // do we get here or not? - } else // ^B (left arrow) - { - if (tmpcmdend != cmdstart) // at the begining? - { - tmpcmdend = (tmpcmdend - 1) & CMDLENMSK; - bpWstring("\x1B[D"); // move left - } else { - UART1TX(BELL); // beep, at begining - } - } - break; -right: - case 0x06: // ^F (right arrow) - if (tmpcmdend != cmdend) // ^F (right arrow) - { // ensure not at end - tmpcmdend = (tmpcmdend + 1) & CMDLENMSK; - bpWstring("\x1B[C"); // move right - } else { - UART1TX(BELL); // beep, at end - } - break; -up: - case 0x10: // ^P (up arrow) - tmphistcnt = 0; // reset counter - for (temp = (cmdstart - 1) & CMDLENMSK; temp != cmdend; temp = (temp - 1) & CMDLENMSK) { - if (!cmdbuf[temp] && cmdbuf[(temp - 1) & CMDLENMSK]) { // found previous entry, temp is old cmdend - tmphistcnt++; - if (tmphistcnt > histcnt) { - histcnt++; - if (cmdstart != cmdend) { // clear partially entered cmd line - while (cmdend != cmdstart) { - cmdbuf[cmdend] = 0x00; - cmdend = (cmdend - 1) & CMDLENMSK; - } - cmdbuf[cmdend] = 0x00; - } - repeat = (temp - 1) & CMDLENMSK; - while (repeat != cmdend) { - if (!cmdbuf[repeat]) { - temp2 = (repeat + 1) & CMDLENMSK; - /* start of old cmd */ - break; - } - repeat = (repeat - 1) & CMDLENMSK; - } - bpWstring("\x1B[2K\x0D"); // clear line, CR - bpWstring(protos[bpConfig.busMode].protocol_name); - if (bpConfig.basic) BPMSG1084; - bpWstring(">"); - for (repeat = temp2; repeat != temp; repeat = (repeat + 1) & CMDLENMSK) { - UART1TX(cmdbuf[repeat]); - cmdbuf[cmdend] = cmdbuf[repeat]; - cmdend = (cmdend + 1) & CMDLENMSK; - } - cmdbuf[cmdend] = 0x00; - tmpcmdend = cmdend; // resync - break; - } - } - } - if (temp == cmdend) UART1TX(BELL); // beep, top - break; -down: - case 0x0E: // ^N (down arrow) - tmphistcnt = 0; // reset counter - for (temp = (cmdstart - 1) & CMDLENMSK; temp != cmdend; temp = (temp - 1) & CMDLENMSK) { - if (!cmdbuf[temp] && cmdbuf[(temp - 1) & CMDLENMSK]) { // found previous entry, temp is old cmdend - tmphistcnt++; - if (tmphistcnt == (histcnt - 1)) { - histcnt--; - if (cmdstart != cmdend) { // clear partially entered cmd line - while (cmdend != cmdstart) { - cmdbuf[cmdend] = 0x00; - cmdend = (cmdend - 1) & CMDLENMSK; - } - cmdbuf[cmdend] = 0x00; - } - repeat = (temp - 1) & CMDLENMSK; - while (repeat != cmdend) { - if (!cmdbuf[repeat]) { - temp2 = (repeat + 1) & CMDLENMSK; - /* start of old cmd */ - break; - } - repeat = (repeat - 1) & CMDLENMSK; - } - bpWstring("\x1B[2K\x0D"); // clear line, CR - bpWstring(protos[bpConfig.busMode].protocol_name); - if (bpConfig.basic) BPMSG1084; - bpWstring(">"); - for (repeat = temp2; repeat != temp; repeat = (repeat + 1) & CMDLENMSK) { - UART1TX(cmdbuf[repeat]); - cmdbuf[cmdend] = cmdbuf[repeat]; - cmdend = (cmdend + 1) & CMDLENMSK; - } - cmdbuf[cmdend] = 0x00; - tmpcmdend = cmdend; // resync - break; - } - } - } - if (temp == cmdend) { - if (histcnt == 1) { - bpWstring("\x1B[2K\x0D"); // clear line, CR - bpWstring(protos[bpConfig.busMode].protocol_name); - if (bpConfig.basic) BPMSG1084; - bpWstring(">"); - while (cmdend != cmdstart) { - cmdbuf[cmdend] = 0x00; - cmdend = (cmdend - 1) & CMDLENMSK; - } - cmdbuf[cmdend] = 0x00; - tmpcmdend = cmdend; // resync - histcnt = 0; - } else UART1TX(BELL); // beep, top - } - break; -home: - case 0x01: // ^A (goto begining of line) - if (tmpcmdend != cmdstart) { - repeat = (tmpcmdend - cmdstart) & CMDLENMSK; - bpWstring("\x1B["); // move left - bpWdec(repeat); // to start - bpWstring("D"); // of command line - tmpcmdend = cmdstart; - } else { - UART1TX(BELL); // beep, at start - } - break; -end: - case 0x05: // ^E (goto end of line) - if (tmpcmdend != cmdend) { - repeat = (cmdend - tmpcmdend) & CMDLENMSK; - bpWstring("\x1B["); // move right - bpWdec(repeat); // to end - bpWstring("C"); // of command line - tmpcmdend = cmdend; - } else { - UART1TX(BELL); // beep, at end - } - break; - case 0x0A: // Does any terminal only send a CR? - case 0x0D: // Enter pressed (LF) - cmd = 1; // command received - histcnt = 0; // reset counter - cmdbuf[cmdend] = 0x00; // use to find history - cmdend = (cmdend + 1) & CMDLENMSK; - tmpcmdend = cmdend; // resync - bpWline(""); - break; - case 0x00: - binmodecnt++; - if (binmodecnt == 20) { - binBB(); -#if defined (BUSPIRATEV4) - binmodecnt = 0; //no reset, cleanup manually - goto bpv4reset; //versionInfo(); //and simulate reset for dependent apps (looking at you AVR dude!) -#endif - } - break; - default: - if ((((cmdend + 1) & CMDLENMSK) != cmdstart) && (c >= 0x20) && (c < 0x7F)) { // no overflow and printable - if (cmdend == tmpcmdend) // adding to the end - { - UART1TX(c); // echo back - cmdbuf[cmdend] = c; // store char - cmdend = (cmdend + 1) & CMDLENMSK; - cmdbuf[cmdend] = 0x00; // add end marker - tmpcmdend = cmdend; // update temp - } else // not at end, left arrow used - { - repeat = (cmdend - tmpcmdend) & CMDLENMSK; - bpWstring("\x1B["); // move right - bpWdec(repeat); // to end - bpWstring("C"); // of line - temp = cmdend; - while (temp != ((tmpcmdend - 1) & CMDLENMSK)) { - cmdbuf[temp + 1] = cmdbuf[temp]; - if (cmdbuf[temp]) // not NULL - { - UART1TX(cmdbuf[temp]); - bpWstring("\x1B[2D"); // left 2 - } - temp = (temp - 1) & CMDLENMSK; - } - UART1TX(c); // echo back - cmdbuf[tmpcmdend] = c; // store char - tmpcmdend = (tmpcmdend + 1) & CMDLENMSK; - cmdend = (cmdend + 1) & CMDLENMSK; - } - } else { - UART1TX(BELL); // beep, overflow or non printable - } //default: - } //switch(c) - } //while(!cmd) - - newstart = cmdend; - oldstart = cmdstart; - cmd = 0; - - // bpWline("\x0a\x0dreceived enter, processing input"); - // bpWline("cmdbuf[]="); - - // for(i=0; i"); //was printed twice -#else - BPMSG1093; - while (0 == UART1TXRdy()); //wait untill TX finishes - asm("RESET"); - //} -#endif - break; - case '$': //bpWline("-bootloader jump"); - if (agree()) { //bpWline("BOOTLOADER"); - BPMSG1094; - bpDelayMS(100); - bpInit(); // turn off nasty things, cleanup first needed? - while (0 == UART1TXRdy()); //wait untill TX finishes - asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location - "goto w1 \n"); - } - break; - case 'a': //bpWline("-AUX low"); - repeat = getrepeat() + 1; - while (--repeat) bpAuxLow(); - break; - case 'A': //bpWline("-AUX hi"); - repeat = getrepeat() + 1; - while (--repeat) bpAuxHigh(); - break; - case '@': //bpWline("-Aux read"); - repeat = getrepeat() + 1; - while (--repeat) { //bpWstring(OUMSG_AUX_INPUT_READ); - BPMSG1095; - bpEchoState(bpAuxRead()); - bpWBR; - } - break; - case 'W': //bpWline("-PSU on"); //enable any active power supplies - if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); - BPMSG1088; - } else { - BP_VREG_ON(); - ADCON(); // turn ADC ON - bpDelayMS(2); //wait for VREG to come up - - if ((bpADC(BP_ADC_3V3) > V33L) && (bpADC(BP_ADC_5V0) > V5L)) { //voltages are correct - //bpWmessage(MSG_VREG_ON); - BPMSG1096; - bpBR; - //modeConfig.vregEN=1; - - //Engaging Clutch - //finishes the settup and conects the pins... - protos[bpConfig.busMode].protocol_setup_exc(); - bpWline("Clutch engaged!!!"); - } else { - BP_VREG_OFF(); - bpWline("VREG too low, is there a short?"); - BPMSG1097; - bpBR; - } - ADCOFF(); // turn ADC OFF - } - break; - case 'w': //bpWline("-PSU off"); //disable the power supplies - if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); - BPMSG1088; - } else { - //disengaging Clutch - //cleansup the protocol and HiZs the pins - protos[bpConfig.busMode].protocol_cleanup(); - bpWline("Clutch disengaged!!!"); - - BP_VREG_OFF(); - //bpWmessage(MSG_VREG_OFF); - BPMSG1097; - bpBR; - //modeConfig.vregEN=0; - } - break; - case 'd': //bpWline("-read ADC"); //do an adc reading - //bpWstring(OUMSG_PS_ADC_VOLT_PROBE); - BPMSG1044; - bpADCprobe(); - //bpWline(OUMSG_PS_ADC_VOLTS);` - BPMSG1045; - bpBR; - break; - case 'D': //bpWline("-DVM mode"); //dumb voltmeter mode - bpADCCprobe(); - break; - case '&': //bpWline("-delay 1ms"); - repeat = getrepeat(); - //bpWstring(OUMSG_PS_DELAY); - BPMSG1099; - bpWintdec(repeat); - //bpWline(OUMSG_PS_DELAY_US); - BPMSG1100; - bpDelayUS(repeat); - break; - case '%': repeat = getrepeat(); - BPMSG1099; - bpWintdec(repeat); - BPMSG1212; - bpDelayMS(repeat); - break; - case '+': //bpWline("-easter egg"); - //easterEgg(); - break; -#ifdef BP_USE_BASIC - case 's': //bpWline("Listing:"); - bpConfig.basic = 1; - break; -#endif - case 'S': //servo control - if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); - BPMSG1088; - } else { - bpServo(); - } - break; - case '<': cmderror = 1; - temp = 1; - - while (cmdbuf[((cmdstart + temp) & CMDLENMSK)] != 0x00) { - if (cmdbuf[((cmdstart + temp) & CMDLENMSK)] == '>') cmderror = 0; // clear error if we found a > before the command ends - temp++; - } - if (temp >= (USRMACROLEN + 3)) cmderror = 1; // too long (avoid overflows) - if (!cmderror) { - cmdstart = (cmdstart + 1) & CMDLENMSK; - temp = getint(); - if (cmdbuf[((cmdstart) & CMDLENMSK)] == '=') // assignment - { - if ((temp > 0) && (temp <= USRMACROS)) { - cmdstart = (cmdstart + 1) & CMDLENMSK; - temp--; - for (repeat = 0; repeat < USRMACROLEN; repeat++) { - usrmacros[temp][repeat] = 0; - } - repeat = 0; - while (cmdbuf[cmdstart] != '>') { - usrmacros[temp][repeat] = cmdbuf[cmdstart]; - repeat++; - cmdstart = (cmdstart + 1) & CMDLENMSK; - } - } else { - cmderror = 1; - } - } else { - if (temp == 0) { - for (repeat = 0; repeat < USRMACROS; repeat++) { - bpWdec(repeat + 1); - bpWstring(". <"); - bpWstring(usrmacros[repeat]); - bpWline(">"); - } - } else if ((temp > 0) && (temp <= USRMACROS)) { //bpWstring("execute : "); - //BPMSG1236; - //bpWdec(temp-1); - bpBR; - usrmacro = temp; - } else { - cmderror = 1; - } - } - } - break; - // command for subsys (i2c, UART, etc) - case '(': //bpWline("-macro"); - cmdstart = (cmdstart + 1) & CMDLENMSK; - sendw = getint(); - consumewhitechars(); - if (cmdbuf[((cmdstart) & CMDLENMSK)] == ')') { //cmdstart++; // skip ) - //cmdstart&=CMDLENMSK; - //bpWdec(sendw); - protos[bpConfig.busMode].protocol_macro(sendw); - bpBR; - } else { - cmderror = 1; - } - break; - case 0x22: //bpWline("-send string"); - cmderror = 1; - temp = 1; - - while (cmdbuf[((cmdstart + temp) & CMDLENMSK)] != 0x00) { - if (cmdbuf[((cmdstart + temp) & CMDLENMSK)] == 0x22) cmderror = 0; // clear error if we found a " before the command ends - temp++; - } - - if (!cmderror) { - BPMSG1101; - UART1TX(0x22); - while (cmdbuf[((++cmdstart) & CMDLENMSK)] != 0x22) { - cmdstart &= CMDLENMSK; - UART1TX(cmdbuf[cmdstart]); - sendw = cmdbuf[cmdstart]; - if (modeConfig.lsbEN == 1) //adjust bitorder - { - sendw = bpRevByte(sendw); - } - protos[bpConfig.busMode].protocol_send(sendw); - } - cmdstart &= CMDLENMSK; - UART1TX(0x22); - bpBR; - } - break; - case '[': //bpWline("-Start"); - protos[bpConfig.busMode].protocol_start(); - break; - case '{': //bpWline("-StartR"); - protos[bpConfig.busMode].protocol_startR(); - break; - case ']': //bpWline("-Stop"); - protos[bpConfig.busMode].protocol_stop(); - break; - case '}': //bpWline("-StopR"); - protos[bpConfig.busMode].protocol_stopR(); - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': //bpWline("-Send"); - //bpWmessage(MSG_WRITE); - BPMSG1101; - sendw = getint(); - cmdstart--; - cmdstart &= CMDLENMSK; - repeat = getrepeat() + 1; - numbits = getnumbits(); - if (numbits) { - modeConfig.numbits = numbits; - if (numbits > 8) modeConfig.int16 = 1; - else modeConfig.int16 = 0; - } - while (--repeat) { - bpWbyte(sendw); - if (((modeConfig.int16 == 0) && (modeConfig.numbits != 8)) || ((modeConfig.int16 == 1) && (modeConfig.numbits != 16))) { - UART1TX(';'); - bpWdec(modeConfig.numbits); - } - if (modeConfig.lsbEN == 1) {//adjust bitorder - sendw = bpRevByte(sendw); - } - received = protos[bpConfig.busMode].protocol_send(sendw); - bpSP; - if (modeConfig.wwr) { //bpWmessage(MSG_READ); - BPMSG1102; - if (modeConfig.lsbEN == 1) {//adjust bitorder - received = bpRevByte(received); - } - bpWbyte(received); - bpSP; - } - } - bpBR; - break; - case 'r': //bpWline("-Read"); - //bpWmessage(MSG_READ); - BPMSG1102; - //newDmode = 0; - newDmode = changeReadDisplay(); - repeat = getrepeat() + 1; - numbits = getnumbits(); - if (numbits) { - modeConfig.numbits = numbits; - if (numbits > 8) modeConfig.int16 = 1; - else modeConfig.int16 = 0; - } - if(newDmode) - { - oldDmode = bpConfig.displayMode; - bpConfig.displayMode = newDmode-1; - } - while (--repeat) { - received = protos[bpConfig.busMode].protocol_read(); - if (modeConfig.lsbEN == 1) {//adjust bitorder - received = bpRevByte(received); - } - bpWbyte(received); - if (((modeConfig.int16 == 0) && (modeConfig.numbits != 8)) || ((modeConfig.int16 == 1) && (modeConfig.numbits != 16))) { - UART1TX(';'); - bpWdec(modeConfig.numbits); - } - bpSP; - } - if(newDmode) - { - bpConfig.displayMode = oldDmode; - newDmode=0; - } - bpBR; - break; - case '/': //bpWline("-CLK hi"); - //repeat=getrepeat()+1; - //while(--repeat) - //{ //bpWmessage(MSG_BIT_CLKH); - BPMSG1103; - protos[bpConfig.busMode].protocol_clkh(); - //} - break; - case '\\': //bpWline("-CLK lo"); - //repeat=getrepeat()+1; - //while(--repeat) - //{ //bpWmessage(MSG_BIT_CLKL); - BPMSG1104; - protos[bpConfig.busMode].protocol_clkl(); - //} - break; - case '-': //bpWline("-DAT hi"); - //repeat=getrepeat()+1; - //while(--repeat) - //{ //bpWmessage(MSG_BIT_DATH); - BPMSG1105; - protos[bpConfig.busMode].protocol_dath(); - //} - break; - case '_': //bpWline("-DAT lo"); - //repeat=getrepeat()+1; - //while(--repeat) - //{ //bpWmessage(MSG_BIT_DATL); - BPMSG1106; - protos[bpConfig.busMode].protocol_datl(); - //} - break; - case '.': //bpWline("-DAT state read"); - //repeat=getrepeat()+1; - BPMSG1098; - //while(--repeat) - { - bpEchoState(protos[bpConfig.busMode].protocol_dats()); - //bpWmessage(MSG_BIT_NOWINPUT); - //BPMSG1107; - } - break; - case '^': //bpWline("-CLK pulse"); - repeat = getrepeat(); - BPMSG1108; - bpWbyte(repeat); - repeat++; - while (--repeat) { //bpWmessage(MSG_BIT_CLK); - protos[bpConfig.busMode].protocol_clk(); - } - bpBR; - break; - case '!': //bpWline("-bit read"); - repeat = getrepeat() + 1; - BPMSG1109; - while (--repeat) { //bpWmessage(MSG_BIT_READ); - bpEchoState(protos[bpConfig.busMode].protocol_bitr()); - bpSP; - } - //bpWmessage(MSG_BIT_NOWINPUT); - BPMSG1107; - break; - // white char/delimeters - case 0x00: - case 0x0D: // not necessary but got random error msg at end, just to be sure - case 0x0A: // same here - case ' ': - case ',': break; // no match so it is an error - default: cmderror = 1; - } //switch(c) - cmdstart = (cmdstart + 1) & CMDLENMSK; - - // bpWline(""); - // bpWstring("cmdstart = "); - // bpWinthex(cmdstart); - // bpWstring(" cmdend = "); - // bpWinthex(cmdend); - // bpWline(""); - - if (cmderror) { //bpWstring("Syntax error at char "); - BPMSG1110; - if (cmdstart > oldstart) // find error position :S - { - bpWdec(cmdstart - oldstart); - } else { - bpWdec((CMDBUFLEN + cmdstart) - oldstart); - } - cmderror = 0; - stop = 1; - bpBR; - } - - if (cmdstart == cmdend) stop = 1; // reached end of user input?? - } //while(!stop) - - - cmdstart = newstart; - cmdend = newstart; // 'empty' cmdbuffer - cmd = 0; - } //while(1) -} //serviceuser(void) - -int getint(void) // get int from user (accept decimal, hex (0x) or binairy (0b) -{ - int i; - int number; - - i = 0; - number = 0; - - if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x31) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // 1-9 is for sure decimal - { - number = cmdbuf[(cmdstart + i) & CMDLENMSK] - 0x30; - i++; - while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // consume all digits - { - number *= 10; - number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; - i++; - } - } else if (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 0x30) // 0 could be anything - { - i++; - if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'b') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'B')) { - i++; - while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == '1') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == '0')) { - number <<= 1; - number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; - i++; - } - } else if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'x') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'X')) { - i++; - while (((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) || \ - ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 'A') && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 'F')) || \ - ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 'a') && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 'f'))) { - number <<= 4; - if ((cmdbuf[(cmdstart + i) & CMDLENMSK] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) { - number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; - } else { - cmdbuf[((cmdstart + i) & CMDLENMSK)] |= 0x20; // make it lowercase - number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x57; // 'a'-0x61+0x0a - } - i++; - } - } else if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) { - number = cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; - while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // consume all digits - { - number *= 10; - number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; - i++; - } - } - } else // how did we come here?? - { - cmderror = 1; - return 0; - } - - cmdstart += i; // we used i chars - cmdstart &= CMDLENMSK; - return number; -} //getint(void) - -int getrepeat(void) { - int temp; - - if (cmdbuf[(cmdstart + 1) & CMDLENMSK] == ':') { - cmdstart += 2; - cmdstart &= CMDLENMSK; - temp = getint(); - cmdstart--; // to allow [6:10] (start send 6 10 times stop) - cmdstart &= CMDLENMSK; - return temp; - } - return 1; // no repeat count=1 -} // - -int getnumbits(void) { - int temp; - - if (cmdbuf[(cmdstart + 1) & CMDLENMSK] == ';') { - cmdstart = (cmdstart + 2) & CMDLENMSK; - temp = getint(); - cmdstart = (cmdstart - 1) & CMDLENMSK; // to allow [6:10] (start send 6 10 times stop) - return temp; - } - return 0; // no numbits=0; -} // - -unsigned char changeReadDisplay(void) -{ - if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'x') - { - cmdstart = (cmdstart + 1) & CMDLENMSK; - return 1; - } - if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'd') - { - cmdstart = (cmdstart + 1) & CMDLENMSK; - return 2; - } - if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'b') - { - cmdstart = (cmdstart + 1) & CMDLENMSK; - return 3; - } - if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'w') - { - cmdstart = (cmdstart + 1) & CMDLENMSK; - return 4; - } -return 0; -} - -void consumewhitechars(void) { - while (cmdbuf[cmdstart] == 0x20) { - cmdstart = (cmdstart + 1) & CMDLENMSK; // remove spaces - } -} // - -void changemode(void) { - int i, busmode; - - busmode = 0; - - // bpWline("--changemode()"); - - cmdstart = (cmdstart + 1) & CMDLENMSK; - - consumewhitechars(); - - // bpWline("whitechars stripped"); - - busmode = getint(); - - // bpWstring("numbers: busmode="); - // bpWinthex(busmode); - // bpWline(""); - - if (!busmode) // no argument entered - { - for (i = 0; i < MAXPROTO; i++) { - bpWdec(i + 1); - bpWstring(". "); - bpWline(protos[i].protocol_name); - } - //bpWline("x. exit(without change)"); - BPMSG1111; - cmderror = 0; // error is set because no number found, but it is no error here:S eeeh confusing right? - busmode = getnumber(1, 1, MAXPROTO, 1) - 1; - // bpWstring("busmode= "); - // bpWdec(busmode); - if ((busmode == -2) || (busmode == -1)) { //bpWline("no mode change"); - BPMSG1112; - } else { - protos[bpConfig.busMode].protocol_cleanup(); - bpInit(); - bpConfig.busMode = busmode; - protos[bpConfig.busMode].protocol_setup(); - bpWline("Clutch disengaged!!!"); - if (busmode) { - BP_LEDMODE = 1; // mode led is on when proto >0 - bpWline("To finish setup, start up the power supplies with command 'W'\r\n"); - } - //bpWmessage(MSG_READY); - BPMSG1085; - } - } else // number entered - { - busmode--; // save a couple of programwords to do it here :D - if (busmode < MAXPROTO) { - protos[bpConfig.busMode].protocol_cleanup(); - bpInit(); - bpConfig.busMode = busmode; - protos[bpConfig.busMode].protocol_setup(); - if (busmode) BP_LEDMODE = 1; // mode led is on when proto >0 - //bpWmessage(MSG_READY); - BPMSG1085; - } else { //bpWline("Nonexistent protocol!!"); - BPMSG1114; - } - } - cmdstart = (cmdend - 1) & CMDLENMSK; -} //changemode(void) - - -#if defined(BP_ENABLE_HISTORY) - -int cmdhistory(void) { - int i, j, k; - - int historypos[CMDHISTORY]; - - i = 1; - j = (cmdstart - 1) & CMDLENMSK; - - while (j != cmdstart) // scroll through the whole buffer - { - if ((cmdbuf[j] == 0x00) && (cmdbuf[(j + 1) & CMDLENMSK] != 0x00)) // did we find an end? is it not empty? - { - bpWdec(i); - bpWstring(". "); - k = 1; - while (cmdbuf[((j + k) & CMDLENMSK)]) { - UART1TX(cmdbuf[((j + k) & CMDLENMSK)]); // print it - k++; - } - historypos[i] = (j + 1) & CMDLENMSK; - i++; - if (i == CMDHISTORY) break; - bpWline(""); - } - j = (j - 1) & CMDLENMSK; - } - - bpWline(" "); - BPMSG1115; - - j = getnumber(0, 1, i, 1); - - if (j == -1 || !j) // x is -1, default is 0 - { - bpWline(""); - return 1; - } else { - i = 0; - while (cmdbuf[(historypos[j] + i) & CMDLENMSK]) // copy it to the end of the ringbuffer - { - cmdbuf[(cmdend + i) & CMDLENMSK] = cmdbuf[(historypos[j] + i) & CMDLENMSK]; - i++; - } - cmdstart = (cmdend - 1) & CMDLENMSK; // start will be increased before parsing in main loop - cmdend = (cmdstart + i + 2) & CMDLENMSK; - cmdbuf[(cmdend - 1) & CMDLENMSK] = 0x00; - } - return 0; -} //cmdhistory(void) -#endif - - -// gets number from input -// -1 = abort (x) -// -2 = input to much -// 0-max return -// x=1 exit is enabled (we don't want that in the mode changes ;) - -int getnumber(int def, int min, int max, int x) //default, minimum, maximum, show exit option -{ - char c; - char buf[6]; // max 4 digits; - int i, j, stop, temp, neg; - -again: // need to do it proper with whiles and ifs.. - - i = 0; - stop = 0; - temp = 0; - neg = 0; - - bpWstring("\r\n("); - if (def < 0) { - bpWstring("x"); - } else { - bpWdec(def); - } - bpWstring(")>"); - - while (!stop) { - c = UART1RX(); - switch (c) { - case 0x08: if (i) { - i--; - bpWstring("\x08 \x08"); - } else { - if (neg) { - neg = 0; - bpWstring("\x08 \x08"); - } else { - UART1TX(BELL); - } - } - break; - case 0x0A: - case 0x0D: stop = 1; - break; - case '-': if (!i) // enable negative numbers - { - UART1TX('-'); - neg = 1; - } else { - UART1TX(BELL); - } - break; - case 'x': if (x) return -1; // user wants to quit :( only if we enable it :D - default: if ((c >= 0x30) && (c <= 0x39)) // we got a digit - { - if (i > 3) // 0-9999 should be enough?? - { - UART1TX(BELL); - i = 4; - } else { - UART1TX(c); - buf[i] = c; // store user input - i++; - } - } else // ignore input :) - { - UART1TX(BELL); - } - - } - } - bpBR; - - if (i == 0) { - return def; // no user input so return default option - } else { - temp = 0; - i--; - j = i; - - for (; i >= 0; i--) { - temp *= 10; - temp += (buf[j - i] - 0x30); - } - - if ((temp >= min) && (temp <= max)) { - if (neg) { - return -temp; - } else { - return temp; - } - } else { //bpWline("\r\nInvalid choice, try again\r\n"); - BPMSG1211; - goto again; - } - } - return temp; // we dont get here, but keep compiler happy -} //getnumber(int def, int min, int max, int x) - - -#if defined(BUSPIRATEV4) -// gets number from input -// -1 = abort (x) -// -2 = input to much -// 0-max return -// x=1 exit is enabled (we don't want that in the mode changes ;) - -long getlong(long def, int min, long max, int x) //default, minimum, maximum, show exit option -{ - char c; - char buf[12]; // max long = 2147483647 so 10 - int i, j, stop, neg; - long temp; - -again: // need to do it proper with whiles and ifs.. - - i = 0; - stop = 0; - temp = 0; - neg = 0; - - bpWstring("\r\n("); - if (def < 0) { - bpWstring("x"); - } else { - bpWlongdec(def); - } - bpWstring(")>"); - - while (!stop) { - c = UART1RX(); - switch (c) { - case 0x08: if (i) { - i--; - bpWstring("\x08 \x08"); - } else { - if (neg) { - neg = 0; - bpWstring("\x08 \x08"); - } else { - UART1TX(BELL); - } - } - break; - case 0x0A: - case 0x0D: stop = 1; - break; - case '-': if (!i) // enable negative numbers - { - UART1TX('-'); - neg = 1; - } else { - UART1TX(BELL); - } - break; - case 'x': if (x) return -1; // user wants to quit :( only if we enable it :D - default: if ((c >= 0x30) && (c <= 0x39)) // we got a digit - { - if (i > 9) // 0-9999 should be enough?? - { - UART1TX(BELL); - i = 10; - } else { - UART1TX(c); - buf[i] = c; // store user input - i++; - } - } else // ignore input :) - { - UART1TX(BELL); - } - - } - } - bpBR; - - if (i == 0) { - return def; // no user input so return default option - } else { - temp = 0; - i--; - j = i; - - for (; i >= 0; i--) { - temp *= 10; - temp += (buf[j - i] - 0x30); - } - - if ((temp >= min) && (temp <= max)) { - if (neg) { - return -temp; - } else { - return temp; - } - } else { //bpWline("\r\nInvalid choice, try again\r\n"); - BPMSG1211; - goto again; - } - } - return temp; // we dont get here, but keep compiler happy -} -#endif - - - - -//print version info (used in menu and at startup in main.c) - -void versionInfo(void) { - unsigned int i; - - -#if defined (BUSPIRATEV2) //we can tell if it's v3a or v3b, show it here - bpWstring(BP_VERSION_STRING); - UART1TX('.'); - UART1TX(bpConfig.HWversion); - if (bpConfig.dev_type == 0x44F) {//sandbox electronics clone with 44pin PIC24FJ64GA004 - bpWstring(" clone w/different PIC"); - } - bpBR; -#else - bpWline(BP_VERSION_STRING); -#endif - - bpWstring(BP_FIRMWARE_STRING); - -#ifndef BP_MAIN - UART1TX('['); - for (i = 0; i < MAXPROTO; i++) { - if (i) bpSP; - bpWstring(protos[i].protocol_name); - } - UART1TX(']'); -#endif - -#ifndef BUSPIRATEV4 - //bpWstring(" Bootloader v"); - BPMSG1126; - i = bpReadFlash(0x0000, BL_ADDR_VER); - bpWdec(i >> 8); - UART1TX('.'); - bpWdec(i); -#endif - bpWline(""); - - //bpWstring("DEVID:"); - BPMSG1117; - bpWinthex(bpConfig.dev_type); - - //bpWstring(" REVID:"); - BPMSG1210; - bpWinthex(bpConfig.dev_rev); -#ifdef BUSPIRATEV4 - bpWstring(" (24FJ256GB106 "); - switch (bpConfig.dev_rev) { - case PIC_REV_A3: - bpWstring("A3"); - break; - case PIC_REV_A5: - bpWstring("A5"); - break; - default: - bpWstring("UNK"); - break; - } -#else - bpWstring(" (24FJ64GA00"); - if (bpConfig.dev_type == 0x44F) {//sandbox electronics clone with 44pin PIC24FJ64GA004 - bpWstring("4 "); - } else { - bpWstring("2 "); - } - - switch (bpConfig.dev_rev) { - case PIC_REV_A3: - bpWstring("A3"); //also A4, but that's not in the wild and makes it confusing to users - break; - case PIC_REV_B4: - bpWstring("B4"); - break; - case PIC_REV_B5: - bpWstring("B5"); - break; - case PIC_REV_B8: - bpWstring("B8"); - break; - default: - bpWstring("UNK"); - break; - } -#endif - - bpWline(")"); - //bpWline("http://dangerousprototypes.com"); - BPMSG1118; - i = 0; -} //versionInfo(void) - -//display properties of the current bus mode (pullups, vreg, lsb, output type, etc) - -void statusInfo(void) { - -#ifdef BUSPIRATEV4 - bpWstring("CFG0: "); - bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_0)); - bpSP; -#endif - - //bpWstring("CFG1:"); - BPMSG1136; - bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_1)); - //bpWstring(" CFG2:"); - BPMSG1137; - bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_2)); - bpBR; - - //bpWline("*----------*"); - BPMSG1119; - - pinStates(); - - //vreg status (was modeConfig.vregEN) - if (BP_VREGEN == 1) BPMSG1096; - else BPMSG1097; //bpWmessage(MSG_VREG_ON); else bpWmessage(MSG_VREG_OFF); - UART1TX(','); - bpSP; - -#ifndef BUSPIRATEV1A - //pullups available, enabled? - //was modeConfig.pullupEN - if (BP_PULLUP == 1) BPMSG1091; - else BPMSG1089; //bpWmessage(MSG_OPT_PULLUP_ON); else bpWmessage(MSG_OPT_PULLUP_OFF); - UART1TX(','); - bpSP; -#endif - -#ifdef BUSPIRATEV4 - if (BP_PUVSEL50_DIR == 0) bpWstring("Vpu=5V, "); - if (BP_PUVSEL33_DIR == 0) bpWstring("Vpu=3V3, "); - -#endif - - //open collector outputs? - if (modeConfig.HiZ == 1) BPMSG1120; - else BPMSG1121; // bpWmessage(MSG_STATUS_OUTPUT_HIZ); else bpWmessage(MSG_STATUS_OUTPUT_NORMAL); - - //voltage report - // measureSupplyVoltages(); - - - //bitorder toggle available, enabled - if (modeConfig.lsbEN == 0) BPMSG1123; - else BPMSG1124; //bpWmessage(MSG_OPT_BITORDER_LSB); else bpWmessage(MSG_OPT_BITORDER_MSB); - UART1TX(','); - bpSP; - - // show partial writes - //bpWline("Number of bits read/write: "); - BPMSG1252; - bpWdec(modeConfig.numbits); - bpBR; - - //AUX pin setting -#ifndef BUSPIRATEV4 - if (modeConfig.altAUX == 1) BPMSG1087; - else BPMSG1086; //bpWmessage(MSG_OPT_AUXPIN_CS); else bpWmessage(MSG_OPT_AUXPIN_AUX); -#endif -#ifdef BUSPIRATEV4 - switch (modeConfig.altAUX) { - case 0: BPMSG1087; - break; - case 1: BPMSG1086; - break; - case 2: BPMSG1263; - break; - case 3: BPMSG1264; - break; - } -#endif - - protos[bpConfig.busMode].protocol_settings(); - - //bpWline("*----------*"); - BPMSG1119; -} //statusInfo(void) - -void pinStates(void) { //bpWline("Pinstates:"); - BPMSG1226; -#if defined(BUSPIRATEV4) - BPMSG1256; //bpWstring("12.(RD)\t11.(BR)\t10.(BLK)\t9.(WT)\t8.(GR)\t7.(PU)\t6.(BL)\t5.(GN)\t4.(YW)\t3.(OR)\t2.(RD)\1.(BR)"); -#else - BPMSG1233; //bpWstring("1.(BR)\t2.(RD)\t3.(OR)\t4.(YW)\t5.(GN)\t6.(BL)\t7.(PU)\t8.(GR)\t9.(WT)\t0.(BLK)"); -#endif -#if defined(BUSPIRATEV4) - BPMSG1257; //bpWstring("GND\t5.0V\t3.3V\tVPU\tADC\tAUX2\tAUX1\tAUX\t"); -#elif defined(BUSPIRATEV25) - BPMSG1235; //bpWstring("GND\tADC\t5.0V\t3.3V\tVPU\tAUX\t"); -#else - BPMSG1227; //bpWstring("GND\t3.3V\t5.0V\tADC\tVPU\tAUX\t"); -#endif - protos[bpConfig.busMode].protocol_pins(); - BPMSG1228; //bpWstring("P\tP\tP\tI\tI\t"); -#if defined(BUSPIRATEV4) - pinDirection(AUX2); - pinDirection(AUX1); - pinDirection(AUX); - pinDirection(CS); - pinDirection(MISO); - pinDirection(CLK); - pinDirection(MOSI); -#else - pinDirection(AUX); - pinDirection(CLK); - pinDirection(MOSI); - pinDirection(CS); - pinDirection(MISO); -#endif - bpBR; - BPMSG1234; //bpWstring("GND\t"); - ADCON(); - - -#if defined(BUSPIRATEV25) - bpWvolts(bpADC(BP_ADC_PROBE)); - BPMSG1045; - UART1TX('\t'); -#elif defined(BUSPIRATEV4) - bpWvolts(bpADC(BP_ADC_5V0)); - BPMSG1045; - UART1TX('\t'); -#else - bpWvolts(bpADC(BP_ADC_3V3)); - BPMSG1045; - UART1TX('\t'); -#endif - -#if defined(BUSPIRATEV4) - bpWvolts(bpADC(BP_ADC_3V3)); - BPMSG1045; - UART1TX('\t'); -#else - bpWvolts(bpADC(BP_ADC_5V0)); - BPMSG1045; - UART1TX('\t'); -#endif - -#if defined(BUSPIRATEV25) - bpWvolts(bpADC(BP_ADC_3V3)); - BPMSG1045; - UART1TX('\t'); -#elif defined(BUSPIRATEV4) - bpWvolts(bpADC(BP_ADC_VPU)); - BPMSG1045; - UART1TX('\t'); -#else - bpWvolts(bpADC(BP_ADC_PROBE)); - BPMSG1045; - UART1TX('\t'); -#endif - -#if defined(BUSPIRATEV4) - bpWvolts(bpADC(BP_ADC_PROBE)); - BPMSG1045; - UART1TX('\t'); -#else - bpWvolts(bpADC(BP_ADC_VPU)); - BPMSG1045; - UART1TX('\t'); -#endif - ADCOFF(); -#if defined(BUSPIRATEV4) - pinState(AUX2); - pinState(AUX1); - pinState(AUX); - pinState(CS); - pinState(MISO); - pinState(CLK); - pinState(MOSI); -#else - pinState(AUX); - pinState(CLK); - pinState(MOSI); - pinState(CS); - pinState(MISO); -#endif - bpBR; -} //pinStates(void) - -void pinDirection(unsigned int pin) { - - if (IODIR & pin) { - bpWstring("I\t"); - } else { - bpWstring("O\t"); - } - -} // - -void pinState(unsigned int pin) { - - if (IOPOR & pin) { - bpWstring("H\t"); - } else { - bpWstring("L\t"); - } - -} // - -//user terminal number display mode dialog (eg HEX, DEC, BIN, RAW) - -void setDisplayMode(void) { - int mode; - - cmdstart = (cmdstart + 1) & CMDLENMSK; - - consumewhitechars(); - mode = getint(); - - if ((mode > 0) && (mode <= 4)) { - bpConfig.displayMode = mode - 1; - } else { - cmderror = 0; - //bpWmessage(MSG_OPT_DISPLAYMODE); //show the display mode options message - BPMSG1127; - // bpConfig.displayMode=(bpUserNumberPrompt(1, 4, 1)-1); //get, store user reply - bpConfig.displayMode = getnumber(1, 1, 4, 0) - 1; //get, store user reply - } - //bpWmessage(MSG_OPT_DISPLAYMODESET);//show display mode update text - BPMSG1128; -} // - -//configure user terminal side UART baud rate - -void setBaudRate(void) { - unsigned char speed; - unsigned char brg = 0; - - cmdstart = (cmdstart + 1) & CMDLENMSK; - - consumewhitechars(); - speed = getint(); - - if ((speed > 0) && (speed <= 10)) { - bpConfig.termSpeed = speed - 1; - } else { - cmderror = 0; - //bpWmessage(MSG_OPT_UART_BAUD); //show stored dialog - BPMSG1133; - // bpConfig.termSpeed=(bpUserNumberPrompt(1, 9, 9)-1); - bpConfig.termSpeed = getnumber(9, 1, 10, 0) - 1; - } - - if (bpConfig.termSpeed == 9) { - consumewhitechars(); - brg = getint(); - - if (brg == 0) { - cmderror = 0; - bpWline("Enter raw value for BRG"); - brg = getnumber(34, 0, 32767, 0); - } - } - - //bpWmessage(MSG_OPT_TERMBAUD_ADJUST); //show 'adjust and press space dialog' - BPMSG1134; - BPMSG1251; - while (0 == UART1TXRdy()); //wait for TX to finish or reinit flushes part of prompt string from buffer - - if (bpConfig.termSpeed == 9) { - UART1Speed(brg); - } - InitializeUART1(); - while (1) { //wait for space to prove valid baud rate switch - //JTR Not required while (!UART1RXRdy()); - if (UART1RX() == ' ')break; - } -} // - -#ifdef BUSPIRATEV4 - -void setPullupVoltage(void) { - int temp; - - - //don't allow pullups on some modules. also: V0a limitation of 2 resistors - if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); - BPMSG1088; - cmderror = 1; // raise error - return; - } - if (modeConfig.HiZ == 0) { //bpWmessage(MSG_ERROR_NOTHIZPIN); - BPMSG1209; - cmderror = 1; // raise error - return; - } - - BP_3V3PU_OFF(); //disable any existing pullup - bpDelayMS(2); - ADCON(); - if (bpADC(BP_ADC_VPU) > 0x100) { //is there already an external voltage? - bpWline("Warning: already a voltage on Vpullup pin"); // shouldn;t this be an error? - } - ADCOFF(); - - cmdstart = (cmdstart + 1) & CMDLENMSK; - consumewhitechars(); - - temp = getint(); - if (cmderror) // I think the user wants a menu - { - cmderror = 0; - - //bpWline("Select Vpu source"); - //bpWline(" 1) None or external"); - //bpWline(" 2) Onboard 3V3 Vreg"); - //bpWline(" 3) Onboard 5V Vreg"); - BPMSG1271; - - temp = getnumber(1, 1, 3, 0); - } - switch (temp) { - case 1: BP_3V3PU_OFF(); - - BPMSG1272; //;0;" on-board pullup voltage " - BPMSG1274; //1;"disabled" - - //bpWline("on-board pullup voltage disabled"); - break; - case 2: BP_3V3PU_ON(); - BPMSG1173; //3.3v - BPMSG1272; //;0;" on-board pullup voltage " - BPMSG1273; //1;"enabled" - //bpWline("3V3 on-board pullup voltage enabled"); - break; - case 3: BP_5VPU_ON(); - BPMSG1171; //5v - BPMSG1272; //;0;" on-board pullup voltage " - BPMSG1273; //1;"enabled" - //bpWline("5V on-board pullup voltage enabled"); - break; - default:BP_3V3PU_OFF(); - BPMSG1272; //;0;" on-board pullup voltage " - BPMSG1274; //1;"disabled" - //bpWline("on-board pullup voltage disabled"); - } -} - -#endif - - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +#include "AUXpin.h" +#include "busPirateCore.h" +#include "procMenu.h" //need our public versionInfo() function +#ifndef BUSPIRATEV1A +#include "selftest.h" +#endif +#include "binIO.h" +#include "SUMP.h" +#include "basic.h" + +extern struct _bpConfig bpConfig; +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; +extern proto protos[MAXPROTO]; + +#ifdef BUSPIRATEV4 +extern volatile BYTE cdc_Out_len; +#endif + +void walkdungeon(void); + +void setMode(void); //change protocol/bus mode +void setDisplayMode(void); //user terminal number display mode dialog (eg HEX, DEC, BIN, RAW) +void setPullups(void); //pullup resistor dialog +void measureSupplyVoltages(void); //display supply voltage measurements for 3.3, 5, and vpullup +void setBitOrder(void); //LSB/MSB menu for modes that allow it +void setAltAuxPin(void); //configure AUX syntax to control AUX or CS pin +void setBaudRate(void); //configure user terminal side UART baud rate +void statusInfo(void); //display properties of the current bus mode (pullups, vreg, lsb, output type, etc) +void convert(void); //convert input to HEX/DEC/BIN +void pinDirection(unsigned int pin); +void pinState(unsigned int pin); +void pinStates(void); + +#ifdef BUSPIRATEV4 +void setPullupVoltage(void); // onboard Vpu selection +#endif + +//global vars move to bpconfig structure? +char cmdbuf[CMDBUFLEN]; +unsigned int cmdend; +unsigned int cmdstart; +int cmderror; + +//int currentproto; // port to the other way :) + +#define USRMACROS 5 +#define USRMACROLEN 32 + +char usrmacros[USRMACROS][USRMACROLEN]; +int usrmacro; + +void serviceuser(void) { + int cmd, stop; + int newstart; + int oldstart; + unsigned int sendw, received; + int repeat; + unsigned char c; + int temp; + int temp2; + int binmodecnt; + int numbits; + unsigned int tmpcmdend, histcnt, tmphistcnt; + + // init + cmd = 0; + cmdstart = 0; + cmdend = 0; + tmpcmdend = cmdend; + histcnt = 0; + tmphistcnt = 0; + bpConfig.busMode = HIZ; + temp2 = 0; + cmderror = 0; // we don't want to start with error do we? + binmodecnt = 0; + + for (repeat = 0; repeat < USRMACROS; repeat++) { + for (temp = 0; temp < 32; temp++) { + usrmacros[repeat][temp] = 0; + } + } + usrmacro = 0; + + while (1) { + bpWstring(protos[bpConfig.busMode].protocol_name); + if (bpConfig.basic) { //bpWstring("(BASIC)"); + BPMSG1084; + } + bpWstring(">"); + while (!cmd) { + if (usrmacro) { + usrmacro--; + temp = 0; + while (usrmacros[usrmacro][temp]) { + cmdbuf[cmdend] = usrmacros[usrmacro][temp]; + UART1TX(usrmacros[usrmacro][temp]); + cmdend++; + temp++; + cmdend &= CMDLENMSK; + } + usrmacro = 0; + } + + while (!UART1RXRdy()) // as long as there is no user input poll periodicservice + { + if (modeConfig.periodicService == 1) { + if (protos[bpConfig.busMode].protocol_periodic()) // did we print something? + { + bpWstring(protos[bpConfig.busMode].protocol_name); + bpWstring(">"); + if (cmdstart != cmdend) { + for (temp = cmdstart; temp != cmdend; temp++) { + UART1TX(cmdbuf[temp]); + temp &= CMDLENMSK; + } + } + } + } +#ifdef BUSPIRATEV4 + if (!BP_BUTTON) { // button pressed + } else { // button not pressed + } +#endif + } + + if (CheckCommsError()) { //check for user terminal buffer overflow error + ClearCommsError(); + continue; //resume getting more user input + } else { + c = UART1RX(); //no error, process byte + } + + switch (c) { + case 0x08: // backspace(^H) + if (tmpcmdend != cmdstart) // not at begining? + { + if (tmpcmdend == cmdend) // at the end? + { + cmdend = (cmdend - 1) & CMDLENMSK; + cmdbuf[cmdend] = 0x00; // add end marker + tmpcmdend = cmdend; // update temp + bpWstring("\x08 \x08"); // destructive backspace ian !! :P + } else // not at end, left arrow used + { + repeat = 0; // use as temp, not valid here anyway + tmpcmdend = (tmpcmdend - 1) & CMDLENMSK; + bpWstring("\x1B[D"); // move left + for (temp = tmpcmdend; temp != cmdend; temp = (temp + 1) & CMDLENMSK) { + cmdbuf[temp] = cmdbuf[temp + 1]; + if (cmdbuf[temp]) // not NULL + UART1TX(cmdbuf[temp]); + else + UART1TX(0x20); + repeat++; + } + cmdend = (cmdend - 1) & CMDLENMSK; // end pointer moves left one + bpWstring("\x1B["); // move left + bpWdec(repeat); // to original + bpWstring("D"); // cursor position + } + } else { + UART1TX(BELL); // beep, at begining + } + break; + case 0x04: // delete (^D) + case 0x7F: // delete key + if (tmpcmdend != cmdend) // not at the end + { + repeat = 0; // use as temp, not valid here anyway + for (temp = tmpcmdend; temp != cmdend; temp = (temp + 1) & CMDLENMSK) { + cmdbuf[temp] = cmdbuf[temp + 1]; + if (cmdbuf[temp]) // not NULL + UART1TX(cmdbuf[temp]); + else + UART1TX(0x20); + repeat++; + } + cmdend = (cmdend - 1) & CMDLENMSK; // end pointer moves left one + bpWstring("\x1B["); // move left + bpWdec(repeat); // to original + bpWstring("D"); // cursor position + } else { + UART1TX(BELL); // beep, at end + } + break; + case 0x1B: // escape + c = UART1RX(); // get next char + if (c == '[') // got CSI + { + c = UART1RX(); // get next char + switch (c) { + case 'D': // left arrow + goto left; + break; + case 'C': // right arrow + goto right; + break; + case 'A': // up arrow + goto up; + break; + case 'B': // down arrow + goto down; + break; + case '1': // VT100+ home key (example use in PuTTY) + c = UART1RX(); + if (c == '~') goto home; + break; + case '4': // VT100+ end key (example use in PuTTY) + c = UART1RX(); + if (c == '~') goto end; + break; + } + } + break; +left: + case 0x02: // ^B (left arrow) or SUMP + if (binmodecnt >= 5) { + SUMP(); + binmodecnt = 0; // do we get here or not? + } else // ^B (left arrow) + { + if (tmpcmdend != cmdstart) // at the begining? + { + tmpcmdend = (tmpcmdend - 1) & CMDLENMSK; + bpWstring("\x1B[D"); // move left + } else { + UART1TX(BELL); // beep, at begining + } + } + break; +right: + case 0x06: // ^F (right arrow) + if (tmpcmdend != cmdend) // ^F (right arrow) + { // ensure not at end + tmpcmdend = (tmpcmdend + 1) & CMDLENMSK; + bpWstring("\x1B[C"); // move right + } else { + UART1TX(BELL); // beep, at end + } + break; +up: + case 0x10: // ^P (up arrow) + tmphistcnt = 0; // reset counter + for (temp = (cmdstart - 1) & CMDLENMSK; temp != cmdend; temp = (temp - 1) & CMDLENMSK) { + if (!cmdbuf[temp] && cmdbuf[(temp - 1) & CMDLENMSK]) { // found previous entry, temp is old cmdend + tmphistcnt++; + if (tmphistcnt > histcnt) { + histcnt++; + if (cmdstart != cmdend) { // clear partially entered cmd line + while (cmdend != cmdstart) { + cmdbuf[cmdend] = 0x00; + cmdend = (cmdend - 1) & CMDLENMSK; + } + cmdbuf[cmdend] = 0x00; + } + repeat = (temp - 1) & CMDLENMSK; + while (repeat != cmdend) { + if (!cmdbuf[repeat]) { + temp2 = (repeat + 1) & CMDLENMSK; + /* start of old cmd */ + break; + } + repeat = (repeat - 1) & CMDLENMSK; + } + bpWstring("\x1B[2K\x0D"); // clear line, CR + bpWstring(protos[bpConfig.busMode].protocol_name); + if (bpConfig.basic) BPMSG1084; + bpWstring(">"); + for (repeat = temp2; repeat != temp; repeat = (repeat + 1) & CMDLENMSK) { + UART1TX(cmdbuf[repeat]); + cmdbuf[cmdend] = cmdbuf[repeat]; + cmdend = (cmdend + 1) & CMDLENMSK; + } + cmdbuf[cmdend] = 0x00; + tmpcmdend = cmdend; // resync + break; + } + } + } + if (temp == cmdend) UART1TX(BELL); // beep, top + break; +down: + case 0x0E: // ^N (down arrow) + tmphistcnt = 0; // reset counter + for (temp = (cmdstart - 1) & CMDLENMSK; temp != cmdend; temp = (temp - 1) & CMDLENMSK) { + if (!cmdbuf[temp] && cmdbuf[(temp - 1) & CMDLENMSK]) { // found previous entry, temp is old cmdend + tmphistcnt++; + if (tmphistcnt == (histcnt - 1)) { + histcnt--; + if (cmdstart != cmdend) { // clear partially entered cmd line + while (cmdend != cmdstart) { + cmdbuf[cmdend] = 0x00; + cmdend = (cmdend - 1) & CMDLENMSK; + } + cmdbuf[cmdend] = 0x00; + } + repeat = (temp - 1) & CMDLENMSK; + while (repeat != cmdend) { + if (!cmdbuf[repeat]) { + temp2 = (repeat + 1) & CMDLENMSK; + /* start of old cmd */ + break; + } + repeat = (repeat - 1) & CMDLENMSK; + } + bpWstring("\x1B[2K\x0D"); // clear line, CR + bpWstring(protos[bpConfig.busMode].protocol_name); + if (bpConfig.basic) BPMSG1084; + bpWstring(">"); + for (repeat = temp2; repeat != temp; repeat = (repeat + 1) & CMDLENMSK) { + UART1TX(cmdbuf[repeat]); + cmdbuf[cmdend] = cmdbuf[repeat]; + cmdend = (cmdend + 1) & CMDLENMSK; + } + cmdbuf[cmdend] = 0x00; + tmpcmdend = cmdend; // resync + break; + } + } + } + if (temp == cmdend) { + if (histcnt == 1) { + bpWstring("\x1B[2K\x0D"); // clear line, CR + bpWstring(protos[bpConfig.busMode].protocol_name); + if (bpConfig.basic) BPMSG1084; + bpWstring(">"); + while (cmdend != cmdstart) { + cmdbuf[cmdend] = 0x00; + cmdend = (cmdend - 1) & CMDLENMSK; + } + cmdbuf[cmdend] = 0x00; + tmpcmdend = cmdend; // resync + histcnt = 0; + } else UART1TX(BELL); // beep, top + } + break; +home: + case 0x01: // ^A (goto begining of line) + if (tmpcmdend != cmdstart) { + repeat = (tmpcmdend - cmdstart) & CMDLENMSK; + bpWstring("\x1B["); // move left + bpWdec(repeat); // to start + bpWstring("D"); // of command line + tmpcmdend = cmdstart; + } else { + UART1TX(BELL); // beep, at start + } + break; +end: + case 0x05: // ^E (goto end of line) + if (tmpcmdend != cmdend) { + repeat = (cmdend - tmpcmdend) & CMDLENMSK; + bpWstring("\x1B["); // move right + bpWdec(repeat); // to end + bpWstring("C"); // of command line + tmpcmdend = cmdend; + } else { + UART1TX(BELL); // beep, at end + } + break; + case 0x0A: // Does any terminal only send a CR? + case 0x0D: // Enter pressed (LF) + cmd = 1; // command received + histcnt = 0; // reset counter + cmdbuf[cmdend] = 0x00; // use to find history + cmdend = (cmdend + 1) & CMDLENMSK; + tmpcmdend = cmdend; // resync + bpWline(""); + break; + case 0x00: + binmodecnt++; + if (binmodecnt == 20) { + binBB(); +#if defined (BUSPIRATEV4) + binmodecnt = 0; //no reset, cleanup manually + goto bpv4reset; //versionInfo(); //and simulate reset for dependent apps (looking at you AVR dude!) +#endif + } + break; + default: + if ((((cmdend + 1) & CMDLENMSK) != cmdstart) && (c >= 0x20) && (c < 0x7F)) { // no overflow and printable + if (cmdend == tmpcmdend) // adding to the end + { + UART1TX(c); // echo back + cmdbuf[cmdend] = c; // store char + cmdend = (cmdend + 1) & CMDLENMSK; + cmdbuf[cmdend] = 0x00; // add end marker + tmpcmdend = cmdend; // update temp + } else // not at end, left arrow used + { + repeat = (cmdend - tmpcmdend) & CMDLENMSK; + bpWstring("\x1B["); // move right + bpWdec(repeat); // to end + bpWstring("C"); // of line + temp = cmdend; + while (temp != ((tmpcmdend - 1) & CMDLENMSK)) { + cmdbuf[temp + 1] = cmdbuf[temp]; + if (cmdbuf[temp]) // not NULL + { + UART1TX(cmdbuf[temp]); + bpWstring("\x1B[2D"); // left 2 + } + temp = (temp - 1) & CMDLENMSK; + } + UART1TX(c); // echo back + cmdbuf[tmpcmdend] = c; // store char + tmpcmdend = (tmpcmdend + 1) & CMDLENMSK; + cmdend = (cmdend + 1) & CMDLENMSK; + } + } else { + UART1TX(BELL); // beep, overflow or non printable + } //default: + } //switch(c) + } //while(!cmd) + + newstart = cmdend; + oldstart = cmdstart; + cmd = 0; + + // bpWline("\x0a\x0dreceived enter, processing input"); + // bpWline("cmdbuf[]="); + + // for(i=0; i"); //was printed twice +#else + BPMSG1093; + while (0 == UART1TXRdy()); //wait untill TX finishes + asm("RESET"); + //} +#endif + break; + case '$': //bpWline("-bootloader jump"); + if (agree()) { //bpWline("BOOTLOADER"); + BPMSG1094; + bpDelayMS(100); + bpInit(); // turn off nasty things, cleanup first needed? + while (0 == UART1TXRdy()); //wait untill TX finishes + asm volatile ("mov #BLJUMPADDRESS, w1 \n" //bootloader location + "goto w1 \n"); + } + break; + case 'a': //bpWline("-AUX low"); + repeat = getrepeat() + 1; + while (--repeat) bpAuxLow(); + break; + case 'A': //bpWline("-AUX hi"); + repeat = getrepeat() + 1; + while (--repeat) bpAuxHigh(); + break; + case '@': //bpWline("-Aux read"); + repeat = getrepeat() + 1; + while (--repeat) { //bpWstring(OUMSG_AUX_INPUT_READ); + BPMSG1095; + bpEchoState(bpAuxRead()); + bpWBR; + } + break; + case 'W': //bpWline("-PSU on"); //enable any active power supplies + if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); + BPMSG1088; + } else { + BP_VREG_ON(); + ADCON(); // turn ADC ON + bpDelayMS(2); //wait for VREG to come up + + if ((bpADC(BP_ADC_3V3) > V33L) && (bpADC(BP_ADC_5V0) > V5L)) { //voltages are correct + //bpWmessage(MSG_VREG_ON); + BPMSG1096; + bpBR; + //modeConfig.vregEN=1; + + //Engaging Clutch + //finishes the settup and conects the pins... + protos[bpConfig.busMode].protocol_setup_exc(); + bpWline("Clutch engaged!!!"); + } else { + BP_VREG_OFF(); + bpWline("VREG too low, is there a short?"); + BPMSG1097; + bpBR; + } + ADCOFF(); // turn ADC OFF + } + break; + case 'w': //bpWline("-PSU off"); //disable the power supplies + if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); + BPMSG1088; + } else { + //disengaging Clutch + //cleansup the protocol and HiZs the pins + protos[bpConfig.busMode].protocol_cleanup(); + bpWline("Clutch disengaged!!!"); + + BP_VREG_OFF(); + //bpWmessage(MSG_VREG_OFF); + BPMSG1097; + bpBR; + //modeConfig.vregEN=0; + } + break; + case 'd': //bpWline("-read ADC"); //do an adc reading + //bpWstring(OUMSG_PS_ADC_VOLT_PROBE); + BPMSG1044; + bpADCprobe(); + //bpWline(OUMSG_PS_ADC_VOLTS);` + BPMSG1045; + bpBR; + break; + case 'D': //bpWline("-DVM mode"); //dumb voltmeter mode + bpADCCprobe(); + break; + case '&': //bpWline("-delay 1ms"); + repeat = getrepeat(); + //bpWstring(OUMSG_PS_DELAY); + BPMSG1099; + bpWintdec(repeat); + //bpWline(OUMSG_PS_DELAY_US); + BPMSG1100; + bpDelayUS(repeat); + break; + case '%': repeat = getrepeat(); + BPMSG1099; + bpWintdec(repeat); + BPMSG1212; + bpDelayMS(repeat); + break; + case '+': //bpWline("-easter egg"); + //easterEgg(); + break; +#ifdef BP_USE_BASIC + case 's': //bpWline("Listing:"); + bpConfig.basic = 1; + break; +#endif + case 'S': //servo control + if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); + BPMSG1088; + } else { + bpServo(); + } + break; + case '<': cmderror = 1; + temp = 1; + + while (cmdbuf[((cmdstart + temp) & CMDLENMSK)] != 0x00) { + if (cmdbuf[((cmdstart + temp) & CMDLENMSK)] == '>') cmderror = 0; // clear error if we found a > before the command ends + temp++; + } + if (temp >= (USRMACROLEN + 3)) cmderror = 1; // too long (avoid overflows) + if (!cmderror) { + cmdstart = (cmdstart + 1) & CMDLENMSK; + temp = getint(); + if (cmdbuf[((cmdstart) & CMDLENMSK)] == '=') // assignment + { + if ((temp > 0) && (temp <= USRMACROS)) { + cmdstart = (cmdstart + 1) & CMDLENMSK; + temp--; + for (repeat = 0; repeat < USRMACROLEN; repeat++) { + usrmacros[temp][repeat] = 0; + } + repeat = 0; + while (cmdbuf[cmdstart] != '>') { + usrmacros[temp][repeat] = cmdbuf[cmdstart]; + repeat++; + cmdstart = (cmdstart + 1) & CMDLENMSK; + } + } else { + cmderror = 1; + } + } else { + if (temp == 0) { + for (repeat = 0; repeat < USRMACROS; repeat++) { + bpWdec(repeat + 1); + bpWstring(". <"); + bpWstring(usrmacros[repeat]); + bpWline(">"); + } + } else if ((temp > 0) && (temp <= USRMACROS)) { //bpWstring("execute : "); + //BPMSG1236; + //bpWdec(temp-1); + bpBR; + usrmacro = temp; + } else { + cmderror = 1; + } + } + } + break; + // command for subsys (i2c, UART, etc) + case '(': //bpWline("-macro"); + cmdstart = (cmdstart + 1) & CMDLENMSK; + sendw = getint(); + consumewhitechars(); + if (cmdbuf[((cmdstart) & CMDLENMSK)] == ')') { //cmdstart++; // skip ) + //cmdstart&=CMDLENMSK; + //bpWdec(sendw); + protos[bpConfig.busMode].protocol_macro(sendw); + bpBR; + } else { + cmderror = 1; + } + break; + case 0x22: //bpWline("-send string"); + cmderror = 1; + temp = 1; + + while (cmdbuf[((cmdstart + temp) & CMDLENMSK)] != 0x00) { + if (cmdbuf[((cmdstart + temp) & CMDLENMSK)] == 0x22) cmderror = 0; // clear error if we found a " before the command ends + temp++; + } + + if (!cmderror) { + BPMSG1101; + UART1TX(0x22); + while (cmdbuf[((++cmdstart) & CMDLENMSK)] != 0x22) { + cmdstart &= CMDLENMSK; + UART1TX(cmdbuf[cmdstart]); + sendw = cmdbuf[cmdstart]; + if (modeConfig.lsbEN == 1) //adjust bitorder + { + sendw = bpRevByte(sendw); + } + protos[bpConfig.busMode].protocol_send(sendw); + } + cmdstart &= CMDLENMSK; + UART1TX(0x22); + bpBR; + } + break; + case '[': //bpWline("-Start"); + protos[bpConfig.busMode].protocol_start(); + break; + case '{': //bpWline("-StartR"); + protos[bpConfig.busMode].protocol_startR(); + break; + case ']': //bpWline("-Stop"); + protos[bpConfig.busMode].protocol_stop(); + break; + case '}': //bpWline("-StopR"); + protos[bpConfig.busMode].protocol_stopR(); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': //bpWline("-Send"); + //bpWmessage(MSG_WRITE); + BPMSG1101; + sendw = getint(); + cmdstart--; + cmdstart &= CMDLENMSK; + repeat = getrepeat() + 1; + numbits = getnumbits(); + if (numbits) { + modeConfig.numbits = numbits; + if (numbits > 8) modeConfig.int16 = 1; + else modeConfig.int16 = 0; + } + while (--repeat) { + bpWbyte(sendw); + if (((modeConfig.int16 == 0) && (modeConfig.numbits != 8)) || ((modeConfig.int16 == 1) && (modeConfig.numbits != 16))) { + UART1TX(';'); + bpWdec(modeConfig.numbits); + } + if (modeConfig.lsbEN == 1) {//adjust bitorder + sendw = bpRevByte(sendw); + } + received = protos[bpConfig.busMode].protocol_send(sendw); + bpSP; + if (modeConfig.wwr) { //bpWmessage(MSG_READ); + BPMSG1102; + if (modeConfig.lsbEN == 1) {//adjust bitorder + received = bpRevByte(received); + } + bpWbyte(received); + bpSP; + } + } + bpBR; + break; + case 'r': //bpWline("-Read"); + //bpWmessage(MSG_READ); + BPMSG1102; + //newDmode = 0; + newDmode = changeReadDisplay(); + repeat = getrepeat() + 1; + numbits = getnumbits(); + if (numbits) { + modeConfig.numbits = numbits; + if (numbits > 8) modeConfig.int16 = 1; + else modeConfig.int16 = 0; + } + if(newDmode) + { + oldDmode = bpConfig.displayMode; + bpConfig.displayMode = newDmode-1; + } + while (--repeat) { + received = protos[bpConfig.busMode].protocol_read(); + if (modeConfig.lsbEN == 1) {//adjust bitorder + received = bpRevByte(received); + } + bpWbyte(received); + if (((modeConfig.int16 == 0) && (modeConfig.numbits != 8)) || ((modeConfig.int16 == 1) && (modeConfig.numbits != 16))) { + UART1TX(';'); + bpWdec(modeConfig.numbits); + } + bpSP; + } + if(newDmode) + { + bpConfig.displayMode = oldDmode; + newDmode=0; + } + bpBR; + break; + case '/': //bpWline("-CLK hi"); + //repeat=getrepeat()+1; + //while(--repeat) + //{ //bpWmessage(MSG_BIT_CLKH); + BPMSG1103; + protos[bpConfig.busMode].protocol_clkh(); + //} + break; + case '\\': //bpWline("-CLK lo"); + //repeat=getrepeat()+1; + //while(--repeat) + //{ //bpWmessage(MSG_BIT_CLKL); + BPMSG1104; + protos[bpConfig.busMode].protocol_clkl(); + //} + break; + case '-': //bpWline("-DAT hi"); + //repeat=getrepeat()+1; + //while(--repeat) + //{ //bpWmessage(MSG_BIT_DATH); + BPMSG1105; + protos[bpConfig.busMode].protocol_dath(); + //} + break; + case '_': //bpWline("-DAT lo"); + //repeat=getrepeat()+1; + //while(--repeat) + //{ //bpWmessage(MSG_BIT_DATL); + BPMSG1106; + protos[bpConfig.busMode].protocol_datl(); + //} + break; + case '.': //bpWline("-DAT state read"); + //repeat=getrepeat()+1; + BPMSG1098; + //while(--repeat) + { + bpEchoState(protos[bpConfig.busMode].protocol_dats()); + //bpWmessage(MSG_BIT_NOWINPUT); + //BPMSG1107; + } + break; + case '^': //bpWline("-CLK pulse"); + repeat = getrepeat(); + BPMSG1108; + bpWbyte(repeat); + repeat++; + while (--repeat) { //bpWmessage(MSG_BIT_CLK); + protos[bpConfig.busMode].protocol_clk(); + } + bpBR; + break; + case '!': //bpWline("-bit read"); + repeat = getrepeat() + 1; + BPMSG1109; + while (--repeat) { //bpWmessage(MSG_BIT_READ); + bpEchoState(protos[bpConfig.busMode].protocol_bitr()); + bpSP; + } + //bpWmessage(MSG_BIT_NOWINPUT); + BPMSG1107; + break; + // white char/delimeters + case 0x00: + case 0x0D: // not necessary but got random error msg at end, just to be sure + case 0x0A: // same here + case ' ': + case ',': break; // no match so it is an error + default: cmderror = 1; + } //switch(c) + cmdstart = (cmdstart + 1) & CMDLENMSK; + + // bpWline(""); + // bpWstring("cmdstart = "); + // bpWinthex(cmdstart); + // bpWstring(" cmdend = "); + // bpWinthex(cmdend); + // bpWline(""); + + if (cmderror) { //bpWstring("Syntax error at char "); + BPMSG1110; + if (cmdstart > oldstart) // find error position :S + { + bpWdec(cmdstart - oldstart); + } else { + bpWdec((CMDBUFLEN + cmdstart) - oldstart); + } + cmderror = 0; + stop = 1; + bpBR; + } + + if (cmdstart == cmdend) stop = 1; // reached end of user input?? + } //while(!stop) + + + cmdstart = newstart; + cmdend = newstart; // 'empty' cmdbuffer + cmd = 0; + } //while(1) +} //serviceuser(void) + +int getint(void) // get int from user (accept decimal, hex (0x) or binairy (0b) +{ + int i; + int number; + + i = 0; + number = 0; + + if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x31) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // 1-9 is for sure decimal + { + number = cmdbuf[(cmdstart + i) & CMDLENMSK] - 0x30; + i++; + while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // consume all digits + { + number *= 10; + number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; + i++; + } + } else if (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 0x30) // 0 could be anything + { + i++; + if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'b') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'B')) { + i++; + while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == '1') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == '0')) { + number <<= 1; + number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; + i++; + } + } else if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'x') || (cmdbuf[((cmdstart + i) & CMDLENMSK)] == 'X')) { + i++; + while (((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) || \ + ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 'A') && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 'F')) || \ + ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 'a') && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 'f'))) { + number <<= 4; + if ((cmdbuf[(cmdstart + i) & CMDLENMSK] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) { + number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; + } else { + cmdbuf[((cmdstart + i) & CMDLENMSK)] |= 0x20; // make it lowercase + number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x57; // 'a'-0x61+0x0a + } + i++; + } + } else if ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) { + number = cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; + while ((cmdbuf[((cmdstart + i) & CMDLENMSK)] >= 0x30) && (cmdbuf[((cmdstart + i) & CMDLENMSK)] <= 0x39)) // consume all digits + { + number *= 10; + number += cmdbuf[((cmdstart + i) & CMDLENMSK)] - 0x30; + i++; + } + } + } else // how did we come here?? + { + cmderror = 1; + return 0; + } + + cmdstart += i; // we used i chars + cmdstart &= CMDLENMSK; + return number; +} //getint(void) + +int getrepeat(void) { + int temp; + + if (cmdbuf[(cmdstart + 1) & CMDLENMSK] == ':') { + cmdstart += 2; + cmdstart &= CMDLENMSK; + temp = getint(); + cmdstart--; // to allow [6:10] (start send 6 10 times stop) + cmdstart &= CMDLENMSK; + return temp; + } + return 1; // no repeat count=1 +} // + +int getnumbits(void) { + int temp; + + if (cmdbuf[(cmdstart + 1) & CMDLENMSK] == ';') { + cmdstart = (cmdstart + 2) & CMDLENMSK; + temp = getint(); + cmdstart = (cmdstart - 1) & CMDLENMSK; // to allow [6:10] (start send 6 10 times stop) + return temp; + } + return 0; // no numbits=0; +} // + +unsigned char changeReadDisplay(void) +{ + if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'x') + { + cmdstart = (cmdstart + 1) & CMDLENMSK; + return 1; + } + if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'd') + { + cmdstart = (cmdstart + 1) & CMDLENMSK; + return 2; + } + if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'b') + { + cmdstart = (cmdstart + 1) & CMDLENMSK; + return 3; + } + if(cmdbuf[(cmdstart + 1) & CMDLENMSK] == 'w') + { + cmdstart = (cmdstart + 1) & CMDLENMSK; + return 4; + } +return 0; +} + +void consumewhitechars(void) { + while (cmdbuf[cmdstart] == 0x20) { + cmdstart = (cmdstart + 1) & CMDLENMSK; // remove spaces + } +} // + +void changemode(void) { + int i, busmode; + + busmode = 0; + + // bpWline("--changemode()"); + + cmdstart = (cmdstart + 1) & CMDLENMSK; + + consumewhitechars(); + + // bpWline("whitechars stripped"); + + busmode = getint(); + + // bpWstring("numbers: busmode="); + // bpWinthex(busmode); + // bpWline(""); + + if (!busmode) // no argument entered + { + for (i = 0; i < MAXPROTO; i++) { + bpWdec(i + 1); + bpWstring(". "); + bpWline(protos[i].protocol_name); + } + //bpWline("x. exit(without change)"); + BPMSG1111; + cmderror = 0; // error is set because no number found, but it is no error here:S eeeh confusing right? + busmode = getnumber(1, 1, MAXPROTO, 1) - 1; + // bpWstring("busmode= "); + // bpWdec(busmode); + if ((busmode == -2) || (busmode == -1)) { //bpWline("no mode change"); + BPMSG1112; + } else { + protos[bpConfig.busMode].protocol_cleanup(); + bpInit(); + bpConfig.busMode = busmode; + protos[bpConfig.busMode].protocol_setup(); + bpWline("Clutch disengaged!!!"); + if (busmode) { + BP_LEDMODE = 1; // mode led is on when proto >0 + bpWline("To finish setup, start up the power supplies with command 'W'\r\n"); + } + //bpWmessage(MSG_READY); + BPMSG1085; + } + } else // number entered + { + busmode--; // save a couple of programwords to do it here :D + if (busmode < MAXPROTO) { + protos[bpConfig.busMode].protocol_cleanup(); + bpInit(); + bpConfig.busMode = busmode; + protos[bpConfig.busMode].protocol_setup(); + if (busmode) BP_LEDMODE = 1; // mode led is on when proto >0 + //bpWmessage(MSG_READY); + BPMSG1085; + } else { //bpWline("Nonexistent protocol!!"); + BPMSG1114; + } + } + cmdstart = (cmdend - 1) & CMDLENMSK; +} //changemode(void) + + +#if defined(BP_ENABLE_HISTORY) + +int cmdhistory(void) { + int i, j, k; + + int historypos[CMDHISTORY]; + + i = 1; + j = (cmdstart - 1) & CMDLENMSK; + + while (j != cmdstart) // scroll through the whole buffer + { + if ((cmdbuf[j] == 0x00) && (cmdbuf[(j + 1) & CMDLENMSK] != 0x00)) // did we find an end? is it not empty? + { + bpWdec(i); + bpWstring(". "); + k = 1; + while (cmdbuf[((j + k) & CMDLENMSK)]) { + UART1TX(cmdbuf[((j + k) & CMDLENMSK)]); // print it + k++; + } + historypos[i] = (j + 1) & CMDLENMSK; + i++; + if (i == CMDHISTORY) break; + bpWline(""); + } + j = (j - 1) & CMDLENMSK; + } + + bpWline(" "); + BPMSG1115; + + j = getnumber(0, 1, i, 1); + + if (j == -1 || !j) // x is -1, default is 0 + { + bpWline(""); + return 1; + } else { + i = 0; + while (cmdbuf[(historypos[j] + i) & CMDLENMSK]) // copy it to the end of the ringbuffer + { + cmdbuf[(cmdend + i) & CMDLENMSK] = cmdbuf[(historypos[j] + i) & CMDLENMSK]; + i++; + } + cmdstart = (cmdend - 1) & CMDLENMSK; // start will be increased before parsing in main loop + cmdend = (cmdstart + i + 2) & CMDLENMSK; + cmdbuf[(cmdend - 1) & CMDLENMSK] = 0x00; + } + return 0; +} //cmdhistory(void) +#endif + + +// gets number from input +// -1 = abort (x) +// -2 = input to much +// 0-max return +// x=1 exit is enabled (we don't want that in the mode changes ;) + +int getnumber(int def, int min, int max, int x) //default, minimum, maximum, show exit option +{ + char c; + char buf[6]; // max 4 digits; + int i, j, stop, temp, neg; + +again: // need to do it proper with whiles and ifs.. + + i = 0; + stop = 0; + temp = 0; + neg = 0; + + bpWstring("\r\n("); + if (def < 0) { + bpWstring("x"); + } else { + bpWdec(def); + } + bpWstring(")>"); + + while (!stop) { + c = UART1RX(); + switch (c) { + case 0x08: if (i) { + i--; + bpWstring("\x08 \x08"); + } else { + if (neg) { + neg = 0; + bpWstring("\x08 \x08"); + } else { + UART1TX(BELL); + } + } + break; + case 0x0A: + case 0x0D: stop = 1; + break; + case '-': if (!i) // enable negative numbers + { + UART1TX('-'); + neg = 1; + } else { + UART1TX(BELL); + } + break; + case 'x': if (x) return -1; // user wants to quit :( only if we enable it :D + default: if ((c >= 0x30) && (c <= 0x39)) // we got a digit + { + if (i > 3) // 0-9999 should be enough?? + { + UART1TX(BELL); + i = 4; + } else { + UART1TX(c); + buf[i] = c; // store user input + i++; + } + } else // ignore input :) + { + UART1TX(BELL); + } + + } + } + bpBR; + + if (i == 0) { + return def; // no user input so return default option + } else { + temp = 0; + i--; + j = i; + + for (; i >= 0; i--) { + temp *= 10; + temp += (buf[j - i] - 0x30); + } + + if ((temp >= min) && (temp <= max)) { + if (neg) { + return -temp; + } else { + return temp; + } + } else { //bpWline("\r\nInvalid choice, try again\r\n"); + BPMSG1211; + goto again; + } + } + return temp; // we dont get here, but keep compiler happy +} //getnumber(int def, int min, int max, int x) + + +#if defined(BUSPIRATEV4) +// gets number from input +// -1 = abort (x) +// -2 = input to much +// 0-max return +// x=1 exit is enabled (we don't want that in the mode changes ;) + +long getlong(long def, int min, long max, int x) //default, minimum, maximum, show exit option +{ + char c; + char buf[12]; // max long = 2147483647 so 10 + int i, j, stop, neg; + long temp; + +again: // need to do it proper with whiles and ifs.. + + i = 0; + stop = 0; + temp = 0; + neg = 0; + + bpWstring("\r\n("); + if (def < 0) { + bpWstring("x"); + } else { + bpWlongdec(def); + } + bpWstring(")>"); + + while (!stop) { + c = UART1RX(); + switch (c) { + case 0x08: if (i) { + i--; + bpWstring("\x08 \x08"); + } else { + if (neg) { + neg = 0; + bpWstring("\x08 \x08"); + } else { + UART1TX(BELL); + } + } + break; + case 0x0A: + case 0x0D: stop = 1; + break; + case '-': if (!i) // enable negative numbers + { + UART1TX('-'); + neg = 1; + } else { + UART1TX(BELL); + } + break; + case 'x': if (x) return -1; // user wants to quit :( only if we enable it :D + default: if ((c >= 0x30) && (c <= 0x39)) // we got a digit + { + if (i > 9) // 0-9999 should be enough?? + { + UART1TX(BELL); + i = 10; + } else { + UART1TX(c); + buf[i] = c; // store user input + i++; + } + } else // ignore input :) + { + UART1TX(BELL); + } + + } + } + bpBR; + + if (i == 0) { + return def; // no user input so return default option + } else { + temp = 0; + i--; + j = i; + + for (; i >= 0; i--) { + temp *= 10; + temp += (buf[j - i] - 0x30); + } + + if ((temp >= min) && (temp <= max)) { + if (neg) { + return -temp; + } else { + return temp; + } + } else { //bpWline("\r\nInvalid choice, try again\r\n"); + BPMSG1211; + goto again; + } + } + return temp; // we dont get here, but keep compiler happy +} +#endif + + + + +//print version info (used in menu and at startup in main.c) + +void versionInfo(void) { + unsigned int i; + + +#if defined (BUSPIRATEV2) //we can tell if it's v3a or v3b, show it here + bpWstring(BP_VERSION_STRING); + UART1TX('.'); + UART1TX(bpConfig.HWversion); + if (bpConfig.dev_type == 0x44F) {//sandbox electronics clone with 44pin PIC24FJ64GA004 + bpWstring(" clone w/different PIC"); + } + bpBR; +#else + bpWline(BP_VERSION_STRING); +#endif + + bpWstring(BP_FIRMWARE_STRING); + +#ifndef BP_MAIN + UART1TX('['); + for (i = 0; i < MAXPROTO; i++) { + if (i) bpSP; + bpWstring(protos[i].protocol_name); + } + UART1TX(']'); +#endif + +#ifndef BUSPIRATEV4 + //bpWstring(" Bootloader v"); + BPMSG1126; + i = bpReadFlash(0x0000, BL_ADDR_VER); + bpWdec(i >> 8); + UART1TX('.'); + bpWdec(i); +#endif + bpWline(""); + + //bpWstring("DEVID:"); + BPMSG1117; + bpWinthex(bpConfig.dev_type); + + //bpWstring(" REVID:"); + BPMSG1210; + bpWinthex(bpConfig.dev_rev); +#ifdef BUSPIRATEV4 + bpWstring(" (24FJ256GB106 "); + switch (bpConfig.dev_rev) { + case PIC_REV_A3: + bpWstring("A3"); + break; + case PIC_REV_A5: + bpWstring("A5"); + break; + default: + bpWstring("UNK"); + break; + } +#else + bpWstring(" (24FJ64GA00"); + if (bpConfig.dev_type == 0x44F) {//sandbox electronics clone with 44pin PIC24FJ64GA004 + bpWstring("4 "); + } else { + bpWstring("2 "); + } + + switch (bpConfig.dev_rev) { + case PIC_REV_A3: + bpWstring("A3"); //also A4, but that's not in the wild and makes it confusing to users + break; + case PIC_REV_B4: + bpWstring("B4"); + break; + case PIC_REV_B5: + bpWstring("B5"); + break; + case PIC_REV_B8: + bpWstring("B8"); + break; + default: + bpWstring("UNK"); + break; + } +#endif + + bpWline(")"); + //bpWline("http://dangerousprototypes.com"); + BPMSG1118; + i = 0; +} //versionInfo(void) + +//display properties of the current bus mode (pullups, vreg, lsb, output type, etc) + +void statusInfo(void) { + +#ifdef BUSPIRATEV4 + bpWstring("CFG0: "); + bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_0)); + bpSP; +#endif + + //bpWstring("CFG1:"); + BPMSG1136; + bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_1)); + //bpWstring(" CFG2:"); + BPMSG1137; + bpWinthex(bpReadFlash(CFG_ADDR_UPPER, CFG_ADDR_2)); + bpBR; + + //bpWline("*----------*"); + BPMSG1119; + + pinStates(); + + //vreg status (was modeConfig.vregEN) + if (BP_VREGEN == 1) BPMSG1096; + else BPMSG1097; //bpWmessage(MSG_VREG_ON); else bpWmessage(MSG_VREG_OFF); + UART1TX(','); + bpSP; + +#ifndef BUSPIRATEV1A + //pullups available, enabled? + //was modeConfig.pullupEN + if (BP_PULLUP == 1) BPMSG1091; + else BPMSG1089; //bpWmessage(MSG_OPT_PULLUP_ON); else bpWmessage(MSG_OPT_PULLUP_OFF); + UART1TX(','); + bpSP; +#endif + +#ifdef BUSPIRATEV4 + if (BP_PUVSEL50_DIR == 0) bpWstring("Vpu=5V, "); + if (BP_PUVSEL33_DIR == 0) bpWstring("Vpu=3V3, "); + +#endif + + //open collector outputs? + if (modeConfig.HiZ == 1) BPMSG1120; + else BPMSG1121; // bpWmessage(MSG_STATUS_OUTPUT_HIZ); else bpWmessage(MSG_STATUS_OUTPUT_NORMAL); + + //voltage report + // measureSupplyVoltages(); + + + //bitorder toggle available, enabled + if (modeConfig.lsbEN == 0) BPMSG1123; + else BPMSG1124; //bpWmessage(MSG_OPT_BITORDER_LSB); else bpWmessage(MSG_OPT_BITORDER_MSB); + UART1TX(','); + bpSP; + + // show partial writes + //bpWline("Number of bits read/write: "); + BPMSG1252; + bpWdec(modeConfig.numbits); + bpBR; + + //AUX pin setting +#ifndef BUSPIRATEV4 + if (modeConfig.altAUX == 1) BPMSG1087; + else BPMSG1086; //bpWmessage(MSG_OPT_AUXPIN_CS); else bpWmessage(MSG_OPT_AUXPIN_AUX); +#endif +#ifdef BUSPIRATEV4 + switch (modeConfig.altAUX) { + case 0: BPMSG1087; + break; + case 1: BPMSG1086; + break; + case 2: BPMSG1263; + break; + case 3: BPMSG1264; + break; + } +#endif + + protos[bpConfig.busMode].protocol_settings(); + + //bpWline("*----------*"); + BPMSG1119; +} //statusInfo(void) + +void pinStates(void) { //bpWline("Pinstates:"); + BPMSG1226; +#if defined(BUSPIRATEV4) + BPMSG1256; //bpWstring("12.(RD)\t11.(BR)\t10.(BLK)\t9.(WT)\t8.(GR)\t7.(PU)\t6.(BL)\t5.(GN)\t4.(YW)\t3.(OR)\t2.(RD)\1.(BR)"); +#else + BPMSG1233; //bpWstring("1.(BR)\t2.(RD)\t3.(OR)\t4.(YW)\t5.(GN)\t6.(BL)\t7.(PU)\t8.(GR)\t9.(WT)\t0.(BLK)"); +#endif +#if defined(BUSPIRATEV4) + BPMSG1257; //bpWstring("GND\t5.0V\t3.3V\tVPU\tADC\tAUX2\tAUX1\tAUX\t"); +#elif defined(BUSPIRATEV25) + BPMSG1235; //bpWstring("GND\tADC\t5.0V\t3.3V\tVPU\tAUX\t"); +#else + BPMSG1227; //bpWstring("GND\t3.3V\t5.0V\tADC\tVPU\tAUX\t"); +#endif + protos[bpConfig.busMode].protocol_pins(); + BPMSG1228; //bpWstring("P\tP\tP\tI\tI\t"); +#if defined(BUSPIRATEV4) + pinDirection(AUX2); + pinDirection(AUX1); + pinDirection(AUX); + pinDirection(CS); + pinDirection(MISO); + pinDirection(CLK); + pinDirection(MOSI); +#else + pinDirection(AUX); + pinDirection(CLK); + pinDirection(MOSI); + pinDirection(CS); + pinDirection(MISO); +#endif + bpBR; + BPMSG1234; //bpWstring("GND\t"); + ADCON(); + + +#if defined(BUSPIRATEV25) + bpWvolts(bpADC(BP_ADC_PROBE)); + BPMSG1045; + UART1TX('\t'); +#elif defined(BUSPIRATEV4) + bpWvolts(bpADC(BP_ADC_5V0)); + BPMSG1045; + UART1TX('\t'); +#else + bpWvolts(bpADC(BP_ADC_3V3)); + BPMSG1045; + UART1TX('\t'); +#endif + +#if defined(BUSPIRATEV4) + bpWvolts(bpADC(BP_ADC_3V3)); + BPMSG1045; + UART1TX('\t'); +#else + bpWvolts(bpADC(BP_ADC_5V0)); + BPMSG1045; + UART1TX('\t'); +#endif + +#if defined(BUSPIRATEV25) + bpWvolts(bpADC(BP_ADC_3V3)); + BPMSG1045; + UART1TX('\t'); +#elif defined(BUSPIRATEV4) + bpWvolts(bpADC(BP_ADC_VPU)); + BPMSG1045; + UART1TX('\t'); +#else + bpWvolts(bpADC(BP_ADC_PROBE)); + BPMSG1045; + UART1TX('\t'); +#endif + +#if defined(BUSPIRATEV4) + bpWvolts(bpADC(BP_ADC_PROBE)); + BPMSG1045; + UART1TX('\t'); +#else + bpWvolts(bpADC(BP_ADC_VPU)); + BPMSG1045; + UART1TX('\t'); +#endif + ADCOFF(); +#if defined(BUSPIRATEV4) + pinState(AUX2); + pinState(AUX1); + pinState(AUX); + pinState(CS); + pinState(MISO); + pinState(CLK); + pinState(MOSI); +#else + pinState(AUX); + pinState(CLK); + pinState(MOSI); + pinState(CS); + pinState(MISO); +#endif + bpBR; +} //pinStates(void) + +void pinDirection(unsigned int pin) { + + if (IODIR & pin) { + bpWstring("I\t"); + } else { + bpWstring("O\t"); + } + +} // + +void pinState(unsigned int pin) { + + if (IOPOR & pin) { + bpWstring("H\t"); + } else { + bpWstring("L\t"); + } + +} // + +//user terminal number display mode dialog (eg HEX, DEC, BIN, RAW) + +void setDisplayMode(void) { + int mode; + + cmdstart = (cmdstart + 1) & CMDLENMSK; + + consumewhitechars(); + mode = getint(); + + if ((mode > 0) && (mode <= 4)) { + bpConfig.displayMode = mode - 1; + } else { + cmderror = 0; + //bpWmessage(MSG_OPT_DISPLAYMODE); //show the display mode options message + BPMSG1127; + // bpConfig.displayMode=(bpUserNumberPrompt(1, 4, 1)-1); //get, store user reply + bpConfig.displayMode = getnumber(1, 1, 4, 0) - 1; //get, store user reply + } + //bpWmessage(MSG_OPT_DISPLAYMODESET);//show display mode update text + BPMSG1128; +} // + +//configure user terminal side UART baud rate + +void setBaudRate(void) { + unsigned char speed; + unsigned char brg = 0; + + cmdstart = (cmdstart + 1) & CMDLENMSK; + + consumewhitechars(); + speed = getint(); + + if ((speed > 0) && (speed <= 10)) { + bpConfig.termSpeed = speed - 1; + } else { + cmderror = 0; + //bpWmessage(MSG_OPT_UART_BAUD); //show stored dialog + BPMSG1133; + // bpConfig.termSpeed=(bpUserNumberPrompt(1, 9, 9)-1); + bpConfig.termSpeed = getnumber(9, 1, 10, 0) - 1; + } + + if (bpConfig.termSpeed == 9) { + consumewhitechars(); + brg = getint(); + + if (brg == 0) { + cmderror = 0; + bpWline("Enter raw value for BRG"); + brg = getnumber(34, 0, 32767, 0); + } + } + + //bpWmessage(MSG_OPT_TERMBAUD_ADJUST); //show 'adjust and press space dialog' + BPMSG1134; + BPMSG1251; + while (0 == UART1TXRdy()); //wait for TX to finish or reinit flushes part of prompt string from buffer + + if (bpConfig.termSpeed == 9) { + UART1Speed(brg); + } + InitializeUART1(); + while (1) { //wait for space to prove valid baud rate switch + //JTR Not required while (!UART1RXRdy()); + if (UART1RX() == ' ')break; + } +} // + +#ifdef BUSPIRATEV4 + +void setPullupVoltage(void) { + int temp; + + + //don't allow pullups on some modules. also: V0a limitation of 2 resistors + if (bpConfig.busMode == HIZ) { //bpWmessage(MSG_ERROR_MODE); + BPMSG1088; + cmderror = 1; // raise error + return; + } + if (modeConfig.HiZ == 0) { //bpWmessage(MSG_ERROR_NOTHIZPIN); + BPMSG1209; + cmderror = 1; // raise error + return; + } + + BP_3V3PU_OFF(); //disable any existing pullup + bpDelayMS(2); + ADCON(); + if (bpADC(BP_ADC_VPU) > 0x100) { //is there already an external voltage? + bpWline("Warning: already a voltage on Vpullup pin"); // shouldn;t this be an error? + } + ADCOFF(); + + cmdstart = (cmdstart + 1) & CMDLENMSK; + consumewhitechars(); + + temp = getint(); + if (cmderror) // I think the user wants a menu + { + cmderror = 0; + + //bpWline("Select Vpu source"); + //bpWline(" 1) None or external"); + //bpWline(" 2) Onboard 3V3 Vreg"); + //bpWline(" 3) Onboard 5V Vreg"); + BPMSG1271; + + temp = getnumber(1, 1, 3, 0); + } + switch (temp) { + case 1: BP_3V3PU_OFF(); + + BPMSG1272; //;0;" on-board pullup voltage " + BPMSG1274; //1;"disabled" + + //bpWline("on-board pullup voltage disabled"); + break; + case 2: BP_3V3PU_ON(); + BPMSG1173; //3.3v + BPMSG1272; //;0;" on-board pullup voltage " + BPMSG1273; //1;"enabled" + //bpWline("3V3 on-board pullup voltage enabled"); + break; + case 3: BP_5VPU_ON(); + BPMSG1171; //5v + BPMSG1272; //;0;" on-board pullup voltage " + BPMSG1273; //1;"enabled" + //bpWline("5V on-board pullup voltage enabled"); + break; + default:BP_3V3PU_OFF(); + BPMSG1272; //;0;" on-board pullup voltage " + BPMSG1274; //1;"disabled" + //bpWline("on-board pullup voltage disabled"); + } +} + +#endif + + + diff --git a/Firmware/procMenu.h b/Firmware/procMenu.h index ccfac46a..561c6489 100644 --- a/Firmware/procMenu.h +++ b/Firmware/procMenu.h @@ -1,49 +1,49 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#define CMDHISTORY 15 -#define BELL 0x07 - -#define CMDBUFLEN 256 -#define CMDLENMSK (CMDBUFLEN-1) - -extern char cmdbuf[CMDBUFLEN]; -extern unsigned int cmdend; -extern unsigned int cmdstart; -extern int cmderror; -//extern int currentproto; -//extern proto protos[MAXPROTO]; - -//prints version, used internally and in main.c -void versionInfo(void); - -void serviceuser(void); - - -int getint(void); -int getrepeat(void); -int getnumbits(void); -unsigned char changeReadDisplay(void); -void consumewhitechars(void); -void changemode(void); -#if defined(BP_ENABLE_HISTORY) -int cmdhistory(void); -#endif -int getnumber(int def, int min, int max, int x); -#if defined(BUSPIRATEV4) -long getlong(long def, int min, long max, int x); -#endif - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#define CMDHISTORY 15 +#define BELL 0x07 + +#define CMDBUFLEN 256 +#define CMDLENMSK (CMDBUFLEN-1) + +extern char cmdbuf[CMDBUFLEN]; +extern unsigned int cmdend; +extern unsigned int cmdstart; +extern int cmderror; +//extern int currentproto; +//extern proto protos[MAXPROTO]; + +//prints version, used internally and in main.c +void versionInfo(void); + +void serviceuser(void); + + +int getint(void); +int getrepeat(void); +int getnumbits(void); +unsigned char changeReadDisplay(void); +void consumewhitechars(void); +void changemode(void); +#if defined(BP_ENABLE_HISTORY) +int cmdhistory(void); +#endif +int getnumber(int def, int min, int max, int x); +#if defined(BUSPIRATEV4) +long getlong(long def, int min, long max, int x); +#endif + diff --git a/Firmware/raw2wire.c b/Firmware/raw2wire.c index 7032a42d..d455b405 100644 --- a/Firmware/raw2wire.c +++ b/Firmware/raw2wire.c @@ -1,299 +1,299 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -//#include "raw2wire.h" -#include "bitbang.h" -#include "AUXpin.h" - -#include "procMenu.h" // for the userinteraction subs - - -#define R2WCLK_TRIS BP_CLK_DIR -#define R2WCLK BP_CLK - -#define R2WDIO_TRIS BP_MOSI_DIR -#define R2WDIO BP_MOSI - -#define MENU 0 -#define ISO78133ATR 1 -#define ISO78133ATR_PARSE 2 - -extern struct _modeConfig modeConfig; -extern struct _command bpCommand; - -void r2wMacro_78133Read(void); -void r2wMacro_78133Write(void); - -/* -// move into a .h or other .c??? -int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* -int getint(void); -int getrepeat(void); -void consumewhitechars(void); -extern int cmderror; -*/ - - -unsigned int R2Wread(void) -{ return (bbReadByte()); -} - -unsigned int R2Wwrite(unsigned int c) -{ bbWriteByte(c);//send byte - return 0x100; -} - -void R2Wstart(void) -{ bbI2Cstart(); - //bpWstring("(\\-/_\\)"); - BPMSG1138; - //bpWmessage(MSG_I2C_START); - BPMSG1062; -} - -void R2Wstop(void) -{ bbI2Cstop(); - //bpWstring("(_/-\\)"); - BPMSG1140; - //bpWmessage(MSG_I2C_STOP); - BPMSG1063; -} - -unsigned int R2Wbitr(void) -{ return (bbReadBit()); - //bpWmessage(MSG_BIT_NOWINPUT); -} - -unsigned int R2Wbitp(void) -{ return (bbMISO()); - //bpWmessage(MSG_BIT_NOWINPUT); -} - -void R2Wclkl(void) -{ bbCLK(0); -} - -void R2Wclkh(void) -{ bbCLK(1); -} - -void R2Wclk(void) -{ bbClockTicks(1); -} - -void R2Wdatl(void) -{ bbMOSI(0); -} - -void R2Wdath(void) -{ bbMOSI(1); -} - -void R2Wsettings(void) -{ //bpWstring("R2W (spd hiz)=( "); - BPMSG1143; - bpWdec(modeConfig.speed); bpSP; - bpWdec(modeConfig.HiZ); bpSP; - //bpWline(")\r\n"); - BPMSG1162; -} - - -void R2Wsetup(void) -{ int speed, output; - - consumewhitechars(); - speed=getint(); - consumewhitechars(); - output=getint(); - - // check for userinput (and sanitycheck it!!) - if((speed>0)&&(speed<=4)) - { modeConfig.speed=speed-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - if((output>0)&&(output<=2)) - { modeConfig.HiZ=(~(output-1)); - } - else - { speed=0; // when speed is 0 we ask the user - } - - if(speed==0) - { cmderror=0; - //bpWmessage(MSG_OPT_BB_SPEED); - BPMSG1065; - modeConfig.speed=(getnumber(1,1,4,0)-1); - //bpWmessage(MSG_OPT_OUTPUT_TYPE); - BPMSG1142; - modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); - } - else - { R2Wsettings(); - } -} -void R2Wsetup_exc(void) -{ - //writes to the PORTs write to the LATCH - R2WCLK=0; //B8 scl - R2WDIO=0; //B9 sda - R2WDIO_TRIS=1;//data input - R2WCLK_TRIS=0;//clock output - bbSetup(2, modeConfig.speed); -} - -void R2Wmacro(unsigned int c) -{ - switch(c) - { case MENU: - //bpWstring(OUMSG_R2W_MACRO_MENU); - BPMSG1144; - break; - case ISO78133ATR: - r2wMacro_78133Write(); - case ISO78133ATR_PARSE: - r2wMacro_78133Read(); - break; - default: - //bpWmessage(MSG_ERROR_MACRO); - BPMSG1016; - } -} - -void R2Wpins(pins) { - #if defined(BUSPIRATEV4) - BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); - #else - BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); - #endif -} - -// -// R2W macros - -//ISO 7813-3 Answer to reset macro for smartcards -// syntax: a0%255@^a -// **depricated, some cards are MSB first** forces LSB format for easy use -// now uses CS pin instead of AUX pin because 1,2,3 have build in pullups on CS but not AUX -void r2wMacro_78133Write(void){ - - //bpWline("ISO 7816-3 ATR (RESET on CS)"); - //bpWline("RESET HIGH, CLOCK TICK, RESET LOW"); - BPMSG1145; - - //Reset needs to start low - bbCS(0); //bpAuxLow(); - bpDelayUS(0xff); - - //RESET HIGH - bbCS(1); - - //clock tick - bbClockTicks(1); - - //reset low again - bbCS(0); //bpAuxLow(); -} - -void r2wMacro_78133Read(void){ - unsigned char m[4];//macro buffer... - unsigned char c; - unsigned int i; - - //bpWstring("ISO 7816-3 reply (uses current LSB setting): "); - BPMSG1146; - - //read and display ISO 7813-3 bytes - for(i=0; i<4; i++){ - m[i]=bbReadByte(); - bpWhex(m[i]); - bpSP; - } - bpWBR; - - //modeConfig.lsbEN=c;//return to previous LSB setting - - //parse the first two bytes for 7813-3 atr header info - //bits8:5 8=serial, 9=3wire, 10=2wire 0xf=RFU - //c=(m[0]>>4); - //bpWstring("Protocol: "); - BPMSG1147; - switch((m[0]>>4)){ - case 0x08: - //bpWline("serial"); - BPMSG1148; - break; - case 0x09: - //bpWline("3 wire"); - BPMSG1149; - break; - case 0x0A: - //bpWline("2 wire"); - BPMSG1150; - break; - case 0x0F: - //bpWline("RFU"); - BPMSG1151; - break; - default: - //bpWline("unknown"); - BPMSG1152; - break; - } - bpBR; - //bits4 RFU - //bits 3:1 structure, x00 reserved, 010 general, 110 proprietary, x01 x11, special - - //bit 8 Supports random read lengths (0=false) - //bpWstring("Read type: "); - BPMSG1153; - //c=(m[1]>>7); - if((m[1]>>7)==0){ - //bpWline("to end"); - BPMSG1154; - }else{ - //bpWline("variable length"); - BPMSG1155; - } - bpBR; - //bit 7:4 data units (0001=128, 0010 = 256, 0011=512, etc, 1111=RFU) - //bpWstring("Data units: "); - BPMSG1156; - c=((m[1]&(~0b10000111))>>3); - if(c==0){ - //bpWstring("no indication"); - BPMSG1157; - }else if(c==0b1111){ - //bpWstring("RFU"); - BPMSG1151; - }else{ - i=64;//no 0... - for(m[0]=0;m[0]0)&&(speed<=4)) + { modeConfig.speed=speed-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + if((output>0)&&(output<=2)) + { modeConfig.HiZ=(~(output-1)); + } + else + { speed=0; // when speed is 0 we ask the user + } + + if(speed==0) + { cmderror=0; + //bpWmessage(MSG_OPT_BB_SPEED); + BPMSG1065; + modeConfig.speed=(getnumber(1,1,4,0)-1); + //bpWmessage(MSG_OPT_OUTPUT_TYPE); + BPMSG1142; + modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); + } + else + { R2Wsettings(); + } +} +void R2Wsetup_exc(void) +{ + //writes to the PORTs write to the LATCH + R2WCLK=0; //B8 scl + R2WDIO=0; //B9 sda + R2WDIO_TRIS=1;//data input + R2WCLK_TRIS=0;//clock output + bbSetup(2, modeConfig.speed); +} + +void R2Wmacro(unsigned int c) +{ + switch(c) + { case MENU: + //bpWstring(OUMSG_R2W_MACRO_MENU); + BPMSG1144; + break; + case ISO78133ATR: + r2wMacro_78133Write(); + case ISO78133ATR_PARSE: + r2wMacro_78133Read(); + break; + default: + //bpWmessage(MSG_ERROR_MACRO); + BPMSG1016; + } +} + +void R2Wpins(pins) { + #if defined(BUSPIRATEV4) + BPMSG1261; //bpWline("-\t-\tSCL\tSDA"); + #else + BPMSG1231; //bpWline("SCL\tSDA\t-\t-"); + #endif +} + +// +// R2W macros + +//ISO 7813-3 Answer to reset macro for smartcards +// syntax: a0%255@^a +// **depricated, some cards are MSB first** forces LSB format for easy use +// now uses CS pin instead of AUX pin because 1,2,3 have build in pullups on CS but not AUX +void r2wMacro_78133Write(void){ + + //bpWline("ISO 7816-3 ATR (RESET on CS)"); + //bpWline("RESET HIGH, CLOCK TICK, RESET LOW"); + BPMSG1145; + + //Reset needs to start low + bbCS(0); //bpAuxLow(); + bpDelayUS(0xff); + + //RESET HIGH + bbCS(1); + + //clock tick + bbClockTicks(1); + + //reset low again + bbCS(0); //bpAuxLow(); +} + +void r2wMacro_78133Read(void){ + unsigned char m[4];//macro buffer... + unsigned char c; + unsigned int i; + + //bpWstring("ISO 7816-3 reply (uses current LSB setting): "); + BPMSG1146; + + //read and display ISO 7813-3 bytes + for(i=0; i<4; i++){ + m[i]=bbReadByte(); + bpWhex(m[i]); + bpSP; + } + bpWBR; + + //modeConfig.lsbEN=c;//return to previous LSB setting + + //parse the first two bytes for 7813-3 atr header info + //bits8:5 8=serial, 9=3wire, 10=2wire 0xf=RFU + //c=(m[0]>>4); + //bpWstring("Protocol: "); + BPMSG1147; + switch((m[0]>>4)){ + case 0x08: + //bpWline("serial"); + BPMSG1148; + break; + case 0x09: + //bpWline("3 wire"); + BPMSG1149; + break; + case 0x0A: + //bpWline("2 wire"); + BPMSG1150; + break; + case 0x0F: + //bpWline("RFU"); + BPMSG1151; + break; + default: + //bpWline("unknown"); + BPMSG1152; + break; + } + bpBR; + //bits4 RFU + //bits 3:1 structure, x00 reserved, 010 general, 110 proprietary, x01 x11, special + + //bit 8 Supports random read lengths (0=false) + //bpWstring("Read type: "); + BPMSG1153; + //c=(m[1]>>7); + if((m[1]>>7)==0){ + //bpWline("to end"); + BPMSG1154; + }else{ + //bpWline("variable length"); + BPMSG1155; + } + bpBR; + //bit 7:4 data units (0001=128, 0010 = 256, 0011=512, etc, 1111=RFU) + //bpWstring("Data units: "); + BPMSG1156; + c=((m[1]&(~0b10000111))>>3); + if(c==0){ + //bpWstring("no indication"); + BPMSG1157; + }else if(c==0b1111){ + //bpWstring("RFU"); + BPMSG1151; + }else{ + i=64;//no 0... + for(m[0]=0;m[0]0)&&(speed<=4)) - { modeConfig.speed=speed-1; - } - else - { speed=0; // when speed is 0 we ask the user - } - if((cslow>0)&&(cslow<=2)) - { r3wSettings.csl=(cslow-1); - } - else - { speed=0; // when speed is 0 we ask the user - } - if((output>0)&&(output<=2)) - { modeConfig.HiZ=(~(output-1)); - } - else - { speed=0; // when speed is 0 we ask the user - } - - if(speed==0) - { //bpWmessage(MSG_OPT_BB_SPEED); - BPMSG1065; - modeConfig.speed=(getnumber(1,1,4,0)-1); - - //bpWline("CS:\r\n 1. CS\r\n 2. /CS *default"); - BPMSG1253; - r3wSettings.csl=getnumber(2,1,2,0)-1; - - //bpWmessage(MSG_OPT_OUTPUT_TYPE); - BPMSG1142; - modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); - cmderror=0; - } - else - { R3Wsettings(); - } - - //reset the write with read variable - modeConfig.wwr=0; - modeConfig.int16=0; //8 bit -} -void R3Wsetup_exc(void) -{ - bbSetup(3, modeConfig.speed); //setup the bitbang library, must be done before calling bbCS below - //setup pins (pins are input/low when we start) - //MOSI output, low - //clock output, low - //MISO input - //CS output, high - R3WMOSI_TRIS=0; - R3WCLK_TRIS=0; - R3WMISO_TRIS=1; - - // set cs the way the user wants - bbCS(r3wSettings.csl);//takes care of custom HiZ settings too -} - -void R3Wpins(void) { - #if defined(BUSPIRATEV4) - BPMSG1258; //bpWline("CS\tMISO\tCLK\tMOSI"); - #else - BPMSG1225; //bpWline("CLK\tMOSI\tCS\tMISO"); - #endif -} - - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +//#include "raw3wire.h" +#include "bitbang.h" +#include "procMenu.h" // for the userinteraction subs + +#define R3WMOSI_TRIS BP_MOSI_DIR +#define R3WCLK_TRIS BP_CLK_DIR +#define R3WMISO_TRIS BP_MISO_DIR +#define R3WCS_TRIS BP_CS_DIR + + +#define R3WMOSI BP_MOSI +#define R3WCLK BP_CLK +#define R3WMISO BP_MISO +#define R3WCS BP_CS + +// should this come from an .h? +extern struct _modeConfig modeConfig; +extern struct _command bpCommand; +void R3Wsetup_exc(void); +/* +// move into a .h or other .c??? +int getnumber(int def, int max); // everything to make the compiler happy *dubbelzucht* +int getint(void); +int getrepeat(void); +void consumewhitechars(void); +extern int cmderror; +*/ + + +struct _R3W{ +// unsigned char wwr:1; + unsigned char csl:1; +} r3wSettings; + + +unsigned int R3Wread(void) +{ return (bbReadWriteByte(0xff)); +} + +unsigned int R3Wwrite(unsigned int c) +{ c=bbReadWriteByte(c); + if(modeConfig.wwr==1) + { return c; + } + return 0x00; +} + +void R3Wstartr(void) +{ modeConfig.wwr=1; + if(r3wSettings.csl) + { bbCS(0); + } + else + { bbCS(1); + } + //bpWmessage(MSG_CS_ENABLED); + if(r3wSettings.csl) UART1TX('/'); + BPMSG1159; +} +void R3Wstart(void) +{ modeConfig.wwr=0; + if(r3wSettings.csl) + { bbCS(0); + } + else + { bbCS(1); + } + //bpWmessage(MSG_CS_ENABLED); + if(r3wSettings.csl) UART1TX('/'); + BPMSG1159; +} +void R3Wstop(void) +{ modeConfig.wwr=0; + if(r3wSettings.csl) + { bbCS(1); + } + else + { bbCS(0); + } + //bpWmessage(MSG_CS_DISABLED); + if(r3wSettings.csl) UART1TX('/'); + BPMSG1160; +} +unsigned int R3Wbitr(void) +{ return (bbReadBit()); +} +unsigned int R3Wbitp(void) +{ return (bbMISO()); +} +void R3Wclk(void) +{ bbClockTicks(1); +} +void R3Wclkh(void) +{ bbCLK(1); // same as r2wire? +} +void R3Wclkl(void) +{ bbCLK(0); // same as r2wire? +} +void R3Wdath(void) +{ bbMOSI(1); // same as r2wire? +} +void R3Wdatl(void) +{ bbMOSI(0); // same as r2wire? +} + +void R3Wsettings(void) +{ //bpWstring("R3W (spd hiz)=( "); + BPMSG1161; + bpWdec(modeConfig.speed); bpSP; + bpWdec(r3wSettings.csl); bpSP; + bpWdec(modeConfig.HiZ); bpSP; + bpWline(")"); +} + + +void R3Wsetup(void) +{ int speed, output, cslow; + + consumewhitechars(); + speed=getint(); + consumewhitechars(); + cslow=getint(); + consumewhitechars(); + output=getint(); + + if((speed>0)&&(speed<=4)) + { modeConfig.speed=speed-1; + } + else + { speed=0; // when speed is 0 we ask the user + } + if((cslow>0)&&(cslow<=2)) + { r3wSettings.csl=(cslow-1); + } + else + { speed=0; // when speed is 0 we ask the user + } + if((output>0)&&(output<=2)) + { modeConfig.HiZ=(~(output-1)); + } + else + { speed=0; // when speed is 0 we ask the user + } + + if(speed==0) + { //bpWmessage(MSG_OPT_BB_SPEED); + BPMSG1065; + modeConfig.speed=(getnumber(1,1,4,0)-1); + + //bpWline("CS:\r\n 1. CS\r\n 2. /CS *default"); + BPMSG1253; + r3wSettings.csl=getnumber(2,1,2,0)-1; + + //bpWmessage(MSG_OPT_OUTPUT_TYPE); + BPMSG1142; + modeConfig.HiZ=(~(getnumber(1,1,2,0)-1)); + cmderror=0; + } + else + { R3Wsettings(); + } + + //reset the write with read variable + modeConfig.wwr=0; + modeConfig.int16=0; //8 bit +} +void R3Wsetup_exc(void) +{ + bbSetup(3, modeConfig.speed); //setup the bitbang library, must be done before calling bbCS below + //setup pins (pins are input/low when we start) + //MOSI output, low + //clock output, low + //MISO input + //CS output, high + R3WMOSI_TRIS=0; + R3WCLK_TRIS=0; + R3WMISO_TRIS=1; + + // set cs the way the user wants + bbCS(r3wSettings.csl);//takes care of custom HiZ settings too +} + +void R3Wpins(void) { + #if defined(BUSPIRATEV4) + BPMSG1258; //bpWline("CS\tMISO\tCLK\tMOSI"); + #else + BPMSG1225; //bpWline("CLK\tMOSI\tCS\tMISO"); + #endif +} + + diff --git a/Firmware/raw3wire.h b/Firmware/raw3wire.h index 05457a63..613eb8c7 100644 --- a/Firmware/raw3wire.h +++ b/Firmware/raw3wire.h @@ -1,35 +1,35 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -void r3wProcess(void); - -unsigned int R3Wread(void); -unsigned int R3Wwrite(unsigned int c); -void R3Wstartr(void); -void R3Wstart(void); -void R3Wstop(void); -unsigned int R3Wbitr(void); -unsigned int R3Wbitp(void); -void R3Wclk(void); -void R3Wclkh(void); -void R3Wclkl(void); -void R3Wdath(void); -void R3Wdatl(void); -void R3Wsetup(void); -void R3Wsetup_exc(void); -void R3Wpins(void); -void R3Wsettings(void); - +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +void r3wProcess(void); + +unsigned int R3Wread(void); +unsigned int R3Wwrite(unsigned int c); +void R3Wstartr(void); +void R3Wstart(void); +void R3Wstop(void); +unsigned int R3Wbitr(void); +unsigned int R3Wbitp(void); +void R3Wclk(void); +void R3Wclkh(void); +void R3Wclkl(void); +void R3Wdath(void); +void R3Wdatl(void); +void R3Wsetup(void); +void R3Wsetup_exc(void); +void R3Wpins(void); +void R3Wsettings(void); + diff --git a/Firmware/selftest.c b/Firmware/selftest.c index c916963e..ca1406b7 100644 --- a/Firmware/selftest.c +++ b/Firmware/selftest.c @@ -1,274 +1,274 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include "base.h" -#include "selftest.h" -#include "busPirateCore.h" - -#ifndef BUSPIRATEV1A -void bpTest(unsigned char p, unsigned char d); -void bpBusPinsTest(unsigned char d); -void bpADCPinTest(unsigned char a, unsigned int lval, unsigned int hval); - -static unsigned char errors; -extern struct _bpConfig bpConfig; - - -//self test, showProgress=1 displays the test results in the terminal, set to 0 for silent mode -//errors are counted in the global errors variable -//returns number of errors -unsigned char selfTest(unsigned char showProgress, unsigned char jumperTest){ -//toggle display of test results with show Progress variable - errors=0; - if(!showProgress) bpConfig.quiet=1; - -//instructions (skip pause if no display output) - if(showProgress && jumperTest){ - //bpPOSTWline("Disconnect any devices"); - //bpPOSTWline("Connect (Vpu to +5V) and (ADC to +3.3V)"); - BPMSG1163; - BPMSG1251; // //bpPOSTWline("Press a key to start"); - //JTR Not required while(!UART1RXRdy()); //wait for key - UART1RX();//discard byte - } - - //bpPOSTWline("Ctrl"); - BPMSG1164; - BP_AUX0=1; - BP_AUX0_DIR=0; - //bpPOSTWstring("AUX"); - BPMSG1165; - bpTest(BP_AUX0,1); - BP_AUX0=0; - BP_AUX0_DIR=1; - - BP_LEDMODE=1; - BP_LEDMODE_DIR=0; - //bpPOSTWstring("MODE LED"); - BPMSG1166; - bpTest(BP_LEDMODE,1); - BP_LEDMODE=0; - - BP_PULLUP_ON(); - //bpPOSTWstring("PULLUP H"); - BPMSG1167; - bpTest(BP_PULLUP,1); - BP_PULLUP_OFF(); - //bpPOSTWstring("PULLUP L"); - BPMSG1168; - bpTest(BP_PULLUP,0); - - BP_VREG_ON(); - bpDelayMS(2);//in silent mode there's not enought delay for the power supplied to come on - //bpPOSTWstring("VREG"); - BPMSG1169; - bpTest(BP_VREGEN,1); - -#if defined (BUSPIRATEV4) - BPMSG1265; //bpWline("EEPROM"); - BPMSG1266; //bpWstring("SCL"); - bpTest(BP_EE_SCL, 1); - BPMSG1267; //bpWstring("SDA"); - bpTest(BP_EE_SDA, 1); - BPMSG1268; //bpWstring("WP"); - bpTest(BP_EE_WP, 1); - BPMSG1269; //bpWstring("ACK"); - bpTest(eetest(), 0); -#endif - - //ADC check - //bpPOSTWline("ADC and supply"); - BPMSG1170; - ADCON(); // turn ADC ON - -#if defined (BUSPIRATEV4) - BPMSG1270; //bpWstring("Vusb"); - bpADCPinTest(BP_ADC_USB,V5L, V5H); - - //bpPOSTWstring("5V"); - BPMSG1171; - bpADCPinTest(BP_ADC_5V0,V5L, V5H); - - //enable 5v0 pullup and test - BP_5VPU_ON(); - BPMSG1171; //bpWstring("5V0 VPU"); - bpWstring(" "); - BPMSG1172; //VPU - - bpDelayMS(2); - bpADCPinTest(BP_ADC_VPU,V5L, V5H); - BP_5VPU_OFF(); - - //ADC test (3.3 volts) - if(jumperTest){ - //ADC is connected to 3.3 volts - //bpPOSTWstring("ADC"); - BPMSG1174; - bpADCPinTest(BP_ADC_PROBE,V33L, V33H); - } - - //bpPOSTWstring("3.3V"); - BPMSG1173; - bpADCPinTest(BP_ADC_3V3,V33L, V33H); - - //enable 3v3 pullup and test - BP_3V3PU_ON(); - BPMSG1173; //bpWstring("3V3 VPU"); - bpWstring(" "); - BPMSG1172; //VPU - - bpDelayMS(2); - bpADCPinTest(BP_ADC_VPU,V33L, V33H); - BP_3V3PU_OFF(); - -#elif defined (BUSPIRATEV3) - //v3 test - //0x030F is 5volts - //bpPOSTWstring("5V"); - BPMSG1171; - bpADCPinTest(BP_ADC_5V0,V5L, V5H); - - if(jumperTest){ - //Vpullup is connected to 5volts - //bpPOSTWstring("VPU"); - BPMSG1172; - bpADCPinTest(BP_ADC_VPU,V5L, V5H); - } - - //0x0208 is 3.3volts - //bpPOSTWstring("3.3V"); - BPMSG1173; - bpADCPinTest(BP_ADC_3V3,V33L, V33H); - - if(jumperTest){ - //ADC is connected to 3.3volts - //bpPOSTWstring("ADC"); - BPMSG1174; - bpADCPinTest(BP_ADC_PROBE,V33L, V33H); - } -#endif - - ADCOFF(); // turn ADC OFF - -//************* -// -// Test bus pins three ways, also tests on-board pullup resistors: -// 1. normal/high, 2. open collector ground, 3. open collector high. -// -//*************** - - //pullup off, pins=output & high, read input, high? - //bpPOSTWline("Bus high"); - BPMSG1175; - IODIR&= ~(ALLIO);//output - IOLAT|=ALLIO; //high - bpDelayMS(100); - bpBusPinsTest(1); - - //pullup on, pins=output & low, read input, low? - //bpPOSTWline("Bus Hi-Z 0"); - BPMSG1176; - IOLAT&= ~(ALLIO); //low - if(jumperTest){ - #if defined (BUSPIRATEV4) - BP_3V3PU_ON(); - #endif - BP_PULLUP_ON(); - } - bpDelayMS(100); - bpBusPinsTest(0); - - if(jumperTest){ - //pullup on, pins=input & low, read input, high? - //bpPOSTWline("Bus Hi-Z 1"); - BPMSG1177; - IODIR|=ALLIO;//output - bpDelayMS(100); - bpBusPinsTest(1); - #if defined (BUSPIRATEV4) - BP_3V3PU_OFF(); - #endif - } - -//instructions (skip pause if no display output) - if(showProgress && jumperTest){ - BP_VREG_ON(); - BP_MODELED_ON(); - //bpPOSTWline("MODE and VREG LEDs should be on! Any key exits."); - #if defined (BUSPIRATEV4) - BP_USBLED_ON(); - #endif - BPMSG1178; - BPMSG1250; - //JTR Not required while(!UART1RXRdy()); - UART1RX(); - #ifdef BUSPIRATEV4 - BP_USBLED_OFF(); - #endif - BP_MODELED_OFF(); - BP_VREG_OFF(); - } - - bpInit();//clean up - - BPMSG1179; - bpWdec(errors); - BPMSG1180; - bpConfig.quiet=0; - - - return errors; - -} - -void bpADCPinTest(unsigned char a, unsigned int lval, unsigned int hval){ - unsigned int b; - UART1TX('('); - b=bpADC(a); - bpWvolts(b); - UART1TX(')'); - bpTest(((b>lval)&&(blval)&&(b> 8)); - UART1TX(reading); -} - -void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt() { - IFS0bits.AD1IF = 0; // Clear ADC interrupt - reading = ADC1BUF0; // For checking out ADC results in binmode - - if(ADC1BUF0 > V_out) { // if output voltage is higher than requested voltage - OC5R = 0x00; // turn PWM off temporarily - } else { - OC5R = PWM_dutycycle; // otherwise continue normally - } -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include "smps.h" +#include "base.h" +#include "procMenu.h" + +unsigned int PWM_dutycycle, V_out, reading; + +void smpsStart(unsigned int V) { + V_out = V; + PWM_dutycycle = 128-(64000/V_out); // Duty cycle in 7 bits + V_out = V_out*45/58; // Change to 1024 bit ADC reading to make comparison faster + + // Assign pin with PPS + BP_AUX1_RPOUT = OC5_IO; + + // ADC settings + AD1CHS = BP_ADC_PROBE; // Set channel to ADC pin + IFS0bits.AD1IF = 0; // Clear ADC interrupt + IEC0bits.AD1IE = 1; // Enable ADC interrupt + AD1CON1bits.ASAM = 1; // Enable auto sample + + // PWM settings + OC5R = PWM_dutycycle; + OC5RS = 0x7F; // Set PWM period to 125 KHZ + OC5CON2 = 0x1F; + OC5CON = 0x1C06; + + AD1CON1bits.ADON = 1; // turn ADC ON +} + +void smpsStop(void) { + AD1CON1bits.ADON = 0; // Turn ADC OFF + IEC0bits.AD1IE = 0; // Disable ADC interrupt + AD1CON1bits.ASAM = 0; // Disable auto sample + OC5R = 0; + OC5RS = 0; + OC5CON2 = 0; + OC5CON = 0; + BP_AUX1_RPOUT = 0; +} + +void smpsADC(void) { + UART1TX((reading >> 8)); + UART1TX(reading); +} + +void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt() { + IFS0bits.AD1IF = 0; // Clear ADC interrupt + reading = ADC1BUF0; // For checking out ADC results in binmode + + if(ADC1BUF0 > V_out) { // if output voltage is higher than requested voltage + OC5R = 0x00; // turn PWM off temporarily + } else { + OC5R = PWM_dutycycle; // otherwise continue normally + } +} diff --git a/Firmware/smps.h b/Firmware/smps.h index 77fd6bd5..4ed53272 100644 --- a/Firmware/smps.h +++ b/Firmware/smps.h @@ -1,20 +1,20 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -void smpsStart(unsigned int V); -void smpsStop(void); -void smpsADC(void); -void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(); +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +void smpsStart(unsigned int V); +void smpsStop(void); +void smpsADC(void); +void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(); diff --git a/Firmware/translations/BPv3_en_US.h b/Firmware/translations/BPv3_en_US.h index 1acf7d74..2acbc181 100644 --- a/Firmware/translations/BPv3_en_US.h +++ b/Firmware/translations/BPv3_en_US.h @@ -1,258 +1,258 @@ -#ifdef LANGUAGE_EN_US - -#define BPMSG1000 bpMsg(0, 11) -#define BPMSG1001 bpMsgBR(11, 36) -#define BPMSG1004 bpMsgBR(47, 41) -#define BPMSG1005 bpMsg(88, 14) -#define BPMSG1006 bpMsgBR(102, 13) -#define BPMSG1007 bpMsgBR(115, 23) -#define BPMSG1008 bpMsg(138, 6) -#define BPMSG1009 bpMsgBR(144, 211) -#define BPMSG1010 bpMsgBR(355, 19) -#define BPMSG1011 bpMsgBR(374, 13) -#define BPMSG1012 bpMsgBR(387, 43) -#define BPMSG1013 bpMsg(430, 17) -#define BPMSG1014 bpMsgBR(447, 16) -#define BPMSG1015 bpMsgBR(463, 15) -#define BPMSG1016 bpMsgBR(478, 36) -#define BPMSG1017 bpMsg(514, 10) -#define BPMSG1019 bpMsg(524, 9) -#define BPMSG1020 bpMsg(533, 21) -#define BPMSG1021 bpMsg(554, 20) -#define BPMSG1022 bpMsgBR(574, 27) -#define BPMSG1023 bpMsgBR(601, 26) -#define BPMSG1024 bpMsgBR(627, 21) -#define BPMSG1025 bpMsgBR(648, 24) -#define BPMSG1026 bpMsgBR(672, 16) -#define BPMSG1027 bpMsg(688, 14) -#define BPMSG1028 bpMsgBR(702, 12) -#define BPMSG1029 bpMsgBR(714, 17) -#define BPMSG1030 bpMsg(731, 17) -#define BPMSG1031 bpMsg(748, 9) -#define BPMSG1032 bpMsg(757, 4) -#define BPMSG1033 bpMsg(761, 16) -#define BPMSG1034 bpMsgBR(777, 10) -#define BPMSG1037 bpMsgBR(787, 31) -#define BPMSG1038 bpMsg(818, 15) -#define BPMSG1039 bpMsg(833, 14) -#define BPMSG1040 bpMsgBR(847, 8) -#define BPMSG1041 bpMsgBR(855, 7) -#define BPMSG1042 bpMsgBR(862, 14) -#define BPMSG1044 bpMsg(876, 15) -#define BPMSG1045 bpMsg(891, 1) -#define BPMSG1047 bpMsg(892, 6) -#define BPMSG1048 bpMsg(898, 8) -#define BPMSG1049 bpMsg(906, 11) -#define BPMSG1050 bpMsgBR(917, 7) -#define BPMSG1051 bpMsgBR(924, 9) -#define BPMSG1052 bpMsgBR(933, 12) -#define BPMSG1053 bpMsgBR(945, 9) -#define BPMSG1054 bpMsg(954, 7) -#define BPMSG1055 bpMsgBR(961, 4) -#define BPMSG1056 bpMsg(965, 15) -#define BPMSG1057 bpMsgBR(980, 12) -#define BPMSG1058 bpMsg(992, 18) -#define BPMSG1059 bpMsgBR(1010, 33) -#define BPMSG1060 bpMsg(1043, 3) -#define BPMSG1061 bpMsg(1046, 4) -#define BPMSG1062 bpMsgBR(1050, 13) -#define BPMSG1063 bpMsgBR(1063, 12) -#define BPMSG1064 bpMsgBR(1075, 37) -#define BPMSG1065 bpMsgBR(1112, 59) -#define BPMSG1066 bpMsg(1171, 53) -#define BPMSG1067 bpMsg(1224, 44) -#define BPMSG1068 bpMsg(1268, 16) -#define BPMSG1069 bpMsg(1284, 53) -#define BPMSG1070 bpMsgBR(1337, 46) -#define BPMSG1071 bpMsgBR(1383, 7) -#define BPMSG1084 bpMsg(1390, 7) -#define BPMSG1085 bpMsgBR(1397, 5) -#define BPMSG1086 bpMsgBR(1402, 22) -#define BPMSG1087 bpMsgBR(1424, 21) -#define BPMSG1088 bpMsgBR(1445, 29) -#define BPMSG1089 bpMsg(1474, 21) -#define BPMSG1091 bpMsg(1495, 20) -#define BPMSG1092 bpMsgBR(1515, 26) -#define BPMSG1093 bpMsgBR(1541, 5) -#define BPMSG1094 bpMsgBR(1546, 10) -#define BPMSG1095 bpMsg(1556, 22) -#define BPMSG1096 bpMsg(1578, 17) -#define BPMSG1097 bpMsg(1595, 18) -#define BPMSG1098 bpMsg(1613, 12) -#define BPMSG1099 bpMsg(1625, 6) -#define BPMSG1100 bpMsgBR(1631, 2) -#define BPMSG1101 bpMsg(1633, 7) -#define BPMSG1102 bpMsg(1640, 6) -#define BPMSG1103 bpMsgBR(1646, 8) -#define BPMSG1104 bpMsgBR(1654, 8) -#define BPMSG1105 bpMsgBR(1662, 14) -#define BPMSG1106 bpMsgBR(1676, 14) -#define BPMSG1107 bpMsgBR(1690, 16) -#define BPMSG1108 bpMsg(1706, 13) -#define BPMSG1109 bpMsg(1719, 10) -#define BPMSG1110 bpMsg(1729, 21) -#define BPMSG1111 bpMsgBR(1750, 23) -#define BPMSG1112 bpMsgBR(1773, 14) -#define BPMSG1113 bpMsgBR(1787, 49) -#define BPMSG1114 bpMsgBR(1836, 21) -#define BPMSG1115 bpMsgBR(1857, 7) -#define BPMSG1116 bpMsgBR(1864, 27) -#define BPMSG1117 bpMsg(1891, 6) -#define BPMSG1118 bpMsgBR(1897, 30) -#define BPMSG1119 bpMsgBR(1927, 12) -#define BPMSG1120 bpMsgBR(1939, 34) -#define BPMSG1121 bpMsgBR(1973, 30) -#define BPMSG1123 bpMsg(2003, 27) -#define BPMSG1124 bpMsg(2030, 28) -#define BPMSG1126 bpMsg(2058, 13) -#define BPMSG1127 bpMsgBR(2071, 34) -#define BPMSG1128 bpMsgBR(2105, 18) -#define BPMSG1129 bpMsgBR(2123, 18) -#define BPMSG1130 bpMsg(2141, 4) -#define BPMSG1131 bpMsg(2145, 9) -#define BPMSG1132 bpMsg(2154, 12) -#define BPMSG1133 bpMsgBR(2166, 141) -#define BPMSG1134 bpMsgBR(2307, 20) -#define BPMSG1135 bpMsg(2327, 14) -#define BPMSG1136 bpMsg(2341, 5) -#define BPMSG1137 bpMsg(2346, 6) -#define BPMSG1138 bpMsg(2352, 8) -#define BPMSG1140 bpMsg(2360, 6) -#define BPMSG1142 bpMsgBR(2366, 79) -#define BPMSG1143 bpMsg(2445, 16) -#define BPMSG1144 bpMsg(2461, 56) -#define BPMSG1145 bpMsgBR(2517, 63) -#define BPMSG1146 bpMsg(2580, 45) -#define BPMSG1147 bpMsg(2625, 10) -#define BPMSG1148 bpMsg(2635, 6) -#define BPMSG1149 bpMsg(2641, 6) -#define BPMSG1150 bpMsg(2647, 6) -#define BPMSG1151 bpMsg(2653, 3) -#define BPMSG1152 bpMsg(2656, 7) -#define BPMSG1153 bpMsg(2663, 11) -#define BPMSG1154 bpMsg(2674, 6) -#define BPMSG1155 bpMsg(2680, 15) -#define BPMSG1156 bpMsg(2695, 12) -#define BPMSG1157 bpMsg(2707, 13) -#define BPMSG1158 bpMsg(2720, 25) -#define BPMSG1159 bpMsgBR(2745, 10) -#define BPMSG1160 bpMsgBR(2755, 11) -#define BPMSG1161 bpMsg(2766, 20) -#define BPMSG1162 bpMsg(2786, 3) -#define BPMSG1163 bpMsgBR(2789, 63) -#define BPMSG1164 bpMsgBR(2852, 4) -#define BPMSG1165 bpMsg(2856, 3) -#define BPMSG1166 bpMsg(2859, 8) -#define BPMSG1167 bpMsg(2867, 8) -#define BPMSG1168 bpMsg(2875, 8) -#define BPMSG1169 bpMsg(2883, 4) -#define BPMSG1170 bpMsgBR(2887, 14) -#define BPMSG1171 bpMsg(2901, 2) -#define BPMSG1172 bpMsg(2903, 3) -#define BPMSG1173 bpMsg(2906, 4) -#define BPMSG1174 bpMsg(2910, 3) -#define BPMSG1175 bpMsgBR(2913, 8) -#define BPMSG1176 bpMsgBR(2921, 10) -#define BPMSG1177 bpMsgBR(2931, 10) -#define BPMSG1178 bpMsgBR(2941, 32) -#define BPMSG1179 bpMsg(2973, 6) -#define BPMSG1180 bpMsgBR(2979, 8) -#define BPMSG1181 bpMsg(2987, 4) -#define BPMSG1182 bpMsg(2991, 3) -#define BPMSG1183 bpMsg(2994, 4) -#define BPMSG1184 bpMsg(2998, 2) -#define BPMSG1185 bpMsgBR(3000, 3) -#define BPMSG1186 bpMsgBR(3003, 5) -#define BPMSG1187 bpMsgBR(3008, 55) -#define BPMSG1188 bpMsgBR(3063, 53) -#define BPMSG1189 bpMsgBR(3116, 67) -#define BPMSG1190 bpMsgBR(3183, 49) -#define BPMSG1191 bpMsg(3232, 32) -#define BPMSG1192 bpMsgBR(3264, 52) -#define BPMSG1194 bpMsg(3316, 3) -#define BPMSG1195 bpMsg(3319, 3) -#define BPMSG1196 bpMsg(3322, 15) -#define BPMSG1197 bpMsg(3337, 15) -#define BPMSG1199 bpMsg(3352, 84) -#define BPMSG1200 bpMsg(3436, 33) -#define BPMSG1201 bpMsg(3469, 50) -#define BPMSG1202 bpMsg(3519, 32) -#define BPMSG1203 bpMsg(3551, 106) -#define BPMSG1204 bpMsgBR(3657, 11) -#define BPMSG1205 bpMsgBR(3668, 13) -#define BPMSG1206 bpMsgBR(3681, 14) -#define BPMSG1207 bpMsgBR(3695, 28) -#define BPMSG1208 bpMsgBR(3723, 20) -#define BPMSG1209 bpMsgBR(3743, 34) -#define BPMSG1210 bpMsg(3777, 7) -#define BPMSG1211 bpMsgBR(3784, 27) -#define BPMSG1212 bpMsgBR(3811, 2) -#define BPMSG1213 bpMsgBR(3813, 20) -#define BPMSG1214 bpMsgBR(3833, 18) -#define BPMSG1215 bpMsgBR(3851, 19) -#define BPMSG1216 bpMsgBR(3870, 29) -#define BPMSG1217 bpMsgBR(3899, 50) -#define BPMSG1218 bpMsg(3949, 19) -#define BPMSG1219 bpMsgBR(3968, 152) -#define BPMSG1220 bpMsgBR(4120, 36) -#define BPMSG1221 bpMsgBR(4156, 4) -#define BPMSG1222 bpMsgBR(4160, 5) -#define BPMSG1223 bpMsgBR(4165, 10) -#define BPMSG1224 bpMsgBR(4175, 21) -#define BPMSG1225 bpMsgBR(4196, 16) -#define BPMSG1226 bpMsgBR(4212, 10) -#define BPMSG1227 bpMsg(4222, 26) -#define BPMSG1228 bpMsg(4248, 10) -#define BPMSG1229 bpMsgBR(4258, 9) -#define BPMSG1230 bpMsgBR(4267, 11) -#define BPMSG1231 bpMsgBR(4278, 11) -#define BPMSG1232 bpMsgBR(4289, 11) -#define BPMSG1233 bpMsgBR(4300, 70) -#define BPMSG1234 bpMsg(4370, 4) -#define BPMSG1235 bpMsg(4374, 26) -#define BPMSG1236 bpMsg(4400, 10) -#define BPMSG1245 bpMsg(4410, 11) -#define BPMSG1246 bpMsg(4421, 5) -#define BPMSG1247 bpMsg(4426, 5) -#define BPMSG1248 bpMsgBR(4431, 28) -#define BPMSG1249 bpMsgBR(4459, 32) -#define BPMSG1250 bpMsgBR(4491, 15) -#define BPMSG1251 bpMsgBR(4506, 17) -#define BPMSG1252 bpMsg(4523, 27) -#define BPMSG1253 bpMsgBR(4550, 29) -#define BPMSG1254 bpMsgBR(4579, 19) -#define BPMSG1255 bpMsgBR(4598, 12) -#define BPMSG1280 bpMsgBR(4610, 19) -#define BPMSG1281 bpMsgBR(4629, 14) -#define BPMSG1282 bpMsgBR(4643, 47) -#define BPMSG1283 bpMsg(4690, 15) -#define BPMSG1284 bpMsg(4705, 15) -#define BPMSG1285 bpMsgBR(4720, 4) -#define HLP1000 bpMsgBR(4724, 33) -#define HLP1001 bpMsgBR(4757, 76) -#define HLP1002 bpMsgBR(4833, 38) -#define HLP1003 bpMsgBR(4871, 40) -#define HLP1004 bpMsgBR(4911, 21) -#define HLP1005 bpMsgBR(4932, 27) -#define HLP1006 bpMsgBR(4959, 40) -#define HLP1007 bpMsgBR(4999, 27) -#define HLP1008 bpMsgBR(5026, 46) -#define HLP1009 bpMsgBR(5072, 21) -#define HLP1010 bpMsgBR(5093, 35) -#define HLP1011 bpMsgBR(5128, 46) -#define HLP1012 bpMsgBR(5174, 28) -#define HLP1013 bpMsgBR(5202, 33) -#define HLP1014 bpMsgBR(5235, 28) -#define HLP1015 bpMsgBR(5263, 37) -#define HLP1016 bpMsgBR(5300, 33) -#define HLP1017 bpMsgBR(5333, 25) -#define HLP1018 bpMsgBR(5358, 31) -#define HLP1019 bpMsgBR(5389, 41) -#define HLP1020 bpMsgBR(5430, 37) -#define HLP1021 bpMsgBR(5467, 54) -#define HLP1022 bpMsgBR(5521, 62) - - - - -#endif +#ifdef LANGUAGE_EN_US + +#define BPMSG1000 bpMsg(0, 11) +#define BPMSG1001 bpMsgBR(11, 36) +#define BPMSG1004 bpMsgBR(47, 41) +#define BPMSG1005 bpMsg(88, 14) +#define BPMSG1006 bpMsgBR(102, 13) +#define BPMSG1007 bpMsgBR(115, 23) +#define BPMSG1008 bpMsg(138, 6) +#define BPMSG1009 bpMsgBR(144, 211) +#define BPMSG1010 bpMsgBR(355, 19) +#define BPMSG1011 bpMsgBR(374, 13) +#define BPMSG1012 bpMsgBR(387, 43) +#define BPMSG1013 bpMsg(430, 17) +#define BPMSG1014 bpMsgBR(447, 16) +#define BPMSG1015 bpMsgBR(463, 15) +#define BPMSG1016 bpMsgBR(478, 36) +#define BPMSG1017 bpMsg(514, 10) +#define BPMSG1019 bpMsg(524, 9) +#define BPMSG1020 bpMsg(533, 21) +#define BPMSG1021 bpMsg(554, 20) +#define BPMSG1022 bpMsgBR(574, 27) +#define BPMSG1023 bpMsgBR(601, 26) +#define BPMSG1024 bpMsgBR(627, 21) +#define BPMSG1025 bpMsgBR(648, 24) +#define BPMSG1026 bpMsgBR(672, 16) +#define BPMSG1027 bpMsg(688, 14) +#define BPMSG1028 bpMsgBR(702, 12) +#define BPMSG1029 bpMsgBR(714, 17) +#define BPMSG1030 bpMsg(731, 17) +#define BPMSG1031 bpMsg(748, 9) +#define BPMSG1032 bpMsg(757, 4) +#define BPMSG1033 bpMsg(761, 16) +#define BPMSG1034 bpMsgBR(777, 10) +#define BPMSG1037 bpMsgBR(787, 31) +#define BPMSG1038 bpMsg(818, 15) +#define BPMSG1039 bpMsg(833, 14) +#define BPMSG1040 bpMsgBR(847, 8) +#define BPMSG1041 bpMsgBR(855, 7) +#define BPMSG1042 bpMsgBR(862, 14) +#define BPMSG1044 bpMsg(876, 15) +#define BPMSG1045 bpMsg(891, 1) +#define BPMSG1047 bpMsg(892, 6) +#define BPMSG1048 bpMsg(898, 8) +#define BPMSG1049 bpMsg(906, 11) +#define BPMSG1050 bpMsgBR(917, 7) +#define BPMSG1051 bpMsgBR(924, 9) +#define BPMSG1052 bpMsgBR(933, 12) +#define BPMSG1053 bpMsgBR(945, 9) +#define BPMSG1054 bpMsg(954, 7) +#define BPMSG1055 bpMsgBR(961, 4) +#define BPMSG1056 bpMsg(965, 15) +#define BPMSG1057 bpMsgBR(980, 12) +#define BPMSG1058 bpMsg(992, 18) +#define BPMSG1059 bpMsgBR(1010, 33) +#define BPMSG1060 bpMsg(1043, 3) +#define BPMSG1061 bpMsg(1046, 4) +#define BPMSG1062 bpMsgBR(1050, 13) +#define BPMSG1063 bpMsgBR(1063, 12) +#define BPMSG1064 bpMsgBR(1075, 37) +#define BPMSG1065 bpMsgBR(1112, 59) +#define BPMSG1066 bpMsg(1171, 53) +#define BPMSG1067 bpMsg(1224, 44) +#define BPMSG1068 bpMsg(1268, 16) +#define BPMSG1069 bpMsg(1284, 53) +#define BPMSG1070 bpMsgBR(1337, 46) +#define BPMSG1071 bpMsgBR(1383, 7) +#define BPMSG1084 bpMsg(1390, 7) +#define BPMSG1085 bpMsgBR(1397, 5) +#define BPMSG1086 bpMsgBR(1402, 22) +#define BPMSG1087 bpMsgBR(1424, 21) +#define BPMSG1088 bpMsgBR(1445, 29) +#define BPMSG1089 bpMsg(1474, 21) +#define BPMSG1091 bpMsg(1495, 20) +#define BPMSG1092 bpMsgBR(1515, 26) +#define BPMSG1093 bpMsgBR(1541, 5) +#define BPMSG1094 bpMsgBR(1546, 10) +#define BPMSG1095 bpMsg(1556, 22) +#define BPMSG1096 bpMsg(1578, 17) +#define BPMSG1097 bpMsg(1595, 18) +#define BPMSG1098 bpMsg(1613, 12) +#define BPMSG1099 bpMsg(1625, 6) +#define BPMSG1100 bpMsgBR(1631, 2) +#define BPMSG1101 bpMsg(1633, 7) +#define BPMSG1102 bpMsg(1640, 6) +#define BPMSG1103 bpMsgBR(1646, 8) +#define BPMSG1104 bpMsgBR(1654, 8) +#define BPMSG1105 bpMsgBR(1662, 14) +#define BPMSG1106 bpMsgBR(1676, 14) +#define BPMSG1107 bpMsgBR(1690, 16) +#define BPMSG1108 bpMsg(1706, 13) +#define BPMSG1109 bpMsg(1719, 10) +#define BPMSG1110 bpMsg(1729, 21) +#define BPMSG1111 bpMsgBR(1750, 23) +#define BPMSG1112 bpMsgBR(1773, 14) +#define BPMSG1113 bpMsgBR(1787, 49) +#define BPMSG1114 bpMsgBR(1836, 21) +#define BPMSG1115 bpMsgBR(1857, 7) +#define BPMSG1116 bpMsgBR(1864, 27) +#define BPMSG1117 bpMsg(1891, 6) +#define BPMSG1118 bpMsgBR(1897, 30) +#define BPMSG1119 bpMsgBR(1927, 12) +#define BPMSG1120 bpMsgBR(1939, 34) +#define BPMSG1121 bpMsgBR(1973, 30) +#define BPMSG1123 bpMsg(2003, 27) +#define BPMSG1124 bpMsg(2030, 28) +#define BPMSG1126 bpMsg(2058, 13) +#define BPMSG1127 bpMsgBR(2071, 34) +#define BPMSG1128 bpMsgBR(2105, 18) +#define BPMSG1129 bpMsgBR(2123, 18) +#define BPMSG1130 bpMsg(2141, 4) +#define BPMSG1131 bpMsg(2145, 9) +#define BPMSG1132 bpMsg(2154, 12) +#define BPMSG1133 bpMsgBR(2166, 141) +#define BPMSG1134 bpMsgBR(2307, 20) +#define BPMSG1135 bpMsg(2327, 14) +#define BPMSG1136 bpMsg(2341, 5) +#define BPMSG1137 bpMsg(2346, 6) +#define BPMSG1138 bpMsg(2352, 8) +#define BPMSG1140 bpMsg(2360, 6) +#define BPMSG1142 bpMsgBR(2366, 79) +#define BPMSG1143 bpMsg(2445, 16) +#define BPMSG1144 bpMsg(2461, 56) +#define BPMSG1145 bpMsgBR(2517, 63) +#define BPMSG1146 bpMsg(2580, 45) +#define BPMSG1147 bpMsg(2625, 10) +#define BPMSG1148 bpMsg(2635, 6) +#define BPMSG1149 bpMsg(2641, 6) +#define BPMSG1150 bpMsg(2647, 6) +#define BPMSG1151 bpMsg(2653, 3) +#define BPMSG1152 bpMsg(2656, 7) +#define BPMSG1153 bpMsg(2663, 11) +#define BPMSG1154 bpMsg(2674, 6) +#define BPMSG1155 bpMsg(2680, 15) +#define BPMSG1156 bpMsg(2695, 12) +#define BPMSG1157 bpMsg(2707, 13) +#define BPMSG1158 bpMsg(2720, 25) +#define BPMSG1159 bpMsgBR(2745, 10) +#define BPMSG1160 bpMsgBR(2755, 11) +#define BPMSG1161 bpMsg(2766, 20) +#define BPMSG1162 bpMsg(2786, 3) +#define BPMSG1163 bpMsgBR(2789, 63) +#define BPMSG1164 bpMsgBR(2852, 4) +#define BPMSG1165 bpMsg(2856, 3) +#define BPMSG1166 bpMsg(2859, 8) +#define BPMSG1167 bpMsg(2867, 8) +#define BPMSG1168 bpMsg(2875, 8) +#define BPMSG1169 bpMsg(2883, 4) +#define BPMSG1170 bpMsgBR(2887, 14) +#define BPMSG1171 bpMsg(2901, 2) +#define BPMSG1172 bpMsg(2903, 3) +#define BPMSG1173 bpMsg(2906, 4) +#define BPMSG1174 bpMsg(2910, 3) +#define BPMSG1175 bpMsgBR(2913, 8) +#define BPMSG1176 bpMsgBR(2921, 10) +#define BPMSG1177 bpMsgBR(2931, 10) +#define BPMSG1178 bpMsgBR(2941, 32) +#define BPMSG1179 bpMsg(2973, 6) +#define BPMSG1180 bpMsgBR(2979, 8) +#define BPMSG1181 bpMsg(2987, 4) +#define BPMSG1182 bpMsg(2991, 3) +#define BPMSG1183 bpMsg(2994, 4) +#define BPMSG1184 bpMsg(2998, 2) +#define BPMSG1185 bpMsgBR(3000, 3) +#define BPMSG1186 bpMsgBR(3003, 5) +#define BPMSG1187 bpMsgBR(3008, 55) +#define BPMSG1188 bpMsgBR(3063, 53) +#define BPMSG1189 bpMsgBR(3116, 67) +#define BPMSG1190 bpMsgBR(3183, 49) +#define BPMSG1191 bpMsg(3232, 32) +#define BPMSG1192 bpMsgBR(3264, 52) +#define BPMSG1194 bpMsg(3316, 3) +#define BPMSG1195 bpMsg(3319, 3) +#define BPMSG1196 bpMsg(3322, 15) +#define BPMSG1197 bpMsg(3337, 15) +#define BPMSG1199 bpMsg(3352, 84) +#define BPMSG1200 bpMsg(3436, 33) +#define BPMSG1201 bpMsg(3469, 50) +#define BPMSG1202 bpMsg(3519, 32) +#define BPMSG1203 bpMsg(3551, 106) +#define BPMSG1204 bpMsgBR(3657, 11) +#define BPMSG1205 bpMsgBR(3668, 13) +#define BPMSG1206 bpMsgBR(3681, 14) +#define BPMSG1207 bpMsgBR(3695, 28) +#define BPMSG1208 bpMsgBR(3723, 20) +#define BPMSG1209 bpMsgBR(3743, 34) +#define BPMSG1210 bpMsg(3777, 7) +#define BPMSG1211 bpMsgBR(3784, 27) +#define BPMSG1212 bpMsgBR(3811, 2) +#define BPMSG1213 bpMsgBR(3813, 20) +#define BPMSG1214 bpMsgBR(3833, 18) +#define BPMSG1215 bpMsgBR(3851, 19) +#define BPMSG1216 bpMsgBR(3870, 29) +#define BPMSG1217 bpMsgBR(3899, 50) +#define BPMSG1218 bpMsg(3949, 19) +#define BPMSG1219 bpMsgBR(3968, 152) +#define BPMSG1220 bpMsgBR(4120, 36) +#define BPMSG1221 bpMsgBR(4156, 4) +#define BPMSG1222 bpMsgBR(4160, 5) +#define BPMSG1223 bpMsgBR(4165, 10) +#define BPMSG1224 bpMsgBR(4175, 21) +#define BPMSG1225 bpMsgBR(4196, 16) +#define BPMSG1226 bpMsgBR(4212, 10) +#define BPMSG1227 bpMsg(4222, 26) +#define BPMSG1228 bpMsg(4248, 10) +#define BPMSG1229 bpMsgBR(4258, 9) +#define BPMSG1230 bpMsgBR(4267, 11) +#define BPMSG1231 bpMsgBR(4278, 11) +#define BPMSG1232 bpMsgBR(4289, 11) +#define BPMSG1233 bpMsgBR(4300, 70) +#define BPMSG1234 bpMsg(4370, 4) +#define BPMSG1235 bpMsg(4374, 26) +#define BPMSG1236 bpMsg(4400, 10) +#define BPMSG1245 bpMsg(4410, 11) +#define BPMSG1246 bpMsg(4421, 5) +#define BPMSG1247 bpMsg(4426, 5) +#define BPMSG1248 bpMsgBR(4431, 28) +#define BPMSG1249 bpMsgBR(4459, 32) +#define BPMSG1250 bpMsgBR(4491, 15) +#define BPMSG1251 bpMsgBR(4506, 17) +#define BPMSG1252 bpMsg(4523, 27) +#define BPMSG1253 bpMsgBR(4550, 29) +#define BPMSG1254 bpMsgBR(4579, 19) +#define BPMSG1255 bpMsgBR(4598, 12) +#define BPMSG1280 bpMsgBR(4610, 19) +#define BPMSG1281 bpMsgBR(4629, 14) +#define BPMSG1282 bpMsgBR(4643, 47) +#define BPMSG1283 bpMsg(4690, 15) +#define BPMSG1284 bpMsg(4705, 15) +#define BPMSG1285 bpMsgBR(4720, 4) +#define HLP1000 bpMsgBR(4724, 33) +#define HLP1001 bpMsgBR(4757, 76) +#define HLP1002 bpMsgBR(4833, 38) +#define HLP1003 bpMsgBR(4871, 40) +#define HLP1004 bpMsgBR(4911, 21) +#define HLP1005 bpMsgBR(4932, 27) +#define HLP1006 bpMsgBR(4959, 40) +#define HLP1007 bpMsgBR(4999, 27) +#define HLP1008 bpMsgBR(5026, 46) +#define HLP1009 bpMsgBR(5072, 21) +#define HLP1010 bpMsgBR(5093, 35) +#define HLP1011 bpMsgBR(5128, 46) +#define HLP1012 bpMsgBR(5174, 28) +#define HLP1013 bpMsgBR(5202, 33) +#define HLP1014 bpMsgBR(5235, 28) +#define HLP1015 bpMsgBR(5263, 37) +#define HLP1016 bpMsgBR(5300, 33) +#define HLP1017 bpMsgBR(5333, 25) +#define HLP1018 bpMsgBR(5358, 31) +#define HLP1019 bpMsgBR(5389, 41) +#define HLP1020 bpMsgBR(5430, 37) +#define HLP1021 bpMsgBR(5467, 54) +#define HLP1022 bpMsgBR(5521, 62) + + + + +#endif diff --git a/Firmware/translations/BPv4_en_US.h b/Firmware/translations/BPv4_en_US.h index b846481a..e57b353c 100644 --- a/Firmware/translations/BPv4_en_US.h +++ b/Firmware/translations/BPv4_en_US.h @@ -1,298 +1,298 @@ -#ifdef LANGUAGE_EN_US - -#define BPMSG1000 bpMsg(0, 11) -#define BPMSG1001 bpMsgBR(11, 36) -#define BPMSG1004 bpMsgBR(47, 41) -#define BPMSG1005 bpMsg(88, 14) -#define BPMSG1006 bpMsgBR(102, 13) -#define BPMSG1007 bpMsgBR(115, 23) -#define BPMSG1008 bpMsg(138, 6) -#define BPMSG1009 bpMsgBR(144, 211) -#define BPMSG1010 bpMsgBR(355, 19) -#define BPMSG1011 bpMsgBR(374, 13) -#define BPMSG1012 bpMsgBR(387, 43) -#define BPMSG1013 bpMsg(430, 17) -#define BPMSG1014 bpMsgBR(447, 16) -#define BPMSG1015 bpMsgBR(463, 15) -#define BPMSG1016 bpMsgBR(478, 36) -#define BPMSG1017 bpMsg(514, 10) -#define BPMSG1019 bpMsg(524, 9) -#define BPMSG1020 bpMsg(533, 21) -#define BPMSG1021 bpMsg(554, 20) -#define BPMSG1022 bpMsgBR(574, 27) -#define BPMSG1023 bpMsgBR(601, 26) -#define BPMSG1024 bpMsgBR(627, 21) -#define BPMSG1025 bpMsgBR(648, 24) -#define BPMSG1026 bpMsgBR(672, 16) -#define BPMSG1027 bpMsg(688, 14) -#define BPMSG1028 bpMsgBR(702, 12) -#define BPMSG1029 bpMsgBR(714, 17) -#define BPMSG1030 bpMsg(731, 17) -#define BPMSG1031 bpMsg(748, 9) -#define BPMSG1032 bpMsg(757, 4) -#define BPMSG1033 bpMsg(761, 16) -#define BPMSG1034 bpMsgBR(777, 10) -#define BPMSG1037 bpMsgBR(787, 31) -#define BPMSG1038 bpMsg(818, 15) -#define BPMSG1039 bpMsg(833, 14) -#define BPMSG1040 bpMsgBR(847, 8) -#define BPMSG1041 bpMsgBR(855, 7) -#define BPMSG1042 bpMsgBR(862, 14) -#define BPMSG1044 bpMsg(876, 15) -#define BPMSG1045 bpMsg(891, 1) -#define BPMSG1047 bpMsg(892, 6) -#define BPMSG1048 bpMsg(898, 8) -#define BPMSG1049 bpMsg(906, 11) -#define BPMSG1050 bpMsgBR(917, 7) -#define BPMSG1051 bpMsgBR(924, 9) -#define BPMSG1052 bpMsgBR(933, 12) -#define BPMSG1053 bpMsgBR(945, 9) -#define BPMSG1054 bpMsg(954, 7) -#define BPMSG1055 bpMsgBR(961, 4) -#define BPMSG1056 bpMsg(965, 15) -#define BPMSG1057 bpMsgBR(980, 12) -#define BPMSG1058 bpMsg(992, 18) -#define BPMSG1059 bpMsgBR(1010, 33) -#define BPMSG1060 bpMsg(1043, 3) -#define BPMSG1061 bpMsg(1046, 4) -#define BPMSG1062 bpMsgBR(1050, 13) -#define BPMSG1063 bpMsgBR(1063, 12) -#define BPMSG1064 bpMsgBR(1075, 37) -#define BPMSG1065 bpMsgBR(1112, 59) -#define BPMSG1066 bpMsg(1171, 53) -#define BPMSG1067 bpMsg(1224, 44) -#define BPMSG1068 bpMsg(1268, 16) -#define BPMSG1069 bpMsg(1284, 123) -#define BPMSG1070 bpMsgBR(1407, 46) -#define BPMSG1071 bpMsgBR(1453, 7) -#define BPMSG1072 bpMsgBR(1460, 34) -#define BPMSG1073 bpMsgBR(1494, 6) -#define BPMSG1074 bpMsg(1500, 14) -#define BPMSG1075 bpMsg(1514, 3) -#define BPMSG1076 bpMsgBR(1517, 3) -#define BPMSG1077 bpMsgBR(1520, 7) -#define BPMSG1078 bpMsgBR(1527, 12) -#define BPMSG1079 bpMsgBR(1539, 13) -#define BPMSG1080 bpMsg(1552, 8) -#define BPMSG1081 bpMsg(1560, 7) -#define BPMSG1082 bpMsgBR(1567, 21) -#define BPMSG1083 bpMsgBR(1588, 32) -#define BPMSG1084 bpMsg(1620, 7) -#define BPMSG1085 bpMsgBR(1627, 5) -#define BPMSG1086 bpMsgBR(1632, 22) -#define BPMSG1087 bpMsgBR(1654, 21) -#define BPMSG1088 bpMsgBR(1675, 29) -#define BPMSG1089 bpMsg(1704, 21) -#define BPMSG1091 bpMsg(1725, 20) -#define BPMSG1092 bpMsgBR(1745, 26) -#define BPMSG1093 bpMsgBR(1771, 5) -#define BPMSG1094 bpMsgBR(1776, 10) -#define BPMSG1095 bpMsg(1786, 22) -#define BPMSG1096 bpMsg(1808, 17) -#define BPMSG1097 bpMsg(1825, 18) -#define BPMSG1098 bpMsg(1843, 12) -#define BPMSG1099 bpMsg(1855, 6) -#define BPMSG1100 bpMsgBR(1861, 2) -#define BPMSG1101 bpMsg(1863, 7) -#define BPMSG1102 bpMsg(1870, 6) -#define BPMSG1103 bpMsgBR(1876, 8) -#define BPMSG1104 bpMsgBR(1884, 8) -#define BPMSG1105 bpMsgBR(1892, 14) -#define BPMSG1106 bpMsgBR(1906, 14) -#define BPMSG1107 bpMsgBR(1920, 16) -#define BPMSG1108 bpMsg(1936, 13) -#define BPMSG1109 bpMsg(1949, 10) -#define BPMSG1110 bpMsg(1959, 21) -#define BPMSG1111 bpMsgBR(1980, 23) -#define BPMSG1112 bpMsgBR(2003, 14) -#define BPMSG1113 bpMsgBR(2017, 49) -#define BPMSG1114 bpMsgBR(2066, 21) -#define BPMSG1115 bpMsgBR(2087, 7) -#define BPMSG1116 bpMsgBR(2094, 27) -#define BPMSG1117 bpMsg(2121, 6) -#define BPMSG1118 bpMsgBR(2127, 30) -#define BPMSG1119 bpMsgBR(2157, 12) -#define BPMSG1120 bpMsgBR(2169, 34) -#define BPMSG1121 bpMsgBR(2203, 30) -#define BPMSG1123 bpMsg(2233, 27) -#define BPMSG1124 bpMsg(2260, 28) -#define BPMSG1126 bpMsg(2288, 13) -#define BPMSG1127 bpMsgBR(2301, 34) -#define BPMSG1128 bpMsgBR(2335, 18) -#define BPMSG1129 bpMsgBR(2353, 18) -#define BPMSG1130 bpMsg(2371, 4) -#define BPMSG1131 bpMsg(2375, 9) -#define BPMSG1132 bpMsg(2384, 12) -#define BPMSG1133 bpMsgBR(2396, 190) -#define BPMSG1134 bpMsgBR(2586, 20) -#define BPMSG1135 bpMsg(2606, 14) -#define BPMSG1136 bpMsg(2620, 5) -#define BPMSG1137 bpMsg(2625, 6) -#define BPMSG1138 bpMsg(2631, 8) -#define BPMSG1140 bpMsg(2639, 6) -#define BPMSG1142 bpMsgBR(2645, 79) -#define BPMSG1143 bpMsg(2724, 16) -#define BPMSG1144 bpMsg(2740, 56) -#define BPMSG1145 bpMsgBR(2796, 63) -#define BPMSG1146 bpMsg(2859, 45) -#define BPMSG1147 bpMsg(2904, 10) -#define BPMSG1148 bpMsg(2914, 6) -#define BPMSG1149 bpMsg(2920, 6) -#define BPMSG1150 bpMsg(2926, 6) -#define BPMSG1151 bpMsg(2932, 3) -#define BPMSG1152 bpMsg(2935, 7) -#define BPMSG1153 bpMsg(2942, 11) -#define BPMSG1154 bpMsg(2953, 6) -#define BPMSG1155 bpMsg(2959, 15) -#define BPMSG1156 bpMsg(2974, 12) -#define BPMSG1157 bpMsg(2986, 13) -#define BPMSG1158 bpMsg(2999, 25) -#define BPMSG1159 bpMsgBR(3024, 10) -#define BPMSG1160 bpMsgBR(3034, 11) -#define BPMSG1161 bpMsg(3045, 20) -#define BPMSG1162 bpMsg(3065, 3) -#define BPMSG1163 bpMsgBR(3068, 46) -#define BPMSG1164 bpMsgBR(3114, 4) -#define BPMSG1165 bpMsg(3118, 3) -#define BPMSG1166 bpMsg(3121, 8) -#define BPMSG1167 bpMsg(3129, 8) -#define BPMSG1168 bpMsg(3137, 8) -#define BPMSG1169 bpMsg(3145, 4) -#define BPMSG1170 bpMsgBR(3149, 14) -#define BPMSG1171 bpMsg(3163, 2) -#define BPMSG1172 bpMsg(3165, 3) -#define BPMSG1173 bpMsg(3168, 4) -#define BPMSG1174 bpMsg(3172, 3) -#define BPMSG1175 bpMsgBR(3175, 8) -#define BPMSG1176 bpMsgBR(3183, 10) -#define BPMSG1177 bpMsgBR(3193, 10) -#define BPMSG1178 bpMsgBR(3203, 38) -#define BPMSG1179 bpMsg(3241, 6) -#define BPMSG1180 bpMsgBR(3247, 8) -#define BPMSG1181 bpMsg(3255, 4) -#define BPMSG1182 bpMsg(3259, 3) -#define BPMSG1183 bpMsg(3262, 4) -#define BPMSG1184 bpMsg(3266, 2) -#define BPMSG1185 bpMsgBR(3268, 3) -#define BPMSG1186 bpMsgBR(3271, 5) -#define BPMSG1187 bpMsgBR(3276, 55) -#define BPMSG1188 bpMsgBR(3331, 53) -#define BPMSG1189 bpMsgBR(3384, 67) -#define BPMSG1190 bpMsgBR(3451, 49) -#define BPMSG1191 bpMsg(3500, 32) -#define BPMSG1192 bpMsgBR(3532, 52) -#define BPMSG1194 bpMsg(3584, 3) -#define BPMSG1195 bpMsg(3587, 3) -#define BPMSG1196 bpMsg(3590, 15) -#define BPMSG1197 bpMsg(3605, 15) -#define BPMSG1199 bpMsg(3620, 84) -#define BPMSG1200 bpMsg(3704, 33) -#define BPMSG1201 bpMsg(3737, 50) -#define BPMSG1202 bpMsg(3787, 32) -#define BPMSG1203 bpMsg(3819, 124) -#define BPMSG1204 bpMsgBR(3943, 11) -#define BPMSG1205 bpMsgBR(3954, 13) -#define BPMSG1206 bpMsgBR(3967, 14) -#define BPMSG1207 bpMsgBR(3981, 28) -#define BPMSG1208 bpMsgBR(4009, 20) -#define BPMSG1209 bpMsgBR(4029, 34) -#define BPMSG1210 bpMsg(4063, 7) -#define BPMSG1211 bpMsgBR(4070, 27) -#define BPMSG1212 bpMsgBR(4097, 2) -#define BPMSG1213 bpMsgBR(4099, 20) -#define BPMSG1214 bpMsgBR(4119, 18) -#define BPMSG1215 bpMsgBR(4137, 19) -#define BPMSG1216 bpMsgBR(4156, 29) -#define BPMSG1217 bpMsgBR(4185, 50) -#define BPMSG1218 bpMsg(4235, 19) -#define BPMSG1219 bpMsgBR(4254, 152) -#define BPMSG1220 bpMsgBR(4406, 36) -#define BPMSG1221 bpMsgBR(4442, 4) -#define BPMSG1222 bpMsgBR(4446, 5) -#define BPMSG1223 bpMsgBR(4451, 10) -#define BPMSG1224 bpMsgBR(4461, 21) -#define BPMSG1225 bpMsgBR(4482, 16) -#define BPMSG1226 bpMsgBR(4498, 10) -#define BPMSG1227 bpMsg(4508, 26) -#define BPMSG1228 bpMsg(4534, 10) -#define BPMSG1229 bpMsgBR(4544, 9) -#define BPMSG1230 bpMsgBR(4553, 11) -#define BPMSG1231 bpMsgBR(4564, 11) -#define BPMSG1232 bpMsgBR(4575, 11) -#define BPMSG1233 bpMsgBR(4586, 70) -#define BPMSG1234 bpMsg(4656, 4) -#define BPMSG1235 bpMsg(4660, 26) -#define BPMSG1236 bpMsg(4686, 10) -#define BPMSG1237 bpMsg(4696, 8) -#define BPMSG1238 bpMsgBR(4704, 38) -#define BPMSG1239 bpMsgBR(4742, 28) -#define BPMSG1240 bpMsgBR(4770, 16) -#define BPMSG1241 bpMsgBR(4786, 14) -#define BPMSG1242 bpMsgBR(4800, 15) -#define BPMSG1243 bpMsgBR(4815, 5) -#define BPMSG1244 bpMsgBR(4820, 14) -#define BPMSG1245 bpMsg(4834, 11) -#define BPMSG1246 bpMsg(4845, 5) -#define BPMSG1247 bpMsg(4850, 5) -#define BPMSG1248 bpMsgBR(4855, 25) -#define BPMSG1249 bpMsgBR(4880, 32) -#define BPMSG1250 bpMsgBR(4912, 15) -#define BPMSG1251 bpMsgBR(4927, 17) -#define BPMSG1252 bpMsg(4944, 27) -#define BPMSG1253 bpMsgBR(4971, 29) -#define BPMSG1254 bpMsgBR(5000, 19) -#define BPMSG1255 bpMsgBR(5019, 12) -#define BPMSG1256 bpMsgBR(5031, 86) -#define BPMSG1257 bpMsg(5117, 36) -#define BPMSG1258 bpMsgBR(5153, 16) -#define BPMSG1259 bpMsgBR(5169, 9) -#define BPMSG1260 bpMsgBR(5178, 11) -#define BPMSG1261 bpMsgBR(5189, 11) -#define BPMSG1262 bpMsgBR(5200, 11) -#define BPMSG1263 bpMsgBR(5211, 23) -#define BPMSG1264 bpMsgBR(5234, 23) -#define BPMSG1265 bpMsgBR(5257, 6) -#define BPMSG1266 bpMsg(5263, 3) -#define BPMSG1267 bpMsg(5266, 3) -#define BPMSG1268 bpMsg(5269, 2) -#define BPMSG1269 bpMsg(5271, 10) -#define BPMSG1270 bpMsg(5281, 4) -#define BPMSG1271 bpMsgBR(5285, 87) -#define BPMSG1272 bpMsg(5372, 25) -#define BPMSG1273 bpMsgBR(5397, 7) -#define BPMSG1274 bpMsgBR(5404, 8) -#define BPMSG1278 bpMsgBR(5412, 14) -#define BPMSG1280 bpMsgBR(5426, 19) -#define BPMSG1281 bpMsgBR(5445, 14) -#define BPMSG1282 bpMsgBR(5459, 56) -#define BPMSG1283 bpMsg(5515, 15) -#define BPMSG1284 bpMsg(5530, 15) -#define BPMSG1285 bpMsgBR(5545, 4) -#define HLP1000 bpMsgBR(5549, 32) -#define HLP1001 bpMsgBR(5581, 75) -#define HLP1002 bpMsgBR(5656, 37) -#define HLP1003 bpMsgBR(5693, 39) -#define HLP1004 bpMsgBR(5732, 20) -#define HLP1005 bpMsgBR(5752, 26) -#define HLP1006 bpMsgBR(5778, 39) -#define HLP1007 bpMsgBR(5817, 26) -#define HLP1008 bpMsgBR(5843, 45) -#define HLP1009 bpMsgBR(5888, 39) -#define HLP1010 bpMsgBR(5927, 57) -#define HLP1011 bpMsgBR(5984, 52) -#define HLP1012 bpMsgBR(6036, 27) -#define HLP1013 bpMsgBR(6063, 32) -#define HLP1014 bpMsgBR(6095, 27) -#define HLP1015 bpMsgBR(6122, 36) -#define HLP1016 bpMsgBR(6158, 32) -#define HLP1017 bpMsgBR(6190, 24) -#define HLP1018 bpMsgBR(6214, 31) -#define HLP1019 bpMsgBR(6245, 40) -#define HLP1020 bpMsgBR(6285, 36) -#define HLP1021 bpMsgBR(6321, 53) -#define HLP1022 bpMsgBR(6374, 61) - - - -#endif - +#ifdef LANGUAGE_EN_US + +#define BPMSG1000 bpMsg(0, 11) +#define BPMSG1001 bpMsgBR(11, 36) +#define BPMSG1004 bpMsgBR(47, 41) +#define BPMSG1005 bpMsg(88, 14) +#define BPMSG1006 bpMsgBR(102, 13) +#define BPMSG1007 bpMsgBR(115, 23) +#define BPMSG1008 bpMsg(138, 6) +#define BPMSG1009 bpMsgBR(144, 211) +#define BPMSG1010 bpMsgBR(355, 19) +#define BPMSG1011 bpMsgBR(374, 13) +#define BPMSG1012 bpMsgBR(387, 43) +#define BPMSG1013 bpMsg(430, 17) +#define BPMSG1014 bpMsgBR(447, 16) +#define BPMSG1015 bpMsgBR(463, 15) +#define BPMSG1016 bpMsgBR(478, 36) +#define BPMSG1017 bpMsg(514, 10) +#define BPMSG1019 bpMsg(524, 9) +#define BPMSG1020 bpMsg(533, 21) +#define BPMSG1021 bpMsg(554, 20) +#define BPMSG1022 bpMsgBR(574, 27) +#define BPMSG1023 bpMsgBR(601, 26) +#define BPMSG1024 bpMsgBR(627, 21) +#define BPMSG1025 bpMsgBR(648, 24) +#define BPMSG1026 bpMsgBR(672, 16) +#define BPMSG1027 bpMsg(688, 14) +#define BPMSG1028 bpMsgBR(702, 12) +#define BPMSG1029 bpMsgBR(714, 17) +#define BPMSG1030 bpMsg(731, 17) +#define BPMSG1031 bpMsg(748, 9) +#define BPMSG1032 bpMsg(757, 4) +#define BPMSG1033 bpMsg(761, 16) +#define BPMSG1034 bpMsgBR(777, 10) +#define BPMSG1037 bpMsgBR(787, 31) +#define BPMSG1038 bpMsg(818, 15) +#define BPMSG1039 bpMsg(833, 14) +#define BPMSG1040 bpMsgBR(847, 8) +#define BPMSG1041 bpMsgBR(855, 7) +#define BPMSG1042 bpMsgBR(862, 14) +#define BPMSG1044 bpMsg(876, 15) +#define BPMSG1045 bpMsg(891, 1) +#define BPMSG1047 bpMsg(892, 6) +#define BPMSG1048 bpMsg(898, 8) +#define BPMSG1049 bpMsg(906, 11) +#define BPMSG1050 bpMsgBR(917, 7) +#define BPMSG1051 bpMsgBR(924, 9) +#define BPMSG1052 bpMsgBR(933, 12) +#define BPMSG1053 bpMsgBR(945, 9) +#define BPMSG1054 bpMsg(954, 7) +#define BPMSG1055 bpMsgBR(961, 4) +#define BPMSG1056 bpMsg(965, 15) +#define BPMSG1057 bpMsgBR(980, 12) +#define BPMSG1058 bpMsg(992, 18) +#define BPMSG1059 bpMsgBR(1010, 33) +#define BPMSG1060 bpMsg(1043, 3) +#define BPMSG1061 bpMsg(1046, 4) +#define BPMSG1062 bpMsgBR(1050, 13) +#define BPMSG1063 bpMsgBR(1063, 12) +#define BPMSG1064 bpMsgBR(1075, 37) +#define BPMSG1065 bpMsgBR(1112, 59) +#define BPMSG1066 bpMsg(1171, 53) +#define BPMSG1067 bpMsg(1224, 44) +#define BPMSG1068 bpMsg(1268, 16) +#define BPMSG1069 bpMsg(1284, 123) +#define BPMSG1070 bpMsgBR(1407, 46) +#define BPMSG1071 bpMsgBR(1453, 7) +#define BPMSG1072 bpMsgBR(1460, 34) +#define BPMSG1073 bpMsgBR(1494, 6) +#define BPMSG1074 bpMsg(1500, 14) +#define BPMSG1075 bpMsg(1514, 3) +#define BPMSG1076 bpMsgBR(1517, 3) +#define BPMSG1077 bpMsgBR(1520, 7) +#define BPMSG1078 bpMsgBR(1527, 12) +#define BPMSG1079 bpMsgBR(1539, 13) +#define BPMSG1080 bpMsg(1552, 8) +#define BPMSG1081 bpMsg(1560, 7) +#define BPMSG1082 bpMsgBR(1567, 21) +#define BPMSG1083 bpMsgBR(1588, 32) +#define BPMSG1084 bpMsg(1620, 7) +#define BPMSG1085 bpMsgBR(1627, 5) +#define BPMSG1086 bpMsgBR(1632, 22) +#define BPMSG1087 bpMsgBR(1654, 21) +#define BPMSG1088 bpMsgBR(1675, 29) +#define BPMSG1089 bpMsg(1704, 21) +#define BPMSG1091 bpMsg(1725, 20) +#define BPMSG1092 bpMsgBR(1745, 26) +#define BPMSG1093 bpMsgBR(1771, 5) +#define BPMSG1094 bpMsgBR(1776, 10) +#define BPMSG1095 bpMsg(1786, 22) +#define BPMSG1096 bpMsg(1808, 17) +#define BPMSG1097 bpMsg(1825, 18) +#define BPMSG1098 bpMsg(1843, 12) +#define BPMSG1099 bpMsg(1855, 6) +#define BPMSG1100 bpMsgBR(1861, 2) +#define BPMSG1101 bpMsg(1863, 7) +#define BPMSG1102 bpMsg(1870, 6) +#define BPMSG1103 bpMsgBR(1876, 8) +#define BPMSG1104 bpMsgBR(1884, 8) +#define BPMSG1105 bpMsgBR(1892, 14) +#define BPMSG1106 bpMsgBR(1906, 14) +#define BPMSG1107 bpMsgBR(1920, 16) +#define BPMSG1108 bpMsg(1936, 13) +#define BPMSG1109 bpMsg(1949, 10) +#define BPMSG1110 bpMsg(1959, 21) +#define BPMSG1111 bpMsgBR(1980, 23) +#define BPMSG1112 bpMsgBR(2003, 14) +#define BPMSG1113 bpMsgBR(2017, 49) +#define BPMSG1114 bpMsgBR(2066, 21) +#define BPMSG1115 bpMsgBR(2087, 7) +#define BPMSG1116 bpMsgBR(2094, 27) +#define BPMSG1117 bpMsg(2121, 6) +#define BPMSG1118 bpMsgBR(2127, 30) +#define BPMSG1119 bpMsgBR(2157, 12) +#define BPMSG1120 bpMsgBR(2169, 34) +#define BPMSG1121 bpMsgBR(2203, 30) +#define BPMSG1123 bpMsg(2233, 27) +#define BPMSG1124 bpMsg(2260, 28) +#define BPMSG1126 bpMsg(2288, 13) +#define BPMSG1127 bpMsgBR(2301, 34) +#define BPMSG1128 bpMsgBR(2335, 18) +#define BPMSG1129 bpMsgBR(2353, 18) +#define BPMSG1130 bpMsg(2371, 4) +#define BPMSG1131 bpMsg(2375, 9) +#define BPMSG1132 bpMsg(2384, 12) +#define BPMSG1133 bpMsgBR(2396, 190) +#define BPMSG1134 bpMsgBR(2586, 20) +#define BPMSG1135 bpMsg(2606, 14) +#define BPMSG1136 bpMsg(2620, 5) +#define BPMSG1137 bpMsg(2625, 6) +#define BPMSG1138 bpMsg(2631, 8) +#define BPMSG1140 bpMsg(2639, 6) +#define BPMSG1142 bpMsgBR(2645, 79) +#define BPMSG1143 bpMsg(2724, 16) +#define BPMSG1144 bpMsg(2740, 56) +#define BPMSG1145 bpMsgBR(2796, 63) +#define BPMSG1146 bpMsg(2859, 45) +#define BPMSG1147 bpMsg(2904, 10) +#define BPMSG1148 bpMsg(2914, 6) +#define BPMSG1149 bpMsg(2920, 6) +#define BPMSG1150 bpMsg(2926, 6) +#define BPMSG1151 bpMsg(2932, 3) +#define BPMSG1152 bpMsg(2935, 7) +#define BPMSG1153 bpMsg(2942, 11) +#define BPMSG1154 bpMsg(2953, 6) +#define BPMSG1155 bpMsg(2959, 15) +#define BPMSG1156 bpMsg(2974, 12) +#define BPMSG1157 bpMsg(2986, 13) +#define BPMSG1158 bpMsg(2999, 25) +#define BPMSG1159 bpMsgBR(3024, 10) +#define BPMSG1160 bpMsgBR(3034, 11) +#define BPMSG1161 bpMsg(3045, 20) +#define BPMSG1162 bpMsg(3065, 3) +#define BPMSG1163 bpMsgBR(3068, 46) +#define BPMSG1164 bpMsgBR(3114, 4) +#define BPMSG1165 bpMsg(3118, 3) +#define BPMSG1166 bpMsg(3121, 8) +#define BPMSG1167 bpMsg(3129, 8) +#define BPMSG1168 bpMsg(3137, 8) +#define BPMSG1169 bpMsg(3145, 4) +#define BPMSG1170 bpMsgBR(3149, 14) +#define BPMSG1171 bpMsg(3163, 2) +#define BPMSG1172 bpMsg(3165, 3) +#define BPMSG1173 bpMsg(3168, 4) +#define BPMSG1174 bpMsg(3172, 3) +#define BPMSG1175 bpMsgBR(3175, 8) +#define BPMSG1176 bpMsgBR(3183, 10) +#define BPMSG1177 bpMsgBR(3193, 10) +#define BPMSG1178 bpMsgBR(3203, 38) +#define BPMSG1179 bpMsg(3241, 6) +#define BPMSG1180 bpMsgBR(3247, 8) +#define BPMSG1181 bpMsg(3255, 4) +#define BPMSG1182 bpMsg(3259, 3) +#define BPMSG1183 bpMsg(3262, 4) +#define BPMSG1184 bpMsg(3266, 2) +#define BPMSG1185 bpMsgBR(3268, 3) +#define BPMSG1186 bpMsgBR(3271, 5) +#define BPMSG1187 bpMsgBR(3276, 55) +#define BPMSG1188 bpMsgBR(3331, 53) +#define BPMSG1189 bpMsgBR(3384, 67) +#define BPMSG1190 bpMsgBR(3451, 49) +#define BPMSG1191 bpMsg(3500, 32) +#define BPMSG1192 bpMsgBR(3532, 52) +#define BPMSG1194 bpMsg(3584, 3) +#define BPMSG1195 bpMsg(3587, 3) +#define BPMSG1196 bpMsg(3590, 15) +#define BPMSG1197 bpMsg(3605, 15) +#define BPMSG1199 bpMsg(3620, 84) +#define BPMSG1200 bpMsg(3704, 33) +#define BPMSG1201 bpMsg(3737, 50) +#define BPMSG1202 bpMsg(3787, 32) +#define BPMSG1203 bpMsg(3819, 124) +#define BPMSG1204 bpMsgBR(3943, 11) +#define BPMSG1205 bpMsgBR(3954, 13) +#define BPMSG1206 bpMsgBR(3967, 14) +#define BPMSG1207 bpMsgBR(3981, 28) +#define BPMSG1208 bpMsgBR(4009, 20) +#define BPMSG1209 bpMsgBR(4029, 34) +#define BPMSG1210 bpMsg(4063, 7) +#define BPMSG1211 bpMsgBR(4070, 27) +#define BPMSG1212 bpMsgBR(4097, 2) +#define BPMSG1213 bpMsgBR(4099, 20) +#define BPMSG1214 bpMsgBR(4119, 18) +#define BPMSG1215 bpMsgBR(4137, 19) +#define BPMSG1216 bpMsgBR(4156, 29) +#define BPMSG1217 bpMsgBR(4185, 50) +#define BPMSG1218 bpMsg(4235, 19) +#define BPMSG1219 bpMsgBR(4254, 152) +#define BPMSG1220 bpMsgBR(4406, 36) +#define BPMSG1221 bpMsgBR(4442, 4) +#define BPMSG1222 bpMsgBR(4446, 5) +#define BPMSG1223 bpMsgBR(4451, 10) +#define BPMSG1224 bpMsgBR(4461, 21) +#define BPMSG1225 bpMsgBR(4482, 16) +#define BPMSG1226 bpMsgBR(4498, 10) +#define BPMSG1227 bpMsg(4508, 26) +#define BPMSG1228 bpMsg(4534, 10) +#define BPMSG1229 bpMsgBR(4544, 9) +#define BPMSG1230 bpMsgBR(4553, 11) +#define BPMSG1231 bpMsgBR(4564, 11) +#define BPMSG1232 bpMsgBR(4575, 11) +#define BPMSG1233 bpMsgBR(4586, 70) +#define BPMSG1234 bpMsg(4656, 4) +#define BPMSG1235 bpMsg(4660, 26) +#define BPMSG1236 bpMsg(4686, 10) +#define BPMSG1237 bpMsg(4696, 8) +#define BPMSG1238 bpMsgBR(4704, 38) +#define BPMSG1239 bpMsgBR(4742, 28) +#define BPMSG1240 bpMsgBR(4770, 16) +#define BPMSG1241 bpMsgBR(4786, 14) +#define BPMSG1242 bpMsgBR(4800, 15) +#define BPMSG1243 bpMsgBR(4815, 5) +#define BPMSG1244 bpMsgBR(4820, 14) +#define BPMSG1245 bpMsg(4834, 11) +#define BPMSG1246 bpMsg(4845, 5) +#define BPMSG1247 bpMsg(4850, 5) +#define BPMSG1248 bpMsgBR(4855, 25) +#define BPMSG1249 bpMsgBR(4880, 32) +#define BPMSG1250 bpMsgBR(4912, 15) +#define BPMSG1251 bpMsgBR(4927, 17) +#define BPMSG1252 bpMsg(4944, 27) +#define BPMSG1253 bpMsgBR(4971, 29) +#define BPMSG1254 bpMsgBR(5000, 19) +#define BPMSG1255 bpMsgBR(5019, 12) +#define BPMSG1256 bpMsgBR(5031, 86) +#define BPMSG1257 bpMsg(5117, 36) +#define BPMSG1258 bpMsgBR(5153, 16) +#define BPMSG1259 bpMsgBR(5169, 9) +#define BPMSG1260 bpMsgBR(5178, 11) +#define BPMSG1261 bpMsgBR(5189, 11) +#define BPMSG1262 bpMsgBR(5200, 11) +#define BPMSG1263 bpMsgBR(5211, 23) +#define BPMSG1264 bpMsgBR(5234, 23) +#define BPMSG1265 bpMsgBR(5257, 6) +#define BPMSG1266 bpMsg(5263, 3) +#define BPMSG1267 bpMsg(5266, 3) +#define BPMSG1268 bpMsg(5269, 2) +#define BPMSG1269 bpMsg(5271, 10) +#define BPMSG1270 bpMsg(5281, 4) +#define BPMSG1271 bpMsgBR(5285, 87) +#define BPMSG1272 bpMsg(5372, 25) +#define BPMSG1273 bpMsgBR(5397, 7) +#define BPMSG1274 bpMsgBR(5404, 8) +#define BPMSG1278 bpMsgBR(5412, 14) +#define BPMSG1280 bpMsgBR(5426, 19) +#define BPMSG1281 bpMsgBR(5445, 14) +#define BPMSG1282 bpMsgBR(5459, 56) +#define BPMSG1283 bpMsg(5515, 15) +#define BPMSG1284 bpMsg(5530, 15) +#define BPMSG1285 bpMsgBR(5545, 4) +#define HLP1000 bpMsgBR(5549, 32) +#define HLP1001 bpMsgBR(5581, 75) +#define HLP1002 bpMsgBR(5656, 37) +#define HLP1003 bpMsgBR(5693, 39) +#define HLP1004 bpMsgBR(5732, 20) +#define HLP1005 bpMsgBR(5752, 26) +#define HLP1006 bpMsgBR(5778, 39) +#define HLP1007 bpMsgBR(5817, 26) +#define HLP1008 bpMsgBR(5843, 45) +#define HLP1009 bpMsgBR(5888, 39) +#define HLP1010 bpMsgBR(5927, 57) +#define HLP1011 bpMsgBR(5984, 52) +#define HLP1012 bpMsgBR(6036, 27) +#define HLP1013 bpMsgBR(6063, 32) +#define HLP1014 bpMsgBR(6095, 27) +#define HLP1015 bpMsgBR(6122, 36) +#define HLP1016 bpMsgBR(6158, 32) +#define HLP1017 bpMsgBR(6190, 24) +#define HLP1018 bpMsgBR(6214, 31) +#define HLP1019 bpMsgBR(6245, 40) +#define HLP1020 bpMsgBR(6285, 36) +#define HLP1021 bpMsgBR(6321, 53) +#define HLP1022 bpMsgBR(6374, 61) + + + +#endif + diff --git a/Firmware/translations/tools/translate/main.c b/Firmware/translations/tools/translate/main.c index 3fbb2249..357768ab 100644 --- a/Firmware/translations/tools/translate/main.c +++ b/Firmware/translations/tools/translate/main.c @@ -1,228 +1,228 @@ -// -// -// -// -// Convertion Utility: -// Purpose: Convert textfile to assembly format. -// -// Created by: James L. Santos -// -// -// -// -// -// Copyright (c) 2010 http://www.dangerousprototypes.com by Ian Lesnet -// -// -// - -#include -#include -#include -#define LEN 256 // max dos path -#define MAXCHAR 512 - -//replace ch1 with ch2 in stringline -char *replace(char *stringline, char ch1, char ch2) -{ - char *buf; - int i=0,j=0; - char temp[MAXCHAR]; - - while(stringline[i] !='\0'){ - if(stringline[i] == ch1 ){ - temp[j] = ch2; - i++; - j++; - } - else - temp[j++] = stringline[i++]; - } - temp[j] = '\0'; - buf = (char *) malloc(strlen(temp) + 1); - strcpy(buf,temp); - return buf; -} -// remove cr-lf and extra space -char *chomp(char *raw_text){ - const char *invalid_characters = "\n\r"; - int len,j=0; - char stripline[MAXCHAR]; - len=strlen(raw_text); - char *c=raw_text; - while (*c) - { - if (!strchr(invalid_characters, *c)) - { - - if (*c==0x20) - { - if(stripline[j-1]==0x20) //if previous char is space - //don't write it anymore - j--; - } - - stripline[j++]=*c; - - - } - - c++; - } - - //remove extra space at end of line - if (stripline[j-1]==0x20) - stripline[j-1]='\0'; - else - stripline[j]='\0'; - - strcpy(raw_text,stripline); - - return raw_text; - } - -char *replace_ext(char *fname,char *ext) -{ - char szPath[LEN]; - strcpy(szPath,fname); - char *pFile = strrchr(szPath, '/'); - pFile = pFile == NULL ? szPath : pFile+1; - char *pExt = strrchr(pFile, '.'); - - if (pExt != NULL) - strcpy(pExt, ext); - else - strcat(pFile, ext); - - return pFile; -} - - - - -int main(int argc, char *argv[]) - -{ - - FILE *infile, *outsfile, *outhfile; - char hname[LEN],sname[LEN]; // storage for output filename - char buffer1[MAXCHAR],buffer2[MAXCHAR],stringdata[MAXCHAR],temp[MAXCHAR]; - char *token; - char column[3][MAXCHAR]; - int i,k,len; - printf("\n\n TEXT to Assembly Translator\n"); - printf(" http://www.dangerousprototypes.com\n\n"); - if (argc < 2) { - fprintf(stderr, "Usage: %s filename.txt\n", argv[0]); - exit(-1); - } - - - if ((infile= fopen(argv[1], "r")) == NULL) { - fprintf(stderr, "Error Opening Input file \"%s\"\n",argv[1]); - exit(-1); - } - - // set up output - // look for a '.' and replace it with .s and .h - - - - strcpy(sname,replace_ext(argv[1],".s")); - - if ((outsfile = fopen(sname, "w")) == NULL) { - fprintf(stderr,"Can't create output file.\n"); - exit(-1); - } - - strcpy(hname,replace_ext(argv[1],".h")); - if ((outhfile = fopen(hname, "w")) == NULL) { - fprintf(stderr,"Can't create output file.\n"); - exit(-1); - } - - -// read line by line and parse - k=0; - strcpy(buffer2,"#ifdef LANGUAGE_EN_US\n\n"); - - fputs(buffer2, outsfile); - fputs(buffer2, outhfile); - strcpy(buffer2,".global _bpmessages\n_bpmessages:"); - while (feof(infile) == 0) { - fgets(stringdata, sizeof(stringdata), infile); // Read line by line - - token = strtok(stringdata,";"); - - i=0; - while( token != NULL) { - strcpy(temp,token); - strcpy(column[i++],temp); - - token = strtok(NULL,";"); - } - if (i>2) { // we got 3 columns - if (strcmp(column[1],"0")==0){ - - strcpy(buffer1,"#define\t"); - - strcat(buffer1,chomp(column[0])); - strcat(buffer1,"\tbpMsg("); - itoa(k, temp, 10); - strcat(buffer1,temp); - strcat(buffer1,", "); - - } - else { - - strcpy(buffer1,"#define\t"); - - strcat(buffer1,chomp(column[0])); - strcat(buffer1,"\tbpMsgBR("); - itoa(k, temp, 10); - strcat(buffer1,temp); - strcat(buffer1,", "); - - } - - strcat(buffer2,"\n\t.pascii\t"); - - strcat(buffer2,chomp(column[2])); - - - fputs(buffer2, outsfile); - - strcpy(temp,chomp(column[2])); - - strcpy(temp,replace(temp, '\r',' ')); - strcpy(temp,replace(temp, '\t',' ')); - strcpy(temp,replace(temp, '\n',' ')); - strcpy(temp,replace(temp, '\\',' ')); - strcpy(temp,replace(temp, '\\',' ')); - - strcpy(temp,replace(temp, '\'',' ')); - len=strlen(temp)-2; - k+=len; - - itoa(len, temp, 10); - strcat(buffer1,temp); - strcat(buffer1,")\n"); - - fputs(buffer1, outhfile); - strcpy(buffer2,"\0"); - } - - } - - strcpy(buffer2,"\r\n\r\n#endif\r\n"); - fprintf(outsfile,buffer2,"%s"); - fprintf(outhfile,buffer2,"%s"); - - if (fclose(infile) != 0 || fclose(outsfile)!=0 || fclose(outhfile)!= 0){ - fprintf(stderr,"Error in closing files\n"); - } - printf("\nConversion Successful. Files created: \n"); - printf("%s and %s \n",hname,sname); - return 0; - -} +// +// +// +// +// Convertion Utility: +// Purpose: Convert textfile to assembly format. +// +// Created by: James L. Santos +// +// +// +// +// +// Copyright (c) 2010 http://www.dangerousprototypes.com by Ian Lesnet +// +// +// + +#include +#include +#include +#define LEN 256 // max dos path +#define MAXCHAR 512 + +//replace ch1 with ch2 in stringline +char *replace(char *stringline, char ch1, char ch2) +{ + char *buf; + int i=0,j=0; + char temp[MAXCHAR]; + + while(stringline[i] !='\0'){ + if(stringline[i] == ch1 ){ + temp[j] = ch2; + i++; + j++; + } + else + temp[j++] = stringline[i++]; + } + temp[j] = '\0'; + buf = (char *) malloc(strlen(temp) + 1); + strcpy(buf,temp); + return buf; +} +// remove cr-lf and extra space +char *chomp(char *raw_text){ + const char *invalid_characters = "\n\r"; + int len,j=0; + char stripline[MAXCHAR]; + len=strlen(raw_text); + char *c=raw_text; + while (*c) + { + if (!strchr(invalid_characters, *c)) + { + + if (*c==0x20) + { + if(stripline[j-1]==0x20) //if previous char is space + //don't write it anymore + j--; + } + + stripline[j++]=*c; + + + } + + c++; + } + + //remove extra space at end of line + if (stripline[j-1]==0x20) + stripline[j-1]='\0'; + else + stripline[j]='\0'; + + strcpy(raw_text,stripline); + + return raw_text; + } + +char *replace_ext(char *fname,char *ext) +{ + char szPath[LEN]; + strcpy(szPath,fname); + char *pFile = strrchr(szPath, '/'); + pFile = pFile == NULL ? szPath : pFile+1; + char *pExt = strrchr(pFile, '.'); + + if (pExt != NULL) + strcpy(pExt, ext); + else + strcat(pFile, ext); + + return pFile; +} + + + + +int main(int argc, char *argv[]) + +{ + + FILE *infile, *outsfile, *outhfile; + char hname[LEN],sname[LEN]; // storage for output filename + char buffer1[MAXCHAR],buffer2[MAXCHAR],stringdata[MAXCHAR],temp[MAXCHAR]; + char *token; + char column[3][MAXCHAR]; + int i,k,len; + printf("\n\n TEXT to Assembly Translator\n"); + printf(" http://www.dangerousprototypes.com\n\n"); + if (argc < 2) { + fprintf(stderr, "Usage: %s filename.txt\n", argv[0]); + exit(-1); + } + + + if ((infile= fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "Error Opening Input file \"%s\"\n",argv[1]); + exit(-1); + } + + // set up output + // look for a '.' and replace it with .s and .h + + + + strcpy(sname,replace_ext(argv[1],".s")); + + if ((outsfile = fopen(sname, "w")) == NULL) { + fprintf(stderr,"Can't create output file.\n"); + exit(-1); + } + + strcpy(hname,replace_ext(argv[1],".h")); + if ((outhfile = fopen(hname, "w")) == NULL) { + fprintf(stderr,"Can't create output file.\n"); + exit(-1); + } + + +// read line by line and parse + k=0; + strcpy(buffer2,"#ifdef LANGUAGE_EN_US\n\n"); + + fputs(buffer2, outsfile); + fputs(buffer2, outhfile); + strcpy(buffer2,".global _bpmessages\n_bpmessages:"); + while (feof(infile) == 0) { + fgets(stringdata, sizeof(stringdata), infile); // Read line by line + + token = strtok(stringdata,";"); + + i=0; + while( token != NULL) { + strcpy(temp,token); + strcpy(column[i++],temp); + + token = strtok(NULL,";"); + } + if (i>2) { // we got 3 columns + if (strcmp(column[1],"0")==0){ + + strcpy(buffer1,"#define\t"); + + strcat(buffer1,chomp(column[0])); + strcat(buffer1,"\tbpMsg("); + itoa(k, temp, 10); + strcat(buffer1,temp); + strcat(buffer1,", "); + + } + else { + + strcpy(buffer1,"#define\t"); + + strcat(buffer1,chomp(column[0])); + strcat(buffer1,"\tbpMsgBR("); + itoa(k, temp, 10); + strcat(buffer1,temp); + strcat(buffer1,", "); + + } + + strcat(buffer2,"\n\t.pascii\t"); + + strcat(buffer2,chomp(column[2])); + + + fputs(buffer2, outsfile); + + strcpy(temp,chomp(column[2])); + + strcpy(temp,replace(temp, '\r',' ')); + strcpy(temp,replace(temp, '\t',' ')); + strcpy(temp,replace(temp, '\n',' ')); + strcpy(temp,replace(temp, '\\',' ')); + strcpy(temp,replace(temp, '\\',' ')); + + strcpy(temp,replace(temp, '\'',' ')); + len=strlen(temp)-2; + k+=len; + + itoa(len, temp, 10); + strcat(buffer1,temp); + strcat(buffer1,")\n"); + + fputs(buffer1, outhfile); + strcpy(buffer2,"\0"); + } + + } + + strcpy(buffer2,"\r\n\r\n#endif\r\n"); + fprintf(outsfile,buffer2,"%s"); + fprintf(outhfile,buffer2,"%s"); + + if (fclose(infile) != 0 || fclose(outsfile)!=0 || fclose(outhfile)!= 0){ + fprintf(stderr,"Error in closing files\n"); + } + printf("\nConversion Successful. Files created: \n"); + printf("%s and %s \n",hname,sname); + return 0; + +} diff --git a/Firmware/uart2io.c b/Firmware/uart2io.c index ce6cc7f6..2ab1fd3d 100644 --- a/Firmware/uart2io.c +++ b/Firmware/uart2io.c @@ -1,98 +1,98 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -#include "base.h" -//#include "uart2io.h" - -//PPS Settings - Adjust in the correct hardware file. -#define UARTRX_PIN BP_MISO_RPIN -#define UARTTX_PIN BP_MOSI_RPOUT - -// Old settings remain intact -// #if defined (BUSPIRATEV3) -// #define UARTRX_PIN 7 -// #define UARTTX_PIN RPOR4bits.RP9R -// #elif defined (BUSPIRATEV4) -// #define UARTRX_PIN 22 -// #define UARTTX_PIN RPOR12bits.RP24R -// #endif - -#define UARTTX_ODC BP_MOSI_ODC - -void UART2Setup(unsigned int brg, unsigned char ODCoutput, unsigned char rxp, unsigned char dbp, unsigned char sb ){ - //use open drain control register to - //enable Hi-Z mode on hardware module outputs - //inputs are already HiZ - //if(ODCoutput==1){ - UARTTX_ODC=ODCoutput;//1; - //} - - //assign UART2 with PPS - RPINR19bits.U2RXR=UARTRX_PIN; //Inputs U2RX - UARTTX_PIN=U2TX_IO; // Outputs U2TX - - U2BRG = brg; //assign baud rate - U2MODE = 0; - U2MODEbits.BRGH = 1; - U2MODEbits.RXINV=rxp; //rx polarity - U2MODEbits.PDSEL=dbp; //databits and parity - - //U2MODEbits.PDSEL1=1;//dbp; //databits and parity - U2MODEbits.STSEL=sb; //stop bits - - U2STA = 0; - U2STAbits.UTXINV = rxp; // Change TX polarity to match RX polarity -} - -void UART2Enable(void){ - U2MODEbits.UARTEN = 1; - U2STAbits.UTXEN = 1; - IFS1bits.U2RXIF = 0; - //Tris bits are ignored by the PPS peripheral - //implemented here so the 'v' command returns the right direction. - BP_MOSI_DIR =0; //sets MOISI (TX) to output -} - -void UART2Disable(void){ - U2MODEbits.UARTEN = 0; - U2STAbits.UTXEN = 0; - RPINR19bits.U2RXR=0b11111; //B7 U2RX - - //PPS - UARTTX_PIN=0; -// #if defined(BUSPIRATEV3) -// RPOR3bits.RP6R=0; //B6 U2TX -// #elif defined(BUSPIRATEV4) -// RPOR12bits.RP24R=0; -// #endif - UARTTX_ODC=0; - //Tris bits are ignored by the PPS peripheral - //implemented here so the 'v' command returns the right direction. - BP_MOSI_DIR =1; //sets MOISI (TX) to input -} - -void UART2TX(unsigned int c){ - while(U2STAbits.UTXBF == 1); //if buffer is full, wait - U2TXREG = c; -} - -unsigned int UART2RX(void){ - while(U2STAbits.URXDA == 0); - return U2RXREG; -} - -unsigned char UART2RXRdy(void){ - return U2STAbits.URXDA; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#include "base.h" +//#include "uart2io.h" + +//PPS Settings - Adjust in the correct hardware file. +#define UARTRX_PIN BP_MISO_RPIN +#define UARTTX_PIN BP_MOSI_RPOUT + +// Old settings remain intact +// #if defined (BUSPIRATEV3) +// #define UARTRX_PIN 7 +// #define UARTTX_PIN RPOR4bits.RP9R +// #elif defined (BUSPIRATEV4) +// #define UARTRX_PIN 22 +// #define UARTTX_PIN RPOR12bits.RP24R +// #endif + +#define UARTTX_ODC BP_MOSI_ODC + +void UART2Setup(unsigned int brg, unsigned char ODCoutput, unsigned char rxp, unsigned char dbp, unsigned char sb ){ + //use open drain control register to + //enable Hi-Z mode on hardware module outputs + //inputs are already HiZ + //if(ODCoutput==1){ + UARTTX_ODC=ODCoutput;//1; + //} + + //assign UART2 with PPS + RPINR19bits.U2RXR=UARTRX_PIN; //Inputs U2RX + UARTTX_PIN=U2TX_IO; // Outputs U2TX + + U2BRG = brg; //assign baud rate + U2MODE = 0; + U2MODEbits.BRGH = 1; + U2MODEbits.RXINV=rxp; //rx polarity + U2MODEbits.PDSEL=dbp; //databits and parity + + //U2MODEbits.PDSEL1=1;//dbp; //databits and parity + U2MODEbits.STSEL=sb; //stop bits + + U2STA = 0; + U2STAbits.UTXINV = rxp; // Change TX polarity to match RX polarity +} + +void UART2Enable(void){ + U2MODEbits.UARTEN = 1; + U2STAbits.UTXEN = 1; + IFS1bits.U2RXIF = 0; + //Tris bits are ignored by the PPS peripheral + //implemented here so the 'v' command returns the right direction. + BP_MOSI_DIR =0; //sets MOISI (TX) to output +} + +void UART2Disable(void){ + U2MODEbits.UARTEN = 0; + U2STAbits.UTXEN = 0; + RPINR19bits.U2RXR=0b11111; //B7 U2RX + + //PPS + UARTTX_PIN=0; +// #if defined(BUSPIRATEV3) +// RPOR3bits.RP6R=0; //B6 U2TX +// #elif defined(BUSPIRATEV4) +// RPOR12bits.RP24R=0; +// #endif + UARTTX_ODC=0; + //Tris bits are ignored by the PPS peripheral + //implemented here so the 'v' command returns the right direction. + BP_MOSI_DIR =1; //sets MOISI (TX) to input +} + +void UART2TX(unsigned int c){ + while(U2STAbits.UTXBF == 1); //if buffer is full, wait + U2TXREG = c; +} + +unsigned int UART2RX(void){ + while(U2STAbits.URXDA == 0); + return U2RXREG; +} + +unsigned char UART2RXRdy(void){ + return U2STAbits.URXDA; +} diff --git a/Firmware/uart2io.h b/Firmware/uart2io.h index 126fae50..1bdfecb4 100644 --- a/Firmware/uart2io.h +++ b/Firmware/uart2io.h @@ -1,21 +1,21 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -void UART2Setup(unsigned int brg, unsigned char ODCoutput, unsigned char rxp, unsigned char dbpl, unsigned char sb ); -void UART2Enable(void); -void UART2Disable(void); -void UART2TX(unsigned int c); -unsigned char UART2RXRdy(void); -unsigned int UART2RX(void); +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +void UART2Setup(unsigned int brg, unsigned char ODCoutput, unsigned char rxp, unsigned char dbpl, unsigned char sb ); +void UART2Enable(void); +void UART2Disable(void); +void UART2TX(unsigned int c); +unsigned char UART2RXRdy(void); +unsigned int UART2RX(void); diff --git a/Firmware/usb_lang.h b/Firmware/usb_lang.h index c9250a23..72d7e77d 100644 --- a/Firmware/usb_lang.h +++ b/Firmware/usb_lang.h @@ -1,121 +1,121 @@ -#ifndef __USB_LANG_H__ -#define __USB_LANG_H__ - -#define USB_LANGID_Chinese_Macau_SAR 0x1404 -#define USB_LANGID_Croatian 0x041a -#define USB_LANGID_Czech 0x0405 -#define USB_LANGID_Danish 0x0406 -#define USB_LANGID_Dutch_Netherlands 0x0413 -#define USB_LANGID_Dutch_Belgium 0x0813 -#define USB_LANGID_English_United_States 0x0409 -#define USB_LANGID_English_United_Kingdom 0x0809 -#define USB_LANGID_English_Australian 0x0c09 -#define USB_LANGID_English_Canadian 0x1009 -#define USB_LANGID_English_New_Zealand 0x1409 -#define USB_LANGID_English_Ireland 0x1809 -#define USB_LANGID_English_South_Africa 0x1c09 -#define USB_LANGID_English_Jamaica 0x2009 -#define USB_LANGID_English_Caribbean 0x2409 -#define USB_LANGID_English_Belize 0x2809 -#define USB_LANGID_English_Trinidad 0x2c09 -#define USB_LANGID_English_Zimbabwe 0x3009 -#define USB_LANGID_English_Philippines 0x3409 -#define USB_LANGID_Estonian 0x0425 -#define USB_LANGID_Faeroese 0x0438 -#define USB_LANGID_Farsi 0x0429 -#define USB_LANGID_Finnish 0x040b -#define USB_LANGID_French_Standard 0x040c -#define USB_LANGID_French_Belgian 0x080c -#define USB_LANGID_French_Canadian 0x0c0c -#define USB_LANGID_French_Switzerland 0x100c -#define USB_LANGID_French_Luxembourg 0x140c -#define USB_LANGID_French_Monaco 0x180c -#define USB_LANGID_Georgian 0x0437 -#define USB_LANGID_German_Standard 0x0407 -#define USB_LANGID_German_Switzerland 0x0807 -#define USB_LANGID_German_Austria 0x0c07 -#define USB_LANGID_German_Luxembourg 0x1007 -#define USB_LANGID_German_Liechtenstein 0x1407 -#define USB_LANGID_Greek 0x0408 -#define USB_LANGID_Gujarati 0x0447 -#define USB_LANGID_Hebrew 0x040d -#define USB_LANGID_Hindi 0x0439 -#define USB_LANGID_Hungarian 0x040e -#define USB_LANGID_Icelandic 0x040f -#define USB_LANGID_Indonesian 0x0421 -#define USB_LANGID_Italian_Standard 0x0410 -#define USB_LANGID_Italian_Switzerland 0x0810 -#define USB_LANGID_Japanese 0x0411 -#define USB_LANGID_Kannada 0x044b -#define USB_LANGID_Kashmiri_India 0x0860 -#define USB_LANGID_Kazakh 0x043f -#define USB_LANGID_Konkani 0x0457 -#define USB_LANGID_Korean 0x0412 -#define USB_LANGID_Korean_Johab 0x0812 -#define USB_LANGID_Latvian 0x0426 -#define USB_LANGID_Lithuanian 0x0427 -#define USB_LANGID_Lithuanian_Classic 0x0827 -#define USB_LANGID_Macedonian 0x042f -#define USB_LANGID_Malay_Malaysian 0x043e -#define USB_LANGID_Malay_Brunei_Darussalam 0x083e -#define USB_LANGID_Malayalam 0x044c -#define USB_LANGID_Manipuri 0x0458 -#define USB_LANGID_Marathi 0x044e -#define USB_LANGID_Nepali_India 0x0861 -#define USB_LANGID_Norwegian_Bokmal 0x0414 -#define USB_LANGID_Norwegian_Nynorsk 0x0814 -#define USB_LANGID_Oriya 0x0448 -#define USB_LANGID_Polish 0x0415 -#define USB_LANGID_Portuguese_Brazil 0x0416 -#define USB_LANGID_Portuguese_Standard 0x0816 -#define USB_LANGID_Punjabi 0x0446 -#define USB_LANGID_Romanian 0x0418 -#define USB_LANGID_Russian 0x0419 -#define USB_LANGID_Sanskrit 0x044f -#define USB_LANGID_Serbian_Cyrillic 0x0c1a -#define USB_LANGID_Serbian_Latin 0x081a -#define USB_LANGID_Sindhi 0x0459 -#define USB_LANGID_Slovak 0x041b -#define USB_LANGID_Slovenian 0x0424 -#define USB_LANGID_Spanish_Traditional_Sort 0x040a -#define USB_LANGID_Spanish_Mexican 0x080a -#define USB_LANGID_Spanish_Modern_Sort 0x0c0a -#define USB_LANGID_Spanish_Guatemala 0x100a -#define USB_LANGID_Spanish_Costa_Rica 0x140a -#define USB_LANGID_Spanish_Panama 0x180a -#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a -#define USB_LANGID_Spanish_Venezuela 0x200a -#define USB_LANGID_Spanish_Colombia 0x240a -#define USB_LANGID_Spanish_Peru 0x280a -#define USB_LANGID_Spanish_Argentina 0x2c0a -#define USB_LANGID_Spanish_Ecuador 0x300a -#define USB_LANGID_Spanish_Chile 0x340a -#define USB_LANGID_Spanish_Uruguay 0x380a -#define USB_LANGID_Spanish_Paraguay 0x3c0a -#define USB_LANGID_Spanish_Bolivia 0x400a -#define USB_LANGID_Spanish_El_Salvador 0x440a -#define USB_LANGID_Spanish_Honduras 0x480a -#define USB_LANGID_Spanish_Nicaragua 0x4c0a -#define USB_LANGID_Spanish_Puerto_Rico 0x500a -#define USB_LANGID_Sutu 0x0430 -#define USB_LANGID_Swahili_Kenya 0x0441 -#define USB_LANGID_Swedish 0x041d -#define USB_LANGID_Swedish_Finland 0x081d -#define USB_LANGID_Tamil 0x0449 -#define USB_LANGID_Tatar_Tatarstan 0x0444 -#define USB_LANGID_Telugu 0x044a -#define USB_LANGID_Thai 0x041e -#define USB_LANGID_Turkish 0x041f -#define USB_LANGID_Ukrainian 0x0422 -#define USB_LANGID_Urdu_Pakistan 0x0420 -#define USB_LANGID_Urdu_India 0x0820 -#define USB_LANGID_Uzbek_Latin 0x0443 -#define USB_LANGID_Uzbek_Cyrillic 0x0843 -#define USB_LANGID_Vietnamese 0x042a -#define USB_LANGID_HID_UDD 0x04ff -#define USB_LANGID_HID_Vendor1 0xf0ff -#define USB_LANGID_HID_Vendor2 0xf4ff -#define USB_LANGID_HID_Vendor3 0xf8ff -#define USB_LANGID_HID_Vendor4 0xfcff - -#endif /*__USB_LANG_H__*/ +#ifndef __USB_LANG_H__ +#define __USB_LANG_H__ + +#define USB_LANGID_Chinese_Macau_SAR 0x1404 +#define USB_LANGID_Croatian 0x041a +#define USB_LANGID_Czech 0x0405 +#define USB_LANGID_Danish 0x0406 +#define USB_LANGID_Dutch_Netherlands 0x0413 +#define USB_LANGID_Dutch_Belgium 0x0813 +#define USB_LANGID_English_United_States 0x0409 +#define USB_LANGID_English_United_Kingdom 0x0809 +#define USB_LANGID_English_Australian 0x0c09 +#define USB_LANGID_English_Canadian 0x1009 +#define USB_LANGID_English_New_Zealand 0x1409 +#define USB_LANGID_English_Ireland 0x1809 +#define USB_LANGID_English_South_Africa 0x1c09 +#define USB_LANGID_English_Jamaica 0x2009 +#define USB_LANGID_English_Caribbean 0x2409 +#define USB_LANGID_English_Belize 0x2809 +#define USB_LANGID_English_Trinidad 0x2c09 +#define USB_LANGID_English_Zimbabwe 0x3009 +#define USB_LANGID_English_Philippines 0x3409 +#define USB_LANGID_Estonian 0x0425 +#define USB_LANGID_Faeroese 0x0438 +#define USB_LANGID_Farsi 0x0429 +#define USB_LANGID_Finnish 0x040b +#define USB_LANGID_French_Standard 0x040c +#define USB_LANGID_French_Belgian 0x080c +#define USB_LANGID_French_Canadian 0x0c0c +#define USB_LANGID_French_Switzerland 0x100c +#define USB_LANGID_French_Luxembourg 0x140c +#define USB_LANGID_French_Monaco 0x180c +#define USB_LANGID_Georgian 0x0437 +#define USB_LANGID_German_Standard 0x0407 +#define USB_LANGID_German_Switzerland 0x0807 +#define USB_LANGID_German_Austria 0x0c07 +#define USB_LANGID_German_Luxembourg 0x1007 +#define USB_LANGID_German_Liechtenstein 0x1407 +#define USB_LANGID_Greek 0x0408 +#define USB_LANGID_Gujarati 0x0447 +#define USB_LANGID_Hebrew 0x040d +#define USB_LANGID_Hindi 0x0439 +#define USB_LANGID_Hungarian 0x040e +#define USB_LANGID_Icelandic 0x040f +#define USB_LANGID_Indonesian 0x0421 +#define USB_LANGID_Italian_Standard 0x0410 +#define USB_LANGID_Italian_Switzerland 0x0810 +#define USB_LANGID_Japanese 0x0411 +#define USB_LANGID_Kannada 0x044b +#define USB_LANGID_Kashmiri_India 0x0860 +#define USB_LANGID_Kazakh 0x043f +#define USB_LANGID_Konkani 0x0457 +#define USB_LANGID_Korean 0x0412 +#define USB_LANGID_Korean_Johab 0x0812 +#define USB_LANGID_Latvian 0x0426 +#define USB_LANGID_Lithuanian 0x0427 +#define USB_LANGID_Lithuanian_Classic 0x0827 +#define USB_LANGID_Macedonian 0x042f +#define USB_LANGID_Malay_Malaysian 0x043e +#define USB_LANGID_Malay_Brunei_Darussalam 0x083e +#define USB_LANGID_Malayalam 0x044c +#define USB_LANGID_Manipuri 0x0458 +#define USB_LANGID_Marathi 0x044e +#define USB_LANGID_Nepali_India 0x0861 +#define USB_LANGID_Norwegian_Bokmal 0x0414 +#define USB_LANGID_Norwegian_Nynorsk 0x0814 +#define USB_LANGID_Oriya 0x0448 +#define USB_LANGID_Polish 0x0415 +#define USB_LANGID_Portuguese_Brazil 0x0416 +#define USB_LANGID_Portuguese_Standard 0x0816 +#define USB_LANGID_Punjabi 0x0446 +#define USB_LANGID_Romanian 0x0418 +#define USB_LANGID_Russian 0x0419 +#define USB_LANGID_Sanskrit 0x044f +#define USB_LANGID_Serbian_Cyrillic 0x0c1a +#define USB_LANGID_Serbian_Latin 0x081a +#define USB_LANGID_Sindhi 0x0459 +#define USB_LANGID_Slovak 0x041b +#define USB_LANGID_Slovenian 0x0424 +#define USB_LANGID_Spanish_Traditional_Sort 0x040a +#define USB_LANGID_Spanish_Mexican 0x080a +#define USB_LANGID_Spanish_Modern_Sort 0x0c0a +#define USB_LANGID_Spanish_Guatemala 0x100a +#define USB_LANGID_Spanish_Costa_Rica 0x140a +#define USB_LANGID_Spanish_Panama 0x180a +#define USB_LANGID_Spanish_Dominican_Republic 0x1c0a +#define USB_LANGID_Spanish_Venezuela 0x200a +#define USB_LANGID_Spanish_Colombia 0x240a +#define USB_LANGID_Spanish_Peru 0x280a +#define USB_LANGID_Spanish_Argentina 0x2c0a +#define USB_LANGID_Spanish_Ecuador 0x300a +#define USB_LANGID_Spanish_Chile 0x340a +#define USB_LANGID_Spanish_Uruguay 0x380a +#define USB_LANGID_Spanish_Paraguay 0x3c0a +#define USB_LANGID_Spanish_Bolivia 0x400a +#define USB_LANGID_Spanish_El_Salvador 0x440a +#define USB_LANGID_Spanish_Honduras 0x480a +#define USB_LANGID_Spanish_Nicaragua 0x4c0a +#define USB_LANGID_Spanish_Puerto_Rico 0x500a +#define USB_LANGID_Sutu 0x0430 +#define USB_LANGID_Swahili_Kenya 0x0441 +#define USB_LANGID_Swedish 0x041d +#define USB_LANGID_Swedish_Finland 0x081d +#define USB_LANGID_Tamil 0x0449 +#define USB_LANGID_Tatar_Tatarstan 0x0444 +#define USB_LANGID_Telugu 0x044a +#define USB_LANGID_Thai 0x041e +#define USB_LANGID_Turkish 0x041f +#define USB_LANGID_Ukrainian 0x0422 +#define USB_LANGID_Urdu_Pakistan 0x0420 +#define USB_LANGID_Urdu_India 0x0820 +#define USB_LANGID_Uzbek_Latin 0x0443 +#define USB_LANGID_Uzbek_Cyrillic 0x0843 +#define USB_LANGID_Vietnamese 0x042a +#define USB_LANGID_HID_UDD 0x04ff +#define USB_LANGID_HID_Vendor1 0xf0ff +#define USB_LANGID_HID_Vendor2 0xf4ff +#define USB_LANGID_HID_Vendor3 0xf8ff +#define USB_LANGID_HID_Vendor4 0xfcff + +#endif /*__USB_LANG_H__*/ diff --git a/firmware-STK500v2/UART.c b/firmware-STK500v2/UART.c index 9c302025..7b689f9a 100644 --- a/firmware-STK500v2/UART.c +++ b/firmware-STK500v2/UART.c @@ -1,39 +1,39 @@ -// -// -// Base user terminal UART functions -// -// -#include "base.h" - -//is data available in RX buffer? -//#define UART1RXRdy() U1STAbits.URXDA -unsigned char UART1RXRdy(void){ - return U1STAbits.URXDA; -} - -//get a byte from UART -unsigned char UART1RX(void){ - - while(U1STAbits.URXDA == 0); - return U1RXREG; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//Initialize the terminal UART -void InitializeUART1(void){ - //setup UART - U1BRG = 34;//34@32mhz=115200 - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; -} - +// +// +// Base user terminal UART functions +// +// +#include "base.h" + +//is data available in RX buffer? +//#define UART1RXRdy() U1STAbits.URXDA +unsigned char UART1RXRdy(void){ + return U1STAbits.URXDA; +} + +//get a byte from UART +unsigned char UART1RX(void){ + + while(U1STAbits.URXDA == 0); + return U1RXREG; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//Initialize the terminal UART +void InitializeUART1(void){ + //setup UART + U1BRG = 34;//34@32mhz=115200 + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; +} + diff --git a/firmware-STK500v2/UART.h b/firmware-STK500v2/UART.h index 32914f4c..49d40c14 100644 --- a/firmware-STK500v2/UART.h +++ b/firmware-STK500v2/UART.h @@ -1,4 +1,4 @@ -unsigned char UART1RXRdy(void); -unsigned char UART1RX(void); -void UART1TX(char c); -void InitializeUART1(void); +unsigned char UART1RXRdy(void); +unsigned char UART1RX(void); +void UART1TX(char c); +void InitializeUART1(void); diff --git a/firmware-STK500v2/base.c b/firmware-STK500v2/base.c index 6559d83c..cabcd6f4 100644 --- a/firmware-STK500v2/base.c +++ b/firmware-STK500v2/base.c @@ -1,71 +1,71 @@ -#include "base.h" -/* Bus Pirate init function */ -//bus pirate initialization -//setup clock, terminal UART, pins, LEDs, and display version info -void Initialize(void){ - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; - - BP_LEDMODE_DIR=0; //MODE LED output - - IODIR|=(AUX+MOSI+CLK+MISO+CS);//AUX, MOSI, CLK, MISO, CS pins to input - IOLAT&=(~AUX+MOSI+CLK+MISO+CS); //low pin - - //modify setup behavior according to defines in base.h - #ifdef BUSPIRATEV2 //only for v2 and v3, not v1a or v0a - #if defined(ENABLE_PULLUP_RESISTORS) - BP_PULLUP_ON(); //enable pullups - #else - BP_PULLUP_OFF();//disable pullups - #endif - #endif - - #if defined(ENABLE_VREG) - BP_VREG_ON(); //enable vreg - #else - BP_VREG_OFF(); //disable vreg - #endif - - //set pin configuration using peripheral pin select - BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; -} - -//delay in MS -void bpDelayMS(const unsigned int delay){ - volatile unsigned char i; - volatile unsigned int c; - - for(c=0;c - -//this sets the hardware version -//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ -//#define BUSPIRATEV2A -//#define BUSPIRATEV25 //AKA Bus Pirate v2go -//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ -#define BUSPIRATEV3 - -//these compile time options enable various features -#define ENABLE_VREG //voltage regulators always on -//#define OUTPUT_HIGH_IMPEDANCE //use high impedance outputs for multi-voltage interfacing -#ifdef OUTPUT_HIGH_IMPEDANCE - #define ENABLE_PULLUP_RESISTORS //enable on-board pullup resistors (only if Hi-Z, only on hardware v2, v3) -#endif - - -// -// -// -// END CONFIG AREA -// -// -// -#if defined(BUSPIRATEV0A) - #include "hardwarev0a.h" -#elif defined(BUSPIRATEV1A) - #include "hardwarev1a.h" -#elif defined(BUSPIRATEV2A) - #define BUSPIRATEV2 - #include "hardwarev2a.h" -#elif defined(BUSPIRATEV25) - #define BUSPIRATEV2 - #include "hardwarev25.h" -#elif defined(BUSPIRATEV3) - #define BUSPIRATEV2 - #include "hardwarev3.h" -#else - #error "No hardware defined in base.h." -#endif - -void Initialize(void); - -//delays used by many libraries -void bpDelayMS(const unsigned int delay); -void bpDelayUS(const unsigned char delay); -#endif +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +#ifndef BASE +#define BASE +#include + +//this sets the hardware version +//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ +//#define BUSPIRATEV2A +//#define BUSPIRATEV25 //AKA Bus Pirate v2go +//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ +#define BUSPIRATEV3 + +//these compile time options enable various features +#define ENABLE_VREG //voltage regulators always on +//#define OUTPUT_HIGH_IMPEDANCE //use high impedance outputs for multi-voltage interfacing +#ifdef OUTPUT_HIGH_IMPEDANCE + #define ENABLE_PULLUP_RESISTORS //enable on-board pullup resistors (only if Hi-Z, only on hardware v2, v3) +#endif + + +// +// +// +// END CONFIG AREA +// +// +// +#if defined(BUSPIRATEV0A) + #include "hardwarev0a.h" +#elif defined(BUSPIRATEV1A) + #include "hardwarev1a.h" +#elif defined(BUSPIRATEV2A) + #define BUSPIRATEV2 + #include "hardwarev2a.h" +#elif defined(BUSPIRATEV25) + #define BUSPIRATEV2 + #include "hardwarev25.h" +#elif defined(BUSPIRATEV3) + #define BUSPIRATEV2 + #include "hardwarev3.h" +#else + #error "No hardware defined in base.h." +#endif + +void Initialize(void); + +//delays used by many libraries +void bpDelayMS(const unsigned int delay); +void bpDelayUS(const unsigned char delay); +#endif diff --git a/firmware-STK500v2/command.h b/firmware-STK500v2/command.h index 28021858..cc0cbc72 100644 --- a/firmware-STK500v2/command.h +++ b/firmware-STK500v2/command.h @@ -1,117 +1,117 @@ -// This is the original command.h file available from the atmel -// website (file: AVR068.zip) -// -//**** ATMEL AVR - A P P L I C A T I O N N O T E ************************ -//* -//* Title: AVR068 - STK500 Communication Protocol -//* Filename: command.h -//* Version: 1.0 -//* Last updated: 31.01.2005 -//* -//* Support E-mail: avr@atmel.com -//* -//************************************************************************** - -// *****************[ STK message constants ]*************************** - -#define MESSAGE_START 0x1B //= ESC = 27 decimal -#define TOKEN 0x0E - -// *****************[ STK general command constants ]************************** - -#define CMD_SIGN_ON 0x01 -#define CMD_SET_PARAMETER 0x02 -#define CMD_GET_PARAMETER 0x03 -#define CMD_SET_DEVICE_PARAMETERS 0x04 -#define CMD_OSCCAL 0x05 -#define CMD_LOAD_ADDRESS 0x06 -#define CMD_FIRMWARE_UPGRADE 0x07 - - -// *****************[ STK ISP command constants ]****************************** - -#define CMD_ENTER_PROGMODE_ISP 0x10 -#define CMD_LEAVE_PROGMODE_ISP 0x11 -#define CMD_CHIP_ERASE_ISP 0x12 -#define CMD_PROGRAM_FLASH_ISP 0x13 -#define CMD_READ_FLASH_ISP 0x14 -#define CMD_PROGRAM_EEPROM_ISP 0x15 -#define CMD_READ_EEPROM_ISP 0x16 -#define CMD_PROGRAM_FUSE_ISP 0x17 -#define CMD_READ_FUSE_ISP 0x18 -#define CMD_PROGRAM_LOCK_ISP 0x19 -#define CMD_READ_LOCK_ISP 0x1A -#define CMD_READ_SIGNATURE_ISP 0x1B -#define CMD_READ_OSCCAL_ISP 0x1C -#define CMD_SPI_MULTI 0x1D - -// *****************[ STK PP command constants ]******************************* - -#define CMD_ENTER_PROGMODE_PP 0x20 -#define CMD_LEAVE_PROGMODE_PP 0x21 -#define CMD_CHIP_ERASE_PP 0x22 -#define CMD_PROGRAM_FLASH_PP 0x23 -#define CMD_READ_FLASH_PP 0x24 -#define CMD_PROGRAM_EEPROM_PP 0x25 -#define CMD_READ_EEPROM_PP 0x26 -#define CMD_PROGRAM_FUSE_PP 0x27 -#define CMD_READ_FUSE_PP 0x28 -#define CMD_PROGRAM_LOCK_PP 0x29 -#define CMD_READ_LOCK_PP 0x2A -#define CMD_READ_SIGNATURE_PP 0x2B -#define CMD_READ_OSCCAL_PP 0x2C - -#define CMD_SET_CONTROL_STACK 0x2D - -// *****************[ STK HVSP command constants ]***************************** - -#define CMD_ENTER_PROGMODE_HVSP 0x30 -#define CMD_LEAVE_PROGMODE_HVSP 0x31 -#define CMD_CHIP_ERASE_HVSP 0x32 -#define CMD_PROGRAM_FLASH_HVSP 0x33 -#define CMD_READ_FLASH_HVSP 0x34 -#define CMD_PROGRAM_EEPROM_HVSP 0x35 -#define CMD_READ_EEPROM_HVSP 0x36 -#define CMD_PROGRAM_FUSE_HVSP 0x37 -#define CMD_READ_FUSE_HVSP 0x38 -#define CMD_PROGRAM_LOCK_HVSP 0x39 -#define CMD_READ_LOCK_HVSP 0x3A -#define CMD_READ_SIGNATURE_HVSP 0x3B -#define CMD_READ_OSCCAL_HVSP 0x3C - -// *****************[ STK status constants ]*************************** - -// Success -#define STATUS_CMD_OK 0x00 - -// Warnings -#define STATUS_CMD_TOUT 0x80 -#define STATUS_RDY_BSY_TOUT 0x81 -#define STATUS_SET_PARAM_MISSING 0x82 - -// Errors -#define STATUS_CMD_FAILED 0xC0 -#define STATUS_CKSUM_ERROR 0xC1 -#define STATUS_CMD_UNKNOWN 0xC9 - -// *****************[ STK parameter constants ]*************************** -#define PARAM_BUILD_NUMBER_LOW 0x80 -#define PARAM_BUILD_NUMBER_HIGH 0x81 -#define PARAM_HW_VER 0x90 -#define PARAM_SW_MAJOR 0x91 -#define PARAM_SW_MINOR 0x92 -#define PARAM_VTARGET 0x94 -#define PARAM_VADJUST 0x95 -#define PARAM_OSC_PSCALE 0x96 -#define PARAM_OSC_CMATCH 0x97 -#define PARAM_SCK_DURATION 0x98 -#define PARAM_TOPCARD_DETECT 0x9A -#define PARAM_STATUS 0x9C -#define PARAM_DATA 0x9D -#define PARAM_RESET_POLARITY 0x9E -#define PARAM_CONTROLLER_INIT 0x9F - -// *****************[ STK answer constants ]*************************** - -#define ANSWER_CKSUM_ERROR 0xB0 - +// This is the original command.h file available from the atmel +// website (file: AVR068.zip) +// +//**** ATMEL AVR - A P P L I C A T I O N N O T E ************************ +//* +//* Title: AVR068 - STK500 Communication Protocol +//* Filename: command.h +//* Version: 1.0 +//* Last updated: 31.01.2005 +//* +//* Support E-mail: avr@atmel.com +//* +//************************************************************************** + +// *****************[ STK message constants ]*************************** + +#define MESSAGE_START 0x1B //= ESC = 27 decimal +#define TOKEN 0x0E + +// *****************[ STK general command constants ]************************** + +#define CMD_SIGN_ON 0x01 +#define CMD_SET_PARAMETER 0x02 +#define CMD_GET_PARAMETER 0x03 +#define CMD_SET_DEVICE_PARAMETERS 0x04 +#define CMD_OSCCAL 0x05 +#define CMD_LOAD_ADDRESS 0x06 +#define CMD_FIRMWARE_UPGRADE 0x07 + + +// *****************[ STK ISP command constants ]****************************** + +#define CMD_ENTER_PROGMODE_ISP 0x10 +#define CMD_LEAVE_PROGMODE_ISP 0x11 +#define CMD_CHIP_ERASE_ISP 0x12 +#define CMD_PROGRAM_FLASH_ISP 0x13 +#define CMD_READ_FLASH_ISP 0x14 +#define CMD_PROGRAM_EEPROM_ISP 0x15 +#define CMD_READ_EEPROM_ISP 0x16 +#define CMD_PROGRAM_FUSE_ISP 0x17 +#define CMD_READ_FUSE_ISP 0x18 +#define CMD_PROGRAM_LOCK_ISP 0x19 +#define CMD_READ_LOCK_ISP 0x1A +#define CMD_READ_SIGNATURE_ISP 0x1B +#define CMD_READ_OSCCAL_ISP 0x1C +#define CMD_SPI_MULTI 0x1D + +// *****************[ STK PP command constants ]******************************* + +#define CMD_ENTER_PROGMODE_PP 0x20 +#define CMD_LEAVE_PROGMODE_PP 0x21 +#define CMD_CHIP_ERASE_PP 0x22 +#define CMD_PROGRAM_FLASH_PP 0x23 +#define CMD_READ_FLASH_PP 0x24 +#define CMD_PROGRAM_EEPROM_PP 0x25 +#define CMD_READ_EEPROM_PP 0x26 +#define CMD_PROGRAM_FUSE_PP 0x27 +#define CMD_READ_FUSE_PP 0x28 +#define CMD_PROGRAM_LOCK_PP 0x29 +#define CMD_READ_LOCK_PP 0x2A +#define CMD_READ_SIGNATURE_PP 0x2B +#define CMD_READ_OSCCAL_PP 0x2C + +#define CMD_SET_CONTROL_STACK 0x2D + +// *****************[ STK HVSP command constants ]***************************** + +#define CMD_ENTER_PROGMODE_HVSP 0x30 +#define CMD_LEAVE_PROGMODE_HVSP 0x31 +#define CMD_CHIP_ERASE_HVSP 0x32 +#define CMD_PROGRAM_FLASH_HVSP 0x33 +#define CMD_READ_FLASH_HVSP 0x34 +#define CMD_PROGRAM_EEPROM_HVSP 0x35 +#define CMD_READ_EEPROM_HVSP 0x36 +#define CMD_PROGRAM_FUSE_HVSP 0x37 +#define CMD_READ_FUSE_HVSP 0x38 +#define CMD_PROGRAM_LOCK_HVSP 0x39 +#define CMD_READ_LOCK_HVSP 0x3A +#define CMD_READ_SIGNATURE_HVSP 0x3B +#define CMD_READ_OSCCAL_HVSP 0x3C + +// *****************[ STK status constants ]*************************** + +// Success +#define STATUS_CMD_OK 0x00 + +// Warnings +#define STATUS_CMD_TOUT 0x80 +#define STATUS_RDY_BSY_TOUT 0x81 +#define STATUS_SET_PARAM_MISSING 0x82 + +// Errors +#define STATUS_CMD_FAILED 0xC0 +#define STATUS_CKSUM_ERROR 0xC1 +#define STATUS_CMD_UNKNOWN 0xC9 + +// *****************[ STK parameter constants ]*************************** +#define PARAM_BUILD_NUMBER_LOW 0x80 +#define PARAM_BUILD_NUMBER_HIGH 0x81 +#define PARAM_HW_VER 0x90 +#define PARAM_SW_MAJOR 0x91 +#define PARAM_SW_MINOR 0x92 +#define PARAM_VTARGET 0x94 +#define PARAM_VADJUST 0x95 +#define PARAM_OSC_PSCALE 0x96 +#define PARAM_OSC_CMATCH 0x97 +#define PARAM_SCK_DURATION 0x98 +#define PARAM_TOPCARD_DETECT 0x9A +#define PARAM_STATUS 0x9C +#define PARAM_DATA 0x9D +#define PARAM_RESET_POLARITY 0x9E +#define PARAM_CONTROLLER_INIT 0x9F + +// *****************[ STK answer constants ]*************************** + +#define ANSWER_CKSUM_ERROR 0xB0 + diff --git a/firmware-STK500v2/hardwarev1a.h b/firmware-STK500v2/hardwarev1a.h index 271f6dd9..142a30c6 100644 --- a/firmware-STK500v2/hardwarev1a.h +++ b/firmware-STK500v2/hardwarev1a.h @@ -1,114 +1,114 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -//this hardware profile is for the second, surface mount Bus Pirate. -//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface - -#define BP_VERSION_STRING "Bus Pirate v1a" -#define BP_VERSION "v1a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -//when we move to a USB PIC, we'll need to do that.... -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 3 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_PIN6 PORTBbits.RB11 -#define BP_LEDMODE PORTBbits.RB4 -#define BP_LEDPWR PORTAbits.RA3 -#define BP_EN50V PORTAbits.RA4 -#define BP_EN33V PORTBbits.RB5 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_PIN6_DIR TRISBbits.TRISB11 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_LEDPWR_DIR TRISAbits.TRISA3 -#define BP_EN50V_DIR TRISAbits.TRISA4 -#define BP_EN33V_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 -#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 - -#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 -#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 -#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +//this hardware profile is for the second, surface mount Bus Pirate. +//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface + +#define BP_VERSION_STRING "Bus Pirate v1a" +#define BP_VERSION "v1a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +//when we move to a USB PIC, we'll need to do that.... +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 3 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_PIN6 PORTBbits.RB11 +#define BP_LEDMODE PORTBbits.RB4 +#define BP_LEDPWR PORTAbits.RA3 +#define BP_EN50V PORTAbits.RA4 +#define BP_EN33V PORTBbits.RB5 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_PIN6_DIR TRISBbits.TRISB11 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_LEDPWR_DIR TRISAbits.TRISA3 +#define BP_EN50V_DIR TRISAbits.TRISA4 +#define BP_EN33V_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 +#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 + +#define BP_AUX2_HI() BP_PIN6_DIR=1;BP_PIN6=0 +#define BP_AUX2_LOW() BP_PIN6=0;BP_PIN6_DIR=0 +#define BP_AUX2_IN() BP_PIN6_DIR=0;BP_PIN6=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-STK500v2/hardwarev3.h b/firmware-STK500v2/hardwarev3.h index 95b8f35c..6a7c2c21 100644 --- a/firmware-STK500v2/hardwarev3.h +++ b/firmware-STK500v2/hardwarev3.h @@ -1,110 +1,110 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ -//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com -#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" -#define BP_VERSION "v3" - -//set the pin to use for POST detection -#define BP_POST PORTBbits.RB0 -#define BP_POST_DIR TRISBbits.TRISB0 - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//this is a different type of pin identifier used by the bitbang library -#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) -#define MOSI 0b1000000000 //RB9 -#define CLK 0b100000000 //RB8 -#define MISO 0b10000000 //RB7 -#define CS 0b1000000 //RB6 -#define IOPOR PORTB -#define IOLAT LATB -#define IODIR TRISB - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -//define device ID and revision memory location -#define DEV_ADDR_UPPER 0x00FF -#define DEV_ADDR_TYPE 0x0000 -#define DEV_ADDR_REV 0x0002 -#define CFG_ADDR_UPPER 0x0000 -#define CFG_ADDR_1 0xABFC -#define CFG_ADDR_2 0xABFE - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ +//this profile is for the Bus Pirate v3 hardware from dangerousprototypes.com +#define BP_VERSION_STRING "Bus Pirate v3"// (Seeed Studio)" +#define BP_VERSION "v3" + +//set the pin to use for POST detection +#define BP_POST PORTBbits.RB0 +#define BP_POST_DIR TRISBbits.TRISB0 + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//this is a different type of pin identifier used by the bitbang library +#define AUX 0b10000000000 //RB10 on v1, 2, 2go, 3 (not v0) +#define MOSI 0b1000000000 //RB9 +#define CLK 0b100000000 //RB8 +#define MISO 0b10000000 //RB7 +#define CS 0b1000000 //RB6 +#define IOPOR PORTB +#define IOLAT LATB +#define IODIR TRISB + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2/3 we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +//define device ID and revision memory location +#define DEV_ADDR_UPPER 0x00FF +#define DEV_ADDR_TYPE 0x0000 +#define DEV_ADDR_REV 0x0002 +#define CFG_ADDR_UPPER 0x0000 +#define CFG_ADDR_1 0xABFC +#define CFG_ADDR_2 0xABFE + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-STK500v2/main.c b/firmware-STK500v2/main.c index 6ceaa8a4..e0ef2be1 100644 --- a/firmware-STK500v2/main.c +++ b/firmware-STK500v2/main.c @@ -4,12 +4,12 @@ * * Author: Guido Socher, Copyright: GPL * * Ported to PIC24/Bus Pirate by Ian Lesnet, http://dangerousprototypes.com, 2009 * Copyright: GPL -**********************************************/ -#include "base.h" +**********************************************/ +#include "base.h" #include "UART.h" #include "SPI.h" #include "command.h" -#include "redefines.h" /* Definitions to get the existing code working with the Bus Pirate */ +#include "redefines.h" /* Definitions to get the existing code working with the Bus Pirate */ /* Orig. source defines */ #define CONFIG_PARAM_BUILD_NUMBER_LOW 0 @@ -19,7 +19,7 @@ // update here our own default sw version: #define D_CONFIG_PARAM_SW_MAJOR 0x02 //was 2 #define D_CONFIG_PARAM_SW_MINOR 0x0a //was 4 - + #define CONFIG_PARAM_VTARGET 50 #define CONFIG_PARAM_VADJUST 25 #define CONFIG_PARAM_OSC_PSCALE 2 @@ -34,11 +34,11 @@ uint8_t CONFIG_PARAM_SW_MINOR; #define MSG_WAIT_SIZE2 3 #define MSG_WAIT_TOKEN 4 #define MSG_WAIT_MSG 5 -#define MSG_WAIT_CKSUM 6 - -//set custom configuration for PIC 24F -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need +#define MSG_WAIT_CKSUM 6 + +//set custom configuration for PIC 24F +_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need static unsigned char msg_buf[295]; static unsigned char param_reset_polarity=1; // 1=avr (reset active=low), 0=at89 (not supported by this avrusb500) @@ -85,8 +85,8 @@ void transmit_answer(unsigned char seqnum,unsigned int len) } uart_sendchar(cksum); } - -/* Act on incomming packet. Comand in msg_buf, seqnum needed for reply */ + +/* Act on incomming packet. Comand in msg_buf, seqnum needed for reply */ void programcmd(unsigned char seqnum) { unsigned char tmp,tmp2,addressing_is_word,ci,cj,cstatus; @@ -103,14 +103,14 @@ void programcmd(unsigned char seqnum) msg_buf[1] = STATUS_CMD_OK; // 0x00 msg_buf[2] = 8; //len //strcpy((char *)&(msg_buf[3]),"AVRISP_2"); // note: this copied also the null termination - msg_buf[3] = 'S'; - msg_buf[4] = 'T'; - msg_buf[5] = 'K'; - msg_buf[6] = '5'; - msg_buf[7] = '0'; - msg_buf[8] = '0'; - msg_buf[9] = '_'; - msg_buf[10] = '2'; + msg_buf[3] = 'S'; + msg_buf[4] = 'T'; + msg_buf[5] = 'K'; + msg_buf[6] = '5'; + msg_buf[7] = '0'; + msg_buf[8] = '0'; + msg_buf[9] = '_'; + msg_buf[10] = '2'; answerlen=11; break; @@ -670,9 +670,9 @@ void programcmd(unsigned char seqnum) transmit_answer(seqnum,answerlen); } - -//the main function waits for all the bytes in the STK500v2 packet, -//then sends for processing in the above functions + +//the main function waits for all the bytes in the STK500v2 packet, +//then sends for processing in the above functions int main(void) { unsigned char ch; @@ -705,7 +705,7 @@ int main(void) cksum = ch^0; continue; } - + if (msgparsestate==MSG_WAIT_SEQNUM){ seqnum=ch; @@ -713,21 +713,21 @@ int main(void) msgparsestate=MSG_WAIT_SIZE1; continue; } - + if (msgparsestate==MSG_WAIT_SIZE1){ cksum^=ch; msglen=ch<<8; msgparsestate=MSG_WAIT_SIZE2; continue; } - + if (msgparsestate==MSG_WAIT_SIZE2){ cksum^=ch; msglen|=ch; msgparsestate=MSG_WAIT_TOKEN; continue; } - + if (msgparsestate==MSG_WAIT_TOKEN){ cksum^=ch; if (ch==TOKEN){ @@ -738,7 +738,7 @@ int main(void) } continue; } - + if (msgparsestate==MSG_WAIT_MSG && i 0){ // message correct, process it diff --git a/firmware-STK500v2/redefines.h b/firmware-STK500v2/redefines.h index 478c1936..391e21ee 100644 --- a/firmware-STK500v2/redefines.h +++ b/firmware-STK500v2/redefines.h @@ -1,21 +1,21 @@ -/* Definitions to get the existing code working with the Bus Pirate */ -#define uart_sendchar(X) UART1TX(X) -#define uart_getchar(X) UART1RX() -#define uart_init() Initialize(); InitializeUART1() //do both - -#define delay_ms(X) bpDelayMS(X) -#define _delay_ms(X) bpDelayUS(X*1000) - +/* Definitions to get the existing code working with the Bus Pirate */ +#define uart_sendchar(X) UART1TX(X) +#define uart_getchar(X) UART1RX() +#define uart_init() Initialize(); InitializeUART1() //do both + +#define delay_ms(X) bpDelayMS(X) +#define _delay_ms(X) bpDelayUS(X*1000) + #define LED_INIT BP_LEDMODE_DIR=0 //LED to output #define LED_ON BP_LEDMODE=1 //LED ON -#define LED_OFF BP_LEDMODE=0 //LED OFF - -#define uint8_t unsigned char -#define uint16_t unsigned int - -//unused stuff -#define wd_kick() asm( "nop" ) -#define wdt_reset() asm( "nop" ) - - - +#define LED_OFF BP_LEDMODE=0 //LED OFF + +#define uint8_t unsigned char +#define uint16_t unsigned int + +//unused stuff +#define wd_kick() asm( "nop" ) +#define wdt_reset() asm( "nop" ) + + + diff --git a/firmware-STK500v2/spi.c b/firmware-STK500v2/spi.c index 476cde0c..6b32c410 100644 --- a/firmware-STK500v2/spi.c +++ b/firmware-STK500v2/spi.c @@ -3,27 +3,27 @@ * Serial Peripheral Interface * Author: Guido Socher, Copyright: GPL * Copyright: GPL -**********************************************/ -#include "base.h" -#include "redefines.h" /* Definitions to get the existing code working with the Bus Pirate */ - -//direction registers -#define SPIMOSI_TRIS BP_MOSI_DIR -#define SPICLK_TRIS BP_CLK_DIR -#define SPIMISO_TRIS BP_MISO_DIR -#define SPICS_TRIS BP_CS_DIR - -//pin control registers -#define SPIMOSI BP_MOSI -#define SPICLK BP_CLK -#define SPIMISO BP_MISO -#define SPICS BP_CS - -//open drain control registers for OUTPUT pins -#define SPIMOSI_ODC BP_MISO_ODC -#define SPICLK_ODC BP_CLK_ODC -#define SPICS_ODC BP_CS_ODC - +**********************************************/ +#include "base.h" +#include "redefines.h" /* Definitions to get the existing code working with the Bus Pirate */ + +//direction registers +#define SPIMOSI_TRIS BP_MOSI_DIR +#define SPICLK_TRIS BP_CLK_DIR +#define SPIMISO_TRIS BP_MISO_DIR +#define SPICS_TRIS BP_CS_DIR + +//pin control registers +#define SPIMOSI BP_MOSI +#define SPICLK BP_CLK +#define SPIMISO BP_MISO +#define SPICS BP_CS + +//open drain control registers for OUTPUT pins +#define SPIMOSI_ODC BP_MISO_ODC +#define SPICLK_ODC BP_CLK_ODC +#define SPICS_ODC BP_CS_ODC + // timing for software spi: #define F_CPU 3686400UL // 3.6864 MHz @@ -41,43 +41,43 @@ static unsigned char spi_in_sw=0; // 0 1 1 f/128 28KHz : sckdur =3 // //SPI2X=SPSR bit 0 -//SPR0 and SPR1 =SPCR bit 0 and 1 - +//SPR0 and SPR1 =SPCR bit 0 and 1 + unsigned char spi_set_sck_duration(unsigned char dur) -{ +{ SPI1STATbits.SPIEN = 0;//disable, just in case... spi_in_sw=0; if (dur >= 4){ // sofware spi very slow spi_in_sw=1; - sck_dur=12; + sck_dur=12; SPI1CON1 =0; //30khz }else if (dur >= 3){ // 28KHz - sck_dur=3; + sck_dur=3; SPI1CON1 = 0; //30khz }else if (dur >= 2){ // 57KHz - sck_dur=2; + sck_dur=2; SPI1CON1 =0b1000; //63khz }else if (dur == 1){ // 230KHz - sck_dur=1; + sck_dur=1; SPI1CON1 =0b11100; //250khz - }else if (dur == 0){ + }else if (dur == 0){ // 921KHz - sck_dur=0; + sck_dur=0; SPI1CON1 = 0b11101; //1000khz - } - /* CKE=1, CKP=0, SMP=0 */ - SPI1CON1bits.MSTEN=1; - //SPI1CON1bits.CKP=0; - SPI1CON1bits.CKE=1; - //SPI1CON1bits.SMP=0; - SPI1CON2 = 0; - SPI1STAT = 0; // clear SPI - SPI1STATbits.SPIEN = 1; + } + /* CKE=1, CKP=0, SMP=0 */ + SPI1CON1bits.MSTEN=1; + //SPI1CON1bits.CKP=0; + SPI1CON1bits.CKE=1; + //SPI1CON1bits.SMP=0; + SPI1CON2 = 0; + SPI1STAT = 0; // clear SPI + SPI1STATbits.SPIEN = 1; return(sck_dur); } @@ -89,13 +89,13 @@ unsigned char spi_get_sck_duration(void) void spi_sck_pulse(void) { - RPOR4bits.RP8R=0;// remove SPI CLK from pin - SPICLK=0; // SCK low - _delay_ms(0.10); - SPICLK=1; // SCK high - _delay_ms(0.10); - SPICLK=0; // SCK low - RPOR4bits.RP8R=SCK1OUT_IO; //reassign SPI CLK + RPOR4bits.RP8R=0;// remove SPI CLK from pin + SPICLK=0; // SCK low + _delay_ms(0.10); + SPICLK=1; // SCK high + _delay_ms(0.10); + SPICLK=0; // SCK low + RPOR4bits.RP8R=SCK1OUT_IO; //reassign SPI CLK Nop(); } @@ -120,39 +120,39 @@ void spi_init(void) // must be output for master to work. SPICS_TRIS=0; // reset = high = not active - // now output pins low in case somebody used it as output in his/her circuit - //MOSI, SCK is output/low - SPI1STATbits.SPIEN = 0;//disable, just in case... - - #ifdef OUTPUT_HIGH_IMPEDANCE - SPIMOSI_ODC=1; - SPICLK_ODC=1; - SPICS_ODC=1; - #else - SPIMOSI_ODC=0; - SPICLK_ODC=0; - SPICS_ODC=0; - #endif - // Inputs - RPINR20bits.SDI1R=7; //B7 MISO - // Outputs - RPOR4bits.RP9R=SDO1_IO; //B9 MOSI - RPOR4bits.RP8R=SCK1OUT_IO; //B8 CLK - //pps configures pins and this doesn't really matter.... - SPICLK_TRIS=0; //B8 sck output - SPIMISO_TRIS=1; //B7 SDI input - SPIMOSI_TRIS=0; //B9 SDO output - SPICLK=0; - SPIMISO=0; - SPIMOSI=0; - - delay_ms(20); // discharge MOSI/SCK + // now output pins low in case somebody used it as output in his/her circuit + //MOSI, SCK is output/low + SPI1STATbits.SPIEN = 0;//disable, just in case... + + #ifdef OUTPUT_HIGH_IMPEDANCE + SPIMOSI_ODC=1; + SPICLK_ODC=1; + SPICS_ODC=1; + #else + SPIMOSI_ODC=0; + SPICLK_ODC=0; + SPICS_ODC=0; + #endif + // Inputs + RPINR20bits.SDI1R=7; //B7 MISO + // Outputs + RPOR4bits.RP9R=SDO1_IO; //B9 MOSI + RPOR4bits.RP8R=SCK1OUT_IO; //B8 CLK + //pps configures pins and this doesn't really matter.... + SPICLK_TRIS=0; //B8 sck output + SPIMISO_TRIS=1; //B7 SDI input + SPIMOSI_TRIS=0; //B9 SDO output + SPICLK=0; + SPIMISO=0; + SPIMOSI=0; + + delay_ms(20); // discharge MOSI/SCK //if (spi_in_sw==0){ /* Enable SPI, Master, set clock rate fck/16 */ spi_set_sck_duration(sck_dur); // now SCK and MOSI are under control of SPI - //} + //} SPICS=0; // reset = low, stay active delay_ms(20); // stab delay @@ -169,11 +169,11 @@ unsigned char spi_mastertransmit(unsigned char data) SPI1BUF = data; /* Wait for transmission complete */ while(!IFS0bits.SPI1IF); - data=SPI1BUF; - IFS0bits.SPI1IF = 0; + data=SPI1BUF; + IFS0bits.SPI1IF = 0; return data; //} - // software spi, slow + // software spi, slow /* cbi(PORTB,PB5); // SCK low while(i){ @@ -193,7 +193,7 @@ unsigned char spi_mastertransmit(unsigned char data) cbi(PORTB,PB5); // SCK low i=i>>1; } - return(rval); + return(rval); */ } @@ -216,27 +216,27 @@ unsigned char spi_mastertransmit_32(unsigned long data) void spi_disable(void) { - - SPICS=1;// reset = high, off - - // SPI off - SPI1STATbits.SPIEN = 0; - RPINR20bits.SDI1R=0b11111; //B7 MISO - RPOR4bits.RP9R=0; //B9 MOSI - RPOR4bits.RP8R=0; //B8 CLK - //disable all open drain control register bits - SPIMOSI_ODC=0; - SPICLK_ODC=0; - SPICS_ODC=0; - - //reset pins - SPIMOSI_TRIS=1; //B9 SDO output - SPIMOSI=0; - SPICLK_TRIS=1; //B8 sck output - SPICLK=0; - SPIMISO_TRIS=1; //B7 SDI input - SPIMISO=0; - SPICS_TRIS=1;// reset pin as input, high impedance + + SPICS=1;// reset = high, off + + // SPI off + SPI1STATbits.SPIEN = 0; + RPINR20bits.SDI1R=0b11111; //B7 MISO + RPOR4bits.RP9R=0; //B9 MOSI + RPOR4bits.RP8R=0; //B8 CLK + //disable all open drain control register bits + SPIMOSI_ODC=0; + SPICLK_ODC=0; + SPICS_ODC=0; + + //reset pins + SPIMOSI_TRIS=1; //B9 SDO output + SPIMOSI=0; + SPICLK_TRIS=1; //B8 sck output + SPICLK=0; + SPIMISO_TRIS=1; //B7 SDI input + SPIMISO=0; + SPICS_TRIS=1;// reset pin as input, high impedance SPICS=0; } diff --git a/firmware-XSVFplayer/UART.c b/firmware-XSVFplayer/UART.c index 529ace4f..04419171 100644 --- a/firmware-XSVFplayer/UART.c +++ b/firmware-XSVFplayer/UART.c @@ -1,27 +1,27 @@ -//get a byte from UART -unsigned char UART1RX(void){ - - while(U1STAbits.URXDA == 0); - return U1RXREG; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -void InitializeUART1(void){ - //set pin configuration using peripheral pin select - BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; - U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; +//get a byte from UART +unsigned char UART1RX(void){ + + while(U1STAbits.URXDA == 0); + return U1RXREG; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +void InitializeUART1(void){ + //set pin configuration using peripheral pin select + BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; + BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; + U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; } \ No newline at end of file diff --git a/firmware-XSVFplayer/UART.h b/firmware-XSVFplayer/UART.h index 38898cd4..76c8b7af 100644 --- a/firmware-XSVFplayer/UART.h +++ b/firmware-XSVFplayer/UART.h @@ -1,8 +1,8 @@ -//get a byte from UART -unsigned char UART1RX(void); - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c); - +//get a byte from UART +unsigned char UART1RX(void); + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c); + void InitializeUART1(void); \ No newline at end of file diff --git a/firmware-XSVFplayer/base.c b/firmware-XSVFplayer/base.c index 4698ba4e..410b31a6 100644 --- a/firmware-XSVFplayer/base.c +++ b/firmware-XSVFplayer/base.c @@ -1,133 +1,133 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -#include "base.h" - -//get bus pirate ready after first start -//or clean up after any dirty modules -//modules MUST teardown any PPS functions on the CMD_CLEANUP command -void bpInit(void){ - //do this manually so there aren't problems with hardware - //that has I/O on different ports - BP_MOSI_DIR=1; - BP_CLK_DIR=1; - BP_MISO_DIR=1; - BP_CS_DIR=1; - BP_AUX_DIR=1; - BP_LEDMODE=0; //mode LED OFF - BP_LEDMODE_DIR=0; - #ifdef BUSPIRATEV1A - BP_PIN6_DIR=1; //AUX2... unused - BP_LEDPWR=1; //light power led - BP_LEDPWR_DIR=0; - #endif - - #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) - BP_PULLUP_OFF(); - #endif - - #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) - - BP_VREG_OFF();//disable the VREG - - //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! - AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V - AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V - AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup - AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT - //AD1PCFG&=(~0b11110);//analog pins - - //configure the ADC - AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal - // counter ends sampling and starts - // converting. - AD1CSSL = 0; - AD1CON3 = 0x1F02; // Sample time = 31Tad, - // Tad = 2 Tcy - AD1CON2 = 0; - #endif -} - - -//get a byte from UART -unsigned char UART1RX(void){ - - while(U1STAbits.URXDA == 0); - return U1RXREG; -} - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c){ - while(U1STAbits.UTXBF == 1); //if buffer is full, wait - U1TXREG = c; -} - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -void InitializeUART1(void){ - //set pin configuration using peripheral pin select - BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; - BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; - - U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... - U1MODE = 0; - U1MODEbits.BRGH = 1; - U1STA = 0; - U1MODEbits.UARTEN = 1; - U1STAbits.UTXEN = 1; - IFS0bits.U1RXIF = 0; -} - -//delay in MS -void bpDelayMS(const unsigned char delay){ - volatile unsigned int i; - volatile unsigned char c; - - for(c=0;c + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +#include "base.h" + +//get bus pirate ready after first start +//or clean up after any dirty modules +//modules MUST teardown any PPS functions on the CMD_CLEANUP command +void bpInit(void){ + //do this manually so there aren't problems with hardware + //that has I/O on different ports + BP_MOSI_DIR=1; + BP_CLK_DIR=1; + BP_MISO_DIR=1; + BP_CS_DIR=1; + BP_AUX_DIR=1; + BP_LEDMODE=0; //mode LED OFF + BP_LEDMODE_DIR=0; + #ifdef BUSPIRATEV1A + BP_PIN6_DIR=1; //AUX2... unused + BP_LEDPWR=1; //light power led + BP_LEDPWR_DIR=0; + #endif + + #if defined( BUSPIRATEV0A) || defined( BUSPIRATEV2) + BP_PULLUP_OFF(); + #endif + + #if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2) + + BP_VREG_OFF();//disable the VREG + + //setup voltage monitoring on ADC. Should be moved to hardwarevx.h! + AD1PCFGbits.PCFG9=0; //B15/AN9/ADC4/50V + AD1PCFGbits.PCFG10=0;//B14/AN10/ADC3/33V + AD1PCFGbits.PCFG11=0;//B13/AN11/ADC2/Vextpullup + AD1PCFGbits.PCFG12=0;//B12/AN12/ADC1/EXT + //AD1PCFG&=(~0b11110);//analog pins + + //configure the ADC + AD1CON1bits.SSRC = 0b111;// SSRC<3:0> = 111 implies internal + // counter ends sampling and starts + // converting. + AD1CSSL = 0; + AD1CON3 = 0x1F02; // Sample time = 31Tad, + // Tad = 2 Tcy + AD1CON2 = 0; + #endif +} + + +//get a byte from UART +unsigned char UART1RX(void){ + + while(U1STAbits.URXDA == 0); + return U1RXREG; +} + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c){ + while(U1STAbits.UTXBF == 1); //if buffer is full, wait + U1TXREG = c; +} + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +void InitializeUART1(void){ + //set pin configuration using peripheral pin select + BP_TERM_RX=BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; + BP_TERM_TX_RP=BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; + + U1BRG = 34;//13332=300, 1666=2400, 416=9600, 34@32mhz=115200.... + U1MODE = 0; + U1MODEbits.BRGH = 1; + U1STA = 0; + U1MODEbits.UARTEN = 1; + U1STAbits.UTXEN = 1; + IFS0bits.U1RXIF = 0; +} + +//delay in MS +void bpDelayMS(const unsigned char delay){ + volatile unsigned int i; + volatile unsigned char c; + + for(c=0;c - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -#ifndef BASE -#define BASE -#include - -//this sets the hardware version (v0a, or v1a) -//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ -//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ -//#define BUSPIRATEV2A -#define BUSPIRATEV25 //AKA Bus Pirate v2go - -#if defined(BUSPIRATEV0A) - #include "hardwarev0a.h" -#elif defined(BUSPIRATEV1A) - #include "hardwarev1a.h" -#elif defined(BUSPIRATEV2A) - #define BUSPIRATEV2 - #include "hardwarev2a.h" -#elif defined(BUSPIRATEV25) - #define BUSPIRATEV2 - #include "hardwarev25.h" -#else - #error "No hardware defined in base.h." -#endif - -//reset all peripherals before configuring a new bus protocol -void bpInit(void); - -//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed -void InitializeUART1(void); - -//get a byte from UART -unsigned char UART1RX(void); - -//add byte to buffer, pause if full -//uses PIC 4 byte UART FIFO buffer -void UART1TX(char c); - -//delays used by many libraries -void bpDelayMS(const unsigned char delay); -void bpDelayUS(const unsigned char delay); -#endif +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +#ifndef BASE +#define BASE +#include + +//this sets the hardware version (v0a, or v1a) +//#define BUSPIRATEV0A //http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ +//#define BUSPIRATEV1A //http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface/ +//#define BUSPIRATEV2A +#define BUSPIRATEV25 //AKA Bus Pirate v2go + +#if defined(BUSPIRATEV0A) + #include "hardwarev0a.h" +#elif defined(BUSPIRATEV1A) + #include "hardwarev1a.h" +#elif defined(BUSPIRATEV2A) + #define BUSPIRATEV2 + #include "hardwarev2a.h" +#elif defined(BUSPIRATEV25) + #define BUSPIRATEV2 + #include "hardwarev25.h" +#else + #error "No hardware defined in base.h." +#endif + +//reset all peripherals before configuring a new bus protocol +void bpInit(void); + +//Initialize the terminal UART for the speed currently set in bpConfig.termSpeed +void InitializeUART1(void); + +//get a byte from UART +unsigned char UART1RX(void); + +//add byte to buffer, pause if full +//uses PIC 4 byte UART FIFO buffer +void UART1TX(char c); + +//delays used by many libraries +void bpDelayMS(const unsigned char delay); +void bpDelayUS(const unsigned char delay); +#endif diff --git a/firmware-XSVFplayer/hardwarev0a.h b/firmware-XSVFplayer/hardwarev0a.h index 9c9c7a44..c380e3dc 100644 --- a/firmware-XSVFplayer/hardwarev0a.h +++ b/firmware-XSVFplayer/hardwarev0a.h @@ -1,87 +1,87 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//this hardware profile is for the first, through-hole Bus Pirate. -//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ - -#define BP_VERSION_STRING "Hack a Day Bus Pirate v0a" -#define BP_VERSION "v0a" - -//this configures the proper placement of the serial port for -//the PC side terminal interface -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 4 - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB5 -#define BP_PULLUP1 PORTBbits.RB11 -#define BP_PULLUP2 PORTBbits.RB10 -#define BP_LEDMODE PORTBbits.RB14 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB5 -#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA -#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL -#define BP_LEDMODE_DIR TRISBbits.TRISB14 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB5 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 5 -#define BP_AUX_RPOUT RPOR2bits.RP5R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for pullup resistors -#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 -#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//this hardware profile is for the first, through-hole Bus Pirate. +//http://hackaday.com/2008/11/19/how-to-the-bus-pirate-universal-serial-interface/ + +#define BP_VERSION_STRING "Hack a Day Bus Pirate v0a" +#define BP_VERSION "v0a" + +//this configures the proper placement of the serial port for +//the PC side terminal interface +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 4 + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB5 +#define BP_PULLUP1 PORTBbits.RB11 +#define BP_PULLUP2 PORTBbits.RB10 +#define BP_LEDMODE PORTBbits.RB14 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB5 +#define BP_PULLUP1_DIR TRISBbits.TRISB11 //SDA +#define BP_PULLUP2_DIR TRISBbits.TRISB10 //SCL +#define BP_LEDMODE_DIR TRISBbits.TRISB14 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB5 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 5 +#define BP_AUX_RPOUT RPOR2bits.RP5R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for pullup resistors +#define BP_PULLUP_ON() BP_PULLUP1=1;BP_PULLUP2=1;BP_PULLUP1_DIR=0;BP_PULLUP2_DIR=0 +#define BP_PULLUP_OFF() BP_PULLUP1_DIR=1;BP_PULLUP2_DIR=1 + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-XSVFplayer/hardwarev1a.h b/firmware-XSVFplayer/hardwarev1a.h index f7131bfd..c6e4e7d0 100644 --- a/firmware-XSVFplayer/hardwarev1a.h +++ b/firmware-XSVFplayer/hardwarev1a.h @@ -1,93 +1,93 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//this hardware profile is for the second, surface mount Bus Pirate. -//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface - -#define BP_VERSION_STRING "Hack a Day Bus Pirate v1a" -#define BP_VERSION "v1a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -//when we move to a USB PIC, we'll need to do that.... -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP2R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 3 - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_PIN6 PORTBbits.RB11 -#define BP_LEDMODE PORTBbits.RB4 -#define BP_LEDPWR PORTAbits.RA3 -#define BP_EN50V PORTAbits.RA4 -#define BP_EN33V PORTBbits.RB5 - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_PIN6_DIR TRISBbits.TRISB11 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_LEDPWR_DIR TRISAbits.TRISA3 -#define BP_EN50V_DIR TRISAbits.TRISA4 -#define BP_EN33V_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudo function to reset pins to hizstate -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 -#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//this hardware profile is for the second, surface mount Bus Pirate. +//http://hackaday.com/2009/01/22/how-to-bus-pirate-v1-improved-universal-serial-interface + +#define BP_VERSION_STRING "Hack a Day Bus Pirate v1a" +#define BP_VERSION "v1a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +//when we move to a USB PIC, we'll need to do that.... +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP2R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 3 + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_PIN6 PORTBbits.RB11 +#define BP_LEDMODE PORTBbits.RB4 +#define BP_LEDPWR PORTAbits.RA3 +#define BP_EN50V PORTAbits.RA4 +#define BP_EN33V PORTBbits.RB5 + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_PIN6_DIR TRISBbits.TRISB11 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_LEDPWR_DIR TRISAbits.TRISA3 +#define BP_EN50V_DIR TRISAbits.TRISA4 +#define BP_EN33V_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudo function to reset pins to hizstate +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_EN50V_DIR=0; BP_EN50V=1; BP_EN33V_DIR=0; BP_EN33V=1 +#define BP_VREG_OFF() BP_EN50V_DIR=0; BP_EN50V=0; BP_EN33V_DIR=0; BP_EN33V=0 + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-XSVFplayer/hardwarev25.h b/firmware-XSVFplayer/hardwarev25.h index 87d2c929..d0539489 100644 --- a/firmware-XSVFplayer/hardwarev25.h +++ b/firmware-XSVFplayer/hardwarev25.h @@ -1,92 +1,92 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//this profile is for the Bus Pirate v2go hardware -//http:// - -#define BP_VERSION_STRING "Hack a Day Bus Pirate v2go" -#define BP_VERSION "v2go" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR2bits.RP4R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 5 - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISAbits.TRISA1 -#define BP_VREGEN_DIR TRISAbits.TRISA0 -#define BP_PULLUP_DIR TRISBbits.TRISB11 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//this profile is for the Bus Pirate v2go hardware +//http:// + +#define BP_VERSION_STRING "Hack a Day Bus Pirate v2go" +#define BP_VERSION "v2go" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A and v1a share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR2bits.RP4R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 5 + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTAbits.RA1 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA0 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB11 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISAbits.TRISA1 +#define BP_VREGEN_DIR TRISAbits.TRISA0 +#define BP_PULLUP_DIR TRISBbits.TRISB11 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-XSVFplayer/hardwarev2a.h b/firmware-XSVFplayer/hardwarev2a.h index 5435df1f..15747bf9 100644 --- a/firmware-XSVFplayer/hardwarev2a.h +++ b/firmware-XSVFplayer/hardwarev2a.h @@ -1,92 +1,92 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//this hardware profile is for hardware V2a. -//http:// - -#define BP_VERSION_STRING "Hack a Day Bus Pirate v2a" -#define BP_VERSION "v2a" - -//this configures the placement of the serial port for -//the PC side terminal interface -//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater -#define BP_TERM_TX U1TX_IO; -#define BP_TERM_TX_RP RPOR1bits.RP3R -#define BP_TERM_RX RPINR18bits.U1RXR -#define BP_TERM_RX_RP 2 - -//these macros set the pin IO used in each module -#define BP_MOSI PORTBbits.RB9 -#define BP_CLK PORTBbits.RB8 -#define BP_MISO PORTBbits.RB7 -#define BP_CS PORTBbits.RB6 -#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 -#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a -#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a -#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a - -//these macros set the direction registers for each IO pin -#define BP_MOSI_DIR TRISBbits.TRISB9 -#define BP_CLK_DIR TRISBbits.TRISB8 -#define BP_MISO_DIR TRISBbits.TRISB7 -#define BP_CS_DIR TRISBbits.TRISB6 -#define BP_AUX_DIR TRISBbits.TRISB10 -#define BP_LEDMODE_DIR TRISBbits.TRISB4 -#define BP_VREGEN_DIR TRISAbits.TRISA4 -#define BP_PULLUP_DIR TRISBbits.TRISB5 - -//Open drain/high impedance pin setup -#define BP_MOSI_ODC ODCBbits.ODB9 -#define BP_CLK_ODC ODCBbits.ODB8 -#define BP_MISO_ODC ODCBbits.ODB7 -#define BP_CS_ODC ODCBbits.ODB6 -#define BP_AUX_ODC ODCBbits.ODB10 - -//define the PPS input/output for the AUX pin -#define BP_AUX_RPIN 10 -#define BP_AUX_RPOUT RPOR5bits.RP10R -#define BP_CS_RPIN 6 -#define BP_CS_RPOUT RPOR5bits.RP6R - -//pseudofunctions for pullup resistors -//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... -#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 -#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 - -//pseudofunctions for voltage regulator switch -#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 -#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 - -// Peripheral Pin Select Outputs -#define NULL_IO 0 -#define C1OUT_IO 1 -#define C2OUT_IO 2 -#define U1TX_IO 3 -#define U1RTS_IO 4 -#define U2TX_IO 5 -#define U2RTS_IO 6 -#define SDO1_IO 7 -#define SCK1OUT_IO 8 -#define SS1OUT_IO 9 -#define SDO2_IO 10 -#define SCK2OUT_IO 11 -#define SS2OUT_IO 12 -#define OC1_IO 18 -#define OC2_IO 19 -#define OC3_IO 20 -#define OC4_IO 21 -#define OC5_IO 22 +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//this hardware profile is for hardware V2a. +//http:// + +#define BP_VERSION_STRING "Hack a Day Bus Pirate v2a" +#define BP_VERSION "v2a" + +//this configures the placement of the serial port for +//the PC side terminal interface +//the other modules should be similarly abstracted, but since v0A,v1a,V2 share pins, it doesn't mater +#define BP_TERM_TX U1TX_IO; +#define BP_TERM_TX_RP RPOR1bits.RP3R +#define BP_TERM_RX RPINR18bits.U1RXR +#define BP_TERM_RX_RP 2 + +//these macros set the pin IO used in each module +#define BP_MOSI PORTBbits.RB9 +#define BP_CLK PORTBbits.RB8 +#define BP_MISO PORTBbits.RB7 +#define BP_CS PORTBbits.RB6 +#define BP_AUX PORTBbits.RB10 //moved pin5 (AUX) to RB10 +#define BP_LEDMODE PORTBbits.RB4 //single MODE led on v2a +#define BP_VREGEN PORTAbits.RA4 //single vreg enable pin on v2a +#define BP_PULLUP PORTBbits.RB5 //Pull-up pin on V2a + +//these macros set the direction registers for each IO pin +#define BP_MOSI_DIR TRISBbits.TRISB9 +#define BP_CLK_DIR TRISBbits.TRISB8 +#define BP_MISO_DIR TRISBbits.TRISB7 +#define BP_CS_DIR TRISBbits.TRISB6 +#define BP_AUX_DIR TRISBbits.TRISB10 +#define BP_LEDMODE_DIR TRISBbits.TRISB4 +#define BP_VREGEN_DIR TRISAbits.TRISA4 +#define BP_PULLUP_DIR TRISBbits.TRISB5 + +//Open drain/high impedance pin setup +#define BP_MOSI_ODC ODCBbits.ODB9 +#define BP_CLK_ODC ODCBbits.ODB8 +#define BP_MISO_ODC ODCBbits.ODB7 +#define BP_CS_ODC ODCBbits.ODB6 +#define BP_AUX_ODC ODCBbits.ODB10 + +//define the PPS input/output for the AUX pin +#define BP_AUX_RPIN 10 +#define BP_AUX_RPOUT RPOR5bits.RP10R +#define BP_CS_RPIN 6 +#define BP_CS_RPOUT RPOR5bits.RP6R + +//pseudofunctions for pullup resistors +//for V2a we need B5/pullup to be output and gnd to turn OFF the pullups... +#define BP_PULLUP_ON() BP_PULLUP_DIR=1;BP_PULLUP=0 +#define BP_PULLUP_OFF() BP_PULLUP=0;BP_PULLUP_DIR=0 + +//pseudofunctions for voltage regulator switch +#define BP_VREG_ON() BP_VREGEN_DIR=0; BP_VREGEN=1 +#define BP_VREG_OFF() BP_VREGEN_DIR=0; BP_VREGEN=0 + +// Peripheral Pin Select Outputs +#define NULL_IO 0 +#define C1OUT_IO 1 +#define C2OUT_IO 2 +#define U1TX_IO 3 +#define U1RTS_IO 4 +#define U2TX_IO 5 +#define U2RTS_IO 6 +#define SDO1_IO 7 +#define SCK1OUT_IO 8 +#define SS1OUT_IO 9 +#define SDO2_IO 10 +#define SCK2OUT_IO 11 +#define SS2OUT_IO 12 +#define OC1_IO 18 +#define OC2_IO 19 +#define OC3_IO 20 +#define OC4_IO 21 +#define OC5_IO 22 diff --git a/firmware-XSVFplayer/jtag.c b/firmware-XSVFplayer/jtag.c index 20b9df5f..49a75a0c 100644 --- a/firmware-XSVFplayer/jtag.c +++ b/firmware-XSVFplayer/jtag.c @@ -1,130 +1,130 @@ -/* - * This file is part of the Bus Pirate project (buspirate.com). - * - * Originally written by hackaday.com - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -#include "base.h" -#include "jtag.h" -#include "jtag/micro.h" -#include "jtag/ports.h" - -#define JTAGDATASETTLE 20 -#define JTAGCLOCK 100 - -#define RESET 0 -#define IDLE 1 -#define SHIFTIR 2 -#define SHIFTDR 3 - -//private functions -void jtagSetup(void); - -//these are JTAG state machine related -void jtagSetState(unsigned char c); -void jtagLeaveState(void); -void jtagReset(void); -void jtagCleanPendingBit(void); -//high level byte operations -unsigned char jtagWriteByte(unsigned char c); -unsigned char jtagReadByte(void); -//bit level functions -unsigned char jtagWriteBit(unsigned char c); -unsigned char jtagReadBit(void); -unsigned char jtagReadDataState(void); -//bus level control -void jtagDataLow(void); -void jtagDataHigh(void); -void jtagClockLow(void); -void jtagClockHigh(void); -void jtagClockTicks(unsigned char c); -void jtagTMSHigh(void); -void jtagTMSLow(void); - -static unsigned char jtagState=0, jtagBitPending=0, jtagDelayedBit=0;//static configuration variables for highZ and LSB - -struct _JTAG{ - unsigned char HiZ:1; -} jtagSettings; - - -void jtag(void){ - static unsigned char c, cmd; - static unsigned int i; - - jtagSetup(); - jtagSettings.HiZ=1; - - while(1){ - cmd=UART1RX(); - switch(cmd){ - //bpWline("JTAG READY"); - case 1://jtag reset - jtagReset();//reset - //bpWline("JTAGSM: RESET"); - jtagLeaveState();//move chain to idle (gives own message) - break; - case 2://read ID, chain length, # devices - //bpWline("JTAG INIT CHAIN"); - jtagReset();//reset - //bpWline("JTAGSM: RESET"); - //data high - jtagDataHigh(); - //how many devices? - //[0xffx255]{while not 1} - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTIR); //shift IR to enter data - jtagClockTicks(0xff); - jtagClockTicks(0xff); - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTDR); - i=0; - while(jtagReadBit()==0){ - i++; - if(i<250)break;//250 device timout/limit... - } - jtagLeaveState(); //clean up from previous state - //reset - jtagReset(); - //bpWline("JTAGSM: RESET"); - - //read ID#s (32 bits * devices) {r: (4*devices)} - jtagLeaveState(); //clean up from previous state - jtagSetState(SHIFTDR); - //bpWline("JTAG CHAIN REPORT:"); - UART1TX(i*4); //how many bytes are we returning - for(c=0;c + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +#include "base.h" +#include "jtag.h" +#include "jtag/micro.h" +#include "jtag/ports.h" + +#define JTAGDATASETTLE 20 +#define JTAGCLOCK 100 + +#define RESET 0 +#define IDLE 1 +#define SHIFTIR 2 +#define SHIFTDR 3 + +//private functions +void jtagSetup(void); + +//these are JTAG state machine related +void jtagSetState(unsigned char c); +void jtagLeaveState(void); +void jtagReset(void); +void jtagCleanPendingBit(void); +//high level byte operations +unsigned char jtagWriteByte(unsigned char c); +unsigned char jtagReadByte(void); +//bit level functions +unsigned char jtagWriteBit(unsigned char c); +unsigned char jtagReadBit(void); +unsigned char jtagReadDataState(void); +//bus level control +void jtagDataLow(void); +void jtagDataHigh(void); +void jtagClockLow(void); +void jtagClockHigh(void); +void jtagClockTicks(unsigned char c); +void jtagTMSHigh(void); +void jtagTMSLow(void); + +static unsigned char jtagState=0, jtagBitPending=0, jtagDelayedBit=0;//static configuration variables for highZ and LSB + +struct _JTAG{ + unsigned char HiZ:1; +} jtagSettings; + + +void jtag(void){ + static unsigned char c, cmd; + static unsigned int i; + + jtagSetup(); + jtagSettings.HiZ=1; + + while(1){ + cmd=UART1RX(); + switch(cmd){ + //bpWline("JTAG READY"); + case 1://jtag reset + jtagReset();//reset + //bpWline("JTAGSM: RESET"); + jtagLeaveState();//move chain to idle (gives own message) + break; + case 2://read ID, chain length, # devices + //bpWline("JTAG INIT CHAIN"); + jtagReset();//reset + //bpWline("JTAGSM: RESET"); + //data high + jtagDataHigh(); + //how many devices? + //[0xffx255]{while not 1} + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTIR); //shift IR to enter data + jtagClockTicks(0xff); + jtagClockTicks(0xff); + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTDR); + i=0; + while(jtagReadBit()==0){ + i++; + if(i<250)break;//250 device timout/limit... + } + jtagLeaveState(); //clean up from previous state + //reset + jtagReset(); + //bpWline("JTAGSM: RESET"); + + //read ID#s (32 bits * devices) {r: (4*devices)} + jtagLeaveState(); //clean up from previous state + jtagSetState(SHIFTDR); + //bpWline("JTAG CHAIN REPORT:"); + UART1TX(i*4); //how many bytes are we returning + for(c=0;c lenVal MAX_LEN buffer size*/ /* Insert new errors here */ - #define XSVF_ERROR_LAST 7 - i=xsvfExecute(); - UART1TX(i); - break; - default: - break;//bpWmessage(MSG_ERROR_MACRO); - }//switch - }//while -}//function - - -//exits an existing JTAG state, moves to IDLE -void jtagLeaveState(void){ - //move to IDLE -// bpWstring("JTAGSM: "); - switch(jtagState){ - case IDLE://already in idle - //bpWline("ALREADY IDLE"); - break; - case RESET://move to idle 0 - jtagTMSLow(); - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("RESET->IDLE"); - break; - case SHIFTIR://clean up pending writes... - if(jtagBitPending==1){ - //set proper bit direction - if(jtagDelayedBit==1){ - jtagDataHigh(); - }else{ - jtagDataLow(); - } - jtagBitPending=0;//clear pending - //bpWstring("(WROTE DELAYED BIT) "); - } - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("IR->IDLE"); - break; - case SHIFTDR://both same path 110 - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(1); - jtagState=IDLE; - //bpWline("DR->IDLE"); - break; - default: - //couldn't change state, error, try resetting state machine... - //bpWline("UNKNOWN STATE"); - break; - } -} - -void jtagReset(void){ - jtagTMSHigh(); - jtagClockTicks(10);//one extra if clk starts high - jtagTMSLow();//always return to low for writes - jtagState=RESET; -} - -//moves to specified state from IDLE (reset from anywhere) -void jtagSetState(unsigned char c){ - //if(jtagState!=IDLE) bpWline("JTAGSM: WARNING OUT OF SYNC"); - //bpWstring("JTAGSM: "); - - //move to desired state - switch(c){ - case IDLE://alread idle - //bpWline("ALREADY IDLE"); - break; - case RESET://always return to reset with 11111 - jtagReset(); - //bpWline("RESET"); - break; - case SHIFTDR://100 from IDLE - jtagTMSHigh(); - jtagClockTicks(1); - jtagTMSLow();//always return to low for writes - jtagClockTicks(2); - jtagState=SHIFTDR; - //bpWline("IDLE->Data Register"); - break; - case SHIFTIR: //1100 from IDLE - jtagTMSHigh(); - jtagClockTicks(2); - jtagTMSLow();//always return to low for writes - jtagClockTicks(2); - jtagState=SHIFTIR; - //bpWline("IDLE->Instruction Register (DELAYED ONE BIT FOR TMS)"); - break; - default: - //unknown state, try resetting the JTAG state machine - //bpWline("UNKNOWN STATE, TRY A RESET MACRO (1)"); - break; - } -} - -void jtagSetup(void){ - JTAGTD0_TRIS=1;//input from chain - JTAGTCK_TRIS=0; - JTAGTDI_TRIS=0; - JTAGTMS_TRIS=0; //B6 cs output is state machine control - //writes to the PORTs write to the LATCH - JTAGTDO=0; - JTAGTCK=0; - JTAGTDI=0; - JTAGTMS=0; -} - -//this is a new write routine, untested. See old below... -unsigned char jtagWriteByte(unsigned char c){ - unsigned char i,j,a,l; - - jtagClockLow();//begin with clock low... - - //clean up any pending bits - if(jtagBitPending==1){ - jtagWriteBit(jtagDelayedBit); - jtagBitPending=0;//clear pending - //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); - } - - //if(modeConfig.lsbEN==1) l=0x01; else l=0b10000000; - l=0x01; - - for(i=0;i<8;i++){ - if( (c & l)== 0) jtagDataLow(); else jtagDataHigh();//setup the data pin - - jtagClockHigh();//set clock high - - j=JTAGTDO; - //if(modeConfig.lsbEN==1){ - c=c>>1; //-- Shift next bit into position - a=a>>1; - if(j==1)a+=0b10000000; - //}else{ - // c=c<<1; //-- Shift next bit into position - // a=a<<1; - // if(j==1)a++; - //} - - jtagClockLow();//set clock low - - //catch bit seven and delay the write until we do the next byte or exit state with TMS=1.... - if(jtagState==SHIFTIR && i==6){ - jtagBitPending=1; - if((c & l)==0) jtagDelayedBit=0; else jtagDelayedBit=1; - return a;//meaningless....rather, 1 bit short - } - } - return a; -} - -unsigned char jtagReadByte(void){ - unsigned char i,j,a; - - jtagClockLow();//begin with clock low... - - if(jtagBitPending==1){ - jtagWriteBit(jtagDelayedBit); - jtagBitPending=0;//clear pending - //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); - } - - for(i=0;i<8;i++){ - jtagClockHigh();//set clock high - j=JTAGTDO; - //if(modeConfig.lsbEN==0){ - // a=a<<1; - // if(j)a++; - //}else{ - a=a>>1; - if(j)a+=0b10000000; - //} - jtagClockLow();//set clock low - } - - return a; -} - -unsigned char jtagWriteBit(unsigned char c){ - unsigned char i; - - if(c==0){ - jtagDataLow(); - }else{ - jtagDataHigh(); - } - jtagClockHigh();//set clock high - i=JTAGTDO; - jtagClockLow();//set clock low - return i; -} - -unsigned char jtagReadBit(void){ - unsigned char i; - - jtagClockHigh();//set clock high - i=JTAGTDO; - jtagClockLow();//set clock low - return i; -} - -unsigned char jtagReadDataState(void){ - return JTAGTDO; -} - -void jtagDataHigh(void){ - JTAGTDI_TRIS=(~jtagSettings.HiZ);//set output - JTAGTDI=jtagSettings.HiZ;//data - bpDelayUS(JTAGDATASETTLE);//delay -} - -void jtagDataLow(void){ - JTAGTDI=0; //data low - JTAGTDI_TRIS=0;//set to output for HIGHZ low - bpDelayUS(JTAGDATASETTLE);//delay -} - -void jtagClockTicks(unsigned char c){ - unsigned char i; - - for(i=0;iIDLE"); + break; + case SHIFTIR://clean up pending writes... + if(jtagBitPending==1){ + //set proper bit direction + if(jtagDelayedBit==1){ + jtagDataHigh(); + }else{ + jtagDataLow(); + } + jtagBitPending=0;//clear pending + //bpWstring("(WROTE DELAYED BIT) "); + } + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(1); + jtagState=IDLE; + //bpWline("IR->IDLE"); + break; + case SHIFTDR://both same path 110 + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(1); + jtagState=IDLE; + //bpWline("DR->IDLE"); + break; + default: + //couldn't change state, error, try resetting state machine... + //bpWline("UNKNOWN STATE"); + break; + } +} + +void jtagReset(void){ + jtagTMSHigh(); + jtagClockTicks(10);//one extra if clk starts high + jtagTMSLow();//always return to low for writes + jtagState=RESET; +} + +//moves to specified state from IDLE (reset from anywhere) +void jtagSetState(unsigned char c){ + //if(jtagState!=IDLE) bpWline("JTAGSM: WARNING OUT OF SYNC"); + //bpWstring("JTAGSM: "); + + //move to desired state + switch(c){ + case IDLE://alread idle + //bpWline("ALREADY IDLE"); + break; + case RESET://always return to reset with 11111 + jtagReset(); + //bpWline("RESET"); + break; + case SHIFTDR://100 from IDLE + jtagTMSHigh(); + jtagClockTicks(1); + jtagTMSLow();//always return to low for writes + jtagClockTicks(2); + jtagState=SHIFTDR; + //bpWline("IDLE->Data Register"); + break; + case SHIFTIR: //1100 from IDLE + jtagTMSHigh(); + jtagClockTicks(2); + jtagTMSLow();//always return to low for writes + jtagClockTicks(2); + jtagState=SHIFTIR; + //bpWline("IDLE->Instruction Register (DELAYED ONE BIT FOR TMS)"); + break; + default: + //unknown state, try resetting the JTAG state machine + //bpWline("UNKNOWN STATE, TRY A RESET MACRO (1)"); + break; + } +} + +void jtagSetup(void){ + JTAGTD0_TRIS=1;//input from chain + JTAGTCK_TRIS=0; + JTAGTDI_TRIS=0; + JTAGTMS_TRIS=0; //B6 cs output is state machine control + //writes to the PORTs write to the LATCH + JTAGTDO=0; + JTAGTCK=0; + JTAGTDI=0; + JTAGTMS=0; +} + +//this is a new write routine, untested. See old below... +unsigned char jtagWriteByte(unsigned char c){ + unsigned char i,j,a,l; + + jtagClockLow();//begin with clock low... + + //clean up any pending bits + if(jtagBitPending==1){ + jtagWriteBit(jtagDelayedBit); + jtagBitPending=0;//clear pending + //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); + } + + //if(modeConfig.lsbEN==1) l=0x01; else l=0b10000000; + l=0x01; + + for(i=0;i<8;i++){ + if( (c & l)== 0) jtagDataLow(); else jtagDataHigh();//setup the data pin + + jtagClockHigh();//set clock high + + j=JTAGTDO; + //if(modeConfig.lsbEN==1){ + c=c>>1; //-- Shift next bit into position + a=a>>1; + if(j==1)a+=0b10000000; + //}else{ + // c=c<<1; //-- Shift next bit into position + // a=a<<1; + // if(j==1)a++; + //} + + jtagClockLow();//set clock low + + //catch bit seven and delay the write until we do the next byte or exit state with TMS=1.... + if(jtagState==SHIFTIR && i==6){ + jtagBitPending=1; + if((c & l)==0) jtagDelayedBit=0; else jtagDelayedBit=1; + return a;//meaningless....rather, 1 bit short + } + } + return a; +} + +unsigned char jtagReadByte(void){ + unsigned char i,j,a; + + jtagClockLow();//begin with clock low... + + if(jtagBitPending==1){ + jtagWriteBit(jtagDelayedBit); + jtagBitPending=0;//clear pending + //bpWstring("NOTE: WROTE DELAYED BIT\x0D\x0A"); + } + + for(i=0;i<8;i++){ + jtagClockHigh();//set clock high + j=JTAGTDO; + //if(modeConfig.lsbEN==0){ + // a=a<<1; + // if(j)a++; + //}else{ + a=a>>1; + if(j)a+=0b10000000; + //} + jtagClockLow();//set clock low + } + + return a; +} + +unsigned char jtagWriteBit(unsigned char c){ + unsigned char i; + + if(c==0){ + jtagDataLow(); + }else{ + jtagDataHigh(); + } + jtagClockHigh();//set clock high + i=JTAGTDO; + jtagClockLow();//set clock low + return i; +} + +unsigned char jtagReadBit(void){ + unsigned char i; + + jtagClockHigh();//set clock high + i=JTAGTDO; + jtagClockLow();//set clock low + return i; +} + +unsigned char jtagReadDataState(void){ + return JTAGTDO; +} + +void jtagDataHigh(void){ + JTAGTDI_TRIS=(~jtagSettings.HiZ);//set output + JTAGTDI=jtagSettings.HiZ;//data + bpDelayUS(JTAGDATASETTLE);//delay +} + +void jtagDataLow(void){ + JTAGTDI=0; //data low + JTAGTDI_TRIS=0;//set to output for HIGHZ low + bpDelayUS(JTAGDATASETTLE);//delay +} + +void jtagClockTicks(unsigned char c){ + unsigned char i; + + for(i=0;i - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ - -//we need the pin definitions here for use in port.c of the XSVF programmer -#define JTAGTDI_TRIS BP_MOSI_DIR -#define JTAGTCK_TRIS BP_CLK_DIR -#define JTAGTD0_TRIS BP_MISO_DIR -#define JTAGTMS_TRIS BP_CS_DIR - -#define JTAGTDI BP_MOSI -#define JTAGTCK BP_CLK -#define JTAGTDO BP_MISO -#define JTAGTMS BP_CS - -void jtag(void); - - - - +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ + +//we need the pin definitions here for use in port.c of the XSVF programmer +#define JTAGTDI_TRIS BP_MOSI_DIR +#define JTAGTCK_TRIS BP_CLK_DIR +#define JTAGTD0_TRIS BP_MISO_DIR +#define JTAGTMS_TRIS BP_CS_DIR + +#define JTAGTDI BP_MOSI +#define JTAGTCK BP_CLK +#define JTAGTDO BP_MISO +#define JTAGTMS BP_CS + +void jtag(void); + + + + diff --git a/firmware-XSVFplayer/jtag/micro.c b/firmware-XSVFplayer/jtag/micro.c index ccdb0bdf..da310b19 100644 --- a/firmware-XSVFplayer/jtag/micro.c +++ b/firmware-XSVFplayer/jtag/micro.c @@ -1617,14 +1617,14 @@ int xsvfExecute() { SXsvfInfo xsvfInfo; - xsvfInitialize( &xsvfInfo ); + xsvfInitialize( &xsvfInfo ); while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) ) { xsvfRun( &xsvfInfo ); } - xsvfCleanup( &xsvfInfo ); + xsvfCleanup( &xsvfInfo ); return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) ); } diff --git a/firmware-XSVFplayer/jtag/ports.c b/firmware-XSVFplayer/jtag/ports.c index f463f72d..e15c56bc 100644 --- a/firmware-XSVFplayer/jtag/ports.c +++ b/firmware-XSVFplayer/jtag/ports.c @@ -1,57 +1,57 @@ -//Our open source (CC-0) version of the ports.c from XAPP058 -// CC-0, public domain -// -// You'll also need lenval.c and micro.c from XAPP058 -// IT doesn't have a stated license so we're hesitant to include it in SVN -// Get it here: http://www.xilinx.com/support/documentation/application_notes/xapp058.zip -#include "../base.h" -#include "ports.h" -#include "../jtag.h" +//Our open source (CC-0) version of the ports.c from XAPP058 +// CC-0, public domain +// +// You'll also need lenval.c and micro.c from XAPP058 +// IT doesn't have a stated license so we're hesitant to include it in SVN +// Get it here: http://www.xilinx.com/support/documentation/application_notes/xapp058.zip +#include "../base.h" +#include "ports.h" +#include "../jtag.h" #define MAX_BUFFER 4096 -static unsigned char buf[MAX_BUFFER]; //buffer to hold incoming bytes +static unsigned char buf[MAX_BUFFER]; //buffer to hold incoming bytes static unsigned int bufBytes=0, bufPointer=0; - -void xsvf_setup(void){ - bufBytes=0; - JTAGTDI_TRIS=0; - JTAGTCK_TRIS=0; - JTAGTD0_TRIS=1; - JTAGTMS_TRIS=0; -} -void setPort(short p,short val){ +void xsvf_setup(void){ + bufBytes=0; + JTAGTDI_TRIS=0; + JTAGTCK_TRIS=0; + JTAGTD0_TRIS=1; + JTAGTMS_TRIS=0; +} + +void setPort(short p,short val){ if (p==TMS) {JTAGTMS = (unsigned char) val;}// bpDelayUS(10);} if (p==TDI) {JTAGTDI = (unsigned char) val;}// bpDelayUS(10);} if (p==TCK) {JTAGTCK = (unsigned char) val;}// bpDelayUS(50);} } - -void readByte(unsigned char *data){ - unsigned int i; - unsigned char bh, bl; - - if(bufBytes==0){ - UART1TX(0xff); - while(U1STAbits.URXDA == 0); - bh=UART1RX();//get up to 255 bytes each time - bl=UART1RX();//get up to 255 bytes each time - bufBytes=(bh<<8)|bl; - for(i=0; i - * - * To the extent possible under law, hackaday.com has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - */ -//http://dangerousprototypes.com/docs/Bus_Pirate_JTAG_XSVF_player -#include "base.h" -#include "jtag.h" - -_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz -_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need - -#pragma code -//this loop services user input and passes it to be processed on -int main(void){ - - CLKDIVbits.RCDIV0=0; //clock divider to 0 - AD1PCFG = 0xFFFF; // Default all pins to digital - OSCCONbits.SOSCEN=0; - - bpInit(); - - InitializeUART1(); //init the PC side serial port (needs bpInit() first for speed setting) - - BP_VREG_ON(); - - jtag(); - - while(1); //while -} - - - - +/* + * This file is part of the Bus Pirate project (buspirate.com). + * + * Originally written by hackaday.com + * + * To the extent possible under law, hackaday.com has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + */ +//http://dangerousprototypes.com/docs/Bus_Pirate_JTAG_XSVF_player +#include "base.h" +#include "jtag.h" + +_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz +_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need + +#pragma code +//this loop services user input and passes it to be processed on +int main(void){ + + CLKDIVbits.RCDIV0=0; //clock divider to 0 + AD1PCFG = 0xFFFF; // Default all pins to digital + OSCCONbits.SOSCEN=0; + + bpInit(); + + InitializeUART1(); //init the PC side serial port (needs bpInit() first for speed setting) + + BP_VREG_ON(); + + jtag(); + + while(1); //while +} + + + + diff --git a/package/BPv4-firmware/pirate-loader-v4-source/pirate-loader.c b/package/BPv4-firmware/pirate-loader-v4-source/pirate-loader.c index 2de44b0a..c95ca8ae 100644 --- a/package/BPv4-firmware/pirate-loader-v4-source/pirate-loader.c +++ b/package/BPv4-firmware/pirate-loader-v4-source/pirate-loader.c @@ -1,1256 +1,1256 @@ -/* - - Pirate-Loader for Bootloader v4 - - Version : 1.0.2 - - Changelog: - +2010-06-28 - Made HEX parser case-insensative - - + 2010-02-04 - Changed polling interval to 10ms on Windows select wrapper, suggested by Michal (robots) - - + 2010-02-04 - Added sleep(0) between write instructions, patch submitted by kbulgrien - - + 2010-01-22 - Added loader version number to the console output and source code - - + 2010-01-19 - Fixed BigEndian incompatibility - - Added programming simulate switch ( --simulate ) for data verification - - + 2010-01-18 - Initial release - - - Building: - - UNIX family systems: - - gcc pirate-loader.c -o pirate-loader - - WINDOWS: - - cl pirate-loader.c /DWIN32=1 - - - Usage: - - Run ./pirate-loader --help for more information on usage and possible switches - - */ - -#include -#include -#include -#include -#include - -#define PIRATE_LOADER_VERSION "1.0.2" - -#define STR_EXPAND(tok) #tok -#define OS_NAME(tok) STR_EXPAND(tok) - - -#ifdef WIN32 -#include -#include - -#define O_NOCTTY 0 -#define O_NDELAY 0 -#define B115200 921600 - -#define OS WINDOWS - -int write(int fd, const void* buf, int len) -{ - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bwritten = 0; - - - res = WriteFile(hCom, buf, len, &bwritten, NULL); - - if( res == FALSE ) - { - return -1; - } - else - { - return bwritten; - } -} - -int read(int fd, void* buf, int len) -{ - HANDLE hCom = (HANDLE)fd; - int res = 0; - unsigned long bread = 0; - - res = ReadFile(hCom, buf, len, &bread, NULL); - - if( res == FALSE ) - { - return -1; - } - else - { - return bread; - } -} - -int close(int fd) -{ - HANDLE hCom = (HANDLE)fd; - - CloseHandle(hCom); - return 0; -} - -int open(const char* path, unsigned long flags) -{ - static char full_path[32] = {0}; - - HANDLE hCom = NULL; - - if( path[0] != '\\' ) - { - _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); - path = full_path; - } - - hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if( !hCom || hCom == INVALID_HANDLE_VALUE ) - { - return -1; - } - else - { - return (int)hCom; - } -} - -int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) -{ - time_t maxtc = time(0) + (timeout->tv_sec); - COMSTAT cs = {0}; -// JTR not used so get rig of warning unsigned long dwErrors = 0; - - if( readfds->fd_count != 1 ) - { - return -1; - } - - while( time(0) <= maxtc ) - { - //only one file supported - if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ) - { - return -1; - } - - if( cs.cbInQue > 0 ) - { - return 1; - } - - Sleep(10); - } - return 0; -} - -unsigned int sleep(unsigned int sec) -{ - Sleep(sec * 1000); - - return 0; -} - -#else -#include -#include -#include -#include -#include -#endif - -/* macro definitions */ - -/* type definitions */ - -typedef unsigned char uint8; -typedef unsigned short uint16; -typedef unsigned long uint32; - -#if !defined OS -#define OS UNKNOWN -#endif - -#define BOOTLOADER_HELLO_STR "\xC1" -#define BOOTLOADER_OK 0x4B -#define BOOTLOADER_PROT 'P' -#define PIC_WORD_SIZE (3) -#define PIC_NUM_ROWS_IN_PAGE 8 -#define PIC_NUM_WORDS_IN_ROW 64 -#define PIC_ROW_SIZE (PIC_NUM_WORDS_IN_ROW * PIC_WORD_SIZE) -#define PIC_PAGE_SIZE (PIC_NUM_ROWS_IN_PAGE * PIC_ROW_SIZE) -#define PIC_ROW_ADDR(p,r) (((p) * PIC_PAGE_SIZE) + ((r) * PIC_ROW_SIZE)) -#define PIC_WORD_ADDR(p,r,w) (PIC_ROW_ADDR(p,r) + ((w) * PIC_WORD_SIZE)) -#define PIC_PAGE_ADDR(p) (PIC_PAGE_SIZE * (p)) - -#define PAYLOAD_OFFSET 5 -#define HEADER_LENGTH PAYLOAD_OFFSET -#define LENGTH_OFFSET 4 -#define COMMAND_OFFSET 3 -#define IS_24FJ 1 -#define PIC_NUM_PAGES 512 - -//#define flashsize 0x2AC00 //was 0xac00 -//#define PIC_NUM_PAGES 512 - -//unsigned short pic_num_pages; -unsigned short family = IS_24FJ; -unsigned long flashsize = 0x2AC00; -unsigned short eesizeb = 0; -unsigned long blstartaddr = 0x400L; // not currently used -unsigned long blendaddr = 0x23FFL; // not currently used - - -/* global settings, command line arguments */ - -uint8 g_verbose = 0; -uint8 g_hello_only = 0; -uint8 g_simulate = 0; -const char* g_device_path = NULL; -const char* g_hexfile_path = NULL; - -/* functions */ - -int readWithTimeout(int fd, uint8* out, int length, int timeout) -{ - fd_set fds; - struct timeval tv = {timeout, 0}; - int res = -1; - int got = 0; - - do - { - - FD_ZERO(&fds); - FD_SET(fd, &fds); - - res = select(fd + 1, &fds, NULL, NULL, &tv); - - if( res > 0 ) - { - res = read(fd, out, length); - if( res > 0 ) - { - length -= res; - got += res; - out += res; - } - else - { - break; - } - } - else - { - return res; - } - } - while( length > 0); - - return got; -} - -unsigned char hexdec(const char* pc) -{ - unsigned char temp; - - if(pc[0]>='a') - { - temp=pc[0]-'a'+10; - } - else if(pc[0] >= 'A') - { - temp=pc[0]-'A'+10; - } - else - { - temp=pc[0] - '0'; - } - temp=temp<<4; - - if(pc[1]>='a') - { - temp|=pc[1]-'a'+10; - } - else if(pc[1] >= 'A') - { - temp|=pc[1]-'A'+10; - } - else - { - temp|=pc[1] - '0'; - } - - return(temp & 0x0FF); - - - //return (((pc[0] >= 'A') ? ( pc[0] - 'A' + 10 ) : ( pc[0] - '0' ) ) << 4 | - // ((pc[1] >= 'A') ? ( pc[1] - 'A' + 10 ) : ( pc[1] - '0' ) )) & 0x0FF; - -} - -void dumpHex(uint8* buf, uint32 len) -{ - uint32 i=0; - - for(i=0; i pline && *pc <= ' ' ) - { - *pc-- = 0; - res--; - } - - if( res & 0x01 || res > 512 || res < 10) - { - fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); - return -1; - } - - hex_crc = 0; - - for( pc = pline, i = 0; i> 1] = hexdec(pc); - hex_crc += linebin[i >> 1]; - } - - binlen = res / 2; - - if( hex_crc != 0 ) - { - fprintf(stderr, "Checksum does not match, line %d\n", line_no); - return -1; - } - - hex_addr = (linebin[1] << 8) | linebin[2]; - hex_len = linebin[0]; - hex_type = linebin[3]; - - if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) - { - fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); - return -1; - } - - if( hex_type == 0x00 ) - { - f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU - - if( hex_len % 4 ) - { - fprintf(stderr, "Misaligned data, line %d\n", line_no); - return -1; - } - else if( f_addr >= flashsize ) - { - fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); - return -1; - } - - hex_words = hex_len / 4; - o_addr = (f_addr / 2) * PIC_WORD_SIZE; //BYTES - - for( i=0; i= flashsize ) - { - fprintf(stderr, "Address out of flash\n"); - return -1; - } - - //erase page - command[0] = (u_addr & 0x00FF0000) >> 16; - command[1] = (u_addr & 0x0000FF00) >> 8; - command[2] = (u_addr & 0x000000FF) >> 0; - command[COMMAND_OFFSET] = 0x01; //erase command - command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC - command[PAYLOAD_OFFSET] = makeCrc(command, 5); - - if( g_verbose ) - { - dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); - } - - printf("Erasing page %ld, %04lx...", page, u_addr); - - if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) - { - return -1; - } - - puts("OK"); - - //write 8 rows - for( row = 0; row < PIC_NUM_ROWS_IN_PAGE; row ++, u_addr += (PIC_NUM_WORDS_IN_ROW * 2)) - { - command[0] = (u_addr & 0x00FF0000) >> 16; - command[1] = (u_addr & 0x0000FF00) >> 8; - command[2] = (u_addr & 0x000000FF) >> 0; - command[COMMAND_OFFSET] = 0x02; //write command - command[LENGTH_OFFSET ] = PIC_ROW_SIZE + 0x01; //DATA_LENGTH + CRC - - memcpy(&command[PAYLOAD_OFFSET], &data[PIC_ROW_ADDR(page, row)], PIC_ROW_SIZE); - - command[PAYLOAD_OFFSET + PIC_ROW_SIZE] = makeCrc(command, HEADER_LENGTH + PIC_ROW_SIZE); - - printf("Writing page %ld row %ld, %04lx...", page, row + page*PIC_NUM_ROWS_IN_PAGE, u_addr); - - if( g_verbose ) - { - dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); - } - - if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) - { - return -1; - } - - puts("OK"); - - sleep(0); - - - done += PIC_ROW_SIZE; - } - } - - return done; -} - - -/* non-firmware functions */ - -int configurePort(int fd, unsigned long baudrate) -{ -#ifdef WIN32 - DCB dcb = {0}; - HANDLE hCom = (HANDLE)fd; - - dcb.DCBlength = sizeof(dcb); - - dcb.BaudRate = baudrate; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - if( !SetCommState(hCom, &dcb) ) - { - return -1; - } - - return (int)hCom; -#else - struct termios g_new_tio; - - memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); - cfmakeraw(&g_new_tio); - - g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); - g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); - g_new_tio.c_oflag = 0; - g_new_tio.c_lflag = 0; - - - g_new_tio.c_cc[VTIME] = 0; - g_new_tio.c_cc[VMIN] = 1; - - cfsetispeed (&g_new_tio, baudrate); - cfsetospeed (&g_new_tio, baudrate); - - tcflush(fd, TCIOFLUSH); - - return tcsetattr(fd, TCSANOW, &g_new_tio); -#endif -} - -int openPort(const char* dev, unsigned long flags) -{ - return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); -} - -int parseCommandLine(int argc, const char** argv) -{ - int i = 0; - - for(i=1; i flashsize ) - { - fprintf(stderr, "Could not load HEX file, result=%d\n", res); - goto Error; - } - - printf("Found %d words (%d bytes)\n", res, res * 3); - - //printf("Fixing bootloader/userprogram jumps\n"); - //fixJumps(bin_buff, pages_used); - } - - if( g_simulate ) - { - sendFirmware(dev_fd, bin_buff, pages_used); - goto Finished; - } - - if( !g_device_path ) - { - fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); - return -1; - } - - printf("Opening serial device %s...", g_device_path); - - dev_fd = openPort(g_device_path, 0); - - if( dev_fd < 0 ) - { - puts("ERROR"); - fprintf(stderr, "Could not open %s\n", g_device_path); - goto Error; - } - puts("OK"); - - printf("Configuring serial port settings..."); - - if( configurePort(dev_fd, B115200) < 0 ) - { - puts("ERROR"); - fprintf(stderr, "Could not configure device, errno=%d\n", errno); - goto Error; - } - puts("OK"); - - printf("Sending Hello to the Bootloader..."); - - //send HELLO - res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); - - res = readWithTimeout(dev_fd, buffer, 4, 3); - - if( res != 4 || buffer[3] != BOOTLOADER_OK ) - { - puts("ERROR"); - fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); - fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); - goto Error; - } - puts("OK\n"); //extra LF for spacing - - printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); - - printf("Device ID [%02x]:",buffer[0]); - switch(buffer[0]) - { -// case 0xd4: -// printf("PIC24FJ64GA002\n"); -// break; - case 9: - printf("PIC24FJ128GB206\n"); - // __PIC24FJ128GB206__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 17: - printf("PIC24FJ128GB210\n"); -// __PIC24FJ128GB210__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - - break; - case 18: - printf("PIC24FJ256GB206\n"); - // __PIC24FJ256GB206__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 19: - printf("PIC24FJ256GB210\n"); - // __PIC24FJ256GB210__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 191: - printf("PIC24FJ256DA206\n"); - // __PIC24FJ256DA206__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 192: - printf("PIC24FJ256DA210\n"); - // __PIC24FJ256DA210__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 217: - printf("PIC24FJ64GB106\n"); - // __PIC24FJ64GB106__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 218: - printf("PIC24FJ64GB108\n"); - // __PIC24FJ64GB108__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 219: - printf("PIC24FJ64GB110\n"); - // __PIC24FJ64GB110__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 229: - printf("PIC24FJ128GB106\n"); - // __PIC24FJ128GB106__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 230: - printf("PIC24FJ128GB108\n"); - // __PIC24FJ128GB108__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 231: - printf("PIC24FJ128GB110\n"); - // __PIC24FJ128GB110__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 235: - printf("PIC24FJ192GB106\n"); - // __PIC24FJ192GB106__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 236: - printf("PIC24FJ192GB108\n"); - // __PIC24FJ192GB108__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 237: - printf("PIC24FJ192GB110\n"); - // __PIC24FJ192GB110__ - family = IS_24FJ; - flashsize = 0x20C00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 241: - printf("PIC24FJ256GB106\n"); - // __PIC24FJ256GB106__ -#define IS_24FJ 1 - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 242: - printf("PIC24FJ256GB108\n"); - // __PIC24FJ256GB108__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 243: - printf("PIC24FJ256GB110\n"); - // __PIC24FJ256GB110__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 244: - printf("PIC24FJ32GB002\n"); - // __PIC24FJ32GB002__ - family = IS_24FJ; - flashsize = 0x5800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 245: - printf("PIC24FJ32GB004\n"); - // __PIC24FJ32GB004__ - family = IS_24FJ; - flashsize = 0x5800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 246: - printf("PIC24FJ64GB002\n"); - // __PIC24FJ64GB002__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 247: - printf("PIC24FJ64GB004\n"); - // __PIC24FJ64GB004__ - family = IS_24FJ; - flashsize = 0xAC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 250: - printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 251: - printf("PIC24FJ128DA110\n"); - // __PIC24FJ128DA110__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 252: - printf("PIC24FJ128DA206\n"); - // __PIC24FJ128DA206__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 253: - printf("PIC24FJ128DA210\n"); - // __PIC24FJ128DA210__ - family = IS_24FJ; - flashsize = 0x15800; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 254: - printf("PIC24FJ256DA106\n"); - // __PIC24FJ256DA106__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 255: - printf("PIC24FJ256DA110\n"); - // __PIC24FJ256DA110__ - family = IS_24FJ; - flashsize = 0x2AC00; - eesizeb = 0; - blstartaddr = 0x400L; - blendaddr = 0x23FFL; - break; - case 206: - printf("PIC24FJ16GA002\n"); - flashsize = 0x2C00; - - break; - - case 207: - printf("PIC24FJ16GA004\n"); - flashsize = 0x2C00; - - break; - - - case 208: - printf("PIC24FJ32GA002\n"); - flashsize = 0x5800; - - break; - - case 209: - printf("PIC24FJ32GA004\n"); - flashsize = 0x5800; - - break; - - case 210: - printf("PIC24FJ48GA002\n"); - flashsize = 0x8400; - - break; - - case 211: - printf("PIC24FJ48GA004\n"); - flashsize = 0x8400; - - break; - - - case 212: - printf("PIC24FJ64GA002\n"); - flashsize = 0xAC00; - - break; - - case 213: - printf("PIC24FJ64GA004\n"); - flashsize = 0xAC00; - - break; - - case 214: - printf("PIC24FJ64GA006\n"); - flashsize = 0xAC00; - - break; - - printf("PIC24FJ64GA008\n"); - flashsize = 0xAC00; - case 215: - break; - - case 216: - printf("PIC24FJ64GA010\n"); - flashsize = 0xAC00; - - break; - - case 220: - printf("PIC24FJ96GA006\n"); - flashsize = 0x10000; - - break; - - case 221: - printf("PIC24FJ96GA008\n"); - flashsize = 0x10000; - - break; - - case 222: - printf("PIC24FJ96GA010\n"); - flashsize = 0x10000; - - break; - - - case 223: - printf("PIC24FJ128GA006\n"); - flashsize = 0x15800; - - break; - - case 224: - printf("PIC24FJ128GA008\n"); - flashsize = 0x15800; - - break; - - case 225: - printf("PIC24FJ128GA010\n"); - flashsize = 0x15800; - - break; - - case 226: - printf("PIC24FJ128GA106\n"); - flashsize = 0x15800; - - break; - - case 227: - printf("PIC24FJ128GA108\n"); - flashsize = 0x15800; - break; - - case 228: - printf("PIC24FJ128GA110\n"); - flashsize = 0x15800; - break; - - case 232: - printf("PIC24FJ192GA106\n"); - flashsize = 0x20C00; - break; - - case 233: - printf("PIC24FJ192GA108\n"); - flashsize = 0x20C00; - break; - - case 234: - printf("PIC24FJ192GA110\n"); - flashsize = 0x20C00; - break; - - case 238: - printf("PIC24FJ256GA106\n"); - flashsize = 0x2AC00; - - break; - - case 239: - printf("PIC24FJ256GA108\n"); - flashsize = 0x2AC00; - break; - - case 240: - printf("PIC24FJ256GA110\n"); - flashsize = 0x2AC00; - break; - - - default: - printf("UNKNOWN"); - fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); - goto Error; - break; - } - - if( !g_hello_only ) - { - - res = sendFirmware(dev_fd, bin_buff, pages_used); - - if( res > 0 ) - { - puts("\nFirmware updated successfully :)!"); - //printf("Use screen %s 115200 to verify\n", g_device_path); - } - else - { - puts("\nError updating firmware :("); - goto Error; - } - - } - -Finished: - if( bin_buff ) - { - free( bin_buff ); - } - if( dev_fd >= 0 ) - { - close(dev_fd); - } - return 0; - -Error: - if( bin_buff ) - { - free( bin_buff ); - } - if( dev_fd >= 0 ) - { - close(dev_fd); - } - return -1; -} +/* + + Pirate-Loader for Bootloader v4 + + Version : 1.0.2 + + Changelog: + +2010-06-28 - Made HEX parser case-insensative + + + 2010-02-04 - Changed polling interval to 10ms on Windows select wrapper, suggested by Michal (robots) + + + 2010-02-04 - Added sleep(0) between write instructions, patch submitted by kbulgrien + + + 2010-01-22 - Added loader version number to the console output and source code + + + 2010-01-19 - Fixed BigEndian incompatibility + - Added programming simulate switch ( --simulate ) for data verification + + + 2010-01-18 - Initial release + + + Building: + + UNIX family systems: + + gcc pirate-loader.c -o pirate-loader + + WINDOWS: + + cl pirate-loader.c /DWIN32=1 + + + Usage: + + Run ./pirate-loader --help for more information on usage and possible switches + + */ + +#include +#include +#include +#include +#include + +#define PIRATE_LOADER_VERSION "1.0.2" + +#define STR_EXPAND(tok) #tok +#define OS_NAME(tok) STR_EXPAND(tok) + + +#ifdef WIN32 +#include +#include + +#define O_NOCTTY 0 +#define O_NDELAY 0 +#define B115200 921600 + +#define OS WINDOWS + +int write(int fd, const void* buf, int len) +{ + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bwritten = 0; + + + res = WriteFile(hCom, buf, len, &bwritten, NULL); + + if( res == FALSE ) + { + return -1; + } + else + { + return bwritten; + } +} + +int read(int fd, void* buf, int len) +{ + HANDLE hCom = (HANDLE)fd; + int res = 0; + unsigned long bread = 0; + + res = ReadFile(hCom, buf, len, &bread, NULL); + + if( res == FALSE ) + { + return -1; + } + else + { + return bread; + } +} + +int close(int fd) +{ + HANDLE hCom = (HANDLE)fd; + + CloseHandle(hCom); + return 0; +} + +int open(const char* path, unsigned long flags) +{ + static char full_path[32] = {0}; + + HANDLE hCom = NULL; + + if( path[0] != '\\' ) + { + _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", path); + path = full_path; + } + + hCom = CreateFileA(path, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if( !hCom || hCom == INVALID_HANDLE_VALUE ) + { + return -1; + } + else + { + return (int)hCom; + } +} + +int __stdcall select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfs, const struct timeval* timeout) +{ + time_t maxtc = time(0) + (timeout->tv_sec); + COMSTAT cs = {0}; +// JTR not used so get rig of warning unsigned long dwErrors = 0; + + if( readfds->fd_count != 1 ) + { + return -1; + } + + while( time(0) <= maxtc ) + { + //only one file supported + if( ClearCommError( (HANDLE)readfds->fd_array[0], 0, &cs) != TRUE ) + { + return -1; + } + + if( cs.cbInQue > 0 ) + { + return 1; + } + + Sleep(10); + } + return 0; +} + +unsigned int sleep(unsigned int sec) +{ + Sleep(sec * 1000); + + return 0; +} + +#else +#include +#include +#include +#include +#include +#endif + +/* macro definitions */ + +/* type definitions */ + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned long uint32; + +#if !defined OS +#define OS UNKNOWN +#endif + +#define BOOTLOADER_HELLO_STR "\xC1" +#define BOOTLOADER_OK 0x4B +#define BOOTLOADER_PROT 'P' +#define PIC_WORD_SIZE (3) +#define PIC_NUM_ROWS_IN_PAGE 8 +#define PIC_NUM_WORDS_IN_ROW 64 +#define PIC_ROW_SIZE (PIC_NUM_WORDS_IN_ROW * PIC_WORD_SIZE) +#define PIC_PAGE_SIZE (PIC_NUM_ROWS_IN_PAGE * PIC_ROW_SIZE) +#define PIC_ROW_ADDR(p,r) (((p) * PIC_PAGE_SIZE) + ((r) * PIC_ROW_SIZE)) +#define PIC_WORD_ADDR(p,r,w) (PIC_ROW_ADDR(p,r) + ((w) * PIC_WORD_SIZE)) +#define PIC_PAGE_ADDR(p) (PIC_PAGE_SIZE * (p)) + +#define PAYLOAD_OFFSET 5 +#define HEADER_LENGTH PAYLOAD_OFFSET +#define LENGTH_OFFSET 4 +#define COMMAND_OFFSET 3 +#define IS_24FJ 1 +#define PIC_NUM_PAGES 512 + +//#define flashsize 0x2AC00 //was 0xac00 +//#define PIC_NUM_PAGES 512 + +//unsigned short pic_num_pages; +unsigned short family = IS_24FJ; +unsigned long flashsize = 0x2AC00; +unsigned short eesizeb = 0; +unsigned long blstartaddr = 0x400L; // not currently used +unsigned long blendaddr = 0x23FFL; // not currently used + + +/* global settings, command line arguments */ + +uint8 g_verbose = 0; +uint8 g_hello_only = 0; +uint8 g_simulate = 0; +const char* g_device_path = NULL; +const char* g_hexfile_path = NULL; + +/* functions */ + +int readWithTimeout(int fd, uint8* out, int length, int timeout) +{ + fd_set fds; + struct timeval tv = {timeout, 0}; + int res = -1; + int got = 0; + + do + { + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + res = select(fd + 1, &fds, NULL, NULL, &tv); + + if( res > 0 ) + { + res = read(fd, out, length); + if( res > 0 ) + { + length -= res; + got += res; + out += res; + } + else + { + break; + } + } + else + { + return res; + } + } + while( length > 0); + + return got; +} + +unsigned char hexdec(const char* pc) +{ + unsigned char temp; + + if(pc[0]>='a') + { + temp=pc[0]-'a'+10; + } + else if(pc[0] >= 'A') + { + temp=pc[0]-'A'+10; + } + else + { + temp=pc[0] - '0'; + } + temp=temp<<4; + + if(pc[1]>='a') + { + temp|=pc[1]-'a'+10; + } + else if(pc[1] >= 'A') + { + temp|=pc[1]-'A'+10; + } + else + { + temp|=pc[1] - '0'; + } + + return(temp & 0x0FF); + + + //return (((pc[0] >= 'A') ? ( pc[0] - 'A' + 10 ) : ( pc[0] - '0' ) ) << 4 | + // ((pc[1] >= 'A') ? ( pc[1] - 'A' + 10 ) : ( pc[1] - '0' ) )) & 0x0FF; + +} + +void dumpHex(uint8* buf, uint32 len) +{ + uint32 i=0; + + for(i=0; i pline && *pc <= ' ' ) + { + *pc-- = 0; + res--; + } + + if( res & 0x01 || res > 512 || res < 10) + { + fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); + return -1; + } + + hex_crc = 0; + + for( pc = pline, i = 0; i> 1] = hexdec(pc); + hex_crc += linebin[i >> 1]; + } + + binlen = res / 2; + + if( hex_crc != 0 ) + { + fprintf(stderr, "Checksum does not match, line %d\n", line_no); + return -1; + } + + hex_addr = (linebin[1] << 8) | linebin[2]; + hex_len = linebin[0]; + hex_type = linebin[3]; + + if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) + { + fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); + return -1; + } + + if( hex_type == 0x00 ) + { + f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU + + if( hex_len % 4 ) + { + fprintf(stderr, "Misaligned data, line %d\n", line_no); + return -1; + } + else if( f_addr >= flashsize ) + { + fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); + return -1; + } + + hex_words = hex_len / 4; + o_addr = (f_addr / 2) * PIC_WORD_SIZE; //BYTES + + for( i=0; i= flashsize ) + { + fprintf(stderr, "Address out of flash\n"); + return -1; + } + + //erase page + command[0] = (u_addr & 0x00FF0000) >> 16; + command[1] = (u_addr & 0x0000FF00) >> 8; + command[2] = (u_addr & 0x000000FF) >> 0; + command[COMMAND_OFFSET] = 0x01; //erase command + command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC + command[PAYLOAD_OFFSET] = makeCrc(command, 5); + + if( g_verbose ) + { + dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); + } + + printf("Erasing page %ld, %04lx...", page, u_addr); + + if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) + { + return -1; + } + + puts("OK"); + + //write 8 rows + for( row = 0; row < PIC_NUM_ROWS_IN_PAGE; row ++, u_addr += (PIC_NUM_WORDS_IN_ROW * 2)) + { + command[0] = (u_addr & 0x00FF0000) >> 16; + command[1] = (u_addr & 0x0000FF00) >> 8; + command[2] = (u_addr & 0x000000FF) >> 0; + command[COMMAND_OFFSET] = 0x02; //write command + command[LENGTH_OFFSET ] = PIC_ROW_SIZE + 0x01; //DATA_LENGTH + CRC + + memcpy(&command[PAYLOAD_OFFSET], &data[PIC_ROW_ADDR(page, row)], PIC_ROW_SIZE); + + command[PAYLOAD_OFFSET + PIC_ROW_SIZE] = makeCrc(command, HEADER_LENGTH + PIC_ROW_SIZE); + + printf("Writing page %ld row %ld, %04lx...", page, row + page*PIC_NUM_ROWS_IN_PAGE, u_addr); + + if( g_verbose ) + { + dumpHex(command, HEADER_LENGTH + command[LENGTH_OFFSET]); + } + + if( g_simulate == 0 && sendCommandAndWaitForResponse(fd, command) < 0 ) + { + return -1; + } + + puts("OK"); + + sleep(0); + + + done += PIC_ROW_SIZE; + } + } + + return done; +} + + +/* non-firmware functions */ + +int configurePort(int fd, unsigned long baudrate) +{ +#ifdef WIN32 + DCB dcb = {0}; + HANDLE hCom = (HANDLE)fd; + + dcb.DCBlength = sizeof(dcb); + + dcb.BaudRate = baudrate; + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + if( !SetCommState(hCom, &dcb) ) + { + return -1; + } + + return (int)hCom; +#else + struct termios g_new_tio; + + memset(&g_new_tio, 0x00 , sizeof(g_new_tio)); + cfmakeraw(&g_new_tio); + + g_new_tio.c_cflag |= (CS8 | CLOCAL | CREAD); + g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE); + g_new_tio.c_oflag = 0; + g_new_tio.c_lflag = 0; + + + g_new_tio.c_cc[VTIME] = 0; + g_new_tio.c_cc[VMIN] = 1; + + cfsetispeed (&g_new_tio, baudrate); + cfsetospeed (&g_new_tio, baudrate); + + tcflush(fd, TCIOFLUSH); + + return tcsetattr(fd, TCSANOW, &g_new_tio); +#endif +} + +int openPort(const char* dev, unsigned long flags) +{ + return open(dev, O_RDWR | O_NOCTTY | O_NDELAY | flags); +} + +int parseCommandLine(int argc, const char** argv) +{ + int i = 0; + + for(i=1; i flashsize ) + { + fprintf(stderr, "Could not load HEX file, result=%d\n", res); + goto Error; + } + + printf("Found %d words (%d bytes)\n", res, res * 3); + + //printf("Fixing bootloader/userprogram jumps\n"); + //fixJumps(bin_buff, pages_used); + } + + if( g_simulate ) + { + sendFirmware(dev_fd, bin_buff, pages_used); + goto Finished; + } + + if( !g_device_path ) + { + fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); + return -1; + } + + printf("Opening serial device %s...", g_device_path); + + dev_fd = openPort(g_device_path, 0); + + if( dev_fd < 0 ) + { + puts("ERROR"); + fprintf(stderr, "Could not open %s\n", g_device_path); + goto Error; + } + puts("OK"); + + printf("Configuring serial port settings..."); + + if( configurePort(dev_fd, B115200) < 0 ) + { + puts("ERROR"); + fprintf(stderr, "Could not configure device, errno=%d\n", errno); + goto Error; + } + puts("OK"); + + printf("Sending Hello to the Bootloader..."); + + //send HELLO + res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); + + res = readWithTimeout(dev_fd, buffer, 4, 3); + + if( res != 4 || buffer[3] != BOOTLOADER_OK ) + { + puts("ERROR"); + fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); + fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); + goto Error; + } + puts("OK\n"); //extra LF for spacing + + printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); + + printf("Device ID [%02x]:",buffer[0]); + switch(buffer[0]) + { +// case 0xd4: +// printf("PIC24FJ64GA002\n"); +// break; + case 9: + printf("PIC24FJ128GB206\n"); + // __PIC24FJ128GB206__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 17: + printf("PIC24FJ128GB210\n"); +// __PIC24FJ128GB210__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + + break; + case 18: + printf("PIC24FJ256GB206\n"); + // __PIC24FJ256GB206__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 19: + printf("PIC24FJ256GB210\n"); + // __PIC24FJ256GB210__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 191: + printf("PIC24FJ256DA206\n"); + // __PIC24FJ256DA206__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 192: + printf("PIC24FJ256DA210\n"); + // __PIC24FJ256DA210__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 217: + printf("PIC24FJ64GB106\n"); + // __PIC24FJ64GB106__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 218: + printf("PIC24FJ64GB108\n"); + // __PIC24FJ64GB108__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 219: + printf("PIC24FJ64GB110\n"); + // __PIC24FJ64GB110__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 229: + printf("PIC24FJ128GB106\n"); + // __PIC24FJ128GB106__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 230: + printf("PIC24FJ128GB108\n"); + // __PIC24FJ128GB108__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 231: + printf("PIC24FJ128GB110\n"); + // __PIC24FJ128GB110__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 235: + printf("PIC24FJ192GB106\n"); + // __PIC24FJ192GB106__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 236: + printf("PIC24FJ192GB108\n"); + // __PIC24FJ192GB108__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 237: + printf("PIC24FJ192GB110\n"); + // __PIC24FJ192GB110__ + family = IS_24FJ; + flashsize = 0x20C00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 241: + printf("PIC24FJ256GB106\n"); + // __PIC24FJ256GB106__ +#define IS_24FJ 1 + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 242: + printf("PIC24FJ256GB108\n"); + // __PIC24FJ256GB108__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 243: + printf("PIC24FJ256GB110\n"); + // __PIC24FJ256GB110__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 244: + printf("PIC24FJ32GB002\n"); + // __PIC24FJ32GB002__ + family = IS_24FJ; + flashsize = 0x5800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 245: + printf("PIC24FJ32GB004\n"); + // __PIC24FJ32GB004__ + family = IS_24FJ; + flashsize = 0x5800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 246: + printf("PIC24FJ64GB002\n"); + // __PIC24FJ64GB002__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 247: + printf("PIC24FJ64GB004\n"); + // __PIC24FJ64GB004__ + family = IS_24FJ; + flashsize = 0xAC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 250: + printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 251: + printf("PIC24FJ128DA110\n"); + // __PIC24FJ128DA110__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 252: + printf("PIC24FJ128DA206\n"); + // __PIC24FJ128DA206__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 253: + printf("PIC24FJ128DA210\n"); + // __PIC24FJ128DA210__ + family = IS_24FJ; + flashsize = 0x15800; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 254: + printf("PIC24FJ256DA106\n"); + // __PIC24FJ256DA106__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 255: + printf("PIC24FJ256DA110\n"); + // __PIC24FJ256DA110__ + family = IS_24FJ; + flashsize = 0x2AC00; + eesizeb = 0; + blstartaddr = 0x400L; + blendaddr = 0x23FFL; + break; + case 206: + printf("PIC24FJ16GA002\n"); + flashsize = 0x2C00; + + break; + + case 207: + printf("PIC24FJ16GA004\n"); + flashsize = 0x2C00; + + break; + + + case 208: + printf("PIC24FJ32GA002\n"); + flashsize = 0x5800; + + break; + + case 209: + printf("PIC24FJ32GA004\n"); + flashsize = 0x5800; + + break; + + case 210: + printf("PIC24FJ48GA002\n"); + flashsize = 0x8400; + + break; + + case 211: + printf("PIC24FJ48GA004\n"); + flashsize = 0x8400; + + break; + + + case 212: + printf("PIC24FJ64GA002\n"); + flashsize = 0xAC00; + + break; + + case 213: + printf("PIC24FJ64GA004\n"); + flashsize = 0xAC00; + + break; + + case 214: + printf("PIC24FJ64GA006\n"); + flashsize = 0xAC00; + + break; + + printf("PIC24FJ64GA008\n"); + flashsize = 0xAC00; + case 215: + break; + + case 216: + printf("PIC24FJ64GA010\n"); + flashsize = 0xAC00; + + break; + + case 220: + printf("PIC24FJ96GA006\n"); + flashsize = 0x10000; + + break; + + case 221: + printf("PIC24FJ96GA008\n"); + flashsize = 0x10000; + + break; + + case 222: + printf("PIC24FJ96GA010\n"); + flashsize = 0x10000; + + break; + + + case 223: + printf("PIC24FJ128GA006\n"); + flashsize = 0x15800; + + break; + + case 224: + printf("PIC24FJ128GA008\n"); + flashsize = 0x15800; + + break; + + case 225: + printf("PIC24FJ128GA010\n"); + flashsize = 0x15800; + + break; + + case 226: + printf("PIC24FJ128GA106\n"); + flashsize = 0x15800; + + break; + + case 227: + printf("PIC24FJ128GA108\n"); + flashsize = 0x15800; + break; + + case 228: + printf("PIC24FJ128GA110\n"); + flashsize = 0x15800; + break; + + case 232: + printf("PIC24FJ192GA106\n"); + flashsize = 0x20C00; + break; + + case 233: + printf("PIC24FJ192GA108\n"); + flashsize = 0x20C00; + break; + + case 234: + printf("PIC24FJ192GA110\n"); + flashsize = 0x20C00; + break; + + case 238: + printf("PIC24FJ256GA106\n"); + flashsize = 0x2AC00; + + break; + + case 239: + printf("PIC24FJ256GA108\n"); + flashsize = 0x2AC00; + break; + + case 240: + printf("PIC24FJ256GA110\n"); + flashsize = 0x2AC00; + break; + + + default: + printf("UNKNOWN"); + fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); + goto Error; + break; + } + + if( !g_hello_only ) + { + + res = sendFirmware(dev_fd, bin_buff, pages_used); + + if( res > 0 ) + { + puts("\nFirmware updated successfully :)!"); + //printf("Use screen %s 115200 to verify\n", g_device_path); + } + else + { + puts("\nError updating firmware :("); + goto Error; + } + + } + +Finished: + if( bin_buff ) + { + free( bin_buff ); + } + if( dev_fd >= 0 ) + { + close(dev_fd); + } + return 0; + +Error: + if( bin_buff ) + { + free( bin_buff ); + } + if( dev_fd >= 0 ) + { + close(dev_fd); + } + return -1; +} diff --git a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/ftd2xx.h b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/ftd2xx.h index 0c4ef375..332cc21d 100644 --- a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/ftd2xx.h +++ b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/ftd2xx.h @@ -1,1011 +1,1011 @@ -/*++ - -Copyright © 2001-2010 Future Technology Devices International Limited - -THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT -OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) -HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR -TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, -EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -FTDI DRIVERS MAY BE USED ONLY IN CONJUNCTION WITH PRODUCTS BASED ON FTDI PARTS. - -FTDI DRIVERS MAY BE DISTRIBUTED IN ANY FORM AS LONG AS LICENSE INFORMATION IS NOT MODIFIED. - -IF A CUSTOM VENDOR ID AND/OR PRODUCT ID OR DESCRIPTION STRING ARE USED, IT IS THE -RESPONSIBILITY OF THE PRODUCT MANUFACTURER TO MAINTAIN ANY CHANGES AND SUBSEQUENT WHQL -RE-CERTIFICATION AS A RESULT OF MAKING THESE CHANGES. - - -Module Name: - -ftd2xx.h - -Abstract: - -Native USB device driver for FTDI FT232x, FT245x, FT2232x and FT4232x devices -FTD2XX library definitions - -Environment: - -kernel & user mode - -Revision History: - - 13/03/01 awm Created. - 13/01/03 awm Added device information support. - 19/03/03 awm Added FT_W32_CancelIo. - 12/06/03 awm Added FT_StopInTask and FT_RestartInTask. - 18/09/03 awm Added FT_SetResetPipeRetryCount. - 10/10/03 awm Added FT_ResetPort. - 23/01/04 awm Added support for open-by-location. - 16/03/04 awm Added support for FT2232C. - 23/09/04 awm Added support for FT232R. - 20/10/04 awm Added FT_CyclePort. - 18/01/05 awm Added FT_DEVICE_LIST_INFO_NODE type. - 11/02/05 awm Added LocId to FT_DEVICE_LIST_INFO_NODE. - 25/08/05 awm Added FT_SetDeadmanTimeout. - 02/12/05 awm Removed obsolete references. - 05/12/05 awm Added FT_GetVersion, FT_GetVersionEx. - 08/09/06 awm Added FT_W32_GetCommMask. - 11/09/06 awm Added FT_Rescan. - 11/07/07 awm Added support for FT2232H and FT4232H. - 10/08/07 awm Added flags definitions. - 21/11/07 mja Added FT_GetComPortNumber. - 05/06/08 mja Added EEPROM extensions for FT2232H. - - ---*/ - - -#ifndef FTD2XX_H -#define FTD2XX_H - -// The following ifdef block is the standard way of creating macros -// which make exporting from a DLL simpler. All files within this DLL -// are compiled with the FTD2XX_EXPORTS symbol defined on the command line. -// This symbol should not be defined on any project that uses this DLL. -// This way any other project whose source files include this file see -// FTD2XX_API functions as being imported from a DLL, whereas this DLL -// sees symbols defined with this macro as being exported. - -#ifdef FTD2XX_EXPORTS -#define FTD2XX_API __declspec(dllexport) -#else -#define FTD2XX_API __declspec(dllimport) -#endif - - -typedef PVOID FT_HANDLE; -typedef ULONG FT_STATUS; - -// -// Device status -// -enum { - FT_OK, - FT_INVALID_HANDLE, - FT_DEVICE_NOT_FOUND, - FT_DEVICE_NOT_OPENED, - FT_IO_ERROR, - FT_INSUFFICIENT_RESOURCES, - FT_INVALID_PARAMETER, - FT_INVALID_BAUD_RATE, - - FT_DEVICE_NOT_OPENED_FOR_ERASE, - FT_DEVICE_NOT_OPENED_FOR_WRITE, - FT_FAILED_TO_WRITE_DEVICE, - FT_EEPROM_READ_FAILED, - FT_EEPROM_WRITE_FAILED, - FT_EEPROM_ERASE_FAILED, - FT_EEPROM_NOT_PRESENT, - FT_EEPROM_NOT_PROGRAMMED, - FT_INVALID_ARGS, - FT_NOT_SUPPORTED, - FT_OTHER_ERROR, - FT_DEVICE_LIST_NOT_READY, -}; - - -#define FT_SUCCESS(status) ((status) == FT_OK) - -// -// FT_OpenEx Flags -// - -#define FT_OPEN_BY_SERIAL_NUMBER 1 -#define FT_OPEN_BY_DESCRIPTION 2 -#define FT_OPEN_BY_LOCATION 4 - -// -// FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags -// - -#define FT_LIST_NUMBER_ONLY 0x80000000 -#define FT_LIST_BY_INDEX 0x40000000 -#define FT_LIST_ALL 0x20000000 - -#define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) - -// -// Baud Rates -// - -#define FT_BAUD_300 300 -#define FT_BAUD_600 600 -#define FT_BAUD_1200 1200 -#define FT_BAUD_2400 2400 -#define FT_BAUD_4800 4800 -#define FT_BAUD_9600 9600 -#define FT_BAUD_14400 14400 -#define FT_BAUD_19200 19200 -#define FT_BAUD_38400 38400 -#define FT_BAUD_57600 57600 -#define FT_BAUD_115200 115200 -#define FT_BAUD_230400 230400 -#define FT_BAUD_460800 460800 -#define FT_BAUD_921600 921600 - -// -// Word Lengths -// - -#define FT_BITS_8 (UCHAR) 8 -#define FT_BITS_7 (UCHAR) 7 - -// -// Stop Bits -// - -#define FT_STOP_BITS_1 (UCHAR) 0 -#define FT_STOP_BITS_2 (UCHAR) 2 - -// -// Parity -// - -#define FT_PARITY_NONE (UCHAR) 0 -#define FT_PARITY_ODD (UCHAR) 1 -#define FT_PARITY_EVEN (UCHAR) 2 -#define FT_PARITY_MARK (UCHAR) 3 -#define FT_PARITY_SPACE (UCHAR) 4 - -// -// Flow Control -// - -#define FT_FLOW_NONE 0x0000 -#define FT_FLOW_RTS_CTS 0x0100 -#define FT_FLOW_DTR_DSR 0x0200 -#define FT_FLOW_XON_XOFF 0x0400 - -// -// Purge rx and tx buffers -// -#define FT_PURGE_RX 1 -#define FT_PURGE_TX 2 - -// -// Events -// - -typedef void (*PFT_EVENT_HANDLER)(DWORD,DWORD); - -#define FT_EVENT_RXCHAR 1 -#define FT_EVENT_MODEM_STATUS 2 -#define FT_EVENT_LINE_STATUS 4 - -// -// Timeouts -// - -#define FT_DEFAULT_RX_TIMEOUT 300 -#define FT_DEFAULT_TX_TIMEOUT 300 - -// -// Device types -// - -typedef ULONG FT_DEVICE; - -enum { - FT_DEVICE_BM, - FT_DEVICE_AM, - FT_DEVICE_100AX, - FT_DEVICE_UNKNOWN, - FT_DEVICE_2232C, - FT_DEVICE_232R, - FT_DEVICE_2232H, - FT_DEVICE_4232H, - FT_DEVICE_232H -}; - - -#ifdef __cplusplus -extern "C" { -#endif - - - FTD2XX_API - FT_STATUS WINAPI FT_Open( - int deviceNumber, - FT_HANDLE *pHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_OpenEx( - PVOID pArg1, - DWORD Flags, - FT_HANDLE *pHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ListDevices( - PVOID pArg1, - PVOID pArg2, - DWORD Flags - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Close( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Read( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD dwBytesToRead, - LPDWORD lpBytesReturned - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Write( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD dwBytesToWrite, - LPDWORD lpBytesWritten - ); - - FTD2XX_API - FT_STATUS WINAPI FT_IoCtl( - FT_HANDLE ftHandle, - DWORD dwIoControlCode, - LPVOID lpInBuf, - DWORD nInBufSize, - LPVOID lpOutBuf, - DWORD nOutBufSize, - LPDWORD lpBytesReturned, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBaudRate( - FT_HANDLE ftHandle, - ULONG BaudRate - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDivisor( - FT_HANDLE ftHandle, - USHORT Divisor - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDataCharacteristics( - FT_HANDLE ftHandle, - UCHAR WordLength, - UCHAR StopBits, - UCHAR Parity - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetFlowControl( - FT_HANDLE ftHandle, - USHORT FlowControl, - UCHAR XonChar, - UCHAR XoffChar - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ResetDevice( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDtr( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ClrDtr( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetRts( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ClrRts( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetModemStatus( - FT_HANDLE ftHandle, - ULONG *pModemStatus - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetChars( - FT_HANDLE ftHandle, - UCHAR EventChar, - UCHAR EventCharEnabled, - UCHAR ErrorChar, - UCHAR ErrorCharEnabled - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Purge( - FT_HANDLE ftHandle, - ULONG Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetTimeouts( - FT_HANDLE ftHandle, - ULONG ReadTimeout, - ULONG WriteTimeout - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetQueueStatus( - FT_HANDLE ftHandle, - DWORD *dwRxBytes - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetEventNotification( - FT_HANDLE ftHandle, - DWORD Mask, - PVOID Param - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetStatus( - FT_HANDLE ftHandle, - DWORD *dwRxBytes, - DWORD *dwTxBytes, - DWORD *dwEventDWord - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBreakOn( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBreakOff( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetWaitMask( - FT_HANDLE ftHandle, - DWORD Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_WaitOnMask( - FT_HANDLE ftHandle, - DWORD *Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetEventStatus( - FT_HANDLE ftHandle, - DWORD *dwEventDWord - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ReadEE( - FT_HANDLE ftHandle, - DWORD dwWordOffset, - LPWORD lpwValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_WriteEE( - FT_HANDLE ftHandle, - DWORD dwWordOffset, - WORD wValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EraseEE( - FT_HANDLE ftHandle - ); - - // - // structure to hold program data for FT_Program function - // - typedef struct ft_program_data { - - DWORD Signature1; // Header - must be 0x00000000 - DWORD Signature2; // Header - must be 0xffffffff - DWORD Version; // Header - FT_PROGRAM_DATA version - // 0 = original - // 1 = FT2232C extensions - // 2 = FT232R extensions - // 3 = FT2232H extensions - // 4 = FT4232H extensions - - WORD VendorId; // 0x0403 - WORD ProductId; // 0x6001 - char *Manufacturer; // "FTDI" - char *ManufacturerId; // "FT" - char *Description; // "USB HS Serial Converter" - char *SerialNumber; // "FT000001" if fixed, or NULL - WORD MaxPower; // 0 < MaxPower <= 500 - WORD PnP; // 0 = disabled, 1 = enabled - WORD SelfPowered; // 0 = bus powered, 1 = self powered - WORD RemoteWakeup; // 0 = not capable, 1 = capable - // - // Rev4 (FT232B) extensions - // - UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise - UCHAR IsoIn; // non-zero if in endpoint is isochronous - UCHAR IsoOut; // non-zero if out endpoint is isochronous - UCHAR PullDownEnable; // non-zero if pull down enabled - UCHAR SerNumEnable; // non-zero if serial number to be used - UCHAR USBVersionEnable; // non-zero if chip uses USBVersion - WORD USBVersion; // BCD (0x0200 => USB2) - // - // Rev 5 (FT2232) extensions - // - UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise - UCHAR IsoInA; // non-zero if in endpoint is isochronous - UCHAR IsoInB; // non-zero if in endpoint is isochronous - UCHAR IsoOutA; // non-zero if out endpoint is isochronous - UCHAR IsoOutB; // non-zero if out endpoint is isochronous - UCHAR PullDownEnable5; // non-zero if pull down enabled - UCHAR SerNumEnable5; // non-zero if serial number to be used - UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion - WORD USBVersion5; // BCD (0x0200 => USB2) - UCHAR AIsHighCurrent; // non-zero if interface is high current - UCHAR BIsHighCurrent; // non-zero if interface is high current - UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO - UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR IFAIsFastSer; // non-zero if interface is Fast serial - UCHAR AIsVCP; // non-zero if interface is to use VCP drivers - UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO - UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR IFBIsFastSer; // non-zero if interface is Fast serial - UCHAR BIsVCP; // non-zero if interface is to use VCP drivers - // - // Rev 6 (FT232R) extensions - // - UCHAR UseExtOsc; // Use External Oscillator - UCHAR HighDriveIOs; // High Drive I/Os - UCHAR EndpointSize; // Endpoint size - UCHAR PullDownEnableR; // non-zero if pull down enabled - UCHAR SerNumEnableR; // non-zero if serial number to be used - UCHAR InvertTXD; // non-zero if invert TXD - UCHAR InvertRXD; // non-zero if invert RXD - UCHAR InvertRTS; // non-zero if invert RTS - UCHAR InvertCTS; // non-zero if invert CTS - UCHAR InvertDTR; // non-zero if invert DTR - UCHAR InvertDSR; // non-zero if invert DSR - UCHAR InvertDCD; // non-zero if invert DCD - UCHAR InvertRI; // non-zero if invert RI - UCHAR Cbus0; // Cbus Mux control - UCHAR Cbus1; // Cbus Mux control - UCHAR Cbus2; // Cbus Mux control - UCHAR Cbus3; // Cbus Mux control - UCHAR Cbus4; // Cbus Mux control - UCHAR RIsD2XX; // non-zero if using D2XX driver - // - // Rev 7 (FT2232H) Extensions - // - UCHAR PullDownEnable7; // non-zero if pull down enabled - UCHAR SerNumEnable7; // non-zero if serial number to be used - UCHAR ALSlowSlew; // non-zero if AL pins have slow slew - UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input - UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR AHSlowSlew; // non-zero if AH pins have slow slew - UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input - UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BLSlowSlew; // non-zero if BL pins have slow slew - UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input - UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BHSlowSlew; // non-zero if BH pins have slow slew - UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input - UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO - UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial - UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers - UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO - UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial - UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers - UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs - // - // Rev 8 (FT4232H) Extensions - // - UCHAR PullDownEnable8; // non-zero if pull down enabled - UCHAR SerNumEnable8; // non-zero if serial number to be used - UCHAR ASlowSlew; // non-zero if AL pins have slow slew - UCHAR ASchmittInput; // non-zero if AL pins are Schmitt input - UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BSlowSlew; // non-zero if AH pins have slow slew - UCHAR BSchmittInput; // non-zero if AH pins are Schmitt input - UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR CSlowSlew; // non-zero if BL pins have slow slew - UCHAR CSchmittInput; // non-zero if BL pins are Schmitt input - UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR DSlowSlew; // non-zero if BH pins have slow slew - UCHAR DSchmittInput; // non-zero if BH pins are Schmitt input - UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN - UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN - UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN - UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN - UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers - - } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; - - FTD2XX_API - FT_STATUS WINAPI FT_EE_Program( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ProgramEx( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_Read( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadEx( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UASize( - FT_HANDLE ftHandle, - LPDWORD lpdwSize - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UAWrite( - FT_HANDLE ftHandle, - PUCHAR pucData, - DWORD dwDataLen - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UARead( - FT_HANDLE ftHandle, - PUCHAR pucData, - DWORD dwDataLen, - LPDWORD lpdwBytesRead - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetLatencyTimer( - FT_HANDLE ftHandle, - UCHAR ucLatency - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetLatencyTimer( - FT_HANDLE ftHandle, - PUCHAR pucLatency - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBitMode( - FT_HANDLE ftHandle, - UCHAR ucMask, - UCHAR ucEnable - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetBitMode( - FT_HANDLE ftHandle, - PUCHAR pucMode - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetUSBParameters( - FT_HANDLE ftHandle, - ULONG ulInTransferSize, - ULONG ulOutTransferSize - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDeadmanTimeout( - FT_HANDLE ftHandle, - ULONG ulDeadmanTimeout - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfo( - FT_HANDLE ftHandle, - FT_DEVICE *lpftDevice, - LPDWORD lpdwID, - PCHAR SerialNumber, - PCHAR Description, - LPVOID Dummy - ); - - FTD2XX_API - FT_STATUS WINAPI FT_StopInTask( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_RestartInTask( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetResetPipeRetryCount( - FT_HANDLE ftHandle, - DWORD dwCount - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ResetPort( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_CyclePort( - FT_HANDLE ftHandle - ); - - - // - // Win32-type functions - // - - FTD2XX_API - FT_HANDLE WINAPI FT_W32_CreateFile( - LPCTSTR lpszName, - DWORD dwAccess, - DWORD dwShareMode, - LPSECURITY_ATTRIBUTES lpSecurityAttributes, - DWORD dwCreate, - DWORD dwAttrsAndFlags, - HANDLE hTemplate - ); - - FTD2XX_API - BOOL WINAPI FT_W32_CloseHandle( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_ReadFile( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD nBufferSize, - LPDWORD lpBytesReturned, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - BOOL WINAPI FT_W32_WriteFile( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD nBufferSize, - LPDWORD lpBytesWritten, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - DWORD WINAPI FT_W32_GetLastError( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetOverlappedResult( - FT_HANDLE ftHandle, - LPOVERLAPPED lpOverlapped, - LPDWORD lpdwBytesTransferred, - BOOL bWait - ); - - FTD2XX_API - BOOL WINAPI FT_W32_CancelIo( - FT_HANDLE ftHandle - ); - - - // - // Win32 COMM API type functions - // - typedef struct _FTCOMSTAT { - DWORD fCtsHold : 1; - DWORD fDsrHold : 1; - DWORD fRlsdHold : 1; - DWORD fXoffHold : 1; - DWORD fXoffSent : 1; - DWORD fEof : 1; - DWORD fTxim : 1; - DWORD fReserved : 25; - DWORD cbInQue; - DWORD cbOutQue; - } FTCOMSTAT, *LPFTCOMSTAT; - - typedef struct _FTDCB { - DWORD DCBlength; /* sizeof(FTDCB) */ - DWORD BaudRate; /* Baudrate at which running */ - DWORD fBinary: 1; /* Binary Mode (skip EOF check) */ - DWORD fParity: 1; /* Enable parity checking */ - DWORD fOutxCtsFlow:1; /* CTS handshaking on output */ - DWORD fOutxDsrFlow:1; /* DSR handshaking on output */ - DWORD fDtrControl:2; /* DTR Flow control */ - DWORD fDsrSensitivity:1; /* DSR Sensitivity */ - DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */ - DWORD fOutX: 1; /* Enable output X-ON/X-OFF */ - DWORD fInX: 1; /* Enable input X-ON/X-OFF */ - DWORD fErrorChar: 1; /* Enable Err Replacement */ - DWORD fNull: 1; /* Enable Null stripping */ - DWORD fRtsControl:2; /* Rts Flow control */ - DWORD fAbortOnError:1; /* Abort all reads and writes on Error */ - DWORD fDummy2:17; /* Reserved */ - WORD wReserved; /* Not currently used */ - WORD XonLim; /* Transmit X-ON threshold */ - WORD XoffLim; /* Transmit X-OFF threshold */ - BYTE ByteSize; /* Number of bits/byte, 4-8 */ - BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ - BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 */ - char XonChar; /* Tx and Rx X-ON character */ - char XoffChar; /* Tx and Rx X-OFF character */ - char ErrorChar; /* Error replacement char */ - char EofChar; /* End of Input character */ - char EvtChar; /* Received Event character */ - WORD wReserved1; /* Fill for now. */ - } FTDCB, *LPFTDCB; - - typedef struct _FTTIMEOUTS { - DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ - DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ - DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ - DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ - DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ - } FTTIMEOUTS,*LPFTTIMEOUTS; - - - FTD2XX_API - BOOL WINAPI FT_W32_ClearCommBreak( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_ClearCommError( - FT_HANDLE ftHandle, - LPDWORD lpdwErrors, - LPFTCOMSTAT lpftComstat - ); - - FTD2XX_API - BOOL WINAPI FT_W32_EscapeCommFunction( - FT_HANDLE ftHandle, - DWORD dwFunc - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommModemStatus( - FT_HANDLE ftHandle, - LPDWORD lpdwModemStatus - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommState( - FT_HANDLE ftHandle, - LPFTDCB lpftDcb - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommTimeouts( - FT_HANDLE ftHandle, - FTTIMEOUTS *pTimeouts - ); - - FTD2XX_API - BOOL WINAPI FT_W32_PurgeComm( - FT_HANDLE ftHandle, - DWORD dwMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommBreak( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommMask( - FT_HANDLE ftHandle, - ULONG ulEventMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommMask( - FT_HANDLE ftHandle, - LPDWORD lpdwEventMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommState( - FT_HANDLE ftHandle, - LPFTDCB lpftDcb - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommTimeouts( - FT_HANDLE ftHandle, - FTTIMEOUTS *pTimeouts - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetupComm( - FT_HANDLE ftHandle, - DWORD dwReadBufferSize, - DWORD dwWriteBufferSize - ); - - FTD2XX_API - BOOL WINAPI FT_W32_WaitCommEvent( - FT_HANDLE ftHandle, - PULONG pulEvent, - LPOVERLAPPED lpOverlapped - ); - - - // - // Device information - // - - typedef struct _ft_device_list_info_node { - ULONG Flags; - ULONG Type; - ULONG ID; - DWORD LocId; - char SerialNumber[16]; - char Description[64]; - FT_HANDLE ftHandle; - } FT_DEVICE_LIST_INFO_NODE; - - // Device information flags - enum { - FT_FLAGS_OPENED = 1, - FT_FLAGS_HISPEED = 2 - }; - - - FTD2XX_API - FT_STATUS WINAPI FT_CreateDeviceInfoList( - LPDWORD lpdwNumDevs - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfoList( - FT_DEVICE_LIST_INFO_NODE *pDest, - LPDWORD lpdwNumDevs - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfoDetail( - DWORD dwIndex, - LPDWORD lpdwFlags, - LPDWORD lpdwType, - LPDWORD lpdwID, - LPDWORD lpdwLocId, - LPVOID lpSerialNumber, - LPVOID lpDescription, - FT_HANDLE *pftHandle - ); - - - // - // Version information - // - - FTD2XX_API - FT_STATUS WINAPI FT_GetDriverVersion( - FT_HANDLE ftHandle, - LPDWORD lpdwVersion - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetLibraryVersion( - LPDWORD lpdwVersion - ); - - - FTD2XX_API - FT_STATUS WINAPI FT_Rescan( - void - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Reload( - WORD wVid, - WORD wPid - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetComPortNumber( - FT_HANDLE ftHandle, - LPLONG lpdwComPortNumber - ); - - - // - // FT232H additional EEPROM functions - // - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadConfig( - FT_HANDLE ftHandle, - UCHAR ucAddress, - PUCHAR pucValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_WriteConfig( - FT_HANDLE ftHandle, - UCHAR ucAddress, - UCHAR ucValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadECC( - FT_HANDLE ftHandle, - UCHAR ucOption, - LPWORD lpwValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetQueueStatusEx( - FT_HANDLE ftHandle, - DWORD *dwRxBytes - ); - - -#ifdef __cplusplus -} -#endif - - -#endif /* FTD2XX_H */ - +/*++ + +Copyright © 2001-2010 Future Technology Devices International Limited + +THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +FTDI DRIVERS MAY BE USED ONLY IN CONJUNCTION WITH PRODUCTS BASED ON FTDI PARTS. + +FTDI DRIVERS MAY BE DISTRIBUTED IN ANY FORM AS LONG AS LICENSE INFORMATION IS NOT MODIFIED. + +IF A CUSTOM VENDOR ID AND/OR PRODUCT ID OR DESCRIPTION STRING ARE USED, IT IS THE +RESPONSIBILITY OF THE PRODUCT MANUFACTURER TO MAINTAIN ANY CHANGES AND SUBSEQUENT WHQL +RE-CERTIFICATION AS A RESULT OF MAKING THESE CHANGES. + + +Module Name: + +ftd2xx.h + +Abstract: + +Native USB device driver for FTDI FT232x, FT245x, FT2232x and FT4232x devices +FTD2XX library definitions + +Environment: + +kernel & user mode + +Revision History: + + 13/03/01 awm Created. + 13/01/03 awm Added device information support. + 19/03/03 awm Added FT_W32_CancelIo. + 12/06/03 awm Added FT_StopInTask and FT_RestartInTask. + 18/09/03 awm Added FT_SetResetPipeRetryCount. + 10/10/03 awm Added FT_ResetPort. + 23/01/04 awm Added support for open-by-location. + 16/03/04 awm Added support for FT2232C. + 23/09/04 awm Added support for FT232R. + 20/10/04 awm Added FT_CyclePort. + 18/01/05 awm Added FT_DEVICE_LIST_INFO_NODE type. + 11/02/05 awm Added LocId to FT_DEVICE_LIST_INFO_NODE. + 25/08/05 awm Added FT_SetDeadmanTimeout. + 02/12/05 awm Removed obsolete references. + 05/12/05 awm Added FT_GetVersion, FT_GetVersionEx. + 08/09/06 awm Added FT_W32_GetCommMask. + 11/09/06 awm Added FT_Rescan. + 11/07/07 awm Added support for FT2232H and FT4232H. + 10/08/07 awm Added flags definitions. + 21/11/07 mja Added FT_GetComPortNumber. + 05/06/08 mja Added EEPROM extensions for FT2232H. + + +--*/ + + +#ifndef FTD2XX_H +#define FTD2XX_H + +// The following ifdef block is the standard way of creating macros +// which make exporting from a DLL simpler. All files within this DLL +// are compiled with the FTD2XX_EXPORTS symbol defined on the command line. +// This symbol should not be defined on any project that uses this DLL. +// This way any other project whose source files include this file see +// FTD2XX_API functions as being imported from a DLL, whereas this DLL +// sees symbols defined with this macro as being exported. + +#ifdef FTD2XX_EXPORTS +#define FTD2XX_API __declspec(dllexport) +#else +#define FTD2XX_API __declspec(dllimport) +#endif + + +typedef PVOID FT_HANDLE; +typedef ULONG FT_STATUS; + +// +// Device status +// +enum { + FT_OK, + FT_INVALID_HANDLE, + FT_DEVICE_NOT_FOUND, + FT_DEVICE_NOT_OPENED, + FT_IO_ERROR, + FT_INSUFFICIENT_RESOURCES, + FT_INVALID_PARAMETER, + FT_INVALID_BAUD_RATE, + + FT_DEVICE_NOT_OPENED_FOR_ERASE, + FT_DEVICE_NOT_OPENED_FOR_WRITE, + FT_FAILED_TO_WRITE_DEVICE, + FT_EEPROM_READ_FAILED, + FT_EEPROM_WRITE_FAILED, + FT_EEPROM_ERASE_FAILED, + FT_EEPROM_NOT_PRESENT, + FT_EEPROM_NOT_PROGRAMMED, + FT_INVALID_ARGS, + FT_NOT_SUPPORTED, + FT_OTHER_ERROR, + FT_DEVICE_LIST_NOT_READY, +}; + + +#define FT_SUCCESS(status) ((status) == FT_OK) + +// +// FT_OpenEx Flags +// + +#define FT_OPEN_BY_SERIAL_NUMBER 1 +#define FT_OPEN_BY_DESCRIPTION 2 +#define FT_OPEN_BY_LOCATION 4 + +// +// FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags +// + +#define FT_LIST_NUMBER_ONLY 0x80000000 +#define FT_LIST_BY_INDEX 0x40000000 +#define FT_LIST_ALL 0x20000000 + +#define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) + +// +// Baud Rates +// + +#define FT_BAUD_300 300 +#define FT_BAUD_600 600 +#define FT_BAUD_1200 1200 +#define FT_BAUD_2400 2400 +#define FT_BAUD_4800 4800 +#define FT_BAUD_9600 9600 +#define FT_BAUD_14400 14400 +#define FT_BAUD_19200 19200 +#define FT_BAUD_38400 38400 +#define FT_BAUD_57600 57600 +#define FT_BAUD_115200 115200 +#define FT_BAUD_230400 230400 +#define FT_BAUD_460800 460800 +#define FT_BAUD_921600 921600 + +// +// Word Lengths +// + +#define FT_BITS_8 (UCHAR) 8 +#define FT_BITS_7 (UCHAR) 7 + +// +// Stop Bits +// + +#define FT_STOP_BITS_1 (UCHAR) 0 +#define FT_STOP_BITS_2 (UCHAR) 2 + +// +// Parity +// + +#define FT_PARITY_NONE (UCHAR) 0 +#define FT_PARITY_ODD (UCHAR) 1 +#define FT_PARITY_EVEN (UCHAR) 2 +#define FT_PARITY_MARK (UCHAR) 3 +#define FT_PARITY_SPACE (UCHAR) 4 + +// +// Flow Control +// + +#define FT_FLOW_NONE 0x0000 +#define FT_FLOW_RTS_CTS 0x0100 +#define FT_FLOW_DTR_DSR 0x0200 +#define FT_FLOW_XON_XOFF 0x0400 + +// +// Purge rx and tx buffers +// +#define FT_PURGE_RX 1 +#define FT_PURGE_TX 2 + +// +// Events +// + +typedef void (*PFT_EVENT_HANDLER)(DWORD,DWORD); + +#define FT_EVENT_RXCHAR 1 +#define FT_EVENT_MODEM_STATUS 2 +#define FT_EVENT_LINE_STATUS 4 + +// +// Timeouts +// + +#define FT_DEFAULT_RX_TIMEOUT 300 +#define FT_DEFAULT_TX_TIMEOUT 300 + +// +// Device types +// + +typedef ULONG FT_DEVICE; + +enum { + FT_DEVICE_BM, + FT_DEVICE_AM, + FT_DEVICE_100AX, + FT_DEVICE_UNKNOWN, + FT_DEVICE_2232C, + FT_DEVICE_232R, + FT_DEVICE_2232H, + FT_DEVICE_4232H, + FT_DEVICE_232H +}; + + +#ifdef __cplusplus +extern "C" { +#endif + + + FTD2XX_API + FT_STATUS WINAPI FT_Open( + int deviceNumber, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_OpenEx( + PVOID pArg1, + DWORD Flags, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ListDevices( + PVOID pArg1, + PVOID pArg2, + DWORD Flags + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Close( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Read( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToRead, + LPDWORD lpBytesReturned + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Write( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToWrite, + LPDWORD lpBytesWritten + ); + + FTD2XX_API + FT_STATUS WINAPI FT_IoCtl( + FT_HANDLE ftHandle, + DWORD dwIoControlCode, + LPVOID lpInBuf, + DWORD nInBufSize, + LPVOID lpOutBuf, + DWORD nOutBufSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBaudRate( + FT_HANDLE ftHandle, + ULONG BaudRate + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDivisor( + FT_HANDLE ftHandle, + USHORT Divisor + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDataCharacteristics( + FT_HANDLE ftHandle, + UCHAR WordLength, + UCHAR StopBits, + UCHAR Parity + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetFlowControl( + FT_HANDLE ftHandle, + USHORT FlowControl, + UCHAR XonChar, + UCHAR XoffChar + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetDevice( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetModemStatus( + FT_HANDLE ftHandle, + ULONG *pModemStatus + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetChars( + FT_HANDLE ftHandle, + UCHAR EventChar, + UCHAR EventCharEnabled, + UCHAR ErrorChar, + UCHAR ErrorCharEnabled + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Purge( + FT_HANDLE ftHandle, + ULONG Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetTimeouts( + FT_HANDLE ftHandle, + ULONG ReadTimeout, + ULONG WriteTimeout + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetEventNotification( + FT_HANDLE ftHandle, + DWORD Mask, + PVOID Param + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes, + DWORD *dwTxBytes, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOn( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOff( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetWaitMask( + FT_HANDLE ftHandle, + DWORD Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WaitOnMask( + FT_HANDLE ftHandle, + DWORD *Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetEventStatus( + FT_HANDLE ftHandle, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ReadEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WriteEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + WORD wValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EraseEE( + FT_HANDLE ftHandle + ); + + // + // structure to hold program data for FT_Program function + // + typedef struct ft_program_data { + + DWORD Signature1; // Header - must be 0x00000000 + DWORD Signature2; // Header - must be 0xffffffff + DWORD Version; // Header - FT_PROGRAM_DATA version + // 0 = original + // 1 = FT2232C extensions + // 2 = FT232R extensions + // 3 = FT2232H extensions + // 4 = FT4232H extensions + + WORD VendorId; // 0x0403 + WORD ProductId; // 0x6001 + char *Manufacturer; // "FTDI" + char *ManufacturerId; // "FT" + char *Description; // "USB HS Serial Converter" + char *SerialNumber; // "FT000001" if fixed, or NULL + WORD MaxPower; // 0 < MaxPower <= 500 + WORD PnP; // 0 = disabled, 1 = enabled + WORD SelfPowered; // 0 = bus powered, 1 = self powered + WORD RemoteWakeup; // 0 = not capable, 1 = capable + // + // Rev4 (FT232B) extensions + // + UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise + UCHAR IsoIn; // non-zero if in endpoint is isochronous + UCHAR IsoOut; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable; // non-zero if pull down enabled + UCHAR SerNumEnable; // non-zero if serial number to be used + UCHAR USBVersionEnable; // non-zero if chip uses USBVersion + WORD USBVersion; // BCD (0x0200 => USB2) + // + // Rev 5 (FT2232) extensions + // + UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise + UCHAR IsoInA; // non-zero if in endpoint is isochronous + UCHAR IsoInB; // non-zero if in endpoint is isochronous + UCHAR IsoOutA; // non-zero if out endpoint is isochronous + UCHAR IsoOutB; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable5; // non-zero if pull down enabled + UCHAR SerNumEnable5; // non-zero if serial number to be used + UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion + WORD USBVersion5; // BCD (0x0200 => USB2) + UCHAR AIsHighCurrent; // non-zero if interface is high current + UCHAR BIsHighCurrent; // non-zero if interface is high current + UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer; // non-zero if interface is Fast serial + UCHAR AIsVCP; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsVCP; // non-zero if interface is to use VCP drivers + // + // Rev 6 (FT232R) extensions + // + UCHAR UseExtOsc; // Use External Oscillator + UCHAR HighDriveIOs; // High Drive I/Os + UCHAR EndpointSize; // Endpoint size + UCHAR PullDownEnableR; // non-zero if pull down enabled + UCHAR SerNumEnableR; // non-zero if serial number to be used + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR RIsD2XX; // non-zero if using D2XX driver + // + // Rev 7 (FT2232H) Extensions + // + UCHAR PullDownEnable7; // non-zero if pull down enabled + UCHAR SerNumEnable7; // non-zero if serial number to be used + UCHAR ALSlowSlew; // non-zero if AL pins have slow slew + UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR AHSlowSlew; // non-zero if AH pins have slow slew + UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BLSlowSlew; // non-zero if BL pins have slow slew + UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BHSlowSlew; // non-zero if BH pins have slow slew + UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial + UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial + UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // + // Rev 8 (FT4232H) Extensions + // + UCHAR PullDownEnable8; // non-zero if pull down enabled + UCHAR SerNumEnable8; // non-zero if serial number to be used + UCHAR ASlowSlew; // non-zero if AL pins have slow slew + UCHAR ASchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BSlowSlew; // non-zero if AH pins have slow slew + UCHAR BSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR CSlowSlew; // non-zero if BL pins have slow slew + UCHAR CSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR DSlowSlew; // non-zero if BH pins have slow slew + UCHAR DSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN + UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN + UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN + UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN + UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers + + } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Program( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ProgramEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Read( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UASize( + FT_HANDLE ftHandle, + LPDWORD lpdwSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UAWrite( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UARead( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen, + LPDWORD lpdwBytesRead + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetLatencyTimer( + FT_HANDLE ftHandle, + UCHAR ucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLatencyTimer( + FT_HANDLE ftHandle, + PUCHAR pucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBitMode( + FT_HANDLE ftHandle, + UCHAR ucMask, + UCHAR ucEnable + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetBitMode( + FT_HANDLE ftHandle, + PUCHAR pucMode + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetUSBParameters( + FT_HANDLE ftHandle, + ULONG ulInTransferSize, + ULONG ulOutTransferSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDeadmanTimeout( + FT_HANDLE ftHandle, + ULONG ulDeadmanTimeout + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfo( + FT_HANDLE ftHandle, + FT_DEVICE *lpftDevice, + LPDWORD lpdwID, + PCHAR SerialNumber, + PCHAR Description, + LPVOID Dummy + ); + + FTD2XX_API + FT_STATUS WINAPI FT_StopInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_RestartInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetResetPipeRetryCount( + FT_HANDLE ftHandle, + DWORD dwCount + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetPort( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_CyclePort( + FT_HANDLE ftHandle + ); + + + // + // Win32-type functions + // + + FTD2XX_API + FT_HANDLE WINAPI FT_W32_CreateFile( + LPCTSTR lpszName, + DWORD dwAccess, + DWORD dwShareMode, + LPSECURITY_ATTRIBUTES lpSecurityAttributes, + DWORD dwCreate, + DWORD dwAttrsAndFlags, + HANDLE hTemplate + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CloseHandle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ReadFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WriteFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesWritten, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + DWORD WINAPI FT_W32_GetLastError( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetOverlappedResult( + FT_HANDLE ftHandle, + LPOVERLAPPED lpOverlapped, + LPDWORD lpdwBytesTransferred, + BOOL bWait + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CancelIo( + FT_HANDLE ftHandle + ); + + + // + // Win32 COMM API type functions + // + typedef struct _FTCOMSTAT { + DWORD fCtsHold : 1; + DWORD fDsrHold : 1; + DWORD fRlsdHold : 1; + DWORD fXoffHold : 1; + DWORD fXoffSent : 1; + DWORD fEof : 1; + DWORD fTxim : 1; + DWORD fReserved : 25; + DWORD cbInQue; + DWORD cbOutQue; + } FTCOMSTAT, *LPFTCOMSTAT; + + typedef struct _FTDCB { + DWORD DCBlength; /* sizeof(FTDCB) */ + DWORD BaudRate; /* Baudrate at which running */ + DWORD fBinary: 1; /* Binary Mode (skip EOF check) */ + DWORD fParity: 1; /* Enable parity checking */ + DWORD fOutxCtsFlow:1; /* CTS handshaking on output */ + DWORD fOutxDsrFlow:1; /* DSR handshaking on output */ + DWORD fDtrControl:2; /* DTR Flow control */ + DWORD fDsrSensitivity:1; /* DSR Sensitivity */ + DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */ + DWORD fOutX: 1; /* Enable output X-ON/X-OFF */ + DWORD fInX: 1; /* Enable input X-ON/X-OFF */ + DWORD fErrorChar: 1; /* Enable Err Replacement */ + DWORD fNull: 1; /* Enable Null stripping */ + DWORD fRtsControl:2; /* Rts Flow control */ + DWORD fAbortOnError:1; /* Abort all reads and writes on Error */ + DWORD fDummy2:17; /* Reserved */ + WORD wReserved; /* Not currently used */ + WORD XonLim; /* Transmit X-ON threshold */ + WORD XoffLim; /* Transmit X-OFF threshold */ + BYTE ByteSize; /* Number of bits/byte, 4-8 */ + BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ + BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 */ + char XonChar; /* Tx and Rx X-ON character */ + char XoffChar; /* Tx and Rx X-OFF character */ + char ErrorChar; /* Error replacement char */ + char EofChar; /* End of Input character */ + char EvtChar; /* Received Event character */ + WORD wReserved1; /* Fill for now. */ + } FTDCB, *LPFTDCB; + + typedef struct _FTTIMEOUTS { + DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ + DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ + DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ + } FTTIMEOUTS,*LPFTTIMEOUTS; + + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommError( + FT_HANDLE ftHandle, + LPDWORD lpdwErrors, + LPFTCOMSTAT lpftComstat + ); + + FTD2XX_API + BOOL WINAPI FT_W32_EscapeCommFunction( + FT_HANDLE ftHandle, + DWORD dwFunc + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommModemStatus( + FT_HANDLE ftHandle, + LPDWORD lpdwModemStatus + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_PurgeComm( + FT_HANDLE ftHandle, + DWORD dwMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommMask( + FT_HANDLE ftHandle, + ULONG ulEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommMask( + FT_HANDLE ftHandle, + LPDWORD lpdwEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetupComm( + FT_HANDLE ftHandle, + DWORD dwReadBufferSize, + DWORD dwWriteBufferSize + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WaitCommEvent( + FT_HANDLE ftHandle, + PULONG pulEvent, + LPOVERLAPPED lpOverlapped + ); + + + // + // Device information + // + + typedef struct _ft_device_list_info_node { + ULONG Flags; + ULONG Type; + ULONG ID; + DWORD LocId; + char SerialNumber[16]; + char Description[64]; + FT_HANDLE ftHandle; + } FT_DEVICE_LIST_INFO_NODE; + + // Device information flags + enum { + FT_FLAGS_OPENED = 1, + FT_FLAGS_HISPEED = 2 + }; + + + FTD2XX_API + FT_STATUS WINAPI FT_CreateDeviceInfoList( + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoList( + FT_DEVICE_LIST_INFO_NODE *pDest, + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoDetail( + DWORD dwIndex, + LPDWORD lpdwFlags, + LPDWORD lpdwType, + LPDWORD lpdwID, + LPDWORD lpdwLocId, + LPVOID lpSerialNumber, + LPVOID lpDescription, + FT_HANDLE *pftHandle + ); + + + // + // Version information + // + + FTD2XX_API + FT_STATUS WINAPI FT_GetDriverVersion( + FT_HANDLE ftHandle, + LPDWORD lpdwVersion + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLibraryVersion( + LPDWORD lpdwVersion + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_Rescan( + void + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Reload( + WORD wVid, + WORD wPid + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetComPortNumber( + FT_HANDLE ftHandle, + LPLONG lpdwComPortNumber + ); + + + // + // FT232H additional EEPROM functions + // + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + PUCHAR pucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_WriteConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + UCHAR ucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadECC( + FT_HANDLE ftHandle, + UCHAR ucOption, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatusEx( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTD2XX_H */ + diff --git a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/stdafx.h b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/stdafx.h index 47a0d025..b005a839 100644 --- a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/stdafx.h +++ b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/stdafx.h @@ -1,15 +1,15 @@ -// stdafx.h : include file for standard system include files, -// or project specific include files that are used frequently, but -// are changed infrequently -// - -#pragma once - -#include "targetver.h" - -#include -#include - - - -// TODO: reference additional headers your program requires here +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/targetver.h b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/targetver.h index 90e767bf..87c0086d 100644 --- a/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/targetver.h +++ b/scripts/BPTestApp/TestFT232/ft232testapp0/ft232testapp0/targetver.h @@ -1,8 +1,8 @@ -#pragma once - -// Including SDKDDKVer.h defines the highest available Windows platform. - -// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and -// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. - -#include +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include diff --git a/scripts/BPXSVFPlayer/buspirate.c b/scripts/BPXSVFPlayer/buspirate.c index ca1347b8..30b676e9 100644 --- a/scripts/BPXSVFPlayer/buspirate.c +++ b/scripts/BPXSVFPlayer/buspirate.c @@ -3,69 +3,69 @@ #include #include #include "serial.h" -#include "buspirate.h" - -#ifndef WIN32 +#include "buspirate.h" + +#ifndef WIN32 #define TRUE 1 #define FALSE 0 -#define Sleep(x) usleep(x); -#endif - -const char *modes[]={ - "BBIO", - "SPI", - "I2C", - "ART", - "1W0", - "RAW", -}; - -//static struct BP_t pBP; -//static uint8_t BP_reversebyte(uint8_t c); -//static char bpbuf[4096]; -//static int bpbufcnt; -extern int disable_comport; -extern int dumphandle; -extern int verbose; -extern int modem; -//extern char *modes[]; -//low lever send command, get reply function +#define Sleep(x) usleep(x); +#endif + +const char *modes[]={ + "BBIO", + "SPI", + "I2C", + "ART", + "1W0", + "RAW", +}; + +//static struct BP_t pBP; +//static uint8_t BP_reversebyte(uint8_t c); +//static char bpbuf[4096]; +//static int bpbufcnt; +extern int disable_comport; +extern int dumphandle; +extern int verbose; +extern int modem; +//extern char *modes[]; +//low lever send command, get reply function uint32_t BP_WriteToPirate(int fd, char * val) { int res = -1; - char ret = 0; - - + char ret = 0; + + serial_write(fd, val, 1); - Sleep(1); + Sleep(1); res = serial_read(fd, &ret, 1); - if( ret != '\x01') { - if (modem==TRUE){ - printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); - return 0; - } + if( ret != '\x01') { + if (modem==TRUE){ + printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); + return 0; + } else { printf(" ERROR: BusPirate replied with 0x%x instead of 0x01 \n",ret); - return -1; + return -1; } - } + } //printf(" BusPirate said: OK\n"); return 0; -} - +} + uint32_t BP_WriteToPirateNoCheck(int fd, char * val) { int res = -1; - char ret = 0; - - + char ret = 0; + + serial_write(fd, val, 1); - Sleep(1); + Sleep(1); res = serial_read(fd, &ret, 1); return 0; } - + int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not { int ret; @@ -75,53 +75,53 @@ int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not int tries=0; printf(" Entering binary mode...\n"); - - if (fd==-1) //added because the fd has already returned null - { - printf("Port does not exist!"); - ret=ERR; - + + if (fd==-1) //added because the fd has already returned null + { + printf("Port does not exist!"); + ret=ERR; + } - else - { - - + else + { + + while (!done) { - tmp[0]=0x00; + tmp[0]=0x00; //printf("Sending 0X%X to port\n",tmp[0]); serial_write(fd, tmp, 1); - tries++; + tries++; // printf("tries: %i Ret %i\n",tries,ret); usleep(1); ret = serial_read(fd, tmp, 5); - if (modem==TRUE) - { - printf("\n Modem Responded = %i\n",ret); - done=1; - } - else { + if (modem==TRUE) + { + printf("\n Modem Responded = %i\n",ret); + done=1; + } + else { if (ret != 5 && tries>20) { fprintf(stderr, " Buspirate did not respond correctly :( %i \n", ret ); ret=ERR; - } + } else if (strncmp(tmp, "BBIO1", 5) == 0) { done=1; - ret=BBIO; - } - } - - if (tries>25){ - printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); - break; - ret=ERR; + ret=BBIO; + } + } + + if (tries>25){ + printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); + break; + ret=ERR; } } - } + } return ret; -} - - - +} + + + int BP_EnableMode(int fd, char bbmode) { @@ -132,73 +132,73 @@ int BP_EnableMode(int fd, char bbmode) int tries=0; current_mode=(int)bbmode; printf(" Switching to %s mode\n",modes[current_mode]); - - tmp[0] = bbmode; + + tmp[0] = bbmode; //printf("Sending 0X%X to port\n",tmp[0]); serial_write(fd, tmp, 1); tries++; usleep(1); - ret = serial_read(fd, tmp, 4); - if (modem==TRUE) - { - printf(" Modem Responded = %i with value of ",ret); - if(ret>0){ - for(c=0; c0){ + for(c=0; c - - - +#include + + + #endif - + #define ERR -1 -#define BBIO 0x00 -#define SPI 0x01 +#define BBIO 0x00 +#define SPI 0x01 #define I2C 0x02 -#define ART 0x03 -#define OWD 0x04 -#define RAW 0X05 - - +#define ART 0x03 +#define OWD 0x04 +#define RAW 0X05 + + uint32_t BP_WriteToPirate(int , char *); -int BP_EnableBinary(int); -int BP_EnableMode(int , char ); -uint32_t BP_WriteToPirateNoCheck(int fd, char * val); +int BP_EnableBinary(int); +int BP_EnableMode(int , char ); +uint32_t BP_WriteToPirateNoCheck(int fd, char * val); diff --git a/scripts/BPXSVFPlayer/main.c b/scripts/BPXSVFPlayer/main.c index a6a34f9c..fc15315f 100644 --- a/scripts/BPXSVFPlayer/main.c +++ b/scripts/BPXSVFPlayer/main.c @@ -1,157 +1,157 @@ -#include -#include -#include -#include -#include -#include -#ifdef WIN32 -#include - #include - -#else -//#include -#endif - -#include "serial.h" -#include "buspirate.h" - - -#define JTAG_RESET 0x01 -#define JTAG_CHAIN_SCAN 0x02 -#define XSVF_PLAYER 0x03 - - -#define XSVF_ERROR_NONE 0x00 -#define XSVF_ERROR_UNKNOWN 0x01 -#define XSVF_ERROR_TDOMISMATCH 0x02 -#define XSVF_ERROR_MAXRETRIES 0x03 -#define XSVF_ERROR_ILLEGALCMD 0x04 -#define XSVF_ERROR_ILLEGALSTATE 0x05 -#define XSVF_ERROR_DATAOVERFLOW 0x06 -#define XSVF_ERROR_LAST 0x07 -#define XSVF_READY_FOR_DATA 0xFF - -#ifndef WIN32 -//#define usleep(x) Sleep(x); -#define Sleep(x) usleep(x); +#include +#include +#include +#include +#include +#include +#ifdef WIN32 +#include + #include + +#else +//#include +#endif + +#include "serial.h" +#include "buspirate.h" + + +#define JTAG_RESET 0x01 +#define JTAG_CHAIN_SCAN 0x02 +#define XSVF_PLAYER 0x03 + + +#define XSVF_ERROR_NONE 0x00 +#define XSVF_ERROR_UNKNOWN 0x01 +#define XSVF_ERROR_TDOMISMATCH 0x02 +#define XSVF_ERROR_MAXRETRIES 0x03 +#define XSVF_ERROR_ILLEGALCMD 0x04 +#define XSVF_ERROR_ILLEGALSTATE 0x05 +#define XSVF_ERROR_DATAOVERFLOW 0x06 +#define XSVF_ERROR_LAST 0x07 +#define XSVF_READY_FOR_DATA 0xFF + +#ifndef WIN32 +//#define usleep(x) Sleep(x); +#define Sleep(x) usleep(x); #define TRUE 1 #define FALSE 0 -#endif - -char *dumpfile; +#endif + +char *dumpfile; #ifdef WIN32 -HANDLE dumphandle; +HANDLE dumphandle; #endif -int modem =FALSE; -int cnt=0; -uint8_t *bin_buf; -uint32_t bin_buf_size; -#define FREE(x) if(x) free(x); -#define MAX_BUFFER 4096 //255 bytes - -//http://www.whereisian.com/files/j-xsvf_002.swf - -int print_usage(char * appname) -{ - //print usage - printf("\n"); - printf("\n"); - printf(" Help Menu\n"); - printf(" Usage: \n"); - printf(" %s -p device -f filename.xsvf -s speed [-x] [-r] \n ",appname); - printf("\n"); - printf(" Example Usage: %s -p COM1 -s 115200 -f example.xsvf \n",appname); - printf("\n"); - printf(" Where: -p device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf(" -f Filename of XSVF file \n"); - printf(" -x Perform a JTAG Chain Scan by sending 0x02 command. -f is optional. \n"); - printf(" -r Perform a JTAG Reset Scan by sending 0x01 command. -f is optional. \n"); - printf("\n"); - - printf("-----------------------------------------------------------------------------\n"); - - - return 0; -} - -int main(int argc, char** argv) -{ - int opt; - uint8_t buffer[MAX_BUFFER]={0}; - uint8_t temp[2]={0}; // command buffer -// struct stat stbuf; - int fd,timeout_counter; - int res,c, nparam_bytechunks, bytePointer, readSize; - long fileSize; +int modem =FALSE; +int cnt=0; +uint8_t *bin_buf; +uint32_t bin_buf_size; +#define FREE(x) if(x) free(x); +#define MAX_BUFFER 4096 //255 bytes + +//http://www.whereisian.com/files/j-xsvf_002.swf + +int print_usage(char * appname) +{ + //print usage + printf("\n"); + printf("\n"); + printf(" Help Menu\n"); + printf(" Usage: \n"); + printf(" %s -p device -f filename.xsvf -s speed [-x] [-r] \n ",appname); + printf("\n"); + printf(" Example Usage: %s -p COM1 -s 115200 -f example.xsvf \n",appname); + printf("\n"); + printf(" Where: -p device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf(" -f Filename of XSVF file \n"); + printf(" -x Perform a JTAG Chain Scan by sending 0x02 command. -f is optional. \n"); + printf(" -r Perform a JTAG Reset Scan by sending 0x01 command. -f is optional. \n"); + printf("\n"); + + printf("-----------------------------------------------------------------------------\n"); + + + return 0; +} + +int main(int argc, char** argv) +{ + int opt; + uint8_t buffer[MAX_BUFFER]={0}; + uint8_t temp[2]={0}; // command buffer +// struct stat stbuf; + int fd,timeout_counter; + int res,c, nparam_bytechunks, bytePointer, readSize; + long fileSize; FILE *XSVF; -// int xsvf; - int timer_out=0; - char *param_port = NULL; - char *param_speed = NULL; - char *param_XSVF=NULL; - char *param_bytechunks=NULL; - int jtag_reset=FALSE; - int chainscan=FALSE; - - const char *XSVF_ERROR[]={ "XSVF_ERROR_NONE", - "XSVF_ERROR_UNKNOWN", - "XSVF_ERROR_TDOMISMATCH", - "XSVF_ERROR_MAXRETRIES", - "XSVF_ERROR_ILLEGALCMD", - "XSVF_ERROR_ILLEGALSTATE", - "XSVF_ERROR_DATAOVERFLOW", - "XSVF_ERROR_LAST", - "XSVF_READY_FOR_DATA", - 0 }; - - printf("-----------------------------------------------------------------------------\n"); - printf("\n"); - printf(" BusPirate XSVF Player V.01\n"); - printf(" http://www.dangerousprototypes.com\n"); - printf("\n"); - printf("-----------------------------------------------------------------------------\n"); - - - - if (argc <= 1) { +// int xsvf; + int timer_out=0; + char *param_port = NULL; + char *param_speed = NULL; + char *param_XSVF=NULL; + char *param_bytechunks=NULL; + int jtag_reset=FALSE; + int chainscan=FALSE; + + const char *XSVF_ERROR[]={ "XSVF_ERROR_NONE", + "XSVF_ERROR_UNKNOWN", + "XSVF_ERROR_TDOMISMATCH", + "XSVF_ERROR_MAXRETRIES", + "XSVF_ERROR_ILLEGALCMD", + "XSVF_ERROR_ILLEGALSTATE", + "XSVF_ERROR_DATAOVERFLOW", + "XSVF_ERROR_LAST", + "XSVF_READY_FOR_DATA", + 0 }; + + printf("-----------------------------------------------------------------------------\n"); + printf("\n"); + printf(" BusPirate XSVF Player V.01\n"); + printf(" http://www.dangerousprototypes.com\n"); + printf("\n"); + printf("-----------------------------------------------------------------------------\n"); + + + + if (argc <= 1) { print_usage(argv[0]); exit(-1); - } - - - while ((opt = getopt(argc, argv, "s:p:f:rx")) != -1) { - - switch (opt) { - case 'p': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf("Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); + } + + + while ((opt = getopt(argc, argv, "s:p:f:rx")) != -1) { + + switch (opt) { + case 'p': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf("Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 'r': + jtag_reset=TRUE; break; - case 'r': - jtag_reset=TRUE; - break; - case 'x': - chainscan=TRUE; - break; - case 'f': - if (param_XSVF != NULL) { - printf(" No XSVF file \n"); - exit(-1); - } - param_XSVF = strdup(optarg); - - break; - - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); + case 'x': + chainscan=TRUE; + break; + case 'f': + if (param_XSVF != NULL) { + printf(" No XSVF file \n"); + exit(-1); + } + param_XSVF = strdup(optarg); + + break; + + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); break; @@ -161,211 +161,211 @@ int main(int argc, char** argv) exit(-1); break; } - } - - if (param_port==NULL){ + } + + if (param_port==NULL){ printf(" No serial port specified\n"); print_usage(argv[0]); - exit(-1); - } - + exit(-1); + } + nparam_bytechunks=MAX_BUFFER; - - if (param_speed==NULL) { - param_speed=strdup("115200"); //default is 115200kbps - } - - fd = serial_open(param_port); + if (param_speed==NULL) { + param_speed=strdup("115200"); //default is 115200kbps + } + + + fd = serial_open(param_port); if (fd < 0) { fprintf(stderr, " Error opening serial port\n"); return -1; - } - - //setup port and speed - serial_setup(fd,(speed_t) atoi(param_speed)); - - if (jtag_reset==TRUE){ - printf(" Performing Reset..\n"); - temp[0]=0x01; - serial_write( fd, (char *)temp, 1 ); - Sleep(1); - printf(" Done \n\n"); - } - - // perform Chain scan - // Send 0x02 to perform a chain scan - // Wait for 1 byte, the number of bytes that will be returned - // Get that many bytes - if (chainscan==TRUE) { - printf(" Performing Chain Scan..\n"); - temp[0]=0x02; - serial_write( fd, (char *)temp, 1 ); - Sleep(1); - res=0; - timeout_counter=0; - - while (res==0) { - res= serial_read(fd, (char *)buffer, sizeof(buffer)); - Sleep(1); - timeout_counter++; - if (timeout_counter>=5) { - printf(" Got no reply for a Chain scan\n"); - } else { - printf(" Waiting for a chain scan reply\n"); - } - } - if (res >0) { - printf(" Chain Scan Result:" ); - for(c=0;c0){ - printf("ok\n"); - // wait for 0xFF and send data, or error - if ((buffer[0]!=XSVF_READY_FOR_DATA) || (fileSize==0)) { - c=buffer[0]; - if (c==0xFF) - c=8; - printf(" End of operation reply: %s \n",XSVF_ERROR[c]); - switch (buffer[0]) { - case XSVF_ERROR_NONE : - printf(" Success!\n"); - break; - case XSVF_ERROR_UNKNOWN: - printf(" Unknown error: XSVF_ERROR_UNKNOWN \n"); - break; - case XSVF_ERROR_TDOMISMATCH: - printf(" Device did not respond as expected: XSVF_ERROR_TDOMISMATCH \n"); - break; - case XSVF_ERROR_MAXRETRIES: - printf(" Device did not respond: XSVF_ERROR_MAXRETRIES \n"); - break; - case XSVF_ERROR_ILLEGALCMD : - printf(" Unknown XSVF command: XSVF_ERROR_ILLEGALCMD \n"); - break; - case XSVF_ERROR_ILLEGALSTATE: - printf(" Unknown JTAG state: XSVF_ERROR_ILLEGALSTATE \n"); - break; - case XSVF_ERROR_DATAOVERFLOW : - printf(" Error, data overflow: XSVF_ERROR_DATAOVERFLOW \n"); - break; - case XSVF_ERROR_LAST: - printf(" Some other error I don't remember, probably isn't active: XSVF_ERROR_LAST \n"); - break; - case XSVF_READY_FOR_DATA: - if (fileSize==0) { - printf(" End of file reached. \n"); - } else { - printf(" Programmer says more data: XSVF_READY_FOR_DATA \n"); - } - break; - default: - printf(" Unknown error\n "); - - } - - } - break; //break loop and send data - }else{ - printf("\n Waiting for reply..."); - // Sleep(1); - timeout_counter++; - if(timeout_counter > 4){ - printf("\n No reply.... Quitting.\n "); - timeout_counter=0; - timer_out=-1; - break; - } - } - } - if (fileSize==0) { - break; - } - - if (timer_out==-1) - break; - //send data - if(fileSize>8); - temp[1]=readSize; - cnt=cnt+readSize; - - printf(" Sending %i Bytes (%04X)...",readSize, cnt); - serial_write( fd, (char *)temp,2 ); - serial_write( fd, (char *) &bin_buf[bytePointer],readSize ); - bytePointer=bytePointer+readSize;//start 1 chunk in next itme - fileSize=fileSize-readSize; //deincrement the remaining byte count - - } - - printf(" Thank you for playing! :-)\n\n"); + } + + //setup port and speed + serial_setup(fd,(speed_t) atoi(param_speed)); + + if (jtag_reset==TRUE){ + printf(" Performing Reset..\n"); + temp[0]=0x01; + serial_write( fd, (char *)temp, 1 ); + Sleep(1); + printf(" Done \n\n"); + } + + // perform Chain scan + // Send 0x02 to perform a chain scan + // Wait for 1 byte, the number of bytes that will be returned + // Get that many bytes + if (chainscan==TRUE) { + printf(" Performing Chain Scan..\n"); + temp[0]=0x02; + serial_write( fd, (char *)temp, 1 ); + Sleep(1); + res=0; + timeout_counter=0; + + while (res==0) { + res= serial_read(fd, (char *)buffer, sizeof(buffer)); + Sleep(1); + timeout_counter++; + if (timeout_counter>=5) { + printf(" Got no reply for a Chain scan\n"); + } else { + printf(" Waiting for a chain scan reply\n"); + } + } + if (res >0) { + printf(" Chain Scan Result:" ); + for(c=0;c0){ + printf("ok\n"); + // wait for 0xFF and send data, or error + if ((buffer[0]!=XSVF_READY_FOR_DATA) || (fileSize==0)) { + c=buffer[0]; + if (c==0xFF) + c=8; + printf(" End of operation reply: %s \n",XSVF_ERROR[c]); + switch (buffer[0]) { + case XSVF_ERROR_NONE : + printf(" Success!\n"); + break; + case XSVF_ERROR_UNKNOWN: + printf(" Unknown error: XSVF_ERROR_UNKNOWN \n"); + break; + case XSVF_ERROR_TDOMISMATCH: + printf(" Device did not respond as expected: XSVF_ERROR_TDOMISMATCH \n"); + break; + case XSVF_ERROR_MAXRETRIES: + printf(" Device did not respond: XSVF_ERROR_MAXRETRIES \n"); + break; + case XSVF_ERROR_ILLEGALCMD : + printf(" Unknown XSVF command: XSVF_ERROR_ILLEGALCMD \n"); + break; + case XSVF_ERROR_ILLEGALSTATE: + printf(" Unknown JTAG state: XSVF_ERROR_ILLEGALSTATE \n"); + break; + case XSVF_ERROR_DATAOVERFLOW : + printf(" Error, data overflow: XSVF_ERROR_DATAOVERFLOW \n"); + break; + case XSVF_ERROR_LAST: + printf(" Some other error I don't remember, probably isn't active: XSVF_ERROR_LAST \n"); + break; + case XSVF_READY_FOR_DATA: + if (fileSize==0) { + printf(" End of file reached. \n"); + } else { + printf(" Programmer says more data: XSVF_READY_FOR_DATA \n"); + } + break; + default: + printf(" Unknown error\n "); + + } + + } + break; //break loop and send data + }else{ + printf("\n Waiting for reply..."); + // Sleep(1); + timeout_counter++; + if(timeout_counter > 4){ + printf("\n No reply.... Quitting.\n "); + timeout_counter=0; + timer_out=-1; + break; + } + } + } + if (fileSize==0) { + break; + } + + if (timer_out==-1) + break; + //send data + if(fileSize>8); + temp[1]=readSize; + cnt=cnt+readSize; + + printf(" Sending %i Bytes (%04X)...",readSize, cnt); + serial_write( fd, (char *)temp,2 ); + serial_write( fd, (char *) &bin_buf[bytePointer],readSize ); + bytePointer=bytePointer+readSize;//start 1 chunk in next itme + fileSize=fileSize-readSize; //deincrement the remaining byte count + + } + + printf(" Thank you for playing! :-)\n\n"); #ifdef WIN32 fclose(XSVF); FREE(param_port); - FREE(param_speed); - FREE(param_bytechunks); - FREE(param_XSVF); - FREE( bin_buf); + FREE(param_speed); + FREE(param_bytechunks); + FREE(param_XSVF); + FREE( bin_buf); #endif - return 0; - } //end main() + return 0; + } //end main() diff --git a/scripts/BPXSVFPlayer/serial.c b/scripts/BPXSVFPlayer/serial.c index dfbaa379..967aebb4 100644 --- a/scripts/BPXSVFPlayer/serial.c +++ b/scripts/BPXSVFPlayer/serial.c @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * @@ -27,15 +27,15 @@ #include -#include "serial.h" -extern int disable_comport; +#include "serial.h" +extern int disable_comport; extern char *dumpfile; #ifdef WIN32 -extern HANDLE dumphandle; +extern HANDLE dumphandle; #endif -#ifndef WIN32 -//#define usleep(x) Sleep(x); -#define Sleep(x) usleep(x); +#ifndef WIN32 +//#define usleep(x) Sleep(x); +#define Sleep(x) usleep(x); #endif /* #ifdef WIN32 @@ -194,15 +194,15 @@ int serial_write(int fd, char *buf, int size) unsigned long bwritten = 0; - res = WriteFile(hCom, buf, size, &bwritten, NULL); - + res = WriteFile(hCom, buf, size, &bwritten, NULL); + //printf("Serial: Written %i bytes: ",size); - //int i; + //int i; //for (i = 0; i < size; i++){ //printf("%02X ", buf[i]); - //} - //printf("\n"); - + //} + //printf("\n"); + if( res == FALSE ) { ret = -1; } else { @@ -214,20 +214,20 @@ int serial_write(int fd, char *buf, int size) //fprintf(stderr, "size = %d ret = %d\n", size, ret); //buspirate_print_buffer(buf, size); - + if (ret != size) fprintf(stderr, "Error sending data"); - return ret; - + return ret; + } int serial_read(int fd, char *buf, int size) { int len = 0; - int ret = 0; + int ret = 0; #ifndef WIN32 - int timeout = 0; + int timeout = 0; #endif #ifdef WIN32 HANDLE hCom = (HANDLE)fd; @@ -264,7 +264,7 @@ int serial_read(int fd, char *buf, int size) //printf("should have read = %i actual size = %i \n", size, len); //fprintf(stderr, "should have read = %d actual size = %d \n", size, len); //buspirate_print_buffer(buf, len); - + return len; } @@ -287,15 +287,15 @@ int serial_open(char *port) fd = -1; } else { fd = (int)hCom; - } - - //added to create a dumpfile - if (dumpfile !=NULL) { + } + + //added to create a dumpfile + if (dumpfile !=NULL) { dumphandle = CreateFileA(dumpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if( !dumphandle || dumphandle == INVALID_HANDLE_VALUE ) { - printf("Invalid dump file: %s\n",dumpfile); - return -1; - } + printf("Invalid dump file: %s\n",dumpfile); + return -1; + } } #else fd = open(port, O_RDWR | O_NOCTTY); @@ -312,13 +312,13 @@ int serial_close(int fd) #ifdef WIN32 HANDLE hCom = (HANDLE)fd; - CloseHandle(hCom); - - //close dumpfile if it was created - if (dumpfile !=NULL) { - if (dumphandle != NULL) { - CloseHandle(dumphandle); - } + CloseHandle(hCom); + + //close dumpfile if it was created + if (dumpfile !=NULL) { + if (dumphandle != NULL) { + CloseHandle(dumphandle); + } } #else close(fd); diff --git a/scripts/BPXSVFPlayer/serial.h b/scripts/BPXSVFPlayer/serial.h index 9dc8a545..a7293021 100644 --- a/scripts/BPXSVFPlayer/serial.h +++ b/scripts/BPXSVFPlayer/serial.h @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * diff --git a/scripts/HVPselftest/main.c b/scripts/HVPselftest/main.c index e8dec629..e67b8bdb 100644 --- a/scripts/HVPselftest/main.c +++ b/scripts/HVPselftest/main.c @@ -1,307 +1,307 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - * Reference on the wiki: - * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter - * http://dangerousprototypes.com/docs/Bitbang - * - * video instruction : http://www.whereisian.com/files/j-hvp-self-test.swf - * - * Forum links : http://dangerousprototypes.com/forum/index.php?topic=1046.0 - */ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) -{ - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -p device \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 -R \n",appname); - printf("\n"); - printf(" Where: -p device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; -} - - - -int main(int argc, char** argv) -{ - int opt; - char buffer[256] = {0}, ADCraw[2]={0}; - int fd; - int res,c,adc; - float voltage ; - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate HVP Adapter SELF TEST utility v0.1 (CC-0)\n"); - printf(" http://www.dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - - if (argc <= 1){ - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - - while ((opt = getopt(argc, argv, "ms:p:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'p': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - flag=0; - // - // Loop and repeat test as needed for manufacturing - // - - printf(" Press Esc to exit, any other key to start the self-test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1){ - printf("\n--------------------- Starting a new Test-------------------------\n"); - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - } - else {//make space only - printf("\n Starting test! \n"); - break; - } - } - } - - - -// -// Open serial port -// - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - //Set Speed of port - serial_setup(fd,(speed_t) param_speed); - - if (modem==TRUE){ // connected to modem for testing response { - - serial_write( fd, "ATI7\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - - } - else{ - printf(" Configuring Bus Pirate HVP Adapter...\n"); - //go into binary bitbang mode - printf(" Going into Binary Bitbang mode.."); - if(BP_EnableBinary(fd)!=BBIO){ - printf(" Buspirate cannot switch to binary bitbang mode :( \n"); - return -1; - } - printf("ok\n"); - // - //Start self-test - // 010xxxxx - Configure pins as input(1) or output(0): AUX|MOSI|CLK|MISO|CS - // should configured as output 01000000 = 0x40 - - printf(" configuring pins as output...\n"); - printf(" sending 01000000..."); - BP_WriteToPirate(fd,"\x40"); - printf("OK\n"); - //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS - //11000000 - on : poweronly = 0xC0 - printf(" Sending Command to power on : 11000000...."); - BP_WriteToPirate(fd,"\xC0"); - printf("OK\n"); - } - - // - // Done with setup ,start with testing - // 00010100 - Take voltage probe measurement (returns 2 bytes) - // 00010100 = 0x14 - - printf(" Voltage Probe measurement...,sending 00010100 \n"); - serial_write( fd, "\x14",1 ); - res=0; - while(res==0){ - Sleep(1); //how long wait for a reply? - res = serial_read(fd, ADCraw, 2); - } - //for debugging without hardware using only modem - if (modem==TRUE){ - res=2; - ADCraw[0]=0x4D; - ADCraw[1]=0x9C; - } - // comment the above if with hardware - if (res!=2){ - printf(" Expecting 2 bytes returned but got %i instead\n",res); - printf(" Voltage Measurement: !!!!FAIL!!!! \n"); - } - else - { - // Take a measurement from the Bus Pirate voltage probe. - // Returns a 2 byte ADC reading, high 8bits come first. - // To determine the actual voltage measurement: - // (ADC/1024)*3.3volts/(49/10) - adc = ADCraw[0] << 8; - adc += ADCraw[1]; - voltage = (adc/1024.0) * 3.3/(49.0/10.0); - - printf(" ADC Reading: %2.0f Volts\n",voltage); - if(voltage > 12.0 ){ - printf(" Voltage Measurement: ****PASS**** \n"); - }else{ - printf(" Voltage Measurement: !!!!FAIL!!!! \n"); - } - - } - //power off - //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS - //10000000 - off : power only = 0x80 - printf( " powering off.....\n"); - BP_WriteToPirate(fd,"\x80"); - - // pause after the result - - if (firsttime==0){ // run here once and don't say again the next time - printf(" Press any key to continue...\n"); - firsttime=1; - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - break; - } - } - } - - res = serial_write( fd, "\x0F", 1); //exit BBIO - - //close port so they can attach the next Bus Pirate - serial_close(fd); - - - //TODO: Loop back to pause after this - printf("\n Connect another Bus Pirate HVP adapter and press any key to start the self-test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - - #define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + * Reference on the wiki: + * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter + * http://dangerousprototypes.com/docs/Bitbang + * + * video instruction : http://www.whereisian.com/files/j-hvp-self-test.swf + * + * Forum links : http://dangerousprototypes.com/forum/index.php?topic=1046.0 + */ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) +{ + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -p device \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 -R \n",appname); + printf("\n"); + printf(" Where: -p device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; +} + + + +int main(int argc, char** argv) +{ + int opt; + char buffer[256] = {0}, ADCraw[2]={0}; + int fd; + int res,c,adc; + float voltage ; + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate HVP Adapter SELF TEST utility v0.1 (CC-0)\n"); + printf(" http://www.dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + + if (argc <= 1){ + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + + while ((opt = getopt(argc, argv, "ms:p:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'p': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + flag=0; + // + // Loop and repeat test as needed for manufacturing + // + + printf(" Press Esc to exit, any other key to start the self-test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1){ + printf("\n--------------------- Starting a new Test-------------------------\n"); + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + } + else {//make space only + printf("\n Starting test! \n"); + break; + } + } + } + + + +// +// Open serial port +// + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + //Set Speed of port + serial_setup(fd,(speed_t) param_speed); + + if (modem==TRUE){ // connected to modem for testing response { + + serial_write( fd, "ATI7\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + + } + else{ + printf(" Configuring Bus Pirate HVP Adapter...\n"); + //go into binary bitbang mode + printf(" Going into Binary Bitbang mode.."); + if(BP_EnableBinary(fd)!=BBIO){ + printf(" Buspirate cannot switch to binary bitbang mode :( \n"); + return -1; + } + printf("ok\n"); + // + //Start self-test + // 010xxxxx - Configure pins as input(1) or output(0): AUX|MOSI|CLK|MISO|CS + // should configured as output 01000000 = 0x40 + + printf(" configuring pins as output...\n"); + printf(" sending 01000000..."); + BP_WriteToPirate(fd,"\x40"); + printf("OK\n"); + //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS + //11000000 - on : poweronly = 0xC0 + printf(" Sending Command to power on : 11000000...."); + BP_WriteToPirate(fd,"\xC0"); + printf("OK\n"); + } + + // + // Done with setup ,start with testing + // 00010100 - Take voltage probe measurement (returns 2 bytes) + // 00010100 = 0x14 + + printf(" Voltage Probe measurement...,sending 00010100 \n"); + serial_write( fd, "\x14",1 ); + res=0; + while(res==0){ + Sleep(1); //how long wait for a reply? + res = serial_read(fd, ADCraw, 2); + } + //for debugging without hardware using only modem + if (modem==TRUE){ + res=2; + ADCraw[0]=0x4D; + ADCraw[1]=0x9C; + } + // comment the above if with hardware + if (res!=2){ + printf(" Expecting 2 bytes returned but got %i instead\n",res); + printf(" Voltage Measurement: !!!!FAIL!!!! \n"); + } + else + { + // Take a measurement from the Bus Pirate voltage probe. + // Returns a 2 byte ADC reading, high 8bits come first. + // To determine the actual voltage measurement: + // (ADC/1024)*3.3volts/(49/10) + adc = ADCraw[0] << 8; + adc += ADCraw[1]; + voltage = (adc/1024.0) * 3.3/(49.0/10.0); + + printf(" ADC Reading: %2.0f Volts\n",voltage); + if(voltage > 12.0 ){ + printf(" Voltage Measurement: ****PASS**** \n"); + }else{ + printf(" Voltage Measurement: !!!!FAIL!!!! \n"); + } + + } + //power off + //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS + //10000000 - off : power only = 0x80 + printf( " powering off.....\n"); + BP_WriteToPirate(fd,"\x80"); + + // pause after the result + + if (firsttime==0){ // run here once and don't say again the next time + printf(" Press any key to continue...\n"); + firsttime=1; + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + break; + } + } + } + + res = serial_write( fd, "\x0F", 1); //exit BBIO + + //close port so they can attach the next Bus Pirate + serial_close(fd); + + + //TODO: Loop back to pause after this + printf("\n Connect another Bus Pirate HVP adapter and press any key to start the self-test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + + #define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} diff --git a/scripts/I2CEEPROMWIN.c b/scripts/I2CEEPROMWIN.c index 590bc290..f842c804 100644 --- a/scripts/I2CEEPROMWIN.c +++ b/scripts/I2CEEPROMWIN.c @@ -1,139 +1,139 @@ -/* Author: James Stephenson - * This program runs under windows and uses the bus pirate to read/write a file in - * ascii hex to/from an I2C eeprom such as the 24FC64, note the address used is 2 bytes. - * Page write isn't used since chips don't have a standard write buffer size. - * Released under the WTF license. - * - * Hex file should contain space seperated hex values and no line breaks, ie: - * 0xFF 0xAA 0x BB - */ - -#include -#include -#include -#include - -int main(int argc, char **argv){ - FILE *inoutpfile; - DCB my_dcb; - HANDLE hComm; - int bytes_written, bytes_received, retval, memaddr = 0, loop, failcount, datalen = 0; - unsigned char receive_buf[10], write_buf[9], curcode[5], curbincode, chipaddr, flags = 0; - - if(argc < 4 || argc > 7){ - printf("pc_serial [com?] [chip addr 0-8] [R/W] [file] (read len) (mem addr)\n"); - return 1; - } - - if(argv[2][0] > '8' || argv[2][0] < '0'){ - printf("Chip address out of range\n"); - return 2; - } - chipaddr = 0xA0 | ((argv[2][0] - '0') << 1); - - if(argc >= 6){ - if(!sscanf(argv[5], "%d", &datalen)){ - printf("Data length invalid\n"); - return 3; - } - } - - if(argc == 7){ - if(!sscanf(argv[6], "%d", &memaddr)){ - printf("Memory address invalid\n"); - return 3; - } - } - - if(argv[3][0] != 'R' && argv[3][0] != 'W'){ - printf("Must specify read or write mode\n"); - return 4; - } - - if(argv[3][0] == 'R' && !datalen){ - printf("Must specify read length\n"); - return 4; - } - - if(argv[3][0] == 'R') - inoutpfile = fopen(argv[4], "wb"); - else - inoutpfile = fopen(argv[4], "rb"); - if(!inoutpfile){ - printf("Error opening file: %s\n", argv[4]); - return 2; - } - - hComm = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if(hComm == INVALID_HANDLE_VALUE){ - printf("Error Opening Serial Port\n"); - return 3; - } - - my_dcb.DCBlength = sizeof(my_dcb); - GetCommState(hComm, &my_dcb); - my_dcb.BaudRate = CBR_115200; - my_dcb.ByteSize = 8; - my_dcb.StopBits = ONESTOPBIT; - my_dcb.Parity = NOPARITY; - - if(!SetCommState(hComm, &my_dcb)){ - printf("Error setting up serial port\n"); - return 4; - } - - WriteFile(hComm, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02\x4C", 22, &bytes_written, 0); - ReadFile(hComm, receive_buf, 10, &bytes_received, 0); - receive_buf[8] = 0; - if(memcmp(receive_buf, "BBIO1I2C", 8)){ - printf("Error setting up RAW I2C mode: %s\n", receive_buf); - return 5; - } - sleep(500); - - if(argv[3][0] == 'R'){ - memcpy(write_buf, "\x02\x12\xAA\xBB\xCC\x03", 6); - write_buf[2] = chipaddr; - write_buf[3] = memaddr >> 8; - write_buf[4] = memaddr & 0xFF; - WriteFile(hComm, write_buf, 6, &bytes_written, 0); - ReadFile(hComm, receive_buf, 6, &bytes_received, 0); - memcpy(write_buf, "\x02\x10\xAA", 3); - write_buf[2] = chipaddr | 1; - WriteFile(hComm, write_buf, 3, &bytes_written, 0); - ReadFile(hComm, receive_buf, 3, &bytes_received, 0); - for(; datalen; datalen--){ - WriteFile(hComm, "\x04\x06", 2, &bytes_written, 0); - ReadFile(hComm, receive_buf, 2, &bytes_received, 0); - printf("%02X ", receive_buf[0]); - fprintf(inoutpfile, "%02X ", receive_buf[0]); - } - WriteFile(hComm, "\x04\x07\x03\x00\x0F", 5, &bytes_written, 0); - }else{ - failcount = 0; - memcpy(write_buf, "\x02\x13\xAA\xBB\xCC\xDD\x03", 7); - write_buf[2] = chipaddr; - while(fread(curcode, 5, 1, inoutpfile)){ - if(!sscanf(curcode, "%X", &curbincode)){ - printf("Error processing code in file: %s", curcode); - return 6; - } - write_buf[3] = memaddr >> 8; - write_buf[4] = memaddr & 0xFF; - write_buf[5] = curbincode; - do{ - failcount++; - WriteFile(hComm, write_buf, 7, &bytes_written, 0); - ReadFile(hComm, receive_buf, 7, &bytes_received, 0); - }while(receive_buf[2] || receive_buf[3] || receive_buf[4] || receive_buf[5]); - printf("%02X ", curbincode); - failcount--; - memaddr++; - } - printf("\n%d bytes written with %d retries\n", memaddr+1, failcount); - WriteFile(hComm, "\x00\x0F", 2, &bytes_written, 0); - } - - fclose(inoutpfile); - return 0; -} +/* Author: James Stephenson + * This program runs under windows and uses the bus pirate to read/write a file in + * ascii hex to/from an I2C eeprom such as the 24FC64, note the address used is 2 bytes. + * Page write isn't used since chips don't have a standard write buffer size. + * Released under the WTF license. + * + * Hex file should contain space seperated hex values and no line breaks, ie: + * 0xFF 0xAA 0x BB + */ + +#include +#include +#include +#include + +int main(int argc, char **argv){ + FILE *inoutpfile; + DCB my_dcb; + HANDLE hComm; + int bytes_written, bytes_received, retval, memaddr = 0, loop, failcount, datalen = 0; + unsigned char receive_buf[10], write_buf[9], curcode[5], curbincode, chipaddr, flags = 0; + + if(argc < 4 || argc > 7){ + printf("pc_serial [com?] [chip addr 0-8] [R/W] [file] (read len) (mem addr)\n"); + return 1; + } + + if(argv[2][0] > '8' || argv[2][0] < '0'){ + printf("Chip address out of range\n"); + return 2; + } + chipaddr = 0xA0 | ((argv[2][0] - '0') << 1); + + if(argc >= 6){ + if(!sscanf(argv[5], "%d", &datalen)){ + printf("Data length invalid\n"); + return 3; + } + } + + if(argc == 7){ + if(!sscanf(argv[6], "%d", &memaddr)){ + printf("Memory address invalid\n"); + return 3; + } + } + + if(argv[3][0] != 'R' && argv[3][0] != 'W'){ + printf("Must specify read or write mode\n"); + return 4; + } + + if(argv[3][0] == 'R' && !datalen){ + printf("Must specify read length\n"); + return 4; + } + + if(argv[3][0] == 'R') + inoutpfile = fopen(argv[4], "wb"); + else + inoutpfile = fopen(argv[4], "rb"); + if(!inoutpfile){ + printf("Error opening file: %s\n", argv[4]); + return 2; + } + + hComm = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + if(hComm == INVALID_HANDLE_VALUE){ + printf("Error Opening Serial Port\n"); + return 3; + } + + my_dcb.DCBlength = sizeof(my_dcb); + GetCommState(hComm, &my_dcb); + my_dcb.BaudRate = CBR_115200; + my_dcb.ByteSize = 8; + my_dcb.StopBits = ONESTOPBIT; + my_dcb.Parity = NOPARITY; + + if(!SetCommState(hComm, &my_dcb)){ + printf("Error setting up serial port\n"); + return 4; + } + + WriteFile(hComm, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02\x4C", 22, &bytes_written, 0); + ReadFile(hComm, receive_buf, 10, &bytes_received, 0); + receive_buf[8] = 0; + if(memcmp(receive_buf, "BBIO1I2C", 8)){ + printf("Error setting up RAW I2C mode: %s\n", receive_buf); + return 5; + } + sleep(500); + + if(argv[3][0] == 'R'){ + memcpy(write_buf, "\x02\x12\xAA\xBB\xCC\x03", 6); + write_buf[2] = chipaddr; + write_buf[3] = memaddr >> 8; + write_buf[4] = memaddr & 0xFF; + WriteFile(hComm, write_buf, 6, &bytes_written, 0); + ReadFile(hComm, receive_buf, 6, &bytes_received, 0); + memcpy(write_buf, "\x02\x10\xAA", 3); + write_buf[2] = chipaddr | 1; + WriteFile(hComm, write_buf, 3, &bytes_written, 0); + ReadFile(hComm, receive_buf, 3, &bytes_received, 0); + for(; datalen; datalen--){ + WriteFile(hComm, "\x04\x06", 2, &bytes_written, 0); + ReadFile(hComm, receive_buf, 2, &bytes_received, 0); + printf("%02X ", receive_buf[0]); + fprintf(inoutpfile, "%02X ", receive_buf[0]); + } + WriteFile(hComm, "\x04\x07\x03\x00\x0F", 5, &bytes_written, 0); + }else{ + failcount = 0; + memcpy(write_buf, "\x02\x13\xAA\xBB\xCC\xDD\x03", 7); + write_buf[2] = chipaddr; + while(fread(curcode, 5, 1, inoutpfile)){ + if(!sscanf(curcode, "%X", &curbincode)){ + printf("Error processing code in file: %s", curcode); + return 6; + } + write_buf[3] = memaddr >> 8; + write_buf[4] = memaddr & 0xFF; + write_buf[5] = curbincode; + do{ + failcount++; + WriteFile(hComm, write_buf, 7, &bytes_written, 0); + ReadFile(hComm, receive_buf, 7, &bytes_received, 0); + }while(receive_buf[2] || receive_buf[3] || receive_buf[4] || receive_buf[5]); + printf("%02X ", curbincode); + failcount--; + memaddr++; + } + printf("\n%d bytes written with %d retries\n", memaddr+1, failcount); + WriteFile(hComm, "\x00\x0F", 2, &bytes_written, 0); + } + + fclose(inoutpfile); + return 0; +} diff --git a/scripts/powertools/BP_MMA_Selftest/main.c b/scripts/powertools/BP_MMA_Selftest/main.c index 4b5a6c09..392ec1e5 100644 --- a/scripts/powertools/BP_MMA_Selftest/main.c +++ b/scripts/powertools/BP_MMA_Selftest/main.c @@ -1,325 +1,325 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - * Reference on the wiki: - * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter - * http://dangerousprototypes.com/docs/Bitbang - * - * video instruction : http://www.whereisian.com/files/j-hvp-self-test.swf - * - * Forum links : http://dangerousprototypes.com/forum/index.php?topic=1046.0 - */ - -// under codebase, You always need to add the other files (includes) to compile properly -// -/* -Doc ref: http://dangerousprototypes.com/docs/MMA7455L_breakout_board_manufacturing_tests#Notes -The automated test app uses the Bus Pirate to test the connection to the MMA7455L: -Enter BBIO mode -Enter SPI mode -Enable power, normal pin outputs -Set CS high -Set CS low -Send 0x1A -Read one byte -Set CS high -Verify read=0x1D -Exit SPI mode (to disable power and pins) -Repeat from 2 -*/ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) -{ - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -p device \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 -R \n",appname); - printf("\n"); - printf(" Where: -p device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; -} - - - -int main(int argc, char** argv) -{ - int opt; - char buffer[256] = {0}; - int fd; - int res,c; - - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - char ret,i; - - printf("-------------------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate-MMA7455L breakout board manufacturing SELF TEST utility v0.1 (CC-0)\n"); - printf(" http://www.dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------------------------------------\n"); - - if (argc <= 1){ - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - - while ((opt = getopt(argc, argv, "ms:p:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'p': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - flag=0; - // - // Loop and repeat test as needed for manufacturing - // - - printf(" Press Esc to exit, any other key to start the self-test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1){ - printf("\n--------------------- Starting a new Test-------------------------\n"); - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - } - else {//make space only - printf("\n Starting test! \n"); - break; - } - } - } - - - -// -// Open serial port -// - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - //Set Speed of port - serial_setup(fd,(speed_t) param_speed); - - printf(" Configuring Bus Pirate ....\n"); - - //go into binary bitbang mode - printf(" Going into Binary Bitbang mode..\n"); - if(BP_EnableBinary(fd)!=BBIO){ - printf(" Buspirate cannot switch to binary bitbang mode :( \n"); - return -1; - } - else - printf(" ..ok\n\n"); - - printf(" Going into SPI mode.."); - - if(BP_EnableMode(fd, SPI)!=SPI){ - printf(" Buspirate cannot switch to SPI mode :( \n"); - return -1; - } - else - printf(" ..ok\n\n"); - //0100wxyz - Configure peripherals w=power, x=pull-ups, y=AUX, z=CS - i=0x40; - i|=0x08;//power on - i|=0x01; //CS high - printf(" Power on...\n"); - BP_WriteToPirate(fd,&i); - - //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample - // 10001110 - // 3. Enable power, normal pin outputs - i=0x80; - i|=0x02; - //i|=0x04; - // w=hiZ/3.3v - i|=0x08; - printf(" Enabling power, normal pin outputs..\n"); +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + * Reference on the wiki: + * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter + * http://dangerousprototypes.com/docs/Bitbang + * + * video instruction : http://www.whereisian.com/files/j-hvp-self-test.swf + * + * Forum links : http://dangerousprototypes.com/forum/index.php?topic=1046.0 + */ + +// under codebase, You always need to add the other files (includes) to compile properly +// +/* +Doc ref: http://dangerousprototypes.com/docs/MMA7455L_breakout_board_manufacturing_tests#Notes +The automated test app uses the Bus Pirate to test the connection to the MMA7455L: +Enter BBIO mode +Enter SPI mode +Enable power, normal pin outputs +Set CS high +Set CS low +Send 0x1A +Read one byte +Set CS high +Verify read=0x1D +Exit SPI mode (to disable power and pins) +Repeat from 2 +*/ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) +{ + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -p device \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 -R \n",appname); + printf("\n"); + printf(" Where: -p device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; +} + + + +int main(int argc, char** argv) +{ + int opt; + char buffer[256] = {0}; + int fd; + int res,c; + + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + char ret,i; + + printf("-------------------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate-MMA7455L breakout board manufacturing SELF TEST utility v0.1 (CC-0)\n"); + printf(" http://www.dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------------------------------------\n"); + + if (argc <= 1){ + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + + while ((opt = getopt(argc, argv, "ms:p:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'p': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + flag=0; + // + // Loop and repeat test as needed for manufacturing + // + + printf(" Press Esc to exit, any other key to start the self-test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1){ + printf("\n--------------------- Starting a new Test-------------------------\n"); + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + } + else {//make space only + printf("\n Starting test! \n"); + break; + } + } + } + + + +// +// Open serial port +// + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + //Set Speed of port + serial_setup(fd,(speed_t) param_speed); + + printf(" Configuring Bus Pirate ....\n"); + + //go into binary bitbang mode + printf(" Going into Binary Bitbang mode..\n"); + if(BP_EnableBinary(fd)!=BBIO){ + printf(" Buspirate cannot switch to binary bitbang mode :( \n"); + return -1; + } + else + printf(" ..ok\n\n"); + + printf(" Going into SPI mode.."); + + if(BP_EnableMode(fd, SPI)!=SPI){ + printf(" Buspirate cannot switch to SPI mode :( \n"); + return -1; + } + else + printf(" ..ok\n\n"); + //0100wxyz - Configure peripherals w=power, x=pull-ups, y=AUX, z=CS + i=0x40; + i|=0x08;//power on + i|=0x01; //CS high + printf(" Power on...\n"); BP_WriteToPirate(fd,&i); - - // - //Start self-test - - // Set CS high - i=0x03; - printf(" CS High : 0x03....\n"); - BP_WriteToPirate(fd,&i); - - // Set CS low - i=0x02; - printf(" CS Low : 0x02....\n\n"); - BP_WriteToPirate(fd,&i); - - // Send 0x1A - printf(" Sending 0x1A....\n"); - buffer[0]=0x11;//number of bytes - buffer[1]=0x1a; //command to MMA7455 - buffer[2]=0xff; //read one byte + + //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample + // 10001110 + // 3. Enable power, normal pin outputs + i=0x80; + i|=0x02; + //i|=0x04; + // w=hiZ/3.3v + i|=0x08; + printf(" Enabling power, normal pin outputs..\n"); + BP_WriteToPirate(fd,&i); + + // + //Start self-test + + // Set CS high + i=0x03; + printf(" CS High : 0x03....\n"); + BP_WriteToPirate(fd,&i); + + // Set CS low + i=0x02; + printf(" CS Low : 0x02....\n\n"); + BP_WriteToPirate(fd,&i); + + // Send 0x1A + printf(" Sending 0x1A....\n"); + buffer[0]=0x11;//number of bytes + buffer[1]=0x1a; //command to MMA7455 + buffer[2]=0xff; //read one byte serial_write(fd, buffer, 3); - Sleep(1); - // Read one byte - res = serial_read(fd, buffer, 3); - if (res!=0) { - printf(" Got reply: "); - printf("0X%02X ",buffer[2]); - printf(" \n\n"); - if (buffer[2] !=0x1D) - printf(" Self test status with MMA7455L breakout board: !!!! FAILED! !!!! \n\n"); - else - printf(" Self test status with MMA7455L breakout board: **** PASS! *****\n\n"); - }else{ - printf("FAIL! Got No Reply\n"); - } - - // Set CS high - i=0x03; - printf(" CS High : 0x03....\n"); - BP_WriteToPirate(fd,&i); - - printf(" Exiting SPI mode/reseting Buspirate..\n"); - i=0x00; - serial_write(fd, &i, 1); - Sleep(1); - res = serial_read(fd, buffer, sizeof(buffer)); - - // pause after the result - - if (firsttime==0){ // run here once and don't say again the next time - printf(" Press any key to continue...\n"); - firsttime=1; - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - break; - } - } - } - - res = serial_write( fd, "\x00", 1); //reset buspirate - - //close port so they can attach the next Bus Pirate - serial_close(fd); - - - //TODO: Loop back to pause after this - printf("\n Connect another MMA7455L breakout board to the bus pirate \n and press any key to start the self-test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - - #define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} + Sleep(1); + // Read one byte + res = serial_read(fd, buffer, 3); + if (res!=0) { + printf(" Got reply: "); + printf("0X%02X ",buffer[2]); + printf(" \n\n"); + if (buffer[2] !=0x1D) + printf(" Self test status with MMA7455L breakout board: !!!! FAILED! !!!! \n\n"); + else + printf(" Self test status with MMA7455L breakout board: **** PASS! *****\n\n"); + }else{ + printf("FAIL! Got No Reply\n"); + } + + // Set CS high + i=0x03; + printf(" CS High : 0x03....\n"); + BP_WriteToPirate(fd,&i); + + printf(" Exiting SPI mode/reseting Buspirate..\n"); + i=0x00; + serial_write(fd, &i, 1); + Sleep(1); + res = serial_read(fd, buffer, sizeof(buffer)); + + // pause after the result + + if (firsttime==0){ // run here once and don't say again the next time + printf(" Press any key to continue...\n"); + firsttime=1; + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + break; + } + } + } + + res = serial_write( fd, "\x00", 1); //reset buspirate + + //close port so they can attach the next Bus Pirate + serial_close(fd); + + + //TODO: Loop back to pause after this + printf("\n Connect another MMA7455L breakout board to the bus pirate \n and press any key to start the self-test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + + #define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} diff --git a/scripts/powertools/HVPselftest/main.c b/scripts/powertools/HVPselftest/main.c index b940a7a7..1ec0b128 100644 --- a/scripts/powertools/HVPselftest/main.c +++ b/scripts/powertools/HVPselftest/main.c @@ -1,287 +1,287 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - * - * Reference on the wiki: - * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter - * http://dangerousprototypes.com/docs/Bitbang - * - */ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) -{ - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -p device -s speed \n ",appname); - printf("\n"); - printf(" Example Usage: %s -p COM1 -s 115200 \n",appname); - printf("\n"); - printf(" Where: -p device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; -} - - - -int main(int argc, char** argv) -{ - int opt; - char ADCraw[2]={0}; - int fd; - int res,c,adc; - float voltage,vout ; - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate HVP Adapter SELF TEST utility v0.3 (CC-0)\n"); - printf(" http://www.dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - - if (argc <= 1){ - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - - while ((opt = getopt(argc, argv, "ms:p:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'p': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - flag=0; - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - //Set Speed of port - serial_setup(fd,(speed_t) param_speed); - - //printf(" Configuring Bus Pirate HVP Adapter...\n"); - //go into binary bitbang mode - printf(" Going into Binary Bitbang mode.."); - if(BP_EnableBinary(fd)!=BBIO){ - printf(" Buspirate cannot switch to binary bitbang mode :( \n"); - return -1; - } - - // - // Loop and repeat test as needed for manufacturing - // - - printf(" Press Esc to exit, any other key to start the self-test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1){ - printf("\n--------------------- Starting a new Test-------------------------\n"); - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - } - else {//make space only - printf("\n Starting test! \n"); - break; - } - } - } - - - -// -// Open serial port -// - // - //Start self-test - // 010xxxxx - Configure pins as input(1) or output(0): AUX|MOSI|CLK|MISO|CS - // should configured as output 01000000 = 0x40 - - printf(" Configuring pin direction: 01001100...\n"); - //printf(" sending 01000000..."); - BP_WriteToPirateNoCheck(fd,"\x4C"); - //printf("OK\n"); - //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS - //11000000 - on : poweronly = 0xC0 - printf(" Sending Command to power on : 11010000...."); - BP_WriteToPirateNoCheck(fd,"\xD2");//c0 - printf("OK\n"); - Sleep(100); - - // - // Done with setup ,start with testing - // 00010100 - Take voltage probe measurement (returns 2 bytes) - // 00010100 = 0x14 - - printf(" Voltage Probe measurement...,sending 00010100 \n"); - serial_write( fd, "\x14",1 ); - res=0; - while(res==0){ - Sleep(1); //how long wait for a reply? - res = serial_read(fd, ADCraw, 2); - } - //for debugging without hardware using only modem, use -m to pass - if (modem==TRUE){ - res=2; - ADCraw[0]=0x02; - ADCraw[1]=0xA6; - } - // comment the above if with hardware - if (res!=2){ - printf(" Expecting 2 bytes returned but got %i instead\n",res); - printf(" Voltage Measurement: !!!!FAIL!!!! \n"); - } - else - { - // Take a measurement from the Bus Pirate voltage probe. - // Returns a 2 byte ADC reading, high 8bits come first. - // To determine the actual voltage measurement: - // Raw reading: 0x2A6 (678) - // Actual voltage: (678/1024)*3.3volts=2.18volts - // Scale for resistor divider: Vin = (Vout*(R1+R2))/R2 = (2.18volts*(49K+10K))/10K = 12.86volts (ideal is 13volts) - - adc = ADCraw[0] << 8; - adc |= (ADCraw[1]&0x00ff); - vout= ((float)adc/1024.0f)*3.3f; - voltage =(vout*(49.0f+10.0f))/10.0f; - - printf(" ADC Reading: %2.1f Volts (%02X, %02X)\n",voltage, (char)ADCraw[0], ((char)ADCraw[1]&0xff)); - if(voltage > 10.5 ){ - printf(" Voltage Measurement: ****PASS**** \n"); - }else{ - printf(" Voltage Measurement: !!!!FAIL!!!! \n"); - } - - } - - printf(" Pins to input: 01011111...\n"); - //printf(" sending 01000000..."); - BP_WriteToPirateNoCheck(fd,"\x5F"); - //power off - //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS - //10000000 - off : power only = 0x80 - printf( " powering off.....\n"); - BP_WriteToPirateNoCheck(fd,"\x80"); - - - //TODO: Loop back to pause after this - printf("\n Connect another HVP adapter and press any key to test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - res = serial_write( fd, "\x0F", 1); //exit BBIO - //close port so they can attach the next Bus Pirate - serial_close(fd); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - - #define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + * + * Reference on the wiki: + * http://dangerousprototypes.com/docs/Bus_Pirate_PIC_programming_adapter + * http://dangerousprototypes.com/docs/Bitbang + * + */ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) +{ + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -p device -s speed \n ",appname); + printf("\n"); + printf(" Example Usage: %s -p COM1 -s 115200 \n",appname); + printf("\n"); + printf(" Where: -p device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; +} + + + +int main(int argc, char** argv) +{ + int opt; + char ADCraw[2]={0}; + int fd; + int res,c,adc; + float voltage,vout ; + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate HVP Adapter SELF TEST utility v0.3 (CC-0)\n"); + printf(" http://www.dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + + if (argc <= 1){ + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + + while ((opt = getopt(argc, argv, "ms:p:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'p': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + flag=0; + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + //Set Speed of port + serial_setup(fd,(speed_t) param_speed); + + //printf(" Configuring Bus Pirate HVP Adapter...\n"); + //go into binary bitbang mode + printf(" Going into Binary Bitbang mode.."); + if(BP_EnableBinary(fd)!=BBIO){ + printf(" Buspirate cannot switch to binary bitbang mode :( \n"); + return -1; + } + + // + // Loop and repeat test as needed for manufacturing + // + + printf(" Press Esc to exit, any other key to start the self-test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1){ + printf("\n--------------------- Starting a new Test-------------------------\n"); + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + } + else {//make space only + printf("\n Starting test! \n"); + break; + } + } + } + + + +// +// Open serial port +// + // + //Start self-test + // 010xxxxx - Configure pins as input(1) or output(0): AUX|MOSI|CLK|MISO|CS + // should configured as output 01000000 = 0x40 + + printf(" Configuring pin direction: 01001100...\n"); + //printf(" sending 01000000..."); + BP_WriteToPirateNoCheck(fd,"\x4C"); + //printf("OK\n"); + //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS + //11000000 - on : poweronly = 0xC0 + printf(" Sending Command to power on : 11010000...."); + BP_WriteToPirateNoCheck(fd,"\xD2");//c0 + printf("OK\n"); + Sleep(100); + + // + // Done with setup ,start with testing + // 00010100 - Take voltage probe measurement (returns 2 bytes) + // 00010100 = 0x14 + + printf(" Voltage Probe measurement...,sending 00010100 \n"); + serial_write( fd, "\x14",1 ); + res=0; + while(res==0){ + Sleep(1); //how long wait for a reply? + res = serial_read(fd, ADCraw, 2); + } + //for debugging without hardware using only modem, use -m to pass + if (modem==TRUE){ + res=2; + ADCraw[0]=0x02; + ADCraw[1]=0xA6; + } + // comment the above if with hardware + if (res!=2){ + printf(" Expecting 2 bytes returned but got %i instead\n",res); + printf(" Voltage Measurement: !!!!FAIL!!!! \n"); + } + else + { + // Take a measurement from the Bus Pirate voltage probe. + // Returns a 2 byte ADC reading, high 8bits come first. + // To determine the actual voltage measurement: + // Raw reading: 0x2A6 (678) + // Actual voltage: (678/1024)*3.3volts=2.18volts + // Scale for resistor divider: Vin = (Vout*(R1+R2))/R2 = (2.18volts*(49K+10K))/10K = 12.86volts (ideal is 13volts) + + adc = ADCraw[0] << 8; + adc |= (ADCraw[1]&0x00ff); + vout= ((float)adc/1024.0f)*3.3f; + voltage =(vout*(49.0f+10.0f))/10.0f; + + printf(" ADC Reading: %2.1f Volts (%02X, %02X)\n",voltage, (char)ADCraw[0], ((char)ADCraw[1]&0xff)); + if(voltage > 10.5 ){ + printf(" Voltage Measurement: ****PASS**** \n"); + }else{ + printf(" Voltage Measurement: !!!!FAIL!!!! \n"); + } + + } + + printf(" Pins to input: 01011111...\n"); + //printf(" sending 01000000..."); + BP_WriteToPirateNoCheck(fd,"\x5F"); + //power off + //1xxxxxxx - Set on (1) or off (0): POWER|PULLUP|AUX|MOSI|CLK|MISO|CS + //10000000 - off : power only = 0x80 + printf( " powering off.....\n"); + BP_WriteToPirateNoCheck(fd,"\x80"); + + + //TODO: Loop back to pause after this + printf("\n Connect another HVP adapter and press any key to test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + res = serial_write( fd, "\x0F", 1); //exit BBIO + //close port so they can attach the next Bus Pirate + serial_close(fd); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + + #define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} diff --git a/scripts/powertools/LCDv2-tester/main-text.c b/scripts/powertools/LCDv2-tester/main-text.c index a491caad..227b15fd 100644 --- a/scripts/powertools/LCDv2-tester/main-text.c +++ b/scripts/powertools/LCDv2-tester/main-text.c @@ -1,280 +1,280 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) - { - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -d device \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 \n",appname); - printf("\n"); - printf(" Where: -d device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; - } - - - -int main(int argc, char** argv) -{ - int opt; - char buffer[256] = {0}, i; - int fd; - int res,c; - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate LCD adapter test utility v0.1 (CC-0)\n"); - printf(" http://dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - // printf("\n"); - -if (argc <= 1) { - - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - -while ((opt = getopt(argc, argv, "ms:d:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'd': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - // printf(" Press escape to exit \n"); - // printf("\n"); - - - - //param_port=strdup("COM3"); - //Set default if NULL - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - - - // - // Open serial port - // - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - - // - // Enter binary mode, then enter a protocol mode - // - serial_setup(fd,(speed_t) param_speed); - - flag=0; - // - // Loop and repeat test as needed for manufacturing - // - //TO DO: add loop here - printf(" Press Esc to exit, any other key to start the test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1) - { - printf("\n--------------------- Starting a new Test-------------------------\n"); - - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - break; - } - } - } - - - - // - //Start test - // - //printf(" Starting test...\n"); - - //fprintf(stderr, " SPI power on...\n"); - - buffer[0]='m';//data - buffer[1]=' '; - buffer[2]='9'; - buffer[3]=0x0d; - buffer[4]=0x0d; - serial_write(fd,buffer,4); - Sleep(1); - - buffer[0]='W';//data - buffer[1]=0x0d; - buffer[2]=0x0d; - serial_write(fd,buffer,2); - Sleep(1); - - buffer[0]='(';//data - buffer[1]='2'; - buffer[2]=')'; - buffer[3]=0x0d; - buffer[4]=0x0d; - serial_write(fd,buffer,4); - Sleep(250); - - buffer[0]='2';//data - buffer[1]=0x0d; - buffer[2]=0x0d; - serial_write(fd,buffer,2); - Sleep(250); - - buffer[0]='(';//data - buffer[1]='7'; - buffer[2]=')'; - buffer[3]='8'; - buffer[4]='0'; - buffer[5]=0x0d; - buffer[6]=0x0d; - serial_write(fd,buffer,6); - Sleep(1); - - printf(" Check LED and LCD\n "); - - - printf(" Press any key to continue...\n"); - firsttime=1; - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - break; - } - } - - buffer[0]='m';//data - buffer[1]='1'; - buffer[2]=0x0d; - buffer[3]=0x0d; - serial_write(fd,buffer,3); - Sleep(1); - - //clear the serial buffer - serial_read(fd, buffer, sizeof(buffer)); - //printf("%s\n",buffer); - - //TODO: Loop back to pause after this - printf("\n Connect another adapter and press any key to start the self-test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - -#define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) + { + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -d device \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 \n",appname); + printf("\n"); + printf(" Where: -d device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; + } + + + +int main(int argc, char** argv) +{ + int opt; + char buffer[256] = {0}, i; + int fd; + int res,c; + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate LCD adapter test utility v0.1 (CC-0)\n"); + printf(" http://dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + // printf("\n"); + +if (argc <= 1) { + + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + +while ((opt = getopt(argc, argv, "ms:d:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'd': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + // printf(" Press escape to exit \n"); + // printf("\n"); + + + + //param_port=strdup("COM3"); + //Set default if NULL + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + + + // + // Open serial port + // + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + + // + // Enter binary mode, then enter a protocol mode + // + serial_setup(fd,(speed_t) param_speed); + + flag=0; + // + // Loop and repeat test as needed for manufacturing + // + //TO DO: add loop here + printf(" Press Esc to exit, any other key to start the test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1) + { + printf("\n--------------------- Starting a new Test-------------------------\n"); + + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + break; + } + } + } + + + + // + //Start test + // + //printf(" Starting test...\n"); + + //fprintf(stderr, " SPI power on...\n"); + + buffer[0]='m';//data + buffer[1]=' '; + buffer[2]='9'; + buffer[3]=0x0d; + buffer[4]=0x0d; + serial_write(fd,buffer,4); + Sleep(1); + + buffer[0]='W';//data + buffer[1]=0x0d; + buffer[2]=0x0d; + serial_write(fd,buffer,2); + Sleep(1); + + buffer[0]='(';//data + buffer[1]='2'; + buffer[2]=')'; + buffer[3]=0x0d; + buffer[4]=0x0d; + serial_write(fd,buffer,4); + Sleep(250); + + buffer[0]='2';//data + buffer[1]=0x0d; + buffer[2]=0x0d; + serial_write(fd,buffer,2); + Sleep(250); + + buffer[0]='(';//data + buffer[1]='7'; + buffer[2]=')'; + buffer[3]='8'; + buffer[4]='0'; + buffer[5]=0x0d; + buffer[6]=0x0d; + serial_write(fd,buffer,6); + Sleep(1); + + printf(" Check LED and LCD\n "); + + + printf(" Press any key to continue...\n"); + firsttime=1; + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + break; + } + } + + buffer[0]='m';//data + buffer[1]='1'; + buffer[2]=0x0d; + buffer[3]=0x0d; + serial_write(fd,buffer,3); + Sleep(1); + + //clear the serial buffer + serial_read(fd, buffer, sizeof(buffer)); + //printf("%s\n",buffer); + + //TODO: Loop back to pause after this + printf("\n Connect another adapter and press any key to start the self-test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + +#define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} diff --git a/scripts/powertools/LCDv2-tester/main.c b/scripts/powertools/LCDv2-tester/main.c index f9d47d5b..85c6e8c9 100644 --- a/scripts/powertools/LCDv2-tester/main.c +++ b/scripts/powertools/LCDv2-tester/main.c @@ -1,305 +1,305 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) - { - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -d device \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 \n",appname); - printf("\n"); - printf(" Where: -d device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; - } - - - -int main(int argc, char** argv) -{ - int opt; - char buffer[256] = {0}, i; - int fd; - int res,c; - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate LCD adapter test utility v0.1 (CC-0)\n"); - printf(" http://dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - // printf("\n"); - -if (argc <= 1) { - - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - -while ((opt = getopt(argc, argv, "ms:d:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'd': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - // printf(" Press escape to exit \n"); - // printf("\n"); - - - - //param_port=strdup("COM3"); - //Set default if NULL - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - - - // - // Open serial port - // - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - - // - // Enter binary mode, then enter a protocol mode - // - serial_setup(fd,(speed_t) param_speed); - - - fprintf(stderr, " Configuring Bus Pirate...\n"); - //BP_EnableMode(fd, SPI); //enter BBIO then SPI - if(BP_EnableBinary(fd)!=BBIO){ - fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); - return -1; - } - - fprintf(stderr, " Switch to SPI...\n"); - if(BP_EnableMode(fd , SPI)!=SPI){ - fprintf(stderr, " Buspirate cannot switch to SPI mode :( \n"); - fprintf(stderr, " Exiting...\n"); - return -1; - } - - flag=0; - // - // Loop and repeat test as needed for manufacturing - // - //TO DO: add loop here - printf(" Press Esc to exit, any other key to start the test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1) - { - printf("\n--------------------- Starting a new Test-------------------------\n"); - - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - break; - } - } - } - - - - // - //Start self-test - // - //printf(" Starting test...\n"); - - //fprintf(stderr, " SPI power on...\n"); - - i=0x48;//power on, CS low, etc. - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - - //fprintf(stderr, " SPI pin setup...\n"); - //configure according to user settings - //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample - i=0x80; - i|=0x0A; - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - printf(" LED and all outputs should blink!!!\n "); - - - printf(" Press any key to continue...\n"); - firsttime=1; - while(1){ - i=0x10;//send data - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - i=0x00;//data - BP_WriteToPirateNoCheck(fd, &i); - - i=0x03;//cs High - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - i=0x02;//cs low - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - - i=0x10;//send data - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - i=0xFF;//data - BP_WriteToPirateNoCheck(fd, &i); - - i=0x03;//cs High - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - i=0x02;//cs low - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - Sleep(1); - if(kbhit()){ - c = getch(); - break; - } - } - - //fprintf(stderr, " SPI power off...\n"); - - i=0x40;//power on, CS low, etc. - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - - //fprintf(stderr, " SPI pin hiz...\n"); - //configure according to user settings - //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample - i=0x80; - i|=0x02; - if (BP_WriteToPirate(fd, &i)) - printf("WARNING.. Not Good\n"); - - //TODO: Loop back to pause after this - printf("\n Connect another adapter and press any key to start the self-test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - -#define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) + { + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -d device \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 \n",appname); + printf("\n"); + printf(" Where: -d device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; + } + + + +int main(int argc, char** argv) +{ + int opt; + char buffer[256] = {0}, i; + int fd; + int res,c; + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate LCD adapter test utility v0.1 (CC-0)\n"); + printf(" http://dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + // printf("\n"); + +if (argc <= 1) { + + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + +while ((opt = getopt(argc, argv, "ms:d:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'd': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + // printf(" Press escape to exit \n"); + // printf("\n"); + + + + //param_port=strdup("COM3"); + //Set default if NULL + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + + + // + // Open serial port + // + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + + // + // Enter binary mode, then enter a protocol mode + // + serial_setup(fd,(speed_t) param_speed); + + + fprintf(stderr, " Configuring Bus Pirate...\n"); + //BP_EnableMode(fd, SPI); //enter BBIO then SPI + if(BP_EnableBinary(fd)!=BBIO){ + fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); + return -1; + } + + fprintf(stderr, " Switch to SPI...\n"); + if(BP_EnableMode(fd , SPI)!=SPI){ + fprintf(stderr, " Buspirate cannot switch to SPI mode :( \n"); + fprintf(stderr, " Exiting...\n"); + return -1; + } + + flag=0; + // + // Loop and repeat test as needed for manufacturing + // + //TO DO: add loop here + printf(" Press Esc to exit, any other key to start the test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1) + { + printf("\n--------------------- Starting a new Test-------------------------\n"); + + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + break; + } + } + } + + + + // + //Start self-test + // + //printf(" Starting test...\n"); + + //fprintf(stderr, " SPI power on...\n"); + + i=0x48;//power on, CS low, etc. + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + + //fprintf(stderr, " SPI pin setup...\n"); + //configure according to user settings + //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample + i=0x80; + i|=0x0A; + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + printf(" LED and all outputs should blink!!!\n "); + + + printf(" Press any key to continue...\n"); + firsttime=1; + while(1){ + i=0x10;//send data + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + i=0x00;//data + BP_WriteToPirateNoCheck(fd, &i); + + i=0x03;//cs High + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + i=0x02;//cs low + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + + i=0x10;//send data + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + i=0xFF;//data + BP_WriteToPirateNoCheck(fd, &i); + + i=0x03;//cs High + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + i=0x02;//cs low + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + Sleep(1); + if(kbhit()){ + c = getch(); + break; + } + } + + //fprintf(stderr, " SPI power off...\n"); + + i=0x40;//power on, CS low, etc. + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + + //fprintf(stderr, " SPI pin hiz...\n"); + //configure according to user settings + //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample + i=0x80; + i|=0x02; + if (BP_WriteToPirate(fd, &i)) + printf("WARNING.. Not Good\n"); + + //TODO: Loop back to pause after this + printf("\n Connect another adapter and press any key to start the self-test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + +#define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} diff --git a/scripts/powertools/SPISniffer/linux-version/buspirate.c b/scripts/powertools/SPISniffer/linux-version/buspirate.c index d63540fb..802b2c4e 100644 --- a/scripts/powertools/SPISniffer/linux-version/buspirate.c +++ b/scripts/powertools/SPISniffer/linux-version/buspirate.c @@ -1,106 +1,106 @@ -#include -#include -#include -#include -#include "serial.h" -#include "buspirate.h" - -//static struct BP_t pBP; -//static uint8_t BP_reversebyte(uint8_t c); -//static char bpbuf[4096]; -//static int bpbufcnt; -extern int disable_comport; -extern int dumphandle; -extern int verbose; -extern int modem; -//low lever send command, get reply function - -uint32_t BP_WriteToPirate(int fd, char * val) { - int res = -1; - char ret = 0; - - - serial_write(fd, val, 1); - - if (disable_comport != 1) { //if comport is enable, we need a response from the port - - res = serial_read(fd, &ret, 1); - - if( ret != '\x01') { - if (modem==TRUE){ - printf("Modem responded with %i byte and with a value of 0X%X\n",res,ret); - - } else { - printf("ERROR"); - return -1; - } - } - } - return 0; -} - -void BP_EnableMode(int fd, char bbmode) -{ - int ret; - char tmp[100] = { [0 ... 20] = 0x00 }; - int done = 0; - //int cmd_sent = 0; - int tries=0; - - printf(" Entering binary mode...\n"); - - if (fd==-1) //added because the fd has already returned null - { - printf("Port does not exist!"); - return; - - } - while (!done) { - tmp[0]=0x00; - //printf("Sending 0X%02X to port\n",tmp[0]); - serial_write(fd, tmp, 1); - tries++; - //printf("tries: %i Ret %i\n",tries,ret); - usleep(1); - ret = serial_read(fd, tmp, 5); - //printf("read returned %i:%s\n", ret,tmp); - if (modem==TRUE) - { - printf("\nModem Responded = %i\n",ret); - done=1; - } - else { - if (ret != 5 && tries>20) { - fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret ); - exit(-1); - } else if (strncmp(tmp, "BBIO1", 5) == 0) { - done=1; - } - } - - if (tries>25){ - printf("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); - exit(-1); - } - } - - done=0; - tmp[0] = bbmode; - //printf("Sending 0X%02X to port\n",tmp[0]); - serial_write(fd, tmp, 1); - tries++; - usleep(1); - ret = serial_read(fd, tmp, 4); - if (modem==TRUE) - { - printf("Modem Responded = %i with value %#X\n",ret,tmp[0]); - } - else { - if ( (ret==4) && (strncmp(tmp, "SPI1", 4) == 0)) { - - } else{ - fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret ); - exit(-1); - } - } -} +#include +#include +#include +#include +#include "serial.h" +#include "buspirate.h" + +//static struct BP_t pBP; +//static uint8_t BP_reversebyte(uint8_t c); +//static char bpbuf[4096]; +//static int bpbufcnt; +extern int disable_comport; +extern int dumphandle; +extern int verbose; +extern int modem; +//low lever send command, get reply function + +uint32_t BP_WriteToPirate(int fd, char * val) { + int res = -1; + char ret = 0; + + + serial_write(fd, val, 1); + + if (disable_comport != 1) { //if comport is enable, we need a response from the port + + res = serial_read(fd, &ret, 1); + + if( ret != '\x01') { + if (modem==TRUE){ + printf("Modem responded with %i byte and with a value of 0X%X\n",res,ret); + + } else { + printf("ERROR"); + return -1; + } + } + } + return 0; +} + +void BP_EnableMode(int fd, char bbmode) +{ + int ret; + char tmp[100] = { [0 ... 20] = 0x00 }; + int done = 0; + //int cmd_sent = 0; + int tries=0; + + printf(" Entering binary mode...\n"); + + if (fd==-1) //added because the fd has already returned null + { + printf("Port does not exist!"); + return; + + } + while (!done) { + tmp[0]=0x00; + //printf("Sending 0X%02X to port\n",tmp[0]); + serial_write(fd, tmp, 1); + tries++; + //printf("tries: %i Ret %i\n",tries,ret); + usleep(1); + ret = serial_read(fd, tmp, 5); + //printf("read returned %i:%s\n", ret,tmp); + if (modem==TRUE) + { + printf("\nModem Responded = %i\n",ret); + done=1; + } + else { + if (ret != 5 && tries>20) { + fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret ); + exit(-1); + } else if (strncmp(tmp, "BBIO1", 5) == 0) { + done=1; + } + } + + if (tries>25){ + printf("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); + exit(-1); + } + } + + done=0; + tmp[0] = bbmode; + //printf("Sending 0X%02X to port\n",tmp[0]); + serial_write(fd, tmp, 1); + tries++; + usleep(1); + ret = serial_read(fd, tmp, 4); + if (modem==TRUE) + { + printf("Modem Responded = %i with value %#X\n",ret,tmp[0]); + } + else { + if ( (ret==4) && (strncmp(tmp, "SPI1", 4) == 0)) { + + } else{ + fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret ); + exit(-1); + } + } +} diff --git a/scripts/powertools/SPISniffer/linux-version/buspirate.h b/scripts/powertools/SPISniffer/linux-version/buspirate.h index 2d4db00c..6cfa6967 100644 --- a/scripts/powertools/SPISniffer/linux-version/buspirate.h +++ b/scripts/powertools/SPISniffer/linux-version/buspirate.h @@ -1,12 +1,12 @@ -#ifndef BUSPIRATE_h_ -#define BUSPIRATE_h_ - -#include - - -uint32_t BP_WriteToPirate(int fd, char * val); -void BP_EnableMode(int fd, char bbmode); - -#endif - - +#ifndef BUSPIRATE_h_ +#define BUSPIRATE_h_ + +#include + + +uint32_t BP_WriteToPirate(int fd, char * val); +void BP_EnableMode(int fd, char bbmode); + +#endif + + diff --git a/scripts/powertools/SPISniffer/linux-version/main.c b/scripts/powertools/SPISniffer/linux-version/main.c index 73c23291..e8b0b38f 100644 --- a/scripts/powertools/SPISniffer/linux-version/main.c +++ b/scripts/powertools/SPISniffer/linux-version/main.c @@ -1,331 +1,331 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include -#include -#include -#include - -#ifdef WIN32 -#include -#include -#else -//#include -#endif - -#include "buspirate.h" -#include "serial.h" - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -#define SPI 0x01 - -int print_usage(char * appname) - { - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -d device -e 1 -p 0 \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 \n",appname); - printf("\n"); - printf(" Where: -d device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf(" -e ClockEdge is 0 or 1 default is 1 \n"); - printf(" -p Polarity is 0 or 1 default is 0 \n"); - printf(" -r RawData is 0 or 1 default is 0 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------\n"); - - - return 0; - } - - - -int main(int argc, char** argv) -{ -int opt; - char buffer[256] = {0}, i; - int fd; - int res,c; - int new_state,state=0; - - char *param_port = NULL; - char *param_speed = NULL; - char *param_polarity=NULL; - char *param_clockedge=NULL; - char *param_rawdata=NULL; - -// int clock_edge; -// int polarity; - //default speed - // param_port=strdup("COM3"); //default for debugging - - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate binary mode SPI SNIFFER utility v0.2 (CC-0)\n"); - printf(" http://dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - printf("\n\n\n"); - -//p == port -// p=polarity -// e=clock edge - -if (argc <= 1) { - printf("ERROR: Invalid argument(s).\n\n"); - printf("Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - -while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'd': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf("Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 'e': // clock edge - if (param_clockedge != NULL) { - printf("Clock Edge should be 0 or 1\n"); - exit(-1); - } - param_clockedge = strdup(optarg); - - break; - case 'p': - if (param_polarity != NULL) { - printf("Polarity must be 0 or 1\n"); - exit(-1); - } - param_polarity = strdup(optarg); - - break; - case 's': - if (param_speed != NULL) { - printf("Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'r': // raw data - if (param_rawdata != NULL) { - printf("Raw Data should be 0 or 1\n"); - exit(-1); - } - param_rawdata = strdup(optarg); - - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf("Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - printf(" Press escape to exit \n"); - printf("\n"); - - - - //param_port=strdup("COM3"); - //Set default if NULL - if (param_port==NULL){ - printf("No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_clockedge==NULL) - param_clockedge=strdup("1"); - - if (param_polarity==NULL) - param_polarity=strdup("0"); - - if (param_speed==NULL) - param_speed=strdup("115200"); - - if (param_rawdata==NULL) - param_rawdata=strdup("0"); - - - printf("\n Parameters used: Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s\n\n",param_port,param_speed,param_clockedge,param_polarity); - - - // - // Open serial port - // - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, "Error opening serial port\n"); - return -1; - } - - // - // Enter binary mode, then enter a protocol mode - // - serial_setup(fd,(speed_t) atoi(param_speed)); - - printf(" Starting SPI sniffer...\n"); - - - if (modem==TRUE){ // connected to modem for testing response { - - serial_write( fd, "ATI3\x0D\0",5 ); - usleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - - serial_write( fd, "ATI4\x0D\0",5 ); - usleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - serial_write( fd, "ATI7\x0D\0",5 ); - usleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - - - } - else - { - - - fprintf(stderr, " Configuring Bus Pirate...\n"); - BP_EnableMode(fd, SPI); //enter BBIO then SPI - // - //Start sniffer - // - - //configure according to user settings - //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample - i=0x80; - if(strncmp(param_clockedge, "1", 1)==0) - i|=0x02; - if(strncmp(param_polarity, "1", 1)==0) - i|=0x04; - - BP_WriteToPirate(fd, &i); - - //start the sniffer - serial_write( fd, "\x0E", 1); - - // - // Done with setup - // - } - - - printf(" (OK) Happy sniffing! Press ESC to stop.\n"); - - // - // Loop and print input from the serial port - // - while(1){ - - usleep(1); - res= serial_read(fd, buffer, 100); - - if(res>0){ - for(c=0; c +#include +#include +#include + +#ifdef WIN32 +#include +#include +#else +//#include +#endif + +#include "buspirate.h" +#include "serial.h" + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +#define SPI 0x01 + +int print_usage(char * appname) + { + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -d device -e 1 -p 0 \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 \n",appname); + printf("\n"); + printf(" Where: -d device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf(" -e ClockEdge is 0 or 1 default is 1 \n"); + printf(" -p Polarity is 0 or 1 default is 0 \n"); + printf(" -r RawData is 0 or 1 default is 0 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------\n"); + + + return 0; + } + + + +int main(int argc, char** argv) +{ +int opt; + char buffer[256] = {0}, i; + int fd; + int res,c; + int new_state,state=0; + + char *param_port = NULL; + char *param_speed = NULL; + char *param_polarity=NULL; + char *param_clockedge=NULL; + char *param_rawdata=NULL; + +// int clock_edge; +// int polarity; + //default speed + // param_port=strdup("COM3"); //default for debugging + + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate binary mode SPI SNIFFER utility v0.2 (CC-0)\n"); + printf(" http://dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + printf("\n\n\n"); + +//p == port +// p=polarity +// e=clock edge + +if (argc <= 1) { + printf("ERROR: Invalid argument(s).\n\n"); + printf("Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + +while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'd': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf("Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 'e': // clock edge + if (param_clockedge != NULL) { + printf("Clock Edge should be 0 or 1\n"); + exit(-1); + } + param_clockedge = strdup(optarg); + + break; + case 'p': + if (param_polarity != NULL) { + printf("Polarity must be 0 or 1\n"); + exit(-1); + } + param_polarity = strdup(optarg); + + break; + case 's': + if (param_speed != NULL) { + printf("Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'r': // raw data + if (param_rawdata != NULL) { + printf("Raw Data should be 0 or 1\n"); + exit(-1); + } + param_rawdata = strdup(optarg); + + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf("Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + printf(" Press escape to exit \n"); + printf("\n"); + + + + //param_port=strdup("COM3"); + //Set default if NULL + if (param_port==NULL){ + printf("No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_clockedge==NULL) + param_clockedge=strdup("1"); + + if (param_polarity==NULL) + param_polarity=strdup("0"); + + if (param_speed==NULL) + param_speed=strdup("115200"); + + if (param_rawdata==NULL) + param_rawdata=strdup("0"); + + + printf("\n Parameters used: Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s\n\n",param_port,param_speed,param_clockedge,param_polarity); + + + // + // Open serial port + // + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, "Error opening serial port\n"); + return -1; + } + + // + // Enter binary mode, then enter a protocol mode + // + serial_setup(fd,(speed_t) atoi(param_speed)); + + printf(" Starting SPI sniffer...\n"); + + + if (modem==TRUE){ // connected to modem for testing response { + + serial_write( fd, "ATI3\x0D\0",5 ); + usleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + + serial_write( fd, "ATI4\x0D\0",5 ); + usleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + serial_write( fd, "ATI7\x0D\0",5 ); + usleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + + + } + else + { + + + fprintf(stderr, " Configuring Bus Pirate...\n"); + BP_EnableMode(fd, SPI); //enter BBIO then SPI + // + //Start sniffer + // + + //configure according to user settings + //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample + i=0x80; + if(strncmp(param_clockedge, "1", 1)==0) + i|=0x02; + if(strncmp(param_polarity, "1", 1)==0) + i|=0x04; + + BP_WriteToPirate(fd, &i); + + //start the sniffer + serial_write( fd, "\x0E", 1); + + // + // Done with setup + // + } + + + printf(" (OK) Happy sniffing! Press ESC to stop.\n"); + + // + // Loop and print input from the serial port + // + while(1){ + + usleep(1); + res= serial_read(fd, buffer, 100); + + if(res>0){ + for(c=0; c -#include "serial.h" -extern int disable_comport; +#include "serial.h" +extern int disable_comport; extern char *dumpfile; #ifdef WIN32 -extern HANDLE dumphandle; +extern HANDLE dumphandle; #endif /* #ifdef WIN32 @@ -191,15 +191,15 @@ int serial_write(int fd, char *buf, int size) unsigned long bwritten = 0; - res = WriteFile(hCom, buf, size, &bwritten, NULL); - + res = WriteFile(hCom, buf, size, &bwritten, NULL); + //printf("Serial: Written %i bytes: ",size); - //int i; + //int i; //for (i = 0; i < size; i++){ //printf("%02X ", buf[i]); - //} - //printf("\n"); - + //} + //printf("\n"); + if( res == FALSE ) { ret = -1; } else { @@ -211,21 +211,21 @@ int serial_write(int fd, char *buf, int size) //fprintf(stderr, "size = %d ret = %d\n", size, ret); //buspirate_print_buffer(buf, size); - if (disable_comport !=1) //added for no port - { + if (disable_comport !=1) //added for no port + { if (ret != size) fprintf(stderr, "Error sending data"); - return ret; - } + return ret; + } return ret; } int serial_read(int fd, char *buf, int size) { int len = 0; - int ret = 0; + int ret = 0; #ifndef WIN32 - int timeout = 0; + int timeout = 0; #endif #ifdef WIN32 HANDLE hCom = (HANDLE)fd; @@ -262,17 +262,17 @@ int serial_read(int fd, char *buf, int size) //printf("should have read = %i actual size = %i \n", size, len); //fprintf(stderr, "should have read = %d actual size = %d \n", size, len); //buspirate_print_buffer(buf, len); -if (disable_comport!=1) -{ - +if (disable_comport!=1) +{ + //if (len != size) // fprintf(stderr, "Error sending data"); - - return len; -} -else + + return len; +} +else return len; } @@ -295,15 +295,15 @@ int serial_open(char *port) fd = -1; } else { fd = (int)hCom; - } - - //added to create a dumpfile - if (dumpfile !=NULL) { + } + + //added to create a dumpfile + if (dumpfile !=NULL) { dumphandle = CreateFileA(dumpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if( !dumphandle || dumphandle == INVALID_HANDLE_VALUE ) { - printf("Invalid dump file: %s\n",dumpfile); - return -1; - } + printf("Invalid dump file: %s\n",dumpfile); + return -1; + } } #else fd = open(port, O_RDWR | O_NOCTTY); @@ -320,13 +320,13 @@ int serial_close(int fd) #ifdef WIN32 HANDLE hCom = (HANDLE)fd; - CloseHandle(hCom); - - //close dumpfile if it was created - if (dumpfile !=NULL) { - if (dumphandle != NULL) { - CloseHandle(dumphandle); - } + CloseHandle(hCom); + + //close dumpfile if it was created + if (dumpfile !=NULL) { + if (dumphandle != NULL) { + CloseHandle(dumphandle); + } } #else close(fd); diff --git a/scripts/powertools/SPISniffer/linux-version/serial.h b/scripts/powertools/SPISniffer/linux-version/serial.h index 9dc8a545..a7293021 100644 --- a/scripts/powertools/SPISniffer/linux-version/serial.h +++ b/scripts/powertools/SPISniffer/linux-version/serial.h @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * diff --git a/scripts/powertools/SPISniffer/main.c b/scripts/powertools/SPISniffer/main.c index db7851f5..29415e7a 100644 --- a/scripts/powertools/SPISniffer/main.c +++ b/scripts/powertools/SPISniffer/main.c @@ -1,144 +1,144 @@ - #include - #include - #include - #include - -#ifdef WIN32 -#include - #include - -#else -//#include -#endif - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - int modem =FALSE; //set this to TRUE of testing a MODEM - int verbose = 0; - -int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. - int dumphandle; // use by dump file when using the -d dumfile.txt parameter - char *dumpfile; - -#define SPI 0x01 -#ifndef WIN32 -#define usleep(x) Sleep(x); -#endif - -int print_usage(char * appname) - { - //print usage - printf("\n"); -// printf("-----------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Help Menu\n"); - printf(" Usage: \n"); - printf(" %s -d device -e 1 -p 0 \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 -r 1\n",appname); - printf("\n"); - printf(" Where: -d device is port e.g. COM1 \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf(" -e ClockEdge is 0 or 1 default is 1 \n"); - printf(" -p Polarity is 0 or 1 default is 0 \n"); - printf(" -r RawData is 0 or 1 default is 0 \n"); - printf("\n"); - - printf("\n"); - - printf("-----------------------------------------------------------------------------\n"); - - - return 0; - } - - int main(int argc, char** argv) - { - int opt; - - char buffer[256] = {0}, i; - int fd; - int res,c; - - - int new_state,state=0; - char *param_port = NULL; - char *param_speed = NULL; - char *param_polarity=NULL; - char *param_clockedge=NULL; - char *param_rawdata=NULL; - - // int clock_edge; - // int polarity; - //default speed - - printf("-----------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)\n"); - printf(" http://dangerousprototypes.com\n"); - printf("\n"); - printf("-----------------------------------------------------------------------------\n"); - - //p == port - // p=polarity - // e=clock edge - - if (argc <= 1) { - - + #include + #include + #include + #include + +#ifdef WIN32 +#include + #include + +#else +//#include +#endif + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + int modem =FALSE; //set this to TRUE of testing a MODEM + int verbose = 0; + +int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. + int dumphandle; // use by dump file when using the -d dumfile.txt parameter + char *dumpfile; + +#define SPI 0x01 +#ifndef WIN32 +#define usleep(x) Sleep(x); +#endif + +int print_usage(char * appname) + { + //print usage + printf("\n"); +// printf("-----------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Help Menu\n"); + printf(" Usage: \n"); + printf(" %s -d device -e 1 -p 0 \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 -r 1\n",appname); + printf("\n"); + printf(" Where: -d device is port e.g. COM1 \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf(" -e ClockEdge is 0 or 1 default is 1 \n"); + printf(" -p Polarity is 0 or 1 default is 0 \n"); + printf(" -r RawData is 0 or 1 default is 0 \n"); + printf("\n"); + + printf("\n"); + + printf("-----------------------------------------------------------------------------\n"); + + + return 0; + } + + int main(int argc, char** argv) + { + int opt; + + char buffer[256] = {0}, i; + int fd; + int res,c; + + + int new_state,state=0; + char *param_port = NULL; + char *param_speed = NULL; + char *param_polarity=NULL; + char *param_clockedge=NULL; + char *param_rawdata=NULL; + + // int clock_edge; + // int polarity; + //default speed + + printf("-----------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)\n"); + printf(" http://dangerousprototypes.com\n"); + printf("\n"); + printf("-----------------------------------------------------------------------------\n"); + + //p == port + // p=polarity + // e=clock edge + + if (argc <= 1) { + + print_usage(argv[0]); exit(-1); - - } - -while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) { - // printf("%c \n",opt); + } + + +while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) { + // printf("%c \n",opt); - switch (opt) { - case 'd': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf("Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); + switch (opt) { + case 'd': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf("Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); break; - - case 'e': // clock edge - if (param_clockedge != NULL) { - printf("Clock Edge should be 0 or 1\n"); - exit(-1); - } - param_clockedge = strdup(optarg); + + case 'e': // clock edge + if (param_clockedge != NULL) { + printf("Clock Edge should be 0 or 1\n"); + exit(-1); + } + param_clockedge = strdup(optarg); break; - case 'p': - if (param_polarity != NULL) { - printf("Polarity must be 0 or 1\n"); - exit(-1); - } - param_polarity = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf("Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); + case 'p': + if (param_polarity != NULL) { + printf("Polarity must be 0 or 1\n"); + exit(-1); + } + param_polarity = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf("Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); break; - case 'r': // raw data - if (param_rawdata != NULL) { - printf("Raw Data should be 0 or 1\n"); - exit(-1); - } - param_rawdata = strdup(optarg); - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; + case 'r': // raw data + if (param_rawdata != NULL) { + printf("Raw Data should be 0 or 1\n"); + exit(-1); + } + param_rawdata = strdup(optarg); + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; default: printf(" Invalid argument %c", opt); @@ -146,177 +146,177 @@ while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) { exit(-1); break; } - - - - } - // printf(" Press escape to exit \n"); - // printf("\n"); - //param_port=strdup("COM3"); - //Set default if NULL - if (param_port==NULL){ + + + + } + // printf(" Press escape to exit \n"); + // printf("\n"); + //param_port=strdup("COM3"); + //Set default if NULL + if (param_port==NULL){ printf(" No serial port set\n"); print_usage(argv[0]); - exit(-1); - } - - if (param_clockedge==NULL) - param_clockedge=strdup("1"); - - - if (param_polarity==NULL) - param_polarity=strdup("0"); - - if (param_speed==NULL) - param_speed=strdup("115200"); - - if (param_rawdata==NULL) - param_rawdata=strdup("0"); - - printf("\n Parameters used:\n"); - printf(" Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n",param_port,param_speed,param_clockedge,param_polarity,param_rawdata); - - - // - // Open serial port - // - + exit(-1); + } + + if (param_clockedge==NULL) + param_clockedge=strdup("1"); + + + if (param_polarity==NULL) + param_polarity=strdup("0"); + + if (param_speed==NULL) + param_speed=strdup("115200"); + + if (param_rawdata==NULL) + param_rawdata=strdup("0"); + + printf("\n Parameters used:\n"); + printf(" Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n",param_port,param_speed,param_clockedge,param_polarity,param_rawdata); + + + // + // Open serial port + // + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - - fd = serial_open(param_port); + + fd = serial_open(param_port); if (fd < 0) { fprintf(stderr, " Error opening serial port\n"); return -1; - - } - - // - // Enter binary mode, then enter a protocol mode + + } + + // + // Enter binary mode, then enter a protocol mode // - - serial_setup(fd,(speed_t) atoi(param_speed)); - - printf(" Starting SPI sniffer...\n"); - - if (modem==TRUE){ // connected to modem for testing response { - - serial_write( fd, "ATI3\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - serial_write( fd, "ATI4\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - serial_write( fd, "ATI7\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - } - - fprintf(stderr, " Configuring Bus Pirate...\n"); - //BP_EnableMode(fd, SPI); //enter BBIO then SPI - if(BP_EnableBinary(fd)!=BBIO){ - fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); - fprintf(stderr, " Exiting...\n"); - return -1; - } - if(BP_EnableMode(fd , SPI)!=SPI){ - fprintf(stderr, " Buspirate cannot switch to SPI mode :( \n"); - fprintf(stderr, " Exiting...\n"); - return -1; + + serial_setup(fd,(speed_t) atoi(param_speed)); + + printf(" Starting SPI sniffer...\n"); + + if (modem==TRUE){ // connected to modem for testing response { + + serial_write( fd, "ATI3\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + serial_write( fd, "ATI4\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + serial_write( fd, "ATI7\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + } + + fprintf(stderr, " Configuring Bus Pirate...\n"); + //BP_EnableMode(fd, SPI); //enter BBIO then SPI + if(BP_EnableBinary(fd)!=BBIO){ + fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); + fprintf(stderr, " Exiting...\n"); + return -1; + } + if(BP_EnableMode(fd , SPI)!=SPI){ + fprintf(stderr, " Buspirate cannot switch to SPI mode :( \n"); + fprintf(stderr, " Exiting...\n"); + return -1; } - - //configure according to user settings - //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample - printf(" Setting Clockedge/Polarity ......"); - i=0x80; - if(strncmp(param_clockedge, "1", 1)==0){ - printf(" CKE=1"); - i|=0x02; - } - - - if(strncmp(param_polarity, "1", 1)==0){ - printf(" CKP=1"); - i|=0x04; - } - - - if (BP_WriteToPirate(fd, &i)==0) - printf("OK\n"); - else - printf("WARNING.. Not Good\n"); - - //start the sniffer + + //configure according to user settings + //1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample + printf(" Setting Clockedge/Polarity ......"); + i=0x80; + if(strncmp(param_clockedge, "1", 1)==0){ + printf(" CKE=1"); + i|=0x02; + } + + + if(strncmp(param_polarity, "1", 1)==0){ + printf(" CKP=1"); + i|=0x04; + } + + + if (BP_WriteToPirate(fd, &i)==0) + printf("OK\n"); + else + printf("WARNING.. Not Good\n"); + + //start the sniffer serial_write( fd, "\x0E", 1); - // - // Done with setup - // - while(1){ - Sleep(1); - res= serial_read(fd, buffer, 100); - if(res>0){ - for(c=0; c0) - -#ifdef WIN32 - if(kbhit()){ - c = getch(); - break; - } - #endif - } //end while(1) - - - #define FREE(x) if(x) free(x); + // + // Done with setup + // + while(1){ + Sleep(1); + res= serial_read(fd, buffer, 100); + if(res>0){ + for(c=0; c0) + +#ifdef WIN32 + if(kbhit()){ + c = getch(); + break; + } + #endif + } //end while(1) + + + #define FREE(x) if(x) free(x); FREE(param_port); - FREE(param_speed); - FREE(param_polarity); - FREE(param_clockedge); - return 0; - } //end main() + FREE(param_speed); + FREE(param_polarity); + FREE(param_clockedge); + return 0; + } //end main() diff --git a/scripts/powertools/framework/buspirate.c b/scripts/powertools/framework/buspirate.c index 4675dafb..bade77e5 100644 --- a/scripts/powertools/framework/buspirate.c +++ b/scripts/powertools/framework/buspirate.c @@ -3,64 +3,64 @@ #include #include #include "serial.h" -#include "buspirate.h" - - -const char *modes[]={ - "BBIO", - "SPI", - "I2C", - "ART", - "1W0", - "RAW", -}; - -//static struct BP_t pBP; -//static uint8_t BP_reversebyte(uint8_t c); -//static char bpbuf[4096]; -//static int bpbufcnt; -extern int disable_comport; -extern int dumphandle; -extern int verbose; -extern int modem; -//extern char *modes[]; -//low lever send command, get reply function +#include "buspirate.h" + + +const char *modes[]={ + "BBIO", + "SPI", + "I2C", + "ART", + "1W0", + "RAW", +}; + +//static struct BP_t pBP; +//static uint8_t BP_reversebyte(uint8_t c); +//static char bpbuf[4096]; +//static int bpbufcnt; +extern int disable_comport; +extern int dumphandle; +extern int verbose; +extern int modem; +//extern char *modes[]; +//low lever send command, get reply function uint32_t BP_WriteToPirate(int fd, char * val) { int res = -1; - char ret = 0; - - + char ret = 0; + + serial_write(fd, val, 1); - Sleep(1); + Sleep(1); res = serial_read(fd, &ret, 1); - if( ret != '\x01') { - if (modem==TRUE){ - printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); - return 0; - } + if( ret != '\x01') { + if (modem==TRUE){ + printf(" Modem responded with %i byte and with a value of 0X%X\n",res,ret); + return 0; + } else { printf(" ERROR: BusPirate replied with 0x%x instead of 0x01 \n",ret); - return -1; + return -1; } - } + } //printf(" BusPirate said: OK\n"); return 0; -} - +} + uint32_t BP_WriteToPirateNoCheck(int fd, char * val) { int res = -1; - char ret = 0; - - + char ret = 0; + + serial_write(fd, val, 1); - Sleep(1); + Sleep(1); res = serial_read(fd, &ret, 1); return 0; } - + int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not { int ret; @@ -70,53 +70,53 @@ int BP_EnableBinary(int fd) // should return BBIO if ok, ERR if not int tries=0; printf(" Entering binary mode...\n"); - - if (fd==-1) //added because the fd has already returned null - { - printf("Port does not exist!"); - ret=ERR; - + + if (fd==-1) //added because the fd has already returned null + { + printf("Port does not exist!"); + ret=ERR; + } - else - { - - + else + { + + while (!done) { - tmp[0]=0x00; + tmp[0]=0x00; //printf("Sending 0X%X to port\n",tmp[0]); serial_write(fd, tmp, 1); - tries++; + tries++; // printf("tries: %i Ret %i\n",tries,ret); usleep(1); ret = serial_read(fd, tmp, 5); - if (modem==TRUE) - { - printf("\n Modem Responded = %i\n",ret); - done=1; - } - else { + if (modem==TRUE) + { + printf("\n Modem Responded = %i\n",ret); + done=1; + } + else { if (ret != 5 && tries>20) { fprintf(stderr, " Buspirate did not respond correctly :( %i \n", ret ); ret=ERR; - } + } else if (strncmp(tmp, "BBIO1", 5) == 0) { done=1; - ret=BBIO; - } - } - - if (tries>25){ - printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); - break; - ret=ERR; + ret=BBIO; + } + } + + if (tries>25){ + printf(" Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n"); + break; + ret=ERR; } } - } + } return ret; -} - - - +} + + + int BP_EnableMode(int fd, char bbmode) { @@ -127,73 +127,73 @@ int BP_EnableMode(int fd, char bbmode) int tries=0; current_mode=(int)bbmode; printf(" Switching to %s mode\n",modes[current_mode]); - - tmp[0] = bbmode; + + tmp[0] = bbmode; //printf("Sending 0X%X to port\n",tmp[0]); serial_write(fd, tmp, 1); tries++; usleep(1); - ret = serial_read(fd, tmp, 4); - if (modem==TRUE) - { - printf(" Modem Responded = %i with value of ",ret); - if(ret>0){ - for(c=0; c0){ + for(c=0; c - - - +#include + + + #endif - + #define ERR -1 -#define BBIO 0x00 -#define SPI 0x01 +#define BBIO 0x00 +#define SPI 0x01 #define I2C 0x02 -#define ART 0x03 -#define OWD 0x04 -#define RAW 0X05 - - +#define ART 0x03 +#define OWD 0x04 +#define RAW 0X05 + + uint32_t BP_WriteToPirate(int , char *); -int BP_EnableBinary(int); -int BP_EnableMode(int , char ); -uint32_t BP_WriteToPirateNoCheck(int fd, char * val); +int BP_EnableBinary(int); +int BP_EnableMode(int , char ); +uint32_t BP_WriteToPirateNoCheck(int fd, char * val); diff --git a/scripts/powertools/framework/serial.c b/scripts/powertools/framework/serial.c index c6df5e84..450ecf07 100644 --- a/scripts/powertools/framework/serial.c +++ b/scripts/powertools/framework/serial.c @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * @@ -27,10 +27,10 @@ #include -#include "serial.h" -extern int disable_comport; +#include "serial.h" +extern int disable_comport; extern char *dumpfile; -extern HANDLE dumphandle; +extern HANDLE dumphandle; /* #ifdef WIN32 int write(int fd, const void* buf, int len) @@ -188,15 +188,15 @@ int serial_write(int fd, char *buf, int size) unsigned long bwritten = 0; - res = WriteFile(hCom, buf, size, &bwritten, NULL); - + res = WriteFile(hCom, buf, size, &bwritten, NULL); + //printf("Serial: Written %i bytes: ",size); - //int i; + //int i; //for (i = 0; i < size; i++){ //printf("%02X ", buf[i]); - //} - //printf("\n"); - + //} + //printf("\n"); + if( res == FALSE ) { ret = -1; } else { @@ -208,20 +208,20 @@ int serial_write(int fd, char *buf, int size) //fprintf(stderr, "size = %d ret = %d\n", size, ret); //buspirate_print_buffer(buf, size); - + if (ret != size) fprintf(stderr, "Error sending data"); - return ret; - + return ret; + } int serial_read(int fd, char *buf, int size) { int len = 0; - int ret = 0; + int ret = 0; #ifndef WIN32 - int timeout = 0; + int timeout = 0; #endif #ifdef WIN32 HANDLE hCom = (HANDLE)fd; @@ -258,7 +258,7 @@ int serial_read(int fd, char *buf, int size) //printf("should have read = %i actual size = %i \n", size, len); //fprintf(stderr, "should have read = %d actual size = %d \n", size, len); //buspirate_print_buffer(buf, len); - + return len; } @@ -281,15 +281,15 @@ int serial_open(char *port) fd = -1; } else { fd = (int)hCom; - } - - //added to create a dumpfile - if (dumpfile !=NULL) { + } + + //added to create a dumpfile + if (dumpfile !=NULL) { dumphandle = CreateFileA(dumpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if( !dumphandle || dumphandle == INVALID_HANDLE_VALUE ) { - printf("Invalid dump file: %s\n",dumpfile); - return -1; - } + printf("Invalid dump file: %s\n",dumpfile); + return -1; + } } #else fd = open(port, O_RDWR | O_NOCTTY); @@ -306,13 +306,13 @@ int serial_close(int fd) #ifdef WIN32 HANDLE hCom = (HANDLE)fd; - CloseHandle(hCom); - - //close dumpfile if it was created - if (dumpfile !=NULL) { - if (dumphandle != NULL) { - CloseHandle(dumphandle); - } + CloseHandle(hCom); + + //close dumpfile if it was created + if (dumpfile !=NULL) { + if (dumphandle != NULL) { + CloseHandle(dumphandle); + } } #else close(fd); diff --git a/scripts/powertools/framework/serial.h b/scripts/powertools/framework/serial.h index 9dc8a545..a7293021 100644 --- a/scripts/powertools/framework/serial.h +++ b/scripts/powertools/framework/serial.h @@ -1,18 +1,18 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ /* * OS independent serial interface * diff --git a/scripts/powertools/self-test/main.c b/scripts/powertools/self-test/main.c index add6df99..19d609db 100644 --- a/scripts/powertools/self-test/main.c +++ b/scripts/powertools/self-test/main.c @@ -1,311 +1,311 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This 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. - */ - -#include -#include -#include -#include -#include -#include - -#include "..\framework\buspirate.h" -#include "..\framework\serial.h" - - -int modem =FALSE; //set this to TRUE of testing a MODEM -int verbose = 0; -//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. -int dumphandle; // use by dump file when using the -d dumfile.txt parameter -char *dumpfile; - -int print_usage(char * appname) - { - //print usage - printf("\n\n"); - - printf("-------------------------------------------------------------------------\n"); - printf("\n"); - printf(" Usage: \n"); - printf(" %s -d device \n ",appname); - printf("\n"); - printf(" Example Usage: %s COM1 -R \n",appname); - printf("\n"); - printf(" Where: -d device is port e.g. COM1 \n"); - printf(" -R Reduced (short) self-test (no jumpers required) \n"); - printf(" -s Speed is port Speed default is 115200 \n"); - printf("\n"); - - printf("\n"); - - printf("-------------------------------------------------------------------------\n"); - - - return 0; - } - - - -int main(int argc, char** argv) -{ - int opt; - char buffer[256] = {0}, i; - int fd; - int res,c; - int flag=0,firsttime=0; - char *param_port = NULL; - char *param_speed = NULL; - char param_test=1; - -// int clock_edge; -// int polarity; - //default speed - // param_port=strdup("COM3"); //default for debugging - - - printf("-------------------------------------------------------\n"); - printf("\n"); - printf(" Bus Pirate binary mode SELF TEST utility v0.1 (CC-0)\n"); - printf(" http://dangerousprototypes.com\n"); - printf("\n"); - printf("-------------------------------------------------------\n"); - // printf("\n"); - -//p == port -// p=polarity -// e=clock edge - -if (argc <= 1) { - - printf(" Help Menu\n"); - print_usage(argv[0]); - exit(-1); - } - -while ((opt = getopt(argc, argv, "mRs:d:")) != -1) { - // printf("%c \n",opt); - switch (opt) { - - case 'd': // device eg. com1 com12 etc - if ( param_port != NULL){ - printf(" Device/PORT error!\n"); - exit(-1); - } - param_port = strdup(optarg); - break; - case 's': - if (param_speed != NULL) { - printf(" Speed should be set: eg 115200 \n"); - exit(-1); - } - param_speed = strdup(optarg); - - break; - case 'R': //do short self test - param_test = 0; - break; - case 'm': //modem debugging for testing - modem =TRUE; // enable modem mode - break; - - default: - printf(" Invalid argument %c", opt); - print_usage(argv[0]); - //exit(-1); - break; - } - } - // printf(" Press escape to exit \n"); - // printf("\n"); - - - - //param_port=strdup("COM3"); - //Set default if NULL - if (param_port==NULL){ - printf(" No serial port set\n"); - print_usage(argv[0]); - exit(-1); - } - - if (param_speed==NULL) - param_speed=strdup("115200"); - - - printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); - - if(param_test==1){//full self-test - printf(" Full self-test, attach jumpers (+5V to VPU) and (+3.3V to ADC)\n"); - }else{//short test - printf(" !!!Warning, short self-test only!!!\n Many things will NOT be tested!!!\n"); - } - - flag=0; - // - // Loop and repeat test as needed for manufacturing - // - //TO DO: add loop here - printf(" Press Esc to exit, any other key to start the self-test \n\n"); - while(1){ - - //pause for space, or ESC to exit - - if (flag==1) - { - printf("\n--------------------- Starting a new Test-------------------------\n"); - - } - while(1){ - Sleep(1); - if (flag==1){ - flag=0; //has flag been set to just go testing? - break; // proceed with test - } - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - printf("\n Starting test! \n"); - break; - } - } - } - - - - // - // Open serial port - // - - printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); - fd = serial_open(param_port); - if (fd < 0) { - fprintf(stderr, " Error opening serial port\n"); - return -1; - } - - // - // Enter binary mode, then enter a protocol mode - // - serial_setup(fd,(speed_t) param_speed); - - if (modem==TRUE){ // connected to modem for testing response { - - serial_write( fd, "ATI3\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - - serial_write( fd, "ATI4\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - serial_write( fd, "ATI7\x0D\0",5 ); - Sleep(1); - res= serial_read(fd, buffer, sizeof(buffer)); - printf("\n %s\n",buffer); - - - } - else - { - - - fprintf(stderr, " Configuring Bus Pirate...\n"); - //BP_EnableMode(fd, SPI); //enter BBIO then SPI - if(BP_EnableBinary(fd)!=BBIO){ - fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); - return -1; - } - // - //Start self-test - // - printf(" Starting self-test...\n"); - - if(param_test==1){//full self-test - serial_write( fd, "\x11", 1); - }else{//short test - serial_write( fd, "\x10", 1); - } - - // - // Done with setup - // - } - - printf(" Getting self-test result...\n"); - Sleep(1000); // how long to wait to get response? - - res = serial_read(fd, &i, 1); //get number of errors... - - if(i>0){ - printf("\n %u ERRORS!!! WARNING!!! :(\n\n", (uint8_t)i); - printf(" MODE LED should be on!!!\n "); - }else{ - printf("\n Self-test passed :)\n\n"); - printf(" MODE LED should blink!!!\n"); - - } - // pause after the result - - if (firsttime==0){ // run here once and don't say again the next time - printf(" Press any key to continue...\n"); - firsttime=1; - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - break; - } - } - } - - buffer[0]=0xFF;//exit self-test - buffer[1]=0x0f;//exit BBIO - res = serial_write( fd, buffer, 2); - - //close port so they can attach the next Bus Pirate - serial_close(fd); - - - //TODO: Loop back to pause after this - printf("\n Connect another Bus Pirate and press any key to start the self-test again \n"); - printf(" Or hit ESC key to stop and end the test.\n"); - - while(1){ - Sleep(1); - if(kbhit()){ - c = getch(); - if(c == 27){ - printf("\n Esc key hit, stopping...\n"); - printf(" (Bye for now!)\n"); - exit(-1); - }else {//make space only - flag=1; //flag to tell the other loop to bypass another keypress - - break; - } - } - } - } - -#define FREE(x) if(x) free(x); - FREE(param_port); - FREE(param_speed); - return 0; -} +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project and http://dangerousprototypes.com + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "..\framework\buspirate.h" +#include "..\framework\serial.h" + + +int modem =FALSE; //set this to TRUE of testing a MODEM +int verbose = 0; +//int disable_comport = 0; //1 to say yes, disable comport, any value to enable port default is 0 meaning port is enable. +int dumphandle; // use by dump file when using the -d dumfile.txt parameter +char *dumpfile; + +int print_usage(char * appname) + { + //print usage + printf("\n\n"); + + printf("-------------------------------------------------------------------------\n"); + printf("\n"); + printf(" Usage: \n"); + printf(" %s -d device \n ",appname); + printf("\n"); + printf(" Example Usage: %s COM1 -R \n",appname); + printf("\n"); + printf(" Where: -d device is port e.g. COM1 \n"); + printf(" -R Reduced (short) self-test (no jumpers required) \n"); + printf(" -s Speed is port Speed default is 115200 \n"); + printf("\n"); + + printf("\n"); + + printf("-------------------------------------------------------------------------\n"); + + + return 0; + } + + + +int main(int argc, char** argv) +{ + int opt; + char buffer[256] = {0}, i; + int fd; + int res,c; + int flag=0,firsttime=0; + char *param_port = NULL; + char *param_speed = NULL; + char param_test=1; + +// int clock_edge; +// int polarity; + //default speed + // param_port=strdup("COM3"); //default for debugging + + + printf("-------------------------------------------------------\n"); + printf("\n"); + printf(" Bus Pirate binary mode SELF TEST utility v0.1 (CC-0)\n"); + printf(" http://dangerousprototypes.com\n"); + printf("\n"); + printf("-------------------------------------------------------\n"); + // printf("\n"); + +//p == port +// p=polarity +// e=clock edge + +if (argc <= 1) { + + printf(" Help Menu\n"); + print_usage(argv[0]); + exit(-1); + } + +while ((opt = getopt(argc, argv, "mRs:d:")) != -1) { + // printf("%c \n",opt); + switch (opt) { + + case 'd': // device eg. com1 com12 etc + if ( param_port != NULL){ + printf(" Device/PORT error!\n"); + exit(-1); + } + param_port = strdup(optarg); + break; + case 's': + if (param_speed != NULL) { + printf(" Speed should be set: eg 115200 \n"); + exit(-1); + } + param_speed = strdup(optarg); + + break; + case 'R': //do short self test + param_test = 0; + break; + case 'm': //modem debugging for testing + modem =TRUE; // enable modem mode + break; + + default: + printf(" Invalid argument %c", opt); + print_usage(argv[0]); + //exit(-1); + break; + } + } + // printf(" Press escape to exit \n"); + // printf("\n"); + + + + //param_port=strdup("COM3"); + //Set default if NULL + if (param_port==NULL){ + printf(" No serial port set\n"); + print_usage(argv[0]); + exit(-1); + } + + if (param_speed==NULL) + param_speed=strdup("115200"); + + + printf("\n Parameters used: Device = %s, Speed = %s\n\n",param_port,param_speed); + + if(param_test==1){//full self-test + printf(" Full self-test, attach jumpers (+5V to VPU) and (+3.3V to ADC)\n"); + }else{//short test + printf(" !!!Warning, short self-test only!!!\n Many things will NOT be tested!!!\n"); + } + + flag=0; + // + // Loop and repeat test as needed for manufacturing + // + //TO DO: add loop here + printf(" Press Esc to exit, any other key to start the self-test \n\n"); + while(1){ + + //pause for space, or ESC to exit + + if (flag==1) + { + printf("\n--------------------- Starting a new Test-------------------------\n"); + + } + while(1){ + Sleep(1); + if (flag==1){ + flag=0; //has flag been set to just go testing? + break; // proceed with test + } + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + printf("\n Starting test! \n"); + break; + } + } + } + + + + // + // Open serial port + // + + printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed); + fd = serial_open(param_port); + if (fd < 0) { + fprintf(stderr, " Error opening serial port\n"); + return -1; + } + + // + // Enter binary mode, then enter a protocol mode + // + serial_setup(fd,(speed_t) param_speed); + + if (modem==TRUE){ // connected to modem for testing response { + + serial_write( fd, "ATI3\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + + serial_write( fd, "ATI4\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + serial_write( fd, "ATI7\x0D\0",5 ); + Sleep(1); + res= serial_read(fd, buffer, sizeof(buffer)); + printf("\n %s\n",buffer); + + + } + else + { + + + fprintf(stderr, " Configuring Bus Pirate...\n"); + //BP_EnableMode(fd, SPI); //enter BBIO then SPI + if(BP_EnableBinary(fd)!=BBIO){ + fprintf(stderr, " Buspirate cannot switch to binary mode :( \n"); + return -1; + } + // + //Start self-test + // + printf(" Starting self-test...\n"); + + if(param_test==1){//full self-test + serial_write( fd, "\x11", 1); + }else{//short test + serial_write( fd, "\x10", 1); + } + + // + // Done with setup + // + } + + printf(" Getting self-test result...\n"); + Sleep(1000); // how long to wait to get response? + + res = serial_read(fd, &i, 1); //get number of errors... + + if(i>0){ + printf("\n %u ERRORS!!! WARNING!!! :(\n\n", (uint8_t)i); + printf(" MODE LED should be on!!!\n "); + }else{ + printf("\n Self-test passed :)\n\n"); + printf(" MODE LED should blink!!!\n"); + + } + // pause after the result + + if (firsttime==0){ // run here once and don't say again the next time + printf(" Press any key to continue...\n"); + firsttime=1; + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + break; + } + } + } + + buffer[0]=0xFF;//exit self-test + buffer[1]=0x0f;//exit BBIO + res = serial_write( fd, buffer, 2); + + //close port so they can attach the next Bus Pirate + serial_close(fd); + + + //TODO: Loop back to pause after this + printf("\n Connect another Bus Pirate and press any key to start the self-test again \n"); + printf(" Or hit ESC key to stop and end the test.\n"); + + while(1){ + Sleep(1); + if(kbhit()){ + c = getch(); + if(c == 27){ + printf("\n Esc key hit, stopping...\n"); + printf(" (Bye for now!)\n"); + exit(-1); + }else {//make space only + flag=1; //flag to tell the other loop to bypass another keypress + + break; + } + } + } + } + +#define FREE(x) if(x) free(x); + FREE(param_port); + FREE(param_speed); + return 0; +} From 2f4b4af89cafcc39d5043ea6ece5806b8e7cab8e Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Wed, 30 Mar 2016 16:16:13 -0500 Subject: [PATCH 3/5] Introduce gitattributes to prevent wall of pink --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..029e0967 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto From 8ad2657cd333da47faffd6f14e8745f11c932e05 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Wed, 30 Mar 2016 16:18:45 -0500 Subject: [PATCH 4/5] binIO: Fix modular code inclusion --- Firmware/base.h | 1 + Firmware/binIO.c | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Firmware/base.h b/Firmware/base.h index b1b656b0..4ad9016a 100644 --- a/Firmware/base.h +++ b/Firmware/base.h @@ -147,6 +147,7 @@ #define BP_USE_DIO //binary mode #define BP_USE_PCATKB #define BP_USE_PIC + #define BP_USE_JTAG #endif #elif defined(BP_ADDONS) diff --git a/Firmware/binIO.c b/Firmware/binIO.c index 2e97f354..51912258 100644 --- a/Firmware/binIO.c +++ b/Firmware/binIO.c @@ -19,20 +19,30 @@ #include "base.h" #include "bitbang.h" #include "selftest.h" -#include "SPI.h" -#include "I2C.h" -#include "UART.h" -#include "1wire.h" -#include "binwire.h" -#include "OpenOCD.h" -#include "pic.h" + +#ifdef BP_USE_HWSPI + #include "SPI.h" +#endif +#ifdef BP_USE_I2C + #include "I2C.h" +#endif +#ifdef BP_USE_HWUART + #include "UART.h" +#endif +#ifdef BP_USE_1WIRE + #include "1wire.h" +#endif +#ifdef BP_USE_PIC + #include "pic.h" +#endif +#ifdef BP_USE_JTAG + #include "jtag.h" +#endif + #include "binIO.h" #include "AUXpin.h" -//--- Added JM -#ifdef BUSPIRATEV4 -#include "jtag.h" -#endif -//--- End added JM +#include "binwire.h" +#include "OpenOCD.h" From 3c90c421671b4df58358ab99d264fa7a0f7846b5 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Thu, 31 Mar 2016 14:48:17 -0500 Subject: [PATCH 5/5] README: Add a readme to base project --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..6de45429 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# Bus Pirate +The Bus Pirate is an open source hacker multi-tool that talks to electronic stuff. It's got a bunch of features an intrepid hacker might need to prototype their next project. + +## Protocols + +* 1-Wire +* I2C* +* SPI +* JTAG +* Asynchronous serial +* MIDI +* PC keyboard +* HD44780 LCD +* 2- and 3-wire libraries with bitwise pin control +* Scriptable binary bitbang, 1-Wire, I2C, SPI, and UART modes + +## Features + +* 0-5.5volt tolerant pins +* 0-6volt measurement probe +* 1Hz-40MHz frequency measurement +* 1kHz - 4MHz pulse-width modulator, frequency generator +* On-board multi-voltage pull-up resistors +* On-board 3.3volt and 5volt power supplies with software reset +* Macros for common operations +* Bus traffic sniffers (SPI, I2C) +* A bootloader for easy firmware updates +* Transparent USB->serial mode +* 10Hz-1MHz low-speed logic analyzer +* Servo driver +* Can program many AVR microcontrollers + * Supported by AVRdude + * Can emulate the AVR STK500 v2 with alternate ST500 Clone firmware + * Programs FPGAs and CPLDs with alternate XSVF firmware +* Scriptable from Perl, Python, etc. +* Translations (currently Spanish and Italian) +* Public domain (Creative Commons Zero) source. Prototype with the Bus Pirate, then use the code in your project however you want. + +## Application Support +The Bus Pirate is used through a simple terminal interface, but these applications also support the Bus Pirate as a programming device, etc. + +* [AVRDude] AVR programmer (AVRDude v5.8+, firmware v4 (any) or v5.9+) +* [OpenOCD] JTAG debugger +* [flashrom] bios/flash programmer + + +# Purchasing + +* Bus Pirate v4.0 - *Newer, more space for bigger firmwares* + * Worldwide: +* Bus Pirate v3.6 - *Proven, more well tested* + * Worldwide: + * USA: + * USA: + * Germany: + * Europe: + * USA: + * USA: + +[OpenOCD]: http://openocd.org +[AVRDude]: http://www.nongnu.org/avrdude +[flashrom]: https://www.flashrom.org/Flashrom