-
Notifications
You must be signed in to change notification settings - Fork 89
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
Changes from all commits
a6e6cde
7c4f0d9
3bf0491
c499001
4a952f3
1504971
af3dd5e
d20b076
10471b1
7f6e02b
ad7f6d7
cd920e4
186a811
611f7bb
ec65ba6
d721f18
85534c6
5ae5da8
bef5b1b
413604c
61a8236
ab4a435
7f8f6e1
f327e11
cf12707
d0dfa1c
fbdc6ae
0c66da0
7174556
4cc478f
515cbba
a7fd030
92042ea
092c39e
0d6f908
e308fd4
6696b15
ebad3df
dc329b4
3c8ea24
22821c2
3e05a1a
20c68e0
7e12a3d
5b07353
c0428f0
78d6be4
b047952
2fadc1e
ddade94
09dd49d
174caaf
753d0e2
1aaefe5
2962935
7d346f0
d8fc2c3
f356733
0cd86ff
c9ef355
a6542e2
7aca43f
f6a5570
59e8979
91e7a8b
31067bc
6eb05ca
ee6b678
657b094
976fd14
0bcf33b
d3e9bd8
e22fccb
4144659
0d9b979
c5bcc6e
028d26e
08bb7f1
401cd60
6b7b322
f1eaee4
19fa1d0
a45fad9
351afaa
44ff266
b78e95f
4df0a3f
81b9198
0cbd4bb
e069cc9
337407a
292db97
787a80f
fa5f9cf
396dce9
b1744ed
cf4fdd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
@@ -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) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
@@ -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); | ||
|
@@ -268,5 +257,6 @@ int main(void) | |
} | ||
|
||
printf("\nExample Complete.\n"); | ||
LED_On(0); | ||
return E_NO_ERROR; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Add your config here! | ||
|
||
# Build SPI v2 library for example. | ||
MXC_SPI_VERSION = v2 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.