little_flash_port.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "little_flash.h"
  2. #include "luat_rtos.h"
  3. #include "luat_mem.h"
  4. #include "luat_spi.h"
  5. static lf_err_t little_flash_spi_transfer(const little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len){
  6. lf_err_t result = LF_ERR_OK;
  7. luat_spi_device_t *spi_dev = (luat_spi_device_t*)lf->spi.user_data;
  8. if (tx_len && rx_len) {
  9. if (luat_spi_device_transfer(spi_dev , (const char*)tx_buf, tx_len, (char*)rx_buf, rx_len) <= 0) {
  10. result = LF_ERR_TRANSFER;
  11. }
  12. } else if (tx_buf) {
  13. if (luat_spi_device_send(spi_dev , (const char*)tx_buf, tx_len) <= 0) {
  14. result = LF_ERR_WRITE;
  15. }
  16. } else {
  17. if (luat_spi_device_recv(spi_dev , (char*)rx_buf, rx_len) <= 0) {
  18. result = LF_ERR_READ;
  19. }
  20. }
  21. return result;
  22. }
  23. static void little_flash_wait_10us(uint32_t count){
  24. uint32_t delay = 12*count;
  25. while(delay--);
  26. }
  27. static void little_flash_wait_ms(uint32_t ms){
  28. luat_rtos_task_sleep(ms);
  29. }
  30. #ifdef LF_USE_HEAP
  31. static void* little_flash_malloc(size_t size){
  32. return luat_heap_opt_malloc(LUAT_HEAP_SRAM,size);
  33. }
  34. static void little_flash_free(void* ptr){
  35. luat_heap_opt_free(LUAT_HEAP_AUTO,ptr);
  36. }
  37. #endif
  38. static void little_flash_lock(little_flash_t *lf){
  39. luat_mutex_lock(lf->user_data);
  40. }
  41. static void little_flash_unlock(little_flash_t *lf){
  42. luat_mutex_unlock(lf->user_data);
  43. }
  44. lf_err_t little_flash_port_init(little_flash_t *lf){
  45. lf->spi.transfer = little_flash_spi_transfer;
  46. lf->wait_10us = little_flash_wait_10us;
  47. lf->wait_ms = little_flash_wait_ms;
  48. #ifdef LF_USE_HEAP
  49. lf->malloc = little_flash_malloc;
  50. lf->free = little_flash_free;
  51. #endif
  52. /* create a mutex for locking */
  53. lf->user_data = luat_mutex_create();
  54. lf->lock = little_flash_lock;
  55. lf->unlock = little_flash_unlock;
  56. return LF_ERR_OK;
  57. }