Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Examples,PeriphDrivers)!: Add SPI v2 Target Transaction functions #662

Merged
merged 97 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
a6e6cde
feat(PeriphDrivers): Add SPI Target Transaction functions
sihyung-maxim Jul 11, 2023
7c4f0d9
feat(Examples): Update SPI_MasterSlave example to use SPI v2 function…
sihyung-maxim Jul 11, 2023
3bf0491
fix(Examples): Update SPI README for MAX78002
sihyung-maxim Jul 11, 2023
c499001
clang-format bot reformatting.
sihyung-maxim Jul 11, 2023
4a952f3
feat(Examples): Rename SPI_MasterSlave example to SPI_ControllerTarge…
sihyung-maxim Jul 11, 2023
1504971
feat(Boards): Rename SPI_MasterSlave to SPI_ControllerTarget for MAX7…
sihyung-maxim Jul 11, 2023
af3dd5e
fix(PeriphDrivers): Fix threshold and DMA burst sizes for SPI DMA
sihyung-maxim Jul 12, 2023
d20b076
feat(Examples): Add blocking SPI function without the use of interrupts
sihyung-maxim Jul 12, 2023
10471b1
feat(Examples): Update use of blocking SPI functions without the use …
sihyung-maxim Jul 12, 2023
7f6e02b
Merge branch 'main' into dev/spi_target
sihyung-maxim Jul 12, 2023
ad7f6d7
clang-format bot reformatting.
sihyung-maxim Jul 12, 2023
cd920e4
fix(PeriphDrivers): Add blockng SPI Target transaction function and c…
sihyung-maxim Jul 12, 2023
186a811
Merge branch 'dev/spi_target' of github.com:Analog-Devices-MSDK/msdk …
sihyung-maxim Jul 12, 2023
611f7bb
fix(PeriphDrivers): Update app-level SPI Target functions
sihyung-maxim Jul 12, 2023
ec65ba6
feat(Examples,PeriphDrivers): Add GPIO DS config and SPI Custom targe…
sihyung-maxim Jul 12, 2023
d721f18
feat(PeriphDrivers): Add drive strength configu to GPIO
sihyung-maxim Jul 12, 2023
85534c6
fix(Examples): Rename drive strngth enum in SPI_ControllerTarget for …
sihyung-maxim Jul 12, 2023
5ae5da8
fix(PeriphDrivers): Add drive strength support to GPIO Config
sihyung-maxim Jul 12, 2023
bef5b1b
fix(PeriphDrivers): Add drive strength support to GPIO Config
sihyung-maxim Jul 12, 2023
413604c
clang-format bot reformatting.
sihyung-maxim Jul 12, 2023
61a8236
Merge branch 'main' into dev/spi_target
sihyung-maxim Jul 17, 2023
ab4a435
fix(Examples,PeriphDrivers): Clean up SPI v2 drivers
sihyung-maxim Jul 17, 2023
7f8f6e1
feat(PeriphDrivers): Add GPIO drive strength support
sihyung-maxim Jul 17, 2023
f327e11
fix(Examples): Fix README pinout for SPI_ControllerTarget
sihyung-maxim Jul 17, 2023
cf12707
revert(PeriphDrivers): Revert adding GPIO Drive Strength config option
sihyung-maxim Jul 17, 2023
d0dfa1c
revert(Examples): Remove drive strength option from SPI_ControllerTarget
sihyung-maxim Jul 17, 2023
fbdc6ae
fix(PeriphDrivers): Undo spi.h revert
sihyung-maxim Jul 17, 2023
0c66da0
feat(Examples): Add back SPI_MasterSlave example to showcase SPI v2 u…
sihyung-maxim Jul 17, 2023
7174556
fix(Examples): Revert project.mk for SPI_MasterSlave example
sihyung-maxim Jul 17, 2023
4cc478f
fix(PeriphDrivers): Remove deassert parameter comment in TargetTransa…
sihyung-maxim Jul 18, 2023
515cbba
fix(PeriphDrivers): Add measures, similar to Controller, when Target …
sihyung-maxim Jul 18, 2023
a7fd030
fix(Examples): Revise SPI READMEs for SPI v2
sihyung-maxim Jul 18, 2023
92042ea
fix(Build): Fix "empty.c file not found" Error on Make v3.81 or Older…
Jake-Carter Jul 17, 2023
092c39e
Merge branch 'main' into dev/spi_target
sihyung-maxim Jul 18, 2023
0d6f908
Fix comments in spi_reva2.c
sihyung-maxim Jul 18, 2023
e308fd4
Fix grammar in spi_reva2.c
sihyung-maxim Jul 18, 2023
6696b15
fix(PeriphDrivers): Rename Transaction functions and minimize mainten…
sihyung-maxim Jul 18, 2023
ebad3df
Merge branch 'dev/spi_target' of github.com:Analog-Devices-MSDK/msdk …
sihyung-maxim Jul 18, 2023
dc329b4
Merge branch 'main' into dev/spi_target
sihyung-maxim Jul 19, 2023
3c8ea24
refactor(PeriphDrivers): Reduce maintenance overhead and use request …
sihyung-maxim Jul 24, 2023
22821c2
clang-format bot reformatting.
sihyung-maxim Jul 24, 2023
3e05a1a
fix(PeriphDrivers): Update DMA settings for SPI v2
sihyung-maxim Jul 24, 2023
20c68e0
Merge branch 'dev/spi_target' of github.com:Analog-Devices-MSDK/msdk …
sihyung-maxim Jul 24, 2023
7e12a3d
revert(PeriphDrivers): Revert transaction prototypes in SPI RevA2 level
sihyung-maxim Jul 24, 2023
5b07353
clang-format bot reformatting.
sihyung-maxim Jul 24, 2023
c0428f0
fix(Examples): Add request structs for SPI_ControllerTarget
sihyung-maxim Jul 24, 2023
78d6be4
Merge branch 'dev/spi_target' of github.com:Analog-Devices-MSDK/msdk …
sihyung-maxim Jul 24, 2023
b047952
fix(PeriphDrivers): Update TARGET transaction gards for SPI
sihyung-maxim Jul 24, 2023
2fadc1e
fix(PeriphDrivers): Rename mode to if_mode and reduce code size by re…
sihyung-maxim Jul 25, 2023
ddade94
fix(PeriphDrivers): Remove LED toggle
sihyung-maxim Jul 25, 2023
09dd49d
docs(Examples): Update documentation with SPI v2
sihyung-maxim Jul 26, 2023
174caaf
docs(Documentation): Add another limitation for SPI v2 userguide
sihyung-maxim Jul 26, 2023
753d0e2
docs(Documentation): Fix spacing for SPI v2 userguide
sihyung-maxim Jul 26, 2023
1aaefe5
fix(Examples): Remove extra TFT example for MAX78002
sihyung-maxim Jul 26, 2023
2962935
fix(PeriphDrivers): Update SPI v2 based on code review
sihyung-maxim Aug 14, 2023
7d346f0
fix(PeriphDrivers): Update TS Control functions
sihyung-maxim Aug 15, 2023
d8fc2c3
fix(Examples): Fix build errors
sihyung-maxim Aug 15, 2023
f356733
Merge branch 'main' into dev/spi_target
sihyung-maxim Aug 18, 2023
0cd86ff
fix(Examples): Fix build errors with SPI_ControllerTarget
sihyung-maxim Aug 18, 2023
c9ef355
clang-format bot reformatting.
sihyung-maxim Aug 21, 2023
a6542e2
fix(PeriphDrivers): Fix SetFrameSize conditional for controller vs ta…
sihyung-maxim Aug 21, 2023
7aca43f
Merge branch 'dev/spi_target' of github.com:Analog-Devices-MSDK/msdk …
sihyung-maxim Aug 21, 2023
f6a5570
clang-format bot reformatting.
sihyung-maxim Aug 21, 2023
59e8979
fix(Examples,PeriphDrivers): Fix NEWHAVEN touchscreen and default bui…
sihyung-maxim Aug 22, 2023
91e7a8b
fix(PeriphDrivers): Fix possible hardfaults
sihyung-maxim Aug 22, 2023
31067bc
fix(PeriphDrivers): Update documentation and fix any final bugs
sihyung-maxim Aug 22, 2023
6eb05ca
clang-format bot reformatting.
sihyung-maxim Aug 22, 2023
ee6b678
fix(Examples): Build updated SPI examples with SPI v2
sihyung-maxim Aug 22, 2023
657b094
fix(Examples): Remove build warnings
sihyung-maxim Aug 22, 2023
976fd14
fix(Examples): Remove build warnings
sihyung-maxim Aug 22, 2023
0bcf33b
fix(Examples): Update READMEs
sihyung-maxim Aug 22, 2023
d3e9bd8
clang-format bot reformatting.
sihyung-maxim Aug 22, 2023
e22fccb
Fix ai87 SPI implicit function declaration
Jake-Carter Sep 6, 2023
4144659
Rename build variable to MXC_SPI_VERSION, simplify variable, update d…
Jake-Carter Sep 6, 2023
0d9b979
Fix multi-line comment warning
Jake-Carter Sep 6, 2023
c5bcc6e
Fix VS Code auto-detection of SPI V1/V2 on build
Jake-Carter Sep 6, 2023
028d26e
Fix clang-format multi-line error
Jake-Carter Sep 6, 2023
08bb7f1
clang-format
Jake-Carter Sep 6, 2023
401cd60
UG formatting fixes
Jake-Carter Sep 6, 2023
6b7b322
Fix race condition on ctrl0 read-modify-writes in spi_reva1
Jake-Carter Sep 21, 2023
f1eaee4
Merge branch 'main' into dev/spi_target
sihyung-maxim Sep 25, 2023
19fa1d0
fix(Examples,PeriphDrivers): Handle change request and fix SPI DMA
sihyung-maxim Sep 25, 2023
a45fad9
fix(Examples): Fix example name and SPI README
sihyung-maxim Sep 25, 2023
351afaa
fix(Boards): Handle other GPIO instance interrupts (weak) for push bu…
sihyung-maxim Sep 25, 2023
44ff266
Revert "fix(Boards): Handle other GPIO instance interrupts (weak) for…
sihyung-maxim Sep 25, 2023
b78e95f
fix(Examples,PeriphDrivers): Simplify TS Control Scheme setup
sihyung-maxim Sep 25, 2023
4df0a3f
fix(Examples): Reset example to default
sihyung-maxim Sep 25, 2023
81b9198
Merge branch 'main' into dev/spi_target
sihyung-maxim Sep 26, 2023
0cbd4bb
fix(PeriphDrivers): Fix build errors between SPI v1 and v2
sihyung-maxim Sep 26, 2023
e069cc9
fix(Examples): SPI_MasterSlave working
sihyung-maxim Sep 26, 2023
337407a
clang-format bot reformatting.
sihyung-maxim Sep 26, 2023
292db97
fix(Examples,PeriphDrivers): Revert requet struct names and remove SW…
sihyung-maxim Sep 28, 2023
787a80f
fix(Documentation): Reflect SPI v2 changes to user guide
sihyung-maxim Sep 28, 2023
fa5f9cf
clang-format bot reformatting.
sihyung-maxim Sep 28, 2023
396dce9
fix(Examples,PeriphDrivers): Revert active_pol changes
sihyung-maxim Sep 28, 2023
b1744ed
clang-format bot reformatting.
sihyung-maxim Sep 28, 2023
cf4fdd0
fix(Examples): Update documentation in SPI examples
sihyung-maxim Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 6 additions & 80 deletions Examples/MAX78002/SPI/README.md
Original file line number Diff line number Diff line change
@@ -1,106 +1,32 @@
## Description

This example configures the SPI to send data between the MISO (P0.22) and
MOSI (P0.21) pins. Connect these two pins together.
This example configures the SPI to send data between the MISO (P0.22) and MOSI (P0.21) pins. Connect these two pins together.

Multiple word sizes (2 through 16 bits) are demonstrated.

By default, the example performs blocking SPI transactions. To switch to non-blocking (asynchronous) transactions, reset the BLOCKING macro to 0 and set the NON_BLOCKING macro to 1. To use DMA transactions, set the DMA macro to 1 instead.
By default, the example performs blocking SPI transactions. To switch to non-blocking (asynchronous) transactions, reset the `CONTROLLER_SYNC` macro to 0 and set the `CONTROLLER_ASYNC` macro to 1. To use DMA transactions, set the `CONTROLLER_DMA` macro to 1 instead.

This example also demonstrates the feature to use custom Target Selects that the SPI v2 Driver will automatically assert/deassert during transactions. Set the CUSTOM_TARGET macro to 1 to use the custom target. To use the default TS pins, set the CUSTOM_TARGET macro to 0 instead.
This example uses the Hardware Target Select control scheme (application does not assert the TS pins).

## Software

This example uses the SPI v2 Library. To use the previous SPI library, set `MXC_SPI_BUILD_LEGACY=1` in the Project's project.mk file.

