Guest User

ACI Master Code

a guest
Oct 2nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include <hidef.h>          /* for EnableInterrupts macro */
  2. #include "derivative.h"     /* include peripheral declarations */
  3. #include "hcs08.h"            // This is our definition file!
  4.  
  5. //MC9S08QE8 pins
  6. #define ORANGELED PTAD_PTAD0
  7. #define WHITELED PTAD_PTAD1
  8. #define RESET PTAD_PTAD2
  9. #define REQN PTBD_PTBD5
  10. #define RDYN PTAD_PTAD3
  11.  
  12. //Temporary bytes for storing SPI registers
  13. char r_data = 0;
  14. char t_data = 0;
  15. char status = 0;
  16.  
  17. //Buffers for receiving and transmitting
  18. char r_buffer[32] = "";
  19. char t_buffer[32] = "";
  20.  
  21. //Crude Software Delay
  22. void myDelayer(int cycles) // > 50ns per cycle
  23. {
  24.     int i;
  25.     for(i = 0; i < cycles; i++)
  26.     {
  27.     }
  28. }
  29. //Reads from SPI registers
  30. char readACIByte()
  31. {
  32.     status = SPIS;
  33.     return SPID;
  34. }
  35. //Writes to SPI registers
  36. void writeACIByte(char data)
  37. {
  38.     status = SPIS;
  39.     SPID = data;
  40. }
  41.  
  42. //Interrupt that is triggered by the SPI receive register being full
  43. void interrupt VectorNumber_Vspi spi_receive_isr(void)
  44. {
  45.   ORANGELED = 1;            //toggles LED
  46.   r_data = readACIByte();   //reads data register into memory
  47.   ORANGELED = 0;
  48. }
  49.  
  50. //sends t_buffer via SPI
  51. void writeCycle()
  52. {
  53.     int i;
  54.    
  55.     while(RDYN == 0);       //requires RDYN to be high
  56.    
  57.     WHITELED = RDYN;
  58.  
  59.     REQN = 0;               //Sets REQN low
  60.    
  61.     while(RDYN == 1);       //waits for RDYN to be low
  62.    
  63.     for(i = 0; i < (t_buffer[0] + 1); i++)  //loops through length
  64.     {
  65.         writeACIByte(t_buffer[i]);          //writes byte to SPI Data register
  66.         r_buffer[i] = r_data;               //stores received reply in r_buffer
  67.     }
  68.        
  69.     REQN = 1;                               //sets REQN to be high
  70.    
  71.     while(RDYN == 0);                       //waits for RDYN to be high
  72. }
  73.  
  74. //reads sent SPI bytes into r_buffer
  75. void readCycle()
  76. {
  77.     int i;
  78.    
  79.     while(RDYN == 1);       //Requires RDYN to be low
  80.    
  81.     WHITELED = RDYN;
  82.    
  83.     REQN = 0;               //Sets REQN low
  84.    
  85.     writeACIByte(0x00);     //writes zeros to get first byte
  86.     r_buffer[0] = r_data;   //stores received data in memory
  87.     if(r_data == 0x00)      //if the debug byte is 0, check again
  88.     {
  89.         writeACIByte(0x00);
  90.         r_buffer[0] = r_data;
  91.     }
  92.            
  93.     writeACIByte(0x00);     //write a zero to receive the length byte
  94.     r_buffer[1] = r_data;   //store length byte
  95.    
  96.     for(i = 0; i < r_buffer[1]; i++)    //loop according to length
  97.     {
  98.         writeACIByte(0x00);             //write zero
  99.         r_buffer[i+2] = r_data;         //receive and store byte
  100.     }
  101.    
  102.     REQN = 1;                           //set REQN high again
  103.    
  104.     while(RDYN == 0);                   //wait for RDYN to be high
  105.    
  106. }
  107.  
  108. void readWriteCycle()       //read/write cycle first reads, then writes
  109. {
  110.     readCycle();
  111.     writeCycle();
  112. }
  113.  
  114. //Boots up the MCU
  115. void mcu_init(void)
  116. {
  117.       SOPT1 = bBKGDPE;
  118.      
  119.       //Set up bus clock = 20MHz
  120.       ICSC1 = 0b00000000;
  121.       ICSC2 = 0b00000000;        
  122.       ICSSC = 0b00000000;
  123.      
  124.       //Configures SPI as per data sheet
  125.       SPIC1 = 0b11010111;
  126.       SPIC2 = 0b00001000;
  127.       SPIBR = 0b00010000;       //Sets up SPI clock to 2Mhz
  128.        
  129.       PTADD_PTADD0 = 1;     //ORANGE LED is out
  130.       PTADD_PTADD1 = 1;     //WHITE LED is out
  131.       PTADD_PTADD2 = 1;     //RESET is out
  132.       PTBDD_PTBDD5 = 1;     //REQN is out
  133.       PTADD_PTADD3 = 0;     //RDYN is in
  134.      
  135.       REQN = 1;             //starts with REQN being high
  136.       RESET = 1;            //starts with RESET being high
  137.      
  138.       EnableInterrupts;     //enables interrupts
  139. }
  140.  
  141. //Resets the BLE module
  142. void ble_init(void)
  143. {
  144.     RESET = 0;          //sets RESET low
  145.     myDelayer(4);       //waits ~200 ns
  146.     RESET = 1;          //sets RESET high
  147.     myDelayer(20000);   //waits ~100 us
  148. }
  149.  
  150. void main(void)
  151. {
  152.     mcu_init();                 // Initialise the MCU
  153.     ble_init();                 // Reset the BLE
  154.    
  155.     //fill the transmit buffer with test command
  156.     t_buffer[0] = 0x02;
  157.     t_buffer[1] = 0x01;
  158.     t_buffer[2] = 0x02;
  159.            
  160.     readWriteCycle();
  161.    
  162.     //fill the transmit buffer with echo command
  163.     t_buffer[0] = 0x04;
  164.     t_buffer[1] = 0x02;
  165.     t_buffer[2] = 0x47;
  166.     t_buffer[3] = 0x48;
  167.     t_buffer[4] = 0x49;
  168.    
  169.     readWriteCycle();
  170.    
  171.     readCycle();
  172.    
  173.       while(1)
  174.       {
  175.           WHITELED = 0;
  176.           ORANGELED = 0;
  177.       }
  178. }
Add Comment
Please, Sign In to add comment