Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. /*
  2.  * main.cpp
  3.  *
  4.  *  Created on: 4 oct. 2017
  5.  *      Author: Duc Nguyen
  6.  */
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "inc/hw_types.h"
  10. #include "driverlib/sysctl.h"
  11. #include "driverlib/ssi.h"
  12. #include "driverlib/gpio.h"
  13. #include "driverlib/uart.h"
  14. #include "inc/hw_memmap.h"
  15. #include "inc/hw_types.h"
  16. #include "driverlib/pin_map.h"
  17. #include "cc1101_register_map.h"
  18. #include "utils/uartstdio.h"
  19.  
  20. void WriteReg(uint32_t addr, uint32_t value);
  21. int main()
  22. {
  23.  
  24.     SysCtlClockSet( SYSCTL_SYSDIV_2 | SYSCTL_USE_OSC | SYSCTL_XTAL_4MHZ | SYSCTL_OSC_MAIN); // set clock 4MHz
  25.     SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0); // enable modul SSI0;
  26.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // enable port A
  27.  
  28.     GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5); // config those pin as SSIO
  29.     SSIDisable(SSI0_BASE); // disable before configuration
  30.     GPIOPinConfigure(GPIO_PA2_SSI0CLK);
  31.     GPIOPinConfigure(GPIO_PA3_SSI0FSS);//configure those pin as SSI function
  32.     GPIOPinConfigure(GPIO_PA4_SSI0RX);// like changing them into alternate function
  33.     GPIOPinConfigure(GPIO_PA5_SSI0TX);
  34.  
  35.     SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0, SSI_MODE_MASTER, 1000000, 8);
  36.     // config clock and mode (CPHA & CPOL) and bit rate and the bandwidth
  37.     SSIEnable(SSI0_BASE);// enable the module
  38.     //Up here, finish the setup for SSI module...
  39.  
  40.     //start configuring UART for debug purpose
  41.     SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
  42.     GPIOPinConfigure(GPIO_PA0_U0RX);
  43.     GPIOPinConfigure(GPIO_PA1_U0TX);
  44.     GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  45.     UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
  46.     UARTStdioConfig(0, 115200, SysCtlClockGet());
  47. //    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200, UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE );
  48.     //finish configure UART to the computer
  49.     UARTCharPut(UART0_BASE,'a');
  50.     UARTCharPut(UART0_BASE,'H');
  51.     //now print one line as an example
  52.  
  53.     //config the CC1101 here
  54.     //
  55.     // Rf settings for CC1101
  56.     //
  57.     WriteReg(IOCFG0,0x06);  //GDO0 Output Pin Configuration
  58.     WriteReg(FIFOTHR,0x47); //RX FIFO and TX FIFO Thresholds
  59.     WriteReg(PKTCTRL0,0x05);//Packet Automation Control
  60.     WriteReg(CHANNR,0x01);  //Channel Number
  61.     WriteReg(FSCTRL1,0x06); //Frequency Synthesizer Control
  62.     WriteReg(FREQ2,0x10);   //Frequency Control Word, High Byte
  63.     WriteReg(FREQ1,0xA7);   //Frequency Control Word, Middle Byte
  64.     WriteReg(FREQ0,0x62);   //Frequency Control Word, Low Byte
  65.     WriteReg(MDMCFG4,0xFC); //Modem Configuration
  66.     WriteReg(MDMCFG2,0x13); //Modem Configuration
  67.     WriteReg(DEVIATN,0x15); //Modem Deviation Setting
  68.     WriteReg(MCSM0,0x18);   //Main Radio Control State Machine Configuration
  69.     WriteReg(FOCCFG,0x16);  //Frequency Offset Compensation Configuration
  70.     WriteReg(WORCTRL,0xFB); //Wake On Radio Control
  71.     WriteReg(FSCAL3,0xEA);  //Frequency Synthesizer Calibration
  72.     WriteReg(FSCAL2,0x2A);  //Frequency Synthesizer Calibration
  73.     WriteReg(FSCAL1,0x00);  //Frequency Synthesizer Calibration
  74.     WriteReg(FSCAL0,0x1F);  //Frequency Synthesizer Calibration
  75.     WriteReg(TEST2,0x81);   //Various Test Settings
  76.     WriteReg(TEST1,0x35);   //Various Test Settings
  77.     WriteReg(TEST0,0x09);   //Various Test Settings
  78.     // end config
  79.     while(1)
  80.     {
  81.         ;
  82.     }
  83. }
  84.  
  85. //this function
  86. // write value to address in the spi device, to configue par example
  87. void WriteReg(uint32_t addr, uint32_t value)
  88. {
  89.     uint8_t header_adr = (0x00 | addr); // set up the header
  90.     uint32_t dummy = 0x00;
  91.     GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0x00);// pull csn low
  92.  
  93.     // wait until MISO pin goes low
  94.     while ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_4 ));
  95.     //for debugging, enable UART
  96.     UARTprintf("Write addr: 0x%x, header: 0x%x\r\n", addr, header_adr);
  97.     while ( SSIBusy(SSI0_BASE)); //wait until ssi is free
  98.     SSIDataPut(SSI0_BASE, header_adr);
  99.     SSIDataGet(SSI0_BASE, &dummy);
  100.     UARTprintf("Readback status 0x%x\r\n",dummy & 0x00FF);
  101.     //wait until miso pin goes low
  102.     while (GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_4));
  103.     //when not busy, write back value to the register
  104.     UARTprintf("write val: 0x%x\r\n",value);
  105.     while( SSIBusy(SSI0_BASE));
  106.     SSIDataPut(SSI0_BASE, value); //get into the register, ready for transfer
  107.     SSIDataGet(SSI0_BASE, &dummy); // read dummy value;
  108.     UARTprintf("read back 2nd status 0x%x\r\n", dummy & 0x00FF);
  109.  
  110.     GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0x01); // PULL CSn high, stop writting;
  111.  
  112. }
  113.  
  114. void
  115. readReg(uint8_t raddr)
  116. {
  117.     uint8_t header_read = (0x80 | raddr);    //Set up header for reading
  118.     uint32_t dummy = 0x00;
  119.     //Pull the CSn low
  120.     GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0x00);
  121.  
  122.     //Wait until the MISO pin goes low
  123.     while (GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_4));
  124.  
  125.     //When not busy, write header for reading data from MISO Pin
  126.     UARTprintf("write address for read back: 0x%x\r\n",header_read);
  127.     while(SSIBusy(SSI0_BASE)){}                             //Wait for SSI to not be busy
  128.     SSIDataPut(SSI0_BASE, header_read);            //write header for reading data
  129.     SSIDataGet(SSI0_BASE, &dummy);                  //read status byte
  130.     UARTprintf("Status 0x%x ", dummy & 0x00FF);
  131.  
  132.     //Wait until the MISO pin goes low
  133.     while (GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_4));
  134.  
  135.     //When not busy, read data from MISO pin
  136.     while(SSIBusy(SSI0_BASE)){}                             //wait for SSI to be ready
  137.     SSIDataPut(SSI0_BASE, 0);                          //Write dummy value
  138.     SSIDataGet(SSI0_BASE, &dummy);                  //read dummy value
  139.     UARTprintf(" \t\t read back value 0x%x\r\n", dummy & 0x00FF);
  140.  
  141.     //Pull CSN high
  142.     GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0x1);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement