SHARE
TWEET

Untitled

a guest Apr 19th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stm32f0xx.h"
  2. #include "stm32f0_discovery.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include "fifo.h"
  7.  
  8. #define UNK -1
  9. #define NON_INTR 0
  10. #define INTR 1
  11.  
  12. int __io_putchar(int ch);
  13. static int putchar_nonirq(int ch);
  14.  
  15. void step3(void);
  16. void step4(void);
  17. void step5(void);
  18.  
  19. static struct fifo input_fifo;  // input buffer
  20. static struct fifo output_fifo; // output buffer
  21. int interrupt_mode = UNK;   // which version of putchar/getchar to use.
  22. int echo_mode = 0;          // should we echo input characters?
  23. int ships[16][16];
  24. int player_1 = 1;
  25. char input[5];
  26.  
  27. //=============================================================================
  28. // This is a version of printf() that will disable interrupts for the
  29. // USART and write characters directly.  It is intended to handle fatal
  30. // exceptional conditions.
  31. // It's also an example of how to create a variadic function.
  32. /*static void safe_printf(const char *format, ...) {
  33.     va_list ap;
  34.     va_start(ap, format);
  35.     char buf[80];
  36.     int len = vsnprintf(buf, sizeof buf, format, ap);
  37.     int saved_txeie = USART1->CR1 & USART_CR1_TXEIE;
  38.     USART1->CR1 &= ~USART_CR1_TXEIE;
  39.     int x;
  40.     for(x=0; x<len; x++) {
  41.         putchar_nonirq(buf[x]);
  42.     }
  43.     USART1->CR1 |= saved_txeie;
  44.     va_end(ap);
  45. }
  46. */
  47. //=======================================================================
  48. // Simply write a string one char at a time.
  49. //=======================================================================
  50. static void putstr(const char *s) {
  51.     while(*s)
  52.         __io_putchar(*s++);
  53. }
  54.  
  55. //=======================================================================
  56. // Insert a character and echo it.
  57. // (or, if it's a backspace, remove a char and erase it from the line).
  58. // If echo_mode is turned off, just insert the character and get out.
  59. //=======================================================================
  60. static void insert_echo_char(char ch) {
  61.     if (ch == '\r')
  62.         ch = '\n';
  63.     if (!echo_mode) {
  64.         fifo_insert(&input_fifo, ch);
  65.         return;
  66.     }
  67.     if (ch == '\b' || ch == '\177') {
  68.         if (!fifo_empty(&input_fifo)) {
  69.             char tmp = fifo_uninsert(&input_fifo);
  70.             if (tmp == '\n')
  71.                 fifo_insert(&input_fifo, '\n');
  72.             else if (tmp < 32)
  73.                 putstr("\b\b  \b\b");
  74.             else
  75.                 putstr("\b \b");
  76.         }
  77.         return; // Don't put a backspace into buffer.
  78.     } else if (ch == '\n') {
  79.         __io_putchar('\n');
  80.     } else if (ch == 0){
  81.         putstr("^0");
  82.     } else if (ch == 28) {
  83.         putstr("^\\");
  84.     } else if (ch < 32) {
  85.         __io_putchar('^');
  86.         __io_putchar('A'-1+ch);
  87.     } else {
  88.         __io_putchar(ch);
  89.     }
  90.     fifo_insert(&input_fifo, ch);
  91. }
  92.  
  93.  
  94. //-----------------------------------------------------------------------------
  95. // Section 6.2
  96. //-----------------------------------------------------------------------------
  97. // This should should perform the following
  98. // 1) Enable clock to GPIO port A
  99. // 2) Configure PA9 and PA10 to alternate function to use a USART
  100. //    Note: Configure both MODER and AFRL registers
  101. // 3) Enable clock to the USART module, it is up to you to determine
  102. //    which RCC register to use
  103. // 4) Disable the USART module (hint UE bit in CR1)
  104. // 5) Configure USART for 8 bits, 1 stop bit and no parity bit
  105. // 6) Use 16x oversampling
  106. // 7) Configure for 115200 baud rate
  107. // 8) Enable the USART for both transmit and receive
  108. // 9) Enable the USART
  109. // 10) Wait for TEACK and REACK to be set by hardware in the ISR register
  110. // 11) Set the 'interrupt_mode' variable to NON_INTR
  111. void tty_init(void) {
  112.     // Disable buffers for stdio streams.  Otherwise, the first use of
  113.     // each stream will result in a *malloc* of 2K.  Not good.
  114.     setbuf(stdin,0);
  115.     setbuf(stdout,0);
  116.     setbuf(stderr,0);
  117.     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  118.     GPIOA->MODER |= GPIO_MODER_MODER9_1;
  119.     GPIOA->MODER &= ~GPIO_MODER_MODER9_0;
  120.     GPIOA->MODER |= GPIO_MODER_MODER10_1;
  121.     GPIOA->MODER &= ~GPIO_MODER_MODER10_0;
  122.     GPIOA->AFR[1] |= 0x00000110;
  123.     RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
  124.     USART1->CR1 &= ~(USART_CR1_UE);
  125.     USART1->CR1 &= ~(USART_CR1_OVER8);
  126.     USART1->CR2 &= ~(USART_CR2_STOP);
  127.     USART1->CR1 &= ~(USART_CR1_M);
  128.     USART1->CR1 &= ~(USART_CR1_PCE);
  129.     USART1->BRR = 417;
  130.     USART1->CR1 |= USART_CR1_TE;
  131.     USART1->CR1 |= USART_CR1_RE;
  132.     USART1->CR1 |= (USART_CR1_UE);
  133.     while (!(((USART1->ISR & USART_ISR_REACK) == USART_ISR_REACK) && ((USART1->ISR & USART_ISR_TEACK) == USART_ISR_TEACK)));
  134.     interrupt_mode = NON_INTR;
  135.     // Student code goes here...
  136.  
  137. }
  138.  
  139. //=======================================================================
  140. // Enable the USART RXNE interrupt.
  141. // Remember to enable the right bit in the NVIC registers
  142. //=======================================================================
  143. void enable_tty_irq(void) {
  144.     // Student code goes here...
  145.     USART1->CR1 |= USART_CR1_RXNEIE;
  146.     NVIC->ISER[0] |= 1 << USART1_IRQn;
  147.     interrupt_mode = INTR;
  148. }
  149.  
  150. //-----------------------------------------------------------------------------
  151. // Section 6.5
  152. //-----------------------------------------------------------------------------
  153. // See lab document for description
  154. //=======================================================================
  155. // IRQ invoked for USART1 activity.
  156. void USART1_IRQHandler(void) {
  157.     // Student code goes here...
  158.     if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) {
  159.         insert_echo_char(USART1->RDR & 0xff);
  160.     }
  161.     if((USART1->ISR & USART_ISR_TXE) == USART_ISR_TXE) {
  162.         if (fifo_empty(&output_fifo)) {
  163.             USART1->CR1 &= ~(USART_CR1_TXEIE);
  164.         }
  165.         else {
  166.             USART1->TDR = fifo_remove(&output_fifo);
  167.         }
  168.     }
  169.     //-----------------------------------------------------------------
  170.     // Leave this checking code here to make sure nothing bad happens.
  171.     if (USART1->ISR & (USART_ISR_RXNE|
  172.             USART_ISR_ORE|USART_ISR_NE|USART_ISR_FE|USART_ISR_PE)) {
  173.    //     safe_printf("Problem in USART1_IRQHandler: ISR = 0x%x\n", USART1->ISR);
  174.     }
  175. }
  176.  
  177. // See lab document for description
  178. static int getchar_irq(void) {
  179.     // Student code goes here...
  180.     while(!fifo_newline(&input_fifo)) {
  181.         asm("wfi");
  182.     }
  183.     return fifo_remove(&input_fifo);
  184. }
  185.  
  186. // See lab document for description
  187. static int putchar_irq(char ch) {
  188.     // Student code goes here...
  189.     while (fifo_full(&output_fifo)) {
  190.         asm("wfi");
  191.     }
  192.     if (ch == 10) {
  193.         fifo_insert(&output_fifo, '\r');
  194.     }
  195.     else {
  196.         fifo_insert(&output_fifo, ch);
  197.     }
  198.     if ((USART1->CR1 & USART_CR1_TXEIE) != USART_CR1_TXEIE) {
  199.         USART1->CR1 |= USART_CR1_TXEIE;
  200.         USART1_IRQHandler();
  201.     }
  202.     if (ch == 10) {
  203.         while (fifo_full(&output_fifo)) {
  204.             asm("wfi");
  205.         }
  206.         fifo_insert(&output_fifo, '\n');
  207.     }
  208. }
  209.  
  210. //=======================================================================
  211. // Called by the Standard Peripheral library for a write()
  212. int __io_putchar(int ch) {
  213.     if (interrupt_mode == INTR)
  214.         return putchar_irq(ch);
  215.  
  216. }
  217.  
  218. //=======================================================================
  219. // Called by the Standard Peripheral library for a read()
  220. int __io_getchar(void) {
  221.     // Choose the right implementation.
  222.     if (interrupt_mode == INTR)
  223.         return getchar_irq();
  224.  
  225. }
  226.  
  227. //-----------------------------------------------------------------------------
  228. // Section 6.6
  229. //-----------------------------------------------------------------------------
  230. //===========================================================================
  231. // Act on a command read by testbench().
  232. static void action(char **words) {
  233.     if (words[0] != 0) {
  234.         if (strcasecmp(words[0],"alpha") == 0) {
  235.             // Print the alphabet repeatedly until you press <Enter>.
  236.             char buf[81];
  237.             for(int x=0; x<80; x++)
  238.                 buf[x] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[x%26];
  239.             buf[80] = '\0';
  240.             echo_mode = 0;
  241.             for(;;) {
  242.                 putstr(buf);
  243.                 if (fifo_newline(&input_fifo)) {
  244.                     echo_mode = 1;
  245.                     return;
  246.                 }
  247.             }
  248.         }
  249.         if (strcasecmp(words[0],"init") == 0) {
  250.             if (strcasecmp(words[1],"lcd") == 0) {
  251.                 printf("lcd command not implemenented yet\n");
  252.                 return;
  253.             }
  254.         }
  255.  
  256.         if (strcasecmp(words[0],"green") == 0) {
  257.             if (strcasecmp(words[1],"on") == 0) {
  258.                 RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  259.                 GPIOC->MODER &= ~(3<<18);
  260.                 GPIOC->MODER |= 2<<18;
  261.                 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
  262.                 TIM3->CR1 &= ~TIM_CR1_DIR;
  263.                 TIM3->PSC = 11999;
  264.                 TIM3->ARR =  3999;
  265.                 TIM3->CCR3 = 3999;
  266.                 TIM3->CCMR2 &= ~TIM_CCMR2_OC4M_2;
  267.                 TIM3->CCMR2 |= TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_0;
  268.                 TIM3->CCER |= TIM_CCER_CC4E;
  269.                 TIM3->CR1 |= TIM_CR1_CEN;
  270.                // while (1)
  271.                //        asm("wfi");
  272.                 return;
  273.             }
  274.         }
  275.         if (strcasecmp(words[0],"green") == 0) {
  276.                     if (strcasecmp(words[1],"off") == 0) {
  277.                         RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  278.                         GPIOC->MODER &= ~(3<<18);
  279.                         GPIOC->MODER |= 1<<18;
  280.                         GPIOC->ODR &=  ~(1<<9);
  281.                         RCC->AHBENR &= ~RCC_AHBENR_GPIOCEN;
  282.                         TIM3->CR1 &= ~TIM_CR1_CEN;
  283.                         return;
  284.                     }
  285.                 }
  286.  
  287.         if (strcasecmp(words[0],"display1") == 0) {
  288.             printf("display1 command not implemented yet\n");
  289.             return;
  290.         }
  291.         if (strcasecmp(words[0],"display2") == 0) {
  292.             printf("display2 command not implemented yet\n");
  293.             return;
  294.         }
  295.  
  296.         printf("Unrecognized command: %s\n", words[0]);
  297.     }
  298. }
  299.  
  300. //===========================================================================
  301. // Interact with the hardware.
  302. // This subroutine waits for a line of input, breaks it apart into an
  303. // array of words, and passes that array of words to the action()
  304. // subroutine.
  305. // The "display1" and "display2" are special words that tell it to
  306. // keep everything after the first space together into words[1].
  307. //
  308. void testbench(void) {
  309.     printf("STM32 testbench.\n");
  310.     for(;;) {
  311.         char buf[60];
  312.         printf("> ");
  313.         fgets(buf, sizeof buf - 1, stdin);
  314.         int sz = strlen(buf);
  315.         if (sz > 0)
  316.             buf[sz-1] = '\0';
  317.         char *words[7] = { 0,0,0,0,0,0,0 };
  318.         int i;
  319.         char *cp = buf;
  320.         for(i=0; i<6; i++) {
  321.             // strtok tokenizes a string, splitting it up into words that
  322.             // are divided by any characters in the second argument.
  323.             words[i] = strtok(cp," \t");
  324.             // Once strtok() is initialized with the buffer,
  325.             // subsequent calls should be made with NULL.
  326.             cp = 0;
  327.             if (words[i] == 0)
  328.                 break;
  329.             if (i==0 && strcasecmp(words[0], "display1") == 0) {
  330.                 words[1] = strtok(cp, ""); // words[1] is rest of string
  331.                 break;
  332.             }
  333.             if (i==0 && strcasecmp(words[0], "display2") == 0) {
  334.                 words[1] = strtok(cp, ""); // words[1] is rest of string
  335.                 break;
  336.             }
  337.         }
  338.         action(words);
  339.     }
  340. }
  341.  
  342. void initilize_ships() {
  343.     for (int i =0; i < 16; i++) {
  344.         for (int j =0; j < 16; j++) {
  345.             ships[i][j] = 0;
  346.         }
  347.     }
  348.     for (int i =0; i <2; i++) {
  349.         ships[0][i] = 1;
  350.     }
  351.     for (int i =0; i <3; i++) {
  352.         ships[i+10][5] = 1;
  353.     }
  354.     for (int i =0; i <4; i++) {
  355.         ships[i][9] = 1;
  356.         ships[i][14] = 1;
  357.     }
  358.     for (int i =0; i <5; i++) {
  359.         ships[3][i+2] = 1;
  360.     }
  361.     return;
  362. }
  363.  
  364. //void TIM3_IRQHandler() {
  365.  
  366. //}
  367.  
  368. /*void game() {
  369.     if (player_1 == 1) { //this means stm 1 is going to be the first one going
  370.        for (int i=0; i < 4;i++) {
  371.  
  372.        }
  373.     }
  374. }
  375. */
  376.  
  377.  
  378. int main(void)
  379. {
  380.     tty_init();
  381.     enable_tty_irq();
  382.     //insert_echo_char('h');
  383.     //insert_echo_char('e');
  384.     //insert_echo_char('l');
  385.     //insert_echo_char('l');
  386.     //insert_echo_char('o');
  387.         getchar_irq();
  388.     for (int i=0; i < 5; i++) {
  389.     putchar_irq(getchar_irq());
  390.    }
  391.     putchar_irq('g');
  392.     for(;;)
  393.         asm("wfi");
  394.     return 0;
  395. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top