From 072b5453b483e8d975eb83b99c2cd08314bb0ecb Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Sun, 7 Feb 2021 19:18:44 +0000 Subject: [PATCH] Migrating to clang for native builds on MBP M1 - parts1-6 working, part7 incomplete, part8 untouched --- part2-building/Makefile | 12 +- part2-building/Makefile.gcc | 19 +++ part3-helloworld/Makefile | 12 +- part3-helloworld/Makefile.gcc | 19 +++ part4-miniuart/Makefile | 12 +- part4-miniuart/Makefile.gcc | 19 +++ part5-framebuffer/Makefile | 12 +- part5-framebuffer/Makefile.gcc | 19 +++ part6-breakout/Makefile | 12 +- part6-breakout/Makefile.gcc | 19 +++ part6-breakout/io.c | 4 +- part6-breakout/kernel.c | 15 ++- part7-bluetooth/Makefile | 14 +-- part7-bluetooth/Makefile.gcc | 22 ++++ part7-bluetooth/bt.c | 121 +++++++++++++----- part7-bluetooth/io.h | 1 + part7-bluetooth/kernel.c | 218 ++------------------------------- 17 files changed, 267 insertions(+), 283 deletions(-) create mode 100644 part2-building/Makefile.gcc create mode 100644 part3-helloworld/Makefile.gcc create mode 100644 part4-miniuart/Makefile.gcc create mode 100644 part5-framebuffer/Makefile.gcc create mode 100644 part6-breakout/Makefile.gcc create mode 100644 part7-bluetooth/Makefile.gcc diff --git a/part2-building/Makefile b/part2-building/Makefile index 89235e9..86c7420 100644 --- a/part2-building/Makefile +++ b/part2-building/Makefile @@ -1,19 +1,19 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part2-building/Makefile.gcc b/part2-building/Makefile.gcc new file mode 100644 index 0000000..89235e9 --- /dev/null +++ b/part2-building/Makefile.gcc @@ -0,0 +1,19 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part3-helloworld/Makefile b/part3-helloworld/Makefile index 89235e9..86c7420 100644 --- a/part3-helloworld/Makefile +++ b/part3-helloworld/Makefile @@ -1,19 +1,19 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part3-helloworld/Makefile.gcc b/part3-helloworld/Makefile.gcc new file mode 100644 index 0000000..89235e9 --- /dev/null +++ b/part3-helloworld/Makefile.gcc @@ -0,0 +1,19 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part4-miniuart/Makefile b/part4-miniuart/Makefile index 89235e9..86c7420 100644 --- a/part4-miniuart/Makefile +++ b/part4-miniuart/Makefile @@ -1,19 +1,19 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part4-miniuart/Makefile.gcc b/part4-miniuart/Makefile.gcc new file mode 100644 index 0000000..89235e9 --- /dev/null +++ b/part4-miniuart/Makefile.gcc @@ -0,0 +1,19 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part5-framebuffer/Makefile b/part5-framebuffer/Makefile index 89235e9..86c7420 100644 --- a/part5-framebuffer/Makefile +++ b/part5-framebuffer/Makefile @@ -1,19 +1,19 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part5-framebuffer/Makefile.gcc b/part5-framebuffer/Makefile.gcc new file mode 100644 index 0000000..89235e9 --- /dev/null +++ b/part5-framebuffer/Makefile.gcc @@ -0,0 +1,19 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part6-breakout/Makefile b/part6-breakout/Makefile index 89235e9..86c7420 100644 --- a/part6-breakout/Makefile +++ b/part6-breakout/Makefile @@ -1,19 +1,19 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part6-breakout/Makefile.gcc b/part6-breakout/Makefile.gcc new file mode 100644 index 0000000..89235e9 --- /dev/null +++ b/part6-breakout/Makefile.gcc @@ -0,0 +1,19 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part6-breakout/io.c b/part6-breakout/io.c index e8af328..e06b64d 100644 --- a/part6-breakout/io.c +++ b/part6-breakout/io.c @@ -147,8 +147,8 @@ void uart_writeByteBlocking(unsigned char ch) { void uart_writeText(char *buffer) { while (*buffer) { - if (*buffer == '\n') uart_writeByteBlocking('\r'); - uart_writeByteBlocking(*buffer++); + if (*buffer == '\n') uart_writeByteBlockingActual('\r'); + uart_writeByteBlockingActual(*buffer++); } } diff --git a/part6-breakout/kernel.c b/part6-breakout/kernel.c index 930e2f0..1a6e4ae 100644 --- a/part6-breakout/kernel.c +++ b/part6-breakout/kernel.c @@ -35,8 +35,10 @@ enum { OBJ_BALL = 3 }; +#define OBJS_ADDRESS 0x02100000 // Somewhere safe to store a lot of data + unsigned int numobjs = 0; -struct Object objects[(ROWS * COLS) + (2 * NUM_LIVES)]; +struct Object *objects = (struct Object *)OBJS_ADDRESS; struct Object *ball; struct Object *paddle; @@ -149,13 +151,10 @@ void drawScoreboard(int score, int lives) char tens = score / 10; score -= (10 * tens); char ones = score; - char string[] = "Score: 0xx Lives: x\0\0"; - - string[8] = tens + 0x30; - string[9] = ones + 0x30; - string[20] = (char)lives + 0x30; - - drawString((WIDTH/2)-252, MARGIN-25, string, 0x0f, 3); + drawString((WIDTH/2)-252, MARGIN-25, "Score: 0 Lives: ", 0x0f, 3); + drawChar(tens + 0x30, (WIDTH/2)-252 + (8*8*3), MARGIN-25, 0x0f, 3); + drawChar(ones + 0x30, (WIDTH/2)-252 + (8*9*3), MARGIN-25, 0x0f, 3); + drawChar((char)lives + 0x30, (WIDTH/2)-252 + (8*20*3), MARGIN-25, 0x0f, 3); } void main() diff --git a/part7-bluetooth/Makefile b/part7-bluetooth/Makefile index 3ecc081..143c51b 100644 --- a/part7-bluetooth/Makefile +++ b/part7-bluetooth/Makefile @@ -1,22 +1,22 @@ CFILES = $(wildcard *.c) OFILES = $(CFILES:.c=.o) -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin +LLVMPATH = /opt/homebrew/opt/llvm/bin +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd all: clean kernel8.img boot.o: boot.S - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + clang --target=aarch64-elf $(GCCFLAGS) -c boot.S -o boot.o BCM4345C0.o : BCM4345C0.hcd - $(GCCPATH)/aarch64-none-elf-objcopy -I binary -O elf64-littleaarch64 -B aarch64 $< $@ + $(LLVMPATH)/llvm-objcopy -I binary -O elf64-littleaarch64 -B aarch64 $< $@ %.o: %.c - $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + clang --target=aarch64-elf $(GCCFLAGS) -c $< -o $@ kernel8.img: boot.o $(OFILES) BCM4345C0.o - $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) BCM4345C0.o -T link.ld -o kernel8.elf - $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + $(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot.o $(OFILES) BCM4345C0.o -T link.ld -o kernel8.elf + $(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img clean: /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part7-bluetooth/Makefile.gcc b/part7-bluetooth/Makefile.gcc new file mode 100644 index 0000000..3ecc081 --- /dev/null +++ b/part7-bluetooth/Makefile.gcc @@ -0,0 +1,22 @@ +CFILES = $(wildcard *.c) +OFILES = $(CFILES:.c=.o) +GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCPATH = ../../gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf/bin + +all: clean kernel8.img + +boot.o: boot.S + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c boot.S -o boot.o + +BCM4345C0.o : BCM4345C0.hcd + $(GCCPATH)/aarch64-none-elf-objcopy -I binary -O elf64-littleaarch64 -B aarch64 $< $@ + +%.o: %.c + $(GCCPATH)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $@ + +kernel8.img: boot.o $(OFILES) BCM4345C0.o + $(GCCPATH)/aarch64-none-elf-ld -nostdlib -nostartfiles boot.o $(OFILES) BCM4345C0.o -T link.ld -o kernel8.elf + $(GCCPATH)/aarch64-none-elf-objcopy -O binary kernel8.elf kernel8.img + +clean: + /bin/rm kernel8.elf *.o *.img > /dev/null 2> /dev/null || true diff --git a/part7-bluetooth/bt.c b/part7-bluetooth/bt.c index c91c854..0a1d6ad 100644 --- a/part7-bluetooth/bt.c +++ b/part7-bluetooth/bt.c @@ -1,6 +1,8 @@ #include "io.h" #include "fb.h" +unsigned char *params = (unsigned char *)SAFE_ADDRESS; + // UART0 enum { @@ -151,11 +153,12 @@ void bt_loadfirmware() unsigned int size = (long)&_binary_BCM4345C0_hcd_size; while (c < size) { - unsigned char opcodebytes[] = { _binary_BCM4345C0_hcd_start[c], _binary_BCM4345C0_hcd_start[c+1] }; + params[0] = _binary_BCM4345C0_hcd_start[c]; + params[1] = _binary_BCM4345C0_hcd_start[c+1]; unsigned char length = _binary_BCM4345C0_hcd_start[c+2]; unsigned char *data = &(_binary_BCM4345C0_hcd_start[c+3]); - if (hciCommandBytes(opcodebytes, data, length)) { + if (hciCommandBytes(params, data, length)) { uart_writeText("Firmware data load failed\n"); break; } @@ -167,13 +170,23 @@ void bt_loadfirmware() void bt_setbaud() { - static unsigned char params[] = { 0, 0, 0x00, 0xc2, 0x01, 0x00 }; // little endian, 115200 + params[0] = 0; + params[1] = 0; + params[2] = 0x00; + params[3] = 0xc2; + params[4] = 0x01; + params[5] = 0x00; // little endian, 115200 if (hciCommand(OGF_VENDOR, COMMAND_SET_BAUD, params, 6)) uart_writeText("bt_setbaud() failed\n"); } void bt_setbdaddr() { - static unsigned char params[] = { 0xee, 0xff, 0xc0, 0xee, 0xff, 0xc0 }; // reversed + params[0] = 0xee; + params[1] = 0xff; + params[2] = 0xc0; + params[3] = 0xee; + params[4] = 0xff; + params[5] = 0xc0; // reversed if (hciCommand(OGF_VENDOR, COMMAND_SET_BDADDR, params, 6)) uart_writeText("bt_setbdaddr() failed\n"); } @@ -213,7 +226,11 @@ void sendACLsubscribe(unsigned int handle) bt_writeByte(lo(channel)); bt_writeByte(hi(channel)); - unsigned char params[] = { 0x12, 0x2b, 0x00, 0x01, 0x00 }; + params[0] = 0x12; + params[1] = 0x2b; + params[2] = 0x00; + params[3] = 0x01; + params[4] = 0x00; unsigned int c=0; while (c++ MAX_MSG_LEN) poll_state = 0; - else { - poll_state = 3; - data_len = byte; - } - break; - default: - data_buf[poll_state - 3] = byte; - if (poll_state == data_len + 3 - 1) { - messages_received++; - poll_state = 0; - } else poll_state++; - } -} - -unsigned char *hci_poll() -{ - unsigned int goal = messages_received + 1; - - if (bt_isReadByteReady()) { - unsigned int run = 0; - - while (run < MAX_READ_RUN && messages_received < goal && bt_isReadByteReady()) { - unsigned char byte = bt_readByte(); - hci_poll2(byte); - run++; - } - if (run == MAX_READ_RUN) return 0; - else return data_buf; - } - return 0; -} - -void bt_search() -{ - unsigned char *buf; - - while ( (buf = hci_poll()) ) { - if (data_len >= 2) { - if (buf[0] == LE_ADREPORT_CODE) { - unsigned char numreports = buf[1]; - - if (numreports == 1) { - unsigned char event_type = buf[2]; - - if (event_type == 0x00) { - unsigned char buf_len = buf[10]; - unsigned char ad_len = buf[11]; - - if (ad_len < data_len && buf_len + 11 == data_len - 1) { - for (int c=9;c>=4;c--) echo_addr[9-c] = buf[c]; - buf += 11; - - got_echo_sid = 0; got_echo_name = 0; // Reset the search state machine - do { - ad_len = buf[0]; - unsigned char ad_type = buf[1]; - buf += 2; - - if (ad_len >= 2) { - if (ad_type == 0x03) { - unsigned int sid=0; - - for (int d=0;d= 2) { - if (buf[0] == LE_CONNECT_CODE && !connected) { - connected = !buf[1]; - uart_hex(connected); uart_writeText(" "); - connection_handle = buf[2] | (buf[3] << 8); - uart_hex(connection_handle); uart_writeText(" "); - } - } - } -} - -void acl_poll() -{ - while (bt_isReadByteReady()) { - unsigned char byte = bt_readByte(); - - if (byte == HCI_EVENT_PKT) { - unsigned char opcode = bt_waitReadByte(); - unsigned char length = bt_waitReadByte(); - for (int i=0;i> 4; - - h1 = bt_waitReadByte(); - h2 = bt_waitReadByte(); - - unsigned int length = h1 | (h2 << 8); - unsigned char data[length]; - - for (int i=0;i> reset: "); bt_reset(); + uart_writeText(">> firmware load: "); bt_loadfirmware(); + uart_writeText(">> set baud: "); bt_setbaud(); + uart_writeText(">> set bdaddr: "); bt_setbdaddr(); // Print the BD_ADDR @@ -205,30 +22,15 @@ void main() for (int c=5;c>=0;c--) uart_byte(local_addr[c]); uart_writeText("\n"); - // Start scanning for echo + // Start advertising + uart_writeText("Setting event mask... "); setLEeventmask(0xff); - startActiveScanning(); - uart_writeText("Waiting for echo: "); - while (!(got_echo_sid && got_echo_name)) bt_search(); - stopScanning(); - for (int c=0;c<=5;c++) uart_byte(echo_addr[c]); - uart_writeText("\n"); + uart_writeText("Starting advertsing... "); + startActiveAdvertising(); - // Ask to connect to the echo - uart_writeText("Connecting to echo: "); - connect(echo_addr); - while (!connected) bt_conn(); - uart_writeText("\n"); - - // Get the characteristic value - uart_writeText("Sending read request: "); - uart_hex(connection_handle); uart_writeText("\n"); - sendACLsubscribe(connection_handle); - - // Into the main infinite loop - uart_writeText("Waiting for input...\n"); + // Enter an infinite loop + uart_writeText("Going loopy..."); while (1) { - acl_poll(); uart_update(); } }