PD Buddy Sink Firmware
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

config.c 8.0KB

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