PD Buddy Sink Firmware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "storage.h"
  19. #include "chprintf.h"
  20. struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;
  21. void pdb_config_print(BaseSequentialStream *chp, const struct pdb_config *cfg)
  22. {
  23. /* Print the status */
  24. chprintf(chp, "status: ");
  25. switch (cfg->status) {
  26. case PDB_CONFIG_STATUS_INVALID:
  27. chprintf(chp, "in");
  28. /* fall-through */
  29. case PDB_CONFIG_STATUS_VALID:
  30. chprintf(chp, "valid\r\n");
  31. break;
  32. case PDB_CONFIG_STATUS_EMPTY:
  33. chprintf(chp, "empty\r\n");
  34. /* Stop early because the rest of the information is meaningless in
  35. * this case. */
  36. return;
  37. default:
  38. chprintf(chp, "%04X\r\n", cfg->status);
  39. break;
  40. }
  41. /* Print the flags */
  42. chprintf(chp, "flags: ");
  43. if (cfg->flags == 0) {
  44. chprintf(chp, "(none)");
  45. }
  46. if (cfg->flags & PDB_CONFIG_FLAGS_GIVEBACK) {
  47. chprintf(chp, "GiveBack ");
  48. }
  49. if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
  50. chprintf(chp, "Var/Bat ");
  51. }
  52. chprintf(chp, "\r\n");
  53. /* Print voltages and current */
  54. chprintf(chp, "v: %d.%02d V\r\n", cfg->v/20, 5*(cfg->v%20));
  55. chprintf(chp, "i: %d.%02d A\r\n", cfg->i/100, cfg->i%100);
  56. if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
  57. chprintf(chp, "v_min: %d.%02d V\r\n", cfg->v_min/20, 5*(cfg->v_min%20));
  58. chprintf(chp, "v_max: %d.%02d V\r\n", cfg->v_max/20, 5*(cfg->v_max%20));
  59. }
  60. }
  61. /*
  62. * Unlock the flash interface
  63. */
  64. static void flash_unlock(void)
  65. {
  66. /* Wait till no operation is on going */
  67. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  68. /* Note: we might want a timeout here */
  69. }
  70. /* Check that the Flash is locked */
  71. if ((FLASH->CR & FLASH_CR_LOCK) != 0) {
  72. /* Perform unlock sequence */
  73. FLASH->KEYR = FLASH_KEY1;
  74. FLASH->KEYR = FLASH_KEY2;
  75. }
  76. }
  77. /*
  78. * Lock the flash interface
  79. */
  80. static void flash_lock(void)
  81. {
  82. /* Wait till no operation is on going */
  83. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  84. /* Note: we might want a timeout here */
  85. }
  86. /* Check that the Flash is unlocked */
  87. if ((FLASH->CR & FLASH_CR_LOCK) == 0) {
  88. /* Lock the flash */
  89. FLASH->CR |= FLASH_CR_LOCK;
  90. }
  91. }
  92. /*
  93. * Write one halfword to flash
  94. */
  95. static void flash_write_halfword(uint16_t *addr, uint16_t data)
  96. {
  97. /* Set the PG bit in the FLASH_CR register to enable programming */
  98. FLASH->CR |= FLASH_CR_PG;
  99. /* Perform the data write (half-word) at the desired address */
  100. *(__IO uint16_t*)(addr) = data;
  101. /* Wait until the BSY bit is reset in the FLASH_SR register */
  102. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  103. /* For robust implementation, add here time-out management */
  104. }
  105. /* Check the EOP flag in the FLASH_SR register */
  106. if ((FLASH->SR & FLASH_SR_EOP) != 0) {
  107. /* clear it by software by writing it at 1 */
  108. FLASH->SR = FLASH_SR_EOP;
  109. } else {
  110. /* Manage the error cases */
  111. }
  112. /* Reset the PG Bit to disable programming */
  113. FLASH->CR &= ~FLASH_CR_PG;
  114. }
  115. /*
  116. * Erase the configuration page, without any locking
  117. */
  118. static void flash_erase(void)
  119. {
  120. /* Set the PER bit in the FLASH_CR register to enable page erasing */
  121. FLASH->CR |= FLASH_CR_PER;
  122. /* Program the FLASH_AR register to select a page to erase */
  123. FLASH->AR = (int) pdb_config_array;
  124. /* Set the STRT bit in the FLASH_CR register to start the erasing */
  125. FLASH->CR |= FLASH_CR_STRT;
  126. /* Wait till no operation is on going */
  127. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  128. /* Note: we might want a timeout here */
  129. }
  130. /* Check the EOP flag in the FLASH_SR register */
  131. if ((FLASH->SR & FLASH_SR_EOP) != 0) {
  132. /* Clear EOP flag by software by writing EOP at 1 */
  133. FLASH->SR = FLASH_SR_EOP;
  134. } else {
  135. /* Manage the error cases */
  136. }
  137. /* Reset the PER Bit to disable the page erase */
  138. FLASH->CR &= ~FLASH_CR_PER;
  139. }
  140. void pdb_config_flash_erase(void)
  141. {
  142. /* Enter a critical zone */
  143. chSysLock();
  144. flash_unlock();
  145. /* Erase the flash page */
  146. flash_erase();
  147. flash_lock();
  148. /* Exit the critical zone */
  149. chSysUnlock();
  150. }
  151. void pdb_config_flash_update(const struct pdb_config *cfg)
  152. {
  153. /* Enter a critical zone */
  154. chSysLock();
  155. flash_unlock();
  156. /* If there is an old entry, invalidate it. */
  157. struct pdb_config *old = pdb_config_flash_read();
  158. if (old != NULL) {
  159. flash_write_halfword(&(old->status), PDB_CONFIG_STATUS_INVALID);
  160. }
  161. /* Find the first empty entry */
  162. struct pdb_config *empty = NULL;
  163. for (int i = 0; i < PDB_CONFIG_ARRAY_LEN; i++) {
  164. /* If we've found it, return it. */
  165. if (pdb_config_array[i].status == PDB_CONFIG_STATUS_EMPTY) {
  166. empty = &pdb_config_array[i];
  167. break;
  168. }
  169. }
  170. /* If empty is still NULL, the page is full. Erase it. */
  171. if (empty == NULL) {
  172. flash_erase();
  173. /* Write to the first element */
  174. empty = &pdb_config_array[0];
  175. }
  176. /* Write the new configuration */
  177. flash_write_halfword(&(empty->status), cfg->status);
  178. flash_write_halfword(&(empty->flags), cfg->flags);
  179. flash_write_halfword(&(empty->v), cfg->v);
  180. flash_write_halfword(&(empty->i), cfg->i);
  181. flash_write_halfword(&(empty->v_min), cfg->v_min);
  182. flash_write_halfword(&(empty->v_max), cfg->v_max);
  183. flash_lock();
  184. /* Exit the critical zone */
  185. chSysUnlock();
  186. }
  187. struct pdb_config *pdb_config_flash_read(void)
  188. {
  189. /* If the first element is empty, there is no valid structure. */
  190. if (pdb_config_array[0].status == PDB_CONFIG_STATUS_EMPTY) {
  191. return NULL;
  192. }
  193. /* Find the valid structure, if there is one. */
  194. for (int i = 0; i < PDB_CONFIG_ARRAY_LEN; i++) {
  195. /* If we've found it, return it. */
  196. if (pdb_config_array[i].status == PDB_CONFIG_STATUS_VALID) {
  197. return &pdb_config_array[i];
  198. }
  199. }
  200. /* If we got to the end, none of the structures is valid. */
  201. return NULL;
  202. }