Skip to content

Commit 343793b

Browse files
ArcaneNibbledlech
authored andcommitted
pbio/platform/ev3: Read Bluetooth MAC address from boot EEPROM
This is needed for BT as well as to have a unique serial number for USB
1 parent d76f61a commit 343793b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lib/pbio/platform/ev3/platform.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
*/
3838

39+
#include <string.h>
40+
3941
#include <tiam1808/armv5/am1808/edma_event.h>
4042
#include <tiam1808/armv5/am1808/evmAM1808.h>
4143
#include <tiam1808/armv5/am1808/interrupt.h>
@@ -45,6 +47,7 @@
4547
#include <tiam1808/hw/hw_syscfg1_AM1808.h>
4648
#include <tiam1808/hw/hw_types.h>
4749
#include <tiam1808/hw/soc_AM1808.h>
50+
#include <tiam1808/i2c.h>
4851
#include <tiam1808/psc.h>
4952

5053
#include <pbdrv/ioport.h>
@@ -542,6 +545,11 @@ static void Edma3CCErrHandlerIsr(void) {
542545
}
543546
}
544547

548+
enum {
549+
BOOT_EEPROM_I2C_ADDRESS = 0x50,
550+
};
551+
uint8_t pbdrv_ev3_bluetooth_mac_address[6];
552+
545553
// Called from assembly code in startup.s. After this, the "main" function in
546554
// lib/pbio/sys/main.c is called. That contains all calls to the driver
547555
// initialization (low level in pbdrv, high level in pbio), and system level
@@ -587,6 +595,54 @@ void SystemInit(void) {
587595
const pbdrv_gpio_t bluetooth_uart_tx = PBDRV_GPIO_EV3_PIN(4, 23, 20, 1, 2);
588596
pbdrv_gpio_alt(&bluetooth_uart_rx, SYSCFG_PINMUX4_PINMUX4_19_16_UART2_RXD);
589597
pbdrv_gpio_alt(&bluetooth_uart_tx, SYSCFG_PINMUX4_PINMUX4_23_20_UART2_TXD);
598+
599+
// Read the EV3 Bluetooth MAC address from the I2C boot EEPROM
600+
601+
// Set up pin mux
602+
const pbdrv_gpio_t i2c_scl = PBDRV_GPIO_EV3_PIN(4, 11, 8, 1, 5);
603+
const pbdrv_gpio_t i2c_sda = PBDRV_GPIO_EV3_PIN(4, 15, 12, 1, 4);
604+
pbdrv_gpio_alt(&i2c_scl, SYSCFG_PINMUX4_PINMUX4_11_8_I2C0_SCL);
605+
pbdrv_gpio_alt(&i2c_sda, SYSCFG_PINMUX4_PINMUX4_15_12_I2C0_SDA);
606+
// Reset I2C
607+
I2CMasterDisable(SOC_I2C_0_REGS);
608+
// Configure I2C bus speed to 100 kHz
609+
I2CMasterInitExpClk(SOC_I2C_0_REGS, SOC_ASYNC_2_FREQ, 8000000, 100000);
610+
// Configure I2C to be in master mode
611+
I2CMasterControl(SOC_I2C_0_REGS, I2C_CFG_MST_TX);
612+
// Un-reset I2C
613+
I2CMasterEnable(SOC_I2C_0_REGS);
614+
615+
// Send EEPROM address of 0x3f00
616+
I2CMasterSlaveAddrSet(SOC_I2C_0_REGS, BOOT_EEPROM_I2C_ADDRESS);
617+
I2CSetDataCount(SOC_I2C_0_REGS, 2);
618+
I2CMasterStart(SOC_I2C_0_REGS);
619+
while (!(I2CMasterIntStatus(SOC_I2C_0_REGS) & I2C_ICSTR_ICXRDY)) {
620+
}
621+
I2CMasterDataPut(SOC_I2C_0_REGS, 0x3f);
622+
while (!(I2CMasterIntStatus(SOC_I2C_0_REGS) & I2C_ICSTR_ICXRDY)) {
623+
}
624+
I2CMasterDataPut(SOC_I2C_0_REGS, 0x00);
625+
626+
// Get 12 bytes
627+
uint8_t i2c_buf[12];
628+
HWREG(SOC_I2C_0_REGS + I2C_ICMDR) &= ~I2C_ICMDR_TRX;
629+
I2CSetDataCount(SOC_I2C_0_REGS, sizeof(i2c_buf));
630+
I2CMasterStart(SOC_I2C_0_REGS);
631+
for (unsigned int i = 0; i < sizeof(i2c_buf); i++) {
632+
while (!(I2CMasterIntStatus(SOC_I2C_0_REGS) & I2C_ICSTR_ICRRDY)) {
633+
}
634+
i2c_buf[i] = I2CMasterDataGet(SOC_I2C_0_REGS);
635+
}
636+
637+
// Check for presence of hardware version number according to notes at
638+
// https://www.ev3dev.org/docs/kernel-hackers-notebook/ev3-eeprom/
639+
unsigned int b0 = i2c_buf[0];
640+
unsigned int b1 = i2c_buf[1] ^ 0xff;
641+
if (b0 == b1) {
642+
memcpy(pbdrv_ev3_bluetooth_mac_address, &i2c_buf[6], 6);
643+
} else {
644+
memcpy(pbdrv_ev3_bluetooth_mac_address, &i2c_buf[0], 6);
645+
}
590646
}
591647

592648

0 commit comments

Comments
 (0)