snickerdoodle Platform Controller
firmware for STM32F0 platform controller
SPI Functions

Modules

 SPI Private Functions
 

Functions

void sd_spi2_init (void)
 
void HAL_SPI_MspInit (SPI_HandleTypeDef *hspi)
 
void HAL_SPI_MspDeInit (SPI_HandleTypeDef *hspi)
 
enum sd_spi_error sd_spi_dev_init (struct sd_spi_dev *dev, SPI_HandleTypeDef *spi)
 SPI Device Initialization Initialize SPI device structure. More...
 
enum sd_spi_error sd_spi_rx_init (struct sd_spi_dev *dev, struct sd_cbuf *rx_buff)
 SPI Receive Interrupt Initialization Enable and prepare the SPI to receive on interrupt. More...
 
void sd_spi_irqhandler (struct sd_spi_dev *dev)
 SPI Interrupt Handler Handle interrupt events on the spi peripheral. More...
 
static enum sd_spi_error sd_spi_rx_isr (struct sd_spi_dev *dev)
 Receive an amount of data in interrupt mode. More...
 
static void sd_spi_error_cb (struct sd_spi_dev *dev)
 Handle SPI error interrupts. More...
 

Detailed Description

Function Documentation

◆ HAL_SPI_MspDeInit()

void HAL_SPI_MspDeInit ( SPI_HandleTypeDef *  hspi)

#include <Src/sd_spi.c>

SPI2 GPIO Configuration
PB15 ---—> SPI2_MOSI PB14 ---—> SPI2_MISO PB13 ---—> SPI2_SCK PB12 ---—> SPI2_NSS

145 {
146  if (hspi->Instance==SPI2) {
147  /* Peripheral clock disable */
148  __SPI2_CLK_DISABLE();
149 
157  HAL_GPIO_DeInit(GPIOB, ZYNQ_SPI_MOSI_Pin|ZYNQ_SPI_MISO_Pin|ZYNQ_SPI_CLK_Pin|ZYNQ_SPI_NSS_Pin);
158 
159  /* Peripheral interrupt Deinit*/
160  HAL_NVIC_DisableIRQ(SPI2_IRQn);
161 
162  }
163 }

◆ HAL_SPI_MspInit()

void HAL_SPI_MspInit ( SPI_HandleTypeDef *  hspi)

#include <Src/sd_spi.c>

SPI2 GPIO Configuration

PB15 ---—> SPI2_MOSI PB14 ---—> SPI2_MISO PB13 ---—> SPI2_SCK PB12 ---—> SPI2_NSS

117 {
118  GPIO_InitTypeDef GPIO_InitStruct;
119 
120  if (hspi->Instance==SPI2) {
121  /* Peripheral clock enable */
122  __SPI2_CLK_ENABLE();
123 
131  GPIO_InitStruct.Pin = ZYNQ_SPI_MOSI_Pin|ZYNQ_SPI_MISO_Pin|ZYNQ_SPI_CLK_Pin|ZYNQ_SPI_NSS_Pin;
132  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
133  GPIO_InitStruct.Pull = GPIO_NOPULL;
134  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
135  GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
136  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
137 
138  /* Peripheral interrupt init*/
139  HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
140  HAL_NVIC_EnableIRQ(SPI2_IRQn);
141  }
142 }

◆ sd_spi_dev_init()

enum sd_spi_error sd_spi_dev_init ( struct sd_spi_dev dev,
SPI_HandleTypeDef *  spi 
)

#include <Src/sd_spi.c>

SPI Device Initialization Initialize SPI device structure.

Parameters
spi_devDevice structure to initialize
Return values
SD_SPI_SUCCESSon success, error state otherwise
175 {
176  /* Reset the UART device structure */
177  memset(dev, 0, sizeof(struct sd_spi_dev));
178 
179  dev->spi = spi;
180 
181  return SD_SPI_SUCCESS;
182 }
SPI device structure.
Definition: sd_spi.h:98
Definition: sd_spi.h:54
SPI_HandleTypeDef * spi
Definition: sd_spi.h:99

◆ sd_spi_error_cb()

static void sd_spi_error_cb ( struct sd_spi_dev dev)
inlinestatic

#include <Src/sd_spi.c>

Handle SPI error interrupts.

