stm32h7 udp server with adc

Dependencies:   ADE7912

Committer:
yuliyasm
Date:
Wed Oct 28 15:35:16 2020 +0000
Revision:
5:0f04c6c72fc9
Parent:
4:55a7bfd3a297
new

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 5:0f04c6c72fc9 9 #include "dma.h"
yuliyasm 5:0f04c6c72fc9 10 #include "spi.h"
yuliyasm 5:0f04c6c72fc9 11 #include "gpio.h"
yuliyasm 0:837bd71aa81c 12
yuliyasm 0:837bd71aa81c 13 EthernetInterface net;
yuliyasm 0:837bd71aa81c 14
yuliyasm 0:837bd71aa81c 15 DigitalOut led1(LED1);
yuliyasm 0:837bd71aa81c 16 Thread thread;
yuliyasm 2:fcb521c36965 17 int out_buffer[2];
yuliyasm 2:fcb521c36965 18
yuliyasm 5:0f04c6c72fc9 19 uint32_t bufFR = 0xAA;
yuliyasm 5:0f04c6c72fc9 20 uint8_t str4byte[3];
yuliyasm 5:0f04c6c72fc9 21
yuliyasm 0:837bd71aa81c 22
yuliyasm 0:837bd71aa81c 23 void UDP_thread()
yuliyasm 0:837bd71aa81c 24 {
yuliyasm 0:837bd71aa81c 25 printf("UDP Socket example\n");
yuliyasm 0:837bd71aa81c 26 net.set_network(SocketAddress("192.168.0.10"), SocketAddress("255.255.255.0"), SocketAddress("192.168.0.1"));
yuliyasm 0:837bd71aa81c 27 if (0 != net.connect()) {
yuliyasm 0:837bd71aa81c 28 printf("Error connecting\n");
yuliyasm 0:837bd71aa81c 29 }
yuliyasm 0:837bd71aa81c 30
yuliyasm 0:837bd71aa81c 31 UDPSocket sock;
yuliyasm 0:837bd71aa81c 32 sock.open(&net);
yuliyasm 0:837bd71aa81c 33 sock.bind(55151);
yuliyasm 0:837bd71aa81c 34 SocketAddress receive("192.168.0.1", 55151);
yuliyasm 0:837bd71aa81c 35
yuliyasm 0:837bd71aa81c 36 for(;;) {
yuliyasm 2:fcb521c36965 37 // out_buffer = {rand(), rand()};
yuliyasm 0:837bd71aa81c 38 // sock.recvfrom(&receive, &out_buffer, sizeof(out_buffer));
yuliyasm 0:837bd71aa81c 39 // printf(out_buffer);
yuliyasm 5:0f04c6c72fc9 40 if (0 > sock.sendto(receive, str4byte, sizeof(str4byte))) {
yuliyasm 0:837bd71aa81c 41 printf("Error sending data\n");
yuliyasm 0:837bd71aa81c 42 }
yuliyasm 5:0f04c6c72fc9 43 printf("Update %d, %d, %d \n", str4byte[0], str4byte[1], str4byte[2]);
yuliyasm 0:837bd71aa81c 44 ThisThread::sleep_for(2000);
yuliyasm 0:837bd71aa81c 45 }
yuliyasm 0:837bd71aa81c 46 }
yuliyasm 0:837bd71aa81c 47
yuliyasm 5:0f04c6c72fc9 48
yuliyasm 5:0f04c6c72fc9 49 void ADC_thread(){
yuliyasm 5:0f04c6c72fc9 50 struct ADE7912_Inst *ade = New_ADE7912(&hspi1);
yuliyasm 5:0f04c6c72fc9 51 struct ADE7912_Phase_Settings a_phase;
yuliyasm 5:0f04c6c72fc9 52 a_phase.CS_pin = PHASE_A_CS_Pin;
yuliyasm 5:0f04c6c72fc9 53 a_phase.CS_port = PHASE_A_CS_GPIO_Port;
yuliyasm 5:0f04c6c72fc9 54 ADE7912_PhaseInit(ade, &a_phase, PHASE_A);
yuliyasm 5:0f04c6c72fc9 55 ADE7912_EnablePhase(ade, PHASE_A);
yuliyasm 5:0f04c6c72fc9 56 //SCB_DisableDCache();
yuliyasm 5:0f04c6c72fc9 57
yuliyasm 5:0f04c6c72fc9 58 uint32_t tickCount = osKernelSysTick();
yuliyasm 5:0f04c6c72fc9 59 /* Infinite loop */
yuliyasm 5:0f04c6c72fc9 60 for(;;)
yuliyasm 5:0f04c6c72fc9 61 {
yuliyasm 5:0f04c6c72fc9 62 ADE7912_UpdateData(ade);
yuliyasm 5:0f04c6c72fc9 63
yuliyasm 5:0f04c6c72fc9 64 bufFR = (int32_t)(ADE7912_GetCurrent(ade, PHASE_A) * 1000);
yuliyasm 5:0f04c6c72fc9 65 sprintf( (char *)str4byte, "%03d", (short)bufFR );
yuliyasm 5:0f04c6c72fc9 66
yuliyasm 5:0f04c6c72fc9 67 // TransmitUARTPackage(&huart3, str_cur+1, 12);
yuliyasm 5:0f04c6c72fc9 68 // TransmitUARTPackage(&huart3, str4byte, 3);
yuliyasm 5:0f04c6c72fc9 69 // TransmitUARTPackage(&huart3, str_vol+1, 12);
yuliyasm 5:0f04c6c72fc9 70
yuliyasm 5:0f04c6c72fc9 71 bufFR = (ADE7912_GetVoltage(ade, PHASE_A) * 1000);
yuliyasm 5:0f04c6c72fc9 72 sprintf( (char *)str4byte, "%03d", (short)bufFR );
yuliyasm 5:0f04c6c72fc9 73
yuliyasm 5:0f04c6c72fc9 74 // TransmitUARTPackage(&huart3, str4byte, 3);
yuliyasm 5:0f04c6c72fc9 75 // TransmitUARTPackage(&huart3, str_temp+1, 9);
yuliyasm 5:0f04c6c72fc9 76
yuliyasm 5:0f04c6c72fc9 77 bufFR = (ADE7912_GetTemp(ade, PHASE_A) * 1);
yuliyasm 5:0f04c6c72fc9 78 sprintf( (char *)str4byte, "%03d", (short)bufFR );
yuliyasm 5:0f04c6c72fc9 79
yuliyasm 5:0f04c6c72fc9 80 // TransmitUARTPackage(&huart3, str4byte, 3);
yuliyasm 5:0f04c6c72fc9 81 // TransmitUARTPackage(&huart3, str_space+1, 2);
yuliyasm 5:0f04c6c72fc9 82
yuliyasm 5:0f04c6c72fc9 83 ThisThread::sleep_for(1000);
yuliyasm 5:0f04c6c72fc9 84
yuliyasm 5:0f04c6c72fc9 85 }
yuliyasm 5:0f04c6c72fc9 86 }
yuliyasm 2:fcb521c36965 87
yuliyasm 0:837bd71aa81c 88 int main()
yuliyasm 0:837bd71aa81c 89 {
yuliyasm 5:0f04c6c72fc9 90 MX_GPIO_Init();
yuliyasm 5:0f04c6c72fc9 91 MX_DMA_Init();
yuliyasm 5:0f04c6c72fc9 92 MX_SPI1_Init();
yuliyasm 5:0f04c6c72fc9 93
yuliyasm 0:837bd71aa81c 94 thread.start(UDP_thread);
yuliyasm 0:837bd71aa81c 95
yuliyasm 5:0f04c6c72fc9 96 thread.start(ADC_thread);
yuliyasm 2:fcb521c36965 97
yuliyasm 2:fcb521c36965 98 //while (true) {
yuliyasm 2:fcb521c36965 99 // led1 = !led1;
yuliyasm 2:fcb521c36965 100 // ThisThread::sleep_for(500);
yuliyasm 2:fcb521c36965 101 // }
yuliyasm 0:837bd71aa81c 102 }