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.
Fork of mbed_SPIS_multiByte_example_SOG by
Diff: spi_api_multibyte.c
- Revision:
- 5:6a1155885fc9
- Parent:
- 4:8e796b751cd3
- Child:
- 6:bce37a45b9cb
--- a/spi_api_multibyte.c Thu Jun 01 07:59:21 2017 +0000
+++ b/spi_api_multibyte.c Tue Jun 13 04:34:01 2017 +0000
@@ -1,28 +1,28 @@
-/*
+/*
* Copyright (c) 2013 Nordic Semiconductor ASA
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this list
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
- * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
- * integrated circuit in a product or a software update for such product, must reproduce
- * the above copyright notice, this list of conditions and the following disclaimer in
+ * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
+ * integrated circuit in a product or a software update for such product, must reproduce
+ * the above copyright notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
- * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
- * used to endorse or promote products derived from this software without specific prior
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without specific prior
* written permission.
*
- * 4. This software, with or without modification, must only be used with a
+ * 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
- * 5. Any software provided in binary or object form under this license must not be reverse
- * engineered, decompiled, modified and/or disassembled.
- *
+ * 5. Any software provided in binary or object form under this license must not be reverse
+ * engineered, decompiled, modified and/or disassembled.
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -33,7 +33,7 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
+ *
*/
@@ -50,14 +50,20 @@
#include "app_util_platform.h"
#if DEVICE_SPI_ASYNCH
- #define SPI_IDX(obj) ((obj)->spi.spi_idx)
+#define SPI_IDX(obj) ((obj)->spi.spi_idx)
#else
- #define SPI_IDX(obj) ((obj)->spi_idx)
+#define SPI_IDX(obj) ((obj)->spi_idx)
#endif
#define SPI_INFO(obj) (&m_spi_info[SPI_IDX(obj)])
#define MASTER_INST(obj) (&m_instances[SPI_IDX(obj)].master)
#define SLAVE_INST(obj) (&m_instances[SPI_IDX(obj)].slave)
+#define TEST_STRING "STR34567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890END"
+static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
+static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
+static uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
+
+
typedef struct {
bool initialized;
bool master;
@@ -80,13 +86,10 @@
#endif
} spi_info_t;
- #define TEST_STRING "012" //Tsungta, handle 32 bytes per SPI CS cycle
- #define DATA_LENGTH 16
- uint8_t m_tx_buf[DATA_LENGTH]; /**< TX buffer. */
- uint8_t m_rx_buf[sizeof(TEST_STRING)+1]; /**< RX buffer. */
- int m_length = sizeof(m_tx_buf); /**< Transfer length. */
- int receive_bytes;
-
+//#define TEST_STRING "012" //Tsungta, handle 32 bytes per SPI CS cycle
+
+int receive_bytes;
+
static spi_info_t m_spi_info[SPI_COUNT];
typedef struct {
@@ -116,7 +119,7 @@
SPIS2_IRQ,
(uint32_t) SPIM2_SPIS2_SPI2_IRQHandler
},
-#endif
+#endif
};
@@ -160,13 +163,13 @@
master_event_handler(SPI##idx##_INSTANCE_INDEX, p_event); \
}
#if SPI0_ENABLED
- MASTER_EVENT_HANDLER(0)
+MASTER_EVENT_HANDLER(0)
#endif
#if SPI1_ENABLED
- MASTER_EVENT_HANDLER(1)
+MASTER_EVENT_HANDLER(1)
#endif
#if SPI2_ENABLED
- MASTER_EVENT_HANDLER(2)
+MASTER_EVENT_HANDLER(2)
#endif
static nrf_drv_spi_handler_t const m_master_event_handlers[SPI_COUNT] = {
@@ -188,30 +191,39 @@
spi_info_t *p_spi_info = &m_spi_info[spi_idx];
if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) {
- // Signal that there is some data received that could be read.
- if(event.rx_amount == receive_bytes)//Tsungta
- p_spi_info->flag.readable = true;
+ p_spi_info->flag.readable = true;
+
+ // m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
+ }
- // And prepare for the next transfer.
- // Previous data set in 'spi_slave_write' (if any) has been transmitted,
- // now use the default one, until some new is set by 'spi_slave_write'.
- //m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
- nrf_drv_spis_buffers_set(&m_instances[spi_idx].slave,
- m_tx_buf, m_length, m_rx_buf, m_length);
- }
+ /*
+ if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) {
+ // Signal that there is some data received that could be read.
+ if(event.rx_amount == receive_bytes)//Tsungta
+ p_spi_info->flag.readable = true;
+
+ // And prepare for the next transfer.
+ // Previous data set in 'spi_slave_write' (if any) has been transmitted,
+ // now use the default one, until some new is set by 'spi_slave_write'.
+ //m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
+ nrf_drv_spis_buffers_set(&m_instances[spi_idx].slave,
+ m_tx_buf, m_length, m_rx_buf, m_length);
+ }
+ */
+
}
#define SLAVE_EVENT_HANDLER(idx) \
static void slave_event_handler_##idx(nrf_drv_spis_event_t event) { \
slave_event_handler(SPIS##idx##_INSTANCE_INDEX, event); \
}
#if SPIS0_ENABLED
- SLAVE_EVENT_HANDLER(0)
+SLAVE_EVENT_HANDLER(0)
#endif
#if SPIS1_ENABLED
- SLAVE_EVENT_HANDLER(1)
+SLAVE_EVENT_HANDLER(1)
#endif
#if SPIS2_ENABLED
- SLAVE_EVENT_HANDLER(2)
+SLAVE_EVENT_HANDLER(2)
#endif
static nrf_drv_spis_event_handler_t const m_slave_event_handlers[SPIS_COUNT] = {
@@ -259,22 +271,22 @@
}
void spi_init_multibyte(spi_mb_t *obj,
- PinName mosi, PinName miso, PinName sclk, PinName ssel)
+ PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
int i;
for (i = 0; i < SPI_COUNT; ++i) {
spi_info_t *p_spi_info = &m_spi_info[i];
if (!p_spi_info->initialized) {
-
+
NVIC_SetVector(spi_hanlder_desc[i].IRQn, spi_hanlder_desc[i].vector);
-
+
p_spi_info->sck_pin = (uint8_t)sclk;
p_spi_info->mosi_pin = (mosi != NC) ?
- (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
+ (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->miso_pin = (miso != NC) ?
- (uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED;
+ (uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->ss_pin = (ssel != NC) ?
- (uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED;
+ (uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0;
p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M;
@@ -286,14 +298,14 @@
nrf_drv_spi_t const *p_spi = &m_instances[i].master;
ret_code_t ret_code = nrf_drv_spi_init(p_spi,
- &config, m_master_event_handlers[i]);
+ &config, m_master_event_handlers[i]);
if (ret_code == NRF_SUCCESS) {
p_spi_info->initialized = true;
p_spi_info->master = true;
p_spi_info->flag.busy = false;
- #if DEVICE_SPI_ASYNCH
+#if DEVICE_SPI_ASYNCH
p_spi_info->handler = 0;
- #endif
+#endif
SPI_IDX(obj) = i;
return;
@@ -310,8 +322,7 @@
spi_info_t *p_spi_info = SPI_INFO(obj);
if (p_spi_info->master) {
nrf_drv_spi_uninit(MASTER_INST(obj));
- }
- else {
+ } else {
nrf_drv_spis_uninit(SLAVE_INST(obj));
}
p_spi_info->initialized = false;
@@ -324,17 +335,18 @@
void spi_format_multibyte(spi_mb_t *obj, int bits, int mode, int slave)
{
+ /*
if (bits != 8) {
error("Only 8-bits SPI is supported\r\n");
}
if (mode > 3) {
error("SPI format error\r\n");
}
+ */
spi_info_t *p_spi_info = SPI_INFO(obj);
- if (slave)
- {
+ if (slave) {
nrf_drv_spis_mode_t spi_modes[4] = {
NRF_DRV_SPIS_MODE_0,
NRF_DRV_SPIS_MODE_1,
@@ -352,8 +364,7 @@
// re-initialized (there is no other way to change its configuration).
else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
nrf_drv_spis_uninit(SLAVE_INST(obj));
- }
- else {
+ } else {
return;
}
@@ -365,46 +376,12 @@
nrf_drv_spis_config_t config;
prepare_slave_config(&config, p_spi_info);
(void)nrf_drv_spis_init(SLAVE_INST(obj), &config,
- m_slave_event_handlers[SPI_IDX(obj)]);
+ m_slave_event_handlers[SPI_IDX(obj)]);
// Prepare the slave for transfer.
//m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
nrf_drv_spis_buffers_set(SLAVE_INST(obj),
- m_tx_buf, m_length, m_rx_buf, m_length);
- }
- else // master
- {
- nrf_drv_spi_mode_t spi_modes[4] = {
- NRF_DRV_SPI_MODE_0,
- NRF_DRV_SPI_MODE_1,
- NRF_DRV_SPI_MODE_2,
- NRF_DRV_SPI_MODE_3,
- };
- nrf_drv_spi_mode_t new_mode = spi_modes[mode];
-
- // If the peripheral is currently working as a slave, the SDK driver
- // it uses needs to be switched from SPIS to SPI.
- if (!p_spi_info->master) {
- nrf_drv_spis_uninit(SLAVE_INST(obj));
- }
- // I the SPI mode has to be changed, the SDK's SPI driver needs to be
- // re-initialized (there is no other way to change its configuration).
- else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
- nrf_drv_spi_uninit(MASTER_INST(obj));
- }
- else {
- return;
- }
-
- p_spi_info->spi_mode = (uint8_t)new_mode;
- p_spi_info->master = true;
- p_spi_info->flag.busy = false;
-
- // Initialize SDK's SPI driver with the new configuration.
- nrf_drv_spi_config_t config;
- prepare_master_config(&config, p_spi_info);
- (void)nrf_drv_spi_init(MASTER_INST(obj), &config,
- m_master_event_handlers[SPI_IDX(obj)]);
+ m_tx_buf, m_length, m_rx_buf, m_length);
}
}
@@ -434,8 +411,7 @@
spi_info_t *p_spi_info = SPI_INFO(obj);
nrf_drv_spi_frequency_t new_frequency = freq_translate(hz);
- if (p_spi_info->master)
- {
+ if (p_spi_info->master) {
if (p_spi_info->frequency != new_frequency) {
p_spi_info->frequency = new_frequency;
@@ -445,7 +421,7 @@
nrf_drv_spi_t const *p_spi = MASTER_INST(obj);
nrf_drv_spi_uninit(p_spi);
(void)nrf_drv_spi_init(p_spi, &config,
- m_master_event_handlers[SPI_IDX(obj)]);
+ m_master_event_handlers[SPI_IDX(obj)]);
}
}
// There is no need to set anything in slaves when it comes to frequency,
@@ -464,7 +440,7 @@
//m_tx_buf = value;
p_spi_info->flag.busy = true;
(void)nrf_drv_spi_transfer(MASTER_INST(obj),
- m_tx_buf, m_length, m_rx_buf, m_length);
+ m_tx_buf, m_length, m_rx_buf, m_length);
while (p_spi_info->flag.busy) {
}
@@ -477,17 +453,32 @@
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(!p_spi_info->master);
return p_spi_info->flag.readable;
-;
+ ;
+}
+
+bool get_spi_slave_readable(spi_mb_t *obj)
+{
+ spi_info_t *p_spi_info = SPI_INFO(obj);
+ return p_spi_info->flag.readable;
}
int spi_slave_read_multibyte(spi_mb_t *obj)
{
+
spi_info_t *p_spi_info = SPI_INFO(obj);
+ memset(m_rx_buf, 0, m_length);
+ p_spi_info->flag.readable = false;
+
+ nrf_drv_spis_buffers_set(SLAVE_INST(obj),
+ m_tx_buf, m_length, m_rx_buf, m_length);
MBED_ASSERT(!p_spi_info->master);
+/*
while (!p_spi_info->flag.readable) {
+ __WFE();
}
- p_spi_info->flag.readable = false;
- return m_rx_buf;
+*/
+ //return m_rx_buf[223]; //Sog test
+ return &m_rx_buf;
}
void spi_slave_write_multibyte(spi_mb_t *obj, uint8_t *value, int bytes)
@@ -502,9 +493,9 @@
#if DEVICE_SPI_ASYNCH
void spi_master_transfer_multibyte(spi_mb_t *obj,
- const void *tx, size_t tx_length,
- void *rx, size_t rx_length, uint8_t bit_width,
- uint32_t handler, uint32_t event, DMAUsage hint)
+ const void *tx, size_t tx_length,
+ void *rx, size_t rx_length, uint8_t bit_width,
+ uint32_t handler, uint32_t event, DMAUsage hint)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(p_spi_info->master);
@@ -516,8 +507,8 @@
p_spi_info->flag.busy = true;
(void)nrf_drv_spi_transfer(MASTER_INST(obj),
- (uint8_t const *)tx, tx_length,
- (uint8_t *)rx, rx_length);
+ (uint8_t const *)tx, tx_length,
+ (uint8_t *)rx, rx_length);
}
uint32_t spi_irq_handler_asynch_multibyte(spi_mb_t *obj)
