Advertisement
kyleh04

Untitled

May 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. /*! ----------------------------------------------------------------------------
  2.  * @file    deca_spi.c
  3.  * @brief   SPI access functions
  4.  *
  5.  * @attention
  6.  *
  7.  * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland.
  8.  *
  9.  * All rights reserved.
  10.  *
  11.  * @author DecaWave
  12.  */
  13. #include "deca_spi.h"
  14. #include "deca_device_api.h"
  15.  
  16. /*! ------------------------------------------------------------------------------------------------------------------
  17.  * Function: openspi()
  18.  *
  19.  * Low level abstract function to open and initialise access to the SPI device.
  20.  * returns 0 for success, or -1 for error
  21.  */
  22. int openspi(/*SPI_TypeDef* SPIx*/)
  23. {
  24.  
  25.     return 0;
  26.  
  27. } // end openspi()
  28.  
  29. /*! ------------------------------------------------------------------------------------------------------------------
  30.  * Function: closespi()
  31.  *
  32.  * Low level abstract function to close the the SPI device.
  33.  * returns 0 for success, or -1 for error
  34.  */
  35. int closespi(void)
  36. {
  37.  
  38.     return 0;
  39.  
  40. } // end closespi()
  41.  
  42. /*! ------------------------------------------------------------------------------------------------------------------
  43.  * Function: writetospi()
  44.  *
  45.  * Low level abstract function to write to the SPI
  46.  * Takes two separate byte buffers for write header and write data
  47.  * returns 0 for success, or -1 for error
  48.  */
  49.  
  50.  uint8_t spi_recieve_byte(uint8_t b)
  51.  {
  52.    SPDR = b;
  53.    while(!(SPSR & (1<<SPIF)));
  54.    return SPDR;
  55.  }
  56.  
  57. int writetospi(uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer)
  58. {
  59.     decaIrqStatus_t  stat ;
  60.  
  61.     //stat = decamutexon() ;
  62.  
  63.     digitalWrite(10, LOW);
  64.  
  65.     for (uint8_t i = 0; i < headerLength; i++)
  66.         spi_recieve_byte(headerBuffer[i]);
  67.  
  68.     for (uint8_t i = 0; i < bodylength; i++)
  69.         spi_recieve_byte(bodyBuffer[i]);
  70.  
  71.     digitalWrite(10, HIGH);
  72.  
  73.   //decamutexoff(stat) ;
  74.  
  75.   return 0;
  76. } // end writetospi()
  77.  
  78.  
  79. /*! ------------------------------------------------------------------------------------------------------------------
  80.  * Function: readfromspi()
  81.  *
  82.  * Low level abstract function to read from the SPI
  83.  * Takes two separate byte buffers for write header and read data
  84.  * returns the offset into read buffer where first byte of read data may be found,
  85.  * or returns -1 if there was an error
  86.  */
  87. int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)
  88. {
  89.     decaIrqStatus_t  stat ;
  90.  
  91.     //stat = decamutexon() ;
  92.  
  93.     digitalWrite(10, LOW);
  94.  
  95.     for (uint8_t i = 0; i < headerLength; i++)
  96.         spi_recieve_byte(headerBuffer[i]);
  97.  
  98.     for (uint8_t i = 0; i < readlength; i++)
  99.         readBuffer[i] = spi_recieve_byte(0);
  100.  
  101.     digitalWrite(10, HIGH);
  102.   //decamutexoff(stat) ;
  103.  
  104.   return 0;
  105. } // end readfromspi()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement