|
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> |
@@ -535,6 +538,11 @@ static void Edma3CCErrHandlerIsr(void) { |
535 | 538 | } |
536 | 539 | } |
537 | 540 |
|
| 541 | +enum { |
| 542 | + BOOT_EEPROM_I2C_ADDRESS = 0x50, |
| 543 | +}; |
| 544 | +uint8_t pbdrv_ev3_bluetooth_mac_address[6]; |
| 545 | + |
538 | 546 | // Called from assembly code in startup.s. After this, the "main" function in |
539 | 547 | // lib/pbio/sys/main.c is called. That contains all calls to the driver |
540 | 548 | // initialization (low level in pbdrv, high level in pbio), and system level |
@@ -580,6 +588,54 @@ void SystemInit(void) { |
580 | 588 | const pbdrv_gpio_t bluetooth_uart_tx = PBDRV_GPIO_EV3_PIN(4, 23, 20, 1, 2); |
581 | 589 | pbdrv_gpio_alt(&bluetooth_uart_rx, SYSCFG_PINMUX4_PINMUX4_19_16_UART2_RXD); |
582 | 590 | 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 | + } |
583 | 639 | } |
584 | 640 |
|
585 | 641 |
|
|
0 commit comments