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

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