Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. /** LAB4 mems SPI sensor template
  2.  *  Hardware platform: STM32F411E-DISCO
  3.  *  PA2 Port - USART TXD should be connected to serial<->usb converter
  4.  */
  5. #include <config/conf.h>
  6. #include <periph/gpio/gpio.hpp>
  7. #include <foundation/sys/dbglog.h>
  8. #include <periph/drivers/serial/uart_early.hpp>
  9. #include <periph/drivers/spi/spi_master.hpp>
  10. #include <isix.h>
  11. #include <periph/dma/dma.hpp>
  12. #include <cstring>
  13. #include <config/conf.h>
  14. #include <periph/gpio/gpio.hpp>
  15. #include <foundation/sys/dbglog.h>
  16. #include <periph/drivers/i2c/i2c_master.hpp>
  17. #include <isix.h>
  18. #include <cstring>
  19. #include <config/conf.h>
  20. #include <foundation/sys/dbglog.h>
  21. #include <stm32_ll_usart.h>
  22. #include <stm32_ll_tim.h>
  23. #include <stm32_ll_system.h>
  24. #include <stm32_ll_bus.h>
  25. #include <isix/arch/irq.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29.  
  30. #define ADDR_WHO_AM_I 0x0F
  31. #define DIR_READ 0x80
  32. #define IADDR_INCREMENT 0x40
  33. #define L3G20_CTRL_REG1 0x20
  34. #define L3G20_CTRL_REG2 0x21
  35. #define L3G20_CTRL_REG3 0x22
  36. #define L3G20_CTRL_REG4 0x23
  37. #define L3G20_CTRL_REG5 0x24
  38. #define L3G20_OUT_X_L 0x28
  39. #define L3G20_OUT_X_H 0x29
  40. #define L3G20_OUT_Y_L 0x2A
  41. #define L3G20_OUT_Y_H 0x2B
  42. #define L3G20_OUT_Z_L 0x2C
  43. #define L3G20_OUT_Z_H 0x2D
  44.  
  45.  
  46. namespace {
  47.     /* Initialize the debug USART */
  48.     auto usart_protected_init() -> void {
  49.         static isix::mutex m_mtx;
  50.         dblog_init_locked(
  51.                 [](int ch, void*) {
  52.                     return periph::drivers::uart_early::putc(ch);
  53.                 },
  54.                 nullptr,
  55.                 []() { m_mtx.lock();  },
  56.                 []() { m_mtx.unlock(); },
  57.                 periph::drivers::uart_early::open, "serial0", 115200
  58.         );
  59.     }
  60.  
  61.    
  62.  
  63.  
  64.     //Task for the led blinking
  65.     auto watch_sensor() -> void {
  66.         namespace opt = periph::option;
  67.         int ret {};
  68.         periph::drivers::spi_master spi("spi1");
  69.         do {
  70.             if((ret=spi.set_option(opt::speed(10E6)))<0) {
  71.                 break;
  72.             }
  73.             if((ret=spi.set_option(opt::polarity(opt::polarity::low)))<0) {
  74.                 break;
  75.             }
  76.             if((ret=spi.set_option(opt::phase(opt::phase::_1_edge)))<0) {
  77.                 break;
  78.             }
  79.             if((ret=spi.set_option(opt::dwidth(8)))<0) {
  80.                 break;
  81.             }
  82.             if((ret=spi.set_option(opt::bitorder(opt::bitorder::msb)))<0) {
  83.                 break;
  84.             }
  85.             if((ret=spi.open(ISIX_TIME_INFINITE))<0) {
  86.                 break;
  87.             }
  88.             //TODO: LAB4 user code here
  89.  
  90.  
  91. LL_GPIO_SetPinMode(GPIOD,LL_GPIO_PIN_12,LL_GPIO_MODE_OUTPUT);
  92. LL_GPIO_SetPinMode(GPIOD,LL_GPIO_PIN_13,LL_GPIO_MODE_OUTPUT);
  93. LL_GPIO_SetPinMode(GPIOD,LL_GPIO_PIN_14,LL_GPIO_MODE_OUTPUT);
  94. LL_GPIO_SetPinMode(GPIOD,LL_GPIO_PIN_15,LL_GPIO_MODE_OUTPUT);
  95.  
  96.  
  97.  
  98.             const uint8_t inbuf1[2]{ADDR_WHO_AM_I | DIR_READ};
  99.             uint8_t outbuf1[2]{};
  100.             static_assert(sizeof outbuf1 == sizeof inbuf1);
  101.             static constexpr auto csno = 0;    
  102.  
  103.             periph::blk::trx_transfer tran1(inbuf1,outbuf1,sizeof outbuf1);
  104.             int ret = spi.transaction(csno,tran1);
  105.  
  106.  dbg_info("outbuf[1] = %x\n",outbuf1[1]);
  107.             if (outbuf1[1] != 0xD4)
  108.             {
  109.                 dbg_info("Urzadzenie nie podclaczone \n");
  110.                 break;
  111.             }
  112.  
  113.         uint8_t inbuf2[2]{L3G20_CTRL_REG1 | DIR_READ};
  114.             uint8_t outbuf[2]{};
  115.             periph::blk::trx_transfer tran2(inbuf2,outbuf,sizeof outbuf1);
  116.             ret= spi.transaction(csno,tran2);
  117.             dbg_info("Odczytano reg1 = %x %x\n",outbuf[0],outbuf[1]);
  118.            
  119.             uint8_t inbuf3[2]{L3G20_CTRL_REG1 | 0x0F};
  120.             periph::blk::trx_transfer tran3(inbuf3,outbuf,sizeof outbuf);
  121.             ret= spi.transaction(csno,tran3);
  122.             dbg_info("Odczytano reg1 = %x %x\n",outbuf[0],outbuf[1]);          
  123.            
  124.             uint8_t inbuf4[2]{L3G20_CTRL_REG2 | 0x00};
  125.             periph::blk::trx_transfer tran4(inbuf4,outbuf,sizeof outbuf);
  126.             ret= spi.transaction(csno,tran4);
  127.  
  128.             uint8_t inbuf5[2]{L3G20_CTRL_REG3 | 0b00001000};
  129.             periph::blk::trx_transfer tran5(inbuf5,outbuf,sizeof outbuf);
  130.             ret= spi.transaction(csno,tran5);
  131.            
  132.             uint8_t inbuf6[2]{L3G20_CTRL_REG4 | 0b00001000};
  133.             periph::blk::trx_transfer tran6(inbuf6,outbuf,sizeof outbuf);
  134.             ret= spi.transaction(csno,tran6);  
  135.  
  136.             uint8_t inbuf7[2]{L3G20_CTRL_REG5 | 0x00};
  137.             periph::blk::trx_transfer tran7(inbuf7,outbuf,sizeof outbuf);
  138.             ret= spi.transaction(csno,tran7);  
  139.  
  140.  while(1)
  141.             {
  142.                 constexpr uint8_t inbuf9[7]={L3G20_OUT_X_L | DIR_READ | IADDR_INCREMENT};
  143.                 uint8_t outbuf9[7] {};
  144.                // enum {na,xla,xha,yla,yha,zla,zha};
  145.                 static_assert(sizeof outbuf9 == sizeof inbuf9);
  146.                 periph::blk::trx_transfer tran9(inbuf9,outbuf9,sizeof outbuf9);
  147.                 ret=spi.transaction(csno,tran9);
  148.                
  149.                 int16_t val_x=(int16_t(outbuf9[2]<<8|int16_t(outbuf9[1])));
  150.                 int16_t val_y=(int16_t(outbuf9[4]<<8|int16_t(outbuf9[3])));      
  151.                 int16_t val_z=(int16_t(outbuf9[6]<<8|int16_t(outbuf9[5])));  
  152.     dbg_info("L3G20 position : x = %i, y = %i, z = %i", val_x,val_y,val_z);
  153.  
  154. if (abs(val_x)>300) LL_GPIO_TogglePin(GPIOD,LL_GPIO_PIN_12); else LL_GPIO_ResetOutputPin(GPIOD,LL_GPIO_PIN_12);
  155. if (abs(val_y)>300) LL_GPIO_TogglePin(GPIOD,LL_GPIO_PIN_13); else LL_GPIO_ResetOutputPin(GPIOD,LL_GPIO_PIN_12);
  156. if (abs(val_z)>300) LL_GPIO_TogglePin(GPIOD,LL_GPIO_PIN_14); else LL_GPIO_ResetOutputPin(GPIOD,LL_GPIO_PIN_12);
  157.  
  158. isix::wait_ms(250);
  159.             }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.         } while(0);
  166.         dbg_info("Task exit with code %i", ret);
  167.     }
  168.  
  169.     }
  170.  
  171.  
  172. // Start main function
  173. auto main() -> int
  174. {
  175.     usart_protected_init();
  176.     // Wait some time before startup
  177.     isix::wait_ms(500);
  178.     static constexpr auto tsk_stack = 1024;
  179.     static constexpr auto tsk_prio = 6;
  180.     static auto watch_task =
  181.         isix::thread_create_and_run(tsk_stack,tsk_prio,0,watch_sensor);
  182.     dbprintf("<<<< LAB4 SPI MEMS sensor demo >>>>");
  183.     isix::start_scheduler();
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement