/* * Application entry point. */ int main(void) { /* * SPI interface configuration */ static SPIConfig spicfg = { SPI_ROLE, IOPORT2, SPI1_SS, SPI_MODE_0, SPI_MSB_FIRST, SPI_SCK_FOSC_128, spicallback }; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); spiStart(&SPID1, &spicfg); sdStart(&SD1, NULL); chprintf((BaseSequentialStream *) &SD1, "Start %s\r\n", ROLE); while(1) { char buf[24]; #if (SPI_ROLE == SPI_ROLE_MASTER) spiSelect(&SPID1); spiExchange(&SPID1, sizeof(msg), msg, buf); spiUnselect(&SPID1); chprintf((BaseSequentialStream *) &SD1,"%s: %s\r\n", ROLE, buf); chThdSleepMilliseconds(500); #else spiExchange(&SPID1, sizeof(msg), msg, buf); chprintf((BaseSequentialStream *) &SD1,"%s: %s\r\n", ROLE, buf); #endif } }
/* Exchange a byte */ static BYTE xchgSpi(BYTE dat) { BYTE receive; spiExchange(1, &dat, &receive); return (BYTE)receive; }
int main(void) { setupSPI(); char m; _delay_ms(50); spiExchange(0xFF); while(1) { // Infinite loop; define here the retex = spiExchange(SPPMLEN); /**GCV**/ retex = spiExchange(0x46); /**fourth channel **/ retex = spiExchange(0x50); /**dont care**/ channelValue |= retex << 8; /**make hibyte **/ retex = spiExchange(NOP); channelValue |= retex; /**make lobyte**/ m = 0 ; } };
static msg_t spi_thread(void *p) { unsigned i; SPIDriver *spip = (SPIDriver *)p; VirtualTimer vt; uint8_t txbuf[256]; uint8_t rxbuf[256]; /* Prepare transmit pattern.*/ for (i = 0; i < sizeof(txbuf); i++) txbuf[i] = (uint8_t)i; /* Continuous transmission.*/ while (TRUE) { /* Starts a VT working as watchdog to catch a malfunction in the SPI driver.*/ chSysLock(); chVTSetI(&vt, MS2ST(10), tmo, NULL); chSysUnlock(); spiExchange(spip, sizeof(txbuf), txbuf, rxbuf); /* Stops the watchdog.*/ chSysLock(); if (chVTIsArmedI(&vt)) chVTResetI(&vt); chSysUnlock(); } }
static THD_FUNCTION(spi_thread, p) { unsigned i; SPIDriver *spip = (SPIDriver *)p; virtual_timer_t vt; uint8_t txbuf[256]; uint8_t rxbuf[256]; chRegSetThreadName("SPI overlord"); chVTObjectInit(&vt); /* Prepare transmit pattern.*/ for (i = 0; i < sizeof(txbuf); i++) txbuf[i] = (uint8_t)i; /* Continuous transmission.*/ while (true) { /* Starts a VT working as watchdog to catch a malfunction in the SPI driver.*/ chVTSet(&vt, MS2ST(10), tmo, (void *)"SPI timeout"); spiExchange(spip, sizeof(txbuf), txbuf, rxbuf); /* Stops the watchdog.*/ chVTReset(&vt); } }
uint16_t ad5504GetData(void){ txBuf[0] = AD5504_READ_BIT | AD5504_DAC_A; spiSelect(&SPID1); spiExchange(&SPID1, sizeof(uint16_t), (uint8_t *)txBuf, (uint8_t *)rxBuf); spiUnselect(&SPID1); return rxBuf[0]; }
static int SPIExchangeData(SPIDriver *SPIPtr, uint8_t *TxBuf, uint8_t *RxBuf, size_t Size) { spiStart(SPIPtr, &HSSpiConfig); /* Setup transfer parameters. */ spiSelect(SPIPtr); /* Slave Select assertion. */ spiExchange(SPIPtr, Size, TxBuf, RxBuf); /* Atomic transfer operations. */ spiUnselect(SPIPtr); /* Slave Select de-assertion. */ return 0; }
/** * @brief Reads a register value. * @pre The SPI interface must be initialized and the driver started. * * @param[in] spip pointer to the SPI initerface * @param[in] reg register number * @return The register value. */ uint8_t l3g4200dReadRegister(SPIDriver *spip, uint8_t reg) { spiSelect(spip); txbuf[0] = 0x80 | reg; txbuf[1] = 0xff; spiExchange(spip, 2, txbuf, rxbuf); spiUnselect(spip); return rxbuf[1]; }
static u8 spi_write(u8 reg, u8 data) { const u8 send_buf[2] = {reg, data}; u8 recv_buf[2]; spiExchange(&FRONTEND_SPI, sizeof(send_buf), send_buf, recv_buf); return recv_buf[1]; }
/** * @brief Reads a register value. * @pre The SPI interface must be initialized and the driver started. * * @param[in] spip pointer to the SPI initerface * @param[in] reg register number * @return The register value. */ uint8_t lsm303dReadRegister(SPIDriver *spip, uint8_t reg) { palClearPad(GPIOA, GPIOA_AM_CS); txbuf[0] = 0x80 | reg; txbuf[1] = 0xff; spiExchange(spip, 2, txbuf, rxbuf); palSetPad(GPIOA, GPIOA_AM_CS); return rxbuf[1]; }
static uint8_t _readOp(uint8_t op, uint8_t addr) { uint8_t data; uint8_t tx[3] = { op | (addr & ADDR_MASK), 0, 0 }; uint8_t rx[3] = { 0 }; spiSelect(_spip); if (addr & 0x80) { spiExchange(_spip, 3, tx, rx); data = rx[2]; } else { spiExchange(_spip, 2, tx, rx); data = rx[1]; } spiUnselect(_spip); return data; }
uint8_t fm25l16_exchange(int len) { spiAcquireBus(&SPID1); /* Acquire ownership of the bus. */ spiStart(&SPID1, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID1); /* Slave Select assertion. */ spiExchange(&SPID1, len, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID1); /* Slave Select de-assertion. */ spiReleaseBus(&SPID1); /* Ownership release. */ return len; }
uint16_t A4960::readReg(const uint8_t addr) { const uint16_t txData = (uint16_t(addr & 0x7) << 13); uint16_t rxData; spiAcquireBus(spip); spiSelect(spip); spiExchange(spip, 1, &txData, &rxData); spiUnselect(spip); spiReleaseBus(spip); return rxData; }
uint16_t A4960::writeReg(const uint8_t addr, const uint16_t data) { const uint16_t txData = (uint16_t(addr & 0x7) << 13) | 0x1000 | (data & 0xfff); uint16_t rxData; spiAcquireBus(spip); spiSelect(spip); spiExchange(spip, 1, &txData, &rxData); spiUnselect(spip); spiReleaseBus(spip); return rxData; }
uint8_t fm25l16_write_read(uint8_t cmd) { spiAcquireBus(&SPID1); /* Acquire ownership of the bus. */ spiStart(&SPID1, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID1); /* Slave Select assertion. */ tx_ch = cmd; spiExchange(&SPID1, 1, &tx_ch, &rx_ch); /* Atomic transfer operations. */ spiUnselect(&SPID1); /* Slave Select de-assertion. */ spiReleaseBus(&SPID1); /* Ownership release. */ return rx_ch; }
uint8_t writeByteSPI(uint8_t txbyte) { uint8_t rxbyte = 0; spiAcquireBus(&SPID1); /* Acquire ownership of the bus. */ spiStart(&SPID1, spicfg); /* Setup transfer parameters. */ spiSelect(&SPID1); /* Slave Select assertion. */ spiExchange(&SPID1, 1, &txbyte, &rxbyte); /* Atomic transfer operations. */ spiUnselect(&SPID1); /* Slave Select de-assertion. */ spiReleaseBus(&SPID1); chThdSleepMilliseconds(1); return rxbyte; }
static THD_FUNCTION(spi_thread_2, p) { (void)p; chRegSetThreadName("SPI thread 2"); while (true) { spiAcquireBus(&PORTAB_SPI1); /* Acquire ownership of the bus. */ palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_OFF); spiStart(&PORTAB_SPI1, &ls_spicfg); /* Setup transfer parameters. */ spiSelect(&PORTAB_SPI1); /* Slave Select assertion. */ spiExchange(&PORTAB_SPI1, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&PORTAB_SPI1); /* Slave Select de-assertion. */ spiReleaseBus(&PORTAB_SPI1); /* Ownership release. */ } }
static THD_FUNCTION(spi_thread_1, p) { (void)p; chRegSetThreadName("SPI thread 1"); while (TRUE) { spiAcquireBus(&SPID1); /* Acquire ownership of the bus. */ palSetPad(GPIOF, GPIOF_LED_GREEN); /* LED ON. */ spiStart(&SPID1, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID1); /* Slave Select assertion. */ spiExchange(&SPID1, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID1); /* Slave Select de-assertion. */ spiReleaseBus(&SPID1); /* Ownership release. */ } }
static THD_FUNCTION(spi_thread_2, p) { (void)p; chRegSetThreadName("SPI thread 2"); while (true) { spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */ palClearPad(GPIOA, GPIOA_LED_GREEN);/* LED OFF. */ spiStart(&SPID2, &ls_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID2); /* Slave Select assertion. */ spiExchange(&SPID2, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID2); /* Slave Select de-assertion. */ spiReleaseBus(&SPID2); /* Ownership release. */ } }
static msg_t spi_thread_2(void *p) { (void)p; chRegSetThreadName("SPI thread 2"); while (TRUE) { spiAcquireBus(&SPID1); /* Acquire ownership of the bus. */ palSetPad(IOPORT3, GPIOC_LED); /* LED OFF. */ spiStart(&SPID1, &ls_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID1); /* Slave Select assertion. */ spiExchange(&SPID1, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID1); /* Slave Select de-assertion. */ spiReleaseBus(&SPID1); /* Ownership release. */ } return 0; }
static msg_t spi_thread_1(void *p) { (void)p; chRegSetThreadName("SPI thread 1"); while (TRUE) { spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */ palClearPad(PORT11, P11_LED1); /* LED ON. */ spiStart(&SPID2, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID2); /* Slave Select assertion. */ spiExchange(&SPID2, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID2); /* Slave Select de-assertion. */ spiReleaseBus(&SPID2); /* Ownership release. */ } return 0; }
/* * SPI bus exchange routine */ int SPIExchangeData(SPIDriver *spip, uint8_t *tx, uint8_t *rx, size_t size) { chMtxLock(&SPIMtx); /* * Do exchange between device and MCU. */ spiAcquireBus(spip); /* Acquire ownership of the bus. */ spiStart(spip, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(spip); /* Slave Select assertion. */ spiExchange(spip, size, tx, rx); /* Atomic transfer operations. */ spiUnselect(spip); /* Slave Select de-assertion. */ spiReleaseBus(spip); /* Ownership release. */ chMtxUnlock(); return 0; }
static THD_FUNCTION(Thread1, arg) { static uint8_t txbuf[5]; static uint8_t rxbuf[5]; (void)arg; chRegSetThreadName("Blinker"); while (true) { palSetPad(GPIOB, GPIOB_LED); /* Send the Manufacturer and Device ID Read command */ txbuf[0] = 0x9F; spiSelect(&SPID1); spiExchange(&SPID1, sizeof(txbuf), txbuf, rxbuf); spiUnselect(&SPID1); chThdSleepMilliseconds(1000); } }
/* * Application entry point. */ void main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * OS initialization. */ chSysInit(); /* * Activates the SPI driver 1 using the driver default configuration. */ spiStart(&SPID1, &spicfg); /* * Normal main() thread activity. */ while (TRUE) { volatile uint8_t b; chThdSleepMilliseconds(1000); /* Exchanging data, if the pins MISO and MOSI are connected then the transmitted data is received back into the buffer. On the STM8S-Discovery board the pins are CN2-9 and CN2-10.*/ spiSelect(&SPID1); spiExchange(&SPID1, sizeof(digits), digits, buffer); /* Polled transfers test.*/ b = spiPolledExchange(&SPID1, 0x55); b = spiPolledExchange(&SPID1, 0xAA); spiUnselect(&SPID1); } }
void sc_spi_exchange(uint8_t spin, uint8_t *tx, uint8_t *rx, size_t bytes) { SPIConfig spi_cfg; chDbgAssert(spin < SC_SPI_MAX_CLIENTS, "SPI n outside range", "#3"); chDbgAssert(spi_conf[spin].spip != NULL, "SPI n not initialized", "#2"); /* spiStart always to set CS for every call according to SPI client. */ spi_cfg.end_cb = NULL; spi_cfg.ssport = spi_conf[spin].cs_port; spi_cfg.sspad = spi_conf[spin].cs_pin; spi_cfg.cr1 = spi_conf[spin].cr1; spiStart(spi_conf[spin].spip, &spi_cfg); spiAcquireBus(spi_conf[spin].spip); spiSelect(spi_conf[spin].spip); spiExchange(spi_conf[spin].spip, bytes, tx, rx); spiUnselect(spi_conf[spin].spip); spiReleaseBus(spi_conf[spin].spip); spiStop(spi_conf[spin].spip); }
void Rf24ChibiosIo::transfernb(const uint8_t* tx, uint8_t* rx, uint32_t len) { spiExchange(driver, len, tx, rx); }
unsigned char halSpiExc(unsigned char x) { unsigned char rx; spiExchange(&CC2520_SPI, 1, &x, &rx); return rx; }
/* * Application entry point. */ int main(void) { unsigned i; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Prepare transmit pattern. */ for (i = 0; i < sizeof(txbuf); i++) txbuf[i] = (uint8_t)i; /* Starting driver for test, DSPI_B I/O pins setup.*/ spiStart(&SPID2, &ls_spicfg); SIU.PCR[102].R = PAL_MODE_OUTPUT_ALTERNATE(1); /* SCK */ SIU.PCR[103].R = PAL_MODE_OUTPUT_ALTERNATE(1); /* SIN */ SIU.PCR[104].R = PAL_MODE_OUTPUT_ALTERNATE(1); /* SOUT */ SIU.PCR[105].R = PAL_MODE_OUTPUT_ALTERNATE(1); /* PCS[0] */ SIU.PCR[106].R = PAL_MODE_OUTPUT_ALTERNATE(1); /* PCS[1] */ /* Testing sending and receiving at the same time.*/ spiExchange(&SPID2, 4, txbuf, rxbuf); spiExchange(&SPID2, 32, txbuf, rxbuf); spiExchange(&SPID2, 512, txbuf, rxbuf); /* Testing clock pulses without data buffering.*/ spiIgnore(&SPID2, 4); spiIgnore(&SPID2, 32); /* Testing sending data ignoring incoming data.*/ spiSend(&SPID2, 4, txbuf); spiSend(&SPID2, 32, txbuf); /* Testing receiving data while sending idle bits (high level).*/ spiReceive(&SPID2, 4, rxbuf); spiReceive(&SPID2, 32, rxbuf); /* Testing stop procedure.*/ spiStop(&SPID2); /* * Starting the transmitter and receiver threads. */ chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa), NORMALPRIO + 1, spi_thread_1, NULL); chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa), NORMALPRIO + 1, spi_thread_2, NULL); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); palTogglePad(PORT11, P11_LED2); } return 0; }
/* Send multiple byte */ static void xmitSpiMulti(const BYTE *buff, UINT btx) { spiExchange(btx, buff, exchangeBuff); }
/* Receive multiple byte */ static void rcvrSpiMulti(BYTE *buff, UINT btr) { memset(exchangeBuff, 0xFFFFFFFF, btr); spiExchange(btr, exchangeBuff, buff); }