diff --git a/NPR_14/Makefile b/NPR_14/Makefile index bc33133..84972f0 100644 --- a/NPR_14/Makefile +++ b/NPR_14/Makefile @@ -249,6 +249,7 @@ OBJECTS += ./source/Eth_IPv4.o OBJECTS += ./source/HMI_telnet.o OBJECTS += ./source/L1L2_radio.o OBJECTS += ./source/SI4463.o +OBJECTS += ./source/SPI_F4HDK.o OBJECTS += ./source/TDMA.o OBJECTS += ./source/Virt_Chan.o OBJECTS += ./source/W5500.o diff --git a/NPR_14/source/SI4463.h b/NPR_14/source/SI4463.h index e9f3f73..981ff2e 100644 --- a/NPR_14/source/SI4463.h +++ b/NPR_14/source/SI4463.h @@ -20,6 +20,7 @@ #include "mbed.h" #include "SI4463.h" +#include "SPI_F4HDK.h" #define SI4463_offset_size 90 #define SI4463_CONF_RX_FIFO_threshold 90 @@ -32,7 +33,7 @@ //#define SI4463_zero_frame_time 590 struct SI4463_Chip{ - SPI* spi; + SPI_F4HDK* spi; DigitalOut* cs; InterruptIn* interrupt; int RX_TX_state; //0:nothing 1:RX 2:TX diff --git a/NPR_14/source/SPI_F4HDK.cpp b/NPR_14/source/SPI_F4HDK.cpp new file mode 100644 index 0000000..4ee9bf2 --- /dev/null +++ b/NPR_14/source/SPI_F4HDK.cpp @@ -0,0 +1,30 @@ +#include "spi_api.h" +#include "SPI_F4HDK.h" + +#if DEVICE_SPI_ASYNCH + #define SPI_S(obj) (( struct spi_s *)(&(obj->spi))) +#else + #define SPI_S(obj) (( struct spi_s *)(obj)) +#endif + +int spi_master_transfer_2(spi_t *obj, const unsigned char *tx, size_t tx_length, unsigned char *rx, size_t rx_length) { + struct spi_s *spiobj = SPI_S(obj); + SPI_HandleTypeDef *handle = &(spiobj->handle); + + if(tx_length < rx_length) { + tx_length = rx_length; + } + + /* Use 10ms timeout */ + uint16_t ret = HAL_SPI_TransmitReceive(handle, (uint8_t *)tx, (uint8_t *)rx, tx_length, 3); //3 + + if(ret == HAL_OK) { + return tx_length; + } else { + return -1; + } +} + +int SPI_F4HDK::transfer_2(const unsigned char *tx_buffer, int tx_length, unsigned char *rx_buffer, int rx_length) { + return spi_master_transfer_2 (&_spi, tx_buffer, tx_length, rx_buffer, rx_length); +} diff --git a/NPR_14/source/SPI_F4HDK.h b/NPR_14/source/SPI_F4HDK.h new file mode 100644 index 0000000..f1c1d0a --- /dev/null +++ b/NPR_14/source/SPI_F4HDK.h @@ -0,0 +1,21 @@ +#ifndef __SPI_F4HDK_H__ +#define __SPI_F4HDK_H__ + +#include "mbed.h" +#include "SPI.h" + +/** + * This class inherits from the standard SPI and adds a custom function, + * `transfer_2`, originally written by F4HDK. The original code directly + * modified the MbedOS files `SPI.h` and `stm_spi_api.c`, which is not ideal. + * The new approach uses inheritance in C++ to create a custom SPI class with + * an additional method, `transfer_2`. + */ +class SPI_F4HDK: public SPI { +public: + using SPI::SPI; + + int transfer_2(const unsigned char *tx_buffer, int tx_length, unsigned char *rx_buffer, int rx_length); +}; + +#endif diff --git a/NPR_14/source/W5500.h b/NPR_14/source/W5500.h index ef46243..49bad5d 100644 --- a/NPR_14/source/W5500.h +++ b/NPR_14/source/W5500.h @@ -19,9 +19,10 @@ #define W5500_F4 #include "mbed.h" +#include "SPI_F4HDK.h" struct W5500_chip{ - SPI* spi_port; + SPI_F4HDK* spi_port; DigitalOut* cs; DigitalIn* interrupt; unsigned char sock_interrupt; diff --git a/NPR_14/source/ext_SRAM2.h b/NPR_14/source/ext_SRAM2.h index ba4eacc..0f11c4f 100644 --- a/NPR_14/source/ext_SRAM2.h +++ b/NPR_14/source/ext_SRAM2.h @@ -19,9 +19,10 @@ #define EXT_SRAM_F4 #include "mbed.h" +#include "SPI_F4HDK.h" struct ext_SRAM_chip{ - SPI* spi_port; + SPI_F4HDK* spi_port; DigitalOut* cs; }; diff --git a/NPR_14/source/main.cpp b/NPR_14/source/main.cpp index c341f5e..d285550 100644 --- a/NPR_14/source/main.cpp +++ b/NPR_14/source/main.cpp @@ -27,6 +27,7 @@ #include "TDMA.h" #include "signaling.h" #include "config_flash.h" +#include "SPI_F4HDK.h" #include "ext_SRAM2.h" @@ -46,12 +47,12 @@ DigitalOut LED_connected(PA_12); DigitalIn Int_W5500(PA_8); DigitalOut CS1(PA_11);//CS W5500 -SPI spi_2(PB_5, PB_4, PB_3); // mosi, miso, sclk +SPI_F4HDK spi_2(PB_5, PB_4, PB_3); // mosi, miso, sclk DigitalOut CS3(PB_0);// CS ext SRAM PB_0 InterruptIn Int_SI4463(PA_3); DigitalOut CS2(PA_4); -SPI spi_1(PA_7, PA_6, PA_5); // mosi, miso, sclk +SPI_F4HDK spi_1(PA_7, PA_6, PA_5); // mosi, miso, sclk int main() {