+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
b3e97e8
master
1// ------------------------------------------------------------------- 2// File name: esp8266.c 3// Description: This starter code is used to bridge the TM4C123 board 4// and the Blynk Application via the ESP8266. 5// 6// Authors: Mark McDermott 7// Converted to EE445L style Jonathan Valvano 8// Date: May 21, 2018 9// Last update: Sept 20, 2018 10// 11// TM4C123 ESP8266-ESP01 (2 by 4 header) 12// PE5 (U5TX) to Pin 1 (Rx) 13// PE4 (U5RX) to Pin 5 (TX) 14// PE3 output debugging 15// PE2 nc 16// PE1 output Pin 7 Reset 17// PE0 input Pin 3 Rdy IO2 18// Pin 2 IO0, 10k pullup to 3.3V 19// Pin 8 Vcc, 3.3V (separate supply from LaunchPad) 20// Gnd Pin 4 Gnd 21// Place a 4.7uF tantalum and 0.1 ceramic next to ESP8266 3.3V power pin 22// Use LM2937-3.3 and two 4.7 uF capacitors to convert USB +5V to 3.3V for the ESP8266 23// http://www.ti.com/lit/ds/symlink/lm2937-3.3.pdf 24#include <stdint.h> 25#include <string.h> 26#include "tm4c123gh6pm.h" 27#include "UART.h" 28#include "ST7735.h" 29#include "esp8266.h" 30// the following two lines connect you to the internet 31char ssid [32 ]= "EE-IOT-Platform-03" ; 32char pass [32 ]= "g!TyA>hR2JTy" ; 33// create your own Blynk server app and edit this next line with your authentication code 34char auth [64 ]= "izpLwRbVnJK-xnC1LD17lvm09c9V5I0m" ; 35 36#define UART_FR_RXFF 0x00000040 // UART Receive FIFO Full 37#define UART_FR_TXFF 0x00000020 // UART Transmit FIFO Full 38#define UART_FR_RXFE 0x00000010 // UART Receive FIFO Empty 39#define UART_LCRH_WLEN_8 0x00000060 // 8 bit word length 40#define UART_LCRH_FEN 0x00000010 // UART Enable FIFOs 41#define UART_CTL_UARTEN 0x00000001 // UART Enable 42#define UART_IFLS_RX1_8 0x00000000 // RX FIFO >= 1/8 full 43#define UART_IFLS_TX1_8 0x00000000 // TX FIFO <= 1/8 full 44#define UART_IM_RTIM 0x00000040 // UART Receive Time-Out Interrupt 45// Mask 46#define UART_IM_TXIM 0x00000020 // UART Transmit Interrupt Mask 47#define UART_IM_RXIM 0x00000010 // UART Receive Interrupt Mask 48#define UART_RIS_RTRIS 0x00000040 // UART Receive Time-Out Raw 49// Interrupt Status 50#define UART_RIS_TXRIS 0x00000020 // UART Transmit Raw Interrupt 51// Status 52#define UART_RIS_RXRIS 0x00000010 // UART Receive Raw Interrupt 53// Status 54#define UART_ICR_RTIC 0x00000040 // Receive Time-Out Interrupt Clear 55#define UART_ICR_TXIC 0x00000020 // Transmit Interrupt Clear 56#define UART_ICR_RXIC 0x00000010 // Receive Interrupt Clear 57 58 59void DisableInterrupts (void );// Disable interrupts 60void EnableInterrupts (void );// Enable interrupts 61long StartCritical (void );// previous I bit, disable interrupts 62void EndCritical (long sr );// restore I bit to previous value 63void WaitForInterrupt (void );// low power mode 64 65#define FIFOSUCCESS 1 // return value on success 66#define FIFOFAIL 0 // return value on failure 67// create index implementation FIFO (see FIFO.h) 68// Two-index implementation of the receive FIFO 69// can hold 0 to RX5FIFOSIZE messages 70#define RX5FIFOSIZE 8 // must be a power of 2 71#define MESSAGESIZE 64 72uint32_t volatile Rx5PutI ;// put next message 73uint32_t volatile Rx5PutJ ;// put next character 0 to MESSAGESIZE-1 74uint32_t volatile Rx5GetI ;// get next message 75char Rx5Fifo [RX5FIFOSIZE ][MESSAGESIZE ];// each message can have up to MESSAGESIZE-1 characters 76char RxMessage [MESSAGESIZE ]; 77// initialize index FIFO 78void Rx5Fifo_Init (void ){long sr ; 79sr = StartCritical ();// make atomic 80Rx5PutI = Rx5GetI = Rx5PutJ = 0 ;// Empty 81EndCritical (sr ); 82} 83// add element to end of index FIFO 84// return TXFIFOSUCCESS if successful 85int Rx5Fifo_Put (char data ){ 86if ((Rx5PutI - Rx5GetI )& ~(RX5FIFOSIZE - 1 )){ 87return (FIFOFAIL );// Failed, fifo full 88 } 89Rx5Fifo [Rx5PutI & (RX5FIFOSIZE - 1 )][Rx5PutJ ]= data ;// put 90if (data != '\n' ){ 91if (Rx5PutJ == (MESSAGESIZE - 3 )){ 92// you should never get here! 93Rx5Fifo [Rx5PutI & (RX5FIFOSIZE - 1 )][MESSAGESIZE - 2 ]= ',' ; 94Rx5Fifo [Rx5PutI & (RX5FIFOSIZE - 1 )][MESSAGESIZE - 1 ]= '\n' ;// message overflow, force a '\n' 95Rx5PutI ++ ;// end of message 96Rx5PutJ = 0 ;// get ready for new message 97return (FIFOSUCCESS );// bad situation, message buffer overflow 98 } 99Rx5PutJ ++ ;// Success, update 100return (FIFOSUCCESS ); 101 } 102Rx5Fifo [Rx5PutI & (RX5FIFOSIZE - 1 )][Rx5PutJ ]= ',' ; 103Rx5Fifo [Rx5PutI & (RX5FIFOSIZE - 1 )][Rx5PutJ + 1 ]= '\n' ; 104Rx5PutI ++ ;// end of message 105Rx5PutJ = 0 ;// get ready for new message 106return (FIFOSUCCESS ); 107} 108// remove a message from front of index FIFO 109// datapt points to an empty data buffer of MESSAGESIZE character 110// if successful a message is copied from the FIFO into data buffer 111// return FIFOSUCCESS if successful 112// return FIFOFAIL if the FIFO is empty (no messages) 113int ESP8266_GetMessage (char * datapt ){char data ;int j ; 114if (Rx5PutI == Rx5GetI ){ 115return (FIFOFAIL );// Empty if PutI=GetI 116 } 117j = 0 ; 118do { 119data = Rx5Fifo [Rx5GetI & (RX5FIFOSIZE - 1 )][j ]; 120datapt [j ]= data ; 121j ++ ; 122 }while ((j < MESSAGESIZE )&& (data != '\n' )); 123Rx5GetI ++ ;// Success, update 124return (FIFOSUCCESS ); 125} 126// number of messages in index FIFO 127// 0 to RX5FIFOSIZE-1 128uint32_t Rx5Fifo_Size (void ){ 129return ((uint32_t )(Rx5PutI - Rx5GetI )); 130} 131 132// Two-index implementation of the transmit FIFO 133// can hold 0 to TX5FIFOSIZE elements 134#define TX5FIFOSIZE 256 // must be a power of 2 135uint32_t volatile Tx5PutI ;// put next 136uint32_t volatile Tx5GetI ;// get next 137char static Tx5Fifo [TX5FIFOSIZE ]; 138 139// initialize index FIFO 140void Tx5Fifo_Init (void ){long sr ; 141sr = StartCritical ();// make atomic 142Tx5PutI = Tx5GetI = 0 ;// Empty 143EndCritical (sr ); 144} 145// add element to end of index FIFO 146// return TXFIFOSUCCESS if successful 147int Tx5Fifo_Put (char data ){ 148if ((Tx5PutI - Tx5GetI )& ~(TX5FIFOSIZE - 1 )){ 149return (FIFOFAIL );// Failed, fifo full 150 } 151Tx5Fifo [Tx5PutI & (TX5FIFOSIZE - 1 )]= data ;// put 152Tx5PutI ++ ;// Success, update 153return (FIFOSUCCESS ); 154} 155// remove element from front of index FIFO 156// return TXFIFOSUCCESS if successful 157int Tx5Fifo_Get (char * datapt ){ 158if (Tx5PutI == Tx5GetI ){ 159return (FIFOFAIL );// Empty if TxPutI=TxGetI 160 } 161* datapt = Tx5Fifo [Tx5GetI & (TX5FIFOSIZE - 1 )]; 162Tx5GetI ++ ;// Success, update 163return (FIFOSUCCESS ); 164} 165// number of elements in index FIFO 166// 0 to TX5FIFOSIZE-1 167unsigned long Tx5Fifo_Size (void ){ 168return ((unsigned long )(Tx5PutI - Tx5GetI )); 169} 170 171// Initialize UART5, interrupt driven 172// Baud rate is 5,000,000/(ibrd+fbrd/64) 173// ESP8266 needs 9600 bits/sec 174// PE5=U5Tx,PE4=U5Rx UART5, bottom board 175// inputs: ibrd,fbrd 176// priority 0 to 5 allowed 177// outputs: none 178void UART5_Init (uint32_t priority ){ 179if (priority > 7 ){ 180priority = 7 ; 181 } 182SYSCTL_RCGCUART_R |=0x20 ;// activate UART5 183SYSCTL_RCGCGPIO_R |=0x10 ;// activate port E 184Rx5Fifo_Init ();// initialize empty FIFOs 185Tx5Fifo_Init (); 186UART5_CTL_R &= ~UART_CTL_UARTEN ;// disable UART 187 188UART5_IBRD_R = 520 ;// IBRD = int(80,000,000 / (16 * 9600)) = int(520.833) 189UART5_FBRD_R = 53 ;// FBRD = round(0.833 * 64) = 53 190// 8 bit word length (no parity bits, one stop bit, FIFOs) 191UART5_LCRH_R = (UART_LCRH_WLEN_8 |UART_LCRH_FEN ); 192UART5_IFLS_R &= ~0x3F ;// clear TX and RX interrupt FIFO level fields 193// configure interrupt for TX FIFO <= 1/8 full 194// configure interrupt for RX FIFO >= 1/8 full 195UART5_IFLS_R += (UART_IFLS_TX1_8 |UART_IFLS_RX1_8 ); 196// enable TX and RX FIFO interrupts and RX time-out interrupt 197UART5_IM_R |= (UART_IM_RXIM |UART_IM_TXIM |UART_IM_RTIM ); 198UART5_CTL_R |=UART_CTL_UARTEN ;// enable UART 199GPIO_PORTE_AFSEL_R |=0x30 ;// enable alt funct on PE5-4 200GPIO_PORTE_DEN_R |=0x30 ;// enable digital I/O on PE5-4 201// configure PE5-4 as UART 202GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R & 0xFF00FFFF )+ 0x00110000 ; 203GPIO_PORTE_AMSEL_R &= ~0x30 ;// disable analog functionality on PE 204// set priority 205NVIC_PRI15_R = (NVIC_PRI15_R & 0xFFFF00FF )|(priority <<13 );// bits 13-15 206//interrupt 61, n=15, Bits 15:13 Interrupt [4n+1] 207NVIC_EN1_R = 0x01 <<(61 - 32 );// enable interrupt 61 in NVIC 208} 209 210// copy from hardware RX FIFO to software RX FIFO 211// stop when hardware RX FIFO is empty or software RX FIFO is full 212void static copyHardwareToSoftware5 (void ){ 213char letter ; 214while (((UART5_FR_R & UART_FR_RXFE )== 0 )&& (Rx5Fifo_Size ()< (RX5FIFOSIZE - 1 ))){ 215letter = UART5_DR_R ; 216Rx5Fifo_Put (letter ); 217 } 218} 219// copy from software TX FIFO to hardware TX FIFO 220// stop when software TX FIFO is empty or hardware TX FIFO is full 221void static copySoftwareToHardware5 (void ){ 222char letter ; 223while (((UART5_FR_R & UART_FR_TXFF )== 0 )&& (Tx5Fifo_Size ()> 0 )){ 224Tx5Fifo_Get (& letter ); 225UART5_DR_R = letter ; 226 } 227} 228 229// Are there any received messages to be input? 230// return the number of characters available to be read 231uint32_t ESP8266_AvailableInput (void ){ 232return Rx5Fifo_Size (); 233} 234// output ASCII character to UART 235// spin if TxFifo is full 236void ESP8266_OutChar (char data ){ 237while (Tx5Fifo_Put (data )== FIFOFAIL ){}; 238UART5_IM_R &= ~UART_IM_TXIM ;// disable TX FIFO interrupt 239copySoftwareToHardware5 (); 240UART5_IM_R |=UART_IM_TXIM ;// enable TX FIFO interrupt 241} 242// at least one of three things has happened: 243// hardware TX FIFO goes from 3 to 2 or less items 244// hardware RX FIFO goes from 1 to 2 or more items 245// UART receiver has timed out 246void UART5_Handler (void ){ 247if (UART5_RIS_R & UART_RIS_TXRIS ){// hardware TX FIFO <= 2 items 248UART5_ICR_R = UART_ICR_TXIC ;// acknowledge TX FIFO 249// copy from software TX FIFO to hardware TX FIFO 250copySoftwareToHardware5 (); 251if (Tx5Fifo_Size ()== 0 ){// software TX FIFO is empty 252UART5_IM_R &= ~UART_IM_TXIM ;// disable TX FIFO interrupt 253 } 254 } 255if (UART5_RIS_R & UART_RIS_RXRIS ){// hardware RX FIFO >= 2 items 256UART5_ICR_R = UART_ICR_RXIC ;// acknowledge RX FIFO 257// copy from hardware RX FIFO to software RX FIFO 258copyHardwareToSoftware5 (); 259 } 260if (UART5_RIS_R & UART_RIS_RTRIS ){// receiver timed out 261UART5_ICR_R = UART_ICR_RTIC ;// acknowledge receiver time out 262// copy from hardware RX FIFO to software RX FIFO 263copyHardwareToSoftware5 (); 264 } 265} 266 267//------------ESP8266_OutString------------ 268// Output String (NULL termination) 269// Input: pointer to a NULL-terminated string to be transferred 270// Output: none 271void ESP8266_OutString (char * pt ){ 272while (* pt ){ 273ESP8266_OutChar (* pt ); 274pt ++ ; 275 } 276} 277 278//-----------------------ESP8266_OutUDec----------------------- 279// Output a 32-bit number in unsigned decimal format 280// Input: 32-bit number to be transferred 281// Output: none 282// Variable format 1-10 digits with no space before or after 283void ESP8266_OutUDec (uint32_t n ){ 284// This function uses recursion to convert decimal number 285// of unspecified length as an ASCII string 286if (n >=10 ){ 287ESP8266_OutUDec (n /10 ); 288n = n %10 ; 289 } 290ESP8266_OutChar (n + '0' );/* n is between 0 and 9 */ 291} 292 293 294// DelayMs 295// - busy wait n milliseconds 296// Input: time to wait in msec 297// Outputs: none 298void DelayMs (uint32_t n ){ 299volatile uint32_t time ; 300while (n ){ 301time = 6665 ;// 1msec, tuned at 80 MHz 302while (time ){ 303time -- ; 304 } 305n -- ; 306 } 307} 308 309#define PE0 (*((volatile uint32_t *)0x40024004)) // RDY from ESP 8266 310#define PE1 (*((volatile uint32_t *)0x40024008)) // RST_B to ESP8266 311#define PE3 (*((volatile uint32_t *)0x40024020)) // LED to indicate that the 8266 connected 312 313#define RDY PE0 314#define RDY1 0x01 315#define RST PE1 316#define RST1 0x02 317#define LOW 0x00 318 319#define BIT0 0x01 320#define BIT1 0x02 321#define BIT2 0x04 322#define BIT3 0x08 323 324// Initialize PE5,4,3,1,0 for interface to ESP8266 325// Uses interrupt driven UART5 on PE5,4 326// Uses simple GPIO output on PE3,1,0 327void ESP8266_Init (void ){ 328SYSCTL_RCGCGPIO_R |=0x10 ;// activate port E 329while ((SYSCTL_PRGPIO_R & 0x10 )== 0 ){}; 330GPIO_PORTE_DIR_R |=0x0A ;// output digital I/O on PE3,1 331GPIO_PORTE_DIR_R &= ~0x01 ;// input digital I/O on PE0 332GPIO_PORTE_AFSEL_R &= ~0x0B ;// disable alt funct on PE3,1,0 333GPIO_PORTE_DEN_R |=0x0B ;// enable digital I/O on PE3,1,0 334GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R & 0xFFFF0F00 ); 335GPIO_PORTE_AMSEL_R &= ~0x0B ;// disable analog functionality on PE3,1,0 336UART5_Init (2 );// Enable ESP8266 Serial Port 337EnableInterrupts (); 338} 339// Initialize ESP8266 interface and reset the 340void ESP8266_Reset (void ) { 341#ifdef DEBUG1 342UART_OutString ("In Reset_8266 routine\r\n" ); 343UART_OutString ("Reset pin being held low for 5 sec\r\n" ); 344#endif 345RST = LOW ;// Reset the 8266 346DelayMs (5000 );// Wait for 8266 to reset 347RST = RST1 ;// Enable the 8266 348#ifdef DEBUG1 349UART_OutString ("Reset pin is now high for 5 sec\r\n" ); 350UART_OutString ("Waiting for ESP to finish initializing\r\n" ); 351#endif 352DelayMs (5000 );// Wait before setting up 8266 353#ifdef DEBUG1 354UART_OutString ("Leaving Reset_8266 routine\r\n" ); 355#endif 356PE3 = LOW ;// Turn off 357} 358 359// ---------------------------------------------------------------------- 360// This routine sets up the Wifi connection between the TM4C and the 361// hotspot. Enable the DEBUG flags in esp8266.h if you want to watch the transactions. 362void ESP8266_SetupWiFi (void ) { 363#ifdef DEBUG1 364UART_OutString ("\r\nIn WiFI_Setup routine\r\n" ); 365UART_OutString ("Waiting for RDY flag from ESP\r\n" ); 366#endif 367 368#ifdef DEBUG3 369Output_Color (ST7735_YELLOW ); 370ST7735_OutString ("In WiFI_Setup routine\n" ); 371ST7735_OutString ("Waiting for RDY flag\n" ); 372#endif 373while (!RDY ) {// Wait for ESP8266 indicate it is ready for programming data 374#ifdef DEBUG1 375UART_OutString ("." ); 376#endif 377DelayMs (1000 ); 378 } 379ESP8266_OutString (auth );// Send authorization code 380ESP8266_OutChar (',' ); 381ESP8266_OutString (ssid ); 382ESP8266_OutChar (',' ); 383ESP8266_OutString (pass ); 384ESP8266_OutChar (',' );// Extra comma needed for 8266 parser code 385ESP8266_OutChar ('\n' );// Send NL to indicate EOT 386 387#ifdef DEBUG1 388UART_OutString (auth ); 389UART_OutChar (',' ); 390UART_OutString (ssid ); 391UART_OutChar (',' ); 392UART_OutString (pass ); 393UART_OutString (",\n\r" ); 394#endif 395#ifdef DEBUG3 396Output_Color (ST7735_WHITE ); 397ST7735_OutString (ssid );ST7735_OutChar ('\n' ); 398ST7735_OutString (pass );ST7735_OutChar ('\n' ); 399ST7735_OutString (auth );ST7735_OutChar ('\n' ); 400#endif 401 402// 403// This while loop receives debug info from the 8266 and optionally 404// sends it out the debug port. The loop exits once the RDY signal 405// is deasserted and the serial port has no more character to xmit 406// 407#ifdef DEBUG1 408UART_OutString ("\n\rWaiting for RDY to go low\n\r" ); 409#endif 410while (RDY ){// pause while RDY=1 411#ifdef DEBUG1 412UART_OutString ("." ); 413#endif 414DelayMs (500 ); 415 } 416while (ESP8266_GetMessage (RxMessage )){ 417 } 418#ifdef DEBUG1 419UART_OutString ("\n\rRDY went low\n\r" ); 420#endif 421Rx5Fifo_Init ();// flush buffer 422 423#ifdef DEBUG3 424Output_Color (ST7735_YELLOW ); 425ST7735_OutString ("Exiting WiFI_Setup\nReady to talk\n" ); 426#endif 427#ifdef DEBUG1 428UART_OutString ("Exiting TM4C WiFI_Setup routine\r\nReady to talk\r\n" ); 429#endif 430PE3 = BIT3 ; 431} 432 433