Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
mbed-pokitto/targets/hal/TARGET_NXP/TARGET_LPC11U6X/spi_api.c@5:ea7377f3d1af, 2017-10-11 (annotated)
- Committer:
- Pokitto
- Date:
- Wed Oct 11 20:35:27 2017 +0000
- Revision:
- 5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Pokitto | 5:ea7377f3d1af | 1 | /* mbed Microcontroller Library |
| Pokitto | 5:ea7377f3d1af | 2 | * Copyright (c) 2006-2013 ARM Limited |
| Pokitto | 5:ea7377f3d1af | 3 | * |
| Pokitto | 5:ea7377f3d1af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| Pokitto | 5:ea7377f3d1af | 5 | * you may not use this file except in compliance with the License. |
| Pokitto | 5:ea7377f3d1af | 6 | * You may obtain a copy of the License at |
| Pokitto | 5:ea7377f3d1af | 7 | * |
| Pokitto | 5:ea7377f3d1af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Pokitto | 5:ea7377f3d1af | 9 | * |
| Pokitto | 5:ea7377f3d1af | 10 | * Unless required by applicable law or agreed to in writing, software |
| Pokitto | 5:ea7377f3d1af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| Pokitto | 5:ea7377f3d1af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| Pokitto | 5:ea7377f3d1af | 13 | * See the License for the specific language governing permissions and |
| Pokitto | 5:ea7377f3d1af | 14 | * limitations under the License. |
| Pokitto | 5:ea7377f3d1af | 15 | */ |
| Pokitto | 5:ea7377f3d1af | 16 | #include "mbed_assert.h" |
| Pokitto | 5:ea7377f3d1af | 17 | #include <math.h> |
| Pokitto | 5:ea7377f3d1af | 18 | |
| Pokitto | 5:ea7377f3d1af | 19 | #include "spi_api.h" |
| Pokitto | 5:ea7377f3d1af | 20 | #include "cmsis.h" |
| Pokitto | 5:ea7377f3d1af | 21 | #include "pinmap.h" |
| Pokitto | 5:ea7377f3d1af | 22 | #include "mbed_error.h" |
| Pokitto | 5:ea7377f3d1af | 23 | |
| Pokitto | 5:ea7377f3d1af | 24 | #if DEVICE_SPI |
| Pokitto | 5:ea7377f3d1af | 25 | |
| Pokitto | 5:ea7377f3d1af | 26 | static const PinMap PinMap_SPI_SCLK[] = { |
| Pokitto | 5:ea7377f3d1af | 27 | {P0_6 , SPI_0, 0x02}, |
| Pokitto | 5:ea7377f3d1af | 28 | {P1_29, SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 29 | {P2_7 , SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 30 | {P1_20, SPI_1, 0x02}, |
| Pokitto | 5:ea7377f3d1af | 31 | {P1_27, SPI_1, 0x04}, |
| Pokitto | 5:ea7377f3d1af | 32 | {NC , NC , 0} |
| Pokitto | 5:ea7377f3d1af | 33 | }; |
| Pokitto | 5:ea7377f3d1af | 34 | |
| Pokitto | 5:ea7377f3d1af | 35 | static const PinMap PinMap_SPI_MOSI[] = { |
| Pokitto | 5:ea7377f3d1af | 36 | {P0_9 , SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 37 | {P1_12, SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 38 | {P0_21, SPI_1, 0x02}, |
| Pokitto | 5:ea7377f3d1af | 39 | {P1_22, SPI_1, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 40 | {NC , NC , 0} |
| Pokitto | 5:ea7377f3d1af | 41 | }; |
| Pokitto | 5:ea7377f3d1af | 42 | |
| Pokitto | 5:ea7377f3d1af | 43 | static const PinMap PinMap_SPI_MISO[] = { |
| Pokitto | 5:ea7377f3d1af | 44 | {P0_8 , SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 45 | {P1_16, SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 46 | {P0_22, SPI_1, 0x03}, |
| Pokitto | 5:ea7377f3d1af | 47 | {P1_21, SPI_1, 0x02}, |
| Pokitto | 5:ea7377f3d1af | 48 | {NC , NC , 0} |
| Pokitto | 5:ea7377f3d1af | 49 | }; |
| Pokitto | 5:ea7377f3d1af | 50 | |
| Pokitto | 5:ea7377f3d1af | 51 | static const PinMap PinMap_SPI_SSEL[] = { |
| Pokitto | 5:ea7377f3d1af | 52 | {P0_2 , SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 53 | {P1_15, SPI_0, 0x01}, |
| Pokitto | 5:ea7377f3d1af | 54 | {P0_23, SPI_1, 0x04}, |
| Pokitto | 5:ea7377f3d1af | 55 | {P1_23, SPI_1, 0x02}, |
| Pokitto | 5:ea7377f3d1af | 56 | {NC , NC , 0} |
| Pokitto | 5:ea7377f3d1af | 57 | }; |
| Pokitto | 5:ea7377f3d1af | 58 | |
| Pokitto | 5:ea7377f3d1af | 59 | static inline int ssp_disable(spi_t *obj); |
| Pokitto | 5:ea7377f3d1af | 60 | static inline int ssp_enable(spi_t *obj); |
| Pokitto | 5:ea7377f3d1af | 61 | |
| Pokitto | 5:ea7377f3d1af | 62 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
| Pokitto | 5:ea7377f3d1af | 63 | // determine the SPI to use |
| Pokitto | 5:ea7377f3d1af | 64 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
| Pokitto | 5:ea7377f3d1af | 65 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
| Pokitto | 5:ea7377f3d1af | 66 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
| Pokitto | 5:ea7377f3d1af | 67 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
| Pokitto | 5:ea7377f3d1af | 68 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
| Pokitto | 5:ea7377f3d1af | 69 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
| Pokitto | 5:ea7377f3d1af | 70 | |
| Pokitto | 5:ea7377f3d1af | 71 | obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl); |
| Pokitto | 5:ea7377f3d1af | 72 | MBED_ASSERT((int)obj->spi != NC); |
| Pokitto | 5:ea7377f3d1af | 73 | |
| Pokitto | 5:ea7377f3d1af | 74 | // enable power and clocking |
| Pokitto | 5:ea7377f3d1af | 75 | switch ((int)obj->spi) { |
| Pokitto | 5:ea7377f3d1af | 76 | case SPI_0: |
| Pokitto | 5:ea7377f3d1af | 77 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 11; |
| Pokitto | 5:ea7377f3d1af | 78 | LPC_SYSCON->SSP0CLKDIV = 0x01; |
| Pokitto | 5:ea7377f3d1af | 79 | LPC_SYSCON->PRESETCTRL |= 1 << 0; |
| Pokitto | 5:ea7377f3d1af | 80 | break; |
| Pokitto | 5:ea7377f3d1af | 81 | case SPI_1: |
| Pokitto | 5:ea7377f3d1af | 82 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 18; |
| Pokitto | 5:ea7377f3d1af | 83 | LPC_SYSCON->SSP1CLKDIV = 0x01; |
| Pokitto | 5:ea7377f3d1af | 84 | LPC_SYSCON->PRESETCTRL |= 1 << 2; |
| Pokitto | 5:ea7377f3d1af | 85 | break; |
| Pokitto | 5:ea7377f3d1af | 86 | } |
| Pokitto | 5:ea7377f3d1af | 87 | |
| Pokitto | 5:ea7377f3d1af | 88 | // pin out the spi pins |
| Pokitto | 5:ea7377f3d1af | 89 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
| Pokitto | 5:ea7377f3d1af | 90 | pinmap_pinout(miso, PinMap_SPI_MISO); |
| Pokitto | 5:ea7377f3d1af | 91 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
| Pokitto | 5:ea7377f3d1af | 92 | if (ssel != NC) { |
| Pokitto | 5:ea7377f3d1af | 93 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
| Pokitto | 5:ea7377f3d1af | 94 | } |
| Pokitto | 5:ea7377f3d1af | 95 | } |
| Pokitto | 5:ea7377f3d1af | 96 | |
| Pokitto | 5:ea7377f3d1af | 97 | void spi_free(spi_t *obj) {} |
| Pokitto | 5:ea7377f3d1af | 98 | |
| Pokitto | 5:ea7377f3d1af | 99 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
| Pokitto | 5:ea7377f3d1af | 100 | ssp_disable(obj); |
| Pokitto | 5:ea7377f3d1af | 101 | MBED_ASSERT(((bits >= 4) && (bits <= 16)) || ((mode >= 0) && (mode <= 3))); |
| Pokitto | 5:ea7377f3d1af | 102 | |
| Pokitto | 5:ea7377f3d1af | 103 | int polarity = (mode & 0x2) ? 1 : 0; |
| Pokitto | 5:ea7377f3d1af | 104 | int phase = (mode & 0x1) ? 1 : 0; |
| Pokitto | 5:ea7377f3d1af | 105 | |
| Pokitto | 5:ea7377f3d1af | 106 | // set it up |
| Pokitto | 5:ea7377f3d1af | 107 | int DSS = bits - 1; // DSS (data select size) |
| Pokitto | 5:ea7377f3d1af | 108 | int SPO = (polarity) ? 1 : 0; // SPO - clock out polarity |
| Pokitto | 5:ea7377f3d1af | 109 | int SPH = (phase) ? 1 : 0; // SPH - clock out phase |
| Pokitto | 5:ea7377f3d1af | 110 | |
| Pokitto | 5:ea7377f3d1af | 111 | int FRF = 0; // FRF (frame format) = SPI |
| Pokitto | 5:ea7377f3d1af | 112 | uint32_t tmp = obj->spi->CR0; |
| Pokitto | 5:ea7377f3d1af | 113 | tmp &= ~(0xFFFF); |
| Pokitto | 5:ea7377f3d1af | 114 | tmp |= DSS << 0 |
| Pokitto | 5:ea7377f3d1af | 115 | | FRF << 4 |
| Pokitto | 5:ea7377f3d1af | 116 | | SPO << 6 |
| Pokitto | 5:ea7377f3d1af | 117 | | SPH << 7; |
| Pokitto | 5:ea7377f3d1af | 118 | obj->spi->CR0 = tmp; |
| Pokitto | 5:ea7377f3d1af | 119 | |
| Pokitto | 5:ea7377f3d1af | 120 | tmp = obj->spi->CR1; |
| Pokitto | 5:ea7377f3d1af | 121 | tmp &= ~(0xD); |
| Pokitto | 5:ea7377f3d1af | 122 | tmp |= 0 << 0 // LBM - loop back mode - off |
| Pokitto | 5:ea7377f3d1af | 123 | | ((slave) ? 1 : 0) << 2 // MS - master slave mode, 1 = slave |
| Pokitto | 5:ea7377f3d1af | 124 | | 0 << 3; // SOD - slave output disable - na |
| Pokitto | 5:ea7377f3d1af | 125 | obj->spi->CR1 = tmp; |
| Pokitto | 5:ea7377f3d1af | 126 | |
| Pokitto | 5:ea7377f3d1af | 127 | ssp_enable(obj); |
| Pokitto | 5:ea7377f3d1af | 128 | } |
| Pokitto | 5:ea7377f3d1af | 129 | |
| Pokitto | 5:ea7377f3d1af | 130 | void spi_frequency(spi_t *obj, int hz) { |
| Pokitto | 5:ea7377f3d1af | 131 | ssp_disable(obj); |
| Pokitto | 5:ea7377f3d1af | 132 | |
| Pokitto | 5:ea7377f3d1af | 133 | uint32_t PCLK = SystemCoreClock; |
| Pokitto | 5:ea7377f3d1af | 134 | |
| Pokitto | 5:ea7377f3d1af | 135 | int prescaler; |
| Pokitto | 5:ea7377f3d1af | 136 | |
| Pokitto | 5:ea7377f3d1af | 137 | for (prescaler = 2; prescaler <= 254; prescaler += 2) { |
| Pokitto | 5:ea7377f3d1af | 138 | int prescale_hz = PCLK / prescaler; |
| Pokitto | 5:ea7377f3d1af | 139 | |
| Pokitto | 5:ea7377f3d1af | 140 | // calculate the divider |
| Pokitto | 5:ea7377f3d1af | 141 | int divider = floor(((float)prescale_hz / (float)hz) + 0.5f); |
| Pokitto | 5:ea7377f3d1af | 142 | |
| Pokitto | 5:ea7377f3d1af | 143 | // check we can support the divider |
| Pokitto | 5:ea7377f3d1af | 144 | if (divider < 256) { |
| Pokitto | 5:ea7377f3d1af | 145 | // prescaler |
| Pokitto | 5:ea7377f3d1af | 146 | obj->spi->CPSR = prescaler; |
| Pokitto | 5:ea7377f3d1af | 147 | |
| Pokitto | 5:ea7377f3d1af | 148 | // divider |
| Pokitto | 5:ea7377f3d1af | 149 | obj->spi->CR0 &= ~(0xFFFF << 8); |
| Pokitto | 5:ea7377f3d1af | 150 | obj->spi->CR0 |= (divider - 1) << 8; |
| Pokitto | 5:ea7377f3d1af | 151 | ssp_enable(obj); |
| Pokitto | 5:ea7377f3d1af | 152 | return; |
| Pokitto | 5:ea7377f3d1af | 153 | } |
| Pokitto | 5:ea7377f3d1af | 154 | } |
| Pokitto | 5:ea7377f3d1af | 155 | error("Couldn't setup requested SPI frequency"); |
| Pokitto | 5:ea7377f3d1af | 156 | } |
| Pokitto | 5:ea7377f3d1af | 157 | |
| Pokitto | 5:ea7377f3d1af | 158 | static inline int ssp_disable(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 159 | return obj->spi->CR1 &= ~(1 << 1); |
| Pokitto | 5:ea7377f3d1af | 160 | } |
| Pokitto | 5:ea7377f3d1af | 161 | |
| Pokitto | 5:ea7377f3d1af | 162 | static inline int ssp_enable(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 163 | return obj->spi->CR1 |= (1 << 1); |
| Pokitto | 5:ea7377f3d1af | 164 | } |
| Pokitto | 5:ea7377f3d1af | 165 | |
| Pokitto | 5:ea7377f3d1af | 166 | static inline int ssp_readable(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 167 | return obj->spi->SR & (1 << 2); |
| Pokitto | 5:ea7377f3d1af | 168 | } |
| Pokitto | 5:ea7377f3d1af | 169 | |
| Pokitto | 5:ea7377f3d1af | 170 | static inline int ssp_writeable(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 171 | return obj->spi->SR & (1 << 1); |
| Pokitto | 5:ea7377f3d1af | 172 | } |
| Pokitto | 5:ea7377f3d1af | 173 | |
| Pokitto | 5:ea7377f3d1af | 174 | static inline void ssp_write(spi_t *obj, int value) { |
| Pokitto | 5:ea7377f3d1af | 175 | while (!ssp_writeable(obj)); |
| Pokitto | 5:ea7377f3d1af | 176 | obj->spi->DR = value; |
| Pokitto | 5:ea7377f3d1af | 177 | } |
| Pokitto | 5:ea7377f3d1af | 178 | |
| Pokitto | 5:ea7377f3d1af | 179 | static inline int ssp_read(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 180 | while (!ssp_readable(obj)); |
| Pokitto | 5:ea7377f3d1af | 181 | return obj->spi->DR; |
| Pokitto | 5:ea7377f3d1af | 182 | } |
| Pokitto | 5:ea7377f3d1af | 183 | |
| Pokitto | 5:ea7377f3d1af | 184 | static inline int ssp_busy(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 185 | return (obj->spi->SR & (1 << 4)) ? (1) : (0); |
| Pokitto | 5:ea7377f3d1af | 186 | } |
| Pokitto | 5:ea7377f3d1af | 187 | |
| Pokitto | 5:ea7377f3d1af | 188 | int spi_master_write(spi_t *obj, int value) { |
| Pokitto | 5:ea7377f3d1af | 189 | ssp_write(obj, value); |
| Pokitto | 5:ea7377f3d1af | 190 | return ssp_read(obj); |
| Pokitto | 5:ea7377f3d1af | 191 | } |
| Pokitto | 5:ea7377f3d1af | 192 | |
| Pokitto | 5:ea7377f3d1af | 193 | int spi_slave_receive(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 194 | return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); |
| Pokitto | 5:ea7377f3d1af | 195 | } |
| Pokitto | 5:ea7377f3d1af | 196 | |
| Pokitto | 5:ea7377f3d1af | 197 | int spi_slave_read(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 198 | return obj->spi->DR; |
| Pokitto | 5:ea7377f3d1af | 199 | } |
| Pokitto | 5:ea7377f3d1af | 200 | |
| Pokitto | 5:ea7377f3d1af | 201 | void spi_slave_write(spi_t *obj, int value) { |
| Pokitto | 5:ea7377f3d1af | 202 | while (ssp_writeable(obj) == 0) ; |
| Pokitto | 5:ea7377f3d1af | 203 | obj->spi->DR = value; |
| Pokitto | 5:ea7377f3d1af | 204 | } |
| Pokitto | 5:ea7377f3d1af | 205 | |
| Pokitto | 5:ea7377f3d1af | 206 | int spi_busy(spi_t *obj) { |
| Pokitto | 5:ea7377f3d1af | 207 | return ssp_busy(obj); |
| Pokitto | 5:ea7377f3d1af | 208 | } |
| Pokitto | 5:ea7377f3d1af | 209 | |
| Pokitto | 5:ea7377f3d1af | 210 | #endif |