PD Buddy Sink Firmware
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

config.c 7.5KB

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