1 /* mbed Microcontroller Library
2 * Copyright (c) 2015 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
23 #include "clk_freqs.h"
24 #include "PeripheralPins.h"
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);
35 obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
36 MBED_ASSERT((int)obj->spi != NC);
38 SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK;
39 SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK;
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;
44 // set default format and frequency
46 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
48 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
50 spi_frequency(obj, 1000000);
52 // not halt in the debug mode
53 obj->spi->SR |= SPI_SR_EOQF_MASK;
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);
60 pinmap_pinout(ssel, PinMap_SPI_SSEL);
64 void spi_free(spi_t *obj) {
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));
71 uint32_t polarity = (mode & 0x2) ? 1 : 0;
72 uint32_t phase = (mode & 0x1) ? 1 : 0;
75 obj->spi->MCR &= ~SPI_MCR_MSTR_MASK;
76 obj->spi->MCR |= ((!slave) << SPI_MCR_MSTR_SHIFT);
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);
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};
86 void spi_frequency(spi_t *obj, int hz) {
88 uint32_t p_error = 0xffffffff;
92 uint32_t ref_prescaler = 0;
96 uint32_t PCLK = bus_frequency();
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)
105 if (f_error < p_error) {
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);
120 static inline int spi_writeable(spi_t *obj) {
121 return (obj->spi->SR & SPI_SR_TFFF_MASK) ? 1 : 0;
124 static inline int spi_readable(spi_t *obj) {
125 return (obj->spi->SR & SPI_SR_RFDF_MASK) ? 1 : 0;
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*/;
135 // wait rx buffer full
136 while (!spi_readable(obj));
137 return obj->spi->POPR;
140 int spi_slave_receive(spi_t *obj) {
141 return spi_readable(obj);
144 int spi_slave_read(spi_t *obj) {
145 obj->spi->SR |= SPI_SR_RFDF_MASK;
146 return obj->spi->POPR;
149 void spi_slave_write(spi_t *obj, int value) {
150 while (!spi_writeable(obj));