Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

flush FTDI buffer to avoid 16ms latency timeout #169

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Bootloaders/BPv4-bootloader/firmware-v1/boot_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ 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
Expand Down
12 changes: 8 additions & 4 deletions Bootloaders/BPv4-bootloader/firmware-v1/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BYTE bldone = 0;
extern BYTE cdc_In_buffer[64];
extern BYTE cdc_Out_buffer[64];
#define VER_H 0x04
#define VER_L 0x0a
#define VER_L 0x0c

unsigned int userversion __attribute__((space(prog),address(BLENDADDR-9))) = ((VER_H<<8)|VER_L);

Expand Down Expand Up @@ -110,7 +110,6 @@ void bootloader(void) {
if(crc!=0){
bootstruct.blreturn='N';//return checksum error
goto error;

}

//calculate flash address
Expand All @@ -130,9 +129,14 @@ void bootloader(void) {
WritePage();
break;
case 0xff:
U1CONbits.USBEN=0; //USB off
bootstruct.blreturn = 'K';
cdc_In_buffer[0] = bootstruct.blreturn; //answer OK
putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1);
i=0x7FFF;
while(i--);
U1CONbits.USBEN=0; //USB off
//delay a while so computer sees us turn off USB
j=0xFFFF;
j=0x0010;
while(j--){
i=0xFFFF;
while(i--);
Expand Down
4 changes: 2 additions & 2 deletions Bootloaders/BPv4-bootloader/firmware-v1/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ typedef unsigned char BYTE;

#define LedSetup() TRISBbits.TRISB8=0;TRISBbits.TRISB9=0;TRISBbits.TRISB10=0

#define uLedOn() LATBbits.LATB10 = 0
#define uLedOff() LATBbits.LATB10 = 1
#define uLedOn() LATBbits.LATB10 = 1
#define uLedOff() LATBbits.LATB10 = 0
#define uLedToggle() LATBbits.LATB10 ^= LATBbits.LATB10

#define mLedOn() LATBbits.LATB8 = 1
Expand Down
32 changes: 27 additions & 5 deletions Bootloaders/BPv4-bootloader/firmware-v1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
#include <stdbool.h>
#include <stdint.h>

// Hardware 'NORMAL' button on BPv4 definitions
#define BP_BUTTON_DIR TRISCbits.TRISC14
#define BP_BUTTON PORTCbits.RC14
#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)

#define FIRMWARE_SIGNATURE 0x31415926

bool __attribute__((address(0x47FA), persistent)) skip_pgc_pgd_check;
Expand Down Expand Up @@ -85,17 +97,27 @@ int main(void) {
PGC_OUT = 0;
PGC_TRIS = 0;

if ((firmware_signature != FIRMWARE_SIGNATURE) || !skip_pgc_pgd_check) {
LedSetup();

BP_BUTTON_SETUP();

if ((firmware_signature != FIRMWARE_SIGNATURE) || !skip_pgc_pgd_check || BP_BUTTON_ISDOWN()) {
volatile int i;

skip_pgc_pgd_check = false;

i = 5000;
while (i--)
;
int j = 35;
while (j--) {
i = 65000;
while (i--);
}

vLedOn();
mLedOn();
uLedOn();

for (i = 0; i < 20; i++) {
if ((PGD_IN == 1)) { // go to user space on first mis-match
if ((PGD_IN == 1 && !(BP_BUTTON_ISDOWN()))) { // 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
Expand Down
98 changes: 65 additions & 33 deletions Bootloaders/BPv4-bootloader/pirate-loader/pirate-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#include <fcntl.h>
#include <errno.h>

#define PIRATE_LOADER_VERSION "1.0.3"
#define PIRATE_LOADER_VERSION "1.0.4"

#define STR_EXPAND(tok) #tok
#define OS_NAME(tok) STR_EXPAND(tok)
Expand All @@ -77,7 +77,6 @@ int write(int fd, const void* buf, int len)
int res = 0;
unsigned long bwritten = 0;


res = WriteFile(hCom, buf, len, &bwritten, NULL);

if( res == FALSE )
Expand Down Expand Up @@ -197,8 +196,10 @@ typedef unsigned long uint32;
#endif

#define BOOTLOADER_HELLO_STR "\xC1"
#define BOOTLOADER_OK 0x4B
#define BOOTLOADER_OK 'K'
#define BOOTLOADER_PROT 'P'
#define BOOTLOADER_CHECKSUM 'N'

#define PIC_WORD_SIZE (3)
#define PIC_NUM_ROWS_IN_PAGE 8
#define PIC_NUM_WORDS_IN_ROW 64
Expand Down Expand Up @@ -484,41 +485,43 @@ int sendCommandAndWaitForResponse(int fd, uint8 *command)
res = readWithTimeout(fd, response, 1, 5);
if( res != 1 )
{
puts("ERROR");
puts("ERROR, timeout");
return -1;
}
else if (response[0]== BOOTLOADER_PROT)
{
printf("(SKIPPED by bootloader)...");
return 0;

}
else if ( response[0] != BOOTLOADER_OK )
{
printf("ERROR [%02x]\n", response[0]);
return -1;
}
else
{
return 0;
}
switch (response[0])
{
case BOOTLOADER_OK:
return 0;
break;

case BOOTLOADER_PROT:
printf("(SKIPPED by bootloader)...");
return 0;
break;

case BOOTLOADER_CHECKSUM:
printf("cmd checksum error...\n");
return -1;
break;

default:
printf("ERROR [0x%02x]\n", response[0]);
return -1;
}
}


int sendFirmware(int fd, uint8* data, uint8* pages_used)
{
uint32 u_addr;


uint32 page = 0;
uint32 done = 0;
uint32 row = 0;
uint8 command[256] = {0};


for( page=0; page<PIC_NUM_PAGES; page++)
{

u_addr = page * ( PIC_NUM_WORDS_IN_ROW * 2 * PIC_NUM_ROWS_IN_PAGE );

if( pages_used[page] != 1 )
Expand Down Expand Up @@ -652,7 +655,6 @@ int parseCommandLine(int argc, const char** argv)

for(i=1; i<argc; i++)
{

if( !strncmp(argv[i], "--hex=", 6) )
{
g_hexfile_path = argv[i] + 6;
Expand Down Expand Up @@ -775,12 +777,31 @@ int main (int argc, const char** argv)
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");
{
switch( errno )
{
case EACCES: // permission issues
printf("permission issue\n");
break;

case EEXIST: // file already exists and you used O_CREAT and O_EXCL
printf("already exists issue\n");
break;

case EFAULT: // bad path
printf("bad path issue\n");
break;
default:
printf("unknown failure\n");

}
printf("file read failed\n");

puts("ERROR");
fprintf(stderr, "Could not open %s\n", g_device_path);
goto Error;
}
puts("OK");

printf("Configuring serial port settings...");

Expand Down Expand Up @@ -811,7 +832,7 @@ int main (int argc, const char** argv)

printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]);

printf("Device ID [%02x]:",buffer[0]);
printf("Device ID [0x%02x]:",buffer[0]);
switch(buffer[0])
{
// case 0xd4:
Expand Down Expand Up @@ -958,7 +979,7 @@ int main (int argc, const char** argv)
// __PIC24FJ256GB106__
#define IS_24FJ 1
flashsize = 0x2AC00;
eesizeb = 0;
eesizeb = 0;
blstartaddr = 0x400L;
blendaddr = 0x23FFL;
break;
Expand Down Expand Up @@ -1235,17 +1256,28 @@ int main (int argc, const char** argv)

if( res > 0 )
{
puts("\nFirmware updated successfully :)!");
puts("\nFirmware updated successfully! :)");
//printf("Use screen %s 115200 to verify\n", g_device_path);
}
else
{
puts("\nError updating firmware :(");
goto Error;
}

}

uint8 command[6] = {0};

command[0] = 1; // fake data
command[1] = 2;
command[2] = 3;
command[COMMAND_OFFSET] = 0xff;
command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC
command[PAYLOAD_OFFSET] = makeCrc(command, 5);

//send byebye (cmd: 0xff)
int ret = sendCommandAndWaitForResponse(dev_fd, command);

Finished:
if( bin_buff )
{
Expand Down
3 changes: 3 additions & 0 deletions Firmware/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,9 @@ void user_serial_initialise(void) {
U1STA = 0x0400;

IFS0bits.U1RXIF = NO;

/* FTDI CTS setup. */
FTDI_CTS_DIR = OUTPUT;
}

bool user_serial_transmit_done(void) { return U1STAbits.TRMT; }
Expand Down
10 changes: 6 additions & 4 deletions Firmware/binary_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static inline void handle_clear_pwm(void);
static inline void handle_read_adc_one_shot(void);
static inline void handle_read_adc_continuously(void);
static inline void handle_frequency_measurement(void);
static inline void handle_bitbang_command(const bitbang_command command);
static inline int handle_bitbang_command(const bitbang_command command);

static void read_and_transmit_adc_measurement(void);

Expand Down Expand Up @@ -368,14 +368,15 @@ void enter_binary_bitbang_mode(void) {
uint8_t input_byte = user_serial_read_byte();

if ((input_byte & 0b10000000) == 0) {
handle_bitbang_command((bitbang_command)input_byte);
if (handle_bitbang_command((bitbang_command)input_byte))
break;
} else {
user_serial_transmit_character(bitbang_pin_state_set(input_byte));
}
}
}

void handle_bitbang_command(const bitbang_command command) {
int handle_bitbang_command(const bitbang_command command) {
switch (command) {
case BITBANG_COMMAND_RESET:
send_binary_io_mode_identifier();
Expand Down Expand Up @@ -448,7 +449,7 @@ void handle_bitbang_command(const bitbang_command command) {
user_serial_wait_transmission_done();
#if defined(BUSPIRATEV4)
reset_state();
return;
return -1;
#else
__asm volatile("RESET");
#endif /* BUSPIRATEV4 */
Expand Down Expand Up @@ -500,6 +501,7 @@ void handle_bitbang_command(const bitbang_command command) {
}
break;
}
return 0;
}

void reset_state(void) {
Expand Down
1 change: 1 addition & 0 deletions Firmware/hardwarev4.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@

#define PIC_REV_A3 0x0001
#define PIC_REV_A5 0x0003
#define PIC_REV_A6 0x0004

static inline void bp_enable_usb_led(void) {
BP_LEDUSB_DIR = OUTPUT;
Expand Down
3 changes: 2 additions & 1 deletion Firmware/messages_v3.s
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,8 @@ _MSG_SPI_SAMPLE_PROMPT_str:
.section .text.MSG_SPI_SPEED_PROMPT, code
.global _MSG_SPI_SPEED_PROMPT_str
_MSG_SPI_SPEED_PROMPT_str:
.pasciz "Set speed:\r\n 1. 30KHz\r\n 2. 125KHz\r\n 3. 250KHz\r\n 4. 1MHz\r\n 5. 50KHz\r\n 6. 1.3MHz\r\n 7. 2MHz\r\n 8. 2.6MHz\r\n 9. 3.2MHz\r\n10. 4MHz\r\n11. 5.3MHz\r\n12. 8MHz"
.pasciz "Set speed:\r\n 1. 30kHz\r\n 2. 125kHz\r\n 3. 250kHz\r\n 4. 1MHz\r\n 5. 2MHz\r\n 6. 2.6MHz\r\n 7. 4MHz\r\n 8. 8MHz\r\n 9. 50KHz\r\n10. 1.3MHz\r\n11. 3.2MHz\r\n12. 5.3MHz"


; MSG_UART_BAUD_CALCULATED
.section .text.MSG_UART_BAUD_CALCULATED, code
Expand Down
2 changes: 2 additions & 0 deletions Firmware/messages_v4.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ void MSG_CHIP_REVISION_A3_str(void);
#define MSG_CHIP_REVISION_A3 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A3_str))
void MSG_CHIP_REVISION_A5_str(void);
#define MSG_CHIP_REVISION_A5 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A5_str))
void MSG_CHIP_REVISION_A6_str(void);
#define MSG_CHIP_REVISION_A6 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A6_str))
void MSG_CHIP_REVISION_ID_BEGIN_str(void);
#define MSG_CHIP_REVISION_ID_BEGIN bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_ID_BEGIN_str))
void MSG_CHIP_REVISION_UNKNOWN_str(void);
Expand Down
8 changes: 7 additions & 1 deletion Firmware/messages_v4.s
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,12 @@ _MSG_CHIP_REVISION_A3_str:
_MSG_CHIP_REVISION_A5_str:
.pasciz "A5"

; MSG_CHIP_REVISION_A6
.section .text.MSG_CHIP_REVISION_A6, code
.global _MSG_CHIP_REVISION_A6_str
_MSG_CHIP_REVISION_A6_str:
.pasciz "A6"

; MSG_CHIP_REVISION_ID_BEGIN
.section .text.MSG_CHIP_REVISION_ID_BEGIN, code
.global _MSG_CHIP_REVISION_ID_BEGIN_str
Expand Down Expand Up @@ -1616,7 +1622,7 @@ _MSG_SPI_SAMPLE_PROMPT_str:
.section .text.MSG_SPI_SPEED_PROMPT, code
.global _MSG_SPI_SPEED_PROMPT_str
_MSG_SPI_SPEED_PROMPT_str:
.pasciz "Set speed:\r\n 1. 30KHz\r\n 2. 125KHz\r\n 3. 250KHz\r\n 4. 1MHz\r\n 5. 50KHz\r\n 6. 1.3MHz\r\n 7. 2MHz\r\n 8. 2.6MHz\r\n 9. 3.2MHz\r\n10. 4MHz\r\n11. 5.3MHz\r\n12. 8MHz"
.pasciz "Set speed:\r\n 1. 30kHz\r\n 2. 125kHz\r\n 3. 250kHz\r\n 4. 1MHz\r\n 5. 2MHz\r\n 6. 2.6MHz\r\n 7. 4MHz\r\n 8. 8MHz\r\n 9. 50KHz\r\n10. 1.3MHz\r\n11. 3.2MHz\r\n12. 5.3MHz"

; MSG_UART_BAUD_CALCULATED
.section .text.MSG_UART_BAUD_CALCULATED, code
Expand Down
Loading