]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20XX/spi_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_K20XX / spi_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2015 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "mbed_assert.h"
17 #include "spi_api.h"
18
19 #include <math.h>
20
21 #include "cmsis.h"
22 #include "pinmap.h"
23 #include "clk_freqs.h"
24 #include "PeripheralPins.h"
25
26 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
27     // determine the SPI to use
28     SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
29     SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
30     SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
31     SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
32     SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
33     SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
34     
35     obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
36     MBED_ASSERT((int)obj->spi != NC);
37
38     SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK;
39     SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK;
40
41     obj->spi->MCR &= ~(SPI_MCR_MDIS_MASK | SPI_MCR_HALT_MASK);
42     //obj->spi->MCR |= SPI_MCR_DIS_RXF_MASK | SPI_MCR_DIS_TXF_MASK;
43
44     // set default format and frequency
45     if (ssel == NC) {
46         spi_format(obj, 8, 0, 0);  // 8 bits, mode 0, master
47     } else {
48         spi_format(obj, 8, 0, 1);  // 8 bits, mode 0, slave
49     }
50     spi_frequency(obj, 1000000);
51
52     // not halt in the debug mode
53     obj->spi->SR |= SPI_SR_EOQF_MASK;
54
55     // pin out the spi pins
56     pinmap_pinout(mosi, PinMap_SPI_MOSI);
57     pinmap_pinout(miso, PinMap_SPI_MISO);
58     pinmap_pinout(sclk, PinMap_SPI_SCLK);
59     if (ssel != NC) {
60         pinmap_pinout(ssel, PinMap_SPI_SSEL);
61     }
62 }
63
64 void spi_free(spi_t *obj) {
65     // [TODO]
66 }
67 void spi_format(spi_t *obj, int bits, int mode, int slave) {
68     MBED_ASSERT((bits > 4) || (bits < 16));
69     MBED_ASSERT((mode >= 0) && (mode <= 3));
70
71     uint32_t polarity = (mode & 0x2) ? 1 : 0;
72     uint32_t phase = (mode & 0x1) ? 1 : 0;
73
74     // set master/slave
75     obj->spi->MCR &= ~SPI_MCR_MSTR_MASK;
76     obj->spi->MCR |= ((!slave) << SPI_MCR_MSTR_SHIFT);
77
78     // CTAR0 is used
79     obj->spi->CTAR[0] &= ~(SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_FMSZ_MASK);
80     obj->spi->CTAR[0] |= (polarity << SPI_CTAR_CPOL_SHIFT) | (phase << SPI_CTAR_CPHA_SHIFT) | ((bits - 1) << SPI_CTAR_FMSZ_SHIFT);
81 }
82
83 static const uint8_t baudrate_prescaler[] = {2,3,5,7};
84 static const uint16_t baudrate_scaler[] = {2,4,6,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
85
86 void spi_frequency(spi_t *obj, int hz) {
87     uint32_t f_error = 0;
88     uint32_t p_error = 0xffffffff;
89     uint32_t ref = 0;
90     uint32_t br = 0;
91     uint32_t ref_spr = 0;
92     uint32_t ref_prescaler = 0;
93     uint32_t ref_dr = 0;
94
95     // bus clk
96     uint32_t PCLK = bus_frequency();
97
98     for (uint32_t i = 0; i < 4; i++) {
99         for (br = 0; br <= 15; br++) {
100             for (uint32_t dr = 0; dr < 2; dr++) {
101                 ref = (PCLK * (1U + dr) / baudrate_prescaler[i]) / baudrate_scaler[br];
102                 if (ref > (uint32_t)hz)
103                     continue;
104                 f_error = hz - ref;
105                 if (f_error < p_error) {
106                     ref_spr = br;
107                     ref_prescaler = i;
108                     ref_dr = dr;
109                     p_error = f_error;
110                 }
111             }
112         }
113     }
114
115     // set PBR and BR
116     obj->spi->CTAR[0] &= ~(SPI_CTAR_PBR_MASK | SPI_CTAR_BR_MASK | SPI_CTAR_DBR_MASK);
117     obj->spi->CTAR[0] |= (ref_prescaler << SPI_CTAR_PBR_SHIFT) | (ref_spr << SPI_CTAR_BR_SHIFT) | (ref_dr << SPI_CTAR_DBR_SHIFT);
118 }
119
120 static inline int spi_writeable(spi_t *obj) {
121     return (obj->spi->SR & SPI_SR_TFFF_MASK) ? 1 : 0;
122 }
123
124 static inline int spi_readable(spi_t *obj) {
125     return (obj->spi->SR & SPI_SR_RFDF_MASK) ? 1 : 0;
126 }
127
128 int spi_master_write(spi_t *obj, int value) {
129     //clear RX buffer flag
130     obj->spi->SR |= SPI_SR_RFDF_MASK;
131     // wait tx buffer empty
132     while(!spi_writeable(obj));
133     obj->spi->PUSHR = SPI_PUSHR_TXDATA(value & 0xffff) /*| SPI_PUSHR_EOQ_MASK*/;
134
135     // wait rx buffer full
136     while (!spi_readable(obj));
137     return obj->spi->POPR;
138 }
139
140 int spi_slave_receive(spi_t *obj) {
141     return spi_readable(obj);
142 }
143
144 int spi_slave_read(spi_t *obj) {
145     obj->spi->SR |= SPI_SR_RFDF_MASK;
146     return obj->spi->POPR;
147 }
148
149 void spi_slave_write(spi_t *obj, int value) {
150     while (!spi_writeable(obj));
151 }