├── .cproject ├── .gitignore ├── .project ├── Inc ├── ByteBuffer.h ├── PubSubClient.h ├── SerialToTCPBridgeProtocol.h ├── crc.h ├── gpio.h ├── mxconstants.h ├── stm32f3xx_hal_conf.h ├── stm32f3xx_it.h └── usart.h ├── README.md ├── STM32SerialToTCPBridgeClient.elf.launch ├── STM32SerialToTCPBridgeClient.ioc └── Src ├── ByteBuffer.c ├── PubSubClient.c ├── SerialToTCPBridgeProtocol.c ├── crc.c ├── gpio.c ├── main.c ├── stm32f3xx_hal_msp.c ├── stm32f3xx_it.c └── usart.c /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 36 | 37 | 38 | 39 | 50 | 51 | 66 | 67 | 71 | 80 | 81 | 89 | 90 | 91 | 92 | 93 | 94 | 97 | 100 | 101 | 102 | 103 | 104 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Drivers/ 2 | Debug/ 3 | Middlewares/ 4 | .settings/ 5 | STM32F334R8_FLASH.ld 6 | .cproject 7 | .mxproject 8 | .project.bak 9 | STM32SerialToTCPBridgeClient.elf.launch 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | STM32SerialToTCPBridgeClient 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | ?children? 14 | ?name?=outputEntries\|?children?=?name?=entry\\\\\\\|\\\|\|| 15 | 16 | 17 | ?name? 18 | 19 | 20 | 21 | org.eclipse.cdt.make.core.append_environment 22 | true 23 | 24 | 25 | org.eclipse.cdt.make.core.buildArguments 26 | 27 | 28 | 29 | org.eclipse.cdt.make.core.buildCommand 30 | make 31 | 32 | 33 | org.eclipse.cdt.make.core.buildLocation 34 | ${workspace_loc:/STM32100B-EVAL/Debug} 35 | 36 | 37 | org.eclipse.cdt.make.core.contents 38 | org.eclipse.cdt.make.core.activeConfigSettings 39 | 40 | 41 | org.eclipse.cdt.make.core.enableAutoBuild 42 | false 43 | 44 | 45 | org.eclipse.cdt.make.core.enableCleanBuild 46 | true 47 | 48 | 49 | org.eclipse.cdt.make.core.enableFullBuild 50 | true 51 | 52 | 53 | org.eclipse.cdt.make.core.stopOnError 54 | true 55 | 56 | 57 | org.eclipse.cdt.make.core.useDefaultBuildCmd 58 | true 59 | 60 | 61 | 62 | 63 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 64 | 65 | 66 | 67 | 68 | 69 | org.eclipse.cdt.core.cnature 70 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 71 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Inc/ByteBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple, small, 8-bit aligned buffer 3 | */ 4 | 5 | #ifndef BYTEBUFFER_H_ 6 | #define BYTEBUFFER_H_ 7 | 8 | #include 9 | #include 10 | 11 | typedef struct ByteBuffer_t 12 | { 13 | uint8_t Buf[256]; 14 | uint8_t pH; 15 | uint8_t pT; 16 | bool isFull; 17 | 18 | int (*available)(const void* b); 19 | } ByteBuffer; 20 | 21 | // Constructor 22 | void newByteBuffer(ByteBuffer* b); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Inc/PubSubClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | PubSubClient.h - A simple client for MQTT. 3 | Nick O'Leary 4 | http://knolleary.net 5 | */ 6 | 7 | #ifndef PubSubClient_h 8 | #define PubSubClient_h 9 | 10 | #include "SerialToTCPBridgeProtocol.h" 11 | #include 12 | #include 13 | 14 | #define boolean bool 15 | 16 | #define MQTT_VERSION_3_1 3 17 | #define MQTT_VERSION_3_1_1 4 18 | 19 | // MQTT_VERSION : Pick the version 20 | //#define MQTT_VERSION MQTT_VERSION_3_1 21 | #ifndef MQTT_VERSION 22 | #define MQTT_VERSION MQTT_VERSION_3_1_1 23 | #endif 24 | 25 | // MQTT_MAX_PACKET_SIZE : Maximum packet size 26 | #ifndef MQTT_MAX_PACKET_SIZE 27 | #define MQTT_MAX_PACKET_SIZE 128 28 | #endif 29 | 30 | // MQTT_KEEPALIVE : keepAlive interval in Seconds 31 | #ifndef MQTT_KEEPALIVE 32 | #define MQTT_KEEPALIVE 15 33 | #endif 34 | 35 | // MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds 36 | #ifndef MQTT_SOCKET_TIMEOUT 37 | #define MQTT_SOCKET_TIMEOUT 15 38 | #endif 39 | 40 | // MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client 41 | // in each write call. Needed for the Arduino Wifi Shield. Leave undefined to 42 | // pass the entire MQTT packet in each write call. 43 | //#define MQTT_MAX_TRANSFER_SIZE 80 44 | 45 | // Possible values for client.state() 46 | #define MQTT_CONNECTION_TIMEOUT -4 47 | #define MQTT_CONNECTION_LOST -3 48 | #define MQTT_CONNECT_FAILED -2 49 | #define MQTT_DISCONNECTED -1 50 | #define MQTT_CONNECTED 0 51 | #define MQTT_CONNECT_BAD_PROTOCOL 1 52 | #define MQTT_CONNECT_BAD_CLIENT_ID 2 53 | #define MQTT_CONNECT_UNAVAILABLE 3 54 | #define MQTT_CONNECT_BAD_CREDENTIALS 4 55 | #define MQTT_CONNECT_UNAUTHORIZED 5 56 | 57 | #define MQTTCONNECT 1 << 4 // Client request to connect to Server 58 | #define MQTTCONNACK 2 << 4 // Connect Acknowledgment 59 | #define MQTTPUBLISH 3 << 4 // Publish message 60 | #define MQTTPUBACK 4 << 4 // Publish Acknowledgment 61 | #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1) 62 | #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2) 63 | #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3) 64 | #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request 65 | #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment 66 | #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request 67 | #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment 68 | #define MQTTPINGREQ 12 << 4 // PING Request 69 | #define MQTTPINGRESP 13 << 4 // PING Response 70 | #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting 71 | #define MQTTReserved 15 << 4 // Reserved 72 | 73 | #define MQTTQOS0 (0 << 1) 74 | #define MQTTQOS1 (1 << 1) 75 | #define MQTTQOS2 (2 << 1) 76 | 77 | #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int) 78 | 79 | typedef struct PubSubClient_t { 80 | Client* _client; 81 | uint8_t buffer[MQTT_MAX_PACKET_SIZE]; 82 | uint16_t nextMsgId; 83 | unsigned long lastOutActivity; 84 | unsigned long lastInActivity; 85 | bool pingOutstanding; 86 | MQTT_CALLBACK_SIGNATURE; 87 | uint8_t ip[4]; 88 | uint16_t port; 89 | int _state; 90 | 91 | boolean (*connect)(const void*, const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage); 92 | void (*disconnect)(const void*); 93 | boolean (*publish)(const void*, const char* topic, const uint8_t* payload, unsigned int plength, boolean retained); 94 | boolean (*subscribe)(const void*, const char* topic, uint8_t qos); 95 | boolean (*unsubscribe)(const void*, const char* topic); 96 | boolean (*loop)(const void*); 97 | boolean (*connected)(const void*); 98 | int (*state)(const void*); 99 | } PubSubClient; 100 | 101 | // Constructor 102 | void newPubSubClient(PubSubClient* c, uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client* client); 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /Inc/SerialToTCPBridgeProtocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SerialToTCPBridgeProtocol.h 3 | * 4 | * Created on: Jul 29, 2016 5 | * Author: Roan 6 | */ 7 | 8 | #ifndef SERIALTOTCPBRIDGEPROTOCOL_H_ 9 | #define SERIALTOTCPBRIDGEPROTOCOL_H_ 10 | 11 | #include 12 | #include "stm32f3xx_hal.h" 13 | #include "ByteBuffer.h" 14 | 15 | #define SERIAL_TIMEOUT 400 16 | 17 | // Protocol Packet Headers 18 | #define PROTOCOL_CONNECT 0 19 | #define PROTOCOL_CONNACK 1 20 | #define PROTOCOL_DISCONNECT 2 21 | #define PROTOCOL_PUBLISH 3 22 | #define PROTOCOL_ACK 4 23 | 24 | // Protocol Link State 25 | #define STATE_DISCONNECTED 0 26 | #define STATE_CONNECTED 1 27 | 28 | // Protocol Packet RX State 29 | #define RX_PACKET_IDLE 0 30 | #define RX_PACKET_GOTLENGTH 1 31 | #define RX_PACKET_GOTCOMMAND 2 32 | 33 | #define TX_IDLE 0 34 | #define TX_BUSY 1 35 | #define TX_WAIT 2 36 | 37 | // "class" 38 | typedef struct Client_t 39 | { 40 | UART_HandleTypeDef* peripheral_UART; 41 | CRC_HandleTypeDef* peripheral_CRC; 42 | 43 | uint8_t rxByte; // The latest received byte 44 | 45 | uint8_t rxBuffer[256]; // Buffer for incoming uart data (RX) 46 | ByteBuffer txBuf; // Buffer for outgoing uart data (TX) 47 | ByteBuffer readBuf; // Received App data to be read. 48 | 49 | uint8_t workBuffer[128]; 50 | bool ackOutstanding; 51 | bool expectedAckSeq; 52 | bool expectedRxSeqFlag; 53 | uint32_t lastInAct, lastOutAct; 54 | uint8_t state; 55 | 56 | // Arduino Client interface API 57 | int (*connect)(const void*, uint8_t ip[4], uint16_t port); 58 | uint8_t (*connected)(const void*); 59 | int (*available)(const void*); 60 | int (*read)(const void*); 61 | size_t (*write)(const void*, uint8_t* payload, uint8_t pLength); 62 | void (*flush)(const void*); // wait until all sent 63 | void (*stop)(const void*); 64 | } Client; 65 | 66 | // Constructor 67 | void newClient(Client*, UART_HandleTypeDef* uartUnit, CRC_HandleTypeDef* crcUnit); 68 | 69 | // Callback hook ups 70 | void uartTxCompleteCallback(Client* c); 71 | void uartRxCompleteCallback(Client* c); 72 | void tickInterupt(Client* c); 73 | 74 | #endif /* SERIALTOTCPBRIDGEPROTOCOL_H_ */ 75 | -------------------------------------------------------------------------------- /Inc/crc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : CRC.h 4 | * Description : This file provides code for the configuration 5 | * of the CRC instances. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | /* Define to prevent recursive inclusion -------------------------------------*/ 35 | #ifndef __crc_H 36 | #define __crc_H 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* Includes ------------------------------------------------------------------*/ 42 | #include "stm32f3xx_hal.h" 43 | 44 | /* USER CODE BEGIN Includes */ 45 | 46 | /* USER CODE END Includes */ 47 | 48 | extern CRC_HandleTypeDef hcrc; 49 | 50 | /* USER CODE BEGIN Private defines */ 51 | 52 | /* USER CODE END Private defines */ 53 | 54 | extern void Error_Handler(void); 55 | 56 | void MX_CRC_Init(void); 57 | 58 | /* USER CODE BEGIN Prototypes */ 59 | 60 | /* USER CODE END Prototypes */ 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | #endif /*__ crc_H */ 66 | 67 | /** 68 | * @} 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 76 | -------------------------------------------------------------------------------- /Inc/gpio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : gpio.h 4 | * Description : This file contains all the functions prototypes for 5 | * the gpio 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Define to prevent recursive inclusion -------------------------------------*/ 36 | #ifndef __gpio_H 37 | #define __gpio_H 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /* Includes ------------------------------------------------------------------*/ 43 | #include "stm32f3xx_hal.h" 44 | 45 | /* USER CODE BEGIN Includes */ 46 | 47 | /* USER CODE END Includes */ 48 | 49 | /* USER CODE BEGIN Private defines */ 50 | 51 | /* USER CODE END Private defines */ 52 | 53 | void MX_GPIO_Init(void); 54 | 55 | /* USER CODE BEGIN Prototypes */ 56 | 57 | /* USER CODE END Prototypes */ 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | #endif /*__ pinoutConfig_H */ 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** 69 | * @} 70 | */ 71 | 72 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 73 | -------------------------------------------------------------------------------- /Inc/mxconstants.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : mxconstants.h 4 | * Description : This file contains the common defines of the application 5 | ****************************************************************************** 6 | * 7 | * COPYRIGHT(c) 2016 STMicroelectronics 8 | * 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | ****************************************************************************** 32 | */ 33 | /* Define to prevent recursive inclusion -------------------------------------*/ 34 | #ifndef __MXCONSTANT_H 35 | #define __MXCONSTANT_H 36 | /* Includes ------------------------------------------------------------------*/ 37 | 38 | /* USER CODE BEGIN Includes */ 39 | 40 | /* USER CODE END Includes */ 41 | 42 | /* Private define ------------------------------------------------------------*/ 43 | 44 | /* USER CODE BEGIN Private defines */ 45 | 46 | /* USER CODE END Private defines */ 47 | 48 | /** 49 | * @} 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | #endif /* __MXCONSTANT_H */ 57 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 58 | -------------------------------------------------------------------------------- /Inc/stm32f3xx_hal_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f3xx_hal_conf.h 4 | * @brief HAL configuration file. 5 | ****************************************************************************** 6 | * @attention 7 | * 8 | *

