├── cpu_init.h ├── uart_bitbang.h ├── console.h ├── Makefile ├── test.c ├── console.c └── uart_bitbang_msp430.c /cpu_init.h: -------------------------------------------------------------------------------- 1 | /* Initialize CPU/MCU so it run in known and stable execution environment */ 2 | void cpu_init(); 3 | -------------------------------------------------------------------------------- /uart_bitbang.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void uartbb_init(); 4 | void uartbb_autodetect_baud(); 5 | uint8_t uartbb_rx(); 6 | void uartbb_tx(uint8_t c); 7 | 8 | extern uint16_t bit_time; 9 | -------------------------------------------------------------------------------- /console.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define putchar(c) uartbb_tx(c) 4 | void puthex8(uint8_t val); 5 | void puthex16(uint16_t val); 6 | void putdec(uint16_t val); 7 | void putstr(char *s); 8 | void newline(); 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = msp430-gcc 2 | CFLAGS=-Os -g -mmcu=msp430g2231 3 | OBJDUMP = msp430-objdump 4 | 5 | TARGET = test 6 | OBJS = test.o cpu_init_msp430.o uart_bitbang_msp430.o console.o 7 | 8 | all: $(TARGET) 9 | 10 | $(TARGET): $(OBJS) 11 | 12 | program: $(TARGET) 13 | mspdebug rf2500 "prog $^" 14 | 15 | disasm: $(TARGET) 16 | $(OBJDUMP) -dSt $^ >$^.disasm 17 | 18 | clean: 19 | rm -rf *.o 20 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include "uart_bitbang.h" 2 | #include "cpu_init.h" 3 | #include "console.h" 4 | 5 | int main() 6 | { 7 | cpu_init(); 8 | uartbb_init(); 9 | 10 | // We don't use/enable interrupts 11 | //eint(); 12 | 13 | // At this point MCU expected byte 0x55 received form host 14 | // to calibrate baud rate. 15 | uartbb_autodetect_baud(); 16 | putstr("Bit width in clock ticks: 0x"); 17 | puthex16(bit_time); 18 | newline(); 19 | 20 | while (1) { 21 | uint8_t b = uartbb_rx(); 22 | puthex8(b); 23 | newline(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /console.c: -------------------------------------------------------------------------------- 1 | #include "uart_bitbang.h" 2 | 3 | void newline() 4 | { 5 | uartbb_tx('\r'); 6 | uartbb_tx('\n'); 7 | } 8 | 9 | void puthex4(uint8_t b) 10 | { 11 | b += '0'; 12 | if (b > '9') 13 | b += 7; 14 | uartbb_tx(b); 15 | } 16 | 17 | void puthex8(uint8_t b) 18 | { 19 | puthex4(b >> 4); 20 | puthex4(b & 0xf); 21 | } 22 | 23 | void puthex16(uint16_t v) 24 | { 25 | puthex8(v >> 8); 26 | puthex8(v & 0xff); 27 | } 28 | 29 | void putstr(char *s) 30 | { 31 | while (*s) { 32 | uartbb_tx(*s++); 33 | } 34 | } 35 | 36 | void putdec(uint16_t v) 37 | { 38 | char buf[6]; 39 | char *p = buf + 5; 40 | do { 41 | *--p = (v % 10) + '0'; 42 | v = v / 10; 43 | } while (v > 0); 44 | putstr(p); 45 | } 46 | -------------------------------------------------------------------------------- /uart_bitbang_msp430.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "uart_bitbang.h" 3 | 4 | // Allow uart_bitbang_config.h to override port and pins to use for UART 5 | #ifdef USE_CONFIG 6 | #include "uart_bitbang_config.h" 7 | #endif 8 | 9 | #ifndef UART_BITBANG_PIN_TXD 10 | #define UART_BITBANG_PORT_RXD_DIR P1DIR 11 | #define UART_BITBANG_PORT_TXD_DIR P1DIR 12 | #define UART_BITBANG_PORT_RXD P1IN 13 | #define UART_BITBANG_PORT_TXD P1OUT 14 | #define UART_BITBANG_PIN_TXD BIT1 15 | #define UART_BITBANG_PIN_RXD BIT2 16 | #endif 17 | 18 | 19 | uint16_t bit_time; 20 | uint16_t half_bit_time; 21 | 22 | void uartbb_init() 23 | { 24 | #if UART_BITBANG_PORT_RXD_DIR == P2DIR || UART_BITBANG_PORT_TXD_DIR == P2DIR 25 | // Set (entire) P2 as GPIO 26 | P2SEL = 0; 27 | #endif 28 | UART_BITBANG_PORT_RXD_DIR &= ~UART_BITBANG_PIN_RXD; 29 | UART_BITBANG_PORT_TXD_DIR |= UART_BITBANG_PIN_TXD; 30 | // SMCLK, continuous mode 31 | TACTL = TASSEL_2 + MC_2; 32 | } 33 | 34 | void uartbb_autodetect_baud() 35 | { 36 | uint16_t start, stop; 37 | while (UART_BITBANG_PORT_RXD & UART_BITBANG_PIN_RXD); 38 | start = TAR; 39 | while (!(UART_BITBANG_PORT_RXD & UART_BITBANG_PIN_RXD)); 40 | stop = TAR; 41 | bit_time = stop - start; 42 | half_bit_time = bit_time / 2; 43 | } 44 | 45 | void uartbb_tx(uint8_t val) 46 | { 47 | uint16_t start; 48 | uint8_t bit_count = 8; 49 | 50 | start = TAR; 51 | 52 | UART_BITBANG_PORT_TXD &= ~UART_BITBANG_PIN_TXD; 53 | while (TAR - start < bit_time); 54 | 55 | do { 56 | if (val & 1) 57 | UART_BITBANG_PORT_TXD |= UART_BITBANG_PIN_TXD; 58 | else 59 | UART_BITBANG_PORT_TXD &= ~UART_BITBANG_PIN_TXD; 60 | start += bit_time; 61 | while (TAR - start < bit_time); 62 | val >>= 1; 63 | } while (--bit_count); 64 | 65 | UART_BITBANG_PORT_TXD |= UART_BITBANG_PIN_TXD; 66 | start += bit_time; 67 | while (TAR - start < bit_time); 68 | } 69 | 70 | uint8_t uartbb_rx() 71 | { 72 | uint16_t start; 73 | uint8_t val; 74 | uint8_t b = 0, bit_count = 8; 75 | 76 | while (UART_BITBANG_PORT_RXD & UART_BITBANG_PIN_RXD); 77 | start = TAR; 78 | do val = UART_BITBANG_PORT_RXD; while (TAR - start < half_bit_time); 79 | start += half_bit_time; 80 | 81 | do { 82 | do val = UART_BITBANG_PORT_RXD; while (TAR - start < bit_time); 83 | b >>= 1; 84 | if (val & UART_BITBANG_PIN_RXD) 85 | b |= 0x80; 86 | start += bit_time; 87 | } while (--bit_count); 88 | 89 | return b; 90 | } 91 | --------------------------------------------------------------------------------