|
36 | 36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
37 | 37 | */ |
38 | 38 |
|
| 39 | +#include <string.h> |
| 40 | + |
39 | 41 | #include <tiam1808/armv5/am1808/edma_event.h> |
40 | 42 | #include <tiam1808/armv5/am1808/evmAM1808.h> |
41 | 43 | #include <tiam1808/armv5/am1808/interrupt.h> |
|
45 | 47 | #include <tiam1808/hw/hw_syscfg1_AM1808.h> |
46 | 48 | #include <tiam1808/hw/hw_types.h> |
47 | 49 | #include <tiam1808/hw/soc_AM1808.h> |
| 50 | +#include <tiam1808/i2c.h> |
48 | 51 | #include <tiam1808/psc.h> |
49 | 52 |
|
50 | 53 | #include <pbdrv/ioport.h> |
@@ -542,6 +545,11 @@ static void Edma3CCErrHandlerIsr(void) { |
542 | 545 | } |
543 | 546 | } |
544 | 547 |
|
| 548 | +enum { |
| 549 | + BOOT_EEPROM_I2C_ADDRESS = 0x50, |
| 550 | +}; |
| 551 | +uint8_t pbdrv_ev3_bluetooth_mac_address[6]; |
| 552 | + |
545 | 553 | // Called from assembly code in startup.s. After this, the "main" function in |
546 | 554 | // lib/pbio/sys/main.c is called. That contains all calls to the driver |
547 | 555 | // initialization (low level in pbdrv, high level in pbio), and system level |
@@ -587,6 +595,54 @@ void SystemInit(void) { |
587 | 595 | const pbdrv_gpio_t bluetooth_uart_tx = PBDRV_GPIO_EV3_PIN(4, 23, 20, 1, 2); |
588 | 596 | pbdrv_gpio_alt(&bluetooth_uart_rx, SYSCFG_PINMUX4_PINMUX4_19_16_UART2_RXD); |
589 | 597 | 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 | + } |
590 | 646 | } |
591 | 647 |
|
592 | 648 |
|
|
0 commit comments