bunch of tesitng for F746NG
Dependencies: BSP_DISCO_F746NG F746_GUI F7_Ethernet LCD_DISCO_F746NG SimpleSocket TMP36 GZ TS_DISCO_F746NG TextLCD WebSocketClient mbed-rtos mbed sMotor
Revision 0:45610c4af223, committed 2017-12-11
- Comitter:
- Maricius
- Date:
- Mon Dec 11 09:46:58 2017 +0000
- Child:
- 1:1f4543ea364d
- Commit message:
- testing2
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_DISCO_F746NG.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/teams/ST/code/BSP_DISCO_F746NG/#56384bddaba5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F746_GUI.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/MikamiUitOpen/code/F746_GUI/#a9cf68d24f40
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F7_Ethernet.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/DieterGraef/code/F7_Ethernet/#28ba13dd96f7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_DISCO_F746NG.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/teams/ST/code/LCD_DISCO_F746NG/#d44525b1de98
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient/NTPClient.cpp Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,164 @@ +/* NTPClient.cpp */ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +//Debug is disabled by default +#if 0 +//Enable debug +#define __DEBUG__ +#include <cstdio> +#define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__); + +#else +//Disable debug +#define DBG(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) + +#endif + +#include "NTPClient.h" + +#include "UDPSocket.h" + +#include "mbed.h" //time() and set_time() + +#define NTP_PORT 123 +#define NTP_CLIENT_PORT 0 //Random port +#define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900) + +NTPClient::NTPClient() : m_sock() +{ + + +} + +NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout) +{ +#ifdef __DEBUG__ + time_t ctTime; + ctTime = time(NULL); + DBG("Time is set to (UTC): %s", ctime(&ctTime)); +#endif + + //Create & bind socket + DBG("Binding socket"); + m_sock.bind(0); //Bind to a random port + + m_sock.set_blocking(false, timeout); //Set not blocking + + struct NTPPacket pkt; + + //Now ping the server and wait for response + DBG("Ping"); + //Prepare NTP Packet: + pkt.li = 0; //Leap Indicator : No warning + pkt.vn = 4; //Version Number : 4 + pkt.mode = 3; //Client mode + pkt.stratum = 0; //Not relevant here + pkt.poll = 0; //Not significant as well + pkt.precision = 0; //Neither this one is + + pkt.rootDelay = 0; //Or this one + pkt.rootDispersion = 0; //Or that one + pkt.refId = 0; //... + + pkt.refTm_s = 0; + pkt.origTm_s = 0; + pkt.rxTm_s = 0; + pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE + + pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0; + + Endpoint outEndpoint; + + if( outEndpoint.set_address(host, port) < 0) + { + m_sock.close(); + return NTP_DNS; + } + + //Set timeout, non-blocking and wait using select + int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) ); + if (ret < 0 ) + { + ERR("Could not send packet"); + m_sock.close(); + return NTP_CONN; + } + + //Read response + Endpoint inEndpoint; + // Set the inEndpoint address property + inEndpoint.set_address(outEndpoint.get_address(), 0); + DBG("Pong"); + do + { + ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name + if(ret < 0) + { + ERR("Could not receive packet"); + m_sock.close(); + return NTP_CONN; + } + } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 ); + + if(ret < sizeof(NTPPacket)) //TODO: Accept chunks + { + ERR("Receive packet size does not match"); + m_sock.close(); + return NTP_PRTCL; + } + + if( pkt.stratum == 0) //Kiss of death message : Not good ! + { + ERR("Kissed to death!"); + m_sock.close(); + return NTP_PRTCL; + } + + //Correct Endianness + pkt.refTm_s = ntohl( pkt.refTm_s ); + pkt.refTm_f = ntohl( pkt.refTm_f ); + pkt.origTm_s = ntohl( pkt.origTm_s ); + pkt.origTm_f = ntohl( pkt.origTm_f ); + pkt.rxTm_s = ntohl( pkt.rxTm_s ); + pkt.rxTm_f = ntohl( pkt.rxTm_f ); + pkt.txTm_s = ntohl( pkt.txTm_s ); + pkt.txTm_f = ntohl( pkt.txTm_f ); + + //Compute offset, see RFC 4330 p.13 + uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL)); + int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow + DBG("Sent @%ul", pkt.txTm_s); + DBG("Offset: %lld", offset); + //Set time accordingly + set_time( time(NULL) + offset ); + +#ifdef __DEBUG__ + ctTime = time(NULL); + DBG("Time is now (UTC): %s", ctime(&ctTime)); +#endif + + m_sock.close(); + + return NTP_OK; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient/NTPClient.h Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,102 @@ +/* NTPClient.h */ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** \file +NTP Client header file +*/ + +#ifndef NTPCLIENT_H_ +#define NTPCLIENT_H_ + +#include <cstdint> + +using std::uint8_t; +using std::uint16_t; +using std::uint32_t; + +#include "UDPSocket.h" + +#define NTP_DEFAULT_PORT 123 +#define NTP_DEFAULT_TIMEOUT 4000 + +///NTP client results +enum NTPResult +{ + NTP_DNS, ///<Could not resolve name + NTP_PRTCL, ///<Protocol error + NTP_TIMEOUT, ///<Connection timeout + NTP_CONN, ///<Connection error + NTP_OK = 0, ///<Success +}; + +/** NTP Client to update the mbed's RTC using a remote time server +* +*/ +class NTPClient +{ +public: + /** + Instantiate the NTP client + */ + NTPClient(); + + /**Get current time (blocking) + Update the time using the server host + Blocks until completion + @param host NTP server IPv4 address or hostname (will be resolved via DNS) + @param port port to use; defaults to 123 + @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) + @return 0 on success, NTP error code (<0) on failure + */ + NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking + +private: + struct NTPPacket //See RFC 4330 for Simple NTP + { + //WARN: We are in LE! Network is BE! + //LSb first + unsigned mode : 3; + unsigned vn : 3; + unsigned li : 2; + + uint8_t stratum; + uint8_t poll; + uint8_t precision; + //32 bits header + + uint32_t rootDelay; + uint32_t rootDispersion; + uint32_t refId; + + uint32_t refTm_s; + uint32_t refTm_f; + uint32_t origTm_s; + uint32_t origTm_f; + uint32_t rxTm_s; + uint32_t rxTm_f; + uint32_t txTm_s; + uint32_t txTm_f; + } __attribute__ ((packed)); + + UDPSocket m_sock; + +}; + + +#endif /* NTPCLIENT_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TMP36-GZ.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/tylerjw/code/TMP36-GZ/#2b0feb7bdebc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TS_DISCO_F746NG.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/TS_DISCO_F746NG/#fe0cf5e2960f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#308d188a2d3a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/documentation/readme.txt Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,28 @@ +This small test program for the STM32F746G-DISCO +Is written by Christoffer + +The code is written in the online compiler as developer.mbed.org. + +For access to the LCD screen i use the LCD_DISCO_F746NG library + +For Ethernet access i use the F7_Ethernet library that again depends on +the mbed-rtos library to work. This libary also include everything needed for + writing threaded tasks. + +I also use the F746_GUI LIbrary for varius GUI related task aswell as +the TextLCD library for writing text to the LCD display. The standard library +mbed is used for all analog and digital input and output aswell as varius other +low level tasks. + +Futher comments will be in the code directly in the main.cpp file. + +As of 25-06.2017 this is using the newest version of all these librarys. + +List of librarys and version number: + +F7_Ethernet: revision 1:28ba13dd96f7 +LCD_DISCO_F746NG: revision 0:d44525b1de98 +mbed-rtos: revision 2:bf7dc5f5bca9 +mbed: 144.0f02307a0877 +F746_GUI: 32:e6648167e8d3 +TextLCD: 8:308d188a2d3a \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/input_functions.cpp Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,89 @@ +#include "mbed.h" +#include "rtos.h" + + +AnalogIn heart_rate2(A0); +AnalogIn adc_temp(A3); +AnalogIn adc_sound(A1); +//AnalogOut buzzer(D4); +//AnalogIn Proximity_measure(A2); +void heartrate(); +void sound(); +void temp(); +//void write_to_socket(); +//void proximity(); + + double h; + double *h_p = (double*)malloc(20*sizeof(double)); + + void heartrate(){ + + h_p = &h; + while (1){ + h = heart_rate2.read(); + wait(0.1); + } +} + +void temp(){ + //------------------------------------------------------------------------// + //This section is using the external temperatur sensor "Grove temperatur sensor v1.2 + //float resistance; + const int B = 4275; + //const int R0 = 10000; + //char temp[10]; + + //reads the voltage from the analog temperatur sensor + double a = adc_temp.read(); + double R; + R = 1023.0/a-1.0; + //R = R0*R; + double temperature; + //Calculate the temperature from the voltage + temperature = 1.0/(log(R)/B+1/298.15)-173.15; + //temperature = ((a*1000)-500)/10; + //sprintf(temp, "%f", a); + //resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor; + //temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ; + //delay(100); + + } + + +void sound() { + float b = adc_sound.read(); + char temp_sound[10]; + float noiselevel; + noiselevel = b; + //if the sound sensor is detection any noise a LED is switched on and a message with the current sound level is displayed. + if (noiselevel != 0) { + + //do something with noiselevel!! + + } + + //external_LCD.DisplayStringAt(0, LINE(1), (uint8_t *)"This is just a test", CENTER_MODE); + wait(0.1); + } + + + +//void write_to_stocket(){ + + // } + +/*void proximity(){ + double prox_read; + while(1){ + + prox_read = Proximity_measure.read(); + + + wait(0.5); + + buzzer.write(0); + wait(3); + } + + }*/ + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,175 @@ +#include "mbed.h" +#include "F746_GUI.hpp" +#include "LCD_DISCO_F746NG.h" +#include "TextLCD.h" +#include <string> +#include <math.h> +#include "EthernetInterface.h" +#include "rtos.h" +#include "lwip/inet.h" +#include "lwip/netif.h" +#include "netif/etharp.h" +#include "lwip/dhcp.h" +#include "eth_arch.h" +#include "lwip/tcpip.h" + +#include "NTPClient.h" +#include <stdio.h> + + +//Initialization of varius connections and interfaces + + +DigitalOut myled2(D4); + + +int number_clicks; +LCD_DISCO_F746NG lcd; +EthernetInterface eth; +Thread t1, t2; + +//declare external funktions and pointers +extern void heartrate(); +extern double *h_p; +//extern void proximity(); +NTPClient ntp; + + +int main() +{ + + + + //First we check if the ethernet interface was able to initialize + if(eth.init()!=0) { + + char eth_init [40]; + sprintf(eth_init, "EthernetInterface Initialize Error"); + lcd.DisplayStringAt(0, LINE(10), (uint8_t *)eth_init, CENTER_MODE); + while (1) { + wait(10); + } + } + //check if the ethernet intercace is connected to a network. + + if(eth.connect()!=0) { + char eth_conn[30]; + sprintf(eth_conn, "EthernetInterface Connect Error"); + lcd.DisplayStringAt(0, LINE(8), (uint8_t *)eth_conn, CENTER_MODE); + char mac_add[15]; + sprintf(mac_add, "Mac: %s", eth.getMACAddress()); + lcd.DisplayStringAt(0, LINE(10), (uint8_t *)mac_add, CENTER_MODE); + while (1) { + wait(10); + } + } + char ip_add[30]; + char net_mask[30]; + char gate_Way[30]; + char dhcp_status[30]; + + + /*Display the varius information achived from the dhcp server, + It is also possible to set all these values manually, + if no dhcp server is available + */ + sprintf(ip_add, "IP Address is %s", eth.getIPAddress()); + sprintf(net_mask, "NetMask is %s", eth.getNetworkMask()); + sprintf(gate_Way, "Gateway Address is %s", eth.getGateway()); + sprintf(dhcp_status, "Ethernet Setup OK"); + lcd.DisplayStringAt(0, LINE(2), (uint8_t *)ip_add, CENTER_MODE); + lcd.DisplayStringAt(0, LINE(4), (uint8_t *)net_mask, CENTER_MODE); + lcd.DisplayStringAt(0, LINE(6), (uint8_t *)gate_Way, CENTER_MODE); + lcd.DisplayStringAt(0, LINE(8), (uint8_t *)dhcp_status, CENTER_MODE); + wait(5); + lcd.Clear(LCD_COLOR_GREEN); + if (ntp.setTime("1.dk.pool.ntp.org") == 0) + { + char ntp_message [50]; + time_t ctTime; + ctTime = time(NULL); + sprintf(ntp_message, "%s \r\n", ctime(&ctTime)); + //sprintf(ntp_message, "Time is set to : %s \r\n", ctime(&ctTime)); + lcd.DisplayStringAt(-10, LINE(8), (uint8_t *)ntp_message, CENTER_MODE); + + } + else + { + + lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Error getting time", CENTER_MODE); + } + wait(2); + lcd.Clear(LCD_COLOR_GREEN); + + + + //This is the main tasks included in the programm + Button b1(300, 3, 70, 40, "Hjerterytme", Font16); + Button b2(200, 3, 70, 40, "Lyd og temp", Font16); + lcd.DisplayStringAt(0, LINE(10), (uint8_t *)ip_add, CENTER_MODE); + while(1) { + + + + + + + + + lcd.SetTextColor(LCD_COLOR_BLACK); + lcd.SetBackColor(LCD_COLOR_WHITE); + //print raw text to the LCD display + lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Commence Initial Testing...", CENTER_MODE); + //shows the number of times the button was clicked + + lcd.DisplayStringAt(0, LINE(6), (uint8_t *) "Christoffer Bisander", CENTER_MODE); + + +//t1.start(testing); +//t1.detach(); +//input_functions fact; + //t1.start(proximity); + //t1.join(); + + + + + + + + //Check if the button was clicked and then increments the number_clicks with one + if (b1.Touched()) { // if user touched "MODE" panel then return + lcd.SetTextColor(LCD_COLOR_RED); + + t2.start(heartrate); + lcd.Clear(LCD_COLOR_GREEN); + char heart_rate_current[50]; + double hr; + while(1) { + hr = *h_p; + +//t2.start(input_functions::heartrate, fact); + sprintf(heart_rate_current, "hjerterytme: %f", hr); + lcd.DisplayStringAt(0, LINE(14), (uint8_t *)heart_rate_current, CENTER_MODE); + wait(0.1); + } + t2.join(); + + + wait(0.5); + lcd.Clear(LCD_COLOR_BLACK); + } + + //----------------------------------------------------------------------------// + //analog sound sensor. + //read the voltage from the sound sensor + + + } + +} + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/0f02307a0877 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/socket_com.cpp Mon Dec 11 09:46:58 2017 +0000 @@ -0,0 +1,47 @@ +#include "mbed.h" +#include "rtos.h" +#include "LCD_DISCO_F746NG.h" +#include "TextLCD.h" +#include "TCPSocketConnection.h" + +LCD_DISCO_F746NG lcd_socket; + +void sending() +{ + char init_socket_error[50]; + char connect_error[50]; + int sockfd = 0,n = 0; + char recvBuff[1024]; + struct sockaddr_in serv_addr; + + memset(recvBuff, '0' ,sizeof(recvBuff)); + if((sockfd = lwip_socket(AF_INET, SOCK_STREAM, 0))< 0) { + sprintf(init_socket_error, "\n Error : Could not create socket \n"); + lcd_socket.DisplayStringAt(0, LINE(4), (uint8_t *)init_socket_error, CENTER_MODE); + + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(8889); + serv_addr.sin_addr.s_addr = inet_addr("10.130.56.27"); + + if(lwip_connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0) { + sprintf(connect_error, "\n Error : Connect Failed \n"); + lcd_socket.DisplayStringAt(0, LINE(4), (uint8_t *)connect_error, CENTER_MODE); + + } + + while((n = lwip_read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) { + recvBuff[n] = 0; + + if(fputs(recvBuff, stdout) == EOF) { + printf("\n Error : Fputs error"); + } + printf("\n"); + } + + if( n < 0) { + printf("\n Read Error \n"); + } + +} \ No newline at end of file