UDP send and receive

This commit is contained in:
2023-03-17 15:25:33 -03:00
parent 2cebf353fa
commit 7e69147bc4
16 changed files with 357 additions and 29 deletions

5
Code/ESP8266_UDP_Telemetry/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View File

@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View File

@@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View File

@@ -0,0 +1,18 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
upload_port = COM9
upload_speed = 921600
monitor_port = COM9
monitor_speed = 921600

View File

@@ -0,0 +1,47 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define TX_BUFFER_SIZE 4096
#define RX_BUFFER_SIZE 4096
WiFiUDP udp;
const IPAddress multicastIP(239, 0, 0, 1);
const uint16_t remotePort = 10001;
uint8_t txBuffer[TX_BUFFER_SIZE];
uint8_t rxBuffer[RX_BUFFER_SIZE];
void setup() {
// put your setup code here, to run once:
pinMode(1, OUTPUT);
pinMode(LED_BUILTIN_AUX, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(921600);
Serial.setTimeout(1);
WiFi.begin("RoboIME-HRR", "roboimehrr");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
Serial.println(udp.beginMulticast(WiFi.localIP(), multicastIP, 11001));
}
void loop() {
// put your main code here, to run repeatedly:
int packetSize = udp.parsePacket();
if (packetSize) {
size_t receivedBytes = udp.readBytes(rxBuffer, packetSize);
Serial.write(rxBuffer, receivedBytes);
}
if(Serial.available()){
size_t bytesToSend = Serial.readBytes(txBuffer, TX_BUFFER_SIZE);
udp.beginPacketMulticast(multicastIP, remotePort, WiFi.localIP());
udp.write(txBuffer, bytesToSend);
udp.endPacket();
}
}

View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html

View File

@@ -55,6 +55,8 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel4_IRQHandler(void);
void DMA1_Channel5_IRQHandler(void);
void DMA1_Channel7_IRQHandler(void);
void TIM4_IRQHandler(void);
void USART1_IRQHandler(void);

View File

@@ -0,0 +1,31 @@
/*
* BTS7960B.cpp
*
* Created on: Dec 29, 2021
* Author: Gabriel
*/
#include "BTS7960B.hpp"
BTS7960B::BTS7960B(__IO uint32_t* ina_ccr, __IO uint32_t* inb_ccr, GPIO_TypeDef* inha_gpio_port, uint16_t inha_gpio_pin, GPIO_TypeDef* inhb_gpio_port, uint16_t inhb_gpio_pin)
: ina(ina_ccr), inb(inb_ccr), inha_port(inha_gpio_port), inha_pin(inha_gpio_pin), inhb_port(inhb_gpio_port), inhb_pin(inhb_gpio_pin){
}
void BTS7960B::setSpeed(int32_t speed){
if(speed > 0){
HAL_GPIO_WritePin(inha_port, inha_pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(inhb_port, inhb_pin, GPIO_PIN_SET);
*ina = speed;
*inb = 0;
}else if(speed < 0){
HAL_GPIO_WritePin(inha_port, inha_pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(inhb_port, inhb_pin, GPIO_PIN_SET);
*ina = 0;
*inb = -speed;
}else{
HAL_GPIO_WritePin(inha_port, inha_pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(inhb_port, inhb_pin, GPIO_PIN_RESET);
}
}

View File

@@ -0,0 +1,32 @@
/*
* BTS7960B.hpp
*
* Created on: Dec 29, 2021
* Author: Gabriel
*/
#ifndef SRC_COMPONENTS_BTS7960B_HPP_
#define SRC_COMPONENTS_BTS7960B_HPP_
#include "main.h"
class BTS7960B {
public:
BTS7960B(__IO uint32_t* ina_ccr, __IO uint32_t* inb_ccr, GPIO_TypeDef* inha_gpio_port, uint16_t inha_gpio_pin, GPIO_TypeDef* inhb_gpio_port, uint16_t inhb_gpio_pin);
void setSpeed(int32_t speed);
private:
__IO uint32_t* ina;
__IO uint32_t* inb;
GPIO_TypeDef* inha_port;
uint16_t inha_pin;
GPIO_TypeDef* inhb_port;
uint16_t inhb_pin;
};
#endif /* SRC_COMPONENTS_BTS7960B_HPP_ */

View File

@@ -12,12 +12,12 @@ extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
SerialDebug debug(&huart2, 32);
ESP8266 esp0(&huart1);
//ESP8266 esp0(&huart1);
BTS7960B motor0(&(TIM2->CCR1), &(TIM2->CCR2), GPIOB, GPIO_PIN_4, GPIOB, GPIO_PIN_5);
BTS7960B motor1(&(TIM2->CCR3), &(TIM2->CCR4), GPIOB, GPIO_PIN_0, GPIOB, GPIO_PIN_1);
//Temporary variables begin
char buf[64];
uint8_t buf[1500];
float error = 0;
float lastError = 0;
float derror = 0;
@@ -44,6 +44,11 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
HAL_GPIO_TogglePin(LED_BUILTIN_GPIO_Port, LED_BUILTIN_Pin);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, buf, 256);
}
void pid(int32_t input){
lastError = error;
error = input - (int32_t)TIM3->CNT;
@@ -80,30 +85,10 @@ void start(){
debug.info("Init timers end");
debug.info("Init ESP8266 begin");
HAL_Delay(1000);
switch(esp0.command("ATE0\r\n")){
case ESP8266::STATUS_OK:
debug.debug("ATE0 OK");
break;
case ESP8266::STATUS_ERROR:
debug.debug("ATE0 ERROR");
break;
case ESP8266::STATUS_TIMEOUT:
debug.debug("ATE0 TIMEOUT");
break;
}
esp0.init();
//+IPD,0,14:Hello World 01\r\n
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, buf, 256);
debug.info("Init ESP8266 end");
while(true){
esp0.send_uint32("TIM3->CNT", TIM3->CNT);
HAL_Delay(10);
uint64_t recv = esp0.receive_uint64();
//sprintf(buf, "recv: %llX", recv);
//debug.debug(buf);
//motor0.setSpeed(recv>>32);
//motor1.setSpeed(recv & 0x00000000FFFFFFFF);
pid(recv & 0x00000000FFFFFFFF);
HAL_Delay(10);
}
}

View File

@@ -46,6 +46,8 @@ TIM_HandleTypeDef htim4;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart2_tx;
PCD_HandleTypeDef hpcd_USB_FS;
@@ -402,7 +404,7 @@ static void MX_USART1_UART_Init(void)
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 2250000;
huart1.Init.BaudRate = 921600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
@@ -435,7 +437,7 @@ static void MX_USART2_UART_Init(void)
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 2250000;
huart2.Init.BaudRate = 921600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
@@ -493,6 +495,12 @@ static void MX_DMA_Init(void)
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
/* DMA1_Channel5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
/* DMA1_Channel7_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);

View File

@@ -23,6 +23,10 @@
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart2_tx;
/* Private typedef -----------------------------------------------------------*/
@@ -327,6 +331,39 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
__HAL_AFIO_REMAP_USART1_ENABLE();
/* USART1 DMA Init */
/* USART1_RX Init */
hdma_usart1_rx.Instance = DMA1_Channel5;
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_rx.Init.Mode = DMA_NORMAL;
hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
/* USART1_TX Init */
hdma_usart1_tx.Instance = DMA1_Channel4;
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_tx.Init.Mode = DMA_NORMAL;
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(huart,hdmatx,hdma_usart1_tx);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 7, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
@@ -406,6 +443,10 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
/* USART1 DMA DeInit */
HAL_DMA_DeInit(huart->hdmarx);
HAL_DMA_DeInit(huart->hdmatx);
/* USART1 interrupt DeInit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */

View File

@@ -56,6 +56,8 @@
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim4;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
@@ -201,6 +203,34 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f1xx.s). */
/******************************************************************************/
/**
* @brief This function handles DMA1 channel4 global interrupt.
*/
void DMA1_Channel4_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel4_IRQn 0 */
/* USER CODE END DMA1_Channel4_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_tx);
/* USER CODE BEGIN DMA1_Channel4_IRQn 1 */
/* USER CODE END DMA1_Channel4_IRQn 1 */
}
/**
* @brief This function handles DMA1 channel5 global interrupt.
*/
void DMA1_Channel5_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel5_IRQn 0 */
/* USER CODE END DMA1_Channel5_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_rx);
/* USER CODE BEGIN DMA1_Channel5_IRQn 1 */
/* USER CODE END DMA1_Channel5_IRQn 1 */
}
/**
* @brief This function handles DMA1 channel7 global interrupt.
*/

