diff --git a/README.md b/README.md index 973290b..01f2638 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ If you want to start a new project, for instance, `hello_world`, you only need t ```shell mkdir build && cd build -cmake .. -DPROJ= -DTOOLCHAIN=/opt/riscv-toolchain/bin && make +cmake .. -DPROJ= -DTOOLCHAIN=/opt/riscv-toolchain/bin/ && make ``` You will get 2 key files, `hello_world` and `hello_world.bin`. @@ -21,3 +21,5 @@ You will get 2 key files, `hello_world` and `hello_world.bin`. 2. If you want to flash it in UOG, using `hello_world.bin`, then using flash-tool(s) burn .bin to your flash. This is very important, don't make a mistake in files. + +*If you don't like place code inside SDK, see `CMakeLists.txt.example.cmake`* diff --git a/cmake/dump-config.cmake b/cmake/dump-config.cmake index 12ac5af..8531f6c 100644 --- a/cmake/dump-config.cmake +++ b/cmake/dump-config.cmake @@ -10,6 +10,7 @@ message(" CMAKE_C_COMPILER=${CMAKE_C_COMPILER}") message(" CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") message(" CMAKE_LINKER=${CMAKE_LINKER}") message(" CMAKE_OBJCOPY=${CMAKE_OBJCOPY}") +message(" CMAKE_OBJDUMP=${CMAKE_OBJDUMP}") message(" CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}") message("") message(" CMAKE_C_FLAGS=${CMAKE_C_FLAGS}") diff --git a/cmake/executable.cmake b/cmake/executable.cmake index 5070df6..4a53fb2 100644 --- a/cmake/executable.cmake +++ b/cmake/executable.cmake @@ -1,16 +1,13 @@ - -message("CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}") -message("_TC_MAKE=${_TC_MAKE}") -if (_TC_MAKE) - global_set(CMAKE_MAKE_PROGRAM "${_TC_MAKE}") -endif() +include(${CMAKE_CURRENT_LIST_DIR}/reset.cmake) if (NOT BUILDING_SDK) if(EXISTS ${SDK_ROOT}/libkendryte.a) + ### compiled sdk header_directories(${SDK_ROOT}/include) add_library(kendryte STATIC IMPORTED) set_property(TARGET kendryte PROPERTY IMPORTED_LOCATION ${SDK_ROOT}/libkendryte.a) else() + ### source code sdk header_directories(${SDK_ROOT}/lib) add_subdirectory(${SDK_ROOT}/lib SDK) endif() @@ -21,24 +18,9 @@ removeDuplicateSubstring(${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS) message("SOURCE_FILES=${SOURCE_FILES}") add_executable(${PROJECT_NAME} ${SOURCE_FILES}) -# add_dependencies(${PROJECT_NAME} kendryte) # TODO: third_party -# target_link_libraries(${PROJECT_NAME} kendryte) # TODO: third_party -# link_directories(${CMAKE_BINARY_DIR}) set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C) -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crt0.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRT0_OBJ) -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtbegin.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTBEGIN_OBJ) -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtend.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTEND_OBJ) -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crti.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTI_OBJ) -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtn.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTN_OBJ) - -set(CMAKE_C_LINK_EXECUTABLE -" \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") - -set(CMAKE_CXX_LINK_EXECUTABLE -" \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") - target_link_libraries(${PROJECT_NAME} -Wl,--start-group m kendryte diff --git a/cmake/reset.cmake b/cmake/reset.cmake new file mode 100644 index 0000000..7677914 --- /dev/null +++ b/cmake/reset.cmake @@ -0,0 +1,7 @@ +global_set(CMAKE_C_COMPILER "${__CMAKE_C_COMPILER}") +global_set(CMAKE_CXX_COMPILER "${__CMAKE_CXX_COMPILER}") +global_set(CMAKE_LINKER "${__CMAKE_LINKER}") +global_set(CMAKE_AR "${__CMAKE_AR}") +global_set(CMAKE_OBJCOPY "${__CMAKE_OBJCOPY}") +global_set(CMAKE_SIZE "${__CMAKE_SIZE}") +global_set(CMAKE_OBJDUMP "${__CMAKE_OBJDUMP}") diff --git a/cmake/save.cmake b/cmake/save.cmake new file mode 100644 index 0000000..6191d7c --- /dev/null +++ b/cmake/save.cmake @@ -0,0 +1,7 @@ +global_set(__CMAKE_C_COMPILER "${CMAKE_C_COMPILER}") +global_set(__CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}") +global_set(__CMAKE_LINKER "${CMAKE_LINKER}") +global_set(__CMAKE_AR "${CMAKE_AR}") +global_set(__CMAKE_OBJCOPY "${CMAKE_OBJCOPY}") +global_set(__CMAKE_SIZE "${CMAKE_SIZE}") +global_set(__CMAKE_OBJDUMP "${CMAKE_OBJDUMP}") diff --git a/cmake/toolchain.cmake b/cmake/toolchain.cmake index b3e308f..967a97c 100644 --- a/cmake/toolchain.cmake +++ b/cmake/toolchain.cmake @@ -10,35 +10,40 @@ endif () message(STATUS "Check for RISCV toolchain ...") -IF("${TOOLCHAIN}" STREQUAL "") - message(STATUS "Using default RISCV toolchain") - - global_set(CMAKE_C_COMPILER "riscv64-unknown-elf-gcc${EXT}") - global_set(CMAKE_CXX_COMPILER "riscv64-unknown-elf-g++${EXT}") - global_set(CMAKE_LINKER "riscv64-unknown-elf-ld${EXT}") - global_set(CMAKE_AR "riscv64-unknown-elf-ar${EXT}") - global_set(CMAKE_OBJCOPY "riscv64-unknown-elf-objcopy${EXT}") - global_set(CMAKE_SIZE "riscv64-unknown-elf-size${EXT}") - global_set(CMAKE_OBJDUMP "riscv64-unknown-elf-objdump${EXT}") -ELSE() - message(STATUS "Using ${TOOLCHAIN} RISCV toolchain") - - global_set(CMAKE_C_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-gcc${EXT}") - global_set(CMAKE_CXX_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-g++${EXT}") - global_set(CMAKE_LINKER "${TOOLCHAIN}/riscv64-unknown-elf-ld${EXT}") - global_set(CMAKE_AR "${TOOLCHAIN}/riscv64-unknown-elf-ar${EXT}") - global_set(CMAKE_OBJCOPY "${TOOLCHAIN}/riscv64-unknown-elf-objcopy${EXT}") - global_set(CMAKE_SIZE "${TOOLCHAIN}/riscv64-unknown-elf-size${EXT}") - global_set(CMAKE_OBJDUMP "${TOOLCHAIN}/riscv64-unknown-elf-objdump${EXT}") -ENDIF() - -if (CMAKE_MAKE_PROGRAM) - global_set(_TC_MAKE "${CMAKE_MAKE_PROGRAM}") +if(NOT TOOLCHAIN) + # do nothing +elseif(NOT "${TOOLCHAIN}" MATCHES "/$") + global_set(TOOLCHAIN "${TOOLCHAIN}") endif() +message(STATUS "Using ${TOOLCHAIN} RISCV toolchain") + +global_set(CMAKE_C_COMPILER "${TOOLCHAIN}riscv64-unknown-elf-gcc${EXT}") +global_set(CMAKE_CXX_COMPILER "${TOOLCHAIN}riscv64-unknown-elf-g++${EXT}") +global_set(CMAKE_LINKER "${TOOLCHAIN}riscv64-unknown-elf-ld${EXT}") +global_set(CMAKE_AR "${TOOLCHAIN}riscv64-unknown-elf-ar${EXT}") +global_set(CMAKE_OBJCOPY "${TOOLCHAIN}riscv64-unknown-elf-objcopy${EXT}") +global_set(CMAKE_SIZE "${TOOLCHAIN}riscv64-unknown-elf-size${EXT}") +global_set(CMAKE_OBJDUMP "${TOOLCHAIN}riscv64-unknown-elf-objdump${EXT}") + +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crt0.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRT0_OBJ) +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtbegin.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTBEGIN_OBJ) +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtend.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTEND_OBJ) +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crti.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTI_OBJ) +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtn.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTN_OBJ) + +global_set(CMAKE_C_LINK_EXECUTABLE + " \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") + +global_set(CMAKE_CXX_LINK_EXECUTABLE + " \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") + +## do a simple check get_filename_component(_BIN_DIR "${CMAKE_C_COMPILER}" DIRECTORY) if (NOT "${TOOLCHAIN}" STREQUAL "${_BIN_DIR}" AND NOT "${TOOLCHAIN}" STREQUAL "${_BIN_DIR}/") message("TOOLCHAIN is [${TOOLCHAIN}]") message("_BIN_DIR is [${_BIN_DIR}]") message(WARNING "CMAKE_C_COMPILER is not in kendryte-toolchain dist/bin folder.") endif () + +include(${CMAKE_CURRENT_LIST_DIR}/save.cmake) diff --git a/lib/bsp/device/aes.c b/lib/bsp/device/aes.c index b2221d6..cb0371b 100644 --- a/lib/bsp/device/aes.c +++ b/lib/bsp/device/aes.c @@ -15,24 +15,17 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include #include -#define AES_USE_DMA 1 #define AES_BASE_ADDR (0x50450000) -#define COMMON_ENTRY \ - aes_dev_data* data = (aes_dev_data*)userdata; \ - volatile aes_t* aes = (volatile aes_t*)data->base_addr; \ +#define COMMON_ENTRY \ + aes_dev_data *data = (aes_dev_data *)userdata; \ + volatile aes_t *aes = (volatile aes_t *)data->base_addr; \ (void)aes; typedef struct { + enum sysctl_clock_e clock; uintptr_t base_addr; SemaphoreHandle_t free_mutex; @@ -42,130 +35,121 @@ typedef struct }; } aes_dev_data; -static void aes_install(void* userdata) +static void aes_install(void *userdata) { COMMON_ENTRY; - sysctl_clock_enable(SYSCTL_CLOCK_AES); - sysctl_reset(SYSCTL_RESET_AES); + sysctl_clock_enable(data->clock); data->free_mutex = xSemaphoreCreateMutex(); } -static int aes_open(void* userdata) +static int aes_open(void *userdata) { return 1; } -static void aes_close(void* userdata) +static void aes_close(void *userdata) { } -static void entry_exclusive(aes_dev_data* data) +static void entry_exclusive(aes_dev_data *data) { configASSERT(xSemaphoreTake(data->free_mutex, portMAX_DELAY) == pdTRUE); } -static void exit_exclusive(aes_dev_data* data) +static void exit_exclusive(aes_dev_data *data) { xSemaphoreGive(data->free_mutex); } -static int os_aes_write_aad(uint32_t aad_data, void* userdata) +static void os_aes_write_aad(uint32_t aad_data, void *userdata) { COMMON_ENTRY; aes->aes_aad_data = aad_data; - return 0; } -static int os_aes_write_text(uint32_t text_data, void* userdata) +static void os_aes_write_text(uint32_t text_data, void *userdata) { COMMON_ENTRY; aes->aes_text_data = text_data; - return 0; } -static int os_aes_write_tag(uint32_t* tag, void* userdata) +static void os_aes_write_tag(uint32_t *tag, void *userdata) { COMMON_ENTRY; aes->gcm_in_tag[0] = tag[3]; aes->gcm_in_tag[1] = tag[2]; aes->gcm_in_tag[2] = tag[1]; aes->gcm_in_tag[3] = tag[0]; - return 0; } -static int os_get_data_in_flag(void* userdata) +static uint32_t os_get_data_in_flag(void *userdata) { COMMON_ENTRY; - /* data can in flag 1: data ready 0: data not ready */ return aes->data_in_flag; } -static int os_get_data_out_flag(void* userdata) +static uint32_t os_get_data_out_flag(void *userdata) { COMMON_ENTRY; - /* data can output flag 1: data ready 0: data not ready */ return aes->data_out_flag; } -static int os_get_tag_in_flag(void* userdata) +static uint32_t os_get_tag_in_flag(void *userdata) { COMMON_ENTRY; - /* data can output flag 1: data ready 0: data not ready */ return aes->tag_in_flag; } -static uint32_t os_read_out_data(void* userdata) +static uint32_t os_read_out_data(void *userdata) { COMMON_ENTRY; return aes->aes_out_data; } -static int os_aes_check_tag(void* userdata) +static uint32_t os_aes_check_tag(void *userdata) { COMMON_ENTRY; return aes->tag_chk; } -static int os_aes_get_tag(uint8_t* l_tag, void* userdata) +static void os_aes_get_tag(uint8_t *output_tag, void *userdata) { COMMON_ENTRY; - uint32_t u32tag; + uint32_t uint32_tag; uint8_t i = 0; - u32tag = aes->gcm_out_tag[3]; - l_tag[i++] = (uint8_t)((u32tag >> 24) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 16) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 8) & 0xff); - l_tag[i++] = (uint8_t)((u32tag)&0xff); - - u32tag = aes->gcm_out_tag[2]; - l_tag[i++] = (uint8_t)((u32tag >> 24) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 16) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 8) & 0xff); - l_tag[i++] = (uint8_t)((u32tag)&0xff); - - u32tag = aes->gcm_out_tag[1]; - l_tag[i++] = (uint8_t)((u32tag >> 24) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 16) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 8) & 0xff); - l_tag[i++] = (uint8_t)((u32tag)&0xff); - - u32tag = aes->gcm_out_tag[0]; - l_tag[i++] = (uint8_t)((u32tag >> 24) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 16) & 0xff); - l_tag[i++] = (uint8_t)((u32tag >> 8) & 0xff); - l_tag[i++] = (uint8_t)((u32tag)&0xff); - return 1; + uint32_tag = aes->gcm_out_tag[3]; + output_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[2]; + output_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[1]; + output_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[0]; + output_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + output_tag[i++] = (uint8_t)((uint32_tag)&0xff); } -static int os_aes_clear_chk_tag(void* userdata) +static void os_aes_clear_chk_tag(void *userdata) { COMMON_ENTRY; aes->tag_clear = 0; - return 0; } -static int os_check_tag(uint32_t* aes_gcm_tag, void* userdata) +static int os_check_tag(uint32_t *aes_gcm_tag, void *userdata) { while (!os_get_tag_in_flag(userdata)) ; @@ -184,359 +168,267 @@ static int os_check_tag(uint32_t* aes_gcm_tag, void* userdata) } } -static int os_aes_init(uint8_t* key_addr, uint8_t key_length, uint8_t* aes_iv, - uint8_t iv_length, uint8_t* aes_aad, aes_cipher_mod cipher_mod, aes_encrypt_sel encrypt_sel, - uint32_t add_size, uint32_t data_size, void* userdata) +static void os_aes_init(uint8_t *input_key, size_t input_key_len, uint8_t *iv, size_t iv_len, + uint8_t *gcm_add, aes_cipher_mode_t cipher_mode, aes_encrypt_sel_t encrypt_sel, size_t gcm_add_len, + size_t input_data_len, void *userdata) { COMMON_ENTRY; + size_t padding_len, i, remainder, uint32_num, uint8_num; + uint32_t uint32_data; + uint8_t uint8_data[4] = {0}; - int i, remainder, num, cnt; - uint32_t u32data; - uint8_t u8data[4] = {0}; - - if ((cipher_mod == AES_CIPHER_ECB) || (cipher_mod == AES_CIPHER_CBC)) - data_size = ((data_size + 15) / 16) * 16; + padding_len = input_data_len; + if ((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) + padding_len = ((input_data_len + 15) / 16) * 16; aes->aes_endian |= 1; - - /* write key Low byte alignment */ - num = key_length / 4; - for (i = 0; i < num; i++) + uint32_num = input_key_len / 4; + for (i = 0; i < uint32_num; i++) { if (i < 4) - aes->aes_key[i] = *((uint32_t*)(&key_addr[key_length - (4 * i) - 4])); + aes->aes_key[i] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); else - aes->aes_key_ext[i - 4] = *((uint32_t*)(&key_addr[key_length - (4 * i) - 4])); + aes->aes_key_ext[i - 4] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); } - remainder = key_length % 4; + remainder = input_key_len % 4; if (remainder) { switch (remainder) { - case 1: - u8data[0] = key_addr[0]; - break; - case 2: - u8data[0] = key_addr[0]; - u8data[1] = key_addr[1]; - break; - case 3: - u8data[0] = key_addr[0]; - u8data[1] = key_addr[1]; - u8data[2] = key_addr[2]; - break; - default: - break; + case 1: + uint8_data[0] = input_key[0]; + break; + case 2: + uint8_data[0] = input_key[0]; + uint8_data[1] = input_key[1]; + break; + case 3: + uint8_data[0] = input_key[0]; + uint8_data[1] = input_key[1]; + uint8_data[2] = input_key[2]; + break; + default: + break; } - if (num < 4) - aes->aes_key[num] = *((uint32_t*)(&u8data[0])); + if (uint32_num < 4) + aes->aes_key[uint32_num] = *((uint32_t *)(&uint8_data[0])); else - aes->aes_key_ext[num - 4] = *((uint32_t*)(&u8data[0])); + aes->aes_key_ext[uint32_num - 4] = *((uint32_t *)(&uint8_data[0])); } - - /* write iv Low byte alignment */ - num = iv_length / 4; - for (i = 0; i < num; i++) - aes->aes_iv[i] = *((uint32_t*)(&aes_iv[iv_length - (4 * i) - 4])); - remainder = iv_length % 4; + uint32_num = iv_len / 4; + for (i = 0; i < uint32_num; i++) + aes->aes_iv[i] = *((uint32_t *)(&iv[iv_len - (4 * i) - 4])); + remainder = iv_len % 4; if (remainder) { switch (remainder) { case 1: - u8data[0] = aes_iv[0]; + uint8_data[0] = iv[0]; break; case 2: - u8data[0] = aes_iv[0]; - u8data[1] = aes_iv[1]; + uint8_data[0] = iv[0]; + uint8_data[1] = iv[1]; break; case 3: - u8data[0] = aes_iv[0]; - u8data[1] = aes_iv[1]; - u8data[2] = aes_iv[2]; + uint8_data[0] = iv[0]; + uint8_data[1] = iv[1]; + uint8_data[2] = iv[2]; break; default: break; } - aes->aes_iv[num] = *((uint32_t*)(&u8data[0])); + aes->aes_iv[uint32_num] = *((uint32_t *)(&uint8_data[0])); } - aes->mode_ctl.kmode = key_length / 8 - 2; /* 00:AES_128 01:AES_192 10:AES_256 11:RESERVED */ - aes->mode_ctl.cipher_mode = cipher_mod; - - /* - * [1:0],set the first bit and second bit 00:ecb; 01:cbc; - * 10,11:AES_CIPHER_GCM - */ + aes->mode_ctl.kmode = input_key_len / 8 - 2; /* 00:AES_128 01:AES_192 10:AES_256 11:RESERVED */ + aes->mode_ctl.cipher_mode = cipher_mode; aes->encrypt_sel = encrypt_sel; - aes->gb_aad_end_adr = add_size - 1; - aes->gb_pc_end_adr = data_size - 1; + aes->gb_aad_end_adr = gcm_add_len - 1; + aes->gb_pc_end_adr = padding_len - 1; aes->gb_aes_en |= 1; - /* write aad */ - if (cipher_mod == AES_CIPHER_GCM) + if (cipher_mode == AES_GCM) { - num = add_size / 4; - for (i = 0; i < num; i++) + uint32_num = gcm_add_len / 4; + for (i = 0; i < uint32_num; i++) { - u32data = *((uint32_t*)(aes_aad + i * 4)); + uint32_data = *((uint32_t *)(gcm_add + i * 4)); while (!os_get_data_in_flag(userdata)) ; - os_aes_write_aad(u32data, userdata); + os_aes_write_aad(uint32_data, userdata); } - cnt = 4 * num; - remainder = add_size % 4; + uint8_num = 4 * uint32_num; + remainder = gcm_add_len % 4; if (remainder) { switch (remainder) { - case 1: - u8data[0] = aes_aad[cnt]; - break; - case 2: - u8data[0] = aes_aad[cnt]; - u8data[1] = aes_aad[cnt + 1]; - break; - case 3: - u8data[0] = aes_aad[cnt]; - u8data[1] = aes_aad[cnt + 1]; - u8data[2] = aes_aad[cnt + 2]; - break; - default: - return 0; + case 1: + uint8_data[0] = gcm_add[uint8_num]; + break; + case 2: + uint8_data[0] = gcm_add[uint8_num]; + uint8_data[1] = gcm_add[uint8_num + 1]; + break; + case 3: + uint8_data[0] = gcm_add[uint8_num]; + uint8_data[1] = gcm_add[uint8_num + 1]; + uint8_data[2] = gcm_add[uint8_num + 2]; + break; + default: + break; } - u32data = *((uint32_t*)(&u8data[0])); + uint32_data = *((uint32_t *)(&uint8_data[0])); while (!os_get_data_in_flag(userdata)) ; - os_aes_write_aad(u32data, userdata); + os_aes_write_aad(uint32_data, userdata); } } - - return 1; } -static int aes_process_less_80_bytes(uint8_t* aes_in_data, - uint8_t* aes_out_data, - uint32_t data_size, - aes_cipher_mod cipher_mod, - void* userdata) +static void aes_process_less_80_bytes(uint8_t *input_data, + uint8_t *output_data, + uint32_t input_data_len, + aes_cipher_mode_t cipher_mode, + void *userdata) { - int padding_size; - int num, i, remainder, cnt; - uint32_t u32data; - uint8_t u8data[4] = {0}; - - /* Fill 128 bits (16byte) */ - padding_size = ((data_size + 15) / 16) * 16; - - /* write text */ - num = data_size / 4; - for (i = 0; i < num; i++) + size_t padding_len, uint32_num, i, remainder, uint8_num; + uint32_t uint32_data; + uint8_t uint8_data[4] = {0}; + padding_len = ((input_data_len + 15) / 16) * 16; + uint32_num = input_data_len / 4; + for (i = 0; i < uint32_num; i++) { - u32data = *((uint32_t*)(&aes_in_data[i * 4])); + uint32_data = *((uint32_t *)(&input_data[i * 4])); while (!os_get_data_in_flag(userdata)) ; - os_aes_write_text(u32data, userdata); + os_aes_write_text(uint32_data, userdata); } - cnt = 4 * num; - remainder = data_size % 4; + uint8_num = 4 * uint32_num; + remainder = input_data_len % 4; if (remainder) { switch (remainder) { case 1: - u8data[0] = aes_in_data[cnt]; + uint8_data[0] = input_data[uint8_num]; break; case 2: - u8data[0] = aes_in_data[cnt]; - u8data[1] = aes_in_data[cnt + 1]; + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; break; case 3: - u8data[0] = aes_in_data[cnt]; - u8data[1] = aes_in_data[cnt + 1]; - u8data[2] = aes_in_data[cnt + 2]; + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; + uint8_data[2] = input_data[uint8_num + 2]; break; default: - return 0; + break; } - u32data = *((uint32_t*)(&u8data[0])); + uint32_data = *((uint32_t *)(&uint8_data[0])); while (!os_get_data_in_flag(userdata)) ; - os_aes_write_text(u32data, userdata); + os_aes_write_text(uint32_data, userdata); } - - if ((cipher_mod == AES_CIPHER_ECB) || (cipher_mod == AES_CIPHER_CBC)) + if ((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) { - /* use 0 to Fill 128 bits */ - num = (padding_size - data_size) / 4; - for (i = 0; i < num; i++) + uint32_num = (padding_len - input_data_len) / 4; + for (i = 0; i < uint32_num; i++) { while (!os_get_data_in_flag(userdata)) ; os_aes_write_text(0, userdata); } - - /* get data */ - num = padding_size / 4; + uint32_num = padding_len / 4; } - - /* get data */ - for (i = 0; i < num; i++) + for (i = 0; i < uint32_num; i++) { while (!os_get_data_out_flag(userdata)) ; - *((uint32_t*)(&aes_out_data[i * 4])) = os_read_out_data(userdata); + *((uint32_t *)(&output_data[i * 4])) = os_read_out_data(userdata); } - if ((cipher_mod == AES_CIPHER_GCM) && (remainder)) + if ((cipher_mode == AES_GCM) && (remainder)) { while (!os_get_data_out_flag(userdata)) ; - *((uint32_t*)(&u8data[0])) = os_read_out_data(userdata); + *((uint32_t *)(&uint8_data[0])) = os_read_out_data(userdata); switch (remainder) { - case 1: - aes_out_data[num * 4] = u8data[0]; - break; - case 2: - aes_out_data[num * 4] = u8data[0]; - aes_out_data[(i * 4) + 1] = u8data[1]; - break; - case 3: - aes_out_data[num * 4] = u8data[0]; - aes_out_data[(i * 4) + 1] = u8data[1]; - aes_out_data[(i * 4) + 2] = u8data[2]; - break; - default: - return 0; + case 1: + output_data[uint32_num * 4] = uint8_data[0]; + break; + case 2: + output_data[uint32_num * 4] = uint8_data[0]; + output_data[(i * 4) + 1] = uint8_data[1]; + break; + case 3: + output_data[uint32_num * 4] = uint8_data[0]; + output_data[(i * 4) + 1] = uint8_data[1]; + output_data[(i * 4) + 2] = uint8_data[2]; + break; + default: + break; } } - - return 1; } -static int os_aes_process(uint8_t* aes_in_data, - uint8_t* aes_out_data, - uint32_t data_size, - aes_cipher_mod cipher_mod, - void* userdata) +static int os_aes_process(uint8_t *input_data, + uint8_t *output_data, + size_t input_data_len, + aes_cipher_mode_t cipher_mode, + void *userdata) { + size_t temp_len, i = 0; - uint32_t i, temp_size; - - i = 0; - if (data_size >= 80) + if (input_data_len >= 80) { - for (i = 0; i < (data_size / 80); i++) - aes_process_less_80_bytes(&aes_in_data[i * 80], &aes_out_data[i * 80], 80, cipher_mod, userdata); + for (i = 0; i < (input_data_len / 80); i++) + aes_process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], 80, cipher_mode, userdata); } - temp_size = data_size % 80; - if (temp_size) - aes_process_less_80_bytes(&aes_in_data[i * 80], &aes_out_data[i * 80], temp_size, cipher_mod, userdata); + temp_len = input_data_len % 80; + if (temp_len) + aes_process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], temp_len, cipher_mode, userdata); return 1; } -static void aes_decrypt(aes_parameter* aes_param, void* userdata) +static void aes_hard_decrypt(const aes_param_t *param, void *userdata) { COMMON_ENTRY; - configASSERT(aes_param->key_length == AES_128 || aes_param->key_length == AES_192 || aes_param->key_length == AES_256); - + configASSERT(param->input_key_len == AES_128 || param->input_key_len == AES_192 || param->input_key_len == AES_256); + int padding_len = param->input_data_len; entry_exclusive(data); - os_aes_init(aes_param->key_addr, aes_param->key_length, aes_param->gcm_iv, aes_param->iv_length, aes_param->aes_aad, - aes_param->cipher_mod, AES_MODE_DECRYPTION, aes_param->add_size, aes_param->data_size, userdata); - - int padding_size = aes_param->data_size; - if (aes_param->cipher_mod == AES_CIPHER_CBC || aes_param->cipher_mod == AES_CIPHER_ECB) + os_aes_init(param->input_key, param->input_key_len, param->iv, param->iv_len, param->gcm_add, + param->cipher_mode, AES_HARD_DECRYPTION, param->gcm_add_len, param->input_data_len, userdata); + if (param->cipher_mode == AES_CBC || param->cipher_mode == AES_ECB) { - padding_size = ((padding_size + 15) / 16) * 16; + padding_len = ((padding_len + 15) / 16) * 16; } - -#if (AES_USE_DMA == 1) - uint8_t* padding_buffer = NULL; - padding_buffer = (uint8_t*)malloc(padding_size * sizeof(uint8_t)); - memset(padding_buffer, 0, padding_size); - memcpy(padding_buffer, aes_param->aes_in_data, aes_param->data_size); - - uintptr_t aes_write = dma_open_free(); - uintptr_t aes_read = dma_open_free(); - - dma_set_select_request(aes_read, SYSCTL_DMA_SELECT_AES_REQ); - - SemaphoreHandle_t event_read = xSemaphoreCreateBinary(), event_write = xSemaphoreCreateBinary(); - - dma_transmit_async(aes_read, &aes->aes_out_data, aes_param->aes_out_data, 0, 1, sizeof(uint32_t), padding_size >> 2, 4, event_read); - dma_transmit_async(aes_write, aes_param->aes_in_data, &aes->aes_text_data, 1, 0, sizeof(uint32_t), padding_size >> 2, 4, event_write); - aes->dma_sel = 1; - - configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE && xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); - - dma_close(aes_write); - dma_close(aes_read); - vSemaphoreDelete(event_read); - vSemaphoreDelete(event_write); -#else - os_aes_process(aes_param->aes_in_data, aes_param->aes_out_data, padding_size, aes_param->cipher_mod, userdata); -#endif - if (aes_param->cipher_mod == AES_CIPHER_GCM) + os_aes_process(param->input_data, param->output_data, padding_len, param->cipher_mode, userdata); + if (param->cipher_mode == AES_GCM) { - os_aes_get_tag(aes_param->tag, userdata); + os_aes_get_tag(param->gcm_tag, userdata); os_check_tag(data->aes_hardware_tag, userdata); /* haredware need this */ } -#if (AES_USE_DMA == 1) - free(padding_buffer); -#endif exit_exclusive(data); } -static void aes_encrypt(aes_parameter* aes_param, void* userdata) +static void aes_hard_encrypt(const aes_param_t *param, void *userdata) { COMMON_ENTRY; - configASSERT(aes_param->key_length == AES_128 || aes_param->key_length == AES_192 || aes_param->key_length == AES_256); - + configASSERT(param->input_key_len == AES_128 || param->input_key_len == AES_192 || param->input_key_len == AES_256); entry_exclusive(data); - os_aes_init(aes_param->key_addr, aes_param->key_length, aes_param->gcm_iv, aes_param->iv_length, aes_param->aes_aad, - aes_param->cipher_mod, AES_MODE_ENCRYPTION, aes_param->add_size, aes_param->data_size, userdata); - - int padding_size = aes_param->data_size; -#if (AES_USE_DMA == 1) - uint8_t* padding_buffer = NULL; - if (aes_param->cipher_mod == AES_CIPHER_CBC || aes_param->cipher_mod == AES_CIPHER_ECB) - { - padding_size = ((padding_size + 15) / 16) * 16; - } - padding_buffer = (uint8_t*)malloc(padding_size * sizeof(uint8_t)); - memset(padding_buffer, 0, padding_size); - memcpy(padding_buffer, aes_param->aes_in_data, aes_param->data_size); - - uintptr_t aes_read = dma_open_free(); - uintptr_t aes_write = dma_open_free(); - - dma_set_select_request(aes_read, SYSCTL_DMA_SELECT_AES_REQ); - - SemaphoreHandle_t event_read = xSemaphoreCreateBinary(), event_write = xSemaphoreCreateBinary(); - aes->dma_sel = 1; - dma_transmit_async(aes_read, &aes->aes_out_data, aes_param->aes_out_data, 0, 1, sizeof(uint32_t), padding_size >> 2, 4, event_read); - dma_transmit_async(aes_write, padding_buffer, &aes->aes_text_data, 1, 0, sizeof(uint32_t), padding_size >> 2, 4, event_write); - - configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE && xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); - - dma_close(aes_write); - dma_close(aes_read); - vSemaphoreDelete(event_read); - vSemaphoreDelete(event_write); -#else - os_aes_process(aes_param->aes_in_data, aes_param->aes_out_data, aes_param->data_size, aes_param->cipher_mod, userdata); -#endif - if (aes_param->cipher_mod == AES_CIPHER_GCM) + os_aes_init(param->input_key, param->input_key_len, param->iv, param->iv_len, param->gcm_add, + param->cipher_mode, AES_HARD_ENCRYPTION, param->gcm_add_len, param->input_data_len, userdata); + os_aes_process(param->input_data, param->output_data, param->input_data_len, param->cipher_mode, userdata); + if (param->cipher_mode == AES_GCM) { - os_aes_get_tag(aes_param->tag, userdata); + os_aes_get_tag(param->gcm_tag, userdata); os_check_tag(data->aes_hardware_tag, userdata); /* haredware need this */ } -#if (AES_USE_DMA == 1) - free(padding_buffer); -#endif exit_exclusive(data); } -static aes_dev_data dev0_data = {AES_BASE_ADDR, 0, {{0}}}; +static aes_dev_data dev0_data = {SYSCTL_CLOCK_AES, AES_BASE_ADDR, 0, {{0}}}; -const aes_driver_t g_aes_driver_aes0 = {{&dev0_data, aes_install, aes_open, aes_close}, aes_decrypt, aes_encrypt}; +const aes_driver_t g_aes_driver_aes0 = {{&dev0_data, aes_install, aes_open, aes_close}, aes_hard_decrypt, aes_hard_encrypt}; diff --git a/lib/bsp/device/dmac.c b/lib/bsp/device/dmac.c index 281cc08..3c9e94e 100644 --- a/lib/bsp/device/dmac.c +++ b/lib/bsp/device/dmac.c @@ -27,9 +27,9 @@ /* DMAC */ -#define COMMON_ENTRY \ - dmac_data* data = (dmac_data*)userdata; \ - volatile struct dmac_t* dmac = (volatile struct dmac_t*)data->base_addr; +#define COMMON_ENTRY \ + dmac_data *data = (dmac_data *)userdata; \ + volatile dmac_t *dmac = (volatile dmac_t *)data->base_addr; typedef struct { @@ -125,7 +125,7 @@ const dmac_driver_t g_dmac_driver_dmac0 = {{&dev0_data, dmac_install, dmac_open, #define C_COMMON_ENTRY \ dma_data* data = (dma_data*)userdata; \ dmac_data* dmacdata = data->dmac_data; \ - volatile struct dmac_t* dmac = (volatile struct dmac_t*)dmacdata->base_addr; \ + volatile dmac_t* dmac = (volatile dmac_t*)dmacdata->base_addr; \ (void)dmac; \ volatile struct dmac_channel_t* dma = (volatile struct dmac_channel_t*)dmac->channel + data->channel; \ (void)dma; @@ -140,7 +140,7 @@ typedef struct { SemaphoreHandle_t completion_event; uint32_t axi_master; - int is_ping_pong; + int is_loop; union { struct @@ -160,7 +160,7 @@ typedef struct size_t dest_num; size_t next_src_id; size_t next_dest_id; - dma_stage_completion_handler stage_completion_handler; + dma_stage_completion_handler_t stage_completion_handler; void* stage_completion_handler_data; int* stop_signal; }; @@ -177,7 +177,7 @@ static void dma_completion_isr(void* userdata) BaseType_t xHigherPriorityTaskWoken = pdFALSE; - if (data->session.is_ping_pong) + if (data->session.is_loop) { if (atomic_read(data->session.stop_signal)) { @@ -338,7 +338,7 @@ static int is_memory(uintptr_t address) return ((address >= 0x80000000) && (address < 0x80000000 + mem_len)) || ((address >= 0x40000000) && (address < 0x40000000 + mem_len)) || (address == 0x50450040); } -static void dma_loop_async_imp(const volatile void** srcs, size_t src_num, volatile void** dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler stage_completion_handler, void* stage_completion_handler_data, SemaphoreHandle_t completion_event, int* stop_signal, void* userdata) +static void dma_loop_async_imp(const volatile void** srcs, size_t src_num, volatile void** dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void* stage_completion_handler_data, SemaphoreHandle_t completion_event, int* stop_signal, void* userdata) { C_COMMON_ENTRY; @@ -385,7 +385,7 @@ static void dma_loop_async_imp(const volatile void** srcs, size_t src_num, volat writeq(cfg_u.data, &dma->cfg); - data->session.is_ping_pong = 1; + data->session.is_loop = 1; data->session.flow_control = flow_control; dma->sar = (uint64_t)srcs[0]; @@ -521,7 +521,7 @@ static void dma_transmit_async_imp(const volatile void* src, volatile void* dest writeq(cfg_u.data, &dma->cfg); - data->session.is_ping_pong = 0; + data->session.is_loop = 0; data->session.flow_control = flow_control; size_t old_elm_size = element_size; diff --git a/lib/bsp/device/dvp.c b/lib/bsp/device/dvp.c index 7d1c6c8..9e519b0 100644 --- a/lib/bsp/device/dvp.c +++ b/lib/bsp/device/dvp.c @@ -36,7 +36,7 @@ typedef struct struct { - dvp_on_frame_event frame_event_callback; + dvp_on_frame_event_t frame_event_callback; void* frame_event_callback_data; size_t width; size_t height; @@ -63,7 +63,7 @@ static void dvp_close(void* userdata) { } -static void dvp_config(size_t width, size_t height, int auto_enable, void* userdata) +static void dvp_config(uint32_t width, uint32_t height, int auto_enable, void* userdata) { COMMON_ENTRY; configASSERT(width % 8 == 0 && width && height); @@ -100,7 +100,7 @@ static void dvp_enable_frame(void* userdata) dvp->sts = DVP_STS_DVP_EN | DVP_STS_DVP_EN_WE; } -static void dvp_set_signal(dvp_signal_type type, int value, void* userdata) +static void dvp_set_signal(dvp_signal_type_t type, int value, void* userdata) { COMMON_ENTRY; switch (type) @@ -123,7 +123,7 @@ static void dvp_set_signal(dvp_signal_type type, int value, void* userdata) } } -static void dvp_set_output_enable(size_t index, int enable, void* userdata) +static void dvp_set_output_enable(uint32_t index, int enable, void* userdata) { COMMON_ENTRY; configASSERT(index < 2); @@ -144,14 +144,14 @@ static void dvp_set_output_enable(size_t index, int enable, void* userdata) } } -static void dvp_set_output_attributes(size_t index, video_format format, void* output_buffer, void* userdata) +static void dvp_set_output_attributes(uint32_t index, video_format_t format, void* output_buffer, void* userdata) { COMMON_ENTRY; configASSERT(index < 2); if (index == 0) { - configASSERT(format == VIDEO_FMT_RGB24Planar); + configASSERT(format == VIDEO_FMT_RGB24_PLANAR); uintptr_t buffer_addr = (uintptr_t)output_buffer; size_t planar_size = data->width * data->height; dvp->r_addr = buffer_addr; @@ -171,21 +171,21 @@ static void dvp_frame_event_isr(void* userdata) if (dvp->sts & DVP_STS_FRAME_START) { - dvp_on_frame_event callback; + dvp_on_frame_event_t callback; if ((callback = data->frame_event_callback)) callback(VIDEO_FE_BEGIN, data->frame_event_callback_data); dvp->sts |= DVP_STS_FRAME_START | DVP_STS_FRAME_START_WE; } if (dvp->sts & DVP_STS_FRAME_FINISH) { - dvp_on_frame_event callback; + dvp_on_frame_event_t callback; if ((callback = data->frame_event_callback)) callback(VIDEO_FE_END, data->frame_event_callback_data); dvp->sts |= DVP_STS_FRAME_FINISH | DVP_STS_FRAME_FINISH_WE; } } -static void dvp_set_frame_event_enable(video_frame_event event, int enable, void* userdata) +static void dvp_set_frame_event_enable(dvp_frame_event_t event, int enable, void* userdata) { COMMON_ENTRY; switch (event) @@ -220,7 +220,7 @@ static void dvp_set_frame_event_enable(video_frame_event event, int enable, void pic_set_irq_enable(IRQN_DVP_INTERRUPT, 1); } -static void dvp_set_on_frame_event(dvp_on_frame_event callback, void* callback_data, void* userdata) +static void dvp_set_on_frame_event(dvp_on_frame_event_t callback, void* callback_data, void* userdata) { COMMON_ENTRY; diff --git a/lib/bsp/device/fft.c b/lib/bsp/device/fft.c index 6204b79..9f51dd0 100644 --- a/lib/bsp/device/fft.c +++ b/lib/bsp/device/fft.c @@ -15,105 +15,91 @@ #include #include #include -#include -#include -#include -#include -#include +#include #include #define FFT_BASE_ADDR (0x42000000) -#define FFT_RESULT_ADDR (0x42100000 + 0x2000) -#define COMMON_ENTRY \ - fft_dev_data* data = (fft_dev_data*)userdata; \ - volatile fft_t* fft = (volatile fft_t*)data->base_addr; +#define COMMON_ENTRY \ + fft_dev_data *data = (fft_dev_data *)userdata; \ + volatile fft_t *fft = (volatile fft_t *)data->base_addr; typedef struct { + enum sysctl_clock_e clock; uintptr_t base_addr; SemaphoreHandle_t free_mutex; } fft_dev_data; -static void fft_install(void* userdata) +static void fft_install(void *userdata) { COMMON_ENTRY; - sysctl_clock_enable(SYSCTL_CLOCK_FFT); - sysctl_reset(SYSCTL_RESET_FFT); + sysctl_clock_enable(data->clock); data->free_mutex = xSemaphoreCreateMutex(); - fft->intr_clear.fft_done_clear = 1; fft->intr_mask.fft_done_mask = 0; } -static int fft_open(void* userdata) +static int fft_open(void *userdata) { return 1; } -static void fft_close(void* userdata) +static void fft_close(void *userdata) { } -static void entry_exclusive(fft_dev_data* data) +static void entry_exclusive(fft_dev_data *data) { configASSERT(xSemaphoreTake(data->free_mutex, portMAX_DELAY) == pdTRUE); } -static void exit_exclusive(fft_dev_data* data) +static void exit_exclusive(fft_dev_data *data) { xSemaphoreGive(data->free_mutex); } -static void fft_complex_uint16(fft_point point, fft_direction direction, uint32_t shifts_mask, const uint16_t* input, uint16_t* output, void* userdata) +static void fft_complex_uint16(fft_direction_t direction, const uint64_t *input, size_t point_num, uint64_t *output, void *userdata) { COMMON_ENTRY; - configASSERT(((uintptr_t)input) % 8 == 0 && ((uintptr_t)output) % 8 == 0); - configASSERT(shifts_mask <= 0x1ff); - entry_exclusive(data); - + uint16_t shift = (direction==FFT_DIR_FORWARD) ? 0x1ff : 0x0; + fft_point_t point = FFT_512; + switch(point_num) + { + case 512: + point = FFT_512; + break; + case 256: + point = FFT_256; + break; + case 128: + point = FFT_128; + break; + case 64: + point = FFT_64; + break; + default: + configASSERT(!"Invalid fft point"); + break; + } fft_fft_ctrl_t ctl = fft->fft_ctrl; ctl.dma_send = 1; ctl.fft_input_mode = 0; ctl.fft_data_mode = 0; ctl.fft_point = point; ctl.fft_mode = direction; - ctl.fft_shift = shifts_mask; + ctl.fft_shift = shift; ctl.fft_enable = 1; fft->fft_ctrl = ctl; - size_t blocks = 0; - switch (point) - { - case FFT_512: - blocks = 256; - break; - case FFT_256: - blocks = 128; - break; - case FFT_128: - blocks = 64; - break; - case FFT_64: - blocks = 32; - break; - default: - configASSERT(!"Invalid fft point"); - break; - } - uintptr_t dma_write = dma_open_free(); uintptr_t dma_read = dma_open_free(); - - dma_set_select_request(dma_write, SYSCTL_DMA_SELECT_FFT_TX_REQ); - dma_set_select_request(dma_read, SYSCTL_DMA_SELECT_FFT_RX_REQ); - + dma_set_request_source(dma_write, SYSCTL_DMA_SELECT_FFT_TX_REQ); + dma_set_request_source(dma_read, SYSCTL_DMA_SELECT_FFT_RX_REQ); SemaphoreHandle_t event_read = xSemaphoreCreateBinary(), event_write = xSemaphoreCreateBinary(); - - dma_transmit_async(dma_read, &fft->fft_output_fifo, output, 0, 1, sizeof(uint64_t), blocks, 4, event_read); - dma_transmit_async(dma_write, input, &fft->fft_input_fifo, 1, 0, sizeof(uint64_t), blocks, 4, event_write); - + dma_transmit_async(dma_read, &fft->fft_output_fifo, output, 0, 1, sizeof(uint64_t), point_num>>1, 4, event_read); + dma_transmit_async(dma_write, input, &fft->fft_input_fifo, 1, 0, sizeof(uint64_t), point_num>>1, 4, event_write); configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE && xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); dma_close(dma_write); @@ -124,6 +110,6 @@ static void fft_complex_uint16(fft_point point, fft_direction direction, uint32_ exit_exclusive(data); } -static fft_dev_data dev0_data = {FFT_BASE_ADDR, 0}; +static fft_dev_data dev0_data = {SYSCTL_CLOCK_FFT, FFT_BASE_ADDR, 0}; const fft_driver_t g_fft_driver_fft0 = {{&dev0_data, fft_install, fft_open, fft_close}, fft_complex_uint16}; diff --git a/lib/bsp/device/gpio.c b/lib/bsp/device/gpio.c index 24b5f25..4c1813b 100644 --- a/lib/bsp/device/gpio.c +++ b/lib/bsp/device/gpio.c @@ -24,14 +24,14 @@ #define COMMON_ENTRY \ gpio_data* data = (gpio_data*)userdata; \ - volatile gpio_t* gpio = (volatile gpio_t*)data->base_addr; \ + volatile gpio_t* gpio = (volatile gpio_t*)data->base_addr; \ configASSERT(pin < data->pin_count); \ (void)data; \ (void)gpio; typedef struct { - size_t pin_count; + uint32_t pin_count; uintptr_t base_addr; } gpio_data; @@ -52,7 +52,7 @@ static void gpio_close(void* userdata) { } -static void gpio_set_drive_mode(void* userdata, size_t pin, gpio_drive_mode mode) +static void gpio_set_drive_mode(uint32_t pin, gpio_drive_mode_t mode, void* userdata) { COMMON_ENTRY; int io_number = fpioa_get_io_by_func(FUNC_GPIO0 + pin); @@ -80,26 +80,27 @@ static void gpio_set_drive_mode(void* userdata, size_t pin, gpio_drive_mode mode dir = 1; break; default: - configASSERT(!"GPIO drive mode is not supported.") break; + configASSERT(!"GPIO drive mode is not supported."); + break; } fpioa_set_io_pull(io_number, pull); set_bit_idx(gpio->direction.u32, pin, dir); } -static void gpio_set_pin_edge(void* userdata, size_t pin, gpio_pin_edge edge) +static void gpio_set_pin_edge(uint32_t pin, gpio_pin_edge_t edge, void* userdata) { COMMON_ENTRY; - configASSERT(!"Not supported.") + configASSERT(!"Not supported."); } -static void gpio_set_onchanged(void* userdata, size_t pin, gpio_onchanged callback, void* callback_data) +static void gpio_set_on_changed(uint32_t pin, gpio_on_changed_t callback, void* callback_data, void* userdata) { COMMON_ENTRY; - configASSERT(!"Not supported.") + configASSERT(!"Not supported."); } -static gpio_pin_value gpio_get_pin_value(void* userdata, size_t pin) +static gpio_pin_value_t gpio_get_pin_value(uint32_t pin, void* userdata) { COMMON_ENTRY; @@ -108,7 +109,7 @@ static gpio_pin_value gpio_get_pin_value(void* userdata, size_t pin) return get_bit_idx(reg, pin); } -static void gpio_set_pin_value(void* userdata, size_t pin, gpio_pin_value value) +static void gpio_set_pin_value(uint32_t pin, gpio_pin_value_t value, void* userdata) { COMMON_ENTRY; @@ -120,4 +121,4 @@ static void gpio_set_pin_value(void* userdata, size_t pin, gpio_pin_value value) static gpio_data dev0_data = {8, GPIO_BASE_ADDR}; -const gpio_driver_t g_gpio_driver_gpio0 = {{&dev0_data, gpio_install, gpio_open, gpio_close}, 8, gpio_set_drive_mode, gpio_set_pin_edge, gpio_set_onchanged, gpio_set_pin_value, gpio_get_pin_value}; +const gpio_driver_t g_gpio_driver_gpio0 = {{&dev0_data, gpio_install, gpio_open, gpio_close}, 8, gpio_set_drive_mode, gpio_set_pin_edge, gpio_set_on_changed, gpio_set_pin_value, gpio_get_pin_value}; diff --git a/lib/bsp/device/gpiohs.c b/lib/bsp/device/gpiohs.c index 55d2179..923e100 100644 --- a/lib/bsp/device/gpiohs.c +++ b/lib/bsp/device/gpiohs.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -34,21 +35,21 @@ typedef struct { - size_t pin_count; + uint32_t pin_count; uintptr_t base_addr; struct gpiohs_pin_context { void* gpio_userdata; - size_t pin; - gpio_pin_edge edge; - gpio_onchanged callback; + uint32_t pin; + gpio_pin_edge_t edge; + gpio_on_changed_t callback; void* userdata; } pin_context[32]; } gpiohs_data; -static void gpiohs_pin_onchange_isr(void* userdata); +static void gpiohs_pin_on_change_isr(void* userdata); static void gpiohs_install(void* userdata) { @@ -59,14 +60,14 @@ static void gpiohs_install(void* userdata) gpiohs->fall_ie.u32[0] = 0; gpiohs->fall_ip.u32[0] = 0xFFFFFFFF; - size_t i; + uint32_t i; for (i = 0; i < 32; i++) { data->pin_context[i].gpio_userdata = data; data->pin_context[i].pin = i; data->pin_context[i].callback = NULL; data->pin_context[i].userdata = NULL; - pic_set_irq_handler(IRQN_GPIOHS0_INTERRUPT + i, gpiohs_pin_onchange_isr, data->pin_context + i); + pic_set_irq_handler(IRQN_GPIOHS0_INTERRUPT + i, gpiohs_pin_on_change_isr, data->pin_context + i); pic_set_irq_priority(IRQN_GPIOHS0_INTERRUPT + i, 1); } } @@ -80,18 +81,7 @@ static void gpiohs_close(void* userdata) { } -static uint32_t get_bit(volatile uint32_t* bits, size_t idx) -{ - return ((*bits) & (1 << idx)) >> idx; -} - -static void set_bit(volatile uint32_t* bits, size_t idx, uint32_t value) -{ - uint32_t org = (*bits) & ~(1 << idx); - *bits = org | (value << idx); -} - -static void gpiohs_set_drive_mode(void* userdata, size_t pin, gpio_drive_mode mode) +static void gpiohs_set_drive_mode(uint32_t pin, gpio_drive_mode_t mode, void* userdata) { COMMON_ENTRY; int io_number = fpioa_get_io_by_func(FUNC_GPIOHS0 + pin); @@ -125,11 +115,11 @@ static void gpiohs_set_drive_mode(void* userdata, size_t pin, gpio_drive_mode mo fpioa_set_io_pull(io_number, pull); volatile uint32_t* reg = dir ? gpiohs->output_en.u32 : gpiohs->input_en.u32; volatile uint32_t* reg_d = !dir ? gpiohs->output_en.u32 : gpiohs->input_en.u32; - set_bit(reg_d, pin, 0); - set_bit(reg, pin, 1); + set_bit_idx(reg_d, pin, 0); + set_bit_idx(reg, pin, 1); } -void gpiohs_set_pin_edge(void* userdata, size_t pin, gpio_pin_edge edge) +void gpiohs_set_pin_edge(uint32_t pin, gpio_pin_edge_t edge, void* userdata) { COMMON_ENTRY; @@ -156,18 +146,18 @@ void gpiohs_set_pin_edge(void* userdata, size_t pin, gpio_pin_edge edge) } data->pin_context[pin].edge = edge; - set_bit(gpiohs->rise_ie.u32, pin, rise); - set_bit(gpiohs->fall_ie.u32, pin, fall); + set_bit_idx(gpiohs->rise_ie.u32, pin, rise); + set_bit_idx(gpiohs->fall_ie.u32, pin, fall); pic_set_irq_enable(IRQN_GPIOHS0_INTERRUPT + pin, irq); } -static void gpiohs_pin_onchange_isr(void* userdata) +static void gpiohs_pin_on_change_isr(void* userdata) { struct gpiohs_pin_context* ctx = (struct gpiohs_pin_context*)userdata; gpiohs_data* data = (gpiohs_data*)ctx->gpio_userdata; volatile gpiohs_t* gpiohs = (volatile gpiohs_t*)data->base_addr; - size_t pin = ctx->pin; + uint32_t pin = ctx->pin; uint32_t rise = 0, fall = 0; switch (ctx->edge) { @@ -192,41 +182,41 @@ static void gpiohs_pin_onchange_isr(void* userdata) if (rise) { - set_bit(gpiohs->rise_ie.u32, pin, 0); - set_bit(gpiohs->rise_ip.u32, pin, 1); - set_bit(gpiohs->rise_ie.u32, pin, 1); + set_bit_idx(gpiohs->rise_ie.u32, pin, 0); + set_bit_idx(gpiohs->rise_ip.u32, pin, 1); + set_bit_idx(gpiohs->rise_ie.u32, pin, 1); } if (fall) { - set_bit(gpiohs->fall_ie.u32, pin, 0); - set_bit(gpiohs->fall_ip.u32, pin, 1); - set_bit(gpiohs->fall_ie.u32, pin, 1); + set_bit_idx(gpiohs->fall_ie.u32, pin, 0); + set_bit_idx(gpiohs->fall_ip.u32, pin, 1); + set_bit_idx(gpiohs->fall_ie.u32, pin, 1); } if (ctx->callback) ctx->callback(ctx->pin, ctx->userdata); } -void gpiohs_set_onchanged(void* userdata, size_t pin, gpio_onchanged callback, void* callback_data) +void gpiohs_set_on_changed(uint32_t pin, gpio_on_changed_t callback, void* callback_data, void* userdata) { COMMON_ENTRY; data->pin_context[pin].userdata = callback_data; data->pin_context[pin].callback = callback; } -gpio_pin_value gpiohs_get_pin_value(void* userdata, size_t pin) +gpio_pin_value_t gpiohs_get_pin_value(uint32_t pin, void* userdata) { COMMON_ENTRY; - return get_bit(gpiohs->input_val.u32, pin); + return get_bit_idx(gpiohs->input_val.u32, pin); } -void gpiohs_set_pin_value(void* userdata, size_t pin, gpio_pin_value value) +void gpiohs_set_pin_value(uint32_t pin, gpio_pin_value_t value, void* userdata) { COMMON_ENTRY; - set_bit(gpiohs->output_val.u32, pin, value); + set_bit_idx(gpiohs->output_val.u32, pin, value); } static gpiohs_data dev0_data = {32, GPIOHS_BASE_ADDR, {{0}}}; -const gpio_driver_t g_gpiohs_driver_gpio0 = {{&dev0_data, gpiohs_install, gpiohs_open, gpiohs_close}, 32, gpiohs_set_drive_mode, gpiohs_set_pin_edge, gpiohs_set_onchanged, gpiohs_set_pin_value, gpiohs_get_pin_value}; +const gpio_driver_t g_gpiohs_driver_gpio0 = {{&dev0_data, gpiohs_install, gpiohs_open, gpiohs_close}, 32, gpiohs_set_drive_mode, gpiohs_set_pin_edge, gpiohs_set_on_changed, gpiohs_set_pin_value, gpiohs_get_pin_value}; diff --git a/lib/bsp/device/i2c.c b/lib/bsp/device/i2c.c index afdf4bc..e7e084e 100644 --- a/lib/bsp/device/i2c.c +++ b/lib/bsp/device/i2c.c @@ -41,7 +41,7 @@ typedef struct struct { SemaphoreHandle_t free_mutex; - i2c_slave_handler slave_handler; + i2c_slave_handler_t slave_handler; }; } i2c_data; @@ -75,17 +75,17 @@ typedef struct i2c_data* i2c_data; size_t slave_address; size_t address_width; - i2c_bus_speed_mode bus_speed_mode; + i2c_bus_speed_mode_t bus_speed_mode; } i2c_dev_data; static void i2c_dev_install(void* userdata); static int i2c_dev_open(void* userdata); static void i2c_dev_close(void* userdata); -static int i2c_dev_read(char* buffer, size_t len, void* userdata); -static int i2c_dev_write(const char* buffer, size_t len, void* userdata); -static int i2c_dev_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); +static int i2c_dev_read(uint8_t* buffer, size_t len, void* userdata); +static int i2c_dev_write(const uint8_t* buffer, size_t len, void* userdata); +static int i2c_dev_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata); -static i2c_device_driver_t* i2c_get_device(size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, void* userdata) +static i2c_device_driver_t* i2c_get_device(uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, void* userdata) { i2c_device_driver_t* driver = (i2c_device_driver_t*)malloc(sizeof(i2c_device_driver_t)); memset(driver, 0, sizeof(i2c_device_driver_t)); @@ -106,7 +106,7 @@ static i2c_device_driver_t* i2c_get_device(size_t slave_address, size_t address_ return driver; } -static void i2c_config_as_master(size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, void* userdata) +static void i2c_config_as_master(uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, void* userdata) { configASSERT(address_width == 7 || address_width == 10); COMMON_ENTRY; @@ -136,7 +136,7 @@ static void i2c_config_as_master(size_t slave_address, size_t address_width, i2c i2c->enable = I2C_ENABLE_ENABLE; } -static int i2c_read(char* buffer, size_t len, void* userdata) +static int i2c_read(uint8_t* buffer, size_t len, void* userdata) { COMMON_ENTRY; @@ -168,13 +168,13 @@ static int i2c_read(char* buffer, size_t len, void* userdata) return read; } -static int i2c_write(const char* buffer, size_t len, void* userdata) +static int i2c_write(const uint8_t* buffer, size_t len, void* userdata) { COMMON_ENTRY; uintptr_t dma_write = dma_open_free(); - dma_set_select_request(dma_write, data->dma_req_base + 1); + dma_set_request_source(dma_write, data->dma_req_base + 1); dma_transmit(dma_write, buffer, &i2c->data_cmd, 1, 0, 1, len, 4); dma_close(dma_write); @@ -186,7 +186,7 @@ static int i2c_write(const char* buffer, size_t len, void* userdata) return len; } -static int i2c_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int i2c_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_ENTRY; @@ -201,8 +201,8 @@ static int i2c_transfer_sequential(const char* write_buffer, size_t write_len, c uintptr_t dma_read = dma_open_free(); SemaphoreHandle_t event_read = xSemaphoreCreateBinary(), event_write = xSemaphoreCreateBinary(); - dma_set_select_request(dma_write, data->dma_req_base + 1); - dma_set_select_request(dma_read, data->dma_req_base); + dma_set_request_source(dma_write, data->dma_req_base + 1); + dma_set_request_source(dma_read, data->dma_req_base); dma_transmit_async(dma_read, &i2c->data_cmd, read_buffer, 0, 1, 1 /*sizeof(uint32_t)*/, read_len, 1 /*4*/, event_read); dma_transmit_async(dma_write, write_cmd, &i2c->data_cmd, 1, 0, sizeof(uint32_t), write_len + read_len, 4, event_write); @@ -243,7 +243,7 @@ static void i2c_dev_close(void* userdata) { } -static int i2c_dev_read(char* buffer, size_t len, void* userdata) +static int i2c_dev_read(uint8_t* buffer, size_t len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -252,7 +252,7 @@ static int i2c_dev_read(char* buffer, size_t len, void* userdata) return ret; } -static int i2c_dev_write(const char* buffer, size_t len, void* userdata) +static int i2c_dev_write(const uint8_t* buffer, size_t len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -261,7 +261,7 @@ static int i2c_dev_write(const char* buffer, size_t len, void* userdata) return ret; } -static int i2c_dev_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int i2c_dev_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -300,7 +300,7 @@ static void on_i2c_irq(void* userdata) (void)dummy; } -static void i2c_config_as_slave(size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, i2c_slave_handler* handler, void* userdata) +static void i2c_config_as_slave(uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, i2c_slave_handler_t* handler, void* userdata) { configASSERT(address_width == 7 || address_width == 10); COMMON_ENTRY; @@ -312,7 +312,7 @@ static void i2c_config_as_slave(size_t slave_address, size_t address_width, i2c_ speed_mode = 1; break; default: - configASSERT(!"I2C bus speed is not supported."); + configASSERT(!"I2C bus clock_rate is not supported."); break; } diff --git a/lib/bsp/device/i2s.c b/lib/bsp/device/i2s.c index 08b7bd4..68e908e 100644 --- a/lib/bsp/device/i2s.c +++ b/lib/bsp/device/i2s.c @@ -44,7 +44,7 @@ typedef struct struct { i2s_transmit transmit; - char* buffer; + uint8_t* buffer; size_t buffer_frames; size_t buffer_size; size_t block_align; @@ -165,7 +165,7 @@ static void extract_params(const audio_format_t* format, enum word_select_cycles } } -static void i2s_config_as_render(const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask, void* userdata) +static void i2s_config_as_render(const audio_format_t* format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void* userdata) { COMMON_ENTRY; @@ -262,7 +262,7 @@ static void i2s_config_as_render(const audio_format_t* format, size_t delay_ms, configASSERT(data->buffer_frames >= 100); free(data->buffer); data->buffer_size = data->block_align * data->buffer_frames; - data->buffer = (char*)malloc(data->buffer_size * BUFFER_COUNT); + data->buffer = (uint8_t*)malloc(data->buffer_size * BUFFER_COUNT); data->buffer_ptr = 0; data->next_free_buffer = 0; data->stop_signal = 0; @@ -270,7 +270,7 @@ static void i2s_config_as_render(const audio_format_t* format, size_t delay_ms, data->dma_in_use_buffer = -1; } -static void i2s_config_as_capture(const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask, void* userdata) +static void i2s_config_as_capture(const audio_format_t* format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void* userdata) { COMMON_ENTRY; @@ -369,7 +369,7 @@ static void i2s_config_as_capture(const audio_format_t* format, size_t delay_ms, configASSERT(data->buffer_frames >= 100); free(data->buffer); data->buffer_size = data->block_align * data->buffer_frames; - data->buffer = (char*)malloc(data->buffer_size * BUFFER_COUNT); + data->buffer = (uint8_t*)malloc(data->buffer_size * BUFFER_COUNT); data->buffer_ptr = 0; data->next_free_buffer = 0; data->stop_signal = 0; @@ -377,7 +377,7 @@ static void i2s_config_as_capture(const audio_format_t* format, size_t delay_ms, data->dma_in_use_buffer = 0; } -static void i2s_get_buffer(char** buffer, size_t* frames, void* userdata) +static void i2s_get_buffer(uint8_t** buffer, size_t* frames, void* userdata) { COMMON_ENTRY; @@ -462,7 +462,7 @@ static void i2s_start(void* userdata) data->stop_signal = 0; data->transmit_dma = dma_open_free(); - dma_set_select_request(data->transmit_dma, data->dma_req_base - 1); + dma_set_request_source(data->transmit_dma, data->dma_req_base - 1); data->dma_in_use_buffer = 0; data->stage_completion_event = xSemaphoreCreateCounting(100, 0); data->completion_event = xSemaphoreCreateBinary(); @@ -481,7 +481,7 @@ static void i2s_start(void* userdata) data->stop_signal = 0; data->transmit_dma = dma_open_free(); - dma_set_select_request(data->transmit_dma, data->dma_req_base); + dma_set_request_source(data->transmit_dma, data->dma_req_base); data->dma_in_use_buffer = 0; data->stage_completion_event = xSemaphoreCreateCounting(100, 0); data->completion_event = xSemaphoreCreateBinary(); diff --git a/lib/bsp/device/plic.c b/lib/bsp/device/plic.c index edb9702..0058c29 100644 --- a/lib/bsp/device/plic.c +++ b/lib/bsp/device/plic.c @@ -56,7 +56,7 @@ static void plic_close(void* userdata) { } -static void plic_set_irq_enable(size_t irq, int enable, void* userdata) +static void plic_set_irq_enable(uint32_t irq, int enable, void* userdata) { configASSERT(irq <= PLIC_NUM_SOURCES); @@ -71,7 +71,7 @@ static void plic_set_irq_enable(size_t irq, int enable, void* userdata) plic->target_enables.target[0].enable[irq / 32] = current; } -static void plic_set_irq_priority(size_t irq, size_t priority, void* userdata) +static void plic_set_irq_priority(uint32_t irq, uint32_t priority, void* userdata) { configASSERT(irq <= PLIC_NUM_SOURCES); /* Set interrupt priority by IRQ number */ diff --git a/lib/bsp/device/pwm.c b/lib/bsp/device/pwm.c index 2422f66..534b95d 100644 --- a/lib/bsp/device/pwm.c +++ b/lib/bsp/device/pwm.c @@ -33,7 +33,7 @@ typedef struct { enum sysctl_clock_e clock; uintptr_t base_addr; - size_t pin_count; + uint32_t pin_count; struct { uint32_t periods; @@ -67,7 +67,7 @@ static double pwm_set_frequency(double frequency, void* userdata) return frequency; } -static double pwm_set_active_duty_cycle_percentage(size_t pin, double duty_cycle_percentage, void* userdata) +static double pwm_set_active_duty_cycle_percentage(uint32_t pin, double duty_cycle_percentage, void* userdata) { COMMON_ENTRY; configASSERT(pin < data->pin_count); @@ -79,7 +79,7 @@ static double pwm_set_active_duty_cycle_percentage(size_t pin, double duty_cycle return percent / 100.0; } -static void pwm_set_enable(size_t pin, int enable, void* userdata) +static void pwm_set_enable(uint32_t pin, int enable, void* userdata) { COMMON_ENTRY; configASSERT(pin < data->pin_count); diff --git a/lib/bsp/device/registry.c b/lib/bsp/device/registry.c index 249a070..cc5b1b4 100644 --- a/lib/bsp/device/registry.c +++ b/lib/bsp/device/registry.c @@ -62,6 +62,11 @@ extern const pwm_driver_t g_pwm_driver_pwm0; extern const pwm_driver_t g_pwm_driver_pwm1; extern const pwm_driver_t g_pwm_driver_pwm2; +extern const wdt_driver_t g_wdt_driver_wdt0; +extern const wdt_driver_t g_wdt_driver_wdt1; + +extern const rtc_driver_t g_rtc_driver_rtc0; + driver_registry_t g_system_drivers[] = { {"/dev/uart1", &g_uart_driver_uart0, DRIVER_UART}, @@ -109,6 +114,11 @@ driver_registry_t g_system_drivers[] = {"/dev/pwm0", &g_pwm_driver_pwm0, DRIVER_PWM}, {"/dev/pwm1", &g_pwm_driver_pwm1, DRIVER_PWM}, {"/dev/pwm2", &g_pwm_driver_pwm2, DRIVER_PWM}, + + {"/dev/wdt0", &g_wdt_driver_wdt0, DRIVER_WDT}, + {"/dev/wdt1", &g_wdt_driver_wdt1, DRIVER_WDT}, + + {"/dev/rtc0", &g_rtc_driver_rtc0, DRIVER_RTC}, {0} }; diff --git a/lib/bsp/device/rtc.c b/lib/bsp/device/rtc.c new file mode 100644 index 0000000..24f9f19 --- /dev/null +++ b/lib/bsp/device/rtc.c @@ -0,0 +1,317 @@ +/* Copyright 2018 Canaan Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define COMMON_ENTRY \ + rtc_data* data = (rtc_data*)userdata; \ + volatile rtc_t* rtc = (volatile rtc_t*)data->base_addr; \ + (void)rtc; + +typedef struct +{ + uintptr_t base_addr; + enum sysctl_clock_e clock; +} rtc_data; + +static int rtc_in_range(int value, int min, int max); +static int rtc_get_wday(int year, int month, int day); +static int rtc_year_is_leap(int year); +static int rtc_get_yday(int year, int month, int day); +static int rtc_timer_set_mode(volatile rtc_t* rtc, rtc_timer_mode_e timer_mode); +static int rtc_protect_set(volatile rtc_t* rtc, int enable); +static int rtc_timer_set_clock_frequency(volatile rtc_t* rtc, unsigned int frequency); +static int rtc_timer_set_clock_count_value(volatile rtc_t* rtc, unsigned int count); + +static void rtc_install(void* userdata) +{ + COMMON_ENTRY; + sysctl_clock_enable(data->clock); + + rtc_timer_set_mode(rtc, RTC_TIMER_SETTING); + /* Unprotect RTC */ + rtc_protect_set(rtc, 0); + /* Set RTC clock frequency */ + rtc_timer_set_clock_frequency(rtc, sysctl_clock_source_get_freq(SYSCTL_SOURCE_IN0)); + rtc_timer_set_clock_count_value(rtc, 1); + + /* Set RTC mode to timer running mode */ + rtc_timer_set_mode(rtc, RTC_TIMER_RUNNING); +} + +static int rtc_open(void* userdata) +{ + COMMON_ENTRY; + return 1; +} + +static void rtc_close(void* userdata) +{ +} + +static void rtc_get_datetime(struct tm *datetime, void* userdata) +{ + COMMON_ENTRY; + struct rtc_date_t timer_date = rtc->date; + struct rtc_time_t timer_time = rtc->time; + struct rtc_extended_t timer_extended = rtc->extended; + + datetime->tm_sec = timer_time.second % 60; + datetime->tm_min = timer_time.minute % 60; + datetime->tm_hour = timer_time.hour % 24; + datetime->tm_mday = timer_date.day % 31; + datetime->tm_mon = (timer_date.month % 12) - 1; + datetime->tm_year = (timer_date.year % 100) + (timer_extended.century * 100) - 1900; + datetime->tm_wday = timer_date.week; + datetime->tm_yday = rtc_get_yday(datetime->tm_year + 1900, datetime->tm_mon + 1, datetime->tm_mday); + datetime->tm_isdst = -1; +} + +static void rtc_set_datetime(const struct tm *datetime, void* userdata) +{ + COMMON_ENTRY; + struct rtc_date_t timer_date; + struct rtc_time_t timer_time; + struct rtc_extended_t timer_extended; + + /* + * Range of tm->tm_sec could be [0,61] + * + * Range of tm->tm_sec allows for a positive leap second. Two + * leap seconds in the same minute are not allowed (the C90 + * range 0..61 was a defect) + */ + if (rtc_in_range(datetime->tm_sec, 0, 59)) + timer_time.second = datetime->tm_sec; + else + configASSERT(!"Invalid second."); + + /* Range of tm->tm_min could be [0,59] */ + if (rtc_in_range(datetime->tm_min, 0, 59)) + timer_time.minute = datetime->tm_min; + else + configASSERT(!"Invalid minute."); + + /* Range of tm->tm_hour could be [0, 23] */ + if (rtc_in_range(datetime->tm_hour, 0, 23)) + timer_time.hour = datetime->tm_hour; + else + configASSERT(!"Invalid hour."); + + /* Range of tm->tm_mday could be [1, 31] */ + if (rtc_in_range(datetime->tm_mday, 1, 31)) + timer_date.day = datetime->tm_mday; + else + configASSERT(!"Invalid day."); + + /* + * Range of tm->tm_mon could be [0, 11] + * But in this RTC, date.month should be [1, 12] + */ + if (rtc_in_range(datetime->tm_mon, 0, 11)) + timer_date.month = datetime->tm_mon + 1; + else + configASSERT(!"Invalid month."); + + /* + * Range of tm->tm_year is the years since 1900 + * But in this RTC, year is split into year and century + * In this RTC, century range is [0,31], year range is [0,99] + */ + int human_year = datetime->tm_year + 1900; + int rtc_year = human_year % 100; + int rtc_century = human_year / 100; + + if (rtc_in_range(rtc_year, 0, 99) && + rtc_in_range(rtc_century, 0, 31)) + { + timer_date.year = rtc_year; + timer_extended.century = rtc_century; + } + else + { + configASSERT(!"Invalid year."); + } + + /* Range of tm->tm_wday could be [0, 6] */ + if (rtc_in_range(datetime->tm_wday, 0, 6)) + timer_date.week = datetime->tm_wday; + else + configASSERT(!"Invalid day."); + + /* Set RTC mode to timer setting mode */ + rtc_timer_set_mode(rtc, RTC_TIMER_SETTING); + /* Write value to RTC */ + rtc->date = timer_date; + rtc->time = timer_time; + rtc->extended = timer_extended; + /* Get CPU current freq */ + unsigned long freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); + /* Set threshold to 1/26000000 s */ + freq = freq / 26000000; + /* Get current CPU cycle */ + unsigned long start_cycle = read_csr(mcycle); + /* Wait for 1/26000000 s to sync data */ + while (read_csr(mcycle) - start_cycle < freq) + continue; + /* Set RTC mode to timer running mode */ + rtc_timer_set_mode(rtc, RTC_TIMER_RUNNING); +} + +static int rtc_in_range(int value, int min, int max) +{ + return ((value >= min) && (value <= max)); +} + +static int rtc_get_wday(int year, int month, int day) +{ + /* Magic method to get weekday */ + int weekday = (day += month < 3 ? year-- : year - 2, 23 * month / 9 + day + 4 + year / 4 - year / 100 + year / 400) % 7; + return weekday; +} + +static int rtc_year_is_leap(int year) +{ + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +} + +static int rtc_get_yday(int year, int month, int day) +{ + static const int days[2][13] = { + {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, + {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} + }; + int leap = rtc_year_is_leap(year); + + return days[leap][month] + day; +} + +static int rtc_timer_set_mode(volatile rtc_t* rtc, rtc_timer_mode_e timer_mode) +{ + struct rtc_register_ctrl_t register_ctrl = rtc->register_ctrl; + + switch (timer_mode) + { + case RTC_TIMER_PAUSE: + register_ctrl.read_enable = 0; + register_ctrl.write_enable = 0; + break; + case RTC_TIMER_RUNNING: + register_ctrl.read_enable = 1; + register_ctrl.write_enable = 0; + break; + case RTC_TIMER_SETTING: + register_ctrl.read_enable = 0; + register_ctrl.write_enable = 1; + break; + default: + register_ctrl.read_enable = 0; + register_ctrl.write_enable = 0; + break; + } + + rtc->register_ctrl = register_ctrl; + + return 0; +} + +static int rtc_protect_set(volatile rtc_t* rtc, int enable) +{ + struct rtc_register_ctrl_t register_ctrl = rtc->register_ctrl; + + struct rtc_mask_t mask = { + .second = 1, + /* Second mask */ + .minute = 1, + /* Minute mask */ + .hour = 1, + /* Hour mask */ + .week = 1, + /* Week mask */ + .day = 1, + /* Day mask */ + .month = 1, + /* Month mask */ + .year = 1, + }; + + struct rtc_mask_t unmask = { + .second = 0, + /* Second mask */ + .minute = 0, + /* Minute mask */ + .hour = 0, + /* Hour mask */ + .week = 0, + /* Week mask */ + .day = 0, + /* Day mask */ + .month = 0, + /* Month mask */ + .year = 0, + }; + + if (enable) + { + /* Turn RTC in protect mode, no one can write time */ + register_ctrl.timer_mask = *(uint8_t *)&unmask; + register_ctrl.alarm_mask = *(uint8_t *)&unmask; + register_ctrl.initial_count_mask = 0; + register_ctrl.interrupt_register_mask = 0; + } + else + { + /* Turn RTC in unprotect mode, everyone can write time */ + register_ctrl.timer_mask = *(uint8_t *)&mask; + register_ctrl.alarm_mask = *(uint8_t *)&mask; + register_ctrl.initial_count_mask = 1; + register_ctrl.interrupt_register_mask = 1; + } + + rtc->register_ctrl = register_ctrl; + return 0; +} + +static int rtc_timer_set_clock_frequency(volatile rtc_t* rtc, unsigned int frequency) +{ + struct rtc_initial_count_t initial_count; + + initial_count.count = frequency; + rtc->initial_count = initial_count; + return 0; +} + +static int rtc_timer_set_clock_count_value(volatile rtc_t* rtc, unsigned int count) +{ + struct rtc_current_count_t current_count; + + current_count.count = count; + rtc->current_count = current_count; + return 0; +} + +static rtc_data dev0_data = { RTC_BASE_ADDR, SYSCTL_CLOCK_RTC }; + +const rtc_driver_t g_rtc_driver_rtc0 = { {&dev0_data, rtc_install, rtc_open, rtc_close}, rtc_get_datetime, rtc_set_datetime }; diff --git a/lib/bsp/device/sccb.c b/lib/bsp/device/sccb.c index 4e9a1d6..cee2d3c 100644 --- a/lib/bsp/device/sccb.c +++ b/lib/bsp/device/sccb.c @@ -68,8 +68,8 @@ static void sccb_close(void* userdata) typedef struct { sccb_data* sccb_data; - size_t slaveAddress; - size_t address_width; + uint32_t slave_address; + uint32_t address_width; } sccb_dev_data; static void sccb_dev_install(void* userdata); @@ -78,7 +78,7 @@ static void sccb_dev_close(void* userdata); static uint8_t sccb_dev_read_byte(uint16_t reg_address, void* userdata); static void sccb_dev_write_byte(uint16_t reg_address, uint8_t value, void* userdata); -static sccb_device_driver_t* sccb_get_device(size_t slaveAddress, size_t address_width, void* userdata) +static sccb_device_driver_t* sccb_get_device(uint32_t slave_address, uint32_t address_width, void* userdata) { configASSERT(address_width == 8 || address_width == 16); @@ -86,7 +86,7 @@ static sccb_device_driver_t* sccb_get_device(size_t slaveAddress, size_t address memset(driver, 0, sizeof(sccb_device_driver_t)); sccb_dev_data* dev_data = (sccb_dev_data*)malloc(sizeof(sccb_dev_data)); - dev_data->slaveAddress = slaveAddress; + dev_data->slave_address = slave_address; dev_data->address_width = address_width; dev_data->sccb_data = userdata; @@ -141,16 +141,16 @@ static uint8_t sccb_dev_read_byte(uint16_t reg_address, void* userdata) if (dev_data->address_width == 8) { set_bit_mask(&sccb->sccb_cfg, DVP_SCCB_BYTE_NUM_MASK, DVP_SCCB_BYTE_NUM_2); - sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slaveAddress) | DVP_SCCB_REG_ADDRESS(reg_address); + sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slave_address) | DVP_SCCB_REG_ADDRESS(reg_address); } else { set_bit_mask(&sccb->sccb_cfg, DVP_SCCB_BYTE_NUM_MASK, DVP_SCCB_BYTE_NUM_3); - sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slaveAddress) | DVP_SCCB_REG_ADDRESS(reg_address >> 8) | DVP_SCCB_WDATA_BYTE0(reg_address & 0xFF); + sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slave_address) | DVP_SCCB_REG_ADDRESS(reg_address >> 8) | DVP_SCCB_WDATA_BYTE0(reg_address & 0xFF); } dvp_sccb_start_transfer(sccb); - sccb->sccb_ctl = DVP_SCCB_DEVICE_ADDRESS(dev_data->slaveAddress); + sccb->sccb_ctl = DVP_SCCB_DEVICE_ADDRESS(dev_data->slave_address); dvp_sccb_start_transfer(sccb); uint8_t ret = DVP_SCCB_RDATA_BYTE(sccb->sccb_cfg); @@ -167,12 +167,12 @@ static void sccb_dev_write_byte(uint16_t reg_address, uint8_t value, void* userd if (dev_data->address_width == 8) { set_bit_mask(&sccb->sccb_cfg, DVP_SCCB_BYTE_NUM_MASK, DVP_SCCB_BYTE_NUM_3); - sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slaveAddress) | DVP_SCCB_REG_ADDRESS(reg_address) | DVP_SCCB_WDATA_BYTE0(value); + sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slave_address) | DVP_SCCB_REG_ADDRESS(reg_address) | DVP_SCCB_WDATA_BYTE0(value); } else { set_bit_mask(&sccb->sccb_cfg, DVP_SCCB_BYTE_NUM_MASK, DVP_SCCB_BYTE_NUM_4); - sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slaveAddress) | DVP_SCCB_REG_ADDRESS(reg_address >> 8) | DVP_SCCB_WDATA_BYTE0(reg_address & 0xFF) | DVP_SCCB_WDATA_BYTE1(value); + sccb->sccb_ctl = DVP_SCCB_WRITE_ENABLE | DVP_SCCB_DEVICE_ADDRESS(dev_data->slave_address) | DVP_SCCB_REG_ADDRESS(reg_address >> 8) | DVP_SCCB_WDATA_BYTE0(reg_address & 0xFF) | DVP_SCCB_WDATA_BYTE1(value); } dvp_sccb_start_transfer(sccb); diff --git a/lib/bsp/device/sha256.c b/lib/bsp/device/sha256.c index 4871cce..79909ef 100644 --- a/lib/bsp/device/sha256.c +++ b/lib/bsp/device/sha256.c @@ -16,11 +16,7 @@ #include #include #include -#include -#include -#include #include -#include #include #include #include @@ -30,30 +26,17 @@ #define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL)) #define BYTESWAP64(x) byteswap64(x) #define COMMON_ENTRY \ - sha256_dev_data* data = (sha256_dev_data*)userdata; \ - volatile struct sha256_t* sha256 = (volatile struct sha256_t*)data->base_addr; \ + sha256_dev_data *data = (sha256_dev_data*)userdata; \ + volatile sha256_t *sha256 = (volatile sha256_t *)data->base_addr; \ (void)sha256; typedef struct { + enum sysctl_clock_e clock; uintptr_t base_addr; SemaphoreHandle_t free_mutex; } sha256_dev_data; -typedef struct -{ - uint64_t total_length; - uint32_t hash[SHA256_HASH_WORDS]; - uint32_t dma_buf_length; - uint32_t* dma_buf; - uint32_t buffer_length; - union - { - uint32_t words[16]; - uint8_t bytes[64]; - } buffer; -} sha256_context; - static const uint8_t padding[64] = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -74,124 +57,120 @@ static inline uint64_t byteswap64(uint64_t x) return ((uint64_t)BYTESWAP(b) << 32) | (uint64_t)BYTESWAP(a); } -static void sha256_install(void* userdata) +static void sha256_install(void *userdata) { COMMON_ENTRY; - sysctl_clock_enable(SYSCTL_CLOCK_SHA); - sysctl_reset(SYSCTL_RESET_SHA); + sysctl_clock_enable(data->clock); data->free_mutex = xSemaphoreCreateMutex(); } -static int sha256_open(void* userdata) +static int sha256_open(void *userdata) { return 1; } -static void sha256_close(void* userdata) +static void sha256_close(void *userdata) { } -static void entry_exclusive(sha256_dev_data* data) +static void entry_exclusive(sha256_dev_data *data) { configASSERT(xSemaphoreTake(data->free_mutex, portMAX_DELAY) == pdTRUE); } -static void exit_exclusive(sha256_dev_data* data) +static void exit_exclusive(sha256_dev_data *data) { xSemaphoreGive(data->free_mutex); } -static void sha256_update_buf(sha256_context* sc, const void* vdata, uint32_t len) +static void sha256_update_buf(sha256_context_t *context, const void *input, uint32_t len) { - const uint8_t* data = vdata; - uint32_t buffer_bytes_left; - uint32_t bytes_to_copy; + const uint8_t *input_data = input; + size_t buffer_bytes_left; + size_t bytes_to_copy; uint32_t i; while (len) { - buffer_bytes_left = 64L - sc->buffer_length; - + buffer_bytes_left = 64 - context->buffer_len; bytes_to_copy = buffer_bytes_left; if (bytes_to_copy > len) bytes_to_copy = len; - memcpy(&sc->buffer.bytes[sc->buffer_length], data, bytes_to_copy); + memcpy(&context->buffer.bytes[context->buffer_len], input_data, bytes_to_copy); - sc->total_length += bytes_to_copy * 8L; + context->total_len += bytes_to_copy * 8; - sc->buffer_length += bytes_to_copy; - data += bytes_to_copy; + context->buffer_len += bytes_to_copy; + input_data += bytes_to_copy; len -= bytes_to_copy; - if (sc->buffer_length == 64L) + if (context->buffer_len == 64) { for (i = 0; i < 16; i++) { - sc->dma_buf[sc->dma_buf_length++] = sc->buffer.words[i]; + context->dma_buf[context->dma_buf_len++] = context->buffer.words[i]; } - sc->buffer_length = 0L; + context->buffer_len = 0; } } } -void sha256_final_buf(sha256_context* sc) +static void sha256_final_buf(sha256_context_t* context) { - uint32_t bytes_to_pad; - uint64_t length_pad; + size_t bytes_to_pad; + size_t padding_len; - bytes_to_pad = 120L - sc->buffer_length; + bytes_to_pad = 120 - context->buffer_len; - if (bytes_to_pad > 64L) - bytes_to_pad -= 64L; - length_pad = BYTESWAP64(sc->total_length); - sha256_update_buf(sc, padding, bytes_to_pad); - sha256_update_buf(sc, &length_pad, 8L); + if (bytes_to_pad > 64) + bytes_to_pad -= 64; + padding_len = BYTESWAP64(context->total_len); + sha256_update_buf(context, padding, bytes_to_pad); + sha256_update_buf(context, &padding_len, 8); } -static void sha256_str(const char* str, size_t length, uint8_t* hash, void* userdata) +static void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output, void *userdata) { COMMON_ENTRY; entry_exclusive(data); - int i = 0; - sha256_context sha; - sysctl_clock_enable(SYSCTL_CLOCK_SHA); - sysctl_reset(SYSCTL_RESET_SHA); - + uint32_t i = 0; + sha256_context_t context; + sysctl_clock_enable(data->clock); sha256->reserved0 = 0; sha256->sha_status |= 1 << 16; /*!< 0 for little endian, 1 for big endian */ sha256->sha_status |= 1; /*!< enable sha256 */ - sha256->sha_data_num = (length + 64 + 8) / 64; - sha.dma_buf = (uint32_t*)malloc((length + 64 + 8) / 64 * 16 * sizeof(uint32_t)); - sha.buffer_length = 0L; - sha.dma_buf_length = 0L; - sha.total_length = 0LL; - for (i = 0; i < (sizeof(sha.dma_buf) / 4); i++) - sha.dma_buf[i] = 0; - sha256_update_buf(&sha, str, length); - sha256_final_buf(&sha); + sha256->sha_data_num = (input_len + 64 + 8) / 64; + context.dma_buf = (uint32_t*)malloc((input_len + 64 + 8) / 64 * 16 * sizeof(uint32_t)); + context.buffer_len = 0; + context.dma_buf_len = 0; + context.total_len = 0; + for (i = 0; i < (sizeof(context.dma_buf) / 4); i++) + context.dma_buf[i] = 0; + sha256_update_buf(&context, input, input_len); + sha256_final_buf(&context); uintptr_t dma_write = dma_open_free(); - dma_set_select_request(dma_write, SYSCTL_DMA_SELECT_SHA_RX_REQ); + dma_set_request_source(dma_write, SYSCTL_DMA_SELECT_SHA_RX_REQ); SemaphoreHandle_t event_write = xSemaphoreCreateBinary(); - dma_transmit_async(dma_write, sha.dma_buf, &sha256->sha_data_in1, 1, 0, sizeof(uint32_t), sha.dma_buf_length, 16, event_write); + dma_transmit_async(dma_write, context.dma_buf, &sha256->sha_data_in1, 1, 0, sizeof(uint32_t), context.dma_buf_len, 16, event_write); sha256->sha_input_ctrl |= 1; /*!< dma enable */ configASSERT(xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); while (!(sha256->sha_status & 0x01)) ; for (i = 0; i < SHA256_HASH_WORDS; i++) - *((uint32_t*)&hash[i * 4]) = sha256->sha_result[SHA256_HASH_WORDS - i - 1]; - free(sha.dma_buf); + *((uint32_t*)&output[i * 4]) = sha256->sha_result[SHA256_HASH_WORDS - i - 1]; + free(context.dma_buf); dma_close(dma_write); vSemaphoreDelete(event_write); exit_exclusive(data); } -static sha256_dev_data dev0_data = {SHA256_BASE_ADDR, 0}; +static sha256_dev_data dev0_data = {SYSCTL_CLOCK_SHA, SHA256_BASE_ADDR, 0}; -const sha256_driver_t g_sha_driver_sha256 = {{&dev0_data, sha256_install, sha256_open, sha256_close}, sha256_str}; +const sha256_driver_t g_sha_driver_sha256 = {{&dev0_data, sha256_install, sha256_open, sha256_close}, sha256_hard_calculate}; diff --git a/lib/bsp/device/spi.c b/lib/bsp/device/spi.c index 91e84cb..f73a363 100644 --- a/lib/bsp/device/spi.c +++ b/lib/bsp/device/spi.c @@ -49,8 +49,8 @@ typedef struct struct { - spi_frame_format frame_format; - size_t chip_select_line; + spi_frame_format_t frame_format; + size_t chip_select_mask; size_t buffer_width; size_t inst_width; size_t addr_width; @@ -85,29 +85,29 @@ static void spi_close(void* userdata) typedef struct { spi_data* spi_data; - spi_mode mode; - spi_frame_format frame_format; - size_t chip_select_line; + spi_mode_t mode; + spi_frame_format_t frame_format; + size_t chip_select_mask; size_t data_bit_length; size_t instruction_length; size_t address_length; size_t wait_cycles; - spi_addr_inst_trans_mode trans_mode; + spi_inst_addr_trans_mode_t trans_mode; uint32_t baud_rate; } spi_dev_data; static void spi_dev_install(void* userdata); static int spi_dev_open(void* userdata); static void spi_dev_close(void* userdata); -static int spi_dev_read(char* buffer, size_t len, void* userdata); -static int spi_dev_write(const char* buffer, size_t len, void* userdata); -static void spi_dev_config(size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode, void* userdata); -static double spi_dev_set_speed(double speed, void* userdata); -static int spi_dev_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); -static int spi_dev_transfer_full_duplex(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); -static void spi_dev_fill(size_t instruction, size_t address, uint32_t value, size_t count, void* userdata); - -static spi_device_driver_t* spi_get_device(spi_mode mode, spi_frame_format frame_format, size_t chip_select_line, size_t data_bit_length, void* userdata) +static int spi_dev_read(uint8_t* buffer, size_t len, void* userdata); +static int spi_dev_write(const uint8_t* buffer, size_t len, void* userdata); +static void spi_dev_config_non_standard(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode, void* userdata); +static double spi_dev_set_clock_rate(double clock_rate, void* userdata); +static int spi_dev_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata); +static int spi_dev_transfer_full_duplex(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata); +static void spi_dev_fill(uint32_t instruction, uint32_t address, uint32_t value, size_t count, void* userdata); + +static spi_device_driver_t* spi_get_device(spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length, void* userdata) { spi_device_driver_t* driver = (spi_device_driver_t*)malloc(sizeof(spi_device_driver_t)); memset(driver, 0, sizeof(spi_device_driver_t)); @@ -116,7 +116,7 @@ static spi_device_driver_t* spi_get_device(spi_mode mode, spi_frame_format frame dev_data->spi_data = userdata; dev_data->mode = mode; dev_data->frame_format = frame_format; - dev_data->chip_select_line = chip_select_line; + dev_data->chip_select_mask = chip_select_mask; dev_data->data_bit_length = data_bit_length; dev_data->baud_rate = 0x2; @@ -126,8 +126,8 @@ static spi_device_driver_t* spi_get_device(spi_mode mode, spi_frame_format frame driver->base.close = spi_dev_close; driver->read = spi_dev_read; driver->write = spi_dev_write; - driver->config = spi_dev_config; - driver->set_speed = spi_dev_set_speed; + driver->config = spi_dev_config_non_standard; + driver->set_clock_rate = spi_dev_set_clock_rate; driver->transfer_sequential = spi_dev_transfer_sequential; driver->transfer_full_duplex = spi_dev_transfer_full_duplex; driver->fill = spi_dev_fill; @@ -156,11 +156,11 @@ static int get_inst_addr_width(size_t length) return 4; } -static void spi_config_as_master(spi_mode mode, spi_frame_format frame_format, size_t chip_select_line, size_t data_bit_length, uint32_t baud_rate, void* userdata) +static void spi_config_as_master(spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length, uint32_t baud_rate, void* userdata) { COMMON_ENTRY; configASSERT(data_bit_length >= 4 && data_bit_length <= 32); - configASSERT(chip_select_line); + configASSERT(chip_select_mask); switch (frame_format) { @@ -187,14 +187,14 @@ static void spi_config_as_master(spi_mode mode, spi_frame_format frame_format, s spi->ctrlr0 = (mode << 6) | (frame_format << data->frf_off) | ((data_bit_length - 1) << data->dfs_off); spi->spi_ctrlr0 = 0; - data->chip_select_line = chip_select_line; + data->chip_select_mask = chip_select_mask; data->frame_format = frame_format; data->buffer_width = get_buffer_width(data_bit_length); data->inst_width = 0; data->addr_width = 0; } -static void spi_config(size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode, void* userdata) +static void spi_config(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode, void* userdata) { COMMON_ENTRY; configASSERT(data->frame_format != SPI_FF_STANDARD); @@ -245,13 +245,13 @@ static void spi_config(size_t instruction_length, size_t address_length, size_t data->addr_width = get_inst_addr_width(address_length); } -static void write_inst_addr(volatile uint32_t* dr, const char** buffer, size_t width) +static void write_inst_addr(volatile uint32_t* dr, const uint8_t** buffer, size_t width) { configASSERT(width <= 4); if (width) { uint32_t cmd = 0; - char* pcmd = (char*)&cmd; + uint8_t* pcmd = (uint8_t*)&cmd; size_t i; for (i = 0; i < width; i++) { @@ -263,15 +263,15 @@ static void write_inst_addr(volatile uint32_t* dr, const char** buffer, size_t w } } -static int spi_read(char* buffer, size_t len, void* userdata) +static int spi_read(uint8_t* buffer, size_t len, void* userdata) { COMMON_ENTRY; size_t frames = len / data->buffer_width; uintptr_t dma_read = dma_open_free(); - dma_set_select_request(dma_read, data->dma_req_base); + dma_set_request_source(dma_read, data->dma_req_base); - char* ori_buffer = buffer; + uint8_t* ori_buffer = buffer; set_bit_mask(&spi->ctrlr0, TMOD_MASK, TMOD_VALUE(2)); spi->ctrlr1 = frames - 1; @@ -282,9 +282,9 @@ static int spi_read(char* buffer, size_t len, void* userdata) dma_transmit_async(dma_read, &spi->dr[0], ori_buffer, 0, 1, data->buffer_width, frames, 1, event_read); - write_inst_addr(spi->dr, (const char**)&buffer, data->inst_width); - write_inst_addr(spi->dr, (const char**)&buffer, data->addr_width); - spi->ser = data->chip_select_line; + write_inst_addr(spi->dr, (const uint8_t**)&buffer, data->inst_width); + write_inst_addr(spi->dr, (const uint8_t**)&buffer, data->addr_width); + spi->ser = data->chip_select_mask; configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE); dma_close(dma_read); @@ -296,12 +296,12 @@ static int spi_read(char* buffer, size_t len, void* userdata) return len; } -static int spi_write(const char* buffer, size_t len, void* userdata) +static int spi_write(const uint8_t* buffer, size_t len, void* userdata) { COMMON_ENTRY; uintptr_t dma_write = dma_open_free(); - dma_set_select_request(dma_write, data->dma_req_base + 1); + dma_set_request_source(dma_write, data->dma_req_base + 1); set_bit_mask(&spi->ctrlr0, TMOD_MASK, TMOD_VALUE(1)); spi->dmacr = 0x2; @@ -314,7 +314,7 @@ static int spi_write(const char* buffer, size_t len, void* userdata) SemaphoreHandle_t event_write = xSemaphoreCreateBinary(); dma_transmit_async(dma_write, buffer, &spi->dr[0], 1, 0, data->buffer_width, frames, 4, event_write); - spi->ser = data->chip_select_line; + spi->ser = data->chip_select_mask; configASSERT(xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); dma_close(dma_write); vSemaphoreDelete(event_write); @@ -327,26 +327,26 @@ static int spi_write(const char* buffer, size_t len, void* userdata) return len; } -void spi_fill(size_t instruction, size_t address, uint32_t value, size_t count, void* userdata) +void spi_fill(uint32_t instruction, uint32_t address, uint32_t value, size_t count, void* userdata) { COMMON_ENTRY; uintptr_t dma_write = dma_open_free(); - dma_set_select_request(dma_write, data->dma_req_base + 1); + dma_set_request_source(dma_write, data->dma_req_base + 1); set_bit_mask(&spi->ctrlr0, TMOD_MASK, TMOD_VALUE(1)); spi->dmacr = 0x2; spi->ssienr = 0x01; - const char* buffer = (const char*)&instruction; + const uint8_t* buffer = (const uint8_t*)&instruction; write_inst_addr(spi->dr, &buffer, data->inst_width); - buffer = (const char*)&address; + buffer = (const uint8_t*)&address; write_inst_addr(spi->dr, &buffer, data->addr_width); SemaphoreHandle_t event_write = xSemaphoreCreateBinary(); dma_transmit_async(dma_write, &value, &spi->dr[0], 0, 0, sizeof(uint32_t), count, 4, event_write); - spi->ser = data->chip_select_line; + spi->ser = data->chip_select_mask; configASSERT(xSemaphoreTake(event_write, portMAX_DELAY) == pdTRUE); dma_close(dma_write); vSemaphoreDelete(event_write); @@ -358,7 +358,7 @@ void spi_fill(size_t instruction, size_t address, uint32_t value, size_t count, spi->dmacr = 0x00; } -static int spi_read_write(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int spi_read_write(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_ENTRY; configASSERT(data->frame_format == SPI_FF_STANDARD); @@ -366,8 +366,8 @@ static int spi_read_write(const char* write_buffer, size_t write_len, char* read uintptr_t dma_write = dma_open_free(); uintptr_t dma_read = dma_open_free(); - dma_set_select_request(dma_write, data->dma_req_base + 1); - dma_set_select_request(dma_read, data->dma_req_base); + dma_set_request_source(dma_write, data->dma_req_base + 1); + dma_set_request_source(dma_read, data->dma_req_base); size_t tx_frames = write_len / data->buffer_width; size_t rx_frames = read_len / data->buffer_width; @@ -375,7 +375,7 @@ static int spi_read_write(const char* write_buffer, size_t write_len, char* read spi->ctrlr1 = rx_frames - 1; spi->dmacr = 0x3; spi->ssienr = 0x01; - spi->ser = data->chip_select_line; + spi->ser = data->chip_select_mask; SemaphoreHandle_t event_read = xSemaphoreCreateBinary(), event_write = xSemaphoreCreateBinary(); dma_transmit_async(dma_read, &spi->dr[0], read_buffer, 0, 1, data->buffer_width, rx_frames, 1, event_read); @@ -395,7 +395,7 @@ static int spi_read_write(const char* write_buffer, size_t write_len, char* read return read_len; } -static int spi_transfer_full_duplex(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int spi_transfer_full_duplex(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_ENTRY; configASSERT(data->frame_format == SPI_FF_STANDARD); @@ -403,7 +403,7 @@ static int spi_transfer_full_duplex(const char* write_buffer, size_t write_len, return spi_read_write(write_buffer, write_len, read_buffer, read_len, userdata); } -static int spi_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int spi_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_ENTRY; configASSERT(data->frame_format == SPI_FF_STANDARD); @@ -415,7 +415,7 @@ static void entry_exclusive(spi_dev_data* dev_data) { spi_data* data = (spi_data*)dev_data->spi_data; configASSERT(xSemaphoreTake(data->free_mutex, portMAX_DELAY) == pdTRUE); - spi_config_as_master(dev_data->mode, dev_data->frame_format, dev_data->chip_select_line, dev_data->data_bit_length, dev_data->baud_rate, data); + spi_config_as_master(dev_data->mode, dev_data->frame_format, dev_data->chip_select_mask, dev_data->data_bit_length, dev_data->baud_rate, data); if (dev_data->frame_format != SPI_FF_STANDARD) spi_config(dev_data->instruction_length, dev_data->address_length, dev_data->wait_cycles, dev_data->trans_mode, data); } @@ -439,7 +439,7 @@ static void spi_dev_close(void* userdata) { } -static int spi_dev_read(char* buffer, size_t len, void* userdata) +static int spi_dev_read(uint8_t* buffer, size_t len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -448,7 +448,7 @@ static int spi_dev_read(char* buffer, size_t len, void* userdata) return ret; } -static int spi_dev_write(const char* buffer, size_t len, void* userdata) +static int spi_dev_write(const uint8_t* buffer, size_t len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -457,7 +457,7 @@ static int spi_dev_write(const char* buffer, size_t len, void* userdata) return ret; } -static void spi_dev_config(size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode, void* userdata) +static void spi_dev_config_non_standard(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode, void* userdata) { spi_dev_data* dev_data = (spi_dev_data*)userdata; dev_data->instruction_length = instruction_length; @@ -466,18 +466,18 @@ static void spi_dev_config(size_t instruction_length, size_t address_length, siz dev_data->trans_mode = trans_mode; } -static double spi_dev_set_speed(double speed, void* userdata) +static double spi_dev_set_clock_rate(double clock_rate, void* userdata) { COMMON_DEV_ENTRY; double clk = (double)sysctl_clock_get_freq(data->clock); - uint32_t div = min(65534, max((uint32_t)ceil(clk / speed), 2)); + uint32_t div = min(65534, max((uint32_t)ceil(clk / clock_rate), 2)); if (div & 1) div++; dev_data->baud_rate = div; return clk / div; } -static int spi_dev_transfer_sequential(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int spi_dev_transfer_sequential(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -486,7 +486,7 @@ static int spi_dev_transfer_sequential(const char* write_buffer, size_t write_le return ret; } -static int spi_dev_transfer_full_duplex(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata) +static int spi_dev_transfer_full_duplex(const uint8_t* write_buffer, size_t write_len, uint8_t* read_buffer, size_t read_len, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); @@ -495,7 +495,7 @@ static int spi_dev_transfer_full_duplex(const char* write_buffer, size_t write_l return ret; } -static void spi_dev_fill(size_t instruction, size_t address, uint32_t value, size_t count, void* userdata) +static void spi_dev_fill(uint32_t instruction, uint32_t address, uint32_t value, size_t count, void* userdata) { COMMON_DEV_ENTRY; entry_exclusive(dev_data); diff --git a/lib/bsp/device/timer.c b/lib/bsp/device/timer.c index 9b0ee72..571bd68 100644 --- a/lib/bsp/device/timer.c +++ b/lib/bsp/device/timer.c @@ -1,17 +1,3 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ /* Copyright 2018 Canaan Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,7 +36,7 @@ typedef struct enum sysctl_clock_e clock; enum plic_irq_t irq; size_t channel; - timer_ontick ontick; + timer_on_tick_t on_tick; void* ontick_data; } timer_data; @@ -99,11 +85,11 @@ static size_t timer_set_interval(size_t nanoseconds, void* userdata) return (size_t)(min_step * value); } -static void timer_set_ontick(timer_ontick ontick, void* ontick_data, void* userdata) +static void timer_set_on_tick(timer_on_tick_t on_tick, void* ontick_data, void* userdata) { COMMON_ENTRY; data->ontick_data = ontick_data; - data->ontick = ontick; + data->on_tick = on_tick; } static void timer_set_enable(int enable, void* userdata) @@ -125,8 +111,8 @@ static void timer_isr(void* userdata) if (channel & 1) { timer_data* td = data + i; - if (td->ontick) - td->ontick(td->ontick_data); + if (td->on_tick) + td->on_tick(td->ontick_data); } channel >>= 1; @@ -143,7 +129,7 @@ static void timer_isr(void* userdata) { TIMER##i##_BASE_ADDR, SYSCTL_CLOCK_TIMER##i, IRQN_TIMER##i##A_INTERRUPT, 3, NULL, NULL } /* clang format on */ -#define INIT_TIMER_DRIVER(i) { { &dev_data[i], timer_install, timer_open, timer_close }, timer_set_interval, timer_set_ontick, timer_set_enable } +#define INIT_TIMER_DRIVER(i) { { &dev_data[i], timer_install, timer_open, timer_close }, timer_set_interval, timer_set_on_tick, timer_set_enable } static timer_data dev_data[12] = { diff --git a/lib/bsp/device/uart.c b/lib/bsp/device/uart.c index ceef55e..8c6a597 100644 --- a/lib/bsp/device/uart.c +++ b/lib/bsp/device/uart.c @@ -34,7 +34,7 @@ typedef struct size_t head; size_t tail; size_t length; - char ring_buffer[RINGBUFF_LEN]; + uint8_t ring_buffer[RINGBUFF_LEN]; } ringbuffer_t; typedef struct @@ -42,13 +42,13 @@ typedef struct enum sysctl_clock_e clock; uintptr_t base_addr; size_t channel; - ringbuffer_t* recBuf; + ringbuffer_t* recv_buf; } uart_data; static int write_ringbuff(uint8_t rdata, void* userdata) { COMMON_ENTRY; - ringbuffer_t* ring_buff = data->recBuf; + ringbuffer_t* ring_buff = data->recv_buf; if (ring_buff->length >= RINGBUFF_LEN) { @@ -60,10 +60,10 @@ static int write_ringbuff(uint8_t rdata, void* userdata) return 0; } -static int read_ringbuff(char* rData, size_t len, void* userdata) +static int read_ringbuff(uint8_t* rData, size_t len, void* userdata) { COMMON_ENTRY; - ringbuffer_t* ring_buff = data->recBuf; + ringbuffer_t* ring_buff = data->recv_buf; size_t cnt = 0; while ((len--) && ring_buff->length) { @@ -98,7 +98,7 @@ static int uart_open(void* userdata) ring_buff->head = 0; ring_buff->tail = 0; ring_buff->length = 0; - data->recBuf = ring_buff; + data->recv_buf = ring_buff; pic_set_irq_handler(IRQN_UART1_INTERRUPT + data->channel, on_irq_apbuart_recv, userdata); pic_set_irq_priority(IRQN_UART1_INTERRUPT + data->channel, 1); pic_set_irq_enable(IRQN_UART1_INTERRUPT + data->channel, 1); @@ -108,24 +108,24 @@ static int uart_open(void* userdata) static void uart_close(void* userdata) { COMMON_ENTRY; - free(data->recBuf); + free(data->recv_buf); } -static void uart_config(size_t baud_rate, size_t data_width, uart_stopbit stopbit, uart_parity parity, void* userdata) +static void uart_config(uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity, void* userdata) { COMMON_ENTRY; - configASSERT(data_width >= 5 && data_width <= 8); - if (data_width == 5) + configASSERT(databits >= 5 && databits <= 8); + if (databits == 5) { - configASSERT(stopbit != UART_STOP_2); + configASSERT(stopbits != UART_STOP_2); } else { - configASSERT(stopbit != UART_STOP_1_5); + configASSERT(stopbits != UART_STOP_1_5); } - uint32_t stopbit_val = stopbit == UART_STOP_1 ? 0 : 1; + uint32_t stopbit_val = stopbits == UART_STOP_1 ? 0 : 1; uint32_t parity_val = 0; switch (parity) { @@ -158,7 +158,7 @@ static void uart_config(size_t baud_rate, size_t data_width, uart_stopbit stopbi uart->DLL = u16Divider & 0xFF; uart->DLH = u16Divider >> 8; uart->LCR = 0; - uart->LCR = (data_width - 5) | (stopbit_val << 2) | (parity_val << 3); + uart->LCR = (databits - 5) | (stopbit_val << 2) | (parity_val << 3); uart->LCR &= ~(1u << 7); uart->MCR &= ~3; uart->IER = 1; @@ -172,12 +172,12 @@ static int uart_putc(volatile uart_t* uart, char c) return 0; } -static int uart_read(char* buffer, size_t len, void* userdata) +static int uart_read(uint8_t* buffer, size_t len, void* userdata) { return read_ringbuff(buffer, len, userdata); } -static int uart_write(const char* buffer, size_t len, void* userdata) +static int uart_write(const uint8_t* buffer, size_t len, void* userdata) { COMMON_ENTRY; diff --git a/lib/bsp/device/wdt.c b/lib/bsp/device/wdt.c new file mode 100644 index 0000000..ab5ba42 --- /dev/null +++ b/lib/bsp/device/wdt.c @@ -0,0 +1,144 @@ +/* Copyright 2018 Canaan Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define COMMON_ENTRY \ + wdt_data* data = (wdt_data*)userdata; \ + volatile wdt_t* wdt = (volatile wdt_t*)data->base_addr; \ + (void)wdt; + +typedef struct +{ + uintptr_t base_addr; + enum sysctl_clock_e clock; + enum plic_irq_t irq; + + struct + { + wdt_on_timeout_t on_timeout; + void* on_timeout_data; + }; +} wdt_data; + +static void wdt_isr(void* userdata); + +static void wdt_install(void* userdata) +{ + COMMON_ENTRY; + sysctl_clock_enable(data->clock); + pic_set_irq_handler(data->irq, wdt_isr, userdata); + pic_set_irq_priority(data->irq, 1); +} + +static int wdt_open(void* userdata) +{ + COMMON_ENTRY; + return 1; +} + +static void wdt_close(void* userdata) +{ +} + +static void wdt_set_response_mode(wdt_response_mode_t mode, void* userdata) +{ + COMMON_ENTRY; + + uint8_t rmode = 0; + switch (mode) + { + case WDT_RESP_RESET: + rmode = WDT_CR_RMOD_RESET; + break; + case WDT_RESP_INTERRUPT: + rmode = WDT_CR_RMOD_INTERRUPT; + default: + configASSERT(!"Invalid wdt response mode."); + break; + } + + wdt->cr &= (~WDT_CR_RMOD_MASK); + wdt->cr |= rmode; +} + +static size_t wdt_set_timeout(size_t nanoseconds, void* userdata) +{ + COMMON_ENTRY; + uint32_t clk_freq = sysctl_clock_get_freq(data->clock); + double min_step = 1e9 / clk_freq; + double value = (int32_t)log2(nanoseconds / min_step); + configASSERT(value >= 0 && value <= 0xF); + wdt->torr = WDT_TORR_TOP((uint8_t)value); + return (size_t)(min_step * value); +} + +static void wdt_set_on_timeout(wdt_on_timeout_t on_timeout, void* on_timeout_data, void* userdata) +{ + COMMON_ENTRY; + data->on_timeout_data = on_timeout_data; + data->on_timeout = on_timeout; + + pic_set_irq_enable(data->irq, on_timeout_data ? 1 : 0); +} + +static void wdt_restart_counter(void* userdata) +{ + COMMON_ENTRY; + wdt->crr = WDT_CRR_MASK; +} + +static void wdt_set_enable(int enable, void* userdata) +{ + COMMON_ENTRY; + if (enable) + { + wdt->crr = WDT_CRR_MASK; + wdt->cr |= WDT_CR_ENABLE; + } + else + { + wdt->crr = WDT_CRR_MASK; + wdt->cr &= (~WDT_CR_ENABLE); + } +} + +static void wdt_isr(void* userdata) +{ + COMMON_ENTRY; + if (data->on_timeout) + { + if (data->on_timeout(data->on_timeout_data)) + { + readl(&wdt->eoi); + } + } +} + +static wdt_data dev0_data = {WDT0_BASE_ADDR, SYSCTL_CLOCK_WDT0, IRQN_WDT0_INTERRUPT, {0}}; +static wdt_data dev1_data = {WDT1_BASE_ADDR, SYSCTL_CLOCK_WDT1, IRQN_WDT0_INTERRUPT, {0}}; + +const wdt_driver_t g_wdt_driver_wdt0 = {{&dev0_data, wdt_install, wdt_open, wdt_close}, wdt_set_response_mode, wdt_set_timeout, wdt_set_on_timeout, wdt_restart_counter, wdt_set_enable}; +const wdt_driver_t g_wdt_driver_wdt1 = {{&dev1_data, wdt_install, wdt_open, wdt_close}, wdt_set_response_mode, wdt_set_timeout, wdt_set_on_timeout, wdt_restart_counter, wdt_set_enable}; diff --git a/lib/bsp/entry_user.c b/lib/bsp/entry_user.c index 3126e7d..f9f1744 100644 --- a/lib/bsp/entry_user.c +++ b/lib/bsp/entry_user.c @@ -12,11 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* Enable kernel-mode log API */ -#include - #include #include +#include #include #include "clint.h" #include "entry.h" @@ -66,7 +64,7 @@ void _init_bsp(int core_id, int number_of_cores) /* Setup clocks */ setup_clocks(); /* Init UART */ - uart_init(); + uarths_init(); } exit(os_entry(core_id, number_of_cores, main)); diff --git a/lib/bsp/interrupt.c b/lib/bsp/interrupt.c index 35bd866..0c53259 100644 --- a/lib/bsp/interrupt.c +++ b/lib/bsp/interrupt.c @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* Enable kernel-mode log API */ #include #include #include diff --git a/lib/bsp/printf.c b/lib/bsp/printf.c index 25f18fe..c858ad2 100644 --- a/lib/bsp/printf.c +++ b/lib/bsp/printf.c @@ -641,7 +641,7 @@ static corelock_t lock = CORELOCK_INIT; static void uart_putf(void* unused, char c) { UNUSED(unused); - uart_putchar(c); + uarths_putchar(c); } int printk(const char* format, ...) diff --git a/lib/bsp/sleep.c b/lib/bsp/sleep.c index 3635ce0..0b2f6da 100644 --- a/lib/bsp/sleep.c +++ b/lib/bsp/sleep.c @@ -13,7 +13,6 @@ * limitations under the License. */ #include -#include #include #include #include "sleep.h" diff --git a/lib/bsp/syscalls.c b/lib/bsp/syscalls.c index 5483492..b380f17 100644 --- a/lib/bsp/syscalls.c +++ b/lib/bsp/syscalls.c @@ -118,7 +118,7 @@ void __attribute__((noreturn)) sys_exit(int code) /* Write stop bit and write back */ *reg = (*reg) | 0x80000000UL; /* Send 0 to uart */ - uart_putchar(0); + uarths_putchar(0); while (1) continue; } @@ -131,7 +131,7 @@ static int sys_nosys(long a0, long a1, long a2, long a3, long a4, long a5, unsig LOGE(TAG, "Unsupported syscall %ld: a0=%lx, a1=%lx, a2=%lx!\n", n, a0, a1, a2); /* Send 0 to uart */ - uart_putchar(0); + uarths_putchar(0); while (1) continue; return -ENOSYS; @@ -211,13 +211,13 @@ static ssize_t sys_write(int file, const void* ptr, size_t len) /* Get size to write */ register size_t length = len; /* Get data pointer */ - register char* data = (char*)ptr; + register uint8_t* data = (uint8_t*)ptr; if (STDOUT_FILENO == file || STDERR_FILENO == file) { /* Write data */ while (length-- > 0 && *data != 0) - uart_putchar(*(data++)); + uarths_putchar(*(data++)); /* Return the actual size written */ res = len; diff --git a/lib/drivers/include/aes.h b/lib/drivers/include/aes.h index b8bb4ec..390171d 100644 --- a/lib/drivers/include/aes.h +++ b/lib/drivers/include/aes.h @@ -16,8 +16,6 @@ #define _DRIVER_AES_H #include -#include "encoding.h" -#include "platform.h" #ifdef __cplusplus extern "C" { diff --git a/lib/drivers/include/audio_bf.h b/lib/drivers/include/audio_bf.h deleted file mode 100644 index 4251e87..0000000 --- a/lib/drivers/include/audio_bf.h +++ /dev/null @@ -1,331 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef _DRIVER_AUDIO_BF_H -#define _DRIVER_AUDIO_BF_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -extern volatile struct audio_bf_reg_t* const audio_bf; - -enum en_bf_dir -{ - BF_DIR0 = 0, - BF_DIR1, - BF_DIR2, - BF_DIR3, - BF_DIR4, - BF_DIR5, - BF_DIR6, - BF_DIR7, - BF_DIR8, - BF_DIR9, - BF_DIR10, - BF_DIR11, - BF_DIR12, - BF_DIR13, - BF_DIR14, - BF_DIR15, -}; - -struct audio_bf_ch_cfg_t -{ - /** - * BF unit sound channel enable control bits. - * Bit 'x' corresponds to enable bit for sound channel 'x' (x = 0, 1, 2, - * . . ., 7). BF sound channels are related with I2S host RX channels. - * BF sound channel 0/1 correspond to the left/right channel of I2S RX0; - * BF channel 2/3 correspond to left/right channels of I2S RX1; and - * things like that. 0x1: writing '1' to enable the corresponding BF - * sound channel. 0x0: writing '0' to close the corresponding BF sound - * channel. - */ - uint32_t bf_sound_ch_en : 8; - /** - * Target direction select for valid voice output. - * When the source voice direaction searching is done, software can use - * this field to select one from 16 sound directions for the following - * voice recognition. 0x0: select sound direction 0; 0x1: select sound - * direction 1; . . . . . . 0xF: select sound direction 15. - */ - uint32_t bf_target_dir : 4; - /** - * This is the audio sample gain factor. Using this gain factor to - * enhance or reduce the stength of the sum of at most 8 source - * sound channel outputs. This is a unsigned 11-bit fix-point number, - * bit 10 is integer part and bit 9~0 are the fractional part. - */ - uint32_t audio_gain : 11; - uint32_t reserved1 : 1; - /** - * audio data source configure parameter. This parameter controls where - * the audio data source comes from. 0x0: audio data directly sourcing - * from audio_bf internal buffer; 0x1: audio data sourcing from - * FFT result buffer. - */ - uint32_t data_src_mode : 1; - uint32_t reserved2 : 3; - /** - * write enable for bf_sound_ch_en parameter. - * 0x1: allowing updates made to 'bf_sound_ch_en'. - * Access Mode: write only - */ - uint32_t we_bf_sound_ch_en : 1; - /** - * write enable for bf_target_dir parameter. - * 0x1: allowing updates made to 'bf_target_dir'. - * Access Mode: write only - */ - uint32_t we_bf_target_dir : 1; - /** - * write enable for audio_gain parameter. - * 0x1: allowing updates made to 'audio_gain'. - * Access Mode: write only - */ - uint32_t we_audio_gain : 1; - /** - * write enable for data_out_mode parameter. - * 0x1: allowing updates made to 'data_src_mode'. - */ - uint32_t we_data_src_mode : 1; -} __attribute__((packed, aligned(4))); - -struct audio_bf_ctl_t -{ - /** - * Sound direction searching enable bit. - * Software writes '1' to start sound direction searching function. - * When all the sound sample buffers are filled full, this bit is - * cleared by hardware (this sample buffers are used for direction - * detect only). 0x1: enable direction searching. - */ - uint32_t bf_dir_search_en : 1; - /* - *use this parameter to reset all the control logic on direction search processing path. This bit is self-clearing. - * 0x1: apply reset to direction searching control logic; - * 0x0: No operation. - */ - uint32_t search_path_reset : 1; - uint32_t reserved : 2; - /** - * Valid voice sample stream generation enable bit. - * After sound direction searching is done, software can configure this - * bit to generate a stream of voice samples for voice recognition. 0x1: - * enable output of voice sample stream. 0x0: stop the voice samlpe - * stream output. - */ - uint32_t bf_stream_gen_en : 1; - /* - *use this parameter to reset all the control logic on voice stream generating path. This bit is self-clearing. - * 0x1: apply reset to voice stream generating control logic; - * 0x0: No operation. - */ - uint32_t voice_gen_path_reset : 1; - /* - *use this parameter to switch to a new voice source direction. Software write '1' here and hardware will automatically clear it. - * 0x1: write '1' here to request switching to new voice source direction. - */ - uint32_t update_voice_dir : 1; - - uint32_t reserved1 : 1; - //write enable for 'bf_dir_search_en' parameter. - uint32_t we_bf_dir_search_en : 1; - uint32_t we_search_path_rst : 1; - uint32_t we_bf_stream_gen : 1; - uint32_t we_voice_gen_path_rst : 1; - uint32_t we_update_voice_dir : 1; - uint32_t reserved2 : 19; - -} __attribute__((packed, aligned(4))); - -struct audio_bf_dir_bidx_t -{ - uint32_t dir_rd_idx0 : 6; - uint32_t reserved : 2; - uint32_t dir_rd_idx1 : 6; - uint32_t reserved1 : 2; - uint32_t dir_rd_idx2 : 6; - uint32_t reserved2 : 2; - uint32_t dir_rd_idx3 : 6; - uint32_t reserved3 : 2; -} __attribute__((packed, aligned(4))); - -struct audio_bf_fir_coef_t -{ - uint32_t fir_tap0 : 16; - uint32_t fir_tap1 : 16; -} __attribute__((packed, aligned(4))); - -struct audio_bf_dwsz_cfg_t -{ - /** - * TThe down-sizing ratio used for direction searching. - * 0x0: no down-sizing; - * 0x1: 1/2 down sizing; - * 0x2: 1/3 down sizing; - * . . . . . . - * 0xF: 1/16 down sizing. - */ - uint32_t dir_dwn_siz_rate : 4; - /** - * The down-sizing ratio used for voice stream generation. - * 0x0: no down-sizing; - * 0x1: 1/2 down sizing; - * 0x2: 1/3 down sizing; - * . . . . . . - * 0xF: 1/16 down sizing. - */ - uint32_t voc_dwn_siz_rate : 4; - /** - * This bit field is used to perform sample precision reduction when - * the source sound sample (from I2S0 host receiving channels) - * precision is 20/24/32 bits. - * 0x0: take bits 15~0 from the source sound sample; - * 0x1: take bits 16~1 from the source sound sample; - * 0x2: take bits 17~2 from the source sound sample; - * . . . . . . - * 0x10: take bits 31~16 from the source sound sample; - */ - uint32_t smpl_shift_bits : 5; - uint32_t reserved : 19; -} __attribute__((packed, aligned(4))); - -struct audio_bf_fft_cfg_t -{ - uint32_t fft_shift_factor : 9; - uint32_t reserved1 : 3; - uint32_t fft_enable : 1; - uint32_t reserved2 : 19; -} __attribute__((packed, aligned(4))); - -struct audio_bf_int_stat_t -{ - /** - * sound direction searching data ready interrupt event. - * Writing '1' to clear this interrupt event. - * 0x1: data is ready for sound direction detect; - * 0x0: no event. - */ - uint32_t dir_search_data_rdy : 1; - /** - * voice output stream buffer data ready interrupt event. - * When a block of 512 voice samples are collected, this interrupt event - * is asserted. Writing '1' to clear this interrupt event. 0x1: voice - * output stream buffer data is ready; 0x0: no event. - */ - uint32_t voc_buf_data_rdy : 1; - uint32_t reserved : 30; -} __attribute__((packed, aligned(4))); - -struct audio_bf_int_mask_t -{ - /** - * This is the interrupt mask to dir searching data ready interrupt. - * 0x1: mask off this interrupt; - * 0x0: enable this interrupt. - */ - uint32_t dir_data_rdy_msk : 1; - /** - * This is the interrupt mask to voice output stream buffer ready - * interrupt. 0x1: mask off this interrupt; 0x0: enable this interrupt. - */ - uint32_t voc_buf_rdy_msk : 1; - uint32_t reserved : 30; -} __attribute__((packed, aligned(4))); - -struct audio_bf_reg_t -{ - /* 0x200 */ - struct audio_bf_ch_cfg_t bf_ch_cfg_reg; - /* 0x204 */ - struct audio_bf_ctl_t bf_ctl_reg; - /* 0x208 */ - struct audio_bf_dir_bidx_t bf_dir_bidx[16][2]; - /* 0x288 */ - struct audio_bf_fir_coef_t bf_pre_fir0_coef[9]; - /* 0x2ac */ - struct audio_bf_fir_coef_t bf_post_fir0_coef[9]; - /* 0x2d0 */ - struct audio_bf_fir_coef_t bf_pre_fir1_coef[9]; - /* 0x2f4 */ - struct audio_bf_fir_coef_t bf_post_fir1_coef[9]; - /* 0x318 */ - struct audio_bf_dwsz_cfg_t bf_dwsz_cfg_reg; - /* 0x31c */ - struct audio_bf_fft_cfg_t bf_fft_cfg_reg; - /* 0x320 */ - /** - * This is the read register for system DMA to read data stored in - * sample out buffers (the sample out buffers are used for sound - * direction detect). Each data contains two sound samples. - */ - volatile uint32_t sobuf_dma_rdata; - /* 0x324 */ - /** - * This is the read register for system DMA to read data stored in voice - * out buffers (the voice out buffers are used for voice recognition). - * Each data contains two sound samples. - */ - volatile uint32_t vobuf_dma_rdata; - /* 0x328 */ - struct audio_bf_int_stat_t bf_int_stat_reg; - /* 0x32c */ - struct audio_bf_int_mask_t bf_int_mask_reg; - /* 0x330 */ - uint32_t saturation_counter; - /* 0x334 */ - uint32_t saturation_limits; -} __attribute__((packed, aligned(4))); - -void audio_bf_set_audio_gain(uint16_t gain); -void audio_bf_set_smpl_shift(uint8_t smpl_shift); -uint8_t audio_bf_get_smpl_shift(void); -void audio_bf_set_channel_enabled(uint8_t channel_bit); -void audio_bf_set_direction_delay(uint8_t dir_num, uint8_t* dir_bidx); -void audio_bf_set_fft_shift_factor(uint8_t enable_flag, uint16_t shift_factor); -void audio_bf_set_down_size(uint8_t dir_dwn_siz, uint8_t voc_dwn_siz); -void audio_bf_set_interrupt_mask(uint8_t dir_int_mask, uint8_t voc_int_mask); - -void audio_bf_dir_enable(void); -void audio_bf_dir_reset(void); -void audio_bf_dir_set_prev_fir(uint16_t* fir_coef); -void audio_bf_dir_set_post_fir(uint16_t* fir_coef); -void audio_bf_dir_set_down_size(uint8_t dir_dwn_size); -void audio_bf_dir_set_interrupt_mask(uint8_t dir_int_mask); -void audio_bf_dir_clear_int_state(void); - -void audio_bf_voc_enable(uint8_t enable_flag); -void audio_bf_voc_reset(void); -void audio_bf_voc_set_direction(enum en_bf_dir direction); -void audio_bf_voc_set_prev_fir(uint16_t* fir_coef); -void audio_bf_voc_set_post_fir(uint16_t* fir_coef); -void audio_bf_voc_set_down_size(uint8_t voc_dwn_size); -void audio_bf_voc_set_interrupt_mask(uint8_t voc_int_mask); -void audio_bf_voc_clear_int_state(void); -void audio_bf_voc_reset_saturation_counter(void); -uint32_t audio_bf_voc_get_saturation_counter(void); -void audio_bf_voc_set_saturation_limit(uint16_t upper, uint16_t bottom); -uint32_t audio_bf_voc_get_saturation_limit(void); - -void audio_bf_print_setting(void); - -#ifdef __cplusplus -} -#endif - -#endif /* _DRIVER_AUDIO_BF_H */ diff --git a/lib/drivers/include/common.h b/lib/drivers/include/common.h deleted file mode 100644 index 28af6da..0000000 --- a/lib/drivers/include/common.h +++ /dev/null @@ -1,289 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef _DRIVER_COMMON_H -#define _DRIVER_COMMON_H - -#ifdef __cplusplus -#include -#include -#include -#else /* __cplusplus */ -#include -#include -#include -#include -#endif /* __cplusplus */ - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#define KENDRYTE_MIN(a, b) ((a) > (b) ? (b) : (a)) -#define KENDRYTE_MAX(a, b) ((a) > (b) ? (a) : (b)) - -#ifdef __ASSEMBLY__ -#define KENDRYTE_CAST(type, ptr) ptr -#else /* __ASSEMBLY__ */ -/** - * @brief Cast the pointer to specified pointer type. - * - * @param[in] type The pointer type to cast to - * @param[in] ptr The pointer to apply the type cast to - */ -#define KENDRYTE_CAST(type, ptr) ((type)(ptr)) -#endif /* __ASSEMBLY__ */ - -/** - * @addtogroup UTIL_RW_FUNC Memory Read/Write Utilities - * - * This section implements read and write functionality for various - * memory untis. The memory unit terms used for these functions are - * consistent with those used in the ARM Architecture Reference Manual - * ARMv7-A and ARMv7-R edition manual. The terms used for units of memory are: - * - * Unit of Memory | Abbreviation | Size in Bits - * :---------------|:-------------|:------------: - * Byte | byte | 8 - * Half Word | hword | 16 - * Word | word | 32 - * Double Word | dword | 64 - * - */ - -/** - * @brief Write the 8 bit byte to the destination address in device memory. - * - * @param[in] dest Write destination pointer address - * @param[in] src 8 bit data byte to write to memory - */ -#define kendryte_write_byte(dest, src) \ - (*KENDRYTE_CAST(volatile uint8_t*, (dest)) = (src)) - -/** - * @brief Read and return the 8 bit byte from the source address in device memory. - * - * @param[in] src Read source pointer address - * - * @return 8 bit data byte value - */ -#define kendryte_read_byte(src) (*KENDRYTE_CAST(volatile uint8_t*, (src))) - -/** - * @brief Write the 16 bit half word to the destination address in device memory. - * - * @param[in] dest Write destination pointer address - * @param[in] src 16 bit data half word to write to memory - */ -#define kendryte_write_hword(dest, src) \ - (*KENDRYTE_CAST(volatile uint16_t*, (dest)) = (src)) - -/** - * @brief Read and return the 16 bit half word from the source address in device - * - * @param[in] src Read source pointer address - * - * @return 16 bit data half word value - */ -#define kendryte_read_hword(src) (*KENDRYTE_CAST(volatile uint16_t*, (src))) - -/** - * @brief Write the 32 bit word to the destination address in device memory. - * - * @param[in] dest Write destination pointer address - * @param[in] src 32 bit data word to write to memory - */ -#define kendryte_write_word(dest, src) \ - (*KENDRYTE_CAST(volatile uint32_t*, (dest)) = (src)) - -/** - * @brief Read and return the 32 bit word from the source address in device memory. - * - * @param[in] src Read source pointer address - * - * @return 32 bit data half word value - */ -#define kendryte_read_word(src) (*KENDRYTE_CAST(volatile uint32_t*, (src))) - -/** - * @brief Write the 64 bit double word to the destination address in device memory. - * - * @param[in] dest Write destination pointer address - * @param[in] src 64 bit data word to write to memory - */ -#define kendryte_write_dword(dest, src) \ - (*KENDRYTE_CAST(volatile uint64_t*, (dest)) = (src)) - -/** - * @brief Read and return the 64 bit double word from the source address in device - * - * @param[in] src Read source pointer address - * - * @return 64 bit data half word value - */ -#define kendryte_read_dword(src) (*KENDRYTE_CAST(volatile uint64_t*, (src))) - -/** - * @brief Set selected bits in the 8 bit byte at the destination address in device - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to set in destination byte - */ -#define kendryte_setbits_byte(dest, bits) \ - (kendryte_write_byte(dest, kendryte_read_byte(dest) | (bits))) - -/** - * @brief Clear selected bits in the 8 bit byte at the destination address in device - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to clear in destination byte - */ -#define kendryte_clrbits_byte(dest, bits) \ - (kendryte_write_byte(dest, kendryte_read_byte(dest) & ~(bits))) - -/** - * @brief Change or toggle selected bits in the 8 bit byte at the destination address - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to change in destination byte - */ -#define kendryte_xorbits_byte(dest, bits) \ - (kendryte_write_byte(dest, kendryte_read_byte(dest) ^ (bits))) - -/** - * @brief Replace selected bits in the 8 bit byte at the destination address in device - * - * @param[in] dest Destination pointer address - * @param[in] msk Bits to replace in destination byte - * @param[in] src Source bits to write to cleared bits in destination byte - */ -#define kendryte_replbits_byte(dest, msk, src) \ - (kendryte_write_byte(dest, (kendryte_read_byte(dest) & ~(msk)) | ((src) & (msk)))) - -/** - * @brief Set selected bits in the 16 bit halfword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to set in destination halfword - */ -#define kendryte_setbits_hword(dest, bits) \ - (kendryte_write_hword(dest, kendryte_read_hword(dest) | (bits))) - -/** - * @brief Clear selected bits in the 16 bit halfword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to clear in destination halfword - */ -#define kendryte_clrbits_hword(dest, bits) \ - (kendryte_write_hword(dest, kendryte_read_hword(dest) & ~(bits))) - -/** - * @brief Change or toggle selected bits in the 16 bit halfword at the destination - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to change in destination halfword - */ -#define kendryte_xorbits_hword(dest, bits) \ - (kendryte_write_hword(dest, kendryte_read_hword(dest) ^ (bits))) - -/** - * @brief Replace selected bits in the 16 bit halfword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] msk Bits to replace in destination byte - * @param[in] src Source bits to write to cleared bits in destination halfword - */ -#define kendryte_replbits_hword(dest, msk, src) \ - (kendryte_write_hword(dest, (kendryte_read_hword(dest) & ~(msk)) | ((src) & (msk)))) - -/** - * @brief Set selected bits in the 32 bit word at the destination address in device - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to set in destination word - */ -#define kendryte_setbits_word(dest, bits) \ - (kendryte_write_word(dest, kendryte_read_word(dest) | (bits))) - -/** - * @brief Clear selected bits in the 32 bit word at the destination address in device - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to clear in destination word - */ -#define kendryte_clrbits_word(dest, bits) \ - (kendryte_write_word(dest, kendryte_read_word(dest) & ~(bits))) - -/** - * @brief Change or toggle selected bits in the 32 bit word at the destination address - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to change in destination word - */ -#define kendryte_xorbits_word(dest, bits) \ - (kendryte_write_word(dest, kendryte_read_word(dest) ^ (bits))) - -/** - * @brief Replace selected bits in the 32 bit word at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] msk Bits to replace in destination word - * @param[in] src Source bits to write to cleared bits in destination word - */ -#define kendryte_replbits_word(dest, msk, src) \ - (kendryte_write_word(dest, (kendryte_read_word(dest) & ~(msk)) | ((src) & (msk)))) - -/** - * @brief Set selected bits in the 64 bit doubleword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to set in destination doubleword - */ -#define kendryte_setbits_dword(dest, bits) \ - (kendryte_write_dword(dest, kendryte_read_dword(dest) | (bits))) - -/** - * @brief Clear selected bits in the 64 bit doubleword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to clear in destination doubleword - */ -#define kendryte_clrbits_dword(dest, bits) \ - (kendryte_write_dword(dest, kendryte_read_dword(dest) & ~(bits))) - -/** - * @brief Change or toggle selected bits in the 64 bit doubleword at the destination - * - * @param[in] dest Destination pointer address - * @param[in] bits Bits to change in destination doubleword - */ -#define kendryte_xorbits_dword(dest, bits) \ - (kendryte_write_dword(dest, kendryte_read_dword(dest) ^ (bits))) - -/** - * @brief Replace selected bits in the 64 bit doubleword at the destination address in - * - * @param[in] dest Destination pointer address - * @param[in] msk its to replace in destination doubleword - * @param[in] src Source bits to write to cleared bits in destination word - */ -#define kendryte_replbits_dword(dest, msk, src) \ - (kendryte_write_dword(dest, (kendryte_read_dword(dest) & ~(msk)) | ((src) & (msk)))) - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* _DRIVER_COMMON_H */ - diff --git a/lib/drivers/include/dmac.h b/lib/drivers/include/dmac.h index 1dd446e..128f05d 100644 --- a/lib/drivers/include/dmac.h +++ b/lib/drivers/include/dmac.h @@ -1303,7 +1303,7 @@ struct dmac_channel_t uint64_t reserved2[12]; } __attribute__((packed, aligned(8))); -struct dmac_t +typedef struct _dmac { /* (0x00) DMAC ID Rgister */ uint64_t id; @@ -1328,7 +1328,7 @@ struct dmac_t uint64_t reset; uint64_t reserved2[20]; struct dmac_channel_t channel[DMAC_CHANNEL_COUNT]; -} __attribute__((packed, aligned(8))); +} __attribute__((packed, aligned(8))) dmac_t; struct dmac_channel_config_t { diff --git a/lib/drivers/include/hard_fft.h b/lib/drivers/include/fft.h similarity index 97% rename from lib/drivers/include/hard_fft.h rename to lib/drivers/include/fft.h index 87c7160..6f0865d 100644 --- a/lib/drivers/include/hard_fft.h +++ b/lib/drivers/include/fft.h @@ -12,16 +12,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _DRIVER_HARD_FFT_H -#define _DRIVER_HARD_FFT_H - +#ifndef _DRIVER_FFT_H +#define _DRIVER_FFT_H #include -#include "platform.h" #ifdef __cplusplus extern "C" { #endif +typedef enum +{ + FFT_512, + FFT_256, + FFT_128, + FFT_64 +} fft_point_t; + /** * @brief FFT algorithm accelerator register * diff --git a/lib/drivers/include/io.h b/lib/drivers/include/io.h index 0915a19..967d707 100644 --- a/lib/drivers/include/io.h +++ b/lib/drivers/include/io.h @@ -46,8 +46,8 @@ extern "C" { uint32_t get_bit_mask(volatile uint32_t* bits, uint32_t mask); void set_bit_mask(volatile uint32_t* bits, uint32_t mask, uint32_t value); -uint32_t get_bit_idx(volatile uint32_t* bits, size_t idx); -void set_bit_idx(volatile uint32_t* bits, size_t idx, uint32_t value); +uint32_t get_bit_idx(volatile uint32_t* bits, uint32_t idx); +void set_bit_idx(volatile uint32_t* bits, uint32_t idx, uint32_t value); #ifdef __cplusplus } diff --git a/lib/drivers/include/otp.h b/lib/drivers/include/otp.h deleted file mode 100644 index 1597207..0000000 --- a/lib/drivers/include/otp.h +++ /dev/null @@ -1,361 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef _DRIVER_OTP_H -#define _DRIVER_OTP_H - -#include - -/* clang-format off */ -#define OTP_COMMON_DATA_ADDR 0x00000000U -#define OTP_SYSTEM_DATA_ADDR 0x00003AD0U -#define OTP_BISR_DATA_ADDR 0x00003DD0U -#define OTP_BLOCK_CTL_ADDR 0x00003FD0U -#define OTP_WIRED_REG_ADDR 0x00003FE0U -#define OTP_AES_KEY_ADDR 0x00003FF0U - -#define OTP_BUSY_FLAG 0x00000001U -#define OTP_BYPASS_FLAG 0x00000002U -#define OTP_TEST_FLAG 0x00000004U -/* clang-format on */ - -enum otp_status_t -{ - OTP_OK = 0, - OTP_ERROR_TIMEOUT, /* operation timeout*/ - OTP_ERROR_ADDRESS, /* invalid address*/ - OTP_ERROR_WRITE, /* write error*/ - OTP_ERROR_BLANK, /* blank check error*/ - OTP_ERROR_BISR, /* bisr error*/ - OTP_ERROR_TESTDEC, /* testdec error*/ - OTP_ERROR_WRTEST, /* wrtest error*/ - OTP_ERROR_KEYCOMP, /* key is wrong*/ - OTP_ERROR_PARAM, /* param error*/ - OTP_ERROR_NULL, /* undefine error*/ - OTP_BLOCK_NORMAL, /* block can be written*/ - OTP_BLOCK_PROTECTED,/* block can not be written*/ - OTP_FUNC_ENABLE, /* function available*/ - OTP_FUNC_DISABLE, /* function unavailable*/ - OTP_FLAG_SET, /* flag set*/ - OTP_FLAG_UNSET, /* flag unset*/ -}; - -enum otp_data_block_t -{ - COMMON_DATA_BLOCK1 = 0, - COMMON_DATA_BLOCK2, - COMMON_DATA_BLOCK3, - COMMON_DATA_BLOCK4, - COMMON_DATA_BLOCK5, - COMMON_DATA_BLOCK6, - COMMON_DATA_BLOCK7, - COMMON_DATA_BLOCK8, - COMMON_DATA_BLOCK9, - COMMON_DATA_BLOCK10, - COMMON_DATA_BLOCK11, - COMMON_DATA_BLOCK12, - COMMON_DATA_BLOCK13, - COMMON_DATA_BLOCK14, - COMMON_DATA_BLOCK15, - DATA_BLOCK_RESERVE, - SYSTEM_DATA_BLOCK1, - SYSTEM_DATA_BLOCK2, - SYSTEM_DATA_BLOCK3, - SYSTEM_DATA_BLOCK4, - SYSTEM_DATA_BLOCK5, - SYSTEM_DATA_BLOCK6, - SYSTEM_DATA_BLOCK7, - SYSTEM_DATA_BLOCK8, - SYSTEM_DATA_BLOCK9, - SYSTEM_DATA_BLOCK10, - SYSTEM_DATA_BLOCK11, - SYSTEM_DATA_BLOCK12, - SYSTEM_DATA_BLOCK13, - SYSTEM_DATA_BLOCK14, - SYSTEM_DATA_BLOCK15, - SYSTEM_DATA_BLOCK16, - SYSTEM_DATA_BLOCK17, - SYSTEM_DATA_BLOCK18, - SYSTEM_DATA_BLOCK19, - SYSTEM_DATA_BLOCK20, - SYSTEM_DATA_BLOCK21, - SYSTEM_DATA_BLOCK22, - SYSTEM_DATA_BLOCK23, - SYSTEM_DATA_BLOCK24, - SYSTEM_DATA_BLOCK25, - SYSTEM_DATA_BLOCK26, - SYSTEM_DATA_BLOCK27, - SYSTEM_DATA_BLOCK28, - SYSTEM_DATA_BLOCK29, - SYSTEM_DATA_BLOCK30, - SYSTEM_DATA_BLOCK31, - SYSTEM_DATA_BLOCK32, - SYSTEM_DATA_BLOCK33, - SYSTEM_DATA_BLOCK34, - SYSTEM_DATA_BLOCK35, - SYSTEM_DATA_BLOCK36, - SYSTEM_DATA_BLOCK37, - SYSTEM_DATA_BLOCK38, - SYSTEM_DATA_BLOCK39, - SYSTEM_DATA_BLOCK40, - SYSTEM_DATA_BLOCK41, - SYSTEM_DATA_BLOCK42, - SYSTEM_DATA_BLOCK43, - SYSTEM_DATA_BLOCK44, - SYSTEM_DATA_BLOCK45, - SYSTEM_DATA_BLOCK46, - SYSTEM_DATA_BLOCK47, - SYSTEM_DATA_BLOCK48, - DATA_BLOCK_MAX = 64, -}; - -enum otp_func_reg_t -{ - BLANK_TEST_DISABLE = 0, - RAM_BISR_DISABLE, - AES_WRITE_DISABLE, - AES_VERIFY_DISABLE, - JTAG_DISABLE, - TEST_EN_DISABLE = 6, - ISP_DISABLE, - OTP_FUNC_FIRMWARE_CIPHER_DISABLE, - FUNC_REG_MAX = 64, -}; - -struct otp_t -{ - volatile uint32_t otp_ceb; - volatile uint32_t otp_test_mode; - volatile uint32_t otp_mode; - volatile uint32_t gb_otp_en; - volatile uint32_t dat_in_finish; - volatile uint32_t otp_bisr_fail; - volatile uint32_t test_step; - volatile uint32_t otp_pwrrdy; - volatile uint32_t otp_last_dat; - volatile uint32_t otp_data; - volatile uint32_t otp_pwr_mode; - volatile uint32_t otp_in_dat; - volatile uint32_t otp_apb_adr; - volatile uint32_t td_result; - volatile uint32_t data_acp_flag; - volatile uint32_t otp_adr_in_flag; - volatile uint32_t wr_result; - volatile uint32_t otp_thershold; - volatile uint32_t bisr_finish; - volatile uint32_t key_cmp_result; - volatile uint32_t otp_cmp_key; - volatile uint32_t cmp_result_rdy; - volatile uint32_t otp_cle; - volatile uint32_t data_blk_ctrl; - volatile uint32_t otp_wrg_adr_flag; - volatile uint32_t pro_wrong; - volatile uint32_t otp_status; - volatile uint32_t otp_pro_adr; - volatile uint32_t blank_finish; - volatile uint32_t bisr2otp_en; - volatile uint32_t otp_cpu_ctrl; - volatile uint32_t otp_web_cpu; - volatile uint32_t otp_rstb_cpu; - volatile uint32_t otp_seltm_cpu; - volatile uint32_t otp_readen_cpu; - volatile uint32_t otp_pgmen_cpu; - volatile uint32_t otp_dle_cpu; - volatile uint32_t otp_din_cpu; - volatile uint32_t otp_cpumpen_cpu; - volatile uint32_t otp_cle_cpu; - volatile uint32_t otp_ceb_cpu; - volatile uint32_t otp_adr_cpu; - volatile uint32_t otp_dat_cpu; - volatile uint32_t otp_data_rdy; - volatile uint32_t block_flag_high; - volatile uint32_t block_flag_low; - volatile uint32_t reg_flag_high; - volatile uint32_t reg_flag_low; -} __attribute__((packed, aligned(4))); - -/** - * @brief Init OTP - * - * @note The otp clock frequency is 12.5M by default - * - * @param[in] div bus_clk / otp_clk - */ -void otp_init(uint8_t div); - -/** - * @brief Enable otp test mode - */ -void otp_test_enable(void); - -/** - * @brief Disable otp test mode - */ -void otp_test_disable(void); - -/** - * @brief Enable key output to aes - */ -void otp_key_output_enable(void); - -/** - * @brief Disable key output to aes - */ -void otp_key_output_disable(void); - -/** - * @brief Get the wrong address when programming fails - * - * @return The wrong address - */ -uint32_t otp_wrong_address_get(void); - -/** - * @brief Get OTP status - * - * @param[in] flag status flag - * - * @return Results of the operation - */ -enum otp_status_t otp_status_get(uint32_t flag); - -/** - * @brief Perform the blank check operation - * - * @return Results of the operation - */ -enum otp_status_t otp_blank_check(void); - -/** - * @brief Perform the testdec operation - * - * @return Results of the operation - */ -enum otp_status_t otp_testdec(void); - -/** - * @brief Perform the wrtest operation - * - * @return Results of the operation - */ -enum otp_status_t otp_wrtest(void); - -/** - * @brief Write data - * - * @param[in] addr Start programming address(bit) - * @param[in] data_buf Need to write the data point - * @param[in] length Need to write the data length(bit) - * - * @return Results of the operation - */ -enum otp_status_t otp_write_data(uint32_t addr, uint8_t *data_buf, uint32_t length); - -/** - * @brief Read data - * - * @param[in] addr Start read address(bit). - * @param[in] data_buf Need to read the data point - * @param[in] length Need to read the data length(bit) - * - * @return Results of the operation - */ -enum otp_status_t otp_read_data(uint32_t addr, uint8_t *data_buf, uint32_t length); - -/** - * @brief Write the key - * - * @param[in] data_buf The key data,length is 128 bits(4 words) - * - * @return Results of the operation - */ -enum otp_status_t otp_key_write(uint8_t *data_buf); - -/** - * @brief Compare the key - * - * @param[in] data_buf The key data,length is 128 bits(4 words) - * - * @return Results of the operation - */ -enum otp_status_t otp_key_compare(uint8_t *data_buf); - -/** - * @brief Data block write protect - * - * @param[in] block Need to write a protected data block - * - * @return Results of the operation - */ -enum otp_status_t otp_data_block_protect_set(enum otp_data_block_t block); - -/** - * @brief Disable the specified function - * - * @param[in] reg Need to disable the function - * - * @return Results of the operation - */ -enum otp_status_t otp_func_reg_disable_set(enum otp_func_reg_t func); - -/** - * @brief Get the data block status - * - * @param[in] block The specified data block - * - * @return Results of the operation - */ -enum otp_status_t otp_data_block_protect_get(enum otp_data_block_t block); - -/** - * @brief Get the function status - * - * @param[in] reg The specified function - * - * @return Results of the operation - */ -enum otp_status_t otp_func_reg_disable_get(enum otp_func_reg_t func); - -/** - * @brief Refresh the data block status - * - * @param[in] block The specified data block - * - * @return Results of the operation - */ -enum otp_status_t otp_data_block_protect_refresh(enum otp_data_block_t block); - -/** - * @brief Write data(bypass mode) - * - * @param[in] addr Start programming address(bit) - * @param[in] data_buf Need to write the data point - * @param[in] length Need to write the data length(bit) - * - * @return Results of the operation - */ -enum otp_status_t otp_soft_write(uint32_t addr, uint8_t *data_buf, uint32_t length); - -/** - * @brief Read data(bypass mode) - * - * @param[in] addr Start read address(bit).Be sure to align 16 bits - * @param[in] data_buf Need to read the data point - * @param[in] length Need to read the data length(half word/16bits) - * - * @return Results of the operation - */ -enum otp_status_t otp_soft_read(uint32_t addr, uint8_t *data_buf, uint32_t length); - -#endif diff --git a/lib/drivers/include/rtc.h b/lib/drivers/include/rtc.h index 00a1f99..8592470 100644 --- a/lib/drivers/include/rtc.h +++ b/lib/drivers/include/rtc.h @@ -272,28 +272,6 @@ struct rtc_register_ctrl_t uint32_t resv1 : 1; } __attribute__((packed, aligned(4))); -/** - * @brief Reserved - * - * No. 8 Register (0x20) - */ -struct rtc_reserved0_t -{ - /* Reserved */ - uint32_t resv : 32; -} __attribute__((packed, aligned(4))); - -/** - * @brief Reserved - * - * No. 9 Register (0x24) - */ -struct rtc_reserved1_t -{ - /* Reserved */ - uint32_t resv : 32; -} __attribute__((packed, aligned(4))); - /** * @brief Timer extended information * @@ -309,14 +287,13 @@ struct rtc_extended_t uint32_t resv : 26; } __attribute__((packed, aligned(4))); - /** * @brief Real-time clock struct * * A real-time clock (RTC) is a computer clock that keeps track of * the current time. */ -struct rtc_t +typedef struct _rtc { /* No. 0 (0x00): Timer date information */ struct rtc_date_t date; @@ -335,266 +312,12 @@ struct rtc_t /* No. 7 (0x1c): RTC register settings */ struct rtc_register_ctrl_t register_ctrl; /* No. 8 (0x20): Reserved */ - struct rtc_reserved0_t reserved0; + uint32_t reserved0; /* No. 9 (0x24): Reserved */ - struct rtc_reserved1_t reserved1; + uint32_t reserved1; /* No. 10 (0x28): Timer extended information */ struct rtc_extended_t extended; -} __attribute__((packed, aligned(4))); - - -/** - * @brief Real-time clock object - */ -extern volatile struct rtc_t *const rtc; -extern volatile uint32_t *const rtc_base; -/** - * @brief Set RTC timer mode - * - * @param[in] timer_mode The timer mode - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_mode_set(rtc_timer_mode_e timer_mode); - -/** - * @brief Get RTC timer mode - * - * @return The timer mode - */ -rtc_timer_mode_e rtc_timer_get_mode(void); - -/** - * @brief Set date time to RTC - * - * @param[in] tm The Broken-down date time - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_set_tm(const struct tm *tm); - -/** - * @brief Get date time from RTC - * - * @return The Broken-down date time - */ -struct tm *rtc_timer_get_tm(void); - -/** - * @brief Set date time to Alarm - * - * @param[in] tm The Broken-down date time - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_set_alarm_tm(const struct tm *tm); - -/** - * @brief Get date time from Alarm - * - * @return The Broken-down date time - */ -struct tm *rtc_timer_get_alarm_tm(void); - -/** - * @brief Check if it is a leap year - * - * @param[in] year The year - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_year_is_leap(int year); - -/** - * @brief Get day of year from date - * - * @param[in] year The year - * @param[in] month The month - * @param[in] day The day - * - * @return The day of year from date - */ -int rtc_get_yday(int year, int month, int day); - -/** - * @brief Get the day of the week from date - * - * @param[in] year The year - * @param[in] month The month - * @param[in] day The day - * - * @return The day of the week. - * Where Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, - * Thursday = 4, Friday = 5, Saturday = 6. - */ -int rtc_get_wday(int year, int month, int day); - -/** - * @brief Set date time to RTC - * - * @param[in] year The year - * @param[in] month The month - * @param[in] day The day - * @param[in] hour The hour - * @param[in] minute The minute - * @param[in] second The second - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_set(int year, int month, int day, int hour, int minute, int second); - -/** - * @brief Get date time from RTC - * - * @param year The year - * @param month The month - * @param day The day - * @param hour The hour - * @param minute The minute - * @param second The second - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_get(int *year, int *month, int *day, int *hour, int *minute, int *second); - -/** - * @brief Set date time to Alarm - * - * @param[in] year The year - * @param[in] month The month - * @param[in] day The day - * @param[in] hour The hour - * @param[in] minute The minute - * @param[in] second The second - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_set_alarm(int year, int month, int day, int hour, int minute, int second); - -/** - * @brief Get date time from Alarm - * - * @param year The year - * @param month The month - * @param day The day - * @param hour The hour - * @param minute The minute - * @param second The second - * - * @return result - * - 0 Success - * - Other Fail - */ -int rtc_timer_get_alarm(int *year, int *month, int *day, int *hour, int *minute, int *second); - -/** - * @brief Set rtc tick interrupt - * - * @param enable enable or disale rtc interrupt - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_tick_interrupt_set(int enable); - -/** - * @brief Get tick interrupt - * - * - * @return Result - * - 0 Disable - * - 1 Enable - */ -int rtc_tick_interrupt_get(void); - -/** - * @brief Set tick interrupt mode - * - * @param mode The mode interrumpted - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_tick_interrupt_mode_set(rtc_tick_interrupt_mode_e mode); - -/** - * @brief Get tick interrupt mode - * - * @return Tick interrupt mode - */ -rtc_tick_interrupt_mode_e rtc_tick_interrupt_mode_get(void); - -/** - * @brief Enable alarm interrupt - * - * @param enable Enable or disable alarm interrupt - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_alarm_interrupt_set(int enable); - -/** - * @brief Get alarm interrupt status - * - * @return Alarm interrupt status - */ -int rtc_alarm_interrupt_get(void); - -/** - * @brief Set alarm interrupt mask - * - * @param mask Alarm interrupt mask - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_alarm_interrupt_mask_set(struct rtc_mask_t mask); - -/** - * @brief Get alarm interrupt mask - * - * @return Alarm interrupt mask - */ -struct rtc_mask_t rtc_alarm_interrupt_mask_get(void); - -/** - * @brief Initialize RTC - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_init(void); - -/** - * @brief RTC timer set mode - * - * @param timer_mode timer mode - * - * @return Result - * - 0 Success - * - Other Fail - */ -int rtc_timer_set_mode(rtc_timer_mode_e timer_mode); +} __attribute__((packed, aligned(4))) rtc_t; #ifdef __cplusplus } diff --git a/lib/drivers/include/sha256.h b/lib/drivers/include/sha256.h index 65900eb..e421b5c 100644 --- a/lib/drivers/include/sha256.h +++ b/lib/drivers/include/sha256.h @@ -14,82 +14,33 @@ */ #ifndef _SHA256_H #define _SHA256_H - #include -#include "encoding.h" -#include "platform.h" -#define DISABLE_SHA_DMA 0 -#define ENABLE_SHA_DMA 1 +#define SHA256_HASH_WORDS 8 +#define SHA256_HASH_LEN 32 -#define DISABLE_DOUBLE_SHA 0 -/** - * @brief AES - * - */ -struct sha256_t +typedef struct _sha256 { uint32_t sha_result[8]; uint32_t sha_data_in1; uint32_t sha_data_in2; - uint32_t sha_data_num; /*1 unit represents 64 bytes*/ + uint32_t sha_data_num; uint32_t sha_status; uint32_t reserved0; uint32_t sha_input_ctrl; -} __attribute__((packed, aligned(4))); - -#define SHA256_HASH_SIZE 32 - -/* Hash size in 32-bit words */ -#define SHA256_HASH_WORDS 8 +} __attribute__((packed, aligned(4))) sha256_t; -struct _SHA256Context +typedef struct _sha256_context { - uint64_t totalLength; - uint32_t hash[SHA256_HASH_WORDS]; - uint32_t bufferLength; + size_t total_len; + size_t dma_buf_len; + uint32_t *dma_buf; + size_t buffer_len; union { uint32_t words[16]; uint8_t bytes[64]; } buffer; -#ifdef RUNTIME_ENDIAN - int littleEndian; -#endif /* RUNTIME_ENDIAN */ -}; - -typedef struct _SHA256Context SHA256Context; - -/** - * @brief Sha256 initialize - * - * @param[in] dma_en Dma enable flag - * @param[in] input_size Input size - * @param[in] sc Sha256 Context point - * - * @return Result - * - 0 Success - * - Other Fail - */ -int sha256_init(uint8_t dma_en, uint32_t input_size, SHA256Context *sc); - -/** - * @brief Sha256 update - * - * @param[in] sc Sha256 Context point - * @param[in] data Input data point - * @param[in] len Input data size - * - */ -void sha256_update(SHA256Context *sc, const void *data, uint32_t len); - -/** - * @brief Sha256 final - * - * @param[in] sc Sha256 Context point - * @param[out] hash Sha256 result - * - */ -void sha256_final(SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]); +} sha256_context_t; #endif diff --git a/lib/drivers/include/uarths.h b/lib/drivers/include/uarths.h index 93a8bdb..99f1dae 100644 --- a/lib/drivers/include/uarths.h +++ b/lib/drivers/include/uarths.h @@ -170,8 +170,6 @@ struct uarths_t struct uarths_div_t div; } __attribute__((packed, aligned(4))); -extern volatile struct uarths_t *const uarths; - /** * @brief Initialization Core UART * @@ -179,7 +177,7 @@ extern volatile struct uarths_t *const uarths; * - 0 Success * - Other Fail */ -int uart_init(); +int uarths_init(); /** * @brief Put a char to UART @@ -192,7 +190,7 @@ int uart_init(); * - 0 Success * - Other Fail */ -int uart_putchar(char c); +int uarths_putchar(char c); /** * @brief Send a string to UART @@ -205,7 +203,7 @@ int uart_putchar(char c); * - 0 Success * - Other Fail */ -int uart_puts(const char *s); +int uarths_puts(const char *s); /** @@ -214,7 +212,7 @@ int uart_puts(const char *s); * @return byte as int type from UART */ -int uart_getc(void); +int uarths_getc(void); #ifdef __cplusplus diff --git a/lib/drivers/include/wdt.h b/lib/drivers/include/wdt.h index 647d622..b4f8739 100644 --- a/lib/drivers/include/wdt.h +++ b/lib/drivers/include/wdt.h @@ -24,7 +24,7 @@ extern "C" { #endif /* clang-format off */ -struct wdt_t +typedef struct _wdt { /* WDT Control Register (0x00) */ volatile uint32_t cr; @@ -58,7 +58,7 @@ struct wdt_t volatile uint32_t comp_version; /* WDT Component Type Register (0xfc) */ volatile uint32_t comp_type; -} __attribute__((packed, aligned(4))); +} __attribute__((packed, aligned(4))) wdt_t; #define WDT_RESET_ALL 0x00000000U @@ -110,77 +110,6 @@ struct wdt_t #define WDT_COMP_VERSION_MASK 0xFFFFFFFFU /* WDT Component Type Register */ #define WDT_COMP_TYPE_MASK 0xFFFFFFFFU -/* clang-format on */ - -/** - * @brief WDT object instanse - */ -extern volatile struct wdt_t *const wdt[2]; - -/** - * @brief Feed wdt - */ -void wdt_feed(uint8_t id); - -/** - * @brief Enable wdt - * - * @param[in] id Wdt id 0 or 1 - * - */ -void wdt_enable(uint8_t id); - -/** - * @brief Clear wdt interrupt - * - * @param[in] id Wdt id 0 or 1 - * - */ -void wdt_interrupt_clear(uint8_t id); - -/** - * @brief Clear wdt interrupt - * - * @param[in] id Wdt id 0 or 1 - * @param[in] mode Set wdt work mode - * - */ -void wdt_response_mode(uint8_t id, uint8_t mode); - -/** - * @brief Set wdt timeout - * - * @param[in] id Wdt id 0 or 1 - * @param[in] timeout Wdt trigger time - * - */ -void wdt_timeout_set(uint8_t id, uint8_t timeout); - -/** - * @brief Start wdt - * - * @param[in] id Wdt id 0 or 1 - * @param[in] toms Wdt trigger time - * - */ -int wdt_start(uint8_t id, size_t toms); - -/** - * @brief Stop wdt - * - * @param[in] id Wdt id 0 or 1 - * - */ -void wdt_stop(uint8_t id); - -/** - * @brief Set wdt interrupt function - * - * @param[in] id Wdt id 0 or 1 - * @param[in] on_irq Wdt interrupt function - * - */ -void wdt_set_irq(uint8_t id, plic_irq_callback_t on_irq); #ifdef __cplusplus } diff --git a/lib/drivers/io.c b/lib/drivers/io.c index 4b47412..2bea17d 100644 --- a/lib/drivers/io.c +++ b/lib/drivers/io.c @@ -25,12 +25,12 @@ void set_bit_mask(volatile uint32_t* bits, uint32_t mask, uint32_t value) *bits = org | (value & mask); } -uint32_t get_bit_idx(volatile uint32_t* bits, size_t idx) +uint32_t get_bit_idx(volatile uint32_t* bits, uint32_t idx) { return ((*bits) & (1 << idx)) >> idx; } -void set_bit_idx(volatile uint32_t* bits, size_t idx, uint32_t value) +void set_bit_idx(volatile uint32_t* bits, uint32_t idx, uint32_t value) { uint32_t org = (*bits) & ~(1 << idx); *bits = org | (value << idx); diff --git a/lib/drivers/otp.c b/lib/drivers/otp.c deleted file mode 100644 index 03142a7..0000000 --- a/lib/drivers/otp.c +++ /dev/null @@ -1,602 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "otp.h" -#include "platform.h" -#include "sysctl.h" - -/* clang-format off */ -#define DELAY_TIMEOUT 0xFFFFFF -#define WRTEST_NUM 0xA5 -/* clang-format on */ - -volatile struct otp_t* const otp = (volatile struct otp_t*)OTP_BASE_ADDR; - -void otp_init(uint8_t div) -{ - sysctl_clock_enable(SYSCTL_CLOCK_OTP); - otp->otp_cpu_ctrl = 0; - otp->otp_thershold = div; - otp->data_blk_ctrl = 0; - otp->gb_otp_en = 0; - otp->otp_pwr_mode = 0; - otp->otp_web_cpu = 1; - otp->otp_rstb_cpu = 1; - otp->otp_seltm_cpu = 0; - otp->otp_readen_cpu = 0; - otp->otp_pgmen_cpu = 0; - otp->otp_dle_cpu = 0; - otp->otp_din_cpu = 0; - otp->otp_cpumpen_cpu = 0; - otp->otp_cle_cpu = 0; - otp->otp_ceb_cpu = 1; - otp->otp_adr_cpu = 0; - otp->otp_dat_cpu = 0; -} - -void otp_test_enable(void) -{ - otp->otp_cpu_ctrl = 0xCAAC; -} - -void otp_test_disable(void) -{ - otp->otp_cpu_ctrl = 0; -} - -void otp_key_output_enable(void) -{ - otp->gb_otp_en = 1; -} - -void otp_key_output_disable(void) -{ - otp->gb_otp_en = 0; -} - -enum otp_status_t otp_status_get(uint32_t flag) -{ - if (otp->otp_status & flag) - return OTP_FLAG_SET; - return OTP_FLAG_UNSET; -} - -static enum otp_status_t otp_bisr_write(void) -{ - uint32_t time_out = 0; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x30; - otp->test_step = 0; - otp->otp_ceb = 0; - while (otp->bisr_finish == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - otp->bisr_finish = 0; - if (otp->pro_wrong) - return OTP_ERROR_BISR; - return OTP_OK; -} - -static enum otp_status_t otp_bisr_read(void) -{ - uint32_t time_out = 0; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x30; - otp->test_step = 1; - otp->otp_ceb = 0; - while (otp->bisr_finish == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - otp->bisr_finish = 0; - return OTP_OK; -} - -enum otp_status_t otp_blank_check(void) -{ - enum otp_status_t status; - uint32_t time_out = 0; - - if (otp_func_reg_disable_get(BLANK_TEST_DISABLE) == OTP_FUNC_DISABLE) - return OTP_FUNC_DISABLE; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x24; - otp->blank_finish = 0; - otp->otp_ceb = 0; - while (otp->blank_finish == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->otp_bisr_fail) - return OTP_ERROR_BLANK; - - status = otp_bisr_write(); - if (status != OTP_OK) - return status; - status = otp_bisr_read(); - if (status != OTP_OK) - return status; - status = otp_func_reg_disable_set(BLANK_TEST_DISABLE); - if (status != OTP_OK) - return status; - return OTP_OK; -} - -enum otp_status_t otp_testdec(void) -{ - uint32_t time_out = 0; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x21; - otp->otp_ceb = 0; - while (otp->td_result == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->td_result == 0x01) - { - otp->td_result = 0; - return OTP_ERROR_TESTDEC; - } - return OTP_OK; -} - -enum otp_status_t otp_wrtest(void) -{ - uint16_t addr, data, i, j; - uint32_t time_out; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x01; - otp->test_step = 0; - otp->data_acp_flag = 0; - otp->otp_ceb = 0; - addr = 0; - for (i = 0; i < 128; i++) - { - data = WRTEST_NUM; - for (j = 0; j < 8; j++) - { - if ((addr == 1023) || (data & 0x01)) - { - time_out = 0; - while (otp->otp_adr_in_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - otp->otp_apb_adr = addr; - if (addr == 1023) - { - otp->otp_in_dat = data & 0x01; - otp->otp_last_dat = 0x01; - } - else - otp->otp_in_dat = 0x01; - otp->dat_in_finish = 0x01; - time_out = 0; - while (otp->data_acp_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->data_acp_flag == 0x01) - { - otp->data_acp_flag = 0; - return OTP_ERROR_WRITE; - } - otp->data_acp_flag = 0; - } - data >>= 1; - addr++; - } - } - time_out = 0; - while ((otp->wr_result & 0x04) == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - otp->wr_result &= 0xFFFFFFFB; - - otp->otp_cle = 1; - otp->otp_mode = 0x02; - otp->otp_test_mode = 0x01; - otp->test_step = 1; - otp->data_acp_flag = 0; - otp->otp_ceb = 0; - addr = 0; - for (i = 0; i < 128; i++) - { - time_out = 0; - while (otp->otp_adr_in_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - if (otp->wr_result == 0x01) - { - otp->wr_result = 0; - return OTP_ERROR_WRTEST; - } - } - otp->otp_in_dat = WRTEST_NUM; - otp->otp_apb_adr = addr; - if (i == 127) - otp->otp_last_dat = 0x01; - addr += 8; - } - time_out = 0; - while ((otp->wr_result & 0x03) == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if ((otp->wr_result & 0x03) == 0x01) - { - otp->wr_result = 0; - return OTP_ERROR_WRTEST; - } - return OTP_OK; -} - -static enum otp_status_t otp_write_byte(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - enum otp_status_t status; - uint8_t data, index; - uint32_t time_out; - - otp->otp_cle = 0; - otp->otp_mode = 1; - otp->data_acp_flag = 0; - otp->otp_wrg_adr_flag = 0; - otp->otp_ceb = 0; - index = 0; - addr *= 8; - length *= 8; - data = *data_buf++; - while (length--) - { - if ((length == 0) || (data & 0x01)) - { - time_out = 0; - while (otp->otp_adr_in_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - otp->otp_apb_adr = addr; - if (length == 0) - { - otp->otp_in_dat = data & 0x01; - otp->otp_last_dat = 1; - } - else - otp->otp_in_dat = 1; - otp->dat_in_finish = 1; - time_out = 0; - while (otp->data_acp_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->otp_wrg_adr_flag == 1) - return OTP_ERROR_ADDRESS; - if (otp->data_acp_flag == 1) - return OTP_ERROR_WRITE; - otp->data_acp_flag = 0; - } - data >>= 1; - addr++; - index++; - if (index == 8) - { - index = 0; - data = *data_buf++; - } - } - - status = otp_bisr_write(); - if (status != OTP_OK) - return status; - status = otp_bisr_read(); - if (status != OTP_OK) - return status; - return OTP_OK; -} - -static enum otp_status_t otp_read_byte(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - uint32_t time_out; - - otp->otp_cle = 0; - otp->otp_mode = 0; - otp->otp_wrg_adr_flag = 0; - otp->otp_ceb = 0; - addr *= 8; - while (length--) - { - time_out = 0; - while (otp->otp_adr_in_flag == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (length == 0) - otp->otp_last_dat = 1; - otp->otp_apb_adr = addr; - time_out = 0; - while (otp->otp_data_rdy == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->otp_wrg_adr_flag == 0x01) - return OTP_ERROR_ADDRESS; - *data_buf++ = otp->otp_data; - addr += 8; - } - return OTP_OK; -} - -enum otp_status_t otp_write_data(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - enum otp_status_t status; - - if (addr >= OTP_BISR_DATA_ADDR) - return OTP_ERROR_ADDRESS; - length = length <= OTP_BISR_DATA_ADDR - addr ? length : OTP_BISR_DATA_ADDR - addr; - - status = otp_write_byte(addr, data_buf, length); - if (status == OTP_ERROR_ADDRESS) - status = OTP_BLOCK_PROTECTED; - return status; -} - -enum otp_status_t otp_read_data(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - enum otp_status_t status; - - if (addr >= OTP_BISR_DATA_ADDR) - return OTP_ERROR_ADDRESS; - length = length <= OTP_BISR_DATA_ADDR - addr ? length : OTP_BISR_DATA_ADDR - addr; - - status = otp_read_byte(addr, data_buf, length); - if (status == OTP_ERROR_ADDRESS) - status = OTP_ERROR_NULL; - return status; -} - -enum otp_status_t otp_key_write(uint8_t* data_buf) -{ - enum otp_status_t status; - - status = otp_write_byte(OTP_AES_KEY_ADDR, data_buf, 16); - if (status == OTP_ERROR_ADDRESS) - status = OTP_FUNC_DISABLE; - return status; -} - -enum otp_status_t otp_key_compare(uint8_t* data_buf) -{ - uint32_t time_out = 0; - - otp->key_cmp_result = 0; - otp->otp_cmp_key = ((uint32_t)data_buf[0] << 24) | ((uint32_t)data_buf[1] << 16) | ((uint32_t)data_buf[2] << 8) | (uint32_t)data_buf[3]; - otp->otp_cmp_key = ((uint32_t)data_buf[4] << 24) | ((uint32_t)data_buf[5] << 16) | ((uint32_t)data_buf[6] << 8) | (uint32_t)data_buf[7]; - otp->otp_cmp_key = ((uint32_t)data_buf[8] << 24) | ((uint32_t)data_buf[9] << 16) | ((uint32_t)data_buf[10] << 8) | (uint32_t)data_buf[11]; - otp->otp_cmp_key = ((uint32_t)data_buf[12] << 24) | ((uint32_t)data_buf[13] << 16) | ((uint32_t)data_buf[14] << 8) | (uint32_t)data_buf[15]; - while (otp->key_cmp_result == 0) - { - time_out++; - if (time_out >= DELAY_TIMEOUT) - return OTP_ERROR_TIMEOUT; - } - if (otp->key_cmp_result == 0x01) - return OTP_ERROR_KEYCOMP; - else if (otp->key_cmp_result == 0x03) - return OTP_FUNC_DISABLE; - return OTP_OK; -} - -enum otp_status_t otp_data_block_protect_set(enum otp_data_block_t block) -{ - enum otp_status_t status; - uint8_t value; - - if (block >= DATA_BLOCK_MAX) - return OTP_ERROR_PARAM; - otp->data_blk_ctrl = 0x01; - value = 0x03 << ((block % 4) * 2); - status = otp_write_byte(OTP_BLOCK_CTL_ADDR + block / 4, &value, 1); - otp->data_blk_ctrl = 0; - return status; -} - -enum otp_status_t otp_func_reg_disable_set(enum otp_func_reg_t func) -{ - enum otp_status_t status; - uint8_t value; - - if (func >= FUNC_REG_MAX) - return OTP_ERROR_PARAM; - otp->data_blk_ctrl = 0x01; - value = 0x03 << ((func % 4) * 2); - status = otp_write_byte(OTP_WIRED_REG_ADDR + func / 4, &value, 1); - otp->data_blk_ctrl = 0; - return status; -} - -enum otp_status_t otp_data_block_protect_get(enum otp_data_block_t block) -{ - if (block < DATA_BLOCK_MAX / 2) - { - if (otp->block_flag_low & (0x01 << block)) - return OTP_BLOCK_PROTECTED; - } - else if (block < DATA_BLOCK_MAX) - { - if (otp->block_flag_high & (0x01 << (block - DATA_BLOCK_MAX / 2))) - return OTP_BLOCK_PROTECTED; - } - else - return OTP_ERROR_PARAM; - return OTP_BLOCK_NORMAL; -} - -enum otp_status_t otp_func_reg_disable_get(enum otp_func_reg_t func) -{ - if (func < FUNC_REG_MAX / 2) - { - if (otp->reg_flag_low & (0x01 << func)) - return OTP_FUNC_DISABLE; - } - else if (func < FUNC_REG_MAX) - { - if (otp->reg_flag_high & (0x01 << (func - FUNC_REG_MAX / 2))) - return OTP_FUNC_DISABLE; - } - else - return OTP_ERROR_PARAM; - return OTP_FUNC_ENABLE; -} - -enum otp_status_t otp_data_block_protect_refresh(enum otp_data_block_t block) -{ - uint8_t value; - - if (block < DATA_BLOCK_MAX) - return otp_read_byte(OTP_BLOCK_CTL_ADDR + block / 4, &value, 1); - else - return OTP_ERROR_PARAM; -} - -enum otp_status_t otp_soft_write(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - uint8_t data, index, count; - - otp->otp_ceb_cpu = 1; - otp->otp_cle_cpu = 0; - otp->otp_seltm_cpu = 0; - otp->otp_readen_cpu = 0; - otp->otp_dle_cpu = 0; - otp->otp_web_cpu = 1; - otp->otp_cpumpen_cpu = 0; - otp->otp_pgmen_cpu = 0; - otp->otp_rstb_cpu = 1; - - otp->otp_ceb_cpu = 0; - otp->otp_rstb_cpu = 0; - otp->otp_rstb_cpu = 1; - - index = 0; - addr *= 8; - length *= 8; - data = *data_buf++; - while (length) - { - otp->otp_adr_cpu = addr; - otp->otp_din_cpu = data & 0x01; - otp->otp_dle_cpu = 1; - otp->otp_web_cpu = 0; - otp->otp_web_cpu = 1; - otp->otp_dle_cpu = 0; - count = 20; - while (count--) - { - otp->otp_pgmen_cpu = 1; - otp->otp_cpumpen_cpu = 1; - otp->otp_web_cpu = 0; - otp->otp_web_cpu = 1; - otp->otp_cpumpen_cpu = 0; - otp->otp_pgmen_cpu = 0; - if (otp->otp_dat_cpu == 0) - break; - } - if (otp->otp_dat_cpu & 0x01) - break; - data >>= 1; - addr++; - index++; - if (index == 8) - { - index = 0; - data = *data_buf++; - } - length--; - } - otp->otp_ceb_cpu = 1; - if (length) - return OTP_ERROR_WRITE; - return OTP_OK; -} - -enum otp_status_t otp_soft_read(uint32_t addr, uint8_t* data_buf, uint32_t length) -{ - otp->otp_ceb_cpu = 1; - otp->otp_dle_cpu = 0; - otp->otp_cle_cpu = 0; - otp->otp_pgmen_cpu = 0; - otp->otp_web_cpu = 1; - otp->otp_readen_cpu = 0; - otp->otp_rstb_cpu = 1; - - otp->otp_ceb_cpu = 0; - otp->otp_rstb_cpu = 0; - otp->otp_rstb_cpu = 1; - - while (length) - { - otp->otp_adr_cpu = addr; - otp->otp_readen_cpu = 1; - *data_buf++ = otp->otp_dat_cpu; - otp->otp_readen_cpu = 0; - addr += 8; - length--; - } - otp->otp_ceb_cpu = 1; - if (length) - return OTP_ERROR_WRITE; - return OTP_OK; -} - -uint32_t otp_wrong_address_get(void) -{ - return otp->otp_pro_adr; -} diff --git a/lib/drivers/rtc.c b/lib/drivers/rtc.c deleted file mode 100644 index cc84bdd..0000000 --- a/lib/drivers/rtc.c +++ /dev/null @@ -1,563 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include -#include -#include "encoding.h" -#include "sysctl.h" -#include "rtc.h" - -volatile struct rtc_t *const rtc = (volatile struct rtc_t *)RTC_BASE_ADDR; - -struct tm rtc_date_time; - -int rtc_timer_set_mode(rtc_timer_mode_e timer_mode) -{ - struct rtc_register_ctrl_t register_ctrl = rtc->register_ctrl; - - switch (timer_mode) { - case RTC_TIMER_PAUSE: - register_ctrl.read_enable = 0; - register_ctrl.write_enable = 0; - break; - case RTC_TIMER_RUNNING: - register_ctrl.read_enable = 1; - register_ctrl.write_enable = 0; - break; - case RTC_TIMER_SETTING: - register_ctrl.read_enable = 0; - register_ctrl.write_enable = 1; - break; - default: - register_ctrl.read_enable = 0; - register_ctrl.write_enable = 0; - break; - } - - rtc->register_ctrl = register_ctrl; - - return 0; -} - -rtc_timer_mode_e rtc_timer_get_mode(void) -{ - struct rtc_register_ctrl_t register_ctrl = rtc->register_ctrl; - rtc_timer_mode_e timer_mode = RTC_TIMER_PAUSE; - - if ((!register_ctrl.read_enable) && (!register_ctrl.write_enable)) { - /* RTC_TIMER_PAUSE */ - timer_mode = RTC_TIMER_PAUSE; - } else if ((register_ctrl.read_enable) && (!register_ctrl.write_enable)) { - /* RTC_TIMER_RUNNING */ - timer_mode = RTC_TIMER_RUNNING; - } else if ((!register_ctrl.read_enable) && (register_ctrl.write_enable)) { - /* RTC_TIMER_SETTING */ - timer_mode = RTC_TIMER_RUNNING; - } else { - /* Something is error, reset timer mode */ - rtc_timer_set_mode(timer_mode); - } - - return timer_mode; -} - -static inline int rtc_in_range(int value, int min, int max) -{ - return ((value >= min) && (value <= max)); -} - -int rtc_timer_set_tm(const struct tm *tm) -{ - struct rtc_date_t timer_date; - struct rtc_time_t timer_time; - struct rtc_extended_t timer_extended; - - if (tm) { - /* - * Range of tm->tm_sec could be [0,61] - * - * Range of tm->tm_sec allows for a positive leap second. Two - * leap seconds in the same minute are not allowed (the C90 - * range 0..61 was a defect) - */ - if (rtc_in_range(tm->tm_sec, 0, 59)) - timer_time.second = tm->tm_sec; - else - return -1; - - /* Range of tm->tm_min could be [0,59] */ - if (rtc_in_range(tm->tm_min, 0, 59)) - timer_time.minute = tm->tm_min; - else - return -1; - - /* Range of tm->tm_hour could be [0, 23] */ - if (rtc_in_range(tm->tm_hour, 0, 23)) - timer_time.hour = tm->tm_hour; - else - return -1; - - /* Range of tm->tm_mday could be [1, 31] */ - if (rtc_in_range(tm->tm_mday, 1, 31)) - timer_date.day = tm->tm_mday; - else - return -1; - - /* - * Range of tm->tm_mon could be [0, 11] - * But in this RTC, date.month should be [1, 12] - */ - if (rtc_in_range(tm->tm_mon, 0, 11)) - timer_date.month = tm->tm_mon + 1; - else - return -1; - - /* - * Range of tm->tm_year is the years since 1900 - * But in this RTC, year is split into year and century - * In this RTC, century range is [0,31], year range is [0,99] - */ - int human_year = tm->tm_year + 1900; - int rtc_year = human_year % 100; - int rtc_century = human_year / 100; - - if (rtc_in_range(rtc_year, 0, 99) && - rtc_in_range(rtc_century, 0, 31)) { - timer_date.year = rtc_year; - timer_extended.century = rtc_century; - } else - return -1; - - /* Range of tm->tm_wday could be [0, 6] */ - if (rtc_in_range(tm->tm_wday, 0, 6)) - timer_date.week = tm->tm_wday; - else - return -1; - - /* Set RTC mode to timer setting mode */ - rtc_timer_set_mode(RTC_TIMER_SETTING); - /* Write value to RTC */ - rtc->date = timer_date; - rtc->time = timer_time; - rtc->extended = timer_extended; - /* Get CPU current freq */ - unsigned long freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); - /* Set threshold to 1/26000000 s */ - freq = freq / 26000000; - /* Get current CPU cycle */ - unsigned long start_cycle = read_csr(mcycle); - /* Wait for 1/26000000 s to sync data */ - while (read_csr(mcycle) - start_cycle < freq) - continue; - /* Set RTC mode to timer running mode */ - rtc_timer_set_mode(RTC_TIMER_RUNNING); - } - - return 0; -} - -int rtc_timer_set_alarm_tm(const struct tm *tm) -{ - struct rtc_alarm_date_t alarm_date; - struct rtc_alarm_time_t alarm_time; - - if (tm) { - /* - * Range of tm->tm_sec could be [0,61] - * - * Range of tm->tm_sec allows for a positive leap second. Two - * leap seconds in the same minute are not allowed (the C90 - * range 0..61 was a defect) - */ - if (rtc_in_range(tm->tm_sec, 0, 59)) - alarm_time.second = tm->tm_sec; - else - return -1; - - /* Range of tm->tm_min could be [0,59] */ - if (rtc_in_range(tm->tm_min, 0, 59)) - alarm_time.minute = tm->tm_min; - else - return -1; - - /* Range of tm->tm_hour could be [0, 23] */ - if (rtc_in_range(tm->tm_hour, 0, 23)) - alarm_time.hour = tm->tm_hour; - else - return -1; - - /* Range of tm->tm_mday could be [1, 31] */ - if (rtc_in_range(tm->tm_mday, 1, 31)) - alarm_date.day = tm->tm_mday; - else - return -1; - - /* - * Range of tm->tm_mon could be [0, 11] - * But in this RTC, date.month should be [1, 12] - */ - if (rtc_in_range(tm->tm_mon, 0, 11)) - alarm_date.month = tm->tm_mon + 1; - else - return -1; - - /* - * Range of tm->tm_year is the years since 1900 - * But in this RTC, year is split into year and century - * In this RTC, century range is [0,31], year range is [0,99] - */ - int human_year = tm->tm_year + 1900; - int rtc_year = human_year % 100; - int rtc_century = human_year / 100; - - if (rtc_in_range(rtc_year, 0, 99) && - rtc_in_range(rtc_century, 0, 31)) { - alarm_date.year = rtc_year; - } else - return -1; - - /* Range of tm->tm_wday could be [0, 6] */ - if (rtc_in_range(tm->tm_wday, 0, 6)) - alarm_date.week = tm->tm_wday; - else - return -1; - - /* Write value to RTC */ - rtc->alarm_date = alarm_date; - rtc->alarm_time = alarm_time; - } - - return 0; -} - -int rtc_year_is_leap(int year) -{ - return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); -} - -int rtc_get_yday(int year, int month, int day) -{ - static const int days[2][13] = { - {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, - {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} - }; - int leap = rtc_year_is_leap(year); - - return days[leap][month] + day; -} - -int rtc_get_wday(int year, int month, int day) -{ - /* Magic method to get weekday */ - int weekday = (day += month < 3 ? year-- : year - 2, 23 * month / 9 + day + 4 + year / 4 - year / 100 + year / 400) % 7; - return weekday; -} - -struct tm *rtc_timer_get_tm(void) -{ - if (rtc_timer_get_mode() != RTC_TIMER_RUNNING) - return NULL; - - struct rtc_date_t timer_date = rtc->date; - struct rtc_time_t timer_time = rtc->time; - struct rtc_extended_t timer_extended = rtc->extended; - - struct tm *tm = &rtc_date_time; - - tm->tm_sec = timer_time.second % 60; - tm->tm_min = timer_time.minute % 60; - tm->tm_hour = timer_time.hour % 24; - tm->tm_mday = timer_date.day % 31; - tm->tm_mon = (timer_date.month % 12) - 1; - tm->tm_year = (timer_date.year % 100) + (timer_extended.century * 100) - 1900; - tm->tm_wday = timer_date.week; - tm->tm_yday = rtc_get_yday(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); - tm->tm_isdst = -1; - - return tm; -} - -struct tm *rtc_timer_get_alarm_tm(void) -{ - if (rtc_timer_get_mode() != RTC_TIMER_RUNNING) - return NULL; - - struct rtc_alarm_date_t alarm_date = rtc->alarm_date; - struct rtc_alarm_time_t alarm_time = rtc->alarm_time; - struct rtc_extended_t timer_extended = rtc->extended; - - struct tm *tm = &rtc_date_time; - - tm->tm_sec = alarm_time.second % 60; - tm->tm_min = alarm_time.minute % 60; - tm->tm_hour = alarm_time.hour % 24; - tm->tm_mday = alarm_date.day % 31; - tm->tm_mon = (alarm_date.month % 12) - 1; - /* Alarm and Timer use same timer_extended.century */ - tm->tm_year = (alarm_date.year % 100) + (timer_extended.century * 100) - 1900; - tm->tm_wday = alarm_date.week; - tm->tm_yday = rtc_get_yday(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); - tm->tm_isdst = -1; - - return tm; -} - -int rtc_timer_set(int year, int month, int day, int hour, int minute, int second) -{ - struct tm date_time = { - .tm_sec = second, - .tm_min = minute, - .tm_hour = hour, - .tm_mday = day, - .tm_mon = month - 1, - .tm_year = year - 1900, - .tm_wday = rtc_get_wday(year, month, day), - .tm_yday = rtc_get_yday(year, month, day), - .tm_isdst = -1, - }; - - return rtc_timer_set_tm(&date_time); -} - -int rtc_timer_get(int *year, int *month, int *day, int *hour, int *minute, int *second) -{ - struct tm *tm = rtc_timer_get_tm(); - - if (tm) { - if (year) - *year = tm->tm_year + 1900; - if (month) - *month = tm->tm_mon + 1; - if (day) - *day = tm->tm_mday; - if (hour) - *hour = tm->tm_hour; - if (minute) - *minute = tm->tm_min; - if (second) - *second = tm->tm_sec; - } else - return -1; - - return 0; -} - -int rtc_timer_set_alarm(int year, int month, int day, int hour, int minute, int second) -{ - struct tm date_time = { - .tm_sec = second, - .tm_min = minute, - .tm_hour = hour, - .tm_mday = day, - .tm_mon = month - 1, - .tm_year = year - 1900, - .tm_wday = rtc_get_wday(year, month, day), - .tm_yday = rtc_get_yday(year, month, day), - .tm_isdst = -1, - }; - - return rtc_timer_set_alarm_tm(&date_time); -} - -int rtc_timer_get_alarm(int *year, int *month, int *day, int *hour, int *minute, int *second) -{ - struct tm *tm = rtc_timer_get_alarm_tm(); - - if (tm) { - if (year) - *year = tm->tm_year + 1900; - if (month) - *month = tm->tm_mon + 1; - if (day) - *day = tm->tm_mday; - if (hour) - *hour = tm->tm_hour; - if (minute) - *minute = tm->tm_min; - if (second) - *second = tm->tm_sec; - } else - return -1; - - return 0; -} - -int rtc_timer_set_clock_frequency(unsigned int frequency) -{ - struct rtc_initial_count_t initial_count; - - initial_count.count = frequency; - rtc->initial_count = initial_count; - return 0; -} - -unsigned int rtc_timer_get_clock_frequency(void) -{ - return rtc->initial_count.count; -} - -int rtc_timer_set_clock_count_value(unsigned int count) -{ - struct rtc_current_count_t current_count; - - current_count.count = count; - rtc->current_count = current_count; - return 0; -} - -unsigned int rtc_timer_get_clock_count_value(void) -{ - return rtc->current_count.count; -} - -int rtc_tick_interrupt_set(int enable) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - interrupt_ctrl.tick_enable = enable; - rtc->interrupt_ctrl = interrupt_ctrl; - return 0; -} - -int rtc_tick_interrupt_get(void) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - return interrupt_ctrl.tick_enable; -} - -int rtc_tick_interrupt_mode_set(rtc_tick_interrupt_mode_e mode) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - interrupt_ctrl.tick_int_mode = mode; - rtc->interrupt_ctrl = interrupt_ctrl; - return 0; -} - -rtc_tick_interrupt_mode_e rtc_tick_interrupt_mode_get(void) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - return interrupt_ctrl.tick_int_mode; -} - -int rtc_alarm_interrupt_set(int enable) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - interrupt_ctrl.alarm_enable = enable; - rtc->interrupt_ctrl = interrupt_ctrl; - return 0; -} - -int rtc_alarm_interrupt_get(void) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - return interrupt_ctrl.alarm_enable; -} - -int rtc_alarm_interrupt_mask_set(struct rtc_mask_t mask) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - interrupt_ctrl.alarm_compare_mask = *(uint8_t *)&mask; - rtc->interrupt_ctrl = interrupt_ctrl; - return 0; -} - -struct rtc_mask_t rtc_alarm_interrupt_mask_get(void) -{ - struct rtc_interrupt_ctrl_t interrupt_ctrl = rtc->interrupt_ctrl; - - uint8_t compare_mask = interrupt_ctrl.alarm_compare_mask; - - return *(struct rtc_mask_t *)&compare_mask; -} - -int rtc_protect_set(int enable) -{ - struct rtc_register_ctrl_t register_ctrl = rtc->register_ctrl; - - struct rtc_mask_t mask = { - .second = 1, - /* Second mask */ - .minute = 1, - /* Minute mask */ - .hour = 1, - /* Hour mask */ - .week = 1, - /* Week mask */ - .day = 1, - /* Day mask */ - .month = 1, - /* Month mask */ - .year = 1, - }; - - struct rtc_mask_t unmask = { - .second = 0, - /* Second mask */ - .minute = 0, - /* Minute mask */ - .hour = 0, - /* Hour mask */ - .week = 0, - /* Week mask */ - .day = 0, - /* Day mask */ - .month = 0, - /* Month mask */ - .year = 0, - }; - - if (enable) { - /* Turn RTC in protect mode, no one can write time */ - register_ctrl.timer_mask = *(uint8_t *)&unmask; - register_ctrl.alarm_mask = *(uint8_t *)&unmask; - register_ctrl.initial_count_mask = 0; - register_ctrl.interrupt_register_mask = 0; - } else { - /* Turn RTC in unprotect mode, everyone can write time */ - register_ctrl.timer_mask = *(uint8_t *)&mask; - register_ctrl.alarm_mask = *(uint8_t *)&mask; - register_ctrl.initial_count_mask = 1; - register_ctrl.interrupt_register_mask = 1; - } - - rtc->register_ctrl = register_ctrl; - return 0; -} - -int rtc_init(void) -{ - /* Reset RTC */ - sysctl_reset(SYSCTL_RESET_RTC); - /* Enable RTC */ - sysctl_clock_enable(SYSCTL_CLOCK_RTC); - rtc_timer_set_mode(RTC_TIMER_SETTING); - /* Unprotect RTC */ - rtc_protect_set(0); - /* Set RTC clock frequency */ - rtc_timer_set_clock_frequency( - sysctl_clock_source_get_freq(SYSCTL_SOURCE_IN0) - ); - rtc_timer_set_clock_count_value(1); - - /* Set RTC mode to timer running mode */ - rtc_timer_set_mode(RTC_TIMER_RUNNING); - return 0; -} diff --git a/lib/drivers/uarths.c b/lib/drivers/uarths.c index 9f1cb81..92dfe12 100644 --- a/lib/drivers/uarths.c +++ b/lib/drivers/uarths.c @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include #include #include "uarths.h" @@ -52,7 +51,7 @@ static inline int uart_putc(char c) return 0; } -int uart_getc(void) +int uarths_getc(void) { /* while not empty */ struct uarths_rxdata_t recv = uarths->rxdata; @@ -63,12 +62,12 @@ int uart_getc(void) return recv.data; } -int uart_putchar(char c) +int uarths_putchar(char c) { return uart_putc(c); } -int uart_puts(const char *s) +int uarths_puts(const char *s) { while (*s) if (uart_putc(*s++) != 0) @@ -76,7 +75,7 @@ int uart_puts(const char *s) return 0; } -int uart_init() +int uarths_init() { uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); uint16_t div = freq / 115200 - 1; diff --git a/lib/drivers/wdt.c b/lib/drivers/wdt.c deleted file mode 100644 index fc750f7..0000000 --- a/lib/drivers/wdt.c +++ /dev/null @@ -1,120 +0,0 @@ -/* Copyright 2018 Canaan Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "wdt.h" -#include "platform.h" -#include "stddef.h" -#include "common.h" -#include "sysctl.h" -#include "plic.h" - -plic_irq_callback_t wdt_irq[2]; - -volatile struct wdt_t *const wdt[2] = -{ - (volatile struct wdt_t *)WDT0_BASE_ADDR, - (volatile struct wdt_t *)WDT1_BASE_ADDR -}; - -void wdt_feed(uint8_t id) -{ - wdt[id]->crr = WDT_CRR_MASK; -} - -void wdt_enable(uint8_t id) -{ - wdt[id]->crr = WDT_CRR_MASK; - wdt[id]->cr |= WDT_CR_ENABLE; -} - -void wdt_disable(uint8_t id) -{ - wdt[id]->crr = WDT_CRR_MASK; - wdt[id]->cr &= (~WDT_CR_ENABLE); -} - -void wdt_timeout_set(uint8_t id, uint8_t timeout) -{ - wdt[id]->torr = WDT_TORR_TOP(timeout); -} - -void wdt_response_mode(uint8_t id, uint8_t mode) -{ - wdt[id]->cr &= (~WDT_CR_RMOD_MASK); - wdt[id]->cr |= mode; -} - -void wdt_interrupt_clear(uint8_t id) -{ - wdt[id]->eoi = wdt[id]->eoi; -} - -void wdt_set_irq(uint8_t id, plic_irq_callback_t on_irq) -{ - wdt_irq[id] = on_irq; -} - -size_t wdt_get_pclk(uint8_t id) -{ - return id ? sysctl_clock_get_freq(SYSCTL_CLOCK_WDT1) : sysctl_clock_get_freq(SYSCTL_CLOCK_WDT0); -} - -ssize_t log_2(size_t x) -{ - ssize_t i = 0; - for (i = sizeof(size_t) * 8; i >= 0; i--) - { - if ((x >> i) & 0x1) - { - break; - } - } - return i; -} - -uint8_t wdt_get_top(uint8_t id, size_t timeout_ms) -{ - size_t wdt_clk = wdt_get_pclk(id); - size_t ret = (timeout_ms * wdt_clk / 1000) >> 16; - if (ret) - ret = log_2(ret); - if (ret > 0xf) - ret = 0xf; - return (uint8_t)ret; -} - - -int wdt_start(uint8_t id, size_t toms) -{ - wdt_disable(id); - wdt_interrupt_clear(id); - plic_irq_register(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, wdt_irq[id], NULL); - plic_set_priority(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, 1); - plic_irq_enable(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT); - - sysctl_reset(id ? SYSCTL_RESET_WDT1 : SYSCTL_RESET_WDT0); - sysctl_clock_set_threshold(id ? SYSCTL_THRESHOLD_WDT1 : SYSCTL_THRESHOLD_WDT0, 0); - sysctl_clock_enable(id ? SYSCTL_CLOCK_WDT1 : SYSCTL_CLOCK_WDT0); - wdt_response_mode(id, WDT_CR_RMOD_INTERRUPT); - uint8_t m_top = wdt_get_top(id, toms); - wdt_timeout_set(id, m_top); - wdt_enable(id); - return 0; -} - -void wdt_stop(uint8_t id) -{ - wdt_disable(id); -} - diff --git a/lib/freertos/device/device_priv.h b/lib/freertos/device/device_priv.h new file mode 100644 index 0000000..2b9a650 --- /dev/null +++ b/lib/freertos/device/device_priv.h @@ -0,0 +1,41 @@ +/* Copyright 2018 Canaan Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FREERTOS_DEVICE_PRIV_H +#define _FREERTOS_DEVICE_PRIV_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * @brief Install HAL + */ +void install_hal(); + +/** + * @brief Install all drivers + */ +void install_drivers(); + +#ifdef __cplusplus +} +#endif + +#endif /* _FREERTOS_DEVICE_PRIV_H */ \ No newline at end of file diff --git a/lib/freertos/device/devices.c b/lib/freertos/device/devices.c index 9224502..5dbe3b0 100644 --- a/lib/freertos/device/devices.c +++ b/lib/freertos/device/devices.c @@ -14,15 +14,17 @@ */ #include #include +#include +#include +#include +#include +#include #include #include #include #include #include #include -#include "devices.h" -#include "driver.h" -#include "hal.h" #define MAX_HANDLES 256 #define HANDLE_OFFSET 256 @@ -31,10 +33,10 @@ #define DEFINE_INSTALL_DRIVER(type) \ static void install_##type##_drivers() \ { \ - driver_registry_t* head = g_##type##_drivers; \ + driver_registry_t *head = g_##type##_drivers; \ while (head->name) \ { \ - const driver_base_t* driver = head->driver; \ + const driver_base_t *driver = head->driver; \ driver->install(driver->userdata); \ head++; \ } \ @@ -42,10 +44,10 @@ typedef struct { - driver_registry_t* driver_reg; + driver_registry_t *driver_reg; } _file; -static _file* handles_[MAX_HANDLES] = {0}; +static _file *handles_[MAX_HANDLES] = {0}; static driver_registry_t g_custom_drivers[MAX_CUSTOM_DRIVERS] = {0}; uintptr_t fft_file_; @@ -56,14 +58,14 @@ DEFINE_INSTALL_DRIVER(hal); DEFINE_INSTALL_DRIVER(dma); DEFINE_INSTALL_DRIVER(system); -driver_registry_t* find_free_driver(driver_registry_t* registry, const char* name) +driver_registry_t * find_free_driver(driver_registry_t *registry, const char *name) { - driver_registry_t* head = registry; + driver_registry_t *head = registry; while (head->name) { if (strcmp(name, head->name) == 0) { - driver_base_t* driver = (driver_base_t*)head->driver; + driver_base_t *driver = (driver_base_t*)head->driver; if (driver->open(driver->userdata)) return head; else @@ -76,10 +78,10 @@ driver_registry_t* find_free_driver(driver_registry_t* registry, const char* nam return NULL; } -static driver_registry_t* install_custom_driver_core(const char* name, driver_type type, const void* driver) +static driver_registry_t * install_custom_driver_core(const char *name, driver_type type, const void *driver) { size_t i = 0; - driver_registry_t* head = g_custom_drivers; + driver_registry_t *head = g_custom_drivers; for (i = 0; i < MAX_CUSTOM_DRIVERS; i++, head++) { if (!head->name) @@ -104,11 +106,11 @@ void install_drivers() sha256_file_ = io_open("/dev/sha256"); } -static _file* io_alloc_file(driver_registry_t* driver_reg) +static _file * io_alloc_file(driver_registry_t *driver_reg) { if (driver_reg) { - _file* file = (_file*)malloc(sizeof(_file)); + _file *file = (_file *)malloc(sizeof(_file)); if (!file) return NULL; file->driver_reg = driver_reg; @@ -118,10 +120,10 @@ static _file* io_alloc_file(driver_registry_t* driver_reg) return NULL; } -static _file* io_open_reg(driver_registry_t* registry, const char* name, _file** file) +static _file *io_open_reg(driver_registry_t *registry, const char *name, _file **file) { - driver_registry_t* driver_reg = find_free_driver(registry, name); - _file* ret = io_alloc_file(driver_reg); + driver_registry_t *driver_reg = find_free_driver(registry, name); + _file *ret = io_alloc_file(driver_reg); *file = ret; return ret; } @@ -131,29 +133,29 @@ static _file* io_open_reg(driver_registry_t* registry, const char* name, _file** #define DEFINE_READ_PROXY(tl, tu) \ if (rfile->driver_reg->type == DRIVER_##tu) \ { \ - const tl##_driver_t* tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ + const tl##_driver_t *tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ return tl->read(buffer, len, tl->base.userdata); \ } #define DEFINE_WRITE_PROXY(tl, tu) \ if (rfile->driver_reg->type == DRIVER_##tu) \ { \ - const tl##_driver_t* tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ + const tl##_driver_t *tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ return tl->write(buffer, len, tl->base.userdata); \ } #define DEFINE_CONTROL_PROXY(tl, tu) \ if (rfile->driver_reg->type == DRIVER_##tu) \ { \ - const tl##_driver_t* tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ + const tl##_driver_t *tl = (const tl##_driver_t*)rfile->driver_reg->driver; \ return tl->io_control(control_code, write_buffer, write_len, read_buffer, read_len, tl->base.userdata); \ } static void dma_add_free(); -int io_read(uintptr_t file, char* buffer, size_t len) +int io_read(handle_t file, uint8_t *buffer, size_t len) { - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; /* clang-format off */ DEFINE_READ_PROXY(uart, UART) else DEFINE_READ_PROXY(i2c_device, I2C_DEVICE) @@ -165,21 +167,21 @@ int io_read(uintptr_t file, char* buffer, size_t len) /* clang-format on */ } -static void io_free(_file* file) +static void io_free(_file *file) { if (file) { if (file->driver_reg->type == DRIVER_DMA) dma_add_free(); - driver_base_t* driver = (driver_base_t*)file->driver_reg->driver; + driver_base_t *driver = (driver_base_t *)file->driver_reg->driver; driver->close(driver->userdata); free(file); } } -static uintptr_t io_alloc_handle(_file* file) +static handle_t io_alloc_handle(_file *file) { if (file) { @@ -200,9 +202,9 @@ static uintptr_t io_alloc_handle(_file* file) return 0; } -uintptr_t io_open(const char* name) +handle_t io_open(const char *name) { - _file* file = 0; + _file *file = 0; if (io_open_reg(g_system_drivers, name, &file)) { } @@ -216,11 +218,11 @@ uintptr_t io_open(const char* name) return 0; } -int io_close(uintptr_t file) +int io_close(handle_t file) { if (file) { - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; io_free(rfile); atomic_set(handles_ + file - HANDLE_OFFSET, 0); } @@ -228,9 +230,9 @@ int io_close(uintptr_t file) return 0; } -int io_write(uintptr_t file, const char* buffer, size_t len) +int io_write(handle_t file, const uint8_t *buffer, size_t len) { - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; /* clang-format off */ DEFINE_WRITE_PROXY(uart, UART) else DEFINE_WRITE_PROXY(i2c_device, I2C_DEVICE) @@ -242,9 +244,9 @@ int io_write(uintptr_t file, const char* buffer, size_t len) /* clang-format on */ } -int io_control(uintptr_t file, size_t control_code, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len) +int io_control(handle_t file, uint32_t control_code, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len) { - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; DEFINE_CONTROL_PROXY(custom, CUSTOM) return -1; @@ -252,79 +254,79 @@ int io_control(uintptr_t file, size_t control_code, const char* write_buffer, si /* Device IO Implementation Helper Macros */ -#define COMMON_ENTRY(tl, tu) \ - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; \ - configASSERT(rfile->driver_reg->type == DRIVER_##tu); \ - const tl##_driver_t* tl = (const tl##_driver_t*)rfile->driver_reg->driver; +#define COMMON_ENTRY(tl, tu) \ + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; \ + configASSERT(rfile->driver_reg->type == DRIVER_##tu); \ + const tl##_driver_t *tl = (const tl##_driver_t *)rfile->driver_reg->driver; -#define COMMON_ENTRY_FILE(file, tl, tu) \ - _file* rfile = (_file*)handles_[file - HANDLE_OFFSET]; \ - configASSERT(rfile->driver_reg->type == DRIVER_##tu); \ - const tl##_driver_t* tl = (const tl##_driver_t*)rfile->driver_reg->driver; +#define COMMON_ENTRY_FILE(file, tl, tu) \ + _file *rfile = (_file *)handles_[file - HANDLE_OFFSET]; \ + configASSERT(rfile->driver_reg->type == DRIVER_##tu); \ + const tl##_driver_t *tl = (const tl##_driver_t *)rfile->driver_reg->driver; /* UART */ -void uart_config(uintptr_t file, size_t baud_rate, size_t data_width, uart_stopbit stopbit, uart_parity parity) +void uart_config(handle_t file, uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity) { COMMON_ENTRY(uart, UART); - uart->config(baud_rate, data_width, stopbit, parity, uart->base.userdata); + uart->config(baud_rate, databits, stopbits, parity, uart->base.userdata); } /* GPIO */ -size_t gpio_get_pin_count(uintptr_t file) +uint32_t gpio_get_pin_count(handle_t file) { COMMON_ENTRY(gpio, GPIO); return gpio->pin_count; } -void gpio_set_drive_mode(uintptr_t file, size_t pin, gpio_drive_mode mode) +void gpio_set_drive_mode(handle_t file, uint32_t pin, gpio_drive_mode_t mode) { COMMON_ENTRY(gpio, GPIO); - gpio->set_drive_mode(gpio->base.userdata, pin, mode); + gpio->set_drive_mode(pin, mode, gpio->base.userdata); } -void gpio_set_pin_edge(uintptr_t file, size_t pin, gpio_pin_edge edge) +void gpio_set_pin_edge(handle_t file, uint32_t pin, gpio_pin_edge_t edge) { COMMON_ENTRY(gpio, GPIO); - gpio->set_pin_edge(gpio->base.userdata, pin, edge); + gpio->set_pin_edge(pin, edge, gpio->base.userdata); } -void gpio_set_onchanged(uintptr_t file, size_t pin, gpio_onchanged callback, void* userdata) +void gpio_set_on_changed(handle_t file, uint32_t pin, gpio_on_changed_t callback, void *userdata) { COMMON_ENTRY(gpio, GPIO); - gpio->set_onchanged(gpio->base.userdata, pin, callback, userdata); + gpio->set_on_changed(pin, callback, userdata, gpio->base.userdata); } -gpio_pin_value gpio_get_pin_value(uintptr_t file, size_t pin) +gpio_pin_value_t gpio_get_pin_value(handle_t file, uint32_t pin) { COMMON_ENTRY(gpio, GPIO); - return gpio->get_pin_value(gpio->base.userdata, pin); + return gpio->get_pin_value(pin, gpio->base.userdata); } -void gpio_set_pin_value(uintptr_t file, size_t pin, gpio_pin_value value) +void gpio_set_pin_value(handle_t file, uint32_t pin, gpio_pin_value_t value) { COMMON_ENTRY(gpio, GPIO); - gpio->set_pin_value(gpio->base.userdata, pin, value); + gpio->set_pin_value(pin, value, gpio->base.userdata); } /* I2C */ -uintptr_t i2c_get_device(uintptr_t file, const char* name, size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode) +handle_t i2c_get_device(handle_t file, const char *name, uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode) { COMMON_ENTRY(i2c, I2C); - i2c_device_driver_t* driver = i2c->get_device(slave_address, address_width, bus_speed_mode, i2c->base.userdata); - driver_registry_t* reg = install_custom_driver_core(name, DRIVER_I2C_DEVICE, driver); + i2c_device_driver_t *driver = i2c->get_device(slave_address, address_width, bus_speed_mode, i2c->base.userdata); + driver_registry_t *reg = install_custom_driver_core(name, DRIVER_I2C_DEVICE, driver); return io_alloc_handle(io_alloc_file(reg)); } -int i2c_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len) +int i2c_dev_transfer_sequential(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len) { COMMON_ENTRY(i2c_device, I2C_DEVICE); return i2c_device->transfer_sequential(write_buffer, write_len, read_buffer, read_len, i2c_device->base.userdata); } -void i2c_config_as_slave(uintptr_t file, size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, i2c_slave_handler* handler) +void i2c_config_as_slave(handle_t file, uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, i2c_slave_handler_t *handler) { COMMON_ENTRY(i2c, I2C); i2c->config_as_slave(slave_address, address_width, bus_speed_mode, handler, i2c->base.userdata); @@ -332,37 +334,37 @@ void i2c_config_as_slave(uintptr_t file, size_t slave_address, size_t address_wi /* I2S */ -void i2s_config_as_render(uintptr_t file, const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask) +void i2s_config_as_render(handle_t file, const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask) { COMMON_ENTRY(i2s, I2S); i2s->config_as_render(format, delay_ms, align_mode, channels_mask, i2s->base.userdata); } -void i2s_config_as_capture(uintptr_t file, const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask) +void i2s_config_as_capture(handle_t file, const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask) { COMMON_ENTRY(i2s, I2S); i2s->config_as_capture(format, delay_ms, align_mode, channels_mask, i2s->base.userdata); } -void i2s_get_buffer(uintptr_t file, char** buffer, size_t* frames) +void i2s_get_buffer(handle_t file, uint8_t* *buffer, size_t *frames) { COMMON_ENTRY(i2s, I2S); i2s->get_buffer(buffer, frames, i2s->base.userdata); } -void i2s_release_buffer(uintptr_t file, size_t frames) +void i2s_release_buffer(handle_t file, size_t frames) { COMMON_ENTRY(i2s, I2S); i2s->release_buffer(frames, i2s->base.userdata); } -void i2s_start(uintptr_t file) +void i2s_start(handle_t file) { COMMON_ENTRY(i2s, I2S); i2s->start(i2s->base.userdata); } -void i2s_stop(uintptr_t file) +void i2s_stop(handle_t file) { COMMON_ENTRY(i2s, I2S); i2s->stop(i2s->base.userdata); @@ -370,39 +372,39 @@ void i2s_stop(uintptr_t file) /* SPI */ -uintptr_t spi_get_device(uintptr_t file, const char* name, spi_mode mode, spi_frame_format frame_format, size_t chip_select_line, size_t data_bit_length) +handle_t spi_get_device(handle_t file, const char *name, spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length) { COMMON_ENTRY(spi, SPI); - spi_device_driver_t* driver = spi->get_device(mode, frame_format, chip_select_line, data_bit_length, spi->base.userdata); - driver_registry_t* reg = install_custom_driver_core(name, DRIVER_SPI_DEVICE, driver); + spi_device_driver_t *driver = spi->get_device(mode, frame_format, chip_select_mask, data_bit_length, spi->base.userdata); + driver_registry_t *reg = install_custom_driver_core(name, DRIVER_SPI_DEVICE, driver); return io_alloc_handle(io_alloc_file(reg)); } -void spi_dev_config(uintptr_t file, size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode) +void spi_dev_config_non_standard(handle_t file, uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode) { COMMON_ENTRY(spi_device, SPI_DEVICE); spi_device->config(instruction_length, address_length, wait_cycles, trans_mode, spi_device->base.userdata); } -double spi_dev_set_speed(uintptr_t file, double speed) +double spi_dev_set_clock_rate(handle_t file, double clock_rate) { COMMON_ENTRY(spi_device, SPI_DEVICE); - return spi_device->set_speed(speed, spi_device->base.userdata); + return spi_device->set_clock_rate(clock_rate, spi_device->base.userdata); } -int spi_dev_transfer_full_duplex(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len) +int spi_dev_transfer_full_duplex(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len) { COMMON_ENTRY(spi_device, SPI_DEVICE); return spi_device->transfer_full_duplex(write_buffer, write_len, read_buffer, read_len, spi_device->base.userdata); } -int spi_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len) +int spi_dev_transfer_sequential(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len) { COMMON_ENTRY(spi_device, SPI_DEVICE); return spi_device->transfer_sequential(write_buffer, write_len, read_buffer, read_len, spi_device->base.userdata); } -void spi_dev_fill(uintptr_t file, size_t instruction, size_t address, uint32_t value, size_t count) +void spi_dev_fill(handle_t file, uint32_t instruction, uint32_t address, uint32_t value, size_t count) { COMMON_ENTRY(spi_device, SPI_DEVICE); return spi_device->fill(instruction, address, value, count, spi_device->base.userdata); @@ -410,71 +412,71 @@ void spi_dev_fill(uintptr_t file, size_t instruction, size_t address, uint32_t v /* DVP */ -void dvp_config(uintptr_t file, size_t width, size_t height, int auto_enable) +void dvp_config(handle_t file, uint32_t width, uint32_t height, int auto_enable) { COMMON_ENTRY(dvp, DVP); dvp->config(width, height, auto_enable, dvp->base.userdata); } -void dvp_enable_frame(uintptr_t file) +void dvp_enable_frame(handle_t file) { COMMON_ENTRY(dvp, DVP); dvp->enable_frame(dvp->base.userdata); } -size_t dvp_get_output_num(uintptr_t file) +uint32_t dvp_get_output_num(handle_t file) { COMMON_ENTRY(dvp, DVP); return dvp->output_num; } -void dvp_set_signal(uintptr_t file, dvp_signal_type type, int value) +void dvp_set_signal(handle_t file, dvp_signal_type_t type, int value) { COMMON_ENTRY(dvp, DVP); dvp->set_signal(type, value, dvp->base.userdata); } -void dvp_set_output_enable(uintptr_t file, size_t index, int enable) +void dvp_set_output_enable(handle_t file, uint32_t index, int enable) { COMMON_ENTRY(dvp, DVP); dvp->set_output_enable(index, enable, dvp->base.userdata); } -void dvp_set_output_attributes(uintptr_t file, size_t index, video_format format, void* output_buffer) +void dvp_set_output_attributes(handle_t file, uint32_t index, video_format_t format, void *output_buffer) { COMMON_ENTRY(dvp, DVP); dvp->set_output_attributes(index, format, output_buffer, dvp->base.userdata); } -void dvp_set_frame_event_enable(uintptr_t file, video_frame_event event, int enable) +void dvp_set_frame_event_enable(handle_t file, dvp_frame_event_t event, int enable) { COMMON_ENTRY(dvp, DVP); dvp->set_frame_event_enable(event, enable, dvp->base.userdata); } -void dvp_set_on_frame_event(uintptr_t file, dvp_on_frame_event callback, void* callback_data) +void dvp_set_on_frame_event(handle_t file, dvp_on_frame_event_t handler, void *userdata) { COMMON_ENTRY(dvp, DVP); - dvp->set_on_frame_event(callback, callback_data, dvp->base.userdata); + dvp->set_on_frame_event(handler, userdata, dvp->base.userdata); } /* SSCB */ -uintptr_t sccb_get_device(uintptr_t file, const char* name, size_t slave_address, size_t address_width) +handle_t sccb_get_device(handle_t file, const char *name, uint32_t slave_address, uint32_t address_width) { COMMON_ENTRY(sccb, SCCB); - sccb_device_driver_t* driver = sccb->get_device(slave_address, address_width, sccb->base.userdata); - driver_registry_t* reg = install_custom_driver_core(name, DRIVER_SCCB_DEVICE, driver); + sccb_device_driver_t *driver = sccb->get_device(slave_address, address_width, sccb->base.userdata); + driver_registry_t *reg = install_custom_driver_core(name, DRIVER_SCCB_DEVICE, driver); return io_alloc_handle(io_alloc_file(reg)); } -uint8_t sccb_dev_read_byte(uintptr_t file, uint16_t reg_address) +uint8_t sccb_dev_read_byte(handle_t file, uint16_t reg_address) { COMMON_ENTRY(sccb_device, SCCB_DEVICE); return sccb_device->read_byte(reg_address, sccb_device->base.userdata); } -void sccb_dev_write_byte(uintptr_t file, uint16_t reg_address, uint8_t value) +void sccb_dev_write_byte(handle_t file, uint16_t reg_address, uint8_t value) { COMMON_ENTRY(sccb_device, SCCB_DEVICE); sccb_device->write_byte(reg_address, value, sccb_device->base.userdata); @@ -482,48 +484,48 @@ void sccb_dev_write_byte(uintptr_t file, uint16_t reg_address, uint8_t value) /* FFT */ -void fft_complex_uint16(fft_point point, fft_direction direction, uint32_t shifts_mask, const uint16_t* input, uint16_t* output) +void fft_complex_uint16(fft_direction_t direction, const uint64_t *input, size_t point_num, uint64_t *output) { COMMON_ENTRY_FILE(fft_file_, fft, FFT); - fft->complex_uint16(point, direction, shifts_mask, input, output, fft->base.userdata); + fft->complex_uint16(direction, input, point_num, output, fft->base.userdata); } /* AES */ -void aes_decrypt(aes_parameter* aes_param) +void aes_hard_decrypt(const aes_param_t *param) { COMMON_ENTRY_FILE(aes_file_, aes, AES); - aes->decrypt(aes_param, aes->base.userdata); + aes->hard_decrypt(param, aes->base.userdata); } -void aes_encrypt(aes_parameter* aes_param) +void aes_hard_encrypt(const aes_param_t *param) { COMMON_ENTRY_FILE(aes_file_, aes, AES); - aes->encrypt(aes_param, aes->base.userdata); + aes->hard_encrypt(param, aes->base.userdata); } /* SHA */ -void sha256_str(const char* str, size_t length, uint8_t* hash) +void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output) { COMMON_ENTRY_FILE(sha256_file_, sha256, SHA256); - sha256->sha_str(str, length, hash, sha256->base.userdata); + sha256->sha256_hard_calculate(input, input_len, output, sha256->base.userdata); } /* TIMER */ -size_t timer_set_interval(uintptr_t file, size_t nanoseconds) +size_t timer_set_interval(handle_t file, size_t nanoseconds) { COMMON_ENTRY(timer, TIMER); return timer->set_interval(nanoseconds, timer->base.userdata); } -void timer_set_ontick(uintptr_t file, timer_ontick ontick, void* ontick_data) +void timer_set_on_tick(handle_t file, timer_on_tick_t on_tick, void *ontick_data) { COMMON_ENTRY(timer, TIMER); - timer->set_ontick(ontick, ontick_data, timer->base.userdata); + timer->set_on_tick(on_tick, ontick_data, timer->base.userdata); } -void timer_set_enable(uintptr_t file, int enable) +void timer_set_enable(handle_t file, int enable) { COMMON_ENTRY(timer, TIMER); timer->set_enable(enable, timer->base.userdata); @@ -531,39 +533,70 @@ void timer_set_enable(uintptr_t file, int enable) /* PWM */ -size_t pwm_get_pin_count(uintptr_t file) +uint32_t pwm_get_pin_count(handle_t file) { COMMON_ENTRY(pwm, PWM); return pwm->pin_count; } -double pwm_set_frequency(uintptr_t file, double frequency) +double pwm_set_frequency(handle_t file, double frequency) { COMMON_ENTRY(pwm, PWM); return pwm->set_frequency(frequency, pwm->base.userdata); } -double pwm_set_active_duty_cycle_percentage(uintptr_t file, size_t pin, double duty_cycle_percentage) +double pwm_set_active_duty_cycle_percentage(handle_t file, uint32_t pin, double duty_cycle_percentage) { COMMON_ENTRY(pwm, PWM); return pwm->set_active_duty_cycle_percentage(pin, duty_cycle_percentage, pwm->base.userdata); } -void pwm_set_enable(uintptr_t file, size_t pin, int enable) +void pwm_set_enable(handle_t file, uint32_t pin, int enable) { COMMON_ENTRY(pwm, PWM); pwm->set_enable(pin, enable, pwm->base.userdata); } +/* WDT */ +void wdt_set_response_mode(handle_t file, wdt_response_mode_t mode) +{ + COMMON_ENTRY(wdt, WDT); + wdt->set_response_mode(mode, wdt->base.userdata); +} + +size_t wdt_set_timeout(handle_t file, size_t nanoseconds) +{ + COMMON_ENTRY(wdt, WDT); + return wdt->set_timeout(nanoseconds, wdt->base.userdata); +} + +void wdt_set_on_timeout(handle_t file, wdt_on_timeout_t handler, void *userdata) +{ + COMMON_ENTRY(wdt, WDT); + wdt->set_on_timeout(handler, userdata, wdt->base.userdata); +} + +void wdt_restart_counter(handle_t file) +{ + COMMON_ENTRY(wdt, WDT); + wdt->restart_counter(wdt->base.userdata); +} + +void wdt_set_enable(handle_t file, int enable) +{ + COMMON_ENTRY(wdt, WDT); + wdt->set_enable(enable, wdt->base.userdata); +} + /* RTC */ -void rtc_get_datetime(uintptr_t file, datetime_t* datetime) +void rtc_get_datetime(handle_t file, struct tm *datetime) { COMMON_ENTRY(rtc, RTC); rtc->get_datetime(datetime, rtc->base.userdata); } -void rtc_set_datetime(uintptr_t file, const datetime_t* datetime) +void rtc_set_datetime(handle_t file, const struct tm *datetime) { COMMON_ENTRY(rtc, RTC); rtc->set_datetime(datetime, rtc->base.userdata); @@ -575,8 +608,8 @@ static uintptr_t pic_file_; typedef struct { - pic_irq_handler pic_callbacks[MAX_IRQN]; - void* callback_userdata[MAX_IRQN]; + pic_irq_handler_t pic_callbacks[IRQN_MAX]; + void *callback_userdata[IRQN_MAX]; } pic_context_t; static pic_context_t pic_context_; @@ -585,7 +618,7 @@ static SemaphoreHandle_t dma_free_; static void init_dma_system() { size_t count = 0; - driver_registry_t* head = g_dma_drivers; + driver_registry_t *head = g_dma_drivers; while (head->name) { count++; @@ -607,41 +640,41 @@ void install_hal() /* PIC */ -void pic_set_irq_enable(size_t irq, int enable) +void pic_set_irq_enable(uint32_t irq, int enable) { COMMON_ENTRY_FILE(pic_file_, pic, PIC); pic->set_irq_enable(irq, enable, pic->base.userdata); } -void pic_set_irq_priority(size_t irq, size_t priority) +void pic_set_irq_priority(uint32_t irq, uint32_t priority) { COMMON_ENTRY_FILE(pic_file_, pic, PIC); pic->set_irq_priority(irq, priority, pic->base.userdata); } -void pic_set_irq_handler(size_t irq, pic_irq_handler handler, void* userdata) +void pic_set_irq_handler(uint32_t irq, pic_irq_handler_t handler, void *userdata) { atomic_set(pic_context_.callback_userdata + irq, userdata); pic_context_.pic_callbacks[irq] = handler; } -void kernel_iface_pic_on_irq(size_t irq) +void kernel_iface_pic_on_irq(uint32_t irq) { - pic_irq_handler handler = pic_context_.pic_callbacks[irq]; + pic_irq_handler_t handler = pic_context_.pic_callbacks[irq]; if (handler) handler(pic_context_.callback_userdata[irq]); } /* DMA */ -uintptr_t dma_open_free() +handle_t dma_open_free() { configASSERT(xSemaphoreTake(dma_free_, portMAX_DELAY) == pdTRUE); driver_registry_t *head = g_dma_drivers, *driver_reg = NULL; while (head->name) { - driver_base_t* driver = (driver_base_t*)head->driver; + driver_base_t *driver = (driver_base_t*)head->driver; if (driver->open(driver->userdata)) { driver_reg = head; @@ -656,7 +689,7 @@ uintptr_t dma_open_free() return handle; } -void dma_close(uintptr_t file) +void dma_close(handle_t file) { io_close(file); } @@ -666,19 +699,19 @@ static void dma_add_free() xSemaphoreGive(dma_free_); } -void dma_set_select_request(uintptr_t file, uint32_t request) +void dma_set_request_source(handle_t file, uint32_t request) { COMMON_ENTRY(dma, DMA); dma->set_select_request(request, dma->base.userdata); } -void dma_transmit_async(uintptr_t file, const volatile void* src, volatile void* dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event) +void dma_transmit_async(handle_t file, const volatile void *src, volatile void *dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event) { COMMON_ENTRY(dma, DMA); dma->transmit_async(src, dest, src_inc, dest_inc, element_size, count, burst_size, completion_event, dma->base.userdata); } -void dma_transmit(uintptr_t file, const volatile void* src, volatile void* dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size) +void dma_transmit(handle_t file, const volatile void *src, volatile void *dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size) { SemaphoreHandle_t event = xSemaphoreCreateBinary(); dma_transmit_async(file, src, dest, src_inc, dest_inc, element_size, count, burst_size, event); @@ -687,7 +720,7 @@ void dma_transmit(uintptr_t file, const volatile void* src, volatile void* dest, vSemaphoreDelete(event); } -void dma_loop_async(uintptr_t file, const volatile void** srcs, size_t src_num, volatile void** dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler stage_completion_handler, void* stage_completion_handler_data, SemaphoreHandle_t completion_event, int* stop_signal) +void dma_loop_async(handle_t file, const volatile void **srcs, size_t src_num, volatile void **dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void *stage_completion_handler_data, SemaphoreHandle_t completion_event, int *stop_signal) { COMMON_ENTRY(dma, DMA); dma->loop_async(srcs, src_num, dests, dest_num, src_inc, dest_inc, element_size, count, burst_size, stage_completion_handler, stage_completion_handler_data, completion_event, stop_signal, dma->base.userdata); @@ -695,7 +728,7 @@ void dma_loop_async(uintptr_t file, const volatile void** srcs, size_t src_num, /* Custom Driver */ -void install_custom_driver(const char* name, const custom_driver_t* driver) +void system_install_custom_driver(const char *name, const custom_driver_t *driver) { install_custom_driver_core(name, DRIVER_CUSTOM, driver); } @@ -708,12 +741,12 @@ uint32_t system_set_cpu_frequency(uint32_t frequency) sysctl_pll_disable(SYSCTL_PLL0); sysctl_pll_enable(SYSCTL_PLL0); - uint32_t result = sysctl_pll_set_freq(SYSCTL_PLL0, SYSCTL_SOURCE_IN0, frequency * 2); + uint32_t result = sysctl_pll_set_freq(SYSCTL_PLL0, SYSCTL_SOURCE_IN0, frequency *2); while (sysctl_pll_is_lock(SYSCTL_PLL0) == 0) sysctl_pll_clear_slip(SYSCTL_PLL0); sysctl_clock_enable(SYSCTL_PLL0); sysctl_clock_set_clock_select(SYSCTL_CLOCK_SELECT_ACLK, SYSCTL_SOURCE_PLL0); - uart_init(); + uarths_init(); return result; } diff --git a/lib/freertos/device/devices.h b/lib/freertos/device/devices.h index 605f071..773f469 100644 --- a/lib/freertos/device/devices.h +++ b/lib/freertos/device/devices.h @@ -24,11 +24,6 @@ extern "C" { #endif -/** - * @brief Install all drivers - */ -void install_drivers(); - /** * @brief Open a device * @@ -38,7 +33,7 @@ void install_drivers(); - 0 Fail - other The device handle */ -uintptr_t io_open(const char* name); +handle_t io_open(const char *name); /** * @brief Close a device @@ -49,18 +44,18 @@ uintptr_t io_open(const char* name); * - 0 Success * - other Fail */ -int io_close(uintptr_t file); +int io_close(handle_t file); /** * @brief Read from a device * * @param[in[ file The device handle * @param[out] buffer The destination buffer - * @param[in] len Maximum bytes to read + * @param[in] len Maximum bytes to read * * @return Actual bytes read */ -int io_read(uintptr_t file, char* buffer, size_t len); +int io_read(handle_t file, uint8_t *buffer, size_t len); /** * @brief Write to a device @@ -73,7 +68,7 @@ int io_read(uintptr_t file, char* buffer, size_t len); * - len Success * - other Fail */ -int io_write(uintptr_t file, const char* buffer, size_t len); +int io_write(handle_t file, const uint8_t *buffer, size_t len); /** * @brief Send control info to a device @@ -87,18 +82,18 @@ int io_write(uintptr_t file, const char* buffer, size_t len); * * @return Actual bytes read */ -int io_control(uintptr_t file, size_t control_code, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len); +int io_control(handle_t file, uint32_t control_code, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len); /** * @brief Configure a UART device * * @param[in] file The UART handle * @param[in] baud_rate The baud rate - * @param[in] data_width The databits width - * @param[in] stopbit The stopbit selection + * @param[in] databits The databits width (5-8) + * @param[in] stopbits The stopbits selection * @param[in] parity The parity selection */ -void uart_config(uintptr_t file, size_t baud_rate, size_t data_width, uart_stopbit stopbit, uart_parity parity); +void uart_config(handle_t file, uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity); /** * @brief Get the pin count of a GPIO controller @@ -107,25 +102,25 @@ void uart_config(uintptr_t file, size_t baud_rate, size_t data_width, uart_stopb * * @return The pin count */ -size_t gpio_get_pin_count(uintptr_t file); +uint32_t gpio_get_pin_count(handle_t file); /** * @brief Set the drive mode of a GPIO pin * * @param[in] file The GPIO controller handle * @param[in] pin The GPIO pin - * @param[in] stopbit The drive mode selection + * @param[in] mode The drive mode selection */ -void gpio_set_drive_mode(uintptr_t file, size_t pin, gpio_drive_mode mode); +void gpio_set_drive_mode(handle_t file, uint32_t pin, gpio_drive_mode_t mode); /** * @brief Set the edge trigger mode of a GPIO pin * * @param[in] file The GPIO controller handle * @param[in] pin The GPIO pin - * @param[in] stopbit The edge trigger mode selection + * @param[in] edge The edge trigger mode selection */ -void gpio_set_pin_edge(uintptr_t file, size_t pin, gpio_pin_edge edge); +void gpio_set_pin_edge(handle_t file, uint32_t pin, gpio_pin_edge_t edge); /** * @brief Set the changed handler of a GPIO pin @@ -135,7 +130,7 @@ void gpio_set_pin_edge(uintptr_t file, size_t pin, gpio_pin_edge edge); * @param[in] callback The changed handler * @param[in] userdata The userdata of the handler */ -void gpio_set_onchanged(uintptr_t file, size_t pin, gpio_onchanged callback, void* userdata); +void gpio_set_on_changed(handle_t file, uint32_t pin, gpio_on_changed_t callback, void *userdata); /** * @brief Get the value of a GPIO pin @@ -145,7 +140,7 @@ void gpio_set_onchanged(uintptr_t file, size_t pin, gpio_onchanged callback, voi * * @return The value of the pin */ -gpio_pin_value gpio_get_pin_value(uintptr_t file, size_t pin); +gpio_pin_value_t gpio_get_pin_value(handle_t file, uint32_t pin); /** * @brief Set the value of a GPIO pin @@ -154,7 +149,7 @@ gpio_pin_value gpio_get_pin_value(uintptr_t file, size_t pin); * @param[in] pin The GPIO pin * @param[in] value The value to be set */ -void gpio_set_pin_value(uintptr_t file, size_t pin, gpio_pin_value value); +void gpio_set_pin_value(handle_t file, uint32_t pin, gpio_pin_value_t value); /** * @brief Register and open a I2C device @@ -163,11 +158,11 @@ void gpio_set_pin_value(uintptr_t file, size_t pin, gpio_pin_value value); * @param[in] name Specify the path to access the device * @param[in] slave_address The address of slave * @param[in] address_width The bits width of address - * @param[in] bus_speed_mode The bus speed mode selection + * @param[in] bus_speed_mode The bus clock_rate mode selection * * @return The I2C device handle */ -uintptr_t i2c_get_device(uintptr_t file, const char* name, size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode); +handle_t i2c_get_device(handle_t file, const char *name, uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode); /** * @brief Write to then read from a I2C device @@ -180,7 +175,7 @@ uintptr_t i2c_get_device(uintptr_t file, const char* name, size_t slave_address, * * @return Actual bytes read */ -int i2c_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len); +int i2c_dev_transfer_sequential(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len); /** * @brief Configure a I2C controller with slave mode @@ -188,10 +183,10 @@ int i2c_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t * @param[in] file The I2C controller handle * @param[in] slave_address The address of slave * @param[in] address_width The bits width of address - * @param[in] bus_speed_mode The bus speed mode selection + * @param[in] bus_speed_mode The bus clock_rate mode selection * @param[in] handler The slave handler */ -void i2c_config_as_slave(uintptr_t file, size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, i2c_slave_handler* handler); +void i2c_config_as_slave(handle_t file, uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, i2c_slave_handler_t *handler); /** * @brief Configure a I2S controller with render mode @@ -202,10 +197,10 @@ void i2c_config_as_slave(uintptr_t file, size_t slave_address, size_t address_wi * @param[in] align_mode The I2S align mode selection * @param[in] channels_mask The channels selection mask */ -void i2s_config_as_render(uintptr_t file, const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask); +void i2s_config_as_render(handle_t file, const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask); /** - * @brief Configure a I2S controller with render mode + * @brief Configure a I2S controller with capture mode * * @param[in] file The I2S controller handle * @param[in] format The audio format @@ -213,7 +208,7 @@ void i2s_config_as_render(uintptr_t file, const audio_format_t* format, size_t d * @param[in] align_mode The I2S align mode selection * @param[in] channels_mask The channels selection mask */ -void i2s_config_as_capture(uintptr_t file, const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask); +void i2s_config_as_capture(handle_t file, const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask); /** * @brief Get the audio buffer of a I2S controller @@ -222,7 +217,7 @@ void i2s_config_as_capture(uintptr_t file, const audio_format_t* format, size_t * @param[out] buffer The address of audio buffer * @param[out] frames The available frames count in buffer */ -void i2s_get_buffer(uintptr_t file, char** buffer, size_t* frames); +void i2s_get_buffer(handle_t file, uint8_t **buffer, size_t *frames); /** * @brief Release the audio buffer of a I2S controller @@ -230,21 +225,21 @@ void i2s_get_buffer(uintptr_t file, char** buffer, size_t* frames); * @param[in] file The I2S controller handle * @param[out] frames The frames have been confirmed read or written */ -void i2s_release_buffer(uintptr_t file, size_t frames); +void i2s_release_buffer(handle_t file, size_t frames); /** * @brief Start rendering or recording of a I2S controller * * @param[in] file The I2S controller handle */ -void i2s_start(uintptr_t file); +void i2s_start(handle_t file); /** * @brief Stop rendering or recording of a I2S controller * * @param[in] file The I2S controller handle */ -void i2s_stop(uintptr_t file); +void i2s_stop(handle_t file); /** * @brief Register and open a SPI device @@ -253,33 +248,33 @@ void i2s_stop(uintptr_t file); * @param[in] name Specify the path to access the device * @param[in] mode The SPI mode selection * @param[in] frame_format The SPI frame format selection - * @param[in] chip_select_line The CS mask + * @param[in] chip_select_mask The CS mask * @param[in] data_bit_length The length of data bits * * @return The SPI device handle */ -uintptr_t spi_get_device(uintptr_t file, const char* name, spi_mode mode, spi_frame_format frame_format, size_t chip_select_line, size_t data_bit_length); +handle_t spi_get_device(handle_t file, const char *name, spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length); /** * @brief Configure a SPI device with non-standard mode * - * @param[in] file The SPI device handle - * @param[in] instruction_length The length of instruction - * @param[in] address_length The length of address - * @param[in] wait_cycles The wait cycles - * @param[in] spi_addr_inst_trans_mode The transmition mode of address and instruction + * @param[in] file The SPI device handle + * @param[in] instruction_length The length of instruction + * @param[in] address_length The length of address + * @param[in] wait_cycles The wait cycles + * @param[in] trans_mode The transmition mode of instruction and address */ -void spi_dev_config(uintptr_t file, size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode); +void spi_dev_config_non_standard(handle_t file, uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode); /** - * @brief Set the speed of a SPI device + * @brief Set the clock_rate of a SPI device * - * @param[in] file The SPI device handle - * @param[in] speed The desired speed in Hz + * @param[in] file The SPI device handle + * @param[in] clock_rate The desired clock rate in Hz * - * @return The actual speed after set + * @return The actual clock rate after set */ -double spi_dev_set_speed(uintptr_t file, double speed); +double spi_dev_set_clock_rate(handle_t file, double clock_rate); /** * @brief Transfer data between a SPI device using full duplex @@ -292,7 +287,7 @@ double spi_dev_set_speed(uintptr_t file, double speed); * * @return Actual bytes read */ -int spi_dev_transfer_full_duplex(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len); +int spi_dev_transfer_full_duplex(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len); /** * @brief Write to then read from a SPI device @@ -305,7 +300,7 @@ int spi_dev_transfer_full_duplex(uintptr_t file, const char* write_buffer, size_ * * @return Actual bytes read */ -int spi_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len); +int spi_dev_transfer_sequential(handle_t file, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len); /** * @brief Fill a sequence of idential frame to a SPI device @@ -317,7 +312,7 @@ int spi_dev_transfer_sequential(uintptr_t file, const char* write_buffer, size_t * @param[in] value The value * @param[in] count THe count of frames */ -void spi_dev_fill(uintptr_t file, size_t instruction, size_t address, uint32_t value, size_t count); +void spi_dev_fill(handle_t file, uint32_t instruction, uint32_t address, uint32_t value, size_t count); /** * @brief Configure a DVP device @@ -327,14 +322,14 @@ void spi_dev_fill(uintptr_t file, size_t instruction, size_t address, uint32_t v * @param[in] height The frame height * @param[in] auto_enable Process frames automatically */ -void dvp_config(uintptr_t file, size_t width, size_t height, int auto_enable); +void dvp_config(handle_t file, uint32_t width, uint32_t height, int auto_enable); /** * @brief Enable to process of current frame * * @param[in] file The DVP device handle */ -void dvp_enable_frame(uintptr_t file); +void dvp_enable_frame(handle_t file); /** * @brief Get the count of outputs of a DVP device @@ -343,7 +338,7 @@ void dvp_enable_frame(uintptr_t file); * * @return The count of outputs */ -size_t dvp_get_output_num(uintptr_t file); +uint32_t dvp_get_output_num(handle_t file); /** * @brief Set or unset a signal to a DVP device @@ -352,7 +347,7 @@ size_t dvp_get_output_num(uintptr_t file); * @param[in] type The signal type * @param[in] value 1 is set, 0 is unset */ -void dvp_set_signal(uintptr_t file, dvp_signal_type type, int value); +void dvp_set_signal(handle_t file, dvp_signal_type_t type, int value); /** * @brief Enable or disable a output of a DVP device @@ -361,7 +356,7 @@ void dvp_set_signal(uintptr_t file, dvp_signal_type type, int value); * @param[in] index The output index * @param[in] enable 1 is enable, 0 is disable */ -void dvp_set_output_enable(uintptr_t file, size_t index, int enable); +void dvp_set_output_enable(handle_t file, uint32_t index, int enable); /** * @brief Set output attributes of a DVP device @@ -371,7 +366,7 @@ void dvp_set_output_enable(uintptr_t file, size_t index, int enable); * @param[in] format The output format * @param[out] output_buffer The output buffer */ -void dvp_set_output_attributes(uintptr_t file, size_t index, video_format format, void* output_buffer); +void dvp_set_output_attributes(handle_t file, uint32_t index, video_format_t format, void *output_buffer); /** * @brief Enable or disable a frame event of a DVP device @@ -380,16 +375,16 @@ void dvp_set_output_attributes(uintptr_t file, size_t index, video_format format * @param[in] event The frame event * @param[in] enable 1 is enable, 0 is disable */ -void dvp_set_frame_event_enable(uintptr_t file, video_frame_event event, int enable); +void dvp_set_frame_event_enable(handle_t file, dvp_frame_event_t event, int enable); /** * @brief Set the frame event handler of a DVP device * - * @param[in] file The DVP device handle - * @param[in] callback The event handler - * @param[in] callback_data The userdata of the event handler + * @param[in] file The DVP device handle + * @param[in] handler The event handler + * @param[in] userdata The userdata of the event handler */ -void dvp_set_on_frame_event(uintptr_t file, dvp_on_frame_event callback, void* callback_data); +void dvp_set_on_frame_event(handle_t file, dvp_on_frame_event_t handler, void *userdata); /** * @brief Register and open a SCCB device @@ -401,7 +396,7 @@ void dvp_set_on_frame_event(uintptr_t file, dvp_on_frame_event callback, void* c * * @return The SCCB device handle */ -uintptr_t sccb_get_device(uintptr_t file, const char* name, size_t slave_address, size_t address_width); +handle_t sccb_get_device(handle_t file, const char *name, uint32_t slave_address, uint32_t address_width); /** * @brief Read a byte from a SCCB device @@ -411,7 +406,7 @@ uintptr_t sccb_get_device(uintptr_t file, const char* name, size_t slave_address * * @return The byte read */ -uint8_t sccb_dev_read_byte(uintptr_t file, uint16_t reg_address); +uint8_t sccb_dev_read_byte(handle_t file, uint16_t reg_address); /** * @brief Write a byte to a SCCB device @@ -420,7 +415,7 @@ uint8_t sccb_dev_read_byte(uintptr_t file, uint16_t reg_address); * @param[in] reg_address The register address * @param[in] value The data byte */ -void sccb_dev_write_byte(uintptr_t file, uint16_t reg_address, uint8_t value); +void sccb_dev_write_byte(handle_t file, uint16_t reg_address, uint8_t value); /** * @brief Do 16bit quantized complex FFT @@ -431,50 +426,50 @@ void sccb_dev_write_byte(uintptr_t file, uint16_t reg_address, uint8_t value); * @param[in] input The input data * @param[out] output The output data */ -void fft_complex_uint16(fft_point point, fft_direction direction, uint32_t shifts_mask, const uint16_t* input, uint16_t* output); +void fft_complex_uint16(fft_direction_t direction, const uint64_t *input, size_t point, uint64_t *output); /** * @brief Do aes decrypt * -* @param[in] aes_in_data The aes input decrypt data -* @param[in] key_addr The aes key address -* @param[in] key_length The aes key length.16:AES_128 24:AES_192 32:AES_256 -* @param[in] gcm_iv The gcm iv address -* @param[in] iv_length The gcm iv length -* @param[in] aes_aad The gcm add address -* @param[in] add_size The gcm add length -* @param[in] cipher_mod The cipher mode. 00:AES_CIPHER_ECB 01:AES_CIPHER_CBC 10:AES_CIPHER_GCM -* @param[in] data_size The input data size -* @param[out] aes_out_data The output data -* @param[out] tag The gcm output tag +* @param[in] input_data The aes input decrypt data +* @param[in] input_data_len The input data size +* @param[in] input_key The aes key address +* @param[in] input_key_len The aes key length.16:AES_128 24:AES_192 32:AES_256 +* @param[in] iv The gcm iv address +* @param[in] iv_len The gcm iv length +* @param[in] gcm_add The gcm add address +* @param[in] gcm_add_len The gcm add length +* @param[in] cipher_mode The cipher mode. 00:AES_CIPHER_ECB 01:AES_CIPHER_CBC 10:AES_CIPHER_GCM +* @param[out] output_data The output data +* @param[out] gcm_tag The output tag */ -void aes_decrypt(aes_parameter* aes_param); +void aes_hard_decrypt(const aes_param_t *param); /** * @brief Do aes encrypt * - * @param[in] aes_in_data The aes input decrypt data - * @param[in] key_addr The aes key address - * @param[in] key_length The aes key length.16:AES_128 24:AES_192 32:AES_256 - * @param[in] gcm_iv The gcm iv address - * @param[in] iv_length The gcm iv length - * @param[in] aes_aad The gcm add address - * @param[in] add_size The gcm add length - * @param[in] cipher_mod The cipher mode. 00:AES_CIPHER_ECB 01:AES_CIPHER_CBC 10:AES_CIPHER_GCM - * @param[in] data_size The input data size - * @param[out] aes_out_data The output data - * @param[out] tag The output tag + * @param[in] input_data The aes input encrypt data + * @param[in] input_data_len The input data size + * @param[in] input_key The aes key address + * @param[in] input_key_len The aes key length.16:AES_128 24:AES_192 32:AES_256 + * @param[in] iv The gcm iv address + * @param[in] iv_len The gcm iv length + * @param[in] gcm_add The gcm add address + * @param[in] gcm_add_len The gcm add length + * @param[in] cipher_mode The cipher mode. 00:AES_CIPHER_ECB 01:AES_CIPHER_CBC 10:AES_CIPHER_GCM + * @param[out] output_data The output data + * @param[out] gcm_tag The output tag */ -void aes_encrypt(aes_parameter* aes_param); +void aes_hard_encrypt(const aes_param_t *param); /** * @brief Do sha256 * - * @param[in] str The sha256 string - * @param[in] length The string length - * @param[out] hash The sha256 result + * @param[in] input The sha256 data + * @param[in] input_len The data length + * @param[out] output The sha256 result */ -void sha256_str(const char* str, size_t length, uint8_t* hash); +void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output); /** * @brief Set the interval of a TIMER device @@ -482,18 +477,18 @@ void sha256_str(const char* str, size_t length, uint8_t* hash); * @param[in] file The TIMER controller handle * @param[in] nanoseconds The desired interval in nanoseconds * - * @return The actual interval + * @return The actual interval in nanoseconds */ -size_t timer_set_interval(uintptr_t file, size_t nanoseconds); +size_t timer_set_interval(handle_t file, size_t nanoseconds); /** * @brief Set the tick handler of a TIMER device * * @param[in] file The TIMER controller handle - * @param[in] ontick The tick handler - * @param[in] ontick_data The userdata of the handler + * @param[in] on_tick The tick handler + * @param[in] userdata The userdata of the handler */ -void timer_set_ontick(uintptr_t file, timer_ontick ontick, void* ontick_data); +void timer_set_on_tick(handle_t file, timer_on_tick_t on_tick, void *userdata); /** * @brief Enable or disable a TIMER device @@ -501,7 +496,7 @@ void timer_set_ontick(uintptr_t file, timer_ontick ontick, void* ontick_data); * @param[in] file The TIMER controller handle * @param[in] enable 1 is enable, 0 is disable */ -void timer_set_enable(uintptr_t file, int enable); +void timer_set_enable(handle_t file, int enable); /** * @brief Get the pin count of a PWM controller @@ -510,7 +505,7 @@ void timer_set_enable(uintptr_t file, int enable); * * @return The pin count */ -size_t pwm_get_pin_count(uintptr_t file); +uint32_t pwm_get_pin_count(handle_t file); /** * @brief Set the frequency of a PWM controller @@ -520,7 +515,7 @@ size_t pwm_get_pin_count(uintptr_t file); * * @return The actual frequency after set */ -double pwm_set_frequency(uintptr_t file, double frequency); +double pwm_set_frequency(handle_t file, double frequency); /** * @brief Set the active duty cycle percentage of a PWM pin @@ -531,7 +526,7 @@ double pwm_set_frequency(uintptr_t file, double frequency); * * @return The actual active duty cycle percentage after set */ -double pwm_set_active_duty_cycle_percentage(uintptr_t file, size_t pin, double duty_cycle_percentage); +double pwm_set_active_duty_cycle_percentage(handle_t file, uint32_t pin, double duty_cycle_percentage); /** * @brief Enable or disable a PWM pin @@ -540,7 +535,49 @@ double pwm_set_active_duty_cycle_percentage(uintptr_t file, size_t pin, double d * @param[in] pin The PWM pin * @param[in] enable 1 is enable, 0 is disable */ -void pwm_set_enable(uintptr_t file, size_t pin, int enable); +void pwm_set_enable(handle_t file, uint32_t pin, int enable); + +/** + * @brief Set the response mode of a WDT device + * + * @param[in] file The WDT device handle + * @param[in] mode The response mode + */ +void wdt_set_response_mode(handle_t file, wdt_response_mode_t mode); + +/** + * @brief Set the timeout of a WDT device + * + * @param[in] file The WDT device handle + * @param[in] nanoseconds The desired timeout in nanoseconds + * + * @return The actual timeout in nanoseconds + */ +size_t wdt_set_timeout(handle_t file, size_t nanoseconds); + +/** + * @brief Set the timeout handler of a WDT device + * + * @param[in] file The WDT device handle + * @param[in] handler The timeout handler + * @param[in] userdata The userdata of the handler + */ +void wdt_set_on_timeout(handle_t file, wdt_on_timeout_t handler, void *userdata); + +/** + * @brief Restart the counter a WDT device + * + * @param[in] file The WDT device handle + */ +void wdt_restart_counter(handle_t file); + +/** + * @brief Enable or disable a WDT device + * + * @param[in] file The WDT device handle + * @param[in] enable 1 is enable, 0 is disable + */ +void wdt_set_enable(handle_t file, int enable); /** * @brief Get the datetime of a RTC device @@ -548,7 +585,7 @@ void pwm_set_enable(uintptr_t file, size_t pin, int enable); * @param[in] file The RTC device * @param[out] datetime The datatime */ -void rtc_get_datetime(uintptr_t file, datetime_t* datetime); +void rtc_get_datetime(handle_t file, struct tm *datetime); /** * @brief Set the datetime of a RTC device @@ -556,7 +593,7 @@ void rtc_get_datetime(uintptr_t file, datetime_t* datetime); * @param[in] file The RTC device * @param[out] datetime The datatime to be set */ -void rtc_set_datetime(uintptr_t file, const datetime_t* datetime); +void rtc_set_datetime(handle_t file, const struct tm *datetime); #ifdef __cplusplus } diff --git a/lib/freertos/device/driver.h b/lib/freertos/device/driver.h index 54f4a08..cf09e9c 100644 --- a/lib/freertos/device/driver.h +++ b/lib/freertos/device/driver.h @@ -19,18 +19,21 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #endif -typedef struct tag_driver_base +typedef uintptr_t handle_t; + +typedef struct _driver_base { - void* userdata; - void (*install)(void* userdata); - int (*open)(void* userdata); - void (*close)(void* userdata); + void *userdata; + void (*install)(void *userdata); + int (*open)(void *userdata); + void (*close)(void *userdata); } driver_base_t; typedef enum @@ -50,6 +53,7 @@ typedef enum DRIVER_SHA256, DRIVER_TIMER, DRIVER_PWM, + DRIVER_WDT, DRIVER_RTC, DRIVER_PIC, DRIVER_DMAC, @@ -59,260 +63,247 @@ typedef enum typedef struct tag_driver_registry { - const char* name; - const void* driver; + const char *name; + const void *driver; driver_type type; } driver_registry_t; -typedef enum +typedef enum _uart_stopbits { UART_STOP_1, UART_STOP_1_5, UART_STOP_2 -} uart_stopbit; +} uart_stopbits_t; -typedef enum +typedef enum _uart_parity { UART_PARITY_NONE, UART_PARITY_ODD, UART_PARITY_EVEN -} uart_parity; +} uart_parity_t; typedef struct tag_uart_driver { driver_base_t base; - void (*config)(size_t baud_rate, size_t data_width, uart_stopbit stopbit, uart_parity parity, void* userdata); - int (*read)(char* buffer, size_t len, void* userdata); - int (*write)(const char* buffer, size_t len, void* userdata); + void (*config)(uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity, void *userdata); + int (*read)(uint8_t *buffer, size_t len, void *userdata); + int (*write)(const uint8_t *buffer, size_t len, void *userdata); } uart_driver_t; -typedef enum +typedef enum _gpio_drive_mode { GPIO_DM_INPUT, GPIO_DM_INPUT_PULL_DOWN, GPIO_DM_INPUT_PULL_UP, - GPIO_DM_OUTPUT, - GPIO_DM_OUTPUT_OPEN_DRAIN, - GPIO_DM_OUTPUT_OPEN_DRAIN_PULL_UP, - GPIO_DM_OUTPUT_OPEN_SOURCE, - GPIO_DM_OUTPUT_OPEN_SOURCE_PULL_DOWN -} gpio_drive_mode; + GPIO_DM_OUTPUT +} gpio_drive_mode_t; -typedef enum +typedef enum _gpio_pin_edge { GPIO_PE_NONE, GPIO_PE_FALLING, GPIO_PE_RISING, GPIO_PE_BOTH -} gpio_pin_edge; +} gpio_pin_edge_t; -typedef enum +typedef enum _gpio_pin_value { GPIO_PV_LOW, GPIO_PV_HIGH -} gpio_pin_value; +} gpio_pin_value_t; -typedef void (*gpio_onchanged)(size_t pin, void* userdata); +typedef void (*gpio_on_changed_t)(uint32_t pin, void *userdata); typedef struct tag_gpio_driver { driver_base_t base; - size_t pin_count; - void (*set_drive_mode)(void* userdata, size_t pin, gpio_drive_mode mode); - void (*set_pin_edge)(void* userdata, size_t pin, gpio_pin_edge edge); - void (*set_onchanged)(void* userdata, size_t pin, gpio_onchanged callback, void* callback_data); - void (*set_pin_value)(void* userdata, size_t pin, gpio_pin_value value); - gpio_pin_value (*get_pin_value)(void* userdata, size_t pin); + uint32_t pin_count; + void (*set_drive_mode)(uint32_t pin, gpio_drive_mode_t mode, void *userdata); + void (*set_pin_edge)(uint32_t pin, gpio_pin_edge_t edge, void *userdata); + void (*set_on_changed)(uint32_t pin, gpio_on_changed_t callback, void *callback_data, void *userdata); + void (*set_pin_value)(uint32_t pin, gpio_pin_value_t value, void *userdata); + gpio_pin_value_t (*get_pin_value)(uint32_t pin, void *userdata); } gpio_driver_t; typedef enum { - I2C_BS_STANDARD, - I2C_BS_FAST, - I2C_BS_HIGH_SPEED -} i2c_bus_speed_mode; + I2C_BS_STANDARD +} i2c_bus_speed_mode_t; typedef struct tag_i2c_device_driver { driver_base_t base; - int (*read)(char* buffer, size_t len, void* userdata); - int (*write)(const char* buffer, size_t len, void* userdata); - int (*transfer_sequential)(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); + int (*read)(uint8_t *buffer, size_t len, void *userdata); + int (*write)(const uint8_t *buffer, size_t len, void *userdata); + int (*transfer_sequential)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata); } i2c_device_driver_t; -typedef enum +typedef enum _i2c_event { I2C_EV_START, I2C_EV_RESTART, I2C_EV_STOP -} i2c_event; +} i2c_event_t; -typedef struct +typedef struct _i2c_slave_handler { void (*on_receive)(uint32_t data); uint32_t (*on_transmit)(); - void (*on_event)(i2c_event event); -} i2c_slave_handler; + void (*on_event)(i2c_event_t event); +} i2c_slave_handler_t; typedef struct tag_i2c_driver { driver_base_t base; - i2c_device_driver_t* (*get_device)(size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, void* userdata); - void (*config_as_slave)(size_t slave_address, size_t address_width, i2c_bus_speed_mode bus_speed_mode, i2c_slave_handler* handler, void* userdata); + i2c_device_driver_t * (*get_device)(uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, void *userdata); + void (*config_as_slave)(uint32_t slave_address, uint32_t address_width, i2c_bus_speed_mode_t bus_speed_mode, i2c_slave_handler_t *handler, void *userdata); } i2c_driver_t; -typedef enum -{ - I2S_AM_STANDARD, - I2S_AM_RIGHT, - I2S_AM_LEFT -} i2s_align_mode; - -typedef enum +typedef enum _audio_format_type { AUDIO_FMT_PCM -} audio_format_type; +} audio_format_type_t; -typedef struct tag_audio_format +typedef struct _audio_format { - audio_format_type type; - size_t bits_per_sample; - size_t sample_rate; - size_t channels; + audio_format_type_t type; + uint32_t bits_per_sample; + uint32_t sample_rate; + uint32_t channels; } audio_format_t; +typedef enum _i2s_align_mode +{ + I2S_AM_STANDARD, + I2S_AM_RIGHT, + I2S_AM_LEFT +} i2s_align_mode_t; + typedef struct tag_i2s_driver { driver_base_t base; - void (*config_as_render)(const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask, void* userdata); - void (*config_as_capture)(const audio_format_t* format, size_t delay_ms, i2s_align_mode align_mode, size_t channels_mask, void* userdata); - void (*get_buffer)(char** buffer, size_t* frames, void* userdata); - void (*release_buffer)(size_t frames, void* userdata); - void (*start)(void* userdata); - void (*stop)(void* userdata); + void (*config_as_render)(const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void *userdata); + void (*config_as_capture)(const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void *userdata); + void (*get_buffer)(uint8_t **buffer, size_t *frames, void *userdata); + void (*release_buffer)(size_t frames, void *userdata); + void (*start)(void *userdata); + void (*stop)(void *userdata); } i2s_driver_t; -typedef enum +typedef enum _spi_mode { - SPI_Mode_0, - SPI_Mode_1, - SPI_Mode_2, - SPI_Mode_3, -} spi_mode; + SPI_MODE_0, + SPI_MODE_1, + SPI_MODE_2, + SPI_MODE_3, +} spi_mode_t; -typedef enum +typedef enum _spi_frame_format { SPI_FF_STANDARD, SPI_FF_DUAL, SPI_FF_QUAD, SPI_FF_OCTAL -} spi_frame_format; +} spi_frame_format_t; -typedef enum +typedef enum _spi_inst_addr_trans_mode { SPI_AITM_STANDARD, SPI_AITM_ADDR_STANDARD, SPI_AITM_AS_FRAME_FORMAT -} spi_addr_inst_trans_mode; +} spi_inst_addr_trans_mode_t; typedef struct tag_spi_device_driver { driver_base_t base; - void (*config)(size_t instruction_length, size_t address_length, size_t wait_cycles, spi_addr_inst_trans_mode trans_mode, void* userdata); - double (*set_speed)(double speed, void* userdata); - int (*read)(char* buffer, size_t len, void* userdata); - int (*write)(const char* buffer, size_t len, void* userdata); - int (*transfer_full_duplex)(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); - int (*transfer_sequential)(const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); - void (*fill)(size_t instruction, size_t address, uint32_t value, size_t count, void* userdata); + void (*config)(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode, void *userdata); + double (*set_clock_rate)(double clock_rate, void *userdata); + int (*read)(uint8_t *buffer, size_t len, void *userdata); + int (*write)(const uint8_t *buffer, size_t len, void *userdata); + int (*transfer_full_duplex)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata); + int (*transfer_sequential)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata); + void (*fill)(uint32_t instruction, uint32_t address, uint32_t value, size_t count, void *userdata); } spi_device_driver_t; typedef struct tag_spi_driver { driver_base_t base; - spi_device_driver_t* (*get_device)(spi_mode mode, spi_frame_format frame_format, size_t chip_select_line, size_t data_bit_length, void* userdata); + spi_device_driver_t * (*get_device)(spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length, void *userdata); } spi_driver_t; -typedef enum +typedef enum _video_format { VIDEO_FMT_RGB565, - VIDEO_FMT_RGB24Planar -} video_format; + VIDEO_FMT_RGB24_PLANAR +} video_format_t; -typedef struct tag_sccb_device_driver -{ - driver_base_t base; - uint8_t (*read_byte)(uint16_t reg_address, void* userdata); - void (*write_byte)(uint16_t reg_address, uint8_t value, void* userdata); -} sccb_device_driver_t; - -typedef struct tag_sccb_driver -{ - driver_base_t base; - sccb_device_driver_t* (*get_device)(size_t slave_address, size_t address_width, void* userdata); -} sccb_driver_t; - -typedef enum +typedef enum _video_frame_event { VIDEO_FE_BEGIN, VIDEO_FE_END -} video_frame_event; +} dvp_frame_event_t; -typedef enum +typedef enum _dvp_signal_type { DVP_SIG_POWER_DOWN, DVP_SIG_RESET -} dvp_signal_type; +} dvp_signal_type_t; -typedef void (*dvp_on_frame_event)(video_frame_event event, void* userdata); +typedef void (*dvp_on_frame_event_t)(dvp_frame_event_t event, void *userdata); typedef struct tag_dvp_driver { driver_base_t base; - size_t output_num; - void (*config)(size_t width, size_t height, int auto_enable, void* userdata); + uint32_t output_num; + void (*config)(uint32_t width, uint32_t height, int auto_enable, void *userdata); void (*enable_frame)(void* userdata); - void (*set_signal)(dvp_signal_type type, int value, void* userdata); - void (*set_output_enable)(size_t index, int enable, void* userdata); - void (*set_output_attributes)(size_t index, video_format format, void* output_buffer, void* userdata); - void (*set_frame_event_enable)(video_frame_event event, int enable, void* userdata); - void (*set_on_frame_event)(dvp_on_frame_event callback, void* callback_data, void* userdata); + void (*set_signal)(dvp_signal_type_t type, int value, void *userdata); + void (*set_output_enable)(uint32_t index, int enable, void *userdata); + void (*set_output_attributes)(uint32_t index, video_format_t format, void *output_buffer, void *userdata); + void (*set_frame_event_enable)(dvp_frame_event_t event, int enable, void *userdata); + void (*set_on_frame_event)(dvp_on_frame_event_t callback, void *callback_data, void *userdata); } dvp_driver_t; -typedef struct +typedef struct tag_sccb_device_driver +{ + driver_base_t base; + uint8_t(*read_byte)(uint16_t reg_address, void *userdata); + void(*write_byte)(uint16_t reg_address, uint8_t value, void *userdata); +} sccb_device_driver_t; + +typedef struct tag_sccb_driver +{ + driver_base_t base; + sccb_device_driver_t* (*get_device)(uint32_t slave_address, uint32_t address_width, void *userdata); +} sccb_driver_t; + +typedef struct tag_fft_data { int16_t I1; int16_t R1; int16_t I2; int16_t R2; -} fft_data; +} fft_data_t; -typedef enum -{ - FFT_512, - FFT_256, - FFT_128, - FFT_64 -} fft_point; - -typedef enum +typedef enum tag_fft_direction { FFT_DIR_BACKWARD, FFT_DIR_FORWARD -} fft_direction; +} fft_direction_t; typedef struct tag_fft_driver { driver_base_t base; - void (*complex_uint16)(fft_point point, fft_direction direction, uint32_t shifts_mask, const uint16_t* input, uint16_t* output, void* userdata); + void (*complex_uint16)(fft_direction_t direction, const uint64_t *input, size_t point_num, uint64_t *output, void *userdata); } fft_driver_t; typedef enum { - AES_CIPHER_ECB = 0, - AES_CIPHER_CBC = 1, - AES_CIPHER_GCM = 2 -} aes_cipher_mod; + AES_ECB = 0, + AES_CBC = 1, + AES_GCM = 2, + AES_CIPHER_MAX +} aes_cipher_mode_t; typedef enum { @@ -323,90 +314,98 @@ typedef enum typedef enum { - AES_MODE_ENCRYPTION = 0, - AES_MODE_DECRYPTION = 1, -} aes_encrypt_sel; - -typedef struct tag_aes_parameter -{ - uint8_t* aes_in_data; - uint8_t* key_addr; - uint8_t key_length; - uint8_t* gcm_iv; - uint8_t iv_length; - uint8_t* aes_aad; - uint32_t add_size; - aes_cipher_mod cipher_mod; - uint32_t data_size; - uint8_t* aes_out_data; - uint8_t* tag; -} aes_parameter; + AES_HARD_ENCRYPTION = 0, + AES_HARD_DECRYPTION = 1, +} aes_encrypt_sel_t; + +typedef struct tag_aes_param +{ + uint8_t *input_data; + size_t input_data_len; + uint8_t *input_key; + size_t input_key_len; + uint8_t *iv; + size_t iv_len; + uint8_t* gcm_add; + size_t gcm_add_len; + aes_cipher_mode_t cipher_mode; + uint8_t *output_data; + uint8_t *gcm_tag; +} aes_param_t; typedef struct tag_aes_driver { driver_base_t base; - void (*decrypt)(aes_parameter* aes_param, void* userdata); - void (*encrypt)(aes_parameter* aes_param, void* userdata); + void (*hard_decrypt)(const aes_param_t *param, void *userdata); + void (*hard_encrypt)(const aes_param_t *param, void *userdata); } aes_driver_t; typedef struct tag_sha256_driver { driver_base_t base; - void (*sha_str)(const char* str, size_t length, uint8_t* hash, void* userdata); + void (*sha256_hard_calculate)(const uint8_t *input, size_t input_len, uint8_t *output, void *userdata); } sha256_driver_t; -typedef void (*timer_ontick)(void* userdata); +typedef void (*timer_on_tick_t)(void* userdata); typedef struct tag_timer_driver { driver_base_t base; - size_t (*set_interval)(size_t nanoseconds, void* userdata); - void (*set_ontick)(timer_ontick ontick, void* ontick_data, void* userdata); - void (*set_enable)(int enable, void* userdata); + size_t (*set_interval)(size_t nanoseconds, void *userdata); + void (*set_on_tick)(timer_on_tick_t on_tick, void *ontick_data, void *userdata); + void (*set_enable)(int enable, void *userdata); } timer_driver_t; typedef struct tag_pwm_driver { driver_base_t base; - size_t pin_count; - double (*set_frequency)(double frequency, void* userdata); - double (*set_active_duty_cycle_percentage)(size_t pin, double duty_cycle_percentage, void* userdata); - void (*set_enable)(size_t pin, int enable, void* userdata); + uint32_t pin_count; + double (*set_frequency)(double frequency, void *userdata); + double (*set_active_duty_cycle_percentage)(uint32_t pin, double duty_cycle_percentage, void *userdata); + void (*set_enable)(uint32_t pin, int enable, void *userdata); } pwm_driver_t; -typedef struct tag_datetime +typedef enum _wdt_response_mode +{ + WDT_RESP_RESET, + WDT_RESP_INTERRUPT +} wdt_response_mode_t; + +typedef int (*wdt_on_timeout_t)(void *userdata); + +typedef struct tag_wdt_driver { - uint32_t year; - uint32_t month; - uint32_t day; - uint32_t hour; - uint32_t minute; - uint32_t second; -} datetime_t; + driver_base_t base; + void (*set_response_mode)(wdt_response_mode_t mode, void *userdata); + size_t (*set_timeout)(size_t nanoseconds, void *userdata); + void (*set_on_timeout)(wdt_on_timeout_t handler, void *handler_userdata, void *userdata); + void (*restart_counter)(void *userdata); + void (*set_enable)(int enable, void *userdata); +} wdt_driver_t; typedef struct tag_rtc_driver { driver_base_t base; - void (*get_datetime)(datetime_t* datetime, void* userdata); - void (*set_datetime)(const datetime_t* datetime, void* userdata); + void (*get_datetime)(struct tm *datetime, void *userdata); + void (*set_datetime)(const struct tm *datetime, void *userdata); } rtc_driver_t; -typedef struct tag_custom_driver +typedef struct _custom_driver { driver_base_t base; - int (*io_control)(size_t control_code, const char* write_buffer, size_t write_len, char* read_buffer, size_t read_len, void* userdata); + int (*io_control)(uint32_t control_code, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata); } custom_driver_t; /* ===== internal drivers ======*/ -typedef void (*pic_irq_handler)(void* userdata); -void kernel_iface_pic_on_irq(size_t irq); +typedef void (*pic_irq_handler_t)(void *userdata); +void kernel_iface_pic_on_irq(uint32_t irq); typedef struct tag_pic_driver { driver_base_t base; - void (*set_irq_enable)(size_t irq, int enable, void* userdata); - void (*set_irq_priority)(size_t irq, size_t priority, void* userdata); + void (*set_irq_enable)(uint32_t irq, int enable, void *userdata); + void (*set_irq_priority)(uint32_t irq, uint32_t priority, void *userdata); } pic_driver_t; typedef struct tag_dmac_driver @@ -414,15 +413,15 @@ typedef struct tag_dmac_driver driver_base_t base; } dmac_driver_t; -typedef void (*dma_stage_completion_handler)(void* userdata); +typedef void (*dma_stage_completion_handler_t)(void *userdata); typedef struct tag_dma_driver { driver_base_t base; - void (*set_select_request)(uint32_t request, void* userdata); - void (*config)(uint32_t priority, void* userdata); - void (*transmit_async)(const volatile void* src, volatile void* dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event, void* userdata); - void (*loop_async)(const volatile void** srcs, size_t src_num, volatile void** dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler stage_completion_handler, void* stage_completion_handler_data, SemaphoreHandle_t completion_event, int* stop_signal, void* userdata); + void (*set_select_request)(uint32_t request, void *userdata); + void (*config)(uint32_t priority, void *userdata); + void (*transmit_async)(const volatile void *src, volatile void *dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event, void *userdata); + void (*loop_async)(const volatile void **srcs, size_t src_num, volatile void **dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void *stage_completion_handler_data, SemaphoreHandle_t completion_event, int *stop_signal, void *userdata); } dma_driver_t; extern driver_registry_t g_hal_drivers[]; diff --git a/lib/freertos/device/hal.h b/lib/freertos/device/hal.h index 3f5864c..33eca4b 100644 --- a/lib/freertos/device/hal.h +++ b/lib/freertos/device/hal.h @@ -24,13 +24,6 @@ extern "C" { #endif -#define MAX_IRQN 256 - -/** - * @brief Install HAL - */ -void install_hal(); - /** * @brief Set frequency of CPU * @param[in] frequency The desired frequency in Hz @@ -39,47 +32,54 @@ void install_hal(); */ uint32_t system_set_cpu_frequency(uint32_t frequency); +/** + * @brief Install a custom driver + * @param[in] name Specify the path to access it later + * @param[in] driver The driver info + */ +void system_install_custom_driver(const char *name, const custom_driver_t *driver); + /** * @brief Enable or disable IRQ * @param[in] irq IRQ number * @param[in] enable 1 is enable, 0 is disable */ -void pic_set_irq_enable(size_t irq, int enable); +void pic_set_irq_enable(uint32_t irq, int enable); /** - * @brief Set priority of IRQ + * @brief Set handler of IRQ * @param[in] irq IRQ number - * @param[in] priority The priority of IRQ + * @param[in] handler The handler function + * @param[in] userdata The userdata of the handler function */ -void pic_set_irq_priority(size_t irq, size_t priority); +void pic_set_irq_handler(uint32_t irq, pic_irq_handler_t handler, void *userdata); /** - * @brief Set handler of IRQ + * @brief Set priority of IRQ * @param[in] irq IRQ number - * @param[in] handler The handler function - * @param[in] userdata The userdata of the handler function + * @param[in] priority The priority of IRQ */ -void pic_set_irq_handler(size_t irq, pic_irq_handler handler, void* userdata); +void pic_set_irq_priority(uint32_t irq, uint32_t priority); /** * @brief Wait for a free DMA and open it * * @return The DMA handle */ -uintptr_t dma_open_free(); +handle_t dma_open_free(); /** * @brief Close DMA * @param[in] file The DMA handle */ -void dma_close(uintptr_t file); +void dma_close(handle_t file); /** - * @brief Set the request line of DMA + * @brief Set the request source of DMA * @param[in] file The DMA handle - * @param[in] request The request line number + * @param[in] request The request source number */ -void dma_set_select_request(uintptr_t file, uint32_t request); +void dma_set_request_source(handle_t file, uint32_t request); /** * @brief DMA asynchronously @@ -93,7 +93,7 @@ void dma_set_select_request(uintptr_t file, uint32_t request); * @param[in] burst_size Element count to transmit per request * @param[in] completion_event Event to signal when this transmition is completed */ -void dma_transmit_async(uintptr_t file, const volatile void* src, volatile void* dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event); +void dma_transmit_async(handle_t file, const volatile void *src, volatile void *dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event); /** * @brief DMA synchrnonously @@ -106,7 +106,7 @@ void dma_transmit_async(uintptr_t file, const volatile void* src, volatile void* * @param[in] count Element count to transmit * @param[in] burst_size Element count to transmit per request */ -void dma_transmit(uintptr_t file, const volatile void* src, volatile void* dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size); +void dma_transmit(handle_t file, const volatile void *src, volatile void *dest, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size); /** * @brief DMA loop asynchronously @@ -125,15 +125,7 @@ void dma_transmit(uintptr_t file, const volatile void* src, volatile void* dest, * @param[in] completion_event Event to signal when this transmition is completed * @param[in] stop_signal The address of signal indicating whether to stop the transmition, set to 1 to stop */ -void dma_loop_async(uintptr_t file, const volatile void** srcs, size_t src_num, volatile void** dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler stage_completion_handler, void* stage_completion_handler_data, SemaphoreHandle_t completion_event, int* stop_signal); - - -/** - * @brief Install a custom driver - * @param[in] name Specify the path to access it later - * @param[in] driver The driver info - */ -void install_custom_driver(const char* name, const custom_driver_t* driver); +void dma_loop_async(handle_t file, const volatile void **srcs, size_t src_num, volatile void **dests, size_t dest_num, int src_inc, int dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void *stage_completion_handler_data, SemaphoreHandle_t completion_event, int *stop_signal); #ifdef __cplusplus } diff --git a/lib/freertos/include/task.h b/lib/freertos/include/task.h index 986c07c..29553ee 100644 --- a/lib/freertos/include/task.h +++ b/lib/freertos/include/task.h @@ -233,6 +233,8 @@ is used in assert() statements. */ #define taskSCHEDULER_NOT_STARTED ( ( BaseType_t ) 1 ) #define taskSCHEDULER_RUNNING ( ( BaseType_t ) 2 ) +UBaseType_t uxTaskGetProcessorId(void); + /*----------------------------------------------------------- * TASK CREATION API diff --git a/lib/freertos/os_entry.c b/lib/freertos/os_entry.c index c3aaa8a..b138ab3 100644 --- a/lib/freertos/os_entry.c +++ b/lib/freertos/os_entry.c @@ -18,11 +18,10 @@ #include #include #include -#include "device/devices.h" -#include "device/hal.h" -#include "portable/portmacro.h" +#include #include #include +#include "portable/portmacro.h" typedef struct { diff --git a/lib/freertos/portable/portmacro.h b/lib/freertos/portable/portmacro.h index aacbdaf..9165731 100644 --- a/lib/freertos/portable/portmacro.h +++ b/lib/freertos/portable/portmacro.h @@ -136,6 +136,8 @@ void vPortAddNewTaskToReadyListAsync(UBaseType_t uxPsrId, void* pxNewTaskHandle) void vPortEnterCritical(void); void vPortExitCritical(void); +#define portGET_PROCESSOR_ID() uxPortGetProcessorId() + #define portDISABLE_INTERRUPTS() __asm volatile ( "csrc mstatus,8" ) #define portENABLE_INTERRUPTS() __asm volatile ( "csrs mstatus,8" ) #define portENTER_CRITICAL() vPortEnterCritical() diff --git a/lib/freertos/tasks.c b/lib/freertos/tasks.c index 0f2ac1e..b5458ae 100644 --- a/lib/freertos/tasks.c +++ b/lib/freertos/tasks.c @@ -590,6 +590,11 @@ static void prvAddNewTaskToReadyList( UBaseType_t xProcessorId, TCB_t *pxNewTCB /*-----------------------------------------------------------*/ + UBaseType_t uxTaskGetProcessorId(void) + { + return portGET_PROCESSOR_ID(); + } + #if( configSUPPORT_STATIC_ALLOCATION == 1 ) TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode, diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..b689670 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +*/ +!hello_world/ \ No newline at end of file