Skip to content

Commit 59f455b

Browse files
committed
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 b851bc1 commit 59f455b

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>
@@ -535,6 +538,11 @@ static void Edma3CCErrHandlerIsr(void) {
535538
}
536539
}
537540

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

585641

0 commit comments

Comments
 (0)