DISCO-F746NG_LCDTS_CC3000_NTP

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG NTPClient NVIC_set_all_priorities TS_DISCO_F746NG cc3000_hostdriver_mbedsocket mbed

Fork of DISCO-F746NG_LCDTS_demo by ST

Committer:
JackB
Date:
Sat Oct 17 23:07:22 2015 +0000
Revision:
2:030b4c500fce
Parent:
0:9933f7db9a9b
CC3000 NTP Demo on DISCO-F746NG

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:9933f7db9a9b 1 #include "mbed.h"
JackB 2:030b4c500fce 2 #include "cc3000.h"
JackB 2:030b4c500fce 3 #include "main.h"
JackB 2:030b4c500fce 4 #include "NTPClient.h"
bcostm 0:9933f7db9a9b 5 #include "TS_DISCO_F746NG.h"
bcostm 0:9933f7db9a9b 6 #include "LCD_DISCO_F746NG.h"
bcostm 0:9933f7db9a9b 7
JackB 2:030b4c500fce 8 #define CC3000_IRQ D3 // (D3)
JackB 2:030b4c500fce 9 #define CC3000_EN D5 // (D5)
JackB 2:030b4c500fce 10 #define CC3000_CS D10 // (D10)
JackB 2:030b4c500fce 11 #define CC3000_MOSI D11 // (D11)
JackB 2:030b4c500fce 12 #define CC3000_MISO D12 // (D12)
JackB 2:030b4c500fce 13 #define CC3000_SCLK D13 // (D13)
JackB 2:030b4c500fce 14
JackB 2:030b4c500fce 15 #define SSID "Prakjaroen"
JackB 2:030b4c500fce 16 #define PHRASE "A4B5C6D7E8F9"
JackB 2:030b4c500fce 17 #define SECURITY WPA2
JackB 2:030b4c500fce 18
JackB 2:030b4c500fce 19 #define IP "192.168.2.165"
JackB 2:030b4c500fce 20 #define MASK "255.255.255.0"
JackB 2:030b4c500fce 21 #define GW "192.168.2.1"
JackB 2:030b4c500fce 22
JackB 2:030b4c500fce 23 #define DHCP 1
JackB 2:030b4c500fce 24
JackB 2:030b4c500fce 25 #if (DHCP == 1)
JackB 2:030b4c500fce 26 #define STATIC_IP 0
JackB 2:030b4c500fce 27 #define IP_INIT DHCP
JackB 2:030b4c500fce 28 #else
JackB 2:030b4c500fce 29 #define STATIC_IP 1
JackB 2:030b4c500fce 30 #define IP_INIT STATIC_IP
JackB 2:030b4c500fce 31 #endif
JackB 2:030b4c500fce 32
JackB 2:030b4c500fce 33 using namespace mbed_cc3000;
JackB 2:030b4c500fce 34
JackB 2:030b4c500fce 35 #define MY_BOARD MBED_BOARD_EXAMPLE
JackB 2:030b4c500fce 36
JackB 2:030b4c500fce 37 /* cc3000 module declaration specific for user's board. Check also init() */
JackB 2:030b4c500fce 38 #if (MY_BOARD == WIGO)
JackB 2:030b4c500fce 39 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), SSID, PHRASE, SECURITY, false);
JackB 2:030b4c500fce 40 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 41 #elif (MY_BOARD == WIFI_DIPCORTEX)
JackB 2:030b4c500fce 42 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), SSID, PHRASE, SECURITY, false);
JackB 2:030b4c500fce 43 Serial pc(UART_TX, UART_RX);
JackB 2:030b4c500fce 44 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
JackB 2:030b4c500fce 45 cc3000 wifi(CC3000_IRQ, CC3000_EN, CC3000_CS, SPI(CC3000_MOSI, CC3000_MISO, CC3000_SCLK), SSID, PHRASE, SECURITY, false); //SparkFun Board on Arduino pin definitions
JackB 2:030b4c500fce 46 //Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 47 // Serial pc(P4_28, P4_29);
JackB 2:030b4c500fce 48
JackB 2:030b4c500fce 49 #if defined(TARGET_NUCLEO_F411RE)
JackB 2:030b4c500fce 50 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 51 #elif defined(TARGET_NUCLEO_F446RE)
JackB 2:030b4c500fce 52 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 53 #elif defined(TARGET_ARCH_PRO)
JackB 2:030b4c500fce 54 Serial pc(P4_28, P4_29);
JackB 2:030b4c500fce 55 #elif defined(TARGET_ARCH_MAX)
JackB 2:030b4c500fce 56 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 57 Serial bt(D6, D7); // tx, rx, Arch Max
JackB 2:030b4c500fce 58 #elif defined(TARGET_LPC4337)
JackB 2:030b4c500fce 59 Serial pc(P2_10, P2_11); // TX, RX
JackB 2:030b4c500fce 60 #elif defined(TARGET_DISCO_F746NG)
JackB 2:030b4c500fce 61 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 62 #else
JackB 2:030b4c500fce 63 Serial pc(USBTX, USBRX);
JackB 2:030b4c500fce 64 #endif
JackB 2:030b4c500fce 65
JackB 2:030b4c500fce 66
JackB 2:030b4c500fce 67
JackB 2:030b4c500fce 68 #else
JackB 2:030b4c500fce 69
JackB 2:030b4c500fce 70 #endif
JackB 2:030b4c500fce 71
JackB 2:030b4c500fce 72 // array to store RM parameters from EEPROM
JackB 2:030b4c500fce 73 unsigned char cRMParamsFromEeprom[128];
JackB 2:030b4c500fce 74
JackB 2:030b4c500fce 75 // array to store MAC address from EEPROM
JackB 2:030b4c500fce 76 unsigned char cMacFromEeprom[6];
JackB 2:030b4c500fce 77
bcostm 0:9933f7db9a9b 78 LCD_DISCO_F746NG lcd;
bcostm 0:9933f7db9a9b 79 TS_DISCO_F746NG ts;
bcostm 0:9933f7db9a9b 80
bcostm 0:9933f7db9a9b 81 int main()
bcostm 0:9933f7db9a9b 82 {
JackB 2:030b4c500fce 83 uint8_t firmware_ver[2];
JackB 2:030b4c500fce 84 signed char mac_status = -1;
JackB 2:030b4c500fce 85 unsigned char FW_status = 1;
JackB 2:030b4c500fce 86
JackB 2:030b4c500fce 87 init(); // board dependent init
JackB 2:030b4c500fce 88 pc.baud(230400);
JackB 2:030b4c500fce 89 printf("\r\n--------------------------------------------------------------------------------\r\n");
JackB 2:030b4c500fce 90
JackB 2:030b4c500fce 91 printf("Target : ");
JackB 2:030b4c500fce 92 #if defined(TARGET_NUCLEO_F411RE)
JackB 2:030b4c500fce 93 printf("NUCLEO F411RE\r\n");
JackB 2:030b4c500fce 94 #elif defined(TARGET_NUCLEO_F446RE)
JackB 2:030b4c500fce 95 printf("NUCLEO F446RE\r\n");
JackB 2:030b4c500fce 96 #elif defined(TARGET_ARCH_PRO)
JackB 2:030b4c500fce 97 printf("Seeed Arch PRO\r\n");
JackB 2:030b4c500fce 98 #elif defined(TARGET_ARCH_MAX)
JackB 2:030b4c500fce 99 printf("Seeed Arch Max\r\n");
JackB 2:030b4c500fce 100 #elif defined(TARGET_LPC4337)
JackB 2:030b4c500fce 101 printf("LPC4337\r\n");
JackB 2:030b4c500fce 102 #elif defined(TARGET_DISCO_F746NG)
JackB 2:030b4c500fce 103 printf("DISCO_F746NG\r\n");
JackB 2:030b4c500fce 104 #else
JackB 2:030b4c500fce 105 printf("Unknown\r\n");
JackB 2:030b4c500fce 106 #endif
JackB 2:030b4c500fce 107
JackB 2:030b4c500fce 108 printf("CPU Clock: %3.3f MHz\r\n", ((float)SystemCoreClock/1000000.0));
JackB 2:030b4c500fce 109
JackB 2:030b4c500fce 110 printf("MAC : ");
JackB 2:030b4c500fce 111 char mac_board[6];
JackB 2:030b4c500fce 112 mbed_mac_address(mac_board);
JackB 2:030b4c500fce 113 for(int i=0; i<6;i++) {
JackB 2:030b4c500fce 114 printf("%02X", mac_board[i]);
JackB 2:030b4c500fce 115 if (i < 5) printf(":");
JackB 2:030b4c500fce 116 }
JackB 2:030b4c500fce 117 printf("\r\n");
JackB 2:030b4c500fce 118
JackB 2:030b4c500fce 119
JackB 2:030b4c500fce 120 printf("mbed cc3000 NTP client demo.\r\n");
JackB 2:030b4c500fce 121
JackB 2:030b4c500fce 122 #if (DHCP == 1)
JackB 2:030b4c500fce 123 printf("Initialize the interface using DHCP...\r\n");
JackB 2:030b4c500fce 124 printf("wifi.init() ");
JackB 2:030b4c500fce 125 wifi.init();
JackB 2:030b4c500fce 126 #else
JackB 2:030b4c500fce 127 printf("Initialize the interface using a static IP address...\r\n");
JackB 2:030b4c500fce 128 printf("wifi.init(%s, %s, %s) ", IP, MASK, GW);
JackB 2:030b4c500fce 129 wifi.init(IP, MASK, GW);
JackB 2:030b4c500fce 130 #endif
JackB 2:030b4c500fce 131 printf("done.\r\n");
JackB 2:030b4c500fce 132
JackB 2:030b4c500fce 133 printf("Read from cc3000...\r\n");
JackB 2:030b4c500fce 134
JackB 2:030b4c500fce 135 // Read Firmware Version and MAC Address
JackB 2:030b4c500fce 136 FW_status = wifi.read_sp_version(firmware_ver); // read actual Firmware version
JackB 2:030b4c500fce 137 if(FW_status == 0){
JackB 2:030b4c500fce 138 printf("Firmware version: %d.%d\r\n", firmware_ver[0], firmware_ver[1]);
JackB 2:030b4c500fce 139 mac_status = wifi.get_mac_address(cMacFromEeprom);
JackB 2:030b4c500fce 140 if(mac_status == 0){
JackB 2:030b4c500fce 141 printf("MAC address : ");
JackB 2:030b4c500fce 142 for(int i = 0; i < 6; i++) {
JackB 2:030b4c500fce 143 printf("%02X", cMacFromEeprom[i]);
JackB 2:030b4c500fce 144 if (i < 5) printf(":");
JackB 2:030b4c500fce 145 }
JackB 2:030b4c500fce 146 printf("\r\n");
JackB 2:030b4c500fce 147 }
JackB 2:030b4c500fce 148 } else {
JackB 2:030b4c500fce 149 printf("ERROR: CC3000 not found - check connections !\r\n");
JackB 2:030b4c500fce 150 }
JackB 2:030b4c500fce 151
JackB 2:030b4c500fce 152 // WiFi parameters
JackB 2:030b4c500fce 153 printf("WiFi parameters:\r\n");
JackB 2:030b4c500fce 154 printf("SSID : %s\r\n", SSID);
JackB 2:030b4c500fce 155 printf("Phrase : %s\r\n", PHRASE);
JackB 2:030b4c500fce 156 printf("Security : ");
JackB 2:030b4c500fce 157 if (SECURITY == 0) printf("NONE");
JackB 2:030b4c500fce 158 if (SECURITY == 1) printf("WEP");
JackB 2:030b4c500fce 159 if (SECURITY == 2) printf("WPA");
JackB 2:030b4c500fce 160 if (SECURITY == 3) printf("WPA2");
JackB 2:030b4c500fce 161 printf("\r\n");
JackB 2:030b4c500fce 162
JackB 2:030b4c500fce 163 wait(1.0);
JackB 2:030b4c500fce 164
JackB 2:030b4c500fce 165 // Connect to WiFi
JackB 2:030b4c500fce 166 printf("Bring the interface up...\r\n");
JackB 2:030b4c500fce 167 if (wifi.connect() == -1) {
JackB 2:030b4c500fce 168 printf("ERROR: Failed to connect. Please verify connection details and try again.\r\n");
JackB 2:030b4c500fce 169 }
JackB 2:030b4c500fce 170 char *ip = wifi.getIPAddress();
JackB 2:030b4c500fce 171 char *mask = wifi.getNetworkMask();
JackB 2:030b4c500fce 172 char *gate = wifi.getGateway();
JackB 2:030b4c500fce 173 char *mac = wifi.getMACAddress();
JackB 2:030b4c500fce 174 bool conn = wifi.is_connected();
JackB 2:030b4c500fce 175 bool dhcp = wifi.is_dhcp_configured();
JackB 2:030b4c500fce 176 bool enabled = wifi.is_enabled();
JackB 2:030b4c500fce 177
JackB 2:030b4c500fce 178 printf("IP : %s\r\n", ip);
JackB 2:030b4c500fce 179 printf("Netmask : %s\r\n", mask);
JackB 2:030b4c500fce 180 printf("Gateway : %s\r\n", gate);
JackB 2:030b4c500fce 181 printf("MAC : %s\r\n", mac);
JackB 2:030b4c500fce 182 printf("Connected : %d\r\n", conn);
JackB 2:030b4c500fce 183 printf("DHCP : %d\r\n", dhcp);
JackB 2:030b4c500fce 184 printf("Enabled : %d\r\n", enabled);
JackB 2:030b4c500fce 185
JackB 2:030b4c500fce 186 wait(1.0);
JackB 2:030b4c500fce 187
JackB 2:030b4c500fce 188
JackB 2:030b4c500fce 189 // Read time from server
JackB 2:030b4c500fce 190 NTPClient ntp_client;
JackB 2:030b4c500fce 191 // NTP Server Parameters
JackB 2:030b4c500fce 192 // char* domain_name = "217.114.59.3";
JackB 2:030b4c500fce 193 char* domain_name = "2.nl.pool.ntp.org";
JackB 2:030b4c500fce 194 // char* domain_name = "0.uk.pool.ntp.org";
JackB 2:030b4c500fce 195 int port_number = 123;
JackB 2:030b4c500fce 196 time_t ct_time;
JackB 2:030b4c500fce 197 char time_buffer[80];
JackB 2:030b4c500fce 198 char time_buffer_old[80];
JackB 2:030b4c500fce 199
JackB 2:030b4c500fce 200 printf("Reading time: ");
JackB 2:030b4c500fce 201 if (ntp_client.setTime(domain_name, port_number) == 0) {
JackB 2:030b4c500fce 202 printf("Passed.\r\n");
JackB 2:030b4c500fce 203 ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time
JackB 2:030b4c500fce 204 set_time(ct_time);
JackB 2:030b4c500fce 205 strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
JackB 2:030b4c500fce 206 printf("Time : %s\r\n", time_buffer);
JackB 2:030b4c500fce 207 } else {
JackB 2:030b4c500fce 208 printf("FAILED!\r\n");
JackB 2:030b4c500fce 209 }
JackB 2:030b4c500fce 210
JackB 2:030b4c500fce 211 strcpy(time_buffer_old, "Dummy value");
JackB 2:030b4c500fce 212
JackB 2:030b4c500fce 213
bcostm 0:9933f7db9a9b 214 TS_StateTypeDef TS_State;
bcostm 0:9933f7db9a9b 215 uint16_t x, y;
bcostm 0:9933f7db9a9b 216 uint8_t text[30];
bcostm 0:9933f7db9a9b 217 uint8_t status;
bcostm 0:9933f7db9a9b 218 uint8_t idx;
bcostm 0:9933f7db9a9b 219 uint8_t cleared = 0;
bcostm 0:9933f7db9a9b 220 uint8_t prev_nb_touches = 0;
bcostm 0:9933f7db9a9b 221
bcostm 0:9933f7db9a9b 222 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
bcostm 0:9933f7db9a9b 223 wait(1);
bcostm 0:9933f7db9a9b 224
bcostm 0:9933f7db9a9b 225 status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
bcostm 0:9933f7db9a9b 226 if (status != TS_OK) {
bcostm 0:9933f7db9a9b 227 lcd.Clear(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 228 lcd.SetBackColor(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 229 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 230 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
bcostm 0:9933f7db9a9b 231 } else {
bcostm 0:9933f7db9a9b 232 lcd.Clear(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 233 lcd.SetBackColor(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 234 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 235 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
bcostm 0:9933f7db9a9b 236 }
bcostm 0:9933f7db9a9b 237
bcostm 0:9933f7db9a9b 238 wait(1);
bcostm 0:9933f7db9a9b 239 lcd.SetFont(&Font12);
bcostm 0:9933f7db9a9b 240 lcd.SetBackColor(LCD_COLOR_BLUE);
bcostm 0:9933f7db9a9b 241 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 242
bcostm 0:9933f7db9a9b 243 while(1) {
bcostm 0:9933f7db9a9b 244
JackB 2:030b4c500fce 245 ct_time = time(NULL);
JackB 2:030b4c500fce 246 // strftime(time_buffer, 80, "%S", localtime(&ct_time));
JackB 2:030b4c500fce 247 strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
JackB 2:030b4c500fce 248 if (strcmp(time_buffer, time_buffer_old) != 0) {
JackB 2:030b4c500fce 249 strcpy(time_buffer_old, time_buffer);
JackB 2:030b4c500fce 250 printf("%s\r\n", time_buffer);
JackB 2:030b4c500fce 251 lcd.DisplayStringAt(0, LINE(15), (uint8_t *)time_buffer, CENTER_MODE);
JackB 2:030b4c500fce 252 strftime(time_buffer, 80, "%T", localtime(&ct_time));
JackB 2:030b4c500fce 253 // 21:00:00
JackB 2:030b4c500fce 254 if ((time_buffer[3] == '0') && (time_buffer[4] == '0') &&
JackB 2:030b4c500fce 255 (time_buffer[6] == '0') && (time_buffer[7] == '0')) {
JackB 2:030b4c500fce 256 // Sync once per hour
JackB 2:030b4c500fce 257 printf("Reading time: ");
JackB 2:030b4c500fce 258 if (ntp_client.setTime(domain_name, port_number) == 0) {
JackB 2:030b4c500fce 259 printf("Passed.\r\n");
JackB 2:030b4c500fce 260 ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time
JackB 2:030b4c500fce 261 set_time(ct_time);
JackB 2:030b4c500fce 262 strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
JackB 2:030b4c500fce 263 // printf("Time : %s\r\n", time_buffer);
JackB 2:030b4c500fce 264 lcd.DisplayStringAt(0, LINE(15), (uint8_t *)time_buffer, CENTER_MODE);
JackB 2:030b4c500fce 265 } else {
JackB 2:030b4c500fce 266 printf("FAILED!\r\n");
JackB 2:030b4c500fce 267 }
JackB 2:030b4c500fce 268 }
JackB 2:030b4c500fce 269 }
JackB 2:030b4c500fce 270
bcostm 0:9933f7db9a9b 271 ts.GetState(&TS_State);
bcostm 0:9933f7db9a9b 272 if (TS_State.touchDetected) {
bcostm 0:9933f7db9a9b 273 // Clear lines corresponding to old touches coordinates
bcostm 0:9933f7db9a9b 274 if (TS_State.touchDetected < prev_nb_touches) {
bcostm 0:9933f7db9a9b 275 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
bcostm 0:9933f7db9a9b 276 lcd.ClearStringLine(idx);
bcostm 0:9933f7db9a9b 277 }
bcostm 0:9933f7db9a9b 278 }
bcostm 0:9933f7db9a9b 279 prev_nb_touches = TS_State.touchDetected;
bcostm 0:9933f7db9a9b 280
bcostm 0:9933f7db9a9b 281 cleared = 0;
bcostm 0:9933f7db9a9b 282
bcostm 0:9933f7db9a9b 283 sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
bcostm 0:9933f7db9a9b 284 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 285
bcostm 0:9933f7db9a9b 286 for (idx = 0; idx < TS_State.touchDetected; idx++) {
bcostm 0:9933f7db9a9b 287 x = TS_State.touchX[idx];
bcostm 0:9933f7db9a9b 288 y = TS_State.touchY[idx];
bcostm 0:9933f7db9a9b 289 sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
bcostm 0:9933f7db9a9b 290 lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 291 }
bcostm 0:9933f7db9a9b 292
bcostm 0:9933f7db9a9b 293 lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
bcostm 0:9933f7db9a9b 294 } else {
bcostm 0:9933f7db9a9b 295 if (!cleared) {
bcostm 0:9933f7db9a9b 296 lcd.Clear(LCD_COLOR_BLUE);
bcostm 0:9933f7db9a9b 297 sprintf((char*)text, "Touches: 0");
bcostm 0:9933f7db9a9b 298 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 299 cleared = 1;
bcostm 0:9933f7db9a9b 300 }
bcostm 0:9933f7db9a9b 301 }
bcostm 0:9933f7db9a9b 302 }
bcostm 0:9933f7db9a9b 303 }