MeterLogger
spi.h
Go to the documentation of this file.
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 David Ogilvy (MetalPhreak)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 
25 #ifndef SPI_APP_H
26 #define SPI_APP_H
27 
28 #include "spi_register.h"
29 #include "ets_sys.h"
30 #include "osapi.h"
31 //#include "uart.h"
32 #include "os_type.h"
33 
34 //Define SPI hardware modules
35 #define SPI 0
36 #define HSPI 1
37 
38 #define SPI_CLK_USE_DIV 0
39 #define SPI_CLK_80MHZ_NODIV 1
40 
41 #define SPI_BYTE_ORDER_HIGH_TO_LOW 1
42 #define SPI_BYTE_ORDER_LOW_TO_HIGH 0
43 
44 #ifndef CPU_CLK_FREQ //Should already be defined in eagle_soc.h
45 #define CPU_CLK_FREQ 80*1000000
46 #endif
47 
48 //Define some default SPI clock settings
49 #define SPI_CLK_PREDIV 10
50 #define SPI_CLK_CNTDIV 2
51 #define SPI_CLK_FREQ CPU_CLK_FREQ/(SPI_CLK_PREDIV*SPI_CLK_CNTDIV) // 80 / 20 = 4 MHz
52 
53 
54 
55 
56 
57 ICACHE_FLASH_ATTR void spi_init(uint8 spi_no);
58 ICACHE_FLASH_ATTR void spi_mode(uint8 spi_no, uint8 spi_cpha,uint8 spi_cpol);
59 ICACHE_FLASH_ATTR void spi_init_gpio(uint8 spi_no, uint8 sysclk_as_spiclk);
60 ICACHE_FLASH_ATTR void spi_clock(uint8 spi_no, uint16 prediv, uint8 cntdiv);
61 ICACHE_FLASH_ATTR void spi_tx_byte_order(uint8 spi_no, uint8 byte_order);
62 ICACHE_FLASH_ATTR void spi_rx_byte_order(uint8 spi_no, uint8 byte_order);
63 ICACHE_FLASH_ATTR uint32 spi_transaction(uint8 spi_no, uint8 cmd_bits, uint16 cmd_data, uint32 addr_bits, uint32 addr_data, uint32 dout_bits, uint32 dout_data, uint32 din_bits, uint32 dummy_bits);
64 
65 //Expansion Macros
66 #define spi_busy(spi_no) READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR
67 
68 #define spi_txd(spi_no, bits, data) spi_transaction(spi_no, 0, 0, 0, 0, bits, (uint32) data, 0, 0)
69 #define spi_tx8(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 8, (uint32) data, 0, 0)
70 #define spi_tx16(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 16, (uint32) data, 0, 0)
71 #define spi_tx32(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 32, (uint32) data, 0, 0)
72 
73 #define spi_rxd(spi_no, bits) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, bits, 0)
74 #define spi_rx8(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 8, 0)
75 #define spi_rx16(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 16, 0)
76 #define spi_rx32(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 32, 0)
77 
78 #endif
79 
ICACHE_FLASH_ATTR uint32 spi_transaction(uint8 spi_no, uint8 cmd_bits, uint16 cmd_data, uint32 addr_bits, uint32 addr_data, uint32 dout_bits, uint32 dout_data, uint32 din_bits, uint32 dummy_bits)
Definition: spi.c:241
ICACHE_FLASH_ATTR void spi_init(uint8 spi_no)
Definition: spi.c:38
ICACHE_FLASH_ATTR void spi_init_gpio(uint8 spi_no, uint8 sysclk_as_spiclk)
Definition: spi.c:97
unsigned short uint16
Definition: c_types.h:50
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
ICACHE_FLASH_ATTR void spi_rx_byte_order(uint8 spi_no, uint8 byte_order)
Definition: spi.c:207
ICACHE_FLASH_ATTR void spi_tx_byte_order(uint8 spi_no, uint8 byte_order)
Definition: spi.c:176
unsigned char uint8
Definition: c_types.h:45
ICACHE_FLASH_ATTR void spi_mode(uint8 spi_no, uint8 spi_cpha, uint8 spi_cpol)
Definition: spi.c:67
ICACHE_FLASH_ATTR void spi_clock(uint8 spi_no, uint16 prediv, uint8 cntdiv)
Definition: spi.c:136
unsigned int uint32
Definition: c_types.h:54