2014-02-13 07:52:38 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-05-28 14:38:00 +00:00
|
|
|
#include "mbed_assert.h"
|
2014-02-13 07:52:38 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "spi_api.h"
|
|
|
|
#include "cmsis.h"
|
|
|
|
#include "pinmap.h"
|
2014-08-14 14:16:47 +00:00
|
|
|
#include "mbed_error.h"
|
2014-02-13 07:52:38 +00:00
|
|
|
|
|
|
|
static const SWM_Map SWM_SPI_SSEL[] = {
|
|
|
|
{4, 0},
|
|
|
|
{5, 24},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SWM_Map SWM_SPI_SCLK[] = {
|
|
|
|
{3, 8},
|
|
|
|
{5, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SWM_Map SWM_SPI_MOSI[] = {
|
|
|
|
{3, 16},
|
|
|
|
{5, 8},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SWM_Map SWM_SPI_MISO[] = {
|
|
|
|
{3, 24},
|
|
|
|
{5, 16},
|
|
|
|
};
|
|
|
|
|
2019-02-02 16:51:54 +00:00
|
|
|
// Pinmap used for testing only
|
|
|
|
static const PinMap PinMap_SPI_testing[] = {
|
|
|
|
{P0_0, 0, 0},
|
|
|
|
{P0_1, 0, 0},
|
|
|
|
{P0_2, 0, 0},
|
|
|
|
{P0_3, 0, 0},
|
|
|
|
{P0_4, 0, 0},
|
|
|
|
{P0_5, 0, 0},
|
|
|
|
{P0_6, 0, 0},
|
|
|
|
{P0_7, 0, 0},
|
|
|
|
{P0_8, 0, 0},
|
|
|
|
{P0_9, 0, 0},
|
|
|
|
{P0_10, 0, 0},
|
|
|
|
{P0_11, 0, 0},
|
|
|
|
{P0_12, 0, 0},
|
|
|
|
{P0_13, 0, 0},
|
|
|
|
{P0_14, 0, 0},
|
|
|
|
{P0_15, 0, 0},
|
|
|
|
{P0_16, 0, 0},
|
|
|
|
{P0_17, 0, 0},
|
|
|
|
{P0_18, 0, 0},
|
|
|
|
{P0_19, 0, 0},
|
|
|
|
{P0_20, 0, 0},
|
|
|
|
{P0_21, 0, 0},
|
|
|
|
{P0_22, 0, 0},
|
|
|
|
{P0_23, 0, 0},
|
|
|
|
{P0_24, 0, 0},
|
|
|
|
{P0_25, 0, 0},
|
|
|
|
{P0_26, 0, 0},
|
|
|
|
{P0_27, 0, 0},
|
|
|
|
{P0_28, 0, 0},
|
|
|
|
{P0_29, 0, 0},
|
|
|
|
{P0_30, 0, 0},
|
|
|
|
{P0_31, 0, 0},
|
|
|
|
{P1_0, 0, 0},
|
|
|
|
{P1_1, 0, 0},
|
|
|
|
{P1_2, 0, 0},
|
|
|
|
{P1_3, 0, 0},
|
|
|
|
{P1_4, 0, 0},
|
|
|
|
{P1_5, 0, 0},
|
|
|
|
{P1_6, 0, 0},
|
|
|
|
{P1_7, 0, 0},
|
|
|
|
{P1_8, 0, 0},
|
|
|
|
{P1_9, 0, 0},
|
|
|
|
{P1_10, 0, 0},
|
|
|
|
{P1_11, 0, 0},
|
|
|
|
{P1_12, 0, 0},
|
|
|
|
{P1_13, 0, 0},
|
|
|
|
{P1_14, 0, 0},
|
|
|
|
{P1_15, 0, 0},
|
|
|
|
{P1_16, 0, 0},
|
|
|
|
{P1_17, 0, 0},
|
|
|
|
{P1_18, 0, 0},
|
|
|
|
{P1_19, 0, 0},
|
|
|
|
{P1_20, 0, 0},
|
|
|
|
{P1_21, 0, 0},
|
|
|
|
{P1_22, 0, 0},
|
|
|
|
{P1_23, 0, 0},
|
|
|
|
{P1_24, 0, 0},
|
|
|
|
{P1_25, 0, 0},
|
|
|
|
{P1_26, 0, 0},
|
|
|
|
{P1_27, 0, 0},
|
|
|
|
{P1_28, 0, 0},
|
|
|
|
{P1_29, 0, 0},
|
|
|
|
{P1_30, 0, 0},
|
|
|
|
{P1_31, 0, 0},
|
|
|
|
{P2_0, 0, 0},
|
|
|
|
{P2_1, 0, 0},
|
|
|
|
{P2_2, 0, 0},
|
|
|
|
{P2_3, 0, 0},
|
|
|
|
{P2_4, 0, 0},
|
|
|
|
{P2_5, 0, 0},
|
|
|
|
{P2_6, 0, 0},
|
|
|
|
{P2_7, 0, 0},
|
|
|
|
{P2_8, 0, 0},
|
|
|
|
{P2_9, 0, 0},
|
|
|
|
{P2_10, 0, 0},
|
|
|
|
{P2_11, 0, 0},
|
|
|
|
{P2_12, 0, 0},
|
|
|
|
|
|
|
|
{NC, NC, 0}
|
|
|
|
};
|
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
// bit flags for used SPIs
|
|
|
|
static unsigned char spi_used = 0;
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
static int get_available_spi(PinName mosi, PinName miso, PinName sclk, PinName ssel)
|
2014-07-20 14:04:43 +00:00
|
|
|
{
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
if (spi_used == 0) {
|
|
|
|
return 0; // The first user
|
2014-02-13 07:52:38 +00:00
|
|
|
}
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
|
|
|
|
const SWM_Map *swm;
|
|
|
|
uint32_t regVal;
|
|
|
|
|
|
|
|
// Investigate if same pins as the used SPI0/1 - to be able to reuse it
|
|
|
|
for (int spi_n = 0; spi_n < 2; spi_n++) {
|
|
|
|
if (spi_used & (1<<spi_n)) {
|
|
|
|
if (sclk != NC) {
|
|
|
|
swm = &SWM_SPI_SCLK[spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
|
|
|
|
if (regVal != (sclk << swm->offset)) {
|
|
|
|
// Existing pin is not the same as the one we want
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mosi != NC) {
|
|
|
|
swm = &SWM_SPI_MOSI[spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
|
|
|
|
if (regVal != (mosi << swm->offset)) {
|
|
|
|
// Existing pin is not the same as the one we want
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (miso != NC) {
|
|
|
|
swm = &SWM_SPI_MISO[spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
|
|
|
|
if (regVal != (miso << swm->offset)) {
|
|
|
|
// Existing pin is not the same as the one we want
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssel != NC) {
|
|
|
|
swm = &SWM_SPI_SSEL[spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
|
|
|
|
if (regVal != (ssel << swm->offset)) {
|
|
|
|
// Existing pin is not the same as the one we want
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pins for the currently used SPIx are the same as the
|
|
|
|
// ones we want so we will reuse it
|
|
|
|
return spi_n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the existing SPIx pin setups match the pins we want
|
|
|
|
// so the last hope is to select one unused SPIx
|
|
|
|
if ((spi_used & 1) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if ((spi_used & 2) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No matching setup and no free SPIx
|
2014-02-13 07:52:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spi_disable(spi_t *obj);
|
|
|
|
static inline void spi_enable(spi_t *obj);
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
|
|
|
|
{
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
int spi_n = get_available_spi(mosi, miso, sclk, ssel);
|
2014-02-13 07:52:38 +00:00
|
|
|
if (spi_n == -1) {
|
|
|
|
error("No available SPI");
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->spi_n = spi_n;
|
|
|
|
spi_used |= (1 << spi_n);
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
obj->spi = (spi_n) ? (LPC_SPI0_Type *)(LPC_SPI1_BASE) : (LPC_SPI0_Type *)(LPC_SPI0_BASE);
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
const SWM_Map *swm;
|
|
|
|
uint32_t regVal;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
if (sclk != NC) {
|
|
|
|
swm = &SWM_SPI_SCLK[obj->spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
|
|
|
|
LPC_SWM->PINASSIGN[swm->n] = regVal | (sclk << swm->offset);
|
|
|
|
}
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
if (mosi != NC) {
|
|
|
|
swm = &SWM_SPI_MOSI[obj->spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
|
|
|
|
LPC_SWM->PINASSIGN[swm->n] = regVal | (mosi << swm->offset);
|
|
|
|
}
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
if (miso != NC) {
|
|
|
|
swm = &SWM_SPI_MISO[obj->spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
|
|
|
|
LPC_SWM->PINASSIGN[swm->n] = regVal | (miso << swm->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssel != NC) {
|
|
|
|
swm = &SWM_SPI_SSEL[obj->spi_n];
|
|
|
|
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
|
|
|
|
LPC_SWM->PINASSIGN[swm->n] = regVal | (ssel << swm->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear interrupts
|
|
|
|
obj->spi->INTENCLR = 0x3f;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
// enable power and clocking
|
2014-07-20 14:04:43 +00:00
|
|
|
LPC_SYSCON->SYSAHBCLKCTRL1 |= (0x1 << (obj->spi_n + 9));
|
|
|
|
LPC_SYSCON->PRESETCTRL1 |= (0x1 << (obj->spi_n + 9));
|
|
|
|
LPC_SYSCON->PRESETCTRL1 &= ~(0x1 << (obj->spi_n + 9));
|
2014-02-13 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
void spi_free(spi_t *obj)
|
|
|
|
{
|
|
|
|
}
|
2014-02-13 07:52:38 +00:00
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
void spi_format(spi_t *obj, int bits, int mode, int slave)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
spi_disable(obj);
|
2014-05-28 13:29:12 +00:00
|
|
|
MBED_ASSERT((bits >= 1 && bits <= 16) && (mode >= 0 && mode <= 3));
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
int polarity = (mode & 0x2) ? 1 : 0;
|
|
|
|
int phase = (mode & 0x1) ? 1 : 0;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
// set it up
|
|
|
|
int LEN = bits - 1; // LEN - Data Length
|
|
|
|
int CPOL = (polarity) ? 1 : 0; // CPOL - Clock Polarity select
|
|
|
|
int CPHA = (phase) ? 1 : 0; // CPHA - Clock Phase select
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
uint32_t tmp = obj->spi->CFG;
|
|
|
|
tmp &= ~((1 << 5) | (1 << 4) | (1 << 2));
|
|
|
|
tmp |= (CPOL << 5) | (CPHA << 4) | ((slave ? 0 : 1) << 2);
|
|
|
|
obj->spi->CFG = tmp;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
// select frame length
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
tmp = obj->spi->TXCTL;
|
2014-02-13 07:52:38 +00:00
|
|
|
tmp &= ~(0xf << 24);
|
|
|
|
tmp |= (LEN << 24);
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
obj->spi->TXCTL = tmp;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
spi_enable(obj);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
void spi_frequency(spi_t *obj, int hz)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
spi_disable(obj);
|
2014-07-20 14:04:43 +00:00
|
|
|
|
|
|
|
// rise DIV value if it cannot be divided
|
|
|
|
obj->spi->DIV = (SystemCoreClock + (hz - 1))/hz - 1;
|
2014-02-13 07:52:38 +00:00
|
|
|
obj->spi->DLY = 0;
|
2014-07-20 14:04:43 +00:00
|
|
|
|
2014-02-13 07:52:38 +00:00
|
|
|
spi_enable(obj);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline void spi_disable(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
obj->spi->CFG &= ~(1 << 0);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline void spi_enable(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
obj->spi->CFG |= (1 << 0);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline int spi_readable(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
return obj->spi->STAT & (1 << 0);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline int spi_writeable(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
return obj->spi->STAT & (1 << 1);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline void spi_write(spi_t *obj, int value)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
while (!spi_writeable(obj));
|
|
|
|
// end of transfer
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
obj->spi->TXCTL |= (1 << 20);
|
|
|
|
obj->spi->TXDAT = (value & 0xffff);
|
2014-02-13 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
static inline int spi_read(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
while (!spi_readable(obj));
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
return obj->spi->RXDAT & 0xffff; // Only the lower 16 bits contain data
|
2014-02-13 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
int spi_busy(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
// checking RXOV(Receiver Overrun interrupt flag)
|
|
|
|
return obj->spi->STAT & (1 << 2);
|
2014-07-20 14:04:43 +00:00
|
|
|
}
|
2014-02-13 07:52:38 +00:00
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
int spi_master_write(spi_t *obj, int value)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
spi_write(obj, value);
|
|
|
|
return spi_read(obj);
|
|
|
|
}
|
|
|
|
|
2017-07-11 21:10:14 +00:00
|
|
|
int spi_master_block_write(spi_t *obj, const char *tx_buffer,
|
|
|
|
int tx_length, char *rx_buffer, int rx_length, char write_fill) {
|
2017-05-24 21:54:03 +00:00
|
|
|
int total = (tx_length > rx_length) ? tx_length : rx_length;
|
|
|
|
|
|
|
|
for (int i = 0; i < total; i++) {
|
2017-07-11 21:10:14 +00:00
|
|
|
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
|
2017-05-24 21:54:03 +00:00
|
|
|
char in = spi_master_write(obj, out);
|
|
|
|
if (i < rx_length) {
|
|
|
|
rx_buffer[i] = in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
int spi_slave_receive(spi_t *obj)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
return (spi_readable(obj) && !spi_busy(obj)) ? (1) : (0);
|
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
int spi_slave_read(spi_t *obj)
|
|
|
|
{
|
Fixed three issues with the SPI driver for the LPC1549 platform:
* The TXDATCTL register was used even if only the control signals were
modified which caused extra data to be transmitted.
* The RXDAT register does not only contain the received data, but also
control information in bits 16 to 20. The old code did not mask out
the control information and in rare cases that would cause the returned
data to include too much information (i.e. received 0xaa as data but the
function returned 0x300aa).
* The LPC1549 uses a Switch Matric (SWM) to allow any pin to have any
function. This is not used in the old code which simply assigned
the first instance of the SPI class to SPI0 and the second instance to
SPI1. The third instance would result in a call to error().
This behaviour is not at all working with real world examples where the
SPI bus contains more than two peripherals. The third peripheral would
cause the platform to end up in error().
The solution is to modify the get_available_spi() function to first see
if the MISO/MOSI/SCLK and SSEL pins are already configured for use as
either SPI0 or SPI1. If the exact same pins are already used then the
SPIx will be reused. If one or more pins are different then another
SPIx will be used (or if both are alredy in use then error()). With this
change it is now possible to do this:
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI0
SDFileSystem s(D11,D12,D13,"sd"); // Will use SPI0
MyDisplay d(D11,D12,D13); // Will use SPI0
The old/existing code would have resulted in this
MyFlash f(D11,D12,D13); // Will use SPI0
MyTemp t(D11,D12,D13); // Will use SPI1
SDFileSystem s(D11,D12,D13,"sd"); // error()
MyDisplay d(D11,D12,D13); // Will never be called
2014-07-23 08:15:14 +00:00
|
|
|
return obj->spi->RXDAT & 0xffff; // Only the lower 16 bits contain data
|
2014-02-13 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 14:04:43 +00:00
|
|
|
void spi_slave_write(spi_t *obj, int value)
|
|
|
|
{
|
2014-02-13 07:52:38 +00:00
|
|
|
while (spi_writeable(obj) == 0) ;
|
|
|
|
obj->spi->TXDAT = value;
|
|
|
|
}
|
2019-01-24 05:13:12 +00:00
|
|
|
|
|
|
|
const PinMap *spi_master_mosi_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_master_miso_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_master_clk_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_master_cs_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_slave_mosi_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_slave_miso_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_slave_clk_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PinMap *spi_slave_cs_pinmap()
|
|
|
|
{
|
2019-02-02 16:51:54 +00:00
|
|
|
return PinMap_SPI_testing;
|
2019-01-24 05:13:12 +00:00
|
|
|
}
|