Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <stdio.h>
  4. #include <avr/interrupt.h>
  5.  
  6. #include "lcd.h"
  7. #include "graphics.h"
  8. #include "cpu_speed.h"
  9.  
  10. #include "usb_serial.h"
  11.  
  12. /**
  13. * This define controls whether button pins are initialised for the old or new
  14. * Teensy. You must change this based on which version you have!
  15. */
  16. #define AM_I_OLD 1
  17.  
  18. /**
  19. * Defines used in system time calculations
  20. */
  21. #define FREQUENCY 8000000.0
  22. #define PRESCALER_0 1024.0
  23. #define PRESCALER_1 1024.0
  24.  
  25. /**
  26. * Default buffer length
  27. */
  28. #define BUFF_LENGTH 20
  29.  
  30. /**
  31. * Global overflow count for TIMER1 - keeps time increasing rather than wrapping
  32. */
  33. volatile unsigned long ovf_count = 0;
  34. int pause = 0;
  35. /**
  36. * Function implementations
  37. */
  38. // Series of functions used in debugging
  39. void send_debug_string(char* string);
  40. float get_system_time(void);
  41. void enter_breakpoint(unsigned long line_num);
  42.  
  43. // Other helper functions
  44. void init_hardware(void);
  45. void draw_centred(unsigned char y, char* string);
  46. void send_line(char* string);
  47. void recv_line(char* buff, unsigned char max_length);
  48.  
  49. /**
  50. * Main - Run the main program which prints the system time and flashes the LEDs
  51. * if certain conditions are met
  52. */
  53. int main() {
  54. // Setup the hardware
  55. set_clock_speed(CPU_8MHz);
  56. init_hardware();
  57.  
  58. // Wait until the 'debugger' is attached...
  59. draw_centred(17, "Waiting for");
  60. draw_centred(24, "debugger...");
  61. show_screen();
  62. while(!usb_configured() || !usb_serial_get_control());
  63. if(usb_configured() && usb_serial_get_control()) {
  64. send_line("Debugger initialised. Debugging strings will appear below:");
  65. send_debug_string("Entering main loop...");
  66. }
  67. // Run the main loop displaying the system time @ ~10Hz...
  68. char buff[BUFF_LENGTH];
  69. unsigned long count = 0;
  70. while (1) {
  71. // Draw the current system time on the screen
  72. clear_screen();
  73. sprintf(buff, "%7.4f", get_system_time());
  74. draw_centred(21, buff);
  75. send_debug_string("Calling show_screen()...");
  76. show_screen();
  77. send_debug_string("Finished show_screen()...");
  78. _delay_ms(100);
  79.  
  80. // Toggle LEDs if the conditions are met
  81. if ((count % 25) == 0) {
  82. PORTB ^= (1 << PB2);
  83. send_debug_string("LED0 was toggled.");
  84. }
  85. if ((count % 50) == 10) {
  86. PORTB ^= (1 << PB3);
  87. send_debug_string("LED1 was toggled.");
  88. }
  89.  
  90. // Increment the loop count
  91. count++;
  92. enter_breakpoint(__LINE__);
  93. while(pause == 1) {
  94. break;
  95. }
  96. }
  97.  
  98. // We'll never get here...
  99. return 0;
  100. }
  101. char debug[25];
  102. /**
  103. * Function implementations
  104. */
  105. void send_debug_string(char* string) {
  106. // Send the debug preamble...
  107. sprintf(debug, "[DBG @ %3.2f] ", get_system_time());
  108. usb_serial_write(debug,20);
  109.  
  110. // Send all of the characters in the string
  111. unsigned char char_count = 0;
  112. while (*string != '\0') {
  113. usb_serial_putchar(*string);
  114. string++;
  115. char_count++;
  116. }
  117.  
  118. // Go to a new line (force this to be the start of the line)
  119. usb_serial_putchar('\r');
  120. usb_serial_putchar('\n');
  121. }
  122.  
  123. float get_system_time(void) {
  124. return (ovf_count * 65536 + TCNT1) * PRESCALER_1 / FREQUENCY;
  125. }
  126. char line[25];
  127. void enter_breakpoint(unsigned long line_num) {
  128. // Implement this so that it correctly pauses the code and displays the message
  129. // over serial. NOTHING except the serial connection and the incrementing of
  130. // ovf_count should still be happening.
  131. // TODO
  132. sprintf(line, "Pause at line %i", line_num);
  133. send_debug_string(line);
  134. pause = 1;
  135. }
  136.  
  137. void init_hardware(void) {
  138. // Initialising the LCD screen
  139. LCDInitialise(LCD_DEFAULT_CONTRAST);
  140.  
  141. // Initalising the buttons as inputs (old and new)
  142. if (AM_I_OLD) {
  143. DDRB &= ~((1 << PB0) | (1 << PB1));
  144. } else {
  145. DDRF &= ~((1 << PF5) | (1 << PF6));
  146. }
  147.  
  148. // Initialising the LEDs as outputs
  149. DDRB |= ((1 << PB2) | (1 << PB3));
  150.  
  151. // Initialise the USB serial
  152. usb_init();
  153.  
  154. // Setup two timers with overflow interrupts:
  155. // 0 - button press checking @ ~30Hz
  156. // 1 - system time overflowing every 8.3s
  157. TCCR0B |= ((1 << CS02) | (1 << CS00));
  158. TCCR1B |= ((1 << CS12) | (1 << CS10));
  159. TIMSK0 |= (1 << TOIE0);
  160. TIMSK1 |= (1 << TOIE1);
  161.  
  162. // Enable interrupts globally
  163. sei();
  164. }
  165.  
  166. void draw_centred(unsigned char y, char* string) {
  167. // Draw a string centred in the LCD when you don't know the string length
  168. unsigned char l = 0, i = 0;
  169. while (string[i] != '\0') {
  170. l++;
  171. i++;
  172. }
  173. char x = 42-(l*5/2);
  174. draw_string((x > 0) ? x : 0, y, string);
  175. }
  176.  
  177. void send_line(char* string) {
  178. // Send all of the characters in the string
  179. unsigned char char_count = 0;
  180. while (*string != '\0') {
  181. usb_serial_putchar(*string);
  182. string++;
  183. char_count++;
  184. }
  185.  
  186. // Go to a new line (force this to be the start of the line)
  187. usb_serial_putchar('\r');
  188. usb_serial_putchar('\n');
  189. }
  190.  
  191. void recv_line(char* buff, unsigned char max_length) {
  192. // Loop over storing characters until the buffer is full or a newline character is received
  193. unsigned char char_count = 0;
  194. int16_t curr_char;
  195. do {
  196. // BLOCK here until a character is received
  197. do {
  198. curr_char = usb_serial_getchar();
  199. } while (curr_char == -1);
  200.  
  201. // Add to the buffer if it wasn't a newline (accepts other gibberish that may not necessarily be a character!)
  202. if (curr_char != '\n' && curr_char != '\r') {
  203. buff[char_count] = curr_char;
  204. char_count++;
  205. }
  206. } while (curr_char != '\n' && curr_char != '\r' && char_count < max_length - 1);
  207.  
  208. // Add the null terminator to the end of the string
  209. buff[char_count] = '\0';
  210. }
  211.  
  212. /**
  213. * Interrupt service routines
  214. */
  215. ISR(TIMER0_OVF_vect) {
  216. // De-bounced button press checking (gross bits are just for handling whether
  217. // you are on an old or new Teensy)
  218. if (((AM_I_OLD) ? PINB>>PB0 : PINF>>PF5) & 1) {
  219. _delay_ms(50);
  220. pause = 0;
  221. send_debug_string("Press of SW0 Detected.");
  222. while(((AM_I_OLD) ? PINB>>PB0 : PINF>>PF5) & 1);
  223. }
  224. }
  225.  
  226. ISR(TIMER1_OVF_vect) {
  227. ovf_count++;
  228. if(ovf_count > 0 && pause == 0) {
  229. send_debug_string("TIMER1 overflowed");
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement