STM32-F746NG-DISCO Project.

Dependencies:   BSP_DISCO_F746NG

Committer:
chri721u
Date:
Thu Jan 09 13:39:12 2020 +0000
Revision:
3:1d1ced98a985
Parent:
2:f3bee2fcc2a0
Child:
4:875a2d0a996d
Changed input type from digital to interrupt and adjusted the relevant code. Added new thread. Added debugging comments, and checked for bugs. Most likely final version.

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 3:1d1ced98a985 5 Brief: STM32 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 3:1d1ced98a985 55 break;
chri721u 2:f3bee2fcc2a0 56 } else {
chri721u 3:1d1ced98a985 57 countup = 0;
chri721u 2:f3bee2fcc2a0 58 restarts++;
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 3:1d1ced98a985 67 CounterThread.start(&ninecounter); // Start multithread ninecounter & inputcounter
chri721u 3:1d1ced98a985 68 InputThread.start(&input);
chri721u 3:1d1ced98a985 69
chri721u 3:1d1ced98a985 70 button.rise(&buttonpress);
chri721u 3:1d1ced98a985 71 touch.rise(&touchpress);
chri721u 2:f3bee2fcc2a0 72
chri721u 3:1d1ced98a985 73 uint8_t text[30]; // Prepare text element for later print to LCD
chri721u 3:1d1ced98a985 74 button_count = 0; // Amount of times the button has been pressed
chri721u 3:1d1ced98a985 75 touch_count = 0; // Amount of times the touch sensor has been activated
chri721u 3:1d1ced98a985 76 countup = 0; // Default value for the counter
chri721u 3:1d1ced98a985 77 restarts = 0; // Amount of times 9999 counter has been reset
chri721u 1:0c13955ae7ae 78
chri721u 2:f3bee2fcc2a0 79 // Boot up LCD screen
chri721u 0:6b60860a3ffe 80 BSP_LCD_Init();
chri721u 0:6b60860a3ffe 81 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
chri721u 0:6b60860a3ffe 82 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
chri721u 3:1d1ced98a985 83
chri721u 0:6b60860a3ffe 84 // Clear the screen, add black background and red text
chri721u 0:6b60860a3ffe 85 BSP_LCD_Clear(LCD_COLOR_BLACK);
chri721u 0:6b60860a3ffe 86 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
chri721u 0:6b60860a3ffe 87 BSP_LCD_SetTextColor(LCD_COLOR_RED);
chri721u 3:1d1ced98a985 88
chri721u 0:6b60860a3ffe 89 // Bootup Message
chri721u 3:1d1ced98a985 90 printf("Device is booting up \r\n");
chri721u 3:1d1ced98a985 91 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "HELLDROP STUDIO", CENTER_MODE);
chri721u 3:1d1ced98a985 92 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "A big hard business", CENTER_MODE);
chri721u 3:1d1ced98a985 93 BSP_LCD_DisplayStringAt(0, 175, (uint8_t *) "in a big hard building", CENTER_MODE);
chri721u 3:1d1ced98a985 94 HAL_Delay(10000);
chri721u 2:f3bee2fcc2a0 95 // Change LCD Screen to system
chri721u 0:6b60860a3ffe 96 BSP_LCD_Clear(LCD_COLOR_BLACK);
chri721u 3:1d1ced98a985 97 printf("Device has started functioning \r\n");
chri721u 2:f3bee2fcc2a0 98 // Studio name & department
chri721u 2:f3bee2fcc2a0 99 BSP_LCD_DisplayStringAt(0, 25, (uint8_t *) "HELLDROP STUDIO SOFTWARE", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 100 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "Code by Christian Andresen", CENTER_MODE);
chri721u 0:6b60860a3ffe 101
chri721u 3:1d1ced98a985 102 // While there are less than 10 restarts of the counter..
chri721u 2:f3bee2fcc2a0 103 while(restarts < 10) {
chri721u 3:1d1ced98a985 104 // Combine string and integer value.
chri721u 3:1d1ced98a985 105 sprintf((char*)text, "Button count: %d", button_count);
chri721u 3:1d1ced98a985 106 // Print value to LCD screen
chri721u 3:1d1ced98a985 107 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *)&text, CENTER_MODE);
chri721u 3:1d1ced98a985 108 sprintf((char*)text, "Touch count: %d", touch_count);
chri721u 3:1d1ced98a985 109 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)&text, CENTER_MODE);
chri721u 3:1d1ced98a985 110 sprintf((char*)text, " %d (%d) ", countup, restarts);
chri721u 3:1d1ced98a985 111 BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)&text, CENTER_MODE);
chri721u 2:f3bee2fcc2a0 112 }
chri721u 3:1d1ced98a985 113 if (restarts == 10) { // If there are 10 restarts or more, trigger BSOD
chri721u 2:f3bee2fcc2a0 114 bluescreen();
chri721u 0:6b60860a3ffe 115 }
chri721u 0:6b60860a3ffe 116 }
chri721u 0:6b60860a3ffe 117
chri721u 0:6b60860a3ffe 118 void bluescreen()
chri721u 0:6b60860a3ffe 119 {
chri721u 0:6b60860a3ffe 120 //BLUE SCREEN OF DEATH
chri721u 3:1d1ced98a985 121 printf("FATAL ERROR. SYSTEM REQUIRES MANUAL REBOOT \r\n");
chri721u 0:6b60860a3ffe 122 BSP_LCD_Clear(LCD_COLOR_BLUE);
chri721u 0:6b60860a3ffe 123 BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
chri721u 0:6b60860a3ffe 124 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
chri721u 2:f3bee2fcc2a0 125 BSP_LCD_DisplayStringAt(0, 75, (uint8_t *) "This device has", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 126 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "encountered", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 127 BSP_LCD_DisplayStringAt(0, 125, (uint8_t *) "a fatal error", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 128 BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "and needs to reboot.", CENTER_MODE);
chri721u 2:f3bee2fcc2a0 129 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "HAL Error", LEFT_MODE);
chri721u 2:f3bee2fcc2a0 130 BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "ERROR CODE: 40", RIGHT_MODE);
chri721u 3:1d1ced98a985 131 }
chri721u 3:1d1ced98a985 132
chri721u 3:1d1ced98a985 133 void input()
chri721u 3:1d1ced98a985 134 {
chri721u 3:1d1ced98a985 135 HAL_Delay(10000);
chri721u 3:1d1ced98a985 136 while(1) {
chri721u 3:1d1ced98a985 137 if(buttonclick == true) {
chri721u 3:1d1ced98a985 138 printf("A button has been pressed. \r\n");
chri721u 3:1d1ced98a985 139 button_count++; // Add to button counter
chri721u 3:1d1ced98a985 140 led = 1; // Turn on LED
chri721u 3:1d1ced98a985 141 buzzer = 1; // Turn on buzzer
chri721u 3:1d1ced98a985 142 wait(0.5); // Prevent immediate shutdown
chri721u 3:1d1ced98a985 143 // Turn both off
chri721u 3:1d1ced98a985 144 led = 0;
chri721u 3:1d1ced98a985 145 buzzer = 0;
chri721u 3:1d1ced98a985 146 buttonclick = false; // End button click
chri721u 3:1d1ced98a985 147 } else if(touchclick == true) {
chri721u 3:1d1ced98a985 148 printf("Touch sensor has been activated \r\n");
chri721u 3:1d1ced98a985 149 touch_count++; // Add to touch counter on the LCD
chri721u 3:1d1ced98a985 150 led = 1;
chri721u 3:1d1ced98a985 151 wait(0.5);
chri721u 3:1d1ced98a985 152 led = 0;
chri721u 3:1d1ced98a985 153 touchclick = false; // End touch click
chri721u 3:1d1ced98a985 154 } else if (restarts == 10) {
chri721u 3:1d1ced98a985 155 break;
chri721u 3:1d1ced98a985 156 } else { // Failsafe if neither are turned off during click.
chri721u 3:1d1ced98a985 157 led = 0;
chri721u 3:1d1ced98a985 158 buzzer = 0;
chri721u 3:1d1ced98a985 159 }
chri721u 3:1d1ced98a985 160 }
chri721u 3:1d1ced98a985 161 }
chri721u 3:1d1ced98a985 162
chri721u 3:1d1ced98a985 163 void buttonpress()
chri721u 3:1d1ced98a985 164 {
chri721u 3:1d1ced98a985 165 buttonclick = true;
chri721u 3:1d1ced98a985 166 }
chri721u 3:1d1ced98a985 167
chri721u 3:1d1ced98a985 168 void touchpress()
chri721u 3:1d1ced98a985 169 {
chri721u 3:1d1ced98a985 170 touchclick = true;
chri721u 0:6b60860a3ffe 171 }