From 6dd0ab424e404691e6935378a8fcabf2fc71876a Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Sun, 14 Feb 2021 20:32:02 +0000 Subject: [PATCH] part8 no works with DEBUG=0 in fb.c, but Eddystone beacon in part7 does not --- part7-bluetooth/bt.c | 197 ++++++++++++++------------------- part7-bluetooth/kernel.c | 31 +++--- part8-breakout-ble/bt.c | 198 ++++++++++++++-------------------- part8-breakout-ble/fb.c | 2 +- part8-breakout-ble/kernel.c | 37 ++++--- part8-breakout-ble/terminal.h | 4 +- 6 files changed, 197 insertions(+), 272 deletions(-) diff --git a/part7-bluetooth/bt.c b/part7-bluetooth/bt.c index 800f5b4..a8b3add 100644 --- a/part7-bluetooth/bt.c +++ b/part7-bluetooth/bt.c @@ -1,8 +1,6 @@ #include "io.h" #include "fb.h" -volatile unsigned char *params = (unsigned char *)SAFE_ADDRESS; - // UART0 enum { @@ -91,9 +89,7 @@ enum { LL_ADV_NONCONN_IND = 0x03 }; -unsigned char empty[] = {}; - -int hciCommandBytes(volatile unsigned char *opcodebytes, volatile unsigned char *data, unsigned char length) +int hciCommandBytes(unsigned char *opcodebytes, volatile unsigned char *data, unsigned char length) { unsigned char c=0; @@ -139,11 +135,13 @@ int hciCommand(unsigned short ogf, unsigned short ocf, volatile unsigned char *d } void bt_reset() { + volatile unsigned char empty[] = {}; if (hciCommand(OGF_HOST_CONTROL, COMMAND_RESET_CHIP, empty, 0)) uart_writeText("bt_reset() failed\n"); } void bt_loadfirmware() { + volatile unsigned char empty[] = {}; if (hciCommand(OGF_VENDOR, COMMAND_LOAD_FIRMWARE, empty, 0)) uart_writeText("loadFirmware() failed\n"); extern unsigned char _binary_BCM4345C0_hcd_start[]; @@ -152,17 +150,23 @@ void bt_loadfirmware() unsigned int c=0; unsigned int size = (long)&_binary_BCM4345C0_hcd_size; - while (c < size) { - 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]); + unsigned char opcodebytes[2]; + unsigned char length; + unsigned char *data = &(_binary_BCM4345C0_hcd_start[0]); - if (hciCommandBytes(params, data, length)) { + while (c < size) { + opcodebytes[0] = *data; + opcodebytes[1] = *(data+1); + length = *(data+2); + data += 3; + + if (hciCommandBytes(opcodebytes, data, length)) { uart_writeText("Firmware data load failed\n"); break; } - c += 3 + length; + + data += length; + c += 3 + length; } wait_msec(0x100000); @@ -170,24 +174,14 @@ void bt_loadfirmware() void bt_setbaud() { - 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"); + volatile unsigned char command[6] = { 0, 0, 0x00, 0xc2, 0x01, 0x00 }; // little endian, 115200 + if (hciCommand(OGF_VENDOR, COMMAND_SET_BAUD, command, 6)) uart_writeText("bt_setbaud() failed\n"); } void bt_setbdaddr() { - 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"); + volatile unsigned char command[6] = { 0xee, 0xff, 0xc0, 0xee, 0xff, 0xc0 }; // reversed + if (hciCommand(OGF_VENDOR, COMMAND_SET_BDADDR, command, 6)) uart_writeText("bt_setbdaddr() failed\n"); } void bt_getbdaddr(unsigned char *bdaddr) { @@ -226,84 +220,72 @@ void sendACLsubscribe(unsigned int handle) bt_writeByte(lo(channel)); bt_writeByte(hi(channel)); - params[0] = 0x12; - params[1] = 0x2b; - params[2] = 0x00; - params[3] = 0x01; - params[4] = 0x00; + volatile unsigned char command[5] = { 0x12, 0x2b, 0x00, 0x01, 0x00 }; unsigned int c=0; - while (c++= 2) { - if (buf[0] == LE_CONNECT_CODE && !connected) { - connected = !buf[1]; - debughex(connected); debugstr(" "); - connection_handle = buf[2] | (buf[3] << 8); - debughex(connection_handle); debugstr(" "); - } + if (!connected && data_len >= 2 && buf[0] == LE_CONNECT_CODE) { + connected = !*(buf+1); + debughex(connected); debugstr(" "); + connection_handle = *(buf+2) | (*(buf+3) << 8); + debughex(connection_handle); debugstr(" "); + + if (connection_handle == 0) wait_msec(0x186A); } } } @@ -157,13 +157,14 @@ void acl_poll() unsigned char length = bt_waitReadByte(); for (int i=0;i 7) { @@ -173,9 +174,9 @@ void acl_poll() unsigned int channel = data[2] | (data[3] << 8); unsigned char opcode = data[4]; - if (length == 4 && opcode == 0x1b) { + if (thandle == connection_handle && length == 4 && opcode == 0x1b) { if (channel == 4 && data[5] == 0x2a && data[6] == 0x00) { - debugcrlf(); + debugcrlf(); debugstr("Got ACL packet... "); debugch(data[7]); } @@ -224,7 +225,7 @@ void run_eddystone(void) { // Start advertising debugstr("Setting event mask... "); setLEeventmask(0xff); - debugstr("Starting advertsing... "); + debugstr("Starting advertising... "); startActiveAdvertising(); // Enter an infinite loop diff --git a/part8-breakout-ble/bt.c b/part8-breakout-ble/bt.c index 5fbf048..a8b3add 100644 --- a/part8-breakout-ble/bt.c +++ b/part8-breakout-ble/bt.c @@ -1,8 +1,6 @@ #include "io.h" #include "fb.h" -volatile unsigned char *params = (unsigned char *)SAFE_ADDRESS; - // UART0 enum { @@ -91,9 +89,7 @@ enum { LL_ADV_NONCONN_IND = 0x03 }; -unsigned char empty[] = {}; - -int hciCommandBytes(volatile unsigned char *opcodebytes, volatile unsigned char *data, unsigned char length) +int hciCommandBytes(unsigned char *opcodebytes, volatile unsigned char *data, unsigned char length) { unsigned char c=0; @@ -139,11 +135,13 @@ int hciCommand(unsigned short ogf, unsigned short ocf, volatile unsigned char *d } void bt_reset() { + volatile unsigned char empty[] = {}; if (hciCommand(OGF_HOST_CONTROL, COMMAND_RESET_CHIP, empty, 0)) uart_writeText("bt_reset() failed\n"); } void bt_loadfirmware() { + volatile unsigned char empty[] = {}; if (hciCommand(OGF_VENDOR, COMMAND_LOAD_FIRMWARE, empty, 0)) uart_writeText("loadFirmware() failed\n"); extern unsigned char _binary_BCM4345C0_hcd_start[]; @@ -152,17 +150,23 @@ void bt_loadfirmware() unsigned int c=0; unsigned int size = (long)&_binary_BCM4345C0_hcd_size; - while (c < size) { - 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]); + unsigned char opcodebytes[2]; + unsigned char length; + unsigned char *data = &(_binary_BCM4345C0_hcd_start[0]); - if (hciCommandBytes(params, data, length)) { + while (c < size) { + opcodebytes[0] = *data; + opcodebytes[1] = *(data+1); + length = *(data+2); + data += 3; + + if (hciCommandBytes(opcodebytes, data, length)) { uart_writeText("Firmware data load failed\n"); break; } - c += 3 + length; + + data += length; + c += 3 + length; } wait_msec(0x100000); @@ -170,24 +174,14 @@ void bt_loadfirmware() void bt_setbaud() { - 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"); + volatile unsigned char command[6] = { 0, 0, 0x00, 0xc2, 0x01, 0x00 }; // little endian, 115200 + if (hciCommand(OGF_VENDOR, COMMAND_SET_BAUD, command, 6)) uart_writeText("bt_setbaud() failed\n"); } void bt_setbdaddr() { - 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"); + volatile unsigned char command[6] = { 0xee, 0xff, 0xc0, 0xee, 0xff, 0xc0 }; // reversed + if (hciCommand(OGF_VENDOR, COMMAND_SET_BDADDR, command, 6)) uart_writeText("bt_setbdaddr() failed\n"); } void bt_getbdaddr(unsigned char *bdaddr) { @@ -226,85 +220,72 @@ void sendACLsubscribe(unsigned int handle) bt_writeByte(lo(channel)); bt_writeByte(hi(channel)); - params[0] = 0x12; - params[1] = 0x2b; - params[2] = 0x00; - params[3] = 0x01; - params[4] = 0x00; + volatile unsigned char command[5] = { 0x12, 0x2b, 0x00, 0x01, 0x00 }; unsigned int c=0; - - while (c++= 2) { - if (buf[0] == LE_CONNECT_CODE && !connected) { - connected = !buf[1]; - debughex(connected); debugstr(" "); - connection_handle = buf[2] | (buf[3] << 8); - debughex(connection_handle); debugstr(" "); - } + if (!connected && data_len >= 2 && buf[0] == LE_CONNECT_CODE) { + connected = !*(buf+1); + debughex(connected); debugstr(" "); + connection_handle = *(buf+2) | (*(buf+3) << 8); + debughex(connection_handle); debugstr(" "); + + if (connection_handle == 0) wait_msec(0x186A); } } } @@ -182,13 +182,11 @@ enum { OBJ_BALL = 3 }; -#define OBJS_ADDRESS 0x02200000 // Somewhere safe to store a lot of data - unsigned int numobjs = 0; -struct Object *objects = (struct Object *)OBJS_ADDRESS; +struct Object *objects = (struct Object *)SAFE_ADDRESS; struct Object *ball; struct Object *paddle; -int paddlewidth = 80; +const int paddlewidth = 80; void removeObject(struct Object *object) { @@ -227,7 +225,7 @@ void initBricks() int brickwidth = 32; int brickheight = 8; int brickspacer = 20; - static int brickcols[] = { 0x11, 0x22, 0xEE, 0x44, 0x66 }; + const int brickcols[] = { 0x11, 0x22, 0xEE, 0x44, 0x66 }; int ybrick = MARGIN + brickheight; @@ -305,13 +303,14 @@ void acl_poll() unsigned char length = bt_readByte(); for (int i=0;i 7) { @@ -321,7 +320,7 @@ void acl_poll() unsigned int channel = data[2] | (data[3] << 8); unsigned char opcode = data[4]; - if (length == 4 && opcode == 0x1b) { + if (thandle == connection_handle && length == 4 && opcode == 0x1b) { if (channel == 4 && data[5] == 0x2a && data[6] == 0x00) { dir = data[7]; moveObjectAbs(paddle, MARGIN + (dir * ((VIRTWIDTH - paddlewidth + MARGIN)/100)), paddle->y); @@ -442,7 +441,7 @@ void main() // Connecting to echo debugstr("Connecting to echo: "); connect(echo_addr); - while (!connected) bt_conn(); + while (!(connected && connection_handle)) bt_conn(); debugstr("Connected!"); debugcrlf(); diff --git a/part8-breakout-ble/terminal.h b/part8-breakout-ble/terminal.h index ef05b85..7f7a46d 100644 --- a/part8-breakout-ble/terminal.h +++ b/part8-breakout-ble/terminal.h @@ -1,4 +1,4 @@ -unsigned int vgapal[] = { +const unsigned int vgapal[16] = { 0x000000, 0x0000AA, 0x00AA00, @@ -25,7 +25,7 @@ enum { FONT_NUMGLYPHS = 224 }; -unsigned char font[FONT_NUMGLYPHS][FONT_BPG] = { +const unsigned char font[FONT_NUMGLYPHS][FONT_BPG] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0000 (nul) { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0001 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0002