📄 spi.c
字号:
#include "io430.h"
#include "in430.h"
#include "CC1100.h"
#include "hal_msp430.h"
//----------------------------------------------------------------------------------
// Target specific initialization of SPI interface in hal_spi_config.c
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// void halSpiWrite(uint8 addr, const uint8 *buffer, uint16 length)
//
// DESCRIPTION:
// Write data to device, starting at internal device address "addr".
// The device will increment the address internally for every new byte
// that is written. For single byte write, set length to 1.
//----------------------------------------------------------------------------------
uint8 halSpiWrite(uint8 addr, const uint8* data, uint16 length)
{
uint16 i;
uint8 rc;
HAL_SPI_BEGIN;
#ifdef HAL_SPI_INTERFACE_BITBANG
rc = halSpiBitbangOut(addr);
for (i = 0; i < length; i++)
{
halSpiBitbangOut(data[i]);
}
#else
HAL_SPI_TXBUF_SET(addr);
HAL_SPI_WAIT_TXFIN;
rc = HAL_SPI_RXBUF;
for (i = 0; i < length; i++)
{
HAL_SPI_TXBUF_SET(data[i]);
HAL_SPI_WAIT_TXBUF;
}
#ifndef HAL_SPI_INTERFACE_USI
HAL_SPI_WAIT_TXFIN;
#endif
#endif
HAL_SPI_END;
return(rc);
}
//----------------------------------------------------------------------------------
// uint8 halSpiRead(uint8 addr, uint8* data, uint16 length)
//
// DESCRIPTION:
// Read data from device, starting at internal device address "addr".
// The device will increment the address internally for every new byte
// that is read. Note that the master device needs to write a dummy byte
// (in this case 0) for every new byte in order to generate the clock to
// clock out the data. For single byte read, set length to 1.
//----------------------------------------------------------------------------------
uint8 halSpiRead(uint8 addr, uint8* data, uint16 length)
{
uint16 i;
uint8 rc;
HAL_SPI_BEGIN;
#ifdef HAL_SPI_INTERFACE_BITBANG
rc = halSpiBitbangOut(addr);
for (i = 0; i < length; i++)
{
data[i] = halSpiBitbangIn();
}
#else
HAL_SPI_TXBUF_SET(addr);
HAL_SPI_WAIT_TXFIN;
rc = HAL_SPI_RXBUF;
for (i = 0; i < length; i++)
{
HAL_SPI_TXBUF_SET(0); // Dummy write to read data byte
HAL_SPI_WAIT_TXFIN;
data[i] = HAL_SPI_RXBUF; // Store data from last data RX
}
#endif
HAL_SPI_END;
return(rc);
}
//----------------------------------------------------------------------------------
// uint8 halSpiStrobe(uint8 cmd)
//
// DESCRIPTION:
// Special write function, writing only one byte (cmd) to the device.
//----------------------------------------------------------------------------------
uint8 halSpiStrobe(uint8 cmd)
{
uint8 rc;
HAL_SPI_BEGIN;
#ifdef HAL_SPI_INTERFACE_BITBANG
rc = halSpiBitbangOut(cmd);
#else
HAL_SPI_TXBUF_SET(cmd);
HAL_SPI_WAIT_TXFIN;
rc = HAL_SPI_RXBUF;
#endif
HAL_SPI_END;
return(rc);
}
void halSpiInit()
{
// For the moment, this UART implementation only
// supports communication settings 115200 8N1
// i.e. ignore baudrate and options arguments.
#if 1
// Keep peripheral in reset state
U0CTL = SWRST;
// 8-bit SPI Master 3-pin mode, with SMCLK as clock source
U0CTL |= CHAR + SYNC + MM;
U0TCTL |= CKPH + SSEL1 + SSEL0 + STC;
// Ignore clockrate argument for now, just use clock source/2
U0BR0 = 0x02;
U0BR1 = 0x00;
U0MCTL = 0x00;
// Enable SPI mode
ME1 |= USPIE0;
// P3SEL |= BIT1 + BIT2 + BIT3;
// P3DIR |= BIT3 + BIT0 + BIT1;
// Set up pins used by peripheral unit
MCU_IO_PERIPHERAL(HAL_SPI_SOMI_PORT, HAL_SPI_SOMI_PIN);
MCU_IO_PERIPHERAL(HAL_SPI_SIMO_PORT, HAL_SPI_SIMO_PIN);
MCU_IO_PERIPHERAL(HAL_SPI_CLK_PORT, HAL_SPI_CLK_PIN);
MCU_IO_OUTPUT(HAL_SPI_CS_PORT, HAL_SPI_CS_PIN, 1);
// Release for operation
U0CTL &= ~SWRST;
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -