stm32h7 udp server with adc

Dependencies:   ADE7912

Committer:
yuliyasm
Date:
Wed Oct 28 14:04:06 2020 +0000
Revision:
4:55a7bfd3a297
Parent:
3:5f002243ff14
Child:
5:0f04c6c72fc9
test

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 3:5f002243ff14 74
yuliyasm 2:fcb521c36965 75 struct ADE7912_Inst *ade = New_ADE7912(&hspi1);
yuliyasm 2:fcb521c36965 76 struct ADE7912_Phase_Settings a_phase;
yuliyasm 2:fcb521c36965 77 a_phase.CS_pin = SPI1_CS_Pin;
yuliyasm 2:fcb521c36965 78 a_phase.CS_port = SPI1_CS_GPIO_Port;
yuliyasm 3:5f002243ff14 79
yuliyasm 3:5f002243ff14 80
yuliyasm 2:fcb521c36965 81 ADE7912_PhaseInit(ade, &a_phase, PHASE_A);
yuliyasm 3:5f002243ff14 82
yuliyasm 2:fcb521c36965 83 ADE7912_EnablePhase(ade, PHASE_A);
yuliyasm 2:fcb521c36965 84
yuliyasm 2:fcb521c36965 85 uint32_t tickCount = osKernelSysTick();
yuliyasm 2:fcb521c36965 86 /* Infinite loop */
yuliyasm 2:fcb521c36965 87 for(;;) {
yuliyasm 2:fcb521c36965 88 ADE7912_UpdateData(ade);
yuliyasm 2:fcb521c36965 89 out_buffer[0] = (int32_t)(ADE7912_GetCurrent(ade, PHASE_A) * 1000);
yuliyasm 2:fcb521c36965 90 out_buffer[1] = (ADE7912_GetVoltage(ade, PHASE_A) * 1000);
yuliyasm 4:55a7bfd3a297 91
yuliyasm 4:55a7bfd3a297 92 printf("Update ");
yuliyasm 4:55a7bfd3a297 93 printf("%d, ",out_buffer[0]);
yuliyasm 4:55a7bfd3a297 94 printf("%d\n",out_buffer[1]);
yuliyasm 2:fcb521c36965 95
yuliyasm 2:fcb521c36965 96 ThisThread::sleep_for(1000);
yuliyasm 0:837bd71aa81c 97 }
yuliyasm 2:fcb521c36965 98
yuliyasm 2:fcb521c36965 99 //while (true) {
yuliyasm 2:fcb521c36965 100 // led1 = !led1;
yuliyasm 2:fcb521c36965 101 // ThisThread::sleep_for(500);
yuliyasm 2:fcb521c36965 102 // }
yuliyasm 0:837bd71aa81c 103 }