rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Eric Holland |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <kernel/mutex.h> |
| 25 | #include <platform/gpio.h> |
| 26 | #include <target/gpioconfig.h> |
| 27 | #include <target/bmi055.h> |
| 28 | #include <target/sensor_bus.h> |
| 29 | #include <dev/accelerometer.h> |
| 30 | |
| 31 | static mutex_t sensorbus_mutex; |
| 32 | static SPI_HandleTypeDef spi_handle; |
| 33 | |
| 34 | static uint8_t tx_buff[16]; |
| 35 | static uint8_t rx_buff[16]; |
| 36 | |
| 37 | status_t acc_read_xyz(position_vector_t * pos_vector_p) |
| 38 | { |
| 39 | tx_buff[0] = BMI055_ADDRESS_READ( BMI055_ACC_ACCD_X_LSB ); |
| 40 | if ( acc_flush(tx_buff, rx_buff, 7) == NO_ERROR ) |
| 41 | { |
| 42 | pos_vector_p->x = 0.001*(((int8_t)rx_buff[2] << 4) | ( (rx_buff[1] >> 4) & 0x0F)); |
| 43 | pos_vector_p->y = 0.001*(((int8_t)rx_buff[4] << 4) | ( (rx_buff[3] >> 4) & 0x0F)); |
| 44 | pos_vector_p->z = 0.001*(((int8_t)rx_buff[6] << 4) | ( (rx_buff[5] >> 4) & 0x0F)); |
| 45 | return NO_ERROR; |
| 46 | } |
| 47 | else { |
| 48 | return ERR_GENERIC; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | status_t acc_flush(uint8_t * tbuff, uint8_t * rbuff, uint8_t numbytes) |
| 53 | { |
| 54 | status_t ret_status; |
| 55 | |
| 56 | mutex_acquire(&sensorbus_mutex); |
| 57 | |
| 58 | gpio_set(GPIO_ACC_nCS,GPIO_PIN_RESET); |
| 59 | |
| 60 | ret_status = HAL_SPI_TransmitReceive(&spi_handle, tbuff, rbuff, numbytes, 5000); |
| 61 | |
| 62 | gpio_set(GPIO_ACC_nCS,GPIO_PIN_SET); |
| 63 | |
| 64 | mutex_release(&sensorbus_mutex); |
| 65 | |
| 66 | return ret_status; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @brief Initiale SPI5 module and IO for control of spi bus linking nrf51, accelerometer, and gyroscope. |
| 71 | * |
| 72 | */ |
| 73 | status_t sensor_bus_init_early(void) |
| 74 | { |
| 75 | __HAL_SENSOR_BUS_GPIO_CLK_ENABLE(); |
| 76 | __HAL_RCC_SPI5_CLK_ENABLE(); |
| 77 | |
| 78 | gpio_config(GPIO_SPI5_SCK, GPIO_STM32_AF | GPIO_STM32_AFn(GPIO_AF5_SPI5) | GPIO_PULLUP); |
| 79 | gpio_config(GPIO_SPI5_MISO, GPIO_STM32_AF | GPIO_STM32_AFn(GPIO_AF5_SPI5) | GPIO_PULLUP); |
| 80 | gpio_config(GPIO_SPI5_MOSI, GPIO_STM32_AF | GPIO_STM32_AFn(GPIO_AF5_SPI5) | GPIO_PULLUP); |
| 81 | |
| 82 | gpio_config(GPIO_NRF_CS, GPIO_OUTPUT ); |
| 83 | gpio_config(GPIO_NRF_INT, GPIO_INPUT | GPIO_PULLUP); |
| 84 | |
| 85 | gpio_config(GPIO_GYRO_nCS, GPIO_OUTPUT ); |
| 86 | gpio_config(GPIO_GYRO_INT, GPIO_INPUT | GPIO_PULLUP); |
| 87 | |
| 88 | gpio_config(GPIO_ACC_nCS, GPIO_OUTPUT ); |
| 89 | gpio_config(GPIO_ACC_INT, GPIO_INPUT | GPIO_PULLUP); |
| 90 | |
| 91 | gpio_set(GPIO_NRF_CS, GPIO_PIN_RESET); |
| 92 | gpio_set(GPIO_GYRO_nCS, GPIO_PIN_SET); |
| 93 | gpio_set(GPIO_ACC_nCS, GPIO_PIN_SET); |
| 94 | |
| 95 | spi_handle.Instance = SPI5; |
| 96 | spi_handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; |
| 97 | spi_handle.Init.Direction = SPI_DIRECTION_2LINES; |
| 98 | spi_handle.Init.CLKPhase = SPI_PHASE_1EDGE; |
| 99 | spi_handle.Init.CLKPolarity = SPI_POLARITY_LOW; |
| 100 | spi_handle.Init.DataSize = SPI_DATASIZE_8BIT; |
| 101 | spi_handle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
| 102 | spi_handle.Init.TIMode = SPI_TIMODE_DISABLE; |
| 103 | spi_handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
| 104 | spi_handle.Init.CRCPolynomial = 7; |
| 105 | spi_handle.Init.NSS = SPI_NSS_SOFT; |
| 106 | spi_handle.Init.Mode = SPI_MODE_MASTER; |
| 107 | |
| 108 | if (HAL_SPI_Init(&spi_handle) != HAL_OK) { |
| 109 | return ERR_GENERIC; |
| 110 | } |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | void sensor_bus_init(void) |
| 117 | { |
| 118 | mutex_init(&sensorbus_mutex); |
| 119 | } |
| 120 | |
| 121 | |