Advertisement
Guest User

spi test

a guest
Jun 4th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. uint8_t *txbuf;
  2. uint8_t *rxbuf;
  3.  
  4. void setup(void){
  5.     static uint8_t _txbuf[8] __attribute__ ((section (".sram2")));
  6.     static uint8_t _rxbuf[8] __attribute__ ((section (".sram2")));
  7.     txbuf = _txbuf;
  8.     rxbuf = _rxbuf;
  9.  
  10.     palSetPadMode(GPIOA,3,PAL_MODE_OUTPUT_PUSHPULL);    //POT
  11.     palWritePad(GPIOA,3,1);
  12.     palSetPadMode(GPIOA,2,PAL_MODE_OUTPUT_PUSHPULL);    //CV
  13.     palWritePad(GPIOA,2,1);
  14.    
  15.  
  16. }
  17.  
  18.  
  19. void loop(void){
  20.  
  21.     // first SPI block
  22.     txbuf[0] = 0xE0;
  23.     txbuf[1] = 0xFF;
  24.     txbuf[2] = 0x00;
  25.     txbuf[3] = 0x00;
  26.     txbuf[4] = 0x00;
  27.     txbuf[5] = 0x00;
  28.     txbuf[6] = 0xFF;
  29.    
  30.     //spiAcquireBus(&SPID1);
  31.  
  32.     palWritePad(GPIOA,3,0);
  33.     spiSelect(&SPID1);      // START SPI
  34.     //spiSend(&SPID1,4,txbuf);
  35.     //spiReceive(&SPID1,4,rxbuf);
  36.     spiExchange(&SPID1, 6, txbuf, rxbuf);
  37.     spiUnselect(&SPID1);
  38.     palWritePad(GPIOA,3,1);
  39.    
  40.     //spiReleaseBus(&SPID1);
  41.    
  42.     int z = (rxbuf[4] << 16| rxbuf[3] << 8| rxbuf[2] ) << 8;
  43.  
  44.     chThdSleep(1);
  45.     //chThdSleepMilliseconds(1);
  46.  
  47.     // secound SPI block
  48.     txbuf[0]= 0b01111111;
  49.  
  50.     spiSelect(&SPID1);
  51.     palWritePad(GPIOA,2,0);
  52.     //spiSend(&SPID1, 1, txbuf);
  53.     spiExchange(&SPID1, 1, txbuf, rxbuf);
  54.    
  55.     spiUnselect(&SPID1);
  56.     palWritePad(GPIOA,2,1);
  57.    
  58.     chThdSleep(1);
  59.     // third SPI block
  60.  
  61.     txbuf[0]= 0b01111111;
  62.  
  63.     spiSelect(&SPID1);
  64.     palWritePad(GPIOA,2,0);
  65.     spiSend(&SPID1, 1, txbuf);
  66.     //spiExchange(&SPID1, 1, txbuf, rxbuf);
  67.     spiUnselect(&SPID1);
  68.     palWritePad(GPIOA,2,1);
  69.  
  70.     chThdSleep(1);
  71.     //chThdSleepMilliseconds(1);
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement