stm32h7 udp server with adc

Dependencies:   ADE7912

main.cpp

Committer:
yuliyasm
Date:
2020-10-28
Revision:
4:55a7bfd3a297
Parent:
3:5f002243ff14
Child:
5:0f04c6c72fc9

File content as of revision 4:55a7bfd3a297:

#include "mbed.h"
#include "main.h"
#include "cmsis_os.h"
#include "communication.h"
#include "ADE7912.h"
#include "stdlib.h"
#include "EthernetInterface.h"
#include "LWIPStack.h"

EthernetInterface net;

DigitalOut led1(LED1);
Thread thread;
int out_buffer[2];


void UDP_thread()
{
    printf("UDP Socket example\n");
    net.set_network(SocketAddress("192.168.0.10"), SocketAddress("255.255.255.0"), SocketAddress("192.168.0.1"));
    if (0 != net.connect()) {
        printf("Error connecting\n");
    }

    UDPSocket sock;
    sock.open(&net);
    sock.bind(55151);
    SocketAddress receive("192.168.0.1", 55151);

    for(;;) {
//        out_buffer = {rand(), rand()};
//        sock.recvfrom(&receive, &out_buffer, sizeof(out_buffer));
//        printf(out_buffer);
        if (0 > sock.sendto(receive, out_buffer, sizeof(out_buffer))) {
            printf("Error sending data\n");
        }
//        printf(out_buffer[0]+'0');
        ThisThread::sleep_for(2000);
    }
}

SPI_HandleTypeDef hspi1;

int main()
{
    thread.start(UDP_thread);


    hspi1.Instance = SPI1;
    hspi1.Init.Mode = SPI_MODE_MASTER;
    hspi1.Init.Direction = SPI_DIRECTION_2LINES;
    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
    hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
    hspi1.Init.NSS = SPI_NSS_SOFT;
    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
    hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
    hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspi1.Init.CRCPolynomial = 0x0;
    hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
    hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
    hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
    hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
    hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
    hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
    hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
    hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
    hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
    hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;


    uint32_t buf = 0xAA;

    struct ADE7912_Inst *ade = New_ADE7912(&hspi1);
    struct ADE7912_Phase_Settings a_phase;
    a_phase.CS_pin = SPI1_CS_Pin;
    a_phase.CS_port = SPI1_CS_GPIO_Port;
    
    
    ADE7912_PhaseInit(ade, &a_phase, PHASE_A);

    ADE7912_EnablePhase(ade, PHASE_A);

    uint32_t tickCount = osKernelSysTick();
    /* Infinite loop */
    for(;;) {
        ADE7912_UpdateData(ade);
        out_buffer[0] = (int32_t)(ADE7912_GetCurrent(ade, PHASE_A) * 1000);
        out_buffer[1] = (ADE7912_GetVoltage(ade, PHASE_A) * 1000);
        
        printf("Update ");
        printf("%d, ",out_buffer[0]);
        printf("%d\n",out_buffer[1]);

        ThisThread::sleep_for(1000);
    }

    //while (true) {
//        led1 = !led1;
//        ThisThread::sleep_for(500);
//    }
}