/* * Reads an int24 from the MS5611 address `adr`, stores it to `d`. */ static void ms5611_read_s24(uint8_t adr, int32_t* d) { uint32_t data_received; //To store result uint8_t adc_adr = 0x00; //Command to read ADC from baro spi_enable(MS5611_SPID); // The SPI peripheral is enabled. // CHIBIOS spiSelect(&MS5611_SPID); spi_send8(MS5611_SPID, adr); // Data is written to the SPI interface after the previous write transfer has finished. //TODO: Sleep for 1 ms /* * Wait for conversion to complete. There doesn't appear to be any way * to do this without timing it, unfortunately. * * If we just watch the clock we'll consume enormous CPU time for little * gain. Instead we'll sleep for "roughly" 1ms and then wait until at least * the desired 0.6ms have passed. */ spi_send8(MS5611_SPID, adc_adr); // Asks the device to send the results from the ADC. data_received = spi_read(MS5611_SPID); //A ADC_READ command returns a 3 byte result *d = data_received; /* //OLD CODE: uint8_t adc_adr = 0x00, rx[3]; int32_t t0; */ /* Start conversion */ //spiSelect(&MS5611_SPID); //spiSend(&MS5611_SPID, 1, (void*)&adr); /* * Wait for conversion to complete. There doesn't appear to be any way * to do this without timing it, unfortunately. * * If we just watch the clock we'll consume enormous CPU time for little * gain. Instead we'll sleep for "roughly" 1ms and then wait until at least * the desired 0.6ms have passed. */ // t0 = halGetCounterValue(); //chThdSleepMilliseconds(1); //while(halGetCounterValue() - t0 < US2RTT(600)) { // chThdYield(); //} /* Deassert CS */ //spiUnselect(&MS5611_SPID); /* Read ADC result */ // spiSelect(&MS5611_SPID); // spiSend(&MS5611_SPID, 1, (void*)&adc_adr); // spiReceive(&MS5611_SPID, 3, (void*)rx); //spiUnselect(&MS5611_SPID); // *d = rx[0] << 16 | rx[1] << 8 | rx[2]; }
void radio_write_single_reg(uint8_t reg, uint8_t data) { while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_clear(R_CS_PORT, R_CS_PIN); spi_send8(R_SPI, reg | (1<<7)); spi_read8(R_SPI); // Wait until send FIFO is empty while(SPI_SR(R_SPI) & SPI_SR_BSY); spi_send8(R_SPI, data); spi_read8(R_SPI); // Wait until send FIFO is empty while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_set(R_CS_PORT, R_CS_PIN); }
uint8_t radio_read_single_reg(uint8_t reg) { while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_clear(R_CS_PORT, R_CS_PIN); spi_send8(R_SPI, reg & 0x7F); spi_read8(R_SPI); // Wait until send FIFO is empty while(SPI_SR(R_SPI) & SPI_SR_BSY); spi_send8(R_SPI, 0x0); while(SPI_SR(R_SPI) & SPI_SR_BSY); uint8_t out = spi_read8(R_SPI); gpio_set(R_CS_PORT, R_CS_PIN); return out; }
static void radio_write_reg_start(uint8_t reg) { while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_clear(R_CS_PORT, R_CS_PIN); spi_send8(R_SPI, reg | (1<<7)); spi_read8(R_SPI); }
void radio_read_burst_reg(uint8_t reg, uint8_t *buff, uint16_t len) { while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_clear(R_CS_PORT, R_CS_PIN); spi_send8(R_SPI, reg & 0x7F); spi_read8(R_SPI); // Wait until send FIFO is empty while(SPI_SR(R_SPI) & SPI_SR_BSY); uint16_t i; for ( i = 0; i < len; i++) { spi_send8(R_SPI, 0x0); while(SPI_SR(R_SPI) & SPI_SR_BSY); buff[i] = spi_read8(R_SPI); } gpio_set(R_CS_PORT, R_CS_PIN); }
void radio_write_burst_reg(uint8_t reg, uint8_t *data, uint16_t len) { while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_clear(R_CS_PORT, R_CS_PIN); spi_send8(R_SPI, reg | (1<<7)); spi_read8(R_SPI); // Wait until send FIFO is empty uint16_t i = 0; for (i = 0; i < len; i++) { while(SPI_SR(R_SPI) & SPI_SR_BSY); spi_send8(R_SPI, data[i]); spi_read8(R_SPI); } // Wait until send FIFO is empty while(SPI_SR(R_SPI) & SPI_SR_BSY); gpio_set(R_CS_PORT, R_CS_PIN); }
/* * Reads a uint16 from the MS5611 address `adr`, stores it to `c`. */ static void ms5611_read_u16(uint8_t adr, uint16_t* c) { /* adr must be 0xA0 to 0xAE */ uint16_t data_received; spi_enable(MS5611_SPID); // The SPI peripheral is enabled. // CHIBIOS spiSelect(&MS5611_SPID); spi_send8(MS5611_SPID, adr); // Data is written to the SPI interface after the previous write transfer has finished. data_received = spi_read(MS5611_SPID); //A PROM read returns a 2 byte result spi_disable(MS5611_SPID); // spiUnselect(&MS5611_SPID); *c = data_received; }
/* * Resets the MS5611. Sends 0x1E, waits 5ms. * */ static void ms5611_reset() { uint8_t adr = 0x1E; // Wgat is stored here? uint16_t i = 0; spi_enable(MS5611_SPID); // The SPI peripheral is enabled. // CHIBIOS spiSelect(&MS5611_SPID); spi_send8(MS5611_SPID, adr); // Data is written to the SPI interface after the previous write transfer has finished. // Note that in chibios an adress is sent, while here the origenal data is sent?? // spiSend(&MS5611_SPID, 1, (void*)&adr); //TODO: time this instead for (i = 0; i < 10000; i++) { __asm__("nop"); //Not a pretty way of doing this, but will do for now // chThdSleepMilliseconds(5); } spi_disable(MS5611_SPID); // spiUnselect(&MS5611_SPID); }
static void radio_write_reg_continuing(uint8_t data) { while(SPI_SR(R_SPI) & SPI_SR_BSY); spi_send8(R_SPI, data); spi_read8(R_SPI); }
int main(void) { uint8_t temp; int16_t gyr_x; clock_setup(); gpio_setup(); usart_setup(); spi_setup(); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_CTRL_REG1); spi_read8(SPI1); spi_send8(SPI1, GYR_CTRL_REG1_PD | GYR_CTRL_REG1_XEN | GYR_CTRL_REG1_YEN | GYR_CTRL_REG1_ZEN | (3 << GYR_CTRL_REG1_BW_SHIFT)); spi_read8(SPI1); gpio_set(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_CTRL_REG4); spi_read8(SPI1); spi_send8(SPI1, (1 << GYR_CTRL_REG4_FS_SHIFT)); spi_read8(SPI1); gpio_set(GPIOE, GPIO3); while (1) { gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_WHO_AM_I | GYR_RNW); spi_read8(SPI1); spi_send8(SPI1, 0); temp=spi_read8(SPI1); my_usart_print_int(USART2, (temp)); gpio_set(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_STATUS_REG | GYR_RNW); spi_read8(SPI1); spi_send8(SPI1, 0); temp=spi_read8(SPI1); my_usart_print_int(USART2, (temp)); gpio_set(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_OUT_TEMP | GYR_RNW); spi_read8(SPI1); spi_send8(SPI1, 0); temp=spi_read8(SPI1); my_usart_print_int(USART2, (temp)); gpio_set(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_OUT_X_L | GYR_RNW); spi_read8(SPI1); spi_send8(SPI1, 0); gyr_x=spi_read8(SPI1); gpio_set(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3); spi_send8(SPI1, GYR_OUT_X_H | GYR_RNW); spi_read8(SPI1); spi_send8(SPI1, 0); gyr_x|=spi_read8(SPI1) << 8; my_usart_print_int(USART2, (gyr_x)); gpio_set(GPIOE, GPIO3); int i; for (i = 0; i < 80000; i++) /* Wait a bit. */ __asm__("nop"); } return 0; }