z-stack cc2530 SPI code

z-stack cc2530 SPI code:
/*******************************************************
 *                 INCLUDES
 *******************************************************/
#include "hal_types.h"
#include "hal_spi.h"
#include "OSAL.h"
#include "OnBoard.h"
#include "hal_assert.h"

/******************************************************
 *                     CONSTANTS
 ******************************************************/
/*
  //control軟體控制
  P1.4 - CS
  //spi硬體控制
  P1.5 - CLK
  P1.6 - MOSI
  P1.7 - MISO
*/

#define HAL_SPI_CS_PORT 1
#define HAL_SPI_CS_PIN  4
#define HAL_SPI_CLK_PORT 1
#define HAL_SPI_CLK_PIN  5
#define HAL_SPI_MOSI_PORT 1
#define HAL_SPI_MOSI_PIN  6
#define HAL_SPI_MISO_PORT 1
#define HAL_SPI_MISO_PIN  7

/**************************************************

 *                       MACROS

 **************************************************/
#define HAL_IO_SET(port, pin, val)        HAL_IO_SET_PREP(port, pin, val)
#define HAL_IO_SET_PREP(port, pin, val)   st( P##port##_##pin## = val; )
#define HAL_CONFIG_IO_OUTPUT(port, pin, val)      HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val)
#define HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \
                                                      P##port##_##pin## = val; \
                                                      P##port##DIR |= BV(pin); )
#define HAL_CONFIG_IO_PERIPHERAL(port, pin)      HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin)
#define HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BV(pin); )
/*******************************************************
 *                   FUNCTIONS - API
 *******************************************************/

void HalSpi_HW_Init(void);

void HalSpiInit(void)
{
  HalSpi_HW_Init();
}

static void halSpi_ConfigSPI(void)
{
   uint8 baud_exponent;
   uint8 baud_mantissa;

  PERCFG |= 0x02;
  //設定硬體控制腳位
  HAL_CONFIG_IO_PERIPHERAL(HAL_SPI_CLK_PORT,  HAL_SPI_CLK_PIN);
  HAL_CONFIG_IO_PERIPHERAL(HAL_SPI_MOSI_PORT, HAL_SPI_MOSI_PIN);
  HAL_CONFIG_IO_PERIPHERAL(HAL_SPI_MISO_PORT, HAL_SPI_MISO_PIN);
  //設定傳輸速度
  baud_exponent = 10;
  baud_mantissa =  0;

  /* Configure SPI */
  U1UCR  = 0x80;
  U1CSR  = 0x00;
  //U1GCR  = HAL_SPI_TRANSFER_MSB_FIRST | HAL_SPI_CLOCK_PHA_0 | HAL_SPI_CLOCK_POL_LO | baud_exponent;//正緣觸發,正緣接收資料
  U1GCR  = HAL_SPI_TRANSFER_MSB_FIRST | 0x40 | HAL_SPI_CLOCK_POL_LO | baud_exponent;//正緣觸發,負緣接收資料
  U1BAUD = baud_mantissa;
}

void HalSpi_HW_Init(void)
{
  //設定基本output模式
  HAL_CONFIG_IO_OUTPUT(HAL_SPI_CS_PORT,    HAL_SPI_CS_PIN,    1);
  halSpi_ConfigSPI();
}

static void spi_tx(uint8 x)
{
  U1CSR &= ~((1<<2) | (1<<1));
  U1DBUF = x;
  while( !(U1CSR & (1<<1)) );
}

void HalSpi_Ad7763_Read(uint8 addr, uint8 *recv_data)
{
    //將CS腳位到LOW
    HAL_IO_SET(HAL_SPI_CS_PORT, HAL_SPI_CS_PIN, 0);  //begin
    addr &= 0x3f;//ADE7765讀取 8bit→0 7bit→0
    spi_tx(addr);//傳送
    spi_tx(0);
    *recv_data = U1DBUF;//接收
    spi_tx(0);
    *(recv_data+1) = U1DBUF;
    spi_tx(0);
    *(recv_data+2) = U1DBUF;
    asm("NOP");
    asm("NOP");
    asm("NOP");
    asm("NOP");
    //將CS腳位到HIGH
    HAL_IO_SET(HAL_SPI_CS_PORT, HAL_SPI_CS_PIN, 1);  //end
}

void HalSpi_Ad7763_Write(uint8 addr, uint16 send_data)
{
    //將CS腳位到LOW
    HAL_IO_SET(HAL_SPI_CS_PORT, HAL_SPI_CS_PIN, 0);  //begin
    addr |= 0x80;//ADE7765寫入 8bit→1 7bit→0
    spi_tx(addr);
    spi_tx((send_data&0xff00)>>8);
    spi_tx(send_data&0x00ff);
    asm("NOP");
    asm("NOP");
    asm("NOP");
    asm("NOP");
    //將CS腳位到HIGH
    HAL_IO_SET(HAL_SPI_CS_PORT, HAL_SPI_CS_PIN, 1);  //end
}

0 意見:

張貼留言