+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

Joseph Ryanstart lab3 reportb3e97e8

master
15.7 KiB433 linesraw
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;
79  sr = StartCritical(); // make atomic
80  Rx5PutI = Rx5GetI = Rx5PutJ = 0;  // Empty
81  EndCritical(sr);
82}
83// add element to end of index FIFO
84// return TXFIFOSUCCESS if successful
85int Rx5Fifo_Put(char data){
86  if((Rx5PutI-Rx5GetI) & ~(RX5FIFOSIZE-1)){
87    return(FIFOFAIL); // Failed, fifo full
88  }
89  Rx5Fifo[Rx5PutI&(RX5FIFOSIZE-1)][Rx5PutJ] = data; // put
90  if(data != '\n'){
91    if(Rx5PutJ == (MESSAGESIZE-3)){
92      // you should never get here!
93      Rx5Fifo[Rx5PutI&(RX5FIFOSIZE-1)][MESSAGESIZE-2] = ',';
94      Rx5Fifo[Rx5PutI&(RX5FIFOSIZE-1)][MESSAGESIZE-1] = '\n'; // message overflow, force a '\n'
95      Rx5PutI++;   // end of message
96      Rx5PutJ = 0; // get ready for new message
97      return(FIFOSUCCESS); // bad situation, message buffer overflow
98    }
99    Rx5PutJ++;  // Success, update
100    return(FIFOSUCCESS);
101  }
102  Rx5Fifo[Rx5PutI&(RX5FIFOSIZE-1)][Rx5PutJ] = ',';
103  Rx5Fifo[Rx5PutI&(RX5FIFOSIZE-1)][Rx5PutJ+1] = '\n';
104  Rx5PutI++;   // end of message
105  Rx5PutJ = 0; // get ready for new message
106  return(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;
114  if(Rx5PutI == Rx5GetI ){
115    return(FIFOFAIL); // Empty if PutI=GetI
116  }
117  j = 0;
118  do{
119    data = Rx5Fifo[Rx5GetI&(RX5FIFOSIZE-1)][j];
120    datapt[j] = data;
121    j++;
122  }while((j<MESSAGESIZE)&&(data != '\n'));
123  Rx5GetI++;  // Success, update
124  return(FIFOSUCCESS);
125}
126// number of messages in index FIFO
127// 0 to RX5FIFOSIZE-1
128uint32_t Rx5Fifo_Size(void){
129 return ((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;
141  sr = StartCritical(); // make atomic
142  Tx5PutI = Tx5GetI = 0;  // Empty
143  EndCritical(sr);
144}
145// add element to end of index FIFO
146// return TXFIFOSUCCESS if successful
147int Tx5Fifo_Put(char data){
148  if((Tx5PutI-Tx5GetI) & ~(TX5FIFOSIZE-1)){
149    return(FIFOFAIL); // Failed, fifo full
150  }
151  Tx5Fifo[Tx5PutI&(TX5FIFOSIZE-1)] = data; // put
152  Tx5PutI++;  // Success, update
153  return(FIFOSUCCESS);
154}
155// remove element from front of index FIFO
156// return TXFIFOSUCCESS if successful
157int Tx5Fifo_Get(char *datapt){
158  if(Tx5PutI == Tx5GetI ){
159    return(FIFOFAIL); // Empty if TxPutI=TxGetI
160  }
161  *datapt = Tx5Fifo[Tx5GetI&(TX5FIFOSIZE-1)];
162  Tx5GetI++;  // Success, update
163  return(FIFOSUCCESS);
164}
165// number of elements in index FIFO
166// 0 to TX5FIFOSIZE-1
167unsigned long Tx5Fifo_Size(void){
168 return ((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){
179  if(priority>7){
180    priority = 7;
181  } 
182  SYSCTL_RCGCUART_R |= 0x20; // activate UART5
183  SYSCTL_RCGCGPIO_R |= 0x10; // activate port E
184  Rx5Fifo_Init();                       // initialize empty FIFOs
185  Tx5Fifo_Init();
186  UART5_CTL_R &= ~UART_CTL_UARTEN;      // disable UART
187         
188  UART5_IBRD_R = 520;                   // IBRD = int(80,000,000 / (16 * 9600)) = int(520.833)
189  UART5_FBRD_R = 53;                    // FBRD = round(0.833 * 64) = 53
190                                        // 8 bit word length (no parity bits, one stop bit, FIFOs)
191  UART5_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
192  UART5_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
195  UART5_IFLS_R += (UART_IFLS_TX1_8|UART_IFLS_RX1_8);
196                                        // enable TX and RX FIFO interrupts and RX time-out interrupt
197  UART5_IM_R |= (UART_IM_RXIM|UART_IM_TXIM|UART_IM_RTIM);
198  UART5_CTL_R |= UART_CTL_UARTEN;       // enable UART
199  GPIO_PORTE_AFSEL_R |= 0x30;           // enable alt funct on PE5-4
200  GPIO_PORTE_DEN_R |= 0x30;             // enable digital I/O on PE5-4
201                                        // configure PE5-4 as UART
202  GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)+0x00110000;
203  GPIO_PORTE_AMSEL_R &= ~0x30;          // disable analog functionality on PE
204                                        // set priority
205  NVIC_PRI15_R = (NVIC_PRI15_R&0xFFFF00FF)|(priority<<13); // bits 13-15
206  //interrupt 61, n=15, Bits 15:13 Interrupt [4n+1]
207  NVIC_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){
213  char letter;
214  while(((UART5_FR_R&UART_FR_RXFE) == 0) && (Rx5Fifo_Size() < (RX5FIFOSIZE - 1))){
215    letter = UART5_DR_R;
216    Rx5Fifo_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){
222  char letter;
223  while(((UART5_FR_R&UART_FR_TXFF) == 0) && (Tx5Fifo_Size() > 0)){
224    Tx5Fifo_Get(&letter);
225    UART5_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){
232  return Rx5Fifo_Size();
233}
234// output ASCII character to UART
235// spin if TxFifo is full
236void ESP8266_OutChar(char data){
237  while(Tx5Fifo_Put(data) == FIFOFAIL){};
238  UART5_IM_R &= ~UART_IM_TXIM;          // disable TX FIFO interrupt
239  copySoftwareToHardware5();
240  UART5_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){
247  if(UART5_RIS_R&UART_RIS_TXRIS){       // hardware TX FIFO <= 2 items
248    UART5_ICR_R = UART_ICR_TXIC;        // acknowledge TX FIFO
249    // copy from software TX FIFO to hardware TX FIFO
250    copySoftwareToHardware5();
251    if(Tx5Fifo_Size() == 0){             // software TX FIFO is empty
252      UART5_IM_R &= ~UART_IM_TXIM;      // disable TX FIFO interrupt
253    }
254  }
255  if(UART5_RIS_R&UART_RIS_RXRIS){       // hardware RX FIFO >= 2 items
256    UART5_ICR_R = UART_ICR_RXIC;        // acknowledge RX FIFO
257    // copy from hardware RX FIFO to software RX FIFO
258    copyHardwareToSoftware5();
259  }
260  if(UART5_RIS_R&UART_RIS_RTRIS){       // receiver timed out
261    UART5_ICR_R = UART_ICR_RTIC;        // acknowledge receiver time out
262    // copy from hardware RX FIFO to software RX FIFO
263    copyHardwareToSoftware5();
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){
272  while(*pt){
273    ESP8266_OutChar(*pt);
274    pt++;
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
286  if(n >= 10){
287    ESP8266_OutUDec(n/10);
288    n = n%10;
289  }
290  ESP8266_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){
299  volatile uint32_t time;
300  while(n){
301    time = 6665;  // 1msec, tuned at 80 MHz
302    while(time){
303      time--;
304    }
305    n--;
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){
328  SYSCTL_RCGCGPIO_R |= 0x10; // activate port E
329  while((SYSCTL_PRGPIO_R&0x10)==0){};
330  GPIO_PORTE_DIR_R |= 0x0A;       // output digital I/O on PE3,1
331  GPIO_PORTE_DIR_R &= ~0x01;      // input digital I/O on PE0
332  GPIO_PORTE_AFSEL_R &= ~0x0B;    // disable alt funct on PE3,1,0
333  GPIO_PORTE_DEN_R |= 0x0B;       // enable digital I/O on PE3,1,0
334  GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFFFF0F00);
335  GPIO_PORTE_AMSEL_R &= ~0x0B;    // disable analog functionality on PE3,1,0
336  UART5_Init(2);                  // Enable ESP8266 Serial Port 
337  EnableInterrupts();
338}
339// Initialize ESP8266 interface and reset the
340void ESP8266_Reset(void) {
341#ifdef DEBUG1
342  UART_OutString("In Reset_8266 routine\r\n");
343  UART_OutString("Reset pin being held low for 5 sec\r\n");
344#endif
345  RST = LOW;      // Reset the 8266
346  DelayMs(5000);  // Wait for 8266 to reset
347  RST = RST1;     // Enable the 8266
348#ifdef DEBUG1
349  UART_OutString("Reset pin is now high for 5 sec\r\n");
350  UART_OutString("Waiting for ESP to finish initializing\r\n");
351#endif
352  DelayMs(5000);  // Wait before setting up 8266
353#ifdef DEBUG1
354  UART_OutString("Leaving Reset_8266 routine\r\n");
355#endif
356  PE3 = 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
364  UART_OutString("\r\nIn WiFI_Setup routine\r\n");
365  UART_OutString("Waiting for RDY flag from ESP\r\n");
366#endif
367  
368#ifdef DEBUG3
369  Output_Color(ST7735_YELLOW);
370  ST7735_OutString("In WiFI_Setup routine\n");
371  ST7735_OutString("Waiting for RDY flag\n");
372#endif 
373  while (!RDY) {      // Wait for ESP8266 indicate it is ready for programming data
374#ifdef DEBUG1
375    UART_OutString(".");
376#endif
377    DelayMs(1000);
378  }
379  ESP8266_OutString(auth);    // Send authorization code
380  ESP8266_OutChar(',');
381  ESP8266_OutString(ssid);
382  ESP8266_OutChar(',');
383  ESP8266_OutString(pass); 
384  ESP8266_OutChar(',');       // Extra comma needed for 8266 parser code
385  ESP8266_OutChar('\n');      // Send NL to indicate EOT   
386
387#ifdef DEBUG1
388  UART_OutString(auth);    
389  UART_OutChar(',');
390  UART_OutString(ssid);
391  UART_OutChar(',');
392  UART_OutString(pass);
393  UART_OutString(",\n\r");            
394#endif
395#ifdef DEBUG3
396  Output_Color(ST7735_WHITE);
397  ST7735_OutString(ssid); ST7735_OutChar('\n');
398  ST7735_OutString(pass); ST7735_OutChar('\n');
399  ST7735_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
408  UART_OutString("\n\rWaiting for RDY to go low\n\r");
409#endif 
410  while(RDY){   // pause while RDY=1
411#ifdef DEBUG1
412    UART_OutString(".");
413#endif
414    DelayMs(500);
415  }
416  while(ESP8266_GetMessage(RxMessage)){
417  }
418#ifdef DEBUG1
419  UART_OutString("\n\rRDY went low\n\r");
420#endif 
421  Rx5Fifo_Init(); // flush buffer
422
423#ifdef DEBUG3
424  Output_Color(ST7735_YELLOW);
425  ST7735_OutString("Exiting WiFI_Setup\nReady to talk\n");
426#endif     
427#ifdef DEBUG1
428  UART_OutString("Exiting TM4C WiFI_Setup routine\r\nReady to talk\r\n");
429#endif
430  PE3 = BIT3;
431}
432
433