Parameters
spiSPI handle with error triggered error interrupt
Return values
none
384 {
385  /* Handle errors */
386 }

◆ sd_spi_irqhandler()

void sd_spi_irqhandler ( struct sd_spi_dev dev)

#include <Src/sd_spi.c>

SPI Interrupt Handler Handle interrupt events on the spi peripheral.

Parameters
spi_devSPI device that triggered the interrupt
Return values
none
262 {
263  SPI_HandleTypeDef *spi = dev->spi;
264 
265  uint32_t itsource = spi->Instance->CR2;
266  uint32_t itflag = spi->Instance->SR;
267 
268  /*---------------------- SPI in mode Receiver ------------------------*/
269  if (((itflag & SPI_FLAG_OVR) == RESET) &&
270  ((itflag & SPI_FLAG_RXNE) != RESET) &&
271  ((itsource & SPI_IT_RXNE) != RESET)) {
272  sd_spi_rx_isr(dev);
273  return;
274  }
275 
276  /*--------------------- SPI in mode Transmitter ----------------------*/
277  if (((itflag & SPI_FLAG_TXE) != RESET) &&
278  ((itsource & SPI_IT_TXE) != RESET)) {
279  spi->TxISR(spi);
280  return;
281  }
282 
283  /*-------------------- SPI in Error Treatment ------------------------*/
284  if ((itflag & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE)) != RESET) {
285  /* SPI Overrun error interrupt occurred */
286  if ((itflag & SPI_FLAG_OVR) != RESET) {
287  if (spi->State != HAL_SPI_STATE_BUSY_TX) {
288  spi->ErrorCode |= HAL_SPI_ERROR_OVR;
289  __HAL_SPI_CLEAR_OVRFLAG(spi);
290  } else {
291  return;
292  }
293  }
294 
295  /* SPI Mode Fault error interrupt occurred */
296  if ((itflag & SPI_FLAG_MODF) != RESET) {
297  spi->ErrorCode |= HAL_SPI_ERROR_MODF;
298  __HAL_SPI_CLEAR_MODFFLAG(spi);
299  }
300 
301  /* SPI Frame error interrupt occurred */
302  if ((itflag & SPI_FLAG_FRE) != RESET) {
303  spi->ErrorCode |= HAL_SPI_ERROR_FRE;
304  __HAL_SPI_CLEAR_FREFLAG(spi);
305  }
306 
307  __HAL_SPI_DISABLE_IT(spi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
308  spi->State = HAL_SPI_STATE_READY;
309  sd_spi_error_cb(dev);
310  return;
311  }
312 }
SPI_HandleTypeDef * spi
Definition: sd_spi.h:99
static void sd_spi_error_cb(struct sd_spi_dev *dev)
Handle SPI error interrupts.
Definition: sd_spi.c:383
static enum sd_spi_error sd_spi_rx_isr(struct sd_spi_dev *dev)
Receive an amount of data in interrupt mode.
Definition: sd_spi.c:321

◆ sd_spi_rx_init()

enum sd_spi_error sd_spi_rx_init ( struct sd_spi_dev dev,
struct sd_cbuf rx_buff 
)

#include <Src/sd_spi.c>

SPI Receive Interrupt Initialization Enable and prepare the SPI to receive on interrupt.

