snickerdoodle Platform Controller
firmware for STM32F0 platform controller
SPI Bridge

Bridging from SPI to other peripherals. More...

Data Structures

struct  sd_spbr_cmd
 SPI bridge device operation structure. More...
 
struct  sd_spbr_dev
 SPI bridge device structure. More...
 
struct  sd_spbr
 SPI bridge structure. More...
 

Macros

#define SD_SPBR_NDEV   (3)
 
#define SD_SPBR_I2C_STATUS   ((uint8_t)(1 << 0))
 
#define SD_SPBR_LED_STATUS   ((uint8_t)(1 << 1))
 
#define SD_SPBR_UART_STATUS   ((uint8_t)(1 << 2))
 
#define SD_SPBR_DEV_MASK   (0xF8)
 
#define SD_SPBR_I2C_DEV   (0x80)
 
#define SD_SPBR_CMD_MASK   (0xE0)
 
#define SD_SPBR_I2C_RW_CMD   (0xA0)
 
#define SD_SPBR_I2C_READ_TRANS   ((uint8_t)(1 << 2))
 
#define SD_SPBR_I2C_ADDR_10BIT   ((uint8_t)(1 << 3))
 
#define SD_SPBR_I2C_FREQ_400K   ((uint8_t)(1 << 4))
 
#define SD_SPBR_NULL_DEV   { 0, NULL, NULL }
 
#define SD_SPBR_NULL_OP   { 0, NULL, NULL }
 

Enumerations

enum  sd_spbr_state { SD_SPBR_READ_DEVICE, SD_SPBR_READ_CMD, SD_SPBR_IN_PROCESS }
 

Functions

static void sd_spbr_rx_isr (struct sd_spbr *spbr)
 SPI Bridge Receive Interrupt Routine. More...
 
static struct sd_spbr_devsd_spbr_find_device (struct sd_spbr_dev *head, uint8_t id)
 SPI Bridge Find Device Searches a sentinel terminated table pointed to by head for a matching ID and returns a pointer to the device or NULL. More...
 
static struct sd_spbr_cmdsd_spbr_find_cmd (struct sd_spbr_cmd *head, uint8_t opcode)
 SPI Bridge Find Command Searches a sentinel terminated table pointed to by head for a matching ID and returns a pointer to the device or NULL. More...
 
void sd_spbr_init (struct sd_spbr *spbr, SPI_HandleTypeDef *spi)
 SPI Bridge Initialize. More...
 
void sd_spbr_irqhandler (struct sd_spbr *spbr)
 SPI Bridge Interrupt Handler Handle interrupt events on the SPI peripheral. More...
 

Variables

struct sd_spbr spi2_spbr
 
static struct sd_spbr_dev spbr_dev_tab []
 

Detailed Description

Bridging from SPI to other peripherals.

Function Documentation

◆ sd_spbr_find_cmd()

static struct sd_spbr_cmd* sd_spbr_find_cmd ( struct sd_spbr_cmd head,
uint8_t  opcode 
)
static

#include <Src/sd_spi_bridge.c>

SPI Bridge Find Command Searches a sentinel terminated table pointed to by head for a matching ID and returns a pointer to the device or NULL.

Note
To speed up the loops, this function expects the device mask be applied to the id before it is passed to this function.
Parameters
headPointer to head of table for searching
idID value to search for
Return values
Pointerto matching device or NULL if not found
114 {
115  struct sd_spbr_cmd *pcmd = head;
116 
117  while ((pcmd != NULL) && (pcmd->opcode > 0)) {
118  if (pcmd->opcode == opcode)
119  return pcmd;
120 
121  pcmd++;
122  }
123 
124  return NULL;
125 }
SPI bridge device operation structure.
Definition: sd_spi_bridge.h:93

◆ sd_spbr_find_device()

static struct sd_spbr_dev* sd_spbr_find_device ( struct sd_spbr_dev head,
uint8_t  id 
)
static

#include <Src/sd_spi_bridge.c>

SPI Bridge Find Device Searches a sentinel terminated table pointed to by head for a matching ID and returns a pointer to the device or NULL.

Note
To speed up the loops, this function expects the device mask be applied to the id before it is passed to this function.
Parameters
headPointer to head of table for searching
idID value to search for
Return values
Pointerto matching device or NULL if not found
87 {
88  struct sd_spbr_dev *pdev = head;
89 
90  while ((pdev != NULL) && (pdev->id > 0)) {
91  if (pdev->id == id)
92  return pdev;
93 
94  pdev++;
95  }
96 
97  return NULL;
98 }
SPI bridge device structure.
Definition: sd_spi_bridge.h:105
uint8_t id
Definition: sd_spi_bridge.h:106

◆ sd_spbr_init()

void sd_spbr_init ( struct sd_spbr spbr,
SPI_HandleTypeDef *  spi 
)

#include <Src/sd_spi_bridge.c>

SPI Bridge Initialize.

Parameters
spbrPointer to SPI bridge structure to initialize
spiSPI handle to use for bridge
Return values
None
136 {
137 
138  /* Reset the UART device structure */
139  memset(spbr, 0, sizeof(struct sd_spbr));
140 
141  spbr->spi = spi;
142 
143  /* Prepare the peripheral to receive */
144  /* Enable RXNE and ERR interrupt */
145  __HAL_SPI_ENABLE_IT(spi, (SPI_IT_RXNE | SPI_IT_ERR));
146 
147  /* Check if the SPI is already enabled */
148  if ((spi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
149  __HAL_SPI_ENABLE(spi); /* Enable SPI peripheral */
150 
151  spbr->state = SD_SPBR_READ_DEVICE;
152 }
enum sd_spbr_state state
Definition: sd_spi_bridge.h:121
SPI bridge structure.
Definition: sd_spi_bridge.h:117

◆ sd_spbr_irqhandler()

void sd_spbr_irqhandler ( struct sd_spbr spbr)

#include <Src/sd_spi_bridge.c>

SPI Bridge Interrupt Handler Handle interrupt events on the SPI peripheral.

Parameters
spbrPointer to SPI bridge structure
Return values
None
163 {
164  SPI_HandleTypeDef *spi = spbr->spi;
165 
166  uint32_t itsource = spi->Instance->CR2;
167  uint32_t itflag = spi->Instance->SR;
168 
169  /*---------------------- SPI in mode Receiver ------------------------*/
170  if (((itflag & SPI_FLAG_OVR) == RESET) &&
171  ((itflag & SPI_FLAG_RXNE) != RESET) &&
172  ((itsource & SPI_IT_RXNE) != RESET)) {
173  sd_spbr_rx_isr(spbr);
174  return;
175  }
176 
177  /*--------------------- SPI in mode Transmitter ----------------------*/
178  if (((itflag & SPI_FLAG_TXE) != RESET) &&
179  ((itsource & SPI_IT_TXE) != RESET)) {
180  spi->TxISR(spi);
181  return;
182  }
183 
184  /*-------------------- SPI in Error Treatment ------------------------*/
185  if ((itflag & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE)) != RESET) {
186  /* SPI Overrun error interrupt occurred */
187  if ((itflag & SPI_FLAG_OVR) != RESET) {
188  if (spi->State != HAL_SPI_STATE_BUSY_TX) {
189  spi->ErrorCode |= HAL_SPI_ERROR_OVR;
190  __HAL_SPI_CLEAR_OVRFLAG(spi);
191  } else {
192  return;
193  }
194  }
195 
196  /* SPI Mode Fault error interrupt occurred */
197  if ((itflag & SPI_FLAG_MODF) != RESET) {
198  spi->ErrorCode |= HAL_SPI_ERROR_MODF;
199  __HAL_SPI_CLEAR_MODFFLAG(spi);
200  }
201 
202  /* SPI Frame error interrupt occurred */
203  if ((itflag & SPI_FLAG_FRE) != RESET) {
204  spi->ErrorCode |= HAL_SPI_ERROR_FRE;
205  __HAL_SPI_CLEAR_FREFLAG(spi);
206  }
207 
208  __HAL_SPI_DISABLE_IT(spi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
209  spi->State = HAL_SPI_STATE_READY;
210  return;
211  }
212 }
static void sd_spbr_rx_isr(struct sd_spbr *spbr)
SPI Bridge Receive Interrupt Routine.
Definition: sd_spi_bridge.c:221

◆ sd_spbr_rx_isr()

static void sd_spbr_rx_isr ( struct sd_spbr spbr)
static

#include <Src/sd_spi_bridge.c>

SPI Bridge Receive Interrupt Routine.

Parameters
spbrPointer to SPI bridge structure
Return values
None
222 {
223  uint8_t data = (uint8_t)(spbr->spi->Instance->DR);
224 
225  switch (spbr->state) {
226  case SD_SPBR_READ_DEVICE:
227 
228  /* Find the device from the table */
229  spbr->curr_dev = sd_spbr_find_device(spbr_dev_tab, data & SD_SPBR_DEV_MASK);
230  if (spbr->curr_dev == NULL)
231  return;
232 
233  /* Run callback for device, if one exists */
234  if (spbr->curr_dev->dev_start != NULL)
235  spbr->curr_dev->dev_start(&data);
236 
237  /* Set the device ID in the transmit buffer */
238  HAL_SPI_Transmit_IT(spbr->spi, &data, 1);
239 
240  spbr->state = SD_SPBR_READ_CMD;
241  break;
242 
243  case SD_SPBR_READ_CMD:
244 
246  (data & SD_SPBR_CMD_MASK));
247 
248  if (spbr->curr_dev->curr_cmd == NULL) {
249  /* A matching command from the device list was not found
250  * This is an exception that needs to be handled
251  */
252  spbr->state = SD_SPBR_READ_DEVICE;
253  return;
254  }
255 
256  if (spbr->curr_dev->curr_cmd != NULL)
257  spbr->curr_dev->curr_cmd->start(&data);
258 
259  /* Set the device ID in the transmit buffer */
260  HAL_SPI_Transmit_IT(spbr->spi, &data, 1);
261 
262  spbr->state = SD_SPBR_IN_PROCESS;
263 
264  break;
265 
266  case SD_SPBR_IN_PROCESS:
267 
268 
269  break;
270  }
271 }
enum sd_spbr_state state
Definition: sd_spi_bridge.h:121
static struct sd_spbr_dev * sd_spbr_find_device(struct sd_spbr_dev *head, uint8_t id)
SPI Bridge Find Device Searches a sentinel terminated table pointed to by head for a matching ID and ...
Definition: sd_spi_bridge.c:86
static struct sd_spbr_cmd * sd_spbr_find_cmd(struct sd_spbr_cmd *head, uint8_t opcode)
SPI Bridge Find Command Searches a sentinel terminated table pointed to by head for a matching ID and...
Definition: sd_spi_bridge.c:113
struct sd_spbr_cmd * cmds
Definition: sd_spi_bridge.h:108
struct sd_spbr_dev * curr_dev
Definition: sd_spi_bridge.h:119
void(* dev_start)(uint8_t *data)
Definition: sd_spi_bridge.h:109
struct sd_spbr_cmd * curr_cmd
Definition: sd_spi_bridge.h:107

Variable Documentation

◆ spbr_dev_tab

struct sd_spbr_dev spbr_dev_tab[]
static

#include <Src/sd_spi_bridge.c>

Initial value:
= {
{ SD_SPBR_I2C_DEV, NULL, NULL },
SD_SPBR_NULL_DEV
}