Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. /* Traffic Light Controller
  2. *
  3. * --- Code is best viewed with the tab size of 4. ---
  4. */
  5.  
  6. #include <system.h>
  7. #include <sys/alt_alarm.h>
  8. #include <sys/alt_irq.h>
  9. #include <altera_avalon_pio_regs.h>
  10. #include <alt_types.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. // A template for COMPSYS 303 Assignment 1
  17. //
  18. // NOTE: You do not need to use this! Feel free
  19. // to write your own code from scratch if you
  20. // want, this is purely an example
  21.  
  22. // FUNCTION PROTOTYPES
  23. // Timer ISRs
  24. alt_u32 tlc_timer_isr(void* context);
  25. alt_u32 camera_timer_isr(void* context);
  26.  
  27. // Misc
  28. // Others maybe added eg LEDs / UART
  29. void lcd_set_mode(unsigned int mode);
  30.  
  31. // TLC state machine functions
  32. void init_tlc(void);
  33. void simple_tlc(int* state);
  34. void pedestrian_tlc(int* state);
  35. void configurable_tlc(int* state);
  36. int config_tlc(int *tl_state);
  37. void camera_tlc(int* state);
  38.  
  39. // Button Inputs / Interrupts
  40. void buttons_driver(int* button);
  41. void handle_mode_button(unsigned int* taskid);
  42. void handle_vehicle_button(void);
  43. void init_buttons_pio(void);
  44. void NSEW_ped_isr(void* context, alt_u32 id);
  45.  
  46. // Red light Camera
  47. void clear_vehicle_detected(void);
  48. void vehicle_checked(void);
  49. int is_vehicle_detected(void);
  50. int is_vehicle_left(void);
  51.  
  52. // Configuration Functions
  53. int update_timeout(void);
  54. void config_isr(void* context, alt_u32 id);
  55. void buffer_timeout(unsigned int value);
  56. void timeout_data_handler(void);
  57.  
  58.  
  59. // CONSTANTS
  60. #define OPERATION_MODES 0x03 // number of operation modes (00 - 03 = 4 modes)
  61. #define CAMERA_TIMEOUT 2000 // timeout period of red light camera (in ms)
  62. #define TIMEOUT_NUM 6 // number of timeouts
  63. #define TIME_LEN 8 // buffer length for time digits
  64.  
  65.  
  66. FILE *lcd;
  67.  
  68. // USER DATA TYPES
  69. // Timeout buffer structure
  70. typedef struct {
  71. int index;
  72. unsigned int timeout[TIMEOUT_NUM];
  73. } TimeBuf;
  74.  
  75.  
  76. // GLOBAL VARIABLES
  77. static alt_alarm tlc_timer; // alarm used for traffic light timing
  78. static alt_alarm camera_timer; // alarm used for camera timing
  79.  
  80. // NOTE:
  81. // set contexts for ISRs to be volatile to avoid unwanted Compiler optimisation
  82. static volatile int tlc_timer_event = 0;
  83. static volatile int camera_timer_event = 0;
  84. static volatile int pedestrianNS = 0;
  85. static volatile int pedestrianEW = 0;
  86.  
  87. // 4 States of 'Detection':
  88. // Car Absent
  89. // Car Enters
  90. // Car is Detected running a Red
  91. // Car Leaves
  92. static int vehicle_detected = 0;
  93.  
  94. // Traffic light timeouts
  95. static unsigned int timeout[TIMEOUT_NUM] = {500, 6000, 2000, 500, 6000, 2000};
  96. static TimeBuf timeout_buf = { -1, {500, 6000, 2000, 500, 6000, 2000} };
  97.  
  98. // UART
  99. FILE* fp;
  100.  
  101. // Traffic light LED values
  102. //static unsigned char traffic_lights[TIMEOUT_NUM] = {0x90, 0x50, 0x30, 0x90, 0x88, 0x84};
  103. // NS RGY | EW RGY
  104. // NR,NG | NY,ER,EG,EY
  105. static unsigned char traffic_lights[TIMEOUT_NUM] = {0x24, 0x14, 0x0C, 0x24, 0x22, 0x21};
  106.  
  107. enum traffic_states {RR0, GR, YR, RR1, RG, RY};
  108.  
  109. static unsigned int mode = 0;
  110. // Process states: use -1 as initialization state
  111. static int proc_state[OPERATION_MODES + 1] = {-1, -1, -1, -1};
  112.  
  113. // Initialize the traffic light controller
  114. // for any / all modes
  115. void init_tlc(void)
  116. {
  117.  
  118. }
  119.  
  120.  
  121. /* DESCRIPTION: Writes the mode to the LCD screen
  122. * PARAMETER: mode - the current mode
  123. * RETURNS: none
  124. */
  125. void lcd_set_mode(unsigned int mode)
  126. {
  127. fprintf(lcd, "%d", mode);
  128. }
  129.  
  130. /* DESCRIPTION: Performs button-press detection and debouncing
  131. * PARAMETER: button - referenced argument to indicate the state of the button
  132. * RETURNS: none
  133. */
  134. void buttons_driver(int* button)
  135. {
  136. // Persistant state of 'buttons_driver'
  137. static int state = 0;
  138.  
  139. *button = 0; // no assumption is made on intial value of *button
  140. // Debounce state machine
  141. // call handle_mode_button()
  142. }
  143.  
  144.  
  145. /* DESCRIPTION: Updates the ID of the task to be executed and the 7-segment display
  146. * PARAMETER: taskid - current task ID
  147. * RETURNS: none
  148. */
  149. void handle_mode_button(unsigned int* taskid)
  150. {
  151. // Increment mode
  152. // Update Mode-display
  153. }
  154.  
  155.  
  156. /* DESCRIPTION: Simple traffic light controller
  157. * PARAMETER: state - state of the controller
  158. * RETURNS: none
  159. */
  160. void simple_tlc(int* state)
  161. {
  162. if (*state == -1) {
  163. // Process initialization state
  164. init_tlc();
  165. (*state)++;
  166. return;
  167. }
  168.  
  169. // If the timeout has occured
  170. /*
  171. // Increase state number (within bounds)
  172. // Restart timer with new timeout value
  173. */
  174. }
  175.  
  176.  
  177. /* DESCRIPTION: Handles the traffic light timer interrupt
  178. * PARAMETER: context - opaque reference to user data
  179. * RETURNS: Number of 'ticks' until the next timer interrupt. A return value
  180. * of zero stops the timer.
  181. */
  182. alt_u32 tlc_timer_isr(void* context)
  183. {
  184. volatile int* trigger = (volatile int*)context;
  185. *trigger = 1;
  186. return 0;
  187. }
  188.  
  189.  
  190. /* DESCRIPTION: Initialize the interrupts for all buttons
  191. * PARAMETER: none
  192. * RETURNS: none
  193. */
  194. void init_buttons_pio(void)
  195. {
  196. // Initialize NS/EW pedestrian button
  197. // Reset the edge capture register
  198.  
  199. }
  200.  
  201.  
  202. /* DESCRIPTION: Pedestrian traffic light controller
  203. * PARAMETER: state - state of the controller
  204. * RETURNS: none
  205. */
  206. void pedestrian_tlc(int* state)
  207. {
  208. if (*state == -1) {
  209. // Process initialization state
  210. init_tlc();
  211. (*state)++;
  212. return;
  213. }
  214.  
  215. // Same as simple TLC
  216. // with additional states / signals for Pedestrian crossings
  217.  
  218.  
  219. }
  220.  
  221.  
  222. /* DESCRIPTION: Handles the NSEW pedestrian button interrupt
  223. * PARAMETER: context - opaque reference to user data
  224. * id - hardware interrupt number for the device
  225. * RETURNS: none
  226. */
  227. void NSEW_ped_isr(void* context, alt_u32 id)
  228. {
  229. // NOTE:
  230. // Cast context to volatile to avoid unwanted compiler optimization.
  231. // Store the value in the Button's edge capture register in *context
  232.  
  233.  
  234. }
  235.  
  236.  
  237. /* DESCRIPTION: Configurable traffic light controller
  238. * PARAMETER: state - state of the controller
  239. * RETURNS: none
  240. */
  241. /*
  242. If there is new configuration data... Load it.
  243. Else run pedestrian_tlc();
  244. */
  245. void configurable_tlc(int* state)
  246. {
  247. if (*state == -1) {
  248. // Process initialization state
  249. return;
  250. }
  251.  
  252.  
  253. }
  254.  
  255.  
  256. /* DESCRIPTION: Implements the state machine of the traffic light controller in
  257. * the ***configuration*** phase
  258. * PARAMETER: tl_state - state of the traffic light
  259. * RETURNS: Returns the state of the configuration phase
  260. */
  261. /*
  262. Puts the TLC in a 'safe' state... then begins update
  263. */
  264. int config_tlc(int* tl_state)
  265. {
  266. // State of configuration
  267. static int state = 0;
  268.  
  269. if (*tl_state == -1) {
  270. // Process initialization state
  271. state = 0;
  272. return 0;
  273. }
  274.  
  275. return state;
  276. }
  277.  
  278.  
  279. /* DESCRIPTION: Parses the configuration string and updates the timeouts
  280. * PARAMETER: none
  281. * RETURNS: none
  282. */
  283. /*
  284. buffer_timeout() must be used 'for atomic transfer to the main timeout buffer'
  285. */
  286. void timeout_data_handler(void)
  287. {
  288.  
  289. }
  290.  
  291.  
  292. /* DESCRIPTION: Stores the new timeout values in a secondary buffer for atomic
  293. * transfer to the main timeout buffer at a later stage
  294. * PARAMETER: value - value to store in the buffer
  295. * RETURNS: none
  296. */
  297. void buffer_timeout(unsigned int value)
  298. {
  299.  
  300. }
  301.  
  302.  
  303. /* DESCRIPTION: Implements the update operation of timeout values as a critical
  304. * section by ensuring that timeouts are fully received before
  305. * allowing the update
  306. * PARAMETER: none
  307. * RETURNS: 1 if update is completed; 0 otherwise
  308. */
  309. int update_timeout(void)
  310. {
  311.  
  312. }
  313.  
  314. /* DESCRIPTION: Handles the red light camera timer interrupt
  315. * PARAMETER: context - opaque reference to user data
  316. * RETURNS: Number of 'ticks' until the next timer interrupt. A return value
  317. * of zero stops the timer.
  318. */
  319. alt_u32 camera_timer_isr(void* context)
  320. {
  321. volatile int* trigger = (volatile int*)context;
  322. *trigger = 1;
  323. return 0;
  324. }
  325.  
  326. /* DESCRIPTION: Camera traffic light controller
  327. * PARAMETER: state - state of the controller
  328. * RETURNS: none
  329. */
  330. /*
  331. Same functionality as configurable_tlc
  332. But also handles Red-light camera
  333. */
  334. void camera_tlc(int* state)
  335. {
  336. if (*state == -1) {
  337. configurable_tlc(state);
  338. return;
  339. }
  340.  
  341. }
  342.  
  343.  
  344. /* DESCRIPTION: Simulates the entry and exit of vehicles at the intersection
  345. * PARAMETER: none
  346. * RETURNS: none
  347. */
  348. void handle_vehicle_button(void)
  349. {
  350.  
  351. }
  352.  
  353. // set vehicle_detected to 'no vehicle' state
  354. void clear_vehicle_detected(void)
  355. {
  356. }
  357. // set vehicle_detected to 'checking' state
  358. void vehicle_checked(void)
  359. {
  360. }
  361. // return true or false if a vehicle has been detected
  362. int is_vehicle_detected(void)
  363. {
  364. }
  365. // return true or false if the vehicle has left the intersection yet
  366. int is_vehicle_left(void)
  367. {
  368. }
  369.  
  370.  
  371.  
  372.  
  373.  
  374. void main(void)
  375. {
  376.  
  377. int buttons = 0; // status of mode button
  378. lcd = fopen(LCD_NAME, "w");
  379. lcd_set_mode(0); // initialize lcd
  380. init_buttons_pio(); // initialize buttons
  381. while (1) {
  382. // Button detection & debouncing
  383.  
  384. // if Mode button pushed:
  385. // Set current TLC state to -1
  386. // handle_mode_button to change state & display
  387. // if Car button pushed...
  388. // handle_vehicle_button
  389.  
  390. // Execute the correct TLC
  391.  
  392.  
  393.  
  394.  
  395. int uiSwitchValue = 0;
  396. uiSwitchValue = IORD_ALTERA_AVALON_PIO_DATA(SWITCHES_BASE);
  397. printf("switch value: %d\n", uiSwitchValue);
  398.  
  399.  
  400.  
  401.  
  402. switch (mode) {
  403. case 0:
  404. simple_tlc(&proc_state[0]);
  405. lcd_set_mode(mode);
  406.  
  407.  
  408.  
  409. break;
  410. case 1:
  411. pedestrian_tlc(&proc_state[1]);
  412. lcd_set_mode(mode);
  413.  
  414. break;
  415. case 2:
  416. configurable_tlc(&proc_state[2]);
  417. lcd_set_mode(mode);
  418.  
  419. break;
  420. case 3:
  421. camera_tlc(&proc_state[3]);
  422. lcd_set_mode(mode);
  423.  
  424. break;
  425. }
  426. // Update Displays
  427. }
  428. //return 1;
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement