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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "pd.h"
  21. /* Initialize the location of the configuration array. PDB_CONFIG_BASE is set
  22. * in the Makefile. */
  23. struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;
  24. void pdb_config_print(BaseSequentialStream *chp, const struct pdb_config *cfg)
  25. {
  26. /* Print the status */
  27. chprintf(chp, "status: ");
  28. switch (cfg->status) {
  29. case PDB_CONFIG_STATUS_INVALID:
  30. chprintf(chp, "in");
  31. /* fall-through */
  32. case PDB_CONFIG_STATUS_VALID:
  33. chprintf(chp, "valid\r\n");
  34. break;
  35. case PDB_CONFIG_STATUS_EMPTY:
  36. chprintf(chp, "empty\r\n");
  37. /* Stop early because the rest of the information is meaningless in
  38. * this case. */
  39. return;
  40. default:
  41. chprintf(chp, "%04X\r\n", cfg->status);
  42. break;
  43. }
  44. /* Print the flags */
  45. chprintf(chp, "flags: ");
  46. if (cfg->flags == 0) {
  47. chprintf(chp, "(none)");
  48. }
  49. if (cfg->flags & PDB_CONFIG_FLAGS_GIVEBACK) {
  50. chprintf(chp, "GiveBack ");
  51. }
  52. if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
  53. chprintf(chp, "Var/Bat ");
  54. }
  55. chprintf(chp, "\r\n");
  56. /* Print voltages and current */
  57. chprintf(chp, "v: %d.%02d V\r\n", PD_PDV_V(cfg->v), PD_PDV_CV(cfg->v));
  58. chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));
  59. if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
  60. chprintf(chp, "v_min: %d.%02d V\r\n", PD_PDV_V(cfg->v_min),
  61. PD_PDV_CV(cfg->v_min));
  62. chprintf(chp, "v_max: %d.%02d V\r\n", PD_PDV_V(cfg->v_max),
  63. PD_PDV_CV(cfg->v_max));
  64. }
  65. }
  66. /*
  67. * Unlock the flash interface
  68. */
  69. static void flash_unlock(void)
  70. {
  71. /* Wait till no operation is on going */
  72. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  73. /* Note: we might want a timeout here */
  74. }
  75. /* Check that the Flash is locked */
  76. if ((FLASH->CR & FLASH_CR_LOCK) != 0) {
  77. /* Perform unlock sequence */
  78. FLASH->KEYR = FLASH_KEY1;
  79. FLASH->KEYR = FLASH_KEY2;
  80. }
  81. }
  82. /*
  83. * Lock the flash interface
  84. */
  85. static void flash_lock(void)
  86. {
  87. /* Wait till no operation is on going */
  88. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  89. /* Note: we might want a timeout here */
  90. }
  91. /* Check that the Flash is unlocked */
  92. if ((FLASH->CR & FLASH_CR_LOCK) == 0) {
  93. /* Lock the flash */
  94. FLASH->CR |= FLASH_CR_LOCK;
  95. }
  96. }
  97. /*
  98. * Write one halfword to flash
  99. */
  100. static void flash_write_halfword(uint16_t *addr, uint16_t data)
  101. {
  102. /* Set the PG bit in the FLASH_CR register to enable programming */
  103. FLASH->CR |= FLASH_CR_PG;
  104. /* Perform the data write (half-word) at the desired address */
  105. *(__IO uint16_t*)(addr) = data;
  106. /* Wait until the BSY bit is reset in the FLASH_SR register */
  107. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  108. /* For robust implementation, add here time-out management */
  109. }
  110. /* Check the EOP flag in the FLASH_SR register */
  111. if ((FLASH->SR & FLASH_SR_EOP) != 0) {
  112. /* clear it by software by writing it at 1 */
  113. FLASH->SR = FLASH_SR_EOP;
  114. } else {
  115. /* Manage the error cases */
  116. }
  117. /* Reset the PG Bit to disable programming */
  118. FLASH->CR &= ~FLASH_CR_PG;
  119. }
  120. /*
  121. * Erase the configuration page, without any locking
  122. */
  123. static void flash_erase(void)
  124. {
  125. /* Set the PER bit in the FLASH_CR register to enable page erasing */
  126. FLASH->CR |= FLASH_CR_PER;
  127. /* Program the FLASH_AR register to select a page to erase */
  128. FLASH->AR = (int) pdb_config_array;
  129. /* Set the STRT bit in the FLASH_CR register to start the erasing */
  130. FLASH->CR |= FLASH_CR_STRT;
  131. /* Wait till no operation is on going */
  132. while ((FLASH->SR & FLASH_SR_BSY) != 0) {
  133. /* Note: we might want a timeout here */
  134. }
  135. /* Check the EOP flag in the FLASH_SR register */
  136. if ((FLASH->SR & FLASH_SR_EOP) != 0) {
  137. /* Clear EOP flag by software by writing EOP at 1 */
  138. FLASH->SR = FLASH_SR_EOP;
  139. } else {
  140. /* Manage the error cases */
  141. }
  142. /* Reset the PER Bit to disable the page erase */
  143. FLASH->CR &= ~FLASH_CR_PER;
  144. }
  145. void pdb_config_flash_erase(void)
  146. {
  147. /* Enter a critical zone */
  148. chSysLock();
  149. flash_unlock();
  150. /* Erase the flash page */
  151. flash_erase();
  152. flash_lock();
  153. /* Exit the critical zone */
  154. chSysUnlock();
  155. }
  156. void pdb_config_flash_update(const struct pdb_config *cfg)
  157. {
  158. /* Enter a critical zone */
  159. chSysLock();
  160. flash_unlock();
  161. /* If there is an old entry, invalidate it. */
  162. struct pdb_config *old = pdb_config_flash_read();
  163. if (old != NULL) {
  164. flash_write_halfword(&(old->status), PDB_CONFIG_STATUS_INVALID);
  165. }
  166. /* Find the first empty entry */
  167. struct pdb_config *empty = NULL;
  168. for (int i = 0; i < PDB_CONFIG_ARRAY_LEN; i++) {
  169. /* If we've found it, return it. */
  170. if (pdb_config_array[i].status == PDB_CONFIG_STATUS_EMPTY) {
  171. empty = &pdb_config_array[i];
  172. break;
  173. }
  174. }
  175. /* If empty is still NULL, the page is full. Erase it. */
  176. if (empty == NULL) {
  177. flash_erase();
  178. /* Write to the first element */
  179. empty = &pdb_config_array[0];
  180. }
  181. /* Write the new configuration */
  182. flash_write_halfword(&(empty->status), cfg->status);
  183. flash_write_halfword(&(empty->flags), cfg->flags);
  184. flash_write_halfword(&(empty->v), cfg->v);
  185. flash_write_halfword(&(empty->i), cfg->i);
  186. flash_write_halfword(&(empty->v_min), cfg->v_min);
  187. flash_write_halfword(&(empty->v_max), cfg->v_max);
  188. flash_lock();
  189. /* Exit the critical zone */
  190. chSysUnlock();
  191. }
  192. struct pdb_config *pdb_config_flash_read(void)
  193. {
  194. /* If the first element is empty, there is no valid structure. */
  195. if (pdb_config_array[0].status == PDB_CONFIG_STATUS_EMPTY) {
  196. return NULL;
  197. }
  198. /* Find the valid structure, if there is one. */
  199. for (int i = 0; i < PDB_CONFIG_ARRAY_LEN; i++) {
  200. /* If we've found it, return it. */
  201. if (pdb_config_array[i].status == PDB_CONFIG_STATUS_VALID) {
  202. return &pdb_config_array[i];
  203. }
  204. }
  205. /* If we got to the end, none of the structures is valid. */
  206. return NULL;
  207. }