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.

config.c 7.6KB

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