char CCRead(char addr, char* data) { char stat; spi_select_device(&SPIC, &spi_device_conf); while(PORTC.IN&PIN6_bm==PIN6_bm); //Wait for MISO to go low spi_write_single(&SPIC, addr|0x80); spi_read_single(&SPIC, &stat) spi_write_single(&SPIC, CONFIG_SPI_MASTER_DUMMY); spi_read_single(&SPIC, data); spi_deselect_device(&SPIC, &spi_device_conf); return stat; };
/** * \brief Receive a sequence of bytes from an SPI device. * * All bytes sent out on SPI bus are sent as value 0. * * \param p_spi Base address of the SPI instance. * \param data Data buffer to read. * \param len Length of data to be read. * * \pre SPI device must be selected with spi_select_device() first. */ status_code_t spi_read_packet(Spi *p_spi, uint8_t *data, size_t len) { uint32_t timeout = SPI_TIMEOUT; uint8_t val; uint32_t i = 0; while (len) { timeout = SPI_TIMEOUT; while (!spi_is_tx_ready(p_spi)) { if (!timeout--) { return ERR_TIMEOUT; } } spi_write_single(p_spi, CONFIG_SPI_MASTER_DUMMY); timeout = SPI_TIMEOUT; while (!spi_is_rx_ready(p_spi)) { if (!timeout--) { return ERR_TIMEOUT; } } spi_read_single(p_spi, &val); data[i] = val; i++; len--; } return STATUS_OK; }
/** * \internal * \brief Helper function to read a byte from an arbitrary interface * * This function is used to hide what interface is used by the driver, e.g. * the driver does not need to know if USART in SPI mode is used or the native * SPI module. */ __always_inline static uint8_t hx8347a_read_byte(void) { uint8_t data; #if defined(CONF_HX8347A_USART_SPI) # if UC3 /* A workaround for optimizing data transfer had to be done for the * XMEGA in order for the display to work correctly at all SPI clock * clock speeds */ /* This function could also be used for XMEGA but results in a very slow * framerate, hence a workaround has been implemented for the XMEGA */ usart_spi_read_packet(CONF_HX8347A_USART_SPI, &data, 1); # elif XMEGA usart_spi_read_single(CONF_HX8347A_USART_SPI, &data); # endif #elif defined(CONF_HX8347A_SPI) spi_write_single(CONF_HX8347A_SPI, 0xFF); /* Wait for RX to complete */ while (!spi_is_rx_full(CONF_HX8347A_SPI)) { /* Do nothing */ } spi_read_single(CONF_HX8347A_SPI, &data); #endif return data; }
/** * \internal * \brief Helper function to read a byte from an arbitrary interface * * This function is used to hide what interface is used by the component * driver, e.g. the component driver does not need to know if USART in SPI * mode is used or the native SPI module. * * \retval uint8_t Byte of data read from the display controller */ __always_inline static uint8_t ili9341_read_byte(void) { uint8_t data; #if defined(CONF_ILI9341_USART_SPI) # if XMEGA /* Workaround for clearing the RXCIF for XMEGA */ usart_rx_enable(CONF_ILI9341_USART_SPI); usart_put(CONF_ILI9341_USART_SPI, 0xFF); while (!usart_rx_is_complete(CONF_ILI9341_USART_SPI)) { /* Do nothing */ } data = usart_get(CONF_ILI9341_USART_SPI); /* Workaround for clearing the RXCIF for XMEGA */ usart_rx_disable(CONF_ILI9341_USART_SPI); # else usart_spi_read_single(CONF_ILI9341_USART_SPI, &data); # endif #elif defined(CONF_ILI9341_SPI) spi_write_single(CONF_ILI9341_SPI, 0xFF); ili9341_wait_for_send_done(); /* Wait for RX to complete */ while (!spi_is_rx_full(CONF_ILI9341_SPI)) { /* Do nothing */ } spi_read_single(CONF_ILI9341_SPI, &data); #endif return data; }
/** * \brief Send and receive a sequence of bytes from an SPI device. * * \param p_spi Base address of the SPI instance. * \param tx_data Data buffer to send. * \param rx_data Data buffer to read. * \param len Length of data to be read. * * \pre SPI device must be selected with spi_select_device() first. */ spi_status_t spi_transceive_packet(Spi *p_spi, uint8_t *tx_data, uint8_t *rx_data, size_t len) { uint32_t timeout = SPI_TIMEOUT; uint8_t val; uint32_t i = 0; while (len) { timeout = SPI_TIMEOUT; while (!spi_is_tx_ready(p_spi)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } spi_write_single(p_spi, tx_data[i]); timeout = SPI_TIMEOUT; while (!spi_is_rx_ready(p_spi)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } spi_read_single(p_spi, &val); rx_data[i] = val; i++; len--; } return SPI_OK; }
/** * \brief Receive a sequence of bytes from an SPI device. * * All bytes sent out on SPI bus are sent as value 0. * * \param p_spi Base address of the SPI instance. * \param data Data buffer to read. * \param len Length of data to be read. * * \pre SPI device must be selected with spi_select_device() first. */ spi_status_t spi_read_packet(Spi *p_spi, uint8_t *data, size_t len) { if (len-- == 0) return SPI_OK; for (uint16_t i = 0; i < len; i++) { uint32_t timeout = SPI_TIMEOUT; while (!spi_is_tx_ready(p_spi)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } p_spi->SPI_TDR = 0x000000FF; timeout = SPI_TIMEOUT; while (!spi_is_rx_ready(p_spi)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } data[i] = p_spi->SPI_RDR; } return spi_read_single(p_spi, &data[len]); }
/** * \brief Receive a single byte from a SPI device * * byte sent out on SPI bus is sent as value 0. * * \param spi Base address of the SPI instance. * * \pre SPI device must be selected with spi_select_device() first */ uint8_t spi_read_once(SPI_t *spi) { uint8_t data; spi_write_single(spi,CONFIG_SPI_MASTER_DUMMY); //Dummy write while(!spi_is_rx_full(spi)); spi_read_single(spi,&data); return data; }
/** * \brief Receive a sequence of bytes from a SPI device * * All bytes sent out on SPI bus are sent as value 0. * * \param spi Base address of the SPI instance. * \param data data buffer to read * \param len Length of data * * \pre SPI device must be selected with spi_select_device() first */ status_code_t spi_read_packet(SPI_t *spi, uint8_t *data, size_t len) { while(len) { spi_write_single(spi,CONFIG_SPI_MASTER_DUMMY); //Dummy write while(!spi_is_rx_full(spi)); spi_read_single(spi,data); len--; data++; } return STATUS_OK; }
void AT42QT1110_spi_read_timeout(uint8_t* data, uint32_t timeout_us) { uint32_t t = 0; while (!spi_is_rx_full(SPI_TOUCH)) { delay_us(1); if(t++ > timeout_us) return; } t = 0; spi_read_single(&SPI_TOUCH, data); return; }
char CCStrobe(char addr) { char stat; spi_select_device(&SPIC, &spi_device_conf); while(PORTC.IN&PIN6_bm==PIN6_bm); //Wait for MISO to go low spi_write_single(&SPIC, addr); spi_read_single(&SPIC, &stat); spi_deselect_device(&SPIC, &spi_device_conf); return stat; }
uint8_t CS4270_read_data(void) { uint8_t data; spi_select_device(SPI_CODEC, &spi_device_codec); spi_write_single(SPI_CODEC, CS4270_ADDRESS_READ); spi_read_single(SPI_CODEC, &data); spi_deselect_device(SPI_CODEC, &spi_device_codec); return data; }
char CCWriteBurst(char addr, const char* dataPtr, char size) { char stat; spi_select_device(&SPIC, &spi_device_conf); while(PORTC.IN&PIN6_bm==PIN6_bm); //Wait for MISO to go low spi_write_single(&SPIC, addr|0x40); spi_read_single(&SPIC, &stat); spi_write_packet(&SPIC, *dataPtr, size); spi_deselect_device(&SPIC, &spi_device_conf); return stat; }
/** * \brief Send a command to the ADS7843 touch controller. * * \param uc_cmd command to send. * * \return Command result. */ static uint32_t ads7843_send_cmd(uint8_t uc_cmd) { uint32_t uResult = 0; volatile uint32_t i; uint8_t data; uint32_t timeout = SPI_TIMEOUT; /* (volatile declaration needed for code optimisation by compiler) */ volatile uint8_t bufferRX[ADS7843_BUFSIZE]; volatile uint8_t bufferTX[ADS7843_BUFSIZE]; bufferRX[0] = 0; bufferRX[1] = 0; bufferRX[2] = 0; bufferTX[0] = uc_cmd; bufferTX[1] = 0; bufferTX[2] = 0; for(i = 0; i < ADS7843_BUFSIZE; i++){ timeout = SPI_TIMEOUT; while (!spi_is_tx_ready(BOARD_ADS7843_SPI_BASE)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } spi_write_single(BOARD_ADS7843_SPI_BASE, bufferTX[i]); } for(i = 0; i < ADS7843_BUFSIZE; i++){ timeout = SPI_TIMEOUT; while (!spi_is_rx_full(BOARD_ADS7843_SPI_BASE)) { if (!timeout--) { return SPI_ERROR_TIMEOUT; } } spi_read_single(BOARD_ADS7843_SPI_BASE, &data); bufferRX[i] = data; } uResult = (uint32_t)bufferRX[1] << 8; uResult |= (uint32_t)bufferRX[2]; uResult = uResult >> 4; return uResult; }
static sint8 spi_rw(uint8* pu8Mosi, uint8* pu8Miso, uint16 u16Sz) { struct spi_device spi_device_conf; spi_device_conf.id = CONF_WIFI_M2M_SPI_CS_PIN; uint8 u8Dummy = 0; uint8 u8SkipMosi = 0, u8SkipMiso = 0; uint16_t txd_data = 0; uint16_t rxd_data = 0; if (!pu8Mosi) { pu8Mosi = &u8Dummy; u8SkipMosi = 1; } else if(!pu8Miso) { pu8Miso = &u8Dummy; u8SkipMiso = 1; } else { return M2M_ERR_BUS_FAIL; } spi_select_device(CONF_WIFI_M2M_SPI_MODULE, &spi_device_conf); while (u16Sz) { txd_data = *pu8Mosi; /* Write one byte */ spi_write_packet(CONF_WIFI_M2M_SPI_MODULE, (uint8_t*)(&txd_data), 1); /* Read SPI master data register. */ spi_read_single(CONF_WIFI_M2M_SPI_MODULE, (uint8_t*)(&rxd_data)); *pu8Miso = rxd_data; u16Sz--; if (!u8SkipMiso) pu8Miso++; if (!u8SkipMosi) pu8Mosi++; } spi_deselect_device(CONF_WIFI_M2M_SPI_MODULE, &spi_device_conf); return M2M_SUCCESS; }