/* * Read from the switch registers * */ int switch_read(uint8_t param1) { uint8_t reg[2]; if (param1 < 128) { reg[0] = 96; } else { reg[0] = 97; } reg[1] = param1 << 1; /* Select the DF memory to check. */ usart_spi_select_device(USART_SPI, &USART_SPI_DEVICE); /* Send the Manufacturer ID Read command. */ usart_spi_write_packet(USART_SPI, reg, 2); /* Receive Manufacturer ID. */ usart_spi_read_packet(USART_SPI, reg, 1); /* Deselect the checked DF memory. */ usart_spi_deselect_device(USART_SPI, &USART_SPI_DEVICE); return reg[0]; }
/* * Write to the switch registers * */ int switch_write(uint8_t param1, uint8_t param2) { uint8_t reg[3]; if (param1 < 128) { reg[0] = 64; } else { reg[0] = 65; } reg[1] = param1 << 1; reg[2] = param2; /* Select the DF memory to check. */ usart_spi_select_device(USART_SPI, &USART_SPI_DEVICE); /* Send the Manufacturer ID Read command. */ usart_spi_write_packet(USART_SPI, reg, 3); /* Deselect the checked DF memory. */ usart_spi_deselect_device(USART_SPI, &USART_SPI_DEVICE); for(int x = 0;x<100000;x++); return switch_read(param1); }
/*! \brief SPI slave tranfers data to Usart SPI master */ static bool spi_slave_transfer(void) { /* Select the Slave device */ usart_spi_select_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Put the slave read command in master tx buffer */ data_master_tx[0] = SLAVE_RD_CMD; count = 0; /* Send the Read command */ usart_spi_write_packet(USART_SPI_EXAMPLE, data_master_tx, 1); /* Receive data from slave */ usart_spi_read_packet(USART_SPI_EXAMPLE, data_master_rx, DATA_BUFFER_SIZE); /* Deselect the Slave device */ usart_spi_deselect_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Check the master received data with slave tx data */ for (uint8_t i = 0; i < DATA_BUFFER_SIZE; i++) { if (data_master_rx[i] == data_slave_tx[i]) { continue; } else { return false; } } return true; }
/** * \internal * \brief Helper function to send a byte over 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. * * \param data the byte to be transfered */ __always_inline static void hx8347a_send_byte(uint8_t data) { #if defined(CONF_HX8347A_USART_SPI) /* 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 */ # if UC3 usart_spi_write_packet(CONF_HX8347A_USART_SPI, &data, 1); # elif XMEGA usart_spi_write_single(CONF_HX8347A_USART_SPI, data); # endif #elif defined(CONF_HX8347A_SPI) spi_write_single(CONF_HX8347A_SPI, data); /* Wait for TX to complete */ while (!spi_is_tx_ok(CONF_HX8347A_SPI)) { /* Do nothing */ } #endif }
/** * \internal * \brief Helper function to write a number of bytes * * This function is used to write several bytes to the interface in one go. * * \param data pointer to the location of the data to write * \param length the number of bytes to write */ __always_inline static void hx8347a_write_packet(const uint8_t *data, const uint32_t length) { #if defined(CONF_HX8347A_USART_SPI) usart_spi_write_packet(CONF_HX8347A_USART_SPI, data, length); #elif defined(CONF_HX8347A_SPI) spi_write_packet(CONF_HX8347A_SPI, data, length); #endif }
/** * \brief Send one byte to the SerialFlash. * * \param data The data byte to send. * \pre The SerialFlash should be selected first using at25dfx_spi_select_device. */ status_code_t at25dfx_spi_write_byte(uint8_t data) { #if defined( AT25DFX_USES_SPI_MASTER_SERVICE) return spi_write_packet(AT25DFX_SPI_MODULE, &data, 1); /* Implementation with USART in SPI mode service */ #elif defined(AT25DFX_USES_USART_SPI_SERVICE) return usart_spi_write_packet(AT25DFX_SPI_MODULE, &data, 1); #endif }
/** * \brief Send a sequence of bytes to a SerialFlash. * * * \param data Data buffer to write * \param len Length of data * \pre The SerialFlash should be selected first using at25dfx_spi_select_device * */ status_code_t at25dfx_spi_write_packet(void const *data, size_t len) { #if defined( AT25DFX_USES_SPI_MASTER_SERVICE) return spi_write_packet(AT25DFX_SPI_MODULE, (uint8_t*)data, len); /* Implementation with USART in SPI mode service */ #elif defined(AT25DFX_USES_USART_SPI_SERVICE) return usart_spi_write_packet(AT25DFX_SPI_MODULE, (uint8_t*)data, len); #endif }
/*! \brief Usart configured as SPI master transfers data to SPI slave. */ static bool spi_usart_master_transfer(void) { /* Select the Slave device */ usart_spi_select_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Send the data to slave */ usart_spi_write_packet(USART_SPI_EXAMPLE, data_master_tx, DATA_BUFFER_SIZE); /* Deselect the Slave device */ usart_spi_deselect_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Check the Slave received data with master tx data */ for (uint8_t i = 0; i < DATA_BUFFER_SIZE; i++) { if (data_master_tx[i] == data_slave_rx[i]) { continue; } else { return false; } } return true; }
static bool usart_spi_at45dbx_mem_check(void) { /* Select the DF memory to check. */ usart_spi_select_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Send the Manufacturer ID Read command. */ usart_spi_write_packet(USART_SPI_EXAMPLE, data, 1); /* Receive Manufacturer ID. */ usart_spi_read_packet(USART_SPI_EXAMPLE, data, DATA_BUFFER_SIZE); /* Extract the Manufacturer ID. */ manufacturer_id = data[0]; /* Deselect the checked DF memory. */ usart_spi_deselect_device(USART_SPI_EXAMPLE, &USART_SPI_DEVICE_EXAMPLE); /* Check the Manufacturer id. */ if (manufacturer_id == ATMEL_MANUFACTURER_ID) { return true; } else { return false; } }