Roy
2023-05-18 14:34:49 +08:00
parent d096eabf47
commit af03bc03e2
5 changed files with 204 additions and 30 deletions
@@ -0,0 +1,26 @@
#ifndef SPI_CTRL_H
#define SPI_CTRL_H
#ifdef __cplusplus
extern "C" {
#endif
#define SPI0 0
#define SPI1 1
#define SPI_POL0 0
#define SPI_POL1 1
#define SPI_PHA0 0
#define SPI_PHA1 1
#define SPI_RATE_1M 1000000
#define SPI_RATE_4M 4000000
#define SPI_RATE_6M 6000000
uint8_t spi_open(uint8_t spi_n, uint32_t b_rate, uint8_t pol, uint8_t pha);
uint8_t spi_close(uint8_t spi_n);
uint8_t spi_write(uint8_t spi_n, uint8_t *rxBuf, uint8_t *txBuf, uint8_t len);
#ifdef __cplusplus
}
#endif
#endif // SPI_CTRL_H
@@ -0,0 +1,166 @@
#include "driver/spi_ctrl.h"
#include <Board.h>
#include <ti/drivers/SPI.h>
#define CC2650_SPI_BITRATE_MAX 6e6 // Full-duplex maximum speed = 6M
static SPI_Handle handle0 = NULL;
static SPI_Handle handle1 = NULL;
/**
* _get_spi_mode - transfer both polarity and phase to pol_pha_combine
* @pol: polarity
* @pha: phase
* Returns: spi mode
*/
static SPI_FrameFormat _get_spi_mode(uint8_t pol, uint8_t pha)
{
SPI_FrameFormat spi_mode;
if (pol == 0 && pha == 0)
spi_mode = SPI_POL0_PHA0;
else if (pol == 0 && pha == 1)
spi_mode = SPI_POL0_PHA1;
else if (pol == 1 && pha == 0)
spi_mode = SPI_POL1_PHA0;
else if (pol == 1 && pha == 1)
spi_mode = SPI_POL1_PHA1;
return spi_mode;
}
/**
* spi_open -
* @spi_n: which SPI
* @b_rate: bit rate of SPI
* @pol: polarity
* @pha: phase
* Returns: 0 on success, 1 on no this spi module, 2 on spi already open,
* 3 on unsupported bit rate, 4 on unsupported polarity and phase,
* 5 on failure
* note: Before using PIN_open() and SPI_open(), make sure that the pins are
* not already registered, otherwise it will crash.
*/
uint8_t spi_open(uint8_t spi_n, uint32_t b_rate, uint8_t pol, uint8_t pha)
{
SPI_Params para;
if (spi_n >= 2)
return 1;
if ((spi_n == SPI0 && handle0) || (spi_n == SPI1 && handle1))
return 2;
if (b_rate > CC2650_SPI_BITRATE_MAX)
return 3;
if (pol > 1 || pha > 1)
return 4;
SPI_Params_init(&para);
para.bitRate = b_rate;
para.mode = SPI_MASTER;
para.dataSize = 8;
para.frameFormat = _get_spi_mode(pol, pha);
if (spi_n == SPI0) {
handle0 = SPI_open(Board_SPI0, &para);
if (handle0 == NULL) {
Elite_led_color(COLOR_RED);
while (1);
}
} else {
handle1 = SPI_open(Board_SPI1, &para);
if (handle1 == NULL) {
Elite_led_color(COLOR_RED);
while (1);
}
}
return 0;
}
/**
* spi_close -
* @spi_n: which SPI
* Returns: 0 on success, 1 on no this spi module, 2 on no instance
* note: Before using PIN_close() and SPI_close(), make sure that there is \
* an instance available, otherwise it will crash.
*/
uint8_t spi_close(uint8_t spi_n)
{
if (spi_n >= 2)
return 1;
if ((spi_n == SPI0 && !handle0) || (spi_n == SPI1 && !handle1))
return 2;
if (spi_n == SPI0) {
SPI_close(handle0);
handle0 = NULL;
} else {
SPI_close(handle1);
handle1 = NULL;
}
return 0;
}
/**
* spi_close -
* @spi_n: which SPI
* @*rxBuf: rxbuf
* @*txBuf: txbuf
* @len: what is the required length
* Returns: 0 on success, 1 on no this spi module, 2 on no instance,
* 3 on write failure
*/
uint8_t spi_write(uint8_t spi_n, uint8_t *rxBuf, uint8_t *txBuf, uint8_t len)
{
bool transferOK;
SPI_Transaction spi_tran;
if (spi_n >= 2)
return 1;
if ((spi_n == SPI0 && !handle0) || (spi_n == SPI1 && !handle1))
return 2;
spi_tran.count = len;
spi_tran.txBuf = txBuf;
spi_tran.rxBuf = rxBuf;
if (spi_n == SPI0) {
transferOK = SPI_transfer(handle0, &spi_tran);
} else {
transferOK = SPI_transfer(handle1, &spi_tran);
}
if (!transferOK) {
// Error in SPI or transfer already in progress.
Elite_led_color(COLOR_RED);
while (1);
}
return 0;
}
/* utils.c.h */
/*
#include <stdint.h>
#include <stdio.h>
static void ___print_hex(uint8_t* p, int len)
{
// ___print_hex((uint8_t *)p, sizeof(struct led_series_data_t));
int i;
for (i = 0; i < len; i++) {
printf("0x%x, ", *p++);
}
printf("\n\n");
return;
}
*/
@@ -14,6 +14,8 @@
#include <ti/drivers/dma/UDMACC26XX.h>
#include <ti/drivers/spi/SPICC26XXDMA.h>
#include "driver/spi_ctrl.h"
/* application use SPI parameters and buffers */
#define SPI_LED_SIZE LED_BUFF_SIZE
#define SPI_DAC_SIZE 3
@@ -40,33 +42,12 @@ static SPI_Params spiParams1;
static SPI_Transaction LED_transaction;
static SPI_Transaction ADC_DAC_transaction;
static void Elite_SPI_init()
{
SPI_init();
SPI_Params_init(&spiParams0);
spiParams0.bitRate = 2000; // 12k
spiParams0.mode = SPI_MASTER;
spiParams0.dataSize = 8;
spiParams0.frameFormat = SPI_POL0_PHA1;
spiHandle0 = SPI_open(Board_SPI0, &spiParams0); // LED SPI
SPI_Params_init(&spiParams1);
spiParams1.bitRate = 1000000; // 1M
spiParams1.mode = SPI_MASTER;
spiParams1.dataSize = 8;
spiParams1.frameFormat = SPI_POL0_PHA1;
spiHandle1 = SPI_open(Board_SPI1, &spiParams1); // ADC DAC SPI
}
static void LED_SPI(uint8_t length, uint8_t *spi_txbuf, uint8_t *spi_rxbuf)
{
PIN_setOutputValue(pin_handle, CC2650_LOAD0, 1);
LED_transaction.count = length;
LED_transaction.txBuf = spi_txbuf;
LED_transaction.rxBuf = spi_rxbuf;
spi_write(SPI0, spi_rxbuf, spi_txbuf, length);
SPI_transfer(spiHandle0, &LED_transaction);
PIN_setOutputValue(pin_handle, CC2650_LOAD0, 0);
}
@@ -74,22 +55,21 @@ static void ADC_SPI(uint8_t length, uint8_t *spi_txbuf, uint8_t *spi_rxbuf)
{
PIN_setOutputValue(pin_handle, CC2650_LOAD0, 1);
ADC_DAC_transaction.count = length;
ADC_DAC_transaction.txBuf = spi_txbuf;
ADC_DAC_transaction.rxBuf = spi_rxbuf;
spi_write(SPI1, spi_rxbuf, spi_txbuf, length);
SPI_transfer(spiHandle1, &ADC_DAC_transaction);
PIN_setOutputValue(pin_handle, CC2650_LOAD0, 0);
}
static void ELITE15_SPI_HOLD()
{
Elite_SPI_init();
Board_initSPI();
spi_open(SPI0, SPI_RATE_1M, SPI_POL0, SPI_PHA1); //SPI 1M: LED
spi_open(SPI1, SPI_RATE_4M, SPI_POL0, SPI_PHA1); //SPI 4M: ADC、DAC
}
static void ELITE15_SPI_CLOSE()
{
SPI_close(spiHandle0);
SPI_close(spiHandle1);
spi_close(SPI0);
spi_close(SPI1);
}
static void GPIO_SPI_transfer(uint32_t *GPIO_CLK_CH, uint16_t spi_GPIO_txbuf)
@@ -5,5 +5,5 @@
#define VERSION_DATE_MONTH 5
#define VERSION_DATE_DAY 18
#define VERSION_DATE_HOUR 14
#define VERSION_DATE_MINUTE 7
#define VERSION_DATE_MINUTE 34
#endif
@@ -527,6 +527,7 @@ static uint16_t events;
#include "driver/timers.h"
#include "service/elite_GPtimer.h"
#include "driver/spi_ctrl.h"
struct gptimer0_t GPT;
@@ -1239,3 +1240,4 @@ static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state)
#include "driver/timers_c.h"
#include "service/elite_GPtimer_c.h"
#include "driver/spi_ctrl_c.h"