Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <hidef.h> /* for EnableInterrupts macro */
- #include "derivative.h" /* include peripheral declarations */
- #include "hcs08.h" // This is our definition file!
- //MC9S08QE8 pins
- #define ORANGELED PTAD_PTAD0
- #define WHITELED PTAD_PTAD1
- #define RESET PTAD_PTAD2
- #define REQN PTBD_PTBD5
- #define RDYN PTAD_PTAD3
- //Temporary bytes for storing SPI registers
- char r_data = 0;
- char t_data = 0;
- char status = 0;
- //Buffers for receiving and transmitting
- char r_buffer[32] = "";
- char t_buffer[32] = "";
- //Crude Software Delay
- void myDelayer(int cycles) // > 50ns per cycle
- {
- int i;
- for(i = 0; i < cycles; i++)
- {
- }
- }
- //Reads from SPI registers
- char readACIByte()
- {
- status = SPIS;
- return SPID;
- }
- //Writes to SPI registers
- void writeACIByte(char data)
- {
- status = SPIS;
- SPID = data;
- }
- //Interrupt that is triggered by the SPI receive register being full
- void interrupt VectorNumber_Vspi spi_receive_isr(void)
- {
- ORANGELED = 1; //toggles LED
- r_data = readACIByte(); //reads data register into memory
- ORANGELED = 0;
- }
- //sends t_buffer via SPI
- void writeCycle()
- {
- int i;
- while(RDYN == 0); //requires RDYN to be high
- WHITELED = RDYN;
- REQN = 0; //Sets REQN low
- while(RDYN == 1); //waits for RDYN to be low
- for(i = 0; i < (t_buffer[0] + 1); i++) //loops through length
- {
- writeACIByte(t_buffer[i]); //writes byte to SPI Data register
- r_buffer[i] = r_data; //stores received reply in r_buffer
- }
- REQN = 1; //sets REQN to be high
- while(RDYN == 0); //waits for RDYN to be high
- }
- //reads sent SPI bytes into r_buffer
- void readCycle()
- {
- int i;
- while(RDYN == 1); //Requires RDYN to be low
- WHITELED = RDYN;
- REQN = 0; //Sets REQN low
- writeACIByte(0x00); //writes zeros to get first byte
- r_buffer[0] = r_data; //stores received data in memory
- if(r_data == 0x00) //if the debug byte is 0, check again
- {
- writeACIByte(0x00);
- r_buffer[0] = r_data;
- }
- writeACIByte(0x00); //write a zero to receive the length byte
- r_buffer[1] = r_data; //store length byte
- for(i = 0; i < r_buffer[1]; i++) //loop according to length
- {
- writeACIByte(0x00); //write zero
- r_buffer[i+2] = r_data; //receive and store byte
- }
- REQN = 1; //set REQN high again
- while(RDYN == 0); //wait for RDYN to be high
- }
- void readWriteCycle() //read/write cycle first reads, then writes
- {
- readCycle();
- writeCycle();
- }
- //Boots up the MCU
- void mcu_init(void)
- {
- SOPT1 = bBKGDPE;
- //Set up bus clock = 20MHz
- ICSC1 = 0b00000000;
- ICSC2 = 0b00000000;
- ICSSC = 0b00000000;
- //Configures SPI as per data sheet
- SPIC1 = 0b11010111;
- SPIC2 = 0b00001000;
- SPIBR = 0b00010000; //Sets up SPI clock to 2Mhz
- PTADD_PTADD0 = 1; //ORANGE LED is out
- PTADD_PTADD1 = 1; //WHITE LED is out
- PTADD_PTADD2 = 1; //RESET is out
- PTBDD_PTBDD5 = 1; //REQN is out
- PTADD_PTADD3 = 0; //RDYN is in
- REQN = 1; //starts with REQN being high
- RESET = 1; //starts with RESET being high
- EnableInterrupts; //enables interrupts
- }
- //Resets the BLE module
- void ble_init(void)
- {
- RESET = 0; //sets RESET low
- myDelayer(4); //waits ~200 ns
- RESET = 1; //sets RESET high
- myDelayer(20000); //waits ~100 us
- }
- void main(void)
- {
- mcu_init(); // Initialise the MCU
- ble_init(); // Reset the BLE
- //fill the transmit buffer with test command
- t_buffer[0] = 0x02;
- t_buffer[1] = 0x01;
- t_buffer[2] = 0x02;
- readWriteCycle();
- //fill the transmit buffer with echo command
- t_buffer[0] = 0x04;
- t_buffer[1] = 0x02;
- t_buffer[2] = 0x47;
- t_buffer[3] = 0x48;
- t_buffer[4] = 0x49;
- readWriteCycle();
- readCycle();
- while(1)
- {
- WHITELED = 0;
- ORANGELED = 0;
- }
- }
Add Comment
Please, Sign In to add comment