![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proj 324 Final
Fork of ELEC351_Group_T by
main.cpp@53:71f59e195f06, 2018-05-07 (annotated)
- Committer:
- thomasmorris
- Date:
- Mon May 07 15:44:34 2018 +0000
- Revision:
- 53:71f59e195f06
- Parent:
- 52:99915f5240b2
- Child:
- 54:a4c5949707ca
To test version of code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 53:71f59e195f06 | 1 | |
thomasmorris | 25:36699ed589ab | 2 | #include "SETUP.hpp" |
thomasmorris | 52:99915f5240b2 | 3 | |
thomasmorris | 53:71f59e195f06 | 4 | int SPI_RX_DATA = 0; |
thomasmorris | 53:71f59e195f06 | 5 | int Cubelet_Colours[9] = {0,0,0,0,0,0,0,0,0};//9 cubelets store colours here |
thomasmorris | 53:71f59e195f06 | 6 | int Received_data = 0; |
thomasmorris | 53:71f59e195f06 | 7 | bool Data_from_slave[16] = {0}; |
thomasmorris | 53:71f59e195f06 | 8 | |
thomasmorris | 52:99915f5240b2 | 9 | //Interrupt service routine for handling the timeout of SW1 |
thomasmorris | 52:99915f5240b2 | 10 | void SW1TimeOutHandler() { |
thomasmorris | 52:99915f5240b2 | 11 | SW1TimeOut.detach(); //Stop the timeout counter firing |
thomasmorris | 52:99915f5240b2 | 12 | SW1.fall(&SW1FallingEdge); //Now wait for a falling edge |
thomasmorris | 52:99915f5240b2 | 13 | } |
thomasmorris | 52:99915f5240b2 | 14 | //Interrupt service routive for SW2 falling edge (release) |
thomasmorris | 52:99915f5240b2 | 15 | void SW1FallingEdge() { |
thomasmorris | 52:99915f5240b2 | 16 | SW1.fall(NULL); //Disable this interrupt |
thomasmorris | 52:99915f5240b2 | 17 | SW1TimeOut.attach(&SW1TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter |
thomasmorris | 52:99915f5240b2 | 18 | } |
thomasmorris | 52:99915f5240b2 | 19 | //Interrupt service routine for handling the timeout of SW2 |
thomasmorris | 52:99915f5240b2 | 20 | void SW2TimeOutHandler() { |
thomasmorris | 52:99915f5240b2 | 21 | SW2TimeOut.detach(); //Stop the timeout counter firing |
thomasmorris | 52:99915f5240b2 | 22 | SW2.fall(&SW2FallingEdge); //Now wait for a falling edge |
thomasmorris | 52:99915f5240b2 | 23 | } |
thomasmorris | 52:99915f5240b2 | 24 | //Interrupt service routive for SW2 falling edge (release) |
thomasmorris | 52:99915f5240b2 | 25 | void SW2FallingEdge() { |
thomasmorris | 52:99915f5240b2 | 26 | SW2.fall(NULL); //Disable this interrupt |
thomasmorris | 52:99915f5240b2 | 27 | SW2TimeOut.attach(&SW2TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter |
thomasmorris | 52:99915f5240b2 | 28 | } |
thomasmorris | 47:6d128e500875 | 29 | void LCD_Output() |
thomasmorris | 30:4cde05cc7c4f | 30 | { |
thomasmorris | 30:4cde05cc7c4f | 31 | while(1) |
thomasmorris | 29:64b1f95a807c | 32 | { |
thomasmorris | 53:71f59e195f06 | 33 | //Write to the LCD |
thomasmorris | 7:dfe19413fdc2 | 34 | } |
thomasmorris | 7:dfe19413fdc2 | 35 | } |
thomasmorris | 47:6d128e500875 | 36 | void Serial_Commands() |
thomasmorris | 22:eb4cc12087b2 | 37 | { |
thomasmorris | 47:6d128e500875 | 38 | while(1) |
thomasmorris | 47:6d128e500875 | 39 | { |
thomasmorris | 52:99915f5240b2 | 40 | Serial_Commands_Output(); //Enable Serial Commands |
thomasmorris | 47:6d128e500875 | 41 | } |
thomasmorris | 22:eb4cc12087b2 | 42 | } |
thomasmorris | 48:244d6d81bb52 | 43 | void LED_Logging() |
thomasmorris | 48:244d6d81bb52 | 44 | { |
thomasmorris | 48:244d6d81bb52 | 45 | while(1) |
thomasmorris | 48:244d6d81bb52 | 46 | { |
thomasmorris | 52:99915f5240b2 | 47 | Log_Leds(); //Flashes the yellow led to indicate the logging mode |
thomasmorris | 48:244d6d81bb52 | 48 | } |
thomasmorris | 48:244d6d81bb52 | 49 | } |
thomasmorris | 53:71f59e195f06 | 50 | |
thomasmorris | 53:71f59e195f06 | 51 | void SPI_INTERFACE() |
thomasmorris | 53:71f59e195f06 | 52 | { |
thomasmorris | 53:71f59e195f06 | 53 | //pc.printf("SPI Test \n"); |
thomasmorris | 53:71f59e195f06 | 54 | Thread::wait(1000); |
thomasmorris | 53:71f59e195f06 | 55 | while(1) |
thomasmorris | 53:71f59e195f06 | 56 | { |
thomasmorris | 53:71f59e195f06 | 57 | //Do stuff |
thomasmorris | 53:71f59e195f06 | 58 | cs= 0; |
thomasmorris | 53:71f59e195f06 | 59 | SPI_RX_DATA = spi.write(0xCB); |
thomasmorris | 53:71f59e195f06 | 60 | wait_us(3); |
thomasmorris | 53:71f59e195f06 | 61 | cs= 1; |
thomasmorris | 53:71f59e195f06 | 62 | //SPI_RX_DATA = SPI_RX_DATA >> 7; |
thomasmorris | 53:71f59e195f06 | 63 | //SPI_RX_DATA = SPI_RX_DATA << 1; |
thomasmorris | 53:71f59e195f06 | 64 | //pc.printf("Received data : %x\n",SPI_RX_DATA); |
thomasmorris | 53:71f59e195f06 | 65 | |
thomasmorris | 53:71f59e195f06 | 66 | std::bitset<16> bits(SPI_RX_DATA); |
thomasmorris | 53:71f59e195f06 | 67 | cout << bits << endl; |
thomasmorris | 53:71f59e195f06 | 68 | pc.printf("Received data = %d\n", SPI_RX_DATA); |
thomasmorris | 53:71f59e195f06 | 69 | //wait_us(3); |
thomasmorris | 53:71f59e195f06 | 70 | Thread::wait(1000); |
thomasmorris | 53:71f59e195f06 | 71 | |
thomasmorris | 53:71f59e195f06 | 72 | } |
thomasmorris | 53:71f59e195f06 | 73 | } |
thomasmorris | 53:71f59e195f06 | 74 | void Motor_Control() |
thomasmorris | 5:2594b953f111 | 75 | { |
thomasmorris | 53:71f59e195f06 | 76 | while(1) |
thomasmorris | 53:71f59e195f06 | 77 | { |
thomasmorris | 53:71f59e195f06 | 78 | if(Motor_To_Select !=0 and steps !=0) |
thomasmorris | 53:71f59e195f06 | 79 | { |
thomasmorris | 53:71f59e195f06 | 80 | if(Motor_To_Select == 1) |
thomasmorris | 53:71f59e195f06 | 81 | { |
thomasmorris | 53:71f59e195f06 | 82 | STEPPER_MOTOR_1.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 83 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 84 | } |
thomasmorris | 53:71f59e195f06 | 85 | else if(Motor_To_Select == 2) |
thomasmorris | 53:71f59e195f06 | 86 | { |
thomasmorris | 53:71f59e195f06 | 87 | STEPPER_MOTOR_2.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 88 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 89 | } |
thomasmorris | 53:71f59e195f06 | 90 | else if(Motor_To_Select == 3) |
thomasmorris | 53:71f59e195f06 | 91 | { |
thomasmorris | 53:71f59e195f06 | 92 | STEPPER_MOTOR_3.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 93 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 94 | } |
thomasmorris | 53:71f59e195f06 | 95 | else if(Motor_To_Select == 4) |
thomasmorris | 53:71f59e195f06 | 96 | { |
thomasmorris | 53:71f59e195f06 | 97 | STEPPER_MOTOR_4.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 98 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 99 | } |
thomasmorris | 53:71f59e195f06 | 100 | else if(Motor_To_Select == 5) |
thomasmorris | 53:71f59e195f06 | 101 | { |
thomasmorris | 53:71f59e195f06 | 102 | STEPPER_MOTOR_5.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 103 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 104 | } |
thomasmorris | 53:71f59e195f06 | 105 | |
thomasmorris | 53:71f59e195f06 | 106 | else if(Motor_To_Select == 6) |
thomasmorris | 53:71f59e195f06 | 107 | { |
thomasmorris | 53:71f59e195f06 | 108 | STEPPER_MOTOR_6.Rotate_Steps(steps ,direction); |
thomasmorris | 53:71f59e195f06 | 109 | //Motor_To_Select = 0; |
thomasmorris | 53:71f59e195f06 | 110 | } |
thomasmorris | 53:71f59e195f06 | 111 | |
thomasmorris | 53:71f59e195f06 | 112 | } |
thomasmorris | 53:71f59e195f06 | 113 | } |
thomasmorris | 25:36699ed589ab | 114 | } |
thomasmorris | 53:71f59e195f06 | 115 | |
thomasmorris | 25:36699ed589ab | 116 | int main() |
thomasmorris | 25:36699ed589ab | 117 | { |
thomasmorris | 52:99915f5240b2 | 118 | set_time(1515530152); //Sets time |
thomasmorris | 52:99915f5240b2 | 119 | pc.baud(9600); //Sets the Serial Comms Baud Rate |
thomasmorris | 53:71f59e195f06 | 120 | //SPI_INIT(); |
thomasmorris | 53:71f59e195f06 | 121 | cs = 1; //Active Low |
thomasmorris | 53:71f59e195f06 | 122 | |
thomasmorris | 53:71f59e195f06 | 123 | // Setup the spi for 8 bit data, high steady state clock, |
thomasmorris | 53:71f59e195f06 | 124 | // second edge capture, with a 1MHz clock rate |
thomasmorris | 53:71f59e195f06 | 125 | spi.format(16,1); // 8 Data bits phase 0 polarity 0 |
thomasmorris | 53:71f59e195f06 | 126 | spi.frequency(1000000);//Output clock frequency 1Mhz |
thomasmorris | 53:71f59e195f06 | 127 | //post(); //Power on Self Test |
thomasmorris | 25:36699ed589ab | 128 | |
thomasmorris | 52:99915f5240b2 | 129 | //Start Threads |
thomasmorris | 53:71f59e195f06 | 130 | t1.start(Motor_Control); |
thomasmorris | 53:71f59e195f06 | 131 | t2.start(SPI_INTERFACE); |
thomasmorris | 53:71f59e195f06 | 132 | t3.start(Serial_Commands); |
thomasmorris | 52:99915f5240b2 | 133 | //Interrupts |
thomasmorris | 52:99915f5240b2 | 134 | SW1.fall(&SW1FallingEdge); |
thomasmorris | 52:99915f5240b2 | 135 | SW2.fall(&SW2FallingEdge); |
thomasmorris | 52:99915f5240b2 | 136 | |
thomasmorris | 5:2594b953f111 | 137 | //Main thread ID |
thomasmorris | 5:2594b953f111 | 138 | idMain = osThreadGetId(); //CMSIS RTOS call |
thomasmorris | 25:36699ed589ab | 139 | |
thomasmorris | 53:71f59e195f06 | 140 | |
thomasmorris | 5:2594b953f111 | 141 | //Thread ID |
thomasmorris | 5:2594b953f111 | 142 | id1 = t1.gettid(); |
thomasmorris | 5:2594b953f111 | 143 | id2 = t2.gettid(); |
thomasmorris | 21:3c078c799caa | 144 | id3 = t3.gettid(); |
thomasmorris | 21:3c078c799caa | 145 | id4 = t4.gettid(); |
chills | 24:7d2da96e05ad | 146 | id5 = t5.gettid(); |
thomasmorris | 48:244d6d81bb52 | 147 | id6 = t6.gettid(); |
thomasmorris | 53:71f59e195f06 | 148 | |
thomasmorris | 48:244d6d81bb52 | 149 | while(true) |
thomasmorris | 48:244d6d81bb52 | 150 | { |
thomasmorris | 53:71f59e195f06 | 151 | //Do nothing main thread will sleep |
noutram | 0:36e89e3ed7c4 | 152 | } |
thomasmorris | 45:875f7e18a386 | 153 | } |