util.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Utility functions
  3. *
  4. * Copyright (c) 2013 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "util.h"
  21. #include <avr/wdt.h>
  22. #include <avr/eeprom.h>
  23. /* Bitnumber to bitmask lookup table. */
  24. const uint8_t PROGMEM _bit_to_mask8[8] = {
  25. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  26. };
  27. /* Reboot the SOC. */
  28. void reboot(void)
  29. {
  30. /* Disable IRQs and use the watchdog
  31. * to trigger a watchdog reset. */
  32. irq_disable();
  33. wdt_enable(WDTO_15MS);
  34. while (1);
  35. unreachable();
  36. }
  37. /* A fatal error occurred. */
  38. void panic(void)
  39. {
  40. //TODO: Try to get an error message out.
  41. reboot();
  42. }
  43. /* Read a block of data from the EEPROM.
  44. * This function resets the watchdog timer after each processed byte.
  45. */
  46. void eeprom_read_block_wdtsafe(void *_dst, const void *_src, size_t n)
  47. {
  48. uint8_t *dst = _dst;
  49. const uint8_t *src = _src;
  50. for ( ; n; n--, dst++, src++) {
  51. *dst = eeprom_read_byte(src);
  52. wdt_reset();
  53. }
  54. }
  55. /* Update a block of data in the EEPROM.
  56. * This function resets the watchdog timer after each processed byte.
  57. */
  58. void eeprom_update_block_wdtsafe(const void *_src, void *_dst, size_t n)
  59. {
  60. const uint8_t *src = _src;
  61. uint8_t *dst = _dst;
  62. for ( ; n; n--, dst++, src++) {
  63. eeprom_update_byte(dst, *src);
  64. wdt_reset();
  65. }
  66. }