From f1dd0fd7df758e2b3ac28dcdb16af8582577b814 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 14:50:08 +1000 Subject: [PATCH 001/163] stm32: Remove unused usbd_msc.c file. --- ports/stm32/usbdev/class/src/usbd_msc.c | 609 ------------------------ 1 file changed, 609 deletions(-) delete mode 100644 ports/stm32/usbdev/class/src/usbd_msc.c diff --git a/ports/stm32/usbdev/class/src/usbd_msc.c b/ports/stm32/usbdev/class/src/usbd_msc.c deleted file mode 100644 index 7817c98b1..000000000 --- a/ports/stm32/usbdev/class/src/usbd_msc.c +++ /dev/null @@ -1,609 +0,0 @@ -/** - ****************************************************************************** - * @file usbd_msc_core.c - * @author MCD Application Team - * @version V2.0.0 - * @date 18-February-2014 - * @brief This file provides all the MSC core functions. - * - * @verbatim - * - * =================================================================== - * MSC Class Description - * =================================================================== - * This module manages the MSC class V1.0 following the "Universal - * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0 - * Sep. 31, 1999". - * This driver implements the following aspects of the specification: - * - Bulk-Only Transport protocol - * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3)) - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT 2014 STMicroelectronics

- * - * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); - * You may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.st.com/software_license_agreement_liberty_v2 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc.h" - - -/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup MSC_CORE - * @brief Mass storage core module - * @{ - */ - -/** @defgroup MSC_CORE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_FunctionPrototypes - * @{ - */ -uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum); - - -uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length); - - -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Variables - * @{ - */ - - -USBD_ClassTypeDef USBD_MSC = -{ - USBD_MSC_Init, - USBD_MSC_DeInit, - USBD_MSC_Setup, - NULL, /*EP0_TxSent*/ - NULL, /*EP0_RxReady*/ - USBD_MSC_DataIn, - USBD_MSC_DataOut, - NULL, /*SOF */ - NULL, - NULL, - USBD_MSC_GetHSCfgDesc, - USBD_MSC_GetFSCfgDesc, - USBD_MSC_GetOtherSpeedCfgDesc, - USBD_MSC_GetDeviceQualifierDescriptor, -}; - -/* USB Mass storage device Configuration Descriptor */ -/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ -__ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_HS_PACKET), - HIBYTE(MSC_MAX_HS_PACKET), - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_HS_PACKET), - HIBYTE(MSC_MAX_HS_PACKET), - 0x00 /*Polling interval in milliseconds*/ -}; - -/* USB Mass storage device Configuration Descriptor */ -/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ -uint8_t USBD_MSC_CfgFSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_FS_PACKET), - HIBYTE(MSC_MAX_FS_PACKET), - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_FS_PACKET), - HIBYTE(MSC_MAX_FS_PACKET), - 0x00 /*Polling interval in milliseconds*/ -}; - -__ALIGN_BEGIN uint8_t USBD_MSC_OtherSpeedCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent command set*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - 0x40, - 0x00, - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - 0x40, - 0x00, - 0x00 /*Polling interval in milliseconds*/ -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN uint8_t USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - MSC_MAX_FS_PACKET, - 0x01, - 0x00, -}; -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Functions - * @{ - */ - -/** - * @brief USBD_MSC_Init - * Initialize the mass storage configuration - * @param pdev: device instance - * @param cfgidx: configuration index - * @retval status - */ -uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - int16_t ret = 0; - - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, - MSC_EPOUT_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPIN_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - } - else - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, - MSC_EPOUT_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPIN_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - } - pdev->pClassData = USBD_malloc(sizeof (USBD_MSC_BOT_HandleTypeDef)); - - if(pdev->pClassData == NULL) - { - ret = 1; - } - else - { - /* Init the BOT layer */ - MSC_BOT_Init(pdev); - ret = 0; - } - - return ret; -} - -/** - * @brief USBD_MSC_DeInit - * DeInitilaize the mass storage configuration - * @param pdev: device instance - * @param cfgidx: configuration index - * @retval status - */ -uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - /* Close MSC EPs */ - USBD_LL_CloseEP(pdev, - MSC_EPOUT_ADDR); - - /* Open EP IN */ - USBD_LL_CloseEP(pdev, - MSC_EPIN_ADDR); - - - /* D-Init the BOT layer */ - MSC_BOT_DeInit(pdev); - - /* Free MSC Class Resources */ - if(pdev->pClassData != NULL) - { - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - return 0; -} -/** -* @brief USBD_MSC_Setup -* Handle the MSC specific requests -* @param pdev: device instance -* @param req: USB request -* @retval status -*/ -uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - - /* Class request */ - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) - { - case BOT_GET_MAX_LUN : - - if((req->wValue == 0) && - (req->wLength == 1) && - ((req->bmRequest & 0x80) == 0x80)) - { - hmsc->max_lun = ((USBD_StorageTypeDef *)pdev->pUserData)->GetMaxLun(); - USBD_CtlSendData (pdev, - (uint8_t *)&hmsc->max_lun, - 1); - } - else - { - USBD_CtlError(pdev , req); - return USBD_FAIL; - } - break; - - case BOT_RESET : - if((req->wValue == 0) && - (req->wLength == 0) && - ((req->bmRequest & 0x80) != 0x80)) - { - MSC_BOT_Reset(pdev); - } - else - { - USBD_CtlError(pdev , req); - return USBD_FAIL; - } - break; - - default: - USBD_CtlError(pdev , req); - return USBD_FAIL; - } - break; - /* Interface & Endpoint request */ - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_INTERFACE : - USBD_CtlSendData (pdev, - (uint8_t *)&hmsc->interface, - 1); - break; - - case USB_REQ_SET_INTERFACE : - hmsc->interface = (uint8_t)(req->wValue); - break; - - case USB_REQ_CLEAR_FEATURE: - - /* Flush the FIFO and Clear the stall status */ - USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex); - - /* Re-activate the EP */ - USBD_LL_CloseEP (pdev , (uint8_t)req->wIndex); - if((((uint8_t)req->wIndex) & 0x80) == 0x80) - { - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPIN_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - } - else - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPIN_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - } - } - else - { - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPOUT_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - } - else - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - MSC_EPOUT_ADDR, - USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - } - } - - /* Handle BOT error */ - MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex); - break; - - } - break; - - default: - break; - } - return 0; -} - -/** -* @brief USBD_MSC_DataIn -* handle data IN Stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - MSC_BOT_DataIn(pdev , epnum); - return 0; -} - -/** -* @brief USBD_MSC_DataOut -* handle data OUT Stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - MSC_BOT_DataOut(pdev , epnum); - return 0; -} - -/** -* @brief USBD_MSC_GetHSCfgDesc -* return configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_CfgHSDesc); - return USBD_MSC_CfgHSDesc; -} - -/** -* @brief USBD_MSC_GetFSCfgDesc -* return configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_CfgFSDesc); - return USBD_MSC_CfgFSDesc; -} - -/** -* @brief USBD_MSC_GetOtherSpeedCfgDesc -* return other speed configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_OtherSpeedCfgDesc); - return USBD_MSC_OtherSpeedCfgDesc; -} -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length) -{ - *length = sizeof (USBD_MSC_DeviceQualifierDesc); - return USBD_MSC_DeviceQualifierDesc; -} - -/** -* @brief USBD_MSC_RegisterStorage -* @param fops: storage callback -* @retval status -*/ -uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev, - USBD_StorageTypeDef *fops) -{ - if(fops != NULL) - { - pdev->pUserData= fops; - } - return 0; -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ From 3101a8fe3287dabf3dcb103ff6e883d2e593f69f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 14:53:17 +1000 Subject: [PATCH 002/163] stm32: Replace stray tabs with spaces. --- ports/stm32/extint.c | 2 +- ports/stm32/usb.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index 24a68c2a2..41943f1cd 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -83,7 +83,7 @@ // TODO Add python method to change callback object. -#define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE) +#define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE) // Macro used to set/clear the bit corresponding to the line in the IMR/EMR // register in an atomic fashion by using bitband addressing. diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index d39f49a6c..456f45208 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -44,8 +44,8 @@ typedef enum { } pyb_usb_storage_medium_t; typedef enum { - USB_PHY_FS_ID = 0, - USB_PHY_HS_ID = 1, + USB_PHY_FS_ID = 0, + USB_PHY_HS_ID = 1, } USB_PHY_ID; extern mp_uint_t pyb_usb_flags; From 81375eb47044a6abfc2871c82160d44ee712bc70 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 15:00:16 +1000 Subject: [PATCH 003/163] stm32/boards: Change remaining stm32f4xx_hal_conf.h to unix line ending. --- .gitattributes | 1 - .../boards/HYDRABUS/stm32f4xx_hal_conf.h | 822 ++++++++--------- .../NETDUINO_PLUS_2/stm32f4xx_hal_conf.h | 822 ++++++++--------- .../boards/NUCLEO_F401RE/stm32f4xx_hal_conf.h | 826 +++++++++--------- .../stm32/boards/PYBV10/stm32f4xx_hal_conf.h | 822 ++++++++--------- ports/stm32/boards/PYBV3/stm32f4xx_hal_conf.h | 822 ++++++++--------- ports/stm32/boards/PYBV4/stm32f4xx_hal_conf.h | 822 ++++++++--------- .../boards/STM32F4DISC/stm32f4xx_hal_conf.h | 822 ++++++++--------- 8 files changed, 2879 insertions(+), 2880 deletions(-) diff --git a/.gitattributes b/.gitattributes index 1ac507944..133e2625c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -17,7 +17,6 @@ tests/basics/string_cr_conversion.py -text tests/basics/string_crlf_conversion.py -text ports/stm32/pybcdc.inf_template -text ports/stm32/usbd_* -text -ports/stm32/boards/*/stm32f4xx_hal_conf.h -text ports/stm32/usbdev/** -text ports/stm32/usbhost/** -text ports/cc3200/hal/aes.c -text diff --git a/ports/stm32/boards/HYDRABUS/stm32f4xx_hal_conf.h b/ports/stm32/boards/HYDRABUS/stm32f4xx_hal_conf.h index 8941e8290..d3df51c10 100644 --- a/ports/stm32/boards/HYDRABUS/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/HYDRABUS/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h b/ports/stm32/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h index 8b04c2845..42c2c4e9b 100644 --- a/ports/stm32/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/NUCLEO_F401RE/stm32f4xx_hal_conf.h b/ports/stm32/boards/NUCLEO_F401RE/stm32f4xx_hal_conf.h index a1fd2e083..f4db4cb63 100644 --- a/ports/stm32/boards/NUCLEO_F401RE/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/NUCLEO_F401RE/stm32f4xx_hal_conf.h @@ -1,413 +1,413 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -// This board doesn't really have USB, but the stm32 codebase doesn't build -// without some USB defined, so we leave this on for now. -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +// This board doesn't really have USB, but the stm32 codebase doesn't build +// without some USB defined, so we leave this on for now. +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/PYBV10/stm32f4xx_hal_conf.h b/ports/stm32/boards/PYBV10/stm32f4xx_hal_conf.h index 52518e196..3d9252264 100644 --- a/ports/stm32/boards/PYBV10/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/PYBV10/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -#define HAL_I2S_MODULE_ENABLED -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +#define HAL_I2S_MODULE_ENABLED +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/PYBV3/stm32f4xx_hal_conf.h b/ports/stm32/boards/PYBV3/stm32f4xx_hal_conf.h index 8941e8290..d3df51c10 100644 --- a/ports/stm32/boards/PYBV3/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/PYBV3/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/PYBV4/stm32f4xx_hal_conf.h b/ports/stm32/boards/PYBV4/stm32f4xx_hal_conf.h index 8941e8290..d3df51c10 100644 --- a/ports/stm32/boards/PYBV4/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/PYBV4/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/boards/STM32F4DISC/stm32f4xx_hal_conf.h b/ports/stm32/boards/STM32F4DISC/stm32f4xx_hal_conf.h index 8941e8290..d3df51c10 100644 --- a/ports/stm32/boards/STM32F4DISC/stm32f4xx_hal_conf.h +++ b/ports/stm32/boards/STM32F4DISC/stm32f4xx_hal_conf.h @@ -1,411 +1,411 @@ -/** - ****************************************************************************** - * @file stm32f4xx_hal_conf.h - * @author MCD Application Team - * @version V1.1.0 - * @date 19-June-2014 - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F4xx_HAL_CONF_H -#define __STM32F4xx_HAL_CONF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -#define USE_USB_FS - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED -#define HAL_ADC_MODULE_ENABLED -#define HAL_CAN_MODULE_ENABLED -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -#define HAL_DAC_MODULE_ENABLED -/* #define HAL_DCMI_MODULE_ENABLED */ -#define HAL_DMA_MODULE_ENABLED -/* #define HAL_DMA2D_MODULE_ENABLED */ -/* #define HAL_ETH_MODULE_ENABLED */ -#define HAL_FLASH_MODULE_ENABLED -/* #define HAL_NAND_MODULE_ENABLED */ -/* #define HAL_NOR_MODULE_ENABLED */ -/* #define HAL_PCCARD_MODULE_ENABLED */ -/* #define HAL_SRAM_MODULE_ENABLED */ -/* #define HAL_SDRAM_MODULE_ENABLED */ -/* #define HAL_HASH_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_LTDC_MODULE_ENABLED */ -#define HAL_PWR_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_RNG_MODULE_ENABLED -#define HAL_RTC_MODULE_ENABLED -/* #define HAL_SAI_MODULE_ENABLED */ -#define HAL_SD_MODULE_ENABLED -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PCD_MODULE_ENABLED -/* #define HAL_HCD_MODULE_ENABLED */ - - -/* ########################## HSE/HSI Values adaptation ##################### */ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ -/** - * @brief External Low Speed oscillator (LSE) value. - */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S peripheral - * This value is used by the I2S HAL module to compute the I2S clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 1 -#define DATA_CACHE_ENABLE 1 - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1 */ - -/* ################## Ethernet peripheral configuration ##################### */ - -/* Section 1 : Ethernet peripheral configuration */ - -/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ -#define MAC_ADDR0 2 -#define MAC_ADDR1 0 -#define MAC_ADDR2 0 -#define MAC_ADDR3 0 -#define MAC_ADDR4 0 -#define MAC_ADDR5 0 - -/* Definition of the Ethernet driver buffers size and count */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ -#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ -#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ - -/* Section 2: PHY configuration section */ - -/* DP83848 PHY Address*/ -#define DP83848_PHY_ADDRESS 0x01 -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ -#define PHY_RESET_DELAY ((uint32_t)0x000000FF) -/* PHY Configuration delay */ -#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) - -#define PHY_READ_TO ((uint32_t)0x0000FFFF) -#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) - -/* Section 3: Common PHY Registers */ - -#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ -#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ - -#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ -#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ -#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ -#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ -#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ -#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ -#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ -#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ -#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ -#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ - -#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ -#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ -#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - -/* Section 4: Extended PHY Registers */ - -#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ -#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ -#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ - -#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ -#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ -#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ - -#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ -#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ - -#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ -#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f4xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f4xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f4xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f4xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f4xx_hal_adc.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f4xx_hal_can.h" -#endif /* HAL_CAN_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f4xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED - #include "stm32f4xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DMA2D_MODULE_ENABLED - #include "stm32f4xx_hal_dma2d.h" -#endif /* HAL_DMA2D_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f4xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_DCMI_MODULE_ENABLED - #include "stm32f4xx_hal_dcmi.h" -#endif /* HAL_DCMI_MODULE_ENABLED */ - -#ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f4xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f4xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sram.h" -#endif /* HAL_SRAM_MODULE_ENABLED */ - -#ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f4xx_hal_nor.h" -#endif /* HAL_NOR_MODULE_ENABLED */ - -#ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f4xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ - -#ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f4xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ - -#ifdef HAL_SDRAM_MODULE_ENABLED - #include "stm32f4xx_hal_sdram.h" -#endif /* HAL_SDRAM_MODULE_ENABLED */ - -#ifdef HAL_HASH_MODULE_ENABLED - #include "stm32f4xx_hal_hash.h" -#endif /* HAL_HASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f4xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f4xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f4xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LTDC_MODULE_ENABLED - #include "stm32f4xx_hal_ltdc.h" -#endif /* HAL_LTDC_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f4xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED - #include "stm32f4xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f4xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SAI_MODULE_ENABLED - #include "stm32f4xx_hal_sai.h" -#endif /* HAL_SAI_MODULE_ENABLED */ - -#ifdef HAL_SD_MODULE_ENABLED - #include "stm32f4xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f4xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f4xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED - #include "stm32f4xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED - #include "stm32f4xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f4xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f4xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f4xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -#ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f4xx_hal_pcd.h" -#endif /* HAL_PCD_MODULE_ENABLED */ - -#ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f4xx_hal_hcd.h" -#endif /* HAL_HCD_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); -#else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __STM32F4xx_HAL_CONF_H */ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.1.0 + * @date 19-June-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define USE_USB_FS + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DCMI_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +/* #define HAL_ETH_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_PCCARD_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +/* #define HAL_SDRAM_MODULE_ENABLED */ +/* #define HAL_HASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_I2S_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LTDC_MODULE_ENABLED */ +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100U) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000U) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ From 21c889baeb077b6460cc8d315340e231a8ff049d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 15:24:08 +1000 Subject: [PATCH 004/163] stm32/boards: Change linker scripts to use "K" instead of hex byte size. --- ports/stm32/boards/stm32f401xd.ld | 10 +++++----- ports/stm32/boards/stm32f401xe.ld | 10 +++++----- ports/stm32/boards/stm32f411.ld | 10 +++++----- ports/stm32/boards/stm32f429.ld | 11 ++++++----- ports/stm32/boards/stm32f439.ld | 12 +++++++----- ports/stm32/boards/stm32l476xe.ld | 6 +++--- ports/stm32/boards/stm32l476xg.ld | 6 +++--- 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/ports/stm32/boards/stm32f401xd.ld b/ports/stm32/boards/stm32f401xd.ld index 415c25849..89f605609 100644 --- a/ports/stm32/boards/stm32f401xd.ld +++ b/ports/stm32/boards/stm32f401xd.ld @@ -5,11 +5,11 @@ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x060000 /* entire flash, 384 KiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ - FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 0x01C000 /* sectors 1,2,3 are 16K, 4 is 64K */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x040000 /* sectors 5,6 2*128KiB = 256 KiB */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x018000 /* 96 KiB */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 384K /* entire flash */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */ + FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3 are 16K, 4 is 64K */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 256K /* sectors 5,6 are 128K */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K } /* produce a link error if there is not this amount of RAM for these sections */ diff --git a/ports/stm32/boards/stm32f401xe.ld b/ports/stm32/boards/stm32f401xe.ld index a2e693b49..ae2f89904 100644 --- a/ports/stm32/boards/stm32f401xe.ld +++ b/ports/stm32/boards/stm32f401xe.ld @@ -5,11 +5,11 @@ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x080000 /* entire flash, 512 KiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ - FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 0x01C000 /* sectors 1,2,3 are 16K, 4 is 64K */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x060000 /* sectors 5,6,7 3*128KiB = 384 KiB */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x018000 /* 96 KiB */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* entire flash */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */ + FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3 are 16K, 4 is 64K */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 384K /* sectors 5,6,7 are 128K */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K } /* produce a link error if there is not this amount of RAM for these sections */ diff --git a/ports/stm32/boards/stm32f411.ld b/ports/stm32/boards/stm32f411.ld index d156e852a..7adfa35c9 100644 --- a/ports/stm32/boards/stm32f411.ld +++ b/ports/stm32/boards/stm32f411.ld @@ -5,11 +5,11 @@ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x080000 /* entire flash, 512 KiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ - /* sectors 1,2,3 are 16K, 4 is 64K (for filesystem) */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x060000 /* sectors 5,6,7 3*128KiB = 384 KiB */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* entire flash */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */ + FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3 are 16K, 4 is 64K */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 384K /* sectors 5,6,7 are 128K */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K } /* produce a link error if there is not this amount of RAM for these sections */ diff --git a/ports/stm32/boards/stm32f429.ld b/ports/stm32/boards/stm32f429.ld index f358233a6..a0931684d 100644 --- a/ports/stm32/boards/stm32f429.ld +++ b/ports/stm32/boards/stm32f429.ld @@ -5,11 +5,12 @@ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x0200000 /* entire flash, 2048 KiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x0004000 /* sector 0, 16 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x0088000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x0030000 /* 192 KiB */ - SDRAM(xrw) : ORIGIN = 0xC0000000, LENGTH = 0x0800000 /* 8 MByte */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K /* entire flash */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0, 16 KiB */ + FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1-4: 3*16K+64K */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 896K /* sectors 5-11 are 128K */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K + SDRAM(xrw) : ORIGIN = 0xC0000000, LENGTH = 8192K } /* produce a link error if there is not this amount of RAM for these sections */ diff --git a/ports/stm32/boards/stm32f439.ld b/ports/stm32/boards/stm32f439.ld index 0da185e89..a76a0ebc7 100644 --- a/ports/stm32/boards/stm32f439.ld +++ b/ports/stm32/boards/stm32f439.ld @@ -5,11 +5,13 @@ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x200000 /* entire flash, 2048 KiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x030000 /* 192 KiB */ - CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K /* entire flash */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 896K /* sectors 5-11 are 128K */ + FLASH_FS (rx) : ORIGIN = 0x08100000, LENGTH = 256K /* sectors 12-17 are 4*16K+64K+128K */ + FLASH_FS2 (rx) : ORIGIN = 0x08140000, LENGTH = 128K /* sector 18 */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K + CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K } /* produce a link error if there is not this amount of RAM for these sections */ diff --git a/ports/stm32/boards/stm32l476xe.ld b/ports/stm32/boards/stm32l476xe.ld index bb9895d2a..11b2972ad 100644 --- a/ports/stm32/boards/stm32l476xe.ld +++ b/ports/stm32/boards/stm32l476xe.ld @@ -6,9 +6,9 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x0004000 /* sectors 0-7, 16 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x08004000, LENGTH = 0x005C000 /* sectors 8-191, 368 KiB */ - FLASH_FS (r) : ORIGIN = 0x08060000, LENGTH = 0x0020000 /* sectors 192-255, 128 KiB */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sectors 0-7 */ + FLASH_TEXT (rx) : ORIGIN = 0x08004000, LENGTH = 368K /* sectors 8-191 */ + FLASH_FS (r) : ORIGIN = 0x08060000, LENGTH = 128K /* sectors 192-255 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K SRAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K } diff --git a/ports/stm32/boards/stm32l476xg.ld b/ports/stm32/boards/stm32l476xg.ld index 684078c43..a94fa2750 100644 --- a/ports/stm32/boards/stm32l476xg.ld +++ b/ports/stm32/boards/stm32l476xg.ld @@ -6,9 +6,9 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x0004000 /* sectors 0-7, 16 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x08004000, LENGTH = 0x007C000 /* sectors 8-255, 496 KiB */ - FLASH_FS (r) : ORIGIN = 0x08080000, LENGTH = 0x0080000 /* sectors 256-511 512 KiB */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sectors 0-7 */ + FLASH_TEXT (rx) : ORIGIN = 0x08004000, LENGTH = 496K /* sectors 8-255 */ + FLASH_FS (r) : ORIGIN = 0x08080000, LENGTH = 512K /* sectors 256-511 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K SRAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K } From 069fc48bf60b31fca4339d26cee7b4a415b185f9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 15:40:13 +1000 Subject: [PATCH 005/163] stm32/boards: Fix I2C1 pin mapping on NUCLEO_F401RE/F411RE boards. This patch makes it consistent with the STM document describing the Arduino layout. Thanks to @shaoziyang for the original patch. --- ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h index c12d52756..a843d3c35 100644 --- a/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h @@ -22,8 +22,8 @@ #define MICROPY_HW_UART_REPL_BAUD 115200 // I2C busses -#define MICROPY_HW_I2C1_SCL (pin_B6) // Arduino D10, pin 17 on CN10 -#define MICROPY_HW_I2C1_SDA (pin_B7) // pin 21 on CN7 +#define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 +#define MICROPY_HW_I2C1_SDA (pin_B9) // D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 #define MICROPY_HW_I2C2_SDA (pin_B3) // Arduino D3, pin 31 on CN10 #define MICROPY_HW_I2C3_SCL (pin_A8) // Arduino D7, pin 23 on CN10 diff --git a/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h index 1f7f0a23b..26b1e0b61 100644 --- a/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h @@ -22,8 +22,8 @@ #define MICROPY_HW_UART_REPL_BAUD 115200 // I2C busses -#define MICROPY_HW_I2C1_SCL (pin_B6) // Arduino D10, pin 17 on CN10 -#define MICROPY_HW_I2C1_SDA (pin_B7) // pin 21 on CN7 +#define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 +#define MICROPY_HW_I2C1_SDA (pin_B9) // D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 #define MICROPY_HW_I2C2_SDA (pin_B3) // Arduino D3, pin 31 on CN10 #define MICROPY_HW_I2C3_SCL (pin_A8) // Arduino D7, pin 23 on CN10 From 68c28174d0e0ec3f6b1461aea3a0b6a1b84610bb Mon Sep 17 00:00:00 2001 From: tll <1040424979@qq.com> Date: Sat, 24 Jun 2017 08:38:32 +0800 Subject: [PATCH 006/163] py/objstr: Add check for valid UTF-8 when making a str from bytes. This patch adds a function utf8_check() to check for a valid UTF-8 encoded string, and calls it when constructing a str from raw bytes. The feature is selectable at compile time via MICROPY_PY_BUILTINS_STR_UNICODE_CHECK and is enabled if unicode is enabled. It costs about 110 bytes on Thumb-2, 150 bytes on Xtensa and 170 bytes on x86-64. --- py/mpconfig.h | 5 +++++ py/objstr.c | 10 ++++++++++ py/unicode.c | 28 ++++++++++++++++++++++++++++ py/unicode.h | 1 + tests/unicode/unicode.py | 14 ++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/py/mpconfig.h b/py/mpconfig.h index dac8a903c..38cf4b560 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -691,6 +691,11 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_STR_UNICODE (0) #endif +// Whether to check for valid UTF-8 when converting bytes to str +#ifndef MICROPY_PY_BUILTINS_STR_UNICODE_CHECK +#define MICROPY_PY_BUILTINS_STR_UNICODE_CHECK (MICROPY_PY_BUILTINS_STR_UNICODE) +#endif + // Whether str.center() method provided #ifndef MICROPY_PY_BUILTINS_STR_CENTER #define MICROPY_PY_BUILTINS_STR_CENTER (0) diff --git a/py/objstr.c b/py/objstr.c index 4c287af04..f6214f80c 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -161,6 +161,11 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ if (str_hash == 0) { str_hash = qstr_compute_hash(str_data, str_len); } + #if MICROPY_PY_BUILTINS_STR_UNICODE_CHECK + if (!utf8_check(str_data, str_len)) { + mp_raise_msg(&mp_type_UnicodeError, NULL); + } + #endif mp_obj_str_t *o = MP_OBJ_TO_PTR(mp_obj_new_str_of_type(type, NULL, str_len)); o->data = str_data; o->hash = str_hash; @@ -168,6 +173,11 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ } else { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); + #if MICROPY_PY_BUILTINS_STR_UNICODE_CHECK + if (!utf8_check(bufinfo.buf, bufinfo.len)) { + mp_raise_msg(&mp_type_UnicodeError, NULL); + } + #endif return mp_obj_new_str(bufinfo.buf, bufinfo.len, false); } } diff --git a/py/unicode.c b/py/unicode.c index eddb007d5..140b7ba71 100644 --- a/py/unicode.c +++ b/py/unicode.c @@ -182,3 +182,31 @@ mp_uint_t unichar_xdigit_value(unichar c) { } return n; } + +bool utf8_check(const byte *p, size_t len) { + uint8_t need = 0; + const byte *end = p + len; + for (; p < end; p++) { + byte c = *p; + if (need) { + if (c >= 0x80) { + need--; + } else { + // mismatch + return 0; + } + } else { + if (c >= 0xc0) { + if (c >= 0xf8) { + // mismatch + return 0; + } + need = (0xe5 >> ((c >> 3) & 0x6)) & 3; + } else if (c >= 0x80) { + // mismatch + return 0; + } + } + } + return need == 0; // no pending fragments allowed +} diff --git a/py/unicode.h b/py/unicode.h index 19487a65a..c1fb51789 100644 --- a/py/unicode.h +++ b/py/unicode.h @@ -30,5 +30,6 @@ #include "py/misc.h" mp_uint_t utf8_ptr_to_index(const byte *s, const byte *ptr); +bool utf8_check(const byte *p, size_t len); #endif // MICROPY_INCLUDED_PY_UNICODE_H diff --git a/tests/unicode/unicode.py b/tests/unicode/unicode.py index 5f29bc1c9..3a35ce894 100644 --- a/tests/unicode/unicode.py +++ b/tests/unicode/unicode.py @@ -33,3 +33,17 @@ int('\u0200') except ValueError: print('ValueError') + +# test invalid UTF-8 string +try: + str(b'ab\xa1', 'utf8') +except UnicodeError: + print('UnicodeError') +try: + str(b'ab\xf8', 'utf8') +except UnicodeError: + print('UnicodeError') +try: + str(bytearray(b'ab\xc0a'), 'utf8') +except UnicodeError: + print('UnicodeError') From beeb7483d889f5880895ecdd8e7a354f16956037 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 17:34:45 +1000 Subject: [PATCH 007/163] extmod/modussl_mbedtls: Allow to compile with MBEDTLS_DEBUG_C disabled. With MBEDTLS_DEBUG_C disabled the function mbedtls_debug_set_threshold() doesn't exist. There's also no need to call mbedtls_ssl_conf_dbg() so a few bytes can be saved on disabling that and not needing the mbedtls_debug callback. --- extmod/modussl_mbedtls.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 12ec60a75..a65470e16 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -67,7 +67,7 @@ struct ssl_args { STATIC const mp_obj_type_t ussl_socket_type; -static void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) { +void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) { printf("DBG:%s:%04d: %s\n", file, line, str); } @@ -123,8 +123,10 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { mbedtls_x509_crt_init(&o->cert); mbedtls_pk_init(&o->pkey); mbedtls_ctr_drbg_init(&o->ctr_drbg); + #ifdef MBEDTLS_DEBUG_C // Debug level (0-4) mbedtls_debug_set_threshold(0); + #endif mbedtls_entropy_init(&o->entropy); const byte seed[] = "upy"; @@ -144,7 +146,9 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { mbedtls_ssl_conf_authmode(&o->conf, MBEDTLS_SSL_VERIFY_NONE); mbedtls_ssl_conf_rng(&o->conf, mbedtls_ctr_drbg_random, &o->ctr_drbg); + #ifdef MBEDTLS_DEBUG_C mbedtls_ssl_conf_dbg(&o->conf, mbedtls_debug, NULL); + #endif ret = mbedtls_ssl_setup(&o->ssl, &o->conf); if (ret != 0) { From 5c603bd0fd53e7dbd709883f289d1e580c86e33d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 7 Sep 2017 00:10:10 +0300 Subject: [PATCH 008/163] py/objlist: Properly implement comparison with incompatible types. Should raise TypeError, unless it's (in)equality comparison. --- py/objlist.c | 26 ++++++++++++-------------- tests/basics/list_compare.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/py/objlist.c b/py/objlist.c index 6ac33e80e..d70867ded 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -87,18 +87,6 @@ STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_ } } -// Don't pass MP_BINARY_OP_NOT_EQUAL here -STATIC bool list_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) { - mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); - if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) { - return false; - } - mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_list_t *another = MP_OBJ_TO_PTR(another_in); - - return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len); -} - STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) { mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); switch (op) { @@ -146,8 +134,18 @@ STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { case MP_BINARY_OP_LESS: case MP_BINARY_OP_LESS_EQUAL: case MP_BINARY_OP_MORE: - case MP_BINARY_OP_MORE_EQUAL: - return mp_obj_new_bool(list_cmp_helper(op, lhs, rhs)); + case MP_BINARY_OP_MORE_EQUAL: { + if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { + if (op == MP_BINARY_OP_EQUAL) { + return mp_const_false; + } + return MP_OBJ_NULL; // op not supported + } + + mp_obj_list_t *another = MP_OBJ_TO_PTR(rhs); + bool res = mp_seq_cmp_objs(op, o->items, o->len, another->items, another->len); + return mp_obj_new_bool(res); + } default: return MP_OBJ_NULL; // op not supported diff --git a/tests/basics/list_compare.py b/tests/basics/list_compare.py index eea881424..fd656c7f1 100644 --- a/tests/basics/list_compare.py +++ b/tests/basics/list_compare.py @@ -48,3 +48,13 @@ print([1] <= [1, -1]) print([1, 0] <= [1]) print([1, -1] <= [1]) + + +print([] == {}) +print([] != {}) +print([1] == (1,)) + +try: + print([] < {}) +except TypeError: + print("TypeError") From d4d1c45a553c6361a72053383f0fe242f05cad3d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 7 Sep 2017 10:55:43 +0300 Subject: [PATCH 009/163] py/runtime0.h: Move relational ops to the beginning of mp_binary_op_t. This is to allow to encode arithmetic operations more efficiently, in preparation to introduction of __rOP__ method support. --- py/objint_mpz.c | 2 +- py/runtime0.h | 31 ++++++++++++++++--------------- tests/cmdline/cmd_showbc.py.exp | 22 +++++++++++----------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 148446f0f..c7a3074ba 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -221,7 +221,7 @@ mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i return mp_obj_new_float(flhs / frhs); #endif - } else if (op <= MP_BINARY_OP_INPLACE_POWER) { + } else if (op >= MP_BINARY_OP_OR) { mp_obj_int_t *res = mp_obj_int_new_mpz(); switch (op) { diff --git a/py/runtime0.h b/py/runtime0.h index 703c950f2..074dac1e4 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -54,6 +54,22 @@ typedef enum { } mp_unary_op_t; typedef enum { + // Relational operations, should return a bool + MP_BINARY_OP_LESS, + MP_BINARY_OP_MORE, + MP_BINARY_OP_EQUAL, + MP_BINARY_OP_LESS_EQUAL, + MP_BINARY_OP_MORE_EQUAL, + + MP_BINARY_OP_NOT_EQUAL, + MP_BINARY_OP_IN, + MP_BINARY_OP_IS, + MP_BINARY_OP_EXCEPTION_MATCH, + // these are not supported by the runtime and must be synthesised by the emitter + MP_BINARY_OP_NOT_IN, + MP_BINARY_OP_IS_NOT, + + // Arithmetic operations MP_BINARY_OP_OR, MP_BINARY_OP_XOR, MP_BINARY_OP_AND, @@ -83,21 +99,6 @@ typedef enum { MP_BINARY_OP_INPLACE_TRUE_DIVIDE, MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_POWER, - - // these should return a bool - MP_BINARY_OP_LESS, - MP_BINARY_OP_MORE, - MP_BINARY_OP_EQUAL, - MP_BINARY_OP_LESS_EQUAL, - MP_BINARY_OP_MORE_EQUAL, - - MP_BINARY_OP_NOT_EQUAL, - MP_BINARY_OP_IN, - MP_BINARY_OP_IS, - MP_BINARY_OP_EXCEPTION_MATCH, - // these are not supported by the runtime and must be synthesised by the emitter - MP_BINARY_OP_NOT_IN, - MP_BINARY_OP_IS_NOT, } mp_binary_op_t; typedef enum { diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp index 1e015eb03..2dd927f74 100644 --- a/tests/cmdline/cmd_showbc.py.exp +++ b/tests/cmdline/cmd_showbc.py.exp @@ -43,9 +43,9 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): bc=\\d\+ line=126 00 LOAD_CONST_NONE 01 LOAD_CONST_FALSE -02 BINARY_OP 5 __add__ +02 BINARY_OP 16 __add__ 03 LOAD_CONST_TRUE -04 BINARY_OP 5 __add__ +04 BINARY_OP 16 __add__ 05 STORE_FAST 0 06 LOAD_CONST_SMALL_INT 0 07 STORE_FAST 0 @@ -84,7 +84,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ STORE_FAST 7 \\d\+ LOAD_FAST 0 \\d\+ LOAD_DEREF 14 -\\d\+ BINARY_OP 5 __add__ +\\d\+ BINARY_OP 16 __add__ \\d\+ STORE_FAST 8 \\d\+ LOAD_FAST 0 \\d\+ UNARY_OP 4 @@ -96,21 +96,21 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ LOAD_DEREF 14 \\d\+ DUP_TOP \\d\+ ROT_THREE -\\d\+ BINARY_OP 27 __eq__ +\\d\+ BINARY_OP 2 __eq__ \\d\+ JUMP_IF_FALSE_OR_POP \\d\+ \\d\+ LOAD_FAST 1 -\\d\+ BINARY_OP 27 __eq__ +\\d\+ BINARY_OP 2 __eq__ \\d\+ JUMP \\d\+ \\d\+ ROT_TWO \\d\+ POP_TOP \\d\+ STORE_FAST 10 \\d\+ LOAD_FAST 0 \\d\+ LOAD_DEREF 14 -\\d\+ BINARY_OP 27 __eq__ +\\d\+ BINARY_OP 2 __eq__ \\d\+ JUMP_IF_FALSE_OR_POP \\d\+ \\d\+ LOAD_DEREF 14 \\d\+ LOAD_FAST 1 -\\d\+ BINARY_OP 27 __eq__ +\\d\+ BINARY_OP 2 __eq__ \\d\+ UNARY_OP 6 \\d\+ STORE_FAST 10 \\d\+ LOAD_DEREF 14 @@ -132,7 +132,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ DUP_TOP_TWO \\d\+ LOAD_SUBSCR \\d\+ LOAD_FAST 12 -\\d\+ BINARY_OP 18 __iadd__ +\\d\+ BINARY_OP 29 __iadd__ \\d\+ ROT_THREE \\d\+ STORE_SUBSCR \\d\+ LOAD_DEREF 14 @@ -369,7 +369,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): 42 STORE_FAST_N 19 44 LOAD_FAST 9 45 LOAD_FAST_N 19 -47 BINARY_OP 5 __add__ +47 BINARY_OP 16 __add__ 48 POP_TOP 49 LOAD_CONST_NONE 50 RETURN_VALUE @@ -521,7 +521,7 @@ arg names: * bc=\\d\+ line=113 00 LOAD_DEREF 0 02 LOAD_CONST_SMALL_INT 1 -03 BINARY_OP 5 __add__ +03 BINARY_OP 16 __add__ 04 STORE_FAST 1 05 LOAD_CONST_SMALL_INT 1 06 STORE_DEREF 0 @@ -540,7 +540,7 @@ arg names: * b bc=\\d\+ line=139 00 LOAD_FAST 1 01 LOAD_DEREF 0 -03 BINARY_OP 5 __add__ +03 BINARY_OP 16 __add__ 04 RETURN_VALUE mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ From 50b9329eba29a36b6948699aaea9473681d4f46b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 7 Sep 2017 11:26:42 +0300 Subject: [PATCH 010/163] py/runtime0.h: Move MP_BINARY_OP_DIVMOD to the end of mp_binary_op_t. It starts a dichotomy of mp_binary_op_t values which can't appear in the bytecode. Another reason to move it is to VALUES of OP_* and OP_INPLACE_* nicely adjacent. This also will be needed for OP_REVERSE_*, to be soon introduced. --- py/runtime0.h | 6 +++++- tests/cmdline/cmd_showbc.py.exp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/py/runtime0.h b/py/runtime0.h index 074dac1e4..fd497a6d1 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -84,7 +84,6 @@ typedef enum { MP_BINARY_OP_MODULO, MP_BINARY_OP_POWER, - MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime MP_BINARY_OP_INPLACE_OR, MP_BINARY_OP_INPLACE_XOR, @@ -99,6 +98,11 @@ typedef enum { MP_BINARY_OP_INPLACE_TRUE_DIVIDE, MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_POWER, + + // Operations below this line don't appear in bytecode, they + // just identify special methods. + + MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime } mp_binary_op_t; typedef enum { diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp index 2dd927f74..0919aac53 100644 --- a/tests/cmdline/cmd_showbc.py.exp +++ b/tests/cmdline/cmd_showbc.py.exp @@ -132,7 +132,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ DUP_TOP_TWO \\d\+ LOAD_SUBSCR \\d\+ LOAD_FAST 12 -\\d\+ BINARY_OP 29 __iadd__ +\\d\+ BINARY_OP 28 __iadd__ \\d\+ ROT_THREE \\d\+ STORE_SUBSCR \\d\+ LOAD_DEREF 14 From 6d4cac088eb29c7909f81121608546ca1b5e292e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 7 Sep 2017 12:54:58 +0300 Subject: [PATCH 011/163] py/objtype: Make sure mp_binary_op_method_name has full size again. After recent refactorings to mp_binary_op_t, and make it future refactoring proof for now, at the cost of extra element in the array. --- py/objtype.c | 2 +- py/runtime0.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/py/objtype.c b/py/objtype.c index 83a9836c3..8ce593dba 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -453,7 +453,7 @@ const qstr mp_binary_op_method_name[] = { /* MP_BINARY_OP_IS, */ - [MP_BINARY_OP_EXCEPTION_MATCH] = MP_QSTR_, // not implemented, used to make sure array has full size + [MP_BINARY_OP_LAST] = 0, // used to make sure array has full size, TODO: FIXME }; STATIC mp_obj_t instance_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { diff --git a/py/runtime0.h b/py/runtime0.h index fd497a6d1..723ab582d 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -103,6 +103,8 @@ typedef enum { // just identify special methods. MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime + + MP_BINARY_OP_LAST, } mp_binary_op_t; typedef enum { From c460f6f15af681860f7606761da7579c9945761b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 7 Sep 2017 13:37:33 +0300 Subject: [PATCH 012/163] py/runtime0.h: Regroup operations a bit. Originally, there were grouped in blocks of 5, to make it easier e.g. to assess and numeric code of each. But now it makes more sense to group it by semantics/properties, and then split in chunks still, which usually leads to chunks of ~6 ops. --- py/runtime0.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/py/runtime0.h b/py/runtime0.h index 723ab582d..b34211d57 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -60,8 +60,8 @@ typedef enum { MP_BINARY_OP_EQUAL, MP_BINARY_OP_LESS_EQUAL, MP_BINARY_OP_MORE_EQUAL, - MP_BINARY_OP_NOT_EQUAL, + MP_BINARY_OP_IN, MP_BINARY_OP_IS, MP_BINARY_OP_EXCEPTION_MATCH, @@ -75,24 +75,23 @@ typedef enum { MP_BINARY_OP_AND, MP_BINARY_OP_LSHIFT, MP_BINARY_OP_RSHIFT, - MP_BINARY_OP_ADD, + MP_BINARY_OP_SUBTRACT, MP_BINARY_OP_MULTIPLY, MP_BINARY_OP_FLOOR_DIVIDE, MP_BINARY_OP_TRUE_DIVIDE, - MP_BINARY_OP_MODULO, MP_BINARY_OP_POWER, + MP_BINARY_OP_INPLACE_OR, MP_BINARY_OP_INPLACE_XOR, - MP_BINARY_OP_INPLACE_AND, MP_BINARY_OP_INPLACE_LSHIFT, MP_BINARY_OP_INPLACE_RSHIFT, MP_BINARY_OP_INPLACE_ADD, - MP_BINARY_OP_INPLACE_SUBTRACT, + MP_BINARY_OP_INPLACE_SUBTRACT, MP_BINARY_OP_INPLACE_MULTIPLY, MP_BINARY_OP_INPLACE_FLOOR_DIVIDE, MP_BINARY_OP_INPLACE_TRUE_DIVIDE, From b8ee7ab5b988f424e34e778fe3a679710d675efa Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 8 Sep 2017 00:10:10 +0300 Subject: [PATCH 013/163] py/runtime0.h: Put inplace arith ops in front of normal operations. This is to allow to place reverse ops immediately after normal ops, so they can be tested as one range (which is optimization for reverse ops introduction in the next patch). --- py/objint_mpz.c | 2 +- py/runtime0.h | 28 ++++++++++++++-------------- tests/cmdline/cmd_showbc.py.exp | 14 +++++++------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/py/objint_mpz.c b/py/objint_mpz.c index c7a3074ba..0e318b492 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -221,7 +221,7 @@ mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i return mp_obj_new_float(flhs / frhs); #endif - } else if (op >= MP_BINARY_OP_OR) { + } else if (op >= MP_BINARY_OP_INPLACE_OR) { mp_obj_int_t *res = mp_obj_int_new_mpz(); switch (op) { diff --git a/py/runtime0.h b/py/runtime0.h index b34211d57..3cf5e530d 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -70,20 +70,6 @@ typedef enum { MP_BINARY_OP_IS_NOT, // Arithmetic operations - MP_BINARY_OP_OR, - MP_BINARY_OP_XOR, - MP_BINARY_OP_AND, - MP_BINARY_OP_LSHIFT, - MP_BINARY_OP_RSHIFT, - MP_BINARY_OP_ADD, - - MP_BINARY_OP_SUBTRACT, - MP_BINARY_OP_MULTIPLY, - MP_BINARY_OP_FLOOR_DIVIDE, - MP_BINARY_OP_TRUE_DIVIDE, - MP_BINARY_OP_MODULO, - MP_BINARY_OP_POWER, - MP_BINARY_OP_INPLACE_OR, MP_BINARY_OP_INPLACE_XOR, MP_BINARY_OP_INPLACE_AND, @@ -98,6 +84,20 @@ typedef enum { MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_POWER, + MP_BINARY_OP_OR, + MP_BINARY_OP_XOR, + MP_BINARY_OP_AND, + MP_BINARY_OP_LSHIFT, + MP_BINARY_OP_RSHIFT, + MP_BINARY_OP_ADD, + + MP_BINARY_OP_SUBTRACT, + MP_BINARY_OP_MULTIPLY, + MP_BINARY_OP_FLOOR_DIVIDE, + MP_BINARY_OP_TRUE_DIVIDE, + MP_BINARY_OP_MODULO, + MP_BINARY_OP_POWER, + // Operations below this line don't appear in bytecode, they // just identify special methods. diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp index 0919aac53..0e66a013d 100644 --- a/tests/cmdline/cmd_showbc.py.exp +++ b/tests/cmdline/cmd_showbc.py.exp @@ -43,9 +43,9 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): bc=\\d\+ line=126 00 LOAD_CONST_NONE 01 LOAD_CONST_FALSE -02 BINARY_OP 16 __add__ +02 BINARY_OP 28 __add__ 03 LOAD_CONST_TRUE -04 BINARY_OP 16 __add__ +04 BINARY_OP 28 __add__ 05 STORE_FAST 0 06 LOAD_CONST_SMALL_INT 0 07 STORE_FAST 0 @@ -84,7 +84,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ STORE_FAST 7 \\d\+ LOAD_FAST 0 \\d\+ LOAD_DEREF 14 -\\d\+ BINARY_OP 16 __add__ +\\d\+ BINARY_OP 28 __add__ \\d\+ STORE_FAST 8 \\d\+ LOAD_FAST 0 \\d\+ UNARY_OP 4 @@ -132,7 +132,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): \\d\+ DUP_TOP_TWO \\d\+ LOAD_SUBSCR \\d\+ LOAD_FAST 12 -\\d\+ BINARY_OP 28 __iadd__ +\\d\+ BINARY_OP 16 __iadd__ \\d\+ ROT_THREE \\d\+ STORE_SUBSCR \\d\+ LOAD_DEREF 14 @@ -369,7 +369,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): 42 STORE_FAST_N 19 44 LOAD_FAST 9 45 LOAD_FAST_N 19 -47 BINARY_OP 16 __add__ +47 BINARY_OP 28 __add__ 48 POP_TOP 49 LOAD_CONST_NONE 50 RETURN_VALUE @@ -521,7 +521,7 @@ arg names: * bc=\\d\+ line=113 00 LOAD_DEREF 0 02 LOAD_CONST_SMALL_INT 1 -03 BINARY_OP 16 __add__ +03 BINARY_OP 28 __add__ 04 STORE_FAST 1 05 LOAD_CONST_SMALL_INT 1 06 STORE_DEREF 0 @@ -540,7 +540,7 @@ arg names: * b bc=\\d\+ line=139 00 LOAD_FAST 1 01 LOAD_DEREF 0 -03 BINARY_OP 16 __add__ +03 BINARY_OP 28 __add__ 04 RETURN_VALUE mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ From 19f1b39d6ff723729e993c2559c5eb8d62215d39 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Sep 2017 11:19:40 +1000 Subject: [PATCH 014/163] stm32/i2c: When scanning for I2C devices only do 1 probe per address. Previous to this patch the i2c.scan() method would do up to 100 probes per I2C address, to detect the devices on the bus. This repeated probing was a relic from when the code was copied from the accelerometer initialisation, which requires to do repeated probes while waiting for the accelerometer chip to turn on. But I2C devices shouldn't need more than 1 probe to detect their presence, and the generic software I2C implementation uses 1 probe successfully. So this patch changes the implementation to use 1 probe per address, which significantly speeds up the scan operation. --- ports/stm32/i2c.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index d80301081..fcf3cd9c3 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -699,12 +699,9 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) { mp_obj_t list = mp_obj_new_list(0, NULL); for (uint addr = 0x08; addr <= 0x77; addr++) { - for (int i = 0; i < 10; i++) { - HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 10, 200); - if (status == HAL_OK) { - mp_obj_list_append(list, mp_obj_new_int(addr)); - break; - } + HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 1, 200); + if (status == HAL_OK) { + mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); } } From 0708dd495f19ac935641a914358470b1385c3baa Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Sep 2017 12:11:15 +1000 Subject: [PATCH 015/163] tests/run-bench-tests: Update locations of executables, now in ports/. --- tests/run-bench-tests | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/run-bench-tests b/tests/run-bench-tests index d48b4b7ec..f4a6776cb 100755 --- a/tests/run-bench-tests +++ b/tests/run-bench-tests @@ -13,10 +13,10 @@ from collections import defaultdict # to the correct executable. if os.name == 'nt': CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3.exe') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../windows/micropython.exe') + MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/windows/micropython.exe') else: CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../unix/micropython') + MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/unix/micropython') def run_tests(pyb, test_dict): test_count = 0 From cc7fece309b0ce6d361cade8690b6c3a162d7378 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Sep 2017 12:23:33 +1000 Subject: [PATCH 016/163] stm32/modnwwiznet5k: Release the GIL on blocking network operations. connect, send, recv, sendto and recvfrom now release the GIL. accept already releases the GIL because it calls mp_hal_delay_ms() within its busy-wait loop. --- ports/stm32/modnwwiznet5k.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index ffc383524..2d5a8d51a 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -213,7 +213,10 @@ STATIC int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, m } // now connect + MP_THREAD_GIL_EXIT(); mp_int_t ret = WIZCHIP_EXPORT(connect)(socket->u_param.fileno, ip, port); + MP_THREAD_GIL_ENTER(); + if (ret < 0) { wiznet5k_socket_close(socket); *_errno = -ret; @@ -225,7 +228,10 @@ STATIC int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, m } STATIC mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) { + MP_THREAD_GIL_EXIT(); mp_int_t ret = WIZCHIP_EXPORT(send)(socket->u_param.fileno, (byte*)buf, len); + MP_THREAD_GIL_ENTER(); + // TODO convert Wiz errno's to POSIX ones if (ret < 0) { wiznet5k_socket_close(socket); @@ -236,7 +242,10 @@ STATIC mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const by } STATIC mp_uint_t wiznet5k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) { + MP_THREAD_GIL_EXIT(); mp_int_t ret = WIZCHIP_EXPORT(recv)(socket->u_param.fileno, buf, len); + MP_THREAD_GIL_ENTER(); + // TODO convert Wiz errno's to POSIX ones if (ret < 0) { wiznet5k_socket_close(socket); @@ -254,7 +263,10 @@ STATIC mp_uint_t wiznet5k_socket_sendto(mod_network_socket_obj_t *socket, const } } + MP_THREAD_GIL_EXIT(); mp_int_t ret = WIZCHIP_EXPORT(sendto)(socket->u_param.fileno, (byte*)buf, len, ip, port); + MP_THREAD_GIL_ENTER(); + if (ret < 0) { wiznet5k_socket_close(socket); *_errno = -ret; @@ -265,7 +277,9 @@ STATIC mp_uint_t wiznet5k_socket_sendto(mod_network_socket_obj_t *socket, const STATIC mp_uint_t wiznet5k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) { uint16_t port2; + MP_THREAD_GIL_EXIT(); mp_int_t ret = WIZCHIP_EXPORT(recvfrom)(socket->u_param.fileno, buf, len, ip, &port2); + MP_THREAD_GIL_ENTER(); *port = port2; if (ret < 0) { wiznet5k_socket_close(socket); From da1c80d8509062f155450f09a7112e57e2e14f42 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Thu, 31 Aug 2017 11:14:13 +0100 Subject: [PATCH 017/163] docs/reference/isr_rules.rst Add tutorial on use of micropython.schedule(). --- docs/reference/isr_rules.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/reference/isr_rules.rst b/docs/reference/isr_rules.rst index 23dcfd01f..5009f30f7 100644 --- a/docs/reference/isr_rules.rst +++ b/docs/reference/isr_rules.rst @@ -21,6 +21,7 @@ This summarises the points detailed below and lists the principal recommendation * Keep the code as short and simple as possible. * Avoid memory allocation: no appending to lists or insertion into dictionaries, no floating point. +* Consider using ``micropython.schedule`` to work around the above constraint. * Where an ISR returns multiple bytes use a pre-allocated ``bytearray``. If multiple integers are to be shared between an ISR and the main program consider an array (``array.array``). * Where data is shared between the main program and an ISR, consider disabling interrupts prior to accessing @@ -158,6 +159,26 @@ On platforms with hardware floating point (such as the Pyboard) the inline ARM T round this limitation. This is because the processor stores float values in a machine word; values can be shared between the ISR and main program code via an array of floats. +Using micropython.schedule +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This function enables an ISR to schedule a callback for execution "very soon". The callback is queued for +execution which will take place at a time when the heap is not locked. Hence it can create Python objects +and use floats. The callback is also guaranteed to run at a time when the main program has completed any +update of Python objects, so the callback will not encounter partially updated objects. + +Typical usage is to handle sensor hardware. The ISR acquires data from the hardware and enables it to +issue a further interrupt. It then schedules a callback to process the data. + +Scheduled callbacks should comply with the principles of interrupt handler design outlined below. This is to +avoid problems resulting from I/O activity and the modification of shared data which can arise in any code +which pre-empts the main program loop. + +Execution time needs to be considered in relation to the frequency with which interrupts can occur. If an +interrupt occurs while the previous callback is executing, a further instance of the callback will be queued +for execution; this will run after the current instance has completed. A sustained high interrupt repetition +rate therefore carries a risk of unconstrained queue growth and eventual failure with a ``RuntimeError``. + Exceptions ---------- From e6fbee0981611bed51a628dde77ee75c82609111 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 10 Sep 2017 15:15:41 +1000 Subject: [PATCH 018/163] py/builtinhelp: Simplify code slightly by extracting object type. Reduces code size by about 10 bytes. --- py/builtinhelp.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/py/builtinhelp.c b/py/builtinhelp.c index dbcd6e00f..e10e48b7d 100644 --- a/py/builtinhelp.c +++ b/py/builtinhelp.c @@ -136,20 +136,19 @@ STATIC void mp_help_print_obj(const mp_obj_t obj) { } #endif + mp_obj_type_t *type = mp_obj_get_type(obj); + // try to print something sensible about the given object mp_print_str(MP_PYTHON_PRINTER, "object "); mp_obj_print(obj, PRINT_STR); - mp_printf(MP_PYTHON_PRINTER, " is of type %s\n", mp_obj_get_type_str(obj)); + mp_printf(MP_PYTHON_PRINTER, " is of type %q\n", type->name); mp_map_t *map = NULL; - if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) { + if (type == &mp_type_module) { map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj)); } else { - mp_obj_type_t *type; - if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) { - type = obj; - } else { - type = mp_obj_get_type(obj); + if (type == &mp_type_type) { + type = MP_OBJ_TO_PTR(obj); } if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) { map = mp_obj_dict_get_map(type->locals_dict); From bd71b3252ab8b790547729730b40d5b46737ab9d Mon Sep 17 00:00:00 2001 From: Tobias Badertscher Date: Sun, 3 Sep 2017 13:57:57 +0200 Subject: [PATCH 019/163] stm32/boards: Add new board B_L475E_IOT01A based on STM32L475. --- ports/stm32/adc.c | 2 +- .../boards/B_L475E_IOT01A/mpconfigboard.h | 76 ++++ .../boards/B_L475E_IOT01A/mpconfigboard.mk | 8 + ports/stm32/boards/B_L475E_IOT01A/pins.csv | 82 ++++ .../B_L475E_IOT01A/stm32l4xx_hal_conf.h | 373 ++++++++++++++++++ ports/stm32/storage.c | 4 +- 6 files changed, 542 insertions(+), 3 deletions(-) create mode 100644 ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h create mode 100644 ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.mk create mode 100644 ports/stm32/boards/B_L475E_IOT01A/pins.csv create mode 100644 ports/stm32/boards/B_L475E_IOT01A/stm32l4xx_hal_conf.h diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index dd59e29c8..4192e7fed 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -95,7 +95,7 @@ defined(STM32F746xx) || defined(STM32F767xx) || \ defined(STM32F769xx) || defined(STM32F446xx) #define VBAT_DIV (4) -#elif defined(STM32L476xx) +#elif defined(STM32L475xx) || defined(STM32L476xx) #define VBAT_DIV (3) #else #error Unsupported processor diff --git a/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h new file mode 100644 index 000000000..77f029c00 --- /dev/null +++ b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h @@ -0,0 +1,76 @@ +#define MICROPY_HW_BOARD_NAME "B-L475E-IOT01A" +#define MICROPY_HW_MCU_NAME "STM32L475" + +#define MICROPY_HW_HAS_SWITCH (1) +#define MICROPY_HW_HAS_FLASH (0) +#define MICROPY_HW_HAS_SDCARD (0) +#define MICROPY_HW_HAS_MMA7660 (0) +#define MICROPY_HW_HAS_LIS3DSH (0) +#define MICROPY_HW_HAS_LCD (0) +#define MICROPY_HW_ENABLE_RNG (1) +#define MICROPY_HW_ENABLE_RTC (1) +#define MICROPY_HW_ENABLE_TIMER (1) +#define MICROPY_HW_ENABLE_SERVO (0) +#define MICROPY_HW_ENABLE_DAC (0) +#define MICROPY_HW_ENABLE_CAN (0) + +// MSI is used and is 4MHz +#define MICROPY_HW_CLK_PLLM (1) +#define MICROPY_HW_CLK_PLLN (40) +#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV7) +#define MICROPY_HW_CLK_PLLR (RCC_PLLR_DIV2) +#define MICROPY_HW_CLK_PLLQ (RCC_PLLQ_DIV4) + +#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 + +// USART1 config connected to ST-Link +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +// USART2 config connected to PMOD: Flow control is defined and therfore used +#define MICROPY_HW_UART2_CTS (pin_D3) +#define MICROPY_HW_UART2_RTS (pin_D4) +#define MICROPY_HW_UART2_TX (pin_D5) +#define MICROPY_HW_UART2_RX (pin_D6) +// USART3 config for internal use +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +// USART4 config +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +// USART 1 is connected to the virtual com port on the ST-LINK +#define MICROPY_HW_UART_REPL PYB_UART_1 +#define MICROPY_HW_UART_REPL_BAUD 115200 + +// I2C busses +#define MICROPY_HW_I2C1_SCL (pin_B8) +#define MICROPY_HW_I2C1_SDA (pin_B9) +#define MICROPY_HW_I2C2_SCL (pin_B10) +#define MICROPY_HW_I2C2_SDA (pin_B11) + +// SPI busses +#define MICROPY_HW_SPI1_NSS (pin_A4) +#define MICROPY_HW_SPI1_SCK (pin_A5) +#define MICROPY_HW_SPI1_MISO (pin_A6) +#define MICROPY_HW_SPI1_MOSI (pin_A7) + +#define MICROPY_HW_SPI2_NSS (pin_D0) +#define MICROPY_HW_SPI2_SCK (pin_D1) +#define MICROPY_HW_SPI2_MISO (pin_D3) +#define MICROPY_HW_SPI2_MOSI (pin_D4) + +#define MICROPY_HW_SPI3_NSS (pin_A15) +#define MICROPY_HW_SPI3_SCK (pin_C10) +#define MICROPY_HW_SPI3_MISO (pin_C11) +#define MICROPY_HW_SPI3_MOSI (pin_C12) + +// User and wake-up switch. Pressing the button makes the input go low. +#define MICROPY_HW_USRSW_PIN (pin_C13) +#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) +#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PRESSED (0) + +// LEDs +#define MICROPY_HW_LED1 (pin_A5) // green +#define MICROPY_HW_LED2 (pin_B14) // green +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) diff --git a/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.mk b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.mk new file mode 100644 index 000000000..1ba61a327 --- /dev/null +++ b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.mk @@ -0,0 +1,8 @@ +MCU_SERIES = l4 +CMSIS_MCU = STM32L475xx +# The stm32l475 does not have a LDC controller which is +# the only diffrence to the stm32l476 - so reuse some files. +AF_FILE = boards/stm32l476_af.csv +LD_FILE = boards/stm32l476xg.ld +TEXT_ADDR = 0x08004000 +OPENOCD_CONFIG = boards/openocd_stm32l4.cfg diff --git a/ports/stm32/boards/B_L475E_IOT01A/pins.csv b/ports/stm32/boards/B_L475E_IOT01A/pins.csv new file mode 100644 index 000000000..afe87b71c --- /dev/null +++ b/ports/stm32/boards/B_L475E_IOT01A/pins.csv @@ -0,0 +1,82 @@ +PA0,PA0 +PA1,PA1 +PA2,PA2 +PA3,PA3 +PA4,PA4 +PA5,PA5 +PA6,PA6 +PA7,PA7 +PA8,PA8 +PA9,PA9 +PA10,PA10 +PA11,PA11 +PA12,PA12 +PA13,PA13 +PA14,PA14 +PA15,PA15 +PB0,PB0 +PB1,PB1 +PB2,PB2 +PB3,PB3 +PB4,PB4 +PB5,PB5 +PB6,PB6 +PB7,PB7 +PB8,PB8 +PB9,PB9 +PB10,PB10 +PB11,PB11 +PB12,PB12 +PB13,PB13 +PB14,PB14 +PB15,PB15 +PC0,PC0 +PC1,PC1 +PC2,PC2 +PC3,PC3 +PC4,PC4 +PC5,PC5 +PC6,PC6 +PC7,PC7 +PC8,PC8 +PC9,PC9 +PC10,PC10 +PC11,PC11 +PC12,PC12 +PC13,PC13 +PC14,PC14 +PC15,PC15 +PD0,PD0 +PD1,PD1 +PD2,PD2 +PD3,PD3 +PD4,PD4 +PD5,PD5 +PD6,PD6 +PD7,PD7 +PD8,PD8 +PD9,PD9 +PD10,PD10 +PD11,PD11 +PD12,PD12 +PD13,PD13 +PD14,PD14 +PD15,PD15 +PE0,PE0 +PE1,PE1 +PE2,PE2 +PE3,PE3 +PE4,PE4 +PE5,PE5 +PE6,PE6 +PE7,PE7 +PE8,PE8 +PE9,PE9 +PE10,PE10 +PE11,PE11 +PE12,PE12 +PE13,PE13 +PE14,PE14 +PE15,PE15 +PH0,PH0 +PH1,PH1 diff --git a/ports/stm32/boards/B_L475E_IOT01A/stm32l4xx_hal_conf.h b/ports/stm32/boards/B_L475E_IOT01A/stm32l4xx_hal_conf.h new file mode 100644 index 000000000..9348e0679 --- /dev/null +++ b/ports/stm32/boards/B_L475E_IOT01A/stm32l4xx_hal_conf.h @@ -0,0 +1,373 @@ +/** + ****************************************************************************** + * @file stm32l4xx_hal_conf.h + * @author MCD Application Team + * @version V1.2.0 + * @date 25-November-2015 + * @brief HAL configuration template file. + * This file should be copied to the application folder and renamed + * to stm32l4xx_hal_conf.h. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2015 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32L4xx_HAL_CONF_H +#define __STM32L4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +#define USE_USB_FS +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +/* #define HAL_COMP_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +/* #define HAL_CRC_MODULE_ENABLED */ +/* #define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/* #define HAL_DFSDM_MODULE_ENABLED */ +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_FIREWALL_MODULE_ENABLED */ +#define HAL_FLASH_MODULE_ENABLED +/* #define HAL_HCD_MODULE_ENABLED */ +/* #define HAL_NAND_MODULE_ENABLED */ +/* #define HAL_NOR_MODULE_ENABLED */ +/* #define HAL_SRAM_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/* #define HAL_IRDA_MODULE_ENABLED */ +/* #define HAL_IWDG_MODULE_ENABLED */ +/* #define HAL_LCD_MODULE_ENABLED */ +/* #define HAL_LPTIM_MODULE_ENABLED */ +/* #define HAL_OPAMP_MODULE_ENABLED */ +#define HAL_PCD_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +/* #define HAL_QSPI_MODULE_ENABLED */ +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +/* #define HAL_SMARTCARD_MODULE_ENABLED */ +/* #define HAL_SMBUS_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED +/* #define HAL_SWPMI_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED +/* #define HAL_TSC_MODULE_ENABLED */ +#define HAL_UART_MODULE_ENABLED +/* #define HAL_USART_MODULE_ENABLED */ +/* #define HAL_WWDG_MODULE_ENABLED */ + + +/* ########################## Oscillator Values adaptation ####################*/ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal Multiple Speed oscillator (MSI) default value. + * This value is the default MSI range value after Reset. + */ +#if !defined (MSI_VALUE) + #define MSI_VALUE ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* MSI_VALUE */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)32000) /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External oscillator in Hz*/ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for SAI1 peripheral + * This value is used by the RCC HAL module to compute the SAI1 & SAI2 clock source + * frequency. + */ +#if !defined (EXTERNAL_SAI1_CLOCK_VALUE) + #define EXTERNAL_SAI1_CLOCK_VALUE ((uint32_t)48000) /*!< Value of the SAI1 External clock source in Hz*/ +#endif /* EXTERNAL_SAI1_CLOCK_VALUE */ + +/** + * @brief External clock source for SAI2 peripheral + * This value is used by the RCC HAL module to compute the SAI1 & SAI2 clock source + * frequency. + */ +#if !defined (EXTERNAL_SAI2_CLOCK_VALUE) + #define EXTERNAL_SAI2_CLOCK_VALUE ((uint32_t)48000) /*!< Value of the SAI2 External clock source in Hz*/ +#endif /* EXTERNAL_SAI2_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0x00) /*!< tick interrupt priority */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32l4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32l4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32l4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_DFSDM_MODULE_ENABLED + #include "stm32l4xx_hal_dfsdm.h" +#endif /* HAL_DFSDM_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32l4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32l4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32l4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED + #include "stm32l4xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32l4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32l4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32l4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_FIREWALL_MODULE_ENABLED + #include "stm32l4xx_hal_firewall.h" +#endif /* HAL_FIREWALL_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32l4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32l4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32l4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32l4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32l4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32l4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LCD_MODULE_ENABLED + #include "stm32l4xx_hal_lcd.h" +#endif /* HAL_LCD_MODULE_ENABLED */ + +#ifdef HAL_LPTIM_MODULE_ENABLED +#include "stm32l4xx_hal_lptim.h" +#endif /* HAL_LPTIM_MODULE_ENABLED */ + +#ifdef HAL_OPAMP_MODULE_ENABLED +#include "stm32l4xx_hal_opamp.h" +#endif /* HAL_OPAMP_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32l4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_QSPI_MODULE_ENABLED + #include "stm32l4xx_hal_qspi.h" +#endif /* HAL_QSPI_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32l4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32l4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32l4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32l4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED + #include "stm32l4xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32l4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_SWPMI_MODULE_ENABLED + #include "stm32l4xx_hal_swpmi.h" +#endif /* HAL_SWPMI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32l4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_TSC_MODULE_ENABLED + #include "stm32l4xx_hal_tsc.h" +#endif /* HAL_TSC_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32l4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32l4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32l4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32l4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32l4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32l4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32l4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32L4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index af75ccda4..4b329c2db 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -92,12 +92,12 @@ STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k #define FLASH_MEM_SEG1_START_ADDR (0x08008000) // sector 1 #define FLASH_MEM_SEG1_NUM_BLOCKS (192) // sectors 1,2,3: 32k+32k+32=96k -#elif defined(STM32L476xx) +#elif defined(STM32L475xx) || defined(STM32L476xx) extern uint8_t _flash_fs_start; extern uint8_t _flash_fs_end; -// The STM32L476 doesn't have CCRAM, so we use the 32K SRAM2 for this. +// The STM32L475/6 doesn't have CCRAM, so we use the 32K SRAM2 for this. #define CACHE_MEM_START_ADDR (0x10000000) // SRAM2 data RAM, 32k #define FLASH_SECTOR_SIZE_MAX (0x00800) // 2k max #define FLASH_MEM_SEG1_START_ADDR ((long)&_flash_fs_start) From 5671a11b815b526289837ec4f132e5c94a02e9b6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 09:47:20 +0300 Subject: [PATCH 020/163] esp8266: Rename axtls_helpers.c to posix_helpers.c. As it's used by BerkeleyDB, etc. --- ports/esp8266/Makefile | 2 +- ports/esp8266/{axtls_helpers.c => posix_helpers.c} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename ports/esp8266/{axtls_helpers.c => posix_helpers.c} (97%) diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 70b4ce35c..177a32353 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -85,7 +85,7 @@ SRC_C = \ moduos.c \ ets_alt_task.c \ fatfs_port.c \ - axtls_helpers.c \ + posix_helpers.c \ hspi.c \ $(SRC_MOD) diff --git a/ports/esp8266/axtls_helpers.c b/ports/esp8266/posix_helpers.c similarity index 97% rename from ports/esp8266/axtls_helpers.c rename to ports/esp8266/posix_helpers.c index 6d508fdeb..f8e4880d2 100644 --- a/ports/esp8266/axtls_helpers.c +++ b/ports/esp8266/posix_helpers.c @@ -29,7 +29,7 @@ #include "py/mphal.h" #include "py/gc.h" -// Functions for axTLS +// Functions for external libs like axTLS, BerkeleyDB, etc. void *malloc(size_t size) { return gc_alloc(size, false); From 9b4666dad55be2cc978b3dd5da89fd11da0c3cfc Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 09:55:18 +0300 Subject: [PATCH 021/163] esp8266/posix_helpers: Set ENOMEM on memory alloc failure. POSIX requires malloc(), etc. to set ENOMEM on the failure, and e.g. BerkeleyDB relies on this: http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html This should fix confusing OSError exceptions with 0 error code when working with btree module. --- ports/esp8266/posix_helpers.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ports/esp8266/posix_helpers.c b/ports/esp8266/posix_helpers.c index f8e4880d2..1fc677c5c 100644 --- a/ports/esp8266/posix_helpers.c +++ b/ports/esp8266/posix_helpers.c @@ -26,22 +26,33 @@ #include #include +#include #include "py/mphal.h" #include "py/gc.h" // Functions for external libs like axTLS, BerkeleyDB, etc. void *malloc(size_t size) { - return gc_alloc(size, false); + void *p = gc_alloc(size, false); + if (p == NULL) { + // POSIX requires ENOMEM to be set in case of error + errno = ENOMEM; + } + return p; } void free(void *ptr) { gc_free(ptr); } void *calloc(size_t nmemb, size_t size) { - return m_malloc0(nmemb * size); + return malloc(nmemb * size); } void *realloc(void *ptr, size_t size) { - return gc_realloc(ptr, size, true); + void *p = gc_realloc(ptr, size, true); + if (p == NULL) { + // POSIX requires ENOMEM to be set in case of error + errno = ENOMEM; + } + return p; } #define PLATFORM_HTONL(_n) ((uint32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) )) From e41bc3fcbb098fba4fbb8a9a4119e1dda5519ddd Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 13:51:51 +0300 Subject: [PATCH 022/163] berkeley-db-1.xx: Update, allow to override MINCACHE, DEFPSIZE. --- lib/berkeley-db-1.xx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/berkeley-db-1.xx b/lib/berkeley-db-1.xx index dab957dac..35aaec441 160000 --- a/lib/berkeley-db-1.xx +++ b/lib/berkeley-db-1.xx @@ -1 +1 @@ -Subproject commit dab957dacddcbf6cbc85d42df62e189e4877bb72 +Subproject commit 35aaec4418ad78628a3b935885dd189d41ce779b From 9355cca610e2e7f16a5847271fefe47db97b8cc4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 13:54:00 +0300 Subject: [PATCH 023/163] esp8266: Set DEFPSIZE=1024, MINCACHE=3 for "btree" module. Defaults of 4096 and 5 respectively are too high to esp8266, causing out of memory with a database beyond couple of pages. --- ports/esp8266/Makefile | 1 + py/py.mk | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 177a32353..95236a8d9 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -7,6 +7,7 @@ MICROPY_PY_USSL = 1 MICROPY_SSL_AXTLS = 1 MICROPY_FATFS = 1 MICROPY_PY_BTREE = 1 +BTREE_DEFS_EXTRA = -DDEFPSIZE=1024 -DMINCACHE=3 FROZEN_DIR ?= scripts FROZEN_MPY_DIR ?= modules diff --git a/py/py.mk b/py/py.mk index 663237603..f5faad182 100644 --- a/py/py.mk +++ b/py/py.mk @@ -74,7 +74,7 @@ endif ifeq ($(MICROPY_PY_BTREE),1) BTREE_DIR = lib/berkeley-db-1.xx -BTREE_DEFS = -D__DBINTERFACE_PRIVATE=1 -Dmpool_error=printf -Dabort=abort_ -Dvirt_fd_t=mp_obj_t "-DVIRT_FD_T_HEADER=" +BTREE_DEFS = -D__DBINTERFACE_PRIVATE=1 -Dmpool_error=printf -Dabort=abort_ -Dvirt_fd_t=mp_obj_t "-DVIRT_FD_T_HEADER=" $(BTREE_DEFS_EXTRA) INC += -I$(TOP)/$(BTREE_DIR)/PORT/include SRC_MOD += extmod/modbtree.c SRC_MOD += $(addprefix $(BTREE_DIR)/,\ From de981040b392685b0d7f2381a63526b71e633b9c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Sep 2017 12:35:25 +1000 Subject: [PATCH 024/163] travis: Use --upgrade when pip is installing cpp-coveralls. So that the latest urllib3 is retrieved, which has improved SSL security. This fixes the temporary path from f578947ae3fee5610c5bc1123baf878b92eaa248 --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index db51567f1..83ac2a112 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,9 +19,8 @@ before_script: - sudo apt-get install -y --force-yes gcc-arm-none-eabi # For teensy build - sudo apt-get install realpath - # For coverage testing - # cpp-coveralls 0.4 conflicts with urllib3 preinstalled in Travis VM - - sudo pip install cpp-coveralls==0.3.12 + # For coverage testing (upgrade is used to get latest urllib3 version) + - sudo pip install --upgrade cpp-coveralls - gcc --version - arm-none-eabi-gcc --version - python3 --version From eb84a830df62813f5af7f0144fc77444bf18f3a8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 17:05:20 +0300 Subject: [PATCH 025/163] py/runtime: Implement dispatch for "reverse op" special methods. If, for class X, X.__add__(Y) doesn't exist (or returns NotImplemented), try Y.__radd__(X) instead. This patch could be simpler, but requires undoing operand swap and operation switch to get non-confusing error message in case __radd__ doesn't exist. --- ports/unix/mpconfigport_coverage.h | 1 + py/mpconfig.h | 10 +++++++++- py/objtype.c | 7 +++++++ py/runtime.c | 15 ++++++++++++++- py/runtime0.h | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ports/unix/mpconfigport_coverage.h b/ports/unix/mpconfigport_coverage.h index a9e0a38fa..367b4853a 100644 --- a/ports/unix/mpconfigport_coverage.h +++ b/ports/unix/mpconfigport_coverage.h @@ -35,6 +35,7 @@ #define MICROPY_FLOAT_HIGH_QUALITY_HASH (1) #define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_PY_DELATTR_SETATTR (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_MODULES (1) #define MICROPY_PY_SYS_GETSIZEOF (1) diff --git a/py/mpconfig.h b/py/mpconfig.h index 38cf4b560..63438a846 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -759,11 +759,19 @@ typedef double mp_float_t; #endif // Whether to support complete set of special methods -// for user classes, otherwise only the most used +// for user classes, or only the most used ones. "Reverse" +// methods are controlled by MICROPY_PY_REVERSE_SPECIAL_METHODS +// below. #ifndef MICROPY_PY_ALL_SPECIAL_METHODS #define MICROPY_PY_ALL_SPECIAL_METHODS (0) #endif +// Whether to support reverse arithmetic operarions methods +// (__radd__, etc.) +#ifndef MICROPY_PY_REVERSE_SPECIAL_METHODS +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (0) +#endif + // Whether to support compile function #ifndef MICROPY_PY_BUILTINS_COMPILE #define MICROPY_PY_BUILTINS_COMPILE (0) diff --git a/py/objtype.c b/py/objtype.c index 8ce593dba..cf9311d5c 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -441,6 +441,13 @@ const qstr mp_binary_op_method_name[] = { MP_BINARY_OP_INPLACE_TRUE_DIVIDE, MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_POWER,*/ + + #if MICROPY_PY_REVERSE_SPECIAL_METHODS + [MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__, + [MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__, + [MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__, + #endif + [MP_BINARY_OP_LESS] = MP_QSTR___lt__, [MP_BINARY_OP_MORE] = MP_QSTR___gt__, [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__, diff --git a/py/runtime.c b/py/runtime.c index 21ef42577..c533558da 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -555,7 +555,20 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { } } - // TODO implement dispatch for reverse binary ops +#if MICROPY_PY_REVERSE_SPECIAL_METHODS + if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_REVERSE_POWER) { + mp_obj_t t = rhs; + rhs = lhs; + lhs = t; + if (op <= MP_BINARY_OP_POWER) { + op += MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR; + goto generic_binary_op; + } + + // Convert __rop__ back to __op__ for error message + op -= MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR; + } +#endif unsupported_op: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { diff --git a/py/runtime0.h b/py/runtime0.h index 3cf5e530d..a3e9d46b9 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -101,6 +101,23 @@ typedef enum { // Operations below this line don't appear in bytecode, they // just identify special methods. + // MP_BINARY_OP_REVERSE_* must follow immediately after MP_BINARY_OP_* +#if MICROPY_PY_REVERSE_SPECIAL_METHODS + MP_BINARY_OP_REVERSE_OR, + MP_BINARY_OP_REVERSE_XOR, + MP_BINARY_OP_REVERSE_AND, + MP_BINARY_OP_REVERSE_LSHIFT, + MP_BINARY_OP_REVERSE_RSHIFT, + MP_BINARY_OP_REVERSE_ADD, + + MP_BINARY_OP_REVERSE_SUBTRACT, + MP_BINARY_OP_REVERSE_MULTIPLY, + MP_BINARY_OP_REVERSE_FLOOR_DIVIDE, + MP_BINARY_OP_REVERSE_TRUE_DIVIDE, + MP_BINARY_OP_REVERSE_MODULO, + MP_BINARY_OP_REVERSE_POWER, +#endif + MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime MP_BINARY_OP_LAST, From d6f9d64d97af245d042cbab8438dde25a29c504f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 17:05:31 +0300 Subject: [PATCH 026/163] tests/class_reverse_op: Test for reverse arith ops special methods. This test should be run only if support for reverse ops is enabled, so the corresponding feature_check is added to run-tests. --- ports/qemu-arm/mpconfigport.h | 1 + tests/basics/class_reverse_op.py | 18 ++++++++++++++++++ tests/feature_check/reverse_ops.py | 9 +++++++++ tests/feature_check/reverse_ops.py.exp | 0 tests/run-tests | 7 +++++++ 5 files changed, 35 insertions(+) create mode 100644 tests/basics/class_reverse_op.py create mode 100644 tests/feature_check/reverse_ops.py create mode 100644 tests/feature_check/reverse_ops.py.exp diff --git a/ports/qemu-arm/mpconfigport.h b/ports/qemu-arm/mpconfigport.h index a12165a92..51706b927 100644 --- a/ports/qemu-arm/mpconfigport.h +++ b/ports/qemu-arm/mpconfigport.h @@ -18,6 +18,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) diff --git a/tests/basics/class_reverse_op.py b/tests/basics/class_reverse_op.py new file mode 100644 index 000000000..d41c55c9d --- /dev/null +++ b/tests/basics/class_reverse_op.py @@ -0,0 +1,18 @@ +class A: + + def __init__(self, v): + self.v = v + + def __add__(self, o): + if isinstance(o, A): + return A(self.v + o.v) + return A(self.v + o) + + def __radd__(self, o): + return A(self.v + o) + + def __repr__(self): + return "A(%s)" % self.v + +print(A(3) + 1) +print(2 + A(5)) diff --git a/tests/feature_check/reverse_ops.py b/tests/feature_check/reverse_ops.py new file mode 100644 index 000000000..668748bc5 --- /dev/null +++ b/tests/feature_check/reverse_ops.py @@ -0,0 +1,9 @@ +class Foo: + + def __radd__(self, other): + pass + +try: + 5 + Foo() +except TypeError: + print("TypeError") diff --git a/tests/feature_check/reverse_ops.py.exp b/tests/feature_check/reverse_ops.py.exp new file mode 100644 index 000000000..e69de29bb diff --git a/tests/run-tests b/tests/run-tests index 14df1e986..c9f9efe77 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -207,6 +207,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_set_type = False skip_async = False skip_const = False + skip_revops = False # Check if micropython.native is supported, and skip such tests if it's not native = run_feature_check(pyb, args, base_path, 'native_check.py') @@ -233,6 +234,11 @@ def run_tests(pyb, tests, args, base_path="."): if native == b'CRASH': skip_const = True + # Check if __rOP__ special methods are supported, and skip such tests if it's not + native = run_feature_check(pyb, args, base_path, 'reverse_ops.py') + if native == b'TypeError\n': + skip_revops = True + # Check if emacs repl is supported, and skip such tests if it's not t = run_feature_check(pyb, args, base_path, 'repl_emacs_check.py') if not 'True' in str(t, 'ascii'): @@ -360,6 +366,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_it |= skip_set_type and is_set_type skip_it |= skip_async and is_async skip_it |= skip_const and is_const + skip_it |= skip_revops and test_name.startswith("class_reverse_op") if skip_it: print("skip ", test_file) From c46d480adcaea85375b68da5819561ab91eab001 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 22:25:43 +0300 Subject: [PATCH 027/163] zephyr/Makefile: Revamp "test" target after ports were moved to ports/. --- ports/zephyr/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile index 8b7cb76c3..2e6cb41b9 100644 --- a/ports/zephyr/Makefile +++ b/ports/zephyr/Makefile @@ -102,4 +102,4 @@ prj_$(BOARD)_merged.conf: prj_base.conf prj_$(BOARD).conf $(PYTHON) makeprj.py prj_base.conf prj_$(BOARD).conf $@ test: - cd $(TOP)/tests && ./run-tests --target minimal --device "execpty:make -C ../zephyr run BOARD=$(BOARD) QEMU_PTY=1" + cd $(TOP)/tests && ./run-tests --target minimal --device "execpty:make -C ../ports/zephyr run BOARD=$(BOARD) QEMU_PTY=1" From d1f909005a40469b9d933a7ad75bb3327030538e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 22:32:08 +0300 Subject: [PATCH 028/163] tests/run-tests: Skip class_inplace_op for minimal profile. Don't assume that MICROPY_PY_ALL_SPECIAL_METHODS is defined, as required for inplace special methods. Fixes Zephyr tests. --- tests/run-tests | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/run-tests b/tests/run-tests index c9f9efe77..2a43ff93d 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -308,6 +308,7 @@ def run_tests(pyb, tests, args, base_path="."): elif args.target == 'esp8266': skip_tests.add('misc/rge_sm.py') # too large elif args.target == 'minimal': + skip_tests.add('basics/class_inplace_op.py') # all special methods not supported skip_tests.add('misc/rge_sm.py') # too large skip_tests.add('micropython/opt_level.py') # don't assume line numbers are stored From f54b3527f20291bc7b158b675bebe39e381067d8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 22:38:18 +0300 Subject: [PATCH 029/163] tests/run-tests: Fix copy-paste mistake in var name. --- tests/run-tests | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/run-tests b/tests/run-tests index 2a43ff93d..866a9b7c3 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -210,33 +210,33 @@ def run_tests(pyb, tests, args, base_path="."): skip_revops = False # Check if micropython.native is supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'native_check.py') - if native == b'CRASH': + output = run_feature_check(pyb, args, base_path, 'native_check.py') + if output == b'CRASH': skip_native = True # Check if arbitrary-precision integers are supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'int_big.py') - if native != b'1000000000000000000000000000000000000000000000\n': + output = run_feature_check(pyb, args, base_path, 'int_big.py') + if output != b'1000000000000000000000000000000000000000000000\n': skip_int_big = True # Check if set type (and set literals) is supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'set_check.py') - if native == b'CRASH': + output = run_feature_check(pyb, args, base_path, 'set_check.py') + if output == b'CRASH': skip_set_type = True # Check if async/await keywords are supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'async_check.py') - if native == b'CRASH': + output = run_feature_check(pyb, args, base_path, 'async_check.py') + if output == b'CRASH': skip_async = True # Check if const keyword (MicroPython extension) is supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'const.py') - if native == b'CRASH': + output = run_feature_check(pyb, args, base_path, 'const.py') + if output == b'CRASH': skip_const = True # Check if __rOP__ special methods are supported, and skip such tests if it's not - native = run_feature_check(pyb, args, base_path, 'reverse_ops.py') - if native == b'TypeError\n': + output = run_feature_check(pyb, args, base_path, 'reverse_ops.py') + if output == b'TypeError\n': skip_revops = True # Check if emacs repl is supported, and skip such tests if it's not From 6e06512e0ff47b8022d478fd4ff3f85b96a980a0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 11 Sep 2017 00:33:39 +0300 Subject: [PATCH 030/163] README: Update "Dependencies" section. Given that various ports now require submodules, rewrite the section to be more generic. Also, add git submodule update command to other sections for easy user start. --- README.md | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ff609dba3..13529ff4f 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Alternatively, fallback implementation based on setjmp/longjmp can be used. To build (see section below for required dependencies): + $ git submodule update --init $ cd ports/unix $ make axtls $ make @@ -104,32 +105,36 @@ Standard library modules come from External dependencies --------------------- -Building Unix version requires some dependencies installed. For +Building MicroPython ports may require some dependencies installed. + +For Unix port, `libffi` library and `pkg-config` tool are required. On Debian/Ubuntu/Mint derivative Linux distros, install `build-essential` (includes toolchain and make), `libffi-dev`, and `pkg-config` packages. -Other dependencies can be built together with MicroPython. Oftentimes, -you need to do this to enable extra features or capabilities. To build +Other dependencies can be built together with MicroPython. This may +be required to enable extra features or capabilities, and in recent +versions of MicroPython, these may be enabled by default. To build these additional dependencies, first fetch git submodules for them: $ git submodule update --init -Use this same command to get the latest versions of dependencies, as -they are updated from time to time. After that, in `ports/unix/` dir, execute: +Use the same command to get the latest versions of dependencies, as +they are updated from time to time. After that, in the port directory +(e.g. `ports/unix/`), execute: $ make deplibs This will build all available dependencies (regardless whether they are used or not). If you intend to build MicroPython with additional options (like cross-compiling), the same set of options should be passed -to `make deplibs`. To actually enabled use of dependencies, edit +to `make deplibs`. To actually enable/disable use of dependencies, edit `ports/unix/mpconfigport.mk` file, which has inline descriptions of the options. -For example, to build SSL module (required for `upip` tool described above), -set `MICROPY_PY_USSL` to 1. +For example, to build SSL module (required for `upip` tool described above, +and so enabled by dfeault), `MICROPY_PY_USSL` should be set to 1. -In `ports/unix/mpconfigport.mk`, you can also disable some dependencies enabled -by default, like FFI support, which requires libffi development files to -be installed. +For some ports, building required dependences is transparent, and happens +automatically. They still need to be fetched with the git submodule command +above. The STM32 version ----------------- @@ -141,6 +146,7 @@ https://launchpad.net/gcc-arm-embedded To build: + $ git submodule update --init $ cd ports/stm32 $ make From eea5fcc4428e32f6abd3c5fbf8fa1fe41d206381 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Sep 2017 15:31:43 +1000 Subject: [PATCH 031/163] stm32/make-stmconst.py: Make sure mpz const data lives in ROM. --- ports/stm32/make-stmconst.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ports/stm32/make-stmconst.py b/ports/stm32/make-stmconst.py index 0b5666b9c..3a8e22b38 100644 --- a/ports/stm32/make-stmconst.py +++ b/ports/stm32/make-stmconst.py @@ -1,10 +1,7 @@ """ -Read in the cmsis/devinc/stm32f405xx.h header, extract relevant constants, -and create modstmconst.c. - -This is not part of the automatic build process because stm32f405xx.h isn't -expected to change. After generating the file, some manual intervention is -needed to copy the new qstr definitions to qstrdefsport.h. +This script reads in the given CMSIS device include file (eg stm32f405xx.h), +extracts relevant peripheral constants, and creates qstrs, mpz's and constants +for the stm module. """ from __future__ import print_function @@ -254,7 +251,7 @@ def main(): for mpz in sorted(needed_mpzs): assert 0 <= mpz <= 0xffffffff print('STATIC const mp_obj_int_t mpz_%08x = {{&mp_type_int}, ' - '{.neg=0, .fixed_dig=1, .alloc=2, .len=2, ' '.dig=(uint16_t[]){%#x, %#x}}};' + '{.neg=0, .fixed_dig=1, .alloc=2, .len=2, ' '.dig=(uint16_t*)(const uint16_t[]){%#x, %#x}}};' % (mpz, mpz & 0xffff, (mpz >> 16) & 0xffff), file=mpz_file) if __name__ == "__main__": From 52620c6b0e8a601e49866d0db37abbe217b16a0d Mon Sep 17 00:00:00 2001 From: ASM Date: Fri, 8 Sep 2017 15:01:14 +0300 Subject: [PATCH 032/163] py/nlrx86: Fix building for Android/x86. Tested using Clang on self-hosted Termux environment https://termux.com/. --- py/nlrx86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/nlrx86.c b/py/nlrx86.c index 8d15d7bab..a5a20f373 100644 --- a/py/nlrx86.c +++ b/py/nlrx86.c @@ -55,7 +55,7 @@ unsigned int nlr_push(nlr_buf_t *nlr) { // by default. // TODE: Better support for various x86 calling conventions // (unfortunately, __attribute__((naked)) is not supported on x86). - #ifndef __ZEPHYR__ + #if !(defined(__ZEPHYR__) || defined(__ANDROID__)) "pop %ebp \n" // undo function's prelude #endif "mov 4(%esp), %edx \n" // load nlr_buf From b02be234e1981d42fc890bac5cc7cf3b7e05dfa8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Sep 2017 16:00:21 +1000 Subject: [PATCH 033/163] extmod/machine_pinbase: Put PinBase singleton in ROM. This patch also removes the empty type "pinbase_type" (which crashes if accessed) and uses "machine_pinbase_type" instead as the type of the PinBase singleton. --- extmod/machine_pinbase.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/extmod/machine_pinbase.c b/extmod/machine_pinbase.c index 3c1f09483..070c5cde9 100644 --- a/extmod/machine_pinbase.c +++ b/extmod/machine_pinbase.c @@ -30,6 +30,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "extmod/virtpin.h" +#include "extmod/machine_pinbase.h" // PinBase class @@ -40,10 +41,8 @@ typedef struct _mp_pinbase_t { mp_obj_base_t base; } mp_pinbase_t; -STATIC const mp_obj_type_t pinbase_type; - -STATIC mp_pinbase_t pinbase_singleton = { - .base = { &pinbase_type }, +STATIC const mp_pinbase_t pinbase_singleton = { + .base = { &machine_pinbase_type }, }; STATIC mp_obj_t pinbase_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { From da8c4c2653b5121070f8c2872aead79d67d28441 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Sep 2017 16:03:52 +1000 Subject: [PATCH 034/163] py/builtinhelp: Change signature of help text var from pointer to array. As a pointer (const char *) it takes up an extra word of storage which is in RAM. --- ports/cc3200/misc/help.c | 2 +- ports/esp8266/help.c | 2 +- ports/stm32/help.c | 2 +- ports/teensy/help.c | 2 +- ports/zephyr/help.c | 2 +- py/builtin.h | 2 +- py/builtinhelp.c | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/cc3200/misc/help.c b/ports/cc3200/misc/help.c index 739303e19..ea0c9501d 100644 --- a/ports/cc3200/misc/help.c +++ b/ports/cc3200/misc/help.c @@ -27,6 +27,6 @@ #include "py/builtin.h" -const char *cc3200_help_text = "Welcome to MicroPython!\n" +const char cc3200_help_text[] = "Welcome to MicroPython!\n" "For online help please visit http://micropython.org/help/.\n" "For further help on a specific object, type help(obj)\n"; diff --git a/ports/esp8266/help.c b/ports/esp8266/help.c index 2035cdd6c..0a851f4c4 100644 --- a/ports/esp8266/help.c +++ b/ports/esp8266/help.c @@ -26,7 +26,7 @@ #include "py/builtin.h" -const char *esp_help_text = +const char esp_help_text[] = "Welcome to MicroPython!\n" "\n" "For online docs please visit http://docs.micropython.org/en/latest/esp8266/ .\n" diff --git a/ports/stm32/help.c b/ports/stm32/help.c index ea0b6921b..f9d97b70d 100644 --- a/ports/stm32/help.c +++ b/ports/stm32/help.c @@ -26,7 +26,7 @@ #include "py/builtin.h" -const char *stm32_help_text = +const char stm32_help_text[] = "Welcome to MicroPython!\n" "\n" "For online help please visit http://micropython.org/help/.\n" diff --git a/ports/teensy/help.c b/ports/teensy/help.c index ebe4bed6b..a2370c04d 100644 --- a/ports/teensy/help.c +++ b/ports/teensy/help.c @@ -26,7 +26,7 @@ #include "py/builtin.h" -const char *teensy_help_text = +const char teensy_help_text[] = "Welcome to MicroPython!\n" "\n" "For online help please visit http://micropython.org/help/.\n" diff --git a/ports/zephyr/help.c b/ports/zephyr/help.c index 0c7f27940..becc203f6 100644 --- a/ports/zephyr/help.c +++ b/ports/zephyr/help.c @@ -26,7 +26,7 @@ #include "py/builtin.h" -const char *zephyr_help_text = +const char zephyr_help_text[] = "Welcome to MicroPython!\n" "\n" "Control commands:\n" diff --git a/py/builtin.h b/py/builtin.h index a637b6e22..84b99a8a4 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -118,6 +118,6 @@ extern const mp_obj_module_t mp_module_webrepl; extern const mp_obj_module_t mp_module_framebuf; extern const mp_obj_module_t mp_module_btree; -extern const char *MICROPY_PY_BUILTINS_HELP_TEXT; +extern const char MICROPY_PY_BUILTINS_HELP_TEXT[]; #endif // MICROPY_INCLUDED_PY_BUILTIN_H diff --git a/py/builtinhelp.c b/py/builtinhelp.c index e10e48b7d..c9992906d 100644 --- a/py/builtinhelp.c +++ b/py/builtinhelp.c @@ -32,7 +32,7 @@ #if MICROPY_PY_BUILTINS_HELP -const char *mp_help_default_text = +const char mp_help_default_text[] = "Welcome to MicroPython!\n" "\n" "For online docs please visit http://docs.micropython.org/\n" From d7cd1d2027504278a58795eefab41c80ea14d5e9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Sep 2017 16:20:42 +1000 Subject: [PATCH 035/163] stm32/timer: Make pyb.Timer() instances persistent. Prior to this patch calling pyb.Timer(id) would always create a new timer instance, even if there was an existing one. This patch fixes this behaviour to match other peripherals, like UART, such that constructing a timer with just the id will retrieve any existing instances. The patch also refactors the way timers are validated on construction to simplify and reduce code size. --- ports/stm32/mpconfigport.h | 6 +- ports/stm32/timer.c | 142 ++++++++++++++++++++----------------- 2 files changed, 81 insertions(+), 67 deletions(-) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 21142c17b..abeaa56f6 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -232,9 +232,11 @@ extern const struct _mp_obj_module_t mp_module_onewire; #if defined(MCU_SERIES_F7) #define PYB_EXTI_NUM_VECTORS (24) +#define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (8) #else #define PYB_EXTI_NUM_VECTORS (23) +#define MICROPY_HW_MAX_TIMER (14) #define MICROPY_HW_MAX_UART (6) #endif @@ -254,8 +256,8 @@ extern const struct _mp_obj_module_t mp_module_onewire; \ mp_obj_t pyb_extint_callback[PYB_EXTI_NUM_VECTORS]; \ \ - /* Used to do callbacks to Python code on interrupt */ \ - struct _pyb_timer_obj_t *pyb_timer_obj_all[14]; \ + /* pointers to all Timer objects (if they have been created) */ \ + struct _pyb_timer_obj_t *pyb_timer_obj_all[MICROPY_HW_MAX_TIMER]; \ \ /* stdio is repeated on this UART object if it's not null */ \ struct _pyb_uart_obj_t *pyb_stdio_uart; \ diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 00e9c2a83..f24c2e6bf 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -629,6 +629,62 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons return mp_const_none; } +// This table encodes the timer instance and irq number. +// It assumes that timer instance pointer has the lower 8 bits cleared. +#define TIM_ENTRY(id, irq) [id - 1] = (uint32_t)TIM##id | irq +STATIC const uint32_t tim_instance_table[MICROPY_HW_MAX_TIMER] = { + #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) + TIM_ENTRY(1, TIM1_UP_TIM10_IRQn), + #elif defined(MCU_SERIES_L4) + TIM_ENTRY(1, TIM1_UP_TIM16_IRQn), + #endif + TIM_ENTRY(2, TIM2_IRQn), + TIM_ENTRY(3, TIM3_IRQn), + TIM_ENTRY(4, TIM4_IRQn), + TIM_ENTRY(5, TIM5_IRQn), + #if defined(TIM6) + TIM_ENTRY(6, TIM6_DAC_IRQn), + #endif + #if defined(TIM7) + TIM_ENTRY(7, TIM7_IRQn), + #endif + #if defined(TIM8) + #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) + TIM_ENTRY(8, TIM8_UP_TIM13_IRQn), + #elif defined(MCU_SERIES_L4) + TIM_ENTRY(8, TIM8_UP_IRQn), + #endif + #endif + #if defined(TIM9) + TIM_ENTRY(9, TIM1_BRK_TIM9_IRQn), + #endif + #if defined(TIM10) + TIM_ENTRY(10, TIM1_UP_TIM10_IRQn), + #endif + #if defined(TIM11) + TIM_ENTRY(11, TIM1_TRG_COM_TIM11_IRQn), + #endif + #if defined(TIM12) + TIM_ENTRY(12, TIM8_BRK_TIM12_IRQn), + #endif + #if defined(TIM13) + TIM_ENTRY(13, TIM8_UP_TIM13_IRQn), + #endif + #if defined(TIM14) + TIM_ENTRY(14, TIM8_TRG_COM_TIM14_IRQn), + #endif + #if defined(TIM15) + TIM_ENTRY(15, TIM1_BRK_TIM15_IRQn), + #endif + #if defined(TIM16) + TIM_ENTRY(16, TIM1_UP_TIM16_IRQn), + #endif + #if defined(TIM17) + TIM_ENTRY(17, TIM1_TRG_COM_TIM17_IRQn), + #endif +}; +#undef TIM_ENTRY + /// \classmethod \constructor(id, ...) /// Construct a new timer object of the given id. If additional /// arguments are given, then the timer is initialised by `init(...)`. @@ -637,74 +693,30 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, size_t n_args, siz // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - // create new Timer object - pyb_timer_obj_t *tim = m_new_obj(pyb_timer_obj_t); - memset(tim, 0, sizeof(*tim)); - - tim->base.type = &pyb_timer_type; - tim->callback = mp_const_none; - tim->channel = NULL; - - // get TIM number - tim->tim_id = mp_obj_get_int(args[0]); - tim->is_32bit = false; + // get the timer id + mp_int_t tim_id = mp_obj_get_int(args[0]); - switch (tim->tim_id) { - #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) - case 1: tim->tim.Instance = TIM1; tim->irqn = TIM1_UP_TIM10_IRQn; break; - #elif defined(MCU_SERIES_L4) - case 1: tim->tim.Instance = TIM1; tim->irqn = TIM1_UP_TIM16_IRQn; break; - #endif - case 2: tim->tim.Instance = TIM2; tim->irqn = TIM2_IRQn; tim->is_32bit = true; break; - case 3: tim->tim.Instance = TIM3; tim->irqn = TIM3_IRQn; break; - case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break; - case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break; - #if defined(TIM6) - case 6: tim->tim.Instance = TIM6; tim->irqn = TIM6_DAC_IRQn; break; - #endif - #if defined(TIM7) - case 7: tim->tim.Instance = TIM7; tim->irqn = TIM7_IRQn; break; - #endif - #if defined(TIM8) - #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) - case 8: tim->tim.Instance = TIM8; tim->irqn = TIM8_UP_TIM13_IRQn; break; - #elif defined(MCU_SERIES_L4) - case 8: tim->tim.Instance = TIM8; tim->irqn = TIM8_UP_IRQn; break; - #endif - #endif - #if defined(TIM9) - case 9: tim->tim.Instance = TIM9; tim->irqn = TIM1_BRK_TIM9_IRQn; break; - #endif - #if defined(TIM10) - case 10: tim->tim.Instance = TIM10; tim->irqn = TIM1_UP_TIM10_IRQn; break; - #endif - #if defined(TIM11) - case 11: tim->tim.Instance = TIM11; tim->irqn = TIM1_TRG_COM_TIM11_IRQn; break; - #endif - #if defined(TIM12) - case 12: tim->tim.Instance = TIM12; tim->irqn = TIM8_BRK_TIM12_IRQn; break; - #endif - #if defined(TIM13) - case 13: tim->tim.Instance = TIM13; tim->irqn = TIM8_UP_TIM13_IRQn; break; - #endif - #if defined(TIM14) - case 14: tim->tim.Instance = TIM14; tim->irqn = TIM8_TRG_COM_TIM14_IRQn; break; - #endif - #if defined(TIM15) - case 15: tim->tim.Instance = TIM15; tim->irqn = TIM1_BRK_TIM15_IRQn; break; - #endif - #if defined(TIM16) - case 16: tim->tim.Instance = TIM16; tim->irqn = TIM1_UP_TIM16_IRQn; break; - #endif - #if defined(TIM17) - case 17: tim->tim.Instance = TIM17; tim->irqn = TIM1_TRG_COM_TIM17_IRQn; break; - #endif - default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%d) doesn't exist", tim->tim_id)); + // check if the timer exists + if (tim_id <= 0 || tim_id > MICROPY_HW_MAX_TIMER || tim_instance_table[tim_id - 1] == 0) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%d) doesn't exist", tim_id)); } - // set the global variable for interrupt callbacks - if (tim->tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) { - MP_STATE_PORT(pyb_timer_obj_all)[tim->tim_id - 1] = tim; + pyb_timer_obj_t *tim; + if (MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] == NULL) { + // create new Timer object + tim = m_new_obj(pyb_timer_obj_t); + memset(tim, 0, sizeof(*tim)); + tim->base.type = &pyb_timer_type; + tim->tim_id = tim_id; + tim->is_32bit = tim_id == 2 || tim_id == 5; + tim->callback = mp_const_none; + uint32_t ti = tim_instance_table[tim_id - 1]; + tim->tim.Instance = (TIM_TypeDef*)(ti & 0xffffff00); + tim->irqn = ti & 0xff; + MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] = tim; + } else { + // reference existing Timer object + tim = MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1]; } if (n_args > 1 || n_kw > 0) { From d42b89bc3a9b85488a272235b1cc5fb342ab2536 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Tue, 12 Sep 2017 16:48:39 +0100 Subject: [PATCH 036/163] docs/library/framebuf.rst: Generalise constructor to all colour formats. --- docs/library/framebuf.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/library/framebuf.rst b/docs/library/framebuf.rst index b92bd08ef..74c9f8564 100644 --- a/docs/library/framebuf.rst +++ b/docs/library/framebuf.rst @@ -38,9 +38,9 @@ Constructors - *width* is the width of the FrameBuffer in pixels - *height* is the height of the FrameBuffer in pixels - *format* specifies the type of pixel used in the FrameBuffer; - valid values are ``framebuf.MVLSB``, ``framebuf.RGB565`` - and ``framebuf.GS4_HMSB``. MVLSB is monochrome 1-bit color, - RGB565 is RGB 16-bit color, and GS4_HMSB is grayscale 4-bit color. + permissible values are listed under Constants below. These set the + number of bits used to encode a color value and the layout of these + bits in *buffer*. Where a color value c is passed to a method, c is a small integer with an encoding that is dependent on the format of the FrameBuffer. - *stride* is the number of pixels between each horizontal line @@ -110,8 +110,9 @@ Other methods corresponding color will be considered transparent: all pixels with that color value will not be drawn. - This method works between FrameBuffer's utilising different formats, but the - resulting colors may be unexpected due to the mismatch in color formats. + This method works between FrameBuffer instances utilising different formats, + but the resulting colors may be unexpected due to the mismatch in color + formats. Constants --------- From aca498c2b03abf88da81a3037b3dab9a4f53e800 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Sep 2017 17:03:57 +1000 Subject: [PATCH 037/163] stm32/mpconfigport.h: Add configuration for max periphs on L4 series. --- ports/stm32/mpconfigport.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index abeaa56f6..7888b5355 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -234,6 +234,10 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define PYB_EXTI_NUM_VECTORS (24) #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (8) +#elif defined(MCU_SERIES_L4) +#define PYB_EXTI_NUM_VECTORS (23) +#define MICROPY_HW_MAX_TIMER (17) +#define MICROPY_HW_MAX_UART (6) #else #define PYB_EXTI_NUM_VECTORS (23) #define MICROPY_HW_MAX_TIMER (14) From 89f657f073d212161a04082419faeca30fc3a410 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Sep 2017 20:33:55 +1000 Subject: [PATCH 038/163] py/runtime.h: Change empty mp_warning macro so var-args are non empty. Variable arguments in a macro should take at least 1 argument. --- py/runtime.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/runtime.h b/py/runtime.h index 4abdea584..bbdd7647e 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -175,7 +175,7 @@ void mp_native_raise(mp_obj_t o); #if MICROPY_WARNINGS void mp_warning(const char *msg, ...); #else -#define mp_warning(msg, ...) +#define mp_warning(...) #endif #endif // MICROPY_INCLUDED_PY_RUNTIME_H From 280fb4d92830bedb1f4ef90554300e444661ef8c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Sep 2017 20:36:06 +1000 Subject: [PATCH 039/163] py/emitbc: Remove stray semicolon in outer scope. --- py/emitbc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/emitbc.c b/py/emitbc.c index 677020925..3f4dfc178 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -550,7 +550,7 @@ void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) { void mp_emit_bc_load_null(emit_t *emit) { emit_bc_pre(emit, 1); emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL); -}; +} void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) { (void)qst; From 75163325ae3fc33ebb9919231289fadec620d124 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 16 Sep 2017 13:05:15 +0300 Subject: [PATCH 040/163] tests/cpydiff: Add cases for locals() discrepancies. MicroPython doesn't maintain local symbolic environment, so any feature depending on it won't work as expected. --- tests/cpydiff/core_locals.py | 11 +++++++++++ tests/cpydiff/core_locals_eval.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/cpydiff/core_locals.py create mode 100644 tests/cpydiff/core_locals_eval.py diff --git a/tests/cpydiff/core_locals.py b/tests/cpydiff/core_locals.py new file mode 100644 index 000000000..0240e5a1a --- /dev/null +++ b/tests/cpydiff/core_locals.py @@ -0,0 +1,11 @@ +""" +categories: Core,Runtime +description: Local variables aren't included in locals() result +cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name. +workaround: Unknown +""" +def test(): + val = 2 + print(locals()) + +test() diff --git a/tests/cpydiff/core_locals_eval.py b/tests/cpydiff/core_locals_eval.py new file mode 100644 index 000000000..8416e3b06 --- /dev/null +++ b/tests/cpydiff/core_locals_eval.py @@ -0,0 +1,14 @@ +""" +categories: Core,Runtime +description: Code running in eval() function doesn't have access to local variables +cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name. Effectively, ``eval(expr)`` in MicroPython is equivalent to ``eval(expr, globals(), globals())``. +workaround: Unknown +""" +val = 1 + +def test(): + val = 2 + print(val) + eval("print(val)") + +test() From 72491b3e40db75e7edf5831e8914a1ca5c8e9937 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 17 Sep 2017 21:34:34 +0300 Subject: [PATCH 041/163] docs/btree: Describe page caching policy of the underlying implementation. --- docs/library/btree.rst | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/library/btree.rst b/docs/library/btree.rst index 9322d32e6..8fac67e8d 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -76,20 +76,24 @@ Example:: Functions --------- -.. function:: open(stream, \*, flags=0, cachesize=0, pagesize=0, minkeypage=0) +.. function:: open(stream, \*, flags=0, pagesize=0, cachesize=0, minkeypage=0) Open a database from a random-access `stream` (like an open file). All other parameters are optional and keyword-only, and allow to tweak advanced parameters of the database operation (most users will not need them): * *flags* - Currently unused. - * *cachesize* - Suggested maximum memory cache size in bytes. For a - board with enough memory using larger values may improve performance. - The value is only a recommendation, the module may use more memory if - values set too low. * *pagesize* - Page size used for the nodes in BTree. Acceptable range - is 512-65536. If 0, underlying I/O block size will be used (the best - compromise between memory usage and performance). + is 512-65536. If 0, a port-specific default will be used, optimized for + port's memory usage and/or performance. + * *cachesize* - Suggested memory cache size in bytes. For a + board with enough memory using larger values may improve performance. + Cache policy is as follows: entire cache is not allocated at once; + instead, accessing a new page in database will allocate a memory buffer + for it, until value specified by *cachesize* is reached. Then, these + buffers will be managed using LRU (least recently used) policy. More + buffers may still be allocated if needed (e.g., if a database contains + big keys and/or values). Allocated cache buffers aren't reclaimed. * *minkeypage* - Minimum number of keys to store per page. Default value of 0 equivalent to 2. From 9dce823cfd0a9991350184f08a1373f3887134f4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 18 Sep 2017 00:06:43 +0300 Subject: [PATCH 042/163] py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS. This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative. --- py/modbuiltins.c | 21 +------------------- py/objcomplex.c | 5 +++++ py/objfloat.c | 9 +++++++++ py/objint.c | 10 ---------- py/objint.h | 1 - py/objint_longlong.c | 34 ++++++++++---------------------- py/objint_mpz.c | 30 +++++++++------------------- py/runtime.c | 9 +++++++++ py/runtime0.h | 1 + tests/unix/extra_coverage.py.exp | 2 +- 10 files changed, 45 insertions(+), 77 deletions(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 1c76b8073..0486251b6 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -91,26 +91,7 @@ STATIC mp_obj_t mp_builtin___build_class__(size_t n_args, const mp_obj_t *args) MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) { - #if MICROPY_PY_BUILTINS_FLOAT - if (mp_obj_is_float(o_in)) { - mp_float_t value = mp_obj_float_get(o_in); - // TODO check for NaN etc - if (value < 0) { - return mp_obj_new_float(-value); - } else { - return o_in; - } - #if MICROPY_PY_BUILTINS_COMPLEX - } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) { - mp_float_t real, imag; - mp_obj_complex_get(o_in, &real, &imag); - return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); - #endif - } - #endif - - // this will raise a TypeError if the argument is not integral - return mp_obj_int_abs(o_in); + return mp_unary_op(MP_UNARY_OP_ABS, o_in); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); diff --git a/py/objcomplex.c b/py/objcomplex.c index 07b9d5d41..e7a3c244a 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -124,6 +124,11 @@ STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); + case MP_UNARY_OP_ABS: { + mp_float_t real, imag; + mp_obj_complex_get(o_in, &real, &imag); + return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objfloat.c b/py/objfloat.c index fadbbcb79..fefd78314 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -162,6 +162,15 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(val)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val); + case MP_UNARY_OP_ABS: { + mp_float_t value = mp_obj_float_get(o_in); + // TODO check for NaN etc + if (value < 0) { + return mp_obj_new_float(-value); + } else { + return o_in; + } + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objint.c b/py/objint.c index 6a73b4382..000a0404c 100644 --- a/py/objint.c +++ b/py/objint.c @@ -314,16 +314,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - mp_int_t val = mp_obj_get_int(self_in); - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); -} - // This is called for operations on SMALL_INT that are not handled by mp_unary_op mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { return MP_OBJ_NULL; // op not supported diff --git a/py/objint.h b/py/objint.h index a4d4ff32d..4b95acde9 100644 --- a/py/objint.h +++ b/py/objint.h @@ -57,7 +57,6 @@ mp_int_t mp_obj_int_hash(mp_obj_t self_in); mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf); void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf); int mp_obj_int_sign(mp_obj_t self_in); -mp_obj_t mp_obj_int_abs(mp_obj_t self_in); mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in); mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); diff --git a/py/objint_longlong.c b/py/objint_longlong.c index ddfdae744..8d8ca1b2c 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -94,30 +94,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) { - mp_obj_int_t *self = self_in; - self = mp_obj_new_int_from_ll(self->val); - if (self->val < 0) { - // TODO could overflow long long - self->val = -self->val; - } - return self; - } else { - mp_int_t val = mp_obj_get_int(self_in); - if (val == MP_SMALL_INT_MIN) { - return mp_obj_new_int_from_ll(-val); - } else { - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); - } - } -} - mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { mp_obj_int_t *o = o_in; switch (op) { @@ -130,6 +106,16 @@ mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val); case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val); + case MP_UNARY_OP_ABS: { + mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in); + if (self->val >= 0) { + return o_in; + } + self = mp_obj_new_int_from_ll(self->val); + // TODO could overflow long long + self->val = -self->val; + return MP_OBJ_FROM_PTR(self); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 0e318b492..15aad1d4d 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -141,27 +141,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) { - mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_int_t *self2 = mp_obj_int_new_mpz(); - mpz_abs_inpl(&self2->mpz, &self->mpz); - return MP_OBJ_FROM_PTR(self2); - } else { - mp_int_t val = mp_obj_get_int(self_in); - if (val == MP_SMALL_INT_MIN) { - return mp_obj_new_int_from_ll(-val); - } else { - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); - } - } -} - mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { mp_obj_int_t *o = MP_OBJ_TO_PTR(o_in); switch (op) { @@ -170,6 +149,15 @@ mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } + case MP_UNARY_OP_ABS: { + mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in); + if (self->mpz.neg == 0) { + return o_in; + } + mp_obj_int_t *self2 = mp_obj_int_new_mpz(); + mpz_abs_inpl(&self2->mpz, &self->mpz); + return MP_OBJ_FROM_PTR(self2); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/runtime.c b/py/runtime.c index c533558da..f21eed204 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -231,6 +231,15 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { } else { return MP_OBJ_NEW_SMALL_INT(-val); } + case MP_UNARY_OP_ABS: + if (val >= 0) { + return arg; + } else if (val == MP_SMALL_INT_MIN) { + // check for overflow + return mp_obj_new_int(-val); + } else { + return MP_OBJ_NEW_SMALL_INT(-val); + } default: assert(op == MP_UNARY_OP_INVERT); return MP_OBJ_NEW_SMALL_INT(~val); diff --git a/py/runtime0.h b/py/runtime0.h index a3e9d46b9..54bf192d3 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -50,6 +50,7 @@ typedef enum { MP_UNARY_OP_NEGATIVE, MP_UNARY_OP_INVERT, MP_UNARY_OP_NOT, + MP_UNARY_OP_ABS, // __abs__ MP_UNARY_OP_SIZEOF, // for sys.getsizeof() } mp_unary_op_t; diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index ab638a632..4dc421dab 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -38,7 +38,7 @@ ementation 0 0 # runtime utils -TypeError: can't convert str to int +TypeError: unsupported type for : 'str' TypeError: unsupported types for : 'str', 'str' Warning: test # format float From fdb2aa81b7c7588d94ec59932781e14759045df0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Sep 2017 14:31:03 +1000 Subject: [PATCH 043/163] py/{objfloat,objcomplex}: Optimise MP_UNARY_OP_ABS by reusing variables. --- py/objcomplex.c | 7 ++----- py/objfloat.c | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/py/objcomplex.c b/py/objcomplex.c index e7a3c244a..088ad5211 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -124,11 +124,8 @@ STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); - case MP_UNARY_OP_ABS: { - mp_float_t real, imag; - mp_obj_complex_get(o_in, &real, &imag); - return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); - } + case MP_UNARY_OP_ABS: + return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real*o->real + o->imag*o->imag)); default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objfloat.c b/py/objfloat.c index fefd78314..55a9379ff 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -163,10 +163,9 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val); case MP_UNARY_OP_ABS: { - mp_float_t value = mp_obj_float_get(o_in); // TODO check for NaN etc - if (value < 0) { - return mp_obj_new_float(-value); + if (val < 0) { + return mp_obj_new_float(-val); } else { return o_in; } From 44f0a4d1e7a315c8d2b21fc4b0471d88e382a91e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Sep 2017 23:53:33 +1000 Subject: [PATCH 044/163] py/mpconfig.h: Add note that using computed gotos in VM is not C99. --- py/mpconfig.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/py/mpconfig.h b/py/mpconfig.h index 63438a846..b93f851d6 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -383,6 +383,8 @@ // Whether to use computed gotos in the VM, or a switch // Computed gotos are roughly 10% faster, and increase VM code size by a little +// Note: enabling this will use the gcc-specific extensions of ranged designated +// initialisers and addresses of labels, which are not part of the C99 standard. #ifndef MICROPY_OPT_COMPUTED_GOTO #define MICROPY_OPT_COMPUTED_GOTO (0) #endif From fc9a6dd09e77c34ef04bab57e73bfcb08bc78bb4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 19 Sep 2017 21:19:23 +0300 Subject: [PATCH 045/163] py/objstr: strip: Don't strip "\0" by default. An issue was due to incorrectly taking size of default strip characters set. --- py/objstr.c | 2 +- tests/basics/string_strip.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/py/objstr.c b/py/objstr.c index f6214f80c..11bfb41fc 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -777,7 +777,7 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) { if (n_args == 1) { chars_to_del = whitespace; - chars_to_del_len = sizeof(whitespace); + chars_to_del_len = sizeof(whitespace) - 1; } else { if (mp_obj_get_type(args[1]) != self_type) { bad_implicit_conversion(args[1]); diff --git a/tests/basics/string_strip.py b/tests/basics/string_strip.py index 5d99a78e5..971a4aae5 100644 --- a/tests/basics/string_strip.py +++ b/tests/basics/string_strip.py @@ -32,6 +32,13 @@ print("a ".lstrip()) print("a ".rstrip()) +# \0 used to give a problem + +print("\0abc\0".strip()) +print("\0abc\0".lstrip()) +print("\0abc\0".rstrip()) +print("\0abc\0".strip("\0")) + # Test that stripping unstrippable string returns original object s = "abc" print(id(s.strip()) == id(s)) From 78602a217f921f5f9737a5fb7974d4b61d313742 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Sep 2017 17:44:16 +1000 Subject: [PATCH 046/163] stm32/usbdev: Make the USBD callback struct const so it can go in ROM. --- ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 2 +- ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 2 +- ports/stm32/usbdev/core/inc/usbd_core.h | 2 +- ports/stm32/usbdev/core/inc/usbd_def.h | 2 +- ports/stm32/usbdev/core/src/usbd_core.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index 96617b107..a9fffe958 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -96,7 +96,7 @@ extern const uint8_t USBD_HID_MOUSE_ReportDesc[USBD_HID_MOUSE_REPORT_DESC_SIZE]; extern const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPORT_DESC_SIZE]; -extern USBD_ClassTypeDef USBD_CDC_MSC_HID; +extern const USBD_ClassTypeDef USBD_CDC_MSC_HID; // returns 0 on success, -1 on failure int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info); diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index d61073f4d..0997bb50e 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -1112,7 +1112,7 @@ uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev) { } // CDC/MSC/HID interface class callback structure -USBD_ClassTypeDef USBD_CDC_MSC_HID = { +const USBD_ClassTypeDef USBD_CDC_MSC_HID = { USBD_CDC_MSC_HID_Init, USBD_CDC_MSC_HID_DeInit, USBD_CDC_MSC_HID_Setup, diff --git a/ports/stm32/usbdev/core/inc/usbd_core.h b/ports/stm32/usbdev/core/inc/usbd_core.h index 5360680b9..3178d4a4b 100644 --- a/ports/stm32/usbdev/core/inc/usbd_core.h +++ b/ports/stm32/usbdev/core/inc/usbd_core.h @@ -88,7 +88,7 @@ USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef * USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev); USBD_StatusTypeDef USBD_Start (USBD_HandleTypeDef *pdev); USBD_StatusTypeDef USBD_Stop (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass); +USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, const USBD_ClassTypeDef *pclass); USBD_StatusTypeDef USBD_RunTestMode (USBD_HandleTypeDef *pdev); USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); diff --git a/ports/stm32/usbdev/core/inc/usbd_def.h b/ports/stm32/usbdev/core/inc/usbd_def.h index 5c0506a14..06b57dfd4 100644 --- a/ports/stm32/usbdev/core/inc/usbd_def.h +++ b/ports/stm32/usbdev/core/inc/usbd_def.h @@ -235,7 +235,7 @@ typedef struct _USBD_HandleTypeDef USBD_SetupReqTypedef request; USBD_DescriptorsTypeDef *pDesc; - USBD_ClassTypeDef *pClass; + const USBD_ClassTypeDef *pClass; void *pClassData; void *pUserData; void *pData; diff --git a/ports/stm32/usbdev/core/src/usbd_core.c b/ports/stm32/usbdev/core/src/usbd_core.c index bb44513d7..ae5b99626 100644 --- a/ports/stm32/usbdev/core/src/usbd_core.c +++ b/ports/stm32/usbdev/core/src/usbd_core.c @@ -155,7 +155,7 @@ USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev) * @param pclass: Class handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass) +USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, const USBD_ClassTypeDef *pclass) { USBD_StatusTypeDef status = USBD_OK; if(pclass != 0) From 347de3e218fe02bba6f7a4bccd1b90f8326c1efe Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Sep 2017 17:45:21 +1000 Subject: [PATCH 047/163] stm32/usbdev: Change static function variable to non-static. It's written straight away in the function on every call so it doesn't need to be static. --- ports/stm32/usbdev/core/src/usbd_ctlreq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/usbdev/core/src/usbd_ctlreq.c b/ports/stm32/usbdev/core/src/usbd_ctlreq.c index 80b1da8ea..d744725b2 100644 --- a/ports/stm32/usbdev/core/src/usbd_ctlreq.c +++ b/ports/stm32/usbdev/core/src/usbd_ctlreq.c @@ -479,7 +479,7 @@ static void USBD_SetConfig(USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req) { - static uint8_t cfgidx; + uint8_t cfgidx; cfgidx = (uint8_t)(req->wValue); From 96fd80db1306ed67ee967b888c1c03c136bff4fb Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Sep 2017 15:24:57 +1000 Subject: [PATCH 048/163] py/objexcept: Prevent infinite recursion when allocating exceptions. The aim of this patch is to rewrite the functions that create exception instances (mp_obj_exception_make_new and mp_obj_new_exception_msg_varg) so that they do not call any functions that may raise an exception. Otherwise it's possible to create infinite recursion with an exception being raised while trying to create an exception object. The two main things that are done to accomplish this are: 1. Change mp_obj_new_exception_msg_varg to just format the string, then call mp_obj_exception_make_new to actually create the exception object. 2. In mp_obj_exception_make_new and mp_obj_new_exception_msg_varg try to allocate all memory first using functions that don't raise exceptions If any of the memory allocations fail (return NULL) then degrade gracefully by trying other options for memory allocation, eg using the emergency exception buffer. 3. Use a custom printer backend to conservatively format strings: if it can't allocate memory then it just truncates the string. As part of this rewrite, raising an exception without a message, like KeyError(123), will now use the emergency buffer to store the arg and traceback data if there is no heap memory available. Memory use with this patch is unchanged. Code size is increased by: bare-arm: +136 minimal x86: +124 unix x64: +72 unix nanbox: +96 stm32: +88 esp8266: +92 cc3200: +80 --- py/objexcept.c | 243 ++++++++++++++++++++----------- tests/micropython/emg_exc.py | 16 +- tests/micropython/emg_exc.py.exp | 5 +- tests/run-tests | 1 + 4 files changed, 176 insertions(+), 89 deletions(-) diff --git a/py/objexcept.c b/py/objexcept.c index a9fe04094..0a03df9d0 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -38,6 +38,12 @@ #include "py/gc.h" #include "py/mperrno.h" +// Number of items per traceback entry (file, line, block) +#define TRACEBACK_ENTRY_LEN (3) + +// Number of traceback entries to reserve in the emergency exception buffer +#define EMG_TRACEBACK_ALLOC (2 * TRACEBACK_ENTRY_LEN) + // Instance of MemoryError exception - needed by mp_malloc_fail const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj}; @@ -127,18 +133,51 @@ STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_pr mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false); - mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0); - if (o == NULL) { - // Couldn't allocate heap memory; use local data instead. - o = &MP_STATE_VM(mp_emergency_exception_obj); - // We can't store any args. - o->args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; + + // Try to allocate memory for the exception, with fallback to emergency exception object + mp_obj_exception_t *o_exc = m_new_obj_maybe(mp_obj_exception_t); + if (o_exc == NULL) { + o_exc = &MP_STATE_VM(mp_emergency_exception_obj); + } + + // Populate the exception object + o_exc->base.type = type; + o_exc->traceback_data = NULL; + + mp_obj_tuple_t *o_tuple; + if (n_args == 0) { + // No args, can use the empty tuple straightaway + o_tuple = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; } else { - o->args = MP_OBJ_TO_PTR(mp_obj_new_tuple(n_args, args)); + // Try to allocate memory for the tuple containing the args + o_tuple = m_new_obj_var_maybe(mp_obj_tuple_t, mp_obj_t, n_args); + + #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF + // If we are called by mp_obj_new_exception_msg_varg then it will have + // reserved room (after the traceback data) for a tuple with 1 element. + // Otherwise we are free to use the whole buffer after the traceback data. + if (o_tuple == NULL && mp_emergency_exception_buf_size >= + EMG_TRACEBACK_ALLOC * sizeof(size_t) + sizeof(mp_obj_tuple_t) + n_args * sizeof(mp_obj_t)) { + o_tuple = (mp_obj_tuple_t*) + ((uint8_t*)MP_STATE_VM(mp_emergency_exception_buf) + EMG_TRACEBACK_ALLOC * sizeof(size_t)); + } + #endif + + if (o_tuple == NULL) { + // No memory for a tuple, fallback to an empty tuple + o_tuple = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; + } else { + // Have memory for a tuple so populate it + o_tuple->base.type = &mp_type_tuple; + o_tuple->len = n_args; + memcpy(o_tuple->items, args, n_args * sizeof(mp_obj_t)); + } } - o->base.type = type; - o->traceback_data = NULL; - return MP_OBJ_FROM_PTR(o); + + // Store the tuple of args in the exception object + o_exc->args = o_tuple; + + return MP_OBJ_FROM_PTR(o_exc); } // Get exception "value" - that is, first argument, or None @@ -306,87 +345,95 @@ mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg return mp_obj_new_exception_msg_varg(exc_type, msg); } -mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) { - // check that the given type is an exception type - assert(exc_type->make_new == mp_obj_exception_make_new); - - // make exception object - mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0); - if (o == NULL) { - // Couldn't allocate heap memory; use local data instead. - // Unfortunately, we won't be able to format the string... - o = &MP_STATE_VM(mp_emergency_exception_obj); - o->base.type = exc_type; - o->traceback_data = NULL; - o->args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; - -#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF - // If the user has provided a buffer, then we try to create a tuple - // of length 1, which has a string object and the string data. +// The following struct and function implement a simple printer that conservatively +// allocates memory and truncates the output data if no more memory can be obtained. +// It leaves room for a null byte at the end of the buffer. - if (mp_emergency_exception_buf_size > (sizeof(mp_obj_tuple_t) + sizeof(mp_obj_str_t) + sizeof(mp_obj_t))) { - mp_obj_tuple_t *tuple = (mp_obj_tuple_t *)MP_STATE_VM(mp_emergency_exception_buf); - mp_obj_str_t *str = (mp_obj_str_t *)&tuple->items[1]; - - tuple->base.type = &mp_type_tuple; - tuple->len = 1; - tuple->items[0] = MP_OBJ_FROM_PTR(str); - - byte *str_data = (byte *)&str[1]; - size_t max_len = (byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - - str_data; +struct _exc_printer_t { + bool allow_realloc; + size_t alloc; + size_t len; + byte *buf; +}; - vstr_t vstr; - vstr_init_fixed_buf(&vstr, max_len, (char *)str_data); +STATIC void exc_add_strn(void *data, const char *str, size_t len) { + struct _exc_printer_t *pr = data; + if (pr->len + len >= pr->alloc) { + // Not enough room for data plus a null byte so try to grow the buffer + if (pr->allow_realloc) { + size_t new_alloc = pr->alloc + len + 16; + byte *new_buf = m_renew_maybe(byte, pr->buf, pr->alloc, new_alloc, true); + if (new_buf == NULL) { + pr->allow_realloc = false; + len = pr->alloc - pr->len - 1; + } else { + pr->alloc = new_alloc; + pr->buf = new_buf; + } + } else { + len = pr->alloc - pr->len - 1; + } + } + memcpy(pr->buf + pr->len, str, len); + pr->len += len; +} - va_list ap; - va_start(ap, fmt); - vstr_vprintf(&vstr, fmt, ap); - va_end(ap); +mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) { + assert(fmt != NULL); - str->base.type = &mp_type_str; - str->hash = qstr_compute_hash(str_data, str->len); - str->len = vstr.len; - str->data = str_data; + // Check that the given type is an exception type + assert(exc_type->make_new == mp_obj_exception_make_new); - o->args = tuple; + // Try to allocate memory for the message + mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); + size_t o_str_alloc = strlen(fmt) + 1; + byte *o_str_buf = m_new_maybe(byte, o_str_alloc); + + bool used_emg_buf = false; + #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF + // If memory allocation failed and there is an emergency buffer then try to use + // that buffer to store the string object and its data (at least 16 bytes for + // the string data), reserving room at the start for the traceback and 1-tuple. + if ((o_str == NULL || o_str_buf == NULL) + && mp_emergency_exception_buf_size >= EMG_TRACEBACK_ALLOC * sizeof(size_t) + + sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t) + sizeof(mp_obj_str_t) + 16) { + used_emg_buf = true; + o_str = (mp_obj_str_t*)((uint8_t*)MP_STATE_VM(mp_emergency_exception_buf) + + EMG_TRACEBACK_ALLOC * sizeof(size_t) + sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t)); + o_str_buf = (byte*)&o_str[1]; + o_str_alloc = (uint8_t*)MP_STATE_VM(mp_emergency_exception_buf) + + mp_emergency_exception_buf_size - o_str_buf; + } + #endif - size_t offset = &str_data[str->len] - (byte*)MP_STATE_VM(mp_emergency_exception_buf); - offset += sizeof(void *) - 1; - offset &= ~(sizeof(void *) - 1); + if (o_str == NULL) { + // No memory for the string object so create the exception with no args + return mp_obj_exception_make_new(exc_type, 0, 0, NULL); + } - if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) { - // We have room to store some traceback. - o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset); - o->traceback_alloc = ((byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]); - o->traceback_len = 0; - } - } -#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF + if (o_str_buf == NULL) { + // No memory for the string buffer: assume that the fmt string is in ROM + // and use that data as the data of the string + o_str->len = o_str_alloc - 1; // will be equal to strlen(fmt) + o_str->data = (const byte*)fmt; } else { - o->base.type = exc_type; - o->traceback_data = NULL; - o->args = MP_OBJ_TO_PTR(mp_obj_new_tuple(1, NULL)); - - assert(fmt != NULL); - { - if (strchr(fmt, '%') == NULL) { - // no formatting substitutions, avoid allocating vstr. - o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false); - } else { - // render exception message and store as .args[0] - va_list ap; - vstr_t vstr; - vstr_init(&vstr, 16); - va_start(ap, fmt); - vstr_vprintf(&vstr, fmt, ap); - va_end(ap); - o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); - } - } + // We have some memory to format the string + struct _exc_printer_t exc_pr = {!used_emg_buf, o_str_alloc, 0, o_str_buf}; + mp_print_t print = {&exc_pr, exc_add_strn}; + va_list ap; + va_start(ap, fmt); + mp_vprintf(&print, fmt, ap); + va_end(ap); + exc_pr.buf[exc_pr.len] = '\0'; + o_str->len = exc_pr.len; + o_str->data = exc_pr.buf; } - return MP_OBJ_FROM_PTR(o); + // Create the string object and call mp_obj_exception_make_new to create the exception + o_str->base.type = &mp_type_str; + o_str->hash = qstr_compute_hash(o_str->data, o_str->len); + mp_obj_t arg = MP_OBJ_FROM_PTR(o_str); + return mp_obj_exception_make_new(exc_type, 1, 0, &arg); } // return true if the given object is an exception type @@ -443,24 +490,46 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qs // if memory allocation fails (eg because gc is locked), just return if (self->traceback_data == NULL) { - self->traceback_data = m_new_maybe(size_t, 3); + self->traceback_data = m_new_maybe(size_t, TRACEBACK_ENTRY_LEN); if (self->traceback_data == NULL) { + #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF + if (mp_emergency_exception_buf_size >= EMG_TRACEBACK_ALLOC * sizeof(size_t)) { + // There is room in the emergency buffer for traceback data + size_t *tb = (size_t*)MP_STATE_VM(mp_emergency_exception_buf); + self->traceback_data = tb; + self->traceback_alloc = EMG_TRACEBACK_ALLOC; + } else { + // Can't allocate and no room in emergency buffer + return; + } + #else + // Can't allocate return; + #endif + } else { + // Allocated the traceback data on the heap + self->traceback_alloc = TRACEBACK_ENTRY_LEN; } - self->traceback_alloc = 3; self->traceback_len = 0; - } else if (self->traceback_len + 3 > self->traceback_alloc) { + } else if (self->traceback_len + TRACEBACK_ENTRY_LEN > self->traceback_alloc) { + #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF + if (self->traceback_data == (size_t*)MP_STATE_VM(mp_emergency_exception_buf)) { + // Can't resize the emergency buffer + return; + } + #endif // be conservative with growing traceback data - size_t *tb_data = m_renew_maybe(size_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true); + size_t *tb_data = m_renew_maybe(size_t, self->traceback_data, self->traceback_alloc, + self->traceback_alloc + TRACEBACK_ENTRY_LEN, true); if (tb_data == NULL) { return; } self->traceback_data = tb_data; - self->traceback_alloc += 3; + self->traceback_alloc += TRACEBACK_ENTRY_LEN; } size_t *tb_data = &self->traceback_data[self->traceback_len]; - self->traceback_len += 3; + self->traceback_len += TRACEBACK_ENTRY_LEN; tb_data[0] = file; tb_data[1] = line; tb_data[2] = block; diff --git a/tests/micropython/emg_exc.py b/tests/micropython/emg_exc.py index d228e6faa..4a9fa18bc 100644 --- a/tests/micropython/emg_exc.py +++ b/tests/micropython/emg_exc.py @@ -2,6 +2,11 @@ import micropython import sys +try: + import uio +except ImportError: + print("SKIP") + raise SystemExit # some ports need to allocate heap for the emg exc try: @@ -14,7 +19,16 @@ def f(): try: raise ValueError(1) except ValueError as er: - sys.print_exception(er) + exc = er micropython.heap_unlock() + # print the exception + buf = uio.StringIO() + sys.print_exception(exc, buf) + for l in buf.getvalue().split("\n"): + if l.startswith(" File "): + print(l.split('"')[2]) + else: + print(l) + f() diff --git a/tests/micropython/emg_exc.py.exp b/tests/micropython/emg_exc.py.exp index 82b10b5f5..fd2cfb272 100644 --- a/tests/micropython/emg_exc.py.exp +++ b/tests/micropython/emg_exc.py.exp @@ -1 +1,4 @@ -ValueError: +Traceback (most recent call last): +, line 20, in f +ValueError: 1 + diff --git a/tests/run-tests b/tests/run-tests index 866a9b7c3..568d99f8e 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -345,6 +345,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_tests.add('misc/rge_sm.py') # requires yield skip_tests.add('misc/print_exception.py') # because native doesn't have proper traceback info skip_tests.add('misc/sys_exc_info.py') # sys.exc_info() is not supported for native + skip_tests.add('micropython/emg_exc.py') # because native doesn't have proper traceback info skip_tests.add('micropython/heapalloc_traceback.py') # because native doesn't have proper traceback info skip_tests.add('micropython/heapalloc_iter.py') # requires generators skip_tests.add('micropython/schedule.py') # native code doesn't check pending events From 7885a425d78a5a2fa7da5099cdd547a35f5e69dd Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 31 Aug 2017 21:42:39 +1000 Subject: [PATCH 049/163] py/stream: Remove unnecessary checks for NULL return from vstr_add_len. The vstr argument to the calls to vstr_add_len are dynamically allocated (ie fixed_buf=false) and so vstr_add_len will never return NULL. So there's no need to check for it. Any out-of-memory errors are raised by the call to m_renew in vstr_ensure_extra. --- py/stream.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/py/stream.c b/py/stream.c index 0029a59a7..0adf0af06 100644 --- a/py/stream.c +++ b/py/stream.c @@ -141,9 +141,6 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl mp_uint_t last_buf_offset = 0; while (more_bytes > 0) { char *p = vstr_add_len(&vstr, more_bytes); - if (p == NULL) { - mp_raise_msg(&mp_type_MemoryError, "out of memory"); - } int error; mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error); if (error != 0) { @@ -380,10 +377,6 @@ STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) while (max_size == -1 || max_size-- != 0) { char *p = vstr_add_len(&vstr, 1); - if (p == NULL) { - mp_raise_msg(&mp_type_MemoryError, "out of memory"); - } - int error; mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error); if (out_sz == MP_STREAM_ERROR) { From ede8a0235b47333aaaa8d03a3b9e20b014be3808 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 1 Sep 2017 08:05:24 +1000 Subject: [PATCH 050/163] py/vstr: Raise a RuntimeError if fixed vstr buffer overflows. Current users of fixed vstr buffers (building file paths) assume that there is no overflow and do not check for overflow after building the vstr. This has the potential to lead to NULL pointer dereferences (when vstr_null_terminated_str returns NULL because it can't allocate RAM for the terminating byte) and stat'ing and loading invalid path names (due to the path being truncated). The safest and simplest thing to do in these cases is just raise an exception if a write goes beyond the end of a fixed vstr buffer, which is what this patch does. It also simplifies the vstr code. --- ports/unix/coverage.c | 17 +++++++-- py/vstr.c | 59 ++++++++------------------------ tests/unix/extra_coverage.py.exp | 3 +- 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 4a9ab194b..651db0a94 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -181,8 +181,21 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf); VSTR_FIXED(fix, 4); - vstr_add_str(&fix, "large"); - mp_printf(&mp_plat_print, "%.*s\n", (int)fix.len, fix.buf); + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + vstr_add_str(&fix, "large"); + nlr_pop(); + } else { + mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); + } + + fix.len = fix.alloc; + if (nlr_push(&nlr) == 0) { + vstr_null_terminated_str(&fix); + nlr_pop(); + } else { + mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); + } } // repl autocomplete diff --git a/py/vstr.c b/py/vstr.c index 8a00f6c6f..869b27805 100644 --- a/py/vstr.c +++ b/py/vstr.c @@ -30,7 +30,7 @@ #include #include "py/mpconfig.h" -#include "py/misc.h" +#include "py/runtime.h" #include "py/mpprint.h" // returned value is always at least 1 greater than argument @@ -92,7 +92,9 @@ void vstr_free(vstr_t *vstr) { // Extend vstr strictly by requested size, return pointer to newly added chunk. char *vstr_extend(vstr_t *vstr, size_t size) { if (vstr->fixed_buf) { - return NULL; + // We can't reallocate, and the caller is expecting the space to + // be there, so the only safe option is to raise an exception. + mp_raise_msg(&mp_type_RuntimeError, NULL); } char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size); char *p = new_buf + vstr->alloc; @@ -101,17 +103,18 @@ char *vstr_extend(vstr_t *vstr, size_t size) { return p; } -STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) { +STATIC void vstr_ensure_extra(vstr_t *vstr, size_t size) { if (vstr->len + size > vstr->alloc) { if (vstr->fixed_buf) { - return false; + // We can't reallocate, and the caller is expecting the space to + // be there, so the only safe option is to raise an exception. + mp_raise_msg(&mp_type_RuntimeError, NULL); } size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16); char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc); vstr->alloc = new_alloc; vstr->buf = new_buf; } - return true; } void vstr_hint_size(vstr_t *vstr, size_t size) { @@ -119,9 +122,7 @@ void vstr_hint_size(vstr_t *vstr, size_t size) { } char *vstr_add_len(vstr_t *vstr, size_t len) { - if (!vstr_ensure_extra(vstr, len)) { - return NULL; - } + vstr_ensure_extra(vstr, len); char *buf = vstr->buf + vstr->len; vstr->len += len; return buf; @@ -131,9 +132,7 @@ char *vstr_add_len(vstr_t *vstr, size_t len) { char *vstr_null_terminated_str(vstr_t *vstr) { // If there's no more room, add single byte if (vstr->alloc == vstr->len) { - if (vstr_extend(vstr, 1) == NULL) { - return NULL; - } + vstr_extend(vstr, 1); } vstr->buf[vstr->len] = '\0'; return vstr->buf; @@ -141,9 +140,6 @@ char *vstr_null_terminated_str(vstr_t *vstr) { void vstr_add_byte(vstr_t *vstr, byte b) { byte *buf = (byte*)vstr_add_len(vstr, 1); - if (buf == NULL) { - return; - } buf[0] = b; } @@ -153,31 +149,19 @@ void vstr_add_char(vstr_t *vstr, unichar c) { // Is it worth just calling vstr_add_len(vstr, 4)? if (c < 0x80) { byte *buf = (byte*)vstr_add_len(vstr, 1); - if (buf == NULL) { - return; - } *buf = (byte)c; } else if (c < 0x800) { byte *buf = (byte*)vstr_add_len(vstr, 2); - if (buf == NULL) { - return; - } buf[0] = (c >> 6) | 0xC0; buf[1] = (c & 0x3F) | 0x80; } else if (c < 0x10000) { byte *buf = (byte*)vstr_add_len(vstr, 3); - if (buf == NULL) { - return; - } buf[0] = (c >> 12) | 0xE0; buf[1] = ((c >> 6) & 0x3F) | 0x80; buf[2] = (c & 0x3F) | 0x80; } else { assert(c < 0x110000); byte *buf = (byte*)vstr_add_len(vstr, 4); - if (buf == NULL) { - return; - } buf[0] = (c >> 18) | 0xF0; buf[1] = ((c >> 12) & 0x3F) | 0x80; buf[2] = ((c >> 6) & 0x3F) | 0x80; @@ -193,16 +177,7 @@ void vstr_add_str(vstr_t *vstr, const char *str) { } void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) { - if (!vstr_ensure_extra(vstr, len)) { - // if buf is fixed, we got here because there isn't enough room left - // so just try to copy as much as we can, with room for a possible null byte - if (vstr->fixed_buf && vstr->len < vstr->alloc) { - len = vstr->alloc - vstr->len; - goto copy; - } - return; - } -copy: + vstr_ensure_extra(vstr, len); memmove(vstr->buf + vstr->len, str, len); vstr->len += len; } @@ -214,9 +189,7 @@ STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len } if (byte_len > 0) { // ensure room for the new bytes - if (!vstr_ensure_extra(vstr, byte_len)) { - return NULL; - } + vstr_ensure_extra(vstr, byte_len); // copy up the string to make room for the new bytes memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos); // increase the length @@ -227,17 +200,13 @@ STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) { char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1); - if (s != NULL) { - *s = b; - } + *s = b; } void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) { // TODO UNICODE char *s = vstr_ins_blank_bytes(vstr, char_pos, 1); - if (s != NULL) { - *s = chr; - } + *s = chr; } void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) { diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4dc421dab..4c4f66663 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -18,7 +18,8 @@ sts test tes -larg +RuntimeError: +RuntimeError: # repl ame__ From 980b33177bbb99f4bc6472c24a0cc67bf7760679 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 31 Aug 2017 13:54:49 +1000 Subject: [PATCH 051/163] stm32/usbdev: Put all CDC state in a struct. --- ports/stm32/usb.c | 52 +++-- ports/stm32/usbd_cdc_interface.c | 202 ++++++------------ ports/stm32/usbd_cdc_interface.h | 111 ++++++---- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 20 +- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 45 ++-- ports/stm32/usbdev/class/src/usbd_msc_bot.c | 20 +- ports/stm32/usbdev/class/src/usbd_msc_scsi.c | 34 +-- 7 files changed, 229 insertions(+), 255 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 6e06e70ba..9427329e4 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -55,6 +55,8 @@ mp_uint_t pyb_usb_flags = 0; #ifdef USE_DEVICE_MODE STATIC USBD_HandleTypeDef hUSBDDevice; +STATIC usbd_cdc_msc_hid_state_t usbd_cdc_msc_hid_state; +STATIC usbd_cdc_itf_t usbd_cdc_itf; pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE; #endif @@ -105,25 +107,37 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H #ifdef USE_DEVICE_MODE if (!(pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED)) { // only init USB once in the device's power-lifetime + + // configure the VID, PID and the USBD mode (interfaces it will expose) USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC); if (USBD_SelectMode(mode, hid_info) != 0) { return false; } - USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, USB_PHY_ID); - USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID); - USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops); + + // set up the USBD state + USBD_HandleTypeDef *usbd = &hUSBDDevice; + usbd->id = USB_PHY_ID; + usbd->dev_state = USBD_STATE_DEFAULT; + usbd->pDesc = (USBD_DescriptorsTypeDef*)&USBD_Descriptors; + usbd->pClass = &USBD_CDC_MSC_HID; + usbd_cdc_msc_hid_state.cdc = &usbd_cdc_itf; + usbd->pClassData = &usbd_cdc_msc_hid_state; + switch (pyb_usb_storage_medium) { #if MICROPY_HW_HAS_SDCARD case PYB_USB_STORAGE_MEDIUM_SDCARD: - USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); + USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); break; #endif default: - USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); + USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); break; } - USBD_HID_RegisterInterface(&hUSBDDevice, (USBD_HID_ItfTypeDef*)&USBD_HID_fops); - USBD_Start(&hUSBDDevice); + USBD_HID_RegisterInterface(usbd, (USBD_HID_ItfTypeDef*)&USBD_HID_fops); + + // start the USB device + USBD_LL_Init(usbd); + USBD_LL_Start(usbd); } pyb_usb_flags |= PYB_USB_FLAG_DEV_ENABLED; #endif @@ -143,13 +157,13 @@ bool usb_vcp_is_enabled(void) { } int usb_vcp_recv_byte(uint8_t *c) { - return USBD_CDC_Rx(c, 1, 0); + return usbd_cdc_rx(&usbd_cdc_itf, c, 1, 0); } void usb_vcp_send_strn(const char *str, int len) { #ifdef USE_DEVICE_MODE if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { - USBD_CDC_TxAlways((const uint8_t*)str, len); + usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)str, len); } #endif } @@ -159,9 +173,9 @@ void usb_vcp_send_strn_cooked(const char *str, int len) { if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { for (const char *top = str + len; str < top; str++) { if (*str == '\n') { - USBD_CDC_TxAlways((const uint8_t*)"\r\n", 2); + usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)"\r\n", 2); } else { - USBD_CDC_TxAlways((const uint8_t*)str, 1); + usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)str, 1); } } } @@ -362,7 +376,7 @@ STATIC mp_obj_t pyb_usb_vcp_setinterrupt(mp_obj_t self_in, mp_obj_t int_chr_in) STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_usb_vcp_setinterrupt_obj, pyb_usb_vcp_setinterrupt); STATIC mp_obj_t pyb_usb_vcp_isconnected(mp_obj_t self_in) { - return mp_obj_new_bool(USBD_CDC_IsConnected()); + return mp_obj_new_bool(usbd_cdc_is_connected(&usbd_cdc_itf)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_vcp_isconnected_obj, pyb_usb_vcp_isconnected); @@ -375,7 +389,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc); /// \method any() /// Return `True` if any characters waiting, else `False`. STATIC mp_obj_t pyb_usb_vcp_any(mp_obj_t self_in) { - if (USBD_CDC_RxNum() > 0) { + if (usbd_cdc_rx_num(&usbd_cdc_itf) > 0) { return mp_const_true; } else { return mp_const_false; @@ -407,7 +421,7 @@ STATIC mp_obj_t pyb_usb_vcp_send(size_t n_args, const mp_obj_t *args, mp_map_t * pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data); // send the data - int ret = USBD_CDC_Tx(bufinfo.buf, bufinfo.len, vals[1].u_int); + int ret = usbd_cdc_tx(&usbd_cdc_itf, bufinfo.buf, bufinfo.len, vals[1].u_int); return mp_obj_new_int(ret); } @@ -433,7 +447,7 @@ STATIC mp_obj_t pyb_usb_vcp_recv(size_t n_args, const mp_obj_t *args, mp_map_t * mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // receive the data - int ret = USBD_CDC_Rx((uint8_t*)vstr.buf, vstr.len, vals[1].u_int); + int ret = usbd_cdc_rx(&usbd_cdc_itf, (uint8_t*)vstr.buf, vstr.len, vals[1].u_int); // return the received data if (o_ret != MP_OBJ_NULL) { @@ -470,7 +484,7 @@ STATIC const mp_rom_map_elem_t pyb_usb_vcp_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_usb_vcp_locals_dict, pyb_usb_vcp_locals_dict_table); STATIC mp_uint_t pyb_usb_vcp_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { - int ret = USBD_CDC_Rx((byte*)buf, size, 0); + int ret = usbd_cdc_rx(&usbd_cdc_itf, (byte*)buf, size, 0); if (ret == 0) { // return EAGAIN error to indicate non-blocking *errcode = MP_EAGAIN; @@ -480,7 +494,7 @@ STATIC mp_uint_t pyb_usb_vcp_read(mp_obj_t self_in, void *buf, mp_uint_t size, i } STATIC mp_uint_t pyb_usb_vcp_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { - int ret = USBD_CDC_Tx((const byte*)buf, size, 0); + int ret = usbd_cdc_tx(&usbd_cdc_itf, (const byte*)buf, size, 0); if (ret == 0) { // return EAGAIN error to indicate non-blocking *errcode = MP_EAGAIN; @@ -494,10 +508,10 @@ STATIC mp_uint_t pyb_usb_vcp_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_ if (request == MP_STREAM_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_STREAM_POLL_RD) && USBD_CDC_RxNum() > 0) { + if ((flags & MP_STREAM_POLL_RD) && usbd_cdc_rx_num(&usbd_cdc_itf) > 0) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && USBD_CDC_TxHalfEmpty()) { + if ((flags & MP_STREAM_POLL_WR) && usbd_cdc_tx_half_empty(&usbd_cdc_itf)) { ret |= MP_STREAM_POLL_WR; } } else { diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 3e107d418..40c04061b 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -41,12 +41,9 @@ #include "usbd_cdc_interface.h" #include "pendsv.h" -#include "py/mpstate.h" #include "py/obj.h" #include "lib/utils/interrupt_char.h" #include "irq.h" -#include "timer.h" -#include "usb.h" // CDC control commands #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 @@ -59,78 +56,26 @@ #define CDC_SET_CONTROL_LINE_STATE 0x22 #define CDC_SEND_BREAK 0x23 -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define APP_RX_DATA_SIZE 1024 // this must be 2 or greater, and a power of 2 -#define APP_TX_DATA_SIZE 1024 // I think this can be any value (was 2048) - -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ - -static __IO uint8_t dev_is_connected = 0; // indicates if we are connected - -static uint8_t cdc_rx_packet_buf[CDC_DATA_FS_MAX_PACKET_SIZE]; // received data from USB OUT endpoint is stored in this buffer -static uint8_t cdc_rx_user_buf[APP_RX_DATA_SIZE]; // received data is buffered here until the user reads it -static volatile uint16_t cdc_rx_buf_put = 0; // circular buffer index -static uint16_t cdc_rx_buf_get = 0; // circular buffer index - -static uint8_t UserTxBuffer[APP_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer -static uint16_t UserTxBufPtrIn = 0; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available -static __IO uint16_t UserTxBufPtrOut = 0; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained -static uint16_t UserTxBufPtrOutShadow = 0; // shadow of above -static uint8_t UserTxBufPtrWaitCount = 0; // used to implement a timeout waiting for low-level USB driver -static uint8_t UserTxNeedEmptyPacket = 0; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size - -/* Private function prototypes -----------------------------------------------*/ -static int8_t CDC_Itf_Init (USBD_HandleTypeDef *pdev); -static int8_t CDC_Itf_DeInit (void); -static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length); -static int8_t CDC_Itf_Receive (USBD_HandleTypeDef *pdev, uint8_t* pbuf, uint32_t *Len); - -const USBD_CDC_ItfTypeDef USBD_CDC_fops = { - CDC_Itf_Init, - CDC_Itf_DeInit, - CDC_Itf_Control, - CDC_Itf_Receive -}; - -/* Private functions ---------------------------------------------------------*/ - -/** - * @brief CDC_Itf_Init - * Initializes the CDC media low layer - * @param None - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t CDC_Itf_Init(USBD_HandleTypeDef *pdev) { - USBD_CDC_SetTxBuffer(pdev, UserTxBuffer, 0); - USBD_CDC_SetRxBuffer(pdev, cdc_rx_packet_buf); - - cdc_rx_buf_put = 0; - cdc_rx_buf_get = 0; - - return USBD_OK; +void usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) { + cdc->usb = pdev; + cdc->rx_buf_put = 0; + cdc->rx_buf_get = 0; + cdc->tx_buf_ptr_in = 0; + cdc->tx_buf_ptr_out = 0; + cdc->tx_buf_ptr_out_shadow = 0; + cdc->tx_buf_ptr_wait_count = 0; + cdc->tx_need_empty_packet = 0; + cdc->dev_is_connected = 0; + USBD_CDC_SetTxBuffer(pdev, cdc->tx_buf, 0); + USBD_CDC_SetRxBuffer(pdev, cdc->rx_packet_buf); } -/** - * @brief CDC_Itf_DeInit - * DeInitializes the CDC media low layer - * @param None - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t CDC_Itf_DeInit(void) { - return USBD_OK; -} - -/** - * @brief CDC_Itf_Control - * Manage the CDC class requests - * @param Cmd: Command code - * @param Buf: Buffer containing command data (request parameters) - * @param Len: Number of data to be sent (in bytes) - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) { +// Manage the CDC class requests +// cmd: command code +// pbuf: buffer containing command data (request parameters) +// length: number of data to be sent (in bytes) +// Returns USBD_OK if all operations are OK else USBD_FAIL +int8_t usbd_cdc_control(usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length) { switch (cmd) { case CDC_SEND_ENCAPSULATED_COMMAND: /* Add your code here */ @@ -175,7 +120,7 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) { break; case CDC_SET_CONTROL_LINE_STATE: - dev_is_connected = length & 1; // wValue is passed in Len (bit of a hack) + cdc->dev_is_connected = length & 1; // wValue is passed in Len (bit of a hack) break; case CDC_SEND_BREAK: @@ -193,53 +138,56 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) { // SOF (start of frame) callback so that it is called exactly at the time it is // needed (reducing latency), and often enough (increasing bandwidth). void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { - if (!dev_is_connected) { + usbd_cdc_msc_hid_state_t *usbd = ((USBD_HandleTypeDef*)hpcd->pData)->pClassData; + usbd_cdc_itf_t *cdc = usbd->cdc; + + if (cdc == NULL || !cdc->dev_is_connected) { // CDC device is not connected to a host, so we are unable to send any data return; } - if (UserTxBufPtrOut == UserTxBufPtrIn && !UserTxNeedEmptyPacket) { + if (cdc->tx_buf_ptr_out == cdc->tx_buf_ptr_in && !cdc->tx_need_empty_packet) { // No outstanding data to send return; } - if (UserTxBufPtrOut != UserTxBufPtrOutShadow) { + if (cdc->tx_buf_ptr_out != cdc->tx_buf_ptr_out_shadow) { // We have sent data and are waiting for the low-level USB driver to // finish sending it over the USB in-endpoint. // SOF occurs every 1ms, so we have a 500 * 1ms = 500ms timeout // We have a relatively large timeout because the USB host may be busy // doing other things and we must give it a chance to read our data. - if (UserTxBufPtrWaitCount < 500) { + if (cdc->tx_buf_ptr_wait_count < 500) { USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; if (USBx_INEP(CDC_IN_EP & 0x7f)->DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ) { // USB in-endpoint is still reading the data - UserTxBufPtrWaitCount++; + cdc->tx_buf_ptr_wait_count++; return; } } - UserTxBufPtrOut = UserTxBufPtrOutShadow; + cdc->tx_buf_ptr_out = cdc->tx_buf_ptr_out_shadow; } - if (UserTxBufPtrOutShadow != UserTxBufPtrIn || UserTxNeedEmptyPacket) { + if (cdc->tx_buf_ptr_out_shadow != cdc->tx_buf_ptr_in || cdc->tx_need_empty_packet) { uint32_t buffptr; uint32_t buffsize; - if (UserTxBufPtrOutShadow > UserTxBufPtrIn) { // rollback - buffsize = APP_TX_DATA_SIZE - UserTxBufPtrOutShadow; + if (cdc->tx_buf_ptr_out_shadow > cdc->tx_buf_ptr_in) { // rollback + buffsize = USBD_CDC_TX_DATA_SIZE - cdc->tx_buf_ptr_out_shadow; } else { - buffsize = UserTxBufPtrIn - UserTxBufPtrOutShadow; + buffsize = cdc->tx_buf_ptr_in - cdc->tx_buf_ptr_out_shadow; } - buffptr = UserTxBufPtrOutShadow; + buffptr = cdc->tx_buf_ptr_out_shadow; - USBD_CDC_SetTxBuffer(hpcd->pData, (uint8_t*)&UserTxBuffer[buffptr], buffsize); + USBD_CDC_SetTxBuffer(hpcd->pData, (uint8_t*)&cdc->tx_buf[buffptr], buffsize); if (USBD_CDC_TransmitPacket(hpcd->pData) == USBD_OK) { - UserTxBufPtrOutShadow += buffsize; - if (UserTxBufPtrOutShadow == APP_TX_DATA_SIZE) { - UserTxBufPtrOutShadow = 0; + cdc->tx_buf_ptr_out_shadow += buffsize; + if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) { + cdc->tx_buf_ptr_out_shadow = 0; } - UserTxBufPtrWaitCount = 0; + cdc->tx_buf_ptr_wait_count = 0; // According to the USB specification, a packet size of 64 bytes (CDC_DATA_FS_MAX_PACKET_SIZE) // gets held at the USB host until the next packet is sent. This is because a @@ -247,62 +195,54 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { // the host waits for all data to arrive (ie, waits for a packet < max packet size). // To flush a packet of exactly max packet size, we need to send a zero-size packet. // See eg http://www.cypress.com/?id=4&rID=92719 - UserTxNeedEmptyPacket = (buffsize > 0 && buffsize % CDC_DATA_FS_MAX_PACKET_SIZE == 0 && UserTxBufPtrOutShadow == UserTxBufPtrIn); + cdc->tx_need_empty_packet = (buffsize > 0 && buffsize % CDC_DATA_FS_MAX_PACKET_SIZE == 0 && cdc->tx_buf_ptr_out_shadow == cdc->tx_buf_ptr_in); } } } -/** - * @brief CDC_Itf_DataRx - * Data received over USB OUT endpoint is processed here. - * @param Buf: Buffer of data received - * @param Len: Number of data received (in bytes) - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - * @note The buffer we are passed here is just cdc_rx_packet_buf, so we are - * free to modify it. - */ -static int8_t CDC_Itf_Receive(USBD_HandleTypeDef *pdev, uint8_t* Buf, uint32_t *Len) { +// Data received over USB OUT endpoint is processed here. +// Buf: buffer of data received +// Len: number of data received (in bytes) +// Returns USBD_OK if all operations are OK else USBD_FAIL +// The buffer we are passed here is just cdc_rx_packet_buf, so we are free to modify it. +int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len) { // copy the incoming data into the circular buffer for (uint8_t *src = Buf, *top = Buf + *Len; src < top; ++src) { if (mp_interrupt_char != -1 && *src == mp_interrupt_char) { pendsv_kbd_intr(); } else { - uint16_t next_put = (cdc_rx_buf_put + 1) & (APP_RX_DATA_SIZE - 1); - if (next_put == cdc_rx_buf_get) { + uint16_t next_put = (cdc->rx_buf_put + 1) & (USBD_CDC_RX_DATA_SIZE - 1); + if (next_put == cdc->rx_buf_get) { // overflow, we just discard the rest of the chars break; } - cdc_rx_user_buf[cdc_rx_buf_put] = *src; - cdc_rx_buf_put = next_put; + cdc->rx_user_buf[cdc->rx_buf_put] = *src; + cdc->rx_buf_put = next_put; } } // initiate next USB packet transfer - USBD_CDC_SetRxBuffer(pdev, cdc_rx_packet_buf); - USBD_CDC_ReceivePacket(pdev); + USBD_CDC_SetRxBuffer(cdc->usb, cdc->rx_packet_buf); + USBD_CDC_ReceivePacket(cdc->usb); return USBD_OK; } -int USBD_CDC_IsConnected(void) { - return dev_is_connected; -} - -int USBD_CDC_TxHalfEmpty(void) { - int32_t tx_waiting = (int32_t)UserTxBufPtrIn - (int32_t)UserTxBufPtrOut; +int usbd_cdc_tx_half_empty(usbd_cdc_itf_t *cdc) { + int32_t tx_waiting = (int32_t)cdc->tx_buf_ptr_in - (int32_t)cdc->tx_buf_ptr_out; if (tx_waiting < 0) { - tx_waiting += APP_TX_DATA_SIZE; + tx_waiting += USBD_CDC_TX_DATA_SIZE; } - return tx_waiting <= APP_TX_DATA_SIZE / 2; + return tx_waiting <= USBD_CDC_TX_DATA_SIZE / 2; } // timout in milliseconds. // Returns number of bytes written to the device. -int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout) { +int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t timeout) { for (uint32_t i = 0; i < len; i++) { // Wait until the device is connected and the buffer has space, with a given timeout uint32_t start = HAL_GetTick(); - while (!dev_is_connected || ((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut) { + while (!cdc->dev_is_connected || ((cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1)) == cdc->tx_buf_ptr_out) { // Wraparound of tick is taken care of by 2's complement arithmetic. if (HAL_GetTick() - start >= timeout) { // timeout @@ -316,8 +256,8 @@ int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout) { } // Write data to device buffer - UserTxBuffer[UserTxBufPtrIn] = buf[i]; - UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1); + cdc->tx_buf[cdc->tx_buf_ptr_in] = buf[i]; + cdc->tx_buf_ptr_in = (cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1); } // Success, return number of bytes read @@ -327,18 +267,18 @@ int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout) { // Always write all of the data to the device tx buffer, even if the // device is not connected, or if the buffer is full. Has a small timeout // to wait for the buffer to be drained, in the case the device is connected. -void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len) { +void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { for (int i = 0; i < len; i++) { // If the CDC device is not connected to the host then we don't have anyone to receive our data. // The device may become connected in the future, so we should at least try to fill the buffer // and hope that it doesn't overflow by the time the device connects. // If the device is not connected then we should go ahead and fill the buffer straight away, // ignoring overflow. Otherwise, we should make sure that we have enough room in the buffer. - if (dev_is_connected) { + if (cdc->dev_is_connected) { // If the buffer is full, wait until it gets drained, with a timeout of 500ms // (wraparound of tick is taken care of by 2's complement arithmetic). uint32_t start = HAL_GetTick(); - while (((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut && HAL_GetTick() - start <= 500) { + while (((cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1)) == cdc->tx_buf_ptr_out && HAL_GetTick() - start <= 500) { if (query_irq() == IRQ_STATE_DISABLED) { // IRQs disabled so buffer will never be drained; exit loop break; @@ -365,28 +305,28 @@ void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len) { */ } - UserTxBuffer[UserTxBufPtrIn] = buf[i]; - UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1); + cdc->tx_buf[cdc->tx_buf_ptr_in] = buf[i]; + cdc->tx_buf_ptr_in = (cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1); } } // Returns number of bytes in the rx buffer. -int USBD_CDC_RxNum(void) { - int32_t rx_waiting = (int32_t)cdc_rx_buf_put - (int32_t)cdc_rx_buf_get; +int usbd_cdc_rx_num(usbd_cdc_itf_t *cdc) { + int32_t rx_waiting = (int32_t)cdc->rx_buf_put - (int32_t)cdc->rx_buf_get; if (rx_waiting < 0) { - rx_waiting += APP_RX_DATA_SIZE; + rx_waiting += USBD_CDC_RX_DATA_SIZE; } return rx_waiting; } // timout in milliseconds. // Returns number of bytes read from the device. -int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout) { +int usbd_cdc_rx(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len, uint32_t timeout) { // loop to read bytes for (uint32_t i = 0; i < len; i++) { // Wait until we have at least 1 byte to read uint32_t start = HAL_GetTick(); - while (cdc_rx_buf_put == cdc_rx_buf_get) { + while (cdc->rx_buf_put == cdc->rx_buf_get) { // Wraparound of tick is taken care of by 2's complement arithmetic. if (HAL_GetTick() - start >= timeout) { // timeout @@ -400,8 +340,8 @@ int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout) { } // Copy byte from device to user buffer - buf[i] = cdc_rx_user_buf[cdc_rx_buf_get]; - cdc_rx_buf_get = (cdc_rx_buf_get + 1) & (APP_RX_DATA_SIZE - 1); + buf[i] = cdc->rx_user_buf[cdc->rx_buf_get]; + cdc->rx_buf_get = (cdc->rx_buf_get + 1) & (USBD_CDC_RX_DATA_SIZE - 1); } // Success, return number of bytes read diff --git a/ports/stm32/usbd_cdc_interface.h b/ports/stm32/usbd_cdc_interface.h index 811a28928..dde4fa3e4 100644 --- a/ports/stm32/usbd_cdc_interface.h +++ b/ports/stm32/usbd_cdc_interface.h @@ -1,45 +1,66 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - */ -#ifndef MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H -#define MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H - -/** - ****************************************************************************** - * @file USB_Device/CDC_Standalone/Inc/usbd_cdc_interface.h - * @author MCD Application Team - * @version V1.0.1 - * @date 26-February-2014 - * @brief Header for usbd_cdc_interface.c file. - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2014 STMicroelectronics

- * - * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); - * You may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.st.com/software_license_agreement_liberty_v2 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ****************************************************************************** - */ - -extern const USBD_CDC_ItfTypeDef USBD_CDC_fops; - -int USBD_CDC_IsConnected(void); - -int USBD_CDC_TxHalfEmpty(void); -int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout); -void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len); - -int USBD_CDC_RxNum(void); -int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout); - -#endif // MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H +/* + * This file is part of the MicroPython project, http://micropython.org/ + */ +#ifndef MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H +#define MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H + +/** + ****************************************************************************** + * @file USB_Device/CDC_Standalone/Inc/usbd_cdc_interface.h + * @author MCD Application Team + * @version V1.0.1 + * @date 26-February-2014 + * @brief Header for usbd_cdc_interface.c file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2014 STMicroelectronics

+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + +#define USBD_CDC_RX_DATA_SIZE (1024) // this must be 2 or greater, and a power of 2 +#define USBD_CDC_TX_DATA_SIZE (1024) // I think this can be any value (was 2048) + +typedef struct _usbd_cdc_itf_t { + USBD_HandleTypeDef *usb; // the parent USB device + + uint8_t rx_packet_buf[CDC_DATA_FS_MAX_PACKET_SIZE]; // received data from USB OUT endpoint is stored in this buffer + uint8_t rx_user_buf[USBD_CDC_RX_DATA_SIZE]; // received data is buffered here until the user reads it + volatile uint16_t rx_buf_put; // circular buffer index + uint16_t rx_buf_get; // circular buffer index + + uint8_t tx_buf[USBD_CDC_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer + uint16_t tx_buf_ptr_in; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available + volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained + uint16_t tx_buf_ptr_out_shadow; // shadow of above + uint8_t tx_buf_ptr_wait_count; // used to implement a timeout waiting for low-level USB driver + uint8_t tx_need_empty_packet; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size + + volatile uint8_t dev_is_connected; // indicates if we are connected +} usbd_cdc_itf_t; + +static inline int usbd_cdc_is_connected(usbd_cdc_itf_t *cdc) { + return cdc->dev_is_connected; +} + +int usbd_cdc_tx_half_empty(usbd_cdc_itf_t *cdc); +int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t timeout); +void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len); + +int usbd_cdc_rx_num(usbd_cdc_itf_t *cdc); +int usbd_cdc_rx(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len, uint32_t timeout); + +#endif // MICROPY_INCLUDED_STMHAL_USBD_CDC_INTERFACE_H diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index a9fffe958..5b2ffb4cf 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -27,13 +27,6 @@ typedef struct { uint8_t datatype; } USBD_CDC_LineCodingTypeDef; -typedef struct _USBD_CDC_Itf { - int8_t (* Init) (USBD_HandleTypeDef *pdev); - int8_t (* DeInit) (void); - int8_t (* Control) (uint8_t, uint8_t * , uint16_t); - int8_t (* Receive) (USBD_HandleTypeDef *pdev, uint8_t *, uint32_t *); -} USBD_CDC_ItfTypeDef; - typedef struct { uint32_t data[CDC_DATA_FS_MAX_PACKET_SIZE/4]; /* Force 32bits alignment */ uint8_t CmdOpCode; @@ -86,6 +79,12 @@ typedef struct { uint32_t scsi_blk_len; } USBD_MSC_BOT_HandleTypeDef; +typedef struct _usbd_cdc_msc_hid_state_t { + void *cdc; + void *msc; + void *hid; +} usbd_cdc_msc_hid_state_t; + #define USBD_HID_MOUSE_MAX_PACKET (4) #define USBD_HID_MOUSE_REPORT_DESC_SIZE (74) @@ -103,7 +102,6 @@ int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info); // returns the current usb mode uint8_t USBD_GetMode(); -uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops); uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length); uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff); uint8_t USBD_CDC_ReceivePacket (USBD_HandleTypeDef *pdev); @@ -119,4 +117,10 @@ uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev); uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev); +// These are provided externally to implement the CDC interface +struct _usbd_cdc_itf_t; +void usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev); +int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length); +int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len); + #endif // _USB_CDC_MSC_CORE_H_ diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index 0997bb50e..38e5bcbc6 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -95,7 +95,6 @@ static uint8_t usbd_config_desc_size; static uint8_t *hid_desc; static const uint8_t *hid_report_desc; -static USBD_CDC_ItfTypeDef *CDC_fops; static USBD_StorageTypeDef *MSC_fops; static USBD_HID_ItfTypeDef *HID_fops; @@ -647,6 +646,8 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { return 1; } + usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + if (usbd_mode & USBD_MODE_CDC) { // CDC VCP component @@ -669,7 +670,7 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { CDC_CMD_PACKET_SIZE); // Init physical Interface components - CDC_fops->Init(pdev); + usbd_cdc_init(state->cdc, pdev); // Init Xfer states CDC_ClassData.TxState =0; @@ -694,8 +695,8 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_BULK, MSC_MAX_PACKET); - // MSC uses the pClassData pointer because SCSI and BOT reference it - pdev->pClassData = &MSC_BOT_ClassData; + // Set the MSC data for SCSI and BOT to reference it + state->msc = &MSC_BOT_ClassData; // Init the BOT layer MSC_BOT_Init(pdev); @@ -736,19 +737,18 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { } static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { - if (usbd_mode & USBD_MODE_CDC) { + usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + + if ((usbd_mode & USBD_MODE_CDC) && state->cdc) { // CDC VCP component // close endpoints USBD_LL_CloseEP(pdev, CDC_IN_EP); USBD_LL_CloseEP(pdev, CDC_OUT_EP); USBD_LL_CloseEP(pdev, CDC_CMD_EP); - - // DeInit physical Interface components - CDC_fops->DeInit(); } - if (usbd_mode & USBD_MODE_MSC) { + if ((usbd_mode & USBD_MODE_MSC) && state->msc) { // MSC component // close endpoints @@ -758,8 +758,8 @@ static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) // DeInit the BOT layer MSC_BOT_DeInit(pdev); - // clear the pointer - pdev->pClassData = NULL; + // clear the state pointer + state->msc = NULL; } if (usbd_mode & USBD_MODE_HID) { @@ -791,6 +791,8 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp SU: 21 20 0 1 */ + usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + switch (req->bmRequest & USB_REQ_TYPE_MASK) { // Class request @@ -801,7 +803,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp if (req->wLength) { if (req->bmRequest & 0x80) { // device-to-host request - CDC_fops->Control(req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength); + usbd_cdc_control(state->cdc, req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength); USBD_CtlSendData(pdev, (uint8_t*)CDC_ClassData.data, req->wLength); } else { // host-to-device request @@ -812,7 +814,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp } else { // Not a Data request // Transfer the command to the interface layer - return CDC_fops->Control(req->bRequest, NULL, req->wValue); + return usbd_cdc_control(state->cdc, req->bRequest, NULL, req->wValue); } } else if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { // MSC component @@ -931,8 +933,9 @@ static uint8_t EP0_TxSent(USBD_HandleTypeDef *pdev) { */ static uint8_t USBD_CDC_MSC_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) { - if ((CDC_fops != NULL) && (CDC_ClassData.CmdOpCode != 0xff)) { - CDC_fops->Control(CDC_ClassData.CmdOpCode, (uint8_t*)CDC_ClassData.data, CDC_ClassData.CmdLength); + usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + if (state->cdc != NULL && CDC_ClassData.CmdOpCode != 0xff) { + usbd_cdc_control(state->cdc, CDC_ClassData.CmdOpCode, (uint8_t*)CDC_ClassData.data, CDC_ClassData.CmdLength); CDC_ClassData.CmdOpCode = 0xff; } @@ -957,13 +960,14 @@ static uint8_t USBD_CDC_MSC_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) } static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) { + usbd_cdc_msc_hid_state_t *state = pdev->pClassData; if ((usbd_mode & USBD_MODE_CDC) && epnum == (CDC_OUT_EP & 0x7f)) { /* Get the received data length */ CDC_ClassData.RxLength = USBD_LL_GetRxDataSize (pdev, epnum); /* USB data will be immediately processed, this allow next USB traffic being NAKed till the end of the application Xfer */ - CDC_fops->Receive(pdev, CDC_ClassData.RxBuffer, &CDC_ClassData.RxLength); + usbd_cdc_receive(state->cdc, CDC_ClassData.RxBuffer, &CDC_ClassData.RxLength); return USBD_OK; } else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_OUT_EP & 0x7f)) { @@ -992,15 +996,6 @@ uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) { return NULL; } -uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops) { - if (fops == NULL) { - return USBD_FAIL; - } else { - CDC_fops = fops; - return USBD_OK; - } -} - uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length) { CDC_ClassData.TxBuffer = pbuff; CDC_ClassData.TxLength = length; diff --git a/ports/stm32/usbdev/class/src/usbd_msc_bot.c b/ports/stm32/usbdev/class/src/usbd_msc_bot.c index 3c06f3cf6..7c6ceeecf 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_bot.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_bot.c @@ -104,7 +104,7 @@ static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev); */ void MSC_BOT_Init (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_state = USBD_BOT_IDLE; hmsc->bot_status = USBD_BOT_STATUS_NORMAL; @@ -132,7 +132,7 @@ void MSC_BOT_Init (USBD_HandleTypeDef *pdev) */ void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_state = USBD_BOT_IDLE; hmsc->bot_status = USBD_BOT_STATUS_RECOVERY; @@ -152,7 +152,7 @@ void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) */ void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_state = USBD_BOT_IDLE; } @@ -166,7 +166,7 @@ void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; switch (hmsc->bot_state) { @@ -199,7 +199,7 @@ void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; switch (hmsc->bot_state) { @@ -231,7 +231,7 @@ void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, */ static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->csw.dTag = hmsc->cbw.dTag; hmsc->csw.dDataResidue = hmsc->cbw.dDataLength; @@ -300,7 +300,7 @@ static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t* buf, uint16_t len) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; len = MIN (hmsc->cbw.dDataLength, len); hmsc->csw.dDataResidue -= len; @@ -320,7 +320,7 @@ static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, uint8_t CSW_Status) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->csw.dSignature = USBD_BOT_CSW_SIGNATURE; hmsc->csw.bStatus = CSW_Status; @@ -348,7 +348,7 @@ void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if ((hmsc->cbw.bmFlags == 0) && (hmsc->cbw.dDataLength != 0) && @@ -377,7 +377,7 @@ static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if(hmsc->bot_status == USBD_BOT_STATUS_ERROR )/* Bad CBW Signature */ { diff --git a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c index b2931b745..0664883ad 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c @@ -190,7 +190,7 @@ int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, */ static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; /* case 9 : Hi > D0 */ if (hmsc->cbw.dDataLength != 0) @@ -227,7 +227,7 @@ static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *par { uint8_t* pPage; uint16_t len; - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if (params[1] & 0x01)/*Evpd is set*/ { @@ -264,7 +264,7 @@ static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *par */ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) { @@ -300,7 +300,7 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ */ static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; uint16_t blk_size; uint32_t blk_nbr; @@ -345,7 +345,7 @@ static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, ui */ static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; uint16_t len = 8 ; hmsc->bot_data_length = len; @@ -367,7 +367,7 @@ static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t * static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { uint16_t len = 8; - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_data_length = len; @@ -381,7 +381,7 @@ static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t static int8_t SCSI_SynchronizeCache(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { // nothing to synchronize, so just return "success" - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_data_length = 0; return 0; } @@ -397,7 +397,7 @@ static int8_t SCSI_SynchronizeCache(USBD_HandleTypeDef *pdev, uint8_t lun, uint static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { uint8_t i; - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; for(i=0 ; i < REQUEST_SENSE_DATA_LEN ; i++) { @@ -439,7 +439,7 @@ static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t */ void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_t ASC) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->scsi_sense[hmsc->scsi_sense_tail].Skey = sKey; hmsc->scsi_sense[hmsc->scsi_sense_tail].w.ASC = ASC << 8; @@ -458,7 +458,7 @@ void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_ */ static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_data_length = 0; // On Mac OS X, when the device is ejected a SCSI_START_STOP_UNIT command is sent. @@ -479,7 +479,7 @@ static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t */ static int8_t SCSI_AllowMediumRemoval(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; hmsc->bot_data_length = 0; ((USBD_StorageTypeDef *)pdev->pUserData)->PreventAllowMediumRemoval(lun, params[0]); return 0; @@ -494,7 +494,7 @@ static int8_t SCSI_AllowMediumRemoval(USBD_HandleTypeDef *pdev, uint8_t lun, ui */ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if(hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ { @@ -562,7 +562,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if (hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ { @@ -652,7 +652,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if ((params[1]& 0x02) == 0x02) { @@ -687,7 +687,7 @@ static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa */ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , uint32_t blk_offset , uint16_t blk_nbr) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr ) { @@ -708,7 +708,7 @@ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , u */ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) { - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; uint32_t len; len = MIN(hmsc->scsi_blk_len , MSC_MEDIA_PACKET); @@ -756,7 +756,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) { uint32_t len; - USBD_MSC_BOT_HandleTypeDef *hmsc = pdev->pClassData; + USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; len = MIN(hmsc->scsi_blk_len , MSC_MEDIA_PACKET); From 77e1da40e2fdcef736d05f2646fbaa8abf844543 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 5 Sep 2017 14:07:16 +1000 Subject: [PATCH 052/163] stm32/usbdev: Put all HID state in a struct. --- ports/stm32/usb.c | 7 +- ports/stm32/usbd_hid_interface.c | 80 ++++++------------- ports/stm32/usbd_hid_interface.h | 35 ++++---- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 11 ++- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 14 +--- 5 files changed, 58 insertions(+), 89 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 9427329e4..bbb7310b8 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -57,6 +57,7 @@ mp_uint_t pyb_usb_flags = 0; STATIC USBD_HandleTypeDef hUSBDDevice; STATIC usbd_cdc_msc_hid_state_t usbd_cdc_msc_hid_state; STATIC usbd_cdc_itf_t usbd_cdc_itf; +STATIC usbd_hid_itf_t usbd_hid_itf; pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE; #endif @@ -121,6 +122,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H usbd->pDesc = (USBD_DescriptorsTypeDef*)&USBD_Descriptors; usbd->pClass = &USBD_CDC_MSC_HID; usbd_cdc_msc_hid_state.cdc = &usbd_cdc_itf; + usbd_cdc_msc_hid_state.hid = &usbd_hid_itf; usbd->pClassData = &usbd_cdc_msc_hid_state; switch (pyb_usb_storage_medium) { @@ -133,7 +135,6 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); break; } - USBD_HID_RegisterInterface(usbd, (USBD_HID_ItfTypeDef*)&USBD_HID_fops); // start the USB device USBD_LL_Init(usbd); @@ -582,7 +583,7 @@ STATIC mp_obj_t pyb_usb_hid_recv(size_t n_args, const mp_obj_t *args, mp_map_t * mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // receive the data - int ret = USBD_HID_Rx(&hUSBDDevice, (uint8_t*)vstr.buf, vstr.len, vals[1].u_int); + int ret = usbd_hid_rx(&usbd_hid_itf, vstr.len, (uint8_t*)vstr.buf, vals[1].u_int); // return the received data if (o_ret != MP_OBJ_NULL) { @@ -642,7 +643,7 @@ STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_ if (request == MP_STREAM_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_STREAM_POLL_RD) && USBD_HID_RxNum() > 0) { + if ((flags & MP_STREAM_POLL_RD) && usbd_hid_rx_num(&usbd_hid_itf) > 0) { ret |= MP_STREAM_POLL_RD; } if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) { diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index 11b3a3acd..badf0144b 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -36,75 +36,47 @@ #include -#include "usbd_cdc_msc_hid.h" #include "usbd_hid_interface.h" #include "py/obj.h" #include "irq.h" #include "usb.h" -/* Private variables ---------------------------------------------------------*/ - -static uint8_t buffer[2][HID_DATA_FS_MAX_PACKET_SIZE]; // pair of buffers to read individual packets into -static int8_t current_read_buffer = 0; // which buffer to read from -static uint32_t last_read_len = 0; // length of last read -static int8_t current_write_buffer = 0; // which buffer to write to - -/* Private function prototypes -----------------------------------------------*/ -static int8_t HID_Itf_Init (USBD_HandleTypeDef *pdev); -static int8_t HID_Itf_Receive (USBD_HandleTypeDef *pdev, uint8_t* pbuf, uint32_t Len); - -const USBD_HID_ItfTypeDef USBD_HID_fops = { - HID_Itf_Init, - HID_Itf_Receive -}; - -/** - * @brief HID_Itf_Init - * Initializes the HID media low layer - * @param None - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t HID_Itf_Init(USBD_HandleTypeDef *pdev) -{ - current_read_buffer = 0; - last_read_len = 0; - current_write_buffer = 0; - USBD_HID_SetRxBuffer(pdev, buffer[current_write_buffer]); - return USBD_OK; +void usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { + hid->usb = pdev; + hid->current_read_buffer = 0; + hid->last_read_len = 0; + hid->current_write_buffer = 0; + USBD_HID_SetRxBuffer(pdev, hid->buffer[hid->current_write_buffer]); } -/** - * @brief HID_Itf_Receive - * Data received over USB OUT endpoint is processed here. - * @param Buf: Buffer of data received - * @param Len: Number of data received (in bytes) - * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL - * @note The buffer we are passed here is just UserRxBuffer, so we are - * free to modify it. - */ -static int8_t HID_Itf_Receive(USBD_HandleTypeDef *pdev, uint8_t* Buf, uint32_t Len) { - current_write_buffer = !current_write_buffer; - last_read_len = Len; +// Data received over USB OUT endpoint is processed here. +// buf: Buffer of data received +// len: Number of data received (in bytes) +// Returns USBD_OK if all operations are OK else USBD_FAIL +// The buffer we are passed here is just hid->buffer, so we are free to modify it. +int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len, uint8_t *buf) { + hid->current_write_buffer = !hid->current_write_buffer; + hid->last_read_len = len; // initiate next USB packet transfer, to append to existing data in buffer - USBD_HID_SetRxBuffer(pdev, buffer[current_write_buffer]); - USBD_HID_ReceivePacket(pdev); + USBD_HID_SetRxBuffer(hid->usb, hid->buffer[hid->current_write_buffer]); + USBD_HID_ReceivePacket(hid->usb); // Set NAK to indicate we need to process read buffer - USBD_HID_SetNAK(pdev); + USBD_HID_SetNAK(hid->usb); return USBD_OK; } // Returns number of ready rx buffers. -int USBD_HID_RxNum(void) { - return (current_read_buffer != current_write_buffer); +int usbd_hid_rx_num(usbd_hid_itf_t *hid) { + return hid->current_read_buffer != hid->current_write_buffer; } // timout in milliseconds. // Returns number of bytes read from the device. -int USBD_HID_Rx(USBD_HandleTypeDef *pdev, uint8_t *buf, uint32_t len, uint32_t timeout) { +int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout) { // Wait until we have buffer to read uint32_t start = HAL_GetTick(); - while (current_read_buffer == current_write_buffer) { + while (hid->current_read_buffer == hid->current_write_buffer) { // Wraparound of tick is taken care of by 2's complement arithmetic. if (HAL_GetTick() - start >= timeout) { // timeout @@ -118,17 +90,17 @@ int USBD_HID_Rx(USBD_HandleTypeDef *pdev, uint8_t *buf, uint32_t len, uint32_t t } // There is not enough space in buffer - if (len < last_read_len) { + if (len < hid->last_read_len) { return 0; } // Copy bytes from device to user buffer - memcpy(buf, buffer[current_read_buffer], last_read_len); - current_read_buffer = !current_read_buffer; + memcpy(buf, hid->buffer[hid->current_read_buffer], hid->last_read_len); + hid->current_read_buffer = !hid->current_read_buffer; // Clear NAK to indicate we are ready to read more data - USBD_HID_ClearNAK(pdev); + USBD_HID_ClearNAK(hid->usb); // Success, return number of bytes read - return last_read_len; + return hid->last_read_len; } diff --git a/ports/stm32/usbd_hid_interface.h b/ports/stm32/usbd_hid_interface.h index b2ff75fa1..3dc87de6b 100644 --- a/ports/stm32/usbd_hid_interface.h +++ b/ports/stm32/usbd_hid_interface.h @@ -1,14 +1,21 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - */ -#ifndef MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H -#define MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H - -#include "usbd_cdc_msc_hid.h" - -extern const USBD_HID_ItfTypeDef USBD_HID_fops; - -int USBD_HID_RxNum(void); -int USBD_HID_Rx(USBD_HandleTypeDef *pdev, uint8_t *buf, uint32_t len, uint32_t timeout); - -#endif // MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H +/* + * This file is part of the MicroPython project, http://micropython.org/ + */ +#ifndef MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H +#define MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H + +#include "usbd_cdc_msc_hid.h" + +typedef struct _usbd_hid_itf_t { + USBD_HandleTypeDef *usb; // the parent USB device + + uint8_t buffer[2][HID_DATA_FS_MAX_PACKET_SIZE]; // pair of buffers to read individual packets into + int8_t current_read_buffer; // which buffer to read from + uint32_t last_read_len; // length of last read + int8_t current_write_buffer; // which buffer to write to +} usbd_hid_itf_t; + +int usbd_hid_rx_num(usbd_hid_itf_t *hid); +int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout); + +#endif // MICROPY_INCLUDED_STMHAL_USBD_HID_INTERFACE_H diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index 5b2ffb4cf..c7f9b1560 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -40,11 +40,6 @@ typedef struct { __IO uint32_t RxState; } USBD_CDC_HandleTypeDef; -typedef struct _USBD_HID_Itf { - int8_t (* Init) (USBD_HandleTypeDef *pdev); - int8_t (* Receive)(USBD_HandleTypeDef *pdev, uint8_t *, uint32_t); -} USBD_HID_ItfTypeDef; - typedef struct _USBD_STORAGE { int8_t (* Init) (uint8_t lun); int8_t (* GetCapacity) (uint8_t lun, uint32_t *block_num, uint16_t *block_size); @@ -109,7 +104,6 @@ uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef *pdev); uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops); -uint8_t USBD_HID_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_HID_ItfTypeDef *fops); uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev); int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev); @@ -123,4 +117,9 @@ void usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev); int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length); int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len); +// These are provided externally to implement the HID interface +struct _usbd_hid_itf_t; +void usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); +int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len, uint8_t *buf); + #endif // _USB_CDC_MSC_CORE_H_ diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index 38e5bcbc6..d2daa3aee 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -96,7 +96,6 @@ static uint8_t *hid_desc; static const uint8_t *hid_report_desc; static USBD_StorageTypeDef *MSC_fops; -static USBD_HID_ItfTypeDef *HID_fops; static USBD_CDC_HandleTypeDef CDC_ClassData; static USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; @@ -725,7 +724,7 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_INTR, mps_out); - HID_fops->Init(pdev); + usbd_hid_init(state->hid, pdev); // Prepare Out endpoint to receive next packet USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out); @@ -975,7 +974,7 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) return USBD_OK; } else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_out_ep & 0x7f)) { HID_ClassData.RxLength = USBD_LL_GetRxDataSize(pdev, epnum); - HID_fops->Receive(pdev, HID_ClassData.RxBuffer, HID_ClassData.RxLength); + usbd_hid_receive(state->hid, HID_ClassData.RxLength, HID_ClassData.RxBuffer); } return USBD_OK; @@ -1044,15 +1043,6 @@ uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef * } } -uint8_t USBD_HID_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_HID_ItfTypeDef *fops) { - if (fops == NULL) { - return USBD_FAIL; - } else { - HID_fops = fops; - return USBD_OK; - } -} - uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) { HID_ClassData.RxBuffer = pbuff; return USBD_OK; From e04b4780501f14c7452bc10e948e4c06f17af992 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 22:57:20 +1000 Subject: [PATCH 053/163] stm32/usbdev: Simplify CDC tx/rx buffer passing. --- ports/stm32/usbd_cdc_interface.c | 22 +++++++-------- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 14 +++------- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 27 ++++++------------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 40c04061b..7c5fc2f39 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -56,7 +56,7 @@ #define CDC_SET_CONTROL_LINE_STATE 0x22 #define CDC_SEND_BREAK 0x23 -void usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) { +uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) { cdc->usb = pdev; cdc->rx_buf_put = 0; cdc->rx_buf_get = 0; @@ -66,8 +66,9 @@ void usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) { cdc->tx_buf_ptr_wait_count = 0; cdc->tx_need_empty_packet = 0; cdc->dev_is_connected = 0; - USBD_CDC_SetTxBuffer(pdev, cdc->tx_buf, 0); - USBD_CDC_SetRxBuffer(pdev, cdc->rx_packet_buf); + + // Return the buffer to place the first USB OUT packet + return cdc->rx_packet_buf; } // Manage the CDC class requests @@ -180,9 +181,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { buffptr = cdc->tx_buf_ptr_out_shadow; - USBD_CDC_SetTxBuffer(hpcd->pData, (uint8_t*)&cdc->tx_buf[buffptr], buffsize); - - if (USBD_CDC_TransmitPacket(hpcd->pData) == USBD_OK) { + if (USBD_CDC_TransmitPacket(hpcd->pData, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) { cdc->tx_buf_ptr_out_shadow += buffsize; if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) { cdc->tx_buf_ptr_out_shadow = 0; @@ -201,13 +200,11 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { } // Data received over USB OUT endpoint is processed here. -// Buf: buffer of data received -// Len: number of data received (in bytes) +// len: number of bytes received into the buffer we passed to USBD_CDC_ReceivePacket // Returns USBD_OK if all operations are OK else USBD_FAIL -// The buffer we are passed here is just cdc_rx_packet_buf, so we are free to modify it. -int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len) { +int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, size_t len) { // copy the incoming data into the circular buffer - for (uint8_t *src = Buf, *top = Buf + *Len; src < top; ++src) { + for (const uint8_t *src = cdc->rx_packet_buf, *top = cdc->rx_packet_buf + len; src < top; ++src) { if (mp_interrupt_char != -1 && *src == mp_interrupt_char) { pendsv_kbd_intr(); } else { @@ -222,8 +219,7 @@ int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len) { } // initiate next USB packet transfer - USBD_CDC_SetRxBuffer(cdc->usb, cdc->rx_packet_buf); - USBD_CDC_ReceivePacket(cdc->usb); + USBD_CDC_ReceivePacket(cdc->usb, cdc->rx_packet_buf); return USBD_OK; } diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index c7f9b1560..a7f8d0a9b 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -31,10 +31,6 @@ typedef struct { uint32_t data[CDC_DATA_FS_MAX_PACKET_SIZE/4]; /* Force 32bits alignment */ uint8_t CmdOpCode; uint8_t CmdLength; - uint8_t *RxBuffer; - uint8_t *TxBuffer; - uint32_t RxLength; - uint32_t TxLength; __IO uint32_t TxState; __IO uint32_t RxState; @@ -97,10 +93,8 @@ int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info); // returns the current usb mode uint8_t USBD_GetMode(); -uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length); -uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff); -uint8_t USBD_CDC_ReceivePacket (USBD_HandleTypeDef *pdev); -uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef *pdev); +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf); +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf); uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops); @@ -113,9 +107,9 @@ uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev); // These are provided externally to implement the CDC interface struct _usbd_cdc_itf_t; -void usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev); +uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev); int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length); -int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len); +int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, size_t len); // These are provided externally to implement the HID interface struct _usbd_hid_itf_t; diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index d2daa3aee..f7c082463 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -669,14 +669,14 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { CDC_CMD_PACKET_SIZE); // Init physical Interface components - usbd_cdc_init(state->cdc, pdev); + uint8_t *buf = usbd_cdc_init(state->cdc, pdev); // Init Xfer states CDC_ClassData.TxState =0; CDC_ClassData.RxState =0; // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, CDC_ClassData.RxBuffer, CDC_DATA_OUT_PACKET_SIZE); + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE); } if (usbd_mode & USBD_MODE_MSC) { @@ -962,11 +962,11 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) usbd_cdc_msc_hid_state_t *state = pdev->pClassData; if ((usbd_mode & USBD_MODE_CDC) && epnum == (CDC_OUT_EP & 0x7f)) { /* Get the received data length */ - CDC_ClassData.RxLength = USBD_LL_GetRxDataSize (pdev, epnum); + size_t len = USBD_LL_GetRxDataSize (pdev, epnum); /* USB data will be immediately processed, this allow next USB traffic being NAKed till the end of the application Xfer */ - usbd_cdc_receive(state->cdc, CDC_ClassData.RxBuffer, &CDC_ClassData.RxLength); + usbd_cdc_receive(state->cdc, len); return USBD_OK; } else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_OUT_EP & 0x7f)) { @@ -995,22 +995,11 @@ uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) { return NULL; } -uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length) { - CDC_ClassData.TxBuffer = pbuff; - CDC_ClassData.TxLength = length; - return USBD_OK; -} - -uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) { - CDC_ClassData.RxBuffer = pbuff; - return USBD_OK; -} - // data received on non-control OUT endpoint -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) { +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf) { if (CDC_ClassData.TxState == 0) { // transmit next packet - USBD_LL_Transmit(pdev, CDC_IN_EP, CDC_ClassData.TxBuffer, CDC_ClassData.TxLength); + USBD_LL_Transmit(pdev, CDC_IN_EP, (uint8_t*)buf, len); // Tx transfer in progress CDC_ClassData.TxState = 1; @@ -1021,14 +1010,14 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) { } // prepare OUT endpoint for reception -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) { +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) { // Suspend or Resume USB Out process if (pdev->dev_speed == USBD_SPEED_HIGH) { return USBD_FAIL; } // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, CDC_ClassData.RxBuffer, CDC_DATA_OUT_PACKET_SIZE); + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE); return USBD_OK; } From b3b922f177ee7553b2d06edf7bf3df6c2076b03a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 23:03:01 +1000 Subject: [PATCH 054/163] stm32/usbdev: Simplify HID tx/rx buffer passing. --- ports/stm32/usbd_hid_interface.c | 15 +++++++-------- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 7 +++---- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 19 ++++++------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index badf0144b..f14bcf52c 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -42,25 +42,24 @@ #include "irq.h" #include "usb.h" -void usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { +uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { hid->usb = pdev; hid->current_read_buffer = 0; hid->last_read_len = 0; hid->current_write_buffer = 0; - USBD_HID_SetRxBuffer(pdev, hid->buffer[hid->current_write_buffer]); + + // Return the buffer to place the first USB OUT packet + return hid->buffer[hid->current_write_buffer]; } // Data received over USB OUT endpoint is processed here. -// buf: Buffer of data received -// len: Number of data received (in bytes) +// len: number of bytes received into the buffer we passed to USBD_HID_ReceivePacket // Returns USBD_OK if all operations are OK else USBD_FAIL -// The buffer we are passed here is just hid->buffer, so we are free to modify it. -int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len, uint8_t *buf) { +int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len) { hid->current_write_buffer = !hid->current_write_buffer; hid->last_read_len = len; // initiate next USB packet transfer, to append to existing data in buffer - USBD_HID_SetRxBuffer(hid->usb, hid->buffer[hid->current_write_buffer]); - USBD_HID_ReceivePacket(hid->usb); + USBD_HID_ReceivePacket(hid->usb, hid->buffer[hid->current_write_buffer]); // Set NAK to indicate we need to process read buffer USBD_HID_SetNAK(hid->usb); return USBD_OK; diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index a7f8d0a9b..2fb19bce9 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -98,8 +98,7 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops); -uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev); +uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf); int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev); uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len); uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev); @@ -113,7 +112,7 @@ int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, size_t len); // These are provided externally to implement the HID interface struct _usbd_hid_itf_t; -void usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); -int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len, uint8_t *buf); +uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); +int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len); #endif // _USB_CDC_MSC_CORE_H_ diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index f7c082463..41cd2a8cb 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -82,8 +82,6 @@ typedef struct { uint32_t IdleState; uint32_t AltSetting; HID_StateTypeDef state; - uint8_t *RxBuffer; - uint32_t RxLength; } USBD_HID_HandleTypeDef; static uint8_t usbd_mode; @@ -724,10 +722,10 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_INTR, mps_out); - usbd_hid_init(state->hid, pdev); + uint8_t *buf = usbd_hid_init(state->hid, pdev); // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out); + USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); HID_ClassData.state = HID_IDLE; } @@ -973,8 +971,8 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) MSC_BOT_DataOut(pdev, epnum); return USBD_OK; } else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_out_ep & 0x7f)) { - HID_ClassData.RxLength = USBD_LL_GetRxDataSize(pdev, epnum); - usbd_hid_receive(state->hid, HID_ClassData.RxLength, HID_ClassData.RxBuffer); + size_t len = USBD_LL_GetRxDataSize(pdev, epnum); + usbd_hid_receive(state->hid, len); } return USBD_OK; @@ -1032,13 +1030,8 @@ uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef * } } -uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) { - HID_ClassData.RxBuffer = pbuff; - return USBD_OK; -} - // prepare OUT endpoint for reception -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) { +uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) { // Suspend or Resume USB Out process if (pdev->dev_speed == USBD_SPEED_HIGH) { return USBD_FAIL; @@ -1048,7 +1041,7 @@ uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) { uint16_t mps_out = hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); - USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out); + USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); return USBD_OK; } From 35e3435f6e2ca7494249e781a4fb99bfdf0a039a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Sep 2017 17:33:57 +1000 Subject: [PATCH 055/163] stm32/usbdev/core: Add state parameter to all callback functions. --- ports/stm32/usbdev/core/inc/usbd_def.h | 8 ++++---- ports/stm32/usbdev/core/src/usbd_ctlreq.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/stm32/usbdev/core/inc/usbd_def.h b/ports/stm32/usbdev/core/inc/usbd_def.h index 06b57dfd4..bd913a286 100644 --- a/ports/stm32/usbdev/core/inc/usbd_def.h +++ b/ports/stm32/usbdev/core/inc/usbd_def.h @@ -168,10 +168,10 @@ typedef struct _Device_cb uint8_t (*IsoINIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); uint8_t (*IsoOUTIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); - uint8_t *(*GetHSConfigDescriptor)(uint16_t *length); - uint8_t *(*GetFSConfigDescriptor)(uint16_t *length); - uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length); - uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length); + uint8_t *(*GetHSConfigDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetFSConfigDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetOtherSpeedConfigDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetDeviceQualifierDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); #if (USBD_SUPPORT_USER_STRING == 1) uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length); #endif diff --git a/ports/stm32/usbdev/core/src/usbd_ctlreq.c b/ports/stm32/usbdev/core/src/usbd_ctlreq.c index d744725b2..15d9b6ec4 100644 --- a/ports/stm32/usbdev/core/src/usbd_ctlreq.c +++ b/ports/stm32/usbdev/core/src/usbd_ctlreq.c @@ -336,12 +336,12 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , case USB_DESC_TYPE_CONFIGURATION: if(pdev->dev_speed == USBD_SPEED_HIGH ) { - pbuf = (uint8_t *)pdev->pClass->GetHSConfigDescriptor(&len); + pbuf = (uint8_t *)pdev->pClass->GetHSConfigDescriptor(pdev, &len); pbuf[1] = USB_DESC_TYPE_CONFIGURATION; } else { - pbuf = (uint8_t *)pdev->pClass->GetFSConfigDescriptor(&len); + pbuf = (uint8_t *)pdev->pClass->GetFSConfigDescriptor(pdev, &len); pbuf[1] = USB_DESC_TYPE_CONFIGURATION; } break; @@ -387,7 +387,7 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , if(pdev->dev_speed == USBD_SPEED_HIGH ) { - pbuf = (uint8_t *)pdev->pClass->GetDeviceQualifierDescriptor(&len); + pbuf = (uint8_t *)pdev->pClass->GetDeviceQualifierDescriptor(pdev, &len); break; } else @@ -399,7 +399,7 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION: if(pdev->dev_speed == USBD_SPEED_HIGH ) { - pbuf = (uint8_t *)pdev->pClass->GetOtherSpeedConfigDescriptor(&len); + pbuf = (uint8_t *)pdev->pClass->GetOtherSpeedConfigDescriptor(pdev, &len); pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION; break; } From f8f17f48c5742714690907e768f98369d8657623 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Sep 2017 17:34:45 +1000 Subject: [PATCH 056/163] stm32/usbdev: Put all state for the USB device driver in a struct. --- ports/stm32/usb.c | 13 +- ports/stm32/usbd_cdc_interface.c | 8 +- ports/stm32/usbd_cdc_interface.h | 2 +- ports/stm32/usbd_hid_interface.c | 10 +- ports/stm32/usbd_hid_interface.h | 2 +- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 60 +++- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 262 ++++++++---------- 7 files changed, 183 insertions(+), 174 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index bbb7310b8..e7cb3629d 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -111,7 +111,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H // configure the VID, PID and the USBD mode (interfaces it will expose) USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC); - if (USBD_SelectMode(mode, hid_info) != 0) { + if (USBD_SelectMode(&usbd_cdc_msc_hid_state, mode, hid_info) != 0) { return false; } @@ -121,6 +121,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H usbd->dev_state = USBD_STATE_DEFAULT; usbd->pDesc = (USBD_DescriptorsTypeDef*)&USBD_Descriptors; usbd->pClass = &USBD_CDC_MSC_HID; + usbd_cdc_msc_hid_state.pdev = usbd; usbd_cdc_msc_hid_state.cdc = &usbd_cdc_itf; usbd_cdc_msc_hid_state.hid = &usbd_hid_itf; usbd->pClassData = &usbd_cdc_msc_hid_state; @@ -128,11 +129,11 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H switch (pyb_usb_storage_medium) { #if MICROPY_HW_HAS_SDCARD case PYB_USB_STORAGE_MEDIUM_SDCARD: - USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); + USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); break; #endif default: - USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); + USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); break; } @@ -225,7 +226,7 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t * #if defined(USE_HOST_MODE) return MP_OBJ_NEW_QSTR(MP_QSTR_host); #elif defined(USE_DEVICE_MODE) - uint8_t mode = USBD_GetMode(); + uint8_t mode = USBD_GetMode(&usbd_cdc_msc_hid_state); switch (mode) { case USBD_MODE_CDC: return MP_OBJ_NEW_QSTR(MP_QSTR_VCP); @@ -614,7 +615,7 @@ STATIC mp_obj_t pyb_usb_hid_send(mp_obj_t self_in, mp_obj_t report_in) { } // send the data - if (USBD_OK == USBD_HID_SendReport(&hUSBDDevice, bufinfo.buf, bufinfo.len)) { + if (USBD_OK == USBD_HID_SendReport(&usbd_cdc_msc_hid_state, bufinfo.buf, bufinfo.len)) { return mp_obj_new_int(bufinfo.len); } else { return mp_obj_new_int(0); @@ -646,7 +647,7 @@ STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_ if ((flags & MP_STREAM_POLL_RD) && usbd_hid_rx_num(&usbd_hid_itf) > 0) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) { + if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&usbd_cdc_msc_hid_state)) { ret |= MP_STREAM_POLL_WR; } } else { diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 7c5fc2f39..4c49c9321 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -56,8 +56,8 @@ #define CDC_SET_CONTROL_LINE_STATE 0x22 #define CDC_SEND_BREAK 0x23 -uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) { - cdc->usb = pdev; +uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, usbd_cdc_msc_hid_state_t *usbd) { + cdc->usbd = usbd; cdc->rx_buf_put = 0; cdc->rx_buf_get = 0; cdc->tx_buf_ptr_in = 0; @@ -181,7 +181,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { buffptr = cdc->tx_buf_ptr_out_shadow; - if (USBD_CDC_TransmitPacket(hpcd->pData, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) { + if (USBD_CDC_TransmitPacket(cdc->usbd, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) { cdc->tx_buf_ptr_out_shadow += buffsize; if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) { cdc->tx_buf_ptr_out_shadow = 0; @@ -219,7 +219,7 @@ int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, size_t len) { } // initiate next USB packet transfer - USBD_CDC_ReceivePacket(cdc->usb, cdc->rx_packet_buf); + USBD_CDC_ReceivePacket(cdc->usbd, cdc->rx_packet_buf); return USBD_OK; } diff --git a/ports/stm32/usbd_cdc_interface.h b/ports/stm32/usbd_cdc_interface.h index dde4fa3e4..6dcab7d47 100644 --- a/ports/stm32/usbd_cdc_interface.h +++ b/ports/stm32/usbd_cdc_interface.h @@ -35,7 +35,7 @@ #define USBD_CDC_TX_DATA_SIZE (1024) // I think this can be any value (was 2048) typedef struct _usbd_cdc_itf_t { - USBD_HandleTypeDef *usb; // the parent USB device + usbd_cdc_msc_hid_state_t *usbd; // the parent USB device uint8_t rx_packet_buf[CDC_DATA_FS_MAX_PACKET_SIZE]; // received data from USB OUT endpoint is stored in this buffer uint8_t rx_user_buf[USBD_CDC_RX_DATA_SIZE]; // received data is buffered here until the user reads it diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index f14bcf52c..4ee533c21 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -42,8 +42,8 @@ #include "irq.h" #include "usb.h" -uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { - hid->usb = pdev; +uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, usbd_cdc_msc_hid_state_t *usbd) { + hid->usbd = usbd; hid->current_read_buffer = 0; hid->last_read_len = 0; hid->current_write_buffer = 0; @@ -59,9 +59,9 @@ int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len) { hid->current_write_buffer = !hid->current_write_buffer; hid->last_read_len = len; // initiate next USB packet transfer, to append to existing data in buffer - USBD_HID_ReceivePacket(hid->usb, hid->buffer[hid->current_write_buffer]); + USBD_HID_ReceivePacket(hid->usbd, hid->buffer[hid->current_write_buffer]); // Set NAK to indicate we need to process read buffer - USBD_HID_SetNAK(hid->usb); + USBD_HID_SetNAK(hid->usbd); return USBD_OK; } @@ -98,7 +98,7 @@ int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout) hid->current_read_buffer = !hid->current_read_buffer; // Clear NAK to indicate we are ready to read more data - USBD_HID_ClearNAK(hid->usb); + USBD_HID_ClearNAK(hid->usbd); // Success, return number of bytes read return hid->last_read_len; diff --git a/ports/stm32/usbd_hid_interface.h b/ports/stm32/usbd_hid_interface.h index 3dc87de6b..79040b57e 100644 --- a/ports/stm32/usbd_hid_interface.h +++ b/ports/stm32/usbd_hid_interface.h @@ -7,7 +7,7 @@ #include "usbd_cdc_msc_hid.h" typedef struct _usbd_hid_itf_t { - USBD_HandleTypeDef *usb; // the parent USB device + usbd_cdc_msc_hid_state_t *usbd; // the parent USB device uint8_t buffer[2][HID_DATA_FS_MAX_PACKET_SIZE]; // pair of buffers to read individual packets into int8_t current_read_buffer; // which buffer to read from diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index 2fb19bce9..a6c92b114 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -6,6 +6,10 @@ #include "usbd_msc_scsi.h" #include "usbd_ioreq.h" +// Needed for the CDC+MSC+HID state and should be maximum of all template +// config descriptors defined in usbd_cdc_msc_hid.c +#define MAX_TEMPLATE_CONFIG_DESC_SIZE (107) + // CDC, MSC and HID packet sizes #define CDC_DATA_FS_MAX_PACKET_SIZE (64) // endpoint IN & OUT packet size #define MSC_MEDIA_PACKET (2048) // was 8192; how low can it go whilst still working? @@ -70,7 +74,39 @@ typedef struct { uint32_t scsi_blk_len; } USBD_MSC_BOT_HandleTypeDef; +typedef enum { + HID_IDLE = 0, + HID_BUSY, +} HID_StateTypeDef; + +typedef struct { + uint32_t Protocol; + uint32_t IdleState; + uint32_t AltSetting; + HID_StateTypeDef state; +} USBD_HID_HandleTypeDef; + typedef struct _usbd_cdc_msc_hid_state_t { + USBD_HandleTypeDef *pdev; + + uint8_t usbd_mode; + uint8_t cdc_iface_num; + uint8_t hid_in_ep; + uint8_t hid_out_ep; + uint8_t hid_iface_num; + uint8_t usbd_config_desc_size; + uint8_t *hid_desc; + const uint8_t *hid_report_desc; + + USBD_StorageTypeDef *MSC_fops; + + USBD_CDC_HandleTypeDef CDC_ClassData; + USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; + USBD_HID_HandleTypeDef HID_ClassData; + + // RAM to hold the current configuration descriptor, which we configure on the fly + __ALIGN_BEGIN uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END; + void *cdc; void *msc; void *hid; @@ -89,30 +125,30 @@ extern const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPORT_DESC_ extern const USBD_ClassTypeDef USBD_CDC_MSC_HID; // returns 0 on success, -1 on failure -int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info); +int USBD_SelectMode(usbd_cdc_msc_hid_state_t *usbd, uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info); // returns the current usb mode -uint8_t USBD_GetMode(); +uint8_t USBD_GetMode(usbd_cdc_msc_hid_state_t *usbd); -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf); -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf); +uint8_t USBD_CDC_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf); +uint8_t USBD_CDC_TransmitPacket(usbd_cdc_msc_hid_state_t *usbd, size_t len, const uint8_t *buf); -uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops); +uint8_t USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops); -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf); -int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev); -uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len); -uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev); -uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev); +uint8_t USBD_HID_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf); +int USBD_HID_CanSendReport(usbd_cdc_msc_hid_state_t *usbd); +uint8_t USBD_HID_SendReport(usbd_cdc_msc_hid_state_t *usbd, uint8_t *report, uint16_t len); +uint8_t USBD_HID_SetNAK(usbd_cdc_msc_hid_state_t *usbd); +uint8_t USBD_HID_ClearNAK(usbd_cdc_msc_hid_state_t *usbd); // These are provided externally to implement the CDC interface struct _usbd_cdc_itf_t; -uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev); +uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, usbd_cdc_msc_hid_state_t *usbd); int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length); int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, size_t len); // These are provided externally to implement the HID interface struct _usbd_hid_itf_t; -uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); +uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, usbd_cdc_msc_hid_state_t *usbd); int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len); #endif // _USB_CDC_MSC_CORE_H_ diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index 41cd2a8cb..59eac15e2 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -27,7 +27,6 @@ #include "usbd_ioreq.h" #include "usbd_cdc_msc_hid.h" -#define MAX_TEMPLATE_CONFIG_DESC_SIZE (107) // should be maximum of all template config desc's #define CDC_TEMPLATE_CONFIG_DESC_SIZE (67) #define CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE (98) #define CDC_HID_TEMPLATE_CONFIG_DESC_SIZE (107) @@ -72,36 +71,6 @@ #define HID_REQ_SET_IDLE (0x0a) #define HID_REQ_GET_IDLE (0x02) -typedef enum { - HID_IDLE = 0, - HID_BUSY, -} HID_StateTypeDef; - -typedef struct { - uint32_t Protocol; - uint32_t IdleState; - uint32_t AltSetting; - HID_StateTypeDef state; -} USBD_HID_HandleTypeDef; - -static uint8_t usbd_mode; -static uint8_t cdc_iface_num; -static uint8_t hid_in_ep; -static uint8_t hid_out_ep; -static uint8_t hid_iface_num; -static uint8_t usbd_config_desc_size; -static uint8_t *hid_desc; -static const uint8_t *hid_report_desc; - -static USBD_StorageTypeDef *MSC_fops; - -static USBD_CDC_HandleTypeDef CDC_ClassData; -static USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; -static USBD_HID_HandleTypeDef HID_ClassData; - -// RAM to hold the current configuration descriptor, which we configure on the fly -__ALIGN_BEGIN static uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END; - /* // this is used only in high-speed mode, which we don't support // USB Standard Device Descriptor @@ -574,36 +543,36 @@ __ALIGN_BEGIN const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPOR }; // return the saved usb mode -uint8_t USBD_GetMode() { - return usbd_mode; +uint8_t USBD_GetMode(usbd_cdc_msc_hid_state_t *usbd) { + return usbd->usbd_mode; } -int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { +int USBD_SelectMode(usbd_cdc_msc_hid_state_t *usbd, uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { // save mode - usbd_mode = mode; + usbd->usbd_mode = mode; // construct config desc - switch (usbd_mode) { + switch (usbd->usbd_mode) { case USBD_MODE_CDC_MSC: - usbd_config_desc_size = sizeof(cdc_msc_template_config_desc); - memcpy(usbd_config_desc, cdc_msc_template_config_desc, sizeof(cdc_msc_template_config_desc)); - cdc_iface_num = CDC_IFACE_NUM_WITH_MSC; + usbd->usbd_config_desc_size = sizeof(cdc_msc_template_config_desc); + memcpy(usbd->usbd_config_desc, cdc_msc_template_config_desc, sizeof(cdc_msc_template_config_desc)); + usbd->cdc_iface_num = CDC_IFACE_NUM_WITH_MSC; break; case USBD_MODE_CDC_HID: - usbd_config_desc_size = sizeof(cdc_hid_template_config_desc); - memcpy(usbd_config_desc, cdc_hid_template_config_desc, sizeof(cdc_hid_template_config_desc)); - cdc_iface_num = CDC_IFACE_NUM_WITH_HID; - hid_in_ep = HID_IN_EP_WITH_CDC; - hid_out_ep = HID_OUT_EP_WITH_CDC; - hid_iface_num = HID_IFACE_NUM_WITH_CDC; - hid_desc = usbd_config_desc + CDC_HID_TEMPLATE_HID_DESC_OFFSET; + usbd->usbd_config_desc_size = sizeof(cdc_hid_template_config_desc); + memcpy(usbd->usbd_config_desc, cdc_hid_template_config_desc, sizeof(cdc_hid_template_config_desc)); + usbd->cdc_iface_num = CDC_IFACE_NUM_WITH_HID; + usbd->hid_in_ep = HID_IN_EP_WITH_CDC; + usbd->hid_out_ep = HID_OUT_EP_WITH_CDC; + usbd->hid_iface_num = HID_IFACE_NUM_WITH_CDC; + usbd->hid_desc = usbd->usbd_config_desc + CDC_HID_TEMPLATE_HID_DESC_OFFSET; break; case USBD_MODE_CDC: - usbd_config_desc_size = sizeof(cdc_template_config_desc); - memcpy(usbd_config_desc, cdc_template_config_desc, sizeof(cdc_template_config_desc)); - cdc_iface_num = CDC_IFACE_NUM_ALONE; + usbd->usbd_config_desc_size = sizeof(cdc_template_config_desc); + memcpy(usbd->usbd_config_desc, cdc_template_config_desc, sizeof(cdc_template_config_desc)); + usbd->cdc_iface_num = CDC_IFACE_NUM_ALONE; break; /* @@ -621,7 +590,8 @@ int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { } // configure the HID descriptor, if needed - if (usbd_mode & USBD_MODE_HID) { + if (usbd->usbd_mode & USBD_MODE_HID) { + uint8_t *hid_desc = usbd->hid_desc; hid_desc[HID_DESC_OFFSET_SUBCLASS] = hid_info->subclass; hid_desc[HID_DESC_OFFSET_PROTOCOL] = hid_info->protocol; hid_desc[HID_DESC_OFFSET_REPORT_DESC_LEN] = hid_info->report_desc_len; @@ -631,7 +601,7 @@ int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] = hid_info->max_packet_len; hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] = 0; hid_desc[HID_DESC_OFFSET_POLLING_INTERVAL_OUT] = hid_info->polling_interval; - hid_report_desc = hid_info->report_desc; + usbd->hid_report_desc = hid_info->report_desc; } return 0; @@ -643,9 +613,9 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { return 1; } - usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; - if (usbd_mode & USBD_MODE_CDC) { + if (usbd->usbd_mode & USBD_MODE_CDC) { // CDC VCP component // Open EP IN @@ -667,17 +637,17 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { CDC_CMD_PACKET_SIZE); // Init physical Interface components - uint8_t *buf = usbd_cdc_init(state->cdc, pdev); + uint8_t *buf = usbd_cdc_init(usbd->cdc, usbd); // Init Xfer states - CDC_ClassData.TxState =0; - CDC_ClassData.RxState =0; + usbd->CDC_ClassData.TxState = 0; + usbd->CDC_ClassData.RxState = 0; // Prepare Out endpoint to receive next packet USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE); } - if (usbd_mode & USBD_MODE_MSC) { + if (usbd->usbd_mode & USBD_MODE_MSC) { // MSC component // Open EP OUT @@ -693,50 +663,50 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { MSC_MAX_PACKET); // Set the MSC data for SCSI and BOT to reference it - state->msc = &MSC_BOT_ClassData; + usbd->msc = &usbd->MSC_BOT_ClassData; // Init the BOT layer MSC_BOT_Init(pdev); } - if (usbd_mode & USBD_MODE_HID) { + if (usbd->usbd_mode & USBD_MODE_HID) { // HID component // get max packet lengths from descriptor uint16_t mps_in = - hid_desc[HID_DESC_OFFSET_MAX_PACKET_LO] - | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_HI] << 8); + usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_LO] + | (usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_HI] << 8); uint16_t mps_out = - hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] - | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); + usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] + | (usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); // Open EP IN USBD_LL_OpenEP(pdev, - hid_in_ep, + usbd->hid_in_ep, USBD_EP_TYPE_INTR, mps_in); // Open EP OUT USBD_LL_OpenEP(pdev, - hid_out_ep, + usbd->hid_out_ep, USBD_EP_TYPE_INTR, mps_out); - uint8_t *buf = usbd_hid_init(state->hid, pdev); + uint8_t *buf = usbd_hid_init(usbd->hid, usbd); // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); + USBD_LL_PrepareReceive(pdev, usbd->hid_out_ep, buf, mps_out); - HID_ClassData.state = HID_IDLE; + usbd->HID_ClassData.state = HID_IDLE; } return 0; } static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { - usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; - if ((usbd_mode & USBD_MODE_CDC) && state->cdc) { + if ((usbd->usbd_mode & USBD_MODE_CDC) && usbd->cdc) { // CDC VCP component // close endpoints @@ -745,7 +715,7 @@ static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) USBD_LL_CloseEP(pdev, CDC_CMD_EP); } - if ((usbd_mode & USBD_MODE_MSC) && state->msc) { + if ((usbd->usbd_mode & USBD_MODE_MSC) && usbd->msc) { // MSC component // close endpoints @@ -756,15 +726,15 @@ static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) MSC_BOT_DeInit(pdev); // clear the state pointer - state->msc = NULL; + usbd->msc = NULL; } - if (usbd_mode & USBD_MODE_HID) { + if (usbd->usbd_mode & USBD_MODE_HID) { // HID component // close endpoints - USBD_LL_CloseEP(pdev, hid_in_ep); - USBD_LL_CloseEP(pdev, hid_out_ep); + USBD_LL_CloseEP(pdev, usbd->hid_in_ep); + USBD_LL_CloseEP(pdev, usbd->hid_out_ep); } return 0; @@ -788,38 +758,38 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp SU: 21 20 0 1 */ - usbd_cdc_msc_hid_state_t *state = pdev->pClassData; + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; switch (req->bmRequest & USB_REQ_TYPE_MASK) { // Class request case USB_REQ_TYPE_CLASS: // req->wIndex is the recipient interface number - if ((usbd_mode & USBD_MODE_CDC) && req->wIndex == cdc_iface_num) { + if ((usbd->usbd_mode & USBD_MODE_CDC) && req->wIndex == usbd->cdc_iface_num) { // CDC component if (req->wLength) { if (req->bmRequest & 0x80) { // device-to-host request - usbd_cdc_control(state->cdc, req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength); - USBD_CtlSendData(pdev, (uint8_t*)CDC_ClassData.data, req->wLength); + usbd_cdc_control(usbd->cdc, req->bRequest, (uint8_t*)usbd->CDC_ClassData.data, req->wLength); + USBD_CtlSendData(pdev, (uint8_t*)usbd->CDC_ClassData.data, req->wLength); } else { // host-to-device request - CDC_ClassData.CmdOpCode = req->bRequest; - CDC_ClassData.CmdLength = req->wLength; - USBD_CtlPrepareRx(pdev, (uint8_t*)CDC_ClassData.data, req->wLength); + usbd->CDC_ClassData.CmdOpCode = req->bRequest; + usbd->CDC_ClassData.CmdLength = req->wLength; + USBD_CtlPrepareRx(pdev, (uint8_t*)usbd->CDC_ClassData.data, req->wLength); } } else { // Not a Data request // Transfer the command to the interface layer - return usbd_cdc_control(state->cdc, req->bRequest, NULL, req->wValue); + return usbd_cdc_control(usbd->cdc, req->bRequest, NULL, req->wValue); } - } else if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { + } else if ((usbd->usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { // MSC component switch (req->bRequest) { case BOT_GET_MAX_LUN: if ((req->wValue == 0) && (req->wLength == 1) && ((req->bmRequest & 0x80) == 0x80)) { - MSC_BOT_ClassData.max_lun = MSC_fops->GetMaxLun(); - USBD_CtlSendData(pdev, (uint8_t *)&MSC_BOT_ClassData.max_lun, 1); + usbd->MSC_BOT_ClassData.max_lun = usbd->MSC_fops->GetMaxLun(); + USBD_CtlSendData(pdev, (uint8_t *)&usbd->MSC_BOT_ClassData.max_lun, 1); } else { USBD_CtlError(pdev, req); return USBD_FAIL; @@ -839,22 +809,22 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp USBD_CtlError(pdev, req); return USBD_FAIL; } - } else if ((usbd_mode & USBD_MODE_HID) && req->wIndex == hid_iface_num) { + } else if ((usbd->usbd_mode & USBD_MODE_HID) && req->wIndex == usbd->hid_iface_num) { switch (req->bRequest) { case HID_REQ_SET_PROTOCOL: - HID_ClassData.Protocol = (uint8_t)(req->wValue); + usbd->HID_ClassData.Protocol = (uint8_t)(req->wValue); break; case HID_REQ_GET_PROTOCOL: - USBD_CtlSendData (pdev, (uint8_t *)&HID_ClassData.Protocol, 1); + USBD_CtlSendData(pdev, (uint8_t *)&usbd->HID_ClassData.Protocol, 1); break; case HID_REQ_SET_IDLE: - HID_ClassData.IdleState = (uint8_t)(req->wValue >> 8); + usbd->HID_ClassData.IdleState = (uint8_t)(req->wValue >> 8); break; case HID_REQ_GET_IDLE: - USBD_CtlSendData (pdev, (uint8_t *)&HID_ClassData.IdleState, 1); + USBD_CtlSendData(pdev, (uint8_t *)&usbd->HID_ClassData.IdleState, 1); break; default: @@ -866,14 +836,14 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp // Interface & Endpoint request case USB_REQ_TYPE_STANDARD: - if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { + if ((usbd->usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { switch (req->bRequest) { case USB_REQ_GET_INTERFACE : - USBD_CtlSendData(pdev, (uint8_t *)&MSC_BOT_ClassData.interface, 1); + USBD_CtlSendData(pdev, (uint8_t *)&usbd->MSC_BOT_ClassData.interface, 1); break; case USB_REQ_SET_INTERFACE : - MSC_BOT_ClassData.interface = (uint8_t)(req->wValue); + usbd->MSC_BOT_ClassData.interface = (uint8_t)(req->wValue); break; case USB_REQ_CLEAR_FEATURE: @@ -893,29 +863,29 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex); break; } - } else if ((usbd_mode & USBD_MODE_HID) && req->wIndex == hid_iface_num) { + } else if ((usbd->usbd_mode & USBD_MODE_HID) && req->wIndex == usbd->hid_iface_num) { switch (req->bRequest) { case USB_REQ_GET_DESCRIPTOR: { uint16_t len = 0; const uint8_t *pbuf = NULL; if (req->wValue >> 8 == HID_REPORT_DESC) { - len = hid_desc[HID_DESC_OFFSET_REPORT_DESC_LEN]; + len = usbd->hid_desc[HID_DESC_OFFSET_REPORT_DESC_LEN]; len = MIN(len, req->wLength); - pbuf = hid_report_desc; + pbuf = usbd->hid_report_desc; } else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { len = MIN(HID_SUBDESC_LEN, req->wLength); - pbuf = hid_desc + HID_DESC_OFFSET_SUBDESC; + pbuf = usbd->hid_desc + HID_DESC_OFFSET_SUBDESC; } USBD_CtlSendData(pdev, (uint8_t*)pbuf, len); break; } case USB_REQ_GET_INTERFACE: - USBD_CtlSendData (pdev, (uint8_t *)&HID_ClassData.AltSetting, 1); + USBD_CtlSendData(pdev, (uint8_t *)&usbd->HID_ClassData.AltSetting, 1); break; case USB_REQ_SET_INTERFACE: - HID_ClassData.AltSetting = (uint8_t)(req->wValue); + usbd->HID_ClassData.AltSetting = (uint8_t)(req->wValue); break; } } @@ -930,26 +900,27 @@ static uint8_t EP0_TxSent(USBD_HandleTypeDef *pdev) { */ static uint8_t USBD_CDC_MSC_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) { - usbd_cdc_msc_hid_state_t *state = pdev->pClassData; - if (state->cdc != NULL && CDC_ClassData.CmdOpCode != 0xff) { - usbd_cdc_control(state->cdc, CDC_ClassData.CmdOpCode, (uint8_t*)CDC_ClassData.data, CDC_ClassData.CmdLength); - CDC_ClassData.CmdOpCode = 0xff; + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; + if (usbd->cdc != NULL && usbd->CDC_ClassData.CmdOpCode != 0xff) { + usbd_cdc_control(usbd->cdc, usbd->CDC_ClassData.CmdOpCode, (uint8_t*)usbd->CDC_ClassData.data, usbd->CDC_ClassData.CmdLength); + usbd->CDC_ClassData.CmdOpCode = 0xff; } return USBD_OK; } static uint8_t USBD_CDC_MSC_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) { - if ((usbd_mode & USBD_MODE_CDC) && (epnum == (CDC_IN_EP & 0x7f) || epnum == (CDC_CMD_EP & 0x7f))) { - CDC_ClassData.TxState = 0; + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; + if ((usbd->usbd_mode & USBD_MODE_CDC) && (epnum == (CDC_IN_EP & 0x7f) || epnum == (CDC_CMD_EP & 0x7f))) { + usbd->CDC_ClassData.TxState = 0; return USBD_OK; - } else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_IN_EP & 0x7f)) { + } else if ((usbd->usbd_mode & USBD_MODE_MSC) && epnum == (MSC_IN_EP & 0x7f)) { MSC_BOT_DataIn(pdev, epnum); return USBD_OK; - } else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_in_ep & 0x7f)) { + } else if ((usbd->usbd_mode & USBD_MODE_HID) && epnum == (usbd->hid_in_ep & 0x7f)) { /* Ensure that the FIFO is empty before a new transfer, this condition could be caused by a new transfer before the end of the previous transfer */ - HID_ClassData.state = HID_IDLE; + usbd->HID_ClassData.state = HID_IDLE; return USBD_OK; } @@ -957,34 +928,35 @@ static uint8_t USBD_CDC_MSC_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) } static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) { - usbd_cdc_msc_hid_state_t *state = pdev->pClassData; - if ((usbd_mode & USBD_MODE_CDC) && epnum == (CDC_OUT_EP & 0x7f)) { + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; + if ((usbd->usbd_mode & USBD_MODE_CDC) && epnum == (CDC_OUT_EP & 0x7f)) { /* Get the received data length */ size_t len = USBD_LL_GetRxDataSize (pdev, epnum); /* USB data will be immediately processed, this allow next USB traffic being NAKed till the end of the application Xfer */ - usbd_cdc_receive(state->cdc, len); + usbd_cdc_receive(usbd->cdc, len); return USBD_OK; - } else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_OUT_EP & 0x7f)) { + } else if ((usbd->usbd_mode & USBD_MODE_MSC) && epnum == (MSC_OUT_EP & 0x7f)) { MSC_BOT_DataOut(pdev, epnum); return USBD_OK; - } else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_out_ep & 0x7f)) { + } else if ((usbd->usbd_mode & USBD_MODE_HID) && epnum == (usbd->hid_out_ep & 0x7f)) { size_t len = USBD_LL_GetRxDataSize(pdev, epnum); - usbd_hid_receive(state->hid, len); + usbd_hid_receive(usbd->hid, len); } return USBD_OK; } -static uint8_t *USBD_CDC_MSC_HID_GetCfgDesc(uint16_t *length) { - *length = usbd_config_desc_size; - return usbd_config_desc; +static uint8_t *USBD_CDC_MSC_HID_GetCfgDesc(USBD_HandleTypeDef *pdev, uint16_t *length) { + usbd_cdc_msc_hid_state_t *usbd = pdev->pClassData; + *length = usbd->usbd_config_desc_size; + return usbd->usbd_config_desc; } // this is used only in high-speed mode, which we don't support -uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) { +uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { /* *length = sizeof(USBD_CDC_MSC_HID_DeviceQualifierDesc); return USBD_CDC_MSC_HID_DeviceQualifierDesc; @@ -994,13 +966,13 @@ uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) { } // data received on non-control OUT endpoint -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf) { - if (CDC_ClassData.TxState == 0) { +uint8_t USBD_CDC_TransmitPacket(usbd_cdc_msc_hid_state_t *usbd, size_t len, const uint8_t *buf) { + if (usbd->CDC_ClassData.TxState == 0) { // transmit next packet - USBD_LL_Transmit(pdev, CDC_IN_EP, (uint8_t*)buf, len); + USBD_LL_Transmit(usbd->pdev, CDC_IN_EP, (uint8_t*)buf, len); // Tx transfer in progress - CDC_ClassData.TxState = 1; + usbd->CDC_ClassData.TxState = 1; return USBD_OK; } else { return USBD_BUSY; @@ -1008,70 +980,70 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint } // prepare OUT endpoint for reception -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) { +uint8_t USBD_CDC_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf) { // Suspend or Resume USB Out process - if (pdev->dev_speed == USBD_SPEED_HIGH) { + if (usbd->pdev->dev_speed == USBD_SPEED_HIGH) { return USBD_FAIL; } // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE); + USBD_LL_PrepareReceive(usbd->pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE); return USBD_OK; } -uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops) { +uint8_t USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops) { if (fops == NULL) { return USBD_FAIL; } else { - MSC_fops = fops; - pdev->pUserData = fops; // MSC uses pUserData because SCSI and BOT reference it + usbd->MSC_fops = fops; + usbd->pdev->pUserData = fops; // MSC uses pUserData because SCSI and BOT reference it return USBD_OK; } } // prepare OUT endpoint for reception -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) { +uint8_t USBD_HID_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf) { // Suspend or Resume USB Out process - if (pdev->dev_speed == USBD_SPEED_HIGH) { + if (usbd->pdev->dev_speed == USBD_SPEED_HIGH) { return USBD_FAIL; } // Prepare Out endpoint to receive next packet uint16_t mps_out = - hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] - | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); - USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); + usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] + | (usbd->hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); + USBD_LL_PrepareReceive(usbd->pdev, usbd->hid_out_ep, buf, mps_out); return USBD_OK; } -int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev) { - return pdev->dev_state == USBD_STATE_CONFIGURED && HID_ClassData.state == HID_IDLE; +int USBD_HID_CanSendReport(usbd_cdc_msc_hid_state_t *usbd) { + return usbd->pdev->dev_state == USBD_STATE_CONFIGURED && usbd->HID_ClassData.state == HID_IDLE; } -uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len) { - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - if (HID_ClassData.state == HID_IDLE) { - HID_ClassData.state = HID_BUSY; - USBD_LL_Transmit(pdev, hid_in_ep, report, len); +uint8_t USBD_HID_SendReport(usbd_cdc_msc_hid_state_t *usbd, uint8_t *report, uint16_t len) { + if (usbd->pdev->dev_state == USBD_STATE_CONFIGURED) { + if (usbd->HID_ClassData.state == HID_IDLE) { + usbd->HID_ClassData.state = HID_BUSY; + USBD_LL_Transmit(usbd->pdev, usbd->hid_in_ep, report, len); } } return USBD_OK; } -uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev) { +uint8_t USBD_HID_SetNAK(usbd_cdc_msc_hid_state_t *usbd) { // get USBx object from pdev (needed for USBx_OUTEP macro below) - PCD_HandleTypeDef *hpcd = pdev->pData; + PCD_HandleTypeDef *hpcd = usbd->pdev->pData; USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; // set NAK on HID OUT endpoint USBx_OUTEP(HID_OUT_EP_WITH_CDC)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK; return USBD_OK; } -uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev) { +uint8_t USBD_HID_ClearNAK(usbd_cdc_msc_hid_state_t *usbd) { // get USBx object from pdev (needed for USBx_OUTEP macro below) - PCD_HandleTypeDef *hpcd = pdev->pData; + PCD_HandleTypeDef *hpcd = usbd->pdev->pData; USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; // clear NAK on HID OUT endpoint USBx_OUTEP(HID_OUT_EP_WITH_CDC)->DOEPCTL |= USB_OTG_DOEPCTL_CNAK; From 0ea73d2da76e45d01e76ad91a0e2103f2b317372 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Sep 2017 23:56:46 +1000 Subject: [PATCH 057/163] stm32/usbdev: Simplify pointers to MSC state and block dev operations. --- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 10 ++-- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 20 +------ ports/stm32/usbdev/class/src/usbd_msc_bot.c | 22 ++++---- ports/stm32/usbdev/class/src/usbd_msc_scsi.c | 56 +++++++++---------- 4 files changed, 47 insertions(+), 61 deletions(-) diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index a6c92b114..2283cbe85 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -72,6 +72,9 @@ typedef struct { uint32_t scsi_blk_addr_in_blks; uint32_t scsi_blk_len; + + // operations of the underlying block device + USBD_StorageTypeDef *bdev_ops; } USBD_MSC_BOT_HandleTypeDef; typedef enum { @@ -98,8 +101,6 @@ typedef struct _usbd_cdc_msc_hid_state_t { uint8_t *hid_desc; const uint8_t *hid_report_desc; - USBD_StorageTypeDef *MSC_fops; - USBD_CDC_HandleTypeDef CDC_ClassData; USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; USBD_HID_HandleTypeDef HID_ClassData; @@ -108,7 +109,6 @@ typedef struct _usbd_cdc_msc_hid_state_t { __ALIGN_BEGIN uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END; void *cdc; - void *msc; void *hid; } usbd_cdc_msc_hid_state_t; @@ -132,7 +132,9 @@ uint8_t USBD_GetMode(usbd_cdc_msc_hid_state_t *usbd); uint8_t USBD_CDC_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf); uint8_t USBD_CDC_TransmitPacket(usbd_cdc_msc_hid_state_t *usbd, size_t len, const uint8_t *buf); -uint8_t USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops); +static inline void USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops) { + usbd->MSC_BOT_ClassData.bdev_ops = fops; +} uint8_t USBD_HID_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf); int USBD_HID_CanSendReport(usbd_cdc_msc_hid_state_t *usbd); diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index 59eac15e2..379a8f32c 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -662,9 +662,6 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_BULK, MSC_MAX_PACKET); - // Set the MSC data for SCSI and BOT to reference it - usbd->msc = &usbd->MSC_BOT_ClassData; - // Init the BOT layer MSC_BOT_Init(pdev); } @@ -715,7 +712,7 @@ static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) USBD_LL_CloseEP(pdev, CDC_CMD_EP); } - if ((usbd->usbd_mode & USBD_MODE_MSC) && usbd->msc) { + if (usbd->usbd_mode & USBD_MODE_MSC) { // MSC component // close endpoints @@ -724,9 +721,6 @@ static uint8_t USBD_CDC_MSC_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) // DeInit the BOT layer MSC_BOT_DeInit(pdev); - - // clear the state pointer - usbd->msc = NULL; } if (usbd->usbd_mode & USBD_MODE_HID) { @@ -788,7 +782,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp switch (req->bRequest) { case BOT_GET_MAX_LUN: if ((req->wValue == 0) && (req->wLength == 1) && ((req->bmRequest & 0x80) == 0x80)) { - usbd->MSC_BOT_ClassData.max_lun = usbd->MSC_fops->GetMaxLun(); + usbd->MSC_BOT_ClassData.max_lun = usbd->MSC_BOT_ClassData.bdev_ops->GetMaxLun(); USBD_CtlSendData(pdev, (uint8_t *)&usbd->MSC_BOT_ClassData.max_lun, 1); } else { USBD_CtlError(pdev, req); @@ -992,16 +986,6 @@ uint8_t USBD_CDC_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf) { return USBD_OK; } -uint8_t USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops) { - if (fops == NULL) { - return USBD_FAIL; - } else { - usbd->MSC_fops = fops; - usbd->pdev->pUserData = fops; // MSC uses pUserData because SCSI and BOT reference it - return USBD_OK; - } -} - // prepare OUT endpoint for reception uint8_t USBD_HID_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf) { // Suspend or Resume USB Out process diff --git a/ports/stm32/usbdev/class/src/usbd_msc_bot.c b/ports/stm32/usbdev/class/src/usbd_msc_bot.c index 7c6ceeecf..2fccd9e08 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_bot.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_bot.c @@ -104,7 +104,7 @@ static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev); */ void MSC_BOT_Init (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_state = USBD_BOT_IDLE; hmsc->bot_status = USBD_BOT_STATUS_NORMAL; @@ -112,7 +112,7 @@ void MSC_BOT_Init (USBD_HandleTypeDef *pdev) hmsc->scsi_sense_tail = 0; hmsc->scsi_sense_head = 0; - ((USBD_StorageTypeDef *)pdev->pUserData)->Init(0); + hmsc->bdev_ops->Init(0); USBD_LL_FlushEP(pdev, MSC_OUT_EP); USBD_LL_FlushEP(pdev, MSC_IN_EP); @@ -132,7 +132,7 @@ void MSC_BOT_Init (USBD_HandleTypeDef *pdev) */ void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_state = USBD_BOT_IDLE; hmsc->bot_status = USBD_BOT_STATUS_RECOVERY; @@ -152,7 +152,7 @@ void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) */ void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_state = USBD_BOT_IDLE; } @@ -166,7 +166,7 @@ void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; switch (hmsc->bot_state) { @@ -199,7 +199,7 @@ void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; switch (hmsc->bot_state) { @@ -231,7 +231,7 @@ void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, */ static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->csw.dTag = hmsc->cbw.dTag; hmsc->csw.dDataResidue = hmsc->cbw.dDataLength; @@ -300,7 +300,7 @@ static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t* buf, uint16_t len) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; len = MIN (hmsc->cbw.dDataLength, len); hmsc->csw.dDataResidue -= len; @@ -320,7 +320,7 @@ static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, uint8_t CSW_Status) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->csw.dSignature = USBD_BOT_CSW_SIGNATURE; hmsc->csw.bStatus = CSW_Status; @@ -348,7 +348,7 @@ void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if ((hmsc->cbw.bmFlags == 0) && (hmsc->cbw.dDataLength != 0) && @@ -377,7 +377,7 @@ static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, uint8_t epnum) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if(hmsc->bot_status == USBD_BOT_STATUS_ERROR )/* Bad CBW Signature */ { diff --git a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c index 0664883ad..50cd5b971 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c @@ -190,7 +190,7 @@ int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, */ static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; /* case 9 : Hi > D0 */ if (hmsc->cbw.dDataLength != 0) @@ -202,7 +202,7 @@ static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t return -1; } - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) !=0 ) + if(hmsc->bdev_ops->IsReady(lun) !=0 ) { SCSI_SenseCode(pdev, lun, @@ -227,7 +227,7 @@ static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *par { uint8_t* pPage; uint16_t len; - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if (params[1] & 0x01)/*Evpd is set*/ { @@ -237,7 +237,7 @@ static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *par else { - pPage = (uint8_t *)&((USBD_StorageTypeDef *)pdev->pUserData)->pInquiry[lun * STANDARD_INQUIRY_DATA_LEN]; + pPage = (uint8_t *)&hmsc->bdev_ops->pInquiry[lun * STANDARD_INQUIRY_DATA_LEN]; len = pPage[4] + 5; if (params[4] <= len) @@ -264,9 +264,9 @@ static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *par */ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; - if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) + if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) { SCSI_SenseCode(pdev, lun, @@ -300,7 +300,7 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ */ static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; uint16_t blk_size; uint32_t blk_nbr; @@ -311,7 +311,7 @@ static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, ui hmsc->bot_data[i] = 0; } - if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &blk_nbr, &blk_size) != 0) + if(hmsc->bdev_ops->GetCapacity(lun, &blk_nbr, &blk_size) != 0) { SCSI_SenseCode(pdev, lun, @@ -345,7 +345,7 @@ static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, ui */ static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; uint16_t len = 8 ; hmsc->bot_data_length = len; @@ -367,7 +367,7 @@ static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t * static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { uint16_t len = 8; - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_data_length = len; @@ -381,7 +381,7 @@ static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t static int8_t SCSI_SynchronizeCache(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { // nothing to synchronize, so just return "success" - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_data_length = 0; return 0; } @@ -397,7 +397,7 @@ static int8_t SCSI_SynchronizeCache(USBD_HandleTypeDef *pdev, uint8_t lun, uint static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { uint8_t i; - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; for(i=0 ; i < REQUEST_SENSE_DATA_LEN ; i++) { @@ -439,7 +439,7 @@ static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t */ void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_t ASC) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->scsi_sense[hmsc->scsi_sense_tail].Skey = sKey; hmsc->scsi_sense[hmsc->scsi_sense_tail].w.ASC = ASC << 8; @@ -458,14 +458,14 @@ void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_ */ static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_data_length = 0; // On Mac OS X, when the device is ejected a SCSI_START_STOP_UNIT command is sent. // Bit 0 of params[4] is the START bit. // If we get a stop, we must really stop the device so that the Mac does not // automatically remount it. - ((USBD_StorageTypeDef *)pdev->pUserData)->StartStopUnit(lun, params[4] & 1); + hmsc->bdev_ops->StartStopUnit(lun, params[4] & 1); return 0; } @@ -479,9 +479,9 @@ static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t */ static int8_t SCSI_AllowMediumRemoval(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; hmsc->bot_data_length = 0; - ((USBD_StorageTypeDef *)pdev->pUserData)->PreventAllowMediumRemoval(lun, params[0]); + hmsc->bdev_ops->PreventAllowMediumRemoval(lun, params[0]); return 0; } @@ -494,7 +494,7 @@ static int8_t SCSI_AllowMediumRemoval(USBD_HandleTypeDef *pdev, uint8_t lun, ui */ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if(hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ { @@ -510,7 +510,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para return -1; } - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) !=0 ) + if(hmsc->bdev_ops->IsReady(lun) !=0 ) { SCSI_SenseCode(pdev, lun, @@ -562,7 +562,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if (hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ { @@ -579,7 +579,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa } /* Check whether Media is ready */ - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) !=0 ) + if(hmsc->bdev_ops->IsReady(lun) !=0 ) { SCSI_SenseCode(pdev, lun, @@ -589,7 +589,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa } /* Check If media is write-protected */ - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsWriteProtected(lun) !=0 ) + if(hmsc->bdev_ops->IsWriteProtected(lun) !=0 ) { SCSI_SenseCode(pdev, lun, @@ -652,7 +652,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if ((params[1]& 0x02) == 0x02) { @@ -687,7 +687,7 @@ static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa */ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , uint32_t blk_offset , uint16_t blk_nbr) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr ) { @@ -708,12 +708,12 @@ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , u */ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) { - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; uint32_t len; len = MIN(hmsc->scsi_blk_len , MSC_MEDIA_PACKET); - if( ((USBD_StorageTypeDef *)pdev->pUserData)->Read(lun , + if( hmsc->bdev_ops->Read(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, len / hmsc->scsi_blk_size) < 0) @@ -756,11 +756,11 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) { uint32_t len; - USBD_MSC_BOT_HandleTypeDef *hmsc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->msc; + USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; len = MIN(hmsc->scsi_blk_len , MSC_MEDIA_PACKET); - if(((USBD_StorageTypeDef *)pdev->pUserData)->Write(lun , + if(hmsc->bdev_ops->Write(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, len / hmsc->scsi_blk_size) < 0) From dbff0164b3ce7293d9de05c14fba6481c316c2e8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Sep 2017 21:43:32 +1000 Subject: [PATCH 058/163] stm32/usbdev: Merge all global USB device state into a single struct. This is the final piece of USB device refactoring to support multiple USB device instances. --- ports/stm32/usb.c | 99 ++++++++++++++++++++++++++++------------------- ports/stm32/usb.h | 1 - 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index e7cb3629d..1c8eab354 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -53,11 +53,16 @@ // this will be persistent across a soft-reset mp_uint_t pyb_usb_flags = 0; +typedef struct _usb_device_t { + uint32_t enabled; + USBD_HandleTypeDef hUSBDDevice; + usbd_cdc_msc_hid_state_t usbd_cdc_msc_hid_state; + usbd_cdc_itf_t usbd_cdc_itf; + usbd_hid_itf_t usbd_hid_itf; +} usb_device_t; + #ifdef USE_DEVICE_MODE -STATIC USBD_HandleTypeDef hUSBDDevice; -STATIC usbd_cdc_msc_hid_state_t usbd_cdc_msc_hid_state; -STATIC usbd_cdc_itf_t usbd_cdc_itf; -STATIC usbd_hid_itf_t usbd_hid_itf; +usb_device_t usb_device; pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE; #endif @@ -106,78 +111,80 @@ void pyb_usb_init0(void) { bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { #ifdef USE_DEVICE_MODE - if (!(pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED)) { + usb_device_t *usb_dev = &usb_device; + if (!usb_dev->enabled) { // only init USB once in the device's power-lifetime // configure the VID, PID and the USBD mode (interfaces it will expose) USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC); - if (USBD_SelectMode(&usbd_cdc_msc_hid_state, mode, hid_info) != 0) { + if (USBD_SelectMode(&usb_dev->usbd_cdc_msc_hid_state, mode, hid_info) != 0) { return false; } // set up the USBD state - USBD_HandleTypeDef *usbd = &hUSBDDevice; + USBD_HandleTypeDef *usbd = &usb_dev->hUSBDDevice; usbd->id = USB_PHY_ID; usbd->dev_state = USBD_STATE_DEFAULT; usbd->pDesc = (USBD_DescriptorsTypeDef*)&USBD_Descriptors; usbd->pClass = &USBD_CDC_MSC_HID; - usbd_cdc_msc_hid_state.pdev = usbd; - usbd_cdc_msc_hid_state.cdc = &usbd_cdc_itf; - usbd_cdc_msc_hid_state.hid = &usbd_hid_itf; - usbd->pClassData = &usbd_cdc_msc_hid_state; + usb_dev->usbd_cdc_msc_hid_state.pdev = usbd; + usb_dev->usbd_cdc_msc_hid_state.cdc = &usb_dev->usbd_cdc_itf; + usb_dev->usbd_cdc_msc_hid_state.hid = &usb_dev->usbd_hid_itf; + usbd->pClassData = &usb_dev->usbd_cdc_msc_hid_state; switch (pyb_usb_storage_medium) { #if MICROPY_HW_HAS_SDCARD case PYB_USB_STORAGE_MEDIUM_SDCARD: - USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); + USBD_MSC_RegisterStorage(&usb_dev->usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops); break; #endif default: - USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); + USBD_MSC_RegisterStorage(&usb_dev->usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops); break; } // start the USB device USBD_LL_Init(usbd); USBD_LL_Start(usbd); + usb_dev->enabled = true; } - pyb_usb_flags |= PYB_USB_FLAG_DEV_ENABLED; #endif return true; } void pyb_usb_dev_deinit(void) { - if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { - USBD_Stop(&hUSBDDevice); - pyb_usb_flags &= ~PYB_USB_FLAG_DEV_ENABLED; + usb_device_t *usb_dev = &usb_device; + if (usb_dev->enabled) { + USBD_Stop(&usb_dev->hUSBDDevice); + usb_dev->enabled = false; } } bool usb_vcp_is_enabled(void) { - return (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) != 0; + return usb_device.enabled; } int usb_vcp_recv_byte(uint8_t *c) { - return usbd_cdc_rx(&usbd_cdc_itf, c, 1, 0); + return usbd_cdc_rx(&usb_device.usbd_cdc_itf, c, 1, 0); } void usb_vcp_send_strn(const char *str, int len) { #ifdef USE_DEVICE_MODE - if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { - usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)str, len); + if (usb_device.enabled) { + usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)str, len); } #endif } void usb_vcp_send_strn_cooked(const char *str, int len) { #ifdef USE_DEVICE_MODE - if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { + if (usb_device.enabled) { for (const char *top = str + len; str < top; str++) { if (*str == '\n') { - usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)"\r\n", 2); + usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)"\r\n", 2); } else { - usbd_cdc_tx_always(&usbd_cdc_itf, (const uint8_t*)str, 1); + usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)str, 1); } } } @@ -226,7 +233,7 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t * #if defined(USE_HOST_MODE) return MP_OBJ_NEW_QSTR(MP_QSTR_host); #elif defined(USE_DEVICE_MODE) - uint8_t mode = USBD_GetMode(&usbd_cdc_msc_hid_state); + uint8_t mode = USBD_GetMode(&usb_device.usbd_cdc_msc_hid_state); switch (mode) { case USBD_MODE_CDC: return MP_OBJ_NEW_QSTR(MP_QSTR_VCP); @@ -351,9 +358,10 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 0, pyb_usb_mode); typedef struct _pyb_usb_vcp_obj_t { mp_obj_base_t base; + usb_device_t *usb_dev; } pyb_usb_vcp_obj_t; -STATIC const pyb_usb_vcp_obj_t pyb_usb_vcp_obj = {{&pyb_usb_vcp_type}}; +STATIC const pyb_usb_vcp_obj_t pyb_usb_vcp_obj = {{&pyb_usb_vcp_type}, &usb_device}; STATIC void pyb_usb_vcp_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mp_print_str(print, "USB_VCP()"); @@ -378,7 +386,8 @@ STATIC mp_obj_t pyb_usb_vcp_setinterrupt(mp_obj_t self_in, mp_obj_t int_chr_in) STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_usb_vcp_setinterrupt_obj, pyb_usb_vcp_setinterrupt); STATIC mp_obj_t pyb_usb_vcp_isconnected(mp_obj_t self_in) { - return mp_obj_new_bool(usbd_cdc_is_connected(&usbd_cdc_itf)); + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(usbd_cdc_is_connected(&self->usb_dev->usbd_cdc_itf)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_vcp_isconnected_obj, pyb_usb_vcp_isconnected); @@ -391,7 +400,8 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc); /// \method any() /// Return `True` if any characters waiting, else `False`. STATIC mp_obj_t pyb_usb_vcp_any(mp_obj_t self_in) { - if (usbd_cdc_rx_num(&usbd_cdc_itf) > 0) { + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (usbd_cdc_rx_num(&self->usb_dev->usbd_cdc_itf) > 0) { return mp_const_true; } else { return mp_const_false; @@ -414,6 +424,7 @@ STATIC const mp_arg_t pyb_usb_vcp_send_args[] = { STATIC mp_obj_t pyb_usb_vcp_send(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(args[0]); mp_arg_val_t vals[PYB_USB_VCP_SEND_NUM_ARGS]; mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_USB_VCP_SEND_NUM_ARGS, pyb_usb_vcp_send_args, vals); @@ -423,7 +434,7 @@ STATIC mp_obj_t pyb_usb_vcp_send(size_t n_args, const mp_obj_t *args, mp_map_t * pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data); // send the data - int ret = usbd_cdc_tx(&usbd_cdc_itf, bufinfo.buf, bufinfo.len, vals[1].u_int); + int ret = usbd_cdc_tx(&self->usb_dev->usbd_cdc_itf, bufinfo.buf, bufinfo.len, vals[1].u_int); return mp_obj_new_int(ret); } @@ -441,6 +452,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_vcp_send_obj, 1, pyb_usb_vcp_send); /// otherwise the number of bytes read into `data` is returned. STATIC mp_obj_t pyb_usb_vcp_recv(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(args[0]); mp_arg_val_t vals[PYB_USB_VCP_SEND_NUM_ARGS]; mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_USB_VCP_SEND_NUM_ARGS, pyb_usb_vcp_send_args, vals); @@ -449,7 +461,7 @@ STATIC mp_obj_t pyb_usb_vcp_recv(size_t n_args, const mp_obj_t *args, mp_map_t * mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // receive the data - int ret = usbd_cdc_rx(&usbd_cdc_itf, (uint8_t*)vstr.buf, vstr.len, vals[1].u_int); + int ret = usbd_cdc_rx(&self->usb_dev->usbd_cdc_itf, (uint8_t*)vstr.buf, vstr.len, vals[1].u_int); // return the received data if (o_ret != MP_OBJ_NULL) { @@ -486,7 +498,8 @@ STATIC const mp_rom_map_elem_t pyb_usb_vcp_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_usb_vcp_locals_dict, pyb_usb_vcp_locals_dict_table); STATIC mp_uint_t pyb_usb_vcp_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { - int ret = usbd_cdc_rx(&usbd_cdc_itf, (byte*)buf, size, 0); + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); + int ret = usbd_cdc_rx(&self->usb_dev->usbd_cdc_itf, (byte*)buf, size, 0); if (ret == 0) { // return EAGAIN error to indicate non-blocking *errcode = MP_EAGAIN; @@ -496,7 +509,8 @@ STATIC mp_uint_t pyb_usb_vcp_read(mp_obj_t self_in, void *buf, mp_uint_t size, i } STATIC mp_uint_t pyb_usb_vcp_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { - int ret = usbd_cdc_tx(&usbd_cdc_itf, (const byte*)buf, size, 0); + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); + int ret = usbd_cdc_tx(&self->usb_dev->usbd_cdc_itf, (const byte*)buf, size, 0); if (ret == 0) { // return EAGAIN error to indicate non-blocking *errcode = MP_EAGAIN; @@ -507,13 +521,14 @@ STATIC mp_uint_t pyb_usb_vcp_write(mp_obj_t self_in, const void *buf, mp_uint_t STATIC mp_uint_t pyb_usb_vcp_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { mp_uint_t ret; + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); if (request == MP_STREAM_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_STREAM_POLL_RD) && usbd_cdc_rx_num(&usbd_cdc_itf) > 0) { + if ((flags & MP_STREAM_POLL_RD) && usbd_cdc_rx_num(&self->usb_dev->usbd_cdc_itf) > 0) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && usbd_cdc_tx_half_empty(&usbd_cdc_itf)) { + if ((flags & MP_STREAM_POLL_WR) && usbd_cdc_tx_half_empty(&self->usb_dev->usbd_cdc_itf)) { ret |= MP_STREAM_POLL_WR; } } else { @@ -545,9 +560,10 @@ const mp_obj_type_t pyb_usb_vcp_type = { typedef struct _pyb_usb_hid_obj_t { mp_obj_base_t base; + usb_device_t *usb_dev; } pyb_usb_hid_obj_t; -STATIC const pyb_usb_hid_obj_t pyb_usb_hid_obj = {{&pyb_usb_hid_type}}; +STATIC const pyb_usb_hid_obj_t pyb_usb_hid_obj = {{&pyb_usb_hid_type}, &usb_device}; STATIC mp_obj_t pyb_usb_hid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { // check arguments @@ -576,6 +592,7 @@ STATIC mp_obj_t pyb_usb_hid_recv(size_t n_args, const mp_obj_t *args, mp_map_t * }; // parse args + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(args[0]); mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals); @@ -584,7 +601,7 @@ STATIC mp_obj_t pyb_usb_hid_recv(size_t n_args, const mp_obj_t *args, mp_map_t * mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // receive the data - int ret = usbd_hid_rx(&usbd_hid_itf, vstr.len, (uint8_t*)vstr.buf, vals[1].u_int); + int ret = usbd_hid_rx(&self->usb_dev->usbd_hid_itf, vstr.len, (uint8_t*)vstr.buf, vals[1].u_int); // return the received data if (o_ret != MP_OBJ_NULL) { @@ -598,6 +615,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_hid_recv_obj, 1, pyb_usb_hid_recv); STATIC mp_obj_t pyb_usb_hid_send(mp_obj_t self_in, mp_obj_t report_in) { #ifdef USE_DEVICE_MODE + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; byte temp_buf[8]; // get the buffer to send from @@ -615,7 +633,7 @@ STATIC mp_obj_t pyb_usb_hid_send(mp_obj_t self_in, mp_obj_t report_in) { } // send the data - if (USBD_OK == USBD_HID_SendReport(&usbd_cdc_msc_hid_state, bufinfo.buf, bufinfo.len)) { + if (USBD_OK == USBD_HID_SendReport(&self->usb_dev->usbd_cdc_msc_hid_state, bufinfo.buf, bufinfo.len)) { return mp_obj_new_int(bufinfo.len); } else { return mp_obj_new_int(0); @@ -628,7 +646,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_usb_hid_send_obj, pyb_usb_hid_send); // deprecated in favour of USB_HID.send STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) { - return pyb_usb_hid_send(MP_OBJ_NULL, arg); + return pyb_usb_hid_send(MP_OBJ_FROM_PTR(&pyb_usb_hid_obj), arg); } MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report); @@ -640,14 +658,15 @@ STATIC const mp_rom_map_elem_t pyb_usb_hid_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_usb_hid_locals_dict, pyb_usb_hid_locals_dict_table); STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { + pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_uint_t ret; if (request == MP_STREAM_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_STREAM_POLL_RD) && usbd_hid_rx_num(&usbd_hid_itf) > 0) { + if ((flags & MP_STREAM_POLL_RD) && usbd_hid_rx_num(&self->usb_dev->usbd_hid_itf) > 0) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&usbd_cdc_msc_hid_state)) { + if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&self->usb_dev->usbd_cdc_msc_hid_state)) { ret |= MP_STREAM_POLL_WR; } } else { diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index 456f45208..f60ea8033 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -28,7 +28,6 @@ #include "usbd_cdc_msc_hid0.h" -#define PYB_USB_FLAG_DEV_ENABLED (0x0001) #define PYB_USB_FLAG_USB_MODE_CALLED (0x0002) // Windows needs a different PID to distinguish different device configurations From db7f4aa2cb5848942c421785770831bd9be7b9b6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Sep 2017 10:28:56 +1000 Subject: [PATCH 059/163] stm32/usbdev: Make device descriptor callbacks take a state pointer. --- ports/stm32/usbd_desc.c | 20 ++++++++++---------- ports/stm32/usbdev/core/inc/usbd_def.h | 16 +++++++++------- ports/stm32/usbdev/core/src/usbd_ctlreq.c | 14 +++++++------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/ports/stm32/usbd_desc.c b/ports/stm32/usbd_desc.c index 09e1608c8..8b8e7e7f0 100644 --- a/ports/stm32/usbd_desc.c +++ b/ports/stm32/usbd_desc.c @@ -111,7 +111,7 @@ void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_n * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { *length = sizeof(hUSBDDeviceDesc); return hUSBDDeviceDesc; } @@ -122,7 +122,7 @@ STATIC uint8_t *USBD_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { *length = sizeof(USBD_LangIDDesc); return USBD_LangIDDesc; } @@ -133,8 +133,8 @@ STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *leng * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - if(speed == 0) { +STATIC uint8_t *USBD_ProductStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + if (pdev->dev_speed == USBD_SPEED_HIGH) { USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); } else { USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length); @@ -148,7 +148,7 @@ STATIC uint8_t *USBD_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *len * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); return USBD_StrDesc; } @@ -159,7 +159,7 @@ STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_SerialStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { // This document: http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf // says that the serial number has to be at least 12 digits long and that // the last 12 digits need to be unique. It also stipulates that the valid @@ -189,8 +189,8 @@ STATIC uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *leng * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - if(speed == USBD_SPEED_HIGH) { +STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + if (pdev->dev_speed == USBD_SPEED_HIGH) { USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); } else { USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length); @@ -204,8 +204,8 @@ STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *leng * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - if(speed == 0) { +STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + if (pdev->dev_speed == USBD_SPEED_HIGH) { USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); } else { USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length); diff --git a/ports/stm32/usbdev/core/inc/usbd_def.h b/ports/stm32/usbdev/core/inc/usbd_def.h index bd913a286..888d426ef 100644 --- a/ports/stm32/usbdev/core/inc/usbd_def.h +++ b/ports/stm32/usbdev/core/inc/usbd_def.h @@ -193,16 +193,18 @@ typedef enum { USBD_FAIL, }USBD_StatusTypeDef; +struct _USBD_HandleTypeDef; + /* USB Device descriptors structure */ typedef struct { - uint8_t *(*GetDeviceDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetLangIDStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetManufacturerStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetProductStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetSerialStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetConfigurationStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetInterfaceStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); + uint8_t *(*GetDeviceDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetLangIDStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetManufacturerStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetProductStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetSerialStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetConfigurationStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); + uint8_t *(*GetInterfaceStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length); } USBD_DescriptorsTypeDef; /* USB Device handle structure */ diff --git a/ports/stm32/usbdev/core/src/usbd_ctlreq.c b/ports/stm32/usbdev/core/src/usbd_ctlreq.c index 15d9b6ec4..5fba322fa 100644 --- a/ports/stm32/usbdev/core/src/usbd_ctlreq.c +++ b/ports/stm32/usbdev/core/src/usbd_ctlreq.c @@ -330,7 +330,7 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , switch (req->wValue >> 8) { case USB_DESC_TYPE_DEVICE: - pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetDeviceDescriptor(pdev, &len); break; case USB_DESC_TYPE_CONFIGURATION: @@ -350,27 +350,27 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , switch ((uint8_t)(req->wValue)) { case USBD_IDX_LANGID_STR: - pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev, &len); break; case USBD_IDX_MFC_STR: - pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev, &len); break; case USBD_IDX_PRODUCT_STR: - pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetProductStrDescriptor(pdev, &len); break; case USBD_IDX_SERIAL_STR: - pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev, &len); break; case USBD_IDX_CONFIG_STR: - pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev, &len); break; case USBD_IDX_INTERFACE_STR: - pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len); + pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev, &len); break; default: From f7f4bf0321c083245433bcc40294c695ec522514 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Sep 2017 10:57:21 +1000 Subject: [PATCH 060/163] stm32/usbdev: Move all the USB device descriptor state into its struct. --- ports/stm32/usb.c | 2 +- ports/stm32/usbd_desc.c | 102 ++++++++---------- ports/stm32/usbd_desc.h | 4 +- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 4 +- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 1c8eab354..8f89107ba 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -116,7 +116,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H // only init USB once in the device's power-lifetime // configure the VID, PID and the USBD mode (interfaces it will expose) - USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC); + USBD_SetVIDPIDRelease(&usb_dev->usbd_cdc_msc_hid_state, vid, pid, 0x0200, mode == USBD_MODE_CDC); if (USBD_SelectMode(&usb_dev->usbd_cdc_msc_hid_state, mode, hid_info) != 0) { return false; } diff --git a/ports/stm32/usbd_desc.c b/ports/stm32/usbd_desc.c index 8b8e7e7f0..1de75aee0 100644 --- a/ports/stm32/usbd_desc.c +++ b/ports/stm32/usbd_desc.c @@ -50,59 +50,45 @@ #define USBD_CONFIGURATION_FS_STRING "Pyboard Config" #define USBD_INTERFACE_FS_STRING "Pyboard Interface" -// USB Standard Device Descriptor -// needs to be in RAM because we modify the VID and PID -__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { - 0x12, // bLength - USB_DESC_TYPE_DEVICE, // bDescriptorType - 0x00, // bcdUSB - 0x02, - 0xef, // bDeviceClass: Miscellaneous Device Class - 0x02, // bDeviceSubClass: Common Class - 0x01, // bDeviceProtocol: Interface Association Descriptor - USB_MAX_EP0_SIZE, // bMaxPacketSize - LOBYTE(USBD_VID), // idVendor - HIBYTE(USBD_VID), // idVendor - LOBYTE(USBD_PID), // idVendor - HIBYTE(USBD_PID), // idVendor - 0x00, // bcdDevice rel. 2.00 - 0x02, - USBD_IDX_MFC_STR, // Index of manufacturer string - USBD_IDX_PRODUCT_STR, // Index of product string - USBD_IDX_SERIAL_STR, // Index of serial number string - USBD_MAX_NUM_CONFIGURATION // bNumConfigurations -}; - -__ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { +__ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { USB_LEN_LANGID_STR_DESC, USB_DESC_TYPE_STRING, LOBYTE(USBD_LANGID_STRING), HIBYTE(USBD_LANGID_STRING), }; -__ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; - // set the VID, PID and device release number -void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only) { +void USBD_SetVIDPIDRelease(usbd_cdc_msc_hid_state_t *usbd, uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only) { + uint8_t *dev_desc = &usbd->usbd_device_desc[0]; + + dev_desc[0] = USB_LEN_DEV_DESC; // bLength + dev_desc[1] = USB_DESC_TYPE_DEVICE; // bDescriptorType + dev_desc[2] = 0x00; // bcdUSB + dev_desc[3] = 0x02; // bcdUSB if (cdc_only) { // Make it look like a Communications device if we're only // using CDC. Otherwise, windows gets confused when we tell it that // its a composite device with only a cdc serial interface. - hUSBDDeviceDesc[4] = 0x02; - hUSBDDeviceDesc[5] = 0x00; - hUSBDDeviceDesc[6] = 0x00; + dev_desc[4] = 0x02; // bDeviceClass + dev_desc[5] = 0x00; // bDeviceSubClass + dev_desc[6] = 0x00; // bDeviceProtocol } else { // For the other modes, we make this look like a composite device. - hUSBDDeviceDesc[4] = 0xef; - hUSBDDeviceDesc[5] = 0x02; - hUSBDDeviceDesc[6] = 0x01; + dev_desc[4] = 0xef; // bDeviceClass: Miscellaneous Device Class + dev_desc[5] = 0x02; // bDeviceSubClass: Common Class + dev_desc[6] = 0x01; // bDeviceProtocol: Interface Association Descriptor } - hUSBDDeviceDesc[8] = LOBYTE(vid); - hUSBDDeviceDesc[9] = HIBYTE(vid); - hUSBDDeviceDesc[10] = LOBYTE(pid); - hUSBDDeviceDesc[11] = HIBYTE(pid); - hUSBDDeviceDesc[12] = LOBYTE(device_release_num); - hUSBDDeviceDesc[13] = HIBYTE(device_release_num); + dev_desc[7] = USB_MAX_EP0_SIZE; // bMaxPacketSize + dev_desc[8] = LOBYTE(vid); // idVendor + dev_desc[9] = HIBYTE(vid); // idVendor + dev_desc[10] = LOBYTE(pid); // idVendor + dev_desc[11] = HIBYTE(pid); // idVendor + dev_desc[12] = LOBYTE(device_release_num); // bcdDevice + dev_desc[13] = HIBYTE(device_release_num); // bcdDevice + dev_desc[14] = USBD_IDX_MFC_STR; // Index of manufacturer string + dev_desc[15] = USBD_IDX_PRODUCT_STR; // Index of product string + dev_desc[16] = USBD_IDX_SERIAL_STR; // Index of serial number string + dev_desc[17] = USBD_MAX_NUM_CONFIGURATION; // bNumConfigurations } /** @@ -112,8 +98,9 @@ void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_n * @retval Pointer to descriptor buffer */ STATIC uint8_t *USBD_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { - *length = sizeof(hUSBDDeviceDesc); - return hUSBDDeviceDesc; + uint8_t *dev_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_device_desc; + *length = USB_LEN_DEV_DESC; + return dev_desc; } /** @@ -124,7 +111,7 @@ STATIC uint8_t *USBD_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length */ STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { *length = sizeof(USBD_LangIDDesc); - return USBD_LangIDDesc; + return (uint8_t*)USBD_LangIDDesc; // the data should only be read from this buf } /** @@ -134,12 +121,13 @@ STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *len * @retval Pointer to descriptor buffer */ STATIC uint8_t *USBD_ProductStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc; if (pdev->dev_speed == USBD_SPEED_HIGH) { - USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, str_desc, length); } else { - USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, str_desc, length); } - return USBD_StrDesc; + return str_desc; } /** @@ -149,8 +137,9 @@ STATIC uint8_t *USBD_ProductStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *le * @retval Pointer to descriptor buffer */ STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { - USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); - return USBD_StrDesc; + uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc; + USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, str_desc, length); + return str_desc; } /** @@ -179,8 +168,9 @@ STATIC uint8_t *USBD_SerialStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *len "%02X%02X%02X%02X%02X%02X", id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]); - USBD_GetString((uint8_t *)serial_buf, USBD_StrDesc, length); - return USBD_StrDesc; + uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc; + USBD_GetString((uint8_t *)serial_buf, str_desc, length); + return str_desc; } /** @@ -190,12 +180,13 @@ STATIC uint8_t *USBD_SerialStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *len * @retval Pointer to descriptor buffer */ STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc; if (pdev->dev_speed == USBD_SPEED_HIGH) { - USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, str_desc, length); } else { - USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, str_desc, length); } - return USBD_StrDesc; + return str_desc; } /** @@ -205,12 +196,13 @@ STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *len * @retval Pointer to descriptor buffer */ STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { + uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc; if (pdev->dev_speed == USBD_SPEED_HIGH) { - USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, str_desc, length); } else { - USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length); + USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, str_desc, length); } - return USBD_StrDesc; + return str_desc; } const USBD_DescriptorsTypeDef USBD_Descriptors = { diff --git a/ports/stm32/usbd_desc.h b/ports/stm32/usbd_desc.h index 05cbbe5b1..a4de6c681 100644 --- a/ports/stm32/usbd_desc.h +++ b/ports/stm32/usbd_desc.h @@ -26,8 +26,10 @@ #ifndef MICROPY_INCLUDED_STMHAL_USBD_DESC_H #define MICROPY_INCLUDED_STMHAL_USBD_DESC_H +#include "usbd_cdc_msc_hid.h" + extern const USBD_DescriptorsTypeDef USBD_Descriptors; -void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only); +void USBD_SetVIDPIDRelease(usbd_cdc_msc_hid_state_t *usbd, uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only); #endif // MICROPY_INCLUDED_STMHAL_USBD_DESC_H diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index 2283cbe85..a26b1df0d 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -105,7 +105,9 @@ typedef struct _usbd_cdc_msc_hid_state_t { USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; USBD_HID_HandleTypeDef HID_ClassData; - // RAM to hold the current configuration descriptor, which we configure on the fly + // RAM to hold the current descriptors, which we configure on the fly + __ALIGN_BEGIN uint8_t usbd_device_desc[USB_LEN_DEV_DESC] __ALIGN_END; + __ALIGN_BEGIN uint8_t usbd_str_desc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; __ALIGN_BEGIN uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END; void *cdc; From e2ba45c35fa9f5e84f79e8e220a9a7d43a94ff7f Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Sep 2017 11:28:45 +1000 Subject: [PATCH 061/163] py/vm: Use lowercase letter at start of exception message. For consistency with all the other exception messages. --- py/vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/vm.c b/py/vm.c index c7fc83d04..9a27b2b22 100644 --- a/py/vm.c +++ b/py/vm.c @@ -1124,7 +1124,7 @@ unwind_jump:; } } if (obj == MP_OBJ_NULL) { - obj = mp_obj_new_exception_msg(&mp_type_RuntimeError, "No active exception to reraise"); + obj = mp_obj_new_exception_msg(&mp_type_RuntimeError, "no active exception to reraise"); RAISE(obj); } } else { From d36539df06863cd83fc3a475c7b6f04d20095967 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Sep 2017 11:31:00 +1000 Subject: [PATCH 062/163] lib/embed/abort_: Use mp_raise_msg helper function. --- lib/embed/abort_.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/embed/abort_.c b/lib/embed/abort_.c index a6df8143c..2fba0de4e 100644 --- a/lib/embed/abort_.c +++ b/lib/embed/abort_.c @@ -3,5 +3,5 @@ NORETURN void abort_(void); NORETURN void abort_(void) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "abort() called")); + mp_raise_msg(&mp_type_RuntimeError, "abort() called"); } From 8edc2e4b141ffad74754686fbdc66e033ff24f34 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Sep 2017 11:54:08 +1000 Subject: [PATCH 063/163] py/runtime0: Add comments about unary/binary-op enums used in bytecode. --- py/runtime0.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/runtime0.h b/py/runtime0.h index 54bf192d3..6d77cce68 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -42,6 +42,8 @@ #define MP_NATIVE_TYPE_PTR16 (0x06) #define MP_NATIVE_TYPE_PTR32 (0x07) +// Note: the first 7 of these are used in bytecode and changing +// them requires changing the bytecode version. typedef enum { MP_UNARY_OP_BOOL, // __bool__ MP_UNARY_OP_LEN, // __len__ @@ -54,6 +56,8 @@ typedef enum { MP_UNARY_OP_SIZEOF, // for sys.getsizeof() } mp_unary_op_t; +// Note: the first 35 of these are used in bytecode and changing +// them requires changing the bytecode version. typedef enum { // Relational operations, should return a bool MP_BINARY_OP_LESS, From 4a314a6f63c7157a824aa98c01009a8fbafb7475 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 24 Sep 2017 10:19:24 +0300 Subject: [PATCH 064/163] extmod/re1.5: Update to 0.8.1. Allow literal minus in char classes to be in trailing position, e.g. [a-c-]. (Previously, minus was allowed only at the start.) This increases ARM Thumb2 code size by 8 bytes. --- extmod/re1.5/compilecode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/re1.5/compilecode.c b/extmod/re1.5/compilecode.c index e4635f034..3267a4190 100644 --- a/extmod/re1.5/compilecode.c +++ b/extmod/re1.5/compilecode.c @@ -55,7 +55,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) for (cnt = 0; *re != ']'; re++, cnt++) { if (!*re) return NULL; EMIT(PC++, *re); - if (re[1] == '-') { + if (re[1] == '-' && re[2] != ']') { re += 2; } EMIT(PC++, *re); From d29b7096424638f89d82a2a4ce295e5fd9325fa2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 25 Sep 2017 15:25:08 +1000 Subject: [PATCH 065/163] stm32/timer: Enable ARPE so that timer freq can be changed smoothly. The timer prescaler is buffered by default, and this patch enables ARPE which buffers the auto-reload register. With both of these registers buffered it's now possible to smoothly change the timer's frequency and have a smoothly varying PWM output. --- ports/stm32/timer.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index f24c2e6bf..96a6baa02 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -620,6 +620,12 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons #endif config_deadtime(self, args[6].u_int); } + + // Enable ARPE so that the auto-reload register is buffered. + // This allows to smoothly change the frequency of the timer. + self->tim.Instance->CR1 |= TIM_CR1_ARPE; + + // Start the timer running if (args[5].u_obj == mp_const_none) { HAL_TIM_Base_Start(&self->tim); } else { @@ -1121,10 +1127,6 @@ STATIC mp_obj_t pyb_timer_freq(size_t n_args, const mp_obj_t *args) { uint32_t prescaler = compute_prescaler_period_from_freq(self, args[1], &period); self->tim.Instance->PSC = prescaler; __HAL_TIM_SetAutoreload(&self->tim, period); - // Reset the counter to zero. Otherwise, if counter >= period it will - // continue counting until it wraps (at either 16 or 32 bits depending - // on the timer). - __HAL_TIM_SetCounter(&self->tim, 0); return mp_const_none; } } @@ -1155,10 +1157,6 @@ STATIC mp_obj_t pyb_timer_period(size_t n_args, const mp_obj_t *args) { } else { // set __HAL_TIM_SetAutoreload(&self->tim, mp_obj_get_int(args[1]) & TIMER_CNT_MASK(self)); - // Reset the counter to zero. Otherwise, if counter >= period it will - // continue counting until it wraps (at either 16 or 32 bits depending - // on the timer). - __HAL_TIM_SetCounter(&self->tim, 0); return mp_const_none; } } From 8fa03fee77ff5ac49a0daa544951468f4c193b68 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Fri, 15 Sep 2017 14:51:49 +0100 Subject: [PATCH 066/163] drivers/display/ssd1306.py: Improve performance of graphics methods. It removes the need for a wrapper Python function to dispatch to the framebuf method which makes each function call a bit faster, roughly 2.5x. This patch also adds the rest of the framebuf methods to the SSD class. --- drivers/display/ssd1306.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index 53bcb0d2d..d65f2d350 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -32,7 +32,21 @@ def __init__(self, width, height, external_vcc): self.external_vcc = external_vcc self.pages = self.height // 8 self.buffer = bytearray(self.pages * self.width) - self.framebuf = framebuf.FrameBuffer(self.buffer, self.width, self.height, framebuf.MVLSB) + fb = framebuf.FrameBuffer(self.buffer, self.width, self.height, framebuf.MONO_VLSB) + self.framebuf = fb + # Provide methods for accessing FrameBuffer graphics primitives. This is a + # workround because inheritance from a native class is currently unsupported. + # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html + self.fill = fb.fill + self.pixel = fb.pixel + self.hline = fb.hline + self.vline = fb.vline + self.line = fb.line + self.rect = fb.rect + self.fill_rect = fb.fill_rect + self.text = fb.text + self.scroll = fb.scroll + self.blit = fb.blit self.poweron() self.init_display() @@ -88,18 +102,6 @@ def show(self): self.write_cmd(self.pages - 1) self.write_data(self.buffer) - def fill(self, col): - self.framebuf.fill(col) - - def pixel(self, x, y, col): - self.framebuf.pixel(x, y, col) - - def scroll(self, dx, dy): - self.framebuf.scroll(dx, dy) - - def text(self, string, x, y, col=1): - self.framebuf.text(string, x, y, col) - class SSD1306_I2C(SSD1306): def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False): From f008263022f794c056d8d102deaa62760822761a Mon Sep 17 00:00:00 2001 From: Anton Patrushev Date: Fri, 22 Sep 2017 22:44:09 +0500 Subject: [PATCH 067/163] py/persistentcode: Define mp_raw_code_save_file() for any unix target. A unix target should provide POSIX open/write/close functions regardless of its machine architecture. Fixes issue #3325. --- py/persistentcode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/persistentcode.c b/py/persistentcode.c index 2fa8c1df0..f954038ae 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -374,7 +374,7 @@ void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) { // here we define mp_raw_code_save_file depending on the port // TODO abstract this away properly -#if defined(__i386__) || defined(__x86_64__) || (defined(__arm__) && (defined(__unix__))) +#if defined(__i386__) || defined(__x86_64__) || defined(__unix__) #include #include From 9d836fedbdb1d28bdfc4ba475bbdfc1adb3f007a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 25 Sep 2017 16:35:19 -0700 Subject: [PATCH 068/163] py: Clarify which mp_unary_op_t's may appear in the bytecode. Not all can, so we don't need to reserve bytecodes for them, and can use free slots for something else later. --- py/bc0.h | 2 +- py/runtime0.h | 14 +++++++++----- py/showbc.c | 2 +- py/vmentrytable.h | 2 +- tests/cmdline/cmd_showbc.py.exp | 6 +++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/py/bc0.h b/py/bc0.h index f671c5b5a..38e23c0fd 100644 --- a/py/bc0.h +++ b/py/bc0.h @@ -113,7 +113,7 @@ #define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // + N(64) #define MP_BC_LOAD_FAST_MULTI (0xb0) // + N(16) #define MP_BC_STORE_FAST_MULTI (0xc0) // + N(16) -#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(7) +#define MP_BC_UNARY_OP_MULTI (0xd0) // + op( Date: Sun, 24 Sep 2017 20:15:48 -0500 Subject: [PATCH 069/163] py: Add config option to print warnings/errors to stderr. This adds a new configuration option to print runtime warnings and errors to stderr. On Unix, CPython prints warnings and unhandled exceptions to stderr, so the unix port here is configured to use this option. The unix port already printed unhandled exceptions on the main thread to stderr. This patch fixes unhandled exceptions on other threads and warnings (issue #2838) not printing on stderr. Additionally, a couple tests needed to be fixed to handle this new behavior. This is done by also capturing stderr when running tests. --- ports/unix/mpconfigport.h | 3 +++ py/modthread.c | 8 ++++---- py/mpconfig.h | 5 +++++ py/warning.c | 6 +++--- tests/run-tests | 4 ++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index b557f3d44..50b979279 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -145,8 +145,11 @@ // names in exception messages (may require more RAM). #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) #define MICROPY_WARNINGS (1) +#define MICROPY_ERROR_PRINTER (&mp_stderr_print) #define MICROPY_PY_STR_BYTES_CMP_WARN (1) +extern const struct _mp_print_t mp_stderr_print; + // Define to 1 to use undertested inefficient GC helper implementation // (if more efficient arch-specific one is not available). #ifndef MICROPY_GCREGS_SETJMP diff --git a/py/modthread.c b/py/modthread.c index bf74128e8..cb071d0f8 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -192,10 +192,10 @@ STATIC void *thread_entry(void *args_in) { // swallow exception silently } else { // print exception out - mp_printf(&mp_plat_print, "Unhandled exception in thread started by "); - mp_obj_print_helper(&mp_plat_print, args->fun, PRINT_REPR); - mp_printf(&mp_plat_print, "\n"); - mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(exc)); + mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in thread started by "); + mp_obj_print_helper(MICROPY_ERROR_PRINTER, args->fun, PRINT_REPR); + mp_printf(MICROPY_ERROR_PRINTER, "\n"); + mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(exc)); } } diff --git a/py/mpconfig.h b/py/mpconfig.h index b93f851d6..44de3beeb 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -533,6 +533,11 @@ typedef long long mp_longint_impl_t; #define MICROPY_WARNINGS (0) #endif +// This macro is used when printing runtime warnings and errors +#ifndef MICROPY_ERROR_PRINTER +#define MICROPY_ERROR_PRINTER (&mp_plat_print) +#endif + // Float and complex implementation #define MICROPY_FLOAT_IMPL_NONE (0) #define MICROPY_FLOAT_IMPL_FLOAT (1) diff --git a/py/warning.c b/py/warning.c index 46b31ecca..12d0f9c99 100644 --- a/py/warning.c +++ b/py/warning.c @@ -35,9 +35,9 @@ void mp_warning(const char *msg, ...) { va_list args; va_start(args, msg); - mp_print_str(&mp_plat_print, "Warning: "); - mp_vprintf(&mp_plat_print, msg, args); - mp_print_str(&mp_plat_print, "\n"); + mp_print_str(MICROPY_ERROR_PRINTER, "Warning: "); + mp_vprintf(MICROPY_ERROR_PRINTER, msg, args); + mp_print_str(MICROPY_ERROR_PRINTER, "\n"); va_end(args); } diff --git a/tests/run-tests b/tests/run-tests index 568d99f8e..23fc3d910 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -103,7 +103,7 @@ def run_micropython(pyb, args, test_file, is_special=False): os.close(master) os.close(slave) else: - output_mupy = subprocess.check_output(args + [test_file]) + output_mupy = subprocess.check_output(args + [test_file], stderr=subprocess.STDOUT) except subprocess.CalledProcessError: return b'CRASH' @@ -124,7 +124,7 @@ def run_micropython(pyb, args, test_file, is_special=False): # run the actual test try: - output_mupy = subprocess.check_output(cmdlist) + output_mupy = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: output_mupy = b'CRASH' From bdc6e86e079dd8a82e9ead1d4041c2e17c882437 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Sep 2017 12:57:51 +1000 Subject: [PATCH 070/163] py/objfloat: Support raising a negative number to a fractional power. This returns a complex number, following CPython behaviour. For ports that don't have complex numbers enabled this will raise a ValueError which gives a fail-safe for scripts that were written assuming complex numbers exist. --- py/objfloat.c | 7 +++++++ tests/float/complex1.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/py/objfloat.c b/py/objfloat.c index 55a9379ff..0831be3fd 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -298,6 +298,13 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t if (lhs_val == 0 && rhs_val < 0) { goto zero_division_error; } + if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(floor)(rhs_val)) { + #if MICROPY_PY_BUILTINS_COMPLEX + return mp_obj_complex_binary_op(MP_BINARY_OP_POWER, lhs_val, 0, rhs_in); + #else + mp_raise_ValueError("complex values not supported"); + #endif + } lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break; case MP_BINARY_OP_DIVMOD: { diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 7f0b317b3..854410545 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -53,6 +53,10 @@ # float on lhs should delegate to complex print(1.2 + 3j) +# negative base and fractional power should create a complex +ans = (-1) ** 2.3; print("%.5g %.5g" % (ans.real, ans.imag)) +ans = (-1.2) ** -3.4; print("%.5g %.5g" % (ans.real, ans.imag)) + # check printing of inf/nan print(float('nan') * 1j) print(float('inf') * (1 + 1j)) From 2f7827ba8f8e31eda0e15f3d51073329cf606323 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 29 Sep 2017 18:24:11 -0700 Subject: [PATCH 071/163] tools/upip: Upgrade to 1.2.2. TLS SNI support, fixes after making str.rstrip() behavior compliant. --- tools/upip.py | 2 +- tools/upip_utarfile.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/upip.py b/tools/upip.py index 7b85c718f..411da49e8 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -122,7 +122,7 @@ def url_open(url): s.connect(addr) if proto == "https:": - s = ussl.wrap_socket(s) + s = ussl.wrap_socket(s, server_hostname=host) if warn_ussl: print("Warning: %s SSL certificate is not validated" % host) warn_ussl = False diff --git a/tools/upip_utarfile.py b/tools/upip_utarfile.py index 65ce0bdca..460ca2cd4 100644 --- a/tools/upip_utarfile.py +++ b/tools/upip_utarfile.py @@ -3,7 +3,7 @@ # http://www.gnu.org/software/tar/manual/html_node/Standard.html TAR_HEADER = { "name": (uctypes.ARRAY | 0, uctypes.UINT8 | 100), - "size": (uctypes.ARRAY | 124, uctypes.UINT8 | 12), + "size": (uctypes.ARRAY | 124, uctypes.UINT8 | 11), } DIRTYPE = "dir" @@ -75,8 +75,8 @@ def next(self): return None d = TarInfo() - d.name = str(h.name, "utf-8").rstrip() - d.size = int(bytes(h.size).rstrip(), 8) + d.name = str(h.name, "utf-8").rstrip("\0") + d.size = int(bytes(h.size), 8) d.type = [REGTYPE, DIRTYPE][d.name[-1] == "/"] self.subf = d.subf = FileSection(self.f, d.size, roundup(d.size, 512)) return d From c9a0b2a8182abcb87afa1d8766d74ca8fa87cb34 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 2 Oct 2017 21:20:47 +0300 Subject: [PATCH 072/163] extmod/re1.5: Upgrade to v0.8.2, adds hook for stack overflow checking. --- extmod/re1.5/re1.5.h | 3 +++ extmod/re1.5/recursiveloop.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/extmod/re1.5/re1.5.h b/extmod/re1.5/re1.5.h index 815c5d33d..ba6f97b74 100644 --- a/extmod/re1.5/re1.5.h +++ b/extmod/re1.5/re1.5.h @@ -48,6 +48,9 @@ void printre(Regexp*); #ifndef re1_5_fatal void re1_5_fatal(char*); #endif +#ifndef re1_5_stack_chk +#define re1_5_stack_chk() +#endif void *mal(int); struct Prog diff --git a/extmod/re1.5/recursiveloop.c b/extmod/re1.5/recursiveloop.c index e8fef0304..bb337decf 100644 --- a/extmod/re1.5/recursiveloop.c +++ b/extmod/re1.5/recursiveloop.c @@ -9,7 +9,9 @@ recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int n { const char *old; int off; - + + re1_5_stack_chk(); + for(;;) { if(inst_is_consumer(*pc)) { // If we need to match a character, but there's none left, it's fail From aba1f9167a7db1c0f19d2ab44a8dbfe5c18cc062 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 3 Oct 2017 00:12:06 +0300 Subject: [PATCH 073/163] extmod/modure: Add stack overflow checking when executing a regex. --- extmod/modure.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/modure.c b/extmod/modure.c index 2baebdecc..7e1c24325 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -32,9 +32,12 @@ #include "py/runtime.h" #include "py/binary.h" #include "py/objstr.h" +#include "py/stackctrl.h" #if MICROPY_PY_URE +#define re1_5_stack_chk() MP_STACK_CHECK() + #include "re1.5/re1.5.h" #define FLAG_DEBUG 0x1000 From 8e0b9f495b1967bf7503bfe4e66687955eee1922 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 3 Oct 2017 00:24:32 +0300 Subject: [PATCH 074/163] tests/extmod: Add test for ure regexes leading to infinite recursion. These now should be caught properly and lead to RuntimeError instead of crash. --- tests/extmod/ure_stack_overflow.py | 13 +++++++++++++ tests/extmod/ure_stack_overflow.py.exp | 1 + 2 files changed, 14 insertions(+) create mode 100644 tests/extmod/ure_stack_overflow.py create mode 100644 tests/extmod/ure_stack_overflow.py.exp diff --git a/tests/extmod/ure_stack_overflow.py b/tests/extmod/ure_stack_overflow.py new file mode 100644 index 000000000..d3ce0c5a7 --- /dev/null +++ b/tests/extmod/ure_stack_overflow.py @@ -0,0 +1,13 @@ +try: + import ure as re +except ImportError: + try: + import re + except ImportError: + print("SKIP") + raise SystemExit + +try: + re.match("(a*)*", "aaa") +except RuntimeError: + print("RuntimeError") diff --git a/tests/extmod/ure_stack_overflow.py.exp b/tests/extmod/ure_stack_overflow.py.exp new file mode 100644 index 000000000..8a2b9bfdd --- /dev/null +++ b/tests/extmod/ure_stack_overflow.py.exp @@ -0,0 +1 @@ +RuntimeError From 01978648fdc317c13b17ba186c82df4fb8a5cbac Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 17:26:45 +1100 Subject: [PATCH 075/163] py/objset: Simplify set and frozenset by separating their locals dicts. A lot of set's methods (the mutable ones) are not allowed to operate on a frozenset, and giving frozenset a separate locals dict with only the methods that it supports allows to simplify the logic that verifies if args are a set or a frozenset. Even though the new frozenset locals dict is relatively large (88 bytes on 32-bit archs) there is a much bigger saving coming from the removal of a const string for an error message, along with the removal of some checks for set or frozenset type. Changes in code size due to this patch are (for ports that changed at all): unix x64: -56 unix nanbox: -304 stm32: -64 esp8266: -124 cc3200: -40 Apart from the reduced code, frozenset now has better tab-completion because it only lists the valid methods. And the error message for accessing an invalid method is now more detailed (it includes the method name that wasn't found). --- py/objset.c | 71 ++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/py/objset.c b/py/objset.c index d4a8a1a39..6dede887c 100644 --- a/py/objset.c +++ b/py/objset.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013-2017 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -57,23 +57,13 @@ STATIC bool is_set_or_frozenset(mp_obj_t o) { ; } +// This macro is shorthand for mp_check_self to verify the argument is a set. +#define check_set(o) mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set)) + // This macro is shorthand for mp_check_self to verify the argument is a // set or frozenset for methods that operate on both of these types. #define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o)) -// This function is used to verify the argument for methods that modify -// the set object, and raises an exception if the arg is a frozenset. -STATIC void check_set(mp_obj_t o) { - #if MICROPY_PY_BUILTINS_FROZENSET - if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) { - // Mutable method called on frozenset; emulate CPython behavior, eg: - // AttributeError: 'frozenset' object has no attribute 'add' - mp_raise_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"); - } - #endif - mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set)); -} - STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); @@ -188,26 +178,16 @@ STATIC mp_obj_t set_clear(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); -STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) { +STATIC mp_obj_t set_copy(mp_obj_t self_in) { + check_set_or_frozenset(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_set_t *other = m_new_obj(mp_obj_set_t); - other->base.type = &mp_type_set; + other->base.type = self->base.type; mp_set_init(&other->set, self->set.alloc); other->set.used = self->set.used; memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); - return MP_OBJ_FROM_PTR(other); } - -STATIC mp_obj_t set_copy(mp_obj_t self_in) { - check_set_or_frozenset(self_in); - - mp_obj_t other = set_copy_as_mutable(self_in); - ((mp_obj_base_t*)MP_OBJ_TO_PTR(other))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type; - - return other; -} STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy); STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) { @@ -224,25 +204,23 @@ STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) { check_set(args[0]); self = args[0]; } else { - check_set_or_frozenset(args[0]); - self = set_copy_as_mutable(args[0]); + self = set_copy(args[0]); } - for (size_t i = 1; i < n_args; i++) { mp_obj_t other = args[i]; if (self == other) { set_clear(self); } else { + mp_set_t *self_set = &((mp_obj_set_t*)MP_OBJ_TO_PTR(self))->set; mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { - set_discard(self, next); + mp_set_lookup(self_set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND); } } } - ((mp_obj_base_t*)MP_OBJ_TO_PTR(self))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]))->type; return self; } @@ -333,16 +311,16 @@ STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in)); cleanup_other = true; } - bool out = true; + mp_obj_t out = mp_const_true; if (proper && self->set.used == other->set.used) { - out = false; + out = mp_const_false; } else { mp_obj_iter_buf_t iter_buf; mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf); mp_obj_t next; while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { - out = false; + out = mp_const_false; break; } } @@ -354,7 +332,7 @@ STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool if (cleanup_other) { set_clear(MP_OBJ_FROM_PTR(other)); } - return mp_obj_new_bool(out); + return out; } STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { return set_issubset_internal(self_in, other_in, false); @@ -409,7 +387,7 @@ STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) { - check_set(self_in); + check_set_or_frozenset(self_in); // can be frozenset due to call from set_symmetric_difference mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t iter = mp_getiter(other_in, NULL); mp_obj_t next; @@ -421,10 +399,8 @@ STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update); STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { - check_set_or_frozenset(self_in); - mp_obj_t self_out = set_copy_as_mutable(self_in); + mp_obj_t self_out = set_copy(self_in); set_symmetric_difference_update(self_out, other_in); - ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_out))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type; return self_out; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference); @@ -578,6 +554,19 @@ const mp_obj_type_t mp_type_set = { }; #if MICROPY_PY_BUILTINS_FROZENSET +STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) }, + { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) }, + { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) }, + { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) }, + { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) }, + { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) }, + { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) }, + { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table); + const mp_obj_type_t mp_type_frozenset = { { &mp_type_type }, .name = MP_QSTR_frozenset, @@ -586,7 +575,7 @@ const mp_obj_type_t mp_type_frozenset = { .unary_op = set_unary_op, .binary_op = set_binary_op, .getiter = set_getiter, - .locals_dict = (mp_obj_dict_t*)&set_locals_dict, + .locals_dict = (mp_obj_dict_t*)&frozenset_locals_dict, }; #endif From 2ac1364688cd3ee313661e82a336663551986fc8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 17:56:27 +1100 Subject: [PATCH 076/163] py/objset: Check that RHS of a binary op is a set/frozenset. CPython docs explicitly state that the RHS of a set/frozenset binary op must be a set to prevent user errors. It also preserves commutativity of the ops, eg: "abc" & set() is a TypeError, and so should be set() & "abc". This change actually decreases unix (x64) code by 160 bytes; it increases stm32 by 4 bytes and esp8266 by 28 bytes (but previous patch already introduced a much large saving). --- py/objset.c | 4 ++++ tests/basics/set_binop.py | 12 ++++++++++++ tests/misc/non_compliant.py | 12 ------------ tests/misc/non_compliant.py.exp | 2 -- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/py/objset.c b/py/objset.c index 6dede887c..80ed26334 100644 --- a/py/objset.c +++ b/py/objset.c @@ -463,6 +463,10 @@ STATIC mp_obj_t set_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { #else bool update = true; #endif + if (op != MP_BINARY_OP_IN && !is_set_or_frozenset(rhs)) { + // For all ops except containment the RHS must be a set/frozenset + return MP_OBJ_NULL; + } switch (op) { case MP_BINARY_OP_OR: return set_union(lhs, rhs); diff --git a/tests/basics/set_binop.py b/tests/basics/set_binop.py index 7848920b6..bc76533b1 100644 --- a/tests/basics/set_binop.py +++ b/tests/basics/set_binop.py @@ -47,6 +47,18 @@ s1 -= set('ad') print(s1 is s2, len(s1)) +# RHS must be a set +try: + print(set('12') >= '1') +except TypeError: + print('TypeError') + +# RHS must be a set +try: + print(set('12') <= '123') +except TypeError: + print('TypeError') + # unsupported operator try: set('abc') * 2 diff --git a/tests/misc/non_compliant.py b/tests/misc/non_compliant.py index b4c90e9fc..152633c3b 100644 --- a/tests/misc/non_compliant.py +++ b/tests/misc/non_compliant.py @@ -39,18 +39,6 @@ except NotImplementedError: print('NotImplementedError') -# should raise type error -try: - print(set('12') >= '1') -except TypeError: - print('TypeError') - -# should raise type error -try: - print(set('12') <= '123') -except TypeError: - print('TypeError') - # uPy raises TypeError, shold be ValueError try: '%c' % b'\x01\x02' diff --git a/tests/misc/non_compliant.py.exp b/tests/misc/non_compliant.py.exp index ba5590acc..9c157fd5b 100644 --- a/tests/misc/non_compliant.py.exp +++ b/tests/misc/non_compliant.py.exp @@ -3,8 +3,6 @@ AttributeError TypeError NotImplementedError NotImplementedError -True -True TypeError, ValueError NotImplementedError NotImplementedError From 1394258f378e50966daa1d83053067eeee8ef07d Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 18:03:06 +1100 Subject: [PATCH 077/163] py/objset: Include the failed key in a KeyError raised from set.remove. --- py/objset.c | 2 +- tests/basics/set_remove.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/py/objset.c b/py/objset.c index 80ed26334..aefc26aac 100644 --- a/py/objset.c +++ b/py/objset.c @@ -380,7 +380,7 @@ STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) { check_set(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) { - nlr_raise(mp_obj_new_exception(&mp_type_KeyError)); + nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, item)); } return mp_const_none; } diff --git a/tests/basics/set_remove.py b/tests/basics/set_remove.py index 5627516c4..072723911 100644 --- a/tests/basics/set_remove.py +++ b/tests/basics/set_remove.py @@ -4,8 +4,8 @@ print(list(s)) try: print(s.remove(1), "!!!") -except KeyError: - pass +except KeyError as er: + print('KeyError', er.args[0]) else: print("failed to raise KeyError") From 54acd0b0f050a99e9a5472b8d54cc729a60c3bc8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 19:59:28 +1100 Subject: [PATCH 078/163] drivers/nrf24l01: Make nRF24L01 test script more portable. --- drivers/nrf24l01/nrf24l01test.py | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/drivers/nrf24l01/nrf24l01test.py b/drivers/nrf24l01/nrf24l01test.py index a25194d38..5413511c3 100644 --- a/drivers/nrf24l01/nrf24l01test.py +++ b/drivers/nrf24l01/nrf24l01test.py @@ -1,8 +1,8 @@ """Test for nrf24l01 module.""" import struct -import pyb -from pyb import Pin, SPI +import utime +from machine import Pin, SPI from nrf24l01 import NRF24L01 pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2') @@ -24,7 +24,7 @@ def master(): while num_successes < num_needed and num_failures < num_needed: # stop listening and send packet nrf.stop_listening() - millis = pyb.millis() + millis = utime.ticks_ms() led_state = max(1, (led_state << 1) & 0x0f) print('sending:', millis, led_state) try: @@ -36,10 +36,10 @@ def master(): nrf.start_listening() # wait for response, with 250ms timeout - start_time = pyb.millis() + start_time = utime.ticks_ms() timeout = False while not nrf.any() and not timeout: - if pyb.elapsed_millis(start_time) > 250: + if utime.ticks_diff(utime.ticks_ms(), start_time) > 250: timeout = True if timeout: @@ -51,11 +51,11 @@ def master(): got_millis, = struct.unpack('i', nrf.recv()) # print response and round-trip delay - print('got response:', got_millis, '(delay', pyb.millis() - got_millis, 'ms)') + print('got response:', got_millis, '(delay', utime.ticks_diff(utime.ticks_ms(), got_millis), 'ms)') num_successes += 1 # delay then loop - pyb.delay(250) + utime.sleep_ms(250) print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures)) @@ -69,18 +69,19 @@ def slave(): print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)') while True: - pyb.wfi() + machine.idle() if nrf.any(): while nrf.any(): buf = nrf.recv() millis, led_state = struct.unpack('ii', buf) print('received:', millis, led_state) - for i in range(4): - if led_state & (1 << i): - pyb.LED(i + 1).on() + for led in leds: + if led_state & 1: + led.on() else: - pyb.LED(i + 1).off() - pyb.delay(15) + led.off() + led_state >>= 1 + utime.sleep_ms(15) nrf.stop_listening() try: @@ -90,6 +91,12 @@ def slave(): print('sent response') nrf.start_listening() +try: + import pyb + leds = [pyb.LED(i + 1) for i in range(4)] +except: + leds = [] + print('NRF24L01 test module loaded') print('NRF24L01 pinout for test:') print(' CE on Y4') From b00040c43c156faa3178b44f286f49183021c823 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 19:36:49 +1100 Subject: [PATCH 079/163] esp8266/esp_mphal: Send data in chunks to mp_uos_dupterm_tx_strn. Sending byte-by-byte is inefficient and leads to errors in the WebSocket protocol when sending utf-8 encoded characters. --- ports/esp8266/esp_mphal.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 61848fd34..ec4751e16 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -72,11 +72,6 @@ int mp_hal_stdin_rx_chr(void) { } } -void mp_hal_stdout_tx_char(char c) { - uart_tx_one_char(UART0, c); - mp_uos_dupterm_tx_strn(&c, 1); -} - #if 0 void mp_hal_debug_str(const char *str) { while (*str) { @@ -87,23 +82,39 @@ void mp_hal_debug_str(const char *str) { #endif void mp_hal_stdout_tx_str(const char *str) { + const char *last = str; while (*str) { - mp_hal_stdout_tx_char(*str++); + uart_tx_one_char(UART0, *str++); } + mp_uos_dupterm_tx_strn(last, str - last); } void mp_hal_stdout_tx_strn(const char *str, uint32_t len) { + const char *last = str; while (len--) { - mp_hal_stdout_tx_char(*str++); + uart_tx_one_char(UART0, *str++); } + mp_uos_dupterm_tx_strn(last, str - last); } void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) { + const char *last = str; while (len--) { if (*str == '\n') { - mp_hal_stdout_tx_char('\r'); + if (str > last) { + mp_uos_dupterm_tx_strn(last, str - last); + } + uart_tx_one_char(UART0, '\r'); + uart_tx_one_char(UART0, '\n'); + mp_uos_dupterm_tx_strn("\r\n", 2); + ++str; + last = str; + } else { + uart_tx_one_char(UART0, *str++); } - mp_hal_stdout_tx_char(*str++); + } + if (str > last) { + mp_uos_dupterm_tx_strn(last, str - last); } } From 3289b9b7a76a1230b6bb631e191a47bfc6c7a8ee Mon Sep 17 00:00:00 2001 From: chrismas9 Date: Sun, 10 Sep 2017 00:51:44 +1000 Subject: [PATCH 080/163] py/{mkenv.mk,mkrules.mk}: Append .exe for Windows executable files. Building mpy-cross: this patch adds .exe to the PROG name when building executables for host (eg mpy-cross) on Windows. make clean now removes mpy-cross.exe under Windows. Building MicroPython: this patch sets MPY_CROSS to mpy-cross.exe or mpy-cross so they can coexist and use cygwin or WSL without rebuilding mpy-cross. The dependency in the mpy rule now uses mpy-cross.exe for Windows and mpy-cross for Linux. --- py/mkenv.mk | 6 ++++++ py/mkrules.mk | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/py/mkenv.mk b/py/mkenv.mk index b167b2533..8b637e9ac 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -59,7 +59,13 @@ LD += -m32 endif MAKE_FROZEN = $(TOP)/tools/make-frozen.py +# allow mpy-cross (for WSL) and mpy-cross.exe (for cygwin) to coexist +ifeq ($(OS),Windows_NT) +MPY_CROSS = $(TOP)/mpy-cross/mpy-cross.exe +PROG_EXT = .exe +else MPY_CROSS = $(TOP)/mpy-cross/mpy-cross +endif MPY_TOOL = $(TOP)/tools/mpy-tool.py all: diff --git a/py/mkrules.mk b/py/mkrules.mk index 13545eb6f..fd579557f 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -111,7 +111,7 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) # to build .mpy files from .py files -$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py $(TOP)/mpy-cross/mpy-cross +$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py $(MPY_CROSS) @$(ECHO) "MPY $<" $(Q)$(MKDIR) -p $(dir $@) $(Q)$(MPY_CROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $< @@ -133,13 +133,13 @@ $(PROG): $(OBJ) # we may want to compile using Thumb, but link with non-Thumb libc. $(Q)$(CC) -o $@ $^ $(LIB) $(LDFLAGS) ifndef DEBUG - $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG) + $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG)$(PROG_EXT) endif - $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o") $(PROG) + $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o") $(PROG)$(PROG_EXT) clean: clean-prog clean-prog: - $(RM) -f $(PROG) + $(RM) -f $(PROG)$(PROG_EXT) $(RM) -f $(PROG).map .PHONY: clean-prog From 9e0cdb22f1a549d5e542418a3007c756cd594b84 Mon Sep 17 00:00:00 2001 From: Gabe Date: Wed, 27 Sep 2017 15:49:51 -0400 Subject: [PATCH 081/163] docs/esp8266/tutorial: Update neopixel with example of using 4 bbp. --- docs/esp8266/tutorial/neopixel.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/esp8266/tutorial/neopixel.rst b/docs/esp8266/tutorial/neopixel.rst index 245aed6d4..a1537526f 100644 --- a/docs/esp8266/tutorial/neopixel.rst +++ b/docs/esp8266/tutorial/neopixel.rst @@ -20,6 +20,20 @@ To set the colour of pixels use:: >>> np[1] = (0, 128, 0) # set to green, half brightness >>> np[2] = (0, 0, 64) # set to blue, quarter brightness +For LEDs with more than 3 colours, such as RGBW pixels or RGBY pixels, the +NeoPixel class takes a ``bpp`` parameter. To setup a NeoPixel object for an +RGBW Pixel, do the following:: + + >>> import machine, neopixel + >>> np = neopixel.NeoPixel(machine.Pin(4), 8, bpp=4) + +In a 4-bpp mode, remember to use 4-tuples instead of 3-tuples to set the colour. +For example to set the first three pixels use:: + + >>> np[0] = (255, 0, 0, 128) # Orange in an RGBY Setup + >>> np[1] = (0, 255, 0, 128) # Yellow-green in an RGBY Setup + >>> np[2] = (0, 0, 255, 128) # Green-blue in an RGBY Setup + Then use the ``write()`` method to output the colours to the LEDs:: >>> np.write() From 6c82cfc089466f0b3f4b61c81baee1cabece1eaa Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 11:31:05 +1100 Subject: [PATCH 082/163] py/objtype: Change type of enum-to-qstr table to uint16_t to save space. Qstr values fit in 16-bits (and this fact is used elsewhere in the code) so no need to use more than that for the large lookup tables. The compiler will anyway give a warning if the qstr values don't fit in 16 bits. Saves around 80 bytes of code space for Thumb2 archs. --- py/objtype.c | 4 ++-- py/runtime.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index cf9311d5c..033765a47 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -334,7 +334,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size return MP_OBJ_FROM_PTR(o); } -const qstr mp_unary_op_method_name[] = { +const uint16_t mp_unary_op_method_name[] = { [MP_UNARY_OP_BOOL] = MP_QSTR___bool__, [MP_UNARY_OP_LEN] = MP_QSTR___len__, [MP_UNARY_OP_HASH] = MP_QSTR___hash__, @@ -408,7 +408,7 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } } -const qstr mp_binary_op_method_name[] = { +const uint16_t mp_binary_op_method_name[] = { /* MP_BINARY_OP_OR, MP_BINARY_OP_XOR, diff --git a/py/runtime.h b/py/runtime.h index bbdd7647e..da12f8f04 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -57,9 +57,9 @@ typedef struct _mp_arg_t { mp_arg_val_t defval; } mp_arg_t; -// defined in objtype.c -extern const qstr mp_unary_op_method_name[]; -extern const qstr mp_binary_op_method_name[]; +// Tables mapping operator enums to qstrs, defined in objtype.c +extern const uint16_t mp_unary_op_method_name[]; +extern const uint16_t mp_binary_op_method_name[]; void mp_init(void); void mp_deinit(void); From a3dc1b1957d2c96d7c60c2c629c95077b03488a1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 12:37:50 +1100 Subject: [PATCH 083/163] all: Remove inclusion of internal py header files. Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed. --- drivers/cc3000/src/ccspi.c | 2 -- extmod/machine_mem.c | 2 +- extmod/modbtree.c | 2 -- extmod/modframebuf.c | 2 -- extmod/modlwip.c | 1 - extmod/modubinascii.c | 1 - extmod/moductypes.c | 1 - extmod/moduhashlib.c | 1 - extmod/moduheapq.c | 2 -- extmod/modujson.c | 1 - extmod/modurandom.c | 1 - extmod/modure.c | 1 - extmod/modussl_axtls.c | 1 - extmod/modussl_mbedtls.c | 2 -- extmod/modutimeq.c | 2 -- extmod/moduzlib.c | 1 - extmod/modwebrepl.c | 2 -- extmod/modwebsocket.c | 2 -- extmod/uos_dupterm.c | 1 - extmod/vfs.c | 1 - extmod/vfs_fat.c | 1 - extmod/vfs_fat_file.c | 1 - extmod/vfs_fat_misc.c | 1 - lib/netutils/netutils.c | 2 -- lib/utils/pyexec.c | 1 - mpy-cross/main.c | 1 - ports/bare-arm/main.c | 1 - ports/cc3200/ftp/ftp.c | 3 +-- ports/cc3200/hal/cc3200_hal.c | 1 - ports/cc3200/misc/mpexception.c | 1 - ports/cc3200/mods/modmachine.c | 1 - ports/cc3200/mods/modnetwork.c | 3 --- ports/cc3200/mods/modubinascii.c | 2 -- ports/cc3200/mods/moduhashlib.c | 1 - ports/cc3200/mods/moduos.c | 2 -- ports/cc3200/mods/pybadc.c | 2 -- ports/cc3200/mods/pybi2c.c | 1 - ports/cc3200/mods/pybpin.c | 3 --- ports/cc3200/mods/pybsleep.c | 1 - ports/cc3200/mods/pybspi.c | 1 - ports/cc3200/mods/pybtimer.c | 3 --- ports/cc3200/mods/pybuart.c | 3 --- ports/cc3200/mpthreadport.c | 2 -- ports/cc3200/util/gccollect.c | 2 -- ports/esp8266/esp_mphal.c | 2 -- ports/esp8266/machine_adc.c | 2 -- ports/esp8266/machine_pin.c | 1 - ports/esp8266/machine_pwm.c | 1 - ports/esp8266/machine_rtc.c | 2 -- ports/esp8266/machine_wdt.c | 2 -- ports/esp8266/main.c | 2 -- ports/esp8266/modnetwork.c | 1 - ports/esp8266/modutime.c | 2 -- ports/minimal/main.c | 1 - ports/qemu-arm/main.c | 2 -- ports/qemu-arm/test_main.c | 2 -- ports/stm32/accel.c | 1 - ports/stm32/adc.c | 1 - ports/stm32/can.c | 1 - ports/stm32/gccollect.c | 1 - ports/stm32/i2c.c | 1 - ports/stm32/irq.c | 1 - ports/stm32/lcd.c | 1 - ports/stm32/led.c | 1 - ports/stm32/modmachine.h | 2 -- ports/stm32/modnetwork.c | 1 - ports/stm32/modnwcc3k.c | 1 - ports/stm32/modnwwiznet5k.c | 1 - ports/stm32/modpyb.c | 2 -- ports/stm32/moduos.c | 1 - ports/stm32/modusocket.c | 1 - ports/stm32/mphalport.c | 1 - ports/stm32/mpthreadport.c | 2 -- ports/stm32/pendsv.c | 1 - ports/stm32/pin.c | 1 - ports/stm32/sdcard.c | 1 - ports/stm32/spi.c | 1 - ports/stm32/stm32_it.c | 1 - ports/stm32/uart.c | 1 - ports/teensy/led.c | 1 - ports/teensy/teensy_hal.c | 1 - ports/teensy/timer.c | 1 - ports/teensy/uart.c | 1 - ports/unix/file.c | 1 - ports/unix/main.c | 2 -- ports/unix/modffi.c | 1 - ports/unix/modjni.c | 2 -- ports/unix/modos.c | 1 - ports/unix/modsocket.c | 1 - ports/unix/mpthreadport.c | 1 - ports/unix/unix_mphal.c | 1 - ports/zephyr/machine_pin.c | 1 - ports/zephyr/main.c | 1 - py/argcheck.c | 1 - py/bc.c | 4 +--- py/bc.h | 1 - py/builtinevex.c | 1 - py/builtinimport.c | 1 - py/emit.h | 1 - py/emitnative.c | 1 - py/gc.c | 2 -- py/lexer.c | 1 - py/map.c | 1 - py/modbuiltins.c | 2 -- py/modmicropython.c | 1 - py/modsys.c | 3 --- py/nativeglue.c | 2 -- py/nlrthumb.c | 1 - py/nlrx64.c | 1 - py/nlrx86.c | 2 -- py/nlrxtensa.c | 1 - py/obj.c | 2 -- py/objarray.c | 2 -- py/objbool.c | 2 -- py/objcomplex.c | 3 --- py/objdict.c | 3 --- py/objexcept.c | 1 - py/objfloat.c | 2 -- py/objfun.c | 2 -- py/objgenerator.c | 2 -- py/objgetitemiter.c | 1 - py/objint.c | 2 -- py/objint_longlong.c | 2 -- py/objint_mpz.c | 2 -- py/objlist.c | 2 -- py/objmodule.c | 2 -- py/objnamedtuple.c | 1 - py/objnone.c | 2 -- py/objpolyiter.c | 1 - py/objproperty.c | 1 - py/objrange.c | 2 -- py/objreversed.c | 1 - py/objset.c | 2 -- py/objsingleton.c | 2 -- py/objslice.c | 2 -- py/objstr.c | 2 -- py/objstringio.c | 1 - py/objstrunicode.c | 2 -- py/objtuple.c | 2 -- py/objtype.c | 2 -- py/parse.c | 2 -- py/persistentcode.c | 1 - py/runtime.c | 3 --- py/runtime.h | 1 - py/runtime_utils.c | 2 -- py/sequence.c | 3 --- py/stackctrl.c | 3 --- py/stream.c | 1 - py/vm.c | 2 -- 149 files changed, 3 insertions(+), 226 deletions(-) diff --git a/drivers/cc3000/src/ccspi.c b/drivers/cc3000/src/ccspi.c index 64900efe4..820be809b 100644 --- a/drivers/cc3000/src/ccspi.c +++ b/drivers/cc3000/src/ccspi.c @@ -34,8 +34,6 @@ #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "pin.h" #include "led.h" diff --git a/extmod/machine_mem.c b/extmod/machine_mem.c index af987cb7f..b9f16507c 100644 --- a/extmod/machine_mem.c +++ b/extmod/machine_mem.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ +#include "py/runtime.h" #include "extmod/machine_mem.h" -#include "py/nlr.h" #if MICROPY_PY_MACHINE diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 5017079da..5c1311532 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -29,9 +29,7 @@ #include // for declaration of global errno variable #include -#include "py/nlr.h" #include "py/runtime.h" -#include "py/runtime0.h" #include "py/stream.h" #if MICROPY_PY_BTREE diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 239302295..20e40d579 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -27,8 +27,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #if MICROPY_PY_FRAMEBUF diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 6b8caa42b..f7e776af9 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -29,7 +29,6 @@ #include #include -#include "py/nlr.h" #include "py/objlist.h" #include "py/runtime.h" #include "py/stream.h" diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index f15efea0f..8256a50cf 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "extmod/modubinascii.h" diff --git a/extmod/moductypes.c b/extmod/moductypes.c index dc03f6de5..c3da083cf 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/objtuple.h" #include "py/binary.h" diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index f3beb3939..3fad69247 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #if MICROPY_PY_UHASHLIB diff --git a/extmod/moduheapq.c b/extmod/moduheapq.c index 4a620bad8..71c15368b 100644 --- a/extmod/moduheapq.c +++ b/extmod/moduheapq.c @@ -24,9 +24,7 @@ * THE SOFTWARE. */ -#include "py/nlr.h" #include "py/objlist.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_UHEAPQ diff --git a/extmod/modujson.c b/extmod/modujson.c index 6c4aa1611..f14682d26 100644 --- a/extmod/modujson.c +++ b/extmod/modujson.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/objlist.h" #include "py/objstringio.h" #include "py/parsenum.h" diff --git a/extmod/modurandom.c b/extmod/modurandom.c index 4b63dace4..1512a3fd4 100644 --- a/extmod/modurandom.c +++ b/extmod/modurandom.c @@ -27,7 +27,6 @@ #include #include -//#include "py/nlr.h" #include "py/runtime.h" #if MICROPY_PY_URANDOM diff --git a/extmod/modure.c b/extmod/modure.c index 7e1c24325..78de4706d 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "py/objstr.h" diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index b5d2412d2..88a89a23d 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index a65470e16..d7316cb4a 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -31,10 +31,8 @@ #include #include // needed because mp_is_nonblocking_error uses system error codes -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" -#include "py/obj.h" // mbedtls_time_t #include "mbedtls/platform.h" diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index faa589583..94cbd20d2 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -27,9 +27,7 @@ #include -#include "py/nlr.h" #include "py/objlist.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/smallint.h" diff --git a/extmod/moduzlib.c b/extmod/moduzlib.c index b446dba73..e9af07370 100644 --- a/extmod/moduzlib.c +++ b/extmod/moduzlib.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index d618f5370..4ff282aac 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -28,8 +28,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/stream.h" #include "py/builtin.h" diff --git a/extmod/modwebsocket.c b/extmod/modwebsocket.c index 6c6e32c1a..a651164b2 100644 --- a/extmod/modwebsocket.c +++ b/extmod/modwebsocket.c @@ -28,8 +28,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/stream.h" #include "extmod/modwebsocket.h" diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c index 233d145aa..1d6f02dce 100644 --- a/extmod/uos_dupterm.c +++ b/extmod/uos_dupterm.c @@ -27,7 +27,6 @@ #include #include "py/mpconfig.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/objtuple.h" #include "py/objarray.h" diff --git a/extmod/vfs.c b/extmod/vfs.c index 3bdce80db..a1cd8d5f6 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -27,7 +27,6 @@ #include #include -#include "py/runtime0.h" #include "py/runtime.h" #include "py/objstr.h" #include "py/mperrno.h" diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index b27054111..22346bdf1 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -33,7 +33,6 @@ #endif #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mperrno.h" #include "lib/oofatfs/ff.h" diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 8fb48f01a..1fcbb253d 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -29,7 +29,6 @@ #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 7c16db7e5..9a26b4a2f 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -28,7 +28,6 @@ #if MICROPY_VFS_FAT #include -#include "py/nlr.h" #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" diff --git a/lib/netutils/netutils.c b/lib/netutils/netutils.c index 15e70397c..06c3ff9b0 100644 --- a/lib/netutils/netutils.c +++ b/lib/netutils/netutils.c @@ -29,8 +29,6 @@ #include #include -#include "py/obj.h" -#include "py/nlr.h" #include "py/runtime.h" #include "lib/netutils/netutils.h" diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index d3500b42b..1e99aa649 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -29,7 +29,6 @@ #include #include -#include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" diff --git a/mpy-cross/main.c b/mpy-cross/main.c index 225bc4309..d819f74f1 100644 --- a/mpy-cross/main.c +++ b/mpy-cross/main.c @@ -29,7 +29,6 @@ #include #include -#include "py/mpstate.h" #include "py/compile.h" #include "py/persistentcode.h" #include "py/runtime.h" diff --git a/ports/bare-arm/main.c b/ports/bare-arm/main.c index 938414dfe..b96fb47ac 100644 --- a/ports/bare-arm/main.c +++ b/ports/bare-arm/main.c @@ -2,7 +2,6 @@ #include #include -#include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" diff --git a/ports/cc3200/ftp/ftp.c b/ports/cc3200/ftp/ftp.c index 5461f9180..ee80e51f5 100644 --- a/ports/cc3200/ftp/ftp.c +++ b/ports/cc3200/ftp/ftp.c @@ -27,8 +27,7 @@ #include #include -#include "py/mpstate.h" -#include "py/obj.h" +#include "py/runtime.h" #include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" diff --git a/ports/cc3200/hal/cc3200_hal.c b/ports/cc3200/hal/cc3200_hal.c index 9718980e7..0285d0585 100644 --- a/ports/cc3200/hal/cc3200_hal.c +++ b/ports/cc3200/hal/cc3200_hal.c @@ -33,7 +33,6 @@ #include -#include "py/mpstate.h" #include "py/mphal.h" #include "py/runtime.h" #include "py/objstr.h" diff --git a/ports/cc3200/misc/mpexception.c b/ports/cc3200/misc/mpexception.c index 2dfcbfdef..72d4a155f 100644 --- a/ports/cc3200/misc/mpexception.c +++ b/ports/cc3200/misc/mpexception.c @@ -28,7 +28,6 @@ #include #include -#include "py/mpstate.h" #include "mpexception.h" diff --git a/ports/cc3200/mods/modmachine.c b/ports/cc3200/mods/modmachine.c index fb0fe7f2c..6051497e3 100644 --- a/ports/cc3200/mods/modmachine.c +++ b/ports/cc3200/mods/modmachine.c @@ -27,7 +27,6 @@ #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mphal.h" #include "irq.h" diff --git a/ports/cc3200/mods/modnetwork.c b/ports/cc3200/mods/modnetwork.c index 0234a0ccb..37dffe731 100644 --- a/ports/cc3200/mods/modnetwork.c +++ b/ports/cc3200/mods/modnetwork.c @@ -25,9 +25,6 @@ * THE SOFTWARE. */ -#include "py/mpstate.h" -#include "py/obj.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" diff --git a/ports/cc3200/mods/modubinascii.c b/ports/cc3200/mods/modubinascii.c index 7f51a8f3d..6b020ab39 100644 --- a/ports/cc3200/mods/modubinascii.c +++ b/ports/cc3200/mods/modubinascii.c @@ -25,8 +25,6 @@ * THE SOFTWARE. */ -#include "py/mpconfig.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "extmod/modubinascii.h" diff --git a/ports/cc3200/mods/moduhashlib.c b/ports/cc3200/mods/moduhashlib.c index bec88d7ca..96f514927 100644 --- a/ports/cc3200/mods/moduhashlib.c +++ b/ports/cc3200/mods/moduhashlib.c @@ -30,7 +30,6 @@ #include "py/mpconfig.h" #include MICROPY_HAL_H -#include "py/nlr.h" #include "py/runtime.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" diff --git a/ports/cc3200/mods/moduos.c b/ports/cc3200/mods/moduos.c index ba8fd86a6..7d99c8e80 100644 --- a/ports/cc3200/mods/moduos.c +++ b/ports/cc3200/mods/moduos.c @@ -28,8 +28,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/objtuple.h" #include "py/objstr.h" #include "py/runtime.h" diff --git a/ports/cc3200/mods/pybadc.c b/ports/cc3200/mods/pybadc.c index 850664cab..c73b8c149 100644 --- a/ports/cc3200/mods/pybadc.c +++ b/ports/cc3200/mods/pybadc.c @@ -28,8 +28,6 @@ #include #include -#include "py/mpconfig.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "py/gc.h" diff --git a/ports/cc3200/mods/pybi2c.c b/ports/cc3200/mods/pybi2c.c index efb78a44d..d08627fa4 100644 --- a/ports/cc3200/mods/pybi2c.c +++ b/ports/cc3200/mods/pybi2c.c @@ -28,7 +28,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" diff --git a/ports/cc3200/mods/pybpin.c b/ports/cc3200/mods/pybpin.c index 2402332d5..c877433e9 100644 --- a/ports/cc3200/mods/pybpin.c +++ b/ports/cc3200/mods/pybpin.c @@ -29,11 +29,8 @@ #include #include -#include "py/mpconfig.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/gc.h" -#include "py/mpstate.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" diff --git a/ports/cc3200/mods/pybsleep.c b/ports/cc3200/mods/pybsleep.c index a7488c5f1..798c6538b 100644 --- a/ports/cc3200/mods/pybsleep.c +++ b/ports/cc3200/mods/pybsleep.c @@ -27,7 +27,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mphal.h" #include "inc/hw_types.h" diff --git a/ports/cc3200/mods/pybspi.c b/ports/cc3200/mods/pybspi.c index 8537f649f..27591e4f4 100644 --- a/ports/cc3200/mods/pybspi.c +++ b/ports/cc3200/mods/pybspi.c @@ -28,7 +28,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mperrno.h" #include "bufhelper.h" diff --git a/ports/cc3200/mods/pybtimer.c b/ports/cc3200/mods/pybtimer.c index f3a12c5e7..ea795b848 100644 --- a/ports/cc3200/mods/pybtimer.c +++ b/ports/cc3200/mods/pybtimer.c @@ -29,9 +29,6 @@ #include #include -#include "py/mpconfig.h" -#include "py/obj.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/gc.h" #include "py/mperrno.h" diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index 135e0f2a3..35c0de9f9 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -29,8 +29,6 @@ #include #include -#include "py/mpconfig.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/objlist.h" #include "py/stream.h" @@ -48,7 +46,6 @@ #include "mpirq.h" #include "pybsleep.h" #include "mpexception.h" -#include "py/mpstate.h" #include "osi.h" #include "utils.h" #include "pin.h" diff --git a/ports/cc3200/mpthreadport.c b/ports/cc3200/mpthreadport.c index e77ac4ae5..9dbc518e0 100644 --- a/ports/cc3200/mpthreadport.c +++ b/ports/cc3200/mpthreadport.c @@ -26,8 +26,6 @@ #include -#include "py/mpconfig.h" -#include "py/mpstate.h" #include "py/runtime.h" #include "py/gc.h" #include "py/mpthread.h" diff --git a/ports/cc3200/util/gccollect.c b/ports/cc3200/util/gccollect.c index baee2eeef..6e2a9081c 100644 --- a/ports/cc3200/util/gccollect.c +++ b/ports/cc3200/util/gccollect.c @@ -28,8 +28,6 @@ #include #include -#include "py/mpconfig.h" -#include "py/mpstate.h" #include "py/gc.h" #include "py/mpthread.h" #include "gccollect.h" diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index ec4751e16..71d4c5062 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -31,8 +31,6 @@ #include "esp_mphal.h" #include "user_interface.h" #include "ets_alt_task.h" -#include "py/obj.h" -#include "py/mpstate.h" #include "py/runtime.h" #include "extmod/misc.h" #include "lib/utils/pyexec.h" diff --git a/ports/esp8266/machine_adc.c b/ports/esp8266/machine_adc.c index c8c08695b..b422f0f9e 100644 --- a/ports/esp8266/machine_adc.c +++ b/ports/esp8266/machine_adc.c @@ -27,8 +27,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "user_interface.h" diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c index 3ead3f4c2..14505c8f0 100644 --- a/ports/esp8266/machine_pin.c +++ b/ports/esp8266/machine_pin.c @@ -33,7 +33,6 @@ #include "user_interface.h" #include "gpio.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" diff --git a/ports/esp8266/machine_pwm.c b/ports/esp8266/machine_pwm.c index 8d73e6bb1..4c5cb8727 100644 --- a/ports/esp8266/machine_pwm.c +++ b/ports/esp8266/machine_pwm.c @@ -29,7 +29,6 @@ #include "esppwm.h" -#include "py/nlr.h" #include "py/runtime.h" #include "modmachine.h" diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index f6a13c095..bbfc172cd 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -27,8 +27,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "lib/timeutils/timeutils.h" #include "user_interface.h" diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index 5e23ff782..04b42782e 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -27,8 +27,6 @@ //#include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "user_interface.h" #include "etshal.h" diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c index 957198aa0..7f5dca84e 100644 --- a/ports/esp8266/main.c +++ b/ports/esp8266/main.c @@ -27,9 +27,7 @@ #include #include -#include "py/nlr.h" #include "py/compile.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/mperrno.h" diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index 3acd244b8..2c61e5dcd 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objlist.h" #include "py/runtime.h" #include "py/mphal.h" diff --git a/ports/esp8266/modutime.c b/ports/esp8266/modutime.c index 77dbcfa1f..ab9cb7dc2 100644 --- a/ports/esp8266/modutime.c +++ b/ports/esp8266/modutime.c @@ -28,8 +28,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/gc.h" #include "py/runtime.h" #include "py/mphal.h" diff --git a/ports/minimal/main.c b/ports/minimal/main.c index e28cfe45e..9d43a9cf9 100644 --- a/ports/minimal/main.c +++ b/ports/minimal/main.c @@ -2,7 +2,6 @@ #include #include -#include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" diff --git a/ports/qemu-arm/main.c b/ports/qemu-arm/main.c index d5fbcd84b..d23ef576f 100644 --- a/ports/qemu-arm/main.c +++ b/ports/qemu-arm/main.c @@ -4,10 +4,8 @@ #include #include -#include "py/nlr.h" #include "py/obj.h" #include "py/compile.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/gc.h" diff --git a/ports/qemu-arm/test_main.c b/ports/qemu-arm/test_main.c index 5c07d1607..c018ae428 100644 --- a/ports/qemu-arm/test_main.c +++ b/ports/qemu-arm/test_main.c @@ -4,10 +4,8 @@ #include #include -#include "py/nlr.h" #include "py/obj.h" #include "py/compile.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/gc.h" diff --git a/ports/stm32/accel.c b/ports/stm32/accel.c index 512a6e313..7b36e2082 100644 --- a/ports/stm32/accel.c +++ b/ports/stm32/accel.c @@ -28,7 +28,6 @@ #include #include "py/mphal.h" -#include "py/nlr.h" #include "py/runtime.h" #include "pin.h" #include "genhdr/pins.h" diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 4192e7fed..9a0dc56a3 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "py/mphal.h" diff --git a/ports/stm32/can.c b/ports/stm32/can.c index 2fd593d58..25a608ce9 100644 --- a/ports/stm32/can.c +++ b/ports/stm32/can.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objtuple.h" #include "py/runtime.h" #include "py/gc.h" diff --git a/ports/stm32/gccollect.c b/ports/stm32/gccollect.c index 937fb6f36..cdec2a136 100644 --- a/ports/stm32/gccollect.c +++ b/ports/stm32/gccollect.c @@ -27,7 +27,6 @@ #include #include -#include "py/mpstate.h" #include "py/obj.h" #include "py/gc.h" #include "py/mpthread.h" diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index fcf3cd9c3..b22787cab 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "irq.h" diff --git a/ports/stm32/irq.c b/ports/stm32/irq.c index d6db8e83d..7298a4b50 100644 --- a/ports/stm32/irq.c +++ b/ports/stm32/irq.c @@ -24,7 +24,6 @@ * THE SOFTWARE. */ -#include "py/nlr.h" #include "py/obj.h" #include "py/mphal.h" #include "irq.h" diff --git a/ports/stm32/lcd.c b/ports/stm32/lcd.c index 559616b96..488df1699 100644 --- a/ports/stm32/lcd.c +++ b/ports/stm32/lcd.c @@ -28,7 +28,6 @@ #include #include "py/mphal.h" -#include "py/nlr.h" #include "py/runtime.h" #if MICROPY_HW_HAS_LCD diff --git a/ports/stm32/led.c b/ports/stm32/led.c index e03781bcf..9bbcaa6b3 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "timer.h" diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index ac39f854e..77668695f 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -26,8 +26,6 @@ #ifndef MICROPY_INCLUDED_STMHAL_MODMACHINE_H #define MICROPY_INCLUDED_STMHAL_MODMACHINE_H -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/obj.h" void machine_init(void); diff --git a/ports/stm32/modnetwork.c b/ports/stm32/modnetwork.c index 6b4949a0d..642174532 100644 --- a/ports/stm32/modnetwork.c +++ b/ports/stm32/modnetwork.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objlist.h" #include "py/runtime.h" #include "modnetwork.h" diff --git a/ports/stm32/modnwcc3k.c b/ports/stm32/modnwcc3k.c index 2be5d6c22..551206da8 100644 --- a/ports/stm32/modnwcc3k.c +++ b/ports/stm32/modnwcc3k.c @@ -30,7 +30,6 @@ // CC3000 defines its own ENOBUFS (different to standard one!) #undef ENOBUFS -#include "py/nlr.h" #include "py/objtuple.h" #include "py/objlist.h" #include "py/stream.h" diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index 2d5a8d51a..21e4dbbbf 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objlist.h" #include "py/runtime.h" #include "py/mperrno.h" diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 5dc28e132..176fc8466 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -27,8 +27,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/obj.h" #include "py/gc.h" #include "py/builtin.h" diff --git a/ports/stm32/moduos.c b/ports/stm32/moduos.c index 82ee61726..f661b3b5e 100644 --- a/ports/stm32/moduos.c +++ b/ports/stm32/moduos.c @@ -27,7 +27,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/objtuple.h" #include "py/objstr.h" diff --git a/ports/stm32/modusocket.c b/ports/stm32/modusocket.c index 32dad5ced..c5ab3b6d7 100644 --- a/ports/stm32/modusocket.c +++ b/ports/stm32/modusocket.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/objtuple.h" #include "py/objlist.h" #include "py/runtime.h" diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index dff781ff2..ab3dc227a 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -1,6 +1,5 @@ #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" diff --git a/ports/stm32/mpthreadport.c b/ports/stm32/mpthreadport.c index d7c5b569b..11653b24c 100644 --- a/ports/stm32/mpthreadport.c +++ b/ports/stm32/mpthreadport.c @@ -26,8 +26,6 @@ #include -#include "py/mpconfig.h" -#include "py/mpstate.h" #include "py/gc.h" #include "py/mpthread.h" #include "gccollect.h" diff --git a/ports/stm32/pendsv.c b/ports/stm32/pendsv.c index 00ea12f46..0aeb1a6dc 100644 --- a/ports/stm32/pendsv.c +++ b/ports/stm32/pendsv.c @@ -26,7 +26,6 @@ #include -#include "py/mpstate.h" #include "py/runtime.h" #include "lib/utils/interrupt_char.h" #include "pendsv.h" diff --git a/ports/stm32/pin.c b/ports/stm32/pin.c index b7a2302b0..ee2d84646 100644 --- a/ports/stm32/pin.c +++ b/ports/stm32/pin.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "extmod/virtpin.h" diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 27276332a..484426b84 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "lib/oofatfs/ff.h" diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 654a1327d..cfd9c2667 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "extmod/machine_spi.h" diff --git a/ports/stm32/stm32_it.c b/ports/stm32/stm32_it.c index 2111da1ae..f1ac9b6b8 100644 --- a/ports/stm32/stm32_it.c +++ b/ports/stm32/stm32_it.c @@ -67,7 +67,6 @@ #include -#include "py/mpstate.h" #include "py/obj.h" #include "py/mphal.h" #include "stm32_it.h" diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 1238b4e31..2b2f782f9 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" diff --git a/ports/teensy/led.c b/ports/teensy/led.c index 9159c75ce..add052fad 100644 --- a/ports/teensy/led.c +++ b/ports/teensy/led.c @@ -2,7 +2,6 @@ #include "Arduino.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "led.h" diff --git a/ports/teensy/teensy_hal.c b/ports/teensy/teensy_hal.c index 439e3380d..84d68cff8 100644 --- a/ports/teensy/teensy_hal.c +++ b/ports/teensy/teensy_hal.c @@ -1,7 +1,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mphal.h" #include "usb.h" diff --git a/ports/teensy/timer.c b/ports/teensy/timer.c index cdc7a3c54..b823e6c3b 100644 --- a/ports/teensy/timer.c +++ b/ports/teensy/timer.c @@ -29,7 +29,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" diff --git a/ports/teensy/uart.c b/ports/teensy/uart.c index 768572aff..3d5111217 100644 --- a/ports/teensy/uart.c +++ b/ports/teensy/uart.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "bufhelper.h" #include "uart.h" diff --git a/ports/unix/file.c b/ports/unix/file.c index 0d65f9ca0..84e918082 100644 --- a/ports/unix/file.c +++ b/ports/unix/file.c @@ -31,7 +31,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/stream.h" #include "py/builtin.h" diff --git a/ports/unix/main.c b/ports/unix/main.c index e861d7f11..e1cd33fc1 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -37,8 +37,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" #include "py/builtin.h" diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 9b514371b..78adccac1 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -32,7 +32,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" #include "py/mperrno.h" diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index b7ac49dc8..15b6d9cd7 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -30,8 +30,6 @@ #include #include -#include "py/nlr.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/binary.h" diff --git a/ports/unix/modos.c b/ports/unix/modos.c index 5030fbb65..327116a0a 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -34,7 +34,6 @@ #include #include "py/mpconfig.h" -#include "py/nlr.h" #include "py/runtime.h" #include "py/objtuple.h" #include "py/mphal.h" diff --git a/ports/unix/modsocket.c b/ports/unix/modsocket.c index c612f870d..7e82554c7 100644 --- a/ports/unix/modsocket.c +++ b/ports/unix/modsocket.c @@ -38,7 +38,6 @@ #include #include -#include "py/nlr.h" #include "py/objtuple.h" #include "py/objstr.h" #include "py/runtime.h" diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c index 09f5702e3..8c636a445 100644 --- a/ports/unix/mpthreadport.c +++ b/ports/unix/mpthreadport.c @@ -28,7 +28,6 @@ #include #include -#include "py/mpstate.h" #include "py/runtime.h" #include "py/mpthread.h" #include "py/gc.h" diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 02cdbea11..2b273d834 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -29,7 +29,6 @@ #include #include -#include "py/mpstate.h" #include "py/mphal.h" #include "py/runtime.h" #include "extmod/misc.h" diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index c23ac08f0..4dcd956cf 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -32,7 +32,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index a4301629f..7255d981f 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -33,7 +33,6 @@ #include #endif -#include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" diff --git a/py/argcheck.c b/py/argcheck.c index 0c5c5ca95..add6f8de8 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) { diff --git a/py/bc.c b/py/bc.c index 917eba57d..991b0cf26 100644 --- a/py/bc.c +++ b/py/bc.c @@ -29,9 +29,7 @@ #include #include -#include "py/nlr.h" -#include "py/objfun.h" -#include "py/runtime0.h" +#include "py/runtime.h" #include "py/bc0.h" #include "py/bc.h" diff --git a/py/bc.h b/py/bc.h index 69e213e42..ebfdeaac1 100644 --- a/py/bc.h +++ b/py/bc.h @@ -27,7 +27,6 @@ #define MICROPY_INCLUDED_PY_BC_H #include "py/runtime.h" -#include "py/obj.h" #include "py/objfun.h" // bytecode layout: diff --git a/py/builtinevex.c b/py/builtinevex.c index ba8048f70..846603f46 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/objfun.h" #include "py/compile.h" #include "py/runtime.h" diff --git a/py/builtinimport.c b/py/builtinimport.c index f5bfb0d98..04ce66723 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -29,7 +29,6 @@ #include #include -#include "py/nlr.h" #include "py/compile.h" #include "py/objmodule.h" #include "py/persistentcode.h" diff --git a/py/emit.h b/py/emit.h index 2b2c904f6..270a40633 100644 --- a/py/emit.h +++ b/py/emit.h @@ -28,7 +28,6 @@ #include "py/lexer.h" #include "py/scope.h" -#include "py/runtime0.h" /* Notes on passes: * We don't know exactly the opcodes in pass 1 because they depend on the diff --git a/py/emitnative.c b/py/emitnative.c index 4608cd1e0..b2c9a7366 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -46,7 +46,6 @@ #include #include -#include "py/nlr.h" #include "py/emit.h" #include "py/bc.h" diff --git a/py/gc.c b/py/gc.c index 3a505e9c7..9752b3532 100644 --- a/py/gc.c +++ b/py/gc.c @@ -28,9 +28,7 @@ #include #include -#include "py/mpstate.h" #include "py/gc.h" -#include "py/obj.h" #include "py/runtime.h" #if MICROPY_ENABLE_GC diff --git a/py/lexer.c b/py/lexer.c index 074d6f356..6017d69d6 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -28,7 +28,6 @@ #include #include -#include "py/mpstate.h" #include "py/reader.h" #include "py/lexer.h" #include "py/runtime.h" diff --git a/py/map.c b/py/map.c index f07b8fbd2..4f76b9b16 100644 --- a/py/map.c +++ b/py/map.c @@ -31,7 +31,6 @@ #include "py/mpconfig.h" #include "py/misc.h" -#include "py/runtime0.h" #include "py/runtime.h" // Fixed empty map. Useful when need to call kw-receiving functions diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 0486251b6..82b08cdc9 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -27,12 +27,10 @@ #include #include -#include "py/nlr.h" #include "py/smallint.h" #include "py/objint.h" #include "py/objstr.h" #include "py/objtype.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/builtin.h" #include "py/stream.h" diff --git a/py/modmicropython.c b/py/modmicropython.c index 6fa3f9ad2..2aac53adc 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -26,7 +26,6 @@ #include -#include "py/mpstate.h" #include "py/builtin.h" #include "py/stackctrl.h" #include "py/runtime.h" diff --git a/py/modsys.c b/py/modsys.c index ecc0b6065..84a4eb0f4 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -25,8 +25,6 @@ * THE SOFTWARE. */ -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/builtin.h" #include "py/objlist.h" #include "py/objtuple.h" @@ -35,7 +33,6 @@ #include "py/objtype.h" #include "py/stream.h" #include "py/smallint.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_SYS diff --git a/py/nativeglue.c b/py/nativeglue.c index e954234c2..61b624ec7 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -28,8 +28,6 @@ #include #include -#include "py/nlr.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/emitglue.h" #include "py/bc.h" diff --git a/py/nlrthumb.c b/py/nlrthumb.c index 8ff1bb2d2..6e7d71766 100644 --- a/py/nlrthumb.c +++ b/py/nlrthumb.c @@ -25,7 +25,6 @@ */ #include "py/mpstate.h" -#include "py/nlr.h" #if (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__)) diff --git a/py/nlrx64.c b/py/nlrx64.c index aeaacd50c..847d10398 100644 --- a/py/nlrx64.c +++ b/py/nlrx64.c @@ -25,7 +25,6 @@ */ #include "py/mpstate.h" -#include "py/nlr.h" #if !MICROPY_NLR_SETJMP && defined(__x86_64__) diff --git a/py/nlrx86.c b/py/nlrx86.c index a5a20f373..094dea3cc 100644 --- a/py/nlrx86.c +++ b/py/nlrx86.c @@ -24,9 +24,7 @@ * THE SOFTWARE. */ -#include "py/mpconfig.h" #include "py/mpstate.h" -#include "py/nlr.h" #if !MICROPY_NLR_SETJMP && defined(__i386__) diff --git a/py/nlrxtensa.c b/py/nlrxtensa.c index ccac3597b..4520e7e7a 100644 --- a/py/nlrxtensa.c +++ b/py/nlrxtensa.c @@ -25,7 +25,6 @@ */ #include "py/mpstate.h" -#include "py/nlr.h" #if !MICROPY_NLR_SETJMP && defined(__xtensa__) diff --git a/py/obj.c b/py/obj.c index 857fe373f..a1de89a03 100644 --- a/py/obj.c +++ b/py/obj.c @@ -29,12 +29,10 @@ #include #include -#include "py/nlr.h" #include "py/obj.h" #include "py/objtype.h" #include "py/objint.h" #include "py/objstr.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/stream.h" // for mp_obj_print diff --git a/py/objarray.c b/py/objarray.c index d51cc650b..8a3e7faad 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -29,8 +29,6 @@ #include #include -#include "py/nlr.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/binary.h" #include "py/objstr.h" diff --git a/py/objbool.c b/py/objbool.c index b94c57f87..5755b188e 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -26,8 +26,6 @@ #include -#include "py/obj.h" -#include "py/runtime0.h" #include "py/runtime.h" typedef struct _mp_obj_bool_t { diff --git a/py/objcomplex.c b/py/objcomplex.c index 088ad5211..409d65666 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -28,10 +28,7 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/parsenum.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_COMPLEX diff --git a/py/objdict.c b/py/objdict.c index 6bb243562..1553a83b4 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -27,9 +27,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/builtin.h" #include "py/objtype.h" diff --git a/py/objexcept.c b/py/objexcept.c index 0a03df9d0..b87609a6b 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -29,7 +29,6 @@ #include #include -#include "py/mpstate.h" #include "py/objlist.h" #include "py/objstr.h" #include "py/objtuple.h" diff --git a/py/objfloat.c b/py/objfloat.c index 0831be3fd..743287be6 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -29,9 +29,7 @@ #include #include -#include "py/nlr.h" #include "py/parsenum.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objfun.c b/py/objfun.c index 5606511d8..030b3f7cb 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -28,10 +28,8 @@ #include #include -#include "py/nlr.h" #include "py/objtuple.h" #include "py/objfun.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/bc.h" #include "py/stackctrl.h" diff --git a/py/objgenerator.c b/py/objgenerator.c index 2f39f3a52..bf0bbb0e6 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -28,8 +28,6 @@ #include #include -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/bc.h" #include "py/objgenerator.h" diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index afd6fb22b..ec41c2c5b 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/runtime.h" // this is a wrapper object that turns something that has a __getitem__ method into an iterator diff --git a/py/objint.c b/py/objint.c index 000a0404c..4f2e610a5 100644 --- a/py/objint.c +++ b/py/objint.c @@ -28,12 +28,10 @@ #include #include -#include "py/nlr.h" #include "py/parsenum.h" #include "py/smallint.h" #include "py/objint.h" #include "py/objstr.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/binary.h" diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 8d8ca1b2c..2e567c572 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -28,10 +28,8 @@ #include #include -#include "py/nlr.h" #include "py/smallint.h" #include "py/objint.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 15aad1d4d..7b5cb0b9d 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -28,11 +28,9 @@ #include #include -#include "py/nlr.h" #include "py/parsenumbase.h" #include "py/smallint.h" #include "py/objint.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objlist.c b/py/objlist.c index d70867ded..bc22d9fc3 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -27,9 +27,7 @@ #include #include -#include "py/nlr.h" #include "py/objlist.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" diff --git a/py/objmodule.c b/py/objmodule.c index fc8507c27..f9363e379 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -27,8 +27,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/objmodule.h" #include "py/runtime.h" #include "py/builtin.h" diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index fb9d9f02c..38daccdf2 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -27,7 +27,6 @@ #include -#include "py/nlr.h" #include "py/objtuple.h" #include "py/runtime.h" #include "py/objstr.h" diff --git a/py/objnone.c b/py/objnone.c index cd7319bec..da1031835 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -26,9 +26,7 @@ #include -#include "py/nlr.h" #include "py/obj.h" -#include "py/runtime0.h" typedef struct _mp_obj_none_t { mp_obj_base_t base; diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 61bd1e0ac..01880bff9 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -26,7 +26,6 @@ #include -#include "py/nlr.h" #include "py/runtime.h" // This is universal iterator type which calls "iternext" method stored in diff --git a/py/objproperty.c b/py/objproperty.c index 0934fad05..b66d24a11 100644 --- a/py/objproperty.c +++ b/py/objproperty.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_PROPERTY diff --git a/py/objrange.c b/py/objrange.c index fa99c4c2d..3874adb11 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -26,8 +26,6 @@ #include -#include "py/nlr.h" -#include "py/runtime0.h" #include "py/runtime.h" /******************************************************************************/ diff --git a/py/objreversed.c b/py/objreversed.c index a596a2fde..e498b553d 100644 --- a/py/objreversed.c +++ b/py/objreversed.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_REVERSED diff --git a/py/objset.c b/py/objset.c index aefc26aac..6ed15c791 100644 --- a/py/objset.c +++ b/py/objset.c @@ -28,9 +28,7 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" -#include "py/runtime0.h" #include "py/builtin.h" #if MICROPY_PY_BUILTINS_SET diff --git a/py/objsingleton.c b/py/objsingleton.c index ea72ae38c..67535391e 100644 --- a/py/objsingleton.c +++ b/py/objsingleton.c @@ -27,9 +27,7 @@ #include #include -#include "py/nlr.h" #include "py/obj.h" -#include "py/runtime0.h" /******************************************************************************/ /* singleton objects defined by Python */ diff --git a/py/objslice.c b/py/objslice.c index 358c44d06..de996d831 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -27,9 +27,7 @@ #include #include -#include "py/nlr.h" #include "py/obj.h" -#include "py/runtime0.h" /******************************************************************************/ /* slice object */ diff --git a/py/objstr.c b/py/objstr.c index 11bfb41fc..d17b0a68c 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -28,11 +28,9 @@ #include #include -#include "py/nlr.h" #include "py/unicode.h" #include "py/objstr.h" #include "py/objlist.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/stackctrl.h" diff --git a/py/objstringio.c b/py/objstringio.c index 61da0203e..5c50aa317 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objstr.h" #include "py/objstringio.h" #include "py/runtime.h" diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 785317406..29f7695b7 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -28,10 +28,8 @@ #include #include -#include "py/nlr.h" #include "py/objstr.h" #include "py/objlist.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_PY_BUILTINS_STR_UNICODE diff --git a/py/objtuple.c b/py/objtuple.c index b8916a1a7..34b7664eb 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -27,9 +27,7 @@ #include #include -#include "py/nlr.h" #include "py/objtuple.h" -#include "py/runtime0.h" #include "py/runtime.h" /******************************************************************************/ diff --git a/py/objtype.c b/py/objtype.c index 033765a47..d7f409e30 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -30,9 +30,7 @@ #include #include -#include "py/nlr.h" #include "py/objtype.h" -#include "py/runtime0.h" #include "py/runtime.h" #if MICROPY_DEBUG_VERBOSE // print debugging info diff --git a/py/parse.c b/py/parse.c index e399aac53..8c51b0349 100644 --- a/py/parse.c +++ b/py/parse.c @@ -31,11 +31,9 @@ #include #include -#include "py/nlr.h" #include "py/lexer.h" #include "py/parse.h" #include "py/parsenum.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/objint.h" #include "py/objstr.h" diff --git a/py/persistentcode.c b/py/persistentcode.c index f954038ae..d8b17c7e6 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -99,7 +99,6 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_ #if MICROPY_PERSISTENT_CODE_LOAD #include "py/parsenum.h" -#include "py/bc0.h" STATIC int read_byte(mp_reader_t *reader) { return reader->readbyte(reader->data); diff --git a/py/runtime.c b/py/runtime.c index f21eed204..069548deb 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -28,8 +28,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/parsenum.h" #include "py/compile.h" #include "py/objstr.h" @@ -38,7 +36,6 @@ #include "py/objmodule.h" #include "py/objgenerator.h" #include "py/smallint.h" -#include "py/runtime0.h" #include "py/runtime.h" #include "py/builtin.h" #include "py/stackctrl.h" diff --git a/py/runtime.h b/py/runtime.h index da12f8f04..d410b5614 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -27,7 +27,6 @@ #define MICROPY_INCLUDED_PY_RUNTIME_H #include "py/mpstate.h" -#include "py/obj.h" typedef enum { MP_VM_RETURN_NORMAL, diff --git a/py/runtime_utils.c b/py/runtime_utils.c index 56a918064..a5c5403ba 100644 --- a/py/runtime_utils.c +++ b/py/runtime_utils.c @@ -26,8 +26,6 @@ */ #include "py/runtime.h" -#include "py/obj.h" -#include "py/nlr.h" void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) { nlr_buf_t nlr; diff --git a/py/sequence.c b/py/sequence.c index 0752ee109..c66fde98f 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -27,9 +27,6 @@ #include -#include "py/nlr.h" -#include "py/obj.h" -#include "py/runtime0.h" #include "py/runtime.h" // Helpers for sequence types diff --git a/py/stackctrl.c b/py/stackctrl.c index 0bcd82f4f..7cd35fee2 100644 --- a/py/stackctrl.c +++ b/py/stackctrl.c @@ -24,9 +24,6 @@ * THE SOFTWARE. */ -#include "py/mpstate.h" -#include "py/nlr.h" -#include "py/obj.h" #include "py/runtime.h" #include "py/stackctrl.h" diff --git a/py/stream.c b/py/stream.c index 0adf0af06..453dee769 100644 --- a/py/stream.c +++ b/py/stream.c @@ -28,7 +28,6 @@ #include #include -#include "py/nlr.h" #include "py/objstr.h" #include "py/stream.h" #include "py/runtime.h" diff --git a/py/vm.c b/py/vm.c index 9a27b2b22..564200037 100644 --- a/py/vm.c +++ b/py/vm.c @@ -29,8 +29,6 @@ #include #include -#include "py/mpstate.h" -#include "py/nlr.h" #include "py/emitglue.h" #include "py/objtype.h" #include "py/runtime.h" From dfa563c71fead671931e91b786cd649eed517f92 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 17:59:22 +1100 Subject: [PATCH 084/163] py/objstr: Make empty bytes object have a null-terminating byte. Because a lot of string processing functions assume there is a null terminating byte, so they can work in an efficient way. Fixes issue #3334. --- py/objstr.c | 4 ++-- tests/basics/bytes.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index d17b0a68c..51da7a418 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1973,8 +1973,8 @@ const mp_obj_type_t mp_type_bytes = { .locals_dict = (mp_obj_dict_t*)&str8_locals_dict, }; -// the zero-length bytes -const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL}; +// The zero-length bytes object, with data that includes a null-terminating byte +const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, (const byte*)""}; // Create a str/bytes object using the given data. New memory is allocated and // the data is copied across. diff --git a/tests/basics/bytes.py b/tests/basics/bytes.py index d3da15c8e..1d97e6b16 100644 --- a/tests/basics/bytes.py +++ b/tests/basics/bytes.py @@ -8,6 +8,9 @@ print(bytes()) print(bytes(b'abc')) +# make sure empty bytes is converted correctly +print(str(bytes(), 'utf-8')) + a = b"123" print(a) print(str(a)) From 23faf88cab10a216eb24a30b4f33edd17183522c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 18:07:32 +1100 Subject: [PATCH 085/163] py/mpprint: Only check for null string printing when NDEBUG not defined. Printing "(null)" when a NULL string pointer is passed to %s is a debugging feature and not a feature that's relied upon by the code. So it only needs to be compiled in when debugging (such as assert) is enabled, and saves roughy 30 bytes of code when disabled. This patch also fixes this NULL check to not do the check if the precision is specified as zero. --- py/mpprint.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/py/mpprint.c b/py/mpprint.c index 6c02d7cef..a569ef793 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -485,14 +485,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { case 's': { const char *str = va_arg(args, const char*); - if (str) { - if (prec < 0) { - prec = strlen(str); - } - chrs += mp_print_strn(print, str, prec, flags, fill, width); - } else { + #ifndef NDEBUG + // With debugging enabled, catch printing of null string pointers + if (prec != 0 && str == NULL) { chrs += mp_print_strn(print, "(null)", 6, flags, fill, width); + break; } + #endif + if (prec < 0) { + prec = strlen(str); + } + chrs += mp_print_strn(print, str, prec, flags, fill, width); break; } case 'u': From f869d6b2e339c04469c6c9ea3fb2fabd7bbb2d8c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 21:14:00 +1100 Subject: [PATCH 086/163] lib/libm: Fix tanhf so that it correctly handles +/- infinity args. --- lib/libm/math.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/libm/math.c b/lib/libm/math.c index 984636627..5e00740d1 100644 --- a/lib/libm/math.c +++ b/lib/libm/math.c @@ -61,7 +61,12 @@ float log2f(float x) { return logf(x) / (float)_M_LN2; } static const float _M_LN10 = 2.30258509299404; // 0x40135d8e float log10f(float x) { return logf(x) / (float)_M_LN10; } -float tanhf(float x) { return sinhf(x) / coshf(x); } +float tanhf(float x) { + if (isinf(x)) { + return copysignf(1, x); + } + return sinhf(x) / coshf(x); +} /*****************************************************************************/ /*****************************************************************************/ From 0864a6957fe4717c3ec40ceeb373b19614a18434 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Oct 2017 23:34:28 +1100 Subject: [PATCH 087/163] py: Clean up unary and binary enum list to keep groups together. 2 non-bytecode binary ops (NOT_IN and IN_NOT) are moved out of the bytecode group, so this change will change the bytecode format. --- py/bc0.h | 4 ++-- py/runtime0.h | 36 +++++++++++++++++++-------------- py/showbc.c | 4 ++-- py/vmentrytable.h | 4 ++-- tests/cmdline/cmd_showbc.py.exp | 14 ++++++------- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/py/bc0.h b/py/bc0.h index 38e23c0fd..70acfb0ca 100644 --- a/py/bc0.h +++ b/py/bc0.h @@ -113,7 +113,7 @@ #define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // + N(64) #define MP_BC_LOAD_FAST_MULTI (0xb0) // + N(16) #define MP_BC_STORE_FAST_MULTI (0xc0) // + N(16) -#define MP_BC_UNARY_OP_MULTI (0xd0) // + op( Date: Thu, 5 Oct 2017 10:47:22 +1100 Subject: [PATCH 088/163] py/objtype: Clean up unary- and binary-op enum-to-qstr mapping tables. --- py/objtype.c | 60 ++++++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index d7f409e30..9a639ef03 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -332,7 +332,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size return MP_OBJ_FROM_PTR(o); } -const uint16_t mp_unary_op_method_name[] = { +const uint16_t mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { [MP_UNARY_OP_BOOL] = MP_QSTR___bool__, [MP_UNARY_OP_LEN] = MP_QSTR___len__, [MP_UNARY_OP_HASH] = MP_QSTR___hash__, @@ -344,7 +344,6 @@ const uint16_t mp_unary_op_method_name[] = { #if MICROPY_PY_SYS_GETSIZEOF [MP_UNARY_OP_SIZEOF] = MP_QSTR_getsizeof, #endif - [MP_UNARY_OP_NOT] = MP_QSTR_, // don't need to implement this, used to make sure array has full size }; STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) { @@ -406,14 +405,23 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } } -const uint16_t mp_binary_op_method_name[] = { - /* - MP_BINARY_OP_OR, - MP_BINARY_OP_XOR, - MP_BINARY_OP_AND, - MP_BINARY_OP_LSHIFT, - MP_BINARY_OP_RSHIFT, - */ +// Binary-op enum values not listed here will have the default value of 0 in the +// table, corresponding to MP_QSTR_, and are therefore unsupported (a lookup will +// fail). They can be added at the expense of code size for the qstr. +const uint16_t mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { + [MP_BINARY_OP_LESS] = MP_QSTR___lt__, + [MP_BINARY_OP_MORE] = MP_QSTR___gt__, + [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__, + [MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__, + [MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__, + // MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result + [MP_BINARY_OP_IN] = MP_QSTR___contains__, + + #if MICROPY_PY_ALL_SPECIAL_METHODS + [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__, + [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__, + #endif + [MP_BINARY_OP_ADD] = MP_QSTR___add__, [MP_BINARY_OP_SUBTRACT] = MP_QSTR___sub__, #if MICROPY_PY_ALL_SPECIAL_METHODS @@ -421,44 +429,12 @@ const uint16_t mp_binary_op_method_name[] = { [MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__, [MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__, #endif - /* - MP_BINARY_OP_MODULO, - MP_BINARY_OP_POWER, - MP_BINARY_OP_DIVMOD, - MP_BINARY_OP_INPLACE_OR, - MP_BINARY_OP_INPLACE_XOR, - MP_BINARY_OP_INPLACE_AND, - MP_BINARY_OP_INPLACE_LSHIFT, - MP_BINARY_OP_INPLACE_RSHIFT,*/ - #if MICROPY_PY_ALL_SPECIAL_METHODS - [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__, - [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__, - #endif - /*MP_BINARY_OP_INPLACE_MULTIPLY, - MP_BINARY_OP_INPLACE_FLOOR_DIVIDE, - MP_BINARY_OP_INPLACE_TRUE_DIVIDE, - MP_BINARY_OP_INPLACE_MODULO, - MP_BINARY_OP_INPLACE_POWER,*/ #if MICROPY_PY_REVERSE_SPECIAL_METHODS [MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__, [MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__, [MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__, #endif - - [MP_BINARY_OP_LESS] = MP_QSTR___lt__, - [MP_BINARY_OP_MORE] = MP_QSTR___gt__, - [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__, - [MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__, - [MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__, - /* - MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result - */ - [MP_BINARY_OP_IN] = MP_QSTR___contains__, - /* - MP_BINARY_OP_IS, - */ - [MP_BINARY_OP_LAST] = 0, // used to make sure array has full size, TODO: FIXME }; STATIC mp_obj_t instance_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { From ff93fd4f50321c6190e1659b19e64fef3045a484 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Oct 2017 10:48:23 +1100 Subject: [PATCH 089/163] py/persistentcode: Bump .mpy version number to version 3. The binary and unary ops have changed bytecode encoding. --- ports/minimal/frozentest.mpy | Bin 255 -> 255 bytes py/persistentcode.c | 2 +- tools/mpy-tool.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/minimal/frozentest.mpy b/ports/minimal/frozentest.mpy index 87f9581bfe7290b67f473a49441fd25db204f459..7c6809bf6522f90339ec72ae0dcd16560ac5aafc 100644 GIT binary patch delta 99 zcmey*_@6P_mzhaEhM55bc)1upGH7XNXlQFNF#MN3$RPHKA%)Qh$PkCoj8HxkgcjNL gewHYZ*^0DZ?AumAu6 delta 99 zcmey*_@6P_mx)O}hM55bc)1w9GiYgOXlQFNF#MN3&LH-KA%)Qh$PkCoj8HxkgcjNL gewHYZ*^0EloK2mk;8 diff --git a/py/persistentcode.c b/py/persistentcode.c index d8b17c7e6..e0bb8f1d6 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -39,7 +39,7 @@ #include "py/smallint.h" // The current version of .mpy files -#define MPY_VERSION (2) +#define MPY_VERSION (3) // The feature flags byte encodes the compile-time config options that // affect the generate bytecode. diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index ded962487..5de4ecf1c 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -57,7 +57,7 @@ def __str__(self): return 'error while freezing %s: %s' % (self.rawcode.source_file, self.msg) class Config: - MPY_VERSION = 2 + MPY_VERSION = 3 MICROPY_LONGINT_IMPL_NONE = 0 MICROPY_LONGINT_IMPL_LONGLONG = 1 MICROPY_LONGINT_IMPL_MPZ = 2 From 8c7db42ee3d6e47e4a58e4cb573dc1a30c96fa32 Mon Sep 17 00:00:00 2001 From: Li Weiwei Date: Wed, 27 Sep 2017 22:04:42 +0800 Subject: [PATCH 090/163] stm32/modnwwiznet5k: Get the IP address of an established socket. When wiznet5k_socket_accept is called, if a socket is established, get the IP address of the socket. --- ports/stm32/modnwwiznet5k.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index 21e4dbbbf..78249816d 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -173,11 +173,7 @@ STATIC int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_ int sr = getSn_SR((uint8_t)socket->u_param.fileno); if (sr == SOCK_ESTABLISHED) { socket2->u_param = socket->u_param; - // TODO need to populate this with the correct values - ip[0] = 0; - ip[1] = 0; - ip[2] = 0; - ip[3] = 0; + getSn_DIPR((uint8_t)socket2->u_param.fileno, ip); *port = getSn_PORT(socket2->u_param.fileno); // WIZnet turns the listening socket into the client socket, so we From 98dd126e9818309e4ec9b561e7b6b9f7fa039cb9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Oct 2017 11:32:55 +1100 Subject: [PATCH 091/163] tests/extmod: Add test for '-' in character class in regex. --- tests/extmod/ure1.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/extmod/ure1.py b/tests/extmod/ure1.py index 6075990fc..54471ed4f 100644 --- a/tests/extmod/ure1.py +++ b/tests/extmod/ure1.py @@ -48,7 +48,12 @@ print(m.group(0)) m = r.match("A") print(m.group(0)) +print("===") +# '-' character within character class block +print(re.match("[-a]+", "-a]d").group(0)) +print(re.match("[a-]+", "-a]d").group(0)) +print("===") r = re.compile("o+") m = r.search("foobar") From ea6692a83e7132aa18a8032ae3c76fa91d9d50f2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 5 Oct 2017 23:40:19 +0300 Subject: [PATCH 092/163] tools/pyboard: Use repr() when quoting data in error messages. As it may contain newlines, etc. --- tools/pyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index d15f520ac..887473071 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -343,7 +343,7 @@ def exec_raw_no_follow(self, command): # check if we could exec command data = self.serial.read(2) if data != b'OK': - raise PyboardError('could not exec command (response: %s)' % data) + raise PyboardError('could not exec command (response: %r)' % data) def exec_raw(self, command, timeout=10, data_consumer=None): self.exec_raw_no_follow(command); From 7df4083ac6344944e895b5911d59b64b68c8c134 Mon Sep 17 00:00:00 2001 From: Tiago Queiroz Date: Sat, 4 Feb 2017 23:32:10 -0200 Subject: [PATCH 093/163] drivers/display/ssd1306: Implement SSD1306_I2C poweron method. After a poweroff(), the poweron() method does a soft power-on and any previous state of the display persists. --- drivers/display/ssd1306.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index d65f2d350..a05eda7df 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -124,7 +124,7 @@ def write_data(self, buf): self.i2c.stop() def poweron(self): - pass + self.write_cmd(SET_DISP | 0x01) class SSD1306_SPI(SSD1306): From ca2427c31399834bcc7df6177b3314aa7242046e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 6 Oct 2017 12:45:16 +1100 Subject: [PATCH 094/163] drivers/display/ssd1306: Make poweron() work the same with SSD1306_SPI. The poweroff() and poweron() methods are used to do soft power control of the display, and this patch makes these methods work the same for both I2C and SPI interfaces. --- drivers/display/ssd1306.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index a05eda7df..cd358d00e 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -1,7 +1,6 @@ # MicroPython SSD1306 OLED driver, I2C and SPI interfaces from micropython import const -import time import framebuf @@ -47,7 +46,6 @@ def __init__(self, width, height, external_vcc): self.text = fb.text self.scroll = fb.scroll self.blit = fb.blit - self.poweron() self.init_display() def init_display(self): @@ -80,6 +78,9 @@ def init_display(self): def poweroff(self): self.write_cmd(SET_DISP | 0x00) + def poweron(self): + self.write_cmd(SET_DISP | 0x01) + def contrast(self, contrast): self.write_cmd(SET_CONTRAST) self.write_cmd(contrast) @@ -123,9 +124,6 @@ def write_data(self, buf): self.i2c.write(buf) self.i2c.stop() - def poweron(self): - self.write_cmd(SET_DISP | 0x01) - class SSD1306_SPI(SSD1306): def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): @@ -137,6 +135,12 @@ def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): self.dc = dc self.res = res self.cs = cs + import time + self.res(1) + time.sleep_ms(1) + self.res(0) + time.sleep_ms(10) + self.res(1) super().__init__(width, height, external_vcc) def write_cmd(self, cmd): @@ -154,10 +158,3 @@ def write_data(self, buf): self.cs(0) self.spi.write(buf) self.cs(1) - - def poweron(self): - self.res(1) - time.sleep_ms(1) - self.res(0) - time.sleep_ms(10) - self.res(1) From 6f1a61542746e3ef3704f6e563e2526454df0112 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 6 Oct 2017 14:32:42 +1100 Subject: [PATCH 095/163] stm32/boards: Fix typos in stm32f767_af.csv table. --- ports/stm32/boards/stm32f767_af.csv | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/stm32f767_af.csv b/ports/stm32/boards/stm32f767_af.csv index db27818c6..1708dfcca 100644 --- a/ports/stm32/boards/stm32f767_af.csv +++ b/ports/stm32/boards/stm32f767_af.csv @@ -15,9 +15,9 @@ PortA,PA11,,TIM1_CH4,,,,,,USART1_CTS,,CAN1_RX,OTG_FS_DM,,,,LCD_R4,EVENTOUT PortA,PA12,,TIM1_ETR,,,,,,USART1_RTS,SAI2_FS_B,CAN1_TX,OTG_FS_DP,,,,LCD_R5,EVENTOUT PortA,PA13,JTMS,SWDIO,,,,,,,,,,,,,,EVENTOUT PortA,PA14,JTCK,SWCLK,,,,,,,,,,,,,,EVENTOUT -PortA,PA15,JTDI,TIM2_CH1/TIM2_ETR,,,HDMICE,CSPI1_NSS/I2S1_WS,SPI3_NSS/I2S3_WS,,UART4_RTS,,,,,,,EVENTOUT -PortB,PB0,,TIM1_CH2N,TIM3_CH3T,IM8_CH2N,,,,,UART4_CTS,LCD_R3,OTG_HS_ULPI_D1,ETH_MII_RXD2,,,,EVENTOUT -PortB,PB1,,TIM1_CH3N,TIM3_CH4T,IM8_CH3N,,,,,,LCD_R6,OTG_HS_ULPI_D2,ETH_MII_RXD3,,,,EVENTOUT +PortA,PA15,JTDI,TIM2_CH1/TIM2_ETR,,,HDMICEC,SPI1_NSS/I2S1_WS,SPI3_NSS/I2S3_WS,,UART4_RTS,,,,,,,EVENTOUT +PortB,PB0,,TIM1_CH2N,TIM3_CH3T,TIM8_CH2N,,,,,UART4_CTS,LCD_R3,OTG_HS_ULPI_D1,ETH_MII_RXD2,,,,EVENTOUT +PortB,PB1,,TIM1_CH3N,TIM3_CH4T,TIM8_CH3N,,,,,,LCD_R6,OTG_HS_ULPI_D2,ETH_MII_RXD3,,,,EVENTOUT PortB,PB2,,,,,,,SAI1_SD_A,SPI3_MOSI/I2S3_SD,,QUADSPI_CLK,,,,,,EVENTOUT PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,,SPI1_SCK/I2S1_CK,SPI3_SCK/I2S3_CK,,,,SDMMC2_D2,,,,,EVENTOUT PortB,PB4,NJTRST,,TIM3_CH1,,,SPI1_MISO,SPI3_MISO,SPI2_NSS/I2S2_WS,,,SDMMC2_D3,,,,,EVENTOUT @@ -41,7 +41,7 @@ PortC,PC5,,,,,,,,,SPDIFRX_IN3,,,ETH_MII_RXD1/ETH_RMII_RXD1,FMC_SDCKE0,,,EVENTOUT PortC,PC6,,,TIM3_CH1,TIM8_CH1,,I2S2_MCK,,,USART6_TX,,SDMMC2_D6,,SDMMC1_D6,DCMI_D0,LCD_HSYNC,EVENTOUT PortC,PC7,,,TIM3_CH2,TIM8_CH2,,,I2S3_MCK,,USART6_RX,,SDMMC2_D7,,SDMMC1_D7,DCMI_D1,LCD_G6,EVENTOUT PortC,PC8,TRACED1,,TIM3_CH3,TIM8_CH3,,,,UART5_RTS,USART6_CK,,,,SDMMC1_D0,DCMI_D2,,EVENTOUT -PortC,PC9,MCO2,,TIM3_CH4,TIM8_CH4,I2C3_SDA,I2S_CKIN,,UART5_CTS,,QUADSPI_BK1_IO0,,,SDMMC1_D1,DCMI_D3,,EVENTOUT +PortC,PC9,MCO2,,TIM3_CH4,TIM8_CH4,I2C3_SDA,I2S2_CKIN,,UART5_CTS,,QUADSPI_BK1_IO0,,,SDMMC1_D1,DCMI_D3,,EVENTOUT PortC,PC10,,,,,,,SPI3_SCK/I2S3_CK,USART3_TX,UART4_TX,QUADSPI_BK1_IO1,,,SDMMC1_D2,DCMI_D8,LCD_R2,EVENTOUT PortC,PC11,,,,,,,SPI3_MISO,USART3_RX,UART4_RX,QUADSPI_BK2_NCS,,,SDMMC1_D3,DCMI_D4,,EVENTOUT PortC,PC12,TRACED3,,,,,,SPI3_MOSI/I2S3_SD,USART3_CK,UART5_TX,,,,SDMMC1_CK,DCMI_D9,,EVENTOUT From 58ea239510530b5ce80aa24cd084b43698309bb4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 7 Oct 2017 14:08:50 +0300 Subject: [PATCH 096/163] zephyr: Use CONFIG_NET_APP_SETTINGS to setup initial network addresses. Ideally, these should be configurable from Python (using network module), but as that doesn't exist, we better off using Zephyr's native bootstrap configuration facility. --- ports/zephyr/main.c | 5 ++++- ports/zephyr/prj_base.conf | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 7255d981f..7eb9da3e1 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -60,7 +60,9 @@ static char *stack_top; static char heap[MICROPY_HEAP_SIZE]; void init_zephyr(void) { - // TODO: Make addresses configurable + // We now rely on CONFIG_NET_APP_SETTINGS to set up bootstrap + // network addresses. +#if 0 #ifdef CONFIG_NETWORKING if (net_if_get_default() == NULL) { // If there's no default networking interface, @@ -81,6 +83,7 @@ void init_zephyr(void) { static struct in6_addr in6addr_my = {{{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}}; net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0); #endif +#endif } int real_main(void) { diff --git a/ports/zephyr/prj_base.conf b/ports/zephyr/prj_base.conf index c3ba2812f..a4cc14513 100644 --- a/ports/zephyr/prj_base.conf +++ b/ports/zephyr/prj_base.conf @@ -18,6 +18,14 @@ CONFIG_NET_SOCKETS=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_NBUF_RX_COUNT=5 +CONFIG_NET_APP_SETTINGS=y +CONFIG_NET_APP_INIT_TIMEOUT=3 +CONFIG_NET_APP_NEED_IPV6=y +CONFIG_NET_APP_NEED_IPV4=y +CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1" +CONFIG_NET_APP_MY_IPV4_GW="192.0.2.2" + # DNS CONFIG_DNS_RESOLVER=y CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=2 From 71c1a05d88c9ac84212d164458c5a501e59be389 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 7 Oct 2017 15:49:58 +0300 Subject: [PATCH 097/163] tests/run-tests: Close device under test using "finally". We want to close communication object even if there were exceptions somewhere in the code. This is important for --device exec:/execpty: which may otherwise leave processing running in the background. --- tests/run-tests | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/run-tests b/tests/run-tests index 23fc3d910..6280a5182 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -492,9 +492,12 @@ def main(): # we need to access feature_check's from the same directory as the # run-tests script itself. base_path = os.path.dirname(sys.argv[0]) or "." - res = run_tests(pyb, tests, args, base_path) - if pyb: - pyb.close() + try: + res = run_tests(pyb, tests, args, base_path) + finally: + if pyb: + pyb.close() + if not res: sys.exit(1) From 4514f073c18ab94b3f0f790ae813de61f38d367b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 7 Oct 2017 17:09:53 +0300 Subject: [PATCH 098/163] zephyr: Switch to interrupt-driven pull-style console. While this console API improves handling on real hardware boards (e.g. clipboard paste is much more reliable, as well as programmatic communication), it vice-versa poses problems under QEMU, apparently because it doesn't emulate UART interrupt handling faithfully. That leads to inability to run the testsuite on QEMU at all. To work that around, we have to suuport both old and new console routines, and use the old ones under QEMU. --- ports/zephyr/prj_base.conf | 6 ++++++ ports/zephyr/prj_qemu_cortex_m3.conf | 4 ++++ ports/zephyr/prj_qemu_x86.conf | 4 ++++ ports/zephyr/src/zephyr_start.c | 5 +++++ ports/zephyr/uart_core.c | 14 ++++++++++++++ 5 files changed, 33 insertions(+) diff --git a/ports/zephyr/prj_base.conf b/ports/zephyr/prj_base.conf index a4cc14513..fbcedf260 100644 --- a/ports/zephyr/prj_base.conf +++ b/ports/zephyr/prj_base.conf @@ -4,6 +4,12 @@ CONFIG_REBOOT=y CONFIG_STDOUT_CONSOLE=y CONFIG_CONSOLE_HANDLER=y CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y + +CONFIG_CONSOLE_PULL=y +CONFIG_CONSOLE_GETCHAR=y +CONFIG_CONSOLE_GETCHAR_BUFSIZE=128 +CONFIG_CONSOLE_PUTCHAR_BUFSIZE=128 + CONFIG_NEWLIB_LIBC=y CONFIG_FLOAT=y CONFIG_MAIN_STACK_SIZE=4096 diff --git a/ports/zephyr/prj_qemu_cortex_m3.conf b/ports/zephyr/prj_qemu_cortex_m3.conf index 09614c362..1ade981e2 100644 --- a/ports/zephyr/prj_qemu_cortex_m3.conf +++ b/ports/zephyr/prj_qemu_cortex_m3.conf @@ -1,3 +1,7 @@ +# Interrupt-driven UART console has emulation artifacts under QEMU, +# disable it +CONFIG_CONSOLE_PULL=n + # Networking drivers # SLIP driver for QEMU CONFIG_NET_SLIP_TAP=y diff --git a/ports/zephyr/prj_qemu_x86.conf b/ports/zephyr/prj_qemu_x86.conf index ef60cfec9..9bc81259a 100644 --- a/ports/zephyr/prj_qemu_x86.conf +++ b/ports/zephyr/prj_qemu_x86.conf @@ -1,3 +1,7 @@ +# Interrupt-driven UART console has emulation artifacts under QEMU, +# disable it +CONFIG_CONSOLE_PULL=n + # Networking drivers # SLIP driver for QEMU CONFIG_NET_SLIP_TAP=y diff --git a/ports/zephyr/src/zephyr_start.c b/ports/zephyr/src/zephyr_start.c index 9e8a90beb..452e304ca 100644 --- a/ports/zephyr/src/zephyr_start.c +++ b/ports/zephyr/src/zephyr_start.c @@ -24,11 +24,16 @@ * THE SOFTWARE. */ #include +#include #include "zephyr_getchar.h" int real_main(void); void main(void) { +#ifdef CONFIG_CONSOLE_PULL + console_init(); +#else zephyr_getchar_init(); +#endif real_main(); } diff --git a/ports/zephyr/uart_core.c b/ports/zephyr/uart_core.c index 1e85053cd..e41fb9acc 100644 --- a/ports/zephyr/uart_core.c +++ b/ports/zephyr/uart_core.c @@ -28,6 +28,7 @@ #include "src/zephyr_getchar.h" // Zephyr headers #include +#include /* * Core UART functions to implement for a port @@ -35,11 +36,23 @@ // Receive single character int mp_hal_stdin_rx_chr(void) { +#ifdef CONFIG_CONSOLE_PULL + return console_getchar(); +#else return zephyr_getchar(); +#endif } // Send string of given length void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { +#ifdef CONFIG_CONSOLE_PULL + while (len--) { + char c = *str++; + while (console_putchar(c) == -1) { + k_sleep(1); + } + } +#else static struct device *uart_console_dev; if (uart_console_dev == NULL) { uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); @@ -48,4 +61,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { while (len--) { uart_poll_out(uart_console_dev, *str++); } +#endif } From c15be989ee195d375f93b39aa13bc4e07b36c1eb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 8 Oct 2017 00:04:57 +0300 Subject: [PATCH 099/163] tools/pyboard: Update docstring for additional device support. --- tools/pyboard.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 887473071..16ee41f70 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -5,6 +5,7 @@ # The MIT License (MIT) # # Copyright (c) 2014-2016 Damien P. George +# Copyright (c) 2017 Paul Sokolovsky # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -28,7 +29,11 @@ pyboard interface This module provides the Pyboard class, used to communicate with and -control the pyboard over a serial USB connection. +control a MicroPython device over a communication channel. Both real +boards and emulated devices (e.g. running in QEMU) are supported. +Various communication channels are supported, including a serial +connection, telnet-style network connection, external process +connection. Example usage: From 53966fd9a8c1b7d8a75e8d1c479c8219c8d101d3 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 9 Oct 2017 00:22:30 +0300 Subject: [PATCH 100/163] examples: hwconfig_console: Add .on()/.off() methods. Add these methods to this "GPIO output emulated with console prints" config. --- examples/hwapi/hwconfig_console.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/hwapi/hwconfig_console.py b/examples/hwapi/hwconfig_console.py index 4b0b0d79b..bbcc0e816 100644 --- a/examples/hwapi/hwconfig_console.py +++ b/examples/hwapi/hwconfig_console.py @@ -8,6 +8,12 @@ def __init__(self, id): def value(self, v): print(self.id, v) + def on(self): + self.value(1) + + def off(self): + self.value(0) + LED = LEDClass(1) LED2 = LEDClass(12) From 6db132e1302dcca4671e0d67b872ca226c54682f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Oct 2017 15:27:21 +1100 Subject: [PATCH 101/163] esp8266/modnetwork: Add "bssid" keyword arg to WLAN.connect() method. --- ports/esp8266/modnetwork.c | 44 ++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index 2c61e5dcd..b41a11f59 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -93,29 +93,55 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active); -STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *args) { - require_if(args[0], STATION_IF); +STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_ssid, ARG_password, ARG_bssid }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + require_if(pos_args[0], STATION_IF); struct station_config config = {{0}}; size_t len; const char *p; + bool set_config = false; - if (n_args > 1) { - p = mp_obj_str_get_data(args[1], &len); + // set parameters based on given args + if (args[ARG_ssid].u_obj != mp_const_none) { + p = mp_obj_str_get_data(args[ARG_ssid].u_obj, &len); len = MIN(len, sizeof(config.ssid)); memcpy(config.ssid, p, len); - if (n_args > 2) { - p = mp_obj_str_get_data(args[2], &len); - len = MIN(len, sizeof(config.password)); - memcpy(config.password, p, len); + set_config = true; + } + if (args[ARG_password].u_obj != mp_const_none) { + p = mp_obj_str_get_data(args[ARG_password].u_obj, &len); + len = MIN(len, sizeof(config.password)); + memcpy(config.password, p, len); + set_config = true; + } + if (args[ARG_bssid].u_obj != mp_const_none) { + p = mp_obj_str_get_data(args[ARG_bssid].u_obj, &len); + if (len != sizeof(config.bssid)) { + mp_raise_ValueError(NULL); } + config.bssid_set = 1; + memcpy(config.bssid, p, sizeof(config.bssid)); + set_config = true; + } + if (set_config) { error_check(wifi_station_set_config(&config), "Cannot set STA config"); } error_check(wifi_station_connect(), "Cannot connect to AP"); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 1, 7, esp_connect); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_connect_obj, 1, esp_connect); STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) { require_if(self_in, STATION_IF); From add933feaf188073de99cf54e4d251c48034472c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Oct 2017 15:27:50 +1100 Subject: [PATCH 102/163] docs/library/network: Clarify usage of "bssid" arg in connect() method. --- docs/library/network.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/library/network.rst b/docs/library/network.rst index def6bee74..258b2a20c 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -72,8 +72,7 @@ parameter should be `id`. connection parameters. For various medium types, there are different sets of predefined/recommended parameters, among them: - * WiFi: *bssid* keyword to connect by BSSID (MAC address) instead - of access point name + * WiFi: *bssid* keyword to connect to a specific BSSID (MAC address) .. method:: disconnect() @@ -333,9 +332,12 @@ parameter should be `id`. argument is passed. Otherwise, query current state if no argument is provided. Most other methods require active interface. - .. method:: wlan.connect(ssid, password) + .. method:: wlan.connect(ssid=None, password=None, \*, bssid=None) Connect to the specified wireless network, using the specified password. + If *bssid* is given then the connection will be restricted to the + access-point with that MAC address (the *ssid* must also be specified + in this case). .. method:: wlan.disconnect() From 933eab46fcf2dcca2c3ca3d04b714f238e6020e1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 10 Oct 2017 10:37:38 +1100 Subject: [PATCH 103/163] py/bc: Update opcode_format_table to match the bytecode. --- py/bc.c | 6 +++--- tools/mpy-tool.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/py/bc.c b/py/bc.c index 991b0cf26..89d8b74f9 100644 --- a/py/bc.c +++ b/py/bc.c @@ -321,7 +321,7 @@ STATIC const byte opcode_format_table[64] = { OC4(O, O, U, U), // 0x38-0x3b OC4(U, O, B, O), // 0x3c-0x3f OC4(O, B, B, O), // 0x40-0x43 - OC4(B, B, O, U), // 0x44-0x47 + OC4(B, B, O, B), // 0x44-0x47 OC4(U, U, U, U), // 0x48-0x4b OC4(U, U, U, U), // 0x4c-0x4f OC4(V, V, U, V), // 0x50-0x53 @@ -361,7 +361,7 @@ STATIC const byte opcode_format_table[64] = { OC4(B, B, B, B), // 0xcc-0xcf OC4(B, B, B, B), // 0xd0-0xd3 - OC4(B, B, B, B), // 0xd4-0xd7 + OC4(U, U, U, B), // 0xd4-0xd7 OC4(B, B, B, B), // 0xd8-0xdb OC4(B, B, B, B), // 0xdc-0xdf @@ -372,7 +372,7 @@ STATIC const byte opcode_format_table[64] = { OC4(B, B, B, B), // 0xf0-0xf3 OC4(B, B, B, B), // 0xf4-0xf7 - OC4(B, B, B, U), // 0xf8-0xfb + OC4(U, U, U, U), // 0xf8-0xfb OC4(U, U, U, U), // 0xfc-0xff }; #undef OC4 diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 5de4ecf1c..ac7b2c1cc 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -105,7 +105,7 @@ def OC4(a, b, c, d): OC4(O, O, U, U), # 0x38-0x3b OC4(U, O, B, O), # 0x3c-0x3f OC4(O, B, B, O), # 0x40-0x43 - OC4(B, B, O, U), # 0x44-0x47 + OC4(B, B, O, B), # 0x44-0x47 OC4(U, U, U, U), # 0x48-0x4b OC4(U, U, U, U), # 0x4c-0x4f OC4(V, V, U, V), # 0x50-0x53 @@ -145,7 +145,7 @@ def OC4(a, b, c, d): OC4(B, B, B, B), # 0xcc-0xcf OC4(B, B, B, B), # 0xd0-0xd3 - OC4(B, B, B, B), # 0xd4-0xd7 + OC4(U, U, U, B), # 0xd4-0xd7 OC4(B, B, B, B), # 0xd8-0xdb OC4(B, B, B, B), # 0xdc-0xdf @@ -156,7 +156,7 @@ def OC4(a, b, c, d): OC4(B, B, B, B), # 0xf0-0xf3 OC4(B, B, B, B), # 0xf4-0xf7 - OC4(B, B, B, U), # 0xf8-0xfb + OC4(U, U, U, U), # 0xf8-0xfb OC4(U, U, U, U), # 0xfc-0xff )) From d236d0c415bc34c96e7155ab116258a026b2cda6 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 6 Oct 2017 01:01:05 +1100 Subject: [PATCH 104/163] docs/pyboard/quickref: Add info for Switch, RTC, CAN, Accel classes. --- docs/pyboard/quickref.rst | 54 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/docs/pyboard/quickref.rst b/docs/pyboard/quickref.rst index 5690dddb0..48798aad3 100644 --- a/docs/pyboard/quickref.rst +++ b/docs/pyboard/quickref.rst @@ -39,17 +39,32 @@ Use the :mod:`time ` module:: start = time.ticks_ms() # get value of millisecond counter delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference -LEDs ----- +Internal LEDs +------------- See :ref:`pyb.LED `. :: from pyb import LED - led = LED(1) # red led + led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue led.toggle() led.on() led.off() + + # LEDs 3 and 4 support PWM intensity (0-255) + LED(4).intensity() # get intensity + LED(4).intensity(128) # set intensity to half + +Internal switch +--------------- + +See :ref:`pyb.Switch `. :: + + from pyb import Switch + + sw = Switch() + sw.value() # returns True or False + sw.callback(lambda: pyb.LED(1).toggle()) Pins and GPIO ------------- @@ -99,6 +114,17 @@ See :ref:`pyb.Timer `. :: tim.freq(0.5) # 0.5 Hz tim.callback(lambda t: pyb.LED(1).toggle()) +RTC (real time clock) +--------------------- + +See :ref:`pyb.RTC ` :: + + from pyb import RTC + + rtc = RTC() + rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time + rtc.datetime() # get date and time + PWM (pulse width modulation) ---------------------------- @@ -167,3 +193,25 @@ See :ref:`pyb.I2C `. :: i2c.recv(5, 0x42) # receive 5 bytes from slave i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10 i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10 + +CAN bus (controller area network) +--------------------------------- + +See :ref:`pyb.CAN `. :: + + from pyb import CAN + + can = CAN(1, CAN.LOOPBACK) + can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) + can.send('message!', 123) # send a message with id 123 + can.recv(0) # receive message on FIFO 0 + +Internal accelerometer +---------------------- + +See :ref:`pyb.Accel `. :: + + from pyb import Accel + + accel = Accel() + print(accel.x(), accel.y(), accel.z(), accel.tilt()) From dc92f1c4ee0924cff0b33b5061622f0621e71eac Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 6 Oct 2017 01:36:48 +1100 Subject: [PATCH 105/163] docs/pyboard/tutorial: Update now that yellow LED also supports PWM. --- docs/pyboard/tutorial/leds.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/pyboard/tutorial/leds.rst b/docs/pyboard/tutorial/leds.rst index 763eedf01..6b05f5db0 100644 --- a/docs/pyboard/tutorial/leds.rst +++ b/docs/pyboard/tutorial/leds.rst @@ -60,10 +60,10 @@ One problem you might find is that if you stop the script and then start it agai for l in leds: l.off() -The Fourth Special LED ----------------------- +The Special LEDs +---------------- -The blue LED is special. As well as turning it on and off, you can control the intensity using the intensity() method. This takes a number between 0 and 255 that determines how bright it is. The following script makes the blue LED gradually brighter then turns it off again. :: +The yellow and blue LEDs are special. As well as turning them on and off, you can control their intensity using the intensity() method. This takes a number between 0 and 255 that determines how bright it is. The following script makes the blue LED gradually brighter then turns it off again. :: led = pyb.LED(4) intensity = 0 @@ -72,4 +72,4 @@ The blue LED is special. As well as turning it on and off, you can control the i led.intensity(intensity) pyb.delay(20) -You can call intensity() on the other LEDs but they can only be off or on. 0 sets them off and any other number up to 255 turns them on. +You can call intensity() on LEDs 1 and 2 but they can only be off or on. 0 sets them off and any other number up to 255 turns them on. From f599a380591df2a7bb466eae6a4144634409821b Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 6 Oct 2017 01:48:43 +1100 Subject: [PATCH 106/163] docs/esp8266/quickref: Add quickref info for RTC class. --- docs/esp8266/quickref.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index ccf6365c8..c510e4064 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -223,6 +223,17 @@ and is accessed via the :ref:`machine.I2C ` class:: buf = bytearray(10) # create a buffer with 10 bytes i2c.writeto(0x3a, buf) # write the given buffer to the slave +Real time clock (RTC) +--------------------- + +See :ref:`machine.RTC ` :: + + from machine import RTC + + rtc = RTC() + rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time + rtc.datetime() # get date and time + Deep-sleep mode --------------- From 25e140652b946e9a136a16a9831c0b18f1285702 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 21:00:05 +1100 Subject: [PATCH 107/163] py/modmath: Add full checks for math domain errors. This patch changes how most of the plain math functions are implemented: there are now two generic math wrapper functions that take a pointer to a math function (like sin, cos) and perform the necessary conversion to and from MicroPython types. This helps to reduce code size. The generic functions can also check for math domain errors in a generic way, by testing if the result is NaN or infinity combined with finite inputs. The result is that, with this patch, all math functions now have full domain error checking (even gamma and lgamma) and code size has decreased for most ports. Code size changes in bytes for those with the math module are: unix x64: -432 unix nanbox: -792 stm32: -88 esp8266: +12 Tests are also added to check domain errors are handled correctly. --- py/modmath.c | 65 +++++++++++++++++++++--------- tests/float/math_domain.py | 51 +++++++++++++++++++++++ tests/float/math_domain_special.py | 36 +++++++++++++++++ 3 files changed, 132 insertions(+), 20 deletions(-) create mode 100644 tests/float/math_domain.py create mode 100644 tests/float/math_domain_special.py diff --git a/py/modmath.c b/py/modmath.c index 4d627c67f..6c248844d 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013-2017 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -39,14 +39,31 @@ STATIC NORETURN void math_error(void) { mp_raise_ValueError("math domain error"); } +STATIC mp_obj_t math_generic_1(mp_obj_t x_obj, mp_float_t (*f)(mp_float_t)) { + mp_float_t x = mp_obj_get_float(x_obj); + mp_float_t ans = f(x); + if ((isnan(ans) && !isnan(x)) || (isinf(ans) && !isinf(x))) { + math_error(); + } + return mp_obj_new_float(ans); +} + +STATIC mp_obj_t math_generic_2(mp_obj_t x_obj, mp_obj_t y_obj, mp_float_t (*f)(mp_float_t, mp_float_t)) { + mp_float_t x = mp_obj_get_float(x_obj); + mp_float_t y = mp_obj_get_float(y_obj); + mp_float_t ans = f(x, y); + if ((isnan(ans) && !isnan(x) && !isnan(y)) || (isinf(ans) && !isinf(x))) { + math_error(); + } + return mp_obj_new_float(ans); +} + #define MATH_FUN_1(py_name, c_name) \ - STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { \ + return math_generic_1(x_obj, MICROPY_FLOAT_C_FUN(c_name)); \ + } \ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); -#define MATH_FUN_2(py_name, c_name) \ - STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj), mp_obj_get_float(y_obj))); } \ - STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); - #define MATH_FUN_1_TO_BOOL(py_name, c_name) \ STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_bool(c_name(mp_obj_get_float(x_obj))); } \ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); @@ -55,15 +72,17 @@ STATIC NORETURN void math_error(void) { STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int_from_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); -#define MATH_FUN_1_ERRCOND(py_name, c_name, error_condition) \ - STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { \ - mp_float_t x = mp_obj_get_float(x_obj); \ - if (error_condition) { \ - math_error(); \ - } \ - return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(x)); \ +#define MATH_FUN_2(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { \ + return math_generic_2(x_obj, y_obj, MICROPY_FLOAT_C_FUN(c_name)); \ } \ - STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); + STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); + +#define MATH_FUN_2_FLT_INT(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { \ + return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj), mp_obj_get_int(y_obj))); \ + } \ + STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); #if MP_NEED_LOG2 // 1.442695040888963407354163704 is 1/_M_LN2 @@ -71,7 +90,7 @@ STATIC NORETURN void math_error(void) { #endif // sqrt(x): returns the square root of x -MATH_FUN_1_ERRCOND(sqrt, sqrt, (x < (mp_float_t)0.0)) +MATH_FUN_1(sqrt, sqrt) // pow(x, y): returns x to the power of y MATH_FUN_2(pow, pow) // exp(x) @@ -80,9 +99,9 @@ MATH_FUN_1(exp, exp) // expm1(x) MATH_FUN_1(expm1, expm1) // log2(x) -MATH_FUN_1_ERRCOND(log2, log2, (x <= (mp_float_t)0.0)) +MATH_FUN_1(log2, log2) // log10(x) -MATH_FUN_1_ERRCOND(log10, log10, (x <= (mp_float_t)0.0)) +MATH_FUN_1(log10, log10) // cosh(x) MATH_FUN_1(cosh, cosh) // sinh(x) @@ -113,9 +132,15 @@ MATH_FUN_2(atan2, atan2) // ceil(x) MATH_FUN_1_TO_INT(ceil, ceil) // copysign(x, y) -MATH_FUN_2(copysign, copysign) +STATIC mp_float_t MICROPY_FLOAT_C_FUN(copysign_func)(mp_float_t x, mp_float_t y) { + return MICROPY_FLOAT_C_FUN(copysign)(x, y); +} +MATH_FUN_2(copysign, copysign_func) // fabs(x) -MATH_FUN_1(fabs, fabs) +STATIC mp_float_t MICROPY_FLOAT_C_FUN(fabs_func)(mp_float_t x) { + return MICROPY_FLOAT_C_FUN(fabs)(x); +} +MATH_FUN_1(fabs, fabs_func) // floor(x) MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float // fmod(x, y) @@ -129,7 +154,7 @@ MATH_FUN_1_TO_BOOL(isnan, isnan) // trunc(x) MATH_FUN_1_TO_INT(trunc, trunc) // ldexp(x, exp) -MATH_FUN_2(ldexp, ldexp) +MATH_FUN_2_FLT_INT(ldexp, ldexp) #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS // erf(x): return the error function of x MATH_FUN_1(erf, erf) diff --git a/tests/float/math_domain.py b/tests/float/math_domain.py new file mode 100644 index 000000000..0cf10fb2a --- /dev/null +++ b/tests/float/math_domain.py @@ -0,0 +1,51 @@ +# Tests domain errors in math functions + +try: + import math +except ImportError: + print("SKIP") + raise SystemExit + +inf = float('inf') +nan = float('nan') + +# single argument functions +for name, f, args in ( + ('fabs', math.fabs, ()), + ('ceil', math.ceil, ()), + ('floor', math.floor, ()), + ('trunc', math.trunc, ()), + ('sqrt', math.sqrt, (-1, 0)), + ('exp', math.exp, ()), + ('sin', math.sin, ()), + ('cos', math.cos, ()), + ('tan', math.tan, ()), + ('asin', math.asin, (-1.1, 1, 1.1)), + ('acos', math.acos, (-1.1, 1, 1.1)), + ('atan', math.atan, ()), + ('ldexp', lambda x: math.ldexp(x, 0), ()), + ('radians', math.radians, ()), + ('degrees', math.degrees, ()), + ): + for x in args + (inf, nan): + try: + ans = f(x) + print('%.4f' % ans) + except ValueError: + print(name, 'ValueError') + except OverflowError: + print(name, 'OverflowError') + +# double argument functions +for name, f, args in ( + ('pow', math.pow, ((0, 2), (-1, 2), (0, -1), (-1, 2.3))), + ('fmod', math.fmod, ((1.2, inf), (1.2, 0), (inf, 1.2))), + ('atan2', math.atan2, ((0, 0),)), + ('copysign', math.copysign, ()), + ): + for x in args + ((0, inf), (inf, 0), (inf, inf), (inf, nan), (nan, inf), (nan, nan)): + try: + ans = f(*x) + print('%.4f' % ans) + except ValueError: + print(name, 'ValueError') diff --git a/tests/float/math_domain_special.py b/tests/float/math_domain_special.py new file mode 100644 index 000000000..388920350 --- /dev/null +++ b/tests/float/math_domain_special.py @@ -0,0 +1,36 @@ +# Tests domain errors in special math functions + +try: + import math + math.erf +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + +inf = float('inf') +nan = float('nan') + +# single argument functions +for name, f, args in ( + ('expm1', math.exp, ()), + ('log2', math.log2, (-1, 0)), + ('log10', math.log10, (-1, 0)), + ('sinh', math.sinh, ()), + ('cosh', math.cosh, ()), + ('tanh', math.tanh, ()), + ('asinh', math.asinh, ()), + ('acosh', math.acosh, (-1, 0.9, 1)), + ('atanh', math.atanh, (-1, 1)), + ('erf', math.erf, ()), + ('erfc', math.erfc, ()), + ('gamma', math.gamma, (-2, -1, 0, 1)), + ('lgamma', math.lgamma, (-2, -1, 0, 1)), + ): + for x in args + (inf, nan): + try: + ans = f(x) + print('%.4f' % ans) + except ValueError: + print(name, 'ValueError') + except OverflowError: + print(name, 'OverflowError') From d8d4e4dfbe4ca76a1b0adf4f1938562a308c6d48 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 21:46:38 +1100 Subject: [PATCH 108/163] py/modmath: Convert log2 macro into a function. So that a pointer to it can be passed as a pointer to math_generic_1. This patch also makes the function work for single and double precision floating point. --- py/modmath.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/py/modmath.c b/py/modmath.c index 6c248844d..7eda7594d 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -85,8 +85,12 @@ STATIC mp_obj_t math_generic_2(mp_obj_t x_obj, mp_obj_t y_obj, mp_float_t (*f)(m STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); #if MP_NEED_LOG2 +#undef log2 +#undef log2f // 1.442695040888963407354163704 is 1/_M_LN2 -#define log2(x) (log(x) * 1.442695040888963407354163704) +mp_float_t MICROPY_FLOAT_C_FUN(log2)(mp_float_t x) { + return MICROPY_FLOAT_C_FUN(log)(x) * MICROPY_FLOAT_CONST(1.442695040888963407354163704); +} #endif // sqrt(x): returns the square root of x From 81a06d2c9c3ce081043e1eb948b65014f1b1786a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 21:48:14 +1100 Subject: [PATCH 109/163] lib/libm: Remove implementation of log2f, use MP_NEED_LOG2 instead. --- lib/libm/math.c | 10 ---------- ports/stm32/mpconfigport.h | 3 +++ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/libm/math.c b/lib/libm/math.c index 5e00740d1..6b65202cf 100644 --- a/lib/libm/math.c +++ b/lib/libm/math.c @@ -48,16 +48,6 @@ float copysignf(float x, float y) { } #endif -// some compilers define log2f in terms of logf -#ifdef log2f -#undef log2f -#endif -// some compilers have _M_LN2 defined in math.h, some don't -#ifndef _M_LN2 -#define _M_LN2 (0.69314718055994530942) -#endif -float log2f(float x) { return logf(x) / (float)_M_LN2; } - static const float _M_LN10 = 2.30258509299404; // 0x40135d8e float log10f(float x) { return logf(x) / (float)_M_LN10; } diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 7888b5355..06606e62a 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -335,6 +335,9 @@ static inline mp_uint_t disable_irq(void) { } while (0); #endif +// We need an implementation of the log2 function which is not a macro +#define MP_NEED_LOG2 (1) + // There is no classical C heap in bare-metal ports, only Python // garbage-collected heap. For completeness, emulate C heap via // GC heap. Note that MicroPython core never uses malloc() and friends, From 08a196697c5dfb8cbe3b3e85c1c5f94e3a27804c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Oct 2017 23:15:55 +1100 Subject: [PATCH 110/163] py/formatfloat: Don't print the negative sign of a NaN value. NaN may have the sign bit set but it has no meaning, so don't print it out. --- py/formatfloat.c | 4 ++-- tests/float/complex1.py | 1 + tests/float/float1.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/py/formatfloat.c b/py/formatfloat.c index 35cd5d51a..b61a958a2 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "py/formatfloat.h" /*********************************************************************** @@ -82,7 +83,6 @@ static inline int fp_isless1(float x) { union floatbits fb = {x}; return fb.u < #define FPROUND_TO_ONE 0.999999999995 #define FPDECEXP 256 #define FPMIN_BUF_SIZE 7 // +9e+199 -#include #define fp_signbit(x) signbit(x) #define fp_isspecial(x) 1 #define fp_isnan(x) isnan(x) @@ -122,7 +122,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } return buf_size >= 2; } - if (fp_signbit(f)) { + if (fp_signbit(f) && !isnan(f)) { *s++ = '-'; f = -f; } else { diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 854410545..479b4b348 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -59,6 +59,7 @@ # check printing of inf/nan print(float('nan') * 1j) +print(float('-nan') * 1j) print(float('inf') * (1 + 1j)) print(float('-inf') * (1 + 1j)) diff --git a/tests/float/float1.py b/tests/float/float1.py index 137dacc23..c64f965a7 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -21,6 +21,7 @@ print(float("infinity")) print(float("INFINITY")) print(float("nan")) +print(float("-nan")) print(float("NaN")) try: float("") From dc948e4d54eb8f7fecbd26efcfb3cd5d02bf8500 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 10 Oct 2017 16:27:54 +1100 Subject: [PATCH 111/163] py/formatfloat: Use standard isinf, isnan funcs instead of custom ones. Reduces code size by a tiny bit. --- py/formatfloat.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/py/formatfloat.c b/py/formatfloat.c index b61a958a2..4228f99ff 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -69,12 +69,10 @@ union floatbits { uint32_t u; }; static inline int fp_signbit(float x) { union floatbits fb = {x}; return fb.u & FLT_SIGN_MASK; } -static inline int fp_isspecial(float x) { union floatbits fb = {x}; return (fb.u & FLT_EXP_MASK) == FLT_EXP_MASK; } -static inline int fp_isinf(float x) { union floatbits fb = {x}; return (fb.u & FLT_MAN_MASK) == 0; } +#define fp_isnan(x) isnan(x) +#define fp_isinf(x) isinf(x) static inline int fp_iszero(float x) { union floatbits fb = {x}; return fb.u == 0; } static inline int fp_isless1(float x) { union floatbits fb = {x}; return fb.u < 0x3f800000; } -// Assumes both fp_isspecial() and fp_isinf() were applied before -#define fp_isnan(x) 1 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE @@ -84,7 +82,6 @@ static inline int fp_isless1(float x) { union floatbits fb = {x}; return fb.u < #define FPDECEXP 256 #define FPMIN_BUF_SIZE 7 // +9e+199 #define fp_signbit(x) signbit(x) -#define fp_isspecial(x) 1 #define fp_isnan(x) isnan(x) #define fp_isinf(x) isinf(x) #define fp_iszero(x) (x == 0) @@ -122,7 +119,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } return buf_size >= 2; } - if (fp_signbit(f) && !isnan(f)) { + if (fp_signbit(f) && !fp_isnan(f)) { *s++ = '-'; f = -f; } else { @@ -135,7 +132,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch // It is buf_size minus room for the sign and null byte. int buf_remaining = buf_size - 1 - (s - buf); - if (fp_isspecial(f)) { + { char uc = fmt & 0x20; if (fp_isinf(f)) { *s++ = 'I' ^ uc; From 69da74e53830412cf88ff577aa7d3cbf5741eb83 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Oct 2017 11:25:20 +1100 Subject: [PATCH 112/163] py/modbuiltins: Use existing utf8_get_char helper in builtin ord func. --- py/modbuiltins.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 82b08cdc9..65c03d523 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -348,31 +348,16 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { if (MP_OBJ_IS_STR(o_in)) { len = unichar_charlen(str, len); if (len == 1) { - if (!UTF8_IS_NONASCII(*str)) { - goto return_first_byte; - } - mp_int_t ord = *str++ & 0x7F; - for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) { - ord &= ~mask; - } - while (UTF8_IS_CONT(*str)) { - ord = (ord << 6) | (*str++ & 0x3F); - } - return mp_obj_new_int(ord); + return mp_obj_new_int(utf8_get_char((const byte*)str)); } - } else { - // a bytes object + } else + #endif + { + // a bytes object, or a str without unicode support (don't sign extend the char) if (len == 1) { - return_first_byte: return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]); } } - #else - if (len == 1) { - // don't sign extend when converting to ord - return mp_obj_new_int(((const byte*)str)[0]); - } - #endif if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError("ord expects a character"); From b1457db002a7205727f49c48f56ed270e2568d6a Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Wed, 11 Oct 2017 01:24:44 +1100 Subject: [PATCH 113/163] docs/library: Add missing cross-ref links for classes in pyb module. --- docs/library/pyb.Accel.rst | 1 + docs/library/pyb.CAN.rst | 1 + docs/library/pyb.LCD.rst | 1 + docs/library/pyb.Switch.rst | 1 + docs/library/pyb.USB_HID.rst | 1 + docs/library/pyb.USB_VCP.rst | 1 + 6 files changed, 6 insertions(+) diff --git a/docs/library/pyb.Accel.rst b/docs/library/pyb.Accel.rst index 061996485..9ade5c5c8 100644 --- a/docs/library/pyb.Accel.rst +++ b/docs/library/pyb.Accel.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.Accel: class Accel -- accelerometer control ==================================== diff --git a/docs/library/pyb.CAN.rst b/docs/library/pyb.CAN.rst index 9e71f12b0..232d04d96 100644 --- a/docs/library/pyb.CAN.rst +++ b/docs/library/pyb.CAN.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.CAN: class CAN -- controller area network communication bus ====================================================== diff --git a/docs/library/pyb.LCD.rst b/docs/library/pyb.LCD.rst index 83cf890b6..5ab127edc 100644 --- a/docs/library/pyb.LCD.rst +++ b/docs/library/pyb.LCD.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.LCD: class LCD -- LCD control for the LCD touch-sensor pyskin ======================================================== diff --git a/docs/library/pyb.Switch.rst b/docs/library/pyb.Switch.rst index 0d5dc63b7..e5ab6bd84 100644 --- a/docs/library/pyb.Switch.rst +++ b/docs/library/pyb.Switch.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.Switch: class Switch -- switch object ============================= diff --git a/docs/library/pyb.USB_HID.rst b/docs/library/pyb.USB_HID.rst index 7d17c3099..702704435 100644 --- a/docs/library/pyb.USB_HID.rst +++ b/docs/library/pyb.USB_HID.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.USB_HID: class USB_HID -- USB Human Interface Device (HID) ================================================= diff --git a/docs/library/pyb.USB_VCP.rst b/docs/library/pyb.USB_VCP.rst index 4c4fe4516..80cc40cdd 100644 --- a/docs/library/pyb.USB_VCP.rst +++ b/docs/library/pyb.USB_VCP.rst @@ -1,4 +1,5 @@ .. currentmodule:: pyb +.. _pyb.USB_VCP: class USB_VCP -- USB virtual comm port ====================================== From 1b7d6a795149588eb44c1b33dd7c34fe6669460a Mon Sep 17 00:00:00 2001 From: Vitor Massaru Iha Date: Sun, 8 Oct 2017 15:28:32 -0300 Subject: [PATCH 114/163] esp8266/modules/webrepl_setup: Add info about allowed password length. This patch also makes the code more concise by combining the checks for the password length. --- ports/esp8266/modules/webrepl_setup.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ports/esp8266/modules/webrepl_setup.py b/ports/esp8266/modules/webrepl_setup.py index d91600e6e..5288c49c0 100644 --- a/ports/esp8266/modules/webrepl_setup.py +++ b/ports/esp8266/modules/webrepl_setup.py @@ -17,12 +17,9 @@ def getpass(prompt): def input_pass(): while 1: - passwd1 = getpass("New password: ") - if len(passwd1) < 4: - print("Password too short") - continue - elif len(passwd1) > 9: - print("Password too long") + passwd1 = getpass("New password (4-9 chars): ") + if len(passwd1) < 4 or len(passwd1) > 9: + print("Invalid password length") continue passwd2 = getpass("Confirm password: ") if passwd1 == passwd2: From a3afa8cfc46913f471d5eb55da4ae22dee92c25f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Oct 2017 18:54:34 +1100 Subject: [PATCH 115/163] py/emitnative: Implement floor-division and modulo for viper emitter. --- py/emitnative.c | 16 ++++++++++++ py/nativeglue.c | 3 +++ py/runtime0.h | 2 ++ tests/micropython/viper_binop_divmod.py | 18 +++++++++++++ tests/micropython/viper_binop_divmod.py.exp | 28 +++++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 tests/micropython/viper_binop_divmod.py create mode 100644 tests/micropython/viper_binop_divmod.py.exp diff --git a/py/emitnative.c b/py/emitnative.c index b2c9a7366..e576f767c 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -123,6 +123,8 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = { [MP_F_NEW_CELL] = 1, [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3, [MP_F_SETUP_CODE_STATE] = 5, + [MP_F_SMALL_INT_FLOOR_DIVIDE] = 2, + [MP_F_SMALL_INT_MODULO] = 2, }; #include "py/asmx86.h" @@ -1843,6 +1845,20 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { return; } #endif + + // special cases for floor-divide and module because we dispatch to helper functions + if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_INPLACE_FLOOR_DIVIDE + || op == MP_BINARY_OP_MODULO || op == MP_BINARY_OP_INPLACE_MODULO) { + emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1); + if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_INPLACE_FLOOR_DIVIDE) { + emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE); + } else { + emit_call(emit, MP_F_SMALL_INT_MODULO); + } + emit_post_push_reg(emit, VTYPE_INT, REG_RET); + return; + } + int reg_rhs = REG_ARG_3; emit_pre_pop_reg_flexible(emit, &vtype_rhs, ®_rhs, REG_RET, REG_ARG_2); emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2); diff --git a/py/nativeglue.c b/py/nativeglue.c index 61b624ec7..e63c2fcda 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -29,6 +29,7 @@ #include #include "py/runtime.h" +#include "py/smallint.h" #include "py/emitglue.h" #include "py/bc.h" @@ -170,6 +171,8 @@ void *const mp_fun_table[MP_F_NUMBER_OF] = { mp_obj_new_cell, mp_make_closure_from_raw_code, mp_setup_code_state, + mp_small_int_floor_divide, + mp_small_int_modulo, }; /* diff --git a/py/runtime0.h b/py/runtime0.h index 3edd8918b..a72b7feb7 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -185,6 +185,8 @@ typedef enum { MP_F_NEW_CELL, MP_F_MAKE_CLOSURE_FROM_RAW_CODE, MP_F_SETUP_CODE_STATE, + MP_F_SMALL_INT_FLOOR_DIVIDE, + MP_F_SMALL_INT_MODULO, MP_F_NUMBER_OF, } mp_fun_kind_t; diff --git a/tests/micropython/viper_binop_divmod.py b/tests/micropython/viper_binop_divmod.py new file mode 100644 index 000000000..822424982 --- /dev/null +++ b/tests/micropython/viper_binop_divmod.py @@ -0,0 +1,18 @@ +# test floor-division and modulo operators + +@micropython.viper +def div(x:int, y:int) -> int: + return x // y + +@micropython.viper +def mod(x:int, y:int) -> int: + return x % y + +def dm(x, y): + print(div(x, y), mod(x, y)) + +for x in (-6, 6): + for y in range(-7, 8): + if y == 0: + continue + dm(x, y) diff --git a/tests/micropython/viper_binop_divmod.py.exp b/tests/micropython/viper_binop_divmod.py.exp new file mode 100644 index 000000000..4fc971d46 --- /dev/null +++ b/tests/micropython/viper_binop_divmod.py.exp @@ -0,0 +1,28 @@ +0 -6 +1 0 +1 -1 +1 -2 +2 0 +3 0 +6 0 +-6 0 +-3 0 +-2 0 +-2 2 +-2 4 +-1 0 +-1 1 +-1 -1 +-1 0 +-2 -4 +-2 -2 +-2 0 +-3 0 +-6 0 +6 0 +3 0 +2 0 +1 2 +1 1 +1 0 +0 6 From c59fc1419d5760d7be0bcad1ebdfb70b43fe7093 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Oct 2017 12:26:49 +1100 Subject: [PATCH 116/163] py/emitnative: Simplify binary op emitter, no need to check inplace ops. --- py/emitnative.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/py/emitnative.c b/py/emitnative.c index e576f767c..8e97dda11 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -1825,18 +1825,20 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { vtype_kind_t vtype_lhs = peek_vtype(emit, 1); vtype_kind_t vtype_rhs = peek_vtype(emit, 0); if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) { + // for integers, inplace and normal ops are equivalent, so use just normal ops + if (MP_BINARY_OP_INPLACE_OR <= op && op <= MP_BINARY_OP_INPLACE_POWER) { + op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR; + } + #if N_X64 || N_X86 // special cases for x86 and shifting - if (op == MP_BINARY_OP_LSHIFT - || op == MP_BINARY_OP_INPLACE_LSHIFT - || op == MP_BINARY_OP_RSHIFT - || op == MP_BINARY_OP_INPLACE_RSHIFT) { + if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_RSHIFT) { #if N_X64 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET); #else emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET); #endif - if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) { + if (op == MP_BINARY_OP_LSHIFT) { ASM_LSL_REG(emit->as, REG_RET); } else { ASM_ASR_REG(emit->as, REG_RET); @@ -1847,10 +1849,9 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { #endif // special cases for floor-divide and module because we dispatch to helper functions - if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_INPLACE_FLOOR_DIVIDE - || op == MP_BINARY_OP_MODULO || op == MP_BINARY_OP_INPLACE_MODULO) { + if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_MODULO) { emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1); - if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_INPLACE_FLOOR_DIVIDE) { + if (op == MP_BINARY_OP_FLOOR_DIVIDE) { emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE); } else { emit_call(emit, MP_F_SMALL_INT_MODULO); @@ -1865,29 +1866,29 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { if (0) { // dummy #if !(N_X64 || N_X86) - } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) { + } else if (op == MP_BINARY_OP_LSHIFT) { ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) { + } else if (op == MP_BINARY_OP_RSHIFT) { ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); #endif - } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) { + } else if (op == MP_BINARY_OP_OR) { ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) { + } else if (op == MP_BINARY_OP_XOR) { ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) { + } else if (op == MP_BINARY_OP_AND) { ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) { + } else if (op == MP_BINARY_OP_ADD) { ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) { + } else if (op == MP_BINARY_OP_SUBTRACT) { ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); - } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) { + } else if (op == MP_BINARY_OP_MULTIPLY) { ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs); emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) { From 7c7c7b161df5be99aee9dea444b6e4beacf78438 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 13 Oct 2017 12:00:47 +1100 Subject: [PATCH 117/163] stm32/usbd_cdc_interface: Don't reset CDC output buf on initialisation. So that characters can be buffered before the USB device is connected (restoring behviour of the driver before recent state refactoring). --- ports/stm32/usbd_cdc_interface.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 4c49c9321..2e9fba917 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -57,10 +57,14 @@ #define CDC_SEND_BREAK 0x23 uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, usbd_cdc_msc_hid_state_t *usbd) { + // Link the parent state cdc->usbd = usbd; + + // Reset all the CDC state + // Note: we don't reset tx_buf_ptr_in in order to allow the output buffer to + // be filled (by usbd_cdc_tx_always) before the USB device is connected. cdc->rx_buf_put = 0; cdc->rx_buf_get = 0; - cdc->tx_buf_ptr_in = 0; cdc->tx_buf_ptr_out = 0; cdc->tx_buf_ptr_out_shadow = 0; cdc->tx_buf_ptr_wait_count = 0; From e39fcda8eb1bfc1ccc1660079189f9ae39392abe Mon Sep 17 00:00:00 2001 From: Li Weiwei Date: Fri, 13 Oct 2017 09:14:07 +0800 Subject: [PATCH 118/163] stm32/usbd_cdc_interface.h: Fix code comments after recent refactor. --- ports/stm32/usbd_cdc_interface.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/usbd_cdc_interface.h b/ports/stm32/usbd_cdc_interface.h index 6dcab7d47..98b8fc077 100644 --- a/ports/stm32/usbd_cdc_interface.h +++ b/ports/stm32/usbd_cdc_interface.h @@ -43,8 +43,8 @@ typedef struct _usbd_cdc_itf_t { uint16_t rx_buf_get; // circular buffer index uint8_t tx_buf[USBD_CDC_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer - uint16_t tx_buf_ptr_in; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available - volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained + uint16_t tx_buf_ptr_in; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when new data is available + volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when data is drained uint16_t tx_buf_ptr_out_shadow; // shadow of above uint8_t tx_buf_ptr_wait_count; // used to implement a timeout waiting for low-level USB driver uint8_t tx_need_empty_packet; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size From 37282f8fc135a16ff36e2afe0de907cbde531ed0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 13 Oct 2017 20:01:57 +1100 Subject: [PATCH 119/163] extmod/uos_dupterm: Update uos.dupterm() and helper funcs to have index. The uos.dupterm() signature and behaviour is updated to reflect the latest enhancements in the docs. It has minor backwards incompatibility in that it no longer accepts zero arguments. The dupterm_rx helper function is moved from esp8266 to extmod and generalised to support multiple dupterm slots. A port can specify multiple slots by defining the MICROPY_PY_OS_DUPTERM config macro to an integer, being the number of slots it wants to have; 0 means to disable the dupterm feature altogether. The unix and esp8266 ports are updated to work with the new interface and are otherwise unchanged with respect to functionality. --- extmod/misc.h | 3 +- extmod/uos_dupterm.c | 96 ++++++++++++++++++++++++-------- ports/esp8266/esp_mphal.c | 37 +----------- ports/esp8266/main.c | 2 - ports/esp8266/modules/webrepl.py | 4 +- ports/unix/unix_mphal.c | 17 +++--- py/mpstate.h | 2 +- py/runtime.c | 7 +++ 8 files changed, 95 insertions(+), 73 deletions(-) diff --git a/extmod/misc.h b/extmod/misc.h index 6c13592c7..d6f6d859c 100644 --- a/extmod/misc.h +++ b/extmod/misc.h @@ -35,8 +35,9 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj); #if MICROPY_PY_OS_DUPTERM +int mp_uos_dupterm_rx_chr(void); void mp_uos_dupterm_tx_strn(const char *str, size_t len); -void mp_uos_deactivate(const char *msg, mp_obj_t exc); +void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc); #else #define mp_uos_dupterm_tx_strn(s, l) #endif diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c index 1d6f02dce..d4326d326 100644 --- a/extmod/uos_dupterm.c +++ b/extmod/uos_dupterm.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2016 Paul Sokolovsky + * Copyright (c) 2017 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,12 +32,13 @@ #include "py/objtuple.h" #include "py/objarray.h" #include "py/stream.h" +#include "lib/utils/interrupt_char.h" #if MICROPY_PY_OS_DUPTERM -void mp_uos_deactivate(const char *msg, mp_obj_t exc) { - mp_obj_t term = MP_STATE_PORT(term_obj); - MP_STATE_PORT(term_obj) = NULL; +void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) { + mp_obj_t term = MP_STATE_VM(dupterm_objs[dupterm_idx]); + MP_STATE_VM(dupterm_objs[dupterm_idx]) = MP_OBJ_NULL; mp_printf(&mp_plat_print, msg); if (exc != MP_OBJ_NULL) { mp_obj_print_exception(&mp_plat_print, exc); @@ -44,48 +46,94 @@ void mp_uos_deactivate(const char *msg, mp_obj_t exc) { mp_stream_close(term); } +int mp_uos_dupterm_rx_chr(void) { + for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { + if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) { + continue; + } + + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_t readinto_m[3]; + mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_readinto, readinto_m); + readinto_m[2] = MP_STATE_VM(dupterm_arr_obj); + mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m); + if (res == mp_const_none) { + nlr_pop(); + } else if (res == MP_OBJ_NEW_SMALL_INT(0)) { + mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL); + nlr_pop(); + } else { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(MP_STATE_VM(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ); + nlr_pop(); + if (*(byte*)bufinfo.buf == mp_interrupt_char) { + // Signal keyboard interrupt to be raised as soon as the VM resumes + mp_keyboard_interrupt(); + return -2; + } + return *(byte*)bufinfo.buf; + } + } else { + mp_uos_deactivate(idx, "dupterm: Exception in read() method, deactivating: ", nlr.ret_val); + } + } + + // No chars available + return -1; +} + void mp_uos_dupterm_tx_strn(const char *str, size_t len) { - if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { + for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { + if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) { + continue; + } nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_obj_t write_m[3]; - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); + mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_write, write_m); - mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); + mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj)); void *org_items = arr->items; arr->items = (void*)str; arr->len = len; - write_m[2] = MP_STATE_PORT(dupterm_arr_obj); + write_m[2] = MP_STATE_VM(dupterm_arr_obj); mp_call_method_n_kw(1, 0, write_m); - arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); + arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj)); arr->items = org_items; arr->len = 1; nlr_pop(); } else { - mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val); + mp_uos_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", nlr.ret_val); } } } STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) { - return mp_const_none; - } else { - return MP_STATE_PORT(term_obj); - } + mp_int_t idx = 0; + if (n_args == 2) { + idx = mp_obj_get_int(args[1]); + } + + if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) { + mp_raise_ValueError("invalid dupterm index"); + } + + mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]); + if (previous_obj == MP_OBJ_NULL) { + previous_obj = mp_const_none; + } + if (args[0] == mp_const_none) { + MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL; } else { - if (args[0] == mp_const_none) { - MP_STATE_PORT(term_obj) = MP_OBJ_NULL; - } else { - MP_STATE_PORT(term_obj) = args[0]; - if (MP_STATE_PORT(dupterm_arr_obj) == MP_OBJ_NULL) { - MP_STATE_PORT(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); - } + MP_STATE_VM(dupterm_objs[idx]) = args[0]; + if (MP_STATE_VM(dupterm_arr_obj) == MP_OBJ_NULL) { + MP_STATE_VM(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); } - return mp_const_none; } + + return previous_obj; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 0, 1, mp_uos_dupterm); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 1, 2, mp_uos_dupterm); #endif diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 71d4c5062..9f4f051fd 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -155,41 +155,6 @@ void mp_hal_signal_input(void) { #endif } -static int call_dupterm_read(void) { - if (MP_STATE_PORT(term_obj) == NULL) { - return -1; - } - - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_obj_t readinto_m[3]; - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_readinto, readinto_m); - readinto_m[2] = MP_STATE_PORT(dupterm_arr_obj); - mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m); - if (res == mp_const_none) { - nlr_pop(); - return -2; - } - if (res == MP_OBJ_NEW_SMALL_INT(0)) { - mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); - nlr_pop(); - return -1; - } - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(MP_STATE_PORT(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ); - nlr_pop(); - if (*(byte*)bufinfo.buf == mp_interrupt_char) { - mp_keyboard_interrupt(); - return -2; - } - return *(byte*)bufinfo.buf; - } else { - mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val); - } - - return -1; -} - STATIC void dupterm_task_handler(os_event_t *evt) { static byte lock; if (lock) { @@ -197,7 +162,7 @@ STATIC void dupterm_task_handler(os_event_t *evt) { } lock = 1; while (1) { - int c = call_dupterm_read(); + int c = mp_uos_dupterm_rx_chr(); if (c < 0) { break; } diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c index 7f5dca84e..d1b88a8ce 100644 --- a/ports/esp8266/main.c +++ b/ports/esp8266/main.c @@ -51,8 +51,6 @@ STATIC void mp_reset(void) { mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_)); mp_obj_list_init(mp_sys_argv, 0); - MP_STATE_PORT(term_obj) = MP_OBJ_NULL; - MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA extern void esp_native_code_init(void); esp_native_code_init(); diff --git a/ports/esp8266/modules/webrepl.py b/ports/esp8266/modules/webrepl.py index 5a76e9b26..aa156d148 100644 --- a/ports/esp8266/modules/webrepl.py +++ b/ports/esp8266/modules/webrepl.py @@ -31,7 +31,9 @@ def setup_conn(port, accept_handler): def accept_conn(listen_sock): global client_s cl, remote_addr = listen_sock.accept() - if uos.dupterm(): + prev = uos.dupterm(None) + uos.dupterm(prev) + if prev: print("\nConcurrent WebREPL connection from", remote_addr, "rejected") cl.close() return diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 2b273d834..f27c62fd1 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -108,11 +108,11 @@ void mp_hal_stdio_mode_orig(void) { #endif #if MICROPY_PY_OS_DUPTERM -static int call_dupterm_read(void) { +static int call_dupterm_read(size_t idx) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_obj_t read_m[3]; - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m); + mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_read, read_m); read_m[2] = MP_OBJ_NEW_SMALL_INT(1); mp_obj_t res = mp_call_method_n_kw(1, 0, read_m); if (res == mp_const_none) { @@ -122,18 +122,18 @@ static int call_dupterm_read(void) { mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); if (bufinfo.len == 0) { mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n"); - MP_STATE_PORT(term_obj) = NULL; + MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL; return -1; } nlr_pop(); return *(byte*)bufinfo.buf; } else { // Temporarily disable dupterm to avoid infinite recursion - mp_obj_t save_term = MP_STATE_PORT(term_obj); - MP_STATE_PORT(term_obj) = NULL; + mp_obj_t save_term = MP_STATE_VM(dupterm_objs[idx]); + MP_STATE_VM(dupterm_objs[idx]) = NULL; mp_printf(&mp_plat_print, "dupterm: "); mp_obj_print_exception(&mp_plat_print, nlr.ret_val); - MP_STATE_PORT(term_obj) = save_term; + MP_STATE_VM(dupterm_objs[idx]) = save_term; } return -1; @@ -143,10 +143,11 @@ static int call_dupterm_read(void) { int mp_hal_stdin_rx_chr(void) { unsigned char c; #if MICROPY_PY_OS_DUPTERM - if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { + // TODO only support dupterm one slot at the moment + if (MP_STATE_VM(dupterm_objs[0]) != MP_OBJ_NULL) { int c; do { - c = call_dupterm_read(); + c = call_dupterm_read(0); } while (c == -2); if (c == -1) { goto main_term; diff --git a/py/mpstate.h b/py/mpstate.h index eca14a9e4..6a39ebdea 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -168,7 +168,7 @@ typedef struct _mp_state_vm_t { // root pointers for extmod #if MICROPY_PY_OS_DUPTERM - mp_obj_t term_obj; + mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM]; mp_obj_t dupterm_arr_obj; #endif diff --git a/py/runtime.c b/py/runtime.c index 069548deb..17e5d235c 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -102,6 +102,13 @@ void mp_init(void) { MP_STATE_VM(mp_module_builtins_override_dict) = NULL; #endif + #if MICROPY_PY_OS_DUPTERM + for (size_t i = 0; i < MICROPY_PY_OS_DUPTERM; ++i) { + MP_STATE_VM(dupterm_objs[i]) = MP_OBJ_NULL; + } + MP_STATE_VM(dupterm_arr_obj) = MP_OBJ_NULL; + #endif + #if MICROPY_FSUSERMOUNT // zero out the pointers to the user-mounted devices memset(MP_STATE_VM(fs_user_mount), 0, sizeof(MP_STATE_VM(fs_user_mount))); From 829c329dafb5006f73dc38e499978b9ccc96e6b7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 15 Oct 2017 10:17:24 +0300 Subject: [PATCH 120/163] README: Add explicit section on contributing. To increase visibility of Contributors' Guidelines and Code Conventions docs. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 13529ff4f..00a0405f3 100644 --- a/README.md +++ b/README.md @@ -162,3 +162,13 @@ This will use the included `tools/pydfu.py` script. If flashing the firmware does not work it may be because you don't have the correct permissions, and need to use `sudo make deploy`. See the README.md file in the ports/stm32/ directory for further details. + +Contributing +------------ + +MicroPython is an open-source project and welcomes contributions. To be +productive, please be sure to follow the +[Contributors' Guidelines](https://github.com/micropython/micropython/wiki/ContributorGuidelines) +and the [Code Conventions](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md). +Note that MicroPython is licenced under the MIT license, and all contributions +should follow this license. From 65ba481cb0e067a97f8a999e2522b0656ca3b7fe Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 13:16:00 +1100 Subject: [PATCH 121/163] stm32/modnwwiznet5k: Implement WIZNET5K.isconnected() method. --- docs/library/network.rst | 5 +++++ ports/stm32/modnwwiznet5k.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/docs/library/network.rst b/docs/library/network.rst index 258b2a20c..acb578a7c 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -268,6 +268,11 @@ parameter should be `id`. Methods ------- + .. method:: wiznet5k.isconnected() + + Returns ``True`` if the physical Ethernet link is connected and up. + Returns ``False`` otherwise. + .. method:: wiznet5k.ifconfig([(ip, subnet, gateway, dns)]) Get/set IP address, subnet mask, gateway and DNS. diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index 78249816d..a9e5f5aa9 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -414,6 +414,12 @@ STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_regs_obj, wiznet5k_regs); +STATIC mp_obj_t wiznet5k_isconnected(mp_obj_t self_in) { + (void)self_in; + return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_isconnected_obj, wiznet5k_isconnected); + /// \method ifconfig([(ip, subnet, gateway, dns)]) /// Get/set IP address, subnet mask, gateway and DNS. STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) { @@ -445,6 +451,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&wiznet5k_regs_obj) }, { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) }, + { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&wiznet5k_isconnected_obj) }, }; STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table); From 5d7b0b237b502b7511624bad5fd8fc3092d91972 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 13:17:15 +1100 Subject: [PATCH 122/163] stm32/modusocket: Make getaddrinfo() work when passed an IP address. --- ports/stm32/modusocket.c | 56 +++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/ports/stm32/modusocket.c b/ports/stm32/modusocket.c index c5ab3b6d7..4d14c355f 100644 --- a/ports/stm32/modusocket.c +++ b/ports/stm32/modusocket.c @@ -390,29 +390,49 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { size_t hlen; const char *host = mp_obj_str_get_data(host_in, &hlen); mp_int_t port = mp_obj_get_int(port_in); + uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE]; + bool have_ip = false; + + if (hlen > 0) { + // check if host is already in IP form + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + netutils_parse_ipv4_addr(host_in, out_ip, NETUTILS_BIG); + have_ip = true; + nlr_pop(); + } else { + // swallow exception: host was not in IP form so need to do DNS lookup + } + } - // find a NIC that can do a name lookup - for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { - mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; - mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); - if (nic_type->gethostbyname != NULL) { - uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE]; - int ret = nic_type->gethostbyname(nic, host, hlen, out_ip); - if (ret != 0) { - // TODO CPython raises: socket.gaierror: [Errno -2] Name or service not known - mp_raise_OSError(ret); + if (!have_ip) { + // find a NIC that can do a name lookup + for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { + mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; + mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); + if (nic_type->gethostbyname != NULL) { + int ret = nic_type->gethostbyname(nic, host, hlen, out_ip); + if (ret != 0) { + // TODO CPython raises: socket.gaierror: [Errno -2] Name or service not known + mp_raise_OSError(ret); + } + have_ip = true; + break; } - mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); - tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET); - tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM); - tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); - tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); - tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_BIG); - return mp_obj_new_list(1, (mp_obj_t*)&tuple); } } - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC")); + if (!have_ip) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC")); + } + + mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET); + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM); + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); + tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); + tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_BIG); + return mp_obj_new_list(1, (mp_obj_t*)&tuple); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_usocket_getaddrinfo_obj, mod_usocket_getaddrinfo); From 0a30ad96c886aacf897b3e87cbdb24fb46c5c43d Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 13:19:11 +1100 Subject: [PATCH 123/163] stm32/modusocket: Return OSError(-2) if getaddrinfo fails. This matches the behaviour of getaddrinfo in extmod/modlwip.c. --- ports/stm32/modnwcc3k.c | 2 +- ports/stm32/modnwwiznet5k.c | 2 +- ports/stm32/modusocket.c | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ports/stm32/modnwcc3k.c b/ports/stm32/modnwcc3k.c index 551206da8..8cc0a613d 100644 --- a/ports/stm32/modnwcc3k.c +++ b/ports/stm32/modnwcc3k.c @@ -125,7 +125,7 @@ STATIC int cc3k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uin if (ip == 0) { // unknown host - return MP_ENOENT; + return -2; } out_ip[0] = ip >> 24; diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index a9e5f5aa9..9e9fb9aea 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -92,7 +92,7 @@ STATIC int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, return 0; } else { // failure - return MP_ENOENT; + return -2; } } diff --git a/ports/stm32/modusocket.c b/ports/stm32/modusocket.c index 4d14c355f..71a237b0d 100644 --- a/ports/stm32/modusocket.c +++ b/ports/stm32/modusocket.c @@ -413,7 +413,6 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { if (nic_type->gethostbyname != NULL) { int ret = nic_type->gethostbyname(nic, host, hlen, out_ip); if (ret != 0) { - // TODO CPython raises: socket.gaierror: [Errno -2] Name or service not known mp_raise_OSError(ret); } have_ip = true; From 5c437963d742627ba172d48d601c118c4577e7b7 Mon Sep 17 00:00:00 2001 From: Li Weiwei Date: Tue, 10 Oct 2017 14:22:22 +0800 Subject: [PATCH 124/163] stm32/mpconfigport.h: Add MICROPY_THREAD_YIELD() macro. --- ports/stm32/mpconfigport.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 06606e62a..2d59a1df8 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -326,6 +326,8 @@ static inline mp_uint_t disable_irq(void) { __WFI(); \ } \ } while (0); + +#define MICROPY_THREAD_YIELD() pyb_thread_yield() #else #define MICROPY_EVENT_POLL_HOOK \ do { \ @@ -333,6 +335,8 @@ static inline mp_uint_t disable_irq(void) { mp_handle_pending(); \ __WFI(); \ } while (0); + +#define MICROPY_THREAD_YIELD() #endif // We need an implementation of the log2 function which is not a macro From 73e387cff6a3e31a9ea0c300d92a1a4a62d791ef Mon Sep 17 00:00:00 2001 From: Li Weiwei Date: Tue, 10 Oct 2017 14:27:43 +0800 Subject: [PATCH 125/163] drivers/wiznet5k: Improve the performance of socket ops with threading. Use MICROPY_THREAD_YIELD() instead of HAL_Delay in busy waiting to improve the performance of connect, send, recv, sento and recvfrom. --- drivers/wiznet5k/ethernet/socket.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/wiznet5k/ethernet/socket.c b/drivers/wiznet5k/ethernet/socket.c index 129473ad4..ec25fcc79 100644 --- a/drivers/wiznet5k/ethernet/socket.c +++ b/drivers/wiznet5k/ethernet/socket.c @@ -52,10 +52,9 @@ #include +#include "py/mpthread.h" #include "socket.h" -extern void HAL_Delay(uint32_t); - #define SOCK_ANY_PORT_NUM 0xC000; static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; @@ -242,7 +241,7 @@ int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port) #endif return SOCKERR_TIMEOUT; } - HAL_Delay(1); + MICROPY_THREAD_YIELD(); } #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); @@ -317,6 +316,7 @@ int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len) } if( (sock_io_mode & (1< freesize) ) return SOCK_BUSY; if(len <= freesize) break; + MICROPY_THREAD_YIELD(); } wiz_send_data(sn, buf, len); #if _WIZCHIP_ == 5200 @@ -368,7 +368,7 @@ int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len) } if((sock_io_mode & (1< freesize) ) return SOCK_BUSY; if(len <= freesize) break; - HAL_Delay(1); + MICROPY_THREAD_YIELD(); }; wiz_send_data(sn, buf, len); @@ -446,7 +446,7 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t return SOCKERR_TIMEOUT; } //////////// - HAL_Delay(1); + MICROPY_THREAD_YIELD(); } #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); @@ -486,6 +486,7 @@ int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_ if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; if( (sock_io_mode & (1< Date: Mon, 16 Oct 2017 15:34:08 +1100 Subject: [PATCH 126/163] drivers/wiznet5k: Get low-level W5500 driver working. This patch implements the basic SPI read/write functions for the W5500 chip. It also allows _WIZCHIP_ to be configured externally to select the specific Wiznet chip. --- drivers/wiznet5k/ethernet/w5500/w5500.c | 10 ++++++++++ drivers/wiznet5k/ethernet/w5500/w5500.h | 1 - drivers/wiznet5k/ethernet/wizchip_conf.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/wiznet5k/ethernet/w5500/w5500.c b/drivers/wiznet5k/ethernet/w5500/w5500.c index 6b3111f99..3107b1b71 100644 --- a/drivers/wiznet5k/ethernet/w5500/w5500.c +++ b/drivers/wiznet5k/ethernet/w5500/w5500.c @@ -57,6 +57,16 @@ //////////////////////////////////////////////////// +#define LPC_SSP0 (0) + +static void Chip_SSP_ReadFrames_Blocking(int dummy, uint8_t *buf, uint32_t len) { + WIZCHIP.IF.SPI._read_bytes(buf, len); +} + +static void Chip_SSP_WriteFrames_Blocking(int dummy, const uint8_t *buf, uint32_t len) { + WIZCHIP.IF.SPI._write_bytes(buf, len); +} + uint8_t WIZCHIP_READ(uint32_t AddrSel) { uint8_t ret; diff --git a/drivers/wiznet5k/ethernet/w5500/w5500.h b/drivers/wiznet5k/ethernet/w5500/w5500.h index f0ffcb0c8..c2afb180e 100644 --- a/drivers/wiznet5k/ethernet/w5500/w5500.h +++ b/drivers/wiznet5k/ethernet/w5500/w5500.h @@ -44,7 +44,6 @@ #include #include "../wizchip_conf.h" -#include "board.h" #define _W5500_IO_BASE_ 0x00000000 diff --git a/drivers/wiznet5k/ethernet/wizchip_conf.h b/drivers/wiznet5k/ethernet/wizchip_conf.h index 55c79ae0a..4a7a7bd69 100644 --- a/drivers/wiznet5k/ethernet/wizchip_conf.h +++ b/drivers/wiznet5k/ethernet/wizchip_conf.h @@ -56,7 +56,9 @@ * @todo You should select one, \b 5100, \b 5200 ,\b 5500 or etc. \n\n * ex> #define \_WIZCHIP_ 5500 */ +#ifndef _WIZCHIP_ #define _WIZCHIP_ 5200 // 5100, 5200, 5500 +#endif #define _WIZCHIP_IO_MODE_NONE_ 0x0000 #define _WIZCHIP_IO_MODE_BUS_ 0x0100 /**< Bus interface mode */ From e36821a7662e9c65ee2bfb8c23925a10d2167faf Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 15:36:57 +1100 Subject: [PATCH 127/163] stm32/modnwwiznet5k: Add support for W5500 Ethernet chip. Which Wiznet chip to use is a compile-time option: MICROPY_PY_WIZNET5K should be set to either 5200 or 5500 to support either one of these Ethernet chips. The driver is called network.WIZNET5K in both cases. Note that this commit introduces a breaking-change at the build level because previously the valid values for MICROPY_PY_WIZNET5K were 0 and 1 but now they are 0, 5200 and 5500. --- ports/stm32/Makefile | 6 +++--- ports/stm32/modnwwiznet5k.c | 14 ++++++++++++-- ports/stm32/mpconfigport.mk | 5 ++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 913f0913e..68b007471 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -286,13 +286,13 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\ class/src/usbd_msc_data.c \ ) -ifeq ($(MICROPY_PY_WIZNET5K),1) +ifneq ($(MICROPY_PY_WIZNET5K),0) WIZNET5K_DIR=drivers/wiznet5k INC += -I$(TOP)/$(WIZNET5K_DIR) -CFLAGS_MOD += -DMICROPY_PY_WIZNET5K=1 +CFLAGS_MOD += -DMICROPY_PY_WIZNET5K=$(MICROPY_PY_WIZNET5K) -D_WIZCHIP_=$(MICROPY_PY_WIZNET5K) SRC_MOD += modnwwiznet5k.c SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\ - ethernet/w5200/w5200.c \ + ethernet/w$(MICROPY_PY_WIZNET5K)/w$(MICROPY_PY_WIZNET5K).c \ ethernet/wizchip_conf.c \ ethernet/socket.c \ internet/dns/dns.c \ diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index 9e9fb9aea..d59125054 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -398,7 +398,12 @@ STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) { if (i % 16 == 0) { printf("\n %04x:", i); } - printf(" %02x", WIZCHIP_READ(i)); + #if MICROPY_PY_WIZNET5K == 5200 + uint32_t reg = i; + #else + uint32_t reg = _W5500_IO_BASE_ | i << 8; + #endif + printf(" %02x", WIZCHIP_READ(reg)); } for (int sn = 0; sn < 4; ++sn) { printf("\nWiz SREG[%d]:", sn); @@ -406,7 +411,12 @@ STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) { if (i % 16 == 0) { printf("\n %04x:", i); } - printf(" %02x", WIZCHIP_READ(WIZCHIP_SREG_ADDR(sn, i))); + #if MICROPY_PY_WIZNET5K == 5200 + uint32_t reg = WIZCHIP_SREG_ADDR(sn, i); + #else + uint32_t reg = _W5500_IO_BASE_ | i << 8 | WIZCHIP_SREG_BLOCK(sn) << 3; + #endif + printf(" %02x", WIZCHIP_READ(reg)); } } printf("\n"); diff --git a/ports/stm32/mpconfigport.mk b/ports/stm32/mpconfigport.mk index 64145383e..e708de6c1 100644 --- a/ports/stm32/mpconfigport.mk +++ b/ports/stm32/mpconfigport.mk @@ -1,6 +1,9 @@ # Enable/disable extra modules -# wiznet5k module for ethernet support +# wiznet5k module for ethernet support; valid values are: +# 0 : no Wiznet support +# 5200 : support for W5200 module +# 5500 : support for W5500 module MICROPY_PY_WIZNET5K ?= 0 # cc3k module for wifi support From 06f2fdbe6103be55f102962dd6e6a9b5d0a217d2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 15:51:32 +1100 Subject: [PATCH 128/163] travis: Update build command now that stm32 Wiznet config has changed. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 83ac2a112..fd23a3707 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ script: - make -C ports/bare-arm - make -C ports/qemu-arm test - make -C ports/stm32 - - make -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=1 MICROPY_PY_CC3K=1 + - make -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 - make -C ports/stm32 BOARD=STM32F769DISC - make -C ports/stm32 BOARD=STM32L476DISC - make -C ports/teensy From d90ade5e3e62b28c1f04d18f206509a488d62ec0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Oct 2017 15:51:56 +1100 Subject: [PATCH 129/163] docs/library/network: Update docs to state that W5500 is supported. --- docs/library/network.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/library/network.rst b/docs/library/network.rst index acb578a7c..3b0aa535e 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -224,7 +224,9 @@ parameter should be `id`. ============== This class allows you to control WIZnet5x00 Ethernet adaptors based on - the W5200 and W5500 chipsets (only W5200 tested). + the W5200 and W5500 chipsets. The particular chipset that is supported + by the firmware is selected at compile-time via the MICROPY_PY_WIZNET5K + option. Example usage:: From 285ac585322f0ffced76d25a6f19cd1fa30b3514 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 17 Oct 2017 16:31:12 +1100 Subject: [PATCH 130/163] stm32/modnwwiznet5k: Increase SPI bus speed to 42MHz. The W5200 and W5500 can support up to 80MHz so 42MHz (the maximum the pyboard can do in its standard configuration) should be safe. Tested to give around 1050000 kbytes/sec TCP download speed on a W5500, which is about 10% more than with the previous SPI speed of 21MHz. --- ports/stm32/modnwwiznet5k.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index d59125054..717d88b39 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -346,7 +346,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size wiznet5k_obj.spi->Init.CLKPolarity = SPI_POLARITY_LOW; // clock is low when idle wiznet5k_obj.spi->Init.CLKPhase = SPI_PHASE_1EDGE; // data latched on first edge, which is rising edge for low-idle wiznet5k_obj.spi->Init.NSS = SPI_NSS_SOFT; - wiznet5k_obj.spi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz + wiznet5k_obj.spi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz wiznet5k_obj.spi->Init.FirstBit = SPI_FIRSTBIT_MSB; wiznet5k_obj.spi->Init.TIMode = SPI_TIMODE_DISABLED; wiznet5k_obj.spi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; From 8fa3d2996c9d69b1a6ba20f6beb354f4ce0001c8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 17 Oct 2017 16:34:10 +1100 Subject: [PATCH 131/163] stm32/modnwwiznet5k: Implement stream ioctl for the Wiznet driver. Now supports polling for read and write ability. --- ports/stm32/modnwwiznet5k.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c index 717d88b39..763137c70 100644 --- a/ports/stm32/modnwwiznet5k.c +++ b/ports/stm32/modnwwiznet5k.c @@ -30,6 +30,7 @@ #include "py/objlist.h" #include "py/runtime.h" +#include "py/stream.h" #include "py/mperrno.h" #include "py/mphal.h" #include "lib/netutils/netutils.h" @@ -305,9 +306,19 @@ STATIC int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_ } STATIC int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) { - // TODO - *_errno = MP_EINVAL; - return -1; + if (request == MP_STREAM_POLL) { + int ret = 0; + if (arg & MP_STREAM_POLL_RD && getSn_RX_RSR(socket->u_param.fileno) != 0) { + ret |= MP_STREAM_POLL_RD; + } + if (arg & MP_STREAM_POLL_WR && getSn_TX_FSR(socket->u_param.fileno) != 0) { + ret |= MP_STREAM_POLL_WR; + } + return ret; + } else { + *_errno = MP_EINVAL; + return MP_STREAM_ERROR; + } } #if 0 From c53ca32561d19add3c2f6fae9b09d472fe52165e Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 19 Oct 2017 12:38:28 +1100 Subject: [PATCH 132/163] README: Add gcc and arm-none-eabi-newlib to list of required components. gcc is required for mpy-cross, and arm-none-eabi-newlib for ports using arm-none-eabi-gcc. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 00a0405f3..44d062e87 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Additional components: The subdirectories above may include READMEs with additional info. "make" is used to build the components, or "gmake" on BSD-based systems. -You will also need bash and Python (at least 2.7 or 3.3). +You will also need bash, gcc, and Python (at least 2.7 or 3.3). The Unix version ---------------- @@ -140,8 +140,8 @@ The STM32 version ----------------- The "stm32" port requires an ARM compiler, arm-none-eabi-gcc, and associated -bin-utils. For those using Arch Linux, you need arm-none-eabi-binutils and -arm-none-eabi-gcc packages. Otherwise, try here: +bin-utils. For those using Arch Linux, you need arm-none-eabi-binutils, +arm-none-eabi-gcc and arm-none-eabi-newlib packages. Otherwise, try here: https://launchpad.net/gcc-arm-embedded To build: From 9725a654bdfc4ec0b7d6bc6aa3f4365f0825c5f3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 19 Oct 2017 14:10:17 +1100 Subject: [PATCH 133/163] extmod/uos_dupterm: Swallow any errors from dupterm closing the stream. Without this the board will crash when deactivating a stream that doesn't have a close() method (eg UART) or that raises an exception within the method (eg user-defined function). --- extmod/uos_dupterm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c index d4326d326..f77cff577 100644 --- a/extmod/uos_dupterm.c +++ b/extmod/uos_dupterm.c @@ -43,7 +43,13 @@ void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) { if (exc != MP_OBJ_NULL) { mp_obj_print_exception(&mp_plat_print, exc); } - mp_stream_close(term); + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_stream_close(term); + nlr_pop(); + } else { + // Ignore any errors during stream closing + } } int mp_uos_dupterm_rx_chr(void) { @@ -61,8 +67,8 @@ int mp_uos_dupterm_rx_chr(void) { if (res == mp_const_none) { nlr_pop(); } else if (res == MP_OBJ_NEW_SMALL_INT(0)) { - mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL); nlr_pop(); + mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL); } else { mp_buffer_info_t bufinfo; mp_get_buffer_raise(MP_STATE_VM(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ); From 0eb333e3cf2319cfb1bf558b8f1c6e3e513fb9d8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 19 Oct 2017 14:15:32 +1100 Subject: [PATCH 134/163] stm32/mphalport: Improve efficiency of mp_hal_stdout_tx_strn_cooked. Also simplifies the code by removing the specialised (and inefficient) cooked functions from UART and USB_VCP. --- ports/stm32/mphalport.c | 20 +++++++++++++++----- ports/stm32/uart.c | 15 --------------- ports/stm32/uart.h | 1 - ports/stm32/usb.c | 14 -------------- ports/stm32/usb.h | 1 - 5 files changed, 15 insertions(+), 36 deletions(-) diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index ab3dc227a..3bea6e2d9 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -58,13 +58,23 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { } } +// Efficiently convert "\n" to "\r\n" void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { - // send stdout to UART and USB CDC VCP - if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { - uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len); + 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; + } } - if (usb_vcp_is_enabled()) { - usb_vcp_send_strn_cooked(str, len); + if (str > last) { + mp_hal_stdout_tx_strn(last, str - last); } } diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 2b2f782f9..0b46d4f04 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -452,26 +452,11 @@ STATIC size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_ return num_tx; } -STATIC void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) { - uint16_t ch = c; - int errcode; - uart_tx_data(uart_obj, &ch, 1, &errcode); -} - void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) { int errcode; uart_tx_data(uart_obj, str, len, &errcode); } -void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len) { - for (const char *top = str + len; str < top; str++) { - if (*str == '\n') { - uart_tx_char(uart_obj, '\r'); - } - uart_tx_char(uart_obj, *str); - } -} - // this IRQ handler is set up to handle RXNE interrupts only void uart_irq_handler(mp_uint_t uart_id) { // get the uart object diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index e96b25b5f..d176520a1 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -48,6 +48,5 @@ void uart_irq_handler(mp_uint_t uart_id); mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj); int uart_rx_char(pyb_uart_obj_t *uart_obj); void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); -void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len); #endif // MICROPY_INCLUDED_STMHAL_UART_H diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 8f89107ba..69f381d9b 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -177,20 +177,6 @@ void usb_vcp_send_strn(const char *str, int len) { #endif } -void usb_vcp_send_strn_cooked(const char *str, int len) { -#ifdef USE_DEVICE_MODE - if (usb_device.enabled) { - for (const char *top = str + len; str < top; str++) { - if (*str == '\n') { - usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)"\r\n", 2); - } else { - usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)str, 1); - } - } - } -#endif -} - /******************************************************************************/ // MicroPython bindings for USB diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index f60ea8033..41c461fb2 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -63,7 +63,6 @@ void pyb_usb_dev_deinit(void); bool usb_vcp_is_enabled(void); int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0 void usb_vcp_send_strn(const char* str, int len); -void usb_vcp_send_strn_cooked(const char *str, int len); void pyb_usb_host_init(void); void pyb_usb_host_process(void); From d6bf3658f44e22f06e0d293b8fe0542b57538b4f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 19 Oct 2017 14:16:42 +1100 Subject: [PATCH 135/163] stm32: Make uos.dupterm() conform to specs by using extmod version. The legacy function pyb.repl_uart() is still provided and retains its original behaviour (it only accepts a UART object). uos.dupterm() will now accept any object with write/readinto methods. At the moment there is just 1 dupterm slot. --- ports/stm32/modpyb.c | 26 ++++++++++++++++++++++++-- ports/stm32/moduos.c | 25 ++----------------------- ports/stm32/mpconfigport.h | 1 + ports/stm32/mphalport.c | 6 ++++++ 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 176fc8466..81cbdcc19 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -27,7 +27,7 @@ #include #include -#include "py/obj.h" +#include "py/runtime.h" #include "py/gc.h" #include "py/builtin.h" #include "py/mphal.h" @@ -104,6 +104,28 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros); MP_DECLARE_CONST_FUN_OBJ_KW(pyb_main_obj); // defined in main.c +// Get or set the UART object that the REPL is repeated on. +// This is a legacy function, use of uos.dupterm is preferred. +STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + if (MP_STATE_PORT(pyb_stdio_uart) == NULL) { + return mp_const_none; + } else { + return MP_STATE_PORT(pyb_stdio_uart); + } + } else { + if (args[0] == mp_const_none) { + MP_STATE_PORT(pyb_stdio_uart) = NULL; + } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) { + MP_STATE_PORT(pyb_stdio_uart) = args[0]; + } else { + mp_raise_ValueError("need a UART object"); + } + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart); + STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) }, @@ -126,7 +148,7 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_sleep_obj) }, { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&machine_deepsleep_obj) }, { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) }, - { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&mod_os_dupterm_obj) }, + { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) }, { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) }, { MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) }, diff --git a/ports/stm32/moduos.c b/ports/stm32/moduos.c index f661b3b5e..f6e1483d3 100644 --- a/ports/stm32/moduos.c +++ b/ports/stm32/moduos.c @@ -33,6 +33,7 @@ #include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" +#include "extmod/misc.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" #include "genhdr/mpversion.h" @@ -105,28 +106,6 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); #endif -// Get or set the UART object that the REPL is repeated on. -// TODO should accept any object with read/write methods. -STATIC mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - if (MP_STATE_PORT(pyb_stdio_uart) == NULL) { - return mp_const_none; - } else { - return MP_STATE_PORT(pyb_stdio_uart); - } - } else { - if (args[0] == mp_const_none) { - MP_STATE_PORT(pyb_stdio_uart) = NULL; - } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) { - MP_STATE_PORT(pyb_stdio_uart) = args[0]; - } else { - mp_raise_ValueError("need a UART object"); - } - return mp_const_none; - } -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm); - STATIC const mp_rom_map_elem_t os_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) }, @@ -154,7 +133,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = { #endif // these are MicroPython extensions - { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mod_os_dupterm_obj) }, + { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) }, { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) }, { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) }, diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 2d59a1df8..51d442561 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -126,6 +126,7 @@ #define MICROPY_PY_USELECT (1) #define MICROPY_PY_UTIMEQ (1) #define MICROPY_PY_UTIME_MP_HAL (1) +#define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index 3bea6e2d9..e9c4f28f7 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -3,6 +3,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" +#include "extmod/misc.h" #include "usb.h" #include "uart.h" @@ -38,6 +39,10 @@ int mp_hal_stdin_rx_chr(void) { } else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) { return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart)); } + int dupterm_c = mp_uos_dupterm_rx_chr(); + if (dupterm_c >= 0) { + return dupterm_c; + } MICROPY_EVENT_POLL_HOOK } } @@ -56,6 +61,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { if (usb_vcp_is_enabled()) { usb_vcp_send_strn(str, len); } + mp_uos_dupterm_tx_strn(str, len); } // Efficiently convert "\n" to "\r\n" From 93ce125abe41b5527eb92e483e64cc6d1a027ba0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 19 Oct 2017 18:57:26 +1100 Subject: [PATCH 136/163] py/argcheck: Remove #if guard around terse error message helper func. Not all compilers/analysers are smart enough to realise that this function is never called if MICROPY_ERROR_REPORTING is not TERSE, because the logic in the code uses if statements rather than #if to select whether to call this function or not (MSC in debug mode is an example of this, but there are others). So just unconditionally compile this helper function. The code-base anyway relies on the linker to remove unused functions. --- py/argcheck.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/py/argcheck.c b/py/argcheck.c index add6f8de8..d53bca73a 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -133,11 +133,9 @@ void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals); } -#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE || _MSC_VER NORETURN void mp_arg_error_terse_mismatch(void) { mp_raise_TypeError("argument num/types mismatch"); } -#endif #if MICROPY_CPYTHON_COMPAT NORETURN void mp_arg_error_unimpl_kw(void) { From f2baa9ec245a66c4cd7d990e39a4af3f6fab1cb7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 19 Oct 2017 12:40:41 +0300 Subject: [PATCH 137/163] py/objtype: Use CPython compatible method name for sizeof. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per https://docs.python.org/3/library/sys.html#sys.getsizeof: getsizeof() calls the object’s __sizeof__ method. Previously, "getsizeof" was used mostly to save on new qstr, as we don't really support calling this method on arbitrary objects (so it was used only for reporting). However, normalize it all now. --- py/objtype.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/objtype.c b/py/objtype.c index 9a639ef03..45b119f45 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -342,7 +342,7 @@ const uint16_t mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { [MP_UNARY_OP_INVERT] = MP_QSTR___invert__, #endif #if MICROPY_PY_SYS_GETSIZEOF - [MP_UNARY_OP_SIZEOF] = MP_QSTR_getsizeof, + [MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__, #endif }; From 9956fd0710c3866b9df37857c9aa62b8bb681403 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Oct 2017 11:06:32 +0300 Subject: [PATCH 138/163] py/objtype: Fit qstrs for special methods in byte type. Update makeqstrdata.py to sort strings starting with "__" to the beginning of qstr list, so they get low qstr id's, guaranteedly fitting in 8 bits. Then use this property to further compact op_id => qstr mapping arrays. --- py/makeqstrdata.py | 10 +++++++++- py/objtype.c | 10 +++++++--- py/runtime.h | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index 7249769f4..38fde1a9c 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -108,7 +108,15 @@ def parse_input_headers(infiles): continue # add the qstr to the list, with order number to retain original order in file - qstrs[ident] = (len(qstrs), ident, qstr) + order = len(qstrs) + # but put special method names like __add__ at the top of list, so + # that their id's fit into a byte + if ident == "": + # Sort empty qstr above all still + order = -200000 + elif ident.startswith("__"): + order -= 100000 + qstrs[ident] = (order, ident, qstr) if not qcfgs: sys.stderr.write("ERROR: Empty preprocessor output - check for errors above\n") diff --git a/py/objtype.c b/py/objtype.c index 45b119f45..e75407683 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -332,7 +332,9 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size return MP_OBJ_FROM_PTR(o); } -const uint16_t mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { +// Qstrs for special methods are guaranteed to have a small value, so we use byte +// type to represent them. +const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { [MP_UNARY_OP_BOOL] = MP_QSTR___bool__, [MP_UNARY_OP_LEN] = MP_QSTR___len__, [MP_UNARY_OP_HASH] = MP_QSTR___hash__, @@ -406,9 +408,11 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } // Binary-op enum values not listed here will have the default value of 0 in the -// table, corresponding to MP_QSTR_, and are therefore unsupported (a lookup will +// table, corresponding to MP_QSTR_NULL, and are therefore unsupported (a lookup will // fail). They can be added at the expense of code size for the qstr. -const uint16_t mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { +// Qstrs for special methods are guaranteed to have a small value, so we use byte +// type to represent them. +const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { [MP_BINARY_OP_LESS] = MP_QSTR___lt__, [MP_BINARY_OP_MORE] = MP_QSTR___gt__, [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__, diff --git a/py/runtime.h b/py/runtime.h index d410b5614..9c1921cb5 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -57,8 +57,8 @@ typedef struct _mp_arg_t { } mp_arg_t; // Tables mapping operator enums to qstrs, defined in objtype.c -extern const uint16_t mp_unary_op_method_name[]; -extern const uint16_t mp_binary_op_method_name[]; +extern const byte mp_unary_op_method_name[]; +extern const byte mp_binary_op_method_name[]; void mp_init(void); void mp_deinit(void); From cfff12612f92d6ae460e9748394a0cedf3b3d31d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 23 Oct 2017 12:09:37 +0300 Subject: [PATCH 139/163] unix: Rename modsocket.c to modusocket.c. Unix naming is historical, before current conventions were established. All other ports however have it as "modusocket.c", so rename for consistency and to avoid confusion. --- ports/unix/Makefile | 2 +- ports/unix/{modsocket.c => modusocket.c} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename ports/unix/{modsocket.c => modusocket.c} (100%) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 1a1ea01f7..b96391f69 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -97,7 +97,7 @@ SRC_MOD += modtermios.c endif ifeq ($(MICROPY_PY_SOCKET),1) CFLAGS_MOD += -DMICROPY_PY_SOCKET=1 -SRC_MOD += modsocket.c +SRC_MOD += modusocket.c endif ifeq ($(MICROPY_PY_THREAD),1) CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0 diff --git a/ports/unix/modsocket.c b/ports/unix/modusocket.c similarity index 100% rename from ports/unix/modsocket.c rename to ports/unix/modusocket.c From f4059dcc0c5251256a53fcb49f56fdda6e879341 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 24 Oct 2017 22:39:36 +1100 Subject: [PATCH 140/163] all: Use NULL instead of "" when calling mp_raise exception helpers. This is the established way of doing it and reduces code size by a little bit. --- extmod/modlwip.c | 2 +- extmod/modussl_axtls.c | 2 +- extmod/modutimeq.c | 2 +- extmod/modwebrepl.c | 2 +- ports/esp8266/machine_hspi.c | 2 +- ports/esp8266/machine_wdt.c | 2 +- ports/unix/modjni.c | 2 +- py/objarray.c | 2 +- py/objlist.c | 4 ++-- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/extmod/modlwip.c b/extmod/modlwip.c index f7e776af9..bbb01b5d7 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1032,7 +1032,7 @@ STATIC mp_obj_t lwip_socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) { break; } case MOD_NETWORK_SOCK_DGRAM: - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); break; } diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 88a89a23d..719a65cd1 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -152,7 +152,7 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) { // Currently supports only blocking mode (void)self_in; if (!mp_obj_is_true(flag_in)) { - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); } return mp_const_none; } diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index 94cbd20d2..620e7484b 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -146,7 +146,7 @@ STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) { } mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref); if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) { - mp_raise_TypeError(""); + mp_raise_TypeError(NULL); } struct qentry *item = &heap->items[0]; diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index 4ff282aac..3aba5c0f1 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -308,7 +308,7 @@ STATIC mp_obj_t webrepl_set_password(mp_obj_t passwd_in) { size_t len; const char *passwd = mp_obj_str_get_data(passwd_in, &len); if (len > sizeof(webrepl_passwd) - 1) { - mp_raise_ValueError(""); + mp_raise_ValueError(NULL); } strcpy(webrepl_passwd, passwd); return mp_const_none; diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index eaabbab7e..9fd0f4868 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -149,7 +149,7 @@ mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t // args[0] holds the id of the peripheral if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) { // FlashROM is on SPI0, so far we don't support its usage - mp_raise_ValueError(""); + mp_raise_ValueError(NULL); } machine_hspi_obj_t *self = m_new_obj(machine_hspi_obj_t); diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index 04b42782e..4432297fa 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -51,7 +51,7 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args case 0: return &wdt_default; default: - mp_raise_ValueError(""); + mp_raise_ValueError(NULL); } } diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index 15b6d9cd7..f29c095cf 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -266,7 +266,7 @@ STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) return mp_const_none; } } - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); } if (!JJ(IsInstanceOf, self->obj, List_class)) { diff --git a/py/objarray.c b/py/objarray.c index 8a3e7faad..7003ec9e7 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -286,7 +286,7 @@ STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs // Otherwise, can only look for a scalar numeric value in an array if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) { - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); } return mp_const_false; diff --git a/py/objlist.c b/py/objlist.c index bc22d9fc3..1a18f937d 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -158,7 +158,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); mp_bound_slice_t slice; if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) { - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); } mp_int_t len_adj = slice.start - slice.stop; @@ -198,7 +198,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_get_array(value, &value_len, &value_items); mp_bound_slice_t slice_out; if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) { - mp_raise_NotImplementedError(""); + mp_raise_NotImplementedError(NULL); } mp_int_t len_adj = value_len - (slice_out.stop - slice_out.start); //printf("Len adj: %d\n", len_adj); From 9a7e3469b28b02b05b85789acc6c0a78b9804659 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 24 Oct 2017 23:11:42 +0300 Subject: [PATCH 141/163] unix/modusocket: Remove #if MICROPY_SOCKET_EXTRA code blocks. These defined couple of functions added during initial experimentation, which aren't part of MicroPython API and no longer used or needed. --- ports/unix/modusocket.c | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 7e82554c7..cfb6a9f5e 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -60,8 +60,6 @@ should be add to separate modules (C or Python level). */ -#define MICROPY_SOCKET_EXTRA (0) - // This type must "inherit" from mp_obj_fdfile_t, i.e. matching subset of // fields should have the same layout. typedef struct _mp_obj_socket_t { @@ -382,26 +380,6 @@ const mp_obj_type_t mp_type_socket = { .locals_dict = (mp_obj_dict_t*)&usocket_locals_dict, }; -#if MICROPY_SOCKET_EXTRA -STATIC mp_obj_t mod_socket_htons(mp_obj_t arg) { - return MP_OBJ_NEW_SMALL_INT(htons(MP_OBJ_SMALL_INT_VALUE(arg))); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_htons_obj, mod_socket_htons); - - -STATIC mp_obj_t mod_socket_gethostbyname(mp_obj_t arg) { - const char *s = mp_obj_str_get_str(arg); - struct hostent *h = gethostbyname(s); - if (h == NULL) { - // CPython: socket.herror - mp_raise_OSError(h_errno); - } - assert(h->h_length == 4); - return mp_obj_new_int(*(int*)*h->h_addr_list); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_gethostbyname_obj, mod_socket_gethostbyname); -#endif // MICROPY_SOCKET_EXTRA - #define BINADDR_MAX_LEN sizeof(struct in6_addr) STATIC mp_obj_t mod_socket_inet_pton(mp_obj_t family_in, mp_obj_t addr_in) { int family = mp_obj_get_int(family_in); @@ -549,10 +527,6 @@ STATIC const mp_rom_map_elem_t mp_module_socket_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_inet_pton), MP_ROM_PTR(&mod_socket_inet_pton_obj) }, { MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) }, { MP_ROM_QSTR(MP_QSTR_sockaddr), MP_ROM_PTR(&mod_socket_sockaddr_obj) }, -#if MICROPY_SOCKET_EXTRA - { MP_ROM_QSTR(MP_QSTR_htons), MP_ROM_PTR(&mod_socket_htons_obj) }, - { MP_ROM_QSTR(MP_QSTR_gethostbyname), MP_ROM_PTR(&mod_socket_gethostbyname_obj) }, -#endif #define C(name) { MP_ROM_QSTR(MP_QSTR_ ## name), MP_ROM_INT(name) } C(AF_UNIX), From 328c1e78be68a386fe6fb5940027aad887c823a4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 26 Oct 2017 00:28:45 +0300 Subject: [PATCH 142/163] docs/uselect: Document one-shot polling mode. --- docs/library/uselect.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/library/uselect.rst b/docs/library/uselect.rst index e330207db..beffce69a 100644 --- a/docs/library/uselect.rst +++ b/docs/library/uselect.rst @@ -66,12 +66,18 @@ Methods Tuples returned may contain more than 2 elements as described above. -.. method:: poll.ipoll([timeout]) +.. method:: poll.ipoll(timeout=-1, flags=0) Like :meth:`poll.poll`, but instead returns an iterator which yields - callee-owned tuples. This function provides efficient, allocation-free + `callee-owned tuples`. This function provides efficient, allocation-free way to poll on streams. + If *flags* is 1, one-shot behavior for events is employed: streams for + which events happened, event mask will be automatically reset (equivalent + to ``poll.modify(obj, 0)``), so new events for such a stream won't be + processed until new mask is set with `poll.modify()`. This behavior is + useful for asynchronous I/O schedulers. + .. admonition:: Difference to CPython :class: attention From f36975b6792f6b702dc9184adcdb6e0d89ce23b4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 26 Oct 2017 12:29:24 +1100 Subject: [PATCH 143/163] tests/net_inet: Update tls test to work with CPython and incl new site. CPython only supports the server_hostname keyword arg via the SSLContext object, so use that instead of the top-level ssl.wrap_socket. This allows the test to run on CPython the same as uPy. Also add the "Host:" header to correctly make a GET request (for URLs that are hosted on other servers). This is not strictly needed to test the SSL connection but helps to debug things when printing the response. --- tests/net_inet/test_tls_sites.py | 5 ++++- tests/net_inet/test_tls_sites.py.exp | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/net_inet/test_tls_sites.py b/tests/net_inet/test_tls_sites.py index 67345fd0b..bf8071d08 100644 --- a/tests/net_inet/test_tls_sites.py +++ b/tests/net_inet/test_tls_sites.py @@ -6,6 +6,8 @@ import ussl as ssl except: import ssl + # CPython only supports server_hostname with SSLContext + ssl = ssl.SSLContext() def test_one(site, opts): @@ -22,7 +24,7 @@ def test_one(site, opts): else: s = ssl.wrap_socket(s) - s.write(b"GET / HTTP/1.0\r\n\r\n") + s.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, 'latin')) resp = s.read(4096) # print(resp) @@ -34,6 +36,7 @@ def test_one(site, opts): "google.com", "www.google.com", "api.telegram.org", + {"host": "api.pushbullet.com", "sni": True}, # "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", {"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True}, ] diff --git a/tests/net_inet/test_tls_sites.py.exp b/tests/net_inet/test_tls_sites.py.exp index 12732d1fa..2f3c113d2 100644 --- a/tests/net_inet/test_tls_sites.py.exp +++ b/tests/net_inet/test_tls_sites.py.exp @@ -1,4 +1,5 @@ google.com ok www.google.com ok api.telegram.org ok +api.pushbullet.com ok w9rybpfril.execute-api.ap-southeast-2.amazonaws.com ok From d1cd533134cc115dfe15e6c22d60bb7fd7589d88 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 26 Oct 2017 14:00:16 +0300 Subject: [PATCH 144/163] docs/usocket: Elaborate descriptions. Use the "usocket" module name everywhere. Use "MicroPython port" terminology. Suggest to avoid using IPPROTO_* constants in socket() call. --- docs/library/usocket.rst | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/library/usocket.rst b/docs/library/usocket.rst index dfdcd68bc..a3e987a07 100644 --- a/docs/library/usocket.rst +++ b/docs/library/usocket.rst @@ -68,7 +68,16 @@ Functions .. function:: socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP) - Create a new socket using the given address family, socket type and protocol number. + Create a new socket using the given address family, socket type and + protocol number. Note that specifying *proto* in most cases is not + required (and not recommended, as some MicroPython ports may omit + ``IPPROTO_*`` constants). Instead, *type* argument will select needed + protocol automatically:: + + # Create STREAM TCP socket + socket(AF_INET, SOCK_STREAM) + # Create DGRAM UDP socket + socket(AF_INET, SOCK_DGRAM) .. function:: getaddrinfo(host, port) @@ -80,8 +89,8 @@ Functions The following example shows how to connect to a given url:: - s = socket.socket() - s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1]) + s = usocket.socket() + s.connect(usocket.getaddrinfo('www.micropython.org', 80)[0][-1]) .. admonition:: Difference to CPython :class: attention @@ -102,7 +111,7 @@ Constants .. data:: AF_INET AF_INET6 - Address family types. Availability depends on a particular board. + Address family types. Availability depends on a particular `MicroPython port`. .. data:: SOCK_STREAM SOCK_DGRAM @@ -112,7 +121,11 @@ Constants .. data:: IPPROTO_UDP IPPROTO_TCP - IP protocol numbers. + IP protocol numbers. Availability depends on a particular `MicroPython port`. + Note that you don't need to specify these in a call to `usocket.socket()`, + because `SOCK_STREAM` socket type automatically selects `IPPROTO_TCP`, and + `SOCK_DGRAM` - `IPPROTO_UDP`. Thus, the only real use of these constants + is as an argument to `setsockopt()`. .. data:: usocket.SOL_* @@ -281,7 +294,7 @@ Methods Return value: number of bytes written. -.. exception:: socket.error +.. exception:: usocket.error MicroPython does NOT have this exception. From a33fca99a1cc9d2026ee56e81f372c1e695379f5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Oct 2017 00:27:27 +0300 Subject: [PATCH 145/163] docs/usocket: Document inet_ntop(), inet_pton(). --- docs/library/usocket.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/library/usocket.rst b/docs/library/usocket.rst index a3e987a07..53936505b 100644 --- a/docs/library/usocket.rst +++ b/docs/library/usocket.rst @@ -105,6 +105,22 @@ Functions from an exception object). The use of negative values is a provisional detail which may change in the future. +.. function:: inet_ntop(af, bin_addr) + + Convert a binary network address *bin_addr* of the given address family *af* + to a textual representation:: + + >>> usocket.inet_ntop(usocket.AF_INET, b"\x7f\0\0\1") + '127.0.0.1' + +.. function:: inet_pton(af, txt_addr) + + Convert a textual network address *txt_addr* of the given address family *af* + to a binary representation:: + + >>> usocket.inet_pton(usocket.AF_INET, "1.2.3.4") + b'\x01\x02\x03\x04' + Constants --------- From c64eb4f8ce20b025bb5762d9ce6e093d15a19dce Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Oct 2017 18:01:25 +1100 Subject: [PATCH 146/163] extmod/vfs: Replace VLA in proxy func with small, static sized array. VLAs can be expensive on stack usage due to stack alignment requirements, and also the fact that extra local variables are needed to track the dynamic size of the stack. So using fixed-size arrays when possible can help to reduce code size and stack usage. In this particular case, the maximum value of n_args in the VLA is 2 and so it's more efficient to just allocate this array with a fixed size. This reduces code size by around 30 bytes on Thumb2 and Xtensa archs. It also reduces total stack usage of the function: on Thumb2 the usage with VLA is between 40 and 48 bytes, which is reduced to 32; on Xtensa, VLA usage is between 64 and 80 bytes, reduced to 32; on x86-64 it's at least 88 bytes reduced to 80. --- extmod/vfs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index a1cd8d5f6..44ad8ffad 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -38,6 +38,10 @@ #include "extmod/vfs_fat.h" #endif +// For mp_vfs_proxy_call, the maximum number of additional args that can be passed. +// A fixed maximum size is used to avoid the need for a costly variable array. +#define PROXY_MAX_ARGS (2) + // path is the path to lookup and *path_out holds the path within the VFS // object (starts with / if an absolute path). // Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and @@ -97,6 +101,7 @@ STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { } STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { + assert(n_args <= PROXY_MAX_ARGS); if (vfs == MP_VFS_NONE) { // mount point not found mp_raise_OSError(MP_ENODEV); @@ -105,7 +110,7 @@ STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_ // can't do operation on root dir mp_raise_OSError(MP_EPERM); } - mp_obj_t meth[n_args + 2]; + mp_obj_t meth[2 + PROXY_MAX_ARGS]; mp_load_method(vfs->obj, meth_name, meth); if (args != NULL) { memcpy(meth + 2, args, n_args * sizeof(*args)); From b9923262db4600b3618aa143bb4bcc93bf299814 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Wed, 25 Oct 2017 14:18:11 +0200 Subject: [PATCH 147/163] docs/library/network: Add dhcp_hostname parameter I have not actually tested this, going by information available in https://forum.micropython.org/viewtopic.php?t=2584 --- docs/library/network.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/library/network.rst b/docs/library/network.rst index 3b0aa535e..99a7c242c 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -422,16 +422,17 @@ parameter should be `id`. Following are commonly supported parameters (availability of a specific parameter depends on network technology type, driver, and `MicroPython port`). - ========= =========== - Parameter Description - ========= =========== - mac MAC address (bytes) - essid WiFi access point name (string) - channel WiFi channel (integer) - hidden Whether ESSID is hidden (boolean) - authmode Authentication mode supported (enumeration, see module constants) - password Access password (string) - ========= =========== + ============= =========== + Parameter Description + ============= =========== + mac MAC address (bytes) + essid WiFi access point name (string) + channel WiFi channel (integer) + hidden Whether ESSID is hidden (boolean) + authmode Authentication mode supported (enumeration, see module constants) + password Access password (string) + dhcp_hostname The DHCP hostname to use + ============= =========== From 9b9dbc58155209e07ab7b9d63d63e5e24db5950c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Oct 2017 13:55:02 +0300 Subject: [PATCH 148/163] py/objtype: Define all special methods if requested. If MICROPY_PY_ALL_SPECIAL_METHODS is defined, actually define all special methods (still subject to gating by e.g. MICROPY_PY_REVERSE_SPECIAL_METHODS). This adds quite a number of qstr's, so should be used sparingly. --- py/objtype.c | 32 ++++++++++++++++++++++++++++++++ tests/unix/extra_coverage.py.exp | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index e75407683..d1c1dcba4 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -342,6 +342,7 @@ const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { [MP_UNARY_OP_POSITIVE] = MP_QSTR___pos__, [MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__, [MP_UNARY_OP_INVERT] = MP_QSTR___invert__, + [MP_UNARY_OP_ABS] = MP_QSTR___abs__, #endif #if MICROPY_PY_SYS_GETSIZEOF [MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__, @@ -422,8 +423,20 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { [MP_BINARY_OP_IN] = MP_QSTR___contains__, #if MICROPY_PY_ALL_SPECIAL_METHODS + // All inplace methods are optional, and normal methods will be used + // as a fallback. [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__, [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__, + [MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__, + [MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__, + [MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__, + [MP_BINARY_OP_INPLACE_MODULO] = MP_QSTR___imod__, + [MP_BINARY_OP_INPLACE_POWER] = MP_QSTR___ipow__, + [MP_BINARY_OP_INPLACE_OR] = MP_QSTR___ior__, + [MP_BINARY_OP_INPLACE_XOR] = MP_QSTR___ixor__, + [MP_BINARY_OP_INPLACE_AND] = MP_QSTR___iand__, + [MP_BINARY_OP_INPLACE_LSHIFT] = MP_QSTR___ilshift__, + [MP_BINARY_OP_INPLACE_RSHIFT] = MP_QSTR___irshift__, #endif [MP_BINARY_OP_ADD] = MP_QSTR___add__, @@ -432,12 +445,31 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { [MP_BINARY_OP_MULTIPLY] = MP_QSTR___mul__, [MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__, [MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__, + [MP_BINARY_OP_MODULO] = MP_QSTR___mod__, + [MP_BINARY_OP_DIVMOD] = MP_QSTR___divmod__, + [MP_BINARY_OP_POWER] = MP_QSTR___pow__, + [MP_BINARY_OP_OR] = MP_QSTR___or__, + [MP_BINARY_OP_XOR] = MP_QSTR___xor__, + [MP_BINARY_OP_AND] = MP_QSTR___and__, + [MP_BINARY_OP_LSHIFT] = MP_QSTR___lshift__, + [MP_BINARY_OP_RSHIFT] = MP_QSTR___rshift__, #endif #if MICROPY_PY_REVERSE_SPECIAL_METHODS [MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__, [MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__, + #if MICROPY_PY_ALL_SPECIAL_METHODS [MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__, + [MP_BINARY_OP_REVERSE_FLOOR_DIVIDE] = MP_QSTR___rfloordiv__, + [MP_BINARY_OP_REVERSE_TRUE_DIVIDE] = MP_QSTR___rtruediv__, + [MP_BINARY_OP_REVERSE_MODULO] = MP_QSTR___rmod__, + [MP_BINARY_OP_REVERSE_POWER] = MP_QSTR___rpow__, + [MP_BINARY_OP_REVERSE_OR] = MP_QSTR___ror__, + [MP_BINARY_OP_REVERSE_XOR] = MP_QSTR___rxor__, + [MP_BINARY_OP_REVERSE_AND] = MP_QSTR___rand__, + [MP_BINARY_OP_REVERSE_LSHIFT] = MP_QSTR___rlshift__, + [MP_BINARY_OP_REVERSE_RSHIFT] = MP_QSTR___rrshift__, + #endif #endif }; diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4c4f66663..1db46ab8f 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -39,8 +39,8 @@ ementation 0 0 # runtime utils -TypeError: unsupported type for : 'str' -TypeError: unsupported types for : 'str', 'str' +TypeError: unsupported type for __abs__: 'str' +TypeError: unsupported types for __divmod__: 'str', 'str' Warning: test # format float ? From 0e80f345f88c5db7c2353a5a9d29ed08b0af42f4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Oct 2017 22:29:15 +0300 Subject: [PATCH 149/163] py/objtype: Introduce MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS. This allows to configure support for inplace special methods separately, similar to "normal" and reverse special methods. This is useful, because inplace methods are "the most optional" ones, for example, if inplace methods aren't defined, the operation will be executed using normal methods instead. As a caveat, __iadd__ and __isub__ are implemented even if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS isn't defined. This is similar to the state of affairs before binary operations refactor, and allows to run existing tests even if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS isn't defined. --- py/mpconfig.h | 20 ++++++++++++++------ py/objtype.c | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 44de3beeb..1694a1360 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -765,16 +765,24 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_TIMEOUTERROR (0) #endif -// Whether to support complete set of special methods -// for user classes, or only the most used ones. "Reverse" -// methods are controlled by MICROPY_PY_REVERSE_SPECIAL_METHODS -// below. +// Whether to support complete set of special methods for user +// classes, or only the most used ones. "Inplace" methods are +// controlled by MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS below. +// "Reverse" methods are controlled by +// MICROPY_PY_REVERSE_SPECIAL_METHODS below. #ifndef MICROPY_PY_ALL_SPECIAL_METHODS #define MICROPY_PY_ALL_SPECIAL_METHODS (0) #endif -// Whether to support reverse arithmetic operarions methods -// (__radd__, etc.) +// Whether to support all inplace arithmetic operarion methods +// (__imul__, etc.) +#ifndef MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS +#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (0) +#endif + +// Whether to support reverse arithmetic operarion methods +// (__radd__, etc.). Additionally gated by +// MICROPY_PY_ALL_SPECIAL_METHODS. #ifndef MICROPY_PY_REVERSE_SPECIAL_METHODS #define MICROPY_PY_REVERSE_SPECIAL_METHODS (0) #endif diff --git a/py/objtype.c b/py/objtype.c index d1c1dcba4..6e2ab6c9a 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -422,11 +422,11 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { // MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result [MP_BINARY_OP_IN] = MP_QSTR___contains__, - #if MICROPY_PY_ALL_SPECIAL_METHODS // All inplace methods are optional, and normal methods will be used // as a fallback. [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__, [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__, + #if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS [MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__, [MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__, [MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__, From 24c8eda744e6c13866d70caec1b5c316caee64ac Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 28 Oct 2017 13:05:57 +0300 Subject: [PATCH 150/163] unix: Enable MICROPY_PY_REVERSE_SPECIAL_METHODS. With inplace methods now disabled by default, it makes sense to enable reverse methods, as they allow for more useful features, e.g. allow for datetime module to implement both 2 * HOUR and HOUR * 2 (where HOUR is e.g. timedelta object). --- ports/unix/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 50b979279..db382e0a7 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -85,6 +85,7 @@ #define MICROPY_PY_BUILTINS_POW3 (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) #define MICROPY_PY_SYS_EXIT (1) From 8f9af63c20fbdb49a0e9a29248b8c7abe6f01b61 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 29 Oct 2017 19:52:56 +0200 Subject: [PATCH 151/163] lib/axtls: Update, support for SSL_EAGAIN return code. A step towards implementing non-blocking stream support for SSL. --- lib/axtls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axtls b/lib/axtls index 9b3092eb3..dac9176ca 160000 --- a/lib/axtls +++ b/lib/axtls @@ -1 +1 @@ -Subproject commit 9b3092eb3b4b230a63c0c389bfbd3c55682c620f +Subproject commit dac9176cac58cc5e49669a9a4d404a6f6dd7cc10 From 8f04b740736ba8312353e9bd462f4a27cdac5868 Mon Sep 17 00:00:00 2001 From: Eric Poulsen Date: Sun, 29 Oct 2017 17:10:00 -0700 Subject: [PATCH 152/163] esp32/modnetwork.c: Fix for setting DNS with network.WLAN.ifconfig(). When configuring a static set of values with ifconfig() the DNS was not being set. This patch fixes that, and additionally uses the tcpip_adapter API to ensure it is thread safe. Further discussion is here: https://github.com/micropython/micropython-esp32/issues/210/ --- ports/esp32/modnetwork.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 59af7aea0..2eb20d79f 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -328,16 +328,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected); STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); tcpip_adapter_ip_info_t info; - ip_addr_t dns_addr; + tcpip_adapter_dns_info_t dns_info; tcpip_adapter_get_ip_info(self->if_id, &info); + tcpip_adapter_get_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info); if (n_args == 1) { // get - dns_addr = dns_getserver(0); mp_obj_t tuple[4] = { netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG), - netutils_format_ipv4_addr((uint8_t*)&dns_addr, NETUTILS_BIG), + netutils_format_ipv4_addr((uint8_t*)&dns_info.ip, NETUTILS_BIG), }; return mp_obj_new_tuple(4, tuple); } else { @@ -356,16 +356,18 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { netutils_parse_ipv4_addr(items[1], (void*)&info.netmask, NETUTILS_BIG); } netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG); - netutils_parse_ipv4_addr(items[3], (void*)&dns_addr, NETUTILS_BIG); + netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG); // To set a static IP we have to disable DHCP first if (self->if_id == WIFI_IF_STA) { esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA); if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e); ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info)); + ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_STA, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); } else if (self->if_id == WIFI_IF_AP) { esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP); if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e); ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_AP, &info)); + ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); ESP_EXCEPTIONS(tcpip_adapter_dhcps_start(WIFI_IF_AP)); } return mp_const_none; From 05a2bb888f9cd3dc3e005b8eea4258e20a39cba2 Mon Sep 17 00:00:00 2001 From: Yuval Langer Date: Sun, 29 Oct 2017 22:27:06 +0200 Subject: [PATCH 153/163] docs/reference/isr_rules: Minor typo correction. --- docs/reference/isr_rules.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/isr_rules.rst b/docs/reference/isr_rules.rst index 5009f30f7..2db261c09 100644 --- a/docs/reference/isr_rules.rst +++ b/docs/reference/isr_rules.rst @@ -80,7 +80,7 @@ example causes two LED's to flash at different rates. self.led.toggle() red = Foo(pyb.Timer(4, freq=1), pyb.LED(1)) - greeen = Foo(pyb.Timer(2, freq=0.8), pyb.LED(2)) + green = Foo(pyb.Timer(2, freq=0.8), pyb.LED(2)) In this example the ``red`` instance associates timer 4 with LED 1: when a timer 4 interrupt occurs ``red.cb()`` is called causing LED 1 to change state. The ``green`` instance operates similarly: a timer 2 interrupt From 74ec52d85758ad9da7a3abb24257511b22d74964 Mon Sep 17 00:00:00 2001 From: Eric Poulsen Date: Thu, 26 Oct 2017 21:17:35 -0700 Subject: [PATCH 154/163] extmod/modussl: Add finaliser support for ussl objects. Per the comment found here https://github.com/micropython/micropython-esp32/issues/209#issuecomment-339855157, this patch adds finaliser code to prevent memory leaks from ussl objects, which is especially useful when memory for a ussl context is allocated outside the uPy heap. This patch is in-line with the finaliser code found in many modsocket implementations for various ports. This feature is configured via MICROPY_PY_USSL_FINALISER and is disabled by default because there may be issues using it when the ussl state *is* allocated on the uPy heap, rather than externally. --- extmod/modussl_axtls.c | 7 +++++++ extmod/modussl_mbedtls.c | 7 +++++++ py/mpconfig.h | 2 ++ 3 files changed, 16 insertions(+) diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 719a65cd1..3ad65ebf3 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -51,7 +51,11 @@ struct ssl_args { STATIC const mp_obj_type_t ussl_socket_type; STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { +#if MICROPY_PY_USSL_FINALISER + mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t); +#else mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t); +#endif o->base.type = &ussl_socket_type; o->buf = NULL; o->bytes_left = 0; @@ -178,6 +182,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) }, { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) }, +#if MICROPY_PY_USSL_FINALISER + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) }, +#endif }; STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table); diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index d7316cb4a..59dceb6cf 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -111,7 +111,11 @@ int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) { STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { +#if MICROPY_PY_USSL_FINALISER + mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t); +#else mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t); +#endif o->base.type = &ussl_socket_type; int ret; @@ -272,6 +276,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) }, { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) }, +#if MICROPY_PY_USSL_FINALISER + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) }, +#endif { MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) }, }; diff --git a/py/mpconfig.h b/py/mpconfig.h index 1694a1360..6a32ea2a6 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1109,6 +1109,8 @@ typedef double mp_float_t; #ifndef MICROPY_PY_USSL #define MICROPY_PY_USSL (0) +// Whether to add finaliser code to ussl objects +#define MICROPY_PY_USSL_FINALISER (0) #endif #ifndef MICROPY_PY_WEBSOCKET From 10b76a9620b7407ffed8366d71f5463496f98838 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Oct 2017 15:41:37 +1100 Subject: [PATCH 155/163] extmod/modussl_mbedtls: Allow to compile with unix coverage build. Fixes a few C warnings. No functional changes. --- extmod/modussl_mbedtls.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 59dceb6cf..69a64d2f7 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -65,23 +65,30 @@ struct ssl_args { STATIC const mp_obj_type_t ussl_socket_type; -void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) { +#ifdef MBEDTLS_DEBUG_C +STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) { + (void)ctx; + (void)level; printf("DBG:%s:%04d: %s\n", file, line, str); } +#endif // TODO: FIXME! -int null_entropy_func(void *data, unsigned char *output, size_t len) { +STATIC int null_entropy_func(void *data, unsigned char *output, size_t len) { + (void)data; + (void)output; + (void)len; // enjoy random bytes return 0; } -int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { +STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { mp_obj_t sock = *(mp_obj_t*)ctx; const mp_stream_p_t *sock_stream = mp_get_stream_raise(sock, MP_STREAM_OP_WRITE); int err; - int out_sz = sock_stream->write(sock, buf, len, &err); + mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err); if (out_sz == MP_STREAM_ERROR) { if (mp_is_nonblocking_error(err)) { return MBEDTLS_ERR_SSL_WANT_WRITE; @@ -92,13 +99,13 @@ int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { } } -int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) { +STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) { mp_obj_t sock = *(mp_obj_t*)ctx; const mp_stream_p_t *sock_stream = mp_get_stream_raise(sock, MP_STREAM_OP_READ); int err; - int out_sz = sock_stream->read(sock, buf, len, &err); + mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err); if (out_sz == MP_STREAM_ERROR) { if (mp_is_nonblocking_error(err)) { return MBEDTLS_ERR_SSL_WANT_READ; From 3233f9d6b7f44dcc6562eca5a478fb1a7ece9580 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Oct 2017 16:19:33 +1100 Subject: [PATCH 156/163] esp32/help: Update to build with latest upstream MicroPython core. --- ports/esp32/help.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/help.c b/ports/esp32/help.c index c18ca7282..95d115c56 100644 --- a/ports/esp32/help.c +++ b/ports/esp32/help.c @@ -28,7 +28,7 @@ #include "py/builtin.h" -const char *esp32_help_text = +const char esp32_help_text[] = "Welcome to MicroPython on the ESP32!\n" "\n" "For generic online docs please visit http://docs.micropython.org/\n" From a6566fc2e15e8efd856b39c7ee2132fcdad3450c Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Oct 2017 16:19:52 +1100 Subject: [PATCH 157/163] esp32/mpconfigport.h: Enable ussl finaliser. --- ports/esp32/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 3c4b77d3d..1eb4c408d 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -138,6 +138,7 @@ #define MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly #define MICROPY_PY_USSL (1) #define MICROPY_SSL_MBEDTLS (1) +#define MICROPY_PY_USSL_FINALISER (1) #define MICROPY_PY_WEBSOCKET (0) #define MICROPY_PY_FRAMEBUF (1) From 54c6ebcfd72cae2b3000987f99651e9c8015bf18 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 31 Oct 2017 15:54:15 +1100 Subject: [PATCH 158/163] extmod/modussl_mbedtls: Clean up mbedtls state when error during setup. Without this patch, if the SSL handshake fails (eg the connection was lost) then the mbedtls state (memory) will never be freed. --- extmod/modussl_mbedtls.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 69a64d2f7..197a0651c 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -141,8 +141,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { const byte seed[] = "upy"; ret = mbedtls_ctr_drbg_seed(&o->ctr_drbg, null_entropy_func/*mbedtls_entropy_func*/, &o->entropy, seed, sizeof(seed)); if (ret != 0) { - printf("ret=%d\n", ret); - assert(0); + goto cleanup; } ret = mbedtls_ssl_config_defaults(&o->conf, @@ -150,7 +149,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT); if (ret != 0) { - assert(0); + goto cleanup; } mbedtls_ssl_conf_authmode(&o->conf, MBEDTLS_SSL_VERIFY_NONE); @@ -161,14 +160,14 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { ret = mbedtls_ssl_setup(&o->ssl, &o->conf); if (ret != 0) { - assert(0); + goto cleanup; } if (args->server_hostname.u_obj != mp_const_none) { const char *sni = mp_obj_str_get_str(args->server_hostname.u_obj); ret = mbedtls_ssl_set_hostname(&o->ssl, sni); if (ret != 0) { - assert(0); + goto cleanup; } } @@ -194,13 +193,27 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { - //assert(0); printf("mbedtls_ssl_handshake error: -%x\n", -ret); - mp_raise_OSError(MP_EIO); + goto cleanup; } } return o; + +cleanup: + mbedtls_pk_free(&o->pkey); + mbedtls_x509_crt_free(&o->cert); + mbedtls_x509_crt_free(&o->cacert); + mbedtls_ssl_free(&o->ssl); + mbedtls_ssl_config_free(&o->conf); + mbedtls_ctr_drbg_free(&o->ctr_drbg); + mbedtls_entropy_free(&o->entropy); + + if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) { + mp_raise_OSError(MP_ENOMEM); + } else { + mp_raise_OSError(MP_EIO); + } } STATIC mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) { From 236297f4e09aa9d7000f7d6b516f3e3e1919dfc6 Mon Sep 17 00:00:00 2001 From: Eric Poulsen Date: Wed, 27 Sep 2017 14:59:49 -0700 Subject: [PATCH 159/163] esp32: Implement wired Ethernet via network.LAN(). Updates to Makefile, modnetwork.c, and addition of network_lan.c to implement `network.LAN()` object for wired PHY objects. --- ports/esp32/Makefile | 4 + ports/esp32/modnetwork.c | 44 ++++++--- ports/esp32/modnetwork.h | 34 +++++++ ports/esp32/network_lan.c | 203 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 270 insertions(+), 15 deletions(-) create mode 100644 ports/esp32/modnetwork.h create mode 100644 ports/esp32/network_lan.c diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index a533fd31d..48d4b9d5c 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -136,6 +136,7 @@ SRC_C = \ machine_uart.c \ modmachine.c \ modnetwork.c \ + network_lan.c \ modsocket.c \ modesp.c \ moduhashlib.c \ @@ -267,6 +268,9 @@ ESPIDF_CXX_O = $(addprefix $(ESPCOMP)/cxx/,\ ESPIDF_ETHERNET_O = $(addprefix $(ESPCOMP)/ethernet/,\ emac_dev.o \ emac_main.o \ + eth_phy/phy_tlk110.o \ + eth_phy/phy_lan8720.o \ + eth_phy/phy_common.o \ ) $(BUILD)/$(ESPCOMP)/expat/%.o: CFLAGS += -Wno-unused-function diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 2eb20d79f..ad2891962 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -7,6 +7,7 @@ * The MIT License (MIT) * * Copyright (c) 2016, 2017 Nick Moore @mnemote + * Copyright (c) 2017 "Eric Poulsen" * * Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky * And the ESP IDF example code which is Public Domain / CC0 @@ -48,6 +49,8 @@ #include "lwip/dns.h" #include "tcpip_adapter.h" +#include "modnetwork.h" + #define MODNETWORK_INCLUDE_CONSTANTS (1) NORETURN void _esp_exceptions(esp_err_t e) { @@ -122,7 +125,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) { ESP_LOGI("wifi", "STA_START"); break; case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI("wifi", "GOT_IP"); + ESP_LOGI("network", "GOT_IP"); break; case SYSTEM_EVENT_STA_DISCONNECTED: { // This is a workaround as ESP32 WiFi libs don't currently @@ -161,7 +164,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) { break; } default: - ESP_LOGI("wifi", "event %d", event->event_id); + ESP_LOGI("network", "event %d", event->event_id); break; } return ESP_OK; @@ -182,6 +185,19 @@ STATIC void require_if(mp_obj_t wlan_if, int if_no) { } STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) { + static int initialized = 0; + if (!initialized) { + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_LOGD("modnetwork", "Initializing WiFi"); + ESP_EXCEPTIONS( esp_wifi_init(&cfg) ); + ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); + ESP_LOGD("modnetwork", "Initialized"); + ESP_EXCEPTIONS( esp_wifi_set_mode(0) ); + ESP_EXCEPTIONS( esp_wifi_start() ); + ESP_LOGD("modnetwork", "Started"); + initialized = 1; + } + int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA; if (idx == WIFI_IF_STA) { return MP_OBJ_FROM_PTR(&wlan_sta_obj); @@ -201,14 +217,6 @@ STATIC mp_obj_t esp_initialize() { ESP_LOGD("modnetwork", "Initializing Event Loop"); ESP_EXCEPTIONS( esp_event_loop_init(event_handler, NULL) ); ESP_LOGD("modnetwork", "esp_event_loop_init done"); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_LOGD("modnetwork", "Initializing WiFi"); - ESP_EXCEPTIONS( esp_wifi_init(&cfg) ); - ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - ESP_LOGD("modnetwork", "Initialized"); - ESP_EXCEPTIONS( esp_wifi_set_mode(0) ); - ESP_EXCEPTIONS( esp_wifi_start() ); - ESP_LOGD("modnetwork", "Started"); initialized = 1; } return mp_const_none; @@ -358,11 +366,11 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG); netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG); // To set a static IP we have to disable DHCP first - if (self->if_id == WIFI_IF_STA) { - esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA); + if (self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) { + esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id); if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e); - ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info)); - ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_STA, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); + ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(self->if_id, &info)); + ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); } else if (self->if_id == WIFI_IF_AP) { esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP); if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e); @@ -374,7 +382,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig); STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { if (n_args != 1 && kwargs->used != 0) { @@ -533,6 +541,7 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) }, { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&esp_initialize_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_wlan_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_LAN), (mp_obj_t)&get_lan_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_phy_mode), (mp_obj_t)&esp_phy_mode_obj }, #if MODNETWORK_INCLUDE_CONSTANTS @@ -560,6 +569,11 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = { MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_WPA_WPA2_PSK) }, { MP_OBJ_NEW_QSTR(MP_QSTR_AUTH_MAX), MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_MAX) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_PHY_LAN8720), + MP_OBJ_NEW_SMALL_INT(PHY_LAN8720) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PHY_TLK110), + MP_OBJ_NEW_SMALL_INT(PHY_TLK110) }, #endif }; diff --git a/ports/esp32/modnetwork.h b/ports/esp32/modnetwork.h new file mode 100644 index 000000000..d9f56591e --- /dev/null +++ b/ports/esp32/modnetwork.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 "Eric Poulsen" + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_ESP32_MODNETWORK_H +#define MICROPY_INCLUDED_ESP32_MODNETWORK_H + +enum { PHY_LAN8720, PHY_TLK110 }; + +MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj); + +#endif diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c new file mode 100644 index 000000000..fba4de73a --- /dev/null +++ b/ports/esp32/network_lan.c @@ -0,0 +1,203 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 "Eric Poulsen" + * + * Based on the ESP IDF example code which is Public Domain / CC0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" + +#include "eth_phy/phy.h" +#include "eth_phy/phy_tlk110.h" +#include "eth_phy/phy_lan8720.h" +#include "tcpip_adapter.h" + +#include "modnetwork.h" + +typedef struct _lan_if_obj_t { + mp_obj_base_t base; + int if_id; // MUST BE FIRST to match wlan_if_obj_t + bool initialized; + bool active; + uint8_t mdc_pin; + uint8_t mdio_pin; + int8_t phy_power_pin; + uint8_t phy_addr; + uint8_t phy_type; + eth_phy_check_link_func link_func; + eth_phy_power_enable_func power_func; +} lan_if_obj_t; + +const mp_obj_type_t lan_if_type; +STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false}; + +STATIC void phy_power_enable(bool enable) { + lan_if_obj_t* self = &lan_obj; + + if (self->phy_power_pin != -1) { + + if (!enable) { + // Do the PHY-specific power_enable(false) function before powering down + self->power_func(false); + } + + gpio_pad_select_gpio(self->phy_power_pin); + gpio_set_direction(self->phy_power_pin, GPIO_MODE_OUTPUT); + if (enable) { + gpio_set_level(self->phy_power_pin, 1); + } else { + gpio_set_level(self->phy_power_pin, 0); + } + + // Allow the power up/down to take effect, min 300us + vTaskDelay(1); + + if (enable) { + // Run the PHY-specific power on operations now the PHY has power + self->power_func(true); + } + } +} + +STATIC void init_lan_rmii() { + lan_if_obj_t* self = &lan_obj; + phy_rmii_configure_data_interface_pins(); + phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin); +} + +STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + lan_if_obj_t* self = &lan_obj; + + if (self->initialized) { + return MP_OBJ_FROM_PTR(&lan_obj); + } + + enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_id].u_obj != mp_const_none) { + if (mp_obj_get_int(args[ARG_id].u_obj) != 0) { + mp_raise_ValueError("invalid LAN interface identifier"); + } + } + + self->mdc_pin = machine_pin_get_id(args[ARG_mdc].u_obj); + self->mdio_pin = machine_pin_get_id(args[ARG_mdio].u_obj); + self->phy_power_pin = args[ARG_power].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[ARG_power].u_obj); + + if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) { + mp_raise_ValueError("invalid phy address"); + } + + if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) { + mp_raise_ValueError("invalid phy type"); + } + + eth_config_t config; + + switch (args[ARG_phy_type].u_int) { + case PHY_TLK110: + config = phy_tlk110_default_ethernet_config; + break; + case PHY_LAN8720: + config = phy_lan8720_default_ethernet_config; + break; + } + + self->link_func = config.phy_check_link; + + // Replace default power func with our own + self->power_func = config.phy_power_enable; + config.phy_power_enable = phy_power_enable; + + config.phy_addr = args[ARG_phy_addr].u_int; + config.gpio_config = init_lan_rmii; + config.tcpip_input = tcpip_adapter_eth_input; + + if (esp_eth_init(&config) == ESP_OK) { + self->active = false; + self->initialized = true; + } else { + mp_raise_msg(&mp_type_OSError, "esp_eth_init() failed"); + } + return MP_OBJ_FROM_PTR(&lan_obj); +} +MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan); + +STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) { + lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); + + if (n_args > 1) { + if (mp_obj_is_true(args[1])) { + self->active = (esp_eth_enable() == ESP_OK); + if (!self->active) { + mp_raise_msg(&mp_type_OSError, "ethernet enable failed"); + } + } else { + self->active = !(esp_eth_disable() == ESP_OK); + if (self->active) { + mp_raise_msg(&mp_type_OSError, "ethernet disable failed"); + } + } + } + return mp_obj_new_bool(self->active); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active); + +STATIC mp_obj_t lan_status(mp_obj_t self_in) { + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status); + +STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) { + lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); + return self->active ? mp_obj_new_bool(self->link_func()) : mp_const_false; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected); + +STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) }, + { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) }, + { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) }, + { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table); + +const mp_obj_type_t lan_if_type = { + { &mp_type_type }, + .name = MP_QSTR_LAN, + .locals_dict = (mp_obj_dict_t*)&lan_if_locals_dict, +}; From 292816a15bda188be75f8c50936dba0b59af262c Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 28 Nov 2017 12:13:47 +1100 Subject: [PATCH 160/163] esp32/mpconfigport.h: Enable websocket module. --- ports/esp32/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 1eb4c408d..69d17fd81 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -139,7 +139,7 @@ #define MICROPY_PY_USSL (1) #define MICROPY_SSL_MBEDTLS (1) #define MICROPY_PY_USSL_FINALISER (1) -#define MICROPY_PY_WEBSOCKET (0) +#define MICROPY_PY_WEBSOCKET (1) #define MICROPY_PY_FRAMEBUF (1) // fatfs configuration From 84035f0f78b7584961fb7092612194f093188071 Mon Sep 17 00:00:00 2001 From: Alex King Date: Wed, 14 Jun 2017 21:03:42 -0500 Subject: [PATCH 161/163] esp32/modesp: Add osdebug() function to disable or change IDF logging. Code lineage: osdebug() is based loosely on the version in esp8266, but there didn't seem to be an obvious way of choosing a particular UART. The basic behavior is the same, though: provide None, and logging is disabled; provide an integer and logging is restored to the default level. To build on that, and because the IDF provides more functionality, a second parameter has now been implemented which allows the active log level to be set: esp.osdebug(uart[, level]) The module has a corresponding set of LOG_ values to set this accordingly. --- ports/esp32/modesp.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ports/esp32/modesp.c b/ports/esp32/modesp.c index caa055164..e614f77a6 100644 --- a/ports/esp32/modesp.c +++ b/ports/esp32/modesp.c @@ -30,6 +30,7 @@ #include #include "rom/gpio.h" +#include "esp_log.h" #include "esp_spi_flash.h" #include "py/runtime.h" @@ -38,6 +39,23 @@ #include "drivers/dht/dht.h" #include "modesp.h" +STATIC mp_obj_t esp_osdebug(size_t n_args, const mp_obj_t *args) { + esp_log_level_t level = LOG_LOCAL_LEVEL; + if (n_args == 2) { + level = mp_obj_get_int(args[1]); + } + if (args[0] == mp_const_none) { + // Disable logging + esp_log_level_set("*", ESP_LOG_ERROR); + } else { + // Enable logging at the given level + // TODO args[0] should set the UART to which debug is sent + esp_log_level_set("*", level); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_osdebug_obj, 1, 2, esp_osdebug); + STATIC mp_obj_t esp_flash_read(mp_obj_t offset_in, mp_obj_t buf_in) { mp_int_t offset = mp_obj_get_int(offset_in); mp_buffer_info_t bufinfo; @@ -107,6 +125,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_); STATIC const mp_rom_map_elem_t esp_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) }, + { MP_ROM_QSTR(MP_QSTR_osdebug), MP_ROM_PTR(&esp_osdebug_obj) }, + { MP_ROM_QSTR(MP_QSTR_flash_read), MP_ROM_PTR(&esp_flash_read_obj) }, { MP_ROM_QSTR(MP_QSTR_flash_write), MP_ROM_PTR(&esp_flash_write_obj) }, { MP_ROM_QSTR(MP_QSTR_flash_erase), MP_ROM_PTR(&esp_flash_erase_obj) }, @@ -118,6 +138,14 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) }, { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) }, + + // Constants for second arg of osdebug() + { MP_ROM_QSTR(MP_QSTR_LOG_NONE), MP_ROM_INT((mp_uint_t)ESP_LOG_NONE)}, + { MP_ROM_QSTR(MP_QSTR_LOG_ERROR), MP_ROM_INT((mp_uint_t)ESP_LOG_ERROR)}, + { MP_ROM_QSTR(MP_QSTR_LOG_WARNING), MP_ROM_INT((mp_uint_t)ESP_LOG_WARN)}, + { MP_ROM_QSTR(MP_QSTR_LOG_INFO), MP_ROM_INT((mp_uint_t)ESP_LOG_INFO)}, + { MP_ROM_QSTR(MP_QSTR_LOG_DEBUG), MP_ROM_INT((mp_uint_t)ESP_LOG_DEBUG)}, + { MP_ROM_QSTR(MP_QSTR_LOG_VERBOSE), MP_ROM_INT((mp_uint_t)ESP_LOG_VERBOSE)}, }; STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table); From aabbbf7fcc689414ff4e09f8d61e25b13ff4ef75 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Mon, 12 Feb 2018 10:29:32 +1100 Subject: [PATCH 162/163] README.md: update to indicate port merged back --- README.md | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 19c27e296..725316203 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -[![Build Status](https://travis-ci.org/micropython/micropython.png?branch=master)](https://travis-ci.org/micropython/micropython) [![Coverage Status](https://coveralls.io/repos/micropython/micropython/badge.png?branch=master)](https://coveralls.io/r/micropython/micropython?branch=master) - The MicroPython project =======================

@@ -13,24 +11,12 @@ You can find the official website at [micropython.org](http://www.micropython.or A note about this ESP32 repository ---------------------------------- -This repository is a clone of the main, upstream repository found at -https://github.com/micropython/micropython. This repository adds a new -branch called `esp32` which contains a port of MicroPython to the ESP32 -microcontroller, under the MIT license. Please see the `README.md` file -in the `ports/esp32/` subdirectory for details of this port. - -This `esp32` branch is the default branch and all pull requests should be -made to this branch, and any issues should discuss only the code developed -in the `ports/esp32/` subdirectory. - -The `esp32` branch will not be rebased so it is safe to clone/fork it and -base your work on it. New commits from the upstream repository will -occasionally be merged in the `esp32` branch. Any additional branches in -this repository (apart from `master`) may be rebased or deleted at any time. +**The ESP32 port has now been merged back into the +[main MicroPython repository](https://github.com/micropython/micropython/) +and this repository is maintained for historical purposes.** -If there is enough interest in the port to the ESP32 then this code can -eventually be merged into the upstream repository. So please do let your -interest be known! +**This repository is now getting out of date and new pull requests should +be made against the master branch of the main repository!** About MicroPython ----------------- From 2f4dac5f121a59fc187c1d9c1f9eade365b3aba1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Feb 2018 15:06:10 +1100 Subject: [PATCH 163/163] Add ISSUE_TEMPLATE.md and PULL_REQUEST_TEMPLATE.md. To direct users to the main MicroPython repository. --- ISSUE_TEMPLATE.md | 6 ++++++ PULL_REQUEST_TEMPLATE.md | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 ISSUE_TEMPLATE.md create mode 100644 PULL_REQUEST_TEMPLATE.md diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..aaf16861f --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,6 @@ +**The ESP32 port has now been merged back into the +[main MicroPython repository](https://github.com/micropython/micropython/) +and this repository is maintained for historical purposes.** + +**Please make new issues and pull requests against the master branch of the +main repository, linked above.** diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 000000000..aaf16861f --- /dev/null +++ b/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,6 @@ +**The ESP32 port has now been merged back into the +[main MicroPython repository](https://github.com/micropython/micropython/) +and this repository is maintained for historical purposes.** + +**Please make new issues and pull requests against the master branch of the +main repository, linked above.**