Code to load a LPC1114 over tx/rx. I have only tested with a 1114 chip but it should work with other LPC uControllers
Dependencies: DirectoryList MODSERIAL mbed
Fork of ika_shouyu_poppoyaki by
serial_utilities.cpp@28:689c3880e0e4, 2013-09-20 (annotated)
- Committer:
- okano
- Date:
- Fri Sep 20 00:40:08 2013 +0000
- Revision:
- 28:689c3880e0e4
- Parent:
- 26:a63e73885b21
- Child:
- 29:96e28bc1bd99
made function returns error. LED1 and LED2 assigned to toggle by TX and RX
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 21:e149d0bdbf4a | 1 | #include "mbed.h" |
okano | 22:bd98a782fba6 | 2 | #include "serial_utilities.h" |
okano | 21:e149d0bdbf4a | 3 | #include "ika.h" |
okano | 28:689c3880e0e4 | 4 | #include "error_code.h" |
okano | 21:e149d0bdbf4a | 5 | |
okano | 21:e149d0bdbf4a | 6 | Serial pc ( USBTX,USBRX ); |
okano | 21:e149d0bdbf4a | 7 | |
okano | 21:e149d0bdbf4a | 8 | #if 0 |
okano | 21:e149d0bdbf4a | 9 | Serial target( p28, p27 ); |
okano | 21:e149d0bdbf4a | 10 | #else |
okano | 21:e149d0bdbf4a | 11 | #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512 |
okano | 21:e149d0bdbf4a | 12 | #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 512 |
okano | 21:e149d0bdbf4a | 13 | #include "MODSERIAL.h" |
okano | 21:e149d0bdbf4a | 14 | MODSERIAL target( p28, p27 ); // |
okano | 21:e149d0bdbf4a | 15 | #endif |
okano | 21:e149d0bdbf4a | 16 | |
okano | 26:a63e73885b21 | 17 | Timeout timeout; |
okano | 26:a63e73885b21 | 18 | |
okano | 26:a63e73885b21 | 19 | int timeout_flag = 0; |
okano | 26:a63e73885b21 | 20 | |
okano | 26:a63e73885b21 | 21 | |
okano | 21:e149d0bdbf4a | 22 | void set_target_baud_rate( int baud_rate ) |
okano | 21:e149d0bdbf4a | 23 | { |
okano | 21:e149d0bdbf4a | 24 | target.baud( baud_rate ); |
okano | 21:e149d0bdbf4a | 25 | } |
okano | 21:e149d0bdbf4a | 26 | |
okano | 21:e149d0bdbf4a | 27 | |
okano | 21:e149d0bdbf4a | 28 | void usb_serial_bridge_operation( void ) |
okano | 21:e149d0bdbf4a | 29 | { |
okano | 21:e149d0bdbf4a | 30 | while (1) { |
okano | 21:e149d0bdbf4a | 31 | |
okano | 21:e149d0bdbf4a | 32 | if ( pc.readable() ) { |
okano | 21:e149d0bdbf4a | 33 | target.putc( pc.getc() ); |
okano | 21:e149d0bdbf4a | 34 | } |
okano | 21:e149d0bdbf4a | 35 | |
okano | 21:e149d0bdbf4a | 36 | if ( target.readable() ) { |
okano | 21:e149d0bdbf4a | 37 | pc.putc( target.getc() ); |
okano | 21:e149d0bdbf4a | 38 | } |
okano | 21:e149d0bdbf4a | 39 | } |
okano | 21:e149d0bdbf4a | 40 | } |
okano | 21:e149d0bdbf4a | 41 | |
okano | 21:e149d0bdbf4a | 42 | |
okano | 21:e149d0bdbf4a | 43 | void put_string( char *s ) |
okano | 21:e149d0bdbf4a | 44 | { |
okano | 21:e149d0bdbf4a | 45 | char c; |
okano | 21:e149d0bdbf4a | 46 | |
okano | 21:e149d0bdbf4a | 47 | while ( c = *s++ ) { |
okano | 21:e149d0bdbf4a | 48 | target.putc( c ); |
okano | 28:689c3880e0e4 | 49 | toggle_led( 0 ); |
okano | 21:e149d0bdbf4a | 50 | } |
okano | 21:e149d0bdbf4a | 51 | } |
okano | 21:e149d0bdbf4a | 52 | |
okano | 21:e149d0bdbf4a | 53 | |
okano | 21:e149d0bdbf4a | 54 | void put_binary( char *b, int size ) |
okano | 21:e149d0bdbf4a | 55 | { |
okano | 28:689c3880e0e4 | 56 | for ( int i = 0; i < size; i++ ) { |
okano | 21:e149d0bdbf4a | 57 | target.putc( *b++ ); |
okano | 28:689c3880e0e4 | 58 | toggle_led( 0 ); |
okano | 28:689c3880e0e4 | 59 | } |
okano | 21:e149d0bdbf4a | 60 | } |
okano | 21:e149d0bdbf4a | 61 | |
okano | 21:e149d0bdbf4a | 62 | void set_flag() |
okano | 21:e149d0bdbf4a | 63 | { |
okano | 21:e149d0bdbf4a | 64 | timeout_flag = 1; |
okano | 21:e149d0bdbf4a | 65 | } |
okano | 21:e149d0bdbf4a | 66 | |
okano | 21:e149d0bdbf4a | 67 | |
okano | 21:e149d0bdbf4a | 68 | void get_string( char *s ) |
okano | 21:e149d0bdbf4a | 69 | { |
okano | 21:e149d0bdbf4a | 70 | int i = 0; |
okano | 21:e149d0bdbf4a | 71 | char c = 0; |
okano | 21:e149d0bdbf4a | 72 | timeout_flag = 0; |
okano | 21:e149d0bdbf4a | 73 | |
okano | 21:e149d0bdbf4a | 74 | timeout.attach( &set_flag, 1 ); |
okano | 21:e149d0bdbf4a | 75 | |
okano | 21:e149d0bdbf4a | 76 | do { |
okano | 21:e149d0bdbf4a | 77 | do { |
okano | 21:e149d0bdbf4a | 78 | if ( target.readable() ) { |
okano | 21:e149d0bdbf4a | 79 | c = target.getc(); |
okano | 21:e149d0bdbf4a | 80 | |
okano | 21:e149d0bdbf4a | 81 | if ( ( c == '\n') || (c == '\r') ) |
okano | 21:e149d0bdbf4a | 82 | break; |
okano | 21:e149d0bdbf4a | 83 | |
okano | 21:e149d0bdbf4a | 84 | *s++ = c; |
okano | 21:e149d0bdbf4a | 85 | i++; |
okano | 28:689c3880e0e4 | 86 | toggle_led( 1 ); |
okano | 21:e149d0bdbf4a | 87 | } |
okano | 21:e149d0bdbf4a | 88 | |
okano | 21:e149d0bdbf4a | 89 | if ( timeout_flag ) |
okano | 21:e149d0bdbf4a | 90 | return; |
okano | 21:e149d0bdbf4a | 91 | } while ( 1 ); |
okano | 21:e149d0bdbf4a | 92 | } while ( !i ); |
okano | 21:e149d0bdbf4a | 93 | |
okano | 21:e149d0bdbf4a | 94 | *s = '\0'; |
okano | 21:e149d0bdbf4a | 95 | } |
okano | 21:e149d0bdbf4a | 96 | |
okano | 21:e149d0bdbf4a | 97 | |
okano | 21:e149d0bdbf4a | 98 | int get_binary( char *b, int length ) |
okano | 21:e149d0bdbf4a | 99 | { |
okano | 21:e149d0bdbf4a | 100 | int i; |
okano | 21:e149d0bdbf4a | 101 | |
okano | 21:e149d0bdbf4a | 102 | timeout_flag = 0; |
okano | 21:e149d0bdbf4a | 103 | timeout.attach( &set_flag, 1 ); |
okano | 21:e149d0bdbf4a | 104 | |
okano | 21:e149d0bdbf4a | 105 | for ( i = 0; i < length; i++ ) { |
okano | 28:689c3880e0e4 | 106 | if ( target.readable() ) { |
okano | 21:e149d0bdbf4a | 107 | *b++ = target.getc(); |
okano | 28:689c3880e0e4 | 108 | toggle_led( 1 ); |
okano | 28:689c3880e0e4 | 109 | } |
okano | 28:689c3880e0e4 | 110 | |
okano | 21:e149d0bdbf4a | 111 | if ( timeout_flag ) |
okano | 21:e149d0bdbf4a | 112 | return ( i ); |
okano | 21:e149d0bdbf4a | 113 | } |
okano | 21:e149d0bdbf4a | 114 | |
okano | 21:e149d0bdbf4a | 115 | return ( i ); |
okano | 21:e149d0bdbf4a | 116 | } |
okano | 21:e149d0bdbf4a | 117 | |
okano | 21:e149d0bdbf4a | 118 | |
okano | 21:e149d0bdbf4a | 119 | char read_byte( void ) |
okano | 21:e149d0bdbf4a | 120 | { |
okano | 21:e149d0bdbf4a | 121 | while ( !target.readable() ) |
okano | 21:e149d0bdbf4a | 122 | ; |
okano | 21:e149d0bdbf4a | 123 | |
okano | 21:e149d0bdbf4a | 124 | return ( target.getc() ); |
okano | 21:e149d0bdbf4a | 125 | } |
okano | 21:e149d0bdbf4a | 126 | |
okano | 21:e149d0bdbf4a | 127 |