View File

@@ -3,7 +3,27 @@ CAD.formats=
CAD.pinconfig=
CAD.provider=
Dma.Request0=USART2_TX
Dma.RequestsNb=1
Dma.Request1=USART1_RX
Dma.Request2=USART1_TX
Dma.RequestsNb=3
Dma.USART1_RX.1.Direction=DMA_PERIPH_TO_MEMORY
Dma.USART1_RX.1.Instance=DMA1_Channel5
Dma.USART1_RX.1.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.USART1_RX.1.MemInc=DMA_MINC_ENABLE
Dma.USART1_RX.1.Mode=DMA_NORMAL
Dma.USART1_RX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.USART1_RX.1.PeriphInc=DMA_PINC_DISABLE
Dma.USART1_RX.1.Priority=DMA_PRIORITY_LOW
Dma.USART1_RX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
Dma.USART1_TX.2.Direction=DMA_MEMORY_TO_PERIPH
Dma.USART1_TX.2.Instance=DMA1_Channel4
Dma.USART1_TX.2.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.USART1_TX.2.MemInc=DMA_MINC_ENABLE
Dma.USART1_TX.2.Mode=DMA_NORMAL
Dma.USART1_TX.2.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.USART1_TX.2.PeriphInc=DMA_PINC_DISABLE
Dma.USART1_TX.2.Priority=DMA_PRIORITY_LOW
Dma.USART1_TX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
Dma.USART2_TX.0.Direction=DMA_MEMORY_TO_PERIPH
Dma.USART2_TX.0.Instance=DMA1_Channel7
Dma.USART2_TX.0.MemDataAlignment=DMA_MDATAALIGN_BYTE
@@ -68,6 +88,8 @@ Mcu.UserName=STM32F103C8Tx
MxCube.Version=6.7.0
MxDb.Version=DB.6.0.70
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
NVIC.DMA1_Channel7_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.ForceEnableDMAVector=true
@@ -166,6 +188,7 @@ ProjectManager.PreviousToolchain=
ProjectManager.ProjectBuild=false
ProjectManager.ProjectFileName=F103C8-PFC.ioc
ProjectManager.ProjectName=F103C8-PFC
ProjectManager.ProjectStructure=
ProjectManager.RegisterCallBack=
ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=STM32CubeIDE
@@ -221,10 +244,10 @@ TIM2.Prescaler=63
TIM4.IPParameters=Prescaler,Period
TIM4.Period=9999
TIM4.Prescaler=71
USART1.BaudRate=2250000
USART1.BaudRate=921600
USART1.IPParameters=VirtualMode,BaudRate
USART1.VirtualMode=VM_ASYNC
USART2.BaudRate=2250000
USART2.BaudRate=921600
USART2.IPParameters=VirtualMode,BaudRate
USART2.VirtualMode=VM_ASYNC
VP_SYS_VS_Systick.Mode=SysTick

Binary file not shown.