]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/spi_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KLXX / TARGET_KL46Z / spi_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 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
24 static const PinMap PinMap_SPI_SCLK[] = {
25     {PTA15, SPI_0, 2},
26     {PTB9,  SPI_1, 2},
27     {PTB11, SPI_1, 2},
28     {PTC5,  SPI_0, 2},
29     {PTD1,  SPI_0, 2},
30     {PTD5,  SPI_1, 2},
31     {PTE2,  SPI_1, 2},
32     {PTE17, SPI_0, 2},
33     {NC  ,  NC   , 0}
34 };
35
36 static const PinMap PinMap_SPI_MOSI[] = {
37     {PTA16, SPI_0, 2},
38     {PTA17, SPI_0, 5},
39     {PTB16, SPI_1, 2},
40     {PTB17, SPI_1, 5},
41     {PTC6,  SPI_0, 2},
42     {PTC7,  SPI_0, 5},
43     {PTD2,  SPI_0, 2},
44     {PTD3,  SPI_0, 5},
45     {PTD6,  SPI_1, 2},
46     {PTD7,  SPI_1, 5},
47     {PTE1,  SPI_1, 2},
48     {PTE3,  SPI_1, 5},
49     {PTE18, SPI_0, 2},
50     {PTE19, SPI_0, 5},
51     {NC  ,  NC   , 0}
52 };
53
54 static const PinMap PinMap_SPI_MISO[] = {
55     {PTA16, SPI_0, 5},
56     {PTA17, SPI_0, 2},
57     {PTB16, SPI_1, 5},
58     {PTB17, SPI_1, 2},
59     {PTC6,  SPI_0, 5},
60     {PTC7,  SPI_0, 2},
61     {PTD2,  SPI_0, 5},
62     {PTD3,  SPI_0, 2},
63     {PTD6,  SPI_1, 5},
64     {PTD7,  SPI_1, 2},
65     {PTE1,  SPI_1, 5},
66     {PTE3,  SPI_1, 2},
67     {PTE18, SPI_0, 5},
68     {PTE19, SPI_0, 2},
69     {NC   , NC   , 0}
70 };
71
72 static const PinMap PinMap_SPI_SSEL[] = {
73     {PTA14, SPI_0, 2},
74     {PTB10, SPI_1, 2},
75     {PTC4,  SPI_0, 2},
76     {PTD0,  SPI_0, 2},
77     {PTD4,  SPI_1, 2},
78     {PTE4,  SPI_1, 2},
79     {PTE16, SPI_0, 2},
80     {NC  ,  NC   , 0}
81 };
82
83 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
84     // determine the SPI to use
85     SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
86     SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
87     SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
88     SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
89     SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
90     SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
91
92     obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
93     MBED_ASSERT((int)obj->spi != NC);
94
95     // enable power and clocking
96     switch ((int)obj->spi) {
97         case SPI_0: SIM->SCGC5 |= 1 << 13; SIM->SCGC4 |= 1 << 22; break;
98         case SPI_1: SIM->SCGC5 |= 1 << 13; SIM->SCGC4 |= 1 << 23; break;
99     }
100
101     // set default format and frequency
102     if (ssel == NC) {
103         spi_format(obj, 8, 0, 0);  // 8 bits, mode 0, master
104     } else {
105         spi_format(obj, 8, 0, 1);  // 8 bits, mode 0, slave
106     }
107     spi_frequency(obj, 1000000);
108
109     // enable SPI
110     obj->spi->C1 |= SPI_C1_SPE_MASK;
111     obj->spi->C2 &= ~SPI_C2_SPIMODE_MASK; //8bit
112
113     // pin out the spi pins
114     pinmap_pinout(mosi, PinMap_SPI_MOSI);
115     pinmap_pinout(miso, PinMap_SPI_MISO);
116     pinmap_pinout(sclk, PinMap_SPI_SCLK);
117     if (ssel != NC) {
118         pinmap_pinout(ssel, PinMap_SPI_SSEL);
119     }
120 }
121
122 void spi_free(spi_t *obj) {
123     // [TODO]
124 }
125 void spi_format(spi_t *obj, int bits, int mode, int slave) {
126     MBED_ASSERT((bits == 8) || (bits == 16));
127     MBED_ASSERT((mode >= 0) && (mode <= 3));
128
129     uint8_t polarity = (mode & 0x2) ? 1 : 0;
130     uint8_t phase = (mode & 0x1) ? 1 : 0;
131     uint8_t c1_data = ((!slave) << 4) | (polarity << 3) | (phase << 2);
132
133     // clear MSTR, CPOL and CPHA bits
134     obj->spi->C1 &= ~(0x7 << 2);
135
136     // write new value
137     obj->spi->C1 |= c1_data;
138     if (bits == 8) {
139         obj->spi->C2 &= ~SPI_C2_SPIMODE_MASK;
140     } else {
141         obj->spi->C2 |= SPI_C2_SPIMODE_MASK;
142     }
143 }
144
145 void spi_frequency(spi_t *obj, int hz) {
146     uint32_t error = 0;
147     uint32_t p_error = 0xffffffff;
148     uint32_t ref = 0;
149     uint8_t  spr = 0;
150     uint8_t  ref_spr = 0;
151     uint8_t  ref_prescaler = 0;
152
153     // bus clk
154     uint32_t PCLK = SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV4_MASK) >> SIM_CLKDIV1_OUTDIV4_SHIFT) + 1);
155     uint8_t prescaler = 1;
156     uint8_t divisor = 2;
157
158     for (prescaler = 1; prescaler <= 8; prescaler++) {
159         divisor = 2;
160         for (spr = 0; spr <= 8; spr++, divisor *= 2) {
161             ref = PCLK / (prescaler*divisor);
162             if (ref > (uint32_t)hz)
163                 continue;
164             error = hz - ref;
165             if (error < p_error) {
166                 ref_spr = spr;
167                 ref_prescaler = prescaler - 1;
168                 p_error = error;
169             }
170         }
171     }
172
173     // set SPPR and SPR
174     obj->spi->BR = ((ref_prescaler & 0x7) << 4) | (ref_spr & 0xf);
175 }
176
177 static inline int spi_writeable(spi_t * obj) {
178     return (obj->spi->S & SPI_S_SPTEF_MASK) ? 1 : 0;
179 }
180
181 static inline int spi_readable(spi_t * obj) {
182     return (obj->spi->S & SPI_S_SPRF_MASK) ? 1 : 0;
183 }
184
185 int spi_master_write(spi_t *obj, int value) {
186     int ret;
187     if (obj->spi->C2 & SPI_C2_SPIMODE_MASK) {
188         // 16bit
189         while(!spi_writeable(obj));
190         obj->spi->DL = (value & 0xff);
191         obj->spi->DH = ((value >> 8) & 0xff);
192
193         // wait rx buffer full
194         while (!spi_readable(obj));
195         ret = obj->spi->DH;
196         ret = (ret << 8) | obj->spi->DL;
197     } else {
198         //8bit
199         while(!spi_writeable(obj));
200         obj->spi->DL = (value & 0xff);
201
202         // wait rx buffer full
203         while (!spi_readable(obj));
204         ret = (obj->spi->DL & 0xff);
205     }
206
207     return ret;
208 }
209
210 int spi_slave_receive(spi_t *obj) {
211     return spi_readable(obj);
212 }
213
214 int spi_slave_read(spi_t *obj) {
215     int ret;
216     if (obj->spi->C2 & SPI_C2_SPIMODE_MASK) {
217         ret = obj->spi->DH;
218         ret = ((ret << 8) | obj->spi->DL);
219     } else {
220         ret = obj->spi->DL;
221     }
222     return ret;
223 }
224
225 void spi_slave_write(spi_t *obj, int value) {
226     while (!spi_writeable(obj));
227     if (obj->spi->C2 & SPI_C2_SPIMODE_MASK) {
228         obj->spi->DL = (value & 0xff);
229         obj->spi->DH = ((value >> 8) & 0xff);
230     } else {
231         obj->spi->DL = value;
232     }
233
234 }