ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
hal.c
Go to the documentation of this file.
1 
23 #include "user_config.h"
24 #include "hal.h"
25 
26 #ifdef ESP8266
27 #include <eagle_soc.h>
28 #endif
29 
30 #include <stdint.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <math.h>
34 
35 #ifdef ESP8266
36 #include "hspi.h"
37 #include "gpio.h"
38 #endif
39 
40 #ifdef AVR
41 #include "hardware/iom1284p.h"
42 #endif
43 
44 // =============================================
46 
51 void
53 {
54 #ifdef ESP8266
55  switch(pin)
56  {
57 
58  case 0:
59  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
60  break;
61  case 1:
62  PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1);
63  break;
64  case 2:
65  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
66  break;
67  case 3:
68  PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3);
69  break;
70  case 4: // some esp8266-12 boards have incorrect labels 4 and 5 swapped
71  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
72  break;
73  case 5: // some esp8266-12 boards have incorrect labels 4 and 5 swapped
74  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
75  break;
76  case 6:
77  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_GPIO6);
78  break;
79  case 7:
80  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, FUNC_GPIO7);
81  break;
82  case 8:
83  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, FUNC_GPIO8);
84  break;
85  case 9:
86  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA2_U, FUNC_GPIO9);
87  break;
88  case 10:
89  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA3_U, FUNC_GPIO10);
90  break;
91  case 11:
92  PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, FUNC_GPIO11);
93  break;
94  case 12:
95  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
96  break;
97  case 13:
98  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13);
99  break;
100  case 14:
101  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14);
102  break;
103  case 15:
104  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15);
105  break;
106  case 16:
107  // mux configuration for XPD_DCDC to output rtc_gpio0
108  WRITE_PERI_REG(PAD_XPD_DCDC_CONF,
109  (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1);
110  //mux configuration for out enable
111  WRITE_PERI_REG(RTC_GPIO_CONF,
112  (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0);
113  //out enable
114  WRITE_PERI_REG(RTC_GPIO_ENABLE,
115  (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1);
116  break;
117  }
118 #endif
119 #ifdef AVR
120  //TODO
121 #endif
122 }
123 
124 #ifdef ESP8266
125 
130 void
132 {
133 // mux XPD_DCDC to rtc_gpio0
134  WRITE_PERI_REG(PAD_XPD_DCDC_CONF,
135  (READ_PERI_REG(PAD_XPD_DCDC_CONF) & (uint32_t)0xffffffbc) | (uint32_t)1L);
136 
137 // mux out enable
138  WRITE_PERI_REG(RTC_GPIO_CONF,
139  (READ_PERI_REG(RTC_GPIO_CONF) & (uint32_t)0xfffffffe) | (uint32_t)0L);
140 
141 // out enable
142  WRITE_PERI_REG(RTC_GPIO_ENABLE,
143  (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32_t)0xfffffffe) | (uint32_t)out);
144 }
145 
146 #endif // ESP8266
147 
148 
156 {
157 #ifdef ESP8266
158  if(pin == 16)
159  {
160  if(val)
161  GPIO16_PIN_HI();
162  else
163  GPIO16_PIN_LOW();
164  }
165  else
166 #endif
167  if(val)
168  GPIO_PIN_HI(pin);
169  else
170  GPIO_PIN_LOW(pin);
171 }
172 
179 {
180 #ifdef ESP8266
181  if(pin == 16)
182  {
183  return( GPIO16_PIN_RD() );
184  }
185  else
186 #endif
187  return( GPIO_PIN_RD(pin) );
188 }
189 
190 
191 // =============================================
193 
199 {
200 //FIXME add address decoder options
201 #ifdef HAVE_DECODER
202 #error add address decoder code
203 #endif
204  GPIO_PIN_HI(pin);
205  GPIO_PIN_MODE(pin);
206 }
207 
214 {
215 //FIXME add address decoder options
216 #ifdef HAVE_DECODER
217 #error add address decoder code
218 #endif
219  GPIO_PIN_LOW(pin);
220 }
221 
228 {
229 //FIXME add address decoder options
230 #ifdef HAVE_DECODER
231 #error add address decoder code
232 #endif
233  GPIO_PIN_HI(pin);
234 }
235 
236 // =============================================
238 
243 {
244 #ifdef ADDR_0
245  GPIO_PIN_LOW(ADDR_0);
246  GPIO_PIN_MODE(ADDR_0);
247 #endif
248 
249 #ifdef ADDR_1
250  GPIO_PIN_LOW(ADDR_1);
251  GPIO_PIN_MODE(ADDR_1);
252 #endif
253 
254 #ifdef ADDR_2
255  GPIO_PIN_LOW(ADDR_2);
256  GPIO_PIN_MODE(ADDR_2);
257 #endif
258 
259 #ifdef ADDR_3
260  GPIO_PIN_LOW(ADDR_3);
261  GPIO_PIN_MODE(ADDR_3);
262 #endif
263 }
264 
271 void chip_addr(int addr)
272 {
273 
274 #ifdef ADDR_0
275  if(addr & 1)
276  GPIO_PIN_HI(ADDR_0);
277  else
278  GPIO_PIN_LOW(ADDR_0);
279 #endif
280 #ifdef ADDR_1
281  if(addr & 2)
282  GPIO_PIN_HI(ADDR_1);
283  else
284  GPIO_PIN_LOW(ADDR_1);
285 #endif
286 #ifdef ADDR_2
287  if(addr & 4)
288  GPIO_PIN_HI(ADDR_2);
289  else
290  GPIO_PIN_LOW(ADDR_2);
291 #endif
292 #ifdef ADDR_3
293  if(addr & 8)
294  GPIO_PIN_HI(ADDR_3);
295  else
296  GPIO_PIN_LOW(ADDR_3);
297 #endif
298 }
299 
300 // =============================================
302 
307 {
308 #ifdef AVR
309 #endif
310 #ifdef ESP8266
311  hspi_waitReady();
312 #endif
313 }
314 
315 
327 void spi_init(uint32_t clock, int pin)
328 {
329  spi_waitReady();
330  chip_deselect(pin);
331  _cs_pin = 0xff;
332 
333 #ifdef AVR
334  SPI0_Init(clock); // Initialize the SPI bus - does nothing if clock unchanged
335  SPI0_Mode(0); // Set the clocking mode, etc
336 #endif
337 #ifdef ESP8266
338  hspi_init(clock,0); // Initialize the SPI bus - does nothing if clock unchanged
339 #endif
340  spi_TX(0xff);
341  _spi_clock = clock;
342  // waits for any prior transactions to complete before updating
343  spi_waitReady();
344 }
345 
355 void spi_begin(uint32_t clock, int pin)
356 {
357  // FIXME allow nesting by using an array of clock values for each pin
358 
359  //@brief if there is a prior chip select in progress flag an error
360  if(_cs_pin != 0xff)
361  {
362  // This implies a bug!
363  printf("cs_enable was: %d, want: %d\n", 0xff & _cs_pin, pin);
364  }
365 
366  // waits for any prior transactions to complete before updating
367  spi_waitReady();
368 
371  if(_spi_clock != clock)
372  {
373  spi_init(clock,pin);
374  }
375 
376  chip_select(pin);
377  _cs_pin = pin;
378 }
379 
388 void spi_end(uint8_t pin)
389 {
390 // DEBUG
391  // FIXME allow nesting
392  if(_cs_pin != pin && _cs_pin != 0xff )
393  {
394  // This implies a bug!
395  printf("cs_disable was: %d, want: %d\n", 0xff & _cs_pin, pin);
396  }
397  spi_waitReady();
398  chip_deselect(pin);
399  _cs_pin = 0xff;
400 }
401 
405 {
406  return(_cs_pin);
407 }
408 
409 
414 void spi_TX_buffer(const uint8_t *data, int count)
415 {
416 #ifdef ESP8266
417  hspi_TX((uint8_t *) data,count);
418 #endif
419 #ifdef AVR
420  SPI0_TX((uint8_t *) data,count);
421 #endif
422 }
423 
428 void spi_RX_buffer(const uint8_t *data, int count)
429 {
430 #ifdef ESP8266
431  hspi_RX((uint8_t *) data,count);
432 #endif
433 #ifdef AVR
434  SPI0_RX((uint8_t *)data,count);
435 #endif
436 }
437 
442 void spi_TXRX_buffer(const uint8_t *data, int count)
443 {
444 #ifdef ESP8266
445  hspi_TXRX((uint8_t *) data,count);
446 #endif
447 #ifdef AVR
448  SPI0_TXRX((uint8_t *) data,count);
449 #endif
450 }
451 
455 {
456  uint8_t data;
457 #ifdef ESP8266
458  hspi_RX(&data,1);
459 #endif
460 #ifdef AVR
461  SPI0_RX(&data,1);
462 #endif
463  return(data);
464 }
465 
469 void spi_TX(uint8_t data)
470 {
471 #ifdef ESP8266
472  hspi_TX(&data,1);
473 #endif
474 #ifdef AVR
475  SPI0_TX(&data,1);
476 #endif
477 }
478 
479 
484 {
485 #ifdef ESP8266
486  hspi_TXRX(&data,1);
487 #endif
488 #ifdef AVR
489  SPI0_TXRX(&data,1);
490 #endif
491  return(data);
492 }
void hspi_RX(uint8_t *data, int count)
HSPI read using FIFO.
Definition: hspi.c:318
void chip_addr(int addr)
set address on GPIO lines
Definition: hal.c:271
uint8_t gpio_pin_rd(uint8_t pin)
read GPIO pin
Definition: hal.c:178
void spi_begin(uint32_t clock, int pin)
SPI chip enable function Function waits for current tranaction to finish before proceeding.
Definition: hal.c:355
Master include file for project Includes all project includes and defines here.
void hspi_init(uint32_t prescale, int hwcs)
HSPI Initiaization - with automatic chip select Pins: MISO GPIO12 MOSI GPIO13 CLK GPIO14 CS GPIO15 - ...
Definition: hspi.c:55
void gpio_pin_sfr_mode(int pin)
GPIO HAL.
Definition: hal.c:52
uint8_t spi_chip_select_status()
SPI CS pin status return CS GPIO pin number or 0xff.
Definition: hal.c:404
void spi_TX(uint8_t data)
SPI write 1 byte.
Definition: hal.c:469
unsigned int uint32_t
Definition: send.c:19
void gpio16_pin_dir(uint8_t out)
uint8_t _cs_pin
SPI init function Function waits for current tranaction to finish before proceeding.
Definition: hal.c:325
void spi_waitReady()
SPI bus wrapper functions for multiple device support.
Definition: hal.c:306
void gpio_pin_out(uint8_t pin, uint8_t val)
set GPIO pin state HI or LOW
Definition: hal.c:155
void hspi_waitReady(void)
HSPI Ready wait.
Definition: hspi.c:159
void spi_end(uint8_t pin)
SPI chip disable function wait for current tranaction to finish!
Definition: hal.c:388
void chip_select_init(uint8_t pin)
CHIP select HAL.
Definition: hal.c:198
void spi_init(uint32_t clock, int pin)
Definition: hal.c:327
void hspi_TX(uint8_t *data, int count)
SPI buffered write functions.
Definition: hspi.c:265
uint8_t spi_TXRX(uint8_t data)
SPI read and write 1 byte.
Definition: hal.c:483
uint32_t _spi_clock
Definition: hal.c:326
void spi_TXRX_buffer(const uint8_t *data, int count)
SPI write/read buffer.
Definition: hal.c:442
void spi_RX_buffer(const uint8_t *data, int count)
SPI read buffer.
Definition: hal.c:428
void chip_deselect(uint8_t pin)
set GPIO to deselect - HI
Definition: hal.c:227
MEMSPACE int printf(const char *format,...)
#define L(x)
Definition: cal_dex.c:54
void chip_addr_init()
ADDRESS select HAL.
Definition: hal.c:242
void spi_TX_buffer(const uint8_t *data, int count)
SPI write buffer.
Definition: hal.c:414
unsigned char uint8_t
Definition: send.c:17
uint8_t spi_RX()
SPI read 1 byte.
Definition: hal.c:454
void chip_select(uint8_t pin)
set GPIO to select - LOW
Definition: hal.c:213
void hspi_TXRX(uint8_t *data, int count)
HSPI write and read using FIFO.
Definition: hspi.c:292