Note
This function expects that the SPI peripheral and the buffer have already been inititialized and are ready to be used.
Parameters
devDevice to initialize SPI and buffer
spiSPI peripheral handle
rx_buffBuffer to use for UART receive function
Return values
SD_SPI_SUCCESSon success, error state otherwise
199 {
200  SPI_HandleTypeDef *spi = dev->spi;
201 
202  if ((dev == NULL) || (spi == NULL))
203  goto error;
204 
205  dev->spi = spi; /* Set the UART handle pointer */
206 
207  /* Process Locked */
208  if (spi->Lock == HAL_LOCKED)
209  return SD_SPI_LOCKED;
210  else
211  spi->Lock = HAL_LOCKED;
212 
213  /* Configure communication */
214  spi->State = HAL_SPI_STATE_BUSY_RX;
215  spi->ErrorCode = HAL_SPI_ERROR_NONE;
216 
217 
218  if (spi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) {
219  spi->CRCSize = 1;
220  if ((spi->Init.DataSize <= SPI_DATASIZE_8BIT) && (spi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)) {
221  spi->CRCSize = 2;
222  }
223  } else {
224  spi->CRCSize = 0;
225  }
226 
227  /* set fiforxthresold according the reception data length: 8 bit */
228  SET_BIT(spi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
229 // spi->RxISR = SPI_RxISR_8BIT;
230 
231  /* Configure communication direction : 1Line */
232  if (spi->Init.Direction == SPI_DIRECTION_1LINE)
233  SPI_1LINE_RX(spi);
234 
235  /* Reset CRC Calculation */
236  if (spi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
237  SPI_RESET_CRC(spi);
238 
239 
240  /* Enable RXNE and ERR interrupt */
241  __HAL_SPI_ENABLE_IT(spi, (SPI_IT_RXNE | SPI_IT_ERR));
242 
243  /* Check if the SPI is already enabled */
244  if ((spi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
245  __HAL_SPI_ENABLE(spi); /* Enable SPI peripheral */
246 
247  error :
248  /* Process Unlocked */
249  __HAL_UNLOCK(spi);
250  return SD_SPI_ERROR;
251 }
SPI_HandleTypeDef * spi
Definition: sd_spi.h:99
Definition: sd_spi.h:58
Definition: sd_spi.h:56

◆ sd_spi_rx_isr()

enum sd_spi_error sd_spi_rx_isr ( struct sd_spi_dev dev)
static

#include <Src/sd_spi.c>

Receive an amount of data in interrupt mode.

Parameters
spi_devThe SPI peripheral that triggered interrupt
Return values
SD_SPI_SUCCESSon success, error status otherwise
Todo:
Determine where to put the collected data based on the first two bytes (register and packet length) and point to the peripheral that matches the the register.
322 {
323  struct sd_cbuf *rx_buff = dev->rx_buff;
324  SPI_HandleTypeDef *spi = dev->spi;
325  uint8_t c;
326 
327  rx_buff->buff[rx_buff->in++] = (uint8_t)spi->Instance->DR;
328 
329  if (rx_buff->in >= rx_buff->size)
330  rx_buff->in = 0;
331 
332  switch (dev->rx_state) {
333  case SD_SPI_RX_WAITING:
334  /*
335  * Use the received byte to point to the corresponding
336  * peripheral/interface.
337  */
338  dev->pkt->hdr = c;
339  dev->rx_state = SD_SPI_RX_LENGTH;
340  break;
341 
342  case SD_SPI_RX_LENGTH:
343  /*
344  * Use the received byte to determine the length of the packet
345  * message that is to be received.
346  */
347  dev->pkt->len = c;
348  dev->pkt->pload = &spi_pkt_buff[0];
349  dev->rx_state = SD_SPI_RX_PACKET;
350  break;
351 
352  case SD_SPI_RX_PACKET:
353  /* In the process of receiving a packet. */
354  *(dev->pkt->pload++) = c;
355  if (--dev->pkt->len == 0) {
356  /* Retransmit the buffer */
357  dev->pkt->pload = &spi_pkt_buff[0];
358  /* sd_spi_bridge(dev); */
360  }
361  break;
362  }
363 
364 
370 
371 // if (sd_char_buff_putc_to(rx_buff, c, 100) == SD_BUFF_SUCCESS)
372  return SD_SPI_SUCCESS;
373 // else
374 // return SD_SPI_ERROR;
375 }
uint32_t size
Definition: sd_buffer.h:107
uint8_t * buff
Definition: sd_buffer.h:103
uint8_t len
Definition: sd_spi.h:89
Definition: sd_spi.h:80
uint32_t in
Definition: sd_buffer.h:104
Character buffer structure.
Definition: sd_buffer.h:102
Definition: sd_spi.h:54
SPI_HandleTypeDef * spi
Definition: sd_spi.h:99
Definition: sd_spi.h:79
uint8_t * pload
Definition: sd_spi.h:90
uint8_t hdr
Definition: sd_spi.h:88
Definition: sd_spi.h:78
struct sd_cbuf * rx_buff
Definition: sd_spi.h:100
struct sd_spi_packet * pkt
Definition: sd_spi.h:102
enum sd_spi_rx_state rx_state
Definition: sd_spi.h:101