| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2018-2019 Damien P. George |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include "py/mphal.h" |
| 29 |
#include "storage.h" |
| 30 |
|
| 31 |
#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx) |
| 32 |
#define OTP_ADDR (0x1ff079e0) |
| 33 |
#else |
| 34 |
#define OTP_ADDR (0x1ff0f3c0) |
| 35 |
#endif |
| 36 |
#define OTP ((pyb_otp_t *)OTP_ADDR) |
| 37 |
|
| 38 |
typedef struct _pyb_otp_t { |
| 39 |
uint16_t series; |
| 40 |
uint16_t rev; |
| 41 |
uint8_t mac[6]; |
| 42 |
} pyb_otp_t; |
| 43 |
|
| 44 |
void mboot_board_early_init(void) { |
| 45 |
// Enable 500mA on WBUS-DIP28 |
| 46 |
mp_hal_pin_config(pyb_pin_W23, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_UP, 0); |
| 47 |
|
| 48 |
#if defined(MBOOT_SDCARD_ADDR) |
| 49 |
// Configure EN_3V3 as an output pin so that SD card can be used |
| 50 |
mp_hal_pin_config(pyb_pin_EN_3V3, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0); |
| 51 |
#endif |
| 52 |
} |
| 53 |
|
| 54 |
void board_early_init(void) { |
| 55 |
// Configure EN_3V3 as an output pin, but keep it turned off |
| 56 |
mp_hal_pin_config(pyb_pin_EN_3V3, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0); |
| 57 |
|
| 58 |
// Explicitly init SPI2 because it's not enabled as a block device |
| 59 |
spi_bdev_ioctl(&spi_bdev2, BDEV_IOCTL_INIT, (uint32_t)&spiflash2_config); |
| 60 |
} |
| 61 |
|
| 62 |
#if !BUILDING_MBOOT |
| 63 |
|
| 64 |
#include "boardctrl.h" |
| 65 |
#include "qspi.h" |
| 66 |
|
| 67 |
int board_run_boot_py(boardctrl_state_t *state) { |
| 68 |
// Due to errata 2.4.3, restart memory-mapped mode to deactivate the CS line and save power. |
| 69 |
qspi_memory_map_restart(); |
| 70 |
|
| 71 |
return boardctrl_run_boot_py(state); |
| 72 |
} |
| 73 |
|
| 74 |
void board_sleep(int value) { |
| 75 |
mp_spiflash_deepsleep(&spi_bdev.spiflash, value); |
| 76 |
mp_spiflash_deepsleep(&spi_bdev2.spiflash, value); |
| 77 |
} |
| 78 |
|
| 79 |
void mp_hal_get_mac(int idx, uint8_t buf[6]) { |
| 80 |
// Check if OTP region has a valid MAC address, and use it if it does |
| 81 |
if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') { |
| 82 |
#if __GNUC__ >= 11 |
| 83 |
#pragma GCC diagnostic push |
| 84 |
#pragma GCC diagnostic ignored "-Warray-bounds" |
| 85 |
#pragma GCC diagnostic ignored "-Wstringop-overread" |
| 86 |
#endif |
| 87 |
memcpy(buf, OTP->mac, 6); |
| 88 |
#if __GNUC__ >= 11 |
| 89 |
#pragma GCC diagnostic pop |
| 90 |
#endif |
| 91 |
buf[5] += idx; |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// Generate a random locally administered MAC address (LAA) |
| 96 |
mp_hal_generate_laa_mac(idx, buf); |
| 97 |
} |
| 98 |
|
| 99 |
#endif |