Changed to FREERTOS

This commit is contained in:
Martijn Scheepers
2024-11-18 14:47:02 +01:00
parent ea15946b27
commit 28859aadb5
40 changed files with 490 additions and 500 deletions

4
.gitmodules vendored Normal file
View File

@@ -0,0 +1,4 @@
[submodule "FreeRTOS-Kernel"]
path = FreeRTOS-Kernel
url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git
branch = main

View File

@@ -1,108 +0,0 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "pico/multicore.h"
#include "UserFeedback.h"
#include "RS485.h"
#include "BusReader.h"
#include "CardReader.h"
// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define CONSOLE_ID uart0
#define CONSOLE_BAUD_RATE 115200
#define CONSOLE_TX_PIN 0
#define CONSOLE_RX_PIN 1
#define READER_ID_PIN 2
Global::credentialRequest_t credentialRequest;
#define CARD_READY 42
void core1_cardreader()
{
printf("Core 1 BusReader start\r\n");
CardReader::init();
while (1)
{
if(spin_try_lock_unsafe(credentialRequest.sync_wait_lock))
{
bool result = CardReader::checkCard(&credentialRequest);
spin_unlock_unsafe(credentialRequest.sync_wait_lock);
if(result)
{
//printf("Card read oke\n");
multicore_fifo_push_blocking(CARD_READY);
// printf("UID: ");
// for (size_t i = 0; i < credentialRequest.uid.size; i++)
// {
// printf("0x%02X ", credentialRequest.uid.uidByte[i]);
// }
// printf("\r\n");
// printf("BlockData: ");
// for (uint8_t i = 0; i < 32; i++)
// {
// printf("0x%02X ", credentialRequest.blockData[i]);
// }
// printf("\r\n");
//sleep_ms(1000);
}
}
}
}
int main()
{
stdio_init_all();
uart_init(CONSOLE_ID, CONSOLE_BAUD_RATE);
gpio_set_function(CONSOLE_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(CONSOLE_RX_PIN, GPIO_FUNC_UART);
uart_puts(CONSOLE_ID, "Program Start\r\n");
gpio_init(READER_ID_PIN);
gpio_set_dir(READER_ID_PIN, GPIO_IN);
gpio_pull_up(READER_ID_PIN);
if(gpio_get(READER_ID_PIN))
{
Global::readerID = 0x01;
}
else
{
Global::readerID = 0x02;
}
printf("Reader ID: %d\n", Global::readerID);
RS485::init();
UserFeedback::init();
credentialRequest.sync_wait_lock = spin_lock_init(spin_lock_claim_unused(true));
multicore_launch_core1(core1_cardreader);
while (true)
{
if (multicore_fifo_get_status() & SIO_FIFO_ST_VLD_BITS)
{
if(multicore_fifo_pop_blocking() == CARD_READY)
{
//printf("Card Ready\n");
credentialRequest.cardReady = true;
spin_lock_unsafe_blocking(credentialRequest.sync_wait_lock);
BusReader::checkBus(&credentialRequest);
spin_unlock_unsafe(credentialRequest.sync_wait_lock);
}
}
else
{
BusReader::checkBus(&credentialRequest);
}
}
}

View File

@@ -1,7 +1,9 @@
# Generated Cmake Pico project file # Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13) cmake_minimum_required(VERSION 3.13)
# Set CMake build type. Debug, Release, MinSizeRel, RelWithDebInfo
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -28,6 +30,13 @@ set(PICO_BOARD pico CACHE STRING "Board type")
# Pull in Raspberry Pi Pico SDK (must be before project) # Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake) include(pico_sdk_import.cmake)
# Pull in FreeRTOS
#set(FREERTOS_PORT GCC_RP2040 CACHE STRING "")
set(FREERTOS_KERNEL_PATH ${CMAKE_SOURCE_DIR}/FreeRTOS-Kernel)
include(FreeRTOS_Kernel_import.cmake)
project(ACS_Card_Reader C CXX ASM) project(ACS_Card_Reader C CXX ASM)
add_compile_options( add_compile_options(
@@ -36,7 +45,7 @@ add_compile_options(
-Wpedantic -Wpedantic
-fanalyzer -fanalyzer
-Wdouble-promotion -Wdouble-promotion
-Wnon-virtual-dtor #-Wnon-virtual-dtor
-Wcast-align -Wcast-align
-Wunused -Wunused
@@ -49,13 +58,17 @@ pico_sdk_init()
# Add executable. Default name is the project name, version 0.1 # Add executable. Default name is the project name, version 0.1
add_executable(ACS_Card_Reader add_executable(ACS_Card_Reader
ACS_Card_Reader.cpp src/ACS_Card_Reader.cpp
UserFeedback.cpp src/UserFeedback.cpp
BusReader.cpp src/BusReader.cpp
CardReader.cpp src/CardReader.cpp
Global.cpp src/Global.cpp
) src/RS485.cpp
src/pwm-tone.c
src/rc522/MFRC522.cpp
src/rc522/Desfire.cpp
src/rc522/MFRC522Extended.cpp
)
pico_set_program_name(ACS_Card_Reader "ACS_Card_Reader") pico_set_program_name(ACS_Card_Reader "ACS_Card_Reader")
pico_set_program_version(ACS_Card_Reader "0.1") pico_set_program_version(ACS_Card_Reader "0.1")
@@ -66,31 +79,24 @@ pico_enable_stdio_usb(ACS_Card_Reader 0)
# Add the standard library to the build # Add the standard library to the build
target_link_libraries(ACS_Card_Reader target_link_libraries(ACS_Card_Reader
pico_stdlib pico_stdlib
pico_multicore #pico_async_context_freertos
hardware_spi FreeRTOS-Kernel
hardware_pwm FreeRTOS-Kernel-Heap1
rc522 #FreeRTOS-Kernel-Heap4
pwm_tone hardware_spi
readerProtocol hardware_pwm
rs485 )
)
# Add the standard include files to the build # Add the standard include files to the build
target_include_directories(ACS_Card_Reader PRIVATE target_include_directories(ACS_Card_Reader PRIVATE
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required ${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/include/readerProtocol
${CMAKE_CURRENT_LIST_DIR}/include/rc522
) )
add_subdirectory(rc522)
add_subdirectory(pwm_tone)
add_subdirectory(readerProtocol)
add_subdirectory(rs485)
pico_add_extra_outputs(ACS_Card_Reader) pico_add_extra_outputs(ACS_Card_Reader)
# target_link_libraries(ACS_Card_Reader pico_stdlib rc522)
add_test(NAME test/test_hello_world.cpp COMMAND ExecutableToRun arg1 arg2 ...) add_test(NAME test/test_hello_world.cpp COMMAND ExecutableToRun arg1 arg2 ...)

1
FreeRTOS-Kernel Submodule

Submodule FreeRTOS-Kernel added at 7e419c2dd5

View File

@@ -0,0 +1,62 @@
# This is a copy of <FREERTOS_KERNEL_PATH>/portable/ThirdParty/GCC/RP2040/FREERTOS_KERNEL_import.cmake
# This can be dropped into an external project to help locate the FreeRTOS kernel
# It should be include()ed prior to project(). Alternatively this file may
# or the CMakeLists.txt in this directory may be included or added via add_subdirectory
# respectively.
if (DEFINED ENV{FREERTOS_KERNEL_PATH} AND (NOT FREERTOS_KERNEL_PATH))
set(FREERTOS_KERNEL_PATH $ENV{FREERTOS_KERNEL_PATH})
message("Using FREERTOS_KERNEL_PATH from environment ('${FREERTOS_KERNEL_PATH}')")
endif ()
set(FREERTOS_KERNEL_RP2040_RELATIVE_PATH "portable/ThirdParty/GCC/RP2040")
# undo the above
set(FREERTOS_KERNEL_RP2040_BACK_PATH "../../../..")
if (NOT FREERTOS_KERNEL_PATH)
# check if we are inside the FreeRTOS kernel tree (i.e. this file has been included directly)
get_filename_component(_ACTUAL_PATH ${CMAKE_CURRENT_LIST_DIR} REALPATH)
get_filename_component(_POSSIBLE_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH} REALPATH)
if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH)
get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH)
endif()
if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH)
get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH)
message("Setting FREERTOS_KERNEL_PATH to ${FREERTOS_KERNEL_PATH} based on location of FreeRTOS-Kernel-import.cmake")
elseif (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../FreeRTOS-Kernel")
set(FREERTOS_KERNEL_PATH ${PICO_SDK_PATH}/../FreeRTOS-Kernel)
message("Defaulting FREERTOS_KERNEL_PATH as sibling of PICO_SDK_PATH: ${FREERTOS_KERNEL_PATH}")
endif()
endif ()
if (NOT FREERTOS_KERNEL_PATH)
foreach(POSSIBLE_SUFFIX Source FreeRTOS-Kernel FreeRTOS/Source)
# check if FreeRTOS-Kernel exists under directory that included us
set(SEARCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}})
set(SEARCH_ROOT ../../../..)
get_filename_component(_POSSIBLE_PATH ${SEARCH_ROOT}/${POSSIBLE_SUFFIX} REALPATH)
if (EXISTS ${_POSSIBLE_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}/CMakeLists.txt)
get_filename_component(FREERTOS_KERNEL_PATH ${_POSSIBLE_PATH} REALPATH)
message("Setting FREERTOS_KERNEL_PATH to '${FREERTOS_KERNEL_PATH}' found relative to enclosing project")
break()
endif()
endforeach()
endif()
if (NOT FREERTOS_KERNEL_PATH)
message(FATAL_ERROR "FreeRTOS location was not specified. Please set FREERTOS_KERNEL_PATH.")
endif()
set(FREERTOS_KERNEL_PATH "${FREERTOS_KERNEL_PATH}" CACHE PATH "Path to the FreeRTOS Kernel")
get_filename_component(FREERTOS_KERNEL_PATH "${FREERTOS_KERNEL_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${FREERTOS_KERNEL_PATH})
message(FATAL_ERROR "Directory '${FREERTOS_KERNEL_PATH}' not found")
endif()
if (NOT EXISTS ${FREERTOS_KERNEL_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}/CMakeLists.txt)
message(FATAL_ERROR "Directory '${FREERTOS_KERNEL_PATH}' does not contain an RP2040 port here: ${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}")
endif()
set(FREERTOS_KERNEL_PATH ${FREERTOS_KERNEL_PATH} CACHE PATH "Path to the FreeRTOS_KERNEL" FORCE)
add_subdirectory(${FREERTOS_KERNEL_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH} FREERTOS_KERNEL)

View File

@@ -9,9 +9,10 @@
class BusReader class BusReader
{ {
public: public:
static void checkBus(Global::credentialRequest_t *credentialRequest); static void checkBus(Global::credentialRequest_t *credentialRequest, uint cardReady);
static void credentialNotification(Global::credentialRequest_t *credentialRequest); //static void credentialNotification(Global::credentialRequest_t *credentialRequest);
private: private:
static void credentialNotification(Global::credentialRequest_t *credentialRequest);
static void sendStatusResponse(RP::PDUMessage *message); static void sendStatusResponse(RP::PDUMessage *message);
static void handleIndicationRequest(RP::PDUMessage *message); static void handleIndicationRequest(RP::PDUMessage *message);
static void handleSetMifareKeys(RP::PDUMessage *message); static void handleSetMifareKeys(RP::PDUMessage *message);

156
include/FreeRTOSConfig.h Normal file
View File

@@ -0,0 +1,156 @@
/*
* FreeRTOS V202111.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html
*----------------------------------------------------------*/
/* Scheduler Related */
#define configUSE_PREEMPTION 1
#define configUSE_TICKLESS_IDLE 0
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES 32
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 512
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
/* Synchronization Related */
#define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configUSE_QUEUE_SETS 1
#define configUSE_TIME_SLICING 1
#define configUSE_NEWLIB_REENTRANT 0
// todo need this for lwip FreeRTOS sys_arch to compile
#define configENABLE_BACKWARD_COMPATIBILITY 1
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
/* System */
#define configSTACK_DEPTH_TYPE uint32_t
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
/* Memory allocation related definitions. */
#define configSUPPORT_STATIC_ALLOCATION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE (128*1024)
#define configAPPLICATION_ALLOCATED_HEAP 0
/* Hook function related definitions. */
#define configCHECK_FOR_STACK_OVERFLOW 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
/* Co-routine related definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES 1
/* Software timer related definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH 1024
/* Interrupt nesting behaviour configuration. */
/*
#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor]
#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application]
#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application]
*/
#if FREE_RTOS_KERNEL_SMP // set by the RP2040 SMP port of FreeRTOS
/* SMP port only */
#ifndef configNUMBER_OF_CORES
#define configNUMBER_OF_CORES 2
#endif
#define configNUM_CORES configNUMBER_OF_CORES
#define configTICK_CORE 0
#define configRUN_MULTIPLE_PRIORITIES 1
#if configNUMBER_OF_CORES > 1
#define configUSE_CORE_AFFINITY 1
#endif
#define configUSE_PASSIVE_IDLE_HOOK 0
#endif
/* RP2040 specific */
#define configSUPPORT_PICO_SYNC_INTEROP 1
#define configSUPPORT_PICO_TIME_INTEROP 1
#include <assert.h>
/* Define to trap errors during development. */
#define configASSERT(x) assert(x)
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_xTaskGetCurrentTaskHandle 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_xTaskGetIdleTaskHandle 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xTaskResumeFromISR 1
#define INCLUDE_xQueueGetMutexHolder 1
#if PICO_RP2350
#define configENABLE_MPU 0
#define configENABLE_TRUSTZONE 0
#define configRUN_FREERTOS_SECURE_ONLY 1
#define configENABLE_FPU 1
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 16
#endif
/* A header file that defines trace macro can be included here. */
#endif /* FREERTOS_CONFIG_H */

View File

@@ -5,7 +5,9 @@
#include <vector> #include <vector>
#include "RPEnums.h" #include "RPEnums.h"
#include "MFRC522.h" #include "MFRC522.h"
#include "pico/multicore.h" #include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
class Global class Global
{ {
@@ -16,8 +18,10 @@ public:
MFRC522::Uid uid; MFRC522::Uid uid;
uint8_t blockData[50]; uint8_t blockData[50];
uint8_t blockDataSize; uint8_t blockDataSize;
bool cardReady = false; //bool cardReady = false;
spin_lock_t* sync_wait_lock; //spin_lock_t* sync_wait_lock;
//bool sync_lock;
//SemaphoreHandle_t cardReady;
}; };
static RP::RPStatus readerStatus; static RP::RPStatus readerStatus;

View File

@@ -1,16 +0,0 @@
if (NOT TARGET pwm_tone)
add_library(pwm_tone INTERFACE)
target_sources(pwm_tone INTERFACE
${CMAKE_CURRENT_LIST_DIR}/pwm-tone.c
)
target_include_directories(pwm_tone INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(pwm_tone INTERFACE
pico_stdlib
hardware_pwm
)
endif()

View File

@@ -1,15 +0,0 @@
add_library(rc522
MFRC522.h
MFRC522.cpp
MFRC522Extended.h
MFRC522Extended.cpp
Desfire.h
Desfire.cpp
)
target_link_libraries(rc522
pico_stdlib
hardware_spi
)
target_include_directories(rc522 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

View File

@@ -1,20 +0,0 @@
// /**
// * Copyright (c) 2016 by Ludwig Grill (www.rotzbua.de)
// * Simple deprecated workaround for Arduino IDE
// * IDE 1.6.8 use gcc 4.8 which do not support c++14 [[deprecated]]
// * Later versions should support c++14, then use c++14 syntax
// */
// #ifndef DEPRECATED_H
// #define DEPRECATED_H
// #ifdef __has_cpp_attribute
// #if __has_cpp_attribute(deprecated)
// #define DEPRECATED [[deprecated]]
// #define DEPRECATED_MSG(msg) [[deprecated(msg)]]
// #endif // __has_cpp_attribute(deprecated)
// #else
// #define DEPRECATED __attribute__((deprecated))
// #define DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
// #endif // __has_cpp_attribute
// #endif // DEPRECATED_H

View File

@@ -1,12 +0,0 @@
// /**
// * Copyright (c) 2016 by Ludwig Grill (www.rotzbua.de)
// * Throws error if c++11 is not supported
// */
// #ifndef REQUIRE_CPP11_H
// #define REQUIRE_CPP11_H
// #if __cplusplus < 201103L
// #error "This library needs at least a C++11 compliant compiler, maybe compiler argument for C++11 support is missing or if you use Arduino IDE upgrade to version >=1.6.6"
// #endif
// #endif // REQUIRE_CPP11_H

View File

@@ -1,14 +0,0 @@
add_library(readerProtocol
CRC.h
RPDataTypes.h
RPDefines.h
RPEnums.h
RPPduStructures.h
)
target_link_libraries(readerProtocol
pico_stdlib
hardware_spi
)
target_include_directories(readerProtocol PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

View File

@@ -1,45 +0,0 @@
# cmake_minimum_required(VERSION 3.12)
# include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
# project(uart_rx C CXX ASM)
# set(CMAKE_C_STANDARD 11)
# set(CMAKE_CXX_STANDARD 17)
# pico_sdk_init()
# add_executable(${PROJECT_NAME}
# main.c
# uart_rx.c
# RS485.cpp
# )
# pico_generate_pio_header(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/uart_rx.pio)
# pico_add_extra_outputs(${PROJECT_NAME})
# target_link_libraries(${PROJECT_NAME}
# pico_stdlib
# hardware_irq
# hardware_pio
# hardware_dma
# readerProtocol
# )
# pico_enable_stdio_usb(${PROJECT_NAME} 1)
# pico_enable_stdio_uart(${PROJECT_NAME} 0)
add_library(rs485
RS485.h
RS485.cpp
dma_uart.cpp
)
target_link_libraries(rs485
pico_stdlib
hardware_uart
hardware_dma
readerProtocol
)
target_include_directories(rs485 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

View File

@@ -1,177 +0,0 @@
// #include <hardware/dma.h>
// #include <hardware/gpio.h>
// #include <hardware/irq.h>
// #include <hardware/uart.h>
// #include <string.h>
// #include <dma_uart.hpp>
// DmaUart::DmaUart(uart_inst_t* uart, uint baudrate)
// : uart_(uart),
// rx_user_index_(0),
// rx_dma_index_(0),
// tx_user_index_(0),
// tx_dma_index_(0) {
// init_uart(uart_, baudrate);
// init_dma();
// }
// void DmaUart::init_uart(uart_inst_t* uart, uint baudrate) {
// gpio_set_function(kUartTxPin, GPIO_FUNC_UART);
// gpio_set_function(kUartRxPin, GPIO_FUNC_UART);
// uart_init(uart, baudrate);
// }
// static void dma_irq_handler() {
// dma_hw->ints0 = 1u << kUartRxChannel;
// dma_channel_set_trans_count(kUartRxChannel, kRxBuffLength, true);
// }
// void DmaUart::init_dma() {
// /// DMA uart read
// dma_channel_config rx_config = dma_channel_get_default_config(kUartRxChannel);
// channel_config_set_transfer_data_size(&rx_config, DMA_SIZE_8);
// channel_config_set_read_increment(&rx_config, false);
// channel_config_set_write_increment(&rx_config, true);
// channel_config_set_ring(&rx_config, true, kRxBuffLengthPow);
// channel_config_set_dreq(&rx_config, DREQ_UART0_RX);
// channel_config_set_enable(&rx_config, true);
// dma_channel_configure(kUartRxChannel, &rx_config, rx_buffer_, &uart0_hw->dr,
// kRxBuffLength, true);
// dma_channel_set_irq0_enabled(kUartRxChannel, true);
// irq_set_exclusive_handler(DMA_IRQ_0, dma_irq_handler);
// irq_set_enabled(DMA_IRQ_0, true);
// /// DMA uart write
// dma_channel_config tx_config = dma_channel_get_default_config(kUartTxChannel);
// channel_config_set_transfer_data_size(&tx_config, DMA_SIZE_8);
// channel_config_set_read_increment(&tx_config, true);
// channel_config_set_write_increment(&tx_config, false);
// channel_config_set_ring(&tx_config, false, kTxBuffLengthPow);
// channel_config_set_dreq(&tx_config, DREQ_UART0_TX);
// dma_channel_set_config(kUartTxChannel, &tx_config, false);
// dma_channel_set_write_addr(kUartTxChannel, &uart0_hw->dr, false);
// }
// uint16_t DmaUart::write(const uint8_t* data, uint16_t length) {
// if (length == 0) {
// return 0;
// }
// uint16_t available =
// (tx_dma_index_ <= tx_user_index_)
// ? (kTxBuffLength - 1 + tx_dma_index_ - tx_user_index_)
// : (tx_dma_index_ - tx_user_index_);
// if (length <= available) {
// if (tx_dma_index_ < tx_user_index_) {
// if ((kTxBuffLength - 1) < tx_user_index_ + length) {
// memcpy(&tx_buffer_[tx_user_index_], data,
// kTxBuffLength - tx_user_index_);
// memcpy(tx_buffer_, &data[kTxBuffLength - tx_user_index_],
// length - (kTxBuffLength - tx_user_index_));
// } else {
// memcpy(&tx_buffer_[tx_user_index_], data, length);
// }
// } else {
// if ((kTxBuffLength - 1) < tx_user_index_ + length) {
// memcpy(&tx_buffer_[tx_user_index_], data,
// kTxBuffLength - tx_user_index_);
// memcpy(tx_buffer_, &data[kTxBuffLength - tx_user_index_],
// length - (kTxBuffLength - tx_user_index_));
// } else {
// memcpy(&tx_buffer_[tx_user_index_], data, length);
// }
// }
// tx_user_index_ = (tx_user_index_ + length) & (kTxBuffLength - 1);
// } else { // no enougth space to write
// // TODO: write as much data as possible
// return 0;
// }
// return length;
// }
// void DmaUart::flush() {
// uint size = (tx_dma_index_ < tx_user_index_)
// ? (tx_user_index_ - tx_dma_index_)
// : (kTxBuffLength + tx_user_index_ - tx_dma_index_);
// // Size check
// if (size == 0) {
// return;
// }
// // Busy check
// while (dma_channel_is_busy(kUartTxChannel))
// ;
// uint8_t* start = &tx_buffer_[tx_dma_index_];
// dma_channel_transfer_from_buffer_now(kUartTxChannel, start, size);
// tx_dma_index_ = (tx_dma_index_ + size) & (kTxBuffLength - 1);
// }
// void DmaUart::write_and_flush(const uint8_t* data, uint16_t length) {
// write(data, length);
// flush();
// }
// uint16_t DmaUart::read_byte(uint8_t* data) {
// // Update dma index
// rx_dma_index_ =
// kRxBuffLength - dma_channel_hw_addr(kUartRxChannel)->transfer_count;
// if (rx_dma_index_ == rx_user_index_) {
// return -1;
// }
// *data = rx_buffer_[rx_user_index_];
// rx_user_index_ = (rx_user_index_++) & (kRxBuffLength - 1);
// return 0;
// }
// uint16_t DmaUart::read(uint8_t* data, uint16_t length) {
// // Update DMA index
// rx_dma_index_ =
// kRxBuffLength - dma_channel_hw_addr(kUartRxChannel)->transfer_count;
// uint16_t available;
// available = (rx_user_index_ <= rx_dma_index_)
// ? (rx_dma_index_ - rx_user_index_)
// : (kTxBuffLength + rx_dma_index_ - rx_user_index_);
// if (length <= available) {
// if (rx_user_index_ < rx_dma_index_) {
// memcpy(data, &rx_buffer_[rx_user_index_], length);
// } else {
// uint16_t left = kRxBuffLength - rx_user_index_;
// memcpy(data, &rx_buffer_[rx_user_index_], left);
// memcpy(&data[left], rx_buffer_, length - left);
// }
// rx_user_index_ = (rx_user_index_ + length) & (kRxBuffLength - 1);
// } else {
// return 0;
// }
// return length;
// }
// uint16_t DmaUart::read_all(uint8_t* data) {
// // Update DMA index
// rx_dma_index_ =
// kRxBuffLength - dma_channel_hw_addr(kUartRxChannel)->transfer_count;
// uint16_t available;
// available = (rx_user_index_ <= rx_dma_index_)
// ? (rx_dma_index_ - rx_user_index_)
// : (kRxBuffLength + rx_dma_index_ - rx_user_index_);
// if (0 < available) {
// if (rx_user_index_ < rx_dma_index_) {
// memcpy(data, &rx_buffer_[rx_user_index_], available);
// } else {
// uint16_t left = kRxBuffLength - rx_user_index_;
// memcpy(data, &rx_buffer_[rx_user_index_], left);
// memcpy(&data[left], rx_buffer_, available - left);
// }
// rx_user_index_ = (rx_user_index_ + available) & (kRxBuffLength - 1);
// return available;
// }
// return 0;
// }

View File

@@ -1,40 +0,0 @@
// #pragma once
// #include <hardware/uart.h>
// constexpr uint8_t kRxBuffLengthPow = 5;
// constexpr uint8_t kTxBuffLengthPow = 5;
// constexpr uint16_t kRxBuffLength = 1 << (kRxBuffLengthPow);
// constexpr uint16_t kTxBuffLength = 1 << (kTxBuffLengthPow);
// constexpr int kUartRxChannel = 0;
// constexpr int kUartTxChannel = 1;
// constexpr int kUartTxPin = 0;
// constexpr int kUartRxPin = 1;
// class DmaUart {
// private:
// uart_inst_t* uart_;
// __attribute__((aligned(kRxBuffLength)))
// uint8_t rx_buffer_[kRxBuffLength];
// uint16_t rx_user_index_; // next index to read
// uint16_t rx_dma_index_; // next index dma will write
// __attribute__((aligned(kTxBuffLength)))
// uint8_t tx_buffer_[kTxBuffLength];
// uint16_t tx_user_index_; // next index to write
// uint16_t tx_dma_index_; // next index dma will read
// public:
// DmaUart(uart_inst_t* uart, uint baudrate);
// uint16_t write(const uint8_t* data, uint16_t length);
// void flush();
// void write_and_flush(const uint8_t* data, uint16_t length);
// uint16_t read_byte(uint8_t* data);
// uint16_t read(uint8_t* data, uint16_t length);
// uint16_t read_all(uint8_t* data);
// private:
// void init_uart(uart_inst_t* uart, uint baudrate);
// void init_dma();
// };

201
src/ACS_Card_Reader.cpp Normal file
View File

@@ -0,0 +1,201 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"
#include "hardware/uart.h"
#include "UserFeedback.h"
#include "RS485.h"
#include "BusReader.h"
#include "CardReader.h"
// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define CONSOLE_ID uart0
#define CONSOLE_BAUD_RATE 115200
#define CONSOLE_TX_PIN 0
#define CONSOLE_RX_PIN 1
#define READER_ID_PIN 2
Global::credentialRequest_t credentialRequest;
EventGroupHandle_t cardReaderEvents;
#define CARD_READY_BIT_0 ( 1 << 0 )
void vApplicationMallocFailedHook( void );
//void vApplicationIdleHook( void );
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
static void rfidReaderTask(void *pvParameters);
static void busReaderTask(void *pvParameters);
/*-----------------------------------------------------------*/
void vApplicationMallocFailedHook( void )
{
printf("vApplicationMallocFailedHook");
/* Called if a call to pvPortMalloc() fails because there is insufficient
free memory available in the FreeRTOS heap. pvPortMalloc() is called
internally by FreeRTOS API functions that create tasks, queues, software
timers, and semaphores. The size of the FreeRTOS heap is set by the
configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
/* Force an assert. */
configASSERT( ( volatile void * ) NULL );
}
/*-----------------------------------------------------------*/
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
( void ) pcTaskName;
( void ) pxTask;
printf("vApplicationStackOverflowHook");
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
function is called if a stack overflow is detected. */
/* Force an assert. */
configASSERT( ( volatile void * ) NULL );
}
/*-----------------------------------------------------------*/
// void vApplicationIdleHook( void )
// {
// volatile size_t xFreeHeapSpace;
// /* This is just a trivial example of an idle hook. It is called on each
// cycle of the idle task. It must *NOT* attempt to block. In this case the
// idle task just queries the amount of FreeRTOS heap that remains. See the
// memory management section on the http://www.FreeRTOS.org web site for memory
// management options. If there is a lot of heap memory free then the
// configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
// RAM. */
// xFreeHeapSpace = xPortGetFreeHeapSize();
// /* Remove compiler warning about xFreeHeapSpace being set but never used. */
// ( void ) xFreeHeapSpace;
// }
static void rfidReaderTask(void *pvParameters)
{
( void ) pvParameters;
printf("rfid Task start\r\n");
CardReader::init();
while (1)
{
bool result = CardReader::checkCard(&credentialRequest);
if(result)
{
printf("Card read oke\n");
xEventGroupSetBits(cardReaderEvents, CARD_READY_BIT_0);
// printf("UID: ");
// for (size_t i = 0; i < credentialRequest.uid.size; i++)
// {
// printf("0x%02X ", credentialRequest.uid.uidByte[i]);
// }
// printf("\r\n");
// printf("BlockData: ");
// for (uint8_t i = 0; i < 32; i++)
// {
// printf("0x%02X ", credentialRequest.blockData[i]);
// }
// printf("\r\n");
}
taskYIELD();
}
}
static void busReaderTask(void *pvParameters)
{
(void)pvParameters;
printf("busReader Task start\r\n");
while (1)
{
EventBits_t events = xEventGroupGetBits(cardReaderEvents);
if((events & CARD_READY_BIT_0))
{
xEventGroupClearBits(cardReaderEvents, CARD_READY_BIT_0);
printf("Card REady event\n");
BusReader::checkBus(&credentialRequest, 1);
}
else
{
BusReader::checkBus(&credentialRequest, 0);
}
taskYIELD();
}
}
int main()
{
stdio_init_all();
uart_init(CONSOLE_ID, CONSOLE_BAUD_RATE);
gpio_set_function(CONSOLE_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(CONSOLE_RX_PIN, GPIO_FUNC_UART);
uart_puts(CONSOLE_ID, "Program Start\r\n");
const char *rtos_name;
#if ( FREE_RTOS_KERNEL_SMP == 1 )
rtos_name = "FreeRTOS SMP";
#else
rtos_name = "FreeRTOS";
#endif
#if ( FREE_RTOS_KERNEL_SMP == 1 ) && ( configNUMBER_OF_CORES == 2 )
printf("%s on both cores:\n", rtos_name);
#endif
// #if ( mainRUN_ON_CORE == 1 )
// printf("%s on core 1:\n", rtos_name);
// multicore_launch_core1(vLaunch);
// while (true);
// #else
// printf("%s on core 0:\n", rtos_name);
// #endif
#if configNUMBER_OF_CORES > 1 && !defined(configUSE_CORE_AFFINITY)
#error async_context_freertos requires configUSE_CORE_AFFINITY under SMP
#endif
gpio_init(READER_ID_PIN);
gpio_set_dir(READER_ID_PIN, GPIO_IN);
gpio_pull_up(READER_ID_PIN);
if(gpio_get(READER_ID_PIN))
{
Global::readerID = 0x01;
}
else
{
Global::readerID = 0x02;
}
printf("Reader ID: %d\n", Global::readerID);
RS485::init();
UserFeedback::init();
cardReaderEvents = xEventGroupCreate();
xTaskCreate( rfidReaderTask, "rfidreader", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
xTaskCreate( busReaderTask, "busreader", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
vTaskStartScheduler();
while(1);
}

View File

@@ -81,7 +81,7 @@ void BusReader::credentialNotification(Global::credentialRequest_t *credentialRe
credentialRequest->uid.size = 0; credentialRequest->uid.size = 0;
credentialRequest->uid.sak = 0; credentialRequest->uid.sak = 0;
credentialRequest->cardReady = false; //credentialRequest->cardReady = false;
} }
std::string getParseErrorString(RP::RpParseErrors error) std::string getParseErrorString(RP::RpParseErrors error)
@@ -112,7 +112,7 @@ std::string getParseErrorString(RP::RpParseErrors error)
} }
} }
void BusReader::checkBus(Global::credentialRequest_t *credentialRequest) void BusReader::checkBus(Global::credentialRequest_t *credentialRequest, uint cardReady)
{ {
int len = RS485::dataReady(); int len = RS485::dataReady();
if(len != 0) if(len != 0)
@@ -137,7 +137,10 @@ void BusReader::checkBus(Global::credentialRequest_t *credentialRequest)
{ {
case RP::RPPduId::POLL_REQUEST: case RP::RPPduId::POLL_REQUEST:
//printf("POLL_REQUEST\n"); //printf("POLL_REQUEST\n");
if(credentialRequest->cardReady) //if(credentialRequest->cardReady)
//printf("cardReady:%d\n", cardReady);
if(cardReady == 1)
{ {
credentialNotification(credentialRequest); credentialNotification(credentialRequest);
} }

View File

@@ -100,6 +100,8 @@ uint8_t* RS485::receiveBuffer()
void RS485::transmit(std::vector<uint8_t> data) void RS485::transmit(std::vector<uint8_t> data)
{ {
//sleep_ms(1); //make space between bus messages
// printf("TX: "); // printf("TX: ");
// for (size_t i = 0; i < data.size(); i++) // for (size_t i = 0; i < data.size(); i++)
// { // {

View File

@@ -2,10 +2,9 @@
#include "UserFeedback.h" #include "UserFeedback.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "hardware/pwm.h" #include "hardware/pwm.h"
#include "pwm_tone/pwm-tone.h" #include "pwm-tone.h"
#include "pwm_tone/melodies.h" #include "melodies.h"
#include "pwm_tone/pitches.h" #include "pitches.h"
//#include "pwm_tone/pwm_tone.h"
// Create an instance of the tone generator // Create an instance of the tone generator
tonegenerator_t generator; tonegenerator_t generator;
@@ -13,7 +12,6 @@ tonegenerator_t generator;
bool breathingEnabled = true; bool breathingEnabled = true;
void on_pwm_wrap() { void on_pwm_wrap() {
static int fade = 0; static int fade = 0;
static bool going_up = true; static bool going_up = true;
@@ -42,8 +40,7 @@ void on_pwm_wrap() {
else else
{ {
pwm_set_gpio_level(RGB_LED_BLUE, 65535); pwm_set_gpio_level(RGB_LED_BLUE, 65535);
} }
} }
void UserFeedback::init() void UserFeedback::init()

View File

@@ -236,23 +236,23 @@ MFRC522::StatusCode MFRC522::PCD_CalculateCRC(uint8_t *data, uint8_t length, uin
*/ */
void MFRC522::PCD_Init() void MFRC522::PCD_Init()
{ {
bool hardReset = false; //bool hardReset = false;
//gpio_set_dir(RC522_PIN_RESET, GPIO_IN); //gpio_set_dir(RC522_PIN_RESET, GPIO_IN);
//if(gpio_get(RC522_PIN_RESET) == true) //if(gpio_get(RC522_PIN_RESET) == true)
//{ //{
printf("-- hard reset --\r\n"); printf("-- MFRC522 hard reset --\n");
gpio_set_dir(RC522_PIN_RESET, GPIO_OUT); gpio_set_dir(RC522_PIN_RESET, GPIO_OUT);
gpio_put(RC522_PIN_RESET, 0); gpio_put(RC522_PIN_RESET, 0);
sleep_us(2); sleep_us(2);
gpio_put(RC522_PIN_RESET, 1); gpio_put(RC522_PIN_RESET, 1);
sleep_ms(50); sleep_ms(50);
hardReset = true; //hardReset = true;
//} //}
if (!hardReset) { // Perform a soft reset if we haven't triggered a hard reset above. // if (!hardReset) { // Perform a soft reset if we haven't triggered a hard reset above.
PCD_Reset(); // PCD_Reset();
} // }
// Reset baud rates // Reset baud rates
PCD_WriteRegister(TxModeReg, 0x00); PCD_WriteRegister(TxModeReg, 0x00);