ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
hspi.c
Go to the documentation of this file.
1 
25 #include "user_config.h"
26 
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <math.h>
31 
32 #include "hspi.h"
33 #include "gpio.h"
34 
35 // @brief HSPI Prescale value - You should set this in your Makefile
36 #ifndef HSPI_PRESCALER
37 #define HSPI_PRESCALER 16
38 #endif
39 
40 
41 static _hspi_init_done = 0;
42 
55 void hspi_init(uint32_t prescale, int hwcs)
56 {
57 
58 // FIXME DEBUG
59 #if 1
60  // We only make changes if the speed actually changes
61  if(prescale == hspi_clock)
62  return;
63 #endif
64 
65  // If we have been called make sure all spi transactions have finished before
66  // we change anything
67 
68  if(_hspi_init_done)
70 
71  /* eagle_soc.h does not define MISO,MOSI,CLK and CS */
72  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2); // HSPIQ MISO GPIO12
73  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2); // HSPID MOSI GPIO13
74  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2); // CLK GPIO14
75 
76  // HARDWARE SPI CS
77  if(hwcs)
78  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2); // CS AUTOMATIC ONLY ON GPIO15
79 
80  if(prescale == 0)
81  {
82  WRITE_PERI_REG(PERIPHS_IO_MUX, 0x305); //set bit9
83  WRITE_PERI_REG(SPI_FLASH_CLOCK(HSPI), SPI_CLK_EQU_SYSCLK);
84  }
85  else
86  {
87  // prescale >= 1
88  WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105); //clear bit9
89  WRITE_PERI_REG(SPI_FLASH_CLOCK(HSPI),
90  (((prescale - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
91  ((1 & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
92  ((0 & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
93  ((1 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S));
94  }
95 
96  WRITE_PERI_REG(SPI_FLASH_CTRL1(HSPI), 0);
97  hspi_clock = prescale;
98  _hspi_init_done = 1;
99 }
100 
104 static void hspi_config(int configState)
105 {
106  uint32_t valueOfRegisters = 0;
107 
108  hspi_waitReady();
109 
110  if (configState == CONFIG_FOR_TX)
111  {
112  valueOfRegisters |= SPI_FLASH_DOUT;
113 //clear bit 2 see example IoT_Demo
114  valueOfRegisters &= \
117  }
118  else if (configState == CONFIG_FOR_RX_TX)
119  {
120  valueOfRegisters |= SPI_FLASH_DOUT | SPI_DOUTDIN | SPI_CK_I_EDGE;
121 //clear bit 2 see example IoT_Demo
122  valueOfRegisters &= \
125  }
126  else
127  {
128  return; // Error
129  }
130  WRITE_PERI_REG(SPI_FLASH_USER(HSPI), valueOfRegisters);
131 
132 }
133 
134 
137 // Only called from hspi_writeFIFO or hspi_readFIFO
138 // So bounds testing has already been done
140 static void hspi_setBits(uint16_t bytes)
141 {
142  uint16_t bits = (bytes << 3) - 1;
143  WRITE_PERI_REG(SPI_FLASH_USER1(HSPI),
144  ( (bits & SPI_USR_OUT_BITLEN) << SPI_USR_OUT_BITLEN_S ) |
145  ( (bits & SPI_USR_DIN_BITLEN) << SPI_USR_DIN_BITLEN_S ) );
146 }
147 
148 
151 static void hspi_startSend(void)
152 {
153  SET_PERI_REG_MASK(SPI_FLASH_CMD(HSPI), SPI_FLASH_USR);
154 }
155 
156 
159 void hspi_waitReady(void)
160 {
161  while (READ_PERI_REG(SPI_FLASH_CMD(HSPI)) & SPI_FLASH_USR) {};
162 }
163 
164 
169 static void hspi_writeFIFO(uint8_t *write_data, int bytes)
170 {
171  uint8_t word_ind = 0;
172 
173  if(bytes > HSPI_FIFO_SIZE) // TODO set error status
174  return;
175 
176  hspi_setBits(bytes); // Update FIFO with number of bits we will send
177 
178 // First do a fast write with 32 bit chunks at a time
179  while(bytes >= 4)
180  {
181 // Cast both source and destination to 4 byte word pointers
182  ((uint32_t *)SPI_FLASH_C0(HSPI)) [word_ind] = \
183  ((uint32_t *)write_data) [word_ind];
184 
185  bytes -= 4;
186  word_ind++;
187  }
188 
189 // Next write remaining bytes (if any) to FIFO as a single word
190 // Note: We follow the good practice of avoiding access past the end of an array.
191 // (ie. protected memory, PIC using retw from flash ... could crash some CPU's)
192  if(bytes) // Valid counts are now: 3,2,1 bytes
193  {
194  uint32_t last_word = 0; // Last word to send
195  uint16_t byte_ind = word_ind << 2; // Convert to Byte index
196 
197 // Working MSB to LSB allows assigment to LSB without shifting
198 // Compiler can optimize powers of 8 into byte swaps, etc... (on some CPU's)
199  while(bytes--) // index is now 2,1,0 matching required storage offset
200  {
201  last_word <<= 8;
202 // 2,1,0
203  last_word |= ((uint32_t) 0xffU & write_data[byte_ind + bytes ]);
204  }
205 // Send last partial word
206  ((uint32_t *)SPI_FLASH_C0(HSPI)) [word_ind] = last_word;
207  }
208 }
209 
210 
215 static void hspi_readFIFO(uint8_t *read_data, int bytes)
216 {
217 
218  uint8_t word_ind = 0;
219 
220  if(bytes > HSPI_FIFO_SIZE) // TODO set error status
221  return;
222 
223 // Update FIFO with number of bits to read ?
224  hspi_setBits(bytes);
225 
226 // First do a fast read 32 bit chunks at a time
227  while(bytes >= 4)
228  {
229 // Cast both source and destination to 4 byte word pointers
230  ((uint32_t *)read_data) [word_ind] = \
231  ((uint32_t *)SPI_FLASH_C0(HSPI)) [word_ind];
232  bytes -= 4;
233  word_ind++;
234  }
235 
236 // Next read remaining bytes (if any) from FIFO as a single word
237 // Note: We follow the good practice of avoiding access past the end of an array.
238 // (ie. protected memory, PIC using retw from flash ... could crash some CPU's)
239  if(bytes) // Valid counts are now: 3,2,1 bytes
240  {
241  uint32_t last_word = ((uint32_t *)SPI_FLASH_C0(HSPI)) [word_ind];
242  uint16_t byte_ind = word_ind << 2; // Convert to Byte index
243 
244  while(bytes--) // index is now 2,1,0 matching required storage offset
245  {
246 // 2,1,0 order
247  read_data[byte_ind++] = (uint8_t) 0xff & last_word;
248  last_word >>= 8;
249  }
250 // Send last partial word
251  }
252 }
253 
254 
255 
256 
260 
265 void hspi_TX(uint8_t *data, int count)
266 {
267  int bytes;
268 
269  while(count > 0)
270  {
271  // we will never have zero - we already checked
272  bytes = count;
273  if(bytes > HSPI_FIFO_SIZE)
274  bytes = HSPI_FIFO_SIZE;
275 
276  hspi_config(CONFIG_FOR_TX); // Does hspi_waitReady(); first
277  // clear buffer first
278  hspi_writeFIFO(data, bytes);
279  hspi_startSend();
280  hspi_waitReady();
281 
282  data += bytes;
283  count -= bytes;
284  }
285 }
286 
291 
292 void hspi_TXRX(uint8_t *data, int count)
293 {
294  int bytes;
295 
296  while(count > 0)
297  {
298  // we will never have zero - we already checked
299  bytes = count;
300  if(bytes > HSPI_FIFO_SIZE)
301  bytes = HSPI_FIFO_SIZE;
302 
303  hspi_config(CONFIG_FOR_RX_TX); // Does hspi_waitReady(); first
304  hspi_writeFIFO(data, bytes);
305  hspi_startSend();
306  hspi_waitReady();
307  hspi_readFIFO(data, bytes);
308 
309  data += bytes;
310  count -= bytes;
311  }
312 }
313 
318 void hspi_RX(uint8_t *data, int count)
319 {
320  int bytes;
321 
322  while(count > 0)
323  {
324  // we will never have zero - we already checked
325  bytes = count;
326  if(bytes > HSPI_FIFO_SIZE)
327  bytes = HSPI_FIFO_SIZE;
328 
329  // discard TX data
331  // Writes are ignored, so we set data to 0xff
332  memset(data, 0xff, bytes);
333  hspi_writeFIFO(data, bytes);
334  hspi_startSend();
335  hspi_waitReady();
336  // read result
337  hspi_readFIFO(data, bytes);
338 
339  data += bytes;
340  count -= bytes;
341  }
342 }
343 
#define SPI_FLASH_C0(i)
Definition: spi_register.h:209
#define SPI_DOUTDIN
Definition: spi_register.h:131
void hspi_RX(uint8_t *data, int count)
HSPI read using FIFO.
Definition: hspi.c:318
unsigned short uint16_t
Definition: send.c:18
#define SPI_CLKDIV_PRE_S
Definition: spi_register.h:92
Master include file for project Includes all project includes and defines here.
#define SPI_USR_DIN_BITLEN_S
Definition: spi_register.h:139
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
#define SPI_CLKCNT_L
Definition: spi_register.h:97
#define SPI_FLASH_CTRL1(i)
Definition: spi_register.h:46
static void hspi_startSend(void)
HSPI Start Send.
Definition: hspi.c:151
#define SPI_USR_DIN_BITLEN
Definition: spi_register.h:138
#define SPI_CK_I_EDGE
Definition: spi_register.h:126
#define CONFIG_FOR_RX_TX
Definition: hspi.h:16
unsigned int uint32_t
Definition: send.c:19
#define HSPI_FIFO_SIZE
Definition: hspi.h:13
#define SPI_CLKCNT_N_S
Definition: spi_register.h:94
#define SPI_FLASH_CMD(i)
Definition: spi_register.h:12
#define SPI_CLK_EQU_SYSCLK
Definition: spi_register.h:90
#define SPI_FLASH_USR_ADDR
Definition: spi_register.h:102
#define SPI_FLASH_DOUT
Definition: spi_register.h:105
#define SPI_FLASH_CLOCK(i)
Definition: spi_register.h:89
void hspi_waitReady(void)
HSPI Ready wait.
Definition: hspi.c:159
#define SPI_FLASH_USR_DUMMY
Definition: spi_register.h:103
void hspi_TX(uint8_t *data, int count)
SPI buffered write functions.
Definition: hspi.c:265
#define SPI_CLKCNT_N
Definition: spi_register.h:93
#define SPI_FLASH_USER1(i)
Definition: spi_register.h:133
#define SPI_CLKCNT_H_S
Definition: spi_register.h:96
static void hspi_readFIFO(uint8_t *read_data, int bytes)
HSPI FIFO Read in units of 32 bits (4 byte words)
Definition: hspi.c:215
#define SPI_USR_COMMAND
Definition: spi_register.h:101
#define SPI_FLASH_USER(i)
Definition: spi_register.h:100
#define SPI_FLASH_USR
Definition: spi_register.h:26
#define CONFIG_FOR_TX
Definition: hspi.h:15
static void hspi_writeFIFO(uint8_t *write_data, int bytes)
HSPI FIFO write in units of 32 bits (4 byte words)
Definition: hspi.c:169
static _hspi_init_done
Definition: hspi.c:41
#define SPI_FLASH_USR_DIN
Definition: spi_register.h:104
#define SPI_USR_OUT_BITLEN_S
Definition: spi_register.h:137
#define HSPI
Definition: hspi.h:12
#define SPI_CLKDIV_PRE
Definition: spi_register.h:91
#define SPI_CLKCNT_H
Definition: spi_register.h:95
#define SPI_USR_OUT_BITLEN
Definition: spi_register.h:136
static void hspi_setBits(uint16_t bytes)
HSPI FIFO send or receive byte count.
Definition: hspi.c:140
static void hspi_config(int configState)
HSPI Configuration for tranasmit and receive.
Definition: hspi.c:104
unsigned char uint8_t
Definition: send.c:17
#define SPI_CLKCNT_L_S
Definition: spi_register.h:98
void hspi_TXRX(uint8_t *data, int count)
HSPI write and read using FIFO.
Definition: hspi.c:292
uint32_t hspi_clock
hspi clock cached value
Definition: hspi.c:44