2015-02-13 15:04:53 +00:00
|
|
|
#include <string.h>
|
2014-10-23 14:25:32 +01:00
|
|
|
|
2016-10-07 03:58:25 +01:00
|
|
|
#include "py/runtime.h"
|
2019-06-19 05:02:38 +01:00
|
|
|
#include "py/stream.h"
|
2016-05-10 23:22:54 +01:00
|
|
|
#include "py/mperrno.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2017-10-19 04:16:42 +01:00
|
|
|
#include "extmod/misc.h"
|
2014-11-27 16:58:31 +00:00
|
|
|
#include "usb.h"
|
2015-02-13 15:04:53 +00:00
|
|
|
#include "uart.h"
|
2014-10-23 14:25:32 +01:00
|
|
|
|
|
|
|
// this table converts from HAL_StatusTypeDef to POSIX errno
|
|
|
|
const byte mp_hal_status_to_errno_table[4] = {
|
|
|
|
[HAL_OK] = 0,
|
2016-05-10 23:22:54 +01:00
|
|
|
[HAL_ERROR] = MP_EIO,
|
|
|
|
[HAL_BUSY] = MP_EBUSY,
|
|
|
|
[HAL_TIMEOUT] = MP_ETIMEDOUT,
|
2014-10-23 14:25:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
|
2016-10-07 03:58:25 +01:00
|
|
|
mp_raise_OSError(mp_hal_status_to_errno_table[status]);
|
2014-10-23 14:25:32 +01:00
|
|
|
}
|
2014-11-27 16:58:31 +00:00
|
|
|
|
2019-06-19 05:02:38 +01:00
|
|
|
MP_WEAK uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
|
|
|
|
uintptr_t ret = 0;
|
|
|
|
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
|
|
|
|
int errcode;
|
|
|
|
const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_PORT(pyb_stdio_uart));
|
|
|
|
ret = stream_p->ioctl(MP_STATE_PORT(pyb_stdio_uart), MP_STREAM_POLL, poll_flags, &errcode);
|
|
|
|
}
|
|
|
|
return ret | mp_uos_dupterm_poll(poll_flags);
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:22:35 +01:00
|
|
|
MP_WEAK int mp_hal_stdin_rx_chr(void) {
|
2015-02-13 15:04:53 +00:00
|
|
|
for (;;) {
|
|
|
|
#if 0
|
|
|
|
#ifdef USE_HOST_MODE
|
|
|
|
pyb_usb_host_process();
|
|
|
|
int c = pyb_usb_host_get_keyboard();
|
|
|
|
if (c != 0) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-02-13 07:51:08 +00:00
|
|
|
if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
|
2015-02-13 15:04:53 +00:00
|
|
|
return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart));
|
|
|
|
}
|
2017-10-19 04:16:42 +01:00
|
|
|
int dupterm_c = mp_uos_dupterm_rx_chr();
|
|
|
|
if (dupterm_c >= 0) {
|
|
|
|
return dupterm_c;
|
|
|
|
}
|
2017-02-06 04:10:03 +00:00
|
|
|
MICROPY_EVENT_POLL_HOOK
|
2015-02-13 15:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_hal_stdout_tx_str(const char *str) {
|
|
|
|
mp_hal_stdout_tx_strn(str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:22:35 +01:00
|
|
|
MP_WEAK void mp_hal_stdout_tx_strn(const char *str, size_t len) {
|
2015-02-13 15:04:53 +00:00
|
|
|
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
|
|
|
|
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
|
|
|
|
}
|
|
|
|
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
|
|
|
|
lcd_print_strn(str, len);
|
|
|
|
#endif
|
2017-10-19 04:16:42 +01:00
|
|
|
mp_uos_dupterm_tx_strn(str, len);
|
2015-02-13 15:04:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:15:32 +01:00
|
|
|
// Efficiently convert "\n" to "\r\n"
|
2015-10-30 23:03:58 +00:00
|
|
|
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
|
2017-10-19 04:15:32 +01:00
|
|
|
const char *last = str;
|
|
|
|
while (len--) {
|
|
|
|
if (*str == '\n') {
|
|
|
|
if (str > last) {
|
|
|
|
mp_hal_stdout_tx_strn(last, str - last);
|
|
|
|
}
|
|
|
|
mp_hal_stdout_tx_strn("\r\n", 2);
|
|
|
|
++str;
|
|
|
|
last = str;
|
|
|
|
} else {
|
|
|
|
++str;
|
|
|
|
}
|
2015-02-13 15:04:53 +00:00
|
|
|
}
|
2017-10-19 04:15:32 +01:00
|
|
|
if (str > last) {
|
|
|
|
mp_hal_stdout_tx_strn(last, str - last);
|
2015-02-13 15:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-03 00:05:16 +01:00
|
|
|
|
2018-05-28 02:23:33 +01:00
|
|
|
#if __CORTEX_M >= 0x03
|
2016-10-18 04:30:00 +01:00
|
|
|
void mp_hal_ticks_cpu_enable(void) {
|
2018-03-29 06:23:52 +01:00
|
|
|
if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
|
2016-10-18 04:30:00 +01:00
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
2017-03-10 03:58:26 +00:00
|
|
|
#if defined(__CORTEX_M) && __CORTEX_M == 7
|
|
|
|
// on Cortex-M7 we must unlock the DWT before writing to its registers
|
|
|
|
DWT->LAR = 0xc5acce55;
|
|
|
|
#endif
|
2016-10-18 04:30:00 +01:00
|
|
|
DWT->CYCCNT = 0;
|
|
|
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
|
|
|
}
|
|
|
|
}
|
2018-05-28 02:23:33 +01:00
|
|
|
#endif
|
2016-10-18 04:30:00 +01:00
|
|
|
|
2015-08-03 00:05:16 +01:00
|
|
|
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
|
2018-05-18 08:03:53 +01:00
|
|
|
#if defined(STM32L476xx) || defined(STM32L496xx)
|
2018-05-02 04:08:58 +01:00
|
|
|
if (gpio == GPIOG) {
|
2017-01-11 17:26:33 +00:00
|
|
|
// Port G pins 2 thru 15 are powered using VddIO2 on these MCUs.
|
|
|
|
HAL_PWREx_EnableVddIO2();
|
2018-05-02 04:08:58 +01:00
|
|
|
}
|
2015-08-03 00:05:16 +01:00
|
|
|
#endif
|
2018-05-02 04:08:58 +01:00
|
|
|
|
|
|
|
// This logic assumes that all the GPIOx_EN bits are adjacent and ordered in one register
|
|
|
|
|
2018-05-28 09:10:53 +01:00
|
|
|
#if defined(STM32F0)
|
|
|
|
#define AHBxENR AHBENR
|
|
|
|
#define AHBxENR_GPIOAEN_Pos RCC_AHBENR_GPIOAEN_Pos
|
|
|
|
#elif defined(STM32F4) || defined(STM32F7)
|
2018-05-02 04:08:58 +01:00
|
|
|
#define AHBxENR AHB1ENR
|
|
|
|
#define AHBxENR_GPIOAEN_Pos RCC_AHB1ENR_GPIOAEN_Pos
|
|
|
|
#elif defined(STM32H7)
|
|
|
|
#define AHBxENR AHB4ENR
|
|
|
|
#define AHBxENR_GPIOAEN_Pos RCC_AHB4ENR_GPIOAEN_Pos
|
2019-07-05 08:24:59 +01:00
|
|
|
#elif defined(STM32L0)
|
|
|
|
#define AHBxENR IOPENR
|
2019-07-16 05:47:12 +01:00
|
|
|
#define AHBxENR_GPIOAEN_Pos RCC_IOPENR_IOPAEN_Pos
|
2018-05-02 04:08:58 +01:00
|
|
|
#elif defined(STM32L4)
|
|
|
|
#define AHBxENR AHB2ENR
|
|
|
|
#define AHBxENR_GPIOAEN_Pos RCC_AHB2ENR_GPIOAEN_Pos
|
2015-08-03 00:05:16 +01:00
|
|
|
#endif
|
2018-05-02 04:08:58 +01:00
|
|
|
|
|
|
|
uint32_t gpio_idx = ((uint32_t)gpio - GPIOA_BASE) / (GPIOB_BASE - GPIOA_BASE);
|
|
|
|
RCC->AHBxENR |= 1 << (AHBxENR_GPIOAEN_Pos + gpio_idx);
|
|
|
|
volatile uint32_t tmp = RCC->AHBxENR; // Delay after enabling clock
|
|
|
|
(void)tmp;
|
2015-08-03 00:05:16 +01:00
|
|
|
}
|
2016-02-11 05:20:14 +00:00
|
|
|
|
2016-10-18 04:32:42 +01:00
|
|
|
void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, uint32_t alt) {
|
|
|
|
GPIO_TypeDef *gpio = pin_obj->gpio;
|
|
|
|
uint32_t pin = pin_obj->pin;
|
2016-04-12 13:50:47 +01:00
|
|
|
mp_hal_gpio_clock_enable(gpio);
|
|
|
|
gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | ((mode & 3) << (2 * pin));
|
2018-05-02 03:05:45 +01:00
|
|
|
#if defined(GPIO_ASCR_ASC0)
|
|
|
|
// The L4 has a special analog switch to connect the GPIO to the ADC
|
|
|
|
gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | (((mode >> 2) & 1) << pin);
|
|
|
|
gpio->ASCR = (gpio->ASCR & ~(1 << pin)) | ((mode >> 3) & 1) << pin;
|
|
|
|
#else
|
2016-04-12 13:50:47 +01:00
|
|
|
gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | ((mode >> 2) << pin);
|
2018-05-02 03:05:45 +01:00
|
|
|
#endif
|
2016-04-12 13:50:47 +01:00
|
|
|
gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (2 << (2 * pin)); // full speed
|
|
|
|
gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin));
|
|
|
|
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
|
|
|
|
}
|
|
|
|
|
2016-11-11 06:53:45 +00:00
|
|
|
bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit) {
|
2016-02-11 05:20:14 +00:00
|
|
|
const pin_af_obj_t *af = pin_find_af(pin, fn, unit);
|
|
|
|
if (af == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-11 06:53:45 +00:00
|
|
|
mp_hal_pin_config(pin, mode, pull, af->idx);
|
2016-02-11 05:20:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-01 08:31:23 +01:00
|
|
|
|
|
|
|
void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed) {
|
|
|
|
GPIO_TypeDef *gpio = pin_obj->gpio;
|
|
|
|
uint32_t pin = pin_obj->pin;
|
|
|
|
gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (speed << (2 * pin));
|
|
|
|
}
|
2019-02-22 10:53:41 +00:00
|
|
|
|
2019-06-01 07:08:40 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
// MAC address
|
|
|
|
|
|
|
|
typedef struct _pyb_otp_t {
|
|
|
|
uint16_t series;
|
|
|
|
uint16_t rev;
|
|
|
|
uint8_t mac[6];
|
|
|
|
} pyb_otp_t;
|
|
|
|
|
|
|
|
#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx)
|
|
|
|
#define OTP_ADDR (0x1ff079e0)
|
|
|
|
#else
|
|
|
|
#define OTP_ADDR (0x1ff0f3c0)
|
|
|
|
#endif
|
|
|
|
#define OTP ((pyb_otp_t*)OTP_ADDR)
|
|
|
|
|
2019-02-22 10:53:41 +00:00
|
|
|
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
|
2019-06-01 07:08:40 +01:00
|
|
|
// Check if OTP region has a valid MAC address, and use it if it does
|
|
|
|
if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') {
|
|
|
|
memcpy(buf, OTP->mac, 6);
|
|
|
|
buf[5] += idx;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:53:41 +00:00
|
|
|
// Generate a random locally administered MAC address (LAA)
|
|
|
|
uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
|
|
|
|
buf[0] = 0x02; // LAA range
|
|
|
|
buf[1] = (id[11] << 4) | (id[10] & 0xf);
|
|
|
|
buf[2] = (id[9] << 4) | (id[8] & 0xf);
|
|
|
|
buf[3] = (id[7] << 4) | (id[6] & 0xf);
|
|
|
|
buf[4] = id[2];
|
|
|
|
buf[5] = (id[0] << 2) | idx;
|
|
|
|
}
|
2019-06-01 07:08:40 +01:00
|
|
|
|
|
|
|
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
|
|
|
|
static const char hexchr[16] = "0123456789ABCDEF";
|
|
|
|
uint8_t mac[6];
|
|
|
|
mp_hal_get_mac(idx, mac);
|
|
|
|
for (; chr_len; ++chr_off, --chr_len) {
|
|
|
|
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
|
|
|
|
}
|
|
|
|
}
|