© COPYRIGHT(c) 2016 STMicroelectronics

9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Define to prevent recursive inclusion -------------------------------------*/ 36 | #ifndef __STM32F3xx_HAL_CONF_H 37 | #define __STM32F3xx_HAL_CONF_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | #include "mxconstants.h" 44 | 45 | /* Exported types ------------------------------------------------------------*/ 46 | /* Exported constants --------------------------------------------------------*/ 47 | 48 | /* ########################## Module Selection ############################## */ 49 | /** 50 | * @brief This is the list of modules to be used in the HAL driver 51 | */ 52 | 53 | #define HAL_MODULE_ENABLED 54 | /*#define HAL_ADC_MODULE_ENABLED */ 55 | /*#define HAL_CAN_MODULE_ENABLED */ 56 | /*#define HAL_CEC_MODULE_ENABLED */ 57 | /*#define HAL_NAND_MODULE_ENABLED */ 58 | /*#define HAL_NOR_MODULE_ENABLED */ 59 | /*#define HAL_PCCARD_MODULE_ENABLED */ 60 | /*#define HAL_SRAM_MODULE_ENABLED */ 61 | /*#define HAL_HRTIM_MODULE_ENABLED */ 62 | /*#define HAL_OPAMP_MODULE_ENABLED */ 63 | /*#define HAL_SDADC_MODULE_ENABLED */ 64 | /*#define HAL_TSC_MODULE_ENABLED */ 65 | /*#define HAL_COMP_MODULE_ENABLED */ 66 | #define HAL_CRC_MODULE_ENABLED 67 | /*#define HAL_CRYP_MODULE_ENABLED */ 68 | /*#define HAL_DAC_MODULE_ENABLED */ 69 | /*#define HAL_I2S_MODULE_ENABLED */ 70 | /*#define HAL_IWDG_MODULE_ENABLED */ 71 | /*#define HAL_LCD_MODULE_ENABLED */ 72 | /*#define HAL_LPTIM_MODULE_ENABLED */ 73 | /*#define HAL_RNG_MODULE_ENABLED */ 74 | /*#define HAL_RTC_MODULE_ENABLED */ 75 | /*#define HAL_SPI_MODULE_ENABLED */ 76 | /*#define HAL_TIM_MODULE_ENABLED */ 77 | #define HAL_UART_MODULE_ENABLED 78 | /*#define HAL_USART_MODULE_ENABLED */ 79 | /*#define HAL_IRDA_MODULE_ENABLED */ 80 | /*#define HAL_SMARTCARD_MODULE_ENABLED */ 81 | /*#define HAL_SMBUS_MODULE_ENABLED */ 82 | /*#define HAL_WWDG_MODULE_ENABLED */ 83 | /*#define HAL_PCD_MODULE_ENABLED */ 84 | #define HAL_GPIO_MODULE_ENABLED 85 | #define HAL_DMA_MODULE_ENABLED 86 | #define HAL_RCC_MODULE_ENABLED 87 | #define HAL_FLASH_MODULE_ENABLED 88 | #define HAL_PWR_MODULE_ENABLED 89 | #define HAL_CORTEX_MODULE_ENABLED 90 | #define HAL_I2C_MODULE_ENABLED 91 | /* ########################## HSE/HSI Values adaptation ##################### */ 92 | /** 93 | * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. 94 | * This value is used by the RCC HAL module to compute the system frequency 95 | * (when HSE is used as system clock source, directly or through the PLL). 96 | */ 97 | #if !defined (HSE_VALUE) 98 | #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ 99 | #endif /* HSE_VALUE */ 100 | 101 | /** 102 | * @brief In the following line adjust the External High Speed oscillator (HSE) Startup 103 | * Timeout value 104 | */ 105 | #if !defined (HSE_STARTUP_TIMEOUT) 106 | #define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ 107 | #endif /* HSE_STARTUP_TIMEOUT */ 108 | 109 | /** 110 | * @brief Internal High Speed oscillator (HSI) value. 111 | * This value is used by the RCC HAL module to compute the system frequency 112 | * (when HSI is used as system clock source, directly or through the PLL). 113 | */ 114 | #if !defined (HSI_VALUE) 115 | #define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ 116 | #endif /* HSI_VALUE */ 117 | 118 | /** 119 | * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup 120 | * Timeout value 121 | */ 122 | #if !defined (HSI_STARTUP_TIMEOUT) 123 | #define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ 124 | #endif /* HSI_STARTUP_TIMEOUT */ 125 | 126 | /** 127 | * @brief Internal Low Speed oscillator (LSI) value. 128 | */ 129 | #if !defined (LSI_VALUE) 130 | #define LSI_VALUE ((uint32_t)40000) 131 | #endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz 132 | The real value may vary depending on the variations 133 | in voltage and temperature. */ 134 | /** 135 | * @brief External Low Speed oscillator (LSE) value. 136 | */ 137 | #if !defined (LSE_VALUE) 138 | #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ 139 | #endif /* LSE_VALUE */ 140 | 141 | /** 142 | * @brief Time out for LSE start up value in ms. 143 | */ 144 | #if !defined (LSE_STARTUP_TIMEOUT) 145 | #define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ 146 | #endif /* LSE_STARTUP_TIMEOUT */ 147 | 148 | /** 149 | * @brief External clock source for I2S peripheral 150 | * This value is used by the I2S HAL module to compute the I2S clock source 151 | * frequency, this source is inserted directly through I2S_CKIN pad. 152 | * - External clock generated through external PLL component on EVAL 303 (based on MCO or crystal) 153 | * - External clock not generated on EVAL 373 154 | */ 155 | #if !defined (EXTERNAL_CLOCK_VALUE) 156 | #define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ 157 | #endif /* EXTERNAL_CLOCK_VALUE */ 158 | 159 | /* Tip: To avoid modifying this file each time you need to use different HSE, 160 | === you can define the HSE value in your toolchain compiler preprocessor. */ 161 | 162 | /* ########################### System Configuration ######################### */ 163 | /** 164 | * @brief This is the HAL system configuration section 165 | */ 166 | 167 | #define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ 168 | #define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ 169 | #define USE_RTOS 0 170 | #define PREFETCH_ENABLE 1 171 | #define INSTRUCTION_CACHE_ENABLE 0 172 | #define DATA_CACHE_ENABLE 0 173 | 174 | /* ########################## Assert Selection ############################## */ 175 | /** 176 | * @brief Uncomment the line below to expanse the "assert_param" macro in the 177 | * HAL drivers code 178 | */ 179 | /* #define USE_FULL_ASSERT 1 */ 180 | 181 | /* Includes ------------------------------------------------------------------*/ 182 | /** 183 | * @brief Include module's header file 184 | */ 185 | 186 | #ifdef HAL_RCC_MODULE_ENABLED 187 | #include "stm32f3xx_hal_rcc.h" 188 | #endif /* HAL_RCC_MODULE_ENABLED */ 189 | 190 | #ifdef HAL_GPIO_MODULE_ENABLED 191 | #include "stm32f3xx_hal_gpio.h" 192 | #endif /* HAL_GPIO_MODULE_ENABLED */ 193 | 194 | #ifdef HAL_DMA_MODULE_ENABLED 195 | #include "stm32f3xx_hal_dma.h" 196 | #endif /* HAL_DMA_MODULE_ENABLED */ 197 | 198 | #ifdef HAL_CORTEX_MODULE_ENABLED 199 | #include "stm32f3xx_hal_cortex.h" 200 | #endif /* HAL_CORTEX_MODULE_ENABLED */ 201 | 202 | #ifdef HAL_ADC_MODULE_ENABLED 203 | #include "stm32f3xx_hal_adc.h" 204 | #endif /* HAL_ADC_MODULE_ENABLED */ 205 | 206 | #ifdef HAL_CAN_MODULE_ENABLED 207 | #include "stm32f3xx_hal_can.h" 208 | #endif /* HAL_CAN_MODULE_ENABLED */ 209 | 210 | #ifdef HAL_CEC_MODULE_ENABLED 211 | #include "stm32f3xx_hal_cec.h" 212 | #endif /* HAL_CEC_MODULE_ENABLED */ 213 | 214 | #ifdef HAL_COMP_MODULE_ENABLED 215 | #include "stm32f3xx_hal_comp.h" 216 | #endif /* HAL_COMP_MODULE_ENABLED */ 217 | 218 | #ifdef HAL_CRC_MODULE_ENABLED 219 | #include "stm32f3xx_hal_crc.h" 220 | #endif /* HAL_CRC_MODULE_ENABLED */ 221 | 222 | #ifdef HAL_DAC_MODULE_ENABLED 223 | #include "stm32f3xx_hal_dac.h" 224 | #endif /* HAL_DAC_MODULE_ENABLED */ 225 | 226 | #ifdef HAL_FLASH_MODULE_ENABLED 227 | #include "stm32f3xx_hal_flash.h" 228 | #endif /* HAL_FLASH_MODULE_ENABLED */ 229 | 230 | #ifdef HAL_SRAM_MODULE_ENABLED 231 | #include "stm32f3xx_hal_sram.h" 232 | #endif /* HAL_SRAM_MODULE_ENABLED */ 233 | 234 | #ifdef HAL_NOR_MODULE_ENABLED 235 | #include "stm32f3xx_hal_nor.h" 236 | #endif /* HAL_NOR_MODULE_ENABLED */ 237 | 238 | #ifdef HAL_NAND_MODULE_ENABLED 239 | #include "stm32f3xx_hal_nand.h" 240 | #endif /* HAL_NAND_MODULE_ENABLED */ 241 | 242 | #ifdef HAL_PCCARD_MODULE_ENABLED 243 | #include "stm32f3xx_hal_pccard.h" 244 | #endif /* HAL_PCCARD_MODULE_ENABLED */ 245 | 246 | #ifdef HAL_HRTIM_MODULE_ENABLED 247 | #include "stm32f3xx_hal_hrtim.h" 248 | #endif /* HAL_HRTIM_MODULE_ENABLED */ 249 | 250 | #ifdef HAL_I2C_MODULE_ENABLED 251 | #include "stm32f3xx_hal_i2c.h" 252 | #endif /* HAL_I2C_MODULE_ENABLED */ 253 | 254 | #ifdef HAL_I2S_MODULE_ENABLED 255 | #include "stm32f3xx_hal_i2s.h" 256 | #endif /* HAL_I2S_MODULE_ENABLED */ 257 | 258 | #ifdef HAL_IRDA_MODULE_ENABLED 259 | #include "stm32f3xx_hal_irda.h" 260 | #endif /* HAL_IRDA_MODULE_ENABLED */ 261 | 262 | #ifdef HAL_IWDG_MODULE_ENABLED 263 | #include "stm32f3xx_hal_iwdg.h" 264 | #endif /* HAL_IWDG_MODULE_ENABLED */ 265 | 266 | #ifdef HAL_OPAMP_MODULE_ENABLED 267 | #include "stm32f3xx_hal_opamp.h" 268 | #endif /* HAL_OPAMP_MODULE_ENABLED */ 269 | 270 | #ifdef HAL_PCD_MODULE_ENABLED 271 | #include "stm32f3xx_hal_pcd.h" 272 | #endif /* HAL_PCD_MODULE_ENABLED */ 273 | 274 | #ifdef HAL_PWR_MODULE_ENABLED 275 | #include "stm32f3xx_hal_pwr.h" 276 | #endif /* HAL_PWR_MODULE_ENABLED */ 277 | 278 | #ifdef HAL_RTC_MODULE_ENABLED 279 | #include "stm32f3xx_hal_rtc.h" 280 | #endif /* HAL_RTC_MODULE_ENABLED */ 281 | 282 | #ifdef HAL_SDADC_MODULE_ENABLED 283 | #include "stm32f3xx_hal_sdadc.h" 284 | #endif /* HAL_SDADC_MODULE_ENABLED */ 285 | 286 | #ifdef HAL_SMARTCARD_MODULE_ENABLED 287 | #include "stm32f3xx_hal_smartcard.h" 288 | #endif /* HAL_SMARTCARD_MODULE_ENABLED */ 289 | 290 | #ifdef HAL_SMBUS_MODULE_ENABLED 291 | #include "stm32f3xx_hal_smbus.h" 292 | #endif /* HAL_SMBUS_MODULE_ENABLED */ 293 | 294 | #ifdef HAL_SPI_MODULE_ENABLED 295 | #include "stm32f3xx_hal_spi.h" 296 | #endif /* HAL_SPI_MODULE_ENABLED */ 297 | 298 | #ifdef HAL_TIM_MODULE_ENABLED 299 | #include "stm32f3xx_hal_tim.h" 300 | #endif /* HAL_TIM_MODULE_ENABLED */ 301 | 302 | #ifdef HAL_TSC_MODULE_ENABLED 303 | #include "stm32f3xx_hal_tsc.h" 304 | #endif /* HAL_TSC_MODULE_ENABLED */ 305 | 306 | #ifdef HAL_UART_MODULE_ENABLED 307 | #include "stm32f3xx_hal_uart.h" 308 | #endif /* HAL_UART_MODULE_ENABLED */ 309 | 310 | #ifdef HAL_USART_MODULE_ENABLED 311 | #include "stm32f3xx_hal_usart.h" 312 | #endif /* HAL_USART_MODULE_ENABLED */ 313 | 314 | #ifdef HAL_WWDG_MODULE_ENABLED 315 | #include "stm32f3xx_hal_wwdg.h" 316 | #endif /* HAL_WWDG_MODULE_ENABLED */ 317 | 318 | /* Exported macro ------------------------------------------------------------*/ 319 | #ifdef USE_FULL_ASSERT 320 | /** 321 | * @brief The assert_param macro is used for function's parameters check. 322 | * @param expr: If expr is false, it calls assert_failed function 323 | * which reports the name of the source file and the source 324 | * line number of the call that failed. 325 | * If expr is true, it returns no value. 326 | * @retval None 327 | */ 328 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 329 | /* Exported functions ------------------------------------------------------- */ 330 | void assert_failed(uint8_t* file, uint32_t line); 331 | #else 332 | #define assert_param(expr) ((void)0) 333 | #endif /* USE_FULL_ASSERT */ 334 | 335 | #ifdef __cplusplus 336 | } 337 | #endif 338 | 339 | #endif /* __STM32F3xx_HAL_CONF_H */ 340 | 341 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 342 | -------------------------------------------------------------------------------- /Inc/stm32f3xx_it.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f3xx_it.h 4 | * @brief This file contains the headers of the interrupt handlers. 5 | ****************************************************************************** 6 | * 7 | * COPYRIGHT(c) 2016 STMicroelectronics 8 | * 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | ****************************************************************************** 32 | */ 33 | 34 | /* Define to prevent recursive inclusion -------------------------------------*/ 35 | #ifndef __STM32F3xx_IT_H 36 | #define __STM32F3xx_IT_H 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /* Includes ------------------------------------------------------------------*/ 43 | /* Exported types ------------------------------------------------------------*/ 44 | /* Exported constants --------------------------------------------------------*/ 45 | /* Exported macro ------------------------------------------------------------*/ 46 | /* Exported functions ------------------------------------------------------- */ 47 | 48 | void NMI_Handler(void); 49 | void HardFault_Handler(void); 50 | void MemManage_Handler(void); 51 | void BusFault_Handler(void); 52 | void UsageFault_Handler(void); 53 | void SVC_Handler(void); 54 | void DebugMon_Handler(void); 55 | void PendSV_Handler(void); 56 | void SysTick_Handler(void); 57 | void USART2_IRQHandler(void); 58 | void EXTI15_10_IRQHandler(void); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif /* __STM32F3xx_IT_H */ 65 | 66 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 67 | -------------------------------------------------------------------------------- /Inc/usart.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : USART.h 4 | * Description : This file provides code for the configuration 5 | * of the USART instances. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | /* Define to prevent recursive inclusion -------------------------------------*/ 35 | #ifndef __usart_H 36 | #define __usart_H 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* Includes ------------------------------------------------------------------*/ 42 | #include "stm32f3xx_hal.h" 43 | 44 | /* USER CODE BEGIN Includes */ 45 | 46 | /* USER CODE END Includes */ 47 | 48 | extern UART_HandleTypeDef huart2; 49 | 50 | /* USER CODE BEGIN Private defines */ 51 | 52 | /* USER CODE END Private defines */ 53 | 54 | extern void Error_Handler(void); 55 | 56 | void MX_USART2_UART_Init(void); 57 | 58 | /* USER CODE BEGIN Prototypes */ 59 | 60 | /* USER CODE END Prototypes */ 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | #endif /*__ usart_H */ 66 | 67 | /** 68 | * @} 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STM32 Serial To TCP Bridge Client 2 | STM32 based client for [SerialToTCPBridgeProtocol](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) gateway service. 3 | 4 | Open a TCP connection to a server from the STM32 using just UART. (No shields or network HW) 5 | See [this](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) for more information on the protocol and for the **Protocol Gateway** you will need to run on the host the STM32 is connected to serially. 6 | Currently, there is a MQTT protocol implementation over this connection as an example. 7 | 8 | ## Used 9 | - Atollic TrueSTUDIO for ARM Lite 5.5.2+ 10 | - STM32CubeMX plugin for Eclipse (4.15.1.201606091139) 11 | - STM32F3 firmware package (1.5.0) 12 | - STM32 Nucleo F334R8 board 13 | 14 | ## How to 15 | - Install software. 16 | - Clone repo in your Atollic workspace. 17 | - Open Atollic by clicking `.project` file. 18 | - In Atollic, open the STM32CubeMX perspective. 19 | - Inside STM32CubeMX, open `STM32SerialToTCPBridgeClient.ioc` file. Click *Generate source code*. 20 | - In Atollic, open C/C++ perspective and click *Rebuild*. 21 | - Get the [Protocol Gateway](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) and build it. 22 | - Change the gateway's config to listen on the COM port connected to your STM32 and start it. 23 | - With the STM32 Nucleo ST-Link debugger connected, click *Debug* to load the program. 24 | - Once in the Debug perspective, click *Resume* to start program on STM32. 25 | 26 | #### Details 27 | - The protocol provides the app an in order, duplicates free and error checked byte stream by adding a CRC32 and simple retry mechanism. See [this](https://en.wikibooks.org/wiki/Serial_Programming/Error_Correction_Methods) for background. 28 | - The Protocol utilizes the internal CRC32 unit on the STM32 for communication error checking. 29 | - The **Protocol Gateway** opens a real TCP connection to a set destination on behalf of the **Protocol Client** running on the STM32, and forwards traffic bi-directionally. 30 | 31 | #### Included Example 32 | - A stripped down version of [knolleary's MQTT library for Arduino](https://github.com/knolleary/pubsubclient) is used. 33 | - When the microcontroller starts up it dials to a HiveMQ MQTT broker running on the host. (127.0.0.1, 1883) 34 | - Pressing the button on the Nucleo board the first time causes it to subscribe to topic `led/#`. Subsequent presses publishes a message on another MQTT topic. 35 | - Publishing `1` from another MQTT client to the `led` topic will power on the led on the Nucleo board. Publishing `2` will power it off. 36 | 37 | #### Future plans 38 | - Optimization & more protocol feature complete 39 | - Unit tests 40 | -------------------------------------------------------------------------------- /STM32SerialToTCPBridgeClient.elf.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /STM32SerialToTCPBridgeClient.ioc: -------------------------------------------------------------------------------- 1 | #MicroXplorer Configuration settings - do not modify 2 | CRC.IPParameters=InputDataInversionMode,OutputDataInversionMode 3 | CRC.InputDataInversionMode=CRC_INPUTDATA_INVERSION_BYTE 4 | CRC.OutputDataInversionMode=CRC_OUTPUTDATA_INVERSION_ENABLE 5 | File.Version=6 6 | KeepUserPlacement=false 7 | Mcu.Family=STM32F3 8 | Mcu.IP0=CRC 9 | Mcu.IP1=NVIC 10 | Mcu.IP2=RCC 11 | Mcu.IP3=SYS 12 | Mcu.IP4=USART2 13 | Mcu.IPNb=5 14 | Mcu.Name=STM32F334R(6-8)Tx 15 | Mcu.Package=LQFP64 16 | Mcu.Pin0=PC13 17 | Mcu.Pin1=PF0 / OSC_IN 18 | Mcu.Pin2=PA2 19 | Mcu.Pin3=PA3 20 | Mcu.Pin4=PA5 21 | Mcu.Pin5=VP_CRC_VS_CRC 22 | Mcu.Pin6=VP_SYS_VS_Systick 23 | Mcu.PinsNb=7 24 | Mcu.UserConstants= 25 | Mcu.UserName=STM32F334R8Tx 26 | MxCube.Version=4.15.1 27 | MxDb.Version=DB.4.0.151 28 | NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true 29 | NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true 30 | NVIC.EXTI15_10_IRQn=true\:2\:0\:true\:false\:true 31 | NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true 32 | NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true 33 | NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true 34 | NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true 35 | NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 36 | NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true 37 | NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true 38 | NVIC.USART2_IRQn=true\:1\:0\:true\:false\:true 39 | NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true 40 | PA2.Mode=Asynchronous 41 | PA2.Signal=USART2_TX 42 | PA3.Mode=Asynchronous 43 | PA3.Signal=USART2_RX 44 | PA5.Locked=true 45 | PA5.Signal=GPIO_Output 46 | PC13.GPIOParameters=GPIO_ModeDefaultEXTI 47 | PC13.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING 48 | PC13.Locked=true 49 | PC13.Signal=GPXTI13 50 | PCC.Checker=false 51 | PCC.Line=STM32F334 52 | PCC.MCU=STM32F334R(6-8)Tx 53 | PCC.MXVersion=4.15.1 54 | PCC.PartNumber=STM32F334R8Tx 55 | PCC.Seq0=0 56 | PCC.Series=STM32F3 57 | PCC.Temperature=25 58 | PCC.Vdd=3.6 59 | PF0\ /\ OSC_IN.Mode=HSE-External-Clock-Source 60 | PF0\ /\ OSC_IN.Signal=RCC_OSC_IN 61 | ProjectManager.AskForMigrate=true 62 | ProjectManager.BackupPrevious=false 63 | ProjectManager.CompilerOptimize=2 64 | ProjectManager.ComputerToolchain=false 65 | ProjectManager.CoupleFile=true 66 | ProjectManager.DeletePrevious=true 67 | ProjectManager.DeviceId=STM32F334R8Tx 68 | ProjectManager.FirmwarePackage=STM32Cube FW_F3 V1.5.0 69 | ProjectManager.FreePins=false 70 | ProjectManager.HalAssertFull=false 71 | ProjectManager.HeapSize=0x200 72 | ProjectManager.KeepUserCode=true 73 | ProjectManager.LastFirmware=true 74 | ProjectManager.LibraryCopy=1 75 | ProjectManager.PreviousToolchain=TrueSTUDIO 76 | ProjectManager.ProjectBuild=false 77 | ProjectManager.ProjectFileName=STM32SerialToTCPBridgeClient.ioc 78 | ProjectManager.ProjectName=STM32SerialToTCPBridgeClient 79 | ProjectManager.StackSize=0x400 80 | ProjectManager.TargetToolchain=TrueSTUDIO 81 | ProjectManager.ToolChainLocation=C\:\\Users\\Roan\\Atollic\\TrueSTUDIO\\ARM_workspace_5.5\\STM32SerialToTCPBridgeClient 82 | ProjectManager.UnderRoot=true 83 | ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false,2-MX_CRC_Init-CRC-false,3-MX_USART2_UART_Init-USART2-false 84 | RCC.ADC12outputFreq_Value=72000000 85 | RCC.AHBFreq_Value=72000000 86 | RCC.APB1CLKDivider=RCC_HCLK_DIV2 87 | RCC.APB1Freq_Value=36000000 88 | RCC.APB1TimFreq_Value=72000000 89 | RCC.APB2Freq_Value=72000000 90 | RCC.APB2TimFreq_Value=72000000 91 | RCC.CortexFreq_Value=72000000 92 | RCC.FCLKCortexFreq_Value=72000000 93 | RCC.FamilyName=M 94 | RCC.HCLKFreq_Value=72000000 95 | RCC.HRTIM1Freq_Value=72000000 96 | RCC.HSEPLLFreq_Value=8000000 97 | RCC.HSE_VALUE=8000000 98 | RCC.HSIPLLFreq_Value=4000000 99 | RCC.HSIState=RCC_HSI_ON 100 | RCC.HSI_VALUE=8000000 101 | RCC.I2C1Freq_Value=8000000 102 | RCC.IPParameters=ADC12outputFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HRTIM1Freq_Value,HSEPLLFreq_Value,HSE_VALUE,HSIPLLFreq_Value,HSIState,HSI_VALUE,I2C1Freq_Value,LSE_VALUE,LSI_VALUE,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,PLLSourceVirtual,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,SYSCLKSourceVirtual,TIM1Freq_Value,TIM2Freq_Value,USART1Freq_Value,VCOOutput2Freq_Value 103 | RCC.LSE_VALUE=32768 104 | RCC.LSI_VALUE=40000 105 | RCC.MCOFreq_Value=72000000 106 | RCC.PLLCLKFreq_Value=72000000 107 | RCC.PLLMCOFreq_Value=72000000 108 | RCC.PLLMUL=RCC_PLL_MUL9 109 | RCC.PLLSourceVirtual=RCC_PLLSOURCE_HSE 110 | RCC.RTCFreq_Value=40000 111 | RCC.RTCHSEDivFreq_Value=250000 112 | RCC.SYSCLKFreq_VALUE=72000000 113 | RCC.SYSCLKSourceVirtual=RCC_SYSCLKSOURCE_PLLCLK 114 | RCC.TIM1Freq_Value=72000000 115 | RCC.TIM2Freq_Value=72000000 116 | RCC.USART1Freq_Value=36000000 117 | RCC.VCOOutput2Freq_Value=8000000 118 | SH.GPXTI13.0=GPIO_EXTI13 119 | SH.GPXTI13.ConfNb=1 120 | USART2.BaudRate=115200 121 | USART2.IPParameters=BaudRate,WordLength 122 | USART2.WordLength=UART_WORDLENGTH_8B 123 | VP_CRC_VS_CRC.Mode=CRC_Activate 124 | VP_CRC_VS_CRC.Signal=CRC_VS_CRC 125 | VP_SYS_VS_Systick.Mode=SysTick 126 | VP_SYS_VS_Systick.Signal=SYS_VS_Systick 127 | board=STM32SerialToTCPBridgeClient 128 | -------------------------------------------------------------------------------- /Src/ByteBuffer.c: -------------------------------------------------------------------------------- 1 | #include "ByteBuffer.h" 2 | 3 | static int availablePublic(const void* b) 4 | { 5 | ByteBuffer* self = (ByteBuffer*)b; 6 | 7 | if (self->isFull) 8 | { 9 | return 256; 10 | } 11 | if (self->pT >= self->pH) 12 | { 13 | return (int)(self->pT - self->pH); 14 | } else 15 | { 16 | return 256 - (int)(self->pH - self->pT); 17 | } 18 | } 19 | 20 | // Constructor 21 | void newByteBuffer(ByteBuffer* b) 22 | { 23 | b->pH = 0; 24 | b->pT = 0; 25 | b->isFull = false; 26 | b->available = availablePublic; 27 | } 28 | -------------------------------------------------------------------------------- /Src/PubSubClient.c: -------------------------------------------------------------------------------- 1 | /* 2 | PubSubClient.cpp - A simple client for MQTT. 3 | Nick O'Leary 4 | http://knolleary.net 5 | */ 6 | 7 | #include "PubSubClient.h" 8 | 9 | // PRIVATE METHODS 10 | // reads a byte into result 11 | static boolean MQTTreadByte(PubSubClient* c, uint8_t * result) { 12 | uint32_t previousMillis = HAL_GetTick(); 13 | while(!c->_client->available(c->_client)) 14 | { 15 | uint32_t currentMillis = HAL_GetTick(); 16 | if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)) 17 | { 18 | return false; 19 | } 20 | } 21 | *result = c->_client->read(c->_client); 22 | return true; 23 | } 24 | 25 | // reads a byte into result[*index] and increments index 26 | static boolean MQTTreadByteFromIndex(PubSubClient* c, uint8_t * result, uint16_t * index){ 27 | uint16_t current_index = *index; 28 | uint8_t * write_address = &(result[current_index]); 29 | if(MQTTreadByte(c, write_address)) 30 | { 31 | *index = current_index + 1; 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | static uint16_t MQTTreadPacket(PubSubClient* c, uint8_t* lengthLength) { 38 | uint16_t len = 0; 39 | if(!MQTTreadByteFromIndex(c, c->buffer, &len)) return 0; 40 | bool isPublish = (c->buffer[0]&0xF0) == MQTTPUBLISH; 41 | uint32_t multiplier = 1; 42 | uint16_t length = 0; 43 | uint8_t digit = 0; 44 | uint16_t skip = 0; 45 | uint8_t start = 0; 46 | 47 | do { 48 | if(!MQTTreadByte(c, &digit)) return 0; 49 | c->buffer[len++] = digit; 50 | length += (digit & 127) * multiplier; 51 | multiplier *= 128; 52 | } while ((digit & 128) != 0); 53 | *lengthLength = len-1; 54 | 55 | if (isPublish) { 56 | // Read in topic length to calculate bytes to skip over for Stream writing 57 | if(!MQTTreadByteFromIndex(c, c->buffer, &len)) return 0; 58 | if(!MQTTreadByteFromIndex(c, c->buffer, &len)) return 0; 59 | skip = (c->buffer[*lengthLength+1]<<8)+c->buffer[*lengthLength+2]; 60 | start = 2; 61 | if (c->buffer[0]&MQTTQOS1) { 62 | // skip message id 63 | skip += 2; 64 | } 65 | } 66 | 67 | for (uint16_t i = start;ibuffer[len] = digit; 71 | } 72 | len++; 73 | } 74 | 75 | if (len > MQTT_MAX_PACKET_SIZE) { 76 | len = 0; // This will cause the packet to be ignored. 77 | } 78 | 79 | return len; 80 | } 81 | 82 | static boolean MQTTwrite(PubSubClient* c, uint8_t header, uint8_t* buf, uint16_t length) { 83 | uint8_t lenBuf[4]; 84 | uint8_t llen = 0; 85 | uint8_t digit; 86 | uint8_t pos = 0; 87 | uint16_t rc; 88 | uint16_t len = length; 89 | do { 90 | digit = len % 128; 91 | len = len / 128; 92 | if (len > 0) { 93 | digit |= 0x80; 94 | } 95 | lenBuf[pos++] = digit; 96 | llen++; 97 | } while(len>0); 98 | 99 | buf[4-llen] = header; 100 | for (int i=0;i 0) && result) { 110 | bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining; 111 | rc = c->_client->write(writeBuf,bytesToWrite); 112 | result = (rc == bytesToWrite); 113 | bytesRemaining -= rc; 114 | writeBuf += rc; 115 | } 116 | return result; 117 | #else 118 | rc = c->_client->write(c->_client, buf+(4-llen),length+1+llen); 119 | c->lastOutActivity = HAL_GetTick(); 120 | return (rc == 1+llen+length); 121 | #endif 122 | } 123 | 124 | static uint16_t MQTTwriteString(const char* string, uint8_t* buf, uint16_t pos) { 125 | const char* idp = string; 126 | uint16_t i = 0; 127 | pos += 2; 128 | while (*idp) { 129 | buf[pos++] = *idp++; 130 | i++; 131 | } 132 | buf[pos-i-2] = (i >> 8); 133 | buf[pos-i-1] = (i & 0xFF); 134 | return pos; 135 | } 136 | 137 | // PUBLIC METHODS 138 | static boolean MQTTconnectedPublic(const void* c) { 139 | PubSubClient* self = (PubSubClient*)c; 140 | 141 | boolean rc; 142 | if (self->_client == NULL ) { 143 | rc = false; 144 | } else { 145 | rc = (int)self->_client->connected(self->_client); 146 | if (!rc) { 147 | if (self->_state == MQTT_CONNECTED) { 148 | self->_state = MQTT_CONNECTION_LOST; 149 | self->_client->flush(self->_client); 150 | self->_client->stop(self->_client); 151 | } 152 | } 153 | } 154 | return rc; 155 | } 156 | 157 | static boolean MQTTconnectPublic(const void* c, const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) 158 | { 159 | PubSubClient* self = (PubSubClient*)c; 160 | 161 | if (!MQTTconnectedPublic(self)) { 162 | int result = 0; 163 | 164 | result = self->_client->connect(self->_client, self->ip, self->port); 165 | 166 | if (result == 1) { 167 | self->nextMsgId = 1; 168 | // Leave room in the buffer for header and variable length field 169 | uint16_t length = 5; 170 | unsigned int j; 171 | 172 | #if MQTT_VERSION == MQTT_VERSION_3_1 173 | uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION}; 174 | #define MQTT_HEADER_VERSION_LENGTH 9 175 | #elif MQTT_VERSION == MQTT_VERSION_3_1_1 176 | uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION}; 177 | #define MQTT_HEADER_VERSION_LENGTH 7 178 | #endif 179 | for (j = 0;jbuffer[length++] = d[j]; 181 | } 182 | 183 | uint8_t v; 184 | if (willTopic) { 185 | v = 0x06|(willQos<<3)|(willRetain<<5); 186 | } else { 187 | v = 0x02; 188 | } 189 | 190 | if(user != NULL) { 191 | v = v|0x80; 192 | 193 | if(pass != NULL) { 194 | v = v|(0x80>>1); 195 | } 196 | } 197 | 198 | self->buffer[length++] = v; 199 | 200 | self->buffer[length++] = ((MQTT_KEEPALIVE) >> 8); 201 | self->buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF); 202 | length = MQTTwriteString(id,self->buffer,length); 203 | if (willTopic) { 204 | length = MQTTwriteString(willTopic,self->buffer,length); 205 | length = MQTTwriteString(willMessage,self->buffer,length); 206 | } 207 | 208 | if(user != NULL) { 209 | length = MQTTwriteString(user,self->buffer,length); 210 | if(pass != NULL) { 211 | length = MQTTwriteString(pass,self->buffer,length); 212 | } 213 | } 214 | 215 | MQTTwrite(self, MQTTCONNECT,self->buffer,length-5); 216 | 217 | self->lastInActivity = self->lastOutActivity = HAL_GetTick(); 218 | 219 | while (!self->_client->available(self->_client)) { 220 | unsigned long t = HAL_GetTick(); 221 | if (t-self->lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) { 222 | self->_state = MQTT_CONNECTION_TIMEOUT; 223 | self->_client->stop(self->_client); 224 | return false; 225 | } 226 | } 227 | uint8_t llen; 228 | uint16_t len = MQTTreadPacket(self, &llen); 229 | 230 | if (len == 4) { 231 | if (self->buffer[3] == 0) { 232 | self->lastInActivity = HAL_GetTick(); 233 | self->pingOutstanding = false; 234 | self->_state = MQTT_CONNECTED; 235 | return true; 236 | } else { 237 | self->_state = self->buffer[3]; 238 | } 239 | } 240 | self->_client->stop(self->_client); 241 | } else { 242 | self->_state = MQTT_CONNECT_FAILED; 243 | } 244 | return false; 245 | } 246 | return true; 247 | } 248 | 249 | static boolean MQTTloopPublic(const void* c) { 250 | PubSubClient* self = (PubSubClient*)c; 251 | 252 | if (MQTTconnectedPublic(self)) { 253 | unsigned long t = HAL_GetTick(); 254 | if ((t - self->lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - self->lastOutActivity > MQTT_KEEPALIVE*1000UL)) { 255 | if (self->pingOutstanding) { 256 | self->_state = MQTT_CONNECTION_TIMEOUT; 257 | self->_client->stop(self->_client); 258 | return false; 259 | } else { 260 | self->buffer[0] = MQTTPINGREQ; 261 | self->buffer[1] = 0; 262 | self->_client->write(self->_client, self->buffer,2); 263 | self->lastOutActivity = t; 264 | self->lastInActivity = t; 265 | self->pingOutstanding = true; 266 | } 267 | } 268 | if (self->_client->available(self->_client)) { 269 | uint8_t llen; 270 | uint16_t len = MQTTreadPacket(self, &llen); 271 | uint16_t msgId = 0; 272 | uint8_t *payload; 273 | if (len > 0) { 274 | self->lastInActivity = t; 275 | uint8_t type = self->buffer[0]&0xF0; 276 | if (type == MQTTPUBLISH) { 277 | if (self->callback) { 278 | uint16_t tl = (self->buffer[llen+1]<<8)+self->buffer[llen+2]; 279 | char topic[tl+1]; 280 | for (uint16_t i=0;ibuffer[llen+3+i]; 282 | } 283 | topic[tl] = 0; 284 | // msgId only present for QOS>0 285 | if ((self->buffer[0]&0x06) == MQTTQOS1) { 286 | msgId = (self->buffer[llen+3+tl]<<8)+self->buffer[llen+3+tl+1]; 287 | payload = self->buffer+llen+3+tl+2; 288 | self->callback(topic,payload,len-llen-3-tl-2); 289 | 290 | self->buffer[0] = MQTTPUBACK; 291 | self->buffer[1] = 2; 292 | self->buffer[2] = (msgId >> 8); 293 | self->buffer[3] = (msgId & 0xFF); 294 | self->_client->write(self->_client, self->buffer,4); 295 | self->lastOutActivity = t; 296 | 297 | } else { 298 | payload = self->buffer+llen+3+tl; 299 | self->callback(topic,payload,len-llen-3-tl); 300 | } 301 | } 302 | } else if (type == MQTTPINGREQ) { 303 | self->buffer[0] = MQTTPINGRESP; 304 | self->buffer[1] = 0; 305 | self->_client->write(self->_client, self->buffer,2); 306 | } else if (type == MQTTPINGRESP) { 307 | self->pingOutstanding = false; 308 | } 309 | } 310 | } 311 | return true; 312 | } 313 | return false; 314 | } 315 | 316 | static boolean MQTTpublishPublic(const void* c, const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) { 317 | PubSubClient* self = (PubSubClient*)c; 318 | 319 | if (MQTTconnectedPublic(self)) { 320 | if (MQTT_MAX_PACKET_SIZE < 5 + 2+strlen(topic) + plength) { 321 | // Too long 322 | return false; 323 | } 324 | // Leave room in the buffer for header and variable length field 325 | uint16_t length = 5; 326 | length = MQTTwriteString(topic,self->buffer,length); 327 | uint16_t i; 328 | for (i=0;ibuffer[length++] = payload[i]; 330 | } 331 | uint8_t header = MQTTPUBLISH; 332 | if (retained) { 333 | header |= 1; 334 | } 335 | return MQTTwrite(self, header,self->buffer,length-5); 336 | } 337 | return false; 338 | } 339 | 340 | 341 | 342 | static boolean MQTTsubscribe(const void* c, const char* topic, uint8_t qos) { 343 | PubSubClient* self = (PubSubClient*)c; 344 | 345 | if (qos < 0 || qos > 1) { 346 | return false; 347 | } 348 | if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { 349 | // Too long 350 | return false; 351 | } 352 | if (MQTTconnectedPublic(self)) { 353 | // Leave room in the buffer for header and variable length field 354 | uint16_t length = 5; 355 | self->nextMsgId++; 356 | if (self->nextMsgId == 0) { 357 | self->nextMsgId = 1; 358 | } 359 | self->buffer[length++] = (self->nextMsgId >> 8); 360 | self->buffer[length++] = (self->nextMsgId & 0xFF); 361 | length = MQTTwriteString((char*)topic, self->buffer,length); 362 | self->buffer[length++] = qos; 363 | return MQTTwrite(self, MQTTSUBSCRIBE|MQTTQOS1, self->buffer, length-5); 364 | } 365 | return false; 366 | } 367 | 368 | static boolean MQTTunsubscribe(const void* c, const char* topic) { 369 | PubSubClient* self = (PubSubClient*)c; 370 | 371 | if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { 372 | // Too long 373 | return false; 374 | } 375 | if (MQTTconnectedPublic(self)) { 376 | uint16_t length = 5; 377 | self->nextMsgId++; 378 | if (self->nextMsgId == 0) { 379 | self->nextMsgId = 1; 380 | } 381 | self->buffer[length++] = (self->nextMsgId >> 8); 382 | self->buffer[length++] = (self->nextMsgId & 0xFF); 383 | length = MQTTwriteString(topic, self->buffer,length); 384 | return MQTTwrite(self, MQTTUNSUBSCRIBE|MQTTQOS1,self->buffer, length-5); 385 | } 386 | return false; 387 | } 388 | 389 | static void MQTTdisconnect(void const* c) { 390 | PubSubClient* self = (PubSubClient*)c; 391 | 392 | self->buffer[0] = MQTTDISCONNECT; 393 | self->buffer[1] = 0; 394 | self->_client->write(self->_client, self->buffer,2); 395 | self->_state = MQTT_DISCONNECTED; 396 | self->_client->stop(self->_client); 397 | self->lastInActivity = self->lastOutActivity = HAL_GetTick(); 398 | } 399 | 400 | static int MQTTstate(const void* c) { 401 | return ((PubSubClient*)c)->_state; 402 | } 403 | 404 | // Constructor 405 | void newPubSubClient(PubSubClient* c, uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client* client) 406 | { 407 | c->connect = MQTTconnectPublic; 408 | c->disconnect = MQTTdisconnect; 409 | c->publish = MQTTpublishPublic; 410 | c->subscribe = MQTTsubscribe; 411 | c->unsubscribe = MQTTunsubscribe; 412 | c->loop = MQTTloopPublic; 413 | c->connected = MQTTconnectedPublic; 414 | c->state = MQTTstate; 415 | 416 | c->ip[0] = ip[0]; c->ip[1] = ip[1]; c->ip[2] = ip[2]; c->ip[3] = ip[3]; 417 | c->port = port; 418 | c->_client = client; 419 | c->callback = callback; 420 | 421 | c->_state = MQTT_DISCONNECTED; 422 | } 423 | -------------------------------------------------------------------------------- /Src/SerialToTCPBridgeProtocol.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SerialToTCPBridgeProtocol.c 3 | * 4 | * Created on: Jul 29, 2016 5 | * Author: Roan 6 | */ 7 | 8 | #include "SerialToTCPBridgeProtocol.h" 9 | 10 | // Private Methods 11 | static void startTX(Client* c, bool timeout, bool kickstart, bool ack) 12 | { 13 | static volatile uint8_t txState = TX_IDLE; 14 | static int packetLength; 15 | 16 | // If we are waiting for an Ack and a timeout occured 17 | // we resend the last publish packet. 18 | if (timeout) 19 | { 20 | if (txState == TX_WAIT) 21 | { 22 | // Ack did not arrive in time, resend last pub 23 | txState = TX_IDLE; 24 | } else 25 | { 26 | return; 27 | } 28 | } 29 | 30 | // If we write to the tx buffer and the uart is idling 31 | // we should initiate the transmit process. 32 | if (kickstart && (txState != TX_IDLE)) 33 | { 34 | return; 35 | } 36 | 37 | // If we received an Ack, move on. 38 | if (ack && (txState == TX_WAIT)) 39 | { 40 | c->txBuf.pH += (uint8_t)packetLength; 41 | c->txBuf.isFull = false; 42 | txState = TX_IDLE; 43 | } 44 | 45 | switch (txState) 46 | { 47 | // Start sending the next packet 48 | case TX_IDLE: 49 | if (c->txBuf.available(&c->txBuf) > 0) 50 | { 51 | packetLength = (int)(c->txBuf.Buf[c->txBuf.pH]) + 1; 52 | if ((int)c->txBuf.pH + packetLength > 256) // check if packet surpasses buffer end 53 | { 54 | if (HAL_UART_Transmit_IT(c->peripheral_UART, &c->txBuf.Buf[c->txBuf.pH], 256 - c->txBuf.pH) != HAL_OK) 55 | return; // TODO: react on this 56 | c->lastOutAct = HAL_GetTick(); 57 | txState = TX_BUSY; 58 | } else 59 | { 60 | if (HAL_UART_Transmit_IT(c->peripheral_UART, &c->txBuf.Buf[c->txBuf.pH], (uint16_t)packetLength) != HAL_OK) 61 | return; 62 | c->lastOutAct = HAL_GetTick(); 63 | if ((c->txBuf.Buf[c->txBuf.pH + 1] & 0x7F) == PROTOCOL_PUBLISH) 64 | { 65 | c->expectedAckSeq = (c->txBuf.Buf[c->txBuf.pH + 1] & 0x80) > 0; 66 | c->ackOutstanding = true; 67 | txState = TX_WAIT; 68 | } else 69 | { 70 | c->txBuf.pH += (uint8_t)packetLength; 71 | c->txBuf.isFull = false; 72 | } 73 | } 74 | } 75 | break; 76 | // Complete sending of current packet 77 | case TX_BUSY: 78 | if (HAL_UART_Transmit_IT(c->peripheral_UART, c->txBuf.Buf, packetLength + c->txBuf.pH - 256) != HAL_OK) 79 | return; 80 | c->lastOutAct = HAL_GetTick(); 81 | if ((c->txBuf.Buf[c->txBuf.pH + 1] & 0x7F) == PROTOCOL_PUBLISH) 82 | { 83 | c->expectedAckSeq = (c->txBuf.Buf[c->txBuf.pH + 1] & 0x80) > 0; 84 | c->ackOutstanding = true; 85 | txState = TX_WAIT; 86 | } else 87 | { 88 | c->txBuf.pH += (uint8_t)packetLength; 89 | txState = TX_IDLE; 90 | c->txBuf.isFull = false; 91 | } 92 | break; 93 | case TX_WAIT: 94 | break; 95 | } 96 | } 97 | 98 | static bool writePacket(Client* c, uint8_t command, uint8_t* payload, uint8_t pLength) 99 | { 100 | c->workBuffer[0] = pLength + 5; 101 | c->workBuffer[1] = command; 102 | if (payload != NULL) 103 | { 104 | for (uint8_t i = 2; i < pLength + 2; i++) 105 | { 106 | c->workBuffer[i] = payload[i - 2]; 107 | } 108 | } 109 | uint32_t crcCode = HAL_CRC_Calculate(c->peripheral_CRC, (uint32_t*)(c->workBuffer), pLength + 2); 110 | crcCode ^= 0xffffffff; 111 | c->workBuffer[pLength + 2] = crcCode & 0x000000FF; 112 | c->workBuffer[pLength + 3] = (crcCode & 0x0000FF00) >> 8; 113 | c->workBuffer[pLength + 4] = (crcCode & 0x00FF0000) >> 16; 114 | c->workBuffer[pLength + 5] = (crcCode & 0xFF000000) >> 24; 115 | 116 | // see if packet will fit in transmit buffer 117 | if ((int)(pLength) + 6 > 256 - c->txBuf.available(&c->txBuf)) 118 | { 119 | return false; 120 | } 121 | 122 | // write packet into tx buffer 123 | for (int i = 0; i < pLength + 6; i++) 124 | { 125 | c->txBuf.Buf[c->txBuf.pT++] = c->workBuffer[i]; 126 | } 127 | c->txBuf.isFull = (c->txBuf.pT == c->txBuf.pH); 128 | 129 | startTX(c, false, true, false); 130 | 131 | return true; 132 | } 133 | 134 | static size_t publish(Client* c, uint8_t* payload, uint8_t pLength) 135 | { 136 | static bool pubSequence = false; 137 | 138 | uint8_t cmd = PROTOCOL_PUBLISH; 139 | if (pubSequence) 140 | { 141 | cmd |= 0x80; 142 | } 143 | pubSequence = !pubSequence; 144 | 145 | if (!writePacket(c, cmd, payload, pLength)) 146 | { 147 | return 0; 148 | } 149 | return pLength; 150 | } 151 | 152 | static void rxHandlePacket(Client* c, uint8_t* packetStart) 153 | { 154 | bool rxSeqFlag = (packetStart[1] & 0x80) > 0; 155 | switch (packetStart[1] & 0x7F) 156 | { 157 | // Connection established with destination 158 | case PROTOCOL_CONNACK: 159 | if (packetStart[0] == 5) 160 | { 161 | c->state = STATE_CONNECTED; 162 | } 163 | break; 164 | // Incoming data 165 | case PROTOCOL_PUBLISH: 166 | writePacket(c, PROTOCOL_ACK | (packetStart[1] & 0x80), NULL, 0); 167 | if (rxSeqFlag == c->expectedRxSeqFlag) 168 | { 169 | c->expectedRxSeqFlag = !c->expectedRxSeqFlag; 170 | if (packetStart[0] > 5) 171 | { 172 | for (uint8_t i = 0; i < packetStart[0] - 5; i++) 173 | { 174 | c->readBuf.Buf[c->readBuf.pT++] = packetStart[2 + i]; 175 | } 176 | c->readBuf.isFull = (c->readBuf.pH == c->readBuf.pT); 177 | } 178 | } 179 | break; 180 | // Protocol Acknowledge 181 | case PROTOCOL_ACK: 182 | if (c->ackOutstanding) 183 | { 184 | if (rxSeqFlag == c->expectedAckSeq) 185 | { 186 | c->ackOutstanding = false; 187 | startTX(c, false, false, true); 188 | } 189 | } 190 | break; 191 | } 192 | } 193 | 194 | // Callback hook ups 195 | void uartTxCompleteCallback(Client* c) 196 | { 197 | startTX(c, false, false, false); 198 | } 199 | 200 | // TODO: Packet RX timeout, buffer full check? 201 | void uartRxCompleteCallback(Client* c) 202 | { 203 | static uint8_t packetCount = 0; 204 | static uint8_t rxState = RX_PACKET_IDLE; 205 | 206 | c->rxBuffer[packetCount++] = c->rxByte; 207 | switch (rxState) 208 | { 209 | case RX_PACKET_IDLE: 210 | rxState = RX_PACKET_GOTLENGTH; 211 | break; 212 | case RX_PACKET_GOTLENGTH: 213 | rxState = RX_PACKET_GOTCOMMAND; 214 | break; 215 | case RX_PACKET_GOTCOMMAND: 216 | ; // has to be here, otherwise 'deceleration after label' error 217 | uint8_t packetLength = c->rxBuffer[0]; 218 | if (packetCount == packetLength + 1) // Got rest of packet 219 | { 220 | packetCount = 0; 221 | // Integrity checking 222 | uint32_t crcRx = c->rxBuffer[packetLength - 3] | (c->rxBuffer[packetLength - 2] << 8) 223 | | (c->rxBuffer[packetLength - 1] << 16) | (c->rxBuffer[packetLength] << 24); 224 | uint32_t crcCode = HAL_CRC_Calculate(c->peripheral_CRC, (uint32_t*)(c->rxBuffer), packetLength - 3); 225 | crcCode ^= 0xffffffff; 226 | if (crcRx == crcCode) // validate packet 227 | { 228 | rxHandlePacket(c, c->rxBuffer); 229 | } 230 | rxState = RX_PACKET_IDLE; 231 | } 232 | break; 233 | } 234 | HAL_UART_Receive_IT(c->peripheral_UART, &c->rxByte, 1); 235 | } 236 | 237 | void tickInterupt(Client* c) 238 | { 239 | // If we are waiting for an Ack, 240 | // we need to resend the last publish after timeout 241 | // TODO: Need to change this to a one time pulse using timer, achieves 1 asynchronous callback. 242 | if (c->ackOutstanding) 243 | { 244 | uint32_t now = HAL_GetTick(); 245 | if (now - c->lastOutAct > 500) 246 | { 247 | startTX(c, true, false, false); 248 | } 249 | } 250 | } 251 | 252 | // Public Methods 253 | static int availablePublic(const void* c) 254 | { 255 | Client* self = (Client*)c; 256 | 257 | return self->readBuf.available(&self->readBuf); 258 | } 259 | 260 | static int readPublic(const void* c) 261 | { 262 | Client* self = (Client*)c; 263 | 264 | if (!self->available(c)) 265 | { 266 | return -1; 267 | } 268 | uint8_t ch = self->readBuf.Buf[self->readBuf.pH++]; 269 | self->readBuf.isFull = false; 270 | return ch; 271 | } 272 | 273 | static int connectPublic(const void* c, uint8_t ip[4], uint16_t port) 274 | { 275 | Client* self = (Client*)c; 276 | HAL_UART_Receive_IT(self->peripheral_UART, &self->rxByte, 1); // start rx on hardware uart interface 277 | uint8_t destination[6] = { ip[0], ip[1], ip[2], ip[3], (uint8_t)port, (uint8_t)(port >> 8) }; 278 | writePacket(self, PROTOCOL_CONNECT, destination, 6); 279 | while (self->state != STATE_CONNECTED) 280 | { 281 | uint32_t now = HAL_GetTick(); 282 | if (now - self->lastInAct >= 5000) 283 | { 284 | return -1; 285 | } 286 | } 287 | self->lastInAct = HAL_GetTick(); 288 | return 1; 289 | /* SUCCESS 1 290 | TIMED_OUT -1 291 | INVALID_SERVER -2 292 | TRUNCATED -3 293 | INVALID_RESPONSE -4 294 | */ 295 | } 296 | 297 | static uint8_t connectedPublic(const void* c) 298 | { 299 | Client* self = (Client*)c; 300 | 301 | if (self->state == STATE_CONNECTED) 302 | { 303 | return 1; 304 | } 305 | return 0; 306 | } 307 | 308 | static void flushPublic(const void* c) 309 | { 310 | ; 311 | } 312 | 313 | static void stopPublic(const void* c) 314 | { 315 | ; 316 | } 317 | 318 | static size_t writePublic(const void* c, uint8_t* payload, uint8_t pLength) 319 | { 320 | Client* self = (Client*)c; 321 | 322 | return publish(self, payload, pLength); 323 | } 324 | 325 | // Constructor 326 | void newClient(Client* c, UART_HandleTypeDef* uartUnit, CRC_HandleTypeDef* crcUnit) 327 | { 328 | c->peripheral_UART = uartUnit; 329 | c->peripheral_CRC = crcUnit; 330 | 331 | newByteBuffer(&c->txBuf); 332 | newByteBuffer(&c->readBuf); 333 | c->ackOutstanding = false; 334 | c->expectedAckSeq = false; 335 | c->expectedRxSeqFlag = false; 336 | 337 | c->state = STATE_DISCONNECTED; 338 | 339 | // Arduino Client interface API 340 | c->connect = connectPublic; 341 | c->connected = connectedPublic; 342 | c->available = availablePublic; 343 | c->read = readPublic; 344 | c->write = writePublic; 345 | c->flush = flushPublic; 346 | c->stop = stopPublic; 347 | } 348 | -------------------------------------------------------------------------------- /Src/crc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : CRC.c 4 | * Description : This file provides code for the configuration 5 | * of the CRC instances. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Includes ------------------------------------------------------------------*/ 36 | #include "crc.h" 37 | 38 | /* USER CODE BEGIN 0 */ 39 | 40 | /* USER CODE END 0 */ 41 | 42 | CRC_HandleTypeDef hcrc; 43 | 44 | /* CRC init function */ 45 | void MX_CRC_Init(void) 46 | { 47 | 48 | hcrc.Instance = CRC; 49 | hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; 50 | hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; 51 | hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_BYTE; 52 | hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_ENABLE; 53 | hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; 54 | if (HAL_CRC_Init(&hcrc) != HAL_OK) 55 | { 56 | Error_Handler(); 57 | } 58 | 59 | } 60 | 61 | void HAL_CRC_MspInit(CRC_HandleTypeDef* crcHandle) 62 | { 63 | 64 | if(crcHandle->Instance==CRC) 65 | { 66 | /* USER CODE BEGIN CRC_MspInit 0 */ 67 | 68 | /* USER CODE END CRC_MspInit 0 */ 69 | /* Peripheral clock enable */ 70 | __HAL_RCC_CRC_CLK_ENABLE(); 71 | /* USER CODE BEGIN CRC_MspInit 1 */ 72 | 73 | /* USER CODE END CRC_MspInit 1 */ 74 | } 75 | } 76 | 77 | void HAL_CRC_MspDeInit(CRC_HandleTypeDef* crcHandle) 78 | { 79 | 80 | if(crcHandle->Instance==CRC) 81 | { 82 | /* USER CODE BEGIN CRC_MspDeInit 0 */ 83 | 84 | /* USER CODE END CRC_MspDeInit 0 */ 85 | /* Peripheral clock disable */ 86 | __HAL_RCC_CRC_CLK_DISABLE(); 87 | } 88 | /* USER CODE BEGIN CRC_MspDeInit 1 */ 89 | 90 | /* USER CODE END CRC_MspDeInit 1 */ 91 | } 92 | 93 | /* USER CODE BEGIN 1 */ 94 | 95 | /* USER CODE END 1 */ 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | /** 102 | * @} 103 | */ 104 | 105 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 106 | -------------------------------------------------------------------------------- /Src/gpio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : gpio.c 4 | * Description : This file provides code for the configuration 5 | * of all used GPIO pins. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Includes ------------------------------------------------------------------*/ 36 | #include "gpio.h" 37 | /* USER CODE BEGIN 0 */ 38 | 39 | /* USER CODE END 0 */ 40 | 41 | /*----------------------------------------------------------------------------*/ 42 | /* Configure GPIO */ 43 | /*----------------------------------------------------------------------------*/ 44 | /* USER CODE BEGIN 1 */ 45 | 46 | /* USER CODE END 1 */ 47 | 48 | /** Configure pins as 49 | * Analog 50 | * Input 51 | * Output 52 | * EVENT_OUT 53 | * EXTI 54 | */ 55 | void MX_GPIO_Init(void) 56 | { 57 | 58 | GPIO_InitTypeDef GPIO_InitStruct; 59 | 60 | /* GPIO Ports Clock Enable */ 61 | __HAL_RCC_GPIOC_CLK_ENABLE(); 62 | __HAL_RCC_GPIOF_CLK_ENABLE(); 63 | __HAL_RCC_GPIOA_CLK_ENABLE(); 64 | 65 | /*Configure GPIO pin : PC13 */ 66 | GPIO_InitStruct.Pin = GPIO_PIN_13; 67 | GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 68 | GPIO_InitStruct.Pull = GPIO_NOPULL; 69 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 70 | 71 | /*Configure GPIO pin : PA5 */ 72 | GPIO_InitStruct.Pin = GPIO_PIN_5; 73 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 74 | GPIO_InitStruct.Pull = GPIO_NOPULL; 75 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 76 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 77 | 78 | /*Configure GPIO pin Output Level */ 79 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); 80 | 81 | /* EXTI interrupt init*/ 82 | HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0); 83 | HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 84 | 85 | } 86 | 87 | /* USER CODE BEGIN 2 */ 88 | 89 | /* USER CODE END 2 */ 90 | 91 | /** 92 | * @} 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | 99 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 100 | -------------------------------------------------------------------------------- /Src/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : main.c 4 | * Description : Main program body 5 | ****************************************************************************** 6 | * 7 | * COPYRIGHT(c) 2016 STMicroelectronics 8 | * 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | ****************************************************************************** 32 | */ 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "stm32f3xx_hal.h" 35 | #include "crc.h" 36 | #include "usart.h" 37 | #include "gpio.h" 38 | 39 | /* USER CODE BEGIN Includes */ 40 | #include 41 | #include "PubSubClient.h" 42 | /* USER CODE END Includes */ 43 | 44 | /* Private variables ---------------------------------------------------------*/ 45 | 46 | /* USER CODE BEGIN PV */ 47 | /* Private variables ---------------------------------------------------------*/ 48 | Client connection; 49 | PubSubClient mqttConnection; 50 | bool buttonPressed; 51 | /* USER CODE END PV */ 52 | 53 | /* Private function prototypes -----------------------------------------------*/ 54 | void SystemClock_Config(void); 55 | void Error_Handler(void); 56 | 57 | /* USER CODE BEGIN PFP */ 58 | /* Private function prototypes -----------------------------------------------*/ 59 | 60 | /* USER CODE END PFP */ 61 | 62 | /* USER CODE BEGIN 0 */ 63 | 64 | // Serial Protocol specific 65 | void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 66 | { 67 | uartTxCompleteCallback(&connection); 68 | } 69 | 70 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 71 | { 72 | uartRxCompleteCallback(&connection); 73 | } 74 | 75 | void HAL_SYSTICK_Callback(void) 76 | { 77 | tickInterupt(&connection); 78 | } 79 | 80 | // Application Example specific 81 | void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 82 | { 83 | static uint32_t debounce; 84 | if (GPIO_Pin == GPIO_PIN_13) 85 | { 86 | uint32_t now = HAL_GetTick(); 87 | if (now - debounce > 200) 88 | { 89 | buttonPressed = true; 90 | debounce = now; 91 | } 92 | } 93 | } 94 | 95 | void MQTTCallbek(char* topic, uint8_t* payload, unsigned int length) 96 | { 97 | if (length == 1) 98 | { 99 | // DEBUG LED 100 | if (payload[0] == 0x31) 101 | { 102 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); 103 | } else if (payload[0] == 0x32) 104 | { 105 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); 106 | } 107 | } 108 | } 109 | /* USER CODE END 0 */ 110 | 111 | int main(void) 112 | { 113 | 114 | /* USER CODE BEGIN 1 */ 115 | 116 | /* USER CODE END 1 */ 117 | 118 | /* MCU Configuration----------------------------------------------------------*/ 119 | 120 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 121 | HAL_Init(); 122 | 123 | /* Configure the system clock */ 124 | SystemClock_Config(); 125 | 126 | /* Initialize all configured peripherals */ 127 | MX_GPIO_Init(); 128 | MX_CRC_Init(); 129 | MX_USART2_UART_Init(); 130 | 131 | /* USER CODE BEGIN 2 */ 132 | /**************************** 133 | * User Application Example * 134 | ****************************/ 135 | 136 | // MQTT Connection details 137 | const char* id = "stm32BABY"; 138 | const char* user = NULL; 139 | const char* pass = NULL; 140 | const char* willTopic = NULL; 141 | const char* willMsg = NULL; 142 | 143 | // MQTT Message info 144 | const char* publishTopic = "stm32f334"; 145 | const char* subscribeTopic = "led/#"; 146 | const char* publishMsg = "Hello i5! I am a STM32F3!\n"; 147 | 148 | newClient(&connection, &huart2, &hcrc); 149 | uint8_t address[4] = {127, 0, 0, 1}; // MQTT Broker on host PC on port 1883 150 | newPubSubClient(&mqttConnection, address, 1883, MQTTCallbek, &connection); 151 | bool connectedAfter = false; 152 | bool subscribeOnce = false; 153 | /* USER CODE END 2 */ 154 | 155 | /* Infinite loop */ 156 | /* USER CODE BEGIN WHILE */ 157 | HAL_Delay(4000); // Wait 4s for power stabilization and usb enumeration. 158 | while (1) 159 | { 160 | /* USER CODE END WHILE */ 161 | 162 | /* USER CODE BEGIN 3 */ 163 | if (!connectedAfter) 164 | { 165 | connectedAfter = true; 166 | mqttConnection.connect(&mqttConnection, id, user, pass, willTopic, 0, false, willMsg); 167 | } 168 | if (buttonPressed) 169 | { 170 | buttonPressed = false; 171 | if (!subscribeOnce) 172 | { 173 | subscribeOnce = true; 174 | mqttConnection.subscribe(&mqttConnection, subscribeTopic, 0); 175 | } 176 | mqttConnection.publish(&mqttConnection, publishTopic, (const uint8_t*)publishMsg, strlen(publishMsg), false); 177 | } 178 | mqttConnection.loop(&mqttConnection); 179 | } 180 | /* USER CODE END 3 */ 181 | 182 | } 183 | 184 | /** System Clock Configuration 185 | */ 186 | void SystemClock_Config(void) 187 | { 188 | 189 | RCC_OscInitTypeDef RCC_OscInitStruct; 190 | RCC_ClkInitTypeDef RCC_ClkInitStruct; 191 | 192 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 193 | RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; 194 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; 195 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 196 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 197 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 198 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 199 | { 200 | Error_Handler(); 201 | } 202 | 203 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 204 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 205 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 206 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 207 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 208 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 209 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 210 | { 211 | Error_Handler(); 212 | } 213 | 214 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 215 | 216 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 217 | 218 | /* SysTick_IRQn interrupt configuration */ 219 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 220 | } 221 | 222 | /* USER CODE BEGIN 4 */ 223 | 224 | /* USER CODE END 4 */ 225 | 226 | /** 227 | * @brief This function is executed in case of error occurrence. 228 | * @param None 229 | * @retval None 230 | */ 231 | void Error_Handler(void) 232 | { 233 | /* USER CODE BEGIN Error_Handler */ 234 | /* User can add his own implementation to report the HAL error return state */ 235 | while(1) 236 | { 237 | } 238 | /* USER CODE END Error_Handler */ 239 | } 240 | 241 | #ifdef USE_FULL_ASSERT 242 | 243 | /** 244 | * @brief Reports the name of the source file and the source line number 245 | * where the assert_param error has occurred. 246 | * @param file: pointer to the source file name 247 | * @param line: assert_param error line source number 248 | * @retval None 249 | */ 250 | void assert_failed(uint8_t* file, uint32_t line) 251 | { 252 | /* USER CODE BEGIN 6 */ 253 | /* User can add his own implementation to report the file name and line number, 254 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 255 | /* USER CODE END 6 */ 256 | 257 | } 258 | 259 | #endif 260 | 261 | /** 262 | * @} 263 | */ 264 | 265 | /** 266 | * @} 267 | */ 268 | 269 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 270 | -------------------------------------------------------------------------------- /Src/stm32f3xx_hal_msp.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : stm32f3xx_hal_msp.c 4 | * Description : This file provides code for the MSP Initialization 5 | * and de-Initialization codes. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | /* Includes ------------------------------------------------------------------*/ 35 | #include "stm32f3xx_hal.h" 36 | 37 | extern void Error_Handler(void); 38 | /* USER CODE BEGIN 0 */ 39 | 40 | /* USER CODE END 0 */ 41 | 42 | /** 43 | * Initializes the Global MSP. 44 | */ 45 | void HAL_MspInit(void) 46 | { 47 | /* USER CODE BEGIN MspInit 0 */ 48 | 49 | /* USER CODE END MspInit 0 */ 50 | 51 | __HAL_RCC_SYSCFG_CLK_ENABLE(); 52 | 53 | HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); 54 | 55 | /* System interrupt init*/ 56 | /* MemoryManagement_IRQn interrupt configuration */ 57 | HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0); 58 | /* BusFault_IRQn interrupt configuration */ 59 | HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0); 60 | /* UsageFault_IRQn interrupt configuration */ 61 | HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0); 62 | /* SVCall_IRQn interrupt configuration */ 63 | HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0); 64 | /* DebugMonitor_IRQn interrupt configuration */ 65 | HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0); 66 | /* PendSV_IRQn interrupt configuration */ 67 | HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0); 68 | /* SysTick_IRQn interrupt configuration */ 69 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 70 | 71 | /* USER CODE BEGIN MspInit 1 */ 72 | 73 | /* USER CODE END MspInit 1 */ 74 | } 75 | 76 | /* USER CODE BEGIN 1 */ 77 | 78 | /* USER CODE END 1 */ 79 | 80 | /** 81 | * @} 82 | */ 83 | 84 | /** 85 | * @} 86 | */ 87 | 88 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 89 | -------------------------------------------------------------------------------- /Src/stm32f3xx_it.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f3xx_it.c 4 | * @brief Interrupt Service Routines. 5 | ****************************************************************************** 6 | * 7 | * COPYRIGHT(c) 2016 STMicroelectronics 8 | * 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | ****************************************************************************** 32 | */ 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "stm32f3xx_hal.h" 35 | #include "stm32f3xx.h" 36 | #include "stm32f3xx_it.h" 37 | 38 | /* USER CODE BEGIN 0 */ 39 | 40 | /* USER CODE END 0 */ 41 | 42 | /* External variables --------------------------------------------------------*/ 43 | extern UART_HandleTypeDef huart2; 44 | 45 | /******************************************************************************/ 46 | /* Cortex-M4 Processor Interruption and Exception Handlers */ 47 | /******************************************************************************/ 48 | 49 | /** 50 | * @brief This function handles Non maskable interrupt. 51 | */ 52 | void NMI_Handler(void) 53 | { 54 | /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ 55 | 56 | /* USER CODE END NonMaskableInt_IRQn 0 */ 57 | /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ 58 | 59 | /* USER CODE END NonMaskableInt_IRQn 1 */ 60 | } 61 | 62 | /** 63 | * @brief This function handles Hard fault interrupt. 64 | */ 65 | void HardFault_Handler(void) 66 | { 67 | /* USER CODE BEGIN HardFault_IRQn 0 */ 68 | 69 | /* USER CODE END HardFault_IRQn 0 */ 70 | while (1) 71 | { 72 | } 73 | /* USER CODE BEGIN HardFault_IRQn 1 */ 74 | 75 | /* USER CODE END HardFault_IRQn 1 */ 76 | } 77 | 78 | /** 79 | * @brief This function handles Memory management fault. 80 | */ 81 | void MemManage_Handler(void) 82 | { 83 | /* USER CODE BEGIN MemoryManagement_IRQn 0 */ 84 | 85 | /* USER CODE END MemoryManagement_IRQn 0 */ 86 | while (1) 87 | { 88 | } 89 | /* USER CODE BEGIN MemoryManagement_IRQn 1 */ 90 | 91 | /* USER CODE END MemoryManagement_IRQn 1 */ 92 | } 93 | 94 | /** 95 | * @brief This function handles Pre-fetch fault, memory access fault. 96 | */ 97 | void BusFault_Handler(void) 98 | { 99 | /* USER CODE BEGIN BusFault_IRQn 0 */ 100 | 101 | /* USER CODE END BusFault_IRQn 0 */ 102 | while (1) 103 | { 104 | } 105 | /* USER CODE BEGIN BusFault_IRQn 1 */ 106 | 107 | /* USER CODE END BusFault_IRQn 1 */ 108 | } 109 | 110 | /** 111 | * @brief This function handles Undefined instruction or illegal state. 112 | */ 113 | void UsageFault_Handler(void) 114 | { 115 | /* USER CODE BEGIN UsageFault_IRQn 0 */ 116 | 117 | /* USER CODE END UsageFault_IRQn 0 */ 118 | while (1) 119 | { 120 | } 121 | /* USER CODE BEGIN UsageFault_IRQn 1 */ 122 | 123 | /* USER CODE END UsageFault_IRQn 1 */ 124 | } 125 | 126 | /** 127 | * @brief This function handles System service call via SWI instruction. 128 | */ 129 | void SVC_Handler(void) 130 | { 131 | /* USER CODE BEGIN SVCall_IRQn 0 */ 132 | 133 | /* USER CODE END SVCall_IRQn 0 */ 134 | /* USER CODE BEGIN SVCall_IRQn 1 */ 135 | 136 | /* USER CODE END SVCall_IRQn 1 */ 137 | } 138 | 139 | /** 140 | * @brief This function handles Debug monitor. 141 | */ 142 | void DebugMon_Handler(void) 143 | { 144 | /* USER CODE BEGIN DebugMonitor_IRQn 0 */ 145 | 146 | /* USER CODE END DebugMonitor_IRQn 0 */ 147 | /* USER CODE BEGIN DebugMonitor_IRQn 1 */ 148 | 149 | /* USER CODE END DebugMonitor_IRQn 1 */ 150 | } 151 | 152 | /** 153 | * @brief This function handles Pendable request for system service. 154 | */ 155 | void PendSV_Handler(void) 156 | { 157 | /* USER CODE BEGIN PendSV_IRQn 0 */ 158 | 159 | /* USER CODE END PendSV_IRQn 0 */ 160 | /* USER CODE BEGIN PendSV_IRQn 1 */ 161 | 162 | /* USER CODE END PendSV_IRQn 1 */ 163 | } 164 | 165 | /** 166 | * @brief This function handles System tick timer. 167 | */ 168 | void SysTick_Handler(void) 169 | { 170 | /* USER CODE BEGIN SysTick_IRQn 0 */ 171 | 172 | /* USER CODE END SysTick_IRQn 0 */ 173 | HAL_IncTick(); 174 | HAL_SYSTICK_IRQHandler(); 175 | /* USER CODE BEGIN SysTick_IRQn 1 */ 176 | 177 | /* USER CODE END SysTick_IRQn 1 */ 178 | } 179 | 180 | /******************************************************************************/ 181 | /* STM32F3xx Peripheral Interrupt Handlers */ 182 | /* Add here the Interrupt Handlers for the used peripherals. */ 183 | /* For the available peripheral interrupt handler names, */ 184 | /* please refer to the startup file (startup_stm32f3xx.s). */ 185 | /******************************************************************************/ 186 | 187 | /** 188 | * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXT line 26. 189 | */ 190 | void USART2_IRQHandler(void) 191 | { 192 | /* USER CODE BEGIN USART2_IRQn 0 */ 193 | 194 | /* USER CODE END USART2_IRQn 0 */ 195 | HAL_UART_IRQHandler(&huart2); 196 | /* USER CODE BEGIN USART2_IRQn 1 */ 197 | 198 | /* USER CODE END USART2_IRQn 1 */ 199 | } 200 | 201 | /** 202 | * @brief This function handles EXTI line[15:10] interrupts. 203 | */ 204 | void EXTI15_10_IRQHandler(void) 205 | { 206 | /* USER CODE BEGIN EXTI15_10_IRQn 0 */ 207 | 208 | /* USER CODE END EXTI15_10_IRQn 0 */ 209 | HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); 210 | /* USER CODE BEGIN EXTI15_10_IRQn 1 */ 211 | 212 | /* USER CODE END EXTI15_10_IRQn 1 */ 213 | } 214 | 215 | /* USER CODE BEGIN 1 */ 216 | 217 | /* USER CODE END 1 */ 218 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 219 | -------------------------------------------------------------------------------- /Src/usart.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : USART.c 4 | * Description : This file provides code for the configuration 5 | * of the USART instances. 6 | ****************************************************************************** 7 | * 8 | * COPYRIGHT(c) 2016 STMicroelectronics 9 | * 10 | * Redistribution and use in source and binary forms, with or without modification, 11 | * are permitted provided that the following conditions are met: 12 | * 1. Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Includes ------------------------------------------------------------------*/ 36 | #include "usart.h" 37 | 38 | #include "gpio.h" 39 | 40 | /* USER CODE BEGIN 0 */ 41 | 42 | /* USER CODE END 0 */ 43 | 44 | UART_HandleTypeDef huart2; 45 | 46 | /* USART2 init function */ 47 | 48 | void MX_USART2_UART_Init(void) 49 | { 50 | 51 | huart2.Instance = USART2; 52 | huart2.Init.BaudRate = 115200; 53 | huart2.Init.WordLength = UART_WORDLENGTH_8B; 54 | huart2.Init.StopBits = UART_STOPBITS_1; 55 | huart2.Init.Parity = UART_PARITY_NONE; 56 | huart2.Init.Mode = UART_MODE_TX_RX; 57 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 58 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; 59 | huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; 60 | huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 61 | if (HAL_UART_Init(&huart2) != HAL_OK) 62 | { 63 | Error_Handler(); 64 | } 65 | 66 | } 67 | 68 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) 69 | { 70 | 71 | GPIO_InitTypeDef GPIO_InitStruct; 72 | if(uartHandle->Instance==USART2) 73 | { 74 | /* USER CODE BEGIN USART2_MspInit 0 */ 75 | 76 | /* USER CODE END USART2_MspInit 0 */ 77 | /* Peripheral clock enable */ 78 | __HAL_RCC_USART2_CLK_ENABLE(); 79 | 80 | /**USART2 GPIO Configuration 81 | PA2 ------> USART2_TX 82 | PA3 ------> USART2_RX 83 | */ 84 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; 85 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 86 | GPIO_InitStruct.Pull = GPIO_PULLUP; 87 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 88 | GPIO_InitStruct.Alternate = GPIO_AF7_USART2; 89 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 90 | 91 | /* Peripheral interrupt init */ 92 | HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); 93 | HAL_NVIC_EnableIRQ(USART2_IRQn); 94 | /* USER CODE BEGIN USART2_MspInit 1 */ 95 | 96 | /* USER CODE END USART2_MspInit 1 */ 97 | } 98 | } 99 | 100 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) 101 | { 102 | 103 | if(uartHandle->Instance==USART2) 104 | { 105 | /* USER CODE BEGIN USART2_MspDeInit 0 */ 106 | 107 | /* USER CODE END USART2_MspDeInit 0 */ 108 | /* Peripheral clock disable */ 109 | __HAL_RCC_USART2_CLK_DISABLE(); 110 | 111 | /**USART2 GPIO Configuration 112 | PA2 ------> USART2_TX 113 | PA3 ------> USART2_RX 114 | */ 115 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); 116 | 117 | /* Peripheral interrupt Deinit*/ 118 | HAL_NVIC_DisableIRQ(USART2_IRQn); 119 | 120 | } 121 | /* USER CODE BEGIN USART2_MspDeInit 1 */ 122 | 123 | /* USER CODE END USART2_MspDeInit 1 */ 124 | } 125 | 126 | /* USER CODE BEGIN 1 */ 127 | 128 | /* USER CODE END 1 */ 129 | 130 | /** 131 | * @} 132 | */ 133 | 134 | /** 135 | * @} 136 | */ 137 | 138 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 139 | --------------------------------------------------------------------------------