stm32h7 udp server with adc

Dependencies:   ADE7912

Committer:
yuliyasm
Date:
Tue Oct 27 12:13:27 2020 +0000
Revision:
2:fcb521c36965
Parent:
0:837bd71aa81c
Child:
3:5f002243ff14
bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuliyasm 0:837bd71aa81c 1 #include "mbed.h"
yuliyasm 2:fcb521c36965 2 #include "main.h"
yuliyasm 2:fcb521c36965 3 #include "cmsis_os.h"
yuliyasm 2:fcb521c36965 4 #include "communication.h"
yuliyasm 2:fcb521c36965 5 #include "ADE7912.h"
yuliyasm 2:fcb521c36965 6 #include "stdlib.h"
yuliyasm 0:837bd71aa81c 7 #include "EthernetInterface.h"
yuliyasm 0:837bd71aa81c 8 #include "LWIPStack.h"
yuliyasm 0:837bd71aa81c 9
yuliyasm 0:837bd71aa81c 10 EthernetInterface net;
yuliyasm 0:837bd71aa81c 11
yuliyasm 0:837bd71aa81c 12 DigitalOut led1(LED1);
yuliyasm 0:837bd71aa81c 13 Thread thread;
yuliyasm 2:fcb521c36965 14 int out_buffer[2];
yuliyasm 2:fcb521c36965 15
yuliyasm 0:837bd71aa81c 16
yuliyasm 0:837bd71aa81c 17 void UDP_thread()
yuliyasm 0:837bd71aa81c 18 {
yuliyasm 0:837bd71aa81c 19 printf("UDP Socket example\n");
yuliyasm 0:837bd71aa81c 20 net.set_network(SocketAddress("192.168.0.10"), SocketAddress("255.255.255.0"), SocketAddress("192.168.0.1"));
yuliyasm 0:837bd71aa81c 21 if (0 != net.connect()) {
yuliyasm 0:837bd71aa81c 22 printf("Error connecting\n");
yuliyasm 0:837bd71aa81c 23 }
yuliyasm 0:837bd71aa81c 24
yuliyasm 0:837bd71aa81c 25 UDPSocket sock;
yuliyasm 0:837bd71aa81c 26 sock.open(&net);
yuliyasm 0:837bd71aa81c 27 sock.bind(55151);
yuliyasm 0:837bd71aa81c 28 SocketAddress receive("192.168.0.1", 55151);
yuliyasm 0:837bd71aa81c 29
yuliyasm 0:837bd71aa81c 30 for(;;) {
yuliyasm 2:fcb521c36965 31 // out_buffer = {rand(), rand()};
yuliyasm 0:837bd71aa81c 32 // sock.recvfrom(&receive, &out_buffer, sizeof(out_buffer));
yuliyasm 0:837bd71aa81c 33 // printf(out_buffer);
yuliyasm 0:837bd71aa81c 34 if (0 > sock.sendto(receive, out_buffer, sizeof(out_buffer))) {
yuliyasm 0:837bd71aa81c 35 printf("Error sending data\n");
yuliyasm 0:837bd71aa81c 36 }
yuliyasm 2:fcb521c36965 37 // printf(out_buffer[0]+'0');
yuliyasm 0:837bd71aa81c 38 ThisThread::sleep_for(2000);
yuliyasm 0:837bd71aa81c 39 }
yuliyasm 0:837bd71aa81c 40 }
yuliyasm 0:837bd71aa81c 41
yuliyasm 2:fcb521c36965 42 SPI_HandleTypeDef hspi1;
yuliyasm 2:fcb521c36965 43
yuliyasm 0:837bd71aa81c 44 int main()
yuliyasm 0:837bd71aa81c 45 {
yuliyasm 0:837bd71aa81c 46 thread.start(UDP_thread);
yuliyasm 0:837bd71aa81c 47
yuliyasm 2:fcb521c36965 48
yuliyasm 2:fcb521c36965 49 hspi1.Instance = SPI1;
yuliyasm 2:fcb521c36965 50 hspi1.Init.Mode = SPI_MODE_MASTER;
yuliyasm 2:fcb521c36965 51 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
yuliyasm 2:fcb521c36965 52 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
yuliyasm 2:fcb521c36965 53 hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
yuliyasm 2:fcb521c36965 54 hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
yuliyasm 2:fcb521c36965 55 hspi1.Init.NSS = SPI_NSS_SOFT;
yuliyasm 2:fcb521c36965 56 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
yuliyasm 2:fcb521c36965 57 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
yuliyasm 2:fcb521c36965 58 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
yuliyasm 2:fcb521c36965 59 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
yuliyasm 2:fcb521c36965 60 hspi1.Init.CRCPolynomial = 0x0;
yuliyasm 2:fcb521c36965 61 hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
yuliyasm 2:fcb521c36965 62 hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
yuliyasm 2:fcb521c36965 63 hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
yuliyasm 2:fcb521c36965 64 hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
yuliyasm 2:fcb521c36965 65 hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
yuliyasm 2:fcb521c36965 66 hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
yuliyasm 2:fcb521c36965 67 hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
yuliyasm 2:fcb521c36965 68 hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
yuliyasm 2:fcb521c36965 69 hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
yuliyasm 2:fcb521c36965 70 hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
yuliyasm 2:fcb521c36965 71
yuliyasm 2:fcb521c36965 72
yuliyasm 2:fcb521c36965 73 uint32_t buf = 0xAA;
yuliyasm 2:fcb521c36965 74 struct ADE7912_Inst *ade = New_ADE7912(&hspi1);
yuliyasm 2:fcb521c36965 75 struct ADE7912_Phase_Settings a_phase;
yuliyasm 2:fcb521c36965 76 a_phase.CS_pin = SPI1_CS_Pin;
yuliyasm 2:fcb521c36965 77 a_phase.CS_port = SPI1_CS_GPIO_Port;
yuliyasm 2:fcb521c36965 78 ADE7912_PhaseInit(ade, &a_phase, PHASE_A);
yuliyasm 2:fcb521c36965 79 ADE7912_EnablePhase(ade, PHASE_A);
yuliyasm 2:fcb521c36965 80
yuliyasm 2:fcb521c36965 81 uint32_t tickCount = osKernelSysTick();
yuliyasm 2:fcb521c36965 82 /* Infinite loop */
yuliyasm 2:fcb521c36965 83 for(;;) {
yuliyasm 2:fcb521c36965 84 ADE7912_UpdateData(ade);
yuliyasm 2:fcb521c36965 85 out_buffer[0] = (int32_t)(ADE7912_GetCurrent(ade, PHASE_A) * 1000);
yuliyasm 2:fcb521c36965 86 out_buffer[1] = (ADE7912_GetVoltage(ade, PHASE_A) * 1000);
yuliyasm 2:fcb521c36965 87
yuliyasm 2:fcb521c36965 88 ThisThread::sleep_for(1000);
yuliyasm 0:837bd71aa81c 89 }
yuliyasm 2:fcb521c36965 90
yuliyasm 2:fcb521c36965 91 //while (true) {
yuliyasm 2:fcb521c36965 92 // led1 = !led1;
yuliyasm 2:fcb521c36965 93 // ThisThread::sleep_for(500);
yuliyasm 2:fcb521c36965 94 // }
yuliyasm 0:837bd71aa81c 95 }