STM32-F746NG-DISCO Project.

Dependencies:   BSP_DISCO_F746NG

Committer:
chri721u
Date:
Fri Jan 10 08:41:38 2020 +0000
Revision:
5:f42b50713a12
Parent:
4:875a2d0a996d
Final version with added documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chri721u 0:6b60860a3ffe 1 /*
chri721u 0:6b60860a3ffe 2 Author: Christian Andresen
chri721u 1:0c13955ae7ae 3 Website: xcha11.skp-dp.sde.dk
chri721u 2:f3bee2fcc2a0 4 Date: 10-01-2020
chri721u 5:f42b50713a12 5 Brief: STM32-F746NG-DISCO Project
chri721u 3:1d1ced98a985 6 Project includes:
chri721u 3:1d1ced98a985 7 Button/Touch sensor input, connected to LED lights and grove buzzer.
chri721u 3:1d1ced98a985 8 Infinite counter of 0-9999. Resets automatically.
chri721u 3:1d1ced98a985 9 Blue screen of death
chri721u 3:1d1ced98a985 10 Multithreading
chri721u 3:1d1ced98a985 11 Debug statements located within vital processes, for easy checking over console.
chri721u 0:6b60860a3ffe 12
chri721u 0:6b60860a3ffe 13 */
chri721u 2:f3bee2fcc2a0 14 #include "mbed.h" // mbed-os 5
chri721u 2:f3bee2fcc2a0 15 #include "stm32746g_discovery_lcd.h" // Last updated 27/11/2019
chri721u 2:f3bee2fcc2a0 16
chri721u 3:1d1ced98a985 17 // D2-D4: Input, D5-D8: Output.
chri721u 3:1d1ced98a985 18 InterruptIn button(D2);
chri721u 3:1d1ced98a985 19 InterruptIn touch(D3);
chri721u 0:6b60860a3ffe 20 DigitalOut led(D5);
chri721u 0:6b60860a3ffe 21 DigitalOut buzzer(D6);
chri721u 0:6b60860a3ffe 22
chri721u 2:f3bee2fcc2a0 23 // Multithreading
chri721u 3:1d1ced98a985 24 Thread CounterThread;
chri721u 3:1d1ced98a985 25 Thread InputThread;
chri721u 2:f3bee2fcc2a0 26
chri721u 3:1d1ced98a985 27 // Prototypes
chri721u 0:6b60860a3ffe 28 void bluescreen();
chri721u 3:1d1ced98a985 29 void input();
chri721u 3:1d1ced98a985 30 void buttonpress();
chri721u 3:1d1ced98a985 31 void touchpress();
chri721u 3:1d1ced98a985 32
chri721u 3:1d1ced98a985 33 // Button click counter, button-press bool, and determinator.
chri721u 0:6b60860a3ffe 34 int button_count;
chri721u 3:1d1ced98a985 35 bool buttonclick = false;
chri721u 3:1d1ced98a985 36
chri721u 3:1d1ced98a985 37 // Touch count, touch-sensor bool, and determinator
chri721u 0:6b60860a3ffe 38 int touch_count;
chri721u 3:1d1ced98a985 39 bool touchclick = false;
chri721u 3:1d1ced98a985 40
chri721u 3:1d1ced98a985 41 // Integers used for the 9999 counter.
chri721u 2:f3bee2fcc2a0 42 int countup;
chri721u 2:f3bee2fcc2a0 43 int restarts;
chri721u 0:6b60860a3ffe 44
chri721u 2:f3bee2fcc2a0 45 // 9999 counter program. Added above main for multithread ability.
chri721u 2:f3bee2fcc2a0 46 void ninecounter()
chri721u 3:1d1ced98a985 47 {
chri721u 3:1d1ced98a985 48 HAL_Delay(10000);
chri721u 2:f3bee2fcc2a0 49 while(1) {
chri721u 2:f3bee2fcc2a0 50 if (countup != 9999) {
chri721u 2:f3bee2fcc2a0 51 wait(0.001);
chri721u 2:f3bee2fcc2a0 52 countup++;
chri721u 2:f3bee2fcc2a0 53
chri721u 3:1d1ced98a985 54 } else if (restarts == 10) {
chri721u 5:f42b50713a12 55 break; // Once BSOD hits, the break statement will end the 9999 counter to simulate proper system shutdown.
chri721u 2:f3bee2fcc2a0 56 } else {
chri721u 5:f42b50713a12 57 countup = 0; // Reset 9999 counter
chri721u 5:f42b50713a12 58 restarts++; // Add to restart
chri721u 3:1d1ced98a985 59 printf("Counter has finished counting. Reset nr. %d. \r\n", restarts);
chri721u 2:f3bee2fcc2a0 60 }
chri721u 2:f3bee2fcc2a0 61 }
chri721u 2:f3bee2fcc2a0 62 }
chri721u 2:f3bee2fcc2a0 63
chri721u 0:6b60860a3ffe 64 // Main part of the program
chri721u 0:6b60860a3ffe 65 int main()
chri721u 0:6b60860a3ffe 66 {
chri721u 5:f42b50713a12 67 // Start multithread ninecounter & inputcounter
chri721u 5:f42b50713a12 68 CounterThread.start(&ninecounter);
chri721u 3:1d1ced98a985 69 InputThread.start(&input);
chri721u 3:1d1ced98a985 70
chri721u 5:f42b50713a12 71 // Checks when the button is pushed down and when the touch sensor is activated
chri721u 3:1d1ced98a985 72 button.rise(&buttonpress);
chri721u 3:1d1ced98a985 73 touch.rise(&touchpress);
chri721u 2:f3bee2fcc2a0 74
chri721u 3:1d1ced98a985 75 uint8_t text[30]; // Prepare text element for later print to LCD
chri721u 3:1d1ced98a985 76 button_count = 0; // Amount of times the button has been pressed
chri721u 3:1d1ced98a985 77 touch_count = 0; // Amount of times the touch sensor has been activated
chri721u 3:1d1ced98a985 78 countup = 0; // Default value for the counter
chri721u 3:1d1ced98a985 79 restarts = 0; // Amount of times 9999 counter has been reset
chri721u 1:0c13955ae7ae 80
chri721u 2:f3bee2fcc2a0 81 // Boot up LCD screen
chri721u 5:f42b50713a12 82 BSP_LCD_Init(); // Starts up the LCD
chri721u 0:6b60860a3ffe 83 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
chri721u 0:6b60860a3ffe 84 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
chri721u 3:1d1ced98a985 85
chri721u 0:6b60860a3ffe 86 // Clear the screen, add black background and red text
chri721u 0:6b60860a3ffe 87 BSP_LCD_Clear(LCD_COLOR_BLACK);
chri721u 0:6b60860a3ffe 88 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
chri721u 0:6b60860a3ffe 89 BSP_LCD_SetTextColor(LCD_COLOR_RED);
chri721u 3:1d1ced98a985 90
chri721u 0:6b60860a3ffe 91 // Bootup Message
chri721u 5:f42b50713a12 92 printf("Device is booting up \r\n"); // Debug statement that lets you know that the device is booting.
chri721u 5:f42b50713a12 93 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "HELLDROP STUDIO", CENTER_MODE); // Company
chri721u 5:f42b50713a12 94 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "A big hard business", CENTER_MODE); // Tagline 1
chri721u 5:f42b50713a12 95 BSP_LCD_DisplayStringAt(0, 175, (uint8_t *) "in a big hard building", CENTER_MODE); // Tagline 2
chri721u 5:f42b50713a12 96 HAL_Delay(10000); // Delay 10 seconds to simulate actual bootup
chri721u 5:f42b50713a12 97
chri721u 2:f3bee2fcc2a0 98 // Change LCD Screen to system
chri721u 0:6b60860a3ffe 99 BSP_LCD_Clear(LCD_COLOR_BLACK);
chri721u 3:1d1ced98a985 100 printf("Device has started functioning \r\n");
chri721u 5:f42b50713a12 101
chri721u 5:f42b50713a12 102 BSP_LCD_DisplayStringAt(0, 25, (uint8_t *) "HELLDROP STUDIO SOFTWARE", CENTER_MODE); // Studio name & department
chri721u 5:f42b50713a12 103 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "Code by Christian Andresen", CENTER_MODE); // Developer credit
chri721u 0:6b60860a3ffe 104
chri721u 3:1d1ced98a985 105 // While there are less than 10 restarts of the counter..
chri721u 2:f3bee2fcc2a0 106 while(restarts < 10) {
chri721u 3:1d1ced98a985 107 // Combine string and integer value.
chri721u 3:1d1ced98a985 108 sprintf((char*)text, "Button count: %d", button_count);
chri721u 3:1d1ced98a985 109 // Print value to LCD screen
chri721u 3:1d1ced98a985 110 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *)&text, CENTER_MODE);
chri721u 3:1d1ced98a985 111 sprintf((char*)text, "Touch count: %d", touch_count);
chri721u 3:1d1ced98a985 112 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)&text, CENTER_MODE);
chri721u 3:1d1ced98a985 113 sprintf((char*)text, " %d (%d) ", countup, restarts);
chri721u 3:1d1ced98a985 114 BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)&text, CENTER_MODE);
chri721u 2:f3bee2fcc2a0 115 }
chri721u 3:1d1ced98a985 116 if (restarts == 10) { // If there are 10 restarts or more, trigger BSOD
chri721u 2:f3bee2fcc2a0 117 bluescreen();
chri721u 0:6b60860a3ffe 118 }
chri721u 0:6b60860a3ffe 119 }
chri721u 0:6b60860a3ffe 120
chri721u 0:6b60860a3ffe 121 void bluescreen()
chri721u 0:6b60860a3ffe 122 {
chri721u 0:6b60860a3ffe 123 //BLUE SCREEN OF DEATH
chri721u 3:1d1ced98a985 124 printf("FATAL ERROR. SYSTEM REQUIRES MANUAL REBOOT \r\n");
chri721u 0:6b60860a3ffe 125 BSP_LCD_Clear(LCD_COLOR_BLUE);
chri721u 0:6b60860a3ffe 126 BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
chri721u 0:6b60860a3ffe 127 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
chri721u 2:f3bee2fcc2a0 128 BSP_LCD_DisplayStringAt(0, 75, (uint8_t *) "This device has", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 129 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "encountered", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 130 BSP_LCD_DisplayStringAt(0, 125, (uint8_t *) "a fatal error", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 131 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "and needs to reboot.", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 132 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "HAL Error", LEFT_MODE);
chri721u 2:f3bee2fcc2a0 133 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "ERROR CODE: 40", RIGHT_MODE);
chri721u 3:1d1ced98a985 134 }
chri721u 3:1d1ced98a985 135
chri721u 3:1d1ced98a985 136 void input()
chri721u 3:1d1ced98a985 137 {
chri721u 3:1d1ced98a985 138 HAL_Delay(10000);
chri721u 3:1d1ced98a985 139 while(1) {
chri721u 3:1d1ced98a985 140 if(buttonclick == true) {
chri721u 3:1d1ced98a985 141 printf("A button has been pressed. \r\n");
chri721u 3:1d1ced98a985 142 button_count++; // Add to button counter
chri721u 3:1d1ced98a985 143 led = 1; // Turn on LED
chri721u 3:1d1ced98a985 144 buzzer = 1; // Turn on buzzer
chri721u 5:f42b50713a12 145 wait(0.1); // Prevent immediate shutdown
chri721u 3:1d1ced98a985 146 // Turn both off
chri721u 3:1d1ced98a985 147 led = 0;
chri721u 3:1d1ced98a985 148 buzzer = 0;
chri721u 3:1d1ced98a985 149 buttonclick = false; // End button click
chri721u 3:1d1ced98a985 150 } else if(touchclick == true) {
chri721u 3:1d1ced98a985 151 printf("Touch sensor has been activated \r\n");
chri721u 3:1d1ced98a985 152 touch_count++; // Add to touch counter on the LCD
chri721u 3:1d1ced98a985 153 led = 1;
chri721u 5:f42b50713a12 154 wait(0.1);
chri721u 3:1d1ced98a985 155 led = 0;
chri721u 3:1d1ced98a985 156 touchclick = false; // End touch click
chri721u 3:1d1ced98a985 157 } else if (restarts == 10) {
chri721u 5:f42b50713a12 158 break; // Once BSOD hits, the break statement will end the input counter to simulate proper system shutdown.
chri721u 3:1d1ced98a985 159 } else { // Failsafe if neither are turned off during click.
chri721u 3:1d1ced98a985 160 led = 0;
chri721u 3:1d1ced98a985 161 buzzer = 0;
chri721u 3:1d1ced98a985 162 }
chri721u 3:1d1ced98a985 163 }
chri721u 3:1d1ced98a985 164 }
chri721u 3:1d1ced98a985 165
chri721u 3:1d1ced98a985 166 void buttonpress()
chri721u 3:1d1ced98a985 167 {
chri721u 5:f42b50713a12 168 buttonclick = true; // Bool that activates the input() once it registers a button press
chri721u 3:1d1ced98a985 169 }
chri721u 3:1d1ced98a985 170
chri721u 3:1d1ced98a985 171 void touchpress()
chri721u 3:1d1ced98a985 172 {
chri721u 5:f42b50713a12 173 touchclick = true; // See Buttonpress() comments above
chri721u 0:6b60860a3ffe 174 }