### Porting Guide

The SPI v2 Library is backwards compatible with the previous SPI API - meaning the previously existing function prototypes have not changed. There are functional differences with SPI DMA interrupt handling that must be updated when porting a project from using the previous SPI API to SPI v2.

#### SPI v2 API Differences

##### SPI Init Function

The `MXC_SPI_Init(...)` function is still supported with SPI v2, but there is some added overhead due to the limited settings that this function can set.

Use the `MXC_SPI_Init_v2(...)` function for 1) to decrease overhead and 2) to give the caller more control in the SPI setup.

**`mxc_spi_init_t init` Fields**
- `mxc_spi_regs_t *spi` //<== SPI Instance
- `mxc_gpio_cfg_t *spi_pins` //<== (Optional) Caller supplied SPI pins.
- `mxc_spi_type_t type` //<== Controller (L. Master) or Target (L. Slave) Modes
- `uint32_t freq` //<== SPI Frequency
- `mxc_spi_clkmode_t clk_mode` //<== Clock Mode (CPOL:CPHA)
- `mxc_spi_interface_t mode` //<== Select Interface (Standard 4-wire, 3-wire, dual, quad)
- `mxc_spi_tscontrol_t ts_control` //<== HW Auto, SW Driver, or SW Application Target Control
- `mxc_spi_target_t target` //<== Target settings (custom TS pins, init mask, active polarity)
- `mxc_gpio_vssel_t vssel` //<== Select Pin Voltage Level (VDDIO/VDDIOH)
- `bool use_dma` //<== TRUE/FALSE DMA setting
- `mxc_dma_regs_t *dma` //<== DMA Instance
- `mxc_spi_callback_t callback` //<== Set Callback function for end of transaction
- `void* callback_data` //<== Data to pass through callback function

##### SPI DMA Interrupt Handling

```c
void DMA_TX_IRQHandler(void)
{
MXC_SPI_DMA_TX_Handler(SPI);
}

void DMA_RX_IRQHandler(void)
{
MXC_SPI_DMA_RX_Handler(SPI);
}

```
The previous SPI API uses the MXC_DMA_Handler, but SPI v2 supplies its own Handler processing functions to call for TX and RX DMA.

##### SPI DMA Setup
```c
...
TX_DMA_CH = MXC_SPI_DMA_GetTXChannel(SPI);
RX_DMA_CH = MXC_SPI_DMA_GetRXChannel(SPI);

NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(TX_DMA_CH));
NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(RX_DMA_CH));

MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(TX_DMA_CH), DMA_TX_IRQHandler);
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(RX_DMA_CH), DMA_RX_IRQHandler);

MXC_SPI_MasterTransactionDMA(&req);
...
```
Following the DMA channel interrupt changes from the previous section, it is recommended to set up a generic name DMA TX/RX vector because the the TX and RX DMA channels won't always acquire DMA_CH0 and DMA_CH1, respectively.

##### Blocking SPI Transaction (MXC_SPI_MasterTransaction(...))
```c
void SPI_IRQHandler(void)
{
MXC_SPI_Handler(SPI); // Or MXC_SPI_AsyncHandler(SPI); Same function, different names.
}

...
NVIC_EnableIRQ(SPI);
MXC_SPI_MasterTransaction(&req);
...
```
The blocking SPI transaction function is now interrupt driven for SPI v2 - meaning the SPI instances' IRQ must be enabled and the MXC_SPI_Handler(...) or MXC_SPI_AsyncHandler(...) function must be called in the interrupt routine.
This project uses the SPI v2 library. More information on the SPI v2 library can be found in the **[MSDK User Guide Developer Notes](https://analog-devices-msdk.github.io/msdk/USERGUIDE/#spi-v2-library)**.

### Project Usage

Universal instructions on building, flashing, and debugging this project can be found in the **[MSDK User Guide](https://analog-devices-msdk.github.io/msdk/USERGUIDE/)**.

### Project-Specific Build Notes

(None - this project builds as a standard example)
Set `MXC_SPI_VERSION=v2` to build the SPI v2 libraries.

## Required Connections

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a note to connect the pins together in this section while you're at it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pin connections are in the "Required Connections" section of the README.

- Connect a USB cable between the PC and the CN2 (USB/PWR) connector.
- Connect the 5V power cable at (5V IN).
- Close jumper (RX - P0.0) and (TX - P0.1) at Headers JP23 (UART 0 EN).
- Open an terminal application on the PC and connect to the EV kit's console UART at 115200, 8-N-1.
- Connect P0.21 (MOSI) to P0.22 (MISO).

## Expected Output

Expand Down
140 changes: 65 additions & 75 deletions Examples/MAX78002/SPI/main.c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the main.c Doxygen tag needs an update for this file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file could use a comment explaining the following code:

uint8_t bits = MXC_SPI_GetFrameSize(SPI);

for (j = 0; j < DATA_LEN; j++) {
    if (bits <= 8) {
        if (j < (DATA_LEN / 2)) {
            temp = VALUE >> (16 - bits);
            temp = (temp << 8) | temp;
            temp &= DATA_VALUE;
            tx_data[j] = temp;
        } else if (j == (DATA_LEN / 2) && DATA_LEN % 2 == 1) {
            temp = VALUE >> (16 - bits);
            temp &= DATA_VALUE;
            tx_data[j] = temp;
        } else {
            tx_data[j] = 0x0000;
        }
    } else {
        temp = VALUE >> (16 - bits);
        temp &= DATA_VALUE;
        tx_data[j] = temp;
    }
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/**
* @file main.c
* @brief SPI Controller Demo
* @details Shows Controller loopback demo for QSPI0
* Read the printf() for instructions
*/

/******************************************************************************
* Copyright (C) 2023 Maxim Integrated Products, Inc., All Rights Reserved.
*
Expand Down Expand Up @@ -38,6 +31,12 @@
*
******************************************************************************/

/**
* @file main.c
* @brief SPI Controller Demo
* @details This example demonstrates the SPI controller loopback transactions.
*/

/***** Includes *****/
#include <stdio.h>
#include <stdbool.h>
Expand All @@ -50,24 +49,25 @@
#include "uart.h"
#include "spi.h"
#include "dma.h"
#include "led.h"

/***** Preprocessors *****/
#define BLOCKING 1
#define NON_BLOCKING 0
#define DMA 0
#define CONTROLLER_SYNC 0
#define CONTROLLER_ASYNC 0
#define CONTROLLER_DMA 1

#define CUSTOM_TARGET 0

#if (!(BLOCKING || NON_BLOCKING || DMA))
#error "You must set either BLOCKING or NON_BLOCKING or DMA to 1."
// Preprocessor Error Checking
#if (!(CONTROLLER_SYNC || CONTROLLER_ASYNC || CONTROLLER_DMA))
#error "You must set either CONTROLLER_SYNC or CONTROLLER_ASYNC or CONTROLLER_DMA to 1."
#endif
#if ((BLOCKING && NON_BLOCKING) || (NON_BLOCKING && DMA) || (DMA && BLOCKING))
#error "You must select either BLOCKING or NON_BLOCKING or DMA, not all 3."
#if ((CONTROLLER_SYNC && CONTROLLER_ASYNC) || (CONTROLLER_ASYNC && CONTROLLER_DMA) || \
(CONTROLLER_DMA && CONTROLLER_SYNC))
#error "You must select either CONTROLLER_SYNC or CONTROLLER_ASYNC or CONTROLLER_DMA, not all 3."
#endif

/***** Definitions *****/
#define DATA_LEN 100 // Words
#define DATA_VALUE 0xA5B7 // This is for master mode only...
#define DATA_VALUE 0xA5B7 // This is for Controller mode only...
#define VALUE 0xFFFF
#define SPI_SPEED 100000 // Bit Rate

Expand Down Expand Up @@ -115,21 +115,20 @@ int main(void)
{
int i, j, retVal;
uint16_t temp;
mxc_spi_init_t init;
mxc_spi_target_t target;
mxc_spi_cfg_t cfg;

printf("\n**************************** SPI CONTROLLER TEST *************************\n");
printf("This example configures the SPI to send data between the MISO (P0.22) and\n");
printf("MOSI (P0.21) pins. Connect these two pins together.\n\n");
printf("Multiple word sizes (2 through 16 bits) are demonstrated.\n\n");

#if BLOCKING
#if CONTROLLER_SYNC
printf("Performing blocking (synchronous) transactions...\n");
#endif
#if NON_BLOCKING
#if CONTROLLER_ASYNC
printf("Performing non-blocking (asynchronous) transactions...\n");
#endif
#if DMA
#if CONTROLLER_DMA
printf("Performing transactions with DMA...\n");
#endif

Expand All @@ -139,79 +138,68 @@ int main(void)
tx_data[j] = DATA_VALUE;
}

// Initialization Settings.
init.spi = SPI;
init.freq = SPI_SPEED;
init.spi_pins = NULL; // Use default, predefined pins
init.mode = MXC_SPI_INTERFACE_STANDARD; // 4-wire
init.type = MXC_SPI_TYPE_CONTROLLER;
init.clk_mode = MXC_SPI_CLKMODE_0; // CPOL: 0, CPHA: 0
init.frame_size = i;
init.callback = SPI_Callback;

// Target Select Settings
#if CUSTOM_TARGET
// Example to select a custom target.
mxc_gpio_cfg_t target_pins;
target_pins.port = MXC_GPIO0;
target_pins.mask = MXC_GPIO_PIN_9;
target_pins.func = MXC_GPIO_FUNC_OUT;
target_pins.pad = MXC_GPIO_PAD_PULL_UP;
target_pins.vssel = MXC_GPIO_VSSEL_VDDIOH; // Set custom target pin to VDDIOH (3.3V).

init.ts_control =
MXC_SPI_TSCONTROL_SW_DRV; // SPI Driver will handle deassertion for TS pins.
init.target.pins = target_pins;
init.target.active_polarity = 0;
init.vssel = MXC_GPIO_VSSEL_VDDIOH; // Set SPI pins to VDDIOH (3.3V).

// Select target for transaction.
target.pins = target_pins; // Custom pins
#else
init.ts_control = MXC_SPI_TSCONTROL_HW_AUTO; // HW will deassert/assert TS pins.
init.target.active_polarity = 0;
init.target.init_mask = 0x01; // Initialize Target Select 0 pin.
init.vssel = MXC_GPIO_VSSEL_VDDIO;
mxc_spi_pins_t spi_pins;
// This example enables the TS0 HW pin.
spi_pins.ss0 = true; // TS0
spi_pins.ss1 = false; // TS1
spi_pins.ss2 = false; // TS2
spi_pins.vddioh = true;
spi_pins.drvstr = MXC_GPIO_DRVSTR_0;

// Select target for transaction.
target.index = 0; // TS0
#endif
retVal = MXC_SPI_Init(SPI, MXC_SPI_TYPE_CONTROLLER, MXC_SPI_INTERFACE_STANDARD, 0, 0b000,
SPI_SPEED, spi_pins);
if (retVal != E_NO_ERROR) {
printf("\nSPI INITIALIZATION ERROR\n");
return retVal;
}

// SPI Settings.
cfg.spi = SPI;
cfg.clk_mode = MXC_SPI_CLKMODE_0; // CPOL: 0, CPHA: 0
cfg.frame_size = i;

// DMA Settings.
#if DMA
init.use_dma = true;
init.dma = MXC_DMA;
#if CONTROLLER_DMA
cfg.use_dma_tx = true;
cfg.use_dma_rx = true;
cfg.dma = MXC_DMA;
#else
init.use_dma = false;
cfg.use_dma_rx = false;
cfg.use_dma_tx = false;
cfg.dma = MXC_DMA;
#endif

retVal = MXC_SPI_Init_v2(&init);
retVal = MXC_SPI_Config(&cfg);
if (retVal != E_NO_ERROR) {
printf("\nSPI INITIALIZATION ERROR\n");
printf("\nSPI CONFIGURATION ERROR\n");
return retVal;
}

memset(rx_data, 0x0, DATA_LEN * sizeof(uint16_t));

// SPI Request (Callback)
mxc_spi_req_t req;
req.spi = SPI;
req.txData = (uint8_t *)tx_data;
req.txLen = DATA_LEN;
req.rxData = (uint8_t *)rx_data;
req.rxLen = DATA_LEN;
req.ssDeassert = 1;
req.completeCB = SPI_Callback;
SPI_FLAG = 1;

#if BLOCKING
// Blocking SPI v2 Implementation is Interrupt driven.
NVIC_EnableIRQ(SPI_IRQ);
MXC_SPI_ControllerTransactionB(SPI, (uint8_t *)tx_data, DATA_LEN, (uint8_t *)rx_data,
DATA_LEN, 1, &target);
#if CONTROLLER_SYNC
MXC_SPI_ControllerTransaction(&req);
#endif

#if NON_BLOCKING
#if CONTROLLER_ASYNC
NVIC_EnableIRQ(SPI_IRQ);
MXC_SPI_ControllerTransaction(SPI, (uint8_t *)tx_data, DATA_LEN, (uint8_t *)rx_data,
DATA_LEN, 1, &target);
MXC_SPI_ControllerTransactionAsync(&req);

while (SPI_FLAG == 1) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments explaining how/why we wait on the flag here. This has caused some confusion in the past.

#endif

#if DMA
#if CONTROLLER_DMA
TX_DMA_CH = MXC_SPI_DMA_GetTXChannel(SPI);
RX_DMA_CH = MXC_SPI_DMA_GetRXChannel(SPI);

Expand All @@ -221,14 +209,14 @@ int main(void)
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(TX_DMA_CH), DMA_TX_IRQHandler);
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(RX_DMA_CH), DMA_RX_IRQHandler);

