Here, alternative functions and classes for STM32. The program contains a class for the I2C software bus, a class for working with a watchdog timer and time delay functions based on DWT. All functions and classes use the HAL library. Functions and classes were written for the microcontroller stm32f103.
Dependencies: Stm32F1xx_Alternative mbed
main.cpp@1:461d09240602, 2017-05-24 (annotated)
- Committer:
- Yar
- Date:
- Wed May 24 20:35:38 2017 +0000
- Revision:
- 1:461d09240602
- Parent:
- 0:a5f82a6b0d16
Fixed a bug with a delay of milliseconds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yar | 0:a5f82a6b0d16 | 1 | #include "mbed.h" |
Yar | 0:a5f82a6b0d16 | 2 | #include "Stm32F1xxAlternative.hpp" |
Yar | 0:a5f82a6b0d16 | 3 | |
Yar | 0:a5f82a6b0d16 | 4 | |
Yar | 0:a5f82a6b0d16 | 5 | DigitalOut myled(LED1); |
Yar | 0:a5f82a6b0d16 | 6 | |
Yar | 0:a5f82a6b0d16 | 7 | // Declare the legs of the microcontroller to work with the I2C software bus |
Yar | 0:a5f82a6b0d16 | 8 | #define _GPIO_Pin_9 (1<<9) // It is the 9 leg any port of the microcontroller |
Yar | 0:a5f82a6b0d16 | 9 | #define _GPIO_Pin_8 (1<<8) // It is the 8 leg any port of the microcontroller |
Yar | 0:a5f82a6b0d16 | 10 | // This is the class for working with the I2C software bus |
Yar | 0:a5f82a6b0d16 | 11 | // By default, the I2C frequency is 100 kHz |
Yar | 0:a5f82a6b0d16 | 12 | // In the example, the legs of the PC_9 microcontroller are used for SCL |
Yar | 0:a5f82a6b0d16 | 13 | // and PC_8 for SDA (SCL port, SDA, port, SCL pin, SDA pin) |
Yar | 0:a5f82a6b0d16 | 14 | SoftwareI2C _i2c(GPIOC, GPIOC, _GPIO_Pin_9, _GPIO_Pin_8); |
Yar | 0:a5f82a6b0d16 | 15 | |
Yar | 0:a5f82a6b0d16 | 16 | |
Yar | 0:a5f82a6b0d16 | 17 | // Class for watchdog timer |
Yar | 0:a5f82a6b0d16 | 18 | #if (1) |
Yar | 0:a5f82a6b0d16 | 19 | // You can initialize as follows: |
Yar | 0:a5f82a6b0d16 | 20 | WatchdogTimer Watchdog(10.0); |
Yar | 0:a5f82a6b0d16 | 21 | #else |
Yar | 0:a5f82a6b0d16 | 22 | // Or you can initialize this way: |
Yar | 0:a5f82a6b0d16 | 23 | WatchdogTimer Watchdog(); |
Yar | 0:a5f82a6b0d16 | 24 | #endif |
Yar | 0:a5f82a6b0d16 | 25 | |
Yar | 0:a5f82a6b0d16 | 26 | // So you can find out the status of the watchdog timer |
Yar | 0:a5f82a6b0d16 | 27 | void WatchdogStatus(void) { |
Yar | 0:a5f82a6b0d16 | 28 | switch (Watchdog.getStatus()) { |
Yar | 0:a5f82a6b0d16 | 29 | case WatchdogTimer::RESET: // IWDG not yet initialized or disabled |
Yar | 0:a5f82a6b0d16 | 30 | printf("IWDG not yet initialized or disabled\r\n"); |
Yar | 0:a5f82a6b0d16 | 31 | break; |
Yar | 0:a5f82a6b0d16 | 32 | case WatchdogTimer::READY: // IWDG initialized and ready for use |
Yar | 0:a5f82a6b0d16 | 33 | printf("IIWDG initialized and ready for use\r\n"); |
Yar | 0:a5f82a6b0d16 | 34 | break; |
Yar | 0:a5f82a6b0d16 | 35 | case WatchdogTimer::BUSY: // IWDG internal process is ongoing |
Yar | 0:a5f82a6b0d16 | 36 | printf("IWDG internal process is ongoing\r\n"); |
Yar | 0:a5f82a6b0d16 | 37 | break; |
Yar | 0:a5f82a6b0d16 | 38 | case WatchdogTimer::TIMEOUT: // IWDG timeout state |
Yar | 0:a5f82a6b0d16 | 39 | printf("IWDG timeout state\r\n"); |
Yar | 0:a5f82a6b0d16 | 40 | break; |
Yar | 0:a5f82a6b0d16 | 41 | case WatchdogTimer::ERROR: // IWDG error state |
Yar | 0:a5f82a6b0d16 | 42 | printf("IWDG error state\r\n"); |
Yar | 0:a5f82a6b0d16 | 43 | break; |
Yar | 0:a5f82a6b0d16 | 44 | default: |
Yar | 0:a5f82a6b0d16 | 45 | printf("Unknown state\r\n"); |
Yar | 0:a5f82a6b0d16 | 46 | break; |
Yar | 0:a5f82a6b0d16 | 47 | } |
Yar | 0:a5f82a6b0d16 | 48 | } |
Yar | 0:a5f82a6b0d16 | 49 | |
Yar | 0:a5f82a6b0d16 | 50 | |
Yar | 0:a5f82a6b0d16 | 51 | |
Yar | 0:a5f82a6b0d16 | 52 | |
Yar | 0:a5f82a6b0d16 | 53 | int main() { |
Yar | 0:a5f82a6b0d16 | 54 | #if (0) |
Yar | 0:a5f82a6b0d16 | 55 | // You can pre-initialize an alternative time delay |
Yar | 0:a5f82a6b0d16 | 56 | initializeTimeDelays(); |
Yar | 0:a5f82a6b0d16 | 57 | // Otherwise, initialization will be called automatically |
Yar | 0:a5f82a6b0d16 | 58 | // the first time you call the function delay_ms or delay_us |
Yar | 0:a5f82a6b0d16 | 59 | #endif |
Yar | 0:a5f82a6b0d16 | 60 | #if (0) |
Yar | 0:a5f82a6b0d16 | 61 | // You can re-initialize the watchdog |
Yar | 0:a5f82a6b0d16 | 62 | // The maximum time is 32 seconds |
Yar | 0:a5f82a6b0d16 | 63 | Watchdog.setResponseTime_s(10); |
Yar | 0:a5f82a6b0d16 | 64 | //Watchdog.setResponseTime_us(10000000); |
Yar | 0:a5f82a6b0d16 | 65 | //Watchdog.setResponseTime_ms(10000); |
Yar | 0:a5f82a6b0d16 | 66 | #endif |
Yar | 0:a5f82a6b0d16 | 67 | // We get the state of the watchdog timer |
Yar | 0:a5f82a6b0d16 | 68 | WatchdogStatus(); |
Yar | 0:a5f82a6b0d16 | 69 | // Run watchdog |
Yar | 0:a5f82a6b0d16 | 70 | Watchdog.start(); |
Yar | 0:a5f82a6b0d16 | 71 | |
Yar | 0:a5f82a6b0d16 | 72 | |
Yar | 0:a5f82a6b0d16 | 73 | #if (1) |
Yar | 0:a5f82a6b0d16 | 74 | // Example of using the software I2C |
Yar | 0:a5f82a6b0d16 | 75 | uint8_t _addr = 0x4E; |
Yar | 0:a5f82a6b0d16 | 76 | uint8_t _reg = 0x20; |
Yar | 0:a5f82a6b0d16 | 77 | uint8_t _data = 0x55; |
Yar | 0:a5f82a6b0d16 | 78 | _i2c.start(); // Signal start |
Yar | 0:a5f82a6b0d16 | 79 | _i2c.write(_addr << 1); // Transfer the address of the device |
Yar | 0:a5f82a6b0d16 | 80 | _i2c.write(_reg); // Transfer the number of the register |
Yar | 0:a5f82a6b0d16 | 81 | _i2c.write(_data); // Write the data in the register |
Yar | 0:a5f82a6b0d16 | 82 | _i2c.stop(); // |
Yar | 0:a5f82a6b0d16 | 83 | // You can also use this function |
Yar | 0:a5f82a6b0d16 | 84 | // void write(uint8_t address, uint8_t* data, uint8_t length); |
Yar | 0:a5f82a6b0d16 | 85 | #endif |
Yar | 0:a5f82a6b0d16 | 86 | |
Yar | 0:a5f82a6b0d16 | 87 | |
Yar | 0:a5f82a6b0d16 | 88 | while(1) { |
Yar | 0:a5f82a6b0d16 | 89 | myled = !myled; |
Yar | 0:a5f82a6b0d16 | 90 | // This is an alternative delay for milliseconds |
Yar | 0:a5f82a6b0d16 | 91 | // For microseconds, there is a function delay_us |
Yar | 0:a5f82a6b0d16 | 92 | delay_ms(1000); |
Yar | 0:a5f82a6b0d16 | 93 | // Update Watchdog |
Yar | 0:a5f82a6b0d16 | 94 | // If this is not done, |
Yar | 0:a5f82a6b0d16 | 95 | // the watchdog timer will automatically reset the microcontroller |
Yar | 0:a5f82a6b0d16 | 96 | Watchdog.refresh(); |
Yar | 0:a5f82a6b0d16 | 97 | } |
Yar | 0:a5f82a6b0d16 | 98 | } |