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

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