You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.1KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Driver for ADC scanning.
  28. #include "braids/drivers/adc.h"
  29. #include <string.h>
  30. namespace braids {
  31. const uint16_t kPinSS = GPIO_Pin_0;
  32. void Adc::Init(bool double_speed) {
  33. // Initialize SS pin.
  34. GPIO_InitTypeDef gpio_init;
  35. gpio_init.GPIO_Pin = kPinSS;
  36. gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
  37. gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
  38. GPIO_Init(GPIOB, &gpio_init);
  39. // Initialize MOSI and SCK pins.
  40. gpio_init.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  41. gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
  42. gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
  43. GPIO_Init(GPIOA, &gpio_init);
  44. // Initialize MISO pin.
  45. gpio_init.GPIO_Pin = GPIO_Pin_6;
  46. gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
  47. gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  48. GPIO_Init(GPIOA, &gpio_init);
  49. // Initialize SPI
  50. SPI_InitTypeDef spi_init;
  51. spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  52. spi_init.SPI_Mode = SPI_Mode_Master;
  53. spi_init.SPI_DataSize = SPI_DataSize_8b;
  54. spi_init.SPI_CPOL = SPI_CPOL_Low;
  55. spi_init.SPI_CPHA = SPI_CPHA_1Edge;
  56. spi_init.SPI_NSS = SPI_NSS_Soft;
  57. spi_init.SPI_BaudRatePrescaler = double_speed ? SPI_BaudRatePrescaler_32 : SPI_BaudRatePrescaler_64;
  58. spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
  59. spi_init.SPI_CRCPolynomial = 7;
  60. SPI_Init(SPI1, &spi_init);
  61. GPIO_SetBits(GPIOB, kPinSS);
  62. SPI_Cmd(SPI1, ENABLE);
  63. rx_word_ = 0;
  64. active_channel_ = 0;
  65. acquisition_stage_ = 0;
  66. memset(channels_, 0, sizeof(channels_));
  67. }
  68. bool Adc::PipelinedRead(uint8_t channel) {
  69. switch (acquisition_stage_) {
  70. case 0:
  71. rx_word_ |= SPI_I2S_ReceiveData(SPI1);
  72. channels_[active_channel_] = rx_word_;
  73. GPIO_SetBits(GPIOB, kPinSS);
  74. GPIO_ResetBits(GPIOB, kPinSS);
  75. SPI_I2S_SendData(SPI1, 0x04 | 0x02);
  76. active_channel_ = channel;
  77. acquisition_stage_ = 1;
  78. break;
  79. case 1:
  80. SPI_I2S_ReceiveData(SPI1);
  81. SPI_I2S_SendData(SPI1, active_channel_ << 6);
  82. acquisition_stage_ = 2;
  83. break;
  84. case 2:
  85. rx_word_ = (SPI_I2S_ReceiveData(SPI1) & 0xf) << 8;
  86. SPI_I2S_SendData(SPI1, 0x00); // Dummy trailing data.
  87. acquisition_stage_ = 0;
  88. break;
  89. }
  90. return acquisition_stage_ == 1;
  91. }
  92. uint16_t Adc::Read(uint8_t channel) {
  93. uint16_t value = 0;
  94. // Send header
  95. GPIO_ResetBits(GPIOB, kPinSS);
  96. SPI_I2S_SendData(SPI1, 0x04 | 0x02);
  97. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  98. SPI_I2S_ReceiveData(SPI1);
  99. // Send channel
  100. SPI_I2S_SendData(SPI1, channel << 6);
  101. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  102. value = (SPI_I2S_ReceiveData(SPI1) & 0xf) << 8;
  103. // Send trailing word
  104. SPI_I2S_SendData(SPI1, 0x00);
  105. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  106. value |= SPI_I2S_ReceiveData(SPI1);
  107. GPIO_SetBits(GPIOB, kPinSS);
  108. return value;
  109. }
  110. } // namespace braids