1 /* 2 * Copyright (c) 2022 Unionman Technology Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef __UM_SPI_H__ 17 #define __UM_SPI_H__ 18 19 #define LOW 0 20 #define WIP_Flag 0x01 21 22 #define DIR_MAXSIZ 256 23 #define EXP_MAXSIZ 256 24 #define IN 0 25 #define OUT 1 26 27 #define UM_GPIO_SPI_CS 486 /* SS0 */ 28 #define SPI_FLASH_PageSize 256 29 #define SPI_FLASH_PerWritePageSize 256 30 31 #define FLASH_ADDRESS_HIGHT 0xFF0000 32 #define FLASH_ADDRESS_MIDDLE 0xFF00 33 #define FLASH_ADDRESS_LOW 0xFF 34 // return 35 #define SPI_SUCCESS 0 36 // error return 37 #define SPI_OPEN_DEV_FAIL (-1) 38 #define SPI_NOT_OPENED_FAIL (-2) 39 #define SPI_SET_MODE_FAIL (-3) 40 #define SPI_GET_MODE_FAIL (-4) 41 #define SPI_PARAM_FAIL (-5) 42 #define SPI_TRANSFER_FAIL (-6) 43 #define SPI_SET_DIRECTION_FAIL (-7) 44 #define SPI_OPEN_DIRECTION_FAIL (-8) 45 #define SPI_OPEN_VALUE_FAIL (-9) 46 #define SPI_READ_VALUE_FAIL (-10) 47 #define SPI_WRITE_VALUE_FAIL (-11) 48 #define SET_WRITEENABLE_FAIL (-12) 49 #define SPI_OPEN_DEVICE_FAIL (-13) 50 #define READ_STATUS_REGISTER_FAIL (-14) 51 #define READ_FLASHID_FAIL (-15) 52 #define READ_DEVICEID_FAIL (-16) 53 #define SPI_SET_FLASH_WRITE_ENABLE_FAIL (-17) 54 #define SPI_FLASH_SECTOR_ERASE_FAIL (-18) 55 #define SPI_FLASH_PAGE_WRITE_FAIL (-19) 56 #define SPI_FLASH_BUFFER_READ_FAIL (-20) 57 #define SET_GPIO_FAIL (-21) 58 #define SPI_VALUE_NUMBER_WRONG (-22) 59 #define SPI_SET_FLASH_WRITE_ENABLE (-23) 60 61 #define DEFAULT_DEV "/dev/spidev0.0" 62 63 #ifdef __cplusplus 64 extern "C" { 65 #endif 66 67 void sysfs_gpio_export(int s32GpioNum, int bExport); 68 69 int sysfs_gpio_direction(int Pin, int Dir); 70 71 int sysfs_gpio_write(int Pin, int value); 72 73 int spi_open(void); 74 75 int spi_read_flashid(uint8_t *dataID); 76 77 int spi_read_deviceid(uint8_t *dataID); 78 79 int read_status_register(uint8_t *data, int len); 80 81 int spi_flash_writeenable(void); 82 83 int spi_read_data(int first, int last, uint8_t *data); 84 85 int spi_transfer_fullduplex(uint8_t *txbuf, int len, uint8_t *rxbuf); 86 87 void spi_wait_writeend(void); 88 89 int spi_sector_erase(uint32_t SectorAddr); 90 91 int spi_page_write(uint8_t *Buffer, uint32_t WriteAddr, uint16_t NumByteToWrite); 92 93 int spi_buffer_read(uint8_t *Buffer, uint32_t ReadAddr, uint16_t NumByteToRead); 94 95 int spi_buffer_write(uint8_t *Buffer, uint32_t WriteAddr, uint16_t NumByteToWrite); 96 97 int spi_multi_pages_write(uint8_t *Buffer, uint32_t WriteAddr, uint16_t NumByteToWrite); 98 99 int spi_less_page_write(uint8_t *Buffer, uint32_t WriteAddr, uint16_t NumByteToWrite); 100 101 #ifdef __cplusplus 102 } 103 #endif 104 #endif // endif __UM_SPI_H__ 105