From 032efebe2a201352aed646e65165907e0a3ddc59 Mon Sep 17 00:00:00 2001 From: ROY Date: Wed, 3 Aug 2022 11:10:29 +0800 Subject: [PATCH] [update] update spi code --- .../cc26xx/app/driver/spi_ctrl.h | 8 +- .../cc26xx/app/driver/spi_ctrl_c.h | 174 ++++++++++++------ .../cc26xx/app/simple_peripheral.c | 9 +- 3 files changed, 123 insertions(+), 68 deletions(-) diff --git a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl.h b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl.h index 141846d..279d965 100644 --- a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl.h +++ b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl.h @@ -10,15 +10,15 @@ extern "C" { #define PHA0 0 #define PHA1 1 -#define SPI_CLK_10M 10000000 - +#define SPI_CLK_1M 1000000 +#define SPI_CLK_4M 4000000 uint8_t spi0_open(uint32_t bitRate, uint8_t polarity, uint8_t phase); -void spi0_close(void); +uint8_t spi0_close(void); uint8_t spi0_write(uint8_t *rxBuf, uint8_t *txBuf, uint8_t len); uint8_t spi1_open(uint32_t bitRate, uint8_t polarity, uint8_t phase); -void spi1_close(void); +uint8_t spi1_close(void); uint8_t spi1_write(uint8_t *rxBuf, uint8_t *txBuf, uint8_t len); #ifdef __cplusplus diff --git a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl_c.h b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl_c.h index 2765217..52db187 100644 --- a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl_c.h +++ b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/driver/spi_ctrl_c.h @@ -2,13 +2,43 @@ #include #include "driver/spi_ctrl.h" -static SPI_Handle spiHandle0 = NULL; -static SPI_Params spiParams0; +#define CC2650_SPI_BITRATE_MAX 4000000 //4M -static SPI_Handle spiHandle1 = NULL; -static SPI_Params spiParams1; +static SPI_Handle SpiHandle0 = NULL; +static SPI_Params SpiParams0; -static SPI_FrameFormat get_spi_mode(uint8_t polarity, uint8_t phase) +static SPI_Handle SpiHandle1 = NULL; +static SPI_Params SpiParams1; + +static SPI_Handle __get_spi_handle(uint8_t spi_channel) +{ + uint8_t c = spi_channel; + + if (c >= BOOSTXL_CC2650MA_SPICOUNT) + return NULL; + + if (c == Board_SPI0) + return SpiHandle0; + + if (c == Board_SPI1) + return SpiHandle1; + + return 0; +} + +static void __set_spi_handle(uint8_t spi_channel, SPI_Handle handle) +{ + uint8_t c = spi_channel; + + if (c == Board_SPI0) + SpiHandle0 = handle; + else if (c == Board_SPI1) + SpiHandle1 = handle; + + return; +} + +static SPI_FrameFormat __get_spi_mode(uint8_t polarity, uint8_t phase) { uint8_t pol = polarity; uint8_t pha = phase; @@ -31,47 +61,63 @@ uint8_t spi0_open(uint32_t bitRate, uint8_t polarity, uint8_t phase) uint32_t rate = bitRate; uint8_t pol = polarity; uint8_t pha = phase; - SPI_FrameFormat frameFormat; + SPI_Handle h = __get_spi_handle(Board_SPI0); + SPI_Params *para = &SpiParams0; - frameFormat = get_spi_mode(pol, pha); - - SPI_Params_init(&spiParams0); - spiParams0.bitRate = rate; - spiParams0.mode = SPI_MASTER; - spiParams0.dataSize = 8; - spiParams0.frameFormat = frameFormat; - - spiHandle0 = SPI_open(Board_SPI0, &spiParams0); - - if (spiHandle0 == NULL) + if (rate > CC2650_SPI_BITRATE_MAX) return 1; + if (pol > 1 || pha > 1) + return 2; + + if (h != NULL) + return 3; + + SPI_Params_init(para); + para->bitRate = rate; + para->mode = SPI_MASTER; + para->dataSize = 8; + para->frameFormat = __get_spi_mode(pol, pha); + + h = SPI_open(Board_SPI0, para); + __set_spi_handle(Board_SPI0, h); + + if (h == NULL) + return 4; + return 0; } -void spi0_close(void) +uint8_t spi0_close(void) { - if (spiHandle0 != NULL) { - SPI_close(spiHandle0); - spiHandle0 = NULL; - } + SPI_Handle h = __get_spi_handle(Board_SPI0); - return; + if (h == NULL) + return 1; + + SPI_close(h); + __set_spi_handle(Board_SPI0, NULL); + + return 0; } uint8_t spi0_write(uint8_t *rxBuf, uint8_t *txBuf, uint8_t len) { - SPI_Transaction spi0Transaction; + SPI_Handle h = __get_spi_handle(Board_SPI0); + SPI_Transaction spi0_tran; uint8_t ret; - spi0Transaction.count = len; - spi0Transaction.txBuf = txBuf; - spi0Transaction.arg = NULL; - spi0Transaction.rxBuf = NULL; - ret = SPI_transfer(spiHandle0, &spi0Transaction); + if (h == NULL) + return 1; + + spi0_tran.count = len; + spi0_tran.txBuf = txBuf; + spi0_tran.arg = NULL; + spi0_tran.rxBuf = NULL; + ret = SPI_transfer(h, &spi0_tran); if (ret == false) - return 1; + return 2; return 0; } @@ -81,47 +127,62 @@ uint8_t spi1_open(uint32_t bitRate, uint8_t polarity, uint8_t phase) uint32_t rate = bitRate; uint8_t pol = polarity; uint8_t pha = phase; - SPI_FrameFormat frameFormat; + SPI_Handle h = __get_spi_handle(Board_SPI1); + SPI_Params *para = &SpiParams1; - frameFormat = get_spi_mode(pol, pha); - - SPI_Params_init(&spiParams1); - spiParams1.bitRate = rate; - spiParams1.mode = SPI_MASTER; - spiParams1.dataSize = 8; - spiParams1.frameFormat = frameFormat; - - spiHandle1 = SPI_open(Board_SPI1, &spiParams1); - - if (spiHandle1 == NULL) + if (rate > CC2650_SPI_BITRATE_MAX) return 1; + if (pol > 1 || pha > 1) + return 2; + + if (h != NULL) + return 3; + + SPI_Params_init(para); + para->bitRate = rate; + para->mode = SPI_MASTER; + para->dataSize = 8; + para->frameFormat = __get_spi_mode(pol, pha); + + h = SPI_open(Board_SPI1, para); + __set_spi_handle(Board_SPI1, h); + + if (h == NULL) + return 4; + return 0; } -void spi1_close(void) +uint8_t spi1_close(void) { - if (spiHandle1 != NULL) { - SPI_close(spiHandle1); - spiHandle1 = NULL; - } + SPI_Handle h = __get_spi_handle(Board_SPI1); + if (h == NULL) + return 1; - return; + SPI_close(h); + __set_spi_handle(Board_SPI1, NULL); + + return 0; } uint8_t spi1_write(uint8_t *rxBuf, uint8_t *txBuf, uint8_t len) { - SPI_Transaction spi1Transaction; + SPI_Handle h = __get_spi_handle(Board_SPI1); + SPI_Transaction spi1_tran; uint8_t ret; - spi1Transaction.count = len; - spi1Transaction.txBuf = txBuf; - spi1Transaction.arg = NULL; - spi1Transaction.rxBuf = rxBuf; - ret = SPI_transfer(spiHandle1, &spi1Transaction); + if (h == NULL) + return 1; + + spi1_tran.count = len; + spi1_tran.txBuf = txBuf; + spi1_tran.arg = NULL; + spi1_tran.rxBuf = rxBuf; + ret = SPI_transfer(h, &spi1_tran); if (ret == false) - return 1; + return 2; return 0; } @@ -145,6 +206,3 @@ static void ___print_hex(uint8_t* p, int len) return; } */ - - - diff --git a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/simple_peripheral.c b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/simple_peripheral.c index 6a7ce9a..6d5a04b 100644 --- a/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/simple_peripheral.c +++ b/simplelink/ble_sdk_2_02_02_25/src/examples/simple_peripheral/cc26xx/app/simple_peripheral.c @@ -648,9 +648,6 @@ static void key_manage(uint32_t delta_time) * * @return None. */ -// static Clock_Struct detectKeyClock; -// static void detectKey_clockHandler(UArg arg); - static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1) { bool elite_on = false; batteryADC_flag = false; @@ -663,9 +660,9 @@ static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1) { gpio_init(); Board_initSPI(); - spi0_open(10000000, POL0, PHA1); //10M // SPI0 = LED - spi1_open(1000000, POL0, PHA0); //1M - // Elite_SPI_init(); + spi0_open(SPI_CLK_1M, POL0, PHA1); //SPI 1M: LED + spi1_open(SPI_CLK_4M, POL0, PHA0); //SPI 4M: AD5941 + elite_gptimer_open();