Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Committer:
modtronix
Date:
Sun Aug 30 09:23:05 2015 +1000
Revision:
3:99cb87ee1792
Parent:
2:cd263c5e86f2
Child:
5:e1297df8ee0d
Updated, added new files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 2:cd263c5e86f2 1 /**
modtronix 2:cd263c5e86f2 2 * File: modtronix_nz32s.h
modtronix 2:cd263c5e86f2 3 *
modtronix 2:cd263c5e86f2 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 2:cd263c5e86f2 5 *
modtronix 2:cd263c5e86f2 6 * Description:
modtronix 2:cd263c5e86f2 7 *
modtronix 2:cd263c5e86f2 8 * Software License Agreement:
modtronix 2:cd263c5e86f2 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 2:cd263c5e86f2 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 2:cd263c5e86f2 11 * applications. If this is modified software, any license conditions from original
modtronix 2:cd263c5e86f2 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 2:cd263c5e86f2 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 2:cd263c5e86f2 14 *
modtronix 2:cd263c5e86f2 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 2:cd263c5e86f2 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 2:cd263c5e86f2 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 2:cd263c5e86f2 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 2:cd263c5e86f2 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 2:cd263c5e86f2 20 */
modtronix 2:cd263c5e86f2 21 #ifndef MODTRONIX_NZ32S_H_
modtronix 2:cd263c5e86f2 22 #define MODTRONIX_NZ32S_H_
modtronix 2:cd263c5e86f2 23
modtronix 2:cd263c5e86f2 24 #include "mbed.h"
modtronix 2:cd263c5e86f2 25
modtronix 2:cd263c5e86f2 26 #ifndef WEAK
modtronix 2:cd263c5e86f2 27 #if defined (__ICCARM__)
modtronix 2:cd263c5e86f2 28 #define WEAK __weak
modtronix 2:cd263c5e86f2 29 #else
modtronix 2:cd263c5e86f2 30 #define WEAK __attribute__((weak))
modtronix 2:cd263c5e86f2 31 #endif
modtronix 2:cd263c5e86f2 32 #endif
modtronix 2:cd263c5e86f2 33
modtronix 3:99cb87ee1792 34
modtronix 3:99cb87ee1792 35 class NZ32S {
modtronix 3:99cb87ee1792 36 public:
modtronix 3:99cb87ee1792 37 static IWDG_HandleTypeDef hiwdg; //Watchdog Timer
modtronix 3:99cb87ee1792 38
modtronix 3:99cb87ee1792 39 public:
modtronix 3:99cb87ee1792 40 /**
modtronix 3:99cb87ee1792 41 * Reset I2C bus
modtronix 3:99cb87ee1792 42 */
modtronix 3:99cb87ee1792 43 static void i2c_reset(uint8_t busNumber, PinName sda, PinName scl);
modtronix 3:99cb87ee1792 44
modtronix 3:99cb87ee1792 45 /** Initializes and start the IWDG Watchdog timer with given timerout.
modtronix 3:99cb87ee1792 46 * After calling this function, watchdog_refresh() function must be called before the
modtronix 3:99cb87ee1792 47 * timeout expires, else the MCU will be reset.
modtronix 3:99cb87ee1792 48 *
modtronix 3:99cb87ee1792 49 * @param timeout The timeout in ms. Must be a value from 1 to 32000 (1ms to 32 seconds)
modtronix 3:99cb87ee1792 50 */
modtronix 3:99cb87ee1792 51 static inline void watchdog_start(uint32_t timeout) {
modtronix 3:99cb87ee1792 52 //Watchdog frequency is always 32 kHz
modtronix 3:99cb87ee1792 53 //Prescaler: Min_Value = 4 and Max_Value = 256
modtronix 3:99cb87ee1792 54 //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
modtronix 3:99cb87ee1792 55 //TimeOut in seconds = (Reload * Prescaler) / Freq.
modtronix 3:99cb87ee1792 56 //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
modtronix 3:99cb87ee1792 57 //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
modtronix 3:99cb87ee1792 58 NZ32S::hiwdg.Instance = IWDG;
modtronix 3:99cb87ee1792 59
modtronix 3:99cb87ee1792 60 //MXH_DEBUG("\r\nwatchdog_start() called!!!!!!!!!!!!!!");
modtronix 3:99cb87ee1792 61
modtronix 3:99cb87ee1792 62 //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
modtronix 3:99cb87ee1792 63 //equal to 'timeout' value in ms
modtronix 3:99cb87ee1792 64 if(timeout < 4000) {
modtronix 3:99cb87ee1792 65 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
modtronix 3:99cb87ee1792 66 NZ32S::hiwdg.Init.Reload = timeout; //Timeout = (32 * timeout) / 32000 = (timeout) ms
modtronix 3:99cb87ee1792 67 }
modtronix 3:99cb87ee1792 68 else {
modtronix 3:99cb87ee1792 69 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
modtronix 3:99cb87ee1792 70 NZ32S::hiwdg.Init.Reload = timeout/8; //Timeout = (256 * (timeout/8)) / 32000
modtronix 3:99cb87ee1792 71 }
modtronix 3:99cb87ee1792 72 HAL_IWDG_Init(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 73
modtronix 3:99cb87ee1792 74 //Start the watchdog timer
modtronix 3:99cb87ee1792 75 HAL_IWDG_Start(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 76 }
modtronix 3:99cb87ee1792 77
modtronix 3:99cb87ee1792 78
modtronix 3:99cb87ee1792 79 /** Refreshes the IWDG
modtronix 3:99cb87ee1792 80 */
modtronix 3:99cb87ee1792 81 static inline void watchdog_refresh(void) {
modtronix 3:99cb87ee1792 82 HAL_IWDG_Refresh(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 83 }
modtronix 3:99cb87ee1792 84
modtronix 3:99cb87ee1792 85
modtronix 3:99cb87ee1792 86 /** Return true if last reset was caused by watchdog timer
modtronix 3:99cb87ee1792 87 *
modtronix 3:99cb87ee1792 88 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 89 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 90 */
modtronix 3:99cb87ee1792 91 static inline bool watchdog_caused_reset(bool clearFlags) {
modtronix 3:99cb87ee1792 92 bool retVal;
modtronix 3:99cb87ee1792 93 retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
modtronix 3:99cb87ee1792 94 if (clearFlags==true) {
modtronix 3:99cb87ee1792 95 __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
modtronix 3:99cb87ee1792 96 }
modtronix 3:99cb87ee1792 97 return retVal;
modtronix 3:99cb87ee1792 98 }
modtronix 3:99cb87ee1792 99
modtronix 3:99cb87ee1792 100 /** Print the cause of last reset
modtronix 3:99cb87ee1792 101 *
modtronix 3:99cb87ee1792 102 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 103 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 104 */
modtronix 3:99cb87ee1792 105 static inline void print_reset_cause(bool clearFlags);
modtronix 3:99cb87ee1792 106
modtronix 3:99cb87ee1792 107
modtronix 3:99cb87ee1792 108 };
modtronix 3:99cb87ee1792 109
modtronix 2:cd263c5e86f2 110
modtronix 2:cd263c5e86f2 111 #endif //MODTRONIX_NZ32S_H_