Refactor SPI extension: use inheritance instead of modifying MbedOS files

This commit is contained in:
Vlastimil Slinták 2025-03-15 11:33:43 +01:00
parent 58f257e95c
commit 7e09a4644f
7 changed files with 61 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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);
}

21
NPR_14/source/SPI_F4HDK.h Normal file
View file

@ -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

View file

@ -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;

View file

@ -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;
};

View file

@ -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()
{