MXC_SPI_ControllerTransactionDMA(SPI, (uint8_t *)tx_data, DATA_LEN, (uint8_t *)rx_data,
DATA_LEN, 1, &target);
MXC_SPI_ControllerTransactionDMA(&req);

while (SPI_FLAG == 1) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, add comment explaining flag wait

#endif

uint8_t bits = MXC_SPI_GetFrameSize(SPI);

// Reformat tx_data to match rx_data frame size.
for (j = 0; j < DATA_LEN; j++) {
if (bits <= 8) {
if (j < (DATA_LEN / 2)) {
Expand All @@ -254,6 +242,7 @@ int main(void)
// Printf needs the Uart turned on since they share the same pins
if (memcmp(rx_data, tx_data, sizeof(tx_data)) != 0) {
printf("\n-->%2d Bits Transaction Failed\n", i);
LED_On(1);
return E_COMM_ERR;
} else {
printf("-->%2d Bits Transaction Successful\n", i);
Expand All @@ -268,5 +257,6 @@ int main(void)
}

printf("\nExample Complete.\n");
LED_On(0);
return E_NO_ERROR;
}
2 changes: 2 additions & 0 deletions Examples/MAX78002/SPI/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Add your config here!

# Build SPI v2 library for example.
MXC_SPI_VERSION = v2
Loading