![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_K20XX/spi_api.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2015 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "spi_api.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | |
kadonotakashi | 0:8fdf9a60065b | 19 | #include <math.h> |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "pinmap.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "clk_freqs.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "PeripheralPins.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
kadonotakashi | 0:8fdf9a60065b | 27 | // determine the SPI to use |
kadonotakashi | 0:8fdf9a60065b | 28 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
kadonotakashi | 0:8fdf9a60065b | 29 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
kadonotakashi | 0:8fdf9a60065b | 30 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
kadonotakashi | 0:8fdf9a60065b | 31 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
kadonotakashi | 0:8fdf9a60065b | 32 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
kadonotakashi | 0:8fdf9a60065b | 33 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
kadonotakashi | 0:8fdf9a60065b | 34 | |
kadonotakashi | 0:8fdf9a60065b | 35 | obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); |
kadonotakashi | 0:8fdf9a60065b | 36 | MBED_ASSERT((int)obj->spi != NC); |
kadonotakashi | 0:8fdf9a60065b | 37 | |
kadonotakashi | 0:8fdf9a60065b | 38 | SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK; |
kadonotakashi | 0:8fdf9a60065b | 39 | SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK; |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | obj->spi->MCR &= ~(SPI_MCR_MDIS_MASK | SPI_MCR_HALT_MASK); |
kadonotakashi | 0:8fdf9a60065b | 42 | //obj->spi->MCR |= SPI_MCR_DIS_RXF_MASK | SPI_MCR_DIS_TXF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | // not halt in the debug mode |
kadonotakashi | 0:8fdf9a60065b | 45 | obj->spi->SR |= SPI_SR_EOQF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | // pin out the spi pins |
kadonotakashi | 0:8fdf9a60065b | 48 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
kadonotakashi | 0:8fdf9a60065b | 49 | pinmap_pinout(miso, PinMap_SPI_MISO); |
kadonotakashi | 0:8fdf9a60065b | 50 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
kadonotakashi | 0:8fdf9a60065b | 51 | if (ssel != NC) { |
kadonotakashi | 0:8fdf9a60065b | 52 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
kadonotakashi | 0:8fdf9a60065b | 53 | } |
kadonotakashi | 0:8fdf9a60065b | 54 | } |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | void spi_free(spi_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 57 | // [TODO] |
kadonotakashi | 0:8fdf9a60065b | 58 | } |
kadonotakashi | 0:8fdf9a60065b | 59 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
kadonotakashi | 0:8fdf9a60065b | 60 | MBED_ASSERT((bits > 4) || (bits < 16)); |
kadonotakashi | 0:8fdf9a60065b | 61 | MBED_ASSERT((mode >= 0) && (mode <= 3)); |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | uint8_t polarity = (mode & 0x2) ? 1 : 0; |
kadonotakashi | 0:8fdf9a60065b | 64 | uint8_t phase = (mode & 0x1) ? 1 : 0; |
kadonotakashi | 0:8fdf9a60065b | 65 | uint8_t old_polarity = (obj->spi->CTAR[0] & SPI_CTAR_CPOL_MASK) != 0; |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | // set master/slave |
kadonotakashi | 0:8fdf9a60065b | 68 | if (slave) { |
kadonotakashi | 0:8fdf9a60065b | 69 | obj->spi->MCR &= ~SPI_MCR_MSTR_MASK; |
kadonotakashi | 0:8fdf9a60065b | 70 | } else { |
kadonotakashi | 0:8fdf9a60065b | 71 | obj->spi->MCR |= (1UL << SPI_MCR_MSTR_SHIFT); |
kadonotakashi | 0:8fdf9a60065b | 72 | } |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | // CTAR0 is used |
kadonotakashi | 0:8fdf9a60065b | 75 | obj->spi->CTAR[0] &= ~(SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_FMSZ_MASK); |
kadonotakashi | 0:8fdf9a60065b | 76 | obj->spi->CTAR[0] |= (polarity << SPI_CTAR_CPOL_SHIFT) | (phase << SPI_CTAR_CPHA_SHIFT) | ((bits - 1) << SPI_CTAR_FMSZ_SHIFT); |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | //If clk idle state was changed, start a dummy transmission |
kadonotakashi | 0:8fdf9a60065b | 79 | //This is a 'feature' in DSPI: https://community.freescale.com/thread/105526 |
kadonotakashi | 0:8fdf9a60065b | 80 | if ((old_polarity != polarity) && (slave == 0)) { |
kadonotakashi | 0:8fdf9a60065b | 81 | //Start transfer (CS should be high, so shouldn't matter) |
kadonotakashi | 0:8fdf9a60065b | 82 | spi_master_write(obj, 0xFFFF); |
kadonotakashi | 0:8fdf9a60065b | 83 | } |
kadonotakashi | 0:8fdf9a60065b | 84 | } |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | static const uint8_t baudrate_prescaler[] = {2,3,5,7}; |
kadonotakashi | 0:8fdf9a60065b | 87 | static const uint16_t baudrate_scaler[] = {2,4,6,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}; |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | void spi_frequency(spi_t *obj, int hz) { |
kadonotakashi | 0:8fdf9a60065b | 90 | uint32_t f_error = 0; |
kadonotakashi | 0:8fdf9a60065b | 91 | uint32_t p_error = 0xffffffff; |
kadonotakashi | 0:8fdf9a60065b | 92 | uint32_t ref = 0; |
kadonotakashi | 0:8fdf9a60065b | 93 | uint32_t br = 0; |
kadonotakashi | 0:8fdf9a60065b | 94 | uint32_t ref_spr = 0; |
kadonotakashi | 0:8fdf9a60065b | 95 | uint32_t ref_prescaler = 0; |
kadonotakashi | 0:8fdf9a60065b | 96 | uint32_t ref_dr = 0; |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | // bus clk |
kadonotakashi | 0:8fdf9a60065b | 99 | uint32_t PCLK = bus_frequency(); |
kadonotakashi | 0:8fdf9a60065b | 100 | |
kadonotakashi | 0:8fdf9a60065b | 101 | for (uint32_t i = 0; i < 4; i++) { |
kadonotakashi | 0:8fdf9a60065b | 102 | for (br = 0; br <= 15; br++) { |
kadonotakashi | 0:8fdf9a60065b | 103 | for (uint32_t dr = 0; dr < 2; dr++) { |
kadonotakashi | 0:8fdf9a60065b | 104 | ref = (PCLK * (1U + dr) / baudrate_prescaler[i]) / baudrate_scaler[br]; |
kadonotakashi | 0:8fdf9a60065b | 105 | if (ref > (uint32_t)hz) |
kadonotakashi | 0:8fdf9a60065b | 106 | continue; |
kadonotakashi | 0:8fdf9a60065b | 107 | f_error = hz - ref; |
kadonotakashi | 0:8fdf9a60065b | 108 | if (f_error < p_error) { |
kadonotakashi | 0:8fdf9a60065b | 109 | ref_spr = br; |
kadonotakashi | 0:8fdf9a60065b | 110 | ref_prescaler = i; |
kadonotakashi | 0:8fdf9a60065b | 111 | ref_dr = dr; |
kadonotakashi | 0:8fdf9a60065b | 112 | p_error = f_error; |
kadonotakashi | 0:8fdf9a60065b | 113 | } |
kadonotakashi | 0:8fdf9a60065b | 114 | } |
kadonotakashi | 0:8fdf9a60065b | 115 | } |
kadonotakashi | 0:8fdf9a60065b | 116 | } |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | // set PBR and BR |
kadonotakashi | 0:8fdf9a60065b | 119 | obj->spi->CTAR[0] &= ~(SPI_CTAR_PBR_MASK | SPI_CTAR_BR_MASK | SPI_CTAR_DBR_MASK); |
kadonotakashi | 0:8fdf9a60065b | 120 | obj->spi->CTAR[0] |= (ref_prescaler << SPI_CTAR_PBR_SHIFT) | (ref_spr << SPI_CTAR_BR_SHIFT) | (ref_dr << SPI_CTAR_DBR_SHIFT); |
kadonotakashi | 0:8fdf9a60065b | 121 | } |
kadonotakashi | 0:8fdf9a60065b | 122 | |
kadonotakashi | 0:8fdf9a60065b | 123 | static inline int spi_writeable(spi_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 124 | return (obj->spi->SR & SPI_SR_TFFF_MASK) ? 1 : 0; |
kadonotakashi | 0:8fdf9a60065b | 125 | } |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | static inline int spi_readable(spi_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 128 | return (obj->spi->SR & SPI_SR_RFDF_MASK) ? 1 : 0; |
kadonotakashi | 0:8fdf9a60065b | 129 | } |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | int spi_master_write(spi_t *obj, int value) { |
kadonotakashi | 0:8fdf9a60065b | 132 | //clear RX buffer flag |
kadonotakashi | 0:8fdf9a60065b | 133 | obj->spi->SR |= SPI_SR_RFDF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 134 | // wait tx buffer empty |
kadonotakashi | 0:8fdf9a60065b | 135 | while(!spi_writeable(obj)); |
kadonotakashi | 0:8fdf9a60065b | 136 | obj->spi->PUSHR = SPI_PUSHR_TXDATA(value & 0xffff) /*| SPI_PUSHR_EOQ_MASK*/; |
kadonotakashi | 0:8fdf9a60065b | 137 | |
kadonotakashi | 0:8fdf9a60065b | 138 | // wait rx buffer full |
kadonotakashi | 0:8fdf9a60065b | 139 | while (!spi_readable(obj)); |
kadonotakashi | 0:8fdf9a60065b | 140 | return obj->spi->POPR; |
kadonotakashi | 0:8fdf9a60065b | 141 | } |
kadonotakashi | 0:8fdf9a60065b | 142 | |
kadonotakashi | 0:8fdf9a60065b | 143 | int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, |
kadonotakashi | 0:8fdf9a60065b | 144 | char *rx_buffer, int rx_length, char write_fill) { |
kadonotakashi | 0:8fdf9a60065b | 145 | int total = (tx_length > rx_length) ? tx_length : rx_length; |
kadonotakashi | 0:8fdf9a60065b | 146 | |
kadonotakashi | 0:8fdf9a60065b | 147 | for (int i = 0; i < total; i++) { |
kadonotakashi | 0:8fdf9a60065b | 148 | char out = (i < tx_length) ? tx_buffer[i] : write_fill; |
kadonotakashi | 0:8fdf9a60065b | 149 | char in = spi_master_write(obj, out); |
kadonotakashi | 0:8fdf9a60065b | 150 | if (i < rx_length) { |
kadonotakashi | 0:8fdf9a60065b | 151 | rx_buffer[i] = in; |
kadonotakashi | 0:8fdf9a60065b | 152 | } |
kadonotakashi | 0:8fdf9a60065b | 153 | } |
kadonotakashi | 0:8fdf9a60065b | 154 | |
kadonotakashi | 0:8fdf9a60065b | 155 | return total; |
kadonotakashi | 0:8fdf9a60065b | 156 | } |
kadonotakashi | 0:8fdf9a60065b | 157 | |
kadonotakashi | 0:8fdf9a60065b | 158 | int spi_slave_receive(spi_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 159 | return spi_readable(obj); |
kadonotakashi | 0:8fdf9a60065b | 160 | } |
kadonotakashi | 0:8fdf9a60065b | 161 | |
kadonotakashi | 0:8fdf9a60065b | 162 | int spi_slave_read(spi_t *obj) { |
kadonotakashi | 0:8fdf9a60065b | 163 | obj->spi->SR |= SPI_SR_RFDF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 164 | return obj->spi->POPR; |
kadonotakashi | 0:8fdf9a60065b | 165 | } |
kadonotakashi | 0:8fdf9a60065b | 166 | |
kadonotakashi | 0:8fdf9a60065b | 167 | void spi_slave_write(spi_t *obj, int value) { |
kadonotakashi | 0:8fdf9a60065b | 168 | while (!spi_writeable(obj)); |
kadonotakashi | 0:8fdf9a60065b | 169 | } |